2018-08-12 00:11:13 +00:00
|
|
|
|
|
|
|
#include "jit.h"
|
2018-09-13 00:29:04 +00:00
|
|
|
#include "jitintern.h"
|
2018-08-12 00:11:13 +00:00
|
|
|
|
2018-10-09 12:46:27 +00:00
|
|
|
extern PString *TypeString;
|
|
|
|
extern PStruct *TypeVector2;
|
|
|
|
extern PStruct *TypeVector3;
|
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
static asmjit::JitRuntime *jit;
|
|
|
|
static int jitRefCount = 0;
|
2018-08-13 16:07:36 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
asmjit::JitRuntime *JitGetRuntime()
|
2018-08-26 12:27:46 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
if (!jit)
|
|
|
|
jit = new asmjit::JitRuntime;
|
|
|
|
jitRefCount++;
|
|
|
|
return jit;
|
|
|
|
}
|
2018-08-26 12:27:46 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
void JitCleanUp(VMScriptFunction *func)
|
2018-08-12 00:11:13 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
jitRefCount--;
|
|
|
|
if (jitRefCount == 0)
|
2018-08-18 23:46:56 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
delete jit;
|
|
|
|
jit = nullptr;
|
2018-08-18 23:46:56 +00:00
|
|
|
}
|
2018-09-13 00:29:04 +00:00
|
|
|
}
|
2018-08-18 23:46:56 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
static void OutputJitLog(const asmjit::StringLogger &logger)
|
|
|
|
{
|
|
|
|
// Write line by line since I_FatalError seems to cut off long strings
|
|
|
|
const char *pos = logger.getString();
|
|
|
|
const char *end = pos;
|
|
|
|
while (*end)
|
2018-08-18 23:46:56 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
if (*end == '\n')
|
|
|
|
{
|
|
|
|
FString substr(pos, (int)(ptrdiff_t)(end - pos));
|
|
|
|
Printf("%s\n", substr.GetChars());
|
|
|
|
pos = end + 1;
|
|
|
|
}
|
|
|
|
end++;
|
2018-08-18 23:46:56 +00:00
|
|
|
}
|
2018-09-13 00:29:04 +00:00
|
|
|
if (pos != end)
|
|
|
|
Printf("%s\n", pos);
|
|
|
|
}
|
2018-08-18 23:46:56 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
//#define DEBUG_JIT
|
2018-08-18 23:46:56 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
JitFuncPtr JitCompile(VMScriptFunction *sfunc)
|
|
|
|
{
|
|
|
|
#if defined(DEBUG_JIT)
|
2018-10-07 04:29:54 +00:00
|
|
|
if (strcmp(sfunc->PrintableName.GetChars(), "StatusScreen.drawNum") != 0)
|
2018-09-13 00:29:04 +00:00
|
|
|
return nullptr;
|
|
|
|
#endif
|
2018-08-18 23:46:56 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
//Printf("Jitting function: %s\n", sfunc->PrintableName.GetChars());
|
2018-08-18 23:46:56 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
using namespace asmjit;
|
|
|
|
StringLogger logger;
|
|
|
|
try
|
2018-09-02 22:44:25 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
auto *jit = JitGetRuntime();
|
2018-09-02 22:44:25 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
ThrowingErrorHandler errorHandler;
|
|
|
|
CodeHolder code;
|
|
|
|
code.init(jit->getCodeInfo());
|
|
|
|
code.setErrorHandler(&errorHandler);
|
|
|
|
code.setLogger(&logger);
|
2018-09-02 22:44:25 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
JitCompiler compiler(&code, sfunc);
|
|
|
|
compiler.Codegen();
|
2018-09-02 22:44:25 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
JitFuncPtr fn = nullptr;
|
|
|
|
Error err = jit->add(&fn, &code);
|
|
|
|
if (err)
|
|
|
|
I_FatalError("JitRuntime::add failed: %d", err);
|
2018-08-18 23:46:56 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
#if defined(DEBUG_JIT)
|
|
|
|
OutputJitLog(logger);
|
|
|
|
#endif
|
2018-08-18 23:46:56 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
2018-08-18 23:46:56 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
OutputJitLog(logger);
|
|
|
|
I_FatalError("Unexpected JIT error: %s\n", e.what());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2018-08-18 23:46:56 +00:00
|
|
|
|
2018-10-10 04:17:35 +00:00
|
|
|
void JitDumpLog(FILE *file, VMScriptFunction *sfunc)
|
|
|
|
{
|
|
|
|
using namespace asmjit;
|
|
|
|
StringLogger logger;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto *jit = JitGetRuntime();
|
|
|
|
|
|
|
|
ThrowingErrorHandler errorHandler;
|
|
|
|
CodeHolder code;
|
|
|
|
code.init(jit->getCodeInfo());
|
|
|
|
code.setErrorHandler(&errorHandler);
|
|
|
|
code.setLogger(&logger);
|
|
|
|
|
|
|
|
JitCompiler compiler(&code, sfunc);
|
|
|
|
compiler.Codegen();
|
|
|
|
|
|
|
|
fwrite(logger.getString(), logger.getLength(), 1, file);
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
fwrite(logger.getString(), logger.getLength(), 1, file);
|
|
|
|
|
|
|
|
FString err;
|
|
|
|
err.Format("Unexpected JIT error: %s\n", e.what());
|
|
|
|
fwrite(err.GetChars(), err.Len(), 1, file);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
I_FatalError("Unexpected JIT error: %s\n", e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2018-08-18 23:46:56 +00:00
|
|
|
|
2018-09-13 00:38:51 +00:00
|
|
|
static const char *OpNames[NUM_OPS] =
|
2018-09-13 00:29:04 +00:00
|
|
|
{
|
2018-09-13 17:03:30 +00:00
|
|
|
#define xx(op, name, mode, alt, kreg, ktype) #op,
|
2018-09-13 00:38:51 +00:00
|
|
|
#include "vmops.h"
|
|
|
|
#undef xx
|
|
|
|
};
|
2018-08-14 14:07:09 +00:00
|
|
|
|
2018-09-13 00:38:51 +00:00
|
|
|
void JitCompiler::Codegen()
|
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
Setup();
|
2018-08-14 14:07:09 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
pc = sfunc->Code;
|
|
|
|
auto end = pc + sfunc->CodeSize;
|
|
|
|
while (pc != end)
|
2018-08-18 23:46:56 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
int i = (int)(ptrdiff_t)(pc - sfunc->Code);
|
|
|
|
op = pc->op;
|
2018-08-14 14:07:09 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
FString lineinfo;
|
2018-10-10 04:17:35 +00:00
|
|
|
lineinfo.Format("; line %d: %02x%02x%02x%02x %s", sfunc->PCToLine(pc), pc->op, pc->a, pc->b, pc->c, OpNames[op]);
|
|
|
|
cc.comment("", 0);
|
2018-09-13 00:29:04 +00:00
|
|
|
cc.comment(lineinfo.GetChars(), lineinfo.Len());
|
2018-08-14 14:07:09 +00:00
|
|
|
|
2018-10-10 04:17:35 +00:00
|
|
|
cc.bind(labels[i]);
|
|
|
|
ResetTemp();
|
2018-09-13 00:29:04 +00:00
|
|
|
EmitOpcode();
|
2018-08-14 14:07:09 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
pc++;
|
2018-08-18 23:46:56 +00:00
|
|
|
}
|
2018-08-14 14:07:09 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
cc.endFunc();
|
|
|
|
cc.finalize();
|
|
|
|
}
|
2018-08-14 14:07:09 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
void JitCompiler::EmitOpcode()
|
|
|
|
{
|
|
|
|
switch (op)
|
2018-09-02 01:39:02 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
#define xx(op, name, mode, alt, kreg, ktype) case OP_##op: Emit##op(); break;
|
|
|
|
#include "vmops.h"
|
|
|
|
#undef xx
|
2018-09-02 01:39:02 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
default:
|
|
|
|
I_FatalError("JIT error: Unknown VM opcode %d\n", op);
|
|
|
|
break;
|
2018-09-02 01:39:02 +00:00
|
|
|
}
|
2018-09-13 00:29:04 +00:00
|
|
|
}
|
2018-08-14 14:07:09 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
void JitCompiler::Setup()
|
|
|
|
{
|
|
|
|
using namespace asmjit;
|
2018-08-12 00:11:13 +00:00
|
|
|
|
2018-10-09 01:37:11 +00:00
|
|
|
ResetTemp();
|
|
|
|
|
2018-10-10 04:17:35 +00:00
|
|
|
static const char *marks = "=======================================================";
|
|
|
|
cc.comment("", 0);
|
|
|
|
cc.comment(marks, 56);
|
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
FString funcname;
|
|
|
|
funcname.Format("Function: %s", sfunc->PrintableName.GetChars());
|
|
|
|
cc.comment(funcname.GetChars(), funcname.Len());
|
- implement emitter functions for LKP_R, META, CLSS, LO, LO_R, LP, LP_R, SO, SO_R, SP, SP_R
- add emit code for throwing exceptions on null pointers
2018-08-31 04:57:30 +00:00
|
|
|
|
2018-10-10 04:17:35 +00:00
|
|
|
cc.comment(marks, 56);
|
|
|
|
cc.comment("", 0);
|
|
|
|
|
2018-10-09 14:30:55 +00:00
|
|
|
auto unusedFunc = cc.newIntPtr("func"); // VMFunction*
|
2018-10-09 01:37:11 +00:00
|
|
|
args = cc.newIntPtr("args"); // VMValue *params
|
|
|
|
numargs = cc.newInt32("numargs"); // int numargs
|
2018-09-13 00:29:04 +00:00
|
|
|
ret = cc.newIntPtr("ret"); // VMReturn *ret
|
|
|
|
numret = cc.newInt32("numret"); // int numret
|
2018-08-13 20:36:55 +00:00
|
|
|
|
2018-10-09 14:30:55 +00:00
|
|
|
cc.addFunc(FuncSignature5<int, VMFunction *, void *, int, void *, int>());
|
|
|
|
cc.setArg(0, unusedFunc);
|
|
|
|
cc.setArg(1, args);
|
|
|
|
cc.setArg(2, numargs);
|
|
|
|
cc.setArg(3, ret);
|
|
|
|
cc.setArg(4, numret);
|
2018-08-12 00:11:13 +00:00
|
|
|
|
2018-09-16 01:20:56 +00:00
|
|
|
auto stackalloc = cc.newStack(sizeof(VMReturn) * MAX_RETURNS, alignof(VMReturn));
|
2018-09-13 00:29:04 +00:00
|
|
|
callReturns = cc.newIntPtr("callReturns");
|
2018-09-16 01:20:56 +00:00
|
|
|
cc.lea(callReturns, stackalloc);
|
2018-08-15 02:08:09 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
konstd = sfunc->KonstD;
|
|
|
|
konstf = sfunc->KonstF;
|
|
|
|
konsts = sfunc->KonstS;
|
|
|
|
konsta = sfunc->KonstA;
|
2018-08-15 02:08:09 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
regD.Resize(sfunc->NumRegD);
|
|
|
|
regF.Resize(sfunc->NumRegF);
|
|
|
|
regA.Resize(sfunc->NumRegA);
|
|
|
|
regS.Resize(sfunc->NumRegS);
|
2018-08-15 02:08:09 +00:00
|
|
|
|
2018-10-09 12:46:27 +00:00
|
|
|
for (int i = 0; i < sfunc->NumRegD; i++)
|
|
|
|
{
|
|
|
|
FString regname;
|
|
|
|
regname.Format("regD%d", i);
|
|
|
|
regD[i] = cc.newInt32(regname.GetChars());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < sfunc->NumRegF; i++)
|
|
|
|
{
|
|
|
|
FString regname;
|
|
|
|
regname.Format("regF%d", i);
|
|
|
|
regF[i] = cc.newXmmSd(regname.GetChars());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < sfunc->NumRegS; i++)
|
|
|
|
{
|
|
|
|
FString regname;
|
|
|
|
regname.Format("regS%d", i);
|
|
|
|
regS[i] = cc.newIntPtr(regname.GetChars());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < sfunc->NumRegA; i++)
|
|
|
|
{
|
|
|
|
FString regname;
|
|
|
|
regname.Format("regA%d", i);
|
|
|
|
regA[i] = cc.newIntPtr(regname.GetChars());
|
|
|
|
}
|
|
|
|
|
2018-10-10 04:17:35 +00:00
|
|
|
int size = sfunc->CodeSize;
|
|
|
|
labels.Resize(size);
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
labels[i] = cc.newLabel();
|
|
|
|
|
2018-10-10 21:47:56 +00:00
|
|
|
// VMCalls[0]++
|
|
|
|
auto vmcallsptr = newTempIntPtr();
|
|
|
|
auto vmcalls = newTempInt32();
|
|
|
|
cc.mov(vmcallsptr, imm_ptr(VMCalls));
|
|
|
|
cc.mov(vmcalls, x86::dword_ptr(vmcallsptr));
|
|
|
|
cc.add(vmcalls, (int)1);
|
|
|
|
cc.mov(x86::dword_ptr(vmcallsptr), vmcalls);
|
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
frameD = cc.newIntPtr();
|
|
|
|
frameF = cc.newIntPtr();
|
|
|
|
frameS = cc.newIntPtr();
|
|
|
|
frameA = cc.newIntPtr();
|
|
|
|
params = cc.newIntPtr();
|
2018-08-15 02:08:09 +00:00
|
|
|
|
2018-09-16 01:20:56 +00:00
|
|
|
// the VM version reads this from the stack, but it is constant data
|
|
|
|
int offsetParams = ((int)sizeof(VMFrame) + 15) & ~15;
|
|
|
|
int offsetF = offsetParams + (int)(sfunc->MaxParam * sizeof(VMValue));
|
|
|
|
int offsetS = offsetF + (int)(sfunc->NumRegF * sizeof(double));
|
|
|
|
int offsetA = offsetS + (int)(sfunc->NumRegS * sizeof(FString));
|
|
|
|
int offsetD = offsetA + (int)(sfunc->NumRegA * sizeof(void*));
|
2018-09-16 01:39:54 +00:00
|
|
|
offsetExtra = (offsetD + (int)(sfunc->NumRegD * sizeof(int32_t)) + 15) & ~15;
|
2018-09-16 01:20:56 +00:00
|
|
|
|
2018-10-09 03:19:29 +00:00
|
|
|
vmframe = cc.newIntPtr("vmframe");
|
|
|
|
|
|
|
|
if (sfunc->SpecialInits.Size() == 0 && sfunc->NumRegS == 0)
|
|
|
|
{
|
|
|
|
// This is a simple frame with no constructors or destructors. Allocate it on the stack ourselves.
|
|
|
|
|
|
|
|
auto vmstack = cc.newStack(sfunc->StackSize, 16);
|
|
|
|
cc.lea(vmframe, vmstack);
|
2018-10-09 12:46:27 +00:00
|
|
|
cc.lea(params, x86::ptr(vmframe, offsetParams));
|
|
|
|
cc.lea(frameF, x86::ptr(vmframe, offsetF));
|
|
|
|
cc.lea(frameS, x86::ptr(vmframe, offsetS));
|
|
|
|
cc.lea(frameA, x86::ptr(vmframe, offsetA));
|
|
|
|
cc.lea(frameD, x86::ptr(vmframe, offsetD));
|
|
|
|
|
|
|
|
auto slowinit = cc.newLabel();
|
|
|
|
auto endinit = cc.newLabel();
|
|
|
|
|
|
|
|
#if 0 // this crashes sometimes
|
|
|
|
cc.cmp(numargs, sfunc->NumArgs);
|
|
|
|
cc.jne(slowinit);
|
|
|
|
|
|
|
|
// Is there a better way to know the type than this?
|
|
|
|
int argsPos = 0;
|
|
|
|
int regd = 0, regf = 0, rega = 0;
|
|
|
|
for (unsigned int i = 0; i < sfunc->Proto->ArgumentTypes.Size(); i++)
|
|
|
|
{
|
|
|
|
const PType *type = sfunc->Proto->ArgumentTypes[i];
|
|
|
|
if (type->isPointer())
|
|
|
|
{
|
|
|
|
cc.mov(regA[rega++], x86::ptr(args, argsPos++ * sizeof(VMValue) + offsetof(VMValue, a)));
|
|
|
|
}
|
|
|
|
else if (type->isIntCompatible())
|
|
|
|
{
|
|
|
|
cc.mov(regD[regd++], x86::dword_ptr(args, argsPos++ * sizeof(VMValue) + offsetof(VMValue, i)));
|
|
|
|
}
|
|
|
|
else if (type == TypeVector2)
|
|
|
|
{
|
|
|
|
cc.movsd(regF[regf++], x86::qword_ptr(args, argsPos++ * sizeof(VMValue) + offsetof(VMValue, f)));
|
|
|
|
cc.movsd(regF[regf++], x86::qword_ptr(args, argsPos++ * sizeof(VMValue) + offsetof(VMValue, f)));
|
|
|
|
}
|
|
|
|
else if (type == TypeVector3)
|
|
|
|
{
|
|
|
|
cc.movsd(regF[regf++], x86::qword_ptr(args, argsPos++ * sizeof(VMValue) + offsetof(VMValue, f)));
|
|
|
|
cc.movsd(regF[regf++], x86::qword_ptr(args, argsPos++ * sizeof(VMValue) + offsetof(VMValue, f)));
|
|
|
|
cc.movsd(regF[regf++], x86::qword_ptr(args, argsPos++ * sizeof(VMValue) + offsetof(VMValue, f)));
|
|
|
|
}
|
|
|
|
else if (type->isFloat())
|
|
|
|
{
|
|
|
|
cc.movsd(regF[regf++], x86::qword_ptr(args, argsPos++ * sizeof(VMValue) + offsetof(VMValue, f)));
|
|
|
|
}
|
|
|
|
else if (type == TypeString)
|
|
|
|
{
|
|
|
|
I_FatalError("JIT: Strings are not supported yet for simple frames");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sfunc->NumArgs != argsPos || regd > sfunc->NumRegD || regf > sfunc->NumRegF || rega > sfunc->NumRegA)
|
|
|
|
I_FatalError("JIT: sfunc->NumArgs != argsPos || regd > sfunc->NumRegD || regf > sfunc->NumRegF || rega > sfunc->NumRegA");
|
|
|
|
|
|
|
|
cc.jmp(endinit);
|
|
|
|
#endif
|
|
|
|
cc.bind(slowinit);
|
|
|
|
|
2018-10-09 03:19:29 +00:00
|
|
|
auto sfuncptr = cc.newIntPtr();
|
|
|
|
cc.mov(sfuncptr, imm_ptr(sfunc));
|
|
|
|
if (cc.is64Bit())
|
|
|
|
cc.mov(x86::qword_ptr(vmframe, offsetof(VMFrame, Func)), sfuncptr);
|
|
|
|
else
|
|
|
|
cc.mov(x86::dword_ptr(vmframe, offsetof(VMFrame, Func)), sfuncptr);
|
|
|
|
cc.mov(x86::byte_ptr(vmframe, offsetof(VMFrame, NumRegD)), sfunc->NumRegD);
|
|
|
|
cc.mov(x86::byte_ptr(vmframe, offsetof(VMFrame, NumRegF)), sfunc->NumRegF);
|
|
|
|
cc.mov(x86::byte_ptr(vmframe, offsetof(VMFrame, NumRegS)), sfunc->NumRegS);
|
|
|
|
cc.mov(x86::byte_ptr(vmframe, offsetof(VMFrame, NumRegA)), sfunc->NumRegA);
|
|
|
|
cc.mov(x86::word_ptr(vmframe, offsetof(VMFrame, MaxParam)), sfunc->MaxParam);
|
|
|
|
cc.mov(x86::word_ptr(vmframe, offsetof(VMFrame, NumParam)), 0);
|
|
|
|
|
2018-10-09 14:30:55 +00:00
|
|
|
auto fillParams = CreateCall<void, VMFrame *, VMValue *, int>([](VMFrame *newf, VMValue *args, int numargs) {
|
2018-10-09 03:19:29 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
VMFillParams(args, newf, numargs);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2018-10-09 14:30:55 +00:00
|
|
|
VMThrowException(std::current_exception());
|
2018-10-09 03:19:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
fillParams->setArg(0, vmframe);
|
|
|
|
fillParams->setArg(1, args);
|
|
|
|
fillParams->setArg(2, numargs);
|
2018-10-09 12:46:27 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < sfunc->NumRegD; i++)
|
|
|
|
cc.mov(regD[i], x86::dword_ptr(frameD, i * sizeof(int32_t)));
|
|
|
|
|
|
|
|
for (int i = 0; i < sfunc->NumRegF; i++)
|
|
|
|
cc.movsd(regF[i], x86::qword_ptr(frameF, i * sizeof(double)));
|
|
|
|
|
|
|
|
for (int i = 0; i < sfunc->NumRegS; i++)
|
|
|
|
cc.lea(regS[i], x86::ptr(frameS, i * sizeof(FString)));
|
|
|
|
|
|
|
|
for (int i = 0; i < sfunc->NumRegA; i++)
|
|
|
|
cc.mov(regA[i], x86::ptr(frameA, i * sizeof(void*)));
|
|
|
|
|
|
|
|
cc.bind(endinit);
|
2018-10-09 03:19:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stack = cc.newIntPtr("stack");
|
2018-10-09 14:30:55 +00:00
|
|
|
auto allocFrame = CreateCall<VMFrameStack *, VMScriptFunction *, VMValue *, int>([](VMScriptFunction *func, VMValue *args, int numargs) -> VMFrameStack* {
|
2018-10-09 03:19:29 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
VMFrameStack *stack = &GlobalVMStack;
|
|
|
|
VMFrame *newf = stack->AllocFrame(func);
|
2018-10-09 14:30:55 +00:00
|
|
|
CurrentJitExceptInfo->vmframes++;
|
2018-10-09 03:19:29 +00:00
|
|
|
VMFillParams(args, newf, numargs);
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2018-10-09 14:30:55 +00:00
|
|
|
VMThrowException(std::current_exception());
|
2018-10-09 03:19:29 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
allocFrame->setRet(0, stack);
|
|
|
|
allocFrame->setArg(0, imm_ptr(sfunc));
|
|
|
|
allocFrame->setArg(1, args);
|
|
|
|
allocFrame->setArg(2, numargs);
|
|
|
|
|
|
|
|
cc.mov(vmframe, x86::ptr(stack)); // stack->Blocks
|
|
|
|
cc.mov(vmframe, x86::ptr(vmframe, VMFrameStack::OffsetLastFrame())); // Blocks->LastFrame
|
2018-09-16 01:39:54 +00:00
|
|
|
|
2018-10-09 12:46:27 +00:00
|
|
|
cc.lea(params, x86::ptr(vmframe, offsetParams));
|
|
|
|
cc.lea(frameF, x86::ptr(vmframe, offsetF));
|
|
|
|
cc.lea(frameS, x86::ptr(vmframe, offsetS));
|
|
|
|
cc.lea(frameA, x86::ptr(vmframe, offsetA));
|
|
|
|
cc.lea(frameD, x86::ptr(vmframe, offsetD));
|
2018-09-16 01:20:56 +00:00
|
|
|
|
2018-10-09 12:46:27 +00:00
|
|
|
for (int i = 0; i < sfunc->NumRegD; i++)
|
|
|
|
cc.mov(regD[i], x86::dword_ptr(frameD, i * sizeof(int32_t)));
|
2018-09-16 01:20:56 +00:00
|
|
|
|
2018-10-09 12:46:27 +00:00
|
|
|
for (int i = 0; i < sfunc->NumRegF; i++)
|
|
|
|
cc.movsd(regF[i], x86::qword_ptr(frameF, i * sizeof(double)));
|
2018-09-16 01:20:56 +00:00
|
|
|
|
2018-10-09 12:46:27 +00:00
|
|
|
for (int i = 0; i < sfunc->NumRegS; i++)
|
|
|
|
cc.lea(regS[i], x86::ptr(frameS, i * sizeof(FString)));
|
2018-09-16 01:20:56 +00:00
|
|
|
|
2018-10-09 12:46:27 +00:00
|
|
|
for (int i = 0; i < sfunc->NumRegA; i++)
|
|
|
|
cc.mov(regA[i], x86::ptr(frameA, i * sizeof(void*)));
|
2018-09-02 13:36:39 +00:00
|
|
|
}
|
2018-09-13 00:29:04 +00:00
|
|
|
}
|
2018-09-02 13:36:39 +00:00
|
|
|
|
2018-10-09 01:37:11 +00:00
|
|
|
void JitCompiler::EmitPopFrame()
|
|
|
|
{
|
2018-10-09 03:19:29 +00:00
|
|
|
if (sfunc->SpecialInits.Size() != 0 || sfunc->NumRegS != 0)
|
|
|
|
{
|
2018-10-09 14:30:55 +00:00
|
|
|
auto popFrame = CreateCall<void, VMFrameStack *>([](VMFrameStack *stack) {
|
2018-10-09 03:19:29 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
stack->PopFrame();
|
2018-10-09 14:30:55 +00:00
|
|
|
CurrentJitExceptInfo->vmframes--;
|
2018-10-09 03:19:29 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2018-10-09 14:30:55 +00:00
|
|
|
VMThrowException(std::current_exception());
|
2018-10-09 03:19:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
popFrame->setArg(0, stack);
|
|
|
|
}
|
2018-10-09 01:37:11 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
void JitCompiler::EmitNullPointerThrow(int index, EVMAbortException reason)
|
|
|
|
{
|
|
|
|
auto label = cc.newLabel();
|
|
|
|
cc.test(regA[index], regA[index]);
|
|
|
|
cc.jne(label);
|
|
|
|
EmitThrowException(reason);
|
|
|
|
cc.bind(label);
|
|
|
|
}
|
2018-08-15 02:08:09 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
void JitCompiler::EmitThrowException(EVMAbortException reason)
|
|
|
|
{
|
2018-10-09 14:30:55 +00:00
|
|
|
auto call = CreateCall<void, VMScriptFunction *, VMOP *, int>([](VMScriptFunction *func, VMOP *line, int r) {
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ThrowAbortException(func, line, (EVMAbortException)r, nullptr);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
VMThrowException(std::current_exception());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
call->setArg(0, asmjit::imm_ptr(sfunc));
|
|
|
|
call->setArg(1, asmjit::imm_ptr(pc));
|
|
|
|
call->setArg(2, asmjit::imm(reason));
|
2018-09-13 00:29:04 +00:00
|
|
|
}
|
2018-08-15 02:08:09 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
void JitCompiler::EmitThrowException(EVMAbortException reason, asmjit::X86Gp arg1)
|
|
|
|
{
|
2018-10-09 14:30:55 +00:00
|
|
|
// To do: fix throw message and use arg1
|
|
|
|
EmitThrowException(reason);
|
2018-10-07 18:38:08 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
asmjit::X86Gp JitCompiler::CheckRegD(int r0, int r1)
|
2018-08-18 23:46:56 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
if (r0 != r1)
|
2018-08-18 23:46:56 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
return regD[r0];
|
2018-08-18 23:46:56 +00:00
|
|
|
}
|
2018-09-13 00:29:04 +00:00
|
|
|
else
|
2018-08-18 23:46:56 +00:00
|
|
|
{
|
2018-10-07 07:02:28 +00:00
|
|
|
auto copy = newTempInt32();
|
2018-09-13 00:29:04 +00:00
|
|
|
cc.mov(copy, regD[r0]);
|
|
|
|
return copy;
|
2018-08-18 23:46:56 +00:00
|
|
|
}
|
2018-09-13 00:29:04 +00:00
|
|
|
}
|
2018-08-12 22:15:42 +00:00
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
asmjit::X86Xmm JitCompiler::CheckRegF(int r0, int r1)
|
2018-08-18 23:46:56 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
if (r0 != r1)
|
2018-08-18 23:46:56 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
return regF[r0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-07 07:02:28 +00:00
|
|
|
auto copy = newTempXmmSd();
|
2018-09-13 00:29:04 +00:00
|
|
|
cc.movsd(copy, regF[r0]);
|
|
|
|
return copy;
|
2018-08-18 23:46:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
asmjit::X86Xmm JitCompiler::CheckRegF(int r0, int r1, int r2)
|
2018-08-18 23:46:56 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
if (r0 != r1 && r0 != r2)
|
2018-08-18 23:46:56 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
return regF[r0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-07 07:02:28 +00:00
|
|
|
auto copy = newTempXmmSd();
|
2018-09-13 00:29:04 +00:00
|
|
|
cc.movsd(copy, regF[r0]);
|
|
|
|
return copy;
|
2018-08-18 23:46:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
asmjit::X86Xmm JitCompiler::CheckRegF(int r0, int r1, int r2, int r3)
|
2018-08-30 17:55:00 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
if (r0 != r1 && r0 != r2 && r0 != r3)
|
2018-08-30 17:55:00 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
return regF[r0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-07 07:02:28 +00:00
|
|
|
auto copy = newTempXmmSd();
|
2018-09-13 00:29:04 +00:00
|
|
|
cc.movsd(copy, regF[r0]);
|
|
|
|
return copy;
|
2018-08-30 17:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 19:31:06 +00:00
|
|
|
asmjit::X86Gp JitCompiler::CheckRegS(int r0, int r1)
|
|
|
|
{
|
|
|
|
if (r0 != r1)
|
|
|
|
{
|
|
|
|
return regS[r0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-07 07:02:28 +00:00
|
|
|
auto copy = newTempIntPtr();
|
2018-09-13 19:31:06 +00:00
|
|
|
cc.mov(copy, regS[r0]);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
asmjit::X86Gp JitCompiler::CheckRegA(int r0, int r1)
|
2018-08-18 23:46:56 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
if (r0 != r1)
|
2018-08-18 23:46:56 +00:00
|
|
|
{
|
2018-09-13 00:29:04 +00:00
|
|
|
return regA[r0];
|
2018-08-12 00:11:13 +00:00
|
|
|
}
|
2018-09-13 00:29:04 +00:00
|
|
|
else
|
2018-08-12 00:11:13 +00:00
|
|
|
{
|
2018-10-07 07:02:28 +00:00
|
|
|
auto copy = newTempIntPtr();
|
2018-09-13 00:29:04 +00:00
|
|
|
cc.mov(copy, regA[r0]);
|
|
|
|
return copy;
|
2018-08-12 00:11:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-13 00:29:04 +00:00
|
|
|
|
|
|
|
void JitCompiler::EmitNOP()
|
|
|
|
{
|
|
|
|
cc.nop();
|
|
|
|
}
|