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