Fix label bug

This commit is contained in:
Magnus Norddahl 2020-05-15 02:05:34 +02:00
parent c670e0b016
commit 81c31bebe9
3 changed files with 32 additions and 55 deletions

View file

@ -102,17 +102,15 @@ IRFunction* JitCompiler::Codegen()
cc.CreateBr(labels[i].block); cc.CreateBr(labels[i].block);
cc.SetInsertPoint(labels[i].block); cc.SetInsertPoint(labels[i].block);
} }
else else // Save start location in case GetLabel gets called later
{ {
if (!cc.GetInsertBlock()) if (!cc.GetInsertBlock())
cc.SetInsertPoint(irfunc->createBasicBlock({})); cc.SetInsertPoint(irfunc->createBasicBlock({}));
// Save start location in case GetLabel gets called later
labels[i].block = cc.GetInsertBlock(); 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); int curLine = sfunc->PCToLine(pc);
if (curLine != lastLine) if (curLine != lastLine)
{ {
@ -144,8 +142,6 @@ IRFunction* JitCompiler::Codegen()
pc++; pc++;
} }
BindLabels();
/* /*
auto code = cc.getCode (); auto code = cc.getCode ();
for (unsigned int j = 0; j < LineInfo.Size (); j++) 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() void JitCompiler::CheckVMFrame()
{ {
if (!vmframe) if (!vmframe)
@ -219,37 +197,37 @@ IRValue* JitCompiler::GetCallReturns()
IRBasicBlock* JitCompiler::GetLabel(size_t pos) 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* curbb = labels[pos].block;
IRBasicBlock* block = 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. // Split basic block
if (instbefore) 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(); if (labels[i].block == curbb && labels[i].index >= splitpos)
for (size_t i = 0; i < block->code.size(); i++)
{ {
if (block->code[i] == instbefore) labels[i].block = newlabelbb;
{ labels[i].index -= splitpos;
it = block->code.begin() + (i + 1);
break;
}
} }
// 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) else if (!labels[pos].block)
{ {
@ -389,7 +367,7 @@ void JitCompiler::SetupFullVMFrame()
StoreF(Load(ToDoublePtr(vmframe, offsetF + i * sizeof(double))), i); StoreF(Load(ToDoublePtr(vmframe, offsetF + i * sizeof(double))), i);
for (int i = 0; i < sfunc->NumRegS; 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++) for (int i = 0; i < sfunc->NumRegA; i++)
StoreA(Load(ToInt8PtrPtr(vmframe, offsetA + i * sizeof(void*))), i); StoreA(Load(ToInt8PtrPtr(vmframe, offsetA + i * sizeof(void*))), i);

View file

@ -87,7 +87,7 @@ void JitCompiler::EmitRET()
cc.CreateCondBr(cc.CreateICmpSLE(ConstValueD(retnum), numret), ifbb, endifbb); cc.CreateCondBr(cc.CreateICmpSLE(ConstValueD(retnum), numret), ifbb, endifbb);
cc.SetInsertPoint(ifbb); cc.SetInsertPoint(ifbb);
IRValue* location = Load(ToInt8PtrPtr(ret, retnum * sizeof(VMReturn))); IRValue* location = OffsetPtr(ret, retnum * sizeof(VMReturn));
int regtype = B; int regtype = B;
int regnum = C; int regnum = C;
@ -185,7 +185,7 @@ void JitCompiler::EmitRETI()
cc.CreateCondBr(cc.CreateICmpSLE(ConstValueD(retnum), numret), ifbb, endifbb); cc.CreateCondBr(cc.CreateICmpSLE(ConstValueD(retnum), numret), ifbb, endifbb);
cc.SetInsertPoint(ifbb); cc.SetInsertPoint(ifbb);
IRValue* location = Load(ToInt8PtrPtr(ret, retnum * sizeof(VMReturn))); IRValue* location = OffsetPtr(ret, retnum * sizeof(VMReturn));
Store(ConstValueD(BCs), ToInt32Ptr(location)); Store(ConstValueD(BCs), ToInt32Ptr(location));
if (a & RET_FINAL) if (a & RET_FINAL)

View file

@ -33,7 +33,7 @@ struct JitLineInfo
struct JitLabel struct JitLabel
{ {
IRBasicBlock* block = nullptr; IRBasicBlock* block = nullptr;
IRValue* instbefore = nullptr; size_t index = 0;
}; };
class JitCompiler class JitCompiler
@ -78,7 +78,6 @@ private:
void SetupFrame(); void SetupFrame();
void SetupSimpleFrame(); void SetupSimpleFrame();
void SetupFullVMFrame(); void SetupFullVMFrame();
void BindLabels();
void EmitOpcode(); void EmitOpcode();
void EmitPopFrame(); void EmitPopFrame();