#include "LLLexer.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Instruction.h"
+#include "llvm/LLVMContext.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/Assembly/Parser.h"
+#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/MathExtras.h"
+#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
-#include "llvm/Assembly/Parser.h"
+#include <cctype>
+#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace llvm;
-bool LLLexer::Error(LocTy ErrorLoc, const std::string &Msg) const {
- // Scan backward to find the start of the line.
- const char *LineStart = ErrorLoc;
- while (LineStart != CurBuf->getBufferStart() &&
- LineStart[-1] != '\n' && LineStart[-1] != '\r')
- --LineStart;
- // Get the end of the line.
- const char *LineEnd = ErrorLoc;
- while (LineEnd != CurBuf->getBufferEnd() &&
- LineEnd[0] != '\n' && LineEnd[0] != '\r')
- ++LineEnd;
-
- unsigned LineNo = 1;
- for (const char *FP = CurBuf->getBufferStart(); FP != ErrorLoc; ++FP)
- if (*FP == '\n') ++LineNo;
-
- std::string LineContents(LineStart, LineEnd);
- ErrorInfo.setError(Msg, LineNo, ErrorLoc-LineStart, LineContents);
+bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
+ ErrorInfo = SM.GetMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
return true;
}
return Result;
}
+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;
+}
+
uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
uint64_t Result = 0;
for (; Buffer != End; ++Buffer) {
uint64_t OldRes = Result;
Result *= 16;
- char C = *Buffer;
- if (C >= '0' && C <= '9')
- Result += C-'0';
- else if (C >= 'A' && C <= 'F')
- Result += C-'A'+10;
- else if (C >= 'a' && C <= 'f')
- Result += C-'a'+10;
+ Result += parseHexChar(*Buffer);
if (Result < OldRes) { // Uh, oh, overflow detected!!!
Error("constant bigger than 64 bits detected!");
for (int i=0; i<16; i++, Buffer++) {
assert(Buffer != End);
Pair[0] *= 16;
- char C = *Buffer;
- if (C >= '0' && C <= '9')
- Pair[0] += C-'0';
- else if (C >= 'A' && C <= 'F')
- Pair[0] += C-'A'+10;
- else if (C >= 'a' && C <= 'f')
- Pair[0] += C-'a'+10;
+ Pair[0] += parseHexChar(*Buffer);
}
Pair[1] = 0;
for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
Pair[1] *= 16;
- char C = *Buffer;
- if (C >= '0' && C <= '9')
- Pair[1] += C-'0';
- else if (C >= 'A' && C <= 'F')
- Pair[1] += C-'A'+10;
- else if (C >= 'a' && C <= 'f')
- Pair[1] += C-'a'+10;
+ Pair[1] += parseHexChar(*Buffer);
+ }
+ if (Buffer != End)
+ Error("constant bigger than 128 bits detected!");
+}
+
+/// 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;
+ Pair[1] += parseHexChar(*Buffer);
+ }
+ Pair[0] = 0;
+ for (int i=0; i<16; i++, Buffer++) {
+ Pair[0] *= 16;
+ Pair[0] += parseHexChar(*Buffer);
}
if (Buffer != End)
Error("constant bigger than 128 bits detected!");
*BOut++ = '\\'; // Two \ becomes one
BIn += 2;
} else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
- char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
- *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
- BIn[3] = Tmp; // Restore character
+ *BOut = parseHexChar(BIn[1]) * 16 + parseHexChar(BIn[2]);
BIn += 3; // Skip over handled chars
++BOut;
} else {
// Lexer definition.
//===----------------------------------------------------------------------===//
-LLLexer::LLLexer(MemoryBuffer *StartBuf, ParseError &Err)
- : CurBuf(StartBuf), ErrorInfo(Err), APFloatVal(0.0) {
+LLLexer::LLLexer(MemoryBuffer *StartBuf, SourceMgr &sm, SMDiagnostic &Err,
+ LLVMContext &C)
+ : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
CurPtr = CurBuf->getBufferStart();
}
case ';':
SkipLineComment();
return LexToken();
+ case '!': return LexExclaim();
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '-':
}
// Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
- 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(TokStart+1, CurPtr); // Skip @
+ if (ReadVarName())
return lltok::GlobalVar;
- }
// Handle GlobalVarID: @[0-9]+
if (isdigit(CurPtr[0])) {
return lltok::Error;
}
+/// 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;
+}
/// LexPercent - Lex all tokens that start with a % character:
/// LocalVar ::= %\"[^\"]*\"
// Handle LocalVarName: %\"[^\"]*\"
if (CurPtr[0] == '"') {
++CurPtr;
-
- while (1) {
- int CurChar = getNextChar();
-
- if (CurChar == EOF) {
- Error("end of file in string constant");
- return lltok::Error;
- }
- if (CurChar == '"') {
- StrVal.assign(TokStart+2, CurPtr-1);
- UnEscapeLexed(StrVal);
- return lltok::LocalVar;
- }
- }
+ return ReadString(lltok::LocalVar);
}
// Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
- 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(TokStart+1, CurPtr); // Skip %
+ if (ReadVarName())
return lltok::LocalVar;
- }
// Handle LocalVarID: %[0-9]+
if (isdigit(CurPtr[0])) {
/// QuoteLabel "[^"]+":
/// StringConstant "[^"]*"
lltok::Kind LLLexer::LexQuote() {
- while (1) {
- int CurChar = getNextChar();
-
- if (CurChar == EOF) {
- Error("end of file in quoted string");
- return lltok::Error;
- }
-
- if (CurChar != '"') continue;
-
- if (CurPtr[0] != ':') {
- StrVal.assign(TokStart+1, CurPtr-1);
- UnEscapeLexed(StrVal);
- return lltok::StringConstant;
- }
+ lltok::Kind kind = ReadString(lltok::StringConstant);
+ if (kind == lltok::Error || kind == lltok::Eof)
+ return kind;
+ if (CurPtr[0] == ':') {
++CurPtr;
- StrVal.assign(TokStart+1, CurPtr-2);
- UnEscapeLexed(StrVal);
- return lltok::LabelStr;
+ kind = lltok::LabelStr;
}
-}
-static bool JustWhitespaceNewLine(const char *&Ptr) {
- const char *ThisPtr = Ptr;
- while (*ThisPtr == ' ' || *ThisPtr == '\t')
- ++ThisPtr;
- if (*ThisPtr == '\n' || *ThisPtr == '\r') {
- Ptr = ThisPtr;
- return true;
- }
- return false;
+ return kind;
}
+/// LexExclaim:
+/// !foo
+/// !
+lltok::Kind LLLexer::LexExclaim() {
+ // Lex a metadata name as a MetadataVar.
+ if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
+ CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
+ ++CurPtr;
+ while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
+ CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
+ ++CurPtr;
+ StrVal.assign(TokStart+1, CurPtr); // Skip !
+ UnEscapeLexed(StrVal);
+ return lltok::MetadataVar;
+ }
+ return lltok::exclaim;
+}
+
/// LexIdentifier: Handle several related productions:
/// Label [-a-zA-Z$._0-9]+:
/// IntegerType i[0-9]+
Error("bitwidth for integer type out of range!");
return lltok::Error;
}
- TyVal = IntegerType::get(NumBits);
+ TyVal = IntegerType::get(Context, NumBits);
return lltok::Type;
}
if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
return lltok::kw_##STR;
- KEYWORD(begin); KEYWORD(end);
KEYWORD(true); KEYWORD(false);
KEYWORD(declare); KEYWORD(define);
KEYWORD(global); KEYWORD(constant);
KEYWORD(private);
+ KEYWORD(linker_private);
+ KEYWORD(linker_private_weak);
+ KEYWORD(linker_private_weak_def_auto);
KEYWORD(internal);
+ KEYWORD(available_externally);
KEYWORD(linkonce);
+ KEYWORD(linkonce_odr);
KEYWORD(weak);
+ KEYWORD(weak_odr);
KEYWORD(appending);
KEYWORD(dllimport);
KEYWORD(dllexport);
KEYWORD(default);
KEYWORD(hidden);
KEYWORD(protected);
+ KEYWORD(unnamed_addr);
KEYWORD(extern_weak);
KEYWORD(external);
KEYWORD(thread_local);
KEYWORD(tail);
KEYWORD(target);
KEYWORD(triple);
+ KEYWORD(unwind);
KEYWORD(deplibs);
KEYWORD(datalayout);
KEYWORD(volatile);
+ KEYWORD(atomic);
+ KEYWORD(unordered);
+ KEYWORD(monotonic);
+ KEYWORD(acquire);
+ KEYWORD(release);
+ KEYWORD(acq_rel);
+ KEYWORD(seq_cst);
+ KEYWORD(singlethread);
+
+ KEYWORD(nuw);
+ KEYWORD(nsw);
+ KEYWORD(exact);
+ KEYWORD(inbounds);
KEYWORD(align);
KEYWORD(addrspace);
KEYWORD(section);
KEYWORD(module);
KEYWORD(asm);
KEYWORD(sideeffect);
+ KEYWORD(alignstack);
KEYWORD(gc);
KEYWORD(ccc);
KEYWORD(coldcc);
KEYWORD(x86_stdcallcc);
KEYWORD(x86_fastcallcc);
+ KEYWORD(x86_thiscallcc);
+ KEYWORD(arm_apcscc);
+ KEYWORD(arm_aapcscc);
+ KEYWORD(arm_aapcs_vfpcc);
+ KEYWORD(msp430_intrcc);
+ KEYWORD(ptx_kernel);
+ KEYWORD(ptx_device);
+
KEYWORD(cc);
KEYWORD(c);
KEYWORD(nest);
KEYWORD(readnone);
KEYWORD(readonly);
+ KEYWORD(uwtable);
+ KEYWORD(returns_twice);
+ KEYWORD(inlinehint);
KEYWORD(noinline);
KEYWORD(alwaysinline);
KEYWORD(optsize);
KEYWORD(ssp);
KEYWORD(sspreq);
+ KEYWORD(noredzone);
+ KEYWORD(noimplicitfloat);
+ KEYWORD(naked);
+ KEYWORD(nonlazybind);
+ KEYWORD(address_safety);
KEYWORD(type);
KEYWORD(opaque);
KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
+ KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
+ KEYWORD(umin);
+
KEYWORD(x);
+ KEYWORD(blockaddress);
+
+ KEYWORD(personality);
+ KEYWORD(cleanup);
+ KEYWORD(catch);
+ KEYWORD(filter);
#undef KEYWORD
// Keywords for types.
#define TYPEKEYWORD(STR, LLVMTY) \
if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
TyVal = LLVMTY; return lltok::Type; }
- TYPEKEYWORD("void", Type::VoidTy);
- TYPEKEYWORD("float", Type::FloatTy);
- TYPEKEYWORD("double", Type::DoubleTy);
- TYPEKEYWORD("x86_fp80", Type::X86_FP80Ty);
- TYPEKEYWORD("fp128", Type::FP128Ty);
- TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty);
- TYPEKEYWORD("label", Type::LabelTy);
+ TYPEKEYWORD("void", Type::getVoidTy(Context));
+ TYPEKEYWORD("half", Type::getHalfTy(Context));
+ 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));
+ TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
#undef TYPEKEYWORD
- // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
- // to avoid conflicting with the sext/zext instructions, below.
- if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
- // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
- if (JustWhitespaceNewLine(CurPtr))
- return lltok::kw_signext;
- } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) {
- // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
- if (JustWhitespaceNewLine(CurPtr))
- return lltok::kw_zeroext;
- }
-
// Keywords for instructions.
#define INSTKEYWORD(STR, Enum) \
if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
UIntVal = Instruction::Enum; return lltok::kw_##STR; }
- INSTKEYWORD(add, Add); INSTKEYWORD(sub, Sub); INSTKEYWORD(mul, Mul);
+ INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
+ INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
+ INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
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(vicmp, VICmp); INSTKEYWORD(vfcmp, VFCmp);
INSTKEYWORD(phi, PHI);
INSTKEYWORD(call, Call);
INSTKEYWORD(ret, Ret);
INSTKEYWORD(br, Br);
INSTKEYWORD(switch, Switch);
+ INSTKEYWORD(indirectbr, IndirectBr);
INSTKEYWORD(invoke, Invoke);
- INSTKEYWORD(unwind, Unwind);
+ INSTKEYWORD(resume, Resume);
INSTKEYWORD(unreachable, Unreachable);
- INSTKEYWORD(malloc, Malloc);
INSTKEYWORD(alloca, Alloca);
- INSTKEYWORD(free, Free);
INSTKEYWORD(load, Load);
INSTKEYWORD(store, Store);
+ INSTKEYWORD(cmpxchg, AtomicCmpXchg);
+ INSTKEYWORD(atomicrmw, AtomicRMW);
+ INSTKEYWORD(fence, Fence);
INSTKEYWORD(getelementptr, GetElementPtr);
INSTKEYWORD(extractelement, ExtractElement);
INSTKEYWORD(insertelement, InsertElement);
INSTKEYWORD(shufflevector, ShuffleVector);
- INSTKEYWORD(getresult, ExtractValue);
INSTKEYWORD(extractvalue, ExtractValue);
INSTKEYWORD(insertvalue, InsertValue);
+ INSTKEYWORD(landingpad, LandingPad);
#undef INSTKEYWORD
// Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
int len = CurPtr-TokStart-3;
uint32_t bits = len * 4;
- APInt Tmp(bits, TokStart+3, len, 16);
+ APInt Tmp(bits, StringRef(TokStart+3, len), 16);
uint32_t activeBits = Tmp.getActiveBits();
if (activeBits > 0 && activeBits < bits)
- Tmp.trunc(activeBits);
+ Tmp = Tmp.trunc(activeBits);
APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
return lltok::APSInt;
}
return lltok::kw_cc;
}
- // If this starts with "call", return it as CALL. This is to support old
- // broken .ll files. FIXME: remove this with LLVM 3.0.
- if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
- CurPtr = TokStart+4;
- UIntVal = Instruction::Call;
- return lltok::kw_call;
- }
-
// Finally, if this isn't known, return an error.
CurPtr = TokStart+1;
return lltok::Error;
/// HexFP80Constant 0xK[0-9A-Fa-f]+
/// HexFP128Constant 0xL[0-9A-Fa-f]+
/// HexPPC128Constant 0xM[0-9A-Fa-f]+
+/// HexHalfConstant 0xH[0-9A-Fa-f]+
lltok::Kind LLLexer::Lex0x() {
CurPtr = TokStart + 2;
char Kind;
- if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
+ if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
Kind = *CurPtr++;
} else {
Kind = 'J';
if (Kind == 'J') {
// HexFPConstant - Floating point constant represented in IEEE format as a
// hexadecimal number for when exponential notation is not precise enough.
- // Float and double only.
+ // Half, Float, and double only.
APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
return lltok::APFloat;
}
uint64_t Pair[2];
- HexToIntPair(TokStart+3, CurPtr, Pair);
switch (Kind) {
- default: assert(0 && "Unknown kind!");
+ default: llvm_unreachable("Unknown kind!");
case 'K':
// F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
- APFloatVal = APFloat(APInt(80, 2, Pair));
+ FP80HexToIntPair(TokStart+3, CurPtr, Pair);
+ APFloatVal = APFloat(APInt(80, Pair));
return lltok::APFloat;
case 'L':
// F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
- APFloatVal = APFloat(APInt(128, 2, Pair), true);
+ HexToIntPair(TokStart+3, CurPtr, Pair);
+ APFloatVal = APFloat(APInt(128, Pair), true);
return lltok::APFloat;
case 'M':
// PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
- APFloatVal = APFloat(APInt(128, 2, Pair));
+ HexToIntPair(TokStart+3, CurPtr, Pair);
+ APFloatVal = APFloat(APInt(128, Pair));
+ return lltok::APFloat;
+ case 'H':
+ APFloatVal = APFloat(APInt(16,HexIntToVal(TokStart+3, CurPtr)));
return lltok::APFloat;
}
}
return Lex0x();
unsigned Len = CurPtr-TokStart;
uint32_t numBits = ((Len * 64) / 19) + 2;
- APInt Tmp(numBits, TokStart, Len, 10);
+ APInt Tmp(numBits, StringRef(TokStart, Len), 10);
if (TokStart[0] == '-') {
uint32_t minBits = Tmp.getMinSignedBits();
if (minBits > 0 && minBits < numBits)
- Tmp.trunc(minBits);
+ Tmp = Tmp.trunc(minBits);
APSIntVal = APSInt(Tmp, false);
} else {
uint32_t activeBits = Tmp.getActiveBits();
if (activeBits > 0 && activeBits < numBits)
- Tmp.trunc(activeBits);
+ Tmp = Tmp.trunc(activeBits);
APSIntVal = APSInt(Tmp, true);
}
return lltok::APSInt;
}
}
- APFloatVal = APFloat(atof(TokStart));
+ APFloatVal = APFloat(std::atof(TokStart));
return lltok::APFloat;
}
}
}
- APFloatVal = APFloat(atof(TokStart));
+ APFloatVal = APFloat(std::atof(TokStart));
return lltok::APFloat;
}