LLVM  8.0.1
SparcMCExpr.cpp
Go to the documentation of this file.
1 //===-- SparcMCExpr.cpp - Sparc 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 // This file contains the implementation of the assembly expression modifiers
11 // accepted by the Sparc architecture (e.g. "%hi", "%lo", ...).
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "SparcMCExpr.h"
16 #include "llvm/BinaryFormat/ELF.h"
17 #include "llvm/MC/MCAssembler.h"
18 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCSymbolELF.h"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "sparcmcexpr"
25 
26 const SparcMCExpr*
28  MCContext &Ctx) {
29  return new (Ctx) SparcMCExpr(Kind, Expr);
30 }
31 
32 void SparcMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
33 
34  bool closeParen = printVariantKind(OS, Kind);
35 
36  const MCExpr *Expr = getSubExpr();
37  Expr->print(OS, MAI);
38 
39  if (closeParen)
40  OS << ')';
41 }
42 
44 {
45  bool closeParen = true;
46  switch (Kind) {
47  case VK_Sparc_None: closeParen = false; break;
48  case VK_Sparc_LO: OS << "%lo("; break;
49  case VK_Sparc_HI: OS << "%hi("; break;
50  case VK_Sparc_H44: OS << "%h44("; break;
51  case VK_Sparc_M44: OS << "%m44("; break;
52  case VK_Sparc_L44: OS << "%l44("; break;
53  case VK_Sparc_HH: OS << "%hh("; break;
54  case VK_Sparc_HM: OS << "%hm("; break;
55  // FIXME: use %pc22/%pc10, if system assembler supports them.
56  case VK_Sparc_PC22: OS << "%hi("; break;
57  case VK_Sparc_PC10: OS << "%lo("; break;
58  // FIXME: use %got22/%got10, if system assembler supports them.
59  case VK_Sparc_GOT22: OS << "%hi("; break;
60  case VK_Sparc_GOT10: OS << "%lo("; break;
61  case VK_Sparc_GOT13: closeParen = false; break;
62  case VK_Sparc_13: closeParen = false; break;
63  case VK_Sparc_WPLT30: closeParen = false; break;
64  case VK_Sparc_R_DISP32: OS << "%r_disp32("; break;
65  case VK_Sparc_TLS_GD_HI22: OS << "%tgd_hi22("; break;
66  case VK_Sparc_TLS_GD_LO10: OS << "%tgd_lo10("; break;
67  case VK_Sparc_TLS_GD_ADD: OS << "%tgd_add("; break;
68  case VK_Sparc_TLS_GD_CALL: OS << "%tgd_call("; break;
69  case VK_Sparc_TLS_LDM_HI22: OS << "%tldm_hi22("; break;
70  case VK_Sparc_TLS_LDM_LO10: OS << "%tldm_lo10("; break;
71  case VK_Sparc_TLS_LDM_ADD: OS << "%tldm_add("; break;
72  case VK_Sparc_TLS_LDM_CALL: OS << "%tldm_call("; break;
73  case VK_Sparc_TLS_LDO_HIX22: OS << "%tldo_hix22("; break;
74  case VK_Sparc_TLS_LDO_LOX10: OS << "%tldo_lox10("; break;
75  case VK_Sparc_TLS_LDO_ADD: OS << "%tldo_add("; break;
76  case VK_Sparc_TLS_IE_HI22: OS << "%tie_hi22("; break;
77  case VK_Sparc_TLS_IE_LO10: OS << "%tie_lo10("; break;
78  case VK_Sparc_TLS_IE_LD: OS << "%tie_ld("; break;
79  case VK_Sparc_TLS_IE_LDX: OS << "%tie_ldx("; break;
80  case VK_Sparc_TLS_IE_ADD: OS << "%tie_add("; break;
81  case VK_Sparc_TLS_LE_HIX22: OS << "%tle_hix22("; break;
82  case VK_Sparc_TLS_LE_LOX10: OS << "%tle_lox10("; break;
83  }
84  return closeParen;
85 }
86 
88 {
90  .Case("lo", VK_Sparc_LO)
91  .Case("hi", VK_Sparc_HI)
92  .Case("h44", VK_Sparc_H44)
93  .Case("m44", VK_Sparc_M44)
94  .Case("l44", VK_Sparc_L44)
95  .Case("hh", VK_Sparc_HH)
96  .Case("hm", VK_Sparc_HM)
97  .Case("pc22", VK_Sparc_PC22)
98  .Case("pc10", VK_Sparc_PC10)
99  .Case("got22", VK_Sparc_GOT22)
100  .Case("got10", VK_Sparc_GOT10)
101  .Case("got13", VK_Sparc_GOT13)
102  .Case("r_disp32", VK_Sparc_R_DISP32)
103  .Case("tgd_hi22", VK_Sparc_TLS_GD_HI22)
104  .Case("tgd_lo10", VK_Sparc_TLS_GD_LO10)
105  .Case("tgd_add", VK_Sparc_TLS_GD_ADD)
106  .Case("tgd_call", VK_Sparc_TLS_GD_CALL)
107  .Case("tldm_hi22", VK_Sparc_TLS_LDM_HI22)
108  .Case("tldm_lo10", VK_Sparc_TLS_LDM_LO10)
109  .Case("tldm_add", VK_Sparc_TLS_LDM_ADD)
110  .Case("tldm_call", VK_Sparc_TLS_LDM_CALL)
111  .Case("tldo_hix22", VK_Sparc_TLS_LDO_HIX22)
112  .Case("tldo_lox10", VK_Sparc_TLS_LDO_LOX10)
113  .Case("tldo_add", VK_Sparc_TLS_LDO_ADD)
114  .Case("tie_hi22", VK_Sparc_TLS_IE_HI22)
115  .Case("tie_lo10", VK_Sparc_TLS_IE_LO10)
116  .Case("tie_ld", VK_Sparc_TLS_IE_LD)
117  .Case("tie_ldx", VK_Sparc_TLS_IE_LDX)
118  .Case("tie_add", VK_Sparc_TLS_IE_ADD)
119  .Case("tle_hix22", VK_Sparc_TLS_LE_HIX22)
120  .Case("tle_lox10", VK_Sparc_TLS_LE_LOX10)
122 }
123 
125  switch (Kind) {
126  default: llvm_unreachable("Unhandled SparcMCExpr::VariantKind");
132  case VK_Sparc_HH: return Sparc::fixup_sparc_hh;
133  case VK_Sparc_HM: return Sparc::fixup_sparc_hm;
139  case VK_Sparc_13: return Sparc::fixup_sparc_13;
159  }
160 }
161 
162 bool
164  const MCAsmLayout *Layout,
165  const MCFixup *Fixup) const {
166  return getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup);
167 }
168 
170  switch (Expr->getKind()) {
171  case MCExpr::Target:
172  llvm_unreachable("Can't handle nested target expr!");
173  break;
174 
175  case MCExpr::Constant:
176  break;
177 
178  case MCExpr::Binary: {
179  const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
182  break;
183  }
184 
185  case MCExpr::SymbolRef: {
186  const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
187  cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
188  break;
189  }
190 
191  case MCExpr::Unary:
192  fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
193  break;
194  }
195 
196 }
197 
199  switch(getKind()) {
200  default: return;
202  case VK_Sparc_TLS_LDM_CALL: {
203  // The corresponding relocations reference __tls_get_addr, as they call it,
204  // but this is only implicit; we must explicitly add it to our symbol table
205  // to bind it for these uses.
206  MCSymbol *Symbol = Asm.getContext().getOrCreateSymbol("__tls_get_addr");
207  Asm.registerSymbol(*Symbol);
208  auto ELFSymbol = cast<MCSymbolELF>(Symbol);
209  if (!ELFSymbol->isBindingSet()) {
210  ELFSymbol->setBinding(ELF::STB_GLOBAL);
211  ELFSymbol->setExternal(true);
212  }
214  }
217  case VK_Sparc_TLS_GD_ADD:
226  case VK_Sparc_TLS_IE_LD:
227  case VK_Sparc_TLS_IE_LDX:
228  case VK_Sparc_TLS_IE_ADD:
230  case VK_Sparc_TLS_LE_LOX10: break;
231  }
233 }
234 
235 void SparcMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
236  Streamer.visitUsedExpr(*getSubExpr());
237 }
fixup_sparc_got22 - 22-bit fixup corresponding to got22(foo)
fixup_sparc_13 - 13-bit fixup
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
This represents an "assembler immediate".
Definition: MCValue.h:40
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
#define LLVM_FALLTHROUGH
Definition: Compiler.h:86
static bool printVariantKind(raw_ostream &OS, VariantKind Kind)
Definition: SparcMCExpr.cpp:43
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:564
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
fixup_sparc_m44 - 10-bit fixup corresponding to m44(foo)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:74
MCContext & getContext() const
Definition: MCAssembler.h:285
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
fixup_sparc_got10 - 10-bit fixup corresponding to got10(foo)
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:166
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(T Value)
Definition: StringSwitch.h:203
Context object for machine code objects.
Definition: MCContext.h:63
bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:647
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:567
fixup_sparc_hm - 10-bit fixup corresponding to hm(foo)
Sparc::Fixups getFixupKind() const
getFixupKind - Get the fixup kind of this expression.
Definition: SparcMCExpr.h:87
Unary expressions.
Definition: MCExpr.h:42
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
Streaming machine code generation interface.
Definition: MCStreamer.h:189
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:42
void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override
fixup_sparc_hh - 22-bit fixup corresponding to hh(foo)
static VariantKind parseVariantKind(StringRef name)
Definition: SparcMCExpr.cpp:87
fixup_sparc_l44 - 12-bit fixup corresponding to l44(foo)
const MCExpr * getSubExpr() const
getSubExpr - Get the child of this expression.
Definition: SparcMCExpr.h:84
Binary assembler expressions.
Definition: MCExpr.h:417
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
fixup_sparc_lo10 - 10-bit fixup corresponding to lo(foo)
fixup_sparc_got13 - 13-bit fixup corresponding to got13(foo)
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
Definition: SparcMCExpr.cpp:32
const MCSymbol & getSymbol() const
Definition: MCExpr.h:336
ExprKind getKind() const
Definition: MCExpr.h:73
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:930
fixup_sparc_pc22 - 22-bit fixup corresponding to pc22(foo)
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:70
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:123
References to labels and assigned expressions.
Definition: MCExpr.h:41
void visitUsedExpr(MCStreamer &Streamer) const override
static const SparcMCExpr * create(VariantKind Kind, const MCExpr *Expr, MCContext &Ctx)
Definition: SparcMCExpr.cpp:27
const unsigned Kind
VariantKind getKind() const
getOpcode - Get the kind of this expression.
Definition: SparcMCExpr.h:81
static const char * name
Constant expressions.
Definition: MCExpr.h:40
Binary expressions.
Definition: MCExpr.h:39
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Target specific expression.
Definition: MCExpr.h:43
fixups for Thread Local Storage
fixup_sparc_hi22 - 22-bit fixup corresponding to hi(foo) for sethi
fixup_sparc_h44 - 22-bit fixup corresponding to h44(foo)
fixup_sparc_pc10 - 10-bit fixup corresponding to pc10(foo)