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