blob: b7818bbf52738dd3b196a8782a28cb99d499fe84 [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"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/InlineAsm.h"
21#include "llvm/IR/Instructions.h"
Manman Ren209b17c2013-09-28 00:22:27 +000022#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
25#include "llvm/IR/ValueSymbolTable.h"
Torok Edwin56d06592009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +000027#include "llvm/Support/SaveAndRestore.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000028#include "llvm/Support/raw_ostream.h"
29using namespace llvm;
30
Chris Lattner229907c2011-07-18 04:54:35 +000031static std::string getTypeString(Type *T) {
Alp Tokere69170a2014-06-26 22:52:05 +000032 std::string Result;
33 raw_string_ostream Tmp(Result);
34 Tmp << *T;
35 return Tmp.str();
Chris Lattner0f214eb2011-06-18 21:18:23 +000036}
37
Chris Lattner3822f632009-01-02 08:05:26 +000038/// Run: module ::= toplevelentity*
Chris Lattnerad6f3352009-01-04 20:44:11 +000039bool LLParser::Run() {
Chris Lattner3822f632009-01-02 08:05:26 +000040 // Prime the lexer.
41 Lex.Lex();
42
Chris Lattnerad6f3352009-01-04 20:44:11 +000043 return ParseTopLevelEntities() ||
44 ValidateEndOfModule();
Chris Lattnerac161bf2009-01-02 07:01:27 +000045}
46
47/// ValidateEndOfModule - Do final validity and sanity checks at the end of the
48/// module.
49bool LLParser::ValidateEndOfModule() {
Chris Lattner8eff0152010-04-01 05:14:45 +000050 // Handle any instruction metadata forward references.
51 if (!ForwardRefInstMetadata.empty()) {
52 for (DenseMap<Instruction*, std::vector<MDRef> >::iterator
53 I = ForwardRefInstMetadata.begin(), E = ForwardRefInstMetadata.end();
54 I != E; ++I) {
55 Instruction *Inst = I->first;
56 const std::vector<MDRef> &MDList = I->second;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000057
Chris Lattner8eff0152010-04-01 05:14:45 +000058 for (unsigned i = 0, e = MDList.size(); i != e; ++i) {
59 unsigned SlotNo = MDList[i].MDSlot;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000060
Craig Topper2617dcc2014-04-15 06:32:26 +000061 if (SlotNo >= NumberedMetadata.size() ||
62 NumberedMetadata[SlotNo] == nullptr)
Chris Lattner8eff0152010-04-01 05:14:45 +000063 return Error(MDList[i].Loc, "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +000064 Twine(SlotNo) + "'");
Chris Lattner8eff0152010-04-01 05:14:45 +000065 Inst->setMetadata(MDList[i].MDKind, NumberedMetadata[SlotNo]);
66 }
67 }
68 ForwardRefInstMetadata.clear();
69 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +000070
Manman Ren209b17c2013-09-28 00:22:27 +000071 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
72 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
73
Bill Wendlingb32b0412013-02-08 06:32:06 +000074 // Handle any function attribute group forward references.
75 for (std::map<Value*, std::vector<unsigned> >::iterator
76 I = ForwardRefAttrGroups.begin(), E = ForwardRefAttrGroups.end();
77 I != E; ++I) {
78 Value *V = I->first;
79 std::vector<unsigned> &Vec = I->second;
80 AttrBuilder B;
81
82 for (std::vector<unsigned>::iterator VI = Vec.begin(), VE = Vec.end();
83 VI != VE; ++VI)
84 B.merge(NumberedAttrBuilders[*VI]);
85
86 if (Function *Fn = dyn_cast<Function>(V)) {
87 AttributeSet AS = Fn->getAttributes();
88 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
89 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
90 AS.getFnAttributes());
91
92 FnAttrs.merge(B);
93
94 // If the alignment was parsed as an attribute, move to the alignment
95 // field.
96 if (FnAttrs.hasAlignmentAttr()) {
97 Fn->setAlignment(FnAttrs.getAlignment());
98 FnAttrs.removeAttribute(Attribute::Alignment);
99 }
100
101 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
102 AttributeSet::get(Context,
103 AttributeSet::FunctionIndex,
104 FnAttrs));
105 Fn->setAttributes(AS);
106 } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
107 AttributeSet AS = CI->getAttributes();
108 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
109 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
110 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +0000111 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000112 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
113 AttributeSet::get(Context,
114 AttributeSet::FunctionIndex,
115 FnAttrs));
116 CI->setAttributes(AS);
117 } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
118 AttributeSet AS = II->getAttributes();
119 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
120 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
121 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +0000122 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000123 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
124 AttributeSet::get(Context,
125 AttributeSet::FunctionIndex,
126 FnAttrs));
127 II->setAttributes(AS);
128 } else {
129 llvm_unreachable("invalid object with forward attribute group reference");
130 }
131 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000132
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000133 // If there are entries in ForwardRefBlockAddresses at this point, the
134 // function was never defined.
135 if (!ForwardRefBlockAddresses.empty())
136 return Error(ForwardRefBlockAddresses.begin()->first.Loc,
137 "expected function name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000138
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000139 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i)
140 if (NumberedTypes[i].second.isValid())
141 return Error(NumberedTypes[i].second,
142 "use of undefined type '%" + Twine(i) + "'");
143
144 for (StringMap<std::pair<Type*, LocTy> >::iterator I =
145 NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I)
146 if (I->second.second.isValid())
147 return Error(I->second.second,
148 "use of undefined type named '" + I->getKey() + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000149
David Majnemerdad0a642014-06-27 18:19:56 +0000150 if (!ForwardRefComdats.empty())
151 return Error(ForwardRefComdats.begin()->second,
152 "use of undefined comdat '$" +
153 ForwardRefComdats.begin()->first + "'");
154
Chris Lattnerac161bf2009-01-02 07:01:27 +0000155 if (!ForwardRefVals.empty())
156 return Error(ForwardRefVals.begin()->second.second,
157 "use of undefined value '@" + ForwardRefVals.begin()->first +
158 "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000159
Chris Lattnerac161bf2009-01-02 07:01:27 +0000160 if (!ForwardRefValIDs.empty())
161 return Error(ForwardRefValIDs.begin()->second.second,
162 "use of undefined value '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000163 Twine(ForwardRefValIDs.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000164
Devang Pateld2541152009-07-08 19:23:54 +0000165 if (!ForwardRefMDNodes.empty())
166 return Error(ForwardRefMDNodes.begin()->second.second,
167 "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000168 Twine(ForwardRefMDNodes.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000169
Devang Pateld2541152009-07-08 19:23:54 +0000170
Chris Lattnerac161bf2009-01-02 07:01:27 +0000171 // Look for intrinsic functions and CallInst that need to be upgraded
172 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
173 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000174
Manman Ren8b4306c2013-12-02 21:29:56 +0000175 UpgradeDebugInfo(*M);
176
Chris Lattnerac161bf2009-01-02 07:01:27 +0000177 return false;
178}
179
180//===----------------------------------------------------------------------===//
181// Top-Level Entities
182//===----------------------------------------------------------------------===//
183
184bool LLParser::ParseTopLevelEntities() {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000185 while (1) {
186 switch (Lex.getKind()) {
187 default: return TokError("expected top-level entity");
188 case lltok::Eof: return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000189 case lltok::kw_declare: if (ParseDeclare()) return true; break;
190 case lltok::kw_define: if (ParseDefine()) return true; break;
191 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
192 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
Bill Wendling706d3d62012-11-28 08:41:48 +0000193 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000194 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000195 case lltok::LocalVar: if (ParseNamedType()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000196 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000197 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
David Majnemerdad0a642014-06-27 18:19:56 +0000198 case lltok::ComdatVar: if (parseComdat()) return true; break;
Chris Lattner1eed2d62009-12-30 04:56:59 +0000199 case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break;
Bill Wendling63b88192013-02-06 06:52:58 +0000200 case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000201
202 // The Global variable production with no name can have many different
203 // optional leading prefixes, the production is:
Nico Rieck7157bb72014-01-14 15:22:47 +0000204 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
205 // OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Rafael Espindola45e6c192011-01-08 16:42:36 +0000206 // ('constant'|'global') ...
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000207 case lltok::kw_private: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000208 case lltok::kw_internal: // OptionalLinkage
209 case lltok::kw_weak: // OptionalLinkage
210 case lltok::kw_weak_odr: // OptionalLinkage
211 case lltok::kw_linkonce: // OptionalLinkage
212 case lltok::kw_linkonce_odr: // OptionalLinkage
213 case lltok::kw_appending: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000214 case lltok::kw_common: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000215 case lltok::kw_extern_weak: // OptionalLinkage
Rafael Espindola52b74422014-06-03 20:00:20 +0000216 case lltok::kw_external: // OptionalLinkage
217 case lltok::kw_default: // OptionalVisibility
218 case lltok::kw_hidden: // OptionalVisibility
219 case lltok::kw_protected: // OptionalVisibility
Rafael Espindola63e92fb2014-06-03 20:07:32 +0000220 case lltok::kw_dllimport: // OptionalDLLStorageClass
221 case lltok::kw_dllexport: // OptionalDLLStorageClass
Rafael Espindola52b74422014-06-03 20:00:20 +0000222 case lltok::kw_thread_local: // OptionalThreadLocal
223 case lltok::kw_addrspace: // OptionalAddrSpace
224 case lltok::kw_constant: // GlobalType
225 case lltok::kw_global: { // GlobalType
Nico Rieck7157bb72014-01-14 15:22:47 +0000226 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000227 bool UnnamedAddr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000228 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola52b74422014-06-03 20:00:20 +0000229 bool HasLinkage;
230 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000231 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000232 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000233 ParseOptionalThreadLocal(TLM) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000234 parseOptionalUnnamedAddr(UnnamedAddr) ||
Rafael Espindola52b74422014-06-03 20:00:20 +0000235 ParseGlobal("", SMLoc(), Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000236 DLLStorageClass, TLM, UnnamedAddr))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000237 return true;
238 break;
239 }
Bill Wendlinga7c38772013-02-09 15:48:49 +0000240
241 case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break;
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000242 case lltok::kw_uselistorder: if (ParseUseListOrder()) return true; break;
243 case lltok::kw_uselistorder_bb:
244 if (ParseUseListOrderBB()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000245 }
246 }
247}
248
249
250/// toplevelentity
251/// ::= 'module' 'asm' STRINGCONSTANT
252bool LLParser::ParseModuleAsm() {
253 assert(Lex.getKind() == lltok::kw_module);
254 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000255
256 std::string AsmStr;
Chris Lattner3822f632009-01-02 08:05:26 +0000257 if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
258 ParseStringConstant(AsmStr)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000259
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000260 M->appendModuleInlineAsm(AsmStr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000261 return false;
262}
263
264/// toplevelentity
265/// ::= 'target' 'triple' '=' STRINGCONSTANT
266/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
267bool LLParser::ParseTargetDefinition() {
268 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3822f632009-01-02 08:05:26 +0000269 std::string Str;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000270 switch (Lex.Lex()) {
271 default: return TokError("unknown target property");
272 case lltok::kw_triple:
273 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000274 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
275 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000276 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000277 M->setTargetTriple(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000278 return false;
279 case lltok::kw_datalayout:
280 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000281 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
282 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000283 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000284 M->setDataLayout(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000285 return false;
286 }
287}
288
Bill Wendling706d3d62012-11-28 08:41:48 +0000289/// toplevelentity
290/// ::= 'deplibs' '=' '[' ']'
291/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
292/// FIXME: Remove in 4.0. Currently parse, but ignore.
293bool LLParser::ParseDepLibs() {
294 assert(Lex.getKind() == lltok::kw_deplibs);
295 Lex.Lex();
296 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
297 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
298 return true;
299
300 if (EatIfPresent(lltok::rsquare))
301 return false;
302
303 do {
304 std::string Str;
305 if (ParseStringConstant(Str)) return true;
306 } while (EatIfPresent(lltok::comma));
307
308 return ParseToken(lltok::rsquare, "expected ']' at end of list");
309}
310
Dan Gohman466876b2009-08-12 23:32:33 +0000311/// ParseUnnamedType:
Dan Gohman466876b2009-08-12 23:32:33 +0000312/// ::= LocalVarID '=' 'type' type
Chris Lattnerac161bf2009-01-02 07:01:27 +0000313bool LLParser::ParseUnnamedType() {
Chris Lattner07037362011-06-18 23:51:31 +0000314 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000315 unsigned TypeID = Lex.getUIntVal();
Chris Lattner8936d2b2011-06-19 00:03:46 +0000316 Lex.Lex(); // eat LocalVarID;
317
318 if (ParseToken(lltok::equal, "expected '=' after name") ||
319 ParseToken(lltok::kw_type, "expected 'type' after '='"))
320 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000321
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000322 if (TypeID >= NumberedTypes.size())
323 NumberedTypes.resize(TypeID+1);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000324
Craig Topper2617dcc2014-04-15 06:32:26 +0000325 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000326 if (ParseStructDefinition(TypeLoc, "",
327 NumberedTypes[TypeID], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000328
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000329 if (!isa<StructType>(Result)) {
330 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
331 if (Entry.first)
332 return Error(TypeLoc, "non-struct types may not be recursive");
333 Entry.first = Result;
334 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000335 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000336
Chris Lattnerac161bf2009-01-02 07:01:27 +0000337 return false;
338}
339
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000340
Chris Lattnerac161bf2009-01-02 07:01:27 +0000341/// toplevelentity
342/// ::= LocalVar '=' 'type' type
343bool LLParser::ParseNamedType() {
344 std::string Name = Lex.getStrVal();
345 LocTy NameLoc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000346 Lex.Lex(); // eat LocalVar.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000347
Chris Lattner3822f632009-01-02 08:05:26 +0000348 if (ParseToken(lltok::equal, "expected '=' after name") ||
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000349 ParseToken(lltok::kw_type, "expected 'type' after name"))
Chris Lattner3822f632009-01-02 08:05:26 +0000350 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000351
Craig Topper2617dcc2014-04-15 06:32:26 +0000352 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000353 if (ParseStructDefinition(NameLoc, Name,
354 NamedTypes[Name], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000355
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000356 if (!isa<StructType>(Result)) {
357 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
358 if (Entry.first)
359 return Error(NameLoc, "non-struct types may not be recursive");
360 Entry.first = Result;
361 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000362 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000363
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000364 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000365}
366
367
368/// toplevelentity
369/// ::= 'declare' FunctionHeader
370bool LLParser::ParseDeclare() {
371 assert(Lex.getKind() == lltok::kw_declare);
372 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000373
Chris Lattnerac161bf2009-01-02 07:01:27 +0000374 Function *F;
375 return ParseFunctionHeader(F, false);
376}
377
378/// toplevelentity
379/// ::= 'define' FunctionHeader '{' ...
380bool LLParser::ParseDefine() {
381 assert(Lex.getKind() == lltok::kw_define);
382 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000383
Chris Lattnerac161bf2009-01-02 07:01:27 +0000384 Function *F;
Chris Lattner3822f632009-01-02 08:05:26 +0000385 return ParseFunctionHeader(F, true) ||
386 ParseFunctionBody(*F);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000387}
388
Chris Lattner3822f632009-01-02 08:05:26 +0000389/// ParseGlobalType
390/// ::= 'constant'
391/// ::= 'global'
Chris Lattnerac161bf2009-01-02 07:01:27 +0000392bool LLParser::ParseGlobalType(bool &IsConstant) {
393 if (Lex.getKind() == lltok::kw_constant)
394 IsConstant = true;
395 else if (Lex.getKind() == lltok::kw_global)
396 IsConstant = false;
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000397 else {
398 IsConstant = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000399 return TokError("expected 'global' or 'constant'");
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000400 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000401 Lex.Lex();
402 return false;
403}
404
Dan Gohman466876b2009-08-12 23:32:33 +0000405/// ParseUnnamedGlobal:
406/// OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000407/// OptionalLinkage OptionalVisibility OptionalDLLStorageClass
408/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000409/// GlobalID '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000410/// GlobalID '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
411/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000412bool LLParser::ParseUnnamedGlobal() {
413 unsigned VarID = NumberedVals.size();
414 std::string Name;
415 LocTy NameLoc = Lex.getLoc();
416
417 // Handle the GlobalID form.
418 if (Lex.getKind() == lltok::GlobalID) {
419 if (Lex.getUIntVal() != VarID)
420 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000421 Twine(VarID) + "'");
Dan Gohman466876b2009-08-12 23:32:33 +0000422 Lex.Lex(); // eat GlobalID;
423
424 if (ParseToken(lltok::equal, "expected '=' after name"))
425 return true;
426 }
427
428 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000429 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000430 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000431 bool UnnamedAddr;
Dan Gohman466876b2009-08-12 23:32:33 +0000432 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000433 ParseOptionalVisibility(Visibility) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000434 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000435 ParseOptionalThreadLocal(TLM) ||
436 parseOptionalUnnamedAddr(UnnamedAddr))
Dan Gohman466876b2009-08-12 23:32:33 +0000437 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000438
Rafael Espindola464fe022014-07-30 22:51:54 +0000439 if (Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000440 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000441 DLLStorageClass, TLM, UnnamedAddr);
Rafael Espindola464fe022014-07-30 22:51:54 +0000442 return ParseAlias(Name, NameLoc, Linkage, Visibility, DLLStorageClass, TLM,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000443 UnnamedAddr);
Dan Gohman466876b2009-08-12 23:32:33 +0000444}
445
Chris Lattnerac161bf2009-01-02 07:01:27 +0000446/// ParseNamedGlobal:
447/// GlobalVar '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000448/// GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
449/// ... -> global variable
Chris Lattnerac161bf2009-01-02 07:01:27 +0000450bool LLParser::ParseNamedGlobal() {
451 assert(Lex.getKind() == lltok::GlobalVar);
452 LocTy NameLoc = Lex.getLoc();
453 std::string Name = Lex.getStrVal();
454 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000455
Chris Lattnerac161bf2009-01-02 07:01:27 +0000456 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000457 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000458 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000459 bool UnnamedAddr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000460 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
461 ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000462 ParseOptionalVisibility(Visibility) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000463 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000464 ParseOptionalThreadLocal(TLM) ||
465 parseOptionalUnnamedAddr(UnnamedAddr))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000466 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000467
Rafael Espindola464fe022014-07-30 22:51:54 +0000468 if (Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000469 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000470 DLLStorageClass, TLM, UnnamedAddr);
Rafael Espindola464fe022014-07-30 22:51:54 +0000471
472 return ParseAlias(Name, NameLoc, Linkage, Visibility, DLLStorageClass, TLM,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000473 UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000474}
475
David Majnemerdad0a642014-06-27 18:19:56 +0000476bool LLParser::parseComdat() {
477 assert(Lex.getKind() == lltok::ComdatVar);
478 std::string Name = Lex.getStrVal();
479 LocTy NameLoc = Lex.getLoc();
480 Lex.Lex();
481
482 if (ParseToken(lltok::equal, "expected '=' here"))
483 return true;
484
485 if (ParseToken(lltok::kw_comdat, "expected comdat keyword"))
486 return TokError("expected comdat type");
487
488 Comdat::SelectionKind SK;
489 switch (Lex.getKind()) {
490 default:
491 return TokError("unknown selection kind");
492 case lltok::kw_any:
493 SK = Comdat::Any;
494 break;
495 case lltok::kw_exactmatch:
496 SK = Comdat::ExactMatch;
497 break;
498 case lltok::kw_largest:
499 SK = Comdat::Largest;
500 break;
501 case lltok::kw_noduplicates:
502 SK = Comdat::NoDuplicates;
503 break;
504 case lltok::kw_samesize:
505 SK = Comdat::SameSize;
506 break;
507 }
508 Lex.Lex();
509
510 // See if the comdat was forward referenced, if so, use the comdat.
511 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
512 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
513 if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(Name))
514 return Error(NameLoc, "redefinition of comdat '$" + Name + "'");
515
516 Comdat *C;
517 if (I != ComdatSymTab.end())
518 C = &I->second;
519 else
520 C = M->getOrInsertComdat(Name);
521 C->setSelectionKind(SK);
522
523 return false;
524}
525
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000526// MDString:
527// ::= '!' STRINGCONSTANT
Chris Lattner1797fc72009-12-29 21:53:55 +0000528bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000529 std::string Str;
530 if (ParseStringConstant(Str)) return true;
Eli Bendersky5d5e18d2014-06-25 15:41:00 +0000531 llvm::UpgradeMDStringConstant(Str);
Chris Lattner1797fc72009-12-29 21:53:55 +0000532 Result = MDString::get(Context, Str);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000533 return false;
534}
535
536// MDNode:
537// ::= '!' MDNodeNumber
Chris Lattner8eff0152010-04-01 05:14:45 +0000538//
539/// This version of ParseMDNodeID returns the slot number and null in the case
540/// of a forward reference.
541bool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) {
542 // !{ ..., !42, ... }
543 if (ParseUInt32(SlotNo)) return true;
544
545 // Check existing MDNode.
Craig Topper2617dcc2014-04-15 06:32:26 +0000546 if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != nullptr)
Chris Lattner8eff0152010-04-01 05:14:45 +0000547 Result = NumberedMetadata[SlotNo];
548 else
Craig Topper2617dcc2014-04-15 06:32:26 +0000549 Result = nullptr;
Chris Lattner8eff0152010-04-01 05:14:45 +0000550 return false;
551}
552
Chris Lattner6dac02a2009-12-30 04:15:23 +0000553bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000554 // !{ ..., !42, ... }
555 unsigned MID = 0;
Chris Lattner8eff0152010-04-01 05:14:45 +0000556 if (ParseMDNodeID(Result, MID)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000557
Chris Lattner8eff0152010-04-01 05:14:45 +0000558 // If not a forward reference, just return it now.
559 if (Result) return false;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000560
Chris Lattner8eff0152010-04-01 05:14:45 +0000561 // Otherwise, create MDNode forward reference.
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000562 MDNode *FwdNode = MDNode::getTemporary(Context, None);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000563 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000564
Chris Lattnerfc58af22009-12-30 04:51:58 +0000565 if (NumberedMetadata.size() <= MID)
566 NumberedMetadata.resize(MID+1);
567 NumberedMetadata[MID] = FwdNode;
Chris Lattner1797fc72009-12-29 21:53:55 +0000568 Result = FwdNode;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000569 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000570}
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000571
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000572/// ParseNamedMetadata:
Devang Patelbe626972009-07-29 00:34:02 +0000573/// !foo = !{ !1, !2 }
574bool LLParser::ParseNamedMetadata() {
Chris Lattnereafe4de2009-12-30 05:02:06 +0000575 assert(Lex.getKind() == lltok::MetadataVar);
Devang Patelbe626972009-07-29 00:34:02 +0000576 std::string Name = Lex.getStrVal();
Chris Lattnereafe4de2009-12-30 05:02:06 +0000577 Lex.Lex();
Devang Patelbe626972009-07-29 00:34:02 +0000578
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000579 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000580 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000581 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Patelbe626972009-07-29 00:34:02 +0000582 return true;
583
Dan Gohman2637cc12010-07-21 23:38:33 +0000584 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000585 if (Lex.getKind() != lltok::rbrace)
586 do {
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000587 if (ParseToken(lltok::exclaim, "Expected '!' here"))
588 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000589
Craig Topper2617dcc2014-04-15 06:32:26 +0000590 MDNode *N = nullptr;
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000591 if (ParseMDNodeID(N)) return true;
Dan Gohman2637cc12010-07-21 23:38:33 +0000592 NMD->addOperand(N);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000593 } while (EatIfPresent(lltok::comma));
Devang Patelbe626972009-07-29 00:34:02 +0000594
595 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
596 return true;
597
Devang Patelbe626972009-07-29 00:34:02 +0000598 return false;
599}
600
Devang Patel39e64d42009-07-01 19:21:12 +0000601/// ParseStandaloneMetadata:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000602/// !42 = !{...}
Devang Patel39e64d42009-07-01 19:21:12 +0000603bool LLParser::ParseStandaloneMetadata() {
Chris Lattner1eed2d62009-12-30 04:56:59 +0000604 assert(Lex.getKind() == lltok::exclaim);
Devang Patel39e64d42009-07-01 19:21:12 +0000605 Lex.Lex();
606 unsigned MetadataID = 0;
Devang Patel39e64d42009-07-01 19:21:12 +0000607
608 LocTy TyLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +0000609 Type *Ty = nullptr;
Devang Patele059ba6e2009-07-23 01:07:34 +0000610 SmallVector<Value *, 16> Elts;
Chris Lattner278bc952009-12-29 22:40:21 +0000611 if (ParseUInt32(MetadataID) ||
612 ParseToken(lltok::equal, "expected '=' here") ||
613 ParseType(Ty, TyLoc) ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000614 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner278bc952009-12-29 22:40:21 +0000615 ParseToken(lltok::lbrace, "Expected '{' here") ||
Craig Topper2617dcc2014-04-15 06:32:26 +0000616 ParseMDNodeVector(Elts, nullptr) ||
Chris Lattner278bc952009-12-29 22:40:21 +0000617 ParseToken(lltok::rbrace, "expected end of metadata node"))
Devang Patele059ba6e2009-07-23 01:07:34 +0000618 return true;
619
Jay Foad5514afe2011-04-21 19:59:31 +0000620 MDNode *Init = MDNode::get(Context, Elts);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000621
Chris Lattnerfc58af22009-12-30 04:51:58 +0000622 // See if this was forward referenced, if so, handle it.
Chris Lattner218b22f2009-12-29 21:43:58 +0000623 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
Devang Pateld2541152009-07-08 19:23:54 +0000624 FI = ForwardRefMDNodes.find(MetadataID);
625 if (FI != ForwardRefMDNodes.end()) {
Dan Gohman16a5d982010-08-20 22:02:26 +0000626 MDNode *Temp = FI->second.first;
627 Temp->replaceAllUsesWith(Init);
628 MDNode::deleteTemporary(Temp);
Devang Pateld2541152009-07-08 19:23:54 +0000629 ForwardRefMDNodes.erase(FI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000630
Chris Lattnerfc58af22009-12-30 04:51:58 +0000631 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
632 } else {
633 if (MetadataID >= NumberedMetadata.size())
634 NumberedMetadata.resize(MetadataID+1);
635
Craig Topper2617dcc2014-04-15 06:32:26 +0000636 if (NumberedMetadata[MetadataID] != nullptr)
Chris Lattnerfc58af22009-12-30 04:51:58 +0000637 return TokError("Metadata id is already used");
638 NumberedMetadata[MetadataID] = Init;
Devang Pateld2541152009-07-08 19:23:54 +0000639 }
640
Devang Patel39e64d42009-07-01 19:21:12 +0000641 return false;
642}
643
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000644static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
645 return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
646 (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
647}
648
Chris Lattnerac161bf2009-01-02 07:01:27 +0000649/// ParseAlias:
Rafael Espindola464fe022014-07-30 22:51:54 +0000650/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility
651/// OptionalDLLStorageClass OptionalThreadLocal
652/// OptionalUnNammedAddr 'alias' Aliasee
Rafael Espindola6b238632014-05-16 19:35:39 +0000653///
Chris Lattnerac161bf2009-01-02 07:01:27 +0000654/// Aliasee
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000655/// ::= TypeAndValue
Chris Lattnerac161bf2009-01-02 07:01:27 +0000656///
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000657/// Everything through OptionalUnNammedAddr has already been parsed.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000658///
Rafael Espindola464fe022014-07-30 22:51:54 +0000659bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc, unsigned L,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000660 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000661 GlobalVariable::ThreadLocalMode TLM,
662 bool UnnamedAddr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000663 assert(Lex.getKind() == lltok::kw_alias);
664 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000665
Rafael Espindola78527052013-10-06 15:10:43 +0000666 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
667
Rafael Espindolacaa43562013-10-09 16:07:32 +0000668 if(!GlobalAlias::isValidLinkage(Linkage))
Rafael Espindola464fe022014-07-30 22:51:54 +0000669 return Error(NameLoc, "invalid linkage type for alias");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000670
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000671 if (!isValidVisibilityForLinkage(Visibility, L))
Rafael Espindola464fe022014-07-30 22:51:54 +0000672 return Error(NameLoc,
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000673 "symbol with local linkage must have default visibility");
674
Rafael Espindola64c1e182014-06-03 02:41:57 +0000675 Constant *Aliasee;
676 LocTy AliaseeLoc = Lex.getLoc();
677 if (Lex.getKind() != lltok::kw_bitcast &&
678 Lex.getKind() != lltok::kw_getelementptr &&
679 Lex.getKind() != lltok::kw_addrspacecast &&
680 Lex.getKind() != lltok::kw_inttoptr) {
681 if (ParseGlobalTypeAndValue(Aliasee))
Rafael Espindola6b238632014-05-16 19:35:39 +0000682 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000683 } else {
Rafael Espindola64c1e182014-06-03 02:41:57 +0000684 // The bitcast dest type is not present, it is implied by the dest type.
685 ValID ID;
686 if (ParseValID(ID))
687 return true;
688 if (ID.Kind != ValID::t_Constant)
689 return Error(AliaseeLoc, "invalid aliasee");
690 Aliasee = ID.ConstantVal;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000691 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000692
Rafael Espindola64c1e182014-06-03 02:41:57 +0000693 Type *AliaseeType = Aliasee->getType();
694 auto *PTy = dyn_cast<PointerType>(AliaseeType);
695 if (!PTy)
696 return Error(AliaseeLoc, "An alias must have pointer type");
697 Type *Ty = PTy->getElementType();
698 unsigned AddrSpace = PTy->getAddressSpace();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000699
700 // Okay, create the alias but do not insert it into the module yet.
Rafael Espindola4fe00942014-05-16 13:34:04 +0000701 std::unique_ptr<GlobalAlias> GA(
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000702 GlobalAlias::create(Ty, AddrSpace, (GlobalValue::LinkageTypes)Linkage,
703 Name, Aliasee, /*Parent*/ nullptr));
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000704 GA->setThreadLocalMode(TLM);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000705 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000706 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000707 GA->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000708
Chris Lattnerac161bf2009-01-02 07:01:27 +0000709 // See if this value already exists in the symbol table. If so, it is either
710 // a redefinition or a definition of a forward reference.
Chris Lattnere38317f2009-10-25 23:22:50 +0000711 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000712 // See if this was a redefinition. If so, there is no entry in
713 // ForwardRefVals.
714 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
715 I = ForwardRefVals.find(Name);
716 if (I == ForwardRefVals.end())
717 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
718
719 // Otherwise, this was a definition of forward ref. Verify that types
720 // agree.
721 if (Val->getType() != GA->getType())
722 return Error(NameLoc,
723 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000724
Chris Lattnerac161bf2009-01-02 07:01:27 +0000725 // If they agree, just RAUW the old value with the alias and remove the
726 // forward ref info.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000727 Val->replaceAllUsesWith(GA.get());
Chris Lattnerac161bf2009-01-02 07:01:27 +0000728 Val->eraseFromParent();
729 ForwardRefVals.erase(I);
730 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000731
Chris Lattnerac161bf2009-01-02 07:01:27 +0000732 // Insert into the module, we know its name won't collide now.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000733 M->getAliasList().push_back(GA.get());
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000734 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000735
Rafael Espindolaaa273822014-05-09 21:49:17 +0000736 // The module owns this now
737 GA.release();
738
Chris Lattnerac161bf2009-01-02 07:01:27 +0000739 return false;
740}
741
742/// ParseGlobal
Nico Rieck7157bb72014-01-14 15:22:47 +0000743/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000744/// OptionalThreadLocal OptionalUnNammedAddr OptionalAddrSpace
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000745/// OptionalExternallyInitialized GlobalType Type Const
Nico Rieck7157bb72014-01-14 15:22:47 +0000746/// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000747/// OptionalThreadLocal OptionalUnNammedAddr OptionalAddrSpace
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000748/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerac161bf2009-01-02 07:01:27 +0000749///
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000750/// Everything up to and including OptionalUnNammedAddr has been parsed
David Majnemerc4ab61c2014-03-09 06:41:58 +0000751/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000752///
753bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
754 unsigned Linkage, bool HasLinkage,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000755 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000756 GlobalVariable::ThreadLocalMode TLM,
757 bool UnnamedAddr) {
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000758 if (!isValidVisibilityForLinkage(Visibility, Linkage))
759 return Error(NameLoc,
760 "symbol with local linkage must have default visibility");
761
Chris Lattnerac161bf2009-01-02 07:01:27 +0000762 unsigned AddrSpace;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000763 bool IsConstant, IsExternallyInitialized;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000764 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000765 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000766
Craig Topper2617dcc2014-04-15 06:32:26 +0000767 Type *Ty = nullptr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000768 if (ParseOptionalAddrSpace(AddrSpace) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000769 ParseOptionalToken(lltok::kw_externally_initialized,
770 IsExternallyInitialized,
771 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000772 ParseGlobalType(IsConstant) ||
773 ParseType(Ty, TyLoc))
774 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000775
Chris Lattnerac161bf2009-01-02 07:01:27 +0000776 // If the linkage is specified and is external, then no initializer is
777 // present.
Craig Topper2617dcc2014-04-15 06:32:26 +0000778 Constant *Init = nullptr;
Nico Rieck7157bb72014-01-14 15:22:47 +0000779 if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerac161bf2009-01-02 07:01:27 +0000780 Linkage != GlobalValue::ExternalLinkage)) {
781 if (ParseGlobalValue(Ty, Init))
782 return true;
783 }
784
Duncan Sands19d0b472010-02-16 11:11:14 +0000785 if (Ty->isFunctionTy() || Ty->isLabelTy())
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000786 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000787
Craig Topper2617dcc2014-04-15 06:32:26 +0000788 GlobalVariable *GV = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000789
790 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000791 if (!Name.empty()) {
Chris Lattnere38317f2009-10-25 23:22:50 +0000792 if (GlobalValue *GVal = M->getNamedValue(Name)) {
793 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
794 return Error(NameLoc, "redefinition of global '@" + Name + "'");
795 GV = cast<GlobalVariable>(GVal);
796 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000797 } else {
798 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
799 I = ForwardRefValIDs.find(NumberedVals.size());
800 if (I != ForwardRefValIDs.end()) {
801 GV = cast<GlobalVariable>(I->second.first);
802 ForwardRefValIDs.erase(I);
803 }
804 }
805
Craig Topper2617dcc2014-04-15 06:32:26 +0000806 if (!GV) {
807 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
808 Name, nullptr, GlobalVariable::NotThreadLocal,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000809 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000810 } else {
811 if (GV->getType()->getElementType() != Ty)
812 return Error(TyLoc,
813 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000814
Chris Lattnerac161bf2009-01-02 07:01:27 +0000815 // Move the forward-reference to the correct spot in the module.
816 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
817 }
818
819 if (Name.empty())
820 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000821
Chris Lattnerac161bf2009-01-02 07:01:27 +0000822 // Set the parsed properties on the global.
823 if (Init)
824 GV->setInitializer(Init);
825 GV->setConstant(IsConstant);
826 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
827 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000828 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000829 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000830 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000831 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000832
Chris Lattnerac161bf2009-01-02 07:01:27 +0000833 // Parse attributes on the global.
834 while (Lex.getKind() == lltok::comma) {
835 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000836
Chris Lattnerac161bf2009-01-02 07:01:27 +0000837 if (Lex.getKind() == lltok::kw_section) {
838 Lex.Lex();
839 GV->setSection(Lex.getStrVal());
840 if (ParseToken(lltok::StringConstant, "expected global section string"))
841 return true;
842 } else if (Lex.getKind() == lltok::kw_align) {
843 unsigned Alignment;
844 if (ParseOptionalAlignment(Alignment)) return true;
845 GV->setAlignment(Alignment);
846 } else {
David Majnemerdad0a642014-06-27 18:19:56 +0000847 Comdat *C;
848 if (parseOptionalComdat(C))
849 return true;
850 if (C)
851 GV->setComdat(C);
852 else
853 return TokError("unknown global variable property!");
Chris Lattnerac161bf2009-01-02 07:01:27 +0000854 }
855 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000856
Chris Lattnerac161bf2009-01-02 07:01:27 +0000857 return false;
858}
859
Bill Wendling63b88192013-02-06 06:52:58 +0000860/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +0000861/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +0000862bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +0000863 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +0000864 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +0000865 Lex.Lex();
866
867 assert(Lex.getKind() == lltok::AttrGrpID);
Bill Wendling63b88192013-02-06 06:52:58 +0000868 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000869 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +0000870 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +0000871 Lex.Lex();
872
873 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +0000874 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +0000875 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +0000876 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +0000877 ParseToken(lltok::rbrace, "expected end of attribute group"))
878 return true;
879
Bill Wendlingb32b0412013-02-08 06:32:06 +0000880 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +0000881 return Error(AttrGrpLoc, "attribute group has no attributes");
882
883 return false;
884}
885
Bill Wendling8b0321d2013-02-08 00:52:31 +0000886/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +0000887/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +0000888bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
889 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000890 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +0000891 bool HaveError = false;
892
893 B.clear();
894
Bill Wendling63b88192013-02-06 06:52:58 +0000895 while (true) {
896 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +0000897 if (Token == lltok::kw_builtin)
898 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +0000899 switch (Token) {
900 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000901 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +0000902 return Error(Lex.getLoc(), "unterminated attribute group");
903 case lltok::rbrace:
904 // Finished.
905 return false;
906
Bill Wendlingb32b0412013-02-08 06:32:06 +0000907 case lltok::AttrGrpID: {
908 // Allow a function to reference an attribute group:
909 //
910 // define void @foo() #1 { ... }
911 if (inAttrGrp)
912 HaveError |=
913 Error(Lex.getLoc(),
914 "cannot have an attribute group reference in an attribute group");
915
916 unsigned AttrGrpNum = Lex.getUIntVal();
917 if (inAttrGrp) break;
918
919 // Save the reference to the attribute group. We'll fill it in later.
920 FwdRefAttrGrps.push_back(AttrGrpNum);
921 break;
922 }
Bill Wendling63b88192013-02-06 06:52:58 +0000923 // Target-dependent attributes:
924 case lltok::StringConstant: {
925 std::string Attr = Lex.getStrVal();
926 Lex.Lex();
927 std::string Val;
928 if (EatIfPresent(lltok::equal) &&
929 ParseStringConstant(Val))
930 return true;
931
932 B.addAttribute(Attr, Val);
Bill Wendlingb1ea9802013-02-10 10:12:50 +0000933 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000934 }
935
936 // Target-independent attributes:
937 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +0000938 // As a hack, we allow function alignment to be initially parsed as an
939 // attribute on a function declaration/definition or added to an attribute
940 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +0000941 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000942 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000943 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000944 if (ParseToken(lltok::equal, "expected '=' here") ||
945 ParseUInt32(Alignment))
946 return true;
947 } else {
948 if (ParseOptionalAlignment(Alignment))
949 return true;
950 }
Bill Wendling63b88192013-02-06 06:52:58 +0000951 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000952 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000953 }
954 case lltok::kw_alignstack: {
955 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000956 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000957 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000958 if (ParseToken(lltok::equal, "expected '=' here") ||
959 ParseUInt32(Alignment))
960 return true;
961 } else {
962 if (ParseOptionalStackAlignment(Alignment))
963 return true;
964 }
Bill Wendling63b88192013-02-06 06:52:58 +0000965 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000966 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000967 }
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000968 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Michael Gottesman41748d72013-06-27 00:25:01 +0000969 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
Diego Novilloc6399532013-05-24 12:26:52 +0000970 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000971 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000972 case lltok::kw_jumptable: B.addAttribute(Attribute::JumpTable); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000973 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
974 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
975 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
976 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
977 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
978 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
979 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
980 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
981 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
982 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000983 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000984 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
985 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
986 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
987 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
988 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
989 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
990 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
991 case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
992 case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
993 case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
994 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000995
996 // Error handling.
997 case lltok::kw_inreg:
998 case lltok::kw_signext:
999 case lltok::kw_zeroext:
1000 HaveError |=
1001 Error(Lex.getLoc(),
1002 "invalid use of attribute on a function");
1003 break;
1004 case lltok::kw_byval:
Hal Finkelb0407ba2014-07-18 15:51:28 +00001005 case lltok::kw_dereferenceable:
Reid Klecknera534a382013-12-19 02:14:12 +00001006 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +00001007 case lltok::kw_nest:
1008 case lltok::kw_noalias:
1009 case lltok::kw_nocapture:
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001010 case lltok::kw_nonnull:
Stephen Linb8bd2322013-04-20 05:14:40 +00001011 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +00001012 case lltok::kw_sret:
1013 HaveError |=
1014 Error(Lex.getLoc(),
1015 "invalid use of parameter-only attribute on a function");
1016 break;
Bill Wendling63b88192013-02-06 06:52:58 +00001017 }
1018
1019 Lex.Lex();
1020 }
1021}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001022
1023//===----------------------------------------------------------------------===//
1024// GlobalValue Reference/Resolution Routines.
1025//===----------------------------------------------------------------------===//
1026
1027/// GetGlobalVal - Get a value with the specified name or ID, creating a
1028/// forward reference record if needed. This can return null if the value
1029/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +00001030GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00001031 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +00001032 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001033 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001034 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001035 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001036 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001037
Chris Lattnerac161bf2009-01-02 07:01:27 +00001038 // Look this name up in the normal function symbol table.
1039 GlobalValue *Val =
1040 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001041
Chris Lattnerac161bf2009-01-02 07:01:27 +00001042 // If this is a forward reference for the value, see if we already created a
1043 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001044 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001045 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
1046 I = ForwardRefVals.find(Name);
1047 if (I != ForwardRefVals.end())
1048 Val = I->second.first;
1049 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001050
Chris Lattnerac161bf2009-01-02 07:01:27 +00001051 // If we have the value in the symbol table or fwd-ref table, return it.
1052 if (Val) {
1053 if (Val->getType() == Ty) return Val;
1054 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001055 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001056 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001057 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001058
Chris Lattnerac161bf2009-01-02 07:01:27 +00001059 // Otherwise, create a new forward reference for this value and remember it.
1060 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001061 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001062 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001063 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001064 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001065 GlobalValue::ExternalWeakLinkage, nullptr, Name,
1066 nullptr, GlobalVariable::NotThreadLocal,
Justin Holewinski898a0a02012-11-16 21:03:47 +00001067 PTy->getAddressSpace());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001068
Chris Lattnerac161bf2009-01-02 07:01:27 +00001069 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1070 return FwdVal;
1071}
1072
Chris Lattner229907c2011-07-18 04:54:35 +00001073GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1074 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001075 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001076 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001077 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001078 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001079
Craig Topper2617dcc2014-04-15 06:32:26 +00001080 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001081
Chris Lattnerac161bf2009-01-02 07:01:27 +00001082 // If this is a forward reference for the value, see if we already created a
1083 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001084 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001085 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
1086 I = ForwardRefValIDs.find(ID);
1087 if (I != ForwardRefValIDs.end())
1088 Val = I->second.first;
1089 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001090
Chris Lattnerac161bf2009-01-02 07:01:27 +00001091 // If we have the value in the symbol table or fwd-ref table, return it.
1092 if (Val) {
1093 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001094 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001095 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001096 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001097 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001098
Chris Lattnerac161bf2009-01-02 07:01:27 +00001099 // Otherwise, create a new forward reference for this value and remember it.
1100 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001101 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001102 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001103 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001104 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001105 GlobalValue::ExternalWeakLinkage, nullptr, "");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001106
Chris Lattnerac161bf2009-01-02 07:01:27 +00001107 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1108 return FwdVal;
1109}
1110
1111
1112//===----------------------------------------------------------------------===//
David Majnemerdad0a642014-06-27 18:19:56 +00001113// Comdat Reference/Resolution Routines.
1114//===----------------------------------------------------------------------===//
1115
1116Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) {
1117 // Look this name up in the comdat symbol table.
1118 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
1119 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
1120 if (I != ComdatSymTab.end())
1121 return &I->second;
1122
1123 // Otherwise, create a new forward reference for this value and remember it.
1124 Comdat *C = M->getOrInsertComdat(Name);
1125 ForwardRefComdats[Name] = Loc;
1126 return C;
1127}
1128
1129
1130//===----------------------------------------------------------------------===//
Chris Lattnerac161bf2009-01-02 07:01:27 +00001131// Helper Routines.
1132//===----------------------------------------------------------------------===//
1133
1134/// ParseToken - If the current token has the specified kind, eat it and return
1135/// success. Otherwise, emit the specified error and return failure.
1136bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1137 if (Lex.getKind() != T)
1138 return TokError(ErrMsg);
1139 Lex.Lex();
1140 return false;
1141}
1142
Chris Lattner3822f632009-01-02 08:05:26 +00001143/// ParseStringConstant
1144/// ::= StringConstant
1145bool LLParser::ParseStringConstant(std::string &Result) {
1146 if (Lex.getKind() != lltok::StringConstant)
1147 return TokError("expected string constant");
1148 Result = Lex.getStrVal();
1149 Lex.Lex();
1150 return false;
1151}
1152
1153/// ParseUInt32
1154/// ::= uint32
1155bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001156 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1157 return TokError("expected integer");
1158 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1159 if (Val64 != unsigned(Val64))
1160 return TokError("expected 32-bit integer (too large)");
1161 Val = Val64;
1162 Lex.Lex();
1163 return false;
1164}
1165
Hal Finkelb0407ba2014-07-18 15:51:28 +00001166/// ParseUInt64
1167/// ::= uint64
1168bool LLParser::ParseUInt64(uint64_t &Val) {
1169 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1170 return TokError("expected integer");
1171 Val = Lex.getAPSIntVal().getLimitedValue();
1172 Lex.Lex();
1173 return false;
1174}
1175
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001176/// ParseTLSModel
1177/// := 'localdynamic'
1178/// := 'initialexec'
1179/// := 'localexec'
1180bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1181 switch (Lex.getKind()) {
1182 default:
1183 return TokError("expected localdynamic, initialexec or localexec");
1184 case lltok::kw_localdynamic:
1185 TLM = GlobalVariable::LocalDynamicTLSModel;
1186 break;
1187 case lltok::kw_initialexec:
1188 TLM = GlobalVariable::InitialExecTLSModel;
1189 break;
1190 case lltok::kw_localexec:
1191 TLM = GlobalVariable::LocalExecTLSModel;
1192 break;
1193 }
1194
1195 Lex.Lex();
1196 return false;
1197}
1198
1199/// ParseOptionalThreadLocal
1200/// := /*empty*/
1201/// := 'thread_local'
1202/// := 'thread_local' '(' tlsmodel ')'
1203bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1204 TLM = GlobalVariable::NotThreadLocal;
1205 if (!EatIfPresent(lltok::kw_thread_local))
1206 return false;
1207
1208 TLM = GlobalVariable::GeneralDynamicTLSModel;
1209 if (Lex.getKind() == lltok::lparen) {
1210 Lex.Lex();
1211 return ParseTLSModel(TLM) ||
1212 ParseToken(lltok::rparen, "expected ')' after thread local model");
1213 }
1214 return false;
1215}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001216
1217/// ParseOptionalAddrSpace
1218/// := /*empty*/
1219/// := 'addrspace' '(' uint32 ')'
1220bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1221 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001222 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001223 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001224 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001225 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001226 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001227}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001228
Bill Wendling34c2eb22012-12-04 23:40:58 +00001229/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1230bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1231 bool HaveError = false;
1232
1233 B.clear();
1234
1235 while (1) {
1236 lltok::Kind Token = Lex.getKind();
1237 switch (Token) {
1238 default: // End of attributes.
1239 return HaveError;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001240 case lltok::kw_align: {
1241 unsigned Alignment;
1242 if (ParseOptionalAlignment(Alignment))
1243 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001244 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001245 continue;
1246 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001247 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001248 case lltok::kw_dereferenceable: {
1249 uint64_t Bytes;
1250 if (ParseOptionalDereferenceableBytes(Bytes))
1251 return true;
1252 B.addDereferenceableAttr(Bytes);
1253 continue;
1254 }
Reid Klecknera534a382013-12-19 02:14:12 +00001255 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001256 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1257 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1258 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1259 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001260 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001261 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1262 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001263 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001264 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1265 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1266 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001267
Stephen Lin7577ed52013-04-20 13:16:13 +00001268 case lltok::kw_alignstack:
1269 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001270 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001271 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001272 case lltok::kw_jumptable:
Stephen Lin7577ed52013-04-20 13:16:13 +00001273 case lltok::kw_minsize:
1274 case lltok::kw_naked:
1275 case lltok::kw_nobuiltin:
1276 case lltok::kw_noduplicate:
1277 case lltok::kw_noimplicitfloat:
1278 case lltok::kw_noinline:
1279 case lltok::kw_nonlazybind:
1280 case lltok::kw_noredzone:
1281 case lltok::kw_noreturn:
1282 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001283 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001284 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001285 case lltok::kw_returns_twice:
1286 case lltok::kw_sanitize_address:
1287 case lltok::kw_sanitize_memory:
1288 case lltok::kw_sanitize_thread:
1289 case lltok::kw_ssp:
1290 case lltok::kw_sspreq:
1291 case lltok::kw_sspstrong:
1292 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001293 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1294 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001295 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001296
Bill Wendling34c2eb22012-12-04 23:40:58 +00001297 Lex.Lex();
1298 }
1299}
1300
1301/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1302bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1303 bool HaveError = false;
1304
1305 B.clear();
1306
1307 while (1) {
1308 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001309 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001310 default: // End of attributes.
1311 return HaveError;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001312 case lltok::kw_dereferenceable: {
1313 uint64_t Bytes;
1314 if (ParseOptionalDereferenceableBytes(Bytes))
1315 return true;
1316 B.addDereferenceableAttr(Bytes);
1317 continue;
1318 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001319 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1320 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001321 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001322 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1323 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001324
Bill Wendling34c2eb22012-12-04 23:40:58 +00001325 // Error handling.
Stephen Lin7577ed52013-04-20 13:16:13 +00001326 case lltok::kw_align:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001327 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001328 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001329 case lltok::kw_nest:
1330 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001331 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001332 case lltok::kw_sret:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001333 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001334 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001335
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001336 case lltok::kw_alignstack:
1337 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001338 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001339 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001340 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001341 case lltok::kw_jumptable:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001342 case lltok::kw_minsize:
1343 case lltok::kw_naked:
1344 case lltok::kw_nobuiltin:
1345 case lltok::kw_noduplicate:
1346 case lltok::kw_noimplicitfloat:
1347 case lltok::kw_noinline:
1348 case lltok::kw_nonlazybind:
1349 case lltok::kw_noredzone:
1350 case lltok::kw_noreturn:
1351 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001352 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001353 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001354 case lltok::kw_returns_twice:
1355 case lltok::kw_sanitize_address:
1356 case lltok::kw_sanitize_memory:
1357 case lltok::kw_sanitize_thread:
1358 case lltok::kw_ssp:
1359 case lltok::kw_sspreq:
1360 case lltok::kw_sspstrong:
1361 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001362 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001363 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001364
1365 case lltok::kw_readnone:
1366 case lltok::kw_readonly:
1367 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001368 }
1369
Chris Lattnerac161bf2009-01-02 07:01:27 +00001370 Lex.Lex();
1371 }
1372}
1373
1374/// ParseOptionalLinkage
1375/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001376/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001377/// ::= 'internal'
1378/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001379/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001380/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001381/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001382/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001383/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001384/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001385/// ::= 'extern_weak'
1386/// ::= 'external'
1387bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1388 HasLinkage = false;
1389 switch (Lex.getKind()) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001390 default: Res=GlobalValue::ExternalLinkage; return false;
1391 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001392 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1393 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1394 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1395 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1396 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001397 case lltok::kw_available_externally:
1398 Res = GlobalValue::AvailableExternallyLinkage;
1399 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001400 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001401 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001402 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1403 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001404 }
1405 Lex.Lex();
1406 HasLinkage = true;
1407 return false;
1408}
1409
1410/// ParseOptionalVisibility
1411/// ::= /*empty*/
1412/// ::= 'default'
1413/// ::= 'hidden'
1414/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001415///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001416bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1417 switch (Lex.getKind()) {
1418 default: Res = GlobalValue::DefaultVisibility; return false;
1419 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1420 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1421 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1422 }
1423 Lex.Lex();
1424 return false;
1425}
1426
Nico Rieck7157bb72014-01-14 15:22:47 +00001427/// ParseOptionalDLLStorageClass
1428/// ::= /*empty*/
1429/// ::= 'dllimport'
1430/// ::= 'dllexport'
1431///
1432bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1433 switch (Lex.getKind()) {
1434 default: Res = GlobalValue::DefaultStorageClass; return false;
1435 case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break;
1436 case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break;
1437 }
1438 Lex.Lex();
1439 return false;
1440}
1441
Chris Lattnerac161bf2009-01-02 07:01:27 +00001442/// ParseOptionalCallingConv
1443/// ::= /*empty*/
1444/// ::= 'ccc'
1445/// ::= 'fastcc'
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001446/// ::= 'kw_intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001447/// ::= 'coldcc'
1448/// ::= 'x86_stdcallcc'
1449/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001450/// ::= 'x86_thiscallcc'
Reid Kleckner9ccce992014-10-28 01:29:26 +00001451/// ::= 'x86_vectorcallcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001452/// ::= 'arm_apcscc'
1453/// ::= 'arm_aapcscc'
1454/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001455/// ::= 'msp430_intrcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001456/// ::= 'ptx_kernel'
1457/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001458/// ::= 'spir_func'
1459/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001460/// ::= 'x86_64_sysvcc'
1461/// ::= 'x86_64_win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001462/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001463/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001464/// ::= 'preserve_mostcc'
1465/// ::= 'preserve_allcc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001466/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001467///
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00001468bool LLParser::ParseOptionalCallingConv(unsigned &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001469 switch (Lex.getKind()) {
1470 default: CC = CallingConv::C; return false;
1471 case lltok::kw_ccc: CC = CallingConv::C; break;
1472 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1473 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1474 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1475 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001476 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Reid Kleckner9ccce992014-10-28 01:29:26 +00001477 case lltok::kw_x86_vectorcallcc:CC = CallingConv::X86_VectorCall; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001478 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1479 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1480 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001481 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001482 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1483 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001484 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1485 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001486 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001487 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1488 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001489 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001490 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001491 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1492 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001493 case lltok::kw_cc: {
Sandeep Patel68c5f472009-09-02 08:44:58 +00001494 Lex.Lex();
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00001495 return ParseUInt32(CC);
Sandeep Patel68c5f472009-09-02 08:44:58 +00001496 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001497 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001498
Chris Lattnerac161bf2009-01-02 07:01:27 +00001499 Lex.Lex();
1500 return false;
1501}
1502
Chris Lattner5c427632009-12-30 05:31:19 +00001503/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001504/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman338d9a42010-08-24 02:05:17 +00001505bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1506 PerFunctionState *PFS) {
Chris Lattner5c427632009-12-30 05:31:19 +00001507 do {
1508 if (Lex.getKind() != lltok::MetadataVar)
1509 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001510
Chris Lattner596760d2009-12-29 21:25:40 +00001511 std::string Name = Lex.getStrVal();
Benjamin Kramerb3bd0192011-12-06 11:50:26 +00001512 unsigned MDK = M->getMDKindID(Name);
Chris Lattner596760d2009-12-29 21:25:40 +00001513 Lex.Lex();
Chris Lattner8d58f2f2009-10-19 05:31:10 +00001514
Chris Lattner1797fc72009-12-29 21:53:55 +00001515 MDNode *Node;
Chris Lattner8eff0152010-04-01 05:14:45 +00001516 SMLoc Loc = Lex.getLoc();
Dan Gohmanc828c542010-08-24 02:24:03 +00001517
1518 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001519 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001520
Dan Gohmanf0715b12010-08-24 14:35:45 +00001521 // This code is similar to that of ParseMetadataValue, however it needs to
1522 // have special-case code for a forward reference; see the comments on
1523 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1524 // at the top level here.
Dan Gohmanc828c542010-08-24 02:24:03 +00001525 if (Lex.getKind() == lltok::lbrace) {
1526 ValID ID;
1527 if (ParseMetadataListValue(ID, PFS))
1528 return true;
1529 assert(ID.Kind == ValID::t_MDNode);
1530 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner8eff0152010-04-01 05:14:45 +00001531 } else {
Nick Lewycky912888f2010-09-30 21:04:13 +00001532 unsigned NodeID = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +00001533 if (ParseMDNodeID(Node, NodeID))
1534 return true;
1535 if (Node) {
1536 // If we got the node, add it to the instruction.
1537 Inst->setMetadata(MDK, Node);
1538 } else {
1539 MDRef R = { Loc, MDK, NodeID };
1540 // Otherwise, remember that this should be resolved later.
1541 ForwardRefInstMetadata[Inst].push_back(R);
1542 }
Chris Lattner8eff0152010-04-01 05:14:45 +00001543 }
Chris Lattner596760d2009-12-29 21:25:40 +00001544
Manman Ren209b17c2013-09-28 00:22:27 +00001545 if (MDK == LLVMContext::MD_tbaa)
1546 InstsWithTBAATag.push_back(Inst);
1547
Chris Lattner596760d2009-12-29 21:25:40 +00001548 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001549 } while (EatIfPresent(lltok::comma));
1550 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001551}
1552
Chris Lattnerac161bf2009-01-02 07:01:27 +00001553/// ParseOptionalAlignment
1554/// ::= /* empty */
1555/// ::= 'align' 4
1556bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1557 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001558 if (!EatIfPresent(lltok::kw_align))
1559 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001560 LocTy AlignLoc = Lex.getLoc();
1561 if (ParseUInt32(Alignment)) return true;
1562 if (!isPowerOf2_32(Alignment))
1563 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001564 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001565 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001566 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001567}
1568
Hal Finkelb0407ba2014-07-18 15:51:28 +00001569/// ParseOptionalDereferenceableBytes
1570/// ::= /* empty */
1571/// ::= 'dereferenceable' '(' 4 ')'
1572bool LLParser::ParseOptionalDereferenceableBytes(uint64_t &Bytes) {
1573 Bytes = 0;
1574 if (!EatIfPresent(lltok::kw_dereferenceable))
1575 return false;
1576 LocTy ParenLoc = Lex.getLoc();
1577 if (!EatIfPresent(lltok::lparen))
1578 return Error(ParenLoc, "expected '('");
1579 LocTy DerefLoc = Lex.getLoc();
1580 if (ParseUInt64(Bytes)) return true;
1581 ParenLoc = Lex.getLoc();
1582 if (!EatIfPresent(lltok::rparen))
1583 return Error(ParenLoc, "expected ')'");
1584 if (!Bytes)
1585 return Error(DerefLoc, "dereferenceable bytes must be non-zero");
1586 return false;
1587}
1588
Chris Lattnerb2f39502009-12-30 05:44:30 +00001589/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001590/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001591/// ::= ',' align 4
1592///
1593/// This returns with AteExtraComma set to true if it ate an excess comma at the
1594/// end.
1595bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1596 bool &AteExtraComma) {
1597 AteExtraComma = false;
1598 while (EatIfPresent(lltok::comma)) {
1599 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001600 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001601 AteExtraComma = true;
1602 return false;
1603 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001604
Chris Lattner95b0ff42010-04-23 00:50:50 +00001605 if (Lex.getKind() != lltok::kw_align)
1606 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001607
Chris Lattner95b0ff42010-04-23 00:50:50 +00001608 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001609 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001610
Devang Patelea8a4b92009-09-17 23:04:48 +00001611 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001612}
1613
Eli Friedmanfee02c62011-07-25 23:16:38 +00001614/// ParseScopeAndOrdering
1615/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1616/// else: ::=
1617///
1618/// This sets Scope and Ordering to the parsed values.
1619bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1620 AtomicOrdering &Ordering) {
1621 if (!isAtomic)
1622 return false;
1623
1624 Scope = CrossThread;
1625 if (EatIfPresent(lltok::kw_singlethread))
1626 Scope = SingleThread;
Tim Northovere94a5182014-03-11 10:48:52 +00001627
1628 return ParseOrdering(Ordering);
1629}
1630
1631/// ParseOrdering
1632/// ::= AtomicOrdering
1633///
1634/// This sets Ordering to the parsed value.
1635bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001636 switch (Lex.getKind()) {
1637 default: return TokError("Expected ordering on atomic instruction");
1638 case lltok::kw_unordered: Ordering = Unordered; break;
1639 case lltok::kw_monotonic: Ordering = Monotonic; break;
1640 case lltok::kw_acquire: Ordering = Acquire; break;
1641 case lltok::kw_release: Ordering = Release; break;
1642 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1643 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1644 }
1645 Lex.Lex();
1646 return false;
1647}
1648
Charles Davisbe5557e2010-02-12 00:31:15 +00001649/// ParseOptionalStackAlignment
1650/// ::= /* empty */
1651/// ::= 'alignstack' '(' 4 ')'
1652bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1653 Alignment = 0;
1654 if (!EatIfPresent(lltok::kw_alignstack))
1655 return false;
1656 LocTy ParenLoc = Lex.getLoc();
1657 if (!EatIfPresent(lltok::lparen))
1658 return Error(ParenLoc, "expected '('");
1659 LocTy AlignLoc = Lex.getLoc();
1660 if (ParseUInt32(Alignment)) return true;
1661 ParenLoc = Lex.getLoc();
1662 if (!EatIfPresent(lltok::rparen))
1663 return Error(ParenLoc, "expected ')'");
1664 if (!isPowerOf2_32(Alignment))
1665 return Error(AlignLoc, "stack alignment is not a power of two");
1666 return false;
1667}
Devang Patelea8a4b92009-09-17 23:04:48 +00001668
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001669/// ParseIndexList - This parses the index list for an insert/extractvalue
1670/// instruction. This sets AteExtraComma in the case where we eat an extra
1671/// comma at the end of the line and find that it is followed by metadata.
1672/// Clients that don't allow metadata can call the version of this function that
1673/// only takes one argument.
1674///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001675/// ParseIndexList
1676/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001677///
1678bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1679 bool &AteExtraComma) {
1680 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001681
Chris Lattnerac161bf2009-01-02 07:01:27 +00001682 if (Lex.getKind() != lltok::comma)
1683 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001684
Chris Lattner3822f632009-01-02 08:05:26 +00001685 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001686 if (Lex.getKind() == lltok::MetadataVar) {
1687 AteExtraComma = true;
1688 return false;
1689 }
Nick Lewycky83e47112010-09-29 23:32:20 +00001690 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001691 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001692 Indices.push_back(Idx);
1693 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001694
Chris Lattnerac161bf2009-01-02 07:01:27 +00001695 return false;
1696}
1697
1698//===----------------------------------------------------------------------===//
1699// Type Parsing.
1700//===----------------------------------------------------------------------===//
1701
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001702/// ParseType - Parse a type.
1703bool LLParser::ParseType(Type *&Result, bool AllowVoid) {
1704 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001705 switch (Lex.getKind()) {
1706 default:
1707 return TokError("expected type");
1708 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001709 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00001710 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001711 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001712 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001713 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001714 // Type ::= StructType
1715 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001716 return true;
1717 break;
1718 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001719 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001720 Lex.Lex(); // eat the lsquare.
1721 if (ParseArrayVectorType(Result, false))
1722 return true;
1723 break;
1724 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001725 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00001726 Lex.Lex();
1727 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001728 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00001729 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001730 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001731 } else if (ParseArrayVectorType(Result, true))
1732 return true;
1733 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001734 case lltok::LocalVar: {
1735 // Type ::= %foo
1736 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001737
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001738 // If the type hasn't been defined yet, create a forward definition and
1739 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001740 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001741 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001742 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001743 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001744 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001745 Lex.Lex();
1746 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001747 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001748
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001749 case lltok::LocalVarID: {
1750 // Type ::= %4
1751 if (Lex.getUIntVal() >= NumberedTypes.size())
1752 NumberedTypes.resize(Lex.getUIntVal()+1);
1753 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001754
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001755 // If the type hasn't been defined yet, create a forward definition and
1756 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001757 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001758 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001759 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001760 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001761 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001762 Lex.Lex();
1763 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001764 }
1765 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001766
1767 // Parse the type suffixes.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001768 while (1) {
1769 switch (Lex.getKind()) {
1770 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001771 default:
1772 if (!AllowVoid && Result->isVoidTy())
1773 return Error(TypeLoc, "void type only allowed for function results");
1774 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001775
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001776 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001777 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001778 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001779 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001780 if (Result->isVoidTy())
1781 return TokError("pointers to void are invalid - use i8* instead");
1782 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001783 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001784 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001785 Lex.Lex();
1786 break;
1787
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001788 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001789 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001790 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001791 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001792 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00001793 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001794 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001795 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001796 unsigned AddrSpace;
1797 if (ParseOptionalAddrSpace(AddrSpace) ||
1798 ParseToken(lltok::star, "expected '*' in address space"))
1799 return true;
1800
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001801 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001802 break;
1803 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001804
Chris Lattnerac161bf2009-01-02 07:01:27 +00001805 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1806 case lltok::lparen:
1807 if (ParseFunctionType(Result))
1808 return true;
1809 break;
1810 }
1811 }
1812}
1813
1814/// ParseParameterList
1815/// ::= '(' ')'
1816/// ::= '(' Arg (',' Arg)* ')'
1817/// Arg
1818/// ::= Type OptionalAttributes Value OptionalAttributes
1819bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
Reid Kleckner83498642014-08-26 00:33:28 +00001820 PerFunctionState &PFS, bool IsMustTailCall,
1821 bool InVarArgsFunc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001822 if (ParseToken(lltok::lparen, "expected '(' in call"))
1823 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001824
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001825 unsigned AttrIndex = 1;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001826 while (Lex.getKind() != lltok::rparen) {
1827 // If this isn't the first argument, we need a comma.
1828 if (!ArgList.empty() &&
1829 ParseToken(lltok::comma, "expected ',' in argument list"))
1830 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001831
Reid Kleckner83498642014-08-26 00:33:28 +00001832 // Parse an ellipsis if this is a musttail call in a variadic function.
1833 if (Lex.getKind() == lltok::dotdotdot) {
1834 const char *Msg = "unexpected ellipsis in argument list for ";
1835 if (!IsMustTailCall)
1836 return TokError(Twine(Msg) + "non-musttail call");
1837 if (!InVarArgsFunc)
1838 return TokError(Twine(Msg) + "musttail call in non-varargs function");
1839 Lex.Lex(); // Lex the '...', it is purely for readability.
1840 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
1841 }
1842
Chris Lattnerac161bf2009-01-02 07:01:27 +00001843 // Parse the argument.
1844 LocTy ArgLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00001845 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001846 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001847 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00001848 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001849 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00001850
Chris Lattner5b4a9622009-12-30 02:11:14 +00001851 // Otherwise, handle normal operands.
Bill Wendling34c2eb22012-12-04 23:40:58 +00001852 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
Chris Lattner5b4a9622009-12-30 02:11:14 +00001853 return true;
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001854 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1855 AttrIndex++,
1856 ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001857 }
1858
Reid Kleckner83498642014-08-26 00:33:28 +00001859 if (IsMustTailCall && InVarArgsFunc)
1860 return TokError("expected '...' at end of argument list for musttail call "
1861 "in varargs function");
1862
Chris Lattnerac161bf2009-01-02 07:01:27 +00001863 Lex.Lex(); // Lex the ')'.
1864 return false;
1865}
1866
1867
1868
Chris Lattner2ed06b42009-01-05 18:34:07 +00001869/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001870/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001871/// ::= '(' ArgTypeListI ')'
1872/// ArgTypeListI
1873/// ::= /*empty*/
1874/// ::= '...'
1875/// ::= ArgTypeList ',' '...'
1876/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00001877///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001878bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1879 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00001880 isVarArg = false;
1881 assert(Lex.getKind() == lltok::lparen);
1882 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001883
Chris Lattnerac161bf2009-01-02 07:01:27 +00001884 if (Lex.getKind() == lltok::rparen) {
1885 // empty
1886 } else if (Lex.getKind() == lltok::dotdotdot) {
1887 isVarArg = true;
1888 Lex.Lex();
1889 } else {
1890 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001891 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001892 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001893 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001894
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001895 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00001896 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001897
Chris Lattnerfdd87902009-10-05 05:54:46 +00001898 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001899 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001900
Chris Lattnerdef19492011-06-17 06:36:20 +00001901 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001902 Name = Lex.getStrVal();
1903 Lex.Lex();
1904 }
Chris Lattner3822f632009-01-02 08:05:26 +00001905
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001906 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00001907 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001908
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001909 unsigned AttrIndex = 1;
Bill Wendlingd079a442012-10-15 04:46:55 +00001910 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001911 AttributeSet::get(ArgTy->getContext(),
1912 AttrIndex++, Attrs), Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001913
Chris Lattner3822f632009-01-02 08:05:26 +00001914 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001915 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00001916 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001917 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001918 break;
1919 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001920
Chris Lattnerac161bf2009-01-02 07:01:27 +00001921 // Otherwise must be an argument type.
1922 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00001923 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00001924
Chris Lattnerfdd87902009-10-05 05:54:46 +00001925 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001926 return Error(TypeLoc, "argument can not have void type");
1927
Chris Lattnerdef19492011-06-17 06:36:20 +00001928 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001929 Name = Lex.getStrVal();
1930 Lex.Lex();
1931 } else {
1932 Name = "";
1933 }
Chris Lattner3822f632009-01-02 08:05:26 +00001934
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001935 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00001936 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001937
Bill Wendlingd079a442012-10-15 04:46:55 +00001938 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001939 AttributeSet::get(ArgTy->getContext(),
1940 AttrIndex++, Attrs),
Bill Wendlingd079a442012-10-15 04:46:55 +00001941 Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001942 }
1943 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001944
Chris Lattner3822f632009-01-02 08:05:26 +00001945 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001946}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001947
Chris Lattnerac161bf2009-01-02 07:01:27 +00001948/// ParseFunctionType
1949/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001950bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001951 assert(Lex.getKind() == lltok::lparen);
1952
Chris Lattnerce473c72009-01-05 08:04:33 +00001953 if (!FunctionType::isValidReturnType(Result))
1954 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001955
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001956 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001957 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001958 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001959 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001960
Chris Lattnerac161bf2009-01-02 07:01:27 +00001961 // Reject names on the arguments lists.
1962 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1963 if (!ArgList[i].Name.empty())
1964 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001965 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattner6bc5c892011-06-17 17:37:13 +00001966 return Error(ArgList[i].Loc,
1967 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001968 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001969
Jay Foadb804a2b2011-07-12 14:06:48 +00001970 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001971 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001972 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001973
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001974 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001975 return false;
1976}
1977
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001978/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1979/// other structs.
1980bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1981 SmallVector<Type*, 8> Elts;
1982 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001983
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001984 Result = StructType::get(Context, Elts, Packed);
1985 return false;
1986}
1987
1988/// ParseStructDefinition - Parse a struct in a 'type' definition.
1989bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1990 std::pair<Type*, LocTy> &Entry,
1991 Type *&ResultTy) {
1992 // If the type was already defined, diagnose the redefinition.
1993 if (Entry.first && !Entry.second.isValid())
1994 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001995
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001996 // If we have opaque, just return without filling in the definition for the
1997 // struct. This counts as a definition as far as the .ll file goes.
1998 if (EatIfPresent(lltok::kw_opaque)) {
1999 // This type is being defined, so clear the location to indicate this.
2000 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002001
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002002 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00002003 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00002004 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002005 ResultTy = Entry.first;
2006 return false;
2007 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002008
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002009 // If the type starts with '<', then it is either a packed struct or a vector.
2010 bool isPacked = EatIfPresent(lltok::less);
2011
2012 // If we don't have a struct, then we have a random type alias, which we
2013 // accept for compatibility with old files. These types are not allowed to be
2014 // forward referenced and not allowed to be recursive.
2015 if (Lex.getKind() != lltok::lbrace) {
2016 if (Entry.first)
2017 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002018
Craig Topper2617dcc2014-04-15 06:32:26 +00002019 ResultTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002020 if (isPacked)
2021 return ParseArrayVectorType(ResultTy, true);
2022 return ParseType(ResultTy);
2023 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002024
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002025 // This type is being defined, so clear the location to indicate this.
2026 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002027
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002028 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00002029 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00002030 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002031
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002032 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002033
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002034 SmallVector<Type*, 8> Body;
2035 if (ParseStructBody(Body) ||
2036 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
2037 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002038
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002039 STy->setBody(Body, isPacked);
2040 ResultTy = STy;
2041 return false;
2042}
2043
2044
Chris Lattnerac161bf2009-01-02 07:01:27 +00002045/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002046/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00002047/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002048/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00002049/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002050/// ::= '<' '{' Type (',' Type)* '}' '>'
2051bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002052 assert(Lex.getKind() == lltok::lbrace);
2053 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002054
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002055 // Handle the empty struct.
2056 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002057 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002058
Chris Lattnerf880ca22009-03-09 04:49:14 +00002059 LocTy EltTyLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002060 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002061 if (ParseType(Ty)) return true;
2062 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002063
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002064 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002065 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002066
Chris Lattner3822f632009-01-02 08:05:26 +00002067 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00002068 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002069 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002070
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002071 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002072 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002073
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002074 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002075 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002076
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002077 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002078}
2079
2080/// ParseArrayVectorType - Parse an array or vector type, assuming the first
2081/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002082/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00002083/// ::= '[' APSINTVAL 'x' Types ']'
2084/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002085bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002086 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
2087 Lex.getAPSIntVal().getBitWidth() > 64)
2088 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002089
Chris Lattnerac161bf2009-01-02 07:01:27 +00002090 LocTy SizeLoc = Lex.getLoc();
2091 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00002092 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002093
Chris Lattner3822f632009-01-02 08:05:26 +00002094 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
2095 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002096
2097 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002098 Type *EltTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002099 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00002100
Chris Lattner3822f632009-01-02 08:05:26 +00002101 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
2102 "expected end of sequential type"))
2103 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002104
Chris Lattnerac161bf2009-01-02 07:01:27 +00002105 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00002106 if (Size == 0)
2107 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002108 if ((unsigned)Size != Size)
2109 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002110 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00002111 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00002112 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002113 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002114 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002115 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002116 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002117 }
2118 return false;
2119}
2120
2121//===----------------------------------------------------------------------===//
2122// Function Semantic Analysis.
2123//===----------------------------------------------------------------------===//
2124
Chris Lattner3432c622009-10-28 03:39:23 +00002125LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2126 int functionNumber)
2127 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002128
2129 // Insert unnamed arguments into the NumberedVals list.
2130 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2131 AI != E; ++AI)
2132 if (!AI->hasName())
2133 NumberedVals.push_back(AI);
2134}
2135
2136LLParser::PerFunctionState::~PerFunctionState() {
2137 // If there were any forward referenced non-basicblock values, delete them.
2138 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
2139 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
2140 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002141 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002142 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002143 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002144 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002145 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002146
Chris Lattnerac161bf2009-01-02 07:01:27 +00002147 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2148 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
2149 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002150 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002151 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002152 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002153 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002154 }
2155}
2156
Chris Lattner3432c622009-10-28 03:39:23 +00002157bool LLParser::PerFunctionState::FinishFunction() {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002158 if (!ForwardRefVals.empty())
2159 return P.Error(ForwardRefVals.begin()->second.second,
2160 "use of undefined value '%" + ForwardRefVals.begin()->first +
2161 "'");
2162 if (!ForwardRefValIDs.empty())
2163 return P.Error(ForwardRefValIDs.begin()->second.second,
2164 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002165 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002166 return false;
2167}
2168
2169
2170/// GetVal - Get a value with the specified name or ID, creating a
2171/// forward reference record if needed. This can return null if the value
2172/// exists but does not have the right type.
2173Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattner229907c2011-07-18 04:54:35 +00002174 Type *Ty, LocTy Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002175 // Look this name up in the normal function symbol table.
2176 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002177
Chris Lattnerac161bf2009-01-02 07:01:27 +00002178 // If this is a forward reference for the value, see if we already created a
2179 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002180 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002181 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2182 I = ForwardRefVals.find(Name);
2183 if (I != ForwardRefVals.end())
2184 Val = I->second.first;
2185 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002186
Chris Lattnerac161bf2009-01-02 07:01:27 +00002187 // If we have the value in the symbol table or fwd-ref table, return it.
2188 if (Val) {
2189 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002190 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002191 P.Error(Loc, "'%" + Name + "' is not a basic block");
2192 else
2193 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002194 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002195 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002196 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002197
Chris Lattnerac161bf2009-01-02 07:01:27 +00002198 // Don't make placeholders with invalid type.
Duncan P. N. Exon Smith6a6e9cb2014-08-05 18:22:58 +00002199 if (!Ty->isFirstClassType()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002200 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002201 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002202 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002203
Chris Lattnerac161bf2009-01-02 07:01:27 +00002204 // Otherwise, create a new forward reference for this value and remember it.
2205 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002206 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002207 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002208 else
2209 FwdVal = new Argument(Ty, Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002210
Chris Lattnerac161bf2009-01-02 07:01:27 +00002211 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2212 return FwdVal;
2213}
2214
Chris Lattner229907c2011-07-18 04:54:35 +00002215Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00002216 LocTy Loc) {
2217 // Look this name up in the normal function symbol table.
Craig Topper2617dcc2014-04-15 06:32:26 +00002218 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002219
Chris Lattnerac161bf2009-01-02 07:01:27 +00002220 // If this is a forward reference for the value, see if we already created a
2221 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002222 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002223 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2224 I = ForwardRefValIDs.find(ID);
2225 if (I != ForwardRefValIDs.end())
2226 Val = I->second.first;
2227 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002228
Chris Lattnerac161bf2009-01-02 07:01:27 +00002229 // If we have the value in the symbol table or fwd-ref table, return it.
2230 if (Val) {
2231 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002232 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002233 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002234 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002235 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002236 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002237 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002238 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002239
Duncan P. N. Exon Smith6a6e9cb2014-08-05 18:22:58 +00002240 if (!Ty->isFirstClassType()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002241 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002242 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002243 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002244
Chris Lattnerac161bf2009-01-02 07:01:27 +00002245 // Otherwise, create a new forward reference for this value and remember it.
2246 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002247 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002248 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002249 else
2250 FwdVal = new Argument(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002251
Chris Lattnerac161bf2009-01-02 07:01:27 +00002252 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2253 return FwdVal;
2254}
2255
2256/// SetInstName - After an instruction is parsed and inserted into its
2257/// basic block, this installs its name.
2258bool LLParser::PerFunctionState::SetInstName(int NameID,
2259 const std::string &NameStr,
2260 LocTy NameLoc, Instruction *Inst) {
2261 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002262 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002263 if (NameID != -1 || !NameStr.empty())
2264 return P.Error(NameLoc, "instructions returning void cannot have a name");
2265 return false;
2266 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002267
Chris Lattnerac161bf2009-01-02 07:01:27 +00002268 // If this was a numbered instruction, verify that the instruction is the
2269 // expected value and resolve any forward references.
2270 if (NameStr.empty()) {
2271 // If neither a name nor an ID was specified, just use the next ID.
2272 if (NameID == -1)
2273 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002274
Chris Lattnerac161bf2009-01-02 07:01:27 +00002275 if (unsigned(NameID) != NumberedVals.size())
2276 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002277 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002278
Chris Lattnerac161bf2009-01-02 07:01:27 +00002279 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2280 ForwardRefValIDs.find(NameID);
2281 if (FI != ForwardRefValIDs.end()) {
2282 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002283 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002284 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002285 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2baa6a32009-09-02 15:02:57 +00002286 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002287 ForwardRefValIDs.erase(FI);
2288 }
2289
2290 NumberedVals.push_back(Inst);
2291 return false;
2292 }
2293
2294 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2295 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2296 FI = ForwardRefVals.find(NameStr);
2297 if (FI != ForwardRefVals.end()) {
2298 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002299 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002300 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002301 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2fcee702009-09-02 14:22:03 +00002302 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002303 ForwardRefVals.erase(FI);
2304 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002305
Chris Lattnerac161bf2009-01-02 07:01:27 +00002306 // Set the name on the instruction.
2307 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002308
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002309 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002310 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002311 NameStr + "'");
2312 return false;
2313}
2314
2315/// GetBB - Get a basic block with the specified name or ID, creating a
2316/// forward reference record if needed.
2317BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2318 LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002319 return cast_or_null<BasicBlock>(GetVal(Name,
2320 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002321}
2322
2323BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002324 return cast_or_null<BasicBlock>(GetVal(ID,
2325 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002326}
2327
2328/// DefineBB - Define the specified basic block, which is either named or
2329/// unnamed. If there is an error, this returns null otherwise it returns
2330/// the block being defined.
2331BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2332 LocTy Loc) {
2333 BasicBlock *BB;
2334 if (Name.empty())
2335 BB = GetBB(NumberedVals.size(), Loc);
2336 else
2337 BB = GetBB(Name, Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002338 if (!BB) return nullptr; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002339
Chris Lattnerac161bf2009-01-02 07:01:27 +00002340 // Move the block to the end of the function. Forward ref'd blocks are
2341 // inserted wherever they happen to be referenced.
2342 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002343
Chris Lattnerac161bf2009-01-02 07:01:27 +00002344 // Remove the block from forward ref sets.
2345 if (Name.empty()) {
2346 ForwardRefValIDs.erase(NumberedVals.size());
2347 NumberedVals.push_back(BB);
2348 } else {
2349 // BB forward references are already in the function symbol table.
2350 ForwardRefVals.erase(Name);
2351 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002352
Chris Lattnerac161bf2009-01-02 07:01:27 +00002353 return BB;
2354}
2355
2356//===----------------------------------------------------------------------===//
2357// Constants.
2358//===----------------------------------------------------------------------===//
2359
2360/// ParseValID - Parse an abstract value that doesn't necessarily have a
2361/// type implied. For example, if we parse "4" we don't know what integer type
2362/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002363/// sanity. PFS is used to convert function-local operands of metadata (since
2364/// metadata operands are not just parsed here but also converted to values).
2365/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002366bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002367 ID.Loc = Lex.getLoc();
2368 switch (Lex.getKind()) {
2369 default: return TokError("expected value token");
2370 case lltok::GlobalID: // @42
2371 ID.UIntVal = Lex.getUIntVal();
2372 ID.Kind = ValID::t_GlobalID;
2373 break;
2374 case lltok::GlobalVar: // @foo
2375 ID.StrVal = Lex.getStrVal();
2376 ID.Kind = ValID::t_GlobalName;
2377 break;
2378 case lltok::LocalVarID: // %42
2379 ID.UIntVal = Lex.getUIntVal();
2380 ID.Kind = ValID::t_LocalID;
2381 break;
2382 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002383 ID.StrVal = Lex.getStrVal();
2384 ID.Kind = ValID::t_LocalName;
2385 break;
Dan Gohman8939ba332010-07-14 18:26:50 +00002386 case lltok::exclaim: // !42, !{...}, or !"foo"
2387 return ParseMetadataValue(ID, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002388 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002389 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002390 ID.Kind = ValID::t_APSInt;
2391 break;
2392 case lltok::APFloat:
2393 ID.APFloatVal = Lex.getAPFloatVal();
2394 ID.Kind = ValID::t_APFloat;
2395 break;
2396 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002397 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002398 ID.Kind = ValID::t_Constant;
2399 break;
2400 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002401 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002402 ID.Kind = ValID::t_Constant;
2403 break;
2404 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2405 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2406 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002407
Chris Lattnerac161bf2009-01-02 07:01:27 +00002408 case lltok::lbrace: {
2409 // ValID ::= '{' ConstVector '}'
2410 Lex.Lex();
2411 SmallVector<Constant*, 16> Elts;
2412 if (ParseGlobalValueVector(Elts) ||
2413 ParseToken(lltok::rbrace, "expected end of struct constant"))
2414 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002415
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002416 ID.ConstantStructElts = new Constant*[Elts.size()];
2417 ID.UIntVal = Elts.size();
2418 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2419 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002420 return false;
2421 }
2422 case lltok::less: {
2423 // ValID ::= '<' ConstVector '>' --> Vector.
2424 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2425 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002426 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002427
Chris Lattnerac161bf2009-01-02 07:01:27 +00002428 SmallVector<Constant*, 16> Elts;
2429 LocTy FirstEltLoc = Lex.getLoc();
2430 if (ParseGlobalValueVector(Elts) ||
2431 (isPackedStruct &&
2432 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2433 ParseToken(lltok::greater, "expected end of constant"))
2434 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002435
Chris Lattnerac161bf2009-01-02 07:01:27 +00002436 if (isPackedStruct) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002437 ID.ConstantStructElts = new Constant*[Elts.size()];
2438 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2439 ID.UIntVal = Elts.size();
2440 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002441 return false;
2442 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002443
Chris Lattnerac161bf2009-01-02 07:01:27 +00002444 if (Elts.empty())
2445 return Error(ID.Loc, "constant vector must not be empty");
2446
Duncan Sands9dff9be2010-02-15 16:12:20 +00002447 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002448 !Elts[0]->getType()->isFloatingPointTy() &&
2449 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002450 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002451 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002452
Chris Lattnerac161bf2009-01-02 07:01:27 +00002453 // Verify that all the vector elements have the same type.
2454 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2455 if (Elts[i]->getType() != Elts[0]->getType())
2456 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002457 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002458 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002459
Chris Lattner69229312011-02-15 00:14:00 +00002460 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002461 ID.Kind = ValID::t_Constant;
2462 return false;
2463 }
2464 case lltok::lsquare: { // Array Constant
2465 Lex.Lex();
2466 SmallVector<Constant*, 16> Elts;
2467 LocTy FirstEltLoc = Lex.getLoc();
2468 if (ParseGlobalValueVector(Elts) ||
2469 ParseToken(lltok::rsquare, "expected end of array constant"))
2470 return true;
2471
2472 // Handle empty element.
2473 if (Elts.empty()) {
2474 // Use undef instead of an array because it's inconvenient to determine
2475 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002476 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002477 return false;
2478 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002479
Chris Lattnerac161bf2009-01-02 07:01:27 +00002480 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002481 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002482 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002483
Owen Anderson4056ca92009-07-29 22:17:13 +00002484 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002485
Chris Lattnerac161bf2009-01-02 07:01:27 +00002486 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002487 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002488 if (Elts[i]->getType() != Elts[0]->getType())
2489 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002490 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002491 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002492 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002493
Jay Foad83be3612011-06-22 09:24:39 +00002494 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002495 ID.Kind = ValID::t_Constant;
2496 return false;
2497 }
2498 case lltok::kw_c: // c "foo"
2499 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002500 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2501 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002502 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2503 ID.Kind = ValID::t_Constant;
2504 return false;
2505
2506 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002507 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2508 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002509 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002510 Lex.Lex();
2511 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002512 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002513 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002514 ParseStringConstant(ID.StrVal) ||
2515 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002516 ParseToken(lltok::StringConstant, "expected constraint string"))
2517 return true;
2518 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002519 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002520 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002521 ID.Kind = ValID::t_InlineAsm;
2522 return false;
2523 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002524
Chris Lattner3432c622009-10-28 03:39:23 +00002525 case lltok::kw_blockaddress: {
2526 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2527 Lex.Lex();
2528
2529 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002530
Chris Lattner3432c622009-10-28 03:39:23 +00002531 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2532 ParseValID(Fn) ||
2533 ParseToken(lltok::comma, "expected comma in block address expression")||
2534 ParseValID(Label) ||
2535 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2536 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002537
Chris Lattner3432c622009-10-28 03:39:23 +00002538 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2539 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00002540 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00002541 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002542
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00002543 // Try to find the function (but skip it if it's forward-referenced).
2544 GlobalValue *GV = nullptr;
2545 if (Fn.Kind == ValID::t_GlobalID) {
2546 if (Fn.UIntVal < NumberedVals.size())
2547 GV = NumberedVals[Fn.UIntVal];
2548 } else if (!ForwardRefVals.count(Fn.StrVal)) {
2549 GV = M->getNamedValue(Fn.StrVal);
2550 }
2551 Function *F = nullptr;
2552 if (GV) {
2553 // Confirm that it's actually a function with a definition.
2554 if (!isa<Function>(GV))
2555 return Error(Fn.Loc, "expected function name in blockaddress");
2556 F = cast<Function>(GV);
2557 if (F->isDeclaration())
2558 return Error(Fn.Loc, "cannot take blockaddress inside a declaration");
2559 }
2560
2561 if (!F) {
2562 // Make a global variable as a placeholder for this reference.
2563 GlobalValue *&FwdRef = ForwardRefBlockAddresses[Fn][Label];
2564 if (!FwdRef)
2565 FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), false,
2566 GlobalValue::InternalLinkage, nullptr, "");
2567 ID.ConstantVal = FwdRef;
2568 ID.Kind = ValID::t_Constant;
2569 return false;
2570 }
2571
2572 // We found the function; now find the basic block. Don't use PFS, since we
2573 // might be inside a constant expression.
2574 BasicBlock *BB;
2575 if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) {
2576 if (Label.Kind == ValID::t_LocalID)
2577 BB = BlockAddressPFS->GetBB(Label.UIntVal, Label.Loc);
2578 else
2579 BB = BlockAddressPFS->GetBB(Label.StrVal, Label.Loc);
2580 if (!BB)
2581 return Error(Label.Loc, "referenced value is not a basic block");
2582 } else {
2583 if (Label.Kind == ValID::t_LocalID)
2584 return Error(Label.Loc, "cannot take address of numeric label after "
2585 "the function is defined");
2586 BB = dyn_cast_or_null<BasicBlock>(
2587 F->getValueSymbolTable().lookup(Label.StrVal));
2588 if (!BB)
2589 return Error(Label.Loc, "referenced value is not a basic block");
2590 }
2591
2592 ID.ConstantVal = BlockAddress::get(F, BB);
Chris Lattner3432c622009-10-28 03:39:23 +00002593 ID.Kind = ValID::t_Constant;
2594 return false;
2595 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002596
Chris Lattnerac161bf2009-01-02 07:01:27 +00002597 case lltok::kw_trunc:
2598 case lltok::kw_zext:
2599 case lltok::kw_sext:
2600 case lltok::kw_fptrunc:
2601 case lltok::kw_fpext:
2602 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002603 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002604 case lltok::kw_uitofp:
2605 case lltok::kw_sitofp:
2606 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002607 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002608 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002609 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002610 unsigned Opc = Lex.getUIntVal();
Craig Topper2617dcc2014-04-15 06:32:26 +00002611 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002612 Constant *SrcVal;
2613 Lex.Lex();
2614 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2615 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00002616 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002617 ParseType(DestTy) ||
2618 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2619 return true;
2620 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2621 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002622 getTypeString(SrcVal->getType()) + "' to '" +
2623 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002624 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00002625 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002626 ID.Kind = ValID::t_Constant;
2627 return false;
2628 }
2629 case lltok::kw_extractvalue: {
2630 Lex.Lex();
2631 Constant *Val;
2632 SmallVector<unsigned, 4> Indices;
2633 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2634 ParseGlobalTypeAndValue(Val) ||
2635 ParseIndexList(Indices) ||
2636 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2637 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00002638
Chris Lattner392be582010-02-12 20:49:41 +00002639 if (!Val->getType()->isAggregateType())
2640 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002641 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002642 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002643 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002644 ID.Kind = ValID::t_Constant;
2645 return false;
2646 }
2647 case lltok::kw_insertvalue: {
2648 Lex.Lex();
2649 Constant *Val0, *Val1;
2650 SmallVector<unsigned, 4> Indices;
2651 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2652 ParseGlobalTypeAndValue(Val0) ||
2653 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2654 ParseGlobalTypeAndValue(Val1) ||
2655 ParseIndexList(Indices) ||
2656 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2657 return true;
Chris Lattner392be582010-02-12 20:49:41 +00002658 if (!Val0->getType()->isAggregateType())
2659 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002660 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002661 return Error(ID.Loc, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002662 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002663 ID.Kind = ValID::t_Constant;
2664 return false;
2665 }
2666 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002667 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002668 unsigned PredVal, Opc = Lex.getUIntVal();
2669 Constant *Val0, *Val1;
2670 Lex.Lex();
2671 if (ParseCmpPredicate(PredVal, Opc) ||
2672 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2673 ParseGlobalTypeAndValue(Val0) ||
2674 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2675 ParseGlobalTypeAndValue(Val1) ||
2676 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2677 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002678
Chris Lattnerac161bf2009-01-02 07:01:27 +00002679 if (Val0->getType() != Val1->getType())
2680 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002681
Chris Lattnerac161bf2009-01-02 07:01:27 +00002682 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002683
Chris Lattnerac161bf2009-01-02 07:01:27 +00002684 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002685 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002686 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002687 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002688 } else {
2689 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002690 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002691 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002692 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002693 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002694 }
2695 ID.Kind = ValID::t_Constant;
2696 return false;
2697 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002698
Chris Lattnerac161bf2009-01-02 07:01:27 +00002699 // Binary Operators.
2700 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00002701 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002702 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002703 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002704 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00002705 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002706 case lltok::kw_udiv:
2707 case lltok::kw_sdiv:
2708 case lltok::kw_fdiv:
2709 case lltok::kw_urem:
2710 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002711 case lltok::kw_frem:
2712 case lltok::kw_shl:
2713 case lltok::kw_lshr:
2714 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002715 bool NUW = false;
2716 bool NSW = false;
2717 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002718 unsigned Opc = Lex.getUIntVal();
2719 Constant *Val0, *Val1;
2720 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00002721 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00002722 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2723 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002724 if (EatIfPresent(lltok::kw_nuw))
2725 NUW = true;
2726 if (EatIfPresent(lltok::kw_nsw)) {
2727 NSW = true;
2728 if (EatIfPresent(lltok::kw_nuw))
2729 NUW = true;
2730 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00002731 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2732 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002733 if (EatIfPresent(lltok::kw_exact))
2734 Exact = true;
2735 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002736 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2737 ParseGlobalTypeAndValue(Val0) ||
2738 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2739 ParseGlobalTypeAndValue(Val1) ||
2740 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2741 return true;
2742 if (Val0->getType() != Val1->getType())
2743 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002744 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002745 if (NUW)
2746 return Error(ModifierLoc, "nuw only applies to integer operations");
2747 if (NSW)
2748 return Error(ModifierLoc, "nsw only applies to integer operations");
2749 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00002750 // Check that the type is valid for the operator.
2751 switch (Opc) {
2752 case Instruction::Add:
2753 case Instruction::Sub:
2754 case Instruction::Mul:
2755 case Instruction::UDiv:
2756 case Instruction::SDiv:
2757 case Instruction::URem:
2758 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002759 case Instruction::Shl:
2760 case Instruction::AShr:
2761 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00002762 if (!Val0->getType()->isIntOrIntVectorTy())
2763 return Error(ID.Loc, "constexpr requires integer operands");
2764 break;
2765 case Instruction::FAdd:
2766 case Instruction::FSub:
2767 case Instruction::FMul:
2768 case Instruction::FDiv:
2769 case Instruction::FRem:
2770 if (!Val0->getType()->isFPOrFPVectorTy())
2771 return Error(ID.Loc, "constexpr requires fp operands");
2772 break;
2773 default: llvm_unreachable("Unknown binary operator!");
2774 }
Dan Gohman1b849082009-09-07 23:54:19 +00002775 unsigned Flags = 0;
2776 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2777 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002778 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00002779 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00002780 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002781 ID.Kind = ValID::t_Constant;
2782 return false;
2783 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002784
Chris Lattnerac161bf2009-01-02 07:01:27 +00002785 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00002786 case lltok::kw_and:
2787 case lltok::kw_or:
2788 case lltok::kw_xor: {
2789 unsigned Opc = Lex.getUIntVal();
2790 Constant *Val0, *Val1;
2791 Lex.Lex();
2792 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2793 ParseGlobalTypeAndValue(Val0) ||
2794 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2795 ParseGlobalTypeAndValue(Val1) ||
2796 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2797 return true;
2798 if (Val0->getType() != Val1->getType())
2799 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002800 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002801 return Error(ID.Loc,
2802 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002803 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002804 ID.Kind = ValID::t_Constant;
2805 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002806 }
2807
Chris Lattnerac161bf2009-01-02 07:01:27 +00002808 case lltok::kw_getelementptr:
2809 case lltok::kw_shufflevector:
2810 case lltok::kw_insertelement:
2811 case lltok::kw_extractelement:
2812 case lltok::kw_select: {
2813 unsigned Opc = Lex.getUIntVal();
2814 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00002815 bool InBounds = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002816 Lex.Lex();
Dan Gohman1639c392009-07-27 21:53:46 +00002817 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00002818 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002819 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2820 ParseGlobalValueVector(Elts) ||
2821 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2822 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002823
Chris Lattnerac161bf2009-01-02 07:01:27 +00002824 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00002825 if (Elts.size() == 0 ||
2826 !Elts[0]->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002827 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002828
Jay Foaded8db7d2011-07-21 14:31:17 +00002829 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foadd1b78492011-07-25 09:48:08 +00002830 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002831 return Error(ID.Loc, "invalid indices for getelementptr");
Jay Foad2f5fc8c2011-07-21 15:15:37 +00002832 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
2833 InBounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002834 } else if (Opc == Instruction::Select) {
2835 if (Elts.size() != 3)
2836 return Error(ID.Loc, "expected three operands to select");
2837 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2838 Elts[2]))
2839 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00002840 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002841 } else if (Opc == Instruction::ShuffleVector) {
2842 if (Elts.size() != 3)
2843 return Error(ID.Loc, "expected three operands to shufflevector");
2844 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2845 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00002846 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002847 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002848 } else if (Opc == Instruction::ExtractElement) {
2849 if (Elts.size() != 2)
2850 return Error(ID.Loc, "expected two operands to extractelement");
2851 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2852 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002853 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002854 } else {
2855 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2856 if (Elts.size() != 3)
2857 return Error(ID.Loc, "expected three operands to insertelement");
2858 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2859 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00002860 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002861 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002862 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002863
Chris Lattnerac161bf2009-01-02 07:01:27 +00002864 ID.Kind = ValID::t_Constant;
2865 return false;
2866 }
2867 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002868
Chris Lattnerac161bf2009-01-02 07:01:27 +00002869 Lex.Lex();
2870 return false;
2871}
2872
2873/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00002874bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002875 C = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002876 ValID ID;
Craig Topper2617dcc2014-04-15 06:32:26 +00002877 Value *V = nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002878 bool Parsed = ParseValID(ID) ||
Craig Topper2617dcc2014-04-15 06:32:26 +00002879 ConvertValIDToValue(Ty, ID, V, nullptr);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002880 if (V && !(C = dyn_cast<Constant>(V)))
2881 return Error(ID.Loc, "global values must be constants");
2882 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002883}
2884
Victor Hernandez9d75c962010-01-11 22:31:58 +00002885bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002886 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002887 return ParseType(Ty) ||
2888 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002889}
2890
David Majnemerdad0a642014-06-27 18:19:56 +00002891bool LLParser::parseOptionalComdat(Comdat *&C) {
2892 C = nullptr;
2893 if (!EatIfPresent(lltok::kw_comdat))
2894 return false;
2895 if (Lex.getKind() != lltok::ComdatVar)
2896 return TokError("expected comdat variable");
2897 LocTy Loc = Lex.getLoc();
2898 StringRef Name = Lex.getStrVal();
2899 C = getComdat(Name, Loc);
2900 Lex.Lex();
2901 return false;
2902}
2903
Victor Hernandez9d75c962010-01-11 22:31:58 +00002904/// ParseGlobalValueVector
2905/// ::= /*empty*/
2906/// ::= TypeAndValue (',' TypeAndValue)*
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00002907bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) {
Victor Hernandez9d75c962010-01-11 22:31:58 +00002908 // Empty list.
2909 if (Lex.getKind() == lltok::rbrace ||
2910 Lex.getKind() == lltok::rsquare ||
2911 Lex.getKind() == lltok::greater ||
2912 Lex.getKind() == lltok::rparen)
2913 return false;
2914
2915 Constant *C;
2916 if (ParseGlobalTypeAndValue(C)) return true;
2917 Elts.push_back(C);
2918
2919 while (EatIfPresent(lltok::comma)) {
2920 if (ParseGlobalTypeAndValue(C)) return true;
2921 Elts.push_back(C);
2922 }
2923
2924 return false;
2925}
2926
Dan Gohmanc828c542010-08-24 02:24:03 +00002927bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2928 assert(Lex.getKind() == lltok::lbrace);
2929 Lex.Lex();
2930
2931 SmallVector<Value*, 16> Elts;
2932 if (ParseMDNodeVector(Elts, PFS) ||
2933 ParseToken(lltok::rbrace, "expected end of metadata node"))
2934 return true;
2935
Jay Foad5514afe2011-04-21 19:59:31 +00002936 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00002937 ID.Kind = ValID::t_MDNode;
2938 return false;
2939}
2940
Dan Gohman8939ba332010-07-14 18:26:50 +00002941/// ParseMetadataValue
2942/// ::= !42
2943/// ::= !{...}
2944/// ::= !"string"
2945bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2946 assert(Lex.getKind() == lltok::exclaim);
2947 Lex.Lex();
2948
2949 // MDNode:
2950 // !{ ... }
Dan Gohmanc828c542010-08-24 02:24:03 +00002951 if (Lex.getKind() == lltok::lbrace)
2952 return ParseMetadataListValue(ID, PFS);
Dan Gohman8939ba332010-07-14 18:26:50 +00002953
2954 // Standalone metadata reference
2955 // !42
2956 if (Lex.getKind() == lltok::APSInt) {
2957 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2958 ID.Kind = ValID::t_MDNode;
2959 return false;
2960 }
2961
2962 // MDString:
2963 // ::= '!' STRINGCONSTANT
2964 if (ParseMDString(ID.MDStringVal)) return true;
2965 ID.Kind = ValID::t_MDString;
2966 return false;
2967}
2968
Victor Hernandez9d75c962010-01-11 22:31:58 +00002969
2970//===----------------------------------------------------------------------===//
2971// Function Parsing.
2972//===----------------------------------------------------------------------===//
2973
Chris Lattner229907c2011-07-18 04:54:35 +00002974bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +00002975 PerFunctionState *PFS) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002976 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002977 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002978
Chris Lattnerac161bf2009-01-02 07:01:27 +00002979 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002980 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002981 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2982 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002983 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002984 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002985 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2986 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002987 return V == nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002988 case ValID::t_InlineAsm: {
Chris Lattner229907c2011-07-18 04:54:35 +00002989 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002990 FunctionType *FTy =
Craig Topper2617dcc2014-04-15 06:32:26 +00002991 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002992 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2993 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosierf42fad62012-09-05 00:08:17 +00002994 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosierd8c76102012-09-05 19:00:49 +00002995 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00002996 return false;
2997 }
2998 case ValID::t_MDNode:
2999 if (!Ty->isMetadataTy())
3000 return Error(ID.Loc, "metadata value must have metadata type");
3001 V = ID.MDNodeVal;
3002 return false;
3003 case ValID::t_MDString:
3004 if (!Ty->isMetadataTy())
3005 return Error(ID.Loc, "metadata value must have metadata type");
3006 V = ID.MDStringVal;
3007 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003008 case ValID::t_GlobalName:
3009 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003010 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003011 case ValID::t_GlobalID:
3012 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003013 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003014 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00003015 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003016 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00003017 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00003018 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003019 return false;
3020 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00003021 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003022 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
3023 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003024
Dan Gohman518cda42011-12-17 00:04:22 +00003025 // The lexer has no type info, so builds all half, float, and double FP
3026 // constants as double. Fix this here. Long double does not need this.
3027 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003028 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00003029 if (Ty->isHalfTy())
3030 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
3031 &Ignored);
3032 else if (Ty->isFloatTy())
3033 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
3034 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003035 }
Owen Anderson69c464d2009-07-27 20:59:43 +00003036 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003037
Chris Lattner8f57d29e2009-01-05 18:24:23 +00003038 if (V->getType() != Ty)
3039 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003040 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003041
Chris Lattnerac161bf2009-01-02 07:01:27 +00003042 return false;
3043 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00003044 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003045 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003046 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003047 return false;
3048 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00003049 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003050 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00003051 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003052 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003053 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00003054 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00003055 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00003056 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003057 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00003058 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003059 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00003060 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00003061 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003062 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00003063 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003064 return false;
3065 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00003066 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003067 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00003068
Chris Lattnerac161bf2009-01-02 07:01:27 +00003069 V = ID.ConstantVal;
3070 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003071 case ValID::t_ConstantStruct:
3072 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00003073 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003074 if (ST->getNumElements() != ID.UIntVal)
3075 return Error(ID.Loc,
3076 "initializer with struct type has wrong # elements");
3077 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
3078 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003079
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003080 // Verify that the elements are compatible with the structtype.
3081 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
3082 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
3083 return Error(ID.Loc, "element " + Twine(i) +
3084 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003085
Frits van Bommel717d7ed2011-07-18 12:00:32 +00003086 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
3087 ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003088 } else
3089 return Error(ID.Loc, "constant expression type mismatch");
3090 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003091 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00003092 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003093}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003094
Chris Lattner229907c2011-07-18 04:54:35 +00003095bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003096 V = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003097 ValID ID;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003098 return ParseValID(ID, PFS) ||
3099 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003100}
3101
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003102bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003103 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003104 return ParseType(Ty) ||
3105 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003106}
3107
Chris Lattner3ed871f2009-10-27 19:13:16 +00003108bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
3109 PerFunctionState &PFS) {
3110 Value *V;
3111 Loc = Lex.getLoc();
3112 if (ParseTypeAndValue(V, PFS)) return true;
3113 if (!isa<BasicBlock>(V))
3114 return Error(Loc, "expected a basic block");
3115 BB = cast<BasicBlock>(V);
3116 return false;
3117}
3118
3119
Chris Lattnerac161bf2009-01-02 07:01:27 +00003120/// FunctionHeader
3121/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindola45e6c192011-01-08 16:42:36 +00003122/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003123/// OptionalAlign OptGC OptionalPrefix
Chris Lattnerac161bf2009-01-02 07:01:27 +00003124bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
3125 // Parse the linkage.
3126 LocTy LinkageLoc = Lex.getLoc();
3127 unsigned Linkage;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003128
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00003129 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00003130 unsigned DLLStorageClass;
Bill Wendling50d27842012-10-15 20:35:56 +00003131 AttrBuilder RetAttrs;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00003132 unsigned CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003133 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003134 LocTy RetTypeLoc = Lex.getLoc();
3135 if (ParseOptionalLinkage(Linkage) ||
3136 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +00003137 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003138 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003139 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003140 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003141 return true;
3142
3143 // Verify that the linkage is ok.
3144 switch ((GlobalValue::LinkageTypes)Linkage) {
3145 case GlobalValue::ExternalLinkage:
3146 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00003147 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003148 if (isDefine)
3149 return Error(LinkageLoc, "invalid linkage for function definition");
3150 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00003151 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003152 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00003153 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00003154 case GlobalValue::LinkOnceAnyLinkage:
3155 case GlobalValue::LinkOnceODRLinkage:
3156 case GlobalValue::WeakAnyLinkage:
3157 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003158 if (!isDefine)
3159 return Error(LinkageLoc, "invalid linkage for function declaration");
3160 break;
3161 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00003162 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003163 return Error(LinkageLoc, "invalid function linkage type");
3164 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003165
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003166 if (!isValidVisibilityForLinkage(Visibility, Linkage))
3167 return Error(LinkageLoc,
3168 "symbol with local linkage must have default visibility");
3169
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003170 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003171 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003172
Chris Lattnerac161bf2009-01-02 07:01:27 +00003173 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00003174
3175 std::string FunctionName;
3176 if (Lex.getKind() == lltok::GlobalVar) {
3177 FunctionName = Lex.getStrVal();
3178 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
3179 unsigned NameID = Lex.getUIntVal();
3180
3181 if (NameID != NumberedVals.size())
3182 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003183 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00003184 } else {
3185 return TokError("expected function name");
3186 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003187
Chris Lattner3822f632009-01-02 08:05:26 +00003188 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003189
Chris Lattner3822f632009-01-02 08:05:26 +00003190 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003191 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003192
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003193 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003194 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00003195 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003196 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00003197 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003198 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003199 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00003200 std::string GC;
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003201 bool UnnamedAddr;
3202 LocTy UnnamedAddrLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00003203 Constant *Prefix = nullptr;
David Majnemerdad0a642014-06-27 18:19:56 +00003204 Comdat *C;
Chris Lattner3822f632009-01-02 08:05:26 +00003205
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003206 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003207 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
3208 &UnnamedAddrLoc) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003209 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00003210 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00003211 (EatIfPresent(lltok::kw_section) &&
3212 ParseStringConstant(Section)) ||
David Majnemerdad0a642014-06-27 18:19:56 +00003213 parseOptionalComdat(C) ||
Chris Lattner3822f632009-01-02 08:05:26 +00003214 ParseOptionalAlignment(Alignment) ||
3215 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003216 ParseStringConstant(GC)) ||
3217 (EatIfPresent(lltok::kw_prefix) &&
3218 ParseGlobalTypeAndValue(Prefix)))
Chris Lattner3822f632009-01-02 08:05:26 +00003219 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003220
Michael Gottesman41748d72013-06-27 00:25:01 +00003221 if (FuncAttrs.contains(Attribute::Builtin))
3222 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00003223
Chris Lattnerac161bf2009-01-02 07:01:27 +00003224 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00003225 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00003226 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003227 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003228 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003229
Chris Lattnerac161bf2009-01-02 07:01:27 +00003230 // Okay, if we got here, the function is syntactically valid. Convert types
3231 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00003232 std::vector<Type*> ParamTypeList;
Bill Wendlingf5075a42013-01-27 02:24:02 +00003233 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003234
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003235 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003236 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3237 AttributeSet::ReturnIndex,
3238 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003239
Chris Lattnerac161bf2009-01-02 07:01:27 +00003240 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003241 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003242 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3243 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003244 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3245 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003246 }
3247
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003248 if (FuncAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003249 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3250 AttributeSet::FunctionIndex,
3251 FuncAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003252
Bill Wendlinge94d8432012-12-07 23:16:57 +00003253 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003254
Bill Wendling749a43d2012-12-30 13:50:49 +00003255 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003256 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
3257
Chris Lattner229907c2011-07-18 04:54:35 +00003258 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00003259 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00003260 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003261
Craig Topper2617dcc2014-04-15 06:32:26 +00003262 Fn = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003263 if (!FunctionName.empty()) {
3264 // If this was a definition of a forward reference, remove the definition
3265 // from the forward reference table and fill in the forward ref.
3266 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
3267 ForwardRefVals.find(FunctionName);
3268 if (FRVI != ForwardRefVals.end()) {
3269 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00003270 if (!Fn)
3271 return Error(FRVI->second.second, "invalid forward reference to "
3272 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00003273 if (Fn->getType() != PFT)
3274 return Error(FRVI->second.second, "invalid forward reference to "
3275 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003276
Chris Lattnerac161bf2009-01-02 07:01:27 +00003277 ForwardRefVals.erase(FRVI);
3278 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00003279 // Reject redefinitions.
3280 return Error(NameLoc, "invalid redefinition of function '" +
3281 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00003282 } else if (M->getNamedValue(FunctionName)) {
3283 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003284 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003285
Dan Gohman399d6ae2009-08-29 23:37:49 +00003286 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003287 // If this is a definition of a forward referenced function, make sure the
3288 // types agree.
3289 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
3290 = ForwardRefValIDs.find(NumberedVals.size());
3291 if (I != ForwardRefValIDs.end()) {
3292 Fn = cast<Function>(I->second.first);
3293 if (Fn->getType() != PFT)
3294 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003295 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003296 ForwardRefValIDs.erase(I);
3297 }
3298 }
3299
Craig Topper2617dcc2014-04-15 06:32:26 +00003300 if (!Fn)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003301 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
3302 else // Move the forward-reference to the correct spot in the module.
3303 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
3304
3305 if (FunctionName.empty())
3306 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003307
Chris Lattnerac161bf2009-01-02 07:01:27 +00003308 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
3309 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00003310 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003311 Fn->setCallingConv(CC);
3312 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00003313 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003314 Fn->setAlignment(Alignment);
3315 Fn->setSection(Section);
David Majnemerdad0a642014-06-27 18:19:56 +00003316 Fn->setComdat(C);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003317 if (!GC.empty()) Fn->setGC(GC.c_str());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003318 Fn->setPrefixData(Prefix);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003319 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003320
Chris Lattnerac161bf2009-01-02 07:01:27 +00003321 // Add all of the arguments we parsed to the function.
3322 Function::arg_iterator ArgIt = Fn->arg_begin();
3323 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
3324 // If the argument has a name, insert it into the argument symbol table.
3325 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003326
Chris Lattnerac161bf2009-01-02 07:01:27 +00003327 // Set the name, if it conflicted, it will be auto-renamed.
3328 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003329
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00003330 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003331 return Error(ArgList[i].Loc, "redefinition of argument '%" +
3332 ArgList[i].Name + "'");
3333 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003334
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00003335 if (isDefine)
3336 return false;
3337
Robin Morisset039781e2014-08-29 21:53:01 +00003338 // Check the declaration has no block address forward references.
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00003339 ValID ID;
3340 if (FunctionName.empty()) {
3341 ID.Kind = ValID::t_GlobalID;
3342 ID.UIntVal = NumberedVals.size() - 1;
3343 } else {
3344 ID.Kind = ValID::t_GlobalName;
3345 ID.StrVal = FunctionName;
3346 }
3347 auto Blocks = ForwardRefBlockAddresses.find(ID);
3348 if (Blocks != ForwardRefBlockAddresses.end())
3349 return Error(Blocks->first.Loc,
3350 "cannot take blockaddress inside a declaration");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003351 return false;
3352}
3353
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00003354bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() {
3355 ValID ID;
3356 if (FunctionNumber == -1) {
3357 ID.Kind = ValID::t_GlobalName;
3358 ID.StrVal = F.getName();
3359 } else {
3360 ID.Kind = ValID::t_GlobalID;
3361 ID.UIntVal = FunctionNumber;
3362 }
3363
3364 auto Blocks = P.ForwardRefBlockAddresses.find(ID);
3365 if (Blocks == P.ForwardRefBlockAddresses.end())
3366 return false;
3367
3368 for (const auto &I : Blocks->second) {
3369 const ValID &BBID = I.first;
3370 GlobalValue *GV = I.second;
3371
3372 assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) &&
3373 "Expected local id or name");
3374 BasicBlock *BB;
3375 if (BBID.Kind == ValID::t_LocalName)
3376 BB = GetBB(BBID.StrVal, BBID.Loc);
3377 else
3378 BB = GetBB(BBID.UIntVal, BBID.Loc);
3379 if (!BB)
3380 return P.Error(BBID.Loc, "referenced value is not a basic block");
3381
3382 GV->replaceAllUsesWith(BlockAddress::get(&F, BB));
3383 GV->eraseFromParent();
3384 }
3385
3386 P.ForwardRefBlockAddresses.erase(Blocks);
3387 return false;
3388}
Chris Lattnerac161bf2009-01-02 07:01:27 +00003389
3390/// ParseFunctionBody
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00003391/// ::= '{' BasicBlock+ UseListOrderDirective* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00003392bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00003393 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003394 return TokError("expected '{' in function body");
3395 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003396
Chris Lattner3432c622009-10-28 03:39:23 +00003397 int FunctionNumber = -1;
3398 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003399
Chris Lattner3432c622009-10-28 03:39:23 +00003400 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003401
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00003402 // Resolve block addresses and allow basic blocks to be forward-declared
3403 // within this function.
3404 if (PFS.resolveForwardRefBlockAddresses())
3405 return true;
3406 SaveAndRestore<PerFunctionState *> ScopeExit(BlockAddressPFS, &PFS);
3407
Chris Lattnerbbddd962010-01-09 19:20:07 +00003408 // We need at least one basic block.
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00003409 if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder)
Chris Lattnerbbddd962010-01-09 19:20:07 +00003410 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003411
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00003412 while (Lex.getKind() != lltok::rbrace &&
3413 Lex.getKind() != lltok::kw_uselistorder)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003414 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003415
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00003416 while (Lex.getKind() != lltok::rbrace)
3417 if (ParseUseListOrder(&PFS))
3418 return true;
3419
Chris Lattnerac161bf2009-01-02 07:01:27 +00003420 // Eat the }.
3421 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003422
Chris Lattnerac161bf2009-01-02 07:01:27 +00003423 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00003424 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003425}
3426
3427/// ParseBasicBlock
3428/// ::= LabelStr? Instruction*
3429bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
3430 // If this basic block starts out with a name, remember it.
3431 std::string Name;
3432 LocTy NameLoc = Lex.getLoc();
3433 if (Lex.getKind() == lltok::LabelStr) {
3434 Name = Lex.getStrVal();
3435 Lex.Lex();
3436 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003437
Chris Lattnerac161bf2009-01-02 07:01:27 +00003438 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003439 if (!BB) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003440
Chris Lattnerac161bf2009-01-02 07:01:27 +00003441 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003442
Chris Lattnerac161bf2009-01-02 07:01:27 +00003443 // Parse the instructions in this block until we get a terminator.
3444 Instruction *Inst;
3445 do {
3446 // This instruction may have three possibilities for a name: a) none
3447 // specified, b) name specified "%foo =", c) number specified: "%4 =".
3448 LocTy NameLoc = Lex.getLoc();
3449 int NameID = -1;
3450 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003451
Chris Lattnerac161bf2009-01-02 07:01:27 +00003452 if (Lex.getKind() == lltok::LocalVarID) {
3453 NameID = Lex.getUIntVal();
3454 Lex.Lex();
3455 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
3456 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00003457 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003458 NameStr = Lex.getStrVal();
3459 Lex.Lex();
3460 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
3461 return true;
3462 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003463
Chris Lattner77b89dc2009-12-30 05:23:43 +00003464 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00003465 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00003466 case InstError: return true;
3467 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003468 BB->getInstList().push_back(Inst);
3469
Chris Lattner77b89dc2009-12-30 05:23:43 +00003470 // With a normal result, we check to see if the instruction is followed by
3471 // a comma and metadata.
3472 if (EatIfPresent(lltok::comma))
Dan Gohman338d9a42010-08-24 02:05:17 +00003473 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003474 return true;
3475 break;
3476 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003477 BB->getInstList().push_back(Inst);
3478
Chris Lattner77b89dc2009-12-30 05:23:43 +00003479 // If the instruction parser ate an extra comma at the end of it, it
3480 // *must* be followed by metadata.
Dan Gohman338d9a42010-08-24 02:05:17 +00003481 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003482 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003483 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00003484 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003485
Chris Lattnerac161bf2009-01-02 07:01:27 +00003486 // Set the name on the instruction.
3487 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
3488 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003489
Chris Lattnerac161bf2009-01-02 07:01:27 +00003490 return false;
3491}
3492
3493//===----------------------------------------------------------------------===//
3494// Instruction Parsing.
3495//===----------------------------------------------------------------------===//
3496
3497/// ParseInstruction - Parse one of the many different instructions.
3498///
Chris Lattner77b89dc2009-12-30 05:23:43 +00003499int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
3500 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003501 lltok::Kind Token = Lex.getKind();
3502 if (Token == lltok::Eof)
3503 return TokError("found end of file when expecting more instructions");
3504 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00003505 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003506 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003507
Chris Lattnerac161bf2009-01-02 07:01:27 +00003508 switch (Token) {
3509 default: return Error(Loc, "expected instruction opcode");
3510 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00003511 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003512 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
3513 case lltok::kw_br: return ParseBr(Inst, PFS);
3514 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003515 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003516 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00003517 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003518 // Binary Operators.
3519 case lltok::kw_add:
3520 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003521 case lltok::kw_mul:
3522 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00003523 bool NUW = EatIfPresent(lltok::kw_nuw);
3524 bool NSW = EatIfPresent(lltok::kw_nsw);
3525 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003526
Chris Lattnera676c0f2011-02-07 16:40:21 +00003527 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003528
Chris Lattnera676c0f2011-02-07 16:40:21 +00003529 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3530 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3531 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003532 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003533 case lltok::kw_fadd:
3534 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00003535 case lltok::kw_fmul:
3536 case lltok::kw_fdiv:
3537 case lltok::kw_frem: {
3538 FastMathFlags FMF = EatFastMathFlagsIfPresent();
3539 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
3540 if (Res != 0)
3541 return Res;
3542 if (FMF.any())
3543 Inst->setFastMathFlags(FMF);
3544 return 0;
3545 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003546
Chris Lattner35315d02011-02-06 21:44:57 +00003547 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003548 case lltok::kw_udiv:
3549 case lltok::kw_lshr:
3550 case lltok::kw_ashr: {
3551 bool Exact = EatIfPresent(lltok::kw_exact);
3552
3553 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3554 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3555 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003556 }
3557
Chris Lattnerac161bf2009-01-02 07:01:27 +00003558 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00003559 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003560 case lltok::kw_and:
3561 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00003562 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003563 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003564 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003565 // Casts.
3566 case lltok::kw_trunc:
3567 case lltok::kw_zext:
3568 case lltok::kw_sext:
3569 case lltok::kw_fptrunc:
3570 case lltok::kw_fpext:
3571 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003572 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003573 case lltok::kw_uitofp:
3574 case lltok::kw_sitofp:
3575 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003576 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003577 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00003578 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003579 // Other.
3580 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00003581 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003582 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3583 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3584 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3585 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00003586 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +00003587 // Call.
3588 case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None);
3589 case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail);
3590 case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003591 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00003592 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00003593 case lltok::kw_load: return ParseLoad(Inst, PFS);
3594 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00003595 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
3596 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003597 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003598 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3599 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3600 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3601 }
3602}
3603
3604/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3605bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003606 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003607 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003608 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003609 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3610 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3611 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3612 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3613 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3614 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3615 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3616 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3617 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3618 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3619 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3620 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3621 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3622 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3623 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3624 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3625 }
3626 } else {
3627 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003628 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003629 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3630 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3631 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3632 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3633 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3634 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3635 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3636 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3637 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3638 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3639 }
3640 }
3641 Lex.Lex();
3642 return false;
3643}
3644
3645//===----------------------------------------------------------------------===//
3646// Terminator Instructions.
3647//===----------------------------------------------------------------------===//
3648
3649/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00003650/// ::= 'ret' void (',' !dbg, !1)*
3651/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00003652bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003653 PerFunctionState &PFS) {
3654 SMLoc TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00003655 Type *Ty = nullptr;
Chris Lattnerf880ca22009-03-09 04:49:14 +00003656 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003657
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003658 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003659
Chris Lattnerfdd87902009-10-05 05:54:46 +00003660 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003661 if (!ResType->isVoidTy())
3662 return Error(TypeLoc, "value doesn't match function result type '" +
3663 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003664
Owen Anderson55f1c092009-08-13 21:58:54 +00003665 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003666 return false;
3667 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003668
Chris Lattnerac161bf2009-01-02 07:01:27 +00003669 Value *RV;
3670 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003671
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003672 if (ResType != RV->getType())
3673 return Error(TypeLoc, "value doesn't match function result type '" +
3674 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003675
Owen Anderson55f1c092009-08-13 21:58:54 +00003676 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00003677 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003678}
3679
3680
3681/// ParseBr
3682/// ::= 'br' TypeAndValue
3683/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3684bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3685 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003686 Value *Op0;
3687 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003688 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003689
Chris Lattnerac161bf2009-01-02 07:01:27 +00003690 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3691 Inst = BranchInst::Create(BB);
3692 return false;
3693 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003694
Owen Anderson55f1c092009-08-13 21:58:54 +00003695 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003696 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003697
Chris Lattnerac161bf2009-01-02 07:01:27 +00003698 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003699 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003700 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003701 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003702 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003703
Chris Lattner3ed871f2009-10-27 19:13:16 +00003704 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003705 return false;
3706}
3707
3708/// ParseSwitch
3709/// Instruction
3710/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3711/// JumpTable
3712/// ::= (TypeAndValue ',' TypeAndValue)*
3713bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3714 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003715 Value *Cond;
3716 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003717 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3718 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003719 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003720 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3721 return true;
3722
Duncan Sands19d0b472010-02-16 11:11:14 +00003723 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003724 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003725
Chris Lattnerac161bf2009-01-02 07:01:27 +00003726 // Parse the jump table pairs.
3727 SmallPtrSet<Value*, 32> SeenCases;
3728 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3729 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003730 Value *Constant;
3731 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003732
Chris Lattnerac161bf2009-01-02 07:01:27 +00003733 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3734 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003735 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003736 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003737
Chris Lattnerac161bf2009-01-02 07:01:27 +00003738 if (!SeenCases.insert(Constant))
3739 return Error(CondLoc, "duplicate case value in switch");
3740 if (!isa<ConstantInt>(Constant))
3741 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003742
Chris Lattner3ed871f2009-10-27 19:13:16 +00003743 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003744 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003745
Chris Lattnerac161bf2009-01-02 07:01:27 +00003746 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003747
Chris Lattner3ed871f2009-10-27 19:13:16 +00003748 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003749 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3750 SI->addCase(Table[i].first, Table[i].second);
3751 Inst = SI;
3752 return false;
3753}
3754
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003755/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00003756/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003757/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3758bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003759 LocTy AddrLoc;
3760 Value *Address;
3761 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003762 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3763 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00003764 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003765
Duncan Sands19d0b472010-02-16 11:11:14 +00003766 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003767 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003768
Chris Lattner3ed871f2009-10-27 19:13:16 +00003769 // Parse the destination list.
3770 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003771
Chris Lattner3ed871f2009-10-27 19:13:16 +00003772 if (Lex.getKind() != lltok::rsquare) {
3773 BasicBlock *DestBB;
3774 if (ParseTypeAndBasicBlock(DestBB, PFS))
3775 return true;
3776 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003777
Chris Lattner3ed871f2009-10-27 19:13:16 +00003778 while (EatIfPresent(lltok::comma)) {
3779 if (ParseTypeAndBasicBlock(DestBB, PFS))
3780 return true;
3781 DestList.push_back(DestBB);
3782 }
3783 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003784
Chris Lattner3ed871f2009-10-27 19:13:16 +00003785 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3786 return true;
3787
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003788 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00003789 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3790 IBI->addDestination(DestList[i]);
3791 Inst = IBI;
3792 return false;
3793}
3794
3795
Chris Lattnerac161bf2009-01-02 07:01:27 +00003796/// ParseInvoke
3797/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3798/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3799bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3800 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00003801 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003802 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00003803 LocTy NoBuiltinLoc;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00003804 unsigned CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003805 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003806 LocTy RetTypeLoc;
3807 ValID CalleeID;
3808 SmallVector<ParamInfo, 16> ArgList;
3809
Chris Lattner3ed871f2009-10-27 19:13:16 +00003810 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003811 if (ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003812 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003813 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003814 ParseValID(CalleeID) ||
3815 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003816 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
3817 NoBuiltinLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003818 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003819 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003820 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003821 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003822 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003823
Chris Lattnerac161bf2009-01-02 07:01:27 +00003824 // If RetType is a non-function pointer type, then this is the short syntax
3825 // for the call, which means that RetType is just the return type. Infer the
3826 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00003827 PointerType *PFTy = nullptr;
3828 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003829 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3830 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3831 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00003832 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003833 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3834 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003835
Chris Lattnerac161bf2009-01-02 07:01:27 +00003836 if (!FunctionType::isValidReturnType(RetType))
3837 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003838
Owen Anderson4056ca92009-07-29 22:17:13 +00003839 Ty = FunctionType::get(RetType, ParamTypes, false);
3840 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003841 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003842
Chris Lattnerac161bf2009-01-02 07:01:27 +00003843 // Look up the callee.
3844 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003845 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003846
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003847 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00003848 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003849 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003850 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3851 AttributeSet::ReturnIndex,
3852 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003853
Chris Lattnerac161bf2009-01-02 07:01:27 +00003854 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003855
Chris Lattnerac161bf2009-01-02 07:01:27 +00003856 // Loop through FunctionType's arguments and ensure they are specified
3857 // correctly. Also, gather any parameter attributes.
3858 FunctionType::param_iterator I = Ty->param_begin();
3859 FunctionType::param_iterator E = Ty->param_end();
3860 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003861 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003862 if (I != E) {
3863 ExpectedTy = *I++;
3864 } else if (!Ty->isVarArg()) {
3865 return Error(ArgList[i].Loc, "too many arguments specified");
3866 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003867
Chris Lattnerac161bf2009-01-02 07:01:27 +00003868 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3869 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003870 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003871 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003872 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3873 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003874 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3875 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003876 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003877
Chris Lattnerac161bf2009-01-02 07:01:27 +00003878 if (I != E)
3879 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003880
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003881 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003882 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3883 AttributeSet::FunctionIndex,
3884 FnAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003885
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003886 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00003887 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003888
Jay Foad5bd375a2011-07-15 08:37:34 +00003889 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003890 II->setCallingConv(CC);
3891 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003892 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003893 Inst = II;
3894 return false;
3895}
3896
Bill Wendlingf891bf82011-07-31 06:30:59 +00003897/// ParseResume
3898/// ::= 'resume' TypeAndValue
3899bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
3900 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00003901 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
3902 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003903
Bill Wendlingf891bf82011-07-31 06:30:59 +00003904 ResumeInst *RI = ResumeInst::Create(Exn);
3905 Inst = RI;
3906 return false;
3907}
Chris Lattnerac161bf2009-01-02 07:01:27 +00003908
3909//===----------------------------------------------------------------------===//
3910// Binary Operators.
3911//===----------------------------------------------------------------------===//
3912
3913/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003914/// ::= ArithmeticOps TypeAndValue ',' Value
3915///
3916/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3917/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00003918bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003919 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003920 LocTy Loc; Value *LHS, *RHS;
3921 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3922 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3923 ParseValue(LHS->getType(), RHS, PFS))
3924 return true;
3925
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003926 bool Valid;
3927 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003928 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003929 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00003930 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3931 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003932 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00003933 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3934 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003935 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003936
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003937 if (!Valid)
3938 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003939
Chris Lattnerac161bf2009-01-02 07:01:27 +00003940 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3941 return false;
3942}
3943
3944/// ParseLogical
3945/// ::= ArithmeticOps TypeAndValue ',' Value {
3946bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3947 unsigned Opc) {
3948 LocTy Loc; Value *LHS, *RHS;
3949 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3950 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3951 ParseValue(LHS->getType(), RHS, PFS))
3952 return true;
3953
Duncan Sands9dff9be2010-02-15 16:12:20 +00003954 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003955 return Error(Loc,"instruction requires integer or integer vector operands");
3956
3957 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3958 return false;
3959}
3960
3961
3962/// ParseCompare
3963/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3964/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00003965bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3966 unsigned Opc) {
3967 // Parse the integer/fp comparison predicate.
3968 LocTy Loc;
3969 unsigned Pred;
3970 Value *LHS, *RHS;
3971 if (ParseCmpPredicate(Pred, Opc) ||
3972 ParseTypeAndValue(LHS, Loc, PFS) ||
3973 ParseToken(lltok::comma, "expected ',' after compare value") ||
3974 ParseValue(LHS->getType(), RHS, PFS))
3975 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003976
Chris Lattnerac161bf2009-01-02 07:01:27 +00003977 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00003978 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003979 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003980 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003981 } else {
3982 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00003983 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00003984 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003985 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003986 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003987 }
3988 return false;
3989}
3990
3991//===----------------------------------------------------------------------===//
3992// Other Instructions.
3993//===----------------------------------------------------------------------===//
3994
3995
3996/// ParseCast
3997/// ::= CastOpc TypeAndValue 'to' Type
3998bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3999 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004000 LocTy Loc;
4001 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00004002 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004003 if (ParseTypeAndValue(Op, Loc, PFS) ||
4004 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
4005 ParseType(DestTy))
4006 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004007
Chris Lattner89d856e2009-03-01 00:53:13 +00004008 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
4009 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004010 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004011 getTypeString(Op->getType()) + "' to '" +
4012 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00004013 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004014 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
4015 return false;
4016}
4017
4018/// ParseSelect
4019/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
4020bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
4021 LocTy Loc;
4022 Value *Op0, *Op1, *Op2;
4023 if (ParseTypeAndValue(Op0, Loc, PFS) ||
4024 ParseToken(lltok::comma, "expected ',' after select condition") ||
4025 ParseTypeAndValue(Op1, PFS) ||
4026 ParseToken(lltok::comma, "expected ',' after select value") ||
4027 ParseTypeAndValue(Op2, PFS))
4028 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004029
Chris Lattnerac161bf2009-01-02 07:01:27 +00004030 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
4031 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004032
Chris Lattnerac161bf2009-01-02 07:01:27 +00004033 Inst = SelectInst::Create(Op0, Op1, Op2);
4034 return false;
4035}
4036
Chris Lattnerb55ab542009-01-05 08:18:44 +00004037/// ParseVA_Arg
4038/// ::= 'va_arg' TypeAndValue ',' Type
4039bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004040 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00004041 Type *EltTy = nullptr;
Chris Lattnerb55ab542009-01-05 08:18:44 +00004042 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004043 if (ParseTypeAndValue(Op, PFS) ||
4044 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00004045 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004046 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004047
Chris Lattnerb55ab542009-01-05 08:18:44 +00004048 if (!EltTy->isFirstClassType())
4049 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004050
4051 Inst = new VAArgInst(Op, EltTy);
4052 return false;
4053}
4054
4055/// ParseExtractElement
4056/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
4057bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
4058 LocTy Loc;
4059 Value *Op0, *Op1;
4060 if (ParseTypeAndValue(Op0, Loc, PFS) ||
4061 ParseToken(lltok::comma, "expected ',' after extract value") ||
4062 ParseTypeAndValue(Op1, PFS))
4063 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004064
Chris Lattnerac161bf2009-01-02 07:01:27 +00004065 if (!ExtractElementInst::isValidOperands(Op0, Op1))
4066 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004067
Eric Christopherc9742252009-07-25 02:28:41 +00004068 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004069 return false;
4070}
4071
4072/// ParseInsertElement
4073/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
4074bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
4075 LocTy Loc;
4076 Value *Op0, *Op1, *Op2;
4077 if (ParseTypeAndValue(Op0, Loc, PFS) ||
4078 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
4079 ParseTypeAndValue(Op1, PFS) ||
4080 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
4081 ParseTypeAndValue(Op2, PFS))
4082 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004083
Chris Lattnerac161bf2009-01-02 07:01:27 +00004084 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00004085 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004086
Chris Lattnerac161bf2009-01-02 07:01:27 +00004087 Inst = InsertElementInst::Create(Op0, Op1, Op2);
4088 return false;
4089}
4090
4091/// ParseShuffleVector
4092/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
4093bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
4094 LocTy Loc;
4095 Value *Op0, *Op1, *Op2;
4096 if (ParseTypeAndValue(Op0, Loc, PFS) ||
4097 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
4098 ParseTypeAndValue(Op1, PFS) ||
4099 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
4100 ParseTypeAndValue(Op2, PFS))
4101 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004102
Chris Lattnerac161bf2009-01-02 07:01:27 +00004103 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00004104 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004105
Chris Lattnerac161bf2009-01-02 07:01:27 +00004106 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
4107 return false;
4108}
4109
4110/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00004111/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00004112int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004113 Type *Ty = nullptr; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004114 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004115
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004116 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004117 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
4118 ParseValue(Ty, Op0, PFS) ||
4119 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00004120 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004121 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
4122 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004123
Chris Lattnerf4f03422009-12-30 05:27:33 +00004124 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004125 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
4126 while (1) {
4127 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004128
Chris Lattner3822f632009-01-02 08:05:26 +00004129 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004130 break;
4131
Chris Lattnerf4f03422009-12-30 05:27:33 +00004132 if (Lex.getKind() == lltok::MetadataVar) {
4133 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00004134 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004135 }
Devang Patel8f842d32009-10-16 18:45:49 +00004136
Chris Lattner3822f632009-01-02 08:05:26 +00004137 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004138 ParseValue(Ty, Op0, PFS) ||
4139 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00004140 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004141 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
4142 return true;
4143 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004144
Chris Lattnerac161bf2009-01-02 07:01:27 +00004145 if (!Ty->isFirstClassType())
4146 return Error(TypeLoc, "phi node must have first class type");
4147
Jay Foad52131342011-03-30 11:28:46 +00004148 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00004149 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
4150 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
4151 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004152 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004153}
4154
Bill Wendlingfae14752011-08-12 20:24:12 +00004155/// ParseLandingPad
4156/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
4157/// Clause
4158/// ::= 'catch' TypeAndValue
4159/// ::= 'filter'
4160/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
4161bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004162 Type *Ty = nullptr; LocTy TyLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00004163 Value *PersFn; LocTy PersFnLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00004164
4165 if (ParseType(Ty, TyLoc) ||
4166 ParseToken(lltok::kw_personality, "expected 'personality'") ||
4167 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
4168 return true;
4169
4170 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
4171 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
4172
4173 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
4174 LandingPadInst::ClauseType CT;
4175 if (EatIfPresent(lltok::kw_catch))
4176 CT = LandingPadInst::Catch;
4177 else if (EatIfPresent(lltok::kw_filter))
4178 CT = LandingPadInst::Filter;
4179 else
4180 return TokError("expected 'catch' or 'filter' clause type");
4181
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00004182 Value *V;
4183 LocTy VLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00004184 if (ParseTypeAndValue(V, VLoc, PFS)) {
4185 delete LP;
4186 return true;
4187 }
4188
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00004189 // A 'catch' type expects a non-array constant. A filter clause expects an
4190 // array constant.
4191 if (CT == LandingPadInst::Catch) {
4192 if (isa<ArrayType>(V->getType()))
4193 Error(VLoc, "'catch' clause has an invalid type");
4194 } else {
4195 if (!isa<ArrayType>(V->getType()))
4196 Error(VLoc, "'filter' clause has an invalid type");
4197 }
4198
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00004199 LP->addClause(cast<Constant>(V));
Bill Wendlingfae14752011-08-12 20:24:12 +00004200 }
4201
4202 Inst = LP;
4203 return false;
4204}
4205
Chris Lattnerac161bf2009-01-02 07:01:27 +00004206/// ParseCall
Reid Kleckner5772b772014-04-24 20:14:34 +00004207/// ::= 'call' OptionalCallingConv OptionalAttrs Type Value
4208/// ParameterList OptionalAttrs
4209/// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value
4210/// ParameterList OptionalAttrs
4211/// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00004212/// ParameterList OptionalAttrs
4213bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
Reid Kleckner5772b772014-04-24 20:14:34 +00004214 CallInst::TailCallKind TCK) {
Bill Wendling50d27842012-10-15 20:35:56 +00004215 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004216 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00004217 LocTy BuiltinLoc;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00004218 unsigned CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004219 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004220 LocTy RetTypeLoc;
4221 ValID CalleeID;
4222 SmallVector<ParamInfo, 16> ArgList;
4223 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004224
Reid Kleckner5772b772014-04-24 20:14:34 +00004225 if ((TCK != CallInst::TCK_None &&
4226 ParseToken(lltok::kw_call, "expected 'tail call'")) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004227 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004228 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004229 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004230 ParseValID(CalleeID) ||
Reid Kleckner83498642014-08-26 00:33:28 +00004231 ParseParameterList(ArgList, PFS, TCK == CallInst::TCK_MustTail,
4232 PFS.getFunction().isVarArg()) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004233 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00004234 BuiltinLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004235 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004236
Chris Lattnerac161bf2009-01-02 07:01:27 +00004237 // If RetType is a non-function pointer type, then this is the short syntax
4238 // for the call, which means that RetType is just the return type. Infer the
4239 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00004240 PointerType *PFTy = nullptr;
4241 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004242 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
4243 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
4244 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00004245 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00004246 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
4247 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004248
Chris Lattnerac161bf2009-01-02 07:01:27 +00004249 if (!FunctionType::isValidReturnType(RetType))
4250 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004251
Owen Anderson4056ca92009-07-29 22:17:13 +00004252 Ty = FunctionType::get(RetType, ParamTypes, false);
4253 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004254 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004255
Chris Lattnerac161bf2009-01-02 07:01:27 +00004256 // Look up the callee.
4257 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00004258 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004259
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004260 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00004261 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004262 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004263 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4264 AttributeSet::ReturnIndex,
4265 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004266
Chris Lattnerac161bf2009-01-02 07:01:27 +00004267 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004268
Chris Lattnerac161bf2009-01-02 07:01:27 +00004269 // Loop through FunctionType's arguments and ensure they are specified
4270 // correctly. Also, gather any parameter attributes.
4271 FunctionType::param_iterator I = Ty->param_begin();
4272 FunctionType::param_iterator E = Ty->param_end();
4273 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004274 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004275 if (I != E) {
4276 ExpectedTy = *I++;
4277 } else if (!Ty->isVarArg()) {
4278 return Error(ArgList[i].Loc, "too many arguments specified");
4279 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004280
Chris Lattnerac161bf2009-01-02 07:01:27 +00004281 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
4282 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004283 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004284 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004285 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4286 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004287 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4288 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004289 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004290
Chris Lattnerac161bf2009-01-02 07:01:27 +00004291 if (I != E)
4292 return Error(CallLoc, "not enough parameters specified for call");
4293
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004294 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004295 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4296 AttributeSet::FunctionIndex,
4297 FnAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004298
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004299 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00004300 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004301
Jay Foad5bd375a2011-07-15 08:37:34 +00004302 CallInst *CI = CallInst::Create(Callee, Args);
Reid Kleckner5772b772014-04-24 20:14:34 +00004303 CI->setTailCallKind(TCK);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004304 CI->setCallingConv(CC);
4305 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004306 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004307 Inst = CI;
4308 return false;
4309}
4310
4311//===----------------------------------------------------------------------===//
4312// Memory Instructions.
4313//===----------------------------------------------------------------------===//
4314
4315/// ParseAlloc
David Majnemerc4ab61c2014-03-09 06:41:58 +00004316/// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)?
Chris Lattner78103722011-06-17 03:16:47 +00004317int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004318 Value *Size = nullptr;
Chris Lattner200e0752009-07-02 23:08:13 +00004319 LocTy SizeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004320 unsigned Alignment = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00004321 Type *Ty = nullptr;
David Majnemerc4ab61c2014-03-09 06:41:58 +00004322
4323 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
4324
Chris Lattner3822f632009-01-02 08:05:26 +00004325 if (ParseType(Ty)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004326
Chris Lattnerb2f39502009-12-30 05:44:30 +00004327 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004328 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00004329 if (Lex.getKind() == lltok::kw_align) {
4330 if (ParseOptionalAlignment(Alignment)) return true;
4331 } else if (Lex.getKind() == lltok::MetadataVar) {
4332 AteExtraComma = true;
4333 } else {
4334 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
4335 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4336 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004337 }
4338 }
4339
Dan Gohman2140a742010-05-28 01:14:11 +00004340 if (Size && !Size->getType()->isIntegerTy())
4341 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004342
Reid Kleckner436c42e2014-01-17 23:58:17 +00004343 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
4344 AI->setUsedWithInAlloca(IsInAlloca);
4345 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00004346 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004347}
4348
4349/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00004350/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004351/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00004352/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004353int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004354 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004355 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004356 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004357 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004358 AtomicOrdering Ordering = NotAtomic;
4359 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004360
4361 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004362 isAtomic = true;
4363 Lex.Lex();
4364 }
4365
Chris Lattnerbc639292011-11-27 06:56:53 +00004366 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004367 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004368 isVolatile = true;
4369 Lex.Lex();
4370 }
4371
Chris Lattnerb2f39502009-12-30 05:44:30 +00004372 if (ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004373 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004374 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4375 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004376
Duncan Sands19d0b472010-02-16 11:11:14 +00004377 if (!Val->getType()->isPointerTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004378 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
4379 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00004380 if (isAtomic && !Alignment)
4381 return Error(Loc, "atomic load must have explicit non-zero alignment");
4382 if (Ordering == Release || Ordering == AcquireRelease)
4383 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004384
Eli Friedman59b66882011-08-09 23:02:53 +00004385 Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004386 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004387}
4388
4389/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00004390
4391/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
4392/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00004393/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004394int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004395 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004396 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004397 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004398 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004399 AtomicOrdering Ordering = NotAtomic;
4400 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004401
4402 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004403 isAtomic = true;
4404 Lex.Lex();
4405 }
4406
Chris Lattnerbc639292011-11-27 06:56:53 +00004407 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004408 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004409 isVolatile = true;
4410 Lex.Lex();
4411 }
4412
Chris Lattnerac161bf2009-01-02 07:01:27 +00004413 if (ParseTypeAndValue(Val, Loc, PFS) ||
4414 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004415 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004416 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004417 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004418 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00004419
Duncan Sands19d0b472010-02-16 11:11:14 +00004420 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004421 return Error(PtrLoc, "store operand must be a pointer");
4422 if (!Val->getType()->isFirstClassType())
4423 return Error(Loc, "store operand must be a first class value");
4424 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4425 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00004426 if (isAtomic && !Alignment)
4427 return Error(Loc, "atomic store must have explicit non-zero alignment");
4428 if (Ordering == Acquire || Ordering == AcquireRelease)
4429 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004430
Eli Friedman59b66882011-08-09 23:02:53 +00004431 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004432 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004433}
4434
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004435/// ParseCmpXchg
Tim Northover420a2162014-06-13 14:24:07 +00004436/// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
4437/// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00004438int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004439 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
4440 bool AteExtraComma = false;
Tim Northovere94a5182014-03-11 10:48:52 +00004441 AtomicOrdering SuccessOrdering = NotAtomic;
4442 AtomicOrdering FailureOrdering = NotAtomic;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004443 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004444 bool isVolatile = false;
Tim Northover420a2162014-06-13 14:24:07 +00004445 bool isWeak = false;
4446
4447 if (EatIfPresent(lltok::kw_weak))
4448 isWeak = true;
Eli Friedman02e737b2011-08-12 22:50:01 +00004449
4450 if (EatIfPresent(lltok::kw_volatile))
4451 isVolatile = true;
4452
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004453 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4454 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
4455 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
4456 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
4457 ParseTypeAndValue(New, NewLoc, PFS) ||
Tim Northovere94a5182014-03-11 10:48:52 +00004458 ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) ||
4459 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004460 return true;
4461
Tim Northovere94a5182014-03-11 10:48:52 +00004462 if (SuccessOrdering == Unordered || FailureOrdering == Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004463 return TokError("cmpxchg cannot be unordered");
Tim Northovere94a5182014-03-11 10:48:52 +00004464 if (SuccessOrdering < FailureOrdering)
4465 return TokError("cmpxchg must be at least as ordered on success as failure");
4466 if (FailureOrdering == Release || FailureOrdering == AcquireRelease)
4467 return TokError("cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004468 if (!Ptr->getType()->isPointerTy())
4469 return Error(PtrLoc, "cmpxchg operand must be a pointer");
4470 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
4471 return Error(CmpLoc, "compare value and pointer type do not match");
4472 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
4473 return Error(NewLoc, "new value and pointer type do not match");
4474 if (!New->getType()->isIntegerTy())
4475 return Error(NewLoc, "cmpxchg operand must be an integer");
4476 unsigned Size = New->getType()->getPrimitiveSizeInBits();
4477 if (Size < 8 || (Size & (Size - 1)))
4478 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
4479 " integer");
4480
Tim Northover420a2162014-06-13 14:24:07 +00004481 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(
4482 Ptr, Cmp, New, SuccessOrdering, FailureOrdering, Scope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004483 CXI->setVolatile(isVolatile);
Tim Northover420a2162014-06-13 14:24:07 +00004484 CXI->setWeak(isWeak);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004485 Inst = CXI;
4486 return AteExtraComma ? InstExtraComma : InstNormal;
4487}
4488
4489/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00004490/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
4491/// 'singlethread'? AtomicOrdering
4492int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004493 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
4494 bool AteExtraComma = false;
4495 AtomicOrdering Ordering = NotAtomic;
4496 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004497 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004498 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00004499
4500 if (EatIfPresent(lltok::kw_volatile))
4501 isVolatile = true;
4502
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004503 switch (Lex.getKind()) {
4504 default: return TokError("expected binary operation in atomicrmw");
4505 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
4506 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
4507 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
4508 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
4509 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
4510 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
4511 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
4512 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
4513 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
4514 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
4515 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
4516 }
4517 Lex.Lex(); // Eat the operation.
4518
4519 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4520 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
4521 ParseTypeAndValue(Val, ValLoc, PFS) ||
4522 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4523 return true;
4524
4525 if (Ordering == Unordered)
4526 return TokError("atomicrmw cannot be unordered");
4527 if (!Ptr->getType()->isPointerTy())
4528 return Error(PtrLoc, "atomicrmw operand must be a pointer");
4529 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4530 return Error(ValLoc, "atomicrmw value and pointer type do not match");
4531 if (!Val->getType()->isIntegerTy())
4532 return Error(ValLoc, "atomicrmw operand must be an integer");
4533 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
4534 if (Size < 8 || (Size & (Size - 1)))
4535 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
4536 " integer");
4537
4538 AtomicRMWInst *RMWI =
4539 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
4540 RMWI->setVolatile(isVolatile);
4541 Inst = RMWI;
4542 return AteExtraComma ? InstExtraComma : InstNormal;
4543}
4544
Eli Friedmanfee02c62011-07-25 23:16:38 +00004545/// ParseFence
4546/// ::= 'fence' 'singlethread'? AtomicOrdering
4547int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
4548 AtomicOrdering Ordering = NotAtomic;
4549 SynchronizationScope Scope = CrossThread;
4550 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4551 return true;
4552
4553 if (Ordering == Unordered)
4554 return TokError("fence cannot be unordered");
4555 if (Ordering == Monotonic)
4556 return TokError("fence cannot be monotonic");
4557
4558 Inst = new FenceInst(Context, Ordering, Scope);
4559 return InstNormal;
4560}
4561
Chris Lattnerac161bf2009-01-02 07:01:27 +00004562/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00004563/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00004564int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004565 Value *Ptr = nullptr;
4566 Value *Val = nullptr;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004567 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00004568
Dan Gohman16cbbe42009-07-29 15:58:36 +00004569 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00004570
Chris Lattner3822f632009-01-02 08:05:26 +00004571 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004572
Eli Benderskyd9806682013-04-22 17:03:42 +00004573 Type *BaseType = Ptr->getType();
4574 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
4575 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004576 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004577
Chris Lattnerac161bf2009-01-02 07:01:27 +00004578 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004579 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004580 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00004581 if (Lex.getKind() == lltok::MetadataVar) {
4582 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00004583 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004584 }
Chris Lattner3822f632009-01-02 08:05:26 +00004585 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004586 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004587 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem3924cb02011-12-05 06:29:09 +00004588 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
4589 return Error(EltLoc, "getelementptr index type missmatch");
4590 if (Val->getType()->isVectorTy()) {
4591 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
4592 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
4593 if (ValNumEl != PtrNumEl)
4594 return Error(EltLoc,
4595 "getelementptr vector index has a wrong number of elements");
4596 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004597 Indices.push_back(Val);
4598 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004599
Eli Benderskyd9806682013-04-22 17:03:42 +00004600 if (!Indices.empty() && !BasePointerType->getElementType()->isSized())
4601 return Error(Loc, "base element of getelementptr must be sized");
4602
4603 if (!GetElementPtrInst::getIndexedType(BaseType, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004604 return Error(Loc, "invalid getelementptr indices");
Jay Foadd1b78492011-07-25 09:48:08 +00004605 Inst = GetElementPtrInst::Create(Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00004606 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00004607 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004608 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004609}
4610
4611/// ParseExtractValue
4612/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004613int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004614 Value *Val; LocTy Loc;
4615 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004616 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004617 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004618 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004619 return true;
4620
Chris Lattner392be582010-02-12 20:49:41 +00004621 if (!Val->getType()->isAggregateType())
4622 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004623
Jay Foad57aa6362011-07-13 10:26:04 +00004624 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004625 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004626 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004627 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004628}
4629
4630/// ParseInsertValue
4631/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004632int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004633 Value *Val0, *Val1; LocTy Loc0, Loc1;
4634 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004635 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004636 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
4637 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
4638 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004639 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004640 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004641
Chris Lattner392be582010-02-12 20:49:41 +00004642 if (!Val0->getType()->isAggregateType())
4643 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004644
Jay Foad57aa6362011-07-13 10:26:04 +00004645 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004646 return Error(Loc0, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004647 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004648 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004649}
Nick Lewycky49f89192009-04-04 07:22:01 +00004650
4651//===----------------------------------------------------------------------===//
4652// Embedded metadata.
4653//===----------------------------------------------------------------------===//
4654
4655/// ParseMDNodeVector
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004656/// ::= Element (',' Element)*
4657/// Element
4658/// ::= 'null' | TypeAndValue
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00004659bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandezb8fd1522010-01-10 07:14:18 +00004660 PerFunctionState *PFS) {
Dan Gohman1e0213a2010-07-13 19:33:27 +00004661 // Check for an empty list.
4662 if (Lex.getKind() == lltok::rbrace)
4663 return false;
4664
Nick Lewycky49f89192009-04-04 07:22:01 +00004665 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004666 // Null is a special case since it is typeless.
4667 if (EatIfPresent(lltok::kw_null)) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004668 Elts.push_back(nullptr);
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004669 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004670 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004671
Craig Topper2617dcc2014-04-15 06:32:26 +00004672 Value *V = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004673 if (ParseTypeAndValue(V, PFS)) return true;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004674 Elts.push_back(V);
Nick Lewycky49f89192009-04-04 07:22:01 +00004675 } while (EatIfPresent(lltok::comma));
4676
4677 return false;
4678}
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00004679
4680//===----------------------------------------------------------------------===//
4681// Use-list order directives.
4682//===----------------------------------------------------------------------===//
4683bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes,
4684 SMLoc Loc) {
4685 if (V->use_empty())
4686 return Error(Loc, "value has no uses");
4687
4688 unsigned NumUses = 0;
4689 SmallDenseMap<const Use *, unsigned, 16> Order;
4690 for (const Use &U : V->uses()) {
4691 if (++NumUses > Indexes.size())
4692 break;
4693 Order[&U] = Indexes[NumUses - 1];
4694 }
4695 if (NumUses < 2)
4696 return Error(Loc, "value only has one use");
4697 if (Order.size() != Indexes.size() || NumUses > Indexes.size())
4698 return Error(Loc, "wrong number of indexes, expected " +
4699 Twine(std::distance(V->use_begin(), V->use_end())));
4700
4701 V->sortUseList([&](const Use &L, const Use &R) {
4702 return Order.lookup(&L) < Order.lookup(&R);
4703 });
4704 return false;
4705}
4706
4707/// ParseUseListOrderIndexes
4708/// ::= '{' uint32 (',' uint32)+ '}'
4709bool LLParser::ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) {
4710 SMLoc Loc = Lex.getLoc();
4711 if (ParseToken(lltok::lbrace, "expected '{' here"))
4712 return true;
4713 if (Lex.getKind() == lltok::rbrace)
4714 return Lex.Error("expected non-empty list of uselistorder indexes");
4715
4716 // Use Offset, Max, and IsOrdered to check consistency of indexes. The
4717 // indexes should be distinct numbers in the range [0, size-1], and should
4718 // not be in order.
4719 unsigned Offset = 0;
4720 unsigned Max = 0;
4721 bool IsOrdered = true;
4722 assert(Indexes.empty() && "Expected empty order vector");
4723 do {
4724 unsigned Index;
4725 if (ParseUInt32(Index))
4726 return true;
4727
4728 // Update consistency checks.
4729 Offset += Index - Indexes.size();
4730 Max = std::max(Max, Index);
4731 IsOrdered &= Index == Indexes.size();
4732
4733 Indexes.push_back(Index);
4734 } while (EatIfPresent(lltok::comma));
4735
4736 if (ParseToken(lltok::rbrace, "expected '}' here"))
4737 return true;
4738
4739 if (Indexes.size() < 2)
4740 return Error(Loc, "expected >= 2 uselistorder indexes");
4741 if (Offset != 0 || Max >= Indexes.size())
4742 return Error(Loc, "expected distinct uselistorder indexes in range [0, size)");
4743 if (IsOrdered)
4744 return Error(Loc, "expected uselistorder indexes to change the order");
4745
4746 return false;
4747}
4748
4749/// ParseUseListOrder
4750/// ::= 'uselistorder' Type Value ',' UseListOrderIndexes
4751bool LLParser::ParseUseListOrder(PerFunctionState *PFS) {
4752 SMLoc Loc = Lex.getLoc();
4753 if (ParseToken(lltok::kw_uselistorder, "expected uselistorder directive"))
4754 return true;
4755
4756 Value *V;
4757 SmallVector<unsigned, 16> Indexes;
4758 if (ParseTypeAndValue(V, PFS) ||
4759 ParseToken(lltok::comma, "expected comma in uselistorder directive") ||
4760 ParseUseListOrderIndexes(Indexes))
4761 return true;
4762
4763 return sortUseListOrder(V, Indexes, Loc);
4764}
4765
4766/// ParseUseListOrderBB
4767/// ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes
4768bool LLParser::ParseUseListOrderBB() {
4769 assert(Lex.getKind() == lltok::kw_uselistorder_bb);
4770 SMLoc Loc = Lex.getLoc();
4771 Lex.Lex();
4772
4773 ValID Fn, Label;
4774 SmallVector<unsigned, 16> Indexes;
4775 if (ParseValID(Fn) ||
4776 ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
4777 ParseValID(Label) ||
4778 ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
4779 ParseUseListOrderIndexes(Indexes))
4780 return true;
4781
4782 // Check the function.
4783 GlobalValue *GV;
4784 if (Fn.Kind == ValID::t_GlobalName)
4785 GV = M->getNamedValue(Fn.StrVal);
4786 else if (Fn.Kind == ValID::t_GlobalID)
4787 GV = Fn.UIntVal < NumberedVals.size() ? NumberedVals[Fn.UIntVal] : nullptr;
4788 else
4789 return Error(Fn.Loc, "expected function name in uselistorder_bb");
4790 if (!GV)
4791 return Error(Fn.Loc, "invalid function forward reference in uselistorder_bb");
4792 auto *F = dyn_cast<Function>(GV);
4793 if (!F)
4794 return Error(Fn.Loc, "expected function name in uselistorder_bb");
4795 if (F->isDeclaration())
4796 return Error(Fn.Loc, "invalid declaration in uselistorder_bb");
4797
4798 // Check the basic block.
4799 if (Label.Kind == ValID::t_LocalID)
4800 return Error(Label.Loc, "invalid numeric label in uselistorder_bb");
4801 if (Label.Kind != ValID::t_LocalName)
4802 return Error(Label.Loc, "expected basic block name in uselistorder_bb");
4803 Value *V = F->getValueSymbolTable().lookup(Label.StrVal);
4804 if (!V)
4805 return Error(Label.Loc, "invalid basic block in uselistorder_bb");
4806 if (!isa<BasicBlock>(V))
4807 return Error(Label.Loc, "expected basic block in uselistorder_bb");
4808
4809 return sortUseListOrder(V, Indexes, Loc);
4810}