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