Skip to content

Commit 0846c27

Browse files
committed
Fix C++03 compilation, fix druntime calls to check for sret using helperfunction.
Enable sret-arrays for Win64
1 parent a4db065 commit 0846c27

8 files changed

+294
-215
lines changed

gen/aa.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ DValue* DtoAAIndex(Loc& loc, Type* type, DValue* aa, DValue* key, bool lvalue)
101101
};
102102

103103
// call
104-
llvm::Function* errorfn = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_arraybounds");
105-
gIR->CreateCallOrInvoke(errorfn, args);
104+
LLVM_D_CallRuntimeFunction(loc, "_d_arraybounds", args);
106105

107106
// the function does not return
108107
gIR->ir->CreateUnreachable();

gen/abi-win64.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct Win64TargetABI : TargetABI
5555
bool isAggregate(Type* t)
5656
{
5757
TY ty = t->ty;
58-
return ty == Tstruct || ty == Tsarray || /*ty == Tarray ||*/ ty == Tdelegate
58+
return ty == Tstruct || ty == Tsarray || ty == Tarray || ty == Tdelegate
5959
|| t->iscomplex();
6060
}
6161

gen/abi-x86-64.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ struct X86_64TargetABI : TargetABI {
221221

222222
llvm::CallingConv::ID callingConv(LINK l);
223223

224-
bool returnInArg(Type* tf, LINK linkage);
224+
bool returnInArg(Type* rt, LINK linkage);
225225

226226
bool passByVal(Type* t);
227227

gen/arrays.cpp

+36-43
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,14 @@ void DtoArrayAssign(Loc& loc, DValue *array, DValue *value, int op)
213213
assert(t->nextOf());
214214
Type *elemType = t->nextOf()->toBasetype();
215215

216-
LLFunction* fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, op == TOKconstruct ? "_d_arrayctor" : "_d_arrayassign");
217216
LLValue* args[] = {
218217
DtoTypeInfoOf(elemType),
219-
DtoAggrPaint(DtoSlice(value), fn->getFunctionType()->getParamType(1)),
220-
DtoAggrPaint(DtoSlice(array), fn->getFunctionType()->getParamType(2))
218+
DtoAggrPaint(DtoSlice(value), DtoType(Type::tvoid->arrayOf())),
219+
DtoAggrPaint(DtoSlice(array), DtoType(Type::tvoid->arrayOf()))
221220
};
222221

223-
LLCallSite call = gIR->CreateCallOrInvoke(fn, args, ".array");
224-
call.setCallingConv(llvm::CallingConv::C);
222+
LLVM_D_CallRuntimeFunction(loc, (op == TOKconstruct) ? "_d_arrayctor" : "_d_arrayassign",
223+
args, ".array");
225224
}
226225

227226
// If op is TOKconstruct, does construction of an array;
@@ -237,16 +236,15 @@ void DtoArraySetAssign(Loc& loc, DValue *array, DValue *value, int op)
237236
LLValue *ptr = DtoArrayPtr(array);
238237
LLValue *len = DtoArrayLen(array);
239238

240-
LLFunction* fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, op == TOKconstruct ? "_d_arraysetctor" : "_d_arraysetassign");
241239
LLValue* args[] = {
242240
DtoBitCast(ptr, getVoidPtrType()),
243241
DtoBitCast(makeLValue(loc, value), getVoidPtrType()),
244242
len,
245243
DtoTypeInfoOf(array->type->toBasetype()->nextOf()->toBasetype())
246244
};
247245

248-
LLCallSite call = gIR->CreateCallOrInvoke(fn, args, ".newptr");
249-
call.setCallingConv(llvm::CallingConv::C);
246+
LLVM_D_CallRuntimeFunction(loc, (op == TOKconstruct) ? "_d_arraysetctor" : "_d_arraysetassign",
247+
args, ".newptr");
250248
}
251249

252250
//////////////////////////////////////////////////////////////////////////////////////////
@@ -510,8 +508,8 @@ static void copySlice(Loc& loc, LLValue* dstarr, LLValue* sz1, LLValue* srcarr,
510508
{
511509
if (global.params.useAssert || gIR->emitArrayBoundsChecks())
512510
{
513-
LLValue* fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_array_slice_copy");
514-
gIR->CreateCallOrInvoke4(fn, dstarr, sz1, srcarr, sz2);
511+
LLValue* args[] = { dstarr, sz1, srcarr, sz2 };
512+
LLVM_D_CallRuntimeFunction(loc, "_d_array_slice_copy", args);
515513
}
516514
else
517515
{
@@ -624,10 +622,9 @@ DSliceValue* DtoNewDynArray(Loc& loc, Type* arrayType, DValue* dim, bool default
624622
const char* fnname = defaultInit ?
625623
(zeroInit ? "_d_newarrayT" : "_d_newarrayiT") :
626624
"_d_newarrayU";
627-
LLFunction* fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, fnname);
628625

629-
// call allocator
630-
LLValue* newArray = gIR->CreateCallOrInvoke2(fn, arrayTypeInfo, arrayLen, ".gc_mem").getInstruction();
626+
LLValue* args[] = { arrayTypeInfo, arrayLen };
627+
LLValue* newArray = LLVM_D_CallRuntimeFunction(loc, fnname, args, ".gc_mem");
631628

632629
return getSlice(arrayType, newArray);
633630
}
@@ -653,9 +650,7 @@ DSliceValue* DtoNewMulDimDynArray(Loc& loc, Type* arrayType, DValue** dims, size
653650

654651
const char* fnname = zeroInit ? "_d_newarraymT" : "_d_newarraymiT";
655652

656-
LLFunction* fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, fnname);
657-
658-
std::vector<LLValue*> args;
653+
llvm::SmallVector<llvm::Value*,4> args;
659654
args.reserve(ndims+2);
660655
args.push_back(arrayTypeInfo);
661656
args.push_back(DtoConstSize_t(ndims));
@@ -665,7 +660,7 @@ DSliceValue* DtoNewMulDimDynArray(Loc& loc, Type* arrayType, DValue** dims, size
665660
args.push_back(dims[i]->getRVal());
666661

667662
// call allocator
668-
LLValue* newptr = gIR->CreateCallOrInvoke(fn, args, ".gc_mem").getInstruction();
663+
LLValue* newptr = LLVM_D_CallRuntimeFunction(loc, fnname, args, ".gc_mem");
669664

670665
IF_LOG Logger::cout() << "final ptr = " << *newptr << '\n';
671666

@@ -687,14 +682,13 @@ DSliceValue* DtoResizeDynArray(Loc& loc, Type* arrayType, DValue* array, LLValue
687682
bool zeroInit = arrayType->toBasetype()->nextOf()->isZeroInit();
688683

689684
// call runtime
690-
LLFunction* fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, zeroInit ? "_d_arraysetlengthT" : "_d_arraysetlengthiT" );
691-
685+
const char* fnname = zeroInit ? "_d_arraysetlengthT" : "_d_arraysetlengthiT";
692686
LLValue* args[] = {
693687
DtoTypeInfoOf(arrayType),
694688
newdim,
695-
DtoBitCast(array->getLVal(), fn->getFunctionType()->getParamType(2))
689+
DtoBitCast(array->getLVal(), DtoType(Type::tvoid->arrayOf()->pointerTo()))
696690
};
697-
LLValue* newArray = gIR->CreateCallOrInvoke(fn, args, ".gc_mem").getInstruction();
691+
LLValue* newArray = LLVM_D_CallRuntimeFunction(loc, fnname, args, ".gc_mem");
698692

699693
return getSlice(arrayType, newArray);
700694
}
@@ -714,14 +708,13 @@ void DtoCatAssignElement(Loc& loc, Type* arrayType, DValue* array, Expression* e
714708
// otherwise a ~= a[$-i] won't work correctly
715709
DValue *expVal = toElem(exp);
716710

717-
LLFunction* fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_arrayappendcTX");
718711
LLValue* args[] = {
719712
DtoTypeInfoOf(arrayType),
720-
DtoBitCast(array->getLVal(), fn->getFunctionType()->getParamType(1)),
713+
DtoBitCast(array->getLVal(), DtoType(Type::tint8->arrayOf()->pointerTo())),
721714
DtoConstSize_t(1)
722715
};
716+
LLValue* appendedArray = LLVM_D_CallRuntimeFunction(loc, "_d_arrayappendcTX", args, ".appendedArray");
723717

724-
LLValue* appendedArray = gIR->CreateCallOrInvoke(fn, args, ".appendedArray").getInstruction();
725718
appendedArray = DtoAggrPaint(appendedArray, DtoType(arrayType));
726719

727720
LLValue* val = DtoArrayPtr(array);
@@ -739,19 +732,18 @@ DSliceValue* DtoCatAssignArray(Loc& loc, DValue* arr, Expression* exp)
739732
Type *arrayType = arr->getType();
740733

741734
// Prepare arguments
742-
LLFunction* fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_arrayappendT");
743735
LLSmallVector<LLValue*,3> args;
744736
// TypeInfo ti
745737
args.push_back(DtoTypeInfoOf(arrayType));
746738
// byte[] *px
747-
args.push_back(DtoBitCast(arr->getLVal(), fn->getFunctionType()->getParamType(1)));
739+
args.push_back(DtoBitCast(arr->getLVal(), DtoType(Type::tint8->arrayOf()->pointerTo())));
748740
// byte[] y
749741
LLValue *y = DtoSlice(toElem(exp));
750-
y = DtoAggrPaint(y, fn->getFunctionType()->getParamType(2));
742+
y = DtoAggrPaint(y, DtoType(Type::tint8->arrayOf()));
751743
args.push_back(y);
752744

753745
// Call _d_arrayappendT
754-
LLValue* newArray = gIR->CreateCallOrInvoke(fn, args, ".appendedArray").getInstruction();
746+
LLValue* newArray = LLVM_D_CallRuntimeFunction(loc, "_d_arrayappendT", args, ".appendedArray");
755747

756748
return getSlice(arrayType, newArray);
757749
}
@@ -765,10 +757,11 @@ DSliceValue* DtoCatArrays(Loc& loc, Type* arrayType, Expression* exp1, Expressio
765757

766758
std::vector<LLValue*> args;
767759
LLFunction* fn = 0;
760+
const char* fname = NULL;
768761

769762
if (exp1->op == TOKcat)
770763
{ // handle multiple concat
771-
fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_arraycatnT");
764+
fname = "_d_arraycatnT";
772765

773766
args.push_back(DtoSlicePtr(toElem(exp2)));
774767
CatExp *ce = static_cast<CatExp*>(exp1);
@@ -788,21 +781,21 @@ DSliceValue* DtoCatArrays(Loc& loc, Type* arrayType, Expression* exp1, Expressio
788781
}
789782
else
790783
{
791-
fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_arraycatT");
784+
fname = "_d_arraycatT";
792785

793786
// TypeInfo ti
794787
args.push_back(DtoTypeInfoOf(arrayType));
795788
// byte[] x
796789
LLValue *val = DtoLoad(DtoSlicePtr(toElem(exp1)));
797-
val = DtoAggrPaint(val, fn->getFunctionType()->getParamType(1));
790+
val = DtoAggrPaint(val, DtoType(Type::tint8->arrayOf()));
798791
args.push_back(val);
799792
// byte[] y
800793
val = DtoLoad(DtoSlicePtr(toElem(exp2)));
801-
val = DtoAggrPaint(val, fn->getFunctionType()->getParamType(2));
794+
val = DtoAggrPaint(val, DtoType(Type::tint8->arrayOf()));
802795
args.push_back(val);
803796
}
804797

805-
LLValue *newArray = gIR->CreateCallOrInvoke(fn, args, ".appendedArray").getInstruction();
798+
LLValue *newArray = LLVM_D_CallRuntimeFunction(loc, fname, args, ".appendedArray");
806799
return getSlice(arrayType, newArray);
807800
}
808801

@@ -815,15 +808,16 @@ DSliceValue* DtoAppendDChar(Loc& loc, DValue* arr, Expression* exp, const char *
815808

816809
// Prepare arguments
817810
LLFunction* fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, func);
811+
unsigned arg_offset = fn->hasStructRetAttr() ? 1 : 0; // argument numbering offset due to sret
818812
LLValue* args[] = {
819813
// ref string x
820-
DtoBitCast(arr->getLVal(), fn->getFunctionType()->getParamType(0)),
814+
DtoBitCast(arr->getLVal(), fn->getFunctionType()->getParamType(arg_offset + 0)),
821815
// dchar c
822-
DtoBitCast(valueToAppend->getRVal(), fn->getFunctionType()->getParamType(1))
816+
DtoBitCast(valueToAppend->getRVal(), fn->getFunctionType()->getParamType(arg_offset + 1))
823817
};
824818

825819
// Call function
826-
LLValue* newArray = gIR->CreateCallOrInvoke(fn, args, ".appendedArray").getInstruction();
820+
LLValue* newArray = LLVM_D_CallRuntimeFunction(loc, func, args, ".appendedArray");
827821

828822
return getSlice(arrayType, newArray);
829823
}
@@ -853,6 +847,7 @@ static LLValue* DtoArrayEqCmp_impl(Loc& loc, const char* func, DValue* l, DValue
853847
IF_LOG Logger::println("comparing arrays");
854848
LLFunction* fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, func);
855849
assert(fn);
850+
unsigned arg_offset = fn->hasStructRetAttr() ? 1 : 0; // argument numbering offset due to sret
856851

857852
// find common dynamic array type
858853
Type* commonType = l->getType()->toBasetype()->nextOf()->arrayOf();
@@ -862,7 +857,7 @@ static LLValue* DtoArrayEqCmp_impl(Loc& loc, const char* func, DValue* l, DValue
862857
l = DtoCastArray(loc, l, commonType);
863858
r = DtoCastArray(loc, r, commonType);
864859

865-
LLSmallVector<LLValue*, 3> args;
860+
LLSmallVector<LLValue*, 4> args;
866861

867862
// get values, reinterpret cast to void[]
868863
args.push_back(DtoAggrPaint(l->getRVal(), DtoArrayType(LLType::getInt8Ty(gIR->context()))));
@@ -872,10 +867,10 @@ static LLValue* DtoArrayEqCmp_impl(Loc& loc, const char* func, DValue* l, DValue
872867
if (useti) {
873868
Type* t = l->getType();
874869
LLValue* tival = DtoTypeInfoOf(t);
875-
args.push_back(DtoBitCast(tival, fn->getFunctionType()->getParamType(2)));
870+
args.push_back(DtoBitCast(tival, fn->getFunctionType()->getParamType(arg_offset + 2)));
876871
}
877872

878-
LLCallSite call = gIR->CreateCallOrInvoke(fn, args);
873+
LLCallSite call = LLVM_D_CallRuntimeFunction(loc, func, args);
879874

880875
return call.getInstruction();
881876
}
@@ -933,8 +928,7 @@ LLValue* DtoArrayCastLength(Loc& loc, LLValue* len, LLType* elemty, LLType* newe
933928
LLConstantInt::get(DtoSize_t(), nsz, false)
934929
};
935930

936-
LLFunction* fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_array_cast_len");
937-
return gIR->CreateCallOrInvoke(fn, args).getInstruction();
931+
return LLVM_D_CallRuntimeFunction(loc, "_d_array_cast_len", args);
938932
}
939933

940934
//////////////////////////////////////////////////////////////////////////////////////////
@@ -1182,8 +1176,7 @@ void DtoArrayBoundsCheck(Loc& loc, DValue* arr, DValue* index, DValue* lowerBoun
11821176
args.push_back(c);
11831177

11841178
// call
1185-
llvm::Function* errorfn = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_arraybounds");
1186-
gIR->CreateCallOrInvoke(errorfn, args);
1179+
LLVM_D_CallRuntimeFunction(loc, "_d_arraybounds", args);
11871180

11881181
// the function does not return
11891182
gIR->ir->CreateUnreachable();

gen/classes.cpp

+12-22
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,8 @@ DValue* DtoNewClass(Loc& loc, TypeClass* tc, NewExp* newexp)
103103
// default allocator
104104
else
105105
{
106-
llvm::Function* fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_newclass");
107106
LLConstant* ci = DtoBitCast(getIrAggr(tc->sym)->getClassInfoSymbol(), DtoType(Type::typeinfoclass->type));
108-
mem = gIR->CreateCallOrInvoke(fn, ci, ".newclass_gc_alloc").getInstruction();
107+
mem = LLVM_D_CallRuntimeFunction(loc, "_d_newclass", ci, ".newclass_gc_alloc");
109108
mem = DtoBitCast(mem, DtoType(tc), ".newclass_gc");
110109
}
111110

@@ -185,14 +184,12 @@ void DtoInitClass(TypeClass* tc, LLValue* dst)
185184

186185
void DtoFinalizeClass(Loc& loc, LLValue* inst)
187186
{
188-
// get runtime function
189-
llvm::Function* fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_callfinalizer");
190187
// build args
191188
LLValue* arg[] = {
192-
DtoBitCast(inst, fn->getFunctionType()->getParamType(0), ".tmp")
189+
DtoBitCast(inst, DtoType(Type::tvoidptr), ".tmp")
193190
};
194-
// call
195-
gIR->CreateCallOrInvoke(fn, arg, "");
191+
// call runtime function
192+
LLVM_D_CallRuntimeFunction(loc, "_d_callfinalizer", arg);
196193
}
197194

198195
//////////////////////////////////////////////////////////////////////////////////////////
@@ -352,18 +349,14 @@ DValue* DtoDynamicCastObject(Loc& loc, DValue* val, Type* _to)
352349

353350
DValue* DtoCastInterfaceToObject(Loc& loc, DValue* val, Type* to)
354351
{
355-
// call:
356-
// Object _d_toObject(void* p)
357-
358-
llvm::Function* func = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_toObject");
359-
LLFunctionType* funcTy = func->getFunctionType();
352+
// call: Object _d_toObject(void* p)
360353

361354
// void* p
362355
LLValue* tmp = val->getRVal();
363-
tmp = DtoBitCast(tmp, funcTy->getParamType(0));
356+
tmp = DtoBitCast(tmp, DtoType(Type::tvoidptr));
364357

365358
// call it
366-
LLValue* ret = gIR->CreateCallOrInvoke(func, tmp).getInstruction();
359+
LLValue* ret = LLVM_D_CallRuntimeFunction(loc, "_d_toObject", tmp);
367360

368361
// cast return value
369362
if (to != NULL)
@@ -378,29 +371,26 @@ DValue* DtoCastInterfaceToObject(Loc& loc, DValue* val, Type* to)
378371

379372
DValue* DtoDynamicCastInterface(Loc& loc, DValue* val, Type* _to)
380373
{
381-
// call:
382-
// Object _d_interface_cast(void* p, ClassInfo c)
374+
// call: Object _d_interface_cast(void* p, ClassInfo c)
383375

384376
DtoResolveClass(ClassDeclaration::object);
385377
DtoResolveClass(Type::typeinfoclass);
386378

387-
llvm::Function* func = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_interface_cast");
388-
LLFunctionType* funcTy = func->getFunctionType();
389-
390379
// void* p
391380
LLValue* ptr = val->getRVal();
392-
ptr = DtoBitCast(ptr, funcTy->getParamType(0));
381+
ptr = DtoBitCast(ptr, DtoType(Type::tvoidptr));
393382

394383
// ClassInfo c
395384
TypeClass* to = static_cast<TypeClass*>(_to->toBasetype());
396385
DtoResolveClass(to->sym);
397386
LLValue* cinfo = getIrAggr(to->sym)->getClassInfoSymbol();
398387
// unfortunately this is needed as the implementation of object differs somehow from the declaration
399388
// this could happen in user code as well :/
400-
cinfo = DtoBitCast(cinfo, funcTy->getParamType(1));
389+
cinfo = DtoBitCast(cinfo, DtoType(Type::typeinfoclass->type));
401390

402391
// call it
403-
LLValue* ret = gIR->CreateCallOrInvoke2(func, ptr, cinfo).getInstruction();
392+
LLValue* args[] = { ptr, cinfo };
393+
LLValue* ret = LLVM_D_CallRuntimeFunction(loc, "_d_interface_cast", args);
404394

405395
// cast return value
406396
ret = DtoBitCast(ret, DtoType(_to));

0 commit comments

Comments
 (0)