annotate gen/classes.cpp @ 136:0e28624814e8 trunk

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