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