Mercurial > projects > ldc
comparison gen/statements.cpp @ 136:0e28624814e8 trunk
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
author | lindquist |
---|---|
date | Thu, 17 Jan 2008 03:15:12 +0100 |
parents | 44a95ac7368a |
children | ce7b81fb957f |
comparison
equal
deleted
inserted
replaced
135:176bd52b3cf5 | 136:0e28624814e8 |
---|---|
23 #include "gen/runtime.h" | 23 #include "gen/runtime.h" |
24 #include "gen/arrays.h" | 24 #include "gen/arrays.h" |
25 #include "gen/todebug.h" | 25 #include "gen/todebug.h" |
26 #include "gen/dvalue.h" | 26 #include "gen/dvalue.h" |
27 | 27 |
28 #include "ir/irfunction.h" | |
29 | |
28 ////////////////////////////////////////////////////////////////////////////// | 30 ////////////////////////////////////////////////////////////////////////////// |
29 | 31 |
30 void CompoundStatement::toIR(IRState* p) | 32 void CompoundStatement::toIR(IRState* p) |
31 { | 33 { |
32 Logger::println("CompoundStatement::toIR(): %s", loc.toChars()); | 34 Logger::println("CompoundStatement::toIR(): %s", loc.toChars()); |
55 Type* exptype = DtoDType(exp->type); | 57 Type* exptype = DtoDType(exp->type); |
56 TY expty = exptype->ty; | 58 TY expty = exptype->ty; |
57 if (p->topfunc()->getReturnType() == llvm::Type::VoidTy) { | 59 if (p->topfunc()->getReturnType() == llvm::Type::VoidTy) { |
58 assert(DtoIsPassedByRef(exptype)); | 60 assert(DtoIsPassedByRef(exptype)); |
59 | 61 |
60 IRFunction* f = p->func(); | 62 IrFunction* f = p->func(); |
61 assert(f->type->llvmRetInPtr); | 63 assert(f->type->llvmRetInPtr); |
62 assert(f->decl->llvmRetArg); | 64 assert(f->decl->irFunc->retArg); |
63 | 65 |
64 if (global.params.symdebug) DtoDwarfStopPoint(loc.linnum); | 66 if (global.params.symdebug) DtoDwarfStopPoint(loc.linnum); |
65 | 67 |
66 DValue* rvar = new DVarValue(f->type->next, f->decl->llvmRetArg, true); | 68 DValue* rvar = new DVarValue(f->type->next, f->decl->irFunc->retArg, true); |
67 | 69 |
68 p->exps.push_back(IRExp(NULL,exp,rvar)); | 70 p->exps.push_back(IRExp(NULL,exp,rvar)); |
69 DValue* e = exp->toElem(p); | 71 DValue* e = exp->toElem(p); |
70 p->exps.pop_back(); | 72 p->exps.pop_back(); |
71 | 73 |
72 if (!e->inPlace()) | 74 if (!e->inPlace()) |
73 DtoAssign(rvar, e); | 75 DtoAssign(rvar, e); |
74 | 76 |
75 IRFunction::FinallyVec& fin = f->finallys; | 77 IrFunction::FinallyVec& fin = f->finallys; |
76 if (fin.empty()) { | 78 if (fin.empty()) { |
77 if (global.params.symdebug) DtoDwarfFuncEnd(f->decl); | 79 if (global.params.symdebug) DtoDwarfFuncEnd(f->decl); |
78 new llvm::ReturnInst(p->scopebb()); | 80 new llvm::ReturnInst(p->scopebb()); |
79 } | 81 } |
80 else { | 82 else { |
86 DValue* e = exp->toElem(p); | 88 DValue* e = exp->toElem(p); |
87 llvm::Value* v = e->getRVal(); | 89 llvm::Value* v = e->getRVal(); |
88 delete e; | 90 delete e; |
89 Logger::cout() << "return value is '" <<*v << "'\n"; | 91 Logger::cout() << "return value is '" <<*v << "'\n"; |
90 | 92 |
91 IRFunction::FinallyVec& fin = p->func()->finallys; | 93 IrFunction::FinallyVec& fin = p->func()->finallys; |
92 if (fin.empty()) { | 94 if (fin.empty()) { |
93 if (global.params.symdebug) DtoDwarfFuncEnd(p->func()->decl); | 95 if (global.params.symdebug) DtoDwarfFuncEnd(p->func()->decl); |
94 new llvm::ReturnInst(v, p->scopebb()); | 96 new llvm::ReturnInst(v, p->scopebb()); |
95 } | 97 } |
96 else { | 98 else { |
103 } | 105 } |
104 } | 106 } |
105 else | 107 else |
106 { | 108 { |
107 if (p->topfunc()->getReturnType() == llvm::Type::VoidTy) { | 109 if (p->topfunc()->getReturnType() == llvm::Type::VoidTy) { |
108 IRFunction::FinallyVec& fin = p->func()->finallys; | 110 IrFunction::FinallyVec& fin = p->func()->finallys; |
109 if (fin.empty()) { | 111 if (fin.empty()) { |
110 if (global.params.symdebug) DtoDwarfFuncEnd(p->func()->decl); | 112 if (global.params.symdebug) DtoDwarfFuncEnd(p->func()->decl); |
111 new llvm::ReturnInst(p->scopebb()); | 113 new llvm::ReturnInst(p->scopebb()); |
112 } | 114 } |
113 else { | 115 else { |
423 assert(!gIR->scopereturned()); | 425 assert(!gIR->scopereturned()); |
424 new llvm::BranchInst(trybb, p->scopebb()); | 426 new llvm::BranchInst(trybb, p->scopebb()); |
425 | 427 |
426 // do the try block | 428 // do the try block |
427 p->scope() = IRScope(trybb,finallybb); | 429 p->scope() = IRScope(trybb,finallybb); |
428 gIR->func()->finallys.push_back(IRFinally(finallybb,finallyretbb)); | 430 gIR->func()->finallys.push_back(IrFinally(finallybb,finallyretbb)); |
429 IRFinally& fin = p->func()->finallys.back(); | 431 IrFinally& fin = p->func()->finallys.back(); |
430 | 432 |
431 assert(body); | 433 assert(body); |
432 body->toIR(p); | 434 body->toIR(p); |
433 | 435 |
434 // terminate try BB | 436 // terminate try BB |
451 finalbody->toIR(p); // hope this will work, otherwise it's time it gets fixed | 453 finalbody->toIR(p); // hope this will work, otherwise it's time it gets fixed |
452 | 454 |
453 // terminate finally (return path) | 455 // terminate finally (return path) |
454 size_t nfin = p->func()->finallys.size(); | 456 size_t nfin = p->func()->finallys.size(); |
455 if (nfin > 1) { | 457 if (nfin > 1) { |
456 IRFinally& ofin = p->func()->finallys[nfin-2]; | 458 IrFinally& ofin = p->func()->finallys[nfin-2]; |
457 p->ir->CreateBr(ofin.retbb); | 459 p->ir->CreateBr(ofin.retbb); |
458 } | 460 } |
459 // no outer | 461 // no outer |
460 else | 462 else |
461 { | 463 { |
791 Logger::println("aggr = %s", aggr->toChars()); | 793 Logger::println("aggr = %s", aggr->toChars()); |
792 | 794 |
793 // key | 795 // key |
794 const llvm::Type* keytype = key ? DtoType(key->type) : DtoSize_t(); | 796 const llvm::Type* keytype = key ? DtoType(key->type) : DtoSize_t(); |
795 llvm::Value* keyvar = new llvm::AllocaInst(keytype, "foreachkey", p->topallocapoint()); | 797 llvm::Value* keyvar = new llvm::AllocaInst(keytype, "foreachkey", p->topallocapoint()); |
796 if (key) key->llvmValue = keyvar; | 798 if (key) |
799 { | |
800 //key->llvmValue = keyvar; | |
801 assert(!key->irLocal); | |
802 key->irLocal = new IrLocal(key); | |
803 key->irLocal->value = keyvar; | |
804 } | |
797 llvm::Value* zerokey = llvm::ConstantInt::get(keytype,0,false); | 805 llvm::Value* zerokey = llvm::ConstantInt::get(keytype,0,false); |
798 | 806 |
799 // value | 807 // value |
800 const llvm::Type* valtype = DtoType(value->type); | 808 const llvm::Type* valtype = DtoType(value->type); |
801 llvm::Value* valvar = NULL; | 809 llvm::Value* valvar = NULL; |
802 if (!value->isRef() && !value->isOut()) | 810 if (!value->isRef() && !value->isOut()) |
803 valvar = new llvm::AllocaInst(valtype, "foreachval", p->topallocapoint()); | 811 valvar = new llvm::AllocaInst(valtype, "foreachval", p->topallocapoint()); |
812 assert(!value->irLocal); | |
813 value->irLocal = new IrLocal(value); | |
804 | 814 |
805 // what to iterate | 815 // what to iterate |
806 DValue* aggrval = aggr->toElem(p); | 816 DValue* aggrval = aggr->toElem(p); |
807 Type* aggrtype = DtoDType(aggr->type); | 817 Type* aggrtype = DtoDType(aggr->type); |
808 | 818 |
894 | 904 |
895 // get value for this iteration | 905 // get value for this iteration |
896 llvm::Constant* zero = llvm::ConstantInt::get(keytype,0,false); | 906 llvm::Constant* zero = llvm::ConstantInt::get(keytype,0,false); |
897 llvm::Value* loadedKey = p->ir->CreateLoad(keyvar,"tmp"); | 907 llvm::Value* loadedKey = p->ir->CreateLoad(keyvar,"tmp"); |
898 if (aggrtype->ty == Tsarray) | 908 if (aggrtype->ty == Tsarray) |
899 value->llvmValue = DtoGEP(val,zero,loadedKey,"tmp"); | 909 value->irLocal->value = DtoGEP(val,zero,loadedKey,"tmp"); |
900 else if (aggrtype->ty == Tarray) | 910 else if (aggrtype->ty == Tarray) |
901 value->llvmValue = new llvm::GetElementPtrInst(val,loadedKey,"tmp",p->scopebb()); | 911 value->irLocal->value = new llvm::GetElementPtrInst(val,loadedKey,"tmp",p->scopebb()); |
902 | 912 |
903 if (!value->isRef() && !value->isOut()) { | 913 if (!value->isRef() && !value->isOut()) { |
904 DValue* dst = new DVarValue(value->type, valvar, true); | 914 DValue* dst = new DVarValue(value->type, valvar, true); |
905 DValue* src = new DVarValue(value->type, value->llvmValue, true); | 915 DValue* src = new DVarValue(value->type, value->irLocal->value, true); |
906 DtoAssign(dst, src); | 916 DtoAssign(dst, src); |
907 value->llvmValue = valvar; | 917 value->irLocal->value = valvar; |
908 } | 918 } |
909 | 919 |
910 // emit body | 920 // emit body |
911 p->loopbbs.push_back(IRScope(nextbb,endbb)); | 921 p->loopbbs.push_back(IRScope(nextbb,endbb)); |
912 body->toIR(p); | 922 body->toIR(p); |
980 | 990 |
981 assert(exp); | 991 assert(exp); |
982 assert(body); | 992 assert(body); |
983 | 993 |
984 DValue* e = exp->toElem(p); | 994 DValue* e = exp->toElem(p); |
985 wthis->llvmValue = e->getRVal(); | 995 wthis->irLocal->value = e->getRVal(); |
986 delete e; | 996 delete e; |
987 | 997 |
988 body->toIR(p); | 998 body->toIR(p); |
989 } | 999 } |
990 | 1000 |