Mercurial > projects > ldc
annotate gen/tollvm.c @ 11:d3ee9efe20e2 trunk
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
* Now 50/51 tests compile.
* Added a simple runalltests.d scripts that should be run with 'gdmd -run runalltests.d' - LLVMDC will not compile it yet.
author | lindquist |
---|---|
date | Tue, 02 Oct 2007 05:10:18 +0200 |
parents | dafae18f9c08 |
children | c05ef76f1c20 |
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 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
162 const llvm::FunctionType* LLVM_DtoFunctionType(Type* t, const llvm::Type* thisparam) |
1 | 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 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
206 const llvm::FunctionType* LLVM_DtoFunctionType(FuncDeclaration* fdecl) |
1 | 207 { |
208 TypeFunction* f = (TypeFunction*)fdecl->type; | |
209 assert(f != 0); | |
210 | |
9 | 211 // type has already been resolved |
1 | 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 | |
9 | 250 if (fdecl->needThis()) { |
251 if (AggregateDeclaration* ad = fdecl->isMember()) { | |
252 Logger::print("isMember = this is: %s\n", ad->type->toChars()); | |
253 const llvm::Type* thisty = LLVM_DtoType(ad->type); | |
254 if (llvm::isa<llvm::StructType>(thisty)) | |
255 thisty = llvm::PointerType::get(thisty); | |
256 paramvec.push_back(thisty); | |
257 usesthis = true; | |
258 } | |
259 else | |
260 assert(0); | |
1 | 261 } |
262 | |
263 size_t n = Argument::dim(f->parameters); | |
264 for (int i=0; i < n; ++i) { | |
265 Argument* arg = Argument::getNth(f->parameters, i); | |
266 // ensure scalar | |
267 Type* argT = arg->type; | |
268 assert(argT); | |
269 | |
270 if ((arg->storageClass & STCref) || (arg->storageClass & STCout)) { | |
271 //assert(arg->vardecl); | |
272 //arg->vardecl->refparam = true; | |
273 } | |
274 else | |
275 arg->llvmCopy = true; | |
276 | |
277 const llvm::Type* at = LLVM_DtoType(argT); | |
278 if (llvm::isa<llvm::StructType>(at)) { | |
279 Logger::println("struct param"); | |
280 paramvec.push_back(llvm::PointerType::get(at)); | |
281 } | |
282 else if (llvm::isa<llvm::ArrayType>(at)) { | |
283 Logger::println("sarray param"); | |
284 assert(argT->ty == Tsarray); | |
285 //paramvec.push_back(llvm::PointerType::get(at->getContainedType(0))); | |
286 paramvec.push_back(llvm::PointerType::get(at)); | |
287 } | |
288 else { | |
289 if (!arg->llvmCopy) { | |
290 Logger::println("ref param"); | |
291 at = llvm::PointerType::get(at); | |
292 } | |
293 else { | |
294 Logger::println("in param"); | |
295 } | |
296 paramvec.push_back(at); | |
297 } | |
298 } | |
299 | |
300 // construct function type | |
301 bool isvararg = f->varargs; | |
302 llvm::FunctionType* functype = llvm::FunctionType::get(actualRettype, paramvec, isvararg); | |
303 | |
304 f->llvmType = functype; | |
9 | 305 f->llvmRetInPtr = retinptr; |
306 f->llvmUsesThis = usesthis; | |
1 | 307 return functype; |
308 } | |
309 | |
310 ////////////////////////////////////////////////////////////////////////////////////////// | |
311 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
312 const llvm::StructType* LLVM_DtoDelegateType(Type* t) |
1 | 313 { |
314 const llvm::Type* i8ptr = llvm::PointerType::get(llvm::Type::Int8Ty); | |
315 const llvm::Type* func = LLVM_DtoFunctionType(t->next, i8ptr); | |
316 const llvm::Type* funcptr = llvm::PointerType::get(func); | |
317 | |
318 std::vector<const llvm::Type*> types; | |
319 types.push_back(i8ptr); | |
320 types.push_back(funcptr); | |
321 return llvm::StructType::get(types); | |
322 } | |
323 | |
324 ////////////////////////////////////////////////////////////////////////////////////////// | |
325 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
326 const llvm::Type* LLVM_DtoStructType(Type* t) |
1 | 327 { |
328 assert(0); | |
329 std::vector<const llvm::Type*> types; | |
330 return llvm::StructType::get(types); | |
331 } | |
332 | |
333 | |
334 ////////////////////////////////////////////////////////////////////////////////////////// | |
335 | |
336 static llvm::Function* LLVM_DeclareMemIntrinsic(const char* name, int bits, bool set=false) | |
337 { | |
338 assert(bits == 32 || bits == 64); | |
339 const llvm::Type* int8ty = (const llvm::Type*)llvm::Type::Int8Ty; | |
340 const llvm::Type* int32ty = (const llvm::Type*)llvm::Type::Int32Ty; | |
341 const llvm::Type* int64ty = (const llvm::Type*)llvm::Type::Int64Ty; | |
342 const llvm::Type* int8ptrty = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
343 const llvm::Type* voidty = (const llvm::Type*)llvm::Type::VoidTy; | |
344 | |
345 assert(gIR); | |
346 assert(gIR->module); | |
347 | |
348 // parameter types | |
349 std::vector<const llvm::Type*> pvec; | |
350 pvec.push_back(int8ptrty); | |
351 pvec.push_back(set?int8ty:int8ptrty); | |
352 pvec.push_back(bits==32?int32ty:int64ty); | |
353 pvec.push_back(int32ty); | |
354 llvm::FunctionType* functype = llvm::FunctionType::get(voidty, pvec, false); | |
355 return new llvm::Function(functype, llvm::GlobalValue::ExternalLinkage, name, gIR->module); | |
356 } | |
357 | |
358 ////////////////////////////////////////////////////////////////////////////////////////// | |
359 | |
360 // llvm.memset.i32 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
361 llvm::Function* LLVM_DeclareMemSet32() |
1 | 362 { |
363 static llvm::Function* _func = 0; | |
364 if (_func == 0) { | |
365 _func = LLVM_DeclareMemIntrinsic("llvm.memset.i32", 32, true); | |
366 } | |
367 return _func; | |
368 } | |
369 | |
370 ////////////////////////////////////////////////////////////////////////////////////////// | |
371 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
372 llvm::Function* LLVM_DeclareMemSet64() |
1 | 373 { |
374 static llvm::Function* _func = 0; | |
375 if (_func == 0) { | |
376 _func = LLVM_DeclareMemIntrinsic("llvm.memset.i64", 64, true); | |
377 } | |
378 return _func; | |
379 } | |
380 | |
381 ////////////////////////////////////////////////////////////////////////////////////////// | |
382 | |
383 // llvm.memcpy.i32 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
384 llvm::Function* LLVM_DeclareMemCpy32() |
1 | 385 { |
386 static llvm::Function* _func = 0; | |
387 if (_func == 0) { | |
388 _func = LLVM_DeclareMemIntrinsic("llvm.memcpy.i32", 32); | |
389 } | |
390 return _func; | |
391 } | |
392 | |
393 ////////////////////////////////////////////////////////////////////////////////////////// | |
394 | |
395 // llvm.memcpy.i64 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
396 llvm::Function* LLVM_DeclareMemCpy64() |
1 | 397 { |
398 static llvm::Function* _func = 0; | |
399 if (_func == 0) { | |
400 _func = LLVM_DeclareMemIntrinsic("llvm.memcpy.i64", 64); | |
401 } | |
402 return _func; | |
403 } | |
404 | |
405 ////////////////////////////////////////////////////////////////////////////////////////// | |
406 | |
407 llvm::Value* LLVM_DtoStructZeroInit(TypeStruct* t, llvm::Value* v) | |
408 { | |
409 assert(gIR); | |
410 uint64_t n = gTargetData->getTypeSize(t->llvmType); | |
411 //llvm::Type* sarrty = llvm::PointerType::get(llvm::ArrayType::get(llvm::Type::Int8Ty, n)); | |
412 llvm::Type* sarrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
413 | |
414 llvm::Value* sarr = new llvm::BitCastInst(v,sarrty,"tmp",gIR->scopebb()); | |
415 | |
416 llvm::Function* fn = LLVM_DeclareMemSet32(); | |
417 std::vector<llvm::Value*> llargs; | |
418 llargs.resize(4); | |
419 llargs[0] = sarr; | |
420 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false); | |
421 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
422 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
423 | |
424 llvm::Value* ret = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
425 | |
426 return ret; | |
427 } | |
428 | |
429 ////////////////////////////////////////////////////////////////////////////////////////// | |
430 | |
431 llvm::Value* LLVM_DtoStructCopy(TypeStruct* t, llvm::Value* dst, llvm::Value* src) | |
432 { | |
433 assert(dst->getType() == src->getType()); | |
434 assert(gIR); | |
435 | |
436 uint64_t n = gTargetData->getTypeSize(t->llvmType); | |
437 //llvm::Type* sarrty = llvm::PointerType::get(llvm::ArrayType::get(llvm::Type::Int8Ty, n)); | |
438 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
439 | |
440 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); | |
441 llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb()); | |
442 | |
443 llvm::Function* fn = LLVM_DeclareMemCpy32(); | |
444 std::vector<llvm::Value*> llargs; | |
445 llargs.resize(4); | |
446 llargs[0] = dstarr; | |
447 llargs[1] = srcarr; | |
448 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
449 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
450 | |
451 return new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
452 } | |
453 | |
454 ////////////////////////////////////////////////////////////////////////////////////////// | |
455 llvm::Constant* LLVM_DtoStructInitializer(StructInitializer* si) | |
456 { | |
457 llvm::StructType* structtype = llvm::cast<llvm::StructType>(si->ad->llvmType); | |
458 size_t n = structtype->getNumElements(); | |
459 | |
460 assert(si->value.dim == si->vars.dim); | |
461 | |
462 std::vector<llvm::Constant*> inits; | |
463 inits.resize(n, NULL); | |
464 for (int i = 0; i < si->value.dim; ++i) | |
465 { | |
466 Initializer* ini = (Initializer*)si->value.data[i]; | |
467 assert(ini); | |
468 | |
469 VarDeclaration* vd = (VarDeclaration*)si->vars.data[i]; | |
470 assert(vd); | |
471 Logger::println("vars[%d] = %s", i, vd->toChars()); | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
472 |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
473 std::vector<unsigned> idxs; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
474 si->ad->offsetToIndex(vd->offset, idxs); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
475 assert(idxs.size() == 1); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
476 unsigned idx = idxs[0]; |
1 | 477 |
478 llvm::Constant* v = 0; | |
479 | |
480 if (ExpInitializer* ex = ini->isExpInitializer()) | |
481 { | |
482 elem* e = ex->exp->toElem(gIR); | |
483 v = llvm::cast<llvm::Constant>(e->val); | |
484 delete e; | |
485 } | |
486 else if (StructInitializer* si = ini->isStructInitializer()) | |
487 { | |
488 v = LLVM_DtoStructInitializer(si); | |
489 } | |
490 else if (ArrayInitializer* ai = ini->isArrayInitializer()) | |
491 { | |
492 v = LLVM_DtoArrayInitializer(ai); | |
493 } | |
494 else if (ini->isVoidInitializer()) | |
495 { | |
496 v = llvm::UndefValue::get(structtype->getElementType(idx)); | |
497 } | |
498 else | |
499 assert(v); | |
500 | |
501 inits[idx] = v; | |
502 } | |
503 | |
504 // fill out nulls | |
505 assert(si->ad->llvmInitZ); | |
506 if (si->ad->llvmInitZ->isNullValue()) | |
507 { | |
508 for (int i = 0; i < n; ++i) | |
509 { | |
510 if (inits[i] == 0) | |
511 { | |
512 inits[i] = llvm::Constant::getNullValue(structtype->getElementType(i)); | |
513 } | |
514 } | |
515 } | |
516 else | |
517 { | |
518 for (int i = 0; i < n; ++i) | |
519 { | |
520 if (inits[i] == 0) | |
521 { | |
522 inits[i] = si->ad->llvmInitZ->getOperand(i); | |
523 } | |
524 } | |
525 } | |
526 | |
527 return llvm::ConstantStruct::get(structtype, inits); | |
528 } | |
529 | |
530 | |
531 | |
532 ////////////////////////////////////////////////////////////////////////////////////////// | |
533 | |
534 llvm::Value* LLVM_DtoNullDelegate(llvm::Value* v) | |
535 { | |
536 assert(gIR); | |
537 d_uns64 n = (global.params.is64bit) ? 16 : 8; | |
538 | |
539 llvm::Type* i8p_ty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
540 | |
541 llvm::Value* arr = new llvm::BitCastInst(v,i8p_ty,"tmp",gIR->scopebb()); | |
542 | |
543 llvm::Function* fn = LLVM_DeclareMemSet32(); | |
544 std::vector<llvm::Value*> llargs; | |
545 llargs.resize(4); | |
546 llargs[0] = arr; | |
547 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false); | |
548 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
549 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
550 | |
551 llvm::Value* ret = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
552 | |
553 return ret; | |
554 } | |
555 | |
556 ////////////////////////////////////////////////////////////////////////////////////////// | |
557 | |
558 llvm::Value* LLVM_DtoDelegateCopy(llvm::Value* dst, llvm::Value* src) | |
559 { | |
560 assert(dst->getType() == src->getType()); | |
561 assert(gIR); | |
562 | |
563 d_uns64 n = (global.params.is64bit) ? 16 : 8; | |
564 | |
565 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
566 | |
567 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); | |
568 llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb()); | |
569 | |
570 llvm::Function* fn = LLVM_DeclareMemCpy32(); | |
571 std::vector<llvm::Value*> llargs; | |
572 llargs.resize(4); | |
573 llargs[0] = dstarr; | |
574 llargs[1] = srcarr; | |
575 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
576 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
577 | |
578 return new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
579 } | |
580 | |
581 | |
582 | |
583 ////////////////////////////////////////////////////////////////////////////////////////// | |
584 | |
585 llvm::GlobalValue::LinkageTypes LLVM_DtoLinkage(PROT prot, uint stc) | |
586 { | |
587 switch(prot) | |
588 { | |
589 case PROTprivate: | |
590 return llvm::GlobalValue::InternalLinkage; | |
591 | |
592 case PROTpublic: | |
593 case PROTpackage: | |
594 case PROTprotected: | |
595 case PROTexport: | |
596 return llvm::GlobalValue::ExternalLinkage; | |
597 | |
598 case PROTundefined: | |
599 case PROTnone: | |
600 assert(0 && "Unsupported linkage type"); | |
601 } | |
602 return llvm::GlobalValue::ExternalLinkage; | |
603 | |
604 /* ExternalLinkage = 0, LinkOnceLinkage, WeakLinkage, AppendingLinkage, | |
605 InternalLinkage, DLLImportLinkage, DLLExportLinkage, ExternalWeakLinkage, | |
606 GhostLinkage */ | |
607 } | |
608 | |
609 ////////////////////////////////////////////////////////////////////////////////////////// | |
610 | |
611 unsigned LLVM_DtoCallingConv(LINK l) | |
612 { | |
613 if (l == LINKc) | |
614 return llvm::CallingConv::C; | |
615 else if (l == LINKd || l == LINKdefault) | |
616 return llvm::CallingConv::Fast; | |
617 else if (l == LINKwindows) | |
618 return llvm::CallingConv::X86_StdCall; | |
619 else | |
620 assert(0 && "Unsupported calling convention"); | |
621 } | |
622 | |
623 ////////////////////////////////////////////////////////////////////////////////////////// | |
624 | |
625 llvm::Value* LLVM_DtoPointedType(llvm::Value* ptr, llvm::Value* val) | |
626 { | |
627 const llvm::Type* ptrTy = ptr->getType()->getContainedType(0); | |
628 const llvm::Type* valTy = val->getType(); | |
629 // ptr points to val's type | |
630 if (ptrTy == valTy) | |
631 { | |
632 return val; | |
633 } | |
634 // ptr is integer pointer | |
635 else if (ptrTy->isInteger()) | |
636 { | |
637 // val is integer | |
638 assert(valTy->isInteger()); | |
639 const llvm::IntegerType* pt = llvm::cast<const llvm::IntegerType>(ptrTy); | |
640 const llvm::IntegerType* vt = llvm::cast<const llvm::IntegerType>(valTy); | |
641 if (pt->getBitWidth() < vt->getBitWidth()) { | |
642 return new llvm::TruncInst(val, pt, "tmp", gIR->scopebb()); | |
643 } | |
644 else | |
645 assert(0); | |
646 } | |
647 // something else unsupported | |
648 else | |
649 { | |
650 Logger::cout() << *ptrTy << '|' << *valTy << '\n'; | |
651 assert(0); | |
652 } | |
653 return 0; | |
654 } | |
655 | |
656 ////////////////////////////////////////////////////////////////////////////////////////// | |
657 | |
658 llvm::Value* LLVM_DtoBoolean(llvm::Value* val) | |
659 { | |
660 const llvm::Type* t = val->getType(); | |
661 if (t->isInteger()) | |
662 { | |
663 if (t == llvm::Type::Int1Ty) | |
664 return val; | |
665 else { | |
666 llvm::Value* zero = llvm::ConstantInt::get(t, 0, false); | |
667 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, val, zero, "tmp", gIR->scopebb()); | |
668 } | |
669 } | |
670 else if (llvm::isa<llvm::PointerType>(t)) { | |
671 const llvm::Type* st = LLVM_DtoSize_t(); | |
672 llvm::Value* ptrasint = new llvm::PtrToIntInst(val,st,"tmp",gIR->scopebb()); | |
673 llvm::Value* zero = llvm::ConstantInt::get(st, 0, false); | |
674 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, ptrasint, zero, "tmp", gIR->scopebb()); | |
675 } | |
676 else | |
677 { | |
678 Logger::cout() << *t << '\n'; | |
679 } | |
680 assert(0); | |
681 return 0; | |
682 } | |
683 | |
684 ////////////////////////////////////////////////////////////////////////////////////////// | |
685 | |
686 const llvm::Type* LLVM_DtoSize_t() | |
687 { | |
688 if (global.params.is64bit) | |
689 return llvm::Type::Int64Ty; | |
690 else | |
691 return llvm::Type::Int32Ty; | |
692 } | |
693 | |
694 ////////////////////////////////////////////////////////////////////////////////////////// | |
695 | |
696 void LLVM_DtoMain() | |
697 { | |
698 // emit main function llvm style | |
699 // int main(int argc, char**argv, char**env); | |
700 | |
701 assert(gIR != 0); | |
702 IRState& ir = *gIR; | |
703 | |
704 assert(ir.emitMain && ir.mainFunc); | |
705 | |
706 // parameter types | |
707 std::vector<const llvm::Type*> pvec; | |
708 pvec.push_back((const llvm::Type*)llvm::Type::Int32Ty); | |
709 const llvm::Type* chPtrType = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
710 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
711 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
712 const llvm::Type* rettype = (const llvm::Type*)llvm::Type::Int32Ty; | |
713 | |
714 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false); | |
715 llvm::Function* func = new llvm::Function(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module); | |
716 | |
717 llvm::BasicBlock* bb = new llvm::BasicBlock("entry",func); | |
718 | |
719 // call static ctors | |
720 llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_ctors"); | |
721 new llvm::CallInst(fn,"",bb); | |
722 | |
723 // call user main function | |
724 llvm::CallInst* call = new llvm::CallInst(ir.mainFunc,"ret",bb); | |
725 call->setCallingConv(ir.mainFunc->getCallingConv()); | |
726 | |
727 // call static dtors | |
728 fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_dtors"); | |
729 new llvm::CallInst(fn,"",bb); | |
730 | |
731 // return | |
732 new llvm::ReturnInst(call,bb); | |
733 | |
734 /* | |
735 // return value type | |
736 const llvm::Type* rettype; | |
737 Type* rt = f->next; | |
738 if (rt) { | |
739 rettype = LLVM_DtoType(rt); | |
740 } | |
741 else { | |
742 assert(0); | |
743 } | |
744 | |
745 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, paramvec, false); | |
746 */ | |
747 } | |
748 | |
749 ////////////////////////////////////////////////////////////////////////////////////////// | |
750 | |
751 void LLVM_DtoCallClassDtors(TypeClass* tc, llvm::Value* instance) | |
752 { | |
753 Array* arr = &tc->sym->dtors; | |
754 for (size_t i=0; i<arr->dim; i++) | |
755 { | |
756 FuncDeclaration* fd = (FuncDeclaration*)arr->data[i]; | |
757 assert(fd->llvmValue); | |
758 new llvm::CallInst(fd->llvmValue, instance, "", gIR->scopebb()); | |
759 } | |
760 } | |
761 | |
762 ////////////////////////////////////////////////////////////////////////////////////////// | |
763 | |
764 void LLVM_DtoInitClass(TypeClass* tc, llvm::Value* dst) | |
765 { | |
766 assert(gIR); | |
767 | |
768 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
|
769 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
|
770 uint64_t n = gTargetData->getTypeSize(tc->llvmType) - size_t_size; |
1 | 771 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
772 // 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
|
773 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
|
774 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
|
775 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
|
776 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
777 // 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
|
778 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
|
779 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
|
780 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
|
781 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
782 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); |
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 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
|
785 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
|
786 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
787 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
|
788 srcarr = LLVM_DtoGEPi(srcarr,size_t_size,"tmp",gIR->scopebb()); |
1 | 789 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
790 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
|
791 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
|
792 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
|
793 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
|
794 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
|
795 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
|
796 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
|
797 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
798 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
|
799 } |
1 | 800 } |
801 | |
802 ////////////////////////////////////////////////////////////////////////////////////////// | |
803 | |
804 llvm::Constant* LLVM_DtoInitializer(Type* type, Initializer* init) | |
805 { | |
806 llvm::Constant* _init = 0; | |
807 if (!init) | |
808 { | |
809 elem* e = type->defaultInit()->toElem(gIR); | |
810 if (!e->inplace && !e->isNull()) { | |
811 _init = llvm::cast<llvm::Constant>(e->getValue()); | |
812 } | |
813 delete e; | |
814 } | |
815 else if (ExpInitializer* ex = init->isExpInitializer()) | |
816 { | |
817 elem* e = ex->exp->toElem(gIR); | |
818 if (!e->inplace && !e->isNull()) { | |
819 _init = llvm::cast<llvm::Constant>(e->getValue()); | |
820 } | |
821 delete e; | |
822 } | |
823 else if (StructInitializer* si = init->isStructInitializer()) | |
824 { | |
825 _init = LLVM_DtoStructInitializer(si); | |
826 } | |
827 else if (ArrayInitializer* ai = init->isArrayInitializer()) | |
828 { | |
829 _init = LLVM_DtoArrayInitializer(ai); | |
830 } | |
831 else if (init->isVoidInitializer()) | |
832 { | |
833 const llvm::Type* ty = LLVM_DtoType(type); | |
834 _init = llvm::Constant::getNullValue(ty); | |
835 } | |
836 else { | |
837 Logger::println("unsupported initializer: %s", init->toChars()); | |
838 } | |
839 return _init; | |
840 } | |
6 | 841 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
842 ////////////////////////////////////////////////////////////////////////////////////////// |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
843 |
6 | 844 llvm::Value* LLVM_DtoGEP(llvm::Value* ptr, llvm::Value* i0, llvm::Value* i1, const std::string& var, llvm::BasicBlock* bb) |
845 { | |
846 std::vector<llvm::Value*> v(2); | |
847 v[0] = i0; | |
848 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
|
849 Logger::cout() << "DtoGEP: " << *ptr << '\n'; |
6 | 850 return new llvm::GetElementPtrInst(ptr, v.begin(), v.end(), var, bb); |
851 } | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
852 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
853 ////////////////////////////////////////////////////////////////////////////////////////// |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
854 |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
855 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
|
856 { |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
857 size_t n = src.size(); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
858 std::vector<llvm::Value*> dst(n); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
859 Logger::cout() << "indices:"; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
860 for (size_t i=0; i<n; ++i) |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
861 { |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
862 Logger::cout() << ' ' << i; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
863 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
|
864 } |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
865 Logger::cout() << '\n'; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
866 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
|
867 } |
9 | 868 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
869 ////////////////////////////////////////////////////////////////////////////////////////// |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
870 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
871 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
|
872 { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
873 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
|
874 } |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
875 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
876 ////////////////////////////////////////////////////////////////////////////////////////// |
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 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
|
879 { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
880 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
|
881 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
|
882 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
|
883 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
|
884 } |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
885 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
886 ////////////////////////////////////////////////////////////////////////////////////////// |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
887 |
9 | 888 llvm::Function* LLVM_DtoDeclareFunction(FuncDeclaration* fdecl) |
889 { | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
890 TypeFunction* f = (TypeFunction*)fdecl->type; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
891 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
|
892 |
9 | 893 if (fdecl->llvmValue != 0) { |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
894 assert(llvm::isa<llvm::Function>(fdecl->llvmValue)); |
9 | 895 return llvm::cast<llvm::Function>(fdecl->llvmValue); |
896 } | |
897 | |
898 static int fdi = 0; | |
899 Logger::print("FuncDeclaration::toObjFile(%d,%s): %s\n", fdi++, fdecl->needThis()?"this":"static",fdecl->toChars()); | |
900 LOG_SCOPE; | |
901 | |
902 if (fdecl->llvmInternal == LLVMintrinsic && fdecl->fbody) { | |
903 error("intrinsics cannot have function bodies"); | |
904 fatal(); | |
905 } | |
906 | |
907 // 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
|
908 const llvm::FunctionType* functype = (f->llvmType == 0) ? LLVM_DtoFunctionType(fdecl) : llvm::cast<llvm::FunctionType>(f->llvmType); |
9 | 909 |
910 // mangled name | |
911 char* mangled_name = (fdecl->llvmInternal == LLVMintrinsic) ? fdecl->llvmInternal1 : fdecl->mangle(); | |
912 | |
913 // make the function | |
914 llvm::Function* func = gIR->module->getFunction(mangled_name); | |
915 if (func == 0) { | |
916 func = new llvm::Function(functype,LLVM_DtoLinkage(fdecl->protection, fdecl->storage_class),mangled_name,gIR->module); | |
917 } | |
918 | |
919 if (fdecl->llvmInternal != LLVMintrinsic) | |
920 func->setCallingConv(LLVM_DtoCallingConv(f->linkage)); | |
921 | |
922 fdecl->llvmValue = func; | |
923 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
|
924 assert(llvm::isa<llvm::FunctionType>(f->llvmType)); |
9 | 925 |
926 if (fdecl->isMain()) { | |
927 gIR->mainFunc = func; | |
928 } | |
929 | |
930 // name parameters | |
931 llvm::Function::arg_iterator iarg = func->arg_begin(); | |
932 int k = 0; | |
933 if (f->llvmRetInPtr) { | |
934 iarg->setName("retval"); | |
935 f->llvmRetArg = iarg; | |
936 ++iarg; | |
937 } | |
938 if (f->llvmUsesThis) { | |
939 iarg->setName("this"); | |
940 ++iarg; | |
941 } | |
942 for (; iarg != func->arg_end(); ++iarg) | |
943 { | |
944 Argument* arg = Argument::getNth(f->parameters, k++); | |
945 assert(arg != 0); | |
946 //arg->llvmValue = iarg; | |
947 //printf("identifier: '%s' %p\n", arg->ident->toChars(), arg->ident); | |
948 if (arg->ident != 0) { | |
949 if (arg->vardecl) { | |
950 arg->vardecl->llvmValue = iarg; | |
951 } | |
952 iarg->setName(arg->ident->toChars()); | |
953 } | |
954 else { | |
955 iarg->setName("unnamed"); | |
956 } | |
957 } | |
958 | |
959 return func; | |
960 } |