2018-09-13 00:29:04 +00:00
|
|
|
|
|
|
|
#include "jitintern.h"
|
2018-09-16 22:17:32 +00:00
|
|
|
#include "v_video.h"
|
|
|
|
#include "s_sound.h"
|
|
|
|
#include "r_state.h"
|
2018-09-13 00:29:04 +00:00
|
|
|
|
|
|
|
void JitCompiler::EmitMOVE()
|
|
|
|
{
|
|
|
|
cc.mov(regD[A], regD[B]);
|
|
|
|
}
|
|
|
|
void JitCompiler::EmitMOVEF()
|
|
|
|
{
|
|
|
|
cc.movsd(regF[A], regF[B]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JitCompiler::EmitMOVES()
|
|
|
|
{
|
2018-09-17 10:00:25 +00:00
|
|
|
auto call = CreateCall<void, FString*, FString*>(&JitCompiler::CallAssignString);
|
2018-09-13 19:31:06 +00:00
|
|
|
call->setArg(0, regS[A]);
|
|
|
|
call->setArg(1, regS[B]);
|
2018-09-13 00:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JitCompiler::EmitMOVEA()
|
|
|
|
{
|
|
|
|
cc.mov(regA[A], regA[B]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JitCompiler::EmitMOVEV2()
|
|
|
|
{
|
|
|
|
cc.movsd(regF[A], regF[B]);
|
|
|
|
cc.movsd(regF[A + 1], regF[B + 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JitCompiler::EmitMOVEV3()
|
|
|
|
{
|
|
|
|
cc.movsd(regF[A], regF[B]);
|
|
|
|
cc.movsd(regF[A + 1], regF[B + 1]);
|
|
|
|
cc.movsd(regF[A + 2], regF[B + 2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JitCompiler::EmitCAST()
|
|
|
|
{
|
2018-10-07 04:29:54 +00:00
|
|
|
asmjit::X86Gp tmp, resultD;
|
|
|
|
asmjit::X86Xmm resultF;
|
2018-09-16 22:17:32 +00:00
|
|
|
asmjit::CCFuncCall *call = nullptr;
|
|
|
|
|
2018-09-13 00:29:04 +00:00
|
|
|
switch (C)
|
|
|
|
{
|
|
|
|
case CAST_I2F:
|
|
|
|
cc.cvtsi2sd(regF[A], regD[B]);
|
|
|
|
break;
|
|
|
|
case CAST_U2F:
|
2018-10-07 07:02:28 +00:00
|
|
|
tmp = newTempInt64();
|
2018-09-13 00:29:04 +00:00
|
|
|
cc.xor_(tmp, tmp);
|
|
|
|
cc.mov(tmp.r32(), regD[B]);
|
|
|
|
cc.cvtsi2sd(regF[A], tmp);
|
|
|
|
break;
|
|
|
|
case CAST_F2I:
|
|
|
|
cc.cvttsd2si(regD[A], regF[B]);
|
|
|
|
break;
|
|
|
|
case CAST_F2U:
|
2018-10-07 07:02:28 +00:00
|
|
|
tmp = newTempInt64();
|
2018-09-13 00:29:04 +00:00
|
|
|
cc.cvttsd2si(tmp, regF[B]);
|
|
|
|
cc.mov(regD[A], tmp.r32());
|
|
|
|
break;
|
2018-09-16 22:17:32 +00:00
|
|
|
case CAST_I2S:
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<void, FString*, int>([](FString *a, int b) { a->Format("%d", b); });
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[A]);
|
|
|
|
call->setArg(1, regD[B]);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_U2S:
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<void, FString*, int>([](FString *a, int b) { a->Format("%u", b); });
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[A]);
|
|
|
|
call->setArg(1, regD[B]);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_F2S:
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<void, FString*, double>([](FString *a, double b) { a->Format("%.5f", b); });
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[A]);
|
|
|
|
call->setArg(1, regF[B]);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_V22S:
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<void, FString*, double, double>([](FString *a, double b, double b1) { a->Format("(%.5f, %.5f)", b, b1); });
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[A]);
|
|
|
|
call->setArg(1, regF[B]);
|
|
|
|
call->setArg(2, regF[B + 1]);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_V32S:
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<void, FString*, double, double, double>([](FString *a, double b, double b1, double b2) { a->Format("(%.5f, %.5f, %.5f)", b, b1, b2); });
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[A]);
|
|
|
|
call->setArg(1, regF[B]);
|
|
|
|
call->setArg(2, regF[B + 1]);
|
|
|
|
call->setArg(3, regF[B + 2]);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_P2S:
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<void, FString*, void*>([](FString *a, void *b) { if (b == nullptr) *a = "null"; else a->Format("%p", b); });
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[A]);
|
|
|
|
call->setArg(1, regA[B]);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_S2I:
|
2018-10-07 07:02:28 +00:00
|
|
|
resultD = newResultInt32();
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<int, FString*>([](FString *b) -> int { return (VM_SWORD)b->ToLong(); });
|
2018-10-07 04:29:54 +00:00
|
|
|
call->setRet(0, resultD);
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[B]);
|
2018-10-07 04:29:54 +00:00
|
|
|
cc.mov(regD[A], resultD);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_S2F:
|
2018-10-07 07:02:28 +00:00
|
|
|
resultF = newResultXmmSd();
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<double, FString*>([](FString *b) -> double { return b->ToDouble(); });
|
2018-10-07 04:29:54 +00:00
|
|
|
call->setRet(0, resultF);
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[B]);
|
2018-10-07 04:29:54 +00:00
|
|
|
cc.movsd(regF[A], resultF);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_S2N:
|
2018-10-07 07:02:28 +00:00
|
|
|
resultD = newResultInt32();
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<int, FString*>([](FString *b) -> int { return b->Len() == 0 ? FName(NAME_None) : FName(*b); });
|
2018-10-07 04:29:54 +00:00
|
|
|
call->setRet(0, resultD);
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[B]);
|
2018-10-07 04:29:54 +00:00
|
|
|
cc.mov(regD[A], resultD);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_N2S:
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<void, FString*, int>([](FString *a, int b) { FName name = FName(ENamedName(b)); *a = name.IsValidName() ? name.GetChars() : ""; });
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[A]);
|
|
|
|
call->setArg(1, regD[B]);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_S2Co:
|
2018-10-07 07:02:28 +00:00
|
|
|
resultD = newResultInt32();
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<int, FString*>([](FString *b) -> int { return V_GetColor(nullptr, *b); });
|
2018-10-07 04:29:54 +00:00
|
|
|
call->setRet(0, resultD);
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[B]);
|
2018-10-07 04:29:54 +00:00
|
|
|
cc.mov(regD[A], resultD);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_Co2S:
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<void, FString*, int>([](FString *a, int b) { PalEntry c(b); a->Format("%02x %02x %02x", c.r, c.g, c.b); });
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[A]);
|
|
|
|
call->setArg(1, regD[B]);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_S2So:
|
2018-10-07 07:02:28 +00:00
|
|
|
resultD = newResultInt32();
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<int, FString*>([](FString *b) -> int { return FSoundID(*b); });
|
2018-10-07 04:29:54 +00:00
|
|
|
call->setRet(0, resultD);
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[B]);
|
2018-10-07 04:29:54 +00:00
|
|
|
cc.mov(regD[A], resultD);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_So2S:
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<void, FString*, int>([](FString *a, int b) { *a = S_sfx[b].name; });
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[A]);
|
|
|
|
call->setArg(1, regD[B]);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_SID2S:
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<void, FString*, unsigned int>([](FString *a, unsigned int b) { *a = (b >= sprites.Size()) ? "TNT1" : sprites[b].name; });
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[A]);
|
|
|
|
call->setArg(1, regD[B]);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
case CAST_TID2S:
|
2018-09-17 10:00:25 +00:00
|
|
|
call = CreateCall<void, FString*, int>([](FString *a, int b) { auto tex = TexMan[*(FTextureID*)&b]; *a = (tex == nullptr) ? "(null)" : tex->Name.GetChars(); });
|
2018-09-16 22:17:32 +00:00
|
|
|
call->setArg(0, regS[A]);
|
|
|
|
call->setArg(1, regD[B]);
|
2018-09-13 00:29:04 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-09-16 22:17:32 +00:00
|
|
|
I_FatalError("Unknown OP_CAST type\n");
|
2018-09-13 00:29:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JitCompiler::EmitCASTB()
|
|
|
|
{
|
|
|
|
if (C == CASTB_I)
|
|
|
|
{
|
2018-10-11 02:32:49 +00:00
|
|
|
cc.cmp(regD[B], (int)0);
|
|
|
|
cc.setne(regD[A]);
|
|
|
|
cc.movzx(regD[A], regD[A].r8Lo()); // not sure if this is needed
|
2018-09-13 00:29:04 +00:00
|
|
|
}
|
|
|
|
else if (C == CASTB_F)
|
|
|
|
{
|
2018-10-07 07:02:28 +00:00
|
|
|
auto zero = newTempXmmSd();
|
|
|
|
auto one = newTempInt32();
|
2018-09-13 00:29:04 +00:00
|
|
|
cc.xorpd(zero, zero);
|
|
|
|
cc.mov(one, 1);
|
2018-10-11 02:32:49 +00:00
|
|
|
cc.xor_(regD[A], regD[A]);
|
|
|
|
cc.ucomisd(regF[B], zero);
|
2018-09-13 00:29:04 +00:00
|
|
|
cc.setp(regD[A]);
|
|
|
|
cc.cmovne(regD[A], one);
|
|
|
|
}
|
|
|
|
else if (C == CASTB_A)
|
|
|
|
{
|
2018-10-11 02:32:49 +00:00
|
|
|
cc.test(regA[B], regA[B]);
|
2018-09-13 00:29:04 +00:00
|
|
|
cc.setne(regD[A]);
|
2018-10-11 02:32:49 +00:00
|
|
|
cc.movzx(regD[A], regD[A].r8Lo()); // not sure if this is needed
|
2018-09-13 00:29:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-07 07:02:28 +00:00
|
|
|
auto result = newResultInt32();
|
2018-09-17 10:00:25 +00:00
|
|
|
auto call = CreateCall<int, FString*>([](FString *s) -> int { return s->Len() > 0; });
|
2018-10-07 04:29:54 +00:00
|
|
|
call->setRet(0, result);
|
2018-09-16 20:39:48 +00:00
|
|
|
call->setArg(0, regS[B]);
|
2018-10-07 04:29:54 +00:00
|
|
|
cc.mov(regD[A], result);
|
2018-09-13 00:29:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JitCompiler::EmitDYNCAST_R()
|
|
|
|
{
|
2018-10-07 07:02:28 +00:00
|
|
|
auto result = newResultIntPtr();
|
2018-09-17 10:00:25 +00:00
|
|
|
auto call = CreateCall<DObject*, DObject*, PClass*>([](DObject *obj, PClass *cls) -> DObject* {
|
2018-09-13 00:29:04 +00:00
|
|
|
return (obj && obj->IsKindOf(cls)) ? obj : nullptr;
|
2018-09-17 10:00:25 +00:00
|
|
|
});
|
2018-10-07 04:29:54 +00:00
|
|
|
call->setRet(0, result);
|
2018-09-13 00:29:04 +00:00
|
|
|
call->setArg(0, regA[B]);
|
|
|
|
call->setArg(1, regA[C]);
|
2018-10-07 04:29:54 +00:00
|
|
|
cc.mov(regA[A], result);
|
2018-09-13 00:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JitCompiler::EmitDYNCAST_K()
|
|
|
|
{
|
2018-10-07 07:02:28 +00:00
|
|
|
auto result = newResultIntPtr();
|
|
|
|
auto c = newTempIntPtr();
|
2018-09-18 16:13:53 +00:00
|
|
|
cc.mov(c, asmjit::imm_ptr(konsta[C].o));
|
2018-09-17 10:00:25 +00:00
|
|
|
auto call = CreateCall<DObject*, DObject*, PClass*>([](DObject *obj, PClass *cls) -> DObject* {
|
2018-09-13 00:29:04 +00:00
|
|
|
return (obj && obj->IsKindOf(cls)) ? obj : nullptr;
|
2018-09-17 10:00:25 +00:00
|
|
|
});
|
2018-10-07 04:29:54 +00:00
|
|
|
call->setRet(0, result);
|
2018-09-13 00:29:04 +00:00
|
|
|
call->setArg(0, regA[B]);
|
|
|
|
call->setArg(1, c);
|
2018-10-07 04:29:54 +00:00
|
|
|
cc.mov(regA[A], result);
|
2018-09-13 00:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JitCompiler::EmitDYNCASTC_R()
|
|
|
|
{
|
2018-10-07 07:02:28 +00:00
|
|
|
auto result = newResultIntPtr();
|
2018-09-17 10:00:25 +00:00
|
|
|
auto call = CreateCall<PClass*, PClass*, PClass*>([](PClass *cls1, PClass *cls2) -> PClass* {
|
2018-09-13 00:29:04 +00:00
|
|
|
return (cls1 && cls1->IsDescendantOf(cls2)) ? cls1 : nullptr;
|
2018-09-17 10:00:25 +00:00
|
|
|
});
|
2018-10-07 04:29:54 +00:00
|
|
|
call->setRet(0, result);
|
2018-09-13 00:29:04 +00:00
|
|
|
call->setArg(0, regA[B]);
|
|
|
|
call->setArg(1, regA[C]);
|
2018-10-07 04:29:54 +00:00
|
|
|
cc.mov(regA[A], result);
|
2018-09-13 00:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JitCompiler::EmitDYNCASTC_K()
|
|
|
|
{
|
|
|
|
using namespace asmjit;
|
2018-10-07 07:02:28 +00:00
|
|
|
auto result = newResultIntPtr();
|
|
|
|
auto c = newTempIntPtr();
|
2018-09-18 16:13:53 +00:00
|
|
|
cc.mov(c, asmjit::imm_ptr(konsta[C].o));
|
2018-09-13 00:29:04 +00:00
|
|
|
typedef PClass*(*FuncPtr)(PClass*, PClass*);
|
2018-09-17 10:00:25 +00:00
|
|
|
auto call = CreateCall<PClass*, PClass*, PClass*>([](PClass *cls1, PClass *cls2) -> PClass* {
|
2018-09-13 00:29:04 +00:00
|
|
|
return (cls1 && cls1->IsDescendantOf(cls2)) ? cls1 : nullptr;
|
2018-09-17 10:00:25 +00:00
|
|
|
});
|
2018-10-07 04:29:54 +00:00
|
|
|
call->setRet(0, result);
|
2018-09-13 00:29:04 +00:00
|
|
|
call->setArg(0, regA[B]);
|
|
|
|
call->setArg(1, c);
|
2018-10-07 04:29:54 +00:00
|
|
|
cc.mov(regA[A], result);
|
2018-09-13 00:29:04 +00:00
|
|
|
}
|