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