view python/ir/asmwriter.py @ 155:b28a11c01dbe

Simplified IR classes
author Windel Bouwman
date Sun, 03 Mar 2013 13:20:03 +0100
parents 4e79484a9d47
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)