annotate python/cortexm3.py @ 221:848c4b15fd0b

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