X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FCodeGen%2FMIRParser%2FMILexer.cpp;h=310ac7b4d7161fab92e910c33d30d963a8297404;hb=7a40257530af4fd05cb6d627b7ba0ad25ba66206;hp=6c3d2e064ac53eaa48afd01d42c830edab281649;hpb=03c5b6047eeedbfc10ca3626934c463cdb9e6abf;p=oota-llvm.git diff --git a/lib/CodeGen/MIRParser/MILexer.cpp b/lib/CodeGen/MIRParser/MILexer.cpp index 6c3d2e064ac..310ac7b4d71 100644 --- a/lib/CodeGen/MIRParser/MILexer.cpp +++ b/lib/CodeGen/MIRParser/MILexer.cpp @@ -69,13 +69,14 @@ static bool isIdentifierChar(char C) { C == '$'; } -void MIToken::unescapeQuotedStringValue(std::string &Str) const { - assert(isStringValueQuoted() && "String value isn't quoted"); - StringRef Value = Range.drop_front(StringOffset); +/// Unescapes the given string value. +/// +/// Expects the string value to be quoted. +static std::string unescapeQuotedString(StringRef Value) { assert(Value.front() == '"' && Value.back() == '"'); Cursor C = Cursor(Value.substr(1, Value.size() - 2)); - Str.clear(); + std::string Str; Str.reserve(C.remaining().size()); while (!C.isEOF()) { char Char = C.peek(); @@ -95,6 +96,7 @@ void MIToken::unescapeQuotedStringValue(std::string &Str) const { Str += Char; C.advance(); } + return Str; } /// Lex a string constant using the following regular expression: \"[^\"]*\" @@ -114,6 +116,39 @@ static Cursor lexStringConstant( return C; } +static Cursor lexName( + Cursor C, MIToken &Token, MIToken::TokenKind Type, unsigned PrefixLength, + function_ref ErrorCallback) { + auto Range = C; + C.advance(PrefixLength); + if (C.peek() == '"') { + if (Cursor R = lexStringConstant(C, ErrorCallback)) { + StringRef String = Range.upto(R); + Token = MIToken(Type, String, + unescapeQuotedString(String.drop_front(PrefixLength)), + PrefixLength); + return R; + } + Token = MIToken(MIToken::Error, Range.remaining()); + return Range; + } + while (isIdentifierChar(C.peek())) + C.advance(); + Token = MIToken(Type, Range.upto(C), PrefixLength); + return C; +} + +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(); + Token = MIToken(MIToken::IntegerType, Range.upto(C)); + return C; +} + static MIToken::TokenKind getIdentifierKind(StringRef Identifier) { return StringSwitch(Identifier) .Case("_", MIToken::underscore) @@ -122,8 +157,26 @@ static MIToken::TokenKind getIdentifierKind(StringRef Identifier) { .Case("dead", MIToken::kw_dead) .Case("killed", MIToken::kw_killed) .Case("undef", MIToken::kw_undef) + .Case("early-clobber", MIToken::kw_early_clobber) + .Case("debug-use", MIToken::kw_debug_use) .Case("frame-setup", MIToken::kw_frame_setup) + .Case("debug-location", MIToken::kw_debug_location) + .Case(".cfi_offset", MIToken::kw_cfi_offset) + .Case(".cfi_def_cfa_register", MIToken::kw_cfi_def_cfa_register) .Case(".cfi_def_cfa_offset", MIToken::kw_cfi_def_cfa_offset) + .Case(".cfi_def_cfa", MIToken::kw_cfi_def_cfa) + .Case("blockaddress", MIToken::kw_blockaddress) + .Case("target-index", MIToken::kw_target_index) + .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) + .Case("target-flags", MIToken::kw_target_flags) + .Case("volatile", MIToken::kw_volatile) + .Case("non-temporal", MIToken::kw_non_temporal) + .Case("invariant", MIToken::kw_invariant) .Default(MIToken::Identifier); } @@ -216,6 +269,26 @@ static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) { return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem); } +static Cursor maybeLexIRBlock( + Cursor C, MIToken &Token, + function_ref 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); + return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback); +} + +static Cursor maybeLexIRValue( + Cursor C, MIToken &Token, + function_ref ErrorCallback) { + const StringRef Rule = "%ir."; + if (!C.remaining().startswith(Rule)) + return None; + return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback); +} + static Cursor lexVirtualRegister(Cursor C, MIToken &Token) { auto Range = C; C.advance(); // Skip '%' @@ -241,34 +314,13 @@ static Cursor maybeLexRegister(Cursor C, MIToken &Token) { return C; } -static Cursor lexName( - Cursor C, MIToken &Token, MIToken::TokenKind Type, - MIToken::TokenKind QuotedType, unsigned PrefixLength, - function_ref ErrorCallback) { - auto Range = C; - C.advance(PrefixLength); - if (C.peek() == '"') { - if (Cursor R = lexStringConstant(C, ErrorCallback)) { - Token = MIToken(QuotedType, Range.upto(R), PrefixLength); - return R; - } - Token = MIToken(MIToken::Error, Range.remaining()); - return Range; - } - while (isIdentifierChar(C.peek())) - C.advance(); - Token = MIToken(Type, Range.upto(C), PrefixLength); - return C; -} - static Cursor maybeLexGlobalValue( Cursor C, MIToken &Token, function_ref ErrorCallback) { if (C.peek() != '@') return None; if (!isdigit(C.peek(1))) - return lexName(C, Token, MIToken::NamedGlobalValue, - MIToken::QuotedNamedGlobalValue, /*PrefixLength=*/1, + return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1, ErrorCallback); auto Range = C; C.advance(1); // Skip the '@' @@ -285,18 +337,52 @@ static Cursor maybeLexExternalSymbol( function_ref ErrorCallback) { if (C.peek() != '$') return None; - return lexName(C, Token, MIToken::ExternalSymbol, - MIToken::QuotedExternalSymbol, - /*PrefixLength=*/1, ErrorCallback); + return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1, + ErrorCallback); +} + +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(); + Token = MIToken(MIToken::FloatingPointLiteral, Range.upto(C)); + 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(); + } + Token = MIToken(MIToken::FloatingPointLiteral, Range.upto(C)); + return C; } -static Cursor maybeLexIntegerLiteral(Cursor C, MIToken &Token) { +static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) { if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1)))) return None; auto Range = C; C.advance(); while (isdigit(C.peek())) C.advance(); + if (C.peek() == '.') + return lexFloatingPointLiteral(Range, C, Token); StringRef StrVal = Range.upto(C); Token = MIToken(MIToken::IntegerLiteral, StrVal, APSInt(StrVal)); return C; @@ -312,17 +398,31 @@ static MIToken::TokenKind symbolToken(char C) { return MIToken::colon; case '!': return MIToken::exclaim; + case '(': + return MIToken::lparen; + case ')': + return MIToken::rparen; + case '+': + return MIToken::plus; + case '-': + return MIToken::minus; default: return MIToken::Error; } } static Cursor maybeLexSymbol(Cursor C, MIToken &Token) { - auto Kind = symbolToken(C.peek()); + MIToken::TokenKind Kind; + unsigned Length = 1; + if (C.peek() == ':' && C.peek(1) == ':') { + Kind = MIToken::coloncolon; + Length = 2; + } else + Kind = symbolToken(C.peek()); if (Kind == MIToken::Error) return None; auto Range = C; - C.advance(); + C.advance(Length); Token = MIToken(Kind, Range.upto(C)); return C; } @@ -336,6 +436,8 @@ StringRef llvm::lexMIToken( return C.remaining(); } + if (Cursor R = maybeLexIntegerType(C, Token)) + return R.remaining(); if (Cursor R = maybeLexIdentifier(C, Token)) return R.remaining(); if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback)) @@ -348,13 +450,19 @@ StringRef llvm::lexMIToken( return R.remaining(); if (Cursor R = maybeLexConstantPoolItem(C, Token)) return R.remaining(); + if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback)) + return R.remaining(); + if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback)) + return R.remaining(); if (Cursor R = maybeLexRegister(C, Token)) return R.remaining(); if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback)) return R.remaining(); if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback)) return R.remaining(); - if (Cursor R = maybeLexIntegerLiteral(C, Token)) + if (Cursor R = maybeLexHexFloatingPointLiteral(C, Token)) + return R.remaining(); + if (Cursor R = maybeLexNumericalLiteral(C, Token)) return R.remaining(); if (Cursor R = maybeLexSymbol(C, Token)) return R.remaining();