2015-06-23 04:37:46 +08:00
|
|
|
//===- MILexer.cpp - Machine instructions lexer implementation ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the lexing of machine instructions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MILexer.h"
|
2015-07-21 04:31:01 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2015-07-07 07:07:26 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2015-06-23 04:37:46 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include <cctype>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// This class provides a way to iterate and get characters from the source
|
|
|
|
/// string.
|
|
|
|
class Cursor {
|
|
|
|
const char *Ptr;
|
|
|
|
const char *End;
|
|
|
|
|
|
|
|
public:
|
2015-07-01 00:51:29 +08:00
|
|
|
Cursor(NoneType) : Ptr(nullptr), End(nullptr) {}
|
|
|
|
|
2015-06-23 04:37:46 +08:00
|
|
|
explicit Cursor(StringRef Str) {
|
|
|
|
Ptr = Str.data();
|
|
|
|
End = Ptr + Str.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEOF() const { return Ptr == End; }
|
|
|
|
|
2015-06-24 14:40:09 +08:00
|
|
|
char peek(int I = 0) const { return End - Ptr <= I ? 0 : Ptr[I]; }
|
2015-06-23 04:37:46 +08:00
|
|
|
|
2015-06-27 00:46:11 +08:00
|
|
|
void advance(unsigned I = 1) { Ptr += I; }
|
2015-06-23 04:37:46 +08:00
|
|
|
|
|
|
|
StringRef remaining() const { return StringRef(Ptr, End - Ptr); }
|
|
|
|
|
|
|
|
StringRef upto(Cursor C) const {
|
|
|
|
assert(C.Ptr >= Ptr && C.Ptr <= End);
|
|
|
|
return StringRef(Ptr, C.Ptr - Ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef::iterator location() const { return Ptr; }
|
2015-07-01 00:51:29 +08:00
|
|
|
|
|
|
|
operator bool() const { return Ptr != nullptr; }
|
2015-06-23 04:37:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2015-08-07 07:17:42 +08:00
|
|
|
MIToken &MIToken::reset(TokenKind Kind, StringRef Range) {
|
|
|
|
this->Kind = Kind;
|
|
|
|
this->Range = Range;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
MIToken &MIToken::setStringValue(StringRef StrVal) {
|
|
|
|
StringValue = StrVal;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
MIToken &MIToken::setOwnedStringValue(std::string StrVal) {
|
|
|
|
StringValueStorage = std::move(StrVal);
|
|
|
|
StringValue = StringValueStorage;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
MIToken &MIToken::setIntegerValue(APSInt IntVal) {
|
|
|
|
this->IntVal = std::move(IntVal);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-06-23 04:37:46 +08:00
|
|
|
/// Skip the leading whitespace characters and return the updated cursor.
|
|
|
|
static Cursor skipWhitespace(Cursor C) {
|
|
|
|
while (isspace(C.peek()))
|
|
|
|
C.advance();
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-07-18 06:48:04 +08:00
|
|
|
/// Return true if the given character satisfies the following regular
|
|
|
|
/// expression: [-a-zA-Z$._0-9]
|
2015-06-23 04:37:46 +08:00
|
|
|
static bool isIdentifierChar(char C) {
|
2015-07-18 06:48:04 +08:00
|
|
|
return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.' ||
|
|
|
|
C == '$';
|
2015-06-23 04:37:46 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 01:35:55 +08:00
|
|
|
/// Unescapes the given string value.
|
|
|
|
///
|
|
|
|
/// Expects the string value to be quoted.
|
|
|
|
static std::string unescapeQuotedString(StringRef Value) {
|
2015-07-21 04:31:01 +08:00
|
|
|
assert(Value.front() == '"' && Value.back() == '"');
|
|
|
|
Cursor C = Cursor(Value.substr(1, Value.size() - 2));
|
|
|
|
|
2015-08-06 01:35:55 +08:00
|
|
|
std::string Str;
|
2015-07-21 04:31:01 +08:00
|
|
|
Str.reserve(C.remaining().size());
|
|
|
|
while (!C.isEOF()) {
|
|
|
|
char Char = C.peek();
|
|
|
|
if (Char == '\\') {
|
|
|
|
if (C.peek(1) == '\\') {
|
|
|
|
// Two '\' become one
|
|
|
|
Str += '\\';
|
|
|
|
C.advance(2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isxdigit(C.peek(1)) && isxdigit(C.peek(2))) {
|
|
|
|
Str += hexDigitValue(C.peek(1)) * 16 + hexDigitValue(C.peek(2));
|
|
|
|
C.advance(3);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Str += Char;
|
|
|
|
C.advance();
|
|
|
|
}
|
2015-08-06 01:35:55 +08:00
|
|
|
return Str;
|
2015-07-21 04:31:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Lex a string constant using the following regular expression: \"[^\"]*\"
|
|
|
|
static Cursor lexStringConstant(
|
|
|
|
Cursor C,
|
|
|
|
function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
|
|
|
|
assert(C.peek() == '"');
|
|
|
|
for (C.advance(); C.peek() != '"'; C.advance()) {
|
|
|
|
if (C.isEOF()) {
|
|
|
|
ErrorCallback(
|
|
|
|
C.location(),
|
|
|
|
"end of machine instruction reached before the closing '\"'");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.advance();
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-07-29 01:03:40 +08:00
|
|
|
static Cursor lexName(
|
2015-08-06 01:35:55 +08:00
|
|
|
Cursor C, MIToken &Token, MIToken::TokenKind Type, unsigned PrefixLength,
|
2015-07-29 01:03:40 +08:00
|
|
|
function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
|
|
|
|
auto Range = C;
|
|
|
|
C.advance(PrefixLength);
|
|
|
|
if (C.peek() == '"') {
|
|
|
|
if (Cursor R = lexStringConstant(C, ErrorCallback)) {
|
2015-08-06 01:35:55 +08:00
|
|
|
StringRef String = Range.upto(R);
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(Type, String)
|
|
|
|
.setOwnedStringValue(
|
|
|
|
unescapeQuotedString(String.drop_front(PrefixLength)));
|
2015-07-29 01:03:40 +08:00
|
|
|
return R;
|
|
|
|
}
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(MIToken::Error, Range.remaining());
|
2015-07-29 01:03:40 +08:00
|
|
|
return Range;
|
|
|
|
}
|
|
|
|
while (isIdentifierChar(C.peek()))
|
|
|
|
C.advance();
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(Type, Range.upto(C))
|
|
|
|
.setStringValue(Range.upto(C).drop_front(PrefixLength));
|
2015-07-29 01:03:40 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-08-06 02:52:21 +08:00
|
|
|
static Cursor maybeLexIntegerType(Cursor C, MIToken &Token) {
|
|
|
|
if (C.peek() != 'i' || !isdigit(C.peek(1)))
|
|
|
|
return None;
|
|
|
|
auto Range = C;
|
|
|
|
C.advance(); // Skip 'i'
|
|
|
|
while (isdigit(C.peek()))
|
|
|
|
C.advance();
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(MIToken::IntegerType, Range.upto(C));
|
2015-08-06 02:52:21 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-07-07 07:07:26 +08:00
|
|
|
static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
|
|
|
|
return StringSwitch<MIToken::TokenKind>(Identifier)
|
|
|
|
.Case("_", MIToken::underscore)
|
|
|
|
.Case("implicit", MIToken::kw_implicit)
|
|
|
|
.Case("implicit-def", MIToken::kw_implicit_define)
|
2015-07-08 04:34:53 +08:00
|
|
|
.Case("dead", MIToken::kw_dead)
|
2015-07-09 05:23:34 +08:00
|
|
|
.Case("killed", MIToken::kw_killed)
|
2015-07-09 07:58:31 +08:00
|
|
|
.Case("undef", MIToken::kw_undef)
|
2015-08-06 01:49:03 +08:00
|
|
|
.Case("early-clobber", MIToken::kw_early_clobber)
|
2015-08-06 01:41:17 +08:00
|
|
|
.Case("debug-use", MIToken::kw_debug_use)
|
2015-07-17 08:24:15 +08:00
|
|
|
.Case("frame-setup", MIToken::kw_frame_setup)
|
2015-07-23 05:15:11 +08:00
|
|
|
.Case("debug-location", MIToken::kw_debug_location)
|
2015-07-24 07:09:07 +08:00
|
|
|
.Case(".cfi_offset", MIToken::kw_cfi_offset)
|
2015-07-28 04:39:03 +08:00
|
|
|
.Case(".cfi_def_cfa_register", MIToken::kw_cfi_def_cfa_register)
|
2015-07-22 06:28:27 +08:00
|
|
|
.Case(".cfi_def_cfa_offset", MIToken::kw_cfi_def_cfa_offset)
|
2015-07-30 02:57:23 +08:00
|
|
|
.Case(".cfi_def_cfa", MIToken::kw_cfi_def_cfa)
|
2015-07-29 01:28:03 +08:00
|
|
|
.Case("blockaddress", MIToken::kw_blockaddress)
|
2015-07-29 07:02:45 +08:00
|
|
|
.Case("target-index", MIToken::kw_target_index)
|
2015-08-01 04:49:21 +08:00
|
|
|
.Case("half", MIToken::kw_half)
|
|
|
|
.Case("float", MIToken::kw_float)
|
|
|
|
.Case("double", MIToken::kw_double)
|
|
|
|
.Case("x86_fp80", MIToken::kw_x86_fp80)
|
|
|
|
.Case("fp128", MIToken::kw_fp128)
|
|
|
|
.Case("ppc_fp128", MIToken::kw_ppc_fp128)
|
2015-08-06 08:44:07 +08:00
|
|
|
.Case("target-flags", MIToken::kw_target_flags)
|
2015-08-04 08:24:45 +08:00
|
|
|
.Case("volatile", MIToken::kw_volatile)
|
2015-08-07 00:49:30 +08:00
|
|
|
.Case("non-temporal", MIToken::kw_non_temporal)
|
2015-08-07 00:55:53 +08:00
|
|
|
.Case("invariant", MIToken::kw_invariant)
|
2015-08-08 04:48:30 +08:00
|
|
|
.Case("align", MIToken::kw_align)
|
2015-08-13 04:33:26 +08:00
|
|
|
.Case("constant-pool", MIToken::kw_constant_pool)
|
2015-08-11 07:24:42 +08:00
|
|
|
.Case("liveout", MIToken::kw_liveout)
|
2015-07-07 07:07:26 +08:00
|
|
|
.Default(MIToken::Identifier);
|
|
|
|
}
|
|
|
|
|
2015-07-01 00:51:29 +08:00
|
|
|
static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
|
2015-07-22 06:28:27 +08:00
|
|
|
if (!isalpha(C.peek()) && C.peek() != '_' && C.peek() != '.')
|
2015-07-01 00:51:29 +08:00
|
|
|
return None;
|
2015-06-23 04:37:46 +08:00
|
|
|
auto Range = C;
|
|
|
|
while (isIdentifierChar(C.peek()))
|
|
|
|
C.advance();
|
2015-06-25 01:34:58 +08:00
|
|
|
auto Identifier = Range.upto(C);
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(getIdentifierKind(Identifier), Identifier)
|
|
|
|
.setStringValue(Identifier);
|
2015-06-23 04:37:46 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-07-01 00:51:29 +08:00
|
|
|
static Cursor maybeLexMachineBasicBlock(
|
2015-06-27 00:46:11 +08:00
|
|
|
Cursor C, MIToken &Token,
|
|
|
|
function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
|
2015-07-01 00:51:29 +08:00
|
|
|
if (!C.remaining().startswith("%bb."))
|
|
|
|
return None;
|
2015-06-27 00:46:11 +08:00
|
|
|
auto Range = C;
|
|
|
|
C.advance(4); // Skip '%bb.'
|
|
|
|
if (!isdigit(C.peek())) {
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(MIToken::Error, C.remaining());
|
2015-06-27 00:46:11 +08:00
|
|
|
ErrorCallback(C.location(), "expected a number after '%bb.'");
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
auto NumberRange = C;
|
|
|
|
while (isdigit(C.peek()))
|
|
|
|
C.advance();
|
|
|
|
StringRef Number = NumberRange.upto(C);
|
|
|
|
unsigned StringOffset = 4 + Number.size(); // Drop '%bb.<id>'
|
|
|
|
if (C.peek() == '.') {
|
|
|
|
C.advance(); // Skip '.'
|
|
|
|
++StringOffset;
|
|
|
|
while (isIdentifierChar(C.peek()))
|
|
|
|
C.advance();
|
|
|
|
}
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(MIToken::MachineBasicBlock, Range.upto(C))
|
|
|
|
.setIntegerValue(APSInt(Number))
|
|
|
|
.setStringValue(Range.upto(C).drop_front(StringOffset));
|
2015-06-27 00:46:11 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-07-16 07:38:35 +08:00
|
|
|
static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
|
|
|
|
MIToken::TokenKind Kind) {
|
|
|
|
if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
|
|
|
|
return None;
|
|
|
|
auto Range = C;
|
|
|
|
C.advance(Rule.size());
|
|
|
|
auto NumberRange = C;
|
|
|
|
while (isdigit(C.peek()))
|
|
|
|
C.advance();
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C)));
|
2015-07-16 07:38:35 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-07-17 07:37:45 +08:00
|
|
|
static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
|
|
|
|
MIToken::TokenKind Kind) {
|
|
|
|
if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
|
|
|
|
return None;
|
|
|
|
auto Range = C;
|
|
|
|
C.advance(Rule.size());
|
|
|
|
auto NumberRange = C;
|
|
|
|
while (isdigit(C.peek()))
|
|
|
|
C.advance();
|
|
|
|
StringRef Number = NumberRange.upto(C);
|
|
|
|
unsigned StringOffset = Rule.size() + Number.size();
|
|
|
|
if (C.peek() == '.') {
|
|
|
|
C.advance();
|
|
|
|
++StringOffset;
|
|
|
|
while (isIdentifierChar(C.peek()))
|
|
|
|
C.advance();
|
|
|
|
}
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(Kind, Range.upto(C))
|
|
|
|
.setIntegerValue(APSInt(Number))
|
|
|
|
.setStringValue(Range.upto(C).drop_front(StringOffset));
|
2015-07-17 07:37:45 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-07-16 07:38:35 +08:00
|
|
|
static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
|
|
|
|
return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
|
|
|
|
}
|
|
|
|
|
2015-07-17 07:37:45 +08:00
|
|
|
static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
|
|
|
|
return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
|
|
|
|
return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
|
|
|
|
}
|
|
|
|
|
2015-07-21 04:51:18 +08:00
|
|
|
static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
|
|
|
|
return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
|
|
|
|
}
|
|
|
|
|
2015-07-29 01:28:03 +08:00
|
|
|
static Cursor maybeLexIRBlock(
|
|
|
|
Cursor C, MIToken &Token,
|
|
|
|
function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
|
|
|
|
const StringRef Rule = "%ir-block.";
|
|
|
|
if (!C.remaining().startswith(Rule))
|
|
|
|
return None;
|
|
|
|
if (isdigit(C.peek(Rule.size())))
|
|
|
|
return maybeLexIndex(C, Token, Rule, MIToken::IRBlock);
|
2015-08-06 01:35:55 +08:00
|
|
|
return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
|
2015-07-28 06:42:41 +08:00
|
|
|
}
|
|
|
|
|
2015-08-04 07:08:19 +08:00
|
|
|
static Cursor maybeLexIRValue(
|
|
|
|
Cursor C, MIToken &Token,
|
|
|
|
function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
|
|
|
|
const StringRef Rule = "%ir.";
|
|
|
|
if (!C.remaining().startswith(Rule))
|
|
|
|
return None;
|
2015-08-06 01:35:55 +08:00
|
|
|
return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
|
2015-08-04 07:08:19 +08:00
|
|
|
}
|
|
|
|
|
2015-07-11 06:51:20 +08:00
|
|
|
static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
|
|
|
|
auto Range = C;
|
|
|
|
C.advance(); // Skip '%'
|
|
|
|
auto NumberRange = C;
|
|
|
|
while (isdigit(C.peek()))
|
|
|
|
C.advance();
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(MIToken::VirtualRegister, Range.upto(C))
|
|
|
|
.setIntegerValue(APSInt(NumberRange.upto(C)));
|
2015-07-11 06:51:20 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-07-01 00:51:29 +08:00
|
|
|
static Cursor maybeLexRegister(Cursor C, MIToken &Token) {
|
|
|
|
if (C.peek() != '%')
|
|
|
|
return None;
|
2015-07-11 06:51:20 +08:00
|
|
|
if (isdigit(C.peek(1)))
|
|
|
|
return lexVirtualRegister(C, Token);
|
2015-06-24 00:35:26 +08:00
|
|
|
auto Range = C;
|
|
|
|
C.advance(); // Skip '%'
|
|
|
|
while (isIdentifierChar(C.peek()))
|
|
|
|
C.advance();
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(MIToken::NamedRegister, Range.upto(C))
|
|
|
|
.setStringValue(Range.upto(C).drop_front(1)); // Drop the '%'
|
2015-06-24 00:35:26 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:23:08 +08:00
|
|
|
static Cursor maybeLexGlobalValue(
|
|
|
|
Cursor C, MIToken &Token,
|
|
|
|
function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
|
|
|
|
if (C.peek() != '@')
|
|
|
|
return None;
|
|
|
|
if (!isdigit(C.peek(1)))
|
2015-08-06 01:35:55 +08:00
|
|
|
return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1,
|
2015-07-22 05:23:08 +08:00
|
|
|
ErrorCallback);
|
|
|
|
auto Range = C;
|
|
|
|
C.advance(1); // Skip the '@'
|
|
|
|
auto NumberRange = C;
|
|
|
|
while (isdigit(C.peek()))
|
|
|
|
C.advance();
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(MIToken::GlobalValue, Range.upto(C))
|
|
|
|
.setIntegerValue(APSInt(NumberRange.upto(C)));
|
2015-07-22 05:23:08 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-07-22 00:59:53 +08:00
|
|
|
static Cursor maybeLexExternalSymbol(
|
|
|
|
Cursor C, MIToken &Token,
|
|
|
|
function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
|
|
|
|
if (C.peek() != '$')
|
|
|
|
return None;
|
2015-08-06 01:35:55 +08:00
|
|
|
return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
|
|
|
|
ErrorCallback);
|
2015-07-22 00:59:53 +08:00
|
|
|
}
|
|
|
|
|
2015-08-01 04:49:21 +08:00
|
|
|
static bool isValidHexFloatingPointPrefix(char C) {
|
|
|
|
return C == 'H' || C == 'K' || C == 'L' || C == 'M';
|
|
|
|
}
|
|
|
|
|
|
|
|
static Cursor maybeLexHexFloatingPointLiteral(Cursor C, MIToken &Token) {
|
|
|
|
if (C.peek() != '0' || C.peek(1) != 'x')
|
|
|
|
return None;
|
|
|
|
Cursor Range = C;
|
|
|
|
C.advance(2); // Skip '0x'
|
|
|
|
if (isValidHexFloatingPointPrefix(C.peek()))
|
|
|
|
C.advance();
|
|
|
|
while (isxdigit(C.peek()))
|
|
|
|
C.advance();
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
|
2015-08-01 04:49:21 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) {
|
|
|
|
C.advance();
|
|
|
|
// Skip over [0-9]*([eE][-+]?[0-9]+)?
|
|
|
|
while (isdigit(C.peek()))
|
|
|
|
C.advance();
|
|
|
|
if ((C.peek() == 'e' || C.peek() == 'E') &&
|
|
|
|
(isdigit(C.peek(1)) ||
|
|
|
|
((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) {
|
|
|
|
C.advance(2);
|
|
|
|
while (isdigit(C.peek()))
|
|
|
|
C.advance();
|
|
|
|
}
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
|
2015-08-01 04:49:21 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) {
|
2015-07-01 00:51:29 +08:00
|
|
|
if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
|
|
|
|
return None;
|
2015-06-24 07:42:28 +08:00
|
|
|
auto Range = C;
|
|
|
|
C.advance();
|
|
|
|
while (isdigit(C.peek()))
|
|
|
|
C.advance();
|
2015-08-01 04:49:21 +08:00
|
|
|
if (C.peek() == '.')
|
|
|
|
return lexFloatingPointLiteral(Range, C, Token);
|
2015-06-24 07:42:28 +08:00
|
|
|
StringRef StrVal = Range.upto(C);
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal));
|
2015-06-24 07:42:28 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-06-24 00:35:26 +08:00
|
|
|
static MIToken::TokenKind symbolToken(char C) {
|
|
|
|
switch (C) {
|
|
|
|
case ',':
|
|
|
|
return MIToken::comma;
|
|
|
|
case '=':
|
|
|
|
return MIToken::equal;
|
2015-07-14 07:24:34 +08:00
|
|
|
case ':':
|
|
|
|
return MIToken::colon;
|
2015-07-23 01:58:46 +08:00
|
|
|
case '!':
|
|
|
|
return MIToken::exclaim;
|
2015-07-29 01:28:03 +08:00
|
|
|
case '(':
|
|
|
|
return MIToken::lparen;
|
|
|
|
case ')':
|
|
|
|
return MIToken::rparen;
|
2015-08-06 06:26:15 +08:00
|
|
|
case '+':
|
|
|
|
return MIToken::plus;
|
|
|
|
case '-':
|
|
|
|
return MIToken::minus;
|
2015-06-24 00:35:26 +08:00
|
|
|
default:
|
|
|
|
return MIToken::Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-01 00:51:29 +08:00
|
|
|
static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
|
2015-08-04 07:08:19 +08:00
|
|
|
MIToken::TokenKind Kind;
|
|
|
|
unsigned Length = 1;
|
|
|
|
if (C.peek() == ':' && C.peek(1) == ':') {
|
|
|
|
Kind = MIToken::coloncolon;
|
|
|
|
Length = 2;
|
|
|
|
} else
|
|
|
|
Kind = symbolToken(C.peek());
|
2015-07-01 00:51:29 +08:00
|
|
|
if (Kind == MIToken::Error)
|
|
|
|
return None;
|
2015-06-24 00:35:26 +08:00
|
|
|
auto Range = C;
|
2015-08-04 07:08:19 +08:00
|
|
|
C.advance(Length);
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(Kind, Range.upto(C));
|
2015-06-24 00:35:26 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-06-23 04:37:46 +08:00
|
|
|
StringRef llvm::lexMIToken(
|
|
|
|
StringRef Source, MIToken &Token,
|
|
|
|
function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
|
|
|
|
auto C = skipWhitespace(Cursor(Source));
|
|
|
|
if (C.isEOF()) {
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(MIToken::Eof, C.remaining());
|
2015-06-23 04:37:46 +08:00
|
|
|
return C.remaining();
|
|
|
|
}
|
|
|
|
|
2015-08-06 02:52:21 +08:00
|
|
|
if (Cursor R = maybeLexIntegerType(C, Token))
|
|
|
|
return R.remaining();
|
2015-07-01 00:51:29 +08:00
|
|
|
if (Cursor R = maybeLexIdentifier(C, Token))
|
|
|
|
return R.remaining();
|
|
|
|
if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
|
|
|
|
return R.remaining();
|
2015-07-16 07:38:35 +08:00
|
|
|
if (Cursor R = maybeLexJumpTableIndex(C, Token))
|
|
|
|
return R.remaining();
|
2015-07-17 07:37:45 +08:00
|
|
|
if (Cursor R = maybeLexStackObject(C, Token))
|
|
|
|
return R.remaining();
|
|
|
|
if (Cursor R = maybeLexFixedStackObject(C, Token))
|
|
|
|
return R.remaining();
|
2015-07-21 04:51:18 +08:00
|
|
|
if (Cursor R = maybeLexConstantPoolItem(C, Token))
|
|
|
|
return R.remaining();
|
2015-07-29 01:28:03 +08:00
|
|
|
if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback))
|
2015-07-28 06:42:41 +08:00
|
|
|
return R.remaining();
|
2015-08-04 07:08:19 +08:00
|
|
|
if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback))
|
|
|
|
return R.remaining();
|
2015-07-01 00:51:29 +08:00
|
|
|
if (Cursor R = maybeLexRegister(C, Token))
|
|
|
|
return R.remaining();
|
2015-07-21 04:31:01 +08:00
|
|
|
if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
|
2015-07-01 00:51:29 +08:00
|
|
|
return R.remaining();
|
2015-07-22 00:59:53 +08:00
|
|
|
if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
|
|
|
|
return R.remaining();
|
2015-08-01 04:49:21 +08:00
|
|
|
if (Cursor R = maybeLexHexFloatingPointLiteral(C, Token))
|
|
|
|
return R.remaining();
|
|
|
|
if (Cursor R = maybeLexNumericalLiteral(C, Token))
|
2015-07-01 00:51:29 +08:00
|
|
|
return R.remaining();
|
|
|
|
if (Cursor R = maybeLexSymbol(C, Token))
|
|
|
|
return R.remaining();
|
|
|
|
|
2015-08-07 07:17:42 +08:00
|
|
|
Token.reset(MIToken::Error, C.remaining());
|
2015-06-23 04:37:46 +08:00
|
|
|
ErrorCallback(C.location(),
|
2015-07-01 00:51:29 +08:00
|
|
|
Twine("unexpected character '") + Twine(C.peek()) + "'");
|
2015-06-23 04:37:46 +08:00
|
|
|
return C.remaining();
|
|
|
|
}
|