Mercurial > projects > ldc
annotate gen/toobj.c @ 73:b706170e24a9 trunk
[svn r77] Fixed foreach on slice.
Fixed some nested function problems when accessing outer function parameters.
Major changes to handling of structs.
Initial support for unions.
Probably more...
author | lindquist |
---|---|
date | Wed, 31 Oct 2007 03:11:32 +0100 |
parents | d7e764e62462 |
children | eb379601d445 |
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 | |
40 | 14 #include "gen/llvm.h" |
1 | 15 #include "llvm/Analysis/Verifier.h" |
16 #include "llvm/Bitcode/ReaderWriter.h" | |
17 #include "llvm/Target/TargetMachine.h" | |
18 #include "llvm/Target/TargetMachineRegistry.h" | |
19 | |
20 #include "mars.h" | |
21 #include "module.h" | |
22 #include "mtype.h" | |
23 #include "declaration.h" | |
24 #include "statement.h" | |
25 #include "enum.h" | |
26 #include "aggregate.h" | |
27 #include "init.h" | |
28 #include "attrib.h" | |
29 #include "id.h" | |
30 #include "import.h" | |
31 #include "template.h" | |
18 | 32 #include "scope.h" |
1 | 33 |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
34 #include "gen/irstate.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
35 #include "gen/elem.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
36 #include "gen/logger.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
37 #include "gen/tollvm.h" |
64
b688ad419f8c
[svn r68] Added support for multi-dimensional static arrays.
lindquist
parents:
63
diff
changeset
|
38 #include "gen/arrays.h" |
1 | 39 |
40 ////////////////////////////////////////////////////////////////////////////////////////// | |
41 | |
42 void | |
43 Module::genobjfile() | |
44 { | |
45 Logger::cout() << "Generating module: " << (md ? md->toChars() : toChars()) << '\n'; | |
46 LOG_SCOPE; | |
47 | |
6 | 48 // start by deleting the old object file |
1 | 49 deleteObjFile(); |
50 | |
18 | 51 // create a new ir state |
1 | 52 IRState ir; |
53 gIR = &ir; | |
54 ir.dmodule = this; | |
55 | |
6 | 56 // name the module |
1 | 57 std::string mname(toChars()); |
58 if (md != 0) | |
59 mname = md->toChars(); | |
60 ir.module = new llvm::Module(mname); | |
61 | |
6 | 62 // set target stuff |
1 | 63 std::string target_triple(global.params.tt_arch); |
64 target_triple.append(global.params.tt_os); | |
65 ir.module->setTargetTriple(target_triple); | |
66 ir.module->setDataLayout(global.params.data_layout); | |
67 | |
73 | 68 // heavily inspired by tools/llc/llc.cpp:200-230 |
69 const llvm::TargetMachineRegistry::Entry* targetEntry; | |
70 std::string targetError; | |
71 targetEntry = llvm::TargetMachineRegistry::getClosestStaticTargetForModule(*ir.module, targetError); | |
72 assert(targetEntry && "Failed to find a static target for module"); | |
73 std::auto_ptr<llvm::TargetMachine> targetPtr(targetEntry->CtorFn(*ir.module, "")); // TODO: replace "" with features | |
74 assert(targetPtr.get() && "Could not allocate target machine!"); | |
75 llvm::TargetMachine &targetMachine = *targetPtr.get(); | |
76 gTargetData = targetMachine.getTargetData(); | |
1 | 77 |
6 | 78 // process module members |
1 | 79 for (int k=0; k < members->dim; k++) { |
80 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
81 assert(dsym); | |
82 dsym->toObjFile(); | |
83 } | |
84 | |
85 gTargetData = 0; | |
86 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
87 // 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
|
88 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
|
89 LLVM_DtoMain(); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
90 } |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
91 |
6 | 92 // verify the llvm |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
93 if (!global.params.novalidate) { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
94 std::string verifyErr; |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
95 Logger::println("Verifying module..."); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
96 if (llvm::verifyModule(*ir.module,llvm::ReturnStatusAction,&verifyErr)) |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
97 { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
98 error("%s", verifyErr.c_str()); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
99 fatal(); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
100 } |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
101 else { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
102 Logger::println("Verification passed!"); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
103 } |
1 | 104 } |
105 | |
106 // run passes | |
107 // TODO | |
108 | |
52 | 109 // write bytecode |
110 { | |
111 Logger::println("Writing LLVM bitcode\n"); | |
112 std::ofstream bos(bcfile->name->toChars(), std::ios::binary); | |
113 llvm::WriteBitcodeToFile(ir.module, bos); | |
114 } | |
1 | 115 |
52 | 116 // disassemble ? |
117 if (global.params.disassemble) { | |
118 Logger::println("Writing LLVM asm to: %s\n", llfile->name->toChars()); | |
119 std::ofstream aos(llfile->name->toChars()); | |
120 ir.module->print(aos); | |
121 } | |
1 | 122 |
123 delete ir.module; | |
124 gIR = NULL; | |
125 } | |
126 | |
127 /* ================================================================== */ | |
128 | |
129 // Put out instance of ModuleInfo for this Module | |
130 | |
131 void Module::genmoduleinfo() | |
132 { | |
133 } | |
134 | |
135 /* ================================================================== */ | |
136 | |
137 void Dsymbol::toObjFile() | |
138 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
139 Logger::println("Ignoring Dsymbol::toObjFile for %s", toChars()); |
1 | 140 } |
141 | |
142 /* ================================================================== */ | |
143 | |
144 void Declaration::toObjFile() | |
145 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
146 Logger::println("Ignoring Declaration::toObjFile for %s", toChars()); |
1 | 147 } |
148 | |
149 /* ================================================================== */ | |
150 | |
6 | 151 /// Returns the LLVM style index from a DMD style offset |
73 | 152 size_t AggregateDeclaration::offsetToIndex(Type* t, unsigned os, std::vector<unsigned>& result) |
1 | 153 { |
24
25bb577878e8
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
lindquist
parents:
23
diff
changeset
|
154 Logger::println("checking for offset %u type %s:", os, t->toChars()); |
23 | 155 LOG_SCOPE; |
1 | 156 for (unsigned i=0; i<fields.dim; ++i) { |
157 VarDeclaration* vd = (VarDeclaration*)fields.data[i]; | |
40 | 158 Type* vdtype = LLVM_DtoDType(vd->type); |
159 Logger::println("found %u type %s", vd->offset, vdtype->toChars()); | |
160 if (os == vd->offset && vdtype == t) { | |
73 | 161 assert(vd->llvmFieldIndex >= 0); |
162 result.push_back(vd->llvmFieldIndex); | |
163 return vd->llvmFieldIndexOffset; | |
23 | 164 } |
40 | 165 else if (vdtype->ty == Tstruct && (vd->offset + vdtype->size()) > os) { |
166 TypeStruct* ts = (TypeStruct*)vdtype; | |
24
25bb577878e8
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
lindquist
parents:
23
diff
changeset
|
167 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
|
168 result.push_back(i); |
73 | 169 return sd->offsetToIndex(t, os - vd->offset, result); |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
170 } |
1 | 171 } |
73 | 172 //assert(0 && "Offset not found in any aggregate field"); |
173 return (size_t)-1; | |
1 | 174 } |
175 | |
176 /* ================================================================== */ | |
177 | |
178 static unsigned LLVM_ClassOffsetToIndex(ClassDeclaration* cd, unsigned os, unsigned& idx) | |
179 { | |
180 // start at the bottom of the inheritance chain | |
181 if (cd->baseClass != 0) { | |
182 unsigned o = LLVM_ClassOffsetToIndex(cd->baseClass, os, idx); | |
183 if (o != (unsigned)-1) | |
184 return o; | |
185 } | |
186 | |
187 // check this class | |
188 unsigned i; | |
189 for (i=0; i<cd->fields.dim; ++i) { | |
190 VarDeclaration* vd = (VarDeclaration*)cd->fields.data[i]; | |
191 if (os == vd->offset) | |
192 return i+idx; | |
193 } | |
194 idx += i; | |
195 | |
196 return (unsigned)-1; | |
197 } | |
198 | |
6 | 199 /// Returns the LLVM style index from a DMD style offset |
200 /// Handles class inheritance | |
73 | 201 size_t ClassDeclaration::offsetToIndex(Type* t, unsigned os, std::vector<unsigned>& result) |
1 | 202 { |
203 unsigned idx = 0; | |
204 unsigned r = LLVM_ClassOffsetToIndex(this, os, idx); | |
205 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
|
206 result.push_back(r+1); // vtable is 0 |
73 | 207 return 0; |
1 | 208 } |
209 | |
210 /* ================================================================== */ | |
211 | |
212 void InterfaceDeclaration::toObjFile() | |
213 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
214 Logger::println("Ignoring InterfaceDeclaration::toObjFile for %s", toChars()); |
1 | 215 } |
216 | |
217 /* ================================================================== */ | |
218 | |
219 void StructDeclaration::toObjFile() | |
220 { | |
40 | 221 TypeStruct* ts = (TypeStruct*)LLVM_DtoDType(type); |
1 | 222 if (llvmType != 0) |
223 return; | |
224 | |
225 static int sdi = 0; | |
226 Logger::print("StructDeclaration::toObjFile(%d): %s\n", sdi++, toChars()); | |
227 LOG_SCOPE; | |
228 | |
229 gIR->structs.push_back(IRStruct(ts)); | |
230 | |
231 for (int k=0; k < members->dim; k++) { | |
232 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
233 dsym->toObjFile(); |
1 | 234 } |
235 | |
73 | 236 Logger::println("doing struct fields"); |
237 | |
238 llvm::StructType* structtype = 0; | |
239 std::vector<llvm::Constant*> fieldinits; | |
240 | |
241 if (gIR->topstruct().offsets.empty()) | |
242 { | |
243 std::vector<const llvm::Type*> fieldtypes; | |
244 Logger::println("has no fields"); | |
245 fieldtypes.push_back(llvm::Type::Int8Ty); | |
246 fieldinits.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false)); | |
247 structtype = llvm::StructType::get(fieldtypes); | |
248 } | |
249 else | |
23 | 250 { |
73 | 251 Logger::println("has fields"); |
252 std::vector<const llvm::Type*> fieldtypes; | |
253 unsigned prevsize = (unsigned)-1; | |
254 unsigned lastoffset = (unsigned)-1; | |
255 const llvm::Type* fieldtype = NULL; | |
256 llvm::Constant* fieldinit = NULL; | |
257 size_t fieldpad = 0; | |
258 int idx = 0; | |
259 for (IRStruct::OffsetMap::iterator i=gIR->topstruct().offsets.begin(); i!=gIR->topstruct().offsets.end(); ++i) { | |
260 // first iteration | |
261 if (lastoffset == (unsigned)-1) { | |
262 lastoffset = i->first; | |
263 assert(lastoffset == 0); | |
264 fieldtype = LLVM_DtoType(i->second.var->type); | |
265 fieldinit = i->second.init; | |
266 prevsize = gTargetData->getTypeSize(fieldtype); | |
267 i->second.var->llvmFieldIndex = idx; | |
268 } | |
269 // colliding offset? | |
270 else if (lastoffset == i->first) { | |
271 const llvm::Type* t = LLVM_DtoType(i->second.var->type); | |
272 size_t s = gTargetData->getTypeSize(t); | |
273 if (s > prevsize) { | |
274 fieldpad = s - prevsize; | |
275 prevsize = s; | |
276 } | |
277 llvmHasUnions = true; | |
278 i->second.var->llvmFieldIndex = idx; | |
279 } | |
280 // intersecting offset? | |
281 else if (i->first < (lastoffset + prevsize)) { | |
282 const llvm::Type* t = LLVM_DtoType(i->second.var->type); | |
283 size_t s = gTargetData->getTypeSize(t); | |
284 assert((i->first + s) <= (lastoffset + prevsize)); // this holds because all types are aligned to their size | |
285 llvmHasUnions = true; | |
286 i->second.var->llvmFieldIndex = idx; | |
287 i->second.var->llvmFieldIndexOffset = (i->first - lastoffset) / s; | |
288 } | |
289 // fresh offset | |
290 else { | |
291 // commit the field | |
292 fieldtypes.push_back(fieldtype); | |
293 fieldinits.push_back(fieldinit); | |
294 if (fieldpad) { | |
295 // match up with below | |
296 std::vector<llvm::Constant*> vals(fieldpad, llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false)); | |
297 llvm::Constant* c = llvm::ConstantArray::get(llvm::ArrayType::get(llvm::Type::Int8Ty, fieldpad), vals); | |
298 fieldtypes.push_back(c->getType()); | |
299 fieldinits.push_back(c); | |
300 idx++; | |
301 } | |
302 | |
303 idx++; | |
304 | |
305 // start new | |
306 lastoffset = i->first; | |
307 fieldtype = LLVM_DtoType(i->second.var->type); | |
308 fieldinit = i->second.init; | |
309 prevsize = gTargetData->getTypeSize(fieldtype); | |
310 i->second.var->llvmFieldIndex = idx; | |
311 fieldpad = 0; | |
312 } | |
313 } | |
314 fieldtypes.push_back(fieldtype); | |
315 fieldinits.push_back(fieldinit); | |
316 if (fieldpad) { | |
317 // match up with above | |
318 std::vector<llvm::Constant*> vals(fieldpad, llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false)); | |
319 llvm::Constant* c = llvm::ConstantArray::get(llvm::ArrayType::get(llvm::Type::Int8Ty, fieldpad), vals); | |
320 fieldtypes.push_back(c->getType()); | |
321 fieldinits.push_back(c); | |
322 } | |
323 | |
324 Logger::println("creating struct type"); | |
325 structtype = llvm::StructType::get(fieldtypes); | |
23 | 326 } |
327 | |
1 | 328 // refine abstract types for stuff like: struct S{S* next;} |
329 if (gIR->topstruct().recty != 0) | |
330 { | |
331 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
332 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
333 structtype = llvm::cast<llvm::StructType>(pa.get()); | |
334 } | |
335 | |
336 ts->llvmType = structtype; | |
337 llvmType = structtype; | |
338 | |
339 if (parent->isModule()) { | |
340 gIR->module->addTypeName(mangle(),ts->llvmType); | |
341 } | |
342 | |
343 // generate static data | |
344 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
345 llvm::Constant* _init = 0; | |
346 | |
347 // always generate the constant initalizer | |
348 if (!zeroInit) { | |
349 Logger::println("Not zero initialized"); | |
73 | 350 //assert(tk == gIR->gIR->topstruct()().size()); |
1 | 351 #ifndef LLVMD_NO_LOGGER |
73 | 352 Logger::cout() << "struct type: " << *structtype << '\n'; |
353 for (size_t k=0; k<fieldinits.size(); ++k) { | |
1 | 354 Logger::cout() << "Type:" << '\n'; |
73 | 355 Logger::cout() << *fieldinits[k]->getType() << '\n'; |
1 | 356 Logger::cout() << "Value:" << '\n'; |
73 | 357 Logger::cout() << *fieldinits[k] << '\n'; |
1 | 358 } |
359 Logger::cout() << "Initializer printed" << '\n'; | |
360 #endif | |
73 | 361 llvmInitZ = llvm::ConstantStruct::get(structtype,fieldinits); |
1 | 362 } |
363 else { | |
364 Logger::println("Zero initialized"); | |
365 llvmInitZ = llvm::ConstantAggregateZero::get(structtype); | |
366 } | |
367 | |
368 // only provide the constant initializer for the defining module | |
369 if (getModule() == gIR->dmodule) | |
370 { | |
371 _init = llvmInitZ; | |
372 } | |
373 | |
73 | 374 std::string initname("_D"); |
375 initname.append(mangle()); | |
376 initname.append("6__initZ"); | |
1 | 377 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, _init, initname, gIR->module); |
378 ts->llvmInit = initvar; | |
379 | |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
380 // generate member function definitions |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
381 gIR->topstruct().queueFuncs = false; |
73 | 382 IRStruct::FuncDeclVector& mfs = gIR->topstruct().funcs; |
1 | 383 size_t n = mfs.size(); |
384 for (size_t i=0; i<n; ++i) { | |
385 mfs[i]->toObjFile(); | |
386 } | |
387 | |
388 llvmDModule = gIR->dmodule; | |
389 | |
390 gIR->structs.pop_back(); | |
391 | |
392 // generate typeinfo | |
73 | 393 if (getModule() == gIR->dmodule && llvmInternal != LLVMnotypeinfo) |
72 | 394 type->getTypeInfo(NULL); |
1 | 395 } |
396 | |
397 /* ================================================================== */ | |
398 | |
399 static void LLVM_AddBaseClassData(BaseClasses* bcs) | |
400 { | |
401 // add base class data members first | |
402 for (int j=0; j<bcs->dim; j++) | |
403 { | |
404 BaseClass* bc = (BaseClass*)(bcs->data[j]); | |
405 assert(bc); | |
40 | 406 Logger::println("Adding base class members of %s", bc->base->toChars()); |
407 LOG_SCOPE; | |
408 | |
1 | 409 LLVM_AddBaseClassData(&bc->base->baseclasses); |
410 for (int k=0; k < bc->base->members->dim; k++) { | |
411 Dsymbol* dsym = (Dsymbol*)(bc->base->members->data[k]); | |
412 if (dsym->isVarDeclaration()) | |
413 { | |
414 dsym->toObjFile(); | |
415 } | |
416 } | |
417 } | |
418 } | |
419 | |
420 void ClassDeclaration::toObjFile() | |
421 { | |
40 | 422 TypeClass* ts = (TypeClass*)LLVM_DtoDType(type); |
1 | 423 if (ts->llvmType != 0 || llvmInProgress) |
424 return; | |
425 | |
426 llvmInProgress = true; | |
427 | |
428 static int fdi = 0; | |
429 Logger::print("ClassDeclaration::toObjFile(%d): %s\n", fdi++, toChars()); | |
430 LOG_SCOPE; | |
431 | |
6 | 432 gIR->structs.push_back(IRStruct(ts)); |
1 | 433 gIR->classes.push_back(this); |
434 | |
435 // add vtable | |
9 | 436 llvm::PATypeHolder pa = llvm::OpaqueType::get(); |
437 const llvm::Type* vtabty = llvm::PointerType::get(pa); | |
73 | 438 |
439 std::vector<const llvm::Type*> fieldtypes; | |
440 fieldtypes.push_back(vtabty); | |
441 | |
442 std::vector<llvm::Constant*> fieldinits; | |
443 fieldinits.push_back(0); | |
1 | 444 |
445 // base classes first | |
446 LLVM_AddBaseClassData(&baseclasses); | |
447 | |
448 // then add own members | |
449 for (int k=0; k < members->dim; k++) { | |
450 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
451 dsym->toObjFile(); | |
452 } | |
453 | |
73 | 454 // fill out fieldtypes/inits |
455 for (IRStruct::OffsetMap::iterator i=gIR->topstruct().offsets.begin(); i!=gIR->topstruct().offsets.end(); ++i) { | |
456 fieldtypes.push_back(LLVM_DtoType(i->second.var->type)); | |
457 fieldinits.push_back(i->second.init); | |
458 } | |
459 | |
460 llvm::StructType* structtype = llvm::StructType::get(fieldtypes); | |
6 | 461 // refine abstract types for stuff like: class C {C next;} |
462 if (gIR->topstruct().recty != 0) | |
463 { | |
464 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
465 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
466 structtype = llvm::cast<llvm::StructType>(pa.get()); | |
467 } | |
468 | |
1 | 469 ts->llvmType = structtype; |
470 llvmType = structtype; | |
471 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
472 bool needs_definition = false; |
1 | 473 if (parent->isModule()) { |
474 gIR->module->addTypeName(mangle(),ts->llvmType); | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
475 needs_definition = (getModule() == gIR->dmodule); |
1 | 476 } |
477 else { | |
478 assert(0 && "class parent is not a module"); | |
479 } | |
480 | |
9 | 481 // generate vtable |
482 llvm::GlobalVariable* svtblVar = 0; | |
483 std::vector<llvm::Constant*> sinits; | |
484 std::vector<const llvm::Type*> sinits_ty; | |
485 sinits.reserve(vtbl.dim); | |
486 sinits_ty.reserve(vtbl.dim); | |
487 | |
488 for (int k=0; k < vtbl.dim; k++) | |
489 { | |
490 Dsymbol* dsym = (Dsymbol*)vtbl.data[k]; | |
491 assert(dsym); | |
492 //Logger::cout() << "vtblsym: " << dsym->toChars() << '\n'; | |
493 | |
494 if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { | |
495 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
|
496 assert(fd->llvmValue); |
9 | 497 llvm::Constant* c = llvm::cast<llvm::Constant>(fd->llvmValue); |
498 sinits.push_back(c); | |
499 sinits_ty.push_back(c->getType()); | |
500 } | |
501 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { | |
502 const llvm::Type* cty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
503 llvm::Constant* c = llvm::Constant::getNullValue(cty); | |
504 sinits.push_back(c); | |
505 sinits_ty.push_back(cty); | |
506 } | |
507 else | |
508 assert(0); | |
509 } | |
510 | |
511 const llvm::StructType* svtbl_ty = 0; | |
512 if (!sinits.empty()) | |
513 { | |
514 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
515 | |
52 | 516 std::string varname("_D"); |
517 varname.append(mangle()); | |
518 varname.append("6__vtblZ"); | |
519 | |
9 | 520 std::string styname(mangle()); |
521 styname.append("__vtblTy"); | |
522 | |
523 svtbl_ty = llvm::StructType::get(sinits_ty); | |
524 gIR->module->addTypeName(styname, svtbl_ty); | |
525 svtblVar = new llvm::GlobalVariable(svtbl_ty, true, _linkage, 0, varname, gIR->module); | |
526 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
527 llvmConstVtbl = llvm::cast<llvm::ConstantStruct>(llvm::ConstantStruct::get(svtbl_ty, sinits)); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
528 if (needs_definition) |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
529 svtblVar->setInitializer(llvmConstVtbl); |
9 | 530 llvmVtbl = svtblVar; |
531 } | |
532 | |
533 //////////////////////////////////////////////////////////////////////////////// | |
534 | |
535 // refine for final vtable type | |
536 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(svtbl_ty); | |
537 svtbl_ty = llvm::cast<llvm::StructType>(pa.get()); | |
538 structtype = llvm::cast<llvm::StructType>(gIR->topstruct().recty.get()); | |
539 ts->llvmType = structtype; | |
540 llvmType = structtype; | |
541 | |
542 // generate initializer | |
543 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
544 llvm::Constant* _init = 0; | |
545 | |
546 // first field is always the vtable | |
547 assert(svtblVar != 0); | |
73 | 548 fieldinits[0] = svtblVar; |
9 | 549 |
73 | 550 llvmInitZ = _init = llvm::ConstantStruct::get(structtype,fieldinits); |
9 | 551 assert(_init); |
52 | 552 |
553 std::string initname("_D"); | |
554 initname.append(mangle()); | |
555 initname.append("6__initZ"); | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
556 //Logger::cout() << *_init << '\n'; |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
557 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, NULL, initname, gIR->module); |
9 | 558 ts->llvmInit = initvar; |
559 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
560 if (needs_definition) { |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
561 initvar->setInitializer(_init); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
562 // generate member functions |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
563 gIR->topstruct().queueFuncs = false; |
73 | 564 IRStruct::FuncDeclVector& mfs = gIR->topstruct().funcs; |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
565 size_t n = mfs.size(); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
566 for (size_t i=0; i<n; ++i) { |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
567 mfs[i]->toObjFile(); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
568 } |
1 | 569 } |
570 | |
571 gIR->classes.pop_back(); | |
572 gIR->structs.pop_back(); | |
573 | |
574 llvmInProgress = false; | |
575 } | |
576 | |
577 /****************************************** | |
578 * Get offset of base class's vtbl[] initializer from start of csym. | |
579 * Returns ~0 if not this csym. | |
580 */ | |
581 | |
582 unsigned ClassDeclaration::baseVtblOffset(BaseClass *bc) | |
583 { | |
584 return ~0; | |
585 } | |
586 | |
587 /* ================================================================== */ | |
588 | |
589 void VarDeclaration::toObjFile() | |
590 { | |
40 | 591 Logger::print("VarDeclaration::toObjFile(): %s | %s\n", toChars(), type->toChars()); |
1 | 592 LOG_SCOPE; |
593 llvm::Module* M = gIR->module; | |
594 | |
595 // handle bind pragma | |
596 if (llvmInternal == LLVMbind) { | |
597 Logger::println("var is bound: %s", llvmInternal1); | |
598 llvmValue = M->getGlobalVariable(llvmInternal1); | |
599 assert(llvmValue); | |
600 return; | |
601 } | |
602 | |
603 // global variable or magic | |
40 | 604 if (isDataseg() || parent->isModule()) |
1 | 605 { |
40 | 606 if (llvmTouched) return; |
607 else llvmTouched = true; | |
608 | |
1 | 609 bool _isconst = isConst(); |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
610 |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
611 llvm::GlobalValue::LinkageTypes _linkage; |
52 | 612 if (parent && parent->isFuncDeclaration()) |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
613 _linkage = llvm::GlobalValue::InternalLinkage; |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
614 else |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
615 _linkage = LLVM_DtoLinkage(protection, storage_class); |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
616 |
40 | 617 Type* t = LLVM_DtoDType(type); |
618 | |
619 const llvm::Type* _type = LLVM_DtoType(t); | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
620 assert(_type); |
1 | 621 |
622 llvm::Constant* _init = 0; | |
623 bool _signed = !type->isunsigned(); | |
624 | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
625 Logger::println("Creating global variable"); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
626 std::string _name(mangle()); |
52 | 627 |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
628 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
|
629 llvmValue = gvar; |
40 | 630 |
52 | 631 // if extern don't emit initializer |
632 if (!(storage_class & STCextern)) | |
633 { | |
634 _init = LLVM_DtoConstInitializer(t, init); | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
635 |
52 | 636 //Logger::cout() << "initializer: " << *_init << '\n'; |
637 if (_type != _init->getType()) { | |
638 Logger::cout() << "got type '" << *_init->getType() << "' expected '" << *_type << "'\n"; | |
639 // zero initalizer | |
640 if (_init->isNullValue()) | |
641 _init = llvm::Constant::getNullValue(_type); | |
642 // pointer to global constant (struct.init) | |
643 else if (llvm::isa<llvm::GlobalVariable>(_init)) | |
644 { | |
645 assert(_init->getType()->getContainedType(0) == _type); | |
646 llvm::GlobalVariable* gv = llvm::cast<llvm::GlobalVariable>(_init); | |
647 assert(t->ty == Tstruct); | |
648 TypeStruct* ts = (TypeStruct*)t; | |
649 assert(ts->sym->llvmInitZ); | |
650 _init = ts->sym->llvmInitZ; | |
651 } | |
652 // array single value init | |
653 else if (llvm::isa<llvm::ArrayType>(_type)) | |
654 { | |
69
2b5a2eaa88be
[svn r73] Identity expression for dynamic array and null was broken.
lindquist
parents:
68
diff
changeset
|
655 _init = LLVM_DtoConstStaticArray(_type, _init); |
52 | 656 } |
657 else { | |
658 Logger::cout() << "Unexpected initializer type: " << *_type << '\n'; | |
659 //assert(0); | |
660 } | |
1 | 661 } |
52 | 662 |
64
b688ad419f8c
[svn r68] Added support for multi-dimensional static arrays.
lindquist
parents:
63
diff
changeset
|
663 Logger::cout() << "final init = " << *_init << '\n'; |
52 | 664 gvar->setInitializer(_init); |
1 | 665 } |
666 | |
40 | 667 llvmDModule = gIR->dmodule; |
1 | 668 |
669 //if (storage_class & STCprivate) | |
670 // gvar->setVisibility(llvm::GlobalValue::ProtectedVisibility); | |
671 } | |
672 | |
673 // inside aggregate declaration. declare a field. | |
674 else | |
675 { | |
676 Logger::println("Aggregate var declaration: '%s' offset=%d", toChars(), offset); | |
677 | |
40 | 678 Type* t = LLVM_DtoDType(type); |
679 const llvm::Type* _type = LLVM_DtoType(t); | |
1 | 680 |
40 | 681 llvm::Constant*_init = LLVM_DtoConstInitializer(t, init); |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
682 assert(_init); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
683 Logger::cout() << "field init is: " << *_init << " type should be " << *_type << '\n'; |
40 | 684 if (_type != _init->getType()) |
1 | 685 { |
40 | 686 if (t->ty == Tsarray) |
1 | 687 { |
688 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(_type); | |
689 uint64_t n = arrty->getNumElements(); | |
690 std::vector<llvm::Constant*> vals(n,_init); | |
691 _init = llvm::ConstantArray::get(arrty, vals); | |
692 } | |
40 | 693 else if (t->ty == Tarray) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
694 { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
695 assert(llvm::isa<llvm::StructType>(_type)); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
696 _init = llvm::ConstantAggregateZero::get(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
697 } |
40 | 698 else if (t->ty == Tstruct) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
699 { |
6 | 700 const llvm::StructType* structty = llvm::cast<llvm::StructType>(_type); |
40 | 701 TypeStruct* ts = (TypeStruct*)t; |
9 | 702 assert(ts); |
703 assert(ts->sym); | |
6 | 704 assert(ts->sym->llvmInitZ); |
705 _init = ts->sym->llvmInitZ; | |
706 } | |
40 | 707 else if (t->ty == Tclass) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
708 { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
709 _init = llvm::Constant::getNullValue(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
710 } |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
711 else { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
712 Logger::println("failed for type %s", type->toChars()); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
713 assert(0); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
714 } |
1 | 715 } |
73 | 716 |
717 // add the field in the IRStruct | |
718 gIR->topstruct().offsets.insert(std::make_pair(offset, IRStruct::Offset(this,_init))); | |
1 | 719 } |
720 | |
721 Logger::println("VarDeclaration::toObjFile is done"); | |
722 } | |
723 | |
724 /* ================================================================== */ | |
725 | |
726 void TypedefDeclaration::toObjFile() | |
727 { | |
728 static int tdi = 0; | |
729 Logger::print("TypedefDeclaration::toObjFile(%d): %s\n", tdi++, toChars()); | |
730 LOG_SCOPE; | |
731 | |
52 | 732 // generate typeinfo |
72 | 733 type->getTypeInfo(NULL); |
1 | 734 } |
735 | |
736 /* ================================================================== */ | |
737 | |
738 void EnumDeclaration::toObjFile() | |
739 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
740 Logger::println("Ignoring EnumDeclaration::toObjFile for %s", toChars()); |
1 | 741 } |
742 | |
743 /* ================================================================== */ | |
744 | |
745 void FuncDeclaration::toObjFile() | |
746 { | |
18 | 747 if (llvmDModule) { |
9 | 748 assert(llvmValue != 0); |
1 | 749 return; |
750 } | |
751 | |
63
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
752 if (isUnitTestDeclaration()) { |
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
753 Logger::println("*** ATTENTION: ignoring unittest declaration: %s", toChars()); |
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
754 return; |
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
755 } |
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
756 |
55
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
757 Type* t = LLVM_DtoDType(type); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
758 TypeFunction* f = (TypeFunction*)t; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
759 |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
760 bool declareOnly = false; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
761 if (TemplateInstance* tinst = parent->isTemplateInstance()) { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
762 TemplateDeclaration* tempdecl = tinst->tempdecl; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
763 if (tempdecl->llvmInternal == LLVMva_start) |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
764 { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
765 Logger::println("magic va_start found"); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
766 llvmInternal = LLVMva_start; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
767 declareOnly = true; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
768 } |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
769 else if (tempdecl->llvmInternal == LLVMva_arg) |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
770 { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
771 Logger::println("magic va_arg found"); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
772 llvmInternal = LLVMva_arg; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
773 return; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
774 } |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
775 } |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
776 |
9 | 777 llvm::Function* func = LLVM_DtoDeclareFunction(this); |
778 | |
55
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
779 if (declareOnly) |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
780 return; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
781 |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
782 if (!gIR->structs.empty() && gIR->topstruct().queueFuncs) { |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
783 if (!llvmQueued) { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
784 Logger::println("queueing %s", toChars()); |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
785 gIR->topstruct().funcs.push_back(this); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
786 llvmQueued = true; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
787 } |
10 | 788 return; // we wait with the definition as they might invoke a virtual method and the vtable is not yet complete |
1 | 789 } |
790 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
791 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
|
792 const llvm::FunctionType* functype = llvm::cast<llvm::FunctionType>(llvmValue->getType()->getContainedType(0)); |
1 | 793 |
794 // only members of the current module maybe be defined | |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
795 if (getModule() == gIR->dmodule || parent->isTemplateInstance()) |
1 | 796 { |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
797 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
|
798 |
1 | 799 bool allow_fbody = true; |
800 // handle static constructor / destructor | |
801 if (isStaticCtorDeclaration() || isStaticDtorDeclaration()) { | |
802 const llvm::ArrayType* sctor_type = llvm::ArrayType::get(llvm::PointerType::get(functype),1); | |
803 //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
|
804 |
1 | 805 llvm::Constant* sctor_func = llvm::cast<llvm::Constant>(llvmValue); |
806 //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
|
807 |
1 | 808 llvm::Constant* sctor_init = 0; |
809 if (llvmInternal == LLVMnull) | |
810 { | |
811 llvm::Constant* sctor_init_null = llvm::Constant::getNullValue(sctor_func->getType()); | |
812 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_init_null,1); | |
813 allow_fbody = false; | |
814 } | |
815 else | |
816 { | |
817 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_func,1); | |
818 } | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
819 |
1 | 820 //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
|
821 |
1 | 822 // output the llvm.global_ctors array |
823 const char* varname = isStaticCtorDeclaration() ? "_d_module_ctor_array" : "_d_module_dtor_array"; | |
824 llvm::GlobalVariable* sctor_arr = new llvm::GlobalVariable(sctor_type, false, llvm::GlobalValue::AppendingLinkage, sctor_init, varname, gIR->module); | |
825 } | |
826 | |
827 // function definition | |
828 if (allow_fbody && fbody != 0) | |
829 { | |
40 | 830 gIR->functions.push_back(IRFunction(this)); |
831 gIR->func().func = func; | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
832 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
833 // 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
|
834 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
|
835 |
52 | 836 //Logger::cout() << "func type: " << *f->llvmType << '\n'; |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
837 |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
838 // this handling |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
839 if (f->llvmUsesThis) { |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
840 Logger::println("uses this"); |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
841 if (f->llvmRetInPtr) |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
842 llvmThisVar = ++func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
843 else |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
844 llvmThisVar = func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
845 assert(llvmThisVar != 0); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
846 } |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
847 |
9 | 848 if (isMain()) |
849 gIR->emitMain = true; | |
850 | |
40 | 851 llvm::BasicBlock* beginbb = new llvm::BasicBlock("entry",func); |
852 llvm::BasicBlock* endbb = new llvm::BasicBlock("endentry",func); | |
1 | 853 |
854 //assert(gIR->scopes.empty()); | |
40 | 855 gIR->scopes.push_back(IRScope(beginbb, endbb)); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
856 |
1 | 857 // create alloca point |
858 f->llvmAllocaPoint = new llvm::BitCastInst(llvm::ConstantInt::get(llvm::Type::Int32Ty,0,false),llvm::Type::Int32Ty,"alloca point",gIR->scopebb()); | |
40 | 859 gIR->func().allocapoint = f->llvmAllocaPoint; |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
860 |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
861 llvm::Value* parentNested = NULL; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
862 if (FuncDeclaration* fd = toParent()->isFuncDeclaration()) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
863 parentNested = fd->llvmNested; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
864 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
865 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
866 // construct nested variables struct |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
867 if (!llvmNestedVars.empty() || parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
868 std::vector<const llvm::Type*> nestTypes; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
869 int j = 0; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
870 if (parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
871 nestTypes.push_back(parentNested->getType()); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
872 j++; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
873 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
874 for (std::set<VarDeclaration*>::iterator i=llvmNestedVars.begin(); i!=llvmNestedVars.end(); ++i) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
875 VarDeclaration* vd = *i; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
876 vd->llvmNestedIndex = j++; |
67
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
877 if (vd->isParameter()) { |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
878 assert(vd->llvmValue); |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
879 nestTypes.push_back(vd->llvmValue->getType()); |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
880 } |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
881 else { |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
882 nestTypes.push_back(LLVM_DtoType(vd->type)); |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
883 } |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
884 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
885 const llvm::StructType* nestSType = llvm::StructType::get(nestTypes); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
886 Logger::cout() << "nested var struct has type:" << '\n' << *nestSType; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
887 llvmNested = new llvm::AllocaInst(nestSType,"nestedvars",f->llvmAllocaPoint); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
888 if (parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
889 assert(llvmThisVar); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
890 llvm::Value* ptr = gIR->ir->CreateBitCast(llvmThisVar, parentNested->getType(), "tmp"); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
891 gIR->ir->CreateStore(ptr, LLVM_DtoGEPi(llvmNested, 0,0, "tmp")); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
892 } |
67
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
893 for (std::set<VarDeclaration*>::iterator i=llvmNestedVars.begin(); i!=llvmNestedVars.end(); ++i) { |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
894 VarDeclaration* vd = *i; |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
895 if (vd->isParameter()) { |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
896 gIR->ir->CreateStore(vd->llvmValue, LLVM_DtoGEPi(llvmNested, 0, vd->llvmNestedIndex, "tmp")); |
68
c4b3f5d2cd9b
[svn r72] Calling a nested function that is not a delegate was not working.
lindquist
parents:
67
diff
changeset
|
897 vd->llvmValue = llvmNested; |
67
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
898 } |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
899 } |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
900 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
901 |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
902 // copy _argptr to a memory location |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
903 if (f->linkage == LINKd && f->varargs == 1) |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
904 { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
905 llvm::Value* argptrmem = new llvm::AllocaInst(llvmArgPtr->getType(), "_argptrmem", gIR->topallocapoint()); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
906 new llvm::StoreInst(llvmArgPtr, argptrmem, gIR->scopebb()); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
907 llvmArgPtr = argptrmem; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
908 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
909 |
1 | 910 // output function body |
911 fbody->toIR(gIR); | |
912 | |
913 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
914 // in automatically, so we do it here. | |
40 | 915 if (!isMain()) { |
52 | 916 if (!gIR->scopereturned()) { |
40 | 917 // pass the previous block into this block |
918 //new llvm::BranchInst(irs.end, irs.begin); | |
919 if (func->getReturnType() == llvm::Type::VoidTy) { | |
920 new llvm::ReturnInst(gIR->scopebb()); | |
921 } | |
52 | 922 else { |
923 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
924 } | |
40 | 925 } |
1 | 926 } |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
927 |
1 | 928 // erase alloca point |
929 f->llvmAllocaPoint->eraseFromParent(); | |
930 f->llvmAllocaPoint = 0; | |
40 | 931 gIR->func().allocapoint = 0; |
1 | 932 |
933 gIR->scopes.pop_back(); | |
934 | |
935 // get rid of the endentry block, it's never used | |
9 | 936 assert(!func->getBasicBlockList().empty()); |
1 | 937 func->getBasicBlockList().pop_back(); |
938 | |
939 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
9 | 940 // would be nice to figure out how to assert that this is correct |
1 | 941 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); |
942 if (lastbb->empty()) { | |
52 | 943 if (lastbb->getNumUses() == 0) |
944 lastbb->eraseFromParent(); | |
945 else { | |
946 new llvm::UnreachableInst(lastbb); | |
947 /*if (func->getReturnType() == llvm::Type::VoidTy) { | |
948 new llvm::ReturnInst(lastbb); | |
949 } | |
950 else { | |
951 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), lastbb); | |
952 }*/ | |
953 } | |
1 | 954 } |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
955 |
40 | 956 gIR->functions.pop_back(); |
1 | 957 } |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
958 |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
959 // template instances should have weak linkage |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
960 if (parent->isTemplateInstance()) { |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
961 func->setLinkage(llvm::GlobalValue::WeakLinkage); |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
962 } |
1 | 963 } |
964 } |