1 //===- MCAsmMacro.h - Assembly Macros ---------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCASMMACRO_H
11 #define LLVM_MC_MCASMMACRO_H
13 #include "llvm/ADT/APInt.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/SMLoc.h"
21 /// Target independent representation for an assembler token.
34 BigNum, // larger than 64 bits
49 LParen, RParen, LBrac, RBrac, LCurly, RCurly,
50 Star, Dot, Comma, Dollar, Equal, EqualEqual,
52 Pipe, PipePipe, Caret,
53 Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,
54 Less, LessEqual, LessLess, LessGreater,
55 Greater, GreaterEqual, GreaterGreater, At, MinusGreater,
57 // MIPS unary expression operators such as %neg.
58 PercentCall16, PercentCall_Hi, PercentCall_Lo, PercentDtprel_Hi,
59 PercentDtprel_Lo, PercentGot, PercentGot_Disp, PercentGot_Hi, PercentGot_Lo,
60 PercentGot_Ofst, PercentGot_Page, PercentGottprel, PercentGp_Rel, PercentHi,
61 PercentHigher, PercentHighest, PercentLo, PercentNeg, PercentPcrel_Hi,
62 PercentPcrel_Lo, PercentTlsgd, PercentTlsldm, PercentTprel_Hi,
69 /// A reference to the entire token contents; this is always a pointer into
70 /// a memory buffer owned by the source manager.
77 AsmToken(TokenKind Kind, StringRef Str, APInt IntVal)
78 : Kind(Kind), Str(Str), IntVal(std::move(IntVal)) {}
79 AsmToken(TokenKind Kind, StringRef Str, int64_t IntVal = 0)
80 : Kind(Kind), Str(Str), IntVal(64, IntVal, true) {}
82 TokenKind getKind() const { return Kind; }
83 bool is(TokenKind K) const { return Kind == K; }
84 bool isNot(TokenKind K) const { return Kind != K; }
87 SMLoc getEndLoc() const;
88 SMRange getLocRange() const;
90 /// Get the contents of a string token (without quotes).
91 StringRef getStringContents() const {
92 assert(Kind == String && "This token isn't a string!");
93 return Str.slice(1, Str.size() - 1);
96 /// Get the identifier string for the current token, which should be an
97 /// identifier or a string. This gets the portion of the string which should
98 /// be used as the identifier, e.g., it does not include the quotes on
100 StringRef getIdentifier() const {
101 if (Kind == Identifier)
103 return getStringContents();
106 /// Get the string for the current token, this includes all characters (for
107 /// example, the quotes on strings) in the token.
109 /// The returned StringRef points into the source manager's memory buffer, and
110 /// is safe to store across calls to Lex().
111 StringRef getString() const { return Str; }
113 // FIXME: Don't compute this in advance, it makes every token larger, and is
114 // also not generally what we want (it is nicer for recovery etc. to lex 123br
115 // as a single token, then diagnose as an invalid number).
116 int64_t getIntVal() const {
117 assert(Kind == Integer && "This token isn't an integer!");
118 return IntVal.getZExtValue();
121 APInt getAPIntVal() const {
122 assert((Kind == Integer || Kind == BigNum) &&
123 "This token isn't an integer!");
127 void dump(raw_ostream &OS) const;
128 void dump() const { dump(dbgs()); }
131 struct MCAsmMacroParameter {
133 std::vector<AsmToken> Value;
134 bool Required = false;
137 MCAsmMacroParameter() = default;
139 void dump() const { dump(dbgs()); }
140 void dump(raw_ostream &OS) const;
143 typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
147 MCAsmMacroParameters Parameters;
150 MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
151 : Name(N), Body(B), Parameters(std::move(P)) {}
153 void dump() const { dump(dbgs()); }
154 void dump(raw_ostream &OS) const;