From 210b037dbc9819e27c11ec8c7c8c974b55d57346 Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Fri, 8 Oct 2021 23:04:27 +0200 Subject: [PATCH] Remove sign extended byte loads from byte code --- include/crt.c | 112 +--------------------------------- include/crt.h | 6 -- oscar64/ByteCodeGenerator.cpp | 64 ++++++++++--------- oscar64/ByteCodeGenerator.h | 6 -- oscar64/Disassembler.cpp | 41 ++++--------- oscar64/Emulator.cpp | 49 +++++++-------- oscar64/InterCode.cpp | 2 + 7 files changed, 71 insertions(+), 209 deletions(-) diff --git a/include/crt.c b/include/crt.c index c0d7fb1..7f29a74 100644 --- a/include/crt.c +++ b/include/crt.c @@ -807,76 +807,7 @@ inp_load_addr_8: #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 - 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 - lda #0 - sta $01, x - ldy tmpy - iny - jmp startup.exec -inp_load_addr_u8: - lda (ip), y - tax - iny - lda (ip), y - sty tmpy - tay - jmp L0 -} - -#pragma bytecode(BC_LOAD_ABS_U8, inp_load_abs_u8) -#pragma bytecode(BC_LOAD_ADDR_U8, inp_load_abs_u8.inp_load_addr_u8) - -__asm inp_load_abs_s8 -{ - 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 - bmi W1 - lda #0 - byt $2c -W1: lda #$ff - sta $01, x - ldy tmpy - iny - jmp startup.exec -inp_load_addr_s8: - lda (ip), y - tax - iny - lda (ip), y - sty tmpy - tay - jmp L0 -} - -#pragma bytecode(BC_LOAD_ABS_I8, inp_load_abs_s8) -#pragma bytecode(BC_LOAD_ADDR_I8, inp_load_abs_s8.inp_load_addr_s8) - + __asm inp_load_abs_16 { lda (ip), y @@ -1142,47 +1073,6 @@ __asm inp_load_local_8 #pragma bytecode(BC_LOAD_LOCAL_8, inp_load_local_8) -__asm inp_load_local_u8 -{ - lda (ip), y - tax - iny - lda (ip), y - iny - sty tmpy - tay - lda (fp), y - sta $00, x - lda #0 - sta $01, x - ldy tmpy - jmp startup.exec -} - -#pragma bytecode(BC_LOAD_LOCAL_U8, inp_load_local_u8) - -__asm inp_load_local_s8 -{ - lda (ip), y - tax - iny - lda (ip), y - iny - sty tmpy - tay - lda (fp), y - sta $00, x - bmi W1 - lda #0 - byt $2c -W1: lda #$ff - sta $01, x - ldy tmpy - jmp startup.exec -} - -#pragma bytecode(BC_LOAD_LOCAL_I8, inp_load_local_s8) - __asm inp_store_local_8 { lda (ip), y diff --git a/include/crt.h b/include/crt.h index 35cf9a6..48e0b4e 100644 --- a/include/crt.h +++ b/include/crt.h @@ -34,8 +34,6 @@ enum ByteCode BC_STORE_REG_32, BC_LOAD_ABS_8, - BC_LOAD_ABS_U8, - BC_LOAD_ABS_I8, BC_LOAD_ABS_16, BC_LOAD_ABS_32, @@ -46,8 +44,6 @@ enum ByteCode BC_LEA_ABS, BC_LOAD_LOCAL_8, - BC_LOAD_LOCAL_U8, - BC_LOAD_LOCAL_I8, BC_LOAD_LOCAL_16, BC_LOAD_LOCAL_32, @@ -64,8 +60,6 @@ enum ByteCode BC_LEA_FRAME, BC_LOAD_ADDR_8, - BC_LOAD_ADDR_U8, - BC_LOAD_ADDR_I8, BC_LOAD_ADDR_16, BC_LOAD_ADDR_32, diff --git a/oscar64/ByteCodeGenerator.cpp b/oscar64/ByteCodeGenerator.cpp index 178152f..35f202a 100644 --- a/oscar64/ByteCodeGenerator.cpp +++ b/oscar64/ByteCodeGenerator.cpp @@ -270,8 +270,6 @@ void ByteCodeInstruction::Assemble(ByteCodeGenerator* generator, ByteCodeBasicBl break; case BC_LOAD_ABS_8: - case BC_LOAD_ABS_U8: - case BC_LOAD_ABS_I8: case BC_LOAD_ABS_16: case BC_LOAD_ABS_32: case BC_STORE_ABS_8: @@ -311,8 +309,6 @@ void ByteCodeInstruction::Assemble(ByteCodeGenerator* generator, ByteCodeBasicBl break; case BC_LOAD_LOCAL_8: - case BC_LOAD_LOCAL_U8: - case BC_LOAD_LOCAL_I8: case BC_LOAD_LOCAL_16: case BC_LOAD_LOCAL_32: case BC_STORE_LOCAL_8: @@ -474,8 +470,6 @@ void ByteCodeInstruction::Assemble(ByteCodeGenerator* generator, ByteCodeBasicBl } break; case BC_LOAD_ADDR_8: - case BC_LOAD_ADDR_U8: - case BC_LOAD_ADDR_I8: case BC_LOAD_ADDR_16: case BC_LOAD_ADDR_32: case BC_STORE_ADDR_8: @@ -1732,7 +1726,7 @@ void ByteCodeBasicBlock::LoadDirectValue(InterCodeProcedure* proc, const InterIn { if (ins->mSrc[0].mMemory == IM_GLOBAL) { - ByteCodeInstruction bins((ins->mDst.mType == IT_BOOL || ins->mDst.mType == IT_INT8) ? BC_LOAD_ABS_8 : BC_LOAD_ABS_U8); + ByteCodeInstruction bins(BC_LOAD_ABS_8); bins.mRelocate = true; bins.mLinkerObject = ins->mSrc[0].mLinkerObject; bins.mValue = ins->mSrc[0].mIntConst; @@ -1741,7 +1735,7 @@ void ByteCodeBasicBlock::LoadDirectValue(InterCodeProcedure* proc, const InterIn } else if (ins->mSrc[0].mMemory == IM_ABSOLUTE) { - ByteCodeInstruction bins((ins->mDst.mType == IT_BOOL || ins->mDst.mType == IT_INT8) ? BC_LOAD_ABS_8 : BC_LOAD_ABS_U8); + ByteCodeInstruction bins(BC_LOAD_ABS_8); bins.mValue = ins->mSrc[0].mIntConst; bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins->mDst.mTemp]; mIns.Push(bins); @@ -1766,7 +1760,7 @@ void ByteCodeBasicBlock::LoadDirectValue(InterCodeProcedure* proc, const InterIn if (index <= 255) { - ByteCodeInstruction bins((ins->mDst.mType == IT_BOOL || ins->mDst.mType == IT_INT8) ? BC_LOAD_LOCAL_8 : BC_LOAD_LOCAL_U8); + ByteCodeInstruction bins(BC_LOAD_LOCAL_8); bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins->mDst.mTemp]; bins.mValue = index; mIns.Push(bins); @@ -1777,7 +1771,7 @@ void ByteCodeBasicBlock::LoadDirectValue(InterCodeProcedure* proc, const InterIn lins.mRegister = BC_REG_ADDR; lins.mValue = index; mIns.Push(lins); - ByteCodeInstruction bins((ins->mDst.mType == IT_BOOL || ins->mDst.mType == IT_INT8) ? BC_LOAD_ADDR_8 : BC_LOAD_ADDR_U8); + ByteCodeInstruction bins(BC_LOAD_ADDR_8); bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins->mDst.mTemp]; bins.mValue = 0; mIns.Push(bins); @@ -1908,7 +1902,7 @@ void ByteCodeBasicBlock::LoadDirectValue(InterCodeProcedure* proc, const InterIn if (InterTypeSize[ins->mDst.mType] == 1) { - ByteCodeInstruction bins((ins->mDst.mType == IT_BOOL || ins->mDst.mType == IT_INT8) ? BC_LOAD_ADDR_8 : BC_LOAD_ADDR_U8); + ByteCodeInstruction bins(BC_LOAD_ADDR_8); bins.mRegister = BC_REG_TMP + proc->mTempOffset[ins->mDst.mTemp]; bins.mValue = ins->mSrc[0].mIntConst; mIns.Push(bins); @@ -3286,6 +3280,19 @@ bool ByteCodeBasicBlock::PeepHoleOptimizer(void) mIns[i].mCode = BC_NOP; progress = true; } + else if (mIns[i].mCode == BC_STORE_REG_16 && + (mIns[i + 1].mCode == BC_BINOP_ADDI_16 || mIns[i + 1].mCode == BC_BINOP_MULI8_16 || mIns[i + 1].mCode == BC_BINOP_ANDI_16 || mIns[i + 1].mCode == BC_BINOP_ORI_16) && mIns[i + 1].mRegister == mIns[i].mRegister && + mIns[i + 2].mCode == BC_LOAD_REG_16 && mIns[i].mRegister == mIns[i + 2].mRegister) + { + mIns[i + 0].mCode = BC_NOP; + mIns[i + 1].mRegister = BC_REG_ACCU; + if (mIns[i + 2].mRegisterFinal) + mIns[i + 2].mCode = BC_NOP; + else + mIns[i + 2].mCode = BC_STORE_REG_16; + + progress = true; + } else if (mIns[i].mCode == BC_STORE_REG_32 && !mIns[i + 1].ChangesAccu() && mIns[i + 1].mRegister != mIns[i].mRegister && mIns[i + 2].mCode == BC_LOAD_REG_32 && mIns[i].mRegister == mIns[i + 2].mRegister) @@ -3387,6 +3394,15 @@ bool ByteCodeBasicBlock::PeepHoleOptimizer(void) mIns[i + 0].mCode = BC_NOP; progress = true; } + else if ( + mIns[i + 0].mCode == BC_STORE_REG_16 && + mIns[i + 1].mCode == BC_LOAD_LOCAL_16 && mIns[i + 1].mRegister == BC_REG_ACCU && + mIns[i + 2].IsCommutative() && mIns[i].mRegister == mIns[i + 2].mRegister && mIns[i + 2].mRegisterFinal) + { + mIns[i + 0].mCode = BC_NOP; + mIns[i + 1].mRegister = mIns[i + 0].mRegister; + progress = true; + } } if (i + 1 < mIns.Size()) @@ -3458,24 +3474,6 @@ bool ByteCodeBasicBlock::PeepHoleOptimizer(void) mIns[i + 1].mCode = BC_NOP; progress = true; } - else if (mIns[i].mCode == BC_LOAD_ABS_U8 && mIns[i + 1].mCode == BC_CONV_I8_I16 && mIns[i].mRegister == mIns[i + 1].mRegister && mIns[i + 1].mRegisterFinal) - { - mIns[i].mCode = BC_LOAD_ABS_I8; - mIns[i + 1].mCode = BC_NOP; - progress = true; - } - else if (mIns[i].mCode == BC_LOAD_LOCAL_U8 && mIns[i + 1].mCode == BC_CONV_I8_I16 && mIns[i].mRegister == mIns[i + 1].mRegister && mIns[i + 1].mRegisterFinal) - { - mIns[i].mCode = BC_LOAD_LOCAL_I8; - mIns[i + 1].mCode = BC_NOP; - progress = true; - } - else if (mIns[i].mCode == BC_LOAD_ADDR_U8 && mIns[i + 1].mCode == BC_CONV_I8_I16 && mIns[i].mRegister == mIns[i + 1].mRegister && mIns[i + 1].mRegisterFinal) - { - mIns[i].mCode = BC_LOAD_ADDR_I8; - mIns[i + 1].mCode = BC_NOP; - progress = true; - } else if (mIns[i].mCode == BC_BINOP_ADDI_16 && mIns[i + 1].mCode == BC_BINOP_ADDI_16 && mIns[i].mRegister == mIns[i + 1].mRegister) { mIns[i + 1].mValue += mIns[i].mValue; @@ -3487,6 +3485,14 @@ bool ByteCodeBasicBlock::PeepHoleOptimizer(void) mIns[i].mCode = BC_NOP; progress = true; } +#if 0 + else if ((mIns[i].mCode == BC_LOAD_LOCAL_16 || mIns[i].mCode == BC_LOAD_ABS_16) && mIns[i + 1].mCode == BC_ADDR_REG && mIns[i].mRegister == mIns[i + 1].mRegister && mIns[i + 1].mRegisterFinal) + { + mIns[i].mRegister = BC_REG_ADDR; + mIns[i + 1].mCode = BC_NOP; + progress = true; + } +#endif } if ((mIns[i].mCode == BC_LOAD_REG_16 || mIns[i].mCode == BC_STORE_REG_16 || mIns[i].mCode == BC_LOAD_REG_32 || mIns[i].mCode == BC_STORE_REG_32) && accuTemp == mIns[i].mRegister) diff --git a/oscar64/ByteCodeGenerator.h b/oscar64/ByteCodeGenerator.h index 103e69d..fca1861 100644 --- a/oscar64/ByteCodeGenerator.h +++ b/oscar64/ByteCodeGenerator.h @@ -24,8 +24,6 @@ enum ByteCode BC_STORE_REG_32, BC_LOAD_ABS_8, - BC_LOAD_ABS_U8, - BC_LOAD_ABS_I8, BC_LOAD_ABS_16, BC_LOAD_ABS_32, @@ -36,8 +34,6 @@ enum ByteCode BC_LEA_ABS, BC_LOAD_LOCAL_8, - BC_LOAD_LOCAL_U8, - BC_LOAD_LOCAL_I8, BC_LOAD_LOCAL_16, BC_LOAD_LOCAL_32, @@ -54,8 +50,6 @@ enum ByteCode BC_LEA_FRAME, BC_LOAD_ADDR_8, - BC_LOAD_ADDR_U8, - BC_LOAD_ADDR_I8, BC_LOAD_ADDR_16, BC_LOAD_ADDR_32, diff --git a/oscar64/Disassembler.cpp b/oscar64/Disassembler.cpp index 391fa45..0148f84 100644 --- a/oscar64/Disassembler.cpp +++ b/oscar64/Disassembler.cpp @@ -138,14 +138,6 @@ void ByteCodeDisassembler::Disassemble(FILE* file, const uint8* memory, int star fprintf(file, "MOVUB\t%s, %s", TempName(memory[start + i + 2], tbuffer, proc), AddrName(uint16(memory[start + i + 0] + 256 * memory[start + i + 1]), abuffer, linker)); i += 3; break; - case BC_LOAD_ABS_U8: - fprintf(file, "MOVUB\t%s, %s", TempName(memory[start + i + 2], tbuffer, proc), AddrName(uint16(memory[start + i + 0] + 256 * memory[start + i + 1]), abuffer, linker)); - i += 3; - break; - case BC_LOAD_ABS_I8: - fprintf(file, "MOVSB\t%s, %s", TempName(memory[start + i + 2], tbuffer, proc), AddrName(uint16(memory[start + i + 0] + 256 * memory[start + i + 1]), abuffer, linker)); - i += 3; - break; case BC_LOAD_ABS_16: fprintf(file, "MOV\t%s, %s", TempName(memory[start + i + 2], tbuffer, proc), AddrName(uint16(memory[start + i + 0] + 256 * memory[start + i + 1]), abuffer, linker)); i += 3; @@ -177,14 +169,6 @@ void ByteCodeDisassembler::Disassemble(FILE* file, const uint8* memory, int star fprintf(file, "MOVB\t%s, %d(FP)", TempName(memory[start + i + 0], tbuffer, proc), memory[start + i + 1]); i += 2; break; - case BC_LOAD_LOCAL_U8: - fprintf(file, "MOVUB\t%s, %d(FP)", TempName(memory[start + i + 0], tbuffer, proc), memory[start + i + 1]); - i += 2; - break; - case BC_LOAD_LOCAL_I8: - fprintf(file, "MOVSB\t%s, %d(FP)", TempName(memory[start + i + 0], tbuffer, proc), memory[start + i + 1]); - i += 2; - break; case BC_LOAD_LOCAL_16: fprintf(file, "MOV\t%s, %d(FP)", TempName(memory[start + i + 0], tbuffer, proc), memory[start + i + 1]); i += 2; @@ -522,36 +506,28 @@ void ByteCodeDisassembler::Disassemble(FILE* file, const uint8* memory, int star break; case BC_LOAD_ADDR_8: - fprintf(file, "MOVB\t%s, (ADDR + %d)", TempName(memory[start + i + 0], tbuffer, proc), memory[start + i + 1]); - i += 2; - break; - case BC_LOAD_ADDR_U8: - fprintf(file, "MOVUB\t%s, (ADDR + %d)", TempName(memory[start + i + 0], tbuffer, proc), memory[start + i + 1]); - i += 2; - break; - case BC_LOAD_ADDR_I8: - fprintf(file, "MOVSB\t%s, (ADDR + %d)", TempName(memory[start + i + 0], tbuffer, proc), memory[start + i + 1]); + fprintf(file, "MOVB\t%s, %d(ADDR)", TempName(memory[start + i + 0], tbuffer, proc), memory[start + i + 1]); i += 2; break; case BC_LOAD_ADDR_16: - fprintf(file, "MOV\t%s, (ADDR + %d)", TempName(memory[start + i + 0], tbuffer, proc), memory[start + i + 1]); + fprintf(file, "MOV\t%s, %d(ADDR)", TempName(memory[start + i + 0], tbuffer, proc), memory[start + i + 1]); i += 2; break; case BC_LOAD_ADDR_32: - fprintf(file, "MOVD\t%s, (ADDR + %d)", TempName(memory[start + i + 0], tbuffer, proc), memory[start + i + 1]); + fprintf(file, "MOVD\t%s, %d(ADDR)", TempName(memory[start + i + 0], tbuffer, proc), memory[start + i + 1]); i += 2; break; case BC_STORE_ADDR_8: - fprintf(file, "MOVB\t(ADDR + %d), %s", memory[start + i + 1], TempName(memory[start + i + 0], tbuffer, proc)); + fprintf(file, "MOVB\t%d(ADDR), %s", memory[start + i + 1], TempName(memory[start + i + 0], tbuffer, proc)); i += 2; break; case BC_STORE_ADDR_16: - fprintf(file, "MOV\t(ADDR + %d), %s", memory[start + i + 1], TempName(memory[start + i + 0], tbuffer, proc)); + fprintf(file, "MOV\t%d(ADDR), %s", memory[start + i + 1], TempName(memory[start + i + 0], tbuffer, proc)); i += 2; break; case BC_STORE_ADDR_32: - fprintf(file, "MOV\t(ADDR + %d), %s", memory[start + i + 1], TempName(memory[start + i + 0], tbuffer, proc)); + fprintf(file, "MOV\t%d(ADDR), %s", memory[start + i + 1], TempName(memory[start + i + 0], tbuffer, proc)); i += 2; break; @@ -690,6 +666,11 @@ const char* NativeCodeDisassembler::TempName(uint8 tmp, char* buffer, InterCodeP sprintf_s(buffer, 10, "SP + %d", tmp - BC_REG_STACK); return buffer; } + else if (tmp >= BC_REG_IP && tmp <= BC_REG_IP + 1) + { + sprintf_s(buffer, 10, "IP + %d", tmp - BC_REG_IP); + return buffer; + } else if (tmp >= BC_REG_LOCALS && tmp <= BC_REG_LOCALS + 3) { sprintf_s(buffer, 10, "FP + %d", tmp - BC_REG_LOCALS); diff --git a/oscar64/Emulator.cpp b/oscar64/Emulator.cpp index bb72285..928cbdc 100644 --- a/oscar64/Emulator.cpp +++ b/oscar64/Emulator.cpp @@ -62,21 +62,18 @@ void Emulator::DumpCycles(void) } } - if (numTops == 0 || cycles > topCycles[numTops - 1]) + int j = numTops; + while (j > 0 && topCycles[j-1] < cycles) { - int j = numTops; - while (j > 0 && topCycles[j-1] < cycles) - { - topCycles[j] = topCycles[j - 1]; - topIP[j] = topIP[j - 1]; - j--; - } - topCycles[j] = cycles; - topIP[j] = i; - - if (numTops < 40) - numTops++; + topCycles[j] = topCycles[j - 1]; + topIP[j] = topIP[j - 1]; + j--; } + topCycles[j] = cycles; + topIP[j] = i; + + if (numTops < 40) + numTops++; } } @@ -94,28 +91,26 @@ void Emulator::DumpCycles(void) int cycles = lobjc[i]; if (cycles > 0) { - if (numTops == 0 || cycles > topCycles[numTops - 1]) + int j = numTops; + while (j > 0 && topCycles[j - 1] < cycles) { - int j = numTops; - while (j > 0 && topCycles[j - 1] < cycles) - { - topCycles[j] = topCycles[j - 1]; - topIP[j] = topIP[j - 1]; - j--; - } - topCycles[j] = cycles; - topIP[j] = i; - - if (numTops < 40) - numTops++; + topCycles[j] = topCycles[j - 1]; + topIP[j] = topIP[j - 1]; + j--; } + topCycles[j] = cycles; + topIP[j] = i; + + if (numTops < 40) + numTops++; } } for (int i = 0; i < numTops; i++) { - printf(" %2d : %s : %d\n", i, lobjs[topIP[i]]->mIdent->mString, topCycles[i]); + if (lobjs[topIP[i]]->mIdent) + printf(" %2d : %s : %d\n", i, lobjs[topIP[i]]->mIdent->mString, topCycles[i]); } } diff --git a/oscar64/InterCode.cpp b/oscar64/InterCode.cpp index 7d155b8..9e65f26 100644 --- a/oscar64/InterCode.cpp +++ b/oscar64/InterCode.cpp @@ -309,6 +309,8 @@ static bool MemPtrRange(const InterInstruction* ins, const GrowingInstructionPtr mem = ins->mConst.mMemory; vindex = ins->mConst.mVarIndex; offset = ins->mConst.mIntConst; + + return true; } else if (ins->mCode == IC_LEA) {