Mercurial > projects > ldc
annotate gen/toobj.cpp @ 88:058d3925950e trunk
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
author | lindquist |
---|---|
date | Tue, 06 Nov 2007 10:03:14 +0100 |
parents | fd32135dca3e |
children | ccca1c13e13a |
rev | line source |
---|---|
1 | 1 |
2 // Copyright (c) 1999-2004 by Digital Mars | |
3 // All Rights Reserved | |
4 // written by Walter Bright | |
5 // www.digitalmars.com | |
6 // License for redistribution is by either the Artistic License | |
7 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
8 // See the included readme.txt for details. | |
9 | |
10 #include <cstddef> | |
11 #include <iostream> | |
12 #include <fstream> | |
13 | |
40 | 14 #include "gen/llvm.h" |
1 | 15 #include "llvm/Analysis/Verifier.h" |
16 #include "llvm/Bitcode/ReaderWriter.h" | |
17 #include "llvm/Target/TargetMachine.h" | |
18 #include "llvm/Target/TargetMachineRegistry.h" | |
19 | |
20 #include "mars.h" | |
21 #include "module.h" | |
22 #include "mtype.h" | |
23 #include "declaration.h" | |
24 #include "statement.h" | |
25 #include "enum.h" | |
26 #include "aggregate.h" | |
27 #include "init.h" | |
28 #include "attrib.h" | |
29 #include "id.h" | |
30 #include "import.h" | |
31 #include "template.h" | |
18 | 32 #include "scope.h" |
1 | 33 |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
34 #include "gen/irstate.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
35 #include "gen/elem.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
36 #include "gen/logger.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
37 #include "gen/tollvm.h" |
64
b688ad419f8c
[svn r68] Added support for multi-dimensional static arrays.
lindquist
parents:
63
diff
changeset
|
38 #include "gen/arrays.h" |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
39 #include "gen/structs.h" |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
40 #include "gen/todebug.h" |
86
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
41 #include "gen/runtime.h" |
1 | 42 |
43 ////////////////////////////////////////////////////////////////////////////////////////// | |
44 | |
45 void | |
46 Module::genobjfile() | |
47 { | |
48 Logger::cout() << "Generating module: " << (md ? md->toChars() : toChars()) << '\n'; | |
49 LOG_SCOPE; | |
50 | |
6 | 51 // start by deleting the old object file |
1 | 52 deleteObjFile(); |
53 | |
18 | 54 // create a new ir state |
1 | 55 IRState ir; |
56 gIR = &ir; | |
57 ir.dmodule = this; | |
58 | |
6 | 59 // name the module |
1 | 60 std::string mname(toChars()); |
61 if (md != 0) | |
62 mname = md->toChars(); | |
63 ir.module = new llvm::Module(mname); | |
64 | |
6 | 65 // set target stuff |
1 | 66 std::string target_triple(global.params.tt_arch); |
67 target_triple.append(global.params.tt_os); | |
68 ir.module->setTargetTriple(target_triple); | |
69 ir.module->setDataLayout(global.params.data_layout); | |
70 | |
73 | 71 // heavily inspired by tools/llc/llc.cpp:200-230 |
72 const llvm::TargetMachineRegistry::Entry* targetEntry; | |
73 std::string targetError; | |
74 targetEntry = llvm::TargetMachineRegistry::getClosestStaticTargetForModule(*ir.module, targetError); | |
75 assert(targetEntry && "Failed to find a static target for module"); | |
76 std::auto_ptr<llvm::TargetMachine> targetPtr(targetEntry->CtorFn(*ir.module, "")); // TODO: replace "" with features | |
77 assert(targetPtr.get() && "Could not allocate target machine!"); | |
78 llvm::TargetMachine &targetMachine = *targetPtr.get(); | |
79 gTargetData = targetMachine.getTargetData(); | |
1 | 80 |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
81 // debug info |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
82 if (global.params.symdebug) { |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
83 RegisterDwarfSymbols(ir.module); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
84 ir.dwarfCompileUnit = DtoDwarfCompileUnit(this); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
85 } |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
86 |
6 | 87 // process module members |
1 | 88 for (int k=0; k < members->dim; k++) { |
89 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
90 assert(dsym); | |
91 dsym->toObjFile(); | |
92 } | |
93 | |
94 gTargetData = 0; | |
95 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
96 // 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
|
97 if (ir.emitMain) { |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
98 DtoMain(); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
99 } |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
100 |
6 | 101 // verify the llvm |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
102 if (!global.params.novalidate) { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
103 std::string verifyErr; |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
104 Logger::println("Verifying module..."); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
105 if (llvm::verifyModule(*ir.module,llvm::ReturnStatusAction,&verifyErr)) |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
106 { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
107 error("%s", verifyErr.c_str()); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
108 fatal(); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
109 } |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
110 else { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
111 Logger::println("Verification passed!"); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
112 } |
1 | 113 } |
114 | |
115 // run passes | |
116 // TODO | |
117 | |
52 | 118 // write bytecode |
119 { | |
120 Logger::println("Writing LLVM bitcode\n"); | |
121 std::ofstream bos(bcfile->name->toChars(), std::ios::binary); | |
122 llvm::WriteBitcodeToFile(ir.module, bos); | |
123 } | |
1 | 124 |
52 | 125 // disassemble ? |
126 if (global.params.disassemble) { | |
127 Logger::println("Writing LLVM asm to: %s\n", llfile->name->toChars()); | |
128 std::ofstream aos(llfile->name->toChars()); | |
129 ir.module->print(aos); | |
130 } | |
1 | 131 |
132 delete ir.module; | |
133 gIR = NULL; | |
134 } | |
135 | |
136 /* ================================================================== */ | |
137 | |
138 // Put out instance of ModuleInfo for this Module | |
139 | |
140 void Module::genmoduleinfo() | |
141 { | |
142 } | |
143 | |
144 /* ================================================================== */ | |
145 | |
146 void Dsymbol::toObjFile() | |
147 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
148 Logger::println("Ignoring Dsymbol::toObjFile for %s", toChars()); |
1 | 149 } |
150 | |
151 /* ================================================================== */ | |
152 | |
153 void Declaration::toObjFile() | |
154 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
155 Logger::println("Ignoring Declaration::toObjFile for %s", toChars()); |
1 | 156 } |
157 | |
158 /* ================================================================== */ | |
159 | |
160 void InterfaceDeclaration::toObjFile() | |
161 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
162 Logger::println("Ignoring InterfaceDeclaration::toObjFile for %s", toChars()); |
1 | 163 } |
164 | |
165 /* ================================================================== */ | |
166 | |
167 void StructDeclaration::toObjFile() | |
168 { | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
169 TypeStruct* ts = (TypeStruct*)DtoDType(type); |
1 | 170 if (llvmType != 0) |
171 return; | |
172 | |
173 static int sdi = 0; | |
174 Logger::print("StructDeclaration::toObjFile(%d): %s\n", sdi++, toChars()); | |
175 LOG_SCOPE; | |
176 | |
177 gIR->structs.push_back(IRStruct(ts)); | |
178 | |
179 for (int k=0; k < members->dim; k++) { | |
180 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
181 dsym->toObjFile(); |
1 | 182 } |
183 | |
73 | 184 Logger::println("doing struct fields"); |
185 | |
186 llvm::StructType* structtype = 0; | |
187 std::vector<llvm::Constant*> fieldinits; | |
188 | |
189 if (gIR->topstruct().offsets.empty()) | |
190 { | |
191 std::vector<const llvm::Type*> fieldtypes; | |
192 Logger::println("has no fields"); | |
193 fieldtypes.push_back(llvm::Type::Int8Ty); | |
194 fieldinits.push_back(llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false)); | |
195 structtype = llvm::StructType::get(fieldtypes); | |
196 } | |
197 else | |
23 | 198 { |
73 | 199 Logger::println("has fields"); |
200 std::vector<const llvm::Type*> fieldtypes; | |
201 unsigned prevsize = (unsigned)-1; | |
202 unsigned lastoffset = (unsigned)-1; | |
203 const llvm::Type* fieldtype = NULL; | |
204 llvm::Constant* fieldinit = NULL; | |
205 size_t fieldpad = 0; | |
206 int idx = 0; | |
207 for (IRStruct::OffsetMap::iterator i=gIR->topstruct().offsets.begin(); i!=gIR->topstruct().offsets.end(); ++i) { | |
208 // first iteration | |
209 if (lastoffset == (unsigned)-1) { | |
210 lastoffset = i->first; | |
211 assert(lastoffset == 0); | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
212 fieldtype = DtoType(i->second.var->type); |
73 | 213 fieldinit = i->second.init; |
214 prevsize = gTargetData->getTypeSize(fieldtype); | |
215 i->second.var->llvmFieldIndex = idx; | |
216 } | |
217 // colliding offset? | |
218 else if (lastoffset == i->first) { | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
219 const llvm::Type* t = DtoType(i->second.var->type); |
73 | 220 size_t s = gTargetData->getTypeSize(t); |
221 if (s > prevsize) { | |
77
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
222 fieldpad += s - prevsize; |
73 | 223 prevsize = s; |
224 } | |
225 llvmHasUnions = true; | |
226 i->second.var->llvmFieldIndex = idx; | |
227 } | |
228 // intersecting offset? | |
229 else if (i->first < (lastoffset + prevsize)) { | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
230 const llvm::Type* t = DtoType(i->second.var->type); |
73 | 231 size_t s = gTargetData->getTypeSize(t); |
232 assert((i->first + s) <= (lastoffset + prevsize)); // this holds because all types are aligned to their size | |
233 llvmHasUnions = true; | |
234 i->second.var->llvmFieldIndex = idx; | |
235 i->second.var->llvmFieldIndexOffset = (i->first - lastoffset) / s; | |
236 } | |
237 // fresh offset | |
238 else { | |
239 // commit the field | |
240 fieldtypes.push_back(fieldtype); | |
241 fieldinits.push_back(fieldinit); | |
242 if (fieldpad) { | |
243 // match up with below | |
244 std::vector<llvm::Constant*> vals(fieldpad, llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false)); | |
245 llvm::Constant* c = llvm::ConstantArray::get(llvm::ArrayType::get(llvm::Type::Int8Ty, fieldpad), vals); | |
246 fieldtypes.push_back(c->getType()); | |
247 fieldinits.push_back(c); | |
248 idx++; | |
249 } | |
250 | |
251 idx++; | |
252 | |
253 // start new | |
254 lastoffset = i->first; | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
255 fieldtype = DtoType(i->second.var->type); |
73 | 256 fieldinit = i->second.init; |
257 prevsize = gTargetData->getTypeSize(fieldtype); | |
258 i->second.var->llvmFieldIndex = idx; | |
259 fieldpad = 0; | |
260 } | |
261 } | |
262 fieldtypes.push_back(fieldtype); | |
263 fieldinits.push_back(fieldinit); | |
264 if (fieldpad) { | |
265 // match up with above | |
266 std::vector<llvm::Constant*> vals(fieldpad, llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false)); | |
267 llvm::Constant* c = llvm::ConstantArray::get(llvm::ArrayType::get(llvm::Type::Int8Ty, fieldpad), vals); | |
268 fieldtypes.push_back(c->getType()); | |
269 fieldinits.push_back(c); | |
270 } | |
271 | |
272 Logger::println("creating struct type"); | |
273 structtype = llvm::StructType::get(fieldtypes); | |
23 | 274 } |
275 | |
1 | 276 // refine abstract types for stuff like: struct S{S* next;} |
277 if (gIR->topstruct().recty != 0) | |
278 { | |
279 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
280 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
281 structtype = llvm::cast<llvm::StructType>(pa.get()); | |
282 } | |
283 | |
284 ts->llvmType = structtype; | |
285 llvmType = structtype; | |
286 | |
287 if (parent->isModule()) { | |
288 gIR->module->addTypeName(mangle(),ts->llvmType); | |
289 } | |
290 | |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
291 llvmUnion = new DUnion; // uses gIR->topstruct() |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
292 |
1 | 293 // generate static data |
294 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
295 llvm::Constant* _init = 0; | |
296 | |
297 // always generate the constant initalizer | |
298 if (!zeroInit) { | |
299 Logger::println("Not zero initialized"); | |
73 | 300 //assert(tk == gIR->gIR->topstruct()().size()); |
1 | 301 #ifndef LLVMD_NO_LOGGER |
73 | 302 Logger::cout() << "struct type: " << *structtype << '\n'; |
303 for (size_t k=0; k<fieldinits.size(); ++k) { | |
1 | 304 Logger::cout() << "Type:" << '\n'; |
73 | 305 Logger::cout() << *fieldinits[k]->getType() << '\n'; |
1 | 306 Logger::cout() << "Value:" << '\n'; |
73 | 307 Logger::cout() << *fieldinits[k] << '\n'; |
1 | 308 } |
309 Logger::cout() << "Initializer printed" << '\n'; | |
310 #endif | |
73 | 311 llvmInitZ = llvm::ConstantStruct::get(structtype,fieldinits); |
1 | 312 } |
313 else { | |
314 Logger::println("Zero initialized"); | |
315 llvmInitZ = llvm::ConstantAggregateZero::get(structtype); | |
316 } | |
317 | |
318 // only provide the constant initializer for the defining module | |
319 if (getModule() == gIR->dmodule) | |
320 { | |
321 _init = llvmInitZ; | |
322 } | |
323 | |
73 | 324 std::string initname("_D"); |
325 initname.append(mangle()); | |
326 initname.append("6__initZ"); | |
1 | 327 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, _init, initname, gIR->module); |
328 ts->llvmInit = initvar; | |
329 | |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
330 // generate member function definitions |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
331 gIR->topstruct().queueFuncs = false; |
73 | 332 IRStruct::FuncDeclVector& mfs = gIR->topstruct().funcs; |
1 | 333 size_t n = mfs.size(); |
334 for (size_t i=0; i<n; ++i) { | |
335 mfs[i]->toObjFile(); | |
336 } | |
337 | |
338 llvmDModule = gIR->dmodule; | |
339 | |
340 gIR->structs.pop_back(); | |
341 | |
342 // generate typeinfo | |
73 | 343 if (getModule() == gIR->dmodule && llvmInternal != LLVMnotypeinfo) |
72 | 344 type->getTypeInfo(NULL); |
1 | 345 } |
346 | |
347 /* ================================================================== */ | |
348 | |
77
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
349 static unsigned LLVM_ClassOffsetToIndex(ClassDeclaration* cd, unsigned os, unsigned& idx) |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
350 { |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
351 // start at the bottom of the inheritance chain |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
352 if (cd->baseClass != 0) { |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
353 unsigned o = LLVM_ClassOffsetToIndex(cd->baseClass, os, idx); |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
354 if (o != (unsigned)-1) |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
355 return o; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
356 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
357 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
358 // check this class |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
359 unsigned i; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
360 for (i=0; i<cd->fields.dim; ++i) { |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
361 VarDeclaration* vd = (VarDeclaration*)cd->fields.data[i]; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
362 if (os == vd->offset) |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
363 return i+idx; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
364 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
365 idx += i; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
366 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
367 return (unsigned)-1; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
368 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
369 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
370 void ClassDeclaration::offsetToIndex(Type* t, unsigned os, std::vector<unsigned>& result) |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
371 { |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
372 unsigned idx = 0; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
373 unsigned r = LLVM_ClassOffsetToIndex(this, os, idx); |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
374 assert(r != (unsigned)-1 && "Offset not found in any aggregate field"); |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
375 result.push_back(r+1); // vtable is 0 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
376 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
377 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
378 /* ================================================================== */ |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
379 |
1 | 380 static void LLVM_AddBaseClassData(BaseClasses* bcs) |
381 { | |
382 // add base class data members first | |
383 for (int j=0; j<bcs->dim; j++) | |
384 { | |
385 BaseClass* bc = (BaseClass*)(bcs->data[j]); | |
386 assert(bc); | |
40 | 387 Logger::println("Adding base class members of %s", bc->base->toChars()); |
388 LOG_SCOPE; | |
389 | |
1 | 390 LLVM_AddBaseClassData(&bc->base->baseclasses); |
391 for (int k=0; k < bc->base->members->dim; k++) { | |
392 Dsymbol* dsym = (Dsymbol*)(bc->base->members->data[k]); | |
393 if (dsym->isVarDeclaration()) | |
394 { | |
395 dsym->toObjFile(); | |
396 } | |
397 } | |
398 } | |
399 } | |
400 | |
401 void ClassDeclaration::toObjFile() | |
402 { | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
403 TypeClass* ts = (TypeClass*)DtoDType(type); |
1 | 404 if (ts->llvmType != 0 || llvmInProgress) |
405 return; | |
406 | |
407 llvmInProgress = true; | |
408 | |
409 static int fdi = 0; | |
410 Logger::print("ClassDeclaration::toObjFile(%d): %s\n", fdi++, toChars()); | |
411 LOG_SCOPE; | |
412 | |
6 | 413 gIR->structs.push_back(IRStruct(ts)); |
1 | 414 gIR->classes.push_back(this); |
415 | |
416 // add vtable | |
9 | 417 llvm::PATypeHolder pa = llvm::OpaqueType::get(); |
418 const llvm::Type* vtabty = llvm::PointerType::get(pa); | |
73 | 419 |
420 std::vector<const llvm::Type*> fieldtypes; | |
421 fieldtypes.push_back(vtabty); | |
422 | |
423 std::vector<llvm::Constant*> fieldinits; | |
424 fieldinits.push_back(0); | |
1 | 425 |
426 // base classes first | |
427 LLVM_AddBaseClassData(&baseclasses); | |
428 | |
429 // then add own members | |
430 for (int k=0; k < members->dim; k++) { | |
431 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
432 dsym->toObjFile(); | |
433 } | |
434 | |
73 | 435 // fill out fieldtypes/inits |
436 for (IRStruct::OffsetMap::iterator i=gIR->topstruct().offsets.begin(); i!=gIR->topstruct().offsets.end(); ++i) { | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
437 fieldtypes.push_back(DtoType(i->second.var->type)); |
73 | 438 fieldinits.push_back(i->second.init); |
439 } | |
440 | |
441 llvm::StructType* structtype = llvm::StructType::get(fieldtypes); | |
6 | 442 // refine abstract types for stuff like: class C {C next;} |
443 if (gIR->topstruct().recty != 0) | |
444 { | |
445 llvm::PATypeHolder& pa = gIR->topstruct().recty; | |
446 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(structtype); | |
447 structtype = llvm::cast<llvm::StructType>(pa.get()); | |
448 } | |
449 | |
1 | 450 ts->llvmType = structtype; |
451 llvmType = structtype; | |
452 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
453 bool needs_definition = false; |
1 | 454 if (parent->isModule()) { |
455 gIR->module->addTypeName(mangle(),ts->llvmType); | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
456 needs_definition = (getModule() == gIR->dmodule); |
1 | 457 } |
458 else { | |
459 assert(0 && "class parent is not a module"); | |
460 } | |
461 | |
9 | 462 // generate vtable |
463 llvm::GlobalVariable* svtblVar = 0; | |
464 std::vector<llvm::Constant*> sinits; | |
465 std::vector<const llvm::Type*> sinits_ty; | |
466 sinits.reserve(vtbl.dim); | |
467 sinits_ty.reserve(vtbl.dim); | |
468 | |
469 for (int k=0; k < vtbl.dim; k++) | |
470 { | |
471 Dsymbol* dsym = (Dsymbol*)vtbl.data[k]; | |
472 assert(dsym); | |
473 //Logger::cout() << "vtblsym: " << dsym->toChars() << '\n'; | |
474 | |
475 if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { | |
476 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
|
477 assert(fd->llvmValue); |
9 | 478 llvm::Constant* c = llvm::cast<llvm::Constant>(fd->llvmValue); |
479 sinits.push_back(c); | |
480 sinits_ty.push_back(c->getType()); | |
481 } | |
482 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { | |
483 const llvm::Type* cty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
484 llvm::Constant* c = llvm::Constant::getNullValue(cty); | |
485 sinits.push_back(c); | |
486 sinits_ty.push_back(cty); | |
487 } | |
488 else | |
489 assert(0); | |
490 } | |
491 | |
492 const llvm::StructType* svtbl_ty = 0; | |
493 if (!sinits.empty()) | |
494 { | |
495 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
496 | |
52 | 497 std::string varname("_D"); |
498 varname.append(mangle()); | |
499 varname.append("6__vtblZ"); | |
500 | |
9 | 501 std::string styname(mangle()); |
502 styname.append("__vtblTy"); | |
503 | |
504 svtbl_ty = llvm::StructType::get(sinits_ty); | |
505 gIR->module->addTypeName(styname, svtbl_ty); | |
506 svtblVar = new llvm::GlobalVariable(svtbl_ty, true, _linkage, 0, varname, gIR->module); | |
507 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
508 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
|
509 if (needs_definition) |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
510 svtblVar->setInitializer(llvmConstVtbl); |
9 | 511 llvmVtbl = svtblVar; |
512 } | |
513 | |
514 //////////////////////////////////////////////////////////////////////////////// | |
515 | |
516 // refine for final vtable type | |
517 llvm::cast<llvm::OpaqueType>(pa.get())->refineAbstractTypeTo(svtbl_ty); | |
518 svtbl_ty = llvm::cast<llvm::StructType>(pa.get()); | |
519 structtype = llvm::cast<llvm::StructType>(gIR->topstruct().recty.get()); | |
520 ts->llvmType = structtype; | |
521 llvmType = structtype; | |
522 | |
523 // generate initializer | |
524 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; | |
525 llvm::Constant* _init = 0; | |
526 | |
527 // first field is always the vtable | |
528 assert(svtblVar != 0); | |
73 | 529 fieldinits[0] = svtblVar; |
9 | 530 |
73 | 531 llvmInitZ = _init = llvm::ConstantStruct::get(structtype,fieldinits); |
9 | 532 assert(_init); |
52 | 533 |
534 std::string initname("_D"); | |
535 initname.append(mangle()); | |
536 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
|
537 //Logger::cout() << *_init << '\n'; |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
538 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType, true, _linkage, NULL, initname, gIR->module); |
9 | 539 ts->llvmInit = initvar; |
540 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
541 if (needs_definition) { |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
542 initvar->setInitializer(_init); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
543 // generate member functions |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
544 gIR->topstruct().queueFuncs = false; |
73 | 545 IRStruct::FuncDeclVector& mfs = gIR->topstruct().funcs; |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
546 size_t n = mfs.size(); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
547 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
|
548 mfs[i]->toObjFile(); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
52
diff
changeset
|
549 } |
1 | 550 } |
551 | |
552 gIR->classes.pop_back(); | |
553 gIR->structs.pop_back(); | |
554 | |
555 llvmInProgress = false; | |
556 } | |
557 | |
558 /****************************************** | |
559 * Get offset of base class's vtbl[] initializer from start of csym. | |
560 * Returns ~0 if not this csym. | |
561 */ | |
562 | |
563 unsigned ClassDeclaration::baseVtblOffset(BaseClass *bc) | |
564 { | |
565 return ~0; | |
566 } | |
567 | |
568 /* ================================================================== */ | |
569 | |
570 void VarDeclaration::toObjFile() | |
571 { | |
40 | 572 Logger::print("VarDeclaration::toObjFile(): %s | %s\n", toChars(), type->toChars()); |
1 | 573 LOG_SCOPE; |
574 llvm::Module* M = gIR->module; | |
575 | |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
576 if (aliassym) |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
577 { |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
578 Logger::println("alias sym"); |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
579 toAlias()->toObjFile(); |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
580 return; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
581 } |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
582 |
1 | 583 // global variable or magic |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
584 if (isDataseg()) |
1 | 585 { |
40 | 586 if (llvmTouched) return; |
587 else llvmTouched = true; | |
588 | |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
589 bool _isconst = false; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
590 if (isConst() && (init && !init->isExpInitializer())) |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
591 _isconst = true; |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
592 |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
593 llvm::GlobalValue::LinkageTypes _linkage; |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
594 bool istempl = false; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
595 if ((storage_class & STCcomdat) || (parent && DtoIsTemplateInstance(parent))) { |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
596 _linkage = llvm::GlobalValue::WeakLinkage; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
597 istempl = true; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
598 } |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
599 else if (parent && parent->isFuncDeclaration()) |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
600 _linkage = llvm::GlobalValue::InternalLinkage; |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
601 else |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
602 _linkage = DtoLinkage(protection, storage_class); |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
603 |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
604 Type* t = DtoDType(type); |
40 | 605 |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
606 const llvm::Type* _type = DtoType(t); |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
607 assert(_type); |
1 | 608 |
609 llvm::Constant* _init = 0; | |
610 bool _signed = !type->isunsigned(); | |
611 | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
612 Logger::println("Creating global variable"); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
613 std::string _name(mangle()); |
52 | 614 |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
615 bool emitRTstaticInit = false; |
40 | 616 |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
617 if (!(storage_class & STCextern) && (getModule() == gIR->dmodule || istempl)) |
52 | 618 { |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
619 if (parent && parent->isFuncDeclaration() && init && init->isExpInitializer()) { |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
620 _init = DtoConstInitializer(t, NULL); |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
621 emitRTstaticInit = true; |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
622 } |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
623 else { |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
624 _init = DtoConstInitializer(t, init); |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
625 } |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
626 |
52 | 627 //Logger::cout() << "initializer: " << *_init << '\n'; |
628 if (_type != _init->getType()) { | |
629 Logger::cout() << "got type '" << *_init->getType() << "' expected '" << *_type << "'\n"; | |
630 // zero initalizer | |
631 if (_init->isNullValue()) | |
632 _init = llvm::Constant::getNullValue(_type); | |
633 // pointer to global constant (struct.init) | |
634 else if (llvm::isa<llvm::GlobalVariable>(_init)) | |
635 { | |
636 assert(_init->getType()->getContainedType(0) == _type); | |
637 llvm::GlobalVariable* gv = llvm::cast<llvm::GlobalVariable>(_init); | |
638 assert(t->ty == Tstruct); | |
639 TypeStruct* ts = (TypeStruct*)t; | |
640 assert(ts->sym->llvmInitZ); | |
641 _init = ts->sym->llvmInitZ; | |
642 } | |
643 // array single value init | |
644 else if (llvm::isa<llvm::ArrayType>(_type)) | |
645 { | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
646 _init = DtoConstStaticArray(_type, _init); |
52 | 647 } |
648 else { | |
649 Logger::cout() << "Unexpected initializer type: " << *_type << '\n'; | |
650 //assert(0); | |
651 } | |
1 | 652 } |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
653 } |
52 | 654 |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
655 if (_init && _init->getType() != _type) |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
656 _type = _init->getType(); |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
657 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(_type,_isconst,_linkage,_init,_name,M); |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
658 llvmValue = gvar; |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
659 |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
660 if (emitRTstaticInit) |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
661 DtoLazyStaticInit(istempl, gvar, init, t); |
1 | 662 |
40 | 663 llvmDModule = gIR->dmodule; |
1 | 664 |
665 //if (storage_class & STCprivate) | |
666 // gvar->setVisibility(llvm::GlobalValue::ProtectedVisibility); | |
667 } | |
668 | |
669 // inside aggregate declaration. declare a field. | |
670 else | |
671 { | |
672 Logger::println("Aggregate var declaration: '%s' offset=%d", toChars(), offset); | |
673 | |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
674 Type* t = DtoDType(type); |
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
675 const llvm::Type* _type = DtoType(t); |
1 | 676 |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
677 llvm::Constant*_init = DtoConstInitializer(t, init); |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
678 assert(_init); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
679 Logger::cout() << "field init is: " << *_init << " type should be " << *_type << '\n'; |
40 | 680 if (_type != _init->getType()) |
1 | 681 { |
40 | 682 if (t->ty == Tsarray) |
1 | 683 { |
684 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(_type); | |
685 uint64_t n = arrty->getNumElements(); | |
686 std::vector<llvm::Constant*> vals(n,_init); | |
687 _init = llvm::ConstantArray::get(arrty, vals); | |
688 } | |
40 | 689 else if (t->ty == Tarray) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
690 { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
691 assert(llvm::isa<llvm::StructType>(_type)); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
692 _init = llvm::ConstantAggregateZero::get(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
693 } |
40 | 694 else if (t->ty == Tstruct) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
695 { |
6 | 696 const llvm::StructType* structty = llvm::cast<llvm::StructType>(_type); |
40 | 697 TypeStruct* ts = (TypeStruct*)t; |
9 | 698 assert(ts); |
699 assert(ts->sym); | |
6 | 700 assert(ts->sym->llvmInitZ); |
701 _init = ts->sym->llvmInitZ; | |
702 } | |
40 | 703 else if (t->ty == Tclass) |
28
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
704 { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
705 _init = llvm::Constant::getNullValue(_type); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
706 } |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
707 else { |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
708 Logger::println("failed for type %s", type->toChars()); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
709 assert(0); |
1c80c18f3c82
[svn r32] * Fixed problems with arrays members of aggregates
lindquist
parents:
26
diff
changeset
|
710 } |
1 | 711 } |
73 | 712 |
713 // add the field in the IRStruct | |
714 gIR->topstruct().offsets.insert(std::make_pair(offset, IRStruct::Offset(this,_init))); | |
1 | 715 } |
716 | |
717 Logger::println("VarDeclaration::toObjFile is done"); | |
718 } | |
719 | |
720 /* ================================================================== */ | |
721 | |
722 void TypedefDeclaration::toObjFile() | |
723 { | |
724 static int tdi = 0; | |
725 Logger::print("TypedefDeclaration::toObjFile(%d): %s\n", tdi++, toChars()); | |
726 LOG_SCOPE; | |
727 | |
52 | 728 // generate typeinfo |
72 | 729 type->getTypeInfo(NULL); |
1 | 730 } |
731 | |
732 /* ================================================================== */ | |
733 | |
734 void EnumDeclaration::toObjFile() | |
735 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
736 Logger::println("Ignoring EnumDeclaration::toObjFile for %s", toChars()); |
1 | 737 } |
738 | |
739 /* ================================================================== */ | |
740 | |
741 void FuncDeclaration::toObjFile() | |
742 { | |
18 | 743 if (llvmDModule) { |
9 | 744 assert(llvmValue != 0); |
1 | 745 return; |
746 } | |
747 | |
86
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
748 if (llvmRunTimeHack) { |
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
749 Logger::println("runtime hack func chars: %s", toChars()); |
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
750 if (!llvmValue) |
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
751 llvmValue = LLVM_D_GetRuntimeFunction(gIR->module, toChars()); |
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
752 return; |
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
753 } |
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
754 |
63
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
755 if (isUnitTestDeclaration()) { |
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
756 Logger::println("*** ATTENTION: ignoring unittest declaration: %s", toChars()); |
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
757 return; |
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
758 } |
2c39b5292cf3
[svn r67] Fixed ignore unittests instead of failing to compile
lindquist
parents:
58
diff
changeset
|
759 |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
760 Type* t = DtoDType(type); |
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
|
761 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
|
762 |
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
|
763 bool declareOnly = false; |
86
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
764 if (parent) |
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
765 { |
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
|
766 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
|
767 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
|
768 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
|
769 { |
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
|
770 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
|
771 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
|
772 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
|
773 } |
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
|
774 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
|
775 { |
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
|
776 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
|
777 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
|
778 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
|
779 } |
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
|
780 } |
86
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
781 } |
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
|
782 |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
783 llvm::Function* func = DtoDeclareFunction(this); |
9 | 784 |
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
|
785 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
|
786 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
|
787 |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
788 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
|
789 if (!llvmQueued) { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
790 Logger::println("queueing %s", toChars()); |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
791 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
|
792 llvmQueued = true; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
793 } |
10 | 794 return; // we wait with the definition as they might invoke a virtual method and the vtable is not yet complete |
1 | 795 } |
796 | |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
797 // debug info |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
798 if (global.params.symdebug) { |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
799 llvmDwarfSubProgram = DtoDwarfSubProgram(this); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
800 } |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
801 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
802 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
|
803 const llvm::FunctionType* functype = llvm::cast<llvm::FunctionType>(llvmValue->getType()->getContainedType(0)); |
1 | 804 |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
805 // template instances should have weak linkage |
86
fd32135dca3e
[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!!
lindquist
parents:
85
diff
changeset
|
806 if (parent && DtoIsTemplateInstance(parent)) { |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
807 func->setLinkage(llvm::GlobalValue::WeakLinkage); |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
808 } |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
809 |
1 | 810 // only members of the current module maybe be defined |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
811 if (getModule() == gIR->dmodule || DtoIsTemplateInstance(parent)) |
1 | 812 { |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
813 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
|
814 |
1 | 815 // handle static constructor / destructor |
816 if (isStaticCtorDeclaration() || isStaticDtorDeclaration()) { | |
817 const llvm::ArrayType* sctor_type = llvm::ArrayType::get(llvm::PointerType::get(functype),1); | |
818 //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
|
819 |
1 | 820 llvm::Constant* sctor_func = llvm::cast<llvm::Constant>(llvmValue); |
821 //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
|
822 |
74 | 823 llvm::Constant* sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_func,1); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
824 |
1 | 825 //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
|
826 |
1 | 827 // output the llvm.global_ctors array |
828 const char* varname = isStaticCtorDeclaration() ? "_d_module_ctor_array" : "_d_module_dtor_array"; | |
829 llvm::GlobalVariable* sctor_arr = new llvm::GlobalVariable(sctor_type, false, llvm::GlobalValue::AppendingLinkage, sctor_init, varname, gIR->module); | |
830 } | |
831 | |
832 // function definition | |
74 | 833 if (fbody != 0) |
1 | 834 { |
40 | 835 gIR->functions.push_back(IRFunction(this)); |
836 gIR->func().func = func; | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
837 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
838 // 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
|
839 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
|
840 |
52 | 841 //Logger::cout() << "func type: " << *f->llvmType << '\n'; |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
842 |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
843 // this handling |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
844 if (f->llvmUsesThis) { |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
845 Logger::println("uses this"); |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
846 if (f->llvmRetInPtr) |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
847 llvmThisVar = ++func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
848 else |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
849 llvmThisVar = func->arg_begin(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
850 assert(llvmThisVar != 0); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
851 } |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
852 |
9 | 853 if (isMain()) |
854 gIR->emitMain = true; | |
855 | |
40 | 856 llvm::BasicBlock* beginbb = new llvm::BasicBlock("entry",func); |
857 llvm::BasicBlock* endbb = new llvm::BasicBlock("endentry",func); | |
1 | 858 |
859 //assert(gIR->scopes.empty()); | |
40 | 860 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
|
861 |
1 | 862 // create alloca point |
863 f->llvmAllocaPoint = new llvm::BitCastInst(llvm::ConstantInt::get(llvm::Type::Int32Ty,0,false),llvm::Type::Int32Ty,"alloca point",gIR->scopebb()); | |
40 | 864 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
|
865 |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
866 // give arguments storage |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
867 size_t n = Argument::dim(f->parameters); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
868 for (int i=0; i < n; ++i) { |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
869 Argument* arg = Argument::getNth(f->parameters, i); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
870 if (arg && arg->vardecl) { |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
871 VarDeclaration* vd = arg->vardecl; |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
872 if (!vd->llvmNeedsStorage || vd->nestedref || vd->isRef() || vd->isOut() || DtoIsPassedByRef(vd->type)) |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
873 continue; |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
874 llvm::Value* a = vd->llvmValue; |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
875 assert(a); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
876 std::string s(a->getName()); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
877 Logger::println("giving argument '%s' storage", s.c_str()); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
878 s.append("_storage"); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
879 llvm::Value* v = new llvm::AllocaInst(a->getType(),s,f->llvmAllocaPoint); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
880 gIR->ir->CreateStore(a,v); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
881 vd->llvmValue = v; |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
882 } |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
883 else { |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
884 Logger::println("*** ATTENTION: some unknown argument: %s", arg ? arg->toChars() : 0); |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
885 } |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
886 } |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
887 |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
888 // debug info |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
889 if (global.params.symdebug) DtoDwarfFuncStart(this); |
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
890 |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
891 llvm::Value* parentNested = NULL; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
892 if (FuncDeclaration* fd = toParent()->isFuncDeclaration()) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
893 parentNested = fd->llvmNested; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
894 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
895 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
896 // construct nested variables struct |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
897 if (!llvmNestedVars.empty() || parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
898 std::vector<const llvm::Type*> nestTypes; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
899 int j = 0; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
900 if (parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
901 nestTypes.push_back(parentNested->getType()); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
902 j++; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
903 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
904 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
|
905 VarDeclaration* vd = *i; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
906 vd->llvmNestedIndex = j++; |
67
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
907 if (vd->isParameter()) { |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
908 assert(vd->llvmValue); |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
909 nestTypes.push_back(vd->llvmValue->getType()); |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
910 } |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
911 else { |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
912 nestTypes.push_back(DtoType(vd->type)); |
67
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
913 } |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
914 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
915 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
|
916 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
|
917 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
|
918 if (parentNested) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
919 assert(llvmThisVar); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
920 llvm::Value* ptr = gIR->ir->CreateBitCast(llvmThisVar, parentNested->getType(), "tmp"); |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
921 gIR->ir->CreateStore(ptr, DtoGEPi(llvmNested, 0,0, "tmp")); |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
922 } |
67
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
923 for (std::set<VarDeclaration*>::iterator i=llvmNestedVars.begin(); i!=llvmNestedVars.end(); ++i) { |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
924 VarDeclaration* vd = *i; |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
925 if (vd->isParameter()) { |
81
3587401b6eeb
[svn r85] Fixed: if a return statement appeared in the try block of a nested try-finally, only the inner-most finally block would be executed.
lindquist
parents:
77
diff
changeset
|
926 gIR->ir->CreateStore(vd->llvmValue, DtoGEPi(llvmNested, 0, vd->llvmNestedIndex, "tmp")); |
68
c4b3f5d2cd9b
[svn r72] Calling a nested function that is not a delegate was not working.
lindquist
parents:
67
diff
changeset
|
927 vd->llvmValue = llvmNested; |
67
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
928 } |
f918f3e2e99e
[svn r71] Fixed accessing parent function arguments from inside nested delegates.
lindquist
parents:
64
diff
changeset
|
929 } |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
930 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
40
diff
changeset
|
931 |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
932 // copy _argptr to a memory location |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
933 if (f->linkage == LINKd && f->varargs == 1) |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
934 { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
935 llvm::Value* argptrmem = new llvm::AllocaInst(llvmArgPtr->getType(), "_argptrmem", gIR->topallocapoint()); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
936 new llvm::StoreInst(llvmArgPtr, argptrmem, gIR->scopebb()); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
937 llvmArgPtr = argptrmem; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
938 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
939 |
1 | 940 // output function body |
941 fbody->toIR(gIR); | |
942 | |
943 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
944 // in automatically, so we do it here. | |
40 | 945 if (!isMain()) { |
52 | 946 if (!gIR->scopereturned()) { |
40 | 947 // pass the previous block into this block |
82
d8dd47ef3973
[svn r86] Changed the way arguments are given storage. It is now detected if they will need it during semantic passes.
lindquist
parents:
81
diff
changeset
|
948 if (global.params.symdebug) DtoDwarfFuncEnd(this); |
40 | 949 if (func->getReturnType() == llvm::Type::VoidTy) { |
950 new llvm::ReturnInst(gIR->scopebb()); | |
951 } | |
52 | 952 else { |
953 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
954 } | |
40 | 955 } |
1 | 956 } |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
957 |
1 | 958 // erase alloca point |
959 f->llvmAllocaPoint->eraseFromParent(); | |
960 f->llvmAllocaPoint = 0; | |
40 | 961 gIR->func().allocapoint = 0; |
1 | 962 |
963 gIR->scopes.pop_back(); | |
964 | |
965 // get rid of the endentry block, it's never used | |
9 | 966 assert(!func->getBasicBlockList().empty()); |
1 | 967 func->getBasicBlockList().pop_back(); |
968 | |
969 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
9 | 970 // would be nice to figure out how to assert that this is correct |
1 | 971 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); |
972 if (lastbb->empty()) { | |
52 | 973 if (lastbb->getNumUses() == 0) |
974 lastbb->eraseFromParent(); | |
975 else { | |
976 new llvm::UnreachableInst(lastbb); | |
977 /*if (func->getReturnType() == llvm::Type::VoidTy) { | |
978 new llvm::ReturnInst(lastbb); | |
979 } | |
980 else { | |
981 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), lastbb); | |
982 }*/ | |
983 } | |
1 | 984 } |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
985 |
40 | 986 gIR->functions.pop_back(); |
1 | 987 } |
988 } | |
989 } |