annotate python/ppci/target/thumb/armframe.py @ 345:b4882ff0ed06

Added more arm isa tests
author Windel Bouwman
date Sun, 02 Mar 2014 17:12:08 +0100
parents 86b02c98a717
children
rev   line source
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
1 from ... import ir
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
2 from ..basetarget import Label, Alignment
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
3 from ...irmach import makeIns, Frame
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
4 from .instructions import Dcd, AddSp, SubSp, Push, Pop, Mov2
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
5 from ..arm.registers import R0, R1, R2, R3, R4, R5, R6, R7, LR, PC, SP
274
ea93e0a7a31e Move docs
Windel Bouwman
parents: 272
diff changeset
6
290
7b38782ed496 File moves
Windel Bouwman
parents: 280
diff changeset
7
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
8 class ArmFrame(Frame):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
9 """ Arm specific frame for functions. """
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
10 def __init__(self, name):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
11 # We use r7 as frame pointer.
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
12 super().__init__(name)
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
13 self.regs = [R0, R1, R2, R3, R4, R5, R6]
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
14 self.rv = ir.Temp('special_RV')
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
15 self.p1 = ir.Temp('special_P1')
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
16 self.p2 = ir.Temp('special_P2')
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
17 self.p3 = ir.Temp('special_P3')
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
18 self.p4 = ir.Temp('special_P4')
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
19 self.fp = ir.Temp('special_FP')
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
20 # Pre-colored registers:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
21 self.tempMap = {}
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
22 self.tempMap[self.rv] = R0
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
23 self.tempMap[self.p1] = R1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
24 self.tempMap[self.p2] = R2
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
25 self.tempMap[self.p3] = R3
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
26 self.tempMap[self.p4] = R4
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
27 self.tempMap[self.fp] = R7
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
28 self.locVars = {}
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
29 self.parMap = {}
276
Windel Bouwman
parents: 275
diff changeset
30 # Literal pool:
Windel Bouwman
parents: 275
diff changeset
31 self.constants = []
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
32
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
33 def argLoc(self, pos):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
34 """
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
35 Gets the function parameter location in IR-code format.
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
36 """
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
37 if pos == 0:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
38 return self.p1
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
39 elif pos == 1:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
40 return self.p2
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
41 elif pos == 2:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
42 return self.p3
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
43 elif pos == 3:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
44 return self.p4
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
45 else:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
46 raise NotImplementedError('No more than 4 parameters implemented')
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
47
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
48 def allocVar(self, lvar):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
49 if lvar not in self.locVars:
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
50 self.locVars[lvar] = self.stacksize
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
51 self.stacksize = self.stacksize + 4
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
52 return self.locVars[lvar]
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
53
276
Windel Bouwman
parents: 275
diff changeset
54 def addConstant(self, value):
Windel Bouwman
parents: 275
diff changeset
55 lab_name = '{}_literal_{}'.format(self.name, len(self.constants))
Windel Bouwman
parents: 275
diff changeset
56 self.constants.append((lab_name, value))
Windel Bouwman
parents: 275
diff changeset
57 return lab_name
Windel Bouwman
parents: 275
diff changeset
58
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
59 def prologue(self):
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
60 """ Returns prologue instruction sequence """
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
61 pre = [
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
62 Label(self.name), # Label indication function
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
63 Push({LR, R7})
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
64 ]
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
65 if self.stacksize > 0:
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 323
diff changeset
66 pre.append(SubSp(self.stacksize)) # Reserve stack space
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
67 pre += [
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
68 Mov2(R7, SP) # Setup frame pointer
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
69 ]
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
70 return pre
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
71
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
72 def epilogue(self):
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
73 """ Return epilogue sequence for a frame. Adjust frame pointer and add constant pool """
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
74 post = []
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
75 if self.stacksize > 0:
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 323
diff changeset
76 post.append(AddSp(self.stacksize))
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
77 post += [
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
78 Pop({PC, R7}),
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
79 Alignment(4) # Align at 4 bytes
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
80 ]
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
81 # Add constant literals:
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
82 for ln, v in self.constants:
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
83 post.extend([Label(ln), Dcd(v)])
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
84 return post
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
85
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
86 def EntryExitGlue3(self):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
87 """
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
88 Add code for the prologue and the epilogue. Add a label, the
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
89 return instruction and the stack pointer adjustment for the frame.
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
90 """
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
91 for index, ins in enumerate(self.prologue()):
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
92 self.instructions.insert(index, makeIns(ins))
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
93
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
94 # Postfix code:
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
95 for ins in self.epilogue():
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 301
diff changeset
96 self.instructions.append(makeIns(ins))