From 81c31bebe91c6de570ccd5c3bd0943b7f3fd4c6b Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Fri, 15 May 2020 02:05:34 +0200 Subject: [PATCH] Fix label bug --- src/common/scripting/jit/jit.cpp | 80 ++++++++++----------------- src/common/scripting/jit/jit_flow.cpp | 4 +- src/common/scripting/jit/jitintern.h | 3 +- 3 files changed, 32 insertions(+), 55 deletions(-) diff --git a/src/common/scripting/jit/jit.cpp b/src/common/scripting/jit/jit.cpp index 3832117a07..dd499bdd40 100644 --- a/src/common/scripting/jit/jit.cpp +++ b/src/common/scripting/jit/jit.cpp @@ -102,17 +102,15 @@ IRFunction* JitCompiler::Codegen() cc.CreateBr(labels[i].block); cc.SetInsertPoint(labels[i].block); } - else + else // Save start location in case GetLabel gets called later { if (!cc.GetInsertBlock()) cc.SetInsertPoint(irfunc->createBasicBlock({})); - - // Save start location in case GetLabel gets called later labels[i].block = cc.GetInsertBlock(); - if (!labels[i].block->code.empty()) - labels[i].instbefore = labels[i].block->code.back(); } + labels[i].index = labels[i].block->code.size(); + int curLine = sfunc->PCToLine(pc); if (curLine != lastLine) { @@ -144,8 +142,6 @@ IRFunction* JitCompiler::Codegen() pc++; } - BindLabels(); - /* auto code = cc.getCode (); for (unsigned int j = 0; j < LineInfo.Size (); j++) @@ -182,24 +178,6 @@ void JitCompiler::EmitOpcode() } } -void JitCompiler::BindLabels() -{ -#if 0 - asmjit::CBNode *cursor = cc.getCursor(); - unsigned int size = labels.Size(); - for (unsigned int i = 0; i < size; i++) - { - const OpcodeLabel &label = labels[i]; - if (label.inUse) - { - cc.setCursor(label.cursor); - cc.bind(label.label); - } - } - cc.setCursor(cursor); -#endif -} - void JitCompiler::CheckVMFrame() { if (!vmframe) @@ -219,37 +197,37 @@ IRValue* JitCompiler::GetCallReturns() IRBasicBlock* JitCompiler::GetLabel(size_t pos) { - if (labels[pos].instbefore) + if (labels[pos].index != 0) // Jump targets can only point at the start of a basic block { - IRValue* instbefore = labels[pos].instbefore; - IRBasicBlock* block = labels[pos].block; + IRBasicBlock* curbb = labels[pos].block; + size_t splitpos = labels[pos].index; - // If inst isn't the first then we need to split the basicblock. Jump targets can only be the start of a basicblock. - if (instbefore) + // Split basic block + IRBasicBlock* newlabelbb = irfunc->createBasicBlock({}); + auto itbegin = curbb->code.begin() + splitpos; + auto itend = curbb->code.end(); + newlabelbb->code.insert(newlabelbb->code.begin(), itbegin, itend); + curbb->code.erase(itbegin, itend); + + // Jump from prev block to next + IRBasicBlock* old = cc.GetInsertBlock(); + cc.SetInsertPoint(curbb); + cc.CreateBr(newlabelbb); + cc.SetInsertPoint(old); + + // Update label + labels[pos].block = newlabelbb; + labels[pos].index = 0; + + // Update other label references + for (size_t i = 0; i < labels.Size(); i++) { - auto it = block->code.end(); - for (size_t i = 0; i < block->code.size(); i++) + if (labels[i].block == curbb && labels[i].index >= splitpos) { - if (block->code[i] == instbefore) - { - it = block->code.begin() + (i + 1); - break; - } + labels[i].block = newlabelbb; + labels[i].index -= splitpos; } - - // Split basic block into two - IRBasicBlock* newlabelbb = irfunc->createBasicBlock({}); - newlabelbb->code.insert(newlabelbb->code.begin(), it, block->code.end()); - block->code.erase(it, block->code.end()); - - // Jump from prev block to next - IRBasicBlock* old = cc.GetInsertBlock(); - cc.SetInsertPoint(block); - cc.CreateBr(newlabelbb); - cc.SetInsertPoint(old); } - - labels[pos].instbefore = nullptr; } else if (!labels[pos].block) { @@ -389,7 +367,7 @@ void JitCompiler::SetupFullVMFrame() StoreF(Load(ToDoublePtr(vmframe, offsetF + i * sizeof(double))), i); for (int i = 0; i < sfunc->NumRegS; i++) - StoreS(Load(ToInt8PtrPtr(vmframe, offsetS + i * sizeof(FString))), i); + StoreS(OffsetPtr(vmframe, offsetS + i * sizeof(FString)), i); for (int i = 0; i < sfunc->NumRegA; i++) StoreA(Load(ToInt8PtrPtr(vmframe, offsetA + i * sizeof(void*))), i); diff --git a/src/common/scripting/jit/jit_flow.cpp b/src/common/scripting/jit/jit_flow.cpp index 6cba24f464..c3cc66f7de 100644 --- a/src/common/scripting/jit/jit_flow.cpp +++ b/src/common/scripting/jit/jit_flow.cpp @@ -87,7 +87,7 @@ void JitCompiler::EmitRET() cc.CreateCondBr(cc.CreateICmpSLE(ConstValueD(retnum), numret), ifbb, endifbb); cc.SetInsertPoint(ifbb); - IRValue* location = Load(ToInt8PtrPtr(ret, retnum * sizeof(VMReturn))); + IRValue* location = OffsetPtr(ret, retnum * sizeof(VMReturn)); int regtype = B; int regnum = C; @@ -185,7 +185,7 @@ void JitCompiler::EmitRETI() cc.CreateCondBr(cc.CreateICmpSLE(ConstValueD(retnum), numret), ifbb, endifbb); cc.SetInsertPoint(ifbb); - IRValue* location = Load(ToInt8PtrPtr(ret, retnum * sizeof(VMReturn))); + IRValue* location = OffsetPtr(ret, retnum * sizeof(VMReturn)); Store(ConstValueD(BCs), ToInt32Ptr(location)); if (a & RET_FINAL) diff --git a/src/common/scripting/jit/jitintern.h b/src/common/scripting/jit/jitintern.h index 330e310add..edd4e0faa9 100644 --- a/src/common/scripting/jit/jitintern.h +++ b/src/common/scripting/jit/jitintern.h @@ -33,7 +33,7 @@ struct JitLineInfo struct JitLabel { IRBasicBlock* block = nullptr; - IRValue* instbefore = nullptr; + size_t index = 0; }; class JitCompiler @@ -78,7 +78,6 @@ private: void SetupFrame(); void SetupSimpleFrame(); void SetupFullVMFrame(); - void BindLabels(); void EmitOpcode(); void EmitPopFrame();