Mercurial > projects > ldc
annotate gen/tollvm.c @ 6:35d93ce68cf4 trunk
[svn r10] Updated for LLVM rev. 20070913
Applied fixes from wilsonk on the forum
Some tweaks to work with gc 7.0
Fixed aggregate members of aggregates
Fixed cyclic/recursive class declarations
Other minor tweaks
author | lindquist |
---|---|
date | Wed, 26 Sep 2007 19:05:18 +0200 |
parents | e116aa1488e6 |
children | 5e69b77a5c51 |
rev | line source |
---|---|
1 | 1 #include <iostream> |
2 | |
3 #include "llvm/Constants.h" | |
4 #include "llvm/Type.h" | |
5 #include "llvm/DerivedTypes.h" | |
6 #include "llvm/GlobalValue.h" | |
7 #include "llvm/Instructions.h" | |
8 #include "llvm/CallingConv.h" | |
9 | |
10 #include "mtype.h" | |
11 #include "dsymbol.h" | |
12 #include "aggregate.h" | |
13 #include "declaration.h" | |
14 #include "init.h" | |
15 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
16 #include "gen/tollvm.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
17 #include "gen/irstate.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
18 #include "gen/logger.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
19 #include "gen/runtime.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
20 #include "gen/elem.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
21 #include "gen/arrays.h" |
1 | 22 |
23 const llvm::Type* LLVM_DtoType(Type* t) | |
24 { | |
25 assert(t); | |
26 switch (t->ty) | |
27 { | |
28 // integers | |
29 case Tint8: | |
30 case Tuns8: | |
31 case Tchar: | |
32 return (const llvm::Type*)llvm::Type::Int8Ty; | |
33 case Tint16: | |
34 case Tuns16: | |
35 case Twchar: | |
36 return (const llvm::Type*)llvm::Type::Int16Ty; | |
37 case Tint32: | |
38 case Tuns32: | |
39 case Tdchar: | |
40 return (const llvm::Type*)llvm::Type::Int32Ty; | |
41 case Tint64: | |
42 case Tuns64: | |
43 return (const llvm::Type*)llvm::Type::Int64Ty; | |
44 | |
45 case Tbool: | |
46 return (const llvm::Type*)llvm::ConstantInt::getTrue()->getType(); | |
47 | |
48 // floats | |
49 case Tfloat32: | |
50 return llvm::Type::FloatTy; | |
51 case Tfloat64: | |
52 case Tfloat80: | |
53 return llvm::Type::DoubleTy; | |
54 | |
55 // pointers | |
56 case Tpointer: { | |
57 assert(t->next); | |
58 if (t->next->ty == Tvoid) | |
59 return (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
60 else | |
61 return (const llvm::Type*)llvm::PointerType::get(LLVM_DtoType(t->next)); | |
62 } | |
63 | |
64 // arrays | |
65 case Tarray: | |
66 return LLVM_DtoArrayType(t); | |
67 case Tsarray: | |
68 return LLVM_DtoStaticArrayType(t); | |
69 | |
70 // void | |
71 case Tvoid: | |
72 return llvm::Type::VoidTy; | |
73 | |
74 // aggregates | |
75 case Tstruct: { | |
76 if (t->llvmType == 0) | |
77 { | |
78 // recursive or cyclic declaration | |
79 if (!gIR->structs.empty()) | |
80 { | |
81 IRStruct* found = 0; | |
82 for (IRState::StructVector::iterator i=gIR->structs.begin(); i!=gIR->structs.end(); ++i) | |
83 { | |
84 if (t == i->type) | |
85 { | |
86 return i->recty.get(); | |
87 } | |
88 } | |
89 } | |
90 | |
91 // forward declaration | |
92 TypeStruct* ts = (TypeStruct*)t; | |
93 assert(ts->sym); | |
94 ts->sym->toObjFile(); | |
95 } | |
96 return t->llvmType; | |
97 } | |
98 | |
99 case Tclass: { | |
100 if (t->llvmType == 0) | |
101 { | |
6 | 102 // recursive or cyclic declaration |
103 if (!gIR->structs.empty()) | |
104 { | |
105 IRStruct* found = 0; | |
106 for (IRState::StructVector::iterator i=gIR->structs.begin(); i!=gIR->structs.end(); ++i) | |
107 { | |
108 if (t == i->type) | |
109 { | |
110 return llvm::PointerType::get(i->recty.get()); | |
111 } | |
112 } | |
113 } | |
114 | |
115 // forward declaration | |
1 | 116 TypeClass* tc = (TypeClass*)t; |
117 assert(tc->sym); | |
6 | 118 tc->sym->toObjFile(); |
1 | 119 } |
120 return llvm::PointerType::get(t->llvmType); | |
121 } | |
122 | |
123 // functions | |
124 case Tfunction: | |
125 { | |
126 if (t->llvmType == 0) { | |
127 return LLVM_DtoFunctionType(t); | |
128 } | |
129 else { | |
130 return t->llvmType; | |
131 } | |
132 } | |
6 | 133 |
1 | 134 // delegates |
135 case Tdelegate: | |
136 { | |
137 if (t->llvmType == 0) { | |
138 return LLVM_DtoDelegateType(t); | |
139 } | |
140 else { | |
141 return t->llvmType; | |
142 } | |
143 } | |
144 | |
145 // typedefs | |
146 case Ttypedef: | |
147 { | |
148 Type* bt = t->toBasetype(); | |
149 assert(bt); | |
150 return LLVM_DtoType(bt); | |
151 } | |
152 | |
153 default: | |
154 printf("trying to convert unknown type with value %d\n", t->ty); | |
155 assert(0); | |
156 } | |
157 return 0; | |
158 } | |
159 | |
160 ////////////////////////////////////////////////////////////////////////////////////////// | |
161 | |
162 llvm::FunctionType* LLVM_DtoFunctionType(Type* t, const llvm::Type* thisparam) | |
163 { | |
164 TypeFunction* f = (TypeFunction*)t; | |
165 | |
166 // parameter types | |
167 const llvm::Type* rettype; | |
168 std::vector<const llvm::Type*> paramvec; | |
169 | |
170 TY retty = f->next->ty; | |
171 | |
172 if (retty == Tstruct || retty == Tdelegate || retty == Tarray) { | |
173 rettype = llvm::PointerType::get(LLVM_DtoType(f->next)); | |
174 paramvec.push_back(rettype); | |
175 rettype = llvm::Type::VoidTy; | |
176 } | |
177 else { | |
178 Type* rt = f->next; | |
179 if (rt) | |
180 rettype = LLVM_DtoType(rt); | |
181 else | |
182 assert(0); | |
183 } | |
184 | |
185 if (thisparam) { | |
186 paramvec.push_back(thisparam); | |
187 } | |
188 | |
189 size_t n = Argument::dim(f->parameters); | |
190 for (int i=0; i < n; ++i) { | |
191 Argument* arg = Argument::getNth(f->parameters, i); | |
192 // ensure scalar | |
193 Type* argT = arg->type; | |
194 assert(argT); | |
195 paramvec.push_back(LLVM_DtoType(argT)); | |
196 } | |
197 | |
198 Logger::cout() << "Return type: " << *rettype << '\n'; | |
199 | |
200 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, paramvec, f->varargs); | |
201 return functype; | |
202 } | |
203 | |
204 ////////////////////////////////////////////////////////////////////////////////////////// | |
205 | |
206 llvm::FunctionType* LLVM_DtoFunctionType(FuncDeclaration* fdecl) | |
207 { | |
208 TypeFunction* f = (TypeFunction*)fdecl->type; | |
209 assert(f != 0); | |
210 | |
211 // has already been pulled in by a reference to ( | |
212 if (f->llvmType != 0) { | |
213 return llvm::cast<llvm::FunctionType>(f->llvmType); | |
214 } | |
215 | |
216 // return value type | |
217 const llvm::Type* rettype; | |
218 const llvm::Type* actualRettype; | |
219 Type* rt = f->next; | |
220 bool retinptr = false; | |
221 bool usesthis = false; | |
222 | |
223 if (fdecl->isMain()) { | |
224 rettype = llvm::Type::Int32Ty; | |
225 actualRettype = rettype; | |
226 } | |
227 else if (rt) { | |
228 if (rt->ty == Tstruct || rt->ty == Tdelegate || rt->ty == Tarray) { | |
229 rettype = llvm::PointerType::get(LLVM_DtoType(rt)); | |
230 actualRettype = llvm::Type::VoidTy; | |
231 f->llvmRetInPtr = retinptr = true; | |
232 } | |
233 else { | |
234 rettype = LLVM_DtoType(rt); | |
235 actualRettype = rettype; | |
236 } | |
237 } | |
238 else { | |
239 assert(0); | |
240 } | |
241 | |
242 // parameter types | |
243 std::vector<const llvm::Type*> paramvec; | |
244 | |
245 if (retinptr) { | |
246 Logger::print("returning through pointer parameter\n"); | |
247 paramvec.push_back(rettype); | |
248 } | |
249 | |
250 if (fdecl->needThis() && fdecl->vthis) { | |
251 Logger::print("this is: %s\n", fdecl->vthis->type->toChars()); | |
252 paramvec.push_back(LLVM_DtoType(fdecl->vthis->type)); | |
253 usesthis = true; | |
254 } | |
255 | |
256 size_t n = Argument::dim(f->parameters); | |
257 for (int i=0; i < n; ++i) { | |
258 Argument* arg = Argument::getNth(f->parameters, i); | |
259 // ensure scalar | |
260 Type* argT = arg->type; | |
261 assert(argT); | |
262 | |
263 if ((arg->storageClass & STCref) || (arg->storageClass & STCout)) { | |
264 //assert(arg->vardecl); | |
265 //arg->vardecl->refparam = true; | |
266 } | |
267 else | |
268 arg->llvmCopy = true; | |
269 | |
270 const llvm::Type* at = LLVM_DtoType(argT); | |
271 if (llvm::isa<llvm::StructType>(at)) { | |
272 Logger::println("struct param"); | |
273 paramvec.push_back(llvm::PointerType::get(at)); | |
274 } | |
275 else if (llvm::isa<llvm::ArrayType>(at)) { | |
276 Logger::println("sarray param"); | |
277 assert(argT->ty == Tsarray); | |
278 //paramvec.push_back(llvm::PointerType::get(at->getContainedType(0))); | |
279 paramvec.push_back(llvm::PointerType::get(at)); | |
280 } | |
281 else { | |
282 if (!arg->llvmCopy) { | |
283 Logger::println("ref param"); | |
284 at = llvm::PointerType::get(at); | |
285 } | |
286 else { | |
287 Logger::println("in param"); | |
288 } | |
289 paramvec.push_back(at); | |
290 } | |
291 } | |
292 | |
293 // construct function type | |
294 bool isvararg = f->varargs; | |
295 llvm::FunctionType* functype = llvm::FunctionType::get(actualRettype, paramvec, isvararg); | |
296 | |
297 f->llvmType = functype; | |
298 return functype; | |
299 } | |
300 | |
301 ////////////////////////////////////////////////////////////////////////////////////////// | |
302 | |
303 llvm::StructType* LLVM_DtoDelegateType(Type* t) | |
304 { | |
305 const llvm::Type* i8ptr = llvm::PointerType::get(llvm::Type::Int8Ty); | |
306 const llvm::Type* func = LLVM_DtoFunctionType(t->next, i8ptr); | |
307 const llvm::Type* funcptr = llvm::PointerType::get(func); | |
308 | |
309 std::vector<const llvm::Type*> types; | |
310 types.push_back(i8ptr); | |
311 types.push_back(funcptr); | |
312 return llvm::StructType::get(types); | |
313 } | |
314 | |
315 ////////////////////////////////////////////////////////////////////////////////////////// | |
316 | |
317 llvm::Type* LLVM_DtoStructType(Type* t) | |
318 { | |
319 assert(0); | |
320 std::vector<const llvm::Type*> types; | |
321 return llvm::StructType::get(types); | |
322 } | |
323 | |
324 | |
325 ////////////////////////////////////////////////////////////////////////////////////////// | |
326 | |
327 static llvm::Function* LLVM_DeclareMemIntrinsic(const char* name, int bits, bool set=false) | |
328 { | |
329 assert(bits == 32 || bits == 64); | |
330 const llvm::Type* int8ty = (const llvm::Type*)llvm::Type::Int8Ty; | |
331 const llvm::Type* int32ty = (const llvm::Type*)llvm::Type::Int32Ty; | |
332 const llvm::Type* int64ty = (const llvm::Type*)llvm::Type::Int64Ty; | |
333 const llvm::Type* int8ptrty = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
334 const llvm::Type* voidty = (const llvm::Type*)llvm::Type::VoidTy; | |
335 | |
336 assert(gIR); | |
337 assert(gIR->module); | |
338 | |
339 // parameter types | |
340 std::vector<const llvm::Type*> pvec; | |
341 pvec.push_back(int8ptrty); | |
342 pvec.push_back(set?int8ty:int8ptrty); | |
343 pvec.push_back(bits==32?int32ty:int64ty); | |
344 pvec.push_back(int32ty); | |
345 llvm::FunctionType* functype = llvm::FunctionType::get(voidty, pvec, false); | |
346 return new llvm::Function(functype, llvm::GlobalValue::ExternalLinkage, name, gIR->module); | |
347 } | |
348 | |
349 ////////////////////////////////////////////////////////////////////////////////////////// | |
350 | |
351 // llvm.memset.i32 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
352 llvm::Function* LLVM_DeclareMemSet32() |
1 | 353 { |
354 static llvm::Function* _func = 0; | |
355 if (_func == 0) { | |
356 _func = LLVM_DeclareMemIntrinsic("llvm.memset.i32", 32, true); | |
357 } | |
358 return _func; | |
359 } | |
360 | |
361 ////////////////////////////////////////////////////////////////////////////////////////// | |
362 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
363 llvm::Function* LLVM_DeclareMemSet64() |
1 | 364 { |
365 static llvm::Function* _func = 0; | |
366 if (_func == 0) { | |
367 _func = LLVM_DeclareMemIntrinsic("llvm.memset.i64", 64, true); | |
368 } | |
369 return _func; | |
370 } | |
371 | |
372 ////////////////////////////////////////////////////////////////////////////////////////// | |
373 | |
374 // llvm.memcpy.i32 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
375 llvm::Function* LLVM_DeclareMemCpy32() |
1 | 376 { |
377 static llvm::Function* _func = 0; | |
378 if (_func == 0) { | |
379 _func = LLVM_DeclareMemIntrinsic("llvm.memcpy.i32", 32); | |
380 } | |
381 return _func; | |
382 } | |
383 | |
384 ////////////////////////////////////////////////////////////////////////////////////////// | |
385 | |
386 // llvm.memcpy.i64 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
387 llvm::Function* LLVM_DeclareMemCpy64() |
1 | 388 { |
389 static llvm::Function* _func = 0; | |
390 if (_func == 0) { | |
391 _func = LLVM_DeclareMemIntrinsic("llvm.memcpy.i64", 64); | |
392 } | |
393 return _func; | |
394 } | |
395 | |
396 ////////////////////////////////////////////////////////////////////////////////////////// | |
397 | |
398 llvm::Value* LLVM_DtoStructZeroInit(TypeStruct* t, llvm::Value* v) | |
399 { | |
400 assert(gIR); | |
401 uint64_t n = gTargetData->getTypeSize(t->llvmType); | |
402 //llvm::Type* sarrty = llvm::PointerType::get(llvm::ArrayType::get(llvm::Type::Int8Ty, n)); | |
403 llvm::Type* sarrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
404 | |
405 llvm::Value* sarr = new llvm::BitCastInst(v,sarrty,"tmp",gIR->scopebb()); | |
406 | |
407 llvm::Function* fn = LLVM_DeclareMemSet32(); | |
408 std::vector<llvm::Value*> llargs; | |
409 llargs.resize(4); | |
410 llargs[0] = sarr; | |
411 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false); | |
412 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
413 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
414 | |
415 llvm::Value* ret = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
416 | |
417 return ret; | |
418 } | |
419 | |
420 ////////////////////////////////////////////////////////////////////////////////////////// | |
421 | |
422 llvm::Value* LLVM_DtoStructCopy(TypeStruct* t, llvm::Value* dst, llvm::Value* src) | |
423 { | |
424 assert(dst->getType() == src->getType()); | |
425 assert(gIR); | |
426 | |
427 uint64_t n = gTargetData->getTypeSize(t->llvmType); | |
428 //llvm::Type* sarrty = llvm::PointerType::get(llvm::ArrayType::get(llvm::Type::Int8Ty, n)); | |
429 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
430 | |
431 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); | |
432 llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb()); | |
433 | |
434 llvm::Function* fn = LLVM_DeclareMemCpy32(); | |
435 std::vector<llvm::Value*> llargs; | |
436 llargs.resize(4); | |
437 llargs[0] = dstarr; | |
438 llargs[1] = srcarr; | |
439 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
440 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
441 | |
442 return new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
443 } | |
444 | |
445 ////////////////////////////////////////////////////////////////////////////////////////// | |
446 llvm::Constant* LLVM_DtoStructInitializer(StructInitializer* si) | |
447 { | |
448 llvm::StructType* structtype = llvm::cast<llvm::StructType>(si->ad->llvmType); | |
449 size_t n = structtype->getNumElements(); | |
450 | |
451 assert(si->value.dim == si->vars.dim); | |
452 | |
453 std::vector<llvm::Constant*> inits; | |
454 inits.resize(n, NULL); | |
455 for (int i = 0; i < si->value.dim; ++i) | |
456 { | |
457 Initializer* ini = (Initializer*)si->value.data[i]; | |
458 assert(ini); | |
459 | |
460 VarDeclaration* vd = (VarDeclaration*)si->vars.data[i]; | |
461 assert(vd); | |
462 Logger::println("vars[%d] = %s", i, vd->toChars()); | |
463 unsigned idx = si->ad->offsetToIndex(vd->offset); | |
464 | |
465 llvm::Constant* v = 0; | |
466 | |
467 if (ExpInitializer* ex = ini->isExpInitializer()) | |
468 { | |
469 elem* e = ex->exp->toElem(gIR); | |
470 v = llvm::cast<llvm::Constant>(e->val); | |
471 delete e; | |
472 } | |
473 else if (StructInitializer* si = ini->isStructInitializer()) | |
474 { | |
475 v = LLVM_DtoStructInitializer(si); | |
476 } | |
477 else if (ArrayInitializer* ai = ini->isArrayInitializer()) | |
478 { | |
479 v = LLVM_DtoArrayInitializer(ai); | |
480 } | |
481 else if (ini->isVoidInitializer()) | |
482 { | |
483 v = llvm::UndefValue::get(structtype->getElementType(idx)); | |
484 } | |
485 else | |
486 assert(v); | |
487 | |
488 inits[idx] = v; | |
489 } | |
490 | |
491 // fill out nulls | |
492 assert(si->ad->llvmInitZ); | |
493 if (si->ad->llvmInitZ->isNullValue()) | |
494 { | |
495 for (int i = 0; i < n; ++i) | |
496 { | |
497 if (inits[i] == 0) | |
498 { | |
499 inits[i] = llvm::Constant::getNullValue(structtype->getElementType(i)); | |
500 } | |
501 } | |
502 } | |
503 else | |
504 { | |
505 for (int i = 0; i < n; ++i) | |
506 { | |
507 if (inits[i] == 0) | |
508 { | |
509 inits[i] = si->ad->llvmInitZ->getOperand(i); | |
510 } | |
511 } | |
512 } | |
513 | |
514 return llvm::ConstantStruct::get(structtype, inits); | |
515 } | |
516 | |
517 | |
518 | |
519 ////////////////////////////////////////////////////////////////////////////////////////// | |
520 | |
521 llvm::Value* LLVM_DtoNullDelegate(llvm::Value* v) | |
522 { | |
523 assert(gIR); | |
524 d_uns64 n = (global.params.is64bit) ? 16 : 8; | |
525 | |
526 llvm::Type* i8p_ty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
527 | |
528 llvm::Value* arr = new llvm::BitCastInst(v,i8p_ty,"tmp",gIR->scopebb()); | |
529 | |
530 llvm::Function* fn = LLVM_DeclareMemSet32(); | |
531 std::vector<llvm::Value*> llargs; | |
532 llargs.resize(4); | |
533 llargs[0] = arr; | |
534 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false); | |
535 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
536 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
537 | |
538 llvm::Value* ret = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
539 | |
540 return ret; | |
541 } | |
542 | |
543 ////////////////////////////////////////////////////////////////////////////////////////// | |
544 | |
545 llvm::Value* LLVM_DtoDelegateCopy(llvm::Value* dst, llvm::Value* src) | |
546 { | |
547 assert(dst->getType() == src->getType()); | |
548 assert(gIR); | |
549 | |
550 d_uns64 n = (global.params.is64bit) ? 16 : 8; | |
551 | |
552 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
553 | |
554 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); | |
555 llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb()); | |
556 | |
557 llvm::Function* fn = LLVM_DeclareMemCpy32(); | |
558 std::vector<llvm::Value*> llargs; | |
559 llargs.resize(4); | |
560 llargs[0] = dstarr; | |
561 llargs[1] = srcarr; | |
562 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
563 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
564 | |
565 return new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
566 } | |
567 | |
568 | |
569 | |
570 ////////////////////////////////////////////////////////////////////////////////////////// | |
571 | |
572 llvm::GlobalValue::LinkageTypes LLVM_DtoLinkage(PROT prot, uint stc) | |
573 { | |
574 switch(prot) | |
575 { | |
576 case PROTprivate: | |
577 return llvm::GlobalValue::InternalLinkage; | |
578 | |
579 case PROTpublic: | |
580 case PROTpackage: | |
581 case PROTprotected: | |
582 case PROTexport: | |
583 return llvm::GlobalValue::ExternalLinkage; | |
584 | |
585 case PROTundefined: | |
586 case PROTnone: | |
587 assert(0 && "Unsupported linkage type"); | |
588 } | |
589 return llvm::GlobalValue::ExternalLinkage; | |
590 | |
591 /* ExternalLinkage = 0, LinkOnceLinkage, WeakLinkage, AppendingLinkage, | |
592 InternalLinkage, DLLImportLinkage, DLLExportLinkage, ExternalWeakLinkage, | |
593 GhostLinkage */ | |
594 } | |
595 | |
596 ////////////////////////////////////////////////////////////////////////////////////////// | |
597 | |
598 unsigned LLVM_DtoCallingConv(LINK l) | |
599 { | |
600 if (l == LINKc) | |
601 return llvm::CallingConv::C; | |
602 else if (l == LINKd || l == LINKdefault) | |
603 return llvm::CallingConv::Fast; | |
604 else if (l == LINKwindows) | |
605 return llvm::CallingConv::X86_StdCall; | |
606 else | |
607 assert(0 && "Unsupported calling convention"); | |
608 } | |
609 | |
610 ////////////////////////////////////////////////////////////////////////////////////////// | |
611 | |
612 llvm::Value* LLVM_DtoPointedType(llvm::Value* ptr, llvm::Value* val) | |
613 { | |
614 const llvm::Type* ptrTy = ptr->getType()->getContainedType(0); | |
615 const llvm::Type* valTy = val->getType(); | |
616 // ptr points to val's type | |
617 if (ptrTy == valTy) | |
618 { | |
619 return val; | |
620 } | |
621 // ptr is integer pointer | |
622 else if (ptrTy->isInteger()) | |
623 { | |
624 // val is integer | |
625 assert(valTy->isInteger()); | |
626 const llvm::IntegerType* pt = llvm::cast<const llvm::IntegerType>(ptrTy); | |
627 const llvm::IntegerType* vt = llvm::cast<const llvm::IntegerType>(valTy); | |
628 if (pt->getBitWidth() < vt->getBitWidth()) { | |
629 return new llvm::TruncInst(val, pt, "tmp", gIR->scopebb()); | |
630 } | |
631 else | |
632 assert(0); | |
633 } | |
634 // something else unsupported | |
635 else | |
636 { | |
637 Logger::cout() << *ptrTy << '|' << *valTy << '\n'; | |
638 assert(0); | |
639 } | |
640 return 0; | |
641 } | |
642 | |
643 ////////////////////////////////////////////////////////////////////////////////////////// | |
644 | |
645 llvm::Value* LLVM_DtoBoolean(llvm::Value* val) | |
646 { | |
647 const llvm::Type* t = val->getType(); | |
648 if (t->isInteger()) | |
649 { | |
650 if (t == llvm::Type::Int1Ty) | |
651 return val; | |
652 else { | |
653 llvm::Value* zero = llvm::ConstantInt::get(t, 0, false); | |
654 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, val, zero, "tmp", gIR->scopebb()); | |
655 } | |
656 } | |
657 else if (llvm::isa<llvm::PointerType>(t)) { | |
658 const llvm::Type* st = LLVM_DtoSize_t(); | |
659 llvm::Value* ptrasint = new llvm::PtrToIntInst(val,st,"tmp",gIR->scopebb()); | |
660 llvm::Value* zero = llvm::ConstantInt::get(st, 0, false); | |
661 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, ptrasint, zero, "tmp", gIR->scopebb()); | |
662 } | |
663 else | |
664 { | |
665 Logger::cout() << *t << '\n'; | |
666 } | |
667 assert(0); | |
668 return 0; | |
669 } | |
670 | |
671 ////////////////////////////////////////////////////////////////////////////////////////// | |
672 | |
673 const llvm::Type* LLVM_DtoSize_t() | |
674 { | |
675 if (global.params.is64bit) | |
676 return llvm::Type::Int64Ty; | |
677 else | |
678 return llvm::Type::Int32Ty; | |
679 } | |
680 | |
681 ////////////////////////////////////////////////////////////////////////////////////////// | |
682 | |
683 void LLVM_DtoMain() | |
684 { | |
685 // emit main function llvm style | |
686 // int main(int argc, char**argv, char**env); | |
687 | |
688 assert(gIR != 0); | |
689 IRState& ir = *gIR; | |
690 | |
691 assert(ir.emitMain && ir.mainFunc); | |
692 | |
693 // parameter types | |
694 std::vector<const llvm::Type*> pvec; | |
695 pvec.push_back((const llvm::Type*)llvm::Type::Int32Ty); | |
696 const llvm::Type* chPtrType = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
697 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
698 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
699 const llvm::Type* rettype = (const llvm::Type*)llvm::Type::Int32Ty; | |
700 | |
701 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false); | |
702 llvm::Function* func = new llvm::Function(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module); | |
703 | |
704 llvm::BasicBlock* bb = new llvm::BasicBlock("entry",func); | |
705 | |
706 // call static ctors | |
707 llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_ctors"); | |
708 new llvm::CallInst(fn,"",bb); | |
709 | |
710 // call user main function | |
711 llvm::CallInst* call = new llvm::CallInst(ir.mainFunc,"ret",bb); | |
712 call->setCallingConv(ir.mainFunc->getCallingConv()); | |
713 | |
714 // call static dtors | |
715 fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_dtors"); | |
716 new llvm::CallInst(fn,"",bb); | |
717 | |
718 // return | |
719 new llvm::ReturnInst(call,bb); | |
720 | |
721 /* | |
722 // return value type | |
723 const llvm::Type* rettype; | |
724 Type* rt = f->next; | |
725 if (rt) { | |
726 rettype = LLVM_DtoType(rt); | |
727 } | |
728 else { | |
729 assert(0); | |
730 } | |
731 | |
732 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, paramvec, false); | |
733 */ | |
734 } | |
735 | |
736 ////////////////////////////////////////////////////////////////////////////////////////// | |
737 | |
738 void LLVM_DtoCallClassDtors(TypeClass* tc, llvm::Value* instance) | |
739 { | |
740 Array* arr = &tc->sym->dtors; | |
741 for (size_t i=0; i<arr->dim; i++) | |
742 { | |
743 FuncDeclaration* fd = (FuncDeclaration*)arr->data[i]; | |
744 assert(fd->llvmValue); | |
745 new llvm::CallInst(fd->llvmValue, instance, "", gIR->scopebb()); | |
746 } | |
747 } | |
748 | |
749 ////////////////////////////////////////////////////////////////////////////////////////// | |
750 | |
751 void LLVM_DtoInitClass(TypeClass* tc, llvm::Value* dst) | |
752 { | |
753 assert(tc->llvmInit); | |
754 assert(dst->getType() == tc->llvmInit->getType()); | |
755 assert(gIR); | |
756 | |
757 assert(tc->llvmType); | |
758 uint64_t n = gTargetData->getTypeSize(tc->llvmType); | |
759 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
760 | |
761 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); | |
762 llvm::Value* srcarr = new llvm::BitCastInst(tc->llvmInit,arrty,"tmp",gIR->scopebb()); | |
763 | |
764 llvm::Function* fn = LLVM_DeclareMemCpy32(); | |
765 std::vector<llvm::Value*> llargs; | |
766 llargs.resize(4); | |
767 llargs[0] = dstarr; | |
768 llargs[1] = srcarr; | |
769 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
770 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
771 | |
772 new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
773 } | |
774 | |
775 ////////////////////////////////////////////////////////////////////////////////////////// | |
776 | |
777 llvm::Constant* LLVM_DtoInitializer(Type* type, Initializer* init) | |
778 { | |
779 llvm::Constant* _init = 0; | |
780 if (!init) | |
781 { | |
782 elem* e = type->defaultInit()->toElem(gIR); | |
783 if (!e->inplace && !e->isNull()) { | |
784 _init = llvm::cast<llvm::Constant>(e->getValue()); | |
785 } | |
786 delete e; | |
787 } | |
788 else if (ExpInitializer* ex = init->isExpInitializer()) | |
789 { | |
790 elem* e = ex->exp->toElem(gIR); | |
791 if (!e->inplace && !e->isNull()) { | |
792 _init = llvm::cast<llvm::Constant>(e->getValue()); | |
793 } | |
794 delete e; | |
795 } | |
796 else if (StructInitializer* si = init->isStructInitializer()) | |
797 { | |
798 _init = LLVM_DtoStructInitializer(si); | |
799 } | |
800 else if (ArrayInitializer* ai = init->isArrayInitializer()) | |
801 { | |
802 _init = LLVM_DtoArrayInitializer(ai); | |
803 } | |
804 else if (init->isVoidInitializer()) | |
805 { | |
806 const llvm::Type* ty = LLVM_DtoType(type); | |
807 _init = llvm::Constant::getNullValue(ty); | |
808 } | |
809 else { | |
810 Logger::println("unsupported initializer: %s", init->toChars()); | |
811 } | |
812 return _init; | |
813 } | |
6 | 814 |
815 llvm::Value* LLVM_DtoGEP(llvm::Value* ptr, llvm::Value* i0, llvm::Value* i1, const std::string& var, llvm::BasicBlock* bb) | |
816 { | |
817 std::vector<llvm::Value*> v(2); | |
818 v[0] = i0; | |
819 v[1] = i1; | |
820 return new llvm::GetElementPtrInst(ptr, v.begin(), v.end(), var, bb); | |
821 } |