comparison lib/MC/MCExpr.cpp @ 77:54457678186b LLVM3.6

LLVM 3.6
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Mon, 08 Sep 2014 22:06:00 +0900
parents e4204d083e25
children 60c9769439b8
comparison
equal deleted inserted replaced
34:e874dbf0ad9d 77:54457678186b
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 9
10 #define DEBUG_TYPE "mcexpr"
11 #include "llvm/MC/MCExpr.h" 10 #include "llvm/MC/MCExpr.h"
12 #include "llvm/ADT/Statistic.h" 11 #include "llvm/ADT/Statistic.h"
13 #include "llvm/ADT/StringSwitch.h" 12 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/MC/MCAsmInfo.h" 13 #include "llvm/MC/MCAsmInfo.h"
15 #include "llvm/MC/MCAsmLayout.h" 14 #include "llvm/MC/MCAsmLayout.h"
20 #include "llvm/MC/MCValue.h" 19 #include "llvm/MC/MCValue.h"
21 #include "llvm/Support/Debug.h" 20 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h" 21 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h" 22 #include "llvm/Support/raw_ostream.h"
24 using namespace llvm; 23 using namespace llvm;
24
25 #define DEBUG_TYPE "mcexpr"
25 26
26 namespace { 27 namespace {
27 namespace stats { 28 namespace stats {
28 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations"); 29 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
29 } 30 }
177 case VK_TLSLD: return "TLSLD"; 178 case VK_TLSLD: return "TLSLD";
178 case VK_TLSLDM: return "TLSLDM"; 179 case VK_TLSLDM: return "TLSLDM";
179 case VK_TPOFF: return "TPOFF"; 180 case VK_TPOFF: return "TPOFF";
180 case VK_DTPOFF: return "DTPOFF"; 181 case VK_DTPOFF: return "DTPOFF";
181 case VK_TLVP: return "TLVP"; 182 case VK_TLVP: return "TLVP";
183 case VK_TLVPPAGE: return "TLVPPAGE";
184 case VK_TLVPPAGEOFF: return "TLVPPAGEOFF";
185 case VK_PAGE: return "PAGE";
186 case VK_PAGEOFF: return "PAGEOFF";
187 case VK_GOTPAGE: return "GOTPAGE";
188 case VK_GOTPAGEOFF: return "GOTPAGEOFF";
182 case VK_SECREL: return "SECREL32"; 189 case VK_SECREL: return "SECREL32";
190 case VK_WEAKREF: return "WEAKREF";
183 case VK_ARM_NONE: return "none"; 191 case VK_ARM_NONE: return "none";
184 case VK_ARM_TARGET1: return "target1"; 192 case VK_ARM_TARGET1: return "target1";
185 case VK_ARM_TARGET2: return "target2"; 193 case VK_ARM_TARGET2: return "target2";
186 case VK_ARM_PREL31: return "prel31"; 194 case VK_ARM_PREL31: return "prel31";
195 case VK_ARM_TLSLDO: return "tlsldo";
196 case VK_ARM_TLSCALL: return "tlscall";
197 case VK_ARM_TLSDESC: return "tlsdesc";
198 case VK_ARM_TLSDESCSEQ: return "tlsdescseq";
187 case VK_PPC_LO: return "l"; 199 case VK_PPC_LO: return "l";
188 case VK_PPC_HI: return "h"; 200 case VK_PPC_HI: return "h";
189 case VK_PPC_HA: return "ha"; 201 case VK_PPC_HA: return "ha";
190 case VK_PPC_HIGHER: return "higher"; 202 case VK_PPC_HIGHER: return "higher";
191 case VK_PPC_HIGHERA: return "highera"; 203 case VK_PPC_HIGHERA: return "highera";
257 case VK_Mips_HIGHEST: return "HIGHEST"; 269 case VK_Mips_HIGHEST: return "HIGHEST";
258 case VK_Mips_GOT_HI16: return "GOT_HI16"; 270 case VK_Mips_GOT_HI16: return "GOT_HI16";
259 case VK_Mips_GOT_LO16: return "GOT_LO16"; 271 case VK_Mips_GOT_LO16: return "GOT_LO16";
260 case VK_Mips_CALL_HI16: return "CALL_HI16"; 272 case VK_Mips_CALL_HI16: return "CALL_HI16";
261 case VK_Mips_CALL_LO16: return "CALL_LO16"; 273 case VK_Mips_CALL_LO16: return "CALL_LO16";
274 case VK_Mips_PCREL_HI16: return "PCREL_HI16";
275 case VK_Mips_PCREL_LO16: return "PCREL_LO16";
262 case VK_COFF_IMGREL32: return "IMGREL32"; 276 case VK_COFF_IMGREL32: return "IMGREL32";
263 } 277 }
264 llvm_unreachable("Invalid variant kind"); 278 llvm_unreachable("Invalid variant kind");
265 } 279 }
266 280
271 .Case("got", VK_GOT) 285 .Case("got", VK_GOT)
272 .Case("GOTOFF", VK_GOTOFF) 286 .Case("GOTOFF", VK_GOTOFF)
273 .Case("gotoff", VK_GOTOFF) 287 .Case("gotoff", VK_GOTOFF)
274 .Case("GOTPCREL", VK_GOTPCREL) 288 .Case("GOTPCREL", VK_GOTPCREL)
275 .Case("gotpcrel", VK_GOTPCREL) 289 .Case("gotpcrel", VK_GOTPCREL)
290 .Case("GOT_PREL", VK_GOTPCREL)
291 .Case("got_prel", VK_GOTPCREL)
276 .Case("GOTTPOFF", VK_GOTTPOFF) 292 .Case("GOTTPOFF", VK_GOTTPOFF)
277 .Case("gottpoff", VK_GOTTPOFF) 293 .Case("gottpoff", VK_GOTTPOFF)
278 .Case("INDNTPOFF", VK_INDNTPOFF) 294 .Case("INDNTPOFF", VK_INDNTPOFF)
279 .Case("indntpoff", VK_INDNTPOFF) 295 .Case("indntpoff", VK_INDNTPOFF)
280 .Case("NTPOFF", VK_NTPOFF) 296 .Case("NTPOFF", VK_NTPOFF)
293 .Case("tpoff", VK_TPOFF) 309 .Case("tpoff", VK_TPOFF)
294 .Case("DTPOFF", VK_DTPOFF) 310 .Case("DTPOFF", VK_DTPOFF)
295 .Case("dtpoff", VK_DTPOFF) 311 .Case("dtpoff", VK_DTPOFF)
296 .Case("TLVP", VK_TLVP) 312 .Case("TLVP", VK_TLVP)
297 .Case("tlvp", VK_TLVP) 313 .Case("tlvp", VK_TLVP)
314 .Case("TLVPPAGE", VK_TLVPPAGE)
315 .Case("tlvppage", VK_TLVPPAGE)
316 .Case("TLVPPAGEOFF", VK_TLVPPAGEOFF)
317 .Case("tlvppageoff", VK_TLVPPAGEOFF)
318 .Case("PAGE", VK_PAGE)
319 .Case("page", VK_PAGE)
320 .Case("PAGEOFF", VK_PAGEOFF)
321 .Case("pageoff", VK_PAGEOFF)
322 .Case("GOTPAGE", VK_GOTPAGE)
323 .Case("gotpage", VK_GOTPAGE)
324 .Case("GOTPAGEOFF", VK_GOTPAGEOFF)
325 .Case("gotpageoff", VK_GOTPAGEOFF)
298 .Case("IMGREL", VK_COFF_IMGREL32) 326 .Case("IMGREL", VK_COFF_IMGREL32)
299 .Case("imgrel", VK_COFF_IMGREL32) 327 .Case("imgrel", VK_COFF_IMGREL32)
300 .Case("SECREL32", VK_SECREL) 328 .Case("SECREL32", VK_SECREL)
301 .Case("secrel32", VK_SECREL) 329 .Case("secrel32", VK_SECREL)
302 .Case("L", VK_PPC_LO) 330 .Case("L", VK_PPC_LO)
403 .Case("target1", VK_ARM_TARGET1) 431 .Case("target1", VK_ARM_TARGET1)
404 .Case("TARGET2", VK_ARM_TARGET2) 432 .Case("TARGET2", VK_ARM_TARGET2)
405 .Case("target2", VK_ARM_TARGET2) 433 .Case("target2", VK_ARM_TARGET2)
406 .Case("PREL31", VK_ARM_PREL31) 434 .Case("PREL31", VK_ARM_PREL31)
407 .Case("prel31", VK_ARM_PREL31) 435 .Case("prel31", VK_ARM_PREL31)
436 .Case("TLSLDO", VK_ARM_TLSLDO)
437 .Case("tlsldo", VK_ARM_TLSLDO)
438 .Case("TLSCALL", VK_ARM_TLSCALL)
439 .Case("tlscall", VK_ARM_TLSCALL)
440 .Case("TLSDESC", VK_ARM_TLSDESC)
441 .Case("tlsdesc", VK_ARM_TLSDESC)
408 .Default(VK_Invalid); 442 .Default(VK_Invalid);
409 } 443 }
410 444
411 /* *** */ 445 /* *** */
412 446
413 void MCTargetExpr::anchor() {} 447 void MCTargetExpr::anchor() {}
414 448
415 /* *** */ 449 /* *** */
416 450
417 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const { 451 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
418 return EvaluateAsAbsolute(Res, 0, 0, 0); 452 return EvaluateAsAbsolute(Res, nullptr, nullptr, nullptr);
419 } 453 }
420 454
421 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, 455 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
422 const MCAsmLayout &Layout) const { 456 const MCAsmLayout &Layout) const {
423 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, 0); 457 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr);
424 } 458 }
425 459
426 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, 460 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
427 const MCAsmLayout &Layout, 461 const MCAsmLayout &Layout,
428 const SectionAddrMap &Addrs) const { 462 const SectionAddrMap &Addrs) const {
429 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs); 463 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
430 } 464 }
431 465
432 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const { 466 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
433 return EvaluateAsAbsolute(Res, &Asm, 0, 0); 467 return EvaluateAsAbsolute(Res, &Asm, nullptr, nullptr);
468 }
469
470 int64_t MCExpr::evaluateKnownAbsolute(const MCAsmLayout &Layout) const {
471 int64_t Res;
472 bool Abs =
473 evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr, true);
474 (void)Abs;
475 assert(Abs && "Not actually absolute");
476 return Res;
434 } 477 }
435 478
436 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm, 479 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
437 const MCAsmLayout *Layout, 480 const MCAsmLayout *Layout,
438 const SectionAddrMap *Addrs) const { 481 const SectionAddrMap *Addrs) const {
482 // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
483 // absolutize differences across sections and that is what the MachO writer
484 // uses Addrs for.
485 return evaluateAsAbsolute(Res, Asm, Layout, Addrs, Addrs);
486 }
487
488 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
489 const MCAsmLayout *Layout,
490 const SectionAddrMap *Addrs, bool InSet) const {
439 MCValue Value; 491 MCValue Value;
440 492
441 // Fast path constants. 493 // Fast path constants.
442 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) { 494 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
443 Res = CE->getValue(); 495 Res = CE->getValue();
444 return true; 496 return true;
445 } 497 }
446 498
447 // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us 499 bool IsRelocatable = EvaluateAsRelocatableImpl(
448 // absolutize differences across sections and that is what the MachO writer 500 Value, Asm, Layout, nullptr, Addrs, InSet, /*ForceVarExpansion*/ false);
449 // uses Addrs for.
450 bool IsRelocatable =
451 EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, /*InSet*/ Addrs);
452 501
453 // Record the current value. 502 // Record the current value.
454 Res = Value.getConstant(); 503 Res = Value.getConstant();
455 504
456 return IsRelocatable && Value.isAbsolute(); 505 return IsRelocatable && Value.isAbsolute();
474 return; 523 return;
475 524
476 if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet)) 525 if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
477 return; 526 return;
478 527
479 MCSymbolData &AD = Asm->getSymbolData(SA); 528 const MCSymbolData &AD = Asm->getSymbolData(SA);
480 MCSymbolData &BD = Asm->getSymbolData(SB); 529 const MCSymbolData &BD = Asm->getSymbolData(SB);
481 530
482 if (AD.getFragment() == BD.getFragment()) { 531 if (AD.getFragment() == BD.getFragment()) {
483 Addend += (AD.getOffset() - BD.getOffset()); 532 Addend += (AD.getOffset() - BD.getOffset());
484 533
485 // Pointers to Thumb symbols need to have their low-bit set to allow 534 // Pointers to Thumb symbols need to have their low-bit set to allow
487 if (Asm->isThumbFunc(&SA)) 536 if (Asm->isThumbFunc(&SA))
488 Addend |= 1; 537 Addend |= 1;
489 538
490 // Clear the symbol expr pointers to indicate we have folded these 539 // Clear the symbol expr pointers to indicate we have folded these
491 // operands. 540 // operands.
492 A = B = 0; 541 A = B = nullptr;
493 return; 542 return;
494 } 543 }
495 544
496 if (!Layout) 545 if (!Layout)
497 return; 546 return;
513 if (Asm->isThumbFunc(&SA)) 562 if (Asm->isThumbFunc(&SA))
514 Addend |= 1; 563 Addend |= 1;
515 564
516 // Clear the symbol expr pointers to indicate we have folded these 565 // Clear the symbol expr pointers to indicate we have folded these
517 // operands. 566 // operands.
518 A = B = 0; 567 A = B = nullptr;
519 } 568 }
520 569
521 /// \brief Evaluate the result of an add between (conceptually) two MCValues. 570 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
522 /// 571 ///
523 /// This routine conceptually attempts to construct an MCValue: 572 /// This routine conceptually attempts to construct an MCValue:
595 Res = MCValue::get(A, B, Result_Cst); 644 Res = MCValue::get(A, B, Result_Cst);
596 return true; 645 return true;
597 } 646 }
598 647
599 bool MCExpr::EvaluateAsRelocatable(MCValue &Res, 648 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
600 const MCAsmLayout &Layout) const { 649 const MCAsmLayout *Layout,
601 return EvaluateAsRelocatableImpl(Res, &Layout.getAssembler(), &Layout, 650 const MCFixup *Fixup) const {
602 0, false); 651 MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
603 } 652 return EvaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
604 653 false, /*ForceVarExpansion*/ false);
605 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, 654 }
606 const MCAssembler *Asm, 655
656 bool MCExpr::EvaluateAsValue(MCValue &Res, const MCAsmLayout *Layout,
657 const MCFixup *Fixup) const {
658 MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
659 return EvaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
660 false, /*ForceVarExpansion*/ true);
661 }
662
663 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
607 const MCAsmLayout *Layout, 664 const MCAsmLayout *Layout,
608 const SectionAddrMap *Addrs, 665 const MCFixup *Fixup,
609 bool InSet) const { 666 const SectionAddrMap *Addrs, bool InSet,
667 bool ForceVarExpansion) const {
610 ++stats::MCExprEvaluate; 668 ++stats::MCExprEvaluate;
611 669
612 switch (getKind()) { 670 switch (getKind()) {
613 case Target: 671 case Target:
614 return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout); 672 return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout,
673 Fixup);
615 674
616 case Constant: 675 case Constant:
617 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue()); 676 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
618 return true; 677 return true;
619 678
620 case SymbolRef: { 679 case SymbolRef: {
621 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this); 680 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
622 const MCSymbol &Sym = SRE->getSymbol(); 681 const MCSymbol &Sym = SRE->getSymbol();
682 const MCAsmInfo &MCAsmInfo = SRE->getMCAsmInfo();
623 683
624 // Evaluate recursively if this is a variable. 684 // Evaluate recursively if this is a variable.
625 if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) { 685 if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) {
626 bool Ret = Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Asm, 686 if (Sym.getVariableValue()->EvaluateAsRelocatableImpl(
627 Layout, 687 Res, Asm, Layout, Fixup, Addrs, true, ForceVarExpansion)) {
628 Addrs, 688 const MCSymbolRefExpr *A = Res.getSymA();
629 true); 689 const MCSymbolRefExpr *B = Res.getSymB();
630 // If we failed to simplify this to a constant, let the target 690
631 // handle it. 691 if (MCAsmInfo.hasSubsectionsViaSymbols()) {
632 if (Ret && !Res.getSymA() && !Res.getSymB()) 692 // FIXME: This is small hack. Given
633 return true; 693 // a = b + 4
694 // .long a
695 // the OS X assembler will completely drop the 4. We should probably
696 // include it in the relocation or produce an error if that is not
697 // possible.
698 if (!A && !B)
699 return true;
700 } else {
701 if (ForceVarExpansion)
702 return true;
703 bool IsSymbol = A && A->getSymbol().isDefined();
704 if (!IsSymbol)
705 return true;
706 }
707 }
634 } 708 }
635 709
636 Res = MCValue::get(SRE, 0, 0); 710 Res = MCValue::get(SRE, nullptr, 0);
637 return true; 711 return true;
638 } 712 }
639 713
640 case Unary: { 714 case Unary: {
641 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this); 715 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
642 MCValue Value; 716 MCValue Value;
643 717
644 if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout, 718 if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout,
645 Addrs, InSet)) 719 Fixup, Addrs, InSet,
720 ForceVarExpansion))
646 return false; 721 return false;
647 722
648 switch (AUE->getOpcode()) { 723 switch (AUE->getOpcode()) {
649 case MCUnaryExpr::LNot: 724 case MCUnaryExpr::LNot:
650 if (!Value.isAbsolute()) 725 if (!Value.isAbsolute())
674 case Binary: { 749 case Binary: {
675 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this); 750 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
676 MCValue LHSValue, RHSValue; 751 MCValue LHSValue, RHSValue;
677 752
678 if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout, 753 if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout,
679 Addrs, InSet) || 754 Fixup, Addrs, InSet,
755 ForceVarExpansion) ||
680 !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout, 756 !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout,
681 Addrs, InSet)) 757 Fixup, Addrs, InSet,
758 ForceVarExpansion))
682 return false; 759 return false;
683 760
684 // We only support a few operations on non-constant expressions, handle 761 // We only support a few operations on non-constant expressions, handle
685 // those first. 762 // those first.
686 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) { 763 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
750 const MCSymbol &Sym = SRE->getSymbol(); 827 const MCSymbol &Sym = SRE->getSymbol();
751 828
752 if (Sym.isDefined()) 829 if (Sym.isDefined())
753 return &Sym.getSection(); 830 return &Sym.getSection();
754 831
755 return 0; 832 return nullptr;
756 } 833 }
757 834
758 case Unary: 835 case Unary:
759 return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection(); 836 return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
760 837