annotate python/ppci/frontends/ks/irgenerator.py @ 110:9e552d34bd60

Work on compiler
author Windel Bouwman
date Fri, 04 Jan 2013 15:25:58 +0100
parents 1544e7a4aa98
children
rev   line source
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
1 """
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
2 Generates ir code from ast tree.
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
3 """
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
4
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
5 from .nodes import *
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
6 from ...core.errors import Error
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
7 from ... import core
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
8 from .builtin import real, integer, boolean, char, void
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
9
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
10 def coreType(typ):
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
11 """ Return the correct core type given a type """
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
12 if type(typ) is BaseType:
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
13 if typ is integer:
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
14 return core.i32
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
15 if typ is void:
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
16 return core.void
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
17 elif type(typ) is ProcedureType:
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
18 rType = coreType(typ.returntype)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
19 fpTypes = [coreType(p.typ) for p in typ.parameters]
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
20 return core.FunctionType(rType, fpTypes)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
21 print(typ)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
22 raise NotImplementedError()
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
23
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
24 class KsIrGenerator:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
25 def __init__(self):
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
26 self.builder = core.IRBuilder()
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
27 # Code generation functions:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
28 def genexprcode(self, node):
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
29 """ Generate code for expressions! """
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
30 if isinstance(node, Binop):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
31 """ Handle a binary operation (two arguments) of some kind """
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
32 lhs = self.genexprcode(node.a)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
33 rhs = self.genexprcode(node.b)
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
34
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
35 if node.op == '*':
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
36 if node.typ.isType(integer):
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
37 return self.builder.createMul(lhs, rhs)
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
38 elif node.op == '+':
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
39 if node.typ.isType(integer):
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
40 return self.builder.createAdd(lhs, rhs)
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
41 elif node.op == '-':
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
42 if node.typ.isType(integer):
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
43 return self.builder.createSub(lhs, rhs)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
44 Error('Unknown binop or type {0}'.format(node))
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
45
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
46 elif isinstance(node, Designator):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
47 # dereference, array index. Make sure that the result comes into a register
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
48 if len(node.selectors) > 0:
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
49 Error('Only integer types implemented')
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
50 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
51 # No selectors, load variable directly
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
52 print(node)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
53 #Error('Cannot load variable type {0}'.format(node.typ))
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
54 elif type(node) is Constant:
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
55 return core.Constant(node.value, coreType(node.typ))
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
56 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
57 Error('Cannot generate expression code for: {0}'.format(node))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
58
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
59 def gencode(self, node):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
60 """ Code generation function for AST nodes """
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
61 if isinstance(node, Module):
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
62 # Create module:
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
63 self.mod = core.Module(node.name)
107
1544e7a4aa98 Improvements
Windel Bouwman
parents: 106
diff changeset
64
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
65 globs = node.symtable.getAllLocal(Variable)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
66 for g in globs:
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
67 print('global:', g)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
68 # Loop over all functions:
107
1544e7a4aa98 Improvements
Windel Bouwman
parents: 106
diff changeset
69 print(node.symtable)
1544e7a4aa98 Improvements
Windel Bouwman
parents: 106
diff changeset
70 node.symtable.printTable()
1544e7a4aa98 Improvements
Windel Bouwman
parents: 106
diff changeset
71 funcs = node.symtable.getAllLocal(Procedure)
1544e7a4aa98 Improvements
Windel Bouwman
parents: 106
diff changeset
72 for f in funcs:
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
73 self.gencode(f)
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
74 # Create a function called init for this module:
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
75 self.mod.dump()
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
76 return self.mod
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
77
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
78 elif type(node) is Procedure:
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
79 ftype = coreType(node.typ)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
80 print('function', node, ftype)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
81 func = core.Function(ftype, node.name, self.mod)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
82 bb = core.BasicBlock()
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
83 func.basicblocks.append(bb)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
84 self.builder.setInsertPoint(bb)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
85 self.gencode(node.block)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
86 self.builder.setInsertPoint(None)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
87 variables = node.symtable.getAllLocal(Variable)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
88 print(variables)
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
89
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
90 elif isinstance(node, StatementSequence):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
91 for s in node.statements:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
92 self.gencode(s)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
93
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
94 elif type(node) is ProcedureCall:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
95 # Prepare parameters on the stack:
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
96 print("TODO")
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
97
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
98 elif type(node) is Assignment:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
99 if node.lval.typ.isType(integer):
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
100 print('assign')
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
101 rhs = self.genexprcode(node.rval) # Calculate the value that has to be stored.
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
102 #self.gencode(node.lval)
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
103 print("TODO: assigment")
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
104
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
105 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
106 Error('Assignments of other types not implemented')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
107
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
108 elif type(node) is IfStatement:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
109 self.genexprcode(node.condition)
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
110 print("TODO IF")
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
111 if node.falsestatement:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
112 # If with else clause
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
113 pass
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
114 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
115 # If without else clause
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
116 pass
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
117
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
118 elif isinstance(node, WhileStatement):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
119 self.genexprcode(node.condition)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
120 self.gencode(node.dostatements)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
121 elif type(node) is ForStatement:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
122 # Initial load of iterator variable:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
123 self.genexprcode(node.begin)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
124 self.genexprcode(node.end)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
125 self.gencode(node.statements)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
126 Error('No implementation of FOR statement')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
127
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
128 elif isinstance(node, EmptyStatement):
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
129 pass # That was easy :)
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
130
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
131 elif type(node) is StringConstant:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
132 self.strings.append(node)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
133
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
134 elif type(node) is Designator:
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 107
diff changeset
135 Error('Can only gencode for designator with selectors')
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
136 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
137 print('not generating code for {0}'.format(node))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
138
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
139 def generateIr(self, context, ast):
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
140 """ ir generation front end """
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
141 # Create a new context for this code.
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
142 self.context = context
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
143 return self.gencode(ast)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
144