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