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 | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 11 | #include "PPCFixupKinds.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmInfo.h" |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAssembler.h" |
| 14 | #include "llvm/MC/MCContext.h" |
Rafael Espindola | 2be1281 | 2014-06-25 15:29:54 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCObjectStreamer.h" |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace llvm; |
| 18 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "ppcmcexpr" |
| 20 | |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 21 | const PPCMCExpr* |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 22 | PPCMCExpr::create(VariantKind Kind, const MCExpr *Expr, |
Ulrich Weigand | 266db7f | 2013-07-08 20:20:51 +0000 | [diff] [blame] | 23 | bool isDarwin, MCContext &Ctx) { |
| 24 | return new (Ctx) PPCMCExpr(Kind, Expr, isDarwin); |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 27 | void PPCMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const { |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 28 | if (isDarwinSyntax()) { |
| 29 | switch (Kind) { |
| 30 | default: llvm_unreachable("Invalid kind!"); |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 31 | case VK_PPC_LO: OS << "lo16"; break; |
Ulrich Weigand | e67c565 | 2013-06-21 14:42:49 +0000 | [diff] [blame] | 32 | case VK_PPC_HI: OS << "hi16"; break; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 33 | case VK_PPC_HA: OS << "ha16"; break; |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 34 | } |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 35 | |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 36 | OS << '('; |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 37 | getSubExpr()->print(OS, MAI); |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 38 | OS << ')'; |
| 39 | } else { |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 40 | getSubExpr()->print(OS, MAI); |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 41 | |
| 42 | switch (Kind) { |
| 43 | default: llvm_unreachable("Invalid kind!"); |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 44 | case VK_PPC_LO: OS << "@l"; break; |
Ulrich Weigand | e67c565 | 2013-06-21 14:42:49 +0000 | [diff] [blame] | 45 | case VK_PPC_HI: OS << "@h"; break; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 46 | case VK_PPC_HA: OS << "@ha"; break; |
Sean Fertile | 80b8f82 | 2018-06-15 19:47:11 +0000 | [diff] [blame] | 47 | case VK_PPC_HIGH: OS << "@high"; break; |
| 48 | case VK_PPC_HIGHA: OS << "@higha"; break; |
Ulrich Weigand | e9126f5 | 2013-06-21 14:43:42 +0000 | [diff] [blame] | 49 | case VK_PPC_HIGHER: OS << "@higher"; break; |
| 50 | case VK_PPC_HIGHERA: OS << "@highera"; break; |
| 51 | case VK_PPC_HIGHEST: OS << "@highest"; break; |
| 52 | case VK_PPC_HIGHESTA: OS << "@highesta"; break; |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 58 | PPCMCExpr::evaluateAsConstant(int64_t &Res) const { |
Joerg Sonnenberger | bfef1dd | 2014-08-10 12:41:50 +0000 | [diff] [blame] | 59 | MCValue Value; |
| 60 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 61 | if (!getSubExpr()->evaluateAsRelocatable(Value, nullptr, nullptr)) |
Joerg Sonnenberger | bfef1dd | 2014-08-10 12:41:50 +0000 | [diff] [blame] | 62 | return false; |
| 63 | |
| 64 | if (!Value.isAbsolute()) |
| 65 | return false; |
| 66 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 67 | Res = evaluateAsInt64(Value.getConstant()); |
Joerg Sonnenberger | bfef1dd | 2014-08-10 12:41:50 +0000 | [diff] [blame] | 68 | return true; |
| 69 | } |
| 70 | |
| 71 | int64_t |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 72 | PPCMCExpr::evaluateAsInt64(int64_t Value) const { |
Joerg Sonnenberger | bfef1dd | 2014-08-10 12:41:50 +0000 | [diff] [blame] | 73 | switch (Kind) { |
| 74 | case VK_PPC_LO: |
| 75 | return Value & 0xffff; |
| 76 | case VK_PPC_HI: |
| 77 | return (Value >> 16) & 0xffff; |
| 78 | case VK_PPC_HA: |
| 79 | return ((Value + 0x8000) >> 16) & 0xffff; |
Sean Fertile | 80b8f82 | 2018-06-15 19:47:11 +0000 | [diff] [blame] | 80 | case VK_PPC_HIGH: |
| 81 | return (Value >> 16) & 0xffff; |
| 82 | case VK_PPC_HIGHA: |
| 83 | return ((Value + 0x8000) >> 16) & 0xffff; |
Joerg Sonnenberger | bfef1dd | 2014-08-10 12:41:50 +0000 | [diff] [blame] | 84 | case VK_PPC_HIGHER: |
| 85 | return (Value >> 32) & 0xffff; |
| 86 | case VK_PPC_HIGHERA: |
| 87 | return ((Value + 0x8000) >> 32) & 0xffff; |
| 88 | case VK_PPC_HIGHEST: |
| 89 | return (Value >> 48) & 0xffff; |
| 90 | case VK_PPC_HIGHESTA: |
| 91 | return ((Value + 0x8000) >> 48) & 0xffff; |
| 92 | case VK_PPC_None: |
| 93 | break; |
| 94 | } |
| 95 | llvm_unreachable("Invalid kind!"); |
| 96 | } |
| 97 | |
| 98 | bool |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 99 | PPCMCExpr::evaluateAsRelocatableImpl(MCValue &Res, |
Joerg Sonnenberger | 752b91b | 2014-08-10 11:35:12 +0000 | [diff] [blame] | 100 | const MCAsmLayout *Layout, |
| 101 | const MCFixup *Fixup) const { |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 102 | MCValue Value; |
| 103 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 104 | if (!getSubExpr()->evaluateAsRelocatable(Value, Layout, Fixup)) |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 105 | return false; |
| 106 | |
| 107 | if (Value.isAbsolute()) { |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 108 | int64_t Result = evaluateAsInt64(Value.getConstant()); |
Joerg Sonnenberger | bfef1dd | 2014-08-10 12:41:50 +0000 | [diff] [blame] | 109 | if ((Fixup == nullptr || (unsigned)Fixup->getKind() != PPC::fixup_ppc_half16) && |
| 110 | (Result >= 0x8000)) |
| 111 | return false; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 112 | Res = MCValue::get(Result); |
Alexey Samsonov | a3a037d | 2013-08-28 08:30:47 +0000 | [diff] [blame] | 113 | } else { |
Rafael Espindola | 3d5d464 | 2014-03-12 16:55:59 +0000 | [diff] [blame] | 114 | if (!Layout) |
| 115 | return false; |
| 116 | |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 117 | MCContext &Context = Layout->getAssembler().getContext(); |
| 118 | const MCSymbolRefExpr *Sym = Value.getSymA(); |
| 119 | MCSymbolRefExpr::VariantKind Modifier = Sym->getKind(); |
| 120 | if (Modifier != MCSymbolRefExpr::VK_None) |
| 121 | return false; |
| 122 | switch (Kind) { |
| 123 | default: |
| 124 | llvm_unreachable("Invalid kind!"); |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 125 | case VK_PPC_LO: |
| 126 | Modifier = MCSymbolRefExpr::VK_PPC_LO; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 127 | break; |
Ulrich Weigand | e67c565 | 2013-06-21 14:42:49 +0000 | [diff] [blame] | 128 | case VK_PPC_HI: |
| 129 | Modifier = MCSymbolRefExpr::VK_PPC_HI; |
| 130 | break; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 131 | case VK_PPC_HA: |
| 132 | Modifier = MCSymbolRefExpr::VK_PPC_HA; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 133 | break; |
Sean Fertile | 80b8f82 | 2018-06-15 19:47:11 +0000 | [diff] [blame] | 134 | case VK_PPC_HIGH: |
| 135 | Modifier = MCSymbolRefExpr::VK_PPC_HIGH; |
| 136 | break; |
| 137 | case VK_PPC_HIGHA: |
| 138 | Modifier = MCSymbolRefExpr::VK_PPC_HIGHA; |
| 139 | break; |
Ulrich Weigand | e9126f5 | 2013-06-21 14:43:42 +0000 | [diff] [blame] | 140 | case VK_PPC_HIGHERA: |
| 141 | Modifier = MCSymbolRefExpr::VK_PPC_HIGHERA; |
| 142 | break; |
| 143 | case VK_PPC_HIGHER: |
| 144 | Modifier = MCSymbolRefExpr::VK_PPC_HIGHER; |
| 145 | break; |
| 146 | case VK_PPC_HIGHEST: |
| 147 | Modifier = MCSymbolRefExpr::VK_PPC_HIGHEST; |
| 148 | break; |
| 149 | case VK_PPC_HIGHESTA: |
| 150 | Modifier = MCSymbolRefExpr::VK_PPC_HIGHESTA; |
| 151 | break; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 152 | } |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 153 | Sym = MCSymbolRefExpr::create(&Sym->getSymbol(), Modifier, Context); |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 154 | Res = MCValue::get(Sym, Value.getSymB(), Value.getConstant()); |
Alexey Samsonov | a3a037d | 2013-08-28 08:30:47 +0000 | [diff] [blame] | 155 | } |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 156 | |
| 157 | return true; |
| 158 | } |
| 159 | |
Rafael Espindola | e2c6624 | 2014-06-25 15:45:33 +0000 | [diff] [blame] | 160 | void PPCMCExpr::visitUsedExpr(MCStreamer &Streamer) const { |
Rafael Espindola | 2be1281 | 2014-06-25 15:29:54 +0000 | [diff] [blame] | 161 | Streamer.visitUsedExpr(*getSubExpr()); |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 162 | } |