Mercurial > projects > ldc
annotate gen/toir.c @ 4:e116aa1488e6 trunk
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
fixed passing string literals as array parameters
few other fixes
moved some array routines into gen/arrays
author | lindquist |
---|---|
date | Mon, 03 Sep 2007 17:34:30 +0200 |
parents | c53b6e3fe49a |
children | 3d60e549b0c2 |
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); | |
104 llvm::Value* tmp = new llvm::GetElementPtrInst(p->arrays.back(),zero,zero,"tmp",p->scopebb()); | |
105 e->val = new llvm::LoadInst(tmp,"tmp",p->scopebb()); | |
106 e->type = elem::VAL; | |
107 } | |
108 // magic | |
109 else | |
110 { | |
111 if (TypeInfoDeclaration* tid = vd->isTypeInfoDeclaration()) | |
112 { | |
113 tid->toObjFile(); | |
114 e->mem = tid->llvmValue; | |
115 e->type = elem::VAR; | |
116 } | |
117 else | |
118 assert(0 && "only magic supported is typeinfo"); | |
119 } | |
120 return e; | |
121 } | |
122 | |
123 // function parameter | |
124 if (vd->storage_class & STCparameter) { | |
125 Logger::println("function param"); | |
126 if (vd->storage_class & (STCref | STCout)) { | |
127 e->mem = vd->llvmValue; | |
128 e->type = elem::VAR; | |
129 } | |
130 else { | |
131 if (vd->type->ty == Tstruct || vd->type->ty == Tdelegate || vd->type->ty == Tarray) { | |
132 e->mem = vd->llvmValue; | |
133 e->type = elem::VAR; | |
134 } | |
135 else { | |
136 e->val = vd->llvmValue; | |
137 e->type = elem::VAL; | |
138 } | |
139 } | |
140 } | |
141 else { | |
142 e->mem = vd->llvmValue; | |
143 //e->mem->setName(toChars()); | |
144 e->vardecl = vd; | |
145 e->type = elem::VAR; | |
146 } | |
147 } | |
148 else if (FuncDeclaration* fdecl = var->isFuncDeclaration()) | |
149 { | |
150 Logger::println("FuncDeclaration"); | |
151 if (fdecl->llvmValue == 0) { | |
152 fdecl->toObjFile(); | |
153 } | |
154 e->val = fdecl->llvmValue; | |
155 e->type = elem::FUNC; | |
156 e->funcdecl = fdecl; | |
157 } | |
158 else if (SymbolDeclaration* sdecl = var->isSymbolDeclaration()) | |
159 { | |
160 // this seems to be the static initialiser for structs | |
161 Logger::print("Sym: type=%s\n", sdecl->type->toChars()); | |
162 assert(sdecl->type->ty == Tstruct); | |
163 //assert(sdecl->llvmInitZ); | |
164 //e->val = sdecl->llvmInitZ; | |
165 TypeStruct* ts = (TypeStruct*)sdecl->type; | |
166 e->mem = ts->llvmInit; | |
167 assert(e->mem); | |
168 e->type = elem::VAR; | |
169 } | |
170 else | |
171 { | |
172 assert(0 && "Unimplemented VarExp type"); | |
173 } | |
174 | |
175 assert(e->mem || e->val); | |
176 return e; | |
177 } | |
178 | |
179 ////////////////////////////////////////////////////////////////////////////////////////// | |
180 | |
181 elem* IntegerExp::toElem(IRState* p) | |
182 { | |
183 Logger::print("IntegerExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
184 LOG_SCOPE; | |
185 elem* e = new elem; | |
186 const llvm::Type* t = LLVM_DtoType(type); | |
187 if (llvm::isa<llvm::PointerType>(t)) { | |
188 llvm::Constant* i = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)value,false); | |
189 e->val = llvm::ConstantExpr::getIntToPtr(i, t); | |
190 } | |
191 else if (llvm::isa<llvm::IntegerType>(t)) { | |
192 e->val = llvm::ConstantInt::get(t,(uint64_t)value,!type->isunsigned()); | |
193 } | |
194 else { | |
195 assert(0); | |
196 } | |
197 e->type = elem::CONST; | |
198 return e; | |
199 } | |
200 | |
201 ////////////////////////////////////////////////////////////////////////////////////////// | |
202 | |
203 elem* RealExp::toElem(IRState* p) | |
204 { | |
205 Logger::print("RealExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
206 LOG_SCOPE; | |
207 elem* e = new elem; | |
208 e->val = llvm::ConstantFP::get(LLVM_DtoType(type),value); | |
209 e->type = elem::CONST; | |
210 return e; | |
211 } | |
212 | |
213 ////////////////////////////////////////////////////////////////////////////////////////// | |
214 | |
215 elem* NullExp::toElem(IRState* p) | |
216 { | |
217 Logger::print("NullExp::toElem(type=%s): %s\n", type->toChars(),toChars()); | |
218 LOG_SCOPE; | |
219 elem* e = new elem; | |
220 const llvm::Type* t = LLVM_DtoType(type); | |
221 if (llvm::isa<llvm::StructType>(t)) | |
222 t = llvm::PointerType::get(t); | |
223 Logger::cout() << *t << '\n'; | |
224 e->val = llvm::Constant::getNullValue(t); | |
225 assert(e->val); | |
226 Logger::cout() << *e->val << '\n'; | |
227 e->type = elem::NUL; | |
228 return e; | |
229 } | |
230 | |
231 ////////////////////////////////////////////////////////////////////////////////////////// | |
232 | |
233 elem* StringExp::toElem(IRState* p) | |
234 { | |
235 Logger::print("StringExp::toElem: %s\n", toChars()); | |
236 LOG_SCOPE; | |
237 | |
238 assert(type->next->ty == Tchar && "Only char is supported"); | |
239 assert(sz == 1); | |
240 | |
241 const llvm::Type* ct = LLVM_DtoType(type->next); | |
242 //printf("ct = %s\n", type->next->toChars()); | |
243 const llvm::ArrayType* at = llvm::ArrayType::get(ct,len+1); | |
244 | |
245 uint8_t* str = (uint8_t*)string; | |
246 std::string cont((char*)str, len); | |
247 | |
248 llvm::Constant* _init = llvm::ConstantArray::get(cont,true); | |
249 | |
250 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::InternalLinkage;//WeakLinkage; | |
251 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(at,true,_linkage,_init,"stringliteral",gIR->module); | |
252 | |
253 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
254 llvm::Value* arrptr = new llvm::GetElementPtrInst(gvar,zero,zero,"tmp",p->scopebb()); | |
255 | |
256 elem* e = new elem; | |
257 | |
258 if (type->ty == Tarray) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
259 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
|
260 if (p->lvals.empty()) { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
261 e->type = elem::SLICE; |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
262 e->arg = clen; |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
263 e->mem = arrptr; |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
264 return e; |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
265 } |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
266 else { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
267 llvm::Value* arr = p->toplval(); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
268 LLVM_DtoSetArray(arr, clen, arrptr); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
269 } |
1 | 270 } |
271 else if (type->ty == Tpointer) { | |
272 e->mem = arrptr; | |
273 } | |
274 else { | |
275 assert(0); | |
276 } | |
277 | |
278 e->inplace = true; | |
279 e->type = elem::VAL; | |
280 | |
281 return e; | |
282 } | |
283 | |
284 ////////////////////////////////////////////////////////////////////////////////////////// | |
285 | |
286 elem* AssignExp::toElem(IRState* p) | |
287 { | |
288 Logger::print("AssignExp::toElem: %s | %s = %s\n", toChars(), e1->type->toChars(), e2->type->toChars()); | |
289 LOG_SCOPE; | |
290 | |
291 assert(e1 && e2); | |
292 p->inLvalue = true; | |
293 elem* l = e1->toElem(p); | |
294 p->inLvalue = false; | |
295 | |
296 p->lvals.push_back(l->mem); | |
297 elem* r = e2->toElem(p); | |
298 p->lvals.pop_back(); | |
299 | |
300 assert(l->mem); | |
301 //e->val = l->store(r->getValue()); | |
302 | |
303 TY e1ty = e1->type->ty; | |
304 TY e2ty = e2->type->ty; | |
305 | |
306 elem* e = new elem; | |
307 | |
308 // struct | |
309 if (e1ty == Tstruct) { | |
310 // struct + struct | |
311 if (e2ty == Tstruct) { | |
312 // struct literals do the assignment themselvs (in place) | |
313 if (!r->inplace) { | |
314 TypeStruct* ts = (TypeStruct*)e2->type; | |
315 assert(r->mem); | |
316 LLVM_DtoStructCopy(ts,l->mem,r->mem); | |
317 } | |
318 else { | |
319 e->inplace = true; | |
320 } | |
321 } | |
322 // struct + const int | |
323 else if (e2->type->isintegral()){ | |
324 IntegerExp* iexp = (IntegerExp*)e2; | |
325 assert(iexp->value == 0 && "Only integral struct initializer allowed is zero"); | |
326 TypeStruct* st = (TypeStruct*)e1->type; | |
327 LLVM_DtoStructZeroInit(st, l->mem); | |
328 } | |
329 // :x | |
330 else | |
331 assert(0 && "struct = unknown"); | |
332 } | |
333 else if (e1ty == Tsarray) { | |
334 assert(0 && "static array = not supported"); | |
335 } | |
336 else if (e1ty == Tarray) { | |
337 if (e2->type->isscalar() || e2->type->ty == Tclass){ | |
338 LLVM_DtoArrayInit(l->mem, r->getValue()); | |
339 } | |
340 else if (e2ty == Tarray) { | |
341 //new llvm::StoreInst(r->val,l->val,p->scopebb()); | |
342 if (r->type == elem::NUL) { | |
343 llvm::Constant* c = llvm::cast<llvm::Constant>(r->val); | |
344 assert(c->isNullValue()); | |
345 LLVM_DtoNullArray(l->mem); | |
346 } | |
347 else if (r->type == elem::SLICE) { | |
348 if (l->type == elem::SLICE) | |
349 LLVM_DtoArrayCopy(l,r); | |
350 else | |
351 LLVM_DtoSetArray(l->mem,r->arg,r->mem); | |
352 } | |
353 else { | |
354 // new expressions write directly to the array reference | |
355 // so do string literals | |
356 if (!r->inplace) { | |
357 assert(r->mem); | |
358 LLVM_DtoArrayAssign(l->mem, r->mem); | |
359 } | |
360 else { | |
361 e->inplace = true; | |
362 } | |
363 } | |
364 } | |
365 else | |
366 assert(0); | |
367 } | |
368 else if (e1ty == Tpointer) { | |
369 if (e2ty == Tpointer) { | |
370 llvm::Value* v = r->field ? r->mem : r->getValue(); | |
371 Logger::cout() << "*=*: " << *v << ", " << *l->mem << '\n'; | |
372 new llvm::StoreInst(v, l->mem, p->scopebb()); | |
373 } | |
374 else | |
375 assert(0); | |
376 } | |
377 else if (e1ty == Tclass) { | |
378 if (e2ty == Tclass) { | |
379 llvm::Value* tmp = r->getValue(); | |
380 Logger::cout() << "tmp: " << *tmp << ", " << *l->mem << '\n'; | |
381 new llvm::StoreInst(tmp, l->mem, p->scopebb()); | |
382 } | |
383 else | |
384 assert(0); | |
385 } | |
386 else if (e1ty == Tdelegate) { | |
387 Logger::println("Assigning to delegate"); | |
388 if (e2ty == Tdelegate) { | |
389 if (r->type == elem::NUL) { | |
390 llvm::Constant* c = llvm::cast<llvm::Constant>(r->val); | |
391 if (c->isNullValue()) { | |
392 LLVM_DtoNullDelegate(l->mem); | |
393 } | |
394 else | |
395 assert(0); | |
396 } | |
397 else if (r->inplace) { | |
398 // do nothing | |
399 e->inplace = true; | |
400 } | |
401 else | |
402 assert(0); | |
403 } | |
404 else | |
405 assert(0); | |
406 } | |
407 // !struct && !array && !pointer && !class | |
408 else { | |
409 Logger::cout() << *l->mem << '\n'; | |
410 new llvm::StoreInst(r->getValue(),l->mem,p->scopebb()); | |
411 } | |
412 | |
413 delete r; | |
414 delete l; | |
415 return e; | |
416 } | |
417 | |
418 ////////////////////////////////////////////////////////////////////////////////////////// | |
419 | |
420 elem* AddExp::toElem(IRState* p) | |
421 { | |
422 Logger::print("AddExp::toElem: %s\n", toChars()); | |
423 LOG_SCOPE; | |
424 elem* e = new elem; | |
425 elem* l = e1->toElem(p); | |
426 elem* r = e2->toElem(p); | |
427 | |
428 if (e1->type != e2->type) { | |
429 if (e1->type->ty == Tpointer && e1->type->next->ty == Tstruct) { | |
430 assert(l->field); | |
431 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
432 assert(r->type == elem::CONST); | |
433 llvm::ConstantInt* cofs = llvm::cast<llvm::ConstantInt>(r->val); | |
434 | |
435 TypeStruct* ts = (TypeStruct*)e1->type->next; | |
436 llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, ts->sym->offsetToIndex(cofs->getZExtValue()), false); | |
437 | |
438 e->mem = new llvm::GetElementPtrInst(l->getValue(), zero, offset, "tmp", p->scopebb()); | |
439 e->type = elem::VAR; | |
440 e->field = true; | |
441 } | |
442 else if (e1->type->ty == Tpointer) { | |
443 e->val = new llvm::GetElementPtrInst(l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
444 e->type = elem::VAR; | |
445 } | |
446 else { | |
447 assert(0); | |
448 } | |
449 } | |
450 else { | |
451 e->val = llvm::BinaryOperator::createAdd(l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
452 e->type = elem::VAL; | |
453 } | |
454 delete l; | |
455 delete r; | |
456 return e; | |
457 } | |
458 | |
459 ////////////////////////////////////////////////////////////////////////////////////////// | |
460 | |
461 elem* AddAssignExp::toElem(IRState* p) | |
462 { | |
463 Logger::print("AddAssignExp::toElem: %s\n", toChars()); | |
464 LOG_SCOPE; | |
465 | |
466 elem* l = e1->toElem(p); | |
467 elem* r = e2->toElem(p); | |
468 | |
469 elem* e = new elem; | |
470 llvm::Value* val = 0; | |
471 if (e1->type->ty == Tpointer) { | |
472 val = e->mem = new llvm::GetElementPtrInst(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
473 } | |
474 else { | |
475 val = e->val = llvm::BinaryOperator::createAdd(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
476 } | |
477 | |
478 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
479 if (llvm::isa<llvm::PointerType>(storeVal->getType()) && storeVal->getType()->getContainedType(0) != tmp->getType()) | |
480 { | |
481 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
482 }*/ | |
483 | |
484 new llvm::StoreInst(val,l->mem,p->scopebb()); | |
485 e->type = elem::VAR; | |
486 | |
487 delete l; | |
488 delete r; | |
489 return e; | |
490 } | |
491 | |
492 ////////////////////////////////////////////////////////////////////////////////////////// | |
493 | |
494 elem* MinExp::toElem(IRState* p) | |
495 { | |
496 Logger::print("MinExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
497 LOG_SCOPE; | |
498 elem* e = new elem; | |
499 elem* l = e1->toElem(p); | |
500 elem* r = e2->toElem(p); | |
501 | |
502 llvm::Value* left = l->getValue(); | |
503 if (llvm::isa<llvm::PointerType>(left->getType())) | |
504 left = new llvm::PtrToIntInst(left,LLVM_DtoSize_t(),"tmp",p->scopebb()); | |
505 | |
506 llvm::Value* right = r->getValue(); | |
507 if (llvm::isa<llvm::PointerType>(right->getType())) | |
508 right = new llvm::PtrToIntInst(right,LLVM_DtoSize_t(),"tmp",p->scopebb()); | |
509 | |
510 e->val = llvm::BinaryOperator::createSub(left,right,"tmp",p->scopebb()); | |
511 e->type = elem::VAL; | |
512 | |
513 const llvm::Type* totype = LLVM_DtoType(type); | |
514 if (e->val->getType() != totype) { | |
515 assert(0); | |
516 assert(llvm::isa<llvm::PointerType>(e->val->getType())); | |
517 assert(llvm::isa<llvm::IntegerType>(totype)); | |
518 e->val = new llvm::IntToPtrInst(e->val,totype,"tmp",p->scopebb()); | |
519 } | |
520 | |
521 delete l; | |
522 delete r; | |
523 return e; | |
524 } | |
525 | |
526 ////////////////////////////////////////////////////////////////////////////////////////// | |
527 | |
528 elem* MinAssignExp::toElem(IRState* p) | |
529 { | |
530 Logger::print("MinAssignExp::toElem: %s\n", toChars()); | |
531 LOG_SCOPE; | |
532 | |
533 elem* l = e1->toElem(p); | |
534 elem* r = e2->toElem(p); | |
535 | |
536 llvm::Value* tmp = 0; | |
537 if (e1->type->ty == Tpointer) { | |
538 tmp = r->getValue(); | |
539 llvm::Value* zero = llvm::ConstantInt::get(tmp->getType(),0,false); | |
540 tmp = llvm::BinaryOperator::createSub(zero,tmp,"tmp",p->scopebb()); | |
541 tmp = new llvm::GetElementPtrInst(l->getValue(),tmp,"tmp",p->scopebb()); | |
542 } | |
543 else { | |
544 tmp = llvm::BinaryOperator::createSub(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
545 } | |
546 | |
547 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
548 if (storeVal->getType()->getContainedType(0) != tmp->getType()) | |
549 { | |
550 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
551 }*/ | |
552 | |
553 new llvm::StoreInst(tmp, l->mem, p->scopebb()); | |
554 | |
555 delete l; | |
556 delete r; | |
557 | |
558 elem* e = new elem; | |
559 e->val = tmp; | |
560 e->type = elem::VAR; | |
561 return e; | |
562 } | |
563 | |
564 ////////////////////////////////////////////////////////////////////////////////////////// | |
565 | |
566 elem* MulExp::toElem(IRState* p) | |
567 { | |
568 Logger::print("MulExp::toElem: %s\n", toChars()); | |
569 LOG_SCOPE; | |
570 elem* e = new elem; | |
571 elem* l = e1->toElem(p); | |
572 elem* r = e2->toElem(p); | |
573 llvm::Value* vl = l->getValue(); | |
574 llvm::Value* vr = r->getValue(); | |
575 Logger::cout() << "mul: " << *vl << ", " << *vr << '\n'; | |
576 e->val = llvm::BinaryOperator::createMul(vl,vr,"tmp",p->scopebb()); | |
577 e->type = elem::VAL; | |
578 delete l; | |
579 delete r; | |
580 return e; | |
581 } | |
582 | |
583 ////////////////////////////////////////////////////////////////////////////////////////// | |
584 | |
585 elem* MulAssignExp::toElem(IRState* p) | |
586 { | |
587 Logger::print("MulAssignExp::toElem: %s\n", toChars()); | |
588 LOG_SCOPE; | |
589 | |
590 elem* l = e1->toElem(p); | |
591 elem* r = e2->toElem(p); | |
592 llvm::Value* vl = l->getValue(); | |
593 llvm::Value* vr = r->getValue(); | |
594 Logger::cout() << "mulassign: " << *vl << ", " << *vr << '\n'; | |
595 llvm::Value* tmp = llvm::BinaryOperator::createMul(vl,vr,"tmp",p->scopebb()); | |
596 | |
597 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
598 if (storeVal->getType()->getContainedType(0) != tmp->getType()) | |
599 { | |
600 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
601 }*/ | |
602 | |
603 new llvm::StoreInst(tmp,l->mem,p->scopebb()); | |
604 | |
605 delete l; | |
606 delete r; | |
607 | |
608 elem* e = new elem; | |
609 e->val = tmp; | |
610 e->type = elem::VAR; | |
611 return e; | |
612 } | |
613 | |
614 ////////////////////////////////////////////////////////////////////////////////////////// | |
615 | |
616 elem* DivExp::toElem(IRState* p) | |
617 { | |
618 Logger::print("DivExp::toElem: %s\n", toChars()); | |
619 LOG_SCOPE; | |
620 elem* e = new elem; | |
621 elem* l = e1->toElem(p); | |
622 elem* r = e2->toElem(p); | |
623 | |
624 if (type->isunsigned()) | |
625 e->val = llvm::BinaryOperator::createUDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
626 else if (type->isintegral()) | |
627 e->val = llvm::BinaryOperator::createSDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
628 else if (type->isfloating()) | |
629 e->val = llvm::BinaryOperator::createFDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
630 else | |
631 assert(0); | |
632 e->type = elem::VAL; | |
633 delete l; | |
634 delete r; | |
635 return e; | |
636 } | |
637 | |
638 ////////////////////////////////////////////////////////////////////////////////////////// | |
639 | |
640 elem* DivAssignExp::toElem(IRState* p) | |
641 { | |
642 Logger::print("DivAssignExp::toElem: %s\n", toChars()); | |
643 LOG_SCOPE; | |
644 | |
645 elem* l = e1->toElem(p); | |
646 elem* r = e2->toElem(p); | |
647 | |
648 llvm::Value* tmp; | |
649 if (type->isunsigned()) | |
650 tmp = llvm::BinaryOperator::createUDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
651 else if (type->isintegral()) | |
652 tmp = llvm::BinaryOperator::createSDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
653 else if (type->isfloating()) | |
654 tmp = llvm::BinaryOperator::createFDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
655 else | |
656 assert(0); | |
657 | |
658 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
659 if (storeVal->getType()->getContainedType(0) != tmp->getType()) | |
660 { | |
661 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
662 }*/ | |
663 | |
664 new llvm::StoreInst(tmp,l->mem,p->scopebb()); | |
665 | |
666 delete l; | |
667 delete r; | |
668 | |
669 elem* e = new elem; | |
670 e->val = tmp; | |
671 e->type = elem::VAR; | |
672 return e; | |
673 } | |
674 | |
675 ////////////////////////////////////////////////////////////////////////////////////////// | |
676 | |
677 elem* ModExp::toElem(IRState* p) | |
678 { | |
679 Logger::print("ModExp::toElem: %s\n", toChars()); | |
680 LOG_SCOPE; | |
681 elem* e = new elem; | |
682 elem* l = e1->toElem(p); | |
683 elem* r = e2->toElem(p); | |
684 | |
685 if (type->isunsigned()) | |
686 e->val = llvm::BinaryOperator::createURem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
687 else if (type->isintegral()) | |
688 e->val = llvm::BinaryOperator::createSRem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
689 else if (type->isfloating()) | |
690 e->val = llvm::BinaryOperator::createFRem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
691 else | |
692 assert(0); | |
693 e->type = elem::VAL; | |
694 delete l; | |
695 delete r; | |
696 return e; | |
697 } | |
698 | |
699 ////////////////////////////////////////////////////////////////////////////////////////// | |
700 | |
701 elem* ModAssignExp::toElem(IRState* p) | |
702 { | |
703 Logger::print("ModAssignExp::toElem: %s\n", toChars()); | |
704 LOG_SCOPE; | |
705 | |
706 elem* l = e1->toElem(p); | |
707 elem* r = e2->toElem(p); | |
708 | |
709 llvm::Value* tmp; | |
710 if (type->isunsigned()) | |
711 tmp = llvm::BinaryOperator::createURem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
712 else if (type->isintegral()) | |
713 tmp = llvm::BinaryOperator::createSRem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
714 else if (type->isfloating()) | |
715 tmp = llvm::BinaryOperator::createFRem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
716 else | |
717 assert(0); | |
718 | |
719 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
720 if (storeVal->getType()->getContainedType(0) != tmp->getType()) | |
721 { | |
722 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
723 }*/ | |
724 | |
725 new llvm::StoreInst(tmp,l->mem,p->scopebb()); | |
726 | |
727 delete l; | |
728 delete r; | |
729 | |
730 elem* e = new elem; | |
731 e->val = tmp; | |
732 e->type = elem::VAR; | |
733 return e; | |
734 } | |
735 | |
736 ////////////////////////////////////////////////////////////////////////////////////////// | |
737 | |
738 elem* CallExp::toElem(IRState* p) | |
739 { | |
740 Logger::print("CallExp::toElem: %s\n", toChars()); | |
741 LOG_SCOPE; | |
742 elem* e = new elem; | |
743 elem* fn = e1->toElem(p); | |
744 LINK dlink = LINKdefault; | |
745 | |
746 bool delegateCall = false; | |
747 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty,0,false); | |
748 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty,1,false); | |
749 | |
750 // hidden struct return parameter handling | |
751 bool retinptr = false; | |
752 | |
753 TypeFunction* tf = 0; | |
754 | |
755 // regular functions | |
756 if (e1->type->ty == Tfunction) { | |
757 tf = (TypeFunction*)e1->type; | |
758 if (tf->llvmRetInPtr) { | |
759 retinptr = true; | |
760 } | |
761 dlink = tf->linkage; | |
762 } | |
763 | |
764 // delegates | |
765 else if (e1->type->ty == Tdelegate) { | |
766 Logger::println("delegateTy = %s\n", e1->type->toChars()); | |
767 assert(e1->type->next->ty == Tfunction); | |
768 tf = (TypeFunction*)e1->type->next; | |
769 if (tf->llvmRetInPtr) { | |
770 retinptr = true; | |
771 } | |
772 dlink = tf->linkage; | |
773 delegateCall = true; | |
774 } | |
775 | |
776 // invalid | |
777 else { | |
778 assert(tf); | |
779 } | |
780 | |
781 size_t n = arguments->dim; | |
782 if (fn->arg || delegateCall) n++; | |
783 if (retinptr) n++; | |
784 | |
785 llvm::Value* funcval = fn->getValue(); | |
786 std::vector<llvm::Value*> llargs(n, 0); | |
787 | |
788 const llvm::FunctionType* llfnty = 0; | |
789 | |
790 // normal function call | |
791 if (llvm::isa<llvm::FunctionType>(funcval->getType())) { | |
792 llfnty = llvm::cast<llvm::FunctionType>(funcval->getType()); | |
793 } | |
794 // pointer to something | |
795 else if (llvm::isa<llvm::PointerType>(funcval->getType())) { | |
796 // pointer to function pointer - I think this not really supposed to happen, but does :/ | |
797 // seems like sometimes we get a func* other times a func** | |
798 if (llvm::isa<llvm::PointerType>(funcval->getType()->getContainedType(0))) { | |
799 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); | |
800 } | |
801 | |
802 // function pointer | |
803 if (llvm::isa<llvm::FunctionType>(funcval->getType()->getContainedType(0))) { | |
804 //Logger::cout() << "function pointer type:\n" << *funcval << '\n'; | |
805 llfnty = llvm::cast<llvm::FunctionType>(funcval->getType()->getContainedType(0)); | |
806 } | |
807 // struct pointer - delegate | |
808 else if (llvm::isa<llvm::StructType>(funcval->getType()->getContainedType(0))) { | |
809 funcval = new llvm::GetElementPtrInst(funcval,zero,one,"tmp",p->scopebb()); | |
810 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); | |
811 const llvm::Type* ty = funcval->getType()->getContainedType(0); | |
812 llfnty = llvm::cast<llvm::FunctionType>(ty); | |
813 } | |
814 // unknown | |
815 else { | |
816 Logger::cout() << "what kind of pointer are we calling? : " << *funcval->getType() << '\n'; | |
817 } | |
818 } | |
819 else { | |
820 Logger::cout() << "what are we calling? : " << *funcval << '\n'; | |
821 } | |
822 assert(llfnty); | |
823 Logger::cout() << "Function LLVM type: " << *llfnty << '\n'; | |
824 | |
825 // argument handling | |
826 llvm::FunctionType::param_iterator argiter = llfnty->param_begin(); | |
827 int j = 0; | |
828 | |
829 // hidden struct return parameter | |
830 if (retinptr) { | |
831 if (!p->lvals.empty()) { | |
832 assert(llvm::isa<llvm::StructType>(p->toplval()->getType()->getContainedType(0))); | |
833 llargs[j] = p->toplval(); | |
834 TY Dty = tf->next->ty; | |
835 if (Dty == Tstruct || Dty == Tdelegate || Dty == Tarray) { | |
836 e->inplace = true; | |
837 } | |
838 else | |
839 assert(0); | |
840 } | |
841 else { | |
842 llargs[j] = new llvm::AllocaInst(argiter->get()->getContainedType(0),"rettmp",p->topallocapoint()); | |
843 } | |
844 ++j; | |
845 ++argiter; | |
846 e->type = elem::VAR; | |
847 } | |
848 else { | |
849 e->type = elem::VAL; | |
850 } | |
851 | |
852 // this parameter | |
853 if (fn->arg) { | |
854 Logger::println("This Call"); | |
855 if (fn->arg->getType() != argiter->get()) { | |
856 //Logger::cout() << *fn->thisparam << '|' << *argiter->get() << '\n'; | |
857 llargs[j] = new llvm::BitCastInst(fn->arg, argiter->get(), "tmp", p->scopebb()); | |
858 } | |
859 else { | |
860 llargs[j] = fn->arg; | |
861 } | |
862 ++j; | |
863 ++argiter; | |
864 } | |
865 // delegate context parameter | |
866 else if (delegateCall) { | |
867 Logger::println("Delegate Call"); | |
868 llvm::Value* contextptr = new llvm::GetElementPtrInst(fn->mem,zero,zero,"tmp",p->scopebb()); | |
869 llargs[j] = new llvm::LoadInst(contextptr,"tmp",p->scopebb()); | |
870 ++j; | |
871 ++argiter; | |
872 } | |
873 | |
874 // regular parameters | |
875 for (int i=0; i<arguments->dim; i++,j++) | |
876 { | |
877 Expression* argexp = (Expression*)arguments->data[i]; | |
878 elem* arg = argexp->toElem(p); | |
879 | |
880 Argument* fnarg = Argument::getNth(tf->parameters, i); | |
881 | |
882 TY argty = argexp->type->ty; | |
883 if (argty == Tstruct || argty == Tdelegate || argty == Tarray) { | |
884 if (!fnarg || !fnarg->llvmCopy) { | |
885 llargs[j] = arg->getValue(); | |
886 assert(llargs[j] != 0); | |
887 } | |
888 else { | |
889 llvm::Value* allocaInst = 0; | |
890 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
891 const llvm::PointerType* pty = llvm::cast<llvm::PointerType>(arg->mem->getType()); | |
892 if (argty == Tstruct) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
893 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", p->topallocapoint()); |
1 | 894 TypeStruct* ts = (TypeStruct*)argexp->type; |
895 LLVM_DtoStructCopy(ts,allocaInst,arg->mem); | |
896 } | |
897 else if (argty == Tdelegate) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
898 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", p->topallocapoint()); |
1 | 899 LLVM_DtoDelegateCopy(allocaInst,arg->mem); |
900 } | |
901 else if (argty == Tarray) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
902 if (arg->type == elem::SLICE) { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
903 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
|
904 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
|
905 } |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
906 else { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
907 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
|
908 LLVM_DtoArrayAssign(allocaInst,arg->mem); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
909 } |
1 | 910 } |
911 else | |
912 assert(0); | |
913 | |
914 llargs[j] = allocaInst; | |
915 assert(llargs[j] != 0); | |
916 } | |
917 } | |
918 else if (!fnarg || fnarg->llvmCopy) { | |
919 llargs[j] = arg->getValue(); | |
920 assert(llargs[j] != 0); | |
921 } | |
922 else { | |
923 llargs[j] = arg->mem; | |
924 assert(llargs[j] != 0); | |
925 } | |
926 | |
927 delete arg; | |
928 } | |
929 | |
930 // void returns cannot not be named | |
931 const char* varname = ""; | |
932 if (llfnty->getReturnType() != llvm::Type::VoidTy) | |
933 varname = "tmp"; | |
934 | |
935 Logger::println("%d params passed", n); | |
936 for (int i=0; i<n; ++i) | |
937 { | |
938 Logger::cout() << *llargs[i] << '\n'; | |
939 } | |
940 | |
941 Logger::cout() << "Calling: " << *funcval->getType() << '\n'; | |
942 | |
943 // call the function | |
944 llvm::CallInst* call = new llvm::CallInst(funcval, llargs.begin(), llargs.end(), varname, p->scopebb()); | |
945 e->val = call; | |
946 | |
947 // set calling convention | |
948 if ((fn->funcdecl && (fn->funcdecl->llvmInternal != LLVMintrinsic)) || delegateCall) | |
949 call->setCallingConv(LLVM_DtoCallingConv(dlink)); | |
950 | |
951 delete fn; | |
952 return e; | |
953 } | |
954 | |
955 ////////////////////////////////////////////////////////////////////////////////////////// | |
956 | |
957 elem* CastExp::toElem(IRState* p) | |
958 { | |
959 Logger::print("CastExp::toElem: %s\n", toChars()); | |
960 LOG_SCOPE; | |
961 elem* e = new elem; | |
962 elem* u = e1->toElem(p); | |
963 const llvm::Type* totype = LLVM_DtoType(to); | |
964 Type* from = e1->type; | |
965 int lsz = from->size(); | |
966 int rsz = to->size(); | |
967 | |
968 // this makes sure the strange lvalue casts don't screw things up | |
969 e->mem = u->mem; | |
970 | |
971 if (from->isintegral()) { | |
972 if (to->isintegral()) { | |
973 if (lsz < rsz) { | |
974 Logger::cout() << *totype << '\n'; | |
975 if (from->isunsigned() || from->ty == Tbool) { | |
976 e->val = new llvm::ZExtInst(u->getValue(), totype, "tmp", p->scopebb()); | |
977 } else { | |
978 e->val = new llvm::SExtInst(u->getValue(), totype, "tmp", p->scopebb()); | |
979 } | |
980 } | |
981 else if (lsz > rsz) { | |
982 e->val = new llvm::TruncInst(u->getValue(), totype, "tmp", p->scopebb()); | |
983 } | |
984 else { | |
985 e->val = new llvm::BitCastInst(u->getValue(), totype, "tmp", p->scopebb()); | |
986 } | |
987 } | |
988 else if (to->isfloating()) { | |
989 if (from->isunsigned()) { | |
990 e->val = new llvm::UIToFPInst(u->getValue(), totype, "tmp", p->scopebb()); | |
991 } | |
992 else { | |
993 e->val = new llvm::SIToFPInst(u->getValue(), totype, "tmp", p->scopebb()); | |
994 } | |
995 } | |
996 else { | |
997 assert(0); | |
998 } | |
999 //e->storeVal = u->storeVal ? u->storeVal : u->val; | |
1000 e->type = elem::VAL; | |
1001 } | |
1002 else if (from->isfloating()) { | |
1003 if (to->isfloating()) { | |
1004 if ((from->ty == Tfloat80 || from->ty == Tfloat64) && (to->ty == Tfloat80 || to->ty == Tfloat64)) { | |
1005 e->val = u->getValue(); | |
1006 } | |
1007 else if (lsz < rsz) { | |
1008 e->val = new llvm::FPExtInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1009 } | |
1010 else if (lsz > rsz) { | |
1011 e->val = new llvm::FPTruncInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1012 } | |
1013 else { | |
1014 assert(0); | |
1015 } | |
1016 } | |
1017 else if (to->isintegral()) { | |
1018 if (to->isunsigned()) { | |
1019 e->val = new llvm::FPToUIInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1020 } | |
1021 else { | |
1022 e->val = new llvm::FPToSIInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1023 } | |
1024 } | |
1025 else { | |
1026 assert(0); | |
1027 } | |
1028 e->type = elem::VAL; | |
1029 } | |
1030 else if (from->ty == Tclass) { | |
1031 //assert(to->ty == Tclass); | |
1032 e->val = new llvm::BitCastInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1033 e->type = elem::VAL; | |
1034 } | |
1035 else if (from->ty == Tarray || from->ty == Tsarray) { | |
1036 Logger::cout() << "from array or sarray" << '\n'; | |
1037 if (to->ty == Tpointer) { | |
1038 Logger::cout() << "to pointer" << '\n'; | |
1039 assert(from->next == to->next); | |
1040 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1041 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1042 llvm::Value* ptr = new llvm::GetElementPtrInst(u->getValue(),zero,one,"tmp",p->scopebb()); | |
1043 e->val = new llvm::LoadInst(ptr, "tmp", p->scopebb()); | |
1044 e->type = elem::VAL; | |
1045 } | |
1046 else if (to->ty == Tarray) { | |
1047 Logger::cout() << "to array" << '\n'; | |
1048 assert(from->next->size() == to->next->size()); | |
1049 const llvm::Type* ptrty = LLVM_DtoType(to->next); | |
1050 if (ptrty == llvm::Type::VoidTy) | |
1051 ptrty = llvm::Type::Int8Ty; | |
1052 ptrty = llvm::PointerType::get(ptrty); | |
1053 | |
1054 if (u->type == elem::SLICE) { | |
1055 e->mem = new llvm::BitCastInst(u->mem, ptrty, "tmp", p->scopebb()); | |
1056 e->arg = u->arg; | |
1057 } | |
1058 else { | |
1059 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
|
1060 if (from->ty == Tsarray) { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1061 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
|
1062 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
|
1063 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
|
1064 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
|
1065 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
|
1066 } |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1067 else { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1068 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
|
1069 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1070 e->arg = new llvm::GetElementPtrInst(uval,zero,zero,"tmp",p->scopebb()); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1071 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
|
1072 |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1073 e->mem = new llvm::GetElementPtrInst(uval,zero,one,"tmp",p->scopebb()); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1074 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
|
1075 //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
|
1076 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
|
1077 } |
1 | 1078 } |
1079 e->type = elem::SLICE; | |
1080 } | |
1081 else if (to->ty == Tsarray) { | |
1082 Logger::cout() << "to sarray" << '\n'; | |
1083 assert(0); | |
1084 } | |
1085 else { | |
1086 assert(0); | |
1087 } | |
1088 } | |
1089 else if (from->ty == Tpointer) { | |
1090 if (to->ty == Tpointer || to->ty == Tclass) { | |
1091 llvm::Value* src = u->getValue(); | |
1092 //Logger::cout() << *src << '|' << *totype << '\n'; | |
1093 e->val = new llvm::BitCastInst(src, totype, "tmp", p->scopebb()); | |
1094 } | |
1095 else if (to->isintegral()) { | |
1096 e->val = new llvm::PtrToIntInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1097 } | |
1098 else | |
1099 assert(0); | |
1100 e->type = elem::VAL; | |
1101 } | |
1102 else { | |
1103 assert(0); | |
1104 } | |
1105 delete u; | |
1106 return e; | |
1107 } | |
1108 | |
1109 ////////////////////////////////////////////////////////////////////////////////////////// | |
1110 | |
1111 elem* SymOffExp::toElem(IRState* p) | |
1112 { | |
1113 Logger::print("SymOffExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1114 LOG_SCOPE; | |
1115 elem* e = 0; | |
1116 if (VarDeclaration* vd = var->isVarDeclaration()) | |
1117 { | |
1118 Logger::println("VarDeclaration"); | |
1119 if (vd->type->ty == Tstruct && !(type->ty == Tpointer && type->next == vd->type)) { | |
1120 TypeStruct* vdt = (TypeStruct*)vd->type; | |
1121 e = new elem; | |
1122 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1123 llvm::Value* idx1 = llvm::ConstantInt::get(llvm::Type::Int32Ty, (uint64_t)vdt->sym->offsetToIndex(offset), false); | |
1124 //const llvm::Type* _typ = llvm::GetElementPtrInst::getIndexedType(LLVM_DtoType(type), idx1); | |
1125 llvm::Value* ptr = vd->llvmValue; | |
1126 assert(ptr); | |
1127 e->mem = new llvm::GetElementPtrInst(ptr,idx0,idx1,"tmp",p->scopebb()); | |
1128 e->type = elem::VAL; | |
1129 e->field = true; | |
1130 } | |
1131 else if (vd->type->ty == Tsarray) { | |
1132 /*e = new elem; | |
1133 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1134 e->val = new llvm::GetElementPtrInst(vd->llvmValue,idx0,idx0,"tmp",p->scopebb());*/ | |
1135 e = new elem; | |
1136 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1137 //llvm::Value* idx1 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1138 e->mem = new llvm::GetElementPtrInst(vd->llvmValue,idx0,idx0,"tmp",p->scopebb()); | |
1139 e->type = elem::VAL; | |
1140 } | |
1141 else if (offset == 0) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1142 /*if (!vd->llvmValue) |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1143 vd->toObjFile();*/ |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1144 assert(vd->llvmValue); |
1 | 1145 e = new elem; |
1146 e->mem = vd->llvmValue; | |
1147 //e->vardecl = vd; | |
1148 e->type = elem::VAL; | |
1149 } | |
1150 else { | |
1151 assert(0); | |
1152 } | |
1153 } | |
1154 else if (FuncDeclaration* fd = var->isFuncDeclaration()) | |
1155 { | |
1156 Logger::println("FuncDeclaration"); | |
1157 e = new elem; | |
1158 if (fd->llvmValue == 0) | |
1159 fd->toObjFile(); | |
1160 e->val = fd->llvmValue; | |
1161 //e->aspointer = true; | |
1162 e->type = elem::FUNC; | |
1163 } | |
1164 assert(e != 0); | |
1165 assert(e->type != elem::NONE); | |
1166 return e; | |
1167 } | |
1168 | |
1169 ////////////////////////////////////////////////////////////////////////////////////////// | |
1170 | |
1171 elem* PtrExp::toElem(IRState* p) | |
1172 { | |
1173 Logger::print("PtrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1174 LOG_SCOPE; | |
1175 elem* e = new elem; | |
1176 elem* a = e1->toElem(p); | |
1177 | |
1178 if (a->mem) | |
1179 Logger::cout() << "mem: " << *a->mem << '\n'; | |
1180 if (a->val) | |
1181 Logger::cout() << "val: " << *a->val << '\n'; | |
1182 | |
1183 if (a->field) | |
1184 e->mem = a->mem; | |
1185 else | |
1186 e->mem = a->getValue(); | |
1187 e->type = elem::VAR; | |
1188 | |
1189 delete a; | |
1190 return e; | |
1191 } | |
1192 | |
1193 ////////////////////////////////////////////////////////////////////////////////////////// | |
1194 | |
1195 elem* DotVarExp::toElem(IRState* p) | |
1196 { | |
1197 Logger::print("DotVarExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1198 LOG_SCOPE; | |
1199 elem* e = new elem; | |
1200 | |
1201 elem* l = e1->toElem(p); | |
1202 | |
1203 Logger::print("e1->type=%s\n", e1->type->toChars()); | |
1204 | |
1205 if (VarDeclaration* vd = var->isVarDeclaration()) { | |
1206 size_t vdoffset = (size_t)-1; | |
1207 llvm::Value* src = 0; | |
1208 if (e1->type->ty == Tpointer) { | |
1209 assert(e1->type->next->ty == Tstruct); | |
1210 TypeStruct* ts = (TypeStruct*)e1->type->next; | |
1211 vdoffset = ts->sym->offsetToIndex(vd->offset); | |
1212 Logger::println("Struct member offset:%d index:%d", vd->offset, vdoffset); | |
1213 src = l->val; | |
1214 } | |
1215 else if (e1->type->ty == Tclass) { | |
1216 TypeClass* tc = (TypeClass*)e1->type; | |
1217 Logger::println("Class member offset: %d", vd->offset); | |
1218 vdoffset = tc->sym->offsetToIndex(vd->offset); | |
1219 src = l->getValue(); | |
1220 } | |
1221 assert(vdoffset != (size_t)-1); | |
1222 assert(src != 0); | |
1223 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1224 llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, vdoffset, false); | |
1225 llvm::Value* arrptr = new llvm::GetElementPtrInst(src,zero,offset,"tmp",p->scopebb()); | |
1226 e->mem = arrptr; | |
1227 Logger::cout() << "mem: " << *e->mem << '\n'; | |
1228 e->type = elem::VAR; | |
1229 } | |
1230 else if (FuncDeclaration* fdecl = var->isFuncDeclaration()) | |
1231 { | |
1232 if (fdecl->llvmValue == 0) | |
1233 { | |
1234 fdecl->toObjFile(); | |
1235 } | |
1236 | |
1237 llvm::Value* funcval = fdecl->llvmValue; | |
1238 e->arg = l->getValue(); | |
1239 | |
1240 // virtual call | |
1241 if (fdecl->isVirtual()) { | |
1242 assert(fdecl->vtblIndex > 0); | |
1243 assert(e1->type->ty == Tclass); | |
1244 | |
1245 const llvm::Type* vtbltype = llvm::PointerType::get(llvm::ArrayType::get(llvm::PointerType::get(llvm::Type::Int8Ty),0)); | |
1246 | |
1247 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1248 llvm::Value* vtblidx = llvm::ConstantInt::get(llvm::Type::Int32Ty, (size_t)fdecl->vtblIndex, false); | |
1249 funcval = new llvm::GetElementPtrInst(e->arg, zero, zero, "tmp", p->scopebb()); | |
1250 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); | |
1251 funcval = new llvm::BitCastInst(funcval, vtbltype, "tmp", p->scopebb()); | |
1252 funcval = new llvm::GetElementPtrInst(funcval, zero, vtblidx, "tmp", p->scopebb()); | |
1253 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); | |
1254 funcval = new llvm::BitCastInst(funcval, fdecl->llvmValue->getType(), "tmp", p->scopebb()); | |
1255 } | |
1256 e->val = funcval; | |
1257 e->type = elem::VAL; | |
1258 } | |
1259 else { | |
1260 printf("unknown: %s\n", var->toChars()); | |
1261 assert(0); | |
1262 } | |
1263 | |
1264 delete l; | |
1265 | |
1266 return e; | |
1267 } | |
1268 | |
1269 ////////////////////////////////////////////////////////////////////////////////////////// | |
1270 | |
1271 elem* ThisExp::toElem(IRState* p) | |
1272 { | |
1273 Logger::print("ThisExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1274 LOG_SCOPE; | |
1275 elem* e = new elem; | |
1276 | |
1277 if (VarDeclaration* vd = var->isVarDeclaration()) { | |
1278 assert(vd->llvmValue == 0); | |
1279 | |
1280 llvm::Function* fn = p->topfunc(); | |
1281 assert(fn); | |
1282 | |
1283 TypeFunction* tf = p->topfunctype(); | |
1284 assert(tf); | |
1285 | |
1286 llvm::Value* v = 0; | |
1287 if (tf->llvmRetInPtr) | |
1288 v = ++fn->arg_begin(); | |
1289 else | |
1290 v = fn->arg_begin(); | |
1291 assert(v); | |
1292 | |
1293 e->val = v; | |
1294 e->type = elem::VAL; | |
1295 } | |
1296 else { | |
1297 assert(0); | |
1298 } | |
1299 | |
1300 return e; | |
1301 } | |
1302 | |
1303 ////////////////////////////////////////////////////////////////////////////////////////// | |
1304 | |
1305 elem* AddrExp::toElem(IRState* p) | |
1306 { | |
1307 Logger::print("AddrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1308 LOG_SCOPE; | |
1309 elem* e = e1->toElem(p); | |
1310 e->field = true; | |
1311 return e; | |
1312 } | |
1313 | |
1314 ////////////////////////////////////////////////////////////////////////////////////////// | |
1315 | |
1316 elem* StructLiteralExp::toElem(IRState* p) | |
1317 { | |
1318 Logger::print("StructLiteralExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1319 LOG_SCOPE; | |
1320 elem* e = new elem; | |
1321 | |
1322 // if there is no lval, this must be a static initializer for a global. correct? | |
1323 if (p->lvals.empty()) | |
1324 { | |
1325 // TODO | |
1326 assert(0); | |
1327 } | |
1328 // otherwise write directly in the lvalue | |
1329 else | |
1330 { | |
1331 llvm::Value* sptr = p->toplval(); | |
1332 assert(sptr); | |
1333 | |
1334 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1335 | |
1336 unsigned n = elements->dim; | |
1337 for (unsigned i=0; i<n; ++i) | |
1338 { | |
1339 llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, i, false); | |
1340 llvm::Value* arrptr = new llvm::GetElementPtrInst(sptr,zero,offset,"tmp",p->scopebb()); | |
1341 | |
1342 Expression* vx = (Expression*)elements->data[i]; | |
1343 if (vx != 0) { | |
1344 elem* ve = vx->toElem(p); | |
1345 //Logger::cout() << *ve->val << " | " << *arrptr << '\n'; | |
1346 new llvm::StoreInst(ve->getValue(), arrptr, p->scopebb()); | |
1347 delete ve; | |
1348 } | |
1349 else { | |
1350 assert(0); | |
1351 } | |
1352 } | |
1353 } | |
1354 | |
1355 e->inplace = true; | |
1356 | |
1357 return e; | |
1358 } | |
1359 | |
1360 ////////////////////////////////////////////////////////////////////////////////////////// | |
1361 | |
1362 elem* IndexExp::toElem(IRState* p) | |
1363 { | |
1364 Logger::print("IndexExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1365 LOG_SCOPE; | |
1366 | |
1367 elem* e = new elem; | |
1368 | |
1369 elem* l = e1->toElem(p); | |
1370 | |
1371 p->arrays.push_back(l->mem); // if $ is used it must be an array so this is fine. | |
1372 elem* r = e2->toElem(p); | |
1373 p->arrays.pop_back(); | |
1374 | |
1375 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1376 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1377 | |
1378 llvm::Value* arrptr = 0; | |
1379 if (e1->type->ty == Tpointer) { | |
1380 arrptr = new llvm::GetElementPtrInst(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
1381 } | |
1382 else if (e1->type->ty == Tsarray) { | |
1383 arrptr = new llvm::GetElementPtrInst(l->mem, zero, r->getValue(),"tmp",p->scopebb()); | |
1384 } | |
1385 else if (e1->type->ty == Tarray) { | |
1386 arrptr = new llvm::GetElementPtrInst(l->mem,zero,one,"tmp",p->scopebb()); | |
1387 arrptr = new llvm::LoadInst(arrptr,"tmp",p->scopebb()); | |
1388 arrptr = new llvm::GetElementPtrInst(arrptr,r->getValue(),"tmp",p->scopebb()); | |
1389 } | |
1390 assert(arrptr); | |
1391 | |
1392 e->mem = arrptr; | |
1393 e->type = elem::VAR; | |
1394 | |
1395 delete l; | |
1396 delete r; | |
1397 | |
1398 return e; | |
1399 } | |
1400 | |
1401 ////////////////////////////////////////////////////////////////////////////////////////// | |
1402 | |
1403 elem* SliceExp::toElem(IRState* p) | |
1404 { | |
1405 Logger::print("SliceExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1406 LOG_SCOPE; | |
1407 | |
1408 elem* v = e1->toElem(p); | |
1409 | |
1410 elem* e = new elem; | |
1411 assert(v->mem); | |
1412 e->type = elem::SLICE; | |
1413 | |
1414 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1415 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1416 | |
1417 // partial slice | |
1418 if (lwr) | |
1419 { | |
1420 assert(upr); | |
1421 p->arrays.push_back(v->mem); | |
1422 elem* lo = lwr->toElem(p); | |
1423 | |
1424 bool lwr_is_zero = false; | |
1425 if (lo->type == elem::CONST) | |
1426 { | |
1427 assert(lo->val); | |
1428 assert(llvm::isa<llvm::ConstantInt>(lo->val)); | |
1429 | |
1430 if (e1->type->ty == Tpointer) { | |
1431 e->mem = v->getValue(); | |
1432 } | |
1433 else if (e1->type->ty == Tarray) { | |
1434 llvm::Value* tmp = new llvm::GetElementPtrInst(v->mem,zero,one,"tmp",p->scopebb()); | |
1435 e->mem = new llvm::LoadInst(tmp,"tmp",p->scopebb()); | |
1436 } | |
1437 else | |
1438 assert(e->mem); | |
1439 | |
1440 llvm::ConstantInt* c = llvm::cast<llvm::ConstantInt>(lo->val); | |
1441 if (!(lwr_is_zero = c->isZero())) { | |
1442 e->mem = new llvm::GetElementPtrInst(e->mem,lo->val,"tmp",p->scopebb()); | |
1443 } | |
1444 } | |
1445 else | |
1446 { | |
1447 llvm::Value* tmp = new llvm::GetElementPtrInst(v->mem,zero,one,"tmp",p->scopebb()); | |
1448 tmp = new llvm::LoadInst(tmp,"tmp",p->scopebb()); | |
1449 e->mem = new llvm::GetElementPtrInst(tmp,lo->getValue(),"tmp",p->scopebb()); | |
1450 } | |
1451 | |
1452 elem* up = upr->toElem(p); | |
1453 p->arrays.pop_back(); | |
1454 | |
1455 if (up->type == elem::CONST) | |
1456 { | |
1457 assert(up->val); | |
1458 assert(llvm::isa<llvm::ConstantInt>(up->val)); | |
1459 if (lwr_is_zero) { | |
1460 e->arg = up->val; | |
1461 } | |
1462 else { | |
1463 if (lo->type == elem::CONST) { | |
1464 llvm::Constant* clo = llvm::cast<llvm::Constant>(lo->val); | |
1465 llvm::Constant* cup = llvm::cast<llvm::Constant>(up->val); | |
1466 e->arg = llvm::ConstantExpr::getSub(cup, clo); | |
1467 } | |
1468 else { | |
1469 e->arg = llvm::BinaryOperator::createSub(up->val, lo->getValue(), "tmp", p->scopebb()); | |
1470 } | |
1471 } | |
1472 } | |
1473 else | |
1474 { | |
1475 if (lwr_is_zero) { | |
1476 e->arg = up->getValue(); | |
1477 } | |
1478 else { | |
1479 e->arg = llvm::BinaryOperator::createSub(up->getValue(), lo->getValue(), "tmp", p->scopebb()); | |
1480 } | |
1481 } | |
1482 | |
1483 delete lo; | |
1484 delete up; | |
1485 } | |
1486 // full slice | |
1487 else | |
1488 { | |
1489 e->mem = v->mem; | |
1490 } | |
1491 | |
1492 delete v; | |
1493 | |
1494 return e; | |
1495 } | |
1496 | |
1497 ////////////////////////////////////////////////////////////////////////////////////////// | |
1498 | |
1499 elem* CmpExp::toElem(IRState* p) | |
1500 { | |
1501 Logger::print("CmpExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1502 LOG_SCOPE; | |
1503 | |
1504 elem* e = new elem; | |
1505 | |
1506 elem* l = e1->toElem(p); | |
1507 elem* r = e2->toElem(p); | |
1508 | |
1509 assert(e1->type == e2->type); | |
1510 | |
1511 Type* t = e1->type; | |
1512 | |
1513 if (t->isintegral()) | |
1514 { | |
1515 llvm::ICmpInst::Predicate cmpop; | |
1516 switch(op) | |
1517 { | |
1518 case TOKlt: | |
1519 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_ULT : llvm::ICmpInst::ICMP_SLT; | |
1520 break; | |
1521 case TOKle: | |
1522 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_ULE : llvm::ICmpInst::ICMP_SLE; | |
1523 break; | |
1524 case TOKgt: | |
1525 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_UGT : llvm::ICmpInst::ICMP_SGT; | |
1526 break; | |
1527 case TOKge: | |
1528 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_UGE : llvm::ICmpInst::ICMP_SGE; | |
1529 break; | |
1530 default: | |
1531 assert(0); | |
1532 } | |
1533 e->val = new llvm::ICmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1534 } | |
1535 else if (t->isfloating()) | |
1536 { | |
1537 llvm::FCmpInst::Predicate cmpop; | |
1538 switch(op) | |
1539 { | |
1540 case TOKlt: | |
1541 cmpop = llvm::FCmpInst::FCMP_OLT;break; | |
1542 case TOKle: | |
1543 cmpop = llvm::FCmpInst::FCMP_OLE;break; | |
1544 case TOKgt: | |
1545 cmpop = llvm::FCmpInst::FCMP_OGT;break; | |
1546 case TOKge: | |
1547 cmpop = llvm::FCmpInst::FCMP_OGE;break; | |
1548 case TOKunord: | |
1549 cmpop = llvm::FCmpInst::FCMP_UNO;break; | |
1550 case TOKule: | |
1551 cmpop = llvm::FCmpInst::FCMP_ULE;break; | |
1552 case TOKul: | |
1553 cmpop = llvm::FCmpInst::FCMP_ULT;break; | |
1554 case TOKuge: | |
1555 cmpop = llvm::FCmpInst::FCMP_UGE;break; | |
1556 case TOKug: | |
1557 cmpop = llvm::FCmpInst::FCMP_UGT;break; | |
1558 case TOKue: | |
1559 cmpop = llvm::FCmpInst::FCMP_UEQ;break; | |
1560 case TOKlg: | |
1561 cmpop = llvm::FCmpInst::FCMP_ONE;break; | |
1562 case TOKleg: | |
1563 cmpop = llvm::FCmpInst::FCMP_ORD;break; | |
1564 | |
1565 default: | |
1566 assert(0); | |
1567 } | |
1568 e->val = new llvm::FCmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1569 } | |
1570 else | |
1571 { | |
1572 assert(0 && "Unsupported CmpExp type"); | |
1573 } | |
1574 | |
1575 delete l; | |
1576 delete r; | |
1577 | |
1578 e->type = elem::VAL; | |
1579 | |
1580 return e; | |
1581 } | |
1582 | |
1583 ////////////////////////////////////////////////////////////////////////////////////////// | |
1584 | |
1585 elem* EqualExp::toElem(IRState* p) | |
1586 { | |
1587 Logger::print("EqualExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1588 LOG_SCOPE; | |
1589 | |
1590 elem* e = new elem; | |
1591 | |
1592 elem* l = e1->toElem(p); | |
1593 elem* r = e2->toElem(p); | |
1594 | |
1595 assert(e1->type == e2->type); | |
1596 | |
1597 Type* t = e1->type; | |
1598 | |
1599 if (t->isintegral() || t->ty == Tpointer) | |
1600 { | |
1601 llvm::ICmpInst::Predicate cmpop; | |
1602 switch(op) | |
1603 { | |
1604 case TOKequal: | |
1605 cmpop = llvm::ICmpInst::ICMP_EQ; | |
1606 break; | |
1607 case TOKnotequal: | |
1608 cmpop = llvm::ICmpInst::ICMP_NE; | |
1609 break; | |
1610 default: | |
1611 assert(0); | |
1612 } | |
1613 e->val = new llvm::ICmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1614 } | |
1615 else if (t->isfloating()) | |
1616 { | |
1617 llvm::FCmpInst::Predicate cmpop; | |
1618 switch(op) | |
1619 { | |
1620 case TOKequal: | |
1621 cmpop = llvm::FCmpInst::FCMP_OEQ; | |
1622 break; | |
1623 case TOKnotequal: | |
1624 cmpop = llvm::FCmpInst::FCMP_UNE; | |
1625 break; | |
1626 default: | |
1627 assert(0); | |
1628 } | |
1629 e->val = new llvm::FCmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1630 } | |
1631 else if (t->ty == Tarray) | |
1632 { | |
1633 // array comparison invokes the typeinfo runtime | |
1634 assert(0); | |
1635 } | |
1636 else | |
1637 { | |
1638 assert(0 && "Unsupported EqualExp type"); | |
1639 } | |
1640 | |
1641 delete l; | |
1642 delete r; | |
1643 | |
1644 e->type = elem::VAL; | |
1645 | |
1646 return e; | |
1647 } | |
1648 | |
1649 ////////////////////////////////////////////////////////////////////////////////////////// | |
1650 | |
1651 elem* PostExp::toElem(IRState* p) | |
1652 { | |
1653 Logger::print("PostExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1654 LOG_SCOPE; | |
1655 | |
1656 elem* l = e1->toElem(p); | |
1657 elem* r = e2->toElem(p); | |
1658 | |
1659 elem* e = new elem; | |
1660 e->mem = l->mem; | |
1661 e->val = l->getValue(); | |
1662 e->type = elem::VAL; | |
1663 | |
1664 llvm::Value* val = e->val; | |
1665 llvm::Value* post = 0; | |
1666 | |
1667 if (e1->type->isintegral()) | |
1668 { | |
1669 assert(e2->type->isintegral()); | |
1670 llvm::Value* one = llvm::ConstantInt::get(val->getType(), 1, !e2->type->isunsigned()); | |
1671 if (op == TOKplusplus) { | |
1672 post = llvm::BinaryOperator::createAdd(val,one,"tmp",p->scopebb()); | |
1673 } | |
1674 else if (op == TOKminusminus) { | |
1675 post = llvm::BinaryOperator::createSub(val,one,"tmp",p->scopebb()); | |
1676 } | |
1677 } | |
1678 else if (e1->type->ty == Tpointer) | |
1679 { | |
1680 assert(e2->type->isintegral()); | |
1681 llvm::Constant* minusone = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)-1,true); | |
1682 llvm::Constant* plusone = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)1,false); | |
1683 llvm::Constant* whichone = (op == TOKplusplus) ? plusone : minusone; | |
1684 post = new llvm::GetElementPtrInst(val, whichone, "tmp", p->scopebb()); | |
1685 } | |
1686 else if (e1->type->isfloating()) | |
1687 { | |
1688 assert(e2->type->isfloating()); | |
1689 llvm::Value* one = llvm::ConstantFP::get(val->getType(), 1.0f); | |
1690 if (op == TOKplusplus) { | |
1691 post = llvm::BinaryOperator::createAdd(val,one,"tmp",p->scopebb()); | |
1692 } | |
1693 else if (op == TOKminusminus) { | |
1694 post = llvm::BinaryOperator::createSub(val,one,"tmp",p->scopebb()); | |
1695 } | |
1696 } | |
1697 else | |
1698 assert(post); | |
1699 | |
1700 //llvm::Value* tostore = l->storeVal ? l->storeVal : l->val; | |
1701 new llvm::StoreInst(post,l->mem,p->scopebb()); | |
1702 | |
1703 delete l; | |
1704 delete r; | |
1705 | |
1706 return e; | |
1707 } | |
1708 | |
1709 ////////////////////////////////////////////////////////////////////////////////////////// | |
1710 | |
1711 elem* NewExp::toElem(IRState* p) | |
1712 { | |
1713 Logger::print("NewExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1714 LOG_SCOPE; | |
1715 | |
1716 assert(!thisexp); | |
1717 assert(!newargs); | |
1718 assert(newtype); | |
1719 //assert(!arguments); | |
1720 //assert(!member); | |
1721 assert(!allocator); | |
1722 | |
1723 elem* e = new elem; | |
1724 | |
1725 const llvm::Type* t = LLVM_DtoType(newtype); | |
1726 | |
1727 if (onstack) { | |
1728 assert(newtype->ty == Tclass); | |
1729 e->mem = new llvm::AllocaInst(t->getContainedType(0),"tmp",p->topallocapoint()); | |
1730 } | |
1731 else { | |
1732 if (newtype->ty == Tclass) { | |
1733 e->mem = new llvm::MallocInst(t->getContainedType(0),"tmp",p->scopebb()); | |
1734 } | |
1735 else if (newtype->ty == Tarray) { | |
1736 t = LLVM_DtoType(newtype->next); | |
1737 assert(arguments); | |
1738 if (arguments->dim == 1) { | |
1739 elem* sz = ((Expression*)arguments->data[0])->toElem(p); | |
1740 llvm::Value* dimval = sz->getValue(); | |
1741 llvm::Value* usedimval = dimval; | |
1742 if (dimval->getType() != llvm::Type::Int32Ty) | |
1743 usedimval = new llvm::TruncInst(dimval, llvm::Type::Int32Ty,"tmp",p->scopebb()); | |
1744 e->mem = new llvm::MallocInst(t,usedimval,"tmp",p->scopebb()); | |
1745 | |
1746 LLVM_DtoSetArray(p->toplval(), dimval, e->mem); | |
1747 delete sz; | |
1748 } | |
1749 else { | |
1750 assert(0); | |
1751 } | |
1752 } | |
1753 else { | |
1754 e->mem = new llvm::MallocInst(t,"tmp",p->scopebb()); | |
1755 } | |
1756 } | |
1757 | |
1758 if (newtype->ty == Tclass) { | |
1759 // first apply the static initializer | |
1760 assert(e->mem); | |
1761 LLVM_DtoInitClass((TypeClass*)newtype, e->mem); | |
1762 | |
1763 // then call constructor | |
1764 if (arguments) { | |
1765 std::vector<llvm::Value*> ctorargs; | |
1766 ctorargs.push_back(e->mem); | |
1767 for (size_t i=0; i<arguments->dim; ++i) | |
1768 { | |
1769 Expression* ex = (Expression*)arguments->data[i]; | |
1770 Logger::println("arg=%s", ex->toChars()); | |
1771 elem* exe = ex->toElem(p); | |
1772 assert(exe->getValue()); | |
1773 ctorargs.push_back(exe->getValue()); | |
1774 delete exe; | |
1775 } | |
1776 assert(member); | |
1777 assert(member->llvmValue); | |
1778 new llvm::CallInst(member->llvmValue, ctorargs.begin(), ctorargs.end(), "", p->scopebb()); | |
1779 } | |
1780 } | |
1781 else if (newtype->ty == Tstruct) { | |
1782 TypeStruct* ts = (TypeStruct*)newtype; | |
1783 if (ts->isZeroInit()) { | |
1784 LLVM_DtoStructZeroInit(ts,e->mem); | |
1785 } | |
1786 else { | |
1787 LLVM_DtoStructCopy(ts,e->mem,ts->llvmInit); | |
1788 } | |
1789 } | |
1790 | |
1791 e->inplace = true; | |
1792 e->type = elem::VAR; | |
1793 | |
1794 return e; | |
1795 } | |
1796 | |
1797 ////////////////////////////////////////////////////////////////////////////////////////// | |
1798 | |
1799 elem* DeleteExp::toElem(IRState* p) | |
1800 { | |
1801 Logger::print("DeleteExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1802 LOG_SCOPE; | |
1803 | |
1804 //assert(e1->type->ty != Tclass); | |
1805 | |
1806 elem* v = e1->toElem(p); | |
1807 llvm::Value* val = v->getValue(); | |
1808 llvm::Value* ldval = 0; | |
1809 | |
1810 const llvm::Type* t = val->getType(); | |
1811 llvm::Constant* z = llvm::Constant::getNullValue(t); | |
1812 | |
1813 if (e1->type->ty == Tpointer) { | |
1814 ldval = v->getValue(); | |
1815 new llvm::FreeInst(ldval, p->scopebb()); | |
1816 | |
1817 Logger::cout() << *z << '\n'; | |
1818 Logger::cout() << *val << '\n'; | |
1819 new llvm::StoreInst(z, v->mem, p->scopebb()); | |
1820 } | |
1821 else if (e1->type->ty == Tclass) { | |
1822 TypeClass* tc = (TypeClass*)e1->type; | |
1823 LLVM_DtoCallClassDtors(tc, val); | |
1824 | |
1825 if (v->vardecl && !v->vardecl->onstack) { | |
1826 new llvm::FreeInst(val, p->scopebb()); | |
1827 } | |
1828 new llvm::StoreInst(z, v->mem, p->scopebb()); | |
1829 } | |
1830 else if (e1->type->ty == Tarray) { | |
1831 // must be on the heap (correct?) | |
1832 ldval = v->getValue(); | |
1833 | |
1834 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1835 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1836 | |
1837 llvm::Value* ptr = new llvm::GetElementPtrInst(ldval,zero,one,"tmp",p->scopebb()); | |
1838 ptr = new llvm::LoadInst(ptr,"tmp",p->scopebb()); | |
1839 new llvm::FreeInst(ptr, p->scopebb()); | |
1840 LLVM_DtoNullArray(val); | |
1841 } | |
1842 else { | |
1843 assert(0); | |
1844 } | |
1845 | |
1846 delete v; | |
1847 | |
1848 // this expression produces no useful data | |
1849 return 0; | |
1850 } | |
1851 | |
1852 ////////////////////////////////////////////////////////////////////////////////////////// | |
1853 | |
1854 elem* ArrayLengthExp::toElem(IRState* p) | |
1855 { | |
1856 Logger::print("ArrayLengthExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1857 LOG_SCOPE; | |
1858 | |
1859 elem* e = new elem; | |
1860 elem* u = e1->toElem(p); | |
1861 | |
1862 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1863 llvm::Value* ptr = new llvm::GetElementPtrInst(u->mem,zero,zero,"tmp",p->scopebb()); | |
1864 e->val = new llvm::LoadInst(ptr, "tmp", p->scopebb()); | |
1865 e->type = elem::VAL; | |
1866 | |
1867 delete u; | |
1868 | |
1869 return e; | |
1870 } | |
1871 | |
1872 ////////////////////////////////////////////////////////////////////////////////////////// | |
1873 | |
1874 elem* AssertExp::toElem(IRState* p) | |
1875 { | |
1876 Logger::print("AssertExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1877 LOG_SCOPE; | |
1878 | |
1879 elem* e = new elem; | |
1880 elem* u = e1->toElem(p); | |
1881 elem* m = msg ? msg->toElem(p) : 0; | |
1882 | |
1883 std::vector<llvm::Value*> llargs; | |
1884 llargs.resize(3); | |
1885 llargs[0] = LLVM_DtoBoolean(u->getValue()); | |
1886 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, loc.linnum, false); | |
1887 llargs[2] = m ? m->val : llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty)); | |
1888 | |
1889 delete m; | |
1890 delete u; | |
1891 | |
1892 //Logger::cout() << *llargs[0] << '|' << *llargs[1] << '\n'; | |
1893 | |
1894 llvm::Function* fn = LLVM_D_GetRuntimeFunction(p->module, "_d_assert"); | |
1895 assert(fn); | |
1896 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", p->scopebb()); | |
1897 call->setCallingConv(llvm::CallingConv::C); | |
1898 | |
1899 return e; | |
1900 } | |
1901 | |
1902 ////////////////////////////////////////////////////////////////////////////////////////// | |
1903 | |
1904 elem* NotExp::toElem(IRState* p) | |
1905 { | |
1906 Logger::print("NotExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1907 LOG_SCOPE; | |
1908 | |
1909 elem* e = new elem; | |
1910 elem* u = e1->toElem(p); | |
1911 | |
1912 llvm::Value* b = LLVM_DtoBoolean(u->getValue()); | |
1913 | |
1914 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int1Ty, 0, true); | |
1915 e->val = new llvm::ICmpInst(llvm::ICmpInst::ICMP_EQ,b,zero,"tmp",p->scopebb()); | |
1916 e->type = elem::VAL; | |
1917 | |
1918 delete u; | |
1919 | |
1920 return e; | |
1921 } | |
1922 | |
1923 ////////////////////////////////////////////////////////////////////////////////////////// | |
1924 | |
1925 elem* AndAndExp::toElem(IRState* p) | |
1926 { | |
1927 Logger::print("AndAndExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1928 LOG_SCOPE; | |
1929 | |
1930 // allocate a temporary for the final result. failed to come up with a better way :/ | |
1931 llvm::Value* resval = 0; | |
1932 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
1933 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"andandtmp",p->topallocapoint()); | |
1934 | |
1935 elem* e = new elem; | |
1936 elem* u = e1->toElem(p); | |
1937 | |
1938 llvm::BasicBlock* oldend = p->scopeend(); | |
1939 llvm::BasicBlock* andand = new llvm::BasicBlock("andand", gIR->topfunc(), oldend); | |
1940 llvm::BasicBlock* andandend = new llvm::BasicBlock("andandend", gIR->topfunc(), oldend); | |
1941 | |
1942 llvm::Value* ubool = LLVM_DtoBoolean(u->getValue()); | |
1943 new llvm::StoreInst(ubool,resval,p->scopebb()); | |
1944 new llvm::BranchInst(andand,andandend,ubool,p->scopebb()); | |
1945 | |
1946 p->scope() = IRScope(andand, andandend); | |
1947 elem* v = e2->toElem(p); | |
1948 | |
1949 llvm::Value* vbool = LLVM_DtoBoolean(v->getValue()); | |
1950 llvm::Value* uandvbool = llvm::BinaryOperator::create(llvm::BinaryOperator::And, ubool, vbool,"tmp",p->scopebb()); | |
1951 new llvm::StoreInst(uandvbool,resval,p->scopebb()); | |
1952 new llvm::BranchInst(andandend,p->scopebb()); | |
1953 | |
1954 delete u; | |
1955 delete v; | |
1956 | |
1957 p->scope() = IRScope(andandend, oldend); | |
1958 | |
1959 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
1960 e->type = elem::VAL; | |
1961 | |
1962 return e; | |
1963 } | |
1964 | |
1965 ////////////////////////////////////////////////////////////////////////////////////////// | |
1966 | |
1967 elem* OrOrExp::toElem(IRState* p) | |
1968 { | |
1969 Logger::print("OrOrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1970 LOG_SCOPE; | |
1971 | |
1972 // allocate a temporary for the final result. failed to come up with a better way :/ | |
1973 llvm::Value* resval = 0; | |
1974 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
1975 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"orortmp",p->topallocapoint()); | |
1976 | |
1977 elem* e = new elem; | |
1978 elem* u = e1->toElem(p); | |
1979 | |
1980 llvm::BasicBlock* oldend = p->scopeend(); | |
1981 llvm::BasicBlock* oror = new llvm::BasicBlock("oror", gIR->topfunc(), oldend); | |
1982 llvm::BasicBlock* ororend = new llvm::BasicBlock("ororend", gIR->topfunc(), oldend); | |
1983 | |
1984 llvm::Value* ubool = LLVM_DtoBoolean(u->getValue()); | |
1985 new llvm::StoreInst(ubool,resval,p->scopebb()); | |
1986 new llvm::BranchInst(ororend,oror,ubool,p->scopebb()); | |
1987 | |
1988 p->scope() = IRScope(oror, ororend); | |
1989 elem* v = e2->toElem(p); | |
1990 | |
1991 llvm::Value* vbool = LLVM_DtoBoolean(v->getValue()); | |
1992 new llvm::StoreInst(vbool,resval,p->scopebb()); | |
1993 new llvm::BranchInst(ororend,p->scopebb()); | |
1994 | |
1995 delete u; | |
1996 delete v; | |
1997 | |
1998 p->scope() = IRScope(ororend, oldend); | |
1999 | |
2000 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
2001 e->type = elem::VAL; | |
2002 | |
2003 return e; | |
2004 } | |
2005 | |
2006 ////////////////////////////////////////////////////////////////////////////////////////// | |
2007 | |
2008 #define BinBitExp(X,Y) \ | |
2009 elem* X##Exp::toElem(IRState* p) \ | |
2010 { \ | |
2011 Logger::print("%sExp::toElem: %s | %s\n", #X, toChars(), type->toChars()); \ | |
2012 LOG_SCOPE; \ | |
2013 elem* e = new elem; \ | |
2014 elem* u = e1->toElem(p); \ | |
2015 elem* v = e2->toElem(p); \ | |
2016 e->val = llvm::BinaryOperator::create(llvm::Instruction::Y, u->getValue(), v->getValue(), "tmp", p->scopebb()); \ | |
2017 e->type = elem::VAL; \ | |
2018 delete u; \ | |
2019 delete v; \ | |
2020 return e; \ | |
2021 } \ | |
2022 \ | |
2023 elem* X##AssignExp::toElem(IRState* p) \ | |
2024 { \ | |
2025 Logger::print("%sAssignExp::toElem: %s | %s\n", #X, toChars(), type->toChars()); \ | |
2026 LOG_SCOPE; \ | |
2027 elem* u = e1->toElem(p); \ | |
2028 elem* v = e2->toElem(p); \ | |
2029 llvm::Value* tmp = llvm::BinaryOperator::create(llvm::Instruction::Y, u->getValue(), v->getValue(), "tmp", p->scopebb()); \ | |
2030 Logger::cout() << *tmp << '|' << *u->mem << '\n'; \ | |
2031 new llvm::StoreInst(LLVM_DtoPointedType(u->mem, tmp), u->mem, p->scopebb()); \ | |
2032 delete u; \ | |
2033 delete v; \ | |
2034 elem* e = new elem; \ | |
2035 e->mem = u->mem; \ | |
2036 e->type = elem::VAR; \ | |
2037 return e; \ | |
2038 } | |
2039 | |
2040 BinBitExp(And,And); | |
2041 BinBitExp(Or,Or); | |
2042 BinBitExp(Xor,Xor); | |
2043 BinBitExp(Shl,Shl); | |
2044 BinBitExp(Shr,AShr); | |
2045 BinBitExp(Ushr,LShr); | |
2046 | |
2047 ////////////////////////////////////////////////////////////////////////////////////////// | |
2048 | |
2049 elem* HaltExp::toElem(IRState* p) | |
2050 { | |
2051 Logger::print("HaltExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2052 LOG_SCOPE; | |
2053 | |
2054 std::vector<llvm::Value*> llargs; | |
2055 llargs.resize(3); | |
2056 llargs[0] = llvm::ConstantInt::get(llvm::Type::Int1Ty, 0, false); | |
2057 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, loc.linnum, false); | |
2058 llargs[2] = llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty)); | |
2059 | |
2060 //Logger::cout() << *llargs[0] << '|' << *llargs[1] << '\n'; | |
2061 | |
2062 llvm::Function* fn = LLVM_D_GetRuntimeFunction(p->module, "_d_assert"); | |
2063 assert(fn); | |
2064 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", p->scopebb()); | |
2065 call->setCallingConv(llvm::CallingConv::C); | |
2066 | |
2067 //new llvm::UnreachableInst(p->scopebb()); | |
2068 | |
2069 return 0; | |
2070 } | |
2071 | |
2072 ////////////////////////////////////////////////////////////////////////////////////////// | |
2073 | |
2074 elem* DelegateExp::toElem(IRState* p) | |
2075 { | |
2076 Logger::print("DelegateExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2077 LOG_SCOPE; | |
2078 | |
2079 elem* e = new elem; | |
2080 elem* u = e1->toElem(p); | |
2081 | |
2082 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
2083 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
2084 | |
2085 const llvm::Type* int8ptrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
2086 | |
2087 llvm::Value* lval = p->toplval(); | |
2088 | |
2089 llvm::Value* context = new llvm::GetElementPtrInst(lval,zero,zero,"tmp",p->scopebb()); | |
2090 llvm::Value* castcontext = new llvm::BitCastInst(u->getValue(),int8ptrty,"tmp",p->scopebb()); | |
2091 new llvm::StoreInst(castcontext, context, p->scopebb()); | |
2092 | |
2093 llvm::Value* fptr = new llvm::GetElementPtrInst(lval,zero,one,"tmp",p->scopebb()); | |
2094 | |
2095 assert(func->llvmValue); | |
2096 llvm::Value* castfptr = new llvm::BitCastInst(func->llvmValue,fptr->getType()->getContainedType(0),"tmp",p->scopebb()); | |
2097 new llvm::StoreInst(castfptr, fptr, p->scopebb()); | |
2098 | |
2099 e->inplace = true; | |
2100 | |
2101 delete u; | |
2102 return e; | |
2103 } | |
2104 | |
2105 ////////////////////////////////////////////////////////////////////////////////////////// | |
2106 | |
2107 elem* IdentityExp::toElem(IRState* p) | |
2108 { | |
2109 Logger::print("IdentityExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2110 LOG_SCOPE; | |
2111 | |
2112 elem* u = e1->toElem(p); | |
2113 elem* v = e2->toElem(p); | |
2114 | |
2115 elem* e = new elem; | |
2116 | |
2117 llvm::Value* l = u->getValue(); | |
2118 llvm::Value* r = 0; | |
2119 if (v->type == elem::NUL) | |
2120 r = llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(l->getType())); | |
2121 else | |
2122 r = v->getValue(); | |
2123 | |
2124 llvm::ICmpInst::Predicate pred = (op == TOKidentity) ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE; | |
2125 e->val = new llvm::ICmpInst(pred, l, r, "tmp", p->scopebb()); | |
2126 e->type = elem::VAL; | |
2127 | |
2128 delete u; | |
2129 delete v; | |
2130 | |
2131 return e; | |
2132 } | |
2133 | |
2134 ////////////////////////////////////////////////////////////////////////////////////////// | |
2135 | |
2136 elem* CommaExp::toElem(IRState* p) | |
2137 { | |
2138 Logger::print("CommaExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2139 LOG_SCOPE; | |
2140 | |
2141 elem* u = e1->toElem(p); | |
2142 elem* v = e2->toElem(p); | |
2143 delete u; | |
2144 return v; | |
2145 } | |
2146 | |
2147 ////////////////////////////////////////////////////////////////////////////////////////// | |
2148 | |
2149 elem* CondExp::toElem(IRState* p) | |
2150 { | |
2151 Logger::print("CondExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2152 LOG_SCOPE; | |
2153 | |
2154 const llvm::Type* resty = LLVM_DtoType(type); | |
2155 | |
2156 // allocate a temporary for the final result. failed to come up with a better way :/ | |
2157 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
2158 llvm::Value* resval = new llvm::AllocaInst(resty,"condtmp",p->topallocapoint()); | |
2159 | |
2160 llvm::BasicBlock* oldend = p->scopeend(); | |
2161 llvm::BasicBlock* condtrue = new llvm::BasicBlock("condtrue", gIR->topfunc(), oldend); | |
2162 llvm::BasicBlock* condfalse = new llvm::BasicBlock("condfalse", gIR->topfunc(), oldend); | |
2163 llvm::BasicBlock* condend = new llvm::BasicBlock("condend", gIR->topfunc(), oldend); | |
2164 | |
2165 elem* c = econd->toElem(p); | |
2166 llvm::Value* cond_val = LLVM_DtoBoolean(c->getValue()); | |
2167 delete c; | |
2168 new llvm::BranchInst(condtrue,condfalse,cond_val,p->scopebb()); | |
2169 | |
2170 p->scope() = IRScope(condtrue, condfalse); | |
2171 elem* u = e1->toElem(p); | |
2172 Logger::cout() << *u->val << '|' << *resval << '\n'; \ | |
2173 new llvm::StoreInst(u->getValue(),resval,p->scopebb()); | |
2174 new llvm::BranchInst(condend,p->scopebb()); | |
2175 delete u; | |
2176 | |
2177 p->scope() = IRScope(condfalse, condend); | |
2178 elem* v = e2->toElem(p); | |
2179 new llvm::StoreInst(v->getValue(),resval,p->scopebb()); | |
2180 new llvm::BranchInst(condend,p->scopebb()); | |
2181 delete v; | |
2182 | |
2183 p->scope() = IRScope(condend, oldend); | |
2184 | |
2185 elem* e = new elem; | |
2186 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
2187 e->type = elem::VAL; | |
2188 return e; | |
2189 } | |
2190 | |
2191 ////////////////////////////////////////////////////////////////////////////////////////// | |
2192 | |
2193 elem* ComExp::toElem(IRState* p) | |
2194 { | |
2195 Logger::print("ComExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2196 LOG_SCOPE; | |
2197 | |
2198 elem* e = new elem; | |
2199 elem* u = e1->toElem(p); | |
2200 | |
2201 llvm::Value* value = u->getValue(); | |
2202 llvm::Value* minusone = llvm::ConstantInt::get(value->getType(), -1, true); | |
2203 e->val = llvm::BinaryOperator::create(llvm::Instruction::Xor, value, minusone, "tmp", p->scopebb()); | |
2204 | |
2205 delete u; | |
2206 | |
2207 e->type = elem::VAL; | |
2208 | |
2209 return e; | |
2210 } | |
2211 | |
2212 ////////////////////////////////////////////////////////////////////////////////////////// | |
2213 | |
2214 #define STUB(x) elem *x::toElem(IRState * p) {error("Exp type "#x" not implemented: %s", toChars()); fatal(); return 0; } | |
2215 //STUB(IdentityExp); | |
2216 //STUB(CondExp); | |
2217 //STUB(EqualExp); | |
2218 STUB(InExp); | |
2219 //STUB(CmpExp); | |
2220 //STUB(AndAndExp); | |
2221 //STUB(OrOrExp); | |
2222 //STUB(AndExp); | |
2223 //STUB(AndAssignExp); | |
2224 //STUB(OrExp); | |
2225 //STUB(OrAssignExp); | |
2226 //STUB(XorExp); | |
2227 //STUB(XorAssignExp); | |
2228 //STUB(ShrExp); | |
2229 //STUB(ShrAssignExp); | |
2230 //STUB(ShlExp); | |
2231 //STUB(ShlAssignExp); | |
2232 //STUB(UshrExp); | |
2233 //STUB(UshrAssignExp); | |
2234 //STUB(DivExp); | |
2235 //STUB(DivAssignExp); | |
2236 //STUB(MulExp); | |
2237 //STUB(MulAssignExp); | |
2238 //STUB(ModExp); | |
2239 //STUB(ModAssignExp); | |
2240 STUB(CatExp); | |
2241 STUB(CatAssignExp); | |
2242 //STUB(AddExp); | |
2243 //STUB(AddAssignExp); | |
2244 STUB(Expression); | |
2245 //STUB(MinExp); | |
2246 //STUB(MinAssignExp); | |
2247 //STUB(PostExp); | |
2248 //STUB(NullExp); | |
2249 //STUB(ThisExp); | |
2250 //STUB(CallExp); | |
2251 STUB(DotTypeExp); | |
2252 STUB(TypeDotIdExp); | |
2253 //STUB(DotVarExp); | |
2254 //STUB(AssertExp); | |
2255 STUB(FuncExp); | |
2256 //STUB(DelegateExp); | |
2257 //STUB(VarExp); | |
2258 //STUB(DeclarationExp); | |
2259 //STUB(NewExp); | |
2260 //STUB(SymOffExp); | |
2261 STUB(ScopeExp); | |
2262 //STUB(AssignExp); | |
2263 | |
2264 STUB(TypeExp); | |
2265 //STUB(RealExp); | |
2266 STUB(ComplexExp); | |
2267 //STUB(StringExp); | |
2268 //STUB(IntegerExp); | |
2269 STUB(BoolExp); | |
2270 | |
2271 //STUB(NotExp); | |
2272 //STUB(ComExp); | |
2273 STUB(NegExp); | |
2274 //STUB(PtrExp); | |
2275 //STUB(AddrExp); | |
2276 //STUB(SliceExp); | |
2277 //STUB(CastExp); | |
2278 //STUB(DeleteExp); | |
2279 //STUB(IndexExp); | |
2280 //STUB(CommaExp); | |
2281 //STUB(ArrayLengthExp); | |
2282 //STUB(HaltExp); | |
2283 STUB(RemoveExp); | |
2284 STUB(ArrayLiteralExp); | |
2285 STUB(AssocArrayLiteralExp); | |
2286 //STUB(StructLiteralExp); | |
2287 | |
2288 unsigned Type::totym() { return 0; } | |
2289 | |
2290 type * | |
2291 Type::toCtype() { | |
2292 return 0; | |
2293 } | |
2294 | |
2295 type * Type::toCParamtype() | |
2296 { | |
2297 return 0; | |
2298 } | |
2299 Symbol * Type::toSymbol() | |
2300 { | |
2301 return 0; | |
2302 } | |
2303 | |
2304 type * | |
2305 TypeTypedef::toCtype() | |
2306 { | |
2307 return 0; | |
2308 } | |
2309 | |
2310 type * | |
2311 TypeTypedef::toCParamtype() | |
2312 { | |
2313 return 0; | |
2314 } | |
2315 | |
2316 void | |
2317 TypedefDeclaration::toDebug() | |
2318 { | |
2319 } | |
2320 | |
2321 | |
2322 type * | |
2323 TypeEnum::toCtype() | |
2324 { | |
2325 return 0; | |
2326 } | |
2327 | |
2328 type * | |
2329 TypeStruct::toCtype() | |
2330 { | |
2331 return 0; | |
2332 } | |
2333 | |
2334 void | |
2335 StructDeclaration::toDebug() | |
2336 { | |
2337 } | |
2338 | |
2339 Symbol * TypeClass::toSymbol() | |
2340 { | |
2341 return 0; | |
2342 } | |
2343 | |
2344 unsigned TypeFunction::totym() | |
2345 { | |
2346 return 0; | |
2347 } | |
2348 | |
2349 type * | |
2350 TypeFunction::toCtype() | |
2351 { | |
2352 return 0; | |
2353 } | |
2354 | |
2355 type * | |
2356 TypeSArray::toCtype() | |
2357 { | |
2358 return 0; | |
2359 } | |
2360 | |
2361 type *TypeSArray::toCParamtype() { return 0; } | |
2362 | |
2363 type * | |
2364 TypeDArray::toCtype() | |
2365 { | |
2366 return 0; | |
2367 } | |
2368 | |
2369 type * | |
2370 TypeAArray::toCtype() | |
2371 { | |
2372 return 0; | |
2373 } | |
2374 | |
2375 type * | |
2376 TypePointer::toCtype() | |
2377 { | |
2378 return 0; | |
2379 } | |
2380 | |
2381 type * | |
2382 TypeDelegate::toCtype() | |
2383 { | |
2384 return 0; | |
2385 } | |
2386 | |
2387 type * | |
2388 TypeClass::toCtype() | |
2389 { | |
2390 return 0; | |
2391 } | |
2392 | |
2393 void | |
2394 ClassDeclaration::toDebug() | |
2395 { | |
2396 } | |
2397 | |
2398 /* --------------------------------------------------------------------------------------- */ | |
2399 | |
2400 void CompoundStatement::toIR(IRState* p) | |
2401 { | |
2402 static int csi = 0; | |
2403 Logger::println("CompoundStatement::toIR(%d):\n<<<\n%s>>>", csi++, toChars()); | |
2404 LOG_SCOPE; | |
2405 | |
2406 /* | |
2407 const char* labelname; | |
2408 bool insterm = false; | |
2409 | |
2410 if (!p->scopes()) { | |
2411 labelname = "bb"; | |
2412 insterm = true; | |
2413 } | |
2414 else | |
2415 labelname = "entry"; | |
2416 | |
2417 //if (!llvm::isa<llvm::TerminatorInst>(p->topfunc()->back().back())) | |
2418 // insterm = true; | |
2419 | |
2420 llvm::BasicBlock* bb = new llvm::BasicBlock(labelname, p->topfunc()); | |
2421 | |
2422 if (insterm) { | |
2423 new llvm::BranchInst(bb,p->topbb()); | |
2424 } | |
2425 | |
2426 p->bbs.push(bb); | |
2427 */ | |
2428 | |
2429 size_t n = statements->dim; | |
2430 for (size_t i=0; i<n; i++) | |
2431 { | |
2432 Statement* s = (Statement*)statements->data[i]; | |
2433 if (s) | |
2434 s->toIR(p); | |
2435 else | |
2436 Logger::println("NULL statement found in CompoundStatement !! :S"); | |
2437 } | |
2438 | |
2439 //p->bbs.pop(); | |
2440 } | |
2441 | |
2442 void ReturnStatement::toIR(IRState* p) | |
2443 { | |
2444 static int rsi = 0; | |
2445 Logger::println("ReturnStatement::toIR(%d): %s", rsi++, toChars()); | |
2446 LOG_SCOPE; | |
2447 | |
2448 if (exp) | |
2449 { | |
2450 TY expty = exp->type->ty; | |
2451 if (p->topfunc()->getReturnType() == llvm::Type::VoidTy) { | |
2452 assert(expty == Tstruct || expty == Tdelegate || expty == Tarray); | |
2453 | |
2454 TypeFunction* f = p->topfunctype(); | |
2455 assert(f->llvmRetInPtr && f->llvmRetArg); | |
2456 | |
2457 p->lvals.push_back(f->llvmRetArg); | |
2458 elem* e = exp->toElem(p); | |
2459 p->lvals.pop_back(); | |
2460 | |
2461 // structliterals do this themselves | |
2462 // also they dont produce any value | |
2463 if (expty == Tstruct) { | |
2464 if (!e->inplace) { | |
2465 TypeStruct* ts = (TypeStruct*)exp->type; | |
2466 assert(e->mem); | |
2467 LLVM_DtoStructCopy(ts,f->llvmRetArg,e->mem); | |
2468 } | |
2469 } | |
2470 else if (expty == Tdelegate) { | |
2471 // do nothing, handled by the DelegateExp | |
2472 LLVM_DtoDelegateCopy(f->llvmRetArg,e->mem); | |
2473 } | |
2474 else if (expty == Tarray) { | |
2475 if (e->type == elem::SLICE) { | |
2476 LLVM_DtoSetArray(f->llvmRetArg,e->arg,e->mem); | |
2477 } | |
2478 // else the return value is a variable and should already have been assigned by now | |
2479 } | |
2480 else | |
2481 assert(0); | |
2482 | |
2483 new llvm::ReturnInst(p->scopebb()); | |
2484 delete e; | |
2485 } | |
2486 else { | |
2487 elem* e = exp->toElem(p); | |
2488 llvm::Value* v = e->getValue(); | |
2489 Logger::cout() << *v << '\n'; | |
2490 new llvm::ReturnInst(v, p->scopebb()); | |
2491 delete e; | |
2492 } | |
2493 } | |
2494 else | |
2495 { | |
2496 if (p->topfunc()->getReturnType() == llvm::Type::VoidTy) | |
2497 new llvm::ReturnInst(p->scopebb()); | |
2498 else | |
2499 new llvm::UnreachableInst(p->scopebb()); | |
2500 } | |
2501 | |
2502 p->scope().returned = true; | |
2503 } | |
2504 | |
2505 void ExpStatement::toIR(IRState* p) | |
2506 { | |
2507 static int esi = 0; | |
2508 Logger::println("ExpStatement::toIR(%d): %s", esi++, toChars()); | |
2509 LOG_SCOPE; | |
2510 | |
2511 if (exp != 0) { | |
2512 elem* e = exp->toElem(p); | |
2513 delete e; | |
2514 } | |
2515 /*elem* e = exp->toElem(p); | |
2516 p->buf.printf("%s", e->toChars()); | |
2517 delete e; | |
2518 p->buf.writenl();*/ | |
2519 } | |
2520 | |
2521 void IfStatement::toIR(IRState* p) | |
2522 { | |
2523 static int wsi = 0; | |
2524 Logger::println("IfStatement::toIR(%d): %s", wsi++, toChars()); | |
2525 LOG_SCOPE; | |
2526 | |
2527 elem* cond_e = condition->toElem(p); | |
2528 llvm::Value* cond_val = cond_e->getValue(); | |
2529 delete cond_e; | |
2530 | |
2531 llvm::BasicBlock* oldend = gIR->scopeend(); | |
2532 | |
2533 llvm::BasicBlock* ifbb = new llvm::BasicBlock("if", gIR->topfunc(), oldend); | |
2534 llvm::BasicBlock* endbb = new llvm::BasicBlock("endif", gIR->topfunc(), oldend); | |
2535 llvm::BasicBlock* elsebb = 0; | |
2536 if (elsebody) { | |
2537 elsebb = new llvm::BasicBlock("else", gIR->topfunc(), endbb); | |
2538 } | |
2539 else { | |
2540 elsebb = endbb; | |
2541 } | |
2542 | |
2543 if (cond_val->getType() != llvm::Type::Int1Ty) { | |
2544 Logger::cout() << "if conditional: " << *cond_val << '\n'; | |
2545 cond_val = LLVM_DtoBoolean(cond_val); | |
2546 } | |
2547 llvm::Value* ifgoback = new llvm::BranchInst(ifbb, elsebb, cond_val, gIR->scopebegin()); | |
2548 | |
2549 // replace current scope | |
2550 gIR->scope() = IRScope(ifbb,elsebb); | |
2551 | |
2552 bool endifUsed = false; | |
2553 | |
2554 // do scoped statements | |
2555 ifbody->toIR(p); | |
2556 if (!gIR->scopereturned()) { | |
2557 new llvm::BranchInst(endbb,gIR->scopebegin()); | |
2558 endifUsed = true; | |
2559 } | |
2560 | |
2561 if (elsebody) { | |
2562 //assert(0); | |
2563 gIR->scope() = IRScope(elsebb,endbb); | |
2564 elsebody->toIR(p); | |
2565 if (!gIR->scopereturned()) { | |
2566 new llvm::BranchInst(endbb,gIR->scopebegin()); | |
2567 endifUsed = true; | |
2568 } | |
2569 } | |
2570 | |
2571 // rewrite the scope | |
2572 gIR->scope() = IRScope(endbb,oldend); | |
2573 } | |
2574 | |
2575 void ScopeStatement::toIR(IRState* p) | |
2576 { | |
2577 static int wsi = 0; | |
2578 Logger::println("ScopeStatement::toIR(%d): %s", wsi++, toChars()); | |
2579 LOG_SCOPE; | |
2580 | |
2581 llvm::BasicBlock* oldend = gIR->scopeend(); | |
2582 | |
2583 IRScope irs; | |
2584 irs.begin = new llvm::BasicBlock("scope", gIR->topfunc(), oldend); | |
2585 irs.end = new llvm::BasicBlock("endscope", gIR->topfunc(), oldend); | |
2586 | |
2587 // pass the previous BB into this | |
2588 new llvm::BranchInst(irs.begin, gIR->scopebegin()); | |
2589 | |
2590 gIR->scope() = irs; | |
2591 | |
2592 statement->toIR(p); | |
2593 if (!gIR->scopereturned()) { | |
2594 new llvm::BranchInst(irs.end, gIR->scopebegin()); | |
2595 } | |
2596 | |
2597 // rewrite the scope | |
2598 gIR->scope() = IRScope(irs.end,oldend); | |
2599 } | |
2600 | |
2601 void WhileStatement::toIR(IRState* p) | |
2602 { | |
2603 static int wsi = 0; | |
2604 Logger::println("WhileStatement::toIR(%d): %s", wsi++, toChars()); | |
2605 LOG_SCOPE; | |
2606 | |
2607 // create while blocks | |
2608 llvm::BasicBlock* oldend = gIR->scopeend(); | |
2609 llvm::BasicBlock* whilebb = new llvm::BasicBlock("whilecond", gIR->topfunc(), oldend); | |
2610 llvm::BasicBlock* endbb = new llvm::BasicBlock("endwhile", gIR->topfunc(), oldend); | |
2611 | |
2612 // move into the while block | |
2613 new llvm::BranchInst(whilebb, gIR->scopebegin()); | |
2614 | |
2615 // replace current scope | |
2616 gIR->scope() = IRScope(whilebb,endbb); | |
2617 | |
2618 // create the condition | |
2619 elem* cond_e = condition->toElem(p); | |
2620 llvm::Value* cond_val = LLVM_DtoBoolean(cond_e->getValue()); | |
2621 delete cond_e; | |
2622 | |
2623 // while body block | |
2624 llvm::BasicBlock* whilebodybb = new llvm::BasicBlock("whilebody", gIR->topfunc(), endbb); | |
2625 | |
2626 // conditional branch | |
2627 llvm::Value* ifbreak = new llvm::BranchInst(whilebodybb, endbb, cond_val, whilebb); | |
2628 | |
2629 // rewrite scope | |
2630 gIR->scope() = IRScope(whilebodybb,endbb); | |
2631 | |
2632 // do while body code | |
2633 body->toIR(p); | |
2634 | |
2635 // loop | |
2636 new llvm::BranchInst(whilebb, gIR->scopebegin()); | |
2637 | |
2638 // rewrite the scope | |
2639 gIR->scope() = IRScope(endbb,oldend); | |
2640 } | |
2641 | |
2642 void DoStatement::toIR(IRState* p) | |
2643 { | |
2644 static int wsi = 0; | |
2645 Logger::println("DoStatement::toIR(%d): %s", wsi++, toChars()); | |
2646 LOG_SCOPE; | |
2647 | |
2648 // create while blocks | |
2649 llvm::BasicBlock* oldend = gIR->scopeend(); | |
2650 llvm::BasicBlock* dowhilebb = new llvm::BasicBlock("dowhile", gIR->topfunc(), oldend); | |
2651 llvm::BasicBlock* endbb = new llvm::BasicBlock("enddowhile", gIR->topfunc(), oldend); | |
2652 | |
2653 // move into the while block | |
2654 new llvm::BranchInst(dowhilebb, gIR->scopebegin()); | |
2655 | |
2656 // replace current scope | |
2657 gIR->scope() = IRScope(dowhilebb,endbb); | |
2658 | |
2659 // do do-while body code | |
2660 body->toIR(p); | |
2661 | |
2662 // create the condition | |
2663 elem* cond_e = condition->toElem(p); | |
2664 llvm::Value* cond_val = LLVM_DtoBoolean(cond_e->getValue()); | |
2665 delete cond_e; | |
2666 | |
2667 // conditional branch | |
2668 llvm::Value* ifbreak = new llvm::BranchInst(dowhilebb, endbb, cond_val, gIR->scopebegin()); | |
2669 | |
2670 // rewrite the scope | |
2671 gIR->scope() = IRScope(endbb,oldend); | |
2672 } | |
2673 | |
2674 void ForStatement::toIR(IRState* p) | |
2675 { | |
2676 static int wsi = 0; | |
2677 Logger::println("ForStatement::toIR(%d): %s", wsi++, toChars()); | |
2678 LOG_SCOPE; | |
2679 | |
2680 // create for blocks | |
2681 llvm::BasicBlock* oldend = gIR->scopeend(); | |
2682 llvm::BasicBlock* forbb = new llvm::BasicBlock("forcond", gIR->topfunc(), oldend); | |
2683 llvm::BasicBlock* forbodybb = new llvm::BasicBlock("forbody", gIR->topfunc(), oldend); | |
2684 llvm::BasicBlock* forincbb = new llvm::BasicBlock("forinc", gIR->topfunc(), oldend); | |
2685 llvm::BasicBlock* endbb = new llvm::BasicBlock("endfor", gIR->topfunc(), oldend); | |
2686 | |
2687 // init | |
2688 if (init != 0) | |
2689 init->toIR(p); | |
2690 | |
2691 // move into the for condition block, ie. start the loop | |
2692 new llvm::BranchInst(forbb, gIR->scopebegin()); | |
2693 | |
2694 IRScope loop; | |
2695 loop.begin = forincbb; | |
2696 loop.end = endbb; | |
2697 p->loopbbs.push_back(loop); | |
2698 | |
2699 // replace current scope | |
2700 gIR->scope() = IRScope(forbb,forbodybb); | |
2701 | |
2702 // create the condition | |
2703 elem* cond_e = condition->toElem(p); | |
2704 llvm::Value* cond_val = LLVM_DtoBoolean(cond_e->getValue()); | |
2705 delete cond_e; | |
2706 | |
2707 // conditional branch | |
2708 llvm::Value* ifbreak = new llvm::BranchInst(forbodybb, endbb, cond_val, forbb); | |
2709 | |
2710 // rewrite scope | |
2711 gIR->scope() = IRScope(forbodybb,forincbb); | |
2712 | |
2713 // do for body code | |
2714 body->toIR(p); | |
2715 | |
2716 // move into the for increment block | |
2717 new llvm::BranchInst(forincbb, gIR->scopebegin()); | |
2718 gIR->scope() = IRScope(forincbb, endbb); | |
2719 | |
2720 // increment | |
2721 if (increment) { | |
2722 elem* inc = increment->toElem(p); | |
2723 delete inc; | |
2724 } | |
2725 | |
2726 // loop | |
2727 new llvm::BranchInst(forbb, gIR->scopebegin()); | |
2728 | |
2729 p->loopbbs.pop_back(); | |
2730 | |
2731 // rewrite the scope | |
2732 gIR->scope() = IRScope(endbb,oldend); | |
2733 } | |
2734 | |
2735 void BreakStatement::toIR(IRState* p) | |
2736 { | |
2737 static int wsi = 0; | |
2738 Logger::println("BreakStatement::toIR(%d): %s", wsi++, toChars()); | |
2739 LOG_SCOPE; | |
2740 | |
2741 if (ident != 0) { | |
2742 Logger::println("ident = %s", ident->toChars()); | |
2743 assert(0); | |
2744 } | |
2745 else { | |
2746 new llvm::BranchInst(gIR->loopbbs.back().end, gIR->scopebegin()); | |
2747 } | |
2748 } | |
2749 | |
2750 void ContinueStatement::toIR(IRState* p) | |
2751 { | |
2752 static int wsi = 0; | |
2753 Logger::println("ContinueStatement::toIR(%d): %s", wsi++, toChars()); | |
2754 LOG_SCOPE; | |
2755 | |
2756 if (ident != 0) { | |
2757 Logger::println("ident = %s", ident->toChars()); | |
2758 assert(0); | |
2759 } | |
2760 else { | |
2761 new llvm::BranchInst(gIR->loopbbs.back().begin, gIR->scopebegin()); | |
2762 } | |
2763 } | |
2764 | |
2765 void OnScopeStatement::toIR(IRState* p) | |
2766 { | |
2767 static int wsi = 0; | |
2768 Logger::println("OnScopeStatement::toIR(%d): %s", wsi++, toChars()); | |
2769 LOG_SCOPE; | |
2770 | |
2771 assert(statement); | |
2772 //statement->toIR(p); // this seems to be redundant | |
2773 } | |
2774 | |
2775 void TryFinallyStatement::toIR(IRState* p) | |
2776 { | |
2777 static int wsi = 0; | |
2778 Logger::println("TryFinallyStatement::toIR(%d): %s", wsi++, toChars()); | |
2779 LOG_SCOPE; | |
2780 | |
2781 llvm::BasicBlock* oldend = gIR->scopeend(); | |
2782 | |
2783 llvm::BasicBlock* trybb = new llvm::BasicBlock("try", gIR->topfunc(), oldend); | |
2784 llvm::BasicBlock* finallybb = new llvm::BasicBlock("finally", gIR->topfunc(), oldend); | |
2785 llvm::BasicBlock* endbb = new llvm::BasicBlock("endtryfinally", gIR->topfunc(), oldend); | |
2786 | |
2787 // pass the previous BB into this | |
2788 new llvm::BranchInst(trybb, gIR->scopebegin()); | |
2789 | |
2790 gIR->scope() = IRScope(trybb,finallybb); | |
2791 | |
2792 assert(body); | |
2793 body->toIR(p); | |
2794 new llvm::BranchInst(finallybb, gIR->scopebegin()); | |
2795 | |
2796 // rewrite the scope | |
2797 gIR->scope() = IRScope(finallybb,endbb); | |
2798 | |
2799 assert(finalbody); | |
2800 finalbody->toIR(p); | |
2801 new llvm::BranchInst(endbb, gIR->scopebegin()); | |
2802 | |
2803 // rewrite the scope | |
2804 gIR->scope() = IRScope(endbb,oldend); | |
2805 } | |
2806 | |
2807 void TryCatchStatement::toIR(IRState* p) | |
2808 { | |
2809 static int wsi = 0; | |
2810 Logger::println("TryCatchStatement::toIR(%d): %s", wsi++, toChars()); | |
2811 LOG_SCOPE; | |
2812 | |
2813 assert(0 && "try-catch is not properly"); | |
2814 | |
2815 assert(body); | |
2816 body->toIR(p); | |
2817 | |
2818 assert(catches); | |
2819 for(size_t i=0; i<catches->dim; ++i) | |
2820 { | |
2821 Catch* c = (Catch*)catches->data[i]; | |
2822 c->handler->toIR(p); | |
2823 } | |
2824 } | |
2825 | |
2826 void ThrowStatement::toIR(IRState* p) | |
2827 { | |
2828 static int wsi = 0; | |
2829 Logger::println("ThrowStatement::toIR(%d): %s", wsi++, toChars()); | |
2830 LOG_SCOPE; | |
2831 | |
2832 assert(0 && "throw is not implemented"); | |
2833 | |
2834 assert(exp); | |
2835 elem* e = exp->toElem(p); | |
2836 delete e; | |
2837 } | |
2838 | |
2839 #define STUBST(x) void x::toIR(IRState * p) {error("Statement type "#x" not implemented: %s", toChars());fatal();} | |
2840 //STUBST(BreakStatement); | |
2841 //STUBST(ForStatement); | |
2842 STUBST(WithStatement); | |
2843 STUBST(SynchronizedStatement); | |
2844 //STUBST(ReturnStatement); | |
2845 //STUBST(ContinueStatement); | |
2846 STUBST(DefaultStatement); | |
2847 STUBST(CaseStatement); | |
2848 STUBST(SwitchStatement); | |
2849 STUBST(SwitchErrorStatement); | |
2850 STUBST(Statement); | |
2851 //STUBST(IfStatement); | |
2852 STUBST(ForeachStatement); | |
2853 //STUBST(DoStatement); | |
2854 //STUBST(WhileStatement); | |
2855 //STUBST(ExpStatement); | |
2856 //STUBST(CompoundStatement); | |
2857 //STUBST(ScopeStatement); | |
2858 STUBST(AsmStatement); | |
2859 //STUBST(TryCatchStatement); | |
2860 //STUBST(TryFinallyStatement); | |
2861 STUBST(VolatileStatement); | |
2862 STUBST(LabelStatement); | |
2863 //STUBST(ThrowStatement); | |
2864 STUBST(GotoCaseStatement); | |
2865 STUBST(GotoDefaultStatement); | |
2866 STUBST(GotoStatement); | |
2867 STUBST(UnrolledLoopStatement); | |
2868 //STUBST(OnScopeStatement); | |
2869 | |
2870 | |
2871 void | |
2872 EnumDeclaration::toDebug() | |
2873 { | |
2874 | |
2875 } | |
2876 | |
2877 int | |
2878 Dsymbol::cvMember(unsigned char*) | |
2879 { | |
2880 return 0; | |
2881 } | |
2882 int | |
2883 EnumDeclaration::cvMember(unsigned char*) | |
2884 { | |
2885 return 0; | |
2886 } | |
2887 int | |
2888 FuncDeclaration::cvMember(unsigned char*) | |
2889 { | |
2890 return 0; | |
2891 } | |
2892 int | |
2893 VarDeclaration::cvMember(unsigned char*) | |
2894 { | |
2895 return 0; | |
2896 } | |
2897 int | |
2898 TypedefDeclaration::cvMember(unsigned char*) | |
2899 { | |
2900 return 0; | |
2901 } | |
2902 | |
2903 void obj_includelib(char*){} | |
2904 | |
2905 AsmStatement::AsmStatement(Loc loc, Token *tokens) : | |
2906 Statement(loc) | |
2907 { | |
2908 } | |
2909 Statement *AsmStatement::syntaxCopy() { | |
2910 return 0; | |
2911 } | |
2912 | |
2913 Statement *AsmStatement::semantic(Scope *sc) | |
2914 { | |
2915 return Statement::semantic(sc); | |
2916 } | |
2917 | |
2918 | |
2919 void AsmStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2920 { | |
2921 Statement::toCBuffer(buf, hgs); | |
2922 } | |
2923 | |
2924 int AsmStatement::comeFrom() | |
2925 { | |
2926 return FALSE; | |
2927 } | |
2928 | |
2929 void | |
2930 backend_init() | |
2931 { | |
2932 //LLVM_D_InitRuntime(); | |
2933 // lazily loaded | |
2934 } | |
2935 | |
2936 void | |
2937 backend_term() | |
2938 { | |
2939 LLVM_D_FreeRuntime(); | |
2940 } |