blob: d32e58e83f8776d99b20e0e0fd2def3ea7ed8b2c [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.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000055 t_Metadata, // Value in MetadataVal.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000056 t_ConstantStruct, // Value in ConstantStructElts.
57 t_PackedConstantStruct // Value in ConstantStructElts.
Chris Lattner3432c622009-10-28 03:39:23 +000058 } Kind;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000059
Chris Lattner3432c622009-10-28 03:39:23 +000060 LLLexer::LocTy Loc;
61 unsigned UIntVal;
62 std::string StrVal, StrVal2;
63 APSInt APSIntVal;
64 APFloat APFloatVal;
65 Constant *ConstantVal;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000066 MetadataAsValue *MetadataVal;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000067 Constant **ConstantStructElts;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000068
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000069 ValID() : Kind(t_LocalID), APFloatVal(0.0) {}
70 ~ValID() {
71 if (Kind == t_ConstantStruct || Kind == t_PackedConstantStruct)
72 delete [] ConstantStructElts;
73 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +000074
Chris Lattner3432c622009-10-28 03:39:23 +000075 bool operator<(const ValID &RHS) const {
76 if (Kind == t_LocalID || Kind == t_GlobalID)
77 return UIntVal < RHS.UIntVal;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000078 assert((Kind == t_LocalName || Kind == t_GlobalName ||
Michael Ilseman26ee2b82012-11-15 22:34:00 +000079 Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) &&
Chris Lattner3432c622009-10-28 03:39:23 +000080 "Ordering not defined for this ValID kind yet");
81 return StrVal < RHS.StrVal;
82 }
83 };
Michael Ilseman26ee2b82012-11-15 22:34:00 +000084
Benjamin Kramer079b96e2013-09-11 18:05:11 +000085 class LLParser {
Chris Lattnerac161bf2009-01-02 07:01:27 +000086 public:
87 typedef LLLexer::LocTy LocTy;
88 private:
Chris Lattner2e664bd2010-04-07 04:08:57 +000089 LLVMContext &Context;
Chris Lattnerac161bf2009-01-02 07:01:27 +000090 LLLexer Lex;
91 Module *M;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000092
Chris Lattner8eff0152010-04-01 05:14:45 +000093 // Instruction metadata resolution. Each instruction can have a list of
94 // MDRef info associated with them.
Dan Gohman7c7f13a2010-08-24 14:31:06 +000095 //
96 // The simpler approach of just creating temporary MDNodes and then calling
97 // RAUW on them when the definition is processed doesn't work because some
98 // instruction metadata kinds, such as dbg, get stored in the IR in an
99 // "optimized" format which doesn't participate in the normal value use
100 // lists. This means that RAUW doesn't work, even on temporary MDNodes
101 // which otherwise support RAUW. Instead, we defer resolving MDNode
102 // references until the definitions have been processed.
Chris Lattner8eff0152010-04-01 05:14:45 +0000103 struct MDRef {
104 SMLoc Loc;
105 unsigned MDKind, MDSlot;
106 };
107 DenseMap<Instruction*, std::vector<MDRef> > ForwardRefInstMetadata;
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000108
Manman Ren209b17c2013-09-28 00:22:27 +0000109 SmallVector<Instruction*, 64> InstsWithTBAATag;
110
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000111 // Type resolution handling data structures. The location is set when we
112 // have processed a use of the type but not a definition yet.
113 StringMap<std::pair<Type*, LocTy> > NamedTypes;
114 std::vector<std::pair<Type*, LocTy> > NumberedTypes;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000115
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000116 std::vector<TrackingMDNodeRef> NumberedMetadata;
117 std::map<unsigned, std::pair<MDNodeFwdDecl *, LocTy>> ForwardRefMDNodes;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000118
119 // Global Value reference information.
120 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
121 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
122 std::vector<GlobalValue*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000123
David Majnemerdad0a642014-06-27 18:19:56 +0000124 // Comdat forward reference information.
125 std::map<std::string, LocTy> ForwardRefComdats;
126
Chris Lattner3432c622009-10-28 03:39:23 +0000127 // References to blockaddress. The key is the function ValID, the value is
128 // a list of references to blocks in that function.
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000129 std::map<ValID, std::map<ValID, GlobalValue *>> ForwardRefBlockAddresses;
130 class PerFunctionState;
131 /// Reference to per-function state to allow basic blocks to be
132 /// forward-referenced by blockaddress instructions within the same
133 /// function.
134 PerFunctionState *BlockAddressPFS;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000135
Bill Wendling63b88192013-02-06 06:52:58 +0000136 // Attribute builder reference information.
Bill Wendlingb32b0412013-02-08 06:32:06 +0000137 std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups;
138 std::map<unsigned, AttrBuilder> NumberedAttrBuilders;
Bill Wendling63b88192013-02-06 06:52:58 +0000139
Chris Lattnerac161bf2009-01-02 07:01:27 +0000140 public:
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000141 LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *m)
142 : Context(m->getContext()), Lex(F, SM, Err, m->getContext()), M(m),
143 BlockAddressPFS(nullptr) {}
Chris Lattnerad6f3352009-01-04 20:44:11 +0000144 bool Run();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000145
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000146 LLVMContext &getContext() { return Context; }
Owen Anderson09063ce2009-07-02 17:04:01 +0000147
Chris Lattnerac161bf2009-01-02 07:01:27 +0000148 private:
149
Benjamin Kramerc7583112010-09-27 17:42:11 +0000150 bool Error(LocTy L, const Twine &Msg) const {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000151 return Lex.Error(L, Msg);
152 }
Benjamin Kramerc7583112010-09-27 17:42:11 +0000153 bool TokError(const Twine &Msg) const {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000154 return Error(Lex.getLoc(), Msg);
155 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000156
Chris Lattnerac161bf2009-01-02 07:01:27 +0000157 /// GetGlobalVal - Get a value with the specified name or ID, creating a
158 /// forward reference record if needed. This can return null if the value
159 /// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000160 GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc);
161 GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000162
David Majnemerdad0a642014-06-27 18:19:56 +0000163 /// Get a Comdat with the specified name, creating a forward reference
164 /// record if needed.
165 Comdat *getComdat(const std::string &N, LocTy Loc);
166
Chris Lattnerac161bf2009-01-02 07:01:27 +0000167 // Helper Routines.
168 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3822f632009-01-02 08:05:26 +0000169 bool EatIfPresent(lltok::Kind T) {
170 if (Lex.getKind() != T) return false;
171 Lex.Lex();
172 return true;
173 }
Michael Ilseman92053172012-11-27 00:42:44 +0000174
175 FastMathFlags EatFastMathFlagsIfPresent() {
176 FastMathFlags FMF;
177 while (true)
178 switch (Lex.getKind()) {
Michael Ilseman65f14352012-12-09 21:12:04 +0000179 case lltok::kw_fast: FMF.setUnsafeAlgebra(); Lex.Lex(); continue;
180 case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue;
181 case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue;
182 case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue;
183 case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue;
Michael Ilseman92053172012-11-27 00:42:44 +0000184 default: return FMF;
185 }
186 return FMF;
187 }
188
Craig Topperada08572014-04-16 04:21:27 +0000189 bool ParseOptionalToken(lltok::Kind T, bool &Present,
190 LocTy *Loc = nullptr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000191 if (Lex.getKind() != T) {
192 Present = false;
193 } else {
Rafael Espindola026d1522011-01-13 01:30:30 +0000194 if (Loc)
195 *Loc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000196 Lex.Lex();
197 Present = true;
198 }
199 return false;
200 }
Chris Lattner3822f632009-01-02 08:05:26 +0000201 bool ParseStringConstant(std::string &Result);
202 bool ParseUInt32(unsigned &Val);
203 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000204 Loc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000205 return ParseUInt32(Val);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000206 }
Hal Finkelb0407ba2014-07-18 15:51:28 +0000207 bool ParseUInt64(uint64_t &Val);
208 bool ParseUInt64(uint64_t &Val, LocTy &Loc) {
209 Loc = Lex.getLoc();
210 return ParseUInt64(Val);
211 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000212
213 bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
214 bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000215 bool parseOptionalUnnamedAddr(bool &UnnamedAddr) {
216 return ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr);
217 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000218 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
Bill Wendling34c2eb22012-12-04 23:40:58 +0000219 bool ParseOptionalParamAttrs(AttrBuilder &B);
220 bool ParseOptionalReturnAttrs(AttrBuilder &B);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000221 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
222 bool ParseOptionalLinkage(unsigned &Linkage) {
223 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
224 }
225 bool ParseOptionalVisibility(unsigned &Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000226 bool ParseOptionalDLLStorageClass(unsigned &DLLStorageClass);
Alexey Samsonov17a9cff2014-09-10 18:00:17 +0000227 bool ParseOptionalCallingConv(unsigned &CC);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000228 bool ParseOptionalAlignment(unsigned &Alignment);
Hal Finkelb0407ba2014-07-18 15:51:28 +0000229 bool ParseOptionalDereferenceableBytes(uint64_t &Bytes);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000230 bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
231 AtomicOrdering &Ordering);
Tim Northovere94a5182014-03-11 10:48:52 +0000232 bool ParseOrdering(AtomicOrdering &Ordering);
Charles Davisbe5557e2010-02-12 00:31:15 +0000233 bool ParseOptionalStackAlignment(unsigned &Alignment);
Chris Lattnerb2f39502009-12-30 05:44:30 +0000234 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
Reid Kleckner436c42e2014-01-17 23:58:17 +0000235 bool ParseOptionalCommaInAlloca(bool &IsInAlloca);
Chris Lattner28f1eeb2009-12-30 05:14:00 +0000236 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma);
237 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
238 bool AteExtraComma;
239 if (ParseIndexList(Indices, AteExtraComma)) return true;
240 if (AteExtraComma)
241 return TokError("expected index");
242 return false;
243 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000244
Chris Lattnerac161bf2009-01-02 07:01:27 +0000245 // Top-Level Entities
246 bool ParseTopLevelEntities();
247 bool ValidateEndOfModule();
248 bool ParseTargetDefinition();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000249 bool ParseModuleAsm();
Bill Wendling706d3d62012-11-28 08:41:48 +0000250 bool ParseDepLibs(); // FIXME: Remove in 4.0.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000251 bool ParseUnnamedType();
252 bool ParseNamedType();
253 bool ParseDeclare();
254 bool ParseDefine();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000255
Chris Lattnerac161bf2009-01-02 07:01:27 +0000256 bool ParseGlobalType(bool &IsConstant);
Dan Gohman466876b2009-08-12 23:32:33 +0000257 bool ParseUnnamedGlobal();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000258 bool ParseNamedGlobal();
259 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
Nico Rieck7157bb72014-01-14 15:22:47 +0000260 bool HasLinkage, unsigned Visibility,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000261 unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000262 GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
Rafael Espindola464fe022014-07-30 22:51:54 +0000263 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Linkage,
264 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000265 GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
David Majnemerdad0a642014-06-27 18:19:56 +0000266 bool parseComdat();
Devang Patel39e64d42009-07-01 19:21:12 +0000267 bool ParseStandaloneMetadata();
Devang Patelbe626972009-07-29 00:34:02 +0000268 bool ParseNamedMetadata();
Chris Lattner1797fc72009-12-29 21:53:55 +0000269 bool ParseMDString(MDString *&Result);
Chris Lattner6dac02a2009-12-30 04:15:23 +0000270 bool ParseMDNodeID(MDNode *&Result);
Chris Lattner8eff0152010-04-01 05:14:45 +0000271 bool ParseMDNodeID(MDNode *&Result, unsigned &SlotNo);
Bill Wendling63b88192013-02-06 06:52:58 +0000272 bool ParseUnnamedAttrGrp();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000273 bool ParseFnAttributeValuePairs(AttrBuilder &B,
274 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000275 bool inAttrGrp, LocTy &BuiltinLoc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000276
Chris Lattnerac161bf2009-01-02 07:01:27 +0000277 // Type Parsing.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000278 bool ParseType(Type *&Result, bool AllowVoid = false);
279 bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000280 Loc = Lex.getLoc();
Chris Lattnerf880ca22009-03-09 04:49:14 +0000281 return ParseType(Result, AllowVoid);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000282 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000283 bool ParseAnonStructType(Type *&Result, bool Packed);
284 bool ParseStructBody(SmallVectorImpl<Type*> &Body);
285 bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
286 std::pair<Type*, LocTy> &Entry,
287 Type *&ResultTy);
288
289 bool ParseArrayVectorType(Type *&Result, bool isVector);
290 bool ParseFunctionType(Type *&Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000291
Chris Lattnerac161bf2009-01-02 07:01:27 +0000292 // Function Semantic Analysis.
293 class PerFunctionState {
294 LLParser &P;
295 Function &F;
296 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
297 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
298 std::vector<Value*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000299
Chris Lattner3432c622009-10-28 03:39:23 +0000300 /// FunctionNumber - If this is an unnamed function, this is the slot
301 /// number of it, otherwise it is -1.
302 int FunctionNumber;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000303 public:
Chris Lattner3432c622009-10-28 03:39:23 +0000304 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000305 ~PerFunctionState();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000306
Chris Lattnerac161bf2009-01-02 07:01:27 +0000307 Function &getFunction() const { return F; }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000308
Chris Lattner3432c622009-10-28 03:39:23 +0000309 bool FinishFunction();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000310
Chris Lattnerac161bf2009-01-02 07:01:27 +0000311 /// GetVal - Get a value with the specified name or ID, creating a
312 /// forward reference record if needed. This can return null if the value
313 /// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000314 Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc);
315 Value *GetVal(unsigned ID, Type *Ty, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000316
Chris Lattnerac161bf2009-01-02 07:01:27 +0000317 /// SetInstName - After an instruction is parsed and inserted into its
318 /// basic block, this installs its name.
319 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
320 Instruction *Inst);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000321
Chris Lattnerac161bf2009-01-02 07:01:27 +0000322 /// GetBB - Get a basic block with the specified name or ID, creating a
323 /// forward reference record if needed. This can return null if the value
324 /// is not a BasicBlock.
325 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
326 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000327
Chris Lattnerac161bf2009-01-02 07:01:27 +0000328 /// DefineBB - Define the specified basic block, which is either named or
329 /// unnamed. If there is an error, this returns null otherwise it returns
330 /// the block being defined.
331 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000332
333 bool resolveForwardRefBlockAddresses();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000334 };
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000335
Chris Lattner229907c2011-07-18 04:54:35 +0000336 bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +0000337 PerFunctionState *PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000338
Chris Lattner229907c2011-07-18 04:54:35 +0000339 bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
340 bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000341 return ParseValue(Ty, V, &PFS);
342 }
Chris Lattner229907c2011-07-18 04:54:35 +0000343 bool ParseValue(Type *Ty, Value *&V, LocTy &Loc,
Chris Lattnerac161bf2009-01-02 07:01:27 +0000344 PerFunctionState &PFS) {
345 Loc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000346 return ParseValue(Ty, V, &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000347 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000348
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000349 bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS);
350 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
351 return ParseTypeAndValue(V, &PFS);
352 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000353 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
354 Loc = Lex.getLoc();
355 return ParseTypeAndValue(V, PFS);
356 }
Chris Lattner3ed871f2009-10-27 19:13:16 +0000357 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
358 PerFunctionState &PFS);
359 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
360 LocTy Loc;
361 return ParseTypeAndBasicBlock(BB, Loc, PFS);
362 }
Victor Hernandezfa232232009-12-03 23:40:58 +0000363
Chris Lattner392be582010-02-12 20:49:41 +0000364
Chris Lattnerac161bf2009-01-02 07:01:27 +0000365 struct ParamInfo {
366 LocTy Loc;
367 Value *V;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000368 AttributeSet Attrs;
369 ParamInfo(LocTy loc, Value *v, AttributeSet attrs)
Chris Lattnerac161bf2009-01-02 07:01:27 +0000370 : Loc(loc), V(v), Attrs(attrs) {}
371 };
372 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
Reid Kleckner83498642014-08-26 00:33:28 +0000373 PerFunctionState &PFS,
374 bool IsMustTailCall = false,
375 bool InVarArgsFunc = false);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000376
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000377 // Constant Parsing.
Craig Topperada08572014-04-16 04:21:27 +0000378 bool ParseValID(ValID &ID, PerFunctionState *PFS = nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000379 bool ParseGlobalValue(Type *Ty, Constant *&V);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000380 bool ParseGlobalTypeAndValue(Constant *&V);
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000381 bool ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts);
David Majnemerdad0a642014-06-27 18:19:56 +0000382 bool parseOptionalComdat(Comdat *&C);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000383 bool ParseMetadataAsValue(ValID &ID, PerFunctionState *PFS);
384 bool ParseMetadata(Metadata *&MD, PerFunctionState *PFS);
385 bool ParseMDNode(MDNode *&MD);
386 bool ParseMDNodeOrLocal(Metadata *&MD, PerFunctionState *PFS);
387 bool ParseMDNodeVector(SmallVectorImpl<Metadata *> &,
388 PerFunctionState *PFS);
Dan Gohman338d9a42010-08-24 02:05:17 +0000389 bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000390
Chris Lattnerac161bf2009-01-02 07:01:27 +0000391 // Function Parsing.
392 struct ArgInfo {
393 LocTy Loc;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000394 Type *Ty;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000395 AttributeSet Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000396 std::string Name;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000397 ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000398 : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000399 };
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000400 bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000401 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
402 bool ParseFunctionBody(Function &Fn);
403 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000404
Reid Kleckner5772b772014-04-24 20:14:34 +0000405 enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
406
Chris Lattner77b89dc2009-12-30 05:23:43 +0000407 // Instruction Parsing. Each instruction parsing routine can return with a
408 // normal result, an error result, or return having eaten an extra comma.
409 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
410 int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
411 PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000412 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000413
Chris Lattner33de4272011-06-17 06:49:41 +0000414 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000415 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
416 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +0000417 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000418 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +0000419 bool ParseResume(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000420
Chris Lattnereeefa9a2009-01-05 08:24:46 +0000421 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
422 unsigned OperandType);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000423 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
424 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
425 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
426 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +0000427 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000428 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
429 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
430 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000431 int ParsePHI(Instruction *&I, PerFunctionState &PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +0000432 bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +0000433 bool ParseCall(Instruction *&I, PerFunctionState &PFS,
434 CallInst::TailCallKind IsTail);
Chris Lattner78103722011-06-17 03:16:47 +0000435 int ParseAlloc(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +0000436 int ParseLoad(Instruction *&I, PerFunctionState &PFS);
437 int ParseStore(Instruction *&I, PerFunctionState &PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +0000438 int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS);
439 int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000440 int ParseFence(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000441 int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
442 int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
443 int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000444
445 // Use-list order directives.
446 bool ParseUseListOrder(PerFunctionState *PFS = nullptr);
447 bool ParseUseListOrderBB();
448 bool ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes);
449 bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000450 };
451} // End llvm namespace
452
453#endif