annotate python/cortexm3.py @ 225:1c7364bd74c7

Fixed pointer deref
author Windel Bouwman
date Thu, 11 Jul 2013 07:42:30 +0200
parents 5af52987f5bd
children e621e3ba78d2
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
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
3 from asmnodes import ASymbol, ANumber, AUnop, ABinop, ALabel
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
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 216
diff changeset
7 # TODO: encode this in DSL (domain specific language)
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 216
diff changeset
8
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
9 def u16(h):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
10 return struct.pack('<H', h)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
11
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
12 def u32(x):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
13 return struct.pack('<I', x)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
14
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
15 armtarget = Target('arm')
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
16
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
17 class ArmReg(Register):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
18 def __init__(self, num, name):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
19 super().__init__(name)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
20 self.num = num
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
21 def __repr__(self):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
22 return self.name
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
23
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
24 class RegOp:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
25 def __init__(self, num):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
26 assert num < 16
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
27 self.num = num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
28
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
29 @classmethod
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
30 def Create(cls, vop):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
31 if type(vop) is ASymbol:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
32 name = vop.name
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
33 regs = {}
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
34 for r in armtarget.registers:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
35 regs[r.name] = r
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
36 if name in regs:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
37 r = regs[name]
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
38 return cls(r.num)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
39
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
40 class Reg8Op:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
41 def __init__(self, num):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
42 assert num < 8
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
43 self.num = num
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
44
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
45 @classmethod
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
46 def Create(cls, vop):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
47 if type(vop) is ASymbol:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
48 name = vop.name
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
49 regs = {}
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
50 for r in armtarget.registers:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
51 regs[r.name] = r
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
52 if name in regs:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
53 r = regs[name]
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
54 if r.num < 8:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
55 return cls(r.num)
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
56
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
57 def getRegNum(n):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
58 for r in armtarget.registers:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
59 if r.num == n:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
60 return r
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
61
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
62 def getRegisterRange(n1, n2):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
63 regs = []
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
64 if n1.num < n2.num:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
65 for n in range(n1.num, n2.num + 1):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
66 r = getRegNum(n)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
67 assert r
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
68 regs.append(r)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
69 return regs
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
70
224
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
71 def isRegOffset(regname, x, y):
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
72 if type(x) is ASymbol and type(y) is ANumber and x.name.upper() == regname:
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
73 return y.number
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
74 elif type(y) is ASymbol and type(x) is ANumber and y.name.upper() == regname:
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
75 return x.number
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
76
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
77
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
78 class MemRegXRel:
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
79 def __init__(self, offset):
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
80 assert offset % 4 == 0
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
81 self.offset = offset
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
82
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
83 def __repr__(self):
224
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
84 return '[{}, #{}]'.format(self.regname, self.offset)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
85
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
86 @classmethod
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
87 def Create(cls, vop):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
88 if type(vop) is AUnop and vop.operation == '[]':
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
89 vop = vop.arg # descent
224
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
90 offset = isRegOffset(cls.regname, vop.arg1, vop.arg2)
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
91 if type(offset) is int:
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
92 if offset % 4 == 0:
223
85c8105318e7 Fixup of parser
Windel Bouwman
parents: 219
diff changeset
93 offset = vop.arg2.number
85c8105318e7 Fixup of parser
Windel Bouwman
parents: 219
diff changeset
94 return cls(offset)
224
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
95 elif type(vop) is ASymbol and vop.name.upper() == self.regname:
223
85c8105318e7 Fixup of parser
Windel Bouwman
parents: 219
diff changeset
96 return cls(0)
85c8105318e7 Fixup of parser
Windel Bouwman
parents: 219
diff changeset
97
224
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
98 class MemSpRel(MemRegXRel):
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
99 regname = 'SP'
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
100
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
101 class MemPcRel(MemRegXRel):
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
102 regname = 'PC'
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
103
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 224
diff changeset
104 class MemR8Rel:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
105 def __init__(self, basereg, offset):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
106 assert type(basereg) is ArmReg
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
107 self.basereg = basereg
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
108 self.offset = offset
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
109
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
110 def __repr__(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
111 return '[{}, #{}]'.format(self.basereg, self.offset)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
112
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
113 @classmethod
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
114 def Create(cls, vop):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
115 if type(vop) is AUnop and vop.operation == '[]':
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
116 vop = vop.arg # descent
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
117 if type(vop) is ABinop:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
118 if vop.op == '+' and type(vop.arg1) is ASymbol and type(vop.arg2) is ANumber:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
119 offset = vop.arg2.number
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
120 if offset > 120:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
121 return
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
122 basereg = Reg8Op.Create(vop.arg1)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
123 if not basereg:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
124 return
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
125 else:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
126 return
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
127 elif type(vop) is ASymbol:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
128 offset = 0
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
129 basereg = Reg8Op.Create(vop)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
130 if not basereg:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
131 return
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
132 else:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
133 return
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
134 return cls(getRegNum(basereg.num), offset)
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
135
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
136 class RegisterSet:
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
137 def __init__(self, regs):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
138 assert type(regs) is set
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
139 self.regs = regs
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
140 def __repr__(self):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
141 return ','.join([str(r) for r in self.regs])
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
142 @classmethod
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
143 def Create(cls, vop):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
144 assert type(vop) is AUnop and vop.operation == '{}'
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
145 assert type(vop.arg) is list
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
146 regs = set()
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
147 for arg in vop.arg:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
148 if type(arg) is ASymbol:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
149 reg = RegOp.Create(arg)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
150 if not reg:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
151 return
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
152 regs.add(reg)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
153 elif type(arg) is ABinop and arg.op == '-':
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
154 reg1 = RegOp.Create(arg.arg1)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
155 reg2 = RegOp.Create(arg.arg2)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
156 if not reg1:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
157 return
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
158 if not reg2:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
159 return
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
160 for r in getRegisterRange(reg1, reg2):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
161 regs.add(r)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
162 else:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
163 raise Exception('Cannot be')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
164 return cls(regs)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
165
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
166 def registerNumbers(self):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
167 return [r.num for r in self.regs]
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
168
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
169 # 8 bit registers:
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
170 r0 = ArmReg(0, 'r0')
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
171 armtarget.registers.append(r0)
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
172 r1 = ArmReg(1, 'r1')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
173 armtarget.registers.append(r1)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
174 r2 = ArmReg(2, 'r2')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
175 armtarget.registers.append(r2)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
176 r3 = ArmReg(3, 'r3')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
177 armtarget.registers.append(r3)
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
178 r4 = ArmReg(4, 'r4')
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
179 armtarget.registers.append(r4)
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
180 r5 = ArmReg(5, 'r5')
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
181 armtarget.registers.append(r5)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
182 r6 = ArmReg(6, 'r6')
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
183 armtarget.registers.append(r6)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
184 r7 = ArmReg(7, 'r7')
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
185 armtarget.registers.append(r7)
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
186 # Other registers:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
187 # TODO
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
188 sp = ArmReg(13, 'sp')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
189 armtarget.registers.append(sp)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
190 lr = ArmReg(14, 'lr')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
191 armtarget.registers.append(lr)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
192 pc = ArmReg(15, 'pc')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
193 armtarget.registers.append(pc)
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
194
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
195 class ArmInstruction(Instruction):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
196 pass
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
197
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
198 class dcd_ins(ArmInstruction):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
199 mnemonic = 'dcd'
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
200 def __init__(self, expr):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
201 self.expr = expr
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
202
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
203 def encode(self):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
204 return u32(self.expr)
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
205
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
206 def __repr__(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
207 return 'DCD 0x{0:X}'.format(self.expr)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
208
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
209
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
210
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
211 # Memory related
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
212
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
213 class LS_imm5_base(ArmInstruction):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
214 """ ??? Rt, [Rn, imm5] """
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 224
diff changeset
215 operands = (Reg8Op, MemR8Rel)
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
216 def __init__(self, rt, memop):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
217 assert memop.offset % 4 == 0
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
218 self.imm5 = memop.offset >> 2
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
219 self.rn = memop.basereg.num
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 224
diff changeset
220 self.rt = rt
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
221 self.memloc = memop
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
222 assert self.rn < 8
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 224
diff changeset
223 assert self.rt.num < 8
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
224
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
225 def encode(self):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
226 Rn = self.rn
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 224
diff changeset
227 Rt = self.rt.num
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
228 imm5 = self.imm5
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
229
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
230 h = (self.opcode << 11) | (imm5 << 6) | (Rn << 3) | Rt
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
231 return u16(h)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
232 def __repr__(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
233 return '{} {}, {}'.format(self.mnemonic, self.rt, self.memloc)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
234
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
235 @armtarget.instruction
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
236 class storeimm5_ins(LS_imm5_base):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
237 mnemonic = 'STR'
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
238 opcode = 0xC
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
239
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
240 @armtarget.instruction
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
241 class loadimm5_ins(LS_imm5_base):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
242 mnemonic = 'LDR'
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
243 opcode = 0xD
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
244
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
245 class ls_sp_base_imm8(ArmInstruction):
224
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
246 operands = (Reg8Op, MemSpRel)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
247 def __init__(self, rt, memop):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
248 self.rt = rt
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
249 self.offset = memop.offset
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
250
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
251 def encode(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
252 rt = self.rt.num
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
253 assert rt < 8
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
254 imm8 = self.offset >> 2
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
255 assert imm8 < 256
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
256 h = (self.opcode << 8) | (rt << 8) | imm8
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
257 return u16(h)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
258
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
259 def __repr__(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
260 return '{} {}, [sp,#{}]'.format(self.mnemonic, self.rt, self.offset)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
261
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
262 @armtarget.instruction
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
263 class ldr_pcrel(ArmInstruction):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
264 """ ldr Rt, [PC, imm8], store value into memory """
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
265 mnemonic = 'ldr'
224
5af52987f5bd Fixup of pc rel operand
Windel Bouwman
parents: 223
diff changeset
266 operands = (RegOp, MemPcRel)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
267 def __init__(self, rt, label):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
268 self.rt = rt
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
269 self.label = label
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
270 self.offset = 0
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
271
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
272 def encode(self):
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
273 rt = self.rt.num
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
274 assert rt < 8
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
275 imm8 = self.offset >> 2
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
276 assert imm8 < 256
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
277 h = (0x9 << 11) | (rt << 8) | imm8
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
278 return u16(h)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
279
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
280 def __repr__(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
281 return 'LDR {}, [pc,#{}]'.format(self.rt, self.offset)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
282
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
283 @armtarget.instruction
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
284 class ldr_sprel(ls_sp_base_imm8):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
285 """ ldr Rt, [SP, imm8] """
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
286 mnemonic = 'LDR'
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
287 opcode = 0x98
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
288
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
289 @armtarget.instruction
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
290 class str_sprel(ls_sp_base_imm8):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
291 """ str Rt, [SP, imm8] """
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
292 mnemonic = 'STR'
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
293 opcode = 0x90
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
294
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents: 207
diff changeset
295 @armtarget.instruction
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
296 class mov_ins(ArmInstruction):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
297 """ mov Rd, imm8, move immediate value into register """
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
298 mnemonic = 'mov'
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
299 opcode = 4 # 00100 Rd(3) imm8
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
300 operands = (RegOp, Imm8)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
301 irpattern = ir.ImmLoad
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
302 def __init__(self, rd, imm):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
303 self.imm = imm.imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
304 self.r = rd.num
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
305
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
306 def encode(self):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
307 rd = self.r
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
308 opcode = self.opcode
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
309 imm8 = self.imm
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
310 h = (opcode << 11) | (rd << 8) | imm8
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
311 return u16(h)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
312 def __repr__(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
313 return 'MOV {0}, xx?'.format(self.r)
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
314
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
315 @armtarget.instruction
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
316 class movregreg_ins(ArmInstruction):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
317 """ mov Rd, Rm """
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
318 mnemonic = 'mov'
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
319 operands = (RegOp, RegOp)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
320 def __init__(self, rd, rm):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
321 self.rd = rd
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
322 self.rm = rm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
323 def encode(self):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
324 rd = self.rd.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
325 D = (rd & 0x8) >> 3
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
326 assert D < 2
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
327 rd = rd & 0x7
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
328 rm = self.rm.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
329 assert rm < 16
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
330 opcode = self.opcode
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
331 h = (1 << 14) | (3 << 9) | (D << 7) | (rm << 3) | rd
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
332 return u16(h)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
333
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
334
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
335
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
336 # Arithmatics:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
337
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
338 @armtarget.instruction
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
339 class addregregimm3_ins(ArmInstruction):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
340 """ add Rd, Rn, imm3 """
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
341 mnemonic = 'add'
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
342 opcode = 3 # 00011
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
343 operands = (RegOp, RegOp, Imm3)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
344 irpattern = 3
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
345 def __init__(self, rd, rn, imm3):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
346 self.rd = rd
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
347 self.rn = rn
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
348 self.imm3 = imm3
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
349 def encode(self):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
350 rd = self.rd.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
351 rn = self.rn.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
352 imm3 = self.imm3.imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
353 opcode = self.opcode
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
354 h = (opcode << 11) | (1 << 10) | (imm3 << 6) | (rn << 3) | rd
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
355 return u16(h)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
356
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
357 class regregreg_base(ArmInstruction):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
358 """ ??? Rd, Rn, Rm """
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
359 operands = (Reg8Op, Reg8Op, Reg8Op)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
360 def __init__(self, rd, rn, rm):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
361 self.rd = rd
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
362 self.rn = rn
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
363 self.rm = rm
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
364 def encode(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
365 rd = self.rd.num
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
366 rn = self.rn.num
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
367 rm = self.rm.num
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
368 h = (self.opcode << 9) | (rm << 6) | (rn << 3) | rd
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
369 return u16(h)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
370 def __repr__(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
371 return '{} {}, {}, {}'.format(self.mnemonic, self.rd, self.rn, self.rm)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
372
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
373 @armtarget.instruction
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
374 class addregs_ins(regregreg_base):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
375 mnemonic = 'ADD'
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
376 opcode = 0b0001100
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
377
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
378 @armtarget.instruction
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
379 class subregs_ins(regregreg_base):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
380 mnemonic = 'SUB'
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
381 opcode = 0b0001101
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
382
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
383 class regreg_base(ArmInstruction):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
384 """ ??? Rdn, Rm """
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
385 operands = (Reg8Op, Reg8Op)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
386 def __init__(self, rdn, rm):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
387 self.rdn = rdn
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
388 self.rm = rm
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
389 def encode(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
390 rdn = self.rdn.num
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
391 rm = self.rm.num
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
392 h = (self.opcode << 6) | (rm << 3) | rdn
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
393 return u16(h)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
394 def __repr__(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
395 return '{} {}, {}'.format(self.mnemonic, self.rdn, self.rm)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
396
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
397 @armtarget.instruction
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
398 class andregs_ins(regreg_base):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
399 mnemonic = 'AND'
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
400 opcode = 0b0100000000
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
401
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
402 @armtarget.instruction
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
403 class orrregs_ins(regreg_base):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
404 mnemonic = 'ORR'
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
405 opcode = 0b0100001100
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
406
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
407 @armtarget.instruction
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
408 class cmp_ins(regreg_base):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
409 mnemonic = 'CMP'
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
410 opcode = 0b0100001010
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
411
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
412 @armtarget.instruction
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
413 class cmpregimm8_ins(ArmInstruction):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
414 """ cmp Rn, imm8 """
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
415 mnemonic = 'cmp'
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
416 opcode = 5 # 00101
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
417 operands = (RegOp, Imm8)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
418 def __init__(self, rn, imm):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
419 self.rn = rn
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
420 self.imm = imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
421 def encode(self):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
422 rn = self.rn.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
423 imm = self.imm.imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
424 opcode = self.opcode
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
425 h = (opcode << 11) | (rn << 8) | imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
426 return u16(h)
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
427
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
428 # Jumping:
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 216
diff changeset
429
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 216
diff changeset
430 @armtarget.instruction
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
431 class jmp_ins(ArmInstruction):
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
432 operands = (ALabel,)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
433 mnemonic = 'jmp'
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
434 def __init__(self, target_label):
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
435 assert type(target_label) is ALabel
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
436 self.target = target_label
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
437 def fixUp(self):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
438 pass
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
439 def encode(self):
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
440 h = 0 # TODO
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
441 return u16(h)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
442 def __repr__(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
443 return 'B {0}'.format(self.target.name)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
444
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
445 @armtarget.instruction
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
446 class beq_ins(ArmInstruction):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
447 operands = (ALabel,)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
448 mnemonic = 'beq'
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
449 def __init__(self, target_label):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
450 assert type(target_label) is ALabel
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
451 self.target = target_label
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
452 def fixUp(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
453 pass
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
454 def encode(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
455 h = 0 # TODO
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
456 return u16(h)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
457 def __repr__(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
458 return 'BEQ {0}'.format(self.target.name)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
459
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
460 @armtarget.instruction
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
461 class push_ins(ArmInstruction):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
462 operands = (RegisterSet,)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
463 mnemonic = 'push'
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
464 def __init__(self, regs):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
465 assert (type(regs),) == self.operands, (type(regs),)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
466 self.regs = regs
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
467 def __repr__(self):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
468 return '{0} {{{1}}}'.format(self.mnemonic, self.regs)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
469 def encode(self):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
470 reg_list = 0
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
471 M = 0
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
472 for n in self.regs.registerNumbers():
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
473 if n < 8:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
474 reg_list |= (1 << n)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
475 elif n == 14:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
476 M = 1
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
477 else:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
478 raise NotImplementedError('not implemented for this register')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
479 h = (0x5a << 9) | (M << 8) | reg_list
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
480 return u16(h)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
481
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
482 @armtarget.instruction
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
483 class pop_ins(ArmInstruction):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
484 operands = (RegisterSet,)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
485 mnemonic = 'pop'
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
486 def __init__(self, regs):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
487 self.regs = regs
207
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 206
diff changeset
488 def __repr__(self):
8b2f20aae086 cleaning of files
Windel Bouwman
parents: 206
diff changeset
489 return '{0} {{{1}}}'.format(self.mnemonic, self.regs)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
490 def encode(self):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
491 reg_list = 0
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
492 P = 0
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
493 for n in self.regs.registerNumbers():
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
494 if n < 8:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
495 reg_list |= (1 << n)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
496 elif n == 15:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
497 P = 1
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
498 else:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
499 raise NotImplementedError('not implemented for this register')
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
500 h = (0x5E << 9) | (P << 8) | reg_list
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
501 return u16(h)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
502
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 203
diff changeset
503 @armtarget.instruction
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
504 class yield_ins(ArmInstruction):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
505 operands = ()
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
506 mnemonic = 'yield'
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
507 def encode(self):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
508 return u16(0xbf10)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
509
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
510 armtarget.check()
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
511