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