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