annotate python/ppci/frontends/ks/irgenerator.py @ 107:1544e7a4aa98

Improvements
author Windel Bouwman
date Tue, 01 Jan 2013 17:16:05 +0100
parents f2d980eef509
children 9e552d34bd60
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
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
8 from .builtin import real, integer, boolean, char
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
9
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
10 class KsIrGenerator:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
11 def __init__(self):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
12 pass
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
13
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
14 # Code generation functions:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
15 def genexprcode(self, node):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
16 """
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
17 Generate code for expressions!
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
18 Recursively evaluates, and ensures a register contains the answer.
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
19 register is an integer register or a floating point reg
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
20 """
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
21 if isinstance(node, Binop):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
22 """ Handle a binary operation (two arguments) of some kind """
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
23 self.genexprcode(node.a)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
24 self.genexprcode(node.b)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
25
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
26 if node.op == 'mod':
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
27 assert(node.typ.isType(integer))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
28 elif node.op == 'div':
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
29 assert(node.typ.isType(integer))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
30 elif node.op == '*':
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
31 if node.typ.isType(integer):
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
32 pass
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
33 elif node.op == '+':
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
34 if node.typ.isType(integer):
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
35 pass
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):
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
38 pass
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
39 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
40 Error('Unknown Binop {0}'.format(node.op))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
41
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
42 elif type(node) is Unop:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
43 if node.op == 'INTTOREAL':
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
44 self.genexprcode(node.a)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
45 code.append('Unop inttoreal TODO')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
46 elif node.op == 'ABS':
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
47 if isType(node.typ, real):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
48 Error('ABS error integer')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
49 elif isType(node.typ, integer):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
50 code = []
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
51 Error('ABS error integer')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
52 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
53 Error('ABS error')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
54 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
55 Error('Unknown Unop {0}'.format(node.op))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
56
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
57 elif isinstance(node, Designator):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
58 # dereference, array index. Make sure that the result comes into a register
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
59 if len(node.selectors) > 0:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
60 self.gencode(node) # Load the pointer into some register
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
61
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
62 # Now we can access the object at location '[node.reg]':
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
63 if node.typ.isType(integer):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
64 self.addCode( mov(node.reg, [node.reg, 0x0]) )
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
65 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
66 Error('Only integer types implemented')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
67 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
68 # No selectors, load variable directly
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
69 if node.obj.typ.isType(integer):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
70 if type(node.obj) is Constant:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
71 self.genexprcode(node.obj)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
72 node.reg = node.obj.reg
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
73 else:
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
74 pass
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
75 # Get a register to store the integer value
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
76 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
77 Error('Cannot load variable type {0}'.format(node.typ))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
78 elif type(node) is Constant:
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
79 print('TODO: constant')
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
80 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
81 Error('Cannot generate expression code for: {0}'.format(node))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
82
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
83 def gencode(self, node):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
84 """ Code generation function for AST nodes """
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
85 if isinstance(node, Module):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
86 # TODO: recurse!
107
1544e7a4aa98 Improvements
Windel Bouwman
parents: 106
diff changeset
87
1544e7a4aa98 Improvements
Windel Bouwman
parents: 106
diff changeset
88 print(node.symtable)
1544e7a4aa98 Improvements
Windel Bouwman
parents: 106
diff changeset
89 node.symtable.printTable()
1544e7a4aa98 Improvements
Windel Bouwman
parents: 106
diff changeset
90 funcs = node.symtable.getAllLocal(Procedure)
1544e7a4aa98 Improvements
Windel Bouwman
parents: 106
diff changeset
91 for f in funcs:
1544e7a4aa98 Improvements
Windel Bouwman
parents: 106
diff changeset
92 print('function', f)
1544e7a4aa98 Improvements
Windel Bouwman
parents: 106
diff changeset
93 ftype = f.typ.coreType()
1544e7a4aa98 Improvements
Windel Bouwman
parents: 106
diff changeset
94 ftype = core.FunctionType(retType)
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
95
107
1544e7a4aa98 Improvements
Windel Bouwman
parents: 106
diff changeset
96 self.mod = core.Module(node.name)
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
97 # Create a function called init for this module:
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
98 ftype = core.FunctionType(self.context.VoidType, [])
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
99 func = core.Function(ftype, "init", self.mod)
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
100 bb = self.gencode(node.initcode)
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
101 self.mod.dump()
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
102 return self.mod
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
104 elif type(node) is Procedure:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
105 # calculate offsets for local variables and parameters
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
106 # Variable location relative to 'rbp' register
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
107 variables = node.symtable.getAllLocal(Variable)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
108
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
109 elif isinstance(node, StatementSequence):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
110 for s in node.statements:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
111 self.gencode(s)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
112
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
113 elif type(node) is ProcedureCall:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
114 # Prepare parameters on the stack:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
115 assert(len(node.args) == len(node.proc.typ.parameters))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
116 for arg, param in zip(node.args, node.proc.typ.parameters):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
117
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
118 if param.kind == 'value':
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
119 self.genexprcode(arg)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
120 self.addCode( push(arg.reg) )
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
121 self.freereg( arg )
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
122 stacksize += 8
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
123 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
124 Error('Parameter kind other than value')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
125
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
126 elif type(node) is Assignment:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
127 if node.lval.typ.isType(integer):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
128 # TODO if node.rval is Constant of some datatype, move it to mem directly
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
129 self.genexprcode(node.rval) # Calculate the value that has to be stored.
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
130 print("TODO")
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
131 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
132 Error('Assignments of other types not implemented')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
133 # TODO if left and right are designators, do some sort of memcpy.
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
134
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
135 elif type(node) is IfStatement:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
136 self.genexprcode(node.condition)
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
137 print("TODO IF")
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
138 if node.falsestatement:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
139 # If with else clause
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
140 pass
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
141 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
142 # If without else clause
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
143 pass
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
144
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
145 elif isinstance(node, WhileStatement):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
146 self.genexprcode(node.condition)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
147 self.gencode(node.dostatements)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
148 elif type(node) is ForStatement:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
149 # Initial load of iterator variable:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
150 self.genexprcode(node.begin)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
151 self.genexprcode(node.end)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
152 self.gencode(node.statements)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
153 Error('No implementation of FOR statement')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
154
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
155 elif type(node) is AsmCode:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
156 def processOperand(op):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
157 if type(op) is list:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
158 if type(op[0]) is Variable:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
159 var = op[0]
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
160 if var.isLocal:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
161 return ['rbp', var.offset]
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
162 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
163 Error('Can only use local variables in inline assembler')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
164 return op
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
165 for asmline in node.asmcode:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
166 opcode, operands = asmline
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
167 operands = [processOperand(opx) for opx in operands]
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
168 print('assembling', opcode, *operands)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
169 func,nargs = opcodes[opcode]
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
170 code = func(*operands)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
171 self.addCode(code)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
172
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
173 elif isinstance(node, EmptyStatement):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
174 pass
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
175
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
176 elif type(node) is StringConstant:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
177 self.strings.append(node)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
178 self.data.append(node.value) # Add string to the data section
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
179
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
180 elif type(node) is Designator:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
181 if len(node.selectors) > 0:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
182 self.getreg(node)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
183 # Load starting address
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
184 if node.obj.isLocal:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
185 self.addCode( leareg64(node.reg, ['rbp', node.obj.offset]) )
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
186 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
187 # Global variables need to be relocated...
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
188 self.addCode(leareg64(node.reg, ['RIP', 0]))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
189 self.fixCode(self.rip - 4, imm32(node.obj.offset - self.rip))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
190 # Loop over all designators..
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
191 for selector in node.selectors:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
192 if type(selector) is Index:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
193 # Deref an array index
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
194 self.genexprcode(selector.index)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
195 self.freereg(selector)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
196 elif type(selector) is Field:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
197 print('Field')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
198 Error('Field not implemented')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
199 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
200 Error('Unknown selector')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
201 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
202 Error('Can only gencode for designator with selectors')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
203 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
204 print('not generating code for {0}'.format(node))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
205
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
206 def generateIr(self, context, ast):
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
207 """ ir generation front end """
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
208 # Create a new context for this code.
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
209 self.context = context
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
210 return self.gencode(ast)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
211