blob: 41c2ee75c60440aaf252e0cff750b466c409406b [file] [log] [blame]
Chris Lattnerdf986172009-01-02 07:01:27 +00001//===-- 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
21namespace 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;
Nick Lewycky21cc4462009-04-04 07:22:01 +000030 class MDString;
31 class MDNode;
Chris Lattnerdf986172009-01-02 07:01:27 +000032 struct ValID;
Misha Brukman9ea40342009-01-02 22:46:48 +000033
Chris Lattnerdf986172009-01-02 07:01:27 +000034 class LLParser {
35 public:
36 typedef LLLexer::LocTy LocTy;
37 private:
Bill Wendlingaa3943b2009-07-01 22:33:26 +000038
Chris Lattnerdf986172009-01-02 07:01:27 +000039 LLLexer Lex;
40 Module *M;
Misha Brukman9ea40342009-01-02 22:46:48 +000041
Chris Lattnerdf986172009-01-02 07:01:27 +000042 // 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;
Devang Patel923078c2009-07-01 19:21:12 +000046 /// MetadataCache - This map keeps track of parsed metadata constants.
47 std::map<unsigned, Constant *> MetadataCache;
Chris Lattnerdf986172009-01-02 07:01:27 +000048 struct UpRefRecord {
49 /// Loc - This is the location of the upref.
50 LocTy Loc;
Misha Brukman9ea40342009-01-02 22:46:48 +000051
Chris Lattnerdf986172009-01-02 07:01:27 +000052 /// NestingLevel - The number of nesting levels that need to be popped
53 /// before this type is resolved.
54 unsigned NestingLevel;
Misha Brukman9ea40342009-01-02 22:46:48 +000055
Chris Lattnerdf986172009-01-02 07:01:27 +000056 /// 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;
Misha Brukman9ea40342009-01-02 22:46:48 +000059
Chris Lattnerdf986172009-01-02 07:01:27 +000060 /// UpRefTy - This is the actual opaque type that the upreference is
61 /// represented with.
62 OpaqueType *UpRefTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000063
Chris Lattnerdf986172009-01-02 07:01:27 +000064 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:
Bill Wendlingaa3943b2009-07-01 22:33:26 +000075 LLParser(MemoryBuffer *F, ParseError &Err, Module *m) : Lex(F, Err), M(m) {}
Chris Lattnerad7d1e22009-01-04 20:44:11 +000076 bool Run();
Misha Brukman9ea40342009-01-02 22:46:48 +000077
Chris Lattnerdf986172009-01-02 07:01:27 +000078 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 }
Misha Brukman9ea40342009-01-02 22:46:48 +000086
Chris Lattnerdf986172009-01-02 07:01:27 +000087 /// 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);
Misha Brukman9ea40342009-01-02 22:46:48 +000092
Chris Lattnerdf986172009-01-02 07:01:27 +000093 // Helper Routines.
94 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3ed88ef2009-01-02 08:05:26 +000095 bool EatIfPresent(lltok::Kind T) {
96 if (Lex.getKind() != T) return false;
97 Lex.Lex();
98 return true;
99 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000100 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 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000109 bool ParseStringConstant(std::string &Result);
110 bool ParseUInt32(unsigned &Val);
111 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000112 Loc = Lex.getLoc();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000113 return ParseUInt32(Val);
Chris Lattnerdf986172009-01-02 07:01:27 +0000114 }
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);
Misha Brukman9ea40342009-01-02 22:46:48 +0000126
Chris Lattnerdf986172009-01-02 07:01:27 +0000127 // 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();
Misha Brukman9ea40342009-01-02 22:46:48 +0000137
Chris Lattnerdf986172009-01-02 07:01:27 +0000138 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);
Devang Patel923078c2009-07-01 19:21:12 +0000143 bool ParseStandaloneMetadata();
Misha Brukman9ea40342009-01-02 22:46:48 +0000144
Chris Lattnerdf986172009-01-02 07:01:27 +0000145 // Type Parsing.
Chris Lattnera9a9e072009-03-09 04:49:14 +0000146 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
147 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000148 Loc = Lex.getLoc();
Chris Lattnera9a9e072009-03-09 04:49:14 +0000149 return ParseType(Result, AllowVoid);
Chris Lattnerdf986172009-01-02 07:01:27 +0000150 }
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);
Nick Lewyckycb337992009-05-10 20:57:05 +0000163 bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
Chris Lattnerdf986172009-01-02 07:01:27 +0000164
Misha Brukman9ea40342009-01-02 22:46:48 +0000165
Chris Lattnerdf986172009-01-02 07:01:27 +0000166 // 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();
Misha Brukman9ea40342009-01-02 22:46:48 +0000176
Chris Lattnerdf986172009-01-02 07:01:27 +0000177 Function &getFunction() const { return F; }
Misha Brukman9ea40342009-01-02 22:46:48 +0000178
Chris Lattnerdf986172009-01-02 07:01:27 +0000179 bool VerifyFunctionComplete();
Misha Brukman9ea40342009-01-02 22:46:48 +0000180
Chris Lattnerdf986172009-01-02 07:01:27 +0000181 /// 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);
Misha Brukman9ea40342009-01-02 22:46:48 +0000186
Chris Lattnerdf986172009-01-02 07:01:27 +0000187 /// 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);
Misha Brukman9ea40342009-01-02 22:46:48 +0000191
Chris Lattnerdf986172009-01-02 07:01:27 +0000192 /// 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);
Misha Brukman9ea40342009-01-02 22:46:48 +0000197
Chris Lattnerdf986172009-01-02 07:01:27 +0000198 /// 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 };
Misha Brukman9ea40342009-01-02 22:46:48 +0000203
Chris Lattnerdf986172009-01-02 07:01:27 +0000204 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
205 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000206
Chris Lattnerdf986172009-01-02 07:01:27 +0000207 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 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000213
Chris Lattnerdf986172009-01-02 07:01:27 +0000214 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 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000219
Chris Lattnerdf986172009-01-02 07:01:27 +0000220 struct ParamInfo {
221 LocTy Loc;
222 Value *V;
223 unsigned Attrs;
Misha Brukman9ea40342009-01-02 22:46:48 +0000224 ParamInfo(LocTy loc, Value *v, unsigned attrs)
Chris Lattnerdf986172009-01-02 07:01:27 +0000225 : Loc(loc), V(v), Attrs(attrs) {}
226 };
227 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
228 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000229
Chris Lattnerdf986172009-01-02 07:01:27 +0000230 // 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,
Chris Lattnerdfd19dd2009-01-05 18:34:07 +0000240 bool &isVarArg, bool inType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000241 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
242 bool ParseFunctionBody(Function &Fn);
243 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000244
Chris Lattnerdf986172009-01-02 07:01:27 +0000245 // Instruction Parsing.
246 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
247 PerFunctionState &PFS);
248 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000249
Chris Lattnerdf986172009-01-02 07:01:27 +0000250 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);
Misha Brukman9ea40342009-01-02 22:46:48 +0000254
Chris Lattnere914b592009-01-05 08:24:46 +0000255 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
256 unsigned OperandType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000257 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);
Chris Lattner0088a5c2009-01-05 08:18:44 +0000261 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000262 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