diff python/ppci/target/arm/__init__.py @ 381:6df89163e114

Fix section and ldr pseudo instruction
author Windel Bouwman
date Sat, 26 Apr 2014 17:41:56 +0200
parents 19eacf4f7270
children 2a970e7270e2
line wrap: on
line diff
--- a/python/ppci/target/arm/__init__.py	Fri Apr 18 13:08:45 2014 +0200
+++ b/python/ppci/target/arm/__init__.py	Sat Apr 26 17:41:56 2014 +0200
@@ -12,6 +12,36 @@
 from .instructions import LdrPseudo
 from .selector import ArmInstructionSelector
 from .frame import ArmFrame
+from ...assembler import BaseAssembler
+
+
+class ArmAssembler(BaseAssembler):
+    def __init__(self, target):
+        super().__init__(target)
+        self.target.add_keyword('section')
+        self.target.add_instruction(['section', 'ID'],
+                lambda rhs: self.select_section(rhs[1].val))
+        self.make_parser()
+        self.lit_pool = []
+        self.lit_counter = 0
+
+    def select_section(self, name):
+        self.flush()
+        self.stream.select_section(name)
+
+    def flush(self):
+        while self.lit_pool:
+            i = self.lit_pool.pop(0)
+            self.emit(i)
+
+    def add_literal(self, v):
+        """ For use in the pseudo instruction LDR r0, =SOMESYM """
+        # Invent some label for the literal and store it.
+        self.lit_counter += 1
+        label_name = "_lit_{}".format(self.lit_counter)
+        self.lit_pool.append(Label(label_name))
+        self.lit_pool.append(Dcd(v))
+        return label_name
 
 class ArmTarget(Target):
     def __init__(self):
@@ -19,6 +49,7 @@
         self.make_parser()
         self.ins_sel = ArmInstructionSelector()
         self.FrameClass = ArmFrame
+        self.assembler = ArmAssembler(self)
 
         self.add_lowering(Ldr3, lambda im: Ldr3(im.dst[0], im.others[0]))
         self.add_lowering(Str1, lambda im: Str1(im.src[1], im.src[0], im.others[0]))
@@ -38,6 +69,7 @@
         outs.emit(Label(lname))
         outs.emit(Dcd(0))
 
+
     def make_parser(self):
         # Assembly grammar:
         self.add_keyword('r0')
@@ -157,7 +189,7 @@
 
         # This is a pseudo instruction:
         self.add_instruction(['ldr', 'reg', ',', '=', 'ID'],
-            lambda rhs: LdrPseudo(rhs[1], rhs[4].val))
+            lambda rhs: LdrPseudo(rhs[1], rhs[4].val, self.assembler.add_literal))
 
         self.add_keyword('str')
         self.add_instruction(['str', 'reg', ',', '[', 'reg', ',', 'imm8', ']'],