From 6ad5bd232504801b1b6b911c918677a7aff2c698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Ni=C3=B1o=20D=C3=ADaz?= Date: Sat, 24 Feb 2018 15:43:55 +0000 Subject: [PATCH] Add flag to rgbasm to disable LD->LDH optimization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rgbasm tries to optimize any loads from/to $FF00-$FFFF and generate LDH 2-byte opcodes instead of regular LD 3-byte opcodes. This is a bit inconsistent as it only works for constant values. If a load is trying to access a label in a HRAM floating section, or a section found in a different object file, this optimization doesn't work. This means that a simple refactor or code could allow rgbasm to perform the optimzation or prevent it from doing so. For certain projects, like disassemblies, this is a problem. This patch adds flag -L to rgbasm to disable the optimization, and doesn't change the behaviour of any other existing code. Signed-off-by: Antonio Niño Díaz --- docs/rgbasm.1.html | 17 ++++++++++++++--- docs/rgbasm.5.html | 7 +++++-- docs/rgbds.7.html | 12 ++++++++---- include/asm/main.h | 9 +++++---- src/asm/asmy.y | 6 ++++-- src/asm/main.c | 14 +++++++++----- src/asm/rgbasm.1 | 14 +++++++++++--- src/asm/rgbasm.5 | 10 ++++++++-- src/rgbds.7 | 10 ++++++---- 9 files changed, 70 insertions(+), 29 deletions(-) diff --git a/docs/rgbasm.1.html b/docs/rgbasm.1.html index b39d258b..681567a7 100644 --- a/docs/rgbasm.1.html +++ b/docs/rgbasm.1.html @@ -27,7 +27,7 @@ -
rgbasm[-EhVvw] + [-EhLVvw] [-b chars] [-D @@ -89,6 +89,13 @@ The rgbasm program creates an object file from an
Add an include path.
 
 
+
-L
+
Disable the optimization that turns loads of the form + LD [$FF00+n8],A into the opcode + LDH [$FF00+n8],A in order to have full + control of the result in the final ROM.
+
 
+
 
-M dependfile
Print make(1) dependencies to @@ -120,7 +127,11 @@ The rgbasm program creates an object file from an

EXAMPLES

Assembling a basic source file is simple:
-
$ rgbasm -o bar.o foo.asm
+
+
+$ rgbasm -o bar.o foo.asm
+
+
The resulting object file is not yet a usable ROM image — it must first be run through rgblink(1) and @@ -138,7 +149,7 @@ The resulting object file is not yet a usable ROM image — it must first https://github.com/rednex/rgbds. - +
January 26, 2018February 24, 2018 RGBDS Manual
diff --git a/docs/rgbasm.5.html b/docs/rgbasm.5.html index d754cc73..f805d97b 100644 --- a/docs/rgbasm.5.html +++ b/docs/rgbasm.5.html @@ -125,7 +125,10 @@ Possible section types are as follows: LDH [$FF00+n8],A and LDH A,[$FF00+n8] syntax instead. This forces the assembler to emit the correct instruction and the linker to check if - the value is in the correct range.
+ the value is in the correct range. This optimization can be disabled by + passing the -L flag to + rgbasm as explained in + rgbasm(1).
A section is usually defined as a floating one, but the code can restrict where @@ -1462,7 +1465,7 @@ The options that OPT can modify are currently: b, https://github.com/rednex/rgbds. - +
January 27, 2018February 24, 2018 RGBDS Manual
diff --git a/docs/rgbds.7.html b/docs/rgbds.7.html index 1a21576d..7a86692c 100644 --- a/docs/rgbds.7.html +++ b/docs/rgbds.7.html @@ -26,9 +26,13 @@

EXAMPLES

To get a working ROM image from a single assembly source file:
-
$ rgbasm -o bar.o foo.asm
-
$ rgblink -o baz.gb bar.o
-
$ rgbfix -v -p 0 baz.gb
+
+
+$ rgbasm -o bar.o foo.asm 
+$ rgblink -o baz.gb bar.o 
+$ rgbfix -v -p 0 baz.gb
+
+

SEE ALSO

rgbasm(1), rgbfix(1), @@ -58,7 +62,7 @@ To get a working ROM image from a single assembly source file: - +
January 26, 2018February 24, 2018 RGBDS Manual
diff --git a/include/asm/main.h b/include/asm/main.h index 5a9baf0c..7eed1df4 100644 --- a/include/asm/main.h +++ b/include/asm/main.h @@ -15,12 +15,13 @@ #include "extern/stdnoreturn.h" struct sOptions { - char gbgfx[4]; char binary[2]; - int32_t fillchar; - bool verbose; - bool haltnop; + char gbgfx[4]; bool exportall; + int32_t fillchar; + bool haltnop; + bool optimizeloads; + bool verbose; bool warnings; /* True to enable warnings, false to disable them. */ }; diff --git a/src/asm/asmy.y b/src/asm/asmy.y index 878d0ed6..709ebbb1 100644 --- a/src/asm/asmy.y +++ b/src/asm/asmy.y @@ -1728,7 +1728,8 @@ z80_ld_mem : T_Z80_LD op_mem_ind comma T_MODE_SP } | T_Z80_LD op_mem_ind comma T_MODE_A { - if ((!rpn_isReloc(&$2)) && ($2.nVal >= 0xFF00)) { + if (CurrentOptions.optimizeloads && + (!rpn_isReloc(&$2)) && ($2.nVal >= 0xFF00)) { out_AbsByte(0xE0); out_AbsByte($2.nVal & 0xFF); } else { @@ -1781,7 +1782,8 @@ z80_ld_a : T_Z80_LD reg_r comma T_MODE_C_IND | T_Z80_LD reg_r comma op_mem_ind { if ($2 == REG_A) { - if ((!rpn_isReloc(&$4)) && ($4.nVal >= 0xFF00)) { + if (CurrentOptions.optimizeloads && + (!rpn_isReloc(&$4)) && ($4.nVal >= 0xFF00)) { out_AbsByte(0xF0); out_AbsByte($4.nVal & 0xFF); } else { diff --git a/src/asm/main.c b/src/asm/main.c index 16dcd3e9..28659e53 100644 --- a/src/asm/main.c +++ b/src/asm/main.c @@ -282,7 +282,7 @@ void warning(const char *fmt, ...) static void print_usage(void) { printf( -"usage: rgbasm [-EhVvw] [-b chars] [-Dname[=value]] [-g chars] [-i path]\n" +"usage: rgbasm [-EhLVvw] [-b chars] [-Dname[=value]] [-g chars] [-i path]\n" " [-M dependfile] [-o outfile] [-p pad_value] file.asm\n"); exit(1); } @@ -316,17 +316,18 @@ int main(int argc, char *argv[]) DefaultOptions.gbgfx[3] = '3'; DefaultOptions.binary[0] = '0'; DefaultOptions.binary[1] = '1'; - DefaultOptions.fillchar = 0; - DefaultOptions.verbose = false; - DefaultOptions.haltnop = true; DefaultOptions.exportall = false; + DefaultOptions.fillchar = 0; + DefaultOptions.optimizeloads = true; + DefaultOptions.haltnop = true; + DefaultOptions.verbose = false; DefaultOptions.warnings = true; opt_SetCurrentOptions(&DefaultOptions); newopt = CurrentOptions; - while ((ch = getopt(argc, argv, "b:D:g:hi:M:o:p:EVvw")) != -1) { + while ((ch = getopt(argc, argv, "b:D:Eg:hi:LM:o:p:Vvw")) != -1) { switch (ch) { case 'b': if (strlen(optarg) == 2) { @@ -358,6 +359,9 @@ int main(int argc, char *argv[]) case 'i': fstk_AddIncludePath(optarg); break; + case 'L': + newopt.optimizeloads = false; + break; case 'M': dependfile = fopen(optarg, "w"); if (dependfile == NULL) diff --git a/src/asm/rgbasm.1 b/src/asm/rgbasm.1 index 96370712..698dad22 100644 --- a/src/asm/rgbasm.1 +++ b/src/asm/rgbasm.1 @@ -5,7 +5,7 @@ .\" .\" SPDX-License-Identifier: MIT .\" -.Dd January 26, 2018 +.Dd February 24, 2018 .Dt RGBASM 1 .Os RGBDS Manual .Sh NAME @@ -13,7 +13,7 @@ .Nd Game Boy assembler .Sh SYNOPSIS .Nm rgbasm -.Op Fl EhVvw +.Op Fl EhLVvw .Op Fl b Ar chars .Op Fl D Ar name Ns Op = Ns Ar value .Op Fl g Ar chars @@ -55,6 +55,12 @@ The option disables this behavior. .It Fl i Ar path Add an include path. +.It Fl L +Disable the optimization that turns loads of the form +.Sy LD [$FF00+n8],A +into the opcode +.Sy LDH [$FF00+n8],A +in order to have full control of the result in the final ROM. .It Fl M Ar dependfile Print .Xr make 1 @@ -75,7 +81,9 @@ Disable warning output. .Sh EXAMPLES Assembling a basic source file is simple: .Pp -.D1 $ rgbasm -o bar.o foo.asm +.Bd -literal -offset indent +$ rgbasm -o bar.o foo.asm +.Ed .Pp The resulting object file is not yet a usable ROM image \(em it must first be run through diff --git a/src/asm/rgbasm.5 b/src/asm/rgbasm.5 index 64a028fa..8d7baeb2 100644 --- a/src/asm/rgbasm.5 +++ b/src/asm/rgbasm.5 @@ -5,7 +5,7 @@ .\" .\" SPDX-License-Identifier: MIT .\" -.Dd January 27, 2018 +.Dd February 24, 2018 .Dt RGBASM 5 .Os RGBDS Manual .Sh NAME @@ -110,7 +110,13 @@ and .Sy LDH A,[$FF00+n8] syntax instead. This forces the assembler to emit the correct instruction and the linker to -check if the value is in the correct range. +check if the value is in the correct range. This optimization can be disabled +by passing the +.Fl L +flag to +.Sy rgbasm +as explained in +.Xr rgbasm 1 . .El .Pp A section is usually defined as a floating one, but the code can restrict where diff --git a/src/rgbds.7 b/src/rgbds.7 index 10946b05..e37f0dde 100644 --- a/src/rgbds.7 +++ b/src/rgbds.7 @@ -5,7 +5,7 @@ .\" .\" SPDX-License-Identifier: MIT .\" -.Dd January 26, 2018 +.Dd February 24, 2018 .Dt RGBDS 7 .Os RGBDS Manual .Sh NAME @@ -14,9 +14,11 @@ .Sh EXAMPLES To get a working ROM image from a single assembly source file: .Pp -.D1 $ rgbasm \-o bar.o foo.asm -.D1 $ rgblink \-o baz.gb bar.o -.D1 $ rgbfix \-v \-p 0 baz.gb +.Bd -literal -offset indent +$ rgbasm \-o bar.o foo.asm +$ rgblink \-o baz.gb bar.o +$ rgbfix \-v \-p 0 baz.gb +.Ed .Sh SEE ALSO .Xr rgbasm 1 , .Xr rgbfix 1 ,