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