Allow BANK() in constexpr expressions

This commit is contained in:
ISSOtm
2019-12-07 23:43:02 +01:00
parent e4f4706508
commit b49e025703
3 changed files with 47 additions and 0 deletions

View File

@@ -20,6 +20,8 @@ struct ConstExpression {
}; };
void constexpr_Symbol(struct ConstExpression *expr, char *tzSym); 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_Number(struct ConstExpression *expr, int32_t i);
void constexpr_UnaryOp(struct ConstExpression *expr, void constexpr_UnaryOp(struct ConstExpression *expr,
int32_t op, int32_t op,

View File

@@ -1394,6 +1394,14 @@ const : T_ID { constexpr_Symbol(&$$, $1); }
| T_NUMBER { constexpr_Number(&$$, $1); } | T_NUMBER { constexpr_Number(&$$, $1); }
| T_OP_HIGH '(' const ')' { constexpr_UnaryOp(&$$, $1, &$3); } | T_OP_HIGH '(' const ')' { constexpr_UnaryOp(&$$, $1, &$3); }
| T_OP_LOW '(' 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 | string
{ {
char *s = $1; char *s = $1;

View File

@@ -15,6 +15,7 @@
#include "asm/lexer.h" #include "asm/lexer.h"
#include "asm/main.h" #include "asm/main.h"
#include "asm/mymath.h" #include "asm/mymath.h"
#include "asm/output.h"
#include "asm/rpn.h" #include "asm/rpn.h"
#include "asm/symbol.h" #include "asm/symbol.h"
#include "asm/warning.h" #include "asm/warning.h"
@@ -37,6 +38,42 @@ void constexpr_Symbol(struct ConstExpression *expr, char *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) void constexpr_Number(struct ConstExpression *expr, int32_t i)
{ {
expr->u.nVal = i; expr->u.nVal = i;