Skip to content

Commit 219f939

Browse files
committed
fix evaluation of expressions during first assembler pass
During the first assembler pass, the SymbolTable does not yet have all symbols. During a symbol lookup the SymbolTable has so far returned a fake symbol for non-existing symbols, to make the assembler happy (values are not really being used during the first pass, so it's ok). However now that expressions are supported, when the symbol lookup encountered expressions during pass 1, it assumed those expressions were "not-yet-existing-symbols", which is of course incorrect as they will eventually be evaluated to integer values. Some opcodes were unhappy with receiving an expression during pass 1 (e.g. the req_wr opcode, which expects a sane address as a first argument). This commit simply skips creating instructions during the first pass, because all instructions are 32-bit (4 bytes) long anyway, so the content doesn't matter during that first assembler pass, which only measures section sizes.
1 parent 4dded94 commit 219f939

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

esp32_ulp/assemble.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def assembler_pass(self, lines):
277277
# machine instruction
278278
func = getattr(opcodes, 'i_' + opcode.lower(), None)
279279
if func is not None:
280-
instruction = func(*args)
280+
instruction = 0 if self.a_pass == 1 else func(*args)
281281
self.append_section(instruction.to_bytes(4, 'little'), TEXT)
282282
continue
283283
raise ValueError('Unknown opcode or directive: %s' % opcode)

tests/assemble.py

+14
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ def test_assemble_optional_comment_removal():
175175
assert raised
176176

177177

178+
def test_assemble_test_regressions_from_evaluation():
179+
line = " reg_wr (0x3ff48400 + 0x10), 1, 1, 1"
180+
181+
a = Assembler()
182+
raised = False
183+
try:
184+
a.assemble(line)
185+
except ValueError as e:
186+
if str(e) == 'invalid register base': # ensure we trapped the expected Exception
187+
raised = True
188+
assert not raised
189+
190+
178191
def test_symbols():
179192
st = SymbolTable({}, {}, {})
180193
for entry in [
@@ -237,4 +250,5 @@ def test_symbols():
237250
test_assemble_uppercase_opcode()
238251
test_assemble_evalulate_expressions()
239252
test_assemble_optional_comment_removal()
253+
test_assemble_test_regressions_from_evaluation()
240254
test_symbols()

0 commit comments

Comments
 (0)