Mercurial > projects > ldc
annotate gen/functions.cpp @ 131:5825d48b27d1 trunk
[svn r135] * Merged DMD 1.025 *
* Fixed a minor linking order mishap *
* Added an command line option -annotate *
* Fixed some problems with running optimizations *
* Added std.stdio and dependencies to lphobos (still not 100% working, but compiles and links) *
* Fixed problems with passing aggregate types to variadic functions *
* Added initial code towards full GC support, currently based on malloc and friends, not all the runtime calls the GC yet for memory *
* Fixed problems with resolving nested function context pointers for some heavily nested cases *
* Redid function argument passing + other minor code cleanups, still lots to do on this end... *
author | lindquist |
---|---|
date | Fri, 04 Jan 2008 01:38:42 +0100 |
parents | 8096ba7082db |
children | 1700239cab2e |
rev | line source |
---|---|
100 | 1 #include "gen/llvm.h" |
2 | |
3 #include "mtype.h" | |
4 #include "aggregate.h" | |
5 #include "init.h" | |
6 #include "declaration.h" | |
7 #include "template.h" | |
8 #include "module.h" | |
9 #include "statement.h" | |
10 | |
11 #include "gen/irstate.h" | |
12 #include "gen/tollvm.h" | |
13 #include "gen/runtime.h" | |
14 #include "gen/arrays.h" | |
15 #include "gen/logger.h" | |
16 #include "gen/functions.h" | |
17 #include "gen/todebug.h" | |
18 #include "gen/classes.h" | |
131 | 19 #include "gen/dvalue.h" |
100 | 20 |
21 const llvm::FunctionType* DtoFunctionType(Type* type, const llvm::Type* thistype, bool ismain) | |
22 { | |
23 TypeFunction* f = (TypeFunction*)type; | |
24 assert(f != 0); | |
25 | |
26 if (type->llvmType != NULL) { | |
27 return llvm::cast<llvm::FunctionType>(type->llvmType->get()); | |
28 } | |
29 | |
30 bool typesafeVararg = false; | |
31 if (f->linkage == LINKd && f->varargs == 1) { | |
32 typesafeVararg = true; | |
33 } | |
34 | |
35 // return value type | |
36 const llvm::Type* rettype; | |
37 const llvm::Type* actualRettype; | |
38 Type* rt = f->next; | |
39 bool retinptr = false; | |
40 bool usesthis = false; | |
41 | |
42 if (ismain) { | |
43 rettype = llvm::Type::Int32Ty; | |
44 actualRettype = rettype; | |
45 } | |
46 else { | |
47 assert(rt); | |
109
5ab8e92611f9
[svn r113] Added initial support for associative arrays (AAs).
lindquist
parents:
108
diff
changeset
|
48 Type* rtfin = DtoDType(rt); |
100 | 49 if (DtoIsPassedByRef(rt)) { |
50 rettype = llvm::PointerType::get(DtoType(rt)); | |
51 actualRettype = llvm::Type::VoidTy; | |
52 f->llvmRetInPtr = retinptr = true; | |
53 } | |
54 else { | |
55 rettype = DtoType(rt); | |
56 actualRettype = rettype; | |
57 } | |
58 } | |
59 | |
60 // parameter types | |
61 std::vector<const llvm::Type*> paramvec; | |
62 | |
63 if (retinptr) { | |
123
7f9a0a58394b
[svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents:
121
diff
changeset
|
64 //Logger::cout() << "returning through pointer parameter: " << *rettype << '\n'; |
100 | 65 paramvec.push_back(rettype); |
66 } | |
67 | |
68 if (thistype) { | |
69 paramvec.push_back(thistype); | |
70 usesthis = true; | |
71 } | |
72 | |
73 if (typesafeVararg) { | |
74 ClassDeclaration* ti = Type::typeinfo; | |
75 ti->toObjFile(); | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
76 DtoForceConstInitDsymbol(ti); |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
77 assert(ti->llvmConstInit); |
100 | 78 std::vector<const llvm::Type*> types; |
79 types.push_back(DtoSize_t()); | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
80 types.push_back(llvm::PointerType::get(llvm::PointerType::get(ti->llvmConstInit->getType()))); |
100 | 81 const llvm::Type* t1 = llvm::StructType::get(types); |
82 paramvec.push_back(llvm::PointerType::get(t1)); | |
83 paramvec.push_back(llvm::PointerType::get(llvm::Type::Int8Ty)); | |
84 } | |
85 | |
86 size_t n = Argument::dim(f->parameters); | |
87 | |
88 for (int i=0; i < n; ++i) { | |
89 Argument* arg = Argument::getNth(f->parameters, i); | |
90 // ensure scalar | |
91 Type* argT = DtoDType(arg->type); | |
92 assert(argT); | |
93 | |
94 const llvm::Type* at = DtoType(argT); | |
95 if (isaStruct(at)) { | |
96 Logger::println("struct param"); | |
97 paramvec.push_back(llvm::PointerType::get(at)); | |
98 } | |
99 else if (isaArray(at)) { | |
100 Logger::println("sarray param"); | |
101 assert(argT->ty == Tsarray); | |
102 //paramvec.push_back(llvm::PointerType::get(at->getContainedType(0))); | |
103 paramvec.push_back(llvm::PointerType::get(at)); | |
104 } | |
105 else if (llvm::isa<llvm::OpaqueType>(at)) { | |
106 Logger::println("opaque param"); | |
107 assert(argT->ty == Tstruct || argT->ty == Tclass); | |
108 paramvec.push_back(llvm::PointerType::get(at)); | |
109 } | |
110 else { | |
131 | 111 if ((arg->storageClass & STCref) || (arg->storageClass & STCout)) { |
112 Logger::println("by ref param"); | |
100 | 113 at = llvm::PointerType::get(at); |
114 } | |
115 else { | |
116 Logger::println("in param"); | |
117 } | |
118 paramvec.push_back(at); | |
119 } | |
120 } | |
121 | |
122 // construct function type | |
123 bool isvararg = !typesafeVararg && f->varargs; | |
124 llvm::FunctionType* functype = llvm::FunctionType::get(actualRettype, paramvec, isvararg); | |
125 | |
126 f->llvmRetInPtr = retinptr; | |
127 f->llvmUsesThis = usesthis; | |
128 | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
129 //if (!f->llvmType) |
100 | 130 f->llvmType = new llvm::PATypeHolder(functype); |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
131 //else |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
132 //assert(functype == f->llvmType->get()); |
100 | 133 |
134 return functype; | |
135 } | |
136 | |
137 ////////////////////////////////////////////////////////////////////////////////////////// | |
138 | |
139 static const llvm::FunctionType* DtoVaFunctionType(FuncDeclaration* fdecl) | |
140 { | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
141 // type has already been resolved |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
142 if (fdecl->type->llvmType != 0) { |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
143 return llvm::cast<llvm::FunctionType>(fdecl->type->llvmType->get()); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
144 } |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
145 |
100 | 146 TypeFunction* f = (TypeFunction*)fdecl->type; |
147 assert(f != 0); | |
148 | |
149 const llvm::PointerType* i8pty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
150 std::vector<const llvm::Type*> args; | |
151 | |
152 if (fdecl->llvmInternal == LLVMva_start) { | |
153 args.push_back(i8pty); | |
154 } | |
155 else if (fdecl->llvmInternal == LLVMva_intrinsic) { | |
156 size_t n = Argument::dim(f->parameters); | |
157 for (size_t i=0; i<n; ++i) { | |
158 args.push_back(i8pty); | |
159 } | |
160 } | |
161 else | |
162 assert(0); | |
163 | |
164 const llvm::FunctionType* fty = llvm::FunctionType::get(llvm::Type::VoidTy, args, false); | |
165 | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
166 f->llvmType = new llvm::PATypeHolder(fty); |
100 | 167 |
168 return fty; | |
169 } | |
170 | |
171 ////////////////////////////////////////////////////////////////////////////////////////// | |
172 | |
173 const llvm::FunctionType* DtoFunctionType(FuncDeclaration* fdecl) | |
174 { | |
175 if ((fdecl->llvmInternal == LLVMva_start) || (fdecl->llvmInternal == LLVMva_intrinsic)) { | |
176 return DtoVaFunctionType(fdecl); | |
177 } | |
178 | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
179 // unittest has null type, just build it manually |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
180 /*if (fdecl->isUnitTestDeclaration()) { |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
181 std::vector<const llvm::Type*> args; |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
182 return llvm::FunctionType::get(llvm::Type::VoidTy, args, false); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
183 }*/ |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
184 |
100 | 185 // type has already been resolved |
186 if (fdecl->type->llvmType != 0) { | |
187 return llvm::cast<llvm::FunctionType>(fdecl->type->llvmType->get()); | |
188 } | |
189 | |
190 const llvm::Type* thisty = NULL; | |
191 if (fdecl->needThis()) { | |
192 if (AggregateDeclaration* ad = fdecl->isMember()) { | |
193 Logger::print("isMember = this is: %s\n", ad->type->toChars()); | |
194 thisty = DtoType(ad->type); | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
195 //Logger::cout() << "this llvm type: " << *thisty << '\n'; |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
196 if (isaStruct(thisty) || (!gIR->structs.empty() && thisty == gIR->topstruct()->recty.get())) |
100 | 197 thisty = llvm::PointerType::get(thisty); |
198 } | |
199 else | |
200 assert(0); | |
201 } | |
202 else if (fdecl->isNested()) { | |
203 thisty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
204 } | |
205 | |
206 const llvm::FunctionType* functype = DtoFunctionType(fdecl->type, thisty, fdecl->isMain()); | |
207 | |
208 return functype; | |
209 } | |
210 | |
211 ////////////////////////////////////////////////////////////////////////////////////////// | |
212 | |
213 static llvm::Function* DtoDeclareVaFunction(FuncDeclaration* fdecl) | |
214 { | |
215 TypeFunction* f = (TypeFunction*)DtoDType(fdecl->type); | |
216 const llvm::FunctionType* fty = DtoVaFunctionType(fdecl); | |
217 llvm::Constant* fn = 0; | |
218 | |
219 if (fdecl->llvmInternal == LLVMva_start) { | |
220 fn = gIR->module->getOrInsertFunction("llvm.va_start", fty); | |
221 assert(fn); | |
222 } | |
223 else if (fdecl->llvmInternal == LLVMva_intrinsic) { | |
224 fn = gIR->module->getOrInsertFunction(fdecl->llvmInternal1, fty); | |
225 assert(fn); | |
226 } | |
227 else | |
228 assert(0); | |
229 | |
230 llvm::Function* func = llvm::dyn_cast<llvm::Function>(fn); | |
231 assert(func); | |
232 assert(func->isIntrinsic()); | |
233 fdecl->llvmValue = func; | |
234 return func; | |
235 } | |
236 | |
237 ////////////////////////////////////////////////////////////////////////////////////////// | |
238 | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
239 void DtoResolveFunction(FuncDeclaration* fdecl) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
240 { |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
241 if (!global.params.useUnitTests && fdecl->isUnitTestDeclaration()) { |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
242 return; // ignore declaration completely |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
243 } |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
244 |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
245 if (fdecl->llvmResolved) return; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
246 fdecl->llvmResolved = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
247 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
248 Logger::println("DtoResolveFunction(%s)", fdecl->toPrettyChars()); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
249 LOG_SCOPE; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
250 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
251 if (fdecl->llvmRunTimeHack) { |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
252 gIR->declareList.push_back(fdecl); |
127 | 253 TypeFunction* tf = (TypeFunction*)fdecl->type; |
254 tf->llvmRetInPtr = DtoIsPassedByRef(tf->next); | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
255 return; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
256 } |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
257 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
258 if (fdecl->parent) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
259 if (TemplateInstance* tinst = fdecl->parent->isTemplateInstance()) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
260 { |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
261 TemplateDeclaration* tempdecl = tinst->tempdecl; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
262 if (tempdecl->llvmInternal == LLVMva_arg) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
263 { |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
264 Logger::println("magic va_arg found"); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
265 fdecl->llvmInternal = LLVMva_arg; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
266 fdecl->llvmDeclared = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
267 fdecl->llvmInitialized = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
268 fdecl->llvmDefined = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
269 return; // this gets mapped to an instruction so a declaration makes no sence |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
270 } |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
271 else if (tempdecl->llvmInternal == LLVMva_start) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
272 { |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
273 Logger::println("magic va_start found"); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
274 fdecl->llvmInternal = LLVMva_start; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
275 } |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
276 } |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
277 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
278 DtoFunctionType(fdecl); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
279 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
280 // queue declaration |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
281 if (!fdecl->isAbstract()) |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
282 gIR->declareList.push_back(fdecl); |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
283 } |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
284 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
285 ////////////////////////////////////////////////////////////////////////////////////////// |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
286 |
100 | 287 void DtoDeclareFunction(FuncDeclaration* fdecl) |
288 { | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
289 if (fdecl->llvmDeclared) return; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
290 fdecl->llvmDeclared = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
291 |
100 | 292 Logger::println("DtoDeclareFunction(%s)", fdecl->toPrettyChars()); |
293 LOG_SCOPE; | |
294 | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
295 assert(!fdecl->isAbstract()); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
296 |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
297 // intrinsic sanity check |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
298 if (fdecl->llvmInternal == LLVMintrinsic && fdecl->fbody) { |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
299 error(fdecl->loc, "intrinsics cannot have function bodies"); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
300 fatal(); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
301 } |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
302 |
100 | 303 if (fdecl->llvmRunTimeHack) { |
304 Logger::println("runtime hack func chars: %s", fdecl->toChars()); | |
305 if (!fdecl->llvmValue) | |
306 fdecl->llvmValue = LLVM_D_GetRuntimeFunction(gIR->module, fdecl->toChars()); | |
307 return; | |
308 } | |
309 | |
310 bool declareOnly = false; | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
311 bool templInst = fdecl->parent && DtoIsTemplateInstance(fdecl->parent); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
312 if (!templInst && fdecl->getModule() != gIR->dmodule) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
313 declareOnly = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
314 else if (fdecl->llvmInternal == LLVMva_start) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
315 declareOnly = true; |
100 | 316 |
317 if (!fdecl->llvmIRFunc) { | |
318 fdecl->llvmIRFunc = new IRFunction(fdecl); | |
319 } | |
320 | |
321 // mangled name | |
322 char* mangled_name; | |
323 if (fdecl->llvmInternal == LLVMintrinsic) | |
324 mangled_name = fdecl->llvmInternal1; | |
325 else | |
326 mangled_name = fdecl->mangle(); | |
327 | |
328 llvm::Function* vafunc = 0; | |
329 if ((fdecl->llvmInternal == LLVMva_start) || (fdecl->llvmInternal == LLVMva_intrinsic)) { | |
330 vafunc = DtoDeclareVaFunction(fdecl); | |
331 } | |
332 | |
333 Type* t = DtoDType(fdecl->type); | |
334 TypeFunction* f = (TypeFunction*)t; | |
335 | |
336 // construct function | |
337 const llvm::FunctionType* functype = DtoFunctionType(fdecl); | |
338 llvm::Function* func = vafunc ? vafunc : gIR->module->getFunction(mangled_name); | |
339 if (!func) | |
340 func = new llvm::Function(functype, DtoLinkage(fdecl->protection, fdecl->storage_class), mangled_name, gIR->module); | |
341 else | |
342 assert(func->getFunctionType() == functype); | |
343 | |
344 // add func to IRFunc | |
345 fdecl->llvmIRFunc->func = func; | |
346 | |
347 // calling convention | |
348 if (!vafunc && fdecl->llvmInternal != LLVMintrinsic) | |
349 func->setCallingConv(DtoCallingConv(f->linkage)); | |
350 | |
351 // template instances should have weak linkage | |
352 if (!vafunc && fdecl->llvmInternal != LLVMintrinsic && fdecl->parent && DtoIsTemplateInstance(fdecl->parent)) | |
353 func->setLinkage(llvm::GlobalValue::WeakLinkage); | |
354 | |
355 fdecl->llvmValue = func; | |
356 assert(llvm::isa<llvm::FunctionType>(f->llvmType->get())); | |
357 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
358 // main |
100 | 359 if (fdecl->isMain()) { |
360 gIR->mainFunc = func; | |
361 } | |
362 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
363 // static ctor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
364 if (fdecl->isStaticCtorDeclaration()) { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
365 gIR->ctors.push_back(fdecl); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
366 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
367 // static dtor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
368 else if (fdecl->isStaticDtorDeclaration()) { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
369 gIR->dtors.push_back(fdecl); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
370 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
371 |
100 | 372 // name parameters |
373 llvm::Function::arg_iterator iarg = func->arg_begin(); | |
374 int k = 0; | |
375 if (f->llvmRetInPtr) { | |
376 iarg->setName("retval"); | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
377 fdecl->llvmRetArg = iarg; |
100 | 378 ++iarg; |
379 } | |
380 if (f->llvmUsesThis) { | |
381 iarg->setName("this"); | |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
382 fdecl->llvmThisVar = iarg; |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
383 assert(fdecl->llvmThisVar); |
100 | 384 ++iarg; |
385 } | |
119
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
386 |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
387 if (f->linkage == LINKd && f->varargs == 1) { |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
388 iarg->setName("_arguments"); |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
389 fdecl->llvmArguments = iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
390 ++iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
391 iarg->setName("_argptr"); |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
392 fdecl->llvmArgPtr = iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
393 ++iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
394 } |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
395 |
100 | 396 for (; iarg != func->arg_end(); ++iarg) |
397 { | |
398 Argument* arg = Argument::getNth(f->parameters, k++); | |
399 //arg->llvmValue = iarg; | |
400 //Logger::println("identifier: '%s' %p\n", arg->ident->toChars(), arg->ident); | |
401 if (arg && arg->ident != 0) { | |
402 if (arg->vardecl) { | |
403 arg->vardecl->llvmValue = iarg; | |
404 } | |
405 iarg->setName(arg->ident->toChars()); | |
406 } | |
407 else { | |
408 iarg->setName("unnamed"); | |
409 } | |
410 } | |
411 | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
412 if (fdecl->isUnitTestDeclaration()) |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
413 gIR->unitTests.push_back(fdecl); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
414 |
100 | 415 if (!declareOnly) |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
416 gIR->defineList.push_back(fdecl); |
100 | 417 |
418 Logger::cout() << "func decl: " << *func << '\n'; | |
419 } | |
420 | |
421 ////////////////////////////////////////////////////////////////////////////////////////// | |
422 | |
423 void DtoDefineFunc(FuncDeclaration* fd) | |
424 { | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
425 if (fd->llvmDefined) return; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
426 fd->llvmDefined = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
427 |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
428 assert(fd->llvmDeclared); |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
429 |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
430 Logger::println("DtoDefineFunc(%s)", fd->toPrettyChars()); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
431 LOG_SCOPE; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
432 |
100 | 433 // debug info |
434 if (global.params.symdebug) { | |
435 Module* mo = fd->getModule(); | |
436 if (!mo->llvmCompileUnit) { | |
437 mo->llvmCompileUnit = DtoDwarfCompileUnit(mo,false); | |
438 } | |
439 fd->llvmDwarfSubProgram = DtoDwarfSubProgram(fd, mo->llvmCompileUnit); | |
440 } | |
441 | |
442 Type* t = DtoDType(fd->type); | |
443 TypeFunction* f = (TypeFunction*)t; | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
444 assert(f->llvmType); |
100 | 445 |
446 llvm::Function* func = fd->llvmIRFunc->func; | |
447 const llvm::FunctionType* functype = func->getFunctionType(); | |
448 | |
449 // only members of the current module or template instances maybe be defined | |
450 if (fd->getModule() == gIR->dmodule || DtoIsTemplateInstance(fd->parent)) | |
451 { | |
452 fd->llvmDModule = gIR->dmodule; | |
453 | |
454 // function definition | |
455 if (fd->fbody != 0) | |
456 { | |
457 Logger::println("Doing function body for: %s", fd->toChars()); | |
458 assert(fd->llvmIRFunc); | |
459 gIR->functions.push_back(fd->llvmIRFunc); | |
460 | |
461 if (fd->isMain()) | |
462 gIR->emitMain = true; | |
463 | |
464 llvm::BasicBlock* beginbb = new llvm::BasicBlock("entry",func); | |
465 llvm::BasicBlock* endbb = new llvm::BasicBlock("endentry",func); | |
466 | |
467 //assert(gIR->scopes.empty()); | |
468 gIR->scopes.push_back(IRScope(beginbb, endbb)); | |
469 | |
470 // create alloca point | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
471 llvm::Instruction* allocaPoint = new llvm::BitCastInst(llvm::ConstantInt::get(llvm::Type::Int32Ty,0,false),llvm::Type::Int32Ty,"alloca point",gIR->scopebb()); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
472 gIR->func()->allocapoint = allocaPoint; |
100 | 473 |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
474 // need result variable? (not nested) |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
475 if (fd->vresult && !fd->vresult->nestedref) { |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
476 Logger::println("non-nested vresult value"); |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
477 fd->vresult->llvmValue = new llvm::AllocaInst(DtoType(fd->vresult->type),"function_vresult",allocaPoint); |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
478 } |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
479 |
100 | 480 // give arguments storage |
481 size_t n = Argument::dim(f->parameters); | |
482 for (int i=0; i < n; ++i) { | |
483 Argument* arg = Argument::getNth(f->parameters, i); | |
484 if (arg && arg->vardecl) { | |
485 VarDeclaration* vd = arg->vardecl; | |
486 if (!vd->llvmNeedsStorage || vd->nestedref || vd->isRef() || vd->isOut() || DtoIsPassedByRef(vd->type)) | |
487 continue; | |
488 llvm::Value* a = vd->llvmValue; | |
489 assert(a); | |
490 std::string s(a->getName()); | |
491 Logger::println("giving argument '%s' storage", s.c_str()); | |
492 s.append("_storage"); | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
493 llvm::Value* v = new llvm::AllocaInst(a->getType(),s,allocaPoint); |
100 | 494 gIR->ir->CreateStore(a,v); |
495 vd->llvmValue = v; | |
496 } | |
497 else { | |
498 Logger::attention("some unknown argument: %s", arg ? arg->toChars() : 0); | |
499 } | |
500 } | |
501 | |
502 // debug info | |
503 if (global.params.symdebug) DtoDwarfFuncStart(fd); | |
504 | |
505 llvm::Value* parentNested = NULL; | |
131 | 506 if (FuncDeclaration* fd2 = fd->toParent2()->isFuncDeclaration()) { |
507 if (!fd->isStatic()) // huh? | |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
508 parentNested = fd2->llvmNested; |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
509 } |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
510 |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
511 // need result variable? (nested) |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
512 if (fd->vresult && fd->vresult->nestedref) { |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
513 Logger::println("nested vresult value: %s", fd->vresult->toChars()); |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
514 fd->llvmNestedVars.insert(fd->vresult); |
100 | 515 } |
516 | |
517 // construct nested variables struct | |
518 if (!fd->llvmNestedVars.empty() || parentNested) { | |
519 std::vector<const llvm::Type*> nestTypes; | |
520 int j = 0; | |
521 if (parentNested) { | |
522 nestTypes.push_back(parentNested->getType()); | |
523 j++; | |
524 } | |
525 for (std::set<VarDeclaration*>::iterator i=fd->llvmNestedVars.begin(); i!=fd->llvmNestedVars.end(); ++i) { | |
526 VarDeclaration* vd = *i; | |
527 vd->llvmNestedIndex = j++; | |
528 if (vd->isParameter()) { | |
529 assert(vd->llvmValue); | |
530 nestTypes.push_back(vd->llvmValue->getType()); | |
531 } | |
532 else { | |
533 nestTypes.push_back(DtoType(vd->type)); | |
534 } | |
535 } | |
536 const llvm::StructType* nestSType = llvm::StructType::get(nestTypes); | |
129
8096ba7082db
[svn r133] Fixed some problems with inlining not happening :P
lindquist
parents:
127
diff
changeset
|
537 Logger::cout() << "nested var struct has type:" << *nestSType << '\n'; |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
538 fd->llvmNested = new llvm::AllocaInst(nestSType,"nestedvars",allocaPoint); |
100 | 539 if (parentNested) { |
540 assert(fd->llvmThisVar); | |
541 llvm::Value* ptr = gIR->ir->CreateBitCast(fd->llvmThisVar, parentNested->getType(), "tmp"); | |
542 gIR->ir->CreateStore(ptr, DtoGEPi(fd->llvmNested, 0,0, "tmp")); | |
543 } | |
544 for (std::set<VarDeclaration*>::iterator i=fd->llvmNestedVars.begin(); i!=fd->llvmNestedVars.end(); ++i) { | |
545 VarDeclaration* vd = *i; | |
546 if (vd->isParameter()) { | |
547 gIR->ir->CreateStore(vd->llvmValue, DtoGEPi(fd->llvmNested, 0, vd->llvmNestedIndex, "tmp")); | |
548 vd->llvmValue = fd->llvmNested; | |
549 } | |
550 } | |
551 } | |
552 | |
553 // copy _argptr to a memory location | |
554 if (f->linkage == LINKd && f->varargs == 1) | |
555 { | |
556 llvm::Value* argptrmem = new llvm::AllocaInst(fd->llvmArgPtr->getType(), "_argptrmem", gIR->topallocapoint()); | |
557 new llvm::StoreInst(fd->llvmArgPtr, argptrmem, gIR->scopebb()); | |
558 fd->llvmArgPtr = argptrmem; | |
559 } | |
560 | |
561 // output function body | |
562 fd->fbody->toIR(gIR); | |
563 | |
564 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
565 // in automatically, so we do it here. | |
566 if (!fd->isMain()) { | |
567 if (!gIR->scopereturned()) { | |
568 // pass the previous block into this block | |
569 if (global.params.symdebug) DtoDwarfFuncEnd(fd); | |
570 if (func->getReturnType() == llvm::Type::VoidTy) { | |
571 new llvm::ReturnInst(gIR->scopebb()); | |
572 } | |
573 else { | |
574 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
575 } | |
576 } | |
577 } | |
578 | |
579 // erase alloca point | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
580 allocaPoint->eraseFromParent(); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
581 allocaPoint = 0; |
100 | 582 gIR->func()->allocapoint = 0; |
583 | |
584 gIR->scopes.pop_back(); | |
585 | |
586 // get rid of the endentry block, it's never used | |
587 assert(!func->getBasicBlockList().empty()); | |
588 func->getBasicBlockList().pop_back(); | |
589 | |
590 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
591 // would be nice to figure out how to assert that this is correct | |
592 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); | |
593 if (lastbb->empty()) { | |
594 if (lastbb->getNumUses() == 0) | |
595 lastbb->eraseFromParent(); | |
596 else { | |
597 new llvm::UnreachableInst(lastbb); | |
598 /*if (func->getReturnType() == llvm::Type::VoidTy) { | |
599 new llvm::ReturnInst(lastbb); | |
600 } | |
601 else { | |
602 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), lastbb); | |
603 }*/ | |
604 } | |
605 } | |
606 | |
607 gIR->functions.pop_back(); | |
608 } | |
609 } | |
610 } | |
611 | |
612 ////////////////////////////////////////////////////////////////////////////////////////// | |
613 | |
614 void DtoMain() | |
615 { | |
616 // emit main function llvm style | |
617 // int main(int argc, char**argv, char**env); | |
618 | |
619 assert(gIR != 0); | |
620 IRState& ir = *gIR; | |
621 | |
622 assert(ir.emitMain && ir.mainFunc); | |
623 | |
624 // parameter types | |
625 std::vector<const llvm::Type*> pvec; | |
626 pvec.push_back((const llvm::Type*)llvm::Type::Int32Ty); | |
627 const llvm::Type* chPtrType = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
628 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
629 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
630 const llvm::Type* rettype = (const llvm::Type*)llvm::Type::Int32Ty; | |
631 | |
632 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false); | |
633 llvm::Function* func = new llvm::Function(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module); | |
634 | |
635 llvm::BasicBlock* bb = new llvm::BasicBlock("entry",func); | |
636 | |
637 // call static ctors | |
117 | 638 llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleCtor"); |
100 | 639 llvm::Instruction* apt = new llvm::CallInst(fn,"",bb); |
640 | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
641 // run unit tests if -unittest is provided |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
642 if (global.params.useUnitTests) { |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
643 fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleUnitTests"); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
644 llvm::Instruction* apt = new llvm::CallInst(fn,"",bb); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
645 } |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
646 |
100 | 647 // call user main function |
648 const llvm::FunctionType* mainty = ir.mainFunc->getFunctionType(); | |
649 llvm::CallInst* call; | |
650 if (mainty->getNumParams() > 0) | |
651 { | |
652 // main with arguments | |
653 assert(mainty->getNumParams() == 1); | |
654 std::vector<llvm::Value*> args; | |
655 llvm::Function* mfn = LLVM_D_GetRuntimeFunction(ir.module,"_d_main_args"); | |
656 | |
657 llvm::Function::arg_iterator argi = func->arg_begin(); | |
658 args.push_back(argi++); | |
659 args.push_back(argi++); | |
660 | |
661 const llvm::Type* at = mainty->getParamType(0)->getContainedType(0); | |
662 llvm::Value* arr = new llvm::AllocaInst(at->getContainedType(1)->getContainedType(0), func->arg_begin(), "argstorage", apt); | |
663 llvm::Value* a = new llvm::AllocaInst(at, "argarray", apt); | |
664 llvm::Value* ptr = DtoGEPi(a,0,0,"tmp",bb); | |
665 llvm::Value* v = args[0]; | |
666 if (v->getType() != DtoSize_t()) | |
667 v = new llvm::ZExtInst(v, DtoSize_t(), "tmp", bb); | |
668 new llvm::StoreInst(v,ptr,bb); | |
669 ptr = DtoGEPi(a,0,1,"tmp",bb); | |
670 new llvm::StoreInst(arr,ptr,bb); | |
671 args.push_back(a); | |
672 new llvm::CallInst(mfn, args.begin(), args.end(), "", bb); | |
673 call = new llvm::CallInst(ir.mainFunc,a,"ret",bb); | |
674 } | |
675 else | |
676 { | |
677 // main with no arguments | |
678 call = new llvm::CallInst(ir.mainFunc,"ret",bb); | |
679 } | |
680 call->setCallingConv(ir.mainFunc->getCallingConv()); | |
681 | |
682 // call static dtors | |
117 | 683 fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleDtor"); |
100 | 684 new llvm::CallInst(fn,"",bb); |
685 | |
686 // return | |
687 new llvm::ReturnInst(call,bb); | |
688 } | |
689 | |
690 ////////////////////////////////////////////////////////////////////////////////////////// | |
117 | 691 |
692 const llvm::FunctionType* DtoBaseFunctionType(FuncDeclaration* fdecl) | |
693 { | |
694 Dsymbol* parent = fdecl->toParent(); | |
695 ClassDeclaration* cd = parent->isClassDeclaration(); | |
696 assert(cd); | |
697 | |
698 FuncDeclaration* f = fdecl; | |
699 | |
700 while (cd) | |
701 { | |
702 ClassDeclaration* base = cd->baseClass; | |
703 if (!base) | |
704 break; | |
705 FuncDeclaration* f2 = base->findFunc(fdecl->ident, (TypeFunction*)fdecl->type); | |
706 if (f2) { | |
707 f = f2; | |
708 cd = base; | |
709 } | |
710 else | |
711 break; | |
712 } | |
713 | |
714 DtoResolveDsymbol(f); | |
715 return llvm::cast<llvm::FunctionType>(DtoType(f->type)); | |
716 } | |
717 | |
718 ////////////////////////////////////////////////////////////////////////////////////////// | |
131 | 719 |
720 DValue* DtoArgument(Argument* fnarg, Expression* argexp) | |
721 { | |
722 Logger::println("DtoArgument"); | |
723 LOG_SCOPE; | |
724 | |
725 DValue* arg = argexp->toElem(gIR); | |
726 | |
727 // ref/out arg | |
728 if (fnarg && ((fnarg->storageClass & STCref) || (fnarg->storageClass & STCout))) | |
729 { | |
730 if (arg->isVar() || arg->isLRValue()) | |
731 arg = new DImValue(argexp->type, arg->getLVal(), false); | |
732 else | |
733 arg = new DImValue(argexp->type, arg->getRVal(), false); | |
734 } | |
735 // aggregate arg | |
736 else if (DtoIsPassedByRef(argexp->type)) | |
737 { | |
738 llvm::Value* alloc = new llvm::AllocaInst(DtoType(argexp->type), "tmpparam", gIR->topallocapoint()); | |
739 DVarValue* vv = new DVarValue(argexp->type, alloc, true); | |
740 DtoAssign(vv, arg); | |
741 arg = vv; | |
742 } | |
743 // normal arg (basic/value type) | |
744 else | |
745 { | |
746 // nothing to do | |
747 } | |
748 | |
749 return arg; | |
750 } | |
751 | |
752 ////////////////////////////////////////////////////////////////////////////////////////// | |
753 | |
754 void DtoVariadicArgument(Expression* argexp, llvm::Value* dst) | |
755 { | |
756 Logger::println("DtoVariadicArgument"); | |
757 LOG_SCOPE; | |
758 DVarValue* vv = new DVarValue(argexp->type, dst, true); | |
759 DtoAssign(vv, argexp->toElem(gIR)); | |
760 } | |
761 | |
762 ////////////////////////////////////////////////////////////////////////////////////////// |