2007-11-18 08:46:26 +00:00
|
|
|
//===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-11-18 08:46:26 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implement the Lexer for .ll files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "LLLexer.h"
|
2009-01-02 07:01:27 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Instruction.h"
|
2009-07-07 18:44:11 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2010-09-27 17:42:11 +00:00
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Assembly/Parser.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2007-11-18 08:46:26 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2007-12-08 19:03:30 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2009-07-02 22:46:18 +00:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2009-01-02 07:01:27 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-12-19 20:43:38 +00:00
|
|
|
#include <cctype>
|
2009-08-24 10:34:41 +00:00
|
|
|
#include <cstdio>
|
2009-01-02 07:18:46 +00:00
|
|
|
#include <cstdlib>
|
2009-01-02 22:49:28 +00:00
|
|
|
#include <cstring>
|
2007-11-18 08:46:26 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2010-09-27 17:42:11 +00:00
|
|
|
bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
|
2011-10-16 05:43:57 +00:00
|
|
|
ErrorInfo = SM.GetMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
|
2009-01-02 07:01:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// atoull - Convert an ascii string of decimal digits into the unsigned long
|
|
|
|
// long representation... this does not have to do input error checking,
|
|
|
|
// because we know that the input will be matched by a suitable regex...
|
|
|
|
//
|
2009-01-02 07:01:27 +00:00
|
|
|
uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
|
2007-11-18 08:46:26 +00:00
|
|
|
uint64_t Result = 0;
|
|
|
|
for (; Buffer != End; Buffer++) {
|
|
|
|
uint64_t OldRes = Result;
|
|
|
|
Result *= 10;
|
|
|
|
Result += *Buffer-'0';
|
|
|
|
if (Result < OldRes) { // Uh, oh, overflow detected!!!
|
2009-01-02 07:01:27 +00:00
|
|
|
Error("constant bigger than 64 bits detected!");
|
2007-11-18 08:46:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2011-10-27 14:08:01 +00:00
|
|
|
static char parseHexChar(char C) {
|
|
|
|
if (C >= '0' && C <= '9')
|
|
|
|
return C-'0';
|
|
|
|
if (C >= 'A' && C <= 'F')
|
|
|
|
return C-'A'+10;
|
|
|
|
if (C >= 'a' && C <= 'f')
|
|
|
|
return C-'a'+10;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-02 07:01:27 +00:00
|
|
|
uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
|
2007-11-18 08:46:26 +00:00
|
|
|
uint64_t Result = 0;
|
|
|
|
for (; Buffer != End; ++Buffer) {
|
|
|
|
uint64_t OldRes = Result;
|
|
|
|
Result *= 16;
|
2011-10-27 14:08:01 +00:00
|
|
|
Result += parseHexChar(*Buffer);
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
if (Result < OldRes) { // Uh, oh, overflow detected!!!
|
2009-01-02 07:01:27 +00:00
|
|
|
Error("constant bigger than 64 bits detected!");
|
2007-11-18 08:46:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2009-01-02 07:01:27 +00:00
|
|
|
void LLLexer::HexToIntPair(const char *Buffer, const char *End,
|
|
|
|
uint64_t Pair[2]) {
|
2007-11-18 08:46:26 +00:00
|
|
|
Pair[0] = 0;
|
|
|
|
for (int i=0; i<16; i++, Buffer++) {
|
|
|
|
assert(Buffer != End);
|
|
|
|
Pair[0] *= 16;
|
2011-10-27 14:08:01 +00:00
|
|
|
Pair[0] += parseHexChar(*Buffer);
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
Pair[1] = 0;
|
|
|
|
for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
|
|
|
|
Pair[1] *= 16;
|
2011-10-27 14:08:01 +00:00
|
|
|
Pair[1] += parseHexChar(*Buffer);
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2007-11-18 18:25:18 +00:00
|
|
|
if (Buffer != End)
|
2009-01-02 07:01:27 +00:00
|
|
|
Error("constant bigger than 128 bits detected!");
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
|
2009-03-23 21:16:53 +00:00
|
|
|
/// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
|
|
|
|
/// { low64, high16 } as usual for an APInt.
|
|
|
|
void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
|
|
|
|
uint64_t Pair[2]) {
|
|
|
|
Pair[1] = 0;
|
|
|
|
for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
|
|
|
|
assert(Buffer != End);
|
|
|
|
Pair[1] *= 16;
|
2011-10-27 14:08:01 +00:00
|
|
|
Pair[1] += parseHexChar(*Buffer);
|
2009-03-23 21:16:53 +00:00
|
|
|
}
|
|
|
|
Pair[0] = 0;
|
|
|
|
for (int i=0; i<16; i++, Buffer++) {
|
|
|
|
Pair[0] *= 16;
|
2011-10-27 14:08:01 +00:00
|
|
|
Pair[0] += parseHexChar(*Buffer);
|
2009-03-23 21:16:53 +00:00
|
|
|
}
|
|
|
|
if (Buffer != End)
|
|
|
|
Error("constant bigger than 128 bits detected!");
|
|
|
|
}
|
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
|
|
|
|
// appropriate character.
|
|
|
|
static void UnEscapeLexed(std::string &Str) {
|
|
|
|
if (Str.empty()) return;
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
|
|
|
|
char *BOut = Buffer;
|
|
|
|
for (char *BIn = Buffer; BIn != EndBuffer; ) {
|
|
|
|
if (BIn[0] == '\\') {
|
|
|
|
if (BIn < EndBuffer-1 && BIn[1] == '\\') {
|
|
|
|
*BOut++ = '\\'; // Two \ becomes one
|
|
|
|
BIn += 2;
|
|
|
|
} else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
|
2011-10-27 14:08:01 +00:00
|
|
|
*BOut = parseHexChar(BIn[1]) * 16 + parseHexChar(BIn[2]);
|
2007-11-18 08:46:26 +00:00
|
|
|
BIn += 3; // Skip over handled chars
|
|
|
|
++BOut;
|
|
|
|
} else {
|
|
|
|
*BOut++ = *BIn++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*BOut++ = *BIn++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Str.resize(BOut-Buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isLabelChar - Return true for [-a-zA-Z$._0-9].
|
|
|
|
static bool isLabelChar(char C) {
|
|
|
|
return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// isLabelTail - Return true if this pointer points to a valid end of a label.
|
|
|
|
static const char *isLabelTail(const char *CurPtr) {
|
|
|
|
while (1) {
|
|
|
|
if (CurPtr[0] == ':') return CurPtr+1;
|
|
|
|
if (!isLabelChar(CurPtr[0])) return 0;
|
|
|
|
++CurPtr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Lexer definition.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-07 18:44:11 +00:00
|
|
|
LLLexer::LLLexer(MemoryBuffer *StartBuf, SourceMgr &sm, SMDiagnostic &Err,
|
|
|
|
LLVMContext &C)
|
|
|
|
: CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
|
2007-11-18 08:46:26 +00:00
|
|
|
CurPtr = CurBuf->getBufferStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LLLexer::getFilename() const {
|
|
|
|
return CurBuf->getBufferIdentifier();
|
|
|
|
}
|
|
|
|
|
|
|
|
int LLLexer::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.
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// Otherwise, return end of file.
|
2007-12-16 09:16:12 +00:00
|
|
|
--CurPtr; // Another call to lex will return EOF again.
|
2007-11-18 08:46:26 +00:00
|
|
|
return EOF;
|
2007-12-16 09:16:12 +00:00
|
|
|
}
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-02 07:01:27 +00:00
|
|
|
lltok::Kind LLLexer::LexToken() {
|
2007-11-18 08:46:26 +00:00
|
|
|
TokStart = CurPtr;
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
int CurChar = getNextChar();
|
|
|
|
switch (CurChar) {
|
|
|
|
default:
|
|
|
|
// Handle letters: [a-zA-Z_]
|
|
|
|
if (isalpha(CurChar) || CurChar == '_')
|
|
|
|
return LexIdentifier();
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::Error;
|
|
|
|
case EOF: return lltok::Eof;
|
2007-11-18 08:46:26 +00:00
|
|
|
case 0:
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
// Ignore whitespace.
|
|
|
|
return LexToken();
|
|
|
|
case '+': return LexPositive();
|
|
|
|
case '@': return LexAt();
|
|
|
|
case '%': return LexPercent();
|
|
|
|
case '"': return LexQuote();
|
|
|
|
case '.':
|
|
|
|
if (const char *Ptr = isLabelTail(CurPtr)) {
|
|
|
|
CurPtr = Ptr;
|
2009-01-02 07:01:27 +00:00
|
|
|
StrVal.assign(TokStart, CurPtr-1);
|
|
|
|
return lltok::LabelStr;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
if (CurPtr[0] == '.' && CurPtr[1] == '.') {
|
|
|
|
CurPtr += 2;
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::dotdotdot;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::Error;
|
2007-11-18 08:46:26 +00:00
|
|
|
case '$':
|
|
|
|
if (const char *Ptr = isLabelTail(CurPtr)) {
|
|
|
|
CurPtr = Ptr;
|
2009-01-02 07:01:27 +00:00
|
|
|
StrVal.assign(TokStart, CurPtr-1);
|
|
|
|
return lltok::LabelStr;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::Error;
|
2007-11-18 08:46:26 +00:00
|
|
|
case ';':
|
|
|
|
SkipLineComment();
|
|
|
|
return LexToken();
|
2009-12-30 04:56:59 +00:00
|
|
|
case '!': return LexExclaim();
|
2007-11-18 08:46:26 +00:00
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
2007-12-16 09:16:12 +00:00
|
|
|
case '-':
|
2007-11-18 08:46:26 +00:00
|
|
|
return LexDigitOrNegative();
|
2009-01-02 07:01:27 +00:00
|
|
|
case '=': return lltok::equal;
|
|
|
|
case '[': return lltok::lsquare;
|
|
|
|
case ']': return lltok::rsquare;
|
|
|
|
case '{': return lltok::lbrace;
|
|
|
|
case '}': return lltok::rbrace;
|
|
|
|
case '<': return lltok::less;
|
|
|
|
case '>': return lltok::greater;
|
|
|
|
case '(': return lltok::lparen;
|
|
|
|
case ')': return lltok::rparen;
|
|
|
|
case ',': return lltok::comma;
|
|
|
|
case '*': return lltok::star;
|
|
|
|
case '\\': return lltok::backslash;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLLexer::SkipLineComment() {
|
|
|
|
while (1) {
|
|
|
|
if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LexAt - Lex all tokens that start with an @ character:
|
2009-01-02 07:01:27 +00:00
|
|
|
/// GlobalVar @\"[^\"]*\"
|
|
|
|
/// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
|
|
|
|
/// GlobalVarID @[0-9]+
|
|
|
|
lltok::Kind LLLexer::LexAt() {
|
2007-11-18 08:46:26 +00:00
|
|
|
// Handle AtStringConstant: @\"[^\"]*\"
|
|
|
|
if (CurPtr[0] == '"') {
|
|
|
|
++CurPtr;
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
while (1) {
|
|
|
|
int CurChar = getNextChar();
|
2007-12-16 09:16:12 +00:00
|
|
|
|
|
|
|
if (CurChar == EOF) {
|
2009-01-02 07:01:27 +00:00
|
|
|
Error("end of file in global variable name");
|
|
|
|
return lltok::Error;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
if (CurChar == '"') {
|
2009-01-02 07:01:27 +00:00
|
|
|
StrVal.assign(TokStart+2, CurPtr-1);
|
|
|
|
UnEscapeLexed(StrVal);
|
|
|
|
return lltok::GlobalVar;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
|
2011-06-04 18:16:26 +00:00
|
|
|
if (ReadVarName())
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::GlobalVar;
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// Handle GlobalVarID: @[0-9]+
|
|
|
|
if (isdigit(CurPtr[0])) {
|
2007-12-16 09:16:12 +00:00
|
|
|
for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
|
|
|
|
/*empty*/;
|
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
uint64_t Val = atoull(TokStart+1, CurPtr);
|
|
|
|
if ((unsigned)Val != Val)
|
2009-01-02 07:01:27 +00:00
|
|
|
Error("invalid value number (too large)!");
|
|
|
|
UIntVal = unsigned(Val);
|
|
|
|
return lltok::GlobalID;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::Error;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
|
2011-06-04 18:16:26 +00:00
|
|
|
/// ReadString - Read a string until the closing quote.
|
|
|
|
lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
|
|
|
|
const char *Start = CurPtr;
|
|
|
|
while (1) {
|
|
|
|
int CurChar = getNextChar();
|
|
|
|
|
|
|
|
if (CurChar == EOF) {
|
|
|
|
Error("end of file in string constant");
|
|
|
|
return lltok::Error;
|
|
|
|
}
|
|
|
|
if (CurChar == '"') {
|
|
|
|
StrVal.assign(Start, CurPtr-1);
|
|
|
|
UnEscapeLexed(StrVal);
|
|
|
|
return kind;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ReadVarName - Read the rest of a token containing a variable name.
|
|
|
|
bool LLLexer::ReadVarName() {
|
|
|
|
const char *NameStart = CurPtr;
|
|
|
|
if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
|
|
|
|
CurPtr[0] == '.' || CurPtr[0] == '_') {
|
|
|
|
++CurPtr;
|
|
|
|
while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
|
|
|
|
CurPtr[0] == '.' || CurPtr[0] == '_')
|
|
|
|
++CurPtr;
|
|
|
|
|
|
|
|
StrVal.assign(NameStart, CurPtr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2007-11-18 08:46:26 +00:00
|
|
|
|
|
|
|
/// LexPercent - Lex all tokens that start with a % character:
|
2009-01-02 07:01:27 +00:00
|
|
|
/// LocalVar ::= %\"[^\"]*\"
|
|
|
|
/// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
|
|
|
|
/// LocalVarID ::= %[0-9]+
|
|
|
|
lltok::Kind LLLexer::LexPercent() {
|
|
|
|
// Handle LocalVarName: %\"[^\"]*\"
|
2007-11-18 08:46:26 +00:00
|
|
|
if (CurPtr[0] == '"') {
|
|
|
|
++CurPtr;
|
2011-06-04 18:16:26 +00:00
|
|
|
return ReadString(lltok::LocalVar);
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
|
2011-06-04 18:16:26 +00:00
|
|
|
if (ReadVarName())
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::LocalVar;
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// Handle LocalVarID: %[0-9]+
|
|
|
|
if (isdigit(CurPtr[0])) {
|
2007-12-16 09:16:12 +00:00
|
|
|
for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
|
|
|
|
/*empty*/;
|
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
uint64_t Val = atoull(TokStart+1, CurPtr);
|
|
|
|
if ((unsigned)Val != Val)
|
2009-01-02 07:01:27 +00:00
|
|
|
Error("invalid value number (too large)!");
|
|
|
|
UIntVal = unsigned(Val);
|
|
|
|
return lltok::LocalVarID;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::Error;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// LexQuote - Lex all tokens that start with a " character:
|
|
|
|
/// QuoteLabel "[^"]+":
|
|
|
|
/// StringConstant "[^"]*"
|
2009-01-02 07:01:27 +00:00
|
|
|
lltok::Kind LLLexer::LexQuote() {
|
2011-06-04 18:16:26 +00:00
|
|
|
lltok::Kind kind = ReadString(lltok::StringConstant);
|
|
|
|
if (kind == lltok::Error || kind == lltok::Eof)
|
|
|
|
return kind;
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2011-06-04 18:16:26 +00:00
|
|
|
if (CurPtr[0] == ':') {
|
2007-11-18 08:46:26 +00:00
|
|
|
++CurPtr;
|
2011-06-04 18:16:26 +00:00
|
|
|
kind = lltok::LabelStr;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2011-06-04 18:16:26 +00:00
|
|
|
|
|
|
|
return kind;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
|
2009-12-30 04:56:59 +00:00
|
|
|
/// LexExclaim:
|
2009-07-29 00:34:02 +00:00
|
|
|
/// !foo
|
2009-12-30 05:02:06 +00:00
|
|
|
/// !
|
2009-12-30 04:56:59 +00:00
|
|
|
lltok::Kind LLLexer::LexExclaim() {
|
2009-12-30 05:02:06 +00:00
|
|
|
// Lex a metadata name as a MetadataVar.
|
2011-06-15 06:37:58 +00:00
|
|
|
if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
|
|
|
|
CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
|
2009-07-29 00:34:02 +00:00
|
|
|
++CurPtr;
|
|
|
|
while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
|
2011-06-15 06:37:58 +00:00
|
|
|
CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
|
2009-07-29 00:34:02 +00:00
|
|
|
++CurPtr;
|
2007-11-18 08:46:26 +00:00
|
|
|
|
2009-07-29 00:34:02 +00:00
|
|
|
StrVal.assign(TokStart+1, CurPtr); // Skip !
|
2011-06-15 06:37:58 +00:00
|
|
|
UnEscapeLexed(StrVal);
|
2009-12-30 05:02:06 +00:00
|
|
|
return lltok::MetadataVar;
|
2009-07-29 00:34:02 +00:00
|
|
|
}
|
2009-12-30 04:56:59 +00:00
|
|
|
return lltok::exclaim;
|
2009-07-29 00:34:02 +00:00
|
|
|
}
|
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
/// LexIdentifier: Handle several related productions:
|
|
|
|
/// Label [-a-zA-Z$._0-9]+:
|
|
|
|
/// IntegerType i[0-9]+
|
|
|
|
/// Keyword sdiv, float, ...
|
|
|
|
/// HexIntConstant [us]0x[0-9A-Fa-f]+
|
2009-01-02 07:01:27 +00:00
|
|
|
lltok::Kind LLLexer::LexIdentifier() {
|
2007-11-18 08:46:26 +00:00
|
|
|
const char *StartChar = CurPtr;
|
|
|
|
const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
|
|
|
|
const char *KeywordEnd = 0;
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
for (; isLabelChar(*CurPtr); ++CurPtr) {
|
|
|
|
// If we decide this is an integer, remember the end of the sequence.
|
|
|
|
if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
|
|
|
|
if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
|
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// If we stopped due to a colon, this really is a label.
|
|
|
|
if (*CurPtr == ':') {
|
2009-01-02 07:01:27 +00:00
|
|
|
StrVal.assign(StartChar-1, CurPtr++);
|
|
|
|
return lltok::LabelStr;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// Otherwise, this wasn't a label. If this was valid as an integer type,
|
|
|
|
// return it.
|
|
|
|
if (IntEnd == 0) IntEnd = CurPtr;
|
|
|
|
if (IntEnd != StartChar) {
|
|
|
|
CurPtr = IntEnd;
|
|
|
|
uint64_t NumBits = atoull(StartChar, CurPtr);
|
2007-12-16 09:16:12 +00:00
|
|
|
if (NumBits < IntegerType::MIN_INT_BITS ||
|
2007-11-18 08:46:26 +00:00
|
|
|
NumBits > IntegerType::MAX_INT_BITS) {
|
2009-01-02 07:01:27 +00:00
|
|
|
Error("bitwidth for integer type out of range!");
|
|
|
|
return lltok::Error;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2009-08-13 21:58:54 +00:00
|
|
|
TyVal = IntegerType::get(Context, NumBits);
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::Type;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// Otherwise, this was a letter sequence. See which keyword this is.
|
|
|
|
if (KeywordEnd == 0) KeywordEnd = CurPtr;
|
|
|
|
CurPtr = KeywordEnd;
|
|
|
|
--StartChar;
|
|
|
|
unsigned Len = CurPtr-StartChar;
|
2009-01-02 07:01:27 +00:00
|
|
|
#define KEYWORD(STR) \
|
|
|
|
if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
|
|
|
|
return lltok::kw_##STR;
|
|
|
|
|
|
|
|
KEYWORD(true); KEYWORD(false);
|
|
|
|
KEYWORD(declare); KEYWORD(define);
|
|
|
|
KEYWORD(global); KEYWORD(constant);
|
|
|
|
|
2009-01-15 20:18:42 +00:00
|
|
|
KEYWORD(private);
|
2009-07-20 01:03:30 +00:00
|
|
|
KEYWORD(linker_private);
|
2010-07-01 21:55:59 +00:00
|
|
|
KEYWORD(linker_private_weak);
|
2012-08-17 18:33:14 +00:00
|
|
|
KEYWORD(linker_private_weak_def_auto); // FIXME: For backwards compatibility.
|
2009-01-02 07:01:27 +00:00
|
|
|
KEYWORD(internal);
|
2009-04-13 05:44:34 +00:00
|
|
|
KEYWORD(available_externally);
|
2009-01-02 07:01:27 +00:00
|
|
|
KEYWORD(linkonce);
|
Introduce new linkage types linkonce_odr, weak_odr, common_odr
and extern_weak_odr. These are the same as the non-odr versions,
except that they indicate that the global will only be overridden
by an *equivalent* global. In C, a function with weak linkage can
be overridden by a function which behaves completely differently.
This means that IP passes have to skip weak functions, since any
deductions made from the function definition might be wrong, since
the definition could be replaced by something completely different
at link time. This is not allowed in C++, thanks to the ODR
(One-Definition-Rule): if a function is replaced by another at
link-time, then the new function must be the same as the original
function. If a language knows that a function or other global can
only be overridden by an equivalent global, it can give it the
weak_odr linkage type, and the optimizers will understand that it
is alright to make deductions based on the function body. The
code generators on the other hand map weak and weak_odr linkage
to the same thing.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66339 91177308-0d34-0410-b5e6-96231b3b80d8
2009-03-07 15:45:40 +00:00
|
|
|
KEYWORD(linkonce_odr);
|
2012-08-17 18:33:14 +00:00
|
|
|
KEYWORD(linkonce_odr_auto_hide);
|
2009-01-02 07:01:27 +00:00
|
|
|
KEYWORD(weak);
|
Introduce new linkage types linkonce_odr, weak_odr, common_odr
and extern_weak_odr. These are the same as the non-odr versions,
except that they indicate that the global will only be overridden
by an *equivalent* global. In C, a function with weak linkage can
be overridden by a function which behaves completely differently.
This means that IP passes have to skip weak functions, since any
deductions made from the function definition might be wrong, since
the definition could be replaced by something completely different
at link time. This is not allowed in C++, thanks to the ODR
(One-Definition-Rule): if a function is replaced by another at
link-time, then the new function must be the same as the original
function. If a language knows that a function or other global can
only be overridden by an equivalent global, it can give it the
weak_odr linkage type, and the optimizers will understand that it
is alright to make deductions based on the function body. The
code generators on the other hand map weak and weak_odr linkage
to the same thing.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66339 91177308-0d34-0410-b5e6-96231b3b80d8
2009-03-07 15:45:40 +00:00
|
|
|
KEYWORD(weak_odr);
|
2009-01-02 07:01:27 +00:00
|
|
|
KEYWORD(appending);
|
|
|
|
KEYWORD(dllimport);
|
|
|
|
KEYWORD(dllexport);
|
|
|
|
KEYWORD(common);
|
|
|
|
KEYWORD(default);
|
|
|
|
KEYWORD(hidden);
|
|
|
|
KEYWORD(protected);
|
2011-01-08 16:42:36 +00:00
|
|
|
KEYWORD(unnamed_addr);
|
2009-01-02 07:01:27 +00:00
|
|
|
KEYWORD(extern_weak);
|
|
|
|
KEYWORD(external);
|
|
|
|
KEYWORD(thread_local);
|
2012-06-23 11:37:03 +00:00
|
|
|
KEYWORD(localdynamic);
|
|
|
|
KEYWORD(initialexec);
|
|
|
|
KEYWORD(localexec);
|
2009-01-02 07:01:27 +00:00
|
|
|
KEYWORD(zeroinitializer);
|
|
|
|
KEYWORD(undef);
|
|
|
|
KEYWORD(null);
|
|
|
|
KEYWORD(to);
|
|
|
|
KEYWORD(tail);
|
|
|
|
KEYWORD(target);
|
|
|
|
KEYWORD(triple);
|
2012-02-06 21:02:43 +00:00
|
|
|
KEYWORD(unwind);
|
2009-01-02 07:01:27 +00:00
|
|
|
KEYWORD(deplibs);
|
|
|
|
KEYWORD(datalayout);
|
|
|
|
KEYWORD(volatile);
|
2011-07-25 23:16:38 +00:00
|
|
|
KEYWORD(atomic);
|
|
|
|
KEYWORD(unordered);
|
|
|
|
KEYWORD(monotonic);
|
|
|
|
KEYWORD(acquire);
|
|
|
|
KEYWORD(release);
|
|
|
|
KEYWORD(acq_rel);
|
|
|
|
KEYWORD(seq_cst);
|
|
|
|
KEYWORD(singlethread);
|
|
|
|
|
2009-07-22 22:44:56 +00:00
|
|
|
KEYWORD(nuw);
|
|
|
|
KEYWORD(nsw);
|
2009-07-20 21:19:07 +00:00
|
|
|
KEYWORD(exact);
|
2009-07-27 21:53:46 +00:00
|
|
|
KEYWORD(inbounds);
|
2009-01-02 07:01:27 +00:00
|
|
|
KEYWORD(align);
|
|
|
|
KEYWORD(addrspace);
|
|
|
|
KEYWORD(section);
|
|
|
|
KEYWORD(alias);
|
|
|
|
KEYWORD(module);
|
|
|
|
KEYWORD(asm);
|
|
|
|
KEYWORD(sideeffect);
|
2009-10-21 23:28:00 +00:00
|
|
|
KEYWORD(alignstack);
|
2012-09-05 00:51:02 +00:00
|
|
|
KEYWORD(nsdialect);
|
2009-01-02 07:01:27 +00:00
|
|
|
KEYWORD(gc);
|
|
|
|
|
|
|
|
KEYWORD(ccc);
|
|
|
|
KEYWORD(fastcc);
|
|
|
|
KEYWORD(coldcc);
|
|
|
|
KEYWORD(x86_stdcallcc);
|
|
|
|
KEYWORD(x86_fastcallcc);
|
2010-05-16 09:08:45 +00:00
|
|
|
KEYWORD(x86_thiscallcc);
|
2009-06-16 18:50:49 +00:00
|
|
|
KEYWORD(arm_apcscc);
|
|
|
|
KEYWORD(arm_aapcscc);
|
|
|
|
KEYWORD(arm_aapcs_vfpcc);
|
2009-12-07 02:27:35 +00:00
|
|
|
KEYWORD(msp430_intrcc);
|
2010-09-25 07:46:17 +00:00
|
|
|
KEYWORD(ptx_kernel);
|
|
|
|
KEYWORD(ptx_device);
|
2009-06-16 18:50:49 +00:00
|
|
|
|
2009-01-02 07:01:27 +00:00
|
|
|
KEYWORD(cc);
|
|
|
|
KEYWORD(c);
|
|
|
|
|
|
|
|
KEYWORD(signext);
|
|
|
|
KEYWORD(zeroext);
|
|
|
|
KEYWORD(inreg);
|
|
|
|
KEYWORD(sret);
|
|
|
|
KEYWORD(nounwind);
|
|
|
|
KEYWORD(noreturn);
|
|
|
|
KEYWORD(noalias);
|
|
|
|
KEYWORD(nocapture);
|
|
|
|
KEYWORD(byval);
|
|
|
|
KEYWORD(nest);
|
|
|
|
KEYWORD(readnone);
|
|
|
|
KEYWORD(readonly);
|
2011-05-25 03:44:17 +00:00
|
|
|
KEYWORD(uwtable);
|
2011-10-03 14:45:37 +00:00
|
|
|
KEYWORD(returns_twice);
|
2009-01-02 07:01:27 +00:00
|
|
|
|
2010-02-06 01:16:28 +00:00
|
|
|
KEYWORD(inlinehint);
|
2009-01-02 07:01:27 +00:00
|
|
|
KEYWORD(noinline);
|
|
|
|
KEYWORD(alwaysinline);
|
|
|
|
KEYWORD(optsize);
|
|
|
|
KEYWORD(ssp);
|
|
|
|
KEYWORD(sspreq);
|
2009-06-04 22:05:33 +00:00
|
|
|
KEYWORD(noredzone);
|
2009-06-05 21:57:13 +00:00
|
|
|
KEYWORD(noimplicitfloat);
|
2009-07-17 18:07:26 +00:00
|
|
|
KEYWORD(naked);
|
2011-06-15 20:36:13 +00:00
|
|
|
KEYWORD(nonlazybind);
|
2012-01-20 17:56:17 +00:00
|
|
|
KEYWORD(address_safety);
|
2009-01-02 07:01:27 +00:00
|
|
|
|
|
|
|
KEYWORD(type);
|
|
|
|
KEYWORD(opaque);
|
|
|
|
|
|
|
|
KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
|
|
|
|
KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
|
|
|
|
KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
|
|
|
|
KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
|
2009-01-02 22:46:48 +00:00
|
|
|
|
2011-07-28 21:48:00 +00:00
|
|
|
KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
|
|
|
|
KEYWORD(umin);
|
|
|
|
|
2009-01-02 07:01:27 +00:00
|
|
|
KEYWORD(x);
|
2009-10-28 03:39:23 +00:00
|
|
|
KEYWORD(blockaddress);
|
2011-08-12 20:24:12 +00:00
|
|
|
|
|
|
|
KEYWORD(personality);
|
|
|
|
KEYWORD(cleanup);
|
|
|
|
KEYWORD(catch);
|
|
|
|
KEYWORD(filter);
|
2007-11-18 08:46:26 +00:00
|
|
|
#undef KEYWORD
|
|
|
|
|
|
|
|
// Keywords for types.
|
2009-01-02 07:01:27 +00:00
|
|
|
#define TYPEKEYWORD(STR, LLVMTY) \
|
2007-11-18 08:46:26 +00:00
|
|
|
if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
|
2009-01-02 07:01:27 +00:00
|
|
|
TyVal = LLVMTY; return lltok::Type; }
|
2009-08-13 21:58:54 +00:00
|
|
|
TYPEKEYWORD("void", Type::getVoidTy(Context));
|
2011-12-17 00:04:22 +00:00
|
|
|
TYPEKEYWORD("half", Type::getHalfTy(Context));
|
2009-08-13 21:58:54 +00:00
|
|
|
TYPEKEYWORD("float", Type::getFloatTy(Context));
|
|
|
|
TYPEKEYWORD("double", Type::getDoubleTy(Context));
|
|
|
|
TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
|
|
|
|
TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
|
|
|
|
TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
|
|
|
|
TYPEKEYWORD("label", Type::getLabelTy(Context));
|
|
|
|
TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
|
2010-09-10 20:55:01 +00:00
|
|
|
TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
|
2007-11-18 08:46:26 +00:00
|
|
|
#undef TYPEKEYWORD
|
|
|
|
|
|
|
|
// Keywords for instructions.
|
2009-01-02 07:01:27 +00:00
|
|
|
#define INSTKEYWORD(STR, Enum) \
|
|
|
|
if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
|
|
|
|
UIntVal = Instruction::Enum; return lltok::kw_##STR; }
|
|
|
|
|
2009-06-04 22:49:04 +00:00
|
|
|
INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
|
|
|
|
INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
|
|
|
|
INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
|
2009-01-02 07:01:27 +00:00
|
|
|
INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
|
|
|
|
INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
|
|
|
|
INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
|
|
|
|
INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
|
|
|
|
INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
|
|
|
|
|
|
|
|
INSTKEYWORD(phi, PHI);
|
|
|
|
INSTKEYWORD(call, Call);
|
|
|
|
INSTKEYWORD(trunc, Trunc);
|
|
|
|
INSTKEYWORD(zext, ZExt);
|
|
|
|
INSTKEYWORD(sext, SExt);
|
|
|
|
INSTKEYWORD(fptrunc, FPTrunc);
|
|
|
|
INSTKEYWORD(fpext, FPExt);
|
|
|
|
INSTKEYWORD(uitofp, UIToFP);
|
|
|
|
INSTKEYWORD(sitofp, SIToFP);
|
|
|
|
INSTKEYWORD(fptoui, FPToUI);
|
|
|
|
INSTKEYWORD(fptosi, FPToSI);
|
|
|
|
INSTKEYWORD(inttoptr, IntToPtr);
|
|
|
|
INSTKEYWORD(ptrtoint, PtrToInt);
|
|
|
|
INSTKEYWORD(bitcast, BitCast);
|
|
|
|
INSTKEYWORD(select, Select);
|
|
|
|
INSTKEYWORD(va_arg, VAArg);
|
|
|
|
INSTKEYWORD(ret, Ret);
|
|
|
|
INSTKEYWORD(br, Br);
|
|
|
|
INSTKEYWORD(switch, Switch);
|
2009-10-28 00:19:10 +00:00
|
|
|
INSTKEYWORD(indirectbr, IndirectBr);
|
2009-01-02 07:01:27 +00:00
|
|
|
INSTKEYWORD(invoke, Invoke);
|
2011-07-31 06:30:59 +00:00
|
|
|
INSTKEYWORD(resume, Resume);
|
2009-01-02 07:01:27 +00:00
|
|
|
INSTKEYWORD(unreachable, Unreachable);
|
|
|
|
|
|
|
|
INSTKEYWORD(alloca, Alloca);
|
|
|
|
INSTKEYWORD(load, Load);
|
|
|
|
INSTKEYWORD(store, Store);
|
2011-07-28 21:48:00 +00:00
|
|
|
INSTKEYWORD(cmpxchg, AtomicCmpXchg);
|
|
|
|
INSTKEYWORD(atomicrmw, AtomicRMW);
|
2011-07-25 23:16:38 +00:00
|
|
|
INSTKEYWORD(fence, Fence);
|
2009-01-02 07:01:27 +00:00
|
|
|
INSTKEYWORD(getelementptr, GetElementPtr);
|
|
|
|
|
|
|
|
INSTKEYWORD(extractelement, ExtractElement);
|
|
|
|
INSTKEYWORD(insertelement, InsertElement);
|
|
|
|
INSTKEYWORD(shufflevector, ShuffleVector);
|
|
|
|
INSTKEYWORD(extractvalue, ExtractValue);
|
|
|
|
INSTKEYWORD(insertvalue, InsertValue);
|
2011-08-12 20:24:12 +00:00
|
|
|
INSTKEYWORD(landingpad, LandingPad);
|
2007-12-16 09:16:12 +00:00
|
|
|
#undef INSTKEYWORD
|
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
|
|
|
|
// the CFE to avoid forcing it to deal with 64-bit numbers.
|
|
|
|
if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
|
|
|
|
TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
|
|
|
|
int len = CurPtr-TokStart-3;
|
|
|
|
uint32_t bits = len * 4;
|
2009-08-13 02:33:34 +00:00
|
|
|
APInt Tmp(bits, StringRef(TokStart+3, len), 16);
|
2007-11-18 08:46:26 +00:00
|
|
|
uint32_t activeBits = Tmp.getActiveBits();
|
|
|
|
if (activeBits > 0 && activeBits < bits)
|
2010-12-07 08:25:19 +00:00
|
|
|
Tmp = Tmp.trunc(activeBits);
|
2009-01-02 07:01:27 +00:00
|
|
|
APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
|
|
|
|
return lltok::APSInt;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 18:43:24 +00:00
|
|
|
// If this is "cc1234", return this as just "cc".
|
2007-11-18 08:46:26 +00:00
|
|
|
if (TokStart[0] == 'c' && TokStart[1] == 'c') {
|
|
|
|
CurPtr = TokStart+2;
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::kw_cc;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2009-01-02 07:01:27 +00:00
|
|
|
// Finally, if this isn't known, return an error.
|
2007-11-18 08:46:26 +00:00
|
|
|
CurPtr = TokStart+1;
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::Error;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
|
Revert r136253, r136263, r136269, r136313, r136325, r136326, r136329, r136338,
r136339, r136341, r136369, r136387, r136392, r136396, r136429, r136430, r136444,
r136445, r136446, r136253 pending review.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@136556 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-30 05:42:50 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
/// Lex0x: Handle productions that start with 0x, knowing that it matches and
|
|
|
|
/// that this is not a label:
|
|
|
|
/// HexFPConstant 0x[0-9A-Fa-f]+
|
|
|
|
/// HexFP80Constant 0xK[0-9A-Fa-f]+
|
|
|
|
/// HexFP128Constant 0xL[0-9A-Fa-f]+
|
|
|
|
/// HexPPC128Constant 0xM[0-9A-Fa-f]+
|
2012-05-24 15:59:06 +00:00
|
|
|
/// HexHalfConstant 0xH[0-9A-Fa-f]+
|
2009-01-02 07:01:27 +00:00
|
|
|
lltok::Kind LLLexer::Lex0x() {
|
2007-11-18 08:46:26 +00:00
|
|
|
CurPtr = TokStart + 2;
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
char Kind;
|
2012-05-24 17:11:00 +00:00
|
|
|
if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
|
2007-11-18 08:46:26 +00:00
|
|
|
Kind = *CurPtr++;
|
|
|
|
} else {
|
|
|
|
Kind = 'J';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isxdigit(CurPtr[0])) {
|
2009-01-02 07:01:27 +00:00
|
|
|
// Bad token, return it as an error.
|
2007-11-18 08:46:26 +00:00
|
|
|
CurPtr = TokStart+1;
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::Error;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
while (isxdigit(CurPtr[0]))
|
|
|
|
++CurPtr;
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
if (Kind == 'J') {
|
|
|
|
// HexFPConstant - Floating point constant represented in IEEE format as a
|
|
|
|
// hexadecimal number for when exponential notation is not precise enough.
|
2011-12-17 00:04:22 +00:00
|
|
|
// Half, Float, and double only.
|
2009-01-02 07:01:27 +00:00
|
|
|
APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
|
|
|
|
return lltok::APFloat;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
uint64_t Pair[2];
|
|
|
|
switch (Kind) {
|
2009-07-14 16:55:14 +00:00
|
|
|
default: llvm_unreachable("Unknown kind!");
|
2007-11-18 08:46:26 +00:00
|
|
|
case 'K':
|
|
|
|
// F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
|
2009-03-23 21:16:53 +00:00
|
|
|
FP80HexToIntPair(TokStart+3, CurPtr, Pair);
|
2011-07-18 21:45:40 +00:00
|
|
|
APFloatVal = APFloat(APInt(80, Pair));
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::APFloat;
|
2007-11-18 08:46:26 +00:00
|
|
|
case 'L':
|
|
|
|
// F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
|
2009-03-23 21:16:53 +00:00
|
|
|
HexToIntPair(TokStart+3, CurPtr, Pair);
|
2011-07-18 21:45:40 +00:00
|
|
|
APFloatVal = APFloat(APInt(128, Pair), true);
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::APFloat;
|
2007-11-18 08:46:26 +00:00
|
|
|
case 'M':
|
|
|
|
// PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
|
2009-03-23 21:16:53 +00:00
|
|
|
HexToIntPair(TokStart+3, CurPtr, Pair);
|
2011-07-18 21:45:40 +00:00
|
|
|
APFloatVal = APFloat(APInt(128, Pair));
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::APFloat;
|
2012-05-24 15:59:06 +00:00
|
|
|
case 'H':
|
|
|
|
APFloatVal = APFloat(APInt(16,HexIntToVal(TokStart+3, CurPtr)));
|
|
|
|
return lltok::APFloat;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LexIdentifier: Handle several related productions:
|
|
|
|
/// Label [-a-zA-Z$._0-9]+:
|
|
|
|
/// NInteger -[0-9]+
|
|
|
|
/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
|
|
|
|
/// PInteger [0-9]+
|
|
|
|
/// HexFPConstant 0x[0-9A-Fa-f]+
|
|
|
|
/// HexFP80Constant 0xK[0-9A-Fa-f]+
|
|
|
|
/// HexFP128Constant 0xL[0-9A-Fa-f]+
|
|
|
|
/// HexPPC128Constant 0xM[0-9A-Fa-f]+
|
2009-01-02 07:01:27 +00:00
|
|
|
lltok::Kind LLLexer::LexDigitOrNegative() {
|
2007-11-18 08:46:26 +00:00
|
|
|
// If the letter after the negative is a number, this is probably a label.
|
|
|
|
if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
|
|
|
|
// Okay, this is not a number after the -, it's probably a label.
|
|
|
|
if (const char *End = isLabelTail(CurPtr)) {
|
2009-01-02 07:01:27 +00:00
|
|
|
StrVal.assign(TokStart, End-1);
|
2007-11-18 08:46:26 +00:00
|
|
|
CurPtr = End;
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::LabelStr;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::Error;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// At this point, it is either a label, int or fp constant.
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// Skip digits, we have at least one.
|
2007-12-16 09:16:12 +00:00
|
|
|
for (; isdigit(CurPtr[0]); ++CurPtr)
|
|
|
|
/*empty*/;
|
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// Check to see if this really is a label afterall, e.g. "-1:".
|
|
|
|
if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
|
|
|
|
if (const char *End = isLabelTail(CurPtr)) {
|
2009-01-02 07:01:27 +00:00
|
|
|
StrVal.assign(TokStart, End-1);
|
2007-11-18 08:46:26 +00:00
|
|
|
CurPtr = End;
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::LabelStr;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// If the next character is a '.', then it is a fp value, otherwise its
|
|
|
|
// integer.
|
|
|
|
if (CurPtr[0] != '.') {
|
|
|
|
if (TokStart[0] == '0' && TokStart[1] == 'x')
|
|
|
|
return Lex0x();
|
|
|
|
unsigned Len = CurPtr-TokStart;
|
|
|
|
uint32_t numBits = ((Len * 64) / 19) + 2;
|
2009-08-13 02:33:34 +00:00
|
|
|
APInt Tmp(numBits, StringRef(TokStart, Len), 10);
|
2007-11-18 08:46:26 +00:00
|
|
|
if (TokStart[0] == '-') {
|
|
|
|
uint32_t minBits = Tmp.getMinSignedBits();
|
|
|
|
if (minBits > 0 && minBits < numBits)
|
2010-12-07 08:25:19 +00:00
|
|
|
Tmp = Tmp.trunc(minBits);
|
2009-01-02 07:01:27 +00:00
|
|
|
APSIntVal = APSInt(Tmp, false);
|
2007-11-18 08:46:26 +00:00
|
|
|
} else {
|
|
|
|
uint32_t activeBits = Tmp.getActiveBits();
|
|
|
|
if (activeBits > 0 && activeBits < numBits)
|
2010-12-07 08:25:19 +00:00
|
|
|
Tmp = Tmp.trunc(activeBits);
|
2009-01-02 07:01:27 +00:00
|
|
|
APSIntVal = APSInt(Tmp, true);
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::APSInt;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++CurPtr;
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// Skip over [0-9]*([eE][-+]?[0-9]+)?
|
|
|
|
while (isdigit(CurPtr[0])) ++CurPtr;
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
|
2007-12-16 09:16:12 +00:00
|
|
|
if (isdigit(CurPtr[1]) ||
|
2007-11-18 08:46:26 +00:00
|
|
|
((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
|
|
|
|
CurPtr += 2;
|
|
|
|
while (isdigit(CurPtr[0])) ++CurPtr;
|
|
|
|
}
|
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2010-12-19 20:42:43 +00:00
|
|
|
APFloatVal = APFloat(std::atof(TokStart));
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::APFloat;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
|
2009-01-02 07:01:27 +00:00
|
|
|
lltok::Kind LLLexer::LexPositive() {
|
2007-11-18 08:46:26 +00:00
|
|
|
// If the letter after the negative is a number, this is probably not a
|
|
|
|
// label.
|
|
|
|
if (!isdigit(CurPtr[0]))
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::Error;
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// Skip digits.
|
2007-12-16 09:16:12 +00:00
|
|
|
for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
|
|
|
|
/*empty*/;
|
2007-11-18 08:46:26 +00:00
|
|
|
|
|
|
|
// At this point, we need a '.'.
|
|
|
|
if (CurPtr[0] != '.') {
|
|
|
|
CurPtr = TokStart+1;
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::Error;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
++CurPtr;
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
// Skip over [0-9]*([eE][-+]?[0-9]+)?
|
|
|
|
while (isdigit(CurPtr[0])) ++CurPtr;
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2007-11-18 08:46:26 +00:00
|
|
|
if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
|
2007-12-16 09:16:12 +00:00
|
|
|
if (isdigit(CurPtr[1]) ||
|
2007-11-18 08:46:26 +00:00
|
|
|
((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
|
|
|
|
CurPtr += 2;
|
|
|
|
while (isdigit(CurPtr[0])) ++CurPtr;
|
|
|
|
}
|
|
|
|
}
|
2007-12-16 09:16:12 +00:00
|
|
|
|
2010-12-19 20:42:43 +00:00
|
|
|
APFloatVal = APFloat(std::atof(TokStart));
|
2009-01-02 07:01:27 +00:00
|
|
|
return lltok::APFloat;
|
2007-11-18 08:46:26 +00:00
|
|
|
}
|