diff python/target.py @ 200:5e391d9a3381

Split off asm nodes
author Windel Bouwman
date Sun, 09 Jun 2013 16:06:49 +0200
parents a690473b79e2
children d5debbfc0200
line wrap: on
line diff
--- a/python/target.py	Fri Jun 07 18:59:57 2013 +0200
+++ b/python/target.py	Sun Jun 09 16:06:49 2013 +0200
@@ -1,3 +1,5 @@
+from asmnodes import ASymbol, AInstruction
+from ppci import CompilerError
 
 """
   Base classes for defining a target
@@ -18,9 +20,32 @@
 
 class Target:
     def __init__(self):
+        self.registers = []
         self.instructions = []
-    def createInstruction(self, vi):
-        pass
-    pass
+
+    def mapOperand(self, operand):
+        """ Try to map an operand to a target type """
+        if type(operand) is ASymbol:
+            # Try to map to register:
+            regs = {}
+            for r in self.registers:
+                regs[r.name] = r
+            if operand.name in regs:
+                return regs[operand.name]
+        else:
+            return
 
+    def mapInstruction(self, vi):
+        """ Map ast tree to real instruction for this target """
 
+        # map to real operands:
+        rops = tuple(map(self.mapOperand, vi.operands))
+        optypes = tuple(map(type, rops))
+
+        # look for a suitable instruction
+        for ic in self.instructions:
+            if ic.mnemonic == vi.opcode and ic.operands == optypes:
+                ri = ic(*rops)
+                return ri
+        raise CompilerError('No suitable instruction found')
+