Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement WORD() function #561

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/asm/rpn.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void rpn_BinaryOp(enum RPNCommand op, struct Expression *expr,
const struct Expression *src2);
void rpn_HIGH(struct Expression *expr, const struct Expression *src);
void rpn_LOW(struct Expression *expr, const struct Expression *src);
void rpn_WORD(struct Expression *expr, const struct Expression *src);
void rpn_ISCONST(struct Expression *expr, const struct Expression *src);
void rpn_UNNEG(struct Expression *expr, const struct Expression *src);
void rpn_UNNOT(struct Expression *expr, const struct Expression *src);
Expand Down
2 changes: 2 additions & 0 deletions src/asm/asmy.y
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ static void strsubUTF8(char *dest, const char *src, uint32_t pos, uint32_t len)
%left T_OP_FLOOR

%token T_OP_HIGH T_OP_LOW
%token T_OP_WORD
%token T_OP_ISCONST

%left T_OP_STRCMP
Expand Down Expand Up @@ -1331,6 +1332,7 @@ relocexpr_no_str : scoped_id { rpn_Symbol(&$$, $1); }
| T_OP_NOT relocexpr %prec NEG { rpn_UNNOT(&$$, &$2); }
| T_OP_HIGH '(' relocexpr ')' { rpn_HIGH(&$$, &$3); }
| T_OP_LOW '(' relocexpr ')' { rpn_LOW(&$$, &$3); }
| T_OP_WORD '(' relocexpr ')' { rpn_WORD(&$$, &$3); }
| T_OP_ISCONST '(' relocexpr ')'{ rpn_ISCONST(&$$, &$3); }
| T_OP_BANK '(' scoped_id ')' {
/* '@' is also a T_ID, it is handled here. */
Expand Down
1 change: 1 addition & 0 deletions src/asm/globlex.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ const struct sLexInitString lexer_strings[] = {

{"high", T_OP_HIGH},
{"low", T_OP_LOW},
{"word", T_OP_WORD},
{"isconst", T_OP_ISCONST},

{"strcmp", T_OP_STRCMP},
Expand Down
1 change: 1 addition & 0 deletions src/asm/rgbasm.5
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ has been defined, FALSE (0) otherwise.
String symbols are not expanded within the parentheses.
.It Fn HIGH arg Ta Returns the top 8 bits of the operand if Ar arg No is a label or constant, or the top 8-bit register if it is a 16-bit register.
.It Fn LOW arg Ta Returns the bottom 8 bits of the operand if Ar arg No is a label or constant, or the bottom 8-bit register if it is a 16-bit register Pq Cm AF No isn't a valid register for this function .
.It Fn WORD arg Ta Returns the bottom 16 bits of the operand if Ar arg No is a label or constant.
.It Fn ISCONST arg Ta Returns 1 if Ar arg Ap s value is known by RGBASM (e.g. if it can be an argument to
.Ic IF ) ,
or 0 if only RGBLINK can compute its value.
Expand Down
19 changes: 17 additions & 2 deletions src/asm/rpn.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ void rpn_HIGH(struct Expression *expr, const struct Expression *src)
if (rpn_isKnown(expr)) {
expr->nVal = (uint32_t)expr->nVal >> 8 & 0xFF;
} else {
uint8_t bytes[] = {RPN_CONST, 8, 0, 0, 0, RPN_SHR,
static const uint8_t bytes[] = {RPN_CONST, 8, 0, 0, 0, RPN_SHR,
RPN_CONST, 0xFF, 0, 0, 0, RPN_AND};
expr->nRPNPatchSize += sizeof(bytes);
memcpy(reserveSpace(expr, sizeof(bytes)), bytes, sizeof(bytes));
Expand All @@ -512,7 +512,22 @@ void rpn_LOW(struct Expression *expr, const struct Expression *src)
if (rpn_isKnown(expr)) {
expr->nVal = expr->nVal & 0xFF;
} else {
uint8_t bytes[] = {RPN_CONST, 0xFF, 0, 0, 0, RPN_AND};
static const uint8_t bytes[] = {RPN_CONST, 0xFF, 0, 0, 0, RPN_AND};

expr->nRPNPatchSize += sizeof(bytes);
memcpy(reserveSpace(expr, sizeof(bytes)), bytes, sizeof(bytes));
}
}

void rpn_WORD(struct Expression *expr, const struct Expression *src)
{
*expr = *src;
expr->isSymbol = false;

if (rpn_isKnown(expr)) {
expr->nVal = expr->nVal & 0xFFFF;
} else {
static const uint8_t bytes[] = {RPN_CONST, 0xFF, 0xFF, 0, 0, RPN_AND};

expr->nRPNPatchSize += sizeof(bytes);
memcpy(reserveSpace(expr, sizeof(bytes)), bytes, sizeof(bytes));
Expand Down
17 changes: 17 additions & 0 deletions test/asm/dw-word.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
SECTION "test", ROM0[0]

dw WORD($12)
dw WORD($1234)
dw WORD($123456)
dw WORD(-Label)


SECTION "ram", WRAM0[$c123]

Label:


SECTION "test2", ROM0[8]

dw -Label
dw WORD(-Label)
2 changes: 2 additions & 0 deletions test/asm/dw-word.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
warning: dw-word.asm(16): [-Wtruncation]
Expression must be 16-bit
Empty file added test/asm/dw-word.out
Empty file.
Binary file added test/asm/dw-word.out.bin
Binary file not shown.
4 changes: 2 additions & 2 deletions test/asm/ram-code.asm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Target: dl DEAD << 16 | BEEF
ENDL
After:
jp Target
ld hl, Word
ld hl, Word_
dw Byte, Target.end, After

SECTION "dead", WRAMX[$DEAD],BANK[2]
Expand All @@ -22,7 +22,7 @@ SECTION "beef", SRAM[$BEEF]
BEEF:

SECTION "ram test", WRAMX,BANK[1] ; Should end up at $D005
Word:
Word_:
dw

SECTION "small ram test", WRAMX,BANK[1] ; Should end up at $D000
Expand Down