blob: d8d272b500dec40b0cab0c6b378f2961f4dca605 [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;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +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) {}
68 ~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 };
105 DenseMap<Instruction*, std::vector<MDRef> > ForwardRefInstMetadata;
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000106
Manman Ren209b17c2013-09-28 00:22:27 +0000107 SmallVector<Instruction*, 64> InstsWithTBAATag;
108
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000109 // Type resolution handling data structures. The location is set when we
110 // have processed a use of the type but not a definition yet.
111 StringMap<std::pair<Type*, LocTy> > NamedTypes;
112 std::vector<std::pair<Type*, LocTy> > NumberedTypes;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000113
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000114 std::vector<TrackingMDNodeRef> NumberedMetadata;
115 std::map<unsigned, std::pair<MDNodeFwdDecl *, LocTy>> ForwardRefMDNodes;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000116
117 // Global Value reference information.
118 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
119 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
120 std::vector<GlobalValue*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000121
David Majnemerdad0a642014-06-27 18:19:56 +0000122 // Comdat forward reference information.
123 std::map<std::string, LocTy> ForwardRefComdats;
124
Chris Lattner3432c622009-10-28 03:39:23 +0000125 // References to blockaddress. The key is the function ValID, the value is
126 // a list of references to blocks in that function.
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000127 std::map<ValID, std::map<ValID, GlobalValue *>> ForwardRefBlockAddresses;
128 class PerFunctionState;
129 /// Reference to per-function state to allow basic blocks to be
130 /// forward-referenced by blockaddress instructions within the same
131 /// function.
132 PerFunctionState *BlockAddressPFS;
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:
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000139 LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *m)
140 : Context(m->getContext()), Lex(F, SM, Err, m->getContext()), M(m),
141 BlockAddressPFS(nullptr) {}
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);
Alexey Samsonov17a9cff2014-09-10 18:00:17 +0000225 bool ParseOptionalCallingConv(unsigned &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);
Rafael Espindola464fe022014-07-30 22:51:54 +0000261 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Linkage,
262 unsigned Visibility, 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.
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000276 bool ParseType(Type *&Result, const Twine &Msg, bool AllowVoid = false);
277 bool ParseType(Type *&Result, bool AllowVoid = false) {
278 return ParseType(Result, "expected type", AllowVoid);
279 }
280 bool ParseType(Type *&Result, const Twine &Msg, LocTy &Loc,
281 bool AllowVoid = false) {
282 Loc = Lex.getLoc();
283 return ParseType(Result, Msg, AllowVoid);
284 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000285 bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000286 Loc = Lex.getLoc();
Chris Lattnerf880ca22009-03-09 04:49:14 +0000287 return ParseType(Result, AllowVoid);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000288 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000289 bool ParseAnonStructType(Type *&Result, bool Packed);
290 bool ParseStructBody(SmallVectorImpl<Type*> &Body);
291 bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
292 std::pair<Type*, LocTy> &Entry,
293 Type *&ResultTy);
294
295 bool ParseArrayVectorType(Type *&Result, bool isVector);
296 bool ParseFunctionType(Type *&Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000297
Chris Lattnerac161bf2009-01-02 07:01:27 +0000298 // Function Semantic Analysis.
299 class PerFunctionState {
300 LLParser &P;
301 Function &F;
302 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
303 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
304 std::vector<Value*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000305
Chris Lattner3432c622009-10-28 03:39:23 +0000306 /// FunctionNumber - If this is an unnamed function, this is the slot
307 /// number of it, otherwise it is -1.
308 int FunctionNumber;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000309 public:
Chris Lattner3432c622009-10-28 03:39:23 +0000310 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000311 ~PerFunctionState();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000312
Chris Lattnerac161bf2009-01-02 07:01:27 +0000313 Function &getFunction() const { return F; }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000314
Chris Lattner3432c622009-10-28 03:39:23 +0000315 bool FinishFunction();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000316
Chris Lattnerac161bf2009-01-02 07:01:27 +0000317 /// GetVal - Get a value with the specified name or ID, creating a
318 /// forward reference record if needed. This can return null if the value
319 /// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000320 Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc);
321 Value *GetVal(unsigned ID, Type *Ty, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000322
Chris Lattnerac161bf2009-01-02 07:01:27 +0000323 /// SetInstName - After an instruction is parsed and inserted into its
324 /// basic block, this installs its name.
325 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
326 Instruction *Inst);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000327
Chris Lattnerac161bf2009-01-02 07:01:27 +0000328 /// GetBB - Get a basic block with the specified name or ID, creating a
329 /// forward reference record if needed. This can return null if the value
330 /// is not a BasicBlock.
331 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
332 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000333
Chris Lattnerac161bf2009-01-02 07:01:27 +0000334 /// DefineBB - Define the specified basic block, which is either named or
335 /// unnamed. If there is an error, this returns null otherwise it returns
336 /// the block being defined.
337 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000338
339 bool resolveForwardRefBlockAddresses();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000340 };
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000341
Chris Lattner229907c2011-07-18 04:54:35 +0000342 bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +0000343 PerFunctionState *PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000344
Chris Lattner229907c2011-07-18 04:54:35 +0000345 bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
346 bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000347 return ParseValue(Ty, V, &PFS);
348 }
Chris Lattner229907c2011-07-18 04:54:35 +0000349 bool ParseValue(Type *Ty, Value *&V, LocTy &Loc,
Chris Lattnerac161bf2009-01-02 07:01:27 +0000350 PerFunctionState &PFS) {
351 Loc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000352 return ParseValue(Ty, V, &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000353 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000354
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000355 bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS);
356 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
357 return ParseTypeAndValue(V, &PFS);
358 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000359 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
360 Loc = Lex.getLoc();
361 return ParseTypeAndValue(V, PFS);
362 }
Chris Lattner3ed871f2009-10-27 19:13:16 +0000363 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
364 PerFunctionState &PFS);
365 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
366 LocTy Loc;
367 return ParseTypeAndBasicBlock(BB, Loc, PFS);
368 }
Victor Hernandezfa232232009-12-03 23:40:58 +0000369
Chris Lattner392be582010-02-12 20:49:41 +0000370
Chris Lattnerac161bf2009-01-02 07:01:27 +0000371 struct ParamInfo {
372 LocTy Loc;
373 Value *V;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000374 AttributeSet Attrs;
375 ParamInfo(LocTy loc, Value *v, AttributeSet attrs)
Chris Lattnerac161bf2009-01-02 07:01:27 +0000376 : Loc(loc), V(v), Attrs(attrs) {}
377 };
378 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
Reid Kleckner83498642014-08-26 00:33:28 +0000379 PerFunctionState &PFS,
380 bool IsMustTailCall = false,
381 bool InVarArgsFunc = false);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000382
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000383 // Constant Parsing.
Craig Topperada08572014-04-16 04:21:27 +0000384 bool ParseValID(ValID &ID, PerFunctionState *PFS = nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000385 bool ParseGlobalValue(Type *Ty, Constant *&V);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000386 bool ParseGlobalTypeAndValue(Constant *&V);
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000387 bool ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts);
Rafael Espindola83a362c2015-01-06 22:55:16 +0000388 bool parseOptionalComdat(StringRef GlobalName, Comdat *&C);
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000389 bool ParseMetadataAsValue(Value *&V, PerFunctionState &PFS);
390 bool ParseValueAsMetadata(Metadata *&MD, PerFunctionState *PFS);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000391 bool ParseMetadata(Metadata *&MD, PerFunctionState *PFS);
392 bool ParseMDNode(MDNode *&MD);
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000393 bool ParseMDNodeVector(SmallVectorImpl<Metadata *> &MDs);
Dan Gohman338d9a42010-08-24 02:05:17 +0000394 bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000395
Chris Lattnerac161bf2009-01-02 07:01:27 +0000396 // Function Parsing.
397 struct ArgInfo {
398 LocTy Loc;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000399 Type *Ty;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000400 AttributeSet Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000401 std::string Name;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000402 ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000403 : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000404 };
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000405 bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000406 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
407 bool ParseFunctionBody(Function &Fn);
408 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000409
Reid Kleckner5772b772014-04-24 20:14:34 +0000410 enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
411
Chris Lattner77b89dc2009-12-30 05:23:43 +0000412 // Instruction Parsing. Each instruction parsing routine can return with a
413 // normal result, an error result, or return having eaten an extra comma.
414 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
415 int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
416 PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000417 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000418
Chris Lattner33de4272011-06-17 06:49:41 +0000419 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000420 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
421 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +0000422 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000423 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +0000424 bool ParseResume(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000425
Chris Lattnereeefa9a2009-01-05 08:24:46 +0000426 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
427 unsigned OperandType);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000428 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
429 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
430 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
431 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +0000432 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000433 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
434 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
435 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000436 int ParsePHI(Instruction *&I, PerFunctionState &PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +0000437 bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +0000438 bool ParseCall(Instruction *&I, PerFunctionState &PFS,
439 CallInst::TailCallKind IsTail);
Chris Lattner78103722011-06-17 03:16:47 +0000440 int ParseAlloc(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +0000441 int ParseLoad(Instruction *&I, PerFunctionState &PFS);
442 int ParseStore(Instruction *&I, PerFunctionState &PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +0000443 int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS);
444 int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000445 int ParseFence(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000446 int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
447 int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
448 int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000449
450 // Use-list order directives.
451 bool ParseUseListOrder(PerFunctionState *PFS = nullptr);
452 bool ParseUseListOrderBB();
453 bool ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes);
454 bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000455 };
456} // End llvm namespace
457
458#endif