From 2c30ab8731ab1b9f3880b1a8f55e49abe2b625e9 Mon Sep 17 00:00:00 2001 From: Rangi Date: Mon, 15 Mar 2021 09:44:38 -0400 Subject: [PATCH] Allow OPT to modify -L -L is a Boolean flag option, so you specify 'OPT L' or 'OPT !L'. --- include/asm/main.h | 2 +- include/asm/opt.h | 3 ++- src/asm/main.c | 6 +++--- src/asm/opt.c | 33 ++++++++++++++++++++++++++++++++- src/asm/parser.y | 4 ++-- src/asm/rgbasm.5 | 18 +++++++++++++----- test/asm/opt.asm | 10 ++++++++++ test/asm/opt.err | 0 test/asm/opt.out | 0 test/asm/opt.out.bin | Bin 0 -> 7 bytes 10 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 test/asm/opt.asm create mode 100644 test/asm/opt.err create mode 100644 test/asm/opt.out create mode 100644 test/asm/opt.out.bin diff --git a/include/asm/main.h b/include/asm/main.h index 3b79b043..99c5fb3f 100644 --- a/include/asm/main.h +++ b/include/asm/main.h @@ -16,7 +16,7 @@ #include "helpers.h" extern bool haltnop; -extern bool optimizeloads; +extern bool optimizeLoads; extern bool verbose; extern bool warnings; /* True to enable warnings, false to disable them. */ diff --git a/include/asm/opt.h b/include/asm/opt.h index 769c491b..af25c2a6 100644 --- a/include/asm/opt.h +++ b/include/asm/opt.h @@ -9,14 +9,15 @@ #ifndef RGBDS_OPT_H #define RGBDS_OPT_H +#include void opt_B(char chars[2]); void opt_G(char chars[4]); void opt_P(uint8_t fill); +void opt_L(bool optimize); void opt_Parse(char const *option); void opt_Push(void); void opt_Pop(void); - #endif diff --git a/src/asm/main.c b/src/asm/main.c index ea25224a..a0c4542a 100644 --- a/src/asm/main.c +++ b/src/asm/main.c @@ -51,7 +51,7 @@ bool oGeneratePhonyDeps; char *tzTargetFileName; bool haltnop; -bool optimizeloads; +bool optimizeLoads; bool verbose; bool warnings; /* True to enable warnings, false to disable them. */ @@ -166,7 +166,7 @@ int main(int argc, char *argv[]) opt_B("01"); opt_G("0123"); opt_P(0); - optimizeloads = true; + optimizeLoads = true; haltnop = true; verbose = false; warnings = true; @@ -214,7 +214,7 @@ int main(int argc, char *argv[]) break; case 'L': - optimizeloads = false; + optimizeLoads = false; break; case 'M': diff --git a/src/asm/opt.c b/src/asm/opt.c index 9a10f22e..c8563de5 100644 --- a/src/asm/opt.c +++ b/src/asm/opt.c @@ -1,4 +1,3 @@ - #include #include #include @@ -7,6 +6,7 @@ #include #include "asm/lexer.h" +#include "asm/main.h" #include "asm/section.h" #include "asm/warning.h" @@ -14,6 +14,7 @@ struct OptStackEntry { char binary[2]; char gbgfx[4]; int32_t fillByte; + bool optimizeLoads; struct OptStackEntry *next; }; @@ -34,6 +35,11 @@ void opt_P(uint8_t fill) fillByte = fill; } +void opt_L(bool optimize) +{ + optimizeLoads = optimize; +} + void opt_Parse(char *s) { switch (s[0]) { @@ -66,6 +72,28 @@ void opt_Parse(char *s) } break; + case 'L': + if (s[1] == '\0') + opt_L(true); + else + error("Option 'L' does not take an argument\n"); + break; + + case '!': // negates flag options that do not take an argument + switch (s[1]) { + case 'L': + if (s[2] == '\0') + opt_L(false); + else + error("Option '!L' does not take an argument\n"); + break; + + default: + error("Unknown option '!%c'\n", s[1]); + break; + } + break; + default: error("Unknown option '%c'\n", s[0]); break; @@ -90,6 +118,8 @@ void opt_Push(void) entry->fillByte = fillByte; // Pulled from section.h + entry->optimizeLoads = optimizeLoads; // Pulled from main.h + entry->next = stack; stack = entry; } @@ -106,6 +136,7 @@ void opt_Pop(void) opt_B(entry->binary); opt_G(entry->gbgfx); opt_P(entry->fillByte); + opt_L(entry->optimizeLoads); stack = entry->next; free(entry); } diff --git a/src/asm/parser.y b/src/asm/parser.y index 9f476d15..2198d7e9 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -1831,7 +1831,7 @@ z80_ld_mem : T_Z80_LD op_mem_ind T_COMMA T_MODE_SP { out_RelWord(&$2, 1); } | T_Z80_LD op_mem_ind T_COMMA T_MODE_A { - if (optimizeloads && rpn_isKnown(&$2) + if (optimizeLoads && rpn_isKnown(&$2) && $2.nVal >= 0xFF00) { out_AbsByte(0xE0); out_AbsByte($2.nVal & 0xFF); @@ -1879,7 +1879,7 @@ z80_ld_a : T_Z80_LD reg_r T_COMMA c_ind { } | T_Z80_LD reg_r T_COMMA op_mem_ind { if ($2 == REG_A) { - if (optimizeloads && rpn_isKnown(&$4) + if (optimizeLoads && rpn_isKnown(&$4) && $4.nVal >= 0xFF00) { out_AbsByte(0xF0); out_AbsByte($4.nVal & 0xFF); diff --git a/src/asm/rgbasm.5 b/src/asm/rgbasm.5 index 2371e62f..9378873f 100644 --- a/src/asm/rgbasm.5 +++ b/src/asm/rgbasm.5 @@ -1883,16 +1883,24 @@ can be used to change some of the options during assembling from within the sour takes a comma-separated list of options as its argument: .Bd -literal -offset indent PUSHO -OPT g.oOX ;Set the GB graphics constants to use these characters -DW `..ooOOXX + OPT g.oOX, !L ; acts like command-line -g.oOX and omitting -L + DW `..ooOOXX ; uses the graphics constant characters from OPT g + LD [$FF88], A ; encoded as LD, not LDH POPO -DW `00112233 + DW `00112233 ; uses the default graphics constant characters + LD [$FF88], A ; optimized to use LDH if -L was passed .Ed .Pp The options that OPT can modify are currently: -.Cm b , g +.Cm b , g , p , and -.Cm p . +.Cm L . +The Boolean flag option +.Cm L +can be specified as +.Ql OPT L +or +.Ql OPT !L . .Pp .Ic POPO and diff --git a/test/asm/opt.asm b/test/asm/opt.asm new file mode 100644 index 00000000..9191288d --- /dev/null +++ b/test/asm/opt.asm @@ -0,0 +1,10 @@ +SECTION "test", ROM0 + +pusho + opt p42, !L + ds 1 + ld [$ff88], a +popo + + ds 1 + ld [$ff88], a diff --git a/test/asm/opt.err b/test/asm/opt.err new file mode 100644 index 00000000..e69de29b diff --git a/test/asm/opt.out b/test/asm/opt.out new file mode 100644 index 00000000..e69de29b diff --git a/test/asm/opt.out.bin b/test/asm/opt.out.bin new file mode 100644 index 0000000000000000000000000000000000000000..f659eb00864bea9a6d3ccd6ead722d189fcfcbec GIT binary patch literal 7 OcmZ>I)$yO#8 literal 0 HcmV?d00001