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