mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Implement LOAD/ENDL blocks
Basically implements and closes rednex#274.
This commit is contained in:
@@ -25,6 +25,11 @@ struct SectionSpec {
|
|||||||
struct Section *out_FindSectionByName(const char *pzName);
|
struct Section *out_FindSectionByName(const char *pzName);
|
||||||
void out_NewSection(char const *pzName, uint32_t secttype, int32_t org,
|
void out_NewSection(char const *pzName, uint32_t secttype, int32_t org,
|
||||||
struct SectionSpec const *attributes);
|
struct SectionSpec const *attributes);
|
||||||
|
void out_SetLoadSection(char const *name, uint32_t secttype, int32_t org,
|
||||||
|
struct SectionSpec const *attributes);
|
||||||
|
void out_EndLoadSection(void);
|
||||||
|
|
||||||
|
struct Section *sect_GetSymbolSection(void);
|
||||||
|
|
||||||
void out_AbsByte(uint8_t b);
|
void out_AbsByte(uint8_t b);
|
||||||
void out_AbsByteGroup(uint8_t const *s, int32_t length);
|
void out_AbsByteGroup(uint8_t const *s, int32_t length);
|
||||||
|
|||||||
@@ -575,6 +575,7 @@ static void strsubUTF8(char *dest, const char *src, uint32_t pos, uint32_t len)
|
|||||||
%token T_POP_POPC
|
%token T_POP_POPC
|
||||||
%token T_POP_SHIFT
|
%token T_POP_SHIFT
|
||||||
%token T_POP_ENDR
|
%token T_POP_ENDR
|
||||||
|
%token T_POP_LOAD T_POP_ENDL
|
||||||
%token T_POP_FAIL
|
%token T_POP_FAIL
|
||||||
%token T_POP_WARN
|
%token T_POP_WARN
|
||||||
%token T_POP_PURGE
|
%token T_POP_PURGE
|
||||||
@@ -730,6 +731,7 @@ simple_pseudoop : include
|
|||||||
| setcharmap
|
| setcharmap
|
||||||
| pushc
|
| pushc
|
||||||
| popc
|
| popc
|
||||||
|
| load
|
||||||
| rept
|
| rept
|
||||||
| shift
|
| shift
|
||||||
| fail
|
| fail
|
||||||
@@ -777,6 +779,15 @@ warn : T_POP_WARN string { warning(WARNING_USER, "%s", $2); }
|
|||||||
shift : T_POP_SHIFT { sym_ShiftCurrentMacroArgs(); }
|
shift : T_POP_SHIFT { sym_ShiftCurrentMacroArgs(); }
|
||||||
;
|
;
|
||||||
|
|
||||||
|
load : T_POP_LOAD string comma sectiontype sectorg sectattrs
|
||||||
|
{
|
||||||
|
out_SetLoadSection($2, $4, $5, &$6);
|
||||||
|
}
|
||||||
|
| T_POP_ENDL
|
||||||
|
{
|
||||||
|
out_EndLoadSection();
|
||||||
|
}
|
||||||
|
|
||||||
rept : T_POP_REPT uconst
|
rept : T_POP_REPT uconst
|
||||||
{
|
{
|
||||||
uint32_t nDefinitionLineNo = nLineNo;
|
uint32_t nDefinitionLineNo = nLineNo;
|
||||||
|
|||||||
@@ -508,6 +508,9 @@ const struct sLexInitString lexer_strings[] = {
|
|||||||
/* Not needed but we have it here just to protect the name */
|
/* Not needed but we have it here just to protect the name */
|
||||||
{"endr", T_POP_ENDR},
|
{"endr", T_POP_ENDR},
|
||||||
|
|
||||||
|
{"load", T_POP_LOAD},
|
||||||
|
{"endl", T_POP_ENDL},
|
||||||
|
|
||||||
{"if", T_POP_IF},
|
{"if", T_POP_IF},
|
||||||
{"else", T_POP_ELSE},
|
{"else", T_POP_ELSE},
|
||||||
{"elif", T_POP_ELIF},
|
{"elif", T_POP_ELIF},
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ struct SectionStackEntry {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct SectionStackEntry *pSectionStack;
|
struct SectionStackEntry *pSectionStack;
|
||||||
|
static struct Section *currentLoadSection = NULL;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A quick check to see if we have an initialized section
|
* A quick check to see if we have an initialized section
|
||||||
@@ -82,9 +83,39 @@ struct Section *out_FindSectionByName(const char *pzName)
|
|||||||
/*
|
/*
|
||||||
* Find a section by name and type. If it doesn't exist, create it
|
* Find a section by name and type. If it doesn't exist, create it
|
||||||
*/
|
*/
|
||||||
static struct Section *findSection(char const *pzName, enum SectionType type,
|
static struct Section *getSection(char const *pzName, enum SectionType type,
|
||||||
int32_t org, int32_t bank, int32_t alignment)
|
int32_t org, int32_t bank, int32_t alignment)
|
||||||
{
|
{
|
||||||
|
if (bank != -1) {
|
||||||
|
if (type != SECTTYPE_ROMX && type != SECTTYPE_VRAM
|
||||||
|
&& type != SECTTYPE_SRAM && type != SECTTYPE_WRAMX)
|
||||||
|
yyerror("BANK only allowed for ROMX, WRAMX, SRAM, or VRAM sections");
|
||||||
|
else if (bank < bankranges[type][0]
|
||||||
|
|| bank > bankranges[type][1])
|
||||||
|
yyerror("%s bank value $%x out of range ($%x to $%x)",
|
||||||
|
typeNames[type], bank,
|
||||||
|
bankranges[type][0], bankranges[type][1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (alignment != 1) {
|
||||||
|
/* It doesn't make sense to have both set */
|
||||||
|
uint32_t mask = alignment - 1;
|
||||||
|
|
||||||
|
if (org != -1) {
|
||||||
|
if (org & mask)
|
||||||
|
yyerror("Section \"%s\"'s fixed address doesn't match its alignment",
|
||||||
|
pzName);
|
||||||
|
else
|
||||||
|
alignment = 1; /* Ignore it if it's satisfied */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (org != -1) {
|
||||||
|
if (org < startaddr[type] || org > endaddr(type))
|
||||||
|
yyerror("Section \"%s\"'s fixed address %#x is outside of range [%#x; %#x]",
|
||||||
|
pzName, org, startaddr[type], endaddr(type));
|
||||||
|
}
|
||||||
|
|
||||||
struct Section *pSect = out_FindSectionByName(pzName);
|
struct Section *pSect = out_FindSectionByName(pzName);
|
||||||
|
|
||||||
if (pSect) {
|
if (pSect) {
|
||||||
@@ -140,60 +171,65 @@ static struct Section *findSection(char const *pzName, enum SectionType type,
|
|||||||
/*
|
/*
|
||||||
* Set the current section
|
* Set the current section
|
||||||
*/
|
*/
|
||||||
static void setCurrentSection(struct Section *pSect)
|
static void setSection(struct Section *pSect)
|
||||||
{
|
{
|
||||||
if (nUnionDepth > 0)
|
if (nUnionDepth > 0)
|
||||||
fatalerror("Cannot change the section within a UNION");
|
fatalerror("Cannot change the section within a UNION");
|
||||||
|
|
||||||
pCurrentSection = pSect;
|
|
||||||
nPC = (pSect != NULL) ? pSect->nPC : 0;
|
nPC = (pSect != NULL) ? pSect->nPC : 0;
|
||||||
|
|
||||||
pPCSymbol->pSection = pCurrentSection;
|
pPCSymbol->pSection = pSect;
|
||||||
pPCSymbol->isConstant = pSect && pSect->nOrg != -1;
|
pPCSymbol->isConstant = pSect && pSect->nOrg != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set the current section by name and type
|
* Set the current section by name and type
|
||||||
*/
|
*/
|
||||||
void out_NewSection(char const *pzName, uint32_t secttype, int32_t org,
|
void out_NewSection(char const *pzName, uint32_t type, int32_t org,
|
||||||
struct SectionSpec const *attributes)
|
struct SectionSpec const *attributes)
|
||||||
{
|
{
|
||||||
uint32_t align = 1 << attributes->alignment;
|
if (currentLoadSection)
|
||||||
|
fatalerror("Cannot change the section within a `LOAD` block");
|
||||||
|
|
||||||
if (attributes->bank != -1) {
|
struct Section *pSect = getSection(pzName, type, org, attributes->bank,
|
||||||
if (secttype != SECTTYPE_ROMX && secttype != SECTTYPE_VRAM
|
1 << attributes->alignment);
|
||||||
&& secttype != SECTTYPE_SRAM && secttype != SECTTYPE_WRAMX)
|
|
||||||
yyerror("BANK only allowed for ROMX, WRAMX, SRAM, or VRAM sections");
|
|
||||||
else if (attributes->bank < bankranges[secttype][0]
|
|
||||||
|| attributes->bank > bankranges[secttype][1])
|
|
||||||
yyerror("%s bank value $%x out of range ($%x to $%x)",
|
|
||||||
typeNames[secttype], attributes->bank,
|
|
||||||
bankranges[secttype][0],
|
|
||||||
bankranges[secttype][1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (align != 1) {
|
nPC = pSect->nPC;
|
||||||
/* It doesn't make sense to have both set */
|
setSection(pSect);
|
||||||
uint32_t mask = align - 1;
|
pCurrentSection = pSect;
|
||||||
|
}
|
||||||
|
|
||||||
if (org != -1) {
|
/*
|
||||||
if (org & mask)
|
* Set the current section by name and type
|
||||||
yyerror("Section \"%s\"'s fixed address doesn't match its alignment",
|
*/
|
||||||
pzName);
|
void out_SetLoadSection(char const *name, uint32_t type, int32_t org,
|
||||||
else
|
struct SectionSpec const *attributes)
|
||||||
align = 1; /* Ignore it if it's satisfied */
|
{
|
||||||
}
|
if (currentLoadSection)
|
||||||
}
|
fatalerror("`LOAD` blocks cannot be nested");
|
||||||
|
|
||||||
if (org != -1) {
|
struct Section *pSect = getSection(name, type, org, attributes->bank,
|
||||||
if (org < startaddr[secttype] || org > endaddr(secttype))
|
1 << attributes->alignment);
|
||||||
yyerror("Section \"%s\"'s fixed address %#x is outside of range [%#x; %#x]",
|
|
||||||
pzName, org, startaddr[secttype],
|
|
||||||
endaddr(secttype));
|
|
||||||
}
|
|
||||||
|
|
||||||
setCurrentSection(findSection(pzName, secttype, org, attributes->bank,
|
nPC = pSect->nPC;
|
||||||
1 << attributes->alignment));
|
setSection(pSect);
|
||||||
|
currentLoadSection = pSect;
|
||||||
|
}
|
||||||
|
|
||||||
|
void out_EndLoadSection(void)
|
||||||
|
{
|
||||||
|
if (!currentLoadSection)
|
||||||
|
yyerror("Found `ENDL` outside of a `LOAD` block");
|
||||||
|
currentLoadSection = NULL;
|
||||||
|
sym_SetCurrentSymbolScope(NULL);
|
||||||
|
|
||||||
|
nPC = pCurrentSection->nPC;
|
||||||
|
setSection(pCurrentSection);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Section *sect_GetSymbolSection(void)
|
||||||
|
{
|
||||||
|
return currentLoadSection ? currentLoadSection : pCurrentSection;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -201,8 +237,9 @@ void out_NewSection(char const *pzName, uint32_t secttype, int32_t org,
|
|||||||
*/
|
*/
|
||||||
static void absByteBypassCheck(uint8_t b)
|
static void absByteBypassCheck(uint8_t b)
|
||||||
{
|
{
|
||||||
pCurrentSection->tData[nPC] = b;
|
pCurrentSection->tData[pCurrentSection->nPC++] = b;
|
||||||
pCurrentSection->nPC++;
|
if (currentLoadSection)
|
||||||
|
currentLoadSection->nPC++;
|
||||||
nPC++;
|
nPC++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,6 +270,8 @@ void out_Skip(int32_t skip)
|
|||||||
checksectionoverflow(skip);
|
checksectionoverflow(skip);
|
||||||
if (!sect_HasData(pCurrentSection->nType)) {
|
if (!sect_HasData(pCurrentSection->nType)) {
|
||||||
pCurrentSection->nPC += skip;
|
pCurrentSection->nPC += skip;
|
||||||
|
if (currentLoadSection)
|
||||||
|
currentLoadSection->nPC += skip;
|
||||||
nPC += skip;
|
nPC += skip;
|
||||||
} else if (nUnionDepth > 0) {
|
} else if (nUnionDepth > 0) {
|
||||||
while (skip--)
|
while (skip--)
|
||||||
@@ -277,9 +316,10 @@ static void absWord(uint16_t b)
|
|||||||
{
|
{
|
||||||
checkcodesection();
|
checkcodesection();
|
||||||
checksectionoverflow(2);
|
checksectionoverflow(2);
|
||||||
pCurrentSection->tData[nPC] = b & 0xFF;
|
pCurrentSection->tData[pCurrentSection->nPC++] = b & 0xFF;
|
||||||
pCurrentSection->tData[nPC + 1] = b >> 8;
|
pCurrentSection->tData[pCurrentSection->nPC++] = b >> 8;
|
||||||
pCurrentSection->nPC += 2;
|
if (currentLoadSection)
|
||||||
|
currentLoadSection->nPC += 2;
|
||||||
nPC += 2;
|
nPC += 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -305,11 +345,12 @@ static void absLong(uint32_t b)
|
|||||||
{
|
{
|
||||||
checkcodesection();
|
checkcodesection();
|
||||||
checksectionoverflow(4);
|
checksectionoverflow(4);
|
||||||
pCurrentSection->tData[nPC] = b & 0xFF;
|
pCurrentSection->tData[pCurrentSection->nPC++] = b & 0xFF;
|
||||||
pCurrentSection->tData[nPC + 1] = b >> 8;
|
pCurrentSection->tData[pCurrentSection->nPC++] = b >> 8;
|
||||||
pCurrentSection->tData[nPC + 2] = b >> 16;
|
pCurrentSection->tData[pCurrentSection->nPC++] = b >> 16;
|
||||||
pCurrentSection->tData[nPC + 3] = b >> 24;
|
pCurrentSection->tData[pCurrentSection->nPC++] = b >> 24;
|
||||||
pCurrentSection->nPC += 4;
|
if (currentLoadSection)
|
||||||
|
currentLoadSection->nPC += 4;
|
||||||
nPC += 4;
|
nPC += 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -337,9 +378,10 @@ void out_PCRelByte(struct Expression *expr)
|
|||||||
checkcodesection();
|
checkcodesection();
|
||||||
checksectionoverflow(1);
|
checksectionoverflow(1);
|
||||||
if (!rpn_isKnown(expr) || pCurrentSection->nOrg == -1) {
|
if (!rpn_isKnown(expr) || pCurrentSection->nOrg == -1) {
|
||||||
pCurrentSection->tData[nPC] = 0;
|
|
||||||
out_CreatePatch(PATCHTYPE_JR, expr);
|
out_CreatePatch(PATCHTYPE_JR, expr);
|
||||||
pCurrentSection->nPC++;
|
pCurrentSection->tData[pCurrentSection->nPC++] = 0;
|
||||||
|
if (currentLoadSection)
|
||||||
|
currentLoadSection->nPC++;
|
||||||
nPC++;
|
nPC++;
|
||||||
} else {
|
} else {
|
||||||
/* Target is relative to the byte *after* the operand */
|
/* Target is relative to the byte *after* the operand */
|
||||||
@@ -382,13 +424,13 @@ void out_BinaryFile(char const *s)
|
|||||||
checkcodesection();
|
checkcodesection();
|
||||||
checksectionoverflow(fsize);
|
checksectionoverflow(fsize);
|
||||||
|
|
||||||
int32_t dest = nPC;
|
|
||||||
int32_t todo = fsize;
|
int32_t todo = fsize;
|
||||||
|
|
||||||
while (todo--)
|
while (todo--)
|
||||||
pCurrentSection->tData[dest++] = fgetc(f);
|
pCurrentSection->tData[pCurrentSection->nPC++] = fgetc(f);
|
||||||
|
|
||||||
pCurrentSection->nPC += fsize;
|
if (currentLoadSection)
|
||||||
|
currentLoadSection->nPC += fsize;
|
||||||
nPC += fsize;
|
nPC += fsize;
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
@@ -428,13 +470,13 @@ void out_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length)
|
|||||||
checkcodesection();
|
checkcodesection();
|
||||||
checksectionoverflow(length);
|
checksectionoverflow(length);
|
||||||
|
|
||||||
int32_t dest = nPC;
|
|
||||||
int32_t todo = length;
|
int32_t todo = length;
|
||||||
|
|
||||||
while (todo--)
|
while (todo--)
|
||||||
pCurrentSection->tData[dest++] = fgetc(f);
|
pCurrentSection->tData[pCurrentSection->nPC++] = fgetc(f);
|
||||||
|
|
||||||
pCurrentSection->nPC += length;
|
if (currentLoadSection)
|
||||||
|
currentLoadSection->nPC += length;
|
||||||
nPC += length;
|
nPC += length;
|
||||||
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
@@ -465,7 +507,8 @@ void out_PopSection(void)
|
|||||||
struct SectionStackEntry *pSect;
|
struct SectionStackEntry *pSect;
|
||||||
|
|
||||||
pSect = pSectionStack;
|
pSect = pSectionStack;
|
||||||
setCurrentSection(pSect->pSection);
|
setSection(pSect->pSection);
|
||||||
|
pCurrentSection = pSect->pSection;
|
||||||
sym_SetCurrentSymbolScope(pSect->pScope);
|
sym_SetCurrentSymbolScope(pSect->pScope);
|
||||||
pSectionStack = pSect->pNext;
|
pSectionStack = pSect->pNext;
|
||||||
free(pSect);
|
free(pSect);
|
||||||
|
|||||||
@@ -578,7 +578,7 @@ void sym_AddReloc(char const *tzSym)
|
|||||||
nsym->isExported = true;
|
nsym->isExported = true;
|
||||||
|
|
||||||
nsym->pScope = scope;
|
nsym->pScope = scope;
|
||||||
nsym->pSection = pCurrentSection;
|
nsym->pSection = sect_GetSymbolSection();
|
||||||
/* Labels need to be assigned a section, except PC */
|
/* Labels need to be assigned a section, except PC */
|
||||||
if (!pCurrentSection && strcmp(tzSym, "@"))
|
if (!pCurrentSection && strcmp(tzSym, "@"))
|
||||||
yyerror("Label \"%s\" created outside of a SECTION",
|
yyerror("Label \"%s\" created outside of a SECTION",
|
||||||
|
|||||||
Reference in New Issue
Block a user