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