blob: ca2e2b695f8d5e7578a7bb4e5c095ee09c32a1a7 [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"
Owen Andersonfba933c2009-07-01 23:57:11 +000018#include "llvm/Module.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000019#include "llvm/Type.h"
20#include <map>
21
22namespace llvm {
23 class Module;
24 class OpaqueType;
25 class Function;
26 class Value;
27 class BasicBlock;
28 class Instruction;
29 class Constant;
30 class GlobalValue;
Devang Patele54abc92009-07-22 17:43:22 +000031 class MetadataBase;
Nick Lewycky21cc4462009-04-04 07:22:01 +000032 class MDString;
33 class MDNode;
Chris Lattnerdf986172009-01-02 07:01:27 +000034 struct ValID;
Misha Brukman9ea40342009-01-02 22:46:48 +000035
Chris Lattnerdf986172009-01-02 07:01:27 +000036 class LLParser {
37 public:
38 typedef LLLexer::LocTy LocTy;
39 private:
Owen Andersonfba933c2009-07-01 23:57:11 +000040 LLVMContext& Context;
Chris Lattnerdf986172009-01-02 07:01:27 +000041 LLLexer Lex;
42 Module *M;
Misha Brukman9ea40342009-01-02 22:46:48 +000043
Chris Lattnerdf986172009-01-02 07:01:27 +000044 // Type resolution handling data structures.
45 std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
46 std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
47 std::vector<PATypeHolder> NumberedTypes;
Devang Patel923078c2009-07-01 19:21:12 +000048 /// MetadataCache - This map keeps track of parsed metadata constants.
Devang Patel104cf9e2009-07-23 01:07:34 +000049 std::map<unsigned, MetadataBase *> MetadataCache;
50 std::map<unsigned, std::pair<MetadataBase *, LocTy> > ForwardRefMDNodes;
Devang Patela2148402009-09-28 21:14:55 +000051 SmallVector<std::pair<unsigned, MDNode *>, 2> MDsOnInst;
Chris Lattnerdf986172009-01-02 07:01:27 +000052 struct UpRefRecord {
53 /// Loc - This is the location of the upref.
54 LocTy Loc;
Misha Brukman9ea40342009-01-02 22:46:48 +000055
Chris Lattnerdf986172009-01-02 07:01:27 +000056 /// NestingLevel - The number of nesting levels that need to be popped
57 /// before this type is resolved.
58 unsigned NestingLevel;
Misha Brukman9ea40342009-01-02 22:46:48 +000059
Chris Lattnerdf986172009-01-02 07:01:27 +000060 /// LastContainedTy - This is the type at the current binding level for
61 /// the type. Every time we reduce the nesting level, this gets updated.
62 const Type *LastContainedTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000063
Chris Lattnerdf986172009-01-02 07:01:27 +000064 /// UpRefTy - This is the actual opaque type that the upreference is
65 /// represented with.
66 OpaqueType *UpRefTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000067
Chris Lattnerdf986172009-01-02 07:01:27 +000068 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
69 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
70 UpRefTy(URTy) {}
71 };
72 std::vector<UpRefRecord> UpRefs;
73
74 // Global Value reference information.
75 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
76 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
77 std::vector<GlobalValue*> NumberedVals;
Victor Hernandez13ad5aa2009-10-17 00:00:19 +000078 Function* MallocF;
Chris Lattnerdf986172009-01-02 07:01:27 +000079 public:
Chris Lattnereeb4a842009-07-02 23:08:13 +000080 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
Victor Hernandez13ad5aa2009-10-17 00:00:19 +000081 Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
82 M(m), MallocF(NULL) {}
Chris Lattnerad7d1e22009-01-04 20:44:11 +000083 bool Run();
Misha Brukman9ea40342009-01-02 22:46:48 +000084
Owen Andersonb43eae72009-07-02 17:04:01 +000085 LLVMContext& getContext() { return Context; }
86
Chris Lattnerdf986172009-01-02 07:01:27 +000087 private:
88
89 bool Error(LocTy L, const std::string &Msg) const {
90 return Lex.Error(L, Msg);
91 }
92 bool TokError(const std::string &Msg) const {
93 return Error(Lex.getLoc(), Msg);
94 }
Misha Brukman9ea40342009-01-02 22:46:48 +000095
Chris Lattnerdf986172009-01-02 07:01:27 +000096 /// GetGlobalVal - Get a value with the specified name or ID, creating a
97 /// forward reference record if needed. This can return null if the value
98 /// exists but does not have the right type.
99 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
100 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000101
Chris Lattnerdf986172009-01-02 07:01:27 +0000102 // Helper Routines.
103 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000104 bool EatIfPresent(lltok::Kind T) {
105 if (Lex.getKind() != T) return false;
106 Lex.Lex();
107 return true;
108 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000109 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
110 if (Lex.getKind() != T) {
111 Present = false;
112 } else {
113 Lex.Lex();
114 Present = true;
115 }
116 return false;
117 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000118 bool ParseStringConstant(std::string &Result);
119 bool ParseUInt32(unsigned &Val);
120 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000121 Loc = Lex.getLoc();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000122 return ParseUInt32(Val);
Chris Lattnerdf986172009-01-02 07:01:27 +0000123 }
124 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
125 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
126 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
127 bool ParseOptionalLinkage(unsigned &Linkage) {
128 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
129 }
130 bool ParseOptionalVisibility(unsigned &Visibility);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000131 bool ParseOptionalCallingConv(CallingConv::ID &CC);
Chris Lattnerdf986172009-01-02 07:01:27 +0000132 bool ParseOptionalAlignment(unsigned &Alignment);
Devang Patel0475c912009-09-29 00:01:14 +0000133 bool ParseOptionalCustomMetadata();
Devang Patelf633a062009-09-17 23:04:48 +0000134 bool ParseOptionalInfo(unsigned &Alignment);
Chris Lattnerdf986172009-01-02 07:01:27 +0000135 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
Misha Brukman9ea40342009-01-02 22:46:48 +0000136
Chris Lattnerdf986172009-01-02 07:01:27 +0000137 // Top-Level Entities
138 bool ParseTopLevelEntities();
139 bool ValidateEndOfModule();
140 bool ParseTargetDefinition();
141 bool ParseDepLibs();
142 bool ParseModuleAsm();
143 bool ParseUnnamedType();
144 bool ParseNamedType();
145 bool ParseDeclare();
146 bool ParseDefine();
Misha Brukman9ea40342009-01-02 22:46:48 +0000147
Chris Lattnerdf986172009-01-02 07:01:27 +0000148 bool ParseGlobalType(bool &IsConstant);
Dan Gohman3845e502009-08-12 23:32:33 +0000149 bool ParseUnnamedGlobal();
Chris Lattnerdf986172009-01-02 07:01:27 +0000150 bool ParseNamedGlobal();
151 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
152 bool HasLinkage, unsigned Visibility);
153 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
Devang Patel923078c2009-07-01 19:21:12 +0000154 bool ParseStandaloneMetadata();
Devang Pateleff2ab62009-07-29 00:34:02 +0000155 bool ParseNamedMetadata();
Devang Patele54abc92009-07-22 17:43:22 +0000156 bool ParseMDString(MetadataBase *&S);
Devang Patel104cf9e2009-07-23 01:07:34 +0000157 bool ParseMDNode(MetadataBase *&N);
Misha Brukman9ea40342009-01-02 22:46:48 +0000158
Chris Lattnerdf986172009-01-02 07:01:27 +0000159 // Type Parsing.
Chris Lattnera9a9e072009-03-09 04:49:14 +0000160 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
161 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000162 Loc = Lex.getLoc();
Chris Lattnera9a9e072009-03-09 04:49:14 +0000163 return ParseType(Result, AllowVoid);
Chris Lattnerdf986172009-01-02 07:01:27 +0000164 }
165 bool ParseTypeRec(PATypeHolder &H);
166 bool ParseStructType(PATypeHolder &H, bool Packed);
167 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
168 bool ParseFunctionType(PATypeHolder &Result);
169 PATypeHolder HandleUpRefs(const Type *Ty);
170
171 // Constants.
172 bool ParseValID(ValID &ID);
173 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
174 bool ParseGlobalValue(const Type *Ty, Constant *&V);
175 bool ParseGlobalTypeAndValue(Constant *&V);
176 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
Nick Lewyckycb337992009-05-10 20:57:05 +0000177 bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
Chris Lattnerdf986172009-01-02 07:01:27 +0000178
Misha Brukman9ea40342009-01-02 22:46:48 +0000179
Chris Lattnerdf986172009-01-02 07:01:27 +0000180 // Function Semantic Analysis.
181 class PerFunctionState {
182 LLParser &P;
183 Function &F;
184 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
185 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
186 std::vector<Value*> NumberedVals;
187 public:
188 PerFunctionState(LLParser &p, Function &f);
189 ~PerFunctionState();
Misha Brukman9ea40342009-01-02 22:46:48 +0000190
Chris Lattnerdf986172009-01-02 07:01:27 +0000191 Function &getFunction() const { return F; }
Misha Brukman9ea40342009-01-02 22:46:48 +0000192
Chris Lattnerdf986172009-01-02 07:01:27 +0000193 bool VerifyFunctionComplete();
Misha Brukman9ea40342009-01-02 22:46:48 +0000194
Chris Lattnerdf986172009-01-02 07:01:27 +0000195 /// GetVal - Get a value with the specified name or ID, creating a
196 /// forward reference record if needed. This can return null if the value
197 /// exists but does not have the right type.
198 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
199 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000200
Chris Lattnerdf986172009-01-02 07:01:27 +0000201 /// SetInstName - After an instruction is parsed and inserted into its
202 /// basic block, this installs its name.
203 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
204 Instruction *Inst);
Misha Brukman9ea40342009-01-02 22:46:48 +0000205
Chris Lattnerdf986172009-01-02 07:01:27 +0000206 /// GetBB - Get a basic block with the specified name or ID, creating a
207 /// forward reference record if needed. This can return null if the value
208 /// is not a BasicBlock.
209 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
210 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000211
Chris Lattnerdf986172009-01-02 07:01:27 +0000212 /// DefineBB - Define the specified basic block, which is either named or
213 /// unnamed. If there is an error, this returns null otherwise it returns
214 /// the block being defined.
215 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
216 };
Misha Brukman9ea40342009-01-02 22:46:48 +0000217
Chris Lattnerdf986172009-01-02 07:01:27 +0000218 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
219 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000220
Chris Lattnerdf986172009-01-02 07:01:27 +0000221 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
222 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
223 PerFunctionState &PFS) {
224 Loc = Lex.getLoc();
225 return ParseValue(Ty, V, PFS);
226 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000227
Chris Lattnerdf986172009-01-02 07:01:27 +0000228 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
229 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
230 Loc = Lex.getLoc();
231 return ParseTypeAndValue(V, PFS);
232 }
Chris Lattnerf9be95f2009-10-27 19:13:16 +0000233 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
234 PerFunctionState &PFS);
235 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
236 LocTy Loc;
237 return ParseTypeAndBasicBlock(BB, Loc, PFS);
238 }
239
Chris Lattnerdf986172009-01-02 07:01:27 +0000240 struct ParamInfo {
241 LocTy Loc;
242 Value *V;
243 unsigned Attrs;
Misha Brukman9ea40342009-01-02 22:46:48 +0000244 ParamInfo(LocTy loc, Value *v, unsigned attrs)
Chris Lattnerdf986172009-01-02 07:01:27 +0000245 : Loc(loc), V(v), Attrs(attrs) {}
246 };
247 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
248 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000249
Chris Lattnerdf986172009-01-02 07:01:27 +0000250 // Function Parsing.
251 struct ArgInfo {
252 LocTy Loc;
253 PATypeHolder Type;
254 unsigned Attrs;
255 std::string Name;
256 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
257 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
258 };
259 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
Chris Lattnerdfd19dd2009-01-05 18:34:07 +0000260 bool &isVarArg, bool inType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000261 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
262 bool ParseFunctionBody(Function &Fn);
263 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000264
Chris Lattnerdf986172009-01-02 07:01:27 +0000265 // Instruction Parsing.
266 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
267 PerFunctionState &PFS);
268 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000269
Chris Lattnerdf986172009-01-02 07:01:27 +0000270 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
271 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
272 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerab21db72009-10-28 00:19:10 +0000273 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000274 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000275
Chris Lattnere914b592009-01-05 08:24:46 +0000276 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
277 unsigned OperandType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000278 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
279 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
280 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
281 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattner0088a5c2009-01-05 08:18:44 +0000282 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000283 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
284 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
285 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
286 bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
287 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000288 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS,
289 BasicBlock *BB = 0, bool isAlloca = true);
Victor Hernandez66284e02009-10-24 04:23:03 +0000290 bool ParseFree(Instruction *&I, PerFunctionState &PFS, BasicBlock *BB);
Chris Lattnerdf986172009-01-02 07:01:27 +0000291 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
292 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
293 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
294 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
295 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
296 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
297 };
298} // End llvm namespace
299
300#endif