blob: 117cdcba2551ff64ecf5c427119c445a43c98c41 [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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_ASMPARSER_LLPARSER_H
15#define LLVM_LIB_ASMPARSER_LLPARSER_H
Chris Lattnerac161bf2009-01-02 07:01:27 +000016
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 Lattnerb1ed91f2011-07-09 17:41:24 +000055 t_ConstantStruct, // Value in ConstantStructElts.
56 t_PackedConstantStruct // Value in ConstantStructElts.
Chris Lattner3432c622009-10-28 03:39:23 +000057 } Kind;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000058
Chris Lattner3432c622009-10-28 03:39:23 +000059 LLLexer::LocTy Loc;
60 unsigned UIntVal;
61 std::string StrVal, StrVal2;
62 APSInt APSIntVal;
63 APFloat APFloatVal;
64 Constant *ConstantVal;
Reid Kleckner2ae03e12015-03-04 18:31:10 +000065 Constant **ConstantStructElts;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000066
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000067 ValID() : Kind(t_LocalID), APFloatVal(0.0) {}
Reid Kleckner2ae03e12015-03-04 18:31:10 +000068 ~ValID() {
69 if (Kind == t_ConstantStruct || Kind == t_PackedConstantStruct)
70 delete [] ConstantStructElts;
71 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +000072
Chris Lattner3432c622009-10-28 03:39:23 +000073 bool operator<(const ValID &RHS) const {
74 if (Kind == t_LocalID || Kind == t_GlobalID)
75 return UIntVal < RHS.UIntVal;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000076 assert((Kind == t_LocalName || Kind == t_GlobalName ||
Michael Ilseman26ee2b82012-11-15 22:34:00 +000077 Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) &&
Chris Lattner3432c622009-10-28 03:39:23 +000078 "Ordering not defined for this ValID kind yet");
79 return StrVal < RHS.StrVal;
80 }
81 };
Michael Ilseman26ee2b82012-11-15 22:34:00 +000082
Benjamin Kramer079b96e2013-09-11 18:05:11 +000083 class LLParser {
Chris Lattnerac161bf2009-01-02 07:01:27 +000084 public:
85 typedef LLLexer::LocTy LocTy;
86 private:
Chris Lattner2e664bd2010-04-07 04:08:57 +000087 LLVMContext &Context;
Chris Lattnerac161bf2009-01-02 07:01:27 +000088 LLLexer Lex;
89 Module *M;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000090
Chris Lattner8eff0152010-04-01 05:14:45 +000091 // Instruction metadata resolution. Each instruction can have a list of
92 // MDRef info associated with them.
Dan Gohman7c7f13a2010-08-24 14:31:06 +000093 //
94 // The simpler approach of just creating temporary MDNodes and then calling
95 // RAUW on them when the definition is processed doesn't work because some
96 // instruction metadata kinds, such as dbg, get stored in the IR in an
97 // "optimized" format which doesn't participate in the normal value use
98 // lists. This means that RAUW doesn't work, even on temporary MDNodes
99 // which otherwise support RAUW. Instead, we defer resolving MDNode
100 // references until the definitions have been processed.
Chris Lattner8eff0152010-04-01 05:14:45 +0000101 struct MDRef {
102 SMLoc Loc;
103 unsigned MDKind, MDSlot;
104 };
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000105
Manman Ren209b17c2013-09-28 00:22:27 +0000106 SmallVector<Instruction*, 64> InstsWithTBAATag;
107
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000108 // Type resolution handling data structures. The location is set when we
109 // have processed a use of the type but not a definition yet.
110 StringMap<std::pair<Type*, LocTy> > NamedTypes;
David Majnemer19b51052015-02-11 07:43:56 +0000111 std::map<unsigned, std::pair<Type*, LocTy> > NumberedTypes;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000112
David Majnemer19b51052015-02-11 07:43:56 +0000113 std::map<unsigned, TrackingMDNodeRef> NumberedMetadata;
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000114 std::map<unsigned, std::pair<TempMDTuple, LocTy>> ForwardRefMDNodes;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000115
116 // Global Value reference information.
117 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
118 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
119 std::vector<GlobalValue*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000120
David Majnemerdad0a642014-06-27 18:19:56 +0000121 // Comdat forward reference information.
122 std::map<std::string, LocTy> ForwardRefComdats;
123
Chris Lattner3432c622009-10-28 03:39:23 +0000124 // References to blockaddress. The key is the function ValID, the value is
125 // a list of references to blocks in that function.
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000126 std::map<ValID, std::map<ValID, GlobalValue *>> ForwardRefBlockAddresses;
127 class PerFunctionState;
128 /// Reference to per-function state to allow basic blocks to be
129 /// forward-referenced by blockaddress instructions within the same
130 /// function.
131 PerFunctionState *BlockAddressPFS;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000132
Bill Wendling63b88192013-02-06 06:52:58 +0000133 // Attribute builder reference information.
Bill Wendlingb32b0412013-02-08 06:32:06 +0000134 std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups;
135 std::map<unsigned, AttrBuilder> NumberedAttrBuilders;
Bill Wendling63b88192013-02-06 06:52:58 +0000136
Chris Lattnerac161bf2009-01-02 07:01:27 +0000137 public:
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000138 LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *m)
139 : Context(m->getContext()), Lex(F, SM, Err, m->getContext()), M(m),
140 BlockAddressPFS(nullptr) {}
Chris Lattnerad6f3352009-01-04 20:44:11 +0000141 bool Run();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000142
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000143 LLVMContext &getContext() { return Context; }
Owen Anderson09063ce2009-07-02 17:04:01 +0000144
Chris Lattnerac161bf2009-01-02 07:01:27 +0000145 private:
146
Benjamin Kramerc7583112010-09-27 17:42:11 +0000147 bool Error(LocTy L, const Twine &Msg) const {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000148 return Lex.Error(L, Msg);
149 }
Benjamin Kramerc7583112010-09-27 17:42:11 +0000150 bool TokError(const Twine &Msg) const {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000151 return Error(Lex.getLoc(), Msg);
152 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000153
Chris Lattnerac161bf2009-01-02 07:01:27 +0000154 /// GetGlobalVal - Get a value with the specified name or ID, creating a
155 /// forward reference record if needed. This can return null if the value
156 /// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000157 GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc);
158 GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000159
David Majnemerdad0a642014-06-27 18:19:56 +0000160 /// Get a Comdat with the specified name, creating a forward reference
161 /// record if needed.
162 Comdat *getComdat(const std::string &N, LocTy Loc);
163
Chris Lattnerac161bf2009-01-02 07:01:27 +0000164 // Helper Routines.
165 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3822f632009-01-02 08:05:26 +0000166 bool EatIfPresent(lltok::Kind T) {
167 if (Lex.getKind() != T) return false;
168 Lex.Lex();
169 return true;
170 }
Michael Ilseman92053172012-11-27 00:42:44 +0000171
172 FastMathFlags EatFastMathFlagsIfPresent() {
173 FastMathFlags FMF;
174 while (true)
175 switch (Lex.getKind()) {
Michael Ilseman65f14352012-12-09 21:12:04 +0000176 case lltok::kw_fast: FMF.setUnsafeAlgebra(); Lex.Lex(); continue;
177 case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue;
178 case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue;
179 case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue;
180 case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue;
Michael Ilseman92053172012-11-27 00:42:44 +0000181 default: return FMF;
182 }
183 return FMF;
184 }
185
Craig Topperada08572014-04-16 04:21:27 +0000186 bool ParseOptionalToken(lltok::Kind T, bool &Present,
187 LocTy *Loc = nullptr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000188 if (Lex.getKind() != T) {
189 Present = false;
190 } else {
Rafael Espindola026d1522011-01-13 01:30:30 +0000191 if (Loc)
192 *Loc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000193 Lex.Lex();
194 Present = true;
195 }
196 return false;
197 }
Chris Lattner3822f632009-01-02 08:05:26 +0000198 bool ParseStringConstant(std::string &Result);
199 bool ParseUInt32(unsigned &Val);
200 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000201 Loc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000202 return ParseUInt32(Val);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000203 }
Hal Finkelb0407ba2014-07-18 15:51:28 +0000204 bool ParseUInt64(uint64_t &Val);
205 bool ParseUInt64(uint64_t &Val, LocTy &Loc) {
206 Loc = Lex.getLoc();
207 return ParseUInt64(Val);
208 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000209
210 bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
211 bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000212 bool parseOptionalUnnamedAddr(bool &UnnamedAddr) {
213 return ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr);
214 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000215 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
Bill Wendling34c2eb22012-12-04 23:40:58 +0000216 bool ParseOptionalParamAttrs(AttrBuilder &B);
217 bool ParseOptionalReturnAttrs(AttrBuilder &B);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000218 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
219 bool ParseOptionalLinkage(unsigned &Linkage) {
220 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
221 }
222 bool ParseOptionalVisibility(unsigned &Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000223 bool ParseOptionalDLLStorageClass(unsigned &DLLStorageClass);
Alexey Samsonov17a9cff2014-09-10 18:00:17 +0000224 bool ParseOptionalCallingConv(unsigned &CC);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000225 bool ParseOptionalAlignment(unsigned &Alignment);
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000226 bool ParseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000227 bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
228 AtomicOrdering &Ordering);
Tim Northovere94a5182014-03-11 10:48:52 +0000229 bool ParseOrdering(AtomicOrdering &Ordering);
Charles Davisbe5557e2010-02-12 00:31:15 +0000230 bool ParseOptionalStackAlignment(unsigned &Alignment);
Chris Lattnerb2f39502009-12-30 05:44:30 +0000231 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
Reid Kleckner436c42e2014-01-17 23:58:17 +0000232 bool ParseOptionalCommaInAlloca(bool &IsInAlloca);
Chris Lattner28f1eeb2009-12-30 05:14:00 +0000233 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma);
234 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
235 bool AteExtraComma;
236 if (ParseIndexList(Indices, AteExtraComma)) return true;
237 if (AteExtraComma)
238 return TokError("expected index");
239 return false;
240 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000241
Chris Lattnerac161bf2009-01-02 07:01:27 +0000242 // Top-Level Entities
243 bool ParseTopLevelEntities();
244 bool ValidateEndOfModule();
245 bool ParseTargetDefinition();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000246 bool ParseModuleAsm();
Bill Wendling706d3d62012-11-28 08:41:48 +0000247 bool ParseDepLibs(); // FIXME: Remove in 4.0.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000248 bool ParseUnnamedType();
249 bool ParseNamedType();
250 bool ParseDeclare();
251 bool ParseDefine();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000252
Chris Lattnerac161bf2009-01-02 07:01:27 +0000253 bool ParseGlobalType(bool &IsConstant);
Dan Gohman466876b2009-08-12 23:32:33 +0000254 bool ParseUnnamedGlobal();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000255 bool ParseNamedGlobal();
256 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
Nico Rieck7157bb72014-01-14 15:22:47 +0000257 bool HasLinkage, unsigned Visibility,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000258 unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000259 GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
Rafael Espindola464fe022014-07-30 22:51:54 +0000260 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Linkage,
261 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000262 GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
David Majnemerdad0a642014-06-27 18:19:56 +0000263 bool parseComdat();
Devang Patel39e64d42009-07-01 19:21:12 +0000264 bool ParseStandaloneMetadata();
Devang Patelbe626972009-07-29 00:34:02 +0000265 bool ParseNamedMetadata();
Chris Lattner1797fc72009-12-29 21:53:55 +0000266 bool ParseMDString(MDString *&Result);
Chris Lattner6dac02a2009-12-30 04:15:23 +0000267 bool ParseMDNodeID(MDNode *&Result);
Bill Wendling63b88192013-02-06 06:52:58 +0000268 bool ParseUnnamedAttrGrp();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000269 bool ParseFnAttributeValuePairs(AttrBuilder &B,
270 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000271 bool inAttrGrp, LocTy &BuiltinLoc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000272
Chris Lattnerac161bf2009-01-02 07:01:27 +0000273 // Type Parsing.
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000274 bool ParseType(Type *&Result, const Twine &Msg, bool AllowVoid = false);
275 bool ParseType(Type *&Result, bool AllowVoid = false) {
276 return ParseType(Result, "expected type", AllowVoid);
277 }
278 bool ParseType(Type *&Result, const Twine &Msg, LocTy &Loc,
279 bool AllowVoid = false) {
280 Loc = Lex.getLoc();
281 return ParseType(Result, Msg, AllowVoid);
282 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000283 bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000284 Loc = Lex.getLoc();
Chris Lattnerf880ca22009-03-09 04:49:14 +0000285 return ParseType(Result, AllowVoid);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000286 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000287 bool ParseAnonStructType(Type *&Result, bool Packed);
288 bool ParseStructBody(SmallVectorImpl<Type*> &Body);
289 bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
290 std::pair<Type*, LocTy> &Entry,
291 Type *&ResultTy);
292
293 bool ParseArrayVectorType(Type *&Result, bool isVector);
294 bool ParseFunctionType(Type *&Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000295
Chris Lattnerac161bf2009-01-02 07:01:27 +0000296 // Function Semantic Analysis.
297 class PerFunctionState {
298 LLParser &P;
299 Function &F;
300 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
301 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
302 std::vector<Value*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000303
Chris Lattner3432c622009-10-28 03:39:23 +0000304 /// FunctionNumber - If this is an unnamed function, this is the slot
305 /// number of it, otherwise it is -1.
306 int FunctionNumber;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000307 public:
Chris Lattner3432c622009-10-28 03:39:23 +0000308 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000309 ~PerFunctionState();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000310
Chris Lattnerac161bf2009-01-02 07:01:27 +0000311 Function &getFunction() const { return F; }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000312
Chris Lattner3432c622009-10-28 03:39:23 +0000313 bool FinishFunction();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000314
Chris Lattnerac161bf2009-01-02 07:01:27 +0000315 /// GetVal - Get a value with the specified name or ID, creating a
316 /// forward reference record if needed. This can return null if the value
317 /// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000318 Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc);
319 Value *GetVal(unsigned ID, Type *Ty, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000320
Chris Lattnerac161bf2009-01-02 07:01:27 +0000321 /// SetInstName - After an instruction is parsed and inserted into its
322 /// basic block, this installs its name.
323 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
324 Instruction *Inst);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000325
Chris Lattnerac161bf2009-01-02 07:01:27 +0000326 /// GetBB - Get a basic block with the specified name or ID, creating a
327 /// forward reference record if needed. This can return null if the value
328 /// is not a BasicBlock.
329 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
330 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000331
Chris Lattnerac161bf2009-01-02 07:01:27 +0000332 /// DefineBB - Define the specified basic block, which is either named or
333 /// unnamed. If there is an error, this returns null otherwise it returns
334 /// the block being defined.
335 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000336
337 bool resolveForwardRefBlockAddresses();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000338 };
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000339
Chris Lattner229907c2011-07-18 04:54:35 +0000340 bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +0000341 PerFunctionState *PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000342
Chris Lattner229907c2011-07-18 04:54:35 +0000343 bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
344 bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000345 return ParseValue(Ty, V, &PFS);
346 }
Chris Lattner229907c2011-07-18 04:54:35 +0000347 bool ParseValue(Type *Ty, Value *&V, LocTy &Loc,
Chris Lattnerac161bf2009-01-02 07:01:27 +0000348 PerFunctionState &PFS) {
349 Loc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000350 return ParseValue(Ty, V, &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000351 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000352
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000353 bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS);
354 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
355 return ParseTypeAndValue(V, &PFS);
356 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000357 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
358 Loc = Lex.getLoc();
359 return ParseTypeAndValue(V, PFS);
360 }
Chris Lattner3ed871f2009-10-27 19:13:16 +0000361 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
362 PerFunctionState &PFS);
363 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
364 LocTy Loc;
365 return ParseTypeAndBasicBlock(BB, Loc, PFS);
366 }
Victor Hernandezfa232232009-12-03 23:40:58 +0000367
Chris Lattner392be582010-02-12 20:49:41 +0000368
Chris Lattnerac161bf2009-01-02 07:01:27 +0000369 struct ParamInfo {
370 LocTy Loc;
371 Value *V;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000372 AttributeSet Attrs;
373 ParamInfo(LocTy loc, Value *v, AttributeSet attrs)
Chris Lattnerac161bf2009-01-02 07:01:27 +0000374 : Loc(loc), V(v), Attrs(attrs) {}
375 };
376 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
Reid Kleckner83498642014-08-26 00:33:28 +0000377 PerFunctionState &PFS,
378 bool IsMustTailCall = false,
379 bool InVarArgsFunc = false);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000380
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000381 // Constant Parsing.
Craig Topperada08572014-04-16 04:21:27 +0000382 bool ParseValID(ValID &ID, PerFunctionState *PFS = nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000383 bool ParseGlobalValue(Type *Ty, Constant *&V);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000384 bool ParseGlobalTypeAndValue(Constant *&V);
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000385 bool ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts);
Rafael Espindola83a362c2015-01-06 22:55:16 +0000386 bool parseOptionalComdat(StringRef GlobalName, Comdat *&C);
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000387 bool ParseMetadataAsValue(Value *&V, PerFunctionState &PFS);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +0000388 bool ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
389 PerFunctionState *PFS);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000390 bool ParseMetadata(Metadata *&MD, PerFunctionState *PFS);
Duncan P. N. Exon Smith58ef9d12015-01-12 21:23:11 +0000391 bool ParseMDTuple(MDNode *&MD, bool IsDistinct = false);
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +0000392 bool ParseMDNode(MDNode *&MD);
393 bool ParseMDNodeTail(MDNode *&MD);
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000394 bool ParseMDNodeVector(SmallVectorImpl<Metadata *> &MDs);
Dan Gohman338d9a42010-08-24 02:05:17 +0000395 bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000396
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +0000397 template <class FieldTy>
398 bool ParseMDField(LocTy Loc, StringRef Name, FieldTy &Result);
Duncan P. N. Exon Smitha7477282015-01-20 02:42:29 +0000399 template <class FieldTy> bool ParseMDField(StringRef Name, FieldTy &Result);
Duncan P. N. Exon Smith13890af2015-01-19 23:32:36 +0000400 template <class ParserTy>
Duncan P. N. Exon Smith66ca92e2015-01-19 23:39:32 +0000401 bool ParseMDFieldsImplBody(ParserTy parseField);
402 template <class ParserTy>
Duncan P. N. Exon Smith13890af2015-01-19 23:32:36 +0000403 bool ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +0000404 bool ParseSpecializedMDNode(MDNode *&N, bool IsDistinct = false);
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +0000405
406#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
407 bool Parse##CLASS(MDNode *&Result, bool IsDistinct);
408#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +0000409
Chris Lattnerac161bf2009-01-02 07:01:27 +0000410 // Function Parsing.
411 struct ArgInfo {
412 LocTy Loc;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000413 Type *Ty;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000414 AttributeSet Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000415 std::string Name;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000416 ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000417 : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000418 };
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000419 bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000420 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
421 bool ParseFunctionBody(Function &Fn);
422 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000423
Reid Kleckner5772b772014-04-24 20:14:34 +0000424 enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
425
Chris Lattner77b89dc2009-12-30 05:23:43 +0000426 // Instruction Parsing. Each instruction parsing routine can return with a
427 // normal result, an error result, or return having eaten an extra comma.
428 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
429 int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
430 PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000431 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000432
Chris Lattner33de4272011-06-17 06:49:41 +0000433 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000434 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
435 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +0000436 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000437 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +0000438 bool ParseResume(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000439
Chris Lattnereeefa9a2009-01-05 08:24:46 +0000440 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
441 unsigned OperandType);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000442 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
443 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
444 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
445 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +0000446 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000447 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
448 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
449 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000450 int ParsePHI(Instruction *&I, PerFunctionState &PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +0000451 bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +0000452 bool ParseCall(Instruction *&I, PerFunctionState &PFS,
453 CallInst::TailCallKind IsTail);
Chris Lattner78103722011-06-17 03:16:47 +0000454 int ParseAlloc(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +0000455 int ParseLoad(Instruction *&I, PerFunctionState &PFS);
456 int ParseStore(Instruction *&I, PerFunctionState &PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +0000457 int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS);
458 int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000459 int ParseFence(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000460 int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
461 int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
462 int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000463
464 // Use-list order directives.
465 bool ParseUseListOrder(PerFunctionState *PFS = nullptr);
466 bool ParseUseListOrderBB();
467 bool ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes);
468 bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000469 };
470} // End llvm namespace
471
472#endif