comparison gen/llvmhelpers.cpp @ 1255:9014d7f0433f

Rewrote runtime struct literal codegen.
author Tomas Lindquist Olsen <tomas.l.olsen gmail com>
date Wed, 22 Apr 2009 03:08:28 +0200
parents 79758fd2f48a
children 0686701178d3
comparison
equal deleted inserted replaced
1254:747fdd9245d7 1255:9014d7f0433f
1180 } 1180 }
1181 assert(0); 1181 assert(0);
1182 } 1182 }
1183 1183
1184 return exp->toConstElem(gIR); 1184 return exp->toConstElem(gIR);
1185 }
1186
1187 //////////////////////////////////////////////////////////////////////////////////////////
1188
1189 static LLValue* expand_value_to_sarray(Type *base, Expression* exp)
1190 {
1191 Logger::println("building type %s from expression (%s) of type %s", base->toChars(), exp->toChars(), exp->type->toChars());
1192 const LLType* dstTy = DtoType(base);
1193 if (Logger::enabled())
1194 Logger::cout() << "final llvm type requested: " << *dstTy << '\n';
1195
1196 // get initial value
1197 LLValue* val = exp->toElem(gIR)->getRVal();
1198 if (DtoIsPassedByRef(exp->type))
1199 val = DtoLoad(val);
1200
1201 Type* expbase = exp->type->toBasetype();
1202 Logger::println("expbase: %s", expbase->toChars());
1203 Type* t = base->toBasetype();
1204
1205 LLSmallVector<size_t, 4> dims;
1206
1207 while(1)
1208 {
1209 Logger::println("t: %s", t->toChars());
1210 if (t->equals(expbase))
1211 break;
1212 assert(t->ty == Tsarray);
1213 TypeSArray* tsa = (TypeSArray*)t;
1214 dims.push_back(tsa->dim->toInteger());
1215 assert(t->nextOf());
1216 t = t->nextOf()->toBasetype();
1217 }
1218
1219 size_t i = dims.size();
1220 assert(i);
1221
1222 std::vector<LLValue*> inits;
1223 while (i--)
1224 {
1225 // start with undefined array
1226 const LLArrayType* arrty = LLArrayType::get(val->getType(), dims[i]);
1227 LLValue* tmp = llvm::UndefValue::get(arrty);
1228 for (size_t j = 0; j < dims[i]; j++)
1229 {
1230 tmp = gIR->ir->CreateInsertValue(tmp, val, j);
1231 }
1232 val = tmp;
1233 }
1234
1235 return val;
1236 }
1237
1238 LLValue* DtoExprValue(Type* type, Expression* e)
1239 {
1240 Type* t1 = e->type->toBasetype();
1241 Type* t2 = type->toBasetype();
1242
1243 // expand static arrays
1244 if (t2->ty == Tsarray && !t1->equals(t2))
1245 {
1246 return expand_value_to_sarray(t2, e);
1247 }
1248 // or not
1249 else
1250 {
1251 DValue* dv = e->toElem(gIR);
1252 LLValue* v = dv->getRVal();
1253 if (DtoIsPassedByRef(e->type))
1254 v = DtoLoad(v);
1255 return v;
1256 }
1185 } 1257 }
1186 1258
1187 ////////////////////////////////////////////////////////////////////////////////////////// 1259 //////////////////////////////////////////////////////////////////////////////////////////
1188 1260
1189 void DtoAnnotation(const char* str) 1261 void DtoAnnotation(const char* str)