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