comparison python/testc3.py @ 268:5ec7580976d9

Op naar tree-IR
author Windel Bouwman
date Wed, 14 Aug 2013 20:12:40 +0200
parents c4370696ccc7
children e64bae57cda8
comparison
equal deleted inserted replaced
267:e7c8f7eb3f59 268:5ec7580976d9
124 124
125 def testPackage(self): 125 def testPackage(self):
126 p1 = """package p1; 126 p1 = """package p1;
127 type int A; 127 type int A;
128 """ 128 """
129 self.assertTrue(self.builder.build(p1))
129 p2 = """package p2; 130 p2 = """package p2;
130 import p1; 131 import p1;
131 var A b; 132 var A b;
132 """ 133 """
133 self.assertTrue(self.builder.build(p1))
134 self.expectOK(p2) 134 self.expectOK(p2)
135 135
136 def testFunctArgs(self): 136 def testFunctArgs(self):
137 snippet = """ 137 snippet = """
138 package testargs; 138 package testargs;
169 a = 1; 169 a = 1;
170 b = a * 2 + a * a; 170 b = a * 2 + a * a;
171 c = b * a - 3; 171 c = b * a - 3;
172 } 172 }
173 """ 173 """
174 block_code = """ a0 = alloc 174 self.expectOK(snippet)
175 b1 = alloc
176 c2 = alloc
177 const3 = 1
178 [a0] = const3
179 t4 = [a0]
180 const5 = 2
181 mul6 = t4 * const5
182 t7 = [a0]
183 t8 = [a0]
184 mul9 = t7 * t8
185 add10 = mul6 + mul9
186 [b1] = add10
187 t11 = [b1]
188 t12 = [a0]
189 mul13 = t11 * t12
190 const14 = 3
191 sub15 = mul13 - const14
192 [c2] = sub15
193 ret """
194 self.expectIR(snippet, block_code)
195 175
196 def testEmpty(self): 176 def testEmpty(self):
197 snippet = """ 177 snippet = """
198 package A 178 package A
199 """ 179 """
282 var int a, b; 262 var int a, b;
283 a = 2; 263 a = 2;
284 b = a + 2; 264 b = a + 2;
285 } 265 }
286 """ 266 """
287 block_code = """a0 = alloc 267 self.expectOK(snippet)
288 b1 = alloc
289 const2 = 2
290 [a0] = const2
291 t3 = [a0]
292 const4 = 2
293 add5 = t3 + const4
294 [b1] = add5
295 ret """
296 self.expectIR(snippet, block_code)
297 268
298 def testUnknownType(self): 269 def testUnknownType(self):
299 snippet = """package testlocalvar; 270 snippet = """package testlocalvar;
300 function void t() 271 function void t()
301 { 272 {
312 var struct {int x, y;} a; 283 var struct {int x, y;} a;
313 a.x = 2; 284 a.x = 2;
314 a.y = a.x + 2; 285 a.y = a.x + 2;
315 } 286 }
316 """ 287 """
317 block_code = """a0 = alloc 288 self.expectOK(snippet)
318 const1 = 2
319 off_x2 = 0
320 adr_x3 = a0 + off_x2
321 [adr_x3] = const1
322 off_x4 = 0
323 adr_x5 = a0 + off_x4
324 t6 = [adr_x5]
325 const7 = 2
326 add8 = t6 + const7
327 off_y9 = 4
328 adr_y10 = a0 + off_y9
329 [adr_y10] = add8
330 ret """
331 self.expectIR(snippet, block_code)
332 289
333 def testPointerType1(self): 290 def testPointerType1(self):
334 snippet = """ 291 snippet = """
335 package testpointer1; 292 package testpointer1;
336 var int* pa; 293 var int* pa;
379 var int* a; 336 var int* a;
380 a = cast<int*>(40); 337 a = cast<int*>(40);
381 *a = 2; 338 *a = 2;
382 } 339 }
383 """ 340 """
384 block_code = """a0 = alloc 341 self.expectOK(snippet)
385 const1 = 40
386 [a0] = const1
387 const2 = 2
388 deref3 = [a0]
389 [deref3] = const2
390 ret """
391 self.expectIR(snippet, block_code)
392 342
393 def testPointerTypeIr2(self): 343 def testPointerTypeIr2(self):
394 snippet = """ 344 snippet = """
395 package testptr_ir; 345 package testptr_ir;
396 type struct {int x,y;}* gpio; 346 type struct {int x,y;}* gpio;
400 a = cast<gpio>(40); 350 a = cast<gpio>(40);
401 a->x = 2; 351 a->x = 2;
402 a->y = a->x - 14; 352 a->y = a->x - 14;
403 } 353 }
404 """ 354 """
405 block_code = """a0 = alloc 355 self.expectOK(snippet)
406 const1 = 40
407 [a0] = const1
408 const2 = 2
409 deref3 = [a0]
410 off_x4 = 0
411 adr_x5 = deref3 + off_x4
412 [adr_x5] = const2
413 deref6 = [a0]
414 off_x7 = 0
415 adr_x8 = deref6 + off_x7
416 t9 = [adr_x8]
417 const10 = 14
418 sub11 = t9 - const10
419 deref12 = [a0]
420 off_y13 = 4
421 adr_y14 = deref12 + off_y13
422 [adr_y14] = sub11
423 ret """
424 self.expectIR(snippet, block_code)
425 356
426 def testWrongCast(self): 357 def testWrongCast(self):
427 snippet = """ 358 snippet = """
428 package testptr_ir; 359 package testptr_ir;
429 type struct {int x,y;}* gpio; 360 type struct {int x,y;}* gpio;