annotate python/target/armframe.py @ 306:b145f8e6050b

Start on c3 rewrite
author Windel Bouwman
date Mon, 09 Dec 2013 19:00:21 +0100
parents 6753763d3bec
children 44f336460c2a
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
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
60 def EntryExitGlue3(self):
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
61 """
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
62 Add code for the prologue and the epilogue. Add a label, the
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
63 return instruction and the stack pointer adjustment for the frame.
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 274
diff changeset
64 """
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
65 self.instructions.insert(0, makeIns(Label(self.name)))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
66 self.instructions.insert(1, makeIns(Push({LR, R7})))
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
67 # Reserve stack space for locals:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
68 self.instructions.insert(2, makeIns(SubSp(SP, SP, Imm7(self.stacksize))))
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
69 # Setup frame pointer:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
70 self.instructions.insert(3, makeIns(Mov2(R7, SP)))
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
71 # Stack grows downwards
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
72 self.instructions.append(makeIns(AddSp(SP, SP, Imm7(self.stacksize))))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
73 self.instructions.append(makeIns(Pop({PC, R7})))
276
Windel Bouwman
parents: 275
diff changeset
74 # Add constant literals:
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
75 self.instructions.append(makeIns(Alignment(4))) # Align at 4 bytes
276
Windel Bouwman
parents: 275
diff changeset
76 for ln, v in self.constants:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
77 self.instructions.append(makeIns(Label(ln)))
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
78 self.instructions.append(makeIns(Dcd(v)))