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" |
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* |
| 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 | |
| 26 | void PPCMCExpr::PrintImpl(raw_ostream &OS) 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 << '('; |
| 36 | getSubExpr()->print(OS); |
| 37 | OS << ')'; |
| 38 | } else { |
| 39 | getSubExpr()->print(OS); |
| 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; |
Ulrich Weigand | e9126f5 | 2013-06-21 14:43:42 +0000 | [diff] [blame] | 46 | case VK_PPC_HIGHER: OS << "@higher"; break; |
| 47 | case VK_PPC_HIGHERA: OS << "@highera"; break; |
| 48 | case VK_PPC_HIGHEST: OS << "@highest"; break; |
| 49 | case VK_PPC_HIGHESTA: OS << "@highesta"; break; |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | bool |
| 55 | PPCMCExpr::EvaluateAsRelocatableImpl(MCValue &Res, |
| 56 | const MCAsmLayout *Layout) const { |
| 57 | MCValue Value; |
| 58 | |
Rafael Espindola | 3d5d464 | 2014-03-12 16:55:59 +0000 | [diff] [blame] | 59 | if (!getSubExpr()->EvaluateAsRelocatable(Value, Layout)) |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 60 | return false; |
| 61 | |
| 62 | if (Value.isAbsolute()) { |
| 63 | int64_t Result = Value.getConstant(); |
| 64 | switch (Kind) { |
| 65 | default: |
| 66 | llvm_unreachable("Invalid kind!"); |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 67 | case VK_PPC_LO: |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 68 | Result = Result & 0xffff; |
| 69 | break; |
Ulrich Weigand | e67c565 | 2013-06-21 14:42:49 +0000 | [diff] [blame] | 70 | case VK_PPC_HI: |
| 71 | Result = (Result >> 16) & 0xffff; |
| 72 | break; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 73 | case VK_PPC_HA: |
Ulrich Weigand | e9126f5 | 2013-06-21 14:43:42 +0000 | [diff] [blame] | 74 | Result = ((Result + 0x8000) >> 16) & 0xffff; |
| 75 | break; |
| 76 | case VK_PPC_HIGHER: |
| 77 | Result = (Result >> 32) & 0xffff; |
| 78 | break; |
| 79 | case VK_PPC_HIGHERA: |
| 80 | Result = ((Result + 0x8000) >> 32) & 0xffff; |
| 81 | break; |
| 82 | case VK_PPC_HIGHEST: |
| 83 | Result = (Result >> 48) & 0xffff; |
| 84 | break; |
| 85 | case VK_PPC_HIGHESTA: |
| 86 | Result = ((Result + 0x8000) >> 48) & 0xffff; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 87 | break; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 88 | } |
| 89 | Res = MCValue::get(Result); |
Alexey Samsonov | a3a037d | 2013-08-28 08:30:47 +0000 | [diff] [blame] | 90 | } else { |
Rafael Espindola | 3d5d464 | 2014-03-12 16:55:59 +0000 | [diff] [blame] | 91 | if (!Layout) |
| 92 | return false; |
| 93 | |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 94 | MCContext &Context = Layout->getAssembler().getContext(); |
| 95 | const MCSymbolRefExpr *Sym = Value.getSymA(); |
| 96 | MCSymbolRefExpr::VariantKind Modifier = Sym->getKind(); |
| 97 | if (Modifier != MCSymbolRefExpr::VK_None) |
| 98 | return false; |
| 99 | switch (Kind) { |
| 100 | default: |
| 101 | llvm_unreachable("Invalid kind!"); |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 102 | case VK_PPC_LO: |
| 103 | Modifier = MCSymbolRefExpr::VK_PPC_LO; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 104 | break; |
Ulrich Weigand | e67c565 | 2013-06-21 14:42:49 +0000 | [diff] [blame] | 105 | case VK_PPC_HI: |
| 106 | Modifier = MCSymbolRefExpr::VK_PPC_HI; |
| 107 | break; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 108 | case VK_PPC_HA: |
| 109 | Modifier = MCSymbolRefExpr::VK_PPC_HA; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 110 | break; |
Ulrich Weigand | e9126f5 | 2013-06-21 14:43:42 +0000 | [diff] [blame] | 111 | case VK_PPC_HIGHERA: |
| 112 | Modifier = MCSymbolRefExpr::VK_PPC_HIGHERA; |
| 113 | break; |
| 114 | case VK_PPC_HIGHER: |
| 115 | Modifier = MCSymbolRefExpr::VK_PPC_HIGHER; |
| 116 | break; |
| 117 | case VK_PPC_HIGHEST: |
| 118 | Modifier = MCSymbolRefExpr::VK_PPC_HIGHEST; |
| 119 | break; |
| 120 | case VK_PPC_HIGHESTA: |
| 121 | Modifier = MCSymbolRefExpr::VK_PPC_HIGHESTA; |
| 122 | break; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 123 | } |
| 124 | Sym = MCSymbolRefExpr::Create(&Sym->getSymbol(), Modifier, Context); |
| 125 | Res = MCValue::get(Sym, Value.getSymB(), Value.getConstant()); |
Alexey Samsonov | a3a037d | 2013-08-28 08:30:47 +0000 | [diff] [blame] | 126 | } |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
Rafael Espindola | 2be1281 | 2014-06-25 15:29:54 +0000 | [diff] [blame] | 131 | void PPCMCExpr::visitUsedExpr(MCObjectStreamer &Streamer) const { |
| 132 | Streamer.visitUsedExpr(*getSubExpr()); |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 133 | } |