Mercurial > projects > ldc
annotate gen/functions.cpp @ 123:7f9a0a58394b trunk
[svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
Removed some potentially very long logging lines.
Added support for inner classes.
author | lindquist |
---|---|
date | Wed, 28 Nov 2007 03:01:51 +0100 |
parents | 9c79b61fb638 |
children | facc562f5674 |
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); |
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 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
|
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 |
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 (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
|
263 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
|
264 { |
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 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
|
266 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
|
267 { |
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 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
|
269 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
|
270 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
|
271 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
|
272 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
|
273 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
|
274 } |
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 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
|
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 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
|
278 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
|
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 } |
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 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
|
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 // queue declaration |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
285 if (!fdecl->isAbstract()) |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
286 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
|
287 } |
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
|
288 |
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 |
100 | 291 void DtoDeclareFunction(FuncDeclaration* fdecl) |
292 { | |
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
|
293 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
|
294 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
|
295 |
100 | 296 Logger::println("DtoDeclareFunction(%s)", fdecl->toPrettyChars()); |
297 LOG_SCOPE; | |
298 | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
299 assert(!fdecl->isAbstract()); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
300 |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
301 // intrinsic sanity check |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
302 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
|
303 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
|
304 fatal(); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
305 } |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
306 |
100 | 307 if (fdecl->llvmRunTimeHack) { |
308 Logger::println("runtime hack func chars: %s", fdecl->toChars()); | |
309 if (!fdecl->llvmValue) | |
310 fdecl->llvmValue = LLVM_D_GetRuntimeFunction(gIR->module, fdecl->toChars()); | |
311 return; | |
312 } | |
313 | |
314 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
|
315 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
|
316 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
|
317 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
|
318 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
|
319 declareOnly = true; |
100 | 320 |
321 if (!fdecl->llvmIRFunc) { | |
322 fdecl->llvmIRFunc = new IRFunction(fdecl); | |
323 } | |
324 | |
325 // mangled name | |
326 char* mangled_name; | |
327 if (fdecl->llvmInternal == LLVMintrinsic) | |
328 mangled_name = fdecl->llvmInternal1; | |
329 else | |
330 mangled_name = fdecl->mangle(); | |
331 | |
332 llvm::Function* vafunc = 0; | |
333 if ((fdecl->llvmInternal == LLVMva_start) || (fdecl->llvmInternal == LLVMva_intrinsic)) { | |
334 vafunc = DtoDeclareVaFunction(fdecl); | |
335 } | |
336 | |
337 Type* t = DtoDType(fdecl->type); | |
338 TypeFunction* f = (TypeFunction*)t; | |
339 | |
340 // construct function | |
341 const llvm::FunctionType* functype = DtoFunctionType(fdecl); | |
342 llvm::Function* func = vafunc ? vafunc : gIR->module->getFunction(mangled_name); | |
343 if (!func) | |
344 func = new llvm::Function(functype, DtoLinkage(fdecl->protection, fdecl->storage_class), mangled_name, gIR->module); | |
345 else | |
346 assert(func->getFunctionType() == functype); | |
347 | |
348 // add func to IRFunc | |
349 fdecl->llvmIRFunc->func = func; | |
350 | |
351 // calling convention | |
352 if (!vafunc && fdecl->llvmInternal != LLVMintrinsic) | |
353 func->setCallingConv(DtoCallingConv(f->linkage)); | |
354 | |
355 // template instances should have weak linkage | |
356 if (!vafunc && fdecl->llvmInternal != LLVMintrinsic && fdecl->parent && DtoIsTemplateInstance(fdecl->parent)) | |
357 func->setLinkage(llvm::GlobalValue::WeakLinkage); | |
358 | |
359 fdecl->llvmValue = func; | |
360 assert(llvm::isa<llvm::FunctionType>(f->llvmType->get())); | |
361 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
362 // main |
100 | 363 if (fdecl->isMain()) { |
364 gIR->mainFunc = func; | |
365 } | |
366 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
367 // static ctor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
368 if (fdecl->isStaticCtorDeclaration()) { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
369 gIR->ctors.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 // static dtor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
372 else if (fdecl->isStaticDtorDeclaration()) { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
373 gIR->dtors.push_back(fdecl); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
374 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
375 |
100 | 376 // name parameters |
377 llvm::Function::arg_iterator iarg = func->arg_begin(); | |
378 int k = 0; | |
379 if (f->llvmRetInPtr) { | |
380 iarg->setName("retval"); | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
381 fdecl->llvmRetArg = iarg; |
100 | 382 ++iarg; |
383 } | |
384 if (f->llvmUsesThis) { | |
385 iarg->setName("this"); | |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
386 fdecl->llvmThisVar = iarg; |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
387 assert(fdecl->llvmThisVar); |
100 | 388 ++iarg; |
389 } | |
119
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
390 |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
391 if (f->linkage == LINKd && f->varargs == 1) { |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
392 iarg->setName("_arguments"); |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
393 fdecl->llvmArguments = iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
394 ++iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
395 iarg->setName("_argptr"); |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
396 fdecl->llvmArgPtr = iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
397 ++iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
398 } |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
399 |
100 | 400 for (; iarg != func->arg_end(); ++iarg) |
401 { | |
402 Argument* arg = Argument::getNth(f->parameters, k++); | |
403 //arg->llvmValue = iarg; | |
404 //Logger::println("identifier: '%s' %p\n", arg->ident->toChars(), arg->ident); | |
405 if (arg && arg->ident != 0) { | |
406 if (arg->vardecl) { | |
407 arg->vardecl->llvmValue = iarg; | |
408 } | |
409 iarg->setName(arg->ident->toChars()); | |
410 } | |
411 else { | |
412 iarg->setName("unnamed"); | |
413 } | |
414 } | |
415 | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
416 if (fdecl->isUnitTestDeclaration()) |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
417 gIR->unitTests.push_back(fdecl); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
418 |
100 | 419 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
|
420 gIR->defineList.push_back(fdecl); |
100 | 421 |
422 Logger::cout() << "func decl: " << *func << '\n'; | |
423 } | |
424 | |
425 ////////////////////////////////////////////////////////////////////////////////////////// | |
426 | |
427 void DtoDefineFunc(FuncDeclaration* fd) | |
428 { | |
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
|
429 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
|
430 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
|
431 |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
432 assert(fd->llvmDeclared); |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
433 |
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
|
434 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
|
435 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
|
436 |
100 | 437 // debug info |
438 if (global.params.symdebug) { | |
439 Module* mo = fd->getModule(); | |
440 if (!mo->llvmCompileUnit) { | |
441 mo->llvmCompileUnit = DtoDwarfCompileUnit(mo,false); | |
442 } | |
443 fd->llvmDwarfSubProgram = DtoDwarfSubProgram(fd, mo->llvmCompileUnit); | |
444 } | |
445 | |
446 Type* t = DtoDType(fd->type); | |
447 TypeFunction* f = (TypeFunction*)t; | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
448 assert(f->llvmType); |
100 | 449 |
450 llvm::Function* func = fd->llvmIRFunc->func; | |
451 const llvm::FunctionType* functype = func->getFunctionType(); | |
452 | |
453 // only members of the current module or template instances maybe be defined | |
454 if (fd->getModule() == gIR->dmodule || DtoIsTemplateInstance(fd->parent)) | |
455 { | |
456 fd->llvmDModule = gIR->dmodule; | |
457 | |
458 // function definition | |
459 if (fd->fbody != 0) | |
460 { | |
461 Logger::println("Doing function body for: %s", fd->toChars()); | |
462 assert(fd->llvmIRFunc); | |
463 gIR->functions.push_back(fd->llvmIRFunc); | |
464 | |
465 if (fd->isMain()) | |
466 gIR->emitMain = true; | |
467 | |
468 llvm::BasicBlock* beginbb = new llvm::BasicBlock("entry",func); | |
469 llvm::BasicBlock* endbb = new llvm::BasicBlock("endentry",func); | |
470 | |
471 //assert(gIR->scopes.empty()); | |
472 gIR->scopes.push_back(IRScope(beginbb, endbb)); | |
473 | |
474 // create alloca point | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
475 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
|
476 gIR->func()->allocapoint = allocaPoint; |
100 | 477 |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
478 // need result variable? (not nested) |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
479 if (fd->vresult && !fd->vresult->nestedref) { |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
480 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
|
481 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
|
482 } |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
483 |
100 | 484 // give arguments storage |
485 size_t n = Argument::dim(f->parameters); | |
486 for (int i=0; i < n; ++i) { | |
487 Argument* arg = Argument::getNth(f->parameters, i); | |
488 if (arg && arg->vardecl) { | |
489 VarDeclaration* vd = arg->vardecl; | |
490 if (!vd->llvmNeedsStorage || vd->nestedref || vd->isRef() || vd->isOut() || DtoIsPassedByRef(vd->type)) | |
491 continue; | |
492 llvm::Value* a = vd->llvmValue; | |
493 assert(a); | |
494 std::string s(a->getName()); | |
495 Logger::println("giving argument '%s' storage", s.c_str()); | |
496 s.append("_storage"); | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
497 llvm::Value* v = new llvm::AllocaInst(a->getType(),s,allocaPoint); |
100 | 498 gIR->ir->CreateStore(a,v); |
499 vd->llvmValue = v; | |
500 } | |
501 else { | |
502 Logger::attention("some unknown argument: %s", arg ? arg->toChars() : 0); | |
503 } | |
504 } | |
505 | |
506 // debug info | |
507 if (global.params.symdebug) DtoDwarfFuncStart(fd); | |
508 | |
509 llvm::Value* parentNested = NULL; | |
510 if (FuncDeclaration* fd2 = fd->toParent()->isFuncDeclaration()) { | |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
511 if (!fd->isStatic()) |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
512 parentNested = fd2->llvmNested; |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
513 } |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
514 |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
515 // need result variable? (nested) |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
516 if (fd->vresult && fd->vresult->nestedref) { |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
517 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
|
518 fd->llvmNestedVars.insert(fd->vresult); |
100 | 519 } |
520 | |
521 // construct nested variables struct | |
522 if (!fd->llvmNestedVars.empty() || parentNested) { | |
523 std::vector<const llvm::Type*> nestTypes; | |
524 int j = 0; | |
525 if (parentNested) { | |
526 nestTypes.push_back(parentNested->getType()); | |
527 j++; | |
528 } | |
529 for (std::set<VarDeclaration*>::iterator i=fd->llvmNestedVars.begin(); i!=fd->llvmNestedVars.end(); ++i) { | |
530 VarDeclaration* vd = *i; | |
531 vd->llvmNestedIndex = j++; | |
532 if (vd->isParameter()) { | |
533 assert(vd->llvmValue); | |
534 nestTypes.push_back(vd->llvmValue->getType()); | |
535 } | |
536 else { | |
537 nestTypes.push_back(DtoType(vd->type)); | |
538 } | |
539 } | |
540 const llvm::StructType* nestSType = llvm::StructType::get(nestTypes); | |
541 Logger::cout() << "nested var struct has type:" << '\n' << *nestSType; | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
542 fd->llvmNested = new llvm::AllocaInst(nestSType,"nestedvars",allocaPoint); |
100 | 543 if (parentNested) { |
544 assert(fd->llvmThisVar); | |
545 llvm::Value* ptr = gIR->ir->CreateBitCast(fd->llvmThisVar, parentNested->getType(), "tmp"); | |
546 gIR->ir->CreateStore(ptr, DtoGEPi(fd->llvmNested, 0,0, "tmp")); | |
547 } | |
548 for (std::set<VarDeclaration*>::iterator i=fd->llvmNestedVars.begin(); i!=fd->llvmNestedVars.end(); ++i) { | |
549 VarDeclaration* vd = *i; | |
550 if (vd->isParameter()) { | |
551 gIR->ir->CreateStore(vd->llvmValue, DtoGEPi(fd->llvmNested, 0, vd->llvmNestedIndex, "tmp")); | |
552 vd->llvmValue = fd->llvmNested; | |
553 } | |
554 } | |
555 } | |
556 | |
557 // copy _argptr to a memory location | |
558 if (f->linkage == LINKd && f->varargs == 1) | |
559 { | |
560 llvm::Value* argptrmem = new llvm::AllocaInst(fd->llvmArgPtr->getType(), "_argptrmem", gIR->topallocapoint()); | |
561 new llvm::StoreInst(fd->llvmArgPtr, argptrmem, gIR->scopebb()); | |
562 fd->llvmArgPtr = argptrmem; | |
563 } | |
564 | |
565 // output function body | |
566 fd->fbody->toIR(gIR); | |
567 | |
568 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
569 // in automatically, so we do it here. | |
570 if (!fd->isMain()) { | |
571 if (!gIR->scopereturned()) { | |
572 // pass the previous block into this block | |
573 if (global.params.symdebug) DtoDwarfFuncEnd(fd); | |
574 if (func->getReturnType() == llvm::Type::VoidTy) { | |
575 new llvm::ReturnInst(gIR->scopebb()); | |
576 } | |
577 else { | |
578 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
579 } | |
580 } | |
581 } | |
582 | |
583 // erase alloca point | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
584 allocaPoint->eraseFromParent(); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
585 allocaPoint = 0; |
100 | 586 gIR->func()->allocapoint = 0; |
587 | |
588 gIR->scopes.pop_back(); | |
589 | |
590 // get rid of the endentry block, it's never used | |
591 assert(!func->getBasicBlockList().empty()); | |
592 func->getBasicBlockList().pop_back(); | |
593 | |
594 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
595 // would be nice to figure out how to assert that this is correct | |
596 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); | |
597 if (lastbb->empty()) { | |
598 if (lastbb->getNumUses() == 0) | |
599 lastbb->eraseFromParent(); | |
600 else { | |
601 new llvm::UnreachableInst(lastbb); | |
602 /*if (func->getReturnType() == llvm::Type::VoidTy) { | |
603 new llvm::ReturnInst(lastbb); | |
604 } | |
605 else { | |
606 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), lastbb); | |
607 }*/ | |
608 } | |
609 } | |
610 | |
611 gIR->functions.pop_back(); | |
612 } | |
613 } | |
614 } | |
615 | |
616 ////////////////////////////////////////////////////////////////////////////////////////// | |
617 | |
618 void DtoMain() | |
619 { | |
620 // emit main function llvm style | |
621 // int main(int argc, char**argv, char**env); | |
622 | |
623 assert(gIR != 0); | |
624 IRState& ir = *gIR; | |
625 | |
626 assert(ir.emitMain && ir.mainFunc); | |
627 | |
628 // parameter types | |
629 std::vector<const llvm::Type*> pvec; | |
630 pvec.push_back((const llvm::Type*)llvm::Type::Int32Ty); | |
631 const llvm::Type* chPtrType = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
632 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
633 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
634 const llvm::Type* rettype = (const llvm::Type*)llvm::Type::Int32Ty; | |
635 | |
636 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false); | |
637 llvm::Function* func = new llvm::Function(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module); | |
638 | |
639 llvm::BasicBlock* bb = new llvm::BasicBlock("entry",func); | |
640 | |
641 // call static ctors | |
117 | 642 llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleCtor"); |
100 | 643 llvm::Instruction* apt = new llvm::CallInst(fn,"",bb); |
644 | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
645 // 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
|
646 if (global.params.useUnitTests) { |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
647 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
|
648 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
|
649 } |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
650 |
100 | 651 // call user main function |
652 const llvm::FunctionType* mainty = ir.mainFunc->getFunctionType(); | |
653 llvm::CallInst* call; | |
654 if (mainty->getNumParams() > 0) | |
655 { | |
656 // main with arguments | |
657 assert(mainty->getNumParams() == 1); | |
658 std::vector<llvm::Value*> args; | |
659 llvm::Function* mfn = LLVM_D_GetRuntimeFunction(ir.module,"_d_main_args"); | |
660 | |
661 llvm::Function::arg_iterator argi = func->arg_begin(); | |
662 args.push_back(argi++); | |
663 args.push_back(argi++); | |
664 | |
665 const llvm::Type* at = mainty->getParamType(0)->getContainedType(0); | |
666 llvm::Value* arr = new llvm::AllocaInst(at->getContainedType(1)->getContainedType(0), func->arg_begin(), "argstorage", apt); | |
667 llvm::Value* a = new llvm::AllocaInst(at, "argarray", apt); | |
668 llvm::Value* ptr = DtoGEPi(a,0,0,"tmp",bb); | |
669 llvm::Value* v = args[0]; | |
670 if (v->getType() != DtoSize_t()) | |
671 v = new llvm::ZExtInst(v, DtoSize_t(), "tmp", bb); | |
672 new llvm::StoreInst(v,ptr,bb); | |
673 ptr = DtoGEPi(a,0,1,"tmp",bb); | |
674 new llvm::StoreInst(arr,ptr,bb); | |
675 args.push_back(a); | |
676 new llvm::CallInst(mfn, args.begin(), args.end(), "", bb); | |
677 call = new llvm::CallInst(ir.mainFunc,a,"ret",bb); | |
678 } | |
679 else | |
680 { | |
681 // main with no arguments | |
682 call = new llvm::CallInst(ir.mainFunc,"ret",bb); | |
683 } | |
684 call->setCallingConv(ir.mainFunc->getCallingConv()); | |
685 | |
686 // call static dtors | |
117 | 687 fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleDtor"); |
100 | 688 new llvm::CallInst(fn,"",bb); |
689 | |
690 // return | |
691 new llvm::ReturnInst(call,bb); | |
692 } | |
693 | |
694 ////////////////////////////////////////////////////////////////////////////////////////// | |
117 | 695 |
696 const llvm::FunctionType* DtoBaseFunctionType(FuncDeclaration* fdecl) | |
697 { | |
698 Dsymbol* parent = fdecl->toParent(); | |
699 ClassDeclaration* cd = parent->isClassDeclaration(); | |
700 assert(cd); | |
701 | |
702 FuncDeclaration* f = fdecl; | |
703 | |
704 while (cd) | |
705 { | |
706 ClassDeclaration* base = cd->baseClass; | |
707 if (!base) | |
708 break; | |
709 FuncDeclaration* f2 = base->findFunc(fdecl->ident, (TypeFunction*)fdecl->type); | |
710 if (f2) { | |
711 f = f2; | |
712 cd = base; | |
713 } | |
714 else | |
715 break; | |
716 } | |
717 | |
718 DtoResolveDsymbol(f); | |
719 return llvm::cast<llvm::FunctionType>(DtoType(f->type)); | |
720 } | |
721 | |
722 ////////////////////////////////////////////////////////////////////////////////////////// |