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 | |
| 10 | #define DEBUG_TYPE "ppcmcexpr" |
| 11 | #include "PPCMCExpr.h" |
| 12 | #include "llvm/MC/MCAssembler.h" |
| 13 | #include "llvm/MC/MCContext.h" |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAsmInfo.h" |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
| 17 | |
| 18 | const PPCMCExpr* |
| 19 | PPCMCExpr::Create(VariantKind Kind, const MCExpr *Expr, |
| 20 | MCContext &Ctx) { |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 21 | int AssemblerDialect = Ctx.getAsmInfo()->getAssemblerDialect(); |
| 22 | return new (Ctx) PPCMCExpr(Kind, Expr, AssemblerDialect); |
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; |
| 30 | case VK_PPC_HA: OS << "ha16"; break; |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 31 | } |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 32 | |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 33 | OS << '('; |
| 34 | getSubExpr()->print(OS); |
| 35 | OS << ')'; |
| 36 | } else { |
| 37 | getSubExpr()->print(OS); |
| 38 | |
| 39 | switch (Kind) { |
| 40 | default: llvm_unreachable("Invalid kind!"); |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame^] | 41 | case VK_PPC_LO: OS << "@l"; break; |
| 42 | case VK_PPC_HA: OS << "@ha"; break; |
Ulrich Weigand | 96e6578 | 2013-06-20 16:23:52 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | bool |
| 48 | PPCMCExpr::EvaluateAsRelocatableImpl(MCValue &Res, |
| 49 | const MCAsmLayout *Layout) const { |
| 50 | MCValue Value; |
| 51 | |
| 52 | if (!getSubExpr()->EvaluateAsRelocatable(Value, *Layout)) |
| 53 | return false; |
| 54 | |
| 55 | if (Value.isAbsolute()) { |
| 56 | int64_t Result = Value.getConstant(); |
| 57 | switch (Kind) { |
| 58 | default: |
| 59 | llvm_unreachable("Invalid kind!"); |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame^] | 60 | case VK_PPC_LO: |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 61 | Result = Result & 0xffff; |
| 62 | break; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame^] | 63 | case VK_PPC_HA: |
| 64 | Result = ((Result >> 16) + ((Result & 0x8000) ? 1 : 0)) & 0xffff; |
| 65 | break; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 66 | } |
| 67 | Res = MCValue::get(Result); |
| 68 | } else { |
| 69 | MCContext &Context = Layout->getAssembler().getContext(); |
| 70 | const MCSymbolRefExpr *Sym = Value.getSymA(); |
| 71 | MCSymbolRefExpr::VariantKind Modifier = Sym->getKind(); |
| 72 | if (Modifier != MCSymbolRefExpr::VK_None) |
| 73 | return false; |
| 74 | switch (Kind) { |
| 75 | default: |
| 76 | llvm_unreachable("Invalid kind!"); |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame^] | 77 | case VK_PPC_LO: |
| 78 | Modifier = MCSymbolRefExpr::VK_PPC_LO; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 79 | break; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame^] | 80 | case VK_PPC_HA: |
| 81 | Modifier = MCSymbolRefExpr::VK_PPC_HA; |
Ulrich Weigand | 41789de | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 82 | break; |
| 83 | } |
| 84 | Sym = MCSymbolRefExpr::Create(&Sym->getSymbol(), Modifier, Context); |
| 85 | Res = MCValue::get(Sym, Value.getSymB(), Value.getConstant()); |
| 86 | } |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | // FIXME: This basically copies MCObjectStreamer::AddValueSymbols. Perhaps |
| 92 | // that method should be made public? |
| 93 | static void AddValueSymbols_(const MCExpr *Value, MCAssembler *Asm) { |
| 94 | switch (Value->getKind()) { |
| 95 | case MCExpr::Target: |
| 96 | llvm_unreachable("Can't handle nested target expr!"); |
| 97 | |
| 98 | case MCExpr::Constant: |
| 99 | break; |
| 100 | |
| 101 | case MCExpr::Binary: { |
| 102 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); |
| 103 | AddValueSymbols_(BE->getLHS(), Asm); |
| 104 | AddValueSymbols_(BE->getRHS(), Asm); |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | case MCExpr::SymbolRef: |
| 109 | Asm->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol()); |
| 110 | break; |
| 111 | |
| 112 | case MCExpr::Unary: |
| 113 | AddValueSymbols_(cast<MCUnaryExpr>(Value)->getSubExpr(), Asm); |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void PPCMCExpr::AddValueSymbols(MCAssembler *Asm) const { |
| 119 | AddValueSymbols_(getSubExpr(), Asm); |
| 120 | } |