comparison python/ppci/assembler.py @ 381:6df89163e114

Fix section and ldr pseudo instruction
author Windel Bouwman
date Sat, 26 Apr 2014 17:41:56 +0200
parents 19eacf4f7270
children 0c44e494ef58
comparison
equal deleted inserted replaced
380:67a584582aee 381:6df89163e114
87 def add_rule(self, prod, rhs, f): 87 def add_rule(self, prod, rhs, f):
88 """ Helper function to add a rule, why this is required? """ 88 """ Helper function to add a rule, why this is required? """
89 if prod == 'instruction': 89 if prod == 'instruction':
90 def f_wrap(*args): 90 def f_wrap(*args):
91 i = f(args) 91 i = f(args)
92 self.emit(i) 92 if i:
93 self.emit(i)
93 else: 94 else:
94 def f_wrap(*rhs): 95 def f_wrap(*rhs):
95 return f(rhs) 96 return f(rhs)
96 self.g.add_production(prod, rhs, f_wrap) 97 self.g.add_production(prod, rhs, f_wrap)
97 98
110 g.add_production('asmline2', ['label', 'instruction']) 111 g.add_production('asmline2', ['label', 'instruction'])
111 g.add_production('asmline2', ['instruction']) 112 g.add_production('asmline2', ['instruction'])
112 g.add_production('asmline2', ['label']) 113 g.add_production('asmline2', ['label'])
113 g.add_production('asmline2', []) 114 g.add_production('asmline2', [])
114 g.add_production('label', ['ID', ':'], self.p_label) 115 g.add_production('label', ['ID', ':'], self.p_label)
115 #g.add_production('label', [])
116 116
117 # Add instruction rules for the target in question: 117 # Add instruction rules for the target in question:
118 for prod, rhs, f in instruction_rules: 118 for prod, rhs, f in instruction_rules:
119 self.add_rule(prod, rhs, f) 119 self.add_rule(prod, rhs, f)
120 120
132 132
133 def parse(self, lexer): 133 def parse(self, lexer):
134 self.p.parse(lexer) 134 self.p.parse(lexer)
135 135
136 136
137 class Assembler: 137 class BaseAssembler:
138 """ Assembler base class, inherited by assemblers specific for a target """
138 def __init__(self, target): 139 def __init__(self, target):
139 self.target = target 140 self.target = target
140 assert isinstance(target, Target) 141 assert isinstance(target, Target)
141 self.parser = Parser(target.asm_keywords, target.assembler_rules, self.emit) 142
143 def make_parser(self):
144 self.parser = Parser(self.target.asm_keywords, self.target.assembler_rules, self.emit)
142 145
143 def emit(self, *args): 146 def emit(self, *args):
144 self.stream.emit(*args) 147 self.stream.emit(*args)
145 148
146 # Top level interface: 149 # Top level interface:
149 tokens = Lexer(line, self.target.asm_keywords) 152 tokens = Lexer(line, self.target.asm_keywords)
150 self.parser.parse(tokens) 153 self.parser.parse(tokens)
151 154
152 def assemble(self, asmsrc, stream): 155 def assemble(self, asmsrc, stream):
153 """ Assemble this source snippet """ 156 """ Assemble this source snippet """
154 if hasattr(asmsrc, 'read'): 157 if type(asmsrc) is str:
158 pass
159 elif hasattr(asmsrc, 'read'):
155 asmsrc2 = asmsrc.read() 160 asmsrc2 = asmsrc.read()
156 asmsrc.close() 161 asmsrc.close()
157 asmsrc = asmsrc2 162 asmsrc = asmsrc2
158 # TODO: use generic newline?? 163 # TODO: use generic newline??
159 # TODO: the bothersome newline ... 164 # TODO: the bothersome newline ...
160 self.stream = stream 165 self.stream = stream
161 for line in asmsrc.split('\n'): 166 for line in asmsrc.split('\n'):
162 self.parse_line(line) 167 self.parse_line(line)
163 self.stream = None
164 168
169 def flush(self):
170 pass