Skip to content

Commit 36e2632

Browse files
implement .align, refactor .skip
1 parent ab65e38 commit 36e2632

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

esp32_ulp/assemble.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,30 @@ def d_data(self):
202202
def d_bss(self):
203203
self.section = BSS
204204

205+
def fill(self, section, amount, fill_byte):
206+
if fill_byte is not None and section is BSS:
207+
raise ValueError('fill in bss section not allowed')
208+
if section is TEXT: # TODO: text section should be filled with NOPs
209+
raise ValueError('fill/skip/align in text section not supported')
210+
fill = int(fill_byte or 0).to_bytes(1, 'little') * amount
211+
self.offsets[section] += len(fill)
212+
if section is not BSS:
213+
self.sections[section].append(fill)
214+
205215
def d_skip(self, amount, fill=None):
206-
s = self.section
207216
amount = int(amount)
208-
if fill is not None and s is BSS:
209-
raise ValueError('fill not allowed in section %s' % s)
210-
if s is BSS:
211-
self.append_section(amount)
212-
else:
213-
fill = int(fill or 0).to_bytes(1, 'little') * amount
214-
self.append_section(fill)
217+
self.fill(self.section, amount, fill)
215218

216219
d_space = d_skip
217220

221+
def d_align(self, align=4, fill=None):
222+
align = int(align)
223+
offs = self.offsets[self.section]
224+
mod = offs % align
225+
if mod:
226+
amount = align - mod
227+
self.fill(self.section, amount, fill)
228+
218229
def d_set(self, symbol, expr):
219230
value = int(expr) # TODO: support more than just integers
220231
self.symbols.set_sym(symbol, ABS, None, value)

tests/compat/sections.S

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88

99
.space 4
1010
.space 8, 0xFF
11+
.space 1
12+
.align 4
13+
.space 3
1114

15+
# a section start will be automatically 32bit-aligned:
1216
.bss
1317

1418
.space 10
15-
19+
# a section end will be automatically 32bit-aligned

0 commit comments

Comments
 (0)