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;
|
|
|
|
|
2010-01-21 08:19:58 +08:00
|
|
|
AsmLexer::AsmLexer(const MCAsmInfo &_MAI) : MAI(_MAI) {
|
2014-04-24 14:44:33 +08:00
|
|
|
CurPtr = nullptr;
|
2011-09-15 00:37:04 +08:00
|
|
|
isAtStartOfLine = 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
|
|
|
|
2009-07-29 00:08:33 +08:00
|
|
|
return AsmToken(AsmToken::Error, StringRef(Loc, 0));
|
2009-06-22 03:21:25 +08:00
|
|
|
}
|
|
|
|
|
2009-06-21 15:19:10 +08:00
|
|
|
int AsmLexer::getNextChar() {
|
|
|
|
char CurChar = *CurPtr++;
|
|
|
|
switch (CurChar) {
|
|
|
|
default:
|
|
|
|
return (unsigned char)CurChar;
|
2010-01-21 08:19:58 +08:00
|
|
|
case 0:
|
2009-06-21 15:19:10 +08:00
|
|
|
// A nul character in the stream is either the end of the current buffer or
|
|
|
|
// a random nul in the file. Disambiguate that here.
|
2014-07-06 22:17:29 +08:00
|
|
|
if (CurPtr - 1 != CurBuf.end())
|
2009-06-21 15:19:10 +08:00
|
|
|
return 0; // Just whitespace.
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2009-06-21 15:19:10 +08:00
|
|
|
// Otherwise, return end of file.
|
2011-02-12 03:05:56 +08:00
|
|
|
--CurPtr; // Another call to lex will return EOF again.
|
2009-06-21 15:19:10 +08:00
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
case '*': break; // C style comment.
|
|
|
|
case '/': return ++CurPtr, LexLineComment();
|
2010-10-26 04:18:53 +08:00
|
|
|
default: return AsmToken(AsmToken::Slash, StringRef(CurPtr-1, 1));
|
2009-06-30 05:58:22 +08:00
|
|
|
}
|
2009-06-22 03:21:25 +08:00
|
|
|
|
|
|
|
// C Style comment.
|
|
|
|
++CurPtr; // skip the star.
|
|
|
|
while (1) {
|
|
|
|
int CurChar = getNextChar();
|
|
|
|
switch (CurChar) {
|
|
|
|
case EOF:
|
2009-06-22 04:16:42 +08:00
|
|
|
return ReturnError(TokStart, "unterminated comment");
|
2009-06-22 03:21:25 +08:00
|
|
|
case '*':
|
|
|
|
// End of the comment?
|
|
|
|
if (CurPtr[0] != '/') break;
|
2011-02-12 03:05:56 +08:00
|
|
|
|
2009-06-22 03:21:25 +08:00
|
|
|
++CurPtr; // End the */.
|
|
|
|
return LexToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-30 05:58:22 +08:00
|
|
|
/// LexLineComment: Comment: #[^\n]*
|
|
|
|
/// : //[^\n]*
|
2009-07-28 11:00:54 +08:00
|
|
|
AsmToken AsmLexer::LexLineComment() {
|
|
|
|
// FIXME: This is broken if we happen to a comment at the end of a file, which
|
|
|
|
// was .included, and which doesn't end with a newline.
|
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
|
|
|
|
2009-06-22 03:21:25 +08:00
|
|
|
if (CurChar == EOF)
|
2014-06-19 04:57:32 +08:00
|
|
|
return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
|
|
|
|
return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 0));
|
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
|
|
|
|
2009-06-22 03:21:25 +08:00
|
|
|
if (*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
|
|
|
|
2009-06-22 03:21:25 +08:00
|
|
|
if (*CurPtr == 'x') {
|
|
|
|
++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.
|
2014-07-06 22:17:29 +08:00
|
|
|
*CurPtr != '\n' && *CurPtr != '\r' &&
|
|
|
|
(*CurPtr != 0 || 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;
|
|
|
|
|
2014-07-06 22:17:29 +08:00
|
|
|
while (*CurPtr != '\n' && *CurPtr != '\r' &&
|
|
|
|
(*CurPtr != 0 || CurPtr != CurBuf.end())) {
|
2011-09-14 07:45:18 +08:00
|
|
|
++CurPtr;
|
|
|
|
}
|
|
|
|
return StringRef(TokStart, CurPtr-TokStart);
|
|
|
|
}
|
|
|
|
|
2014-02-10 07:29:24 +08:00
|
|
|
const AsmToken AsmLexer::peekTok(bool ShouldSkipSpace) {
|
|
|
|
const char *SavedTokStart = TokStart;
|
|
|
|
const char *SavedCurPtr = CurPtr;
|
|
|
|
bool SavedAtStartOfLine = isAtStartOfLine;
|
|
|
|
bool SavedSkipSpace = SkipSpace;
|
|
|
|
|
|
|
|
std::string SavedErr = getErr();
|
|
|
|
SMLoc SavedErrLoc = getErrLoc();
|
|
|
|
|
|
|
|
SkipSpace = ShouldSkipSpace;
|
|
|
|
AsmToken Token = LexToken();
|
|
|
|
|
|
|
|
SetError(SavedErrLoc, SavedErr);
|
|
|
|
|
|
|
|
SkipSpace = SavedSkipSpace;
|
|
|
|
isAtStartOfLine = SavedAtStartOfLine;
|
|
|
|
CurPtr = SavedCurPtr;
|
|
|
|
TokStart = SavedTokStart;
|
|
|
|
|
|
|
|
return Token;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-08-14 10:51:43 +08:00
|
|
|
if (isAtStartOfComment(TokStart)) {
|
2011-09-14 07:45:18 +08:00
|
|
|
// If this comment starts with a '#', then return the Hash token and let
|
|
|
|
// the assembler parser see if it can be parsed as a cpp line filename
|
|
|
|
// comment. We do this only if we are at the start of a line.
|
|
|
|
if (CurChar == '#' && isAtStartOfLine)
|
|
|
|
return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
|
|
|
|
isAtStartOfLine = true;
|
2009-09-17 02:08:00 +08:00
|
|
|
return LexLineComment();
|
2011-09-14 07:45:18 +08:00
|
|
|
}
|
2011-03-25 02:46:34 +08:00
|
|
|
if (isAtStatementSeparator(TokStart)) {
|
|
|
|
CurPtr += strlen(MAI.getSeparatorString()) - 1;
|
|
|
|
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.
|
|
|
|
if (CurChar == EOF && !isAtStartOfLine) {
|
|
|
|
isAtStartOfLine = true;
|
|
|
|
return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
isAtStartOfLine = 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");
|
2009-07-29 00:08:33 +08:00
|
|
|
case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
|
2009-06-21 15:19:10 +08:00
|
|
|
case 0:
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
2012-09-20 04:36:12 +08:00
|
|
|
if (SkipSpace) {
|
|
|
|
// Ignore whitespace.
|
|
|
|
return LexToken();
|
|
|
|
} else {
|
|
|
|
int len = 1;
|
|
|
|
while (*CurPtr==' ' || *CurPtr=='\t') {
|
|
|
|
CurPtr++;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
return AsmToken(AsmToken::Space, StringRef(TokStart, len));
|
|
|
|
}
|
2009-06-22 03:21:25 +08:00
|
|
|
case '\n': // FALL THROUGH.
|
2011-03-25 02:46:34 +08:00
|
|
|
case '\r':
|
2011-09-14 07:45:18 +08:00
|
|
|
isAtStartOfLine = 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 '=':
|
2009-06-30 04:37:27 +08:00
|
|
|
if (*CurPtr == '=')
|
2009-07-29 00:08:33 +08:00
|
|
|
return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
|
|
|
|
return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
|
2011-02-12 03:05:56 +08:00
|
|
|
case '|':
|
2009-06-30 04:37:27 +08:00
|
|
|
if (*CurPtr == '|')
|
2009-07-29 00:08:33 +08:00
|
|
|
return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
|
|
|
|
return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
|
|
|
|
case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
|
2011-02-12 03:05:56 +08:00
|
|
|
case '&':
|
2009-06-30 04:37:27 +08:00
|
|
|
if (*CurPtr == '&')
|
2009-07-29 00:08:33 +08:00
|
|
|
return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
|
|
|
|
return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
|
2011-02-12 03:05:56 +08:00
|
|
|
case '!':
|
2009-06-30 04:37:27 +08:00
|
|
|
if (*CurPtr == '=')
|
2009-07-29 00:08:33 +08:00
|
|
|
return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
|
|
|
|
return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
|
2009-09-04 01:15:07 +08:00
|
|
|
case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
|
2009-06-22 03:21:25 +08:00
|
|
|
case '/': 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) {
|
2011-02-12 03:05:56 +08:00
|
|
|
case '<': return ++CurPtr, AsmToken(AsmToken::LessLess,
|
2009-07-28 11:00:54 +08:00
|
|
|
StringRef(TokStart, 2));
|
2011-02-12 03:05:56 +08:00
|
|
|
case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual,
|
2009-07-28 11:00:54 +08:00
|
|
|
StringRef(TokStart, 2));
|
2011-02-12 03:05:56 +08:00
|
|
|
case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater,
|
2009-07-28 11:00:54 +08:00
|
|
|
StringRef(TokStart, 2));
|
2009-07-29 00:08:33 +08:00
|
|
|
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) {
|
2011-02-12 03:05:56 +08:00
|
|
|
case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater,
|
2009-07-28 11:00:54 +08:00
|
|
|
StringRef(TokStart, 2));
|
2011-02-12 03:05:56 +08:00
|
|
|
case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual,
|
2009-07-28 11:00:54 +08:00
|
|
|
StringRef(TokStart, 2));
|
2009-07-29 00:08:33 +08:00
|
|
|
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
|
|
|
}
|