Mercurial > projects > ldc
annotate gen/functions.cpp @ 139:0ab29b838084 trunk
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
Fixed: tango.io.Console seems to be working now.
author | lindquist |
---|---|
date | Tue, 22 Jan 2008 00:01:16 +0100 |
parents | ce7b81fb957f |
children | a27941d00351 |
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) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
324 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
|
325 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
|
326 declareOnly = true; |
100 | 327 |
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
|
328 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
|
329 fdecl->irFunc = new IrFunction(fdecl); |
100 | 330 } |
331 | |
332 // mangled name | |
333 char* mangled_name; | |
334 if (fdecl->llvmInternal == LLVMintrinsic) | |
335 mangled_name = fdecl->llvmInternal1; | |
336 else | |
337 mangled_name = fdecl->mangle(); | |
338 | |
339 llvm::Function* vafunc = 0; | |
340 if ((fdecl->llvmInternal == LLVMva_start) || (fdecl->llvmInternal == LLVMva_intrinsic)) { | |
341 vafunc = DtoDeclareVaFunction(fdecl); | |
342 } | |
343 | |
344 Type* t = DtoDType(fdecl->type); | |
345 TypeFunction* f = (TypeFunction*)t; | |
346 | |
347 // construct function | |
348 const llvm::FunctionType* functype = DtoFunctionType(fdecl); | |
349 llvm::Function* func = vafunc ? vafunc : gIR->module->getFunction(mangled_name); | |
350 if (!func) | |
351 func = new llvm::Function(functype, DtoLinkage(fdecl->protection, fdecl->storage_class), mangled_name, gIR->module); | |
352 else | |
353 assert(func->getFunctionType() == functype); | |
354 | |
355 // 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
|
356 fdecl->irFunc->func = func; |
100 | 357 |
358 // calling convention | |
359 if (!vafunc && fdecl->llvmInternal != LLVMintrinsic) | |
360 func->setCallingConv(DtoCallingConv(f->linkage)); | |
361 | |
362 // template instances should have weak linkage | |
363 if (!vafunc && fdecl->llvmInternal != LLVMintrinsic && fdecl->parent && DtoIsTemplateInstance(fdecl->parent)) | |
364 func->setLinkage(llvm::GlobalValue::WeakLinkage); | |
365 | |
132 | 366 // extern(C) functions are always external |
367 if (f->linkage == LINKc) | |
368 func->setLinkage(llvm::GlobalValue::ExternalLinkage); | |
369 | |
370 // intrinsics are always external C | |
371 if (fdecl->llvmInternal == LLVMintrinsic) | |
372 { | |
373 func->setLinkage(llvm::GlobalValue::ExternalLinkage); | |
374 func->setCallingConv(llvm::CallingConv::C); | |
375 } | |
376 | |
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
|
377 fdecl->irFunc->func = func; |
100 | 378 assert(llvm::isa<llvm::FunctionType>(f->llvmType->get())); |
379 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
380 // main |
100 | 381 if (fdecl->isMain()) { |
382 gIR->mainFunc = func; | |
383 } | |
384 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
385 // static ctor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
386 if (fdecl->isStaticCtorDeclaration()) { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
387 gIR->ctors.push_back(fdecl); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
388 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
389 // static dtor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
390 else if (fdecl->isStaticDtorDeclaration()) { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
391 gIR->dtors.push_back(fdecl); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
392 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
113
diff
changeset
|
393 |
100 | 394 // name parameters |
395 llvm::Function::arg_iterator iarg = func->arg_begin(); | |
396 int k = 0; | |
397 if (f->llvmRetInPtr) { | |
398 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
|
399 fdecl->irFunc->retArg = iarg; |
100 | 400 ++iarg; |
401 } | |
402 if (f->llvmUsesThis) { | |
403 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
|
404 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
|
405 assert(fdecl->irFunc->thisVar); |
100 | 406 ++iarg; |
407 } | |
119
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 if (f->linkage == LINKd && f->varargs == 1) { |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
410 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
|
411 fdecl->irFunc->_arguments = iarg; |
119
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
412 ++iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
413 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
|
414 fdecl->irFunc->_argptr = iarg; |
119
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
415 ++iarg; |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
416 } |
79c9ac745fbc
[svn r123] Fixed some typeinfo module name mismatches.
lindquist
parents:
117
diff
changeset
|
417 |
100 | 418 for (; iarg != func->arg_end(); ++iarg) |
419 { | |
420 Argument* arg = Argument::getNth(f->parameters, k++); | |
421 //arg->llvmValue = iarg; | |
422 //Logger::println("identifier: '%s' %p\n", arg->ident->toChars(), arg->ident); | |
423 if (arg && arg->ident != 0) { | |
424 if (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
|
425 assert(!arg->vardecl->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
|
426 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
|
427 arg->vardecl->irLocal->value = iarg; |
100 | 428 } |
429 iarg->setName(arg->ident->toChars()); | |
430 } | |
431 else { | |
432 iarg->setName("unnamed"); | |
433 } | |
434 } | |
435 | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
436 if (fdecl->isUnitTestDeclaration()) |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
437 gIR->unitTests.push_back(fdecl); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
438 |
100 | 439 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
|
440 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
|
441 else |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
442 assert(func->getLinkage() != llvm::GlobalValue::InternalLinkage); |
100 | 443 |
444 Logger::cout() << "func decl: " << *func << '\n'; | |
445 } | |
446 | |
447 ////////////////////////////////////////////////////////////////////////////////////////// | |
448 | |
449 void DtoDefineFunc(FuncDeclaration* fd) | |
450 { | |
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
|
451 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
|
452 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
|
453 |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
454 assert(fd->llvmDeclared); |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
455 |
132 | 456 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
|
457 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
|
458 |
100 | 459 // debug info |
460 if (global.params.symdebug) { | |
461 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
|
462 fd->irFunc->dwarfSubProg = DtoDwarfSubProgram(fd, DtoDwarfCompileUnit(mo)); |
100 | 463 } |
464 | |
465 Type* t = DtoDType(fd->type); | |
466 TypeFunction* f = (TypeFunction*)t; | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
467 assert(f->llvmType); |
100 | 468 |
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
|
469 llvm::Function* func = fd->irFunc->func; |
100 | 470 const llvm::FunctionType* functype = func->getFunctionType(); |
471 | |
472 // only members of the current module or template instances maybe be defined | |
473 if (fd->getModule() == gIR->dmodule || DtoIsTemplateInstance(fd->parent)) | |
474 { | |
475 fd->llvmDModule = gIR->dmodule; | |
476 | |
477 // function definition | |
478 if (fd->fbody != 0) | |
479 { | |
480 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
|
481 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
|
482 gIR->functions.push_back(fd->irFunc); |
100 | 483 |
484 if (fd->isMain()) | |
485 gIR->emitMain = true; | |
486 | |
487 llvm::BasicBlock* beginbb = new llvm::BasicBlock("entry",func); | |
488 llvm::BasicBlock* endbb = new llvm::BasicBlock("endentry",func); | |
489 | |
490 //assert(gIR->scopes.empty()); | |
491 gIR->scopes.push_back(IRScope(beginbb, endbb)); | |
492 | |
493 // create alloca point | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
494 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
|
495 gIR->func()->allocapoint = allocaPoint; |
100 | 496 |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
497 // need result variable? (not nested) |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
498 if (fd->vresult && !fd->vresult->nestedref) { |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
499 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
|
500 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
|
501 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
|
502 } |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
503 |
100 | 504 // give arguments storage |
505 size_t n = Argument::dim(f->parameters); | |
506 for (int i=0; i < n; ++i) { | |
507 Argument* arg = Argument::getNth(f->parameters, i); | |
508 if (arg && arg->vardecl) { | |
509 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
|
510 if (!vd->needsStorage || vd->nestedref || vd->isRef() || vd->isOut() || DtoIsPassedByRef(vd->type)) |
100 | 511 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
|
512 llvm::Value* a = vd->irLocal->value; |
100 | 513 assert(a); |
514 std::string s(a->getName()); | |
515 Logger::println("giving argument '%s' storage", s.c_str()); | |
516 s.append("_storage"); | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
517 llvm::Value* v = new llvm::AllocaInst(a->getType(),s,allocaPoint); |
100 | 518 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
|
519 vd->irLocal->value = v; |
100 | 520 } |
521 else { | |
132 | 522 Logger::attention(fd->loc, "some unknown argument: %s", arg ? arg->toChars() : 0); |
100 | 523 } |
524 } | |
525 | |
526 // debug info | |
527 if (global.params.symdebug) DtoDwarfFuncStart(fd); | |
528 | |
529 llvm::Value* parentNested = NULL; | |
131 | 530 if (FuncDeclaration* fd2 = fd->toParent2()->isFuncDeclaration()) { |
531 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
|
532 parentNested = fd2->irFunc->nestedVar; |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
533 } |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
534 |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
535 // need result variable? (nested) |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
536 if (fd->vresult && fd->vresult->nestedref) { |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
537 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
|
538 fd->nestedVars.insert(fd->vresult); |
100 | 539 } |
540 | |
541 // 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
|
542 if (!fd->nestedVars.empty() || parentNested) { |
100 | 543 std::vector<const llvm::Type*> nestTypes; |
544 int j = 0; | |
545 if (parentNested) { | |
546 nestTypes.push_back(parentNested->getType()); | |
547 j++; | |
548 } | |
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
|
549 for (std::set<VarDeclaration*>::iterator i=fd->nestedVars.begin(); i!=fd->nestedVars.end(); ++i) { |
100 | 550 VarDeclaration* vd = *i; |
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
|
551 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
|
552 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
|
553 vd->irLocal->nestedIndex = j++; |
100 | 554 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
|
555 |
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
|
556 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
|
557 nestTypes.push_back(vd->irLocal->value->getType()); |
100 | 558 } |
559 else { | |
560 nestTypes.push_back(DtoType(vd->type)); | |
561 } | |
562 } | |
563 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
|
564 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
|
565 fd->irFunc->nestedVar = new llvm::AllocaInst(nestSType,"nestedvars",allocaPoint); |
100 | 566 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
|
567 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
|
568 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
|
569 gIR->ir->CreateStore(ptr, DtoGEPi(fd->irFunc->nestedVar, 0,0, "tmp")); |
100 | 570 } |
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
|
571 for (std::set<VarDeclaration*>::iterator i=fd->nestedVars.begin(); i!=fd->nestedVars.end(); ++i) { |
100 | 572 VarDeclaration* vd = *i; |
573 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
|
574 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
|
575 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
|
576 vd->irLocal->value = fd->irFunc->nestedVar; |
100 | 577 } |
578 } | |
579 } | |
580 | |
581 // copy _argptr to a memory location | |
582 if (f->linkage == LINKd && f->varargs == 1) | |
583 { | |
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
|
584 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
|
585 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
|
586 fd->irFunc->_argptr = argptrmem; |
100 | 587 } |
588 | |
589 // output function body | |
590 fd->fbody->toIR(gIR); | |
591 | |
592 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
593 // in automatically, so we do it here. | |
594 if (!fd->isMain()) { | |
595 if (!gIR->scopereturned()) { | |
596 // pass the previous block into this block | |
597 if (global.params.symdebug) DtoDwarfFuncEnd(fd); | |
598 if (func->getReturnType() == llvm::Type::VoidTy) { | |
599 new llvm::ReturnInst(gIR->scopebb()); | |
600 } | |
601 else { | |
602 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
603 } | |
604 } | |
605 } | |
606 | |
607 // erase alloca point | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
608 allocaPoint->eraseFromParent(); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
609 allocaPoint = 0; |
100 | 610 gIR->func()->allocapoint = 0; |
611 | |
612 gIR->scopes.pop_back(); | |
613 | |
614 // get rid of the endentry block, it's never used | |
615 assert(!func->getBasicBlockList().empty()); | |
616 func->getBasicBlockList().pop_back(); | |
617 | |
618 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
619 // would be nice to figure out how to assert that this is correct | |
620 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); | |
621 if (lastbb->empty()) { | |
622 if (lastbb->getNumUses() == 0) | |
623 lastbb->eraseFromParent(); | |
624 else { | |
625 new llvm::UnreachableInst(lastbb); | |
626 /*if (func->getReturnType() == llvm::Type::VoidTy) { | |
627 new llvm::ReturnInst(lastbb); | |
628 } | |
629 else { | |
630 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), lastbb); | |
631 }*/ | |
632 } | |
633 } | |
634 | |
635 gIR->functions.pop_back(); | |
636 } | |
637 } | |
638 } | |
639 | |
640 ////////////////////////////////////////////////////////////////////////////////////////// | |
641 | |
642 void DtoMain() | |
643 { | |
644 // emit main function llvm style | |
645 // int main(int argc, char**argv, char**env); | |
646 | |
647 assert(gIR != 0); | |
648 IRState& ir = *gIR; | |
649 | |
650 assert(ir.emitMain && ir.mainFunc); | |
651 | |
652 // parameter types | |
653 std::vector<const llvm::Type*> pvec; | |
654 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
|
655 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
|
656 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
|
657 pvec.push_back((const llvm::Type*)getPtrToType(chPtrType)); |
100 | 658 const llvm::Type* rettype = (const llvm::Type*)llvm::Type::Int32Ty; |
659 | |
660 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false); | |
661 llvm::Function* func = new llvm::Function(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module); | |
662 | |
663 llvm::BasicBlock* bb = new llvm::BasicBlock("entry",func); | |
664 | |
665 // call static ctors | |
117 | 666 llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleCtor"); |
100 | 667 llvm::Instruction* apt = new llvm::CallInst(fn,"",bb); |
668 | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
669 // 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
|
670 if (global.params.useUnitTests) { |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
671 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
|
672 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
|
673 } |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
674 |
100 | 675 // call user main function |
676 const llvm::FunctionType* mainty = ir.mainFunc->getFunctionType(); | |
677 llvm::CallInst* call; | |
678 if (mainty->getNumParams() > 0) | |
679 { | |
680 // main with arguments | |
681 assert(mainty->getNumParams() == 1); | |
682 std::vector<llvm::Value*> args; | |
683 llvm::Function* mfn = LLVM_D_GetRuntimeFunction(ir.module,"_d_main_args"); | |
684 | |
685 llvm::Function::arg_iterator argi = func->arg_begin(); | |
686 args.push_back(argi++); | |
687 args.push_back(argi++); | |
688 | |
689 const llvm::Type* at = mainty->getParamType(0)->getContainedType(0); | |
690 llvm::Value* arr = new llvm::AllocaInst(at->getContainedType(1)->getContainedType(0), func->arg_begin(), "argstorage", apt); | |
691 llvm::Value* a = new llvm::AllocaInst(at, "argarray", apt); | |
692 llvm::Value* ptr = DtoGEPi(a,0,0,"tmp",bb); | |
693 llvm::Value* v = args[0]; | |
694 if (v->getType() != DtoSize_t()) | |
695 v = new llvm::ZExtInst(v, DtoSize_t(), "tmp", bb); | |
696 new llvm::StoreInst(v,ptr,bb); | |
697 ptr = DtoGEPi(a,0,1,"tmp",bb); | |
698 new llvm::StoreInst(arr,ptr,bb); | |
699 args.push_back(a); | |
700 new llvm::CallInst(mfn, args.begin(), args.end(), "", bb); | |
701 call = new llvm::CallInst(ir.mainFunc,a,"ret",bb); | |
702 } | |
703 else | |
704 { | |
705 // main with no arguments | |
706 call = new llvm::CallInst(ir.mainFunc,"ret",bb); | |
707 } | |
708 call->setCallingConv(ir.mainFunc->getCallingConv()); | |
709 | |
710 // call static dtors | |
117 | 711 fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleDtor"); |
100 | 712 new llvm::CallInst(fn,"",bb); |
713 | |
714 // return | |
715 new llvm::ReturnInst(call,bb); | |
716 } | |
717 | |
718 ////////////////////////////////////////////////////////////////////////////////////////// | |
117 | 719 |
720 const llvm::FunctionType* DtoBaseFunctionType(FuncDeclaration* fdecl) | |
721 { | |
722 Dsymbol* parent = fdecl->toParent(); | |
723 ClassDeclaration* cd = parent->isClassDeclaration(); | |
724 assert(cd); | |
725 | |
726 FuncDeclaration* f = fdecl; | |
727 | |
728 while (cd) | |
729 { | |
730 ClassDeclaration* base = cd->baseClass; | |
731 if (!base) | |
732 break; | |
733 FuncDeclaration* f2 = base->findFunc(fdecl->ident, (TypeFunction*)fdecl->type); | |
734 if (f2) { | |
735 f = f2; | |
736 cd = base; | |
737 } | |
738 else | |
739 break; | |
740 } | |
741 | |
742 DtoResolveDsymbol(f); | |
743 return llvm::cast<llvm::FunctionType>(DtoType(f->type)); | |
744 } | |
745 | |
746 ////////////////////////////////////////////////////////////////////////////////////////// | |
131 | 747 |
748 DValue* DtoArgument(Argument* fnarg, Expression* argexp) | |
749 { | |
750 Logger::println("DtoArgument"); | |
751 LOG_SCOPE; | |
752 | |
753 DValue* arg = argexp->toElem(gIR); | |
754 | |
755 // ref/out arg | |
756 if (fnarg && ((fnarg->storageClass & STCref) || (fnarg->storageClass & STCout))) | |
757 { | |
758 if (arg->isVar() || arg->isLRValue()) | |
759 arg = new DImValue(argexp->type, arg->getLVal(), false); | |
760 else | |
761 arg = new DImValue(argexp->type, arg->getRVal(), false); | |
762 } | |
763 // aggregate arg | |
764 else if (DtoIsPassedByRef(argexp->type)) | |
765 { | |
766 llvm::Value* alloc = new llvm::AllocaInst(DtoType(argexp->type), "tmpparam", gIR->topallocapoint()); | |
767 DVarValue* vv = new DVarValue(argexp->type, alloc, true); | |
768 DtoAssign(vv, arg); | |
769 arg = vv; | |
770 } | |
771 // normal arg (basic/value type) | |
772 else | |
773 { | |
774 // nothing to do | |
775 } | |
776 | |
777 return arg; | |
778 } | |
779 | |
780 ////////////////////////////////////////////////////////////////////////////////////////// | |
781 | |
782 void DtoVariadicArgument(Expression* argexp, llvm::Value* dst) | |
783 { | |
784 Logger::println("DtoVariadicArgument"); | |
785 LOG_SCOPE; | |
786 DVarValue* vv = new DVarValue(argexp->type, dst, true); | |
787 DtoAssign(vv, argexp->toElem(gIR)); | |
788 } | |
789 | |
790 ////////////////////////////////////////////////////////////////////////////////////////// |