Add support for embedded metadata to LLVM. This introduces two new types of
[oota-llvm.git] / lib / AsmParser / LLParser.h
1 //===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the parser class for .ll files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ASMPARSER_LLPARSER_H
15 #define LLVM_ASMPARSER_LLPARSER_H
16
17 #include "LLLexer.h"
18 #include "llvm/Type.h"
19 #include <map>
20
21 namespace llvm {
22   class Module;
23   class OpaqueType;
24   class Function;
25   class Value;
26   class BasicBlock;
27   class Instruction;
28   class Constant;
29   class GlobalValue;
30   class MDString;
31   class MDNode;
32   struct ValID;
33
34   class LLParser {
35   public:
36     typedef LLLexer::LocTy LocTy;
37   private:
38
39     LLLexer Lex;
40     Module *M;
41
42     // Type resolution handling data structures.
43     std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
44     std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
45     std::vector<PATypeHolder> NumberedTypes;
46
47     struct UpRefRecord {
48       /// Loc - This is the location of the upref.
49       LocTy Loc;
50
51       /// NestingLevel - The number of nesting levels that need to be popped
52       /// before this type is resolved.
53       unsigned NestingLevel;
54
55       /// LastContainedTy - This is the type at the current binding level for
56       /// the type.  Every time we reduce the nesting level, this gets updated.
57       const Type *LastContainedTy;
58
59       /// UpRefTy - This is the actual opaque type that the upreference is
60       /// represented with.
61       OpaqueType *UpRefTy;
62
63       UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
64         : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
65           UpRefTy(URTy) {}
66     };
67     std::vector<UpRefRecord> UpRefs;
68
69     // Global Value reference information.
70     std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
71     std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
72     std::vector<GlobalValue*> NumberedVals;
73   public:
74     LLParser(MemoryBuffer *F, ParseError &Err, Module *m) : Lex(F, Err), M(m) {}
75     bool Run();
76
77   private:
78
79     bool Error(LocTy L, const std::string &Msg) const {
80       return Lex.Error(L, Msg);
81     }
82     bool TokError(const std::string &Msg) const {
83       return Error(Lex.getLoc(), Msg);
84     }
85
86     /// GetGlobalVal - Get a value with the specified name or ID, creating a
87     /// forward reference record if needed.  This can return null if the value
88     /// exists but does not have the right type.
89     GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
90     GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
91
92     // Helper Routines.
93     bool ParseToken(lltok::Kind T, const char *ErrMsg);
94     bool EatIfPresent(lltok::Kind T) {
95       if (Lex.getKind() != T) return false;
96       Lex.Lex();
97       return true;
98     }
99     bool ParseOptionalToken(lltok::Kind T, bool &Present) {
100       if (Lex.getKind() != T) {
101         Present = false;
102       } else {
103         Lex.Lex();
104         Present = true;
105       }
106       return false;
107     }
108     bool ParseStringConstant(std::string &Result);
109     bool ParseUInt32(unsigned &Val);
110     bool ParseUInt32(unsigned &Val, LocTy &Loc) {
111       Loc = Lex.getLoc();
112       return ParseUInt32(Val);
113     }
114     bool ParseOptionalAddrSpace(unsigned &AddrSpace);
115     bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
116     bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
117     bool ParseOptionalLinkage(unsigned &Linkage) {
118       bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
119     }
120     bool ParseOptionalVisibility(unsigned &Visibility);
121     bool ParseOptionalCallingConv(unsigned &CC);
122     bool ParseOptionalAlignment(unsigned &Alignment);
123     bool ParseOptionalCommaAlignment(unsigned &Alignment);
124     bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
125
126     // Top-Level Entities
127     bool ParseTopLevelEntities();
128     bool ValidateEndOfModule();
129     bool ParseTargetDefinition();
130     bool ParseDepLibs();
131     bool ParseModuleAsm();
132     bool ParseUnnamedType();
133     bool ParseNamedType();
134     bool ParseDeclare();
135     bool ParseDefine();
136
137     bool ParseGlobalType(bool &IsConstant);
138     bool ParseNamedGlobal();
139     bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
140                      bool HasLinkage, unsigned Visibility);
141     bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
142
143     // Type Parsing.
144     bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
145     bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
146       Loc = Lex.getLoc();
147       return ParseType(Result, AllowVoid);
148     }
149     bool ParseTypeRec(PATypeHolder &H);
150     bool ParseStructType(PATypeHolder &H, bool Packed);
151     bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
152     bool ParseFunctionType(PATypeHolder &Result);
153     PATypeHolder HandleUpRefs(const Type *Ty);
154
155     // Constants.
156     bool ParseValID(ValID &ID);
157     bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
158     bool ParseGlobalValue(const Type *Ty, Constant *&V);
159     bool ParseGlobalTypeAndValue(Constant *&V);
160     bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
161     bool ParseMDNodeVector(SmallVectorImpl<Constant*> &);
162
163
164     // Function Semantic Analysis.
165     class PerFunctionState {
166       LLParser &P;
167       Function &F;
168       std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
169       std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
170       std::vector<Value*> NumberedVals;
171     public:
172       PerFunctionState(LLParser &p, Function &f);
173       ~PerFunctionState();
174
175       Function &getFunction() const { return F; }
176
177       bool VerifyFunctionComplete();
178
179       /// GetVal - Get a value with the specified name or ID, creating a
180       /// forward reference record if needed.  This can return null if the value
181       /// exists but does not have the right type.
182       Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
183       Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
184
185       /// SetInstName - After an instruction is parsed and inserted into its
186       /// basic block, this installs its name.
187       bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
188                        Instruction *Inst);
189
190       /// GetBB - Get a basic block with the specified name or ID, creating a
191       /// forward reference record if needed.  This can return null if the value
192       /// is not a BasicBlock.
193       BasicBlock *GetBB(const std::string &Name, LocTy Loc);
194       BasicBlock *GetBB(unsigned ID, LocTy Loc);
195
196       /// DefineBB - Define the specified basic block, which is either named or
197       /// unnamed.  If there is an error, this returns null otherwise it returns
198       /// the block being defined.
199       BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
200     };
201
202     bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
203                              PerFunctionState &PFS);
204
205     bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
206     bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
207                     PerFunctionState &PFS) {
208       Loc = Lex.getLoc();
209       return ParseValue(Ty, V, PFS);
210     }
211
212     bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
213     bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
214       Loc = Lex.getLoc();
215       return ParseTypeAndValue(V, PFS);
216     }
217
218     struct ParamInfo {
219       LocTy Loc;
220       Value *V;
221       unsigned Attrs;
222       ParamInfo(LocTy loc, Value *v, unsigned attrs)
223         : Loc(loc), V(v), Attrs(attrs) {}
224     };
225     bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
226                             PerFunctionState &PFS);
227
228     // Function Parsing.
229     struct ArgInfo {
230       LocTy Loc;
231       PATypeHolder Type;
232       unsigned Attrs;
233       std::string Name;
234       ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
235         : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
236     };
237     bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
238                            bool &isVarArg, bool inType);
239     bool ParseFunctionHeader(Function *&Fn, bool isDefine);
240     bool ParseFunctionBody(Function &Fn);
241     bool ParseBasicBlock(PerFunctionState &PFS);
242
243     // Instruction Parsing.
244     bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
245                           PerFunctionState &PFS);
246     bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
247
248     bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
249     bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
250     bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
251     bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
252
253     bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
254                          unsigned OperandType);
255     bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
256     bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
257     bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
258     bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
259     bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
260     bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
261     bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
262     bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
263     bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
264     bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
265     bool ParseAlloc(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
266     bool ParseFree(Instruction *&I, PerFunctionState &PFS);
267     bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
268     bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
269     bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
270     bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
271     bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
272     bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
273   };
274 } // End llvm namespace
275
276 #endif