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