From 44294a051a3cba7cb4b9c3118393fb9c14556cd8 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Fri, 12 Oct 2018 06:14:27 +0200 Subject: [PATCH] - clean up assembly output slightly by only generating labels for the opcodes jumped to --- src/scripting/vm/jit.cpp | 25 ++++++++++++++++++++----- src/scripting/vm/jit_flow.cpp | 8 ++++---- src/scripting/vm/jitintern.h | 23 +++++++++++++++++++++-- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/scripting/vm/jit.cpp b/src/scripting/vm/jit.cpp index 7231f8e590..492b9d2a11 100644 --- a/src/scripting/vm/jit.cpp +++ b/src/scripting/vm/jit.cpp @@ -148,13 +148,15 @@ void JitCompiler::Codegen() cc.comment("", 0); cc.comment(lineinfo.GetChars(), lineinfo.Len()); - cc.bind(labels[i]); + labels[i].cursor = cc.getCursor(); ResetTemp(); EmitOpcode(); pc++; } + BindLabels(); + cc.endFunc(); 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() { using namespace asmjit; @@ -245,10 +263,7 @@ void JitCompiler::Setup() regA[i] = cc.newIntPtr(regname.GetChars()); } - int size = sfunc->CodeSize; - labels.Resize(size); - for (int i = 0; i < size; i++) - labels[i] = cc.newLabel(); + labels.Resize(sfunc->CodeSize); // VMCalls[0]++ auto vmcallsptr = newTempIntPtr(); diff --git a/src/scripting/vm/jit_flow.cpp b/src/scripting/vm/jit_flow.cpp index be74bc9406..15177b57d2 100644 --- a/src/scripting/vm/jit_flow.cpp +++ b/src/scripting/vm/jit_flow.cpp @@ -5,7 +5,7 @@ void JitCompiler::EmitTEST() { int i = (int)(ptrdiff_t)(pc - sfunc->Code); cc.cmp(regD[A], BC); - cc.jne(labels[i + 2]); + cc.jne(GetLabel(i + 2)); } void JitCompiler::EmitTESTN() @@ -13,14 +13,14 @@ void JitCompiler::EmitTESTN() int bc = BC; int i = (int)(ptrdiff_t)(pc - sfunc->Code); cc.cmp(regD[A], -bc); - cc.jne(labels[i + 2]); + cc.jne(GetLabel(i + 2)); } void JitCompiler::EmitJMP() { auto dest = pc + JMPOFS(pc) + 1; int i = (int)(ptrdiff_t)(dest - sfunc->Code); - cc.jmp(labels[i]); + cc.jmp(GetLabel(i)); } void JitCompiler::EmitIJMP() @@ -40,7 +40,7 @@ void JitCompiler::EmitIJMP() int target = i + JMPOFS(&sfunc->Code[i]) + 1; cc.cmp(val, i); - cc.je(labels[target]); + cc.je(GetLabel(target)); } } diff --git a/src/scripting/vm/jitintern.h b/src/scripting/vm/jitintern.h index 4ebef501d8..3a9e24adad 100644 --- a/src/scripting/vm/jitintern.h +++ b/src/scripting/vm/jitintern.h @@ -39,6 +39,7 @@ private: #undef xx void Setup(); + void BindLabels(); void EmitOpcode(); void EmitPopFrame(); @@ -63,7 +64,7 @@ private: auto successLabel = cc.newLabel(); - auto failLabel = labels[i + 2 + JMPOFS(pc + 1)]; + auto failLabel = GetLabel(i + 2 + JMPOFS(pc + 1)); jmpFunc(static_cast(A & CMP_CHECK), failLabel, successLabel); @@ -172,7 +173,25 @@ private: TArray regA; TArray regS; - TArray 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 labels; const VMOP *pc; VM_UBYTE op;