shithub: rgbds

Download patch

ref: 2c30ab8731ab1b9f3880b1a8f55e49abe2b625e9
parent: 432c769d604a481bdb96e9f13c53289ed5028b0b
author: Rangi <[email protected]>
date: Mon Mar 15 05:44:38 EDT 2021

Allow OPT to modify -L

-L is a Boolean flag option, so you specify 'OPT L' or 'OPT !L'.

--- 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. */
 
--- a/include/asm/opt.h
+++ b/include/asm/opt.h
@@ -9,14 +9,15 @@
 #ifndef RGBDS_OPT_H
 #define RGBDS_OPT_H
 
+#include <stdbool.h>
 
 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
--- a/src/asm/main.c
+++ b/src/asm/main.c
@@ -51,7 +51,7 @@
 char *tzTargetFileName;
 
 bool haltnop;
-bool optimizeloads;
+bool optimizeLoads;
 bool verbose;
 bool warnings; /* True to enable warnings, false to disable them. */
 
@@ -166,7 +166,7 @@
 	opt_B("01");
 	opt_G("0123");
 	opt_P(0);
-	optimizeloads = true;
+	optimizeLoads = true;
 	haltnop = true;
 	verbose = false;
 	warnings = true;
@@ -214,7 +214,7 @@
 			break;
 
 		case 'L':
-			optimizeloads = false;
+			optimizeLoads = false;
 			break;
 
 		case 'M':
--- a/src/asm/opt.c
+++ b/src/asm/opt.c
@@ -1,4 +1,3 @@
-
 #include <errno.h>
 #include <stdbool.h>
 #include <stdint.h>
@@ -7,6 +6,7 @@
 #include <string.h>
 
 #include "asm/lexer.h"
+#include "asm/main.h"
 #include "asm/section.h"
 #include "asm/warning.h"
 
@@ -14,6 +14,7 @@
 	char binary[2];
 	char gbgfx[4];
 	int32_t fillByte;
+	bool optimizeLoads;
 	struct OptStackEntry *next;
 };
 
@@ -34,6 +35,11 @@
 	fillByte = fill;
 }
 
+void opt_L(bool optimize)
+{
+	optimizeLoads = optimize;
+}
+
 void opt_Parse(char *s)
 {
 	switch (s[0]) {
@@ -66,6 +72,28 @@
 		}
 		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 @@
 
 	entry->fillByte = fillByte; // Pulled from section.h
 
+	entry->optimizeLoads = optimizeLoads; // Pulled from main.h
+
 	entry->next = stack;
 	stack = entry;
 }
@@ -106,6 +136,7 @@
 	opt_B(entry->binary);
 	opt_G(entry->gbgfx);
 	opt_P(entry->fillByte);
+	opt_L(entry->optimizeLoads);
 	stack = entry->next;
 	free(entry);
 }
--- a/src/asm/parser.y
+++ b/src/asm/parser.y
@@ -1831,7 +1831,7 @@
 			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 @@
 		}
 		| 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);
--- a/src/asm/rgbasm.5
+++ b/src/asm/rgbasm.5
@@ -1883,16 +1883,24 @@
 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
--- /dev/null
+++ b/test/asm/opt.asm
@@ -1,0 +1,10 @@
+SECTION "test", ROM0
+
+pusho
+	opt p42, !L
+	ds 1
+	ld [$ff88], a
+popo
+
+	ds 1
+	ld [$ff88], a
--- /dev/null
+++ b/test/asm/opt.out.bin
@@ -1,0 +1,1 @@
+B��
\ No newline at end of file