Mercurial > projects > ldc
annotate gen/functions.cpp @ 132:1700239cab2e trunk
[svn r136] MAJOR UNSTABLE UPDATE!!!
Initial commit after moving to Tango instead of Phobos.
Lots of bugfixes...
This build is not suitable for most things.
author | lindquist |
---|---|
date | Fri, 11 Jan 2008 17:57:40 +0100 |
parents | 5825d48b27d1 |
children | 44a95ac7368a |
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()) { | |
132 | 192 if (AggregateDeclaration* ad = fdecl->isMember2()) { |
193 Logger::println("isMember = this is: %s", ad->type->toChars()); | |
100 | 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 } | |
132 | 199 else { |
200 Logger::println("chars: %s type: %s kind: %s", fdecl->toChars(), fdecl->type->toChars(), fdecl->kind()); | |
201 assert(0); | |
202 } | |
100 | 203 } |
204 else if (fdecl->isNested()) { | |
205 thisty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
206 } | |
207 | |
208 const llvm::FunctionType* functype = DtoFunctionType(fdecl->type, thisty, fdecl->isMain()); | |
209 | |
210 return functype; | |
211 } | |
212 | |
213 ////////////////////////////////////////////////////////////////////////////////////////// | |
214 | |
215 static llvm::Function* DtoDeclareVaFunction(FuncDeclaration* fdecl) | |
216 { | |
217 TypeFunction* f = (TypeFunction*)DtoDType(fdecl->type); | |
218 const llvm::FunctionType* fty = DtoVaFunctionType(fdecl); | |
219 llvm::Constant* fn = 0; | |
220 | |
221 if (fdecl->llvmInternal == LLVMva_start) { | |
222 fn = gIR->module->getOrInsertFunction("llvm.va_start", fty); | |
223 assert(fn); | |
224 } | |
225 else if (fdecl->llvmInternal == LLVMva_intrinsic) { | |
226 fn = gIR->module->getOrInsertFunction(fdecl->llvmInternal1, fty); | |
227 assert(fn); | |
228 } | |
229 else | |
230 assert(0); | |
231 | |
232 llvm::Function* func = llvm::dyn_cast<llvm::Function>(fn); | |
233 assert(func); | |
234 assert(func->isIntrinsic()); | |
235 fdecl->llvmValue = func; | |
236 return func; | |
237 } | |
238 | |
239 ////////////////////////////////////////////////////////////////////////////////////////// | |
240 | |
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
|
241 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
|
242 { |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
243 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
|
244 return; // ignore declaration completely |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
245 } |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
246 |
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
|
247 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
|
248 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
|
249 |
132 | 250 Logger::println("DtoResolveFunction(%s): %s", fdecl->toPrettyChars(), fdecl->loc.toChars()); |
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 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
|
252 |
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 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
|
254 gIR->declareList.push_back(fdecl); |
127 | 255 TypeFunction* tf = (TypeFunction*)fdecl->type; |
256 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
|
257 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
|
258 } |
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 |
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 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
|
261 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
|
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 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
|
264 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
|
265 { |
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 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
|
267 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
|
268 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
|
269 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
|
270 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
|
271 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
|
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 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
|
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 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
|
276 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
|
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 } |
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 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
|
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 // queue declaration |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
283 if (!fdecl->isAbstract()) |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
284 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
|
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 |
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 |
100 | 289 void DtoDeclareFunction(FuncDeclaration* fdecl) |
290 { | |
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
|
291 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
|
292 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
|
293 |
132 | 294 Logger::println("DtoDeclareFunction(%s): %s", fdecl->toPrettyChars(), fdecl->loc.toChars()); |
100 | 295 LOG_SCOPE; |
296 | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
297 assert(!fdecl->isAbstract()); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
298 |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
299 // intrinsic sanity check |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
300 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
|
301 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
|
302 fatal(); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
303 } |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
304 |
100 | 305 if (fdecl->llvmRunTimeHack) { |
306 Logger::println("runtime hack func chars: %s", fdecl->toChars()); | |
307 if (!fdecl->llvmValue) | |
308 fdecl->llvmValue = LLVM_D_GetRuntimeFunction(gIR->module, fdecl->toChars()); | |
309 return; | |
310 } | |
311 | |
312 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
|
313 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
|
314 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
|
315 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
|
316 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
|
317 declareOnly = true; |
100 | 318 |
319 if (!fdecl->llvmIRFunc) { | |
320 fdecl->llvmIRFunc = new IRFunction(fdecl); | |
321 } | |
322 | |
323 // mangled name | |
324 char* mangled_name; | |
325 if (fdecl->llvmInternal == LLVMintrinsic) | |
326 mangled_name = fdecl->llvmInternal1; | |
327 else | |
328 mangled_name = fdecl->mangle(); | |
329 | |
330 llvm::Function* vafunc = 0; | |
331 if ((fdecl->llvmInternal == LLVMva_start) || (fdecl->llvmInternal == LLVMva_intrinsic)) { | |
332 vafunc = DtoDeclareVaFunction(fdecl); | |
333 } | |
334 | |
335 Type* t = DtoDType(fdecl->type); | |
336 TypeFunction* f = (TypeFunction*)t; | |
337 | |
338 // construct function | |
339 const llvm::FunctionType* functype = DtoFunctionType(fdecl); | |
340 llvm::Function* func = vafunc ? vafunc : gIR->module->getFunction(mangled_name); | |
341 if (!func) | |
342 func = new llvm::Function(functype, DtoLinkage(fdecl->protection, fdecl->storage_class), mangled_name, gIR->module); | |
343 else | |
344 assert(func->getFunctionType() == functype); | |
345 | |
346 // add func to IRFunc | |
347 fdecl->llvmIRFunc->func = func; | |
348 | |
349 // calling convention | |
350 if (!vafunc && fdecl->llvmInternal != LLVMintrinsic) | |
351 func->setCallingConv(DtoCallingConv(f->linkage)); | |
352 | |
353 // template instances should have weak linkage | |
354 if (!vafunc && fdecl->llvmInternal != LLVMintrinsic && fdecl->parent && DtoIsTemplateInstance(fdecl->parent)) | |
355 func->setLinkage(llvm::GlobalValue::WeakLinkage); | |
356 | |
132 | 357 // extern(C) functions are always external |
358 if (f->linkage == LINKc) | |
359 func->setLinkage(llvm::GlobalValue::ExternalLinkage); | |
360 | |
361 // intrinsics are always external C | |
362 if (fdecl->llvmInternal == LLVMintrinsic) | |
363 { | |
364 func->setLinkage(llvm::GlobalValue::ExternalLinkage); | |
365 func->setCallingConv(llvm::CallingConv::C); | |
366 } | |
367 | |
100 | 368 fdecl->llvmValue = func; |
369 assert(llvm::isa<llvm::FunctionType>(f->llvmType->get())); | |
370 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
371 // main |
100 | 372 if (fdecl->isMain()) { |
373 gIR->mainFunc = func; | |
374 } | |
375 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
376 // static ctor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
377 if (fdecl->isStaticCtorDeclaration()) { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
378 gIR->ctors.push_back(fdecl); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
379 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
380 // static dtor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
381 else if (fdecl->isStaticDtorDeclaration()) { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
382 gIR->dtors.push_back(fdecl); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
383 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
384 |
100 | 385 // name parameters |
386 llvm::Function::arg_iterator iarg = func->arg_begin(); | |
387 int k = 0; | |
388 if (f->llvmRetInPtr) { | |
389 iarg->setName("retval"); | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
390 fdecl->llvmRetArg = iarg; |
100 | 391 ++iarg; |
392 } | |
393 if (f->llvmUsesThis) { | |
394 iarg->setName("this"); | |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
395 fdecl->llvmThisVar = iarg; |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
396 assert(fdecl->llvmThisVar); |
100 | 397 ++iarg; |
398 } | |
119
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
399 |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
400 if (f->linkage == LINKd && f->varargs == 1) { |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
401 iarg->setName("_arguments"); |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
402 fdecl->llvmArguments = iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
403 ++iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
404 iarg->setName("_argptr"); |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
405 fdecl->llvmArgPtr = iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
406 ++iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
407 } |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
408 |
100 | 409 for (; iarg != func->arg_end(); ++iarg) |
410 { | |
411 Argument* arg = Argument::getNth(f->parameters, k++); | |
412 //arg->llvmValue = iarg; | |
413 //Logger::println("identifier: '%s' %p\n", arg->ident->toChars(), arg->ident); | |
414 if (arg && arg->ident != 0) { | |
415 if (arg->vardecl) { | |
416 arg->vardecl->llvmValue = iarg; | |
417 } | |
418 iarg->setName(arg->ident->toChars()); | |
419 } | |
420 else { | |
421 iarg->setName("unnamed"); | |
422 } | |
423 } | |
424 | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
425 if (fdecl->isUnitTestDeclaration()) |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
426 gIR->unitTests.push_back(fdecl); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
427 |
100 | 428 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
|
429 gIR->defineList.push_back(fdecl); |
100 | 430 |
431 Logger::cout() << "func decl: " << *func << '\n'; | |
432 } | |
433 | |
434 ////////////////////////////////////////////////////////////////////////////////////////// | |
435 | |
436 void DtoDefineFunc(FuncDeclaration* fd) | |
437 { | |
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
|
438 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
|
439 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
|
440 |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
441 assert(fd->llvmDeclared); |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
442 |
132 | 443 Logger::println("DtoDefineFunc(%s): %s", fd->toPrettyChars(), fd->loc.toChars()); |
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
|
444 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
|
445 |
100 | 446 // debug info |
447 if (global.params.symdebug) { | |
448 Module* mo = fd->getModule(); | |
449 if (!mo->llvmCompileUnit) { | |
450 mo->llvmCompileUnit = DtoDwarfCompileUnit(mo,false); | |
451 } | |
452 fd->llvmDwarfSubProgram = DtoDwarfSubProgram(fd, mo->llvmCompileUnit); | |
453 } | |
454 | |
455 Type* t = DtoDType(fd->type); | |
456 TypeFunction* f = (TypeFunction*)t; | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
457 assert(f->llvmType); |
100 | 458 |
459 llvm::Function* func = fd->llvmIRFunc->func; | |
460 const llvm::FunctionType* functype = func->getFunctionType(); | |
461 | |
462 // only members of the current module or template instances maybe be defined | |
463 if (fd->getModule() == gIR->dmodule || DtoIsTemplateInstance(fd->parent)) | |
464 { | |
465 fd->llvmDModule = gIR->dmodule; | |
466 | |
467 // function definition | |
468 if (fd->fbody != 0) | |
469 { | |
470 Logger::println("Doing function body for: %s", fd->toChars()); | |
471 assert(fd->llvmIRFunc); | |
472 gIR->functions.push_back(fd->llvmIRFunc); | |
473 | |
474 if (fd->isMain()) | |
475 gIR->emitMain = true; | |
476 | |
477 llvm::BasicBlock* beginbb = new llvm::BasicBlock("entry",func); | |
478 llvm::BasicBlock* endbb = new llvm::BasicBlock("endentry",func); | |
479 | |
480 //assert(gIR->scopes.empty()); | |
481 gIR->scopes.push_back(IRScope(beginbb, endbb)); | |
482 | |
483 // create alloca point | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
484 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
|
485 gIR->func()->allocapoint = allocaPoint; |
100 | 486 |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
487 // need result variable? (not nested) |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
488 if (fd->vresult && !fd->vresult->nestedref) { |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
489 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
|
490 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
|
491 } |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
492 |
100 | 493 // give arguments storage |
494 size_t n = Argument::dim(f->parameters); | |
495 for (int i=0; i < n; ++i) { | |
496 Argument* arg = Argument::getNth(f->parameters, i); | |
497 if (arg && arg->vardecl) { | |
498 VarDeclaration* vd = arg->vardecl; | |
499 if (!vd->llvmNeedsStorage || vd->nestedref || vd->isRef() || vd->isOut() || DtoIsPassedByRef(vd->type)) | |
500 continue; | |
501 llvm::Value* a = vd->llvmValue; | |
502 assert(a); | |
503 std::string s(a->getName()); | |
504 Logger::println("giving argument '%s' storage", s.c_str()); | |
505 s.append("_storage"); | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
506 llvm::Value* v = new llvm::AllocaInst(a->getType(),s,allocaPoint); |
100 | 507 gIR->ir->CreateStore(a,v); |
508 vd->llvmValue = v; | |
509 } | |
510 else { | |
132 | 511 Logger::attention(fd->loc, "some unknown argument: %s", arg ? arg->toChars() : 0); |
100 | 512 } |
513 } | |
514 | |
515 // debug info | |
516 if (global.params.symdebug) DtoDwarfFuncStart(fd); | |
517 | |
518 llvm::Value* parentNested = NULL; | |
131 | 519 if (FuncDeclaration* fd2 = fd->toParent2()->isFuncDeclaration()) { |
520 if (!fd->isStatic()) // huh? | |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
521 parentNested = fd2->llvmNested; |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
522 } |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
523 |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
524 // need result variable? (nested) |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
525 if (fd->vresult && fd->vresult->nestedref) { |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
526 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
|
527 fd->llvmNestedVars.insert(fd->vresult); |
100 | 528 } |
529 | |
530 // construct nested variables struct | |
531 if (!fd->llvmNestedVars.empty() || parentNested) { | |
532 std::vector<const llvm::Type*> nestTypes; | |
533 int j = 0; | |
534 if (parentNested) { | |
535 nestTypes.push_back(parentNested->getType()); | |
536 j++; | |
537 } | |
538 for (std::set<VarDeclaration*>::iterator i=fd->llvmNestedVars.begin(); i!=fd->llvmNestedVars.end(); ++i) { | |
539 VarDeclaration* vd = *i; | |
540 vd->llvmNestedIndex = j++; | |
541 if (vd->isParameter()) { | |
542 assert(vd->llvmValue); | |
543 nestTypes.push_back(vd->llvmValue->getType()); | |
544 } | |
545 else { | |
546 nestTypes.push_back(DtoType(vd->type)); | |
547 } | |
548 } | |
549 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
|
550 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
|
551 fd->llvmNested = new llvm::AllocaInst(nestSType,"nestedvars",allocaPoint); |
100 | 552 if (parentNested) { |
553 assert(fd->llvmThisVar); | |
554 llvm::Value* ptr = gIR->ir->CreateBitCast(fd->llvmThisVar, parentNested->getType(), "tmp"); | |
555 gIR->ir->CreateStore(ptr, DtoGEPi(fd->llvmNested, 0,0, "tmp")); | |
556 } | |
557 for (std::set<VarDeclaration*>::iterator i=fd->llvmNestedVars.begin(); i!=fd->llvmNestedVars.end(); ++i) { | |
558 VarDeclaration* vd = *i; | |
559 if (vd->isParameter()) { | |
560 gIR->ir->CreateStore(vd->llvmValue, DtoGEPi(fd->llvmNested, 0, vd->llvmNestedIndex, "tmp")); | |
561 vd->llvmValue = fd->llvmNested; | |
562 } | |
563 } | |
564 } | |
565 | |
566 // copy _argptr to a memory location | |
567 if (f->linkage == LINKd && f->varargs == 1) | |
568 { | |
569 llvm::Value* argptrmem = new llvm::AllocaInst(fd->llvmArgPtr->getType(), "_argptrmem", gIR->topallocapoint()); | |
570 new llvm::StoreInst(fd->llvmArgPtr, argptrmem, gIR->scopebb()); | |
571 fd->llvmArgPtr = argptrmem; | |
572 } | |
573 | |
574 // output function body | |
575 fd->fbody->toIR(gIR); | |
576 | |
577 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
578 // in automatically, so we do it here. | |
579 if (!fd->isMain()) { | |
580 if (!gIR->scopereturned()) { | |
581 // pass the previous block into this block | |
582 if (global.params.symdebug) DtoDwarfFuncEnd(fd); | |
583 if (func->getReturnType() == llvm::Type::VoidTy) { | |
584 new llvm::ReturnInst(gIR->scopebb()); | |
585 } | |
586 else { | |
587 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
588 } | |
589 } | |
590 } | |
591 | |
592 // erase alloca point | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
593 allocaPoint->eraseFromParent(); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
594 allocaPoint = 0; |
100 | 595 gIR->func()->allocapoint = 0; |
596 | |
597 gIR->scopes.pop_back(); | |
598 | |
599 // get rid of the endentry block, it's never used | |
600 assert(!func->getBasicBlockList().empty()); | |
601 func->getBasicBlockList().pop_back(); | |
602 | |
603 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
604 // would be nice to figure out how to assert that this is correct | |
605 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); | |
606 if (lastbb->empty()) { | |
607 if (lastbb->getNumUses() == 0) | |
608 lastbb->eraseFromParent(); | |
609 else { | |
610 new llvm::UnreachableInst(lastbb); | |
611 /*if (func->getReturnType() == llvm::Type::VoidTy) { | |
612 new llvm::ReturnInst(lastbb); | |
613 } | |
614 else { | |
615 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), lastbb); | |
616 }*/ | |
617 } | |
618 } | |
619 | |
620 gIR->functions.pop_back(); | |
621 } | |
622 } | |
623 } | |
624 | |
625 ////////////////////////////////////////////////////////////////////////////////////////// | |
626 | |
627 void DtoMain() | |
628 { | |
629 // emit main function llvm style | |
630 // int main(int argc, char**argv, char**env); | |
631 | |
632 assert(gIR != 0); | |
633 IRState& ir = *gIR; | |
634 | |
635 assert(ir.emitMain && ir.mainFunc); | |
636 | |
637 // parameter types | |
638 std::vector<const llvm::Type*> pvec; | |
639 pvec.push_back((const llvm::Type*)llvm::Type::Int32Ty); | |
640 const llvm::Type* chPtrType = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
641 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
642 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
643 const llvm::Type* rettype = (const llvm::Type*)llvm::Type::Int32Ty; | |
644 | |
645 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false); | |
646 llvm::Function* func = new llvm::Function(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module); | |
647 | |
648 llvm::BasicBlock* bb = new llvm::BasicBlock("entry",func); | |
649 | |
650 // call static ctors | |
117 | 651 llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleCtor"); |
100 | 652 llvm::Instruction* apt = new llvm::CallInst(fn,"",bb); |
653 | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
654 // 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
|
655 if (global.params.useUnitTests) { |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
656 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
|
657 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
|
658 } |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
659 |
100 | 660 // call user main function |
661 const llvm::FunctionType* mainty = ir.mainFunc->getFunctionType(); | |
662 llvm::CallInst* call; | |
663 if (mainty->getNumParams() > 0) | |
664 { | |
665 // main with arguments | |
666 assert(mainty->getNumParams() == 1); | |
667 std::vector<llvm::Value*> args; | |
668 llvm::Function* mfn = LLVM_D_GetRuntimeFunction(ir.module,"_d_main_args"); | |
669 | |
670 llvm::Function::arg_iterator argi = func->arg_begin(); | |
671 args.push_back(argi++); | |
672 args.push_back(argi++); | |
673 | |
674 const llvm::Type* at = mainty->getParamType(0)->getContainedType(0); | |
675 llvm::Value* arr = new llvm::AllocaInst(at->getContainedType(1)->getContainedType(0), func->arg_begin(), "argstorage", apt); | |
676 llvm::Value* a = new llvm::AllocaInst(at, "argarray", apt); | |
677 llvm::Value* ptr = DtoGEPi(a,0,0,"tmp",bb); | |
678 llvm::Value* v = args[0]; | |
679 if (v->getType() != DtoSize_t()) | |
680 v = new llvm::ZExtInst(v, DtoSize_t(), "tmp", bb); | |
681 new llvm::StoreInst(v,ptr,bb); | |
682 ptr = DtoGEPi(a,0,1,"tmp",bb); | |
683 new llvm::StoreInst(arr,ptr,bb); | |
684 args.push_back(a); | |
685 new llvm::CallInst(mfn, args.begin(), args.end(), "", bb); | |
686 call = new llvm::CallInst(ir.mainFunc,a,"ret",bb); | |
687 } | |
688 else | |
689 { | |
690 // main with no arguments | |
691 call = new llvm::CallInst(ir.mainFunc,"ret",bb); | |
692 } | |
693 call->setCallingConv(ir.mainFunc->getCallingConv()); | |
694 | |
695 // call static dtors | |
117 | 696 fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleDtor"); |
100 | 697 new llvm::CallInst(fn,"",bb); |
698 | |
699 // return | |
700 new llvm::ReturnInst(call,bb); | |
701 } | |
702 | |
703 ////////////////////////////////////////////////////////////////////////////////////////// | |
117 | 704 |
705 const llvm::FunctionType* DtoBaseFunctionType(FuncDeclaration* fdecl) | |
706 { | |
707 Dsymbol* parent = fdecl->toParent(); | |
708 ClassDeclaration* cd = parent->isClassDeclaration(); | |
709 assert(cd); | |
710 | |
711 FuncDeclaration* f = fdecl; | |
712 | |
713 while (cd) | |
714 { | |
715 ClassDeclaration* base = cd->baseClass; | |
716 if (!base) | |
717 break; | |
718 FuncDeclaration* f2 = base->findFunc(fdecl->ident, (TypeFunction*)fdecl->type); | |
719 if (f2) { | |
720 f = f2; | |
721 cd = base; | |
722 } | |
723 else | |
724 break; | |
725 } | |
726 | |
727 DtoResolveDsymbol(f); | |
728 return llvm::cast<llvm::FunctionType>(DtoType(f->type)); | |
729 } | |
730 | |
731 ////////////////////////////////////////////////////////////////////////////////////////// | |
131 | 732 |
733 DValue* DtoArgument(Argument* fnarg, Expression* argexp) | |
734 { | |
735 Logger::println("DtoArgument"); | |
736 LOG_SCOPE; | |
737 | |
738 DValue* arg = argexp->toElem(gIR); | |
739 | |
740 // ref/out arg | |
741 if (fnarg && ((fnarg->storageClass & STCref) || (fnarg->storageClass & STCout))) | |
742 { | |
743 if (arg->isVar() || arg->isLRValue()) | |
744 arg = new DImValue(argexp->type, arg->getLVal(), false); | |
745 else | |
746 arg = new DImValue(argexp->type, arg->getRVal(), false); | |
747 } | |
748 // aggregate arg | |
749 else if (DtoIsPassedByRef(argexp->type)) | |
750 { | |
751 llvm::Value* alloc = new llvm::AllocaInst(DtoType(argexp->type), "tmpparam", gIR->topallocapoint()); | |
752 DVarValue* vv = new DVarValue(argexp->type, alloc, true); | |
753 DtoAssign(vv, arg); | |
754 arg = vv; | |
755 } | |
756 // normal arg (basic/value type) | |
757 else | |
758 { | |
759 // nothing to do | |
760 } | |
761 | |
762 return arg; | |
763 } | |
764 | |
765 ////////////////////////////////////////////////////////////////////////////////////////// | |
766 | |
767 void DtoVariadicArgument(Expression* argexp, llvm::Value* dst) | |
768 { | |
769 Logger::println("DtoVariadicArgument"); | |
770 LOG_SCOPE; | |
771 DVarValue* vv = new DVarValue(argexp->type, dst, true); | |
772 DtoAssign(vv, argexp->toElem(gIR)); | |
773 } | |
774 | |
775 ////////////////////////////////////////////////////////////////////////////////////////// |