Skip to content

Commit eadefdc

Browse files
committed
The runtime functions for multi-dim arrays have changed.
_d_newarraymT and _d_newarraymiT are now named _d_newarraymTX and _d_newarraymiTX. There is also a change in the signature: instead of a variable length argumentlist the functions now require an array of dimensions. This should fix test runnable/test28.d
1 parent e91c71c commit eadefdc

File tree

4 files changed

+53
-23
lines changed

4 files changed

+53
-23
lines changed

gen/arrays.cpp

+44-14
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ DSliceValue* DtoNewDynArray(Loc& loc, Type* arrayType, DValue* dim, bool default
631631
}
632632

633633
//////////////////////////////////////////////////////////////////////////////////////////
634-
DSliceValue* DtoNewMulDimDynArray(Loc& loc, Type* arrayType, DValue** dims, size_t ndims, bool defaultInit)
634+
DSliceValue* DtoNewMulDimDynArray(Loc& loc, Type* arrayType, DValue** dims, size_t ndims)
635635
{
636636
IF_LOG Logger::println("DtoNewMulDimDynArray : %s", arrayType->toChars());
637637
LOG_SCOPE;
@@ -641,26 +641,56 @@ DSliceValue* DtoNewMulDimDynArray(Loc& loc, Type* arrayType, DValue** dims, size
641641

642642
// get value type
643643
Type* vtype = arrayType->toBasetype();
644-
for (size_t i=0; i<ndims; ++i)
644+
for (size_t i = 0; i < ndims; ++i)
645645
vtype = vtype->nextOf();
646646

647647
// get runtime function
648-
bool zeroInit = vtype->isZeroInit();
649-
if (defaultInit && !isInitialized(vtype))
650-
defaultInit = false;
651-
652-
const char* fnname = zeroInit ? "_d_newarraymT" : "_d_newarraymiT";
653-
648+
const char* fnname = vtype->isZeroInit() ? "_d_newarraymTX" : "_d_newarraymiTX";
654649
LLFunction* fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, fnname);
655650

656-
std::vector<LLValue*> args;
657-
args.reserve(ndims+2);
658-
args.push_back(arrayTypeInfo);
659-
args.push_back(DtoConstSize_t(ndims));
651+
// Check if constant
652+
bool allDimsConst = true;
653+
for (size_t i = 0; i < ndims; ++i)
654+
{
655+
if (!isaConstant(dims[i]->getRVal()))
656+
allDimsConst = false;
657+
}
660658

661659
// build dims
662-
for (size_t i=0; i<ndims; ++i)
663-
args.push_back(dims[i]->getRVal());
660+
LLValue* array;
661+
if (allDimsConst)
662+
{
663+
// Build constant array for dimensions
664+
std::vector<LLConstant*> argsdims;
665+
argsdims.reserve(ndims);
666+
for (size_t i = 0; i < ndims; ++i)
667+
{
668+
argsdims.push_back(isaConstant(dims[i]->getRVal()));
669+
}
670+
671+
llvm::Constant* dims = llvm::ConstantArray::get(llvm::ArrayType::get(DtoSize_t(), ndims), argsdims);
672+
LLGlobalVariable* gvar = new llvm::GlobalVariable(*gIR->module, dims->getType(), true, LLGlobalValue::InternalLinkage, dims, ".dimsarray");
673+
array = llvm::ConstantExpr::getBitCast(gvar, getPtrToType(dims->getType()));
674+
}
675+
else
676+
{
677+
// Build static array for dimensions
678+
LLArrayType* type = LLArrayType::get(DtoSize_t(), ndims);
679+
array = DtoRawAlloca(type, 0, ".dimarray");
680+
unsigned int i = 0;
681+
for (size_t i = 0; i < ndims; ++i)
682+
DtoStore(dims[i]->getRVal(), DtoGEPi(array, 0, i, ".ndim"));
683+
}
684+
685+
LLStructType* dtype = DtoArrayType(DtoSize_t());
686+
LLValue* darray = DtoRawAlloca(dtype, 0, ".array");
687+
DtoStore(DtoConstSize_t(ndims), DtoGEPi(darray, 0, 0, ".len"));
688+
DtoStore(DtoBitCast(array, getPtrToType(DtoSize_t())), DtoGEPi(darray, 0, 1, ".ptr"));
689+
690+
llvm::Value* args[] = {
691+
arrayTypeInfo,
692+
DtoLoad(darray)
693+
};
664694

665695
// call allocator
666696
LLValue* newptr = gIR->CreateCallOrInvoke(fn, args, ".gc_mem").getInstruction();

gen/arrays.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void DtoSetArray(DValue* array, LLValue* dim, LLValue* ptr);
5757
void DtoSetArrayToNull(LLValue* v);
5858

5959
DSliceValue* DtoNewDynArray(Loc& loc, Type* arrayType, DValue* dim, bool defaultInit=true);
60-
DSliceValue* DtoNewMulDimDynArray(Loc& loc, Type* arrayType, DValue** dims, size_t ndims, bool defaultInit=true);
60+
DSliceValue* DtoNewMulDimDynArray(Loc& loc, Type* arrayType, DValue** dims, size_t ndims);
6161
DSliceValue* DtoResizeDynArray(Loc& loc, Type* arrayType, DValue* array, llvm::Value* newdim);
6262

6363
void DtoCatAssignElement(Loc& loc, Type* type, DValue* arr, Expression* exp);

gen/runtime.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ static void checkForImplicitGCCall(const Loc &loc, const char *name)
8787
"_d_delmemory",
8888
"_d_newarrayT",
8989
"_d_newarrayiT",
90-
"_d_newarraymT",
91-
"_d_newarraymiT",
90+
"_d_newarraymTX",
91+
"_d_newarraymiTX",
9292
"_d_newarrayU",
9393
"_d_newclass",
9494
"_d_newitemT",
@@ -433,13 +433,13 @@ static void LLVM_D_BuildRuntimeModule()
433433
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname2, M);
434434
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname3, M);
435435
}
436-
// void[] _d_newarraymT (const TypeInfo ti, size_t[] dims)
437-
// void[] _d_newarraymiT(const TypeInfo ti, size_t[] dims)
436+
// void[] _d_newarraymTX (const TypeInfo ti, size_t[] dims)
437+
// void[] _d_newarraymiTX(const TypeInfo ti, size_t[] dims)
438438
{
439-
llvm::StringRef fname ("_d_newarraymT");
440-
llvm::StringRef fname2("_d_newarraymiT");
439+
llvm::StringRef fname ("_d_newarraymTX");
440+
llvm::StringRef fname2("_d_newarraymiTX");
441441
LLType *types[] = { typeInfoTy, rt_array(sizeTy) };
442-
LLFunctionType* fty = llvm::FunctionType::get(voidArrayTy, types, true);
442+
LLFunctionType* fty = llvm::FunctionType::get(voidArrayTy, types, false);
443443
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
444444
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname2, M);
445445
}

gen/toir.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,7 @@ class ToElemVisitor : public Visitor
18881888
dims.reserve(ndims);
18891889
for (size_t i=0; i<ndims; ++i)
18901890
dims.push_back(toElem((*e->arguments)[i]));
1891-
result = DtoNewMulDimDynArray(e->loc, e->newtype, &dims[0], ndims, true);
1891+
result = DtoNewMulDimDynArray(e->loc, e->newtype, &dims[0], ndims);
18921892
}
18931893
}
18941894
// new static array

0 commit comments

Comments
 (0)