Skip to content

Commit

Permalink
Fix assembler label low/high byte immediate
Browse files Browse the repository at this point in the history
  • Loading branch information
drmortalwombat committed Sep 19, 2021
1 parent 1d76188 commit fae3771
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 10 deletions.
2 changes: 2 additions & 0 deletions include/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,15 @@ void * sformat(void * data, putstrfn fn, const char * fmt, int * fps)
nformi(&si, buff, *fps++, false);
data = fn(data, buff);
}
#ifndef NOFLOAT
else if (c == 'f' || c == 'g' || c == 'e')
{
nformf(&si, buff, *(float *)fps, c);
data = fn(data, buff);
fps ++;
fps ++;
}
#endif
else if (c == 's')
{
data = fn(data, (char *)*fps++);
Expand Down
15 changes: 14 additions & 1 deletion oscar64/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <stdio.h>

Compiler::Compiler(void)
: mByteCodeFunctions(nullptr), mNativeCode(false)
: mByteCodeFunctions(nullptr), mNativeCode(false), mDefines({nullptr, nullptr})
{
mErrors = new Errors();
mCompilationUnits = new CompilationUnits(mErrors);
Expand All @@ -29,6 +29,15 @@ void Compiler::ForceNativeCode(bool native)
mNativeCode = native;
}

void Compiler::AddDefine(const Ident* ident, const char* value)
{
Define define;
define.mIdent = ident;
define.mValue = value;
mDefines.Push(define);
}


bool Compiler::ParseSource(void)
{
CompilationUnit* cunit;
Expand All @@ -37,6 +46,10 @@ bool Compiler::ParseSource(void)
if (mPreprocessor->OpenSource(cunit->mFileName, true))
{
Scanner* scanner = new Scanner(mErrors, mPreprocessor);

for (int i = 0; i < mDefines.Size(); i++)
scanner->AddMacro(mDefines[i].mIdent, mDefines[i].mValue);

Parser* parser = new Parser(mErrors, scanner, mCompilationUnits);

parser->Parse();
Expand Down
9 changes: 9 additions & 0 deletions oscar64/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ class Compiler

bool mNativeCode;

struct Define
{
const Ident* mIdent;
const char* mValue;
};

GrowingArray<Define> mDefines;

bool ParseSource(void);
bool GenerateCode(void);
bool WriteOutputFile(const char* targetPath);
int ExecuteCode(void);

void ForceNativeCode(bool native);
void AddDefine(const Ident* ident, const char* value);
};
23 changes: 23 additions & 0 deletions oscar64/InterCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,29 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration*
case ASMIM_IMPLIED:
break;
case ASMIM_IMMEDIATE:
if (aexp->mType == DT_CONST_INTEGER)
d[offset++] = cexp->mLeft->mDecValue->mInteger & 255;
else if (aexp->mType == DT_LABEL_REF)
{
if (aexp->mBase->mBase->mVarIndex < 0)
{
InterCodeBasicBlock* bblock = nullptr;
TranslateExpression(procType, proc, bblock, aexp->mBase->mBase->mValue, breakBlock, continueBlock);
}

InterVariable::Reference ref;
ref.mFunction = false;
ref.mUpper = aexp->mFlags & DTF_UPPER_BYTE;
ref.mLower = !(aexp->mFlags & DTF_UPPER_BYTE);
ref.mAddr = offset;
ref.mIndex = aexp->mBase->mBase->mVarIndex;
ref.mOffset = aexp->mOffset + aexp->mBase->mInteger;

references.Push(ref);

offset += 1;
}
break;
case ASMIM_ZERO_PAGE:
case ASMIM_ZERO_PAGE_X:
case ASMIM_INDIRECT_X:
Expand Down
32 changes: 32 additions & 0 deletions oscar64/Linker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "Linker.h"

Linker::Linker(Errors* errors)
: mErrors(errors), mSections(nullptr), mReferences(nullptr), mObjects(nullptr)
{

}

Linker::~Linker(void)
{

}

int Linker::AddSection(const Ident* section, int start, int size)
{
return 0;
}

void Linker::AddSectionData(const Ident* section, int id, const uint8* data, int size)
{

}

uint8* Linker::AddSectionSpace(const Ident* section, int id, int size)
{
return nullptr;
}

void Linker::AddReference(const LinkerReference& ref)
{

}
48 changes: 48 additions & 0 deletions oscar64/Linker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include "MachineTypes.h"
#include "Ident.h"
#include "Array.h"
#include "Errors.h"

struct LinkerReference
{
int mSection, mID, mOffset;
int mRefSection, mRefID, mRefOffset;
bool mLowByte, mHighByte;
};

struct LinkerSection
{
const Ident* mIdent;
int mID;
int mStart, mSize;
};

struct LinkerObject
{
const Ident* mIdent;
int mID;
int mSize;
uint8* mData;
};

class Linker
{
public:
Linker(Errors * errors);
~Linker(void);

int AddSection(const Ident* section, int start, int size);
void AddSectionData(const Ident* section, int id, const uint8* data, int size);
uint8 * AddSectionSpace(const Ident* section, int id, int size);
void AddReference(const LinkerReference& ref);

GrowingArray<LinkerReference*> mReferences;
GrowingArray<LinkerSection*> mSections;
GrowingArray<LinkerObject*> mObjects;

void Link(void);
protected:
Errors* mErrors;
};
48 changes: 48 additions & 0 deletions oscar64/Scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ static inline int HexValue(char ch)
return 0;
}

void Scanner::AddMacro(const Ident* ident, const char* value)
{
Macro* macro = new Macro(ident);
macro->SetString(value);
mDefines->Insert(macro);
}

void Scanner::NextToken(void)
{
for (;;)
Expand Down Expand Up @@ -636,6 +643,29 @@ void Scanner::NextRawToken(void)
}
break;
case '%':
if (mAssemblerMode)
{
int n = 0;
__int64 mant = 0;
while (NextChar())
{
if (mTokenChar >= '0' && mTokenChar <= '9')
mant = mant * 16 + (int)mTokenChar - (int)'0';
else if (mTokenChar >= 'a' && mTokenChar <= 'f')
mant = mant * 16 + 10 + (int)mTokenChar - (int)'a';
else if (mTokenChar >= 'A' && mTokenChar <= 'F')
mant = mant * 16 + 10 + (int)mTokenChar - (int)'A';
else
break;
n++;
}

if (n == 0)
mErrors->Error(mLocation, "Missing digits in hex constant");

mToken = TK_INTEGER;
mTokenInteger = mant;
}
mToken = TK_MOD;
NextChar();
if (mTokenChar == '=')
Expand Down Expand Up @@ -1194,6 +1224,24 @@ void Scanner::ParseNumberToken(void)
mToken = TK_INTEGER;
mTokenInteger = mant;
}
else if (mant == 0 && (mTokenChar == 'b' || mTokenChar == 'B'))
{
int n = 0;
while (NextChar())
{
if (mTokenChar >= '0' && mTokenChar <= '1')
mant = mant * 2 + (int)mTokenChar - (int)'0';
else
break;
n++;
}

if (n == 0)
Error("Missing digits in binary constant");

mToken = TK_INTEGER;
mTokenInteger = mant;
}
else
{
int n = 0;
Expand Down
2 changes: 2 additions & 0 deletions oscar64/Scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ class Scanner
void SetAssemblerMode(bool mode);

bool mAssemblerMode;

void AddMacro(const Ident* ident, const char* value);
protected:
void NextRawToken(void);

Expand Down
20 changes: 18 additions & 2 deletions oscar64/oscar64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main(int argc, const char** argv)
InitDeclarations();
InitAssembler();

if (argc > 0)
if (argc > 1)
{
char basePath[200], crtPath[200], includePath[200], targetPath[200];
char strProductName[100], strProductVersion[200];
Expand Down Expand Up @@ -119,6 +119,21 @@ int main(int argc, const char** argv)
{
emulate = true;
}
else if (arg[1] == 'd')
{
char def[100];
int i = 2;
while (arg[i] && arg[i] != '=')
{
def[i - 2] = arg[i];
i++;
}
def[i - 2] = 0;
if (arg[i] == '=')
compiler->AddDefine(Ident::Unique(def), _strdup(arg + i + 1));
else
compiler->AddDefine(Ident::Unique(def), "");
}
else
compiler->mErrors->Error(loc, "Invalid command line argument", arg);
}
Expand Down Expand Up @@ -147,7 +162,8 @@ int main(int argc, const char** argv)
}
else
{
printf("oscar64 {-i=includePath} [-o=output.prg] [-cr=runtime.c] [-e] {source.c}\n");
printf("oscar64 {-i=includePath} [-o=output.prg] [-cr=runtime.c] [-e] [-n] [-dSYMBOL[=value]] {source.c}\n");

return 0;
}

Expand Down
8 changes: 4 additions & 4 deletions oscar64/oscar64.rc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ LANGUAGE LANG_ENGLISH, SUBLANG_NEUTRAL
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,27,0
PRODUCTVERSION 1,0,27,0
FILEVERSION 1,0,28,0
PRODUCTVERSION 1,0,28,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -43,12 +43,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "oscar64"
VALUE "FileDescription", "oscar64 compiler"
VALUE "FileVersion", "1.0.27.0"
VALUE "FileVersion", "1.0.28.0"
VALUE "InternalName", "oscar64.exe"
VALUE "LegalCopyright", "Copyright (C) 2021"
VALUE "OriginalFilename", "oscar64.exe"
VALUE "ProductName", "oscar64"
VALUE "ProductVersion", "1.0.27.0"
VALUE "ProductVersion", "1.0.28.0"
END
END
BLOCK "VarFileInfo"
Expand Down
2 changes: 2 additions & 0 deletions oscar64/oscar64.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
<ClCompile Include="Ident.cpp" />
<ClCompile Include="InterCode.cpp" />
<ClCompile Include="InterCodeGenerator.cpp" />
<ClCompile Include="Linker.cpp" />
<ClCompile Include="NativeCodeGenerator.cpp" />
<ClCompile Include="NumberSet.cpp" />
<ClCompile Include="oscar64.cpp" />
Expand All @@ -171,6 +172,7 @@
<ClInclude Include="Ident.h" />
<ClInclude Include="InterCode.h" />
<ClInclude Include="InterCodeGenerator.h" />
<ClInclude Include="Linker.h" />
<ClInclude Include="MachineTypes.h" />
<ClInclude Include="NativeCodeGenerator.h" />
<ClInclude Include="NumberSet.h" />
Expand Down
6 changes: 6 additions & 0 deletions oscar64/oscar64.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
<ClCompile Include="NativeCodeGenerator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Linker.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Array.h">
Expand Down Expand Up @@ -125,6 +128,9 @@
<ClInclude Include="NativeCodeGenerator.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Linker.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="oscar64.rc">
Expand Down
Loading

0 comments on commit fae3771

Please sign in to comment.