diff python/target/basetarget.py @ 341:4d204f6f7d4e devel

Rewrite of assembler parts
author Windel Bouwman
date Fri, 28 Feb 2014 18:07:14 +0100
parents 582a1aaa3983
children
line wrap: on
line diff
--- a/python/target/basetarget.py	Sun Feb 23 16:24:01 2014 +0100
+++ b/python/target/basetarget.py	Fri Feb 28 18:07:14 2014 +0100
@@ -140,52 +140,26 @@
         self.name = name
         self.desc = desc
         self.registers = []
-        self.instructions = []
-        self.byte_sizes = {'int':4}  # For front end!
+        self.byte_sizes = {'int' : 4}  # For front end!
+
+        # For assembler:
+        self.assembler_rules = []
+        self.asm_keywords = []
+
+    def add_keyword(self, kw):
+        self.asm_keywords.append(kw)
+
+    def add_instruction(self, rhs, f):
+        self.add_rule('instruction', rhs, f)
+
+    def add_rule(self, lhs, rhs, f):
+        self.assembler_rules.append((lhs, rhs, f))
 
     def instruction(self, cls):
         """ Decorator function that registers an instruction to this target """
         self.addInstruction(cls)
         return cls
 
-    def check(self):
-        """ Check target """
-        for i in self.instructions:
-            assert hasattr(i, 'mnemonic')
-            assert hasattr(i, 'operands'), str(i)
-            assert type(i.mnemonic) is str
-            assert type(i.operands) is tuple, str(i)
-
-    def addInstruction(self, ins_class):
-        self.instructions.append(ins_class)
+    def addInstruction(self, i):
+        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]
-        raise CompilerError('Cannot map {0}'.format(operand))
-
-    def mapInstruction(self, vi):
-        assert type(vi) is AInstruction
-        """ Map ast tree to real instruction for this target """
-
-        # map to real operands:
-        if vi.mnemonic.upper() == 'ALIGN' and len(vi.operands) == 1:
-            if type(vi.operands[0]) == ANumber:
-                return Alignment(vi.operands[0].number)
-
-        # look for a suitable instruction
-        for ic in self.instructions:
-            if ic.mnemonic.upper() == vi.mnemonic.upper() and len(ic.operands) == len(vi.operands):
-                # Try to map operands to the correct operand types:
-                rops = [roptype.Create(vop) for roptype, vop in zip(ic.operands, vi.operands)]
-
-                # Check if we succeeded:
-                if all(isinstance(rop, optype) for rop, optype in zip(rops, ic.operands)):
-                    return ic(*rops)
-        raise CompilerError('No suitable instruction found for "{0}"'.format(vi))