Mercurial > projects > ldc
annotate gen/toobj.c @ 28:1c80c18f3c82 trunk
[svn r32] * Fixed problems with arrays members of aggregates
author | lindquist |
---|---|
date | Thu, 04 Oct 2007 12:49:37 +0200 |
parents | 99737f94abfb |
children | 2841234d2aea |
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 |
24
25bb577878e8
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
lindquist
parents:
23
diff
changeset
|
146 void AggregateDeclaration::offsetToIndex(Type* t, unsigned os, std::vector<unsigned>& result) |
1 | 147 { |
24
25bb577878e8
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
lindquist
parents:
23
diff
changeset
|
148 Logger::println("checking for offset %u type %s:", os, t->toChars()); |
23 | 149 LOG_SCOPE; |
1 | 150 for (unsigned i=0; i<fields.dim; ++i) { |
151 VarDeclaration* vd = (VarDeclaration*)fields.data[i]; | |
24
25bb577878e8
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
lindquist
parents:
23
diff
changeset
|
152 Logger::println("found %u type %s", vd->offset, vd->type->toChars()); |
25bb577878e8
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
lindquist
parents:
23
diff
changeset
|
153 if (os == vd->offset && vd->type == t) { |
23 | 154 result.push_back(i); |
155 return; | |
156 } | |
24
25bb577878e8
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
lindquist
parents:
23
diff
changeset
|
157 else if (vd->type->ty == Tstruct && (vd->offset + vd->type->size()) > os) { |
25bb577878e8
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
lindquist
parents:
23
diff
changeset
|
158 TypeStruct* ts = (TypeStruct*)vd->type; |
25bb577878e8
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
lindquist
parents:
23
diff
changeset
|
159 StructDeclaration* sd = ts->sym; |
25bb577878e8
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
lindquist
parents:
23
diff
changeset
|
160 result.push_back(i); |
25bb577878e8
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
lindquist
parents:
23
diff
changeset
|
161 sd->offsetToIndex(t, os - vd->offset, result); |
25bb577878e8
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
lindquist
parents:
23
diff
changeset
|
162 return; |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
163 } |
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 | |
24
25bb577878e8
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
lindquist
parents:
23
diff
changeset
|
193 void ClassDeclaration::offsetToIndex(Type* t, 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 | |
23 | 237 if (gIR->topstruct().fields.empty()) |
238 { | |
239 gIR->topstruct().fields.push_back(llvm::Type::Int8Ty); | |
240 gIR->topstruct().inits.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false)); | |
241 } | |
242 | |
1 | 243 llvm::StructType* structtype = llvm::StructType::get(gIR->topstruct().fields); |
244 | |
245 // refine abstract types for stuff like: struct S{S* next;} | |
246 if (gIR->topstruct().recty != 0) | |
247 { | |
248 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
249 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
250 structtype = llvm::cast<llvm::StructType>(pa.get()); | |
251 } | |
252 | |
253 ts->llvmType = structtype; | |
254 llvmType = structtype; | |
255 | |
256 if (parent->isModule()) { | |
257 gIR->module->addTypeName(mangle(),ts->llvmType); | |
258 } | |
259 | |
260 // generate static data | |
261 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
262 llvm::Constant* _init = 0; | |
263 | |
264 // always generate the constant initalizer | |
265 if (!zeroInit) { | |
266 Logger::println("Not zero initialized"); | |
267 //assert(tk == gIR->topstruct().size()); | |
268 #ifndef LLVMD_NO_LOGGER | |
269 Logger::cout() << *structtype << '\n'; | |
270 for (size_t k=0; k<gIR->topstruct().inits.size(); ++k) { | |
271 Logger::cout() << "Type:" << '\n'; | |
272 Logger::cout() << *gIR->topstruct().inits[k]->getType() << '\n'; | |
273 Logger::cout() << "Value:" << '\n'; | |
274 Logger::cout() << *gIR->topstruct().inits[k] << '\n'; | |
275 } | |
276 Logger::cout() << "Initializer printed" << '\n'; | |
277 #endif | |
278 llvmInitZ = llvm::ConstantStruct::get(structtype,gIR->topstruct().inits); | |
279 } | |
280 else { | |
281 Logger::println("Zero initialized"); | |
282 llvmInitZ = llvm::ConstantAggregateZero::get(structtype); | |
283 } | |
284 | |
285 // only provide the constant initializer for the defining module | |
286 if (getModule() == gIR->dmodule) | |
287 { | |
288 _init = llvmInitZ; | |
289 } | |
290 | |
291 std::string initname(mangle()); | |
292 initname.append("__initZ"); | |
293 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, _init, initname, gIR->module); | |
294 ts->llvmInit = initvar; | |
295 | |
296 // generate member functions | |
297 size_t n = mfs.size(); | |
298 for (size_t i=0; i<n; ++i) { | |
299 mfs[i]->toObjFile(); | |
300 } | |
301 | |
302 llvmDModule = gIR->dmodule; | |
303 | |
304 gIR->structs.pop_back(); | |
305 | |
306 // generate typeinfo | |
307 type->getTypeInfo(NULL); // generate TypeInfo | |
308 } | |
309 | |
310 /* ================================================================== */ | |
311 | |
312 static void LLVM_AddBaseClassData(BaseClasses* bcs) | |
313 { | |
314 // add base class data members first | |
315 for (int j=0; j<bcs->dim; j++) | |
316 { | |
317 BaseClass* bc = (BaseClass*)(bcs->data[j]); | |
318 assert(bc); | |
319 LLVM_AddBaseClassData(&bc->base->baseclasses); | |
320 for (int k=0; k < bc->base->members->dim; k++) { | |
321 Dsymbol* dsym = (Dsymbol*)(bc->base->members->data[k]); | |
322 if (dsym->isVarDeclaration()) | |
323 { | |
324 dsym->toObjFile(); | |
325 } | |
326 } | |
327 } | |
328 } | |
329 | |
330 void ClassDeclaration::toObjFile() | |
331 { | |
332 TypeClass* ts = (TypeClass*)type; | |
333 if (ts->llvmType != 0 || llvmInProgress) | |
334 return; | |
335 | |
336 llvmInProgress = true; | |
337 | |
338 static int fdi = 0; | |
339 Logger::print("ClassDeclaration::toObjFile(%d): %s\n", fdi++, toChars()); | |
340 LOG_SCOPE; | |
341 | |
6 | 342 gIR->structs.push_back(IRStruct(ts)); |
1 | 343 gIR->classes.push_back(this); |
344 gIR->classmethods.push_back(IRState::FuncDeclVec()); | |
345 gIR->queueClassMethods.push_back(true); | |
346 | |
347 // add vtable | |
9 | 348 llvm::PATypeHolder pa = llvm::OpaqueType::get(); |
349 const llvm::Type* vtabty = llvm::PointerType::get(pa); | |
1 | 350 gIR->topstruct().fields.push_back(vtabty); |
351 gIR->topstruct().inits.push_back(0); | |
352 | |
353 // base classes first | |
354 LLVM_AddBaseClassData(&baseclasses); | |
355 | |
356 // then add own members | |
357 for (int k=0; k < members->dim; k++) { | |
358 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
359 dsym->toObjFile(); | |
360 } | |
361 | |
362 llvm::StructType* structtype = llvm::StructType::get(gIR->topstruct().fields); | |
6 | 363 // refine abstract types for stuff like: class C {C next;} |
364 if (gIR->topstruct().recty != 0) | |
365 { | |
366 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
367 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
368 structtype = llvm::cast<llvm::StructType>(pa.get()); | |
369 } | |
370 | |
1 | 371 ts->llvmType = structtype; |
372 llvmType = structtype; | |
373 | |
374 bool define_vtable = false; | |
375 if (parent->isModule()) { | |
376 gIR->module->addTypeName(mangle(),ts->llvmType); | |
377 define_vtable = (getModule() == gIR->dmodule); | |
378 } | |
379 else { | |
380 assert(0 && "class parent is not a module"); | |
381 } | |
382 | |
9 | 383 // generate vtable |
384 llvm::GlobalVariable* svtblVar = 0; | |
385 std::vector<llvm::Constant*> sinits; | |
386 std::vector<const llvm::Type*> sinits_ty; | |
387 sinits.reserve(vtbl.dim); | |
388 sinits_ty.reserve(vtbl.dim); | |
389 | |
390 for (int k=0; k < vtbl.dim; k++) | |
391 { | |
392 Dsymbol* dsym = (Dsymbol*)vtbl.data[k]; | |
393 assert(dsym); | |
394 //Logger::cout() << "vtblsym: " << dsym->toChars() << '\n'; | |
395 | |
396 if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { | |
397 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
|
398 assert(fd->llvmValue); |
9 | 399 llvm::Constant* c = llvm::cast<llvm::Constant>(fd->llvmValue); |
400 sinits.push_back(c); | |
401 sinits_ty.push_back(c->getType()); | |
402 } | |
403 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { | |
404 const llvm::Type* cty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
405 llvm::Constant* c = llvm::Constant::getNullValue(cty); | |
406 sinits.push_back(c); | |
407 sinits_ty.push_back(cty); | |
408 } | |
409 else | |
410 assert(0); | |
411 } | |
412 | |
413 const llvm::StructType* svtbl_ty = 0; | |
414 if (!sinits.empty()) | |
415 { | |
416 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
417 | |
418 std::string varname(mangle()); | |
419 varname.append("__vtblZ"); | |
420 std::string styname(mangle()); | |
421 styname.append("__vtblTy"); | |
422 | |
423 svtbl_ty = llvm::StructType::get(sinits_ty); | |
424 gIR->module->addTypeName(styname, svtbl_ty); | |
425 svtblVar = new llvm::GlobalVariable(svtbl_ty, true, _linkage, 0, varname, gIR->module); | |
426 | |
427 if (define_vtable) { | |
428 svtblVar->setInitializer(llvm::ConstantStruct::get(svtbl_ty, sinits)); | |
429 } | |
430 llvmVtbl = svtblVar; | |
431 } | |
432 | |
433 //////////////////////////////////////////////////////////////////////////////// | |
434 | |
435 // refine for final vtable type | |
436 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(svtbl_ty); | |
437 svtbl_ty = llvm::cast<llvm::StructType>(pa.get()); | |
438 structtype = llvm::cast<llvm::StructType>(gIR->topstruct().recty.get()); | |
439 ts->llvmType = structtype; | |
440 llvmType = structtype; | |
441 | |
442 // generate initializer | |
443 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
444 llvm::Constant* _init = 0; | |
445 | |
446 // first field is always the vtable | |
447 assert(svtblVar != 0); | |
448 gIR->topstruct().inits[0] = svtblVar; | |
449 | |
450 _init = llvm::ConstantStruct::get(structtype,gIR->topstruct().inits); | |
451 assert(_init); | |
452 std::string initname(mangle()); | |
453 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
|
454 //Logger::cout() << *_init << '\n'; |
9 | 455 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, 0, initname, gIR->module); |
456 ts->llvmInit = initvar; | |
457 if (define_vtable) { | |
458 initvar->setInitializer(_init); | |
459 } | |
460 | |
461 // generate member function definitions | |
1 | 462 gIR->queueClassMethods.back() = false; |
463 IRState::FuncDeclVec& mfs = gIR->classmethods.back(); | |
464 size_t n = mfs.size(); | |
465 for (size_t i=0; i<n; ++i) { | |
466 mfs[i]->toObjFile(); | |
467 } | |
468 | |
469 gIR->queueClassMethods.pop_back(); | |
470 gIR->classmethods.pop_back(); | |
471 gIR->classes.pop_back(); | |
472 gIR->structs.pop_back(); | |
473 | |
474 llvmInProgress = false; | |
475 } | |
476 | |
477 /****************************************** | |
478 * Get offset of base class's vtbl[] initializer from start of csym. | |
479 * Returns ~0 if not this csym. | |
480 */ | |
481 | |
482 unsigned ClassDeclaration::baseVtblOffset(BaseClass *bc) | |
483 { | |
484 return ~0; | |
485 } | |
486 | |
487 /* ================================================================== */ | |
488 | |
489 void VarDeclaration::toObjFile() | |
490 { | |
491 static int vdi = 0; | |
492 Logger::print("VarDeclaration::toObjFile(%d): %s | %s\n", vdi++, toChars(), type->toChars()); | |
493 LOG_SCOPE; | |
494 llvm::Module* M = gIR->module; | |
495 | |
496 // handle bind pragma | |
497 if (llvmInternal == LLVMbind) { | |
498 Logger::println("var is bound: %s", llvmInternal1); | |
499 llvmValue = M->getGlobalVariable(llvmInternal1); | |
500 assert(llvmValue); | |
501 return; | |
502 } | |
503 | |
504 // global variable or magic | |
22
a6360e68134a
[svn r26] * Fixed templates defining a constant value
lindquist
parents:
21
diff
changeset
|
505 if (isDataseg()) |
1 | 506 { |
507 bool _isconst = isConst(); | |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
508 |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
509 llvm::GlobalValue::LinkageTypes _linkage; |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
510 if (parent->isFuncDeclaration()) |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
511 _linkage = llvm::GlobalValue::InternalLinkage; |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
512 else |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
513 _linkage = LLVM_DtoLinkage(protection, storage_class); |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
514 |
1 | 515 const llvm::Type* _type = LLVM_DtoType(type); |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
516 assert(_type); |
1 | 517 |
518 llvm::Constant* _init = 0; | |
519 bool _signed = !type->isunsigned(); | |
520 | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
521 Logger::println("Creating global variable"); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
522 std::string _name(mangle()); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
523 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(_type,_isconst,_linkage,0,_name,M); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
524 llvmValue = gvar; |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
525 gIR->lvals.push_back(gvar); |
1 | 526 |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
527 _init = LLVM_DtoInitializer(type, init); |
1 | 528 assert(_init); |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
529 |
1 | 530 //Logger::cout() << "initializer: " << *_init << '\n'; |
531 if (_type != _init->getType()) { | |
532 Logger::cout() << "got type '" << *_init->getType() << "' expected '" << *_type << "'\n"; | |
533 // zero initalizer | |
534 if (_init->isNullValue()) | |
535 _init = llvm::Constant::getNullValue(_type); | |
536 // pointer to global constant (struct.init) | |
537 else if (llvm::isa<llvm::GlobalVariable>(_init)) | |
538 { | |
539 assert(_init->getType()->getContainedType(0) == _type); | |
540 llvm::GlobalVariable* gv = llvm::cast<llvm::GlobalVariable>(_init); | |
541 assert(type->ty == Tstruct); | |
542 TypeStruct* ts = (TypeStruct*)type; | |
543 assert(ts->sym->llvmInitZ); | |
544 _init = ts->sym->llvmInitZ; | |
545 } | |
546 // array single value init | |
547 else if (llvm::isa<llvm::ArrayType>(_type)) | |
548 { | |
549 const llvm::ArrayType* at = llvm::cast<llvm::ArrayType>(_type); | |
550 assert(_type->getContainedType(0) == _init->getType()); | |
551 std::vector<llvm::Constant*> initvals; | |
552 initvals.resize(at->getNumElements(), _init); | |
553 _init = llvm::ConstantArray::get(at, initvals); | |
554 } | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
555 else { |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
556 Logger::cout() << "Unexpected initializer type: " << *_type << '\n'; |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
557 //assert(0); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
558 } |
1 | 559 } |
560 | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
561 gIR->lvals.pop_back(); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
562 |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
563 gvar->setInitializer(_init); |
1 | 564 |
565 //if (storage_class & STCprivate) | |
566 // gvar->setVisibility(llvm::GlobalValue::ProtectedVisibility); | |
567 } | |
568 | |
569 // inside aggregate declaration. declare a field. | |
570 else | |
571 { | |
572 Logger::println("Aggregate var declaration: '%s' offset=%d", toChars(), offset); | |
573 | |
574 const llvm::Type* _type = LLVM_DtoType(type); | |
575 gIR->topstruct().fields.push_back(_type); | |
576 | |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
577 llvm::Constant*_init = LLVM_DtoInitializer(type, init); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
578 assert(_init); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
579 Logger::cout() << "field init is: " << *_init << " type should be " << *_type << '\n'; |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
580 if (!_init || _type != _init->getType()) |
1 | 581 { |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
582 if (type->ty == Tsarray) |
1 | 583 { |
584 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(_type); | |
585 uint64_t n = arrty->getNumElements(); | |
586 std::vector<llvm::Constant*> vals(n,_init); | |
587 _init = llvm::ConstantArray::get(arrty, vals); | |
588 } | |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
589 else if (type->ty == Tarray) |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
590 { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
591 assert(llvm::isa<llvm::StructType>(_type)); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
592 _init = llvm::ConstantAggregateZero::get(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
593 } |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
594 else if (type->ty == Tstruct) |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
595 { |
6 | 596 const llvm::StructType* structty = llvm::cast<llvm::StructType>(_type); |
597 TypeStruct* ts = (TypeStruct*)type; | |
9 | 598 assert(ts); |
599 assert(ts->sym); | |
6 | 600 assert(ts->sym->llvmInitZ); |
601 _init = ts->sym->llvmInitZ; | |
602 } | |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
603 else if (type->ty == Tclass) |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
604 { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
605 _init = llvm::Constant::getNullValue(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
606 } |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
607 else { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
608 Logger::println("failed for type %s", type->toChars()); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
609 assert(0); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
610 } |
1 | 611 } |
612 gIR->topstruct().inits.push_back(_init); | |
613 } | |
614 | |
615 Logger::println("VarDeclaration::toObjFile is done"); | |
616 } | |
617 | |
618 /* ================================================================== */ | |
619 | |
620 void TypedefDeclaration::toObjFile() | |
621 { | |
622 static int tdi = 0; | |
623 Logger::print("TypedefDeclaration::toObjFile(%d): %s\n", tdi++, toChars()); | |
624 LOG_SCOPE; | |
625 | |
626 // TODO | |
627 } | |
628 | |
629 /* ================================================================== */ | |
630 | |
631 void EnumDeclaration::toObjFile() | |
632 { | |
633 warning("Ignoring EnumDeclaration::toObjFile for %s", toChars()); | |
634 } | |
635 | |
636 /* ================================================================== */ | |
637 | |
638 void FuncDeclaration::toObjFile() | |
639 { | |
18 | 640 if (llvmDModule) { |
9 | 641 assert(llvmValue != 0); |
1 | 642 return; |
643 } | |
644 | |
9 | 645 llvm::Function* func = LLVM_DtoDeclareFunction(this); |
646 | |
1 | 647 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
|
648 if (!llvmQueued) { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
649 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
|
650 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
|
651 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
|
652 llvmQueued = true; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
653 } |
10 | 654 return; // we wait with the definition as they might invoke a virtual method and the vtable is not yet complete |
1 | 655 } |
656 | |
657 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
|
658 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
|
659 const llvm::FunctionType* functype = llvm::cast<llvm::FunctionType>(llvmValue->getType()->getContainedType(0)); |
1 | 660 |
661 // only members of the current module maybe be defined | |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
662 if (getModule() == gIR->dmodule || parent->isTemplateInstance()) |
1 | 663 { |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
664 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
|
665 |
1 | 666 bool allow_fbody = true; |
667 // handle static constructor / destructor | |
668 if (isStaticCtorDeclaration() || isStaticDtorDeclaration()) { | |
669 const llvm::ArrayType* sctor_type = llvm::ArrayType::get(llvm::PointerType::get(functype),1); | |
670 //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
|
671 |
1 | 672 llvm::Constant* sctor_func = llvm::cast<llvm::Constant>(llvmValue); |
673 //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
|
674 |
1 | 675 llvm::Constant* sctor_init = 0; |
676 if (llvmInternal == LLVMnull) | |
677 { | |
678 llvm::Constant* sctor_init_null = llvm::Constant::getNullValue(sctor_func->getType()); | |
679 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_init_null,1); | |
680 allow_fbody = false; | |
681 } | |
682 else | |
683 { | |
684 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_func,1); | |
685 } | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
686 |
1 | 687 //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
|
688 |
1 | 689 // output the llvm.global_ctors array |
690 const char* varname = isStaticCtorDeclaration() ? "_d_module_ctor_array" : "_d_module_dtor_array"; | |
691 llvm::GlobalVariable* sctor_arr = new llvm::GlobalVariable(sctor_type, false, llvm::GlobalValue::AppendingLinkage, sctor_init, varname, gIR->module); | |
692 } | |
693 | |
694 // function definition | |
695 if (allow_fbody && fbody != 0) | |
696 { | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
697 gIR->funcdecls.push_back(this); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
698 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
699 // 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
|
700 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
|
701 |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
702 // this handling |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
703 if (f->llvmUsesThis) { |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
704 if (f->llvmRetInPtr) |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
705 llvmThisVar = ++func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
706 else |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
707 llvmThisVar = func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
708 assert(llvmThisVar != 0); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
709 } |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
710 |
9 | 711 if (isMain()) |
712 gIR->emitMain = true; | |
713 | |
1 | 714 gIR->funcs.push(func); |
715 gIR->functypes.push(f); | |
716 | |
717 IRScope irs; | |
718 irs.begin = new llvm::BasicBlock("entry",func); | |
719 irs.end = new llvm::BasicBlock("endentry",func); | |
720 | |
721 //assert(gIR->scopes.empty()); | |
722 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
|
723 |
1 | 724 // create alloca point |
725 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
|
726 |
1 | 727 // output function body |
728 fbody->toIR(gIR); | |
729 | |
730 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
731 // in automatically, so we do it here. | |
732 if (!isMain() && (gIR->scopebb()->empty() || !llvm::isa<llvm::TerminatorInst>(gIR->scopebb()->back()))) { | |
733 // pass the previous block into this block | |
734 //new llvm::BranchInst(irs.end, irs.begin); | |
735 new llvm::ReturnInst(gIR->scopebb()); | |
736 } | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
737 |
1 | 738 // erase alloca point |
739 f->llvmAllocaPoint->eraseFromParent(); | |
740 f->llvmAllocaPoint = 0; | |
741 | |
742 gIR->scopes.pop_back(); | |
743 //assert(gIR->scopes.empty()); | |
744 | |
745 gIR->functypes.pop(); | |
746 gIR->funcs.pop(); | |
747 | |
748 // get rid of the endentry block, it's never used | |
9 | 749 assert(!func->getBasicBlockList().empty()); |
1 | 750 func->getBasicBlockList().pop_back(); |
751 | |
752 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
9 | 753 // would be nice to figure out how to assert that this is correct |
1 | 754 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); |
755 if (lastbb->empty()) { | |
9 | 756 // possibly assert(lastbb->getNumPredecessors() == 0); ??? try it out sometime ... |
1 | 757 new llvm::UnreachableInst(lastbb); |
758 } | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
759 |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
760 gIR->funcdecls.pop_back(); |
1 | 761 } |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
762 |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
763 // template instances should have weak linkage |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
764 if (parent->isTemplateInstance()) { |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
765 func->setLinkage(llvm::GlobalValue::WeakLinkage); |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
766 } |
1 | 767 } |
768 } |