comparison gen/toir.c @ 4:e116aa1488e6 trunk

[svn r8] changed backend includes to always use the gen/<foo>.h prefix fixed passing string literals as array parameters few other fixes moved some array routines into gen/arrays
author lindquist
date Mon, 03 Sep 2007 17:34:30 +0200
parents c53b6e3fe49a
children 3d60e549b0c2
comparison
equal deleted inserted replaced
3:069cf4b0ec67 4:e116aa1488e6
22 #include "total.h" 22 #include "total.h"
23 #include "init.h" 23 #include "init.h"
24 #include "symbol.h" 24 #include "symbol.h"
25 #include "mtype.h" 25 #include "mtype.h"
26 #include "hdrgen.h" 26 #include "hdrgen.h"
27 #include "irstate.h"
28 #include "elem.h"
29 #include "port.h" 27 #include "port.h"
30 #include "logger.h" 28
31 29 #include "gen/irstate.h"
32 #include "tollvm.h" 30 #include "gen/elem.h"
33 #include "runtime.h" 31 #include "gen/logger.h"
32 #include "gen/tollvm.h"
33 #include "gen/runtime.h"
34 #include "gen/arrays.h"
34 35
35 ////////////////////////////////////////////////////////////////////////////////////////// 36 //////////////////////////////////////////////////////////////////////////////////////////
36 37
37 elem* DeclarationExp::toElem(IRState* p) 38 elem* DeclarationExp::toElem(IRState* p)
38 { 39 {
253 llvm::Value* arrptr = new llvm::GetElementPtrInst(gvar,zero,zero,"tmp",p->scopebb()); 254 llvm::Value* arrptr = new llvm::GetElementPtrInst(gvar,zero,zero,"tmp",p->scopebb());
254 255
255 elem* e = new elem; 256 elem* e = new elem;
256 257
257 if (type->ty == Tarray) { 258 if (type->ty == Tarray) {
258 llvm::Value* arr = p->toplval(); 259 llvm::Constant* clen = llvm::ConstantInt::get(LLVM_DtoSize_t(),len,false);
259 LLVM_DtoSetArray(arr, llvm::ConstantInt::get(LLVM_DtoSize_t(),len,false), arrptr); 260 if (p->lvals.empty()) {
261 e->type = elem::SLICE;
262 e->arg = clen;
263 e->mem = arrptr;
264 return e;
265 }
266 else {
267 llvm::Value* arr = p->toplval();
268 LLVM_DtoSetArray(arr, clen, arrptr);
269 }
260 } 270 }
261 else if (type->ty == Tpointer) { 271 else if (type->ty == Tpointer) {
262 e->mem = arrptr; 272 e->mem = arrptr;
263 } 273 }
264 else { 274 else {
877 } 887 }
878 else { 888 else {
879 llvm::Value* allocaInst = 0; 889 llvm::Value* allocaInst = 0;
880 llvm::BasicBlock* entryblock = &p->topfunc()->front(); 890 llvm::BasicBlock* entryblock = &p->topfunc()->front();
881 const llvm::PointerType* pty = llvm::cast<llvm::PointerType>(arg->mem->getType()); 891 const llvm::PointerType* pty = llvm::cast<llvm::PointerType>(arg->mem->getType());
882 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", p->topallocapoint());
883 if (argty == Tstruct) { 892 if (argty == Tstruct) {
893 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", p->topallocapoint());
884 TypeStruct* ts = (TypeStruct*)argexp->type; 894 TypeStruct* ts = (TypeStruct*)argexp->type;
885 LLVM_DtoStructCopy(ts,allocaInst,arg->mem); 895 LLVM_DtoStructCopy(ts,allocaInst,arg->mem);
886 } 896 }
887 else if (argty == Tdelegate) { 897 else if (argty == Tdelegate) {
898 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", p->topallocapoint());
888 LLVM_DtoDelegateCopy(allocaInst,arg->mem); 899 LLVM_DtoDelegateCopy(allocaInst,arg->mem);
889 } 900 }
890 else if (argty == Tarray) { 901 else if (argty == Tarray) {
891 LLVM_DtoArrayAssign(allocaInst,arg->mem); 902 if (arg->type == elem::SLICE) {
903 allocaInst = new llvm::AllocaInst(LLVM_DtoType(argexp->type), "tmpparam", p->topallocapoint());
904 LLVM_DtoSetArray(allocaInst, arg->arg, arg->mem);
905 }
906 else {
907 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", p->topallocapoint());
908 LLVM_DtoArrayAssign(allocaInst,arg->mem);
909 }
892 } 910 }
893 else 911 else
894 assert(0); 912 assert(0);
895 913
896 llargs[j] = allocaInst; 914 llargs[j] = allocaInst;
1037 e->mem = new llvm::BitCastInst(u->mem, ptrty, "tmp", p->scopebb()); 1055 e->mem = new llvm::BitCastInst(u->mem, ptrty, "tmp", p->scopebb());
1038 e->arg = u->arg; 1056 e->arg = u->arg;
1039 } 1057 }
1040 else { 1058 else {
1041 llvm::Value* uval = u->getValue(); 1059 llvm::Value* uval = u->getValue();
1042 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); 1060 if (from->ty == Tsarray) {
1043 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); 1061 Logger::cout() << "uvalTy = " << *uval->getType() << '\n';
1044 e->arg = new llvm::GetElementPtrInst(uval,zero,zero,"tmp",p->scopebb()); 1062 assert(llvm::isa<llvm::PointerType>(uval->getType()));
1045 e->arg = new llvm::LoadInst(e->arg, "tmp", p->scopebb()); 1063 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(uval->getType()->getContainedType(0));
1046 1064 e->arg = llvm::ConstantInt::get(LLVM_DtoSize_t(), arrty->getNumElements(), false);
1047 e->mem = new llvm::GetElementPtrInst(uval,zero,one,"tmp",p->scopebb()); 1065 e->mem = new llvm::BitCastInst(uval, ptrty, "tmp", p->scopebb());
1048 e->mem = new llvm::LoadInst(e->mem, "tmp", p->scopebb()); 1066 }
1049 e->mem = new llvm::BitCastInst(e->mem, ptrty, "tmp", p->scopebb()); 1067 else {
1068 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
1069 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false);
1070 e->arg = new llvm::GetElementPtrInst(uval,zero,zero,"tmp",p->scopebb());
1071 e->arg = new llvm::LoadInst(e->arg, "tmp", p->scopebb());
1072
1073 e->mem = new llvm::GetElementPtrInst(uval,zero,one,"tmp",p->scopebb());
1074 e->mem = new llvm::LoadInst(e->mem, "tmp", p->scopebb());
1075 //Logger::cout() << *e->mem->getType() << '|' << *ptrty << '\n';
1076 e->mem = new llvm::BitCastInst(e->mem, ptrty, "tmp", p->scopebb());
1077 }
1050 } 1078 }
1051 e->type = elem::SLICE; 1079 e->type = elem::SLICE;
1052 } 1080 }
1053 else if (to->ty == Tsarray) { 1081 else if (to->ty == Tsarray) {
1054 Logger::cout() << "to sarray" << '\n'; 1082 Logger::cout() << "to sarray" << '\n';
1109 //llvm::Value* idx1 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); 1137 //llvm::Value* idx1 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false);
1110 e->mem = new llvm::GetElementPtrInst(vd->llvmValue,idx0,idx0,"tmp",p->scopebb()); 1138 e->mem = new llvm::GetElementPtrInst(vd->llvmValue,idx0,idx0,"tmp",p->scopebb());
1111 e->type = elem::VAL; 1139 e->type = elem::VAL;
1112 } 1140 }
1113 else if (offset == 0) { 1141 else if (offset == 0) {
1114 vd->toObjFile(); 1142 /*if (!vd->llvmValue)
1143 vd->toObjFile();*/
1144 assert(vd->llvmValue);
1115 e = new elem; 1145 e = new elem;
1116 e->mem = vd->llvmValue; 1146 e->mem = vd->llvmValue;
1117 //e->vardecl = vd; 1147 //e->vardecl = vd;
1118 e->type = elem::VAL; 1148 e->type = elem::VAL;
1119 } 1149 }