comparison gen/tollvm.cpp @ 133:44a95ac7368a trunk

[svn r137] Many fixes towards tango.io.Console working, but not quite there yet... In particular, assertions has been fixed to include file/line info, and much more!
author lindquist
date Mon, 14 Jan 2008 05:11:54 +0100
parents 1700239cab2e
children 176bd52b3cf5
comparison
equal deleted inserted replaced
132:1700239cab2e 133:44a95ac7368a
80 80
81 // pointers 81 // pointers
82 case Tpointer: { 82 case Tpointer: {
83 assert(t->next); 83 assert(t->next);
84 if (t->next->ty == Tvoid) 84 if (t->next->ty == Tvoid)
85 return (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); 85 return (const llvm::Type*)getPtrToType(llvm::Type::Int8Ty);
86 else 86 else
87 return (const llvm::Type*)llvm::PointerType::get(DtoType(t->next)); 87 return (const llvm::Type*)getPtrToType(DtoType(t->next));
88 } 88 }
89 89
90 // arrays 90 // arrays
91 case Tarray: 91 case Tarray:
92 return DtoArrayType(t); 92 return DtoArrayType(t);
115 } 115 }
116 116
117 TypeStruct* ts = (TypeStruct*)t; 117 TypeStruct* ts = (TypeStruct*)t;
118 assert(ts->sym); 118 assert(ts->sym);
119 DtoResolveDsymbol(ts->sym); 119 DtoResolveDsymbol(ts->sym);
120 return t->llvmType->get(); 120 return ts->sym->llvmIRStruct->recty.get();//t->llvmType->get();
121 } 121 }
122 122
123 case Tclass: { 123 case Tclass: {
124 if (!t->llvmType || *t->llvmType == NULL) { 124 /*if (!t->llvmType || *t->llvmType == NULL) {
125 // recursive or cyclic declaration 125 // recursive or cyclic declaration
126 if (!gIR->structs.empty()) 126 if (!gIR->structs.empty())
127 { 127 {
128 IRStruct* found = 0; 128 IRStruct* found = 0;
129 for (IRState::StructVector::iterator i=gIR->structs.begin(); i!=gIR->structs.end(); ++i) 129 for (IRState::StructVector::iterator i=gIR->structs.begin(); i!=gIR->structs.end(); ++i)
130 { 130 {
131 if (t == (*i)->type) 131 if (t == (*i)->type)
132 { 132 {
133 return llvm::PointerType::get((*i)->recty.get()); 133 return getPtrToType((*i)->recty.get());
134 } 134 }
135 } 135 }
136 } 136 }
137 Logger::println("no type found"); 137 Logger::println("no type found");
138 } 138 }*/
139 139
140 TypeClass* tc = (TypeClass*)t; 140 TypeClass* tc = (TypeClass*)t;
141 assert(tc->sym); 141 assert(tc->sym);
142 DtoResolveDsymbol(tc->sym); 142 DtoResolveDsymbol(tc->sym);
143 return llvm::PointerType::get(t->llvmType->get()); 143 return getPtrToType(tc->sym->llvmIRStruct->recty.get());//t->llvmType->get());
144 } 144 }
145 145
146 // functions 146 // functions
147 case Tfunction: 147 case Tfunction:
148 { 148 {
180 { 180 {
181 TypeAArray* taa = (TypeAArray*)t; 181 TypeAArray* taa = (TypeAArray*)t;
182 std::vector<const llvm::Type*> types; 182 std::vector<const llvm::Type*> types;
183 types.push_back(DtoType(taa->key)); 183 types.push_back(DtoType(taa->key));
184 types.push_back(DtoType(taa->next)); 184 types.push_back(DtoType(taa->next));
185 return llvm::PointerType::get(llvm::StructType::get(types)); 185 return getPtrToType(llvm::StructType::get(types));
186 } 186 }
187 187
188 default: 188 default:
189 printf("trying to convert unknown type with value %d\n", t->ty); 189 printf("trying to convert unknown type with value %d\n", t->ty);
190 assert(0); 190 assert(0);
194 194
195 ////////////////////////////////////////////////////////////////////////////////////////// 195 //////////////////////////////////////////////////////////////////////////////////////////
196 196
197 const llvm::StructType* DtoDelegateType(Type* t) 197 const llvm::StructType* DtoDelegateType(Type* t)
198 { 198 {
199 const llvm::Type* i8ptr = llvm::PointerType::get(llvm::Type::Int8Ty); 199 const llvm::Type* i8ptr = getPtrToType(llvm::Type::Int8Ty);
200 const llvm::Type* func = DtoFunctionType(t->next, i8ptr); 200 const llvm::Type* func = DtoFunctionType(t->next, i8ptr);
201 const llvm::Type* funcptr = llvm::PointerType::get(func); 201 const llvm::Type* funcptr = getPtrToType(func);
202 202
203 std::vector<const llvm::Type*> types; 203 std::vector<const llvm::Type*> types;
204 types.push_back(i8ptr); 204 types.push_back(i8ptr);
205 types.push_back(funcptr); 205 types.push_back(funcptr);
206 return llvm::StructType::get(types); 206 return llvm::StructType::get(types);
212 { 212 {
213 assert(bits == 32 || bits == 64); 213 assert(bits == 32 || bits == 64);
214 const llvm::Type* int8ty = (const llvm::Type*)llvm::Type::Int8Ty; 214 const llvm::Type* int8ty = (const llvm::Type*)llvm::Type::Int8Ty;
215 const llvm::Type* int32ty = (const llvm::Type*)llvm::Type::Int32Ty; 215 const llvm::Type* int32ty = (const llvm::Type*)llvm::Type::Int32Ty;
216 const llvm::Type* int64ty = (const llvm::Type*)llvm::Type::Int64Ty; 216 const llvm::Type* int64ty = (const llvm::Type*)llvm::Type::Int64Ty;
217 const llvm::Type* int8ptrty = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); 217 const llvm::Type* int8ptrty = (const llvm::Type*)getPtrToType(llvm::Type::Int8Ty);
218 const llvm::Type* voidty = (const llvm::Type*)llvm::Type::VoidTy; 218 const llvm::Type* voidty = (const llvm::Type*)llvm::Type::VoidTy;
219 219
220 assert(gIR); 220 assert(gIR);
221 assert(gIR->module); 221 assert(gIR->module);
222 222
282 llvm::Value* DtoNullDelegate(llvm::Value* v) 282 llvm::Value* DtoNullDelegate(llvm::Value* v)
283 { 283 {
284 assert(gIR); 284 assert(gIR);
285 d_uns64 n = (global.params.is64bit) ? 16 : 8; 285 d_uns64 n = (global.params.is64bit) ? 16 : 8;
286 286
287 llvm::Type* i8p_ty = llvm::PointerType::get(llvm::Type::Int8Ty); 287 const llvm::Type* i8p_ty = getPtrToType(llvm::Type::Int8Ty);
288 288
289 llvm::Value* arr = new llvm::BitCastInst(v,i8p_ty,"tmp",gIR->scopebb()); 289 llvm::Value* arr = new llvm::BitCastInst(v,i8p_ty,"tmp",gIR->scopebb());
290 290
291 llvm::Function* fn = LLVM_DeclareMemSet32(); 291 llvm::Function* fn = LLVM_DeclareMemSet32();
292 std::vector<llvm::Value*> llargs; 292 std::vector<llvm::Value*> llargs;
308 assert(dst->getType() == src->getType()); 308 assert(dst->getType() == src->getType());
309 assert(gIR); 309 assert(gIR);
310 310
311 d_uns64 n = (global.params.is64bit) ? 16 : 8; 311 d_uns64 n = (global.params.is64bit) ? 16 : 8;
312 312
313 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); 313 const llvm::Type* arrty = getPtrToType(llvm::Type::Int8Ty);
314 314
315 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); 315 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb());
316 llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb()); 316 llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb());
317 317
318 llvm::Function* fn = LLVM_DeclareMemCpy32(); 318 llvm::Function* fn = LLVM_DeclareMemCpy32();
432 llvm::Value* zero = llvm::ConstantInt::get(t, 0, false); 432 llvm::Value* zero = llvm::ConstantInt::get(t, 0, false);
433 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, val, zero, "tmp", gIR->scopebb()); 433 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, val, zero, "tmp", gIR->scopebb());
434 } 434 }
435 } 435 }
436 else if (isaPointer(t)) { 436 else if (isaPointer(t)) {
437 const llvm::Type* st = DtoSize_t(); 437 llvm::Value* zero = llvm::Constant::getNullValue(t);
438 llvm::Value* ptrasint = new llvm::PtrToIntInst(val,st,"tmp",gIR->scopebb()); 438 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, val, zero, "tmp", gIR->scopebb());
439 llvm::Value* zero = llvm::ConstantInt::get(st, 0, false);
440 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, ptrasint, zero, "tmp", gIR->scopebb());
441 } 439 }
442 else 440 else
443 { 441 {
444 Logger::cout() << *t << '\n'; 442 Logger::cout() << *t << '\n';
445 } 443 }
613 llvm::Value* DtoRealloc(llvm::Value* ptr, const llvm::Type* ty) 611 llvm::Value* DtoRealloc(llvm::Value* ptr, const llvm::Type* ty)
614 { 612 {
615 /*size_t sz = gTargetData->getTypeSize(ty); 613 /*size_t sz = gTargetData->getTypeSize(ty);
616 llvm::ConstantInt* n = llvm::ConstantInt::get(DtoSize_t(), sz, false); 614 llvm::ConstantInt* n = llvm::ConstantInt::get(DtoSize_t(), sz, false);
617 if (ptr == 0) { 615 if (ptr == 0) {
618 llvm::PointerType* i8pty = llvm::PointerType::get(llvm::Type::Int8Ty); 616 llvm::PointerType* i8pty = getPtrToType(llvm::Type::Int8Ty);
619 ptr = llvm::ConstantPointerNull::get(i8pty); 617 ptr = llvm::ConstantPointerNull::get(i8pty);
620 } 618 }
621 return DtoRealloc(ptr, n);*/ 619 return DtoRealloc(ptr, n);*/
622 return NULL; 620 return NULL;
623 } 621 }
632 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_realloc"); 630 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_realloc");
633 assert(fn); 631 assert(fn);
634 632
635 llvm::Value* newptr = ptr; 633 llvm::Value* newptr = ptr;
636 634
637 llvm::PointerType* i8pty = llvm::PointerType::get(llvm::Type::Int8Ty); 635 const llvm::PointerType* i8pty = getPtrToType(llvm::Type::Int8Ty);
638 if (ptr->getType() != i8pty) { 636 if (ptr->getType() != i8pty) {
639 newptr = new llvm::BitCastInst(ptr,i8pty,"tmp",gIR->scopebb()); 637 newptr = new llvm::BitCastInst(ptr,i8pty,"tmp",gIR->scopebb());
640 } 638 }
641 639
642 std::vector<llvm::Value*> args; 640 std::vector<llvm::Value*> args;
647 return ret->getType() == ptr->getType() ? ret : new llvm::BitCastInst(ret,ptr->getType(),"tmp",gIR->scopebb()); 645 return ret->getType() == ptr->getType() ? ret : new llvm::BitCastInst(ret,ptr->getType(),"tmp",gIR->scopebb());
648 } 646 }
649 647
650 ////////////////////////////////////////////////////////////////////////////////////////// 648 //////////////////////////////////////////////////////////////////////////////////////////
651 649
652 void DtoAssert(llvm::Value* cond, Loc* loc, DValue* msg) 650 void DtoAssert(Loc* loc, DValue* msg)
653 { 651 {
654 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_assert"); 652 std::vector<llvm::Value*> args;
655 const llvm::FunctionType* fnt = fn->getFunctionType(); 653 llvm::Constant* c;
656 654
657 std::vector<llvm::Value*> llargs; 655 // func
658 llargs.resize(3); 656 const char* fname = msg ? "_d_assert_msg" : "_d_assert";
659 llargs[0] = cond ? DtoBoolean(cond) : llvm::ConstantInt::getFalse(); 657
660 llargs[1] = DtoConstUint(loc->linnum); 658 // msg param
661 if (msg) 659 if (msg) args.push_back(msg->getRVal());
662 llargs[2] = msg->getRVal(); 660
663 else { 661 // file param
664 llvm::Constant* c = DtoConstSlice(DtoConstSize_t(0), DtoConstNullPtr(llvm::Type::Int8Ty)); 662 c = DtoConstString(loc->filename);
665 static llvm::AllocaInst* alloc = 0; 663 llvm::AllocaInst* alloc = new llvm::AllocaInst(c->getType(), "srcfile", gIR->topallocapoint());
666 if (!alloc || alloc->getParent()->getParent() != gIR->func()->func) { 664 llvm::Value* ptr = DtoGEPi(alloc, 0,0, "tmp");
667 alloc = new llvm::AllocaInst(c->getType(), "assertnullparam", gIR->topallocapoint()); 665 DtoStore(c->getOperand(0), ptr);
668 DtoSetArrayToNull(alloc); 666 ptr = DtoGEPi(alloc, 0,1, "tmp");
669 } 667 DtoStore(c->getOperand(1), ptr);
670 llargs[2] = alloc; 668 args.push_back(alloc);
671 } 669
672 670 // line param
673 assert(fn); 671 c = DtoConstUint(loc->linnum);
674 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); 672 args.push_back(c);
675 call->setCallingConv(llvm::CallingConv::C); 673
674 // call
675 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, fname);
676 llvm::CallInst* call = new llvm::CallInst(fn, args.begin(), args.end(), "", gIR->scopebb());
676 } 677 }
677 678
678 ////////////////////////////////////////////////////////////////////////////////////////// 679 //////////////////////////////////////////////////////////////////////////////////////////
679 680
680 static const llvm::Type* get_next_frame_ptr_type(Dsymbol* sc) 681 static const llvm::Type* get_next_frame_ptr_type(Dsymbol* sc)
1189 1190
1190 llvm::ConstantFP* DtoConstFP(Type* t, long double value) 1191 llvm::ConstantFP* DtoConstFP(Type* t, long double value)
1191 { 1192 {
1192 TY ty = DtoDType(t)->ty; 1193 TY ty = DtoDType(t)->ty;
1193 if (ty == Tfloat32 || ty == Timaginary32) 1194 if (ty == Tfloat32 || ty == Timaginary32)
1194 return llvm::ConstantFP::get(llvm::Type::FloatTy, float(value)); 1195 return llvm::ConstantFP::get(llvm::Type::FloatTy, llvm::APFloat(float(value)));
1195 else if (ty == Tfloat64 || ty == Timaginary64 || ty == Tfloat80 || ty == Timaginary80) 1196 else if (ty == Tfloat64 || ty == Timaginary64 || ty == Tfloat80 || ty == Timaginary80)
1196 return llvm::ConstantFP::get(llvm::Type::DoubleTy, double(value)); 1197 return llvm::ConstantFP::get(llvm::Type::DoubleTy, llvm::APFloat(double(value)));
1197 } 1198 }
1198 1199
1199 1200
1200 ////////////////////////////////////////////////////////////////////////////////////////// 1201 //////////////////////////////////////////////////////////////////////////////////////////
1201 1202
1225 ////////////////////////////////////////////////////////////////////////////////////////// 1226 //////////////////////////////////////////////////////////////////////////////////////////
1226 1227
1227 llvm::Constant* DtoConstNullPtr(const llvm::Type* t) 1228 llvm::Constant* DtoConstNullPtr(const llvm::Type* t)
1228 { 1229 {
1229 return llvm::ConstantPointerNull::get( 1230 return llvm::ConstantPointerNull::get(
1230 llvm::PointerType::get(t) 1231 getPtrToType(t)
1231 ); 1232 );
1232 } 1233 }
1233 1234
1234 ////////////////////////////////////////////////////////////////////////////////////////// 1235 //////////////////////////////////////////////////////////////////////////////////////////
1235 1236
1236 void DtoMemSetZero(llvm::Value* dst, llvm::Value* nbytes) 1237 void DtoMemSetZero(llvm::Value* dst, llvm::Value* nbytes)
1237 { 1238 {
1238 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); 1239 const llvm::Type* arrty = getPtrToType(llvm::Type::Int8Ty);
1239 llvm::Value *dstarr; 1240 llvm::Value *dstarr;
1240 if (dst->getType() == arrty) 1241 if (dst->getType() == arrty)
1241 { 1242 {
1242 dstarr = dst; 1243 dstarr = dst;
1243 } 1244 }
1261 1262
1262 void DtoMemCpy(llvm::Value* dst, llvm::Value* src, llvm::Value* nbytes) 1263 void DtoMemCpy(llvm::Value* dst, llvm::Value* src, llvm::Value* nbytes)
1263 { 1264 {
1264 assert(dst->getType() == src->getType()); 1265 assert(dst->getType() == src->getType());
1265 1266
1266 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); 1267 const llvm::Type* arrty = getPtrToType(llvm::Type::Int8Ty);
1267 llvm::Value *dstarr, *srcarr; 1268 llvm::Value *dstarr, *srcarr;
1268 if (dst->getType() == arrty) 1269 if (dst->getType() == arrty)
1269 { 1270 {
1270 dstarr = dst; 1271 dstarr = dst;
1271 srcarr = src; 1272 srcarr = src;
1307 return false; 1308 return false;
1308 } 1309 }
1309 1310
1310 ////////////////////////////////////////////////////////////////////////////////////////// 1311 //////////////////////////////////////////////////////////////////////////////////////////
1311 1312
1312 llvm::Value* DtoBitCast(llvm::Value* v, const llvm::Type* t) 1313 llvm::Value* DtoBitCast(llvm::Value* v, const llvm::Type* t, const char* name)
1313 { 1314 {
1314 if (v->getType() == t) 1315 if (v->getType() == t)
1315 return v; 1316 return v;
1316 return gIR->ir->CreateBitCast(v, t, "tmp"); 1317 return gIR->ir->CreateBitCast(v, t, name ? name : "tmp");
1317 } 1318 }
1318 1319
1319 ////////////////////////////////////////////////////////////////////////////////////////// 1320 //////////////////////////////////////////////////////////////////////////////////////////
1320 1321
1321 const llvm::PointerType* isaPointer(llvm::Value* v) 1322 const llvm::PointerType* isaPointer(llvm::Value* v)
1364 } 1365 }
1365 1366
1366 llvm::GlobalVariable* isaGlobalVar(llvm::Value* v) 1367 llvm::GlobalVariable* isaGlobalVar(llvm::Value* v)
1367 { 1368 {
1368 return llvm::dyn_cast<llvm::GlobalVariable>(v); 1369 return llvm::dyn_cast<llvm::GlobalVariable>(v);
1370 }
1371
1372 //////////////////////////////////////////////////////////////////////////////////////////
1373
1374 const llvm::PointerType* getPtrToType(const llvm::Type* t)
1375 {
1376 return llvm::PointerType::get(t, 0);
1377 }
1378
1379 llvm::ConstantPointerNull* getNullPtr(const llvm::Type* t)
1380 {
1381 const llvm::PointerType* pt = llvm::cast<llvm::PointerType>(t);
1382 return llvm::ConstantPointerNull::get(pt);
1383 }
1384
1385 //////////////////////////////////////////////////////////////////////////////////////////
1386
1387 size_t getTypeBitSize(const llvm::Type* t)
1388 {
1389 return gTargetData->getTypeSizeInBits(t);
1390 }
1391
1392 size_t getTypeStoreSize(const llvm::Type* t)
1393 {
1394 return gTargetData->getTypeStoreSize(t);
1395 }
1396
1397 size_t getABITypeSize(const llvm::Type* t)
1398 {
1399 return gTargetData->getABITypeSize(t);
1369 } 1400 }
1370 1401
1371 ////////////////////////////////////////////////////////////////////////////////////////// 1402 //////////////////////////////////////////////////////////////////////////////////////////
1372 1403
1373 bool DtoIsTemplateInstance(Dsymbol* s) 1404 bool DtoIsTemplateInstance(Dsymbol* s)
1654 ////////////////////////////////////////////////////////////////////////////////////////// 1685 //////////////////////////////////////////////////////////////////////////////////////////
1655 1686
1656 void DtoForceDeclareDsymbol(Dsymbol* dsym) 1687 void DtoForceDeclareDsymbol(Dsymbol* dsym)
1657 { 1688 {
1658 if (dsym->llvmDeclared) return; 1689 if (dsym->llvmDeclared) return;
1659 Logger::println("DtoForceDeclareDsymbol(%s)", dsym->toChars()); 1690 Logger::println("DtoForceDeclareDsymbol(%s)", dsym->toPrettyChars());
1660 LOG_SCOPE; 1691 LOG_SCOPE;
1661 DtoResolveDsymbol(dsym); 1692 DtoResolveDsymbol(dsym);
1662 1693
1663 DtoEmptyResolveList(); 1694 DtoEmptyResolveList();
1664 1695
1668 ////////////////////////////////////////////////////////////////////////////////////////// 1699 //////////////////////////////////////////////////////////////////////////////////////////
1669 1700
1670 void DtoForceConstInitDsymbol(Dsymbol* dsym) 1701 void DtoForceConstInitDsymbol(Dsymbol* dsym)
1671 { 1702 {
1672 if (dsym->llvmInitialized) return; 1703 if (dsym->llvmInitialized) return;
1673 Logger::println("DtoForceConstInitDsymbol(%s)", dsym->toChars()); 1704 Logger::println("DtoForceConstInitDsymbol(%s)", dsym->toPrettyChars());
1674 LOG_SCOPE; 1705 LOG_SCOPE;
1675 DtoResolveDsymbol(dsym); 1706 DtoResolveDsymbol(dsym);
1676 1707
1677 DtoEmptyResolveList(); 1708 DtoEmptyResolveList();
1678 DtoEmptyDeclareList(); 1709 DtoEmptyDeclareList();
1683 ////////////////////////////////////////////////////////////////////////////////////////// 1714 //////////////////////////////////////////////////////////////////////////////////////////
1684 1715
1685 void DtoForceDefineDsymbol(Dsymbol* dsym) 1716 void DtoForceDefineDsymbol(Dsymbol* dsym)
1686 { 1717 {
1687 if (dsym->llvmDefined) return; 1718 if (dsym->llvmDefined) return;
1688 Logger::println("DtoForceDefineDsymbol(%s)", dsym->toChars()); 1719 Logger::println("DtoForceDefineDsymbol(%s)", dsym->toPrettyChars());
1689 LOG_SCOPE; 1720 LOG_SCOPE;
1690 DtoResolveDsymbol(dsym); 1721 DtoResolveDsymbol(dsym);
1691 1722
1692 DtoEmptyResolveList(); 1723 DtoEmptyResolveList();
1693 DtoEmptyDeclareList(); 1724 DtoEmptyDeclareList();