Mercurial > projects > ldc
annotate gen/toobj.c @ 9:dafae18f9c08 trunk
[svn r13] * Updated for LLVM 2.1
* Class v-tables are now typesafe
* Code cleanups
author | lindquist |
---|---|
date | Mon, 01 Oct 2007 21:19:53 +0200 |
parents | 5e69b77a5c51 |
children | c0f2c47e5034 |
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 | |
14 #include "llvm/Type.h" | |
15 #include "llvm/Constants.h" | |
16 #include "llvm/DerivedTypes.h" | |
17 #include "llvm/Instructions.h" | |
18 #include "llvm/Analysis/Verifier.h" | |
19 #include "llvm/Bitcode/ReaderWriter.h" | |
20 | |
21 #include "llvm/Target/TargetData.h" | |
22 #include "llvm/Target/TargetMachine.h" | |
23 #include "llvm/Target/TargetMachineRegistry.h" | |
24 | |
25 #include "mars.h" | |
26 #include "module.h" | |
27 #include "mtype.h" | |
28 #include "declaration.h" | |
29 #include "statement.h" | |
30 #include "enum.h" | |
31 #include "aggregate.h" | |
32 #include "init.h" | |
33 #include "attrib.h" | |
34 #include "id.h" | |
35 #include "import.h" | |
36 #include "template.h" | |
37 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
38 #include "gen/irstate.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
39 #include "gen/elem.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
40 #include "gen/logger.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
41 #include "gen/tollvm.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 | |
6 | 54 // creaet 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 | |
71 gTargetData = new llvm::TargetData(ir.module); | |
72 | |
6 | 73 // process module members |
1 | 74 for (int k=0; k < members->dim; k++) { |
75 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
76 assert(dsym); | |
77 dsym->toObjFile(); | |
78 } | |
79 | |
80 delete gTargetData; | |
81 gTargetData = 0; | |
82 | |
6 | 83 // verify the llvm |
1 | 84 std::string verifyErr; |
85 if (llvm::verifyModule(*ir.module,llvm::ReturnStatusAction,&verifyErr)) | |
86 { | |
87 error("%s", verifyErr.c_str()); | |
88 fatal(); | |
89 } | |
90 | |
6 | 91 // emit the llvm main function if necessary |
1 | 92 if (ir.emitMain) { |
93 LLVM_DtoMain(); | |
94 } | |
95 | |
96 // run passes | |
97 // TODO | |
98 | |
99 /*if (global.params.llvmLL) { | |
100 //assert(0); | |
101 std::ofstream os(llfile->name->toChars()); | |
102 //llvm::WriteAssemblyToFile(ir.module, os); | |
103 ir.module->print(os); | |
104 }*/ | |
105 | |
6 | 106 // write bytecode |
1 | 107 //if (global.params.llvmBC) { |
108 Logger::println("Writing LLVM bitcode\n"); | |
109 std::ofstream os(bcfile->name->toChars(), std::ios::binary); | |
110 llvm::WriteBitcodeToFile(ir.module, os); | |
111 //} | |
112 | |
113 delete ir.module; | |
114 gIR = NULL; | |
115 } | |
116 | |
117 /* ================================================================== */ | |
118 | |
119 // Put out instance of ModuleInfo for this Module | |
120 | |
121 void Module::genmoduleinfo() | |
122 { | |
123 } | |
124 | |
125 /* ================================================================== */ | |
126 | |
127 void Dsymbol::toObjFile() | |
128 { | |
129 warning("Ignoring Dsymbol::toObjFile for %s", toChars()); | |
130 } | |
131 | |
132 /* ================================================================== */ | |
133 | |
134 void Declaration::toObjFile() | |
135 { | |
136 warning("Ignoring Declaration::toObjFile for %s", toChars()); | |
137 } | |
138 | |
139 /* ================================================================== */ | |
140 | |
6 | 141 /// Returns the LLVM style index from a DMD style offset |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
142 void AggregateDeclaration::offsetToIndex(unsigned os, std::vector<unsigned>& result) |
1 | 143 { |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
144 unsigned vos = 0; |
1 | 145 for (unsigned i=0; i<fields.dim; ++i) { |
146 VarDeclaration* vd = (VarDeclaration*)fields.data[i]; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
147 if (vd->type->ty == Tstruct) { |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
148 if (vos + vd->type->size() > os) { |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
149 TypeStruct* ts = (TypeStruct*)vd->type; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
150 StructDeclaration* sd = ts->sym; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
151 result.push_back(i); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
152 sd->offsetToIndex(os - vos, result); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
153 return; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
154 } |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
155 } |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
156 else if (os == vd->offset) { |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
157 result.push_back(i); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
158 return; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
159 } |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
160 vos += vd->offset; |
1 | 161 } |
162 assert(0 && "Offset not found in any aggregate field"); | |
163 } | |
164 | |
165 /* ================================================================== */ | |
166 | |
167 static unsigned LLVM_ClassOffsetToIndex(ClassDeclaration* cd, unsigned os, unsigned& idx) | |
168 { | |
169 // start at the bottom of the inheritance chain | |
170 if (cd->baseClass != 0) { | |
171 unsigned o = LLVM_ClassOffsetToIndex(cd->baseClass, os, idx); | |
172 if (o != (unsigned)-1) | |
173 return o; | |
174 } | |
175 | |
176 // check this class | |
177 unsigned i; | |
178 for (i=0; i<cd->fields.dim; ++i) { | |
179 VarDeclaration* vd = (VarDeclaration*)cd->fields.data[i]; | |
180 if (os == vd->offset) | |
181 return i+idx; | |
182 } | |
183 idx += i; | |
184 | |
185 return (unsigned)-1; | |
186 } | |
187 | |
6 | 188 /// Returns the LLVM style index from a DMD style offset |
189 /// Handles class inheritance | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
190 void ClassDeclaration::offsetToIndex(unsigned os, std::vector<unsigned>& result) |
1 | 191 { |
192 unsigned idx = 0; | |
193 unsigned r = LLVM_ClassOffsetToIndex(this, os, idx); | |
194 assert(r != (unsigned)-1 && "Offset not found in any aggregate field"); | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
195 result.push_back(r+1); // vtable is 0 |
1 | 196 } |
197 | |
198 /* ================================================================== */ | |
199 | |
200 void InterfaceDeclaration::toObjFile() | |
201 { | |
202 warning("Ignoring InterfaceDeclaration::toObjFile for %s", toChars()); | |
203 } | |
204 | |
205 /* ================================================================== */ | |
206 | |
207 void StructDeclaration::toObjFile() | |
208 { | |
209 TypeStruct* ts = (TypeStruct*)type; | |
210 if (llvmType != 0) | |
211 return; | |
212 | |
213 static int sdi = 0; | |
214 Logger::print("StructDeclaration::toObjFile(%d): %s\n", sdi++, toChars()); | |
215 LOG_SCOPE; | |
216 | |
217 gIR->structs.push_back(IRStruct(ts)); | |
218 | |
219 std::vector<FuncDeclaration*> mfs; | |
220 | |
221 for (int k=0; k < members->dim; k++) { | |
222 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
223 | |
224 // need late generation of member functions | |
225 // they need the llvm::StructType to exist to take the 'this' parameter | |
226 if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { | |
227 mfs.push_back(fd); | |
228 } | |
229 else { | |
230 dsym->toObjFile(); | |
231 } | |
232 } | |
233 | |
234 llvm::StructType* structtype = llvm::StructType::get(gIR->topstruct().fields); | |
235 | |
236 // refine abstract types for stuff like: struct S{S* next;} | |
237 if (gIR->topstruct().recty != 0) | |
238 { | |
239 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
240 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
241 structtype = llvm::cast<llvm::StructType>(pa.get()); | |
242 } | |
243 | |
244 ts->llvmType = structtype; | |
245 llvmType = structtype; | |
246 | |
247 if (parent->isModule()) { | |
248 gIR->module->addTypeName(mangle(),ts->llvmType); | |
249 } | |
250 | |
251 // generate static data | |
252 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
253 llvm::Constant* _init = 0; | |
254 | |
255 // always generate the constant initalizer | |
256 if (!zeroInit) { | |
257 Logger::println("Not zero initialized"); | |
258 //assert(tk == gIR->topstruct().size()); | |
259 #ifndef LLVMD_NO_LOGGER | |
260 Logger::cout() << *structtype << '\n'; | |
261 for (size_t k=0; k<gIR->topstruct().inits.size(); ++k) { | |
262 Logger::cout() << "Type:" << '\n'; | |
263 Logger::cout() << *gIR->topstruct().inits[k]->getType() << '\n'; | |
264 Logger::cout() << "Value:" << '\n'; | |
265 Logger::cout() << *gIR->topstruct().inits[k] << '\n'; | |
266 } | |
267 Logger::cout() << "Initializer printed" << '\n'; | |
268 #endif | |
269 llvmInitZ = llvm::ConstantStruct::get(structtype,gIR->topstruct().inits); | |
270 } | |
271 else { | |
272 Logger::println("Zero initialized"); | |
273 llvmInitZ = llvm::ConstantAggregateZero::get(structtype); | |
274 } | |
275 | |
276 // only provide the constant initializer for the defining module | |
277 if (getModule() == gIR->dmodule) | |
278 { | |
279 _init = llvmInitZ; | |
280 } | |
281 | |
282 std::string initname(mangle()); | |
283 initname.append("__initZ"); | |
284 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, _init, initname, gIR->module); | |
285 ts->llvmInit = initvar; | |
286 | |
287 // generate member functions | |
288 size_t n = mfs.size(); | |
289 for (size_t i=0; i<n; ++i) { | |
290 mfs[i]->toObjFile(); | |
291 } | |
292 | |
293 llvmDModule = gIR->dmodule; | |
294 | |
295 gIR->structs.pop_back(); | |
296 | |
297 // generate typeinfo | |
298 type->getTypeInfo(NULL); // generate TypeInfo | |
299 } | |
300 | |
301 /* ================================================================== */ | |
302 | |
303 static void LLVM_AddBaseClassData(BaseClasses* bcs) | |
304 { | |
305 // add base class data members first | |
306 for (int j=0; j<bcs->dim; j++) | |
307 { | |
308 BaseClass* bc = (BaseClass*)(bcs->data[j]); | |
309 assert(bc); | |
310 LLVM_AddBaseClassData(&bc->base->baseclasses); | |
311 for (int k=0; k < bc->base->members->dim; k++) { | |
312 Dsymbol* dsym = (Dsymbol*)(bc->base->members->data[k]); | |
313 if (dsym->isVarDeclaration()) | |
314 { | |
315 dsym->toObjFile(); | |
316 } | |
317 } | |
318 } | |
319 } | |
320 | |
321 void ClassDeclaration::toObjFile() | |
322 { | |
323 TypeClass* ts = (TypeClass*)type; | |
324 if (ts->llvmType != 0 || llvmInProgress) | |
325 return; | |
326 | |
327 llvmInProgress = true; | |
328 | |
329 static int fdi = 0; | |
330 Logger::print("ClassDeclaration::toObjFile(%d): %s\n", fdi++, toChars()); | |
331 LOG_SCOPE; | |
332 | |
6 | 333 gIR->structs.push_back(IRStruct(ts)); |
1 | 334 gIR->classes.push_back(this); |
335 gIR->classmethods.push_back(IRState::FuncDeclVec()); | |
336 gIR->queueClassMethods.push_back(true); | |
337 | |
338 // add vtable | |
9 | 339 llvm::PATypeHolder pa = llvm::OpaqueType::get(); |
340 const llvm::Type* vtabty = llvm::PointerType::get(pa); | |
1 | 341 gIR->topstruct().fields.push_back(vtabty); |
342 gIR->topstruct().inits.push_back(0); | |
343 | |
344 // base classes first | |
345 LLVM_AddBaseClassData(&baseclasses); | |
346 | |
347 // then add own members | |
348 for (int k=0; k < members->dim; k++) { | |
349 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
350 dsym->toObjFile(); | |
351 } | |
352 | |
353 llvm::StructType* structtype = llvm::StructType::get(gIR->topstruct().fields); | |
6 | 354 // refine abstract types for stuff like: class C {C next;} |
355 if (gIR->topstruct().recty != 0) | |
356 { | |
357 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
358 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
359 structtype = llvm::cast<llvm::StructType>(pa.get()); | |
360 } | |
361 | |
1 | 362 ts->llvmType = structtype; |
363 llvmType = structtype; | |
364 | |
365 bool define_vtable = false; | |
366 if (parent->isModule()) { | |
367 gIR->module->addTypeName(mangle(),ts->llvmType); | |
368 define_vtable = (getModule() == gIR->dmodule); | |
369 } | |
370 else { | |
371 assert(0 && "class parent is not a module"); | |
372 } | |
373 | |
9 | 374 // generate vtable |
375 llvm::GlobalVariable* svtblVar = 0; | |
376 std::vector<llvm::Constant*> sinits; | |
377 std::vector<const llvm::Type*> sinits_ty; | |
378 sinits.reserve(vtbl.dim); | |
379 sinits_ty.reserve(vtbl.dim); | |
380 | |
381 for (int k=0; k < vtbl.dim; k++) | |
382 { | |
383 Dsymbol* dsym = (Dsymbol*)vtbl.data[k]; | |
384 assert(dsym); | |
385 //Logger::cout() << "vtblsym: " << dsym->toChars() << '\n'; | |
386 | |
387 if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { | |
388 fd->toObjFile(); | |
389 llvm::Constant* c = llvm::cast<llvm::Constant>(fd->llvmValue); | |
390 sinits.push_back(c); | |
391 sinits_ty.push_back(c->getType()); | |
392 } | |
393 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { | |
394 const llvm::Type* cty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
395 llvm::Constant* c = llvm::Constant::getNullValue(cty); | |
396 sinits.push_back(c); | |
397 sinits_ty.push_back(cty); | |
398 } | |
399 else | |
400 assert(0); | |
401 } | |
402 | |
403 const llvm::StructType* svtbl_ty = 0; | |
404 if (!sinits.empty()) | |
405 { | |
406 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
407 | |
408 std::string varname(mangle()); | |
409 varname.append("__vtblZ"); | |
410 std::string styname(mangle()); | |
411 styname.append("__vtblTy"); | |
412 | |
413 svtbl_ty = llvm::StructType::get(sinits_ty); | |
414 gIR->module->addTypeName(styname, svtbl_ty); | |
415 svtblVar = new llvm::GlobalVariable(svtbl_ty, true, _linkage, 0, varname, gIR->module); | |
416 | |
417 if (define_vtable) { | |
418 svtblVar->setInitializer(llvm::ConstantStruct::get(svtbl_ty, sinits)); | |
419 } | |
420 llvmVtbl = svtblVar; | |
421 } | |
422 | |
423 //////////////////////////////////////////////////////////////////////////////// | |
424 | |
425 // refine for final vtable type | |
426 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(svtbl_ty); | |
427 svtbl_ty = llvm::cast<llvm::StructType>(pa.get()); | |
428 structtype = llvm::cast<llvm::StructType>(gIR->topstruct().recty.get()); | |
429 ts->llvmType = structtype; | |
430 llvmType = structtype; | |
431 | |
432 // generate initializer | |
433 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
434 llvm::Constant* _init = 0; | |
435 | |
436 // first field is always the vtable | |
437 assert(svtblVar != 0); | |
438 gIR->topstruct().inits[0] = svtblVar; | |
439 | |
440 //assert(tk == gIR->topstruct().size()); | |
441 #ifndef LLVMD_NO_LOGGER | |
442 Logger::cout() << *structtype << '\n'; | |
443 //for (size_t k=0; k<gIR->topstruct().inits.size(); ++k) | |
444 // Logger::cout() << *gIR->topstruct().inits[k] << '\n'; | |
445 #endif | |
446 _init = llvm::ConstantStruct::get(structtype,gIR->topstruct().inits); | |
447 assert(_init); | |
448 std::string initname(mangle()); | |
449 initname.append("__initZ"); | |
450 Logger::cout() << *_init << '\n'; | |
451 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, 0, initname, gIR->module); | |
452 ts->llvmInit = initvar; | |
453 if (define_vtable) { | |
454 initvar->setInitializer(_init); | |
455 } | |
456 | |
457 // generate member function definitions | |
1 | 458 gIR->queueClassMethods.back() = false; |
459 IRState::FuncDeclVec& mfs = gIR->classmethods.back(); | |
460 size_t n = mfs.size(); | |
461 for (size_t i=0; i<n; ++i) { | |
462 mfs[i]->toObjFile(); | |
463 } | |
464 | |
465 gIR->queueClassMethods.pop_back(); | |
466 gIR->classmethods.pop_back(); | |
467 gIR->classes.pop_back(); | |
468 gIR->structs.pop_back(); | |
469 | |
470 llvmInProgress = false; | |
471 } | |
472 | |
473 /****************************************** | |
474 * Get offset of base class's vtbl[] initializer from start of csym. | |
475 * Returns ~0 if not this csym. | |
476 */ | |
477 | |
478 unsigned ClassDeclaration::baseVtblOffset(BaseClass *bc) | |
479 { | |
480 return ~0; | |
481 } | |
482 | |
483 /* ================================================================== */ | |
484 | |
485 void VarDeclaration::toObjFile() | |
486 { | |
487 static int vdi = 0; | |
488 Logger::print("VarDeclaration::toObjFile(%d): %s | %s\n", vdi++, toChars(), type->toChars()); | |
489 LOG_SCOPE; | |
490 llvm::Module* M = gIR->module; | |
491 | |
492 // handle bind pragma | |
493 if (llvmInternal == LLVMbind) { | |
494 Logger::println("var is bound: %s", llvmInternal1); | |
495 llvmValue = M->getGlobalVariable(llvmInternal1); | |
496 assert(llvmValue); | |
497 return; | |
498 } | |
499 | |
500 // global variable or magic | |
501 if (!parent || parent->isModule()) | |
502 { | |
503 bool _isconst = isConst(); | |
504 if (!_isconst) | |
505 _isconst = (storage_class & STCconst) ? true : false; // doesn't seem to work ): | |
506 llvm::GlobalValue::LinkageTypes _linkage = LLVM_DtoLinkage(protection, storage_class); | |
507 const llvm::Type* _type = LLVM_DtoType(type); | |
508 | |
509 llvm::Constant* _init = 0; | |
510 bool _signed = !type->isunsigned(); | |
511 | |
512 _init = LLVM_DtoInitializer(type, init); | |
513 | |
514 assert(_type); | |
515 assert(_init); | |
516 //Logger::cout() << "initializer: " << *_init << '\n'; | |
517 if (_type != _init->getType()) { | |
518 Logger::cout() << "got type '" << *_init->getType() << "' expected '" << *_type << "'\n"; | |
519 // zero initalizer | |
520 if (_init->isNullValue()) | |
521 _init = llvm::Constant::getNullValue(_type); | |
522 // pointer to global constant (struct.init) | |
523 else if (llvm::isa<llvm::GlobalVariable>(_init)) | |
524 { | |
525 assert(_init->getType()->getContainedType(0) == _type); | |
526 llvm::GlobalVariable* gv = llvm::cast<llvm::GlobalVariable>(_init); | |
527 assert(type->ty == Tstruct); | |
528 TypeStruct* ts = (TypeStruct*)type; | |
529 assert(ts->sym->llvmInitZ); | |
530 _init = ts->sym->llvmInitZ; | |
531 } | |
532 // array single value init | |
533 else if (llvm::isa<llvm::ArrayType>(_type)) | |
534 { | |
535 const llvm::ArrayType* at = llvm::cast<llvm::ArrayType>(_type); | |
536 assert(_type->getContainedType(0) == _init->getType()); | |
537 std::vector<llvm::Constant*> initvals; | |
538 initvals.resize(at->getNumElements(), _init); | |
539 _init = llvm::ConstantArray::get(at, initvals); | |
540 } | |
541 else | |
542 assert(0); | |
543 } | |
544 | |
545 Logger::println("Creating global variable"); | |
546 std::string _name(mangle()); | |
547 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(_type,_isconst,_linkage,_init,_name,M); | |
548 llvmValue = gvar; | |
549 | |
550 //if (storage_class & STCprivate) | |
551 // gvar->setVisibility(llvm::GlobalValue::ProtectedVisibility); | |
552 } | |
553 | |
554 // inside aggregate declaration. declare a field. | |
555 else | |
556 { | |
557 Logger::println("Aggregate var declaration: '%s' offset=%d", toChars(), offset); | |
558 | |
559 const llvm::Type* _type = LLVM_DtoType(type); | |
560 gIR->topstruct().fields.push_back(_type); | |
561 | |
562 llvm::Constant* _init = LLVM_DtoInitializer(type, init); | |
563 if (_type != _init->getType()) | |
564 { | |
565 if (llvm::isa<llvm::ArrayType>(_type)) | |
566 { | |
567 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(_type); | |
568 uint64_t n = arrty->getNumElements(); | |
569 std::vector<llvm::Constant*> vals(n,_init); | |
570 _init = llvm::ConstantArray::get(arrty, vals); | |
571 } | |
6 | 572 else if (llvm::isa<llvm::StructType>(_type)) { |
573 const llvm::StructType* structty = llvm::cast<llvm::StructType>(_type); | |
574 TypeStruct* ts = (TypeStruct*)type; | |
9 | 575 assert(ts); |
576 assert(ts->sym); | |
6 | 577 assert(ts->sym->llvmInitZ); |
578 _init = ts->sym->llvmInitZ; | |
579 } | |
1 | 580 else |
581 assert(0); | |
582 } | |
583 gIR->topstruct().inits.push_back(_init); | |
584 } | |
585 | |
586 Logger::println("VarDeclaration::toObjFile is done"); | |
587 } | |
588 | |
589 /* ================================================================== */ | |
590 | |
591 void TypedefDeclaration::toObjFile() | |
592 { | |
593 static int tdi = 0; | |
594 Logger::print("TypedefDeclaration::toObjFile(%d): %s\n", tdi++, toChars()); | |
595 LOG_SCOPE; | |
596 | |
597 // TODO | |
598 } | |
599 | |
600 /* ================================================================== */ | |
601 | |
602 void EnumDeclaration::toObjFile() | |
603 { | |
604 warning("Ignoring EnumDeclaration::toObjFile for %s", toChars()); | |
605 } | |
606 | |
607 /* ================================================================== */ | |
608 | |
609 void FuncDeclaration::toObjFile() | |
610 { | |
9 | 611 if (llvmDModule == gIR->dmodule) { |
612 assert(llvmValue != 0); | |
1 | 613 return; |
614 } | |
615 | |
9 | 616 llvm::Function* func = LLVM_DtoDeclareFunction(this); |
617 | |
1 | 618 if (!gIR->queueClassMethods.empty() && gIR->queueClassMethods.back()) { |
619 Logger::println("queueing %s", toChars()); | |
620 assert(!gIR->classmethods.empty()); | |
621 gIR->classmethods.back().push_back(this); | |
622 return; // will be generated later when the this parameter has a type | |
623 } | |
624 | |
9 | 625 if (llvmNeedsDefinition) |
626 { | |
1 | 627 |
628 TypeFunction* f = (TypeFunction*)type; | |
9 | 629 llvm::FunctionType* functype = llvm::cast<llvm::FunctionType>(f->llvmType); |
1 | 630 |
631 // only members of the current module maybe be defined | |
632 if (getModule() == gIR->dmodule) | |
633 { | |
634 bool allow_fbody = true; | |
635 // handle static constructor / destructor | |
636 if (isStaticCtorDeclaration() || isStaticDtorDeclaration()) { | |
637 const llvm::ArrayType* sctor_type = llvm::ArrayType::get(llvm::PointerType::get(functype),1); | |
638 //Logger::cout() << "static ctor type: " << *sctor_type << '\n'; | |
639 | |
640 llvm::Constant* sctor_func = llvm::cast<llvm::Constant>(llvmValue); | |
641 //Logger::cout() << "static ctor func: " << *sctor_func << '\n'; | |
642 | |
643 llvm::Constant* sctor_init = 0; | |
644 if (llvmInternal == LLVMnull) | |
645 { | |
646 llvm::Constant* sctor_init_null = llvm::Constant::getNullValue(sctor_func->getType()); | |
647 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_init_null,1); | |
648 allow_fbody = false; | |
649 } | |
650 else | |
651 { | |
652 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_func,1); | |
653 } | |
654 | |
655 //Logger::cout() << "static ctor init: " << *sctor_init << '\n'; | |
656 | |
657 | |
658 // output the llvm.global_ctors array | |
659 const char* varname = isStaticCtorDeclaration() ? "_d_module_ctor_array" : "_d_module_dtor_array"; | |
660 llvm::GlobalVariable* sctor_arr = new llvm::GlobalVariable(sctor_type, false, llvm::GlobalValue::AppendingLinkage, sctor_init, varname, gIR->module); | |
661 } | |
662 | |
663 // function definition | |
664 if (allow_fbody && fbody != 0) | |
665 { | |
9 | 666 if (isMain()) |
667 gIR->emitMain = true; | |
668 | |
1 | 669 gIR->funcs.push(func); |
670 gIR->functypes.push(f); | |
671 | |
672 IRScope irs; | |
673 irs.begin = new llvm::BasicBlock("entry",func); | |
674 irs.end = new llvm::BasicBlock("endentry",func); | |
675 | |
676 //assert(gIR->scopes.empty()); | |
677 gIR->scopes.push_back(irs); | |
678 | |
679 // create alloca point | |
680 f->llvmAllocaPoint = new llvm::BitCastInst(llvm::ConstantInt::get(llvm::Type::Int32Ty,0,false),llvm::Type::Int32Ty,"alloca point",gIR->scopebb()); | |
681 | |
682 // output function body | |
683 fbody->toIR(gIR); | |
684 | |
685 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
686 // in automatically, so we do it here. | |
687 if (!isMain() && (gIR->scopebb()->empty() || !llvm::isa<llvm::TerminatorInst>(gIR->scopebb()->back()))) { | |
688 // pass the previous block into this block | |
689 //new llvm::BranchInst(irs.end, irs.begin); | |
690 new llvm::ReturnInst(gIR->scopebb()); | |
691 } | |
692 | |
693 // erase alloca point | |
694 f->llvmAllocaPoint->eraseFromParent(); | |
695 f->llvmAllocaPoint = 0; | |
696 | |
697 gIR->scopes.pop_back(); | |
698 //assert(gIR->scopes.empty()); | |
699 | |
700 gIR->functypes.pop(); | |
701 gIR->funcs.pop(); | |
702 | |
703 // get rid of the endentry block, it's never used | |
9 | 704 assert(!func->getBasicBlockList().empty()); |
1 | 705 func->getBasicBlockList().pop_back(); |
706 | |
707 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
9 | 708 // would be nice to figure out how to assert that this is correct |
1 | 709 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); |
710 if (lastbb->empty()) { | |
9 | 711 // possibly assert(lastbb->getNumPredecessors() == 0); ??? try it out sometime ... |
1 | 712 new llvm::UnreachableInst(lastbb); |
713 } | |
714 } | |
715 } | |
9 | 716 |
1 | 717 } |
718 | |
719 llvmDModule = gIR->dmodule; | |
720 | |
721 Logger::println("FuncDeclaration done\n"); | |
722 } |