Mercurial > projects > ldc
annotate gen/toobj.c @ 64:b688ad419f8c trunk
[svn r68] Added support for multi-dimensional static arrays.
Several bugfixes to array support.
author | lindquist |
---|---|
date | Thu, 25 Oct 2007 12:09:13 +0200 |
parents | 2c39b5292cf3 |
children | f918f3e2e99e |
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 | |
52 | 299 //type->getTypeInfo(NULL); |
1 | 300 } |
301 | |
302 /* ================================================================== */ | |
303 | |
304 static void LLVM_AddBaseClassData(BaseClasses* bcs) | |
305 { | |
306 // add base class data members first | |
307 for (int j=0; j<bcs->dim; j++) | |
308 { | |
309 BaseClass* bc = (BaseClass*)(bcs->data[j]); | |
310 assert(bc); | |
40 | 311 Logger::println("Adding base class members of %s", bc->base->toChars()); |
312 LOG_SCOPE; | |
313 | |
1 | 314 LLVM_AddBaseClassData(&bc->base->baseclasses); |
315 for (int k=0; k < bc->base->members->dim; k++) { | |
316 Dsymbol* dsym = (Dsymbol*)(bc->base->members->data[k]); | |
317 if (dsym->isVarDeclaration()) | |
318 { | |
319 dsym->toObjFile(); | |
320 } | |
321 } | |
322 } | |
323 } | |
324 | |
325 void ClassDeclaration::toObjFile() | |
326 { | |
40 | 327 TypeClass* ts = (TypeClass*)LLVM_DtoDType(type); |
1 | 328 if (ts->llvmType != 0 || llvmInProgress) |
329 return; | |
330 | |
331 llvmInProgress = true; | |
332 | |
333 static int fdi = 0; | |
334 Logger::print("ClassDeclaration::toObjFile(%d): %s\n", fdi++, toChars()); | |
335 LOG_SCOPE; | |
336 | |
6 | 337 gIR->structs.push_back(IRStruct(ts)); |
1 | 338 gIR->classes.push_back(this); |
339 | |
340 // add vtable | |
9 | 341 llvm::PATypeHolder pa = llvm::OpaqueType::get(); |
342 const llvm::Type* vtabty = llvm::PointerType::get(pa); | |
1 | 343 gIR->topstruct().fields.push_back(vtabty); |
344 gIR->topstruct().inits.push_back(0); | |
345 | |
346 // base classes first | |
347 LLVM_AddBaseClassData(&baseclasses); | |
348 | |
349 // then add own members | |
350 for (int k=0; k < members->dim; k++) { | |
351 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
352 dsym->toObjFile(); | |
353 } | |
354 | |
355 llvm::StructType* structtype = llvm::StructType::get(gIR->topstruct().fields); | |
6 | 356 // refine abstract types for stuff like: class C {C next;} |
357 if (gIR->topstruct().recty != 0) | |
358 { | |
359 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
360 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
361 structtype = llvm::cast<llvm::StructType>(pa.get()); | |
362 } | |
363 | |
1 | 364 ts->llvmType = structtype; |
365 llvmType = structtype; | |
366 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
367 bool needs_definition = false; |
1 | 368 if (parent->isModule()) { |
369 gIR->module->addTypeName(mangle(),ts->llvmType); | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
370 needs_definition = (getModule() == gIR->dmodule); |
1 | 371 } |
372 else { | |
373 assert(0 && "class parent is not a module"); | |
374 } | |
375 | |
9 | 376 // generate vtable |
377 llvm::GlobalVariable* svtblVar = 0; | |
378 std::vector<llvm::Constant*> sinits; | |
379 std::vector<const llvm::Type*> sinits_ty; | |
380 sinits.reserve(vtbl.dim); | |
381 sinits_ty.reserve(vtbl.dim); | |
382 | |
383 for (int k=0; k < vtbl.dim; k++) | |
384 { | |
385 Dsymbol* dsym = (Dsymbol*)vtbl.data[k]; | |
386 assert(dsym); | |
387 //Logger::cout() << "vtblsym: " << dsym->toChars() << '\n'; | |
388 | |
389 if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { | |
390 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
|
391 assert(fd->llvmValue); |
9 | 392 llvm::Constant* c = llvm::cast<llvm::Constant>(fd->llvmValue); |
393 sinits.push_back(c); | |
394 sinits_ty.push_back(c->getType()); | |
395 } | |
396 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { | |
397 const llvm::Type* cty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
398 llvm::Constant* c = llvm::Constant::getNullValue(cty); | |
399 sinits.push_back(c); | |
400 sinits_ty.push_back(cty); | |
401 } | |
402 else | |
403 assert(0); | |
404 } | |
405 | |
406 const llvm::StructType* svtbl_ty = 0; | |
407 if (!sinits.empty()) | |
408 { | |
409 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
410 | |
52 | 411 std::string varname("_D"); |
412 varname.append(mangle()); | |
413 varname.append("6__vtblZ"); | |
414 | |
9 | 415 std::string styname(mangle()); |
416 styname.append("__vtblTy"); | |
417 | |
418 svtbl_ty = llvm::StructType::get(sinits_ty); | |
419 gIR->module->addTypeName(styname, svtbl_ty); | |
420 svtblVar = new llvm::GlobalVariable(svtbl_ty, true, _linkage, 0, varname, gIR->module); | |
421 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
422 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
|
423 if (needs_definition) |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
424 svtblVar->setInitializer(llvmConstVtbl); |
9 | 425 llvmVtbl = svtblVar; |
426 } | |
427 | |
428 //////////////////////////////////////////////////////////////////////////////// | |
429 | |
430 // refine for final vtable type | |
431 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(svtbl_ty); | |
432 svtbl_ty = llvm::cast<llvm::StructType>(pa.get()); | |
433 structtype = llvm::cast<llvm::StructType>(gIR->topstruct().recty.get()); | |
434 ts->llvmType = structtype; | |
435 llvmType = structtype; | |
436 | |
437 // generate initializer | |
438 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
439 llvm::Constant* _init = 0; | |
440 | |
441 // first field is always the vtable | |
442 assert(svtblVar != 0); | |
443 gIR->topstruct().inits[0] = svtblVar; | |
444 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
445 llvmInitZ = _init = llvm::ConstantStruct::get(structtype,gIR->topstruct().inits); |
9 | 446 assert(_init); |
52 | 447 |
448 std::string initname("_D"); | |
449 initname.append(mangle()); | |
450 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
|
451 //Logger::cout() << *_init << '\n'; |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
452 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, NULL, initname, gIR->module); |
9 | 453 ts->llvmInit = initvar; |
454 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
455 if (needs_definition) { |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
456 initvar->setInitializer(_init); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
457 // generate member functions |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
458 gIR->topstruct().queueFuncs = false; |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
459 IRStruct::FuncDeclVec& mfs = gIR->topstruct().funcs; |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
460 size_t n = mfs.size(); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
461 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
|
462 mfs[i]->toObjFile(); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
463 } |
1 | 464 } |
465 | |
466 gIR->classes.pop_back(); | |
467 gIR->structs.pop_back(); | |
468 | |
469 llvmInProgress = false; | |
470 } | |
471 | |
472 /****************************************** | |
473 * Get offset of base class's vtbl[] initializer from start of csym. | |
474 * Returns ~0 if not this csym. | |
475 */ | |
476 | |
477 unsigned ClassDeclaration::baseVtblOffset(BaseClass *bc) | |
478 { | |
479 return ~0; | |
480 } | |
481 | |
482 /* ================================================================== */ | |
483 | |
484 void VarDeclaration::toObjFile() | |
485 { | |
40 | 486 Logger::print("VarDeclaration::toObjFile(): %s | %s\n", toChars(), type->toChars()); |
1 | 487 LOG_SCOPE; |
488 llvm::Module* M = gIR->module; | |
489 | |
490 // handle bind pragma | |
491 if (llvmInternal == LLVMbind) { | |
492 Logger::println("var is bound: %s", llvmInternal1); | |
493 llvmValue = M->getGlobalVariable(llvmInternal1); | |
494 assert(llvmValue); | |
495 return; | |
496 } | |
497 | |
498 // global variable or magic | |
40 | 499 if (isDataseg() || parent->isModule()) |
1 | 500 { |
40 | 501 if (llvmTouched) return; |
502 else llvmTouched = true; | |
503 | |
1 | 504 bool _isconst = isConst(); |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
505 |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
506 llvm::GlobalValue::LinkageTypes _linkage; |
52 | 507 if (parent && parent->isFuncDeclaration()) |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
508 _linkage = llvm::GlobalValue::InternalLinkage; |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
509 else |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
510 _linkage = LLVM_DtoLinkage(protection, storage_class); |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
511 |
40 | 512 Type* t = LLVM_DtoDType(type); |
513 | |
514 const llvm::Type* _type = LLVM_DtoType(t); | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
515 assert(_type); |
1 | 516 |
517 llvm::Constant* _init = 0; | |
518 bool _signed = !type->isunsigned(); | |
519 | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
520 Logger::println("Creating global variable"); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
521 std::string _name(mangle()); |
52 | 522 |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
523 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(_type,_isconst,_linkage,0,_name,M); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
524 llvmValue = gvar; |
40 | 525 |
52 | 526 // if extern don't emit initializer |
527 if (!(storage_class & STCextern)) | |
528 { | |
529 _init = LLVM_DtoConstInitializer(t, init); | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
530 |
52 | 531 //Logger::cout() << "initializer: " << *_init << '\n'; |
532 if (_type != _init->getType()) { | |
533 Logger::cout() << "got type '" << *_init->getType() << "' expected '" << *_type << "'\n"; | |
534 // zero initalizer | |
535 if (_init->isNullValue()) | |
536 _init = llvm::Constant::getNullValue(_type); | |
537 // pointer to global constant (struct.init) | |
538 else if (llvm::isa<llvm::GlobalVariable>(_init)) | |
539 { | |
540 assert(_init->getType()->getContainedType(0) == _type); | |
541 llvm::GlobalVariable* gv = llvm::cast<llvm::GlobalVariable>(_init); | |
542 assert(t->ty == Tstruct); | |
543 TypeStruct* ts = (TypeStruct*)t; | |
544 assert(ts->sym->llvmInitZ); | |
545 _init = ts->sym->llvmInitZ; | |
546 } | |
547 // array single value init | |
548 else if (llvm::isa<llvm::ArrayType>(_type)) | |
549 { | |
64
b688ad419f8c
[svn r68] Added support for multi-dimensional static arrays.
lindquist
parents:
63
diff
changeset
|
550 _init = LLVM_DtoConstantStaticArray(_type, _init); |
52 | 551 } |
552 else { | |
553 Logger::cout() << "Unexpected initializer type: " << *_type << '\n'; | |
554 //assert(0); | |
555 } | |
1 | 556 } |
52 | 557 |
64
b688ad419f8c
[svn r68] Added support for multi-dimensional static arrays.
lindquist
parents:
63
diff
changeset
|
558 Logger::cout() << "final init = " << *_init << '\n'; |
52 | 559 gvar->setInitializer(_init); |
1 | 560 } |
561 | |
40 | 562 llvmDModule = gIR->dmodule; |
1 | 563 |
564 //if (storage_class & STCprivate) | |
565 // gvar->setVisibility(llvm::GlobalValue::ProtectedVisibility); | |
566 } | |
567 | |
568 // inside aggregate declaration. declare a field. | |
569 else | |
570 { | |
571 Logger::println("Aggregate var declaration: '%s' offset=%d", toChars(), offset); | |
572 | |
40 | 573 Type* t = LLVM_DtoDType(type); |
574 const llvm::Type* _type = LLVM_DtoType(t); | |
1 | 575 gIR->topstruct().fields.push_back(_type); |
576 | |
40 | 577 llvm::Constant*_init = LLVM_DtoConstInitializer(t, init); |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
578 assert(_init); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
579 Logger::cout() << "field init is: " << *_init << " type should be " << *_type << '\n'; |
40 | 580 if (_type != _init->getType()) |
1 | 581 { |
40 | 582 if (t->ty == Tsarray) |
1 | 583 { |
584 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(_type); | |
585 uint64_t n = arrty->getNumElements(); | |
586 std::vector<llvm::Constant*> vals(n,_init); | |
587 _init = llvm::ConstantArray::get(arrty, vals); | |
588 } | |
40 | 589 else if (t->ty == Tarray) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
590 { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
591 assert(llvm::isa<llvm::StructType>(_type)); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
592 _init = llvm::ConstantAggregateZero::get(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
593 } |
40 | 594 else if (t->ty == Tstruct) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
595 { |
6 | 596 const llvm::StructType* structty = llvm::cast<llvm::StructType>(_type); |
40 | 597 TypeStruct* ts = (TypeStruct*)t; |
9 | 598 assert(ts); |
599 assert(ts->sym); | |
6 | 600 assert(ts->sym->llvmInitZ); |
601 _init = ts->sym->llvmInitZ; | |
602 } | |
40 | 603 else if (t->ty == Tclass) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
604 { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
605 _init = llvm::Constant::getNullValue(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
606 } |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
607 else { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
608 Logger::println("failed for type %s", type->toChars()); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
609 assert(0); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
610 } |
1 | 611 } |
612 gIR->topstruct().inits.push_back(_init); | |
613 } | |
614 | |
615 Logger::println("VarDeclaration::toObjFile is done"); | |
616 } | |
617 | |
618 /* ================================================================== */ | |
619 | |
620 void TypedefDeclaration::toObjFile() | |
621 { | |
622 static int tdi = 0; | |
623 Logger::print("TypedefDeclaration::toObjFile(%d): %s\n", tdi++, toChars()); | |
624 LOG_SCOPE; | |
625 | |
52 | 626 // generate typeinfo |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
627 if (!type->builtinTypeInfo()) |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
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++; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
772 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
|
773 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
774 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
|
775 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
|
776 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
|
777 if (parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
778 assert(llvmThisVar); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
779 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
|
780 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
|
781 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
782 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
783 |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
784 // copy _argptr to a memory location |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
785 if (f->linkage == LINKd && f->varargs == 1) |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
786 { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
787 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
|
788 new llvm::StoreInst(llvmArgPtr, argptrmem, gIR->scopebb()); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
789 llvmArgPtr = argptrmem; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
790 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
791 |
1 | 792 // output function body |
793 fbody->toIR(gIR); | |
794 | |
795 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
796 // in automatically, so we do it here. | |
40 | 797 if (!isMain()) { |
52 | 798 if (!gIR->scopereturned()) { |
40 | 799 // pass the previous block into this block |
800 //new llvm::BranchInst(irs.end, irs.begin); | |
801 if (func->getReturnType() == llvm::Type::VoidTy) { | |
802 new llvm::ReturnInst(gIR->scopebb()); | |
803 } | |
52 | 804 else { |
805 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
806 } | |
40 | 807 } |
1 | 808 } |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
809 |
1 | 810 // erase alloca point |
811 f->llvmAllocaPoint->eraseFromParent(); | |
812 f->llvmAllocaPoint = 0; | |
40 | 813 gIR->func().allocapoint = 0; |
1 | 814 |
815 gIR->scopes.pop_back(); | |
816 | |
817 // get rid of the endentry block, it's never used | |
9 | 818 assert(!func->getBasicBlockList().empty()); |
1 | 819 func->getBasicBlockList().pop_back(); |
820 | |
821 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
9 | 822 // would be nice to figure out how to assert that this is correct |
1 | 823 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); |
824 if (lastbb->empty()) { | |
52 | 825 if (lastbb->getNumUses() == 0) |
826 lastbb->eraseFromParent(); | |
827 else { | |
828 new llvm::UnreachableInst(lastbb); | |
829 /*if (func->getReturnType() == llvm::Type::VoidTy) { | |
830 new llvm::ReturnInst(lastbb); | |
831 } | |
832 else { | |
833 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), lastbb); | |
834 }*/ | |
835 } | |
1 | 836 } |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
837 |
40 | 838 gIR->functions.pop_back(); |
1 | 839 } |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
840 |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
841 // template instances should have weak linkage |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
842 if (parent->isTemplateInstance()) { |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
843 func->setLinkage(llvm::GlobalValue::WeakLinkage); |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
844 } |
1 | 845 } |
846 } |