Mercurial > lcfOS
comparison python/c3/codegenerator.py @ 225:1c7364bd74c7
Fixed pointer deref
author | Windel Bouwman |
---|---|
date | Thu, 11 Jul 2013 07:42:30 +0200 |
parents | c3f1ce8b638f |
children | 82dfe6a32717 |
comparison
equal
deleted
inserted
replaced
224:5af52987f5bd | 225:1c7364bd74c7 |
---|---|
122 else: | 122 else: |
123 self.builder.addIns(ir.Branch(bbfalse)) | 123 self.builder.addIns(ir.Branch(bbfalse)) |
124 else: | 124 else: |
125 print('Unknown cond', expr) | 125 print('Unknown cond', expr) |
126 def genExprCode(self, expr): | 126 def genExprCode(self, expr): |
127 if type(expr) is astnodes.Binop: | 127 if type(expr) is astnodes.Binop: |
128 ra = self.genExprCode(expr.a) | 128 ra = self.genExprCode(expr.a) |
129 rb = self.genExprCode(expr.b) | 129 rb = self.genExprCode(expr.b) |
130 ops = ['+', '-', '*', '/', '|', '&'] | 130 ops = ['+', '-', '*', '/', '|', '&'] |
131 if expr.op in ops: | 131 if expr.op in ops: |
132 tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', '&':'and'} | 132 tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', '&':'and'} |
138 else: | 138 else: |
139 print('Unknown {0}'.format(expr)) | 139 print('Unknown {0}'.format(expr)) |
140 tmp = self.builder.newTmp() | 140 tmp = self.builder.newTmp() |
141 # TODO | 141 # TODO |
142 return tmp | 142 return tmp |
143 elif type(expr) is astnodes.Unop: | 143 elif type(expr) is astnodes.Unop: |
144 ra = self.genExprCode(expr.a) | 144 ra = self.genExprCode(expr.a) |
145 if expr.op == '&': | 145 if expr.op == '&': |
146 # Address of operator | 146 # Address of operator |
147 tmp = self.builder.newTmp('addr') | 147 tmp = self.builder.newTmp('addr') |
148 return tmp | |
149 #self.builder.addIns(ins) | |
150 else: | |
151 raise Exception('Unknown {0}'.format(expr)) | |
152 elif type(expr) is astnodes.Constant: | |
153 tmp = self.builder.newTmp() | |
154 # TODO | |
148 return tmp | 155 return tmp |
149 #self.builder.addIns(ins) | 156 elif type(expr) is astnodes.VariableUse: |
150 elif expr.op == '*': | 157 tmp = self.builder.newTmp() |
158 loc = self.varMap[expr.target] | |
159 i = ir.Load(loc, tmp) | |
160 self.builder.addIns(i) | |
161 return tmp | |
162 elif type(expr) is astnodes.Deref: | |
151 # dereference pointer type: | 163 # dereference pointer type: |
164 addr = self.genExprCode(expr.ptr) | |
152 tmp = self.builder.newTmp('deref') | 165 tmp = self.builder.newTmp('deref') |
153 ins = ir.Load(ra, tmp) | 166 ins = ir.Load(addr, tmp) |
154 self.builder.addIns(ins) | 167 self.builder.addIns(ins) |
155 return tmp | 168 return tmp |
156 else: | 169 elif type(expr) is astnodes.Literal: |
157 print('Unknown {0}'.format(expr)) | 170 tmp = self.builder.newTmp() |
158 elif type(expr) is astnodes.Constant: | 171 ins = ir.ImmLoad(tmp, expr.val) |
159 tmp = self.builder.newTmp() | 172 self.builder.addIns(ins) |
160 # TODO | 173 return tmp |
161 return tmp | 174 elif type(expr) is astnodes.TypeCast: |
162 elif type(expr) is astnodes.VariableUse: | |
163 tmp = self.builder.newTmp() | |
164 loc = self.varMap[expr.target] | |
165 i = ir.Load(loc, tmp) | |
166 self.builder.addIns(i) | |
167 return tmp | |
168 elif type(expr) is astnodes.Literal: | |
169 tmp = self.builder.newTmp() | |
170 ins = ir.ImmLoad(tmp, expr.val) | |
171 self.builder.addIns(ins) | |
172 return tmp | |
173 elif type(expr) is astnodes.TypeCast: | |
174 ar = self.genExprCode(expr.a) | 175 ar = self.genExprCode(expr.a) |
175 if isinstance(expr.to_type, astnodes.PointerType): | 176 if isinstance(expr.to_type, astnodes.PointerType): |
176 if expr.a.typ is intType: | 177 if expr.a.typ is intType: |
177 return ar | 178 return ar |
178 elif isinstance(expr.a.typ, astnodes.PointerType): | 179 elif isinstance(expr.a.typ, astnodes.PointerType): |
179 return ar | 180 return ar |
180 else: | 181 else: |
181 raise Exception() | 182 raise Exception() |
182 elif type(expr) is astnodes.FunctionCall: | 183 elif type(expr) is astnodes.FunctionCall: |
183 tmp = self.builder.newTmp("res") | 184 tmp = self.builder.newTmp("res") |
184 args = [] | 185 args = [] |
185 for arg in expr.args: | 186 for arg in expr.args: |
186 ar = self.genExprCode(arg) | 187 ar = self.genExprCode(arg) |
187 args.append(ar) | 188 args.append(ar) |
188 fn = self.funcMap[expr.proc] | 189 fn = self.funcMap[expr.proc] |
189 ins = ir.Call(fn, args, tmp) | 190 ins = ir.Call(fn, args, tmp) |
190 self.builder.addIns(ins) | 191 self.builder.addIns(ins) |
191 return tmp | 192 return tmp |
192 else: | 193 else: |
193 raise CompilerError('Unknown expr {}'.format(expr)) | 194 raise CompilerError('Unknown expr {}'.format(expr)) |
194 | 195 |