Skip to content

Commit

Permalink
Disallow file directives within area/region/func.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownbrackets committed Jul 31, 2020
1 parent ce12558 commit ab030fb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Commands/CAssemblerCommand.h
Expand Up @@ -5,7 +5,8 @@ class SymbolData;

struct ValidateState
{
bool allowFileChange = true;
bool noFileChange = false;
const wchar_t *noFileChangeDirective = nullptr;
};

class CAssemblerCommand
Expand Down
5 changes: 4 additions & 1 deletion Commands/CAssemblerLabel.cpp
Expand Up @@ -127,8 +127,11 @@ bool CDirectiveFunction::Validate(const ValidateState &state)
label->applyFileInfo();
bool result = label->Validate(state);

ValidateState contentValidation = state;
contentValidation.noFileChange = true;
contentValidation.noFileChangeDirective = L"function";
content->applyFileInfo();
if (content->Validate(state))
if (content->Validate(contentValidation))
result = true;

end = g_fileManager->getVirtualAddress();
Expand Down
13 changes: 10 additions & 3 deletions Commands/CDirectiveArea.cpp
Expand Up @@ -75,8 +75,11 @@ bool CDirectiveArea::Validate(const ValidateState &state)
bool result = false;
if (content)
{
ValidateState contentValidation = state;
contentValidation.noFileChange = true;
contentValidation.noFileChangeDirective = L"area";
content->applyFileInfo();
result = content->Validate(state);
result = content->Validate(contentValidation);
}
contentSize = g_fileManager->getVirtualAddress()-position;

Expand Down Expand Up @@ -203,13 +206,17 @@ bool CDirectiveAutoRegion::Validate(const ValidateState &state)
{
resetPosition = g_fileManager->getVirtualAddress();

ValidateState contentValidation = state;
contentValidation.noFileChange = true;
contentValidation.noFileChangeDirective = L"region";

// We need at least one full pass run before we can get an address.
if (Global.validationPasses < 1)
{
// Just calculate contentSize.
position = g_fileManager->getVirtualAddress();
content->applyFileInfo();
content->Validate(state);
content->Validate(contentValidation);
contentSize = g_fileManager->getVirtualAddress() - position;

g_fileManager->seekVirtual(resetPosition);
Expand Down Expand Up @@ -249,7 +256,7 @@ bool CDirectiveAutoRegion::Validate(const ValidateState &state)
g_fileManager->seekVirtual(position);

content->applyFileInfo();
bool result = content->Validate(state);
bool result = content->Validate(contentValidation);
contentSize = g_fileManager->getVirtualAddress() - position;

// restore info of this command
Expand Down
9 changes: 9 additions & 0 deletions Commands/CDirectiveFile.cpp
Expand Up @@ -62,6 +62,15 @@ void CDirectiveFile::initClose()

bool CDirectiveFile::Validate(const ValidateState &state)
{
if (state.noFileChange)
{
if (type == Type::Close)
Logger::queueError(Logger::Error, L"Cannot close file within %S", state.noFileChangeDirective);
else
Logger::queueError(Logger::Error, L"Cannot open new file within %S", state.noFileChangeDirective);
return false;
}

virtualAddress = g_fileManager->getVirtualAddress();
Arch->NextSection();

Expand Down
17 changes: 17 additions & 0 deletions Tests/Area/CloseFile/CloseFile.asm
@@ -0,0 +1,17 @@
.gba
.create "out.bin",0

.area 0x8
.fill 0x8
.close
.endarea

.macro myopen,offset
.open "out2.bin",offset
.endmacro

.area 0x8
myopen 4
.endarea

.close
2 changes: 2 additions & 0 deletions Tests/Area/CloseFile/expected.txt
@@ -0,0 +1,2 @@
CloseFile.asm(6) error: Cannot close file within area
CloseFile.asm(14) error: Cannot open new file within area

0 comments on commit ab030fb

Please sign in to comment.