annotate python/c3/codegenerator.py @ 157:8f3924b6076e

Added some code generator things
author Windel Bouwman
date Sun, 03 Mar 2013 18:14:35 +0100
parents 1b4a85bdd99c
children 9683a4cd848f
rev   line source
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
1 import ir
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
2 from . import astnodes
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents:
diff changeset
3
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
4 def genModule(pkg):
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
5 m = ir.Module(pkg.name)
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
6 for s in pkg.scope:
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
7 if type(s) is astnodes.Variable:
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
8 genGlobal(m, s)
156
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
9 elif type(s) is astnodes.Function:
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
10 genFunction(m, s)
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
11 else:
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
12 print(s)
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
13 return m
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
14
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
15 def genGlobal(m, var):
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
16 v = ir.Value()
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
17 v.name = var.name
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
18 m.Globals.append(v)
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents:
diff changeset
19
156
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
20 def genFunction(m, fnc):
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
21 ft = genType(fnc.typ)
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
22 f = ir.Function(fnc.name, ft)
156
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
23 m.Globals.append(f)
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
24 bb = ir.BasicBlock()
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
25 f.BasicBlocks.append(bb)
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
26 genCode(bb, fnc.body)
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
27
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
28 def genCode(bb, code):
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
29 if type(code) is astnodes.CompoundStatement:
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
30 for s in code.statements:
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
31 genCode(bb, s)
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
32 elif type(code) is astnodes.Assignment:
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
33 genCode(bb, code.rval)
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
34 print('assign')
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
35 elif type(code) is astnodes.IfStatement:
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
36 genCode(bb, code.condition)
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
37 genCode(bb, code.truestatement)
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
38 print('If!')
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
39 elif type(code) is astnodes.Binop:
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
40 genCode(bb, code.a)
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
41 genCode(bb, code.b)
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
42 a = 1
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
43 b = 2
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
44 if code.op == '+':
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
45 bb.Instructions.append(ir.AddInstruction(a, b))
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
46 else:
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
47 bb.Instructions.append(ir.BinaryOperator(code.op, a, b))
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
48 elif type(code) is astnodes.Constant:
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
49 print('CST')
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
50 bb.Instructions.append(ir.ImmLoadInstruction(code.value))
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
51 else:
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
52 print('Unknown:', code)
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
53
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
54 def genType(t):
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 156
diff changeset
55 return ir.Type()
156
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
56
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents:
diff changeset
57 class CodeGenerator:
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents:
diff changeset
58 """ Generates intermediate code """
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents:
diff changeset
59 def gencode(self, ast):
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
60 assert type(ast) is astnodes.Package
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
61 return genModule(ast)
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents:
diff changeset
62