blob: 8b7e9560a69f478782044102b6b97ea948400cc5 [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;
Alex Lorenz8955f7d2015-06-23 17:10:10 +000040 struct SlotMapping;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000041 class StructType;
Misha Brukman1d9a93d2009-01-02 22:46:48 +000042
Chris Lattner3432c622009-10-28 03:39:23 +000043 /// ValID - Represents a reference of a definition of some sort with no type.
44 /// There are several cases where we have to parse the value but where the
45 /// type can depend on later context. This may either be a numeric reference
46 /// or a symbolic (%var) reference. This is just a discriminated union.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000047 struct ValID {
Chris Lattner3432c622009-10-28 03:39:23 +000048 enum {
49 t_LocalID, t_GlobalID, // ID in UIntVal.
50 t_LocalName, t_GlobalName, // Name in StrVal.
51 t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal.
52 t_Null, t_Undef, t_Zero, // No value.
53 t_EmptyArray, // No value: []
54 t_Constant, // Value in ConstantVal.
David Blaikie41ba2b42015-07-27 23:32:19 +000055 t_InlineAsm, // Value in FTy/StrVal/StrVal2/UIntVal.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000056 t_ConstantStruct, // Value in ConstantStructElts.
57 t_PackedConstantStruct // Value in ConstantStructElts.
David Blaikieadbda4b2015-08-03 20:08:41 +000058 } Kind = t_LocalID;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000059
Chris Lattner3432c622009-10-28 03:39:23 +000060 LLLexer::LocTy Loc;
61 unsigned UIntVal;
David Blaikie41ba2b42015-07-27 23:32:19 +000062 FunctionType *FTy;
Chris Lattner3432c622009-10-28 03:39:23 +000063 std::string StrVal, StrVal2;
64 APSInt APSIntVal;
David Blaikieadbda4b2015-08-03 20:08:41 +000065 APFloat APFloatVal{0.0};
Chris Lattner3432c622009-10-28 03:39:23 +000066 Constant *ConstantVal;
David Blaikieadbda4b2015-08-03 20:08:41 +000067 std::unique_ptr<Constant *[]> ConstantStructElts;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000068
David Blaikieadbda4b2015-08-03 20:08:41 +000069 ValID() = default;
David Blaikie69374412015-08-03 20:30:53 +000070 ValID(const ValID &RHS)
David Blaikieadbda4b2015-08-03 20:08:41 +000071 : Kind(RHS.Kind), Loc(RHS.Loc), UIntVal(RHS.UIntVal), FTy(RHS.FTy),
David Blaikie69374412015-08-03 20:30:53 +000072 StrVal(RHS.StrVal), StrVal2(RHS.StrVal2), APSIntVal(RHS.APSIntVal),
73 APFloatVal(RHS.APFloatVal), ConstantVal(RHS.ConstantVal) {
74 assert(!RHS.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;
Alex Lorenz8955f7d2015-06-23 17:10:10 +000094 SlotMapping *Slots;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000095
Chris Lattner8eff0152010-04-01 05:14:45 +000096 // Instruction metadata resolution. Each instruction can have a list of
97 // MDRef info associated with them.
Dan Gohman7c7f13a2010-08-24 14:31:06 +000098 //
99 // The simpler approach of just creating temporary MDNodes and then calling
100 // RAUW on them when the definition is processed doesn't work because some
101 // instruction metadata kinds, such as dbg, get stored in the IR in an
102 // "optimized" format which doesn't participate in the normal value use
103 // lists. This means that RAUW doesn't work, even on temporary MDNodes
104 // which otherwise support RAUW. Instead, we defer resolving MDNode
105 // references until the definitions have been processed.
Chris Lattner8eff0152010-04-01 05:14:45 +0000106 struct MDRef {
107 SMLoc Loc;
108 unsigned MDKind, MDSlot;
109 };
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000110
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000111 /// Indicates which operator an operand allows (for the few operands that
112 /// may only reference a certain operator).
113 enum OperatorConstraint {
114 OC_None = 0, // No constraint
115 OC_CatchPad, // Must be CatchPadInst
116 OC_CleanupPad // Must be CleanupPadInst
117 };
118
Manman Ren209b17c2013-09-28 00:22:27 +0000119 SmallVector<Instruction*, 64> InstsWithTBAATag;
120
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000121 // Type resolution handling data structures. The location is set when we
122 // have processed a use of the type but not a definition yet.
123 StringMap<std::pair<Type*, LocTy> > NamedTypes;
David Majnemer19b51052015-02-11 07:43:56 +0000124 std::map<unsigned, std::pair<Type*, LocTy> > NumberedTypes;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000125
David Majnemer19b51052015-02-11 07:43:56 +0000126 std::map<unsigned, TrackingMDNodeRef> NumberedMetadata;
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000127 std::map<unsigned, std::pair<TempMDTuple, LocTy>> ForwardRefMDNodes;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000128
129 // Global Value reference information.
130 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
131 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
132 std::vector<GlobalValue*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000133
David Majnemerdad0a642014-06-27 18:19:56 +0000134 // Comdat forward reference information.
135 std::map<std::string, LocTy> ForwardRefComdats;
136
Chris Lattner3432c622009-10-28 03:39:23 +0000137 // References to blockaddress. The key is the function ValID, the value is
138 // a list of references to blocks in that function.
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000139 std::map<ValID, std::map<ValID, GlobalValue *>> ForwardRefBlockAddresses;
140 class PerFunctionState;
141 /// Reference to per-function state to allow basic blocks to be
142 /// forward-referenced by blockaddress instructions within the same
143 /// function.
144 PerFunctionState *BlockAddressPFS;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000145
Bill Wendling63b88192013-02-06 06:52:58 +0000146 // Attribute builder reference information.
Bill Wendlingb32b0412013-02-08 06:32:06 +0000147 std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups;
148 std::map<unsigned, AttrBuilder> NumberedAttrBuilders;
Bill Wendling63b88192013-02-06 06:52:58 +0000149
Chris Lattnerac161bf2009-01-02 07:01:27 +0000150 public:
Alex Lorenz8955f7d2015-06-23 17:10:10 +0000151 LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *M,
152 SlotMapping *Slots = nullptr)
153 : Context(M->getContext()), Lex(F, SM, Err, M->getContext()), M(M),
154 Slots(Slots), BlockAddressPFS(nullptr) {}
Chris Lattnerad6f3352009-01-04 20:44:11 +0000155 bool Run();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000156
Alex Lorenz1de2acd2015-08-21 21:32:39 +0000157 bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots);
Alex Lorenzd2255952015-07-17 22:07:03 +0000158
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000159 LLVMContext &getContext() { return Context; }
Owen Anderson09063ce2009-07-02 17:04:01 +0000160
Chris Lattnerac161bf2009-01-02 07:01:27 +0000161 private:
162
Benjamin Kramerc7583112010-09-27 17:42:11 +0000163 bool Error(LocTy L, const Twine &Msg) const {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000164 return Lex.Error(L, Msg);
165 }
Benjamin Kramerc7583112010-09-27 17:42:11 +0000166 bool TokError(const Twine &Msg) const {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000167 return Error(Lex.getLoc(), Msg);
168 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000169
Alex Lorenz1de2acd2015-08-21 21:32:39 +0000170 /// Restore the internal name and slot mappings using the mappings that
171 /// were created at an earlier parsing stage.
172 void restoreParsingState(const SlotMapping *Slots);
173
Chris Lattnerac161bf2009-01-02 07:01:27 +0000174 /// GetGlobalVal - Get a value with the specified name or ID, creating a
175 /// forward reference record if needed. This can return null if the value
176 /// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000177 GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc);
178 GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000179
David Majnemerdad0a642014-06-27 18:19:56 +0000180 /// Get a Comdat with the specified name, creating a forward reference
181 /// record if needed.
182 Comdat *getComdat(const std::string &N, LocTy Loc);
183
Chris Lattnerac161bf2009-01-02 07:01:27 +0000184 // Helper Routines.
185 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3822f632009-01-02 08:05:26 +0000186 bool EatIfPresent(lltok::Kind T) {
187 if (Lex.getKind() != T) return false;
188 Lex.Lex();
189 return true;
190 }
Michael Ilseman92053172012-11-27 00:42:44 +0000191
192 FastMathFlags EatFastMathFlagsIfPresent() {
193 FastMathFlags FMF;
194 while (true)
195 switch (Lex.getKind()) {
Michael Ilseman65f14352012-12-09 21:12:04 +0000196 case lltok::kw_fast: FMF.setUnsafeAlgebra(); Lex.Lex(); continue;
197 case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue;
198 case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue;
199 case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue;
200 case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue;
Michael Ilseman92053172012-11-27 00:42:44 +0000201 default: return FMF;
202 }
203 return FMF;
204 }
205
Craig Topperada08572014-04-16 04:21:27 +0000206 bool ParseOptionalToken(lltok::Kind T, bool &Present,
207 LocTy *Loc = nullptr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000208 if (Lex.getKind() != T) {
209 Present = false;
210 } else {
Rafael Espindola026d1522011-01-13 01:30:30 +0000211 if (Loc)
212 *Loc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000213 Lex.Lex();
214 Present = true;
215 }
216 return false;
217 }
Chris Lattner3822f632009-01-02 08:05:26 +0000218 bool ParseStringConstant(std::string &Result);
219 bool ParseUInt32(unsigned &Val);
220 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000221 Loc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000222 return ParseUInt32(Val);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000223 }
Hal Finkelb0407ba2014-07-18 15:51:28 +0000224 bool ParseUInt64(uint64_t &Val);
225 bool ParseUInt64(uint64_t &Val, LocTy &Loc) {
226 Loc = Lex.getLoc();
227 return ParseUInt64(Val);
228 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000229
Artur Pilipenko17376c42015-08-03 14:31:49 +0000230 bool ParseStringAttribute(AttrBuilder &B);
231
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000232 bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
233 bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000234 bool parseOptionalUnnamedAddr(bool &UnnamedAddr) {
235 return ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr);
236 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000237 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
Bill Wendling34c2eb22012-12-04 23:40:58 +0000238 bool ParseOptionalParamAttrs(AttrBuilder &B);
239 bool ParseOptionalReturnAttrs(AttrBuilder &B);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000240 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
241 bool ParseOptionalLinkage(unsigned &Linkage) {
242 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
243 }
244 bool ParseOptionalVisibility(unsigned &Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000245 bool ParseOptionalDLLStorageClass(unsigned &DLLStorageClass);
Alexey Samsonov17a9cff2014-09-10 18:00:17 +0000246 bool ParseOptionalCallingConv(unsigned &CC);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000247 bool ParseOptionalAlignment(unsigned &Alignment);
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000248 bool ParseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000249 bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
250 AtomicOrdering &Ordering);
Tim Northovere94a5182014-03-11 10:48:52 +0000251 bool ParseOrdering(AtomicOrdering &Ordering);
Charles Davisbe5557e2010-02-12 00:31:15 +0000252 bool ParseOptionalStackAlignment(unsigned &Alignment);
Chris Lattnerb2f39502009-12-30 05:44:30 +0000253 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
Reid Kleckner436c42e2014-01-17 23:58:17 +0000254 bool ParseOptionalCommaInAlloca(bool &IsInAlloca);
Chris Lattner28f1eeb2009-12-30 05:14:00 +0000255 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma);
256 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
257 bool AteExtraComma;
258 if (ParseIndexList(Indices, AteExtraComma)) return true;
259 if (AteExtraComma)
260 return TokError("expected index");
261 return false;
262 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000263
Chris Lattnerac161bf2009-01-02 07:01:27 +0000264 // Top-Level Entities
265 bool ParseTopLevelEntities();
266 bool ValidateEndOfModule();
267 bool ParseTargetDefinition();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000268 bool ParseModuleAsm();
Bill Wendling706d3d62012-11-28 08:41:48 +0000269 bool ParseDepLibs(); // FIXME: Remove in 4.0.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000270 bool ParseUnnamedType();
271 bool ParseNamedType();
272 bool ParseDeclare();
273 bool ParseDefine();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000274
Chris Lattnerac161bf2009-01-02 07:01:27 +0000275 bool ParseGlobalType(bool &IsConstant);
Dan Gohman466876b2009-08-12 23:32:33 +0000276 bool ParseUnnamedGlobal();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000277 bool ParseNamedGlobal();
278 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
Nico Rieck7157bb72014-01-14 15:22:47 +0000279 bool HasLinkage, unsigned Visibility,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000280 unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000281 GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
Rafael Espindola464fe022014-07-30 22:51:54 +0000282 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Linkage,
283 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000284 GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
David Majnemerdad0a642014-06-27 18:19:56 +0000285 bool parseComdat();
Devang Patel39e64d42009-07-01 19:21:12 +0000286 bool ParseStandaloneMetadata();
Devang Patelbe626972009-07-29 00:34:02 +0000287 bool ParseNamedMetadata();
Chris Lattner1797fc72009-12-29 21:53:55 +0000288 bool ParseMDString(MDString *&Result);
Chris Lattner6dac02a2009-12-30 04:15:23 +0000289 bool ParseMDNodeID(MDNode *&Result);
Bill Wendling63b88192013-02-06 06:52:58 +0000290 bool ParseUnnamedAttrGrp();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000291 bool ParseFnAttributeValuePairs(AttrBuilder &B,
292 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000293 bool inAttrGrp, LocTy &BuiltinLoc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000294
Chris Lattnerac161bf2009-01-02 07:01:27 +0000295 // Type Parsing.
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000296 bool ParseType(Type *&Result, const Twine &Msg, bool AllowVoid = false);
297 bool ParseType(Type *&Result, bool AllowVoid = false) {
298 return ParseType(Result, "expected type", AllowVoid);
299 }
300 bool ParseType(Type *&Result, const Twine &Msg, LocTy &Loc,
301 bool AllowVoid = false) {
302 Loc = Lex.getLoc();
303 return ParseType(Result, Msg, AllowVoid);
304 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000305 bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000306 Loc = Lex.getLoc();
Chris Lattnerf880ca22009-03-09 04:49:14 +0000307 return ParseType(Result, AllowVoid);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000308 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000309 bool ParseAnonStructType(Type *&Result, bool Packed);
310 bool ParseStructBody(SmallVectorImpl<Type*> &Body);
311 bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
312 std::pair<Type*, LocTy> &Entry,
313 Type *&ResultTy);
314
315 bool ParseArrayVectorType(Type *&Result, bool isVector);
316 bool ParseFunctionType(Type *&Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000317
Chris Lattnerac161bf2009-01-02 07:01:27 +0000318 // Function Semantic Analysis.
319 class PerFunctionState {
320 LLParser &P;
321 Function &F;
322 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
323 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
324 std::vector<Value*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000325
Chris Lattner3432c622009-10-28 03:39:23 +0000326 /// FunctionNumber - If this is an unnamed function, this is the slot
327 /// number of it, otherwise it is -1.
328 int FunctionNumber;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000329 public:
Chris Lattner3432c622009-10-28 03:39:23 +0000330 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000331 ~PerFunctionState();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000332
Chris Lattnerac161bf2009-01-02 07:01:27 +0000333 Function &getFunction() const { return F; }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000334
Chris Lattner3432c622009-10-28 03:39:23 +0000335 bool FinishFunction();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000336
Chris Lattnerac161bf2009-01-02 07:01:27 +0000337 /// GetVal - Get a value with the specified name or ID, creating a
338 /// forward reference record if needed. This can return null if the value
339 /// exists but does not have the right type.
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000340 Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc,
341 OperatorConstraint OC = OC_None);
342 Value *GetVal(unsigned ID, Type *Ty, LocTy Loc,
343 OperatorConstraint OC = OC_None);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000344
Chris Lattnerac161bf2009-01-02 07:01:27 +0000345 /// SetInstName - After an instruction is parsed and inserted into its
346 /// basic block, this installs its name.
347 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
348 Instruction *Inst);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000349
Chris Lattnerac161bf2009-01-02 07:01:27 +0000350 /// GetBB - Get a basic block with the specified name or ID, creating a
351 /// forward reference record if needed. This can return null if the value
352 /// is not a BasicBlock.
353 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
354 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000355
Chris Lattnerac161bf2009-01-02 07:01:27 +0000356 /// DefineBB - Define the specified basic block, which is either named or
357 /// unnamed. If there is an error, this returns null otherwise it returns
358 /// the block being defined.
359 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000360
361 bool resolveForwardRefBlockAddresses();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000362 };
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000363
Chris Lattner229907c2011-07-18 04:54:35 +0000364 bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000365 PerFunctionState *PFS,
366 OperatorConstraint OC = OC_None);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000367
Alex Lorenzd2255952015-07-17 22:07:03 +0000368 bool parseConstantValue(Type *Ty, Constant *&C);
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000369 bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS,
370 OperatorConstraint OC = OC_None);
371 bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS,
372 OperatorConstraint OC = OC_None) {
373 return ParseValue(Ty, V, &PFS, OC);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000374 }
Chris Lattner229907c2011-07-18 04:54:35 +0000375 bool ParseValue(Type *Ty, Value *&V, LocTy &Loc,
Chris Lattnerac161bf2009-01-02 07:01:27 +0000376 PerFunctionState &PFS) {
377 Loc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000378 return ParseValue(Ty, V, &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000379 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000380
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000381 bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS);
382 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
383 return ParseTypeAndValue(V, &PFS);
384 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000385 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
386 Loc = Lex.getLoc();
387 return ParseTypeAndValue(V, PFS);
388 }
Chris Lattner3ed871f2009-10-27 19:13:16 +0000389 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
390 PerFunctionState &PFS);
391 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
392 LocTy Loc;
393 return ParseTypeAndBasicBlock(BB, Loc, PFS);
394 }
Victor Hernandezfa232232009-12-03 23:40:58 +0000395
Chris Lattner392be582010-02-12 20:49:41 +0000396
Chris Lattnerac161bf2009-01-02 07:01:27 +0000397 struct ParamInfo {
398 LocTy Loc;
399 Value *V;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000400 AttributeSet Attrs;
401 ParamInfo(LocTy loc, Value *v, AttributeSet attrs)
Chris Lattnerac161bf2009-01-02 07:01:27 +0000402 : Loc(loc), V(v), Attrs(attrs) {}
403 };
404 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
Reid Kleckner83498642014-08-26 00:33:28 +0000405 PerFunctionState &PFS,
406 bool IsMustTailCall = false,
407 bool InVarArgsFunc = false);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000408
David Majnemer654e1302015-07-31 17:58:14 +0000409 bool ParseExceptionArgs(SmallVectorImpl<Value *> &Args,
410 PerFunctionState &PFS);
411
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000412 // Constant Parsing.
Craig Topperada08572014-04-16 04:21:27 +0000413 bool ParseValID(ValID &ID, PerFunctionState *PFS = nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000414 bool ParseGlobalValue(Type *Ty, Constant *&V);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000415 bool ParseGlobalTypeAndValue(Constant *&V);
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000416 bool ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts);
Rafael Espindola83a362c2015-01-06 22:55:16 +0000417 bool parseOptionalComdat(StringRef GlobalName, Comdat *&C);
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000418 bool ParseMetadataAsValue(Value *&V, PerFunctionState &PFS);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +0000419 bool ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
420 PerFunctionState *PFS);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000421 bool ParseMetadata(Metadata *&MD, PerFunctionState *PFS);
Duncan P. N. Exon Smith58ef9d12015-01-12 21:23:11 +0000422 bool ParseMDTuple(MDNode *&MD, bool IsDistinct = false);
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +0000423 bool ParseMDNode(MDNode *&MD);
424 bool ParseMDNodeTail(MDNode *&MD);
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000425 bool ParseMDNodeVector(SmallVectorImpl<Metadata *> &MDs);
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +0000426 bool ParseMetadataAttachment(unsigned &Kind, MDNode *&MD);
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +0000427 bool ParseInstructionMetadata(Instruction &Inst);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +0000428 bool ParseOptionalFunctionMetadata(Function &F);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000429
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +0000430 template <class FieldTy>
431 bool ParseMDField(LocTy Loc, StringRef Name, FieldTy &Result);
Duncan P. N. Exon Smitha7477282015-01-20 02:42:29 +0000432 template <class FieldTy> bool ParseMDField(StringRef Name, FieldTy &Result);
Duncan P. N. Exon Smith13890af2015-01-19 23:32:36 +0000433 template <class ParserTy>
Duncan P. N. Exon Smith66ca92e2015-01-19 23:39:32 +0000434 bool ParseMDFieldsImplBody(ParserTy parseField);
435 template <class ParserTy>
Duncan P. N. Exon Smith13890af2015-01-19 23:32:36 +0000436 bool ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +0000437 bool ParseSpecializedMDNode(MDNode *&N, bool IsDistinct = false);
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +0000438
439#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
440 bool Parse##CLASS(MDNode *&Result, bool IsDistinct);
441#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +0000442
Chris Lattnerac161bf2009-01-02 07:01:27 +0000443 // Function Parsing.
444 struct ArgInfo {
445 LocTy Loc;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000446 Type *Ty;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000447 AttributeSet Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000448 std::string Name;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000449 ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000450 : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000451 };
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000452 bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000453 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
454 bool ParseFunctionBody(Function &Fn);
455 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000456
Reid Kleckner5772b772014-04-24 20:14:34 +0000457 enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
458
Chris Lattner77b89dc2009-12-30 05:23:43 +0000459 // Instruction Parsing. Each instruction parsing routine can return with a
460 // normal result, an error result, or return having eaten an extra comma.
461 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
462 int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
463 PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000464 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000465
Chris Lattner33de4272011-06-17 06:49:41 +0000466 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000467 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
468 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +0000469 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000470 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +0000471 bool ParseResume(Instruction *&Inst, PerFunctionState &PFS);
David Majnemer654e1302015-07-31 17:58:14 +0000472 bool ParseCleanupRet(Instruction *&Inst, PerFunctionState &PFS);
473 bool ParseCatchRet(Instruction *&Inst, PerFunctionState &PFS);
474 bool ParseCatchPad(Instruction *&Inst, PerFunctionState &PFS);
475 bool ParseTerminatePad(Instruction *&Inst, PerFunctionState &PFS);
476 bool ParseCleanupPad(Instruction *&Inst, PerFunctionState &PFS);
477 bool ParseCatchEndPad(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000478
Chris Lattnereeefa9a2009-01-05 08:24:46 +0000479 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
480 unsigned OperandType);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000481 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
482 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
483 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
484 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +0000485 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000486 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
487 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
488 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000489 int ParsePHI(Instruction *&I, PerFunctionState &PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +0000490 bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +0000491 bool ParseCall(Instruction *&I, PerFunctionState &PFS,
492 CallInst::TailCallKind IsTail);
Chris Lattner78103722011-06-17 03:16:47 +0000493 int ParseAlloc(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +0000494 int ParseLoad(Instruction *&I, PerFunctionState &PFS);
495 int ParseStore(Instruction *&I, PerFunctionState &PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +0000496 int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS);
497 int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000498 int ParseFence(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000499 int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
500 int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
501 int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000502
503 // Use-list order directives.
504 bool ParseUseListOrder(PerFunctionState *PFS = nullptr);
505 bool ParseUseListOrderBB();
506 bool ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes);
507 bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000508 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000509} // End llvm namespace
Chris Lattnerac161bf2009-01-02 07:01:27 +0000510
511#endif