comparison gen/toir.c @ 52:0c77619e803b trunk

[svn r56] Initial support for TypeInfo. Enums not work. Several other bugfixes.
author lindquist
date Tue, 23 Oct 2007 05:55:12 +0200
parents 61bc1b4ad3c4
children 06ccc817acd4
comparison
equal deleted inserted replaced
51:61bc1b4ad3c4 52:0c77619e803b
81 } 81 }
82 // alias declaration 82 // alias declaration
83 else if (AliasDeclaration* a = declaration->isAliasDeclaration()) 83 else if (AliasDeclaration* a = declaration->isAliasDeclaration())
84 { 84 {
85 Logger::println("AliasDeclaration"); 85 Logger::println("AliasDeclaration");
86 assert(0);
87 }
88 else if (EnumDeclaration* e = declaration->isEnumDeclaration())
89 {
90 // do nothing
86 } 91 }
87 // unsupported declaration 92 // unsupported declaration
88 else 93 else
89 { 94 {
90 error("Only Var/Struct-Declaration is supported for DeclarationExp"); 95 error("Only Var/Struct-Declaration is supported for DeclarationExp");
91 fatal(); 96 assert(0);
92 } 97 }
93 return e; 98 return e;
94 } 99 }
95 100
96 ////////////////////////////////////////////////////////////////////////////////////////// 101 //////////////////////////////////////////////////////////////////////////////////////////
136 e->type = elem::VAL; 141 e->type = elem::VAL;
137 } 142 }
138 // typeinfo 143 // typeinfo
139 else if (TypeInfoDeclaration* tid = vd->isTypeInfoDeclaration()) 144 else if (TypeInfoDeclaration* tid = vd->isTypeInfoDeclaration())
140 { 145 {
141 tid->toObjFile(); 146 assert(0);
142 e->mem = tid->llvmValue;
143 e->type = elem::VAR;
144 } 147 }
145 // global forward ref 148 // global forward ref
146 else { 149 else {
147 Logger::println("unsupported: %s\n", vd->toChars()); 150 Logger::println("unsupported: %s\n", vd->toChars());
148 assert(0 && "only magic supported is typeinfo"); 151 assert(0 && "only magic supported is typeinfo");
179 } 182 }
180 } 183 }
181 else { 184 else {
182 // nested variable 185 // nested variable
183 if (vd->nestedref) { 186 if (vd->nestedref) {
184 /*
185 FuncDeclaration* fd = vd->toParent()->isFuncDeclaration();
186 assert(fd != NULL);
187 llvm::Value* ptr = NULL;
188 // inside nested function
189 if (fd != p->func().decl) {
190 ptr = p->func().decl->llvmThisVar;
191 Logger::cout() << "nested var reference:" << '\n' << *ptr << *vd->llvmValue->getType() << '\n';
192 ptr = p->ir->CreateBitCast(ptr, vd->llvmValue->getType(), "tmp");
193 }
194 // inside the actual parent function
195 else {
196 ptr = vd->llvmValue;
197 }
198 assert(ptr);
199 e->mem = LLVM_DtoGEPi(ptr,0,unsigned(vd->llvmNestedIndex),"tmp",p->scopebb());
200 */
201 e->mem = LLVM_DtoNestedVariable(vd); 187 e->mem = LLVM_DtoNestedVariable(vd);
202 } 188 }
203 // normal local variable 189 // normal local variable
204 else { 190 else {
205 e->mem = vd->llvmValue; 191 if (TypeInfoDeclaration* tid = vd->isTypeInfoDeclaration()) {
192 Logger::println("typeinfo varexp");
193 const llvm::Type* vartype = LLVM_DtoType(type);
194 if (tid->llvmValue->getType() != llvm::PointerType::get(vartype)) {
195 e->mem = p->ir->CreateBitCast(tid->llvmValue, vartype, "tmp");
196 }
197 else {
198 e->mem = tid->llvmValue;
199 }
200 Logger::cout() << "got:" << '\n' << *tid->llvmValue << "returned:" << '\n' << *e->mem << '\n';
201 }
202 else {
203 e->mem = vd->llvmValue;
204 }
206 } 205 }
207 e->vardecl = vd; 206 e->vardecl = vd;
208 e->type = elem::VAR; 207 e->type = elem::VAR;
209 } 208 }
210 } 209 }
348 347
349 ////////////////////////////////////////////////////////////////////////////////////////// 348 //////////////////////////////////////////////////////////////////////////////////////////
350 349
351 elem* StringExp::toElem(IRState* p) 350 elem* StringExp::toElem(IRState* p)
352 { 351 {
353 Logger::print("StringExp::toElem: %s | \n", toChars(), type->toChars()); 352 Logger::print("StringExp::toElem: %s | %s\n", toChars(), type->toChars());
354 LOG_SCOPE; 353 LOG_SCOPE;
355 354
356 Type* dtype = LLVM_DtoDType(type); 355 Type* dtype = LLVM_DtoDType(type);
357 356
358 assert(dtype->next->ty == Tchar && "Only char is supported"); 357 assert(dtype->next->ty == Tchar && "Only char is supported");
377 elem* e = new elem; 376 elem* e = new elem;
378 377
379 if (dtype->ty == Tarray) { 378 if (dtype->ty == Tarray) {
380 llvm::Constant* clen = llvm::ConstantInt::get(LLVM_DtoSize_t(),len,false); 379 llvm::Constant* clen = llvm::ConstantInt::get(LLVM_DtoSize_t(),len,false);
381 if (p->lvals.empty() || !p->toplval()) { 380 if (p->lvals.empty() || !p->toplval()) {
382 e->type = elem::SLICE; 381 llvm::Value* tmpmem = new llvm::AllocaInst(LLVM_DtoType(dtype),"tmp",p->topallocapoint());
383 e->arg = clen; 382 LLVM_DtoSetArray(tmpmem, clen, arrptr);
384 e->mem = arrptr; 383 e->mem = tmpmem;
385 return e;
386 } 384 }
387 else if (llvm::Value* arr = p->toplval()) { 385 else if (llvm::Value* arr = p->toplval()) {
388 if (llvm::isa<llvm::GlobalVariable>(arr)) { 386 if (llvm::isa<llvm::GlobalVariable>(arr)) {
389 e->val = LLVM_DtoConstantSlice(clen, arrptr); 387 e->val = LLVM_DtoConstantSlice(clen, arrptr);
390 } 388 }
1245 e->val = new llvm::LoadInst(ptr, "tmp", p->scopebb()); 1243 e->val = new llvm::LoadInst(ptr, "tmp", p->scopebb());
1246 e->type = elem::VAL; 1244 e->type = elem::VAL;
1247 } 1245 }
1248 else if (totype->ty == Tarray) { 1246 else if (totype->ty == Tarray) {
1249 Logger::cout() << "to array" << '\n'; 1247 Logger::cout() << "to array" << '\n';
1250 assert(fromtype->next->size() == totype->next->size());
1251 const llvm::Type* ptrty = LLVM_DtoType(totype->next); 1248 const llvm::Type* ptrty = LLVM_DtoType(totype->next);
1252 if (ptrty == llvm::Type::VoidTy) 1249 if (ptrty == llvm::Type::VoidTy)
1253 ptrty = llvm::Type::Int8Ty; 1250 ptrty = llvm::Type::Int8Ty;
1254 ptrty = llvm::PointerType::get(ptrty); 1251 ptrty = llvm::PointerType::get(ptrty);
1255 1252
1253 const llvm::Type* ety = LLVM_DtoType(fromtype->next);
1254 if (ety == llvm::Type::VoidTy)
1255 ety = llvm::Type::Int8Ty;
1256
1256 if (u->type == elem::SLICE) { 1257 if (u->type == elem::SLICE) {
1257 e->mem = new llvm::BitCastInst(u->mem, ptrty, "tmp", p->scopebb()); 1258 e->mem = new llvm::BitCastInst(u->mem, ptrty, "tmp", p->scopebb());
1258 e->arg = u->arg; 1259 if (fromtype->next->size() == totype->next->size())
1260 e->arg = u->arg;
1261 else
1262 e->arg = LLVM_DtoArrayCastLength(u->arg, ety, ptrty->getContainedType(0));
1259 } 1263 }
1260 else { 1264 else {
1261 llvm::Value* uval = u->getValue(); 1265 llvm::Value* uval = u->getValue();
1262 if (fromtype->ty == Tsarray) { 1266 if (fromtype->ty == Tsarray) {
1263 Logger::cout() << "uvalTy = " << *uval->getType() << '\n'; 1267 Logger::cout() << "uvalTy = " << *uval->getType() << '\n';
1264 assert(llvm::isa<llvm::PointerType>(uval->getType())); 1268 assert(llvm::isa<llvm::PointerType>(uval->getType()));
1265 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(uval->getType()->getContainedType(0)); 1269 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(uval->getType()->getContainedType(0));
1266 e->arg = llvm::ConstantInt::get(LLVM_DtoSize_t(), arrty->getNumElements(), false); 1270 e->arg = llvm::ConstantInt::get(LLVM_DtoSize_t(), arrty->getNumElements(), false);
1271 e->arg = LLVM_DtoArrayCastLength(e->arg, ety, ptrty->getContainedType(0));
1267 e->mem = new llvm::BitCastInst(uval, ptrty, "tmp", p->scopebb()); 1272 e->mem = new llvm::BitCastInst(uval, ptrty, "tmp", p->scopebb());
1268 } 1273 }
1269 else { 1274 else {
1270 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); 1275 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
1271 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); 1276 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false);
1272 e->arg = LLVM_DtoGEP(uval,zero,zero,"tmp",p->scopebb()); 1277 e->arg = LLVM_DtoGEP(uval,zero,zero,"tmp",p->scopebb());
1273 e->arg = new llvm::LoadInst(e->arg, "tmp", p->scopebb()); 1278 e->arg = new llvm::LoadInst(e->arg, "tmp", p->scopebb());
1279 e->arg = LLVM_DtoArrayCastLength(e->arg, ety, ptrty->getContainedType(0));
1274 1280
1275 e->mem = LLVM_DtoGEP(uval,zero,one,"tmp",p->scopebb()); 1281 e->mem = LLVM_DtoGEP(uval,zero,one,"tmp",p->scopebb());
1276 e->mem = new llvm::LoadInst(e->mem, "tmp", p->scopebb()); 1282 e->mem = new llvm::LoadInst(e->mem, "tmp", p->scopebb());
1277 //Logger::cout() << *e->mem->getType() << '|' << *ptrty << '\n'; 1283 //Logger::cout() << *e->mem->getType() << '|' << *ptrty << '\n';
1278 e->mem = new llvm::BitCastInst(e->mem, ptrty, "tmp", p->scopebb()); 1284 e->mem = new llvm::BitCastInst(e->mem, ptrty, "tmp", p->scopebb());
1316 LOG_SCOPE; 1322 LOG_SCOPE;
1317 elem* e = 0; 1323 elem* e = 0;
1318 if (VarDeclaration* vd = var->isVarDeclaration()) 1324 if (VarDeclaration* vd = var->isVarDeclaration())
1319 { 1325 {
1320 Logger::println("VarDeclaration"); 1326 Logger::println("VarDeclaration");
1327 if (!vd->llvmTouched && vd->isDataseg())
1328 vd->toObjFile();
1329
1330 if (vd->isTypedefDeclaration()) {
1331 e->istypeinfo = true;
1332 }
1333
1321 assert(vd->llvmValue); 1334 assert(vd->llvmValue);
1322 Type* t = LLVM_DtoDType(type); 1335 Type* t = LLVM_DtoDType(type);
1323 Type* vdtype = LLVM_DtoDType(vd->type); 1336 Type* vdtype = LLVM_DtoDType(vd->type);
1324 1337
1325 llvm::Value* llvalue = vd->nestedref ? LLVM_DtoNestedVariable(vd) : vd->llvmValue; 1338 llvm::Value* llvalue = vd->nestedref ? LLVM_DtoNestedVariable(vd) : vd->llvmValue;
1632 elem* SliceExp::toElem(IRState* p) 1645 elem* SliceExp::toElem(IRState* p)
1633 { 1646 {
1634 Logger::print("SliceExp::toElem: %s | %s\n", toChars(), type->toChars()); 1647 Logger::print("SliceExp::toElem: %s | %s\n", toChars(), type->toChars());
1635 LOG_SCOPE; 1648 LOG_SCOPE;
1636 1649
1650 Type* t = LLVM_DtoDType(type);
1651 assert(t->ty == Tarray);
1652
1637 elem* v = e1->toElem(p); 1653 elem* v = e1->toElem(p);
1638 Type* e1type = LLVM_DtoDType(e1->type); 1654 Type* e1type = LLVM_DtoDType(e1->type);
1639 1655
1640 elem* e = new elem; 1656 elem* e = new elem;
1641 assert(v->mem); 1657 assert(v->mem);
1720 } 1736 }
1721 } 1737 }
1722 1738
1723 delete lo; 1739 delete lo;
1724 delete up; 1740 delete up;
1741
1742 /*
1743 llvm::Value* tmpmem = new llvm::AllocaInst(LLVM_DtoType(t),"tmp",p->topallocapoint());
1744 llvm::Value* ptr = LLVM_DtoGEPi(tmpmem,0,0,"tmp");
1745 p->ir->CreateStore(e->arg, ptr);
1746 ptr = LLVM_DtoGEPi(tmpmem,0,1,"tmp");
1747 p->ir->CreateStore(e->mem, ptr);
1748 e->arg = NULL;
1749 e->mem = tmpmem;
1750 */
1725 } 1751 }
1726 // full slice 1752 // full slice
1727 else 1753 else
1728 { 1754 {
1729 e->mem = v->mem; 1755 e->mem = v->mem;
1895 { 1921 {
1896 e->val = LLVM_DtoStaticArrayCompare(op,l->mem,r->mem); 1922 e->val = LLVM_DtoStaticArrayCompare(op,l->mem,r->mem);
1897 } 1923 }
1898 else if (t->ty == Tarray) 1924 else if (t->ty == Tarray)
1899 { 1925 {
1900 assert(0 && "array comparison invokes the typeinfo runtime"); 1926 e->val = LLVM_DtoDynArrayCompare(op,l->mem,r->mem);
1901 } 1927 }
1902 else 1928 else
1903 { 1929 {
1904 assert(0 && "Unsupported EqualExp type"); 1930 assert(0 && "Unsupported EqualExp type");
1905 } 1931 }