Mercurial > projects > ldc
annotate gen/toobj.c @ 40:8b0e809563df trunk
[svn r44] Lots of bug fixes.
New array literal support
New array ~= operator support (for single element)
New with statement support
More...
author | lindquist |
---|---|
date | Fri, 19 Oct 2007 07:43:21 +0200 |
parents | 27b2f40bdb58 |
children | 6fcc08a4d406 |
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 | |
101 /*if (global.params.llvmLL) { | |
102 //assert(0); | |
103 std::ofstream os(llfile->name->toChars()); | |
104 //llvm::WriteAssemblyToFile(ir.module, os); | |
105 ir.module->print(os); | |
106 }*/ | |
107 | |
6 | 108 // write bytecode |
1 | 109 //if (global.params.llvmBC) { |
110 Logger::println("Writing LLVM bitcode\n"); | |
111 std::ofstream os(bcfile->name->toChars(), std::ios::binary); | |
112 llvm::WriteBitcodeToFile(ir.module, os); | |
113 //} | |
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 | |
298 type->getTypeInfo(NULL); // generate TypeInfo | |
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 | |
366 bool define_vtable = false; | |
367 if (parent->isModule()) { | |
368 gIR->module->addTypeName(mangle(),ts->llvmType); | |
369 define_vtable = (getModule() == gIR->dmodule); | |
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 | |
410 std::string varname(mangle()); | |
411 varname.append("__vtblZ"); | |
412 std::string styname(mangle()); | |
413 styname.append("__vtblTy"); | |
414 | |
415 svtbl_ty = llvm::StructType::get(sinits_ty); | |
416 gIR->module->addTypeName(styname, svtbl_ty); | |
417 svtblVar = new llvm::GlobalVariable(svtbl_ty, true, _linkage, 0, varname, gIR->module); | |
418 | |
419 if (define_vtable) { | |
420 svtblVar->setInitializer(llvm::ConstantStruct::get(svtbl_ty, sinits)); | |
421 } | |
422 llvmVtbl = svtblVar; | |
423 } | |
424 | |
425 //////////////////////////////////////////////////////////////////////////////// | |
426 | |
427 // refine for final vtable type | |
428 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(svtbl_ty); | |
429 svtbl_ty = llvm::cast<llvm::StructType>(pa.get()); | |
430 structtype = llvm::cast<llvm::StructType>(gIR->topstruct().recty.get()); | |
431 ts->llvmType = structtype; | |
432 llvmType = structtype; | |
433 | |
434 // generate initializer | |
435 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
436 llvm::Constant* _init = 0; | |
437 | |
438 // first field is always the vtable | |
439 assert(svtblVar != 0); | |
440 gIR->topstruct().inits[0] = svtblVar; | |
441 | |
442 _init = llvm::ConstantStruct::get(structtype,gIR->topstruct().inits); | |
443 assert(_init); | |
444 std::string initname(mangle()); | |
445 initname.append("__initZ"); | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
446 //Logger::cout() << *_init << '\n'; |
9 | 447 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, 0, initname, gIR->module); |
448 ts->llvmInit = initvar; | |
449 if (define_vtable) { | |
450 initvar->setInitializer(_init); | |
451 } | |
452 | |
453 // generate member function definitions | |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
454 gIR->topstruct().queueFuncs = false; |
40 | 455 IRStruct::FuncDeclVec& mfs = gIR->topstruct().funcs; |
1 | 456 size_t n = mfs.size(); |
457 for (size_t i=0; i<n; ++i) { | |
458 mfs[i]->toObjFile(); | |
459 } | |
460 | |
461 gIR->classes.pop_back(); | |
462 gIR->structs.pop_back(); | |
463 | |
464 llvmInProgress = false; | |
465 } | |
466 | |
467 /****************************************** | |
468 * Get offset of base class's vtbl[] initializer from start of csym. | |
469 * Returns ~0 if not this csym. | |
470 */ | |
471 | |
472 unsigned ClassDeclaration::baseVtblOffset(BaseClass *bc) | |
473 { | |
474 return ~0; | |
475 } | |
476 | |
477 /* ================================================================== */ | |
478 | |
479 void VarDeclaration::toObjFile() | |
480 { | |
40 | 481 Logger::print("VarDeclaration::toObjFile(): %s | %s\n", toChars(), type->toChars()); |
1 | 482 LOG_SCOPE; |
483 llvm::Module* M = gIR->module; | |
484 | |
485 // handle bind pragma | |
486 if (llvmInternal == LLVMbind) { | |
487 Logger::println("var is bound: %s", llvmInternal1); | |
488 llvmValue = M->getGlobalVariable(llvmInternal1); | |
489 assert(llvmValue); | |
490 return; | |
491 } | |
492 | |
493 // global variable or magic | |
40 | 494 if (isDataseg() || parent->isModule()) |
1 | 495 { |
40 | 496 if (llvmTouched) return; |
497 else llvmTouched = true; | |
498 | |
1 | 499 bool _isconst = isConst(); |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
500 |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
501 llvm::GlobalValue::LinkageTypes _linkage; |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
502 if (parent->isFuncDeclaration()) |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
503 _linkage = llvm::GlobalValue::InternalLinkage; |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
504 else |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
505 _linkage = LLVM_DtoLinkage(protection, storage_class); |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
506 |
40 | 507 Type* t = LLVM_DtoDType(type); |
508 | |
509 const llvm::Type* _type = LLVM_DtoType(t); | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
510 assert(_type); |
1 | 511 |
512 llvm::Constant* _init = 0; | |
513 bool _signed = !type->isunsigned(); | |
514 | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
515 Logger::println("Creating global variable"); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
516 std::string _name(mangle()); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
517 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
|
518 llvmValue = gvar; |
40 | 519 |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
520 gIR->lvals.push_back(gvar); |
40 | 521 _init = LLVM_DtoConstInitializer(t, init); |
522 gIR->lvals.pop_back(); | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
523 |
1 | 524 //Logger::cout() << "initializer: " << *_init << '\n'; |
525 if (_type != _init->getType()) { | |
526 Logger::cout() << "got type '" << *_init->getType() << "' expected '" << *_type << "'\n"; | |
527 // zero initalizer | |
528 if (_init->isNullValue()) | |
529 _init = llvm::Constant::getNullValue(_type); | |
530 // pointer to global constant (struct.init) | |
531 else if (llvm::isa<llvm::GlobalVariable>(_init)) | |
532 { | |
533 assert(_init->getType()->getContainedType(0) == _type); | |
534 llvm::GlobalVariable* gv = llvm::cast<llvm::GlobalVariable>(_init); | |
40 | 535 assert(t->ty == Tstruct); |
536 TypeStruct* ts = (TypeStruct*)t; | |
1 | 537 assert(ts->sym->llvmInitZ); |
538 _init = ts->sym->llvmInitZ; | |
539 } | |
540 // array single value init | |
541 else if (llvm::isa<llvm::ArrayType>(_type)) | |
542 { | |
543 const llvm::ArrayType* at = llvm::cast<llvm::ArrayType>(_type); | |
544 assert(_type->getContainedType(0) == _init->getType()); | |
545 std::vector<llvm::Constant*> initvals; | |
546 initvals.resize(at->getNumElements(), _init); | |
547 _init = llvm::ConstantArray::get(at, initvals); | |
548 } | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
549 else { |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
550 Logger::cout() << "Unexpected initializer type: " << *_type << '\n'; |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
551 //assert(0); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
552 } |
1 | 553 } |
554 | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
555 gvar->setInitializer(_init); |
40 | 556 |
557 llvmDModule = gIR->dmodule; | |
1 | 558 |
559 //if (storage_class & STCprivate) | |
560 // gvar->setVisibility(llvm::GlobalValue::ProtectedVisibility); | |
561 } | |
562 | |
563 // inside aggregate declaration. declare a field. | |
564 else | |
565 { | |
566 Logger::println("Aggregate var declaration: '%s' offset=%d", toChars(), offset); | |
567 | |
40 | 568 Type* t = LLVM_DtoDType(type); |
569 const llvm::Type* _type = LLVM_DtoType(t); | |
1 | 570 gIR->topstruct().fields.push_back(_type); |
571 | |
40 | 572 llvm::Constant*_init = LLVM_DtoConstInitializer(t, init); |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
573 assert(_init); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
574 Logger::cout() << "field init is: " << *_init << " type should be " << *_type << '\n'; |
40 | 575 if (_type != _init->getType()) |
1 | 576 { |
40 | 577 if (t->ty == Tsarray) |
1 | 578 { |
579 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(_type); | |
580 uint64_t n = arrty->getNumElements(); | |
581 std::vector<llvm::Constant*> vals(n,_init); | |
582 _init = llvm::ConstantArray::get(arrty, vals); | |
583 } | |
40 | 584 else if (t->ty == Tarray) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
585 { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
586 assert(llvm::isa<llvm::StructType>(_type)); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
587 _init = llvm::ConstantAggregateZero::get(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
588 } |
40 | 589 else if (t->ty == Tstruct) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
590 { |
6 | 591 const llvm::StructType* structty = llvm::cast<llvm::StructType>(_type); |
40 | 592 TypeStruct* ts = (TypeStruct*)t; |
9 | 593 assert(ts); |
594 assert(ts->sym); | |
6 | 595 assert(ts->sym->llvmInitZ); |
596 _init = ts->sym->llvmInitZ; | |
597 } | |
40 | 598 else if (t->ty == Tclass) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
599 { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
600 _init = llvm::Constant::getNullValue(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
601 } |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
602 else { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
603 Logger::println("failed for type %s", type->toChars()); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
604 assert(0); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
605 } |
1 | 606 } |
607 gIR->topstruct().inits.push_back(_init); | |
608 } | |
609 | |
610 Logger::println("VarDeclaration::toObjFile is done"); | |
611 } | |
612 | |
613 /* ================================================================== */ | |
614 | |
615 void TypedefDeclaration::toObjFile() | |
616 { | |
617 static int tdi = 0; | |
618 Logger::print("TypedefDeclaration::toObjFile(%d): %s\n", tdi++, toChars()); | |
619 LOG_SCOPE; | |
620 | |
621 // TODO | |
622 } | |
623 | |
624 /* ================================================================== */ | |
625 | |
626 void EnumDeclaration::toObjFile() | |
627 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
628 Logger::println("Ignoring EnumDeclaration::toObjFile for %s", toChars()); |
1 | 629 } |
630 | |
631 /* ================================================================== */ | |
632 | |
633 void FuncDeclaration::toObjFile() | |
634 { | |
18 | 635 if (llvmDModule) { |
9 | 636 assert(llvmValue != 0); |
1 | 637 return; |
638 } | |
639 | |
9 | 640 llvm::Function* func = LLVM_DtoDeclareFunction(this); |
641 | |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
642 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
|
643 if (!llvmQueued) { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
644 Logger::println("queueing %s", toChars()); |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
645 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
|
646 llvmQueued = true; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
647 } |
10 | 648 return; // we wait with the definition as they might invoke a virtual method and the vtable is not yet complete |
1 | 649 } |
650 | |
40 | 651 Type* t = LLVM_DtoDType(type); |
652 TypeFunction* f = (TypeFunction*)t; | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
653 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
|
654 const llvm::FunctionType* functype = llvm::cast<llvm::FunctionType>(llvmValue->getType()->getContainedType(0)); |
1 | 655 |
656 // only members of the current module maybe be defined | |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
657 if (getModule() == gIR->dmodule || parent->isTemplateInstance()) |
1 | 658 { |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
659 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
|
660 |
1 | 661 bool allow_fbody = true; |
662 // handle static constructor / destructor | |
663 if (isStaticCtorDeclaration() || isStaticDtorDeclaration()) { | |
664 const llvm::ArrayType* sctor_type = llvm::ArrayType::get(llvm::PointerType::get(functype),1); | |
665 //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
|
666 |
1 | 667 llvm::Constant* sctor_func = llvm::cast<llvm::Constant>(llvmValue); |
668 //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
|
669 |
1 | 670 llvm::Constant* sctor_init = 0; |
671 if (llvmInternal == LLVMnull) | |
672 { | |
673 llvm::Constant* sctor_init_null = llvm::Constant::getNullValue(sctor_func->getType()); | |
674 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_init_null,1); | |
675 allow_fbody = false; | |
676 } | |
677 else | |
678 { | |
679 sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_func,1); | |
680 } | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
681 |
1 | 682 //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
|
683 |
1 | 684 // output the llvm.global_ctors array |
685 const char* varname = isStaticCtorDeclaration() ? "_d_module_ctor_array" : "_d_module_dtor_array"; | |
686 llvm::GlobalVariable* sctor_arr = new llvm::GlobalVariable(sctor_type, false, llvm::GlobalValue::AppendingLinkage, sctor_init, varname, gIR->module); | |
687 } | |
688 | |
689 // function definition | |
690 if (allow_fbody && fbody != 0) | |
691 { | |
40 | 692 gIR->functions.push_back(IRFunction(this)); |
693 gIR->func().func = func; | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
694 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
695 // 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
|
696 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
|
697 |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
698 Logger::cout() << "func type: " << *f->llvmType << '\n'; |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
699 |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
700 // this handling |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
701 if (f->llvmUsesThis) { |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
702 if (f->llvmRetInPtr) |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
703 llvmThisVar = ++func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
704 else |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
705 llvmThisVar = func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
706 assert(llvmThisVar != 0); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
707 } |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
708 |
9 | 709 if (isMain()) |
710 gIR->emitMain = true; | |
711 | |
40 | 712 llvm::BasicBlock* beginbb = new llvm::BasicBlock("entry",func); |
713 llvm::BasicBlock* endbb = new llvm::BasicBlock("endentry",func); | |
1 | 714 |
715 //assert(gIR->scopes.empty()); | |
40 | 716 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
|
717 |
1 | 718 // create alloca point |
719 f->llvmAllocaPoint = new llvm::BitCastInst(llvm::ConstantInt::get(llvm::Type::Int32Ty,0,false),llvm::Type::Int32Ty,"alloca point",gIR->scopebb()); | |
40 | 720 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
|
721 |
1 | 722 // output function body |
723 fbody->toIR(gIR); | |
724 | |
725 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
726 // in automatically, so we do it here. | |
40 | 727 if (!isMain()) { |
728 if (gIR->scopebb()->empty() || !llvm::isa<llvm::TerminatorInst>(gIR->scopebb()->back())) { | |
729 // pass the previous block into this block | |
730 //new llvm::BranchInst(irs.end, irs.begin); | |
731 if (func->getReturnType() == llvm::Type::VoidTy) { | |
732 new llvm::ReturnInst(gIR->scopebb()); | |
733 } | |
734 | |
735 } | |
1 | 736 } |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
737 |
1 | 738 // erase alloca point |
739 f->llvmAllocaPoint->eraseFromParent(); | |
740 f->llvmAllocaPoint = 0; | |
40 | 741 gIR->func().allocapoint = 0; |
1 | 742 |
743 gIR->scopes.pop_back(); | |
744 | |
745 // get rid of the endentry block, it's never used | |
9 | 746 assert(!func->getBasicBlockList().empty()); |
1 | 747 func->getBasicBlockList().pop_back(); |
748 | |
749 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
9 | 750 // would be nice to figure out how to assert that this is correct |
1 | 751 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); |
752 if (lastbb->empty()) { | |
40 | 753 lastbb->eraseFromParent(); |
1 | 754 } |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
755 |
40 | 756 gIR->functions.pop_back(); |
1 | 757 } |
17
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
758 |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
759 // template instances should have weak linkage |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
760 if (parent->isTemplateInstance()) { |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
761 func->setLinkage(llvm::GlobalValue::WeakLinkage); |
6c6cd097bcdf
[svn r21] * Fixed local instances of imported templates
lindquist
parents:
15
diff
changeset
|
762 } |
1 | 763 } |
764 } |