annotate python/ppci/frontends/ks/irgenerator.py @ 104:ed230e947dc6

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