comparison 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
comparison
equal deleted inserted replaced
380:67a584582aee 381:6df89163e114
10 from .instructions import Push, Pop, Str, Ldr, Ldr3, Str1, Ldr1, Adr 10 from .instructions import Push, Pop, Str, Ldr, Ldr3, Str1, Ldr1, Adr
11 from .instructions import Mcr, Mrc 11 from .instructions import Mcr, Mrc
12 from .instructions import LdrPseudo 12 from .instructions import LdrPseudo
13 from .selector import ArmInstructionSelector 13 from .selector import ArmInstructionSelector
14 from .frame import ArmFrame 14 from .frame import ArmFrame
15 from ...assembler import BaseAssembler
16
17
18 class ArmAssembler(BaseAssembler):
19 def __init__(self, target):
20 super().__init__(target)
21 self.target.add_keyword('section')
22 self.target.add_instruction(['section', 'ID'],
23 lambda rhs: self.select_section(rhs[1].val))
24 self.make_parser()
25 self.lit_pool = []
26 self.lit_counter = 0
27
28 def select_section(self, name):
29 self.flush()
30 self.stream.select_section(name)
31
32 def flush(self):
33 while self.lit_pool:
34 i = self.lit_pool.pop(0)
35 self.emit(i)
36
37 def add_literal(self, v):
38 """ For use in the pseudo instruction LDR r0, =SOMESYM """
39 # Invent some label for the literal and store it.
40 self.lit_counter += 1
41 label_name = "_lit_{}".format(self.lit_counter)
42 self.lit_pool.append(Label(label_name))
43 self.lit_pool.append(Dcd(v))
44 return label_name
15 45
16 class ArmTarget(Target): 46 class ArmTarget(Target):
17 def __init__(self): 47 def __init__(self):
18 super().__init__('arm') 48 super().__init__('arm')
19 self.make_parser() 49 self.make_parser()
20 self.ins_sel = ArmInstructionSelector() 50 self.ins_sel = ArmInstructionSelector()
21 self.FrameClass = ArmFrame 51 self.FrameClass = ArmFrame
52 self.assembler = ArmAssembler(self)
22 53
23 self.add_lowering(Ldr3, lambda im: Ldr3(im.dst[0], im.others[0])) 54 self.add_lowering(Ldr3, lambda im: Ldr3(im.dst[0], im.others[0]))
24 self.add_lowering(Str1, lambda im: Str1(im.src[1], im.src[0], im.others[0])) 55 self.add_lowering(Str1, lambda im: Str1(im.src[1], im.src[0], im.others[0]))
25 self.add_lowering(Ldr1, lambda im: Ldr1(im.dst[0], im.src[0], im.others[0])) 56 self.add_lowering(Ldr1, lambda im: Ldr1(im.dst[0], im.src[0], im.others[0]))
26 self.add_lowering(Adr, lambda im: Adr(im.dst[0], im.others[0])) 57 self.add_lowering(Adr, lambda im: Adr(im.dst[0], im.others[0]))
35 self.add_lowering(Mov1, lambda im: Mov1(im.dst[0], im.others[0])) 66 self.add_lowering(Mov1, lambda im: Mov1(im.dst[0], im.others[0]))
36 67
37 def emit_global(self, outs, lname): 68 def emit_global(self, outs, lname):
38 outs.emit(Label(lname)) 69 outs.emit(Label(lname))
39 outs.emit(Dcd(0)) 70 outs.emit(Dcd(0))
71
40 72
41 def make_parser(self): 73 def make_parser(self):
42 # Assembly grammar: 74 # Assembly grammar:
43 self.add_keyword('r0') 75 self.add_keyword('r0')
44 self.add_keyword('r1') 76 self.add_keyword('r1')
155 self.add_instruction(['ldr', 'reg', ',', 'ID'], 187 self.add_instruction(['ldr', 'reg', ',', 'ID'],
156 lambda rhs: Ldr(rhs[1], rhs[3].val)) 188 lambda rhs: Ldr(rhs[1], rhs[3].val))
157 189
158 # This is a pseudo instruction: 190 # This is a pseudo instruction:
159 self.add_instruction(['ldr', 'reg', ',', '=', 'ID'], 191 self.add_instruction(['ldr', 'reg', ',', '=', 'ID'],
160 lambda rhs: LdrPseudo(rhs[1], rhs[4].val)) 192 lambda rhs: LdrPseudo(rhs[1], rhs[4].val, self.assembler.add_literal))
161 193
162 self.add_keyword('str') 194 self.add_keyword('str')
163 self.add_instruction(['str', 'reg', ',', '[', 'reg', ',', 'imm8', ']'], 195 self.add_instruction(['str', 'reg', ',', '[', 'reg', ',', 'imm8', ']'],
164 lambda rhs: Str(rhs[1], rhs[4], rhs[6])) 196 lambda rhs: Str(rhs[1], rhs[4], rhs[6]))
165 197