2009-06-21 15:19:10 +08:00
|
|
|
//===- AsmLexer.cpp - Lexer for Assembly Files ----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This class implements the lexer for assembly files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-01-22 09:58:08 +08:00
|
|
|
#include "llvm/MC/MCParser/AsmLexer.h"
|
2009-09-05 05:45:34 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SMLoc.h"
|
2010-12-20 04:43:38 +08:00
|
|
|
#include <cctype>
|
2009-06-22 03:21:25 +08:00
|
|
|
#include <cerrno>
|
2009-06-22 14:59:32 +08:00
|
|
|
#include <cstdio>
|
2009-06-22 03:43:50 +08:00
|
|
|
#include <cstdlib>
|
2009-06-21 15:19:10 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2015-03-17 02:06:57 +08:00
|
|
|
AsmLexer::AsmLexer(const MCAsmInfo &MAI) : MAI(MAI) {
|
2014-04-24 14:44:33 +08:00
|
|
|
CurPtr = nullptr;
|
2016-06-18 00:06:17 +08:00
|
|
|
IsAtStartOfLine = true;
|
|
|
|
IsAtStartOfStatement = true;
|
2013-12-07 07:05:33 +08:00
|
|
|
AllowAtInIdentifier = !StringRef(MAI.getCommentString()).startswith("@");
|
2009-06-24 08:33:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AsmLexer::~AsmLexer() {
|
2009-06-21 15:19:10 +08:00
|
|
|
}
|
|
|
|
|
2014-07-06 22:17:29 +08:00
|
|
|
void AsmLexer::setBuffer(StringRef Buf, const char *ptr) {
|
|
|
|
CurBuf = Buf;
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2010-01-21 08:19:58 +08:00
|
|
|
if (ptr)
|
|
|
|
CurPtr = ptr;
|
|
|
|
else
|
2014-07-06 22:17:29 +08:00
|
|
|
CurPtr = CurBuf.begin();
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2014-04-24 14:44:33 +08:00
|
|
|
TokStart = nullptr;
|
2010-01-21 08:19:58 +08:00
|
|
|
}
|
|
|
|
|
2009-06-22 03:21:25 +08:00
|
|
|
/// ReturnError - Set the error to the specified string at the specified
|
2009-07-29 00:08:33 +08:00
|
|
|
/// location. This is defined to always return AsmToken::Error.
|
2009-07-28 11:00:54 +08:00
|
|
|
AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
|
2010-01-21 06:18:24 +08:00
|
|
|
SetError(SMLoc::getFromPointer(Loc), Msg);
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2016-06-03 01:15:05 +08:00
|
|
|
return AsmToken(AsmToken::Error, StringRef(Loc, CurPtr - Loc));
|
2009-06-22 03:21:25 +08:00
|
|
|
}
|
|
|
|
|
2009-06-21 15:19:10 +08:00
|
|
|
int AsmLexer::getNextChar() {
|
2016-06-18 00:06:17 +08:00
|
|
|
if (CurPtr == CurBuf.end())
|
2009-06-21 15:19:10 +08:00
|
|
|
return EOF;
|
2016-06-18 00:06:17 +08:00
|
|
|
return (unsigned char)*CurPtr++;
|
2009-06-21 15:19:10 +08:00
|
|
|
}
|
|
|
|
|
2010-09-28 04:12:52 +08:00
|
|
|
/// LexFloatLiteral: [0-9]*[.][0-9]*([eE][+-]?[0-9]*)?
|
|
|
|
///
|
|
|
|
/// The leading integral digit sequence and dot should have already been
|
|
|
|
/// consumed, some or all of the fractional digit sequence *can* have been
|
|
|
|
/// consumed.
|
|
|
|
AsmToken AsmLexer::LexFloatLiteral() {
|
|
|
|
// Skip the fractional digit sequence.
|
|
|
|
while (isdigit(*CurPtr))
|
|
|
|
++CurPtr;
|
|
|
|
|
|
|
|
// Check for exponent; we intentionally accept a slighlty wider set of
|
|
|
|
// literals here and rely on the upstream client to reject invalid ones (e.g.,
|
|
|
|
// "1e+").
|
|
|
|
if (*CurPtr == 'e' || *CurPtr == 'E') {
|
|
|
|
++CurPtr;
|
|
|
|
if (*CurPtr == '-' || *CurPtr == '+')
|
|
|
|
++CurPtr;
|
|
|
|
while (isdigit(*CurPtr))
|
|
|
|
++CurPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AsmToken(AsmToken::Real,
|
|
|
|
StringRef(TokStart, CurPtr - TokStart));
|
|
|
|
}
|
|
|
|
|
2013-08-14 22:23:31 +08:00
|
|
|
/// LexHexFloatLiteral matches essentially (.[0-9a-fA-F]*)?[pP][+-]?[0-9a-fA-F]+
|
|
|
|
/// while making sure there are enough actual digits around for the constant to
|
|
|
|
/// be valid.
|
|
|
|
///
|
|
|
|
/// The leading "0x[0-9a-fA-F]*" (i.e. integer part) has already been consumed
|
|
|
|
/// before we get here.
|
|
|
|
AsmToken AsmLexer::LexHexFloatLiteral(bool NoIntDigits) {
|
|
|
|
assert((*CurPtr == 'p' || *CurPtr == 'P' || *CurPtr == '.') &&
|
|
|
|
"unexpected parse state in floating hex");
|
|
|
|
bool NoFracDigits = true;
|
|
|
|
|
|
|
|
// Skip the fractional part if there is one
|
|
|
|
if (*CurPtr == '.') {
|
|
|
|
++CurPtr;
|
|
|
|
|
|
|
|
const char *FracStart = CurPtr;
|
|
|
|
while (isxdigit(*CurPtr))
|
|
|
|
++CurPtr;
|
|
|
|
|
|
|
|
NoFracDigits = CurPtr == FracStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NoIntDigits && NoFracDigits)
|
|
|
|
return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
|
|
|
|
"expected at least one significand digit");
|
|
|
|
|
|
|
|
// Make sure we do have some kind of proper exponent part
|
|
|
|
if (*CurPtr != 'p' && *CurPtr != 'P')
|
|
|
|
return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
|
|
|
|
"expected exponent part 'p'");
|
|
|
|
++CurPtr;
|
|
|
|
|
|
|
|
if (*CurPtr == '+' || *CurPtr == '-')
|
|
|
|
++CurPtr;
|
|
|
|
|
|
|
|
// N.b. exponent digits are *not* hex
|
|
|
|
const char *ExpStart = CurPtr;
|
|
|
|
while (isdigit(*CurPtr))
|
|
|
|
++CurPtr;
|
|
|
|
|
|
|
|
if (CurPtr == ExpStart)
|
|
|
|
return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
|
|
|
|
"expected at least one exponent digit");
|
|
|
|
|
|
|
|
return AsmToken(AsmToken::Real, StringRef(TokStart, CurPtr - TokStart));
|
|
|
|
}
|
|
|
|
|
2013-10-19 04:46:28 +08:00
|
|
|
/// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@?]*
|
2013-12-07 04:35:58 +08:00
|
|
|
static bool IsIdentifierChar(char c, bool AllowAt) {
|
|
|
|
return isalnum(c) || c == '_' || c == '$' || c == '.' ||
|
|
|
|
(c == '@' && AllowAt) || c == '?';
|
2010-09-24 09:59:31 +08:00
|
|
|
}
|
2009-07-28 11:00:54 +08:00
|
|
|
AsmToken AsmLexer::LexIdentifier() {
|
2010-09-24 09:59:31 +08:00
|
|
|
// Check for floating point literals.
|
|
|
|
if (CurPtr[-1] == '.' && isdigit(*CurPtr)) {
|
2010-09-28 04:12:52 +08:00
|
|
|
// Disambiguate a .1243foo identifier from a floating literal.
|
2010-09-24 09:59:31 +08:00
|
|
|
while (isdigit(*CurPtr))
|
|
|
|
++CurPtr;
|
2013-12-07 04:35:58 +08:00
|
|
|
if (*CurPtr == 'e' || *CurPtr == 'E' ||
|
|
|
|
!IsIdentifierChar(*CurPtr, AllowAtInIdentifier))
|
2010-09-28 04:12:52 +08:00
|
|
|
return LexFloatLiteral();
|
2010-09-24 09:59:31 +08:00
|
|
|
}
|
|
|
|
|
2013-12-07 04:35:58 +08:00
|
|
|
while (IsIdentifierChar(*CurPtr, AllowAtInIdentifier))
|
2009-06-22 03:21:25 +08:00
|
|
|
++CurPtr;
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2010-04-14 12:40:28 +08:00
|
|
|
// Handle . as a special case.
|
2010-05-06 22:46:38 +08:00
|
|
|
if (CurPtr == TokStart+1 && TokStart[0] == '.')
|
|
|
|
return AsmToken(AsmToken::Dot, StringRef(TokStart, 1));
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2009-07-29 00:08:33 +08:00
|
|
|
return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
|
2009-06-22 03:21:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// LexSlash: Slash: /
|
|
|
|
/// C-Style Comment: /* ... */
|
2009-07-28 11:00:54 +08:00
|
|
|
AsmToken AsmLexer::LexSlash() {
|
2009-06-30 05:58:22 +08:00
|
|
|
switch (*CurPtr) {
|
2016-02-19 06:09:30 +08:00
|
|
|
case '*':
|
2016-06-18 00:06:17 +08:00
|
|
|
IsAtStartOfStatement = false;
|
2016-02-19 06:09:30 +08:00
|
|
|
break; // C style comment.
|
|
|
|
case '/':
|
|
|
|
++CurPtr;
|
|
|
|
return LexLineComment();
|
|
|
|
default:
|
2016-06-18 00:06:17 +08:00
|
|
|
IsAtStartOfStatement = false;
|
|
|
|
return AsmToken(AsmToken::Slash, StringRef(TokStart, 1));
|
2009-06-30 05:58:22 +08:00
|
|
|
}
|
2009-06-22 03:21:25 +08:00
|
|
|
|
|
|
|
// C Style comment.
|
|
|
|
++CurPtr; // skip the star.
|
2016-06-18 00:06:17 +08:00
|
|
|
while (CurPtr != CurBuf.end()) {
|
|
|
|
switch (*CurPtr++) {
|
2009-06-22 03:21:25 +08:00
|
|
|
case '*':
|
|
|
|
// End of the comment?
|
2016-06-18 00:06:17 +08:00
|
|
|
if (*CurPtr != '/')
|
|
|
|
break;
|
2009-06-22 03:21:25 +08:00
|
|
|
++CurPtr; // End the */.
|
2016-06-18 00:06:17 +08:00
|
|
|
return AsmToken(AsmToken::Comment,
|
|
|
|
StringRef(TokStart, CurPtr - TokStart));
|
2009-06-22 03:21:25 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-18 00:06:17 +08:00
|
|
|
return ReturnError(TokStart, "unterminated comment");
|
2009-06-22 03:21:25 +08:00
|
|
|
}
|
|
|
|
|
2009-06-30 05:58:22 +08:00
|
|
|
/// LexLineComment: Comment: #[^\n]*
|
|
|
|
/// : //[^\n]*
|
2009-07-28 11:00:54 +08:00
|
|
|
AsmToken AsmLexer::LexLineComment() {
|
2016-06-18 00:06:17 +08:00
|
|
|
// Mark This as an end of statement with a body of the
|
|
|
|
// comment. While it would be nicer to leave this two tokens,
|
|
|
|
// backwards compatability with TargetParsers makes keeping this in this form
|
|
|
|
// better.
|
2009-06-22 03:21:25 +08:00
|
|
|
int CurChar = getNextChar();
|
2011-08-05 03:31:26 +08:00
|
|
|
while (CurChar != '\n' && CurChar != '\r' && CurChar != EOF)
|
2009-06-22 03:21:25 +08:00
|
|
|
CurChar = getNextChar();
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2016-06-18 00:06:17 +08:00
|
|
|
IsAtStartOfLine = true;
|
|
|
|
// Whis is a whole line comment. leave newline
|
|
|
|
if (IsAtStartOfStatement)
|
|
|
|
return AsmToken(AsmToken::EndOfStatement,
|
|
|
|
StringRef(TokStart, CurPtr - TokStart));
|
|
|
|
IsAtStartOfStatement = true;
|
|
|
|
|
|
|
|
return AsmToken(AsmToken::EndOfStatement,
|
|
|
|
StringRef(TokStart, CurPtr - 1 - TokStart));
|
2009-06-22 03:21:25 +08:00
|
|
|
}
|
|
|
|
|
2010-08-24 08:43:25 +08:00
|
|
|
static void SkipIgnoredIntegerSuffix(const char *&CurPtr) {
|
2013-02-27 04:17:10 +08:00
|
|
|
// Skip ULL, UL, U, L and LL suffices.
|
|
|
|
if (CurPtr[0] == 'U')
|
|
|
|
++CurPtr;
|
|
|
|
if (CurPtr[0] == 'L')
|
|
|
|
++CurPtr;
|
|
|
|
if (CurPtr[0] == 'L')
|
|
|
|
++CurPtr;
|
2010-08-24 08:43:25 +08:00
|
|
|
}
|
|
|
|
|
2013-02-12 09:00:01 +08:00
|
|
|
// Look ahead to search for first non-hex digit, if it's [hH], then we treat the
|
|
|
|
// integer as a hexadecimal, possibly with leading zeroes.
|
|
|
|
static unsigned doLookAhead(const char *&CurPtr, unsigned DefaultRadix) {
|
2014-04-24 14:44:33 +08:00
|
|
|
const char *FirstHex = nullptr;
|
2013-02-12 09:00:01 +08:00
|
|
|
const char *LookAhead = CurPtr;
|
|
|
|
while (1) {
|
|
|
|
if (isdigit(*LookAhead)) {
|
|
|
|
++LookAhead;
|
|
|
|
} else if (isxdigit(*LookAhead)) {
|
|
|
|
if (!FirstHex)
|
|
|
|
FirstHex = LookAhead;
|
|
|
|
++LookAhead;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool isHex = *LookAhead == 'h' || *LookAhead == 'H';
|
2013-02-15 00:23:08 +08:00
|
|
|
CurPtr = isHex || !FirstHex ? LookAhead : FirstHex;
|
2013-02-12 09:00:01 +08:00
|
|
|
if (isHex)
|
|
|
|
return 16;
|
|
|
|
return DefaultRadix;
|
|
|
|
}
|
|
|
|
|
2014-02-02 00:20:54 +08:00
|
|
|
static AsmToken intToken(StringRef Ref, APInt &Value)
|
|
|
|
{
|
|
|
|
if (Value.isIntN(64))
|
|
|
|
return AsmToken(AsmToken::Integer, Ref, Value);
|
|
|
|
return AsmToken(AsmToken::BigNum, Ref, Value);
|
|
|
|
}
|
|
|
|
|
2009-06-22 03:21:25 +08:00
|
|
|
/// LexDigit: First character is [0-9].
|
|
|
|
/// Local Label: [0-9][:]
|
2013-02-15 00:23:08 +08:00
|
|
|
/// Forward/Backward Label: [0-9][fb]
|
|
|
|
/// Binary integer: 0b[01]+
|
2009-06-22 03:21:25 +08:00
|
|
|
/// Octal integer: 0[0-7]+
|
2013-02-12 09:00:01 +08:00
|
|
|
/// Hex integer: 0x[0-9a-fA-F]+ or [0x]?[0-9][0-9a-fA-F]*[hH]
|
2009-06-22 03:21:25 +08:00
|
|
|
/// Decimal integer: [1-9][0-9]*
|
2009-07-28 11:00:54 +08:00
|
|
|
AsmToken AsmLexer::LexDigit() {
|
2009-06-22 03:21:25 +08:00
|
|
|
// Decimal integer: [1-9][0-9]*
|
2010-09-25 01:10:26 +08:00
|
|
|
if (CurPtr[-1] != '0' || CurPtr[0] == '.') {
|
2013-02-12 09:00:01 +08:00
|
|
|
unsigned Radix = doLookAhead(CurPtr, 10);
|
2013-02-15 00:23:08 +08:00
|
|
|
bool isHex = Radix == 16;
|
2010-09-24 09:59:31 +08:00
|
|
|
// Check for floating point literals.
|
2013-02-15 00:23:08 +08:00
|
|
|
if (!isHex && (*CurPtr == '.' || *CurPtr == 'e')) {
|
2010-09-24 09:59:31 +08:00
|
|
|
++CurPtr;
|
2010-09-28 04:12:52 +08:00
|
|
|
return LexFloatLiteral();
|
2010-09-24 09:59:31 +08:00
|
|
|
}
|
|
|
|
|
2010-01-22 15:34:12 +08:00
|
|
|
StringRef Result(TokStart, CurPtr - TokStart);
|
2010-08-24 08:43:25 +08:00
|
|
|
|
2014-02-02 00:20:54 +08:00
|
|
|
APInt Value(128, 0, true);
|
|
|
|
if (Result.getAsInteger(Radix, Value))
|
|
|
|
return ReturnError(TokStart, !isHex ? "invalid decimal number" :
|
2013-02-12 09:00:01 +08:00
|
|
|
"invalid hexdecimal number");
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2013-02-12 09:00:01 +08:00
|
|
|
// Consume the [bB][hH].
|
|
|
|
if (Radix == 2 || Radix == 16)
|
|
|
|
++CurPtr;
|
|
|
|
|
2013-02-27 04:17:10 +08:00
|
|
|
// The darwin/x86 (and x86-64) assembler accepts and ignores type
|
|
|
|
// suffices on integer literals.
|
2010-08-24 08:43:25 +08:00
|
|
|
SkipIgnoredIntegerSuffix(CurPtr);
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2014-02-02 00:20:54 +08:00
|
|
|
return intToken(Result, Value);
|
2009-06-22 03:21:25 +08:00
|
|
|
}
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2016-03-19 02:22:07 +08:00
|
|
|
if ((*CurPtr == 'b') || (*CurPtr == 'B')) {
|
2013-02-15 00:23:08 +08:00
|
|
|
++CurPtr;
|
|
|
|
// See if we actually have "0b" as part of something like "jmp 0b\n"
|
|
|
|
if (!isdigit(CurPtr[0])) {
|
|
|
|
--CurPtr;
|
|
|
|
StringRef Result(TokStart, CurPtr - TokStart);
|
|
|
|
return AsmToken(AsmToken::Integer, Result, 0);
|
|
|
|
}
|
|
|
|
const char *NumStart = CurPtr;
|
2009-06-22 03:21:25 +08:00
|
|
|
while (CurPtr[0] == '0' || CurPtr[0] == '1')
|
|
|
|
++CurPtr;
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2009-06-22 03:21:25 +08:00
|
|
|
// Requires at least one binary digit.
|
|
|
|
if (CurPtr == NumStart)
|
2011-04-12 08:18:03 +08:00
|
|
|
return ReturnError(TokStart, "invalid binary number");
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2010-01-22 15:34:12 +08:00
|
|
|
StringRef Result(TokStart, CurPtr - TokStart);
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2014-02-02 00:20:54 +08:00
|
|
|
APInt Value(128, 0, true);
|
2010-08-24 08:43:25 +08:00
|
|
|
if (Result.substr(2).getAsInteger(2, Value))
|
2011-04-12 08:18:03 +08:00
|
|
|
return ReturnError(TokStart, "invalid binary number");
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2010-08-24 08:43:25 +08:00
|
|
|
// The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
|
|
|
|
// suffixes on integer literals.
|
|
|
|
SkipIgnoredIntegerSuffix(CurPtr);
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2014-02-02 00:20:54 +08:00
|
|
|
return intToken(Result, Value);
|
2009-06-22 03:21:25 +08:00
|
|
|
}
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2016-03-19 02:22:07 +08:00
|
|
|
if ((*CurPtr == 'x') || (*CurPtr == 'X')) {
|
2009-06-22 03:21:25 +08:00
|
|
|
++CurPtr;
|
|
|
|
const char *NumStart = CurPtr;
|
|
|
|
while (isxdigit(CurPtr[0]))
|
|
|
|
++CurPtr;
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2013-08-14 22:23:31 +08:00
|
|
|
// "0x.0p0" is valid, and "0x0p0" (but not "0xp0" for example, which will be
|
|
|
|
// diagnosed by LexHexFloatLiteral).
|
|
|
|
if (CurPtr[0] == '.' || CurPtr[0] == 'p' || CurPtr[0] == 'P')
|
|
|
|
return LexHexFloatLiteral(NumStart == CurPtr);
|
|
|
|
|
|
|
|
// Otherwise requires at least one hex digit.
|
2009-06-22 03:21:25 +08:00
|
|
|
if (CurPtr == NumStart)
|
2011-04-12 08:18:03 +08:00
|
|
|
return ReturnError(CurPtr-2, "invalid hexadecimal number");
|
2010-01-22 09:17:12 +08:00
|
|
|
|
2014-02-02 00:20:54 +08:00
|
|
|
APInt Result(128, 0);
|
2010-01-22 09:17:12 +08:00
|
|
|
if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
|
2011-04-12 08:18:03 +08:00
|
|
|
return ReturnError(TokStart, "invalid hexadecimal number");
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2013-02-12 09:00:01 +08:00
|
|
|
// Consume the optional [hH].
|
|
|
|
if (*CurPtr == 'h' || *CurPtr == 'H')
|
|
|
|
++CurPtr;
|
|
|
|
|
2010-08-24 08:43:25 +08:00
|
|
|
// The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
|
|
|
|
// suffixes on integer literals.
|
|
|
|
SkipIgnoredIntegerSuffix(CurPtr);
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2014-02-02 00:20:54 +08:00
|
|
|
return intToken(StringRef(TokStart, CurPtr - TokStart), Result);
|
2009-06-22 03:21:25 +08:00
|
|
|
}
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2013-02-26 02:11:18 +08:00
|
|
|
// Either octal or hexadecimal.
|
2014-02-02 00:20:54 +08:00
|
|
|
APInt Value(128, 0, true);
|
2013-02-12 09:00:01 +08:00
|
|
|
unsigned Radix = doLookAhead(CurPtr, 8);
|
2013-02-15 00:23:08 +08:00
|
|
|
bool isHex = Radix == 16;
|
2013-02-12 09:00:01 +08:00
|
|
|
StringRef Result(TokStart, CurPtr - TokStart);
|
|
|
|
if (Result.getAsInteger(Radix, Value))
|
2013-02-15 00:23:08 +08:00
|
|
|
return ReturnError(TokStart, !isHex ? "invalid octal number" :
|
2013-02-12 09:12:24 +08:00
|
|
|
"invalid hexdecimal number");
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2013-02-15 00:23:08 +08:00
|
|
|
// Consume the [hH].
|
|
|
|
if (Radix == 16)
|
2013-02-12 09:00:01 +08:00
|
|
|
++CurPtr;
|
|
|
|
|
2010-08-24 08:43:25 +08:00
|
|
|
// The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
|
|
|
|
// suffixes on integer literals.
|
|
|
|
SkipIgnoredIntegerSuffix(CurPtr);
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2014-02-02 00:20:54 +08:00
|
|
|
return intToken(Result, Value);
|
2009-06-22 03:21:25 +08:00
|
|
|
}
|
|
|
|
|
2010-12-18 16:56:37 +08:00
|
|
|
/// LexSingleQuote: Integer: 'b'
|
|
|
|
AsmToken AsmLexer::LexSingleQuote() {
|
|
|
|
int CurChar = getNextChar();
|
|
|
|
|
|
|
|
if (CurChar == '\\')
|
|
|
|
CurChar = getNextChar();
|
|
|
|
|
|
|
|
if (CurChar == EOF)
|
|
|
|
return ReturnError(TokStart, "unterminated single quote");
|
|
|
|
|
|
|
|
CurChar = getNextChar();
|
|
|
|
|
|
|
|
if (CurChar != '\'')
|
|
|
|
return ReturnError(TokStart, "single quote way too long");
|
|
|
|
|
|
|
|
// The idea here being that 'c' is basically just an integral
|
|
|
|
// constant.
|
|
|
|
StringRef Res = StringRef(TokStart,CurPtr - TokStart);
|
|
|
|
long long Value;
|
|
|
|
|
|
|
|
if (Res.startswith("\'\\")) {
|
|
|
|
char theChar = Res[2];
|
|
|
|
switch (theChar) {
|
|
|
|
default: Value = theChar; break;
|
|
|
|
case '\'': Value = '\''; break;
|
|
|
|
case 't': Value = '\t'; break;
|
|
|
|
case 'n': Value = '\n'; break;
|
|
|
|
case 'b': Value = '\b'; break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
Value = TokStart[1];
|
|
|
|
|
2011-02-12 03:05:56 +08:00
|
|
|
return AsmToken(AsmToken::Integer, Res, Value);
|
2010-12-18 16:56:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-22 03:56:35 +08:00
|
|
|
/// LexQuote: String: "..."
|
2009-07-28 11:00:54 +08:00
|
|
|
AsmToken AsmLexer::LexQuote() {
|
2009-06-22 03:56:35 +08:00
|
|
|
int CurChar = getNextChar();
|
|
|
|
// TODO: does gas allow multiline string constants?
|
|
|
|
while (CurChar != '"') {
|
|
|
|
if (CurChar == '\\') {
|
|
|
|
// Allow \", etc.
|
|
|
|
CurChar = getNextChar();
|
|
|
|
}
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2009-06-22 05:22:11 +08:00
|
|
|
if (CurChar == EOF)
|
|
|
|
return ReturnError(TokStart, "unterminated string constant");
|
2009-06-22 03:56:35 +08:00
|
|
|
|
|
|
|
CurChar = getNextChar();
|
|
|
|
}
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2009-07-29 00:08:33 +08:00
|
|
|
return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
|
2009-06-22 03:56:35 +08:00
|
|
|
}
|
|
|
|
|
2009-08-10 09:39:42 +08:00
|
|
|
StringRef AsmLexer::LexUntilEndOfStatement() {
|
|
|
|
TokStart = CurPtr;
|
|
|
|
|
2014-08-14 10:51:43 +08:00
|
|
|
while (!isAtStartOfComment(CurPtr) && // Start of line comment.
|
2011-03-25 02:46:34 +08:00
|
|
|
!isAtStatementSeparator(CurPtr) && // End of statement marker.
|
2016-06-18 00:06:17 +08:00
|
|
|
*CurPtr != '\n' && *CurPtr != '\r' && CurPtr != CurBuf.end()) {
|
2009-08-10 09:39:42 +08:00
|
|
|
++CurPtr;
|
2009-09-05 05:45:34 +08:00
|
|
|
}
|
2009-08-10 09:39:42 +08:00
|
|
|
return StringRef(TokStart, CurPtr-TokStart);
|
|
|
|
}
|
2009-06-22 03:21:25 +08:00
|
|
|
|
2011-09-14 07:45:18 +08:00
|
|
|
StringRef AsmLexer::LexUntilEndOfLine() {
|
|
|
|
TokStart = CurPtr;
|
|
|
|
|
2016-06-18 00:06:17 +08:00
|
|
|
while (*CurPtr != '\n' && *CurPtr != '\r' && CurPtr != CurBuf.end()) {
|
2011-09-14 07:45:18 +08:00
|
|
|
++CurPtr;
|
|
|
|
}
|
|
|
|
return StringRef(TokStart, CurPtr-TokStart);
|
|
|
|
}
|
|
|
|
|
2015-08-17 22:35:25 +08:00
|
|
|
size_t AsmLexer::peekTokens(MutableArrayRef<AsmToken> Buf,
|
|
|
|
bool ShouldSkipSpace) {
|
2014-02-10 07:29:24 +08:00
|
|
|
const char *SavedTokStart = TokStart;
|
|
|
|
const char *SavedCurPtr = CurPtr;
|
2016-06-18 00:06:17 +08:00
|
|
|
bool SavedAtStartOfLine = IsAtStartOfLine;
|
|
|
|
bool SavedAtStartOfStatement = IsAtStartOfStatement;
|
2014-02-10 07:29:24 +08:00
|
|
|
bool SavedSkipSpace = SkipSpace;
|
|
|
|
|
|
|
|
std::string SavedErr = getErr();
|
|
|
|
SMLoc SavedErrLoc = getErrLoc();
|
|
|
|
|
|
|
|
SkipSpace = ShouldSkipSpace;
|
2015-08-17 22:35:25 +08:00
|
|
|
|
|
|
|
size_t ReadCount;
|
|
|
|
for (ReadCount = 0; ReadCount < Buf.size(); ++ReadCount) {
|
|
|
|
AsmToken Token = LexToken();
|
|
|
|
|
|
|
|
Buf[ReadCount] = Token;
|
|
|
|
|
|
|
|
if (Token.is(AsmToken::Eof))
|
|
|
|
break;
|
|
|
|
}
|
2014-02-10 07:29:24 +08:00
|
|
|
|
|
|
|
SetError(SavedErrLoc, SavedErr);
|
|
|
|
|
|
|
|
SkipSpace = SavedSkipSpace;
|
2016-06-18 00:06:17 +08:00
|
|
|
IsAtStartOfLine = SavedAtStartOfLine;
|
|
|
|
IsAtStartOfStatement = SavedAtStartOfStatement;
|
2014-02-10 07:29:24 +08:00
|
|
|
CurPtr = SavedCurPtr;
|
|
|
|
TokStart = SavedTokStart;
|
|
|
|
|
2015-08-17 22:35:25 +08:00
|
|
|
return ReadCount;
|
2014-02-10 07:29:24 +08:00
|
|
|
}
|
|
|
|
|
2014-08-14 10:51:43 +08:00
|
|
|
bool AsmLexer::isAtStartOfComment(const char *Ptr) {
|
|
|
|
const char *CommentString = MAI.getCommentString();
|
|
|
|
|
|
|
|
if (CommentString[1] == '\0')
|
|
|
|
return CommentString[0] == Ptr[0];
|
|
|
|
|
|
|
|
// FIXME: special case for the bogus "##" comment string in X86MCAsmInfoDarwin
|
|
|
|
if (CommentString[1] == '#')
|
|
|
|
return CommentString[0] == Ptr[0];
|
|
|
|
|
|
|
|
return strncmp(Ptr, CommentString, strlen(CommentString)) == 0;
|
2009-09-17 02:08:00 +08:00
|
|
|
}
|
|
|
|
|
2011-03-25 02:46:34 +08:00
|
|
|
bool AsmLexer::isAtStatementSeparator(const char *Ptr) {
|
|
|
|
return strncmp(Ptr, MAI.getSeparatorString(),
|
|
|
|
strlen(MAI.getSeparatorString())) == 0;
|
|
|
|
}
|
|
|
|
|
2009-07-28 11:00:54 +08:00
|
|
|
AsmToken AsmLexer::LexToken() {
|
2009-06-21 15:19:10 +08:00
|
|
|
TokStart = CurPtr;
|
|
|
|
// This always consumes at least one character.
|
|
|
|
int CurChar = getNextChar();
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2016-06-18 00:06:17 +08:00
|
|
|
if (CurChar == '#' && IsAtStartOfStatement) {
|
|
|
|
// If this starts with a '#', this may be a cpp
|
|
|
|
// hash directive and otherwise a line comment.
|
|
|
|
AsmToken TokenBuf[2];
|
|
|
|
MutableArrayRef<AsmToken> Buf(TokenBuf, 2);
|
|
|
|
size_t num = peekTokens(Buf, true);
|
|
|
|
// There cannot be a space preceeding this
|
|
|
|
if (IsAtStartOfLine && num == 2 && TokenBuf[0].is(AsmToken::Integer) &&
|
|
|
|
TokenBuf[1].is(AsmToken::String)) {
|
|
|
|
CurPtr = TokStart; // reset curPtr;
|
|
|
|
StringRef s = LexUntilEndOfLine();
|
|
|
|
UnLex(TokenBuf[1]);
|
|
|
|
UnLex(TokenBuf[0]);
|
|
|
|
return AsmToken(AsmToken::HashDirective, s);
|
|
|
|
}
|
2009-09-17 02:08:00 +08:00
|
|
|
return LexLineComment();
|
2011-09-14 07:45:18 +08:00
|
|
|
}
|
2016-06-18 00:06:17 +08:00
|
|
|
|
|
|
|
if (isAtStartOfComment(TokStart))
|
|
|
|
return LexLineComment();
|
|
|
|
|
2011-03-25 02:46:34 +08:00
|
|
|
if (isAtStatementSeparator(TokStart)) {
|
|
|
|
CurPtr += strlen(MAI.getSeparatorString()) - 1;
|
2016-06-18 00:06:17 +08:00
|
|
|
IsAtStartOfLine = true;
|
|
|
|
IsAtStartOfStatement = true;
|
2011-03-25 02:46:34 +08:00
|
|
|
return AsmToken(AsmToken::EndOfStatement,
|
|
|
|
StringRef(TokStart, strlen(MAI.getSeparatorString())));
|
|
|
|
}
|
2009-09-05 05:45:34 +08:00
|
|
|
|
2011-09-16 00:52:06 +08:00
|
|
|
// If we're missing a newline at EOF, make sure we still get an
|
|
|
|
// EndOfStatement token before the Eof token.
|
2016-06-18 00:06:17 +08:00
|
|
|
if (CurChar == EOF && !IsAtStartOfStatement) {
|
|
|
|
IsAtStartOfLine = true;
|
|
|
|
IsAtStartOfStatement = true;
|
2011-09-16 00:52:06 +08:00
|
|
|
return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
|
|
|
|
}
|
2016-06-18 00:06:17 +08:00
|
|
|
IsAtStartOfLine = false;
|
|
|
|
bool OldIsAtStartOfStatement = IsAtStartOfStatement;
|
|
|
|
IsAtStartOfStatement = false;
|
2009-06-21 15:19:10 +08:00
|
|
|
switch (CurChar) {
|
|
|
|
default:
|
2010-05-06 22:46:38 +08:00
|
|
|
// Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
|
|
|
|
if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
|
2009-06-22 03:21:25 +08:00
|
|
|
return LexIdentifier();
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2009-06-21 15:19:10 +08:00
|
|
|
// Unknown character, emit an error.
|
2009-06-22 04:16:42 +08:00
|
|
|
return ReturnError(TokStart, "invalid character in input");
|
2016-06-18 00:06:17 +08:00
|
|
|
case EOF:
|
|
|
|
IsAtStartOfLine = true;
|
|
|
|
IsAtStartOfStatement = true;
|
|
|
|
return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
|
2009-06-21 15:19:10 +08:00
|
|
|
case 0:
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
2016-06-18 00:06:17 +08:00
|
|
|
IsAtStartOfStatement = OldIsAtStartOfStatement;
|
|
|
|
while (*CurPtr == ' ' || *CurPtr == '\t')
|
|
|
|
CurPtr++;
|
|
|
|
if (SkipSpace)
|
|
|
|
return LexToken(); // Ignore whitespace.
|
|
|
|
else
|
|
|
|
return AsmToken(AsmToken::Space, StringRef(TokStart, CurPtr - TokStart));
|
|
|
|
case '\n':
|
2011-03-25 02:46:34 +08:00
|
|
|
case '\r':
|
2016-06-18 00:06:17 +08:00
|
|
|
IsAtStartOfLine = true;
|
|
|
|
IsAtStartOfStatement = true;
|
2011-03-25 02:46:34 +08:00
|
|
|
return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
|
2009-07-29 00:08:33 +08:00
|
|
|
case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
|
|
|
|
case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
|
|
|
|
case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
|
|
|
|
case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
|
|
|
|
case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
|
|
|
|
case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
|
2009-09-05 06:40:31 +08:00
|
|
|
case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
|
|
|
|
case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
|
|
|
|
case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
|
|
|
|
case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
|
2009-07-29 00:08:33 +08:00
|
|
|
case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
|
|
|
|
case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
|
2010-05-06 22:46:38 +08:00
|
|
|
case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
|
2010-05-21 19:36:59 +08:00
|
|
|
case '@': return AsmToken(AsmToken::At, StringRef(TokStart, 1));
|
2011-06-05 10:43:45 +08:00
|
|
|
case '\\': return AsmToken(AsmToken::BackSlash, StringRef(TokStart, 1));
|
2011-02-12 03:05:56 +08:00
|
|
|
case '=':
|
2016-02-19 06:09:30 +08:00
|
|
|
if (*CurPtr == '=') {
|
|
|
|
++CurPtr;
|
|
|
|
return AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
|
|
|
|
}
|
2009-07-29 00:08:33 +08:00
|
|
|
return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
|
2011-02-12 03:05:56 +08:00
|
|
|
case '|':
|
2016-02-19 06:09:30 +08:00
|
|
|
if (*CurPtr == '|') {
|
|
|
|
++CurPtr;
|
|
|
|
return AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
|
|
|
|
}
|
2009-07-29 00:08:33 +08:00
|
|
|
return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
|
|
|
|
case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
|
2011-02-12 03:05:56 +08:00
|
|
|
case '&':
|
2016-02-19 06:09:30 +08:00
|
|
|
if (*CurPtr == '&') {
|
|
|
|
++CurPtr;
|
|
|
|
return AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
|
|
|
|
}
|
2009-07-29 00:08:33 +08:00
|
|
|
return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
|
2011-02-12 03:05:56 +08:00
|
|
|
case '!':
|
2016-02-19 06:09:30 +08:00
|
|
|
if (*CurPtr == '=') {
|
|
|
|
++CurPtr;
|
|
|
|
return AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
|
|
|
|
}
|
2009-07-29 00:08:33 +08:00
|
|
|
return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
|
2009-09-04 01:15:07 +08:00
|
|
|
case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
|
2016-06-18 00:06:17 +08:00
|
|
|
case '/':
|
|
|
|
IsAtStartOfStatement = OldIsAtStartOfStatement;
|
|
|
|
return LexSlash();
|
2009-09-05 05:45:34 +08:00
|
|
|
case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
|
2010-12-18 16:56:37 +08:00
|
|
|
case '\'': return LexSingleQuote();
|
2009-06-22 03:56:35 +08:00
|
|
|
case '"': return LexQuote();
|
2009-06-22 03:21:25 +08:00
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
return LexDigit();
|
2009-06-23 13:57:07 +08:00
|
|
|
case '<':
|
2009-06-30 04:37:27 +08:00
|
|
|
switch (*CurPtr) {
|
2016-02-19 06:09:30 +08:00
|
|
|
case '<':
|
|
|
|
++CurPtr;
|
|
|
|
return AsmToken(AsmToken::LessLess, StringRef(TokStart, 2));
|
|
|
|
case '=':
|
|
|
|
++CurPtr;
|
|
|
|
return AsmToken(AsmToken::LessEqual, StringRef(TokStart, 2));
|
|
|
|
case '>':
|
|
|
|
++CurPtr;
|
|
|
|
return AsmToken(AsmToken::LessGreater, StringRef(TokStart, 2));
|
|
|
|
default:
|
|
|
|
return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
|
2009-06-23 13:57:07 +08:00
|
|
|
}
|
|
|
|
case '>':
|
2009-06-30 04:37:27 +08:00
|
|
|
switch (*CurPtr) {
|
2016-02-19 06:09:30 +08:00
|
|
|
case '>':
|
|
|
|
++CurPtr;
|
|
|
|
return AsmToken(AsmToken::GreaterGreater, StringRef(TokStart, 2));
|
|
|
|
case '=':
|
|
|
|
++CurPtr;
|
|
|
|
return AsmToken(AsmToken::GreaterEqual, StringRef(TokStart, 2));
|
|
|
|
default:
|
|
|
|
return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
|
2009-06-23 13:57:07 +08:00
|
|
|
}
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2009-06-22 03:21:25 +08:00
|
|
|
// TODO: Quoted identifiers (objc methods etc)
|
|
|
|
// local labels: [0-9][:]
|
|
|
|
// Forward/backward labels: [0-9][fb]
|
|
|
|
// Integers, fp constants, character constants.
|
2009-06-21 15:19:10 +08:00
|
|
|
}
|
2009-06-22 14:59:32 +08:00
|
|
|
}
|