1 //===-- llvm/MC/MCValue.h - MCValue class -----------------------*- C++ -*-===// 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 // This file contains the declaration of the MCValue class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_MC_MCVALUE_H 14 #define LLVM_MC_MCVALUE_H 15 16 #include "llvm/MC/MCExpr.h" 17 #include "llvm/Support/DataTypes.h" 18 #include <cassert> 19 20 namespace llvm { 21 class raw_ostream; 22 23 /// This represents an "assembler immediate". 24 /// 25 /// In its most general form, this can hold ":Kind:(SymbolA - SymbolB + 26 /// imm64)". Not all targets supports relocations of this general form, but we 27 /// need to represent this anyway. 28 /// 29 /// In general both SymbolA and SymbolB will also have a modifier 30 /// analogous to the top-level Kind. Current targets are not expected 31 /// to make use of both though. The choice comes down to whether 32 /// relocation modifiers apply to the closest symbol or the whole 33 /// expression. 34 /// 35 /// Note that this class must remain a simple POD value class, because we need 36 /// it to live in unions etc. 37 class MCValue { 38 const MCSymbolRefExpr *SymA = nullptr, *SymB = nullptr; 39 int64_t Cst = 0; 40 uint32_t RefKind = 0; 41 42 public: 43 MCValue() = default; getConstant()44 int64_t getConstant() const { return Cst; } getSymA()45 const MCSymbolRefExpr *getSymA() const { return SymA; } getSymB()46 const MCSymbolRefExpr *getSymB() const { return SymB; } getRefKind()47 uint32_t getRefKind() const { return RefKind; } 48 49 /// Is this an absolute (as opposed to relocatable) value. isAbsolute()50 bool isAbsolute() const { return !SymA && !SymB; } 51 52 /// Print the value to the stream \p OS. 53 void print(raw_ostream &OS) const; 54 55 /// Print the value to stderr. 56 void dump() const; 57 58 MCSymbolRefExpr::VariantKind getAccessVariant() const; 59 60 static MCValue get(const MCSymbolRefExpr *SymA, 61 const MCSymbolRefExpr *SymB = nullptr, 62 int64_t Val = 0, uint32_t RefKind = 0) { 63 MCValue R; 64 R.Cst = Val; 65 R.SymA = SymA; 66 R.SymB = SymB; 67 R.RefKind = RefKind; 68 return R; 69 } 70 get(int64_t Val)71 static MCValue get(int64_t Val) { 72 MCValue R; 73 R.Cst = Val; 74 R.SymA = nullptr; 75 R.SymB = nullptr; 76 R.RefKind = 0; 77 return R; 78 } 79 80 }; 81 82 } // end namespace llvm 83 84 #endif 85