shithub: rgbds

Download patch

ref: b49e025703e43088e7376231474bb546a0ba1093
parent: e4f4706508307f1a1f7b0efb545a82cad53ade2f
author: ISSOtm <eldredhabert0@gmail.com>
date: Sat Dec 7 18:43:02 EST 2019

Allow BANK() in constexpr expressions

--- a/include/asm/constexpr.h
+++ b/include/asm/constexpr.h
@@ -20,6 +20,8 @@
 };
 
 void constexpr_Symbol(struct ConstExpression *expr, char *tzSym);
+void constexpr_BanksSymbol(struct ConstExpression *expr, char *tzSym);
+void constexpr_BankSection(struct ConstExpression *expr, char *tzSym);
 void constexpr_Number(struct ConstExpression *expr, int32_t i);
 void constexpr_UnaryOp(struct ConstExpression *expr,
 		       int32_t op,
--- a/src/asm/asmy.y
+++ b/src/asm/asmy.y
@@ -1394,6 +1394,14 @@
 		| T_NUMBER				{ constexpr_Number(&$$, $1); }
 		| T_OP_HIGH '(' const ')'		{ constexpr_UnaryOp(&$$, $1, &$3); }
 		| T_OP_LOW '(' const ')'		{ constexpr_UnaryOp(&$$, $1, &$3); }
+		| T_OP_BANK '(' T_ID ')'
+		{
+			constexpr_BankSymbol(&$$, $3);
+		}
+		| T_OP_BANK '(' string ')'
+		{
+			constexpr_BankSection(&$$, $3);
+		}
 		| string
 		{
 			char *s = $1;
--- a/src/asm/constexpr.c
+++ b/src/asm/constexpr.c
@@ -15,6 +15,7 @@
 #include "asm/lexer.h"
 #include "asm/main.h"
 #include "asm/mymath.h"
+#include "asm/output.h"
 #include "asm/rpn.h"
 #include "asm/symbol.h"
 #include "asm/warning.h"
@@ -35,6 +36,42 @@
 	} else {
 		constexpr_Number(expr, sym_GetConstantValue(tzSym));
 	}
+}
+
+void constexpr_BankSymbol(struct ConstExpression *expr, char *tzSym)
+{
+	if (sym_FindSymbol(tzSym) == pPCSymbol) {
+		if (pCurrentSection->nBank == -1)
+			yyerror("%s's bank is not known yet", tzSym);
+		else
+			constexpr_Number(expr, pCurrentSection->nBank);
+		return;
+	}
+
+	if (sym_isConstant(tzSym)) {
+		yyerror("BANK argument must be a relocatable identifier");
+	} else {
+		struct sSymbol *pSymbol = sym_FindSymbol(tzSym);
+
+		if (!pSymbol)
+			yyerror("BANK argument doesn't exist");
+		else if (!pSymbol->pSection || pSymbol->pSection->nBank == -1)
+			yyerror("BANK argument must be a relocatable identifier");
+		else
+			constexpr_Number(expr, pSymbol->pSection->nBank);
+	}
+}
+
+void constexpr_BankSection(struct ConstExpression *expr, char *tzSectionName)
+{
+	struct Section *pSection = out_FindSectionByName(tzSectionName);
+
+	if (!pSection)
+		yyerror("Section \"%s\" doesn't exist");
+	else if (pSection->nBank == -1)
+		yyerror("Section \"%s\"'s bank is not known yet");
+	else
+		constexpr_Number(expr, pSection->nBank);
 }
 
 void constexpr_Number(struct ConstExpression *expr, int32_t i)