blob: 0b6740dba3beda541c0a061a78f1efdd9279362f [file] [log] [blame]
Chris Lattnerac161bf2009-01-02 07:01:27 +00001//===-- LLParser.cpp - Parser Class ---------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the parser class for .ll files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "LLParser.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth91065212014-03-05 10:34:14 +000016#include "llvm/IR/AutoUpgrade.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/CallingConv.h"
18#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +000019#include "llvm/IR/DebugInfo.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000020#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/InlineAsm.h"
23#include "llvm/IR/Instructions.h"
Manman Ren209b17c2013-09-28 00:22:27 +000024#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Module.h"
26#include "llvm/IR/Operator.h"
27#include "llvm/IR/ValueSymbolTable.h"
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +000028#include "llvm/Support/Dwarf.h"
Torok Edwin56d06592009-07-11 20:10:48 +000029#include "llvm/Support/ErrorHandling.h"
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +000030#include "llvm/Support/SaveAndRestore.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000031#include "llvm/Support/raw_ostream.h"
32using namespace llvm;
33
Chris Lattner229907c2011-07-18 04:54:35 +000034static std::string getTypeString(Type *T) {
Alp Tokere69170a2014-06-26 22:52:05 +000035 std::string Result;
36 raw_string_ostream Tmp(Result);
37 Tmp << *T;
38 return Tmp.str();
Chris Lattner0f214eb2011-06-18 21:18:23 +000039}
40
Chris Lattner3822f632009-01-02 08:05:26 +000041/// Run: module ::= toplevelentity*
Chris Lattnerad6f3352009-01-04 20:44:11 +000042bool LLParser::Run() {
Chris Lattner3822f632009-01-02 08:05:26 +000043 // Prime the lexer.
44 Lex.Lex();
45
Chris Lattnerad6f3352009-01-04 20:44:11 +000046 return ParseTopLevelEntities() ||
47 ValidateEndOfModule();
Chris Lattnerac161bf2009-01-02 07:01:27 +000048}
49
50/// ValidateEndOfModule - Do final validity and sanity checks at the end of the
51/// module.
52bool LLParser::ValidateEndOfModule() {
Manman Ren209b17c2013-09-28 00:22:27 +000053 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
54 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
55
Bill Wendlingb32b0412013-02-08 06:32:06 +000056 // Handle any function attribute group forward references.
57 for (std::map<Value*, std::vector<unsigned> >::iterator
58 I = ForwardRefAttrGroups.begin(), E = ForwardRefAttrGroups.end();
59 I != E; ++I) {
60 Value *V = I->first;
61 std::vector<unsigned> &Vec = I->second;
62 AttrBuilder B;
63
64 for (std::vector<unsigned>::iterator VI = Vec.begin(), VE = Vec.end();
65 VI != VE; ++VI)
66 B.merge(NumberedAttrBuilders[*VI]);
67
68 if (Function *Fn = dyn_cast<Function>(V)) {
69 AttributeSet AS = Fn->getAttributes();
70 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
71 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
72 AS.getFnAttributes());
73
74 FnAttrs.merge(B);
75
76 // If the alignment was parsed as an attribute, move to the alignment
77 // field.
78 if (FnAttrs.hasAlignmentAttr()) {
79 Fn->setAlignment(FnAttrs.getAlignment());
80 FnAttrs.removeAttribute(Attribute::Alignment);
81 }
82
83 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
84 AttributeSet::get(Context,
85 AttributeSet::FunctionIndex,
86 FnAttrs));
87 Fn->setAttributes(AS);
88 } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
89 AttributeSet AS = CI->getAttributes();
90 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
91 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
92 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +000093 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +000094 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
95 AttributeSet::get(Context,
96 AttributeSet::FunctionIndex,
97 FnAttrs));
98 CI->setAttributes(AS);
99 } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
100 AttributeSet AS = II->getAttributes();
101 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
102 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
103 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +0000104 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000105 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
106 AttributeSet::get(Context,
107 AttributeSet::FunctionIndex,
108 FnAttrs));
109 II->setAttributes(AS);
110 } else {
111 llvm_unreachable("invalid object with forward attribute group reference");
112 }
113 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000114
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000115 // If there are entries in ForwardRefBlockAddresses at this point, the
116 // function was never defined.
117 if (!ForwardRefBlockAddresses.empty())
118 return Error(ForwardRefBlockAddresses.begin()->first.Loc,
119 "expected function name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000120
David Majnemer19b51052015-02-11 07:43:56 +0000121 for (const auto &NT : NumberedTypes)
122 if (NT.second.second.isValid())
123 return Error(NT.second.second,
124 "use of undefined type '%" + Twine(NT.first) + "'");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000125
126 for (StringMap<std::pair<Type*, LocTy> >::iterator I =
127 NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I)
128 if (I->second.second.isValid())
129 return Error(I->second.second,
130 "use of undefined type named '" + I->getKey() + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000131
David Majnemerdad0a642014-06-27 18:19:56 +0000132 if (!ForwardRefComdats.empty())
133 return Error(ForwardRefComdats.begin()->second,
134 "use of undefined comdat '$" +
135 ForwardRefComdats.begin()->first + "'");
136
Chris Lattnerac161bf2009-01-02 07:01:27 +0000137 if (!ForwardRefVals.empty())
138 return Error(ForwardRefVals.begin()->second.second,
139 "use of undefined value '@" + ForwardRefVals.begin()->first +
140 "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000141
Chris Lattnerac161bf2009-01-02 07:01:27 +0000142 if (!ForwardRefValIDs.empty())
143 return Error(ForwardRefValIDs.begin()->second.second,
144 "use of undefined value '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000145 Twine(ForwardRefValIDs.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000146
Devang Pateld2541152009-07-08 19:23:54 +0000147 if (!ForwardRefMDNodes.empty())
148 return Error(ForwardRefMDNodes.begin()->second.second,
149 "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000150 Twine(ForwardRefMDNodes.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000151
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000152 // Resolve metadata cycles.
David Majnemer19b51052015-02-11 07:43:56 +0000153 for (auto &N : NumberedMetadata) {
154 if (N.second && !N.second->isResolved())
155 N.second->resolveCycles();
156 }
Devang Pateld2541152009-07-08 19:23:54 +0000157
Chris Lattnerac161bf2009-01-02 07:01:27 +0000158 // Look for intrinsic functions and CallInst that need to be upgraded
159 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
160 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000161
Manman Ren8b4306c2013-12-02 21:29:56 +0000162 UpgradeDebugInfo(*M);
163
Chris Lattnerac161bf2009-01-02 07:01:27 +0000164 return false;
165}
166
167//===----------------------------------------------------------------------===//
168// Top-Level Entities
169//===----------------------------------------------------------------------===//
170
171bool LLParser::ParseTopLevelEntities() {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000172 while (1) {
173 switch (Lex.getKind()) {
174 default: return TokError("expected top-level entity");
175 case lltok::Eof: return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000176 case lltok::kw_declare: if (ParseDeclare()) return true; break;
177 case lltok::kw_define: if (ParseDefine()) return true; break;
178 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
179 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
Bill Wendling706d3d62012-11-28 08:41:48 +0000180 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000181 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000182 case lltok::LocalVar: if (ParseNamedType()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000183 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000184 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
David Majnemerdad0a642014-06-27 18:19:56 +0000185 case lltok::ComdatVar: if (parseComdat()) return true; break;
Chris Lattner1eed2d62009-12-30 04:56:59 +0000186 case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break;
Bill Wendling63b88192013-02-06 06:52:58 +0000187 case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000188
189 // The Global variable production with no name can have many different
190 // optional leading prefixes, the production is:
Nico Rieck7157bb72014-01-14 15:22:47 +0000191 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Eric Christopher536f0a92015-05-28 23:07:39 +0000192 // OptionalThreadLocal OptionalAddrSpace OptionalUnnamedAddr
Rafael Espindola45e6c192011-01-08 16:42:36 +0000193 // ('constant'|'global') ...
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000194 case lltok::kw_private: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000195 case lltok::kw_internal: // OptionalLinkage
196 case lltok::kw_weak: // OptionalLinkage
197 case lltok::kw_weak_odr: // OptionalLinkage
198 case lltok::kw_linkonce: // OptionalLinkage
199 case lltok::kw_linkonce_odr: // OptionalLinkage
200 case lltok::kw_appending: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000201 case lltok::kw_common: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000202 case lltok::kw_extern_weak: // OptionalLinkage
Rafael Espindola52b74422014-06-03 20:00:20 +0000203 case lltok::kw_external: // OptionalLinkage
204 case lltok::kw_default: // OptionalVisibility
205 case lltok::kw_hidden: // OptionalVisibility
206 case lltok::kw_protected: // OptionalVisibility
Rafael Espindola63e92fb2014-06-03 20:07:32 +0000207 case lltok::kw_dllimport: // OptionalDLLStorageClass
208 case lltok::kw_dllexport: // OptionalDLLStorageClass
Rafael Espindola52b74422014-06-03 20:00:20 +0000209 case lltok::kw_thread_local: // OptionalThreadLocal
210 case lltok::kw_addrspace: // OptionalAddrSpace
211 case lltok::kw_constant: // GlobalType
212 case lltok::kw_global: { // GlobalType
Nico Rieck7157bb72014-01-14 15:22:47 +0000213 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000214 bool UnnamedAddr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000215 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola52b74422014-06-03 20:00:20 +0000216 bool HasLinkage;
217 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000218 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000219 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000220 ParseOptionalThreadLocal(TLM) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000221 parseOptionalUnnamedAddr(UnnamedAddr) ||
Rafael Espindola52b74422014-06-03 20:00:20 +0000222 ParseGlobal("", SMLoc(), Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000223 DLLStorageClass, TLM, UnnamedAddr))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000224 return true;
225 break;
226 }
Bill Wendlinga7c38772013-02-09 15:48:49 +0000227
228 case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break;
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000229 case lltok::kw_uselistorder: if (ParseUseListOrder()) return true; break;
230 case lltok::kw_uselistorder_bb:
231 if (ParseUseListOrderBB()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000232 }
233 }
234}
235
236
237/// toplevelentity
238/// ::= 'module' 'asm' STRINGCONSTANT
239bool LLParser::ParseModuleAsm() {
240 assert(Lex.getKind() == lltok::kw_module);
241 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000242
243 std::string AsmStr;
Chris Lattner3822f632009-01-02 08:05:26 +0000244 if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
245 ParseStringConstant(AsmStr)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000246
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000247 M->appendModuleInlineAsm(AsmStr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000248 return false;
249}
250
251/// toplevelentity
252/// ::= 'target' 'triple' '=' STRINGCONSTANT
253/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
254bool LLParser::ParseTargetDefinition() {
255 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3822f632009-01-02 08:05:26 +0000256 std::string Str;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000257 switch (Lex.Lex()) {
258 default: return TokError("unknown target property");
259 case lltok::kw_triple:
260 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000261 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
262 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000263 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000264 M->setTargetTriple(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000265 return false;
266 case lltok::kw_datalayout:
267 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000268 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
269 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000270 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000271 M->setDataLayout(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000272 return false;
273 }
274}
275
Bill Wendling706d3d62012-11-28 08:41:48 +0000276/// toplevelentity
277/// ::= 'deplibs' '=' '[' ']'
278/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
279/// FIXME: Remove in 4.0. Currently parse, but ignore.
280bool LLParser::ParseDepLibs() {
281 assert(Lex.getKind() == lltok::kw_deplibs);
282 Lex.Lex();
283 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
284 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
285 return true;
286
287 if (EatIfPresent(lltok::rsquare))
288 return false;
289
290 do {
291 std::string Str;
292 if (ParseStringConstant(Str)) return true;
293 } while (EatIfPresent(lltok::comma));
294
295 return ParseToken(lltok::rsquare, "expected ']' at end of list");
296}
297
Dan Gohman466876b2009-08-12 23:32:33 +0000298/// ParseUnnamedType:
Dan Gohman466876b2009-08-12 23:32:33 +0000299/// ::= LocalVarID '=' 'type' type
Chris Lattnerac161bf2009-01-02 07:01:27 +0000300bool LLParser::ParseUnnamedType() {
Chris Lattner07037362011-06-18 23:51:31 +0000301 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000302 unsigned TypeID = Lex.getUIntVal();
Chris Lattner8936d2b2011-06-19 00:03:46 +0000303 Lex.Lex(); // eat LocalVarID;
304
305 if (ParseToken(lltok::equal, "expected '=' after name") ||
306 ParseToken(lltok::kw_type, "expected 'type' after '='"))
307 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000308
Craig Topper2617dcc2014-04-15 06:32:26 +0000309 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000310 if (ParseStructDefinition(TypeLoc, "",
311 NumberedTypes[TypeID], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000312
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000313 if (!isa<StructType>(Result)) {
314 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
315 if (Entry.first)
316 return Error(TypeLoc, "non-struct types may not be recursive");
317 Entry.first = Result;
318 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000319 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000320
Chris Lattnerac161bf2009-01-02 07:01:27 +0000321 return false;
322}
323
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000324
Chris Lattnerac161bf2009-01-02 07:01:27 +0000325/// toplevelentity
326/// ::= LocalVar '=' 'type' type
327bool LLParser::ParseNamedType() {
328 std::string Name = Lex.getStrVal();
329 LocTy NameLoc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000330 Lex.Lex(); // eat LocalVar.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000331
Chris Lattner3822f632009-01-02 08:05:26 +0000332 if (ParseToken(lltok::equal, "expected '=' after name") ||
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000333 ParseToken(lltok::kw_type, "expected 'type' after name"))
Chris Lattner3822f632009-01-02 08:05:26 +0000334 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000335
Craig Topper2617dcc2014-04-15 06:32:26 +0000336 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000337 if (ParseStructDefinition(NameLoc, Name,
338 NamedTypes[Name], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000339
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000340 if (!isa<StructType>(Result)) {
341 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
342 if (Entry.first)
343 return Error(NameLoc, "non-struct types may not be recursive");
344 Entry.first = Result;
345 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000346 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000347
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000348 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000349}
350
351
352/// toplevelentity
353/// ::= 'declare' FunctionHeader
354bool LLParser::ParseDeclare() {
355 assert(Lex.getKind() == lltok::kw_declare);
356 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000357
Chris Lattnerac161bf2009-01-02 07:01:27 +0000358 Function *F;
359 return ParseFunctionHeader(F, false);
360}
361
362/// toplevelentity
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +0000363/// ::= 'define' FunctionHeader (!dbg !56)* '{' ...
Chris Lattnerac161bf2009-01-02 07:01:27 +0000364bool LLParser::ParseDefine() {
365 assert(Lex.getKind() == lltok::kw_define);
366 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000367
Chris Lattnerac161bf2009-01-02 07:01:27 +0000368 Function *F;
Chris Lattner3822f632009-01-02 08:05:26 +0000369 return ParseFunctionHeader(F, true) ||
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +0000370 ParseOptionalFunctionMetadata(*F) ||
Chris Lattner3822f632009-01-02 08:05:26 +0000371 ParseFunctionBody(*F);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000372}
373
Chris Lattner3822f632009-01-02 08:05:26 +0000374/// ParseGlobalType
375/// ::= 'constant'
376/// ::= 'global'
Chris Lattnerac161bf2009-01-02 07:01:27 +0000377bool LLParser::ParseGlobalType(bool &IsConstant) {
378 if (Lex.getKind() == lltok::kw_constant)
379 IsConstant = true;
380 else if (Lex.getKind() == lltok::kw_global)
381 IsConstant = false;
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000382 else {
383 IsConstant = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000384 return TokError("expected 'global' or 'constant'");
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000385 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000386 Lex.Lex();
387 return false;
388}
389
Dan Gohman466876b2009-08-12 23:32:33 +0000390/// ParseUnnamedGlobal:
391/// OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000392/// OptionalLinkage OptionalVisibility OptionalDLLStorageClass
393/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000394/// GlobalID '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000395/// GlobalID '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
396/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000397bool LLParser::ParseUnnamedGlobal() {
398 unsigned VarID = NumberedVals.size();
399 std::string Name;
400 LocTy NameLoc = Lex.getLoc();
401
402 // Handle the GlobalID form.
403 if (Lex.getKind() == lltok::GlobalID) {
404 if (Lex.getUIntVal() != VarID)
405 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000406 Twine(VarID) + "'");
Dan Gohman466876b2009-08-12 23:32:33 +0000407 Lex.Lex(); // eat GlobalID;
408
409 if (ParseToken(lltok::equal, "expected '=' after name"))
410 return true;
411 }
412
413 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000414 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000415 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000416 bool UnnamedAddr;
Dan Gohman466876b2009-08-12 23:32:33 +0000417 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000418 ParseOptionalVisibility(Visibility) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000419 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000420 ParseOptionalThreadLocal(TLM) ||
421 parseOptionalUnnamedAddr(UnnamedAddr))
Dan Gohman466876b2009-08-12 23:32:33 +0000422 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000423
Rafael Espindola464fe022014-07-30 22:51:54 +0000424 if (Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000425 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000426 DLLStorageClass, TLM, UnnamedAddr);
Rafael Espindola464fe022014-07-30 22:51:54 +0000427 return ParseAlias(Name, NameLoc, Linkage, Visibility, DLLStorageClass, TLM,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000428 UnnamedAddr);
Dan Gohman466876b2009-08-12 23:32:33 +0000429}
430
Chris Lattnerac161bf2009-01-02 07:01:27 +0000431/// ParseNamedGlobal:
432/// GlobalVar '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000433/// GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
434/// ... -> global variable
Chris Lattnerac161bf2009-01-02 07:01:27 +0000435bool LLParser::ParseNamedGlobal() {
436 assert(Lex.getKind() == lltok::GlobalVar);
437 LocTy NameLoc = Lex.getLoc();
438 std::string Name = Lex.getStrVal();
439 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000440
Chris Lattnerac161bf2009-01-02 07:01:27 +0000441 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000442 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000443 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000444 bool UnnamedAddr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000445 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
446 ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000447 ParseOptionalVisibility(Visibility) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000448 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000449 ParseOptionalThreadLocal(TLM) ||
450 parseOptionalUnnamedAddr(UnnamedAddr))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000451 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000452
Rafael Espindola464fe022014-07-30 22:51:54 +0000453 if (Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000454 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000455 DLLStorageClass, TLM, UnnamedAddr);
Rafael Espindola464fe022014-07-30 22:51:54 +0000456
457 return ParseAlias(Name, NameLoc, Linkage, Visibility, DLLStorageClass, TLM,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000458 UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000459}
460
David Majnemerdad0a642014-06-27 18:19:56 +0000461bool LLParser::parseComdat() {
462 assert(Lex.getKind() == lltok::ComdatVar);
463 std::string Name = Lex.getStrVal();
464 LocTy NameLoc = Lex.getLoc();
465 Lex.Lex();
466
467 if (ParseToken(lltok::equal, "expected '=' here"))
468 return true;
469
470 if (ParseToken(lltok::kw_comdat, "expected comdat keyword"))
471 return TokError("expected comdat type");
472
473 Comdat::SelectionKind SK;
474 switch (Lex.getKind()) {
475 default:
476 return TokError("unknown selection kind");
477 case lltok::kw_any:
478 SK = Comdat::Any;
479 break;
480 case lltok::kw_exactmatch:
481 SK = Comdat::ExactMatch;
482 break;
483 case lltok::kw_largest:
484 SK = Comdat::Largest;
485 break;
486 case lltok::kw_noduplicates:
487 SK = Comdat::NoDuplicates;
488 break;
489 case lltok::kw_samesize:
490 SK = Comdat::SameSize;
491 break;
492 }
493 Lex.Lex();
494
495 // See if the comdat was forward referenced, if so, use the comdat.
496 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
497 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
498 if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(Name))
499 return Error(NameLoc, "redefinition of comdat '$" + Name + "'");
500
501 Comdat *C;
502 if (I != ComdatSymTab.end())
503 C = &I->second;
504 else
505 C = M->getOrInsertComdat(Name);
506 C->setSelectionKind(SK);
507
508 return false;
509}
510
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000511// MDString:
512// ::= '!' STRINGCONSTANT
Chris Lattner1797fc72009-12-29 21:53:55 +0000513bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000514 std::string Str;
515 if (ParseStringConstant(Str)) return true;
Eli Bendersky5d5e18d2014-06-25 15:41:00 +0000516 llvm::UpgradeMDStringConstant(Str);
Chris Lattner1797fc72009-12-29 21:53:55 +0000517 Result = MDString::get(Context, Str);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000518 return false;
519}
520
521// MDNode:
522// ::= '!' MDNodeNumber
Chris Lattner6dac02a2009-12-30 04:15:23 +0000523bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000524 // !{ ..., !42, ... }
525 unsigned MID = 0;
Duncan P. N. Exon Smitha8d9a022015-01-12 21:14:38 +0000526 if (ParseUInt32(MID))
527 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000528
Chris Lattner8eff0152010-04-01 05:14:45 +0000529 // If not a forward reference, just return it now.
David Majnemer19b51052015-02-11 07:43:56 +0000530 if (NumberedMetadata.count(MID)) {
Duncan P. N. Exon Smitha8d9a022015-01-12 21:14:38 +0000531 Result = NumberedMetadata[MID];
532 return false;
533 }
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000534
Chris Lattner8eff0152010-04-01 05:14:45 +0000535 // Otherwise, create MDNode forward reference.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000536 auto &FwdRef = ForwardRefMDNodes[MID];
537 FwdRef = std::make_pair(MDTuple::getTemporary(Context, None), Lex.getLoc());
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000538
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000539 Result = FwdRef.first.get();
540 NumberedMetadata[MID].reset(Result);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000541 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000542}
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000543
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000544/// ParseNamedMetadata:
Devang Patelbe626972009-07-29 00:34:02 +0000545/// !foo = !{ !1, !2 }
546bool LLParser::ParseNamedMetadata() {
Chris Lattnereafe4de2009-12-30 05:02:06 +0000547 assert(Lex.getKind() == lltok::MetadataVar);
Devang Patelbe626972009-07-29 00:34:02 +0000548 std::string Name = Lex.getStrVal();
Chris Lattnereafe4de2009-12-30 05:02:06 +0000549 Lex.Lex();
Devang Patelbe626972009-07-29 00:34:02 +0000550
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000551 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000552 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000553 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Patelbe626972009-07-29 00:34:02 +0000554 return true;
555
Dan Gohman2637cc12010-07-21 23:38:33 +0000556 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000557 if (Lex.getKind() != lltok::rbrace)
558 do {
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000559 if (ParseToken(lltok::exclaim, "Expected '!' here"))
560 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000561
Craig Topper2617dcc2014-04-15 06:32:26 +0000562 MDNode *N = nullptr;
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000563 if (ParseMDNodeID(N)) return true;
Dan Gohman2637cc12010-07-21 23:38:33 +0000564 NMD->addOperand(N);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000565 } while (EatIfPresent(lltok::comma));
Devang Patelbe626972009-07-29 00:34:02 +0000566
Rafael Espindolac7818fb2015-05-26 20:37:36 +0000567 return ParseToken(lltok::rbrace, "expected end of metadata node");
Devang Patelbe626972009-07-29 00:34:02 +0000568}
569
Devang Patel39e64d42009-07-01 19:21:12 +0000570/// ParseStandaloneMetadata:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000571/// !42 = !{...}
Devang Patel39e64d42009-07-01 19:21:12 +0000572bool LLParser::ParseStandaloneMetadata() {
Chris Lattner1eed2d62009-12-30 04:56:59 +0000573 assert(Lex.getKind() == lltok::exclaim);
Devang Patel39e64d42009-07-01 19:21:12 +0000574 Lex.Lex();
575 unsigned MetadataID = 0;
Devang Patel39e64d42009-07-01 19:21:12 +0000576
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000577 MDNode *Init;
Chris Lattner278bc952009-12-29 22:40:21 +0000578 if (ParseUInt32(MetadataID) ||
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000579 ParseToken(lltok::equal, "expected '=' here"))
580 return true;
581
582 // Detect common error, from old metadata syntax.
583 if (Lex.getKind() == lltok::Type)
584 return TokError("unexpected type in metadata definition");
585
Duncan P. N. Exon Smith090a19b2015-01-08 22:38:29 +0000586 bool IsDistinct = EatIfPresent(lltok::kw_distinct);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +0000587 if (Lex.getKind() == lltok::MetadataVar) {
588 if (ParseSpecializedMDNode(Init, IsDistinct))
589 return true;
590 } else if (ParseToken(lltok::exclaim, "Expected '!' here") ||
591 ParseMDTuple(Init, IsDistinct))
Devang Patele059ba6e2009-07-23 01:07:34 +0000592 return true;
593
Chris Lattnerfc58af22009-12-30 04:51:58 +0000594 // See if this was forward referenced, if so, handle it.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000595 auto FI = ForwardRefMDNodes.find(MetadataID);
Devang Pateld2541152009-07-08 19:23:54 +0000596 if (FI != ForwardRefMDNodes.end()) {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000597 FI->second.first->replaceAllUsesWith(Init);
Devang Pateld2541152009-07-08 19:23:54 +0000598 ForwardRefMDNodes.erase(FI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000599
Chris Lattnerfc58af22009-12-30 04:51:58 +0000600 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
601 } else {
David Majnemer19b51052015-02-11 07:43:56 +0000602 if (NumberedMetadata.count(MetadataID))
Chris Lattnerfc58af22009-12-30 04:51:58 +0000603 return TokError("Metadata id is already used");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000604 NumberedMetadata[MetadataID].reset(Init);
Devang Pateld2541152009-07-08 19:23:54 +0000605 }
606
Devang Patel39e64d42009-07-01 19:21:12 +0000607 return false;
608}
609
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000610static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
611 return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
612 (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
613}
614
Chris Lattnerac161bf2009-01-02 07:01:27 +0000615/// ParseAlias:
Rafael Espindola464fe022014-07-30 22:51:54 +0000616/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility
617/// OptionalDLLStorageClass OptionalThreadLocal
Eric Christopher536f0a92015-05-28 23:07:39 +0000618/// OptionalUnnamedAddr 'alias' Aliasee
Rafael Espindola6b238632014-05-16 19:35:39 +0000619///
Chris Lattnerac161bf2009-01-02 07:01:27 +0000620/// Aliasee
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000621/// ::= TypeAndValue
Chris Lattnerac161bf2009-01-02 07:01:27 +0000622///
Eric Christopher536f0a92015-05-28 23:07:39 +0000623/// Everything through OptionalUnnamedAddr has already been parsed.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000624///
Rafael Espindola464fe022014-07-30 22:51:54 +0000625bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc, unsigned L,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000626 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000627 GlobalVariable::ThreadLocalMode TLM,
628 bool UnnamedAddr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000629 assert(Lex.getKind() == lltok::kw_alias);
630 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000631
Rafael Espindola78527052013-10-06 15:10:43 +0000632 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
633
Rafael Espindolacaa43562013-10-09 16:07:32 +0000634 if(!GlobalAlias::isValidLinkage(Linkage))
Rafael Espindola464fe022014-07-30 22:51:54 +0000635 return Error(NameLoc, "invalid linkage type for alias");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000636
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000637 if (!isValidVisibilityForLinkage(Visibility, L))
Rafael Espindola464fe022014-07-30 22:51:54 +0000638 return Error(NameLoc,
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000639 "symbol with local linkage must have default visibility");
640
Rafael Espindola64c1e182014-06-03 02:41:57 +0000641 Constant *Aliasee;
642 LocTy AliaseeLoc = Lex.getLoc();
643 if (Lex.getKind() != lltok::kw_bitcast &&
644 Lex.getKind() != lltok::kw_getelementptr &&
645 Lex.getKind() != lltok::kw_addrspacecast &&
646 Lex.getKind() != lltok::kw_inttoptr) {
647 if (ParseGlobalTypeAndValue(Aliasee))
Rafael Espindola6b238632014-05-16 19:35:39 +0000648 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000649 } else {
Rafael Espindola64c1e182014-06-03 02:41:57 +0000650 // The bitcast dest type is not present, it is implied by the dest type.
651 ValID ID;
652 if (ParseValID(ID))
653 return true;
654 if (ID.Kind != ValID::t_Constant)
655 return Error(AliaseeLoc, "invalid aliasee");
656 Aliasee = ID.ConstantVal;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000657 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000658
Rafael Espindola64c1e182014-06-03 02:41:57 +0000659 Type *AliaseeType = Aliasee->getType();
660 auto *PTy = dyn_cast<PointerType>(AliaseeType);
661 if (!PTy)
662 return Error(AliaseeLoc, "An alias must have pointer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +0000663
664 // Okay, create the alias but do not insert it into the module yet.
Rafael Espindola4fe00942014-05-16 13:34:04 +0000665 std::unique_ptr<GlobalAlias> GA(
David Blaikief64246b2015-04-29 21:22:39 +0000666 GlobalAlias::create(PTy, (GlobalValue::LinkageTypes)Linkage, Name,
667 Aliasee, /*Parent*/ nullptr));
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000668 GA->setThreadLocalMode(TLM);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000669 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000670 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000671 GA->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000672
Chris Lattnerac161bf2009-01-02 07:01:27 +0000673 // See if this value already exists in the symbol table. If so, it is either
674 // a redefinition or a definition of a forward reference.
Chris Lattnere38317f2009-10-25 23:22:50 +0000675 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000676 // See if this was a redefinition. If so, there is no entry in
677 // ForwardRefVals.
678 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
679 I = ForwardRefVals.find(Name);
680 if (I == ForwardRefVals.end())
681 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
682
683 // Otherwise, this was a definition of forward ref. Verify that types
684 // agree.
685 if (Val->getType() != GA->getType())
686 return Error(NameLoc,
687 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000688
Chris Lattnerac161bf2009-01-02 07:01:27 +0000689 // If they agree, just RAUW the old value with the alias and remove the
690 // forward ref info.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000691 Val->replaceAllUsesWith(GA.get());
Chris Lattnerac161bf2009-01-02 07:01:27 +0000692 Val->eraseFromParent();
693 ForwardRefVals.erase(I);
694 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000695
Chris Lattnerac161bf2009-01-02 07:01:27 +0000696 // Insert into the module, we know its name won't collide now.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000697 M->getAliasList().push_back(GA.get());
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000698 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000699
Rafael Espindolaaa273822014-05-09 21:49:17 +0000700 // The module owns this now
701 GA.release();
702
Chris Lattnerac161bf2009-01-02 07:01:27 +0000703 return false;
704}
705
706/// ParseGlobal
Nico Rieck7157bb72014-01-14 15:22:47 +0000707/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Eric Christopher536f0a92015-05-28 23:07:39 +0000708/// OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000709/// OptionalExternallyInitialized GlobalType Type Const
Nico Rieck7157bb72014-01-14 15:22:47 +0000710/// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Eric Christopher536f0a92015-05-28 23:07:39 +0000711/// OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000712/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerac161bf2009-01-02 07:01:27 +0000713///
Eric Christopher536f0a92015-05-28 23:07:39 +0000714/// Everything up to and including OptionalUnnamedAddr has been parsed
David Majnemerc4ab61c2014-03-09 06:41:58 +0000715/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000716///
717bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
718 unsigned Linkage, bool HasLinkage,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000719 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000720 GlobalVariable::ThreadLocalMode TLM,
721 bool UnnamedAddr) {
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000722 if (!isValidVisibilityForLinkage(Visibility, Linkage))
723 return Error(NameLoc,
724 "symbol with local linkage must have default visibility");
725
Chris Lattnerac161bf2009-01-02 07:01:27 +0000726 unsigned AddrSpace;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000727 bool IsConstant, IsExternallyInitialized;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000728 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000729 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000730
Craig Topper2617dcc2014-04-15 06:32:26 +0000731 Type *Ty = nullptr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000732 if (ParseOptionalAddrSpace(AddrSpace) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000733 ParseOptionalToken(lltok::kw_externally_initialized,
734 IsExternallyInitialized,
735 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000736 ParseGlobalType(IsConstant) ||
737 ParseType(Ty, TyLoc))
738 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000739
Chris Lattnerac161bf2009-01-02 07:01:27 +0000740 // If the linkage is specified and is external, then no initializer is
741 // present.
Craig Topper2617dcc2014-04-15 06:32:26 +0000742 Constant *Init = nullptr;
Nico Rieck7157bb72014-01-14 15:22:47 +0000743 if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerac161bf2009-01-02 07:01:27 +0000744 Linkage != GlobalValue::ExternalLinkage)) {
745 if (ParseGlobalValue(Ty, Init))
746 return true;
747 }
748
David Majnemer49b3d9b2015-02-16 08:41:08 +0000749 if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000750 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000751
David Majnemer598bd052014-12-09 05:56:09 +0000752 GlobalValue *GVal = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000753
754 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000755 if (!Name.empty()) {
David Majnemer598bd052014-12-09 05:56:09 +0000756 GVal = M->getNamedValue(Name);
757 if (GVal) {
Chris Lattnere38317f2009-10-25 23:22:50 +0000758 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
759 return Error(NameLoc, "redefinition of global '@" + Name + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +0000760 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000761 } else {
762 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
763 I = ForwardRefValIDs.find(NumberedVals.size());
764 if (I != ForwardRefValIDs.end()) {
David Majnemer598bd052014-12-09 05:56:09 +0000765 GVal = I->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000766 ForwardRefValIDs.erase(I);
767 }
768 }
769
David Majnemer598bd052014-12-09 05:56:09 +0000770 GlobalVariable *GV;
771 if (!GVal) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000772 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
773 Name, nullptr, GlobalVariable::NotThreadLocal,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000774 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000775 } else {
David Blaikie8f27ae42015-05-13 22:55:01 +0000776 if (GVal->getValueType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +0000777 return Error(TyLoc,
778 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000779
David Majnemer598bd052014-12-09 05:56:09 +0000780 GV = cast<GlobalVariable>(GVal);
781
Chris Lattnerac161bf2009-01-02 07:01:27 +0000782 // Move the forward-reference to the correct spot in the module.
783 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
784 }
785
786 if (Name.empty())
787 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000788
Chris Lattnerac161bf2009-01-02 07:01:27 +0000789 // Set the parsed properties on the global.
790 if (Init)
791 GV->setInitializer(Init);
792 GV->setConstant(IsConstant);
793 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
794 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000795 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000796 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000797 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000798 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000799
Chris Lattnerac161bf2009-01-02 07:01:27 +0000800 // Parse attributes on the global.
801 while (Lex.getKind() == lltok::comma) {
802 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000803
Chris Lattnerac161bf2009-01-02 07:01:27 +0000804 if (Lex.getKind() == lltok::kw_section) {
805 Lex.Lex();
806 GV->setSection(Lex.getStrVal());
807 if (ParseToken(lltok::StringConstant, "expected global section string"))
808 return true;
809 } else if (Lex.getKind() == lltok::kw_align) {
810 unsigned Alignment;
811 if (ParseOptionalAlignment(Alignment)) return true;
812 GV->setAlignment(Alignment);
813 } else {
David Majnemerdad0a642014-06-27 18:19:56 +0000814 Comdat *C;
Rafael Espindola83a362c2015-01-06 22:55:16 +0000815 if (parseOptionalComdat(Name, C))
David Majnemerdad0a642014-06-27 18:19:56 +0000816 return true;
817 if (C)
818 GV->setComdat(C);
819 else
820 return TokError("unknown global variable property!");
Chris Lattnerac161bf2009-01-02 07:01:27 +0000821 }
822 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000823
Chris Lattnerac161bf2009-01-02 07:01:27 +0000824 return false;
825}
826
Bill Wendling63b88192013-02-06 06:52:58 +0000827/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +0000828/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +0000829bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +0000830 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +0000831 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +0000832 Lex.Lex();
833
David Majnemerb39e22b2014-12-09 18:33:57 +0000834 if (Lex.getKind() != lltok::AttrGrpID)
835 return TokError("expected attribute group id");
836
Bill Wendling63b88192013-02-06 06:52:58 +0000837 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000838 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +0000839 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +0000840 Lex.Lex();
841
842 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +0000843 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +0000844 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +0000845 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +0000846 ParseToken(lltok::rbrace, "expected end of attribute group"))
847 return true;
848
Bill Wendlingb32b0412013-02-08 06:32:06 +0000849 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +0000850 return Error(AttrGrpLoc, "attribute group has no attributes");
851
852 return false;
853}
854
Bill Wendling8b0321d2013-02-08 00:52:31 +0000855/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +0000856/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +0000857bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
858 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000859 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +0000860 bool HaveError = false;
861
862 B.clear();
863
Bill Wendling63b88192013-02-06 06:52:58 +0000864 while (true) {
865 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +0000866 if (Token == lltok::kw_builtin)
867 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +0000868 switch (Token) {
869 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000870 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +0000871 return Error(Lex.getLoc(), "unterminated attribute group");
872 case lltok::rbrace:
873 // Finished.
874 return false;
875
Bill Wendlingb32b0412013-02-08 06:32:06 +0000876 case lltok::AttrGrpID: {
877 // Allow a function to reference an attribute group:
878 //
879 // define void @foo() #1 { ... }
880 if (inAttrGrp)
881 HaveError |=
882 Error(Lex.getLoc(),
883 "cannot have an attribute group reference in an attribute group");
884
885 unsigned AttrGrpNum = Lex.getUIntVal();
886 if (inAttrGrp) break;
887
888 // Save the reference to the attribute group. We'll fill it in later.
889 FwdRefAttrGrps.push_back(AttrGrpNum);
890 break;
891 }
Bill Wendling63b88192013-02-06 06:52:58 +0000892 // Target-dependent attributes:
893 case lltok::StringConstant: {
894 std::string Attr = Lex.getStrVal();
895 Lex.Lex();
896 std::string Val;
897 if (EatIfPresent(lltok::equal) &&
898 ParseStringConstant(Val))
899 return true;
900
901 B.addAttribute(Attr, Val);
Bill Wendlingb1ea9802013-02-10 10:12:50 +0000902 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000903 }
904
905 // Target-independent attributes:
906 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +0000907 // As a hack, we allow function alignment to be initially parsed as an
908 // attribute on a function declaration/definition or added to an attribute
909 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +0000910 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000911 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000912 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000913 if (ParseToken(lltok::equal, "expected '=' here") ||
914 ParseUInt32(Alignment))
915 return true;
916 } else {
917 if (ParseOptionalAlignment(Alignment))
918 return true;
919 }
Bill Wendling63b88192013-02-06 06:52:58 +0000920 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000921 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000922 }
923 case lltok::kw_alignstack: {
924 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000925 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000926 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000927 if (ParseToken(lltok::equal, "expected '=' here") ||
928 ParseUInt32(Alignment))
929 return true;
930 } else {
931 if (ParseOptionalStackAlignment(Alignment))
932 return true;
933 }
Bill Wendling63b88192013-02-06 06:52:58 +0000934 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000935 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000936 }
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000937 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Michael Gottesman41748d72013-06-27 00:25:01 +0000938 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
Diego Novilloc6399532013-05-24 12:26:52 +0000939 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000940 case lltok::kw_convergent: B.addAttribute(Attribute::Convergent); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000941 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000942 case lltok::kw_jumptable: B.addAttribute(Attribute::JumpTable); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000943 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
944 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
945 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
946 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
947 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
948 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
949 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
950 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
951 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
952 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000953 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000954 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
955 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
956 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
957 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
958 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
959 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
960 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000961 case lltok::kw_safestack: B.addAttribute(Attribute::SafeStack); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000962 case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
963 case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
964 case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
965 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000966
967 // Error handling.
968 case lltok::kw_inreg:
969 case lltok::kw_signext:
970 case lltok::kw_zeroext:
971 HaveError |=
972 Error(Lex.getLoc(),
973 "invalid use of attribute on a function");
974 break;
975 case lltok::kw_byval:
Hal Finkelb0407ba2014-07-18 15:51:28 +0000976 case lltok::kw_dereferenceable:
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000977 case lltok::kw_dereferenceable_or_null:
Reid Klecknera534a382013-12-19 02:14:12 +0000978 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000979 case lltok::kw_nest:
980 case lltok::kw_noalias:
981 case lltok::kw_nocapture:
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000982 case lltok::kw_nonnull:
Stephen Linb8bd2322013-04-20 05:14:40 +0000983 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000984 case lltok::kw_sret:
985 HaveError |=
986 Error(Lex.getLoc(),
987 "invalid use of parameter-only attribute on a function");
988 break;
Bill Wendling63b88192013-02-06 06:52:58 +0000989 }
990
991 Lex.Lex();
992 }
993}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000994
995//===----------------------------------------------------------------------===//
996// GlobalValue Reference/Resolution Routines.
997//===----------------------------------------------------------------------===//
998
999/// GetGlobalVal - Get a value with the specified name or ID, creating a
1000/// forward reference record if needed. This can return null if the value
1001/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +00001002GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00001003 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +00001004 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001005 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001006 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001007 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001008 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001009
Chris Lattnerac161bf2009-01-02 07:01:27 +00001010 // Look this name up in the normal function symbol table.
1011 GlobalValue *Val =
1012 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001013
Chris Lattnerac161bf2009-01-02 07:01:27 +00001014 // If this is a forward reference for the value, see if we already created a
1015 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001016 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001017 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
1018 I = ForwardRefVals.find(Name);
1019 if (I != ForwardRefVals.end())
1020 Val = I->second.first;
1021 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001022
Chris Lattnerac161bf2009-01-02 07:01:27 +00001023 // If we have the value in the symbol table or fwd-ref table, return it.
1024 if (Val) {
1025 if (Val->getType() == Ty) return Val;
1026 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001027 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001028 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001029 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001030
Chris Lattnerac161bf2009-01-02 07:01:27 +00001031 // Otherwise, create a new forward reference for this value and remember it.
1032 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001033 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001034 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001035 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001036 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001037 GlobalValue::ExternalWeakLinkage, nullptr, Name,
1038 nullptr, GlobalVariable::NotThreadLocal,
Justin Holewinski898a0a02012-11-16 21:03:47 +00001039 PTy->getAddressSpace());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001040
Chris Lattnerac161bf2009-01-02 07:01:27 +00001041 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1042 return FwdVal;
1043}
1044
Chris Lattner229907c2011-07-18 04:54:35 +00001045GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1046 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001047 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001048 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001049 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001050 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001051
Craig Topper2617dcc2014-04-15 06:32:26 +00001052 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001053
Chris Lattnerac161bf2009-01-02 07:01:27 +00001054 // If this is a forward reference for the value, see if we already created a
1055 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001056 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001057 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
1058 I = ForwardRefValIDs.find(ID);
1059 if (I != ForwardRefValIDs.end())
1060 Val = I->second.first;
1061 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001062
Chris Lattnerac161bf2009-01-02 07:01:27 +00001063 // If we have the value in the symbol table or fwd-ref table, return it.
1064 if (Val) {
1065 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001066 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001067 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001068 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001069 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001070
Chris Lattnerac161bf2009-01-02 07:01:27 +00001071 // Otherwise, create a new forward reference for this value and remember it.
1072 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001073 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001074 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001075 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001076 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001077 GlobalValue::ExternalWeakLinkage, nullptr, "");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001078
Chris Lattnerac161bf2009-01-02 07:01:27 +00001079 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1080 return FwdVal;
1081}
1082
1083
1084//===----------------------------------------------------------------------===//
David Majnemerdad0a642014-06-27 18:19:56 +00001085// Comdat Reference/Resolution Routines.
1086//===----------------------------------------------------------------------===//
1087
1088Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) {
1089 // Look this name up in the comdat symbol table.
1090 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
1091 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
1092 if (I != ComdatSymTab.end())
1093 return &I->second;
1094
1095 // Otherwise, create a new forward reference for this value and remember it.
1096 Comdat *C = M->getOrInsertComdat(Name);
1097 ForwardRefComdats[Name] = Loc;
1098 return C;
1099}
1100
1101
1102//===----------------------------------------------------------------------===//
Chris Lattnerac161bf2009-01-02 07:01:27 +00001103// Helper Routines.
1104//===----------------------------------------------------------------------===//
1105
1106/// ParseToken - If the current token has the specified kind, eat it and return
1107/// success. Otherwise, emit the specified error and return failure.
1108bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1109 if (Lex.getKind() != T)
1110 return TokError(ErrMsg);
1111 Lex.Lex();
1112 return false;
1113}
1114
Chris Lattner3822f632009-01-02 08:05:26 +00001115/// ParseStringConstant
1116/// ::= StringConstant
1117bool LLParser::ParseStringConstant(std::string &Result) {
1118 if (Lex.getKind() != lltok::StringConstant)
1119 return TokError("expected string constant");
1120 Result = Lex.getStrVal();
1121 Lex.Lex();
1122 return false;
1123}
1124
1125/// ParseUInt32
1126/// ::= uint32
1127bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001128 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1129 return TokError("expected integer");
1130 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1131 if (Val64 != unsigned(Val64))
1132 return TokError("expected 32-bit integer (too large)");
1133 Val = Val64;
1134 Lex.Lex();
1135 return false;
1136}
1137
Hal Finkelb0407ba2014-07-18 15:51:28 +00001138/// ParseUInt64
1139/// ::= uint64
1140bool LLParser::ParseUInt64(uint64_t &Val) {
1141 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1142 return TokError("expected integer");
1143 Val = Lex.getAPSIntVal().getLimitedValue();
1144 Lex.Lex();
1145 return false;
1146}
1147
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001148/// ParseTLSModel
1149/// := 'localdynamic'
1150/// := 'initialexec'
1151/// := 'localexec'
1152bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1153 switch (Lex.getKind()) {
1154 default:
1155 return TokError("expected localdynamic, initialexec or localexec");
1156 case lltok::kw_localdynamic:
1157 TLM = GlobalVariable::LocalDynamicTLSModel;
1158 break;
1159 case lltok::kw_initialexec:
1160 TLM = GlobalVariable::InitialExecTLSModel;
1161 break;
1162 case lltok::kw_localexec:
1163 TLM = GlobalVariable::LocalExecTLSModel;
1164 break;
1165 }
1166
1167 Lex.Lex();
1168 return false;
1169}
1170
1171/// ParseOptionalThreadLocal
1172/// := /*empty*/
1173/// := 'thread_local'
1174/// := 'thread_local' '(' tlsmodel ')'
1175bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1176 TLM = GlobalVariable::NotThreadLocal;
1177 if (!EatIfPresent(lltok::kw_thread_local))
1178 return false;
1179
1180 TLM = GlobalVariable::GeneralDynamicTLSModel;
1181 if (Lex.getKind() == lltok::lparen) {
1182 Lex.Lex();
1183 return ParseTLSModel(TLM) ||
1184 ParseToken(lltok::rparen, "expected ')' after thread local model");
1185 }
1186 return false;
1187}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001188
1189/// ParseOptionalAddrSpace
1190/// := /*empty*/
1191/// := 'addrspace' '(' uint32 ')'
1192bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1193 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001194 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001195 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001196 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001197 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001198 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001199}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001200
Bill Wendling34c2eb22012-12-04 23:40:58 +00001201/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1202bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1203 bool HaveError = false;
1204
1205 B.clear();
1206
1207 while (1) {
1208 lltok::Kind Token = Lex.getKind();
1209 switch (Token) {
1210 default: // End of attributes.
1211 return HaveError;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001212 case lltok::kw_align: {
1213 unsigned Alignment;
1214 if (ParseOptionalAlignment(Alignment))
1215 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001216 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001217 continue;
1218 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001219 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001220 case lltok::kw_dereferenceable: {
1221 uint64_t Bytes;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001222 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
Hal Finkelb0407ba2014-07-18 15:51:28 +00001223 return true;
1224 B.addDereferenceableAttr(Bytes);
1225 continue;
1226 }
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001227 case lltok::kw_dereferenceable_or_null: {
1228 uint64_t Bytes;
1229 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
1230 return true;
1231 B.addDereferenceableOrNullAttr(Bytes);
1232 continue;
1233 }
Reid Klecknera534a382013-12-19 02:14:12 +00001234 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001235 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1236 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1237 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1238 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001239 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001240 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1241 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001242 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001243 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1244 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1245 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001246
Stephen Lin7577ed52013-04-20 13:16:13 +00001247 case lltok::kw_alignstack:
1248 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001249 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001250 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001251 case lltok::kw_jumptable:
Stephen Lin7577ed52013-04-20 13:16:13 +00001252 case lltok::kw_minsize:
1253 case lltok::kw_naked:
1254 case lltok::kw_nobuiltin:
1255 case lltok::kw_noduplicate:
1256 case lltok::kw_noimplicitfloat:
1257 case lltok::kw_noinline:
1258 case lltok::kw_nonlazybind:
1259 case lltok::kw_noredzone:
1260 case lltok::kw_noreturn:
1261 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001262 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001263 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001264 case lltok::kw_returns_twice:
1265 case lltok::kw_sanitize_address:
1266 case lltok::kw_sanitize_memory:
1267 case lltok::kw_sanitize_thread:
1268 case lltok::kw_ssp:
1269 case lltok::kw_sspreq:
1270 case lltok::kw_sspstrong:
Peter Collingbourne82437bf2015-06-15 21:07:11 +00001271 case lltok::kw_safestack:
Stephen Lin7577ed52013-04-20 13:16:13 +00001272 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001273 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1274 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001275 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001276
Bill Wendling34c2eb22012-12-04 23:40:58 +00001277 Lex.Lex();
1278 }
1279}
1280
1281/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1282bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1283 bool HaveError = false;
1284
1285 B.clear();
1286
1287 while (1) {
1288 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001289 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001290 default: // End of attributes.
1291 return HaveError;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001292 case lltok::kw_dereferenceable: {
1293 uint64_t Bytes;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001294 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
Hal Finkelb0407ba2014-07-18 15:51:28 +00001295 return true;
1296 B.addDereferenceableAttr(Bytes);
1297 continue;
1298 }
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001299 case lltok::kw_dereferenceable_or_null: {
1300 uint64_t Bytes;
1301 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
1302 return true;
1303 B.addDereferenceableOrNullAttr(Bytes);
1304 continue;
1305 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001306 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1307 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001308 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001309 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1310 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001311
Bill Wendling34c2eb22012-12-04 23:40:58 +00001312 // Error handling.
Stephen Lin7577ed52013-04-20 13:16:13 +00001313 case lltok::kw_align:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001314 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001315 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001316 case lltok::kw_nest:
1317 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001318 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001319 case lltok::kw_sret:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001320 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001321 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001322
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001323 case lltok::kw_alignstack:
1324 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001325 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001326 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001327 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001328 case lltok::kw_jumptable:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001329 case lltok::kw_minsize:
1330 case lltok::kw_naked:
1331 case lltok::kw_nobuiltin:
1332 case lltok::kw_noduplicate:
1333 case lltok::kw_noimplicitfloat:
1334 case lltok::kw_noinline:
1335 case lltok::kw_nonlazybind:
1336 case lltok::kw_noredzone:
1337 case lltok::kw_noreturn:
1338 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001339 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001340 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001341 case lltok::kw_returns_twice:
1342 case lltok::kw_sanitize_address:
1343 case lltok::kw_sanitize_memory:
1344 case lltok::kw_sanitize_thread:
1345 case lltok::kw_ssp:
1346 case lltok::kw_sspreq:
1347 case lltok::kw_sspstrong:
Peter Collingbourne82437bf2015-06-15 21:07:11 +00001348 case lltok::kw_safestack:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001349 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001350 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001351 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001352
1353 case lltok::kw_readnone:
1354 case lltok::kw_readonly:
1355 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001356 }
1357
Chris Lattnerac161bf2009-01-02 07:01:27 +00001358 Lex.Lex();
1359 }
1360}
1361
1362/// ParseOptionalLinkage
1363/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001364/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001365/// ::= 'internal'
1366/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001367/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001368/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001369/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001370/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001371/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001372/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001373/// ::= 'extern_weak'
1374/// ::= 'external'
1375bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1376 HasLinkage = false;
1377 switch (Lex.getKind()) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001378 default: Res=GlobalValue::ExternalLinkage; return false;
1379 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001380 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1381 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1382 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1383 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1384 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001385 case lltok::kw_available_externally:
1386 Res = GlobalValue::AvailableExternallyLinkage;
1387 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001388 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001389 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001390 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1391 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001392 }
1393 Lex.Lex();
1394 HasLinkage = true;
1395 return false;
1396}
1397
1398/// ParseOptionalVisibility
1399/// ::= /*empty*/
1400/// ::= 'default'
1401/// ::= 'hidden'
1402/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001403///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001404bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1405 switch (Lex.getKind()) {
1406 default: Res = GlobalValue::DefaultVisibility; return false;
1407 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1408 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1409 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1410 }
1411 Lex.Lex();
1412 return false;
1413}
1414
Nico Rieck7157bb72014-01-14 15:22:47 +00001415/// ParseOptionalDLLStorageClass
1416/// ::= /*empty*/
1417/// ::= 'dllimport'
1418/// ::= 'dllexport'
1419///
1420bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1421 switch (Lex.getKind()) {
1422 default: Res = GlobalValue::DefaultStorageClass; return false;
1423 case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break;
1424 case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break;
1425 }
1426 Lex.Lex();
1427 return false;
1428}
1429
Chris Lattnerac161bf2009-01-02 07:01:27 +00001430/// ParseOptionalCallingConv
1431/// ::= /*empty*/
1432/// ::= 'ccc'
1433/// ::= 'fastcc'
Reid Kleckner35fc3632014-12-01 21:04:44 +00001434/// ::= 'intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001435/// ::= 'coldcc'
1436/// ::= 'x86_stdcallcc'
1437/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001438/// ::= 'x86_thiscallcc'
Reid Kleckner9ccce992014-10-28 01:29:26 +00001439/// ::= 'x86_vectorcallcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001440/// ::= 'arm_apcscc'
1441/// ::= 'arm_aapcscc'
1442/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001443/// ::= 'msp430_intrcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001444/// ::= 'ptx_kernel'
1445/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001446/// ::= 'spir_func'
1447/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001448/// ::= 'x86_64_sysvcc'
1449/// ::= 'x86_64_win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001450/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001451/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001452/// ::= 'preserve_mostcc'
1453/// ::= 'preserve_allcc'
Reid Kleckner35fc3632014-12-01 21:04:44 +00001454/// ::= 'ghccc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001455/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001456///
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00001457bool LLParser::ParseOptionalCallingConv(unsigned &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001458 switch (Lex.getKind()) {
1459 default: CC = CallingConv::C; return false;
1460 case lltok::kw_ccc: CC = CallingConv::C; break;
1461 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1462 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1463 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1464 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001465 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Reid Kleckner9ccce992014-10-28 01:29:26 +00001466 case lltok::kw_x86_vectorcallcc:CC = CallingConv::X86_VectorCall; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001467 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1468 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1469 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001470 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001471 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1472 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001473 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1474 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001475 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001476 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1477 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001478 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001479 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001480 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1481 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Reid Kleckner35fc3632014-12-01 21:04:44 +00001482 case lltok::kw_ghccc: CC = CallingConv::GHC; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001483 case lltok::kw_cc: {
Sandeep Patel68c5f472009-09-02 08:44:58 +00001484 Lex.Lex();
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00001485 return ParseUInt32(CC);
Sandeep Patel68c5f472009-09-02 08:44:58 +00001486 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001487 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001488
Chris Lattnerac161bf2009-01-02 07:01:27 +00001489 Lex.Lex();
1490 return false;
1491}
1492
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +00001493/// ParseMetadataAttachment
1494/// ::= !dbg !42
1495bool LLParser::ParseMetadataAttachment(unsigned &Kind, MDNode *&MD) {
1496 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata attachment");
1497
1498 std::string Name = Lex.getStrVal();
1499 Kind = M->getMDKindID(Name);
1500 Lex.Lex();
1501
1502 return ParseMDNode(MD);
1503}
1504
Chris Lattner5c427632009-12-30 05:31:19 +00001505/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001506/// ::= !dbg !42 (',' !dbg !57)*
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00001507bool LLParser::ParseInstructionMetadata(Instruction &Inst) {
Chris Lattner5c427632009-12-30 05:31:19 +00001508 do {
1509 if (Lex.getKind() != lltok::MetadataVar)
1510 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001511
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +00001512 unsigned MDK;
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00001513 MDNode *N;
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +00001514 if (ParseMetadataAttachment(MDK, N))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001515 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001516
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00001517 Inst.setMetadata(MDK, N);
Manman Ren209b17c2013-09-28 00:22:27 +00001518 if (MDK == LLVMContext::MD_tbaa)
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00001519 InstsWithTBAATag.push_back(&Inst);
Manman Ren209b17c2013-09-28 00:22:27 +00001520
Chris Lattner596760d2009-12-29 21:25:40 +00001521 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001522 } while (EatIfPresent(lltok::comma));
1523 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001524}
1525
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001526/// ParseOptionalFunctionMetadata
1527/// ::= (!dbg !57)*
1528bool LLParser::ParseOptionalFunctionMetadata(Function &F) {
1529 while (Lex.getKind() == lltok::MetadataVar) {
1530 unsigned MDK;
1531 MDNode *N;
1532 if (ParseMetadataAttachment(MDK, N))
1533 return true;
1534
1535 F.setMetadata(MDK, N);
1536 }
1537 return false;
1538}
1539
Chris Lattnerac161bf2009-01-02 07:01:27 +00001540/// ParseOptionalAlignment
1541/// ::= /* empty */
1542/// ::= 'align' 4
1543bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1544 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001545 if (!EatIfPresent(lltok::kw_align))
1546 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001547 LocTy AlignLoc = Lex.getLoc();
1548 if (ParseUInt32(Alignment)) return true;
1549 if (!isPowerOf2_32(Alignment))
1550 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001551 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001552 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001553 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001554}
1555
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001556/// ParseOptionalDerefAttrBytes
Hal Finkelb0407ba2014-07-18 15:51:28 +00001557/// ::= /* empty */
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001558/// ::= AttrKind '(' 4 ')'
1559///
1560/// where AttrKind is either 'dereferenceable' or 'dereferenceable_or_null'.
1561bool LLParser::ParseOptionalDerefAttrBytes(lltok::Kind AttrKind,
1562 uint64_t &Bytes) {
1563 assert((AttrKind == lltok::kw_dereferenceable ||
1564 AttrKind == lltok::kw_dereferenceable_or_null) &&
1565 "contract!");
1566
Hal Finkelb0407ba2014-07-18 15:51:28 +00001567 Bytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001568 if (!EatIfPresent(AttrKind))
Hal Finkelb0407ba2014-07-18 15:51:28 +00001569 return false;
1570 LocTy ParenLoc = Lex.getLoc();
1571 if (!EatIfPresent(lltok::lparen))
1572 return Error(ParenLoc, "expected '('");
1573 LocTy DerefLoc = Lex.getLoc();
1574 if (ParseUInt64(Bytes)) return true;
1575 ParenLoc = Lex.getLoc();
1576 if (!EatIfPresent(lltok::rparen))
1577 return Error(ParenLoc, "expected ')'");
1578 if (!Bytes)
1579 return Error(DerefLoc, "dereferenceable bytes must be non-zero");
1580 return false;
1581}
1582
Chris Lattnerb2f39502009-12-30 05:44:30 +00001583/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001584/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001585/// ::= ',' align 4
1586///
1587/// This returns with AteExtraComma set to true if it ate an excess comma at the
1588/// end.
1589bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1590 bool &AteExtraComma) {
1591 AteExtraComma = false;
1592 while (EatIfPresent(lltok::comma)) {
1593 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001594 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001595 AteExtraComma = true;
1596 return false;
1597 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001598
Chris Lattner95b0ff42010-04-23 00:50:50 +00001599 if (Lex.getKind() != lltok::kw_align)
1600 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001601
Chris Lattner95b0ff42010-04-23 00:50:50 +00001602 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001603 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001604
Devang Patelea8a4b92009-09-17 23:04:48 +00001605 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001606}
1607
Eli Friedmanfee02c62011-07-25 23:16:38 +00001608/// ParseScopeAndOrdering
1609/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1610/// else: ::=
1611///
1612/// This sets Scope and Ordering to the parsed values.
1613bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1614 AtomicOrdering &Ordering) {
1615 if (!isAtomic)
1616 return false;
1617
1618 Scope = CrossThread;
1619 if (EatIfPresent(lltok::kw_singlethread))
1620 Scope = SingleThread;
Tim Northovere94a5182014-03-11 10:48:52 +00001621
1622 return ParseOrdering(Ordering);
1623}
1624
1625/// ParseOrdering
1626/// ::= AtomicOrdering
1627///
1628/// This sets Ordering to the parsed value.
1629bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001630 switch (Lex.getKind()) {
1631 default: return TokError("Expected ordering on atomic instruction");
1632 case lltok::kw_unordered: Ordering = Unordered; break;
1633 case lltok::kw_monotonic: Ordering = Monotonic; break;
1634 case lltok::kw_acquire: Ordering = Acquire; break;
1635 case lltok::kw_release: Ordering = Release; break;
1636 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1637 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1638 }
1639 Lex.Lex();
1640 return false;
1641}
1642
Charles Davisbe5557e2010-02-12 00:31:15 +00001643/// ParseOptionalStackAlignment
1644/// ::= /* empty */
1645/// ::= 'alignstack' '(' 4 ')'
1646bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1647 Alignment = 0;
1648 if (!EatIfPresent(lltok::kw_alignstack))
1649 return false;
1650 LocTy ParenLoc = Lex.getLoc();
1651 if (!EatIfPresent(lltok::lparen))
1652 return Error(ParenLoc, "expected '('");
1653 LocTy AlignLoc = Lex.getLoc();
1654 if (ParseUInt32(Alignment)) return true;
1655 ParenLoc = Lex.getLoc();
1656 if (!EatIfPresent(lltok::rparen))
1657 return Error(ParenLoc, "expected ')'");
1658 if (!isPowerOf2_32(Alignment))
1659 return Error(AlignLoc, "stack alignment is not a power of two");
1660 return false;
1661}
Devang Patelea8a4b92009-09-17 23:04:48 +00001662
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001663/// ParseIndexList - This parses the index list for an insert/extractvalue
1664/// instruction. This sets AteExtraComma in the case where we eat an extra
1665/// comma at the end of the line and find that it is followed by metadata.
1666/// Clients that don't allow metadata can call the version of this function that
1667/// only takes one argument.
1668///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001669/// ParseIndexList
1670/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001671///
1672bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1673 bool &AteExtraComma) {
1674 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001675
Chris Lattnerac161bf2009-01-02 07:01:27 +00001676 if (Lex.getKind() != lltok::comma)
1677 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001678
Chris Lattner3822f632009-01-02 08:05:26 +00001679 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001680 if (Lex.getKind() == lltok::MetadataVar) {
David Majnemer7ccc34d2015-02-16 09:18:13 +00001681 if (Indices.empty()) return TokError("expected index");
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001682 AteExtraComma = true;
1683 return false;
1684 }
Nick Lewycky83e47112010-09-29 23:32:20 +00001685 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001686 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001687 Indices.push_back(Idx);
1688 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001689
Chris Lattnerac161bf2009-01-02 07:01:27 +00001690 return false;
1691}
1692
1693//===----------------------------------------------------------------------===//
1694// Type Parsing.
1695//===----------------------------------------------------------------------===//
1696
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001697/// ParseType - Parse a type.
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00001698bool LLParser::ParseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001699 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001700 switch (Lex.getKind()) {
1701 default:
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00001702 return TokError(Msg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001703 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001704 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00001705 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001706 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001707 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001708 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001709 // Type ::= StructType
1710 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001711 return true;
1712 break;
1713 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001714 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001715 Lex.Lex(); // eat the lsquare.
1716 if (ParseArrayVectorType(Result, false))
1717 return true;
1718 break;
1719 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001720 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00001721 Lex.Lex();
1722 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001723 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00001724 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001725 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001726 } else if (ParseArrayVectorType(Result, true))
1727 return true;
1728 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001729 case lltok::LocalVar: {
1730 // Type ::= %foo
1731 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001732
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001733 // If the type hasn't been defined yet, create a forward definition and
1734 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001735 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001736 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001737 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001738 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001739 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001740 Lex.Lex();
1741 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001742 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001743
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001744 case lltok::LocalVarID: {
1745 // Type ::= %4
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001746 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001747
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001748 // If the type hasn't been defined yet, create a forward definition and
1749 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001750 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001751 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001752 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001753 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001754 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001755 Lex.Lex();
1756 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001757 }
1758 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001759
1760 // Parse the type suffixes.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001761 while (1) {
1762 switch (Lex.getKind()) {
1763 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001764 default:
1765 if (!AllowVoid && Result->isVoidTy())
1766 return Error(TypeLoc, "void type only allowed for function results");
1767 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001768
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001769 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001770 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001771 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001772 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001773 if (Result->isVoidTy())
1774 return TokError("pointers to void are invalid - use i8* instead");
1775 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001776 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001777 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001778 Lex.Lex();
1779 break;
1780
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001781 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001782 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001783 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001784 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001785 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00001786 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001787 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001788 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001789 unsigned AddrSpace;
1790 if (ParseOptionalAddrSpace(AddrSpace) ||
1791 ParseToken(lltok::star, "expected '*' in address space"))
1792 return true;
1793
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001794 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001795 break;
1796 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001797
Chris Lattnerac161bf2009-01-02 07:01:27 +00001798 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1799 case lltok::lparen:
1800 if (ParseFunctionType(Result))
1801 return true;
1802 break;
1803 }
1804 }
1805}
1806
1807/// ParseParameterList
1808/// ::= '(' ')'
1809/// ::= '(' Arg (',' Arg)* ')'
1810/// Arg
1811/// ::= Type OptionalAttributes Value OptionalAttributes
1812bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
Reid Kleckner83498642014-08-26 00:33:28 +00001813 PerFunctionState &PFS, bool IsMustTailCall,
1814 bool InVarArgsFunc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001815 if (ParseToken(lltok::lparen, "expected '(' in call"))
1816 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001817
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001818 unsigned AttrIndex = 1;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001819 while (Lex.getKind() != lltok::rparen) {
1820 // If this isn't the first argument, we need a comma.
1821 if (!ArgList.empty() &&
1822 ParseToken(lltok::comma, "expected ',' in argument list"))
1823 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001824
Reid Kleckner83498642014-08-26 00:33:28 +00001825 // Parse an ellipsis if this is a musttail call in a variadic function.
1826 if (Lex.getKind() == lltok::dotdotdot) {
1827 const char *Msg = "unexpected ellipsis in argument list for ";
1828 if (!IsMustTailCall)
1829 return TokError(Twine(Msg) + "non-musttail call");
1830 if (!InVarArgsFunc)
1831 return TokError(Twine(Msg) + "musttail call in non-varargs function");
1832 Lex.Lex(); // Lex the '...', it is purely for readability.
1833 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
1834 }
1835
Chris Lattnerac161bf2009-01-02 07:01:27 +00001836 // Parse the argument.
1837 LocTy ArgLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00001838 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001839 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001840 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00001841 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001842 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00001843
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00001844 if (ArgTy->isMetadataTy()) {
1845 if (ParseMetadataAsValue(V, PFS))
1846 return true;
1847 } else {
1848 // Otherwise, handle normal operands.
1849 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
1850 return true;
1851 }
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001852 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1853 AttrIndex++,
1854 ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001855 }
1856
Reid Kleckner83498642014-08-26 00:33:28 +00001857 if (IsMustTailCall && InVarArgsFunc)
1858 return TokError("expected '...' at end of argument list for musttail call "
1859 "in varargs function");
1860
Chris Lattnerac161bf2009-01-02 07:01:27 +00001861 Lex.Lex(); // Lex the ')'.
1862 return false;
1863}
1864
1865
1866
Chris Lattner2ed06b42009-01-05 18:34:07 +00001867/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001868/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001869/// ::= '(' ArgTypeListI ')'
1870/// ArgTypeListI
1871/// ::= /*empty*/
1872/// ::= '...'
1873/// ::= ArgTypeList ',' '...'
1874/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00001875///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001876bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1877 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00001878 isVarArg = false;
1879 assert(Lex.getKind() == lltok::lparen);
1880 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001881
Chris Lattnerac161bf2009-01-02 07:01:27 +00001882 if (Lex.getKind() == lltok::rparen) {
1883 // empty
1884 } else if (Lex.getKind() == lltok::dotdotdot) {
1885 isVarArg = true;
1886 Lex.Lex();
1887 } else {
1888 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001889 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001890 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001891 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001892
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001893 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00001894 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001895
Chris Lattnerfdd87902009-10-05 05:54:46 +00001896 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001897 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001898
Chris Lattnerdef19492011-06-17 06:36:20 +00001899 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001900 Name = Lex.getStrVal();
1901 Lex.Lex();
1902 }
Chris Lattner3822f632009-01-02 08:05:26 +00001903
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001904 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00001905 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001906
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001907 unsigned AttrIndex = 1;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001908 ArgList.emplace_back(TypeLoc, ArgTy, AttributeSet::get(ArgTy->getContext(),
1909 AttrIndex++, Attrs),
1910 std::move(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001911
Chris Lattner3822f632009-01-02 08:05:26 +00001912 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001913 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00001914 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001915 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001916 break;
1917 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001918
Chris Lattnerac161bf2009-01-02 07:01:27 +00001919 // Otherwise must be an argument type.
1920 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00001921 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00001922
Chris Lattnerfdd87902009-10-05 05:54:46 +00001923 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001924 return Error(TypeLoc, "argument can not have void type");
1925
Chris Lattnerdef19492011-06-17 06:36:20 +00001926 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001927 Name = Lex.getStrVal();
1928 Lex.Lex();
1929 } else {
1930 Name = "";
1931 }
Chris Lattner3822f632009-01-02 08:05:26 +00001932
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001933 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00001934 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001935
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001936 ArgList.emplace_back(
1937 TypeLoc, ArgTy,
1938 AttributeSet::get(ArgTy->getContext(), AttrIndex++, Attrs),
1939 std::move(Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001940 }
1941 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001942
Chris Lattner3822f632009-01-02 08:05:26 +00001943 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001944}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001945
Chris Lattnerac161bf2009-01-02 07:01:27 +00001946/// ParseFunctionType
1947/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001948bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001949 assert(Lex.getKind() == lltok::lparen);
1950
Chris Lattnerce473c72009-01-05 08:04:33 +00001951 if (!FunctionType::isValidReturnType(Result))
1952 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001953
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001954 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001955 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001956 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001957 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001958
Chris Lattnerac161bf2009-01-02 07:01:27 +00001959 // Reject names on the arguments lists.
1960 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1961 if (!ArgList[i].Name.empty())
1962 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001963 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattner6bc5c892011-06-17 17:37:13 +00001964 return Error(ArgList[i].Loc,
1965 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001966 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001967
Jay Foadb804a2b2011-07-12 14:06:48 +00001968 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001969 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001970 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001971
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001972 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001973 return false;
1974}
1975
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001976/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1977/// other structs.
1978bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1979 SmallVector<Type*, 8> Elts;
1980 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001981
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001982 Result = StructType::get(Context, Elts, Packed);
1983 return false;
1984}
1985
1986/// ParseStructDefinition - Parse a struct in a 'type' definition.
1987bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1988 std::pair<Type*, LocTy> &Entry,
1989 Type *&ResultTy) {
1990 // If the type was already defined, diagnose the redefinition.
1991 if (Entry.first && !Entry.second.isValid())
1992 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001993
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001994 // If we have opaque, just return without filling in the definition for the
1995 // struct. This counts as a definition as far as the .ll file goes.
1996 if (EatIfPresent(lltok::kw_opaque)) {
1997 // This type is being defined, so clear the location to indicate this.
1998 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001999
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002000 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00002001 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00002002 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002003 ResultTy = Entry.first;
2004 return false;
2005 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002006
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002007 // If the type starts with '<', then it is either a packed struct or a vector.
2008 bool isPacked = EatIfPresent(lltok::less);
2009
2010 // If we don't have a struct, then we have a random type alias, which we
2011 // accept for compatibility with old files. These types are not allowed to be
2012 // forward referenced and not allowed to be recursive.
2013 if (Lex.getKind() != lltok::lbrace) {
2014 if (Entry.first)
2015 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002016
Craig Topper2617dcc2014-04-15 06:32:26 +00002017 ResultTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002018 if (isPacked)
2019 return ParseArrayVectorType(ResultTy, true);
2020 return ParseType(ResultTy);
2021 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002022
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002023 // This type is being defined, so clear the location to indicate this.
2024 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002025
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002026 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00002027 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00002028 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002029
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002030 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002031
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002032 SmallVector<Type*, 8> Body;
2033 if (ParseStructBody(Body) ||
2034 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
2035 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002036
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002037 STy->setBody(Body, isPacked);
2038 ResultTy = STy;
2039 return false;
2040}
2041
2042
Chris Lattnerac161bf2009-01-02 07:01:27 +00002043/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002044/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00002045/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002046/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00002047/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002048/// ::= '<' '{' Type (',' Type)* '}' '>'
2049bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002050 assert(Lex.getKind() == lltok::lbrace);
2051 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002052
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002053 // Handle the empty struct.
2054 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002055 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002056
Chris Lattnerf880ca22009-03-09 04:49:14 +00002057 LocTy EltTyLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002058 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002059 if (ParseType(Ty)) return true;
2060 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002061
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002062 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002063 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002064
Chris Lattner3822f632009-01-02 08:05:26 +00002065 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00002066 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002067 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002068
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002069 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002070 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002071
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002072 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002073 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002074
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002075 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002076}
2077
2078/// ParseArrayVectorType - Parse an array or vector type, assuming the first
2079/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002080/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00002081/// ::= '[' APSINTVAL 'x' Types ']'
2082/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002083bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002084 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
2085 Lex.getAPSIntVal().getBitWidth() > 64)
2086 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002087
Chris Lattnerac161bf2009-01-02 07:01:27 +00002088 LocTy SizeLoc = Lex.getLoc();
2089 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00002090 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002091
Chris Lattner3822f632009-01-02 08:05:26 +00002092 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
2093 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002094
2095 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002096 Type *EltTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002097 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00002098
Chris Lattner3822f632009-01-02 08:05:26 +00002099 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
2100 "expected end of sequential type"))
2101 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002102
Chris Lattnerac161bf2009-01-02 07:01:27 +00002103 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00002104 if (Size == 0)
2105 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002106 if ((unsigned)Size != Size)
2107 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002108 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00002109 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00002110 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002111 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002112 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002113 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002114 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002115 }
2116 return false;
2117}
2118
2119//===----------------------------------------------------------------------===//
2120// Function Semantic Analysis.
2121//===----------------------------------------------------------------------===//
2122
Chris Lattner3432c622009-10-28 03:39:23 +00002123LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2124 int functionNumber)
2125 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002126
2127 // Insert unnamed arguments into the NumberedVals list.
2128 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2129 AI != E; ++AI)
2130 if (!AI->hasName())
2131 NumberedVals.push_back(AI);
2132}
2133
2134LLParser::PerFunctionState::~PerFunctionState() {
2135 // If there were any forward referenced non-basicblock values, delete them.
2136 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
2137 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
2138 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002139 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002140 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002141 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002142 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002143 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002144
Chris Lattnerac161bf2009-01-02 07:01:27 +00002145 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2146 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
2147 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002148 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002149 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002150 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002151 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002152 }
2153}
2154
Chris Lattner3432c622009-10-28 03:39:23 +00002155bool LLParser::PerFunctionState::FinishFunction() {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002156 if (!ForwardRefVals.empty())
2157 return P.Error(ForwardRefVals.begin()->second.second,
2158 "use of undefined value '%" + ForwardRefVals.begin()->first +
2159 "'");
2160 if (!ForwardRefValIDs.empty())
2161 return P.Error(ForwardRefValIDs.begin()->second.second,
2162 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002163 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002164 return false;
2165}
2166
2167
2168/// GetVal - Get a value with the specified name or ID, creating a
2169/// forward reference record if needed. This can return null if the value
2170/// exists but does not have the right type.
2171Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattner229907c2011-07-18 04:54:35 +00002172 Type *Ty, LocTy Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002173 // Look this name up in the normal function symbol table.
2174 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002175
Chris Lattnerac161bf2009-01-02 07:01:27 +00002176 // If this is a forward reference for the value, see if we already created a
2177 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002178 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002179 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2180 I = ForwardRefVals.find(Name);
2181 if (I != ForwardRefVals.end())
2182 Val = I->second.first;
2183 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002184
Chris Lattnerac161bf2009-01-02 07:01:27 +00002185 // If we have the value in the symbol table or fwd-ref table, return it.
2186 if (Val) {
2187 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002188 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002189 P.Error(Loc, "'%" + Name + "' is not a basic block");
2190 else
2191 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002192 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002193 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002194 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002195
Chris Lattnerac161bf2009-01-02 07:01:27 +00002196 // Don't make placeholders with invalid type.
Duncan P. N. Exon Smith6a6e9cb2014-08-05 18:22:58 +00002197 if (!Ty->isFirstClassType()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002198 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002199 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002200 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002201
Chris Lattnerac161bf2009-01-02 07:01:27 +00002202 // Otherwise, create a new forward reference for this value and remember it.
2203 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002204 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002205 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002206 else
2207 FwdVal = new Argument(Ty, Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002208
Chris Lattnerac161bf2009-01-02 07:01:27 +00002209 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2210 return FwdVal;
2211}
2212
Chris Lattner229907c2011-07-18 04:54:35 +00002213Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00002214 LocTy Loc) {
2215 // Look this name up in the normal function symbol table.
Craig Topper2617dcc2014-04-15 06:32:26 +00002216 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002217
Chris Lattnerac161bf2009-01-02 07:01:27 +00002218 // If this is a forward reference for the value, see if we already created a
2219 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002220 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002221 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2222 I = ForwardRefValIDs.find(ID);
2223 if (I != ForwardRefValIDs.end())
2224 Val = I->second.first;
2225 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002226
Chris Lattnerac161bf2009-01-02 07:01:27 +00002227 // If we have the value in the symbol table or fwd-ref table, return it.
2228 if (Val) {
2229 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002230 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002231 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002232 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002233 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002234 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002235 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002236 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002237
Duncan P. N. Exon Smith6a6e9cb2014-08-05 18:22:58 +00002238 if (!Ty->isFirstClassType()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002239 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002240 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002241 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002242
Chris Lattnerac161bf2009-01-02 07:01:27 +00002243 // Otherwise, create a new forward reference for this value and remember it.
2244 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002245 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002246 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002247 else
2248 FwdVal = new Argument(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002249
Chris Lattnerac161bf2009-01-02 07:01:27 +00002250 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2251 return FwdVal;
2252}
2253
2254/// SetInstName - After an instruction is parsed and inserted into its
2255/// basic block, this installs its name.
2256bool LLParser::PerFunctionState::SetInstName(int NameID,
2257 const std::string &NameStr,
2258 LocTy NameLoc, Instruction *Inst) {
2259 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002260 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002261 if (NameID != -1 || !NameStr.empty())
2262 return P.Error(NameLoc, "instructions returning void cannot have a name");
2263 return false;
2264 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002265
Chris Lattnerac161bf2009-01-02 07:01:27 +00002266 // If this was a numbered instruction, verify that the instruction is the
2267 // expected value and resolve any forward references.
2268 if (NameStr.empty()) {
2269 // If neither a name nor an ID was specified, just use the next ID.
2270 if (NameID == -1)
2271 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002272
Chris Lattnerac161bf2009-01-02 07:01:27 +00002273 if (unsigned(NameID) != NumberedVals.size())
2274 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002275 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002276
Chris Lattnerac161bf2009-01-02 07:01:27 +00002277 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2278 ForwardRefValIDs.find(NameID);
2279 if (FI != ForwardRefValIDs.end()) {
2280 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002281 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002282 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002283 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2baa6a32009-09-02 15:02:57 +00002284 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002285 ForwardRefValIDs.erase(FI);
2286 }
2287
2288 NumberedVals.push_back(Inst);
2289 return false;
2290 }
2291
2292 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2293 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2294 FI = ForwardRefVals.find(NameStr);
2295 if (FI != ForwardRefVals.end()) {
2296 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002297 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002298 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002299 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2fcee702009-09-02 14:22:03 +00002300 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002301 ForwardRefVals.erase(FI);
2302 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002303
Chris Lattnerac161bf2009-01-02 07:01:27 +00002304 // Set the name on the instruction.
2305 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002306
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002307 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002308 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002309 NameStr + "'");
2310 return false;
2311}
2312
2313/// GetBB - Get a basic block with the specified name or ID, creating a
2314/// forward reference record if needed.
2315BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2316 LocTy Loc) {
Owen Anderson576a9a22015-03-02 05:25:09 +00002317 return dyn_cast_or_null<BasicBlock>(GetVal(Name,
2318 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002319}
2320
2321BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson576a9a22015-03-02 05:25:09 +00002322 return dyn_cast_or_null<BasicBlock>(GetVal(ID,
2323 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002324}
2325
2326/// DefineBB - Define the specified basic block, which is either named or
2327/// unnamed. If there is an error, this returns null otherwise it returns
2328/// the block being defined.
2329BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2330 LocTy Loc) {
2331 BasicBlock *BB;
2332 if (Name.empty())
2333 BB = GetBB(NumberedVals.size(), Loc);
2334 else
2335 BB = GetBB(Name, Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002336 if (!BB) return nullptr; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002337
Chris Lattnerac161bf2009-01-02 07:01:27 +00002338 // Move the block to the end of the function. Forward ref'd blocks are
2339 // inserted wherever they happen to be referenced.
2340 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002341
Chris Lattnerac161bf2009-01-02 07:01:27 +00002342 // Remove the block from forward ref sets.
2343 if (Name.empty()) {
2344 ForwardRefValIDs.erase(NumberedVals.size());
2345 NumberedVals.push_back(BB);
2346 } else {
2347 // BB forward references are already in the function symbol table.
2348 ForwardRefVals.erase(Name);
2349 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002350
Chris Lattnerac161bf2009-01-02 07:01:27 +00002351 return BB;
2352}
2353
2354//===----------------------------------------------------------------------===//
2355// Constants.
2356//===----------------------------------------------------------------------===//
2357
2358/// ParseValID - Parse an abstract value that doesn't necessarily have a
2359/// type implied. For example, if we parse "4" we don't know what integer type
2360/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002361/// sanity. PFS is used to convert function-local operands of metadata (since
2362/// metadata operands are not just parsed here but also converted to values).
2363/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002364bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002365 ID.Loc = Lex.getLoc();
2366 switch (Lex.getKind()) {
2367 default: return TokError("expected value token");
2368 case lltok::GlobalID: // @42
2369 ID.UIntVal = Lex.getUIntVal();
2370 ID.Kind = ValID::t_GlobalID;
2371 break;
2372 case lltok::GlobalVar: // @foo
2373 ID.StrVal = Lex.getStrVal();
2374 ID.Kind = ValID::t_GlobalName;
2375 break;
2376 case lltok::LocalVarID: // %42
2377 ID.UIntVal = Lex.getUIntVal();
2378 ID.Kind = ValID::t_LocalID;
2379 break;
2380 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002381 ID.StrVal = Lex.getStrVal();
2382 ID.Kind = ValID::t_LocalName;
2383 break;
2384 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002385 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002386 ID.Kind = ValID::t_APSInt;
2387 break;
2388 case lltok::APFloat:
2389 ID.APFloatVal = Lex.getAPFloatVal();
2390 ID.Kind = ValID::t_APFloat;
2391 break;
2392 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002393 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002394 ID.Kind = ValID::t_Constant;
2395 break;
2396 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002397 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002398 ID.Kind = ValID::t_Constant;
2399 break;
2400 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2401 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2402 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002403
Chris Lattnerac161bf2009-01-02 07:01:27 +00002404 case lltok::lbrace: {
2405 // ValID ::= '{' ConstVector '}'
2406 Lex.Lex();
2407 SmallVector<Constant*, 16> Elts;
2408 if (ParseGlobalValueVector(Elts) ||
2409 ParseToken(lltok::rbrace, "expected end of struct constant"))
2410 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002411
Reid Kleckner2ae03e12015-03-04 18:31:10 +00002412 ID.ConstantStructElts = new Constant*[Elts.size()];
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002413 ID.UIntVal = Elts.size();
Reid Kleckner2ae03e12015-03-04 18:31:10 +00002414 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002415 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002416 return false;
2417 }
2418 case lltok::less: {
2419 // ValID ::= '<' ConstVector '>' --> Vector.
2420 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2421 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002422 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002423
Chris Lattnerac161bf2009-01-02 07:01:27 +00002424 SmallVector<Constant*, 16> Elts;
2425 LocTy FirstEltLoc = Lex.getLoc();
2426 if (ParseGlobalValueVector(Elts) ||
2427 (isPackedStruct &&
2428 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2429 ParseToken(lltok::greater, "expected end of constant"))
2430 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002431
Chris Lattnerac161bf2009-01-02 07:01:27 +00002432 if (isPackedStruct) {
Reid Kleckner2ae03e12015-03-04 18:31:10 +00002433 ID.ConstantStructElts = new Constant*[Elts.size()];
2434 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002435 ID.UIntVal = Elts.size();
2436 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002437 return false;
2438 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002439
Chris Lattnerac161bf2009-01-02 07:01:27 +00002440 if (Elts.empty())
2441 return Error(ID.Loc, "constant vector must not be empty");
2442
Duncan Sands9dff9be2010-02-15 16:12:20 +00002443 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002444 !Elts[0]->getType()->isFloatingPointTy() &&
2445 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002446 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002447 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002448
Chris Lattnerac161bf2009-01-02 07:01:27 +00002449 // Verify that all the vector elements have the same type.
2450 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2451 if (Elts[i]->getType() != Elts[0]->getType())
2452 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002453 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002454 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002455
Chris Lattner69229312011-02-15 00:14:00 +00002456 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002457 ID.Kind = ValID::t_Constant;
2458 return false;
2459 }
2460 case lltok::lsquare: { // Array Constant
2461 Lex.Lex();
2462 SmallVector<Constant*, 16> Elts;
2463 LocTy FirstEltLoc = Lex.getLoc();
2464 if (ParseGlobalValueVector(Elts) ||
2465 ParseToken(lltok::rsquare, "expected end of array constant"))
2466 return true;
2467
2468 // Handle empty element.
2469 if (Elts.empty()) {
2470 // Use undef instead of an array because it's inconvenient to determine
2471 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002472 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002473 return false;
2474 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002475
Chris Lattnerac161bf2009-01-02 07:01:27 +00002476 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002477 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002478 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002479
Owen Anderson4056ca92009-07-29 22:17:13 +00002480 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002481
Chris Lattnerac161bf2009-01-02 07:01:27 +00002482 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002483 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002484 if (Elts[i]->getType() != Elts[0]->getType())
2485 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002486 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002487 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002488 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002489
Jay Foad83be3612011-06-22 09:24:39 +00002490 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002491 ID.Kind = ValID::t_Constant;
2492 return false;
2493 }
2494 case lltok::kw_c: // c "foo"
2495 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002496 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2497 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002498 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2499 ID.Kind = ValID::t_Constant;
2500 return false;
2501
2502 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002503 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2504 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002505 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002506 Lex.Lex();
2507 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002508 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002509 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002510 ParseStringConstant(ID.StrVal) ||
2511 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002512 ParseToken(lltok::StringConstant, "expected constraint string"))
2513 return true;
2514 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002515 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002516 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002517 ID.Kind = ValID::t_InlineAsm;
2518 return false;
2519 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002520
Chris Lattner3432c622009-10-28 03:39:23 +00002521 case lltok::kw_blockaddress: {
2522 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2523 Lex.Lex();
2524
2525 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002526
Chris Lattner3432c622009-10-28 03:39:23 +00002527 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2528 ParseValID(Fn) ||
2529 ParseToken(lltok::comma, "expected comma in block address expression")||
2530 ParseValID(Label) ||
2531 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2532 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002533
Chris Lattner3432c622009-10-28 03:39:23 +00002534 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2535 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00002536 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00002537 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002538
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00002539 // Try to find the function (but skip it if it's forward-referenced).
2540 GlobalValue *GV = nullptr;
2541 if (Fn.Kind == ValID::t_GlobalID) {
2542 if (Fn.UIntVal < NumberedVals.size())
2543 GV = NumberedVals[Fn.UIntVal];
2544 } else if (!ForwardRefVals.count(Fn.StrVal)) {
2545 GV = M->getNamedValue(Fn.StrVal);
2546 }
2547 Function *F = nullptr;
2548 if (GV) {
2549 // Confirm that it's actually a function with a definition.
2550 if (!isa<Function>(GV))
2551 return Error(Fn.Loc, "expected function name in blockaddress");
2552 F = cast<Function>(GV);
2553 if (F->isDeclaration())
2554 return Error(Fn.Loc, "cannot take blockaddress inside a declaration");
2555 }
2556
2557 if (!F) {
2558 // Make a global variable as a placeholder for this reference.
David Blaikieb9cc6592015-03-04 01:40:07 +00002559 GlobalValue *&FwdRef =
2560 ForwardRefBlockAddresses.insert(std::make_pair(
2561 std::move(Fn),
2562 std::map<ValID, GlobalValue *>()))
2563 .first->second.insert(std::make_pair(std::move(Label), nullptr))
2564 .first->second;
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00002565 if (!FwdRef)
2566 FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), false,
2567 GlobalValue::InternalLinkage, nullptr, "");
2568 ID.ConstantVal = FwdRef;
2569 ID.Kind = ValID::t_Constant;
2570 return false;
2571 }
2572
2573 // We found the function; now find the basic block. Don't use PFS, since we
2574 // might be inside a constant expression.
2575 BasicBlock *BB;
2576 if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) {
2577 if (Label.Kind == ValID::t_LocalID)
2578 BB = BlockAddressPFS->GetBB(Label.UIntVal, Label.Loc);
2579 else
2580 BB = BlockAddressPFS->GetBB(Label.StrVal, Label.Loc);
2581 if (!BB)
2582 return Error(Label.Loc, "referenced value is not a basic block");
2583 } else {
2584 if (Label.Kind == ValID::t_LocalID)
2585 return Error(Label.Loc, "cannot take address of numeric label after "
2586 "the function is defined");
2587 BB = dyn_cast_or_null<BasicBlock>(
2588 F->getValueSymbolTable().lookup(Label.StrVal));
2589 if (!BB)
2590 return Error(Label.Loc, "referenced value is not a basic block");
2591 }
2592
2593 ID.ConstantVal = BlockAddress::get(F, BB);
Chris Lattner3432c622009-10-28 03:39:23 +00002594 ID.Kind = ValID::t_Constant;
2595 return false;
2596 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002597
Chris Lattnerac161bf2009-01-02 07:01:27 +00002598 case lltok::kw_trunc:
2599 case lltok::kw_zext:
2600 case lltok::kw_sext:
2601 case lltok::kw_fptrunc:
2602 case lltok::kw_fpext:
2603 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002604 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002605 case lltok::kw_uitofp:
2606 case lltok::kw_sitofp:
2607 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002608 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002609 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002610 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002611 unsigned Opc = Lex.getUIntVal();
Craig Topper2617dcc2014-04-15 06:32:26 +00002612 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002613 Constant *SrcVal;
2614 Lex.Lex();
2615 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2616 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00002617 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002618 ParseType(DestTy) ||
2619 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2620 return true;
2621 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2622 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002623 getTypeString(SrcVal->getType()) + "' to '" +
2624 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002625 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00002626 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002627 ID.Kind = ValID::t_Constant;
2628 return false;
2629 }
2630 case lltok::kw_extractvalue: {
2631 Lex.Lex();
2632 Constant *Val;
2633 SmallVector<unsigned, 4> Indices;
2634 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2635 ParseGlobalTypeAndValue(Val) ||
2636 ParseIndexList(Indices) ||
2637 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2638 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00002639
Chris Lattner392be582010-02-12 20:49:41 +00002640 if (!Val->getType()->isAggregateType())
2641 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002642 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002643 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002644 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002645 ID.Kind = ValID::t_Constant;
2646 return false;
2647 }
2648 case lltok::kw_insertvalue: {
2649 Lex.Lex();
2650 Constant *Val0, *Val1;
2651 SmallVector<unsigned, 4> Indices;
2652 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2653 ParseGlobalTypeAndValue(Val0) ||
2654 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2655 ParseGlobalTypeAndValue(Val1) ||
2656 ParseIndexList(Indices) ||
2657 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2658 return true;
Chris Lattner392be582010-02-12 20:49:41 +00002659 if (!Val0->getType()->isAggregateType())
2660 return Error(ID.Loc, "insertvalue operand must be aggregate type");
David Majnemereba692d2015-02-23 07:13:52 +00002661 Type *IndexedType =
2662 ExtractValueInst::getIndexedType(Val0->getType(), Indices);
2663 if (!IndexedType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00002664 return Error(ID.Loc, "invalid indices for insertvalue");
David Majnemereba692d2015-02-23 07:13:52 +00002665 if (IndexedType != Val1->getType())
2666 return Error(ID.Loc, "insertvalue operand and field disagree in type: '" +
2667 getTypeString(Val1->getType()) +
2668 "' instead of '" + getTypeString(IndexedType) +
2669 "'");
Jay Foad57aa6362011-07-13 10:26:04 +00002670 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002671 ID.Kind = ValID::t_Constant;
2672 return false;
2673 }
2674 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002675 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002676 unsigned PredVal, Opc = Lex.getUIntVal();
2677 Constant *Val0, *Val1;
2678 Lex.Lex();
2679 if (ParseCmpPredicate(PredVal, Opc) ||
2680 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2681 ParseGlobalTypeAndValue(Val0) ||
2682 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2683 ParseGlobalTypeAndValue(Val1) ||
2684 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2685 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002686
Chris Lattnerac161bf2009-01-02 07:01:27 +00002687 if (Val0->getType() != Val1->getType())
2688 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002689
Chris Lattnerac161bf2009-01-02 07:01:27 +00002690 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002691
Chris Lattnerac161bf2009-01-02 07:01:27 +00002692 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002693 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002694 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002695 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002696 } else {
2697 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002698 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002699 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002700 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002701 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002702 }
2703 ID.Kind = ValID::t_Constant;
2704 return false;
2705 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002706
Chris Lattnerac161bf2009-01-02 07:01:27 +00002707 // Binary Operators.
2708 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00002709 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002710 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002711 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002712 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00002713 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002714 case lltok::kw_udiv:
2715 case lltok::kw_sdiv:
2716 case lltok::kw_fdiv:
2717 case lltok::kw_urem:
2718 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002719 case lltok::kw_frem:
2720 case lltok::kw_shl:
2721 case lltok::kw_lshr:
2722 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002723 bool NUW = false;
2724 bool NSW = false;
2725 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002726 unsigned Opc = Lex.getUIntVal();
2727 Constant *Val0, *Val1;
2728 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00002729 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00002730 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2731 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002732 if (EatIfPresent(lltok::kw_nuw))
2733 NUW = true;
2734 if (EatIfPresent(lltok::kw_nsw)) {
2735 NSW = true;
2736 if (EatIfPresent(lltok::kw_nuw))
2737 NUW = true;
2738 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00002739 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2740 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002741 if (EatIfPresent(lltok::kw_exact))
2742 Exact = true;
2743 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002744 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2745 ParseGlobalTypeAndValue(Val0) ||
2746 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2747 ParseGlobalTypeAndValue(Val1) ||
2748 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2749 return true;
2750 if (Val0->getType() != Val1->getType())
2751 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002752 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002753 if (NUW)
2754 return Error(ModifierLoc, "nuw only applies to integer operations");
2755 if (NSW)
2756 return Error(ModifierLoc, "nsw only applies to integer operations");
2757 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00002758 // Check that the type is valid for the operator.
2759 switch (Opc) {
2760 case Instruction::Add:
2761 case Instruction::Sub:
2762 case Instruction::Mul:
2763 case Instruction::UDiv:
2764 case Instruction::SDiv:
2765 case Instruction::URem:
2766 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002767 case Instruction::Shl:
2768 case Instruction::AShr:
2769 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00002770 if (!Val0->getType()->isIntOrIntVectorTy())
2771 return Error(ID.Loc, "constexpr requires integer operands");
2772 break;
2773 case Instruction::FAdd:
2774 case Instruction::FSub:
2775 case Instruction::FMul:
2776 case Instruction::FDiv:
2777 case Instruction::FRem:
2778 if (!Val0->getType()->isFPOrFPVectorTy())
2779 return Error(ID.Loc, "constexpr requires fp operands");
2780 break;
2781 default: llvm_unreachable("Unknown binary operator!");
2782 }
Dan Gohman1b849082009-09-07 23:54:19 +00002783 unsigned Flags = 0;
2784 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2785 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002786 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00002787 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00002788 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002789 ID.Kind = ValID::t_Constant;
2790 return false;
2791 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002792
Chris Lattnerac161bf2009-01-02 07:01:27 +00002793 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00002794 case lltok::kw_and:
2795 case lltok::kw_or:
2796 case lltok::kw_xor: {
2797 unsigned Opc = Lex.getUIntVal();
2798 Constant *Val0, *Val1;
2799 Lex.Lex();
2800 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2801 ParseGlobalTypeAndValue(Val0) ||
2802 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2803 ParseGlobalTypeAndValue(Val1) ||
2804 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2805 return true;
2806 if (Val0->getType() != Val1->getType())
2807 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002808 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002809 return Error(ID.Loc,
2810 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002811 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002812 ID.Kind = ValID::t_Constant;
2813 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002814 }
2815
Chris Lattnerac161bf2009-01-02 07:01:27 +00002816 case lltok::kw_getelementptr:
2817 case lltok::kw_shufflevector:
2818 case lltok::kw_insertelement:
2819 case lltok::kw_extractelement:
2820 case lltok::kw_select: {
2821 unsigned Opc = Lex.getUIntVal();
2822 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00002823 bool InBounds = false;
David Blaikief72d05b2015-03-13 18:20:45 +00002824 Type *Ty;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002825 Lex.Lex();
David Blaikief72d05b2015-03-13 18:20:45 +00002826
Dan Gohman1639c392009-07-27 21:53:46 +00002827 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00002828 InBounds = EatIfPresent(lltok::kw_inbounds);
David Blaikief72d05b2015-03-13 18:20:45 +00002829
2830 if (ParseToken(lltok::lparen, "expected '(' in constantexpr"))
2831 return true;
2832
2833 LocTy ExplicitTypeLoc = Lex.getLoc();
2834 if (Opc == Instruction::GetElementPtr) {
2835 if (ParseType(Ty) ||
2836 ParseToken(lltok::comma, "expected comma after getelementptr's type"))
2837 return true;
2838 }
2839
2840 if (ParseGlobalValueVector(Elts) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002841 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2842 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002843
Chris Lattnerac161bf2009-01-02 07:01:27 +00002844 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00002845 if (Elts.size() == 0 ||
2846 !Elts[0]->getType()->getScalarType()->isPointerTy())
David Majnemer00303b62015-02-22 23:14:52 +00002847 return Error(ID.Loc, "base of getelementptr must be a pointer");
2848
2849 Type *BaseType = Elts[0]->getType();
2850 auto *BasePointerType = cast<PointerType>(BaseType->getScalarType());
David Blaikief72d05b2015-03-13 18:20:45 +00002851 if (Ty != BasePointerType->getElementType())
2852 return Error(
2853 ExplicitTypeLoc,
2854 "explicit pointee type doesn't match operand's pointee type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002855
Jay Foaded8db7d2011-07-21 14:31:17 +00002856 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
David Majnemer00303b62015-02-22 23:14:52 +00002857 for (Constant *Val : Indices) {
2858 Type *ValTy = Val->getType();
2859 if (!ValTy->getScalarType()->isIntegerTy())
2860 return Error(ID.Loc, "getelementptr index must be an integer");
2861 if (ValTy->isVectorTy() != BaseType->isVectorTy())
2862 return Error(ID.Loc, "getelementptr index type missmatch");
2863 if (ValTy->isVectorTy()) {
2864 unsigned ValNumEl = cast<VectorType>(ValTy)->getNumElements();
2865 unsigned PtrNumEl = cast<VectorType>(BaseType)->getNumElements();
2866 if (ValNumEl != PtrNumEl)
2867 return Error(
2868 ID.Loc,
2869 "getelementptr vector index has a wrong number of elements");
2870 }
2871 }
2872
Owen Andersone90f9922015-03-10 06:34:57 +00002873 SmallPtrSet<const Type*, 4> Visited;
David Blaikiee169e822015-04-22 16:37:35 +00002874 if (!Indices.empty() && !Ty->isSized(&Visited))
David Majnemer00303b62015-02-22 23:14:52 +00002875 return Error(ID.Loc, "base element of getelementptr must be sized");
2876
David Blaikie4a2e73b2015-04-02 18:55:32 +00002877 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
David Majnemer00303b62015-02-22 23:14:52 +00002878 return Error(ID.Loc, "invalid getelementptr indices");
David Blaikie4a2e73b2015-04-02 18:55:32 +00002879 ID.ConstantVal =
2880 ConstantExpr::getGetElementPtr(Ty, Elts[0], Indices, InBounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002881 } else if (Opc == Instruction::Select) {
2882 if (Elts.size() != 3)
2883 return Error(ID.Loc, "expected three operands to select");
2884 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2885 Elts[2]))
2886 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00002887 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002888 } else if (Opc == Instruction::ShuffleVector) {
2889 if (Elts.size() != 3)
2890 return Error(ID.Loc, "expected three operands to shufflevector");
2891 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2892 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00002893 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002894 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002895 } else if (Opc == Instruction::ExtractElement) {
2896 if (Elts.size() != 2)
2897 return Error(ID.Loc, "expected two operands to extractelement");
2898 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2899 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002900 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002901 } else {
2902 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2903 if (Elts.size() != 3)
2904 return Error(ID.Loc, "expected three operands to insertelement");
2905 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2906 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00002907 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002908 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002909 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002910
Chris Lattnerac161bf2009-01-02 07:01:27 +00002911 ID.Kind = ValID::t_Constant;
2912 return false;
2913 }
2914 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002915
Chris Lattnerac161bf2009-01-02 07:01:27 +00002916 Lex.Lex();
2917 return false;
2918}
2919
2920/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00002921bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002922 C = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002923 ValID ID;
Craig Topper2617dcc2014-04-15 06:32:26 +00002924 Value *V = nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002925 bool Parsed = ParseValID(ID) ||
Craig Topper2617dcc2014-04-15 06:32:26 +00002926 ConvertValIDToValue(Ty, ID, V, nullptr);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002927 if (V && !(C = dyn_cast<Constant>(V)))
2928 return Error(ID.Loc, "global values must be constants");
2929 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002930}
2931
Victor Hernandez9d75c962010-01-11 22:31:58 +00002932bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002933 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002934 return ParseType(Ty) ||
2935 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002936}
2937
Rafael Espindola83a362c2015-01-06 22:55:16 +00002938bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) {
David Majnemerdad0a642014-06-27 18:19:56 +00002939 C = nullptr;
Rafael Espindola83a362c2015-01-06 22:55:16 +00002940
2941 LocTy KwLoc = Lex.getLoc();
David Majnemerdad0a642014-06-27 18:19:56 +00002942 if (!EatIfPresent(lltok::kw_comdat))
2943 return false;
Rafael Espindola83a362c2015-01-06 22:55:16 +00002944
2945 if (EatIfPresent(lltok::lparen)) {
2946 if (Lex.getKind() != lltok::ComdatVar)
2947 return TokError("expected comdat variable");
2948 C = getComdat(Lex.getStrVal(), Lex.getLoc());
2949 Lex.Lex();
2950 if (ParseToken(lltok::rparen, "expected ')' after comdat var"))
2951 return true;
2952 } else {
2953 if (GlobalName.empty())
2954 return TokError("comdat cannot be unnamed");
2955 C = getComdat(GlobalName, KwLoc);
2956 }
2957
David Majnemerdad0a642014-06-27 18:19:56 +00002958 return false;
2959}
2960
Victor Hernandez9d75c962010-01-11 22:31:58 +00002961/// ParseGlobalValueVector
2962/// ::= /*empty*/
2963/// ::= TypeAndValue (',' TypeAndValue)*
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00002964bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) {
Victor Hernandez9d75c962010-01-11 22:31:58 +00002965 // Empty list.
2966 if (Lex.getKind() == lltok::rbrace ||
2967 Lex.getKind() == lltok::rsquare ||
2968 Lex.getKind() == lltok::greater ||
2969 Lex.getKind() == lltok::rparen)
2970 return false;
2971
2972 Constant *C;
2973 if (ParseGlobalTypeAndValue(C)) return true;
2974 Elts.push_back(C);
2975
2976 while (EatIfPresent(lltok::comma)) {
2977 if (ParseGlobalTypeAndValue(C)) return true;
2978 Elts.push_back(C);
2979 }
2980
2981 return false;
2982}
2983
Duncan P. N. Exon Smith58ef9d12015-01-12 21:23:11 +00002984bool LLParser::ParseMDTuple(MDNode *&MD, bool IsDistinct) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002985 SmallVector<Metadata *, 16> Elts;
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00002986 if (ParseMDNodeVector(Elts))
Dan Gohmanc828c542010-08-24 02:24:03 +00002987 return true;
2988
Duncan P. N. Exon Smith0b31dd12015-01-12 22:27:39 +00002989 MD = (IsDistinct ? MDTuple::getDistinct : MDTuple::get)(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00002990 return false;
2991}
2992
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00002993/// MDNode:
2994/// ::= !{ ... }
2995/// ::= !7
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002996/// ::= !DILocation(...)
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00002997bool LLParser::ParseMDNode(MDNode *&N) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00002998 if (Lex.getKind() == lltok::MetadataVar)
2999 return ParseSpecializedMDNode(N);
3000
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00003001 return ParseToken(lltok::exclaim, "expected '!' here") ||
3002 ParseMDNodeTail(N);
3003}
3004
3005bool LLParser::ParseMDNodeTail(MDNode *&N) {
3006 // !{ ... }
3007 if (Lex.getKind() == lltok::lbrace)
3008 return ParseMDTuple(N);
3009
3010 // !42
3011 return ParseMDNodeID(N);
3012}
3013
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003014namespace {
3015
3016/// Structure to represent an optional metadata field.
3017template <class FieldTy> struct MDFieldImpl {
3018 typedef MDFieldImpl ImplTy;
3019 FieldTy Val;
3020 bool Seen;
3021
3022 void assign(FieldTy Val) {
3023 Seen = true;
3024 this->Val = std::move(Val);
3025 }
3026
3027 explicit MDFieldImpl(FieldTy Default)
3028 : Val(std::move(Default)), Seen(false) {}
3029};
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003030
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003031struct MDUnsignedField : public MDFieldImpl<uint64_t> {
3032 uint64_t Max;
3033
3034 MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX)
3035 : ImplTy(Default), Max(Max) {}
3036};
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003037struct LineField : public MDUnsignedField {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +00003038 LineField() : MDUnsignedField(0, UINT32_MAX) {}
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003039};
3040struct ColumnField : public MDUnsignedField {
3041 ColumnField() : MDUnsignedField(0, UINT16_MAX) {}
3042};
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003043struct DwarfTagField : public MDUnsignedField {
Duncan P. N. Exon Smitha81f7e12015-02-06 22:29:35 +00003044 DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {}
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00003045 DwarfTagField(dwarf::Tag DefaultTag)
3046 : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {}
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003047};
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003048struct DwarfAttEncodingField : public MDUnsignedField {
3049 DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {}
3050};
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003051struct DwarfVirtualityField : public MDUnsignedField {
3052 DwarfVirtualityField() : MDUnsignedField(0, dwarf::DW_VIRTUALITY_max) {}
3053};
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003054struct DwarfLangField : public MDUnsignedField {
3055 DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {}
3056};
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003057
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003058struct DIFlagField : public MDUnsignedField {
3059 DIFlagField() : MDUnsignedField(0, UINT32_MAX) {}
3060};
3061
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003062struct MDSignedField : public MDFieldImpl<int64_t> {
3063 int64_t Min;
3064 int64_t Max;
3065
3066 MDSignedField(int64_t Default = 0)
3067 : ImplTy(Default), Min(INT64_MIN), Max(INT64_MAX) {}
3068 MDSignedField(int64_t Default, int64_t Min, int64_t Max)
3069 : ImplTy(Default), Min(Min), Max(Max) {}
3070};
3071
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003072struct MDBoolField : public MDFieldImpl<bool> {
3073 MDBoolField(bool Default = false) : ImplTy(Default) {}
3074};
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003075struct MDField : public MDFieldImpl<Metadata *> {
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00003076 bool AllowNull;
3077
3078 MDField(bool AllowNull = true) : ImplTy(nullptr), AllowNull(AllowNull) {}
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003079};
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003080struct MDConstant : public MDFieldImpl<ConstantAsMetadata *> {
3081 MDConstant() : ImplTy(nullptr) {}
3082};
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00003083struct MDStringField : public MDFieldImpl<MDString *> {
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003084 bool AllowEmpty;
3085 MDStringField(bool AllowEmpty = true)
3086 : ImplTy(nullptr), AllowEmpty(AllowEmpty) {}
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003087};
3088struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> {
3089 MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {}
3090};
3091
3092} // end namespace
3093
Duncan P. N. Exon Smith2f09c462015-02-04 22:13:28 +00003094namespace llvm {
3095
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003096template <>
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003097bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003098 MDUnsignedField &Result) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003099 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
3100 return TokError("expected unsigned integer");
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003101
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003102 auto &U = Lex.getAPSIntVal();
3103 if (U.ugt(Result.Max))
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003104 return TokError("value for '" + Name + "' too large, limit is " +
3105 Twine(Result.Max));
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003106 Result.assign(U.getZExtValue());
3107 assert(Result.Val <= Result.Max && "Expected value in range");
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003108 Lex.Lex();
3109 return false;
3110}
3111
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003112template <>
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003113bool LLParser::ParseMDField(LocTy Loc, StringRef Name, LineField &Result) {
3114 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3115}
3116template <>
3117bool LLParser::ParseMDField(LocTy Loc, StringRef Name, ColumnField &Result) {
3118 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3119}
3120
3121template <>
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003122bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) {
3123 if (Lex.getKind() == lltok::APSInt)
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003124 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003125
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003126 if (Lex.getKind() != lltok::DwarfTag)
3127 return TokError("expected DWARF tag");
3128
3129 unsigned Tag = dwarf::getTag(Lex.getStrVal());
3130 if (Tag == dwarf::DW_TAG_invalid)
3131 return TokError("invalid DWARF tag" + Twine(" '") + Lex.getStrVal() + "'");
Duncan P. N. Exon Smithfad631a2015-02-04 22:02:18 +00003132 assert(Tag <= Result.Max && "Expected valid DWARF tag");
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003133
3134 Result.assign(Tag);
3135 Lex.Lex();
3136 return false;
3137}
3138
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003139template <>
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003140bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
3141 DwarfVirtualityField &Result) {
3142 if (Lex.getKind() == lltok::APSInt)
3143 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3144
3145 if (Lex.getKind() != lltok::DwarfVirtuality)
3146 return TokError("expected DWARF virtuality code");
3147
3148 unsigned Virtuality = dwarf::getVirtuality(Lex.getStrVal());
3149 if (!Virtuality)
3150 return TokError("invalid DWARF virtuality code" + Twine(" '") +
3151 Lex.getStrVal() + "'");
3152 assert(Virtuality <= Result.Max && "Expected valid DWARF virtuality code");
3153 Result.assign(Virtuality);
3154 Lex.Lex();
3155 return false;
3156}
3157
3158template <>
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003159bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) {
3160 if (Lex.getKind() == lltok::APSInt)
3161 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3162
3163 if (Lex.getKind() != lltok::DwarfLang)
3164 return TokError("expected DWARF language");
3165
3166 unsigned Lang = dwarf::getLanguage(Lex.getStrVal());
3167 if (!Lang)
3168 return TokError("invalid DWARF language" + Twine(" '") + Lex.getStrVal() +
3169 "'");
3170 assert(Lang <= Result.Max && "Expected valid DWARF language");
3171 Result.assign(Lang);
3172 Lex.Lex();
3173 return false;
3174}
3175
3176template <>
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003177bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003178 DwarfAttEncodingField &Result) {
3179 if (Lex.getKind() == lltok::APSInt)
3180 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3181
3182 if (Lex.getKind() != lltok::DwarfAttEncoding)
3183 return TokError("expected DWARF type attribute encoding");
3184
3185 unsigned Encoding = dwarf::getAttributeEncoding(Lex.getStrVal());
3186 if (!Encoding)
3187 return TokError("invalid DWARF type attribute encoding" + Twine(" '") +
3188 Lex.getStrVal() + "'");
3189 assert(Encoding <= Result.Max && "Expected valid DWARF language");
3190 Result.assign(Encoding);
3191 Lex.Lex();
3192 return false;
3193}
3194
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003195/// DIFlagField
3196/// ::= uint32
3197/// ::= DIFlagVector
3198/// ::= DIFlagVector '|' DIFlagFwdDecl '|' uint32 '|' DIFlagPublic
3199template <>
3200bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DIFlagField &Result) {
3201 assert(Result.Max == UINT32_MAX && "Expected only 32-bits");
3202
3203 // Parser for a single flag.
3204 auto parseFlag = [&](unsigned &Val) {
3205 if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned())
3206 return ParseUInt32(Val);
3207
3208 if (Lex.getKind() != lltok::DIFlag)
3209 return TokError("expected debug info flag");
3210
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003211 Val = DINode::getFlag(Lex.getStrVal());
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003212 if (!Val)
3213 return TokError(Twine("invalid debug info flag flag '") +
3214 Lex.getStrVal() + "'");
3215 Lex.Lex();
3216 return false;
3217 };
3218
3219 // Parse the flags and combine them together.
3220 unsigned Combined = 0;
3221 do {
3222 unsigned Val;
3223 if (parseFlag(Val))
3224 return true;
3225 Combined |= Val;
3226 } while (EatIfPresent(lltok::bar));
3227
3228 Result.assign(Combined);
3229 return false;
3230}
3231
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003232template <>
3233bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003234 MDSignedField &Result) {
3235 if (Lex.getKind() != lltok::APSInt)
3236 return TokError("expected signed integer");
3237
3238 auto &S = Lex.getAPSIntVal();
3239 if (S < Result.Min)
3240 return TokError("value for '" + Name + "' too small, limit is " +
3241 Twine(Result.Min));
3242 if (S > Result.Max)
3243 return TokError("value for '" + Name + "' too large, limit is " +
3244 Twine(Result.Max));
3245 Result.assign(S.getExtValue());
3246 assert(Result.Val >= Result.Min && "Expected value in range");
3247 assert(Result.Val <= Result.Max && "Expected value in range");
3248 Lex.Lex();
3249 return false;
3250}
3251
3252template <>
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003253bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) {
3254 switch (Lex.getKind()) {
3255 default:
3256 return TokError("expected 'true' or 'false'");
3257 case lltok::kw_true:
3258 Result.assign(true);
3259 break;
3260 case lltok::kw_false:
3261 Result.assign(false);
3262 break;
3263 }
3264 Lex.Lex();
3265 return false;
3266}
3267
3268template <>
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003269bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDField &Result) {
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003270 if (Lex.getKind() == lltok::kw_null) {
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00003271 if (!Result.AllowNull)
3272 return TokError("'" + Name + "' cannot be null");
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003273 Lex.Lex();
3274 Result.assign(nullptr);
3275 return false;
3276 }
3277
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003278 Metadata *MD;
3279 if (ParseMetadata(MD, nullptr))
3280 return true;
3281
3282 Result.assign(MD);
3283 return false;
3284}
3285
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003286template <>
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003287bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDConstant &Result) {
3288 Metadata *MD;
3289 if (ParseValueAsMetadata(MD, "expected constant", nullptr))
3290 return true;
3291
3292 Result.assign(cast<ConstantAsMetadata>(MD));
3293 return false;
3294}
3295
3296template <>
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003297bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDStringField &Result) {
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003298 LocTy ValueLoc = Lex.getLoc();
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003299 std::string S;
3300 if (ParseStringConstant(S))
3301 return true;
3302
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003303 if (!Result.AllowEmpty && S.empty())
3304 return Error(ValueLoc, "'" + Name + "' cannot be empty");
3305
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00003306 Result.assign(S.empty() ? nullptr : MDString::get(Context, S));
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003307 return false;
3308}
3309
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003310template <>
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003311bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) {
3312 SmallVector<Metadata *, 4> MDs;
3313 if (ParseMDNodeVector(MDs))
3314 return true;
3315
3316 Result.assign(std::move(MDs));
3317 return false;
3318}
3319
Duncan P. N. Exon Smith2f09c462015-02-04 22:13:28 +00003320} // end namespace llvm
3321
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003322template <class ParserTy>
Duncan P. N. Exon Smith66ca92e2015-01-19 23:39:32 +00003323bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003324 do {
3325 if (Lex.getKind() != lltok::LabelStr)
3326 return TokError("expected field label here");
3327
3328 if (parseField())
3329 return true;
3330 } while (EatIfPresent(lltok::comma));
3331
Duncan P. N. Exon Smith66ca92e2015-01-19 23:39:32 +00003332 return false;
3333}
3334
3335template <class ParserTy>
3336bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
3337 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
3338 Lex.Lex();
3339
3340 if (ParseToken(lltok::lparen, "expected '(' here"))
3341 return true;
3342 if (Lex.getKind() != lltok::rparen)
3343 if (ParseMDFieldsImplBody(parseField))
3344 return true;
3345
Duncan P. N. Exon Smith13890af2015-01-19 23:32:36 +00003346 ClosingLoc = Lex.getLoc();
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003347 return ParseToken(lltok::rparen, "expected ')' here");
3348}
3349
Duncan P. N. Exon Smitha7477282015-01-20 02:42:29 +00003350template <class FieldTy>
3351bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
3352 if (Result.Seen)
3353 return TokError("field '" + Name + "' cannot be specified more than once");
3354
3355 LocTy Loc = Lex.getLoc();
3356 Lex.Lex();
3357 return ParseMDField(Loc, Name, Result);
3358}
3359
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003360bool LLParser::ParseSpecializedMDNode(MDNode *&N, bool IsDistinct) {
3361 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003362
3363#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003364 if (Lex.getStrVal() == #CLASS) \
3365 return Parse##CLASS(N, IsDistinct);
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003366#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003367
3368 return TokError("expected metadata type");
3369}
3370
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003371#define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT
3372#define NOP_FIELD(NAME, TYPE, INIT)
3373#define REQUIRE_FIELD(NAME, TYPE, INIT) \
3374 if (!NAME.Seen) \
3375 return Error(ClosingLoc, "missing required field '" #NAME "'");
3376#define PARSE_MD_FIELD(NAME, TYPE, DEFAULT) \
Duncan P. N. Exon Smitha7477282015-01-20 02:42:29 +00003377 if (Lex.getStrVal() == #NAME) \
3378 return ParseMDField(#NAME, NAME);
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003379#define PARSE_MD_FIELDS() \
3380 VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD) \
3381 do { \
3382 LocTy ClosingLoc; \
3383 if (ParseMDFieldsImpl([&]() -> bool { \
3384 VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD) \
3385 return TokError(Twine("invalid field '") + Lex.getStrVal() + "'"); \
3386 }, ClosingLoc)) \
3387 return true; \
3388 VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD) \
3389 } while (false)
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003390#define GET_OR_DISTINCT(CLASS, ARGS) \
3391 (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003392
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003393/// ParseDILocationFields:
3394/// ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6)
3395bool LLParser::ParseDILocation(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003396#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003397 OPTIONAL(line, LineField, ); \
3398 OPTIONAL(column, ColumnField, ); \
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00003399 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003400 OPTIONAL(inlinedAt, MDField, );
3401 PARSE_MD_FIELDS();
3402#undef VISIT_MD_FIELDS
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003403
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +00003404 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003405 DILocation, (Context, line.Val, column.Val, scope.Val, inlinedAt.Val));
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003406 return false;
3407}
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003408
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003409/// ParseGenericDINode:
3410/// ::= !GenericDINode(tag: 15, header: "...", operands: {...})
3411bool LLParser::ParseGenericDINode(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003412#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003413 REQUIRED(tag, DwarfTagField, ); \
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003414 OPTIONAL(header, MDStringField, ); \
3415 OPTIONAL(operands, MDFieldList, );
3416 PARSE_MD_FIELDS();
3417#undef VISIT_MD_FIELDS
3418
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003419 Result = GET_OR_DISTINCT(GenericDINode,
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003420 (Context, tag.Val, header.Val, operands.Val));
3421 return false;
3422}
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003423
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003424/// ParseDISubrange:
3425/// ::= !DISubrange(count: 30, lowerBound: 2)
3426bool LLParser::ParseDISubrange(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003427#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +00003428 REQUIRED(count, MDSignedField, (-1, -1, INT64_MAX)); \
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003429 OPTIONAL(lowerBound, MDSignedField, );
3430 PARSE_MD_FIELDS();
3431#undef VISIT_MD_FIELDS
3432
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003433 Result = GET_OR_DISTINCT(DISubrange, (Context, count.Val, lowerBound.Val));
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003434 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003435}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003436
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003437/// ParseDIEnumerator:
3438/// ::= !DIEnumerator(value: 30, name: "SomeKind")
3439bool LLParser::ParseDIEnumerator(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00003440#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smithcd8fb602015-02-18 21:16:33 +00003441 REQUIRED(name, MDStringField, ); \
3442 REQUIRED(value, MDSignedField, );
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00003443 PARSE_MD_FIELDS();
3444#undef VISIT_MD_FIELDS
3445
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003446 Result = GET_OR_DISTINCT(DIEnumerator, (Context, value.Val, name.Val));
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00003447 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003448}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00003449
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003450/// ParseDIBasicType:
3451/// ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32)
3452bool LLParser::ParseDIBasicType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003453#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00003454 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003455 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00003456 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
3457 OPTIONAL(align, MDUnsignedField, (0, UINT64_MAX)); \
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003458 OPTIONAL(encoding, DwarfAttEncodingField, );
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003459 PARSE_MD_FIELDS();
3460#undef VISIT_MD_FIELDS
3461
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003462 Result = GET_OR_DISTINCT(DIBasicType, (Context, tag.Val, name.Val, size.Val,
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003463 align.Val, encoding.Val));
3464 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003465}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003466
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003467/// ParseDIDerivedType:
3468/// ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003469/// line: 7, scope: !1, baseType: !2, size: 32,
3470/// align: 32, offset: 0, flags: 0, extraData: !3)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003471bool LLParser::ParseDIDerivedType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003472#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3473 REQUIRED(tag, DwarfTagField, ); \
3474 OPTIONAL(name, MDStringField, ); \
3475 OPTIONAL(file, MDField, ); \
3476 OPTIONAL(line, LineField, ); \
3477 OPTIONAL(scope, MDField, ); \
3478 REQUIRED(baseType, MDField, ); \
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00003479 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
3480 OPTIONAL(align, MDUnsignedField, (0, UINT64_MAX)); \
3481 OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003482 OPTIONAL(flags, DIFlagField, ); \
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003483 OPTIONAL(extraData, MDField, );
3484 PARSE_MD_FIELDS();
3485#undef VISIT_MD_FIELDS
3486
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003487 Result = GET_OR_DISTINCT(DIDerivedType,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003488 (Context, tag.Val, name.Val, file.Val, line.Val,
3489 scope.Val, baseType.Val, size.Val, align.Val,
3490 offset.Val, flags.Val, extraData.Val));
3491 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003492}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003493
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003494bool LLParser::ParseDICompositeType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003495#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3496 REQUIRED(tag, DwarfTagField, ); \
3497 OPTIONAL(name, MDStringField, ); \
3498 OPTIONAL(file, MDField, ); \
3499 OPTIONAL(line, LineField, ); \
3500 OPTIONAL(scope, MDField, ); \
3501 OPTIONAL(baseType, MDField, ); \
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00003502 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
3503 OPTIONAL(align, MDUnsignedField, (0, UINT64_MAX)); \
3504 OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003505 OPTIONAL(flags, DIFlagField, ); \
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003506 OPTIONAL(elements, MDField, ); \
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003507 OPTIONAL(runtimeLang, DwarfLangField, ); \
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003508 OPTIONAL(vtableHolder, MDField, ); \
3509 OPTIONAL(templateParams, MDField, ); \
3510 OPTIONAL(identifier, MDStringField, );
3511 PARSE_MD_FIELDS();
3512#undef VISIT_MD_FIELDS
3513
3514 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003515 DICompositeType,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003516 (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val,
3517 size.Val, align.Val, offset.Val, flags.Val, elements.Val,
3518 runtimeLang.Val, vtableHolder.Val, templateParams.Val, identifier.Val));
3519 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003520}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003521
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003522bool LLParser::ParseDISubroutineType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00003523#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003524 OPTIONAL(flags, DIFlagField, ); \
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00003525 REQUIRED(types, MDField, );
3526 PARSE_MD_FIELDS();
3527#undef VISIT_MD_FIELDS
3528
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003529 Result = GET_OR_DISTINCT(DISubroutineType, (Context, flags.Val, types.Val));
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00003530 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003531}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00003532
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003533/// ParseDIFileType:
3534/// ::= !DIFileType(filename: "path/to/file", directory: "/path/to/dir")
3535bool LLParser::ParseDIFile(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00003536#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3537 REQUIRED(filename, MDStringField, ); \
3538 REQUIRED(directory, MDStringField, );
3539 PARSE_MD_FIELDS();
3540#undef VISIT_MD_FIELDS
3541
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003542 Result = GET_OR_DISTINCT(DIFile, (Context, filename.Val, directory.Val));
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00003543 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003544}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00003545
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003546/// ParseDICompileUnit:
3547/// ::= !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang",
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003548/// isOptimized: true, flags: "-O2", runtimeVersion: 1,
3549/// splitDebugFilename: "abc.debug", emissionKind: 1,
3550/// enums: !1, retainedTypes: !2, subprograms: !3,
Adrian Prantl1f599f92015-05-21 20:37:30 +00003551/// globals: !4, imports: !5, dwoId: 0x0abcd)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003552bool LLParser::ParseDICompileUnit(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003553#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3554 REQUIRED(language, DwarfLangField, ); \
Duncan P. N. Exon Smithcd07efa12015-03-31 00:47:15 +00003555 REQUIRED(file, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003556 OPTIONAL(producer, MDStringField, ); \
3557 OPTIONAL(isOptimized, MDBoolField, ); \
3558 OPTIONAL(flags, MDStringField, ); \
3559 OPTIONAL(runtimeVersion, MDUnsignedField, (0, UINT32_MAX)); \
3560 OPTIONAL(splitDebugFilename, MDStringField, ); \
3561 OPTIONAL(emissionKind, MDUnsignedField, (0, UINT32_MAX)); \
3562 OPTIONAL(enums, MDField, ); \
3563 OPTIONAL(retainedTypes, MDField, ); \
3564 OPTIONAL(subprograms, MDField, ); \
3565 OPTIONAL(globals, MDField, ); \
Adrian Prantl1f599f92015-05-21 20:37:30 +00003566 OPTIONAL(imports, MDField, ); \
3567 OPTIONAL(dwoId, MDUnsignedField, );
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003568 PARSE_MD_FIELDS();
3569#undef VISIT_MD_FIELDS
3570
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003571 Result = GET_OR_DISTINCT(DICompileUnit,
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003572 (Context, language.Val, file.Val, producer.Val,
3573 isOptimized.Val, flags.Val, runtimeVersion.Val,
3574 splitDebugFilename.Val, emissionKind.Val, enums.Val,
3575 retainedTypes.Val, subprograms.Val, globals.Val,
Adrian Prantl1f599f92015-05-21 20:37:30 +00003576 imports.Val, dwoId.Val));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003577 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003578}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003579
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003580/// ParseDISubprogram:
3581/// ::= !DISubprogram(scope: !0, name: "foo", linkageName: "_Zfoo",
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003582/// file: !1, line: 7, type: !2, isLocal: false,
3583/// isDefinition: true, scopeLine: 8, containingType: !3,
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003584/// virtuality: DW_VIRTUALTIY_pure_virtual,
3585/// virtualIndex: 10, flags: 11,
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003586/// isOptimized: false, function: void ()* @_Z3foov,
3587/// templateParams: !4, declaration: !5, variables: !6)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003588bool LLParser::ParseDISubprogram(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003589#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3590 OPTIONAL(scope, MDField, ); \
Duncan P. N. Exon Smith3eea1962015-03-16 19:01:54 +00003591 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003592 OPTIONAL(linkageName, MDStringField, ); \
3593 OPTIONAL(file, MDField, ); \
3594 OPTIONAL(line, LineField, ); \
3595 OPTIONAL(type, MDField, ); \
3596 OPTIONAL(isLocal, MDBoolField, ); \
3597 OPTIONAL(isDefinition, MDBoolField, (true)); \
3598 OPTIONAL(scopeLine, LineField, ); \
3599 OPTIONAL(containingType, MDField, ); \
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003600 OPTIONAL(virtuality, DwarfVirtualityField, ); \
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003601 OPTIONAL(virtualIndex, MDUnsignedField, (0, UINT32_MAX)); \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003602 OPTIONAL(flags, DIFlagField, ); \
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003603 OPTIONAL(isOptimized, MDBoolField, ); \
3604 OPTIONAL(function, MDConstant, ); \
3605 OPTIONAL(templateParams, MDField, ); \
3606 OPTIONAL(declaration, MDField, ); \
3607 OPTIONAL(variables, MDField, );
3608 PARSE_MD_FIELDS();
3609#undef VISIT_MD_FIELDS
3610
3611 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003612 DISubprogram, (Context, scope.Val, name.Val, linkageName.Val, file.Val,
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003613 line.Val, type.Val, isLocal.Val, isDefinition.Val,
3614 scopeLine.Val, containingType.Val, virtuality.Val,
3615 virtualIndex.Val, flags.Val, isOptimized.Val, function.Val,
3616 templateParams.Val, declaration.Val, variables.Val));
3617 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003618}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003619
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003620/// ParseDILexicalBlock:
3621/// ::= !DILexicalBlock(scope: !0, file: !2, line: 7, column: 9)
3622bool LLParser::ParseDILexicalBlock(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00003623#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00003624 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00003625 OPTIONAL(file, MDField, ); \
3626 OPTIONAL(line, LineField, ); \
3627 OPTIONAL(column, ColumnField, );
3628 PARSE_MD_FIELDS();
3629#undef VISIT_MD_FIELDS
3630
3631 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003632 DILexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val));
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00003633 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003634}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00003635
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003636/// ParseDILexicalBlockFile:
3637/// ::= !DILexicalBlockFile(scope: !0, file: !2, discriminator: 9)
3638bool LLParser::ParseDILexicalBlockFile(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00003639#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00003640 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00003641 OPTIONAL(file, MDField, ); \
3642 REQUIRED(discriminator, MDUnsignedField, (0, UINT32_MAX));
3643 PARSE_MD_FIELDS();
3644#undef VISIT_MD_FIELDS
3645
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003646 Result = GET_OR_DISTINCT(DILexicalBlockFile,
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00003647 (Context, scope.Val, file.Val, discriminator.Val));
3648 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003649}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00003650
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003651/// ParseDINamespace:
3652/// ::= !DINamespace(scope: !0, file: !2, name: "SomeNamespace", line: 9)
3653bool LLParser::ParseDINamespace(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00003654#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3655 REQUIRED(scope, MDField, ); \
3656 OPTIONAL(file, MDField, ); \
3657 OPTIONAL(name, MDStringField, ); \
3658 OPTIONAL(line, LineField, );
3659 PARSE_MD_FIELDS();
3660#undef VISIT_MD_FIELDS
3661
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003662 Result = GET_OR_DISTINCT(DINamespace,
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00003663 (Context, scope.Val, file.Val, name.Val, line.Val));
3664 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003665}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00003666
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003667/// ParseDITemplateTypeParameter:
3668/// ::= !DITemplateTypeParameter(name: "Ty", type: !1)
3669bool LLParser::ParseDITemplateTypeParameter(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003670#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003671 OPTIONAL(name, MDStringField, ); \
3672 REQUIRED(type, MDField, );
3673 PARSE_MD_FIELDS();
3674#undef VISIT_MD_FIELDS
3675
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00003676 Result =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003677 GET_OR_DISTINCT(DITemplateTypeParameter, (Context, name.Val, type.Val));
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003678 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003679}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003680
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003681/// ParseDITemplateValueParameter:
3682/// ::= !DITemplateValueParameter(tag: DW_TAG_template_value_parameter,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00003683/// name: "V", type: !1, value: i32 7)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003684bool LLParser::ParseDITemplateValueParameter(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003685#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00003686 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_template_value_parameter)); \
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003687 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00003688 OPTIONAL(type, MDField, ); \
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003689 REQUIRED(value, MDField, );
3690 PARSE_MD_FIELDS();
3691#undef VISIT_MD_FIELDS
3692
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003693 Result = GET_OR_DISTINCT(DITemplateValueParameter,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00003694 (Context, tag.Val, name.Val, type.Val, value.Val));
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003695 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003696}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003697
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003698/// ParseDIGlobalVariable:
3699/// ::= !DIGlobalVariable(scope: !0, name: "foo", linkageName: "foo",
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003700/// file: !1, line: 7, type: !2, isLocal: false,
3701/// isDefinition: true, variable: i32* @foo,
3702/// declaration: !3)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003703bool LLParser::ParseDIGlobalVariable(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003704#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003705 REQUIRED(name, MDStringField, (/* AllowEmpty */ false)); \
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003706 OPTIONAL(scope, MDField, ); \
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003707 OPTIONAL(linkageName, MDStringField, ); \
3708 OPTIONAL(file, MDField, ); \
3709 OPTIONAL(line, LineField, ); \
3710 OPTIONAL(type, MDField, ); \
3711 OPTIONAL(isLocal, MDBoolField, ); \
3712 OPTIONAL(isDefinition, MDBoolField, (true)); \
3713 OPTIONAL(variable, MDConstant, ); \
3714 OPTIONAL(declaration, MDField, );
3715 PARSE_MD_FIELDS();
3716#undef VISIT_MD_FIELDS
3717
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003718 Result = GET_OR_DISTINCT(DIGlobalVariable,
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003719 (Context, scope.Val, name.Val, linkageName.Val,
3720 file.Val, line.Val, type.Val, isLocal.Val,
3721 isDefinition.Val, variable.Val, declaration.Val));
3722 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003723}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003724
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003725/// ParseDILocalVariable:
3726/// ::= !DILocalVariable(tag: DW_TAG_arg_variable, scope: !0, name: "foo",
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00003727/// file: !1, line: 7, type: !2, arg: 2, flags: 7)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003728bool LLParser::ParseDILocalVariable(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003729#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3730 REQUIRED(tag, DwarfTagField, ); \
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00003731 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003732 OPTIONAL(name, MDStringField, ); \
3733 OPTIONAL(file, MDField, ); \
3734 OPTIONAL(line, LineField, ); \
3735 OPTIONAL(type, MDField, ); \
Duncan P. N. Exon Smith69488692015-06-02 17:17:44 +00003736 OPTIONAL(arg, MDUnsignedField, (0, UINT16_MAX)); \
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00003737 OPTIONAL(flags, DIFlagField, );
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003738 PARSE_MD_FIELDS();
3739#undef VISIT_MD_FIELDS
3740
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003741 Result = GET_OR_DISTINCT(DILocalVariable,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00003742 (Context, tag.Val, scope.Val, name.Val, file.Val,
3743 line.Val, type.Val, arg.Val, flags.Val));
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003744 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003745}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003746
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003747/// ParseDIExpression:
3748/// ::= !DIExpression(0, 7, -1)
3749bool LLParser::ParseDIExpression(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00003750 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
3751 Lex.Lex();
3752
3753 if (ParseToken(lltok::lparen, "expected '(' here"))
3754 return true;
3755
3756 SmallVector<uint64_t, 8> Elements;
3757 if (Lex.getKind() != lltok::rparen)
3758 do {
3759 if (Lex.getKind() == lltok::DwarfOp) {
3760 if (unsigned Op = dwarf::getOperationEncoding(Lex.getStrVal())) {
3761 Lex.Lex();
3762 Elements.push_back(Op);
3763 continue;
3764 }
3765 return TokError(Twine("invalid DWARF op '") + Lex.getStrVal() + "'");
3766 }
3767
3768 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
3769 return TokError("expected unsigned integer");
3770
3771 auto &U = Lex.getAPSIntVal();
3772 if (U.ugt(UINT64_MAX))
3773 return TokError("element too large, limit is " + Twine(UINT64_MAX));
3774 Elements.push_back(U.getZExtValue());
3775 Lex.Lex();
3776 } while (EatIfPresent(lltok::comma));
3777
3778 if (ParseToken(lltok::rparen, "expected ')' here"))
3779 return true;
3780
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003781 Result = GET_OR_DISTINCT(DIExpression, (Context, Elements));
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00003782 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003783}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00003784
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003785/// ParseDIObjCProperty:
3786/// ::= !DIObjCProperty(name: "foo", file: !1, line: 7, setter: "setFoo",
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003787/// getter: "getFoo", attributes: 7, type: !2)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003788bool LLParser::ParseDIObjCProperty(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003789#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith3eea1962015-03-16 19:01:54 +00003790 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003791 OPTIONAL(file, MDField, ); \
3792 OPTIONAL(line, LineField, ); \
3793 OPTIONAL(setter, MDStringField, ); \
3794 OPTIONAL(getter, MDStringField, ); \
3795 OPTIONAL(attributes, MDUnsignedField, (0, UINT32_MAX)); \
3796 OPTIONAL(type, MDField, );
3797 PARSE_MD_FIELDS();
3798#undef VISIT_MD_FIELDS
3799
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003800 Result = GET_OR_DISTINCT(DIObjCProperty,
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003801 (Context, name.Val, file.Val, line.Val, setter.Val,
3802 getter.Val, attributes.Val, type.Val));
3803 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003804}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003805
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003806/// ParseDIImportedEntity:
3807/// ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1,
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00003808/// line: 7, name: "foo")
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003809bool LLParser::ParseDIImportedEntity(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00003810#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3811 REQUIRED(tag, DwarfTagField, ); \
3812 REQUIRED(scope, MDField, ); \
3813 OPTIONAL(entity, MDField, ); \
3814 OPTIONAL(line, LineField, ); \
3815 OPTIONAL(name, MDStringField, );
3816 PARSE_MD_FIELDS();
3817#undef VISIT_MD_FIELDS
3818
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003819 Result = GET_OR_DISTINCT(DIImportedEntity, (Context, tag.Val, scope.Val,
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00003820 entity.Val, line.Val, name.Val));
3821 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003822}
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00003823
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003824#undef PARSE_MD_FIELD
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003825#undef NOP_FIELD
3826#undef REQUIRE_FIELD
3827#undef DECLARE_FIELD
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003828
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003829/// ParseMetadataAsValue
3830/// ::= metadata i32 %local
3831/// ::= metadata i32 @global
3832/// ::= metadata i32 7
3833/// ::= metadata !0
3834/// ::= metadata !{...}
3835/// ::= metadata !"string"
3836bool LLParser::ParseMetadataAsValue(Value *&V, PerFunctionState &PFS) {
3837 // Note: the type 'metadata' has already been parsed.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003838 Metadata *MD;
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003839 if (ParseMetadata(MD, &PFS))
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003840 return true;
3841
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003842 V = MetadataAsValue::get(Context, MD);
3843 return false;
3844}
3845
3846/// ParseValueAsMetadata
3847/// ::= i32 %local
3848/// ::= i32 @global
3849/// ::= i32 7
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003850bool LLParser::ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
3851 PerFunctionState *PFS) {
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003852 Type *Ty;
3853 LocTy Loc;
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003854 if (ParseType(Ty, TypeMsg, Loc))
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003855 return true;
3856 if (Ty->isMetadataTy())
3857 return Error(Loc, "invalid metadata-value-metadata roundtrip");
3858
3859 Value *V;
3860 if (ParseValue(Ty, V, PFS))
3861 return true;
3862
3863 MD = ValueAsMetadata::get(V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003864 return false;
3865}
3866
3867/// ParseMetadata
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003868/// ::= i32 %local
3869/// ::= i32 @global
3870/// ::= i32 7
Dan Gohman8939ba332010-07-14 18:26:50 +00003871/// ::= !42
3872/// ::= !{...}
3873/// ::= !"string"
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003874/// ::= !DILocation(...)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003875bool LLParser::ParseMetadata(Metadata *&MD, PerFunctionState *PFS) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003876 if (Lex.getKind() == lltok::MetadataVar) {
3877 MDNode *N;
3878 if (ParseSpecializedMDNode(N))
3879 return true;
3880 MD = N;
3881 return false;
3882 }
3883
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003884 // ValueAsMetadata:
3885 // <type> <value>
3886 if (Lex.getKind() != lltok::exclaim)
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003887 return ParseValueAsMetadata(MD, "expected metadata operand", PFS);
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003888
3889 // '!'.
3890 assert(Lex.getKind() == lltok::exclaim && "Expected '!' here");
3891 Lex.Lex();
Dan Gohman8939ba332010-07-14 18:26:50 +00003892
Duncan P. N. Exon Smith62a79192015-01-12 22:24:50 +00003893 // MDString:
3894 // ::= '!' STRINGCONSTANT
3895 if (Lex.getKind() == lltok::StringConstant) {
3896 MDString *S;
3897 if (ParseMDString(S))
3898 return true;
3899 MD = S;
3900 return false;
3901 }
3902
Dan Gohman8939ba332010-07-14 18:26:50 +00003903 // MDNode:
3904 // !{ ... }
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00003905 // !7
Duncan P. N. Exon Smith62a79192015-01-12 22:24:50 +00003906 MDNode *N;
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00003907 if (ParseMDNodeTail(N))
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003908 return true;
Duncan P. N. Exon Smith62a79192015-01-12 22:24:50 +00003909 MD = N;
Dan Gohman8939ba332010-07-14 18:26:50 +00003910 return false;
3911}
3912
Victor Hernandez9d75c962010-01-11 22:31:58 +00003913
3914//===----------------------------------------------------------------------===//
3915// Function Parsing.
3916//===----------------------------------------------------------------------===//
3917
Chris Lattner229907c2011-07-18 04:54:35 +00003918bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +00003919 PerFunctionState *PFS) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003920 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003921 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003922
Chris Lattnerac161bf2009-01-02 07:01:27 +00003923 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003924 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00003925 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
3926 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003927 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003928 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00003929 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
3930 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003931 return V == nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003932 case ValID::t_InlineAsm: {
Chris Lattner229907c2011-07-18 04:54:35 +00003933 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003934 FunctionType *FTy =
Craig Topper2617dcc2014-04-15 06:32:26 +00003935 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003936 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
3937 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosierf42fad62012-09-05 00:08:17 +00003938 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosierd8c76102012-09-05 19:00:49 +00003939 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00003940 return false;
3941 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003942 case ValID::t_GlobalName:
3943 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003944 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003945 case ValID::t_GlobalID:
3946 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003947 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003948 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00003949 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003950 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00003951 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00003952 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003953 return false;
3954 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00003955 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003956 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
3957 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003958
Dan Gohman518cda42011-12-17 00:04:22 +00003959 // The lexer has no type info, so builds all half, float, and double FP
3960 // constants as double. Fix this here. Long double does not need this.
3961 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003962 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00003963 if (Ty->isHalfTy())
3964 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
3965 &Ignored);
3966 else if (Ty->isFloatTy())
3967 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
3968 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003969 }
Owen Anderson69c464d2009-07-27 20:59:43 +00003970 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003971
Chris Lattner8f57d29e2009-01-05 18:24:23 +00003972 if (V->getType() != Ty)
3973 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003974 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003975
Chris Lattnerac161bf2009-01-02 07:01:27 +00003976 return false;
3977 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00003978 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003979 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003980 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003981 return false;
3982 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00003983 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003984 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00003985 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003986 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003987 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00003988 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00003989 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00003990 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003991 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00003992 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003993 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00003994 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00003995 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003996 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00003997 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003998 return false;
3999 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00004000 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004001 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00004002
Chris Lattnerac161bf2009-01-02 07:01:27 +00004003 V = ID.ConstantVal;
4004 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004005 case ValID::t_ConstantStruct:
4006 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00004007 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004008 if (ST->getNumElements() != ID.UIntVal)
4009 return Error(ID.Loc,
4010 "initializer with struct type has wrong # elements");
4011 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
4012 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004013
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004014 // Verify that the elements are compatible with the structtype.
4015 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
4016 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
4017 return Error(ID.Loc, "element " + Twine(i) +
4018 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004019
Reid Kleckner2ae03e12015-03-04 18:31:10 +00004020 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
4021 ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004022 } else
4023 return Error(ID.Loc, "constant expression type mismatch");
4024 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004025 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00004026 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004027}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004028
Chris Lattner229907c2011-07-18 04:54:35 +00004029bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004030 V = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004031 ValID ID;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004032 return ParseValID(ID, PFS) ||
4033 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004034}
4035
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004036bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004037 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004038 return ParseType(Ty) ||
4039 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004040}
4041
Chris Lattner3ed871f2009-10-27 19:13:16 +00004042bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
4043 PerFunctionState &PFS) {
4044 Value *V;
4045 Loc = Lex.getLoc();
4046 if (ParseTypeAndValue(V, PFS)) return true;
4047 if (!isa<BasicBlock>(V))
4048 return Error(Loc, "expected a basic block");
4049 BB = cast<BasicBlock>(V);
4050 return false;
4051}
4052
4053
Chris Lattnerac161bf2009-01-02 07:01:27 +00004054/// FunctionHeader
4055/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindola45e6c192011-01-08 16:42:36 +00004056/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Peter Collingbourne51d2de72014-12-03 02:08:38 +00004057/// OptionalAlign OptGC OptionalPrefix OptionalPrologue
Chris Lattnerac161bf2009-01-02 07:01:27 +00004058bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
4059 // Parse the linkage.
4060 LocTy LinkageLoc = Lex.getLoc();
4061 unsigned Linkage;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004062
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00004063 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00004064 unsigned DLLStorageClass;
Bill Wendling50d27842012-10-15 20:35:56 +00004065 AttrBuilder RetAttrs;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00004066 unsigned CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004067 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004068 LocTy RetTypeLoc = Lex.getLoc();
4069 if (ParseOptionalLinkage(Linkage) ||
4070 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +00004071 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004072 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004073 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004074 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004075 return true;
4076
4077 // Verify that the linkage is ok.
4078 switch ((GlobalValue::LinkageTypes)Linkage) {
4079 case GlobalValue::ExternalLinkage:
4080 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00004081 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004082 if (isDefine)
4083 return Error(LinkageLoc, "invalid linkage for function definition");
4084 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00004085 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004086 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00004087 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00004088 case GlobalValue::LinkOnceAnyLinkage:
4089 case GlobalValue::LinkOnceODRLinkage:
4090 case GlobalValue::WeakAnyLinkage:
4091 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004092 if (!isDefine)
4093 return Error(LinkageLoc, "invalid linkage for function declaration");
4094 break;
4095 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00004096 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004097 return Error(LinkageLoc, "invalid function linkage type");
4098 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004099
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00004100 if (!isValidVisibilityForLinkage(Visibility, Linkage))
4101 return Error(LinkageLoc,
4102 "symbol with local linkage must have default visibility");
4103
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004104 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004105 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004106
Chris Lattnerac161bf2009-01-02 07:01:27 +00004107 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00004108
4109 std::string FunctionName;
4110 if (Lex.getKind() == lltok::GlobalVar) {
4111 FunctionName = Lex.getStrVal();
4112 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
4113 unsigned NameID = Lex.getUIntVal();
4114
4115 if (NameID != NumberedVals.size())
4116 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00004117 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00004118 } else {
4119 return TokError("expected function name");
4120 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004121
Chris Lattner3822f632009-01-02 08:05:26 +00004122 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004123
Chris Lattner3822f632009-01-02 08:05:26 +00004124 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004125 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004126
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004127 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004128 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00004129 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004130 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00004131 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004132 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004133 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00004134 std::string GC;
Rafael Espindola563eb4b2011-01-25 19:09:56 +00004135 bool UnnamedAddr;
4136 LocTy UnnamedAddrLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00004137 Constant *Prefix = nullptr;
Peter Collingbourne51d2de72014-12-03 02:08:38 +00004138 Constant *Prologue = nullptr;
David Majnemerdad0a642014-06-27 18:19:56 +00004139 Comdat *C;
Chris Lattner3822f632009-01-02 08:05:26 +00004140
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004141 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola563eb4b2011-01-25 19:09:56 +00004142 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
4143 &UnnamedAddrLoc) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004144 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00004145 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00004146 (EatIfPresent(lltok::kw_section) &&
4147 ParseStringConstant(Section)) ||
Rafael Espindola83a362c2015-01-06 22:55:16 +00004148 parseOptionalComdat(FunctionName, C) ||
Chris Lattner3822f632009-01-02 08:05:26 +00004149 ParseOptionalAlignment(Alignment) ||
4150 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00004151 ParseStringConstant(GC)) ||
4152 (EatIfPresent(lltok::kw_prefix) &&
Peter Collingbourne51d2de72014-12-03 02:08:38 +00004153 ParseGlobalTypeAndValue(Prefix)) ||
4154 (EatIfPresent(lltok::kw_prologue) &&
4155 ParseGlobalTypeAndValue(Prologue)))
Chris Lattner3822f632009-01-02 08:05:26 +00004156 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004157
Michael Gottesman41748d72013-06-27 00:25:01 +00004158 if (FuncAttrs.contains(Attribute::Builtin))
4159 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00004160
Chris Lattnerac161bf2009-01-02 07:01:27 +00004161 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00004162 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00004163 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004164 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004165 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004166
Chris Lattnerac161bf2009-01-02 07:01:27 +00004167 // Okay, if we got here, the function is syntactically valid. Convert types
4168 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00004169 std::vector<Type*> ParamTypeList;
Bill Wendlingf5075a42013-01-27 02:24:02 +00004170 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004171
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004172 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004173 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4174 AttributeSet::ReturnIndex,
4175 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004176
Chris Lattnerac161bf2009-01-02 07:01:27 +00004177 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004178 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004179 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4180 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004181 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4182 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004183 }
4184
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004185 if (FuncAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004186 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4187 AttributeSet::FunctionIndex,
4188 FuncAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004189
Bill Wendlinge94d8432012-12-07 23:16:57 +00004190 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004191
Bill Wendling749a43d2012-12-30 13:50:49 +00004192 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004193 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
4194
Chris Lattner229907c2011-07-18 04:54:35 +00004195 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00004196 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00004197 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004198
Craig Topper2617dcc2014-04-15 06:32:26 +00004199 Fn = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004200 if (!FunctionName.empty()) {
4201 // If this was a definition of a forward reference, remove the definition
4202 // from the forward reference table and fill in the forward ref.
4203 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
4204 ForwardRefVals.find(FunctionName);
4205 if (FRVI != ForwardRefVals.end()) {
4206 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00004207 if (!Fn)
4208 return Error(FRVI->second.second, "invalid forward reference to "
4209 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00004210 if (Fn->getType() != PFT)
4211 return Error(FRVI->second.second, "invalid forward reference to "
4212 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004213
Chris Lattnerac161bf2009-01-02 07:01:27 +00004214 ForwardRefVals.erase(FRVI);
4215 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00004216 // Reject redefinitions.
4217 return Error(NameLoc, "invalid redefinition of function '" +
4218 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00004219 } else if (M->getNamedValue(FunctionName)) {
4220 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004221 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004222
Dan Gohman399d6ae2009-08-29 23:37:49 +00004223 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004224 // If this is a definition of a forward referenced function, make sure the
4225 // types agree.
4226 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
4227 = ForwardRefValIDs.find(NumberedVals.size());
4228 if (I != ForwardRefValIDs.end()) {
4229 Fn = cast<Function>(I->second.first);
4230 if (Fn->getType() != PFT)
4231 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00004232 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004233 ForwardRefValIDs.erase(I);
4234 }
4235 }
4236
Craig Topper2617dcc2014-04-15 06:32:26 +00004237 if (!Fn)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004238 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
4239 else // Move the forward-reference to the correct spot in the module.
4240 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
4241
4242 if (FunctionName.empty())
4243 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004244
Chris Lattnerac161bf2009-01-02 07:01:27 +00004245 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
4246 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00004247 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004248 Fn->setCallingConv(CC);
4249 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00004250 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004251 Fn->setAlignment(Alignment);
4252 Fn->setSection(Section);
David Majnemerdad0a642014-06-27 18:19:56 +00004253 Fn->setComdat(C);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004254 if (!GC.empty()) Fn->setGC(GC.c_str());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00004255 Fn->setPrefixData(Prefix);
Peter Collingbourne51d2de72014-12-03 02:08:38 +00004256 Fn->setPrologueData(Prologue);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004257 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004258
Chris Lattnerac161bf2009-01-02 07:01:27 +00004259 // Add all of the arguments we parsed to the function.
4260 Function::arg_iterator ArgIt = Fn->arg_begin();
4261 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
4262 // If the argument has a name, insert it into the argument symbol table.
4263 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004264
Chris Lattnerac161bf2009-01-02 07:01:27 +00004265 // Set the name, if it conflicted, it will be auto-renamed.
4266 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004267
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00004268 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004269 return Error(ArgList[i].Loc, "redefinition of argument '%" +
4270 ArgList[i].Name + "'");
4271 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004272
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00004273 if (isDefine)
4274 return false;
4275
Robin Morisset039781e2014-08-29 21:53:01 +00004276 // Check the declaration has no block address forward references.
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00004277 ValID ID;
4278 if (FunctionName.empty()) {
4279 ID.Kind = ValID::t_GlobalID;
4280 ID.UIntVal = NumberedVals.size() - 1;
4281 } else {
4282 ID.Kind = ValID::t_GlobalName;
4283 ID.StrVal = FunctionName;
4284 }
4285 auto Blocks = ForwardRefBlockAddresses.find(ID);
4286 if (Blocks != ForwardRefBlockAddresses.end())
4287 return Error(Blocks->first.Loc,
4288 "cannot take blockaddress inside a declaration");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004289 return false;
4290}
4291
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00004292bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() {
4293 ValID ID;
4294 if (FunctionNumber == -1) {
4295 ID.Kind = ValID::t_GlobalName;
4296 ID.StrVal = F.getName();
4297 } else {
4298 ID.Kind = ValID::t_GlobalID;
4299 ID.UIntVal = FunctionNumber;
4300 }
4301
4302 auto Blocks = P.ForwardRefBlockAddresses.find(ID);
4303 if (Blocks == P.ForwardRefBlockAddresses.end())
4304 return false;
4305
4306 for (const auto &I : Blocks->second) {
4307 const ValID &BBID = I.first;
4308 GlobalValue *GV = I.second;
4309
4310 assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) &&
4311 "Expected local id or name");
4312 BasicBlock *BB;
4313 if (BBID.Kind == ValID::t_LocalName)
4314 BB = GetBB(BBID.StrVal, BBID.Loc);
4315 else
4316 BB = GetBB(BBID.UIntVal, BBID.Loc);
4317 if (!BB)
4318 return P.Error(BBID.Loc, "referenced value is not a basic block");
4319
4320 GV->replaceAllUsesWith(BlockAddress::get(&F, BB));
4321 GV->eraseFromParent();
4322 }
4323
4324 P.ForwardRefBlockAddresses.erase(Blocks);
4325 return false;
4326}
Chris Lattnerac161bf2009-01-02 07:01:27 +00004327
4328/// ParseFunctionBody
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00004329/// ::= '{' BasicBlock+ UseListOrderDirective* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00004330bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00004331 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004332 return TokError("expected '{' in function body");
4333 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004334
Chris Lattner3432c622009-10-28 03:39:23 +00004335 int FunctionNumber = -1;
4336 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004337
Chris Lattner3432c622009-10-28 03:39:23 +00004338 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004339
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00004340 // Resolve block addresses and allow basic blocks to be forward-declared
4341 // within this function.
4342 if (PFS.resolveForwardRefBlockAddresses())
4343 return true;
4344 SaveAndRestore<PerFunctionState *> ScopeExit(BlockAddressPFS, &PFS);
4345
Chris Lattnerbbddd962010-01-09 19:20:07 +00004346 // We need at least one basic block.
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00004347 if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder)
Chris Lattnerbbddd962010-01-09 19:20:07 +00004348 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004349
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00004350 while (Lex.getKind() != lltok::rbrace &&
4351 Lex.getKind() != lltok::kw_uselistorder)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004352 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004353
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00004354 while (Lex.getKind() != lltok::rbrace)
4355 if (ParseUseListOrder(&PFS))
4356 return true;
4357
Chris Lattnerac161bf2009-01-02 07:01:27 +00004358 // Eat the }.
4359 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004360
Chris Lattnerac161bf2009-01-02 07:01:27 +00004361 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00004362 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00004363}
4364
4365/// ParseBasicBlock
4366/// ::= LabelStr? Instruction*
4367bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
4368 // If this basic block starts out with a name, remember it.
4369 std::string Name;
4370 LocTy NameLoc = Lex.getLoc();
4371 if (Lex.getKind() == lltok::LabelStr) {
4372 Name = Lex.getStrVal();
4373 Lex.Lex();
4374 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004375
Chris Lattnerac161bf2009-01-02 07:01:27 +00004376 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
Owen Anderson576a9a22015-03-02 05:25:09 +00004377 if (!BB)
4378 return Error(NameLoc,
4379 "unable to create block named '" + Name + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004380
Chris Lattnerac161bf2009-01-02 07:01:27 +00004381 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004382
Chris Lattnerac161bf2009-01-02 07:01:27 +00004383 // Parse the instructions in this block until we get a terminator.
4384 Instruction *Inst;
4385 do {
4386 // This instruction may have three possibilities for a name: a) none
4387 // specified, b) name specified "%foo =", c) number specified: "%4 =".
4388 LocTy NameLoc = Lex.getLoc();
4389 int NameID = -1;
4390 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004391
Chris Lattnerac161bf2009-01-02 07:01:27 +00004392 if (Lex.getKind() == lltok::LocalVarID) {
4393 NameID = Lex.getUIntVal();
4394 Lex.Lex();
4395 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
4396 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00004397 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004398 NameStr = Lex.getStrVal();
4399 Lex.Lex();
4400 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
4401 return true;
4402 }
Devang Patelea8a4b92009-09-17 23:04:48 +00004403
Chris Lattner77b89dc2009-12-30 05:23:43 +00004404 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00004405 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00004406 case InstError: return true;
4407 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00004408 BB->getInstList().push_back(Inst);
4409
Chris Lattner77b89dc2009-12-30 05:23:43 +00004410 // With a normal result, we check to see if the instruction is followed by
4411 // a comma and metadata.
4412 if (EatIfPresent(lltok::comma))
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00004413 if (ParseInstructionMetadata(*Inst))
Chris Lattner77b89dc2009-12-30 05:23:43 +00004414 return true;
4415 break;
4416 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00004417 BB->getInstList().push_back(Inst);
4418
Chris Lattner77b89dc2009-12-30 05:23:43 +00004419 // If the instruction parser ate an extra comma at the end of it, it
4420 // *must* be followed by metadata.
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00004421 if (ParseInstructionMetadata(*Inst))
Chris Lattner77b89dc2009-12-30 05:23:43 +00004422 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004423 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00004424 }
Devang Patelea8a4b92009-09-17 23:04:48 +00004425
Chris Lattnerac161bf2009-01-02 07:01:27 +00004426 // Set the name on the instruction.
4427 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
4428 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004429
Chris Lattnerac161bf2009-01-02 07:01:27 +00004430 return false;
4431}
4432
4433//===----------------------------------------------------------------------===//
4434// Instruction Parsing.
4435//===----------------------------------------------------------------------===//
4436
4437/// ParseInstruction - Parse one of the many different instructions.
4438///
Chris Lattner77b89dc2009-12-30 05:23:43 +00004439int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
4440 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004441 lltok::Kind Token = Lex.getKind();
4442 if (Token == lltok::Eof)
4443 return TokError("found end of file when expecting more instructions");
4444 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00004445 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00004446 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004447
Chris Lattnerac161bf2009-01-02 07:01:27 +00004448 switch (Token) {
4449 default: return Error(Loc, "expected instruction opcode");
4450 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00004451 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004452 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
4453 case lltok::kw_br: return ParseBr(Inst, PFS);
4454 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004455 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004456 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00004457 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004458 // Binary Operators.
4459 case lltok::kw_add:
4460 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00004461 case lltok::kw_mul:
4462 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00004463 bool NUW = EatIfPresent(lltok::kw_nuw);
4464 bool NSW = EatIfPresent(lltok::kw_nsw);
4465 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004466
Chris Lattnera676c0f2011-02-07 16:40:21 +00004467 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004468
Chris Lattnera676c0f2011-02-07 16:40:21 +00004469 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
4470 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
4471 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00004472 }
Dan Gohmana5b96452009-06-04 22:49:04 +00004473 case lltok::kw_fadd:
4474 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00004475 case lltok::kw_fmul:
4476 case lltok::kw_fdiv:
4477 case lltok::kw_frem: {
4478 FastMathFlags FMF = EatFastMathFlagsIfPresent();
4479 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
4480 if (Res != 0)
4481 return Res;
4482 if (FMF.any())
4483 Inst->setFastMathFlags(FMF);
4484 return 0;
4485 }
Dan Gohmana5b96452009-06-04 22:49:04 +00004486
Chris Lattner35315d02011-02-06 21:44:57 +00004487 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00004488 case lltok::kw_udiv:
4489 case lltok::kw_lshr:
4490 case lltok::kw_ashr: {
4491 bool Exact = EatIfPresent(lltok::kw_exact);
4492
4493 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
4494 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
4495 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00004496 }
4497
Chris Lattnerac161bf2009-01-02 07:01:27 +00004498 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00004499 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004500 case lltok::kw_and:
4501 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00004502 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004503 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004504 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004505 // Casts.
4506 case lltok::kw_trunc:
4507 case lltok::kw_zext:
4508 case lltok::kw_sext:
4509 case lltok::kw_fptrunc:
4510 case lltok::kw_fpext:
4511 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00004512 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004513 case lltok::kw_uitofp:
4514 case lltok::kw_sitofp:
4515 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004516 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004517 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00004518 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004519 // Other.
4520 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00004521 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004522 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
4523 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
4524 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
4525 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00004526 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +00004527 // Call.
4528 case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None);
4529 case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail);
4530 case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004531 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00004532 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00004533 case lltok::kw_load: return ParseLoad(Inst, PFS);
4534 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00004535 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
4536 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00004537 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004538 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
4539 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
4540 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
4541 }
4542}
4543
4544/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
4545bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004546 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004547 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00004548 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004549 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
4550 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
4551 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
4552 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
4553 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
4554 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
4555 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
4556 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
4557 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
4558 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
4559 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
4560 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
4561 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
4562 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
4563 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
4564 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
4565 }
4566 } else {
4567 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00004568 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004569 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
4570 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
4571 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
4572 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
4573 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
4574 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
4575 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
4576 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
4577 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
4578 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
4579 }
4580 }
4581 Lex.Lex();
4582 return false;
4583}
4584
4585//===----------------------------------------------------------------------===//
4586// Terminator Instructions.
4587//===----------------------------------------------------------------------===//
4588
4589/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00004590/// ::= 'ret' void (',' !dbg, !1)*
4591/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00004592bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004593 PerFunctionState &PFS) {
4594 SMLoc TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00004595 Type *Ty = nullptr;
Chris Lattnerf880ca22009-03-09 04:49:14 +00004596 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004597
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004598 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004599
Chris Lattnerfdd87902009-10-05 05:54:46 +00004600 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004601 if (!ResType->isVoidTy())
4602 return Error(TypeLoc, "value doesn't match function result type '" +
4603 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004604
Owen Anderson55f1c092009-08-13 21:58:54 +00004605 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004606 return false;
4607 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004608
Chris Lattnerac161bf2009-01-02 07:01:27 +00004609 Value *RV;
4610 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004611
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004612 if (ResType != RV->getType())
4613 return Error(TypeLoc, "value doesn't match function result type '" +
4614 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004615
Owen Anderson55f1c092009-08-13 21:58:54 +00004616 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00004617 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004618}
4619
4620
4621/// ParseBr
4622/// ::= 'br' TypeAndValue
4623/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
4624bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
4625 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00004626 Value *Op0;
4627 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004628 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004629
Chris Lattnerac161bf2009-01-02 07:01:27 +00004630 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
4631 Inst = BranchInst::Create(BB);
4632 return false;
4633 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004634
Owen Anderson55f1c092009-08-13 21:58:54 +00004635 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004636 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004637
Chris Lattnerac161bf2009-01-02 07:01:27 +00004638 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004639 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004640 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004641 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004642 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004643
Chris Lattner3ed871f2009-10-27 19:13:16 +00004644 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004645 return false;
4646}
4647
4648/// ParseSwitch
4649/// Instruction
4650/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
4651/// JumpTable
4652/// ::= (TypeAndValue ',' TypeAndValue)*
4653bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
4654 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00004655 Value *Cond;
4656 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004657 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
4658 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004659 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004660 ParseToken(lltok::lsquare, "expected '[' with switch table"))
4661 return true;
4662
Duncan Sands19d0b472010-02-16 11:11:14 +00004663 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004664 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004665
Chris Lattnerac161bf2009-01-02 07:01:27 +00004666 // Parse the jump table pairs.
4667 SmallPtrSet<Value*, 32> SeenCases;
4668 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
4669 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00004670 Value *Constant;
4671 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004672
Chris Lattnerac161bf2009-01-02 07:01:27 +00004673 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
4674 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004675 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004676 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004677
David Blaikie70573dc2014-11-19 07:49:26 +00004678 if (!SeenCases.insert(Constant).second)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004679 return Error(CondLoc, "duplicate case value in switch");
4680 if (!isa<ConstantInt>(Constant))
4681 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004682
Chris Lattner3ed871f2009-10-27 19:13:16 +00004683 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004684 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004685
Chris Lattnerac161bf2009-01-02 07:01:27 +00004686 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004687
Chris Lattner3ed871f2009-10-27 19:13:16 +00004688 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00004689 for (unsigned i = 0, e = Table.size(); i != e; ++i)
4690 SI->addCase(Table[i].first, Table[i].second);
4691 Inst = SI;
4692 return false;
4693}
4694
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004695/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00004696/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004697/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
4698bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00004699 LocTy AddrLoc;
4700 Value *Address;
4701 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004702 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
4703 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00004704 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004705
Duncan Sands19d0b472010-02-16 11:11:14 +00004706 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004707 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004708
Chris Lattner3ed871f2009-10-27 19:13:16 +00004709 // Parse the destination list.
4710 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004711
Chris Lattner3ed871f2009-10-27 19:13:16 +00004712 if (Lex.getKind() != lltok::rsquare) {
4713 BasicBlock *DestBB;
4714 if (ParseTypeAndBasicBlock(DestBB, PFS))
4715 return true;
4716 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004717
Chris Lattner3ed871f2009-10-27 19:13:16 +00004718 while (EatIfPresent(lltok::comma)) {
4719 if (ParseTypeAndBasicBlock(DestBB, PFS))
4720 return true;
4721 DestList.push_back(DestBB);
4722 }
4723 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004724
Chris Lattner3ed871f2009-10-27 19:13:16 +00004725 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
4726 return true;
4727
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004728 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00004729 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
4730 IBI->addDestination(DestList[i]);
4731 Inst = IBI;
4732 return false;
4733}
4734
4735
Chris Lattnerac161bf2009-01-02 07:01:27 +00004736/// ParseInvoke
4737/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
4738/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
4739bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
4740 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00004741 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004742 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00004743 LocTy NoBuiltinLoc;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00004744 unsigned CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004745 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004746 LocTy RetTypeLoc;
4747 ValID CalleeID;
4748 SmallVector<ParamInfo, 16> ArgList;
4749
Chris Lattner3ed871f2009-10-27 19:13:16 +00004750 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004751 if (ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004752 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004753 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004754 ParseValID(CalleeID) ||
4755 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004756 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
4757 NoBuiltinLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004758 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004759 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004760 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004761 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004762 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004763
Chris Lattnerac161bf2009-01-02 07:01:27 +00004764 // If RetType is a non-function pointer type, then this is the short syntax
4765 // for the call, which means that RetType is just the return type. Infer the
4766 // rest of the function argument types from the arguments that are present.
David Blaikie445e3fb2015-04-24 19:32:54 +00004767 FunctionType *Ty = dyn_cast<FunctionType>(RetType);
4768 if (!Ty) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004769 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00004770 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004771 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
4772 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004773
Chris Lattnerac161bf2009-01-02 07:01:27 +00004774 if (!FunctionType::isValidReturnType(RetType))
4775 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004776
Owen Anderson4056ca92009-07-29 22:17:13 +00004777 Ty = FunctionType::get(RetType, ParamTypes, false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004778 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004779
Chris Lattnerac161bf2009-01-02 07:01:27 +00004780 // Look up the callee.
4781 Value *Callee;
David Blaikie445e3fb2015-04-24 19:32:54 +00004782 if (ConvertValIDToValue(PointerType::getUnqual(Ty), CalleeID, Callee, &PFS))
4783 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004784
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004785 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00004786 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004787 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004788 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4789 AttributeSet::ReturnIndex,
4790 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004791
Chris Lattnerac161bf2009-01-02 07:01:27 +00004792 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004793
Chris Lattnerac161bf2009-01-02 07:01:27 +00004794 // Loop through FunctionType's arguments and ensure they are specified
4795 // correctly. Also, gather any parameter attributes.
4796 FunctionType::param_iterator I = Ty->param_begin();
4797 FunctionType::param_iterator E = Ty->param_end();
4798 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004799 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004800 if (I != E) {
4801 ExpectedTy = *I++;
4802 } else if (!Ty->isVarArg()) {
4803 return Error(ArgList[i].Loc, "too many arguments specified");
4804 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004805
Chris Lattnerac161bf2009-01-02 07:01:27 +00004806 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
4807 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004808 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004809 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004810 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4811 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004812 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4813 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004814 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004815
Chris Lattnerac161bf2009-01-02 07:01:27 +00004816 if (I != E)
4817 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004818
David Majnemer8d22abd2015-02-23 00:01:32 +00004819 if (FnAttrs.hasAttributes()) {
4820 if (FnAttrs.hasAlignmentAttr())
4821 return Error(CallLoc, "invoke instructions may not have an alignment");
4822
Bill Wendlingf5075a42013-01-27 02:24:02 +00004823 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4824 AttributeSet::FunctionIndex,
4825 FnAttrs));
David Majnemer8d22abd2015-02-23 00:01:32 +00004826 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004827
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004828 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00004829 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004830
David Blaikie3e807092015-05-13 18:35:26 +00004831 InvokeInst *II = InvokeInst::Create(Ty, Callee, NormalBB, UnwindBB, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004832 II->setCallingConv(CC);
4833 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004834 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004835 Inst = II;
4836 return false;
4837}
4838
Bill Wendlingf891bf82011-07-31 06:30:59 +00004839/// ParseResume
4840/// ::= 'resume' TypeAndValue
4841bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
4842 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00004843 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
4844 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004845
Bill Wendlingf891bf82011-07-31 06:30:59 +00004846 ResumeInst *RI = ResumeInst::Create(Exn);
4847 Inst = RI;
4848 return false;
4849}
Chris Lattnerac161bf2009-01-02 07:01:27 +00004850
4851//===----------------------------------------------------------------------===//
4852// Binary Operators.
4853//===----------------------------------------------------------------------===//
4854
4855/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004856/// ::= ArithmeticOps TypeAndValue ',' Value
4857///
4858/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
4859/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00004860bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004861 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004862 LocTy Loc; Value *LHS, *RHS;
4863 if (ParseTypeAndValue(LHS, Loc, PFS) ||
4864 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
4865 ParseValue(LHS->getType(), RHS, PFS))
4866 return true;
4867
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004868 bool Valid;
4869 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004870 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004871 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00004872 Valid = LHS->getType()->isIntOrIntVectorTy() ||
4873 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004874 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00004875 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
4876 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004877 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004878
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004879 if (!Valid)
4880 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004881
Chris Lattnerac161bf2009-01-02 07:01:27 +00004882 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
4883 return false;
4884}
4885
4886/// ParseLogical
4887/// ::= ArithmeticOps TypeAndValue ',' Value {
4888bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
4889 unsigned Opc) {
4890 LocTy Loc; Value *LHS, *RHS;
4891 if (ParseTypeAndValue(LHS, Loc, PFS) ||
4892 ParseToken(lltok::comma, "expected ',' in logical operation") ||
4893 ParseValue(LHS->getType(), RHS, PFS))
4894 return true;
4895
Duncan Sands9dff9be2010-02-15 16:12:20 +00004896 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004897 return Error(Loc,"instruction requires integer or integer vector operands");
4898
4899 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
4900 return false;
4901}
4902
4903
4904/// ParseCompare
4905/// ::= 'icmp' IPredicates TypeAndValue ',' Value
4906/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00004907bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
4908 unsigned Opc) {
4909 // Parse the integer/fp comparison predicate.
4910 LocTy Loc;
4911 unsigned Pred;
4912 Value *LHS, *RHS;
4913 if (ParseCmpPredicate(Pred, Opc) ||
4914 ParseTypeAndValue(LHS, Loc, PFS) ||
4915 ParseToken(lltok::comma, "expected ',' after compare value") ||
4916 ParseValue(LHS->getType(), RHS, PFS))
4917 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004918
Chris Lattnerac161bf2009-01-02 07:01:27 +00004919 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00004920 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004921 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00004922 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004923 } else {
4924 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00004925 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00004926 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004927 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00004928 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004929 }
4930 return false;
4931}
4932
4933//===----------------------------------------------------------------------===//
4934// Other Instructions.
4935//===----------------------------------------------------------------------===//
4936
4937
4938/// ParseCast
4939/// ::= CastOpc TypeAndValue 'to' Type
4940bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
4941 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004942 LocTy Loc;
4943 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00004944 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004945 if (ParseTypeAndValue(Op, Loc, PFS) ||
4946 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
4947 ParseType(DestTy))
4948 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004949
Chris Lattner89d856e2009-03-01 00:53:13 +00004950 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
4951 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004952 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004953 getTypeString(Op->getType()) + "' to '" +
4954 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00004955 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004956 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
4957 return false;
4958}
4959
4960/// ParseSelect
4961/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
4962bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
4963 LocTy Loc;
4964 Value *Op0, *Op1, *Op2;
4965 if (ParseTypeAndValue(Op0, Loc, PFS) ||
4966 ParseToken(lltok::comma, "expected ',' after select condition") ||
4967 ParseTypeAndValue(Op1, PFS) ||
4968 ParseToken(lltok::comma, "expected ',' after select value") ||
4969 ParseTypeAndValue(Op2, PFS))
4970 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004971
Chris Lattnerac161bf2009-01-02 07:01:27 +00004972 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
4973 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004974
Chris Lattnerac161bf2009-01-02 07:01:27 +00004975 Inst = SelectInst::Create(Op0, Op1, Op2);
4976 return false;
4977}
4978
Chris Lattnerb55ab542009-01-05 08:18:44 +00004979/// ParseVA_Arg
4980/// ::= 'va_arg' TypeAndValue ',' Type
4981bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004982 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00004983 Type *EltTy = nullptr;
Chris Lattnerb55ab542009-01-05 08:18:44 +00004984 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004985 if (ParseTypeAndValue(Op, PFS) ||
4986 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00004987 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004988 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004989
Chris Lattnerb55ab542009-01-05 08:18:44 +00004990 if (!EltTy->isFirstClassType())
4991 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004992
4993 Inst = new VAArgInst(Op, EltTy);
4994 return false;
4995}
4996
4997/// ParseExtractElement
4998/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
4999bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
5000 LocTy Loc;
5001 Value *Op0, *Op1;
5002 if (ParseTypeAndValue(Op0, Loc, PFS) ||
5003 ParseToken(lltok::comma, "expected ',' after extract value") ||
5004 ParseTypeAndValue(Op1, PFS))
5005 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005006
Chris Lattnerac161bf2009-01-02 07:01:27 +00005007 if (!ExtractElementInst::isValidOperands(Op0, Op1))
5008 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005009
Eric Christopherc9742252009-07-25 02:28:41 +00005010 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005011 return false;
5012}
5013
5014/// ParseInsertElement
5015/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
5016bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
5017 LocTy Loc;
5018 Value *Op0, *Op1, *Op2;
5019 if (ParseTypeAndValue(Op0, Loc, PFS) ||
5020 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
5021 ParseTypeAndValue(Op1, PFS) ||
5022 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
5023 ParseTypeAndValue(Op2, PFS))
5024 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005025
Chris Lattnerac161bf2009-01-02 07:01:27 +00005026 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00005027 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005028
Chris Lattnerac161bf2009-01-02 07:01:27 +00005029 Inst = InsertElementInst::Create(Op0, Op1, Op2);
5030 return false;
5031}
5032
5033/// ParseShuffleVector
5034/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
5035bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
5036 LocTy Loc;
5037 Value *Op0, *Op1, *Op2;
5038 if (ParseTypeAndValue(Op0, Loc, PFS) ||
5039 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
5040 ParseTypeAndValue(Op1, PFS) ||
5041 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
5042 ParseTypeAndValue(Op2, PFS))
5043 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005044
Chris Lattnerac161bf2009-01-02 07:01:27 +00005045 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00005046 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005047
Chris Lattnerac161bf2009-01-02 07:01:27 +00005048 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
5049 return false;
5050}
5051
5052/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00005053/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00005054int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005055 Type *Ty = nullptr; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005056 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005057
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00005058 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005059 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
5060 ParseValue(Ty, Op0, PFS) ||
5061 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00005062 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005063 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
5064 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005065
Chris Lattnerf4f03422009-12-30 05:27:33 +00005066 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005067 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
5068 while (1) {
5069 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005070
Chris Lattner3822f632009-01-02 08:05:26 +00005071 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005072 break;
5073
Chris Lattnerf4f03422009-12-30 05:27:33 +00005074 if (Lex.getKind() == lltok::MetadataVar) {
5075 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00005076 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005077 }
Devang Patel8f842d32009-10-16 18:45:49 +00005078
Chris Lattner3822f632009-01-02 08:05:26 +00005079 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005080 ParseValue(Ty, Op0, PFS) ||
5081 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00005082 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005083 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
5084 return true;
5085 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005086
Chris Lattnerac161bf2009-01-02 07:01:27 +00005087 if (!Ty->isFirstClassType())
5088 return Error(TypeLoc, "phi node must have first class type");
5089
Jay Foad52131342011-03-30 11:28:46 +00005090 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00005091 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
5092 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
5093 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005094 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005095}
5096
Bill Wendlingfae14752011-08-12 20:24:12 +00005097/// ParseLandingPad
5098/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
5099/// Clause
5100/// ::= 'catch' TypeAndValue
5101/// ::= 'filter'
5102/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
5103bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005104 Type *Ty = nullptr; LocTy TyLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00005105 Value *PersFn; LocTy PersFnLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00005106
5107 if (ParseType(Ty, TyLoc) ||
5108 ParseToken(lltok::kw_personality, "expected 'personality'") ||
5109 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
5110 return true;
5111
Owen Andersonf8f259d2015-03-09 07:13:42 +00005112 std::unique_ptr<LandingPadInst> LP(LandingPadInst::Create(Ty, PersFn, 0));
Bill Wendlingfae14752011-08-12 20:24:12 +00005113 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
5114
5115 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
5116 LandingPadInst::ClauseType CT;
5117 if (EatIfPresent(lltok::kw_catch))
5118 CT = LandingPadInst::Catch;
5119 else if (EatIfPresent(lltok::kw_filter))
5120 CT = LandingPadInst::Filter;
5121 else
5122 return TokError("expected 'catch' or 'filter' clause type");
5123
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00005124 Value *V;
5125 LocTy VLoc;
Owen Andersonf8f259d2015-03-09 07:13:42 +00005126 if (ParseTypeAndValue(V, VLoc, PFS))
Bill Wendlingfae14752011-08-12 20:24:12 +00005127 return true;
Bill Wendlingfae14752011-08-12 20:24:12 +00005128
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00005129 // A 'catch' type expects a non-array constant. A filter clause expects an
5130 // array constant.
5131 if (CT == LandingPadInst::Catch) {
5132 if (isa<ArrayType>(V->getType()))
5133 Error(VLoc, "'catch' clause has an invalid type");
5134 } else {
5135 if (!isa<ArrayType>(V->getType()))
5136 Error(VLoc, "'filter' clause has an invalid type");
5137 }
5138
Owen Andersonf8f259d2015-03-09 07:13:42 +00005139 Constant *CV = dyn_cast<Constant>(V);
5140 if (!CV)
5141 return Error(VLoc, "clause argument must be a constant");
5142 LP->addClause(CV);
Bill Wendlingfae14752011-08-12 20:24:12 +00005143 }
5144
Owen Andersonf8f259d2015-03-09 07:13:42 +00005145 Inst = LP.release();
Bill Wendlingfae14752011-08-12 20:24:12 +00005146 return false;
5147}
5148
Chris Lattnerac161bf2009-01-02 07:01:27 +00005149/// ParseCall
Reid Kleckner5772b772014-04-24 20:14:34 +00005150/// ::= 'call' OptionalCallingConv OptionalAttrs Type Value
5151/// ParameterList OptionalAttrs
5152/// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value
5153/// ParameterList OptionalAttrs
5154/// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00005155/// ParameterList OptionalAttrs
5156bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
Reid Kleckner5772b772014-04-24 20:14:34 +00005157 CallInst::TailCallKind TCK) {
Bill Wendling50d27842012-10-15 20:35:56 +00005158 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00005159 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00005160 LocTy BuiltinLoc;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00005161 unsigned CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00005162 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005163 LocTy RetTypeLoc;
5164 ValID CalleeID;
5165 SmallVector<ParamInfo, 16> ArgList;
5166 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005167
Reid Kleckner5772b772014-04-24 20:14:34 +00005168 if ((TCK != CallInst::TCK_None &&
5169 ParseToken(lltok::kw_call, "expected 'tail call'")) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005170 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00005171 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00005172 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005173 ParseValID(CalleeID) ||
Reid Kleckner83498642014-08-26 00:33:28 +00005174 ParseParameterList(ArgList, PFS, TCK == CallInst::TCK_MustTail,
5175 PFS.getFunction().isVarArg()) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00005176 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00005177 BuiltinLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005178 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005179
Chris Lattnerac161bf2009-01-02 07:01:27 +00005180 // If RetType is a non-function pointer type, then this is the short syntax
5181 // for the call, which means that RetType is just the return type. Infer the
5182 // rest of the function argument types from the arguments that are present.
David Blaikie23af6482015-04-16 23:24:18 +00005183 FunctionType *Ty = dyn_cast<FunctionType>(RetType);
5184 if (!Ty) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005185 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00005186 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00005187 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
5188 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005189
Chris Lattnerac161bf2009-01-02 07:01:27 +00005190 if (!FunctionType::isValidReturnType(RetType))
5191 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005192
Owen Anderson4056ca92009-07-29 22:17:13 +00005193 Ty = FunctionType::get(RetType, ParamTypes, false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005194 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005195
Chris Lattnerac161bf2009-01-02 07:01:27 +00005196 // Look up the callee.
5197 Value *Callee;
David Blaikie23af6482015-04-16 23:24:18 +00005198 if (ConvertValIDToValue(PointerType::getUnqual(Ty), CalleeID, Callee, &PFS))
5199 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005200
Bill Wendling3d7b0b82012-12-19 07:18:57 +00005201 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00005202 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00005203 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00005204 Attrs.push_back(AttributeSet::get(RetType->getContext(),
5205 AttributeSet::ReturnIndex,
5206 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005207
Chris Lattnerac161bf2009-01-02 07:01:27 +00005208 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005209
Chris Lattnerac161bf2009-01-02 07:01:27 +00005210 // Loop through FunctionType's arguments and ensure they are specified
5211 // correctly. Also, gather any parameter attributes.
5212 FunctionType::param_iterator I = Ty->param_begin();
5213 FunctionType::param_iterator E = Ty->param_end();
5214 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005215 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005216 if (I != E) {
5217 ExpectedTy = *I++;
5218 } else if (!Ty->isVarArg()) {
5219 return Error(ArgList[i].Loc, "too many arguments specified");
5220 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005221
Chris Lattnerac161bf2009-01-02 07:01:27 +00005222 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
5223 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00005224 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005225 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00005226 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
5227 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00005228 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
5229 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00005230 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005231
Chris Lattnerac161bf2009-01-02 07:01:27 +00005232 if (I != E)
5233 return Error(CallLoc, "not enough parameters specified for call");
5234
David Majnemer8d22abd2015-02-23 00:01:32 +00005235 if (FnAttrs.hasAttributes()) {
5236 if (FnAttrs.hasAlignmentAttr())
5237 return Error(CallLoc, "call instructions may not have an alignment");
5238
Bill Wendlingf5075a42013-01-27 02:24:02 +00005239 Attrs.push_back(AttributeSet::get(RetType->getContext(),
5240 AttributeSet::FunctionIndex,
5241 FnAttrs));
David Majnemer8d22abd2015-02-23 00:01:32 +00005242 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00005243
Bill Wendling3d7b0b82012-12-19 07:18:57 +00005244 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00005245 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005246
David Blaikie348de692015-04-23 21:36:23 +00005247 CallInst *CI = CallInst::Create(Ty, Callee, Args);
Reid Kleckner5772b772014-04-24 20:14:34 +00005248 CI->setTailCallKind(TCK);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005249 CI->setCallingConv(CC);
5250 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00005251 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005252 Inst = CI;
5253 return false;
5254}
5255
5256//===----------------------------------------------------------------------===//
5257// Memory Instructions.
5258//===----------------------------------------------------------------------===//
5259
5260/// ParseAlloc
David Majnemerc4ab61c2014-03-09 06:41:58 +00005261/// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)?
Chris Lattner78103722011-06-17 03:16:47 +00005262int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005263 Value *Size = nullptr;
David Majnemera3b0eb22015-02-16 08:38:03 +00005264 LocTy SizeLoc, TyLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005265 unsigned Alignment = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00005266 Type *Ty = nullptr;
David Majnemerc4ab61c2014-03-09 06:41:58 +00005267
5268 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
5269
David Majnemera3b0eb22015-02-16 08:38:03 +00005270 if (ParseType(Ty, TyLoc)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005271
David Majnemera3b0eb22015-02-16 08:38:03 +00005272 if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
5273 return Error(TyLoc, "invalid type for alloca");
David Majnemerfad5a312015-02-11 09:13:11 +00005274
Chris Lattnerb2f39502009-12-30 05:44:30 +00005275 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00005276 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00005277 if (Lex.getKind() == lltok::kw_align) {
5278 if (ParseOptionalAlignment(Alignment)) return true;
5279 } else if (Lex.getKind() == lltok::MetadataVar) {
5280 AteExtraComma = true;
5281 } else {
5282 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
5283 ParseOptionalCommaAlign(Alignment, AteExtraComma))
5284 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005285 }
5286 }
5287
Dan Gohman2140a742010-05-28 01:14:11 +00005288 if (Size && !Size->getType()->isIntegerTy())
5289 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005290
Reid Kleckner436c42e2014-01-17 23:58:17 +00005291 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
5292 AI->setUsedWithInAlloca(IsInAlloca);
5293 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00005294 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005295}
5296
5297/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00005298/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005299/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00005300/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00005301int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005302 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00005303 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00005304 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00005305 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00005306 AtomicOrdering Ordering = NotAtomic;
5307 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00005308
5309 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00005310 isAtomic = true;
5311 Lex.Lex();
5312 }
5313
Chris Lattnerbc639292011-11-27 06:56:53 +00005314 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00005315 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00005316 isVolatile = true;
5317 Lex.Lex();
5318 }
5319
David Blaikie15d9a4c2015-04-06 20:59:48 +00005320 Type *Ty;
David Blaikiea79ac142015-02-27 21:17:42 +00005321 LocTy ExplicitTypeLoc = Lex.getLoc();
5322 if (ParseType(Ty) ||
5323 ParseToken(lltok::comma, "expected comma after load's type") ||
5324 ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00005325 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00005326 ParseOptionalCommaAlign(Alignment, AteExtraComma))
5327 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005328
David Blaikie15d9a4c2015-04-06 20:59:48 +00005329 if (!Val->getType()->isPointerTy() || !Ty->isFirstClassType())
Chris Lattnerac161bf2009-01-02 07:01:27 +00005330 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00005331 if (isAtomic && !Alignment)
5332 return Error(Loc, "atomic load must have explicit non-zero alignment");
5333 if (Ordering == Release || Ordering == AcquireRelease)
5334 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005335
David Blaikiea79ac142015-02-27 21:17:42 +00005336 if (Ty != cast<PointerType>(Val->getType())->getElementType())
5337 return Error(ExplicitTypeLoc,
5338 "explicit pointee type doesn't match operand's pointee type");
5339
David Blaikie15d9a4c2015-04-06 20:59:48 +00005340 Inst = new LoadInst(Ty, Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00005341 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005342}
5343
5344/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00005345
5346/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
5347/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00005348/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00005349int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005350 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00005351 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00005352 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00005353 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00005354 AtomicOrdering Ordering = NotAtomic;
5355 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00005356
5357 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00005358 isAtomic = true;
5359 Lex.Lex();
5360 }
5361
Chris Lattnerbc639292011-11-27 06:56:53 +00005362 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00005363 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00005364 isVolatile = true;
5365 Lex.Lex();
5366 }
5367
Chris Lattnerac161bf2009-01-02 07:01:27 +00005368 if (ParseTypeAndValue(Val, Loc, PFS) ||
5369 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00005370 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00005371 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00005372 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005373 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00005374
Duncan Sands19d0b472010-02-16 11:11:14 +00005375 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00005376 return Error(PtrLoc, "store operand must be a pointer");
5377 if (!Val->getType()->isFirstClassType())
5378 return Error(Loc, "store operand must be a first class value");
5379 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
5380 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00005381 if (isAtomic && !Alignment)
5382 return Error(Loc, "atomic store must have explicit non-zero alignment");
5383 if (Ordering == Acquire || Ordering == AcquireRelease)
5384 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005385
Eli Friedman59b66882011-08-09 23:02:53 +00005386 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00005387 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005388}
5389
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005390/// ParseCmpXchg
Tim Northover420a2162014-06-13 14:24:07 +00005391/// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
5392/// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00005393int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005394 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
5395 bool AteExtraComma = false;
Tim Northovere94a5182014-03-11 10:48:52 +00005396 AtomicOrdering SuccessOrdering = NotAtomic;
5397 AtomicOrdering FailureOrdering = NotAtomic;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005398 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00005399 bool isVolatile = false;
Tim Northover420a2162014-06-13 14:24:07 +00005400 bool isWeak = false;
5401
5402 if (EatIfPresent(lltok::kw_weak))
5403 isWeak = true;
Eli Friedman02e737b2011-08-12 22:50:01 +00005404
5405 if (EatIfPresent(lltok::kw_volatile))
5406 isVolatile = true;
5407
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005408 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
5409 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
5410 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
5411 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
5412 ParseTypeAndValue(New, NewLoc, PFS) ||
Tim Northovere94a5182014-03-11 10:48:52 +00005413 ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) ||
5414 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005415 return true;
5416
Tim Northovere94a5182014-03-11 10:48:52 +00005417 if (SuccessOrdering == Unordered || FailureOrdering == Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005418 return TokError("cmpxchg cannot be unordered");
Tim Northovere94a5182014-03-11 10:48:52 +00005419 if (SuccessOrdering < FailureOrdering)
5420 return TokError("cmpxchg must be at least as ordered on success as failure");
5421 if (FailureOrdering == Release || FailureOrdering == AcquireRelease)
5422 return TokError("cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005423 if (!Ptr->getType()->isPointerTy())
5424 return Error(PtrLoc, "cmpxchg operand must be a pointer");
5425 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
5426 return Error(CmpLoc, "compare value and pointer type do not match");
5427 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
5428 return Error(NewLoc, "new value and pointer type do not match");
5429 if (!New->getType()->isIntegerTy())
5430 return Error(NewLoc, "cmpxchg operand must be an integer");
5431 unsigned Size = New->getType()->getPrimitiveSizeInBits();
5432 if (Size < 8 || (Size & (Size - 1)))
5433 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
5434 " integer");
5435
Tim Northover420a2162014-06-13 14:24:07 +00005436 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(
5437 Ptr, Cmp, New, SuccessOrdering, FailureOrdering, Scope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005438 CXI->setVolatile(isVolatile);
Tim Northover420a2162014-06-13 14:24:07 +00005439 CXI->setWeak(isWeak);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005440 Inst = CXI;
5441 return AteExtraComma ? InstExtraComma : InstNormal;
5442}
5443
5444/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00005445/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
5446/// 'singlethread'? AtomicOrdering
5447int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005448 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
5449 bool AteExtraComma = false;
5450 AtomicOrdering Ordering = NotAtomic;
5451 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00005452 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005453 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00005454
5455 if (EatIfPresent(lltok::kw_volatile))
5456 isVolatile = true;
5457
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005458 switch (Lex.getKind()) {
5459 default: return TokError("expected binary operation in atomicrmw");
5460 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
5461 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
5462 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
5463 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
5464 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
5465 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
5466 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
5467 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
5468 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
5469 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
5470 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
5471 }
5472 Lex.Lex(); // Eat the operation.
5473
5474 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
5475 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
5476 ParseTypeAndValue(Val, ValLoc, PFS) ||
5477 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
5478 return true;
5479
5480 if (Ordering == Unordered)
5481 return TokError("atomicrmw cannot be unordered");
5482 if (!Ptr->getType()->isPointerTy())
5483 return Error(PtrLoc, "atomicrmw operand must be a pointer");
5484 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
5485 return Error(ValLoc, "atomicrmw value and pointer type do not match");
5486 if (!Val->getType()->isIntegerTy())
5487 return Error(ValLoc, "atomicrmw operand must be an integer");
5488 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
5489 if (Size < 8 || (Size & (Size - 1)))
5490 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
5491 " integer");
5492
5493 AtomicRMWInst *RMWI =
5494 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
5495 RMWI->setVolatile(isVolatile);
5496 Inst = RMWI;
5497 return AteExtraComma ? InstExtraComma : InstNormal;
5498}
5499
Eli Friedmanfee02c62011-07-25 23:16:38 +00005500/// ParseFence
5501/// ::= 'fence' 'singlethread'? AtomicOrdering
5502int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
5503 AtomicOrdering Ordering = NotAtomic;
5504 SynchronizationScope Scope = CrossThread;
5505 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
5506 return true;
5507
5508 if (Ordering == Unordered)
5509 return TokError("fence cannot be unordered");
5510 if (Ordering == Monotonic)
5511 return TokError("fence cannot be monotonic");
5512
5513 Inst = new FenceInst(Context, Ordering, Scope);
5514 return InstNormal;
5515}
5516
Chris Lattnerac161bf2009-01-02 07:01:27 +00005517/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00005518/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00005519int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005520 Value *Ptr = nullptr;
5521 Value *Val = nullptr;
Nadav Rotem3924cb02011-12-05 06:29:09 +00005522 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00005523
Dan Gohman16cbbe42009-07-29 15:58:36 +00005524 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00005525
David Blaikie79e6c742015-02-27 19:29:02 +00005526 Type *Ty = nullptr;
5527 LocTy ExplicitTypeLoc = Lex.getLoc();
5528 if (ParseType(Ty) ||
5529 ParseToken(lltok::comma, "expected comma after getelementptr's type") ||
5530 ParseTypeAndValue(Ptr, Loc, PFS))
5531 return true;
5532
Eli Benderskyd9806682013-04-22 17:03:42 +00005533 Type *BaseType = Ptr->getType();
5534 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
5535 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00005536 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005537
David Blaikie8d757942015-03-09 23:08:44 +00005538 if (Ty != BasePointerType->getElementType())
5539 return Error(ExplicitTypeLoc,
5540 "explicit pointee type doesn't match operand's pointee type");
5541
Chris Lattnerac161bf2009-01-02 07:01:27 +00005542 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005543 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00005544 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00005545 if (Lex.getKind() == lltok::MetadataVar) {
5546 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00005547 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005548 }
Chris Lattner3822f632009-01-02 08:05:26 +00005549 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem3924cb02011-12-05 06:29:09 +00005550 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00005551 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem3924cb02011-12-05 06:29:09 +00005552 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
5553 return Error(EltLoc, "getelementptr index type missmatch");
5554 if (Val->getType()->isVectorTy()) {
5555 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
5556 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
5557 if (ValNumEl != PtrNumEl)
5558 return Error(EltLoc,
5559 "getelementptr vector index has a wrong number of elements");
5560 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00005561 Indices.push_back(Val);
5562 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005563
Owen Andersone90f9922015-03-10 06:34:57 +00005564 SmallPtrSet<const Type*, 4> Visited;
David Blaikied33bad32015-04-17 22:32:13 +00005565 if (!Indices.empty() && !Ty->isSized(&Visited))
Eli Benderskyd9806682013-04-22 17:03:42 +00005566 return Error(Loc, "base element of getelementptr must be sized");
5567
David Blaikied33bad32015-04-17 22:32:13 +00005568 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005569 return Error(Loc, "invalid getelementptr indices");
David Blaikiecd7b97e2015-03-14 21:11:24 +00005570 Inst = GetElementPtrInst::Create(Ty, Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00005571 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00005572 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00005573 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005574}
5575
5576/// ParseExtractValue
5577/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00005578int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005579 Value *Val; LocTy Loc;
5580 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005581 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005582 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00005583 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005584 return true;
5585
Chris Lattner392be582010-02-12 20:49:41 +00005586 if (!Val->getType()->isAggregateType())
5587 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005588
Jay Foad57aa6362011-07-13 10:26:04 +00005589 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005590 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00005591 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00005592 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005593}
5594
5595/// ParseInsertValue
5596/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00005597int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005598 Value *Val0, *Val1; LocTy Loc0, Loc1;
5599 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005600 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005601 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
5602 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
5603 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00005604 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005605 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005606
Chris Lattner392be582010-02-12 20:49:41 +00005607 if (!Val0->getType()->isAggregateType())
5608 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005609
David Majnemer30074532015-02-11 07:43:58 +00005610 Type *IndexedType = ExtractValueInst::getIndexedType(Val0->getType(), Indices);
5611 if (!IndexedType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00005612 return Error(Loc0, "invalid indices for insertvalue");
David Majnemer30074532015-02-11 07:43:58 +00005613 if (IndexedType != Val1->getType())
5614 return Error(Loc1, "insertvalue operand and field disagree in type: '" +
5615 getTypeString(Val1->getType()) + "' instead of '" +
5616 getTypeString(IndexedType) + "'");
Jay Foad57aa6362011-07-13 10:26:04 +00005617 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00005618 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005619}
Nick Lewycky49f89192009-04-04 07:22:01 +00005620
5621//===----------------------------------------------------------------------===//
5622// Embedded metadata.
5623//===----------------------------------------------------------------------===//
5624
5625/// ParseMDNodeVector
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005626/// ::= { Element (',' Element)* }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00005627/// Element
5628/// ::= 'null' | TypeAndValue
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00005629bool LLParser::ParseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
David Majnemer06f960d2014-12-11 20:44:09 +00005630 if (ParseToken(lltok::lbrace, "expected '{' here"))
5631 return true;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005632
Dan Gohman1e0213a2010-07-13 19:33:27 +00005633 // Check for an empty list.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005634 if (EatIfPresent(lltok::rbrace))
Dan Gohman1e0213a2010-07-13 19:33:27 +00005635 return false;
5636
Nick Lewycky49f89192009-04-04 07:22:01 +00005637 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00005638 // Null is a special case since it is typeless.
5639 if (EatIfPresent(lltok::kw_null)) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005640 Elts.push_back(nullptr);
Chris Lattnera01ddfc2009-12-30 04:42:57 +00005641 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00005642 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005643
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00005644 Metadata *MD;
5645 if (ParseMetadata(MD, nullptr))
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005646 return true;
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00005647 Elts.push_back(MD);
Nick Lewycky49f89192009-04-04 07:22:01 +00005648 } while (EatIfPresent(lltok::comma));
5649
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005650 return ParseToken(lltok::rbrace, "expected end of metadata node");
Nick Lewycky49f89192009-04-04 07:22:01 +00005651}
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00005652
5653//===----------------------------------------------------------------------===//
5654// Use-list order directives.
5655//===----------------------------------------------------------------------===//
5656bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes,
5657 SMLoc Loc) {
5658 if (V->use_empty())
5659 return Error(Loc, "value has no uses");
5660
5661 unsigned NumUses = 0;
5662 SmallDenseMap<const Use *, unsigned, 16> Order;
5663 for (const Use &U : V->uses()) {
5664 if (++NumUses > Indexes.size())
5665 break;
5666 Order[&U] = Indexes[NumUses - 1];
5667 }
5668 if (NumUses < 2)
5669 return Error(Loc, "value only has one use");
5670 if (Order.size() != Indexes.size() || NumUses > Indexes.size())
5671 return Error(Loc, "wrong number of indexes, expected " +
5672 Twine(std::distance(V->use_begin(), V->use_end())));
5673
5674 V->sortUseList([&](const Use &L, const Use &R) {
5675 return Order.lookup(&L) < Order.lookup(&R);
5676 });
5677 return false;
5678}
5679
5680/// ParseUseListOrderIndexes
5681/// ::= '{' uint32 (',' uint32)+ '}'
5682bool LLParser::ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) {
5683 SMLoc Loc = Lex.getLoc();
5684 if (ParseToken(lltok::lbrace, "expected '{' here"))
5685 return true;
5686 if (Lex.getKind() == lltok::rbrace)
5687 return Lex.Error("expected non-empty list of uselistorder indexes");
5688
5689 // Use Offset, Max, and IsOrdered to check consistency of indexes. The
5690 // indexes should be distinct numbers in the range [0, size-1], and should
5691 // not be in order.
5692 unsigned Offset = 0;
5693 unsigned Max = 0;
5694 bool IsOrdered = true;
5695 assert(Indexes.empty() && "Expected empty order vector");
5696 do {
5697 unsigned Index;
5698 if (ParseUInt32(Index))
5699 return true;
5700
5701 // Update consistency checks.
5702 Offset += Index - Indexes.size();
5703 Max = std::max(Max, Index);
5704 IsOrdered &= Index == Indexes.size();
5705
5706 Indexes.push_back(Index);
5707 } while (EatIfPresent(lltok::comma));
5708
5709 if (ParseToken(lltok::rbrace, "expected '}' here"))
5710 return true;
5711
5712 if (Indexes.size() < 2)
5713 return Error(Loc, "expected >= 2 uselistorder indexes");
5714 if (Offset != 0 || Max >= Indexes.size())
5715 return Error(Loc, "expected distinct uselistorder indexes in range [0, size)");
5716 if (IsOrdered)
5717 return Error(Loc, "expected uselistorder indexes to change the order");
5718
5719 return false;
5720}
5721
5722/// ParseUseListOrder
5723/// ::= 'uselistorder' Type Value ',' UseListOrderIndexes
5724bool LLParser::ParseUseListOrder(PerFunctionState *PFS) {
5725 SMLoc Loc = Lex.getLoc();
5726 if (ParseToken(lltok::kw_uselistorder, "expected uselistorder directive"))
5727 return true;
5728
5729 Value *V;
5730 SmallVector<unsigned, 16> Indexes;
5731 if (ParseTypeAndValue(V, PFS) ||
5732 ParseToken(lltok::comma, "expected comma in uselistorder directive") ||
5733 ParseUseListOrderIndexes(Indexes))
5734 return true;
5735
5736 return sortUseListOrder(V, Indexes, Loc);
5737}
5738
5739/// ParseUseListOrderBB
5740/// ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes
5741bool LLParser::ParseUseListOrderBB() {
5742 assert(Lex.getKind() == lltok::kw_uselistorder_bb);
5743 SMLoc Loc = Lex.getLoc();
5744 Lex.Lex();
5745
5746 ValID Fn, Label;
5747 SmallVector<unsigned, 16> Indexes;
5748 if (ParseValID(Fn) ||
5749 ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
5750 ParseValID(Label) ||
5751 ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
5752 ParseUseListOrderIndexes(Indexes))
5753 return true;
5754
5755 // Check the function.
5756 GlobalValue *GV;
5757 if (Fn.Kind == ValID::t_GlobalName)
5758 GV = M->getNamedValue(Fn.StrVal);
5759 else if (Fn.Kind == ValID::t_GlobalID)
5760 GV = Fn.UIntVal < NumberedVals.size() ? NumberedVals[Fn.UIntVal] : nullptr;
5761 else
5762 return Error(Fn.Loc, "expected function name in uselistorder_bb");
5763 if (!GV)
5764 return Error(Fn.Loc, "invalid function forward reference in uselistorder_bb");
5765 auto *F = dyn_cast<Function>(GV);
5766 if (!F)
5767 return Error(Fn.Loc, "expected function name in uselistorder_bb");
5768 if (F->isDeclaration())
5769 return Error(Fn.Loc, "invalid declaration in uselistorder_bb");
5770
5771 // Check the basic block.
5772 if (Label.Kind == ValID::t_LocalID)
5773 return Error(Label.Loc, "invalid numeric label in uselistorder_bb");
5774 if (Label.Kind != ValID::t_LocalName)
5775 return Error(Label.Loc, "expected basic block name in uselistorder_bb");
5776 Value *V = F->getValueSymbolTable().lookup(Label.StrVal);
5777 if (!V)
5778 return Error(Label.Loc, "invalid basic block in uselistorder_bb");
5779 if (!isa<BasicBlock>(V))
5780 return Error(Label.Loc, "expected basic block in uselistorder_bb");
5781
5782 return sortUseListOrder(V, Indexes, Loc);
5783}