Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 1 | //===-- PPCMCExpr.cpp - PPC specific MC expression classes ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 10 | #include "PPCMCExpr.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAsmInfo.h" |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAssembler.h" |
| 13 | #include "llvm/MC/MCContext.h" |
| 14 | |
| 15 | using namespace llvm; |
| 16 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 17 | #define DEBUG_TYPE "ppcmcexpr" |
| 18 | |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 19 | const PPCMCExpr* |
| 20 | PPCMCExpr::Create(VariantKind Kind, const MCExpr *Expr, |
Ulrich Weigand | 266db7f | 2013-07-08 20:20:51 +0000 | [diff] [blame] | 21 | bool isDarwin, MCContext &Ctx) { |
| 22 | return new (Ctx) PPCMCExpr(Kind, Expr, isDarwin); |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | void PPCMCExpr::PrintImpl(raw_ostream &OS) const { |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 26 | if (isDarwinSyntax()) { |
| 27 | switch (Kind) { |
| 28 | default: llvm_unreachable("Invalid kind!"); |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 29 | case VK_PPC_LO: OS << "lo16"; break; |
Ulrich Weigand | e67c565 | 2013-06-21 14:42:49 +0000 | [diff] [blame] | 30 | case VK_PPC_HI: OS << "hi16"; break; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 31 | case VK_PPC_HA: OS << "ha16"; break; |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 32 | } |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 33 | |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 34 | OS << '('; |
| 35 | getSubExpr()->print(OS); |
| 36 | OS << ')'; |
| 37 | } else { |
| 38 | getSubExpr()->print(OS); |
| 39 | |
| 40 | switch (Kind) { |
| 41 | default: llvm_unreachable("Invalid kind!"); |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 42 | case VK_PPC_LO: OS << "@l"; break; |
Ulrich Weigand | e67c565 | 2013-06-21 14:42:49 +0000 | [diff] [blame] | 43 | case VK_PPC_HI: OS << "@h"; break; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 44 | case VK_PPC_HA: OS << "@ha"; break; |
Ulrich Weigand | e9126f5 | 2013-06-21 14:43:42 +0000 | [diff] [blame] | 45 | case VK_PPC_HIGHER: OS << "@higher"; break; |
| 46 | case VK_PPC_HIGHERA: OS << "@highera"; break; |
| 47 | case VK_PPC_HIGHEST: OS << "@highest"; break; |
| 48 | case VK_PPC_HIGHESTA: OS << "@highesta"; break; |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool |
| 54 | PPCMCExpr::EvaluateAsRelocatableImpl(MCValue &Res, |
| 55 | const MCAsmLayout *Layout) const { |
| 56 | MCValue Value; |
| 57 | |
Rafael Espindola | 3d5d464 | 2014-03-12 16:55:59 +0000 | [diff] [blame] | 58 | if (!getSubExpr()->EvaluateAsRelocatable(Value, Layout)) |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 59 | return false; |
| 60 | |
| 61 | if (Value.isAbsolute()) { |
| 62 | int64_t Result = Value.getConstant(); |
| 63 | switch (Kind) { |
| 64 | default: |
| 65 | llvm_unreachable("Invalid kind!"); |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 66 | case VK_PPC_LO: |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 67 | Result = Result & 0xffff; |
| 68 | break; |
Ulrich Weigand | e67c565 | 2013-06-21 14:42:49 +0000 | [diff] [blame] | 69 | case VK_PPC_HI: |
| 70 | Result = (Result >> 16) & 0xffff; |
| 71 | break; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 72 | case VK_PPC_HA: |
Ulrich Weigand | e9126f5 | 2013-06-21 14:43:42 +0000 | [diff] [blame] | 73 | Result = ((Result + 0x8000) >> 16) & 0xffff; |
| 74 | break; |
| 75 | case VK_PPC_HIGHER: |
| 76 | Result = (Result >> 32) & 0xffff; |
| 77 | break; |
| 78 | case VK_PPC_HIGHERA: |
| 79 | Result = ((Result + 0x8000) >> 32) & 0xffff; |
| 80 | break; |
| 81 | case VK_PPC_HIGHEST: |
| 82 | Result = (Result >> 48) & 0xffff; |
| 83 | break; |
| 84 | case VK_PPC_HIGHESTA: |
| 85 | Result = ((Result + 0x8000) >> 48) & 0xffff; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 86 | break; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 87 | } |
| 88 | Res = MCValue::get(Result); |
Alexey Samsonov | a3a037d | 2013-08-28 08:30:47 +0000 | [diff] [blame] | 89 | } else { |
Rafael Espindola | 3d5d464 | 2014-03-12 16:55:59 +0000 | [diff] [blame] | 90 | if (!Layout) |
| 91 | return false; |
| 92 | |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 93 | MCContext &Context = Layout->getAssembler().getContext(); |
| 94 | const MCSymbolRefExpr *Sym = Value.getSymA(); |
| 95 | MCSymbolRefExpr::VariantKind Modifier = Sym->getKind(); |
| 96 | if (Modifier != MCSymbolRefExpr::VK_None) |
| 97 | return false; |
| 98 | switch (Kind) { |
| 99 | default: |
| 100 | llvm_unreachable("Invalid kind!"); |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 101 | case VK_PPC_LO: |
| 102 | Modifier = MCSymbolRefExpr::VK_PPC_LO; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 103 | break; |
Ulrich Weigand | e67c565 | 2013-06-21 14:42:49 +0000 | [diff] [blame] | 104 | case VK_PPC_HI: |
| 105 | Modifier = MCSymbolRefExpr::VK_PPC_HI; |
| 106 | break; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 107 | case VK_PPC_HA: |
| 108 | Modifier = MCSymbolRefExpr::VK_PPC_HA; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 109 | break; |
Ulrich Weigand | e9126f5 | 2013-06-21 14:43:42 +0000 | [diff] [blame] | 110 | case VK_PPC_HIGHERA: |
| 111 | Modifier = MCSymbolRefExpr::VK_PPC_HIGHERA; |
| 112 | break; |
| 113 | case VK_PPC_HIGHER: |
| 114 | Modifier = MCSymbolRefExpr::VK_PPC_HIGHER; |
| 115 | break; |
| 116 | case VK_PPC_HIGHEST: |
| 117 | Modifier = MCSymbolRefExpr::VK_PPC_HIGHEST; |
| 118 | break; |
| 119 | case VK_PPC_HIGHESTA: |
| 120 | Modifier = MCSymbolRefExpr::VK_PPC_HIGHESTA; |
| 121 | break; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 122 | } |
| 123 | Sym = MCSymbolRefExpr::Create(&Sym->getSymbol(), Modifier, Context); |
| 124 | Res = MCValue::get(Sym, Value.getSymB(), Value.getConstant()); |
Alexey Samsonov | a3a037d | 2013-08-28 08:30:47 +0000 | [diff] [blame] | 125 | } |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | // FIXME: This basically copies MCObjectStreamer::AddValueSymbols. Perhaps |
| 131 | // that method should be made public? |
| 132 | static void AddValueSymbols_(const MCExpr *Value, MCAssembler *Asm) { |
| 133 | switch (Value->getKind()) { |
| 134 | case MCExpr::Target: |
| 135 | llvm_unreachable("Can't handle nested target expr!"); |
| 136 | |
| 137 | case MCExpr::Constant: |
| 138 | break; |
| 139 | |
| 140 | case MCExpr::Binary: { |
| 141 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); |
| 142 | AddValueSymbols_(BE->getLHS(), Asm); |
| 143 | AddValueSymbols_(BE->getRHS(), Asm); |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | case MCExpr::SymbolRef: |
| 148 | Asm->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol()); |
| 149 | break; |
| 150 | |
| 151 | case MCExpr::Unary: |
| 152 | AddValueSymbols_(cast<MCUnaryExpr>(Value)->getSubExpr(), Asm); |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void PPCMCExpr::AddValueSymbols(MCAssembler *Asm) const { |
| 158 | AddValueSymbols_(getSubExpr(), Asm); |
| 159 | } |