blob: 37feee4c9ea8f1c30b5532da9508d8a119769c30 [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"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010017#include "llvm/Support/DataTypes.h"
18#include <cassert>
19
20namespace llvm {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010021class raw_ostream;
22
Andrew Scullcdfcccc2018-10-05 20:58:37 +010023/// This represents an "assembler immediate".
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010024///
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.
37class MCValue {
38 const MCSymbolRefExpr *SymA = nullptr, *SymB = nullptr;
39 int64_t Cst = 0;
40 uint32_t RefKind = 0;
41
42public:
43 MCValue() = default;
44 int64_t getConstant() const { return Cst; }
45 const MCSymbolRefExpr *getSymA() const { return SymA; }
46 const MCSymbolRefExpr *getSymB() const { return SymB; }
47 uint32_t getRefKind() const { return RefKind; }
48
Andrew Scullcdfcccc2018-10-05 20:58:37 +010049 /// Is this an absolute (as opposed to relocatable) value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050 bool isAbsolute() const { return !SymA && !SymB; }
51
Andrew Scullcdfcccc2018-10-05 20:58:37 +010052 /// Print the value to the stream \p OS.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010053 void print(raw_ostream &OS) const;
54
Andrew Scullcdfcccc2018-10-05 20:58:37 +010055 /// Print the value to stderr.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010056 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
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