Skip to content

Commit

Permalink
[in progress] Implement ds align[]
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 committed Oct 26, 2023
1 parent 944c5f0 commit 390f49b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/asm/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ int yylex(void);
bool lexer_CaptureRept(struct CaptureBody *capture);
bool lexer_CaptureMacroBody(struct CaptureBody *capture);

struct DsAlignment {
uint32_t nbBytes;
uint8_t alignment;
uint16_t alignOfs;
};

#define INITIAL_DS_ARG_SIZE 2
struct DsArgList {
size_t nbArgs;
Expand Down
36 changes: 36 additions & 0 deletions src/asm/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ enum {
struct SectionSpec sectSpec;
struct MacroArgs *macroArg;
enum AssertionType assertType;
struct DsAlignment dsAlign;
struct DsArgList dsArgs;
struct PurgeArgList purgeArgs;
struct {
Expand Down Expand Up @@ -664,6 +665,7 @@ enum {
%type <sectMod> sectmod
%type <macroArg> macroargs

%type <dsAlign> ds_align
%type <dsArgs> ds_args

%type <purgeArgs> purge_args
Expand Down Expand Up @@ -1169,6 +1171,40 @@ ds : T_POP_DS uconst { sect_Skip($2, true); }
sect_RelBytes($2, $4.args, $4.nbArgs);
freeDsArgList(&$4);
}
| T_POP_DS ds_align trailing_comma {
sect_Skip($2.nbBytes, true);
sect_AlignPC($2.alignment, $2.alignOfs);
}
| T_POP_DS ds_align T_COMMA ds_args trailing_comma {
sect_RelBytes($2.nbBytes, $4.args, $4.nbArgs);
sect_AlignPC($2.alignment, $2.alignOfs);
freeDsArgList(&$4);
}
;

ds_align : T_OP_ALIGN T_LBRACK uconst T_RBRACK {
if ($3 > 16) {
error("Alignment must be between 0 and 16, not %u\n", $3);
} else {
uint32_t mask = (1 << $3) - 1;

// TODO: correct formula; sym_GetPCValue() won't work in floating sections
$$.alignment = $3;
$$.nbBytes = (mask + 1 - (sym_GetPCValue() & mask)) & mask;
}
}
| T_OP_ALIGN T_LBRACK uconst T_COMMA uconst T_RBRACK {
if ($3 > 16) {
error("Alignment must be between 0 and 16, not %u\n", $3);
} else if ($5 >= 1 << $3) {
error("Offset must be between 0 and %u, not %u\n",
(1 << $3) - 1, $5);
} else {
$$.alignment = $3;
$$.alignOfs = $5;
$$.nbBytes = 0; // TODO: correct formula
}
}
;

ds_args : reloc_8bit {
Expand Down

0 comments on commit 390f49b

Please sign in to comment.