Mercurial > projects > ldc
annotate gen/toobj.cpp @ 119:79c9ac745fbc trunk
[svn r123] Fixed some typeinfo module name mismatches.
Fixed D-style varargs returning through pointer.
Fixed converting nested function to delegate.
Added std.string and std.format to Phobos.
author | lindquist |
---|---|
date | Mon, 26 Nov 2007 06:45:13 +0100 |
parents | 56a21f3e5d3e |
children | 9c79b61fb638 |
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 |
103
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
88 // start out by providing opaque for the built-in class types |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
89 if (!ClassDeclaration::object->type->llvmType) |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
90 ClassDeclaration::object->type->llvmType = new llvm::PATypeHolder(llvm::OpaqueType::get()); |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
91 |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
92 if (!Type::typeinfo->type->llvmType) |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
93 Type::typeinfo->type->llvmType = new llvm::PATypeHolder(llvm::OpaqueType::get()); |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
94 |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
95 if (!ClassDeclaration::classinfo->type->llvmType) |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
96 ClassDeclaration::classinfo->type->llvmType = new llvm::PATypeHolder(llvm::OpaqueType::get()); |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
97 |
106 | 98 /*if (!Type::typeinfoclass->type->llvmType) |
99 Type::typeinfoclass->type->llvmType = new llvm::PATypeHolder(llvm::OpaqueType::get());*/ | |
100 | |
6 | 101 // process module members |
1 | 102 for (int k=0; k < members->dim; k++) { |
103 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
104 assert(dsym); | |
105 dsym->toObjFile(); | |
106 } | |
107 | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
108 // main driver loop |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
109 DtoEmptyAllLists(); |
89 | 110 // generate ModuleInfo |
111 genmoduleinfo(); | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
112 // do this again as moduleinfo might have pulled something in! |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
113 DtoEmptyAllLists(); |
89 | 114 |
1 | 115 gTargetData = 0; |
116 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
117 // 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
|
118 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
|
119 DtoMain(); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
120 } |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
121 |
6 | 122 // verify the llvm |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
123 if (!global.params.novalidate) { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
124 std::string verifyErr; |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
125 Logger::println("Verifying module..."); |
117 | 126 LOG_SCOPE; |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
127 if (llvm::verifyModule(*ir.module,llvm::ReturnStatusAction,&verifyErr)) |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
128 { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
129 error("%s", verifyErr.c_str()); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
130 fatal(); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
131 } |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
132 else { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
133 Logger::println("Verification passed!"); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
134 } |
1 | 135 } |
136 | |
137 // run passes | |
138 // TODO | |
139 | |
52 | 140 // write bytecode |
141 { | |
142 Logger::println("Writing LLVM bitcode\n"); | |
143 std::ofstream bos(bcfile->name->toChars(), std::ios::binary); | |
144 llvm::WriteBitcodeToFile(ir.module, bos); | |
145 } | |
1 | 146 |
52 | 147 // disassemble ? |
148 if (global.params.disassemble) { | |
149 Logger::println("Writing LLVM asm to: %s\n", llfile->name->toChars()); | |
150 std::ofstream aos(llfile->name->toChars()); | |
151 ir.module->print(aos); | |
152 } | |
1 | 153 |
154 delete ir.module; | |
155 gIR = NULL; | |
156 } | |
157 | |
158 /* ================================================================== */ | |
159 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
160 // build module ctor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
161 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
162 static llvm::Function* build_module_ctor() |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
163 { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
164 if (gIR->ctors.empty()) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
165 return NULL; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
166 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
167 size_t n = gIR->ctors.size(); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
168 if (n == 1) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
169 return llvm::cast<llvm::Function>(gIR->ctors[0]->llvmValue); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
170 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
171 std::string name("_D"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
172 name.append(gIR->dmodule->mangle()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
173 name.append("6__ctorZ"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
174 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
175 std::vector<const llvm::Type*> argsTy; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
176 const llvm::FunctionType* fnTy = llvm::FunctionType::get(llvm::Type::VoidTy,argsTy,false); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
177 llvm::Function* fn = new llvm::Function(fnTy, llvm::GlobalValue::InternalLinkage, name, gIR->module); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
178 fn->setCallingConv(llvm::CallingConv::Fast); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
179 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
180 llvm::BasicBlock* bb = new llvm::BasicBlock("entry", fn); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
181 LLVMBuilder builder(bb); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
182 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
183 for (size_t i=0; i<n; i++) { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
184 llvm::Function* f = llvm::cast<llvm::Function>(gIR->ctors[i]->llvmValue); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
185 llvm::CallInst* call = builder.CreateCall(f,""); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
186 call->setCallingConv(llvm::CallingConv::Fast); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
187 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
188 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
189 builder.CreateRetVoid(); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
190 return fn; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
191 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
192 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
193 // build module dtor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
194 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
195 static llvm::Function* build_module_dtor() |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
196 { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
197 if (gIR->dtors.empty()) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
198 return NULL; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
199 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
200 size_t n = gIR->dtors.size(); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
201 if (n == 1) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
202 return llvm::cast<llvm::Function>(gIR->dtors[0]->llvmValue); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
203 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
204 std::string name("_D"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
205 name.append(gIR->dmodule->mangle()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
206 name.append("6__dtorZ"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
207 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
208 std::vector<const llvm::Type*> argsTy; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
209 const llvm::FunctionType* fnTy = llvm::FunctionType::get(llvm::Type::VoidTy,argsTy,false); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
210 llvm::Function* fn = new llvm::Function(fnTy, llvm::GlobalValue::InternalLinkage, name, gIR->module); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
211 fn->setCallingConv(llvm::CallingConv::Fast); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
212 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
213 llvm::BasicBlock* bb = new llvm::BasicBlock("entry", fn); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
214 LLVMBuilder builder(bb); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
215 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
216 for (size_t i=0; i<n; i++) { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
217 llvm::Function* f = llvm::cast<llvm::Function>(gIR->dtors[i]->llvmValue); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
218 llvm::CallInst* call = builder.CreateCall(f,""); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
219 call->setCallingConv(llvm::CallingConv::Fast); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
220 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
221 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
222 builder.CreateRetVoid(); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
223 return fn; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
224 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
225 |
1 | 226 // Put out instance of ModuleInfo for this Module |
227 | |
228 void Module::genmoduleinfo() | |
229 { | |
89 | 230 // The layout is: |
231 // { | |
232 // void **vptr; | |
233 // monitor_t monitor; | |
234 // char[] name; // class name | |
235 // ModuleInfo importedModules[]; | |
236 // ClassInfo localClasses[]; | |
237 // uint flags; // initialization state | |
238 // void *ctor; | |
239 // void *dtor; | |
240 // void *unitTest; | |
241 // } | |
242 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
243 // resolve ModuleInfo |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
244 assert(moduleinfo); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
245 DtoForceConstInitDsymbol(moduleinfo); |
89 | 246 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
247 // moduleinfo llvm struct type |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
248 const llvm::StructType* moduleinfoTy = isaStruct(moduleinfo->type->llvmType->get()); |
89 | 249 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
250 // classinfo llvm struct type |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
251 const llvm::StructType* classinfoTy = isaStruct(ClassDeclaration::classinfo->type->llvmType->get()); |
89 | 252 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
253 // initializer vector |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
254 std::vector<llvm::Constant*> initVec; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
255 llvm::Constant* c = 0; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
256 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
257 // vtable |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
258 c = moduleinfo->llvmVtbl; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
259 initVec.push_back(c); |
89 | 260 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
261 // monitor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
262 c = llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty)); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
263 initVec.push_back(c); |
89 | 264 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
265 // name |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
266 char *name = toPrettyChars(); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
267 c = DtoConstString(name); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
268 initVec.push_back(c); |
89 | 269 |
270 // importedModules[] | |
271 int aimports_dim = aimports.dim; | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
272 std::vector<llvm::Constant*> importInits; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
273 for (size_t i = 0; i < aimports.dim; i++) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
274 { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
275 Module *m = (Module *)aimports.data[i]; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
276 if (!m->needModuleInfo()) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
277 aimports_dim--; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
278 else { // declare |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
279 // create name |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
280 std::string m_name("_D"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
281 m_name.append(m->mangle()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
282 m_name.append("8__ModuleZ"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
283 llvm::GlobalVariable* m_gvar = new llvm::GlobalVariable(moduleinfoTy, false, llvm::GlobalValue::ExternalLinkage, NULL, m_name, gIR->module); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
284 importInits.push_back(m_gvar); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
285 } |
89 | 286 } |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
287 // has import array? |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
288 if (!importInits.empty()) { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
289 const llvm::ArrayType* importArrTy = llvm::ArrayType::get(llvm::PointerType::get(moduleinfoTy), importInits.size()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
290 c = llvm::ConstantArray::get(importArrTy, importInits); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
291 std::string m_name("_D"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
292 m_name.append(mangle()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
293 m_name.append("9__importsZ"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
294 llvm::GlobalVariable* m_gvar = new llvm::GlobalVariable(importArrTy, true, llvm::GlobalValue::InternalLinkage, c, m_name, gIR->module); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
295 c = llvm::ConstantExpr::getBitCast(m_gvar, llvm::PointerType::get(importArrTy->getElementType())); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
296 c = DtoConstSlice(DtoConstSize_t(importInits.size()), c); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
297 } |
89 | 298 else |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
299 c = moduleinfo->llvmInitZ->getOperand(3); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
300 initVec.push_back(c); |
89 | 301 |
302 // localClasses[] | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
303 ClassDeclarations aclasses; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
304 //printf("members->dim = %d\n", members->dim); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
305 for (size_t i = 0; i < members->dim; i++) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
306 { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
307 Dsymbol *member; |
89 | 308 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
309 member = (Dsymbol *)members->data[i]; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
310 //printf("\tmember '%s'\n", member->toChars()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
311 member->addLocalClass(&aclasses); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
312 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
313 // fill inits |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
314 std::vector<llvm::Constant*> classInits; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
315 for (size_t i = 0; i < aclasses.dim; i++) |
89 | 316 { |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
317 ClassDeclaration* cd = (ClassDeclaration*)aclasses.data[i]; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
318 assert(cd->llvmClass); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
319 classInits.push_back(cd->llvmClass); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
320 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
321 // has class array? |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
322 if (!classInits.empty()) { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
323 const llvm::ArrayType* classArrTy = llvm::ArrayType::get(llvm::PointerType::get(classinfoTy), classInits.size()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
324 c = llvm::ConstantArray::get(classArrTy, classInits); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
325 std::string m_name("_D"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
326 m_name.append(mangle()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
327 m_name.append("9__classesZ"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
328 llvm::GlobalVariable* m_gvar = new llvm::GlobalVariable(classArrTy, true, llvm::GlobalValue::InternalLinkage, c, m_name, gIR->module); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
329 c = llvm::ConstantExpr::getBitCast(m_gvar, llvm::PointerType::get(classArrTy->getElementType())); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
330 c = DtoConstSlice(DtoConstSize_t(classInits.size()), c); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
331 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
332 else |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
333 c = moduleinfo->llvmInitZ->getOperand(4); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
334 initVec.push_back(c); |
89 | 335 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
336 // flags |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
337 if (needmoduleinfo) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
338 c = DtoConstUint(0); // flags (4 means MIstandalone) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
339 else |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
340 c = DtoConstUint(4); // flags (4 means MIstandalone) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
341 initVec.push_back(c); |
89 | 342 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
343 // ctor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
344 llvm::Function* fctor = build_module_ctor(); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
345 c = fctor ? fctor : moduleinfo->llvmInitZ->getOperand(6); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
346 initVec.push_back(c); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
347 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
348 // dtor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
349 llvm::Function* fdtor = build_module_dtor(); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
350 c = fdtor ? fdtor : moduleinfo->llvmInitZ->getOperand(7); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
351 initVec.push_back(c); |
89 | 352 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
353 // unitTest |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
354 c = moduleinfo->llvmInitZ->getOperand(8); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
355 initVec.push_back(c); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
356 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
357 // create initializer |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
358 llvm::Constant* constMI = llvm::ConstantStruct::get(moduleinfoTy, initVec); |
89 | 359 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
360 // create name |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
361 std::string MIname("_D"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
362 MIname.append(mangle()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
363 MIname.append("8__ModuleZ"); |
89 | 364 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
365 // declare |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
366 // flags will be modified at runtime so can't make it constant |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
367 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(moduleinfoTy, false, llvm::GlobalValue::ExternalLinkage, constMI, MIname, gIR->module); |
117 | 368 |
369 // declare the appending array | |
370 const llvm::ArrayType* appendArrTy = llvm::ArrayType::get(llvm::PointerType::get(llvm::Type::Int8Ty), 1); | |
371 std::vector<llvm::Constant*> appendInits; | |
372 appendInits.push_back(llvm::ConstantExpr::getBitCast(gvar, llvm::PointerType::get(llvm::Type::Int8Ty))); | |
373 llvm::Constant* appendInit = llvm::ConstantArray::get(appendArrTy, appendInits); | |
374 std::string appendName("_d_moduleinfo_array"); | |
375 llvm::GlobalVariable* appendVar = new llvm::GlobalVariable(appendArrTy, true, llvm::GlobalValue::AppendingLinkage, appendInit, appendName, gIR->module); | |
1 | 376 } |
377 | |
378 /* ================================================================== */ | |
379 | |
380 void Dsymbol::toObjFile() | |
381 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
382 Logger::println("Ignoring Dsymbol::toObjFile for %s", toChars()); |
1 | 383 } |
384 | |
385 /* ================================================================== */ | |
386 | |
387 void Declaration::toObjFile() | |
388 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
389 Logger::println("Ignoring Declaration::toObjFile for %s", toChars()); |
1 | 390 } |
391 | |
392 /* ================================================================== */ | |
393 | |
394 void InterfaceDeclaration::toObjFile() | |
395 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
396 Logger::println("Ignoring InterfaceDeclaration::toObjFile for %s", toChars()); |
1 | 397 } |
398 | |
399 /* ================================================================== */ | |
400 | |
401 void StructDeclaration::toObjFile() | |
402 { | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
403 gIR->resolveList.push_back(this); |
1 | 404 } |
405 | |
406 /* ================================================================== */ | |
407 | |
77
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
408 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
|
409 { |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
410 // 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
|
411 if (cd->baseClass != 0) { |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
412 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
|
413 if (o != (unsigned)-1) |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
414 return o; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
415 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
416 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
417 // check this class |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
418 unsigned i; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
419 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
|
420 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
|
421 if (os == vd->offset) |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
422 return i+idx; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
423 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
424 idx += i; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
425 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
426 return (unsigned)-1; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
427 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
428 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
429 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
|
430 { |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
431 unsigned idx = 0; |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
432 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
|
433 assert(r != (unsigned)-1 && "Offset not found in any aggregate field"); |
115
5ba6d286c941
[svn r119] Added the monitor data field that comes after the vtable pointer to all classes. Represented as a void* initialized to zero.
lindquist
parents:
113
diff
changeset
|
434 // vtable is 0, monitor is 1 |
5ba6d286c941
[svn r119] Added the monitor data field that comes after the vtable pointer to all classes. Represented as a void* initialized to zero.
lindquist
parents:
113
diff
changeset
|
435 r += 2; |
5ba6d286c941
[svn r119] Added the monitor data field that comes after the vtable pointer to all classes. Represented as a void* initialized to zero.
lindquist
parents:
113
diff
changeset
|
436 // interface offset further |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
106
diff
changeset
|
437 r += vtblInterfaces->dim; |
115
5ba6d286c941
[svn r119] Added the monitor data field that comes after the vtable pointer to all classes. Represented as a void* initialized to zero.
lindquist
parents:
113
diff
changeset
|
438 // the final index was not pushed |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
106
diff
changeset
|
439 result.push_back(r); |
77
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
440 } |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
441 |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
442 /* ================================================================== */ |
714057ff2dbb
[svn r81] Fixed: Union support was very buggy. Should be fairly solid now.
lindquist
parents:
74
diff
changeset
|
443 |
1 | 444 void ClassDeclaration::toObjFile() |
445 { | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
446 gIR->resolveList.push_back(this); |
1 | 447 } |
448 | |
449 /****************************************** | |
450 * Get offset of base class's vtbl[] initializer from start of csym. | |
451 * Returns ~0 if not this csym. | |
452 */ | |
453 | |
454 unsigned ClassDeclaration::baseVtblOffset(BaseClass *bc) | |
455 { | |
456 return ~0; | |
457 } | |
458 | |
459 /* ================================================================== */ | |
460 | |
461 void VarDeclaration::toObjFile() | |
462 { | |
40 | 463 Logger::print("VarDeclaration::toObjFile(): %s | %s\n", toChars(), type->toChars()); |
1 | 464 LOG_SCOPE; |
465 | |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
466 if (aliassym) |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
467 { |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
468 Logger::println("alias sym"); |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
469 toAlias()->toObjFile(); |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
470 return; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
471 } |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
472 |
1 | 473 // global variable or magic |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
474 if (isDataseg()) |
1 | 475 { |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
476 if (llvmResolved) return; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
477 llvmResolved = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
478 llvmDeclared = true; |
40 | 479 |
100 | 480 llvmIRGlobal = new IRGlobal(this); |
481 | |
482 Logger::println("parent: %s (%s)", parent->toChars(), parent->kind()); | |
483 | |
484 bool _isconst = isConst(); | |
485 if (parent && parent->isFuncDeclaration() && init && init->isExpInitializer()) | |
486 _isconst = false; | |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
487 |
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
488 llvm::GlobalValue::LinkageTypes _linkage; |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
489 bool istempl = false; |
100 | 490 bool static_local = false; |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
491 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
|
492 _linkage = llvm::GlobalValue::WeakLinkage; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
493 istempl = true; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
494 } |
100 | 495 else if (parent && parent->isFuncDeclaration()) { |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
496 _linkage = llvm::GlobalValue::InternalLinkage; |
100 | 497 static_local = true; |
498 } | |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
499 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
|
500 _linkage = DtoLinkage(protection, storage_class); |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
501 |
100 | 502 const llvm::Type* _type = llvmIRGlobal->type.get(); |
1 | 503 |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
504 Logger::println("Creating global variable"); |
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
505 std::string _name(mangle()); |
52 | 506 |
100 | 507 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
|
508 llvmValue = gvar; |
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
509 |
100 | 510 if (static_local) |
511 DtoConstInitGlobal(this); | |
512 else | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
513 gIR->constInitList.push_back(this); |
1 | 514 |
515 //if (storage_class & STCprivate) | |
516 // gvar->setVisibility(llvm::GlobalValue::ProtectedVisibility); | |
517 } | |
518 | |
519 // inside aggregate declaration. declare a field. | |
520 else | |
521 { | |
522 Logger::println("Aggregate var declaration: '%s' offset=%d", toChars(), offset); | |
523 | |
100 | 524 const llvm::Type* _type = DtoType(type); |
73 | 525 |
526 // add the field in the IRStruct | |
100 | 527 gIR->topstruct()->offsets.insert(std::make_pair(offset, IRStruct::Offset(this, _type))); |
1 | 528 } |
529 | |
530 Logger::println("VarDeclaration::toObjFile is done"); | |
531 } | |
532 | |
533 /* ================================================================== */ | |
534 | |
535 void TypedefDeclaration::toObjFile() | |
536 { | |
537 static int tdi = 0; | |
538 Logger::print("TypedefDeclaration::toObjFile(%d): %s\n", tdi++, toChars()); | |
539 LOG_SCOPE; | |
540 | |
52 | 541 // generate typeinfo |
72 | 542 type->getTypeInfo(NULL); |
1 | 543 } |
544 | |
545 /* ================================================================== */ | |
546 | |
547 void EnumDeclaration::toObjFile() | |
548 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
549 Logger::println("Ignoring EnumDeclaration::toObjFile for %s", toChars()); |
1 | 550 } |
551 | |
552 /* ================================================================== */ | |
553 | |
554 void FuncDeclaration::toObjFile() | |
555 { | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
556 gIR->resolveList.push_back(this); |
1 | 557 } |