//===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // Implement the Lexer for .ll files. // //===----------------------------------------------------------------------===// #include "llvm/AsmParser/LLLexer.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/Twine.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Instruction.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/SourceMgr.h" #include <cassert> #include <cctype> #include <cstdio> usingnamespacellvm; bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const { … } void LLLexer::Warning(LocTy WarningLoc, const Twine &Msg) const { … } //===----------------------------------------------------------------------===// // Helper functions. //===----------------------------------------------------------------------===// // atoull - Convert an ascii string of decimal digits into the unsigned long // long representation... this does not have to do input error checking, // because we know that the input will be matched by a suitable regex... // uint64_t LLLexer::atoull(const char *Buffer, const char *End) { … } uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) { … } void LLLexer::HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]) { … } /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into /// { low64, high16 } as usual for an APInt. void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]) { … } // UnEscapeLexed - Run through the specified buffer and change \xx codes to the // appropriate character. static void UnEscapeLexed(std::string &Str) { … } /// isLabelChar - Return true for [-a-zA-Z$._0-9]. static bool isLabelChar(char C) { … } /// isLabelTail - Return true if this pointer points to a valid end of a label. static const char *isLabelTail(const char *CurPtr) { … } //===----------------------------------------------------------------------===// // Lexer definition. //===----------------------------------------------------------------------===// LLLexer::LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &Err, LLVMContext &C) : … { … } int LLLexer::getNextChar() { … } lltok::Kind LLLexer::LexToken() { … } void LLLexer::SkipLineComment() { … } /// Lex all tokens that start with an @ character. /// GlobalVar @\"[^\"]*\" /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]* /// GlobalVarID @[0-9]+ lltok::Kind LLLexer::LexAt() { … } lltok::Kind LLLexer::LexDollar() { … } /// ReadString - Read a string until the closing quote. lltok::Kind LLLexer::ReadString(lltok::Kind kind) { … } /// ReadVarName - Read the rest of a token containing a variable name. bool LLLexer::ReadVarName() { … } // Lex an ID: [0-9]+. On success, the ID is stored in UIntVal and Token is // returned, otherwise the Error token is returned. lltok::Kind LLLexer::LexUIntID(lltok::Kind Token) { … } lltok::Kind LLLexer::LexVar(lltok::Kind Var, lltok::Kind VarID) { … } /// Lex all tokens that start with a % character. /// LocalVar ::= %\"[^\"]*\" /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]* /// LocalVarID ::= %[0-9]+ lltok::Kind LLLexer::LexPercent() { … } /// Lex all tokens that start with a " character. /// QuoteLabel "[^"]+": /// StringConstant "[^"]*" lltok::Kind LLLexer::LexQuote() { … } /// Lex all tokens that start with a ! character. /// !foo /// ! lltok::Kind LLLexer::LexExclaim() { … } /// Lex all tokens that start with a ^ character. /// SummaryID ::= ^[0-9]+ lltok::Kind LLLexer::LexCaret() { … } /// Lex all tokens that start with a # character. /// AttrGrpID ::= #[0-9]+ /// Hash ::= # lltok::Kind LLLexer::LexHash() { … } /// Lex a label, integer type, keyword, or hexadecimal integer constant. /// Label [-a-zA-Z$._0-9]+: /// IntegerType i[0-9]+ /// Keyword sdiv, float, ... /// HexIntConstant [us]0x[0-9A-Fa-f]+ lltok::Kind LLLexer::LexIdentifier() { … } /// Lex all tokens that start with a 0x prefix, knowing they match and are not /// labels. /// HexFPConstant 0x[0-9A-Fa-f]+ /// HexFP80Constant 0xK[0-9A-Fa-f]+ /// HexFP128Constant 0xL[0-9A-Fa-f]+ /// HexPPC128Constant 0xM[0-9A-Fa-f]+ /// HexHalfConstant 0xH[0-9A-Fa-f]+ /// HexBFloatConstant 0xR[0-9A-Fa-f]+ lltok::Kind LLLexer::Lex0x() { … } /// Lex tokens for a label or a numeric constant, possibly starting with -. /// Label [-a-zA-Z$._0-9]+: /// NInteger -[0-9]+ /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? /// PInteger [0-9]+ /// HexFPConstant 0x[0-9A-Fa-f]+ /// HexFP80Constant 0xK[0-9A-Fa-f]+ /// HexFP128Constant 0xL[0-9A-Fa-f]+ /// HexPPC128Constant 0xM[0-9A-Fa-f]+ lltok::Kind LLLexer::LexDigitOrNegative() { … } /// Lex a floating point constant starting with +. /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? lltok::Kind LLLexer::LexPositive() { … }