annotate gen/classes.cpp @ 213:7816aafeea3c trunk

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