diff python/ir/builder.py @ 171:3eb9b9e2958d

Improved IR code
author Windel Bouwman
date Wed, 03 Apr 2013 22:20:20 +0200
parents
children 5a7d37d615ee
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/ir/builder.py	Wed Apr 03 22:20:20 2013 +0200
@@ -0,0 +1,47 @@
+from . import Value, BasicBlock
+
+class NameGenerator:
+   def __init__(self, prefix):
+      self.prefix = prefix
+      def NumGen():
+         a = 0
+         while True:
+            yield a
+            a = a + 1
+      self.nums = NumGen()
+   def gen(self):
+      return '{0}{1}'.format(self.prefix, self.nums.__next__())
+
+class ValueGenerator(NameGenerator):
+   def __init__(self):
+      super().__init__('t')
+   def gen(self):
+      v = Value(super().gen())
+      return v
+
+class BBGenerator(NameGenerator):
+   def __init__(self):
+      super().__init__('lab')
+   def gen(self):
+      v = BasicBlock(super().gen())
+      return v
+
+class Builder:
+   def __init__(self):
+      self.newTmp = ValueGenerator().gen
+      self.newBBint = BBGenerator().gen
+      self.bb = None
+      self.m = None
+
+   # Helpers:
+   def newBB(self):
+      bb = self.newBBint()
+      self.m.addBB(bb)
+      return bb
+   def setModule(self, m):
+      self.m = m
+   def setBB(self, bb):
+      self.bb = bb
+   def addIns(self, i):
+      self.bb.addIns(i)
+