#include "LLLexer.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Instruction.h"
+#include "llvm/LLVMContext.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 <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);
+ ErrorInfo = SM.GetMessage(ErrorLoc, Msg, "error");
return true;
}
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;
+ 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[0] = 0;
+ for (int i=0; i<16; i++, Buffer++) {
+ 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;
+ }
+ if (Buffer != End)
+ Error("constant bigger than 128 bits detected!");
+}
+
// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
// appropriate character.
static void UnEscapeLexed(std::string &Str) {
// 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 '-':
return false;
}
+/// LexExclaim:
+/// !foo
+/// !
+lltok::Kind LLLexer::LexExclaim() {
+ // Lex a metadata name as a MetadataVar.
+ if (isalpha(CurPtr[0])) {
+ ++CurPtr;
+ while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
+ CurPtr[0] == '.' || CurPtr[0] == '_')
+ ++CurPtr;
+ StrVal.assign(TokStart+1, CurPtr); // Skip !
+ 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;
}
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(deplibs);
KEYWORD(datalayout);
KEYWORD(volatile);
+ 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(cc);
KEYWORD(c);
KEYWORD(readnone);
KEYWORD(readonly);
+ KEYWORD(inlinehint);
KEYWORD(noinline);
KEYWORD(alwaysinline);
KEYWORD(optsize);
KEYWORD(ssp);
KEYWORD(sspreq);
+ KEYWORD(noredzone);
+ KEYWORD(noimplicitfloat);
+ KEYWORD(naked);
KEYWORD(type);
KEYWORD(opaque);
KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
KEYWORD(x);
+ KEYWORD(blockaddress);
#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("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));
#undef TYPEKEYWORD
// Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
// Scan CurPtr ahead, seeing if there is just whitespace before the newline.
if (JustWhitespaceNewLine(CurPtr))
return lltok::kw_zeroext;
+ } else if (Len == 6 && !memcmp(StartChar, "malloc", 6)) {
+ // FIXME: Remove in LLVM 3.0.
+ // Autoupgrade malloc instruction.
+ return lltok::kw_malloc;
+ } else if (Len == 4 && !memcmp(StartChar, "free", 4)) {
+ // FIXME: Remove in LLVM 3.0.
+ // Autoupgrade malloc instruction.
+ return lltok::kw_free;
}
// Keywords for instructions.
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(unreachable, Unreachable);
- INSTKEYWORD(malloc, Malloc);
INSTKEYWORD(alloca, Alloca);
- INSTKEYWORD(free, Free);
INSTKEYWORD(load, Load);
INSTKEYWORD(store, Store);
INSTKEYWORD(getelementptr, GetElementPtr);
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);
}
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)
+ FP80HexToIntPair(TokStart+3, CurPtr, Pair);
APFloatVal = APFloat(APInt(80, 2, Pair));
return lltok::APFloat;
case 'L':
// F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
+ HexToIntPair(TokStart+3, CurPtr, Pair);
APFloatVal = APFloat(APInt(128, 2, Pair), true);
return lltok::APFloat;
case 'M':
// PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
+ HexToIntPair(TokStart+3, CurPtr, Pair);
APFloatVal = APFloat(APInt(128, 2, Pair));
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)