annotate python/arm_cm3.py @ 203:ca1ea402f6a1

Added some arm instructions
author Windel Bouwman
date Sat, 15 Jun 2013 19:13:05 +0200
parents f22b431f4113
children d77cb5962cc5
rev   line source
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
1 from target import Register, Instruction, Target
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
2 from asmnodes import ASymbol, ANumber
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
3 from ppci import CompilerError
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
4 import struct, types
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
5
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
6 def u16(h):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
7 return struct.pack('<H', h)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
8
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
9 armtarget = Target('arm')
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
10
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
11 class ArmReg(Register):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
12 def __init__(self, num, name):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
13 super().__init__(name)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
14 self.num = num
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
15
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
16 class ArmImm:
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
17 def __init__(self, i):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
18 self.i = i
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
19
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
20 class RegOp:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
21 def __init__(self, num):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
22 assert num < 8
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
23 self.num = num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
24
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
25 @classmethod
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
26 def Create(cls, vop):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
27 if type(vop) is ASymbol:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
28 name = vop.name
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
29 regs = {}
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
30 for r in armtarget.registers:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
31 regs[r.name] = r
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
32 if name in regs:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
33 r = regs[name]
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
34 return cls(r.num)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
35
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
36 class Imm8:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
37 def __init__(self, imm):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
38 assert imm < 256
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
39 self.imm = imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
40
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
41 @classmethod
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
42 def Create(cls, vop):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
43 if type(vop) is ANumber and vop.number < 256:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
44 return cls(vop.number)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
45
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
46 class Imm3:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
47 def __init__(self, imm):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
48 assert imm < 8
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
49 assert type(imm) is int
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
50 self.imm = imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
51
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
52 @classmethod
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
53 def Create(cls, vop):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
54 if type(vop) is ANumber and vop.number < 8:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
55 return cls(vop.number)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
56
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
57 # 8 bit registers:
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
58 r4 = ArmReg(4, 'r4')
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
59 armtarget.registers.append(r4)
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
60 r5 = ArmReg(5, 'r5')
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
61 armtarget.registers.append(r5)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
62 r6 = ArmReg(6, 'r6')
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
63 armtarget.registers.append(r6)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
64 r7 = ArmReg(7, 'r7')
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
65 armtarget.registers.append(r7)
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
66
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
67 class ArmInstruction(Instruction):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
68 pass
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
69
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
70 @armtarget.instruction
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
71 class ldr_ins(ArmInstruction):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
72 mnemonic = 'ldr'
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
73 opcode = 1337
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
74
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
75
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
76 class Operand2:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
77 def __init__(self, expr):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
78 if type(expr) is ANumber:
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
79 pass
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
80 pass
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
81
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
82 @armtarget.instruction
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
83 class mov_ins(ArmInstruction):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
84 """ mov Rd, imm8, move immediate value into register """
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
85 mnemonic = 'mov'
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
86 opcode = 4 # 00100 Rd(3) imm8
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
87 operands = (RegOp, Imm8)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
88 def __init__(self, rd, imm):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
89 self.imm = imm.imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
90 self.r = rd.num
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
91 def encode(self):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
92 rd = self.r
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
93 opcode = self.opcode
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
94 imm8 = self.imm
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
95 h = (opcode << 11) | (rd << 8) | imm8
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
96 return u16(h)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
97
203
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
98 @armtarget.instruction
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
99 class movregreg_ins(ArmInstruction):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
100 """ mov Rd, Rm """
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
101 mnemonic = 'mov'
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
102 opcode = 8 # 01000 Rd(3) imm8
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
103 operands = (RegOp, RegOp)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
104 def __init__(self, rd, rm):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
105 self.rd = rd
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
106 self.rm = rm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
107 def encode(self):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
108 rd = self.rd.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
109 D = (rd & 0x8) >> 3
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
110 assert D < 2
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
111 rd = rd & 0x7
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
112 rm = self.rm.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
113 assert rm < 16
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
114 opcode = self.opcode
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
115 h = (opcode << 11) | (3 << 9) | (D << 7) | (rm << 3) | rd
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
116 return u16(h)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
117
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
118 @armtarget.instruction
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
119 class addregregimm3_ins(ArmInstruction):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
120 """ add Rd, Rn, imm3 """
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
121 mnemonic = 'add'
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
122 opcode = 3 # 00011
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
123 operands = (RegOp, RegOp, Imm3)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
124 def __init__(self, rd, rn, imm3):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
125 self.rd = rd
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
126 self.rn = rn
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
127 self.imm3 = imm3
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
128 def encode(self):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
129 rd = self.rd.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
130 rn = self.rn.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
131 imm3 = self.imm3.imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
132 opcode = self.opcode
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
133 h = (opcode << 11) | (1 << 10) | (imm3 << 6) | (rn << 3) | rd
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
134 return u16(h)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
135
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
136 @armtarget.instruction
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
137 class cmpregimm8_ins(ArmInstruction):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
138 """ cmp Rn, imm8 """
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
139 mnemonic = 'cmp'
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
140 opcode = 5 # 00101
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
141 operands = (RegOp, Imm8)
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
142 def __init__(self, rn, imm):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
143 self.rn = rn
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
144 self.imm = imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
145 def encode(self):
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
146 rn = self.rn.num
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
147 imm = self.imm.imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
148 opcode = self.opcode
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
149 h = (opcode << 11) | (rn << 8) | imm
ca1ea402f6a1 Added some arm instructions
Windel Bouwman
parents: 202
diff changeset
150 return u16(h)
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
151
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
152 @armtarget.instruction
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
153 class yield_ins(ArmInstruction):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
154 operands = ()
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
155 mnemonic = 'yield'
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
156 def encode(self):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
157 return u16(0xbf10)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
158