comparison python/codegenarm.py @ 235:ff40407c0240

Fix ALabel to Label
author Windel Bouwman
date Mon, 15 Jul 2013 17:20:37 +0200
parents 83781bd10fdb
children 8786811a5a59
comparison
equal deleted inserted replaced
234:83781bd10fdb 235:ff40407c0240
1 import ir 1 import ir
2 from target import Label, Comment 2 from target import Label, Comment, Alignment, LabelRef, Imm32
3 import cortexm3 as arm 3 import cortexm3 as arm
4 from ppci import CompilerError 4 from ppci import CompilerError
5 5
6 class ArmCodeGenerator: 6 class ArmCodeGenerator:
7 """ 7 """
18 assert isinstance(ircode, ir.Module) 18 assert isinstance(ircode, ir.Module)
19 print('ARM code generation') 19 print('ARM code generation')
20 self.outs.selectSection('data') 20 self.outs.selectSection('data')
21 21
22 for gvar in ircode.Variables: 22 for gvar in ircode.Variables:
23 self.emit(ALabel(gvar.name)) 23 self.emit(Label(gvar.name))
24 # TODO: use initial value: 24 # TODO: use initial value:
25 self.emit(arm.dcd_ins(0)) 25 self.dcd(0)
26 26
27 self.imms = [] # list with immediates relative to PC. 27 self.imms = [] # list with immediates relative to PC.
28 self.outs.selectSection('code') 28 self.outs.selectSection('code')
29 29
30 # Manually inserted startup code: 30 # Manually inserted startup code:
31 self.emit(arm.dcd_ins(0x20000678)) # initial stack ptr 31 self.dcd(0x20000678) # initial stack ptr
32 self.emit(arm.dcd_ins(0x08000401)) # reset vector 32 self.dcd(0x08000401) # reset vector
33 33
34 for f in ircode.Functions: 34 for f in ircode.Functions:
35 # Add global variable addresses to immediate list: 35 # Add global variable addresses to immediate list:
36 for gvar in ircode.Variables: 36 for gvar in ircode.Variables:
37 pass #self.imms.append(( 37 pass #self.imms.append((
43 for bb in f.BasicBlocks: 43 for bb in f.BasicBlocks:
44 self.emit(Label(bb.name)) 44 self.emit(Label(bb.name))
45 for ins in bb.Instructions: 45 for ins in bb.Instructions:
46 self.generateInstruction(ins) 46 self.generateInstruction(ins)
47 47
48 self.outs.align(4) 48 self.align()
49 while self.imms: 49 while self.imms:
50 l, v = self.imms.pop() 50 l, v = self.imms.pop()
51 self.emit(Label(l)) 51 self.emit(Label(l))
52 self.emit(arm.dcd_ins(v)) 52 self.dcd(v)
53 self.outs.align(4) 53 self.align()
54 54
55 def dcd(self, x):
56 self.emit(arm.dcd_ins(Imm32(x)))
57
58 def align(self):
59 self.outs.emit(Alignment(4))
55 # Helper functions: 60 # Helper functions:
56 def getStack(self, v): 61 def getStack(self, v):
57 off = self.stack_frame.index(v) 62 off = self.stack_frame.index(v)
58 return off * 4 63 return off * 4
59 def addStack(self, v): 64 def addStack(self, v):
60 self.stack_frame.append(v) 65 self.stack_frame.append(v)
61 return self.getStack(v) 66 return self.getStack(v)
62 def getGlobal(self, r, g): 67 def getGlobal(self, r, g):
63 _global_address = g.name + '__global' 68 _global_address = g.name + '__global'
64 self.emit(arm.ldr_pcrel(r, ALabel(_global_address))) 69 self.emit(arm.ldr_pcrel(r, LabelRef(_global_address)))
65 def loadStack(self, reg, val): 70 def loadStack(self, reg, val):
66 self.emit(arm.ldr_sprel(reg, arm.MemSpRel(self.getStack(val)))) 71 self.emit(arm.ldr_sprel(reg, arm.MemSpRel(self.getStack(val))))
67 def comment(self, txt): 72 def comment(self, txt):
68 self.emit(Comment(txt)) 73 self.emit(Comment(txt))
69 74
72 if type(ins) is ir.Branch: 77 if type(ins) is ir.Branch:
73 tgt = Label(ins.target.name) 78 tgt = Label(ins.target.name)
74 self.emit(arm.jmp_ins(tgt)) 79 self.emit(arm.jmp_ins(tgt))
75 elif type(ins) is ir.ImmLoad: 80 elif type(ins) is ir.ImmLoad:
76 lname = ins.target.name + '_ivalue' 81 lname = ins.target.name + '_ivalue'
77 self.emit(arm.ldr_pcrel(arm.r0, Label(lname))) 82 self.emit(arm.ldr_pcrel(arm.r0, LabelRef(lname)))
78 self.imms.append((lname, ins.value)) 83 self.imms.append((lname, ins.value))
79 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.target)))) 84 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.target))))
80 elif type(ins) is ir.Store: 85 elif type(ins) is ir.Store:
81 # Load value in r0: 86 # Load value in r0:
82 self.loadStack(arm.r0, ins.value) 87 self.loadStack(arm.r0, ins.value)
111 self.emit(arm.pop_ins(arm.RegisterSet({arm.r4, arm.r5, arm.r6, arm.r7, arm.pc}))) 116 self.emit(arm.pop_ins(arm.RegisterSet({arm.r4, arm.r5, arm.r6, arm.r7, arm.pc})))
112 elif type(ins) is ir.ConditionalBranch: 117 elif type(ins) is ir.ConditionalBranch:
113 self.loadStack(arm.r0, ins.a) 118 self.loadStack(arm.r0, ins.a)
114 self.loadStack(arm.r1, ins.b) 119 self.loadStack(arm.r1, ins.b)
115 self.emit(arm.cmp_ins(arm.r1, arm.r0)) 120 self.emit(arm.cmp_ins(arm.r1, arm.r0))
116 tgt_yes = ALabel(ins.lab1.name) 121 tgt_yes = Label(ins.lab1.name)
117 if ins.cond == '==': 122 if ins.cond == '==':
118 self.emit(arm.beq_ins(tgt_yes)) 123 self.emit(arm.beq_ins(tgt_yes))
119 else: 124 else:
120 print('TODO', ins.cond) 125 print('TODO', ins.cond)
121 tgt_no = ALabel(ins.lab2.name) 126 tgt_no = Label(ins.lab2.name)
122 self.emit(arm.jmp_ins(tgt_no)) 127 self.emit(arm.jmp_ins(tgt_no))
123 elif type(ins) is ir.Alloc: 128 elif type(ins) is ir.Alloc:
124 # Local variables are added to stack 129 # Local variables are added to stack
125 self.addStack(ins.value) 130 self.addStack(ins.value)
126 else: 131 else: