annotate python/cortexm3.py @ 267:e7c8f7eb3f59

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