• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- MCAsmMacro.h - Assembly Macros ---------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_MC_MCASMMACRO_H
11 #define LLVM_MC_MCASMMACRO_H
12 
13 #include "llvm/ADT/APInt.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/SMLoc.h"
17 #include <vector>
18 
19 namespace llvm {
20 
21 /// Target independent representation for an assembler token.
22 class AsmToken {
23 public:
24   enum TokenKind {
25     // Markers
26     Eof, Error,
27 
28     // String values.
29     Identifier,
30     String,
31 
32     // Integer values.
33     Integer,
34     BigNum, // larger than 64 bits
35 
36     // Real values.
37     Real,
38 
39     // Comments
40     Comment,
41     HashDirective,
42     // No-value.
43     EndOfStatement,
44     Colon,
45     Space,
46     Plus, Minus, Tilde,
47     Slash,     // '/'
48     BackSlash, // '\'
49     LParen, RParen, LBrac, RBrac, LCurly, RCurly,
50     Star, Dot, Comma, Dollar, Equal, EqualEqual,
51 
52     Pipe, PipePipe, Caret,
53     Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,
54     Less, LessEqual, LessLess, LessGreater,
55     Greater, GreaterEqual, GreaterGreater, At,
56 
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,
63     PercentTprel_Lo
64   };
65 
66 private:
67   TokenKind Kind;
68 
69   /// A reference to the entire token contents; this is always a pointer into
70   /// a memory buffer owned by the source manager.
71   StringRef Str;
72 
73   APInt IntVal;
74 
75 public:
76   AsmToken() = default;
AsmToken(TokenKind Kind,StringRef Str,APInt IntVal)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)
Kind(Kind)80       : Kind(Kind), Str(Str), IntVal(64, IntVal, true) {}
81 
getKind()82   TokenKind getKind() const { return Kind; }
is(TokenKind K)83   bool is(TokenKind K) const { return Kind == K; }
isNot(TokenKind K)84   bool isNot(TokenKind K) const { return Kind != K; }
85 
86   SMLoc getLoc() const;
87   SMLoc getEndLoc() const;
88   SMRange getLocRange() const;
89 
90   /// Get the contents of a string token (without quotes).
getStringContents()91   StringRef getStringContents() const {
92     assert(Kind == String && "This token isn't a string!");
93     return Str.slice(1, Str.size() - 1);
94   }
95 
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
99   /// strings.
getIdentifier()100   StringRef getIdentifier() const {
101     if (Kind == Identifier)
102       return getString();
103     return getStringContents();
104   }
105 
106   /// Get the string for the current token, this includes all characters (for
107   /// example, the quotes on strings) in the token.
108   ///
109   /// The returned StringRef points into the source manager's memory buffer, and
110   /// is safe to store across calls to Lex().
getString()111   StringRef getString() const { return Str; }
112 
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).
getIntVal()116   int64_t getIntVal() const {
117     assert(Kind == Integer && "This token isn't an integer!");
118     return IntVal.getZExtValue();
119   }
120 
getAPIntVal()121   APInt getAPIntVal() const {
122     assert((Kind == Integer || Kind == BigNum) &&
123            "This token isn't an integer!");
124     return IntVal;
125   }
126 
127   void dump(raw_ostream &OS) const;
dump()128   void dump() const { dump(dbgs()); }
129 };
130 
131 struct MCAsmMacroParameter {
132   StringRef Name;
133   std::vector<AsmToken> Value;
134   bool Required = false;
135   bool Vararg = false;
136 
137   MCAsmMacroParameter() = default;
138 
dumpMCAsmMacroParameter139   void dump() const { dump(dbgs()); }
140   void dump(raw_ostream &OS) const;
141 };
142 
143 typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
144 struct MCAsmMacro {
145   StringRef Name;
146   StringRef Body;
147   MCAsmMacroParameters Parameters;
148 
149 public:
MCAsmMacroMCAsmMacro150   MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
151       : Name(N), Body(B), Parameters(std::move(P)) {}
152 
dumpMCAsmMacro153   void dump() const { dump(dbgs()); }
154   void dump(raw_ostream &OS) const;
155 };
156 } // namespace llvm
157 
158 #endif
159