LLVM  10.0.0svn
MCAsmMacro.h
Go to the documentation of this file.
1 //===- MCAsmMacro.h - Assembly Macros ---------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_MC_MCASMMACRO_H
10 #define LLVM_MC_MCASMMACRO_H
11 
12 #include "llvm/ADT/APInt.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/SMLoc.h"
16 #include <vector>
17 
18 namespace llvm {
19 
20 /// Target independent representation for an assembler token.
21 class AsmToken {
22 public:
23  enum TokenKind {
24  // Markers
26 
27  // String values.
30 
31  // Integer values.
33  BigNum, // larger than 64 bits
34 
35  // Real values.
37 
38  // Comments
41  // No-value.
46  Slash, // '/'
47  BackSlash, // '\'
50 
55 
56  // MIPS unary expression operators such as %neg.
63  };
64 
65 private:
66  TokenKind Kind;
67 
68  /// A reference to the entire token contents; this is always a pointer into
69  /// a memory buffer owned by the source manager.
70  StringRef Str;
71 
72  APInt IntVal;
73 
74 public:
75  AsmToken() = default;
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) {}
80 
81  TokenKind getKind() const { return Kind; }
82  bool is(TokenKind K) const { return Kind == K; }
83  bool isNot(TokenKind K) const { return Kind != K; }
84 
85  SMLoc getLoc() const;
86  SMLoc getEndLoc() const;
87  SMRange getLocRange() const;
88 
89  /// Get the contents of a string token (without quotes).
91  assert(Kind == String && "This token isn't a string!");
92  return Str.slice(1, Str.size() - 1);
93  }
94 
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
98  /// strings.
100  if (Kind == Identifier)
101  return getString();
102  return getStringContents();
103  }
104 
105  /// Get the string for the current token, this includes all characters (for
106  /// example, the quotes on strings) in the token.
107  ///
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; }
111 
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();
118  }
119 
120  APInt getAPIntVal() const {
121  assert((Kind == Integer || Kind == BigNum) &&
122  "This token isn't an integer!");
123  return IntVal;
124  }
125 
126  void dump(raw_ostream &OS) const;
127 };
128 
131  std::vector<AsmToken> Value;
132  bool Required = false;
133  bool Vararg = false;
134 
135 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
136  void dump() const { dump(dbgs()); }
137  LLVM_DUMP_METHOD void dump(raw_ostream &OS) const;
138 #endif
139 };
140 
141 typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
142 struct MCAsmMacro {
145  MCAsmMacroParameters Parameters;
146 
147 public:
148  MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
149  : Name(N), Body(B), Parameters(std::move(P)) {}
150 
151 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
152  void dump() const { dump(dbgs()); }
153  LLVM_DUMP_METHOD void dump(raw_ostream &OS) const;
154 #endif
155 };
156 } // namespace llvm
157 
158 #endif
Represents a range in source code.
Definition: SMLoc.h:48
AsmToken(TokenKind Kind, StringRef Str, APInt IntVal)
Definition: MCAsmMacro.h:76
StringRef getString() const
Get the string for the current token, this includes all characters (for example, the quotes on string...
Definition: MCAsmMacro.h:110
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1569
AsmToken()=default
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:473
bool isNot(TokenKind K) const
Definition: MCAsmMacro.h:83
block Block Frequency true
SMRange getLocRange() const
Definition: MCAsmLexer.cpp:35
StringRef getIdentifier() const
Get the identifier string for the current token, which should be an identifier or a string...
Definition: MCAsmMacro.h:99
Definition: BitVector.h:937
Target independent representation for an assembler token.
Definition: MCAsmMacro.h:21
LLVM_NODISCARD StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:679
This file implements a class to represent arbitrary precision integral constant values and operations...
void dump() const
Definition: MCAsmMacro.h:152
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
SMLoc getLoc() const
Definition: MCAsmLexer.cpp:27
MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
Definition: MCAsmMacro.h:148
#define P(N)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
StringRef Name
Definition: MCAsmMacro.h:143
SMLoc getEndLoc() const
Definition: MCAsmLexer.cpp:31
MCAsmMacroParameters Parameters
Definition: MCAsmMacro.h:145
int64_t getIntVal() const
Definition: MCAsmMacro.h:115
StringRef Body
Definition: MCAsmMacro.h:144
AsmToken(TokenKind Kind, StringRef Str, int64_t IntVal=0)
Definition: MCAsmMacro.h:78
std::vector< MCAsmMacroParameter > MCAsmMacroParameters
Definition: MCAsmMacro.h:141
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
bool is(TokenKind K) const
Definition: MCAsmMacro.h:82
Class for arbitrary precision integers.
Definition: APInt.h:69
APInt getAPIntVal() const
Definition: MCAsmMacro.h:120
StringRef getStringContents() const
Get the contents of a string token (without quotes).
Definition: MCAsmMacro.h:90
#define N
void dump(raw_ostream &OS) const
Definition: MCAsmLexer.cpp:39
std::vector< AsmToken > Value
Definition: MCAsmMacro.h:131
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Represents a location in source code.
Definition: SMLoc.h:23
TokenKind getKind() const
Definition: MCAsmMacro.h:81