Skip to content

Commit

Permalink
Merge pull request #551 from Gutawer/asmjit
Browse files Browse the repository at this point in the history
Add a first draft of exception handling
  • Loading branch information
dpjudas committed Aug 18, 2018
2 parents c89d8bb + d983ae6 commit b6f52c3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/scripting/vm/jit.cpp
Expand Up @@ -292,6 +292,22 @@ static int64_t ToMemAddress(const void *d)
return (int64_t)(ptrdiff_t)d;
}

void setPCOnAbort(VMScriptFunction *sfunc, VMOP* pc) {
sfunc->pcOnJitAbort = pc;
}

void emitAbortExceptionCall(asmjit::X86Compiler& cc, VMScriptFunction* sfunc, const VMOP* pc, EVMAbortException reason, const char* moreinfo) {
using namespace asmjit;

CCFuncCall* setPCCall = cc.call(imm_ptr((void*)setPCOnAbort), FuncSignature2<void, VMScriptFunction*, VMOP*>(CallConv::kIdHost));
setPCCall->setArg(0, imm_ptr(sfunc));
setPCCall->setArg(1, imm_ptr(pc));

CCFuncCall* throwAbortCall = cc.call(imm_ptr((void*)ThrowAbortException), FuncSignatureT<void, int, const char*>(CallConv::kIdHost));
throwAbortCall->setArg(0, imm(reason));
throwAbortCall->setArg(1, imm_ptr(moreinfo));
}

static void CallSqrt(asmjit::X86Compiler& cc, const asmjit::X86Xmm &a, const asmjit::X86Xmm &b)
{
using namespace asmjit;
Expand Down Expand Up @@ -902,10 +918,18 @@ JitFuncPtr JitCompile(VMScriptFunction *sfunc)
{
auto tmp0 = cc.newInt32();
auto tmp1 = cc.newInt32();
auto label = cc.newLabel();

cc.test(regD[C], regD[C]);
cc.je(label);

cc.mov(tmp0, regD[B]);
cc.cdq(tmp1, tmp0);
cc.idiv(tmp1, tmp0, regD[C]);
cc.mov(regD[A], tmp0);

cc.bind(label);
emitAbortExceptionCall(cc, sfunc, pc, X_DIVISION_BY_ZERO, nullptr);
break;
}
case OP_DIV_RK:
Expand Down
14 changes: 12 additions & 2 deletions src/scripting/vm/vmexec.h
Expand Up @@ -80,8 +80,18 @@ static int Exec(VMFrameStack *stack, const VMOP *pc, VMReturn *ret, int numret)
sfunc->JitFunc = JitCompile(sfunc);
sfunc->JitCompiled = true;
}
if (sfunc->JitFunc)
return sfunc->JitFunc(stack, &reg, ret, numret);
if (sfunc->JitFunc) {
try {
return sfunc->JitFunc(stack, &reg, ret, numret);
}
catch (CVMAbortException &err)
{
err.MaybePrintMessage();
err.stacktrace.AppendFormat("Called from %s at %s, line %d\n", sfunc->PrintableName.GetChars(), sfunc->SourceFileName.GetChars(), sfunc->PCToLine(sfunc->pcOnJitAbort));
// PrintParameters(reg.param + f->NumParam - B, B);
throw;
}
}
}

void *ptr;
Expand Down
1 change: 1 addition & 0 deletions src/scripting/vm/vmintern.h
Expand Up @@ -471,6 +471,7 @@ class VMScriptFunction : public VMFunction

bool JitCompiled = false;
JitFuncPtr JitFunc = nullptr;
VMOP* pcOnJitAbort = nullptr;

void InitExtra(void *addr);
void DestroyExtra(void *addr);
Expand Down

0 comments on commit b6f52c3

Please sign in to comment.