Mercurial > projects > ldc
annotate gen/toobj.c @ 17:6c6cd097bcdf trunk
[svn r21] * Fixed local instances of imported templates
author | lindquist |
---|---|
date | Wed, 03 Oct 2007 06:14:41 +0200 |
parents | 37a4fdab33fc |
children | c05ef76f1c20 |
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 |
14
0e86428ee567
[svn r18] * Initial support for switch statements - No string switches yet.
lindquist
parents:
11
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!"); |
14
0e86428ee567
[svn r18] * Initial support for switch statements - No string switches yet.
lindquist
parents:
11
diff
changeset
|
99 */ |
1 | 100 |
101 // run passes | |
102 // TODO | |
103 | |
104 /*if (global.params.llvmLL) { | |
105 //assert(0); | |
106 std::ofstream os(llfile->name->toChars()); | |
107 //llvm::WriteAssemblyToFile(ir.module, os); | |
108 ir.module->print(os); | |
109 }*/ | |
110 | |
6 | 111 // write bytecode |
1 | 112 //if (global.params.llvmBC) { |
113 Logger::println("Writing LLVM bitcode\n"); | |
114 std::ofstream os(bcfile->name->toChars(), std::ios::binary); | |
115 llvm::WriteBitcodeToFile(ir.module, os); | |
116 //} | |
117 | |
118 delete ir.module; | |
119 gIR = NULL; | |
120 } | |
121 | |
122 /* ================================================================== */ | |
123 | |
124 // Put out instance of ModuleInfo for this Module | |
125 | |
126 void Module::genmoduleinfo() | |
127 { | |
128 } | |
129 | |
130 /* ================================================================== */ | |
131 | |
132 void Dsymbol::toObjFile() | |
133 { | |
134 warning("Ignoring Dsymbol::toObjFile for %s", toChars()); | |
135 } | |
136 | |
137 /* ================================================================== */ | |
138 | |
139 void Declaration::toObjFile() | |
140 { | |
141 warning("Ignoring Declaration::toObjFile for %s", toChars()); | |
142 } | |
143 | |
144 /* ================================================================== */ | |
145 | |
6 | 146 /// 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
|
147 void AggregateDeclaration::offsetToIndex(unsigned os, std::vector<unsigned>& result) |
1 | 148 { |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
149 unsigned vos = 0; |
1 | 150 for (unsigned i=0; i<fields.dim; ++i) { |
151 VarDeclaration* vd = (VarDeclaration*)fields.data[i]; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
152 if (vd->type->ty == Tstruct) { |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
153 if (vos + vd->type->size() > os) { |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
154 TypeStruct* ts = (TypeStruct*)vd->type; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
155 StructDeclaration* sd = ts->sym; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
156 result.push_back(i); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
157 sd->offsetToIndex(os - vos, result); |
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 } |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
161 else if (os == vd->offset) { |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
162 result.push_back(i); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
163 return; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
164 } |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
165 vos += vd->offset; |
1 | 166 } |
167 assert(0 && "Offset not found in any aggregate field"); | |
168 } | |
169 | |
170 /* ================================================================== */ | |
171 | |
172 static unsigned LLVM_ClassOffsetToIndex(ClassDeclaration* cd, unsigned os, unsigned& idx) | |
173 { | |
174 // start at the bottom of the inheritance chain | |
175 if (cd->baseClass != 0) { | |
176 unsigned o = LLVM_ClassOffsetToIndex(cd->baseClass, os, idx); | |
177 if (o != (unsigned)-1) | |
178 return o; | |
179 } | |
180 | |
181 // check this class | |
182 unsigned i; | |
183 for (i=0; i<cd->fields.dim; ++i) { | |
184 VarDeclaration* vd = (VarDeclaration*)cd->fields.data[i]; | |
185 if (os == vd->offset) | |
186 return i+idx; | |
187 } | |
188 idx += i; | |
189 | |
190 return (unsigned)-1; | |
191 } | |
192 | |
6 | 193 /// Returns the LLVM style index from a DMD style offset |
194 /// Handles class inheritance | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
195 void ClassDeclaration::offsetToIndex(unsigned os, std::vector<unsigned>& result) |
1 | 196 { |
197 unsigned idx = 0; | |
198 unsigned r = LLVM_ClassOffsetToIndex(this, os, idx); | |
199 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
|
200 result.push_back(r+1); // vtable is 0 |
1 | 201 } |
202 | |
203 /* ================================================================== */ | |
204 | |
205 void InterfaceDeclaration::toObjFile() | |
206 { | |
207 warning("Ignoring InterfaceDeclaration::toObjFile for %s", toChars()); | |
208 } | |
209 | |
210 /* ================================================================== */ | |
211 | |
212 void StructDeclaration::toObjFile() | |
213 { | |
214 TypeStruct* ts = (TypeStruct*)type; | |
215 if (llvmType != 0) | |
216 return; | |
217 | |
218 static int sdi = 0; | |
219 Logger::print("StructDeclaration::toObjFile(%d): %s\n", sdi++, toChars()); | |
220 LOG_SCOPE; | |
221 | |
222 gIR->structs.push_back(IRStruct(ts)); | |
223 | |
224 std::vector<FuncDeclaration*> mfs; | |
225 | |
226 for (int k=0; k < members->dim; k++) { | |
227 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
228 | |
229 // need late generation of member functions | |
230 // they need the llvm::StructType to exist to take the 'this' parameter | |
231 if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { | |
232 mfs.push_back(fd); | |
233 } | |
234 else { | |
235 dsym->toObjFile(); | |
236 } | |
237 } | |
238 | |
239 llvm::StructType* structtype = llvm::StructType::get(gIR->topstruct().fields); | |
240 | |
241 // refine abstract types for stuff like: struct S{S* next;} | |
242 if (gIR->topstruct().recty != 0) | |
243 { | |
244 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
245 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
246 structtype = llvm::cast<llvm::StructType>(pa.get()); | |
247 } | |
248 | |
249 ts->llvmType = structtype; | |
250 llvmType = structtype; | |
251 | |
252 if (parent->isModule()) { | |
253 gIR->module->addTypeName(mangle(),ts->llvmType); | |
254 } | |
255 | |
256 // generate static data | |
257 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
258 llvm::Constant* _init = 0; | |
259 | |
260 // always generate the constant initalizer | |
261 if (!zeroInit) { | |
262 Logger::println("Not zero initialized"); | |
263 //assert(tk == gIR->topstruct().size()); | |
264 #ifndef LLVMD_NO_LOGGER | |
265 Logger::cout() << *structtype << '\n'; | |
266 for (size_t k=0; k<gIR->topstruct().inits.size(); ++k) { | |
267 Logger::cout() << "Type:" << '\n'; | |
268 Logger::cout() << *gIR->topstruct().inits[k]->getType() << '\n'; | |
269 Logger::cout() << "Value:" << '\n'; | |
270 Logger::cout() << *gIR->topstruct().inits[k] << '\n'; | |
271 } | |
272 Logger::cout() << "Initializer printed" << '\n'; | |
273 #endif | |
274 llvmInitZ = llvm::ConstantStruct::get(structtype,gIR->topstruct().inits); | |
275 } | |
276 else { | |
277 Logger::println("Zero initialized"); | |
278 llvmInitZ = llvm::ConstantAggregateZero::get(structtype); | |
279 } | |
280 | |
281 // only provide the constant initializer for the defining module | |
282 if (getModule() == gIR->dmodule) | |
283 { | |
284 _init = llvmInitZ; | |
285 } | |
286 | |
287 std::string initname(mangle()); | |
288 initname.append("__initZ"); | |
289 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, _init, initname, gIR->module); | |
290 ts->llvmInit = initvar; | |
291 | |
292 // generate member functions | |
293 size_t n = mfs.size(); | |
294 for (size_t i=0; i<n; ++i) { | |
295 mfs[i]->toObjFile(); | |
296 } | |
297 | |
298 llvmDModule = gIR->dmodule; | |
299 | |
300 gIR->structs.pop_back(); | |
301 | |
302 // generate typeinfo | |
303 type->getTypeInfo(NULL); // generate TypeInfo | |
304 } | |
305 | |
306 /* ================================================================== */ | |
307 | |
308 static void LLVM_AddBaseClassData(BaseClasses* bcs) | |
309 { | |
310 // add base class data members first | |
311 for (int j=0; j<bcs->dim; j++) | |
312 { | |
313 BaseClass* bc = (BaseClass*)(bcs->data[j]); | |
314 assert(bc); | |
315 LLVM_AddBaseClassData(&bc->base->baseclasses); | |
316 for (int k=0; k < bc->base->members->dim; k++) { | |
317 Dsymbol* dsym = (Dsymbol*)(bc->base->members->data[k]); | |
318 if (dsym->isVarDeclaration()) | |
319 { | |
320 dsym->toObjFile(); | |
321 } | |
322 } | |
323 } | |
324 } | |
325 | |
326 void ClassDeclaration::toObjFile() | |
327 { | |
328 TypeClass* ts = (TypeClass*)type; | |
329 if (ts->llvmType != 0 || llvmInProgress) | |
330 return; | |
331 | |
332 llvmInProgress = true; | |
333 | |
334 static int fdi = 0; | |
335 Logger::print("ClassDeclaration::toObjFile(%d): %s\n", fdi++, toChars()); | |
336 LOG_SCOPE; | |
337 | |
6 | 338 gIR->structs.push_back(IRStruct(ts)); |
1 | 339 gIR->classes.push_back(this); |
340 gIR->classmethods.push_back(IRState::FuncDeclVec()); | |
341 gIR->queueClassMethods.push_back(true); | |
342 | |
343 // add vtable | |
9 | 344 llvm::PATypeHolder pa = llvm::OpaqueType::get(); |
345 const llvm::Type* vtabty = llvm::PointerType::get(pa); | |
1 | 346 gIR->topstruct().fields.push_back(vtabty); |
347 gIR->topstruct().inits.push_back(0); | |
348 | |
349 // base classes first | |
350 LLVM_AddBaseClassData(&baseclasses); | |
351 | |
352 // then add own members | |
353 for (int k=0; k < members->dim; k++) { | |
354 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
355 dsym->toObjFile(); | |
356 } | |
357 | |
358 llvm::StructType* structtype = llvm::StructType::get(gIR->topstruct().fields); | |
6 | 359 // refine abstract types for stuff like: class C {C next;} |
360 if (gIR->topstruct().recty != 0) | |
361 { | |
362 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
363 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
364 structtype = llvm::cast<llvm::StructType>(pa.get()); | |
365 } | |
366 | |
1 | 367 ts->llvmType = structtype; |
368 llvmType = structtype; | |
369 | |
370 bool define_vtable = false; | |
371 if (parent->isModule()) { | |
372 gIR->module->addTypeName(mangle(),ts->llvmType); | |
373 define_vtable = (getModule() == gIR->dmodule); | |
374 } | |
375 else { | |
376 assert(0 && "class parent is not a module"); | |
377 } | |
378 | |
9 | 379 // generate vtable |
380 llvm::GlobalVariable* svtblVar = 0; | |
381 std::vector<llvm::Constant*> sinits; | |
382 std::vector<const llvm::Type*> sinits_ty; | |
383 sinits.reserve(vtbl.dim); | |
384 sinits_ty.reserve(vtbl.dim); | |
385 | |
386 for (int k=0; k < vtbl.dim; k++) | |
387 { | |
388 Dsymbol* dsym = (Dsymbol*)vtbl.data[k]; | |
389 assert(dsym); | |
390 //Logger::cout() << "vtblsym: " << dsym->toChars() << '\n'; | |
391 | |
392 if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { | |
393 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
|
394 assert(fd->llvmValue); |
9 | 395 llvm::Constant* c = llvm::cast<llvm::Constant>(fd->llvmValue); |
396 sinits.push_back(c); | |
397 sinits_ty.push_back(c->getType()); | |
398 } | |
399 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { | |
400 const llvm::Type* cty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
401 llvm::Constant* c = llvm::Constant::getNullValue(cty); | |
402 sinits.push_back(c); | |
403 sinits_ty.push_back(cty); | |
404 } | |
405 else | |
406 assert(0); | |
407 } | |
408 | |
409 const llvm::StructType* svtbl_ty = 0; | |
410 if (!sinits.empty()) | |
411 { | |
412 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
413 | |
414 std::string varname(mangle()); | |
415 varname.append("__vtblZ"); | |
416 std::string styname(mangle()); | |
417 styname.append("__vtblTy"); | |
418 | |
419 svtbl_ty = llvm::StructType::get(sinits_ty); | |
420 gIR->module->addTypeName(styname, svtbl_ty); | |
421 svtblVar = new llvm::GlobalVariable(svtbl_ty, true, _linkage, 0, varname, gIR->module); | |
422 | |
423 if (define_vtable) { | |
424 svtblVar->setInitializer(llvm::ConstantStruct::get(svtbl_ty, sinits)); | |
425 } | |
426 llvmVtbl = svtblVar; | |
427 } | |
428 | |
429 //////////////////////////////////////////////////////////////////////////////// | |
430 | |
431 // refine for final vtable type | |
432 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(svtbl_ty); | |
433 svtbl_ty = llvm::cast<llvm::StructType>(pa.get()); | |
434 structtype = llvm::cast<llvm::StructType>(gIR->topstruct().recty.get()); | |
435 ts->llvmType = structtype; | |
436 llvmType = structtype; | |
437 | |
438 // generate initializer | |
439 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
440 llvm::Constant* _init = 0; | |
441 | |
442 // first field is always the vtable | |
443 assert(svtblVar != 0); | |
444 gIR->topstruct().inits[0] = svtblVar; | |
445 | |
446 _init = llvm::ConstantStruct::get(structtype,gIR->topstruct().inits); | |
447 assert(_init); | |
448 std::string initname(mangle()); | |
449 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
|
450 //Logger::cout() << *_init << '\n'; |
9 | 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()) { |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
619 if (!llvmQueued) { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
620 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
|
621 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
|
622 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
|
623 llvmQueued = true; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
624 } |
10 | 625 return; // we wait with the definition as they might invoke a virtual method and the vtable is not yet complete |
1 | 626 } |
627 | |
628 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
|
629 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
|
630 const llvm::FunctionType* functype = llvm::cast<llvm::FunctionType>(llvmValue->getType()->getContainedType(0)); |
1 | 631 |
632 // only members of the current module maybe be defined | |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
633 if (getModule() == gIR->dmodule || parent->isTemplateInstance()) |
1 | 634 { |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
635 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
|
636 |
1 | 637 bool allow_fbody = true; |
638 // handle static constructor / destructor | |
639 if (isStaticCtorDeclaration() || isStaticDtorDeclaration()) { | |
640 const llvm::ArrayType* sctor_type = llvm::ArrayType::get(llvm::PointerType::get(functype),1); | |
641 //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
|
642 |
1 | 643 llvm::Constant* sctor_func = llvm::cast<llvm::Constant>(llvmValue); |
644 //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
|
645 |
1 | 646 llvm::Constant* sctor_init = 0; |
647 if (llvmInternal == LLVMnull) | |
648 { | |
649 llvm::Constant* sctor_init_null = llvm::Constant::getNullValue(sctor_func->getType()); | |
650 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_init_null,1); | |
651 allow_fbody = false; | |
652 } | |
653 else | |
654 { | |
655 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_func,1); | |
656 } | |
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 //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
|
659 |
1 | 660 // output the llvm.global_ctors array |
661 const char* varname = isStaticCtorDeclaration() ? "_d_module_ctor_array" : "_d_module_dtor_array"; | |
662 llvm::GlobalVariable* sctor_arr = new llvm::GlobalVariable(sctor_type, false, llvm::GlobalValue::AppendingLinkage, sctor_init, varname, gIR->module); | |
663 } | |
664 | |
665 // function definition | |
666 if (allow_fbody && fbody != 0) | |
667 { | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
668 gIR->funcdecls.push_back(this); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
669 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
670 // 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
|
671 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
|
672 |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
673 // this handling |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
674 if (f->llvmUsesThis) { |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
675 if (f->llvmRetInPtr) |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
676 llvmThisVar = ++func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
677 else |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
678 llvmThisVar = func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
679 assert(llvmThisVar != 0); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
680 } |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
681 |
9 | 682 if (isMain()) |
683 gIR->emitMain = true; | |
684 | |
1 | 685 gIR->funcs.push(func); |
686 gIR->functypes.push(f); | |
687 | |
688 IRScope irs; | |
689 irs.begin = new llvm::BasicBlock("entry",func); | |
690 irs.end = new llvm::BasicBlock("endentry",func); | |
691 | |
692 //assert(gIR->scopes.empty()); | |
693 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
|
694 |
1 | 695 // create alloca point |
696 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
|
697 |
1 | 698 // output function body |
699 fbody->toIR(gIR); | |
700 | |
701 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
702 // in automatically, so we do it here. | |
703 if (!isMain() && (gIR->scopebb()->empty() || !llvm::isa<llvm::TerminatorInst>(gIR->scopebb()->back()))) { | |
704 // pass the previous block into this block | |
705 //new llvm::BranchInst(irs.end, irs.begin); | |
706 new llvm::ReturnInst(gIR->scopebb()); | |
707 } | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
708 |
1 | 709 // erase alloca point |
710 f->llvmAllocaPoint->eraseFromParent(); | |
711 f->llvmAllocaPoint = 0; | |
712 | |
713 gIR->scopes.pop_back(); | |
714 //assert(gIR->scopes.empty()); | |
715 | |
716 gIR->functypes.pop(); | |
717 gIR->funcs.pop(); | |
718 | |
719 // get rid of the endentry block, it's never used | |
9 | 720 assert(!func->getBasicBlockList().empty()); |
1 | 721 func->getBasicBlockList().pop_back(); |
722 | |
723 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
9 | 724 // would be nice to figure out how to assert that this is correct |
1 | 725 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); |
726 if (lastbb->empty()) { | |
9 | 727 // possibly assert(lastbb->getNumPredecessors() == 0); ??? try it out sometime ... |
1 | 728 new llvm::UnreachableInst(lastbb); |
729 } | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
730 |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
731 gIR->funcdecls.pop_back(); |
1 | 732 } |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
733 |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
734 // template instances should have weak linkage |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
735 if (parent->isTemplateInstance()) { |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
736 func->setLinkage(llvm::GlobalValue::WeakLinkage); |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
737 } |
1 | 738 } |
739 } |