annotate python/ks/irgenerator.py @ 192:6cd6260789a1

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