From 37828f2baf09f088ffafadf10e83020af84f3aff Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Tue, 14 Sep 2021 21:38:24 +0200 Subject: [PATCH] More byte and long preparations --- autotest/recursiontest.c | 3 +- include/crt.c | 87 ++++++++++++++++ include/crt.h | 6 ++ oscar64/ByteCodeGenerator.cpp | 178 +++++++++++++++++++++----------- oscar64/ByteCodeGenerator.h | 6 ++ oscar64/Emulator.cpp | 17 ++- oscar64/InterCode.cpp | 39 +++++-- oscar64/InterCode.h | 2 + oscar64/InterCodeGenerator.cpp | 8 +- oscar64/NativeCodeGenerator.cpp | 60 +++++++---- 10 files changed, 311 insertions(+), 95 deletions(-) diff --git a/autotest/recursiontest.c b/autotest/recursiontest.c index b0458281..28801f27 100644 --- a/autotest/recursiontest.c +++ b/autotest/recursiontest.c @@ -1,6 +1,7 @@ #include #include #include +#include int fib(int a) { @@ -75,7 +76,7 @@ void btest(void) } collect(tree, buff)[0] = 0; - + assert(strcmp(buff, " DEHLLLOORW") == 0); } diff --git a/include/crt.c b/include/crt.c index 772c12b6..dc72b8be 100644 --- a/include/crt.c +++ b/include/crt.c @@ -283,6 +283,19 @@ __asm inp_native } #pragma bytecode(BC_NATIVE, inp_native) + +__asm inp_const_8 +{ + lda (ip), y + tax + iny + lda (ip), y + sta $00, x + iny + jmp startup.exec +} + +#pragma bytecode(BC_CONST_8, inp_const_8) __asm inp_const_p8 { @@ -351,6 +364,32 @@ __asm inp_const_32 } #pragma bytecode(BC_CONST_32, inp_const_32) + +__asm inp_load_reg_8 +{ + lda (ip), y + tax + iny + lda $00, x + sta accu + lda #0 + sta accu + 1 + jmp startup.exec +} + +#pragma bytecode(BC_LOAD_REG_8, inp_load_reg_8) + +__asm inp_store_reg_8 +{ + lda (ip), y + tax + iny + lda accu + sta $00, x + jmp startup.exec +} + +#pragma bytecode(BC_STORE_REG_8, inp_store_reg_8) __asm inp_load_reg_16 { @@ -445,6 +484,37 @@ __asm inp_addr_reg #pragma bytecode(BC_ADDR_REG, inp_addr_reg) +__asm inp_load_abs_8 +{ + lda (ip), y + sta addr + iny + lda (ip), y + sta addr + 1 + iny + lda (ip), y + tax + sty tmpy + ldy #0 +L0: + lda (addr), y + sta $00, x + ldy tmpy + iny + jmp startup.exec +inp_load_addr_8: + lda (ip), y + tax + iny + lda (ip), y + sty tmpy + tay + jmp L0 +} + +#pragma bytecode(BC_LOAD_ABS_8, inp_load_abs_8) +#pragma bytecode(BC_LOAD_ADDR_8, inp_load_abs_8.inp_load_addr_8) + __asm inp_load_abs_u8 { lda (ip), y @@ -761,6 +831,23 @@ __asm inp_load_local_32 } #pragma bytecode(BC_LOAD_LOCAL_32, inp_load_local_32) + +__asm inp_load_local_8 +{ + lda (ip), y + tax + iny + lda (ip), y + iny + sty tmpy + tay + lda (fp), y + sta $00, x + ldy tmpy + jmp startup.exec +} + +#pragma bytecode(BC_LOAD_LOCAL_8, inp_load_local_8) __asm inp_load_local_u8 { diff --git a/include/crt.h b/include/crt.h index f86df3c9..4157044e 100644 --- a/include/crt.h +++ b/include/crt.h @@ -19,17 +19,21 @@ enum ByteCode BC_NOP, BC_EXIT, + BC_CONST_8, BC_CONST_P8, BC_CONST_N8, BC_CONST_16, BC_CONST_32, + BC_LOAD_REG_8, + BC_STORE_REG_8, BC_LOAD_REG_16, BC_STORE_REG_16, BC_ADDR_REG, BC_LOAD_REG_32, BC_STORE_REG_32, + BC_LOAD_ABS_8, BC_LOAD_ABS_U8, BC_LOAD_ABS_I8, BC_LOAD_ABS_16, @@ -41,6 +45,7 @@ enum ByteCode BC_LEA_ABS, + BC_LOAD_LOCAL_8, BC_LOAD_LOCAL_U8, BC_LOAD_LOCAL_I8, BC_LOAD_LOCAL_16, @@ -56,6 +61,7 @@ enum ByteCode BC_STORE_FRAME_16, BC_STORE_FRAME_32, + BC_LOAD_ADDR_8, BC_LOAD_ADDR_U8, BC_LOAD_ADDR_I8, BC_LOAD_ADDR_16, diff --git a/oscar64/ByteCodeGenerator.cpp b/oscar64/ByteCodeGenerator.cpp index 46e5ef20..511f1dea 100644 --- a/oscar64/ByteCodeGenerator.cpp +++ b/oscar64/ByteCodeGenerator.cpp @@ -1,6 +1,17 @@ #include "ByteCodeGenerator.h" #include "Assembler.h" +static ByteCode StoreTypedTmpCodes[] = { + BC_NOP, + BC_STORE_REG_8, + BC_STORE_REG_8, + BC_STORE_REG_16, + BC_STORE_REG_32, + BC_STORE_REG_32, + BC_STORE_REG_16 +}; + + static ByteCode InvertBranchCondition(ByteCode code) { switch (code) @@ -67,13 +78,13 @@ bool ByteCodeInstruction::LoadsRegister(uint32 reg) const { if (mRegister == reg) { - if (mCode >= BC_LOAD_ABS_U8 && mCode <= BC_LOAD_ABS_32) + if (mCode >= BC_LOAD_ABS_8 && mCode <= BC_LOAD_ABS_32) return true; - if (mCode >= BC_LOAD_LOCAL_U8 && mCode <= BC_LOAD_LOCAL_32) + if (mCode >= BC_LOAD_LOCAL_8 && mCode <= BC_LOAD_LOCAL_32) return true; - if (mCode >= BC_LOAD_ADDR_U8 && mCode <= BC_LOAD_ADDR_32) + if (mCode >= BC_LOAD_ADDR_8 && mCode <= BC_LOAD_ADDR_32) return true; - if (mCode >= BC_CONST_P8 && mCode <= BC_CONST_32) + if (mCode >= BC_CONST_8 && mCode <= BC_CONST_32) return true; if (mCode == BC_LEA_ABS || mCode == BC_LEA_LOCAL) return true; @@ -115,15 +126,15 @@ bool ByteCodeInstruction::ChangesRegister(uint32 reg) const { if (mRegister == reg) { - if (mCode == BC_STORE_REG_16 || mCode == BC_STORE_REG_32) + if (mCode == BC_STORE_REG_8 || mCode == BC_STORE_REG_16 || mCode == BC_STORE_REG_32) return true; - if (mCode >= BC_LOAD_ABS_U8 && mCode <= BC_LOAD_ABS_32) + if (mCode >= BC_LOAD_ABS_8 && mCode <= BC_LOAD_ABS_32) return true; - if (mCode >= BC_LOAD_LOCAL_U8 && mCode <= BC_LOAD_LOCAL_32) + if (mCode >= BC_LOAD_LOCAL_8 && mCode <= BC_LOAD_LOCAL_32) return true; - if (mCode >= BC_LOAD_ADDR_U8 && mCode <= BC_LOAD_ADDR_32) + if (mCode >= BC_LOAD_ADDR_8 && mCode <= BC_LOAD_ADDR_32) return true; - if (mCode >= BC_CONST_P8 && mCode <= BC_CONST_32) + if (mCode >= BC_CONST_8 && mCode <= BC_CONST_32) return true; if (mCode == BC_LEA_ABS || mCode == BC_LEA_LOCAL) return true; @@ -135,7 +146,7 @@ bool ByteCodeInstruction::ChangesRegister(uint32 reg) const if (reg == BC_REG_ACCU) { - if (mCode == BC_LOAD_REG_16 || mCode == BC_LOAD_REG_32) + if (mCode == BC_LOAD_REG_8 || mCode == BC_LOAD_REG_16 || mCode == BC_LOAD_REG_32) return true; if (mCode >= BC_BINOP_ADDR_16 && mCode <= BC_BINOP_SHRR_I16) return true; @@ -159,7 +170,7 @@ bool ByteCodeInstruction::ChangesRegister(uint32 reg) const { if (mCode == BC_ADDR_REG) return true; - if (mCode >= BC_LOAD_ABS_U8 && mCode <= BC_STORE_ABS_32) + if (mCode >= BC_LOAD_ABS_8 && mCode <= BC_STORE_ABS_32) return true; if (mCode == BC_JSR || mCode == BC_CALL) return true; @@ -178,6 +189,9 @@ void ByteCodeInstruction::Assemble(ByteCodeGenerator* generator, ByteCodeBasicBl block->PutCode(generator, mCode); break; + case BC_CONST_8: + block->PutCode(generator, BC_CONST_8); block->PutByte(mRegister); block->PutByte(uint8(mValue)); + break; case BC_CONST_P8: case BC_CONST_N8: case BC_CONST_16: @@ -213,7 +227,8 @@ void ByteCodeInstruction::Assemble(ByteCodeGenerator* generator, ByteCodeBasicBl block->PutCode(generator, BC_CONST_32); block->PutByte(mRegister); block->PutDWord(uint32(mValue)); break; - + case BC_LOAD_REG_8: + case BC_STORE_REG_8: case BC_LOAD_REG_16: case BC_STORE_REG_16: case BC_LOAD_REG_32: @@ -223,6 +238,7 @@ void ByteCodeInstruction::Assemble(ByteCodeGenerator* generator, ByteCodeBasicBl block->PutByte(mRegister); break; + case BC_LOAD_ABS_8: case BC_LOAD_ABS_U8: case BC_LOAD_ABS_I8: case BC_LOAD_ABS_16: @@ -269,6 +285,7 @@ void ByteCodeInstruction::Assemble(ByteCodeGenerator* generator, ByteCodeBasicBl block->PutWord(uint16(mValue)); break; + case BC_LOAD_LOCAL_8: case BC_LOAD_LOCAL_U8: case BC_LOAD_LOCAL_I8: case BC_LOAD_LOCAL_16: @@ -430,6 +447,7 @@ void ByteCodeInstruction::Assemble(ByteCodeGenerator* generator, ByteCodeBasicBl block->PutWord(0); } break; + case BC_LOAD_ADDR_8: case BC_LOAD_ADDR_U8: case BC_LOAD_ADDR_I8: case BC_LOAD_ADDR_16: @@ -614,6 +632,13 @@ void ByteCodeBasicBlock::LoadConstant(InterCodeProcedure* proc, const InterInstr mIns.Push(bins); } } + else if (ins.mTType == IT_BOOL || ins.mTType == IT_INT8) + { + ByteCodeInstruction bins(BC_CONST_8); + bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins.mTTemp]; + bins.mValue = ins.mIntValue; + mIns.Push(bins); + } else { ByteCodeInstruction bins(BC_CONST_16); @@ -1376,7 +1401,7 @@ void ByteCodeBasicBlock::LoadDirectValue(InterCodeProcedure* proc, const InterIn { if (ins.mMemory == IM_GLOBAL) { - ByteCodeInstruction bins(BC_LOAD_ABS_U8); + ByteCodeInstruction bins((ins.mTType == IT_BOOL || ins.mTType == IT_INT8) ? BC_LOAD_ABS_8 : BC_LOAD_ABS_U8); bins.mRelocate = true; bins.mVIndex = ins.mVarIndex; bins.mValue = ins.mSIntConst[0]; @@ -1385,7 +1410,7 @@ void ByteCodeBasicBlock::LoadDirectValue(InterCodeProcedure* proc, const InterIn } else if (ins.mMemory == IM_ABSOLUTE) { - ByteCodeInstruction bins(BC_LOAD_ABS_U8); + ByteCodeInstruction bins((ins.mTType == IT_BOOL || ins.mTType == IT_INT8) ? BC_LOAD_ABS_8 : BC_LOAD_ABS_U8); bins.mValue = ins.mSIntConst[0]; bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins.mTTemp]; mIns.Push(bins); @@ -1400,7 +1425,7 @@ void ByteCodeBasicBlock::LoadDirectValue(InterCodeProcedure* proc, const InterIn if (index <= 255) { - ByteCodeInstruction bins(BC_LOAD_LOCAL_U8); + ByteCodeInstruction bins((ins.mTType == IT_BOOL || ins.mTType == IT_INT8) ? BC_LOAD_LOCAL_8 : BC_LOAD_LOCAL_U8); bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins.mTTemp]; bins.mValue = index; mIns.Push(bins); @@ -1411,7 +1436,7 @@ void ByteCodeBasicBlock::LoadDirectValue(InterCodeProcedure* proc, const InterIn lins.mRegister = BC_REG_ADDR; lins.mValue = index; mIns.Push(lins); - ByteCodeInstruction bins(BC_LOAD_ADDR_U8); + ByteCodeInstruction bins((ins.mTType == IT_BOOL || ins.mTType == IT_INT8) ? BC_LOAD_LOCAL_8 : BC_LOAD_ADDR_U8); bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins.mTTemp]; bins.mValue = 0; mIns.Push(bins); @@ -1476,7 +1501,7 @@ void ByteCodeBasicBlock::LoadDirectValue(InterCodeProcedure* proc, const InterIn if (ins.mOperandSize == 1) { - ByteCodeInstruction bins(BC_LOAD_ADDR_U8); + ByteCodeInstruction bins((ins.mTType == IT_BOOL || ins.mTType == IT_INT8) ? BC_LOAD_ADDR_8 : BC_LOAD_ADDR_U8); bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins.mTTemp]; bins.mValue = ins.mSIntConst[0]; mIns.Push(bins); @@ -1534,7 +1559,6 @@ void ByteCodeBasicBlock::LoadEffectiveAddress(InterCodeProcedure* proc, const In mIns.Push(sins); } } - void ByteCodeBasicBlock::CallFunction(InterCodeProcedure* proc, const InterInstruction& ins) { if (ins.mSTemp[0] < 0) @@ -1560,18 +1584,9 @@ void ByteCodeBasicBlock::CallFunction(InterCodeProcedure* proc, const InterInstr if (ins.mTTemp >= 0) { - if (ins.mTType == IT_FLOAT) - { - ByteCodeInstruction bins(BC_STORE_REG_32); - bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins.mTTemp]; - mIns.Push(bins); - } - else - { - ByteCodeInstruction bins(BC_STORE_REG_16); - bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins.mTTemp]; - mIns.Push(bins); - } + ByteCodeInstruction bins(StoreTypedTmpCodes[ins.mTType]); + bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins.mTTemp]; + mIns.Push(bins); } } @@ -1589,18 +1604,9 @@ void ByteCodeBasicBlock::CallAssembler(InterCodeProcedure* proc, const InterInst if (ins.mTTemp >= 0) { - if (ins.mTType == IT_FLOAT) - { - ByteCodeInstruction bins(BC_STORE_REG_32); - bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins.mTTemp]; - mIns.Push(bins); - } - else - { - ByteCodeInstruction bins(BC_STORE_REG_16); - bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins.mTTemp]; - mIns.Push(bins); - } + ByteCodeInstruction bins(StoreTypedTmpCodes[ins.mTType]); + bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins.mTTemp]; + mIns.Push(bins); } } @@ -1851,7 +1857,7 @@ void ByteCodeBasicBlock::NumericConversion(InterCodeProcedure* proc, const Inter } else { - ByteCodeInstruction lins(BC_LOAD_REG_16); + ByteCodeInstruction lins(BC_LOAD_REG_8); lins.mRegister = BC_REG_TMP + proc->mTempOffset[ins.mSTemp[0]]; lins.mRegisterFinal = ins.mSFinal[0]; mIns.Push(lins); @@ -1877,16 +1883,11 @@ void ByteCodeBasicBlock::NumericConversion(InterCodeProcedure* proc, const Inter } else { - ByteCodeInstruction lins(BC_LOAD_REG_16); + ByteCodeInstruction lins(BC_LOAD_REG_8); lins.mRegister = BC_REG_TMP + proc->mTempOffset[ins.mSTemp[0]]; lins.mRegisterFinal = ins.mSFinal[0]; mIns.Push(lins); - ByteCodeInstruction cins(BC_BINOP_ANDI_16); - cins.mRegister = BC_REG_ACCU; - cins.mValue = 0x00ff; - mIns.Push(cins); - ByteCodeInstruction sins(BC_STORE_REG_16); sins.mRegister = BC_REG_TMP + proc->mTempOffset[ins.mTTemp]; mIns.Push(sins); @@ -2348,13 +2349,43 @@ void ByteCodeBasicBlock::Compile(InterCodeProcedure* iproc, ByteCodeProcedure* p { if (ins.mSTemp[0] != ins.mTTemp) { - ByteCodeInstruction lins(BC_LOAD_REG_16); - lins.mRegister = BC_REG_TMP + iproc->mTempOffset[ins.mSTemp[0]]; - lins.mRegisterFinal = ins.mSFinal[0]; - mIns.Push(lins); - ByteCodeInstruction sins(BC_STORE_REG_16); - sins.mRegister = BC_REG_TMP + iproc->mTempOffset[ins.mTTemp]; - mIns.Push(sins); + switch (ins.mTType) + { + case IT_BOOL: + case IT_INT8: + { + ByteCodeInstruction lins(BC_LOAD_REG_8); + lins.mRegister = BC_REG_TMP + iproc->mTempOffset[ins.mSTemp[0]]; + lins.mRegisterFinal = ins.mSFinal[0]; + mIns.Push(lins); + ByteCodeInstruction sins(BC_STORE_REG_8); + sins.mRegister = BC_REG_TMP + iproc->mTempOffset[ins.mTTemp]; + mIns.Push(sins); + } break; + case IT_INT16: + case IT_POINTER: + { + ByteCodeInstruction lins(BC_LOAD_REG_16); + lins.mRegister = BC_REG_TMP + iproc->mTempOffset[ins.mSTemp[0]]; + lins.mRegisterFinal = ins.mSFinal[0]; + mIns.Push(lins); + ByteCodeInstruction sins(BC_STORE_REG_16); + sins.mRegister = BC_REG_TMP + iproc->mTempOffset[ins.mTTemp]; + mIns.Push(sins); + } break; + case IT_INT32: + case IT_FLOAT: + { + ByteCodeInstruction lins(BC_LOAD_REG_32); + lins.mRegister = BC_REG_TMP + iproc->mTempOffset[ins.mSTemp[0]]; + lins.mRegisterFinal = ins.mSFinal[0]; + mIns.Push(lins); + ByteCodeInstruction sins(BC_STORE_REG_32); + sins.mRegister = BC_REG_TMP + iproc->mTempOffset[ins.mTTemp]; + mIns.Push(sins); + } break; + + } } } break; case IC_BINARY_OPERATOR: @@ -2406,7 +2437,7 @@ void ByteCodeBasicBlock::Compile(InterCodeProcedure* iproc, ByteCodeProcedure* p ByteCode code = RelationalOperator(iproc, ins); ByteCodeInstruction bins(ByteCode(code - BC_BRANCHS_EQ + BC_SET_EQ)); mIns.Push(bins); - ByteCodeInstruction sins(BC_STORE_REG_16); + ByteCodeInstruction sins(StoreTypedTmpCodes[ins.mTType]); sins.mRegister = BC_REG_TMP + iproc->mTempOffset[ins.mTTemp]; mIns.Push(sins); } @@ -2424,7 +2455,7 @@ void ByteCodeBasicBlock::Compile(InterCodeProcedure* iproc, ByteCodeProcedure* p } else { - ByteCodeInstruction lins(BC_LOAD_REG_16); + ByteCodeInstruction lins(InterTypeSize[ins.mSType[0]] == 1 ? BC_LOAD_REG_8 : BC_LOAD_REG_16); lins.mRegister = BC_REG_TMP + iproc->mTempOffset[ins.mSTemp[0]]; lins.mRegisterFinal = ins.mSFinal[0]; mIns.Push(lins); @@ -2460,7 +2491,7 @@ void ByteCodeBasicBlock::Compile(InterCodeProcedure* iproc, ByteCodeProcedure* p } else { - ByteCodeInstruction lins(BC_LOAD_REG_16); + ByteCodeInstruction lins(InterTypeSize[ins.mSType[0]] == 1 ? BC_LOAD_REG_8 : BC_LOAD_REG_16); lins.mRegister = BC_REG_TMP + iproc->mTempOffset[ins.mSTemp[0]]; lins.mRegisterFinal = ins.mSFinal[0]; mIns.Push(lins); @@ -3060,7 +3091,10 @@ void ByteCodeProcedure::Disassemble(FILE * file, ByteCodeGenerator * generator, fprintf(file, "%s:\n", proc->mIdent->mString); char tbuffer[10]; - +#if 0 + for (int i = 0; i < proc->mTemporaries.Size(); i++) + printf("T%d = $%.2x\n", i, BC_REG_TMP + proc->mTempOffset[i]); +#endif int i = 0; while (i < mProgSize) { @@ -3078,6 +3112,10 @@ void ByteCodeProcedure::Disassemble(FILE * file, ByteCodeGenerator * generator, fprintf(file, "EXIT"); break; + case BC_CONST_8: + fprintf(file, "MOVB\t%s, #%d", TempName(generator->mMemory[mProgStart + i], tbuffer, proc), generator->mMemory[mProgStart + i + 1]); + i += 2; + break; case BC_CONST_P8: fprintf(file, "MOV\t%s, #%d", TempName(generator->mMemory[mProgStart + i], tbuffer, proc), generator->mMemory[mProgStart + i + 1]); i += 2; @@ -3095,6 +3133,14 @@ void ByteCodeProcedure::Disassemble(FILE * file, ByteCodeGenerator * generator, i += 5; break; + case BC_LOAD_REG_8: + fprintf(file, "MOVB\tACCU, %s", TempName(generator->mMemory[mProgStart + i], tbuffer, proc)); + i += 1; + break; + case BC_STORE_REG_8: + fprintf(file, "MOVB\t%s, ACCU", TempName(generator->mMemory[mProgStart + i], tbuffer, proc)); + i += 1; + break; case BC_LOAD_REG_16: fprintf(file, "MOV\tACCU, %s", TempName(generator->mMemory[mProgStart + i], tbuffer, proc)); i += 1; @@ -3116,6 +3162,10 @@ void ByteCodeProcedure::Disassemble(FILE * file, ByteCodeGenerator * generator, i += 1; break; + case BC_LOAD_ABS_8: + fprintf(file, "MOVUB\t%s, $%04x", TempName(generator->mMemory[mProgStart + i + 2], tbuffer, proc), uint16(generator->mMemory[mProgStart + i + 0] + 256 * generator->mMemory[mProgStart + i + 1])); + i += 3; + break; case BC_LOAD_ABS_U8: fprintf(file, "MOVUB\t%s, $%04x", TempName(generator->mMemory[mProgStart + i + 2], tbuffer, proc), uint16(generator->mMemory[mProgStart + i + 0] + 256 * generator->mMemory[mProgStart + i + 1])); i += 3; @@ -3151,6 +3201,10 @@ void ByteCodeProcedure::Disassemble(FILE * file, ByteCodeGenerator * generator, i += 3; break; + case BC_LOAD_LOCAL_8: + fprintf(file, "MOVB\t%s, %d(FP)", TempName(generator->mMemory[mProgStart + i + 0], tbuffer, proc), generator->mMemory[mProgStart + i + 1]); + i += 2; + break; case BC_LOAD_LOCAL_U8: fprintf(file, "MOVUB\t%s, %d(FP)", TempName(generator->mMemory[mProgStart + i + 0], tbuffer, proc), generator->mMemory[mProgStart + i + 1]); i += 2; @@ -3477,6 +3531,10 @@ void ByteCodeProcedure::Disassemble(FILE * file, ByteCodeGenerator * generator, i += 2; break; + case BC_LOAD_ADDR_8: + fprintf(file, "MOVB\t%s, (ADDR + %d)", TempName(generator->mMemory[mProgStart + i + 0], tbuffer, proc), generator->mMemory[mProgStart + i + 1]); + i += 2; + break; case BC_LOAD_ADDR_U8: fprintf(file, "MOVUB\t%s, (ADDR + %d)", TempName(generator->mMemory[mProgStart + i + 0], tbuffer, proc), generator->mMemory[mProgStart + i + 1]); i += 2; diff --git a/oscar64/ByteCodeGenerator.h b/oscar64/ByteCodeGenerator.h index 0bd4ff70..e3ee0a7a 100644 --- a/oscar64/ByteCodeGenerator.h +++ b/oscar64/ByteCodeGenerator.h @@ -8,17 +8,21 @@ enum ByteCode BC_NOP, BC_EXIT, + BC_CONST_8, BC_CONST_P8, BC_CONST_N8, BC_CONST_16, BC_CONST_32, + BC_LOAD_REG_8, + BC_STORE_REG_8, BC_LOAD_REG_16, BC_STORE_REG_16, BC_ADDR_REG, BC_LOAD_REG_32, BC_STORE_REG_32, + BC_LOAD_ABS_8, BC_LOAD_ABS_U8, BC_LOAD_ABS_I8, BC_LOAD_ABS_16, @@ -30,6 +34,7 @@ enum ByteCode BC_LEA_ABS, + BC_LOAD_LOCAL_8, BC_LOAD_LOCAL_U8, BC_LOAD_LOCAL_I8, BC_LOAD_LOCAL_16, @@ -45,6 +50,7 @@ enum ByteCode BC_STORE_FRAME_16, BC_STORE_FRAME_32, + BC_LOAD_ADDR_8, BC_LOAD_ADDR_U8, BC_LOAD_ADDR_I8, BC_LOAD_ADDR_16, diff --git a/oscar64/Emulator.cpp b/oscar64/Emulator.cpp index 447a91ba..2e16ead6 100644 --- a/oscar64/Emulator.cpp +++ b/oscar64/Emulator.cpp @@ -447,7 +447,7 @@ bool Emulator::EmulateInstruction(AsmInsType type, AsmInsMode mode, int addr, in return false; break; } - + return true; } @@ -582,15 +582,26 @@ int Emulator::Emulate(int startIP) int accu = mMemory[BC_REG_ACCU] + 256 * mMemory[BC_REG_ACCU + 1]; int ptr = mMemory[BC_REG_ADDR] + 256 * mMemory[BC_REG_ADDR + 1]; int sp = mMemory[BC_REG_STACK] + 256 * mMemory[BC_REG_STACK + 1]; - printf("%04x (A:%04x P:%04x S:%04x) %04x %04x %04x %04x %04x %04x %04x %04x\n", addr, accu, ptr, sp, + printf("%04x (A:%04x P:%04x S:%04x) %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x\n", addr, accu, ptr, sp, mMemory[BC_REG_TMP + 0] + 256 * mMemory[BC_REG_TMP + 1], mMemory[BC_REG_TMP + 2] + 256 * mMemory[BC_REG_TMP + 3], mMemory[BC_REG_TMP + 4] + 256 * mMemory[BC_REG_TMP + 5], mMemory[BC_REG_TMP + 6] + 256 * mMemory[BC_REG_TMP + 7], + mMemory[BC_REG_TMP + 8] + 256 * mMemory[BC_REG_TMP + 9], mMemory[BC_REG_TMP + 10] + 256 * mMemory[BC_REG_TMP + 11], mMemory[BC_REG_TMP + 12] + 256 * mMemory[BC_REG_TMP + 13], - mMemory[BC_REG_TMP + 14] + 256 * mMemory[BC_REG_TMP + 15] + mMemory[BC_REG_TMP + 14] + 256 * mMemory[BC_REG_TMP + 15], + + mMemory[BC_REG_TMP + 16] + 256 * mMemory[BC_REG_TMP + 17], + mMemory[BC_REG_TMP + 18] + 256 * mMemory[BC_REG_TMP + 19], + mMemory[BC_REG_TMP + 20] + 256 * mMemory[BC_REG_TMP + 21], + mMemory[BC_REG_TMP + 22] + 256 * mMemory[BC_REG_TMP + 23], + + mMemory[BC_REG_TMP + 24] + 256 * mMemory[BC_REG_TMP + 25], + mMemory[BC_REG_TMP + 26] + 256 * mMemory[BC_REG_TMP + 27], + mMemory[BC_REG_TMP + 28] + 256 * mMemory[BC_REG_TMP + 29], + mMemory[BC_REG_TMP + 30] + 256 * mMemory[BC_REG_TMP + 31] ); } diff --git a/oscar64/InterCode.cpp b/oscar64/InterCode.cpp index 3c623db7..e4410dfb 100644 --- a/oscar64/InterCode.cpp +++ b/oscar64/InterCode.cpp @@ -5,6 +5,16 @@ #include #include +int InterTypeSize[] = { + 0, + 1, + 1, + 2, + 4, + 4, + 2 +}; + ValueSet::ValueSet(void) { mSize = 32; @@ -1448,17 +1458,21 @@ void InterInstruction::CollectSimpleLocals(FastNumberSet& complexLocals, FastNum case IC_LOAD: if (mMemory == IM_LOCAL && mSTemp[0] < 0) { - localTypes[mVarIndex] = mTType; - if (mOperandSize == 2) + if (localTypes[mVarIndex] == IT_NONE || localTypes[mVarIndex] == mTType) + { + localTypes[mVarIndex] = mTType; simpleLocals += mVarIndex; + } else complexLocals += mVarIndex; } else if (mMemory == IM_PARAM && mSTemp[0] < 0) { - paramTypes[mVarIndex] = mTType; - if (mOperandSize == 2) + if (paramTypes[mVarIndex] == IT_NONE || paramTypes[mVarIndex] == mTType) + { + paramTypes[mVarIndex] = mTType; simpleParams += mVarIndex; + } else complexParams += mVarIndex; } @@ -1466,17 +1480,21 @@ void InterInstruction::CollectSimpleLocals(FastNumberSet& complexLocals, FastNum case IC_STORE: if (mMemory == IM_LOCAL && mSTemp[1] < 0) { - localTypes[mVarIndex] = mSType[0]; - if (mOperandSize == 2) + if (localTypes[mVarIndex] == IT_NONE || localTypes[mVarIndex] == mSType[0]) + { + localTypes[mVarIndex] = mSType[0]; simpleLocals += mVarIndex; + } else complexLocals += mVarIndex; } else if (mMemory == IM_PARAM && mSTemp[1] < 0) { - paramTypes[mVarIndex] = mSType[0]; - if (mOperandSize == 2) + if (paramTypes[mVarIndex] == IT_NONE || paramTypes[mVarIndex] == mSType[0]) + { + localTypes[mVarIndex] = mSType[0]; simpleParams += mVarIndex; + } else complexParams += mVarIndex; } @@ -1518,6 +1536,7 @@ void InterInstruction::SimpleLocalToTemp(int vindex, int temp) { mCode = IC_CONSTANT; mIntValue = mSIntConst[0]; + mFloatValue = mSFloatConst[0]; } else { @@ -3846,7 +3865,7 @@ void InterCodeProcedure::ReduceTemporaries(void) for (j = numFixedTemporaries; j < numTemps; j++) { - if (mRenameTable[j] >= 0 && (collisionSet[i][j] || !TypeCompatible(mTemporaries[j], mTemporaries[i]))) + if (mRenameTable[j] >= 0 && (collisionSet[i][j] || InterTypeSize[mTemporaries[j]] != InterTypeSize[mTemporaries[i]])) { usedTemps += mRenameTable[j]; } @@ -3890,7 +3909,7 @@ void InterCodeProcedure::ReduceTemporaries(void) for (int i = 0; i < mTemporaries.Size(); i++) { - int size = mTemporaries[i] == IT_FLOAT ? 4 : 2; + int size = InterTypeSize[mTemporaries[i]]; if (callerSavedTemps + size <= 16 && !callerSaved[i]) { diff --git a/oscar64/InterCode.h b/oscar64/InterCode.h index 5474ce0e..d858e7e4 100644 --- a/oscar64/InterCode.h +++ b/oscar64/InterCode.h @@ -45,6 +45,8 @@ enum InterType IT_POINTER }; +extern int InterTypeSize[]; + enum InterMemory { IM_NONE, diff --git a/oscar64/InterCodeGenerator.cpp b/oscar64/InterCodeGenerator.cpp index 33e96e07..f236b14c 100644 --- a/oscar64/InterCodeGenerator.cpp +++ b/oscar64/InterCodeGenerator.cpp @@ -824,6 +824,8 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* if (!vr.mType->IsIntegerType()) mErrors->Error(exp->mLocation, "Index operand is not integral number"); + vr = CoerceType(proc, block, vr, TheSignedIntTypeDeclaration); + InterInstruction cins; cins.mCode = IC_CONSTANT; cins.mIntValue = vl.mType->mBase->mSize; @@ -1442,6 +1444,10 @@ InterCodeGenerator::ExValue InterCodeGenerator::TranslateExpression(Declaration* mErrors->Error(texp->mLocation, "Cannot assign incompatible types"); vr = CoerceType(proc, block, vr, pdec->mBase); } + else if (vr.mType->IsIntegerType() && vr.mType->mSize < 2) + { + vr = CoerceType(proc, block, vr, TheSignedIntTypeDeclaration); + } InterInstruction wins; wins.mCode = IC_STORE; @@ -2274,7 +2280,7 @@ void InterCodeGenerator::TranslateLogic(Declaration* procType, InterCodeProcedur InterInstruction ins; ins.mCode = IC_BRANCH; - ins.mSType[0] = IT_BOOL; + ins.mSType[0] = InterTypeOf(vr.mType); ins.mSTemp[0] = vr.mTemp; block->Append(ins); diff --git a/oscar64/NativeCodeGenerator.cpp b/oscar64/NativeCodeGenerator.cpp index e6cd7bd1..46e3a3dc 100644 --- a/oscar64/NativeCodeGenerator.cpp +++ b/oscar64/NativeCodeGenerator.cpp @@ -1373,8 +1373,11 @@ void NativeCodeBasicBlock::LoadConstantToReg(InterCodeProcedure * proc, const In { mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, ins.mIntValue & 0xff)); mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, reg)); - mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, (ins.mIntValue >> 8) & 0xff)); - mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, reg + 1)); + if (InterTypeSize[ins.mTType] > 1) + { + mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, (ins.mIntValue >> 8) & 0xff)); + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, reg + 1)); + } } } @@ -2214,9 +2217,13 @@ void NativeCodeBasicBlock::LoadValueToReg(InterCodeProcedure* proc, const InterI mIns.Push(*ainsl); } mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, reg)); - mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0)); - if (ainsh) mIns.Push(*ainsh); - mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, reg + 1)); + + if (InterTypeSize[ins.mTType] > 1) + { + mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0)); + if (ainsh) mIns.Push(*ainsh); + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, reg + 1)); + } } else if (ins.mOperandSize == 2) { @@ -2297,9 +2304,12 @@ void NativeCodeBasicBlock::LoadValueToReg(InterCodeProcedure* proc, const InterI mIns.Push(*ainsl); } mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, reg)); - mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0)); - if (ainsh) mIns.Push(*ainsh); - mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, reg + 1)); + if (InterTypeSize[ins.mTType] > 1) + { + mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0)); + if (ainsh) mIns.Push(*ainsh); + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, reg + 1)); + } } else if (ins.mOperandSize == 2) { @@ -2315,17 +2325,23 @@ void NativeCodeBasicBlock::LoadValueToReg(InterCodeProcedure* proc, const InterI mIns.Push(NativeCodeInstruction(ASMIT_SEC, ASMIM_IMPLIED)); mIns.Push(*ainsl); } - if (reg == src) - mIns.Push(NativeCodeInstruction(ASMIT_TAX, ASMIM_IMPLIED)); + + if (InterTypeSize[ins.mTType] > 1) + { + if (reg == src) + mIns.Push(NativeCodeInstruction(ASMIT_TAX, ASMIM_IMPLIED)); + else + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, reg)); + + mIns.Push(NativeCodeInstruction(ASMIT_INY, ASMIM_IMPLIED)); + mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_INDIRECT_Y, src)); + if (ainsh) mIns.Push(*ainsh); + mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, reg + 1)); + if (reg == src) + mIns.Push(NativeCodeInstruction(ASMIT_STX, ASMIM_ZERO_PAGE, reg)); + } else mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, reg)); - - mIns.Push(NativeCodeInstruction(ASMIT_INY, ASMIM_IMPLIED)); - mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_INDIRECT_Y, src)); - if (ainsh) mIns.Push(*ainsh); - mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, reg + 1)); - if (reg == src) - mIns.Push(NativeCodeInstruction(ASMIT_STX, ASMIM_ZERO_PAGE, reg)); } } } @@ -4758,8 +4774,11 @@ void NativeCodeProcedure::CompileInterBlock(InterCodeProcedure* iproc, InterCode fblock->Close(rblock, nullptr, ASMIT_JMP); rblock->mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, BC_REG_TMP + iproc->mTempOffset[ins.mTTemp])); - rblock->mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0)); - rblock->mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, BC_REG_TMP + iproc->mTempOffset[ins.mTTemp] + 1)); + if (InterTypeSize[ins.mTType] > 1) + { + rblock->mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_IMMEDIATE, 0)); + rblock->mIns.Push(NativeCodeInstruction(ASMIT_STA, ASMIM_ZERO_PAGE, BC_REG_TMP + iproc->mTempOffset[ins.mTTemp] + 1)); + } block = rblock; } @@ -4835,7 +4854,8 @@ void NativeCodeProcedure::CompileInterBlock(InterCodeProcedure* iproc, InterCode else { block->mIns.Push(NativeCodeInstruction(ASMIT_LDA, ASMIM_ZERO_PAGE, BC_REG_TMP + iproc->mTempOffset[ins.mSTemp[0]])); - block->mIns.Push(NativeCodeInstruction(ASMIT_ORA, ASMIM_ZERO_PAGE, BC_REG_TMP + iproc->mTempOffset[ins.mSTemp[0]] + 1)); + if (InterTypeSize[ins.mSType[0]] > 1) + block->mIns.Push(NativeCodeInstruction(ASMIT_ORA, ASMIM_ZERO_PAGE, BC_REG_TMP + iproc->mTempOffset[ins.mSTemp[0]] + 1)); block->Close(CompileBlock(iproc, iblock->mTrueJump), CompileBlock(iproc, iblock->mFalseJump), ASMIT_BNE); }