diff python/instructionselector.py @ 275:6f2423df0675

Fixed serve arm-as
author Windel Bouwman
date Sat, 14 Sep 2013 17:29:10 +0200
parents ea93e0a7a31e
children 2ccd57b1d78c
line wrap: on
line diff
--- a/python/instructionselector.py	Wed Sep 04 17:35:06 2013 +0200
+++ b/python/instructionselector.py	Sat Sep 14 17:29:10 2013 +0200
@@ -1,11 +1,12 @@
 
 import ir
 import irmach
+from irmach import makeIns
 
 def genTemps():
     n = 900
     while True:
-        yield 't{}'.format(n)
+        yield ir.Temp('t{}'.format(n))
         n = n + 1
 
 class InstructionSelector:
@@ -13,51 +14,42 @@
         Base instruction selector. This class must be overridden by
         backends.
     """
+    def __init__(self):
+        self.temps = genTemps()
+
     def newTmp(self):
         return self.temps.__next__()
 
-    def getTempReg(self, tmp):
-        if tmp not in self.tempMap:
-            self.tempMap[tmp] = self.newTmp()
-        return self.tempMap[tmp]
-
-    def munchProgram(self, p):
+    def munchFunction(self, f, frame):
         # Entry point for instruction selection
-        self.temps = genTemps()
-        assert isinstance(p, ir.Module)
-        self.frames = []
+        assert isinstance(f, ir.Function)
         self.targets = {}
-        self.tempMap = {} # Mapping from temporaries to infinite register
-        for f in p.Functions:
-            # Enter a frame per function:
-            self.frame = self.newFrame(f.name)
-            self.frames.append(self.frame)
-            # First define labels:
-            for bb in f.Blocks:
-                itgt = self.makeIns('{}:'.format(bb.name))
-                self.targets[bb] = itgt
-            for bb in f.Blocks:
-                self.emit2(self.targets[bb])
-                for i in bb.Instructions:
-                    self.munchStm(i)
-                #bb.machIns = self.result
-        return self.frames
+        # Enter a frame per function:
+        self.frame = frame
+        # First define labels:
+        for bb in f.Blocks:
+            itgt = makeIns('{}:'.format(bb.name))
+            self.targets[bb] = itgt
+        # Generate code for all blocks:
+        for bb in f.Blocks:
+            self.emit2(self.targets[bb])
+            for i in bb.Instructions:
+                self.munchStm(i)
+        self.munchStm(ir.Move(self.frame.rv, f.return_value))
+        self.emit('mov %s0, %s0', src=[self.frame.rv])
 
-    def makeIns(self, *args, **kwargs):
-        return irmach.AbstractInstruction(*args, **kwargs)
+    def move(self, dst, src):
+        self.emit('mov %d0, %s0', src=[src], dst=[dst])
 
     def emit(self, *args, **kwargs):
         """ Abstract instruction emitter """
-        i = self.makeIns(*args, **kwargs)
+        i = makeIns(*args, **kwargs)
         return self.emit2(i)
 
     def emit2(self, i):
         self.frame.instructions.append(i)
         return i
 
-    def newFrame(self, name):
-        raise NotImplementedError()
-
     def munchStm(self, s):
         """ Implement this in the target specific back-end """
         raise NotImplementedError()