Skip to content

Commit

Permalink
Adapt to new _d_arraycatnTX template lowering
Browse files Browse the repository at this point in the history
  • Loading branch information
kinke committed Jul 16, 2023
1 parent 2b55c8b commit 1586266
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 110 deletions.
99 changes: 0 additions & 99 deletions gen/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,105 +750,6 @@ DSliceValue *DtoNewMulDimDynArray(const Loc &loc, Type *arrayType,

////////////////////////////////////////////////////////////////////////////////

static LLValue *DtoSlicePtr(DValue *dval) {
Loc loc;
Type *vt = dval->type->toBasetype();
if (vt->ty == TY::Tarray) {
return makeLValue(loc, dval);
}

bool isStaticArray = vt->ty == TY::Tsarray;
LLValue *val = isStaticArray ? DtoLVal(dval) : makeLValue(loc, dval);
LLStructType *i8arrty = DtoArrayType(LLType::getInt8Ty(gIR->context()));
LLValue *array = DtoRawAlloca(i8arrty, 0, ".array");
LLValue *len = isStaticArray ? DtoArrayLen(dval) : DtoConstSize_t(1);
DtoStore(len, DtoGEP(i8arrty, array, 0u, 0));
DtoStore(DtoBitCast(val, getVoidPtrType()), DtoGEP(i8arrty, array, 0, 1));
return array;
}

static llvm::StructType *DtoSlicePtrType(DValue *dval) {
if(dval->type->toBasetype()->ty == TY::Tarray)
return isaStruct(DtoType(dval->type->toBasetype()));
else
return DtoArrayType(LLType::getInt8Ty(gIR->context()));
}

static LLValue *DtoSlicePtr(Expression *e) {
return DtoSlicePtr(toElem(e));
}

DSliceValue *DtoCatArrays(const Loc &loc, Type *arrayType, Expression *exp1,
Expression *exp2) {
IF_LOG Logger::println("DtoCatArrays");
LOG_SCOPE;

llvm::SmallVector<llvm::Value *, 3> args;
LLFunction *fn = nullptr;

if (auto ce = exp1->isCatExp()) { // handle multiple concat
fn = getRuntimeFunction(loc, gIR->module, "_d_arraycatnTX");

// Create array of slices
typedef llvm::SmallVector<llvm::Value *, 16> ArgVector;
ArgVector arrs;
DValue * dval = toElem(exp2);
arrs.push_back(DtoSlicePtr(dval));
do {
arrs.push_back(DtoSlicePtr(ce->e2));
ce = static_cast<CatExp *>(ce->e1);
} while (ce->op == EXP::concatenate);
arrs.push_back(DtoSlicePtr(ce));

// Create static array from slices
LLPointerType *ptrarraytype = isaPointer(arrs[0]);
assert(ptrarraytype && "Expected pointer type");
LLStructType *arraytype = DtoSlicePtrType(dval);
assert(arraytype && "Expected struct type");
LLArrayType *type = LLArrayType::get(arraytype, arrs.size());
LLValue *array = DtoRawAlloca(type, 0, ".slicearray");
unsigned int i = 0;
for (ArgVector::reverse_iterator I = arrs.rbegin(), E = arrs.rend(); I != E;
++I) {
LLValue *v = DtoLoad(arraytype, DtoBitCast(*I, ptrarraytype));
DtoStore(v, DtoGEP(type, array, 0, i++, ".slice"));
}

LLStructType *type2 = DtoArrayType(arraytype);
LLValue *array2 = DtoRawAlloca(type2, 0, ".array");
DtoStore(DtoConstSize_t(arrs.size()), DtoGEP(type2, array2, 0u, 0, ".len"));
DtoStore(DtoBitCast(array, ptrarraytype), DtoGEP(type2, array2, 0, 1, ".ptr"));
LLType *bytearrarr = DtoArrayType(DtoArrayType(LLType::getInt8Ty(gIR->context())));
LLType *pbytearrarr = getPtrToType(bytearrarr);
LLValue *val = DtoLoad(bytearrarr, DtoBitCast(array2, pbytearrarr));

// TypeInfo ti
args.push_back(DtoTypeInfoOf(loc, arrayType));
// byte[][] arrs
args.push_back(val);
} else {
fn = getRuntimeFunction(loc, gIR->module, "_d_arraycatT");

// TypeInfo ti
args.push_back(DtoTypeInfoOf(loc, arrayType));

auto loadArray = [fn](Expression* e, int paramTypeIdx) {
DValue * dval = toElem(e);
LLValue *val = DtoLoad(DtoSlicePtrType(dval), DtoSlicePtr(dval));
return DtoSlicePaint(val, fn->getFunctionType()->getParamType(paramTypeIdx));
};
// byte[] x
args.push_back(loadArray(exp1,1));
// byte[] y
args.push_back(loadArray(exp2,2));
}

auto newArray = gIR->CreateCallOrInvoke(fn, args, ".appendedArray");
return getSlice(arrayType, newArray);
}

////////////////////////////////////////////////////////////////////////////////

DSliceValue *DtoAppendDChar(const Loc &loc, DValue *arr, Expression *exp,
const char *func) {
LLValue *valueToAppend = DtoRVal(exp);
Expand Down
10 changes: 0 additions & 10 deletions gen/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ static void checkForImplicitGCCall(const Loc &loc, const char *name) {
"_d_arrayappendcTX",
"_d_arrayappendcd",
"_d_arrayappendwd",
"_d_arraycatT",
"_d_arraycatnTX",
"_d_arraysetlengthT",
"_d_arraysetlengthiT",
"_d_assocarrayliteralTX",
Expand Down Expand Up @@ -611,14 +609,6 @@ static void buildRuntimeModule() {
createFwdDecl(LINK::c, voidArrayTy, {"_d_arrayappendcd", "_d_arrayappendwd"},
{voidArrayTy, dcharTy}, {STCref, 0});

// byte[] _d_arraycatT(const TypeInfo ti, byte[] x, byte[] y)
createFwdDecl(LINK::c, voidArrayTy, {"_d_arraycatT"},
{typeInfoTy, voidArrayTy, voidArrayTy}, {STCconst, 0, 0});

// void[] _d_arraycatnTX(const TypeInfo ti, byte[][] arrs)
createFwdDecl(LINK::c, voidArrayTy, {"_d_arraycatnTX"},
{typeInfoTy, voidArrayTy->arrayOf()}, {STCconst, 0});

// Object _d_newclass(const ClassInfo ci)
// Object _d_allocclass(const ClassInfo ci)
createFwdDecl(LINK::c, objectTy, {"_d_newclass", "_d_allocclass"},
Expand Down
8 changes: 7 additions & 1 deletion gen/toir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,7 @@ class ToElemVisitor : public Visitor {
e->type->toChars());
LOG_SCOPE;

// TODO: still required?
if (global.params.betterC) {
error(
e->loc,
Expand All @@ -2169,7 +2170,12 @@ class ToElemVisitor : public Visitor {
return;
}

result = DtoCatArrays(e->loc, e->type, e->e1, e->e2);
if (e->lowering) {
result = toElem(e->lowering);
return;
}

llvm_unreachable("CatExp should have been lowered");
}

//////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 1586266

Please sign in to comment.