Mercurial > projects > ldc
annotate gen/tollvm.c @ 52:0c77619e803b trunk
[svn r56] Initial support for TypeInfo.
Enums not work.
Several other bugfixes.
author | lindquist |
---|---|
date | Tue, 23 Oct 2007 05:55:12 +0200 |
parents | 61bc1b4ad3c4 |
children | 06ccc817acd4 |
rev | line source |
---|---|
1 | 1 #include <iostream> |
2 | |
40 | 3 #include "gen/llvm.h" |
1 | 4 |
5 #include "mtype.h" | |
6 #include "dsymbol.h" | |
7 #include "aggregate.h" | |
8 #include "declaration.h" | |
9 #include "init.h" | |
10 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
11 #include "gen/tollvm.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
12 #include "gen/irstate.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
13 #include "gen/logger.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
14 #include "gen/runtime.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
15 #include "gen/elem.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
16 #include "gen/arrays.h" |
1 | 17 |
40 | 18 bool LLVM_DtoIsPassedByRef(Type* type) |
19 { | |
20 TY t = type->ty; | |
21 if (t == Tstruct || t == Tarray || t == Tdelegate) | |
22 return true; | |
23 else if (t == Ttypedef) { | |
24 Type* bt = type->toBasetype(); | |
25 assert(bt); | |
26 return LLVM_DtoIsPassedByRef(bt); | |
27 } | |
28 return false; | |
29 } | |
30 | |
31 Type* LLVM_DtoDType(Type* t) | |
32 { | |
33 if (t->ty == Ttypedef) { | |
34 Type* bt = t->toBasetype(); | |
35 assert(bt); | |
36 return LLVM_DtoDType(bt); | |
37 } | |
38 return t; | |
39 } | |
40 | |
1 | 41 const llvm::Type* LLVM_DtoType(Type* t) |
42 { | |
43 assert(t); | |
44 switch (t->ty) | |
45 { | |
46 // integers | |
47 case Tint8: | |
48 case Tuns8: | |
49 case Tchar: | |
50 return (const llvm::Type*)llvm::Type::Int8Ty; | |
51 case Tint16: | |
52 case Tuns16: | |
53 case Twchar: | |
54 return (const llvm::Type*)llvm::Type::Int16Ty; | |
55 case Tint32: | |
56 case Tuns32: | |
57 case Tdchar: | |
58 return (const llvm::Type*)llvm::Type::Int32Ty; | |
59 case Tint64: | |
60 case Tuns64: | |
61 return (const llvm::Type*)llvm::Type::Int64Ty; | |
62 | |
63 case Tbool: | |
64 return (const llvm::Type*)llvm::ConstantInt::getTrue()->getType(); | |
65 | |
66 // floats | |
67 case Tfloat32: | |
68 return llvm::Type::FloatTy; | |
69 case Tfloat64: | |
70 case Tfloat80: | |
71 return llvm::Type::DoubleTy; | |
72 | |
73 // pointers | |
74 case Tpointer: { | |
75 assert(t->next); | |
76 if (t->next->ty == Tvoid) | |
77 return (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
78 else | |
79 return (const llvm::Type*)llvm::PointerType::get(LLVM_DtoType(t->next)); | |
80 } | |
81 | |
82 // arrays | |
83 case Tarray: | |
84 return LLVM_DtoArrayType(t); | |
85 case Tsarray: | |
86 return LLVM_DtoStaticArrayType(t); | |
87 | |
88 // void | |
89 case Tvoid: | |
90 return llvm::Type::VoidTy; | |
91 | |
92 // aggregates | |
93 case Tstruct: { | |
94 if (t->llvmType == 0) | |
95 { | |
96 // recursive or cyclic declaration | |
97 if (!gIR->structs.empty()) | |
98 { | |
99 IRStruct* found = 0; | |
100 for (IRState::StructVector::iterator i=gIR->structs.begin(); i!=gIR->structs.end(); ++i) | |
101 { | |
102 if (t == i->type) | |
103 { | |
104 return i->recty.get(); | |
105 } | |
106 } | |
107 } | |
108 | |
109 // forward declaration | |
110 TypeStruct* ts = (TypeStruct*)t; | |
111 assert(ts->sym); | |
112 ts->sym->toObjFile(); | |
113 } | |
114 return t->llvmType; | |
115 } | |
116 | |
117 case Tclass: { | |
118 if (t->llvmType == 0) | |
119 { | |
6 | 120 // recursive or cyclic declaration |
121 if (!gIR->structs.empty()) | |
122 { | |
123 IRStruct* found = 0; | |
124 for (IRState::StructVector::iterator i=gIR->structs.begin(); i!=gIR->structs.end(); ++i) | |
125 { | |
126 if (t == i->type) | |
127 { | |
128 return llvm::PointerType::get(i->recty.get()); | |
129 } | |
130 } | |
131 } | |
132 | |
133 // forward declaration | |
1 | 134 TypeClass* tc = (TypeClass*)t; |
135 assert(tc->sym); | |
6 | 136 tc->sym->toObjFile(); |
1 | 137 } |
138 return llvm::PointerType::get(t->llvmType); | |
139 } | |
140 | |
141 // functions | |
142 case Tfunction: | |
143 { | |
144 if (t->llvmType == 0) { | |
145 return LLVM_DtoFunctionType(t); | |
146 } | |
147 else { | |
148 return t->llvmType; | |
149 } | |
150 } | |
6 | 151 |
1 | 152 // delegates |
153 case Tdelegate: | |
154 { | |
155 if (t->llvmType == 0) { | |
156 return LLVM_DtoDelegateType(t); | |
157 } | |
158 else { | |
159 return t->llvmType; | |
160 } | |
161 } | |
162 | |
163 // typedefs | |
52 | 164 // enum |
1 | 165 case Ttypedef: |
52 | 166 case Tenum: |
1 | 167 { |
168 Type* bt = t->toBasetype(); | |
169 assert(bt); | |
170 return LLVM_DtoType(bt); | |
171 } | |
172 | |
173 default: | |
174 printf("trying to convert unknown type with value %d\n", t->ty); | |
175 assert(0); | |
176 } | |
177 return 0; | |
178 } | |
179 | |
180 ////////////////////////////////////////////////////////////////////////////////////////// | |
181 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
182 const llvm::FunctionType* LLVM_DtoFunctionType(Type* t, const llvm::Type* thisparam) |
1 | 183 { |
184 TypeFunction* f = (TypeFunction*)t; | |
185 | |
186 // parameter types | |
187 const llvm::Type* rettype; | |
188 std::vector<const llvm::Type*> paramvec; | |
189 | |
40 | 190 if (LLVM_DtoIsPassedByRef(f->next)) { |
1 | 191 rettype = llvm::PointerType::get(LLVM_DtoType(f->next)); |
192 paramvec.push_back(rettype); | |
193 rettype = llvm::Type::VoidTy; | |
194 } | |
195 else { | |
196 Type* rt = f->next; | |
197 if (rt) | |
198 rettype = LLVM_DtoType(rt); | |
199 else | |
200 assert(0); | |
201 } | |
202 | |
203 if (thisparam) { | |
204 paramvec.push_back(thisparam); | |
205 } | |
206 | |
207 size_t n = Argument::dim(f->parameters); | |
208 for (int i=0; i < n; ++i) { | |
209 Argument* arg = Argument::getNth(f->parameters, i); | |
210 // ensure scalar | |
211 Type* argT = arg->type; | |
212 assert(argT); | |
213 paramvec.push_back(LLVM_DtoType(argT)); | |
214 } | |
215 | |
216 Logger::cout() << "Return type: " << *rettype << '\n'; | |
217 | |
218 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, paramvec, f->varargs); | |
219 return functype; | |
220 } | |
221 | |
222 ////////////////////////////////////////////////////////////////////////////////////////// | |
223 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
224 const llvm::FunctionType* LLVM_DtoFunctionType(FuncDeclaration* fdecl) |
1 | 225 { |
226 TypeFunction* f = (TypeFunction*)fdecl->type; | |
227 assert(f != 0); | |
228 | |
9 | 229 // type has already been resolved |
1 | 230 if (f->llvmType != 0) { |
231 return llvm::cast<llvm::FunctionType>(f->llvmType); | |
232 } | |
233 | |
234 // return value type | |
235 const llvm::Type* rettype; | |
236 const llvm::Type* actualRettype; | |
237 Type* rt = f->next; | |
238 bool retinptr = false; | |
239 bool usesthis = false; | |
240 | |
241 if (fdecl->isMain()) { | |
242 rettype = llvm::Type::Int32Ty; | |
243 actualRettype = rettype; | |
244 } | |
245 else if (rt) { | |
40 | 246 if (LLVM_DtoIsPassedByRef(rt)) { |
1 | 247 rettype = llvm::PointerType::get(LLVM_DtoType(rt)); |
248 actualRettype = llvm::Type::VoidTy; | |
249 f->llvmRetInPtr = retinptr = true; | |
250 } | |
251 else { | |
252 rettype = LLVM_DtoType(rt); | |
253 actualRettype = rettype; | |
254 } | |
255 } | |
256 else { | |
257 assert(0); | |
258 } | |
259 | |
260 // parameter types | |
261 std::vector<const llvm::Type*> paramvec; | |
262 | |
263 if (retinptr) { | |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
29
diff
changeset
|
264 Logger::cout() << "returning through pointer parameter: " << *rettype << '\n'; |
1 | 265 paramvec.push_back(rettype); |
266 } | |
267 | |
9 | 268 if (fdecl->needThis()) { |
269 if (AggregateDeclaration* ad = fdecl->isMember()) { | |
270 Logger::print("isMember = this is: %s\n", ad->type->toChars()); | |
271 const llvm::Type* thisty = LLVM_DtoType(ad->type); | |
31
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
29
diff
changeset
|
272 Logger::cout() << "this llvm type: " << *thisty << '\n'; |
2841234d2aea
[svn r35] * Attributes on struct fields/methods now work
lindquist
parents:
29
diff
changeset
|
273 if (llvm::isa<llvm::StructType>(thisty) || thisty == gIR->topstruct().recty.get()) |
9 | 274 thisty = llvm::PointerType::get(thisty); |
275 paramvec.push_back(thisty); | |
276 usesthis = true; | |
277 } | |
278 else | |
279 assert(0); | |
1 | 280 } |
49
e5c4bece7fa1
[svn r53] added basic support for delegate literals. if you access outer variables you get a broken module
lindquist
parents:
40
diff
changeset
|
281 else if (fdecl->isNested()) { |
e5c4bece7fa1
[svn r53] added basic support for delegate literals. if you access outer variables you get a broken module
lindquist
parents:
40
diff
changeset
|
282 paramvec.push_back(llvm::PointerType::get(llvm::Type::Int8Ty)); |
e5c4bece7fa1
[svn r53] added basic support for delegate literals. if you access outer variables you get a broken module
lindquist
parents:
40
diff
changeset
|
283 usesthis = true; |
e5c4bece7fa1
[svn r53] added basic support for delegate literals. if you access outer variables you get a broken module
lindquist
parents:
40
diff
changeset
|
284 } |
1 | 285 |
286 size_t n = Argument::dim(f->parameters); | |
287 for (int i=0; i < n; ++i) { | |
288 Argument* arg = Argument::getNth(f->parameters, i); | |
289 // ensure scalar | |
40 | 290 Type* argT = LLVM_DtoDType(arg->type); |
1 | 291 assert(argT); |
292 | |
293 if ((arg->storageClass & STCref) || (arg->storageClass & STCout)) { | |
294 //assert(arg->vardecl); | |
295 //arg->vardecl->refparam = true; | |
296 } | |
297 else | |
298 arg->llvmCopy = true; | |
299 | |
300 const llvm::Type* at = LLVM_DtoType(argT); | |
301 if (llvm::isa<llvm::StructType>(at)) { | |
302 Logger::println("struct param"); | |
303 paramvec.push_back(llvm::PointerType::get(at)); | |
304 } | |
305 else if (llvm::isa<llvm::ArrayType>(at)) { | |
306 Logger::println("sarray param"); | |
307 assert(argT->ty == Tsarray); | |
308 //paramvec.push_back(llvm::PointerType::get(at->getContainedType(0))); | |
309 paramvec.push_back(llvm::PointerType::get(at)); | |
310 } | |
40 | 311 else if (llvm::isa<llvm::OpaqueType>(at)) { |
312 Logger::println("opaque param"); | |
313 if (argT->ty == Tstruct || argT->ty == Tclass) | |
314 paramvec.push_back(llvm::PointerType::get(at)); | |
315 else | |
316 assert(0); | |
317 } | |
318 /*if (llvm::isa<llvm::StructType>(at) || argT->ty == Tstruct || argT->ty == Tsarray) { | |
319 paramvec.push_back(llvm::PointerType::get(at)); | |
320 }*/ | |
1 | 321 else { |
322 if (!arg->llvmCopy) { | |
323 Logger::println("ref param"); | |
324 at = llvm::PointerType::get(at); | |
325 } | |
326 else { | |
327 Logger::println("in param"); | |
328 } | |
329 paramvec.push_back(at); | |
330 } | |
331 } | |
332 | |
333 // construct function type | |
334 bool isvararg = f->varargs; | |
335 llvm::FunctionType* functype = llvm::FunctionType::get(actualRettype, paramvec, isvararg); | |
336 | |
337 f->llvmType = functype; | |
9 | 338 f->llvmRetInPtr = retinptr; |
339 f->llvmUsesThis = usesthis; | |
1 | 340 return functype; |
341 } | |
342 | |
343 ////////////////////////////////////////////////////////////////////////////////////////// | |
344 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
345 const llvm::StructType* LLVM_DtoDelegateType(Type* t) |
1 | 346 { |
347 const llvm::Type* i8ptr = llvm::PointerType::get(llvm::Type::Int8Ty); | |
348 const llvm::Type* func = LLVM_DtoFunctionType(t->next, i8ptr); | |
349 const llvm::Type* funcptr = llvm::PointerType::get(func); | |
350 | |
351 std::vector<const llvm::Type*> types; | |
352 types.push_back(i8ptr); | |
353 types.push_back(funcptr); | |
354 return llvm::StructType::get(types); | |
355 } | |
356 | |
357 ////////////////////////////////////////////////////////////////////////////////////////// | |
358 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
359 const llvm::Type* LLVM_DtoStructType(Type* t) |
1 | 360 { |
361 assert(0); | |
362 std::vector<const llvm::Type*> types; | |
363 return llvm::StructType::get(types); | |
364 } | |
365 | |
366 | |
367 ////////////////////////////////////////////////////////////////////////////////////////// | |
368 | |
369 static llvm::Function* LLVM_DeclareMemIntrinsic(const char* name, int bits, bool set=false) | |
370 { | |
371 assert(bits == 32 || bits == 64); | |
372 const llvm::Type* int8ty = (const llvm::Type*)llvm::Type::Int8Ty; | |
373 const llvm::Type* int32ty = (const llvm::Type*)llvm::Type::Int32Ty; | |
374 const llvm::Type* int64ty = (const llvm::Type*)llvm::Type::Int64Ty; | |
375 const llvm::Type* int8ptrty = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
376 const llvm::Type* voidty = (const llvm::Type*)llvm::Type::VoidTy; | |
377 | |
378 assert(gIR); | |
379 assert(gIR->module); | |
380 | |
381 // parameter types | |
382 std::vector<const llvm::Type*> pvec; | |
383 pvec.push_back(int8ptrty); | |
384 pvec.push_back(set?int8ty:int8ptrty); | |
385 pvec.push_back(bits==32?int32ty:int64ty); | |
386 pvec.push_back(int32ty); | |
387 llvm::FunctionType* functype = llvm::FunctionType::get(voidty, pvec, false); | |
388 return new llvm::Function(functype, llvm::GlobalValue::ExternalLinkage, name, gIR->module); | |
389 } | |
390 | |
391 ////////////////////////////////////////////////////////////////////////////////////////// | |
392 | |
393 // llvm.memset.i32 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
394 llvm::Function* LLVM_DeclareMemSet32() |
1 | 395 { |
396 static llvm::Function* _func = 0; | |
397 if (_func == 0) { | |
398 _func = LLVM_DeclareMemIntrinsic("llvm.memset.i32", 32, true); | |
399 } | |
400 return _func; | |
401 } | |
402 | |
403 ////////////////////////////////////////////////////////////////////////////////////////// | |
404 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
405 llvm::Function* LLVM_DeclareMemSet64() |
1 | 406 { |
407 static llvm::Function* _func = 0; | |
408 if (_func == 0) { | |
409 _func = LLVM_DeclareMemIntrinsic("llvm.memset.i64", 64, true); | |
410 } | |
411 return _func; | |
412 } | |
413 | |
414 ////////////////////////////////////////////////////////////////////////////////////////// | |
415 | |
416 // llvm.memcpy.i32 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
417 llvm::Function* LLVM_DeclareMemCpy32() |
1 | 418 { |
419 static llvm::Function* _func = 0; | |
420 if (_func == 0) { | |
421 _func = LLVM_DeclareMemIntrinsic("llvm.memcpy.i32", 32); | |
422 } | |
423 return _func; | |
424 } | |
425 | |
426 ////////////////////////////////////////////////////////////////////////////////////////// | |
427 | |
428 // llvm.memcpy.i64 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
429 llvm::Function* LLVM_DeclareMemCpy64() |
1 | 430 { |
431 static llvm::Function* _func = 0; | |
432 if (_func == 0) { | |
433 _func = LLVM_DeclareMemIntrinsic("llvm.memcpy.i64", 64); | |
434 } | |
435 return _func; | |
436 } | |
437 | |
438 ////////////////////////////////////////////////////////////////////////////////////////// | |
439 | |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
440 llvm::Value* LLVM_DtoStructZeroInit(llvm::Value* v) |
1 | 441 { |
442 assert(gIR); | |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
443 uint64_t n = gTargetData->getTypeSize(v->getType()->getContainedType(0)); |
1 | 444 //llvm::Type* sarrty = llvm::PointerType::get(llvm::ArrayType::get(llvm::Type::Int8Ty, n)); |
445 llvm::Type* sarrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
446 | |
447 llvm::Value* sarr = new llvm::BitCastInst(v,sarrty,"tmp",gIR->scopebb()); | |
448 | |
449 llvm::Function* fn = LLVM_DeclareMemSet32(); | |
450 std::vector<llvm::Value*> llargs; | |
451 llargs.resize(4); | |
452 llargs[0] = sarr; | |
453 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false); | |
454 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
455 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
456 | |
457 llvm::Value* ret = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
458 | |
459 return ret; | |
460 } | |
461 | |
462 ////////////////////////////////////////////////////////////////////////////////////////// | |
463 | |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
464 llvm::Value* LLVM_DtoStructCopy(llvm::Value* dst, llvm::Value* src) |
1 | 465 { |
466 assert(dst->getType() == src->getType()); | |
467 assert(gIR); | |
468 | |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
469 uint64_t n = gTargetData->getTypeSize(dst->getType()->getContainedType(0)); |
1 | 470 //llvm::Type* sarrty = llvm::PointerType::get(llvm::ArrayType::get(llvm::Type::Int8Ty, n)); |
471 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
472 | |
473 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); | |
474 llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb()); | |
475 | |
476 llvm::Function* fn = LLVM_DeclareMemCpy32(); | |
477 std::vector<llvm::Value*> llargs; | |
478 llargs.resize(4); | |
479 llargs[0] = dstarr; | |
480 llargs[1] = srcarr; | |
481 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
482 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
483 | |
484 return new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
485 } | |
486 | |
487 ////////////////////////////////////////////////////////////////////////////////////////// | |
40 | 488 llvm::Constant* LLVM_DtoConstStructInitializer(StructInitializer* si) |
1 | 489 { |
490 llvm::StructType* structtype = llvm::cast<llvm::StructType>(si->ad->llvmType); | |
491 size_t n = structtype->getNumElements(); | |
492 | |
493 assert(si->value.dim == si->vars.dim); | |
494 | |
495 std::vector<llvm::Constant*> inits; | |
496 inits.resize(n, NULL); | |
497 for (int i = 0; i < si->value.dim; ++i) | |
498 { | |
499 Initializer* ini = (Initializer*)si->value.data[i]; | |
500 assert(ini); | |
501 | |
502 VarDeclaration* vd = (VarDeclaration*)si->vars.data[i]; | |
40 | 503 Type* vdtype = LLVM_DtoDType(vd->type); |
1 | 504 assert(vd); |
505 Logger::println("vars[%d] = %s", i, vd->toChars()); | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
506 |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
507 std::vector<unsigned> idxs; |
40 | 508 si->ad->offsetToIndex(vdtype, vd->offset, idxs); |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
509 assert(idxs.size() == 1); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
510 unsigned idx = idxs[0]; |
1 | 511 |
512 llvm::Constant* v = 0; | |
513 | |
514 if (ExpInitializer* ex = ini->isExpInitializer()) | |
515 { | |
40 | 516 v = ex->exp->toConstElem(gIR); |
1 | 517 } |
518 else if (StructInitializer* si = ini->isStructInitializer()) | |
519 { | |
40 | 520 v = LLVM_DtoConstStructInitializer(si); |
1 | 521 } |
522 else if (ArrayInitializer* ai = ini->isArrayInitializer()) | |
523 { | |
40 | 524 v = LLVM_DtoConstArrayInitializer(ai); |
1 | 525 } |
526 else if (ini->isVoidInitializer()) | |
527 { | |
528 v = llvm::UndefValue::get(structtype->getElementType(idx)); | |
529 } | |
530 else | |
531 assert(v); | |
532 | |
533 inits[idx] = v; | |
534 } | |
535 | |
536 // fill out nulls | |
537 assert(si->ad->llvmInitZ); | |
538 if (si->ad->llvmInitZ->isNullValue()) | |
539 { | |
540 for (int i = 0; i < n; ++i) | |
541 { | |
542 if (inits[i] == 0) | |
543 { | |
544 inits[i] = llvm::Constant::getNullValue(structtype->getElementType(i)); | |
545 } | |
546 } | |
547 } | |
548 else | |
549 { | |
550 for (int i = 0; i < n; ++i) | |
551 { | |
552 if (inits[i] == 0) | |
553 { | |
554 inits[i] = si->ad->llvmInitZ->getOperand(i); | |
555 } | |
556 } | |
557 } | |
558 | |
559 return llvm::ConstantStruct::get(structtype, inits); | |
560 } | |
561 | |
562 | |
563 | |
564 ////////////////////////////////////////////////////////////////////////////////////////// | |
565 | |
566 llvm::Value* LLVM_DtoNullDelegate(llvm::Value* v) | |
567 { | |
568 assert(gIR); | |
569 d_uns64 n = (global.params.is64bit) ? 16 : 8; | |
570 | |
571 llvm::Type* i8p_ty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
572 | |
573 llvm::Value* arr = new llvm::BitCastInst(v,i8p_ty,"tmp",gIR->scopebb()); | |
574 | |
575 llvm::Function* fn = LLVM_DeclareMemSet32(); | |
576 std::vector<llvm::Value*> llargs; | |
577 llargs.resize(4); | |
578 llargs[0] = arr; | |
579 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false); | |
580 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
581 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
582 | |
583 llvm::Value* ret = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
584 | |
585 return ret; | |
586 } | |
587 | |
588 ////////////////////////////////////////////////////////////////////////////////////////// | |
589 | |
590 llvm::Value* LLVM_DtoDelegateCopy(llvm::Value* dst, llvm::Value* src) | |
591 { | |
592 assert(dst->getType() == src->getType()); | |
593 assert(gIR); | |
594 | |
595 d_uns64 n = (global.params.is64bit) ? 16 : 8; | |
596 | |
597 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
598 | |
599 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); | |
600 llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb()); | |
601 | |
602 llvm::Function* fn = LLVM_DeclareMemCpy32(); | |
603 std::vector<llvm::Value*> llargs; | |
604 llargs.resize(4); | |
605 llargs[0] = dstarr; | |
606 llargs[1] = srcarr; | |
607 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
608 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
609 | |
610 return new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
611 } | |
612 | |
613 | |
614 | |
615 ////////////////////////////////////////////////////////////////////////////////////////// | |
616 | |
617 llvm::GlobalValue::LinkageTypes LLVM_DtoLinkage(PROT prot, uint stc) | |
618 { | |
619 switch(prot) | |
620 { | |
621 case PROTprivate: | |
622 return llvm::GlobalValue::InternalLinkage; | |
623 | |
624 case PROTpublic: | |
625 case PROTpackage: | |
626 case PROTprotected: | |
627 case PROTexport: | |
628 return llvm::GlobalValue::ExternalLinkage; | |
629 | |
630 case PROTundefined: | |
631 case PROTnone: | |
632 assert(0 && "Unsupported linkage type"); | |
633 } | |
634 return llvm::GlobalValue::ExternalLinkage; | |
635 | |
636 /* ExternalLinkage = 0, LinkOnceLinkage, WeakLinkage, AppendingLinkage, | |
637 InternalLinkage, DLLImportLinkage, DLLExportLinkage, ExternalWeakLinkage, | |
638 GhostLinkage */ | |
639 } | |
640 | |
641 ////////////////////////////////////////////////////////////////////////////////////////// | |
642 | |
643 unsigned LLVM_DtoCallingConv(LINK l) | |
644 { | |
645 if (l == LINKc) | |
646 return llvm::CallingConv::C; | |
647 else if (l == LINKd || l == LINKdefault) | |
648 return llvm::CallingConv::Fast; | |
649 else if (l == LINKwindows) | |
650 return llvm::CallingConv::X86_StdCall; | |
651 else | |
652 assert(0 && "Unsupported calling convention"); | |
653 } | |
654 | |
655 ////////////////////////////////////////////////////////////////////////////////////////// | |
656 | |
657 llvm::Value* LLVM_DtoPointedType(llvm::Value* ptr, llvm::Value* val) | |
658 { | |
659 const llvm::Type* ptrTy = ptr->getType()->getContainedType(0); | |
660 const llvm::Type* valTy = val->getType(); | |
661 // ptr points to val's type | |
662 if (ptrTy == valTy) | |
663 { | |
664 return val; | |
665 } | |
666 // ptr is integer pointer | |
667 else if (ptrTy->isInteger()) | |
668 { | |
669 // val is integer | |
670 assert(valTy->isInteger()); | |
671 const llvm::IntegerType* pt = llvm::cast<const llvm::IntegerType>(ptrTy); | |
672 const llvm::IntegerType* vt = llvm::cast<const llvm::IntegerType>(valTy); | |
673 if (pt->getBitWidth() < vt->getBitWidth()) { | |
674 return new llvm::TruncInst(val, pt, "tmp", gIR->scopebb()); | |
675 } | |
676 else | |
677 assert(0); | |
678 } | |
679 // something else unsupported | |
680 else | |
681 { | |
682 Logger::cout() << *ptrTy << '|' << *valTy << '\n'; | |
683 assert(0); | |
684 } | |
685 return 0; | |
686 } | |
687 | |
688 ////////////////////////////////////////////////////////////////////////////////////////// | |
689 | |
690 llvm::Value* LLVM_DtoBoolean(llvm::Value* val) | |
691 { | |
692 const llvm::Type* t = val->getType(); | |
693 if (t->isInteger()) | |
694 { | |
695 if (t == llvm::Type::Int1Ty) | |
696 return val; | |
697 else { | |
698 llvm::Value* zero = llvm::ConstantInt::get(t, 0, false); | |
699 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, val, zero, "tmp", gIR->scopebb()); | |
700 } | |
701 } | |
702 else if (llvm::isa<llvm::PointerType>(t)) { | |
703 const llvm::Type* st = LLVM_DtoSize_t(); | |
704 llvm::Value* ptrasint = new llvm::PtrToIntInst(val,st,"tmp",gIR->scopebb()); | |
705 llvm::Value* zero = llvm::ConstantInt::get(st, 0, false); | |
706 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, ptrasint, zero, "tmp", gIR->scopebb()); | |
707 } | |
708 else | |
709 { | |
710 Logger::cout() << *t << '\n'; | |
711 } | |
712 assert(0); | |
713 return 0; | |
714 } | |
715 | |
716 ////////////////////////////////////////////////////////////////////////////////////////// | |
717 | |
718 const llvm::Type* LLVM_DtoSize_t() | |
719 { | |
720 if (global.params.is64bit) | |
721 return llvm::Type::Int64Ty; | |
722 else | |
723 return llvm::Type::Int32Ty; | |
724 } | |
725 | |
726 ////////////////////////////////////////////////////////////////////////////////////////// | |
727 | |
728 void LLVM_DtoMain() | |
729 { | |
730 // emit main function llvm style | |
731 // int main(int argc, char**argv, char**env); | |
732 | |
733 assert(gIR != 0); | |
734 IRState& ir = *gIR; | |
735 | |
736 assert(ir.emitMain && ir.mainFunc); | |
737 | |
738 // parameter types | |
739 std::vector<const llvm::Type*> pvec; | |
740 pvec.push_back((const llvm::Type*)llvm::Type::Int32Ty); | |
741 const llvm::Type* chPtrType = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
742 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
743 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
744 const llvm::Type* rettype = (const llvm::Type*)llvm::Type::Int32Ty; | |
745 | |
746 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false); | |
747 llvm::Function* func = new llvm::Function(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module); | |
748 | |
749 llvm::BasicBlock* bb = new llvm::BasicBlock("entry",func); | |
750 | |
751 // call static ctors | |
752 llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_ctors"); | |
753 new llvm::CallInst(fn,"",bb); | |
754 | |
755 // call user main function | |
756 llvm::CallInst* call = new llvm::CallInst(ir.mainFunc,"ret",bb); | |
757 call->setCallingConv(ir.mainFunc->getCallingConv()); | |
758 | |
759 // call static dtors | |
760 fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_dtors"); | |
761 new llvm::CallInst(fn,"",bb); | |
762 | |
763 // return | |
764 new llvm::ReturnInst(call,bb); | |
765 } | |
766 | |
767 ////////////////////////////////////////////////////////////////////////////////////////// | |
768 | |
769 void LLVM_DtoCallClassDtors(TypeClass* tc, llvm::Value* instance) | |
770 { | |
771 Array* arr = &tc->sym->dtors; | |
772 for (size_t i=0; i<arr->dim; i++) | |
773 { | |
774 FuncDeclaration* fd = (FuncDeclaration*)arr->data[i]; | |
775 assert(fd->llvmValue); | |
776 new llvm::CallInst(fd->llvmValue, instance, "", gIR->scopebb()); | |
777 } | |
778 } | |
779 | |
780 ////////////////////////////////////////////////////////////////////////////////////////// | |
781 | |
782 void LLVM_DtoInitClass(TypeClass* tc, llvm::Value* dst) | |
783 { | |
784 assert(gIR); | |
785 | |
786 assert(tc->llvmType); | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
787 uint64_t size_t_size = gTargetData->getTypeSize(LLVM_DtoSize_t()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
788 uint64_t n = gTargetData->getTypeSize(tc->llvmType) - size_t_size; |
1 | 789 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
790 // set vtable field |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
791 llvm::Value* vtblvar = LLVM_DtoGEPi(dst,0,0,"tmp",gIR->scopebb()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
792 assert(tc->sym->llvmVtbl); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
793 new llvm::StoreInst(tc->sym->llvmVtbl, vtblvar, gIR->scopebb()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
794 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
795 // copy the static initializer |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
796 if (n > 0) { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
797 assert(tc->llvmInit); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
798 assert(dst->getType() == tc->llvmInit->getType()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
799 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
800 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); |
1 | 801 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
802 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
803 dstarr = LLVM_DtoGEPi(dstarr,size_t_size,"tmp",gIR->scopebb()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
804 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
805 llvm::Value* srcarr = new llvm::BitCastInst(tc->llvmInit,arrty,"tmp",gIR->scopebb()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
806 srcarr = LLVM_DtoGEPi(srcarr,size_t_size,"tmp",gIR->scopebb()); |
1 | 807 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
808 llvm::Function* fn = LLVM_DeclareMemCpy32(); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
809 std::vector<llvm::Value*> llargs; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
810 llargs.resize(4); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
811 llargs[0] = dstarr; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
812 llargs[1] = srcarr; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
813 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
814 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
815 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
816 new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
817 } |
1 | 818 } |
819 | |
820 ////////////////////////////////////////////////////////////////////////////////////////// | |
821 | |
40 | 822 llvm::Constant* LLVM_DtoConstInitializer(Type* type, Initializer* init) |
1 | 823 { |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
824 llvm::Constant* _init = 0; // may return zero |
1 | 825 if (!init) |
826 { | |
40 | 827 Logger::println("const default initializer for %s", type->toChars()); |
828 _init = type->defaultInit()->toConstElem(gIR); | |
1 | 829 } |
830 else if (ExpInitializer* ex = init->isExpInitializer()) | |
831 { | |
40 | 832 Logger::println("const expression initializer"); |
833 _init = ex->exp->toConstElem(gIR); | |
1 | 834 } |
835 else if (StructInitializer* si = init->isStructInitializer()) | |
836 { | |
40 | 837 Logger::println("const struct initializer"); |
838 _init = LLVM_DtoConstStructInitializer(si); | |
1 | 839 } |
840 else if (ArrayInitializer* ai = init->isArrayInitializer()) | |
841 { | |
40 | 842 Logger::println("const array initializer"); |
843 _init = LLVM_DtoConstArrayInitializer(ai); | |
1 | 844 } |
845 else if (init->isVoidInitializer()) | |
846 { | |
40 | 847 Logger::println("const void initializer"); |
1 | 848 const llvm::Type* ty = LLVM_DtoType(type); |
849 _init = llvm::Constant::getNullValue(ty); | |
850 } | |
851 else { | |
40 | 852 Logger::println("unsupported const initializer: %s", init->toChars()); |
853 } | |
854 return _init; | |
855 } | |
856 | |
857 ////////////////////////////////////////////////////////////////////////////////////////// | |
858 | |
859 void LLVM_DtoInitializer(Initializer* init) | |
860 { | |
861 if (ExpInitializer* ex = init->isExpInitializer()) | |
862 { | |
863 Logger::println("expression initializer"); | |
864 elem* e = ex->exp->toElem(gIR); | |
865 delete e; | |
866 } | |
867 else { | |
1 | 868 Logger::println("unsupported initializer: %s", init->toChars()); |
869 } | |
870 } | |
6 | 871 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
872 ////////////////////////////////////////////////////////////////////////////////////////// |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
873 |
6 | 874 llvm::Value* LLVM_DtoGEP(llvm::Value* ptr, llvm::Value* i0, llvm::Value* i1, const std::string& var, llvm::BasicBlock* bb) |
875 { | |
876 std::vector<llvm::Value*> v(2); | |
877 v[0] = i0; | |
878 v[1] = i1; | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
879 Logger::cout() << "DtoGEP: " << *ptr << '\n'; |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
880 return new llvm::GetElementPtrInst(ptr, v.begin(), v.end(), var, bb?bb:gIR->scopebb()); |
6 | 881 } |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
882 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
883 ////////////////////////////////////////////////////////////////////////////////////////// |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
884 |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
885 llvm::Value* LLVM_DtoGEP(llvm::Value* ptr, const std::vector<unsigned>& src, const std::string& var, llvm::BasicBlock* bb) |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
886 { |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
887 size_t n = src.size(); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
888 std::vector<llvm::Value*> dst(n); |
40 | 889 std::ostream& ostr = Logger::cout(); |
890 ostr << "indices for '" << *ptr << "':"; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
891 for (size_t i=0; i<n; ++i) |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
892 { |
40 | 893 ostr << ' ' << i; |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
894 dst[i] = llvm::ConstantInt::get(llvm::Type::Int32Ty, src[i], false); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
895 } |
40 | 896 ostr << '\n'; |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
897 return new llvm::GetElementPtrInst(ptr, dst.begin(), dst.end(), var, bb?bb:gIR->scopebb()); |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
898 } |
9 | 899 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
900 ////////////////////////////////////////////////////////////////////////////////////////// |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
901 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
902 llvm::Value* LLVM_DtoGEPi(llvm::Value* ptr, unsigned i, const std::string& var, llvm::BasicBlock* bb) |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
903 { |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
904 return new llvm::GetElementPtrInst(ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, i, false), var, bb?bb:gIR->scopebb()); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
905 } |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
906 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
907 ////////////////////////////////////////////////////////////////////////////////////////// |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
908 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
909 llvm::Value* LLVM_DtoGEPi(llvm::Value* ptr, unsigned i0, unsigned i1, const std::string& var, llvm::BasicBlock* bb) |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
910 { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
911 std::vector<llvm::Value*> v(2); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
912 v[0] = llvm::ConstantInt::get(llvm::Type::Int32Ty, i0, false); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
913 v[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, i1, false); |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
914 return new llvm::GetElementPtrInst(ptr, v.begin(), v.end(), var, bb?bb:gIR->scopebb()); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
915 } |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
916 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
917 ////////////////////////////////////////////////////////////////////////////////////////// |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
918 |
9 | 919 llvm::Function* LLVM_DtoDeclareFunction(FuncDeclaration* fdecl) |
920 { | |
27
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
921 // mangled name |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
922 char* mangled_name = (fdecl->llvmInternal == LLVMintrinsic) ? fdecl->llvmInternal1 : fdecl->mangle(); |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
923 |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
924 // unit test special handling |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
925 if (fdecl->isUnitTestDeclaration()) |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
926 { |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
927 assert(0 && "no unittests yet"); |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
928 /*const llvm::FunctionType* fnty = llvm::FunctionType::get(llvm::Type::VoidTy, std::vector<const llvm::Type*>(), false); |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
929 // make the function |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
930 llvm::Function* func = gIR->module->getFunction(mangled_name); |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
931 if (func == 0) |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
932 func = new llvm::Function(fnty,llvm::GlobalValue::InternalLinkage,mangled_name,gIR->module); |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
933 func->setCallingConv(llvm::CallingConv::Fast); |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
934 fdecl->llvmValue = func; |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
935 return func; |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
936 */ |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
937 } |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
938 |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
939 // regular function |
40 | 940 TypeFunction* f = (TypeFunction*)LLVM_DtoDType(fdecl->type); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
941 assert(f != 0); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
942 |
9 | 943 if (fdecl->llvmValue != 0) { |
18 | 944 if (!llvm::isa<llvm::Function>(fdecl->llvmValue)) |
945 { | |
946 Logger::cout() << *fdecl->llvmValue << '\n'; | |
947 assert(0); | |
948 } | |
9 | 949 return llvm::cast<llvm::Function>(fdecl->llvmValue); |
950 } | |
951 | |
18 | 952 Logger::print("FuncDeclaration::toObjFile(%s): %s\n", fdecl->needThis()?"this":"static",fdecl->toChars()); |
9 | 953 LOG_SCOPE; |
954 | |
955 if (fdecl->llvmInternal == LLVMintrinsic && fdecl->fbody) { | |
956 error("intrinsics cannot have function bodies"); | |
957 fatal(); | |
958 } | |
959 | |
960 // construct function | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
961 const llvm::FunctionType* functype = (f->llvmType == 0) ? LLVM_DtoFunctionType(fdecl) : llvm::cast<llvm::FunctionType>(f->llvmType); |
9 | 962 |
963 // make the function | |
964 llvm::Function* func = gIR->module->getFunction(mangled_name); | |
965 if (func == 0) { | |
966 func = new llvm::Function(functype,LLVM_DtoLinkage(fdecl->protection, fdecl->storage_class),mangled_name,gIR->module); | |
967 } | |
968 | |
969 if (fdecl->llvmInternal != LLVMintrinsic) | |
970 func->setCallingConv(LLVM_DtoCallingConv(f->linkage)); | |
971 | |
972 fdecl->llvmValue = func; | |
973 f->llvmType = functype; | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
974 assert(llvm::isa<llvm::FunctionType>(f->llvmType)); |
9 | 975 |
976 if (fdecl->isMain()) { | |
977 gIR->mainFunc = func; | |
978 } | |
979 | |
980 // name parameters | |
981 llvm::Function::arg_iterator iarg = func->arg_begin(); | |
982 int k = 0; | |
983 if (f->llvmRetInPtr) { | |
984 iarg->setName("retval"); | |
985 f->llvmRetArg = iarg; | |
986 ++iarg; | |
987 } | |
988 if (f->llvmUsesThis) { | |
989 iarg->setName("this"); | |
990 ++iarg; | |
991 } | |
992 for (; iarg != func->arg_end(); ++iarg) | |
993 { | |
994 Argument* arg = Argument::getNth(f->parameters, k++); | |
995 assert(arg != 0); | |
996 //arg->llvmValue = iarg; | |
997 //printf("identifier: '%s' %p\n", arg->ident->toChars(), arg->ident); | |
998 if (arg->ident != 0) { | |
999 if (arg->vardecl) { | |
1000 arg->vardecl->llvmValue = iarg; | |
1001 } | |
1002 iarg->setName(arg->ident->toChars()); | |
1003 } | |
1004 else { | |
1005 iarg->setName("unnamed"); | |
1006 } | |
1007 } | |
1008 | |
1009 return func; | |
1010 } | |
29
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1011 |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1012 ////////////////////////////////////////////////////////////////////////////////////////// |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1013 |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1014 void LLVM_DtoGiveArgumentStorage(elem* l) |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1015 { |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1016 assert(l->mem == 0); |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1017 assert(l->val); |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1018 assert(llvm::isa<llvm::Argument>(l->val)); |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1019 assert(l->vardecl != 0); |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1020 |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1021 llvm::AllocaInst* allocainst = new llvm::AllocaInst(l->val->getType(), l->val->getName()+"_storage", gIR->topallocapoint()); |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1022 l->mem = allocainst; |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1023 l->vardecl->llvmValue = l->mem; |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1024 } |
34 | 1025 |
1026 ////////////////////////////////////////////////////////////////////////////////////////// | |
1027 | |
1028 llvm::Value* LLVM_DtoRealloc(llvm::Value* ptr, const llvm::Type* ty) | |
1029 { | |
1030 /*size_t sz = gTargetData->getTypeSize(ty); | |
1031 llvm::ConstantInt* n = llvm::ConstantInt::get(LLVM_DtoSize_t(), sz, false); | |
1032 if (ptr == 0) { | |
1033 llvm::PointerType* i8pty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
1034 ptr = llvm::ConstantPointerNull::get(i8pty); | |
1035 } | |
1036 return LLVM_DtoRealloc(ptr, n);*/ | |
1037 return NULL; | |
1038 } | |
1039 | |
1040 ////////////////////////////////////////////////////////////////////////////////////////// | |
1041 | |
1042 llvm::Value* LLVM_DtoRealloc(llvm::Value* ptr, llvm::Value* n) | |
1043 { | |
1044 assert(ptr); | |
1045 assert(n); | |
1046 | |
1047 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_realloc"); | |
1048 assert(fn); | |
1049 | |
1050 llvm::Value* newptr = ptr; | |
1051 | |
1052 llvm::PointerType* i8pty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
1053 if (ptr->getType() != i8pty) { | |
1054 newptr = new llvm::BitCastInst(ptr,i8pty,"tmp",gIR->scopebb()); | |
1055 } | |
1056 | |
1057 std::vector<llvm::Value*> args; | |
1058 args.push_back(newptr); | |
1059 args.push_back(n); | |
1060 llvm::Value* ret = new llvm::CallInst(fn, args.begin(), args.end(), "tmprealloc", gIR->scopebb()); | |
1061 | |
1062 return ret->getType() == ptr->getType() ? ret : new llvm::BitCastInst(ret,ptr->getType(),"tmp",gIR->scopebb()); | |
1063 } | |
1064 | |
1065 ////////////////////////////////////////////////////////////////////////////////////////// | |
1066 | |
1067 void LLVM_DtoAssert(llvm::Value* cond, llvm::Value* loc, llvm::Value* msg) | |
1068 { | |
1069 assert(loc); | |
1070 std::vector<llvm::Value*> llargs; | |
1071 llargs.resize(3); | |
1072 llargs[0] = cond ? LLVM_DtoBoolean(cond) : llvm::ConstantInt::getFalse(); | |
1073 llargs[1] = loc; | |
1074 llargs[2] = msg ? msg : llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty)); | |
1075 | |
1076 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_assert"); | |
1077 assert(fn); | |
1078 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
1079 call->setCallingConv(llvm::CallingConv::C); | |
1080 } | |
1081 | |
40 | 1082 ////////////////////////////////////////////////////////////////////////////////////////// |
34 | 1083 |
40 | 1084 llvm::Value* LLVM_DtoArgument(const llvm::Type* paramtype, Argument* fnarg, Expression* argexp) |
1085 { | |
1086 llvm::Value* retval = 0; | |
1087 | |
1088 bool haslvals = !gIR->lvals.empty(); | |
1089 if (haslvals) | |
1090 gIR->lvals.push_back(NULL); | |
1091 | |
1092 elem* arg = argexp->toElem(gIR); | |
1093 | |
1094 if (haslvals) | |
1095 gIR->lvals.pop_back(); | |
1096 | |
1097 if (arg->inplace) { | |
1098 assert(arg->mem != 0); | |
1099 retval = arg->mem; | |
1100 delete arg; | |
1101 return retval; | |
1102 } | |
1103 | |
1104 Type* realtype = LLVM_DtoDType(argexp->type); | |
1105 TY argty = realtype->ty; | |
1106 if (LLVM_DtoIsPassedByRef(realtype)) { | |
1107 if (!fnarg || !fnarg->llvmCopy) { | |
1108 retval = arg->getValue(); | |
1109 assert(retval != 0); | |
1110 } | |
1111 else { | |
1112 llvm::Value* allocaInst = 0; | |
1113 llvm::BasicBlock* entryblock = &gIR->topfunc()->front(); | |
1114 //const llvm::PointerType* pty = llvm::cast<llvm::PointerType>(arg->mem->getType()); | |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1115 const llvm::Type* realtypell = LLVM_DtoType(realtype); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1116 const llvm::PointerType* pty = llvm::PointerType::get(realtypell); |
40 | 1117 if (argty == Tstruct) { |
1118 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", gIR->topallocapoint()); | |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1119 LLVM_DtoStructCopy(allocaInst,arg->mem); |
40 | 1120 } |
1121 else if (argty == Tdelegate) { | |
1122 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", gIR->topallocapoint()); | |
1123 LLVM_DtoDelegateCopy(allocaInst,arg->mem); | |
1124 } | |
1125 else if (argty == Tarray) { | |
1126 if (arg->type == elem::SLICE) { | |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1127 allocaInst = new llvm::AllocaInst(realtypell, "tmpparam", gIR->topallocapoint()); |
40 | 1128 LLVM_DtoSetArray(allocaInst, arg->arg, arg->mem); |
1129 } | |
1130 else { | |
1131 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", gIR->topallocapoint()); | |
1132 LLVM_DtoArrayAssign(allocaInst,arg->mem); | |
1133 } | |
1134 } | |
1135 else | |
1136 assert(0); | |
1137 | |
1138 assert(allocaInst != 0); | |
1139 retval = allocaInst; | |
1140 } | |
1141 } | |
1142 else if (!fnarg || fnarg->llvmCopy) { | |
1143 Logger::println("regular arg"); | |
1144 assert(arg->type != elem::SLICE); | |
1145 if (arg->mem) Logger::cout() << "mem = " << *arg->mem << '\n'; | |
1146 if (arg->val) Logger::cout() << "val = " << *arg->val << '\n'; | |
1147 if (arg->arg) Logger::cout() << "arg = " << *arg->arg << '\n'; | |
1148 retval = arg->arg ? arg->arg : arg->field ? arg->mem : arg->getValue(); | |
1149 } | |
1150 else { | |
1151 Logger::println("as ptr arg"); | |
1152 retval = arg->mem ? arg->mem : arg->val; | |
1153 if (retval->getType() != paramtype) | |
1154 { | |
1155 assert(retval->getType() == paramtype->getContainedType(0)); | |
1156 LLVM_DtoGiveArgumentStorage(arg); | |
1157 new llvm::StoreInst(retval, arg->mem, gIR->scopebb()); | |
1158 retval = arg->mem; | |
1159 } | |
1160 } | |
1161 | |
1162 delete arg; | |
1163 | |
1164 if (fnarg && retval->getType() != paramtype) { | |
1165 Logger::cout() << "got '" << *retval->getType() << "' expected '" << *paramtype << "'\n"; | |
1166 assert(0 && "parameter type that was actually passed is invalid"); | |
1167 } | |
1168 return retval; | |
1169 } | |
34 | 1170 |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1171 ////////////////////////////////////////////////////////////////////////////////////////// |
34 | 1172 |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1173 llvm::Value* LLVM_DtoNestedVariable(VarDeclaration* vd) |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1174 { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1175 FuncDeclaration* fd = vd->toParent()->isFuncDeclaration(); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1176 assert(fd != NULL); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1177 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1178 IRFunction* fcur = &gIR->func(); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1179 FuncDeclaration* f = fcur->decl; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1180 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1181 // on this stack |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1182 if (fd == f) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1183 return LLVM_DtoGEPi(vd->llvmValue,0,unsigned(vd->llvmNestedIndex),"tmp"); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1184 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1185 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1186 // on a caller stack |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1187 llvm::Value* ptr = f->llvmThisVar; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1188 assert(ptr); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1189 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1190 f = f->toParent()->isFuncDeclaration(); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1191 assert(f); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1192 assert(f->llvmNested); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1193 const llvm::Type* nesttype = f->llvmNested->getType(); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1194 assert(nesttype); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1195 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1196 ptr = gIR->ir->CreateBitCast(ptr, nesttype, "tmp"); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1197 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1198 Logger::cout() << "nested var reference:" << '\n' << *ptr << *nesttype << '\n'; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1199 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1200 while (f) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1201 if (fd == f) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1202 return LLVM_DtoGEPi(ptr,0,vd->llvmNestedIndex,"tmp"); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1203 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1204 else { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1205 ptr = LLVM_DtoGEPi(ptr,0,0,"tmp"); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1206 ptr = gIR->ir->CreateLoad(ptr,"tmp"); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1207 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1208 f = f->toParent()->isFuncDeclaration(); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1209 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1210 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1211 assert(0 && "nested var not found"); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1212 return NULL; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1213 } |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1214 |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1215 ////////////////////////////////////////////////////////////////////////////////////////// |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1216 |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1217 void LLVM_DtoAssign(Type* t, llvm::Value* lhs, llvm::Value* rhs) |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1218 { |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1219 Logger::cout() << "assignment:" << '\n' << *lhs << *rhs << '\n'; |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1220 |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1221 if (t->ty == Tstruct) { |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1222 assert(lhs->getType() == rhs->getType()); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1223 LLVM_DtoStructCopy(lhs,rhs); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1224 } |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1225 else if (t->ty == Tarray) { |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1226 assert(lhs->getType() == rhs->getType()); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1227 LLVM_DtoArrayAssign(lhs,rhs); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1228 } |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1229 else if (t->ty == Tsarray) { |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1230 assert(lhs->getType() == rhs->getType()); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1231 LLVM_DtoStaticArrayCopy(lhs,rhs); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1232 } |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1233 else if (t->ty == Tdelegate) { |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1234 assert(lhs->getType() == rhs->getType()); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1235 LLVM_DtoDelegateCopy(lhs,rhs); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1236 } |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1237 else { |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1238 assert(lhs->getType()->getContainedType(0) == rhs->getType()); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1239 gIR->ir->CreateStore(rhs, lhs); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1240 } |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1241 } |