Skip to content

Commit 3a2d312

Browse files
committedAug 2, 2021
add support for the .global directive. only symbols flagged as global will be exported
This change is mostly to support code that uses the .global directive without having to modify it first (such as commenting out those lines).
1 parent 15633eb commit 3a2d312

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed
 

‎esp32_ulp/assemble.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212

1313

1414
class SymbolTable:
15-
def __init__(self, symbols, bases):
15+
def __init__(self, symbols, bases, globls):
1616
self._symbols = symbols
1717
self._bases = bases
18+
self._globls = globls
1819
self._pass = None
1920

2021
def set_pass(self, _pass):
@@ -53,7 +54,9 @@ def dump(self):
5354
print(symbol, entry)
5455

5556
def export(self):
56-
addrs_syms = [(self.resolve_absolute(entry), symbol) for symbol, entry in self._symbols.items()]
57+
addrs_syms = [(self.resolve_absolute(entry), symbol)
58+
for symbol, entry in self._symbols.items()
59+
if symbol in self._globls]
5760
return sorted(addrs_syms)
5861

5962
def to_abs_addr(self, section, offset):
@@ -93,11 +96,15 @@ def resolve_relative(self, symbol):
9396
from_addr = self.to_abs_addr(self._from_section, self._from_offset)
9497
return sym_addr - from_addr
9598

99+
def set_global(self, symbol):
100+
self._globls[symbol] = True
101+
pass
102+
96103

97104
class Assembler:
98105

99-
def __init__(self, symbols=None, bases=None):
100-
self.symbols = SymbolTable(symbols or {}, bases or {})
106+
def __init__(self, symbols=None, bases=None, globls=None):
107+
self.symbols = SymbolTable(symbols or {}, bases or {}, globls or {})
101108
opcodes.symbols = self.symbols # XXX dirty hack
102109

103110
def init(self, a_pass):
@@ -236,6 +243,9 @@ def d_set(self, symbol, expr):
236243
value = int(expr) # TODO: support more than just integers
237244
self.symbols.set_sym(symbol, ABS, None, value)
238245

246+
def d_global(self, symbol):
247+
self.symbols.set_global(symbol)
248+
239249
def append_data(self, wordlen, args):
240250
data = [int(arg).to_bytes(wordlen, 'little') for arg in args]
241251
self.append_section(b''.join(data))

‎tests/assemble.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@
2222
"""
2323

2424

25+
src_global = """\
26+
27+
.global counter
28+
counter:
29+
.long 0
30+
31+
internal:
32+
.long 0
33+
34+
.text
35+
.global entry
36+
entry:
37+
wait 42
38+
halt
39+
"""
40+
41+
2542
def test_parse_line():
2643
a = Assembler()
2744
lines = src.splitlines()
@@ -90,8 +107,20 @@ def test_assemble_bss_with_value():
90107
assert raised
91108

92109

110+
def test_assemble_global():
111+
a = Assembler()
112+
a.assemble(src_global)
113+
assert a.symbols.has_sym('counter')
114+
assert a.symbols.has_sym('internal')
115+
assert a.symbols.has_sym('entry')
116+
117+
exported_symbols = a.symbols.export()
118+
assert 'internal' not in [x[1] for x in exported_symbols]
119+
assert exported_symbols == [(0, 'counter'), (2, 'entry')]
120+
121+
93122
def test_symbols():
94-
st = SymbolTable({}, {})
123+
st = SymbolTable({}, {}, {})
95124
for entry in [
96125
('rel_t4', REL, TEXT, 4),
97126
('abs_t4', ABS, TEXT, 4),
@@ -148,4 +177,5 @@ def test_symbols():
148177
test_assemble()
149178
test_assemble_bss()
150179
test_assemble_bss_with_value()
180+
test_assemble_global()
151181
test_symbols()

0 commit comments

Comments
 (0)