view python/ppci/core/asmwriter.py @ 110:9e552d34bd60

Work on compiler
author Windel Bouwman
date Fri, 04 Jan 2013 15:25:58 +0100
parents 1544e7a4aa98
children
line wrap: on
line source


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)