comparison gen/tollvm.c @ 9:dafae18f9c08 trunk

[svn r13] * Updated for LLVM 2.1 * Class v-tables are now typesafe * Code cleanups
author lindquist
date Mon, 01 Oct 2007 21:19:53 +0200
parents 5e69b77a5c51
children d3ee9efe20e2
comparison
equal deleted inserted replaced
8:5e69b77a5c51 9:dafae18f9c08
206 llvm::FunctionType* LLVM_DtoFunctionType(FuncDeclaration* fdecl) 206 llvm::FunctionType* LLVM_DtoFunctionType(FuncDeclaration* fdecl)
207 { 207 {
208 TypeFunction* f = (TypeFunction*)fdecl->type; 208 TypeFunction* f = (TypeFunction*)fdecl->type;
209 assert(f != 0); 209 assert(f != 0);
210 210
211 // has already been pulled in by a reference to ( 211 // type has already been resolved
212 if (f->llvmType != 0) { 212 if (f->llvmType != 0) {
213 return llvm::cast<llvm::FunctionType>(f->llvmType); 213 return llvm::cast<llvm::FunctionType>(f->llvmType);
214 } 214 }
215 215
216 // return value type 216 // return value type
245 if (retinptr) { 245 if (retinptr) {
246 Logger::print("returning through pointer parameter\n"); 246 Logger::print("returning through pointer parameter\n");
247 paramvec.push_back(rettype); 247 paramvec.push_back(rettype);
248 } 248 }
249 249
250 if (fdecl->needThis() && fdecl->vthis) { 250 if (fdecl->needThis()) {
251 Logger::print("this is: %s\n", fdecl->vthis->type->toChars()); 251 if (AggregateDeclaration* ad = fdecl->isMember()) {
252 paramvec.push_back(LLVM_DtoType(fdecl->vthis->type)); 252 Logger::print("isMember = this is: %s\n", ad->type->toChars());
253 usesthis = true; 253 const llvm::Type* thisty = LLVM_DtoType(ad->type);
254 if (llvm::isa<llvm::StructType>(thisty))
255 thisty = llvm::PointerType::get(thisty);
256 paramvec.push_back(thisty);
257 usesthis = true;
258 }
259 else
260 assert(0);
254 } 261 }
255 262
256 size_t n = Argument::dim(f->parameters); 263 size_t n = Argument::dim(f->parameters);
257 for (int i=0; i < n; ++i) { 264 for (int i=0; i < n; ++i) {
258 Argument* arg = Argument::getNth(f->parameters, i); 265 Argument* arg = Argument::getNth(f->parameters, i);
293 // construct function type 300 // construct function type
294 bool isvararg = f->varargs; 301 bool isvararg = f->varargs;
295 llvm::FunctionType* functype = llvm::FunctionType::get(actualRettype, paramvec, isvararg); 302 llvm::FunctionType* functype = llvm::FunctionType::get(actualRettype, paramvec, isvararg);
296 303
297 f->llvmType = functype; 304 f->llvmType = functype;
305 f->llvmRetInPtr = retinptr;
306 f->llvmUsesThis = usesthis;
298 return functype; 307 return functype;
299 } 308 }
300 309
301 ////////////////////////////////////////////////////////////////////////////////////////// 310 //////////////////////////////////////////////////////////////////////////////////////////
302 311
835 dst[i] = llvm::ConstantInt::get(llvm::Type::Int32Ty, src[i], false); 844 dst[i] = llvm::ConstantInt::get(llvm::Type::Int32Ty, src[i], false);
836 } 845 }
837 Logger::cout() << '\n'; 846 Logger::cout() << '\n';
838 return new llvm::GetElementPtrInst(ptr, dst.begin(), dst.end(), var, bb); 847 return new llvm::GetElementPtrInst(ptr, dst.begin(), dst.end(), var, bb);
839 } 848 }
849
850 llvm::Function* LLVM_DtoDeclareFunction(FuncDeclaration* fdecl)
851 {
852 if (fdecl->llvmValue != 0) {
853 return llvm::cast<llvm::Function>(fdecl->llvmValue);
854 }
855
856 static int fdi = 0;
857 Logger::print("FuncDeclaration::toObjFile(%d,%s): %s\n", fdi++, fdecl->needThis()?"this":"static",fdecl->toChars());
858 LOG_SCOPE;
859
860 if (fdecl->llvmInternal == LLVMintrinsic && fdecl->fbody) {
861 error("intrinsics cannot have function bodies");
862 fatal();
863 }
864
865 // construct function
866 TypeFunction* f = (TypeFunction*)fdecl->type;
867 assert(f != 0);
868 llvm::FunctionType* functype = (f->llvmType == 0) ? LLVM_DtoFunctionType(fdecl) : llvm::cast<llvm::FunctionType>(f->llvmType);
869
870 // mangled name
871 char* mangled_name = (fdecl->llvmInternal == LLVMintrinsic) ? fdecl->llvmInternal1 : fdecl->mangle();
872
873 // make the function
874 llvm::Function* func = gIR->module->getFunction(mangled_name);
875 if (func == 0) {
876 func = new llvm::Function(functype,LLVM_DtoLinkage(fdecl->protection, fdecl->storage_class),mangled_name,gIR->module);
877 }
878
879 if (fdecl->llvmInternal != LLVMintrinsic)
880 func->setCallingConv(LLVM_DtoCallingConv(f->linkage));
881
882 fdecl->llvmValue = func;
883 f->llvmType = functype;
884
885 if (fdecl->isMain()) {
886 gIR->mainFunc = func;
887 }
888
889 // name parameters
890 llvm::Function::arg_iterator iarg = func->arg_begin();
891 int k = 0;
892 if (f->llvmRetInPtr) {
893 iarg->setName("retval");
894 f->llvmRetArg = iarg;
895 ++iarg;
896 }
897 if (f->llvmUsesThis) {
898 iarg->setName("this");
899 ++iarg;
900 }
901 for (; iarg != func->arg_end(); ++iarg)
902 {
903 Argument* arg = Argument::getNth(f->parameters, k++);
904 assert(arg != 0);
905 //arg->llvmValue = iarg;
906 //printf("identifier: '%s' %p\n", arg->ident->toChars(), arg->ident);
907 if (arg->ident != 0) {
908 if (arg->vardecl) {
909 arg->vardecl->llvmValue = iarg;
910 }
911 iarg->setName(arg->ident->toChars());
912 }
913 else {
914 iarg->setName("unnamed");
915 }
916 }
917
918 return func;
919 }