Mercurial > projects > ldc
annotate gen/toobj.cpp @ 184:f5ca6bbbf1d7 trunk
[svn r200] Fixed: removed use of std.intrinsic.
Fixed: module info could potentially be masked by a previous reference, resulting in linking failure.
author | lindquist |
---|---|
date | Wed, 07 May 2008 22:01:59 +0200 |
parents | db9890b3fb64 |
children | 9d44ec83acd1 |
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 |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
44 #include "ir/irvar.h" |
139
0ab29b838084
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
lindquist
parents:
137
diff
changeset
|
45 #include "ir/irmodule.h" |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
46 |
1 | 47 ////////////////////////////////////////////////////////////////////////////////////////// |
48 | |
125 | 49 // in gen/optimize.cpp |
50 void llvmdc_optimize_module(llvm::Module* m, char lvl, bool doinline); | |
51 | |
52 ////////////////////////////////////////////////////////////////////////////////////////// | |
53 | |
54 void Module::genobjfile() | |
1 | 55 { |
56 Logger::cout() << "Generating module: " << (md ? md->toChars() : toChars()) << '\n'; | |
57 LOG_SCOPE; | |
58 | |
6 | 59 // start by deleting the old object file |
1 | 60 deleteObjFile(); |
61 | |
18 | 62 // create a new ir state |
139
0ab29b838084
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
lindquist
parents:
137
diff
changeset
|
63 // TODO look at making the instance static and moving most functionality into IrModule where it belongs |
1 | 64 IRState ir; |
65 gIR = &ir; | |
66 ir.dmodule = this; | |
67 | |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
68 // reset all IR data stored in Dsymbols and Types |
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
69 IrDsymbol::resetAll(); |
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
70 IrType::resetAll(); |
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
71 |
139
0ab29b838084
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
lindquist
parents:
137
diff
changeset
|
72 // module ir state |
0ab29b838084
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
lindquist
parents:
137
diff
changeset
|
73 // might already exist via import, just overwrite... |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
74 this->ir.irModule = new IrModule(this); |
139
0ab29b838084
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
lindquist
parents:
137
diff
changeset
|
75 |
6 | 76 // name the module |
1 | 77 std::string mname(toChars()); |
78 if (md != 0) | |
79 mname = md->toChars(); | |
80 ir.module = new llvm::Module(mname); | |
81 | |
6 | 82 // set target stuff |
1 | 83 std::string target_triple(global.params.tt_arch); |
84 target_triple.append(global.params.tt_os); | |
85 ir.module->setTargetTriple(target_triple); | |
86 ir.module->setDataLayout(global.params.data_layout); | |
87 | |
73 | 88 // heavily inspired by tools/llc/llc.cpp:200-230 |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
89 const llvm::TargetMachineRegistry::entry* targetEntry; |
73 | 90 std::string targetError; |
91 targetEntry = llvm::TargetMachineRegistry::getClosestStaticTargetForModule(*ir.module, targetError); | |
92 assert(targetEntry && "Failed to find a static target for module"); | |
93 std::auto_ptr<llvm::TargetMachine> targetPtr(targetEntry->CtorFn(*ir.module, "")); // TODO: replace "" with features | |
94 assert(targetPtr.get() && "Could not allocate target machine!"); | |
95 llvm::TargetMachine &targetMachine = *targetPtr.get(); | |
96 gTargetData = targetMachine.getTargetData(); | |
1 | 97 |
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
|
98 // 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
|
99 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
|
100 RegisterDwarfSymbols(ir.module); |
139
0ab29b838084
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
lindquist
parents:
137
diff
changeset
|
101 DtoDwarfCompileUnit(this); |
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
|
102 } |
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
|
103 |
103
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
104 // start out by providing opaque for the built-in class types |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
105 if (!ClassDeclaration::object->type->ir.type) |
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
106 ClassDeclaration::object->type->ir.type = new llvm::PATypeHolder(llvm::OpaqueType::get()); |
103
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
107 |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
108 if (!Type::typeinfo->type->ir.type) |
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
109 Type::typeinfo->type->ir.type = new llvm::PATypeHolder(llvm::OpaqueType::get()); |
103
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
110 |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
111 if (!ClassDeclaration::classinfo->type->ir.type) |
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
112 ClassDeclaration::classinfo->type->ir.type = new llvm::PATypeHolder(llvm::OpaqueType::get()); |
103
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
113 |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
114 /*if (!Type::typeinfoclass->type->ir.type) |
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
115 Type::typeinfoclass->type->ir.type = new llvm::PATypeHolder(llvm::OpaqueType::get());*/ |
106 | 116 |
6 | 117 // process module members |
1 | 118 for (int k=0; k < members->dim; k++) { |
119 Dsymbol* dsym = (Dsymbol*)(members->data[k]); | |
120 assert(dsym); | |
121 dsym->toObjFile(); | |
122 } | |
123 | |
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
|
124 // main driver loop |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
125 DtoEmptyAllLists(); |
89 | 126 // generate ModuleInfo |
127 genmoduleinfo(); | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
128 // do this again as moduleinfo might have pulled something in! |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
129 DtoEmptyAllLists(); |
89 | 130 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
131 // 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
|
132 if (ir.emitMain) { |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
133 //DtoMain(); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
134 } |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
10
diff
changeset
|
135 |
6 | 136 // verify the llvm |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
137 if (!global.params.novalidate) { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
138 std::string verifyErr; |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
139 Logger::println("Verifying module..."); |
117 | 140 LOG_SCOPE; |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
141 if (llvm::verifyModule(*ir.module,llvm::ReturnStatusAction,&verifyErr)) |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
142 { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
143 error("%s", verifyErr.c_str()); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
144 fatal(); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
145 } |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
146 else { |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
147 Logger::println("Verification passed!"); |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
28
diff
changeset
|
148 } |
1 | 149 } |
150 | |
125 | 151 // run optimizer |
131 | 152 llvmdc_optimize_module(ir.module, global.params.optimizeLevel, global.params.llvmInline); |
1 | 153 |
52 | 154 // write bytecode |
155 { | |
156 Logger::println("Writing LLVM bitcode\n"); | |
157 std::ofstream bos(bcfile->name->toChars(), std::ios::binary); | |
158 llvm::WriteBitcodeToFile(ir.module, bos); | |
159 } | |
1 | 160 |
52 | 161 // disassemble ? |
162 if (global.params.disassemble) { | |
163 Logger::println("Writing LLVM asm to: %s\n", llfile->name->toChars()); | |
164 std::ofstream aos(llfile->name->toChars()); | |
165 ir.module->print(aos); | |
166 } | |
1 | 167 |
168 delete ir.module; | |
125 | 169 gTargetData = 0; |
1 | 170 gIR = NULL; |
171 } | |
172 | |
173 /* ================================================================== */ | |
174 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
175 // build module ctor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
176 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
177 static llvm::Function* build_module_ctor() |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
178 { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
179 if (gIR->ctors.empty()) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
180 return NULL; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
181 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
182 size_t n = gIR->ctors.size(); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
183 if (n == 1) |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
184 return gIR->ctors[0]->ir.irFunc->func; |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
185 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
186 std::string name("_D"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
187 name.append(gIR->dmodule->mangle()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
188 name.append("6__ctorZ"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
189 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
190 std::vector<const llvm::Type*> argsTy; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
191 const llvm::FunctionType* fnTy = llvm::FunctionType::get(llvm::Type::VoidTy,argsTy,false); |
184 | 192 assert(gIR->module->getFunction(name) == NULL); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
193 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
|
194 fn->setCallingConv(llvm::CallingConv::Fast); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
195 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
196 llvm::BasicBlock* bb = new llvm::BasicBlock("entry", fn); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
197 LLVMBuilder builder(bb); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
198 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
199 for (size_t i=0; i<n; i++) { |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
200 llvm::Function* f = gIR->ctors[i]->ir.irFunc->func; |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
201 llvm::CallInst* call = builder.CreateCall(f,""); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
202 call->setCallingConv(llvm::CallingConv::Fast); |
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 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
205 builder.CreateRetVoid(); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
206 return fn; |
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 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
209 // build module dtor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
210 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
211 static llvm::Function* build_module_dtor() |
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 if (gIR->dtors.empty()) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
214 return NULL; |
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 size_t n = gIR->dtors.size(); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
217 if (n == 1) |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
218 return gIR->dtors[0]->ir.irFunc->func; |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
219 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
220 std::string name("_D"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
221 name.append(gIR->dmodule->mangle()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
222 name.append("6__dtorZ"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
223 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
224 std::vector<const llvm::Type*> argsTy; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
225 const llvm::FunctionType* fnTy = llvm::FunctionType::get(llvm::Type::VoidTy,argsTy,false); |
184 | 226 assert(gIR->module->getFunction(name) == NULL); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
227 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
|
228 fn->setCallingConv(llvm::CallingConv::Fast); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
229 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
230 llvm::BasicBlock* bb = new llvm::BasicBlock("entry", fn); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
231 LLVMBuilder builder(bb); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
232 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
233 for (size_t i=0; i<n; i++) { |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
234 llvm::Function* f = gIR->dtors[i]->ir.irFunc->func; |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
235 llvm::CallInst* call = builder.CreateCall(f,""); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
236 call->setCallingConv(llvm::CallingConv::Fast); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
237 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
238 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
239 builder.CreateRetVoid(); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
240 return fn; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
241 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
242 |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
243 // build module unittest |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
244 |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
245 static llvm::Function* build_module_unittest() |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
246 { |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
247 if (gIR->unitTests.empty()) |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
248 return NULL; |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
249 |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
250 size_t n = gIR->unitTests.size(); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
251 if (n == 1) |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
252 return gIR->unitTests[0]->ir.irFunc->func; |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
253 |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
254 std::string name("_D"); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
255 name.append(gIR->dmodule->mangle()); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
256 name.append("10__unittestZ"); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
257 |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
258 std::vector<const llvm::Type*> argsTy; |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
259 const llvm::FunctionType* fnTy = llvm::FunctionType::get(llvm::Type::VoidTy,argsTy,false); |
184 | 260 assert(gIR->module->getFunction(name) == NULL); |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
261 llvm::Function* fn = new llvm::Function(fnTy, llvm::GlobalValue::InternalLinkage, name, gIR->module); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
262 fn->setCallingConv(llvm::CallingConv::Fast); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
263 |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
264 llvm::BasicBlock* bb = new llvm::BasicBlock("entry", fn); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
265 LLVMBuilder builder(bb); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
266 |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
267 for (size_t i=0; i<n; i++) { |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
268 llvm::Function* f = gIR->unitTests[i]->ir.irFunc->func; |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
269 llvm::CallInst* call = builder.CreateCall(f,""); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
270 call->setCallingConv(llvm::CallingConv::Fast); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
271 } |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
272 |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
273 builder.CreateRetVoid(); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
274 return fn; |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
275 } |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
276 |
1 | 277 // Put out instance of ModuleInfo for this Module |
278 | |
279 void Module::genmoduleinfo() | |
280 { | |
89 | 281 // The layout is: |
282 // { | |
283 // void **vptr; | |
284 // monitor_t monitor; | |
285 // char[] name; // class name | |
286 // ModuleInfo importedModules[]; | |
287 // ClassInfo localClasses[]; | |
288 // uint flags; // initialization state | |
289 // void *ctor; | |
290 // void *dtor; | |
291 // void *unitTest; | |
292 // } | |
293 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
294 // resolve ModuleInfo |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
295 assert(moduleinfo); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
296 DtoForceConstInitDsymbol(moduleinfo); |
89 | 297 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
298 // moduleinfo llvm struct type |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
299 const llvm::StructType* moduleinfoTy = isaStruct(moduleinfo->type->ir.type->get()); |
89 | 300 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
301 // classinfo llvm struct type |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
302 const llvm::StructType* classinfoTy = isaStruct(ClassDeclaration::classinfo->type->ir.type->get()); |
89 | 303 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
304 // initializer vector |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
305 std::vector<llvm::Constant*> initVec; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
306 llvm::Constant* c = 0; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
307 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
308 // vtable |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
309 c = moduleinfo->ir.irStruct->vtbl; |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
310 initVec.push_back(c); |
89 | 311 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
312 // monitor |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
313 c = getNullPtr(getPtrToType(llvm::Type::Int8Ty)); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
314 initVec.push_back(c); |
89 | 315 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
316 // name |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
317 char *name = toPrettyChars(); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
318 c = DtoConstString(name); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
319 initVec.push_back(c); |
89 | 320 |
321 // importedModules[] | |
322 int aimports_dim = aimports.dim; | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
323 std::vector<llvm::Constant*> importInits; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
324 for (size_t i = 0; i < aimports.dim; i++) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
325 { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
326 Module *m = (Module *)aimports.data[i]; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
327 if (!m->needModuleInfo()) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
328 aimports_dim--; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
329 else { // declare |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
330 // create name |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
331 std::string m_name("_D"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
332 m_name.append(m->mangle()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
333 m_name.append("8__ModuleZ"); |
163
a8cd9bc1021a
[svn r179] lots and lots of fixes, much more of tango now compiles/works.
lindquist
parents:
157
diff
changeset
|
334 llvm::GlobalVariable* m_gvar = gIR->module->getGlobalVariable(m_name); |
a8cd9bc1021a
[svn r179] lots and lots of fixes, much more of tango now compiles/works.
lindquist
parents:
157
diff
changeset
|
335 if (!m_gvar) m_gvar = new llvm::GlobalVariable(moduleinfoTy, false, llvm::GlobalValue::ExternalLinkage, NULL, m_name, gIR->module); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
336 importInits.push_back(m_gvar); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
337 } |
89 | 338 } |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
339 // has import array? |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
340 if (!importInits.empty()) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
341 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
342 const llvm::ArrayType* importArrTy = llvm::ArrayType::get(getPtrToType(moduleinfoTy), importInits.size()); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
343 c = llvm::ConstantArray::get(importArrTy, importInits); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
344 std::string m_name("_D"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
345 m_name.append(mangle()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
346 m_name.append("9__importsZ"); |
163
a8cd9bc1021a
[svn r179] lots and lots of fixes, much more of tango now compiles/works.
lindquist
parents:
157
diff
changeset
|
347 llvm::GlobalVariable* m_gvar = gIR->module->getGlobalVariable(m_name); |
a8cd9bc1021a
[svn r179] lots and lots of fixes, much more of tango now compiles/works.
lindquist
parents:
157
diff
changeset
|
348 if (!m_gvar) m_gvar = new llvm::GlobalVariable(importArrTy, true, llvm::GlobalValue::InternalLinkage, c, m_name, gIR->module); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
349 c = llvm::ConstantExpr::getBitCast(m_gvar, getPtrToType(importArrTy->getElementType())); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
350 c = DtoConstSlice(DtoConstSize_t(importInits.size()), c); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
351 } |
89 | 352 else |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
353 c = moduleinfo->ir.irStruct->constInit->getOperand(3); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
354 initVec.push_back(c); |
89 | 355 |
356 // localClasses[] | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
357 ClassDeclarations aclasses; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
358 //printf("members->dim = %d\n", members->dim); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
359 for (size_t i = 0; i < members->dim; i++) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
360 { |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
361 Dsymbol *member; |
89 | 362 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
363 member = (Dsymbol *)members->data[i]; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
364 //printf("\tmember '%s'\n", member->toChars()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
365 member->addLocalClass(&aclasses); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
366 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
367 // fill inits |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
368 std::vector<llvm::Constant*> classInits; |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
369 for (size_t i = 0; i < aclasses.dim; i++) |
89 | 370 { |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
371 ClassDeclaration* cd = (ClassDeclaration*)aclasses.data[i]; |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
372 if (cd->isInterfaceDeclaration()) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
373 { |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
374 Logger::println("skipping interface '%s' in moduleinfo", cd->toPrettyChars()); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
375 continue; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
376 } |
132 | 377 Logger::println("class: %s", cd->toPrettyChars()); |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
378 assert(cd->ir.irStruct->classInfo); |
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
379 classInits.push_back(cd->ir.irStruct->classInfo); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
380 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
381 // has class array? |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
382 if (!classInits.empty()) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
383 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
384 const llvm::ArrayType* classArrTy = llvm::ArrayType::get(getPtrToType(classinfoTy), classInits.size()); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
385 c = llvm::ConstantArray::get(classArrTy, classInits); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
386 std::string m_name("_D"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
387 m_name.append(mangle()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
388 m_name.append("9__classesZ"); |
184 | 389 assert(gIR->module->getGlobalVariable(m_name) == NULL); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
390 llvm::GlobalVariable* m_gvar = new llvm::GlobalVariable(classArrTy, true, llvm::GlobalValue::InternalLinkage, c, m_name, gIR->module); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
391 c = llvm::ConstantExpr::getBitCast(m_gvar, getPtrToType(classArrTy->getElementType())); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
392 c = DtoConstSlice(DtoConstSize_t(classInits.size()), c); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
393 } |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
394 else |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
395 c = moduleinfo->ir.irStruct->constInit->getOperand(4); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
396 initVec.push_back(c); |
89 | 397 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
398 // flags |
132 | 399 c = DtoConstUint(0); |
400 if (!needmoduleinfo) | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
401 c = DtoConstUint(4); // flags (4 means MIstandalone) |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
402 initVec.push_back(c); |
89 | 403 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
404 // ctor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
405 llvm::Function* fctor = build_module_ctor(); |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
406 c = fctor ? fctor : moduleinfo->ir.irStruct->constInit->getOperand(6); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
407 initVec.push_back(c); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
408 |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
409 // dtor |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
410 llvm::Function* fdtor = build_module_dtor(); |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
411 c = fdtor ? fdtor : moduleinfo->ir.irStruct->constInit->getOperand(7); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
412 initVec.push_back(c); |
89 | 413 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
414 // unitTest |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
415 llvm::Function* unittest = build_module_unittest(); |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
416 c = unittest ? unittest : moduleinfo->ir.irStruct->constInit->getOperand(8); |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
417 initVec.push_back(c); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
418 |
132 | 419 // xgetMembers |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
420 c = moduleinfo->ir.irStruct->constInit->getOperand(9); |
132 | 421 initVec.push_back(c); |
422 | |
423 // ictor | |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
424 c = moduleinfo->ir.irStruct->constInit->getOperand(10); |
132 | 425 initVec.push_back(c); |
426 | |
427 /*Logger::println("MODULE INFO INITIALIZERS"); | |
428 for (size_t i=0; i<initVec.size(); ++i) | |
429 { | |
430 Logger::cout() << *initVec[i] << '\n'; | |
431 if (initVec[i]->getType() != moduleinfoTy->getElementType(i)) | |
432 assert(0); | |
433 }*/ | |
434 | |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
435 // create initializer |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
436 llvm::Constant* constMI = llvm::ConstantStruct::get(moduleinfoTy, initVec); |
89 | 437 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
438 // create name |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
439 std::string MIname("_D"); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
440 MIname.append(mangle()); |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
441 MIname.append("8__ModuleZ"); |
89 | 442 |
116
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
443 // declare |
fd7ad91fd713
[svn r120] ModuleInfo implementation is now almost complete.
lindquist
parents:
115
diff
changeset
|
444 // flags will be modified at runtime so can't make it constant |
184 | 445 |
446 llvm::GlobalVariable* gvar = gIR->module->getGlobalVariable(MIname); | |
447 if (!gvar) gvar = new llvm::GlobalVariable(moduleinfoTy, false, llvm::GlobalValue::ExternalLinkage, NULL, MIname, gIR->module); | |
448 gvar->setInitializer(constMI); | |
117 | 449 |
450 // declare the appending array | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
451 const llvm::ArrayType* appendArrTy = llvm::ArrayType::get(getPtrToType(llvm::Type::Int8Ty), 1); |
117 | 452 std::vector<llvm::Constant*> appendInits; |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
453 appendInits.push_back(llvm::ConstantExpr::getBitCast(gvar, getPtrToType(llvm::Type::Int8Ty))); |
117 | 454 llvm::Constant* appendInit = llvm::ConstantArray::get(appendArrTy, appendInits); |
455 std::string appendName("_d_moduleinfo_array"); | |
456 llvm::GlobalVariable* appendVar = new llvm::GlobalVariable(appendArrTy, true, llvm::GlobalValue::AppendingLinkage, appendInit, appendName, gIR->module); | |
1 | 457 } |
458 | |
459 /* ================================================================== */ | |
460 | |
461 void Dsymbol::toObjFile() | |
462 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
463 Logger::println("Ignoring Dsymbol::toObjFile for %s", toChars()); |
1 | 464 } |
465 | |
466 /* ================================================================== */ | |
467 | |
468 void Declaration::toObjFile() | |
469 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
470 Logger::println("Ignoring Declaration::toObjFile for %s", toChars()); |
1 | 471 } |
472 | |
473 /* ================================================================== */ | |
474 | |
475 void InterfaceDeclaration::toObjFile() | |
476 { | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
477 //Logger::println("Ignoring InterfaceDeclaration::toObjFile for %s", toChars()); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
478 gIR->resolveList.push_back(this); |
1 | 479 } |
480 | |
481 /* ================================================================== */ | |
482 | |
483 void StructDeclaration::toObjFile() | |
484 { | |
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
|
485 gIR->resolveList.push_back(this); |
1 | 486 } |
487 | |
488 /* ================================================================== */ | |
489 | |
490 void ClassDeclaration::toObjFile() | |
491 { | |
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
|
492 gIR->resolveList.push_back(this); |
1 | 493 } |
494 | |
495 /****************************************** | |
496 * Get offset of base class's vtbl[] initializer from start of csym. | |
497 * Returns ~0 if not this csym. | |
498 */ | |
499 | |
500 unsigned ClassDeclaration::baseVtblOffset(BaseClass *bc) | |
501 { | |
502 return ~0; | |
503 } | |
504 | |
505 /* ================================================================== */ | |
506 | |
507 void VarDeclaration::toObjFile() | |
508 { | |
40 | 509 Logger::print("VarDeclaration::toObjFile(): %s | %s\n", toChars(), type->toChars()); |
1 | 510 LOG_SCOPE; |
511 | |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
512 if (aliassym) |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
513 { |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
514 Logger::println("alias sym"); |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
515 toAlias()->toObjFile(); |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
516 return; |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
517 } |
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
518 |
1 | 519 // global variable or magic |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
520 if (isDataseg()) |
1 | 521 { |
132 | 522 // we don't want to touch private static members at all !!! |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
523 if ((prot() == PROTprivate) && getModule() != gIR->dmodule) |
132 | 524 return; |
525 | |
526 // don't duplicate work | |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
527 if (this->ir.resolved) return; |
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
528 this->ir.resolved = true; |
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
529 this->ir.declared = true; |
40 | 530 |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
531 this->ir.irGlobal = new IrGlobal(this); |
100 | 532 |
533 Logger::println("parent: %s (%s)", parent->toChars(), parent->kind()); | |
534 | |
149
4c577c2b7229
[svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents:
139
diff
changeset
|
535 // handle static local variables |
100 | 536 bool static_local = false; |
149
4c577c2b7229
[svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents:
139
diff
changeset
|
537 bool _isconst = isConst(); |
4c577c2b7229
[svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents:
139
diff
changeset
|
538 if (parent && parent->isFuncDeclaration()) |
4c577c2b7229
[svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents:
139
diff
changeset
|
539 { |
4c577c2b7229
[svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents:
139
diff
changeset
|
540 static_local = true; |
4c577c2b7229
[svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents:
139
diff
changeset
|
541 if (init && init->isExpInitializer()) { |
4c577c2b7229
[svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents:
139
diff
changeset
|
542 _isconst = false; |
4c577c2b7229
[svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents:
139
diff
changeset
|
543 } |
85
f869c636a113
[svn r89] Fixed a bunch of problems with template instance across multiple modules.
lindquist
parents:
84
diff
changeset
|
544 } |
149
4c577c2b7229
[svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents:
139
diff
changeset
|
545 |
4c577c2b7229
[svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents:
139
diff
changeset
|
546 Logger::println("Creating global variable"); |
26
99737f94abfb
[svn r30] * Fixed static function-local variables.
lindquist
parents:
24
diff
changeset
|
547 |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
548 const llvm::Type* _type = this->ir.irGlobal->type.get(); |
149
4c577c2b7229
[svn r155] Fixed a bunch of linkage problems (especially with templates)
lindquist
parents:
139
diff
changeset
|
549 llvm::GlobalValue::LinkageTypes _linkage = DtoLinkage(this); |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
550 std::string _name(mangle()); |
52 | 551 |
100 | 552 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(_type,_isconst,_linkage,NULL,_name,gIR->module); |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
553 this->ir.irGlobal->value = gvar; |
88
058d3925950e
[svn r92] Fixed support for statically initialized unions. lots of bugfixes as cleanups too.
lindquist
parents:
86
diff
changeset
|
554 |
163
a8cd9bc1021a
[svn r179] lots and lots of fixes, much more of tango now compiles/works.
lindquist
parents:
157
diff
changeset
|
555 Logger::cout() << *gvar << '\n'; |
a8cd9bc1021a
[svn r179] lots and lots of fixes, much more of tango now compiles/works.
lindquist
parents:
157
diff
changeset
|
556 |
100 | 557 if (static_local) |
558 DtoConstInitGlobal(this); | |
559 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
|
560 gIR->constInitList.push_back(this); |
1 | 561 } |
562 | |
563 // inside aggregate declaration. declare a field. | |
564 else | |
565 { | |
566 Logger::println("Aggregate var declaration: '%s' offset=%d", toChars(), offset); | |
567 | |
100 | 568 const llvm::Type* _type = DtoType(type); |
173
db9890b3fb64
[svn r189] moving IR data back into DMD frontend nodes
ChristianK
parents:
163
diff
changeset
|
569 this->ir.irField = new IrField(this); |
73 | 570 |
571 // add the field in the IRStruct | |
136
0e28624814e8
[svn r140] did a lot of the work towards being able to pass multiple modules on the command line. not complete yet though
lindquist
parents:
133
diff
changeset
|
572 gIR->topstruct()->offsets.insert(std::make_pair(offset, IrStruct::Offset(this, _type))); |
1 | 573 } |
574 | |
575 Logger::println("VarDeclaration::toObjFile is done"); | |
576 } | |
577 | |
578 /* ================================================================== */ | |
579 | |
580 void TypedefDeclaration::toObjFile() | |
581 { | |
582 static int tdi = 0; | |
583 Logger::print("TypedefDeclaration::toObjFile(%d): %s\n", tdi++, toChars()); | |
584 LOG_SCOPE; | |
585 | |
52 | 586 // generate typeinfo |
72 | 587 type->getTypeInfo(NULL); |
1 | 588 } |
589 | |
590 /* ================================================================== */ | |
591 | |
592 void EnumDeclaration::toObjFile() | |
593 { | |
38
27b2f40bdb58
[svn r42] Disabled the extensive logging by default. Use the -vv flag to get it back.
lindquist
parents:
31
diff
changeset
|
594 Logger::println("Ignoring EnumDeclaration::toObjFile for %s", toChars()); |
1 | 595 } |
596 | |
597 /* ================================================================== */ | |
598 | |
599 void FuncDeclaration::toObjFile() | |
600 { | |
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
|
601 gIR->resolveList.push_back(this); |
1 | 602 } |