comparison gen/toir.cpp @ 602:48f079b4fe0f

Fixed ArrayLiteralExp::toConstElem for dynamic arrays, tango-user library should now be possible to build. It seems to be related to DMD bug 2356, which must have been introduced recently, as we already handled this fine for ArrayInitializers, just not ArrayLiterals... Kinda annoying to have to do this work due to DMD bugs ...
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Mon, 15 Sep 2008 15:48:59 +0200
parents 311f16f26297
children 10bc9eb9e262
comparison
equal deleted inserted replaced
601:03a5e609eef3 602:48f079b4fe0f
2208 LLConstant* ArrayLiteralExp::toConstElem(IRState* p) 2208 LLConstant* ArrayLiteralExp::toConstElem(IRState* p)
2209 { 2209 {
2210 Logger::print("ArrayLiteralExp::toConstElem: %s | %s\n", toChars(), type->toChars()); 2210 Logger::print("ArrayLiteralExp::toConstElem: %s | %s\n", toChars(), type->toChars());
2211 LOG_SCOPE; 2211 LOG_SCOPE;
2212 2212
2213 const LLType* t = DtoType(type); 2213 // extract D types
2214 Logger::cout() << "array literal has llvm type: " << *t << '\n'; 2214 Type* bt = type->toBasetype();
2215 assert(isaArray(t)); 2215 Type* elemt = bt->next;
2216 const LLArrayType* arrtype = isaArray(t); 2216
2217 2217 // build llvm array type
2218 assert(arrtype->getNumElements() == elements->dim); 2218 const LLArrayType* arrtype = LLArrayType::get(DtoType(elemt), elements->dim);
2219
2220 // dynamic arrays can occur here as well ...
2221 bool dyn = (bt->ty == Tsarray);
2222 if (!dyn)
2223 assert(arrtype->getNumElements() == elements->dim);
2224 else
2225 assert(bt->ty == Tarray);
2226
2227 // build the initializer
2219 std::vector<LLConstant*> vals(elements->dim, NULL); 2228 std::vector<LLConstant*> vals(elements->dim, NULL);
2220 for (unsigned i=0; i<elements->dim; ++i) 2229 for (unsigned i=0; i<elements->dim; ++i)
2221 { 2230 {
2222 Expression* expr = (Expression*)elements->data[i]; 2231 Expression* expr = (Expression*)elements->data[i];
2223 vals[i] = expr->toConstElem(p); 2232 vals[i] = expr->toConstElem(p);
2224 } 2233 }
2225 2234
2226 return llvm::ConstantArray::get(arrtype, vals); 2235 // build the constant array initializer
2236 LLConstant* initval = llvm::ConstantArray::get(arrtype, vals);
2237
2238 // if static array, we're done
2239 if (!dyn)
2240 return initval;
2241
2242 // for dynamic arrays we need to put the initializer in a global, and build a constant dynamic array reference with the .ptr field pointing into this global
2243 LLConstant* globalstore = new LLGlobalVariable(arrtype, true, LLGlobalValue::InternalLinkage, initval, ".dynarrayStorage", p->module);
2244 LLConstant* idxs[2] = { DtoConstUint(0), DtoConstUint(0) };
2245 LLConstant* globalstorePtr = llvm::ConstantExpr::getGetElementPtr(globalstore, idxs, 2);
2246
2247 return DtoConstSlice(DtoConstSize_t(elements->dim), globalstorePtr);
2227 } 2248 }
2228 2249
2229 ////////////////////////////////////////////////////////////////////////////////////////// 2250 //////////////////////////////////////////////////////////////////////////////////////////
2230 2251
2231 DValue* StructLiteralExp::toElem(IRState* p) 2252 DValue* StructLiteralExp::toElem(IRState* p)