Skip to content

Commit 5480e1d

Browse files
committedAug 5, 2021
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 returns 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 being supported, expressions are thought of as "not-yet- existing-symbols" during pass 1, instead of the integers they will be once evaluated, which makes some opcodes unhappy (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 d5c274f commit 5480e1d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed
 

‎esp32_ulp/assemble.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,10 @@ 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+
if self.a_pass == 1:
281+
instruction = 0 # dummy
282+
else:
283+
instruction = func(*args)
281284
self.append_section(instruction.to_bytes(4, 'little'), TEXT)
282285
continue
283286
raise Exception('Unknown opcode or directive: %s' % opcode)

‎tests/assemble.py

+14
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@ def test_assemble_evalulate_expressions():
157157
assert a.symbols.get_sym('entry') == (REL, TEXT, 0)
158158

159159

160+
def test_assemble_test_regressions_from_evaluation():
161+
line = " reg_wr (0x3ff48400 + 0x10), 1, 1, 1"
162+
163+
a = Assembler()
164+
raised = False
165+
try:
166+
a.assemble(line)
167+
except ValueError as e:
168+
if str(e) == 'invalid register base': # ensure we trapped the expected Exception
169+
raised = True
170+
assert not raised
171+
172+
160173
def test_symbols():
161174
st = SymbolTable({}, {}, {})
162175
for entry in [
@@ -218,4 +231,5 @@ def test_symbols():
218231
test_assemble_global()
219232
test_assemble_uppercase_opcode()
220233
test_assemble_evalulate_expressions()
234+
test_assemble_test_regressions_from_evaluation()
221235
test_symbols()

0 commit comments

Comments
 (0)