annotate python/x86.py @ 160:10330be89bc2

Started from scratch with code edit
author Windel Bouwman
date Sat, 09 Mar 2013 11:56:48 +0100
parents 9683a4cd848f
children 3eb9b9e2958d
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:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
11 def __init__(self, op, a, b):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
12 self.op = op
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
13 self.a = a
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
14 self.b = b
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
15 def __repr__(self):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
16 return '{0} {1}, {2}'.format(self.op, self.a, self.b)
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
17
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
18 class X86CodeGen:
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
19 def __init__(self, diag):
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
20 self.diag = diag
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
21 self.regs = ['rax', 'rbx', 'rcx', 'rdx']
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
22
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
23 def emit(self, i):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
24 self.asm.append(i)
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
25 def allocateReg(self, typ):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
26 return 'ax'
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
27 def deallocateReg(self, r):
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
28 pass
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
29
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
30 def genBin(self, i):
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
31 self.asm = []
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
32 self.genModule(i)
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
33
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
34 def genModule(self, m):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
35 for g in m.Globals:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
36 self.emit(AsmLabel(g.name))
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
37 # Ignore types for now ..
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
38 self.emit('dw 0')
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
39 for f in m.Functions:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
40 self.genFunction(f)
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
41 def genFunction(self, f):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
42 self.emit('global {0}'.format(f.name))
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
43 self.emit(AsmLabel(f.name))
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
44 for bb in f.BasicBlocks:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
45 self.genBB(bb)
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
46 def genBB(self, bb):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
47 for i in bb.Instructions:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
48 self.genIns(i)
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
49 def genIns(self, i):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
50 if type(i) is ir.BinaryOperator:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
51 if i.operation == 'fadd':
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
52 r = 'rax'
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
53 self.emit(Op('add', r, '11'))
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
54 elif type(i) is ir.LoadInstruction:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
55 r = 'rbx'
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
56 self.emit(Op('mov', r, '{0}'.format(i.value)))
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
57 elif type(i) is ir.RetInstruction:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
58 self.emit('ret')
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
59 elif type(i) is ir.CallInstruction:
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
60 self.emit('call')
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
61 else:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
62 print('Unknown ins', i)
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
63