Skip to content

Commit 0b46979

Browse files
committed
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 89111ba commit 0b46979

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

esp32_ulp/assemble.py

Lines changed: 14 additions & 4 deletions
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, globals):
1616
self._symbols = symbols
1717
self._bases = bases
18+
self._globals = globals
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._globals]
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._globals[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

Lines changed: 30 additions & 1 deletion
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 = iter(src.splitlines())
@@ -90,8 +107,19 @@ 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 exported_symbols == [(0, 'counter'), (2, 'entry')] # internal not exported
119+
120+
93121
def test_symbols():
94-
st = SymbolTable({}, {})
122+
st = SymbolTable({}, {}, {})
95123
for entry in [
96124
('rel_t4', REL, TEXT, 4),
97125
('abs_t4', ABS, TEXT, 4),
@@ -148,4 +176,5 @@ def test_symbols():
148176
test_assemble()
149177
test_assemble_bss()
150178
test_assemble_bss_with_value()
179+
test_assemble_global()
151180
test_symbols()

0 commit comments

Comments
 (0)