Removed TypeOpaque from DMD.

Changed runtime functions taking opaque[] to void[].
Implemented proper type painting, to avoid "resizing" array casts in runtime calls that previously took opaque[].
Implemented dynamic arrays as first class types, this implements proper ABI for these types on x86.
Added dwarf region end after call to assert function, fixes some problems with llvm not allowing this to be missing.
Reverted change to WithStatement from rev [704] it breaks MiniD, mini/with2.d needs to be fixed some other way...
Fixed tango bug 1339 in runtime, problem with _adReverseChar on invalid UTF-8.
Disabled .bc generation in the compiler runtime part, genobj.d triggers some llvm bug when using debug info. the .o seems to work fine.
This commit is contained in:
Tomas Lindquist Olsen
2008-10-22 14:55:33 +02:00
parent 50484db204
commit f7ea1da010
25 changed files with 506 additions and 624 deletions
+24 -118
View File
@@ -55,56 +55,10 @@ void DtoSetArrayToNull(LLValue* v)
Logger::println("DtoSetArrayToNull");
LOG_SCOPE;
LLValue* len = DtoGEPi(v,0,0);
LLValue* zerolen = llvm::ConstantInt::get(len->getType()->getContainedType(0), 0, false);
DtoStore(zerolen, len);
assert(isaPointer(v));
const LLType* t = v->getType()->getContainedType(0);
LLValue* ptr = DtoGEPi(v,0,1);
const LLPointerType* pty = isaPointer(ptr->getType()->getContainedType(0));
LLValue* nullptr = llvm::ConstantPointerNull::get(pty);
DtoStore(nullptr, ptr);
}
//////////////////////////////////////////////////////////////////////////////////////////
void DtoArrayAssign(LLValue* dst, LLValue* src)
{
Logger::println("DtoArrayAssign");
LOG_SCOPE;
assert(gIR);
if (dst->getType() == src->getType())
{
LLValue* ptr = DtoGEPi(src,0,0);
LLValue* val = DtoLoad(ptr);
ptr = DtoGEPi(dst,0,0);
DtoStore(val, ptr);
ptr = DtoGEPi(src,0,1);
val = DtoLoad(ptr);
ptr = DtoGEPi(dst,0,1);
DtoStore(val, ptr);
}
else
{
if (Logger::enabled())
Logger::cout() << "array assignment type dont match: " << *dst->getType() << "\n\n" << *src->getType() << '\n';
const LLArrayType* arrty = isaArray(src->getType()->getContainedType(0));
if (!arrty)
{
std::cout << "invalid: " << *src << '\n';
assert(0);
}
const LLType* dstty = getPtrToType(arrty->getElementType());
LLValue* dstlen = DtoGEPi(dst,0,0);
LLValue* srclen = DtoConstSize_t(arrty->getNumElements());
DtoStore(srclen, dstlen);
LLValue* dstptr = DtoGEPi(dst,0,1);
LLValue* srcptr = DtoBitCast(src, dstty);
DtoStore(srcptr, dstptr);
}
DtoStore(LLConstant::getNullValue(t), v);
}
//////////////////////////////////////////////////////////////////////////////////////////
@@ -684,64 +638,24 @@ static LLValue* DtoArrayEqCmp_impl(Loc& loc, const char* func, DValue* l, DValue
LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, func);
assert(fn);
LLValue* lmem;
LLValue* rmem;
// find common dynamic array type
Type* commonType = l->getType()->toBasetype()->nextOf()->arrayOf();
// cast static arrays to dynamic ones, this turns them into DSliceValues
Logger::println("casting to dynamic arrays");
Type* l_ty = l->getType()->toBasetype();
Type* r_ty = r->getType()->toBasetype();
assert(l_ty->next == r_ty->next);
if ((l_ty->ty == Tsarray) || (r_ty->ty == Tsarray)) {
Type* a_ty = l_ty->next->arrayOf();
if (l_ty->ty == Tsarray)
l = DtoCastArray(loc, l, a_ty);
if (r_ty->ty == Tsarray)
r = DtoCastArray(loc, r, a_ty);
}
Logger::println("giving storage");
// we need to give slices storage
if (l->isSlice()) {
lmem = DtoAlloca(DtoType(l->getType()), "tmpparam");
DtoSetArray(lmem, DtoArrayLen(l), DtoArrayPtr(l));
}
// also null
else if (l->isNull())
{
lmem = DtoAlloca(DtoType(l->getType()), "tmpparam");
DtoSetArray(lmem, llvm::Constant::getNullValue(DtoSize_t()), llvm::Constant::getNullValue(DtoType(l->getType()->next->pointerTo())));
}
else
lmem = l->getRVal();
// and for the rvalue ...
// we need to give slices storage
if (r->isSlice()) {
rmem = DtoAlloca(DtoType(r->getType()), "tmpparam");
DtoSetArray(rmem, DtoArrayLen(r), DtoArrayPtr(r));
}
// also null
else if (r->isNull())
{
rmem = DtoAlloca(DtoType(r->getType()), "tmpparam");
DtoSetArray(rmem, llvm::Constant::getNullValue(DtoSize_t()), llvm::Constant::getNullValue(DtoType(r->getType()->next->pointerTo())));
}
else
rmem = r->getRVal();
const LLType* pt = fn->getFunctionType()->getParamType(0);
l = DtoCastArray(loc, l, commonType);
r = DtoCastArray(loc, r, commonType);
LLValue* lmem;
LLValue* rmem;
LLSmallVector<LLValue*, 3> args;
if (Logger::enabled())
{
Logger::cout() << "bitcasting to " << *pt << '\n';
Logger::cout() << *lmem << '\n';
Logger::cout() << *rmem << '\n';
}
args.push_back(DtoBitCast(lmem,pt));
args.push_back(DtoBitCast(rmem,pt));
// get values, reinterpret cast to void[]
lmem = DtoAggrPaint(l->getRVal(), DtoArrayType(LLType::Int8Ty));
args.push_back(lmem);
rmem = DtoAggrPaint(r->getRVal(), DtoArrayType(LLType::Int8Ty));
args.push_back(rmem);
// pass array typeinfo ?
if (useti) {
@@ -753,18 +667,11 @@ static LLValue* DtoArrayEqCmp_impl(Loc& loc, const char* func, DValue* l, DValue
if (Logger::enabled())
Logger::cout() << "typeinfo decl: " << *tival << '\n';
pt = fn->getFunctionType()->getParamType(2);
args.push_back(DtoBitCast(tival, pt));
args.push_back(DtoBitCast(tival, fn->getFunctionType()->getParamType(2)));
}
CallOrInvoke* call = gIR->CreateCallOrInvoke(fn, args.begin(), args.end(), "tmp");
// set param attrs
llvm::AttrListPtr palist;
palist = palist.addAttr(1, llvm::Attribute::ByVal);
palist = palist.addAttr(2, llvm::Attribute::ByVal);
call->setAttributes(palist);
return call->get();
}
@@ -917,7 +824,9 @@ LLValue* DtoArrayLen(DValue* v)
return s->len;
else if (v->isNull())
return DtoConstSize_t(0);
return DtoLoad(DtoGEPi(v->getRVal(), 0,0));
else if (v->isLVal())
return DtoLoad(DtoGEPi(v->getLVal(), 0,0), ".len");
return gIR->ir->CreateExtractValue(v->getRVal(), 0, ".len");
}
else if (t->ty == Tsarray) {
assert(!v->isSlice());
@@ -943,7 +852,9 @@ LLValue* DtoArrayPtr(DValue* v)
return s->ptr;
else if (v->isNull())
return getNullPtr(getPtrToType(DtoType(t->next)));
return DtoLoad(DtoGEPi(v->getRVal(), 0,1));
else if (v->isLVal())
return DtoLoad(DtoGEPi(v->getLVal(), 0,1), ".ptr");
return gIR->ir->CreateExtractValue(v->getRVal(), 1, ".ptr");
}
else if (t->ty == Tsarray) {
assert(!v->isSlice());
@@ -1046,7 +957,6 @@ DValue* DtoCastArray(Loc& loc, DValue* u, Type* to)
return new DImValue(to, rval);
}
//////////////////////////////////////////////////////////////////////////////////////////
void DtoArrayBoundsCheck(Loc& loc, DValue* arr, DValue* index, bool isslice)
{
@@ -1071,11 +981,9 @@ void DtoArrayBoundsCheck(Loc& loc, DValue* arr, DValue* index, bool isslice)
gIR->scope() = IRScope(failbb, okbb);
std::vector<LLValue*> args;
llvm::AttrListPtr palist;
// file param
args.push_back(gIR->dmodule->ir.irModule->fileName);
palist = palist.addAttr(1, llvm::Attribute::ByVal);
args.push_back(DtoLoad(gIR->dmodule->ir.irModule->fileName));
// line param
LLConstant* c = DtoConstUint(loc.linnum);
@@ -1084,12 +992,10 @@ void DtoArrayBoundsCheck(Loc& loc, DValue* arr, DValue* index, bool isslice)
// call
llvm::Function* errorfn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_array_bounds");
CallOrInvoke* call = gIR->CreateCallOrInvoke(errorfn, args.begin(), args.end());
call->setAttributes(palist);
// the function does not return
gIR->ir->CreateUnreachable();
// if ok, proceed in okbb
gIR->scope() = IRScope(okbb, oldend);
}
+10
View File
@@ -43,6 +43,16 @@ LLValue* DVarValue::getRVal()
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
LLValue* DSliceValue::getRVal()
{
assert(len);
assert(ptr);
return DtoAggrPair(len, ptr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
DFuncValue::DFuncValue(FuncDeclaration* fd, LLValue* v, LLValue* vt)
{
func = fd;
+2 -2
View File
@@ -29,7 +29,6 @@ struct DVarValue;
struct DFieldValue;
struct DFuncValue;
struct DSliceValue;
struct DArrayLenValue;
struct DLRValue;
// base class for d-values
@@ -49,7 +48,6 @@ struct DValue : Object
virtual DFieldValue* isField() { return NULL; }
virtual DSliceValue* isSlice() { return NULL; }
virtual DFuncValue* isFunc() { return NULL; }
virtual DArrayLenValue* isArrayLen() { return NULL; }
virtual DLRValue* isLRValue() { return NULL; }
protected:
@@ -127,6 +125,8 @@ struct DSliceValue : DValue
DSliceValue(Type* t, LLValue* l, LLValue* p) { type=t; ptr=p; len=l; }
virtual LLValue* getRVal();
virtual Type*& getType() { assert(type); return type; }
virtual DSliceValue* isSlice() { return this; }
};
+35 -42
View File
@@ -59,7 +59,7 @@ const llvm::FunctionType* DtoFunctionType(Type* type, const LLType* thistype, co
{
const LLType* arrTy = DtoArrayType(LLType::Int8Ty);
const LLType* arrArrTy = DtoArrayType(arrTy);
paramvec.push_back(getPtrToType(arrArrTy));
paramvec.push_back(arrArrTy);
}
}
else{
@@ -103,7 +103,7 @@ const llvm::FunctionType* DtoFunctionType(Type* type, const LLType* thistype, co
types.push_back(DtoSize_t());
types.push_back(getPtrToType(getPtrToType(ti->ir.irStruct->constInit->getType())));
const LLType* t1 = llvm::StructType::get(types);
paramvec.push_back(getPtrToType(t1));
paramvec.push_back(t1);
paramvec.push_back(getPtrToType(LLType::Int8Ty));
}
else if (arrayVararg)
@@ -123,41 +123,37 @@ const llvm::FunctionType* DtoFunctionType(Type* type, const LLType* thistype, co
const LLType* at = DtoType(argT);
// FIXME: using the llvm type for these is a bad idea... aggregates are first class now and we're starting to use it ...
if (argT->iscomplex()) {
goto Lbadstuff;
}
else if (isaStruct(at)) {
Logger::println("struct param");
paramvec.push_back(getPtrToType(at));
if (!refOrOut)
arg->llvmAttrs |= llvm::Attribute::ByVal;
}
else if (isaArray(at)) {
// static array are passed by reference
Logger::println("sarray param");
assert(argT->ty == Tsarray);
paramvec.push_back(getPtrToType(at));
}
else if (llvm::isa<llvm::OpaqueType>(at)) {
// opaque types need special handling
if (llvm::isa<llvm::OpaqueType>(at)) {
Logger::println("opaque param");
assert(argT->ty == Tstruct || argT->ty == Tclass);
paramvec.push_back(getPtrToType(at));
}
// structs and delegates are passed as a reference, but by value
else if (argT->ty == Tstruct || argT->ty == Tdelegate) {
Logger::println("struct/sarray param");
if (!refOrOut)
arg->llvmAttrs |= llvm::Attribute::ByVal;
paramvec.push_back(getPtrToType(at));
}
// static arrays are passed directly by reference
else if (argT->ty == Tsarray)
{
Logger::println("static array param");
at = getPtrToType(at);
paramvec.push_back(at);
}
// firstclass ' ref/out ' parameter
else if (refOrOut) {
Logger::println("ref/out param");
at = getPtrToType(at);
paramvec.push_back(at);
}
// firstclass ' in ' parameter
else {
Lbadstuff:
if (refOrOut) {
Logger::println("by ref param");
at = getPtrToType(at);
}
else {
Logger::println("in param");
if (unsigned ea = DtoShouldExtend(argT))
{
arg->llvmAttrs |= ea;
}
}
Logger::println("in param");
if (unsigned ea = DtoShouldExtend(argT))
arg->llvmAttrs |= ea;
paramvec.push_back(at);
}
@@ -362,15 +358,6 @@ static void set_param_attrs(TypeFunction* f, llvm::Function* func, FuncDeclarati
attrs.push_back(PAWI);
}
// set byval attrs on implicit main arg
if (fdecl->isMain() && Argument::dim(f->parameters) == 0)
{
PAWI.Index = llidx;
PAWI.Attrs = llvm::Attribute::ByVal;
attrs.push_back(PAWI);
llidx++;
}
// set attrs on the rest of the arguments
for (; llidx <= funcNumArgs && Argument::dim(f->parameters) > k; ++llidx,++k)
{
@@ -775,12 +762,18 @@ void DtoDefineFunc(FuncDeclaration* fd)
}
}
// copy _argptr to a memory location
// copy _argptr and _arguments to a memory location
if (f->linkage == LINKd && f->varargs == 1)
{
// _argptr
LLValue* argptrmem = DtoAlloca(fd->ir.irFunc->_argptr->getType(), "_argptr_mem");
new llvm::StoreInst(fd->ir.irFunc->_argptr, argptrmem, gIR->scopebb());
fd->ir.irFunc->_argptr = argptrmem;
// _arguments
LLValue* argumentsmem = DtoAlloca(fd->ir.irFunc->_arguments->getType(), "_arguments_mem");
new llvm::StoreInst(fd->ir.irFunc->_arguments, argumentsmem, gIR->scopebb());
fd->ir.irFunc->_arguments = argumentsmem;
}
// output function body
+60 -28
View File
@@ -117,35 +117,14 @@ void DtoAssert(Loc* loc, DValue* msg)
const char* fname = msg ? "_d_assert_msg" : "_d_assert";
llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, fname);
// param attrs
llvm::AttrListPtr palist;
int idx = 1;
// msg param
if (msg)
{
if (DSliceValue* s = msg->isSlice())
{
llvm::AllocaInst* alloc = gIR->func()->msgArg;
if (!alloc)
{
alloc = DtoAlloca(DtoArrayType(LLType::Int8Ty), ".assertmsg");
DtoSetArray(alloc, DtoArrayLen(s), DtoArrayPtr(s));
gIR->func()->msgArg = alloc;
}
args.push_back(alloc);
}
else
{
args.push_back(msg->getRVal());
}
palist = palist.addAttr(idx++, llvm::Attribute::ByVal);
args.push_back(msg->getRVal());
}
// file param
args.push_back(gIR->dmodule->ir.irModule->fileName);
palist = palist.addAttr(idx++, llvm::Attribute::ByVal);
args.push_back(DtoLoad(gIR->dmodule->ir.irModule->fileName));
// line param
LLConstant* c = DtoConstUint(loc->linnum);
@@ -153,7 +132,10 @@ void DtoAssert(Loc* loc, DValue* msg)
// call
CallOrInvoke* call = gIR->CreateCallOrInvoke(fn, args.begin(), args.end());
call->setAttributes(palist);
// end debug info
if (global.params.symdebug)
DtoDwarfFuncEnd(gIR->func()->decl);
// after assert is always unreachable
gIR->ir->CreateUnreachable();
@@ -445,8 +427,12 @@ void DtoAssign(Loc& loc, DValue* lhs, DValue* rhs)
DtoSetArrayToNull(lhs->getLVal());
}
// reference assignment
else if (t2->ty == Tarray) {
DtoStore(rhs->getRVal(), lhs->getLVal());
}
// some implicitly converting ref assignment
else {
DtoArrayAssign(lhs->getLVal(), rhs->getRVal());
DtoSetArray(lhs->getLVal(), DtoArrayLen(rhs), DtoArrayPtr(rhs));
}
}
else if (t->ty == Tsarray) {
@@ -756,6 +742,52 @@ DValue* DtoCast(Loc& loc, DValue* val, Type* to)
}
}
//////////////////////////////////////////////////////////////////////////////////////////
DValue* DtoPaintType(Loc& loc, DValue* val, Type* to)
{
Type* from = val->getType()->toBasetype();
Logger::println("repainting from '%s' to '%s'", from->toChars(), to->toChars());
if (from->ty == Tarray)
{
Type* at = to->toBasetype();
assert(at->ty == Tarray);
Type* elem = at->next->pointerTo();
if (DSliceValue* slice = val->isSlice())
{
return new DSliceValue(to, slice->len, DtoBitCast(slice->ptr, DtoType(elem)));
}
else if (val->isLVal())
{
LLValue* ptr = val->getLVal();
ptr = DtoBitCast(ptr, DtoType(at->pointerTo()));
return new DVarValue(to, ptr);
}
else
{
LLValue *len, *ptr;
len = DtoArrayLen(val);
ptr = DtoArrayPtr(val);
ptr = DtoBitCast(ptr, DtoType(elem));
return new DImValue(to, DtoAggrPair(len, ptr, "tmp"));
}
}
else if (from->ty == Tpointer || from->ty == Tclass || from->ty == Taarray)
{
Type* b = to->toBasetype();
assert(b->ty == Tpointer || b->ty == Tclass || b->ty == Taarray);
LLValue* ptr = DtoBitCast(val->getRVal(), DtoType(b));
return new DImValue(to, ptr);
}
else
{
assert(!val->isLVal());
assert(DtoType(to) == DtoType(to));
return new DImValue(to, val->getRVal());
}
}
/****************************************************************************************/
/*////////////////////////////////////////////////////////////////////////////////////////
// TEMPLATE HELPERS
@@ -1413,7 +1445,7 @@ DValue* DtoInitializer(LLValue* target, Initializer* init)
static LLConstant* expand_to_sarray(Type *base, Expression* exp)
{
Logger::println("building type %s from expression (%s) of type %s", base->toChars(), exp->toChars(), exp->type->toChars());
Logger::println("building type %s to expression (%s) of type %s", base->toChars(), exp->toChars(), exp->type->toChars());
const LLType* dstTy = DtoType(base);
if (Logger::enabled())
Logger::cout() << "final llvm type requested: " << *dstTy << '\n';
@@ -1463,12 +1495,12 @@ LLConstant* DtoDefaultInit(Type* type)
{
if (base->ty == Tsarray)
{
Logger::println("type is a static array, building constant array initializer from single value");
Logger::println("type is a static array, building constant array initializer to single value");
return expand_to_sarray(base, exp);
}
else
{
error("cannot yet convert default initializer %s from type %s to %s", exp->toChars(), exp->type->toChars(), type->toChars());
error("cannot yet convert default initializer %s to type %s to %s", exp->toChars(), exp->type->toChars(), type->toChars());
fatal();
}
assert(0);
+3
View File
@@ -58,6 +58,9 @@ DValue* DtoCastFloat(Loc& loc, DValue* val, Type* to);
DValue* DtoCastDelegate(Loc& loc, DValue* val, Type* to);
DValue* DtoCast(Loc& loc, DValue* val, Type* to);
// return the same val as passed in, modified to the target type, if possible, otherwise returns a new DValue
DValue* DtoPaintType(Loc& loc, DValue* val, Type* to);
// is template instance check
bool DtoIsTemplateInstance(Dsymbol* s);
+37 -105
View File
@@ -110,18 +110,7 @@ static const LLType* rt_ptr(const LLType* t)
static const LLType* rt_array(const LLType* elemty)
{
std::vector<const LLType*> t;
t.push_back(DtoSize_t());
t.push_back(rt_ptr(elemty));
return rt_ptr(llvm::StructType::get(t));
}
static const LLType* rt_array2(const LLType* elemty)
{
std::vector<const LLType*> t;
t.push_back(DtoSize_t());
t.push_back(rt_ptr(elemty));
return llvm::StructType::get(t);
return llvm::StructType::get(DtoSize_t(), rt_ptr(elemty), 0);
}
static const LLType* rt_dg1()
@@ -199,11 +188,9 @@ static void LLVM_D_BuildRuntimeModule()
types.push_back(stringTy);
types.push_back(intTy);
const llvm::FunctionType* fty = llvm::FunctionType::get(voidTy, types, false);
llvm::AttrListPtr palist;
palist = palist.addAttr(1, llvm::Attribute::ByVal);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname2, M)->setAttributes(palist);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname3, M)->setAttributes(palist);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname2, M);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname3, M);
}
// void _d_assert_msg( char[] msg, char[] file, uint line )
@@ -214,10 +201,7 @@ static void LLVM_D_BuildRuntimeModule()
types.push_back(stringTy);
types.push_back(intTy);
const llvm::FunctionType* fty = llvm::FunctionType::get(voidPtrTy, types, false);
llvm::AttrListPtr palist;
palist = palist.addAttr(1, llvm::Attribute::ByVal);
palist = palist.addAttr(2, llvm::Attribute::ByVal);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
}
/////////////////////////////////////////////////////////////////////////////////////
@@ -382,7 +366,6 @@ static void LLVM_D_BuildRuntimeModule()
types.push_back(rt_dg1()); \
const llvm::FunctionType* fty = llvm::FunctionType::get(intTy, types, false); \
llvm::AttrListPtr palist; \
palist = palist.addAttr(1, llvm::Attribute::ByVal); \
palist = palist.addAttr(2, llvm::Attribute::ByVal); \
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist); \
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname2, M)->setAttributes(palist); \
@@ -402,7 +385,6 @@ static void LLVM_D_BuildRuntimeModule()
types.push_back(rt_dg2()); \
const llvm::FunctionType* fty = llvm::FunctionType::get(intTy, types, false); \
llvm::AttrListPtr palist; \
palist = palist.addAttr(1, llvm::Attribute::ByVal); \
palist = palist.addAttr(2, llvm::Attribute::ByVal); \
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist); \
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname2, M)->setAttributes(palist); \
@@ -421,7 +403,6 @@ static void LLVM_D_BuildRuntimeModule()
types.push_back(rt_dg1()); \
const llvm::FunctionType* fty = llvm::FunctionType::get(intTy, types, false); \
llvm::AttrListPtr palist; \
palist = palist.addAttr(1, llvm::Attribute::ByVal); \
palist = palist.addAttr(2, llvm::Attribute::ByVal); \
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist); \
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname2, M)->setAttributes(palist); \
@@ -440,7 +421,6 @@ static void LLVM_D_BuildRuntimeModule()
types.push_back(rt_dg2()); \
const llvm::FunctionType* fty = llvm::FunctionType::get(intTy, types, false); \
llvm::AttrListPtr palist; \
palist = palist.addAttr(1, llvm::Attribute::ByVal); \
palist = palist.addAttr(2, llvm::Attribute::ByVal); \
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist); \
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname2, M)->setAttributes(palist); \
@@ -470,22 +450,6 @@ static void LLVM_D_BuildRuntimeModule()
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
// builds the d string[] for the D main args from the C main args
// void _d_main_args(uint n, char** args, ref char[][] res)
{
std::string fname("_d_main_args");
std::vector<const LLType*> types;
types.push_back(intTy);
types.push_back(rt_ptr(rt_ptr(byteTy)));
types.push_back(rt_array(stringTy->getContainedType(0)));
const llvm::FunctionType* fty = llvm::FunctionType::get(voidTy, types, false);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
// cast to object
// Object _d_toObject(void* p)
{
@@ -529,12 +493,9 @@ static void LLVM_D_BuildRuntimeModule()
std::string fname2("_adSortChar");
std::vector<const LLType*> types;
types.push_back(stringTy);
types.push_back(stringTy);
const llvm::FunctionType* fty = llvm::FunctionType::get(voidTy, types, false);
llvm::AttrListPtr palist;
palist = palist.addAttr(2, llvm::Attribute::ByVal);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname2, M)->setAttributes(palist);
const llvm::FunctionType* fty = llvm::FunctionType::get(stringTy, types, false);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname2, M);
}
// wchar[] _adReverseWchar(wchar[] a)
@@ -544,42 +505,33 @@ static void LLVM_D_BuildRuntimeModule()
std::string fname2("_adSortWchar");
std::vector<const LLType*> types;
types.push_back(wstringTy);
types.push_back(wstringTy);
const llvm::FunctionType* fty = llvm::FunctionType::get(voidTy, types, false);
llvm::AttrListPtr palist;
palist = palist.addAttr(2, llvm::Attribute::ByVal);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname2, M)->setAttributes(palist);
const llvm::FunctionType* fty = llvm::FunctionType::get(wstringTy, types, false);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname2, M);
}
// Array _adReverse(Array a, size_t szelem)
// void[] _adReverse(void[] a, size_t szelem)
{
std::string fname("_adReverse");
std::vector<const LLType*> types;
types.push_back(rt_array(byteTy));
types.push_back(rt_array(byteTy));
types.push_back(sizeTy);
const llvm::FunctionType* fty = llvm::FunctionType::get(voidTy, types, false);
llvm::AttrListPtr palist;
palist = palist.addAttr(2, llvm::Attribute::ByVal);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist);
const llvm::FunctionType* fty = llvm::FunctionType::get(rt_array(byteTy), types, false);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
}
// Array _adDupT(TypeInfo ti, Array a)
// void[] _adDupT(TypeInfo ti, void[] a)
{
std::string fname("_adDupT");
std::vector<const LLType*> types;
types.push_back(rt_array(byteTy));
types.push_back(typeInfoTy);
types.push_back(rt_array(byteTy));
const llvm::FunctionType* fty = llvm::FunctionType::get(voidTy, types, false);
llvm::AttrListPtr palist;
palist = palist.addAttr(3, llvm::Attribute::ByVal);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist);
const llvm::FunctionType* fty = llvm::FunctionType::get(rt_array(byteTy), types, false);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
}
// int _adEq(Array a1, Array a2, TypeInfo ti)
// int _adCmp(Array a1, Array a2, TypeInfo ti)
// int _adEq(void[] a1, void[] a2, TypeInfo ti)
// int _adCmp(void[] a1, void[] a2, TypeInfo ti)
{
std::string fname("_adEq");
std::string fname2("_adCmp");
@@ -588,37 +540,28 @@ static void LLVM_D_BuildRuntimeModule()
types.push_back(rt_array(byteTy));
types.push_back(typeInfoTy);
const llvm::FunctionType* fty = llvm::FunctionType::get(intTy, types, false);
llvm::AttrListPtr palist;
palist = palist.addAttr(1, llvm::Attribute::ByVal);
palist = palist.addAttr(2, llvm::Attribute::ByVal);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname2, M)->setAttributes(palist);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname2, M);
}
// int _adCmpChar(Array a1, Array a2)
// int _adCmpChar(void[] a1, void[] a2)
{
std::string fname("_adCmpChar");
std::vector<const LLType*> types;
types.push_back(rt_array(byteTy));
types.push_back(rt_array(byteTy));
const llvm::FunctionType* fty = llvm::FunctionType::get(intTy, types, false);
llvm::AttrListPtr palist;
palist = palist.addAttr(1, llvm::Attribute::ByVal);
palist = palist.addAttr(2, llvm::Attribute::ByVal);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
}
// Array _adSort(Array a, TypeInfo ti)
// void[] _adSort(void[] a, TypeInfo ti)
{
std::string fname("_adSort");
std::vector<const LLType*> types;
types.push_back(rt_array(byteTy));
types.push_back(rt_array(byteTy));
types.push_back(typeInfoTy);
const llvm::FunctionType* fty = llvm::FunctionType::get(voidTy, types, false);
llvm::AttrListPtr palist;
palist = palist.addAttr(2, llvm::Attribute::ByVal);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist);
const llvm::FunctionType* fty = llvm::FunctionType::get(rt_array(byteTy), types, false);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
}
/////////////////////////////////////////////////////////////////////////////////////
@@ -680,15 +623,14 @@ static void LLVM_D_BuildRuntimeModule()
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
}
// ArrayRet_t _aaValues(AA aa, size_t keysize, size_t valuesize)
// void[] _aaValues(AA aa, size_t keysize, size_t valuesize)
{
std::string fname("_aaValues");
std::vector<const LLType*> types;
types.push_back(rt_array(byteTy));
types.push_back(aaTy);
types.push_back(sizeTy);
types.push_back(sizeTy);
const llvm::FunctionType* fty = llvm::FunctionType::get(voidTy, types, false);
const llvm::FunctionType* fty = llvm::FunctionType::get(rt_array(byteTy), types, false);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
}
@@ -702,14 +644,13 @@ static void LLVM_D_BuildRuntimeModule()
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
}
// ArrayRet_t _aaKeys(AA aa, size_t keysize)
// void[] _aaKeys(AA aa, size_t keysize)
{
std::string fname("_aaKeys");
std::vector<const LLType*> types;
types.push_back(rt_array(byteTy));
types.push_back(aaTy);
types.push_back(sizeTy);
const llvm::FunctionType* fty = llvm::FunctionType::get(voidTy, types, false);
const llvm::FunctionType* fty = llvm::FunctionType::get(rt_array(byteTy), types, false);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
}
@@ -732,7 +673,7 @@ static void LLVM_D_BuildRuntimeModule()
std::vector<const LLType*> types;
types.push_back(aaTy);
types.push_back(sizeTy);
types.push_back(rt_dg1());
types.push_back(rt_dg2());
const llvm::FunctionType* fty = llvm::FunctionType::get(intTy, types, false);
llvm::AttrListPtr palist;
palist = palist.addAttr(3, llvm::Attribute::ByVal);
@@ -808,39 +749,30 @@ static void LLVM_D_BuildRuntimeModule()
{
std::string fname("_d_switch_string");
std::vector<const LLType*> types;
types.push_back(rt_array(rt_array2(byteTy)));
types.push_back(rt_array(stringTy));
types.push_back(stringTy);
const llvm::FunctionType* fty = llvm::FunctionType::get(intTy, types, false);
llvm::AttrListPtr palist;
palist = palist.addAttr(1, llvm::Attribute::ByVal);
palist = palist.addAttr(2, llvm::Attribute::ByVal);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
}
// int _d_switch_ustring(wchar[][] table, wchar[] ca)
{
std::string fname("_d_switch_ustring");
std::vector<const LLType*> types;
types.push_back(rt_array(rt_array2(shortTy)));
types.push_back(rt_array(wstringTy));
types.push_back(wstringTy);
const llvm::FunctionType* fty = llvm::FunctionType::get(intTy, types, false);
llvm::AttrListPtr palist;
palist = palist.addAttr(1, llvm::Attribute::ByVal);
palist = palist.addAttr(2, llvm::Attribute::ByVal);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
}
// int _d_switch_dstring(dchar[][] table, dchar[] ca)
{
std::string fname("_d_switch_dstring");
std::vector<const LLType*> types;
types.push_back(rt_array(rt_array2(intTy)));
types.push_back(rt_array(dstringTy));
types.push_back(dstringTy);
const llvm::FunctionType* fty = llvm::FunctionType::get(intTy, types, false);
llvm::AttrListPtr palist;
palist = palist.addAttr(1, llvm::Attribute::ByVal);
palist = palist.addAttr(2, llvm::Attribute::ByVal);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M)->setAttributes(palist);
llvm::Function::Create(fty, llvm::GlobalValue::ExternalLinkage, fname, M);
}
/////////////////////////////////////////////////////////////////////////////////////
+14 -29
View File
@@ -676,7 +676,7 @@ struct Case : Object
}
};
static LLValue* call_string_switch_runtime(llvm::GlobalVariable* table, Expression* e)
static LLValue* call_string_switch_runtime(llvm::Value* table, Expression* e)
{
Type* dt = e->type->toBasetype();
Type* dtnext = dt->next->toBasetype();
@@ -705,27 +705,11 @@ static LLValue* call_string_switch_runtime(llvm::GlobalVariable* table, Expressi
assert(table->getType() == fn->getFunctionType()->getParamType(0));
DValue* val = e->toElem(gIR);
LLValue* llval;
if (DSliceValue* sval = val->isSlice())
{
// give storage
llval = DtoAlloca(DtoType(e->type), "tmp");
DVarValue* vv = new DVarValue(e->type, llval);
DtoAssign(e->loc, vv, val);
}
else
{
llval = val->getRVal();
}
LLValue* llval = val->getRVal();
assert(llval->getType() == fn->getFunctionType()->getParamType(1));
CallOrInvoke* call = gIR->CreateCallOrInvoke2(fn, table, llval, "tmp");
llvm::AttrListPtr palist;
palist = palist.addAttr(1, llvm::Attribute::ByVal);
palist = palist.addAttr(2, llvm::Attribute::ByVal);
call->setAttributes(palist);
return call->get();
}
@@ -748,7 +732,7 @@ void SwitchStatement::toIR(IRState* p)
}
// string switch?
llvm::GlobalVariable* switchTable = 0;
llvm::Value* switchTable = 0;
Array caseArray;
if (!condition->type->isintegral())
{
@@ -790,9 +774,7 @@ void SwitchStatement::toIR(IRState* p)
std::vector<LLConstant*> sinits;
sinits.push_back(DtoConstSize_t(inits.size()));
sinits.push_back(arrPtr);
LLConstant* sInit = llvm::ConstantStruct::get(sTy, sinits);
switchTable = new llvm::GlobalVariable(sTy, true, llvm::GlobalValue::InternalLinkage, sInit, ".string_switch_table", gIR->module);
switchTable = llvm::ConstantStruct::get(sTy, sinits);
}
// body block
@@ -1173,7 +1155,16 @@ void WithStatement::toIR(IRState* p)
DValue* e = exp->toElem(p);
#if 1
// this doesn't handle the mini/with2.d test case ...
assert(!wthis->ir.isSet());
wthis->ir.irLocal = new IrLocal(wthis);
wthis->ir.irLocal->value = DtoAlloca(DtoType(wthis->type), wthis->toChars());
#else
// ... this does, but it also silently breaks MiniD!!!
DtoDeclarationExp(wthis);
#endif
DtoStore(e->getRVal(), wthis->ir.irLocal->value);
body->toIR(p);
@@ -1269,15 +1260,10 @@ void SwitchErrorStatement::toIR(IRState* p)
llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_switch_error");
// param attrs
llvm::AttrListPtr palist;
int idx = 1;
std::vector<LLValue*> args;
// file param
args.push_back(gIR->dmodule->ir.irModule->fileName);
palist = palist.addAttr(idx++, llvm::Attribute::ByVal);
args.push_back(DtoLoad(gIR->dmodule->ir.irModule->fileName));
// line param
LLConstant* c = DtoConstUint(loc.linnum);
@@ -1285,7 +1271,6 @@ void SwitchErrorStatement::toIR(IRState* p)
// call
CallOrInvoke* call = gIR->CreateCallOrInvoke(fn, args.begin(), args.end());
call->setAttributes(palist);
gIR->ir->CreateUnreachable();
}
+33 -9
View File
@@ -175,7 +175,7 @@ void DtoBuildDVarArgList(std::vector<LLValue*>& args, llvm::AttrListPtr& palist,
true, llvm::GlobalValue::InternalLinkage, tiinits, "._arguments.array", gIR->module);
// specify arguments
args.push_back(typeinfoarrayparam);
args.push_back(DtoLoad(typeinfoarrayparam));
++argidx;
args.push_back(gIR->ir->CreateBitCast(mem, getPtrToType(LLType::Int8Ty), "tmp"));
++argidx;
@@ -319,11 +319,11 @@ DValue* DtoCallFunction(Loc& loc, Type* resulttype, DValue* fnval, Expressions*
LLValue* arg = argval->getRVal();
if (fnarg) // can fnarg ever be null in this block?
{
// if (Logger::enabled())
// {
// Logger::cout() << "arg: " << *arg << '\n';
// Logger::cout() << "expects: " << *callableTy->getParamType(j) << '\n';
// }
if (Logger::enabled())
{
Logger::cout() << "arg: " << *arg << '\n';
Logger::cout() << "expects: " << *callableTy->getParamType(j) << '\n';
}
if (arg->getType() != callableTy->getParamType(j))
arg = DtoBitCast(arg, callableTy->getParamType(j));
if (fnarg->llvmAttrs)
@@ -355,9 +355,33 @@ DValue* DtoCallFunction(Loc& loc, Type* resulttype, DValue* fnval, Expressions*
// get return value
LLValue* retllval = (retinptr) ? args[0] : call->get();
// if the type of retllval is abstract, refine to concrete
if (retllval->getType()->isAbstract())
retllval = DtoBitCast(retllval, getPtrToType(DtoType(resulttype)), "retval");
// repaint the type if necessary
if (resulttype)
{
Type* rbase = resulttype->toBasetype();
Type* nextbase = tf->next->toBasetype();
if (!rbase->equals(nextbase))
{
Logger::println("repainting return value from '%s' to '%s'", tf->next->toChars(), rbase->toChars());
switch(rbase->ty)
{
case Tarray:
retllval = DtoAggrPaint(retllval, DtoType(rbase));
break;
case Tclass:
case Taarray:
case Tpointer:
retllval = DtoBitCast(retllval, DtoType(rbase));
break;
default:
assert(0 && "unhandled repainting of return value");
}
if (Logger::enabled())
Logger::cout() << "final return value: " << *retllval << '\n';
}
}
// set calling convention and parameter attributes
if (dfnval && dfnval->func)
+18 -13
View File
@@ -783,21 +783,26 @@ DValue* CastExp::toElem(IRState* p)
Logger::print("CastExp::toElem: %s | %s\n", toChars(), type->toChars());
LOG_SCOPE;
// get the value to cast
DValue* u = e1->toElem(p);
DValue* v = DtoCast(loc, u, to);
// force d type to this->type
v->getType() = type;
if (v->isSlice()) {
// only valid as rvalue!
// cast it to the 'to' type, if necessary
DValue* v = u;
if (!to->equals(e1->type))
v = DtoCast(loc, u, to);
// paint the type, if necessary
if (!type->equals(to))
v = DtoPaintType(loc, v, type);
// slices are not valid lvalues
if (v->isSlice())
return v;
}
// if we're casting a lvalue, keep it around, we might be in a lvalue cast.
else if(u->isLVal())
return new DLRValue(u, v);
else
return v;
// otherwise just return the new value
return v;
}
//////////////////////////////////////////////////////////////////////////////////////////
@@ -811,7 +816,7 @@ LLConstant* CastExp::toConstElem(IRState* p)
const LLType* lltype = DtoType(type);
if(!isaPointer(c->getType()) || !isaPointer(lltype)) {
error("can only cast pointers to pointers at compile time, not %s to %s", type->toChars(), e1->type->toChars());
error("can only cast pointers to pointers at code generation time, not %s to %s", type->toChars(), e1->type->toChars());
fatal();
}
@@ -1552,8 +1557,8 @@ DValue* DeleteExp::toElem(IRState* p)
else if (et->ty == Tarray)
{
DtoDeleteArray(dval);
if (!dval->isSlice())
DtoSetArrayToNull(dval->getRVal());
if (dval->isLVal())
DtoSetArrayToNull(dval->getLVal());
}
// unknown/invalid
else
+29 -5
View File
@@ -25,14 +25,14 @@ bool DtoIsPassedByRef(Type* type)
{
Type* typ = type->toBasetype();
TY t = typ->ty;
return (t == Tstruct || t == Tarray || t == Tdelegate || t == Tsarray);
return (t == Tstruct || t == Tdelegate || t == Tsarray);
}
bool DtoIsReturnedInArg(Type* type)
{
Type* typ = type->toBasetype();
TY t = typ->ty;
return (t == Tstruct || t == Tarray || t == Tdelegate || t == Tsarray);
return (t == Tstruct || t == Tdelegate || t == Tsarray);
}
unsigned DtoShouldExtend(Type* type)
@@ -179,9 +179,6 @@ const LLType* DtoType(Type* t)
return DtoStructTypeFromArguments(ttupl->arguments);
}
*/
// opaque type
case Topaque:
return llvm::OpaqueType::get();
default:
printf("trying to convert unknown type '%s' with value %d\n", t->toChars(), t->ty);
@@ -560,6 +557,8 @@ LLConstant* DtoConstStringPtr(const char* str, const char* section)
LLValue* DtoLoad(LLValue* src, const char* name)
{
if (Logger::enabled())
Logger::cout() << "loading " << *src << '\n';
LLValue* ld = gIR->ir->CreateLoad(src, name ? name : "tmp");
//ld->setVolatile(gIR->func()->inVolatile);
return ld;
@@ -567,6 +566,8 @@ LLValue* DtoLoad(LLValue* src, const char* name)
void DtoStore(LLValue* src, LLValue* dst)
{
if (Logger::enabled())
Logger::cout() << "storing " << *src << " into " << *dst << '\n';
LLValue* st = gIR->ir->CreateStore(src,dst);
//st->setVolatile(gIR->func()->inVolatile);
}
@@ -577,6 +578,7 @@ LLValue* DtoBitCast(LLValue* v, const LLType* t, const char* name)
{
if (v->getType() == t)
return v;
assert(!(isaStruct(t) || isaStruct(v->getType())));
return gIR->ir->CreateBitCast(v, t, name ? name : "tmp");
}
@@ -806,3 +808,25 @@ LLValue* DtoAggrPair(const LLType* type, LLValue* V1, LLValue* V2, const char* n
res = gIR->ir->CreateInsertValue(res, V1, 0, "tmp");
return gIR->ir->CreateInsertValue(res, V2, 1, name?name:"tmp");
}
LLValue* DtoAggrPair(LLValue* V1, LLValue* V2, const char* name)
{
const LLType* t = LLStructType::get(V1->getType(), V2->getType(), 0);
return DtoAggrPair(t, V1, V2, name);
}
LLValue* DtoAggrPaint(LLValue* aggr, const LLType* as)
{
if (aggr->getType() == as)
return aggr;
LLValue* res = llvm::UndefValue::get(as);
LLValue* V = gIR->ir->CreateExtractValue(aggr, 0, "tmp");;
V = DtoBitCast(V, as->getContainedType(0));
res = gIR->ir->CreateInsertValue(res, V, 0, "tmp");
V = gIR->ir->CreateExtractValue(aggr, 1, "tmp");;
V = DtoBitCast(V, as->getContainedType(1));
return gIR->ir->CreateInsertValue(res, V, 1, "tmp");
}
+2
View File
@@ -98,6 +98,8 @@ unsigned char getPrefTypeAlign(const LLType* t);
// pair type helpers
LLValue* DtoAggrPair(const LLType* type, LLValue* V1, LLValue* V2, const char* name = 0);
LLValue* DtoAggrPair(LLValue* V1, LLValue* V2, const char* name = 0);
LLValue* DtoAggrPaint(LLValue* aggr, const LLType* as);
/**
* Generates a call to llvm.memset.i32 (or i64 depending on architecture).
+1 -1
View File
@@ -75,7 +75,7 @@ void Module::genobjfile(int multiobj, char** envp)
Logger::println("Generating module: %s\n", (md ? md->toChars() : toChars()));
LOG_SCOPE;
// printf("codegen: %s\n", srcfile->toChars());
//printf("codegen: %s\n", srcfile->toChars());
// start by deleting the old object file
deleteObjFile();