annotate python/ppci/frontends/ks/irgenerator.py @ 106:f2d980eef509

improved code generation
author Windel Bouwman
date Mon, 31 Dec 2012 18:26:56 +0100
parents ed230e947dc6
children 1544e7a4aa98
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
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
79 elif isinstance(node, Relop):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
80 # Create a boolean from operands
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
81 # TODO create an alternative for expressions used as conditions.
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
82 self.genexprcode(node.a)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
83 self.genexprcode(node.b)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
84
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
85 if node.a.typ.isType(integer):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
86 self.freereg(node.b)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
87 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
88 Error('Relop not implemented for {0}'.format(node.a.typ))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
89
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
90 elif type(node) is Constant:
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
91 print('TODO: constant')
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
92
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
93 elif type(node) is ProcedureCall:
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
94 Error('TODO: proc call')
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
95 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
96 Error('Cannot generate expression code for: {0}'.format(node))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
97
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
98 def gencode(self, node):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
99 """ Code generation function for AST nodes """
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
100 if isinstance(node, Module):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
101 # TODO: recurse!
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
102
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
103 self.mod = core.Module()
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
104 # Create a function called init for this module:
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
105 ftype = core.FunctionType(self.context.VoidType, [])
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
106 func = core.Function(ftype, "init", self.mod)
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
107 bb = self.gencode(node.initcode)
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
108 self.mod.dump()
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
109 return self.mod
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
110
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
111 elif type(node) is Procedure:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
112 # calculate offsets for local variables and parameters
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
113 # Variable location relative to 'rbp' register
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
114 variables = node.symtable.getAllLocal(Variable)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
115
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
116 elif isinstance(node, StatementSequence):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
117 for s in node.statements:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
118 self.gencode(s)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
119
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
120 elif type(node) is ProcedureCall:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
121 # Prepare parameters on the stack:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
122 assert(len(node.args) == len(node.proc.typ.parameters))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
123 for arg, param in zip(node.args, node.proc.typ.parameters):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
124
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
125 if param.kind == 'value':
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
126 self.genexprcode(arg)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
127 self.addCode( push(arg.reg) )
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
128 self.freereg( arg )
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
129 stacksize += 8
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
130 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
131 Error('Parameter kind other than value')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
132
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
133 elif type(node) is Assignment:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
134 if node.lval.typ.isType(integer):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
135 # TODO if node.rval is Constant of some datatype, move it to mem directly
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
136 self.genexprcode(node.rval) # Calculate the value that has to be stored.
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
137 print("TODO")
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
138 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
139 Error('Assignments of other types not implemented')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
140 # TODO if left and right are designators, do some sort of memcpy.
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
141
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
142 elif type(node) is IfStatement:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
143 self.genexprcode(node.condition)
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
144 print("TODO IF")
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
145 if node.falsestatement:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
146 # If with else clause
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
147 pass
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
148 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
149 # If without else clause
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
150 pass
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
151
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
152 elif isinstance(node, WhileStatement):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
153 self.genexprcode(node.condition)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
154 self.gencode(node.dostatements)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
155 elif type(node) is ForStatement:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
156 # Initial load of iterator variable:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
157 self.genexprcode(node.begin)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
158 self.genexprcode(node.end)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
159 self.gencode(node.statements)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
160 Error('No implementation of FOR statement')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
161
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
162 elif type(node) is AsmCode:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
163 def processOperand(op):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
164 if type(op) is list:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
165 if type(op[0]) is Variable:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
166 var = op[0]
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
167 if var.isLocal:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
168 return ['rbp', var.offset]
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
169 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
170 Error('Can only use local variables in inline assembler')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
171 return op
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
172 for asmline in node.asmcode:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
173 opcode, operands = asmline
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
174 operands = [processOperand(opx) for opx in operands]
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
175 print('assembling', opcode, *operands)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
176 func,nargs = opcodes[opcode]
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
177 code = func(*operands)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
178 self.addCode(code)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
179
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
180 elif isinstance(node, EmptyStatement):
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
181 pass
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
182
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
183 elif type(node) is StringConstant:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
184 self.strings.append(node)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
185 self.data.append(node.value) # Add string to the data section
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
186
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
187 elif type(node) is Designator:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
188 if len(node.selectors) > 0:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
189 self.getreg(node)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
190 # Load starting address
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
191 if node.obj.isLocal:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
192 self.addCode( leareg64(node.reg, ['rbp', node.obj.offset]) )
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
193 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
194 # Global variables need to be relocated...
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
195 self.addCode(leareg64(node.reg, ['RIP', 0]))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
196 self.fixCode(self.rip - 4, imm32(node.obj.offset - self.rip))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
197 # Loop over all designators..
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
198 for selector in node.selectors:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
199 if type(selector) is Index:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
200 # Deref an array index
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
201 self.genexprcode(selector.index)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
202 self.freereg(selector)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
203 elif type(selector) is Field:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
204 print('Field')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
205 Error('Field not implemented')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
206 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
207 Error('Unknown selector')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
208 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
209 Error('Can only gencode for designator with selectors')
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
210 else:
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
211 print('not generating code for {0}'.format(node))
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
212
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
213 def generateIr(self, context, ast):
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
214 """ ir generation front end """
104
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
215 # Create a new context for this code.
ed230e947dc6 Added hexviewer
windel
parents: 103
diff changeset
216 self.context = context
103
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
217 return self.gencode(ast)
28a35161ef23 Added forgotten ir generator
windel
parents:
diff changeset
218