Skip to content

Commit

Permalink
SECTION(symbol) returns the name of a symbol's section (#1066)
Browse files Browse the repository at this point in the history
Fixes #963

Co-authored-by: Eldred Habert <[email protected]>
  • Loading branch information
Rangi42 and ISSOtm authored Sep 29, 2022
1 parent c35cb6a commit dec4133
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions man/rgbasm.5
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ is a label, it returns the bank number the label is in.
The result may be constant if
.Nm
is able to compute it.
.It Fn SECTION symbol Ta Returns the name of the section that
.Ar symbol
is in.
.Ar symbol
must have been defined already.
.It Fn SIZEOF arg Ta Returns the size of the section named
.Ar arg .
The result is not constant, since only RGBLINK can compute its value.
Expand Down
13 changes: 13 additions & 0 deletions src/asm/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,19 @@ string : T_STRING
strfmt($$, sizeof($$), $3.format, $3.nbArgs, $3.args);
freeStrFmtArgList(&$3);
}
| T_POP_SECTION T_LPAREN scoped_anon_id T_RPAREN {
struct Symbol *sym = sym_FindScopedSymbol($3);

if (!sym)
fatalerror("Unknown symbol \"%s\"\n", $3);
struct Section const *section = sym_GetSection(sym);

if (!section)
fatalerror("\"%s\" does not belong to any section\n", sym->name);
// Section names are capped by rgbasm's maximum string length,
// so this currently can't overflow.
strcpy($$, section->name);
}
;

strcat_args : string
Expand Down
2 changes: 2 additions & 0 deletions test/asm/long-section-name.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SECTION "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit", ROM0[0]
println "This is section ", SECTION(@)
2 changes: 2 additions & 0 deletions test/asm/long-section-name.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
warning: long-section-name.asm(1): [-Wlong-string]
String constant too long
1 change: 1 addition & 0 deletions test/asm/long-section-name.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is section Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor i
Empty file.
7 changes: 7 additions & 0 deletions test/asm/section-name-invalid.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SECTION "test", ROM0

Label:
println SECTION(Label) ; OK

DEF Value EQU 42
println SECTION(Value) ; not OK
2 changes: 2 additions & 0 deletions test/asm/section-name-invalid.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FATAL: section-name-invalid.asm(7):
"Value" does not belong to any section
1 change: 1 addition & 0 deletions test/asm/section-name-invalid.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
19 changes: 19 additions & 0 deletions test/asm/section-name.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SECTION "aaa", ROM0[5]
println SECTION(@)
Label1: println SECTION(Label1)
dw STARTOF(SECTION(@))

SECTION UNION "bbb", WRAM0
println SECTION(@)
Label2:
.local1: println SECTION(Label2.local1)
.local2: println SECTION(.local2)

SECTION FRAGMENT "ccc", HRAM
println SECTION(@)
: println SECTION(:-)

PUSHS
SECTION "ddd", ROMX
println SECTION(@)
POPS
Empty file added test/asm/section-name.err
Empty file.
8 changes: 8 additions & 0 deletions test/asm/section-name.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
aaa
aaa
bbb
bbb
bbb
ccc
ccc
ddd
Binary file added test/asm/section-name.out.bin
Binary file not shown.

0 comments on commit dec4133

Please sign in to comment.