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