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