diff python/ir/asmwriter.py @ 147:4e79484a9d47

Moved core to ir folder
author Windel Bouwman
date Fri, 22 Feb 2013 10:33:48 +0100
parents python/ppci/core/asmwriter.py@9e552d34bd60
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/ir/asmwriter.py	Fri Feb 22 10:33:48 2013 +0100
@@ -0,0 +1,43 @@
+
+from . import llvmtype
+from .instruction import BinaryOperator
+#typeNames[VoidType] = 'void'
+
+class AsmWriter:
+   def __init__(self):
+      self.typeNames = {}
+      self.typeNames[llvmtype.typeID.Void] = 'void'
+   def printModule(self, module):
+      if module.Identifier:
+         print('; ModuleID = {0}'.format(module.Identifier))
+      # Print functions:
+      for f in module.Functions:
+         self.printFunction(f)
+   def printFunction(self, f):
+      # TODO: if definition:
+
+      t = self.strType(f.ReturnType.tid)
+      args = '()'
+      print('define {0} {1}{2}'.format(t, f.name, args))
+      print('{')
+      for bb in f.BasicBlocks:
+         print('basic block!')
+         self.printBasicBlock(bb)
+      print('}')
+
+   def strType(self, t):
+      return self.typeNames[t]
+      
+   def printBasicBlock(self, bb):
+      if bb.Name:
+         # print label
+         print('{0}:'.format(bb.Name))
+      for instr in bb.Instructions:
+         self.printInstruction(instr)
+   def printInstruction(self, i):
+      print('Instruction!')
+      if isinstance(i, BinaryOperator):
+         print(i.operation, i.value1.Name, i.value2.Name)
+      else:
+         print(i)
+