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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AsmLexer.h"
|
2009-06-24 00:33:19 +00:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2009-06-21 07:19:10 +00:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-06-23 00:24:36 +00:00
|
|
|
#include "llvm/Config/config.h" // for strtoull.
|
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;
|
|
|
|
|
2009-06-24 00:33:19 +00:00
|
|
|
static StringSet<> &getSS(void *TheSS) {
|
|
|
|
return *(StringSet<>*)TheSS;
|
|
|
|
}
|
|
|
|
|
2009-06-21 07:19:10 +00:00
|
|
|
AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) {
|
|
|
|
CurBuffer = 0;
|
|
|
|
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
|
|
|
|
CurPtr = CurBuf->getBufferStart();
|
|
|
|
TokStart = 0;
|
2009-06-24 00:33:19 +00:00
|
|
|
|
|
|
|
TheStringSet = new StringSet<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
AsmLexer::~AsmLexer() {
|
|
|
|
delete &getSS(TheStringSet);
|
2009-06-21 07:19:10 +00:00
|
|
|
}
|
|
|
|
|
2009-06-21 19:21:25 +00:00
|
|
|
SMLoc AsmLexer::getLoc() const {
|
|
|
|
return SMLoc::getFromPointer(TokStart);
|
|
|
|
}
|
|
|
|
|
2009-06-21 21:22:11 +00:00
|
|
|
void AsmLexer::PrintMessage(SMLoc Loc, const std::string &Msg) const {
|
|
|
|
SrcMgr.PrintMessage(Loc, Msg);
|
2009-06-21 07:19:10 +00:00
|
|
|
}
|
|
|
|
|
2009-06-21 19:21:25 +00:00
|
|
|
/// ReturnError - Set the error to the specified string at the specified
|
|
|
|
/// location. This is defined to always return asmtok::Error.
|
|
|
|
asmtok::TokKind AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
|
2009-06-21 21:22:11 +00:00
|
|
|
SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg);
|
2009-06-21 19:21:25 +00:00
|
|
|
return asmtok::Error;
|
|
|
|
}
|
|
|
|
|
2009-06-21 07:19:10 +00:00
|
|
|
int AsmLexer::getNextChar() {
|
|
|
|
char CurChar = *CurPtr++;
|
|
|
|
switch (CurChar) {
|
|
|
|
default:
|
|
|
|
return (unsigned char)CurChar;
|
|
|
|
case 0: {
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// If this is the end of an included file, pop the parent file off the
|
|
|
|
// include stack.
|
|
|
|
SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
|
|
|
|
if (ParentIncludeLoc != SMLoc()) {
|
|
|
|
CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
|
|
|
|
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
|
|
|
|
CurPtr = ParentIncludeLoc.getPointer();
|
|
|
|
return getNextChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, return end of file.
|
|
|
|
--CurPtr; // Another call to lex will return EOF again.
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-21 19:21:25 +00:00
|
|
|
/// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
|
|
|
|
asmtok::TokKind AsmLexer::LexIdentifier() {
|
|
|
|
while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
|
|
|
|
*CurPtr == '.' || *CurPtr == '@')
|
|
|
|
++CurPtr;
|
2009-06-24 00:33:19 +00:00
|
|
|
// Unique string.
|
|
|
|
CurStrVal =
|
|
|
|
getSS(TheStringSet).GetOrCreateValue(TokStart, CurPtr, 0).getKeyData();
|
2009-06-21 19:21:25 +00:00
|
|
|
return asmtok::Identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LexPercent: Register: %[a-zA-Z0-9]+
|
|
|
|
asmtok::TokKind AsmLexer::LexPercent() {
|
|
|
|
if (!isalnum(*CurPtr))
|
2009-06-23 05:57:07 +00:00
|
|
|
return asmtok::Percent; // Single %.
|
|
|
|
|
2009-06-21 19:21:25 +00:00
|
|
|
while (isalnum(*CurPtr))
|
|
|
|
++CurPtr;
|
2009-06-24 00:33:19 +00:00
|
|
|
|
|
|
|
// Unique string.
|
|
|
|
CurStrVal =
|
|
|
|
getSS(TheStringSet).GetOrCreateValue(TokStart, CurPtr, 0).getKeyData();
|
2009-06-21 19:21:25 +00:00
|
|
|
return asmtok::Register;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LexSlash: Slash: /
|
|
|
|
/// C-Style Comment: /* ... */
|
|
|
|
asmtok::TokKind AsmLexer::LexSlash() {
|
|
|
|
if (*CurPtr != '*')
|
|
|
|
return asmtok::Slash;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
++CurPtr; // End the */.
|
|
|
|
return LexToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LexHash: Comment: #[^\n]*
|
|
|
|
asmtok::TokKind AsmLexer::LexHash() {
|
|
|
|
int CurChar = getNextChar();
|
|
|
|
while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
|
|
|
|
CurChar = getNextChar();
|
|
|
|
|
|
|
|
if (CurChar == EOF)
|
|
|
|
return asmtok::Eof;
|
|
|
|
return asmtok::EndOfStatement;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 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]*
|
|
|
|
/// TODO: FP literal.
|
|
|
|
asmtok::TokKind AsmLexer::LexDigit() {
|
|
|
|
if (*CurPtr == ':')
|
2009-06-21 20:16:42 +00:00
|
|
|
return ReturnError(TokStart, "FIXME: local label not implemented");
|
2009-06-21 19:21:25 +00:00
|
|
|
if (*CurPtr == 'f' || *CurPtr == 'b')
|
2009-06-21 20:16:42 +00:00
|
|
|
return ReturnError(TokStart, "FIXME: directional label not implemented");
|
2009-06-21 19:21:25 +00:00
|
|
|
|
|
|
|
// Decimal integer: [1-9][0-9]*
|
|
|
|
if (CurPtr[-1] != '0') {
|
|
|
|
while (isdigit(*CurPtr))
|
|
|
|
++CurPtr;
|
|
|
|
CurIntVal = strtoll(TokStart, 0, 10);
|
|
|
|
return asmtok::IntVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*CurPtr == 'b') {
|
|
|
|
++CurPtr;
|
|
|
|
const char *NumStart = CurPtr;
|
|
|
|
while (CurPtr[0] == '0' || CurPtr[0] == '1')
|
|
|
|
++CurPtr;
|
|
|
|
|
|
|
|
// Requires at least one binary digit.
|
|
|
|
if (CurPtr == NumStart)
|
|
|
|
return ReturnError(CurPtr-2, "Invalid binary number");
|
|
|
|
CurIntVal = strtoll(NumStart, 0, 2);
|
|
|
|
return asmtok::IntVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*CurPtr == 'x') {
|
|
|
|
++CurPtr;
|
|
|
|
const char *NumStart = CurPtr;
|
|
|
|
while (isxdigit(CurPtr[0]))
|
|
|
|
++CurPtr;
|
|
|
|
|
|
|
|
// Requires at least one hex digit.
|
|
|
|
if (CurPtr == NumStart)
|
|
|
|
return ReturnError(CurPtr-2, "Invalid hexadecimal number");
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
CurIntVal = strtoll(NumStart, 0, 16);
|
|
|
|
if (errno == EINVAL)
|
|
|
|
return ReturnError(CurPtr-2, "Invalid hexadecimal number");
|
|
|
|
if (errno == ERANGE) {
|
|
|
|
errno = 0;
|
|
|
|
CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
|
|
|
|
if (errno == EINVAL)
|
|
|
|
return ReturnError(CurPtr-2, "Invalid hexadecimal number");
|
|
|
|
if (errno == ERANGE)
|
|
|
|
return ReturnError(CurPtr-2, "Hexadecimal number out of range");
|
|
|
|
}
|
|
|
|
return asmtok::IntVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must be an octal number, it starts with 0.
|
|
|
|
while (*CurPtr >= '0' && *CurPtr <= '7')
|
|
|
|
++CurPtr;
|
|
|
|
CurIntVal = strtoll(TokStart, 0, 8);
|
|
|
|
return asmtok::IntVal;
|
|
|
|
}
|
|
|
|
|
2009-06-21 19:56:35 +00:00
|
|
|
/// LexQuote: String: "..."
|
|
|
|
asmtok::TokKind AsmLexer::LexQuote() {
|
|
|
|
int CurChar = getNextChar();
|
|
|
|
// TODO: does gas allow multiline string constants?
|
|
|
|
while (CurChar != '"') {
|
|
|
|
if (CurChar == '\\') {
|
|
|
|
// Allow \", etc.
|
|
|
|
CurChar = getNextChar();
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2009-06-24 00:33:19 +00:00
|
|
|
// Unique string, include quotes for now.
|
|
|
|
CurStrVal =
|
|
|
|
getSS(TheStringSet).GetOrCreateValue(TokStart, CurPtr, 0).getKeyData();
|
2009-06-21 19:56:35 +00:00
|
|
|
return asmtok::String;
|
|
|
|
}
|
|
|
|
|
2009-06-21 19:21:25 +00:00
|
|
|
|
2009-06-21 07:19:10 +00:00
|
|
|
asmtok::TokKind AsmLexer::LexToken() {
|
|
|
|
TokStart = CurPtr;
|
|
|
|
// This always consumes at least one character.
|
|
|
|
int CurChar = getNextChar();
|
|
|
|
|
|
|
|
switch (CurChar) {
|
|
|
|
default:
|
2009-06-21 19:21:25 +00:00
|
|
|
// Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
|
|
|
|
if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
|
|
|
|
return LexIdentifier();
|
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-06-21 07:19:10 +00:00
|
|
|
case EOF: return asmtok::Eof;
|
|
|
|
case 0:
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
// Ignore whitespace.
|
|
|
|
return LexToken();
|
2009-06-21 19:21:25 +00:00
|
|
|
case '\n': // FALL THROUGH.
|
|
|
|
case '\r': // FALL THROUGH.
|
|
|
|
case ';': return asmtok::EndOfStatement;
|
2009-06-21 07:19:10 +00:00
|
|
|
case ':': return asmtok::Colon;
|
|
|
|
case '+': return asmtok::Plus;
|
|
|
|
case '-': return asmtok::Minus;
|
2009-06-22 06:32:03 +00:00
|
|
|
case '~': return asmtok::Tilde;
|
2009-06-21 19:21:25 +00:00
|
|
|
case '(': return asmtok::LParen;
|
|
|
|
case ')': return asmtok::RParen;
|
|
|
|
case '*': return asmtok::Star;
|
|
|
|
case ',': return asmtok::Comma;
|
|
|
|
case '$': return asmtok::Dollar;
|
2009-06-29 20:37:27 +00:00
|
|
|
case '=':
|
|
|
|
if (*CurPtr == '=')
|
|
|
|
return ++CurPtr, asmtok::EqualEqual;
|
|
|
|
return asmtok::Equal;
|
|
|
|
case '|':
|
|
|
|
if (*CurPtr == '|')
|
|
|
|
return ++CurPtr, asmtok::PipePipe;
|
|
|
|
return asmtok::Pipe;
|
2009-06-23 05:57:07 +00:00
|
|
|
case '^': return asmtok::Caret;
|
2009-06-29 20:37:27 +00:00
|
|
|
case '&':
|
|
|
|
if (*CurPtr == '&')
|
|
|
|
return ++CurPtr, asmtok::AmpAmp;
|
|
|
|
return asmtok::Amp;
|
|
|
|
case '!':
|
|
|
|
if (*CurPtr == '=')
|
|
|
|
return ++CurPtr, asmtok::ExclaimEqual;
|
|
|
|
return asmtok::Exclaim;
|
2009-06-21 19:21:25 +00:00
|
|
|
case '%': return LexPercent();
|
|
|
|
case '/': return LexSlash();
|
|
|
|
case '#': return LexHash();
|
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) {
|
|
|
|
case '<': return ++CurPtr, asmtok::LessLess;
|
|
|
|
case '=': return ++CurPtr, asmtok::LessEqual;
|
|
|
|
case '>': return ++CurPtr, asmtok::LessGreater;
|
|
|
|
default: return asmtok::Less;
|
2009-06-23 05:57:07 +00:00
|
|
|
}
|
|
|
|
case '>':
|
2009-06-29 20:37:27 +00:00
|
|
|
switch (*CurPtr) {
|
|
|
|
case '>': return ++CurPtr, asmtok::GreaterGreater;
|
|
|
|
case '=': return ++CurPtr, asmtok::GreaterEqual;
|
|
|
|
default: return asmtok::Greater;
|
2009-06-23 05:57:07 +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
|
|
|
}
|