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