annotate python/target/armframe.py @ 340:c7cc54c0dfdf devel

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