From dec4133e84e61fedc29c95cc3984f734422dff16 Mon Sep 17 00:00:00 2001 From: Rangi <35663410+Rangi42@users.noreply.github.com> Date: Thu, 29 Sep 2022 19:59:33 -0400 Subject: [PATCH] `SECTION(symbol)` returns the name of a symbol's section (#1066) Fixes #963 Co-authored-by: Eldred Habert --- man/rgbasm.5 | 5 +++++ src/asm/parser.y | 13 +++++++++++++ test/asm/long-section-name.asm | 2 ++ test/asm/long-section-name.err | 2 ++ test/asm/long-section-name.out | 1 + test/asm/long-section-name.out.bin | 0 test/asm/section-name-invalid.asm | 7 +++++++ test/asm/section-name-invalid.err | 2 ++ test/asm/section-name-invalid.out | 1 + test/asm/section-name.asm | 19 +++++++++++++++++++ test/asm/section-name.err | 0 test/asm/section-name.out | 8 ++++++++ test/asm/section-name.out.bin | Bin 0 -> 7 bytes 13 files changed, 60 insertions(+) create mode 100644 test/asm/long-section-name.asm create mode 100644 test/asm/long-section-name.err create mode 100644 test/asm/long-section-name.out create mode 100644 test/asm/long-section-name.out.bin create mode 100644 test/asm/section-name-invalid.asm create mode 100644 test/asm/section-name-invalid.err create mode 100644 test/asm/section-name-invalid.out create mode 100644 test/asm/section-name.asm create mode 100644 test/asm/section-name.err create mode 100644 test/asm/section-name.out create mode 100644 test/asm/section-name.out.bin diff --git a/man/rgbasm.5 b/man/rgbasm.5 index e75e36f6..b11293f8 100644 --- a/man/rgbasm.5 +++ b/man/rgbasm.5 @@ -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. diff --git a/src/asm/parser.y b/src/asm/parser.y index 3836bb15..2f5d8ce1 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -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 diff --git a/test/asm/long-section-name.asm b/test/asm/long-section-name.asm new file mode 100644 index 00000000..7c4b1f8f --- /dev/null +++ b/test/asm/long-section-name.asm @@ -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(@) diff --git a/test/asm/long-section-name.err b/test/asm/long-section-name.err new file mode 100644 index 00000000..412aa00f --- /dev/null +++ b/test/asm/long-section-name.err @@ -0,0 +1,2 @@ +warning: long-section-name.asm(1): [-Wlong-string] + String constant too long diff --git a/test/asm/long-section-name.out b/test/asm/long-section-name.out new file mode 100644 index 00000000..cfcbfe54 --- /dev/null +++ b/test/asm/long-section-name.out @@ -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 diff --git a/test/asm/long-section-name.out.bin b/test/asm/long-section-name.out.bin new file mode 100644 index 00000000..e69de29b diff --git a/test/asm/section-name-invalid.asm b/test/asm/section-name-invalid.asm new file mode 100644 index 00000000..57e85953 --- /dev/null +++ b/test/asm/section-name-invalid.asm @@ -0,0 +1,7 @@ +SECTION "test", ROM0 + +Label: +println SECTION(Label) ; OK + +DEF Value EQU 42 +println SECTION(Value) ; not OK diff --git a/test/asm/section-name-invalid.err b/test/asm/section-name-invalid.err new file mode 100644 index 00000000..7ae5c505 --- /dev/null +++ b/test/asm/section-name-invalid.err @@ -0,0 +1,2 @@ +FATAL: section-name-invalid.asm(7): + "Value" does not belong to any section diff --git a/test/asm/section-name-invalid.out b/test/asm/section-name-invalid.out new file mode 100644 index 00000000..9daeafb9 --- /dev/null +++ b/test/asm/section-name-invalid.out @@ -0,0 +1 @@ +test diff --git a/test/asm/section-name.asm b/test/asm/section-name.asm new file mode 100644 index 00000000..bccbf064 --- /dev/null +++ b/test/asm/section-name.asm @@ -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 diff --git a/test/asm/section-name.err b/test/asm/section-name.err new file mode 100644 index 00000000..e69de29b diff --git a/test/asm/section-name.out b/test/asm/section-name.out new file mode 100644 index 00000000..b59732eb --- /dev/null +++ b/test/asm/section-name.out @@ -0,0 +1,8 @@ +aaa +aaa +bbb +bbb +bbb +ccc +ccc +ddd diff --git a/test/asm/section-name.out.bin b/test/asm/section-name.out.bin new file mode 100644 index 0000000000000000000000000000000000000000..72ddfcf721e5b405b97a580172993ada70128f4d GIT binary patch literal 7 McmZQz00LG9000pH1^@s6 literal 0 HcmV?d00001