Mercurial > projects > ldc
comparison gen/toir.cpp @ 308:6b62e8cdf970 trunk
[svn r329] Cleaned up a bunch of array code for handling special slice cases no
longer relevant.
author | lindquist |
---|---|
date | Sat, 28 Jun 2008 05:57:16 +0200 |
parents | 895e1b50cf2a |
children | a498b736a0bd |
comparison
equal
deleted
inserted
replaced
307:7ade5e035beb | 308:6b62e8cdf970 |
---|---|
1527 DValue* SliceExp::toElem(IRState* p) | 1527 DValue* SliceExp::toElem(IRState* p) |
1528 { | 1528 { |
1529 Logger::print("SliceExp::toElem: %s | %s\n", toChars(), type->toChars()); | 1529 Logger::print("SliceExp::toElem: %s | %s\n", toChars(), type->toChars()); |
1530 LOG_SCOPE; | 1530 LOG_SCOPE; |
1531 | 1531 |
1532 Type* t = DtoDType(type); | 1532 // this is the new slicing code, it's different in that a full slice will no longer retain the original pointer. |
1533 Type* e1type = DtoDType(e1->type); | 1533 // but this was broken if there *was* no original pointer, ie. a slice of a slice... |
1534 | 1534 // now all slices have *both* the 'len' and 'ptr' fields set to != null. |
1535 DValue* v = e1->toElem(p); | 1535 |
1536 LLValue* vmem = v->getRVal(); | 1536 // value being sliced |
1537 assert(vmem); | 1537 LLValue* elen; |
1538 | 1538 LLValue* eptr; |
1539 LLValue* zero = DtoConstUint(0); | 1539 DValue* e = e1->toElem(p); |
1540 LLValue* one = DtoConstUint(1); | 1540 |
1541 | 1541 // handle pointer slicing |
1542 LLValue* emem = 0; | 1542 Type* etype = e1->type->toBasetype(); |
1543 LLValue* earg = 0; | 1543 if (etype->ty == Tpointer) |
1544 | 1544 { |
1545 // partial slice | 1545 assert(lwr); |
1546 eptr = e->getRVal(); | |
1547 } | |
1548 // array slice | |
1549 else | |
1550 { | |
1551 eptr = DtoArrayPtr(e); | |
1552 } | |
1553 | |
1554 // has lower bound, pointer needs adjustment | |
1546 if (lwr) | 1555 if (lwr) |
1547 { | 1556 { |
1557 // must have upper bound too then | |
1548 assert(upr); | 1558 assert(upr); |
1549 p->arrays.push_back(v); | 1559 |
1560 // get bounds (make sure $ works) | |
1561 p->arrays.push_back(e); | |
1550 DValue* lo = lwr->toElem(p); | 1562 DValue* lo = lwr->toElem(p); |
1551 | |
1552 bool lwr_is_zero = false; | |
1553 if (DConstValue* cv = lo->isConst()) | |
1554 { | |
1555 assert(llvm::isa<llvm::ConstantInt>(cv->c)); | |
1556 | |
1557 if (e1type->ty == Tpointer) { | |
1558 emem = v->getRVal(); | |
1559 } | |
1560 else if (e1type->ty == Tarray) { | |
1561 LLValue* tmp = DtoGEP(vmem,zero,one); | |
1562 emem = DtoLoad(tmp); | |
1563 } | |
1564 else if (e1type->ty == Tsarray) { | |
1565 emem = DtoGEP(vmem,zero,zero); | |
1566 } | |
1567 else | |
1568 assert(emem); | |
1569 | |
1570 llvm::ConstantInt* c = llvm::cast<llvm::ConstantInt>(cv->c); | |
1571 if (!(lwr_is_zero = c->isZero())) { | |
1572 emem = DtoGEP1(emem,cv->c); | |
1573 } | |
1574 } | |
1575 else | |
1576 { | |
1577 if (e1type->ty == Tarray) { | |
1578 LLValue* tmp = DtoGEP(vmem,zero,one); | |
1579 tmp = DtoLoad(tmp); | |
1580 emem = DtoGEP1(tmp,lo->getRVal()); | |
1581 } | |
1582 else if (e1type->ty == Tsarray) { | |
1583 emem = DtoGEP(vmem,zero,lo->getRVal()); | |
1584 } | |
1585 else if (e1type->ty == Tpointer) { | |
1586 emem = DtoGEP1(v->getRVal(),lo->getRVal()); | |
1587 } | |
1588 else { | |
1589 Logger::println("type = %s", e1type->toChars()); | |
1590 assert(0); | |
1591 } | |
1592 } | |
1593 | |
1594 DValue* up = upr->toElem(p); | 1563 DValue* up = upr->toElem(p); |
1595 p->arrays.pop_back(); | 1564 p->arrays.pop_back(); |
1596 | 1565 LLValue* vlo = lo->getRVal(); |
1597 if (DConstValue* cv = up->isConst()) | 1566 LLValue* vup = up->getRVal(); |
1598 { | 1567 |
1599 assert(llvm::isa<llvm::ConstantInt>(cv->c)); | 1568 // offset by lower |
1600 if (lwr_is_zero) { | 1569 eptr = DtoGEP1(eptr, vlo); |
1601 earg = cv->c; | 1570 |
1602 } | 1571 // adjust length |
1603 else { | 1572 elen = p->ir->CreateSub(vup, vlo, "tmp"); |
1604 if (lo->isConst()) { | 1573 } |
1605 LLConstant* clo = llvm::cast<llvm::Constant>(lo->getRVal()); | 1574 // no bounds or full slice -> just convert to slice |
1606 LLConstant* cup = llvm::cast<llvm::Constant>(cv->c); | |
1607 earg = llvm::ConstantExpr::getSub(cup, clo); | |
1608 } | |
1609 else { | |
1610 earg = llvm::BinaryOperator::createSub(cv->c, lo->getRVal(), "tmp", p->scopebb()); | |
1611 } | |
1612 } | |
1613 } | |
1614 else | |
1615 { | |
1616 if (lwr_is_zero) { | |
1617 earg = up->getRVal(); | |
1618 } | |
1619 else { | |
1620 earg = llvm::BinaryOperator::createSub(up->getRVal(), lo->getRVal(), "tmp", p->scopebb()); | |
1621 } | |
1622 } | |
1623 } | |
1624 // full slice | |
1625 else | 1575 else |
1626 { | 1576 { |
1627 emem = vmem; | 1577 assert(e1->type->toBasetype()->ty != Tpointer); |
1628 } | 1578 elen = DtoArrayLen(e); |
1629 | 1579 } |
1630 if (earg) Logger::cout() << "slice exp result, length = " << *earg << '\n'; | 1580 |
1631 Logger::cout() << "slice exp result, ptr = " << *emem << '\n'; | 1581 return new DSliceValue(type, elen, eptr); |
1632 | |
1633 return new DSliceValue(type,earg,emem); | |
1634 } | 1582 } |
1635 | 1583 |
1636 ////////////////////////////////////////////////////////////////////////////////////////// | 1584 ////////////////////////////////////////////////////////////////////////////////////////// |
1637 | 1585 |
1638 DValue* CmpExp::toElem(IRState* p) | 1586 DValue* CmpExp::toElem(IRState* p) |
2338 LOG_SCOPE; | 2286 LOG_SCOPE; |
2339 | 2287 |
2340 DValue* u = e1->toElem(p); | 2288 DValue* u = e1->toElem(p); |
2341 DValue* v = e2->toElem(p); | 2289 DValue* v = e2->toElem(p); |
2342 | 2290 |
2291 Type* t1 = e1->type->toBasetype(); | |
2292 | |
2293 // handle dynarray specially | |
2294 if (t1->ty == Tarray) | |
2295 return new DImValue(type, DtoDynArrayIs(op,u,v)); | |
2296 | |
2343 LLValue* l = u->getRVal(); | 2297 LLValue* l = u->getRVal(); |
2344 LLValue* r = v->getRVal(); | 2298 LLValue* r = v->getRVal(); |
2345 | |
2346 Type* t1 = DtoDType(e1->type); | |
2347 | |
2348 LLValue* eval = 0; | 2299 LLValue* eval = 0; |
2349 | 2300 |
2350 if (t1->ty == Tarray) { | 2301 if (t1->ty == Tdelegate) { |
2351 if (v->isNull()) { | |
2352 r = NULL; | |
2353 } | |
2354 else { | |
2355 assert(l->getType() == r->getType()); | |
2356 } | |
2357 eval = DtoDynArrayIs(op,l,r); | |
2358 } | |
2359 else if (t1->ty == Tdelegate) { | |
2360 if (v->isNull()) { | 2302 if (v->isNull()) { |
2361 r = NULL; | 2303 r = NULL; |
2362 } | 2304 } |
2363 else { | 2305 else { |
2364 assert(l->getType() == r->getType()); | 2306 assert(l->getType() == r->getType()); |
2621 { | 2563 { |
2622 DValue* topval = p->topexp()->v; | 2564 DValue* topval = p->topexp()->v; |
2623 // slice assignment (copy) | 2565 // slice assignment (copy) |
2624 if (DSliceValue* s = topval->isSlice()) | 2566 if (DSliceValue* s = topval->isSlice()) |
2625 { | 2567 { |
2626 dstMem = s->ptr; | 2568 assert(s->ptr->getType()->getContainedType(0) == llStoType->getContainedType(0)); |
2569 dstMem = DtoBitCast(s->ptr, getPtrToType(llStoType)); | |
2627 sliceInPlace = true; | 2570 sliceInPlace = true; |
2628 assert(s->len == NULL); | 2571 // FIXME: insert bounds checks |
2629 } | 2572 } |
2630 // static array assignment | 2573 // static array assignment |
2631 else if (topval->getType()->toBasetype()->ty == Tsarray) | 2574 else if (topval->getType()->toBasetype()->ty == Tsarray) |
2632 { | 2575 { |
2633 dstMem = topval->getLVal(); | 2576 dstMem = topval->getLVal(); |