blob: 7203bb245d0f92a22e047886060158105358997b [file] [log] [blame]
Chris Lattnerac161bf2009-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"
Chandler Carruth802d7552012-12-04 07:12:27 +000018#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/StringMap.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Attributes.h"
21#include "llvm/IR/Instructions.h"
22#include "llvm/IR/Module.h"
23#include "llvm/IR/Operator.h"
24#include "llvm/IR/Type.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000025#include "llvm/IR/ValueHandle.h"
Chris Lattner218b22f2009-12-29 21:43:58 +000026#include <map>
Chris Lattnerac161bf2009-01-02 07:01:27 +000027
28namespace llvm {
29 class Module;
30 class OpaqueType;
31 class Function;
32 class Value;
33 class BasicBlock;
34 class Instruction;
35 class Constant;
36 class GlobalValue;
David Majnemerdad0a642014-06-27 18:19:56 +000037 class Comdat;
Nick Lewycky49f89192009-04-04 07:22:01 +000038 class MDString;
39 class MDNode;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000040 class StructType;
Misha Brukman1d9a93d2009-01-02 22:46:48 +000041
Chris Lattner3432c622009-10-28 03:39:23 +000042 /// ValID - Represents a reference of a definition of some sort with no type.
43 /// There are several cases where we have to parse the value but where the
44 /// type can depend on later context. This may either be a numeric reference
45 /// or a symbolic (%var) reference. This is just a discriminated union.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000046 struct ValID {
Chris Lattner3432c622009-10-28 03:39:23 +000047 enum {
48 t_LocalID, t_GlobalID, // ID in UIntVal.
49 t_LocalName, t_GlobalName, // Name in StrVal.
50 t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal.
51 t_Null, t_Undef, t_Zero, // No value.
52 t_EmptyArray, // No value: []
53 t_Constant, // Value in ConstantVal.
54 t_InlineAsm, // Value in StrVal/StrVal2/UIntVal.
Chris Lattner5b4a9622009-12-30 02:11:14 +000055 t_MDNode, // Value in MDNodeVal.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000056 t_MDString, // Value in MDStringVal.
57 t_ConstantStruct, // Value in ConstantStructElts.
58 t_PackedConstantStruct // Value in ConstantStructElts.
Chris Lattner3432c622009-10-28 03:39:23 +000059 } Kind;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000060
Chris Lattner3432c622009-10-28 03:39:23 +000061 LLLexer::LocTy Loc;
62 unsigned UIntVal;
63 std::string StrVal, StrVal2;
64 APSInt APSIntVal;
65 APFloat APFloatVal;
66 Constant *ConstantVal;
Chris Lattner5b4a9622009-12-30 02:11:14 +000067 MDNode *MDNodeVal;
68 MDString *MDStringVal;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000069 Constant **ConstantStructElts;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000070
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000071 ValID() : Kind(t_LocalID), APFloatVal(0.0) {}
72 ~ValID() {
73 if (Kind == t_ConstantStruct || Kind == t_PackedConstantStruct)
74 delete [] ConstantStructElts;
75 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +000076
Chris Lattner3432c622009-10-28 03:39:23 +000077 bool operator<(const ValID &RHS) const {
78 if (Kind == t_LocalID || Kind == t_GlobalID)
79 return UIntVal < RHS.UIntVal;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000080 assert((Kind == t_LocalName || Kind == t_GlobalName ||
Michael Ilseman26ee2b82012-11-15 22:34:00 +000081 Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) &&
Chris Lattner3432c622009-10-28 03:39:23 +000082 "Ordering not defined for this ValID kind yet");
83 return StrVal < RHS.StrVal;
84 }
85 };
Michael Ilseman26ee2b82012-11-15 22:34:00 +000086
Benjamin Kramer079b96e2013-09-11 18:05:11 +000087 class LLParser {
Chris Lattnerac161bf2009-01-02 07:01:27 +000088 public:
89 typedef LLLexer::LocTy LocTy;
90 private:
Chris Lattner2e664bd2010-04-07 04:08:57 +000091 LLVMContext &Context;
Chris Lattnerac161bf2009-01-02 07:01:27 +000092 LLLexer Lex;
93 Module *M;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000094
Chris Lattner8eff0152010-04-01 05:14:45 +000095 // Instruction metadata resolution. Each instruction can have a list of
96 // MDRef info associated with them.
Dan Gohman7c7f13a2010-08-24 14:31:06 +000097 //
98 // The simpler approach of just creating temporary MDNodes and then calling
99 // RAUW on them when the definition is processed doesn't work because some
100 // instruction metadata kinds, such as dbg, get stored in the IR in an
101 // "optimized" format which doesn't participate in the normal value use
102 // lists. This means that RAUW doesn't work, even on temporary MDNodes
103 // which otherwise support RAUW. Instead, we defer resolving MDNode
104 // references until the definitions have been processed.
Chris Lattner8eff0152010-04-01 05:14:45 +0000105 struct MDRef {
106 SMLoc Loc;
107 unsigned MDKind, MDSlot;
108 };
109 DenseMap<Instruction*, std::vector<MDRef> > ForwardRefInstMetadata;
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000110
Manman Ren209b17c2013-09-28 00:22:27 +0000111 SmallVector<Instruction*, 64> InstsWithTBAATag;
112
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000113 // Type resolution handling data structures. The location is set when we
114 // have processed a use of the type but not a definition yet.
115 StringMap<std::pair<Type*, LocTy> > NamedTypes;
116 std::vector<std::pair<Type*, LocTy> > NumberedTypes;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000117
Chris Lattnerfc58af22009-12-30 04:51:58 +0000118 std::vector<TrackingVH<MDNode> > NumberedMetadata;
Chris Lattner218b22f2009-12-29 21:43:58 +0000119 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000120
121 // Global Value reference information.
122 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
123 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
124 std::vector<GlobalValue*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000125
David Majnemerdad0a642014-06-27 18:19:56 +0000126 // Comdat forward reference information.
127 std::map<std::string, LocTy> ForwardRefComdats;
128
Chris Lattner3432c622009-10-28 03:39:23 +0000129 // References to blockaddress. The key is the function ValID, the value is
130 // a list of references to blocks in that function.
131 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >
132 ForwardRefBlockAddresses;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000133
Bill Wendling63b88192013-02-06 06:52:58 +0000134 // Attribute builder reference information.
Bill Wendlingb32b0412013-02-08 06:32:06 +0000135 std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups;
136 std::map<unsigned, AttrBuilder> NumberedAttrBuilders;
Bill Wendling63b88192013-02-06 06:52:58 +0000137
Chris Lattnerac161bf2009-01-02 07:01:27 +0000138 public:
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000139 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000140 Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
Chris Lattner78103722011-06-17 03:16:47 +0000141 M(m) {}
Chris Lattnerad6f3352009-01-04 20:44:11 +0000142 bool Run();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000143
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000144 LLVMContext &getContext() { return Context; }
Owen Anderson09063ce2009-07-02 17:04:01 +0000145
Chris Lattnerac161bf2009-01-02 07:01:27 +0000146 private:
147
Benjamin Kramerc7583112010-09-27 17:42:11 +0000148 bool Error(LocTy L, const Twine &Msg) const {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000149 return Lex.Error(L, Msg);
150 }
Benjamin Kramerc7583112010-09-27 17:42:11 +0000151 bool TokError(const Twine &Msg) const {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000152 return Error(Lex.getLoc(), Msg);
153 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000154
Chris Lattnerac161bf2009-01-02 07:01:27 +0000155 /// GetGlobalVal - Get a value with the specified name or ID, creating a
156 /// forward reference record if needed. This can return null if the value
157 /// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000158 GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc);
159 GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000160
David Majnemerdad0a642014-06-27 18:19:56 +0000161 /// Get a Comdat with the specified name, creating a forward reference
162 /// record if needed.
163 Comdat *getComdat(const std::string &N, LocTy Loc);
164
Chris Lattnerac161bf2009-01-02 07:01:27 +0000165 // Helper Routines.
166 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3822f632009-01-02 08:05:26 +0000167 bool EatIfPresent(lltok::Kind T) {
168 if (Lex.getKind() != T) return false;
169 Lex.Lex();
170 return true;
171 }
Michael Ilseman92053172012-11-27 00:42:44 +0000172
173 FastMathFlags EatFastMathFlagsIfPresent() {
174 FastMathFlags FMF;
175 while (true)
176 switch (Lex.getKind()) {
Michael Ilseman65f14352012-12-09 21:12:04 +0000177 case lltok::kw_fast: FMF.setUnsafeAlgebra(); Lex.Lex(); continue;
178 case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue;
179 case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue;
180 case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue;
181 case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue;
Michael Ilseman92053172012-11-27 00:42:44 +0000182 default: return FMF;
183 }
184 return FMF;
185 }
186
Craig Topperada08572014-04-16 04:21:27 +0000187 bool ParseOptionalToken(lltok::Kind T, bool &Present,
188 LocTy *Loc = nullptr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000189 if (Lex.getKind() != T) {
190 Present = false;
191 } else {
Rafael Espindola026d1522011-01-13 01:30:30 +0000192 if (Loc)
193 *Loc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000194 Lex.Lex();
195 Present = true;
196 }
197 return false;
198 }
Chris Lattner3822f632009-01-02 08:05:26 +0000199 bool ParseStringConstant(std::string &Result);
200 bool ParseUInt32(unsigned &Val);
201 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000202 Loc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000203 return ParseUInt32(Val);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000204 }
Hal Finkelb0407ba2014-07-18 15:51:28 +0000205 bool ParseUInt64(uint64_t &Val);
206 bool ParseUInt64(uint64_t &Val, LocTy &Loc) {
207 Loc = Lex.getLoc();
208 return ParseUInt64(Val);
209 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000210
211 bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
212 bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000213 bool parseOptionalUnnamedAddr(bool &UnnamedAddr) {
214 return ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr);
215 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000216 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
Bill Wendling34c2eb22012-12-04 23:40:58 +0000217 bool ParseOptionalParamAttrs(AttrBuilder &B);
218 bool ParseOptionalReturnAttrs(AttrBuilder &B);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000219 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
220 bool ParseOptionalLinkage(unsigned &Linkage) {
221 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
222 }
223 bool ParseOptionalVisibility(unsigned &Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000224 bool ParseOptionalDLLStorageClass(unsigned &DLLStorageClass);
Sandeep Patel68c5f472009-09-02 08:44:58 +0000225 bool ParseOptionalCallingConv(CallingConv::ID &CC);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000226 bool ParseOptionalAlignment(unsigned &Alignment);
Hal Finkelb0407ba2014-07-18 15:51:28 +0000227 bool ParseOptionalDereferenceableBytes(uint64_t &Bytes);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000228 bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
229 AtomicOrdering &Ordering);
Tim Northovere94a5182014-03-11 10:48:52 +0000230 bool ParseOrdering(AtomicOrdering &Ordering);
Charles Davisbe5557e2010-02-12 00:31:15 +0000231 bool ParseOptionalStackAlignment(unsigned &Alignment);
Chris Lattnerb2f39502009-12-30 05:44:30 +0000232 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
Reid Kleckner436c42e2014-01-17 23:58:17 +0000233 bool ParseOptionalCommaInAlloca(bool &IsInAlloca);
Chris Lattner28f1eeb2009-12-30 05:14:00 +0000234 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma);
235 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
236 bool AteExtraComma;
237 if (ParseIndexList(Indices, AteExtraComma)) return true;
238 if (AteExtraComma)
239 return TokError("expected index");
240 return false;
241 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000242
Chris Lattnerac161bf2009-01-02 07:01:27 +0000243 // Top-Level Entities
244 bool ParseTopLevelEntities();
245 bool ValidateEndOfModule();
246 bool ParseTargetDefinition();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000247 bool ParseModuleAsm();
Bill Wendling706d3d62012-11-28 08:41:48 +0000248 bool ParseDepLibs(); // FIXME: Remove in 4.0.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000249 bool ParseUnnamedType();
250 bool ParseNamedType();
251 bool ParseDeclare();
252 bool ParseDefine();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000253
Chris Lattnerac161bf2009-01-02 07:01:27 +0000254 bool ParseGlobalType(bool &IsConstant);
Dan Gohman466876b2009-08-12 23:32:33 +0000255 bool ParseUnnamedGlobal();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000256 bool ParseNamedGlobal();
257 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
Nico Rieck7157bb72014-01-14 15:22:47 +0000258 bool HasLinkage, unsigned Visibility,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000259 unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000260 GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
Nico Rieck7157bb72014-01-14 15:22:47 +0000261 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000262 unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000263 GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
David Majnemerdad0a642014-06-27 18:19:56 +0000264 bool parseComdat();
Devang Patel39e64d42009-07-01 19:21:12 +0000265 bool ParseStandaloneMetadata();
Devang Patelbe626972009-07-29 00:34:02 +0000266 bool ParseNamedMetadata();
Chris Lattner1797fc72009-12-29 21:53:55 +0000267 bool ParseMDString(MDString *&Result);
Chris Lattner6dac02a2009-12-30 04:15:23 +0000268 bool ParseMDNodeID(MDNode *&Result);
Chris Lattner8eff0152010-04-01 05:14:45 +0000269 bool ParseMDNodeID(MDNode *&Result, unsigned &SlotNo);
Bill Wendling63b88192013-02-06 06:52:58 +0000270 bool ParseUnnamedAttrGrp();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000271 bool ParseFnAttributeValuePairs(AttrBuilder &B,
272 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000273 bool inAttrGrp, LocTy &BuiltinLoc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000274
Chris Lattnerac161bf2009-01-02 07:01:27 +0000275 // Type Parsing.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000276 bool ParseType(Type *&Result, bool AllowVoid = false);
277 bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000278 Loc = Lex.getLoc();
Chris Lattnerf880ca22009-03-09 04:49:14 +0000279 return ParseType(Result, AllowVoid);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000280 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000281 bool ParseAnonStructType(Type *&Result, bool Packed);
282 bool ParseStructBody(SmallVectorImpl<Type*> &Body);
283 bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
284 std::pair<Type*, LocTy> &Entry,
285 Type *&ResultTy);
286
287 bool ParseArrayVectorType(Type *&Result, bool isVector);
288 bool ParseFunctionType(Type *&Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000289
Chris Lattnerac161bf2009-01-02 07:01:27 +0000290 // Function Semantic Analysis.
291 class PerFunctionState {
292 LLParser &P;
293 Function &F;
294 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
295 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
296 std::vector<Value*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000297
Chris Lattner3432c622009-10-28 03:39:23 +0000298 /// FunctionNumber - If this is an unnamed function, this is the slot
299 /// number of it, otherwise it is -1.
300 int FunctionNumber;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000301 public:
Chris Lattner3432c622009-10-28 03:39:23 +0000302 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000303 ~PerFunctionState();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000304
Chris Lattnerac161bf2009-01-02 07:01:27 +0000305 Function &getFunction() const { return F; }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000306
Chris Lattner3432c622009-10-28 03:39:23 +0000307 bool FinishFunction();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000308
Chris Lattnerac161bf2009-01-02 07:01:27 +0000309 /// GetVal - Get a value with the specified name or ID, creating a
310 /// forward reference record if needed. This can return null if the value
311 /// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000312 Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc);
313 Value *GetVal(unsigned ID, Type *Ty, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000314
Chris Lattnerac161bf2009-01-02 07:01:27 +0000315 /// SetInstName - After an instruction is parsed and inserted into its
316 /// basic block, this installs its name.
317 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
318 Instruction *Inst);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000319
Chris Lattnerac161bf2009-01-02 07:01:27 +0000320 /// GetBB - Get a basic block with the specified name or ID, creating a
321 /// forward reference record if needed. This can return null if the value
322 /// is not a BasicBlock.
323 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
324 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000325
Chris Lattnerac161bf2009-01-02 07:01:27 +0000326 /// DefineBB - Define the specified basic block, which is either named or
327 /// unnamed. If there is an error, this returns null otherwise it returns
328 /// the block being defined.
329 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
330 };
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000331
Chris Lattner229907c2011-07-18 04:54:35 +0000332 bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +0000333 PerFunctionState *PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000334
Chris Lattner229907c2011-07-18 04:54:35 +0000335 bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
336 bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000337 return ParseValue(Ty, V, &PFS);
338 }
Chris Lattner229907c2011-07-18 04:54:35 +0000339 bool ParseValue(Type *Ty, Value *&V, LocTy &Loc,
Chris Lattnerac161bf2009-01-02 07:01:27 +0000340 PerFunctionState &PFS) {
341 Loc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000342 return ParseValue(Ty, V, &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000343 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000344
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000345 bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS);
346 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
347 return ParseTypeAndValue(V, &PFS);
348 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000349 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
350 Loc = Lex.getLoc();
351 return ParseTypeAndValue(V, PFS);
352 }
Chris Lattner3ed871f2009-10-27 19:13:16 +0000353 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
354 PerFunctionState &PFS);
355 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
356 LocTy Loc;
357 return ParseTypeAndBasicBlock(BB, Loc, PFS);
358 }
Victor Hernandezfa232232009-12-03 23:40:58 +0000359
Chris Lattner392be582010-02-12 20:49:41 +0000360
Chris Lattnerac161bf2009-01-02 07:01:27 +0000361 struct ParamInfo {
362 LocTy Loc;
363 Value *V;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000364 AttributeSet Attrs;
365 ParamInfo(LocTy loc, Value *v, AttributeSet attrs)
Chris Lattnerac161bf2009-01-02 07:01:27 +0000366 : Loc(loc), V(v), Attrs(attrs) {}
367 };
368 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
369 PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000370
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000371 // Constant Parsing.
Craig Topperada08572014-04-16 04:21:27 +0000372 bool ParseValID(ValID &ID, PerFunctionState *PFS = nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000373 bool ParseGlobalValue(Type *Ty, Constant *&V);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000374 bool ParseGlobalTypeAndValue(Constant *&V);
375 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
David Majnemerdad0a642014-06-27 18:19:56 +0000376 bool parseOptionalComdat(Comdat *&C);
Dan Gohmanc828c542010-08-24 02:24:03 +0000377 bool ParseMetadataListValue(ValID &ID, PerFunctionState *PFS);
Dan Gohman8939ba332010-07-14 18:26:50 +0000378 bool ParseMetadataValue(ValID &ID, PerFunctionState *PFS);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000379 bool ParseMDNodeVector(SmallVectorImpl<Value*> &, PerFunctionState *PFS);
Dan Gohman338d9a42010-08-24 02:05:17 +0000380 bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000381
Chris Lattnerac161bf2009-01-02 07:01:27 +0000382 // Function Parsing.
383 struct ArgInfo {
384 LocTy Loc;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000385 Type *Ty;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000386 AttributeSet Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000387 std::string Name;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000388 ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000389 : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000390 };
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000391 bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000392 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
393 bool ParseFunctionBody(Function &Fn);
394 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000395
Reid Kleckner5772b772014-04-24 20:14:34 +0000396 enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
397
Chris Lattner77b89dc2009-12-30 05:23:43 +0000398 // Instruction Parsing. Each instruction parsing routine can return with a
399 // normal result, an error result, or return having eaten an extra comma.
400 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
401 int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
402 PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000403 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000404
Chris Lattner33de4272011-06-17 06:49:41 +0000405 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000406 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
407 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +0000408 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000409 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +0000410 bool ParseResume(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000411
Chris Lattnereeefa9a2009-01-05 08:24:46 +0000412 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
413 unsigned OperandType);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000414 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
415 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
416 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
417 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +0000418 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000419 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
420 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
421 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000422 int ParsePHI(Instruction *&I, PerFunctionState &PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +0000423 bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +0000424 bool ParseCall(Instruction *&I, PerFunctionState &PFS,
425 CallInst::TailCallKind IsTail);
Chris Lattner78103722011-06-17 03:16:47 +0000426 int ParseAlloc(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +0000427 int ParseLoad(Instruction *&I, PerFunctionState &PFS);
428 int ParseStore(Instruction *&I, PerFunctionState &PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +0000429 int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS);
430 int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000431 int ParseFence(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000432 int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
433 int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
434 int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000435
436 bool ResolveForwardRefBlockAddresses(Function *TheFn,
Chris Lattner3432c622009-10-28 03:39:23 +0000437 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
438 PerFunctionState *PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000439 };
440} // End llvm namespace
441
442#endif