2009-06-21 07:19:10 +00: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 01:58:08 +00:00
|
|
|
#include "llvm/MC/MCParser/AsmLexer.h"
|
2010-01-21 00:19:58 +00:00
|
|
|
#include "llvm/Support/SMLoc.h"
|
2009-06-21 07:19:10 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-09-04 21:45:34 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2010-12-19 20:43:38 +00:00
|
|
|
#include <cctype>
|
2009-06-21 19:21:25 +00:00
|
|
|
#include <cerrno>
|
2009-06-22 06:59:32 +00:00
|
|
|
#include <cstdio>
|
2009-06-21 19:43:50 +00:00
|
|
|
#include <cstdlib>
|
2009-06-21 07:19:10 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2010-01-21 00:19:58 +00:00
|
|
|
AsmLexer::AsmLexer(const MCAsmInfo &_MAI) : MAI(_MAI) {
|
|
|
|
CurBuf = NULL;
|
|
|
|
CurPtr = NULL;
|
2011-09-14 16:37:04 +00:00
|
|
|
isAtStartOfLine = true;
|
2009-06-24 00:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AsmLexer::~AsmLexer() {
|
2009-06-21 07:19:10 +00:00
|
|
|
}
|
|
|
|
|
2010-01-21 00:19:58 +00:00
|
|
|
void AsmLexer::setBuffer(const MemoryBuffer *buf, const char *ptr) {
|
|
|
|
CurBuf = buf;
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2010-01-21 00:19:58 +00:00
|
|
|
if (ptr)
|
|
|
|
CurPtr = ptr;
|
|
|
|
else
|
|
|
|
CurPtr = CurBuf->getBufferStart();
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2010-01-21 00:19:58 +00:00
|
|
|
TokStart = 0;
|
|
|
|
}
|
|
|
|
|
2009-06-21 19:21:25 +00:00
|
|
|
/// ReturnError - Set the error to the specified string at the specified
|
2009-07-28 16:08:33 +00:00
|
|
|
/// location. This is defined to always return AsmToken::Error.
|
2009-07-28 03:00:54 +00:00
|
|
|
AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
|
2010-01-20 22:18:24 +00:00
|
|
|
SetError(SMLoc::getFromPointer(Loc), Msg);
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
return AsmToken(AsmToken::Error, StringRef(Loc, 0));
|
2009-06-21 19:21:25 +00:00
|
|
|
}
|
|
|
|
|
2009-06-21 07:19:10 +00:00
|
|
|
int AsmLexer::getNextChar() {
|
|
|
|
char CurChar = *CurPtr++;
|
|
|
|
switch (CurChar) {
|
|
|
|
default:
|
|
|
|
return (unsigned char)CurChar;
|
2010-01-21 00:19:58 +00:00
|
|
|
case 0:
|
2009-06-21 07:19:10 +00: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.
|
|
|
|
if (CurPtr-1 != CurBuf->getBufferEnd())
|
|
|
|
return 0; // Just whitespace.
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-06-21 07:19:10 +00:00
|
|
|
// Otherwise, return end of file.
|
2011-02-11 19:05:56 +00:00
|
|
|
--CurPtr; // Another call to lex will return EOF again.
|
2009-06-21 07:19:10 +00:00
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-27 20:12:52 +00: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));
|
|
|
|
}
|
|
|
|
|
2010-05-06 14:46:38 +00:00
|
|
|
/// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
|
2010-09-24 01:59:31 +00:00
|
|
|
static bool IsIdentifierChar(char c) {
|
|
|
|
return isalnum(c) || c == '_' || c == '$' || c == '.' || c == '@';
|
|
|
|
}
|
2009-07-28 03:00:54 +00:00
|
|
|
AsmToken AsmLexer::LexIdentifier() {
|
2010-09-24 01:59:31 +00:00
|
|
|
// Check for floating point literals.
|
|
|
|
if (CurPtr[-1] == '.' && isdigit(*CurPtr)) {
|
2010-09-27 20:12:52 +00:00
|
|
|
// Disambiguate a .1243foo identifier from a floating literal.
|
2010-09-24 01:59:31 +00:00
|
|
|
while (isdigit(*CurPtr))
|
|
|
|
++CurPtr;
|
2010-09-27 20:12:52 +00:00
|
|
|
if (*CurPtr == 'e' || *CurPtr == 'E' || !IsIdentifierChar(*CurPtr))
|
|
|
|
return LexFloatLiteral();
|
2010-09-24 01:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (IsIdentifierChar(*CurPtr))
|
2009-06-21 19:21:25 +00:00
|
|
|
++CurPtr;
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2010-04-14 04:40:28 +00:00
|
|
|
// Handle . as a special case.
|
2010-05-06 14:46:38 +00:00
|
|
|
if (CurPtr == TokStart+1 && TokStart[0] == '.')
|
|
|
|
return AsmToken(AsmToken::Dot, StringRef(TokStart, 1));
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
|
2009-06-21 19:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// LexSlash: Slash: /
|
|
|
|
/// C-Style Comment: /* ... */
|
2009-07-28 03:00:54 +00:00
|
|
|
AsmToken AsmLexer::LexSlash() {
|
2009-06-29 21:58:22 +00:00
|
|
|
switch (*CurPtr) {
|
|
|
|
case '*': break; // C style comment.
|
|
|
|
case '/': return ++CurPtr, LexLineComment();
|
2010-10-25 20:18:53 +00:00
|
|
|
default: return AsmToken(AsmToken::Slash, StringRef(CurPtr-1, 1));
|
2009-06-29 21:58:22 +00:00
|
|
|
}
|
2009-06-21 19:21:25 +00:00
|
|
|
|
|
|
|
// C Style comment.
|
|
|
|
++CurPtr; // skip the star.
|
|
|
|
while (1) {
|
|
|
|
int CurChar = getNextChar();
|
|
|
|
switch (CurChar) {
|
|
|
|
case EOF:
|
2009-06-21 20:16:42 +00:00
|
|
|
return ReturnError(TokStart, "unterminated comment");
|
2009-06-21 19:21:25 +00:00
|
|
|
case '*':
|
|
|
|
// End of the comment?
|
|
|
|
if (CurPtr[0] != '/') break;
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-06-21 19:21:25 +00:00
|
|
|
++CurPtr; // End the */.
|
|
|
|
return LexToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-29 21:58:22 +00:00
|
|
|
/// LexLineComment: Comment: #[^\n]*
|
|
|
|
/// : //[^\n]*
|
2009-07-28 03:00:54 +00: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-21 19:21:25 +00:00
|
|
|
int CurChar = getNextChar();
|
2011-08-04 19:31:26 +00:00
|
|
|
while (CurChar != '\n' && CurChar != '\r' && CurChar != EOF)
|
2009-06-21 19:21:25 +00:00
|
|
|
CurChar = getNextChar();
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-06-21 19:21:25 +00:00
|
|
|
if (CurChar == EOF)
|
2009-07-28 16:08:33 +00:00
|
|
|
return AsmToken(AsmToken::Eof, StringRef(CurPtr, 0));
|
|
|
|
return AsmToken(AsmToken::EndOfStatement, StringRef(CurPtr, 0));
|
2009-06-21 19:21:25 +00:00
|
|
|
}
|
|
|
|
|
2010-08-24 00:43:25 +00:00
|
|
|
static void SkipIgnoredIntegerSuffix(const char *&CurPtr) {
|
|
|
|
if (CurPtr[0] == 'L' && CurPtr[1] == 'L')
|
|
|
|
CurPtr += 2;
|
|
|
|
if (CurPtr[0] == 'U' && CurPtr[1] == 'L' && CurPtr[2] == 'L')
|
|
|
|
CurPtr += 3;
|
|
|
|
}
|
|
|
|
|
2009-06-21 19:21:25 +00:00
|
|
|
/// LexDigit: First character is [0-9].
|
|
|
|
/// Local Label: [0-9][:]
|
|
|
|
/// Forward/Backward Label: [0-9][fb]
|
|
|
|
/// Binary integer: 0b[01]+
|
|
|
|
/// Octal integer: 0[0-7]+
|
|
|
|
/// Hex integer: 0x[0-9a-fA-F]+
|
|
|
|
/// Decimal integer: [1-9][0-9]*
|
2009-07-28 03:00:54 +00:00
|
|
|
AsmToken AsmLexer::LexDigit() {
|
2009-06-21 19:21:25 +00:00
|
|
|
// Decimal integer: [1-9][0-9]*
|
2010-09-24 17:10:26 +00:00
|
|
|
if (CurPtr[-1] != '0' || CurPtr[0] == '.') {
|
2009-06-21 19:21:25 +00:00
|
|
|
while (isdigit(*CurPtr))
|
|
|
|
++CurPtr;
|
2010-09-24 01:59:31 +00:00
|
|
|
|
|
|
|
// Check for floating point literals.
|
2010-09-27 20:12:52 +00:00
|
|
|
if (*CurPtr == '.' || *CurPtr == 'e') {
|
2010-09-24 01:59:31 +00:00
|
|
|
++CurPtr;
|
2010-09-27 20:12:52 +00:00
|
|
|
return LexFloatLiteral();
|
2010-09-24 01:59:31 +00:00
|
|
|
}
|
|
|
|
|
2010-01-22 07:34:12 +00:00
|
|
|
StringRef Result(TokStart, CurPtr - TokStart);
|
2010-08-24 00:43:25 +00:00
|
|
|
|
2010-01-22 07:34:12 +00:00
|
|
|
long long Value;
|
2010-03-13 19:25:13 +00:00
|
|
|
if (Result.getAsInteger(10, Value)) {
|
2010-12-25 21:36:35 +00:00
|
|
|
// Allow positive values that are too large to fit into a signed 64-bit
|
|
|
|
// integer, but that do fit in an unsigned one, we just convert them over.
|
|
|
|
unsigned long long UValue;
|
|
|
|
if (Result.getAsInteger(10, UValue))
|
|
|
|
return ReturnError(TokStart, "invalid decimal number");
|
|
|
|
Value = (long long)UValue;
|
2010-03-13 19:25:13 +00:00
|
|
|
}
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2010-08-24 00:43:25 +00:00
|
|
|
// The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
|
|
|
|
// suffixes on integer literals.
|
|
|
|
SkipIgnoredIntegerSuffix(CurPtr);
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2010-01-22 07:34:12 +00:00
|
|
|
return AsmToken(AsmToken::Integer, Result, Value);
|
2009-06-21 19:21:25 +00:00
|
|
|
}
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-06-21 19:21:25 +00:00
|
|
|
if (*CurPtr == 'b') {
|
|
|
|
++CurPtr;
|
2010-05-17 23:08:19 +00:00
|
|
|
// See if we actually have "0b" as part of something like "jmp 0b\n"
|
2010-05-18 18:09:20 +00:00
|
|
|
if (!isdigit(CurPtr[0])) {
|
2010-05-17 23:08:19 +00:00
|
|
|
--CurPtr;
|
|
|
|
StringRef Result(TokStart, CurPtr - TokStart);
|
|
|
|
return AsmToken(AsmToken::Integer, Result, 0);
|
|
|
|
}
|
2009-06-21 19:21:25 +00:00
|
|
|
const char *NumStart = CurPtr;
|
|
|
|
while (CurPtr[0] == '0' || CurPtr[0] == '1')
|
|
|
|
++CurPtr;
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-06-21 19:21:25 +00:00
|
|
|
// Requires at least one binary digit.
|
|
|
|
if (CurPtr == NumStart)
|
2011-04-12 00:18:03 +00:00
|
|
|
return ReturnError(TokStart, "invalid binary number");
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2010-01-22 07:34:12 +00:00
|
|
|
StringRef Result(TokStart, CurPtr - TokStart);
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2010-01-22 07:34:12 +00:00
|
|
|
long long Value;
|
2010-08-24 00:43:25 +00:00
|
|
|
if (Result.substr(2).getAsInteger(2, Value))
|
2011-04-12 00:18:03 +00:00
|
|
|
return ReturnError(TokStart, "invalid binary number");
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2010-08-24 00:43:25 +00:00
|
|
|
// The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
|
|
|
|
// suffixes on integer literals.
|
|
|
|
SkipIgnoredIntegerSuffix(CurPtr);
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2010-01-22 07:34:12 +00:00
|
|
|
return AsmToken(AsmToken::Integer, Result, Value);
|
2009-06-21 19:21:25 +00:00
|
|
|
}
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-06-21 19:21:25 +00:00
|
|
|
if (*CurPtr == 'x') {
|
|
|
|
++CurPtr;
|
|
|
|
const char *NumStart = CurPtr;
|
|
|
|
while (isxdigit(CurPtr[0]))
|
|
|
|
++CurPtr;
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-06-21 19:21:25 +00:00
|
|
|
// Requires at least one hex digit.
|
|
|
|
if (CurPtr == NumStart)
|
2011-04-12 00:18:03 +00:00
|
|
|
return ReturnError(CurPtr-2, "invalid hexadecimal number");
|
2010-01-22 01:17:12 +00:00
|
|
|
|
|
|
|
unsigned long long Result;
|
|
|
|
if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
|
2011-04-12 00:18:03 +00:00
|
|
|
return ReturnError(TokStart, "invalid hexadecimal number");
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2010-08-24 00:43:25 +00:00
|
|
|
// The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
|
|
|
|
// suffixes on integer literals.
|
|
|
|
SkipIgnoredIntegerSuffix(CurPtr);
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
|
2010-01-22 01:17:12 +00:00
|
|
|
(int64_t)Result);
|
2009-06-21 19:21:25 +00:00
|
|
|
}
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-06-21 19:21:25 +00:00
|
|
|
// Must be an octal number, it starts with 0.
|
2011-04-11 22:24:56 +00:00
|
|
|
while (*CurPtr >= '0' && *CurPtr <= '9')
|
2009-06-21 19:21:25 +00:00
|
|
|
++CurPtr;
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2010-01-22 07:34:12 +00:00
|
|
|
StringRef Result(TokStart, CurPtr - TokStart);
|
|
|
|
long long Value;
|
|
|
|
if (Result.getAsInteger(8, Value))
|
2011-04-12 00:18:03 +00:00
|
|
|
return ReturnError(TokStart, "invalid octal number");
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2010-08-24 00:43:25 +00:00
|
|
|
// The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
|
|
|
|
// suffixes on integer literals.
|
|
|
|
SkipIgnoredIntegerSuffix(CurPtr);
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2010-01-22 07:34:12 +00:00
|
|
|
return AsmToken(AsmToken::Integer, Result, Value);
|
2009-06-21 19:21:25 +00:00
|
|
|
}
|
|
|
|
|
2010-12-18 08:56:37 +00: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-11 19:05:56 +00:00
|
|
|
return AsmToken(AsmToken::Integer, Res, Value);
|
2010-12-18 08:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-21 19:56:35 +00:00
|
|
|
/// LexQuote: String: "..."
|
2009-07-28 03:00:54 +00:00
|
|
|
AsmToken AsmLexer::LexQuote() {
|
2009-06-21 19:56:35 +00:00
|
|
|
int CurChar = getNextChar();
|
|
|
|
// TODO: does gas allow multiline string constants?
|
|
|
|
while (CurChar != '"') {
|
|
|
|
if (CurChar == '\\') {
|
|
|
|
// Allow \", etc.
|
|
|
|
CurChar = getNextChar();
|
|
|
|
}
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-06-21 21:22:11 +00:00
|
|
|
if (CurChar == EOF)
|
|
|
|
return ReturnError(TokStart, "unterminated string constant");
|
2009-06-21 19:56:35 +00:00
|
|
|
|
|
|
|
CurChar = getNextChar();
|
|
|
|
}
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
|
2009-06-21 19:56:35 +00:00
|
|
|
}
|
|
|
|
|
2009-08-10 01:39:42 +00:00
|
|
|
StringRef AsmLexer::LexUntilEndOfStatement() {
|
|
|
|
TokStart = CurPtr;
|
|
|
|
|
2011-03-24 18:46:34 +00:00
|
|
|
while (!isAtStartOfComment(*CurPtr) && // Start of line comment.
|
|
|
|
!isAtStatementSeparator(CurPtr) && // End of statement marker.
|
2009-08-10 01:39:42 +00:00
|
|
|
*CurPtr != '\n' &&
|
|
|
|
*CurPtr != '\r' &&
|
2009-09-04 21:45:34 +00:00
|
|
|
(*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
|
2009-08-10 01:39:42 +00:00
|
|
|
++CurPtr;
|
2009-09-04 21:45:34 +00:00
|
|
|
}
|
2009-08-10 01:39:42 +00:00
|
|
|
return StringRef(TokStart, CurPtr-TokStart);
|
|
|
|
}
|
2009-06-21 19:21:25 +00:00
|
|
|
|
2011-09-13 23:45:18 +00:00
|
|
|
StringRef AsmLexer::LexUntilEndOfLine() {
|
|
|
|
TokStart = CurPtr;
|
|
|
|
|
|
|
|
while (*CurPtr != '\n' &&
|
|
|
|
*CurPtr != '\r' &&
|
|
|
|
(*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
|
|
|
|
++CurPtr;
|
|
|
|
}
|
|
|
|
return StringRef(TokStart, CurPtr-TokStart);
|
|
|
|
}
|
|
|
|
|
2009-09-16 18:08:00 +00:00
|
|
|
bool AsmLexer::isAtStartOfComment(char Char) {
|
2009-09-27 19:38:39 +00:00
|
|
|
// FIXME: This won't work for multi-character comment indicators like "//".
|
|
|
|
return Char == *MAI.getCommentString();
|
2009-09-16 18:08:00 +00:00
|
|
|
}
|
|
|
|
|
2011-03-24 18:46:34 +00:00
|
|
|
bool AsmLexer::isAtStatementSeparator(const char *Ptr) {
|
|
|
|
return strncmp(Ptr, MAI.getSeparatorString(),
|
|
|
|
strlen(MAI.getSeparatorString())) == 0;
|
|
|
|
}
|
|
|
|
|
2009-07-28 03:00:54 +00:00
|
|
|
AsmToken AsmLexer::LexToken() {
|
2009-06-21 07:19:10 +00:00
|
|
|
TokStart = CurPtr;
|
|
|
|
// This always consumes at least one character.
|
|
|
|
int CurChar = getNextChar();
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2011-09-13 23:45:18 +00:00
|
|
|
if (isAtStartOfComment(CurChar)) {
|
|
|
|
// 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-16 18:08:00 +00:00
|
|
|
return LexLineComment();
|
2011-09-13 23:45:18 +00:00
|
|
|
}
|
2011-03-24 18:46:34 +00:00
|
|
|
if (isAtStatementSeparator(TokStart)) {
|
|
|
|
CurPtr += strlen(MAI.getSeparatorString()) - 1;
|
|
|
|
return AsmToken(AsmToken::EndOfStatement,
|
|
|
|
StringRef(TokStart, strlen(MAI.getSeparatorString())));
|
|
|
|
}
|
2009-09-04 21:45:34 +00:00
|
|
|
|
2011-09-15 16:52:06 +00: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 07:19:10 +00:00
|
|
|
switch (CurChar) {
|
|
|
|
default:
|
2010-05-06 14:46:38 +00:00
|
|
|
// Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
|
|
|
|
if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
|
2009-06-21 19:21:25 +00:00
|
|
|
return LexIdentifier();
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-06-21 07:19:10 +00:00
|
|
|
// Unknown character, emit an error.
|
2009-06-21 20:16:42 +00:00
|
|
|
return ReturnError(TokStart, "invalid character in input");
|
2009-07-28 16:08:33 +00:00
|
|
|
case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
|
2009-06-21 07:19:10 +00:00
|
|
|
case 0:
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
2012-09-19 20:36:12 +00: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-21 19:21:25 +00:00
|
|
|
case '\n': // FALL THROUGH.
|
2011-03-24 18:46:34 +00:00
|
|
|
case '\r':
|
2011-09-13 23:45:18 +00:00
|
|
|
isAtStartOfLine = true;
|
2011-03-24 18:46:34 +00:00
|
|
|
return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
|
2009-07-28 16:08:33 +00: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-04 22:40:31 +00: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-28 16:08:33 +00:00
|
|
|
case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
|
|
|
|
case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
|
2010-05-06 14:46:38 +00:00
|
|
|
case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
|
2010-05-21 11:36:59 +00:00
|
|
|
case '@': return AsmToken(AsmToken::At, StringRef(TokStart, 1));
|
2011-06-05 02:43:45 +00:00
|
|
|
case '\\': return AsmToken(AsmToken::BackSlash, StringRef(TokStart, 1));
|
2011-02-11 19:05:56 +00:00
|
|
|
case '=':
|
2009-06-29 20:37:27 +00:00
|
|
|
if (*CurPtr == '=')
|
2009-07-28 16:08:33 +00:00
|
|
|
return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
|
|
|
|
return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
|
2011-02-11 19:05:56 +00:00
|
|
|
case '|':
|
2009-06-29 20:37:27 +00:00
|
|
|
if (*CurPtr == '|')
|
2009-07-28 16:08:33 +00: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-11 19:05:56 +00:00
|
|
|
case '&':
|
2009-06-29 20:37:27 +00:00
|
|
|
if (*CurPtr == '&')
|
2009-07-28 16:08:33 +00:00
|
|
|
return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
|
|
|
|
return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
|
2011-02-11 19:05:56 +00:00
|
|
|
case '!':
|
2009-06-29 20:37:27 +00:00
|
|
|
if (*CurPtr == '=')
|
2009-07-28 16:08:33 +00:00
|
|
|
return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
|
|
|
|
return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
|
2009-09-03 17:15:07 +00:00
|
|
|
case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
|
2009-06-21 19:21:25 +00:00
|
|
|
case '/': return LexSlash();
|
2009-09-04 21:45:34 +00:00
|
|
|
case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
|
2010-12-18 08:56:37 +00:00
|
|
|
case '\'': return LexSingleQuote();
|
2009-06-21 19:56:35 +00:00
|
|
|
case '"': return LexQuote();
|
2009-06-21 19:21:25 +00: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 05:57:07 +00:00
|
|
|
case '<':
|
2009-06-29 20:37:27 +00:00
|
|
|
switch (*CurPtr) {
|
2011-02-11 19:05:56 +00:00
|
|
|
case '<': return ++CurPtr, AsmToken(AsmToken::LessLess,
|
2009-07-28 03:00:54 +00:00
|
|
|
StringRef(TokStart, 2));
|
2011-02-11 19:05:56 +00:00
|
|
|
case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual,
|
2009-07-28 03:00:54 +00:00
|
|
|
StringRef(TokStart, 2));
|
2011-02-11 19:05:56 +00:00
|
|
|
case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater,
|
2009-07-28 03:00:54 +00:00
|
|
|
StringRef(TokStart, 2));
|
2009-07-28 16:08:33 +00:00
|
|
|
default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
|
2009-06-23 05:57:07 +00:00
|
|
|
}
|
|
|
|
case '>':
|
2009-06-29 20:37:27 +00:00
|
|
|
switch (*CurPtr) {
|
2011-02-11 19:05:56 +00:00
|
|
|
case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater,
|
2009-07-28 03:00:54 +00:00
|
|
|
StringRef(TokStart, 2));
|
2011-02-11 19:05:56 +00:00
|
|
|
case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual,
|
2009-07-28 03:00:54 +00:00
|
|
|
StringRef(TokStart, 2));
|
2009-07-28 16:08:33 +00:00
|
|
|
default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
|
2009-06-23 05:57:07 +00:00
|
|
|
}
|
2011-02-11 19:05:56 +00:00
|
|
|
|
2009-06-21 19:21:25 +00: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 07:19:10 +00:00
|
|
|
}
|
2009-06-22 06:59:32 +00:00
|
|
|
}
|