annotate python/codegenarm.py @ 269:5f8c04a8d26b

Towards better modularity
author Windel Bouwman
date Sun, 18 Aug 2013 17:43:18 +0200
parents 5ec7580976d9
children cdc76d183bcc
rev   line source
255
7416c923a02a Added more logging
Windel Bouwman
parents: 250
diff changeset
1 import logging
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
2 import ir
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
3 from target import Label, Comment, Alignment, LabelRef, Imm32, DebugInfo
218
494828a7adf1 added some sort of cache to assembler
Windel Bouwman
parents: 212
diff changeset
4 import cortexm3 as arm
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
5 from ppci import CompilerError
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
6 import graph
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
7 import flowgraph
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
8 import registerallocator
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
9 from instructionselector import InstructionSelector
262
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
10
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
11
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
12 class ArmInstructionSelector(InstructionSelector):
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
13 """ Instruction selector for the arm architecture """
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
14 def munchExpr(self, e):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
15 if isinstance(e, ir.Alloc):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
16 return 0
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
17 elif isinstance(e, ir.Binop) and e.operation == '+':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
18 a = self.munchExpr(e.value1)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
19 b = self.munchExpr(e.value2)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
20 d = self.newTmp()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
21 self.emit('add %d0, %s0, %s1', dst=[d], src=[a, b])
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
22 return d
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
23 elif isinstance(e, ir.Binop) and e.operation == '-':
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
24 a = self.munchExpr(e.value1)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
25 b = self.munchExpr(e.value2)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
26 d = self.newTmp()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
27 self.emit('sub %d0, %s0, %s1', dst=[d], src=[a, b])
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
28 return d
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
29 elif isinstance(e, ir.Binop) and e.operation == '|':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
30 a = self.munchExpr(e.value1)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
31 b = self.munchExpr(e.value2)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
32 d = self.newTmp()
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
33 self.emit('or %d0, %s0, %s1', dst=[d], src=[a, b])
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
34 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
35 elif isinstance(e, ir.Binop) and e.operation == '<<':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
36 a = self.munchExpr(e.value1)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
37 b = self.munchExpr(e.value2)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
38 d = self.newTmp()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
39 self.emit('lsl %d0, %s0, %s1', dst=[d], src=[a, b])
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
40 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
41 elif isinstance(e, ir.Binop) and e.operation == '*':
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
42 a = self.munchExpr(e.value1)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
43 b = self.munchExpr(e.value2)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
44 d = self.newTmp()
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
45 self.emit('mul %d0, %s0, %s1', dst=[d], src=[a, b])
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
46 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
47 elif isinstance(e, ir.Const):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
48 d = self.newTmp()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
49 if e.value < 256:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
50 self.emit('ldr %d0, {}'.format(e.value), dst=[d])
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
51 else:
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
52 self.emit('ldrpcrel TODO', dst=[d])
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
53 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
54 elif isinstance(e, ir.Mem):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
55 # Load from memory
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
56 loc = self.munchExpr(e.e)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
57 d = self.newTmp()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
58 self.emit('ldr %d0, [%s0]', src=[loc], dst=[d])
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
59 return d
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
60 elif isinstance(e, ir.Temp):
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
61 return self.getTempReg(e)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
62 else:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
63 raise NotImplementedError('--> {}'.format(e))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
64
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
65 def munchStm(self, s):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
66 if isinstance(s, ir.Move) and isinstance(s.dst, ir.Mem):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
67 memloc = self.munchExpr(s.dst.e)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
68 val = self.munchExpr(s.src)
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
69 self.emit('str [%s0], %s1')
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
70 elif isinstance(s, ir.Move) and isinstance(s.dst, ir.Temp):
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
71 val = self.munchExpr(s.src)
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
72 dreg = self.getTempReg(s.dst)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
73 self.emit('mov %d0, %s0', dst=[dreg], src=[val])
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
74 elif isinstance(s, ir.Return):
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
75 #etgt = self.targets[
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
76 self.emit('jmp exit', jumps=[])
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
77 elif isinstance(s, ir.Jump):
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
78 tgt = self.targets[s.target]
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
79 self.emit('jmp {}'.format(s), jumps=[tgt])
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
80 elif isinstance(s, ir.CJump):
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
81 a = self.munchExpr(s.a)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
82 b = self.munchExpr(s.b)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
83 self.emit('cmp %s0, %s1', src=[a, b])
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
84 ntgt = self.targets[s.lab_no]
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
85 ytgt = self.targets[s.lab_yes]
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
86 jmp_ins = self.makeIns('jmp {}'.format(s.lab_no), jumps=[ntgt])
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
87 # Explicitely add fallthrough:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
88 self.emit('jeq {}'.format(s.lab_yes), jumps=[ytgt, jmp_ins])
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
89 self.emit2(jmp_ins)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
90 else:
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
91 raise NotImplementedError('--> {}'.format(s))
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
92
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
93
211
99164160fb0b Added another missing file
Windel Bouwman
parents:
diff changeset
94 class ArmCodeGenerator:
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
95 def __init__(self, outs):
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
96 # TODO: schedule traces in better order.
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
97 # This is optional!
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
98 self.ins_sel = ArmInstructionSelector()
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
99 self.outs = outs
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
100 self.outs.getSection('code').address = 0x08000000
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
101 self.outs.getSection('data').address = 0x20000000
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
102
269
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
103 def generate(self, ircode, cfg_file=None, ig_file=None):
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
104 x = self.ins_sel.munchProgram(ircode)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
105 cfg = flowgraph.FlowGraph(x)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
106 if cfg_file:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
107 cfg.to_dot(cfg_file)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
108 ig = registerallocator.InterferenceGraph(cfg)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
109 if ig_file:
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
110 ig.to_dot(ig_file)
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
111
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
112 regs = ['r0', 'r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'r7']
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
113 ra = registerallocator.RegisterAllocator()
5f8c04a8d26b Towards better modularity
Windel Bouwman
parents: 268
diff changeset
114 ra.registerAllocate(ig, regs)
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
115
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 262
diff changeset
116