Mercurial > projects > ldc
annotate gen/toobj.c @ 55:0ccfae271c45 trunk
[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.
Updates to runtime. Rebuild!
author | lindquist |
---|---|
date | Wed, 24 Oct 2007 22:18:06 +0200 |
parents | 28e99b04a132 |
children | a9d29e9f1fed |
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); | |
550 assert(_type->getContainedType(0) == _init->getType()); | |
551 std::vector<llvm::Constant*> initvals; | |
552 initvals.resize(at->getNumElements(), _init); | |
553 _init = llvm::ConstantArray::get(at, initvals); | |
554 } | |
555 else { | |
556 Logger::cout() << "Unexpected initializer type: " << *_type << '\n'; | |
557 //assert(0); | |
558 } | |
1 | 559 } |
52 | 560 |
561 gvar->setInitializer(_init); | |
1 | 562 } |
563 | |
40 | 564 llvmDModule = gIR->dmodule; |
1 | 565 |
566 //if (storage_class & STCprivate) | |
567 // gvar->setVisibility(llvm::GlobalValue::ProtectedVisibility); | |
568 } | |
569 | |
570 // inside aggregate declaration. declare a field. | |
571 else | |
572 { | |
573 Logger::println("Aggregate var declaration: '%s' offset=%d", toChars(), offset); | |
574 | |
40 | 575 Type* t = LLVM_DtoDType(type); |
576 const llvm::Type* _type = LLVM_DtoType(t); | |
1 | 577 gIR->topstruct().fields.push_back(_type); |
578 | |
40 | 579 llvm::Constant*_init = LLVM_DtoConstInitializer(t, init); |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
580 assert(_init); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
581 Logger::cout() << "field init is: " << *_init << " type should be " << *_type << '\n'; |
40 | 582 if (_type != _init->getType()) |
1 | 583 { |
40 | 584 if (t->ty == Tsarray) |
1 | 585 { |
586 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(_type); | |
587 uint64_t n = arrty->getNumElements(); | |
588 std::vector<llvm::Constant*> vals(n,_init); | |
589 _init = llvm::ConstantArray::get(arrty, vals); | |
590 } | |
40 | 591 else if (t->ty == Tarray) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
592 { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
593 assert(llvm::isa<llvm::StructType>(_type)); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
594 _init = llvm::ConstantAggregateZero::get(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
595 } |
40 | 596 else if (t->ty == Tstruct) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
597 { |
6 | 598 const llvm::StructType* structty = llvm::cast<llvm::StructType>(_type); |
40 | 599 TypeStruct* ts = (TypeStruct*)t; |
9 | 600 assert(ts); |
601 assert(ts->sym); | |
6 | 602 assert(ts->sym->llvmInitZ); |
603 _init = ts->sym->llvmInitZ; | |
604 } | |
40 | 605 else if (t->ty == Tclass) |
28
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 _init = llvm::Constant::getNullValue(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
608 } |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
609 else { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
610 Logger::println("failed for type %s", type->toChars()); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
611 assert(0); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
612 } |
1 | 613 } |
614 gIR->topstruct().inits.push_back(_init); | |
615 } | |
616 | |
617 Logger::println("VarDeclaration::toObjFile is done"); | |
618 } | |
619 | |
620 /* ================================================================== */ | |
621 | |
622 void TypedefDeclaration::toObjFile() | |
623 { | |
624 static int tdi = 0; | |
625 Logger::print("TypedefDeclaration::toObjFile(%d): %s\n", tdi++, toChars()); | |
626 LOG_SCOPE; | |
627 | |
52 | 628 // generate typeinfo |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
629 if (!type->builtinTypeInfo()) |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
630 type->getTypeInfo(NULL); |
1 | 631 } |
632 | |
633 /* ================================================================== */ | |
634 | |
635 void EnumDeclaration::toObjFile() | |
636 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
637 Logger::println("Ignoring EnumDeclaration::toObjFile for %s", toChars()); |
1 | 638 } |
639 | |
640 /* ================================================================== */ | |
641 | |
642 void FuncDeclaration::toObjFile() | |
643 { | |
18 | 644 if (llvmDModule) { |
9 | 645 assert(llvmValue != 0); |
1 | 646 return; |
647 } | |
648 | |
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
|
649 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
|
650 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
|
651 |
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 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
|
653 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
|
654 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
|
655 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
|
656 { |
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 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
|
658 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 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
|
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 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
|
662 { |
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 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
|
664 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 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
|
666 } |
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 } |
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 |
9 | 669 llvm::Function* func = LLVM_DtoDeclareFunction(this); |
670 | |
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
|
671 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
|
672 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
|
673 |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
674 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
|
675 if (!llvmQueued) { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
676 Logger::println("queueing %s", toChars()); |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
677 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
|
678 llvmQueued = true; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
679 } |
10 | 680 return; // we wait with the definition as they might invoke a virtual method and the vtable is not yet complete |
1 | 681 } |
682 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
683 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
|
684 const llvm::FunctionType* functype = llvm::cast<llvm::FunctionType>(llvmValue->getType()->getContainedType(0)); |
1 | 685 |
686 // only members of the current module maybe be defined | |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
687 if (getModule() == gIR->dmodule || parent->isTemplateInstance()) |
1 | 688 { |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
689 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
|
690 |
1 | 691 bool allow_fbody = true; |
692 // handle static constructor / destructor | |
693 if (isStaticCtorDeclaration() || isStaticDtorDeclaration()) { | |
694 const llvm::ArrayType* sctor_type = llvm::ArrayType::get(llvm::PointerType::get(functype),1); | |
695 //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
|
696 |
1 | 697 llvm::Constant* sctor_func = llvm::cast<llvm::Constant>(llvmValue); |
698 //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
|
699 |
1 | 700 llvm::Constant* sctor_init = 0; |
701 if (llvmInternal == LLVMnull) | |
702 { | |
703 llvm::Constant* sctor_init_null = llvm::Constant::getNullValue(sctor_func->getType()); | |
704 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_init_null,1); | |
705 allow_fbody = false; | |
706 } | |
707 else | |
708 { | |
709 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_func,1); | |
710 } | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
711 |
1 | 712 //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
|
713 |
1 | 714 // output the llvm.global_ctors array |
715 const char* varname = isStaticCtorDeclaration() ? "_d_module_ctor_array" : "_d_module_dtor_array"; | |
716 llvm::GlobalVariable* sctor_arr = new llvm::GlobalVariable(sctor_type, false, llvm::GlobalValue::AppendingLinkage, sctor_init, varname, gIR->module); | |
717 } | |
718 | |
719 // function definition | |
720 if (allow_fbody && fbody != 0) | |
721 { | |
40 | 722 gIR->functions.push_back(IRFunction(this)); |
723 gIR->func().func = func; | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
724 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
725 // 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
|
726 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
|
727 |
52 | 728 //Logger::cout() << "func type: " << *f->llvmType << '\n'; |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
729 |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
730 // this handling |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
731 if (f->llvmUsesThis) { |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
732 Logger::println("uses this"); |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
733 if (f->llvmRetInPtr) |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
734 llvmThisVar = ++func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
735 else |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
736 llvmThisVar = func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
737 assert(llvmThisVar != 0); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
738 } |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
739 |
9 | 740 if (isMain()) |
741 gIR->emitMain = true; | |
742 | |
40 | 743 llvm::BasicBlock* beginbb = new llvm::BasicBlock("entry",func); |
744 llvm::BasicBlock* endbb = new llvm::BasicBlock("endentry",func); | |
1 | 745 |
746 //assert(gIR->scopes.empty()); | |
40 | 747 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
|
748 |
1 | 749 // create alloca point |
750 f->llvmAllocaPoint = new llvm::BitCastInst(llvm::ConstantInt::get(llvm::Type::Int32Ty,0,false),llvm::Type::Int32Ty,"alloca point",gIR->scopebb()); | |
40 | 751 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
|
752 |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
753 llvm::Value* parentNested = NULL; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
754 if (FuncDeclaration* fd = toParent()->isFuncDeclaration()) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
755 parentNested = fd->llvmNested; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
756 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
757 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
758 // construct nested variables struct |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
759 if (!llvmNestedVars.empty() || parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
760 std::vector<const llvm::Type*> nestTypes; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
761 int j = 0; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
762 if (parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
763 nestTypes.push_back(parentNested->getType()); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
764 j++; |
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 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
|
767 VarDeclaration* vd = *i; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
768 vd->llvmNestedIndex = j++; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
769 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
|
770 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
771 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
|
772 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
|
773 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
|
774 if (parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
775 assert(llvmThisVar); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
776 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
|
777 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
|
778 } |
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 |
1 | 781 // output function body |
782 fbody->toIR(gIR); | |
783 | |
784 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
785 // in automatically, so we do it here. | |
40 | 786 if (!isMain()) { |
52 | 787 if (!gIR->scopereturned()) { |
40 | 788 // pass the previous block into this block |
789 //new llvm::BranchInst(irs.end, irs.begin); | |
790 if (func->getReturnType() == llvm::Type::VoidTy) { | |
791 new llvm::ReturnInst(gIR->scopebb()); | |
792 } | |
52 | 793 else { |
794 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
795 } | |
40 | 796 } |
1 | 797 } |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
798 |
1 | 799 // erase alloca point |
800 f->llvmAllocaPoint->eraseFromParent(); | |
801 f->llvmAllocaPoint = 0; | |
40 | 802 gIR->func().allocapoint = 0; |
1 | 803 |
804 gIR->scopes.pop_back(); | |
805 | |
806 // get rid of the endentry block, it's never used | |
9 | 807 assert(!func->getBasicBlockList().empty()); |
1 | 808 func->getBasicBlockList().pop_back(); |
809 | |
810 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
9 | 811 // would be nice to figure out how to assert that this is correct |
1 | 812 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); |
813 if (lastbb->empty()) { | |
52 | 814 if (lastbb->getNumUses() == 0) |
815 lastbb->eraseFromParent(); | |
816 else { | |
817 new llvm::UnreachableInst(lastbb); | |
818 /*if (func->getReturnType() == llvm::Type::VoidTy) { | |
819 new llvm::ReturnInst(lastbb); | |
820 } | |
821 else { | |
822 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), lastbb); | |
823 }*/ | |
824 } | |
1 | 825 } |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
826 |
40 | 827 gIR->functions.pop_back(); |
1 | 828 } |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
829 |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
830 // template instances should have weak linkage |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
831 if (parent->isTemplateInstance()) { |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
832 func->setLinkage(llvm::GlobalValue::WeakLinkage); |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
833 } |
1 | 834 } |
835 } |