Mercurial > projects > ldc
annotate gen/tollvm.c @ 8:5e69b77a5c51 trunk
[svn r12] fixed accessing aggregate fields of aggregates
removed some useless branches for successive scopes ala {}{}{}
author | lindquist |
---|---|
date | Thu, 27 Sep 2007 06:03:06 +0200 |
parents | 35d93ce68cf4 |
children | dafae18f9c08 |
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()); | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
463 |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
464 std::vector<unsigned> idxs; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
465 si->ad->offsetToIndex(vd->offset, idxs); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
466 assert(idxs.size() == 1); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
467 unsigned idx = idxs[0]; |
1 | 468 |
469 llvm::Constant* v = 0; | |
470 | |
471 if (ExpInitializer* ex = ini->isExpInitializer()) | |
472 { | |
473 elem* e = ex->exp->toElem(gIR); | |
474 v = llvm::cast<llvm::Constant>(e->val); | |
475 delete e; | |
476 } | |
477 else if (StructInitializer* si = ini->isStructInitializer()) | |
478 { | |
479 v = LLVM_DtoStructInitializer(si); | |
480 } | |
481 else if (ArrayInitializer* ai = ini->isArrayInitializer()) | |
482 { | |
483 v = LLVM_DtoArrayInitializer(ai); | |
484 } | |
485 else if (ini->isVoidInitializer()) | |
486 { | |
487 v = llvm::UndefValue::get(structtype->getElementType(idx)); | |
488 } | |
489 else | |
490 assert(v); | |
491 | |
492 inits[idx] = v; | |
493 } | |
494 | |
495 // fill out nulls | |
496 assert(si->ad->llvmInitZ); | |
497 if (si->ad->llvmInitZ->isNullValue()) | |
498 { | |
499 for (int i = 0; i < n; ++i) | |
500 { | |
501 if (inits[i] == 0) | |
502 { | |
503 inits[i] = llvm::Constant::getNullValue(structtype->getElementType(i)); | |
504 } | |
505 } | |
506 } | |
507 else | |
508 { | |
509 for (int i = 0; i < n; ++i) | |
510 { | |
511 if (inits[i] == 0) | |
512 { | |
513 inits[i] = si->ad->llvmInitZ->getOperand(i); | |
514 } | |
515 } | |
516 } | |
517 | |
518 return llvm::ConstantStruct::get(structtype, inits); | |
519 } | |
520 | |
521 | |
522 | |
523 ////////////////////////////////////////////////////////////////////////////////////////// | |
524 | |
525 llvm::Value* LLVM_DtoNullDelegate(llvm::Value* v) | |
526 { | |
527 assert(gIR); | |
528 d_uns64 n = (global.params.is64bit) ? 16 : 8; | |
529 | |
530 llvm::Type* i8p_ty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
531 | |
532 llvm::Value* arr = new llvm::BitCastInst(v,i8p_ty,"tmp",gIR->scopebb()); | |
533 | |
534 llvm::Function* fn = LLVM_DeclareMemSet32(); | |
535 std::vector<llvm::Value*> llargs; | |
536 llargs.resize(4); | |
537 llargs[0] = arr; | |
538 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false); | |
539 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
540 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
541 | |
542 llvm::Value* ret = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
543 | |
544 return ret; | |
545 } | |
546 | |
547 ////////////////////////////////////////////////////////////////////////////////////////// | |
548 | |
549 llvm::Value* LLVM_DtoDelegateCopy(llvm::Value* dst, llvm::Value* src) | |
550 { | |
551 assert(dst->getType() == src->getType()); | |
552 assert(gIR); | |
553 | |
554 d_uns64 n = (global.params.is64bit) ? 16 : 8; | |
555 | |
556 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
557 | |
558 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); | |
559 llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb()); | |
560 | |
561 llvm::Function* fn = LLVM_DeclareMemCpy32(); | |
562 std::vector<llvm::Value*> llargs; | |
563 llargs.resize(4); | |
564 llargs[0] = dstarr; | |
565 llargs[1] = srcarr; | |
566 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
567 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
568 | |
569 return new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
570 } | |
571 | |
572 | |
573 | |
574 ////////////////////////////////////////////////////////////////////////////////////////// | |
575 | |
576 llvm::GlobalValue::LinkageTypes LLVM_DtoLinkage(PROT prot, uint stc) | |
577 { | |
578 switch(prot) | |
579 { | |
580 case PROTprivate: | |
581 return llvm::GlobalValue::InternalLinkage; | |
582 | |
583 case PROTpublic: | |
584 case PROTpackage: | |
585 case PROTprotected: | |
586 case PROTexport: | |
587 return llvm::GlobalValue::ExternalLinkage; | |
588 | |
589 case PROTundefined: | |
590 case PROTnone: | |
591 assert(0 && "Unsupported linkage type"); | |
592 } | |
593 return llvm::GlobalValue::ExternalLinkage; | |
594 | |
595 /* ExternalLinkage = 0, LinkOnceLinkage, WeakLinkage, AppendingLinkage, | |
596 InternalLinkage, DLLImportLinkage, DLLExportLinkage, ExternalWeakLinkage, | |
597 GhostLinkage */ | |
598 } | |
599 | |
600 ////////////////////////////////////////////////////////////////////////////////////////// | |
601 | |
602 unsigned LLVM_DtoCallingConv(LINK l) | |
603 { | |
604 if (l == LINKc) | |
605 return llvm::CallingConv::C; | |
606 else if (l == LINKd || l == LINKdefault) | |
607 return llvm::CallingConv::Fast; | |
608 else if (l == LINKwindows) | |
609 return llvm::CallingConv::X86_StdCall; | |
610 else | |
611 assert(0 && "Unsupported calling convention"); | |
612 } | |
613 | |
614 ////////////////////////////////////////////////////////////////////////////////////////// | |
615 | |
616 llvm::Value* LLVM_DtoPointedType(llvm::Value* ptr, llvm::Value* val) | |
617 { | |
618 const llvm::Type* ptrTy = ptr->getType()->getContainedType(0); | |
619 const llvm::Type* valTy = val->getType(); | |
620 // ptr points to val's type | |
621 if (ptrTy == valTy) | |
622 { | |
623 return val; | |
624 } | |
625 // ptr is integer pointer | |
626 else if (ptrTy->isInteger()) | |
627 { | |
628 // val is integer | |
629 assert(valTy->isInteger()); | |
630 const llvm::IntegerType* pt = llvm::cast<const llvm::IntegerType>(ptrTy); | |
631 const llvm::IntegerType* vt = llvm::cast<const llvm::IntegerType>(valTy); | |
632 if (pt->getBitWidth() < vt->getBitWidth()) { | |
633 return new llvm::TruncInst(val, pt, "tmp", gIR->scopebb()); | |
634 } | |
635 else | |
636 assert(0); | |
637 } | |
638 // something else unsupported | |
639 else | |
640 { | |
641 Logger::cout() << *ptrTy << '|' << *valTy << '\n'; | |
642 assert(0); | |
643 } | |
644 return 0; | |
645 } | |
646 | |
647 ////////////////////////////////////////////////////////////////////////////////////////// | |
648 | |
649 llvm::Value* LLVM_DtoBoolean(llvm::Value* val) | |
650 { | |
651 const llvm::Type* t = val->getType(); | |
652 if (t->isInteger()) | |
653 { | |
654 if (t == llvm::Type::Int1Ty) | |
655 return val; | |
656 else { | |
657 llvm::Value* zero = llvm::ConstantInt::get(t, 0, false); | |
658 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, val, zero, "tmp", gIR->scopebb()); | |
659 } | |
660 } | |
661 else if (llvm::isa<llvm::PointerType>(t)) { | |
662 const llvm::Type* st = LLVM_DtoSize_t(); | |
663 llvm::Value* ptrasint = new llvm::PtrToIntInst(val,st,"tmp",gIR->scopebb()); | |
664 llvm::Value* zero = llvm::ConstantInt::get(st, 0, false); | |
665 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, ptrasint, zero, "tmp", gIR->scopebb()); | |
666 } | |
667 else | |
668 { | |
669 Logger::cout() << *t << '\n'; | |
670 } | |
671 assert(0); | |
672 return 0; | |
673 } | |
674 | |
675 ////////////////////////////////////////////////////////////////////////////////////////// | |
676 | |
677 const llvm::Type* LLVM_DtoSize_t() | |
678 { | |
679 if (global.params.is64bit) | |
680 return llvm::Type::Int64Ty; | |
681 else | |
682 return llvm::Type::Int32Ty; | |
683 } | |
684 | |
685 ////////////////////////////////////////////////////////////////////////////////////////// | |
686 | |
687 void LLVM_DtoMain() | |
688 { | |
689 // emit main function llvm style | |
690 // int main(int argc, char**argv, char**env); | |
691 | |
692 assert(gIR != 0); | |
693 IRState& ir = *gIR; | |
694 | |
695 assert(ir.emitMain && ir.mainFunc); | |
696 | |
697 // parameter types | |
698 std::vector<const llvm::Type*> pvec; | |
699 pvec.push_back((const llvm::Type*)llvm::Type::Int32Ty); | |
700 const llvm::Type* chPtrType = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
701 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
702 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
703 const llvm::Type* rettype = (const llvm::Type*)llvm::Type::Int32Ty; | |
704 | |
705 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false); | |
706 llvm::Function* func = new llvm::Function(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module); | |
707 | |
708 llvm::BasicBlock* bb = new llvm::BasicBlock("entry",func); | |
709 | |
710 // call static ctors | |
711 llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_ctors"); | |
712 new llvm::CallInst(fn,"",bb); | |
713 | |
714 // call user main function | |
715 llvm::CallInst* call = new llvm::CallInst(ir.mainFunc,"ret",bb); | |
716 call->setCallingConv(ir.mainFunc->getCallingConv()); | |
717 | |
718 // call static dtors | |
719 fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_dtors"); | |
720 new llvm::CallInst(fn,"",bb); | |
721 | |
722 // return | |
723 new llvm::ReturnInst(call,bb); | |
724 | |
725 /* | |
726 // return value type | |
727 const llvm::Type* rettype; | |
728 Type* rt = f->next; | |
729 if (rt) { | |
730 rettype = LLVM_DtoType(rt); | |
731 } | |
732 else { | |
733 assert(0); | |
734 } | |
735 | |
736 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, paramvec, false); | |
737 */ | |
738 } | |
739 | |
740 ////////////////////////////////////////////////////////////////////////////////////////// | |
741 | |
742 void LLVM_DtoCallClassDtors(TypeClass* tc, llvm::Value* instance) | |
743 { | |
744 Array* arr = &tc->sym->dtors; | |
745 for (size_t i=0; i<arr->dim; i++) | |
746 { | |
747 FuncDeclaration* fd = (FuncDeclaration*)arr->data[i]; | |
748 assert(fd->llvmValue); | |
749 new llvm::CallInst(fd->llvmValue, instance, "", gIR->scopebb()); | |
750 } | |
751 } | |
752 | |
753 ////////////////////////////////////////////////////////////////////////////////////////// | |
754 | |
755 void LLVM_DtoInitClass(TypeClass* tc, llvm::Value* dst) | |
756 { | |
757 assert(tc->llvmInit); | |
758 assert(dst->getType() == tc->llvmInit->getType()); | |
759 assert(gIR); | |
760 | |
761 assert(tc->llvmType); | |
762 uint64_t n = gTargetData->getTypeSize(tc->llvmType); | |
763 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
764 | |
765 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); | |
766 llvm::Value* srcarr = new llvm::BitCastInst(tc->llvmInit,arrty,"tmp",gIR->scopebb()); | |
767 | |
768 llvm::Function* fn = LLVM_DeclareMemCpy32(); | |
769 std::vector<llvm::Value*> llargs; | |
770 llargs.resize(4); | |
771 llargs[0] = dstarr; | |
772 llargs[1] = srcarr; | |
773 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
774 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
775 | |
776 new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
777 } | |
778 | |
779 ////////////////////////////////////////////////////////////////////////////////////////// | |
780 | |
781 llvm::Constant* LLVM_DtoInitializer(Type* type, Initializer* init) | |
782 { | |
783 llvm::Constant* _init = 0; | |
784 if (!init) | |
785 { | |
786 elem* e = type->defaultInit()->toElem(gIR); | |
787 if (!e->inplace && !e->isNull()) { | |
788 _init = llvm::cast<llvm::Constant>(e->getValue()); | |
789 } | |
790 delete e; | |
791 } | |
792 else if (ExpInitializer* ex = init->isExpInitializer()) | |
793 { | |
794 elem* e = ex->exp->toElem(gIR); | |
795 if (!e->inplace && !e->isNull()) { | |
796 _init = llvm::cast<llvm::Constant>(e->getValue()); | |
797 } | |
798 delete e; | |
799 } | |
800 else if (StructInitializer* si = init->isStructInitializer()) | |
801 { | |
802 _init = LLVM_DtoStructInitializer(si); | |
803 } | |
804 else if (ArrayInitializer* ai = init->isArrayInitializer()) | |
805 { | |
806 _init = LLVM_DtoArrayInitializer(ai); | |
807 } | |
808 else if (init->isVoidInitializer()) | |
809 { | |
810 const llvm::Type* ty = LLVM_DtoType(type); | |
811 _init = llvm::Constant::getNullValue(ty); | |
812 } | |
813 else { | |
814 Logger::println("unsupported initializer: %s", init->toChars()); | |
815 } | |
816 return _init; | |
817 } | |
6 | 818 |
819 llvm::Value* LLVM_DtoGEP(llvm::Value* ptr, llvm::Value* i0, llvm::Value* i1, const std::string& var, llvm::BasicBlock* bb) | |
820 { | |
821 std::vector<llvm::Value*> v(2); | |
822 v[0] = i0; | |
823 v[1] = i1; | |
824 return new llvm::GetElementPtrInst(ptr, v.begin(), v.end(), var, bb); | |
825 } | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
826 |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
827 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
|
828 { |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
829 size_t n = src.size(); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
830 std::vector<llvm::Value*> dst(n); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
831 Logger::cout() << "indices:"; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
832 for (size_t i=0; i<n; ++i) |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
833 { |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
834 Logger::cout() << ' ' << i; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
835 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
|
836 } |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
837 Logger::cout() << '\n'; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
838 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
|
839 } |