Mercurial > projects > ldc
annotate gen/toir.c @ 14:0e86428ee567 trunk
[svn r18] * Initial support for switch statements - No string switches yet.
* Moved Statement::toIR definitions into gen/statements.c - toir.c is still too big.
* Removed some BB bloat with ScopeStatements.
author | lindquist |
---|---|
date | Wed, 03 Oct 2007 02:15:12 +0200 |
parents | d3ee9efe20e2 |
children | 37a4fdab33fc |
rev | line source |
---|---|
1 | 1 // Backend stubs |
2 | |
3 /* DMDFE backend stubs | |
4 * This file contains the implementations of the backend routines. | |
5 * For dmdfe these do nothing but print a message saying the module | |
6 * has been parsed. Substitute your own behaviors for these routimes. | |
7 */ | |
8 | |
9 #include <stdio.h> | |
10 #include <math.h> | |
11 #include <sstream> | |
12 #include <fstream> | |
13 #include <iostream> | |
14 | |
15 #include "llvm/Type.h" | |
16 #include "llvm/DerivedTypes.h" | |
17 #include "llvm/Constants.h" | |
18 #include "llvm/Instructions.h" | |
19 #include "llvm/IntrinsicInst.h" | |
20 #include "llvm/CallingConv.h" | |
21 | |
22 #include "total.h" | |
23 #include "init.h" | |
24 #include "symbol.h" | |
25 #include "mtype.h" | |
26 #include "hdrgen.h" | |
27 #include "port.h" | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
28 |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
29 #include "gen/irstate.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
30 #include "gen/elem.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
31 #include "gen/logger.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
32 #include "gen/tollvm.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
33 #include "gen/runtime.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
34 #include "gen/arrays.h" |
1 | 35 |
36 ////////////////////////////////////////////////////////////////////////////////////////// | |
37 | |
38 elem* DeclarationExp::toElem(IRState* p) | |
39 { | |
40 Logger::print("DeclarationExp::toElem: %s | T=%s\n", toChars(), type->toChars()); | |
41 LOG_SCOPE; | |
42 elem* e = new elem; | |
43 | |
44 // variable declaration | |
45 if (VarDeclaration* vd = declaration->isVarDeclaration()) | |
46 { | |
47 Logger::println("VarDeclaration"); | |
48 | |
49 // handle const | |
50 // TODO probably not correct | |
51 bool isconst = (vd->storage_class & STCconst) != 0; | |
52 | |
53 // allocate storage on the stack | |
54 Logger::println("vdtype = %s", vd->type->toChars()); | |
55 const llvm::Type* lltype = LLVM_DtoType(vd->type); | |
56 llvm::AllocaInst* allocainst = new llvm::AllocaInst(lltype, vd->toChars(), p->topallocapoint()); | |
57 //allocainst->setAlignment(vd->type->alignsize()); // TODO | |
58 vd->llvmValue = allocainst; | |
59 // e->val = really needed?? | |
60 | |
61 LLVM_DtoInitializer(vd->type, vd->init); | |
62 } | |
63 // struct declaration | |
64 else if (StructDeclaration* s = declaration->isStructDeclaration()) | |
65 { | |
66 Logger::println("StructDeclaration"); | |
67 s->toObjFile(); | |
68 } | |
69 // unsupported declaration | |
70 else | |
71 { | |
72 error("Only Var/Struct-Declaration is supported for DeclarationExp"); | |
73 fatal(); | |
74 } | |
75 return e; | |
76 } | |
77 | |
78 ////////////////////////////////////////////////////////////////////////////////////////// | |
79 | |
80 elem* VarExp::toElem(IRState* p) | |
81 { | |
82 Logger::print("VarExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
83 LOG_SCOPE; | |
84 | |
85 elem* e = new elem; | |
86 | |
87 assert(var); | |
88 if (VarDeclaration* vd = var->isVarDeclaration()) | |
89 { | |
90 Logger::println("VarDeclaration"); | |
91 | |
92 if (TypeInfoDeclaration* tid = vd->isTypeInfoDeclaration()) | |
93 { | |
94 Logger::println("TypeInfoDeclaration"); | |
95 } | |
96 | |
97 // this must be a dollar expression or some other magic value | |
98 if (!vd->llvmValue) | |
99 { | |
100 // dollar | |
101 if (!p->arrays.empty()) | |
102 { | |
103 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
6 | 104 //llvm::Value* tmp = new llvm::GetElementPtrInst(p->arrays.back(),zero,zero,"tmp",p->scopebb()); |
105 llvm::Value* tmp = LLVM_DtoGEP(p->arrays.back(),zero,zero,"tmp",p->scopebb()); | |
1 | 106 e->val = new llvm::LoadInst(tmp,"tmp",p->scopebb()); |
107 e->type = elem::VAL; | |
108 } | |
109 // magic | |
110 else | |
111 { | |
112 if (TypeInfoDeclaration* tid = vd->isTypeInfoDeclaration()) | |
113 { | |
114 tid->toObjFile(); | |
115 e->mem = tid->llvmValue; | |
116 e->type = elem::VAR; | |
117 } | |
118 else | |
119 assert(0 && "only magic supported is typeinfo"); | |
120 } | |
121 return e; | |
122 } | |
123 | |
124 // function parameter | |
125 if (vd->storage_class & STCparameter) { | |
126 Logger::println("function param"); | |
127 if (vd->storage_class & (STCref | STCout)) { | |
128 e->mem = vd->llvmValue; | |
129 e->type = elem::VAR; | |
130 } | |
131 else { | |
132 if (vd->type->ty == Tstruct || vd->type->ty == Tdelegate || vd->type->ty == Tarray) { | |
133 e->mem = vd->llvmValue; | |
134 e->type = elem::VAR; | |
135 } | |
136 else { | |
137 e->val = vd->llvmValue; | |
138 e->type = elem::VAL; | |
139 } | |
140 } | |
141 } | |
142 else { | |
143 e->mem = vd->llvmValue; | |
144 //e->mem->setName(toChars()); | |
145 e->vardecl = vd; | |
146 e->type = elem::VAR; | |
147 } | |
148 } | |
149 else if (FuncDeclaration* fdecl = var->isFuncDeclaration()) | |
150 { | |
151 Logger::println("FuncDeclaration"); | |
152 if (fdecl->llvmValue == 0) { | |
153 fdecl->toObjFile(); | |
154 } | |
155 e->val = fdecl->llvmValue; | |
156 e->type = elem::FUNC; | |
157 e->funcdecl = fdecl; | |
158 } | |
159 else if (SymbolDeclaration* sdecl = var->isSymbolDeclaration()) | |
160 { | |
161 // this seems to be the static initialiser for structs | |
162 Logger::print("Sym: type=%s\n", sdecl->type->toChars()); | |
163 assert(sdecl->type->ty == Tstruct); | |
164 //assert(sdecl->llvmInitZ); | |
165 //e->val = sdecl->llvmInitZ; | |
166 TypeStruct* ts = (TypeStruct*)sdecl->type; | |
167 e->mem = ts->llvmInit; | |
168 assert(e->mem); | |
169 e->type = elem::VAR; | |
170 } | |
171 else | |
172 { | |
173 assert(0 && "Unimplemented VarExp type"); | |
174 } | |
175 | |
176 assert(e->mem || e->val); | |
177 return e; | |
178 } | |
179 | |
180 ////////////////////////////////////////////////////////////////////////////////////////// | |
181 | |
182 elem* IntegerExp::toElem(IRState* p) | |
183 { | |
184 Logger::print("IntegerExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
185 LOG_SCOPE; | |
186 elem* e = new elem; | |
187 const llvm::Type* t = LLVM_DtoType(type); | |
188 if (llvm::isa<llvm::PointerType>(t)) { | |
189 llvm::Constant* i = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)value,false); | |
190 e->val = llvm::ConstantExpr::getIntToPtr(i, t); | |
191 } | |
192 else if (llvm::isa<llvm::IntegerType>(t)) { | |
193 e->val = llvm::ConstantInt::get(t,(uint64_t)value,!type->isunsigned()); | |
194 } | |
195 else { | |
196 assert(0); | |
197 } | |
198 e->type = elem::CONST; | |
199 return e; | |
200 } | |
201 | |
202 ////////////////////////////////////////////////////////////////////////////////////////// | |
203 | |
204 elem* RealExp::toElem(IRState* p) | |
205 { | |
206 Logger::print("RealExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
207 LOG_SCOPE; | |
208 elem* e = new elem; | |
6 | 209 const llvm::Type* fty = LLVM_DtoType(type); |
210 if (type->ty == Tfloat32) | |
211 e->val = llvm::ConstantFP::get(fty,float(value)); | |
212 else if (type->ty == Tfloat64 || type->ty == Tfloat80) | |
213 e->val = llvm::ConstantFP::get(fty,double(value)); | |
214 else | |
215 assert(0); | |
1 | 216 e->type = elem::CONST; |
217 return e; | |
218 } | |
219 | |
220 ////////////////////////////////////////////////////////////////////////////////////////// | |
221 | |
222 elem* NullExp::toElem(IRState* p) | |
223 { | |
224 Logger::print("NullExp::toElem(type=%s): %s\n", type->toChars(),toChars()); | |
225 LOG_SCOPE; | |
226 elem* e = new elem; | |
227 const llvm::Type* t = LLVM_DtoType(type); | |
228 if (llvm::isa<llvm::StructType>(t)) | |
229 t = llvm::PointerType::get(t); | |
230 Logger::cout() << *t << '\n'; | |
231 e->val = llvm::Constant::getNullValue(t); | |
232 assert(e->val); | |
233 Logger::cout() << *e->val << '\n'; | |
234 e->type = elem::NUL; | |
235 return e; | |
236 } | |
237 | |
238 ////////////////////////////////////////////////////////////////////////////////////////// | |
239 | |
240 elem* StringExp::toElem(IRState* p) | |
241 { | |
242 Logger::print("StringExp::toElem: %s\n", toChars()); | |
243 LOG_SCOPE; | |
244 | |
245 assert(type->next->ty == Tchar && "Only char is supported"); | |
246 assert(sz == 1); | |
247 | |
248 const llvm::Type* ct = LLVM_DtoType(type->next); | |
249 //printf("ct = %s\n", type->next->toChars()); | |
250 const llvm::ArrayType* at = llvm::ArrayType::get(ct,len+1); | |
251 | |
252 uint8_t* str = (uint8_t*)string; | |
253 std::string cont((char*)str, len); | |
254 | |
255 llvm::Constant* _init = llvm::ConstantArray::get(cont,true); | |
256 | |
257 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::InternalLinkage;//WeakLinkage; | |
258 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(at,true,_linkage,_init,"stringliteral",gIR->module); | |
259 | |
260 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
6 | 261 llvm::Value* arrptr = LLVM_DtoGEP(gvar,zero,zero,"tmp",p->scopebb()); |
1 | 262 |
263 elem* e = new elem; | |
264 | |
265 if (type->ty == Tarray) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
266 llvm::Constant* clen = llvm::ConstantInt::get(LLVM_DtoSize_t(),len,false); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
267 if (p->lvals.empty()) { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
268 e->type = elem::SLICE; |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
269 e->arg = clen; |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
270 e->mem = arrptr; |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
271 return e; |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
272 } |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
273 else { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
274 llvm::Value* arr = p->toplval(); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
275 LLVM_DtoSetArray(arr, clen, arrptr); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
276 } |
1 | 277 } |
278 else if (type->ty == Tpointer) { | |
279 e->mem = arrptr; | |
280 } | |
281 else { | |
282 assert(0); | |
283 } | |
284 | |
285 e->inplace = true; | |
286 e->type = elem::VAL; | |
287 | |
288 return e; | |
289 } | |
290 | |
291 ////////////////////////////////////////////////////////////////////////////////////////// | |
292 | |
293 elem* AssignExp::toElem(IRState* p) | |
294 { | |
295 Logger::print("AssignExp::toElem: %s | %s = %s\n", toChars(), e1->type->toChars(), e2->type->toChars()); | |
296 LOG_SCOPE; | |
297 | |
298 assert(e1 && e2); | |
299 p->inLvalue = true; | |
300 elem* l = e1->toElem(p); | |
301 p->inLvalue = false; | |
302 | |
303 p->lvals.push_back(l->mem); | |
304 elem* r = e2->toElem(p); | |
305 p->lvals.pop_back(); | |
306 | |
307 assert(l->mem); | |
308 //e->val = l->store(r->getValue()); | |
309 | |
310 TY e1ty = e1->type->ty; | |
311 TY e2ty = e2->type->ty; | |
312 | |
313 elem* e = new elem; | |
314 | |
315 // struct | |
316 if (e1ty == Tstruct) { | |
317 // struct + struct | |
318 if (e2ty == Tstruct) { | |
319 // struct literals do the assignment themselvs (in place) | |
320 if (!r->inplace) { | |
321 TypeStruct* ts = (TypeStruct*)e2->type; | |
322 assert(r->mem); | |
323 LLVM_DtoStructCopy(ts,l->mem,r->mem); | |
324 } | |
325 else { | |
326 e->inplace = true; | |
327 } | |
328 } | |
329 // struct + const int | |
330 else if (e2->type->isintegral()){ | |
331 IntegerExp* iexp = (IntegerExp*)e2; | |
332 assert(iexp->value == 0 && "Only integral struct initializer allowed is zero"); | |
333 TypeStruct* st = (TypeStruct*)e1->type; | |
334 LLVM_DtoStructZeroInit(st, l->mem); | |
335 } | |
336 // :x | |
337 else | |
338 assert(0 && "struct = unknown"); | |
339 } | |
340 else if (e1ty == Tsarray) { | |
341 assert(0 && "static array = not supported"); | |
342 } | |
343 else if (e1ty == Tarray) { | |
344 if (e2->type->isscalar() || e2->type->ty == Tclass){ | |
345 LLVM_DtoArrayInit(l->mem, r->getValue()); | |
346 } | |
347 else if (e2ty == Tarray) { | |
348 //new llvm::StoreInst(r->val,l->val,p->scopebb()); | |
349 if (r->type == elem::NUL) { | |
350 llvm::Constant* c = llvm::cast<llvm::Constant>(r->val); | |
351 assert(c->isNullValue()); | |
352 LLVM_DtoNullArray(l->mem); | |
353 } | |
354 else if (r->type == elem::SLICE) { | |
355 if (l->type == elem::SLICE) | |
356 LLVM_DtoArrayCopy(l,r); | |
357 else | |
358 LLVM_DtoSetArray(l->mem,r->arg,r->mem); | |
359 } | |
360 else { | |
361 // new expressions write directly to the array reference | |
362 // so do string literals | |
363 if (!r->inplace) { | |
364 assert(r->mem); | |
365 LLVM_DtoArrayAssign(l->mem, r->mem); | |
366 } | |
367 else { | |
368 e->inplace = true; | |
369 } | |
370 } | |
371 } | |
372 else | |
373 assert(0); | |
374 } | |
375 else if (e1ty == Tpointer) { | |
376 if (e2ty == Tpointer) { | |
377 llvm::Value* v = r->field ? r->mem : r->getValue(); | |
378 Logger::cout() << "*=*: " << *v << ", " << *l->mem << '\n'; | |
379 new llvm::StoreInst(v, l->mem, p->scopebb()); | |
380 } | |
381 else | |
382 assert(0); | |
383 } | |
384 else if (e1ty == Tclass) { | |
385 if (e2ty == Tclass) { | |
386 llvm::Value* tmp = r->getValue(); | |
387 Logger::cout() << "tmp: " << *tmp << ", " << *l->mem << '\n'; | |
388 new llvm::StoreInst(tmp, l->mem, p->scopebb()); | |
389 } | |
390 else | |
391 assert(0); | |
392 } | |
393 else if (e1ty == Tdelegate) { | |
394 Logger::println("Assigning to delegate"); | |
395 if (e2ty == Tdelegate) { | |
396 if (r->type == elem::NUL) { | |
397 llvm::Constant* c = llvm::cast<llvm::Constant>(r->val); | |
398 if (c->isNullValue()) { | |
399 LLVM_DtoNullDelegate(l->mem); | |
400 } | |
401 else | |
402 assert(0); | |
403 } | |
404 else if (r->inplace) { | |
405 // do nothing | |
406 e->inplace = true; | |
407 } | |
408 else | |
409 assert(0); | |
410 } | |
411 else | |
412 assert(0); | |
413 } | |
414 // !struct && !array && !pointer && !class | |
415 else { | |
416 Logger::cout() << *l->mem << '\n'; | |
417 new llvm::StoreInst(r->getValue(),l->mem,p->scopebb()); | |
418 } | |
419 | |
420 delete r; | |
421 delete l; | |
422 return e; | |
423 } | |
424 | |
425 ////////////////////////////////////////////////////////////////////////////////////////// | |
426 | |
427 elem* AddExp::toElem(IRState* p) | |
428 { | |
429 Logger::print("AddExp::toElem: %s\n", toChars()); | |
430 LOG_SCOPE; | |
431 elem* e = new elem; | |
432 elem* l = e1->toElem(p); | |
433 elem* r = e2->toElem(p); | |
434 | |
435 if (e1->type != e2->type) { | |
436 if (e1->type->ty == Tpointer && e1->type->next->ty == Tstruct) { | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
437 //assert(l->field); |
1 | 438 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); |
439 assert(r->type == elem::CONST); | |
440 llvm::ConstantInt* cofs = llvm::cast<llvm::ConstantInt>(r->val); | |
441 | |
442 TypeStruct* ts = (TypeStruct*)e1->type->next; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
443 std::vector<unsigned> offsets(1,0); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
444 ts->sym->offsetToIndex(cofs->getZExtValue(), offsets); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
445 e->mem = LLVM_DtoGEP(l->getValue(), offsets, "tmp", p->scopebb()); |
1 | 446 e->type = elem::VAR; |
447 e->field = true; | |
448 } | |
449 else if (e1->type->ty == Tpointer) { | |
450 e->val = new llvm::GetElementPtrInst(l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
451 e->type = elem::VAR; | |
452 } | |
453 else { | |
454 assert(0); | |
455 } | |
456 } | |
457 else { | |
458 e->val = llvm::BinaryOperator::createAdd(l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
459 e->type = elem::VAL; | |
460 } | |
461 delete l; | |
462 delete r; | |
463 return e; | |
464 } | |
465 | |
466 ////////////////////////////////////////////////////////////////////////////////////////// | |
467 | |
468 elem* AddAssignExp::toElem(IRState* p) | |
469 { | |
470 Logger::print("AddAssignExp::toElem: %s\n", toChars()); | |
471 LOG_SCOPE; | |
472 | |
473 elem* l = e1->toElem(p); | |
474 elem* r = e2->toElem(p); | |
475 | |
476 elem* e = new elem; | |
477 llvm::Value* val = 0; | |
478 if (e1->type->ty == Tpointer) { | |
479 val = e->mem = new llvm::GetElementPtrInst(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
480 } | |
481 else { | |
482 val = e->val = llvm::BinaryOperator::createAdd(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
483 } | |
484 | |
485 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
486 if (llvm::isa<llvm::PointerType>(storeVal->getType()) && storeVal->getType()->getContainedType(0) != tmp->getType()) | |
487 { | |
488 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
489 }*/ | |
490 | |
491 new llvm::StoreInst(val,l->mem,p->scopebb()); | |
492 e->type = elem::VAR; | |
493 | |
494 delete l; | |
495 delete r; | |
496 return e; | |
497 } | |
498 | |
499 ////////////////////////////////////////////////////////////////////////////////////////// | |
500 | |
501 elem* MinExp::toElem(IRState* p) | |
502 { | |
503 Logger::print("MinExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
504 LOG_SCOPE; | |
505 elem* e = new elem; | |
506 elem* l = e1->toElem(p); | |
507 elem* r = e2->toElem(p); | |
508 | |
509 llvm::Value* left = l->getValue(); | |
510 if (llvm::isa<llvm::PointerType>(left->getType())) | |
511 left = new llvm::PtrToIntInst(left,LLVM_DtoSize_t(),"tmp",p->scopebb()); | |
512 | |
513 llvm::Value* right = r->getValue(); | |
514 if (llvm::isa<llvm::PointerType>(right->getType())) | |
515 right = new llvm::PtrToIntInst(right,LLVM_DtoSize_t(),"tmp",p->scopebb()); | |
516 | |
517 e->val = llvm::BinaryOperator::createSub(left,right,"tmp",p->scopebb()); | |
518 e->type = elem::VAL; | |
519 | |
520 const llvm::Type* totype = LLVM_DtoType(type); | |
521 if (e->val->getType() != totype) { | |
522 assert(0); | |
523 assert(llvm::isa<llvm::PointerType>(e->val->getType())); | |
524 assert(llvm::isa<llvm::IntegerType>(totype)); | |
525 e->val = new llvm::IntToPtrInst(e->val,totype,"tmp",p->scopebb()); | |
526 } | |
527 | |
528 delete l; | |
529 delete r; | |
530 return e; | |
531 } | |
532 | |
533 ////////////////////////////////////////////////////////////////////////////////////////// | |
534 | |
535 elem* MinAssignExp::toElem(IRState* p) | |
536 { | |
537 Logger::print("MinAssignExp::toElem: %s\n", toChars()); | |
538 LOG_SCOPE; | |
539 | |
540 elem* l = e1->toElem(p); | |
541 elem* r = e2->toElem(p); | |
542 | |
543 llvm::Value* tmp = 0; | |
544 if (e1->type->ty == Tpointer) { | |
545 tmp = r->getValue(); | |
546 llvm::Value* zero = llvm::ConstantInt::get(tmp->getType(),0,false); | |
547 tmp = llvm::BinaryOperator::createSub(zero,tmp,"tmp",p->scopebb()); | |
548 tmp = new llvm::GetElementPtrInst(l->getValue(),tmp,"tmp",p->scopebb()); | |
549 } | |
550 else { | |
551 tmp = llvm::BinaryOperator::createSub(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
552 } | |
553 | |
554 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
555 if (storeVal->getType()->getContainedType(0) != tmp->getType()) | |
556 { | |
557 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
558 }*/ | |
559 | |
560 new llvm::StoreInst(tmp, l->mem, p->scopebb()); | |
561 | |
562 delete l; | |
563 delete r; | |
564 | |
565 elem* e = new elem; | |
566 e->val = tmp; | |
567 e->type = elem::VAR; | |
568 return e; | |
569 } | |
570 | |
571 ////////////////////////////////////////////////////////////////////////////////////////// | |
572 | |
573 elem* MulExp::toElem(IRState* p) | |
574 { | |
575 Logger::print("MulExp::toElem: %s\n", toChars()); | |
576 LOG_SCOPE; | |
577 elem* e = new elem; | |
578 elem* l = e1->toElem(p); | |
579 elem* r = e2->toElem(p); | |
580 llvm::Value* vl = l->getValue(); | |
581 llvm::Value* vr = r->getValue(); | |
582 Logger::cout() << "mul: " << *vl << ", " << *vr << '\n'; | |
583 e->val = llvm::BinaryOperator::createMul(vl,vr,"tmp",p->scopebb()); | |
584 e->type = elem::VAL; | |
585 delete l; | |
586 delete r; | |
587 return e; | |
588 } | |
589 | |
590 ////////////////////////////////////////////////////////////////////////////////////////// | |
591 | |
592 elem* MulAssignExp::toElem(IRState* p) | |
593 { | |
594 Logger::print("MulAssignExp::toElem: %s\n", toChars()); | |
595 LOG_SCOPE; | |
596 | |
597 elem* l = e1->toElem(p); | |
598 elem* r = e2->toElem(p); | |
599 llvm::Value* vl = l->getValue(); | |
600 llvm::Value* vr = r->getValue(); | |
601 Logger::cout() << "mulassign: " << *vl << ", " << *vr << '\n'; | |
602 llvm::Value* tmp = llvm::BinaryOperator::createMul(vl,vr,"tmp",p->scopebb()); | |
603 | |
604 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
605 if (storeVal->getType()->getContainedType(0) != tmp->getType()) | |
606 { | |
607 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
608 }*/ | |
609 | |
610 new llvm::StoreInst(tmp,l->mem,p->scopebb()); | |
611 | |
612 delete l; | |
613 delete r; | |
614 | |
615 elem* e = new elem; | |
616 e->val = tmp; | |
617 e->type = elem::VAR; | |
618 return e; | |
619 } | |
620 | |
621 ////////////////////////////////////////////////////////////////////////////////////////// | |
622 | |
623 elem* DivExp::toElem(IRState* p) | |
624 { | |
625 Logger::print("DivExp::toElem: %s\n", toChars()); | |
626 LOG_SCOPE; | |
627 elem* e = new elem; | |
628 elem* l = e1->toElem(p); | |
629 elem* r = e2->toElem(p); | |
630 | |
631 if (type->isunsigned()) | |
632 e->val = llvm::BinaryOperator::createUDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
633 else if (type->isintegral()) | |
634 e->val = llvm::BinaryOperator::createSDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
635 else if (type->isfloating()) | |
636 e->val = llvm::BinaryOperator::createFDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
637 else | |
638 assert(0); | |
639 e->type = elem::VAL; | |
640 delete l; | |
641 delete r; | |
642 return e; | |
643 } | |
644 | |
645 ////////////////////////////////////////////////////////////////////////////////////////// | |
646 | |
647 elem* DivAssignExp::toElem(IRState* p) | |
648 { | |
649 Logger::print("DivAssignExp::toElem: %s\n", toChars()); | |
650 LOG_SCOPE; | |
651 | |
652 elem* l = e1->toElem(p); | |
653 elem* r = e2->toElem(p); | |
654 | |
655 llvm::Value* tmp; | |
656 if (type->isunsigned()) | |
657 tmp = llvm::BinaryOperator::createUDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
658 else if (type->isintegral()) | |
659 tmp = llvm::BinaryOperator::createSDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
660 else if (type->isfloating()) | |
661 tmp = llvm::BinaryOperator::createFDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
662 else | |
663 assert(0); | |
664 | |
665 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
666 if (storeVal->getType()->getContainedType(0) != tmp->getType()) | |
667 { | |
668 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
669 }*/ | |
670 | |
671 new llvm::StoreInst(tmp,l->mem,p->scopebb()); | |
672 | |
673 delete l; | |
674 delete r; | |
675 | |
676 elem* e = new elem; | |
677 e->val = tmp; | |
678 e->type = elem::VAR; | |
679 return e; | |
680 } | |
681 | |
682 ////////////////////////////////////////////////////////////////////////////////////////// | |
683 | |
684 elem* ModExp::toElem(IRState* p) | |
685 { | |
686 Logger::print("ModExp::toElem: %s\n", toChars()); | |
687 LOG_SCOPE; | |
688 elem* e = new elem; | |
689 elem* l = e1->toElem(p); | |
690 elem* r = e2->toElem(p); | |
691 | |
692 if (type->isunsigned()) | |
693 e->val = llvm::BinaryOperator::createURem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
694 else if (type->isintegral()) | |
695 e->val = llvm::BinaryOperator::createSRem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
696 else if (type->isfloating()) | |
697 e->val = llvm::BinaryOperator::createFRem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
698 else | |
699 assert(0); | |
700 e->type = elem::VAL; | |
701 delete l; | |
702 delete r; | |
703 return e; | |
704 } | |
705 | |
706 ////////////////////////////////////////////////////////////////////////////////////////// | |
707 | |
708 elem* ModAssignExp::toElem(IRState* p) | |
709 { | |
710 Logger::print("ModAssignExp::toElem: %s\n", toChars()); | |
711 LOG_SCOPE; | |
712 | |
713 elem* l = e1->toElem(p); | |
714 elem* r = e2->toElem(p); | |
715 | |
716 llvm::Value* tmp; | |
717 if (type->isunsigned()) | |
718 tmp = llvm::BinaryOperator::createURem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
719 else if (type->isintegral()) | |
720 tmp = llvm::BinaryOperator::createSRem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
721 else if (type->isfloating()) | |
722 tmp = llvm::BinaryOperator::createFRem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
723 else | |
724 assert(0); | |
725 | |
726 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
727 if (storeVal->getType()->getContainedType(0) != tmp->getType()) | |
728 { | |
729 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
730 }*/ | |
731 | |
732 new llvm::StoreInst(tmp,l->mem,p->scopebb()); | |
733 | |
734 delete l; | |
735 delete r; | |
736 | |
737 elem* e = new elem; | |
738 e->val = tmp; | |
739 e->type = elem::VAR; | |
740 return e; | |
741 } | |
742 | |
743 ////////////////////////////////////////////////////////////////////////////////////////// | |
744 | |
745 elem* CallExp::toElem(IRState* p) | |
746 { | |
747 Logger::print("CallExp::toElem: %s\n", toChars()); | |
748 LOG_SCOPE; | |
749 elem* e = new elem; | |
750 elem* fn = e1->toElem(p); | |
751 LINK dlink = LINKdefault; | |
752 | |
753 bool delegateCall = false; | |
754 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty,0,false); | |
755 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty,1,false); | |
756 | |
757 // hidden struct return parameter handling | |
758 bool retinptr = false; | |
759 | |
760 TypeFunction* tf = 0; | |
761 | |
762 // regular functions | |
763 if (e1->type->ty == Tfunction) { | |
764 tf = (TypeFunction*)e1->type; | |
765 if (tf->llvmRetInPtr) { | |
766 retinptr = true; | |
767 } | |
768 dlink = tf->linkage; | |
769 } | |
770 | |
771 // delegates | |
772 else if (e1->type->ty == Tdelegate) { | |
773 Logger::println("delegateTy = %s\n", e1->type->toChars()); | |
774 assert(e1->type->next->ty == Tfunction); | |
775 tf = (TypeFunction*)e1->type->next; | |
776 if (tf->llvmRetInPtr) { | |
777 retinptr = true; | |
778 } | |
779 dlink = tf->linkage; | |
780 delegateCall = true; | |
781 } | |
782 | |
783 // invalid | |
784 else { | |
785 assert(tf); | |
786 } | |
787 | |
788 size_t n = arguments->dim; | |
789 if (fn->arg || delegateCall) n++; | |
790 if (retinptr) n++; | |
791 | |
792 llvm::Value* funcval = fn->getValue(); | |
793 std::vector<llvm::Value*> llargs(n, 0); | |
794 | |
795 const llvm::FunctionType* llfnty = 0; | |
796 | |
797 // normal function call | |
798 if (llvm::isa<llvm::FunctionType>(funcval->getType())) { | |
799 llfnty = llvm::cast<llvm::FunctionType>(funcval->getType()); | |
800 } | |
801 // pointer to something | |
802 else if (llvm::isa<llvm::PointerType>(funcval->getType())) { | |
803 // pointer to function pointer - I think this not really supposed to happen, but does :/ | |
804 // seems like sometimes we get a func* other times a func** | |
805 if (llvm::isa<llvm::PointerType>(funcval->getType()->getContainedType(0))) { | |
806 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); | |
807 } | |
808 | |
809 // function pointer | |
810 if (llvm::isa<llvm::FunctionType>(funcval->getType()->getContainedType(0))) { | |
811 //Logger::cout() << "function pointer type:\n" << *funcval << '\n'; | |
812 llfnty = llvm::cast<llvm::FunctionType>(funcval->getType()->getContainedType(0)); | |
813 } | |
814 // struct pointer - delegate | |
815 else if (llvm::isa<llvm::StructType>(funcval->getType()->getContainedType(0))) { | |
6 | 816 funcval = LLVM_DtoGEP(funcval,zero,one,"tmp",p->scopebb()); |
1 | 817 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); |
818 const llvm::Type* ty = funcval->getType()->getContainedType(0); | |
819 llfnty = llvm::cast<llvm::FunctionType>(ty); | |
820 } | |
821 // unknown | |
822 else { | |
823 Logger::cout() << "what kind of pointer are we calling? : " << *funcval->getType() << '\n'; | |
824 } | |
825 } | |
826 else { | |
827 Logger::cout() << "what are we calling? : " << *funcval << '\n'; | |
828 } | |
829 assert(llfnty); | |
830 Logger::cout() << "Function LLVM type: " << *llfnty << '\n'; | |
831 | |
832 // argument handling | |
833 llvm::FunctionType::param_iterator argiter = llfnty->param_begin(); | |
834 int j = 0; | |
835 | |
836 // hidden struct return parameter | |
837 if (retinptr) { | |
838 if (!p->lvals.empty()) { | |
839 assert(llvm::isa<llvm::StructType>(p->toplval()->getType()->getContainedType(0))); | |
840 llargs[j] = p->toplval(); | |
841 TY Dty = tf->next->ty; | |
842 if (Dty == Tstruct || Dty == Tdelegate || Dty == Tarray) { | |
843 e->inplace = true; | |
844 } | |
845 else | |
846 assert(0); | |
847 } | |
848 else { | |
849 llargs[j] = new llvm::AllocaInst(argiter->get()->getContainedType(0),"rettmp",p->topallocapoint()); | |
850 } | |
851 ++j; | |
852 ++argiter; | |
853 e->type = elem::VAR; | |
854 } | |
855 else { | |
856 e->type = elem::VAL; | |
857 } | |
858 | |
859 // this parameter | |
860 if (fn->arg) { | |
861 Logger::println("This Call"); | |
862 if (fn->arg->getType() != argiter->get()) { | |
863 //Logger::cout() << *fn->thisparam << '|' << *argiter->get() << '\n'; | |
864 llargs[j] = new llvm::BitCastInst(fn->arg, argiter->get(), "tmp", p->scopebb()); | |
865 } | |
866 else { | |
867 llargs[j] = fn->arg; | |
868 } | |
869 ++j; | |
870 ++argiter; | |
871 } | |
872 // delegate context parameter | |
873 else if (delegateCall) { | |
874 Logger::println("Delegate Call"); | |
6 | 875 llvm::Value* contextptr = LLVM_DtoGEP(fn->mem,zero,zero,"tmp",p->scopebb()); |
1 | 876 llargs[j] = new llvm::LoadInst(contextptr,"tmp",p->scopebb()); |
877 ++j; | |
878 ++argiter; | |
879 } | |
880 | |
881 // regular parameters | |
882 for (int i=0; i<arguments->dim; i++,j++) | |
883 { | |
884 Expression* argexp = (Expression*)arguments->data[i]; | |
885 elem* arg = argexp->toElem(p); | |
886 | |
887 Argument* fnarg = Argument::getNth(tf->parameters, i); | |
888 | |
889 TY argty = argexp->type->ty; | |
890 if (argty == Tstruct || argty == Tdelegate || argty == Tarray) { | |
891 if (!fnarg || !fnarg->llvmCopy) { | |
892 llargs[j] = arg->getValue(); | |
893 assert(llargs[j] != 0); | |
894 } | |
895 else { | |
896 llvm::Value* allocaInst = 0; | |
897 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
898 const llvm::PointerType* pty = llvm::cast<llvm::PointerType>(arg->mem->getType()); | |
899 if (argty == Tstruct) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
900 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", p->topallocapoint()); |
1 | 901 TypeStruct* ts = (TypeStruct*)argexp->type; |
902 LLVM_DtoStructCopy(ts,allocaInst,arg->mem); | |
903 } | |
904 else if (argty == Tdelegate) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
905 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", p->topallocapoint()); |
1 | 906 LLVM_DtoDelegateCopy(allocaInst,arg->mem); |
907 } | |
908 else if (argty == Tarray) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
909 if (arg->type == elem::SLICE) { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
910 allocaInst = new llvm::AllocaInst(LLVM_DtoType(argexp->type), "tmpparam", p->topallocapoint()); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
911 LLVM_DtoSetArray(allocaInst, arg->arg, arg->mem); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
912 } |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
913 else { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
914 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", p->topallocapoint()); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
915 LLVM_DtoArrayAssign(allocaInst,arg->mem); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
916 } |
1 | 917 } |
918 else | |
919 assert(0); | |
920 | |
921 llargs[j] = allocaInst; | |
922 assert(llargs[j] != 0); | |
923 } | |
924 } | |
925 else if (!fnarg || fnarg->llvmCopy) { | |
926 llargs[j] = arg->getValue(); | |
927 assert(llargs[j] != 0); | |
928 } | |
929 else { | |
5
3d60e549b0c2
[svn r9] added a preliminary rebuild profile. llvmdc-posix - very handy :)
lindquist
parents:
4
diff
changeset
|
930 llargs[j] = arg->mem ? arg->mem : arg->val; |
1 | 931 assert(llargs[j] != 0); |
932 } | |
933 | |
934 delete arg; | |
935 } | |
936 | |
937 // void returns cannot not be named | |
938 const char* varname = ""; | |
939 if (llfnty->getReturnType() != llvm::Type::VoidTy) | |
940 varname = "tmp"; | |
941 | |
942 Logger::println("%d params passed", n); | |
943 for (int i=0; i<n; ++i) | |
944 { | |
945 Logger::cout() << *llargs[i] << '\n'; | |
946 } | |
947 | |
948 Logger::cout() << "Calling: " << *funcval->getType() << '\n'; | |
949 | |
950 // call the function | |
951 llvm::CallInst* call = new llvm::CallInst(funcval, llargs.begin(), llargs.end(), varname, p->scopebb()); | |
952 e->val = call; | |
953 | |
954 // set calling convention | |
955 if ((fn->funcdecl && (fn->funcdecl->llvmInternal != LLVMintrinsic)) || delegateCall) | |
956 call->setCallingConv(LLVM_DtoCallingConv(dlink)); | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
957 else if (fn->callconv != (unsigned)-1) |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
958 call->setCallingConv(fn->callconv); |
1 | 959 |
960 delete fn; | |
961 return e; | |
962 } | |
963 | |
964 ////////////////////////////////////////////////////////////////////////////////////////// | |
965 | |
966 elem* CastExp::toElem(IRState* p) | |
967 { | |
968 Logger::print("CastExp::toElem: %s\n", toChars()); | |
969 LOG_SCOPE; | |
970 elem* e = new elem; | |
971 elem* u = e1->toElem(p); | |
972 const llvm::Type* totype = LLVM_DtoType(to); | |
973 Type* from = e1->type; | |
974 int lsz = from->size(); | |
975 int rsz = to->size(); | |
976 | |
977 // this makes sure the strange lvalue casts don't screw things up | |
978 e->mem = u->mem; | |
979 | |
980 if (from->isintegral()) { | |
981 if (to->isintegral()) { | |
982 if (lsz < rsz) { | |
983 Logger::cout() << *totype << '\n'; | |
984 if (from->isunsigned() || from->ty == Tbool) { | |
985 e->val = new llvm::ZExtInst(u->getValue(), totype, "tmp", p->scopebb()); | |
986 } else { | |
987 e->val = new llvm::SExtInst(u->getValue(), totype, "tmp", p->scopebb()); | |
988 } | |
989 } | |
990 else if (lsz > rsz) { | |
991 e->val = new llvm::TruncInst(u->getValue(), totype, "tmp", p->scopebb()); | |
992 } | |
993 else { | |
994 e->val = new llvm::BitCastInst(u->getValue(), totype, "tmp", p->scopebb()); | |
995 } | |
996 } | |
997 else if (to->isfloating()) { | |
998 if (from->isunsigned()) { | |
999 e->val = new llvm::UIToFPInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1000 } | |
1001 else { | |
1002 e->val = new llvm::SIToFPInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1003 } | |
1004 } | |
1005 else { | |
1006 assert(0); | |
1007 } | |
1008 //e->storeVal = u->storeVal ? u->storeVal : u->val; | |
1009 e->type = elem::VAL; | |
1010 } | |
1011 else if (from->isfloating()) { | |
1012 if (to->isfloating()) { | |
1013 if ((from->ty == Tfloat80 || from->ty == Tfloat64) && (to->ty == Tfloat80 || to->ty == Tfloat64)) { | |
1014 e->val = u->getValue(); | |
1015 } | |
1016 else if (lsz < rsz) { | |
1017 e->val = new llvm::FPExtInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1018 } | |
1019 else if (lsz > rsz) { | |
1020 e->val = new llvm::FPTruncInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1021 } | |
1022 else { | |
1023 assert(0); | |
1024 } | |
1025 } | |
1026 else if (to->isintegral()) { | |
1027 if (to->isunsigned()) { | |
1028 e->val = new llvm::FPToUIInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1029 } | |
1030 else { | |
1031 e->val = new llvm::FPToSIInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1032 } | |
1033 } | |
1034 else { | |
1035 assert(0); | |
1036 } | |
1037 e->type = elem::VAL; | |
1038 } | |
1039 else if (from->ty == Tclass) { | |
1040 //assert(to->ty == Tclass); | |
1041 e->val = new llvm::BitCastInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1042 e->type = elem::VAL; | |
1043 } | |
1044 else if (from->ty == Tarray || from->ty == Tsarray) { | |
1045 Logger::cout() << "from array or sarray" << '\n'; | |
1046 if (to->ty == Tpointer) { | |
1047 Logger::cout() << "to pointer" << '\n'; | |
1048 assert(from->next == to->next); | |
1049 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1050 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
6 | 1051 llvm::Value* ptr = LLVM_DtoGEP(u->getValue(),zero,one,"tmp",p->scopebb()); |
1 | 1052 e->val = new llvm::LoadInst(ptr, "tmp", p->scopebb()); |
1053 e->type = elem::VAL; | |
1054 } | |
1055 else if (to->ty == Tarray) { | |
1056 Logger::cout() << "to array" << '\n'; | |
1057 assert(from->next->size() == to->next->size()); | |
1058 const llvm::Type* ptrty = LLVM_DtoType(to->next); | |
1059 if (ptrty == llvm::Type::VoidTy) | |
1060 ptrty = llvm::Type::Int8Ty; | |
1061 ptrty = llvm::PointerType::get(ptrty); | |
1062 | |
1063 if (u->type == elem::SLICE) { | |
1064 e->mem = new llvm::BitCastInst(u->mem, ptrty, "tmp", p->scopebb()); | |
1065 e->arg = u->arg; | |
1066 } | |
1067 else { | |
1068 llvm::Value* uval = u->getValue(); | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1069 if (from->ty == Tsarray) { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1070 Logger::cout() << "uvalTy = " << *uval->getType() << '\n'; |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1071 assert(llvm::isa<llvm::PointerType>(uval->getType())); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1072 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(uval->getType()->getContainedType(0)); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1073 e->arg = llvm::ConstantInt::get(LLVM_DtoSize_t(), arrty->getNumElements(), false); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1074 e->mem = new llvm::BitCastInst(uval, ptrty, "tmp", p->scopebb()); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1075 } |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1076 else { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1077 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1078 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); |
6 | 1079 e->arg = LLVM_DtoGEP(uval,zero,zero,"tmp",p->scopebb()); |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1080 e->arg = new llvm::LoadInst(e->arg, "tmp", p->scopebb()); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1081 |
6 | 1082 e->mem = LLVM_DtoGEP(uval,zero,one,"tmp",p->scopebb()); |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1083 e->mem = new llvm::LoadInst(e->mem, "tmp", p->scopebb()); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1084 //Logger::cout() << *e->mem->getType() << '|' << *ptrty << '\n'; |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1085 e->mem = new llvm::BitCastInst(e->mem, ptrty, "tmp", p->scopebb()); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1086 } |
1 | 1087 } |
1088 e->type = elem::SLICE; | |
1089 } | |
1090 else if (to->ty == Tsarray) { | |
1091 Logger::cout() << "to sarray" << '\n'; | |
1092 assert(0); | |
1093 } | |
1094 else { | |
1095 assert(0); | |
1096 } | |
1097 } | |
1098 else if (from->ty == Tpointer) { | |
1099 if (to->ty == Tpointer || to->ty == Tclass) { | |
1100 llvm::Value* src = u->getValue(); | |
1101 //Logger::cout() << *src << '|' << *totype << '\n'; | |
1102 e->val = new llvm::BitCastInst(src, totype, "tmp", p->scopebb()); | |
1103 } | |
1104 else if (to->isintegral()) { | |
1105 e->val = new llvm::PtrToIntInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1106 } | |
1107 else | |
1108 assert(0); | |
1109 e->type = elem::VAL; | |
1110 } | |
1111 else { | |
1112 assert(0); | |
1113 } | |
1114 delete u; | |
1115 return e; | |
1116 } | |
1117 | |
1118 ////////////////////////////////////////////////////////////////////////////////////////// | |
1119 | |
1120 elem* SymOffExp::toElem(IRState* p) | |
1121 { | |
1122 Logger::print("SymOffExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1123 LOG_SCOPE; | |
1124 elem* e = 0; | |
1125 if (VarDeclaration* vd = var->isVarDeclaration()) | |
1126 { | |
1127 Logger::println("VarDeclaration"); | |
1128 if (vd->type->ty == Tstruct && !(type->ty == Tpointer && type->next == vd->type)) { | |
1129 TypeStruct* vdt = (TypeStruct*)vd->type; | |
1130 e = new elem; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1131 std::vector<unsigned> dst(1,0); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1132 vdt->sym->offsetToIndex(offset, dst); |
1 | 1133 llvm::Value* ptr = vd->llvmValue; |
1134 assert(ptr); | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1135 e->mem = LLVM_DtoGEP(ptr,dst,"tmp",p->scopebb()); |
1 | 1136 e->type = elem::VAL; |
1137 e->field = true; | |
1138 } | |
1139 else if (vd->type->ty == Tsarray) { | |
1140 /*e = new elem; | |
1141 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1142 e->val = new llvm::GetElementPtrInst(vd->llvmValue,idx0,idx0,"tmp",p->scopebb());*/ | |
1143 e = new elem; | |
1144 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1145 //llvm::Value* idx1 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
6 | 1146 e->mem = LLVM_DtoGEP(vd->llvmValue,idx0,idx0,"tmp",p->scopebb()); |
1 | 1147 e->type = elem::VAL; |
1148 } | |
1149 else if (offset == 0) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1150 /*if (!vd->llvmValue) |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1151 vd->toObjFile();*/ |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1152 assert(vd->llvmValue); |
1 | 1153 e = new elem; |
1154 e->mem = vd->llvmValue; | |
1155 //e->vardecl = vd; | |
1156 e->type = elem::VAL; | |
1157 } | |
1158 else { | |
1159 assert(0); | |
1160 } | |
1161 } | |
1162 else if (FuncDeclaration* fd = var->isFuncDeclaration()) | |
1163 { | |
1164 Logger::println("FuncDeclaration"); | |
1165 e = new elem; | |
1166 if (fd->llvmValue == 0) | |
1167 fd->toObjFile(); | |
1168 e->val = fd->llvmValue; | |
1169 //e->aspointer = true; | |
1170 e->type = elem::FUNC; | |
1171 } | |
1172 assert(e != 0); | |
1173 assert(e->type != elem::NONE); | |
1174 return e; | |
1175 } | |
1176 | |
1177 ////////////////////////////////////////////////////////////////////////////////////////// | |
1178 | |
1179 elem* PtrExp::toElem(IRState* p) | |
1180 { | |
1181 Logger::print("PtrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1182 LOG_SCOPE; | |
1183 elem* e = new elem; | |
1184 elem* a = e1->toElem(p); | |
1185 | |
1186 if (a->mem) | |
1187 Logger::cout() << "mem: " << *a->mem << '\n'; | |
1188 if (a->val) | |
1189 Logger::cout() << "val: " << *a->val << '\n'; | |
1190 | |
1191 if (a->field) | |
1192 e->mem = a->mem; | |
1193 else | |
1194 e->mem = a->getValue(); | |
1195 e->type = elem::VAR; | |
1196 | |
1197 delete a; | |
1198 return e; | |
1199 } | |
1200 | |
1201 ////////////////////////////////////////////////////////////////////////////////////////// | |
1202 | |
1203 elem* DotVarExp::toElem(IRState* p) | |
1204 { | |
1205 Logger::print("DotVarExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1206 LOG_SCOPE; | |
1207 elem* e = new elem; | |
1208 | |
1209 elem* l = e1->toElem(p); | |
1210 | |
1211 Logger::print("e1->type=%s\n", e1->type->toChars()); | |
1212 | |
1213 if (VarDeclaration* vd = var->isVarDeclaration()) { | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1214 std::vector<unsigned> vdoffsets(1,0); |
1 | 1215 llvm::Value* src = 0; |
1216 if (e1->type->ty == Tpointer) { | |
1217 assert(e1->type->next->ty == Tstruct); | |
1218 TypeStruct* ts = (TypeStruct*)e1->type->next; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1219 ts->sym->offsetToIndex(vd->offset, vdoffsets); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1220 Logger::println("Struct member offset:%d", vd->offset); |
1 | 1221 src = l->val; |
1222 } | |
1223 else if (e1->type->ty == Tclass) { | |
1224 TypeClass* tc = (TypeClass*)e1->type; | |
1225 Logger::println("Class member offset: %d", vd->offset); | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1226 tc->sym->offsetToIndex(vd->offset, vdoffsets); |
1 | 1227 src = l->getValue(); |
1228 } | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1229 assert(vdoffsets.size() != 1); |
1 | 1230 assert(src != 0); |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1231 llvm::Value* arrptr = LLVM_DtoGEP(src,vdoffsets,"tmp",p->scopebb()); |
1 | 1232 e->mem = arrptr; |
1233 Logger::cout() << "mem: " << *e->mem << '\n'; | |
1234 e->type = elem::VAR; | |
1235 } | |
1236 else if (FuncDeclaration* fdecl = var->isFuncDeclaration()) | |
1237 { | |
1238 if (fdecl->llvmValue == 0) | |
1239 { | |
1240 fdecl->toObjFile(); | |
1241 } | |
1242 | |
1243 llvm::Value* funcval = fdecl->llvmValue; | |
1244 e->arg = l->getValue(); | |
1245 | |
1246 // virtual call | |
1247 if (fdecl->isVirtual()) { | |
1248 assert(fdecl->vtblIndex > 0); | |
1249 assert(e1->type->ty == Tclass); | |
1250 | |
1251 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1252 llvm::Value* vtblidx = llvm::ConstantInt::get(llvm::Type::Int32Ty, (size_t)fdecl->vtblIndex, false); | |
6 | 1253 funcval = LLVM_DtoGEP(e->arg, zero, zero, "tmp", p->scopebb()); |
1 | 1254 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); |
6 | 1255 funcval = LLVM_DtoGEP(funcval, zero, vtblidx, "tmp", p->scopebb()); |
1 | 1256 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); |
9 | 1257 assert(funcval->getType() == fdecl->llvmValue->getType()); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
1258 e->callconv = LLVM_DtoCallingConv(fdecl->linkage); |
1 | 1259 } |
1260 e->val = funcval; | |
1261 e->type = elem::VAL; | |
1262 } | |
1263 else { | |
1264 printf("unknown: %s\n", var->toChars()); | |
1265 assert(0); | |
1266 } | |
1267 | |
1268 delete l; | |
1269 | |
1270 return e; | |
1271 } | |
1272 | |
1273 ////////////////////////////////////////////////////////////////////////////////////////// | |
1274 | |
1275 elem* ThisExp::toElem(IRState* p) | |
1276 { | |
1277 Logger::print("ThisExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1278 LOG_SCOPE; | |
1279 elem* e = new elem; | |
1280 | |
1281 if (VarDeclaration* vd = var->isVarDeclaration()) { | |
1282 assert(vd->llvmValue == 0); | |
1283 | |
1284 llvm::Function* fn = p->topfunc(); | |
1285 assert(fn); | |
1286 | |
1287 TypeFunction* tf = p->topfunctype(); | |
1288 assert(tf); | |
1289 | |
1290 llvm::Value* v = 0; | |
1291 if (tf->llvmRetInPtr) | |
1292 v = ++fn->arg_begin(); | |
1293 else | |
1294 v = fn->arg_begin(); | |
1295 assert(v); | |
1296 | |
1297 e->val = v; | |
1298 e->type = elem::VAL; | |
1299 } | |
1300 else { | |
1301 assert(0); | |
1302 } | |
1303 | |
1304 return e; | |
1305 } | |
1306 | |
1307 ////////////////////////////////////////////////////////////////////////////////////////// | |
1308 | |
1309 elem* AddrExp::toElem(IRState* p) | |
1310 { | |
1311 Logger::print("AddrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1312 LOG_SCOPE; | |
1313 elem* e = e1->toElem(p); | |
1314 e->field = true; | |
1315 return e; | |
1316 } | |
1317 | |
1318 ////////////////////////////////////////////////////////////////////////////////////////// | |
1319 | |
1320 elem* StructLiteralExp::toElem(IRState* p) | |
1321 { | |
1322 Logger::print("StructLiteralExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1323 LOG_SCOPE; | |
1324 elem* e = new elem; | |
1325 | |
1326 // if there is no lval, this must be a static initializer for a global. correct? | |
1327 if (p->lvals.empty()) | |
1328 { | |
1329 // TODO | |
1330 assert(0); | |
1331 } | |
1332 // otherwise write directly in the lvalue | |
1333 else | |
1334 { | |
1335 llvm::Value* sptr = p->toplval(); | |
1336 assert(sptr); | |
1337 | |
1338 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1339 | |
1340 unsigned n = elements->dim; | |
1341 for (unsigned i=0; i<n; ++i) | |
1342 { | |
1343 llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, i, false); | |
6 | 1344 llvm::Value* arrptr = LLVM_DtoGEP(sptr,zero,offset,"tmp",p->scopebb()); |
1 | 1345 |
1346 Expression* vx = (Expression*)elements->data[i]; | |
1347 if (vx != 0) { | |
1348 elem* ve = vx->toElem(p); | |
1349 //Logger::cout() << *ve->val << " | " << *arrptr << '\n'; | |
1350 new llvm::StoreInst(ve->getValue(), arrptr, p->scopebb()); | |
1351 delete ve; | |
1352 } | |
1353 else { | |
1354 assert(0); | |
1355 } | |
1356 } | |
1357 } | |
1358 | |
1359 e->inplace = true; | |
1360 | |
1361 return e; | |
1362 } | |
1363 | |
1364 ////////////////////////////////////////////////////////////////////////////////////////// | |
1365 | |
1366 elem* IndexExp::toElem(IRState* p) | |
1367 { | |
1368 Logger::print("IndexExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1369 LOG_SCOPE; | |
1370 | |
1371 elem* e = new elem; | |
1372 | |
1373 elem* l = e1->toElem(p); | |
1374 | |
1375 p->arrays.push_back(l->mem); // if $ is used it must be an array so this is fine. | |
1376 elem* r = e2->toElem(p); | |
1377 p->arrays.pop_back(); | |
1378 | |
1379 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1380 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1381 | |
1382 llvm::Value* arrptr = 0; | |
1383 if (e1->type->ty == Tpointer) { | |
1384 arrptr = new llvm::GetElementPtrInst(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
1385 } | |
1386 else if (e1->type->ty == Tsarray) { | |
6 | 1387 arrptr = LLVM_DtoGEP(l->mem, zero, r->getValue(),"tmp",p->scopebb()); |
1 | 1388 } |
1389 else if (e1->type->ty == Tarray) { | |
6 | 1390 arrptr = LLVM_DtoGEP(l->mem,zero,one,"tmp",p->scopebb()); |
1 | 1391 arrptr = new llvm::LoadInst(arrptr,"tmp",p->scopebb()); |
1392 arrptr = new llvm::GetElementPtrInst(arrptr,r->getValue(),"tmp",p->scopebb()); | |
1393 } | |
1394 assert(arrptr); | |
1395 | |
1396 e->mem = arrptr; | |
1397 e->type = elem::VAR; | |
1398 | |
1399 delete l; | |
1400 delete r; | |
1401 | |
1402 return e; | |
1403 } | |
1404 | |
1405 ////////////////////////////////////////////////////////////////////////////////////////// | |
1406 | |
1407 elem* SliceExp::toElem(IRState* p) | |
1408 { | |
1409 Logger::print("SliceExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1410 LOG_SCOPE; | |
1411 | |
1412 elem* v = e1->toElem(p); | |
1413 | |
1414 elem* e = new elem; | |
1415 assert(v->mem); | |
1416 e->type = elem::SLICE; | |
1417 | |
1418 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1419 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1420 | |
1421 // partial slice | |
1422 if (lwr) | |
1423 { | |
1424 assert(upr); | |
1425 p->arrays.push_back(v->mem); | |
1426 elem* lo = lwr->toElem(p); | |
1427 | |
1428 bool lwr_is_zero = false; | |
1429 if (lo->type == elem::CONST) | |
1430 { | |
1431 assert(lo->val); | |
1432 assert(llvm::isa<llvm::ConstantInt>(lo->val)); | |
1433 | |
1434 if (e1->type->ty == Tpointer) { | |
1435 e->mem = v->getValue(); | |
1436 } | |
1437 else if (e1->type->ty == Tarray) { | |
6 | 1438 llvm::Value* tmp = LLVM_DtoGEP(v->mem,zero,one,"tmp",p->scopebb()); |
1 | 1439 e->mem = new llvm::LoadInst(tmp,"tmp",p->scopebb()); |
1440 } | |
1441 else | |
1442 assert(e->mem); | |
1443 | |
1444 llvm::ConstantInt* c = llvm::cast<llvm::ConstantInt>(lo->val); | |
1445 if (!(lwr_is_zero = c->isZero())) { | |
1446 e->mem = new llvm::GetElementPtrInst(e->mem,lo->val,"tmp",p->scopebb()); | |
1447 } | |
1448 } | |
1449 else | |
1450 { | |
6 | 1451 llvm::Value* tmp = LLVM_DtoGEP(v->mem,zero,one,"tmp",p->scopebb()); |
1 | 1452 tmp = new llvm::LoadInst(tmp,"tmp",p->scopebb()); |
1453 e->mem = new llvm::GetElementPtrInst(tmp,lo->getValue(),"tmp",p->scopebb()); | |
1454 } | |
1455 | |
1456 elem* up = upr->toElem(p); | |
1457 p->arrays.pop_back(); | |
1458 | |
1459 if (up->type == elem::CONST) | |
1460 { | |
1461 assert(up->val); | |
1462 assert(llvm::isa<llvm::ConstantInt>(up->val)); | |
1463 if (lwr_is_zero) { | |
1464 e->arg = up->val; | |
1465 } | |
1466 else { | |
1467 if (lo->type == elem::CONST) { | |
1468 llvm::Constant* clo = llvm::cast<llvm::Constant>(lo->val); | |
1469 llvm::Constant* cup = llvm::cast<llvm::Constant>(up->val); | |
1470 e->arg = llvm::ConstantExpr::getSub(cup, clo); | |
1471 } | |
1472 else { | |
1473 e->arg = llvm::BinaryOperator::createSub(up->val, lo->getValue(), "tmp", p->scopebb()); | |
1474 } | |
1475 } | |
1476 } | |
1477 else | |
1478 { | |
1479 if (lwr_is_zero) { | |
1480 e->arg = up->getValue(); | |
1481 } | |
1482 else { | |
1483 e->arg = llvm::BinaryOperator::createSub(up->getValue(), lo->getValue(), "tmp", p->scopebb()); | |
1484 } | |
1485 } | |
1486 | |
1487 delete lo; | |
1488 delete up; | |
1489 } | |
1490 // full slice | |
1491 else | |
1492 { | |
1493 e->mem = v->mem; | |
1494 } | |
1495 | |
1496 delete v; | |
1497 | |
1498 return e; | |
1499 } | |
1500 | |
1501 ////////////////////////////////////////////////////////////////////////////////////////// | |
1502 | |
1503 elem* CmpExp::toElem(IRState* p) | |
1504 { | |
1505 Logger::print("CmpExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1506 LOG_SCOPE; | |
1507 | |
1508 elem* e = new elem; | |
1509 | |
1510 elem* l = e1->toElem(p); | |
1511 elem* r = e2->toElem(p); | |
1512 | |
1513 assert(e1->type == e2->type); | |
1514 | |
1515 Type* t = e1->type; | |
1516 | |
1517 if (t->isintegral()) | |
1518 { | |
1519 llvm::ICmpInst::Predicate cmpop; | |
1520 switch(op) | |
1521 { | |
1522 case TOKlt: | |
1523 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_ULT : llvm::ICmpInst::ICMP_SLT; | |
1524 break; | |
1525 case TOKle: | |
1526 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_ULE : llvm::ICmpInst::ICMP_SLE; | |
1527 break; | |
1528 case TOKgt: | |
1529 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_UGT : llvm::ICmpInst::ICMP_SGT; | |
1530 break; | |
1531 case TOKge: | |
1532 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_UGE : llvm::ICmpInst::ICMP_SGE; | |
1533 break; | |
1534 default: | |
1535 assert(0); | |
1536 } | |
1537 e->val = new llvm::ICmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1538 } | |
1539 else if (t->isfloating()) | |
1540 { | |
1541 llvm::FCmpInst::Predicate cmpop; | |
1542 switch(op) | |
1543 { | |
1544 case TOKlt: | |
1545 cmpop = llvm::FCmpInst::FCMP_OLT;break; | |
1546 case TOKle: | |
1547 cmpop = llvm::FCmpInst::FCMP_OLE;break; | |
1548 case TOKgt: | |
1549 cmpop = llvm::FCmpInst::FCMP_OGT;break; | |
1550 case TOKge: | |
1551 cmpop = llvm::FCmpInst::FCMP_OGE;break; | |
1552 case TOKunord: | |
1553 cmpop = llvm::FCmpInst::FCMP_UNO;break; | |
1554 case TOKule: | |
1555 cmpop = llvm::FCmpInst::FCMP_ULE;break; | |
1556 case TOKul: | |
1557 cmpop = llvm::FCmpInst::FCMP_ULT;break; | |
1558 case TOKuge: | |
1559 cmpop = llvm::FCmpInst::FCMP_UGE;break; | |
1560 case TOKug: | |
1561 cmpop = llvm::FCmpInst::FCMP_UGT;break; | |
1562 case TOKue: | |
1563 cmpop = llvm::FCmpInst::FCMP_UEQ;break; | |
1564 case TOKlg: | |
1565 cmpop = llvm::FCmpInst::FCMP_ONE;break; | |
1566 case TOKleg: | |
1567 cmpop = llvm::FCmpInst::FCMP_ORD;break; | |
1568 | |
1569 default: | |
1570 assert(0); | |
1571 } | |
1572 e->val = new llvm::FCmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1573 } | |
1574 else | |
1575 { | |
1576 assert(0 && "Unsupported CmpExp type"); | |
1577 } | |
1578 | |
1579 delete l; | |
1580 delete r; | |
1581 | |
1582 e->type = elem::VAL; | |
1583 | |
1584 return e; | |
1585 } | |
1586 | |
1587 ////////////////////////////////////////////////////////////////////////////////////////// | |
1588 | |
1589 elem* EqualExp::toElem(IRState* p) | |
1590 { | |
1591 Logger::print("EqualExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1592 LOG_SCOPE; | |
1593 | |
1594 elem* e = new elem; | |
1595 | |
1596 elem* l = e1->toElem(p); | |
1597 elem* r = e2->toElem(p); | |
1598 | |
1599 assert(e1->type == e2->type); | |
1600 | |
1601 Type* t = e1->type; | |
1602 | |
1603 if (t->isintegral() || t->ty == Tpointer) | |
1604 { | |
1605 llvm::ICmpInst::Predicate cmpop; | |
1606 switch(op) | |
1607 { | |
1608 case TOKequal: | |
1609 cmpop = llvm::ICmpInst::ICMP_EQ; | |
1610 break; | |
1611 case TOKnotequal: | |
1612 cmpop = llvm::ICmpInst::ICMP_NE; | |
1613 break; | |
1614 default: | |
1615 assert(0); | |
1616 } | |
1617 e->val = new llvm::ICmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1618 } | |
1619 else if (t->isfloating()) | |
1620 { | |
1621 llvm::FCmpInst::Predicate cmpop; | |
1622 switch(op) | |
1623 { | |
1624 case TOKequal: | |
1625 cmpop = llvm::FCmpInst::FCMP_OEQ; | |
1626 break; | |
1627 case TOKnotequal: | |
1628 cmpop = llvm::FCmpInst::FCMP_UNE; | |
1629 break; | |
1630 default: | |
1631 assert(0); | |
1632 } | |
1633 e->val = new llvm::FCmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1634 } | |
1635 else if (t->ty == Tarray) | |
1636 { | |
1637 // array comparison invokes the typeinfo runtime | |
1638 assert(0); | |
1639 } | |
1640 else | |
1641 { | |
1642 assert(0 && "Unsupported EqualExp type"); | |
1643 } | |
1644 | |
1645 delete l; | |
1646 delete r; | |
1647 | |
1648 e->type = elem::VAL; | |
1649 | |
1650 return e; | |
1651 } | |
1652 | |
1653 ////////////////////////////////////////////////////////////////////////////////////////// | |
1654 | |
1655 elem* PostExp::toElem(IRState* p) | |
1656 { | |
1657 Logger::print("PostExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1658 LOG_SCOPE; | |
1659 | |
1660 elem* l = e1->toElem(p); | |
1661 elem* r = e2->toElem(p); | |
1662 | |
1663 elem* e = new elem; | |
1664 e->mem = l->mem; | |
1665 e->val = l->getValue(); | |
1666 e->type = elem::VAL; | |
1667 | |
1668 llvm::Value* val = e->val; | |
1669 llvm::Value* post = 0; | |
1670 | |
1671 if (e1->type->isintegral()) | |
1672 { | |
1673 assert(e2->type->isintegral()); | |
1674 llvm::Value* one = llvm::ConstantInt::get(val->getType(), 1, !e2->type->isunsigned()); | |
1675 if (op == TOKplusplus) { | |
1676 post = llvm::BinaryOperator::createAdd(val,one,"tmp",p->scopebb()); | |
1677 } | |
1678 else if (op == TOKminusminus) { | |
1679 post = llvm::BinaryOperator::createSub(val,one,"tmp",p->scopebb()); | |
1680 } | |
1681 } | |
1682 else if (e1->type->ty == Tpointer) | |
1683 { | |
1684 assert(e2->type->isintegral()); | |
1685 llvm::Constant* minusone = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)-1,true); | |
1686 llvm::Constant* plusone = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)1,false); | |
1687 llvm::Constant* whichone = (op == TOKplusplus) ? plusone : minusone; | |
1688 post = new llvm::GetElementPtrInst(val, whichone, "tmp", p->scopebb()); | |
1689 } | |
1690 else if (e1->type->isfloating()) | |
1691 { | |
1692 assert(e2->type->isfloating()); | |
1693 llvm::Value* one = llvm::ConstantFP::get(val->getType(), 1.0f); | |
1694 if (op == TOKplusplus) { | |
1695 post = llvm::BinaryOperator::createAdd(val,one,"tmp",p->scopebb()); | |
1696 } | |
1697 else if (op == TOKminusminus) { | |
1698 post = llvm::BinaryOperator::createSub(val,one,"tmp",p->scopebb()); | |
1699 } | |
1700 } | |
1701 else | |
1702 assert(post); | |
1703 | |
1704 //llvm::Value* tostore = l->storeVal ? l->storeVal : l->val; | |
1705 new llvm::StoreInst(post,l->mem,p->scopebb()); | |
1706 | |
1707 delete l; | |
1708 delete r; | |
1709 | |
1710 return e; | |
1711 } | |
1712 | |
1713 ////////////////////////////////////////////////////////////////////////////////////////// | |
1714 | |
1715 elem* NewExp::toElem(IRState* p) | |
1716 { | |
1717 Logger::print("NewExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1718 LOG_SCOPE; | |
1719 | |
1720 assert(!thisexp); | |
1721 assert(!newargs); | |
1722 assert(newtype); | |
1723 //assert(!arguments); | |
1724 //assert(!member); | |
1725 assert(!allocator); | |
1726 | |
1727 elem* e = new elem; | |
1728 | |
1729 const llvm::Type* t = LLVM_DtoType(newtype); | |
1730 | |
1731 if (onstack) { | |
1732 assert(newtype->ty == Tclass); | |
1733 e->mem = new llvm::AllocaInst(t->getContainedType(0),"tmp",p->topallocapoint()); | |
1734 } | |
1735 else { | |
1736 if (newtype->ty == Tclass) { | |
1737 e->mem = new llvm::MallocInst(t->getContainedType(0),"tmp",p->scopebb()); | |
1738 } | |
1739 else if (newtype->ty == Tarray) { | |
1740 t = LLVM_DtoType(newtype->next); | |
1741 assert(arguments); | |
1742 if (arguments->dim == 1) { | |
1743 elem* sz = ((Expression*)arguments->data[0])->toElem(p); | |
1744 llvm::Value* dimval = sz->getValue(); | |
1745 llvm::Value* usedimval = dimval; | |
1746 if (dimval->getType() != llvm::Type::Int32Ty) | |
1747 usedimval = new llvm::TruncInst(dimval, llvm::Type::Int32Ty,"tmp",p->scopebb()); | |
1748 e->mem = new llvm::MallocInst(t,usedimval,"tmp",p->scopebb()); | |
1749 | |
1750 LLVM_DtoSetArray(p->toplval(), dimval, e->mem); | |
1751 delete sz; | |
1752 } | |
1753 else { | |
1754 assert(0); | |
1755 } | |
1756 } | |
1757 else { | |
1758 e->mem = new llvm::MallocInst(t,"tmp",p->scopebb()); | |
1759 } | |
1760 } | |
1761 | |
1762 if (newtype->ty == Tclass) { | |
1763 // first apply the static initializer | |
1764 assert(e->mem); | |
1765 LLVM_DtoInitClass((TypeClass*)newtype, e->mem); | |
1766 | |
1767 // then call constructor | |
1768 if (arguments) { | |
1769 std::vector<llvm::Value*> ctorargs; | |
1770 ctorargs.push_back(e->mem); | |
1771 for (size_t i=0; i<arguments->dim; ++i) | |
1772 { | |
1773 Expression* ex = (Expression*)arguments->data[i]; | |
1774 Logger::println("arg=%s", ex->toChars()); | |
1775 elem* exe = ex->toElem(p); | |
1776 assert(exe->getValue()); | |
1777 ctorargs.push_back(exe->getValue()); | |
1778 delete exe; | |
1779 } | |
1780 assert(member); | |
1781 assert(member->llvmValue); | |
1782 new llvm::CallInst(member->llvmValue, ctorargs.begin(), ctorargs.end(), "", p->scopebb()); | |
1783 } | |
1784 } | |
1785 else if (newtype->ty == Tstruct) { | |
1786 TypeStruct* ts = (TypeStruct*)newtype; | |
1787 if (ts->isZeroInit()) { | |
1788 LLVM_DtoStructZeroInit(ts,e->mem); | |
1789 } | |
1790 else { | |
1791 LLVM_DtoStructCopy(ts,e->mem,ts->llvmInit); | |
1792 } | |
1793 } | |
1794 | |
1795 e->inplace = true; | |
1796 e->type = elem::VAR; | |
1797 | |
1798 return e; | |
1799 } | |
1800 | |
1801 ////////////////////////////////////////////////////////////////////////////////////////// | |
1802 | |
1803 elem* DeleteExp::toElem(IRState* p) | |
1804 { | |
1805 Logger::print("DeleteExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1806 LOG_SCOPE; | |
1807 | |
1808 //assert(e1->type->ty != Tclass); | |
1809 | |
1810 elem* v = e1->toElem(p); | |
1811 llvm::Value* val = v->getValue(); | |
1812 llvm::Value* ldval = 0; | |
1813 | |
1814 const llvm::Type* t = val->getType(); | |
1815 llvm::Constant* z = llvm::Constant::getNullValue(t); | |
1816 | |
1817 if (e1->type->ty == Tpointer) { | |
1818 ldval = v->getValue(); | |
1819 new llvm::FreeInst(ldval, p->scopebb()); | |
1820 | |
1821 Logger::cout() << *z << '\n'; | |
1822 Logger::cout() << *val << '\n'; | |
1823 new llvm::StoreInst(z, v->mem, p->scopebb()); | |
1824 } | |
1825 else if (e1->type->ty == Tclass) { | |
1826 TypeClass* tc = (TypeClass*)e1->type; | |
1827 LLVM_DtoCallClassDtors(tc, val); | |
1828 | |
1829 if (v->vardecl && !v->vardecl->onstack) { | |
1830 new llvm::FreeInst(val, p->scopebb()); | |
1831 } | |
1832 new llvm::StoreInst(z, v->mem, p->scopebb()); | |
1833 } | |
1834 else if (e1->type->ty == Tarray) { | |
1835 // must be on the heap (correct?) | |
1836 ldval = v->getValue(); | |
1837 | |
1838 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1839 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1840 | |
6 | 1841 llvm::Value* ptr = LLVM_DtoGEP(ldval,zero,one,"tmp",p->scopebb()); |
1 | 1842 ptr = new llvm::LoadInst(ptr,"tmp",p->scopebb()); |
1843 new llvm::FreeInst(ptr, p->scopebb()); | |
1844 LLVM_DtoNullArray(val); | |
1845 } | |
1846 else { | |
1847 assert(0); | |
1848 } | |
1849 | |
1850 delete v; | |
1851 | |
1852 // this expression produces no useful data | |
1853 return 0; | |
1854 } | |
1855 | |
1856 ////////////////////////////////////////////////////////////////////////////////////////// | |
1857 | |
1858 elem* ArrayLengthExp::toElem(IRState* p) | |
1859 { | |
1860 Logger::print("ArrayLengthExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1861 LOG_SCOPE; | |
1862 | |
1863 elem* e = new elem; | |
1864 elem* u = e1->toElem(p); | |
1865 | |
1866 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
6 | 1867 llvm::Value* ptr = LLVM_DtoGEP(u->mem,zero,zero,"tmp",p->scopebb()); |
1 | 1868 e->val = new llvm::LoadInst(ptr, "tmp", p->scopebb()); |
1869 e->type = elem::VAL; | |
1870 | |
1871 delete u; | |
1872 | |
1873 return e; | |
1874 } | |
1875 | |
1876 ////////////////////////////////////////////////////////////////////////////////////////// | |
1877 | |
1878 elem* AssertExp::toElem(IRState* p) | |
1879 { | |
1880 Logger::print("AssertExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1881 LOG_SCOPE; | |
1882 | |
1883 elem* e = new elem; | |
1884 elem* u = e1->toElem(p); | |
1885 elem* m = msg ? msg->toElem(p) : 0; | |
1886 | |
1887 std::vector<llvm::Value*> llargs; | |
1888 llargs.resize(3); | |
1889 llargs[0] = LLVM_DtoBoolean(u->getValue()); | |
1890 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, loc.linnum, false); | |
1891 llargs[2] = m ? m->val : llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty)); | |
1892 | |
1893 delete m; | |
1894 delete u; | |
1895 | |
1896 //Logger::cout() << *llargs[0] << '|' << *llargs[1] << '\n'; | |
1897 | |
1898 llvm::Function* fn = LLVM_D_GetRuntimeFunction(p->module, "_d_assert"); | |
1899 assert(fn); | |
1900 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", p->scopebb()); | |
1901 call->setCallingConv(llvm::CallingConv::C); | |
1902 | |
1903 return e; | |
1904 } | |
1905 | |
1906 ////////////////////////////////////////////////////////////////////////////////////////// | |
1907 | |
1908 elem* NotExp::toElem(IRState* p) | |
1909 { | |
1910 Logger::print("NotExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1911 LOG_SCOPE; | |
1912 | |
1913 elem* e = new elem; | |
1914 elem* u = e1->toElem(p); | |
1915 | |
1916 llvm::Value* b = LLVM_DtoBoolean(u->getValue()); | |
1917 | |
1918 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int1Ty, 0, true); | |
1919 e->val = new llvm::ICmpInst(llvm::ICmpInst::ICMP_EQ,b,zero,"tmp",p->scopebb()); | |
1920 e->type = elem::VAL; | |
1921 | |
1922 delete u; | |
1923 | |
1924 return e; | |
1925 } | |
1926 | |
1927 ////////////////////////////////////////////////////////////////////////////////////////// | |
1928 | |
1929 elem* AndAndExp::toElem(IRState* p) | |
1930 { | |
1931 Logger::print("AndAndExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1932 LOG_SCOPE; | |
1933 | |
1934 // allocate a temporary for the final result. failed to come up with a better way :/ | |
1935 llvm::Value* resval = 0; | |
1936 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
1937 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"andandtmp",p->topallocapoint()); | |
1938 | |
1939 elem* e = new elem; | |
1940 elem* u = e1->toElem(p); | |
1941 | |
1942 llvm::BasicBlock* oldend = p->scopeend(); | |
1943 llvm::BasicBlock* andand = new llvm::BasicBlock("andand", gIR->topfunc(), oldend); | |
1944 llvm::BasicBlock* andandend = new llvm::BasicBlock("andandend", gIR->topfunc(), oldend); | |
1945 | |
1946 llvm::Value* ubool = LLVM_DtoBoolean(u->getValue()); | |
1947 new llvm::StoreInst(ubool,resval,p->scopebb()); | |
1948 new llvm::BranchInst(andand,andandend,ubool,p->scopebb()); | |
1949 | |
1950 p->scope() = IRScope(andand, andandend); | |
1951 elem* v = e2->toElem(p); | |
1952 | |
1953 llvm::Value* vbool = LLVM_DtoBoolean(v->getValue()); | |
1954 llvm::Value* uandvbool = llvm::BinaryOperator::create(llvm::BinaryOperator::And, ubool, vbool,"tmp",p->scopebb()); | |
1955 new llvm::StoreInst(uandvbool,resval,p->scopebb()); | |
1956 new llvm::BranchInst(andandend,p->scopebb()); | |
1957 | |
1958 delete u; | |
1959 delete v; | |
1960 | |
1961 p->scope() = IRScope(andandend, oldend); | |
1962 | |
1963 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
1964 e->type = elem::VAL; | |
1965 | |
1966 return e; | |
1967 } | |
1968 | |
1969 ////////////////////////////////////////////////////////////////////////////////////////// | |
1970 | |
1971 elem* OrOrExp::toElem(IRState* p) | |
1972 { | |
1973 Logger::print("OrOrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1974 LOG_SCOPE; | |
1975 | |
1976 // allocate a temporary for the final result. failed to come up with a better way :/ | |
1977 llvm::Value* resval = 0; | |
1978 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
1979 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"orortmp",p->topallocapoint()); | |
1980 | |
1981 elem* e = new elem; | |
1982 elem* u = e1->toElem(p); | |
1983 | |
1984 llvm::BasicBlock* oldend = p->scopeend(); | |
1985 llvm::BasicBlock* oror = new llvm::BasicBlock("oror", gIR->topfunc(), oldend); | |
1986 llvm::BasicBlock* ororend = new llvm::BasicBlock("ororend", gIR->topfunc(), oldend); | |
1987 | |
1988 llvm::Value* ubool = LLVM_DtoBoolean(u->getValue()); | |
1989 new llvm::StoreInst(ubool,resval,p->scopebb()); | |
1990 new llvm::BranchInst(ororend,oror,ubool,p->scopebb()); | |
1991 | |
1992 p->scope() = IRScope(oror, ororend); | |
1993 elem* v = e2->toElem(p); | |
1994 | |
1995 llvm::Value* vbool = LLVM_DtoBoolean(v->getValue()); | |
1996 new llvm::StoreInst(vbool,resval,p->scopebb()); | |
1997 new llvm::BranchInst(ororend,p->scopebb()); | |
1998 | |
1999 delete u; | |
2000 delete v; | |
2001 | |
2002 p->scope() = IRScope(ororend, oldend); | |
2003 | |
2004 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
2005 e->type = elem::VAL; | |
2006 | |
2007 return e; | |
2008 } | |
2009 | |
2010 ////////////////////////////////////////////////////////////////////////////////////////// | |
2011 | |
2012 #define BinBitExp(X,Y) \ | |
2013 elem* X##Exp::toElem(IRState* p) \ | |
2014 { \ | |
2015 Logger::print("%sExp::toElem: %s | %s\n", #X, toChars(), type->toChars()); \ | |
2016 LOG_SCOPE; \ | |
2017 elem* e = new elem; \ | |
2018 elem* u = e1->toElem(p); \ | |
2019 elem* v = e2->toElem(p); \ | |
2020 e->val = llvm::BinaryOperator::create(llvm::Instruction::Y, u->getValue(), v->getValue(), "tmp", p->scopebb()); \ | |
2021 e->type = elem::VAL; \ | |
2022 delete u; \ | |
2023 delete v; \ | |
2024 return e; \ | |
2025 } \ | |
2026 \ | |
2027 elem* X##AssignExp::toElem(IRState* p) \ | |
2028 { \ | |
2029 Logger::print("%sAssignExp::toElem: %s | %s\n", #X, toChars(), type->toChars()); \ | |
2030 LOG_SCOPE; \ | |
2031 elem* u = e1->toElem(p); \ | |
2032 elem* v = e2->toElem(p); \ | |
2033 llvm::Value* tmp = llvm::BinaryOperator::create(llvm::Instruction::Y, u->getValue(), v->getValue(), "tmp", p->scopebb()); \ | |
2034 Logger::cout() << *tmp << '|' << *u->mem << '\n'; \ | |
2035 new llvm::StoreInst(LLVM_DtoPointedType(u->mem, tmp), u->mem, p->scopebb()); \ | |
2036 delete u; \ | |
2037 delete v; \ | |
2038 elem* e = new elem; \ | |
2039 e->mem = u->mem; \ | |
2040 e->type = elem::VAR; \ | |
2041 return e; \ | |
2042 } | |
2043 | |
2044 BinBitExp(And,And); | |
2045 BinBitExp(Or,Or); | |
2046 BinBitExp(Xor,Xor); | |
2047 BinBitExp(Shl,Shl); | |
2048 BinBitExp(Shr,AShr); | |
2049 BinBitExp(Ushr,LShr); | |
2050 | |
2051 ////////////////////////////////////////////////////////////////////////////////////////// | |
2052 | |
2053 elem* HaltExp::toElem(IRState* p) | |
2054 { | |
2055 Logger::print("HaltExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2056 LOG_SCOPE; | |
2057 | |
2058 std::vector<llvm::Value*> llargs; | |
2059 llargs.resize(3); | |
2060 llargs[0] = llvm::ConstantInt::get(llvm::Type::Int1Ty, 0, false); | |
2061 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, loc.linnum, false); | |
2062 llargs[2] = llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty)); | |
2063 | |
2064 //Logger::cout() << *llargs[0] << '|' << *llargs[1] << '\n'; | |
2065 | |
2066 llvm::Function* fn = LLVM_D_GetRuntimeFunction(p->module, "_d_assert"); | |
2067 assert(fn); | |
2068 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", p->scopebb()); | |
2069 call->setCallingConv(llvm::CallingConv::C); | |
2070 | |
2071 //new llvm::UnreachableInst(p->scopebb()); | |
2072 | |
2073 return 0; | |
2074 } | |
2075 | |
2076 ////////////////////////////////////////////////////////////////////////////////////////// | |
2077 | |
2078 elem* DelegateExp::toElem(IRState* p) | |
2079 { | |
2080 Logger::print("DelegateExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2081 LOG_SCOPE; | |
2082 | |
2083 elem* e = new elem; | |
2084 elem* u = e1->toElem(p); | |
2085 | |
2086 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
2087 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
2088 | |
2089 const llvm::Type* int8ptrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
2090 | |
2091 llvm::Value* lval = p->toplval(); | |
2092 | |
6 | 2093 llvm::Value* context = LLVM_DtoGEP(lval,zero,zero,"tmp",p->scopebb()); |
1 | 2094 llvm::Value* castcontext = new llvm::BitCastInst(u->getValue(),int8ptrty,"tmp",p->scopebb()); |
2095 new llvm::StoreInst(castcontext, context, p->scopebb()); | |
2096 | |
6 | 2097 llvm::Value* fptr = LLVM_DtoGEP(lval,zero,one,"tmp",p->scopebb()); |
1 | 2098 |
2099 assert(func->llvmValue); | |
2100 llvm::Value* castfptr = new llvm::BitCastInst(func->llvmValue,fptr->getType()->getContainedType(0),"tmp",p->scopebb()); | |
2101 new llvm::StoreInst(castfptr, fptr, p->scopebb()); | |
2102 | |
2103 e->inplace = true; | |
2104 | |
2105 delete u; | |
2106 return e; | |
2107 } | |
2108 | |
2109 ////////////////////////////////////////////////////////////////////////////////////////// | |
2110 | |
2111 elem* IdentityExp::toElem(IRState* p) | |
2112 { | |
2113 Logger::print("IdentityExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2114 LOG_SCOPE; | |
2115 | |
2116 elem* u = e1->toElem(p); | |
2117 elem* v = e2->toElem(p); | |
2118 | |
2119 elem* e = new elem; | |
2120 | |
2121 llvm::Value* l = u->getValue(); | |
2122 llvm::Value* r = 0; | |
2123 if (v->type == elem::NUL) | |
2124 r = llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(l->getType())); | |
2125 else | |
2126 r = v->getValue(); | |
2127 | |
2128 llvm::ICmpInst::Predicate pred = (op == TOKidentity) ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE; | |
2129 e->val = new llvm::ICmpInst(pred, l, r, "tmp", p->scopebb()); | |
2130 e->type = elem::VAL; | |
2131 | |
2132 delete u; | |
2133 delete v; | |
2134 | |
2135 return e; | |
2136 } | |
2137 | |
2138 ////////////////////////////////////////////////////////////////////////////////////////// | |
2139 | |
2140 elem* CommaExp::toElem(IRState* p) | |
2141 { | |
2142 Logger::print("CommaExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2143 LOG_SCOPE; | |
2144 | |
2145 elem* u = e1->toElem(p); | |
2146 elem* v = e2->toElem(p); | |
2147 delete u; | |
2148 return v; | |
2149 } | |
2150 | |
2151 ////////////////////////////////////////////////////////////////////////////////////////// | |
2152 | |
2153 elem* CondExp::toElem(IRState* p) | |
2154 { | |
2155 Logger::print("CondExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2156 LOG_SCOPE; | |
2157 | |
2158 const llvm::Type* resty = LLVM_DtoType(type); | |
2159 | |
2160 // allocate a temporary for the final result. failed to come up with a better way :/ | |
2161 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
2162 llvm::Value* resval = new llvm::AllocaInst(resty,"condtmp",p->topallocapoint()); | |
2163 | |
2164 llvm::BasicBlock* oldend = p->scopeend(); | |
2165 llvm::BasicBlock* condtrue = new llvm::BasicBlock("condtrue", gIR->topfunc(), oldend); | |
2166 llvm::BasicBlock* condfalse = new llvm::BasicBlock("condfalse", gIR->topfunc(), oldend); | |
2167 llvm::BasicBlock* condend = new llvm::BasicBlock("condend", gIR->topfunc(), oldend); | |
2168 | |
2169 elem* c = econd->toElem(p); | |
2170 llvm::Value* cond_val = LLVM_DtoBoolean(c->getValue()); | |
2171 delete c; | |
2172 new llvm::BranchInst(condtrue,condfalse,cond_val,p->scopebb()); | |
2173 | |
2174 p->scope() = IRScope(condtrue, condfalse); | |
2175 elem* u = e1->toElem(p); | |
2176 Logger::cout() << *u->val << '|' << *resval << '\n'; \ | |
2177 new llvm::StoreInst(u->getValue(),resval,p->scopebb()); | |
2178 new llvm::BranchInst(condend,p->scopebb()); | |
2179 delete u; | |
2180 | |
2181 p->scope() = IRScope(condfalse, condend); | |
2182 elem* v = e2->toElem(p); | |
2183 new llvm::StoreInst(v->getValue(),resval,p->scopebb()); | |
2184 new llvm::BranchInst(condend,p->scopebb()); | |
2185 delete v; | |
2186 | |
2187 p->scope() = IRScope(condend, oldend); | |
2188 | |
2189 elem* e = new elem; | |
2190 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
2191 e->type = elem::VAL; | |
2192 return e; | |
2193 } | |
2194 | |
2195 ////////////////////////////////////////////////////////////////////////////////////////// | |
2196 | |
2197 elem* ComExp::toElem(IRState* p) | |
2198 { | |
2199 Logger::print("ComExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2200 LOG_SCOPE; | |
2201 | |
2202 elem* e = new elem; | |
2203 elem* u = e1->toElem(p); | |
2204 | |
2205 llvm::Value* value = u->getValue(); | |
2206 llvm::Value* minusone = llvm::ConstantInt::get(value->getType(), -1, true); | |
2207 e->val = llvm::BinaryOperator::create(llvm::Instruction::Xor, value, minusone, "tmp", p->scopebb()); | |
2208 | |
2209 delete u; | |
2210 | |
2211 e->type = elem::VAL; | |
2212 | |
2213 return e; | |
2214 } | |
2215 | |
2216 ////////////////////////////////////////////////////////////////////////////////////////// | |
2217 | |
2218 #define STUB(x) elem *x::toElem(IRState * p) {error("Exp type "#x" not implemented: %s", toChars()); fatal(); return 0; } | |
2219 //STUB(IdentityExp); | |
2220 //STUB(CondExp); | |
2221 //STUB(EqualExp); | |
2222 STUB(InExp); | |
2223 //STUB(CmpExp); | |
2224 //STUB(AndAndExp); | |
2225 //STUB(OrOrExp); | |
2226 //STUB(AndExp); | |
2227 //STUB(AndAssignExp); | |
2228 //STUB(OrExp); | |
2229 //STUB(OrAssignExp); | |
2230 //STUB(XorExp); | |
2231 //STUB(XorAssignExp); | |
2232 //STUB(ShrExp); | |
2233 //STUB(ShrAssignExp); | |
2234 //STUB(ShlExp); | |
2235 //STUB(ShlAssignExp); | |
2236 //STUB(UshrExp); | |
2237 //STUB(UshrAssignExp); | |
2238 //STUB(DivExp); | |
2239 //STUB(DivAssignExp); | |
2240 //STUB(MulExp); | |
2241 //STUB(MulAssignExp); | |
2242 //STUB(ModExp); | |
2243 //STUB(ModAssignExp); | |
2244 STUB(CatExp); | |
2245 STUB(CatAssignExp); | |
2246 //STUB(AddExp); | |
2247 //STUB(AddAssignExp); | |
2248 STUB(Expression); | |
2249 //STUB(MinExp); | |
2250 //STUB(MinAssignExp); | |
2251 //STUB(PostExp); | |
2252 //STUB(NullExp); | |
2253 //STUB(ThisExp); | |
2254 //STUB(CallExp); | |
2255 STUB(DotTypeExp); | |
2256 STUB(TypeDotIdExp); | |
2257 //STUB(DotVarExp); | |
2258 //STUB(AssertExp); | |
2259 STUB(FuncExp); | |
2260 //STUB(DelegateExp); | |
2261 //STUB(VarExp); | |
2262 //STUB(DeclarationExp); | |
2263 //STUB(NewExp); | |
2264 //STUB(SymOffExp); | |
2265 STUB(ScopeExp); | |
2266 //STUB(AssignExp); | |
2267 | |
2268 STUB(TypeExp); | |
2269 //STUB(RealExp); | |
2270 STUB(ComplexExp); | |
2271 //STUB(StringExp); | |
2272 //STUB(IntegerExp); | |
2273 STUB(BoolExp); | |
2274 | |
2275 //STUB(NotExp); | |
2276 //STUB(ComExp); | |
2277 STUB(NegExp); | |
2278 //STUB(PtrExp); | |
2279 //STUB(AddrExp); | |
2280 //STUB(SliceExp); | |
2281 //STUB(CastExp); | |
2282 //STUB(DeleteExp); | |
2283 //STUB(IndexExp); | |
2284 //STUB(CommaExp); | |
2285 //STUB(ArrayLengthExp); | |
2286 //STUB(HaltExp); | |
2287 STUB(RemoveExp); | |
2288 STUB(ArrayLiteralExp); | |
2289 STUB(AssocArrayLiteralExp); | |
2290 //STUB(StructLiteralExp); | |
2291 | |
2292 unsigned Type::totym() { return 0; } | |
2293 | |
2294 type * | |
2295 Type::toCtype() { | |
2296 return 0; | |
2297 } | |
2298 | |
2299 type * Type::toCParamtype() | |
2300 { | |
2301 return 0; | |
2302 } | |
2303 Symbol * Type::toSymbol() | |
2304 { | |
2305 return 0; | |
2306 } | |
2307 | |
2308 type * | |
2309 TypeTypedef::toCtype() | |
2310 { | |
2311 return 0; | |
2312 } | |
2313 | |
2314 type * | |
2315 TypeTypedef::toCParamtype() | |
2316 { | |
2317 return 0; | |
2318 } | |
2319 | |
2320 void | |
2321 TypedefDeclaration::toDebug() | |
2322 { | |
2323 } | |
2324 | |
2325 | |
2326 type * | |
2327 TypeEnum::toCtype() | |
2328 { | |
2329 return 0; | |
2330 } | |
2331 | |
2332 type * | |
2333 TypeStruct::toCtype() | |
2334 { | |
2335 return 0; | |
2336 } | |
2337 | |
2338 void | |
2339 StructDeclaration::toDebug() | |
2340 { | |
2341 } | |
2342 | |
2343 Symbol * TypeClass::toSymbol() | |
2344 { | |
2345 return 0; | |
2346 } | |
2347 | |
2348 unsigned TypeFunction::totym() | |
2349 { | |
2350 return 0; | |
2351 } | |
2352 | |
2353 type * | |
2354 TypeFunction::toCtype() | |
2355 { | |
2356 return 0; | |
2357 } | |
2358 | |
2359 type * | |
2360 TypeSArray::toCtype() | |
2361 { | |
2362 return 0; | |
2363 } | |
2364 | |
2365 type *TypeSArray::toCParamtype() { return 0; } | |
2366 | |
2367 type * | |
2368 TypeDArray::toCtype() | |
2369 { | |
2370 return 0; | |
2371 } | |
2372 | |
2373 type * | |
2374 TypeAArray::toCtype() | |
2375 { | |
2376 return 0; | |
2377 } | |
2378 | |
2379 type * | |
2380 TypePointer::toCtype() | |
2381 { | |
2382 return 0; | |
2383 } | |
2384 | |
2385 type * | |
2386 TypeDelegate::toCtype() | |
2387 { | |
2388 return 0; | |
2389 } | |
2390 | |
2391 type * | |
2392 TypeClass::toCtype() | |
2393 { | |
2394 return 0; | |
2395 } | |
2396 | |
2397 void | |
2398 ClassDeclaration::toDebug() | |
2399 { | |
2400 } | |
2401 | |
14
0e86428ee567
[svn r18] * Initial support for switch statements - No string switches yet.
lindquist
parents:
11
diff
changeset
|
2402 ////////////////////////////////////////////////////////////////////////////// |
1 | 2403 |
2404 void | |
2405 EnumDeclaration::toDebug() | |
2406 { | |
2407 | |
2408 } | |
2409 | |
2410 int | |
2411 Dsymbol::cvMember(unsigned char*) | |
2412 { | |
2413 return 0; | |
2414 } | |
2415 int | |
2416 EnumDeclaration::cvMember(unsigned char*) | |
2417 { | |
2418 return 0; | |
2419 } | |
2420 int | |
2421 FuncDeclaration::cvMember(unsigned char*) | |
2422 { | |
2423 return 0; | |
2424 } | |
2425 int | |
2426 VarDeclaration::cvMember(unsigned char*) | |
2427 { | |
2428 return 0; | |
2429 } | |
2430 int | |
2431 TypedefDeclaration::cvMember(unsigned char*) | |
2432 { | |
2433 return 0; | |
2434 } | |
2435 | |
2436 void obj_includelib(char*){} | |
2437 | |
2438 AsmStatement::AsmStatement(Loc loc, Token *tokens) : | |
2439 Statement(loc) | |
2440 { | |
2441 } | |
2442 Statement *AsmStatement::syntaxCopy() { | |
2443 return 0; | |
2444 } | |
2445 | |
2446 Statement *AsmStatement::semantic(Scope *sc) | |
2447 { | |
2448 return Statement::semantic(sc); | |
2449 } | |
2450 | |
2451 | |
2452 void AsmStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2453 { | |
2454 Statement::toCBuffer(buf, hgs); | |
2455 } | |
2456 | |
2457 int AsmStatement::comeFrom() | |
2458 { | |
2459 return FALSE; | |
2460 } | |
2461 | |
2462 void | |
2463 backend_init() | |
2464 { | |
2465 //LLVM_D_InitRuntime(); | |
2466 // lazily loaded | |
2467 } | |
2468 | |
2469 void | |
2470 backend_term() | |
2471 { | |
2472 LLVM_D_FreeRuntime(); | |
2473 } |