Mercurial > projects > ldc
comparison gen/toir.cpp @ 132:1700239cab2e trunk
[svn r136] MAJOR UNSTABLE UPDATE!!!
Initial commit after moving to Tango instead of Phobos.
Lots of bugfixes...
This build is not suitable for most things.
author | lindquist |
---|---|
date | Fri, 11 Jan 2008 17:57:40 +0100 |
parents | 5825d48b27d1 |
children | 44a95ac7368a |
comparison
equal
deleted
inserted
replaced
131:5825d48b27d1 | 132:1700239cab2e |
---|---|
12 #include <fstream> | 12 #include <fstream> |
13 #include <iostream> | 13 #include <iostream> |
14 | 14 |
15 #include "gen/llvm.h" | 15 #include "gen/llvm.h" |
16 | 16 |
17 #include "attrib.h" | |
17 #include "total.h" | 18 #include "total.h" |
18 #include "init.h" | 19 #include "init.h" |
19 #include "mtype.h" | 20 #include "mtype.h" |
20 #include "hdrgen.h" | 21 #include "hdrgen.h" |
21 #include "port.h" | 22 #include "port.h" |
112 else if (TypedefDeclaration* tdef = declaration->isTypedefDeclaration()) | 113 else if (TypedefDeclaration* tdef = declaration->isTypedefDeclaration()) |
113 { | 114 { |
114 Logger::println("TypedefDeclaration"); | 115 Logger::println("TypedefDeclaration"); |
115 tdef->type->getTypeInfo(NULL); | 116 tdef->type->getTypeInfo(NULL); |
116 } | 117 } |
118 // attribute declaration | |
119 else if (AttribDeclaration* a = declaration->isAttribDeclaration()) | |
120 { | |
121 Logger::println("AttribDeclaration"); | |
122 for (int i=0; i < a->decl->dim; ++i) | |
123 { | |
124 DtoForceDeclareDsymbol((Dsymbol*)a->decl->data[i]); | |
125 } | |
126 } | |
117 // unsupported declaration | 127 // unsupported declaration |
118 else | 128 else |
119 { | 129 { |
120 error("Unimplemented DeclarationExp type"); | 130 error("Unimplemented DeclarationExp type. kind: %s", declaration->kind()); |
121 assert(0); | 131 assert(0); |
122 } | 132 } |
123 return 0; | 133 return 0; |
124 } | 134 } |
125 | 135 |
259 TypeStruct* ts = (TypeStruct*)sdecltype; | 269 TypeStruct* ts = (TypeStruct*)sdecltype; |
260 DtoForceConstInitDsymbol(ts->sym); | 270 DtoForceConstInitDsymbol(ts->sym); |
261 assert(ts->sym->llvmConstInit); | 271 assert(ts->sym->llvmConstInit); |
262 return ts->sym->llvmConstInit; | 272 return ts->sym->llvmConstInit; |
263 } | 273 } |
264 assert(0 && "Only supported const VarExp is of a SymbolDeclaration"); | 274 else if (TypeInfoDeclaration* ti = var->isTypeInfoDeclaration()) |
275 { | |
276 DtoForceDeclareDsymbol(ti); | |
277 assert(ti->llvmValue); | |
278 const llvm::Type* vartype = DtoType(type); | |
279 llvm::Constant* m = isaConstant(ti->llvmValue); | |
280 assert(m); | |
281 if (ti->llvmValue->getType() != llvm::PointerType::get(vartype)) | |
282 m = llvm::ConstantExpr::getBitCast(m, vartype); | |
283 return m; | |
284 } | |
285 assert(0 && "Unsupported const VarExp kind"); | |
265 return NULL; | 286 return NULL; |
266 } | 287 } |
267 | 288 |
268 ////////////////////////////////////////////////////////////////////////////////////////// | 289 ////////////////////////////////////////////////////////////////////////////////////////// |
269 | 290 |
834 // invalid | 855 // invalid |
835 else { | 856 else { |
836 assert(tf); | 857 assert(tf); |
837 } | 858 } |
838 | 859 |
839 // va args | 860 // magic stuff |
840 bool va_magic = false; | 861 bool va_magic = false; |
841 bool va_intrinsic = false; | 862 bool va_intrinsic = false; |
842 DFuncValue* dfv = fn->isFunc(); | 863 DFuncValue* dfv = fn->isFunc(); |
843 if (dfv && dfv->func) { | 864 if (dfv && dfv->func) { |
844 FuncDeclaration* fndecl = dfv->func; | 865 FuncDeclaration* fndecl = dfv->func; |
865 } | 886 } |
866 else if (fndecl->llvmInternal == LLVMalloca) { | 887 else if (fndecl->llvmInternal == LLVMalloca) { |
867 //Argument* fnarg = Argument::getNth(tf->parameters, 0); | 888 //Argument* fnarg = Argument::getNth(tf->parameters, 0); |
868 Expression* exp = (Expression*)arguments->data[0]; | 889 Expression* exp = (Expression*)arguments->data[0]; |
869 DValue* expv = exp->toElem(p); | 890 DValue* expv = exp->toElem(p); |
891 if (expv->getType()->toBasetype()->ty != Tint32) | |
892 expv = DtoCast(expv, Type::tint32); | |
870 llvm::Value* alloc = new llvm::AllocaInst(llvm::Type::Int8Ty, expv->getRVal(), "alloca", p->scopebb()); | 893 llvm::Value* alloc = new llvm::AllocaInst(llvm::Type::Int8Ty, expv->getRVal(), "alloca", p->scopebb()); |
871 return new DImValue(type, alloc); | 894 return new DImValue(type, alloc); |
872 } | 895 } |
873 } | 896 } |
874 | 897 |
1313 llvm::Value* arrptr; | 1336 llvm::Value* arrptr; |
1314 if (e1type->ty == Tpointer) { | 1337 if (e1type->ty == Tpointer) { |
1315 assert(e1type->next->ty == Tstruct); | 1338 assert(e1type->next->ty == Tstruct); |
1316 TypeStruct* ts = (TypeStruct*)e1type->next; | 1339 TypeStruct* ts = (TypeStruct*)e1type->next; |
1317 Logger::println("Struct member offset:%d", vd->offset); | 1340 Logger::println("Struct member offset:%d", vd->offset); |
1341 | |
1318 llvm::Value* src = l->getRVal(); | 1342 llvm::Value* src = l->getRVal(); |
1343 | |
1319 std::vector<unsigned> vdoffsets; | 1344 std::vector<unsigned> vdoffsets; |
1320 arrptr = DtoIndexStruct(src, ts->sym, vd->type, vd->offset, vdoffsets); | 1345 arrptr = DtoIndexStruct(src, ts->sym, vd->type, vd->offset, vdoffsets); |
1321 } | 1346 } |
1322 else if (e1type->ty == Tclass) { | 1347 else if (e1type->ty == Tclass) { |
1323 TypeClass* tc = (TypeClass*)e1type; | 1348 TypeClass* tc = (TypeClass*)e1type; |
1324 Logger::println("Class member offset: %d", vd->offset); | 1349 Logger::println("Class member offset: %d", vd->offset); |
1325 std::vector<unsigned> vdoffsets(1,0); | 1350 |
1351 llvm::Value* src = l->getRVal(); | |
1352 | |
1353 std::vector<unsigned> vdoffsets; | |
1354 arrptr = DtoIndexClass(src, tc->sym, vd->type, vd->offset, vdoffsets); | |
1355 | |
1356 /*std::vector<unsigned> vdoffsets(1,0); | |
1326 tc->sym->offsetToIndex(vd->type, vd->offset, vdoffsets); | 1357 tc->sym->offsetToIndex(vd->type, vd->offset, vdoffsets); |
1358 | |
1327 llvm::Value* src = l->getRVal(); | 1359 llvm::Value* src = l->getRVal(); |
1328 //Logger::cout() << "src: " << *src << '\n'; | 1360 |
1361 Logger::println("indices:"); | |
1362 for (size_t i=0; i<vdoffsets.size(); ++i) | |
1363 Logger::println("%d", vdoffsets[i]); | |
1364 | |
1365 Logger::cout() << "src: " << *src << '\n'; | |
1329 arrptr = DtoGEP(src,vdoffsets,"tmp",p->scopebb()); | 1366 arrptr = DtoGEP(src,vdoffsets,"tmp",p->scopebb()); |
1367 Logger::cout() << "dst: " << *arrptr << '\n';*/ | |
1330 } | 1368 } |
1331 else | 1369 else |
1332 assert(0); | 1370 assert(0); |
1333 | 1371 |
1334 //Logger::cout() << "mem: " << *arrptr << '\n'; | 1372 //Logger::cout() << "mem: " << *arrptr << '\n'; |
1607 default: | 1645 default: |
1608 assert(0); | 1646 assert(0); |
1609 } | 1647 } |
1610 if (!skip) | 1648 if (!skip) |
1611 { | 1649 { |
1612 eval = new llvm::ICmpInst(cmpop, l->getRVal(), r->getRVal(), "tmp", p->scopebb()); | 1650 llvm::Value* a = l->getRVal(); |
1651 llvm::Value* b = r->getRVal(); | |
1652 Logger::cout() << "type 1: " << *a << '\n'; | |
1653 Logger::cout() << "type 2: " << *b << '\n'; | |
1654 eval = new llvm::ICmpInst(cmpop, a, b, "tmp", p->scopebb()); | |
1613 } | 1655 } |
1614 } | 1656 } |
1615 else if (t->isfloating()) | 1657 else if (t->isfloating()) |
1616 { | 1658 { |
1617 llvm::FCmpInst::Predicate cmpop; | 1659 llvm::FCmpInst::Predicate cmpop; |
1808 const llvm::Type* t = DtoType(ntype); | 1850 const llvm::Type* t = DtoType(ntype); |
1809 | 1851 |
1810 llvm::Value* emem = 0; | 1852 llvm::Value* emem = 0; |
1811 bool inplace = false; | 1853 bool inplace = false; |
1812 | 1854 |
1813 if (onstack) { | 1855 { |
1814 assert(ntype->ty == Tclass); | 1856 Logger::println("Allocating memory"); |
1815 emem = new llvm::AllocaInst(t->getContainedType(0),"tmp",p->topallocapoint()); | 1857 LOG_SCOPE; |
1816 } | 1858 if (onstack) { |
1817 else if (ntype->ty == Tclass) { | 1859 assert(ntype->ty == Tclass); |
1818 emem = new llvm::MallocInst(t->getContainedType(0),"tmp",p->scopebb()); | 1860 emem = new llvm::AllocaInst(t->getContainedType(0),"tmp",p->topallocapoint()); |
1819 } | 1861 } |
1820 else if (ntype->ty == Tarray) { | 1862 else if (ntype->ty == Tclass) { |
1821 assert(arguments); | 1863 emem = new llvm::MallocInst(t->getContainedType(0),"tmp",p->scopebb()); |
1822 if (arguments->dim == 1) { | 1864 } |
1823 DValue* sz = ((Expression*)arguments->data[0])->toElem(p); | 1865 else if (ntype->ty == Tarray) { |
1824 llvm::Value* dimval = sz->getRVal(); | 1866 assert(arguments); |
1825 Type* nnt = DtoDType(ntype->next); | 1867 if (arguments->dim == 1) { |
1826 if (nnt->ty == Tvoid) | 1868 DValue* sz = ((Expression*)arguments->data[0])->toElem(p); |
1827 nnt = Type::tint8; | 1869 llvm::Value* dimval = sz->getRVal(); |
1828 if (!p->topexp() || p->topexp()->e2 != this) { | 1870 Type* nnt = DtoDType(ntype->next); |
1829 const llvm::Type* restype = DtoType(type); | 1871 if (nnt->ty == Tvoid) |
1830 Logger::cout() << "restype = " << *restype << '\n'; | 1872 nnt = Type::tint8; |
1831 emem = new llvm::AllocaInst(restype,"newstorage",p->topallocapoint()); | 1873 |
1832 DtoNewDynArray(emem, dimval, nnt); | 1874 if (p->topexp() && p->topexp()->e2 == this) { |
1833 return new DVarValue(newtype, emem, true); | 1875 assert(p->topexp()->v); |
1834 } | 1876 emem = p->topexp()->v->getLVal(); |
1835 else if (p->topexp() && p->topexp()->e2 == this) { | 1877 DtoNewDynArray(emem, dimval, nnt); |
1836 assert(p->topexp()->v); | 1878 inplace = true; |
1837 emem = p->topexp()->v->getLVal(); | 1879 } |
1838 DtoNewDynArray(emem, dimval, nnt); | 1880 else { |
1839 inplace = true; | 1881 const llvm::Type* restype = DtoType(type); |
1840 } | 1882 Logger::cout() << "restype = " << *restype << '\n'; |
1841 else | 1883 emem = new llvm::AllocaInst(restype,"newstorage",p->topallocapoint()); |
1842 assert(0); | 1884 DtoNewDynArray(emem, dimval, nnt); |
1885 return new DVarValue(newtype, emem, true); | |
1886 } | |
1887 } | |
1888 else { | |
1889 assert(0 && "num args to 'new' != 1"); | |
1890 } | |
1843 } | 1891 } |
1844 else { | 1892 else { |
1845 assert(0); | 1893 emem = new llvm::MallocInst(t,"tmp",p->scopebb()); |
1846 } | 1894 } |
1847 } | |
1848 else { | |
1849 emem = new llvm::MallocInst(t,"tmp",p->scopebb()); | |
1850 } | 1895 } |
1851 | 1896 |
1852 if (ntype->ty == Tclass) { | 1897 if (ntype->ty == Tclass) { |
1853 // first apply the static initializer | 1898 // first apply the static initializer |
1854 TypeClass* tc = (TypeClass*)ntype; | 1899 TypeClass* tc = (TypeClass*)ntype; |
1855 DtoInitClass(tc, emem); | 1900 DtoInitClass(tc, emem); |
1856 | 1901 |
1857 // set the this var for nested classes | 1902 // set the this var for nested classes |
1858 if (thisexp) { | 1903 if (thisexp) { |
1904 Logger::println("Resolving 'this' expression"); | |
1905 LOG_SCOPE; | |
1859 DValue* thisval = thisexp->toElem(p); | 1906 DValue* thisval = thisexp->toElem(p); |
1860 size_t idx = 2; | 1907 size_t idx = 2; |
1861 idx += tc->sym->llvmIRStruct->interfaces.size(); | 1908 idx += tc->sym->llvmIRStruct->interfaces.size(); |
1862 DtoStore(thisval->getRVal(), DtoGEPi(emem,0,idx,"tmp")); | 1909 llvm::Value* dst = thisval->getRVal(); |
1910 llvm::Value* src = DtoGEPi(emem,0,idx,"tmp"); | |
1911 Logger::cout() << "dst: " << *dst << "\nsrc: " << *src << '\n'; | |
1912 DtoStore(dst, src); | |
1863 } | 1913 } |
1864 else if (tc->sym->isNested()) | 1914 else if (tc->sym->isNested()) |
1865 { | 1915 { |
1916 Logger::println("Resolving nested context"); | |
1917 LOG_SCOPE; | |
1866 size_t idx = 2; | 1918 size_t idx = 2; |
1867 idx += tc->sym->llvmIRStruct->interfaces.size(); | 1919 idx += tc->sym->llvmIRStruct->interfaces.size(); |
1868 llvm::Value* nest = p->func()->decl->llvmNested; | 1920 llvm::Value* nest = p->func()->decl->llvmNested; |
1869 if (!nest) | 1921 if (!nest) |
1870 nest = p->func()->decl->llvmThisVar; | 1922 nest = p->func()->decl->llvmThisVar; |
1874 DtoStore(nest, gep); | 1926 DtoStore(nest, gep); |
1875 } | 1927 } |
1876 | 1928 |
1877 // then call constructor | 1929 // then call constructor |
1878 if (arguments) { | 1930 if (arguments) { |
1931 Logger::println("Calling constructor"); | |
1932 LOG_SCOPE; | |
1879 assert(member); | 1933 assert(member); |
1880 assert(member->llvmValue); | 1934 assert(member->llvmValue); |
1881 llvm::Function* fn = llvm::cast<llvm::Function>(member->llvmValue); | 1935 llvm::Function* fn = llvm::cast<llvm::Function>(member->llvmValue); |
1882 TypeFunction* tf = (TypeFunction*)DtoDType(member->type); | 1936 TypeFunction* tf = (TypeFunction*)DtoDType(member->type); |
1883 | 1937 |
2334 Logger::print("CatExp::toElem: %s | %s\n", toChars(), type->toChars()); | 2388 Logger::print("CatExp::toElem: %s | %s\n", toChars(), type->toChars()); |
2335 LOG_SCOPE; | 2389 LOG_SCOPE; |
2336 | 2390 |
2337 Type* t = DtoDType(type); | 2391 Type* t = DtoDType(type); |
2338 | 2392 |
2393 bool arrNarr = DtoDType(e1->type) == DtoDType(e2->type); | |
2394 | |
2339 IRExp* ex = p->topexp(); | 2395 IRExp* ex = p->topexp(); |
2340 if (ex && ex->e2 == this) { | 2396 if (ex && ex->e2 == this) { |
2341 assert(ex->v); | 2397 assert(ex->v); |
2342 DtoCatArrays(ex->v->getLVal(),e1,e2); | 2398 if (arrNarr) |
2399 DtoCatArrays(ex->v->getLVal(),e1,e2); | |
2400 else | |
2401 DtoCatArrayElement(ex->v->getLVal(),e1,e2); | |
2343 return new DImValue(type, ex->v->getLVal(), true); | 2402 return new DImValue(type, ex->v->getLVal(), true); |
2344 } | 2403 } |
2345 else { | 2404 else { |
2346 assert(t->ty == Tarray); | 2405 assert(t->ty == Tarray); |
2347 const llvm::Type* arrty = DtoType(t); | 2406 const llvm::Type* arrty = DtoType(t); |
2348 llvm::Value* dst = new llvm::AllocaInst(arrty, "tmpmem", p->topallocapoint()); | 2407 llvm::Value* dst = new llvm::AllocaInst(arrty, "tmpmem", p->topallocapoint()); |
2349 DtoCatArrays(dst,e1,e2); | 2408 if (arrNarr) |
2409 DtoCatArrays(dst,e1,e2); | |
2410 else | |
2411 DtoCatArrayElement(dst,e1,e2); | |
2350 return new DVarValue(type, dst, true); | 2412 return new DVarValue(type, dst, true); |
2351 } | 2413 } |
2352 } | 2414 } |
2353 | 2415 |
2354 ////////////////////////////////////////////////////////////////////////////////////////// | 2416 ////////////////////////////////////////////////////////////////////////////////////////// |