Mercurial > projects > ldc
annotate gen/toobj.cpp @ 96:ce7ed8f59b99 trunk
[svn r100] Moved test/ray.d to demos/ray.d.
Cleanups.
author | lindquist |
---|---|
date | Mon, 12 Nov 2007 07:58:44 +0100 |
parents | 61615fa85940 |
children | 6789050b5ad1 |
rev | line source |
---|---|
1 | 1 |
2 // Copyright (c) 1999-2004 by Digital Mars | |
3 // All Rights Reserved | |
4 // written by Walter Bright | |
5 // www.digitalmars.com | |
6 // License for redistribution is by either the Artistic License | |
7 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
8 // See the included readme.txt for details. | |
9 | |
10 #include <cstddef> | |
11 #include <iostream> | |
12 #include <fstream> | |
13 | |
40 | 14 #include "gen/llvm.h" |
1 | 15 #include "llvm/Analysis/Verifier.h" |
16 #include "llvm/Bitcode/ReaderWriter.h" | |
17 #include "llvm/Target/TargetMachine.h" | |
18 #include "llvm/Target/TargetMachineRegistry.h" | |
19 | |
20 #include "mars.h" | |
21 #include "module.h" | |
22 #include "mtype.h" | |
23 #include "declaration.h" | |
24 #include "statement.h" | |
25 #include "enum.h" | |
26 #include "aggregate.h" | |
27 #include "init.h" | |
28 #include "attrib.h" | |
29 #include "id.h" | |
30 #include "import.h" | |
31 #include "template.h" | |
18 | 32 #include "scope.h" |
1 | 33 |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
34 #include "gen/irstate.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
35 #include "gen/logger.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
36 #include "gen/tollvm.h" |
64
b688ad419f8c
[svn r68] Added support for multi-dimensional static arrays.
lindquist
parents:
63
diff
changeset
|
37 #include "gen/arrays.h" |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
38 #include "gen/structs.h" |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
39 #include "gen/todebug.h" |
86
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
40 #include "gen/runtime.h" |
1 | 41 |
42 ////////////////////////////////////////////////////////////////////////////////////////// | |
43 | |
44 void | |
45 Module::genobjfile() | |
46 { | |
47 Logger::cout() << "Generating module: " << (md ? md->toChars() : toChars()) << '\n'; | |
48 LOG_SCOPE; | |
49 | |
6 | 50 // start by deleting the old object file |
1 | 51 deleteObjFile(); |
52 | |
18 | 53 // create a new ir state |
1 | 54 IRState ir; |
55 gIR = &ir; | |
56 ir.dmodule = this; | |
57 | |
6 | 58 // name the module |
1 | 59 std::string mname(toChars()); |
60 if (md != 0) | |
61 mname = md->toChars(); | |
62 ir.module = new llvm::Module(mname); | |
63 | |
6 | 64 // set target stuff |
1 | 65 std::string target_triple(global.params.tt_arch); |
66 target_triple.append(global.params.tt_os); | |
67 ir.module->setTargetTriple(target_triple); | |
68 ir.module->setDataLayout(global.params.data_layout); | |
69 | |
73 | 70 // heavily inspired by tools/llc/llc.cpp:200-230 |
71 const llvm::TargetMachineRegistry::Entry* targetEntry; | |
72 std::string targetError; | |
73 targetEntry = llvm::TargetMachineRegistry::getClosestStaticTargetForModule(*ir.module, targetError); | |
74 assert(targetEntry && "Failed to find a static target for module"); | |
75 std::auto_ptr<llvm::TargetMachine> targetPtr(targetEntry->CtorFn(*ir.module, "")); // TODO: replace "" with features | |
76 assert(targetPtr.get() && "Could not allocate target machine!"); | |
77 llvm::TargetMachine &targetMachine = *targetPtr.get(); | |
78 gTargetData = targetMachine.getTargetData(); | |
1 | 79 |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
80 // debug info |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
81 if (global.params.symdebug) { |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
82 RegisterDwarfSymbols(ir.module); |
94
61615fa85940
[svn r98] Added support for std.c.stdlib.alloca via pragma(LLVM_internal, "alloca").
lindquist
parents:
89
diff
changeset
|
83 ir.dmodule->llvmCompileUnit = DtoDwarfCompileUnit(this,true); |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
84 } |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
85 |
6 | 86 // process module members |
1 | 87 for (int k=0; k < members->dim; k++) { |
88 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
89 assert(dsym); | |
90 dsym->toObjFile(); | |
91 } | |
92 | |
89 | 93 // generate ModuleInfo |
94 genmoduleinfo(); | |
95 | |
1 | 96 gTargetData = 0; |
97 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
98 // emit the llvm main function if necessary |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
99 if (ir.emitMain) { |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
100 DtoMain(); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
101 } |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
102 |
6 | 103 // verify the llvm |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
104 if (!global.params.novalidate) { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
105 std::string verifyErr; |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
106 Logger::println("Verifying module..."); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
107 if (llvm::verifyModule(*ir.module,llvm::ReturnStatusAction,&verifyErr)) |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
108 { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
109 error("%s", verifyErr.c_str()); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
110 fatal(); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
111 } |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
112 else { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
113 Logger::println("Verification passed!"); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
114 } |
1 | 115 } |
116 | |
117 // run passes | |
118 // TODO | |
119 | |
52 | 120 // write bytecode |
121 { | |
122 Logger::println("Writing LLVM bitcode\n"); | |
123 std::ofstream bos(bcfile->name->toChars(), std::ios::binary); | |
124 llvm::WriteBitcodeToFile(ir.module, bos); | |
125 } | |
1 | 126 |
52 | 127 // disassemble ? |
128 if (global.params.disassemble) { | |
129 Logger::println("Writing LLVM asm to: %s\n", llfile->name->toChars()); | |
130 std::ofstream aos(llfile->name->toChars()); | |
131 ir.module->print(aos); | |
132 } | |
1 | 133 |
134 delete ir.module; | |
135 gIR = NULL; | |
136 } | |
137 | |
138 /* ================================================================== */ | |
139 | |
140 // Put out instance of ModuleInfo for this Module | |
141 | |
142 void Module::genmoduleinfo() | |
143 { | |
89 | 144 // The layout is: |
145 // { | |
146 // void **vptr; | |
147 // monitor_t monitor; | |
148 // char[] name; // class name | |
149 // ModuleInfo importedModules[]; | |
150 // ClassInfo localClasses[]; | |
151 // uint flags; // initialization state | |
152 // void *ctor; | |
153 // void *dtor; | |
154 // void *unitTest; | |
155 // } | |
156 | |
157 if (moduleinfo) { | |
158 Logger::println("moduleinfo"); | |
159 } | |
160 if (vmoduleinfo) { | |
161 Logger::println("vmoduleinfo"); | |
162 } | |
163 if (needModuleInfo()) { | |
94
61615fa85940
[svn r98] Added support for std.c.stdlib.alloca via pragma(LLVM_internal, "alloca").
lindquist
parents:
89
diff
changeset
|
164 Logger::attention("module info is needed but skipped"); |
89 | 165 } |
166 | |
167 | |
168 /* | |
169 Symbol *msym = toSymbol(); | |
170 unsigned offset; | |
171 unsigned sizeof_ModuleInfo = 12 * PTRSIZE; | |
172 | |
173 ////////////////////////////////////////////// | |
174 | |
175 csym->Sclass = SCglobal; | |
176 csym->Sfl = FLdata; | |
177 | |
178 // The layout is: | |
179 // { | |
180 // void **vptr; | |
181 // monitor_t monitor; | |
182 // char[] name; // class name | |
183 // ModuleInfo importedModules[]; | |
184 // ClassInfo localClasses[]; | |
185 // uint flags; // initialization state | |
186 // void *ctor; | |
187 // void *dtor; | |
188 // void *unitTest; | |
189 // } | |
190 dt_t *dt = NULL; | |
191 | |
192 if (moduleinfo) | |
193 dtxoff(&dt, moduleinfo->toVtblSymbol(), 0, TYnptr); // vtbl for ModuleInfo | |
194 else | |
195 dtdword(&dt, 0); // BUG: should be an assert() | |
196 dtdword(&dt, 0); // monitor | |
197 | |
198 // name[] | |
199 char *name = toPrettyChars(); | |
200 size_t namelen = strlen(name); | |
201 dtdword(&dt, namelen); | |
202 dtabytes(&dt, TYnptr, 0, namelen + 1, name); | |
203 | |
204 ClassDeclarations aclasses; | |
205 int i; | |
206 | |
207 //printf("members->dim = %d\n", members->dim); | |
208 for (i = 0; i < members->dim; i++) | |
209 { | |
210 Dsymbol *member; | |
211 | |
212 member = (Dsymbol *)members->data[i]; | |
213 //printf("\tmember '%s'\n", member->toChars()); | |
214 member->addLocalClass(&aclasses); | |
215 } | |
216 | |
217 // importedModules[] | |
218 int aimports_dim = aimports.dim; | |
219 for (i = 0; i < aimports.dim; i++) | |
220 { Module *m = (Module *)aimports.data[i]; | |
221 if (!m->needModuleInfo()) | |
222 aimports_dim--; | |
223 } | |
224 dtdword(&dt, aimports_dim); | |
225 if (aimports.dim) | |
226 dtxoff(&dt, csym, sizeof_ModuleInfo, TYnptr); | |
227 else | |
228 dtdword(&dt, 0); | |
229 | |
230 // localClasses[] | |
231 dtdword(&dt, aclasses.dim); | |
232 if (aclasses.dim) | |
233 dtxoff(&dt, csym, sizeof_ModuleInfo + aimports_dim * PTRSIZE, TYnptr); | |
234 else | |
235 dtdword(&dt, 0); | |
236 | |
237 if (needmoduleinfo) | |
238 dtdword(&dt, 0); // flags (4 means MIstandalone) | |
239 else | |
240 dtdword(&dt, 4); // flags (4 means MIstandalone) | |
241 | |
242 if (sctor) | |
243 dtxoff(&dt, sctor, 0, TYnptr); | |
244 else | |
245 dtdword(&dt, 0); | |
246 | |
247 if (sdtor) | |
248 dtxoff(&dt, sdtor, 0, TYnptr); | |
249 else | |
250 dtdword(&dt, 0); | |
251 | |
252 if (stest) | |
253 dtxoff(&dt, stest, 0, TYnptr); | |
254 else | |
255 dtdword(&dt, 0); | |
256 | |
257 ////////////////////////////////////////////// | |
258 | |
259 for (i = 0; i < aimports.dim; i++) | |
260 { | |
261 Module *m; | |
262 | |
263 m = (Module *)aimports.data[i]; | |
264 if (m->needModuleInfo()) | |
265 { Symbol *s = m->toSymbol(); | |
266 s->Sflags |= SFLweak; | |
267 dtxoff(&dt, s, 0, TYnptr); | |
268 } | |
269 } | |
270 | |
271 for (i = 0; i < aclasses.dim; i++) | |
272 { | |
273 ClassDeclaration *cd; | |
274 | |
275 cd = (ClassDeclaration *)aclasses.data[i]; | |
276 dtxoff(&dt, cd->toSymbol(), 0, TYnptr); | |
277 } | |
278 | |
279 csym->Sdt = dt; | |
280 #if ELFOBJ | |
281 // Cannot be CONST because the startup code sets flag bits in it | |
282 csym->Sseg = DATA; | |
283 #endif | |
284 outdata(csym); | |
285 | |
286 ////////////////////////////////////////////// | |
287 | |
288 obj_moduleinfo(msym); | |
289 */ | |
1 | 290 } |
291 | |
292 /* ================================================================== */ | |
293 | |
294 void Dsymbol::toObjFile() | |
295 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
296 Logger::println("Ignoring Dsymbol::toObjFile for %s", toChars()); |
1 | 297 } |
298 | |
299 /* ================================================================== */ | |
300 | |
301 void Declaration::toObjFile() | |
302 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
303 Logger::println("Ignoring Declaration::toObjFile for %s", toChars()); |
1 | 304 } |
305 | |
306 /* ================================================================== */ | |
307 | |
308 void InterfaceDeclaration::toObjFile() | |
309 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
310 Logger::println("Ignoring InterfaceDeclaration::toObjFile for %s", toChars()); |
1 | 311 } |
312 | |
313 /* ================================================================== */ | |
314 | |
315 void StructDeclaration::toObjFile() | |
316 { | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
317 TypeStruct* ts = (TypeStruct*)DtoDType(type); |
1 | 318 if (llvmType != 0) |
319 return; | |
320 | |
321 static int sdi = 0; | |
322 Logger::print("StructDeclaration::toObjFile(%d): %s\n", sdi++, toChars()); | |
323 LOG_SCOPE; | |
324 | |
325 gIR->structs.push_back(IRStruct(ts)); | |
326 | |
327 for (int k=0; k < members->dim; k++) { | |
328 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
329 dsym->toObjFile(); |
1 | 330 } |
331 | |
73 | 332 Logger::println("doing struct fields"); |
333 | |
96 | 334 const llvm::StructType* structtype = 0; |
73 | 335 std::vector<llvm::Constant*> fieldinits; |
336 | |
337 if (gIR->topstruct().offsets.empty()) | |
338 { | |
339 std::vector<const llvm::Type*> fieldtypes; | |
340 Logger::println("has no fields"); | |
341 fieldtypes.push_back(llvm::Type::Int8Ty); | |
342 fieldinits.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false)); | |
343 structtype = llvm::StructType::get(fieldtypes); | |
344 } | |
345 else | |
23 | 346 { |
73 | 347 Logger::println("has fields"); |
348 std::vector<const llvm::Type*> fieldtypes; | |
349 unsigned prevsize = (unsigned)-1; | |
350 unsigned lastoffset = (unsigned)-1; | |
351 const llvm::Type* fieldtype = NULL; | |
352 llvm::Constant* fieldinit = NULL; | |
353 size_t fieldpad = 0; | |
354 int idx = 0; | |
355 for (IRStruct::OffsetMap::iterator i=gIR->topstruct().offsets.begin(); i!=gIR->topstruct().offsets.end(); ++i) { | |
356 // first iteration | |
357 if (lastoffset == (unsigned)-1) { | |
358 lastoffset = i->first; | |
359 assert(lastoffset == 0); | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
360 fieldtype = DtoType(i->second.var->type); |
73 | 361 fieldinit = i->second.init; |
362 prevsize = gTargetData->getTypeSize(fieldtype); | |
363 i->second.var->llvmFieldIndex = idx; | |
364 } | |
365 // colliding offset? | |
366 else if (lastoffset == i->first) { | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
367 const llvm::Type* t = DtoType(i->second.var->type); |
73 | 368 size_t s = gTargetData->getTypeSize(t); |
369 if (s > prevsize) { | |
77
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
370 fieldpad += s - prevsize; |
73 | 371 prevsize = s; |
372 } | |
373 llvmHasUnions = true; | |
374 i->second.var->llvmFieldIndex = idx; | |
375 } | |
376 // intersecting offset? | |
377 else if (i->first < (lastoffset + prevsize)) { | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
378 const llvm::Type* t = DtoType(i->second.var->type); |
73 | 379 size_t s = gTargetData->getTypeSize(t); |
380 assert((i->first + s) <= (lastoffset + prevsize)); // this holds because all types are aligned to their size | |
381 llvmHasUnions = true; | |
382 i->second.var->llvmFieldIndex = idx; | |
383 i->second.var->llvmFieldIndexOffset = (i->first - lastoffset) / s; | |
384 } | |
385 // fresh offset | |
386 else { | |
387 // commit the field | |
388 fieldtypes.push_back(fieldtype); | |
389 fieldinits.push_back(fieldinit); | |
390 if (fieldpad) { | |
391 // match up with below | |
392 std::vector<llvm::Constant*> vals(fieldpad, llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false)); | |
393 llvm::Constant* c = llvm::ConstantArray::get(llvm::ArrayType::get(llvm::Type::Int8Ty, fieldpad), vals); | |
394 fieldtypes.push_back(c->getType()); | |
395 fieldinits.push_back(c); | |
396 idx++; | |
397 } | |
398 | |
399 idx++; | |
400 | |
401 // start new | |
402 lastoffset = i->first; | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
403 fieldtype = DtoType(i->second.var->type); |
73 | 404 fieldinit = i->second.init; |
405 prevsize = gTargetData->getTypeSize(fieldtype); | |
406 i->second.var->llvmFieldIndex = idx; | |
407 fieldpad = 0; | |
408 } | |
409 } | |
410 fieldtypes.push_back(fieldtype); | |
411 fieldinits.push_back(fieldinit); | |
412 if (fieldpad) { | |
413 // match up with above | |
414 std::vector<llvm::Constant*> vals(fieldpad, llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false)); | |
415 llvm::Constant* c = llvm::ConstantArray::get(llvm::ArrayType::get(llvm::Type::Int8Ty, fieldpad), vals); | |
416 fieldtypes.push_back(c->getType()); | |
417 fieldinits.push_back(c); | |
418 } | |
419 | |
420 Logger::println("creating struct type"); | |
421 structtype = llvm::StructType::get(fieldtypes); | |
23 | 422 } |
423 | |
1 | 424 // refine abstract types for stuff like: struct S{S* next;} |
425 if (gIR->topstruct().recty != 0) | |
426 { | |
427 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
428 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
96 | 429 structtype = isaStruct(pa.get()); |
1 | 430 } |
431 | |
432 ts->llvmType = structtype; | |
433 llvmType = structtype; | |
434 | |
435 if (parent->isModule()) { | |
436 gIR->module->addTypeName(mangle(),ts->llvmType); | |
437 } | |
438 | |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
439 llvmUnion = new DUnion; // uses gIR->topstruct() |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
440 |
1 | 441 // generate static data |
442 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
443 llvm::Constant* _init = 0; | |
444 | |
445 // always generate the constant initalizer | |
446 if (!zeroInit) { | |
447 Logger::println("Not zero initialized"); | |
73 | 448 //assert(tk == gIR->gIR->topstruct()().size()); |
1 | 449 #ifndef LLVMD_NO_LOGGER |
73 | 450 Logger::cout() << "struct type: " << *structtype << '\n'; |
451 for (size_t k=0; k<fieldinits.size(); ++k) { | |
1 | 452 Logger::cout() << "Type:" << '\n'; |
73 | 453 Logger::cout() << *fieldinits[k]->getType() << '\n'; |
1 | 454 Logger::cout() << "Value:" << '\n'; |
73 | 455 Logger::cout() << *fieldinits[k] << '\n'; |
1 | 456 } |
457 Logger::cout() << "Initializer printed" << '\n'; | |
458 #endif | |
73 | 459 llvmInitZ = llvm::ConstantStruct::get(structtype,fieldinits); |
1 | 460 } |
461 else { | |
462 Logger::println("Zero initialized"); | |
463 llvmInitZ = llvm::ConstantAggregateZero::get(structtype); | |
464 } | |
465 | |
466 // only provide the constant initializer for the defining module | |
467 if (getModule() == gIR->dmodule) | |
468 { | |
469 _init = llvmInitZ; | |
470 } | |
471 | |
73 | 472 std::string initname("_D"); |
473 initname.append(mangle()); | |
474 initname.append("6__initZ"); | |
1 | 475 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, _init, initname, gIR->module); |
476 ts->llvmInit = initvar; | |
477 | |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
478 // generate member function definitions |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
479 gIR->topstruct().queueFuncs = false; |
73 | 480 IRStruct::FuncDeclVector& mfs = gIR->topstruct().funcs; |
1 | 481 size_t n = mfs.size(); |
482 for (size_t i=0; i<n; ++i) { | |
483 mfs[i]->toObjFile(); | |
484 } | |
485 | |
486 llvmDModule = gIR->dmodule; | |
487 | |
488 gIR->structs.pop_back(); | |
489 | |
490 // generate typeinfo | |
73 | 491 if (getModule() == gIR->dmodule && llvmInternal != LLVMnotypeinfo) |
72 | 492 type->getTypeInfo(NULL); |
1 | 493 } |
494 | |
495 /* ================================================================== */ | |
496 | |
77
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
497 static unsigned LLVM_ClassOffsetToIndex(ClassDeclaration* cd, unsigned os, unsigned& idx) |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
498 { |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
499 // start at the bottom of the inheritance chain |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
500 if (cd->baseClass != 0) { |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
501 unsigned o = LLVM_ClassOffsetToIndex(cd->baseClass, os, idx); |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
502 if (o != (unsigned)-1) |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
503 return o; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
504 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
505 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
506 // check this class |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
507 unsigned i; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
508 for (i=0; i<cd->fields.dim; ++i) { |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
509 VarDeclaration* vd = (VarDeclaration*)cd->fields.data[i]; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
510 if (os == vd->offset) |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
511 return i+idx; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
512 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
513 idx += i; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
514 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
515 return (unsigned)-1; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
516 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
517 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
518 void ClassDeclaration::offsetToIndex(Type* t, unsigned os, std::vector<unsigned>& result) |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
519 { |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
520 unsigned idx = 0; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
521 unsigned r = LLVM_ClassOffsetToIndex(this, os, idx); |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
522 assert(r != (unsigned)-1 && "Offset not found in any aggregate field"); |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
523 result.push_back(r+1); // vtable is 0 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
524 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
525 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
526 /* ================================================================== */ |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
527 |
1 | 528 static void LLVM_AddBaseClassData(BaseClasses* bcs) |
529 { | |
530 // add base class data members first | |
531 for (int j=0; j<bcs->dim; j++) | |
532 { | |
533 BaseClass* bc = (BaseClass*)(bcs->data[j]); | |
534 assert(bc); | |
40 | 535 Logger::println("Adding base class members of %s", bc->base->toChars()); |
536 LOG_SCOPE; | |
537 | |
1 | 538 LLVM_AddBaseClassData(&bc->base->baseclasses); |
539 for (int k=0; k < bc->base->members->dim; k++) { | |
540 Dsymbol* dsym = (Dsymbol*)(bc->base->members->data[k]); | |
541 if (dsym->isVarDeclaration()) | |
542 { | |
543 dsym->toObjFile(); | |
544 } | |
545 } | |
546 } | |
547 } | |
548 | |
549 void ClassDeclaration::toObjFile() | |
550 { | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
551 TypeClass* ts = (TypeClass*)DtoDType(type); |
1 | 552 if (ts->llvmType != 0 || llvmInProgress) |
553 return; | |
554 | |
555 llvmInProgress = true; | |
556 | |
557 static int fdi = 0; | |
558 Logger::print("ClassDeclaration::toObjFile(%d): %s\n", fdi++, toChars()); | |
559 LOG_SCOPE; | |
560 | |
6 | 561 gIR->structs.push_back(IRStruct(ts)); |
1 | 562 gIR->classes.push_back(this); |
563 | |
564 // add vtable | |
9 | 565 llvm::PATypeHolder pa = llvm::OpaqueType::get(); |
566 const llvm::Type* vtabty = llvm::PointerType::get(pa); | |
73 | 567 |
568 std::vector<const llvm::Type*> fieldtypes; | |
569 fieldtypes.push_back(vtabty); | |
570 | |
571 std::vector<llvm::Constant*> fieldinits; | |
572 fieldinits.push_back(0); | |
1 | 573 |
574 // base classes first | |
575 LLVM_AddBaseClassData(&baseclasses); | |
576 | |
577 // then add own members | |
578 for (int k=0; k < members->dim; k++) { | |
579 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
580 dsym->toObjFile(); | |
581 } | |
582 | |
73 | 583 // fill out fieldtypes/inits |
584 for (IRStruct::OffsetMap::iterator i=gIR->topstruct().offsets.begin(); i!=gIR->topstruct().offsets.end(); ++i) { | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
585 fieldtypes.push_back(DtoType(i->second.var->type)); |
73 | 586 fieldinits.push_back(i->second.init); |
587 } | |
588 | |
96 | 589 const llvm::StructType* structtype = llvm::StructType::get(fieldtypes); |
6 | 590 // refine abstract types for stuff like: class C {C next;} |
591 if (gIR->topstruct().recty != 0) | |
592 { | |
593 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
594 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
96 | 595 structtype = isaStruct(pa.get()); |
6 | 596 } |
597 | |
1 | 598 ts->llvmType = structtype; |
599 llvmType = structtype; | |
600 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
601 bool needs_definition = false; |
1 | 602 if (parent->isModule()) { |
603 gIR->module->addTypeName(mangle(),ts->llvmType); | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
604 needs_definition = (getModule() == gIR->dmodule); |
1 | 605 } |
606 else { | |
607 assert(0 && "class parent is not a module"); | |
608 } | |
609 | |
9 | 610 // generate vtable |
611 llvm::GlobalVariable* svtblVar = 0; | |
612 std::vector<llvm::Constant*> sinits; | |
613 std::vector<const llvm::Type*> sinits_ty; | |
614 sinits.reserve(vtbl.dim); | |
615 sinits_ty.reserve(vtbl.dim); | |
616 | |
617 for (int k=0; k < vtbl.dim; k++) | |
618 { | |
619 Dsymbol* dsym = (Dsymbol*)vtbl.data[k]; | |
620 assert(dsym); | |
621 //Logger::cout() << "vtblsym: " << dsym->toChars() << '\n'; | |
622 | |
623 if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { | |
624 fd->toObjFile(); | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
625 assert(fd->llvmValue); |
9 | 626 llvm::Constant* c = llvm::cast<llvm::Constant>(fd->llvmValue); |
627 sinits.push_back(c); | |
628 sinits_ty.push_back(c->getType()); | |
629 } | |
630 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { | |
631 const llvm::Type* cty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
632 llvm::Constant* c = llvm::Constant::getNullValue(cty); | |
633 sinits.push_back(c); | |
634 sinits_ty.push_back(cty); | |
635 } | |
636 else | |
637 assert(0); | |
638 } | |
639 | |
640 const llvm::StructType* svtbl_ty = 0; | |
641 if (!sinits.empty()) | |
642 { | |
643 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
644 | |
52 | 645 std::string varname("_D"); |
646 varname.append(mangle()); | |
647 varname.append("6__vtblZ"); | |
648 | |
9 | 649 std::string styname(mangle()); |
650 styname.append("__vtblTy"); | |
651 | |
652 svtbl_ty = llvm::StructType::get(sinits_ty); | |
653 gIR->module->addTypeName(styname, svtbl_ty); | |
654 svtblVar = new llvm::GlobalVariable(svtbl_ty, true, _linkage, 0, varname, gIR->module); | |
655 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
656 llvmConstVtbl = llvm::cast<llvm::ConstantStruct>(llvm::ConstantStruct::get(svtbl_ty, sinits)); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
657 if (needs_definition) |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
658 svtblVar->setInitializer(llvmConstVtbl); |
9 | 659 llvmVtbl = svtblVar; |
660 } | |
661 | |
662 //////////////////////////////////////////////////////////////////////////////// | |
663 | |
664 // refine for final vtable type | |
665 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(svtbl_ty); | |
96 | 666 svtbl_ty = isaStruct(pa.get()); |
667 structtype = isaStruct(gIR->topstruct().recty.get()); | |
9 | 668 ts->llvmType = structtype; |
669 llvmType = structtype; | |
670 | |
671 // generate initializer | |
672 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
673 llvm::Constant* _init = 0; | |
674 | |
675 // first field is always the vtable | |
676 assert(svtblVar != 0); | |
73 | 677 fieldinits[0] = svtblVar; |
9 | 678 |
73 | 679 llvmInitZ = _init = llvm::ConstantStruct::get(structtype,fieldinits); |
9 | 680 assert(_init); |
52 | 681 |
682 std::string initname("_D"); | |
683 initname.append(mangle()); | |
684 initname.append("6__initZ"); | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
685 //Logger::cout() << *_init << '\n'; |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
686 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, NULL, initname, gIR->module); |
9 | 687 ts->llvmInit = initvar; |
688 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
689 if (needs_definition) { |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
690 initvar->setInitializer(_init); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
691 // generate member functions |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
692 gIR->topstruct().queueFuncs = false; |
73 | 693 IRStruct::FuncDeclVector& mfs = gIR->topstruct().funcs; |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
694 size_t n = mfs.size(); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
695 for (size_t i=0; i<n; ++i) { |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
696 mfs[i]->toObjFile(); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
697 } |
1 | 698 } |
699 | |
700 gIR->classes.pop_back(); | |
701 gIR->structs.pop_back(); | |
702 | |
703 llvmInProgress = false; | |
89 | 704 |
705 // if (ClassDeclaration::classinfo != this) | |
706 // DtoClassInfo(this); | |
1 | 707 } |
708 | |
709 /****************************************** | |
710 * Get offset of base class's vtbl[] initializer from start of csym. | |
711 * Returns ~0 if not this csym. | |
712 */ | |
713 | |
714 unsigned ClassDeclaration::baseVtblOffset(BaseClass *bc) | |
715 { | |
716 return ~0; | |
717 } | |
718 | |
719 /* ================================================================== */ | |
720 | |
721 void VarDeclaration::toObjFile() | |
722 { | |
40 | 723 Logger::print("VarDeclaration::toObjFile(): %s | %s\n", toChars(), type->toChars()); |
1 | 724 LOG_SCOPE; |
725 llvm::Module* M = gIR->module; | |
726 | |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
727 if (aliassym) |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
728 { |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
729 Logger::println("alias sym"); |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
730 toAlias()->toObjFile(); |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
731 return; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
732 } |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
733 |
1 | 734 // global variable or magic |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
735 if (isDataseg()) |
1 | 736 { |
40 | 737 if (llvmTouched) return; |
738 else llvmTouched = true; | |
739 | |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
740 bool _isconst = false; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
741 if (isConst() && (init && !init->isExpInitializer())) |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
742 _isconst = true; |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
743 |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
744 llvm::GlobalValue::LinkageTypes _linkage; |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
745 bool istempl = false; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
746 if ((storage_class & STCcomdat) || (parent && DtoIsTemplateInstance(parent))) { |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
747 _linkage = llvm::GlobalValue::WeakLinkage; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
748 istempl = true; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
749 } |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
750 else if (parent && parent->isFuncDeclaration()) |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
751 _linkage = llvm::GlobalValue::InternalLinkage; |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
752 else |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
753 _linkage = DtoLinkage(protection, storage_class); |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
754 |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
755 Type* t = DtoDType(type); |
40 | 756 |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
757 const llvm::Type* _type = DtoType(t); |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
758 assert(_type); |
1 | 759 |
760 llvm::Constant* _init = 0; | |
761 bool _signed = !type->isunsigned(); | |
762 | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
763 Logger::println("Creating global variable"); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
764 std::string _name(mangle()); |
52 | 765 |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
766 bool emitRTstaticInit = false; |
40 | 767 |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
768 if (!(storage_class & STCextern) && (getModule() == gIR->dmodule || istempl)) |
52 | 769 { |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
770 if (parent && parent->isFuncDeclaration() && init && init->isExpInitializer()) { |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
771 _init = DtoConstInitializer(t, NULL); |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
772 emitRTstaticInit = true; |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
773 } |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
774 else { |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
775 _init = DtoConstInitializer(t, init); |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
776 } |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
777 |
52 | 778 //Logger::cout() << "initializer: " << *_init << '\n'; |
779 if (_type != _init->getType()) { | |
780 Logger::cout() << "got type '" << *_init->getType() << "' expected '" << *_type << "'\n"; | |
781 // zero initalizer | |
782 if (_init->isNullValue()) | |
783 _init = llvm::Constant::getNullValue(_type); | |
784 // pointer to global constant (struct.init) | |
785 else if (llvm::isa<llvm::GlobalVariable>(_init)) | |
786 { | |
787 assert(_init->getType()->getContainedType(0) == _type); | |
788 llvm::GlobalVariable* gv = llvm::cast<llvm::GlobalVariable>(_init); | |
789 assert(t->ty == Tstruct); | |
790 TypeStruct* ts = (TypeStruct*)t; | |
791 assert(ts->sym->llvmInitZ); | |
792 _init = ts->sym->llvmInitZ; | |
793 } | |
794 // array single value init | |
96 | 795 else if (isaArray(_type)) |
52 | 796 { |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
797 _init = DtoConstStaticArray(_type, _init); |
52 | 798 } |
799 else { | |
800 Logger::cout() << "Unexpected initializer type: " << *_type << '\n'; | |
801 //assert(0); | |
802 } | |
1 | 803 } |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
804 } |
52 | 805 |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
806 if (_init && _init->getType() != _type) |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
807 _type = _init->getType(); |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
808 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(_type,_isconst,_linkage,_init,_name,M); |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
809 llvmValue = gvar; |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
810 |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
811 if (emitRTstaticInit) |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
812 DtoLazyStaticInit(istempl, gvar, init, t); |
1 | 813 |
40 | 814 llvmDModule = gIR->dmodule; |
1 | 815 |
816 //if (storage_class & STCprivate) | |
817 // gvar->setVisibility(llvm::GlobalValue::ProtectedVisibility); | |
818 } | |
819 | |
820 // inside aggregate declaration. declare a field. | |
821 else | |
822 { | |
823 Logger::println("Aggregate var declaration: '%s' offset=%d", toChars(), offset); | |
824 | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
825 Type* t = DtoDType(type); |
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
826 const llvm::Type* _type = DtoType(t); |
1 | 827 |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
828 llvm::Constant*_init = DtoConstInitializer(t, init); |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
829 assert(_init); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
830 Logger::cout() << "field init is: " << *_init << " type should be " << *_type << '\n'; |
40 | 831 if (_type != _init->getType()) |
1 | 832 { |
40 | 833 if (t->ty == Tsarray) |
1 | 834 { |
96 | 835 const llvm::ArrayType* arrty = isaArray(_type); |
1 | 836 uint64_t n = arrty->getNumElements(); |
837 std::vector<llvm::Constant*> vals(n,_init); | |
838 _init = llvm::ConstantArray::get(arrty, vals); | |
839 } | |
40 | 840 else if (t->ty == Tarray) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
841 { |
96 | 842 assert(isaStruct(_type)); |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
843 _init = llvm::ConstantAggregateZero::get(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
844 } |
40 | 845 else if (t->ty == Tstruct) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
846 { |
96 | 847 const llvm::StructType* structty = isaStruct(_type); |
40 | 848 TypeStruct* ts = (TypeStruct*)t; |
9 | 849 assert(ts); |
850 assert(ts->sym); | |
6 | 851 assert(ts->sym->llvmInitZ); |
852 _init = ts->sym->llvmInitZ; | |
853 } | |
40 | 854 else if (t->ty == Tclass) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
855 { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
856 _init = llvm::Constant::getNullValue(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
857 } |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
858 else { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
859 Logger::println("failed for type %s", type->toChars()); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
860 assert(0); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
861 } |
1 | 862 } |
73 | 863 |
864 // add the field in the IRStruct | |
865 gIR->topstruct().offsets.insert(std::make_pair(offset, IRStruct::Offset(this,_init))); | |
1 | 866 } |
867 | |
868 Logger::println("VarDeclaration::toObjFile is done"); | |
869 } | |
870 | |
871 /* ================================================================== */ | |
872 | |
873 void TypedefDeclaration::toObjFile() | |
874 { | |
875 static int tdi = 0; | |
876 Logger::print("TypedefDeclaration::toObjFile(%d): %s\n", tdi++, toChars()); | |
877 LOG_SCOPE; | |
878 | |
52 | 879 // generate typeinfo |
72 | 880 type->getTypeInfo(NULL); |
1 | 881 } |
882 | |
883 /* ================================================================== */ | |
884 | |
885 void EnumDeclaration::toObjFile() | |
886 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
887 Logger::println("Ignoring EnumDeclaration::toObjFile for %s", toChars()); |
1 | 888 } |
889 | |
890 /* ================================================================== */ | |
891 | |
892 void FuncDeclaration::toObjFile() | |
893 { | |
18 | 894 if (llvmDModule) { |
9 | 895 assert(llvmValue != 0); |
1 | 896 return; |
897 } | |
898 | |
86
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
899 if (llvmRunTimeHack) { |
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
900 Logger::println("runtime hack func chars: %s", toChars()); |
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
901 if (!llvmValue) |
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
902 llvmValue = LLVM_D_GetRuntimeFunction(gIR->module, toChars()); |
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
903 return; |
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
904 } |
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
905 |
63
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
906 if (isUnitTestDeclaration()) { |
94
61615fa85940
[svn r98] Added support for std.c.stdlib.alloca via pragma(LLVM_internal, "alloca").
lindquist
parents:
89
diff
changeset
|
907 Logger::attention("ignoring unittest declaration: %s", toChars()); |
63
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
908 return; |
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
909 } |
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
910 |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
911 Type* t = DtoDType(type); |
55
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
912 TypeFunction* f = (TypeFunction*)t; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
913 |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
914 bool declareOnly = false; |
86
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
915 if (parent) |
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
916 { |
55
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
917 if (TemplateInstance* tinst = parent->isTemplateInstance()) { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
918 TemplateDeclaration* tempdecl = tinst->tempdecl; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
919 if (tempdecl->llvmInternal == LLVMva_start) |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
920 { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
921 Logger::println("magic va_start found"); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
922 llvmInternal = LLVMva_start; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
923 declareOnly = true; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
924 } |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
925 else if (tempdecl->llvmInternal == LLVMva_arg) |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
926 { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
927 Logger::println("magic va_arg found"); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
928 llvmInternal = LLVMva_arg; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
929 return; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
930 } |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
931 } |
86
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
932 } |
55
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
933 |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
934 llvm::Function* func = DtoDeclareFunction(this); |
9 | 935 |
55
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
936 if (declareOnly) |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
937 return; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
938 |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
939 if (!gIR->structs.empty() && gIR->topstruct().queueFuncs) { |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
940 if (!llvmQueued) { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
941 Logger::println("queueing %s", toChars()); |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
942 gIR->topstruct().funcs.push_back(this); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
943 llvmQueued = true; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
944 } |
10 | 945 return; // we wait with the definition as they might invoke a virtual method and the vtable is not yet complete |
1 | 946 } |
947 | |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
948 // debug info |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
949 if (global.params.symdebug) { |
94
61615fa85940
[svn r98] Added support for std.c.stdlib.alloca via pragma(LLVM_internal, "alloca").
lindquist
parents:
89
diff
changeset
|
950 Module* mo = getModule(); |
61615fa85940
[svn r98] Added support for std.c.stdlib.alloca via pragma(LLVM_internal, "alloca").
lindquist
parents:
89
diff
changeset
|
951 if (!mo->llvmCompileUnit) { |
61615fa85940
[svn r98] Added support for std.c.stdlib.alloca via pragma(LLVM_internal, "alloca").
lindquist
parents:
89
diff
changeset
|
952 mo->llvmCompileUnit = DtoDwarfCompileUnit(mo,false); |
61615fa85940
[svn r98] Added support for std.c.stdlib.alloca via pragma(LLVM_internal, "alloca").
lindquist
parents:
89
diff
changeset
|
953 } |
61615fa85940
[svn r98] Added support for std.c.stdlib.alloca via pragma(LLVM_internal, "alloca").
lindquist
parents:
89
diff
changeset
|
954 llvmDwarfSubProgram = DtoDwarfSubProgram(this, mo->llvmCompileUnit); |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
955 } |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
956 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
957 assert(f->llvmType); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
958 const llvm::FunctionType* functype = llvm::cast<llvm::FunctionType>(llvmValue->getType()->getContainedType(0)); |
1 | 959 |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
960 // template instances should have weak linkage |
86
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
961 if (parent && DtoIsTemplateInstance(parent)) { |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
962 func->setLinkage(llvm::GlobalValue::WeakLinkage); |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
963 } |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
964 |
1 | 965 // only members of the current module maybe be defined |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
966 if (getModule() == gIR->dmodule || DtoIsTemplateInstance(parent)) |
1 | 967 { |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
968 llvmDModule = gIR->dmodule; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
969 |
1 | 970 // handle static constructor / destructor |
971 if (isStaticCtorDeclaration() || isStaticDtorDeclaration()) { | |
972 const llvm::ArrayType* sctor_type = llvm::ArrayType::get(llvm::PointerType::get(functype),1); | |
973 //Logger::cout() << "static ctor type: " << *sctor_type << '\n'; | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
974 |
1 | 975 llvm::Constant* sctor_func = llvm::cast<llvm::Constant>(llvmValue); |
976 //Logger::cout() << "static ctor func: " << *sctor_func << '\n'; | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
977 |
74 | 978 llvm::Constant* sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_func,1); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
979 |
1 | 980 //Logger::cout() << "static ctor init: " << *sctor_init << '\n'; |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
981 |
1 | 982 // output the llvm.global_ctors array |
983 const char* varname = isStaticCtorDeclaration() ? "_d_module_ctor_array" : "_d_module_dtor_array"; | |
984 llvm::GlobalVariable* sctor_arr = new llvm::GlobalVariable(sctor_type, false, llvm::GlobalValue::AppendingLinkage, sctor_init, varname, gIR->module); | |
985 } | |
986 | |
987 // function definition | |
74 | 988 if (fbody != 0) |
1 | 989 { |
40 | 990 gIR->functions.push_back(IRFunction(this)); |
991 gIR->func().func = func; | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
992 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
993 // first make absolutely sure the type is up to date |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
994 f->llvmType = llvmValue->getType()->getContainedType(0); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
995 |
52 | 996 //Logger::cout() << "func type: " << *f->llvmType << '\n'; |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
997 |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
998 // this handling |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
999 if (f->llvmUsesThis) { |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1000 Logger::println("uses this"); |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1001 if (f->llvmRetInPtr) |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1002 llvmThisVar = ++func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1003 else |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1004 llvmThisVar = func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1005 assert(llvmThisVar != 0); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1006 } |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1007 |
9 | 1008 if (isMain()) |
1009 gIR->emitMain = true; | |
1010 | |
40 | 1011 llvm::BasicBlock* beginbb = new llvm::BasicBlock("entry",func); |
1012 llvm::BasicBlock* endbb = new llvm::BasicBlock("endentry",func); | |
1 | 1013 |
1014 //assert(gIR->scopes.empty()); | |
40 | 1015 gIR->scopes.push_back(IRScope(beginbb, endbb)); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
1016 |
1 | 1017 // create alloca point |
1018 f->llvmAllocaPoint = new llvm::BitCastInst(llvm::ConstantInt::get(llvm::Type::Int32Ty,0,false),llvm::Type::Int32Ty,"alloca point",gIR->scopebb()); | |
40 | 1019 gIR->func().allocapoint = f->llvmAllocaPoint; |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
1020 |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1021 // give arguments storage |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1022 size_t n = Argument::dim(f->parameters); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1023 for (int i=0; i < n; ++i) { |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1024 Argument* arg = Argument::getNth(f->parameters, i); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1025 if (arg && arg->vardecl) { |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1026 VarDeclaration* vd = arg->vardecl; |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1027 if (!vd->llvmNeedsStorage || vd->nestedref || vd->isRef() || vd->isOut() || DtoIsPassedByRef(vd->type)) |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1028 continue; |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1029 llvm::Value* a = vd->llvmValue; |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1030 assert(a); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1031 std::string s(a->getName()); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1032 Logger::println("giving argument '%s' storage", s.c_str()); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1033 s.append("_storage"); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1034 llvm::Value* v = new llvm::AllocaInst(a->getType(),s,f->llvmAllocaPoint); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1035 gIR->ir->CreateStore(a,v); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1036 vd->llvmValue = v; |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1037 } |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
1038 else { |
94
61615fa85940
[svn r98] Added support for std.c.stdlib.alloca via pragma(LLVM_internal, "alloca").
lindquist
parents:
89
diff
changeset
|
1039 Logger::attention("some unknown argument: %s", arg ? arg->toChars() : 0); |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
1040 } |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1041 } |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1042 |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1043 // debug info |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1044 if (global.params.symdebug) DtoDwarfFuncStart(this); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1045 |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1046 llvm::Value* parentNested = NULL; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1047 if (FuncDeclaration* fd = toParent()->isFuncDeclaration()) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1048 parentNested = fd->llvmNested; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1049 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1050 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1051 // construct nested variables struct |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1052 if (!llvmNestedVars.empty() || parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1053 std::vector<const llvm::Type*> nestTypes; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1054 int j = 0; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1055 if (parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1056 nestTypes.push_back(parentNested->getType()); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1057 j++; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1058 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1059 for (std::set<VarDeclaration*>::iterator i=llvmNestedVars.begin(); i!=llvmNestedVars.end(); ++i) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1060 VarDeclaration* vd = *i; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1061 vd->llvmNestedIndex = j++; |
67
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
1062 if (vd->isParameter()) { |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
1063 assert(vd->llvmValue); |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
1064 nestTypes.push_back(vd->llvmValue->getType()); |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
1065 } |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
1066 else { |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
1067 nestTypes.push_back(DtoType(vd->type)); |
67
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
1068 } |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1069 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1070 const llvm::StructType* nestSType = llvm::StructType::get(nestTypes); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1071 Logger::cout() << "nested var struct has type:" << '\n' << *nestSType; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1072 llvmNested = new llvm::AllocaInst(nestSType,"nestedvars",f->llvmAllocaPoint); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1073 if (parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1074 assert(llvmThisVar); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1075 llvm::Value* ptr = gIR->ir->CreateBitCast(llvmThisVar, parentNested->getType(), "tmp"); |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
1076 gIR->ir->CreateStore(ptr, DtoGEPi(llvmNested, 0,0, "tmp")); |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1077 } |
67
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
1078 for (std::set<VarDeclaration*>::iterator i=llvmNestedVars.begin(); i!=llvmNestedVars.end(); ++i) { |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
1079 VarDeclaration* vd = *i; |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
1080 if (vd->isParameter()) { |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
1081 gIR->ir->CreateStore(vd->llvmValue, DtoGEPi(llvmNested, 0, vd->llvmNestedIndex, "tmp")); |
68
c4b3f5d2cd9b
[svn r72] Calling a nested function that is not a delegate was not working.
lindquist
parents:
67
diff
changeset
|
1082 vd->llvmValue = llvmNested; |
67
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
1083 } |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
1084 } |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1085 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
1086 |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1087 // copy _argptr to a memory location |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1088 if (f->linkage == LINKd && f->varargs == 1) |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1089 { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1090 llvm::Value* argptrmem = new llvm::AllocaInst(llvmArgPtr->getType(), "_argptrmem", gIR->topallocapoint()); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1091 new llvm::StoreInst(llvmArgPtr, argptrmem, gIR->scopebb()); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1092 llvmArgPtr = argptrmem; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1093 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1094 |
1 | 1095 // output function body |
1096 fbody->toIR(gIR); | |
1097 | |
1098 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
1099 // in automatically, so we do it here. | |
40 | 1100 if (!isMain()) { |
52 | 1101 if (!gIR->scopereturned()) { |
40 | 1102 // pass the previous block into this block |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
1103 if (global.params.symdebug) DtoDwarfFuncEnd(this); |
40 | 1104 if (func->getReturnType() == llvm::Type::VoidTy) { |
1105 new llvm::ReturnInst(gIR->scopebb()); | |
1106 } | |
52 | 1107 else { |
1108 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
1109 } | |
40 | 1110 } |
1 | 1111 } |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
1112 |
1 | 1113 // erase alloca point |
1114 f->llvmAllocaPoint->eraseFromParent(); | |
1115 f->llvmAllocaPoint = 0; | |
40 | 1116 gIR->func().allocapoint = 0; |
1 | 1117 |
1118 gIR->scopes.pop_back(); | |
1119 | |
1120 // get rid of the endentry block, it's never used | |
9 | 1121 assert(!func->getBasicBlockList().empty()); |
1 | 1122 func->getBasicBlockList().pop_back(); |
1123 | |
1124 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
9 | 1125 // would be nice to figure out how to assert that this is correct |
1 | 1126 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); |
1127 if (lastbb->empty()) { | |
52 | 1128 if (lastbb->getNumUses() == 0) |
1129 lastbb->eraseFromParent(); | |
1130 else { | |
1131 new llvm::UnreachableInst(lastbb); | |
1132 /*if (func->getReturnType() == llvm::Type::VoidTy) { | |
1133 new llvm::ReturnInst(lastbb); | |
1134 } | |
1135 else { | |
1136 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), lastbb); | |
1137 }*/ | |
1138 } | |
1 | 1139 } |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1140 |
40 | 1141 gIR->functions.pop_back(); |
1 | 1142 } |
1143 } | |
1144 } |