diff python/c3/codegenerator.py @ 158:9683a4cd848f

Added some functions for code generation
author Windel Bouwman
date Fri, 08 Mar 2013 16:52:44 +0100
parents 8f3924b6076e
children 0b5b2ee6b435
line wrap: on
line diff
--- a/python/c3/codegenerator.py	Sun Mar 03 18:14:35 2013 +0100
+++ b/python/c3/codegenerator.py	Fri Mar 08 16:52:44 2013 +0100
@@ -20,36 +20,73 @@
 def genFunction(m, fnc):
    ft = genType(fnc.typ)
    f = ir.Function(fnc.name, ft)
-   m.Globals.append(f)
+   m.Functions.append(f)
    bb = ir.BasicBlock()
    f.BasicBlocks.append(bb)
    genCode(bb, fnc.body)
+   bb.Instructions.append(ir.RetInstruction())
 
 def genCode(bb, code):
    if type(code) is astnodes.CompoundStatement:
       for s in code.statements:
          genCode(bb, s)
    elif type(code) is astnodes.Assignment:
-      genCode(bb, code.rval)
-      print('assign')
+      genExprCode(bb, code.rval)
+      # TODO: store
    elif type(code) is astnodes.IfStatement:
-      genCode(bb, code.condition)
+      genExprCode(bb, code.condition)
+      # TODO: implement IF.
+      t1, t2 = 1,2
+      b = ir.BranchInstruction(t1, t2)
+      bb.Instructions.append(b)
       genCode(bb, code.truestatement)
-      print('If!')
-   elif type(code) is astnodes.Binop:
-      genCode(bb, code.a)
-      genCode(bb, code.b)
-      a = 1
-      b = 2
-      if code.op == '+':
-         bb.Instructions.append(ir.AddInstruction(a, b))
+      genCode(bb, code.falsestatement)
+   elif type(code) is astnodes.ProcedureCall:
+      ins = ir.CallInstruction('f', [])
+      bb.Instructions.append(ins)
+   elif type(code) is astnodes.EmptyStatement:
+      pass
+   elif type(code) is astnodes.ReturnStatement:
+      bb.Instructions.append(ir.RetInstruction())
+   else:
+      print('Unknown stmt:', code)
+
+def NumGen():
+   a = 0
+   while True:
+      yield a
+      a = a + 1
+
+nums = NumGen()
+def unique():
+   return 'tmp{0}'.format(nums.__next__())
+
+def genExprCode(bb, code):
+   if type(code) is astnodes.Binop:
+      a = genExprCode(bb, code.a)
+      b = genExprCode(bb, code.b)
+      ops = {'+': 'fadd', '-': 'fsub', '*':'fmul', '/':'fdiv'}
+      if code.op in ops:
+         op = ops[code.op]
+         tmp = unique()
+         ins = ir.BinaryOperator(tmp, op, a, b)
+         bb.Instructions.append(ins)
+         return tmp
       else:
-         bb.Instructions.append(ir.BinaryOperator(code.op, a, b))
+         print('Unknown binop')
+         bb.Instructions.append(ir.BinaryOperator('unk2', code.op, a, b))
+         return 'unk2'
    elif type(code) is astnodes.Constant:
-      print('CST')
-      bb.Instructions.append(ir.ImmLoadInstruction(code.value))
+      tmp = unique()
+      bb.Instructions.append(ir.LoadInstruction(tmp, code.value))
+      return tmp
+   elif type(code) is astnodes.VariableUse:
+      tmp = unique()
+      ins = ir.LoadInstruction(tmp, code.target.name)
+      return tmp
    else:
-      print('Unknown:', code)
+      print('Unknown expr:', code)
+      return 'unk'
 
 def genType(t):
    return ir.Type()