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