Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:12

0001 //===- MCAsmMacro.h - Assembly Macros ---------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_MC_MCASMMACRO_H
0010 #define LLVM_MC_MCASMMACRO_H
0011 
0012 #include "llvm/ADT/APInt.h"
0013 #include "llvm/ADT/StringRef.h"
0014 #include "llvm/Support/Debug.h"
0015 #include "llvm/Support/SMLoc.h"
0016 #include <vector>
0017 
0018 namespace llvm {
0019 
0020 /// Target independent representation for an assembler token.
0021 class AsmToken {
0022 public:
0023   enum TokenKind {
0024     // Markers
0025     Eof, Error,
0026 
0027     // String values.
0028     Identifier,
0029     String,
0030 
0031     // Integer values.
0032     Integer,
0033     BigNum, // larger than 64 bits
0034 
0035     // Real values.
0036     Real,
0037 
0038     // Comments
0039     Comment,
0040     HashDirective,
0041     // No-value.
0042     EndOfStatement,
0043     Colon,
0044     Space,
0045     Plus, Minus, Tilde,
0046     Slash,     // '/'
0047     BackSlash, // '\'
0048     LParen, RParen, LBrac, RBrac, LCurly, RCurly,
0049     Question, Star, Dot, Comma, Dollar, Equal, EqualEqual,
0050 
0051     Pipe, PipePipe, Caret,
0052     Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,
0053     Less, LessEqual, LessLess, LessGreater,
0054     Greater, GreaterEqual, GreaterGreater, At, MinusGreater,
0055 
0056     // MIPS unary expression operators such as %neg.
0057     PercentCall16, PercentCall_Hi, PercentCall_Lo, PercentDtprel_Hi,
0058     PercentDtprel_Lo, PercentGot, PercentGot_Disp, PercentGot_Hi, PercentGot_Lo,
0059     PercentGot_Ofst, PercentGot_Page, PercentGottprel, PercentGp_Rel, PercentHi,
0060     PercentHigher, PercentHighest, PercentLo, PercentNeg, PercentPcrel_Hi,
0061     PercentPcrel_Lo, PercentTlsgd, PercentTlsldm, PercentTprel_Hi,
0062     PercentTprel_Lo
0063   };
0064 
0065 private:
0066   TokenKind Kind = TokenKind::Eof;
0067 
0068   /// A reference to the entire token contents; this is always a pointer into
0069   /// a memory buffer owned by the source manager.
0070   StringRef Str;
0071 
0072   APInt IntVal;
0073 
0074 public:
0075   AsmToken() = default;
0076   AsmToken(TokenKind Kind, StringRef Str, APInt IntVal)
0077       : Kind(Kind), Str(Str), IntVal(std::move(IntVal)) {}
0078   AsmToken(TokenKind Kind, StringRef Str, int64_t IntVal = 0)
0079       : Kind(Kind), Str(Str), IntVal(64, IntVal, true) {}
0080 
0081   TokenKind getKind() const { return Kind; }
0082   bool is(TokenKind K) const { return Kind == K; }
0083   bool isNot(TokenKind K) const { return Kind != K; }
0084 
0085   SMLoc getLoc() const;
0086   SMLoc getEndLoc() const;
0087   SMRange getLocRange() const;
0088 
0089   /// Get the contents of a string token (without quotes).
0090   StringRef getStringContents() const {
0091     assert(Kind == String && "This token isn't a string!");
0092     return Str.slice(1, Str.size() - 1);
0093   }
0094 
0095   /// Get the identifier string for the current token, which should be an
0096   /// identifier or a string. This gets the portion of the string which should
0097   /// be used as the identifier, e.g., it does not include the quotes on
0098   /// strings.
0099   StringRef getIdentifier() const {
0100     if (Kind == Identifier)
0101       return getString();
0102     return getStringContents();
0103   }
0104 
0105   /// Get the string for the current token, this includes all characters (for
0106   /// example, the quotes on strings) in the token.
0107   ///
0108   /// The returned StringRef points into the source manager's memory buffer, and
0109   /// is safe to store across calls to Lex().
0110   StringRef getString() const { return Str; }
0111 
0112   // FIXME: Don't compute this in advance, it makes every token larger, and is
0113   // also not generally what we want (it is nicer for recovery etc. to lex 123br
0114   // as a single token, then diagnose as an invalid number).
0115   int64_t getIntVal() const {
0116     assert(Kind == Integer && "This token isn't an integer!");
0117     return IntVal.getZExtValue();
0118   }
0119 
0120   APInt getAPIntVal() const {
0121     assert((Kind == Integer || Kind == BigNum) &&
0122            "This token isn't an integer!");
0123     return IntVal;
0124   }
0125 
0126   void dump(raw_ostream &OS) const;
0127 };
0128 
0129 struct MCAsmMacroParameter {
0130   StringRef Name;
0131   std::vector<AsmToken> Value;
0132   bool Required = false;
0133   bool Vararg = false;
0134 
0135 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
0136   void dump() const { dump(dbgs()); }
0137   LLVM_DUMP_METHOD void dump(raw_ostream &OS) const;
0138 #endif
0139 };
0140 
0141 typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
0142 struct MCAsmMacro {
0143   StringRef Name;
0144   StringRef Body;
0145   MCAsmMacroParameters Parameters;
0146   std::vector<std::string> Locals;
0147   bool IsFunction = false;
0148   unsigned Count = 0;
0149 
0150 public:
0151   MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
0152       : Name(N), Body(B), Parameters(std::move(P)) {}
0153   MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P,
0154              std::vector<std::string> L, bool F)
0155       : Name(N), Body(B), Parameters(std::move(P)), Locals(std::move(L)),
0156         IsFunction(F) {}
0157 
0158 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
0159   void dump() const { dump(dbgs()); }
0160   LLVM_DUMP_METHOD void dump(raw_ostream &OS) const;
0161 #endif
0162 };
0163 } // namespace llvm
0164 
0165 #endif