Mercurial > projects > ldc
annotate gen/toobj.cpp @ 100:5071469303d4 trunk
[svn r104] TONS OF FIXES.
Split up declaration, constant initializer gen and definition for globals, structs, classes and functions.
Improved ClassInfo support (not complete), not in vtable yet.
Fixed a bunch of forward reference problems.
Much more. Major commit! :)
author | lindquist |
---|---|
date | Fri, 16 Nov 2007 08:21:47 +0100 |
parents | 6789050b5ad1 |
children | 027b8d8b71ec |
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/logger.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
36 #include "gen/tollvm.h" |
64
b688ad419f8c
[svn r68] Added support for multi-dimensional static arrays.
lindquist
parents:
63
diff
changeset
|
37 #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
|
38 #include "gen/structs.h" |
100 | 39 #include "gen/classes.h" |
40 #include "gen/functions.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
|
41 #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
|
42 #include "gen/runtime.h" |
1 | 43 |
44 ////////////////////////////////////////////////////////////////////////////////////////// | |
45 | |
46 void | |
47 Module::genobjfile() | |
48 { | |
49 Logger::cout() << "Generating module: " << (md ? md->toChars() : toChars()) << '\n'; | |
50 LOG_SCOPE; | |
51 | |
6 | 52 // start by deleting the old object file |
1 | 53 deleteObjFile(); |
54 | |
18 | 55 // create a new ir state |
1 | 56 IRState ir; |
57 gIR = &ir; | |
58 ir.dmodule = this; | |
59 | |
6 | 60 // name the module |
1 | 61 std::string mname(toChars()); |
62 if (md != 0) | |
63 mname = md->toChars(); | |
64 ir.module = new llvm::Module(mname); | |
65 | |
6 | 66 // set target stuff |
1 | 67 std::string target_triple(global.params.tt_arch); |
68 target_triple.append(global.params.tt_os); | |
69 ir.module->setTargetTriple(target_triple); | |
70 ir.module->setDataLayout(global.params.data_layout); | |
71 | |
73 | 72 // heavily inspired by tools/llc/llc.cpp:200-230 |
73 const llvm::TargetMachineRegistry::Entry* targetEntry; | |
74 std::string targetError; | |
75 targetEntry = llvm::TargetMachineRegistry::getClosestStaticTargetForModule(*ir.module, targetError); | |
76 assert(targetEntry && "Failed to find a static target for module"); | |
77 std::auto_ptr<llvm::TargetMachine> targetPtr(targetEntry->CtorFn(*ir.module, "")); // TODO: replace "" with features | |
78 assert(targetPtr.get() && "Could not allocate target machine!"); | |
79 llvm::TargetMachine &targetMachine = *targetPtr.get(); | |
80 gTargetData = targetMachine.getTargetData(); | |
1 | 81 |
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
|
82 // 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
|
83 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
|
84 RegisterDwarfSymbols(ir.module); |
94
61615fa85940
[svn r98] Added support for std.c.stdlib.alloca via pragma(LLVM_internal, "alloca").
lindquist
parents:
89
diff
changeset
|
85 ir.dmodule->llvmCompileUnit = DtoDwarfCompileUnit(this,true); |
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
|
86 } |
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
|
87 |
6 | 88 // process module members |
1 | 89 for (int k=0; k < members->dim; k++) { |
90 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
91 assert(dsym); | |
92 dsym->toObjFile(); | |
93 } | |
94 | |
100 | 95 // process deferred const initializers |
96 for (size_t i=0; i<ir.constInitQueue.size(); ++i) { | |
97 DtoConstInitDsymbol(ir.constInitQueue[i]); | |
98 } | |
99 | |
100 // process deferred definitions | |
101 for (size_t i=0; i<ir.defineQueue.size(); ++i) { | |
102 DtoDefineDsymbol(ir.defineQueue[i]); | |
98
6789050b5ad1
[svn r102] Further delayed emission of function bodies to avoid problems with circular-forward-references.
lindquist
parents:
96
diff
changeset
|
103 } |
6789050b5ad1
[svn r102] Further delayed emission of function bodies to avoid problems with circular-forward-references.
lindquist
parents:
96
diff
changeset
|
104 |
89 | 105 // generate ModuleInfo |
106 genmoduleinfo(); | |
107 | |
1 | 108 gTargetData = 0; |
109 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
110 // 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
|
111 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
|
112 DtoMain(); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
113 } |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
114 |
6 | 115 // verify the llvm |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
116 if (!global.params.novalidate) { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
117 std::string verifyErr; |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
118 Logger::println("Verifying module..."); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
119 if (llvm::verifyModule(*ir.module,llvm::ReturnStatusAction,&verifyErr)) |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
120 { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
121 error("%s", verifyErr.c_str()); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
122 fatal(); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
123 } |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
124 else { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
125 Logger::println("Verification passed!"); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
126 } |
1 | 127 } |
128 | |
129 // run passes | |
130 // TODO | |
131 | |
52 | 132 // write bytecode |
133 { | |
134 Logger::println("Writing LLVM bitcode\n"); | |
135 std::ofstream bos(bcfile->name->toChars(), std::ios::binary); | |
136 llvm::WriteBitcodeToFile(ir.module, bos); | |
137 } | |
1 | 138 |
52 | 139 // disassemble ? |
140 if (global.params.disassemble) { | |
141 Logger::println("Writing LLVM asm to: %s\n", llfile->name->toChars()); | |
142 std::ofstream aos(llfile->name->toChars()); | |
143 ir.module->print(aos); | |
144 } | |
1 | 145 |
146 delete ir.module; | |
147 gIR = NULL; | |
148 } | |
149 | |
150 /* ================================================================== */ | |
151 | |
152 // Put out instance of ModuleInfo for this Module | |
153 | |
154 void Module::genmoduleinfo() | |
155 { | |
89 | 156 // The layout is: |
157 // { | |
158 // void **vptr; | |
159 // monitor_t monitor; | |
160 // char[] name; // class name | |
161 // ModuleInfo importedModules[]; | |
162 // ClassInfo localClasses[]; | |
163 // uint flags; // initialization state | |
164 // void *ctor; | |
165 // void *dtor; | |
166 // void *unitTest; | |
167 // } | |
168 | |
169 if (moduleinfo) { | |
170 Logger::println("moduleinfo"); | |
171 } | |
172 if (vmoduleinfo) { | |
173 Logger::println("vmoduleinfo"); | |
174 } | |
175 if (needModuleInfo()) { | |
94
61615fa85940
[svn r98] Added support for std.c.stdlib.alloca via pragma(LLVM_internal, "alloca").
lindquist
parents:
89
diff
changeset
|
176 Logger::attention("module info is needed but skipped"); |
89 | 177 } |
178 | |
179 | |
180 /* | |
181 Symbol *msym = toSymbol(); | |
182 unsigned offset; | |
183 unsigned sizeof_ModuleInfo = 12 * PTRSIZE; | |
184 | |
185 ////////////////////////////////////////////// | |
186 | |
187 csym->Sclass = SCglobal; | |
188 csym->Sfl = FLdata; | |
189 | |
190 // The layout is: | |
191 // { | |
192 // void **vptr; | |
193 // monitor_t monitor; | |
194 // char[] name; // class name | |
195 // ModuleInfo importedModules[]; | |
196 // ClassInfo localClasses[]; | |
197 // uint flags; // initialization state | |
198 // void *ctor; | |
199 // void *dtor; | |
200 // void *unitTest; | |
201 // } | |
202 dt_t *dt = NULL; | |
203 | |
204 if (moduleinfo) | |
205 dtxoff(&dt, moduleinfo->toVtblSymbol(), 0, TYnptr); // vtbl for ModuleInfo | |
206 else | |
207 dtdword(&dt, 0); // BUG: should be an assert() | |
208 dtdword(&dt, 0); // monitor | |
209 | |
210 // name[] | |
211 char *name = toPrettyChars(); | |
212 size_t namelen = strlen(name); | |
213 dtdword(&dt, namelen); | |
214 dtabytes(&dt, TYnptr, 0, namelen + 1, name); | |
215 | |
216 ClassDeclarations aclasses; | |
217 int i; | |
218 | |
219 //printf("members->dim = %d\n", members->dim); | |
220 for (i = 0; i < members->dim; i++) | |
221 { | |
222 Dsymbol *member; | |
223 | |
224 member = (Dsymbol *)members->data[i]; | |
225 //printf("\tmember '%s'\n", member->toChars()); | |
226 member->addLocalClass(&aclasses); | |
227 } | |
228 | |
229 // importedModules[] | |
230 int aimports_dim = aimports.dim; | |
231 for (i = 0; i < aimports.dim; i++) | |
232 { Module *m = (Module *)aimports.data[i]; | |
233 if (!m->needModuleInfo()) | |
234 aimports_dim--; | |
235 } | |
236 dtdword(&dt, aimports_dim); | |
237 if (aimports.dim) | |
238 dtxoff(&dt, csym, sizeof_ModuleInfo, TYnptr); | |
239 else | |
240 dtdword(&dt, 0); | |
241 | |
242 // localClasses[] | |
243 dtdword(&dt, aclasses.dim); | |
244 if (aclasses.dim) | |
245 dtxoff(&dt, csym, sizeof_ModuleInfo + aimports_dim * PTRSIZE, TYnptr); | |
246 else | |
247 dtdword(&dt, 0); | |
248 | |
249 if (needmoduleinfo) | |
250 dtdword(&dt, 0); // flags (4 means MIstandalone) | |
251 else | |
252 dtdword(&dt, 4); // flags (4 means MIstandalone) | |
253 | |
254 if (sctor) | |
255 dtxoff(&dt, sctor, 0, TYnptr); | |
256 else | |
257 dtdword(&dt, 0); | |
258 | |
259 if (sdtor) | |
260 dtxoff(&dt, sdtor, 0, TYnptr); | |
261 else | |
262 dtdword(&dt, 0); | |
263 | |
264 if (stest) | |
265 dtxoff(&dt, stest, 0, TYnptr); | |
266 else | |
267 dtdword(&dt, 0); | |
268 | |
269 ////////////////////////////////////////////// | |
270 | |
271 for (i = 0; i < aimports.dim; i++) | |
272 { | |
273 Module *m; | |
274 | |
275 m = (Module *)aimports.data[i]; | |
276 if (m->needModuleInfo()) | |
277 { Symbol *s = m->toSymbol(); | |
278 s->Sflags |= SFLweak; | |
279 dtxoff(&dt, s, 0, TYnptr); | |
280 } | |
281 } | |
282 | |
283 for (i = 0; i < aclasses.dim; i++) | |
284 { | |
285 ClassDeclaration *cd; | |
286 | |
287 cd = (ClassDeclaration *)aclasses.data[i]; | |
288 dtxoff(&dt, cd->toSymbol(), 0, TYnptr); | |
289 } | |
290 | |
291 csym->Sdt = dt; | |
292 #if ELFOBJ | |
293 // Cannot be CONST because the startup code sets flag bits in it | |
294 csym->Sseg = DATA; | |
295 #endif | |
296 outdata(csym); | |
297 | |
298 ////////////////////////////////////////////// | |
299 | |
300 obj_moduleinfo(msym); | |
301 */ | |
1 | 302 } |
303 | |
304 /* ================================================================== */ | |
305 | |
306 void Dsymbol::toObjFile() | |
307 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
308 Logger::println("Ignoring Dsymbol::toObjFile for %s", toChars()); |
1 | 309 } |
310 | |
311 /* ================================================================== */ | |
312 | |
313 void Declaration::toObjFile() | |
314 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
315 Logger::println("Ignoring Declaration::toObjFile for %s", toChars()); |
1 | 316 } |
317 | |
318 /* ================================================================== */ | |
319 | |
320 void InterfaceDeclaration::toObjFile() | |
321 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
322 Logger::println("Ignoring InterfaceDeclaration::toObjFile for %s", toChars()); |
1 | 323 } |
324 | |
325 /* ================================================================== */ | |
326 | |
327 void StructDeclaration::toObjFile() | |
328 { | |
100 | 329 DtoDeclareStruct(this); |
1 | 330 } |
331 | |
332 /* ================================================================== */ | |
333 | |
77
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
334 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
|
335 { |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
336 // 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
|
337 if (cd->baseClass != 0) { |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
338 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
|
339 if (o != (unsigned)-1) |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
340 return o; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
341 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
342 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
343 // check this class |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
344 unsigned i; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
345 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
|
346 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
|
347 if (os == vd->offset) |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
348 return i+idx; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
349 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
350 idx += i; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
351 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
352 return (unsigned)-1; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
353 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
354 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
355 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
|
356 { |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
357 unsigned idx = 0; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
358 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
|
359 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
|
360 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
|
361 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
362 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
363 /* ================================================================== */ |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
364 |
1 | 365 void ClassDeclaration::toObjFile() |
366 { | |
100 | 367 DtoDeclareClass(this); |
1 | 368 } |
369 | |
370 /****************************************** | |
371 * Get offset of base class's vtbl[] initializer from start of csym. | |
372 * Returns ~0 if not this csym. | |
373 */ | |
374 | |
375 unsigned ClassDeclaration::baseVtblOffset(BaseClass *bc) | |
376 { | |
377 return ~0; | |
378 } | |
379 | |
380 /* ================================================================== */ | |
381 | |
382 void VarDeclaration::toObjFile() | |
383 { | |
40 | 384 Logger::print("VarDeclaration::toObjFile(): %s | %s\n", toChars(), type->toChars()); |
1 | 385 LOG_SCOPE; |
386 | |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
387 if (aliassym) |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
388 { |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
389 Logger::println("alias sym"); |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
390 toAlias()->toObjFile(); |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
391 return; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
392 } |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
393 |
1 | 394 // global variable or magic |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
395 if (isDataseg()) |
1 | 396 { |
40 | 397 if (llvmTouched) return; |
398 else llvmTouched = true; | |
399 | |
100 | 400 llvmIRGlobal = new IRGlobal(this); |
401 | |
402 Logger::println("parent: %s (%s)", parent->toChars(), parent->kind()); | |
403 | |
404 bool _isconst = isConst(); | |
405 if (parent && parent->isFuncDeclaration() && init && init->isExpInitializer()) | |
406 _isconst = false; | |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
407 |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
408 llvm::GlobalValue::LinkageTypes _linkage; |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
409 bool istempl = false; |
100 | 410 bool static_local = false; |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
411 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
|
412 _linkage = llvm::GlobalValue::WeakLinkage; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
413 istempl = true; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
414 } |
100 | 415 else if (parent && parent->isFuncDeclaration()) { |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
416 _linkage = llvm::GlobalValue::InternalLinkage; |
100 | 417 static_local = true; |
418 } | |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
419 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
|
420 _linkage = DtoLinkage(protection, storage_class); |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
421 |
100 | 422 const llvm::Type* _type = llvmIRGlobal->type.get(); |
1 | 423 |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
424 Logger::println("Creating global variable"); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
425 std::string _name(mangle()); |
52 | 426 |
100 | 427 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(_type,_isconst,_linkage,NULL,_name,gIR->module); |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
428 llvmValue = gvar; |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
429 |
100 | 430 if (static_local) |
431 DtoConstInitGlobal(this); | |
432 else | |
433 gIR->constInitQueue.push_back(this); | |
1 | 434 |
435 //if (storage_class & STCprivate) | |
436 // gvar->setVisibility(llvm::GlobalValue::ProtectedVisibility); | |
437 } | |
438 | |
439 // inside aggregate declaration. declare a field. | |
440 else | |
441 { | |
442 Logger::println("Aggregate var declaration: '%s' offset=%d", toChars(), offset); | |
443 | |
100 | 444 const llvm::Type* _type = DtoType(type); |
73 | 445 |
446 // add the field in the IRStruct | |
100 | 447 gIR->topstruct()->offsets.insert(std::make_pair(offset, IRStruct::Offset(this, _type))); |
1 | 448 } |
449 | |
450 Logger::println("VarDeclaration::toObjFile is done"); | |
451 } | |
452 | |
453 /* ================================================================== */ | |
454 | |
455 void TypedefDeclaration::toObjFile() | |
456 { | |
457 static int tdi = 0; | |
458 Logger::print("TypedefDeclaration::toObjFile(%d): %s\n", tdi++, toChars()); | |
459 LOG_SCOPE; | |
460 | |
52 | 461 // generate typeinfo |
72 | 462 type->getTypeInfo(NULL); |
1 | 463 } |
464 | |
465 /* ================================================================== */ | |
466 | |
467 void EnumDeclaration::toObjFile() | |
468 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
469 Logger::println("Ignoring EnumDeclaration::toObjFile for %s", toChars()); |
1 | 470 } |
471 | |
472 /* ================================================================== */ | |
473 | |
474 void FuncDeclaration::toObjFile() | |
475 { | |
100 | 476 DtoDeclareFunction(this); |
1 | 477 } |