Mercurial > projects > ldc
annotate gen/toobj.c @ 72:d7e764e62462 trunk
[svn r76] Fixed: TypeInfo for structs.
Fixed: PostExp was unable to allocate storage for parameters.
Fixed: Many types of functions and delegates were broken.
Misc cleanups.
author | lindquist |
---|---|
date | Mon, 29 Oct 2007 03:28:12 +0100 |
parents | 2b5a2eaa88be |
children | b706170e24a9 |
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 | |
68 gTargetData = new llvm::TargetData(ir.module); | |
69 | |
6 | 70 // process module members |
1 | 71 for (int k=0; k < members->dim; k++) { |
72 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
73 assert(dsym); | |
74 dsym->toObjFile(); | |
75 } | |
76 | |
77 delete gTargetData; | |
78 gTargetData = 0; | |
79 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
80 // 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
|
81 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
|
82 LLVM_DtoMain(); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
83 } |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
84 |
6 | 85 // verify the llvm |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
86 if (!global.params.novalidate) { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
87 std::string verifyErr; |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
88 Logger::println("Verifying module..."); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
89 if (llvm::verifyModule(*ir.module,llvm::ReturnStatusAction,&verifyErr)) |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
90 { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
91 error("%s", verifyErr.c_str()); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
92 fatal(); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
93 } |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
94 else { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
95 Logger::println("Verification passed!"); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
96 } |
1 | 97 } |
98 | |
99 // run passes | |
100 // TODO | |
101 | |
52 | 102 // write bytecode |
103 { | |
104 Logger::println("Writing LLVM bitcode\n"); | |
105 std::ofstream bos(bcfile->name->toChars(), std::ios::binary); | |
106 llvm::WriteBitcodeToFile(ir.module, bos); | |
107 } | |
1 | 108 |
52 | 109 // disassemble ? |
110 if (global.params.disassemble) { | |
111 Logger::println("Writing LLVM asm to: %s\n", llfile->name->toChars()); | |
112 std::ofstream aos(llfile->name->toChars()); | |
113 ir.module->print(aos); | |
114 } | |
1 | 115 |
116 delete ir.module; | |
117 gIR = NULL; | |
118 } | |
119 | |
120 /* ================================================================== */ | |
121 | |
122 // Put out instance of ModuleInfo for this Module | |
123 | |
124 void Module::genmoduleinfo() | |
125 { | |
126 } | |
127 | |
128 /* ================================================================== */ | |
129 | |
130 void Dsymbol::toObjFile() | |
131 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
132 Logger::println("Ignoring Dsymbol::toObjFile for %s", toChars()); |
1 | 133 } |
134 | |
135 /* ================================================================== */ | |
136 | |
137 void Declaration::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 Declaration::toObjFile for %s", toChars()); |
1 | 140 } |
141 | |
142 /* ================================================================== */ | |
143 | |
6 | 144 /// 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
|
145 void AggregateDeclaration::offsetToIndex(Type* t, unsigned os, std::vector<unsigned>& result) |
1 | 146 { |
24
25bb577878e8
[svn r28] * Fixed accessing aggregate fields. it was still not quite right. hopefully is now :)
lindquist
parents:
23
diff
changeset
|
147 Logger::println("checking for offset %u type %s:", os, t->toChars()); |
23 | 148 LOG_SCOPE; |
1 | 149 for (unsigned i=0; i<fields.dim; ++i) { |
150 VarDeclaration* vd = (VarDeclaration*)fields.data[i]; | |
40 | 151 Type* vdtype = LLVM_DtoDType(vd->type); |
152 Logger::println("found %u type %s", vd->offset, vdtype->toChars()); | |
153 if (os == vd->offset && vdtype == t) { | |
23 | 154 result.push_back(i); |
155 return; | |
156 } | |
40 | 157 else if (vdtype->ty == Tstruct && (vd->offset + vdtype->size()) > os) { |
158 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
|
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 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
205 Logger::println("Ignoring InterfaceDeclaration::toObjFile for %s", toChars()); |
1 | 206 } |
207 | |
208 /* ================================================================== */ | |
209 | |
210 void StructDeclaration::toObjFile() | |
211 { | |
40 | 212 TypeStruct* ts = (TypeStruct*)LLVM_DtoDType(type); |
1 | 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 for (int k=0; k < members->dim; k++) { | |
223 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
224 dsym->toObjFile(); |
1 | 225 } |
226 | |
23 | 227 if (gIR->topstruct().fields.empty()) |
228 { | |
229 gIR->topstruct().fields.push_back(llvm::Type::Int8Ty); | |
230 gIR->topstruct().inits.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false)); | |
231 } | |
232 | |
1 | 233 llvm::StructType* structtype = llvm::StructType::get(gIR->topstruct().fields); |
234 | |
235 // refine abstract types for stuff like: struct S{S* next;} | |
236 if (gIR->topstruct().recty != 0) | |
237 { | |
238 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
239 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
240 structtype = llvm::cast<llvm::StructType>(pa.get()); | |
241 } | |
242 | |
243 ts->llvmType = structtype; | |
244 llvmType = structtype; | |
245 | |
246 if (parent->isModule()) { | |
247 gIR->module->addTypeName(mangle(),ts->llvmType); | |
248 } | |
249 | |
250 // generate static data | |
251 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
252 llvm::Constant* _init = 0; | |
253 | |
254 // always generate the constant initalizer | |
255 if (!zeroInit) { | |
256 Logger::println("Not zero initialized"); | |
257 //assert(tk == gIR->topstruct().size()); | |
258 #ifndef LLVMD_NO_LOGGER | |
259 Logger::cout() << *structtype << '\n'; | |
260 for (size_t k=0; k<gIR->topstruct().inits.size(); ++k) { | |
261 Logger::cout() << "Type:" << '\n'; | |
262 Logger::cout() << *gIR->topstruct().inits[k]->getType() << '\n'; | |
263 Logger::cout() << "Value:" << '\n'; | |
264 Logger::cout() << *gIR->topstruct().inits[k] << '\n'; | |
265 } | |
266 Logger::cout() << "Initializer printed" << '\n'; | |
267 #endif | |
268 llvmInitZ = llvm::ConstantStruct::get(structtype,gIR->topstruct().inits); | |
269 } | |
270 else { | |
271 Logger::println("Zero initialized"); | |
272 llvmInitZ = llvm::ConstantAggregateZero::get(structtype); | |
273 } | |
274 | |
275 // only provide the constant initializer for the defining module | |
276 if (getModule() == gIR->dmodule) | |
277 { | |
278 _init = llvmInitZ; | |
279 } | |
280 | |
281 std::string initname(mangle()); | |
282 initname.append("__initZ"); | |
283 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, _init, initname, gIR->module); | |
284 ts->llvmInit = initvar; | |
285 | |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
286 // generate member function definitions |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
287 gIR->topstruct().queueFuncs = false; |
40 | 288 IRStruct::FuncDeclVec& mfs = gIR->topstruct().funcs; |
1 | 289 size_t n = mfs.size(); |
290 for (size_t i=0; i<n; ++i) { | |
291 mfs[i]->toObjFile(); | |
292 } | |
293 | |
294 llvmDModule = gIR->dmodule; | |
295 | |
296 gIR->structs.pop_back(); | |
297 | |
298 // generate typeinfo | |
72 | 299 if (getModule() == gIR->dmodule) |
300 type->getTypeInfo(NULL); | |
1 | 301 } |
302 | |
303 /* ================================================================== */ | |
304 | |
305 static void LLVM_AddBaseClassData(BaseClasses* bcs) | |
306 { | |
307 // add base class data members first | |
308 for (int j=0; j<bcs->dim; j++) | |
309 { | |
310 BaseClass* bc = (BaseClass*)(bcs->data[j]); | |
311 assert(bc); | |
40 | 312 Logger::println("Adding base class members of %s", bc->base->toChars()); |
313 LOG_SCOPE; | |
314 | |
1 | 315 LLVM_AddBaseClassData(&bc->base->baseclasses); |
316 for (int k=0; k < bc->base->members->dim; k++) { | |
317 Dsymbol* dsym = (Dsymbol*)(bc->base->members->data[k]); | |
318 if (dsym->isVarDeclaration()) | |
319 { | |
320 dsym->toObjFile(); | |
321 } | |
322 } | |
323 } | |
324 } | |
325 | |
326 void ClassDeclaration::toObjFile() | |
327 { | |
40 | 328 TypeClass* ts = (TypeClass*)LLVM_DtoDType(type); |
1 | 329 if (ts->llvmType != 0 || llvmInProgress) |
330 return; | |
331 | |
332 llvmInProgress = true; | |
333 | |
334 static int fdi = 0; | |
335 Logger::print("ClassDeclaration::toObjFile(%d): %s\n", fdi++, toChars()); | |
336 LOG_SCOPE; | |
337 | |
6 | 338 gIR->structs.push_back(IRStruct(ts)); |
1 | 339 gIR->classes.push_back(this); |
340 | |
341 // add vtable | |
9 | 342 llvm::PATypeHolder pa = llvm::OpaqueType::get(); |
343 const llvm::Type* vtabty = llvm::PointerType::get(pa); | |
1 | 344 gIR->topstruct().fields.push_back(vtabty); |
345 gIR->topstruct().inits.push_back(0); | |
346 | |
347 // base classes first | |
348 LLVM_AddBaseClassData(&baseclasses); | |
349 | |
350 // then add own members | |
351 for (int k=0; k < members->dim; k++) { | |
352 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
353 dsym->toObjFile(); | |
354 } | |
355 | |
356 llvm::StructType* structtype = llvm::StructType::get(gIR->topstruct().fields); | |
6 | 357 // refine abstract types for stuff like: class C {C next;} |
358 if (gIR->topstruct().recty != 0) | |
359 { | |
360 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
361 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
362 structtype = llvm::cast<llvm::StructType>(pa.get()); | |
363 } | |
364 | |
1 | 365 ts->llvmType = structtype; |
366 llvmType = structtype; | |
367 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
368 bool needs_definition = false; |
1 | 369 if (parent->isModule()) { |
370 gIR->module->addTypeName(mangle(),ts->llvmType); | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
371 needs_definition = (getModule() == gIR->dmodule); |
1 | 372 } |
373 else { | |
374 assert(0 && "class parent is not a module"); | |
375 } | |
376 | |
9 | 377 // generate vtable |
378 llvm::GlobalVariable* svtblVar = 0; | |
379 std::vector<llvm::Constant*> sinits; | |
380 std::vector<const llvm::Type*> sinits_ty; | |
381 sinits.reserve(vtbl.dim); | |
382 sinits_ty.reserve(vtbl.dim); | |
383 | |
384 for (int k=0; k < vtbl.dim; k++) | |
385 { | |
386 Dsymbol* dsym = (Dsymbol*)vtbl.data[k]; | |
387 assert(dsym); | |
388 //Logger::cout() << "vtblsym: " << dsym->toChars() << '\n'; | |
389 | |
390 if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { | |
391 fd->toObjFile(); | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
392 assert(fd->llvmValue); |
9 | 393 llvm::Constant* c = llvm::cast<llvm::Constant>(fd->llvmValue); |
394 sinits.push_back(c); | |
395 sinits_ty.push_back(c->getType()); | |
396 } | |
397 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { | |
398 const llvm::Type* cty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
399 llvm::Constant* c = llvm::Constant::getNullValue(cty); | |
400 sinits.push_back(c); | |
401 sinits_ty.push_back(cty); | |
402 } | |
403 else | |
404 assert(0); | |
405 } | |
406 | |
407 const llvm::StructType* svtbl_ty = 0; | |
408 if (!sinits.empty()) | |
409 { | |
410 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
411 | |
52 | 412 std::string varname("_D"); |
413 varname.append(mangle()); | |
414 varname.append("6__vtblZ"); | |
415 | |
9 | 416 std::string styname(mangle()); |
417 styname.append("__vtblTy"); | |
418 | |
419 svtbl_ty = llvm::StructType::get(sinits_ty); | |
420 gIR->module->addTypeName(styname, svtbl_ty); | |
421 svtblVar = new llvm::GlobalVariable(svtbl_ty, true, _linkage, 0, varname, gIR->module); | |
422 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
423 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
|
424 if (needs_definition) |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
425 svtblVar->setInitializer(llvmConstVtbl); |
9 | 426 llvmVtbl = svtblVar; |
427 } | |
428 | |
429 //////////////////////////////////////////////////////////////////////////////// | |
430 | |
431 // refine for final vtable type | |
432 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(svtbl_ty); | |
433 svtbl_ty = llvm::cast<llvm::StructType>(pa.get()); | |
434 structtype = llvm::cast<llvm::StructType>(gIR->topstruct().recty.get()); | |
435 ts->llvmType = structtype; | |
436 llvmType = structtype; | |
437 | |
438 // generate initializer | |
439 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
440 llvm::Constant* _init = 0; | |
441 | |
442 // first field is always the vtable | |
443 assert(svtblVar != 0); | |
444 gIR->topstruct().inits[0] = svtblVar; | |
445 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
446 llvmInitZ = _init = llvm::ConstantStruct::get(structtype,gIR->topstruct().inits); |
9 | 447 assert(_init); |
52 | 448 |
449 std::string initname("_D"); | |
450 initname.append(mangle()); | |
451 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
|
452 //Logger::cout() << *_init << '\n'; |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
453 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, NULL, initname, gIR->module); |
9 | 454 ts->llvmInit = initvar; |
455 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
456 if (needs_definition) { |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
457 initvar->setInitializer(_init); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
458 // generate member functions |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
459 gIR->topstruct().queueFuncs = false; |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
460 IRStruct::FuncDeclVec& mfs = gIR->topstruct().funcs; |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
461 size_t n = mfs.size(); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
462 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
|
463 mfs[i]->toObjFile(); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
464 } |
1 | 465 } |
466 | |
467 gIR->classes.pop_back(); | |
468 gIR->structs.pop_back(); | |
469 | |
470 llvmInProgress = false; | |
471 } | |
472 | |
473 /****************************************** | |
474 * Get offset of base class's vtbl[] initializer from start of csym. | |
475 * Returns ~0 if not this csym. | |
476 */ | |
477 | |
478 unsigned ClassDeclaration::baseVtblOffset(BaseClass *bc) | |
479 { | |
480 return ~0; | |
481 } | |
482 | |
483 /* ================================================================== */ | |
484 | |
485 void VarDeclaration::toObjFile() | |
486 { | |
40 | 487 Logger::print("VarDeclaration::toObjFile(): %s | %s\n", toChars(), type->toChars()); |
1 | 488 LOG_SCOPE; |
489 llvm::Module* M = gIR->module; | |
490 | |
491 // handle bind pragma | |
492 if (llvmInternal == LLVMbind) { | |
493 Logger::println("var is bound: %s", llvmInternal1); | |
494 llvmValue = M->getGlobalVariable(llvmInternal1); | |
495 assert(llvmValue); | |
496 return; | |
497 } | |
498 | |
499 // global variable or magic | |
40 | 500 if (isDataseg() || parent->isModule()) |
1 | 501 { |
40 | 502 if (llvmTouched) return; |
503 else llvmTouched = true; | |
504 | |
1 | 505 bool _isconst = isConst(); |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
506 |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
507 llvm::GlobalValue::LinkageTypes _linkage; |
52 | 508 if (parent && parent->isFuncDeclaration()) |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
509 _linkage = llvm::GlobalValue::InternalLinkage; |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
510 else |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
511 _linkage = LLVM_DtoLinkage(protection, storage_class); |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
512 |
40 | 513 Type* t = LLVM_DtoDType(type); |
514 | |
515 const llvm::Type* _type = LLVM_DtoType(t); | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
516 assert(_type); |
1 | 517 |
518 llvm::Constant* _init = 0; | |
519 bool _signed = !type->isunsigned(); | |
520 | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
521 Logger::println("Creating global variable"); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
522 std::string _name(mangle()); |
52 | 523 |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
524 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
|
525 llvmValue = gvar; |
40 | 526 |
52 | 527 // if extern don't emit initializer |
528 if (!(storage_class & STCextern)) | |
529 { | |
530 _init = LLVM_DtoConstInitializer(t, init); | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
531 |
52 | 532 //Logger::cout() << "initializer: " << *_init << '\n'; |
533 if (_type != _init->getType()) { | |
534 Logger::cout() << "got type '" << *_init->getType() << "' expected '" << *_type << "'\n"; | |
535 // zero initalizer | |
536 if (_init->isNullValue()) | |
537 _init = llvm::Constant::getNullValue(_type); | |
538 // pointer to global constant (struct.init) | |
539 else if (llvm::isa<llvm::GlobalVariable>(_init)) | |
540 { | |
541 assert(_init->getType()->getContainedType(0) == _type); | |
542 llvm::GlobalVariable* gv = llvm::cast<llvm::GlobalVariable>(_init); | |
543 assert(t->ty == Tstruct); | |
544 TypeStruct* ts = (TypeStruct*)t; | |
545 assert(ts->sym->llvmInitZ); | |
546 _init = ts->sym->llvmInitZ; | |
547 } | |
548 // array single value init | |
549 else if (llvm::isa<llvm::ArrayType>(_type)) | |
550 { | |
69
2b5a2eaa88be
[svn r73] Identity expression for dynamic array and null was broken.
lindquist
parents:
68
diff
changeset
|
551 _init = LLVM_DtoConstStaticArray(_type, _init); |
52 | 552 } |
553 else { | |
554 Logger::cout() << "Unexpected initializer type: " << *_type << '\n'; | |
555 //assert(0); | |
556 } | |
1 | 557 } |
52 | 558 |
64
b688ad419f8c
[svn r68] Added support for multi-dimensional static arrays.
lindquist
parents:
63
diff
changeset
|
559 Logger::cout() << "final init = " << *_init << '\n'; |
52 | 560 gvar->setInitializer(_init); |
1 | 561 } |
562 | |
40 | 563 llvmDModule = gIR->dmodule; |
1 | 564 |
565 //if (storage_class & STCprivate) | |
566 // gvar->setVisibility(llvm::GlobalValue::ProtectedVisibility); | |
567 } | |
568 | |
569 // inside aggregate declaration. declare a field. | |
570 else | |
571 { | |
572 Logger::println("Aggregate var declaration: '%s' offset=%d", toChars(), offset); | |
573 | |
40 | 574 Type* t = LLVM_DtoDType(type); |
575 const llvm::Type* _type = LLVM_DtoType(t); | |
1 | 576 gIR->topstruct().fields.push_back(_type); |
577 | |
40 | 578 llvm::Constant*_init = LLVM_DtoConstInitializer(t, init); |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
579 assert(_init); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
580 Logger::cout() << "field init is: " << *_init << " type should be " << *_type << '\n'; |
40 | 581 if (_type != _init->getType()) |
1 | 582 { |
40 | 583 if (t->ty == Tsarray) |
1 | 584 { |
585 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(_type); | |
586 uint64_t n = arrty->getNumElements(); | |
587 std::vector<llvm::Constant*> vals(n,_init); | |
588 _init = llvm::ConstantArray::get(arrty, vals); | |
589 } | |
40 | 590 else if (t->ty == Tarray) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
591 { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
592 assert(llvm::isa<llvm::StructType>(_type)); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
593 _init = llvm::ConstantAggregateZero::get(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
594 } |
40 | 595 else if (t->ty == Tstruct) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
596 { |
6 | 597 const llvm::StructType* structty = llvm::cast<llvm::StructType>(_type); |
40 | 598 TypeStruct* ts = (TypeStruct*)t; |
9 | 599 assert(ts); |
600 assert(ts->sym); | |
6 | 601 assert(ts->sym->llvmInitZ); |
602 _init = ts->sym->llvmInitZ; | |
603 } | |
40 | 604 else if (t->ty == Tclass) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
605 { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
606 _init = llvm::Constant::getNullValue(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
607 } |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
608 else { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
609 Logger::println("failed for type %s", type->toChars()); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
610 assert(0); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
611 } |
1 | 612 } |
613 gIR->topstruct().inits.push_back(_init); | |
614 } | |
615 | |
616 Logger::println("VarDeclaration::toObjFile is done"); | |
617 } | |
618 | |
619 /* ================================================================== */ | |
620 | |
621 void TypedefDeclaration::toObjFile() | |
622 { | |
623 static int tdi = 0; | |
624 Logger::print("TypedefDeclaration::toObjFile(%d): %s\n", tdi++, toChars()); | |
625 LOG_SCOPE; | |
626 | |
52 | 627 // generate typeinfo |
72 | 628 type->getTypeInfo(NULL); |
1 | 629 } |
630 | |
631 /* ================================================================== */ | |
632 | |
633 void EnumDeclaration::toObjFile() | |
634 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
635 Logger::println("Ignoring EnumDeclaration::toObjFile for %s", toChars()); |
1 | 636 } |
637 | |
638 /* ================================================================== */ | |
639 | |
640 void FuncDeclaration::toObjFile() | |
641 { | |
18 | 642 if (llvmDModule) { |
9 | 643 assert(llvmValue != 0); |
1 | 644 return; |
645 } | |
646 | |
63
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
647 if (isUnitTestDeclaration()) { |
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
648 Logger::println("*** ATTENTION: ignoring unittest declaration: %s", toChars()); |
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
649 return; |
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
650 } |
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
651 |
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
|
652 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
|
653 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
|
654 |
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
|
655 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
|
656 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
|
657 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
|
658 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
|
659 { |
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
|
660 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
|
661 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
|
662 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
|
663 } |
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
|
664 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
|
665 { |
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
|
666 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
|
667 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
|
668 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
|
669 } |
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
|
670 } |
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
|
671 |
9 | 672 llvm::Function* func = LLVM_DtoDeclareFunction(this); |
673 | |
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
|
674 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
|
675 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
|
676 |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
677 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
|
678 if (!llvmQueued) { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
679 Logger::println("queueing %s", toChars()); |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
680 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
|
681 llvmQueued = true; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
682 } |
10 | 683 return; // we wait with the definition as they might invoke a virtual method and the vtable is not yet complete |
1 | 684 } |
685 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
686 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
|
687 const llvm::FunctionType* functype = llvm::cast<llvm::FunctionType>(llvmValue->getType()->getContainedType(0)); |
1 | 688 |
689 // only members of the current module maybe be defined | |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
690 if (getModule() == gIR->dmodule || parent->isTemplateInstance()) |
1 | 691 { |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
692 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
|
693 |
1 | 694 bool allow_fbody = true; |
695 // handle static constructor / destructor | |
696 if (isStaticCtorDeclaration() || isStaticDtorDeclaration()) { | |
697 const llvm::ArrayType* sctor_type = llvm::ArrayType::get(llvm::PointerType::get(functype),1); | |
698 //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
|
699 |
1 | 700 llvm::Constant* sctor_func = llvm::cast<llvm::Constant>(llvmValue); |
701 //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
|
702 |
1 | 703 llvm::Constant* sctor_init = 0; |
704 if (llvmInternal == LLVMnull) | |
705 { | |
706 llvm::Constant* sctor_init_null = llvm::Constant::getNullValue(sctor_func->getType()); | |
707 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_init_null,1); | |
708 allow_fbody = false; | |
709 } | |
710 else | |
711 { | |
712 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_func,1); | |
713 } | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
714 |
1 | 715 //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
|
716 |
1 | 717 // output the llvm.global_ctors array |
718 const char* varname = isStaticCtorDeclaration() ? "_d_module_ctor_array" : "_d_module_dtor_array"; | |
719 llvm::GlobalVariable* sctor_arr = new llvm::GlobalVariable(sctor_type, false, llvm::GlobalValue::AppendingLinkage, sctor_init, varname, gIR->module); | |
720 } | |
721 | |
722 // function definition | |
723 if (allow_fbody && fbody != 0) | |
724 { | |
40 | 725 gIR->functions.push_back(IRFunction(this)); |
726 gIR->func().func = func; | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
727 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
728 // 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
|
729 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
|
730 |
52 | 731 //Logger::cout() << "func type: " << *f->llvmType << '\n'; |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
732 |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
733 // this handling |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
734 if (f->llvmUsesThis) { |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
735 Logger::println("uses this"); |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
736 if (f->llvmRetInPtr) |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
737 llvmThisVar = ++func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
738 else |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
739 llvmThisVar = func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
740 assert(llvmThisVar != 0); |
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 |
9 | 743 if (isMain()) |
744 gIR->emitMain = true; | |
745 | |
40 | 746 llvm::BasicBlock* beginbb = new llvm::BasicBlock("entry",func); |
747 llvm::BasicBlock* endbb = new llvm::BasicBlock("endentry",func); | |
1 | 748 |
749 //assert(gIR->scopes.empty()); | |
40 | 750 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
|
751 |
1 | 752 // create alloca point |
753 f->llvmAllocaPoint = new llvm::BitCastInst(llvm::ConstantInt::get(llvm::Type::Int32Ty,0,false),llvm::Type::Int32Ty,"alloca point",gIR->scopebb()); | |
40 | 754 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
|
755 |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
756 llvm::Value* parentNested = NULL; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
757 if (FuncDeclaration* fd = toParent()->isFuncDeclaration()) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
758 parentNested = fd->llvmNested; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
759 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
760 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
761 // construct nested variables struct |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
762 if (!llvmNestedVars.empty() || parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
763 std::vector<const llvm::Type*> nestTypes; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
764 int j = 0; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
765 if (parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
766 nestTypes.push_back(parentNested->getType()); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
767 j++; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
768 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
769 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
|
770 VarDeclaration* vd = *i; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
771 vd->llvmNestedIndex = j++; |
67
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
772 if (vd->isParameter()) { |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
773 assert(vd->llvmValue); |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
774 nestTypes.push_back(vd->llvmValue->getType()); |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
775 } |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
776 else { |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
777 nestTypes.push_back(LLVM_DtoType(vd->type)); |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
778 } |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
779 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
780 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
|
781 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
|
782 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
|
783 if (parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
784 assert(llvmThisVar); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
785 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
|
786 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
|
787 } |
67
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
788 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
|
789 VarDeclaration* vd = *i; |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
790 if (vd->isParameter()) { |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
791 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
|
792 vd->llvmValue = llvmNested; |
67
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
793 } |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
794 } |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
795 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
796 |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
797 // copy _argptr to a memory location |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
798 if (f->linkage == LINKd && f->varargs == 1) |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
799 { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
800 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
|
801 new llvm::StoreInst(llvmArgPtr, argptrmem, gIR->scopebb()); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
802 llvmArgPtr = argptrmem; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
803 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
804 |
1 | 805 // output function body |
806 fbody->toIR(gIR); | |
807 | |
808 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
809 // in automatically, so we do it here. | |
40 | 810 if (!isMain()) { |
52 | 811 if (!gIR->scopereturned()) { |
40 | 812 // pass the previous block into this block |
813 //new llvm::BranchInst(irs.end, irs.begin); | |
814 if (func->getReturnType() == llvm::Type::VoidTy) { | |
815 new llvm::ReturnInst(gIR->scopebb()); | |
816 } | |
52 | 817 else { |
818 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
819 } | |
40 | 820 } |
1 | 821 } |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
822 |
1 | 823 // erase alloca point |
824 f->llvmAllocaPoint->eraseFromParent(); | |
825 f->llvmAllocaPoint = 0; | |
40 | 826 gIR->func().allocapoint = 0; |
1 | 827 |
828 gIR->scopes.pop_back(); | |
829 | |
830 // get rid of the endentry block, it's never used | |
9 | 831 assert(!func->getBasicBlockList().empty()); |
1 | 832 func->getBasicBlockList().pop_back(); |
833 | |
834 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
9 | 835 // would be nice to figure out how to assert that this is correct |
1 | 836 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); |
837 if (lastbb->empty()) { | |
52 | 838 if (lastbb->getNumUses() == 0) |
839 lastbb->eraseFromParent(); | |
840 else { | |
841 new llvm::UnreachableInst(lastbb); | |
842 /*if (func->getReturnType() == llvm::Type::VoidTy) { | |
843 new llvm::ReturnInst(lastbb); | |
844 } | |
845 else { | |
846 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), lastbb); | |
847 }*/ | |
848 } | |
1 | 849 } |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
850 |
40 | 851 gIR->functions.pop_back(); |
1 | 852 } |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
853 |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
854 // template instances should have weak linkage |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
855 if (parent->isTemplateInstance()) { |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
856 func->setLinkage(llvm::GlobalValue::WeakLinkage); |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
857 } |
1 | 858 } |
859 } |