comparison gen/toir.cpp @ 1554:d6e8d5db259f

LLVMContext changes up to r77366
author Benjamin Kramer <benny.kra@gmail.com>
date Thu, 30 Jul 2009 15:25:10 +0200
parents a326f145a57b
children 4897323d47b5
comparison
equal deleted inserted replaced
1553:f55ca8a1598c 1554:d6e8d5db259f
286 Logger::print("IntegerExp::toConstElem: %s @ %s\n", toChars(), type->toChars()); 286 Logger::print("IntegerExp::toConstElem: %s @ %s\n", toChars(), type->toChars());
287 LOG_SCOPE; 287 LOG_SCOPE;
288 const LLType* t = DtoType(type); 288 const LLType* t = DtoType(type);
289 if (isaPointer(t)) { 289 if (isaPointer(t)) {
290 Logger::println("pointer"); 290 Logger::println("pointer");
291 LLConstant* i = gIR->context().getConstantInt(DtoSize_t(),(uint64_t)value,false); 291 LLConstant* i = LLConstantInt::get(DtoSize_t(),(uint64_t)value,false);
292 return llvm::ConstantExpr::getIntToPtr(i, t); 292 return llvm::ConstantExpr::getIntToPtr(i, t);
293 } 293 }
294 assert(llvm::isa<LLIntegerType>(t)); 294 assert(llvm::isa<LLIntegerType>(t));
295 LLConstant* c = gIR->context().getConstantInt(t,(uint64_t)value,!type->isunsigned()); 295 LLConstant* c = LLConstantInt::get(t,(uint64_t)value,!type->isunsigned());
296 assert(c); 296 assert(c);
297 if (Logger::enabled()) 297 if (Logger::enabled())
298 Logger::cout() << "value = " << *c << '\n'; 298 Logger::cout() << "value = " << *c << '\n';
299 return c; 299 return c;
300 } 300 }
336 Logger::print("NullExp::toConstElem(type=%s): %s\n", type->toChars(),toChars()); 336 Logger::print("NullExp::toConstElem(type=%s): %s\n", type->toChars(),toChars());
337 LOG_SCOPE; 337 LOG_SCOPE;
338 const LLType* t = DtoType(type); 338 const LLType* t = DtoType(type);
339 if (type->ty == Tarray) { 339 if (type->ty == Tarray) {
340 assert(isaStruct(t)); 340 assert(isaStruct(t));
341 return llvm::ConstantAggregateZero::get(t); 341 return gIR->context().getConstantAggregateZero(t);
342 } 342 }
343 else { 343 else {
344 return gIR->context().getNullValue(t); 344 return gIR->context().getNullValue(t);
345 } 345 }
346 assert(0); 346 assert(0);
400 400
401 LLConstant* _init; 401 LLConstant* _init;
402 if (cty->size() == 1) { 402 if (cty->size() == 1) {
403 uint8_t* str = (uint8_t*)string; 403 uint8_t* str = (uint8_t*)string;
404 std::string cont((char*)str, len); 404 std::string cont((char*)str, len);
405 _init = gIR->context().getConstantArray(cont,true); 405 _init = LLConstantArray::get(cont,true);
406 } 406 }
407 else if (cty->size() == 2) { 407 else if (cty->size() == 2) {
408 uint16_t* str = (uint16_t*)string; 408 uint16_t* str = (uint16_t*)string;
409 std::vector<LLConstant*> vals; 409 std::vector<LLConstant*> vals;
410 vals.reserve(len+1); 410 vals.reserve(len+1);
411 for(size_t i=0; i<len; ++i) { 411 for(size_t i=0; i<len; ++i) {
412 vals.push_back(gIR->context().getConstantInt(ct, str[i], false));; 412 vals.push_back(LLConstantInt::get(ct, str[i], false));;
413 } 413 }
414 vals.push_back(gIR->context().getConstantInt(ct, 0, false)); 414 vals.push_back(LLConstantInt::get(ct, 0, false));
415 _init = llvm::ConstantArray::get(at,vals); 415 _init = LLConstantArray::get(at,vals);
416 } 416 }
417 else if (cty->size() == 4) { 417 else if (cty->size() == 4) {
418 uint32_t* str = (uint32_t*)string; 418 uint32_t* str = (uint32_t*)string;
419 std::vector<LLConstant*> vals; 419 std::vector<LLConstant*> vals;
420 vals.reserve(len+1); 420 vals.reserve(len+1);
421 for(size_t i=0; i<len; ++i) { 421 for(size_t i=0; i<len; ++i) {
422 vals.push_back(gIR->context().getConstantInt(ct, str[i], false));; 422 vals.push_back(LLConstantInt::get(ct, str[i], false));;
423 } 423 }
424 vals.push_back(gIR->context().getConstantInt(ct, 0, false)); 424 vals.push_back(LLConstantInt::get(ct, 0, false));
425 _init = llvm::ConstantArray::get(at,vals); 425 _init = LLConstantArray::get(at,vals);
426 } 426 }
427 else 427 else
428 assert(0); 428 assert(0);
429 429
430 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::InternalLinkage; 430 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::InternalLinkage;
431 if (Logger::enabled()) 431 if (Logger::enabled())
432 Logger::cout() << "type: " << *at << "\ninit: " << *_init << '\n'; 432 Logger::cout() << "type: " << *at << "\ninit: " << *_init << '\n';
433 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(*gIR->module,at,true,_linkage,_init,".str"); 433 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(*gIR->module,at,true,_linkage,_init,".str");
434 434
435 llvm::ConstantInt* zero = gIR->context().getConstantInt(LLType::Int32Ty, 0, false); 435 llvm::ConstantInt* zero = LLConstantInt::get(LLType::Int32Ty, 0, false);
436 LLConstant* idxs[2] = { zero, zero }; 436 LLConstant* idxs[2] = { zero, zero };
437 LLConstant* arrptr = llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2); 437 LLConstant* arrptr = llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2);
438 438
439 if (dtype->ty == Tarray) { 439 if (dtype->ty == Tarray) {
440 LLConstant* clen = gIR->context().getConstantInt(DtoSize_t(),len,false); 440 LLConstant* clen = LLConstantInt::get(DtoSize_t(),len,false);
441 return new DImValue(type, DtoConstSlice(clen, arrptr)); 441 return new DImValue(type, DtoConstSlice(clen, arrptr));
442 } 442 }
443 else if (dtype->ty == Tsarray) { 443 else if (dtype->ty == Tsarray) {
444 const LLType* dstType = getPtrToType(LLArrayType::get(ct, len)); 444 const LLType* dstType = getPtrToType(LLArrayType::get(ct, len));
445 LLValue* emem = (gvar->getType() == dstType) ? gvar : DtoBitCast(gvar, dstType); 445 LLValue* emem = (gvar->getType() == dstType) ? gvar : DtoBitCast(gvar, dstType);
471 471
472 LLConstant* _init; 472 LLConstant* _init;
473 if (cty->size() == 1) { 473 if (cty->size() == 1) {
474 uint8_t* str = (uint8_t*)string; 474 uint8_t* str = (uint8_t*)string;
475 std::string cont((char*)str, len); 475 std::string cont((char*)str, len);
476 _init = gIR->context().getConstantArray(cont, nullterm); 476 _init = LLConstantArray::get(cont, nullterm);
477 } 477 }
478 else if (cty->size() == 2) { 478 else if (cty->size() == 2) {
479 uint16_t* str = (uint16_t*)string; 479 uint16_t* str = (uint16_t*)string;
480 std::vector<LLConstant*> vals; 480 std::vector<LLConstant*> vals;
481 vals.reserve(len+1); 481 vals.reserve(len+1);
482 for(size_t i=0; i<len; ++i) { 482 for(size_t i=0; i<len; ++i) {
483 vals.push_back(gIR->context().getConstantInt(ct, str[i], false));; 483 vals.push_back(LLConstantInt::get(ct, str[i], false));;
484 } 484 }
485 if (nullterm) 485 if (nullterm)
486 vals.push_back(gIR->context().getConstantInt(ct, 0, false)); 486 vals.push_back(LLConstantInt::get(ct, 0, false));
487 _init = llvm::ConstantArray::get(at,vals); 487 _init = LLConstantArray::get(at,vals);
488 } 488 }
489 else if (cty->size() == 4) { 489 else if (cty->size() == 4) {
490 uint32_t* str = (uint32_t*)string; 490 uint32_t* str = (uint32_t*)string;
491 std::vector<LLConstant*> vals; 491 std::vector<LLConstant*> vals;
492 vals.reserve(len+1); 492 vals.reserve(len+1);
493 for(size_t i=0; i<len; ++i) { 493 for(size_t i=0; i<len; ++i) {
494 vals.push_back(gIR->context().getConstantInt(ct, str[i], false));; 494 vals.push_back(LLConstantInt::get(ct, str[i], false));;
495 } 495 }
496 if (nullterm) 496 if (nullterm)
497 vals.push_back(gIR->context().getConstantInt(ct, 0, false)); 497 vals.push_back(LLConstantInt::get(ct, 0, false));
498 _init = llvm::ConstantArray::get(at,vals); 498 _init = LLConstantArray::get(at,vals);
499 } 499 }
500 else 500 else
501 assert(0); 501 assert(0);
502 502
503 if (t->ty == Tsarray) 503 if (t->ty == Tsarray)
506 } 506 }
507 507
508 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::InternalLinkage; 508 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::InternalLinkage;
509 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(*gIR->module,_init->getType(),true,_linkage,_init,".str"); 509 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(*gIR->module,_init->getType(),true,_linkage,_init,".str");
510 510
511 llvm::ConstantInt* zero = gIR->context().getConstantInt(LLType::Int32Ty, 0, false); 511 llvm::ConstantInt* zero = LLConstantInt::get(LLType::Int32Ty, 0, false);
512 LLConstant* idxs[2] = { zero, zero }; 512 LLConstant* idxs[2] = { zero, zero };
513 LLConstant* arrptr = llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2); 513 LLConstant* arrptr = llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2);
514 514
515 if (t->ty == Tpointer) { 515 if (t->ty == Tpointer) {
516 return arrptr; 516 return arrptr;
517 } 517 }
518 else if (t->ty == Tarray) { 518 else if (t->ty == Tarray) {
519 LLConstant* clen = gIR->context().getConstantInt(DtoSize_t(),len,false); 519 LLConstant* clen = LLConstantInt::get(DtoSize_t(),len,false);
520 return DtoConstSlice(clen, arrptr); 520 return DtoConstSlice(clen, arrptr);
521 } 521 }
522 522
523 assert(0); 523 assert(0);
524 return NULL; 524 return NULL;
1354 case TOKlg: 1354 case TOKlg:
1355 cmpop = llvm::ICmpInst::ICMP_NE; 1355 cmpop = llvm::ICmpInst::ICMP_NE;
1356 break; 1356 break;
1357 case TOKleg: 1357 case TOKleg:
1358 skip = true; 1358 skip = true;
1359 eval = gIR->context().getConstantIntTrue(); 1359 eval = gIR->context().getTrue();
1360 break; 1360 break;
1361 case TOKunord: 1361 case TOKunord:
1362 skip = true; 1362 skip = true;
1363 eval = gIR->context().getConstantIntFalse(); 1363 eval = gIR->context().getFalse();
1364 break; 1364 break;
1365 1365
1366 default: 1366 default:
1367 assert(0); 1367 assert(0);
1368 } 1368 }
1523 Type* e2type = e2->type->toBasetype(); 1523 Type* e2type = e2->type->toBasetype();
1524 1524
1525 if (e1type->isintegral()) 1525 if (e1type->isintegral())
1526 { 1526 {
1527 assert(e2type->isintegral()); 1527 assert(e2type->isintegral());
1528 LLValue* one = gIR->context().getConstantInt(val->getType(), 1, !e2type->isunsigned()); 1528 LLValue* one = LLConstantInt::get(val->getType(), 1, !e2type->isunsigned());
1529 if (op == TOKplusplus) { 1529 if (op == TOKplusplus) {
1530 post = llvm::BinaryOperator::CreateAdd(val,one,"tmp",p->scopebb()); 1530 post = llvm::BinaryOperator::CreateAdd(val,one,"tmp",p->scopebb());
1531 } 1531 }
1532 else if (op == TOKminusminus) { 1532 else if (op == TOKminusminus) {
1533 post = llvm::BinaryOperator::CreateSub(val,one,"tmp",p->scopebb()); 1533 post = llvm::BinaryOperator::CreateSub(val,one,"tmp",p->scopebb());
1534 } 1534 }
1535 } 1535 }
1536 else if (e1type->ty == Tpointer) 1536 else if (e1type->ty == Tpointer)
1537 { 1537 {
1538 assert(e2type->isintegral()); 1538 assert(e2type->isintegral());
1539 LLConstant* minusone = gIR->context().getConstantInt(DtoSize_t(),(uint64_t)-1,true); 1539 LLConstant* minusone = LLConstantInt::get(DtoSize_t(),(uint64_t)-1,true);
1540 LLConstant* plusone = gIR->context().getConstantInt(DtoSize_t(),(uint64_t)1,false); 1540 LLConstant* plusone = LLConstantInt::get(DtoSize_t(),(uint64_t)1,false);
1541 LLConstant* whichone = (op == TOKplusplus) ? plusone : minusone; 1541 LLConstant* whichone = (op == TOKplusplus) ? plusone : minusone;
1542 post = llvm::GetElementPtrInst::Create(val, whichone, "tmp", p->scopebb()); 1542 post = llvm::GetElementPtrInst::Create(val, whichone, "tmp", p->scopebb());
1543 } 1543 }
1544 else if (e1type->isfloating()) 1544 else if (e1type->isfloating())
1545 { 1545 {
1842 resval = ubool; 1842 resval = ubool;
1843 } else { 1843 } else {
1844 llvm::PHINode* phi = p->ir->CreatePHI(LLType::Int1Ty, "andandval"); 1844 llvm::PHINode* phi = p->ir->CreatePHI(LLType::Int1Ty, "andandval");
1845 // If we jumped over evaluation of the right-hand side, 1845 // If we jumped over evaluation of the right-hand side,
1846 // the result is false. Otherwise it's the value of the right-hand side. 1846 // the result is false. Otherwise it's the value of the right-hand side.
1847 phi->addIncoming(gIR->context().getConstantIntFalse(), oldblock); 1847 phi->addIncoming(gIR->context().getFalse(), oldblock);
1848 phi->addIncoming(vbool, newblock); 1848 phi->addIncoming(vbool, newblock);
1849 resval = phi; 1849 resval = phi;
1850 } 1850 }
1851 1851
1852 return new DImValue(type, resval); 1852 return new DImValue(type, resval);
1889 resval = ubool; 1889 resval = ubool;
1890 } else { 1890 } else {
1891 llvm::PHINode* phi = p->ir->CreatePHI(LLType::Int1Ty, "ororval"); 1891 llvm::PHINode* phi = p->ir->CreatePHI(LLType::Int1Ty, "ororval");
1892 // If we jumped over evaluation of the right-hand side, 1892 // If we jumped over evaluation of the right-hand side,
1893 // the result is true. Otherwise, it's the value of the right-hand side. 1893 // the result is true. Otherwise, it's the value of the right-hand side.
1894 phi->addIncoming(gIR->context().getConstantIntTrue(), oldblock); 1894 phi->addIncoming(gIR->context().getTrue(), oldblock);
1895 phi->addIncoming(vbool, newblock); 1895 phi->addIncoming(vbool, newblock);
1896 resval = phi; 1896 resval = phi;
1897 } 1897 }
1898 1898
1899 return new DImValue(type, resval); 1899 return new DImValue(type, resval);
2144 LOG_SCOPE; 2144 LOG_SCOPE;
2145 2145
2146 DValue* u = e1->toElem(p); 2146 DValue* u = e1->toElem(p);
2147 2147
2148 LLValue* value = u->getRVal(); 2148 LLValue* value = u->getRVal();
2149 LLValue* minusone = gIR->context().getConstantInt(value->getType(), (uint64_t)-1, true); 2149 LLValue* minusone = LLConstantInt::get(value->getType(), (uint64_t)-1, true);
2150 value = llvm::BinaryOperator::Create(llvm::Instruction::Xor, value, minusone, "tmp", p->scopebb()); 2150 value = llvm::BinaryOperator::Create(llvm::Instruction::Xor, value, minusone, "tmp", p->scopebb());
2151 2151
2152 return new DImValue(type, value); 2152 return new DImValue(type, value);
2153 } 2153 }
2154 2154
2374 Expression* expr = (Expression*)elements->data[i]; 2374 Expression* expr = (Expression*)elements->data[i];
2375 vals[i] = expr->toConstElem(p); 2375 vals[i] = expr->toConstElem(p);
2376 } 2376 }
2377 2377
2378 // build the constant array initializer 2378 // build the constant array initializer
2379 LLConstant* initval = llvm::ConstantArray::get(arrtype, vals); 2379 LLConstant* initval = LLConstantArray::get(arrtype, vals);
2380 2380
2381 // if static array, we're done 2381 // if static array, we're done
2382 if (!dyn) 2382 if (!dyn)
2383 return initval; 2383 return initval;
2384 2384
2499 std::vector<LLConstant*> constvals(values.size(), NULL); 2499 std::vector<LLConstant*> constvals(values.size(), NULL);
2500 for (size_t i = 0; i < values.size(); ++i) 2500 for (size_t i = 0; i < values.size(); ++i)
2501 constvals[i] = llvm::cast<LLConstant>(values[i]); 2501 constvals[i] = llvm::cast<LLConstant>(values[i]);
2502 2502
2503 // return constant struct 2503 // return constant struct
2504 return gIR->context().getConstantStruct(constvals, sd->ir.irStruct->packed); 2504 return LLConstantStruct::get(constvals, sd->ir.irStruct->packed);
2505 } 2505 }
2506 2506
2507 ////////////////////////////////////////////////////////////////////////////////////////// 2507 //////////////////////////////////////////////////////////////////////////////////////////
2508 2508
2509 DValue* InExp::toElem(IRState* p) 2509 DValue* InExp::toElem(IRState* p)