LLVM  16.0.0git
PPCMCExpr.cpp
Go to the documentation of this file.
1 //===-- PPCMCExpr.cpp - PPC specific MC expression classes ----------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "PPCMCExpr.h"
10 #include "PPCFixupKinds.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCContext.h"
15 
16 using namespace llvm;
17 
18 #define DEBUG_TYPE "ppcmcexpr"
19 
20 const PPCMCExpr *PPCMCExpr::create(VariantKind Kind, const MCExpr *Expr,
21  MCContext &Ctx) {
22  return new (Ctx) PPCMCExpr(Kind, Expr);
23 }
24 
25 void PPCMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
26  getSubExpr()->print(OS, MAI);
27 
28  switch (Kind) {
29  default:
30  llvm_unreachable("Invalid kind!");
31  case VK_PPC_LO:
32  OS << "@l";
33  break;
34  case VK_PPC_HI:
35  OS << "@h";
36  break;
37  case VK_PPC_HA:
38  OS << "@ha";
39  break;
40  case VK_PPC_HIGH:
41  OS << "@high";
42  break;
43  case VK_PPC_HIGHA:
44  OS << "@higha";
45  break;
46  case VK_PPC_HIGHER:
47  OS << "@higher";
48  break;
49  case VK_PPC_HIGHERA:
50  OS << "@highera";
51  break;
52  case VK_PPC_HIGHEST:
53  OS << "@highest";
54  break;
55  case VK_PPC_HIGHESTA:
56  OS << "@highesta";
57  break;
58  }
59 }
60 
61 bool
62 PPCMCExpr::evaluateAsConstant(int64_t &Res) const {
63  MCValue Value;
64 
65  if (!getSubExpr()->evaluateAsRelocatable(Value, nullptr, nullptr))
66  return false;
67 
68  if (!Value.isAbsolute())
69  return false;
70 
71  Res = evaluateAsInt64(Value.getConstant());
72  return true;
73 }
74 
75 int64_t
76 PPCMCExpr::evaluateAsInt64(int64_t Value) const {
77  switch (Kind) {
78  case VK_PPC_LO:
79  return Value & 0xffff;
80  case VK_PPC_HI:
81  return (Value >> 16) & 0xffff;
82  case VK_PPC_HA:
83  return ((Value + 0x8000) >> 16) & 0xffff;
84  case VK_PPC_HIGH:
85  return (Value >> 16) & 0xffff;
86  case VK_PPC_HIGHA:
87  return ((Value + 0x8000) >> 16) & 0xffff;
88  case VK_PPC_HIGHER:
89  return (Value >> 32) & 0xffff;
90  case VK_PPC_HIGHERA:
91  return ((Value + 0x8000) >> 32) & 0xffff;
92  case VK_PPC_HIGHEST:
93  return (Value >> 48) & 0xffff;
94  case VK_PPC_HIGHESTA:
95  return ((Value + 0x8000) >> 48) & 0xffff;
96  case VK_PPC_None:
97  break;
98  }
99  llvm_unreachable("Invalid kind!");
100 }
101 
102 bool
104  const MCAsmLayout *Layout,
105  const MCFixup *Fixup) const {
106  MCValue Value;
107 
108  if (!getSubExpr()->evaluateAsRelocatable(Value, Layout, Fixup))
109  return false;
110 
111  if (Value.isAbsolute()) {
112  int64_t Result = evaluateAsInt64(Value.getConstant());
113  bool IsHalf16 = Fixup && Fixup->getTargetKind() == PPC::fixup_ppc_half16;
114  bool IsHalf16DS =
115  Fixup && Fixup->getTargetKind() == PPC::fixup_ppc_half16ds;
116  bool IsHalf16DQ =
117  Fixup && Fixup->getTargetKind() == PPC::fixup_ppc_half16dq;
118  bool IsHalf = IsHalf16 || IsHalf16DS || IsHalf16DQ;
119 
120  if (!IsHalf && Result >= 0x8000)
121  return false;
122  if ((IsHalf16DS && (Result & 0x3)) || (IsHalf16DQ && (Result & 0xf)))
123  return false;
124 
125  Res = MCValue::get(Result);
126  } else {
127  if (!Layout)
128  return false;
129 
130  MCContext &Context = Layout->getAssembler().getContext();
131  const MCSymbolRefExpr *Sym = Value.getSymA();
132  MCSymbolRefExpr::VariantKind Modifier = Sym->getKind();
133  if (Modifier != MCSymbolRefExpr::VK_None)
134  return false;
135  switch (Kind) {
136  default:
137  llvm_unreachable("Invalid kind!");
138  case VK_PPC_LO:
139  Modifier = MCSymbolRefExpr::VK_PPC_LO;
140  break;
141  case VK_PPC_HI:
142  Modifier = MCSymbolRefExpr::VK_PPC_HI;
143  break;
144  case VK_PPC_HA:
145  Modifier = MCSymbolRefExpr::VK_PPC_HA;
146  break;
147  case VK_PPC_HIGH:
148  Modifier = MCSymbolRefExpr::VK_PPC_HIGH;
149  break;
150  case VK_PPC_HIGHA:
152  break;
153  case VK_PPC_HIGHERA:
155  break;
156  case VK_PPC_HIGHER:
158  break;
159  case VK_PPC_HIGHEST:
161  break;
162  case VK_PPC_HIGHESTA:
164  break;
165  }
166  Sym = MCSymbolRefExpr::create(&Sym->getSymbol(), Modifier, Context);
167  Res = MCValue::get(Sym, Value.getSymB(), Value.getConstant());
168  }
169 
170  return true;
171 }
172 
173 void PPCMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
174  Streamer.visitUsedExpr(*getSubExpr());
175 }
llvm::MCSymbolRefExpr::getKind
VariantKind getKind() const
Definition: MCExpr.h:401
llvm::PPCMCExpr::VK_PPC_HA
@ VK_PPC_HA
Definition: PPCMCExpr.h:24
llvm::MCStreamer::visitUsedExpr
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:1068
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::MCSymbolRefExpr::VK_PPC_HIGHESTA
@ VK_PPC_HIGHESTA
Definition: MCExpr.h:256
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:76
llvm::PPCMCExpr::evaluateAsRelocatableImpl
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
Definition: PPCMCExpr.cpp:103
llvm::MCSymbolRefExpr::VK_PPC_HIGHA
@ VK_PPC_HIGHA
Definition: MCExpr.h:252
llvm::MCAsmInfo
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
llvm::PPCMCExpr::getSubExpr
const MCExpr * getSubExpr() const
getSubExpr - Get the child of this expression.
Definition: PPCMCExpr.h:69
MCAssembler.h
llvm::PPCMCExpr::VK_PPC_LO
@ VK_PPC_LO
Definition: PPCMCExpr.h:22
llvm::PPCMCExpr::VK_PPC_HIGHA
@ VK_PPC_HIGHA
Definition: PPCMCExpr.h:26
llvm::MCExpr::evaluateAsRelocatable
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:749
llvm::PPCMCExpr
Definition: PPCMCExpr.h:18
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
MCObjectStreamer.h
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:212
x3
In x86 we generate this spiffy xmm0 xmm0 ret in x86 we generate this which could be xmm1 movss xmm1 xmm0 ret In sse4 we could use insertps to make both better Here s another testcase that could use x3
Definition: README-SSE.txt:547
llvm::MCAssembler::getContext
MCContext & getContext() const
Definition: MCAssembler.h:321
MCContext.h
llvm::PPCMCExpr::VK_PPC_HIGHEST
@ VK_PPC_HIGHEST
Definition: PPCMCExpr.h:29
llvm::PPC::fixup_ppc_half16dq
@ fixup_ppc_half16dq
A 16-bit fixup corresponding to lo16(_foo) with implied 3 zero bits for instrs like 'lxv'.
Definition: PPCFixupKinds.h:56
llvm::PPCMCExpr::VK_PPC_HI
@ VK_PPC_HI
Definition: PPCMCExpr.h:23
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::MCSymbolRefExpr::getSymbol
const MCSymbol & getSymbol() const
Definition: MCExpr.h:399
llvm::PPCMCExpr::VK_PPC_HIGHERA
@ VK_PPC_HIGHERA
Definition: PPCMCExpr.h:28
llvm::MCSymbolRefExpr::VK_PPC_HA
@ VK_PPC_HA
Definition: MCExpr.h:250
llvm::PPC::fixup_ppc_half16
@ fixup_ppc_half16
A 16-bit fixup corresponding to lo16(_foo) or ha16(_foo) for instrs like 'li' or 'addis'.
Definition: PPCFixupKinds.h:37
llvm::MCSymbolRefExpr::VK_PPC_HI
@ VK_PPC_HI
Definition: MCExpr.h:249
llvm::PPCMCExpr::VK_PPC_None
@ VK_PPC_None
Definition: PPCMCExpr.h:21
llvm::MCSymbolRefExpr::VariantKind
VariantKind
Definition: MCExpr.h:194
PPCMCExpr.h
llvm::MCValue::get
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:59
PPCFixupKinds.h
llvm::MCSymbolRefExpr::VK_PPC_HIGHEST
@ VK_PPC_HIGHEST
Definition: MCExpr.h:255
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
llvm::PPC::fixup_ppc_half16ds
@ fixup_ppc_half16ds
A 14-bit fixup corresponding to lo16(_foo) with implied 2 zero bits for instrs like 'std'.
Definition: PPCFixupKinds.h:41
MCAsmInfo.h
Fixup
PowerPC TLS Dynamic Call Fixup
Definition: PPCTLSDynamicCall.cpp:215
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::PPCMCExpr::create
static const PPCMCExpr * create(VariantKind Kind, const MCExpr *Expr, MCContext &Ctx)
Definition: PPCMCExpr.cpp:20
llvm::MCSymbolRefExpr::VK_PPC_HIGH
@ VK_PPC_HIGH
Definition: MCExpr.h:251
llvm::PPCMCExpr::VK_PPC_HIGHESTA
@ VK_PPC_HIGHESTA
Definition: PPCMCExpr.h:30
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:386
llvm::MCSymbolRefExpr::VK_PPC_HIGHER
@ VK_PPC_HIGHER
Definition: MCExpr.h:253
llvm::PPCMCExpr::evaluateAsConstant
bool evaluateAsConstant(int64_t &Res) const
Definition: PPCMCExpr.cpp:62
llvm::MCExpr::print
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:41
llvm::MCSymbolRefExpr::VK_PPC_LO
@ VK_PPC_LO
Definition: MCExpr.h:248
llvm::PPCMCExpr::VariantKind
VariantKind
Definition: PPCMCExpr.h:20
llvm::PPCMCExpr::VK_PPC_HIGHER
@ VK_PPC_HIGHER
Definition: PPCMCExpr.h:27
llvm::PPCMCExpr::visitUsedExpr
void visitUsedExpr(MCStreamer &Streamer) const override
Definition: PPCMCExpr.cpp:173
llvm::MCAsmLayout::getAssembler
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
Definition: MCAsmLayout.h:50
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:36
llvm::PPCMCExpr::VK_PPC_HIGH
@ VK_PPC_HIGH
Definition: PPCMCExpr.h:25
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition: MCExpr.h:195
llvm::PPCMCExpr::printImpl
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
Definition: PPCMCExpr.cpp:25
llvm::MCSymbolRefExpr::VK_PPC_HIGHERA
@ VK_PPC_HIGHERA
Definition: MCExpr.h:254
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35