annotate python/x86.py @ 171:3eb9b9e2958d

Improved IR code
author Windel Bouwman
date Wed, 03 Apr 2013 22:20:20 +0200
parents 10330be89bc2
children c1d2b6b9f9a7
rev   line source
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
1 import ppci
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
2 import ir
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
3
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
4 class AsmLabel:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
5 def __init__(self, lab):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
6 self.lab = lab
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
7 def __repr__(self):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
8 return '{0}:'.format(self.lab)
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
9
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
10 class Op:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
11 def __init__(self, op, dst, src):
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
12 self.op = op
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
13 self.src = src
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
14 self.dst = dst
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
15 def __repr__(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
16 return '{0} {1}, {2}'.format(self.op, self.dst, self.src)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
17
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
18 class Jmp:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
19 def __init__(self, j, target):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
20 self.j = j
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
21 self.target = target
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
22 def __repr__(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
23 return '{0} {1}'.format(self.j, self.target)
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
24
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
25 class X86CodeGen:
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
26 def __init__(self, diag):
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
27 self.diag = diag
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
28 self.regs = ['rax', 'rbx', 'rcx', 'rdx']
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
29
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
30 def emit(self, i):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
31 self.asm.append(i)
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
32
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
33 def genBin(self, ir):
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
34 self.asm = []
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
35 # Allocate registers:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
36 ir.registerAllocate(self.regs)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
37 self.genModule(ir)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
38 return self.asm
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
39
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
40 def genModule(self, ir):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
41 #for f in ir.Functions:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
42 # self.genFunction(f)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
43 for bb in ir.BasicBlocks:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
44 self.genBB(bb)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
45 def genFunction(self, f):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
46 self.emit('global {0}'.format(f.name))
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
47 self.emit(AsmLabel(f.name))
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
48 for bb in f.BasicBlocks:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
49 self.genBB(bb)
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
50 def genBB(self, bb):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
51 self.emit(AsmLabel(bb.name))
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
52 for i in bb.Instructions:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
53 self.genIns(i)
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
54 def genIns(self, i):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
55 if type(i) is ir.BinaryOperator:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
56 ops = {'+':'add', '-':'sub', '*':'mul'}
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
57 if i.operation in ops:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
58 self.emit(Op('mov', i.result.reg, i.value1.reg))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
59 self.emit(Op(ops[i.operation], i.result.reg, i.value2.reg))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
60 else:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
61 raise NotImplementedError('op {0}'.format(i.operation))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
62 elif type(i) is ir.Load:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
63 self.emit(Op('mov', i.value, '[{0}]'.format(i.name)))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
64 elif type(i) is ir.Return:
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
65 self.emit('ret')
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
66 elif type(i) is ir.Call:
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
67 self.emit('call')
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
68 elif type(i) is ir.ImmLoad:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
69 self.emit(Op('mov', i.target, i.value))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
70 elif type(i) is ir.Store:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
71 self.emit(Op('mov', '[{0}]'.format(i.name), i.value))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
72 elif type(i) is ir.ConditionalBranch:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
73 self.emit(Op('cmp', i.a, i.b))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
74 jmps = {'>':'jg', '<':'jl', '==':'je'}
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
75 if i.cond in jmps:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
76 j = jmps[i.cond]
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
77 self.emit(Jmp(j, i.lab1.name))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
78 else:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
79 raise NotImplementedError('condition {0}'.format(i.cond))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
80 self.emit(Jmp('jmp', i.lab2.name))
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
81 elif type(i) is ir.Branch:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
82 self.emit(Jmp('jmp', i.target.name))
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
83 else:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 160
diff changeset
84 raise NotImplementedError('{0}'.format(i))
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
85