comparison gen/structs.cpp @ 133:44a95ac7368a trunk

[svn r137] Many fixes towards tango.io.Console working, but not quite there yet... In particular, assertions has been fixed to include file/line info, and much more!
author lindquist
date Mon, 14 Jan 2008 05:11:54 +0100
parents 1700239cab2e
children 0e28624814e8
comparison
equal deleted inserted replaced
132:1700239cab2e 133:44a95ac7368a
25 ////////////////////////////////////////////////////////////////////////////////////////// 25 //////////////////////////////////////////////////////////////////////////////////////////
26 26
27 llvm::Value* DtoStructZeroInit(llvm::Value* v) 27 llvm::Value* DtoStructZeroInit(llvm::Value* v)
28 { 28 {
29 assert(gIR); 29 assert(gIR);
30 uint64_t n = gTargetData->getTypeSize(v->getType()->getContainedType(0)); 30 uint64_t n = getTypeStoreSize(v->getType()->getContainedType(0));
31 //llvm::Type* sarrty = llvm::PointerType::get(llvm::ArrayType::get(llvm::Type::Int8Ty, n)); 31 //llvm::Type* sarrty = getPtrToType(llvm::ArrayType::get(llvm::Type::Int8Ty, n));
32 llvm::Type* sarrty = llvm::PointerType::get(llvm::Type::Int8Ty); 32 const llvm::Type* sarrty = getPtrToType(llvm::Type::Int8Ty);
33 33
34 llvm::Value* sarr = new llvm::BitCastInst(v,sarrty,"tmp",gIR->scopebb()); 34 llvm::Value* sarr = DtoBitCast(v, sarrty);
35 35
36 llvm::Function* fn = LLVM_DeclareMemSet32(); 36 llvm::Function* fn = LLVM_DeclareMemSet32();
37 std::vector<llvm::Value*> llargs; 37 std::vector<llvm::Value*> llargs;
38 llargs.resize(4); 38 llargs.resize(4);
39 llargs[0] = sarr; 39 llargs[0] = sarr;
52 { 52 {
53 Logger::cout() << "dst = " << *dst << " src = " << *src << '\n'; 53 Logger::cout() << "dst = " << *dst << " src = " << *src << '\n';
54 assert(dst->getType() == src->getType()); 54 assert(dst->getType() == src->getType());
55 assert(gIR); 55 assert(gIR);
56 56
57 uint64_t n = gTargetData->getTypeSize(dst->getType()->getContainedType(0)); 57 uint64_t n = getTypeStoreSize(dst->getType()->getContainedType(0));
58 //llvm::Type* sarrty = llvm::PointerType::get(llvm::ArrayType::get(llvm::Type::Int8Ty, n)); 58 //llvm::Type* sarrty = getPtrToType(llvm::ArrayType::get(llvm::Type::Int8Ty, n));
59 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); 59 const llvm::Type* arrty = getPtrToType(llvm::Type::Int8Ty);
60 60
61 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); 61 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb());
62 llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb()); 62 llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb());
63 63
64 llvm::Function* fn = LLVM_DeclareMemCpy32(); 64 llvm::Function* fn = LLVM_DeclareMemCpy32();
108 LOG_SCOPE; 108 LOG_SCOPE;
109 109
110 if (idxs.empty()) 110 if (idxs.empty())
111 idxs.push_back(0); 111 idxs.push_back(0);
112 112
113 const llvm::Type* llt = llvm::PointerType::get(DtoType(t)); 113 const llvm::Type* llt = getPtrToType(DtoType(t));
114 const llvm::Type* st = llvm::PointerType::get(DtoType(sd->type)); 114 const llvm::Type* st = getPtrToType(DtoType(sd->type));
115 if (ptr->getType() != st) { 115 if (ptr->getType() != st) {
116 assert(sd->llvmHasUnions); 116 assert(sd->llvmHasUnions);
117 ptr = gIR->ir->CreateBitCast(ptr, st, "tmp"); 117 ptr = gIR->ir->CreateBitCast(ptr, st, "tmp");
118 } 118 }
119 119
137 idxs.push_back(vd->llvmFieldIndex); 137 idxs.push_back(vd->llvmFieldIndex);
138 if (vd->llvmFieldIndexOffset) { 138 if (vd->llvmFieldIndexOffset) {
139 Logger::println("has union field offset"); 139 Logger::println("has union field offset");
140 ptr = DtoGEP(ptr, idxs, "tmp"); 140 ptr = DtoGEP(ptr, idxs, "tmp");
141 if (ptr->getType() != llt) 141 if (ptr->getType() != llt)
142 ptr = gIR->ir->CreateBitCast(ptr, llt, "tmp"); 142 ptr = DtoBitCast(ptr, llt);
143 ptr = new llvm::GetElementPtrInst(ptr, DtoConstUint(vd->llvmFieldIndexOffset), "tmp", gIR->scopebb()); 143 ptr = new llvm::GetElementPtrInst(ptr, DtoConstUint(vd->llvmFieldIndexOffset), "tmp", gIR->scopebb());
144 std::vector<unsigned> tmp; 144 std::vector<unsigned> tmp;
145 return DtoIndexStruct(ptr, ssd, t, os-vd->offset, tmp); 145 return DtoIndexStruct(ptr, ssd, t, os-vd->offset, tmp);
146 } 146 }
147 else { 147 else {
148 const llvm::Type* sty = llvm::PointerType::get(DtoType(vd->type)); 148 const llvm::Type* sty = getPtrToType(DtoType(vd->type));
149 if (ptr->getType() != sty) { 149 if (ptr->getType() != sty) {
150 ptr = gIR->ir->CreateBitCast(ptr, sty, "tmp"); 150 ptr = DtoBitCast(ptr, sty);
151 std::vector<unsigned> tmp; 151 std::vector<unsigned> tmp;
152 return DtoIndexStruct(ptr, ssd, t, os-vd->offset, tmp); 152 return DtoIndexStruct(ptr, ssd, t, os-vd->offset, tmp);
153 } 153 }
154 else { 154 else {
155 return DtoIndexStruct(ptr, ssd, t, os-vd->offset, idxs); 155 return DtoIndexStruct(ptr, ssd, t, os-vd->offset, idxs);
156 } 156 }
157 } 157 }
158 } 158 }
159 } 159 }
160 160
161 size_t llt_sz = gTargetData->getTypeSize(llt->getContainedType(0)); 161 size_t llt_sz = getTypeStoreSize(llt->getContainedType(0));
162 assert(os % llt_sz == 0); 162 assert(os % llt_sz == 0);
163 ptr = gIR->ir->CreateBitCast(ptr, llt, "tmp"); 163 ptr = DtoBitCast(ptr, llt);
164 return new llvm::GetElementPtrInst(ptr, DtoConstUint(os / llt_sz), "tmp", gIR->scopebb()); 164 return new llvm::GetElementPtrInst(ptr, DtoConstUint(os / llt_sz), "tmp", gIR->scopebb());
165 } 165 }
166 166
167 ////////////////////////////////////////////////////////////////////////////////////////// 167 //////////////////////////////////////////////////////////////////////////////////////////
168 168
178 178
179 IRStruct* irstruct = new IRStruct(ts); 179 IRStruct* irstruct = new IRStruct(ts);
180 sd->llvmIRStruct = irstruct; 180 sd->llvmIRStruct = irstruct;
181 gIR->structs.push_back(irstruct); 181 gIR->structs.push_back(irstruct);
182 182
183 // fields
183 Array* arr = &sd->fields; 184 Array* arr = &sd->fields;
184 for (int k=0; k < arr->dim; k++) { 185 for (int k=0; k < arr->dim; k++) {
185 VarDeclaration* v = (VarDeclaration*)(arr->data[k]); 186 VarDeclaration* v = (VarDeclaration*)arr->data[k];
186 v->toObjFile(); 187 v->toObjFile();
188 }
189
190 bool thisModule = false;
191 if (sd->getModule() == gIR->dmodule)
192 thisModule = true;
193
194 // methods
195 arr = sd->members;
196 for (int k=0; k < arr->dim; k++) {
197 Dsymbol* s = (Dsymbol*)arr->data[k];
198 if (FuncDeclaration* fd = s->isFuncDeclaration()) {
199 if (thisModule || (fd->prot() != PROTprivate)) {
200 fd->toObjFile();
201 }
202 }
203 else if (s->isAttribDeclaration()) {
204 s->toObjFile();
205 }
206 else {
207 Logger::println("Ignoring dsymbol '%s' in this->members of kind '%s'", s->toPrettyChars(), s->kind());
208 }
187 } 209 }
188 210
189 /*for (int k=0; k < sd->members->dim; k++) { 211 /*for (int k=0; k < sd->members->dim; k++) {
190 Dsymbol* dsym = (Dsymbol*)(sd->members->data[k]); 212 Dsymbol* dsym = (Dsymbol*)(sd->members->data[k]);
191 dsym->toObjFile(); 213 dsym->toObjFile();
216 if (lastoffset == (unsigned)-1) { 238 if (lastoffset == (unsigned)-1) {
217 lastoffset = i->first; 239 lastoffset = i->first;
218 assert(lastoffset == 0); 240 assert(lastoffset == 0);
219 fieldtype = i->second.type; 241 fieldtype = i->second.type;
220 fieldinit = i->second.var; 242 fieldinit = i->second.var;
221 prevsize = gTargetData->getTypeSize(fieldtype); 243 prevsize = getABITypeSize(fieldtype);
222 i->second.var->llvmFieldIndex = idx; 244 i->second.var->llvmFieldIndex = idx;
223 } 245 }
224 // colliding offset? 246 // colliding offset?
225 else if (lastoffset == i->first) { 247 else if (lastoffset == i->first) {
226 size_t s = gTargetData->getTypeSize(i->second.type); 248 size_t s = getABITypeSize(i->second.type);
227 if (s > prevsize) { 249 if (s > prevsize) {
228 fieldpad += s - prevsize; 250 fieldpad += s - prevsize;
229 prevsize = s; 251 prevsize = s;
230 } 252 }
231 sd->llvmHasUnions = true; 253 sd->llvmHasUnions = true;
232 i->second.var->llvmFieldIndex = idx; 254 i->second.var->llvmFieldIndex = idx;
233 } 255 }
234 // intersecting offset? 256 // intersecting offset?
235 else if (i->first < (lastoffset + prevsize)) { 257 else if (i->first < (lastoffset + prevsize)) {
236 size_t s = gTargetData->getTypeSize(i->second.type); 258 size_t s = getABITypeSize(i->second.type);
237 assert((i->first + s) <= (lastoffset + prevsize)); // this holds because all types are aligned to their size 259 assert((i->first + s) <= (lastoffset + prevsize)); // this holds because all types are aligned to their size
238 sd->llvmHasUnions = true; 260 sd->llvmHasUnions = true;
239 i->second.var->llvmFieldIndex = idx; 261 i->second.var->llvmFieldIndex = idx;
240 i->second.var->llvmFieldIndexOffset = (i->first - lastoffset) / s; 262 i->second.var->llvmFieldIndexOffset = (i->first - lastoffset) / s;
241 } 263 }
254 276
255 // start new 277 // start new
256 lastoffset = i->first; 278 lastoffset = i->first;
257 fieldtype = i->second.type; 279 fieldtype = i->second.type;
258 fieldinit = i->second.var; 280 fieldinit = i->second.var;
259 prevsize = gTargetData->getTypeSize(fieldtype); 281 prevsize = getABITypeSize(fieldtype);
260 i->second.var->llvmFieldIndex = idx; 282 i->second.var->llvmFieldIndex = idx;
261 fieldpad = 0; 283 fieldpad = 0;
262 } 284 }
263 } 285 }
264 fieldtypes.push_back(fieldtype); 286 fieldtypes.push_back(fieldtype);
418 for (IRStruct::OffsetMap::iterator i=topstruct->offsets.begin(); i!=topstruct->offsets.end(); ++i) 440 for (IRStruct::OffsetMap::iterator i=topstruct->offsets.begin(); i!=topstruct->offsets.end(); ++i)
419 { 441 {
420 unsigned o = i->first; 442 unsigned o = i->first;
421 IRStruct::Offset* so = &i->second; 443 IRStruct::Offset* so = &i->second;
422 const llvm::Type* ft = so->init->getType(); 444 const llvm::Type* ft = so->init->getType();
423 size_t sz = gTargetData->getTypeSize(ft); 445 size_t sz = getABITypeSize(ft);
424 if (f == NULL) { // new field 446 if (f == NULL) { // new field
425 fields.push_back(DUnionField()); 447 fields.push_back(DUnionField());
426 f = &fields.back(); 448 f = &fields.back();
427 f->size = sz; 449 f->size = sz;
428 f->offset = o; 450 f->offset = o;
493 515
494 bool nextSame = (ii+1 < nin) && (in[ii+1].idx == fi); 516 bool nextSame = (ii+1 < nin) && (in[ii+1].idx == fi);
495 517
496 if (ii < nin && fi == in[ii].idx) 518 if (ii < nin && fi == in[ii].idx)
497 { 519 {
498 size_t s = gTargetData->getTypeSize(in[ii].c->getType()); 520 size_t s = getABITypeSize(in[ii].c->getType());
499 if (in[ii].idx == last) 521 if (in[ii].idx == last)
500 { 522 {
501 size_t nos = in[ii].idxos * s; 523 size_t nos = in[ii].idxos * s;
502 if (nos && nos-os) { 524 if (nos && nos-os) {
503 assert(nos >= os); 525 assert(nos >= os);