annotate python/arm_cm3.py @ 207:8b2f20aae086

cleaning of files
author Windel Bouwman
date Sat, 29 Jun 2013 10:05:42 +0200
parents 6c6bf8890d8a
children 62386bcee1ba
rev   line source
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
1 import struct, types
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
2 from target import Register, Instruction, Target, Imm8, Label, Imm3
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
3 from asmnodes import ASymbol, ANumber, AUnop, ABinop
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
4 from ppci import CompilerError
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
5 import ir
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
6
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
7 def u16(h):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
8 return struct.pack('<H', h)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
9
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
10 def u32(x):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
11 return struct.pack('<I', x)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
12
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
13 armtarget = Target('arm')
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
14
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
15 class ArmReg(Register):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
16 def __init__(self, num, name):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
17 super().__init__(name)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
18 self.num = num
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
19 def __repr__(self):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
20 return self.name
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
21
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
22 class RegOp:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
23 def __init__(self, num):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
24 assert num < 16
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
25 self.num = num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
26
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
27 @classmethod
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
28 def Create(cls, vop):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
29 if type(vop) is ASymbol:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
30 name = vop.name
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
31 regs = {}
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
32 for r in armtarget.registers:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
33 regs[r.name] = r
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
34 if name in regs:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
35 r = regs[name]
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
36 return cls(r.num)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
37
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
38 def getRegNum(n):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
39 for r in armtarget.registers:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
40 if r.num == n:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
41 return r
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
42
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
43 def getRegisterRange(n1, n2):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
44 regs = []
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
45 if n1.num < n2.num:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
46 for n in range(n1.num, n2.num + 1):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
47 r = getRegNum(n)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
48 assert r
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
49 regs.append(r)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
50 return regs
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
51
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
52 class RegisterSet:
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
53 def __init__(self, regs):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
54 assert type(regs) is set
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
55 self.regs = regs
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
56 def __repr__(self):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
57 return ','.join([str(r) for r in self.regs])
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
58 @classmethod
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
59 def Create(cls, vop):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
60 assert type(vop) is AUnop and vop.operation == '{}'
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
61 assert type(vop.arg) is list
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
62 regs = set()
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
63 for arg in vop.arg:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
64 if type(arg) is ASymbol:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
65 reg = RegOp.Create(arg)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
66 if not reg:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
67 return
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
68 regs.add(reg)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
69 elif type(arg) is ABinop and arg.op == '-':
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
70 reg1 = RegOp.Create(arg.arg1)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
71 reg2 = RegOp.Create(arg.arg2)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
72 if not reg1:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
73 return
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
74 if not reg2:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
75 return
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
76 for r in getRegisterRange(reg1, reg2):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
77 regs.add(r)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
78 else:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
79 raise Exception('Cannot be')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
80 return cls(regs)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
81
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
82 def registerNumbers(self):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
83 return [r.num for r in self.regs]
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
84
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
85 # 8 bit registers:
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
86 r0 = ArmReg(0, 'r0')
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
87 armtarget.registers.append(r0)
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
88 r1 = ArmReg(1, 'r1')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
89 armtarget.registers.append(r1)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
90 r2 = ArmReg(2, 'r2')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
91 armtarget.registers.append(r2)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
92 r3 = ArmReg(3, 'r3')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
93 armtarget.registers.append(r3)
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
94 r4 = ArmReg(4, 'r4')
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
95 armtarget.registers.append(r4)
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
96 r5 = ArmReg(5, 'r5')
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
97 armtarget.registers.append(r5)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
98 r6 = ArmReg(6, 'r6')
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
99 armtarget.registers.append(r6)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
100 r7 = ArmReg(7, 'r7')
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
101 armtarget.registers.append(r7)
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
102 # Other registers:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
103 # TODO
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
104 sp = ArmReg(13, 'sp')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
105 armtarget.registers.append(sp)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
106 lr = ArmReg(14, 'lr')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
107 armtarget.registers.append(lr)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
108 pc = ArmReg(15, 'pc')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
109 armtarget.registers.append(pc)
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
110
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
111 class ArmInstruction(Instruction):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
112 pass
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
113
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
114 class ldr_ins(ArmInstruction):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
115 mnemonic = 'ldr'
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
116 opcode = 1337
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
117 irpattern = 'todo'
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
118
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
119 class dcd_ins(ArmInstruction):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
120 mnemonic = 'dcd'
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
121 def __init__(self, expr):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
122 self.expr = expr
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
123 def encode(self):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
124 return u32(self.expr)
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
125
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
126 @armtarget.instruction
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
127 class mov_ins(ArmInstruction):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
128 """ mov Rd, imm8, move immediate value into register """
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
129 mnemonic = 'mov'
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
130 opcode = 4 # 00100 Rd(3) imm8
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
131 operands = (RegOp, Imm8)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
132 irpattern = ir.ImmLoad
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
133 def __init__(self, rd, imm):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
134 self.imm = imm.imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
135 self.r = rd.num
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
136
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
137 @classmethod
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
138 def FromIr(cls, ir_ins):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
139 pass
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
140
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
141 def encode(self):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
142 rd = self.r
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
143 opcode = self.opcode
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
144 imm8 = self.imm
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
145 h = (opcode << 11) | (rd << 8) | imm8
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
146 return u16(h)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
147
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
148 @armtarget.instruction
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
149 class movregreg_ins(ArmInstruction):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
150 """ mov Rd, Rm """
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
151 mnemonic = 'mov'
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
152 opcode = 8 # 01000 Rd(3) imm8
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
153 operands = (RegOp, RegOp)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
154 def __init__(self, rd, rm):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
155 self.rd = rd
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
156 self.rm = rm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
157 def encode(self):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
158 rd = self.rd.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
159 D = (rd & 0x8) >> 3
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
160 assert D < 2
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
161 rd = rd & 0x7
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
162 rm = self.rm.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
163 assert rm < 16
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
164 opcode = self.opcode
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
165 h = (opcode << 11) | (3 << 9) | (D << 7) | (rm << 3) | rd
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
166 return u16(h)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
167
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
168 @armtarget.instruction
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
169 class addregregimm3_ins(ArmInstruction):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
170 """ add Rd, Rn, imm3 """
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
171 mnemonic = 'add'
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
172 opcode = 3 # 00011
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
173 operands = (RegOp, RegOp, Imm3)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
174 irpattern = 3
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
175 def __init__(self, rd, rn, imm3):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
176 self.rd = rd
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
177 self.rn = rn
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
178 self.imm3 = imm3
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
179 def encode(self):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
180 rd = self.rd.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
181 rn = self.rn.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
182 imm3 = self.imm3.imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
183 opcode = self.opcode
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
184 h = (opcode << 11) | (1 << 10) | (imm3 << 6) | (rn << 3) | rd
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
185 return u16(h)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
186
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
187 @armtarget.instruction
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
188 class cmpregimm8_ins(ArmInstruction):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
189 """ cmp Rn, imm8 """
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
190 mnemonic = 'cmp'
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
191 opcode = 5 # 00101
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
192 operands = (RegOp, Imm8)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
193 def __init__(self, rn, imm):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
194 self.rn = rn
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
195 self.imm = imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
196 def encode(self):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
197 rn = self.rn.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
198 imm = self.imm.imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
199 opcode = self.opcode
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
200 h = (opcode << 11) | (rn << 8) | imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
201 return u16(h)
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
203 @armtarget.instruction
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
204 class jmp_ins(ArmInstruction):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
205 operands = (Label,)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
206 mnemonic = 'jmp'
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
207 def __init__(self, target_label):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
208 self.target = target_label
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
209 def fixUp(self):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
210 pass
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
211 def encode(self):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
212 h = 1337 # TODO
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
213 return u16(h)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
214
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
215 @armtarget.instruction
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
216 class push_ins(ArmInstruction):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
217 operands = (RegisterSet,)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
218 mnemonic = 'push'
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
219 def __init__(self, regs):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
220 print(self.operands)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
221 assert (type(regs),) == self.operands, (type(regs),)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
222 self.regs = regs
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
223 def __repr__(self):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
224 return '{0} {{{1}}}'.format(self.mnemonic, self.regs)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
225 def encode(self):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
226 reg_list = 0
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
227 M = 0
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
228 for n in self.regs.registerNumbers():
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
229 if n < 8:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
230 reg_list |= (1 << n)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
231 elif n == 14:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
232 M = 1
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
233 else:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
234 raise NotImplementedError('not implemented for this register')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
235 h = (0x5a << 9) | (M << 8) | reg_list
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
236 return u16(h)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
237
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
238 @armtarget.instruction
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
239 class pop_ins(ArmInstruction):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
240 operands = (RegisterSet,)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
241 mnemonic = 'pop'
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
242 def __init__(self, regs):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
243 self.regs = regs
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 206
diff changeset
244 def __repr__(self):
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 206
diff changeset
245 return '{0} {{{1}}}'.format(self.mnemonic, self.regs)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
246 def encode(self):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
247 reg_list = 0
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
248 P = 0
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
249 for n in self.regs.registerNumbers():
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
250 if n < 8:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
251 reg_list |= (1 << n)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
252 elif n == 15:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
253 P = 1
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
254 else:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
255 raise NotImplementedError('not implemented for this register')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
256 h = (0x5E << 9) | (P << 8) | reg_list
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
257 return u16(h)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
258 return u16(0)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
259
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
260 @armtarget.instruction
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
261 class yield_ins(ArmInstruction):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
262 operands = ()
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
263 mnemonic = 'yield'
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
264 def encode(self):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
265 return u16(0xbf10)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
266
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
267 armtarget.check()
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
268