view python/ir/module.py @ 174:3eb06f5fb987

Added memory alloc for locals
author Windel Bouwman
date Fri, 19 Apr 2013 19:22:52 +0200
parents c1d2b6b9f9a7
children 460db5669efa
line wrap: on
line source

# IR-Structures:
from .instruction import *
from .basicblock import BasicBlock

class Module:
   """ Main container for a piece of code. """
   def __init__(self, name):
      self.name = name
      self.funcs = []
   def __repr__(self):
      return 'IR-module [{0}]'.format(self.name)
   def getInstructions(self):
      ins = []
      for bb in self.BasicBlocks:
         ins += bb.Instructions
      return ins
   Instructions = property(getInstructions)
   def getBBs(self):
      bbs = []
      for f in self.Functions:
         bbs += f.BasicBlocks
      return bbs
   BasicBlocks = property(getBBs)
   def addFunc(self, f):
      self.funcs.append(f)
   def getFuncs(self):
      return self.funcs
   Functions = property(getFuncs)
   def dump(self):
      print(self)
      for fn in self.Functions:
         print(fn)
         for bb in fn.BasicBlocks:
            print('   ', bb)
            for ins in bb.Instructions:
               print('      ', ins)
      print('END')
   def dumpgv(self, outf):
      outf.write('digraph G \n{\n')
      for f in self.Functions:
         outf.write('{0} [label="{1}" shape=box3d]\n'.format(id(f), f))
         for bb in f.BasicBlocks:
            contents = str(bb) + '\n'
            contents += '\n'.join([str(i) for i in bb.Instructions])
            outf.write('{0} [shape=note label="{1}"];\n'.format(id(bb), contents))
            for successor in bb.Successors:
               outf.write('"{0}" -> "{1}"\n'.format(id(bb), id(successor)))
         outf.write('"{0}" -> "{1}" [label="entry"]\n'.format(id(f), id(f.entry)))
      outf.write('}\n')

   # Analysis functions:
   def check(self):
      """ Perform sanity check on module """
      for i in self.Instructions:
         for t in i.defs:
            assert type(t) is Value, "def must be Value, not {0}".format(type(t))
         for t in i.uses:
            assert type(t) is Use, "use must be Value, not {0}".format(type(t))