Add heapsize and stacksize pragmas and check in linker

This commit is contained in:
drmortalwombat 2022-09-12 22:01:58 +02:00
parent f0c36c6400
commit 8c19b1f148
4 changed files with 36 additions and 0 deletions

View File

@ -400,7 +400,16 @@ Regions can also be used to place assets such as character sets at fixed locatio
The #pragma data(), #pragma code() and #pragma bss() control the placement of the generated objects into sections other than the default sections. The #pragma data(), #pragma code() and #pragma bss() control the placement of the generated objects into sections other than the default sections.
#### Heap and Stack sections
The heap and stack sections have an initial size of 1K and 4K. The heap will grow to use all space between the end of the data/bss section and
the start of the stack section. The size of the stack and the minimum size of the heap can be specified using pragmas:
#pragma stacksize( 4096 )
#pragma heapsize( 4096 )
The linker will throw an error if the heap or stack cannot be placed without collision.
### Inline Assembler ### Inline Assembler

View File

@ -23,6 +23,7 @@ Compiler::Compiler(void)
mCompilationUnits->mSectionStack = mLinker->AddSection(Ident::Unique("stack"), LST_STACK); mCompilationUnits->mSectionStack = mLinker->AddSection(Ident::Unique("stack"), LST_STACK);
mCompilationUnits->mSectionZeroPage = mLinker->AddSection(Ident::Unique("zeropage"), LST_ZEROPAGE); mCompilationUnits->mSectionZeroPage = mLinker->AddSection(Ident::Unique("zeropage"), LST_ZEROPAGE);
mCompilationUnits->mSectionStack->mSize = 4096; mCompilationUnits->mSectionStack->mSize = 4096;
mCompilationUnits->mSectionHeap->mSize = 1024;
mPreprocessor = new Preprocessor(mErrors); mPreprocessor = new Preprocessor(mErrors);
mByteCodeGenerator = new ByteCodeGenerator(mErrors, mLinker); mByteCodeGenerator = new ByteCodeGenerator(mErrors, mLinker);

View File

@ -392,6 +392,12 @@ void Linker::Link(void)
lsec->mEnd = lsec->mStart; lsec->mEnd = lsec->mStart;
lsec->mStart = lrgn->mEnd; lsec->mStart = lrgn->mEnd;
if (lsec->mStart < lrgn->mStart + lrgn->mUsed)
{
Location loc;
mErrors->Error(loc, ERRR_INSUFFICIENT_MEMORY, "Cannot place stack section");
}
} }
} }
} }
@ -409,6 +415,12 @@ void Linker::Link(void)
{ {
lsec->mStart = lrgn->mStart + lrgn->mUsed; lsec->mStart = lrgn->mStart + lrgn->mUsed;
lsec->mEnd = lrgn->mEnd; lsec->mEnd = lrgn->mEnd;
if (lsec->mStart + lsec->mSize > lsec->mEnd)
{
Location loc;
mErrors->Error(loc, ERRR_INSUFFICIENT_MEMORY, "Cannot place heap section");
}
} }
} }
} }

View File

@ -3367,6 +3367,20 @@ void Parser::ParsePragma(void)
ConsumeToken(TK_CLOSE_PARENTHESIS); ConsumeToken(TK_CLOSE_PARENTHESIS);
} }
else if (!strcmp(mScanner->mTokenIdent->mString, "heapsize"))
{
mScanner->NextToken();
ConsumeToken(TK_OPEN_PARENTHESIS);
if (mScanner->mToken == TK_INTEGER)
{
mCompilationUnits->mSectionHeap->mSize = mScanner->mTokenInteger;
mScanner->NextToken();
}
else
mErrors->Error(mScanner->mLocation, EERR_PRAGMA_PARAMETER, "Heap size expected");
ConsumeToken(TK_CLOSE_PARENTHESIS);
}
else if (!strcmp(mScanner->mTokenIdent->mString, "charmap")) else if (!strcmp(mScanner->mTokenIdent->mString, "charmap"))
{ {
mScanner->NextToken(); mScanner->NextToken();