Mercurial > projects > ldc
annotate gen/toobj.c @ 24:25bb577878e8 trunk
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
author | lindquist |
---|---|
date | Thu, 04 Oct 2007 10:13:21 +0200 |
parents | 77e3d1ddae3f |
children | 99737f94abfb |
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(); | |
508 if (!_isconst) | |
509 _isconst = (storage_class & STCconst) ? true : false; // doesn't seem to work ): | |
510 llvm::GlobalValue::LinkageTypes _linkage = LLVM_DtoLinkage(protection, storage_class); | |
511 const llvm::Type* _type = LLVM_DtoType(type); | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
512 assert(_type); |
1 | 513 |
514 llvm::Constant* _init = 0; | |
515 bool _signed = !type->isunsigned(); | |
516 | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
517 Logger::println("Creating global variable"); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
518 std::string _name(mangle()); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
519 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
|
520 llvmValue = gvar; |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
521 gIR->lvals.push_back(gvar); |
1 | 522 |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
523 _init = LLVM_DtoInitializer(type, init); |
1 | 524 assert(_init); |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
525 |
1 | 526 //Logger::cout() << "initializer: " << *_init << '\n'; |
527 if (_type != _init->getType()) { | |
528 Logger::cout() << "got type '" << *_init->getType() << "' expected '" << *_type << "'\n"; | |
529 // zero initalizer | |
530 if (_init->isNullValue()) | |
531 _init = llvm::Constant::getNullValue(_type); | |
532 // pointer to global constant (struct.init) | |
533 else if (llvm::isa<llvm::GlobalVariable>(_init)) | |
534 { | |
535 assert(_init->getType()->getContainedType(0) == _type); | |
536 llvm::GlobalVariable* gv = llvm::cast<llvm::GlobalVariable>(_init); | |
537 assert(type->ty == Tstruct); | |
538 TypeStruct* ts = (TypeStruct*)type; | |
539 assert(ts->sym->llvmInitZ); | |
540 _init = ts->sym->llvmInitZ; | |
541 } | |
542 // array single value init | |
543 else if (llvm::isa<llvm::ArrayType>(_type)) | |
544 { | |
545 const llvm::ArrayType* at = llvm::cast<llvm::ArrayType>(_type); | |
546 assert(_type->getContainedType(0) == _init->getType()); | |
547 std::vector<llvm::Constant*> initvals; | |
548 initvals.resize(at->getNumElements(), _init); | |
549 _init = llvm::ConstantArray::get(at, initvals); | |
550 } | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
551 else { |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
552 Logger::cout() << "Unexpected initializer type: " << *_type << '\n'; |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
553 //assert(0); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
554 } |
1 | 555 } |
556 | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
557 gIR->lvals.pop_back(); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
558 |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
559 gvar->setInitializer(_init); |
1 | 560 |
561 //if (storage_class & STCprivate) | |
562 // gvar->setVisibility(llvm::GlobalValue::ProtectedVisibility); | |
563 } | |
564 | |
565 // inside aggregate declaration. declare a field. | |
566 else | |
567 { | |
568 Logger::println("Aggregate var declaration: '%s' offset=%d", toChars(), offset); | |
569 | |
570 const llvm::Type* _type = LLVM_DtoType(type); | |
571 gIR->topstruct().fields.push_back(_type); | |
572 | |
573 llvm::Constant* _init = LLVM_DtoInitializer(type, init); | |
574 if (_type != _init->getType()) | |
575 { | |
576 if (llvm::isa<llvm::ArrayType>(_type)) | |
577 { | |
578 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(_type); | |
579 uint64_t n = arrty->getNumElements(); | |
580 std::vector<llvm::Constant*> vals(n,_init); | |
581 _init = llvm::ConstantArray::get(arrty, vals); | |
582 } | |
6 | 583 else if (llvm::isa<llvm::StructType>(_type)) { |
584 const llvm::StructType* structty = llvm::cast<llvm::StructType>(_type); | |
585 TypeStruct* ts = (TypeStruct*)type; | |
9 | 586 assert(ts); |
587 assert(ts->sym); | |
6 | 588 assert(ts->sym->llvmInitZ); |
589 _init = ts->sym->llvmInitZ; | |
590 } | |
1 | 591 else |
592 assert(0); | |
593 } | |
594 gIR->topstruct().inits.push_back(_init); | |
595 } | |
596 | |
597 Logger::println("VarDeclaration::toObjFile is done"); | |
598 } | |
599 | |
600 /* ================================================================== */ | |
601 | |
602 void TypedefDeclaration::toObjFile() | |
603 { | |
604 static int tdi = 0; | |
605 Logger::print("TypedefDeclaration::toObjFile(%d): %s\n", tdi++, toChars()); | |
606 LOG_SCOPE; | |
607 | |
608 // TODO | |
609 } | |
610 | |
611 /* ================================================================== */ | |
612 | |
613 void EnumDeclaration::toObjFile() | |
614 { | |
615 warning("Ignoring EnumDeclaration::toObjFile for %s", toChars()); | |
616 } | |
617 | |
618 /* ================================================================== */ | |
619 | |
620 void FuncDeclaration::toObjFile() | |
621 { | |
18 | 622 if (llvmDModule) { |
9 | 623 assert(llvmValue != 0); |
1 | 624 return; |
625 } | |
626 | |
9 | 627 llvm::Function* func = LLVM_DtoDeclareFunction(this); |
628 | |
1 | 629 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
|
630 if (!llvmQueued) { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
631 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
|
632 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
|
633 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
|
634 llvmQueued = true; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
635 } |
10 | 636 return; // we wait with the definition as they might invoke a virtual method and the vtable is not yet complete |
1 | 637 } |
638 | |
639 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
|
640 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
|
641 const llvm::FunctionType* functype = llvm::cast<llvm::FunctionType>(llvmValue->getType()->getContainedType(0)); |
1 | 642 |
643 // only members of the current module maybe be defined | |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
644 if (getModule() == gIR->dmodule || parent->isTemplateInstance()) |
1 | 645 { |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
646 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
|
647 |
1 | 648 bool allow_fbody = true; |
649 // handle static constructor / destructor | |
650 if (isStaticCtorDeclaration() || isStaticDtorDeclaration()) { | |
651 const llvm::ArrayType* sctor_type = llvm::ArrayType::get(llvm::PointerType::get(functype),1); | |
652 //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
|
653 |
1 | 654 llvm::Constant* sctor_func = llvm::cast<llvm::Constant>(llvmValue); |
655 //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
|
656 |
1 | 657 llvm::Constant* sctor_init = 0; |
658 if (llvmInternal == LLVMnull) | |
659 { | |
660 llvm::Constant* sctor_init_null = llvm::Constant::getNullValue(sctor_func->getType()); | |
661 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_init_null,1); | |
662 allow_fbody = false; | |
663 } | |
664 else | |
665 { | |
666 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_func,1); | |
667 } | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
668 |
1 | 669 //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
|
670 |
1 | 671 // output the llvm.global_ctors array |
672 const char* varname = isStaticCtorDeclaration() ? "_d_module_ctor_array" : "_d_module_dtor_array"; | |
673 llvm::GlobalVariable* sctor_arr = new llvm::GlobalVariable(sctor_type, false, llvm::GlobalValue::AppendingLinkage, sctor_init, varname, gIR->module); | |
674 } | |
675 | |
676 // function definition | |
677 if (allow_fbody && fbody != 0) | |
678 { | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
679 gIR->funcdecls.push_back(this); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
680 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
681 // 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
|
682 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
|
683 |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
684 // this handling |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
685 if (f->llvmUsesThis) { |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
686 if (f->llvmRetInPtr) |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
687 llvmThisVar = ++func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
688 else |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
689 llvmThisVar = func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
690 assert(llvmThisVar != 0); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
691 } |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
692 |
9 | 693 if (isMain()) |
694 gIR->emitMain = true; | |
695 | |
1 | 696 gIR->funcs.push(func); |
697 gIR->functypes.push(f); | |
698 | |
699 IRScope irs; | |
700 irs.begin = new llvm::BasicBlock("entry",func); | |
701 irs.end = new llvm::BasicBlock("endentry",func); | |
702 | |
703 //assert(gIR->scopes.empty()); | |
704 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
|
705 |
1 | 706 // create alloca point |
707 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
|
708 |
1 | 709 // output function body |
710 fbody->toIR(gIR); | |
711 | |
712 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
713 // in automatically, so we do it here. | |
714 if (!isMain() && (gIR->scopebb()->empty() || !llvm::isa<llvm::TerminatorInst>(gIR->scopebb()->back()))) { | |
715 // pass the previous block into this block | |
716 //new llvm::BranchInst(irs.end, irs.begin); | |
717 new llvm::ReturnInst(gIR->scopebb()); | |
718 } | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
719 |
1 | 720 // erase alloca point |
721 f->llvmAllocaPoint->eraseFromParent(); | |
722 f->llvmAllocaPoint = 0; | |
723 | |
724 gIR->scopes.pop_back(); | |
725 //assert(gIR->scopes.empty()); | |
726 | |
727 gIR->functypes.pop(); | |
728 gIR->funcs.pop(); | |
729 | |
730 // get rid of the endentry block, it's never used | |
9 | 731 assert(!func->getBasicBlockList().empty()); |
1 | 732 func->getBasicBlockList().pop_back(); |
733 | |
734 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
9 | 735 // would be nice to figure out how to assert that this is correct |
1 | 736 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); |
737 if (lastbb->empty()) { | |
9 | 738 // possibly assert(lastbb->getNumPredecessors() == 0); ??? try it out sometime ... |
1 | 739 new llvm::UnreachableInst(lastbb); |
740 } | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
741 |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
742 gIR->funcdecls.pop_back(); |
1 | 743 } |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
744 |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
745 // template instances should have weak linkage |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
746 if (parent->isTemplateInstance()) { |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
747 func->setLinkage(llvm::GlobalValue::WeakLinkage); |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
748 } |
1 | 749 } |
750 } |