blob: 2efb260d66b8827ab27953fcd3f01f75d51c2044 [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 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000205
206 bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
207 bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000208 bool parseOptionalUnnamedAddr(bool &UnnamedAddr) {
209 return ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr);
210 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000211 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
Bill Wendling34c2eb22012-12-04 23:40:58 +0000212 bool ParseOptionalParamAttrs(AttrBuilder &B);
213 bool ParseOptionalReturnAttrs(AttrBuilder &B);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000214 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
215 bool ParseOptionalLinkage(unsigned &Linkage) {
216 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
217 }
218 bool ParseOptionalVisibility(unsigned &Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000219 bool ParseOptionalDLLStorageClass(unsigned &DLLStorageClass);
Sandeep Patel68c5f472009-09-02 08:44:58 +0000220 bool ParseOptionalCallingConv(CallingConv::ID &CC);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000221 bool ParseOptionalAlignment(unsigned &Alignment);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000222 bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
223 AtomicOrdering &Ordering);
Tim Northovere94a5182014-03-11 10:48:52 +0000224 bool ParseOrdering(AtomicOrdering &Ordering);
Charles Davisbe5557e2010-02-12 00:31:15 +0000225 bool ParseOptionalStackAlignment(unsigned &Alignment);
Chris Lattnerb2f39502009-12-30 05:44:30 +0000226 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
Reid Kleckner436c42e2014-01-17 23:58:17 +0000227 bool ParseOptionalCommaInAlloca(bool &IsInAlloca);
Chris Lattner28f1eeb2009-12-30 05:14:00 +0000228 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma);
229 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
230 bool AteExtraComma;
231 if (ParseIndexList(Indices, AteExtraComma)) return true;
232 if (AteExtraComma)
233 return TokError("expected index");
234 return false;
235 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000236
Chris Lattnerac161bf2009-01-02 07:01:27 +0000237 // Top-Level Entities
238 bool ParseTopLevelEntities();
239 bool ValidateEndOfModule();
240 bool ParseTargetDefinition();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000241 bool ParseModuleAsm();
Bill Wendling706d3d62012-11-28 08:41:48 +0000242 bool ParseDepLibs(); // FIXME: Remove in 4.0.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000243 bool ParseUnnamedType();
244 bool ParseNamedType();
245 bool ParseDeclare();
246 bool ParseDefine();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000247
Chris Lattnerac161bf2009-01-02 07:01:27 +0000248 bool ParseGlobalType(bool &IsConstant);
Dan Gohman466876b2009-08-12 23:32:33 +0000249 bool ParseUnnamedGlobal();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000250 bool ParseNamedGlobal();
251 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
Nico Rieck7157bb72014-01-14 15:22:47 +0000252 bool HasLinkage, unsigned Visibility,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000253 unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000254 GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
Nico Rieck7157bb72014-01-14 15:22:47 +0000255 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000256 unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000257 GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
David Majnemerdad0a642014-06-27 18:19:56 +0000258 bool parseComdat();
Devang Patel39e64d42009-07-01 19:21:12 +0000259 bool ParseStandaloneMetadata();
Devang Patelbe626972009-07-29 00:34:02 +0000260 bool ParseNamedMetadata();
Chris Lattner1797fc72009-12-29 21:53:55 +0000261 bool ParseMDString(MDString *&Result);
Chris Lattner6dac02a2009-12-30 04:15:23 +0000262 bool ParseMDNodeID(MDNode *&Result);
Chris Lattner8eff0152010-04-01 05:14:45 +0000263 bool ParseMDNodeID(MDNode *&Result, unsigned &SlotNo);
Bill Wendling63b88192013-02-06 06:52:58 +0000264 bool ParseUnnamedAttrGrp();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000265 bool ParseFnAttributeValuePairs(AttrBuilder &B,
266 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000267 bool inAttrGrp, LocTy &BuiltinLoc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000268
Chris Lattnerac161bf2009-01-02 07:01:27 +0000269 // Type Parsing.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000270 bool ParseType(Type *&Result, bool AllowVoid = false);
271 bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000272 Loc = Lex.getLoc();
Chris Lattnerf880ca22009-03-09 04:49:14 +0000273 return ParseType(Result, AllowVoid);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000274 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000275 bool ParseAnonStructType(Type *&Result, bool Packed);
276 bool ParseStructBody(SmallVectorImpl<Type*> &Body);
277 bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
278 std::pair<Type*, LocTy> &Entry,
279 Type *&ResultTy);
280
281 bool ParseArrayVectorType(Type *&Result, bool isVector);
282 bool ParseFunctionType(Type *&Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000283
Chris Lattnerac161bf2009-01-02 07:01:27 +0000284 // Function Semantic Analysis.
285 class PerFunctionState {
286 LLParser &P;
287 Function &F;
288 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
289 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
290 std::vector<Value*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000291
Chris Lattner3432c622009-10-28 03:39:23 +0000292 /// FunctionNumber - If this is an unnamed function, this is the slot
293 /// number of it, otherwise it is -1.
294 int FunctionNumber;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000295 public:
Chris Lattner3432c622009-10-28 03:39:23 +0000296 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000297 ~PerFunctionState();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000298
Chris Lattnerac161bf2009-01-02 07:01:27 +0000299 Function &getFunction() const { return F; }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000300
Chris Lattner3432c622009-10-28 03:39:23 +0000301 bool FinishFunction();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000302
Chris Lattnerac161bf2009-01-02 07:01:27 +0000303 /// GetVal - Get a value with the specified name or ID, creating a
304 /// forward reference record if needed. This can return null if the value
305 /// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000306 Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc);
307 Value *GetVal(unsigned ID, Type *Ty, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000308
Chris Lattnerac161bf2009-01-02 07:01:27 +0000309 /// SetInstName - After an instruction is parsed and inserted into its
310 /// basic block, this installs its name.
311 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
312 Instruction *Inst);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000313
Chris Lattnerac161bf2009-01-02 07:01:27 +0000314 /// GetBB - Get a basic block with the specified name or ID, creating a
315 /// forward reference record if needed. This can return null if the value
316 /// is not a BasicBlock.
317 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
318 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000319
Chris Lattnerac161bf2009-01-02 07:01:27 +0000320 /// DefineBB - Define the specified basic block, which is either named or
321 /// unnamed. If there is an error, this returns null otherwise it returns
322 /// the block being defined.
323 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
324 };
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000325
Chris Lattner229907c2011-07-18 04:54:35 +0000326 bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +0000327 PerFunctionState *PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000328
Chris Lattner229907c2011-07-18 04:54:35 +0000329 bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
330 bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000331 return ParseValue(Ty, V, &PFS);
332 }
Chris Lattner229907c2011-07-18 04:54:35 +0000333 bool ParseValue(Type *Ty, Value *&V, LocTy &Loc,
Chris Lattnerac161bf2009-01-02 07:01:27 +0000334 PerFunctionState &PFS) {
335 Loc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000336 return ParseValue(Ty, V, &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000337 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000338
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000339 bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS);
340 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
341 return ParseTypeAndValue(V, &PFS);
342 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000343 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
344 Loc = Lex.getLoc();
345 return ParseTypeAndValue(V, PFS);
346 }
Chris Lattner3ed871f2009-10-27 19:13:16 +0000347 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
348 PerFunctionState &PFS);
349 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
350 LocTy Loc;
351 return ParseTypeAndBasicBlock(BB, Loc, PFS);
352 }
Victor Hernandezfa232232009-12-03 23:40:58 +0000353
Chris Lattner392be582010-02-12 20:49:41 +0000354
Chris Lattnerac161bf2009-01-02 07:01:27 +0000355 struct ParamInfo {
356 LocTy Loc;
357 Value *V;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000358 AttributeSet Attrs;
359 ParamInfo(LocTy loc, Value *v, AttributeSet attrs)
Chris Lattnerac161bf2009-01-02 07:01:27 +0000360 : Loc(loc), V(v), Attrs(attrs) {}
361 };
362 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
363 PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000364
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000365 // Constant Parsing.
Craig Topperada08572014-04-16 04:21:27 +0000366 bool ParseValID(ValID &ID, PerFunctionState *PFS = nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000367 bool ParseGlobalValue(Type *Ty, Constant *&V);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000368 bool ParseGlobalTypeAndValue(Constant *&V);
369 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
David Majnemerdad0a642014-06-27 18:19:56 +0000370 bool parseOptionalComdat(Comdat *&C);
Dan Gohmanc828c542010-08-24 02:24:03 +0000371 bool ParseMetadataListValue(ValID &ID, PerFunctionState *PFS);
Dan Gohman8939ba332010-07-14 18:26:50 +0000372 bool ParseMetadataValue(ValID &ID, PerFunctionState *PFS);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000373 bool ParseMDNodeVector(SmallVectorImpl<Value*> &, PerFunctionState *PFS);
Dan Gohman338d9a42010-08-24 02:05:17 +0000374 bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000375
Chris Lattnerac161bf2009-01-02 07:01:27 +0000376 // Function Parsing.
377 struct ArgInfo {
378 LocTy Loc;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000379 Type *Ty;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000380 AttributeSet Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000381 std::string Name;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000382 ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000383 : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000384 };
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000385 bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000386 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
387 bool ParseFunctionBody(Function &Fn);
388 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000389
Reid Kleckner5772b772014-04-24 20:14:34 +0000390 enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
391
Chris Lattner77b89dc2009-12-30 05:23:43 +0000392 // Instruction Parsing. Each instruction parsing routine can return with a
393 // normal result, an error result, or return having eaten an extra comma.
394 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
395 int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
396 PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000397 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000398
Chris Lattner33de4272011-06-17 06:49:41 +0000399 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000400 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
401 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +0000402 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000403 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +0000404 bool ParseResume(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000405
Chris Lattnereeefa9a2009-01-05 08:24:46 +0000406 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
407 unsigned OperandType);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000408 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
409 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
410 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
411 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +0000412 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000413 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
414 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
415 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000416 int ParsePHI(Instruction *&I, PerFunctionState &PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +0000417 bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +0000418 bool ParseCall(Instruction *&I, PerFunctionState &PFS,
419 CallInst::TailCallKind IsTail);
Chris Lattner78103722011-06-17 03:16:47 +0000420 int ParseAlloc(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +0000421 int ParseLoad(Instruction *&I, PerFunctionState &PFS);
422 int ParseStore(Instruction *&I, PerFunctionState &PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +0000423 int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS);
424 int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000425 int ParseFence(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000426 int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
427 int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
428 int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000429
430 bool ResolveForwardRefBlockAddresses(Function *TheFn,
Chris Lattner3432c622009-10-28 03:39:23 +0000431 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
432 PerFunctionState *PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000433 };
434} // End llvm namespace
435
436#endif