Skip to content

Commit 5fd45a2

Browse files
committed
refactor: small improvements based on PR comments.
1 parent c3bd101 commit 5fd45a2

File tree

9 files changed

+44
-16
lines changed

9 files changed

+44
-16
lines changed

esp32_ulp/__main__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
def src_to_binary(src):
1212
assembler = Assembler()
13+
src = preprocess(src)
1314
assembler.assemble(src, remove_comments=False) # comments already removed by preprocessor
1415
garbage_collect('before symbols export')
1516
addrs_syms = assembler.symbols.export()
@@ -24,7 +25,6 @@ def main(fn):
2425
with open(fn) as f:
2526
src = f.read()
2627

27-
src = preprocess(src)
2828
binary = src_to_binary(src)
2929

3030
if fn.endswith('.s') or fn.endswith('.S'):

esp32_ulp/assemble.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ def set_global(self, symbol):
8787

8888
class Assembler:
8989

90-
def __init__(self, symbols=None, bases=None, globls=None):
91-
self.symbols = SymbolTable(symbols or {}, bases or {}, globls or {})
90+
def __init__(self, symbols=None, bases=None, globals=None):
91+
self.symbols = SymbolTable(symbols or {}, bases or {}, globals or {})
9292
opcodes.symbols = self.symbols # XXX dirty hack
9393

9494
def init(self, a_pass):
@@ -223,7 +223,7 @@ def d_align(self, align=4, fill=None):
223223
self.fill(self.section, amount, fill)
224224

225225
def d_set(self, symbol, expr):
226-
value = int(opcodes.eval_arg(expr)) # TODO: support more than just integers
226+
value = int(opcodes.eval_arg(expr))
227227
self.symbols.set_sym(symbol, ABS, None, value)
228228

229229
def d_global(self, symbol):
@@ -265,6 +265,12 @@ def assembler_pass(self, lines):
265265
# machine instruction
266266
func = getattr(opcodes, 'i_' + opcode.lower(), None)
267267
if func is not None:
268+
# during the first pass, symbols are not all known yet.
269+
# so some expressions may not evaluate to something (yet).
270+
# instruction building requires sane arguments however.
271+
# since all instructions are 4 bytes long, we simply skip
272+
# building instructions during pass 1, and append an "empty
273+
# instruction" to the section to get the right section size.
268274
instruction = 0 if self.a_pass == 1 else func(*args)
269275
self.append_section(instruction.to_bytes(4, 'little'), TEXT)
270276
continue

esp32_ulp/definesdb.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ def clear(self):
1919
except OSError:
2020
pass
2121

22+
def is_open(self):
23+
return self._db is not None
24+
2225
def open(self):
23-
if self._db:
26+
if self.is_open():
2427
return
2528
try:
2629
self._file = open(DBNAME, 'r+b')
@@ -30,7 +33,7 @@ def open(self):
3033
self._db_exists = True
3134

3235
def close(self):
33-
if not self._db:
36+
if not self.is_open():
3437
return
3538
self._db.close()
3639
self._db = None

esp32_ulp/opcodes.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ def arg_qualify(arg):
307307
pass
308308
try:
309309
entry = symbols.get_sym(arg)
310-
return ARG(SYM, entry, arg)
311310
except KeyError:
312-
pass
313-
return ARG(IMM, int(eval_arg(arg)), arg)
311+
return ARG(IMM, int(eval_arg(arg)), arg)
312+
else:
313+
return ARG(SYM, entry, arg)
314314

315315

316316
def get_reg(arg):

esp32_ulp/preprocess.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ def READ_RTC_REG(rtc_reg, low_bit, bit_width):
1414

1515
@staticmethod
1616
def WRITE_RTC_REG(rtc_reg, low_bit, bit_width, value):
17-
args = (
17+
return '\treg_wr ' + ', '.join((
1818
rtc_reg,
1919
'%s + %s - 1' % (low_bit, bit_width),
2020
low_bit,
2121
value
22-
)
23-
return '\treg_wr ' + ', '.join(args)
22+
))
2423

2524
@staticmethod
2625
def READ_RTC_FIELD(rtc_reg, low_bit):

esp32_ulp/util.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ def split_tokens(line):
1919
tokens = []
2020
state = NORMAL
2121
for c in line:
22-
if ('a' <= c <= 'z') or ('A' <= c <= 'Z') or ('0' <= c <= '9') or c == '_':
22+
if c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_":
2323
if state != NORMAL:
2424
if len(buf) > 0:
2525
tokens.append(buf)
2626
buf = ""
2727
state = NORMAL
2828
buf += c
29-
elif c == ' ' or c == '\t':
29+
elif c in " \t":
3030
if state != WHITESPACE:
3131
if len(buf) > 0:
3232
tokens.append(buf)

tests/fixtures/incl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
#define MACRO(x,y) x+y
33
#define MULTI_LINE abc \
44
xyz
5-
#define CONST2 99
5+
#define CONST2 99

tests/fixtures/incl2.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#define CONST2 123
2-
#define CONST3 777
2+
#define CONST3 777

tests/preprocess.py

+20
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,26 @@ def test_preprocess_should_ensure_no_definesdb_is_created_when_only_reading_from
312312
assert not file_exists(DBNAME)
313313

314314

315+
@test
316+
def test_preprocess_should_ensure_the_definesdb_is_properly_closed_after_use():
317+
content = """\
318+
#define CONST 42
319+
move r1, CONST"""
320+
321+
# remove any existing db
322+
db = DefinesDB()
323+
db.open()
324+
assert db.is_open()
325+
326+
# now preprocess using db
327+
p = Preprocessor()
328+
p.use_db(db)
329+
330+
p.preprocess(content)
331+
332+
assert not db.is_open()
333+
334+
315335
if __name__ == '__main__':
316336
# run all methods marked with @test
317337
for t in tests:

0 commit comments

Comments
 (0)