Mercurial > projects > ldc
annotate gen/functions.cpp @ 153:2c447715c047 trunk
[svn r159] added some code to help diagnose ticket #35
author | lindquist |
---|---|
date | Tue, 25 Mar 2008 16:33:19 +0100 |
parents | 4c577c2b7229 |
children | 7f92f477ff53 |
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)) { |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
50 rettype = getPtrToType(DtoType(rt)); |
100 | 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); |
137 | 77 assert(ti->irStruct->constInit); |
100 | 78 std::vector<const llvm::Type*> types; |
79 types.push_back(DtoSize_t()); | |
137 | 80 types.push_back(getPtrToType(getPtrToType(ti->irStruct->constInit->getType()))); |
100 | 81 const llvm::Type* t1 = llvm::StructType::get(types); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
82 paramvec.push_back(getPtrToType(t1)); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
83 paramvec.push_back(getPtrToType(llvm::Type::Int8Ty)); |
100 | 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"); | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
97 paramvec.push_back(getPtrToType(at)); |
100 | 98 } |
99 else if (isaArray(at)) { | |
100 Logger::println("sarray param"); | |
101 assert(argT->ty == Tsarray); | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
102 //paramvec.push_back(getPtrToType(at->getContainedType(0))); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
103 paramvec.push_back(getPtrToType(at)); |
100 | 104 } |
105 else if (llvm::isa<llvm::OpaqueType>(at)) { | |
106 Logger::println("opaque param"); | |
107 assert(argT->ty == Tstruct || argT->ty == Tclass); | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
108 paramvec.push_back(getPtrToType(at)); |
100 | 109 } |
110 else { | |
131 | 111 if ((arg->storageClass & STCref) || (arg->storageClass & STCout)) { |
112 Logger::println("by ref param"); | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
113 at = getPtrToType(at); |
100 | 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 | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
149 const llvm::PointerType* i8pty = getPtrToType(llvm::Type::Int8Ty); |
100 | 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())) |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
197 thisty = getPtrToType(thisty); |
100 | 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()) { | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
205 thisty = getPtrToType(llvm::Type::Int8Ty); |
100 | 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()); | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
235 fdecl->irFunc->func = func; |
100 | 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 |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
247 // is imported and we don't have access? |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
248 if (fdecl->getModule() != gIR->dmodule) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
249 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
250 if (fdecl->prot() == PROTprivate) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
251 return; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
252 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
253 |
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
|
254 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
|
255 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
|
256 |
132 | 257 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
|
258 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
|
259 |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
260 if (fdecl->runTimeHack) { |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
261 gIR->declareList.push_back(fdecl); |
127 | 262 TypeFunction* tf = (TypeFunction*)fdecl->type; |
263 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
|
264 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
|
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 |
027b8d8b71ec
[svn 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 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
|
268 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
|
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 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
|
271 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
|
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 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
|
274 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
|
275 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
|
276 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
|
277 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
|
278 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
|
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 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
|
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 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
|
283 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
|
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 } |
027b8d8b71ec
[svn 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 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
|
288 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
289 // queue declaration |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
290 if (!fdecl->isAbstract()) |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
291 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
|
292 } |
027b8d8b71ec
[svn 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 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
294 ////////////////////////////////////////////////////////////////////////////////////////// |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
295 |
100 | 296 void DtoDeclareFunction(FuncDeclaration* fdecl) |
297 { | |
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
|
298 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
|
299 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
|
300 |
132 | 301 Logger::println("DtoDeclareFunction(%s): %s", fdecl->toPrettyChars(), fdecl->loc.toChars()); |
100 | 302 LOG_SCOPE; |
303 | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
304 assert(!fdecl->isAbstract()); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
109
diff
changeset
|
305 |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
306 // intrinsic sanity check |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
307 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
|
308 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
|
309 fatal(); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
310 } |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
311 |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
312 if (fdecl->runTimeHack) { |
100 | 313 Logger::println("runtime hack func chars: %s", fdecl->toChars()); |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
314 if (!fdecl->irFunc) { |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
315 fdecl->irFunc = new IrFunction(fdecl); |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
316 fdecl->irFunc->func = LLVM_D_GetRuntimeFunction(gIR->module, fdecl->toChars()); |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
317 } |
100 | 318 return; |
319 } | |
320 | |
321 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
|
322 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
|
323 if (!templInst && fdecl->getModule() != gIR->dmodule) |
144
a27941d00351
[svn r149] fixed: a bunch of D-style variadics problems.
lindquist
parents:
139
diff
changeset
|
324 { |
a27941d00351
[svn r149] fixed: a bunch of D-style variadics problems.
lindquist
parents:
139
diff
changeset
|
325 Logger::println("not template instance, and not in this module. declare only!"); |
a27941d00351
[svn r149] fixed: a bunch of D-style variadics problems.
lindquist
parents:
139
diff
changeset
|
326 Logger::println("current module: %s", gIR->dmodule->ident->toChars()); |
a27941d00351
[svn r149] fixed: a bunch of D-style variadics problems.
lindquist
parents:
139
diff
changeset
|
327 Logger::println("func module: %s", fdecl->getModule()->ident->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
|
328 declareOnly = true; |
144
a27941d00351
[svn r149] fixed: a bunch of D-style variadics problems.
lindquist
parents:
139
diff
changeset
|
329 } |
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
|
330 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
|
331 declareOnly = true; |
100 | 332 |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
333 if (!fdecl->irFunc) { |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
334 fdecl->irFunc = new IrFunction(fdecl); |
100 | 335 } |
336 | |
337 // mangled name | |
338 char* mangled_name; | |
339 if (fdecl->llvmInternal == LLVMintrinsic) | |
340 mangled_name = fdecl->llvmInternal1; | |
341 else | |
342 mangled_name = fdecl->mangle(); | |
343 | |
344 llvm::Function* vafunc = 0; | |
345 if ((fdecl->llvmInternal == LLVMva_start) || (fdecl->llvmInternal == LLVMva_intrinsic)) { | |
346 vafunc = DtoDeclareVaFunction(fdecl); | |
347 } | |
348 | |
349 Type* t = DtoDType(fdecl->type); | |
350 TypeFunction* f = (TypeFunction*)t; | |
351 | |
352 // construct function | |
353 const llvm::FunctionType* functype = DtoFunctionType(fdecl); | |
354 llvm::Function* func = vafunc ? vafunc : gIR->module->getFunction(mangled_name); | |
355 if (!func) | |
149
4c577c2b7229
[svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents:
144
diff
changeset
|
356 func = new llvm::Function(functype, DtoLinkage(fdecl), mangled_name, gIR->module); |
100 | 357 else |
358 assert(func->getFunctionType() == functype); | |
359 | |
360 // add func to IRFunc | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
361 fdecl->irFunc->func = func; |
100 | 362 |
363 // calling convention | |
364 if (!vafunc && fdecl->llvmInternal != LLVMintrinsic) | |
365 func->setCallingConv(DtoCallingConv(f->linkage)); | |
149
4c577c2b7229
[svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents:
144
diff
changeset
|
366 else // fall back to C, it should be the right thing to do |
132 | 367 func->setCallingConv(llvm::CallingConv::C); |
368 | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
369 fdecl->irFunc->func = func; |
100 | 370 assert(llvm::isa<llvm::FunctionType>(f->llvmType->get())); |
371 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
372 // main |
100 | 373 if (fdecl->isMain()) { |
374 gIR->mainFunc = func; | |
375 } | |
376 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
377 // static ctor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
378 if (fdecl->isStaticCtorDeclaration()) { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
379 gIR->ctors.push_back(fdecl); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
380 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
381 // static dtor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
382 else if (fdecl->isStaticDtorDeclaration()) { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
383 gIR->dtors.push_back(fdecl); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
384 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
385 |
100 | 386 // name parameters |
387 llvm::Function::arg_iterator iarg = func->arg_begin(); | |
388 int k = 0; | |
389 if (f->llvmRetInPtr) { | |
390 iarg->setName("retval"); | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
391 fdecl->irFunc->retArg = iarg; |
100 | 392 ++iarg; |
393 } | |
394 if (f->llvmUsesThis) { | |
395 iarg->setName("this"); | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
396 fdecl->irFunc->thisVar = iarg; |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
397 assert(fdecl->irFunc->thisVar); |
100 | 398 ++iarg; |
399 } | |
119
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
400 |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
401 if (f->linkage == LINKd && f->varargs == 1) { |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
402 iarg->setName("_arguments"); |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
403 fdecl->irFunc->_arguments = iarg; |
119
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
404 ++iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
405 iarg->setName("_argptr"); |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
406 fdecl->irFunc->_argptr = iarg; |
119
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
407 ++iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
408 } |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
409 |
100 | 410 for (; iarg != func->arg_end(); ++iarg) |
411 { | |
412 Argument* arg = Argument::getNth(f->parameters, k++); | |
413 //arg->llvmValue = iarg; | |
414 //Logger::println("identifier: '%s' %p\n", arg->ident->toChars(), arg->ident); | |
415 if (arg && arg->ident != 0) { | |
416 if (arg->vardecl) { | |
153
2c447715c047
[svn r159] added some code to help diagnose ticket #35
lindquist
parents:
149
diff
changeset
|
417 if (arg->vardecl->irLocal) |
2c447715c047
[svn r159] added some code to help diagnose ticket #35
lindquist
parents:
149
diff
changeset
|
418 { |
2c447715c047
[svn r159] added some code to help diagnose ticket #35
lindquist
parents:
149
diff
changeset
|
419 Logger::cout() << "WTF!?!: " << *arg->vardecl->irLocal->value << '\n'; |
2c447715c047
[svn r159] added some code to help diagnose ticket #35
lindquist
parents:
149
diff
changeset
|
420 } |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
421 assert(!arg->vardecl->irLocal); |
153
2c447715c047
[svn r159] added some code to help diagnose ticket #35
lindquist
parents:
149
diff
changeset
|
422 assert(!arg->vardecl->irGlobal); |
2c447715c047
[svn r159] added some code to help diagnose ticket #35
lindquist
parents:
149
diff
changeset
|
423 assert(!arg->vardecl->irField); |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
424 arg->vardecl->irLocal = new IrLocal(arg->vardecl); |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
425 arg->vardecl->irLocal->value = iarg; |
100 | 426 } |
427 iarg->setName(arg->ident->toChars()); | |
428 } | |
429 else { | |
430 iarg->setName("unnamed"); | |
431 } | |
432 } | |
433 | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
434 if (fdecl->isUnitTestDeclaration()) |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
435 gIR->unitTests.push_back(fdecl); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
436 |
100 | 437 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
|
438 gIR->defineList.push_back(fdecl); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
439 else |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
440 assert(func->getLinkage() != llvm::GlobalValue::InternalLinkage); |
100 | 441 |
442 Logger::cout() << "func decl: " << *func << '\n'; | |
443 } | |
444 | |
445 ////////////////////////////////////////////////////////////////////////////////////////// | |
446 | |
447 void DtoDefineFunc(FuncDeclaration* fd) | |
448 { | |
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
|
449 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
|
450 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
|
451 |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
452 assert(fd->llvmDeclared); |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
453 |
132 | 454 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
|
455 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
|
456 |
100 | 457 // debug info |
458 if (global.params.symdebug) { | |
459 Module* mo = fd->getModule(); | |
139
0ab29b838084
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
lindquist
parents:
137
diff
changeset
|
460 fd->irFunc->dwarfSubProg = DtoDwarfSubProgram(fd, DtoDwarfCompileUnit(mo)); |
100 | 461 } |
462 | |
463 Type* t = DtoDType(fd->type); | |
464 TypeFunction* f = (TypeFunction*)t; | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
465 assert(f->llvmType); |
100 | 466 |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
467 llvm::Function* func = fd->irFunc->func; |
100 | 468 const llvm::FunctionType* functype = func->getFunctionType(); |
469 | |
470 // only members of the current module or template instances maybe be defined | |
471 if (fd->getModule() == gIR->dmodule || DtoIsTemplateInstance(fd->parent)) | |
472 { | |
473 fd->llvmDModule = gIR->dmodule; | |
474 | |
475 // function definition | |
476 if (fd->fbody != 0) | |
477 { | |
478 Logger::println("Doing function body for: %s", fd->toChars()); | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
479 assert(fd->irFunc); |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
480 gIR->functions.push_back(fd->irFunc); |
100 | 481 |
482 if (fd->isMain()) | |
483 gIR->emitMain = true; | |
484 | |
485 llvm::BasicBlock* beginbb = new llvm::BasicBlock("entry",func); | |
486 llvm::BasicBlock* endbb = new llvm::BasicBlock("endentry",func); | |
487 | |
488 //assert(gIR->scopes.empty()); | |
489 gIR->scopes.push_back(IRScope(beginbb, endbb)); | |
490 | |
491 // create alloca point | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
492 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
|
493 gIR->func()->allocapoint = allocaPoint; |
100 | 494 |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
495 // need result variable? (not nested) |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
496 if (fd->vresult && !fd->vresult->nestedref) { |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
497 Logger::println("non-nested vresult value"); |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
498 fd->vresult->irLocal = new IrLocal(fd->vresult); |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
499 fd->vresult->irLocal->value = 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
|
500 } |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
501 |
100 | 502 // give arguments storage |
503 size_t n = Argument::dim(f->parameters); | |
504 for (int i=0; i < n; ++i) { | |
505 Argument* arg = Argument::getNth(f->parameters, i); | |
506 if (arg && arg->vardecl) { | |
507 VarDeclaration* vd = arg->vardecl; | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
508 if (!vd->needsStorage || vd->nestedref || vd->isRef() || vd->isOut() || DtoIsPassedByRef(vd->type)) |
100 | 509 continue; |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
510 llvm::Value* a = vd->irLocal->value; |
100 | 511 assert(a); |
512 std::string s(a->getName()); | |
513 Logger::println("giving argument '%s' storage", s.c_str()); | |
514 s.append("_storage"); | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
515 llvm::Value* v = new llvm::AllocaInst(a->getType(),s,allocaPoint); |
100 | 516 gIR->ir->CreateStore(a,v); |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
517 vd->irLocal->value = v; |
100 | 518 } |
519 else { | |
132 | 520 Logger::attention(fd->loc, "some unknown argument: %s", arg ? arg->toChars() : 0); |
100 | 521 } |
522 } | |
523 | |
524 // debug info | |
525 if (global.params.symdebug) DtoDwarfFuncStart(fd); | |
526 | |
527 llvm::Value* parentNested = NULL; | |
131 | 528 if (FuncDeclaration* fd2 = fd->toParent2()->isFuncDeclaration()) { |
529 if (!fd->isStatic()) // huh? | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
530 parentNested = fd2->irFunc->nestedVar; |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
531 } |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
532 |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
533 // need result variable? (nested) |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
534 if (fd->vresult && fd->vresult->nestedref) { |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
535 Logger::println("nested vresult value: %s", fd->vresult->toChars()); |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
536 fd->nestedVars.insert(fd->vresult); |
100 | 537 } |
538 | |
539 // construct nested variables struct | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
540 if (!fd->nestedVars.empty() || parentNested) { |
100 | 541 std::vector<const llvm::Type*> nestTypes; |
542 int j = 0; | |
543 if (parentNested) { | |
544 nestTypes.push_back(parentNested->getType()); | |
545 j++; | |
546 } | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
547 for (std::set<VarDeclaration*>::iterator i=fd->nestedVars.begin(); i!=fd->nestedVars.end(); ++i) { |
100 | 548 VarDeclaration* vd = *i; |
144
a27941d00351
[svn r149] fixed: a bunch of D-style variadics problems.
lindquist
parents:
139
diff
changeset
|
549 Logger::println("referenced nested variable %s", vd->toChars()); |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
550 if (!vd->irLocal) |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
551 vd->irLocal = new IrLocal(vd); |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
552 vd->irLocal->nestedIndex = j++; |
100 | 553 if (vd->isParameter()) { |
144
a27941d00351
[svn r149] fixed: a bunch of D-style variadics problems.
lindquist
parents:
139
diff
changeset
|
554 if (!vd->irLocal->value) { |
a27941d00351
[svn r149] fixed: a bunch of D-style variadics problems.
lindquist
parents:
139
diff
changeset
|
555 assert(vd == fd->vthis); |
a27941d00351
[svn r149] fixed: a bunch of D-style variadics problems.
lindquist
parents:
139
diff
changeset
|
556 vd->irLocal->value = fd->irFunc->thisVar; |
a27941d00351
[svn r149] fixed: a bunch of D-style variadics problems.
lindquist
parents:
139
diff
changeset
|
557 } |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
558 assert(vd->irLocal->value); |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
559 nestTypes.push_back(vd->irLocal->value->getType()); |
100 | 560 } |
561 else { | |
562 nestTypes.push_back(DtoType(vd->type)); | |
563 } | |
564 } | |
565 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
|
566 Logger::cout() << "nested var struct has type:" << *nestSType << '\n'; |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
567 fd->irFunc->nestedVar = new llvm::AllocaInst(nestSType,"nestedvars",allocaPoint); |
100 | 568 if (parentNested) { |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
569 assert(fd->irFunc->thisVar); |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
570 llvm::Value* ptr = gIR->ir->CreateBitCast(fd->irFunc->thisVar, parentNested->getType(), "tmp"); |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
571 gIR->ir->CreateStore(ptr, DtoGEPi(fd->irFunc->nestedVar, 0,0, "tmp")); |
100 | 572 } |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
573 for (std::set<VarDeclaration*>::iterator i=fd->nestedVars.begin(); i!=fd->nestedVars.end(); ++i) { |
100 | 574 VarDeclaration* vd = *i; |
575 if (vd->isParameter()) { | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
576 assert(vd->irLocal); |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
577 gIR->ir->CreateStore(vd->irLocal->value, DtoGEPi(fd->irFunc->nestedVar, 0, vd->irLocal->nestedIndex, "tmp")); |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
578 vd->irLocal->value = fd->irFunc->nestedVar; |
100 | 579 } |
580 } | |
581 } | |
582 | |
583 // copy _argptr to a memory location | |
584 if (f->linkage == LINKd && f->varargs == 1) | |
585 { | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
586 llvm::Value* argptrmem = new llvm::AllocaInst(fd->irFunc->_argptr->getType(), "_argptrmem", gIR->topallocapoint()); |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
587 new llvm::StoreInst(fd->irFunc->_argptr, argptrmem, gIR->scopebb()); |
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
588 fd->irFunc->_argptr = argptrmem; |
100 | 589 } |
590 | |
591 // output function body | |
592 fd->fbody->toIR(gIR); | |
593 | |
594 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
595 // in automatically, so we do it here. | |
596 if (!fd->isMain()) { | |
597 if (!gIR->scopereturned()) { | |
598 // pass the previous block into this block | |
599 if (global.params.symdebug) DtoDwarfFuncEnd(fd); | |
600 if (func->getReturnType() == llvm::Type::VoidTy) { | |
601 new llvm::ReturnInst(gIR->scopebb()); | |
602 } | |
603 else { | |
604 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
605 } | |
606 } | |
607 } | |
608 | |
609 // erase alloca point | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
610 allocaPoint->eraseFromParent(); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
611 allocaPoint = 0; |
100 | 612 gIR->func()->allocapoint = 0; |
613 | |
614 gIR->scopes.pop_back(); | |
615 | |
616 // get rid of the endentry block, it's never used | |
617 assert(!func->getBasicBlockList().empty()); | |
618 func->getBasicBlockList().pop_back(); | |
619 | |
620 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
621 // would be nice to figure out how to assert that this is correct | |
622 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); | |
623 if (lastbb->empty()) { | |
624 if (lastbb->getNumUses() == 0) | |
625 lastbb->eraseFromParent(); | |
626 else { | |
627 new llvm::UnreachableInst(lastbb); | |
628 /*if (func->getReturnType() == llvm::Type::VoidTy) { | |
629 new llvm::ReturnInst(lastbb); | |
630 } | |
631 else { | |
632 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), lastbb); | |
633 }*/ | |
634 } | |
635 } | |
636 | |
637 gIR->functions.pop_back(); | |
638 } | |
639 } | |
640 } | |
641 | |
642 ////////////////////////////////////////////////////////////////////////////////////////// | |
643 | |
644 void DtoMain() | |
645 { | |
646 // emit main function llvm style | |
647 // int main(int argc, char**argv, char**env); | |
648 | |
649 assert(gIR != 0); | |
650 IRState& ir = *gIR; | |
651 | |
652 assert(ir.emitMain && ir.mainFunc); | |
653 | |
654 // parameter types | |
655 std::vector<const llvm::Type*> pvec; | |
656 pvec.push_back((const llvm::Type*)llvm::Type::Int32Ty); | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
657 const llvm::Type* chPtrType = (const llvm::Type*)getPtrToType(llvm::Type::Int8Ty); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
658 pvec.push_back((const llvm::Type*)getPtrToType(chPtrType)); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
659 pvec.push_back((const llvm::Type*)getPtrToType(chPtrType)); |
100 | 660 const llvm::Type* rettype = (const llvm::Type*)llvm::Type::Int32Ty; |
661 | |
662 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false); | |
663 llvm::Function* func = new llvm::Function(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module); | |
664 | |
665 llvm::BasicBlock* bb = new llvm::BasicBlock("entry",func); | |
666 | |
667 // call static ctors | |
117 | 668 llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleCtor"); |
100 | 669 llvm::Instruction* apt = new llvm::CallInst(fn,"",bb); |
670 | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
671 // 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
|
672 if (global.params.useUnitTests) { |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
673 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
|
674 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
|
675 } |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
676 |
100 | 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 | |
117 | 713 fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleDtor"); |
100 | 714 new llvm::CallInst(fn,"",bb); |
715 | |
716 // return | |
717 new llvm::ReturnInst(call,bb); | |
718 } | |
719 | |
720 ////////////////////////////////////////////////////////////////////////////////////////// | |
117 | 721 |
722 const llvm::FunctionType* DtoBaseFunctionType(FuncDeclaration* fdecl) | |
723 { | |
724 Dsymbol* parent = fdecl->toParent(); | |
725 ClassDeclaration* cd = parent->isClassDeclaration(); | |
726 assert(cd); | |
727 | |
728 FuncDeclaration* f = fdecl; | |
729 | |
730 while (cd) | |
731 { | |
732 ClassDeclaration* base = cd->baseClass; | |
733 if (!base) | |
734 break; | |
735 FuncDeclaration* f2 = base->findFunc(fdecl->ident, (TypeFunction*)fdecl->type); | |
736 if (f2) { | |
737 f = f2; | |
738 cd = base; | |
739 } | |
740 else | |
741 break; | |
742 } | |
743 | |
744 DtoResolveDsymbol(f); | |
745 return llvm::cast<llvm::FunctionType>(DtoType(f->type)); | |
746 } | |
747 | |
748 ////////////////////////////////////////////////////////////////////////////////////////// | |
131 | 749 |
750 DValue* DtoArgument(Argument* fnarg, Expression* argexp) | |
751 { | |
752 Logger::println("DtoArgument"); | |
753 LOG_SCOPE; | |
754 | |
755 DValue* arg = argexp->toElem(gIR); | |
756 | |
757 // ref/out arg | |
758 if (fnarg && ((fnarg->storageClass & STCref) || (fnarg->storageClass & STCout))) | |
759 { | |
760 if (arg->isVar() || arg->isLRValue()) | |
761 arg = new DImValue(argexp->type, arg->getLVal(), false); | |
762 else | |
763 arg = new DImValue(argexp->type, arg->getRVal(), false); | |
764 } | |
765 // aggregate arg | |
766 else if (DtoIsPassedByRef(argexp->type)) | |
767 { | |
768 llvm::Value* alloc = new llvm::AllocaInst(DtoType(argexp->type), "tmpparam", gIR->topallocapoint()); | |
769 DVarValue* vv = new DVarValue(argexp->type, alloc, true); | |
770 DtoAssign(vv, arg); | |
771 arg = vv; | |
772 } | |
773 // normal arg (basic/value type) | |
774 else | |
775 { | |
776 // nothing to do | |
777 } | |
778 | |
779 return arg; | |
780 } | |
781 | |
782 ////////////////////////////////////////////////////////////////////////////////////////// | |
783 | |
784 void DtoVariadicArgument(Expression* argexp, llvm::Value* dst) | |
785 { | |
786 Logger::println("DtoVariadicArgument"); | |
787 LOG_SCOPE; | |
788 DVarValue* vv = new DVarValue(argexp->type, dst, true); | |
789 DtoAssign(vv, argexp->toElem(gIR)); | |
790 } | |
791 | |
792 ////////////////////////////////////////////////////////////////////////////////////////// |