2009-01-02 08:01:27 +01:00
|
|
|
//===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the parser class for .ll files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ASMPARSER_LLPARSER_H
|
|
|
|
#define LLVM_ASMPARSER_LLPARSER_H
|
|
|
|
|
|
|
|
#include "LLLexer.h"
|
2009-07-02 01:57:11 +02:00
|
|
|
#include "llvm/Module.h"
|
2009-01-02 08:01:27 +01:00
|
|
|
#include "llvm/Type.h"
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class Module;
|
|
|
|
class OpaqueType;
|
|
|
|
class Function;
|
|
|
|
class Value;
|
|
|
|
class BasicBlock;
|
|
|
|
class Instruction;
|
|
|
|
class Constant;
|
|
|
|
class GlobalValue;
|
2009-04-04 09:22:01 +02:00
|
|
|
class MDString;
|
|
|
|
class MDNode;
|
2009-01-02 08:01:27 +01:00
|
|
|
struct ValID;
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
class LLParser {
|
|
|
|
public:
|
|
|
|
typedef LLLexer::LocTy LocTy;
|
|
|
|
private:
|
2009-07-02 01:57:11 +02:00
|
|
|
LLVMContext& Context;
|
2009-01-02 08:01:27 +01:00
|
|
|
LLLexer Lex;
|
|
|
|
Module *M;
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
// Type resolution handling data structures.
|
|
|
|
std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
|
|
|
|
std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
|
|
|
|
std::vector<PATypeHolder> NumberedTypes;
|
2009-07-01 21:21:12 +02:00
|
|
|
/// MetadataCache - This map keeps track of parsed metadata constants.
|
|
|
|
std::map<unsigned, Constant *> MetadataCache;
|
2009-07-08 21:23:54 +02:00
|
|
|
std::map<unsigned, std::pair<Constant *, LocTy> > ForwardRefMDNodes;
|
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
struct UpRefRecord {
|
|
|
|
/// Loc - This is the location of the upref.
|
|
|
|
LocTy Loc;
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
/// NestingLevel - The number of nesting levels that need to be popped
|
|
|
|
/// before this type is resolved.
|
|
|
|
unsigned NestingLevel;
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
/// LastContainedTy - This is the type at the current binding level for
|
|
|
|
/// the type. Every time we reduce the nesting level, this gets updated.
|
|
|
|
const Type *LastContainedTy;
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
/// UpRefTy - This is the actual opaque type that the upreference is
|
|
|
|
/// represented with.
|
|
|
|
OpaqueType *UpRefTy;
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
|
|
|
|
: Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
|
|
|
|
UpRefTy(URTy) {}
|
|
|
|
};
|
|
|
|
std::vector<UpRefRecord> UpRefs;
|
|
|
|
|
|
|
|
// Global Value reference information.
|
|
|
|
std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
|
|
|
|
std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
|
|
|
|
std::vector<GlobalValue*> NumberedVals;
|
|
|
|
public:
|
2009-07-03 01:08:13 +02:00
|
|
|
LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
|
2009-07-07 20:44:11 +02:00
|
|
|
Context(m->getContext()), Lex(F, SM, Err, m->getContext()), M(m) {}
|
2009-01-04 21:44:11 +01:00
|
|
|
bool Run();
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-07-02 19:04:01 +02:00
|
|
|
LLVMContext& getContext() { return Context; }
|
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
bool Error(LocTy L, const std::string &Msg) const {
|
|
|
|
return Lex.Error(L, Msg);
|
|
|
|
}
|
|
|
|
bool TokError(const std::string &Msg) const {
|
|
|
|
return Error(Lex.getLoc(), Msg);
|
|
|
|
}
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
/// GetGlobalVal - Get a value with the specified name or ID, creating a
|
|
|
|
/// forward reference record if needed. This can return null if the value
|
|
|
|
/// exists but does not have the right type.
|
|
|
|
GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
|
|
|
|
GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
// Helper Routines.
|
|
|
|
bool ParseToken(lltok::Kind T, const char *ErrMsg);
|
2009-01-02 09:05:26 +01:00
|
|
|
bool EatIfPresent(lltok::Kind T) {
|
|
|
|
if (Lex.getKind() != T) return false;
|
|
|
|
Lex.Lex();
|
|
|
|
return true;
|
|
|
|
}
|
2009-01-02 08:01:27 +01:00
|
|
|
bool ParseOptionalToken(lltok::Kind T, bool &Present) {
|
|
|
|
if (Lex.getKind() != T) {
|
|
|
|
Present = false;
|
|
|
|
} else {
|
|
|
|
Lex.Lex();
|
|
|
|
Present = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-02 09:05:26 +01:00
|
|
|
bool ParseStringConstant(std::string &Result);
|
|
|
|
bool ParseUInt32(unsigned &Val);
|
|
|
|
bool ParseUInt32(unsigned &Val, LocTy &Loc) {
|
2009-01-02 08:01:27 +01:00
|
|
|
Loc = Lex.getLoc();
|
2009-01-02 09:05:26 +01:00
|
|
|
return ParseUInt32(Val);
|
2009-01-02 08:01:27 +01:00
|
|
|
}
|
|
|
|
bool ParseOptionalAddrSpace(unsigned &AddrSpace);
|
|
|
|
bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
|
|
|
|
bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
|
|
|
|
bool ParseOptionalLinkage(unsigned &Linkage) {
|
|
|
|
bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
|
|
|
|
}
|
|
|
|
bool ParseOptionalVisibility(unsigned &Visibility);
|
|
|
|
bool ParseOptionalCallingConv(unsigned &CC);
|
|
|
|
bool ParseOptionalAlignment(unsigned &Alignment);
|
|
|
|
bool ParseOptionalCommaAlignment(unsigned &Alignment);
|
|
|
|
bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
// Top-Level Entities
|
|
|
|
bool ParseTopLevelEntities();
|
|
|
|
bool ValidateEndOfModule();
|
|
|
|
bool ParseTargetDefinition();
|
|
|
|
bool ParseDepLibs();
|
|
|
|
bool ParseModuleAsm();
|
|
|
|
bool ParseUnnamedType();
|
|
|
|
bool ParseNamedType();
|
|
|
|
bool ParseDeclare();
|
|
|
|
bool ParseDefine();
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
bool ParseGlobalType(bool &IsConstant);
|
|
|
|
bool ParseNamedGlobal();
|
|
|
|
bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
|
|
|
|
bool HasLinkage, unsigned Visibility);
|
|
|
|
bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
|
2009-07-01 21:21:12 +02:00
|
|
|
bool ParseStandaloneMetadata();
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
// Type Parsing.
|
2009-03-09 05:49:14 +01:00
|
|
|
bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
|
|
|
|
bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
|
2009-01-02 08:01:27 +01:00
|
|
|
Loc = Lex.getLoc();
|
2009-03-09 05:49:14 +01:00
|
|
|
return ParseType(Result, AllowVoid);
|
2009-01-02 08:01:27 +01:00
|
|
|
}
|
|
|
|
bool ParseTypeRec(PATypeHolder &H);
|
|
|
|
bool ParseStructType(PATypeHolder &H, bool Packed);
|
|
|
|
bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
|
|
|
|
bool ParseFunctionType(PATypeHolder &Result);
|
|
|
|
PATypeHolder HandleUpRefs(const Type *Ty);
|
|
|
|
|
|
|
|
// Constants.
|
|
|
|
bool ParseValID(ValID &ID);
|
|
|
|
bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
|
|
|
|
bool ParseGlobalValue(const Type *Ty, Constant *&V);
|
|
|
|
bool ParseGlobalTypeAndValue(Constant *&V);
|
|
|
|
bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
|
2009-05-10 22:57:05 +02:00
|
|
|
bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
|
2009-01-02 08:01:27 +01:00
|
|
|
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
// Function Semantic Analysis.
|
|
|
|
class PerFunctionState {
|
|
|
|
LLParser &P;
|
|
|
|
Function &F;
|
|
|
|
std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
|
|
|
|
std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
|
|
|
|
std::vector<Value*> NumberedVals;
|
|
|
|
public:
|
|
|
|
PerFunctionState(LLParser &p, Function &f);
|
|
|
|
~PerFunctionState();
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
Function &getFunction() const { return F; }
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
bool VerifyFunctionComplete();
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
/// GetVal - Get a value with the specified name or ID, creating a
|
|
|
|
/// forward reference record if needed. This can return null if the value
|
|
|
|
/// exists but does not have the right type.
|
|
|
|
Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
|
|
|
|
Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
/// SetInstName - After an instruction is parsed and inserted into its
|
|
|
|
/// basic block, this installs its name.
|
|
|
|
bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
|
|
|
|
Instruction *Inst);
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
/// GetBB - Get a basic block with the specified name or ID, creating a
|
|
|
|
/// forward reference record if needed. This can return null if the value
|
|
|
|
/// is not a BasicBlock.
|
|
|
|
BasicBlock *GetBB(const std::string &Name, LocTy Loc);
|
|
|
|
BasicBlock *GetBB(unsigned ID, LocTy Loc);
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
/// DefineBB - Define the specified basic block, which is either named or
|
|
|
|
/// unnamed. If there is an error, this returns null otherwise it returns
|
|
|
|
/// the block being defined.
|
|
|
|
BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
|
|
|
|
};
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
|
|
|
|
PerFunctionState &PFS);
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
|
|
|
|
bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
|
|
|
|
PerFunctionState &PFS) {
|
|
|
|
Loc = Lex.getLoc();
|
|
|
|
return ParseValue(Ty, V, PFS);
|
|
|
|
}
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
|
|
|
|
bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
|
|
|
|
Loc = Lex.getLoc();
|
|
|
|
return ParseTypeAndValue(V, PFS);
|
|
|
|
}
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
struct ParamInfo {
|
|
|
|
LocTy Loc;
|
|
|
|
Value *V;
|
|
|
|
unsigned Attrs;
|
2009-01-02 23:46:48 +01:00
|
|
|
ParamInfo(LocTy loc, Value *v, unsigned attrs)
|
2009-01-02 08:01:27 +01:00
|
|
|
: Loc(loc), V(v), Attrs(attrs) {}
|
|
|
|
};
|
|
|
|
bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
|
|
|
|
PerFunctionState &PFS);
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
// Function Parsing.
|
|
|
|
struct ArgInfo {
|
|
|
|
LocTy Loc;
|
|
|
|
PATypeHolder Type;
|
|
|
|
unsigned Attrs;
|
|
|
|
std::string Name;
|
|
|
|
ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
|
|
|
|
: Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
|
|
|
|
};
|
|
|
|
bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
|
2009-01-05 19:34:07 +01:00
|
|
|
bool &isVarArg, bool inType);
|
2009-01-02 08:01:27 +01:00
|
|
|
bool ParseFunctionHeader(Function *&Fn, bool isDefine);
|
|
|
|
bool ParseFunctionBody(Function &Fn);
|
|
|
|
bool ParseBasicBlock(PerFunctionState &PFS);
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
// Instruction Parsing.
|
|
|
|
bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
|
|
|
|
PerFunctionState &PFS);
|
|
|
|
bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-02 08:01:27 +01:00
|
|
|
bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
|
|
|
|
bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
|
|
|
|
bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
|
|
|
|
bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2009-01-05 09:24:46 +01:00
|
|
|
bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
|
|
|
|
unsigned OperandType);
|
2009-01-02 08:01:27 +01:00
|
|
|
bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
|
|
|
|
bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
|
|
|
|
bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
|
|
|
|
bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
|
2009-01-05 09:18:44 +01:00
|
|
|
bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
|
2009-01-02 08:01:27 +01:00
|
|
|
bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
|
|
|
|
bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
|
|
|
|
bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
|
|
|
|
bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
|
|
|
|
bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
|
|
|
|
bool ParseAlloc(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
|
|
|
|
bool ParseFree(Instruction *&I, PerFunctionState &PFS);
|
|
|
|
bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
|
|
|
|
bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
|
|
|
|
bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
|
|
|
|
bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
|
|
|
|
bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
|
|
|
|
bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
|
|
|
|
};
|
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|