Skip to content

unnamed_addr for immutable array literals #494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 9, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gen/arrays.cpp
Original file line number Diff line number Diff line change
@@ -473,6 +473,7 @@ void initializeArrayLiteral(IRState* p, ArrayLiteralExp* ale, LLValue* dstMem)
constarr,
".arrayliteral"
);
gvar->setUnnamedAddr(true);
DtoMemCpy(dstMem, gvar, DtoConstSize_t(getTypePaddedSize(constarr->getType())));
}
}
17 changes: 8 additions & 9 deletions gen/toir.cpp
Original file line number Diff line number Diff line change
@@ -2945,21 +2945,20 @@ LLConstant* ArrayLiteralExp::toConstElem(IRState* p)
if (!dyn)
return initval;

// we need to put the initializer in a global, and so we have a pointer to the array
// Important: don't make the global constant, since this const initializer might
// be used as an initializer for a static T[] - where modifying contents is allowed.
LLConstant* globalstore = new LLGlobalVariable(*gIR->module,
initval->getType(), false, LLGlobalValue::InternalLinkage, initval,
bool canBeConst = type->isConst() || type->isImmutable();
llvm::GlobalVariable* gvar = new llvm::GlobalVariable(*gIR->module,
initval->getType(), canBeConst, llvm::GlobalValue::InternalLinkage, initval,
".dynarrayStorage");
globalstore = DtoBitCast(globalstore, getPtrToType(arrtype));
gvar->setUnnamedAddr(canBeConst);
llvm::Constant* store = DtoBitCast(gvar, getPtrToType(arrtype));

if (bt->ty == Tpointer)
// we need to return pointer to the static array.
return globalstore;
return store;

// build a constant dynamic array reference with the .ptr field pointing into globalstore
// build a constant dynamic array reference with the .ptr field pointing into store
LLConstant* idxs[2] = { DtoConstUint(0), DtoConstUint(0) };
LLConstant* globalstorePtr = llvm::ConstantExpr::getGetElementPtr(globalstore, idxs, true);
LLConstant* globalstorePtr = llvm::ConstantExpr::getGetElementPtr(store, idxs, true);

return DtoConstSlice(DtoConstSize_t(elements->dim), globalstorePtr);
}