Mercurial > lcfOS
comparison python/codegenarm.py @ 232:e621e3ba78d2
Added left shift instruction
author | Windel Bouwman |
---|---|
date | Sun, 14 Jul 2013 11:50:58 +0200 |
parents | 1c7364bd74c7 |
children | 83781bd10fdb |
comparison
equal
deleted
inserted
replaced
231:521567d17388 | 232:e621e3ba78d2 |
---|---|
24 # TODO: use initial value: | 24 # TODO: use initial value: |
25 self.emit(arm.dcd_ins(0)) | 25 self.emit(arm.dcd_ins(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 | |
30 # Manually inserted startup code: | |
31 self.emit(arm.dcd_ins(0x20000678)) # initial stack ptr | |
32 self.emit(arm.dcd_ins(0x08000401)) # reset vector | |
33 | |
29 for f in ircode.Functions: | 34 for f in ircode.Functions: |
30 # Add global variable addresses to immediate list: | 35 # Add global variable addresses to immediate list: |
31 for gvar in ircode.Variables: | 36 for gvar in ircode.Variables: |
32 pass #self.imms.append(( | 37 pass #self.imms.append(( |
33 | 38 |
61 self.emit(arm.ldr_sprel(reg, arm.MemSpRel(self.getStack(val)))) | 66 self.emit(arm.ldr_sprel(reg, arm.MemSpRel(self.getStack(val)))) |
62 def comment(self, txt): | 67 def comment(self, txt): |
63 self.emit(AComment(txt)) | 68 self.emit(AComment(txt)) |
64 | 69 |
65 def generateInstruction(self, ins): | 70 def generateInstruction(self, ins): |
71 self.comment(str(ins)) | |
66 if type(ins) is ir.Branch: | 72 if type(ins) is ir.Branch: |
67 tgt = ALabel(ins.target.name) | 73 tgt = ALabel(ins.target.name) |
68 self.emit(arm.jmp_ins(tgt)) | 74 self.emit(arm.jmp_ins(tgt)) |
69 elif type(ins) is ir.ImmLoad: | 75 elif type(ins) is ir.ImmLoad: |
70 self.comment(str(ins)) | |
71 lname = ins.target.name + '_ivalue' | 76 lname = ins.target.name + '_ivalue' |
72 self.emit(arm.ldr_pcrel(arm.r0, ALabel(lname))) | 77 self.emit(arm.ldr_pcrel(arm.r0, ALabel(lname))) |
73 self.imms.append((lname, ins.value)) | 78 self.imms.append((lname, ins.value)) |
74 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.target)))) | 79 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.target)))) |
75 elif type(ins) is ir.Store: | 80 elif type(ins) is ir.Store: |
76 self.comment(str(ins)) | |
77 # Load value in r0: | 81 # Load value in r0: |
78 self.loadStack(arm.r0, ins.value) | 82 self.loadStack(arm.r0, ins.value) |
79 # store in memory: | 83 # store in memory: |
80 self.getGlobal(arm.r1, ins.location) | 84 # TODO: split globals and locals?? |
85 #self.getGlobal(arm.r1, ins.location) | |
86 self.loadStack(arm.r1, ins.location) | |
81 self.emit(arm.storeimm5_ins(arm.r0, arm.MemR8Rel(arm.r1, 0))) | 87 self.emit(arm.storeimm5_ins(arm.r0, arm.MemR8Rel(arm.r1, 0))) |
82 elif type(ins) is ir.Load: | 88 elif type(ins) is ir.Load: |
83 self.comment(str(ins)) | 89 # TODO: differ global and local?? |
84 self.getGlobal(arm.r0, ins.location) | 90 #self.getGlobal(arm.r0, ins.location) |
91 self.loadStack(arm.r0, ins.location) | |
85 self.emit(arm.loadimm5_ins(arm.r0, arm.MemR8Rel(arm.r0, 0))) | 92 self.emit(arm.loadimm5_ins(arm.r0, arm.MemR8Rel(arm.r0, 0))) |
86 # Store value on stack: | 93 # Store value on stack: |
87 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.value)))) | 94 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.value)))) |
88 elif type(ins) is ir.BinaryOperator: | 95 elif type(ins) is ir.BinaryOperator: |
89 self.comment(str(ins)) | |
90 # Load operands: | 96 # Load operands: |
91 self.loadStack(arm.r0, ins.value1) | 97 self.loadStack(arm.r0, ins.value1) |
92 self.loadStack(arm.r1, ins.value2) | 98 self.loadStack(arm.r1, ins.value2) |
93 # do operation: | 99 # do operation: |
94 if ins.operation == '+': | 100 if ins.operation == '+': |
95 self.emit(arm.addregs_ins(arm.r0, arm.r0, arm.r1)) | 101 self.emit(arm.addregs_ins(arm.r0, arm.r0, arm.r1)) |
102 elif ins.operation == '<<': | |
103 self.emit(arm.lslregs_ins(arm.r0, arm.r1)) | |
104 elif ins.operation == '|': | |
105 self.emit(arm.orrregs_ins(arm.r0, arm.r1)) | |
96 else: | 106 else: |
97 print('operation not implemented', ins.operation) | 107 print('operation not implemented', ins.operation) |
98 # Store value back: | 108 # Store value back: |
99 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.result)))) | 109 self.emit(arm.str_sprel(arm.r0, arm.MemSpRel(self.addStack(ins.result)))) |
100 elif type(ins) is ir.Return: | 110 elif type(ins) is ir.Return: |
101 self.comment(str(ins)) | |
102 self.emit(arm.pop_ins(arm.RegisterSet({arm.r4, arm.r5, arm.r6, arm.r7, arm.pc}))) | 111 self.emit(arm.pop_ins(arm.RegisterSet({arm.r4, arm.r5, arm.r6, arm.r7, arm.pc}))) |
103 elif type(ins) is ir.ConditionalBranch: | 112 elif type(ins) is ir.ConditionalBranch: |
104 self.comment(str(ins)) | |
105 self.loadStack(arm.r0, ins.a) | 113 self.loadStack(arm.r0, ins.a) |
106 self.loadStack(arm.r1, ins.b) | 114 self.loadStack(arm.r1, ins.b) |
107 self.emit(arm.cmp_ins(arm.r1, arm.r0)) | 115 self.emit(arm.cmp_ins(arm.r1, arm.r0)) |
108 tgt_yes = ALabel(ins.lab1.name) | 116 tgt_yes = ALabel(ins.lab1.name) |
109 if ins.cond == '==': | 117 if ins.cond == '==': |
111 else: | 119 else: |
112 print('TODO', ins.cond) | 120 print('TODO', ins.cond) |
113 tgt_no = ALabel(ins.lab2.name) | 121 tgt_no = ALabel(ins.lab2.name) |
114 self.emit(arm.jmp_ins(tgt_no)) | 122 self.emit(arm.jmp_ins(tgt_no)) |
115 elif type(ins) is ir.Alloc: | 123 elif type(ins) is ir.Alloc: |
124 # Local variables are added to stack | |
116 self.addStack(ins.value) | 125 self.addStack(ins.value) |
117 else: | 126 else: |
118 raise CompilerError('IR "{}" not covered'.format(ins)) | 127 raise CompilerError('IR "{}" not covered'.format(ins)) |
119 | 128 |
120 | 129 |