annotate python/x86.py @ 158:9683a4cd848f

Added some functions for code generation
author Windel Bouwman
date Fri, 08 Mar 2013 16:52:44 +0100
parents 8f3924b6076e
children 10330be89bc2
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
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
21
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
22 def emit(self, i):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
23 self.asm.append(i)
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
24
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
25 def genBin(self, i):
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
26 self.asm = []
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
27 self.genModule(i)
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents:
diff changeset
28
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
29 def genModule(self, m):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
30 for g in m.Globals:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
31 self.emit(AsmLabel(g.name))
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
32 # Ignore types for now ..
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
33 self.emit('dw 0')
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
34 for f in m.Functions:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
35 self.genFunction(f)
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
36 def genFunction(self, f):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
37 self.emit('global {0}'.format(f.name))
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
38 self.emit(AsmLabel(f.name))
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
39 for bb in f.BasicBlocks:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
40 self.genBB(bb)
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
41 def genBB(self, bb):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
42 for i in bb.Instructions:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
43 self.genIns(i)
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
44 def genIns(self, i):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
45 if type(i) is ir.BinaryOperator:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
46 if i.operation == 'fadd':
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
47 r = 'rax'
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
48 self.emit(Op('add', r, '11'))
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
49 elif type(i) is ir.RetInstruction:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
50 self.emit('ret')
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
51 else:
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
52 print('Unknown ins', i)
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
53