- clean up assembly output slightly by only generating labels for the opcodes jumped to

This commit is contained in:
Magnus Norddahl 2018-10-12 06:14:27 +02:00
parent c099b2d3c8
commit 44294a051a
3 changed files with 45 additions and 11 deletions

View file

@ -148,13 +148,15 @@ void JitCompiler::Codegen()
cc.comment("", 0); cc.comment("", 0);
cc.comment(lineinfo.GetChars(), lineinfo.Len()); cc.comment(lineinfo.GetChars(), lineinfo.Len());
cc.bind(labels[i]); labels[i].cursor = cc.getCursor();
ResetTemp(); ResetTemp();
EmitOpcode(); EmitOpcode();
pc++; pc++;
} }
BindLabels();
cc.endFunc(); cc.endFunc();
cc.finalize(); cc.finalize();
} }
@ -173,6 +175,22 @@ void JitCompiler::EmitOpcode()
} }
} }
void JitCompiler::BindLabels()
{
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);
}
void JitCompiler::Setup() void JitCompiler::Setup()
{ {
using namespace asmjit; using namespace asmjit;
@ -245,10 +263,7 @@ void JitCompiler::Setup()
regA[i] = cc.newIntPtr(regname.GetChars()); regA[i] = cc.newIntPtr(regname.GetChars());
} }
int size = sfunc->CodeSize; labels.Resize(sfunc->CodeSize);
labels.Resize(size);
for (int i = 0; i < size; i++)
labels[i] = cc.newLabel();
// VMCalls[0]++ // VMCalls[0]++
auto vmcallsptr = newTempIntPtr(); auto vmcallsptr = newTempIntPtr();

View file

@ -5,7 +5,7 @@ void JitCompiler::EmitTEST()
{ {
int i = (int)(ptrdiff_t)(pc - sfunc->Code); int i = (int)(ptrdiff_t)(pc - sfunc->Code);
cc.cmp(regD[A], BC); cc.cmp(regD[A], BC);
cc.jne(labels[i + 2]); cc.jne(GetLabel(i + 2));
} }
void JitCompiler::EmitTESTN() void JitCompiler::EmitTESTN()
@ -13,14 +13,14 @@ void JitCompiler::EmitTESTN()
int bc = BC; int bc = BC;
int i = (int)(ptrdiff_t)(pc - sfunc->Code); int i = (int)(ptrdiff_t)(pc - sfunc->Code);
cc.cmp(regD[A], -bc); cc.cmp(regD[A], -bc);
cc.jne(labels[i + 2]); cc.jne(GetLabel(i + 2));
} }
void JitCompiler::EmitJMP() void JitCompiler::EmitJMP()
{ {
auto dest = pc + JMPOFS(pc) + 1; auto dest = pc + JMPOFS(pc) + 1;
int i = (int)(ptrdiff_t)(dest - sfunc->Code); int i = (int)(ptrdiff_t)(dest - sfunc->Code);
cc.jmp(labels[i]); cc.jmp(GetLabel(i));
} }
void JitCompiler::EmitIJMP() void JitCompiler::EmitIJMP()
@ -40,7 +40,7 @@ void JitCompiler::EmitIJMP()
int target = i + JMPOFS(&sfunc->Code[i]) + 1; int target = i + JMPOFS(&sfunc->Code[i]) + 1;
cc.cmp(val, i); cc.cmp(val, i);
cc.je(labels[target]); cc.je(GetLabel(target));
} }
} }

View file

@ -39,6 +39,7 @@ private:
#undef xx #undef xx
void Setup(); void Setup();
void BindLabels();
void EmitOpcode(); void EmitOpcode();
void EmitPopFrame(); void EmitPopFrame();
@ -63,7 +64,7 @@ private:
auto successLabel = cc.newLabel(); auto successLabel = cc.newLabel();
auto failLabel = labels[i + 2 + JMPOFS(pc + 1)]; auto failLabel = GetLabel(i + 2 + JMPOFS(pc + 1));
jmpFunc(static_cast<bool>(A & CMP_CHECK), failLabel, successLabel); jmpFunc(static_cast<bool>(A & CMP_CHECK), failLabel, successLabel);
@ -172,7 +173,25 @@ private:
TArray<asmjit::X86Gp> regA; TArray<asmjit::X86Gp> regA;
TArray<asmjit::X86Gp> regS; TArray<asmjit::X86Gp> regS;
TArray<asmjit::Label> labels; struct OpcodeLabel
{
asmjit::CBNode *cursor = nullptr;
asmjit::Label label;
bool inUse = false;
};
asmjit::Label GetLabel(size_t pos)
{
auto &label = labels[pos];
if (!label.inUse)
{
label.label = cc.newLabel();
label.inUse = true;
}
return label.label;
}
TArray<OpcodeLabel> labels;
const VMOP *pc; const VMOP *pc;
VM_UBYTE op; VM_UBYTE op;