annotate python/ppci/target/thumb/armtarget.py @ 342:86b02c98a717 devel

Moved target directory
author Windel Bouwman
date Sat, 01 Mar 2014 15:40:31 +0100
parents python/target/armtarget.py@4d204f6f7d4e
children b4882ff0ed06
rev   line source
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 258
diff changeset
1 import struct
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
2 from ..basetarget import Register, Instruction, Target, Label, Alignment
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
3 from ..basetarget import Imm32, Imm8, Imm7, Imm3
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
4 from .instructions import Add2, Sub, Add3, Cmp, Lsl, Orr, Add, Cmp2
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
5 from .instructions import Dcd, Pop, Push, Yield, Mov2, Mov3
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
6 from .instructions import B, Bl, Bne, Beq, Blt, Bgt
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
7 from .instructions import Ldr, Str2, Ldr2, Str1, Ldr1
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents:
diff changeset
8
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 292
diff changeset
9 from .armframe import ArmFrame
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 292
diff changeset
10 from .arminstructionselector import ArmInstructionSelector
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
11 from ..arm.registers import R0, R1, R2, R3, R4, R5, R6, R7, SP, LR, PC
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
12 from ..arm.registers import register_range
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
13
290
7b38782ed496 File moves
Windel Bouwman
parents: 287
diff changeset
14
7b38782ed496 File moves
Windel Bouwman
parents: 287
diff changeset
15 """ ARM target description. """
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 280
diff changeset
16
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 216
diff changeset
17 # TODO: encode this in DSL (domain specific language)
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 268
diff changeset
18 # TBD: is this required?
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
19 # TODO: make a difference between armv7 and armv5?
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 218
diff changeset
20
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
21 thumb_assembly_rules = []
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
22 def add_rule(rhs, f):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
23 thumb_assembly_rules.append(('instruction', rhs, f))
277
046017431c6a Started register allocator
Windel Bouwman
parents: 276
diff changeset
24
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
25
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
26 class ThumbTarget(Target):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
27 def __init__(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
28 super().__init__('thumb')
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 292
diff changeset
29 self.ins_sel = ArmInstructionSelector()
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 292
diff changeset
30 self.FrameClass = ArmFrame
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
31 self.add_rules()
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
32
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
33 def add_rules(self):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
34
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
35 # Add instructions:
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
36 self.add_keyword('dcd')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
37 self.add_instruction(['dcd', 'imm32'], lambda rhs: Dcd(rhs[1]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
38
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
39 self.add_keyword('mov')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
40 self.add_instruction(['mov', 'reg8', ',', 'reg8'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
41 lambda rhs: Mov2(rhs[1], rhs[3]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
42
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
43 self.add_instruction(['mov', 'reg8', ',', 'imm8'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
44 lambda rhs: Mov3(rhs[1], rhs[3]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
45
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
46 self.add_keyword('add')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
47 self.add_instruction(['add', 'reg8', ',', 'reg8', ',', 'imm3'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
48 lambda rhs: Add2(rhs[1], rhs[3], rhs[5]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
49
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
50 self.add_instruction(['add', 'reg8', ',', 'reg8', ',', 'reg8'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
51 lambda rhs: Add3(rhs[1], rhs[3], rhs[5]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
52
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
53 self.add_keyword('sub')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
54 self.add_instruction(['sub', 'reg8', ',', 'reg8', ',', 'imm3'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
55 lambda rhs: Sub(rhs[1], rhs[3], rhs[5]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
56
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
57 self.add_instruction(['sub', 'sp', ',', 'sp', ',', 'imm8'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
58 lambda rhs: Sub(SP, SP, rhs[5]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
59
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
60 self.add_instruction(['add', 'sp', ',', 'sp', ',', 'imm8'],
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
61 lambda rhs: Add(SP, SP, rhs[5]))
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
62
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
63 self.add_keyword('cmp')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
64 self.add_instruction(['cmp', 'reg8', ',', 'reg8'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
65 lambda rhs: Cmp(rhs[1], rhs[3]))
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
66 self.add_instruction(['cmp', 'reg8', ',', 'imm8'],
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
67 lambda rhs: Cmp2(rhs[1], rhs[3]))
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
68
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
69 self.add_keyword('lsl')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
70 self.add_instruction(['lsl', 'reg8', ',', 'reg8'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
71 lambda rhs: Lsl(rhs[1], rhs[3]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
72
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
73 self.add_keyword('str')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
74 self.add_instruction(['str', 'reg8', ',', '[', 'reg8', '+', 'imm5', ']'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
75 lambda rhs: Str2(rhs[1], rhs[4], rhs[6]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
76
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
77 self.add_keyword('ldr')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
78 self.add_instruction(['ldr', 'reg8', ',', '[', 'reg8', '+', 'imm5', ']'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
79 lambda rhs: Ldr2(rhs[1], rhs[4], rhs[6]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
80
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
81 self.add_instruction(['str', 'reg8', ',', '[', 'sp', '+', 'imm8', ']'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
82 lambda rhs: Str1(rhs[1], rhs[6]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
83
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
84 self.add_instruction(['ldr', 'reg8', ',', '[', 'sp', '+', 'imm8', ']'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
85 lambda rhs: Ldr1(rhs[1], rhs[6]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
86
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
87 self.add_keyword('pop')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
88 self.add_instruction(['pop', 'reg_list'], lambda rhs: Pop(rhs[1]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
89 self.add_keyword('push')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
90 self.add_instruction(['push', 'reg_list'], lambda rhs: Push(rhs[1]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
91
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
92 self.add_keyword('yield')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
93 self.add_instruction(['yield'], lambda rhs: Yield())
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
94
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
95 self.add_keyword('b')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
96 self.add_keyword('bl')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
97 self.add_instruction(['b', 'ID'], lambda rhs: B(rhs[1].val))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
98 self.add_instruction(['bl', 'ID'], lambda rhs: Bl(rhs[1].val))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
99 self.add_keyword('beq')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
100 self.add_keyword('bne')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
101 self.add_keyword('blt')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
102 self.add_keyword('bgt')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
103 self.add_instruction(['beq', 'ID'], lambda rhs: Beq(rhs[1].val))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
104 self.add_instruction(['bne', 'ID'], lambda rhs: Bne(rhs[1].val))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
105 self.add_instruction(['blt', 'ID'], lambda rhs: Blt(rhs[1].val))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
106 self.add_instruction(['bgt', 'ID'], lambda rhs: Bgt(rhs[1].val))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
107
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
108 self.add_keyword('align')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
109 self.add_instruction(['align', 'imm8'], lambda rhs: Alignment(rhs[1]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
110
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
111 self.add_instruction(['ldr', 'reg8', ',', 'ID'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
112 lambda rhs: Ldr(rhs[1], rhs[3].val))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
113
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
114 # Additional rules:
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
115
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
116 # Register list grammar:
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
117 self.add_rule('reg_list', ['{', 'reg_list_inner', '}'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
118 lambda rhs: rhs[1])
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
119 self.add_rule('reg_list_inner', ['reg_or_range'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
120 lambda rhs: rhs[0])
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
121 self.add_rule('reg_list_inner', ['reg_or_range', ',', 'reg_list_inner'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
122 lambda rhs: rhs[0] | rhs[2])
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
123 self.add_rule('reg_or_range', ['reg8'], lambda rhs: {rhs[0]})
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
124 self.add_rule('reg_or_range', ['lr'], lambda rhs: {LR})
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
125 self.add_rule('reg_or_range', ['pc'], lambda rhs: {PC})
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
126
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
127 self.add_rule('reg_or_range', ['reg8', '-', 'reg8'],
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
128 lambda rhs: register_range(rhs[0], rhs[2]))
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
129
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
130 self.add_keyword('r0')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
131 self.add_keyword('r1')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
132 self.add_keyword('r2')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
133 self.add_keyword('r3')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
134 self.add_keyword('r4')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
135 self.add_keyword('r5')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
136 self.add_keyword('r6')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
137 self.add_keyword('r7')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
138 self.add_keyword('sp')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
139 self.add_keyword('lr')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
140 self.add_keyword('pc')
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
141 self.add_rule('reg8', ['r0'], lambda rhs: R0)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
142 self.add_rule('reg8', ['r1'], lambda rhs: R1)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
143 self.add_rule('reg8', ['r2'], lambda rhs: R2)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
144 self.add_rule('reg8', ['r3'], lambda rhs: R3)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
145 self.add_rule('reg8', ['r4'], lambda rhs: R4)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
146 self.add_rule('reg8', ['r5'], lambda rhs: R5)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
147 self.add_rule('reg8', ['r6'], lambda rhs: R6)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
148 self.add_rule('reg8', ['r7'], lambda rhs: R7)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
149 # Constants:
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
150 self.add_rule('imm32', ['val32'], lambda x: x[0].val)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
151 self.add_rule('imm32', ['imm8'], lambda x: x[0])
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
152 self.add_rule('imm8', ['val8'], lambda x: x[0].val)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
153 self.add_rule('imm8', ['imm5'], lambda x: x[0])
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
154 self.add_rule('imm5', ['val5'], lambda x: x[0].val)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
155 self.add_rule('imm5', ['imm3'], lambda x: x[0])
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
156 self.add_rule('imm3', ['val3'], lambda x: x[0].val)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
157