Mercurial > projects > ldc
annotate gen/classes.cpp @ 133:44a95ac7368a trunk
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
In particular, assertions has been fixed to include file/line info, and much more!
author | lindquist |
---|---|
date | Mon, 14 Jan 2008 05:11:54 +0100 |
parents | 1700239cab2e |
children | 0e28624814e8 |
rev | line source |
---|---|
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1 #include <sstream> |
100 | 2 #include "gen/llvm.h" |
3 | |
4 #include "mtype.h" | |
5 #include "aggregate.h" | |
6 #include "init.h" | |
7 #include "declaration.h" | |
8 | |
9 #include "gen/irstate.h" | |
10 #include "gen/tollvm.h" | |
11 #include "gen/arrays.h" | |
12 #include "gen/logger.h" | |
13 #include "gen/classes.h" | |
132 | 14 #include "gen/structs.h" |
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
|
15 #include "gen/functions.h" |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
16 #include "gen/runtime.h" |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
17 #include "gen/dvalue.h" |
100 | 18 |
19 ////////////////////////////////////////////////////////////////////////////////////////// | |
20 | |
21 static void LLVM_AddBaseClassData(BaseClasses* bcs) | |
22 { | |
23 // add base class data members first | |
24 for (int j=0; j<bcs->dim; j++) | |
25 { | |
26 BaseClass* bc = (BaseClass*)(bcs->data[j]); | |
27 assert(bc); | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
28 if (bc->base->isInterfaceDeclaration()) |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
29 continue; // interfaces only has methods |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
30 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
31 LLVM_AddBaseClassData(&bc->base->baseclasses); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
32 |
100 | 33 Logger::println("Adding base class members of %s", bc->base->toChars()); |
34 LOG_SCOPE; | |
35 | |
132 | 36 Array* arr = &bc->base->fields; |
37 for (int k=0; k < arr->dim; k++) { | |
38 VarDeclaration* v = (VarDeclaration*)(arr->data[k]); | |
39 v->toObjFile(); | |
40 } | |
41 | |
42 /*for (int k=0; k < bc->base->members->dim; k++) { | |
100 | 43 Dsymbol* dsym = (Dsymbol*)(bc->base->members->data[k]); |
44 if (dsym->isVarDeclaration()) | |
45 { | |
46 dsym->toObjFile(); | |
47 } | |
132 | 48 }*/ |
100 | 49 } |
50 } | |
51 | |
52 ////////////////////////////////////////////////////////////////////////////////////////// | |
53 | |
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
|
54 void DtoResolveClass(ClassDeclaration* cd) |
100 | 55 { |
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
|
56 if (cd->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
|
57 cd->llvmResolved = true; |
100 | 58 |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
59 Logger::println("DtoResolveClass(%s): %s", cd->toPrettyChars(), cd->loc.toChars()); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
60 LOG_SCOPE; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
61 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
62 // get the TypeClass |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
63 assert(cd->type->ty == Tclass); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
64 TypeClass* ts = (TypeClass*)cd->type; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
65 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
66 // make sure the IRStruct is created |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
67 IRStruct* irstruct = cd->llvmIRStruct; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
68 if (!irstruct) { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
69 irstruct = new IRStruct(ts); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
70 cd->llvmIRStruct = irstruct; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
71 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
72 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
73 // resolve the base class |
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
|
74 if (cd->baseClass) { |
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
|
75 DtoResolveClass(cd->baseClass); |
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
|
76 } |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
77 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
78 // resolve interfaces |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
79 if (cd->vtblInterfaces) { |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
80 for (int i=0; i < cd->vtblInterfaces->dim; i++) { |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
81 BaseClass *b = (BaseClass *)cd->vtblInterfaces->data[i]; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
82 ClassDeclaration *id = b->base; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
83 DtoResolveClass(id); |
114 | 84 // Fill in vtbl[] |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
85 b->fillVtbl(cd, &b->vtbl, 1); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
86 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
87 } |
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
|
88 |
100 | 89 gIR->structs.push_back(irstruct); |
90 gIR->classes.push_back(cd); | |
91 | |
92 // add vtable | |
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
|
93 ts->llvmVtblType = new llvm::PATypeHolder(llvm::OpaqueType::get()); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
94 const llvm::Type* vtabty = getPtrToType(ts->llvmVtblType->get()); |
100 | 95 |
96 std::vector<const llvm::Type*> fieldtypes; | |
97 fieldtypes.push_back(vtabty); | |
98 | |
115
5ba6d286c941
[svn r119] Added the monitor data field that comes after the vtable pointer to all classes. Represented as a void* initialized to zero.
lindquist
parents:
114
diff
changeset
|
99 // add monitor |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
100 fieldtypes.push_back(getPtrToType(llvm::Type::Int8Ty)); |
115
5ba6d286c941
[svn r119] Added the monitor data field that comes after the vtable pointer to all classes. Represented as a void* initialized to zero.
lindquist
parents:
114
diff
changeset
|
101 |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
102 // add interface vtables |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
103 if (cd->vtblInterfaces) |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
104 for (size_t i = 0; i < cd->vtblInterfaces->dim; i++) |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
105 { |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
106 BaseClass *b = (BaseClass *)cd->vtblInterfaces->data[i]; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
107 ClassDeclaration *id = b->base; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
108 assert(id->type->ty == Tclass); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
109 TypeClass* itc = (TypeClass*)id->type; |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
110 const llvm::Type* ivtblTy = getPtrToType(itc->llvmVtblType->get()); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
111 fieldtypes.push_back(ivtblTy); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
112 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
113 // add this interface to the map |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
114 IRInterface* iri = new IRInterface(b, isaStruct(itc->llvmVtblType->get())); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
115 irstruct->interfaces.insert(std::make_pair(id, iri)); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
116 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
117 |
100 | 118 // base classes first |
119 LLVM_AddBaseClassData(&cd->baseclasses); | |
120 | |
121 // then add own members | |
122 for (int k=0; k < cd->members->dim; k++) { | |
123 Dsymbol* dsym = (Dsymbol*)(cd->members->data[k]); | |
124 dsym->toObjFile(); | |
125 } | |
126 | |
132 | 127 // resolve class data fields (possibly unions) |
128 Logger::println("doing class fields"); | |
129 | |
130 if (irstruct->offsets.empty()) | |
131 { | |
132 Logger::println("has no fields"); | |
133 } | |
134 else | |
135 { | |
136 Logger::println("has fields"); | |
137 unsigned prevsize = (unsigned)-1; | |
138 unsigned lastoffset = (unsigned)-1; | |
139 const llvm::Type* fieldtype = NULL; | |
140 VarDeclaration* fieldinit = NULL; | |
141 size_t fieldpad = 0; | |
142 int idx = 0; | |
143 for (IRStruct::OffsetMap::iterator i=irstruct->offsets.begin(); i!=irstruct->offsets.end(); ++i) { | |
144 // first iteration | |
145 if (lastoffset == (unsigned)-1) { | |
146 lastoffset = i->first; | |
147 fieldtype = i->second.type; | |
148 fieldinit = i->second.var; | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
149 prevsize = getABITypeSize(fieldtype); |
132 | 150 i->second.var->llvmFieldIndex = idx; |
151 } | |
152 // colliding offset? | |
153 else if (lastoffset == i->first) { | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
154 size_t s = getABITypeSize(i->second.type); |
132 | 155 if (s > prevsize) { |
156 fieldpad += s - prevsize; | |
157 prevsize = s; | |
158 } | |
159 cd->llvmHasUnions = true; | |
160 i->second.var->llvmFieldIndex = idx; | |
161 } | |
162 // intersecting offset? | |
163 else if (i->first < (lastoffset + prevsize)) { | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
164 size_t s = getABITypeSize(i->second.type); |
132 | 165 assert((i->first + s) <= (lastoffset + prevsize)); // this holds because all types are aligned to their size |
166 cd->llvmHasUnions = true; | |
167 i->second.var->llvmFieldIndex = idx; | |
168 i->second.var->llvmFieldIndexOffset = (i->first - lastoffset) / s; | |
169 } | |
170 // fresh offset | |
171 else { | |
172 // commit the field | |
173 fieldtypes.push_back(fieldtype); | |
174 irstruct->defaultFields.push_back(fieldinit); | |
175 if (fieldpad) { | |
176 fieldtypes.push_back(llvm::ArrayType::get(llvm::Type::Int8Ty, fieldpad)); | |
177 irstruct->defaultFields.push_back(NULL); | |
178 idx++; | |
179 } | |
180 | |
181 idx++; | |
182 | |
183 // start new | |
184 lastoffset = i->first; | |
185 fieldtype = i->second.type; | |
186 fieldinit = i->second.var; | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
187 prevsize = getABITypeSize(fieldtype); |
132 | 188 i->second.var->llvmFieldIndex = idx; |
189 fieldpad = 0; | |
190 } | |
191 } | |
192 fieldtypes.push_back(fieldtype); | |
193 irstruct->defaultFields.push_back(fieldinit); | |
194 if (fieldpad) { | |
195 fieldtypes.push_back(llvm::ArrayType::get(llvm::Type::Int8Ty, fieldpad)); | |
196 irstruct->defaultFields.push_back(NULL); | |
197 } | |
198 } | |
199 | |
200 /* | |
100 | 201 // add field types |
202 for (IRStruct::OffsetMap::iterator i=irstruct->offsets.begin(); i!=irstruct->offsets.end(); ++i) { | |
203 fieldtypes.push_back(i->second.type); | |
204 } | |
132 | 205 */ |
100 | 206 |
207 const llvm::StructType* structtype = llvm::StructType::get(fieldtypes); | |
208 // refine abstract types for stuff like: class C {C next;} | |
209 assert(irstruct->recty != 0); | |
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
|
210 |
100 | 211 llvm::PATypeHolder& spa = irstruct->recty; |
212 llvm::cast<llvm::OpaqueType>(spa.get())->refineAbstractTypeTo(structtype); | |
213 structtype = isaStruct(spa.get()); | |
214 | |
103
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
215 if (!ts->llvmType) |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
216 ts->llvmType = new llvm::PATypeHolder(structtype); |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
217 else |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
218 *ts->llvmType = structtype; |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
219 spa = *ts->llvmType; |
100 | 220 |
123
7f9a0a58394b
[svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents:
121
diff
changeset
|
221 // name the type |
7f9a0a58394b
[svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents:
121
diff
changeset
|
222 gIR->module->addTypeName(cd->mangle(), ts->llvmType->get()); |
100 | 223 |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
224 // build interface info type |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
225 std::vector<const llvm::Type*> infoTypes; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
226 // ClassInfo classinfo |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
227 ClassDeclaration* cinfod = ClassDeclaration::classinfo; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
228 DtoResolveClass(cinfod); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
229 infoTypes.push_back(getPtrToType(cinfod->type->llvmType->get())); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
230 // void*[] vtbl |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
231 std::vector<const llvm::Type*> infoVtbltypes; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
232 infoVtbltypes.push_back(DtoSize_t()); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
233 const llvm::Type* byteptrptrty = getPtrToType(getPtrToType(llvm::Type::Int8Ty)); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
234 infoVtbltypes.push_back(byteptrptrty); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
235 infoTypes.push_back(llvm::StructType::get(infoVtbltypes)); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
236 // int offset |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
237 infoTypes.push_back(llvm::Type::Int32Ty); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
238 // create type |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
239 const llvm::StructType* infoTy = llvm::StructType::get(infoTypes); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
240 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
241 // create vtable type |
100 | 242 llvm::GlobalVariable* svtblVar = 0; |
243 std::vector<const llvm::Type*> sinits_ty; | |
244 | |
245 for (int k=0; k < cd->vtbl.dim; k++) | |
246 { | |
247 Dsymbol* dsym = (Dsymbol*)cd->vtbl.data[k]; | |
248 assert(dsym); | |
249 //Logger::cout() << "vtblsym: " << dsym->toChars() << '\n'; | |
250 | |
251 if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { | |
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
|
252 DtoResolveFunction(fd); |
117 | 253 //assert(fd->type->ty == Tfunction); |
254 //TypeFunction* tf = (TypeFunction*)fd->type; | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
255 //const llvm::Type* fpty = getPtrToType(tf->llvmType->get()); |
117 | 256 const llvm::FunctionType* vfty = DtoBaseFunctionType(fd); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
257 const llvm::Type* vfpty = getPtrToType(vfty); |
117 | 258 sinits_ty.push_back(vfpty); |
100 | 259 } |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
260 else if (ClassDeclaration* cd2 = dsym->isClassDeclaration()) { |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
261 Logger::println("*** ClassDeclaration in vtable: %s", cd2->toChars()); |
103
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
262 const llvm::Type* cinfoty; |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
263 if (cd->isInterfaceDeclaration()) { |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
264 cinfoty = infoTy; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
265 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
266 else if (cd != cinfod) { |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
267 DtoResolveClass(cinfod); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
268 cinfoty = cinfod->type->llvmType->get(); |
103
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
269 } |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
270 else { |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
271 // this is the ClassInfo class, the type is this type |
103
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
272 cinfoty = ts->llvmType->get(); |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
273 } |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
274 const llvm::Type* cty = getPtrToType(cinfoty); |
100 | 275 sinits_ty.push_back(cty); |
276 } | |
277 else | |
278 assert(0); | |
279 } | |
280 | |
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
|
281 assert(!sinits_ty.empty()); |
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
|
282 const llvm::StructType* svtbl_ty = llvm::StructType::get(sinits_ty); |
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
|
283 |
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
|
284 std::string styname(cd->mangle()); |
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
|
285 styname.append("__vtblType"); |
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
|
286 gIR->module->addTypeName(styname, svtbl_ty); |
100 | 287 |
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
|
288 // refine for final vtable type |
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
|
289 llvm::cast<llvm::OpaqueType>(ts->llvmVtblType->get())->refineAbstractTypeTo(svtbl_ty); |
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
|
290 |
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
|
291 gIR->classes.pop_back(); |
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
|
292 gIR->structs.pop_back(); |
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
|
293 |
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
|
294 gIR->declareList.push_back(cd); |
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
|
295 } |
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
|
296 |
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
|
297 ////////////////////////////////////////////////////////////////////////////////////////// |
100 | 298 |
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
|
299 void DtoDeclareClass(ClassDeclaration* cd) |
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
|
300 { |
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
|
301 if (cd->llvmDeclared) 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
|
302 cd->llvmDeclared = 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
|
303 |
132 | 304 Logger::println("DtoDeclareClass(%s): %s", cd->toPrettyChars(), cd->loc.toChars()); |
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
|
305 LOG_SCOPE; |
100 | 306 |
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
|
307 assert(cd->type->ty == Tclass); |
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
|
308 TypeClass* ts = (TypeClass*)cd->type; |
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
|
309 |
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
|
310 assert(cd->llvmIRStruct); |
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
|
311 IRStruct* irstruct = cd->llvmIRStruct; |
100 | 312 |
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
|
313 gIR->structs.push_back(irstruct); |
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
|
314 gIR->classes.push_back(cd); |
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
|
315 |
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
|
316 bool needs_definition = false; |
123
7f9a0a58394b
[svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents:
121
diff
changeset
|
317 if (cd->getModule() == gIR->dmodule) { |
7f9a0a58394b
[svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents:
121
diff
changeset
|
318 needs_definition = true; |
100 | 319 } |
320 | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
321 // interface vtables are emitted by the class implementing them |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
322 // also, interfaces have no static initializer |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
323 // also, abstract classes have no vtable |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
324 if (!cd->isInterfaceDeclaration() && !cd->isAbstract()) { |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
325 // vtable |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
326 std::string varname("_D"); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
327 varname.append(cd->mangle()); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
328 varname.append("6__vtblZ"); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
329 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
330 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::ExternalLinkage; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
331 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
332 const llvm::StructType* svtbl_ty = isaStruct(ts->llvmVtblType->get()); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
333 cd->llvmVtbl = new llvm::GlobalVariable(svtbl_ty, true, _linkage, 0, varname, gIR->module); |
100 | 334 |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
335 // build interface info type |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
336 std::vector<const llvm::Type*> types; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
337 // ClassInfo classinfo |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
338 ClassDeclaration* cd2 = ClassDeclaration::classinfo; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
339 DtoResolveClass(cd2); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
340 types.push_back(getPtrToType(cd2->type->llvmType->get())); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
341 // void*[] vtbl |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
342 std::vector<const llvm::Type*> vtbltypes; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
343 vtbltypes.push_back(DtoSize_t()); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
344 const llvm::Type* byteptrptrty = getPtrToType(getPtrToType(llvm::Type::Int8Ty)); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
345 vtbltypes.push_back(byteptrptrty); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
346 types.push_back(llvm::StructType::get(vtbltypes)); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
347 // int offset |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
348 types.push_back(llvm::Type::Int32Ty); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
349 // create type |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
350 const llvm::StructType* infoTy = llvm::StructType::get(types); |
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
|
351 |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
352 // interface info array |
132 | 353 if (cd->vtblInterfaces->dim > 0) { |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
354 // symbol name |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
355 std::string nam = "_D"; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
356 nam.append(cd->mangle()); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
357 nam.append("16__interfaceInfosZ"); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
358 // resolve array type |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
359 const llvm::ArrayType* arrTy = llvm::ArrayType::get(infoTy, cd->vtblInterfaces->dim); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
360 // declare global |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
361 irstruct->interfaceInfosTy = arrTy; |
132 | 362 irstruct->interfaceInfos = new llvm::GlobalVariable(arrTy, true, _linkage, NULL, nam, gIR->module); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
363 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
364 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
365 // interface vtables |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
366 unsigned idx = 0; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
367 for (IRStruct::InterfaceIter i=irstruct->interfaces.begin(); i!=irstruct->interfaces.end(); ++i) |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
368 { |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
369 ClassDeclaration* id = i->first; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
370 IRInterface* iri = i->second; |
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
|
371 |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
372 std::string nam("_D"); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
373 nam.append(cd->mangle()); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
374 nam.append("11__interface"); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
375 nam.append(id->mangle()); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
376 nam.append("6__vtblZ"); |
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
|
377 |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
378 assert(iri->vtblTy); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
379 iri->vtbl = new llvm::GlobalVariable(iri->vtblTy, true, _linkage, 0, nam, gIR->module); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
380 iri->infoTy = infoTy; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
381 llvm::Constant* idxs[2] = {DtoConstUint(0), DtoConstUint(idx)}; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
382 iri->info = llvm::ConstantExpr::getGetElementPtr(irstruct->interfaceInfos, idxs, 2); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
383 idx++; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
384 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
385 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
386 // init |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
387 std::string initname("_D"); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
388 initname.append(cd->mangle()); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
389 initname.append("6__initZ"); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
390 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
391 llvm::GlobalVariable* initvar = new llvm::GlobalVariable(ts->llvmType->get(), true, _linkage, NULL, initname, gIR->module); |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
392 cd->llvmInit = initvar; |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
393 } |
100 | 394 |
395 gIR->classes.pop_back(); | |
396 gIR->structs.pop_back(); | |
397 | |
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
|
398 gIR->constInitList.push_back(cd); |
100 | 399 if (needs_definition) |
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
|
400 gIR->defineList.push_back(cd); |
103
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
401 |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
402 // classinfo |
855adfdb8d38
[svn r107] Getting .classinfo on a class instance now works (classinfo is stored in vtable)
lindquist
parents:
102
diff
changeset
|
403 DtoDeclareClassInfo(cd); |
106 | 404 |
405 // typeinfo | |
123
7f9a0a58394b
[svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents:
121
diff
changeset
|
406 if (needs_definition) |
106 | 407 cd->type->getTypeInfo(NULL); |
100 | 408 } |
409 | |
410 ////////////////////////////////////////////////////////////////////////////////////////// | |
411 | |
412 void DtoConstInitClass(ClassDeclaration* cd) | |
413 { | |
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
|
414 if (cd->llvmInitialized) 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
|
415 cd->llvmInitialized = true; |
100 | 416 |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
417 if (cd->isInterfaceDeclaration()) |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
418 return; // nothing to do |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
419 |
132 | 420 Logger::println("DtoConstInitClass(%s): %s", cd->toPrettyChars(), cd->loc.toChars()); |
100 | 421 LOG_SCOPE; |
422 | |
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
|
423 IRStruct* irstruct = cd->llvmIRStruct; |
100 | 424 gIR->structs.push_back(irstruct); |
425 gIR->classes.push_back(cd); | |
426 | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
427 // get the struct (class) type |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
428 assert(cd->type->ty == Tclass); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
429 TypeClass* ts = (TypeClass*)cd->type; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
430 const llvm::StructType* structtype = isaStruct(ts->llvmType->get()); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
431 const llvm::StructType* vtbltype = isaStruct(ts->llvmVtblType->get()); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
432 |
100 | 433 // make sure each offset knows its default initializer |
434 for (IRStruct::OffsetMap::iterator i=irstruct->offsets.begin(); i!=irstruct->offsets.end(); ++i) | |
435 { | |
436 IRStruct::Offset* so = &i->second; | |
437 llvm::Constant* finit = DtoConstFieldInitializer(so->var->type, so->var->init); | |
438 so->init = finit; | |
439 so->var->llvmConstInit = finit; | |
440 } | |
441 | |
442 // fill out fieldtypes/inits | |
443 std::vector<llvm::Constant*> fieldinits; | |
444 | |
445 // first field is always the vtable | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
446 if (cd->isAbstract()) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
447 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
448 fieldinits.push_back( |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
449 llvm::ConstantPointerNull::get( |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
450 getPtrToType( |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
451 ts->llvmVtblType->get() |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
452 ) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
453 ) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
454 ); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
455 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
456 else |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
457 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
458 assert(cd->llvmVtbl != 0); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
459 fieldinits.push_back(cd->llvmVtbl); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
460 } |
100 | 461 |
115
5ba6d286c941
[svn r119] Added the monitor data field that comes after the vtable pointer to all classes. Represented as a void* initialized to zero.
lindquist
parents:
114
diff
changeset
|
462 // then comes monitor |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
463 fieldinits.push_back(llvm::ConstantPointerNull::get(getPtrToType(llvm::Type::Int8Ty))); |
115
5ba6d286c941
[svn r119] Added the monitor data field that comes after the vtable pointer to all classes. Represented as a void* initialized to zero.
lindquist
parents:
114
diff
changeset
|
464 |
132 | 465 size_t dataoffset = 2; |
466 | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
467 // next comes interface vtables |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
468 for (IRStruct::InterfaceIter i=irstruct->interfaces.begin(); i!=irstruct->interfaces.end(); ++i) |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
469 { |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
470 IRInterface* iri = i->second; |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
471 if (cd->isAbstract()) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
472 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
473 fieldinits.push_back(llvm::Constant::getNullValue(iri->vtblTy)); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
474 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
475 else |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
476 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
477 assert(iri->vtbl); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
478 fieldinits.push_back(iri->vtbl); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
479 ++dataoffset; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
480 } |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
481 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
482 |
132 | 483 /* |
100 | 484 // rest |
485 for (IRStruct::OffsetMap::iterator i=irstruct->offsets.begin(); i!=irstruct->offsets.end(); ++i) { | |
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
|
486 Logger::println("adding fieldinit for: %s", i->second.var->toChars()); |
100 | 487 fieldinits.push_back(i->second.init); |
488 } | |
132 | 489 */ |
100 | 490 |
132 | 491 // go through the field inits and build the default initializer |
492 size_t nfi = irstruct->defaultFields.size(); | |
493 for (size_t i=0; i<nfi; ++i) { | |
494 llvm::Constant* c; | |
495 if (irstruct->defaultFields[i] != NULL) { | |
496 c = irstruct->defaultFields[i]->llvmConstInit; | |
497 assert(c); | |
498 } | |
499 else { | |
500 const llvm::ArrayType* arrty = isaArray(structtype->getElementType(i+dataoffset)); | |
501 std::vector<llvm::Constant*> vals(arrty->getNumElements(), llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false)); | |
502 c = llvm::ConstantArray::get(arrty, vals); | |
503 } | |
504 fieldinits.push_back(c); | |
505 } | |
506 | |
100 | 507 // generate initializer |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
508 #if 0 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
509 Logger::cout() << cd->toPrettyChars() << " | " << *structtype << '\n'; |
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
|
510 |
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
|
511 for(size_t i=0; i<structtype->getNumElements(); ++i) { |
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
|
512 Logger::cout() << "s#" << i << " = " << *structtype->getElementType(i) << '\n'; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
513 } |
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
|
514 |
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
|
515 for(size_t i=0; i<fieldinits.size(); ++i) { |
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
|
516 Logger::cout() << "i#" << i << " = " << *fieldinits[i]->getType() << '\n'; |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
517 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
518 #endif |
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
|
519 |
100 | 520 llvm::Constant* _init = llvm::ConstantStruct::get(structtype, fieldinits); |
521 assert(_init); | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
522 cd->llvmConstInit = _init; |
100 | 523 |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
524 // abstract classes have no static vtable |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
525 if (!cd->isAbstract()) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
526 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
527 // generate vtable initializer |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
528 std::vector<llvm::Constant*> sinits; |
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
|
529 |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
530 for (int k=0; k < cd->vtbl.dim; k++) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
531 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
532 Dsymbol* dsym = (Dsymbol*)cd->vtbl.data[k]; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
533 assert(dsym); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
534 //Logger::cout() << "vtblsym: " << dsym->toChars() << '\n'; |
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
|
535 |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
536 if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
537 DtoForceDeclareDsymbol(fd); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
538 assert(fd->llvmValue); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
539 llvm::Constant* c = llvm::cast<llvm::Constant>(fd->llvmValue); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
540 // cast if necessary (overridden method) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
541 if (c->getType() != vtbltype->getElementType(k)) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
542 c = llvm::ConstantExpr::getBitCast(c, vtbltype->getElementType(k)); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
543 sinits.push_back(c); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
544 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
545 else if (ClassDeclaration* cd2 = dsym->isClassDeclaration()) { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
546 assert(cd->llvmClass); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
547 llvm::Constant* c = cd->llvmClass; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
548 sinits.push_back(c); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
549 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
550 else |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
551 assert(0); |
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
|
552 } |
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
|
553 |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
554 const llvm::StructType* svtbl_ty = isaStruct(ts->llvmVtblType->get()); |
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
|
555 |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
556 #if 0 |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
557 for (size_t i=0; i< sinits.size(); ++i) |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
558 { |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
559 Logger::cout() << "field[" << i << "] = " << *svtbl_ty->getElementType(i) << '\n'; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
560 Logger::cout() << "init [" << i << "] = " << *sinits[i]->getType() << '\n'; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
561 assert(svtbl_ty->getElementType(i) == sinits[i]->getType()); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
562 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
563 #endif |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
564 |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
565 llvm::Constant* cvtblInit = llvm::ConstantStruct::get(svtbl_ty, sinits); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
566 cd->llvmConstVtbl = llvm::cast<llvm::ConstantStruct>(cvtblInit); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
567 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
568 // create interface vtable const initalizers |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
569 int idx = 2; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
570 int idxScale = PTRSIZE; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
571 for (IRStruct::InterfaceIter i=irstruct->interfaces.begin(); i!=irstruct->interfaces.end(); ++i) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
572 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
573 ClassDeclaration* id = i->first; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
574 assert(id->type->ty == Tclass); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
575 TypeClass* its = (TypeClass*)id->type; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
576 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
577 IRInterface* iri = i->second; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
578 BaseClass* b = iri->base; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
579 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
580 const llvm::StructType* ivtbl_ty = isaStruct(its->llvmVtblType->get()); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
581 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
582 // generate interface info initializer |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
583 std::vector<llvm::Constant*> infoInits; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
584 // classinfo |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
585 assert(id->llvmClass); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
586 llvm::Constant* c = id->llvmClass; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
587 infoInits.push_back(c); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
588 // vtbl |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
589 const llvm::Type* byteptrptrty = getPtrToType(getPtrToType(llvm::Type::Int8Ty)); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
590 c = llvm::ConstantExpr::getBitCast(iri->vtbl, byteptrptrty); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
591 c = DtoConstSlice(DtoConstSize_t(b->vtbl.dim), c); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
592 infoInits.push_back(c); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
593 // offset |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
594 infoInits.push_back(DtoConstInt(idx*idxScale)); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
595 // create interface info initializer constant |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
596 iri->infoInit = llvm::cast<llvm::ConstantStruct>(llvm::ConstantStruct::get(iri->infoTy, infoInits)); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
597 |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
598 // generate vtable initializer |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
599 std::vector<llvm::Constant*> iinits; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
600 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
601 // add interface info |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
602 iinits.push_back(iri->info); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
603 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
604 for (int k=1; k < b->vtbl.dim; k++) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
605 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
606 Logger::println("interface vtbl const init nr. %d", k); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
607 Dsymbol* dsym = (Dsymbol*)b->vtbl.data[k]; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
608 FuncDeclaration* fd = dsym->isFuncDeclaration(); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
609 assert(fd); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
610 DtoForceDeclareDsymbol(fd); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
611 assert(fd->llvmValue); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
612 llvm::Constant* c = llvm::cast<llvm::Constant>(fd->llvmValue); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
613 // we have to bitcast, as the type created in ResolveClass expects a different this type |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
614 c = llvm::ConstantExpr::getBitCast(c, iri->vtblTy->getContainedType(k)); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
615 iinits.push_back(c); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
616 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
617 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
618 #if 1 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
619 for (size_t x=0; x< iinits.size(); ++x) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
620 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
621 Logger::cout() << "field[" << x << "] = " << *ivtbl_ty->getElementType(x) << "\n\n"; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
622 Logger::cout() << "init [" << x << "] = " << *iinits[x] << "\n\n"; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
623 assert(ivtbl_ty->getElementType(x) == iinits[x]->getType()); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
624 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
625 #endif |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
626 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
627 llvm::Constant* civtblInit = llvm::ConstantStruct::get(ivtbl_ty, iinits); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
628 iri->vtblInit = llvm::cast<llvm::ConstantStruct>(civtblInit); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
629 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
630 idx++; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
631 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
632 } // !abstract |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
633 |
100 | 634 gIR->classes.pop_back(); |
635 gIR->structs.pop_back(); | |
636 } | |
637 | |
638 ////////////////////////////////////////////////////////////////////////////////////////// | |
639 | |
640 void DtoDefineClass(ClassDeclaration* cd) | |
641 { | |
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
|
642 if (cd->llvmDefined) 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
|
643 cd->llvmDefined = true; |
100 | 644 |
132 | 645 Logger::println("DtoDefineClass(%s): %s", cd->toPrettyChars(), cd->loc.toChars()); |
100 | 646 LOG_SCOPE; |
647 | |
648 // get the struct (class) type | |
649 assert(cd->type->ty == Tclass); | |
650 TypeClass* ts = (TypeClass*)cd->type; | |
651 | |
123
7f9a0a58394b
[svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents:
121
diff
changeset
|
652 if (cd->getModule() == gIR->dmodule) { |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
653 // interfaces don't have initializers |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
654 // neither do abstract classes |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
655 if (!cd->isInterfaceDeclaration() && !cd->isAbstract()) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
656 { |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
657 cd->llvmInit->setInitializer(cd->llvmConstInit); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
658 cd->llvmVtbl->setInitializer(cd->llvmConstVtbl); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
659 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
660 // initialize interface vtables |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
661 IRStruct* irstruct = cd->llvmIRStruct; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
662 std::vector<llvm::Constant*> infoInits; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
663 for (IRStruct::InterfaceIter i=irstruct->interfaces.begin(); i!=irstruct->interfaces.end(); ++i) |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
664 { |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
665 IRInterface* iri = i->second; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
666 iri->vtbl->setInitializer(iri->vtblInit); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
667 infoInits.push_back(iri->infoInit); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
668 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
669 // initialize interface info array |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
670 if (!infoInits.empty()) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
671 { |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
672 llvm::Constant* arrInit = llvm::ConstantArray::get(irstruct->interfaceInfosTy, infoInits); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
673 irstruct->interfaceInfos->setInitializer(arrInit); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
674 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
675 } |
123
7f9a0a58394b
[svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents:
121
diff
changeset
|
676 |
7f9a0a58394b
[svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents:
121
diff
changeset
|
677 // generate classinfo |
7f9a0a58394b
[svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents:
121
diff
changeset
|
678 DtoDefineClassInfo(cd); |
100 | 679 } |
680 } | |
681 | |
682 ////////////////////////////////////////////////////////////////////////////////////////// | |
683 | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
684 DValue* DtoNewClass(TypeClass* tc, NewExp* newexp) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
685 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
686 // resolve type |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
687 DtoForceDeclareDsymbol(tc->sym); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
688 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
689 // allocate |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
690 llvm::Value* mem; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
691 if (newexp->onstack) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
692 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
693 mem = new llvm::AllocaInst(DtoType(tc)->getContainedType(0), "newclass_alloca", gIR->topallocapoint()); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
694 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
695 else |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
696 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
697 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_newclass"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
698 std::vector<llvm::Value*> args; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
699 args.push_back(tc->sym->llvmClass); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
700 mem = gIR->ir->CreateCall(fn, args.begin(), args.end(), "newclass_gc_alloc"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
701 mem = DtoBitCast(mem, DtoType(tc), "newclass_gc"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
702 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
703 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
704 // init |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
705 DtoInitClass(tc, mem); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
706 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
707 // init inner-class outer reference |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
708 if (newexp->thisexp) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
709 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
710 Logger::println("Resolving outer class"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
711 LOG_SCOPE; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
712 DValue* thisval = newexp->thisexp->toElem(gIR); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
713 size_t idx = 2; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
714 idx += tc->sym->llvmIRStruct->interfaces.size(); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
715 llvm::Value* dst = thisval->getRVal(); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
716 llvm::Value* src = DtoGEPi(mem,0,idx,"tmp"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
717 Logger::cout() << "dst: " << *dst << "\nsrc: " << *src << '\n'; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
718 DtoStore(dst, src); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
719 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
720 // set the context for nested classes |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
721 else if (tc->sym->isNested()) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
722 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
723 Logger::println("Resolving nested context"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
724 LOG_SCOPE; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
725 size_t idx = 2; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
726 idx += tc->sym->llvmIRStruct->interfaces.size(); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
727 llvm::Value* nest = gIR->func()->decl->llvmNested; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
728 if (!nest) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
729 nest = gIR->func()->decl->llvmThisVar; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
730 assert(nest); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
731 llvm::Value* gep = DtoGEPi(mem,0,idx,"tmp"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
732 nest = DtoBitCast(nest, gep->getType()->getContainedType(0)); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
733 DtoStore(nest, gep); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
734 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
735 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
736 // call constructor |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
737 if (newexp->arguments) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
738 return DtoCallClassCtor(tc, newexp->member, newexp->arguments, mem); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
739 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
740 // return default constructed class |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
741 return new DImValue(tc, mem, false); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
742 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
743 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
744 ////////////////////////////////////////////////////////////////////////////////////////// |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
745 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
746 void DtoInitClass(TypeClass* tc, llvm::Value* dst) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
747 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
748 size_t presz = 2*getABITypeSize(DtoSize_t()); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
749 uint64_t n = getABITypeSize(tc->llvmType->get()) - presz; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
750 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
751 // set vtable field seperately, this might give better optimization |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
752 assert(tc->sym->llvmVtbl); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
753 DtoStore(tc->sym->llvmVtbl, DtoGEPi(dst,0,0,"vtbl")); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
754 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
755 // monitor always defaults to zero |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
756 llvm::Value* tmp = DtoGEPi(dst,0,1,"monitor"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
757 DtoStore(llvm::Constant::getNullValue(tmp->getType()->getContainedType(0)), tmp); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
758 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
759 // done? |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
760 if (n == 0) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
761 return; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
762 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
763 // copy the rest from the static initializer |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
764 assert(tc->sym->llvmInit); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
765 assert(dst->getType() == tc->sym->llvmInit->getType()); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
766 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
767 const llvm::Type* arrty = getPtrToType(llvm::Type::Int8Ty); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
768 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
769 llvm::Value* dstarr = DtoGEPi(dst,0,2,"tmp"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
770 dstarr = DtoBitCast(dstarr, arrty); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
771 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
772 llvm::Value* srcarr = DtoGEPi(tc->sym->llvmInit,0,2,"tmp"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
773 srcarr = DtoBitCast(srcarr, arrty); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
774 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
775 llvm::Function* fn = LLVM_DeclareMemCpy32(); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
776 std::vector<llvm::Value*> llargs; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
777 llargs.resize(4); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
778 llargs[0] = dstarr; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
779 llargs[1] = srcarr; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
780 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
781 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
782 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
783 new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
784 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
785 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
786 ////////////////////////////////////////////////////////////////////////////////////////// |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
787 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
788 DValue* DtoCallClassCtor(TypeClass* type, CtorDeclaration* ctor, Array* arguments, llvm::Value* mem) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
789 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
790 Logger::println("Calling constructor"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
791 LOG_SCOPE; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
792 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
793 assert(ctor); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
794 DtoForceDeclareDsymbol(ctor); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
795 llvm::Function* fn = llvm::cast<llvm::Function>(ctor->llvmValue); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
796 TypeFunction* tf = (TypeFunction*)DtoDType(ctor->type); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
797 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
798 std::vector<llvm::Value*> ctorargs; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
799 ctorargs.push_back(mem); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
800 for (size_t i=0; i<arguments->dim; ++i) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
801 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
802 Expression* ex = (Expression*)arguments->data[i]; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
803 Argument* fnarg = Argument::getNth(tf->parameters, i); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
804 DValue* argval = DtoArgument(fnarg, ex); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
805 llvm::Value* a = argval->getRVal(); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
806 const llvm::Type* aty = fn->getFunctionType()->getParamType(i+1); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
807 if (a->getType() != aty) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
808 a = DtoBitCast(a, aty); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
809 ctorargs.push_back(a); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
810 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
811 llvm::CallInst* call = new llvm::CallInst(fn, ctorargs.begin(), ctorargs.end(), "tmp", gIR->scopebb()); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
812 call->setCallingConv(DtoCallingConv(LINKd)); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
813 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
814 return new DImValue(type, call, false); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
815 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
816 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
817 ////////////////////////////////////////////////////////////////////////////////////////// |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
818 |
100 | 819 void DtoCallClassDtors(TypeClass* tc, llvm::Value* instance) |
820 { | |
821 Array* arr = &tc->sym->dtors; | |
822 for (size_t i=0; i<arr->dim; i++) | |
823 { | |
824 FuncDeclaration* fd = (FuncDeclaration*)arr->data[i]; | |
825 assert(fd->llvmValue); | |
826 new llvm::CallInst(fd->llvmValue, instance, "", gIR->scopebb()); | |
827 } | |
828 } | |
829 | |
830 ////////////////////////////////////////////////////////////////////////////////////////// | |
831 | |
114 | 832 DValue* DtoCastClass(DValue* val, Type* _to) |
833 { | |
834 Type* to = DtoDType(_to); | |
835 if (to->ty == Tpointer) { | |
836 const llvm::Type* tolltype = DtoType(_to); | |
837 llvm::Value* rval = DtoBitCast(val->getRVal(), tolltype); | |
838 return new DImValue(_to, rval); | |
839 } | |
840 | |
841 assert(to->ty == Tclass); | |
842 TypeClass* tc = (TypeClass*)to; | |
843 | |
844 Type* from = DtoDType(val->getType()); | |
845 TypeClass* fc = (TypeClass*)from; | |
846 | |
847 if (tc->sym->isInterfaceDeclaration()) { | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
848 if (fc->sym->isInterfaceDeclaration()) { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
849 return DtoDynamicCastInterface(val, _to); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
850 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
851 else { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
852 return DtoDynamicCastObject(val, _to); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
853 } |
114 | 854 } |
855 else { | |
856 int poffset; | |
857 if (fc->sym->isInterfaceDeclaration()) { | |
858 return DtoCastInterfaceToObject(val, _to); | |
859 } | |
860 else if (tc->sym->isBaseOf(fc->sym,NULL)) { | |
861 const llvm::Type* tolltype = DtoType(_to); | |
862 llvm::Value* rval = DtoBitCast(val->getRVal(), tolltype); | |
863 return new DImValue(_to, rval); | |
864 } | |
865 else { | |
866 return DtoDynamicCastObject(val, _to); | |
867 } | |
868 } | |
869 } | |
870 | |
871 ////////////////////////////////////////////////////////////////////////////////////////// | |
872 | |
873 DValue* DtoDynamicCastObject(DValue* val, Type* _to) | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
874 { |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
875 // call: |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
876 // Object _d_dynamic_cast(Object o, ClassInfo c) |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
877 |
114 | 878 DtoForceDeclareDsymbol(ClassDeclaration::object); |
879 DtoForceDeclareDsymbol(ClassDeclaration::classinfo); | |
880 | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
881 llvm::Function* func = LLVM_D_GetRuntimeFunction(gIR->module, "_d_dynamic_cast"); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
882 const llvm::FunctionType* funcTy = func->getFunctionType(); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
883 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
884 std::vector<llvm::Value*> args; |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
885 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
886 // Object o |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
887 llvm::Value* tmp = val->getRVal(); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
888 tmp = DtoBitCast(tmp, funcTy->getParamType(0)); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
889 args.push_back(tmp); |
114 | 890 assert(funcTy->getParamType(0) == tmp->getType()); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
891 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
892 // ClassInfo c |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
893 TypeClass* to = (TypeClass*)DtoDType(_to); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
894 DtoForceDeclareDsymbol(to->sym); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
895 assert(to->sym->llvmClass); |
114 | 896 tmp = to->sym->llvmClass; |
897 // unfortunately this is needed as the implementation of object differs somehow from the declaration | |
898 // this could happen in user code as well :/ | |
899 tmp = DtoBitCast(tmp, funcTy->getParamType(1)); | |
900 args.push_back(tmp); | |
901 assert(funcTy->getParamType(1) == tmp->getType()); | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
902 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
903 // call it |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
904 llvm::Value* ret = gIR->ir->CreateCall(func, args.begin(), args.end(), "tmp"); |
114 | 905 |
906 // cast return value | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
907 ret = DtoBitCast(ret, DtoType(_to)); |
114 | 908 |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
909 return new DImValue(_to, ret); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
910 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
911 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
912 ////////////////////////////////////////////////////////////////////////////////////////// |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
913 |
114 | 914 DValue* DtoCastInterfaceToObject(DValue* val, Type* to) |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
915 { |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
916 // call: |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
917 // Object _d_toObject(void* p) |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
918 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
919 llvm::Function* func = LLVM_D_GetRuntimeFunction(gIR->module, "_d_toObject"); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
920 const llvm::FunctionType* funcTy = func->getFunctionType(); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
921 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
922 // void* p |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
923 llvm::Value* tmp = val->getRVal(); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
924 tmp = DtoBitCast(tmp, funcTy->getParamType(0)); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
925 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
926 // call it |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
927 llvm::Value* ret = gIR->ir->CreateCall(func, tmp, "tmp"); |
114 | 928 |
929 // cast return value | |
930 if (to != NULL) | |
931 ret = DtoBitCast(ret, DtoType(to)); | |
932 else | |
933 to = ClassDeclaration::object->type; | |
934 | |
935 return new DImValue(to, ret); | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
936 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
937 |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
938 ////////////////////////////////////////////////////////////////////////////////////////// |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
939 |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
940 DValue* DtoDynamicCastInterface(DValue* val, Type* _to) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
941 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
942 // call: |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
943 // Object _d_interface_cast(void* p, ClassInfo c) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
944 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
945 DtoForceDeclareDsymbol(ClassDeclaration::object); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
946 DtoForceDeclareDsymbol(ClassDeclaration::classinfo); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
947 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
948 llvm::Function* func = LLVM_D_GetRuntimeFunction(gIR->module, "_d_interface_cast"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
949 const llvm::FunctionType* funcTy = func->getFunctionType(); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
950 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
951 std::vector<llvm::Value*> args; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
952 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
953 // void* p |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
954 llvm::Value* tmp = val->getRVal(); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
955 tmp = DtoBitCast(tmp, funcTy->getParamType(0)); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
956 args.push_back(tmp); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
957 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
958 // ClassInfo c |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
959 TypeClass* to = (TypeClass*)DtoDType(_to); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
960 DtoForceDeclareDsymbol(to->sym); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
961 assert(to->sym->llvmClass); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
962 tmp = to->sym->llvmClass; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
963 // unfortunately this is needed as the implementation of object differs somehow from the declaration |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
964 // this could happen in user code as well :/ |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
965 tmp = DtoBitCast(tmp, funcTy->getParamType(1)); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
966 args.push_back(tmp); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
967 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
968 // call it |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
969 llvm::Value* ret = gIR->ir->CreateCall(func, args.begin(), args.end(), "tmp"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
970 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
971 // cast return value |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
972 ret = DtoBitCast(ret, DtoType(_to)); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
973 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
974 return new DImValue(_to, ret); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
975 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
976 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
977 ////////////////////////////////////////////////////////////////////////////////////////// |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
978 |
132 | 979 static unsigned LLVM_ClassOffsetToIndex(ClassDeclaration* cd, unsigned os, unsigned& idx) |
980 { | |
981 // start at the bottom of the inheritance chain | |
982 if (cd->baseClass != 0) { | |
983 unsigned o = LLVM_ClassOffsetToIndex(cd->baseClass, os, idx); | |
984 if (o != (unsigned)-1) | |
985 return o; | |
986 } | |
987 | |
988 // check this class | |
989 unsigned i; | |
990 for (i=0; i<cd->fields.dim; ++i) { | |
991 VarDeclaration* vd = (VarDeclaration*)cd->fields.data[i]; | |
992 if (os == vd->offset) | |
993 return i+idx; | |
994 } | |
995 idx += i; | |
996 | |
997 return (unsigned)-1; | |
998 } | |
999 | |
1000 ////////////////////////////////////////////////////////////////////////////////////////// | |
1001 | |
1002 void ClassDeclaration::offsetToIndex(Type* t, unsigned os, std::vector<unsigned>& result) | |
1003 { | |
1004 unsigned idx = 0; | |
1005 unsigned r = LLVM_ClassOffsetToIndex(this, os, idx); | |
1006 assert(r != (unsigned)-1 && "Offset not found in any aggregate field"); | |
1007 // vtable is 0, monitor is 1 | |
1008 r += 2; | |
1009 // interface offset further | |
1010 r += vtblInterfaces->dim; | |
1011 // the final index was not pushed | |
1012 result.push_back(r); | |
1013 } | |
1014 | |
1015 ////////////////////////////////////////////////////////////////////////////////////////// | |
1016 | |
1017 llvm::Value* DtoIndexClass(llvm::Value* ptr, ClassDeclaration* cd, Type* t, unsigned os, std::vector<unsigned>& idxs) | |
1018 { | |
1019 Logger::println("checking for offset %u type %s:", os, t->toChars()); | |
1020 LOG_SCOPE; | |
1021 | |
1022 if (idxs.empty()) | |
1023 idxs.push_back(0); | |
1024 | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1025 const llvm::Type* llt = getPtrToType(DtoType(t)); |
132 | 1026 const llvm::Type* st = DtoType(cd->type); |
1027 if (ptr->getType() != st) { | |
1028 assert(cd->llvmHasUnions); | |
1029 ptr = gIR->ir->CreateBitCast(ptr, st, "tmp"); | |
1030 } | |
1031 | |
1032 unsigned dataoffset = 2 + cd->vtblInterfaces->dim; | |
1033 | |
1034 IRStruct* irstruct = cd->llvmIRStruct; | |
1035 for (IRStruct::OffsetMap::iterator i=irstruct->offsets.begin(); i!=irstruct->offsets.end(); ++i) { | |
1036 //for (unsigned i=0; i<cd->fields.dim; ++i) { | |
1037 //VarDeclaration* vd = (VarDeclaration*)cd->fields.data[i]; | |
1038 VarDeclaration* vd = i->second.var; | |
1039 assert(vd); | |
1040 Type* vdtype = DtoDType(vd->type); | |
1041 Logger::println("found %u type %s", vd->offset, vdtype->toChars()); | |
1042 assert(vd->llvmFieldIndex >= 0); | |
1043 if (os == vd->offset && vdtype == t) { | |
1044 idxs.push_back(vd->llvmFieldIndex + dataoffset); | |
1045 Logger::cout() << "indexing: " << *ptr << '\n'; | |
1046 ptr = DtoGEP(ptr, idxs, "tmp"); | |
1047 if (ptr->getType() != llt) | |
1048 ptr = gIR->ir->CreateBitCast(ptr, llt, "tmp"); | |
1049 Logger::cout() << "indexing: " << *ptr << '\n'; | |
1050 if (vd->llvmFieldIndexOffset) | |
1051 ptr = new llvm::GetElementPtrInst(ptr, DtoConstUint(vd->llvmFieldIndexOffset), "tmp", gIR->scopebb()); | |
1052 Logger::cout() << "indexing: " << *ptr << '\n'; | |
1053 return ptr; | |
1054 } | |
1055 else if (vdtype->ty == Tstruct && (vd->offset + vdtype->size()) > os) { | |
1056 TypeStruct* ts = (TypeStruct*)vdtype; | |
1057 StructDeclaration* ssd = ts->sym; | |
1058 idxs.push_back(vd->llvmFieldIndex + dataoffset); | |
1059 if (vd->llvmFieldIndexOffset) { | |
1060 Logger::println("has union field offset"); | |
1061 ptr = DtoGEP(ptr, idxs, "tmp"); | |
1062 if (ptr->getType() != llt) | |
1063 ptr = gIR->ir->CreateBitCast(ptr, llt, "tmp"); | |
1064 ptr = new llvm::GetElementPtrInst(ptr, DtoConstUint(vd->llvmFieldIndexOffset), "tmp", gIR->scopebb()); | |
1065 std::vector<unsigned> tmp; | |
1066 return DtoIndexStruct(ptr, ssd, t, os-vd->offset, tmp); | |
1067 } | |
1068 else { | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1069 const llvm::Type* sty = getPtrToType(DtoType(vd->type)); |
132 | 1070 if (ptr->getType() != sty) { |
1071 ptr = gIR->ir->CreateBitCast(ptr, sty, "tmp"); | |
1072 std::vector<unsigned> tmp; | |
1073 return DtoIndexStruct(ptr, ssd, t, os-vd->offset, tmp); | |
1074 } | |
1075 else { | |
1076 return DtoIndexStruct(ptr, ssd, t, os-vd->offset, idxs); | |
1077 } | |
1078 } | |
1079 } | |
1080 } | |
1081 | |
1082 assert(0); | |
1083 | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1084 size_t llt_sz = getABITypeSize(llt->getContainedType(0)); |
132 | 1085 assert(os % llt_sz == 0); |
1086 ptr = gIR->ir->CreateBitCast(ptr, llt, "tmp"); | |
1087 return new llvm::GetElementPtrInst(ptr, DtoConstUint(os / llt_sz), "tmp", gIR->scopebb()); | |
1088 } | |
1089 | |
1090 ////////////////////////////////////////////////////////////////////////////////////////// | |
1091 | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1092 llvm::Value* DtoVirtualFunctionPointer(DValue* inst, FuncDeclaration* fdecl) |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1093 { |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1094 assert(fdecl->isVirtual());//fdecl->isAbstract() || (!fdecl->isFinal() && fdecl->isVirtual())); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1095 assert(fdecl->vtblIndex > 0); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1096 assert(DtoDType(inst->getType())->ty == Tclass); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1097 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1098 llvm::Value* vthis = inst->getRVal(); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1099 //Logger::cout() << "vthis: " << *vthis << '\n'; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1100 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1101 llvm::Value* funcval; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1102 funcval = DtoGEPi(vthis, 0, 0, "tmp"); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1103 funcval = DtoLoad(funcval); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1104 funcval = DtoGEPi(funcval, 0, fdecl->vtblIndex, fdecl->toPrettyChars()); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1105 funcval = DtoLoad(funcval); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1106 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1107 //assert(funcval->getType() == DtoType(fdecl->type)); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1108 //cc = DtoCallingConv(fdecl->linkage); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1109 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1110 return funcval; |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1111 } |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1112 |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1113 ////////////////////////////////////////////////////////////////////////////////////////// |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1114 |
100 | 1115 void DtoDeclareClassInfo(ClassDeclaration* cd) |
1116 { | |
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
|
1117 if (cd->llvmClassDeclared) 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
|
1118 cd->llvmClassDeclared = true; |
100 | 1119 |
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
|
1120 Logger::println("DtoDeclareClassInfo(%s)", cd->toChars()); |
100 | 1121 LOG_SCOPE; |
1122 | |
1123 ClassDeclaration* cinfo = ClassDeclaration::classinfo; | |
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
|
1124 DtoResolveClass(cinfo); |
100 | 1125 |
1126 std::string gname("_D"); | |
1127 gname.append(cd->mangle()); | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1128 if (!cd->isInterfaceDeclaration()) |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1129 gname.append("7__ClassZ"); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1130 else |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1131 gname.append("11__InterfaceZ"); |
100 | 1132 |
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
|
1133 const llvm::Type* st = cinfo->type->llvmType->get(); |
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
|
1134 |
100 | 1135 cd->llvmClass = new llvm::GlobalVariable(st, true, llvm::GlobalValue::ExternalLinkage, NULL, gname, gIR->module); |
1136 } | |
1137 | |
110
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1138 static llvm::Constant* build_offti_entry(VarDeclaration* vd) |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1139 { |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1140 std::vector<const llvm::Type*> types; |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1141 std::vector<llvm::Constant*> inits; |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1142 |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1143 types.push_back(DtoSize_t()); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1144 |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1145 size_t offset = vd->offset; // TODO might not be the true offset |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1146 // dmd only accounts for the vtable, not classinfo or monitor |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1147 if (global.params.is64bit) |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1148 offset += 8; |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1149 else |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1150 offset += 4; |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1151 inits.push_back(DtoConstSize_t(offset)); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1152 |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1153 vd->type->getTypeInfo(NULL); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1154 assert(vd->type->vtinfo); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1155 DtoForceDeclareDsymbol(vd->type->vtinfo); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1156 llvm::Constant* c = isaConstant(vd->type->vtinfo->llvmValue); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1157 |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1158 const llvm::Type* tiTy = getPtrToType(Type::typeinfo->type->llvmType->get()); |
123
7f9a0a58394b
[svn r127] Updated the lphobos build scripts a little. Created a new rebuild profile.
lindquist
parents:
121
diff
changeset
|
1159 //Logger::cout() << "tiTy = " << *tiTy << '\n'; |
110
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1160 |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1161 types.push_back(tiTy); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1162 inits.push_back(llvm::ConstantExpr::getBitCast(c, tiTy)); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1163 |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1164 const llvm::StructType* sTy = llvm::StructType::get(types); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1165 return llvm::ConstantStruct::get(sTy, inits); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1166 } |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1167 |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1168 static llvm::Constant* build_offti_array(ClassDeclaration* cd, llvm::Constant* init) |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1169 { |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1170 const llvm::StructType* initTy = isaStruct(init->getType()); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1171 assert(initTy); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1172 |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1173 std::vector<llvm::Constant*> arrayInits; |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1174 for (ClassDeclaration *cd2 = cd; cd2; cd2 = cd2->baseClass) |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1175 { |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1176 if (cd2->members) |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1177 { |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1178 for (size_t i = 0; i < cd2->members->dim; i++) |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1179 { |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1180 Dsymbol *sm = (Dsymbol *)cd2->members->data[i]; |
112
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1181 if (VarDeclaration* vd = sm->isVarDeclaration()) // is this enough? |
110
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1182 { |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1183 llvm::Constant* c = build_offti_entry(vd); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1184 assert(c); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1185 arrayInits.push_back(c); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1186 } |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1187 } |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1188 } |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1189 } |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1190 |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1191 size_t ninits = arrayInits.size(); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1192 llvm::Constant* size = DtoConstSize_t(ninits); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1193 llvm::Constant* ptr; |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1194 |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1195 if (ninits > 0) { |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1196 // OffsetTypeInfo type |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1197 std::vector<const llvm::Type*> elemtypes; |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1198 elemtypes.push_back(DtoSize_t()); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1199 const llvm::Type* tiTy = getPtrToType(Type::typeinfo->type->llvmType->get()); |
110
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1200 elemtypes.push_back(tiTy); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1201 const llvm::StructType* sTy = llvm::StructType::get(elemtypes); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1202 |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1203 // array type |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1204 const llvm::ArrayType* arrTy = llvm::ArrayType::get(sTy, ninits); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1205 llvm::Constant* arrInit = llvm::ConstantArray::get(arrTy, arrayInits); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1206 |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1207 std::string name(cd->type->vtinfo->toChars()); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1208 name.append("__OffsetTypeInfos"); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1209 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(arrTy,true,llvm::GlobalValue::InternalLinkage,arrInit,name,gIR->module); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1210 ptr = llvm::ConstantExpr::getBitCast(gvar, getPtrToType(sTy)); |
110
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1211 } |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1212 else { |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1213 ptr = llvm::ConstantPointerNull::get(isaPointer(initTy->getElementType(1))); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1214 } |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1215 |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1216 return DtoConstSlice(size, ptr); |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1217 } |
e8da7856a260
[svn r114] Implemented the ClassInfo.offTi member.
lindquist
parents:
106
diff
changeset
|
1218 |
112
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1219 static llvm::Constant* build_class_dtor(ClassDeclaration* cd) |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1220 { |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1221 // construct the function |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1222 std::vector<const llvm::Type*> paramTypes; |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1223 paramTypes.push_back(getPtrToType(cd->type->llvmType->get())); |
112
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1224 |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1225 const llvm::FunctionType* fnTy = llvm::FunctionType::get(llvm::Type::VoidTy, paramTypes, false); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1226 |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1227 if (cd->dtors.dim == 0) { |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1228 return llvm::ConstantPointerNull::get(getPtrToType(llvm::Type::Int8Ty)); |
112
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1229 } |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1230 else if (cd->dtors.dim == 1) { |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1231 DtorDeclaration *d = (DtorDeclaration *)cd->dtors.data[0]; |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1232 DtoForceDeclareDsymbol(d); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1233 assert(d->llvmValue); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1234 return llvm::ConstantExpr::getBitCast(isaConstant(d->llvmValue), getPtrToType(llvm::Type::Int8Ty)); |
112
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1235 } |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1236 |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1237 std::string gname("_D"); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1238 gname.append(cd->mangle()); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1239 gname.append("12__destructorMFZv"); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1240 |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1241 llvm::Function* func = new llvm::Function(fnTy, llvm::GlobalValue::InternalLinkage, gname, gIR->module); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1242 llvm::Value* thisptr = func->arg_begin(); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1243 thisptr->setName("this"); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1244 |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1245 llvm::BasicBlock* bb = new llvm::BasicBlock("entry", func); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1246 LLVMBuilder builder(bb); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1247 |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1248 for (size_t i = 0; i < cd->dtors.dim; i++) |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1249 { |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1250 DtorDeclaration *d = (DtorDeclaration *)cd->dtors.data[i]; |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1251 DtoForceDeclareDsymbol(d); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1252 assert(d->llvmValue); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1253 builder.CreateCall(d->llvmValue, thisptr); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1254 } |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1255 builder.CreateRetVoid(); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1256 |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1257 return llvm::ConstantExpr::getBitCast(func, getPtrToType(llvm::Type::Int8Ty)); |
112
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1258 } |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1259 |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1260 static uint build_classinfo_flags(ClassDeclaration* cd) |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1261 { |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1262 // adapted from original dmd code |
112
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1263 uint flags = 0; |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1264 //flags |= isCOMclass(); // IUnknown |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1265 bool hasOffTi = false; |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1266 if (cd->ctor) flags |= 8; |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1267 for (ClassDeclaration *cd2 = cd; cd2; cd2 = cd2->baseClass) |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1268 { |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1269 if (cd2->members) |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1270 { |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1271 for (size_t i = 0; i < cd2->members->dim; i++) |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1272 { |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1273 Dsymbol *sm = (Dsymbol *)cd2->members->data[i]; |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1274 if (sm->isVarDeclaration()) // is this enough? |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1275 hasOffTi = true; |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1276 //printf("sm = %s %s\n", sm->kind(), sm->toChars()); |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1277 if (sm->hasPointers()) |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1278 goto L2; |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1279 } |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1280 } |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1281 } |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1282 flags |= 2; // no pointers |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1283 L2: |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1284 if (hasOffTi) |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1285 flags |= 4; |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1286 return flags; |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1287 } |
368547b1cbe6
[svn r116] Implemented the ClassInfo.destructor field.
lindquist
parents:
111
diff
changeset
|
1288 |
100 | 1289 void DtoDefineClassInfo(ClassDeclaration* cd) |
1290 { | |
1291 // The layout is: | |
1292 // { | |
1293 // void **vptr; | |
1294 // monitor_t monitor; | |
1295 // byte[] initializer; // static initialization data | |
1296 // char[] name; // class name | |
1297 // void *[] vtbl; | |
1298 // Interface[] interfaces; | |
1299 // ClassInfo *base; // base class | |
1300 // void *destructor; | |
1301 // void *invariant; // class invariant | |
1302 // uint flags; | |
1303 // void *deallocator; | |
1304 // OffsetTypeInfo[] offTi; | |
1305 // void *defaultConstructor; | |
1306 // } | |
1307 | |
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
|
1308 if (cd->llvmClassDefined) 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
|
1309 cd->llvmClassDefined = 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
|
1310 |
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
|
1311 Logger::println("DtoDefineClassInfo(%s)", cd->toChars()); |
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
|
1312 LOG_SCOPE; |
100 | 1313 |
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
|
1314 assert(cd->type->ty == Tclass); |
100 | 1315 assert(cd->llvmClass); |
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
|
1316 |
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
|
1317 TypeClass* cdty = (TypeClass*)cd->type; |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1318 if (!cd->isInterfaceDeclaration() && !cd->isAbstract()) { |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1319 assert(cd->llvmInit); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1320 assert(cd->llvmConstInit); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1321 assert(cd->llvmVtbl); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1322 assert(cd->llvmConstVtbl); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1323 } |
100 | 1324 |
1325 // holds the list of initializers for llvm | |
1326 std::vector<llvm::Constant*> inits; | |
1327 | |
1328 ClassDeclaration* cinfo = ClassDeclaration::classinfo; | |
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
|
1329 DtoForceConstInitDsymbol(cinfo); |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1330 assert(cinfo->llvmConstInit); |
100 | 1331 |
1332 llvm::Constant* c; | |
1333 | |
1334 // own vtable | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1335 c = cinfo->llvmConstInit->getOperand(0); |
100 | 1336 assert(c); |
1337 inits.push_back(c); | |
1338 | |
1339 // monitor | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1340 c = cinfo->llvmConstInit->getOperand(1); |
115
5ba6d286c941
[svn r119] Added the monitor data field that comes after the vtable pointer to all classes. Represented as a void* initialized to zero.
lindquist
parents:
114
diff
changeset
|
1341 inits.push_back(c); |
100 | 1342 |
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
|
1343 // byte[] init |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1344 const llvm::Type* byteptrty = getPtrToType(llvm::Type::Int8Ty); |
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1345 if (cd->isInterfaceDeclaration() || cd->isAbstract()) { |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1346 c = cinfo->llvmConstInit->getOperand(2); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1347 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1348 else { |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1349 c = llvm::ConstantExpr::getBitCast(cd->llvmInit, byteptrty); |
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1350 assert(!cd->llvmConstInit->getType()->isAbstract()); |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1351 size_t initsz = getABITypeSize(cd->llvmConstInit->getType()); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1352 c = DtoConstSlice(DtoConstSize_t(initsz), c); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1353 } |
100 | 1354 inits.push_back(c); |
1355 | |
1356 // class name | |
1357 // from dmd | |
1358 char *name = cd->ident->toChars(); | |
1359 size_t namelen = strlen(name); | |
1360 if (!(namelen > 9 && memcmp(name, "TypeInfo_", 9) == 0)) | |
1361 { | |
1362 name = cd->toPrettyChars(); | |
1363 namelen = strlen(name); | |
1364 } | |
1365 c = DtoConstString(name); | |
1366 inits.push_back(c); | |
1367 | |
1368 // vtbl array | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1369 if (cd->isInterfaceDeclaration() || cd->isAbstract()) { |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1370 c = cinfo->llvmConstInit->getOperand(4); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1371 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1372 else { |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1373 const llvm::Type* byteptrptrty = getPtrToType(byteptrty); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1374 assert(!cd->llvmVtbl->getType()->isAbstract()); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1375 c = llvm::ConstantExpr::getBitCast(cd->llvmVtbl, byteptrptrty); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1376 assert(!cd->llvmConstVtbl->getType()->isAbstract()); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1377 size_t vtblsz = cd->llvmConstVtbl->getType()->getNumElements(); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1378 c = DtoConstSlice(DtoConstSize_t(vtblsz), c); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1379 } |
100 | 1380 inits.push_back(c); |
1381 | |
1382 // interfaces array | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1383 IRStruct* irstruct = cd->llvmIRStruct; |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1384 if (cd->isInterfaceDeclaration() || !irstruct->interfaceInfos || cd->isAbstract()) { |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1385 c = cinfo->llvmConstInit->getOperand(5); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1386 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1387 else { |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1388 const llvm::Type* t = cinfo->llvmConstInit->getOperand(5)->getType()->getContainedType(1); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1389 c = llvm::ConstantExpr::getBitCast(irstruct->interfaceInfos, t); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1390 size_t iisz = irstruct->interfaceInfosTy->getNumElements(); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1391 c = DtoConstSlice(DtoConstSize_t(iisz), c); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1392 } |
100 | 1393 inits.push_back(c); |
1394 | |
1395 // base classinfo | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1396 if (cd->baseClass && !cd->isInterfaceDeclaration() && !cd->isAbstract()) { |
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
|
1397 DtoDeclareClassInfo(cd->baseClass); |
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
|
1398 c = cd->baseClass->llvmClass; |
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
|
1399 assert(c); |
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
|
1400 inits.push_back(c); |
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
|
1401 } |
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
|
1402 else { |
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
|
1403 // null |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1404 c = cinfo->llvmConstInit->getOperand(6); |
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
|
1405 inits.push_back(c); |
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
|
1406 } |
100 | 1407 |
1408 // destructor | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1409 if (cd->isInterfaceDeclaration() || cd->isAbstract()) { |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1410 c = cinfo->llvmConstInit->getOperand(7); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1411 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1412 else { |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1413 c = build_class_dtor(cd); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1414 } |
100 | 1415 inits.push_back(c); |
1416 | |
1417 // invariant | |
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
|
1418 // TODO |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1419 c = cinfo->llvmConstInit->getOperand(8); |
100 | 1420 inits.push_back(c); |
1421 | |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1422 // uint flags |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1423 if (cd->isInterfaceDeclaration() || cd->isAbstract()) { |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1424 c = cinfo->llvmConstInit->getOperand(9); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1425 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1426 else { |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1427 uint flags = build_classinfo_flags(cd); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1428 c = DtoConstUint(flags); |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1429 } |
100 | 1430 inits.push_back(c); |
1431 | |
1432 // allocator | |
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
|
1433 // TODO |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1434 c = cinfo->llvmConstInit->getOperand(10); |
100 | 1435 inits.push_back(c); |
1436 | |
1437 // offset typeinfo | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1438 if (cd->isInterfaceDeclaration() || cd->isAbstract()) { |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1439 c = cinfo->llvmConstInit->getOperand(11); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1440 } |
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1441 else { |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1442 c = build_offti_array(cd, cinfo->llvmConstInit->getOperand(11)); |
113
27b9f749d9fe
[svn r117] Initial working implementation of interfaces.
lindquist
parents:
112
diff
changeset
|
1443 } |
100 | 1444 inits.push_back(c); |
1445 | |
1446 // default constructor | |
133
44a95ac7368a
[svn r137] Many fixes towards tango.io.Console working, but not quite there yet...
lindquist
parents:
132
diff
changeset
|
1447 if (cd->defaultCtor && !cd->isInterfaceDeclaration() && !cd->isAbstract()) { |
111
a7ae554ce4f4
[svn r115] Implemented the ClassInfo.defaultConstructor member.
lindquist
parents:
110
diff
changeset
|
1448 DtoForceDeclareDsymbol(cd->defaultCtor); |
a7ae554ce4f4
[svn r115] Implemented the ClassInfo.defaultConstructor member.
lindquist
parents:
110
diff
changeset
|
1449 c = isaConstant(cd->defaultCtor->llvmValue); |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1450 const llvm::Type* toTy = cinfo->llvmConstInit->getOperand(12)->getType(); |
117 | 1451 c = llvm::ConstantExpr::getBitCast(c, toTy); |
111
a7ae554ce4f4
[svn r115] Implemented the ClassInfo.defaultConstructor member.
lindquist
parents:
110
diff
changeset
|
1452 } |
a7ae554ce4f4
[svn r115] Implemented the ClassInfo.defaultConstructor member.
lindquist
parents:
110
diff
changeset
|
1453 else { |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1454 c = cinfo->llvmConstInit->getOperand(12); |
111
a7ae554ce4f4
[svn r115] Implemented the ClassInfo.defaultConstructor member.
lindquist
parents:
110
diff
changeset
|
1455 } |
100 | 1456 inits.push_back(c); |
1457 | |
1458 /*size_t n = inits.size(); | |
1459 for (size_t i=0; i<n; ++i) | |
1460 { | |
1461 Logger::cout() << "inits[" << i << "]: " << *inits[i] << '\n'; | |
1462 }*/ | |
1463 | |
1464 // build the initializer | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1465 const llvm::StructType* st = isaStruct(cinfo->llvmConstInit->getType()); |
100 | 1466 llvm::Constant* finalinit = llvm::ConstantStruct::get(st, inits); |
1467 //Logger::cout() << "built the classinfo initializer:\n" << *finalinit <<'\n'; | |
1468 | |
121
9c79b61fb638
[svn r125] Renamed/moved a few backend member inside DMD structures for consistency.
lindquist
parents:
117
diff
changeset
|
1469 cd->llvmConstClass = finalinit; |
100 | 1470 cd->llvmClass->setInitializer(finalinit); |
1471 } |