Mercurial > projects > ldc
annotate gen/functions.cpp @ 136:0e28624814e8 trunk
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
author | lindquist |
---|---|
date | Thu, 17 Jan 2008 03:15:12 +0100 |
parents | 44a95ac7368a |
children | ce7b81fb957f |
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); |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
77 assert(ti->llvmConstInit); |
100 | 78 std::vector<const llvm::Type*> types; |
79 types.push_back(DtoSize_t()); | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
80 types.push_back(getPtrToType(getPtrToType(ti->llvmConstInit->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(); | |
462 if (!mo->llvmCompileUnit) { | |
463 mo->llvmCompileUnit = DtoDwarfCompileUnit(mo,false); | |
464 } | |
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
|
465 fd->irFunc->dwarfSubProg = DtoDwarfSubProgram(fd, mo->llvmCompileUnit); |
100 | 466 } |
467 | |
468 Type* t = DtoDType(fd->type); | |
469 TypeFunction* f = (TypeFunction*)t; | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
470 assert(f->llvmType); |
100 | 471 |
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
|
472 llvm::Function* func = fd->irFunc->func; |
100 | 473 const llvm::FunctionType* functype = func->getFunctionType(); |
474 | |
475 // only members of the current module or template instances maybe be defined | |
476 if (fd->getModule() == gIR->dmodule || DtoIsTemplateInstance(fd->parent)) | |
477 { | |
478 fd->llvmDModule = gIR->dmodule; | |
479 | |
480 // function definition | |
481 if (fd->fbody != 0) | |
482 { | |
483 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
|
484 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
|
485 gIR->functions.push_back(fd->irFunc); |
100 | 486 |
487 if (fd->isMain()) | |
488 gIR->emitMain = true; | |
489 | |
490 llvm::BasicBlock* beginbb = new llvm::BasicBlock("entry",func); | |
491 llvm::BasicBlock* endbb = new llvm::BasicBlock("endentry",func); | |
492 | |
493 //assert(gIR->scopes.empty()); | |
494 gIR->scopes.push_back(IRScope(beginbb, endbb)); | |
495 | |
496 // create alloca point | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
497 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
|
498 gIR->func()->allocapoint = allocaPoint; |
100 | 499 |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
500 // need result variable? (not nested) |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
501 if (fd->vresult && !fd->vresult->nestedref) { |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
502 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
|
503 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
|
504 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
|
505 } |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
506 |
100 | 507 // give arguments storage |
508 size_t n = Argument::dim(f->parameters); | |
509 for (int i=0; i < n; ++i) { | |
510 Argument* arg = Argument::getNth(f->parameters, i); | |
511 if (arg && arg->vardecl) { | |
512 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
|
513 if (!vd->needsStorage || vd->nestedref || vd->isRef() || vd->isOut() || DtoIsPassedByRef(vd->type)) |
100 | 514 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
|
515 llvm::Value* a = vd->irLocal->value; |
100 | 516 assert(a); |
517 std::string s(a->getName()); | |
518 Logger::println("giving argument '%s' storage", s.c_str()); | |
519 s.append("_storage"); | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
520 llvm::Value* v = new llvm::AllocaInst(a->getType(),s,allocaPoint); |
100 | 521 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
|
522 vd->irLocal->value = v; |
100 | 523 } |
524 else { | |
132 | 525 Logger::attention(fd->loc, "some unknown argument: %s", arg ? arg->toChars() : 0); |
100 | 526 } |
527 } | |
528 | |
529 // debug info | |
530 if (global.params.symdebug) DtoDwarfFuncStart(fd); | |
531 | |
532 llvm::Value* parentNested = NULL; | |
131 | 533 if (FuncDeclaration* fd2 = fd->toParent2()->isFuncDeclaration()) { |
534 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
|
535 parentNested = fd2->irFunc->nestedVar; |
108
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
536 } |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
537 |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
538 // need result variable? (nested) |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
539 if (fd->vresult && fd->vresult->nestedref) { |
288fe1029e1f
[svn r112] Fixed 'case 1,2,3:' style case statements.
lindquist
parents:
102
diff
changeset
|
540 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
|
541 fd->nestedVars.insert(fd->vresult); |
100 | 542 } |
543 | |
544 // 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
|
545 if (!fd->nestedVars.empty() || parentNested) { |
100 | 546 std::vector<const llvm::Type*> nestTypes; |
547 int j = 0; | |
548 if (parentNested) { | |
549 nestTypes.push_back(parentNested->getType()); | |
550 j++; | |
551 } | |
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
|
552 for (std::set<VarDeclaration*>::iterator i=fd->nestedVars.begin(); i!=fd->nestedVars.end(); ++i) { |
100 | 553 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
|
554 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
|
555 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
|
556 vd->irLocal->nestedIndex = j++; |
100 | 557 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
|
558 |
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 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
|
560 nestTypes.push_back(vd->irLocal->value->getType()); |
100 | 561 } |
562 else { | |
563 nestTypes.push_back(DtoType(vd->type)); | |
564 } | |
565 } | |
566 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
|
567 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
|
568 fd->irFunc->nestedVar = new llvm::AllocaInst(nestSType,"nestedvars",allocaPoint); |
100 | 569 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
|
570 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
|
571 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
|
572 gIR->ir->CreateStore(ptr, DtoGEPi(fd->irFunc->nestedVar, 0,0, "tmp")); |
100 | 573 } |
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 for (std::set<VarDeclaration*>::iterator i=fd->nestedVars.begin(); i!=fd->nestedVars.end(); ++i) { |
100 | 575 VarDeclaration* vd = *i; |
576 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
|
577 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
|
578 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
|
579 vd->irLocal->value = fd->irFunc->nestedVar; |
100 | 580 } |
581 } | |
582 } | |
583 | |
584 // copy _argptr to a memory location | |
585 if (f->linkage == LINKd && f->varargs == 1) | |
586 { | |
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
|
587 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
|
588 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
|
589 fd->irFunc->_argptr = argptrmem; |
100 | 590 } |
591 | |
592 // output function body | |
593 fd->fbody->toIR(gIR); | |
594 | |
595 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
596 // in automatically, so we do it here. | |
597 if (!fd->isMain()) { | |
598 if (!gIR->scopereturned()) { | |
599 // pass the previous block into this block | |
600 if (global.params.symdebug) DtoDwarfFuncEnd(fd); | |
601 if (func->getReturnType() == llvm::Type::VoidTy) { | |
602 new llvm::ReturnInst(gIR->scopebb()); | |
603 } | |
604 else { | |
605 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
606 } | |
607 } | |
608 } | |
609 | |
610 // erase alloca point | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
611 allocaPoint->eraseFromParent(); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
612 allocaPoint = 0; |
100 | 613 gIR->func()->allocapoint = 0; |
614 | |
615 gIR->scopes.pop_back(); | |
616 | |
617 // get rid of the endentry block, it's never used | |
618 assert(!func->getBasicBlockList().empty()); | |
619 func->getBasicBlockList().pop_back(); | |
620 | |
621 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
622 // would be nice to figure out how to assert that this is correct | |
623 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); | |
624 if (lastbb->empty()) { | |
625 if (lastbb->getNumUses() == 0) | |
626 lastbb->eraseFromParent(); | |
627 else { | |
628 new llvm::UnreachableInst(lastbb); | |
629 /*if (func->getReturnType() == llvm::Type::VoidTy) { | |
630 new llvm::ReturnInst(lastbb); | |
631 } | |
632 else { | |
633 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), lastbb); | |
634 }*/ | |
635 } | |
636 } | |
637 | |
638 gIR->functions.pop_back(); | |
639 } | |
640 } | |
641 } | |
642 | |
643 ////////////////////////////////////////////////////////////////////////////////////////// | |
644 | |
645 void DtoMain() | |
646 { | |
647 // emit main function llvm style | |
648 // int main(int argc, char**argv, char**env); | |
649 | |
650 assert(gIR != 0); | |
651 IRState& ir = *gIR; | |
652 | |
653 assert(ir.emitMain && ir.mainFunc); | |
654 | |
655 // parameter types | |
656 std::vector<const llvm::Type*> pvec; | |
657 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
|
658 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
|
659 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
|
660 pvec.push_back((const llvm::Type*)getPtrToType(chPtrType)); |
100 | 661 const llvm::Type* rettype = (const llvm::Type*)llvm::Type::Int32Ty; |
662 | |
663 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false); | |
664 llvm::Function* func = new llvm::Function(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module); | |
665 | |
666 llvm::BasicBlock* bb = new llvm::BasicBlock("entry",func); | |
667 | |
668 // call static ctors | |
117 | 669 llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleCtor"); |
100 | 670 llvm::Instruction* apt = new llvm::CallInst(fn,"",bb); |
671 | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
672 // 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
|
673 if (global.params.useUnitTests) { |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
674 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
|
675 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
|
676 } |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
119
diff
changeset
|
677 |
100 | 678 // call user main function |
679 const llvm::FunctionType* mainty = ir.mainFunc->getFunctionType(); | |
680 llvm::CallInst* call; | |
681 if (mainty->getNumParams() > 0) | |
682 { | |
683 // main with arguments | |
684 assert(mainty->getNumParams() == 1); | |
685 std::vector<llvm::Value*> args; | |
686 llvm::Function* mfn = LLVM_D_GetRuntimeFunction(ir.module,"_d_main_args"); | |
687 | |
688 llvm::Function::arg_iterator argi = func->arg_begin(); | |
689 args.push_back(argi++); | |
690 args.push_back(argi++); | |
691 | |
692 const llvm::Type* at = mainty->getParamType(0)->getContainedType(0); | |
693 llvm::Value* arr = new llvm::AllocaInst(at->getContainedType(1)->getContainedType(0), func->arg_begin(), "argstorage", apt); | |
694 llvm::Value* a = new llvm::AllocaInst(at, "argarray", apt); | |
695 llvm::Value* ptr = DtoGEPi(a,0,0,"tmp",bb); | |
696 llvm::Value* v = args[0]; | |
697 if (v->getType() != DtoSize_t()) | |
698 v = new llvm::ZExtInst(v, DtoSize_t(), "tmp", bb); | |
699 new llvm::StoreInst(v,ptr,bb); | |
700 ptr = DtoGEPi(a,0,1,"tmp",bb); | |
701 new llvm::StoreInst(arr,ptr,bb); | |
702 args.push_back(a); | |
703 new llvm::CallInst(mfn, args.begin(), args.end(), "", bb); | |
704 call = new llvm::CallInst(ir.mainFunc,a,"ret",bb); | |
705 } | |
706 else | |
707 { | |
708 // main with no arguments | |
709 call = new llvm::CallInst(ir.mainFunc,"ret",bb); | |
710 } | |
711 call->setCallingConv(ir.mainFunc->getCallingConv()); | |
712 | |
713 // call static dtors | |
117 | 714 fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleDtor"); |
100 | 715 new llvm::CallInst(fn,"",bb); |
716 | |
717 // return | |
718 new llvm::ReturnInst(call,bb); | |
719 } | |
720 | |
721 ////////////////////////////////////////////////////////////////////////////////////////// | |
117 | 722 |
723 const llvm::FunctionType* DtoBaseFunctionType(FuncDeclaration* fdecl) | |
724 { | |
725 Dsymbol* parent = fdecl->toParent(); | |
726 ClassDeclaration* cd = parent->isClassDeclaration(); | |
727 assert(cd); | |
728 | |
729 FuncDeclaration* f = fdecl; | |
730 | |
731 while (cd) | |
732 { | |
733 ClassDeclaration* base = cd->baseClass; | |
734 if (!base) | |
735 break; | |
736 FuncDeclaration* f2 = base->findFunc(fdecl->ident, (TypeFunction*)fdecl->type); | |
737 if (f2) { | |
738 f = f2; | |
739 cd = base; | |
740 } | |
741 else | |
742 break; | |
743 } | |
744 | |
745 DtoResolveDsymbol(f); | |
746 return llvm::cast<llvm::FunctionType>(DtoType(f->type)); | |
747 } | |
748 | |
749 ////////////////////////////////////////////////////////////////////////////////////////// | |
131 | 750 |
751 DValue* DtoArgument(Argument* fnarg, Expression* argexp) | |
752 { | |
753 Logger::println("DtoArgument"); | |
754 LOG_SCOPE; | |
755 | |
756 DValue* arg = argexp->toElem(gIR); | |
757 | |
758 // ref/out arg | |
759 if (fnarg && ((fnarg->storageClass & STCref) || (fnarg->storageClass & STCout))) | |
760 { | |
761 if (arg->isVar() || arg->isLRValue()) | |
762 arg = new DImValue(argexp->type, arg->getLVal(), false); | |
763 else | |
764 arg = new DImValue(argexp->type, arg->getRVal(), false); | |
765 } | |
766 // aggregate arg | |
767 else if (DtoIsPassedByRef(argexp->type)) | |
768 { | |
769 llvm::Value* alloc = new llvm::AllocaInst(DtoType(argexp->type), "tmpparam", gIR->topallocapoint()); | |
770 DVarValue* vv = new DVarValue(argexp->type, alloc, true); | |
771 DtoAssign(vv, arg); | |
772 arg = vv; | |
773 } | |
774 // normal arg (basic/value type) | |
775 else | |
776 { | |
777 // nothing to do | |
778 } | |
779 | |
780 return arg; | |
781 } | |
782 | |
783 ////////////////////////////////////////////////////////////////////////////////////////// | |
784 | |
785 void DtoVariadicArgument(Expression* argexp, llvm::Value* dst) | |
786 { | |
787 Logger::println("DtoVariadicArgument"); | |
788 LOG_SCOPE; | |
789 DVarValue* vv = new DVarValue(argexp->type, dst, true); | |
790 DtoAssign(vv, argexp->toElem(gIR)); | |
791 } | |
792 | |
793 ////////////////////////////////////////////////////////////////////////////////////////// |