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