Skip to content

Commit

Permalink
Replace the native call templates with CreateNativeFunction to make i…
Browse files Browse the repository at this point in the history
…t more explicit which IR types are actually used
  • Loading branch information
dpjudas committed Jun 5, 2020
1 parent d3288a8 commit 1c9df61
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 339 deletions.
230 changes: 197 additions & 33 deletions src/common/scripting/jit/jit.cpp

Large diffs are not rendered by default.

33 changes: 17 additions & 16 deletions src/common/scripting/jit/jit_call.cpp
Expand Up @@ -85,7 +85,8 @@ void JitCompiler::EmitVMCall(IRValue* vmfunc, VMFunction* target)
IRValue* paramsptr = OffsetPtr(vmframe, offsetParams);
IRValue* scriptcall = Load(ToInt8PtrPtr(vmfunc, myoffsetof(VMScriptFunction, ScriptCall)));

IRInst* call = cc.CreateCall(cc.CreateBitCast(scriptcall, GetFunctionType5<int, VMFunction*, VMValue*, int, VMReturn*, int>()), { vmfunc, paramsptr, ConstValueD(B), GetCallReturns(), ConstValueD(C) });
IRFunctionType* functype = ircontext->getFunctionType(int32Ty, { int8PtrTy, int8PtrTy, int32Ty, int8PtrTy, int32Ty });
IRInst* call = cc.CreateCall(cc.CreateBitCast(scriptcall, functype), { vmfunc, paramsptr, ConstValueD(B), GetCallReturns(), ConstValueD(C) });
call->comment = std::string("call ") + (target ? target->PrintableName.GetChars() : "VMCall");

LoadInOuts();
Expand Down Expand Up @@ -518,7 +519,7 @@ IRFunctionType* JitCompiler::GetFuncSignature()
{
if (ParamOpcodes[i]->op == OP_PARAMI)
{
args.push_back(ircontext->getInt32Ty());
args.push_back(int32Ty);
}
else // OP_PARAM
{
Expand All @@ -532,28 +533,28 @@ IRFunctionType* JitCompiler::GetFuncSignature()
case REGT_INT | REGT_ADDROF:
case REGT_POINTER | REGT_ADDROF:
case REGT_FLOAT | REGT_ADDROF:
args.push_back(ircontext->getInt8PtrTy());
args.push_back(int8PtrTy);
break;
case REGT_INT:
case REGT_INT | REGT_KONST:
args.push_back(ircontext->getInt32Ty());
args.push_back(int32Ty);
break;
case REGT_STRING:
case REGT_STRING | REGT_KONST:
args.push_back(ircontext->getInt8PtrTy());
args.push_back(int8PtrTy);
break;
case REGT_FLOAT:
case REGT_FLOAT | REGT_KONST:
args.push_back(ircontext->getDoublePtrTy());
args.push_back(doublePtrTy);
break;
case REGT_FLOAT | REGT_MULTIREG2:
args.push_back(ircontext->getDoublePtrTy());
args.push_back(ircontext->getDoublePtrTy());
args.push_back(doublePtrTy);
args.push_back(doublePtrTy);
break;
case REGT_FLOAT | REGT_MULTIREG3:
args.push_back(ircontext->getDoublePtrTy());
args.push_back(ircontext->getDoublePtrTy());
args.push_back(ircontext->getDoublePtrTy());
args.push_back(doublePtrTy);
args.push_back(doublePtrTy);
args.push_back(doublePtrTy);
break;

default:
Expand All @@ -566,7 +567,7 @@ IRFunctionType* JitCompiler::GetFuncSignature()
const VMOP *retval = pc + 1;
int numret = C;

IRType* rettype = ircontext->getVoidTy();
IRType* rettype = voidTy;

// Check if first return value can be placed in the function's real return value slot
int startret = 1;
Expand All @@ -581,13 +582,13 @@ IRFunctionType* JitCompiler::GetFuncSignature()
switch (type)
{
case REGT_INT:
rettype = ircontext->getInt32Ty();
rettype = int32Ty;
break;
case REGT_FLOAT:
rettype = ircontext->getDoublePtrTy();
rettype = doublePtrTy;
break;
case REGT_POINTER:
rettype = ircontext->getInt8PtrTy();
rettype = int8PtrTy;
break;
case REGT_STRING:
default:
Expand All @@ -604,7 +605,7 @@ IRFunctionType* JitCompiler::GetFuncSignature()
I_Error("Expected OP_RESULT to follow OP_CALL\n");
}

args.push_back(ircontext->getInt8PtrTy());
args.push_back(int8PtrTy);
}

return ircontext->getFunctionType(rettype, args);
Expand Down
32 changes: 5 additions & 27 deletions src/common/scripting/jit/jit_flow.cpp
Expand Up @@ -50,23 +50,13 @@ void JitCompiler::EmitIJMP()
EmitThrowException(X_OTHER);
}

static void ValidateCall(DObject *o, VMFunction *f, int b)
{
FScopeBarrier::ValidateCall(o->GetClass(), f, b - 1);
}

void JitCompiler::EmitSCOPE()
{
auto continuebb = irfunc->createBasicBlock({});
auto exceptionbb = EmitThrowExceptionLabel(X_READ_NIL);
cc.CreateCondBr(cc.CreateICmpEQ(LoadD(A), ConstValueA(0)), exceptionbb, continuebb);
cc.SetInsertPoint(continuebb);
cc.CreateCall(GetNativeFunc<void, DObject*, VMFunction*, int>("__ValidateCall", ValidateCall), { LoadA(A), ConstA(C), ConstValueD(B) });
}

static void SetString(VMReturn* ret, FString* str)
{
ret->SetString(*str);
cc.CreateCall(validateCall, { LoadA(A), ConstA(C), ConstValueD(B) });
}

void JitCompiler::EmitRET()
Expand Down Expand Up @@ -139,7 +129,7 @@ void JitCompiler::EmitRET()
break;
case REGT_STRING:
{
cc.CreateCall(GetNativeFunc<void, VMReturn*, FString*>("__SetString", SetString), { location, (regtype & REGT_KONST) ? ConstS(regnum) : LoadS(regnum) });
cc.CreateCall(setReturnString, { location, (regtype & REGT_KONST) ? ConstS(regnum) : LoadS(regnum) });
break;
}
case REGT_POINTER:
Expand Down Expand Up @@ -218,7 +208,7 @@ void JitCompiler::EmitBOUND()
IRBasicBlock* exceptionbb = irfunc->createBasicBlock({});
cc.CreateCondBr(cc.CreateICmpUGE(LoadD(A), ConstValueD(BC)), exceptionbb, continuebb);
cc.SetInsertPoint(exceptionbb);
cc.CreateCall(GetNativeFunc<void, int, int>("__ThrowArrayOutOfBounds", &JitCompiler::ThrowArrayOutOfBounds), { LoadD(A), ConstValueD(BC) });
cc.CreateCall(throwArrayOutOfBounds, { LoadD(A), ConstValueD(BC) });
exceptionbb->code.front()->lineNumber = sfunc->PCToLine(pc);
cc.CreateBr(continuebb);
cc.SetInsertPoint(continuebb);
Expand All @@ -230,7 +220,7 @@ void JitCompiler::EmitBOUND_K()
IRBasicBlock* exceptionbb = irfunc->createBasicBlock({});
cc.CreateCondBr(cc.CreateICmpUGE(LoadD(A), ConstD(BC)), exceptionbb, continuebb);
cc.SetInsertPoint(exceptionbb);
cc.CreateCall(GetNativeFunc<void, int, int>("__ThrowArrayOutOfBounds", &JitCompiler::ThrowArrayOutOfBounds), { LoadD(A), ConstD(BC) });
cc.CreateCall(throwArrayOutOfBounds, { LoadD(A), ConstD(BC) });
exceptionbb->code.front()->lineNumber = sfunc->PCToLine(pc);
cc.CreateBr(continuebb);
cc.SetInsertPoint(continuebb);
Expand All @@ -242,20 +232,8 @@ void JitCompiler::EmitBOUND_R()
IRBasicBlock* exceptionbb = irfunc->createBasicBlock({});
cc.CreateCondBr(cc.CreateICmpUGE(LoadD(A), LoadD(B)), exceptionbb, continuebb);
cc.SetInsertPoint(exceptionbb);
cc.CreateCall(GetNativeFunc<void, int, int>("__ThrowArrayOutOfBounds", &JitCompiler::ThrowArrayOutOfBounds), { LoadD(A), LoadD(BC) });
cc.CreateCall(throwArrayOutOfBounds, { LoadD(A), LoadD(BC) });
exceptionbb->code.front()->lineNumber = sfunc->PCToLine(pc);
cc.CreateBr(continuebb);
cc.SetInsertPoint(continuebb);
}

void JitCompiler::ThrowArrayOutOfBounds(int index, int size)
{
if (index >= size)
{
ThrowAbortException(X_ARRAY_OUT_OF_BOUNDS, "Max.index = %u, current index = %u\n", size, index);
}
else
{
ThrowAbortException(X_ARRAY_OUT_OF_BOUNDS, "Negative current index = %i\n", index);
}
}
36 changes: 13 additions & 23 deletions src/common/scripting/jit/jit_load.cpp
Expand Up @@ -21,7 +21,7 @@ void JitCompiler::EmitLKF()

void JitCompiler::EmitLKS()
{
cc.CreateCall(GetNativeFunc<void, FString*, FString*>("__CallAssignString", &JitCompiler::CallAssignString), { LoadS(A), ConstS(BC) });
cc.CreateCall(stringAssignmentOperator, { LoadS(A), ConstS(BC) });
}

void JitCompiler::EmitLKP()
Expand All @@ -31,25 +31,25 @@ void JitCompiler::EmitLKP()

void JitCompiler::EmitLK_R()
{
IRValue* base = ircontext->getConstantInt(ircontext->getInt32PtrTy(), (uint64_t)&konstd[C]);
IRValue* base = ircontext->getConstantInt(int32PtrTy, (uint64_t)&konstd[C]);
StoreD(Load(OffsetPtr(base, LoadD(B))), A);
}

void JitCompiler::EmitLKF_R()
{
IRValue* base = ircontext->getConstantInt(ircontext->getDoublePtrTy(), (uint64_t)&konstf[C]);
IRValue* base = ircontext->getConstantInt(doublePtrTy, (uint64_t)&konstf[C]);
StoreF(Load(OffsetPtr(base, LoadD(B))), A);
}

void JitCompiler::EmitLKS_R()
{
IRValue* base = ircontext->getConstantInt(ircontext->getInt8PtrTy()->getPointerTo(ircontext), (uint64_t)&konsts[C]);
cc.CreateCall(GetNativeFunc<void, FString*, FString*>("__CallAssignString", &JitCompiler::CallAssignString), { LoadS(A), Load(OffsetPtr(base, LoadD(B))) });
IRValue* base = ircontext->getConstantInt(int8PtrPtrTy, (uint64_t)&konsts[C]);
cc.CreateCall(stringAssignmentOperator, { LoadS(A), Load(OffsetPtr(base, LoadD(B))) });
}

void JitCompiler::EmitLKP_R()
{
IRValue* base = ircontext->getConstantInt(ircontext->getInt8PtrTy()->getPointerTo(ircontext), (uint64_t)&konsta[C]);
IRValue* base = ircontext->getConstantInt(int8PtrPtrTy, (uint64_t)&konsta[C]);
StoreA(Load(OffsetPtr(base, LoadD(B))), A);
}

Expand Down Expand Up @@ -174,13 +174,13 @@ void JitCompiler::EmitLDP_R()
void JitCompiler::EmitLS()
{
EmitNullPointerThrow(B, X_READ_NIL);
cc.CreateCall(GetNativeFunc<void, FString*, FString*>("__CallAssignString", &JitCompiler::CallAssignString), { LoadS(A), OffsetPtr(LoadA(B), ConstD(C)) });
cc.CreateCall(stringAssignmentOperator, { LoadS(A), OffsetPtr(LoadA(B), ConstD(C)) });
}

void JitCompiler::EmitLS_R()
{
EmitNullPointerThrow(B, X_READ_NIL);
cc.CreateCall(GetNativeFunc<void, FString*, FString*>("__CallAssignString", &JitCompiler::CallAssignString), { LoadS(A), OffsetPtr(LoadA(B), LoadD(C)) });
cc.CreateCall(stringAssignmentOperator, { LoadS(A), OffsetPtr(LoadA(B), LoadD(C)) });
}

#if 0 // Inline read barrier impl
Expand Down Expand Up @@ -219,21 +219,16 @@ void JitCompiler::EmitLO_R()

#else

static DObject *ReadBarrier(DObject *p)
{
return GC::ReadBarrier(p);
}

void JitCompiler::EmitLO()
{
EmitNullPointerThrow(B, X_READ_NIL);
StoreA(cc.CreateCall(GetNativeFunc<DObject*, DObject*>("__ReadBarrier", ReadBarrier), { OffsetPtr(LoadA(B), ConstD(C)) }), A);
StoreA(cc.CreateCall(readBarrier, { OffsetPtr(LoadA(B), ConstD(C)) }), A);
}

void JitCompiler::EmitLO_R()
{
EmitNullPointerThrow(B, X_READ_NIL);
StoreA(cc.CreateCall(GetNativeFunc<DObject*, DObject*>("__ReadBarrier", ReadBarrier), { OffsetPtr(LoadA(B), LoadD(C)) }), A);
StoreA(cc.CreateCall(readBarrier, { OffsetPtr(LoadA(B), LoadD(C)) }), A);
}

#endif
Expand Down Expand Up @@ -284,21 +279,16 @@ void JitCompiler::EmitLV3_R()
StoreF(Load(OffsetPtr(base, 2)), A + 2);
}

static void SetString(FString *to, char **from)
{
*to = *from;
}

void JitCompiler::EmitLCS()
{
EmitNullPointerThrow(B, X_READ_NIL);
cc.CreateCall(GetNativeFunc<void, FString*, char**>("__SetString", SetString), { LoadS(A), OffsetPtr(LoadA(B), ConstD(C)) });
cc.CreateCall(stringAssignmentOperatorCStr, { LoadS(A), OffsetPtr(LoadA(B), ConstD(C)) });
}

void JitCompiler::EmitLCS_R()
{
EmitNullPointerThrow(B, X_READ_NIL);
cc.CreateCall(GetNativeFunc<void, FString*, char**>("__SetString", SetString), { LoadS(A), OffsetPtr(LoadA(B), LoadD(C)) });
cc.CreateCall(stringAssignmentOperatorCStr, { LoadS(A), OffsetPtr(LoadA(B), LoadD(C)) });
}

void JitCompiler::EmitLBIT()
Expand All @@ -307,6 +297,6 @@ void JitCompiler::EmitLBIT()
IRValue* value = Load(LoadA(B));
value = cc.CreateAnd(value, ircontext->getConstantInt(C));
value = cc.CreateICmpNE(value, ircontext->getConstantInt(0));
value = cc.CreateZExt(value, ircontext->getInt32Ty());
value = cc.CreateZExt(value, int32Ty);
StoreD(value, A);
}

0 comments on commit 1c9df61

Please sign in to comment.