From 722347609d1e1847582dd7fde1b25b467f7f699f Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Tue, 7 Sep 2021 13:37:56 +0200 Subject: [PATCH] Fix array of array of char init with strings --- oscar64/NativeCodeGenerator.cpp | 113 ++++++++++++++++++++++++++++++++ oscar64/NativeCodeGenerator.h | 71 ++++++++++++++++++++ oscar64/Parser.cpp | 3 +- oscar64/oscar64.vcxproj | 2 + oscar64/oscar64.vcxproj.filters | 6 ++ 5 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 oscar64/NativeCodeGenerator.cpp create mode 100644 oscar64/NativeCodeGenerator.h diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp new file mode 100644 index 0000000..a8f77bf --- /dev/null +++ b/oscar64/NativeCodeGenerator.cpp @@ -0,0 +1,113 @@ +#include "NativeCodeGenerator.h" + +NativeCodeInstruction::NativeCodeInstruction(AsmInsType type, AsmInsMode mode) + : mType(type), mMode(mode), mGlobal(false), mAddress(0), mVarIndex(-1) +{} + + +void NativeCodeInstruction::Assemble(ByteCodeGenerator* generator, NativeCodeBasicBlock* block) +{ + switch (mMode) + { + block->PutByte(AsmInsOpcodes[mType][mMode]); + + switch (mMode) + { + case ASMIM_IMPLIED: + break; + case ASMIM_IMMEDIATE: + case ASMIM_ZERO_PAGE: + case ASMIM_ZERO_PAGE_X: + case ASMIM_INDIRECT_X: + case ASMIM_INDIRECT_Y: + block->PutByte(uint8(mAddress)); + break; + case ASMIM_ABSOLUTE: + case ASMIM_INDIRECT: + case ASMIM_ABSOLUTE_X: + case ASMIM_ABSOLUTE_Y: + if (mGlobal) + { + ByteCodeRelocation rl; + rl.mAddr = block->mCode.Size(); + rl.mFunction = false; + rl.mLower = true; + rl.mUpper = true; + rl.mIndex = mVarIndex; + rl.mOffset = mAddress; + block->mRelocations.Push(rl); + block->PutWord(0); + } + else + { + block->PutWord(uint16(mAddress)); + } + break; + case ASMIM_RELATIVE: + block->PutByte(uint8(mAddress - block->mCode.Size() - 1)); + break; + } + } +} + + +void NativeCodeBasicBlock::PutByte(uint8 code) +{ + this->mCode.Insert(code); +} + +void NativeCodeBasicBlock::PutWord(uint16 code) +{ + this->mCode.Insert((uint8)(code & 0xff)); + this->mCode.Insert((uint8)(code >> 8)); +} +#if 0 +static AsmInsType InvertBranchCondition(AsmInsType code) +{ + switch (code) + { + case BC_BRANCHS_EQ: return BC_BRANCHS_NE; + case BC_BRANCHS_NE: return BC_BRANCHS_EQ; + case BC_BRANCHS_GT: return BC_BRANCHS_LE; + case BC_BRANCHS_GE: return BC_BRANCHS_LT; + case BC_BRANCHS_LT: return BC_BRANCHS_GE; + case BC_BRANCHS_LE: return BC_BRANCHS_GT; + default: + return code; + } +} + +static AsmInsType TransposeBranchCondition(AsmInsType code) +{ + switch (code) + { + case BC_BRANCHS_EQ: return BC_BRANCHS_EQ; + case BC_BRANCHS_NE: return BC_BRANCHS_NE; + case BC_BRANCHS_GT: return BC_BRANCHS_LT; + case BC_BRANCHS_GE: return BC_BRANCHS_LE; + case BC_BRANCHS_LT: return BC_BRANCHS_GT; + case BC_BRANCHS_LE: return BC_BRANCHS_GE; + default: + return code; + } +} + + +int NativeCodeBasicBlock::PutBranch(ByteCodeGenerator* generator, AsmInsType code, int offset) +{ + if (offset >= -126 && offset <= 129) + { + PutByte(AsmInsOpcodes[code][ASMIM_RELATIVE]); + PutByte(offset - 2); + return 2; + } + else + { + PutByte(AsmInsOpcodes[code][ASMIM_RELATIVE]); + + PutCode(generator, ByteCode(code + (BC_JUMPF - BC_JUMPS))); + PutWord(offset - 3); + return 3; + } +} +#endif diff --git a/oscar64/NativeCodeGenerator.h b/oscar64/NativeCodeGenerator.h new file mode 100644 index 0000000..04b6be7 --- /dev/null +++ b/oscar64/NativeCodeGenerator.h @@ -0,0 +1,71 @@ +#pragma once + +#include "ByteCodeGenerator.h" +#include "Assembler.h" + +class NativeCodeProcedure; +class NativeCodeBasicBlock; + +class NativeCodeInstruction +{ +public: + NativeCodeInstruction(AsmInsType type, AsmInsMode mode); + + AsmInsType mType; + AsmInsMode mMode; + + int mAddress, mVarIndex; + bool mGlobal; + + void Assemble(ByteCodeGenerator* generator, NativeCodeBasicBlock* block); +}; + +class NativeCodeBasicBlock +{ +public: + NativeCodeBasicBlock(void); + ~NativeCodeBasicBlock(void); + + DynamicArray mCode; + int mIndex; + + NativeCodeBasicBlock* mTrueJump, * mFalseJump; + AsmInsType mBranch; + + GrowingArray mIns; + GrowingArray mRelocations; + + int mOffset, mSize; + bool mPlaced, mCopied, mKnownShortBranch, mBypassed, mAssembled; + + int PutBranch(ByteCodeGenerator* generator, AsmInsType code, int offset); + + NativeCodeBasicBlock* BypassEmptyBlocks(void); + void CalculateOffset(int& total); + void CopyCode(ByteCodeGenerator* generator, uint8* target); + + void Assemble(ByteCodeGenerator* generator); + void Compile(InterCodeProcedure* iproc, NativeCodeProcedure* proc, InterCodeBasicBlock* block); + void Close(NativeCodeBasicBlock* trueJump, NativeCodeBasicBlock* falseJump, AsmInsType branch); + + void PutByte(uint8 code); + void PutWord(uint16 code); + +}; + +class NativeCodeProcedure +{ + public: + NativeCodeProcedure(void); + ~NativeCodeProcedure(void); + + NativeCodeBasicBlock* entryBlock, * exitBlock; + NativeCodeBasicBlock** tblocks; + + int mProgStart, mProgSize; + + void Compile(ByteCodeGenerator* generator, InterCodeProcedure* proc); + NativeCodeBasicBlock* CompileBlock(InterCodeProcedure* iproc, InterCodeBasicBlock* block); + +}; + diff --git a/oscar64/Parser.cpp b/oscar64/Parser.cpp index c1ff539..d4c002b 100644 --- a/oscar64/Parser.cpp +++ b/oscar64/Parser.cpp @@ -469,7 +469,8 @@ Declaration * Parser::CopyConstantInitializer(int offset, Declaration* dtype, Ex else { Declaration* ndec = new Declaration(dec->mLocation, DT_CONST_DATA); - ndec->mValue = exp; + ndec->mData = dec->mData; + ndec->mSize = dec->mSize; ndec->mBase = dtype; dec = ndec; } diff --git a/oscar64/oscar64.vcxproj b/oscar64/oscar64.vcxproj index d3edcc7..556f9e7 100644 --- a/oscar64/oscar64.vcxproj +++ b/oscar64/oscar64.vcxproj @@ -151,6 +151,7 @@ + @@ -171,6 +172,7 @@ + diff --git a/oscar64/oscar64.vcxproj.filters b/oscar64/oscar64.vcxproj.filters index b8231f3..effd814 100644 --- a/oscar64/oscar64.vcxproj.filters +++ b/oscar64/oscar64.vcxproj.filters @@ -60,6 +60,9 @@ Source Files + + Source Files + @@ -119,6 +122,9 @@ Header Files + + Header Files +