view python/ppci/core/asmwriter.py @ 143:1cc59ac80950

Fixed halt button
author Windel Bouwman
date Sun, 27 Jan 2013 13:18:53 +0100
parents 9e552d34bd60
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)