1 //===- MCAsmMacro.h - Assembly Macros ---------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_MC_MCASMMACRO_H
10 #define LLVM_MC_MCASMMACRO_H
12 #include "llvm/ADT/APInt.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/SMLoc.h"
20 /// Target independent representation for an assembler token.
33 BigNum
, // larger than 64 bits
48 LParen
, RParen
, LBrac
, RBrac
, LCurly
, RCurly
,
49 Star
, Dot
, Comma
, Dollar
, Equal
, EqualEqual
,
51 Pipe
, PipePipe
, Caret
,
52 Amp
, AmpAmp
, Exclaim
, ExclaimEqual
, Percent
, Hash
,
53 Less
, LessEqual
, LessLess
, LessGreater
,
54 Greater
, GreaterEqual
, GreaterGreater
, At
, MinusGreater
,
56 // MIPS unary expression operators such as %neg.
57 PercentCall16
, PercentCall_Hi
, PercentCall_Lo
, PercentDtprel_Hi
,
58 PercentDtprel_Lo
, PercentGot
, PercentGot_Disp
, PercentGot_Hi
, PercentGot_Lo
,
59 PercentGot_Ofst
, PercentGot_Page
, PercentGottprel
, PercentGp_Rel
, PercentHi
,
60 PercentHigher
, PercentHighest
, PercentLo
, PercentNeg
, PercentPcrel_Hi
,
61 PercentPcrel_Lo
, PercentTlsgd
, PercentTlsldm
, PercentTprel_Hi
,
68 /// A reference to the entire token contents; this is always a pointer into
69 /// a memory buffer owned by the source manager.
76 AsmToken(TokenKind Kind
, StringRef Str
, APInt IntVal
)
77 : Kind(Kind
), Str(Str
), IntVal(std::move(IntVal
)) {}
78 AsmToken(TokenKind Kind
, StringRef Str
, int64_t IntVal
= 0)
79 : Kind(Kind
), Str(Str
), IntVal(64, IntVal
, true) {}
81 TokenKind
getKind() const { return Kind
; }
82 bool is(TokenKind K
) const { return Kind
== K
; }
83 bool isNot(TokenKind K
) const { return Kind
!= K
; }
86 SMLoc
getEndLoc() const;
87 SMRange
getLocRange() const;
89 /// Get the contents of a string token (without quotes).
90 StringRef
getStringContents() const {
91 assert(Kind
== String
&& "This token isn't a string!");
92 return Str
.slice(1, Str
.size() - 1);
95 /// Get the identifier string for the current token, which should be an
96 /// identifier or a string. This gets the portion of the string which should
97 /// be used as the identifier, e.g., it does not include the quotes on
99 StringRef
getIdentifier() const {
100 if (Kind
== Identifier
)
102 return getStringContents();
105 /// Get the string for the current token, this includes all characters (for
106 /// example, the quotes on strings) in the token.
108 /// The returned StringRef points into the source manager's memory buffer, and
109 /// is safe to store across calls to Lex().
110 StringRef
getString() const { return Str
; }
112 // FIXME: Don't compute this in advance, it makes every token larger, and is
113 // also not generally what we want (it is nicer for recovery etc. to lex 123br
114 // as a single token, then diagnose as an invalid number).
115 int64_t getIntVal() const {
116 assert(Kind
== Integer
&& "This token isn't an integer!");
117 return IntVal
.getZExtValue();
120 APInt
getAPIntVal() const {
121 assert((Kind
== Integer
|| Kind
== BigNum
) &&
122 "This token isn't an integer!");
126 void dump(raw_ostream
&OS
) const;
129 struct MCAsmMacroParameter
{
131 std::vector
<AsmToken
> Value
;
132 bool Required
= false;
135 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
136 void dump() const { dump(dbgs()); }
137 LLVM_DUMP_METHOD
void dump(raw_ostream
&OS
) const;
141 typedef std::vector
<MCAsmMacroParameter
> MCAsmMacroParameters
;
145 MCAsmMacroParameters Parameters
;
148 MCAsmMacro(StringRef N
, StringRef B
, MCAsmMacroParameters P
)
149 : Name(N
), Body(B
), Parameters(std::move(P
)) {}
151 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
152 void dump() const { dump(dbgs()); }
153 LLVM_DUMP_METHOD
void dump(raw_ostream
&OS
) const;