1 //===-- LLParser.cpp - Parser Class ---------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the parser class for .ll files.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/AsmParser/LLParser.h"
14 #include "llvm/ADT/APSInt.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/ScopeExit.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/AsmParser/LLToken.h"
20 #include "llvm/AsmParser/SlotMapping.h"
21 #include "llvm/BinaryFormat/Dwarf.h"
22 #include "llvm/IR/Argument.h"
23 #include "llvm/IR/AutoUpgrade.h"
24 #include "llvm/IR/BasicBlock.h"
25 #include "llvm/IR/CallingConv.h"
26 #include "llvm/IR/Comdat.h"
27 #include "llvm/IR/ConstantRange.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DebugInfoMetadata.h"
30 #include "llvm/IR/DerivedTypes.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/GlobalIFunc.h"
33 #include "llvm/IR/GlobalObject.h"
34 #include "llvm/IR/InlineAsm.h"
35 #include "llvm/IR/Instructions.h"
36 #include "llvm/IR/Intrinsics.h"
37 #include "llvm/IR/LLVMContext.h"
38 #include "llvm/IR/Metadata.h"
39 #include "llvm/IR/Module.h"
40 #include "llvm/IR/Operator.h"
41 #include "llvm/IR/Value.h"
42 #include "llvm/IR/ValueSymbolTable.h"
43 #include "llvm/Support/Casting.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/MathExtras.h"
46 #include "llvm/Support/ModRef.h"
47 #include "llvm/Support/SaveAndRestore.h"
48 #include "llvm/Support/raw_ostream.h"
49 #include <algorithm>
50 #include <cassert>
51 #include <cstring>
52 #include <optional>
53 #include <vector>
54
55 using namespace llvm;
56
getTypeString(Type * T)57 static std::string getTypeString(Type *T) {
58 std::string Result;
59 raw_string_ostream Tmp(Result);
60 Tmp << *T;
61 return Tmp.str();
62 }
63
64 /// Run: module ::= toplevelentity*
Run(bool UpgradeDebugInfo,DataLayoutCallbackTy DataLayoutCallback)65 bool LLParser::Run(bool UpgradeDebugInfo,
66 DataLayoutCallbackTy DataLayoutCallback) {
67 // Prime the lexer.
68 Lex.Lex();
69
70 if (Context.shouldDiscardValueNames())
71 return error(
72 Lex.getLoc(),
73 "Can't read textual IR with a Context that discards named Values");
74
75 if (M) {
76 if (parseTargetDefinitions(DataLayoutCallback))
77 return true;
78 }
79
80 return parseTopLevelEntities() || validateEndOfModule(UpgradeDebugInfo) ||
81 validateEndOfIndex();
82 }
83
parseStandaloneConstantValue(Constant * & C,const SlotMapping * Slots)84 bool LLParser::parseStandaloneConstantValue(Constant *&C,
85 const SlotMapping *Slots) {
86 restoreParsingState(Slots);
87 Lex.Lex();
88
89 Type *Ty = nullptr;
90 if (parseType(Ty) || parseConstantValue(Ty, C))
91 return true;
92 if (Lex.getKind() != lltok::Eof)
93 return error(Lex.getLoc(), "expected end of string");
94 return false;
95 }
96
parseTypeAtBeginning(Type * & Ty,unsigned & Read,const SlotMapping * Slots)97 bool LLParser::parseTypeAtBeginning(Type *&Ty, unsigned &Read,
98 const SlotMapping *Slots) {
99 restoreParsingState(Slots);
100 Lex.Lex();
101
102 Read = 0;
103 SMLoc Start = Lex.getLoc();
104 Ty = nullptr;
105 if (parseType(Ty))
106 return true;
107 SMLoc End = Lex.getLoc();
108 Read = End.getPointer() - Start.getPointer();
109
110 return false;
111 }
112
restoreParsingState(const SlotMapping * Slots)113 void LLParser::restoreParsingState(const SlotMapping *Slots) {
114 if (!Slots)
115 return;
116 NumberedVals = Slots->GlobalValues;
117 NumberedMetadata = Slots->MetadataNodes;
118 for (const auto &I : Slots->NamedTypes)
119 NamedTypes.insert(
120 std::make_pair(I.getKey(), std::make_pair(I.second, LocTy())));
121 for (const auto &I : Slots->Types)
122 NumberedTypes.insert(
123 std::make_pair(I.first, std::make_pair(I.second, LocTy())));
124 }
125
126 /// validateEndOfModule - Do final validity and basic correctness checks at the
127 /// end of the module.
validateEndOfModule(bool UpgradeDebugInfo)128 bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) {
129 if (!M)
130 return false;
131 // Handle any function attribute group forward references.
132 for (const auto &RAG : ForwardRefAttrGroups) {
133 Value *V = RAG.first;
134 const std::vector<unsigned> &Attrs = RAG.second;
135 AttrBuilder B(Context);
136
137 for (const auto &Attr : Attrs) {
138 auto R = NumberedAttrBuilders.find(Attr);
139 if (R != NumberedAttrBuilders.end())
140 B.merge(R->second);
141 }
142
143 if (Function *Fn = dyn_cast<Function>(V)) {
144 AttributeList AS = Fn->getAttributes();
145 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
146 AS = AS.removeFnAttributes(Context);
147
148 FnAttrs.merge(B);
149
150 // If the alignment was parsed as an attribute, move to the alignment
151 // field.
152 if (MaybeAlign A = FnAttrs.getAlignment()) {
153 Fn->setAlignment(A);
154 FnAttrs.removeAttribute(Attribute::Alignment);
155 }
156
157 AS = AS.addFnAttributes(Context, FnAttrs);
158 Fn->setAttributes(AS);
159 } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
160 AttributeList AS = CI->getAttributes();
161 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
162 AS = AS.removeFnAttributes(Context);
163 FnAttrs.merge(B);
164 AS = AS.addFnAttributes(Context, FnAttrs);
165 CI->setAttributes(AS);
166 } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
167 AttributeList AS = II->getAttributes();
168 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
169 AS = AS.removeFnAttributes(Context);
170 FnAttrs.merge(B);
171 AS = AS.addFnAttributes(Context, FnAttrs);
172 II->setAttributes(AS);
173 } else if (CallBrInst *CBI = dyn_cast<CallBrInst>(V)) {
174 AttributeList AS = CBI->getAttributes();
175 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
176 AS = AS.removeFnAttributes(Context);
177 FnAttrs.merge(B);
178 AS = AS.addFnAttributes(Context, FnAttrs);
179 CBI->setAttributes(AS);
180 } else if (auto *GV = dyn_cast<GlobalVariable>(V)) {
181 AttrBuilder Attrs(M->getContext(), GV->getAttributes());
182 Attrs.merge(B);
183 GV->setAttributes(AttributeSet::get(Context,Attrs));
184 } else {
185 llvm_unreachable("invalid object with forward attribute group reference");
186 }
187 }
188
189 // If there are entries in ForwardRefBlockAddresses at this point, the
190 // function was never defined.
191 if (!ForwardRefBlockAddresses.empty())
192 return error(ForwardRefBlockAddresses.begin()->first.Loc,
193 "expected function name in blockaddress");
194
195 auto ResolveForwardRefDSOLocalEquivalents = [&](const ValID &GVRef,
196 GlobalValue *FwdRef) {
197 GlobalValue *GV = nullptr;
198 if (GVRef.Kind == ValID::t_GlobalName) {
199 GV = M->getNamedValue(GVRef.StrVal);
200 } else if (GVRef.UIntVal < NumberedVals.size()) {
201 GV = dyn_cast<GlobalValue>(NumberedVals[GVRef.UIntVal]);
202 }
203
204 if (!GV)
205 return error(GVRef.Loc, "unknown function '" + GVRef.StrVal +
206 "' referenced by dso_local_equivalent");
207
208 if (!GV->getValueType()->isFunctionTy())
209 return error(GVRef.Loc,
210 "expected a function, alias to function, or ifunc "
211 "in dso_local_equivalent");
212
213 auto *Equiv = DSOLocalEquivalent::get(GV);
214 FwdRef->replaceAllUsesWith(Equiv);
215 FwdRef->eraseFromParent();
216 return false;
217 };
218
219 // If there are entries in ForwardRefDSOLocalEquivalentIDs/Names at this
220 // point, they are references after the function was defined. Resolve those
221 // now.
222 for (auto &Iter : ForwardRefDSOLocalEquivalentIDs) {
223 if (ResolveForwardRefDSOLocalEquivalents(Iter.first, Iter.second))
224 return true;
225 }
226 for (auto &Iter : ForwardRefDSOLocalEquivalentNames) {
227 if (ResolveForwardRefDSOLocalEquivalents(Iter.first, Iter.second))
228 return true;
229 }
230 ForwardRefDSOLocalEquivalentIDs.clear();
231 ForwardRefDSOLocalEquivalentNames.clear();
232
233 for (const auto &NT : NumberedTypes)
234 if (NT.second.second.isValid())
235 return error(NT.second.second,
236 "use of undefined type '%" + Twine(NT.first) + "'");
237
238 for (StringMap<std::pair<Type*, LocTy> >::iterator I =
239 NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I)
240 if (I->second.second.isValid())
241 return error(I->second.second,
242 "use of undefined type named '" + I->getKey() + "'");
243
244 if (!ForwardRefComdats.empty())
245 return error(ForwardRefComdats.begin()->second,
246 "use of undefined comdat '$" +
247 ForwardRefComdats.begin()->first + "'");
248
249 if (!ForwardRefVals.empty())
250 return error(ForwardRefVals.begin()->second.second,
251 "use of undefined value '@" + ForwardRefVals.begin()->first +
252 "'");
253
254 if (!ForwardRefValIDs.empty())
255 return error(ForwardRefValIDs.begin()->second.second,
256 "use of undefined value '@" +
257 Twine(ForwardRefValIDs.begin()->first) + "'");
258
259 if (!ForwardRefMDNodes.empty())
260 return error(ForwardRefMDNodes.begin()->second.second,
261 "use of undefined metadata '!" +
262 Twine(ForwardRefMDNodes.begin()->first) + "'");
263
264 // Resolve metadata cycles.
265 for (auto &N : NumberedMetadata) {
266 if (N.second && !N.second->isResolved())
267 N.second->resolveCycles();
268 }
269
270 for (auto *Inst : InstsWithTBAATag) {
271 MDNode *MD = Inst->getMetadata(LLVMContext::MD_tbaa);
272 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
273 auto *UpgradedMD = UpgradeTBAANode(*MD);
274 if (MD != UpgradedMD)
275 Inst->setMetadata(LLVMContext::MD_tbaa, UpgradedMD);
276 }
277
278 // Look for intrinsic functions and CallInst that need to be upgraded. We use
279 // make_early_inc_range here because we may remove some functions.
280 for (Function &F : llvm::make_early_inc_range(*M))
281 UpgradeCallsToIntrinsic(&F);
282
283 if (UpgradeDebugInfo)
284 llvm::UpgradeDebugInfo(*M);
285
286 UpgradeModuleFlags(*M);
287 UpgradeSectionAttributes(*M);
288
289 if (!Slots)
290 return false;
291 // Initialize the slot mapping.
292 // Because by this point we've parsed and validated everything, we can "steal"
293 // the mapping from LLParser as it doesn't need it anymore.
294 Slots->GlobalValues = std::move(NumberedVals);
295 Slots->MetadataNodes = std::move(NumberedMetadata);
296 for (const auto &I : NamedTypes)
297 Slots->NamedTypes.insert(std::make_pair(I.getKey(), I.second.first));
298 for (const auto &I : NumberedTypes)
299 Slots->Types.insert(std::make_pair(I.first, I.second.first));
300
301 return false;
302 }
303
304 /// Do final validity and basic correctness checks at the end of the index.
validateEndOfIndex()305 bool LLParser::validateEndOfIndex() {
306 if (!Index)
307 return false;
308
309 if (!ForwardRefValueInfos.empty())
310 return error(ForwardRefValueInfos.begin()->second.front().second,
311 "use of undefined summary '^" +
312 Twine(ForwardRefValueInfos.begin()->first) + "'");
313
314 if (!ForwardRefAliasees.empty())
315 return error(ForwardRefAliasees.begin()->second.front().second,
316 "use of undefined summary '^" +
317 Twine(ForwardRefAliasees.begin()->first) + "'");
318
319 if (!ForwardRefTypeIds.empty())
320 return error(ForwardRefTypeIds.begin()->second.front().second,
321 "use of undefined type id summary '^" +
322 Twine(ForwardRefTypeIds.begin()->first) + "'");
323
324 return false;
325 }
326
327 //===----------------------------------------------------------------------===//
328 // Top-Level Entities
329 //===----------------------------------------------------------------------===//
330
parseTargetDefinitions(DataLayoutCallbackTy DataLayoutCallback)331 bool LLParser::parseTargetDefinitions(DataLayoutCallbackTy DataLayoutCallback) {
332 // Delay parsing of the data layout string until the target triple is known.
333 // Then, pass both the the target triple and the tentative data layout string
334 // to DataLayoutCallback, allowing to override the DL string.
335 // This enables importing modules with invalid DL strings.
336 std::string TentativeDLStr = M->getDataLayoutStr();
337 LocTy DLStrLoc;
338
339 bool Done = false;
340 while (!Done) {
341 switch (Lex.getKind()) {
342 case lltok::kw_target:
343 if (parseTargetDefinition(TentativeDLStr, DLStrLoc))
344 return true;
345 break;
346 case lltok::kw_source_filename:
347 if (parseSourceFileName())
348 return true;
349 break;
350 default:
351 Done = true;
352 }
353 }
354 // Run the override callback to potentially change the data layout string, and
355 // parse the data layout string.
356 if (auto LayoutOverride =
357 DataLayoutCallback(M->getTargetTriple(), TentativeDLStr)) {
358 TentativeDLStr = *LayoutOverride;
359 DLStrLoc = {};
360 }
361 Expected<DataLayout> MaybeDL = DataLayout::parse(TentativeDLStr);
362 if (!MaybeDL)
363 return error(DLStrLoc, toString(MaybeDL.takeError()));
364 M->setDataLayout(MaybeDL.get());
365 return false;
366 }
367
parseTopLevelEntities()368 bool LLParser::parseTopLevelEntities() {
369 // If there is no Module, then parse just the summary index entries.
370 if (!M) {
371 while (true) {
372 switch (Lex.getKind()) {
373 case lltok::Eof:
374 return false;
375 case lltok::SummaryID:
376 if (parseSummaryEntry())
377 return true;
378 break;
379 case lltok::kw_source_filename:
380 if (parseSourceFileName())
381 return true;
382 break;
383 default:
384 // Skip everything else
385 Lex.Lex();
386 }
387 }
388 }
389 while (true) {
390 switch (Lex.getKind()) {
391 default:
392 return tokError("expected top-level entity");
393 case lltok::Eof: return false;
394 case lltok::kw_declare:
395 if (parseDeclare())
396 return true;
397 break;
398 case lltok::kw_define:
399 if (parseDefine())
400 return true;
401 break;
402 case lltok::kw_module:
403 if (parseModuleAsm())
404 return true;
405 break;
406 case lltok::LocalVarID:
407 if (parseUnnamedType())
408 return true;
409 break;
410 case lltok::LocalVar:
411 if (parseNamedType())
412 return true;
413 break;
414 case lltok::GlobalID:
415 if (parseUnnamedGlobal())
416 return true;
417 break;
418 case lltok::GlobalVar:
419 if (parseNamedGlobal())
420 return true;
421 break;
422 case lltok::ComdatVar: if (parseComdat()) return true; break;
423 case lltok::exclaim:
424 if (parseStandaloneMetadata())
425 return true;
426 break;
427 case lltok::SummaryID:
428 if (parseSummaryEntry())
429 return true;
430 break;
431 case lltok::MetadataVar:
432 if (parseNamedMetadata())
433 return true;
434 break;
435 case lltok::kw_attributes:
436 if (parseUnnamedAttrGrp())
437 return true;
438 break;
439 case lltok::kw_uselistorder:
440 if (parseUseListOrder())
441 return true;
442 break;
443 case lltok::kw_uselistorder_bb:
444 if (parseUseListOrderBB())
445 return true;
446 break;
447 }
448 }
449 }
450
451 /// toplevelentity
452 /// ::= 'module' 'asm' STRINGCONSTANT
parseModuleAsm()453 bool LLParser::parseModuleAsm() {
454 assert(Lex.getKind() == lltok::kw_module);
455 Lex.Lex();
456
457 std::string AsmStr;
458 if (parseToken(lltok::kw_asm, "expected 'module asm'") ||
459 parseStringConstant(AsmStr))
460 return true;
461
462 M->appendModuleInlineAsm(AsmStr);
463 return false;
464 }
465
466 /// toplevelentity
467 /// ::= 'target' 'triple' '=' STRINGCONSTANT
468 /// ::= 'target' 'datalayout' '=' STRINGCONSTANT
parseTargetDefinition(std::string & TentativeDLStr,LocTy & DLStrLoc)469 bool LLParser::parseTargetDefinition(std::string &TentativeDLStr,
470 LocTy &DLStrLoc) {
471 assert(Lex.getKind() == lltok::kw_target);
472 std::string Str;
473 switch (Lex.Lex()) {
474 default:
475 return tokError("unknown target property");
476 case lltok::kw_triple:
477 Lex.Lex();
478 if (parseToken(lltok::equal, "expected '=' after target triple") ||
479 parseStringConstant(Str))
480 return true;
481 M->setTargetTriple(Str);
482 return false;
483 case lltok::kw_datalayout:
484 Lex.Lex();
485 if (parseToken(lltok::equal, "expected '=' after target datalayout"))
486 return true;
487 DLStrLoc = Lex.getLoc();
488 if (parseStringConstant(TentativeDLStr))
489 return true;
490 return false;
491 }
492 }
493
494 /// toplevelentity
495 /// ::= 'source_filename' '=' STRINGCONSTANT
parseSourceFileName()496 bool LLParser::parseSourceFileName() {
497 assert(Lex.getKind() == lltok::kw_source_filename);
498 Lex.Lex();
499 if (parseToken(lltok::equal, "expected '=' after source_filename") ||
500 parseStringConstant(SourceFileName))
501 return true;
502 if (M)
503 M->setSourceFileName(SourceFileName);
504 return false;
505 }
506
507 /// parseUnnamedType:
508 /// ::= LocalVarID '=' 'type' type
parseUnnamedType()509 bool LLParser::parseUnnamedType() {
510 LocTy TypeLoc = Lex.getLoc();
511 unsigned TypeID = Lex.getUIntVal();
512 Lex.Lex(); // eat LocalVarID;
513
514 if (parseToken(lltok::equal, "expected '=' after name") ||
515 parseToken(lltok::kw_type, "expected 'type' after '='"))
516 return true;
517
518 Type *Result = nullptr;
519 if (parseStructDefinition(TypeLoc, "", NumberedTypes[TypeID], Result))
520 return true;
521
522 if (!isa<StructType>(Result)) {
523 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
524 if (Entry.first)
525 return error(TypeLoc, "non-struct types may not be recursive");
526 Entry.first = Result;
527 Entry.second = SMLoc();
528 }
529
530 return false;
531 }
532
533 /// toplevelentity
534 /// ::= LocalVar '=' 'type' type
parseNamedType()535 bool LLParser::parseNamedType() {
536 std::string Name = Lex.getStrVal();
537 LocTy NameLoc = Lex.getLoc();
538 Lex.Lex(); // eat LocalVar.
539
540 if (parseToken(lltok::equal, "expected '=' after name") ||
541 parseToken(lltok::kw_type, "expected 'type' after name"))
542 return true;
543
544 Type *Result = nullptr;
545 if (parseStructDefinition(NameLoc, Name, NamedTypes[Name], Result))
546 return true;
547
548 if (!isa<StructType>(Result)) {
549 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
550 if (Entry.first)
551 return error(NameLoc, "non-struct types may not be recursive");
552 Entry.first = Result;
553 Entry.second = SMLoc();
554 }
555
556 return false;
557 }
558
559 /// toplevelentity
560 /// ::= 'declare' FunctionHeader
parseDeclare()561 bool LLParser::parseDeclare() {
562 assert(Lex.getKind() == lltok::kw_declare);
563 Lex.Lex();
564
565 std::vector<std::pair<unsigned, MDNode *>> MDs;
566 while (Lex.getKind() == lltok::MetadataVar) {
567 unsigned MDK;
568 MDNode *N;
569 if (parseMetadataAttachment(MDK, N))
570 return true;
571 MDs.push_back({MDK, N});
572 }
573
574 Function *F;
575 if (parseFunctionHeader(F, false))
576 return true;
577 for (auto &MD : MDs)
578 F->addMetadata(MD.first, *MD.second);
579 return false;
580 }
581
582 /// toplevelentity
583 /// ::= 'define' FunctionHeader (!dbg !56)* '{' ...
parseDefine()584 bool LLParser::parseDefine() {
585 assert(Lex.getKind() == lltok::kw_define);
586 Lex.Lex();
587
588 Function *F;
589 return parseFunctionHeader(F, true) || parseOptionalFunctionMetadata(*F) ||
590 parseFunctionBody(*F);
591 }
592
593 /// parseGlobalType
594 /// ::= 'constant'
595 /// ::= 'global'
parseGlobalType(bool & IsConstant)596 bool LLParser::parseGlobalType(bool &IsConstant) {
597 if (Lex.getKind() == lltok::kw_constant)
598 IsConstant = true;
599 else if (Lex.getKind() == lltok::kw_global)
600 IsConstant = false;
601 else {
602 IsConstant = false;
603 return tokError("expected 'global' or 'constant'");
604 }
605 Lex.Lex();
606 return false;
607 }
608
parseOptionalUnnamedAddr(GlobalVariable::UnnamedAddr & UnnamedAddr)609 bool LLParser::parseOptionalUnnamedAddr(
610 GlobalVariable::UnnamedAddr &UnnamedAddr) {
611 if (EatIfPresent(lltok::kw_unnamed_addr))
612 UnnamedAddr = GlobalValue::UnnamedAddr::Global;
613 else if (EatIfPresent(lltok::kw_local_unnamed_addr))
614 UnnamedAddr = GlobalValue::UnnamedAddr::Local;
615 else
616 UnnamedAddr = GlobalValue::UnnamedAddr::None;
617 return false;
618 }
619
620 /// parseUnnamedGlobal:
621 /// OptionalVisibility (ALIAS | IFUNC) ...
622 /// OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
623 /// OptionalDLLStorageClass
624 /// ... -> global variable
625 /// GlobalID '=' OptionalVisibility (ALIAS | IFUNC) ...
626 /// GlobalID '=' OptionalLinkage OptionalPreemptionSpecifier
627 /// OptionalVisibility
628 /// OptionalDLLStorageClass
629 /// ... -> global variable
parseUnnamedGlobal()630 bool LLParser::parseUnnamedGlobal() {
631 unsigned VarID = NumberedVals.size();
632 std::string Name;
633 LocTy NameLoc = Lex.getLoc();
634
635 // Handle the GlobalID form.
636 if (Lex.getKind() == lltok::GlobalID) {
637 if (Lex.getUIntVal() != VarID)
638 return error(Lex.getLoc(),
639 "variable expected to be numbered '%" + Twine(VarID) + "'");
640 Lex.Lex(); // eat GlobalID;
641
642 if (parseToken(lltok::equal, "expected '=' after name"))
643 return true;
644 }
645
646 bool HasLinkage;
647 unsigned Linkage, Visibility, DLLStorageClass;
648 bool DSOLocal;
649 GlobalVariable::ThreadLocalMode TLM;
650 GlobalVariable::UnnamedAddr UnnamedAddr;
651 if (parseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
652 DSOLocal) ||
653 parseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr))
654 return true;
655
656 switch (Lex.getKind()) {
657 default:
658 return parseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
659 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
660 case lltok::kw_alias:
661 case lltok::kw_ifunc:
662 return parseAliasOrIFunc(Name, NameLoc, Linkage, Visibility,
663 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
664 }
665 }
666
667 /// parseNamedGlobal:
668 /// GlobalVar '=' OptionalVisibility (ALIAS | IFUNC) ...
669 /// GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
670 /// OptionalVisibility OptionalDLLStorageClass
671 /// ... -> global variable
parseNamedGlobal()672 bool LLParser::parseNamedGlobal() {
673 assert(Lex.getKind() == lltok::GlobalVar);
674 LocTy NameLoc = Lex.getLoc();
675 std::string Name = Lex.getStrVal();
676 Lex.Lex();
677
678 bool HasLinkage;
679 unsigned Linkage, Visibility, DLLStorageClass;
680 bool DSOLocal;
681 GlobalVariable::ThreadLocalMode TLM;
682 GlobalVariable::UnnamedAddr UnnamedAddr;
683 if (parseToken(lltok::equal, "expected '=' in global variable") ||
684 parseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
685 DSOLocal) ||
686 parseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr))
687 return true;
688
689 switch (Lex.getKind()) {
690 default:
691 return parseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
692 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
693 case lltok::kw_alias:
694 case lltok::kw_ifunc:
695 return parseAliasOrIFunc(Name, NameLoc, Linkage, Visibility,
696 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
697 }
698 }
699
parseComdat()700 bool LLParser::parseComdat() {
701 assert(Lex.getKind() == lltok::ComdatVar);
702 std::string Name = Lex.getStrVal();
703 LocTy NameLoc = Lex.getLoc();
704 Lex.Lex();
705
706 if (parseToken(lltok::equal, "expected '=' here"))
707 return true;
708
709 if (parseToken(lltok::kw_comdat, "expected comdat keyword"))
710 return tokError("expected comdat type");
711
712 Comdat::SelectionKind SK;
713 switch (Lex.getKind()) {
714 default:
715 return tokError("unknown selection kind");
716 case lltok::kw_any:
717 SK = Comdat::Any;
718 break;
719 case lltok::kw_exactmatch:
720 SK = Comdat::ExactMatch;
721 break;
722 case lltok::kw_largest:
723 SK = Comdat::Largest;
724 break;
725 case lltok::kw_nodeduplicate:
726 SK = Comdat::NoDeduplicate;
727 break;
728 case lltok::kw_samesize:
729 SK = Comdat::SameSize;
730 break;
731 }
732 Lex.Lex();
733
734 // See if the comdat was forward referenced, if so, use the comdat.
735 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
736 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
737 if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(Name))
738 return error(NameLoc, "redefinition of comdat '$" + Name + "'");
739
740 Comdat *C;
741 if (I != ComdatSymTab.end())
742 C = &I->second;
743 else
744 C = M->getOrInsertComdat(Name);
745 C->setSelectionKind(SK);
746
747 return false;
748 }
749
750 // MDString:
751 // ::= '!' STRINGCONSTANT
parseMDString(MDString * & Result)752 bool LLParser::parseMDString(MDString *&Result) {
753 std::string Str;
754 if (parseStringConstant(Str))
755 return true;
756 Result = MDString::get(Context, Str);
757 return false;
758 }
759
760 // MDNode:
761 // ::= '!' MDNodeNumber
parseMDNodeID(MDNode * & Result)762 bool LLParser::parseMDNodeID(MDNode *&Result) {
763 // !{ ..., !42, ... }
764 LocTy IDLoc = Lex.getLoc();
765 unsigned MID = 0;
766 if (parseUInt32(MID))
767 return true;
768
769 // If not a forward reference, just return it now.
770 if (NumberedMetadata.count(MID)) {
771 Result = NumberedMetadata[MID];
772 return false;
773 }
774
775 // Otherwise, create MDNode forward reference.
776 auto &FwdRef = ForwardRefMDNodes[MID];
777 FwdRef = std::make_pair(MDTuple::getTemporary(Context, std::nullopt), IDLoc);
778
779 Result = FwdRef.first.get();
780 NumberedMetadata[MID].reset(Result);
781 return false;
782 }
783
784 /// parseNamedMetadata:
785 /// !foo = !{ !1, !2 }
parseNamedMetadata()786 bool LLParser::parseNamedMetadata() {
787 assert(Lex.getKind() == lltok::MetadataVar);
788 std::string Name = Lex.getStrVal();
789 Lex.Lex();
790
791 if (parseToken(lltok::equal, "expected '=' here") ||
792 parseToken(lltok::exclaim, "Expected '!' here") ||
793 parseToken(lltok::lbrace, "Expected '{' here"))
794 return true;
795
796 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
797 if (Lex.getKind() != lltok::rbrace)
798 do {
799 MDNode *N = nullptr;
800 // parse DIExpressions inline as a special case. They are still MDNodes,
801 // so they can still appear in named metadata. Remove this logic if they
802 // become plain Metadata.
803 if (Lex.getKind() == lltok::MetadataVar &&
804 Lex.getStrVal() == "DIExpression") {
805 if (parseDIExpression(N, /*IsDistinct=*/false))
806 return true;
807 // DIArgLists should only appear inline in a function, as they may
808 // contain LocalAsMetadata arguments which require a function context.
809 } else if (Lex.getKind() == lltok::MetadataVar &&
810 Lex.getStrVal() == "DIArgList") {
811 return tokError("found DIArgList outside of function");
812 } else if (parseToken(lltok::exclaim, "Expected '!' here") ||
813 parseMDNodeID(N)) {
814 return true;
815 }
816 NMD->addOperand(N);
817 } while (EatIfPresent(lltok::comma));
818
819 return parseToken(lltok::rbrace, "expected end of metadata node");
820 }
821
822 /// parseStandaloneMetadata:
823 /// !42 = !{...}
parseStandaloneMetadata()824 bool LLParser::parseStandaloneMetadata() {
825 assert(Lex.getKind() == lltok::exclaim);
826 Lex.Lex();
827 unsigned MetadataID = 0;
828
829 MDNode *Init;
830 if (parseUInt32(MetadataID) || parseToken(lltok::equal, "expected '=' here"))
831 return true;
832
833 // Detect common error, from old metadata syntax.
834 if (Lex.getKind() == lltok::Type)
835 return tokError("unexpected type in metadata definition");
836
837 bool IsDistinct = EatIfPresent(lltok::kw_distinct);
838 if (Lex.getKind() == lltok::MetadataVar) {
839 if (parseSpecializedMDNode(Init, IsDistinct))
840 return true;
841 } else if (parseToken(lltok::exclaim, "Expected '!' here") ||
842 parseMDTuple(Init, IsDistinct))
843 return true;
844
845 // See if this was forward referenced, if so, handle it.
846 auto FI = ForwardRefMDNodes.find(MetadataID);
847 if (FI != ForwardRefMDNodes.end()) {
848 auto *ToReplace = FI->second.first.get();
849 // DIAssignID has its own special forward-reference "replacement" for
850 // attachments (the temporary attachments are never actually attached).
851 if (isa<DIAssignID>(Init)) {
852 for (auto *Inst : TempDIAssignIDAttachments[ToReplace]) {
853 assert(!Inst->getMetadata(LLVMContext::MD_DIAssignID) &&
854 "Inst unexpectedly already has DIAssignID attachment");
855 Inst->setMetadata(LLVMContext::MD_DIAssignID, Init);
856 }
857 }
858
859 ToReplace->replaceAllUsesWith(Init);
860 ForwardRefMDNodes.erase(FI);
861
862 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
863 } else {
864 if (NumberedMetadata.count(MetadataID))
865 return tokError("Metadata id is already used");
866 NumberedMetadata[MetadataID].reset(Init);
867 }
868
869 return false;
870 }
871
872 // Skips a single module summary entry.
skipModuleSummaryEntry()873 bool LLParser::skipModuleSummaryEntry() {
874 // Each module summary entry consists of a tag for the entry
875 // type, followed by a colon, then the fields which may be surrounded by
876 // nested sets of parentheses. The "tag:" looks like a Label. Once parsing
877 // support is in place we will look for the tokens corresponding to the
878 // expected tags.
879 if (Lex.getKind() != lltok::kw_gv && Lex.getKind() != lltok::kw_module &&
880 Lex.getKind() != lltok::kw_typeid && Lex.getKind() != lltok::kw_flags &&
881 Lex.getKind() != lltok::kw_blockcount)
882 return tokError(
883 "Expected 'gv', 'module', 'typeid', 'flags' or 'blockcount' at the "
884 "start of summary entry");
885 if (Lex.getKind() == lltok::kw_flags)
886 return parseSummaryIndexFlags();
887 if (Lex.getKind() == lltok::kw_blockcount)
888 return parseBlockCount();
889 Lex.Lex();
890 if (parseToken(lltok::colon, "expected ':' at start of summary entry") ||
891 parseToken(lltok::lparen, "expected '(' at start of summary entry"))
892 return true;
893 // Now walk through the parenthesized entry, until the number of open
894 // parentheses goes back down to 0 (the first '(' was parsed above).
895 unsigned NumOpenParen = 1;
896 do {
897 switch (Lex.getKind()) {
898 case lltok::lparen:
899 NumOpenParen++;
900 break;
901 case lltok::rparen:
902 NumOpenParen--;
903 break;
904 case lltok::Eof:
905 return tokError("found end of file while parsing summary entry");
906 default:
907 // Skip everything in between parentheses.
908 break;
909 }
910 Lex.Lex();
911 } while (NumOpenParen > 0);
912 return false;
913 }
914
915 /// SummaryEntry
916 /// ::= SummaryID '=' GVEntry | ModuleEntry | TypeIdEntry
parseSummaryEntry()917 bool LLParser::parseSummaryEntry() {
918 assert(Lex.getKind() == lltok::SummaryID);
919 unsigned SummaryID = Lex.getUIntVal();
920
921 // For summary entries, colons should be treated as distinct tokens,
922 // not an indication of the end of a label token.
923 Lex.setIgnoreColonInIdentifiers(true);
924
925 Lex.Lex();
926 if (parseToken(lltok::equal, "expected '=' here"))
927 return true;
928
929 // If we don't have an index object, skip the summary entry.
930 if (!Index)
931 return skipModuleSummaryEntry();
932
933 bool result = false;
934 switch (Lex.getKind()) {
935 case lltok::kw_gv:
936 result = parseGVEntry(SummaryID);
937 break;
938 case lltok::kw_module:
939 result = parseModuleEntry(SummaryID);
940 break;
941 case lltok::kw_typeid:
942 result = parseTypeIdEntry(SummaryID);
943 break;
944 case lltok::kw_typeidCompatibleVTable:
945 result = parseTypeIdCompatibleVtableEntry(SummaryID);
946 break;
947 case lltok::kw_flags:
948 result = parseSummaryIndexFlags();
949 break;
950 case lltok::kw_blockcount:
951 result = parseBlockCount();
952 break;
953 default:
954 result = error(Lex.getLoc(), "unexpected summary kind");
955 break;
956 }
957 Lex.setIgnoreColonInIdentifiers(false);
958 return result;
959 }
960
isValidVisibilityForLinkage(unsigned V,unsigned L)961 static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
962 return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
963 (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
964 }
isValidDLLStorageClassForLinkage(unsigned S,unsigned L)965 static bool isValidDLLStorageClassForLinkage(unsigned S, unsigned L) {
966 return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
967 (GlobalValue::DLLStorageClassTypes)S == GlobalValue::DefaultStorageClass;
968 }
969
970 // If there was an explicit dso_local, update GV. In the absence of an explicit
971 // dso_local we keep the default value.
maybeSetDSOLocal(bool DSOLocal,GlobalValue & GV)972 static void maybeSetDSOLocal(bool DSOLocal, GlobalValue &GV) {
973 if (DSOLocal)
974 GV.setDSOLocal(true);
975 }
976
typeComparisonErrorMessage(StringRef Message,Type * Ty1,Type * Ty2)977 static std::string typeComparisonErrorMessage(StringRef Message, Type *Ty1,
978 Type *Ty2) {
979 std::string ErrString;
980 raw_string_ostream ErrOS(ErrString);
981 ErrOS << Message << " (" << *Ty1 << " vs " << *Ty2 << ")";
982 return ErrOS.str();
983 }
984
985 /// parseAliasOrIFunc:
986 /// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
987 /// OptionalVisibility OptionalDLLStorageClass
988 /// OptionalThreadLocal OptionalUnnamedAddr
989 /// 'alias|ifunc' AliaseeOrResolver SymbolAttrs*
990 ///
991 /// AliaseeOrResolver
992 /// ::= TypeAndValue
993 ///
994 /// SymbolAttrs
995 /// ::= ',' 'partition' StringConstant
996 ///
997 /// Everything through OptionalUnnamedAddr has already been parsed.
998 ///
parseAliasOrIFunc(const std::string & Name,LocTy NameLoc,unsigned L,unsigned Visibility,unsigned DLLStorageClass,bool DSOLocal,GlobalVariable::ThreadLocalMode TLM,GlobalVariable::UnnamedAddr UnnamedAddr)999 bool LLParser::parseAliasOrIFunc(const std::string &Name, LocTy NameLoc,
1000 unsigned L, unsigned Visibility,
1001 unsigned DLLStorageClass, bool DSOLocal,
1002 GlobalVariable::ThreadLocalMode TLM,
1003 GlobalVariable::UnnamedAddr UnnamedAddr) {
1004 bool IsAlias;
1005 if (Lex.getKind() == lltok::kw_alias)
1006 IsAlias = true;
1007 else if (Lex.getKind() == lltok::kw_ifunc)
1008 IsAlias = false;
1009 else
1010 llvm_unreachable("Not an alias or ifunc!");
1011 Lex.Lex();
1012
1013 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
1014
1015 if(IsAlias && !GlobalAlias::isValidLinkage(Linkage))
1016 return error(NameLoc, "invalid linkage type for alias");
1017
1018 if (!isValidVisibilityForLinkage(Visibility, L))
1019 return error(NameLoc,
1020 "symbol with local linkage must have default visibility");
1021
1022 if (!isValidDLLStorageClassForLinkage(DLLStorageClass, L))
1023 return error(NameLoc,
1024 "symbol with local linkage cannot have a DLL storage class");
1025
1026 Type *Ty;
1027 LocTy ExplicitTypeLoc = Lex.getLoc();
1028 if (parseType(Ty) ||
1029 parseToken(lltok::comma, "expected comma after alias or ifunc's type"))
1030 return true;
1031
1032 Constant *Aliasee;
1033 LocTy AliaseeLoc = Lex.getLoc();
1034 if (Lex.getKind() != lltok::kw_bitcast &&
1035 Lex.getKind() != lltok::kw_getelementptr &&
1036 Lex.getKind() != lltok::kw_addrspacecast &&
1037 Lex.getKind() != lltok::kw_inttoptr) {
1038 if (parseGlobalTypeAndValue(Aliasee))
1039 return true;
1040 } else {
1041 // The bitcast dest type is not present, it is implied by the dest type.
1042 ValID ID;
1043 if (parseValID(ID, /*PFS=*/nullptr))
1044 return true;
1045 if (ID.Kind != ValID::t_Constant)
1046 return error(AliaseeLoc, "invalid aliasee");
1047 Aliasee = ID.ConstantVal;
1048 }
1049
1050 Type *AliaseeType = Aliasee->getType();
1051 auto *PTy = dyn_cast<PointerType>(AliaseeType);
1052 if (!PTy)
1053 return error(AliaseeLoc, "An alias or ifunc must have pointer type");
1054 unsigned AddrSpace = PTy->getAddressSpace();
1055
1056 if (IsAlias) {
1057 if (!PTy->isOpaqueOrPointeeTypeMatches(Ty))
1058 return error(
1059 ExplicitTypeLoc,
1060 typeComparisonErrorMessage(
1061 "explicit pointee type doesn't match operand's pointee type", Ty,
1062 PTy->getNonOpaquePointerElementType()));
1063 } else {
1064 if (!PTy->isOpaque() &&
1065 !PTy->getNonOpaquePointerElementType()->isFunctionTy())
1066 return error(ExplicitTypeLoc,
1067 "explicit pointee type should be a function type");
1068 }
1069
1070 GlobalValue *GVal = nullptr;
1071
1072 // See if the alias was forward referenced, if so, prepare to replace the
1073 // forward reference.
1074 if (!Name.empty()) {
1075 auto I = ForwardRefVals.find(Name);
1076 if (I != ForwardRefVals.end()) {
1077 GVal = I->second.first;
1078 ForwardRefVals.erase(Name);
1079 } else if (M->getNamedValue(Name)) {
1080 return error(NameLoc, "redefinition of global '@" + Name + "'");
1081 }
1082 } else {
1083 auto I = ForwardRefValIDs.find(NumberedVals.size());
1084 if (I != ForwardRefValIDs.end()) {
1085 GVal = I->second.first;
1086 ForwardRefValIDs.erase(I);
1087 }
1088 }
1089
1090 // Okay, create the alias/ifunc but do not insert it into the module yet.
1091 std::unique_ptr<GlobalAlias> GA;
1092 std::unique_ptr<GlobalIFunc> GI;
1093 GlobalValue *GV;
1094 if (IsAlias) {
1095 GA.reset(GlobalAlias::create(Ty, AddrSpace,
1096 (GlobalValue::LinkageTypes)Linkage, Name,
1097 Aliasee, /*Parent*/ nullptr));
1098 GV = GA.get();
1099 } else {
1100 GI.reset(GlobalIFunc::create(Ty, AddrSpace,
1101 (GlobalValue::LinkageTypes)Linkage, Name,
1102 Aliasee, /*Parent*/ nullptr));
1103 GV = GI.get();
1104 }
1105 GV->setThreadLocalMode(TLM);
1106 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
1107 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
1108 GV->setUnnamedAddr(UnnamedAddr);
1109 maybeSetDSOLocal(DSOLocal, *GV);
1110
1111 // At this point we've parsed everything except for the IndirectSymbolAttrs.
1112 // Now parse them if there are any.
1113 while (Lex.getKind() == lltok::comma) {
1114 Lex.Lex();
1115
1116 if (Lex.getKind() == lltok::kw_partition) {
1117 Lex.Lex();
1118 GV->setPartition(Lex.getStrVal());
1119 if (parseToken(lltok::StringConstant, "expected partition string"))
1120 return true;
1121 } else {
1122 return tokError("unknown alias or ifunc property!");
1123 }
1124 }
1125
1126 if (Name.empty())
1127 NumberedVals.push_back(GV);
1128
1129 if (GVal) {
1130 // Verify that types agree.
1131 if (GVal->getType() != GV->getType())
1132 return error(
1133 ExplicitTypeLoc,
1134 "forward reference and definition of alias have different types");
1135
1136 // If they agree, just RAUW the old value with the alias and remove the
1137 // forward ref info.
1138 GVal->replaceAllUsesWith(GV);
1139 GVal->eraseFromParent();
1140 }
1141
1142 // Insert into the module, we know its name won't collide now.
1143 if (IsAlias)
1144 M->getAliasList().push_back(GA.release());
1145 else
1146 M->getIFuncList().push_back(GI.release());
1147 assert(GV->getName() == Name && "Should not be a name conflict!");
1148
1149 return false;
1150 }
1151
isSanitizer(lltok::Kind Kind)1152 static bool isSanitizer(lltok::Kind Kind) {
1153 switch (Kind) {
1154 case lltok::kw_no_sanitize_address:
1155 case lltok::kw_no_sanitize_hwaddress:
1156 case lltok::kw_sanitize_memtag:
1157 case lltok::kw_sanitize_address_dyninit:
1158 return true;
1159 default:
1160 return false;
1161 }
1162 }
1163
parseSanitizer(GlobalVariable * GV)1164 bool LLParser::parseSanitizer(GlobalVariable *GV) {
1165 using SanitizerMetadata = GlobalValue::SanitizerMetadata;
1166 SanitizerMetadata Meta;
1167 if (GV->hasSanitizerMetadata())
1168 Meta = GV->getSanitizerMetadata();
1169
1170 switch (Lex.getKind()) {
1171 case lltok::kw_no_sanitize_address:
1172 Meta.NoAddress = true;
1173 break;
1174 case lltok::kw_no_sanitize_hwaddress:
1175 Meta.NoHWAddress = true;
1176 break;
1177 case lltok::kw_sanitize_memtag:
1178 Meta.Memtag = true;
1179 break;
1180 case lltok::kw_sanitize_address_dyninit:
1181 Meta.IsDynInit = true;
1182 break;
1183 default:
1184 return tokError("non-sanitizer token passed to LLParser::parseSanitizer()");
1185 }
1186 GV->setSanitizerMetadata(Meta);
1187 Lex.Lex();
1188 return false;
1189 }
1190
1191 /// parseGlobal
1192 /// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
1193 /// OptionalVisibility OptionalDLLStorageClass
1194 /// OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace
1195 /// OptionalExternallyInitialized GlobalType Type Const OptionalAttrs
1196 /// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
1197 /// OptionalDLLStorageClass OptionalThreadLocal OptionalUnnamedAddr
1198 /// OptionalAddrSpace OptionalExternallyInitialized GlobalType Type
1199 /// Const OptionalAttrs
1200 ///
1201 /// Everything up to and including OptionalUnnamedAddr has been parsed
1202 /// already.
1203 ///
parseGlobal(const std::string & Name,LocTy NameLoc,unsigned Linkage,bool HasLinkage,unsigned Visibility,unsigned DLLStorageClass,bool DSOLocal,GlobalVariable::ThreadLocalMode TLM,GlobalVariable::UnnamedAddr UnnamedAddr)1204 bool LLParser::parseGlobal(const std::string &Name, LocTy NameLoc,
1205 unsigned Linkage, bool HasLinkage,
1206 unsigned Visibility, unsigned DLLStorageClass,
1207 bool DSOLocal, GlobalVariable::ThreadLocalMode TLM,
1208 GlobalVariable::UnnamedAddr UnnamedAddr) {
1209 if (!isValidVisibilityForLinkage(Visibility, Linkage))
1210 return error(NameLoc,
1211 "symbol with local linkage must have default visibility");
1212
1213 if (!isValidDLLStorageClassForLinkage(DLLStorageClass, Linkage))
1214 return error(NameLoc,
1215 "symbol with local linkage cannot have a DLL storage class");
1216
1217 unsigned AddrSpace;
1218 bool IsConstant, IsExternallyInitialized;
1219 LocTy IsExternallyInitializedLoc;
1220 LocTy TyLoc;
1221
1222 Type *Ty = nullptr;
1223 if (parseOptionalAddrSpace(AddrSpace) ||
1224 parseOptionalToken(lltok::kw_externally_initialized,
1225 IsExternallyInitialized,
1226 &IsExternallyInitializedLoc) ||
1227 parseGlobalType(IsConstant) || parseType(Ty, TyLoc))
1228 return true;
1229
1230 // If the linkage is specified and is external, then no initializer is
1231 // present.
1232 Constant *Init = nullptr;
1233 if (!HasLinkage ||
1234 !GlobalValue::isValidDeclarationLinkage(
1235 (GlobalValue::LinkageTypes)Linkage)) {
1236 if (parseGlobalValue(Ty, Init))
1237 return true;
1238 }
1239
1240 if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
1241 return error(TyLoc, "invalid type for global variable");
1242
1243 GlobalValue *GVal = nullptr;
1244
1245 // See if the global was forward referenced, if so, use the global.
1246 if (!Name.empty()) {
1247 auto I = ForwardRefVals.find(Name);
1248 if (I != ForwardRefVals.end()) {
1249 GVal = I->second.first;
1250 ForwardRefVals.erase(I);
1251 } else if (M->getNamedValue(Name)) {
1252 return error(NameLoc, "redefinition of global '@" + Name + "'");
1253 }
1254 } else {
1255 auto I = ForwardRefValIDs.find(NumberedVals.size());
1256 if (I != ForwardRefValIDs.end()) {
1257 GVal = I->second.first;
1258 ForwardRefValIDs.erase(I);
1259 }
1260 }
1261
1262 GlobalVariable *GV = new GlobalVariable(
1263 *M, Ty, false, GlobalValue::ExternalLinkage, nullptr, Name, nullptr,
1264 GlobalVariable::NotThreadLocal, AddrSpace);
1265
1266 if (Name.empty())
1267 NumberedVals.push_back(GV);
1268
1269 // Set the parsed properties on the global.
1270 if (Init)
1271 GV->setInitializer(Init);
1272 GV->setConstant(IsConstant);
1273 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
1274 maybeSetDSOLocal(DSOLocal, *GV);
1275 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
1276 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
1277 GV->setExternallyInitialized(IsExternallyInitialized);
1278 GV->setThreadLocalMode(TLM);
1279 GV->setUnnamedAddr(UnnamedAddr);
1280
1281 if (GVal) {
1282 if (GVal->getType() != Ty->getPointerTo(AddrSpace))
1283 return error(
1284 TyLoc,
1285 "forward reference and definition of global have different types");
1286
1287 GVal->replaceAllUsesWith(GV);
1288 GVal->eraseFromParent();
1289 }
1290
1291 // parse attributes on the global.
1292 while (Lex.getKind() == lltok::comma) {
1293 Lex.Lex();
1294
1295 if (Lex.getKind() == lltok::kw_section) {
1296 Lex.Lex();
1297 GV->setSection(Lex.getStrVal());
1298 if (parseToken(lltok::StringConstant, "expected global section string"))
1299 return true;
1300 } else if (Lex.getKind() == lltok::kw_partition) {
1301 Lex.Lex();
1302 GV->setPartition(Lex.getStrVal());
1303 if (parseToken(lltok::StringConstant, "expected partition string"))
1304 return true;
1305 } else if (Lex.getKind() == lltok::kw_align) {
1306 MaybeAlign Alignment;
1307 if (parseOptionalAlignment(Alignment))
1308 return true;
1309 GV->setAlignment(Alignment);
1310 } else if (Lex.getKind() == lltok::MetadataVar) {
1311 if (parseGlobalObjectMetadataAttachment(*GV))
1312 return true;
1313 } else if (isSanitizer(Lex.getKind())) {
1314 if (parseSanitizer(GV))
1315 return true;
1316 } else {
1317 Comdat *C;
1318 if (parseOptionalComdat(Name, C))
1319 return true;
1320 if (C)
1321 GV->setComdat(C);
1322 else
1323 return tokError("unknown global variable property!");
1324 }
1325 }
1326
1327 AttrBuilder Attrs(M->getContext());
1328 LocTy BuiltinLoc;
1329 std::vector<unsigned> FwdRefAttrGrps;
1330 if (parseFnAttributeValuePairs(Attrs, FwdRefAttrGrps, false, BuiltinLoc))
1331 return true;
1332 if (Attrs.hasAttributes() || !FwdRefAttrGrps.empty()) {
1333 GV->setAttributes(AttributeSet::get(Context, Attrs));
1334 ForwardRefAttrGroups[GV] = FwdRefAttrGrps;
1335 }
1336
1337 return false;
1338 }
1339
1340 /// parseUnnamedAttrGrp
1341 /// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
parseUnnamedAttrGrp()1342 bool LLParser::parseUnnamedAttrGrp() {
1343 assert(Lex.getKind() == lltok::kw_attributes);
1344 LocTy AttrGrpLoc = Lex.getLoc();
1345 Lex.Lex();
1346
1347 if (Lex.getKind() != lltok::AttrGrpID)
1348 return tokError("expected attribute group id");
1349
1350 unsigned VarID = Lex.getUIntVal();
1351 std::vector<unsigned> unused;
1352 LocTy BuiltinLoc;
1353 Lex.Lex();
1354
1355 if (parseToken(lltok::equal, "expected '=' here") ||
1356 parseToken(lltok::lbrace, "expected '{' here"))
1357 return true;
1358
1359 auto R = NumberedAttrBuilders.find(VarID);
1360 if (R == NumberedAttrBuilders.end())
1361 R = NumberedAttrBuilders.emplace(VarID, AttrBuilder(M->getContext())).first;
1362
1363 if (parseFnAttributeValuePairs(R->second, unused, true, BuiltinLoc) ||
1364 parseToken(lltok::rbrace, "expected end of attribute group"))
1365 return true;
1366
1367 if (!R->second.hasAttributes())
1368 return error(AttrGrpLoc, "attribute group has no attributes");
1369
1370 return false;
1371 }
1372
tokenToAttribute(lltok::Kind Kind)1373 static Attribute::AttrKind tokenToAttribute(lltok::Kind Kind) {
1374 switch (Kind) {
1375 #define GET_ATTR_NAMES
1376 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
1377 case lltok::kw_##DISPLAY_NAME: \
1378 return Attribute::ENUM_NAME;
1379 #include "llvm/IR/Attributes.inc"
1380 default:
1381 return Attribute::None;
1382 }
1383 }
1384
parseEnumAttribute(Attribute::AttrKind Attr,AttrBuilder & B,bool InAttrGroup)1385 bool LLParser::parseEnumAttribute(Attribute::AttrKind Attr, AttrBuilder &B,
1386 bool InAttrGroup) {
1387 if (Attribute::isTypeAttrKind(Attr))
1388 return parseRequiredTypeAttr(B, Lex.getKind(), Attr);
1389
1390 switch (Attr) {
1391 case Attribute::Alignment: {
1392 MaybeAlign Alignment;
1393 if (InAttrGroup) {
1394 uint32_t Value = 0;
1395 Lex.Lex();
1396 if (parseToken(lltok::equal, "expected '=' here") || parseUInt32(Value))
1397 return true;
1398 Alignment = Align(Value);
1399 } else {
1400 if (parseOptionalAlignment(Alignment, true))
1401 return true;
1402 }
1403 B.addAlignmentAttr(Alignment);
1404 return false;
1405 }
1406 case Attribute::StackAlignment: {
1407 unsigned Alignment;
1408 if (InAttrGroup) {
1409 Lex.Lex();
1410 if (parseToken(lltok::equal, "expected '=' here") ||
1411 parseUInt32(Alignment))
1412 return true;
1413 } else {
1414 if (parseOptionalStackAlignment(Alignment))
1415 return true;
1416 }
1417 B.addStackAlignmentAttr(Alignment);
1418 return false;
1419 }
1420 case Attribute::AllocSize: {
1421 unsigned ElemSizeArg;
1422 std::optional<unsigned> NumElemsArg;
1423 if (parseAllocSizeArguments(ElemSizeArg, NumElemsArg))
1424 return true;
1425 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1426 return false;
1427 }
1428 case Attribute::VScaleRange: {
1429 unsigned MinValue, MaxValue;
1430 if (parseVScaleRangeArguments(MinValue, MaxValue))
1431 return true;
1432 B.addVScaleRangeAttr(MinValue,
1433 MaxValue > 0 ? MaxValue : std::optional<unsigned>());
1434 return false;
1435 }
1436 case Attribute::Dereferenceable: {
1437 uint64_t Bytes;
1438 if (parseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
1439 return true;
1440 B.addDereferenceableAttr(Bytes);
1441 return false;
1442 }
1443 case Attribute::DereferenceableOrNull: {
1444 uint64_t Bytes;
1445 if (parseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
1446 return true;
1447 B.addDereferenceableOrNullAttr(Bytes);
1448 return false;
1449 }
1450 case Attribute::UWTable: {
1451 UWTableKind Kind;
1452 if (parseOptionalUWTableKind(Kind))
1453 return true;
1454 B.addUWTableAttr(Kind);
1455 return false;
1456 }
1457 case Attribute::AllocKind: {
1458 AllocFnKind Kind = AllocFnKind::Unknown;
1459 if (parseAllocKind(Kind))
1460 return true;
1461 B.addAllocKindAttr(Kind);
1462 return false;
1463 }
1464 case Attribute::Memory: {
1465 std::optional<MemoryEffects> ME = parseMemoryAttr();
1466 if (!ME)
1467 return true;
1468 B.addMemoryAttr(*ME);
1469 return false;
1470 }
1471 default:
1472 B.addAttribute(Attr);
1473 Lex.Lex();
1474 return false;
1475 }
1476 }
1477
upgradeMemoryAttr(MemoryEffects & ME,lltok::Kind Kind)1478 static bool upgradeMemoryAttr(MemoryEffects &ME, lltok::Kind Kind) {
1479 switch (Kind) {
1480 case lltok::kw_readnone:
1481 ME &= MemoryEffects::none();
1482 return true;
1483 case lltok::kw_readonly:
1484 ME &= MemoryEffects::readOnly();
1485 return true;
1486 case lltok::kw_writeonly:
1487 ME &= MemoryEffects::writeOnly();
1488 return true;
1489 case lltok::kw_argmemonly:
1490 ME &= MemoryEffects::argMemOnly();
1491 return true;
1492 case lltok::kw_inaccessiblememonly:
1493 ME &= MemoryEffects::inaccessibleMemOnly();
1494 return true;
1495 case lltok::kw_inaccessiblemem_or_argmemonly:
1496 ME &= MemoryEffects::inaccessibleOrArgMemOnly();
1497 return true;
1498 default:
1499 return false;
1500 }
1501 }
1502
1503 /// parseFnAttributeValuePairs
1504 /// ::= <attr> | <attr> '=' <value>
parseFnAttributeValuePairs(AttrBuilder & B,std::vector<unsigned> & FwdRefAttrGrps,bool InAttrGrp,LocTy & BuiltinLoc)1505 bool LLParser::parseFnAttributeValuePairs(AttrBuilder &B,
1506 std::vector<unsigned> &FwdRefAttrGrps,
1507 bool InAttrGrp, LocTy &BuiltinLoc) {
1508 bool HaveError = false;
1509
1510 B.clear();
1511
1512 MemoryEffects ME = MemoryEffects::unknown();
1513 while (true) {
1514 lltok::Kind Token = Lex.getKind();
1515 if (Token == lltok::rbrace)
1516 break; // Finished.
1517
1518 if (Token == lltok::StringConstant) {
1519 if (parseStringAttribute(B))
1520 return true;
1521 continue;
1522 }
1523
1524 if (Token == lltok::AttrGrpID) {
1525 // Allow a function to reference an attribute group:
1526 //
1527 // define void @foo() #1 { ... }
1528 if (InAttrGrp) {
1529 HaveError |= error(
1530 Lex.getLoc(),
1531 "cannot have an attribute group reference in an attribute group");
1532 } else {
1533 // Save the reference to the attribute group. We'll fill it in later.
1534 FwdRefAttrGrps.push_back(Lex.getUIntVal());
1535 }
1536 Lex.Lex();
1537 continue;
1538 }
1539
1540 SMLoc Loc = Lex.getLoc();
1541 if (Token == lltok::kw_builtin)
1542 BuiltinLoc = Loc;
1543
1544 if (upgradeMemoryAttr(ME, Token)) {
1545 Lex.Lex();
1546 continue;
1547 }
1548
1549 Attribute::AttrKind Attr = tokenToAttribute(Token);
1550 if (Attr == Attribute::None) {
1551 if (!InAttrGrp)
1552 break;
1553 return error(Lex.getLoc(), "unterminated attribute group");
1554 }
1555
1556 if (parseEnumAttribute(Attr, B, InAttrGrp))
1557 return true;
1558
1559 // As a hack, we allow function alignment to be initially parsed as an
1560 // attribute on a function declaration/definition or added to an attribute
1561 // group and later moved to the alignment field.
1562 if (!Attribute::canUseAsFnAttr(Attr) && Attr != Attribute::Alignment)
1563 HaveError |= error(Loc, "this attribute does not apply to functions");
1564 }
1565
1566 if (ME != MemoryEffects::unknown())
1567 B.addMemoryAttr(ME);
1568 return HaveError;
1569 }
1570
1571 //===----------------------------------------------------------------------===//
1572 // GlobalValue Reference/Resolution Routines.
1573 //===----------------------------------------------------------------------===//
1574
createGlobalFwdRef(Module * M,PointerType * PTy)1575 static inline GlobalValue *createGlobalFwdRef(Module *M, PointerType *PTy) {
1576 // For opaque pointers, the used global type does not matter. We will later
1577 // RAUW it with a global/function of the correct type.
1578 if (PTy->isOpaque())
1579 return new GlobalVariable(*M, Type::getInt8Ty(M->getContext()), false,
1580 GlobalValue::ExternalWeakLinkage, nullptr, "",
1581 nullptr, GlobalVariable::NotThreadLocal,
1582 PTy->getAddressSpace());
1583
1584 Type *ElemTy = PTy->getNonOpaquePointerElementType();
1585 if (auto *FT = dyn_cast<FunctionType>(ElemTy))
1586 return Function::Create(FT, GlobalValue::ExternalWeakLinkage,
1587 PTy->getAddressSpace(), "", M);
1588 else
1589 return new GlobalVariable(
1590 *M, ElemTy, false, GlobalValue::ExternalWeakLinkage, nullptr, "",
1591 nullptr, GlobalVariable::NotThreadLocal, PTy->getAddressSpace());
1592 }
1593
checkValidVariableType(LocTy Loc,const Twine & Name,Type * Ty,Value * Val)1594 Value *LLParser::checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty,
1595 Value *Val) {
1596 Type *ValTy = Val->getType();
1597 if (ValTy == Ty)
1598 return Val;
1599 if (Ty->isLabelTy())
1600 error(Loc, "'" + Name + "' is not a basic block");
1601 else
1602 error(Loc, "'" + Name + "' defined with type '" +
1603 getTypeString(Val->getType()) + "' but expected '" +
1604 getTypeString(Ty) + "'");
1605 return nullptr;
1606 }
1607
1608 /// getGlobalVal - Get a value with the specified name or ID, creating a
1609 /// forward reference record if needed. This can return null if the value
1610 /// exists but does not have the right type.
getGlobalVal(const std::string & Name,Type * Ty,LocTy Loc)1611 GlobalValue *LLParser::getGlobalVal(const std::string &Name, Type *Ty,
1612 LocTy Loc) {
1613 PointerType *PTy = dyn_cast<PointerType>(Ty);
1614 if (!PTy) {
1615 error(Loc, "global variable reference must have pointer type");
1616 return nullptr;
1617 }
1618
1619 // Look this name up in the normal function symbol table.
1620 GlobalValue *Val =
1621 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
1622
1623 // If this is a forward reference for the value, see if we already created a
1624 // forward ref record.
1625 if (!Val) {
1626 auto I = ForwardRefVals.find(Name);
1627 if (I != ForwardRefVals.end())
1628 Val = I->second.first;
1629 }
1630
1631 // If we have the value in the symbol table or fwd-ref table, return it.
1632 if (Val)
1633 return cast_or_null<GlobalValue>(
1634 checkValidVariableType(Loc, "@" + Name, Ty, Val));
1635
1636 // Otherwise, create a new forward reference for this value and remember it.
1637 GlobalValue *FwdVal = createGlobalFwdRef(M, PTy);
1638 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1639 return FwdVal;
1640 }
1641
getGlobalVal(unsigned ID,Type * Ty,LocTy Loc)1642 GlobalValue *LLParser::getGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1643 PointerType *PTy = dyn_cast<PointerType>(Ty);
1644 if (!PTy) {
1645 error(Loc, "global variable reference must have pointer type");
1646 return nullptr;
1647 }
1648
1649 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
1650
1651 // If this is a forward reference for the value, see if we already created a
1652 // forward ref record.
1653 if (!Val) {
1654 auto I = ForwardRefValIDs.find(ID);
1655 if (I != ForwardRefValIDs.end())
1656 Val = I->second.first;
1657 }
1658
1659 // If we have the value in the symbol table or fwd-ref table, return it.
1660 if (Val)
1661 return cast_or_null<GlobalValue>(
1662 checkValidVariableType(Loc, "@" + Twine(ID), Ty, Val));
1663
1664 // Otherwise, create a new forward reference for this value and remember it.
1665 GlobalValue *FwdVal = createGlobalFwdRef(M, PTy);
1666 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1667 return FwdVal;
1668 }
1669
1670 //===----------------------------------------------------------------------===//
1671 // Comdat Reference/Resolution Routines.
1672 //===----------------------------------------------------------------------===//
1673
getComdat(const std::string & Name,LocTy Loc)1674 Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) {
1675 // Look this name up in the comdat symbol table.
1676 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
1677 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
1678 if (I != ComdatSymTab.end())
1679 return &I->second;
1680
1681 // Otherwise, create a new forward reference for this value and remember it.
1682 Comdat *C = M->getOrInsertComdat(Name);
1683 ForwardRefComdats[Name] = Loc;
1684 return C;
1685 }
1686
1687 //===----------------------------------------------------------------------===//
1688 // Helper Routines.
1689 //===----------------------------------------------------------------------===//
1690
1691 /// parseToken - If the current token has the specified kind, eat it and return
1692 /// success. Otherwise, emit the specified error and return failure.
parseToken(lltok::Kind T,const char * ErrMsg)1693 bool LLParser::parseToken(lltok::Kind T, const char *ErrMsg) {
1694 if (Lex.getKind() != T)
1695 return tokError(ErrMsg);
1696 Lex.Lex();
1697 return false;
1698 }
1699
1700 /// parseStringConstant
1701 /// ::= StringConstant
parseStringConstant(std::string & Result)1702 bool LLParser::parseStringConstant(std::string &Result) {
1703 if (Lex.getKind() != lltok::StringConstant)
1704 return tokError("expected string constant");
1705 Result = Lex.getStrVal();
1706 Lex.Lex();
1707 return false;
1708 }
1709
1710 /// parseUInt32
1711 /// ::= uint32
parseUInt32(uint32_t & Val)1712 bool LLParser::parseUInt32(uint32_t &Val) {
1713 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1714 return tokError("expected integer");
1715 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1716 if (Val64 != unsigned(Val64))
1717 return tokError("expected 32-bit integer (too large)");
1718 Val = Val64;
1719 Lex.Lex();
1720 return false;
1721 }
1722
1723 /// parseUInt64
1724 /// ::= uint64
parseUInt64(uint64_t & Val)1725 bool LLParser::parseUInt64(uint64_t &Val) {
1726 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1727 return tokError("expected integer");
1728 Val = Lex.getAPSIntVal().getLimitedValue();
1729 Lex.Lex();
1730 return false;
1731 }
1732
1733 /// parseTLSModel
1734 /// := 'localdynamic'
1735 /// := 'initialexec'
1736 /// := 'localexec'
parseTLSModel(GlobalVariable::ThreadLocalMode & TLM)1737 bool LLParser::parseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1738 switch (Lex.getKind()) {
1739 default:
1740 return tokError("expected localdynamic, initialexec or localexec");
1741 case lltok::kw_localdynamic:
1742 TLM = GlobalVariable::LocalDynamicTLSModel;
1743 break;
1744 case lltok::kw_initialexec:
1745 TLM = GlobalVariable::InitialExecTLSModel;
1746 break;
1747 case lltok::kw_localexec:
1748 TLM = GlobalVariable::LocalExecTLSModel;
1749 break;
1750 }
1751
1752 Lex.Lex();
1753 return false;
1754 }
1755
1756 /// parseOptionalThreadLocal
1757 /// := /*empty*/
1758 /// := 'thread_local'
1759 /// := 'thread_local' '(' tlsmodel ')'
parseOptionalThreadLocal(GlobalVariable::ThreadLocalMode & TLM)1760 bool LLParser::parseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1761 TLM = GlobalVariable::NotThreadLocal;
1762 if (!EatIfPresent(lltok::kw_thread_local))
1763 return false;
1764
1765 TLM = GlobalVariable::GeneralDynamicTLSModel;
1766 if (Lex.getKind() == lltok::lparen) {
1767 Lex.Lex();
1768 return parseTLSModel(TLM) ||
1769 parseToken(lltok::rparen, "expected ')' after thread local model");
1770 }
1771 return false;
1772 }
1773
1774 /// parseOptionalAddrSpace
1775 /// := /*empty*/
1776 /// := 'addrspace' '(' uint32 ')'
parseOptionalAddrSpace(unsigned & AddrSpace,unsigned DefaultAS)1777 bool LLParser::parseOptionalAddrSpace(unsigned &AddrSpace, unsigned DefaultAS) {
1778 AddrSpace = DefaultAS;
1779 if (!EatIfPresent(lltok::kw_addrspace))
1780 return false;
1781
1782 auto ParseAddrspaceValue = [&](unsigned &AddrSpace) -> bool {
1783 if (Lex.getKind() == lltok::StringConstant) {
1784 auto AddrSpaceStr = Lex.getStrVal();
1785 if (AddrSpaceStr == "A") {
1786 AddrSpace = M->getDataLayout().getAllocaAddrSpace();
1787 } else if (AddrSpaceStr == "G") {
1788 AddrSpace = M->getDataLayout().getDefaultGlobalsAddressSpace();
1789 } else if (AddrSpaceStr == "P") {
1790 AddrSpace = M->getDataLayout().getProgramAddressSpace();
1791 } else {
1792 return tokError("invalid symbolic addrspace '" + AddrSpaceStr + "'");
1793 }
1794 Lex.Lex();
1795 return false;
1796 }
1797 if (Lex.getKind() != lltok::APSInt)
1798 return tokError("expected integer or string constant");
1799 SMLoc Loc = Lex.getLoc();
1800 if (parseUInt32(AddrSpace))
1801 return true;
1802 if (!isUInt<24>(AddrSpace))
1803 return error(Loc, "invalid address space, must be a 24-bit integer");
1804 return false;
1805 };
1806
1807 return parseToken(lltok::lparen, "expected '(' in address space") ||
1808 ParseAddrspaceValue(AddrSpace) ||
1809 parseToken(lltok::rparen, "expected ')' in address space");
1810 }
1811
1812 /// parseStringAttribute
1813 /// := StringConstant
1814 /// := StringConstant '=' StringConstant
parseStringAttribute(AttrBuilder & B)1815 bool LLParser::parseStringAttribute(AttrBuilder &B) {
1816 std::string Attr = Lex.getStrVal();
1817 Lex.Lex();
1818 std::string Val;
1819 if (EatIfPresent(lltok::equal) && parseStringConstant(Val))
1820 return true;
1821 B.addAttribute(Attr, Val);
1822 return false;
1823 }
1824
1825 /// Parse a potentially empty list of parameter or return attributes.
parseOptionalParamOrReturnAttrs(AttrBuilder & B,bool IsParam)1826 bool LLParser::parseOptionalParamOrReturnAttrs(AttrBuilder &B, bool IsParam) {
1827 bool HaveError = false;
1828
1829 B.clear();
1830
1831 while (true) {
1832 lltok::Kind Token = Lex.getKind();
1833 if (Token == lltok::StringConstant) {
1834 if (parseStringAttribute(B))
1835 return true;
1836 continue;
1837 }
1838
1839 SMLoc Loc = Lex.getLoc();
1840 Attribute::AttrKind Attr = tokenToAttribute(Token);
1841 if (Attr == Attribute::None)
1842 return HaveError;
1843
1844 if (parseEnumAttribute(Attr, B, /* InAttrGroup */ false))
1845 return true;
1846
1847 if (IsParam && !Attribute::canUseAsParamAttr(Attr))
1848 HaveError |= error(Loc, "this attribute does not apply to parameters");
1849 if (!IsParam && !Attribute::canUseAsRetAttr(Attr))
1850 HaveError |= error(Loc, "this attribute does not apply to return values");
1851 }
1852 }
1853
parseOptionalLinkageAux(lltok::Kind Kind,bool & HasLinkage)1854 static unsigned parseOptionalLinkageAux(lltok::Kind Kind, bool &HasLinkage) {
1855 HasLinkage = true;
1856 switch (Kind) {
1857 default:
1858 HasLinkage = false;
1859 return GlobalValue::ExternalLinkage;
1860 case lltok::kw_private:
1861 return GlobalValue::PrivateLinkage;
1862 case lltok::kw_internal:
1863 return GlobalValue::InternalLinkage;
1864 case lltok::kw_weak:
1865 return GlobalValue::WeakAnyLinkage;
1866 case lltok::kw_weak_odr:
1867 return GlobalValue::WeakODRLinkage;
1868 case lltok::kw_linkonce:
1869 return GlobalValue::LinkOnceAnyLinkage;
1870 case lltok::kw_linkonce_odr:
1871 return GlobalValue::LinkOnceODRLinkage;
1872 case lltok::kw_available_externally:
1873 return GlobalValue::AvailableExternallyLinkage;
1874 case lltok::kw_appending:
1875 return GlobalValue::AppendingLinkage;
1876 case lltok::kw_common:
1877 return GlobalValue::CommonLinkage;
1878 case lltok::kw_extern_weak:
1879 return GlobalValue::ExternalWeakLinkage;
1880 case lltok::kw_external:
1881 return GlobalValue::ExternalLinkage;
1882 }
1883 }
1884
1885 /// parseOptionalLinkage
1886 /// ::= /*empty*/
1887 /// ::= 'private'
1888 /// ::= 'internal'
1889 /// ::= 'weak'
1890 /// ::= 'weak_odr'
1891 /// ::= 'linkonce'
1892 /// ::= 'linkonce_odr'
1893 /// ::= 'available_externally'
1894 /// ::= 'appending'
1895 /// ::= 'common'
1896 /// ::= 'extern_weak'
1897 /// ::= 'external'
parseOptionalLinkage(unsigned & Res,bool & HasLinkage,unsigned & Visibility,unsigned & DLLStorageClass,bool & DSOLocal)1898 bool LLParser::parseOptionalLinkage(unsigned &Res, bool &HasLinkage,
1899 unsigned &Visibility,
1900 unsigned &DLLStorageClass, bool &DSOLocal) {
1901 Res = parseOptionalLinkageAux(Lex.getKind(), HasLinkage);
1902 if (HasLinkage)
1903 Lex.Lex();
1904 parseOptionalDSOLocal(DSOLocal);
1905 parseOptionalVisibility(Visibility);
1906 parseOptionalDLLStorageClass(DLLStorageClass);
1907
1908 if (DSOLocal && DLLStorageClass == GlobalValue::DLLImportStorageClass) {
1909 return error(Lex.getLoc(), "dso_location and DLL-StorageClass mismatch");
1910 }
1911
1912 return false;
1913 }
1914
parseOptionalDSOLocal(bool & DSOLocal)1915 void LLParser::parseOptionalDSOLocal(bool &DSOLocal) {
1916 switch (Lex.getKind()) {
1917 default:
1918 DSOLocal = false;
1919 break;
1920 case lltok::kw_dso_local:
1921 DSOLocal = true;
1922 Lex.Lex();
1923 break;
1924 case lltok::kw_dso_preemptable:
1925 DSOLocal = false;
1926 Lex.Lex();
1927 break;
1928 }
1929 }
1930
1931 /// parseOptionalVisibility
1932 /// ::= /*empty*/
1933 /// ::= 'default'
1934 /// ::= 'hidden'
1935 /// ::= 'protected'
1936 ///
parseOptionalVisibility(unsigned & Res)1937 void LLParser::parseOptionalVisibility(unsigned &Res) {
1938 switch (Lex.getKind()) {
1939 default:
1940 Res = GlobalValue::DefaultVisibility;
1941 return;
1942 case lltok::kw_default:
1943 Res = GlobalValue::DefaultVisibility;
1944 break;
1945 case lltok::kw_hidden:
1946 Res = GlobalValue::HiddenVisibility;
1947 break;
1948 case lltok::kw_protected:
1949 Res = GlobalValue::ProtectedVisibility;
1950 break;
1951 }
1952 Lex.Lex();
1953 }
1954
1955 /// parseOptionalDLLStorageClass
1956 /// ::= /*empty*/
1957 /// ::= 'dllimport'
1958 /// ::= 'dllexport'
1959 ///
parseOptionalDLLStorageClass(unsigned & Res)1960 void LLParser::parseOptionalDLLStorageClass(unsigned &Res) {
1961 switch (Lex.getKind()) {
1962 default:
1963 Res = GlobalValue::DefaultStorageClass;
1964 return;
1965 case lltok::kw_dllimport:
1966 Res = GlobalValue::DLLImportStorageClass;
1967 break;
1968 case lltok::kw_dllexport:
1969 Res = GlobalValue::DLLExportStorageClass;
1970 break;
1971 }
1972 Lex.Lex();
1973 }
1974
1975 /// parseOptionalCallingConv
1976 /// ::= /*empty*/
1977 /// ::= 'ccc'
1978 /// ::= 'fastcc'
1979 /// ::= 'intel_ocl_bicc'
1980 /// ::= 'coldcc'
1981 /// ::= 'cfguard_checkcc'
1982 /// ::= 'x86_stdcallcc'
1983 /// ::= 'x86_fastcallcc'
1984 /// ::= 'x86_thiscallcc'
1985 /// ::= 'x86_vectorcallcc'
1986 /// ::= 'arm_apcscc'
1987 /// ::= 'arm_aapcscc'
1988 /// ::= 'arm_aapcs_vfpcc'
1989 /// ::= 'aarch64_vector_pcs'
1990 /// ::= 'aarch64_sve_vector_pcs'
1991 /// ::= 'aarch64_sme_preservemost_from_x0'
1992 /// ::= 'aarch64_sme_preservemost_from_x2'
1993 /// ::= 'msp430_intrcc'
1994 /// ::= 'avr_intrcc'
1995 /// ::= 'avr_signalcc'
1996 /// ::= 'ptx_kernel'
1997 /// ::= 'ptx_device'
1998 /// ::= 'spir_func'
1999 /// ::= 'spir_kernel'
2000 /// ::= 'x86_64_sysvcc'
2001 /// ::= 'win64cc'
2002 /// ::= 'webkit_jscc'
2003 /// ::= 'anyregcc'
2004 /// ::= 'preserve_mostcc'
2005 /// ::= 'preserve_allcc'
2006 /// ::= 'ghccc'
2007 /// ::= 'swiftcc'
2008 /// ::= 'swifttailcc'
2009 /// ::= 'x86_intrcc'
2010 /// ::= 'hhvmcc'
2011 /// ::= 'hhvm_ccc'
2012 /// ::= 'cxx_fast_tlscc'
2013 /// ::= 'amdgpu_vs'
2014 /// ::= 'amdgpu_ls'
2015 /// ::= 'amdgpu_hs'
2016 /// ::= 'amdgpu_es'
2017 /// ::= 'amdgpu_gs'
2018 /// ::= 'amdgpu_ps'
2019 /// ::= 'amdgpu_cs'
2020 /// ::= 'amdgpu_kernel'
2021 /// ::= 'tailcc'
2022 /// ::= 'cc' UINT
2023 ///
parseOptionalCallingConv(unsigned & CC)2024 bool LLParser::parseOptionalCallingConv(unsigned &CC) {
2025 switch (Lex.getKind()) {
2026 default: CC = CallingConv::C; return false;
2027 case lltok::kw_ccc: CC = CallingConv::C; break;
2028 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
2029 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
2030 case lltok::kw_cfguard_checkcc: CC = CallingConv::CFGuard_Check; break;
2031 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
2032 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
2033 case lltok::kw_x86_regcallcc: CC = CallingConv::X86_RegCall; break;
2034 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
2035 case lltok::kw_x86_vectorcallcc:CC = CallingConv::X86_VectorCall; break;
2036 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
2037 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
2038 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
2039 case lltok::kw_aarch64_vector_pcs:CC = CallingConv::AArch64_VectorCall; break;
2040 case lltok::kw_aarch64_sve_vector_pcs:
2041 CC = CallingConv::AArch64_SVE_VectorCall;
2042 break;
2043 case lltok::kw_aarch64_sme_preservemost_from_x0:
2044 CC = CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0;
2045 break;
2046 case lltok::kw_aarch64_sme_preservemost_from_x2:
2047 CC = CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2;
2048 break;
2049 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
2050 case lltok::kw_avr_intrcc: CC = CallingConv::AVR_INTR; break;
2051 case lltok::kw_avr_signalcc: CC = CallingConv::AVR_SIGNAL; break;
2052 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
2053 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
2054 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
2055 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
2056 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
2057 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
2058 case lltok::kw_win64cc: CC = CallingConv::Win64; break;
2059 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
2060 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
2061 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
2062 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
2063 case lltok::kw_ghccc: CC = CallingConv::GHC; break;
2064 case lltok::kw_swiftcc: CC = CallingConv::Swift; break;
2065 case lltok::kw_swifttailcc: CC = CallingConv::SwiftTail; break;
2066 case lltok::kw_x86_intrcc: CC = CallingConv::X86_INTR; break;
2067 case lltok::kw_hhvmcc: CC = CallingConv::HHVM; break;
2068 case lltok::kw_hhvm_ccc: CC = CallingConv::HHVM_C; break;
2069 case lltok::kw_cxx_fast_tlscc: CC = CallingConv::CXX_FAST_TLS; break;
2070 case lltok::kw_amdgpu_vs: CC = CallingConv::AMDGPU_VS; break;
2071 case lltok::kw_amdgpu_gfx: CC = CallingConv::AMDGPU_Gfx; break;
2072 case lltok::kw_amdgpu_ls: CC = CallingConv::AMDGPU_LS; break;
2073 case lltok::kw_amdgpu_hs: CC = CallingConv::AMDGPU_HS; break;
2074 case lltok::kw_amdgpu_es: CC = CallingConv::AMDGPU_ES; break;
2075 case lltok::kw_amdgpu_gs: CC = CallingConv::AMDGPU_GS; break;
2076 case lltok::kw_amdgpu_ps: CC = CallingConv::AMDGPU_PS; break;
2077 case lltok::kw_amdgpu_cs: CC = CallingConv::AMDGPU_CS; break;
2078 case lltok::kw_amdgpu_kernel: CC = CallingConv::AMDGPU_KERNEL; break;
2079 case lltok::kw_tailcc: CC = CallingConv::Tail; break;
2080 case lltok::kw_cc: {
2081 Lex.Lex();
2082 return parseUInt32(CC);
2083 }
2084 }
2085
2086 Lex.Lex();
2087 return false;
2088 }
2089
2090 /// parseMetadataAttachment
2091 /// ::= !dbg !42
parseMetadataAttachment(unsigned & Kind,MDNode * & MD)2092 bool LLParser::parseMetadataAttachment(unsigned &Kind, MDNode *&MD) {
2093 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata attachment");
2094
2095 std::string Name = Lex.getStrVal();
2096 Kind = M->getMDKindID(Name);
2097 Lex.Lex();
2098
2099 return parseMDNode(MD);
2100 }
2101
2102 /// parseInstructionMetadata
2103 /// ::= !dbg !42 (',' !dbg !57)*
parseInstructionMetadata(Instruction & Inst)2104 bool LLParser::parseInstructionMetadata(Instruction &Inst) {
2105 do {
2106 if (Lex.getKind() != lltok::MetadataVar)
2107 return tokError("expected metadata after comma");
2108
2109 unsigned MDK;
2110 MDNode *N;
2111 if (parseMetadataAttachment(MDK, N))
2112 return true;
2113
2114 if (MDK == LLVMContext::MD_DIAssignID)
2115 TempDIAssignIDAttachments[N].push_back(&Inst);
2116 else
2117 Inst.setMetadata(MDK, N);
2118
2119 if (MDK == LLVMContext::MD_tbaa)
2120 InstsWithTBAATag.push_back(&Inst);
2121
2122 // If this is the end of the list, we're done.
2123 } while (EatIfPresent(lltok::comma));
2124 return false;
2125 }
2126
2127 /// parseGlobalObjectMetadataAttachment
2128 /// ::= !dbg !57
parseGlobalObjectMetadataAttachment(GlobalObject & GO)2129 bool LLParser::parseGlobalObjectMetadataAttachment(GlobalObject &GO) {
2130 unsigned MDK;
2131 MDNode *N;
2132 if (parseMetadataAttachment(MDK, N))
2133 return true;
2134
2135 GO.addMetadata(MDK, *N);
2136 return false;
2137 }
2138
2139 /// parseOptionalFunctionMetadata
2140 /// ::= (!dbg !57)*
parseOptionalFunctionMetadata(Function & F)2141 bool LLParser::parseOptionalFunctionMetadata(Function &F) {
2142 while (Lex.getKind() == lltok::MetadataVar)
2143 if (parseGlobalObjectMetadataAttachment(F))
2144 return true;
2145 return false;
2146 }
2147
2148 /// parseOptionalAlignment
2149 /// ::= /* empty */
2150 /// ::= 'align' 4
parseOptionalAlignment(MaybeAlign & Alignment,bool AllowParens)2151 bool LLParser::parseOptionalAlignment(MaybeAlign &Alignment, bool AllowParens) {
2152 Alignment = std::nullopt;
2153 if (!EatIfPresent(lltok::kw_align))
2154 return false;
2155 LocTy AlignLoc = Lex.getLoc();
2156 uint64_t Value = 0;
2157
2158 LocTy ParenLoc = Lex.getLoc();
2159 bool HaveParens = false;
2160 if (AllowParens) {
2161 if (EatIfPresent(lltok::lparen))
2162 HaveParens = true;
2163 }
2164
2165 if (parseUInt64(Value))
2166 return true;
2167
2168 if (HaveParens && !EatIfPresent(lltok::rparen))
2169 return error(ParenLoc, "expected ')'");
2170
2171 if (!isPowerOf2_64(Value))
2172 return error(AlignLoc, "alignment is not a power of two");
2173 if (Value > Value::MaximumAlignment)
2174 return error(AlignLoc, "huge alignments are not supported yet");
2175 Alignment = Align(Value);
2176 return false;
2177 }
2178
2179 /// parseOptionalDerefAttrBytes
2180 /// ::= /* empty */
2181 /// ::= AttrKind '(' 4 ')'
2182 ///
2183 /// where AttrKind is either 'dereferenceable' or 'dereferenceable_or_null'.
parseOptionalDerefAttrBytes(lltok::Kind AttrKind,uint64_t & Bytes)2184 bool LLParser::parseOptionalDerefAttrBytes(lltok::Kind AttrKind,
2185 uint64_t &Bytes) {
2186 assert((AttrKind == lltok::kw_dereferenceable ||
2187 AttrKind == lltok::kw_dereferenceable_or_null) &&
2188 "contract!");
2189
2190 Bytes = 0;
2191 if (!EatIfPresent(AttrKind))
2192 return false;
2193 LocTy ParenLoc = Lex.getLoc();
2194 if (!EatIfPresent(lltok::lparen))
2195 return error(ParenLoc, "expected '('");
2196 LocTy DerefLoc = Lex.getLoc();
2197 if (parseUInt64(Bytes))
2198 return true;
2199 ParenLoc = Lex.getLoc();
2200 if (!EatIfPresent(lltok::rparen))
2201 return error(ParenLoc, "expected ')'");
2202 if (!Bytes)
2203 return error(DerefLoc, "dereferenceable bytes must be non-zero");
2204 return false;
2205 }
2206
parseOptionalUWTableKind(UWTableKind & Kind)2207 bool LLParser::parseOptionalUWTableKind(UWTableKind &Kind) {
2208 Lex.Lex();
2209 Kind = UWTableKind::Default;
2210 if (!EatIfPresent(lltok::lparen))
2211 return false;
2212 LocTy KindLoc = Lex.getLoc();
2213 if (Lex.getKind() == lltok::kw_sync)
2214 Kind = UWTableKind::Sync;
2215 else if (Lex.getKind() == lltok::kw_async)
2216 Kind = UWTableKind::Async;
2217 else
2218 return error(KindLoc, "expected unwind table kind");
2219 Lex.Lex();
2220 return parseToken(lltok::rparen, "expected ')'");
2221 }
2222
parseAllocKind(AllocFnKind & Kind)2223 bool LLParser::parseAllocKind(AllocFnKind &Kind) {
2224 Lex.Lex();
2225 LocTy ParenLoc = Lex.getLoc();
2226 if (!EatIfPresent(lltok::lparen))
2227 return error(ParenLoc, "expected '('");
2228 LocTy KindLoc = Lex.getLoc();
2229 std::string Arg;
2230 if (parseStringConstant(Arg))
2231 return error(KindLoc, "expected allockind value");
2232 for (StringRef A : llvm::split(Arg, ",")) {
2233 if (A == "alloc") {
2234 Kind |= AllocFnKind::Alloc;
2235 } else if (A == "realloc") {
2236 Kind |= AllocFnKind::Realloc;
2237 } else if (A == "free") {
2238 Kind |= AllocFnKind::Free;
2239 } else if (A == "uninitialized") {
2240 Kind |= AllocFnKind::Uninitialized;
2241 } else if (A == "zeroed") {
2242 Kind |= AllocFnKind::Zeroed;
2243 } else if (A == "aligned") {
2244 Kind |= AllocFnKind::Aligned;
2245 } else {
2246 return error(KindLoc, Twine("unknown allockind ") + A);
2247 }
2248 }
2249 ParenLoc = Lex.getLoc();
2250 if (!EatIfPresent(lltok::rparen))
2251 return error(ParenLoc, "expected ')'");
2252 if (Kind == AllocFnKind::Unknown)
2253 return error(KindLoc, "expected allockind value");
2254 return false;
2255 }
2256
keywordToLoc(lltok::Kind Tok)2257 static std::optional<MemoryEffects::Location> keywordToLoc(lltok::Kind Tok) {
2258 switch (Tok) {
2259 case lltok::kw_argmem:
2260 return MemoryEffects::ArgMem;
2261 case lltok::kw_inaccessiblemem:
2262 return MemoryEffects::InaccessibleMem;
2263 default:
2264 return std::nullopt;
2265 }
2266 }
2267
keywordToModRef(lltok::Kind Tok)2268 static std::optional<ModRefInfo> keywordToModRef(lltok::Kind Tok) {
2269 switch (Tok) {
2270 case lltok::kw_none:
2271 return ModRefInfo::NoModRef;
2272 case lltok::kw_read:
2273 return ModRefInfo::Ref;
2274 case lltok::kw_write:
2275 return ModRefInfo::Mod;
2276 case lltok::kw_readwrite:
2277 return ModRefInfo::ModRef;
2278 default:
2279 return std::nullopt;
2280 }
2281 }
2282
parseMemoryAttr()2283 std::optional<MemoryEffects> LLParser::parseMemoryAttr() {
2284 MemoryEffects ME = MemoryEffects::none();
2285
2286 // We use syntax like memory(argmem: read), so the colon should not be
2287 // interpreted as a label terminator.
2288 Lex.setIgnoreColonInIdentifiers(true);
2289 auto _ = make_scope_exit([&] { Lex.setIgnoreColonInIdentifiers(false); });
2290
2291 Lex.Lex();
2292 if (!EatIfPresent(lltok::lparen)) {
2293 tokError("expected '('");
2294 return std::nullopt;
2295 }
2296
2297 bool SeenLoc = false;
2298 do {
2299 std::optional<MemoryEffects::Location> Loc = keywordToLoc(Lex.getKind());
2300 if (Loc) {
2301 Lex.Lex();
2302 if (!EatIfPresent(lltok::colon)) {
2303 tokError("expected ':' after location");
2304 return std::nullopt;
2305 }
2306 }
2307
2308 std::optional<ModRefInfo> MR = keywordToModRef(Lex.getKind());
2309 if (!MR) {
2310 if (!Loc)
2311 tokError("expected memory location (argmem, inaccessiblemem) "
2312 "or access kind (none, read, write, readwrite)");
2313 else
2314 tokError("expected access kind (none, read, write, readwrite)");
2315 return std::nullopt;
2316 }
2317
2318 Lex.Lex();
2319 if (Loc) {
2320 SeenLoc = true;
2321 ME = ME.getWithModRef(*Loc, *MR);
2322 } else {
2323 if (SeenLoc) {
2324 tokError("default access kind must be specified first");
2325 return std::nullopt;
2326 }
2327 ME = MemoryEffects(*MR);
2328 }
2329
2330 if (EatIfPresent(lltok::rparen))
2331 return ME;
2332 } while (EatIfPresent(lltok::comma));
2333
2334 tokError("unterminated memory attribute");
2335 return std::nullopt;
2336 }
2337
2338 /// parseOptionalCommaAlign
2339 /// ::=
2340 /// ::= ',' align 4
2341 ///
2342 /// This returns with AteExtraComma set to true if it ate an excess comma at the
2343 /// end.
parseOptionalCommaAlign(MaybeAlign & Alignment,bool & AteExtraComma)2344 bool LLParser::parseOptionalCommaAlign(MaybeAlign &Alignment,
2345 bool &AteExtraComma) {
2346 AteExtraComma = false;
2347 while (EatIfPresent(lltok::comma)) {
2348 // Metadata at the end is an early exit.
2349 if (Lex.getKind() == lltok::MetadataVar) {
2350 AteExtraComma = true;
2351 return false;
2352 }
2353
2354 if (Lex.getKind() != lltok::kw_align)
2355 return error(Lex.getLoc(), "expected metadata or 'align'");
2356
2357 if (parseOptionalAlignment(Alignment))
2358 return true;
2359 }
2360
2361 return false;
2362 }
2363
2364 /// parseOptionalCommaAddrSpace
2365 /// ::=
2366 /// ::= ',' addrspace(1)
2367 ///
2368 /// This returns with AteExtraComma set to true if it ate an excess comma at the
2369 /// end.
parseOptionalCommaAddrSpace(unsigned & AddrSpace,LocTy & Loc,bool & AteExtraComma)2370 bool LLParser::parseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc,
2371 bool &AteExtraComma) {
2372 AteExtraComma = false;
2373 while (EatIfPresent(lltok::comma)) {
2374 // Metadata at the end is an early exit.
2375 if (Lex.getKind() == lltok::MetadataVar) {
2376 AteExtraComma = true;
2377 return false;
2378 }
2379
2380 Loc = Lex.getLoc();
2381 if (Lex.getKind() != lltok::kw_addrspace)
2382 return error(Lex.getLoc(), "expected metadata or 'addrspace'");
2383
2384 if (parseOptionalAddrSpace(AddrSpace))
2385 return true;
2386 }
2387
2388 return false;
2389 }
2390
parseAllocSizeArguments(unsigned & BaseSizeArg,std::optional<unsigned> & HowManyArg)2391 bool LLParser::parseAllocSizeArguments(unsigned &BaseSizeArg,
2392 std::optional<unsigned> &HowManyArg) {
2393 Lex.Lex();
2394
2395 auto StartParen = Lex.getLoc();
2396 if (!EatIfPresent(lltok::lparen))
2397 return error(StartParen, "expected '('");
2398
2399 if (parseUInt32(BaseSizeArg))
2400 return true;
2401
2402 if (EatIfPresent(lltok::comma)) {
2403 auto HowManyAt = Lex.getLoc();
2404 unsigned HowMany;
2405 if (parseUInt32(HowMany))
2406 return true;
2407 if (HowMany == BaseSizeArg)
2408 return error(HowManyAt,
2409 "'allocsize' indices can't refer to the same parameter");
2410 HowManyArg = HowMany;
2411 } else
2412 HowManyArg = std::nullopt;
2413
2414 auto EndParen = Lex.getLoc();
2415 if (!EatIfPresent(lltok::rparen))
2416 return error(EndParen, "expected ')'");
2417 return false;
2418 }
2419
parseVScaleRangeArguments(unsigned & MinValue,unsigned & MaxValue)2420 bool LLParser::parseVScaleRangeArguments(unsigned &MinValue,
2421 unsigned &MaxValue) {
2422 Lex.Lex();
2423
2424 auto StartParen = Lex.getLoc();
2425 if (!EatIfPresent(lltok::lparen))
2426 return error(StartParen, "expected '('");
2427
2428 if (parseUInt32(MinValue))
2429 return true;
2430
2431 if (EatIfPresent(lltok::comma)) {
2432 if (parseUInt32(MaxValue))
2433 return true;
2434 } else
2435 MaxValue = MinValue;
2436
2437 auto EndParen = Lex.getLoc();
2438 if (!EatIfPresent(lltok::rparen))
2439 return error(EndParen, "expected ')'");
2440 return false;
2441 }
2442
2443 /// parseScopeAndOrdering
2444 /// if isAtomic: ::= SyncScope? AtomicOrdering
2445 /// else: ::=
2446 ///
2447 /// This sets Scope and Ordering to the parsed values.
parseScopeAndOrdering(bool IsAtomic,SyncScope::ID & SSID,AtomicOrdering & Ordering)2448 bool LLParser::parseScopeAndOrdering(bool IsAtomic, SyncScope::ID &SSID,
2449 AtomicOrdering &Ordering) {
2450 if (!IsAtomic)
2451 return false;
2452
2453 return parseScope(SSID) || parseOrdering(Ordering);
2454 }
2455
2456 /// parseScope
2457 /// ::= syncscope("singlethread" | "<target scope>")?
2458 ///
2459 /// This sets synchronization scope ID to the ID of the parsed value.
parseScope(SyncScope::ID & SSID)2460 bool LLParser::parseScope(SyncScope::ID &SSID) {
2461 SSID = SyncScope::System;
2462 if (EatIfPresent(lltok::kw_syncscope)) {
2463 auto StartParenAt = Lex.getLoc();
2464 if (!EatIfPresent(lltok::lparen))
2465 return error(StartParenAt, "Expected '(' in syncscope");
2466
2467 std::string SSN;
2468 auto SSNAt = Lex.getLoc();
2469 if (parseStringConstant(SSN))
2470 return error(SSNAt, "Expected synchronization scope name");
2471
2472 auto EndParenAt = Lex.getLoc();
2473 if (!EatIfPresent(lltok::rparen))
2474 return error(EndParenAt, "Expected ')' in syncscope");
2475
2476 SSID = Context.getOrInsertSyncScopeID(SSN);
2477 }
2478
2479 return false;
2480 }
2481
2482 /// parseOrdering
2483 /// ::= AtomicOrdering
2484 ///
2485 /// This sets Ordering to the parsed value.
parseOrdering(AtomicOrdering & Ordering)2486 bool LLParser::parseOrdering(AtomicOrdering &Ordering) {
2487 switch (Lex.getKind()) {
2488 default:
2489 return tokError("Expected ordering on atomic instruction");
2490 case lltok::kw_unordered: Ordering = AtomicOrdering::Unordered; break;
2491 case lltok::kw_monotonic: Ordering = AtomicOrdering::Monotonic; break;
2492 // Not specified yet:
2493 // case lltok::kw_consume: Ordering = AtomicOrdering::Consume; break;
2494 case lltok::kw_acquire: Ordering = AtomicOrdering::Acquire; break;
2495 case lltok::kw_release: Ordering = AtomicOrdering::Release; break;
2496 case lltok::kw_acq_rel: Ordering = AtomicOrdering::AcquireRelease; break;
2497 case lltok::kw_seq_cst:
2498 Ordering = AtomicOrdering::SequentiallyConsistent;
2499 break;
2500 }
2501 Lex.Lex();
2502 return false;
2503 }
2504
2505 /// parseOptionalStackAlignment
2506 /// ::= /* empty */
2507 /// ::= 'alignstack' '(' 4 ')'
parseOptionalStackAlignment(unsigned & Alignment)2508 bool LLParser::parseOptionalStackAlignment(unsigned &Alignment) {
2509 Alignment = 0;
2510 if (!EatIfPresent(lltok::kw_alignstack))
2511 return false;
2512 LocTy ParenLoc = Lex.getLoc();
2513 if (!EatIfPresent(lltok::lparen))
2514 return error(ParenLoc, "expected '('");
2515 LocTy AlignLoc = Lex.getLoc();
2516 if (parseUInt32(Alignment))
2517 return true;
2518 ParenLoc = Lex.getLoc();
2519 if (!EatIfPresent(lltok::rparen))
2520 return error(ParenLoc, "expected ')'");
2521 if (!isPowerOf2_32(Alignment))
2522 return error(AlignLoc, "stack alignment is not a power of two");
2523 return false;
2524 }
2525
2526 /// parseIndexList - This parses the index list for an insert/extractvalue
2527 /// instruction. This sets AteExtraComma in the case where we eat an extra
2528 /// comma at the end of the line and find that it is followed by metadata.
2529 /// Clients that don't allow metadata can call the version of this function that
2530 /// only takes one argument.
2531 ///
2532 /// parseIndexList
2533 /// ::= (',' uint32)+
2534 ///
parseIndexList(SmallVectorImpl<unsigned> & Indices,bool & AteExtraComma)2535 bool LLParser::parseIndexList(SmallVectorImpl<unsigned> &Indices,
2536 bool &AteExtraComma) {
2537 AteExtraComma = false;
2538
2539 if (Lex.getKind() != lltok::comma)
2540 return tokError("expected ',' as start of index list");
2541
2542 while (EatIfPresent(lltok::comma)) {
2543 if (Lex.getKind() == lltok::MetadataVar) {
2544 if (Indices.empty())
2545 return tokError("expected index");
2546 AteExtraComma = true;
2547 return false;
2548 }
2549 unsigned Idx = 0;
2550 if (parseUInt32(Idx))
2551 return true;
2552 Indices.push_back(Idx);
2553 }
2554
2555 return false;
2556 }
2557
2558 //===----------------------------------------------------------------------===//
2559 // Type Parsing.
2560 //===----------------------------------------------------------------------===//
2561
2562 /// parseType - parse a type.
parseType(Type * & Result,const Twine & Msg,bool AllowVoid)2563 bool LLParser::parseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
2564 SMLoc TypeLoc = Lex.getLoc();
2565 switch (Lex.getKind()) {
2566 default:
2567 return tokError(Msg);
2568 case lltok::Type:
2569 // Type ::= 'float' | 'void' (etc)
2570 Result = Lex.getTyVal();
2571 Lex.Lex();
2572
2573 // Handle "ptr" opaque pointer type.
2574 //
2575 // Type ::= ptr ('addrspace' '(' uint32 ')')?
2576 if (Result->isOpaquePointerTy()) {
2577 unsigned AddrSpace;
2578 if (parseOptionalAddrSpace(AddrSpace))
2579 return true;
2580 Result = PointerType::get(getContext(), AddrSpace);
2581
2582 // Give a nice error for 'ptr*'.
2583 if (Lex.getKind() == lltok::star)
2584 return tokError("ptr* is invalid - use ptr instead");
2585
2586 // Fall through to parsing the type suffixes only if this 'ptr' is a
2587 // function return. Otherwise, return success, implicitly rejecting other
2588 // suffixes.
2589 if (Lex.getKind() != lltok::lparen)
2590 return false;
2591 }
2592 break;
2593 case lltok::kw_target: {
2594 // Type ::= TargetExtType
2595 if (parseTargetExtType(Result))
2596 return true;
2597 break;
2598 }
2599 case lltok::lbrace:
2600 // Type ::= StructType
2601 if (parseAnonStructType(Result, false))
2602 return true;
2603 break;
2604 case lltok::lsquare:
2605 // Type ::= '[' ... ']'
2606 Lex.Lex(); // eat the lsquare.
2607 if (parseArrayVectorType(Result, false))
2608 return true;
2609 break;
2610 case lltok::less: // Either vector or packed struct.
2611 // Type ::= '<' ... '>'
2612 Lex.Lex();
2613 if (Lex.getKind() == lltok::lbrace) {
2614 if (parseAnonStructType(Result, true) ||
2615 parseToken(lltok::greater, "expected '>' at end of packed struct"))
2616 return true;
2617 } else if (parseArrayVectorType(Result, true))
2618 return true;
2619 break;
2620 case lltok::LocalVar: {
2621 // Type ::= %foo
2622 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
2623
2624 // If the type hasn't been defined yet, create a forward definition and
2625 // remember where that forward def'n was seen (in case it never is defined).
2626 if (!Entry.first) {
2627 Entry.first = StructType::create(Context, Lex.getStrVal());
2628 Entry.second = Lex.getLoc();
2629 }
2630 Result = Entry.first;
2631 Lex.Lex();
2632 break;
2633 }
2634
2635 case lltok::LocalVarID: {
2636 // Type ::= %4
2637 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
2638
2639 // If the type hasn't been defined yet, create a forward definition and
2640 // remember where that forward def'n was seen (in case it never is defined).
2641 if (!Entry.first) {
2642 Entry.first = StructType::create(Context);
2643 Entry.second = Lex.getLoc();
2644 }
2645 Result = Entry.first;
2646 Lex.Lex();
2647 break;
2648 }
2649 }
2650
2651 // parse the type suffixes.
2652 while (true) {
2653 switch (Lex.getKind()) {
2654 // End of type.
2655 default:
2656 if (!AllowVoid && Result->isVoidTy())
2657 return error(TypeLoc, "void type only allowed for function results");
2658 return false;
2659
2660 // Type ::= Type '*'
2661 case lltok::star:
2662 if (Result->isLabelTy())
2663 return tokError("basic block pointers are invalid");
2664 if (Result->isVoidTy())
2665 return tokError("pointers to void are invalid - use i8* instead");
2666 if (!PointerType::isValidElementType(Result))
2667 return tokError("pointer to this type is invalid");
2668 Result = PointerType::getUnqual(Result);
2669 Lex.Lex();
2670 break;
2671
2672 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
2673 case lltok::kw_addrspace: {
2674 if (Result->isLabelTy())
2675 return tokError("basic block pointers are invalid");
2676 if (Result->isVoidTy())
2677 return tokError("pointers to void are invalid; use i8* instead");
2678 if (!PointerType::isValidElementType(Result))
2679 return tokError("pointer to this type is invalid");
2680 unsigned AddrSpace;
2681 if (parseOptionalAddrSpace(AddrSpace) ||
2682 parseToken(lltok::star, "expected '*' in address space"))
2683 return true;
2684
2685 Result = PointerType::get(Result, AddrSpace);
2686 break;
2687 }
2688
2689 /// Types '(' ArgTypeListI ')' OptFuncAttrs
2690 case lltok::lparen:
2691 if (parseFunctionType(Result))
2692 return true;
2693 break;
2694 }
2695 }
2696 }
2697
2698 /// parseParameterList
2699 /// ::= '(' ')'
2700 /// ::= '(' Arg (',' Arg)* ')'
2701 /// Arg
2702 /// ::= Type OptionalAttributes Value OptionalAttributes
parseParameterList(SmallVectorImpl<ParamInfo> & ArgList,PerFunctionState & PFS,bool IsMustTailCall,bool InVarArgsFunc)2703 bool LLParser::parseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
2704 PerFunctionState &PFS, bool IsMustTailCall,
2705 bool InVarArgsFunc) {
2706 if (parseToken(lltok::lparen, "expected '(' in call"))
2707 return true;
2708
2709 while (Lex.getKind() != lltok::rparen) {
2710 // If this isn't the first argument, we need a comma.
2711 if (!ArgList.empty() &&
2712 parseToken(lltok::comma, "expected ',' in argument list"))
2713 return true;
2714
2715 // parse an ellipsis if this is a musttail call in a variadic function.
2716 if (Lex.getKind() == lltok::dotdotdot) {
2717 const char *Msg = "unexpected ellipsis in argument list for ";
2718 if (!IsMustTailCall)
2719 return tokError(Twine(Msg) + "non-musttail call");
2720 if (!InVarArgsFunc)
2721 return tokError(Twine(Msg) + "musttail call in non-varargs function");
2722 Lex.Lex(); // Lex the '...', it is purely for readability.
2723 return parseToken(lltok::rparen, "expected ')' at end of argument list");
2724 }
2725
2726 // parse the argument.
2727 LocTy ArgLoc;
2728 Type *ArgTy = nullptr;
2729 Value *V;
2730 if (parseType(ArgTy, ArgLoc))
2731 return true;
2732
2733 AttrBuilder ArgAttrs(M->getContext());
2734
2735 if (ArgTy->isMetadataTy()) {
2736 if (parseMetadataAsValue(V, PFS))
2737 return true;
2738 } else {
2739 // Otherwise, handle normal operands.
2740 if (parseOptionalParamAttrs(ArgAttrs) || parseValue(ArgTy, V, PFS))
2741 return true;
2742 }
2743 ArgList.push_back(ParamInfo(
2744 ArgLoc, V, AttributeSet::get(V->getContext(), ArgAttrs)));
2745 }
2746
2747 if (IsMustTailCall && InVarArgsFunc)
2748 return tokError("expected '...' at end of argument list for musttail call "
2749 "in varargs function");
2750
2751 Lex.Lex(); // Lex the ')'.
2752 return false;
2753 }
2754
2755 /// parseRequiredTypeAttr
2756 /// ::= attrname(<ty>)
parseRequiredTypeAttr(AttrBuilder & B,lltok::Kind AttrToken,Attribute::AttrKind AttrKind)2757 bool LLParser::parseRequiredTypeAttr(AttrBuilder &B, lltok::Kind AttrToken,
2758 Attribute::AttrKind AttrKind) {
2759 Type *Ty = nullptr;
2760 if (!EatIfPresent(AttrToken))
2761 return true;
2762 if (!EatIfPresent(lltok::lparen))
2763 return error(Lex.getLoc(), "expected '('");
2764 if (parseType(Ty))
2765 return true;
2766 if (!EatIfPresent(lltok::rparen))
2767 return error(Lex.getLoc(), "expected ')'");
2768
2769 B.addTypeAttr(AttrKind, Ty);
2770 return false;
2771 }
2772
2773 /// parseOptionalOperandBundles
2774 /// ::= /*empty*/
2775 /// ::= '[' OperandBundle [, OperandBundle ]* ']'
2776 ///
2777 /// OperandBundle
2778 /// ::= bundle-tag '(' ')'
2779 /// ::= bundle-tag '(' Type Value [, Type Value ]* ')'
2780 ///
2781 /// bundle-tag ::= String Constant
parseOptionalOperandBundles(SmallVectorImpl<OperandBundleDef> & BundleList,PerFunctionState & PFS)2782 bool LLParser::parseOptionalOperandBundles(
2783 SmallVectorImpl<OperandBundleDef> &BundleList, PerFunctionState &PFS) {
2784 LocTy BeginLoc = Lex.getLoc();
2785 if (!EatIfPresent(lltok::lsquare))
2786 return false;
2787
2788 while (Lex.getKind() != lltok::rsquare) {
2789 // If this isn't the first operand bundle, we need a comma.
2790 if (!BundleList.empty() &&
2791 parseToken(lltok::comma, "expected ',' in input list"))
2792 return true;
2793
2794 std::string Tag;
2795 if (parseStringConstant(Tag))
2796 return true;
2797
2798 if (parseToken(lltok::lparen, "expected '(' in operand bundle"))
2799 return true;
2800
2801 std::vector<Value *> Inputs;
2802 while (Lex.getKind() != lltok::rparen) {
2803 // If this isn't the first input, we need a comma.
2804 if (!Inputs.empty() &&
2805 parseToken(lltok::comma, "expected ',' in input list"))
2806 return true;
2807
2808 Type *Ty = nullptr;
2809 Value *Input = nullptr;
2810 if (parseType(Ty) || parseValue(Ty, Input, PFS))
2811 return true;
2812 Inputs.push_back(Input);
2813 }
2814
2815 BundleList.emplace_back(std::move(Tag), std::move(Inputs));
2816
2817 Lex.Lex(); // Lex the ')'.
2818 }
2819
2820 if (BundleList.empty())
2821 return error(BeginLoc, "operand bundle set must not be empty");
2822
2823 Lex.Lex(); // Lex the ']'.
2824 return false;
2825 }
2826
2827 /// parseArgumentList - parse the argument list for a function type or function
2828 /// prototype.
2829 /// ::= '(' ArgTypeListI ')'
2830 /// ArgTypeListI
2831 /// ::= /*empty*/
2832 /// ::= '...'
2833 /// ::= ArgTypeList ',' '...'
2834 /// ::= ArgType (',' ArgType)*
2835 ///
parseArgumentList(SmallVectorImpl<ArgInfo> & ArgList,bool & IsVarArg)2836 bool LLParser::parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
2837 bool &IsVarArg) {
2838 unsigned CurValID = 0;
2839 IsVarArg = false;
2840 assert(Lex.getKind() == lltok::lparen);
2841 Lex.Lex(); // eat the (.
2842
2843 if (Lex.getKind() == lltok::rparen) {
2844 // empty
2845 } else if (Lex.getKind() == lltok::dotdotdot) {
2846 IsVarArg = true;
2847 Lex.Lex();
2848 } else {
2849 LocTy TypeLoc = Lex.getLoc();
2850 Type *ArgTy = nullptr;
2851 AttrBuilder Attrs(M->getContext());
2852 std::string Name;
2853
2854 if (parseType(ArgTy) || parseOptionalParamAttrs(Attrs))
2855 return true;
2856
2857 if (ArgTy->isVoidTy())
2858 return error(TypeLoc, "argument can not have void type");
2859
2860 if (Lex.getKind() == lltok::LocalVar) {
2861 Name = Lex.getStrVal();
2862 Lex.Lex();
2863 } else if (Lex.getKind() == lltok::LocalVarID) {
2864 if (Lex.getUIntVal() != CurValID)
2865 return error(TypeLoc, "argument expected to be numbered '%" +
2866 Twine(CurValID) + "'");
2867 ++CurValID;
2868 Lex.Lex();
2869 }
2870
2871 if (!FunctionType::isValidArgumentType(ArgTy))
2872 return error(TypeLoc, "invalid type for function argument");
2873
2874 ArgList.emplace_back(TypeLoc, ArgTy,
2875 AttributeSet::get(ArgTy->getContext(), Attrs),
2876 std::move(Name));
2877
2878 while (EatIfPresent(lltok::comma)) {
2879 // Handle ... at end of arg list.
2880 if (EatIfPresent(lltok::dotdotdot)) {
2881 IsVarArg = true;
2882 break;
2883 }
2884
2885 // Otherwise must be an argument type.
2886 TypeLoc = Lex.getLoc();
2887 if (parseType(ArgTy) || parseOptionalParamAttrs(Attrs))
2888 return true;
2889
2890 if (ArgTy->isVoidTy())
2891 return error(TypeLoc, "argument can not have void type");
2892
2893 if (Lex.getKind() == lltok::LocalVar) {
2894 Name = Lex.getStrVal();
2895 Lex.Lex();
2896 } else {
2897 if (Lex.getKind() == lltok::LocalVarID) {
2898 if (Lex.getUIntVal() != CurValID)
2899 return error(TypeLoc, "argument expected to be numbered '%" +
2900 Twine(CurValID) + "'");
2901 Lex.Lex();
2902 }
2903 ++CurValID;
2904 Name = "";
2905 }
2906
2907 if (!ArgTy->isFirstClassType())
2908 return error(TypeLoc, "invalid type for function argument");
2909
2910 ArgList.emplace_back(TypeLoc, ArgTy,
2911 AttributeSet::get(ArgTy->getContext(), Attrs),
2912 std::move(Name));
2913 }
2914 }
2915
2916 return parseToken(lltok::rparen, "expected ')' at end of argument list");
2917 }
2918
2919 /// parseFunctionType
2920 /// ::= Type ArgumentList OptionalAttrs
parseFunctionType(Type * & Result)2921 bool LLParser::parseFunctionType(Type *&Result) {
2922 assert(Lex.getKind() == lltok::lparen);
2923
2924 if (!FunctionType::isValidReturnType(Result))
2925 return tokError("invalid function return type");
2926
2927 SmallVector<ArgInfo, 8> ArgList;
2928 bool IsVarArg;
2929 if (parseArgumentList(ArgList, IsVarArg))
2930 return true;
2931
2932 // Reject names on the arguments lists.
2933 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
2934 if (!ArgList[i].Name.empty())
2935 return error(ArgList[i].Loc, "argument name invalid in function type");
2936 if (ArgList[i].Attrs.hasAttributes())
2937 return error(ArgList[i].Loc,
2938 "argument attributes invalid in function type");
2939 }
2940
2941 SmallVector<Type*, 16> ArgListTy;
2942 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
2943 ArgListTy.push_back(ArgList[i].Ty);
2944
2945 Result = FunctionType::get(Result, ArgListTy, IsVarArg);
2946 return false;
2947 }
2948
2949 /// parseAnonStructType - parse an anonymous struct type, which is inlined into
2950 /// other structs.
parseAnonStructType(Type * & Result,bool Packed)2951 bool LLParser::parseAnonStructType(Type *&Result, bool Packed) {
2952 SmallVector<Type*, 8> Elts;
2953 if (parseStructBody(Elts))
2954 return true;
2955
2956 Result = StructType::get(Context, Elts, Packed);
2957 return false;
2958 }
2959
2960 /// parseStructDefinition - parse a struct in a 'type' definition.
parseStructDefinition(SMLoc TypeLoc,StringRef Name,std::pair<Type *,LocTy> & Entry,Type * & ResultTy)2961 bool LLParser::parseStructDefinition(SMLoc TypeLoc, StringRef Name,
2962 std::pair<Type *, LocTy> &Entry,
2963 Type *&ResultTy) {
2964 // If the type was already defined, diagnose the redefinition.
2965 if (Entry.first && !Entry.second.isValid())
2966 return error(TypeLoc, "redefinition of type");
2967
2968 // If we have opaque, just return without filling in the definition for the
2969 // struct. This counts as a definition as far as the .ll file goes.
2970 if (EatIfPresent(lltok::kw_opaque)) {
2971 // This type is being defined, so clear the location to indicate this.
2972 Entry.second = SMLoc();
2973
2974 // If this type number has never been uttered, create it.
2975 if (!Entry.first)
2976 Entry.first = StructType::create(Context, Name);
2977 ResultTy = Entry.first;
2978 return false;
2979 }
2980
2981 // If the type starts with '<', then it is either a packed struct or a vector.
2982 bool isPacked = EatIfPresent(lltok::less);
2983
2984 // If we don't have a struct, then we have a random type alias, which we
2985 // accept for compatibility with old files. These types are not allowed to be
2986 // forward referenced and not allowed to be recursive.
2987 if (Lex.getKind() != lltok::lbrace) {
2988 if (Entry.first)
2989 return error(TypeLoc, "forward references to non-struct type");
2990
2991 ResultTy = nullptr;
2992 if (isPacked)
2993 return parseArrayVectorType(ResultTy, true);
2994 return parseType(ResultTy);
2995 }
2996
2997 // This type is being defined, so clear the location to indicate this.
2998 Entry.second = SMLoc();
2999
3000 // If this type number has never been uttered, create it.
3001 if (!Entry.first)
3002 Entry.first = StructType::create(Context, Name);
3003
3004 StructType *STy = cast<StructType>(Entry.first);
3005
3006 SmallVector<Type*, 8> Body;
3007 if (parseStructBody(Body) ||
3008 (isPacked && parseToken(lltok::greater, "expected '>' in packed struct")))
3009 return true;
3010
3011 STy->setBody(Body, isPacked);
3012 ResultTy = STy;
3013 return false;
3014 }
3015
3016 /// parseStructType: Handles packed and unpacked types. </> parsed elsewhere.
3017 /// StructType
3018 /// ::= '{' '}'
3019 /// ::= '{' Type (',' Type)* '}'
3020 /// ::= '<' '{' '}' '>'
3021 /// ::= '<' '{' Type (',' Type)* '}' '>'
parseStructBody(SmallVectorImpl<Type * > & Body)3022 bool LLParser::parseStructBody(SmallVectorImpl<Type *> &Body) {
3023 assert(Lex.getKind() == lltok::lbrace);
3024 Lex.Lex(); // Consume the '{'
3025
3026 // Handle the empty struct.
3027 if (EatIfPresent(lltok::rbrace))
3028 return false;
3029
3030 LocTy EltTyLoc = Lex.getLoc();
3031 Type *Ty = nullptr;
3032 if (parseType(Ty))
3033 return true;
3034 Body.push_back(Ty);
3035
3036 if (!StructType::isValidElementType(Ty))
3037 return error(EltTyLoc, "invalid element type for struct");
3038
3039 while (EatIfPresent(lltok::comma)) {
3040 EltTyLoc = Lex.getLoc();
3041 if (parseType(Ty))
3042 return true;
3043
3044 if (!StructType::isValidElementType(Ty))
3045 return error(EltTyLoc, "invalid element type for struct");
3046
3047 Body.push_back(Ty);
3048 }
3049
3050 return parseToken(lltok::rbrace, "expected '}' at end of struct");
3051 }
3052
3053 /// parseArrayVectorType - parse an array or vector type, assuming the first
3054 /// token has already been consumed.
3055 /// Type
3056 /// ::= '[' APSINTVAL 'x' Types ']'
3057 /// ::= '<' APSINTVAL 'x' Types '>'
3058 /// ::= '<' 'vscale' 'x' APSINTVAL 'x' Types '>'
parseArrayVectorType(Type * & Result,bool IsVector)3059 bool LLParser::parseArrayVectorType(Type *&Result, bool IsVector) {
3060 bool Scalable = false;
3061
3062 if (IsVector && Lex.getKind() == lltok::kw_vscale) {
3063 Lex.Lex(); // consume the 'vscale'
3064 if (parseToken(lltok::kw_x, "expected 'x' after vscale"))
3065 return true;
3066
3067 Scalable = true;
3068 }
3069
3070 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
3071 Lex.getAPSIntVal().getBitWidth() > 64)
3072 return tokError("expected number in address space");
3073
3074 LocTy SizeLoc = Lex.getLoc();
3075 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
3076 Lex.Lex();
3077
3078 if (parseToken(lltok::kw_x, "expected 'x' after element count"))
3079 return true;
3080
3081 LocTy TypeLoc = Lex.getLoc();
3082 Type *EltTy = nullptr;
3083 if (parseType(EltTy))
3084 return true;
3085
3086 if (parseToken(IsVector ? lltok::greater : lltok::rsquare,
3087 "expected end of sequential type"))
3088 return true;
3089
3090 if (IsVector) {
3091 if (Size == 0)
3092 return error(SizeLoc, "zero element vector is illegal");
3093 if ((unsigned)Size != Size)
3094 return error(SizeLoc, "size too large for vector");
3095 if (!VectorType::isValidElementType(EltTy))
3096 return error(TypeLoc, "invalid vector element type");
3097 Result = VectorType::get(EltTy, unsigned(Size), Scalable);
3098 } else {
3099 if (!ArrayType::isValidElementType(EltTy))
3100 return error(TypeLoc, "invalid array element type");
3101 Result = ArrayType::get(EltTy, Size);
3102 }
3103 return false;
3104 }
3105
3106 /// parseTargetExtType - handle target extension type syntax
3107 /// TargetExtType
3108 /// ::= 'target' '(' STRINGCONSTANT TargetExtTypeParams TargetExtIntParams ')'
3109 ///
3110 /// TargetExtTypeParams
3111 /// ::= /*empty*/
3112 /// ::= ',' Type TargetExtTypeParams
3113 ///
3114 /// TargetExtIntParams
3115 /// ::= /*empty*/
3116 /// ::= ',' uint32 TargetExtIntParams
parseTargetExtType(Type * & Result)3117 bool LLParser::parseTargetExtType(Type *&Result) {
3118 Lex.Lex(); // Eat the 'target' keyword.
3119
3120 // Get the mandatory type name.
3121 std::string TypeName;
3122 if (parseToken(lltok::lparen, "expected '(' in target extension type") ||
3123 parseStringConstant(TypeName))
3124 return true;
3125
3126 // Parse all of the integer and type parameters at the same time; the use of
3127 // SeenInt will allow us to catch cases where type parameters follow integer
3128 // parameters.
3129 SmallVector<Type *> TypeParams;
3130 SmallVector<unsigned> IntParams;
3131 bool SeenInt = false;
3132 while (Lex.getKind() == lltok::comma) {
3133 Lex.Lex(); // Eat the comma.
3134
3135 if (Lex.getKind() == lltok::APSInt) {
3136 SeenInt = true;
3137 unsigned IntVal;
3138 if (parseUInt32(IntVal))
3139 return true;
3140 IntParams.push_back(IntVal);
3141 } else if (SeenInt) {
3142 // The only other kind of parameter we support is type parameters, which
3143 // must precede the integer parameters. This is therefore an error.
3144 return tokError("expected uint32 param");
3145 } else {
3146 Type *TypeParam;
3147 if (parseType(TypeParam, /*AllowVoid=*/true))
3148 return true;
3149 TypeParams.push_back(TypeParam);
3150 }
3151 }
3152
3153 if (parseToken(lltok::rparen, "expected ')' in target extension type"))
3154 return true;
3155
3156 Result = TargetExtType::get(Context, TypeName, TypeParams, IntParams);
3157 return false;
3158 }
3159
3160 //===----------------------------------------------------------------------===//
3161 // Function Semantic Analysis.
3162 //===----------------------------------------------------------------------===//
3163
PerFunctionState(LLParser & p,Function & f,int functionNumber)3164 LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
3165 int functionNumber)
3166 : P(p), F(f), FunctionNumber(functionNumber) {
3167
3168 // Insert unnamed arguments into the NumberedVals list.
3169 for (Argument &A : F.args())
3170 if (!A.hasName())
3171 NumberedVals.push_back(&A);
3172 }
3173
~PerFunctionState()3174 LLParser::PerFunctionState::~PerFunctionState() {
3175 // If there were any forward referenced non-basicblock values, delete them.
3176
3177 for (const auto &P : ForwardRefVals) {
3178 if (isa<BasicBlock>(P.second.first))
3179 continue;
3180 P.second.first->replaceAllUsesWith(
3181 UndefValue::get(P.second.first->getType()));
3182 P.second.first->deleteValue();
3183 }
3184
3185 for (const auto &P : ForwardRefValIDs) {
3186 if (isa<BasicBlock>(P.second.first))
3187 continue;
3188 P.second.first->replaceAllUsesWith(
3189 UndefValue::get(P.second.first->getType()));
3190 P.second.first->deleteValue();
3191 }
3192 }
3193
finishFunction()3194 bool LLParser::PerFunctionState::finishFunction() {
3195 if (!ForwardRefVals.empty())
3196 return P.error(ForwardRefVals.begin()->second.second,
3197 "use of undefined value '%" + ForwardRefVals.begin()->first +
3198 "'");
3199 if (!ForwardRefValIDs.empty())
3200 return P.error(ForwardRefValIDs.begin()->second.second,
3201 "use of undefined value '%" +
3202 Twine(ForwardRefValIDs.begin()->first) + "'");
3203 return false;
3204 }
3205
3206 /// getVal - Get a value with the specified name or ID, creating a
3207 /// forward reference record if needed. This can return null if the value
3208 /// exists but does not have the right type.
getVal(const std::string & Name,Type * Ty,LocTy Loc)3209 Value *LLParser::PerFunctionState::getVal(const std::string &Name, Type *Ty,
3210 LocTy Loc) {
3211 // Look this name up in the normal function symbol table.
3212 Value *Val = F.getValueSymbolTable()->lookup(Name);
3213
3214 // If this is a forward reference for the value, see if we already created a
3215 // forward ref record.
3216 if (!Val) {
3217 auto I = ForwardRefVals.find(Name);
3218 if (I != ForwardRefVals.end())
3219 Val = I->second.first;
3220 }
3221
3222 // If we have the value in the symbol table or fwd-ref table, return it.
3223 if (Val)
3224 return P.checkValidVariableType(Loc, "%" + Name, Ty, Val);
3225
3226 // Don't make placeholders with invalid type.
3227 if (!Ty->isFirstClassType()) {
3228 P.error(Loc, "invalid use of a non-first-class type");
3229 return nullptr;
3230 }
3231
3232 // Otherwise, create a new forward reference for this value and remember it.
3233 Value *FwdVal;
3234 if (Ty->isLabelTy()) {
3235 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
3236 } else {
3237 FwdVal = new Argument(Ty, Name);
3238 }
3239
3240 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
3241 return FwdVal;
3242 }
3243
getVal(unsigned ID,Type * Ty,LocTy Loc)3244 Value *LLParser::PerFunctionState::getVal(unsigned ID, Type *Ty, LocTy Loc) {
3245 // Look this name up in the normal function symbol table.
3246 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
3247
3248 // If this is a forward reference for the value, see if we already created a
3249 // forward ref record.
3250 if (!Val) {
3251 auto I = ForwardRefValIDs.find(ID);
3252 if (I != ForwardRefValIDs.end())
3253 Val = I->second.first;
3254 }
3255
3256 // If we have the value in the symbol table or fwd-ref table, return it.
3257 if (Val)
3258 return P.checkValidVariableType(Loc, "%" + Twine(ID), Ty, Val);
3259
3260 if (!Ty->isFirstClassType()) {
3261 P.error(Loc, "invalid use of a non-first-class type");
3262 return nullptr;
3263 }
3264
3265 // Otherwise, create a new forward reference for this value and remember it.
3266 Value *FwdVal;
3267 if (Ty->isLabelTy()) {
3268 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
3269 } else {
3270 FwdVal = new Argument(Ty);
3271 }
3272
3273 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
3274 return FwdVal;
3275 }
3276
3277 /// setInstName - After an instruction is parsed and inserted into its
3278 /// basic block, this installs its name.
setInstName(int NameID,const std::string & NameStr,LocTy NameLoc,Instruction * Inst)3279 bool LLParser::PerFunctionState::setInstName(int NameID,
3280 const std::string &NameStr,
3281 LocTy NameLoc, Instruction *Inst) {
3282 // If this instruction has void type, it cannot have a name or ID specified.
3283 if (Inst->getType()->isVoidTy()) {
3284 if (NameID != -1 || !NameStr.empty())
3285 return P.error(NameLoc, "instructions returning void cannot have a name");
3286 return false;
3287 }
3288
3289 // If this was a numbered instruction, verify that the instruction is the
3290 // expected value and resolve any forward references.
3291 if (NameStr.empty()) {
3292 // If neither a name nor an ID was specified, just use the next ID.
3293 if (NameID == -1)
3294 NameID = NumberedVals.size();
3295
3296 if (unsigned(NameID) != NumberedVals.size())
3297 return P.error(NameLoc, "instruction expected to be numbered '%" +
3298 Twine(NumberedVals.size()) + "'");
3299
3300 auto FI = ForwardRefValIDs.find(NameID);
3301 if (FI != ForwardRefValIDs.end()) {
3302 Value *Sentinel = FI->second.first;
3303 if (Sentinel->getType() != Inst->getType())
3304 return P.error(NameLoc, "instruction forward referenced with type '" +
3305 getTypeString(FI->second.first->getType()) +
3306 "'");
3307
3308 Sentinel->replaceAllUsesWith(Inst);
3309 Sentinel->deleteValue();
3310 ForwardRefValIDs.erase(FI);
3311 }
3312
3313 NumberedVals.push_back(Inst);
3314 return false;
3315 }
3316
3317 // Otherwise, the instruction had a name. Resolve forward refs and set it.
3318 auto FI = ForwardRefVals.find(NameStr);
3319 if (FI != ForwardRefVals.end()) {
3320 Value *Sentinel = FI->second.first;
3321 if (Sentinel->getType() != Inst->getType())
3322 return P.error(NameLoc, "instruction forward referenced with type '" +
3323 getTypeString(FI->second.first->getType()) +
3324 "'");
3325
3326 Sentinel->replaceAllUsesWith(Inst);
3327 Sentinel->deleteValue();
3328 ForwardRefVals.erase(FI);
3329 }
3330
3331 // Set the name on the instruction.
3332 Inst->setName(NameStr);
3333
3334 if (Inst->getName() != NameStr)
3335 return P.error(NameLoc, "multiple definition of local value named '" +
3336 NameStr + "'");
3337 return false;
3338 }
3339
3340 /// getBB - Get a basic block with the specified name or ID, creating a
3341 /// forward reference record if needed.
getBB(const std::string & Name,LocTy Loc)3342 BasicBlock *LLParser::PerFunctionState::getBB(const std::string &Name,
3343 LocTy Loc) {
3344 return dyn_cast_or_null<BasicBlock>(
3345 getVal(Name, Type::getLabelTy(F.getContext()), Loc));
3346 }
3347
getBB(unsigned ID,LocTy Loc)3348 BasicBlock *LLParser::PerFunctionState::getBB(unsigned ID, LocTy Loc) {
3349 return dyn_cast_or_null<BasicBlock>(
3350 getVal(ID, Type::getLabelTy(F.getContext()), Loc));
3351 }
3352
3353 /// defineBB - Define the specified basic block, which is either named or
3354 /// unnamed. If there is an error, this returns null otherwise it returns
3355 /// the block being defined.
defineBB(const std::string & Name,int NameID,LocTy Loc)3356 BasicBlock *LLParser::PerFunctionState::defineBB(const std::string &Name,
3357 int NameID, LocTy Loc) {
3358 BasicBlock *BB;
3359 if (Name.empty()) {
3360 if (NameID != -1 && unsigned(NameID) != NumberedVals.size()) {
3361 P.error(Loc, "label expected to be numbered '" +
3362 Twine(NumberedVals.size()) + "'");
3363 return nullptr;
3364 }
3365 BB = getBB(NumberedVals.size(), Loc);
3366 if (!BB) {
3367 P.error(Loc, "unable to create block numbered '" +
3368 Twine(NumberedVals.size()) + "'");
3369 return nullptr;
3370 }
3371 } else {
3372 BB = getBB(Name, Loc);
3373 if (!BB) {
3374 P.error(Loc, "unable to create block named '" + Name + "'");
3375 return nullptr;
3376 }
3377 }
3378
3379 // Move the block to the end of the function. Forward ref'd blocks are
3380 // inserted wherever they happen to be referenced.
3381 F.splice(F.end(), &F, BB->getIterator());
3382
3383 // Remove the block from forward ref sets.
3384 if (Name.empty()) {
3385 ForwardRefValIDs.erase(NumberedVals.size());
3386 NumberedVals.push_back(BB);
3387 } else {
3388 // BB forward references are already in the function symbol table.
3389 ForwardRefVals.erase(Name);
3390 }
3391
3392 return BB;
3393 }
3394
3395 //===----------------------------------------------------------------------===//
3396 // Constants.
3397 //===----------------------------------------------------------------------===//
3398
3399 /// parseValID - parse an abstract value that doesn't necessarily have a
3400 /// type implied. For example, if we parse "4" we don't know what integer type
3401 /// it has. The value will later be combined with its type and checked for
3402 /// basic correctness. PFS is used to convert function-local operands of
3403 /// metadata (since metadata operands are not just parsed here but also
3404 /// converted to values). PFS can be null when we are not parsing metadata
3405 /// values inside a function.
parseValID(ValID & ID,PerFunctionState * PFS,Type * ExpectedTy)3406 bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
3407 ID.Loc = Lex.getLoc();
3408 switch (Lex.getKind()) {
3409 default:
3410 return tokError("expected value token");
3411 case lltok::GlobalID: // @42
3412 ID.UIntVal = Lex.getUIntVal();
3413 ID.Kind = ValID::t_GlobalID;
3414 break;
3415 case lltok::GlobalVar: // @foo
3416 ID.StrVal = Lex.getStrVal();
3417 ID.Kind = ValID::t_GlobalName;
3418 break;
3419 case lltok::LocalVarID: // %42
3420 ID.UIntVal = Lex.getUIntVal();
3421 ID.Kind = ValID::t_LocalID;
3422 break;
3423 case lltok::LocalVar: // %foo
3424 ID.StrVal = Lex.getStrVal();
3425 ID.Kind = ValID::t_LocalName;
3426 break;
3427 case lltok::APSInt:
3428 ID.APSIntVal = Lex.getAPSIntVal();
3429 ID.Kind = ValID::t_APSInt;
3430 break;
3431 case lltok::APFloat:
3432 ID.APFloatVal = Lex.getAPFloatVal();
3433 ID.Kind = ValID::t_APFloat;
3434 break;
3435 case lltok::kw_true:
3436 ID.ConstantVal = ConstantInt::getTrue(Context);
3437 ID.Kind = ValID::t_Constant;
3438 break;
3439 case lltok::kw_false:
3440 ID.ConstantVal = ConstantInt::getFalse(Context);
3441 ID.Kind = ValID::t_Constant;
3442 break;
3443 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
3444 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
3445 case lltok::kw_poison: ID.Kind = ValID::t_Poison; break;
3446 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
3447 case lltok::kw_none: ID.Kind = ValID::t_None; break;
3448
3449 case lltok::lbrace: {
3450 // ValID ::= '{' ConstVector '}'
3451 Lex.Lex();
3452 SmallVector<Constant*, 16> Elts;
3453 if (parseGlobalValueVector(Elts) ||
3454 parseToken(lltok::rbrace, "expected end of struct constant"))
3455 return true;
3456
3457 ID.ConstantStructElts = std::make_unique<Constant *[]>(Elts.size());
3458 ID.UIntVal = Elts.size();
3459 memcpy(ID.ConstantStructElts.get(), Elts.data(),
3460 Elts.size() * sizeof(Elts[0]));
3461 ID.Kind = ValID::t_ConstantStruct;
3462 return false;
3463 }
3464 case lltok::less: {
3465 // ValID ::= '<' ConstVector '>' --> Vector.
3466 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
3467 Lex.Lex();
3468 bool isPackedStruct = EatIfPresent(lltok::lbrace);
3469
3470 SmallVector<Constant*, 16> Elts;
3471 LocTy FirstEltLoc = Lex.getLoc();
3472 if (parseGlobalValueVector(Elts) ||
3473 (isPackedStruct &&
3474 parseToken(lltok::rbrace, "expected end of packed struct")) ||
3475 parseToken(lltok::greater, "expected end of constant"))
3476 return true;
3477
3478 if (isPackedStruct) {
3479 ID.ConstantStructElts = std::make_unique<Constant *[]>(Elts.size());
3480 memcpy(ID.ConstantStructElts.get(), Elts.data(),
3481 Elts.size() * sizeof(Elts[0]));
3482 ID.UIntVal = Elts.size();
3483 ID.Kind = ValID::t_PackedConstantStruct;
3484 return false;
3485 }
3486
3487 if (Elts.empty())
3488 return error(ID.Loc, "constant vector must not be empty");
3489
3490 if (!Elts[0]->getType()->isIntegerTy() &&
3491 !Elts[0]->getType()->isFloatingPointTy() &&
3492 !Elts[0]->getType()->isPointerTy())
3493 return error(
3494 FirstEltLoc,
3495 "vector elements must have integer, pointer or floating point type");
3496
3497 // Verify that all the vector elements have the same type.
3498 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
3499 if (Elts[i]->getType() != Elts[0]->getType())
3500 return error(FirstEltLoc, "vector element #" + Twine(i) +
3501 " is not of type '" +
3502 getTypeString(Elts[0]->getType()));
3503
3504 ID.ConstantVal = ConstantVector::get(Elts);
3505 ID.Kind = ValID::t_Constant;
3506 return false;
3507 }
3508 case lltok::lsquare: { // Array Constant
3509 Lex.Lex();
3510 SmallVector<Constant*, 16> Elts;
3511 LocTy FirstEltLoc = Lex.getLoc();
3512 if (parseGlobalValueVector(Elts) ||
3513 parseToken(lltok::rsquare, "expected end of array constant"))
3514 return true;
3515
3516 // Handle empty element.
3517 if (Elts.empty()) {
3518 // Use undef instead of an array because it's inconvenient to determine
3519 // the element type at this point, there being no elements to examine.
3520 ID.Kind = ValID::t_EmptyArray;
3521 return false;
3522 }
3523
3524 if (!Elts[0]->getType()->isFirstClassType())
3525 return error(FirstEltLoc, "invalid array element type: " +
3526 getTypeString(Elts[0]->getType()));
3527
3528 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
3529
3530 // Verify all elements are correct type!
3531 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
3532 if (Elts[i]->getType() != Elts[0]->getType())
3533 return error(FirstEltLoc, "array element #" + Twine(i) +
3534 " is not of type '" +
3535 getTypeString(Elts[0]->getType()));
3536 }
3537
3538 ID.ConstantVal = ConstantArray::get(ATy, Elts);
3539 ID.Kind = ValID::t_Constant;
3540 return false;
3541 }
3542 case lltok::kw_c: // c "foo"
3543 Lex.Lex();
3544 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
3545 false);
3546 if (parseToken(lltok::StringConstant, "expected string"))
3547 return true;
3548 ID.Kind = ValID::t_Constant;
3549 return false;
3550
3551 case lltok::kw_asm: {
3552 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
3553 // STRINGCONSTANT
3554 bool HasSideEffect, AlignStack, AsmDialect, CanThrow;
3555 Lex.Lex();
3556 if (parseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
3557 parseOptionalToken(lltok::kw_alignstack, AlignStack) ||
3558 parseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
3559 parseOptionalToken(lltok::kw_unwind, CanThrow) ||
3560 parseStringConstant(ID.StrVal) ||
3561 parseToken(lltok::comma, "expected comma in inline asm expression") ||
3562 parseToken(lltok::StringConstant, "expected constraint string"))
3563 return true;
3564 ID.StrVal2 = Lex.getStrVal();
3565 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack) << 1) |
3566 (unsigned(AsmDialect) << 2) | (unsigned(CanThrow) << 3);
3567 ID.Kind = ValID::t_InlineAsm;
3568 return false;
3569 }
3570
3571 case lltok::kw_blockaddress: {
3572 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
3573 Lex.Lex();
3574
3575 ValID Fn, Label;
3576
3577 if (parseToken(lltok::lparen, "expected '(' in block address expression") ||
3578 parseValID(Fn, PFS) ||
3579 parseToken(lltok::comma,
3580 "expected comma in block address expression") ||
3581 parseValID(Label, PFS) ||
3582 parseToken(lltok::rparen, "expected ')' in block address expression"))
3583 return true;
3584
3585 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
3586 return error(Fn.Loc, "expected function name in blockaddress");
3587 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
3588 return error(Label.Loc, "expected basic block name in blockaddress");
3589
3590 // Try to find the function (but skip it if it's forward-referenced).
3591 GlobalValue *GV = nullptr;
3592 if (Fn.Kind == ValID::t_GlobalID) {
3593 if (Fn.UIntVal < NumberedVals.size())
3594 GV = NumberedVals[Fn.UIntVal];
3595 } else if (!ForwardRefVals.count(Fn.StrVal)) {
3596 GV = M->getNamedValue(Fn.StrVal);
3597 }
3598 Function *F = nullptr;
3599 if (GV) {
3600 // Confirm that it's actually a function with a definition.
3601 if (!isa<Function>(GV))
3602 return error(Fn.Loc, "expected function name in blockaddress");
3603 F = cast<Function>(GV);
3604 if (F->isDeclaration())
3605 return error(Fn.Loc, "cannot take blockaddress inside a declaration");
3606 }
3607
3608 if (!F) {
3609 // Make a global variable as a placeholder for this reference.
3610 GlobalValue *&FwdRef =
3611 ForwardRefBlockAddresses.insert(std::make_pair(
3612 std::move(Fn),
3613 std::map<ValID, GlobalValue *>()))
3614 .first->second.insert(std::make_pair(std::move(Label), nullptr))
3615 .first->second;
3616 if (!FwdRef) {
3617 unsigned FwdDeclAS;
3618 if (ExpectedTy) {
3619 // If we know the type that the blockaddress is being assigned to,
3620 // we can use the address space of that type.
3621 if (!ExpectedTy->isPointerTy())
3622 return error(ID.Loc,
3623 "type of blockaddress must be a pointer and not '" +
3624 getTypeString(ExpectedTy) + "'");
3625 FwdDeclAS = ExpectedTy->getPointerAddressSpace();
3626 } else if (PFS) {
3627 // Otherwise, we default the address space of the current function.
3628 FwdDeclAS = PFS->getFunction().getAddressSpace();
3629 } else {
3630 llvm_unreachable("Unknown address space for blockaddress");
3631 }
3632 FwdRef = new GlobalVariable(
3633 *M, Type::getInt8Ty(Context), false, GlobalValue::InternalLinkage,
3634 nullptr, "", nullptr, GlobalValue::NotThreadLocal, FwdDeclAS);
3635 }
3636
3637 ID.ConstantVal = FwdRef;
3638 ID.Kind = ValID::t_Constant;
3639 return false;
3640 }
3641
3642 // We found the function; now find the basic block. Don't use PFS, since we
3643 // might be inside a constant expression.
3644 BasicBlock *BB;
3645 if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) {
3646 if (Label.Kind == ValID::t_LocalID)
3647 BB = BlockAddressPFS->getBB(Label.UIntVal, Label.Loc);
3648 else
3649 BB = BlockAddressPFS->getBB(Label.StrVal, Label.Loc);
3650 if (!BB)
3651 return error(Label.Loc, "referenced value is not a basic block");
3652 } else {
3653 if (Label.Kind == ValID::t_LocalID)
3654 return error(Label.Loc, "cannot take address of numeric label after "
3655 "the function is defined");
3656 BB = dyn_cast_or_null<BasicBlock>(
3657 F->getValueSymbolTable()->lookup(Label.StrVal));
3658 if (!BB)
3659 return error(Label.Loc, "referenced value is not a basic block");
3660 }
3661
3662 ID.ConstantVal = BlockAddress::get(F, BB);
3663 ID.Kind = ValID::t_Constant;
3664 return false;
3665 }
3666
3667 case lltok::kw_dso_local_equivalent: {
3668 // ValID ::= 'dso_local_equivalent' @foo
3669 Lex.Lex();
3670
3671 ValID Fn;
3672
3673 if (parseValID(Fn, PFS))
3674 return true;
3675
3676 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
3677 return error(Fn.Loc,
3678 "expected global value name in dso_local_equivalent");
3679
3680 // Try to find the function (but skip it if it's forward-referenced).
3681 GlobalValue *GV = nullptr;
3682 if (Fn.Kind == ValID::t_GlobalID) {
3683 if (Fn.UIntVal < NumberedVals.size())
3684 GV = NumberedVals[Fn.UIntVal];
3685 } else if (!ForwardRefVals.count(Fn.StrVal)) {
3686 GV = M->getNamedValue(Fn.StrVal);
3687 }
3688
3689 if (!GV) {
3690 // Make a placeholder global variable as a placeholder for this reference.
3691 auto &FwdRefMap = (Fn.Kind == ValID::t_GlobalID)
3692 ? ForwardRefDSOLocalEquivalentIDs
3693 : ForwardRefDSOLocalEquivalentNames;
3694 GlobalValue *&FwdRef = FwdRefMap.try_emplace(Fn, nullptr).first->second;
3695 if (!FwdRef) {
3696 FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), false,
3697 GlobalValue::InternalLinkage, nullptr, "",
3698 nullptr, GlobalValue::NotThreadLocal);
3699 }
3700
3701 ID.ConstantVal = FwdRef;
3702 ID.Kind = ValID::t_Constant;
3703 return false;
3704 }
3705
3706 if (!GV->getValueType()->isFunctionTy())
3707 return error(Fn.Loc, "expected a function, alias to function, or ifunc "
3708 "in dso_local_equivalent");
3709
3710 ID.ConstantVal = DSOLocalEquivalent::get(GV);
3711 ID.Kind = ValID::t_Constant;
3712 return false;
3713 }
3714
3715 case lltok::kw_no_cfi: {
3716 // ValID ::= 'no_cfi' @foo
3717 Lex.Lex();
3718
3719 if (parseValID(ID, PFS))
3720 return true;
3721
3722 if (ID.Kind != ValID::t_GlobalID && ID.Kind != ValID::t_GlobalName)
3723 return error(ID.Loc, "expected global value name in no_cfi");
3724
3725 ID.NoCFI = true;
3726 return false;
3727 }
3728
3729 case lltok::kw_trunc:
3730 case lltok::kw_zext:
3731 case lltok::kw_sext:
3732 case lltok::kw_fptrunc:
3733 case lltok::kw_fpext:
3734 case lltok::kw_bitcast:
3735 case lltok::kw_addrspacecast:
3736 case lltok::kw_uitofp:
3737 case lltok::kw_sitofp:
3738 case lltok::kw_fptoui:
3739 case lltok::kw_fptosi:
3740 case lltok::kw_inttoptr:
3741 case lltok::kw_ptrtoint: {
3742 unsigned Opc = Lex.getUIntVal();
3743 Type *DestTy = nullptr;
3744 Constant *SrcVal;
3745 Lex.Lex();
3746 if (parseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
3747 parseGlobalTypeAndValue(SrcVal) ||
3748 parseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
3749 parseType(DestTy) ||
3750 parseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
3751 return true;
3752 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
3753 return error(ID.Loc, "invalid cast opcode for cast from '" +
3754 getTypeString(SrcVal->getType()) + "' to '" +
3755 getTypeString(DestTy) + "'");
3756 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
3757 SrcVal, DestTy);
3758 ID.Kind = ValID::t_Constant;
3759 return false;
3760 }
3761 case lltok::kw_extractvalue:
3762 return error(ID.Loc, "extractvalue constexprs are no longer supported");
3763 case lltok::kw_insertvalue:
3764 return error(ID.Loc, "insertvalue constexprs are no longer supported");
3765 case lltok::kw_udiv:
3766 return error(ID.Loc, "udiv constexprs are no longer supported");
3767 case lltok::kw_sdiv:
3768 return error(ID.Loc, "sdiv constexprs are no longer supported");
3769 case lltok::kw_urem:
3770 return error(ID.Loc, "urem constexprs are no longer supported");
3771 case lltok::kw_srem:
3772 return error(ID.Loc, "srem constexprs are no longer supported");
3773 case lltok::kw_fadd:
3774 return error(ID.Loc, "fadd constexprs are no longer supported");
3775 case lltok::kw_fsub:
3776 return error(ID.Loc, "fsub constexprs are no longer supported");
3777 case lltok::kw_fmul:
3778 return error(ID.Loc, "fmul constexprs are no longer supported");
3779 case lltok::kw_fdiv:
3780 return error(ID.Loc, "fdiv constexprs are no longer supported");
3781 case lltok::kw_frem:
3782 return error(ID.Loc, "frem constexprs are no longer supported");
3783 case lltok::kw_fneg:
3784 return error(ID.Loc, "fneg constexprs are no longer supported");
3785 case lltok::kw_icmp:
3786 case lltok::kw_fcmp: {
3787 unsigned PredVal, Opc = Lex.getUIntVal();
3788 Constant *Val0, *Val1;
3789 Lex.Lex();
3790 if (parseCmpPredicate(PredVal, Opc) ||
3791 parseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
3792 parseGlobalTypeAndValue(Val0) ||
3793 parseToken(lltok::comma, "expected comma in compare constantexpr") ||
3794 parseGlobalTypeAndValue(Val1) ||
3795 parseToken(lltok::rparen, "expected ')' in compare constantexpr"))
3796 return true;
3797
3798 if (Val0->getType() != Val1->getType())
3799 return error(ID.Loc, "compare operands must have the same type");
3800
3801 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
3802
3803 if (Opc == Instruction::FCmp) {
3804 if (!Val0->getType()->isFPOrFPVectorTy())
3805 return error(ID.Loc, "fcmp requires floating point operands");
3806 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
3807 } else {
3808 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
3809 if (!Val0->getType()->isIntOrIntVectorTy() &&
3810 !Val0->getType()->isPtrOrPtrVectorTy())
3811 return error(ID.Loc, "icmp requires pointer or integer operands");
3812 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
3813 }
3814 ID.Kind = ValID::t_Constant;
3815 return false;
3816 }
3817
3818 // Binary Operators.
3819 case lltok::kw_add:
3820 case lltok::kw_sub:
3821 case lltok::kw_mul:
3822 case lltok::kw_shl:
3823 case lltok::kw_lshr:
3824 case lltok::kw_ashr: {
3825 bool NUW = false;
3826 bool NSW = false;
3827 bool Exact = false;
3828 unsigned Opc = Lex.getUIntVal();
3829 Constant *Val0, *Val1;
3830 Lex.Lex();
3831 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
3832 Opc == Instruction::Mul || Opc == Instruction::Shl) {
3833 if (EatIfPresent(lltok::kw_nuw))
3834 NUW = true;
3835 if (EatIfPresent(lltok::kw_nsw)) {
3836 NSW = true;
3837 if (EatIfPresent(lltok::kw_nuw))
3838 NUW = true;
3839 }
3840 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
3841 Opc == Instruction::LShr || Opc == Instruction::AShr) {
3842 if (EatIfPresent(lltok::kw_exact))
3843 Exact = true;
3844 }
3845 if (parseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
3846 parseGlobalTypeAndValue(Val0) ||
3847 parseToken(lltok::comma, "expected comma in binary constantexpr") ||
3848 parseGlobalTypeAndValue(Val1) ||
3849 parseToken(lltok::rparen, "expected ')' in binary constantexpr"))
3850 return true;
3851 if (Val0->getType() != Val1->getType())
3852 return error(ID.Loc, "operands of constexpr must have same type");
3853 // Check that the type is valid for the operator.
3854 switch (Opc) {
3855 case Instruction::Add:
3856 case Instruction::Sub:
3857 case Instruction::Mul:
3858 case Instruction::UDiv:
3859 case Instruction::SDiv:
3860 case Instruction::URem:
3861 case Instruction::SRem:
3862 case Instruction::Shl:
3863 case Instruction::AShr:
3864 case Instruction::LShr:
3865 if (!Val0->getType()->isIntOrIntVectorTy())
3866 return error(ID.Loc, "constexpr requires integer operands");
3867 break;
3868 case Instruction::FAdd:
3869 case Instruction::FSub:
3870 case Instruction::FMul:
3871 case Instruction::FDiv:
3872 case Instruction::FRem:
3873 if (!Val0->getType()->isFPOrFPVectorTy())
3874 return error(ID.Loc, "constexpr requires fp operands");
3875 break;
3876 default: llvm_unreachable("Unknown binary operator!");
3877 }
3878 unsigned Flags = 0;
3879 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
3880 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
3881 if (Exact) Flags |= PossiblyExactOperator::IsExact;
3882 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
3883 ID.ConstantVal = C;
3884 ID.Kind = ValID::t_Constant;
3885 return false;
3886 }
3887
3888 // Logical Operations
3889 case lltok::kw_and:
3890 case lltok::kw_or:
3891 case lltok::kw_xor: {
3892 unsigned Opc = Lex.getUIntVal();
3893 Constant *Val0, *Val1;
3894 Lex.Lex();
3895 if (parseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
3896 parseGlobalTypeAndValue(Val0) ||
3897 parseToken(lltok::comma, "expected comma in logical constantexpr") ||
3898 parseGlobalTypeAndValue(Val1) ||
3899 parseToken(lltok::rparen, "expected ')' in logical constantexpr"))
3900 return true;
3901 if (Val0->getType() != Val1->getType())
3902 return error(ID.Loc, "operands of constexpr must have same type");
3903 if (!Val0->getType()->isIntOrIntVectorTy())
3904 return error(ID.Loc,
3905 "constexpr requires integer or integer vector operands");
3906 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
3907 ID.Kind = ValID::t_Constant;
3908 return false;
3909 }
3910
3911 case lltok::kw_getelementptr:
3912 case lltok::kw_shufflevector:
3913 case lltok::kw_insertelement:
3914 case lltok::kw_extractelement:
3915 case lltok::kw_select: {
3916 unsigned Opc = Lex.getUIntVal();
3917 SmallVector<Constant*, 16> Elts;
3918 bool InBounds = false;
3919 Type *Ty;
3920 Lex.Lex();
3921
3922 if (Opc == Instruction::GetElementPtr)
3923 InBounds = EatIfPresent(lltok::kw_inbounds);
3924
3925 if (parseToken(lltok::lparen, "expected '(' in constantexpr"))
3926 return true;
3927
3928 LocTy ExplicitTypeLoc = Lex.getLoc();
3929 if (Opc == Instruction::GetElementPtr) {
3930 if (parseType(Ty) ||
3931 parseToken(lltok::comma, "expected comma after getelementptr's type"))
3932 return true;
3933 }
3934
3935 std::optional<unsigned> InRangeOp;
3936 if (parseGlobalValueVector(
3937 Elts, Opc == Instruction::GetElementPtr ? &InRangeOp : nullptr) ||
3938 parseToken(lltok::rparen, "expected ')' in constantexpr"))
3939 return true;
3940
3941 if (Opc == Instruction::GetElementPtr) {
3942 if (Elts.size() == 0 ||
3943 !Elts[0]->getType()->isPtrOrPtrVectorTy())
3944 return error(ID.Loc, "base of getelementptr must be a pointer");
3945
3946 Type *BaseType = Elts[0]->getType();
3947 auto *BasePointerType = cast<PointerType>(BaseType->getScalarType());
3948 if (!BasePointerType->isOpaqueOrPointeeTypeMatches(Ty)) {
3949 return error(
3950 ExplicitTypeLoc,
3951 typeComparisonErrorMessage(
3952 "explicit pointee type doesn't match operand's pointee type",
3953 Ty, BasePointerType->getNonOpaquePointerElementType()));
3954 }
3955
3956 unsigned GEPWidth =
3957 BaseType->isVectorTy()
3958 ? cast<FixedVectorType>(BaseType)->getNumElements()
3959 : 0;
3960
3961 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
3962 for (Constant *Val : Indices) {
3963 Type *ValTy = Val->getType();
3964 if (!ValTy->isIntOrIntVectorTy())
3965 return error(ID.Loc, "getelementptr index must be an integer");
3966 if (auto *ValVTy = dyn_cast<VectorType>(ValTy)) {
3967 unsigned ValNumEl = cast<FixedVectorType>(ValVTy)->getNumElements();
3968 if (GEPWidth && (ValNumEl != GEPWidth))
3969 return error(
3970 ID.Loc,
3971 "getelementptr vector index has a wrong number of elements");
3972 // GEPWidth may have been unknown because the base is a scalar,
3973 // but it is known now.
3974 GEPWidth = ValNumEl;
3975 }
3976 }
3977
3978 SmallPtrSet<Type*, 4> Visited;
3979 if (!Indices.empty() && !Ty->isSized(&Visited))
3980 return error(ID.Loc, "base element of getelementptr must be sized");
3981
3982 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
3983 return error(ID.Loc, "invalid getelementptr indices");
3984
3985 if (InRangeOp) {
3986 if (*InRangeOp == 0)
3987 return error(ID.Loc,
3988 "inrange keyword may not appear on pointer operand");
3989 --*InRangeOp;
3990 }
3991
3992 ID.ConstantVal = ConstantExpr::getGetElementPtr(Ty, Elts[0], Indices,
3993 InBounds, InRangeOp);
3994 } else if (Opc == Instruction::Select) {
3995 if (Elts.size() != 3)
3996 return error(ID.Loc, "expected three operands to select");
3997 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
3998 Elts[2]))
3999 return error(ID.Loc, Reason);
4000 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
4001 } else if (Opc == Instruction::ShuffleVector) {
4002 if (Elts.size() != 3)
4003 return error(ID.Loc, "expected three operands to shufflevector");
4004 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
4005 return error(ID.Loc, "invalid operands to shufflevector");
4006 SmallVector<int, 16> Mask;
4007 ShuffleVectorInst::getShuffleMask(cast<Constant>(Elts[2]), Mask);
4008 ID.ConstantVal = ConstantExpr::getShuffleVector(Elts[0], Elts[1], Mask);
4009 } else if (Opc == Instruction::ExtractElement) {
4010 if (Elts.size() != 2)
4011 return error(ID.Loc, "expected two operands to extractelement");
4012 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
4013 return error(ID.Loc, "invalid extractelement operands");
4014 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
4015 } else {
4016 assert(Opc == Instruction::InsertElement && "Unknown opcode");
4017 if (Elts.size() != 3)
4018 return error(ID.Loc, "expected three operands to insertelement");
4019 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
4020 return error(ID.Loc, "invalid insertelement operands");
4021 ID.ConstantVal =
4022 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
4023 }
4024
4025 ID.Kind = ValID::t_Constant;
4026 return false;
4027 }
4028 }
4029
4030 Lex.Lex();
4031 return false;
4032 }
4033
4034 /// parseGlobalValue - parse a global value with the specified type.
parseGlobalValue(Type * Ty,Constant * & C)4035 bool LLParser::parseGlobalValue(Type *Ty, Constant *&C) {
4036 C = nullptr;
4037 ValID ID;
4038 Value *V = nullptr;
4039 bool Parsed = parseValID(ID, /*PFS=*/nullptr, Ty) ||
4040 convertValIDToValue(Ty, ID, V, nullptr);
4041 if (V && !(C = dyn_cast<Constant>(V)))
4042 return error(ID.Loc, "global values must be constants");
4043 return Parsed;
4044 }
4045
parseGlobalTypeAndValue(Constant * & V)4046 bool LLParser::parseGlobalTypeAndValue(Constant *&V) {
4047 Type *Ty = nullptr;
4048 return parseType(Ty) || parseGlobalValue(Ty, V);
4049 }
4050
parseOptionalComdat(StringRef GlobalName,Comdat * & C)4051 bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) {
4052 C = nullptr;
4053
4054 LocTy KwLoc = Lex.getLoc();
4055 if (!EatIfPresent(lltok::kw_comdat))
4056 return false;
4057
4058 if (EatIfPresent(lltok::lparen)) {
4059 if (Lex.getKind() != lltok::ComdatVar)
4060 return tokError("expected comdat variable");
4061 C = getComdat(Lex.getStrVal(), Lex.getLoc());
4062 Lex.Lex();
4063 if (parseToken(lltok::rparen, "expected ')' after comdat var"))
4064 return true;
4065 } else {
4066 if (GlobalName.empty())
4067 return tokError("comdat cannot be unnamed");
4068 C = getComdat(std::string(GlobalName), KwLoc);
4069 }
4070
4071 return false;
4072 }
4073
4074 /// parseGlobalValueVector
4075 /// ::= /*empty*/
4076 /// ::= [inrange] TypeAndValue (',' [inrange] TypeAndValue)*
parseGlobalValueVector(SmallVectorImpl<Constant * > & Elts,std::optional<unsigned> * InRangeOp)4077 bool LLParser::parseGlobalValueVector(SmallVectorImpl<Constant *> &Elts,
4078 std::optional<unsigned> *InRangeOp) {
4079 // Empty list.
4080 if (Lex.getKind() == lltok::rbrace ||
4081 Lex.getKind() == lltok::rsquare ||
4082 Lex.getKind() == lltok::greater ||
4083 Lex.getKind() == lltok::rparen)
4084 return false;
4085
4086 do {
4087 if (InRangeOp && !*InRangeOp && EatIfPresent(lltok::kw_inrange))
4088 *InRangeOp = Elts.size();
4089
4090 Constant *C;
4091 if (parseGlobalTypeAndValue(C))
4092 return true;
4093 Elts.push_back(C);
4094 } while (EatIfPresent(lltok::comma));
4095
4096 return false;
4097 }
4098
parseMDTuple(MDNode * & MD,bool IsDistinct)4099 bool LLParser::parseMDTuple(MDNode *&MD, bool IsDistinct) {
4100 SmallVector<Metadata *, 16> Elts;
4101 if (parseMDNodeVector(Elts))
4102 return true;
4103
4104 MD = (IsDistinct ? MDTuple::getDistinct : MDTuple::get)(Context, Elts);
4105 return false;
4106 }
4107
4108 /// MDNode:
4109 /// ::= !{ ... }
4110 /// ::= !7
4111 /// ::= !DILocation(...)
parseMDNode(MDNode * & N)4112 bool LLParser::parseMDNode(MDNode *&N) {
4113 if (Lex.getKind() == lltok::MetadataVar)
4114 return parseSpecializedMDNode(N);
4115
4116 return parseToken(lltok::exclaim, "expected '!' here") || parseMDNodeTail(N);
4117 }
4118
parseMDNodeTail(MDNode * & N)4119 bool LLParser::parseMDNodeTail(MDNode *&N) {
4120 // !{ ... }
4121 if (Lex.getKind() == lltok::lbrace)
4122 return parseMDTuple(N);
4123
4124 // !42
4125 return parseMDNodeID(N);
4126 }
4127
4128 namespace {
4129
4130 /// Structure to represent an optional metadata field.
4131 template <class FieldTy> struct MDFieldImpl {
4132 typedef MDFieldImpl ImplTy;
4133 FieldTy Val;
4134 bool Seen;
4135
assign__anon01dc21920411::MDFieldImpl4136 void assign(FieldTy Val) {
4137 Seen = true;
4138 this->Val = std::move(Val);
4139 }
4140
MDFieldImpl__anon01dc21920411::MDFieldImpl4141 explicit MDFieldImpl(FieldTy Default)
4142 : Val(std::move(Default)), Seen(false) {}
4143 };
4144
4145 /// Structure to represent an optional metadata field that
4146 /// can be of either type (A or B) and encapsulates the
4147 /// MD<typeofA>Field and MD<typeofB>Field structs, so not
4148 /// to reimplement the specifics for representing each Field.
4149 template <class FieldTypeA, class FieldTypeB> struct MDEitherFieldImpl {
4150 typedef MDEitherFieldImpl<FieldTypeA, FieldTypeB> ImplTy;
4151 FieldTypeA A;
4152 FieldTypeB B;
4153 bool Seen;
4154
4155 enum {
4156 IsInvalid = 0,
4157 IsTypeA = 1,
4158 IsTypeB = 2
4159 } WhatIs;
4160
assign__anon01dc21920411::MDEitherFieldImpl4161 void assign(FieldTypeA A) {
4162 Seen = true;
4163 this->A = std::move(A);
4164 WhatIs = IsTypeA;
4165 }
4166
assign__anon01dc21920411::MDEitherFieldImpl4167 void assign(FieldTypeB B) {
4168 Seen = true;
4169 this->B = std::move(B);
4170 WhatIs = IsTypeB;
4171 }
4172
MDEitherFieldImpl__anon01dc21920411::MDEitherFieldImpl4173 explicit MDEitherFieldImpl(FieldTypeA DefaultA, FieldTypeB DefaultB)
4174 : A(std::move(DefaultA)), B(std::move(DefaultB)), Seen(false),
4175 WhatIs(IsInvalid) {}
4176 };
4177
4178 struct MDUnsignedField : public MDFieldImpl<uint64_t> {
4179 uint64_t Max;
4180
MDUnsignedField__anon01dc21920411::MDUnsignedField4181 MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX)
4182 : ImplTy(Default), Max(Max) {}
4183 };
4184
4185 struct LineField : public MDUnsignedField {
LineField__anon01dc21920411::LineField4186 LineField() : MDUnsignedField(0, UINT32_MAX) {}
4187 };
4188
4189 struct ColumnField : public MDUnsignedField {
ColumnField__anon01dc21920411::ColumnField4190 ColumnField() : MDUnsignedField(0, UINT16_MAX) {}
4191 };
4192
4193 struct DwarfTagField : public MDUnsignedField {
DwarfTagField__anon01dc21920411::DwarfTagField4194 DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {}
DwarfTagField__anon01dc21920411::DwarfTagField4195 DwarfTagField(dwarf::Tag DefaultTag)
4196 : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {}
4197 };
4198
4199 struct DwarfMacinfoTypeField : public MDUnsignedField {
DwarfMacinfoTypeField__anon01dc21920411::DwarfMacinfoTypeField4200 DwarfMacinfoTypeField() : MDUnsignedField(0, dwarf::DW_MACINFO_vendor_ext) {}
DwarfMacinfoTypeField__anon01dc21920411::DwarfMacinfoTypeField4201 DwarfMacinfoTypeField(dwarf::MacinfoRecordType DefaultType)
4202 : MDUnsignedField(DefaultType, dwarf::DW_MACINFO_vendor_ext) {}
4203 };
4204
4205 struct DwarfAttEncodingField : public MDUnsignedField {
DwarfAttEncodingField__anon01dc21920411::DwarfAttEncodingField4206 DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {}
4207 };
4208
4209 struct DwarfVirtualityField : public MDUnsignedField {
DwarfVirtualityField__anon01dc21920411::DwarfVirtualityField4210 DwarfVirtualityField() : MDUnsignedField(0, dwarf::DW_VIRTUALITY_max) {}
4211 };
4212
4213 struct DwarfLangField : public MDUnsignedField {
DwarfLangField__anon01dc21920411::DwarfLangField4214 DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {}
4215 };
4216
4217 struct DwarfCCField : public MDUnsignedField {
DwarfCCField__anon01dc21920411::DwarfCCField4218 DwarfCCField() : MDUnsignedField(0, dwarf::DW_CC_hi_user) {}
4219 };
4220
4221 struct EmissionKindField : public MDUnsignedField {
EmissionKindField__anon01dc21920411::EmissionKindField4222 EmissionKindField() : MDUnsignedField(0, DICompileUnit::LastEmissionKind) {}
4223 };
4224
4225 struct NameTableKindField : public MDUnsignedField {
NameTableKindField__anon01dc21920411::NameTableKindField4226 NameTableKindField()
4227 : MDUnsignedField(
4228 0, (unsigned)
4229 DICompileUnit::DebugNameTableKind::LastDebugNameTableKind) {}
4230 };
4231
4232 struct DIFlagField : public MDFieldImpl<DINode::DIFlags> {
DIFlagField__anon01dc21920411::DIFlagField4233 DIFlagField() : MDFieldImpl(DINode::FlagZero) {}
4234 };
4235
4236 struct DISPFlagField : public MDFieldImpl<DISubprogram::DISPFlags> {
DISPFlagField__anon01dc21920411::DISPFlagField4237 DISPFlagField() : MDFieldImpl(DISubprogram::SPFlagZero) {}
4238 };
4239
4240 struct MDAPSIntField : public MDFieldImpl<APSInt> {
MDAPSIntField__anon01dc21920411::MDAPSIntField4241 MDAPSIntField() : ImplTy(APSInt()) {}
4242 };
4243
4244 struct MDSignedField : public MDFieldImpl<int64_t> {
4245 int64_t Min = INT64_MIN;
4246 int64_t Max = INT64_MAX;
4247
MDSignedField__anon01dc21920411::MDSignedField4248 MDSignedField(int64_t Default = 0)
4249 : ImplTy(Default) {}
MDSignedField__anon01dc21920411::MDSignedField4250 MDSignedField(int64_t Default, int64_t Min, int64_t Max)
4251 : ImplTy(Default), Min(Min), Max(Max) {}
4252 };
4253
4254 struct MDBoolField : public MDFieldImpl<bool> {
MDBoolField__anon01dc21920411::MDBoolField4255 MDBoolField(bool Default = false) : ImplTy(Default) {}
4256 };
4257
4258 struct MDField : public MDFieldImpl<Metadata *> {
4259 bool AllowNull;
4260
MDField__anon01dc21920411::MDField4261 MDField(bool AllowNull = true) : ImplTy(nullptr), AllowNull(AllowNull) {}
4262 };
4263
4264 struct MDStringField : public MDFieldImpl<MDString *> {
4265 bool AllowEmpty;
MDStringField__anon01dc21920411::MDStringField4266 MDStringField(bool AllowEmpty = true)
4267 : ImplTy(nullptr), AllowEmpty(AllowEmpty) {}
4268 };
4269
4270 struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> {
MDFieldList__anon01dc21920411::MDFieldList4271 MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {}
4272 };
4273
4274 struct ChecksumKindField : public MDFieldImpl<DIFile::ChecksumKind> {
ChecksumKindField__anon01dc21920411::ChecksumKindField4275 ChecksumKindField(DIFile::ChecksumKind CSKind) : ImplTy(CSKind) {}
4276 };
4277
4278 struct MDSignedOrMDField : MDEitherFieldImpl<MDSignedField, MDField> {
MDSignedOrMDField__anon01dc21920411::MDSignedOrMDField4279 MDSignedOrMDField(int64_t Default = 0, bool AllowNull = true)
4280 : ImplTy(MDSignedField(Default), MDField(AllowNull)) {}
4281
MDSignedOrMDField__anon01dc21920411::MDSignedOrMDField4282 MDSignedOrMDField(int64_t Default, int64_t Min, int64_t Max,
4283 bool AllowNull = true)
4284 : ImplTy(MDSignedField(Default, Min, Max), MDField(AllowNull)) {}
4285
isMDSignedField__anon01dc21920411::MDSignedOrMDField4286 bool isMDSignedField() const { return WhatIs == IsTypeA; }
isMDField__anon01dc21920411::MDSignedOrMDField4287 bool isMDField() const { return WhatIs == IsTypeB; }
getMDSignedValue__anon01dc21920411::MDSignedOrMDField4288 int64_t getMDSignedValue() const {
4289 assert(isMDSignedField() && "Wrong field type");
4290 return A.Val;
4291 }
getMDFieldValue__anon01dc21920411::MDSignedOrMDField4292 Metadata *getMDFieldValue() const {
4293 assert(isMDField() && "Wrong field type");
4294 return B.Val;
4295 }
4296 };
4297
4298 } // end anonymous namespace
4299
4300 namespace llvm {
4301
4302 template <>
parseMDField(LocTy Loc,StringRef Name,MDAPSIntField & Result)4303 bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDAPSIntField &Result) {
4304 if (Lex.getKind() != lltok::APSInt)
4305 return tokError("expected integer");
4306
4307 Result.assign(Lex.getAPSIntVal());
4308 Lex.Lex();
4309 return false;
4310 }
4311
4312 template <>
parseMDField(LocTy Loc,StringRef Name,MDUnsignedField & Result)4313 bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4314 MDUnsignedField &Result) {
4315 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
4316 return tokError("expected unsigned integer");
4317
4318 auto &U = Lex.getAPSIntVal();
4319 if (U.ugt(Result.Max))
4320 return tokError("value for '" + Name + "' too large, limit is " +
4321 Twine(Result.Max));
4322 Result.assign(U.getZExtValue());
4323 assert(Result.Val <= Result.Max && "Expected value in range");
4324 Lex.Lex();
4325 return false;
4326 }
4327
4328 template <>
parseMDField(LocTy Loc,StringRef Name,LineField & Result)4329 bool LLParser::parseMDField(LocTy Loc, StringRef Name, LineField &Result) {
4330 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4331 }
4332 template <>
parseMDField(LocTy Loc,StringRef Name,ColumnField & Result)4333 bool LLParser::parseMDField(LocTy Loc, StringRef Name, ColumnField &Result) {
4334 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4335 }
4336
4337 template <>
parseMDField(LocTy Loc,StringRef Name,DwarfTagField & Result)4338 bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) {
4339 if (Lex.getKind() == lltok::APSInt)
4340 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4341
4342 if (Lex.getKind() != lltok::DwarfTag)
4343 return tokError("expected DWARF tag");
4344
4345 unsigned Tag = dwarf::getTag(Lex.getStrVal());
4346 if (Tag == dwarf::DW_TAG_invalid)
4347 return tokError("invalid DWARF tag" + Twine(" '") + Lex.getStrVal() + "'");
4348 assert(Tag <= Result.Max && "Expected valid DWARF tag");
4349
4350 Result.assign(Tag);
4351 Lex.Lex();
4352 return false;
4353 }
4354
4355 template <>
parseMDField(LocTy Loc,StringRef Name,DwarfMacinfoTypeField & Result)4356 bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4357 DwarfMacinfoTypeField &Result) {
4358 if (Lex.getKind() == lltok::APSInt)
4359 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4360
4361 if (Lex.getKind() != lltok::DwarfMacinfo)
4362 return tokError("expected DWARF macinfo type");
4363
4364 unsigned Macinfo = dwarf::getMacinfo(Lex.getStrVal());
4365 if (Macinfo == dwarf::DW_MACINFO_invalid)
4366 return tokError("invalid DWARF macinfo type" + Twine(" '") +
4367 Lex.getStrVal() + "'");
4368 assert(Macinfo <= Result.Max && "Expected valid DWARF macinfo type");
4369
4370 Result.assign(Macinfo);
4371 Lex.Lex();
4372 return false;
4373 }
4374
4375 template <>
parseMDField(LocTy Loc,StringRef Name,DwarfVirtualityField & Result)4376 bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4377 DwarfVirtualityField &Result) {
4378 if (Lex.getKind() == lltok::APSInt)
4379 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4380
4381 if (Lex.getKind() != lltok::DwarfVirtuality)
4382 return tokError("expected DWARF virtuality code");
4383
4384 unsigned Virtuality = dwarf::getVirtuality(Lex.getStrVal());
4385 if (Virtuality == dwarf::DW_VIRTUALITY_invalid)
4386 return tokError("invalid DWARF virtuality code" + Twine(" '") +
4387 Lex.getStrVal() + "'");
4388 assert(Virtuality <= Result.Max && "Expected valid DWARF virtuality code");
4389 Result.assign(Virtuality);
4390 Lex.Lex();
4391 return false;
4392 }
4393
4394 template <>
parseMDField(LocTy Loc,StringRef Name,DwarfLangField & Result)4395 bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) {
4396 if (Lex.getKind() == lltok::APSInt)
4397 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4398
4399 if (Lex.getKind() != lltok::DwarfLang)
4400 return tokError("expected DWARF language");
4401
4402 unsigned Lang = dwarf::getLanguage(Lex.getStrVal());
4403 if (!Lang)
4404 return tokError("invalid DWARF language" + Twine(" '") + Lex.getStrVal() +
4405 "'");
4406 assert(Lang <= Result.Max && "Expected valid DWARF language");
4407 Result.assign(Lang);
4408 Lex.Lex();
4409 return false;
4410 }
4411
4412 template <>
parseMDField(LocTy Loc,StringRef Name,DwarfCCField & Result)4413 bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfCCField &Result) {
4414 if (Lex.getKind() == lltok::APSInt)
4415 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4416
4417 if (Lex.getKind() != lltok::DwarfCC)
4418 return tokError("expected DWARF calling convention");
4419
4420 unsigned CC = dwarf::getCallingConvention(Lex.getStrVal());
4421 if (!CC)
4422 return tokError("invalid DWARF calling convention" + Twine(" '") +
4423 Lex.getStrVal() + "'");
4424 assert(CC <= Result.Max && "Expected valid DWARF calling convention");
4425 Result.assign(CC);
4426 Lex.Lex();
4427 return false;
4428 }
4429
4430 template <>
parseMDField(LocTy Loc,StringRef Name,EmissionKindField & Result)4431 bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4432 EmissionKindField &Result) {
4433 if (Lex.getKind() == lltok::APSInt)
4434 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4435
4436 if (Lex.getKind() != lltok::EmissionKind)
4437 return tokError("expected emission kind");
4438
4439 auto Kind = DICompileUnit::getEmissionKind(Lex.getStrVal());
4440 if (!Kind)
4441 return tokError("invalid emission kind" + Twine(" '") + Lex.getStrVal() +
4442 "'");
4443 assert(*Kind <= Result.Max && "Expected valid emission kind");
4444 Result.assign(*Kind);
4445 Lex.Lex();
4446 return false;
4447 }
4448
4449 template <>
parseMDField(LocTy Loc,StringRef Name,NameTableKindField & Result)4450 bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4451 NameTableKindField &Result) {
4452 if (Lex.getKind() == lltok::APSInt)
4453 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4454
4455 if (Lex.getKind() != lltok::NameTableKind)
4456 return tokError("expected nameTable kind");
4457
4458 auto Kind = DICompileUnit::getNameTableKind(Lex.getStrVal());
4459 if (!Kind)
4460 return tokError("invalid nameTable kind" + Twine(" '") + Lex.getStrVal() +
4461 "'");
4462 assert(((unsigned)*Kind) <= Result.Max && "Expected valid nameTable kind");
4463 Result.assign((unsigned)*Kind);
4464 Lex.Lex();
4465 return false;
4466 }
4467
4468 template <>
parseMDField(LocTy Loc,StringRef Name,DwarfAttEncodingField & Result)4469 bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4470 DwarfAttEncodingField &Result) {
4471 if (Lex.getKind() == lltok::APSInt)
4472 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4473
4474 if (Lex.getKind() != lltok::DwarfAttEncoding)
4475 return tokError("expected DWARF type attribute encoding");
4476
4477 unsigned Encoding = dwarf::getAttributeEncoding(Lex.getStrVal());
4478 if (!Encoding)
4479 return tokError("invalid DWARF type attribute encoding" + Twine(" '") +
4480 Lex.getStrVal() + "'");
4481 assert(Encoding <= Result.Max && "Expected valid DWARF language");
4482 Result.assign(Encoding);
4483 Lex.Lex();
4484 return false;
4485 }
4486
4487 /// DIFlagField
4488 /// ::= uint32
4489 /// ::= DIFlagVector
4490 /// ::= DIFlagVector '|' DIFlagFwdDecl '|' uint32 '|' DIFlagPublic
4491 template <>
parseMDField(LocTy Loc,StringRef Name,DIFlagField & Result)4492 bool LLParser::parseMDField(LocTy Loc, StringRef Name, DIFlagField &Result) {
4493
4494 // parser for a single flag.
4495 auto parseFlag = [&](DINode::DIFlags &Val) {
4496 if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) {
4497 uint32_t TempVal = static_cast<uint32_t>(Val);
4498 bool Res = parseUInt32(TempVal);
4499 Val = static_cast<DINode::DIFlags>(TempVal);
4500 return Res;
4501 }
4502
4503 if (Lex.getKind() != lltok::DIFlag)
4504 return tokError("expected debug info flag");
4505
4506 Val = DINode::getFlag(Lex.getStrVal());
4507 if (!Val)
4508 return tokError(Twine("invalid debug info flag '") + Lex.getStrVal() +
4509 "'");
4510 Lex.Lex();
4511 return false;
4512 };
4513
4514 // parse the flags and combine them together.
4515 DINode::DIFlags Combined = DINode::FlagZero;
4516 do {
4517 DINode::DIFlags Val;
4518 if (parseFlag(Val))
4519 return true;
4520 Combined |= Val;
4521 } while (EatIfPresent(lltok::bar));
4522
4523 Result.assign(Combined);
4524 return false;
4525 }
4526
4527 /// DISPFlagField
4528 /// ::= uint32
4529 /// ::= DISPFlagVector
4530 /// ::= DISPFlagVector '|' DISPFlag* '|' uint32
4531 template <>
parseMDField(LocTy Loc,StringRef Name,DISPFlagField & Result)4532 bool LLParser::parseMDField(LocTy Loc, StringRef Name, DISPFlagField &Result) {
4533
4534 // parser for a single flag.
4535 auto parseFlag = [&](DISubprogram::DISPFlags &Val) {
4536 if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) {
4537 uint32_t TempVal = static_cast<uint32_t>(Val);
4538 bool Res = parseUInt32(TempVal);
4539 Val = static_cast<DISubprogram::DISPFlags>(TempVal);
4540 return Res;
4541 }
4542
4543 if (Lex.getKind() != lltok::DISPFlag)
4544 return tokError("expected debug info flag");
4545
4546 Val = DISubprogram::getFlag(Lex.getStrVal());
4547 if (!Val)
4548 return tokError(Twine("invalid subprogram debug info flag '") +
4549 Lex.getStrVal() + "'");
4550 Lex.Lex();
4551 return false;
4552 };
4553
4554 // parse the flags and combine them together.
4555 DISubprogram::DISPFlags Combined = DISubprogram::SPFlagZero;
4556 do {
4557 DISubprogram::DISPFlags Val;
4558 if (parseFlag(Val))
4559 return true;
4560 Combined |= Val;
4561 } while (EatIfPresent(lltok::bar));
4562
4563 Result.assign(Combined);
4564 return false;
4565 }
4566
4567 template <>
parseMDField(LocTy Loc,StringRef Name,MDSignedField & Result)4568 bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDSignedField &Result) {
4569 if (Lex.getKind() != lltok::APSInt)
4570 return tokError("expected signed integer");
4571
4572 auto &S = Lex.getAPSIntVal();
4573 if (S < Result.Min)
4574 return tokError("value for '" + Name + "' too small, limit is " +
4575 Twine(Result.Min));
4576 if (S > Result.Max)
4577 return tokError("value for '" + Name + "' too large, limit is " +
4578 Twine(Result.Max));
4579 Result.assign(S.getExtValue());
4580 assert(Result.Val >= Result.Min && "Expected value in range");
4581 assert(Result.Val <= Result.Max && "Expected value in range");
4582 Lex.Lex();
4583 return false;
4584 }
4585
4586 template <>
parseMDField(LocTy Loc,StringRef Name,MDBoolField & Result)4587 bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) {
4588 switch (Lex.getKind()) {
4589 default:
4590 return tokError("expected 'true' or 'false'");
4591 case lltok::kw_true:
4592 Result.assign(true);
4593 break;
4594 case lltok::kw_false:
4595 Result.assign(false);
4596 break;
4597 }
4598 Lex.Lex();
4599 return false;
4600 }
4601
4602 template <>
parseMDField(LocTy Loc,StringRef Name,MDField & Result)4603 bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDField &Result) {
4604 if (Lex.getKind() == lltok::kw_null) {
4605 if (!Result.AllowNull)
4606 return tokError("'" + Name + "' cannot be null");
4607 Lex.Lex();
4608 Result.assign(nullptr);
4609 return false;
4610 }
4611
4612 Metadata *MD;
4613 if (parseMetadata(MD, nullptr))
4614 return true;
4615
4616 Result.assign(MD);
4617 return false;
4618 }
4619
4620 template <>
parseMDField(LocTy Loc,StringRef Name,MDSignedOrMDField & Result)4621 bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4622 MDSignedOrMDField &Result) {
4623 // Try to parse a signed int.
4624 if (Lex.getKind() == lltok::APSInt) {
4625 MDSignedField Res = Result.A;
4626 if (!parseMDField(Loc, Name, Res)) {
4627 Result.assign(Res);
4628 return false;
4629 }
4630 return true;
4631 }
4632
4633 // Otherwise, try to parse as an MDField.
4634 MDField Res = Result.B;
4635 if (!parseMDField(Loc, Name, Res)) {
4636 Result.assign(Res);
4637 return false;
4638 }
4639
4640 return true;
4641 }
4642
4643 template <>
parseMDField(LocTy Loc,StringRef Name,MDStringField & Result)4644 bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDStringField &Result) {
4645 LocTy ValueLoc = Lex.getLoc();
4646 std::string S;
4647 if (parseStringConstant(S))
4648 return true;
4649
4650 if (!Result.AllowEmpty && S.empty())
4651 return error(ValueLoc, "'" + Name + "' cannot be empty");
4652
4653 Result.assign(S.empty() ? nullptr : MDString::get(Context, S));
4654 return false;
4655 }
4656
4657 template <>
parseMDField(LocTy Loc,StringRef Name,MDFieldList & Result)4658 bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) {
4659 SmallVector<Metadata *, 4> MDs;
4660 if (parseMDNodeVector(MDs))
4661 return true;
4662
4663 Result.assign(std::move(MDs));
4664 return false;
4665 }
4666
4667 template <>
parseMDField(LocTy Loc,StringRef Name,ChecksumKindField & Result)4668 bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4669 ChecksumKindField &Result) {
4670 std::optional<DIFile::ChecksumKind> CSKind =
4671 DIFile::getChecksumKind(Lex.getStrVal());
4672
4673 if (Lex.getKind() != lltok::ChecksumKind || !CSKind)
4674 return tokError("invalid checksum kind" + Twine(" '") + Lex.getStrVal() +
4675 "'");
4676
4677 Result.assign(*CSKind);
4678 Lex.Lex();
4679 return false;
4680 }
4681
4682 } // end namespace llvm
4683
4684 template <class ParserTy>
parseMDFieldsImplBody(ParserTy ParseField)4685 bool LLParser::parseMDFieldsImplBody(ParserTy ParseField) {
4686 do {
4687 if (Lex.getKind() != lltok::LabelStr)
4688 return tokError("expected field label here");
4689
4690 if (ParseField())
4691 return true;
4692 } while (EatIfPresent(lltok::comma));
4693
4694 return false;
4695 }
4696
4697 template <class ParserTy>
parseMDFieldsImpl(ParserTy ParseField,LocTy & ClosingLoc)4698 bool LLParser::parseMDFieldsImpl(ParserTy ParseField, LocTy &ClosingLoc) {
4699 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4700 Lex.Lex();
4701
4702 if (parseToken(lltok::lparen, "expected '(' here"))
4703 return true;
4704 if (Lex.getKind() != lltok::rparen)
4705 if (parseMDFieldsImplBody(ParseField))
4706 return true;
4707
4708 ClosingLoc = Lex.getLoc();
4709 return parseToken(lltok::rparen, "expected ')' here");
4710 }
4711
4712 template <class FieldTy>
parseMDField(StringRef Name,FieldTy & Result)4713 bool LLParser::parseMDField(StringRef Name, FieldTy &Result) {
4714 if (Result.Seen)
4715 return tokError("field '" + Name + "' cannot be specified more than once");
4716
4717 LocTy Loc = Lex.getLoc();
4718 Lex.Lex();
4719 return parseMDField(Loc, Name, Result);
4720 }
4721
parseSpecializedMDNode(MDNode * & N,bool IsDistinct)4722 bool LLParser::parseSpecializedMDNode(MDNode *&N, bool IsDistinct) {
4723 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4724
4725 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
4726 if (Lex.getStrVal() == #CLASS) \
4727 return parse##CLASS(N, IsDistinct);
4728 #include "llvm/IR/Metadata.def"
4729
4730 return tokError("expected metadata type");
4731 }
4732
4733 #define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT
4734 #define NOP_FIELD(NAME, TYPE, INIT)
4735 #define REQUIRE_FIELD(NAME, TYPE, INIT) \
4736 if (!NAME.Seen) \
4737 return error(ClosingLoc, "missing required field '" #NAME "'");
4738 #define PARSE_MD_FIELD(NAME, TYPE, DEFAULT) \
4739 if (Lex.getStrVal() == #NAME) \
4740 return parseMDField(#NAME, NAME);
4741 #define PARSE_MD_FIELDS() \
4742 VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD) \
4743 do { \
4744 LocTy ClosingLoc; \
4745 if (parseMDFieldsImpl( \
4746 [&]() -> bool { \
4747 VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD) \
4748 return tokError(Twine("invalid field '") + Lex.getStrVal() + \
4749 "'"); \
4750 }, \
4751 ClosingLoc)) \
4752 return true; \
4753 VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD) \
4754 } while (false)
4755 #define GET_OR_DISTINCT(CLASS, ARGS) \
4756 (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
4757
4758 /// parseDILocationFields:
4759 /// ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6,
4760 /// isImplicitCode: true)
parseDILocation(MDNode * & Result,bool IsDistinct)4761 bool LLParser::parseDILocation(MDNode *&Result, bool IsDistinct) {
4762 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4763 OPTIONAL(line, LineField, ); \
4764 OPTIONAL(column, ColumnField, ); \
4765 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
4766 OPTIONAL(inlinedAt, MDField, ); \
4767 OPTIONAL(isImplicitCode, MDBoolField, (false));
4768 PARSE_MD_FIELDS();
4769 #undef VISIT_MD_FIELDS
4770
4771 Result =
4772 GET_OR_DISTINCT(DILocation, (Context, line.Val, column.Val, scope.Val,
4773 inlinedAt.Val, isImplicitCode.Val));
4774 return false;
4775 }
4776
4777 /// parseDIAssignID:
4778 /// ::= distinct !DIAssignID()
parseDIAssignID(MDNode * & Result,bool IsDistinct)4779 bool LLParser::parseDIAssignID(MDNode *&Result, bool IsDistinct) {
4780 if (!IsDistinct)
4781 return Lex.Error("missing 'distinct', required for !DIAssignID()");
4782
4783 Lex.Lex();
4784
4785 // Now eat the parens.
4786 if (parseToken(lltok::lparen, "expected '(' here"))
4787 return true;
4788 if (parseToken(lltok::rparen, "expected ')' here"))
4789 return true;
4790
4791 Result = DIAssignID::getDistinct(Context);
4792 return false;
4793 }
4794
4795 /// parseGenericDINode:
4796 /// ::= !GenericDINode(tag: 15, header: "...", operands: {...})
parseGenericDINode(MDNode * & Result,bool IsDistinct)4797 bool LLParser::parseGenericDINode(MDNode *&Result, bool IsDistinct) {
4798 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4799 REQUIRED(tag, DwarfTagField, ); \
4800 OPTIONAL(header, MDStringField, ); \
4801 OPTIONAL(operands, MDFieldList, );
4802 PARSE_MD_FIELDS();
4803 #undef VISIT_MD_FIELDS
4804
4805 Result = GET_OR_DISTINCT(GenericDINode,
4806 (Context, tag.Val, header.Val, operands.Val));
4807 return false;
4808 }
4809
4810 /// parseDISubrange:
4811 /// ::= !DISubrange(count: 30, lowerBound: 2)
4812 /// ::= !DISubrange(count: !node, lowerBound: 2)
4813 /// ::= !DISubrange(lowerBound: !node1, upperBound: !node2, stride: !node3)
parseDISubrange(MDNode * & Result,bool IsDistinct)4814 bool LLParser::parseDISubrange(MDNode *&Result, bool IsDistinct) {
4815 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4816 OPTIONAL(count, MDSignedOrMDField, (-1, -1, INT64_MAX, false)); \
4817 OPTIONAL(lowerBound, MDSignedOrMDField, ); \
4818 OPTIONAL(upperBound, MDSignedOrMDField, ); \
4819 OPTIONAL(stride, MDSignedOrMDField, );
4820 PARSE_MD_FIELDS();
4821 #undef VISIT_MD_FIELDS
4822
4823 Metadata *Count = nullptr;
4824 Metadata *LowerBound = nullptr;
4825 Metadata *UpperBound = nullptr;
4826 Metadata *Stride = nullptr;
4827
4828 auto convToMetadata = [&](MDSignedOrMDField Bound) -> Metadata * {
4829 if (Bound.isMDSignedField())
4830 return ConstantAsMetadata::get(ConstantInt::getSigned(
4831 Type::getInt64Ty(Context), Bound.getMDSignedValue()));
4832 if (Bound.isMDField())
4833 return Bound.getMDFieldValue();
4834 return nullptr;
4835 };
4836
4837 Count = convToMetadata(count);
4838 LowerBound = convToMetadata(lowerBound);
4839 UpperBound = convToMetadata(upperBound);
4840 Stride = convToMetadata(stride);
4841
4842 Result = GET_OR_DISTINCT(DISubrange,
4843 (Context, Count, LowerBound, UpperBound, Stride));
4844
4845 return false;
4846 }
4847
4848 /// parseDIGenericSubrange:
4849 /// ::= !DIGenericSubrange(lowerBound: !node1, upperBound: !node2, stride:
4850 /// !node3)
parseDIGenericSubrange(MDNode * & Result,bool IsDistinct)4851 bool LLParser::parseDIGenericSubrange(MDNode *&Result, bool IsDistinct) {
4852 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4853 OPTIONAL(count, MDSignedOrMDField, ); \
4854 OPTIONAL(lowerBound, MDSignedOrMDField, ); \
4855 OPTIONAL(upperBound, MDSignedOrMDField, ); \
4856 OPTIONAL(stride, MDSignedOrMDField, );
4857 PARSE_MD_FIELDS();
4858 #undef VISIT_MD_FIELDS
4859
4860 auto ConvToMetadata = [&](MDSignedOrMDField Bound) -> Metadata * {
4861 if (Bound.isMDSignedField())
4862 return DIExpression::get(
4863 Context, {dwarf::DW_OP_consts,
4864 static_cast<uint64_t>(Bound.getMDSignedValue())});
4865 if (Bound.isMDField())
4866 return Bound.getMDFieldValue();
4867 return nullptr;
4868 };
4869
4870 Metadata *Count = ConvToMetadata(count);
4871 Metadata *LowerBound = ConvToMetadata(lowerBound);
4872 Metadata *UpperBound = ConvToMetadata(upperBound);
4873 Metadata *Stride = ConvToMetadata(stride);
4874
4875 Result = GET_OR_DISTINCT(DIGenericSubrange,
4876 (Context, Count, LowerBound, UpperBound, Stride));
4877
4878 return false;
4879 }
4880
4881 /// parseDIEnumerator:
4882 /// ::= !DIEnumerator(value: 30, isUnsigned: true, name: "SomeKind")
parseDIEnumerator(MDNode * & Result,bool IsDistinct)4883 bool LLParser::parseDIEnumerator(MDNode *&Result, bool IsDistinct) {
4884 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4885 REQUIRED(name, MDStringField, ); \
4886 REQUIRED(value, MDAPSIntField, ); \
4887 OPTIONAL(isUnsigned, MDBoolField, (false));
4888 PARSE_MD_FIELDS();
4889 #undef VISIT_MD_FIELDS
4890
4891 if (isUnsigned.Val && value.Val.isNegative())
4892 return tokError("unsigned enumerator with negative value");
4893
4894 APSInt Value(value.Val);
4895 // Add a leading zero so that unsigned values with the msb set are not
4896 // mistaken for negative values when used for signed enumerators.
4897 if (!isUnsigned.Val && value.Val.isUnsigned() && value.Val.isSignBitSet())
4898 Value = Value.zext(Value.getBitWidth() + 1);
4899
4900 Result =
4901 GET_OR_DISTINCT(DIEnumerator, (Context, Value, isUnsigned.Val, name.Val));
4902
4903 return false;
4904 }
4905
4906 /// parseDIBasicType:
4907 /// ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32,
4908 /// encoding: DW_ATE_encoding, flags: 0)
parseDIBasicType(MDNode * & Result,bool IsDistinct)4909 bool LLParser::parseDIBasicType(MDNode *&Result, bool IsDistinct) {
4910 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4911 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \
4912 OPTIONAL(name, MDStringField, ); \
4913 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
4914 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
4915 OPTIONAL(encoding, DwarfAttEncodingField, ); \
4916 OPTIONAL(flags, DIFlagField, );
4917 PARSE_MD_FIELDS();
4918 #undef VISIT_MD_FIELDS
4919
4920 Result = GET_OR_DISTINCT(DIBasicType, (Context, tag.Val, name.Val, size.Val,
4921 align.Val, encoding.Val, flags.Val));
4922 return false;
4923 }
4924
4925 /// parseDIStringType:
4926 /// ::= !DIStringType(name: "character(4)", size: 32, align: 32)
parseDIStringType(MDNode * & Result,bool IsDistinct)4927 bool LLParser::parseDIStringType(MDNode *&Result, bool IsDistinct) {
4928 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4929 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_string_type)); \
4930 OPTIONAL(name, MDStringField, ); \
4931 OPTIONAL(stringLength, MDField, ); \
4932 OPTIONAL(stringLengthExpression, MDField, ); \
4933 OPTIONAL(stringLocationExpression, MDField, ); \
4934 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
4935 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
4936 OPTIONAL(encoding, DwarfAttEncodingField, );
4937 PARSE_MD_FIELDS();
4938 #undef VISIT_MD_FIELDS
4939
4940 Result = GET_OR_DISTINCT(
4941 DIStringType,
4942 (Context, tag.Val, name.Val, stringLength.Val, stringLengthExpression.Val,
4943 stringLocationExpression.Val, size.Val, align.Val, encoding.Val));
4944 return false;
4945 }
4946
4947 /// parseDIDerivedType:
4948 /// ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0,
4949 /// line: 7, scope: !1, baseType: !2, size: 32,
4950 /// align: 32, offset: 0, flags: 0, extraData: !3,
4951 /// dwarfAddressSpace: 3)
parseDIDerivedType(MDNode * & Result,bool IsDistinct)4952 bool LLParser::parseDIDerivedType(MDNode *&Result, bool IsDistinct) {
4953 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4954 REQUIRED(tag, DwarfTagField, ); \
4955 OPTIONAL(name, MDStringField, ); \
4956 OPTIONAL(file, MDField, ); \
4957 OPTIONAL(line, LineField, ); \
4958 OPTIONAL(scope, MDField, ); \
4959 REQUIRED(baseType, MDField, ); \
4960 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
4961 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
4962 OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \
4963 OPTIONAL(flags, DIFlagField, ); \
4964 OPTIONAL(extraData, MDField, ); \
4965 OPTIONAL(dwarfAddressSpace, MDUnsignedField, (UINT32_MAX, UINT32_MAX)); \
4966 OPTIONAL(annotations, MDField, );
4967 PARSE_MD_FIELDS();
4968 #undef VISIT_MD_FIELDS
4969
4970 std::optional<unsigned> DWARFAddressSpace;
4971 if (dwarfAddressSpace.Val != UINT32_MAX)
4972 DWARFAddressSpace = dwarfAddressSpace.Val;
4973
4974 Result = GET_OR_DISTINCT(DIDerivedType,
4975 (Context, tag.Val, name.Val, file.Val, line.Val,
4976 scope.Val, baseType.Val, size.Val, align.Val,
4977 offset.Val, DWARFAddressSpace, flags.Val,
4978 extraData.Val, annotations.Val));
4979 return false;
4980 }
4981
parseDICompositeType(MDNode * & Result,bool IsDistinct)4982 bool LLParser::parseDICompositeType(MDNode *&Result, bool IsDistinct) {
4983 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4984 REQUIRED(tag, DwarfTagField, ); \
4985 OPTIONAL(name, MDStringField, ); \
4986 OPTIONAL(file, MDField, ); \
4987 OPTIONAL(line, LineField, ); \
4988 OPTIONAL(scope, MDField, ); \
4989 OPTIONAL(baseType, MDField, ); \
4990 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
4991 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
4992 OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \
4993 OPTIONAL(flags, DIFlagField, ); \
4994 OPTIONAL(elements, MDField, ); \
4995 OPTIONAL(runtimeLang, DwarfLangField, ); \
4996 OPTIONAL(vtableHolder, MDField, ); \
4997 OPTIONAL(templateParams, MDField, ); \
4998 OPTIONAL(identifier, MDStringField, ); \
4999 OPTIONAL(discriminator, MDField, ); \
5000 OPTIONAL(dataLocation, MDField, ); \
5001 OPTIONAL(associated, MDField, ); \
5002 OPTIONAL(allocated, MDField, ); \
5003 OPTIONAL(rank, MDSignedOrMDField, ); \
5004 OPTIONAL(annotations, MDField, );
5005 PARSE_MD_FIELDS();
5006 #undef VISIT_MD_FIELDS
5007
5008 Metadata *Rank = nullptr;
5009 if (rank.isMDSignedField())
5010 Rank = ConstantAsMetadata::get(ConstantInt::getSigned(
5011 Type::getInt64Ty(Context), rank.getMDSignedValue()));
5012 else if (rank.isMDField())
5013 Rank = rank.getMDFieldValue();
5014
5015 // If this has an identifier try to build an ODR type.
5016 if (identifier.Val)
5017 if (auto *CT = DICompositeType::buildODRType(
5018 Context, *identifier.Val, tag.Val, name.Val, file.Val, line.Val,
5019 scope.Val, baseType.Val, size.Val, align.Val, offset.Val, flags.Val,
5020 elements.Val, runtimeLang.Val, vtableHolder.Val, templateParams.Val,
5021 discriminator.Val, dataLocation.Val, associated.Val, allocated.Val,
5022 Rank, annotations.Val)) {
5023 Result = CT;
5024 return false;
5025 }
5026
5027 // Create a new node, and save it in the context if it belongs in the type
5028 // map.
5029 Result = GET_OR_DISTINCT(
5030 DICompositeType,
5031 (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val,
5032 size.Val, align.Val, offset.Val, flags.Val, elements.Val,
5033 runtimeLang.Val, vtableHolder.Val, templateParams.Val, identifier.Val,
5034 discriminator.Val, dataLocation.Val, associated.Val, allocated.Val, Rank,
5035 annotations.Val));
5036 return false;
5037 }
5038
parseDISubroutineType(MDNode * & Result,bool IsDistinct)5039 bool LLParser::parseDISubroutineType(MDNode *&Result, bool IsDistinct) {
5040 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5041 OPTIONAL(flags, DIFlagField, ); \
5042 OPTIONAL(cc, DwarfCCField, ); \
5043 REQUIRED(types, MDField, );
5044 PARSE_MD_FIELDS();
5045 #undef VISIT_MD_FIELDS
5046
5047 Result = GET_OR_DISTINCT(DISubroutineType,
5048 (Context, flags.Val, cc.Val, types.Val));
5049 return false;
5050 }
5051
5052 /// parseDIFileType:
5053 /// ::= !DIFileType(filename: "path/to/file", directory: "/path/to/dir",
5054 /// checksumkind: CSK_MD5,
5055 /// checksum: "000102030405060708090a0b0c0d0e0f",
5056 /// source: "source file contents")
parseDIFile(MDNode * & Result,bool IsDistinct)5057 bool LLParser::parseDIFile(MDNode *&Result, bool IsDistinct) {
5058 // The default constructed value for checksumkind is required, but will never
5059 // be used, as the parser checks if the field was actually Seen before using
5060 // the Val.
5061 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5062 REQUIRED(filename, MDStringField, ); \
5063 REQUIRED(directory, MDStringField, ); \
5064 OPTIONAL(checksumkind, ChecksumKindField, (DIFile::CSK_MD5)); \
5065 OPTIONAL(checksum, MDStringField, ); \
5066 OPTIONAL(source, MDStringField, );
5067 PARSE_MD_FIELDS();
5068 #undef VISIT_MD_FIELDS
5069
5070 std::optional<DIFile::ChecksumInfo<MDString *>> OptChecksum;
5071 if (checksumkind.Seen && checksum.Seen)
5072 OptChecksum.emplace(checksumkind.Val, checksum.Val);
5073 else if (checksumkind.Seen || checksum.Seen)
5074 return Lex.Error("'checksumkind' and 'checksum' must be provided together");
5075
5076 MDString *Source = nullptr;
5077 if (source.Seen)
5078 Source = source.Val;
5079 Result = GET_OR_DISTINCT(
5080 DIFile, (Context, filename.Val, directory.Val, OptChecksum, Source));
5081 return false;
5082 }
5083
5084 /// parseDICompileUnit:
5085 /// ::= !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang",
5086 /// isOptimized: true, flags: "-O2", runtimeVersion: 1,
5087 /// splitDebugFilename: "abc.debug",
5088 /// emissionKind: FullDebug, enums: !1, retainedTypes: !2,
5089 /// globals: !4, imports: !5, macros: !6, dwoId: 0x0abcd,
5090 /// sysroot: "/", sdk: "MacOSX.sdk")
parseDICompileUnit(MDNode * & Result,bool IsDistinct)5091 bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) {
5092 if (!IsDistinct)
5093 return Lex.Error("missing 'distinct', required for !DICompileUnit");
5094
5095 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5096 REQUIRED(language, DwarfLangField, ); \
5097 REQUIRED(file, MDField, (/* AllowNull */ false)); \
5098 OPTIONAL(producer, MDStringField, ); \
5099 OPTIONAL(isOptimized, MDBoolField, ); \
5100 OPTIONAL(flags, MDStringField, ); \
5101 OPTIONAL(runtimeVersion, MDUnsignedField, (0, UINT32_MAX)); \
5102 OPTIONAL(splitDebugFilename, MDStringField, ); \
5103 OPTIONAL(emissionKind, EmissionKindField, ); \
5104 OPTIONAL(enums, MDField, ); \
5105 OPTIONAL(retainedTypes, MDField, ); \
5106 OPTIONAL(globals, MDField, ); \
5107 OPTIONAL(imports, MDField, ); \
5108 OPTIONAL(macros, MDField, ); \
5109 OPTIONAL(dwoId, MDUnsignedField, ); \
5110 OPTIONAL(splitDebugInlining, MDBoolField, = true); \
5111 OPTIONAL(debugInfoForProfiling, MDBoolField, = false); \
5112 OPTIONAL(nameTableKind, NameTableKindField, ); \
5113 OPTIONAL(rangesBaseAddress, MDBoolField, = false); \
5114 OPTIONAL(sysroot, MDStringField, ); \
5115 OPTIONAL(sdk, MDStringField, );
5116 PARSE_MD_FIELDS();
5117 #undef VISIT_MD_FIELDS
5118
5119 Result = DICompileUnit::getDistinct(
5120 Context, language.Val, file.Val, producer.Val, isOptimized.Val, flags.Val,
5121 runtimeVersion.Val, splitDebugFilename.Val, emissionKind.Val, enums.Val,
5122 retainedTypes.Val, globals.Val, imports.Val, macros.Val, dwoId.Val,
5123 splitDebugInlining.Val, debugInfoForProfiling.Val, nameTableKind.Val,
5124 rangesBaseAddress.Val, sysroot.Val, sdk.Val);
5125 return false;
5126 }
5127
5128 /// parseDISubprogram:
5129 /// ::= !DISubprogram(scope: !0, name: "foo", linkageName: "_Zfoo",
5130 /// file: !1, line: 7, type: !2, isLocal: false,
5131 /// isDefinition: true, scopeLine: 8, containingType: !3,
5132 /// virtuality: DW_VIRTUALTIY_pure_virtual,
5133 /// virtualIndex: 10, thisAdjustment: 4, flags: 11,
5134 /// spFlags: 10, isOptimized: false, templateParams: !4,
5135 /// declaration: !5, retainedNodes: !6, thrownTypes: !7,
5136 /// annotations: !8)
parseDISubprogram(MDNode * & Result,bool IsDistinct)5137 bool LLParser::parseDISubprogram(MDNode *&Result, bool IsDistinct) {
5138 auto Loc = Lex.getLoc();
5139 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5140 OPTIONAL(scope, MDField, ); \
5141 OPTIONAL(name, MDStringField, ); \
5142 OPTIONAL(linkageName, MDStringField, ); \
5143 OPTIONAL(file, MDField, ); \
5144 OPTIONAL(line, LineField, ); \
5145 OPTIONAL(type, MDField, ); \
5146 OPTIONAL(isLocal, MDBoolField, ); \
5147 OPTIONAL(isDefinition, MDBoolField, (true)); \
5148 OPTIONAL(scopeLine, LineField, ); \
5149 OPTIONAL(containingType, MDField, ); \
5150 OPTIONAL(virtuality, DwarfVirtualityField, ); \
5151 OPTIONAL(virtualIndex, MDUnsignedField, (0, UINT32_MAX)); \
5152 OPTIONAL(thisAdjustment, MDSignedField, (0, INT32_MIN, INT32_MAX)); \
5153 OPTIONAL(flags, DIFlagField, ); \
5154 OPTIONAL(spFlags, DISPFlagField, ); \
5155 OPTIONAL(isOptimized, MDBoolField, ); \
5156 OPTIONAL(unit, MDField, ); \
5157 OPTIONAL(templateParams, MDField, ); \
5158 OPTIONAL(declaration, MDField, ); \
5159 OPTIONAL(retainedNodes, MDField, ); \
5160 OPTIONAL(thrownTypes, MDField, ); \
5161 OPTIONAL(annotations, MDField, ); \
5162 OPTIONAL(targetFuncName, MDStringField, );
5163 PARSE_MD_FIELDS();
5164 #undef VISIT_MD_FIELDS
5165
5166 // An explicit spFlags field takes precedence over individual fields in
5167 // older IR versions.
5168 DISubprogram::DISPFlags SPFlags =
5169 spFlags.Seen ? spFlags.Val
5170 : DISubprogram::toSPFlags(isLocal.Val, isDefinition.Val,
5171 isOptimized.Val, virtuality.Val);
5172 if ((SPFlags & DISubprogram::SPFlagDefinition) && !IsDistinct)
5173 return Lex.Error(
5174 Loc,
5175 "missing 'distinct', required for !DISubprogram that is a Definition");
5176 Result = GET_OR_DISTINCT(
5177 DISubprogram,
5178 (Context, scope.Val, name.Val, linkageName.Val, file.Val, line.Val,
5179 type.Val, scopeLine.Val, containingType.Val, virtualIndex.Val,
5180 thisAdjustment.Val, flags.Val, SPFlags, unit.Val, templateParams.Val,
5181 declaration.Val, retainedNodes.Val, thrownTypes.Val, annotations.Val,
5182 targetFuncName.Val));
5183 return false;
5184 }
5185
5186 /// parseDILexicalBlock:
5187 /// ::= !DILexicalBlock(scope: !0, file: !2, line: 7, column: 9)
parseDILexicalBlock(MDNode * & Result,bool IsDistinct)5188 bool LLParser::parseDILexicalBlock(MDNode *&Result, bool IsDistinct) {
5189 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5190 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
5191 OPTIONAL(file, MDField, ); \
5192 OPTIONAL(line, LineField, ); \
5193 OPTIONAL(column, ColumnField, );
5194 PARSE_MD_FIELDS();
5195 #undef VISIT_MD_FIELDS
5196
5197 Result = GET_OR_DISTINCT(
5198 DILexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val));
5199 return false;
5200 }
5201
5202 /// parseDILexicalBlockFile:
5203 /// ::= !DILexicalBlockFile(scope: !0, file: !2, discriminator: 9)
parseDILexicalBlockFile(MDNode * & Result,bool IsDistinct)5204 bool LLParser::parseDILexicalBlockFile(MDNode *&Result, bool IsDistinct) {
5205 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5206 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
5207 OPTIONAL(file, MDField, ); \
5208 REQUIRED(discriminator, MDUnsignedField, (0, UINT32_MAX));
5209 PARSE_MD_FIELDS();
5210 #undef VISIT_MD_FIELDS
5211
5212 Result = GET_OR_DISTINCT(DILexicalBlockFile,
5213 (Context, scope.Val, file.Val, discriminator.Val));
5214 return false;
5215 }
5216
5217 /// parseDICommonBlock:
5218 /// ::= !DICommonBlock(scope: !0, file: !2, name: "COMMON name", line: 9)
parseDICommonBlock(MDNode * & Result,bool IsDistinct)5219 bool LLParser::parseDICommonBlock(MDNode *&Result, bool IsDistinct) {
5220 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5221 REQUIRED(scope, MDField, ); \
5222 OPTIONAL(declaration, MDField, ); \
5223 OPTIONAL(name, MDStringField, ); \
5224 OPTIONAL(file, MDField, ); \
5225 OPTIONAL(line, LineField, );
5226 PARSE_MD_FIELDS();
5227 #undef VISIT_MD_FIELDS
5228
5229 Result = GET_OR_DISTINCT(DICommonBlock,
5230 (Context, scope.Val, declaration.Val, name.Val,
5231 file.Val, line.Val));
5232 return false;
5233 }
5234
5235 /// parseDINamespace:
5236 /// ::= !DINamespace(scope: !0, file: !2, name: "SomeNamespace", line: 9)
parseDINamespace(MDNode * & Result,bool IsDistinct)5237 bool LLParser::parseDINamespace(MDNode *&Result, bool IsDistinct) {
5238 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5239 REQUIRED(scope, MDField, ); \
5240 OPTIONAL(name, MDStringField, ); \
5241 OPTIONAL(exportSymbols, MDBoolField, );
5242 PARSE_MD_FIELDS();
5243 #undef VISIT_MD_FIELDS
5244
5245 Result = GET_OR_DISTINCT(DINamespace,
5246 (Context, scope.Val, name.Val, exportSymbols.Val));
5247 return false;
5248 }
5249
5250 /// parseDIMacro:
5251 /// ::= !DIMacro(macinfo: type, line: 9, name: "SomeMacro", value:
5252 /// "SomeValue")
parseDIMacro(MDNode * & Result,bool IsDistinct)5253 bool LLParser::parseDIMacro(MDNode *&Result, bool IsDistinct) {
5254 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5255 REQUIRED(type, DwarfMacinfoTypeField, ); \
5256 OPTIONAL(line, LineField, ); \
5257 REQUIRED(name, MDStringField, ); \
5258 OPTIONAL(value, MDStringField, );
5259 PARSE_MD_FIELDS();
5260 #undef VISIT_MD_FIELDS
5261
5262 Result = GET_OR_DISTINCT(DIMacro,
5263 (Context, type.Val, line.Val, name.Val, value.Val));
5264 return false;
5265 }
5266
5267 /// parseDIMacroFile:
5268 /// ::= !DIMacroFile(line: 9, file: !2, nodes: !3)
parseDIMacroFile(MDNode * & Result,bool IsDistinct)5269 bool LLParser::parseDIMacroFile(MDNode *&Result, bool IsDistinct) {
5270 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5271 OPTIONAL(type, DwarfMacinfoTypeField, (dwarf::DW_MACINFO_start_file)); \
5272 OPTIONAL(line, LineField, ); \
5273 REQUIRED(file, MDField, ); \
5274 OPTIONAL(nodes, MDField, );
5275 PARSE_MD_FIELDS();
5276 #undef VISIT_MD_FIELDS
5277
5278 Result = GET_OR_DISTINCT(DIMacroFile,
5279 (Context, type.Val, line.Val, file.Val, nodes.Val));
5280 return false;
5281 }
5282
5283 /// parseDIModule:
5284 /// ::= !DIModule(scope: !0, name: "SomeModule", configMacros:
5285 /// "-DNDEBUG", includePath: "/usr/include", apinotes: "module.apinotes",
5286 /// file: !1, line: 4, isDecl: false)
parseDIModule(MDNode * & Result,bool IsDistinct)5287 bool LLParser::parseDIModule(MDNode *&Result, bool IsDistinct) {
5288 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5289 REQUIRED(scope, MDField, ); \
5290 REQUIRED(name, MDStringField, ); \
5291 OPTIONAL(configMacros, MDStringField, ); \
5292 OPTIONAL(includePath, MDStringField, ); \
5293 OPTIONAL(apinotes, MDStringField, ); \
5294 OPTIONAL(file, MDField, ); \
5295 OPTIONAL(line, LineField, ); \
5296 OPTIONAL(isDecl, MDBoolField, );
5297 PARSE_MD_FIELDS();
5298 #undef VISIT_MD_FIELDS
5299
5300 Result = GET_OR_DISTINCT(DIModule, (Context, file.Val, scope.Val, name.Val,
5301 configMacros.Val, includePath.Val,
5302 apinotes.Val, line.Val, isDecl.Val));
5303 return false;
5304 }
5305
5306 /// parseDITemplateTypeParameter:
5307 /// ::= !DITemplateTypeParameter(name: "Ty", type: !1, defaulted: false)
parseDITemplateTypeParameter(MDNode * & Result,bool IsDistinct)5308 bool LLParser::parseDITemplateTypeParameter(MDNode *&Result, bool IsDistinct) {
5309 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5310 OPTIONAL(name, MDStringField, ); \
5311 REQUIRED(type, MDField, ); \
5312 OPTIONAL(defaulted, MDBoolField, );
5313 PARSE_MD_FIELDS();
5314 #undef VISIT_MD_FIELDS
5315
5316 Result = GET_OR_DISTINCT(DITemplateTypeParameter,
5317 (Context, name.Val, type.Val, defaulted.Val));
5318 return false;
5319 }
5320
5321 /// parseDITemplateValueParameter:
5322 /// ::= !DITemplateValueParameter(tag: DW_TAG_template_value_parameter,
5323 /// name: "V", type: !1, defaulted: false,
5324 /// value: i32 7)
parseDITemplateValueParameter(MDNode * & Result,bool IsDistinct)5325 bool LLParser::parseDITemplateValueParameter(MDNode *&Result, bool IsDistinct) {
5326 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5327 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_template_value_parameter)); \
5328 OPTIONAL(name, MDStringField, ); \
5329 OPTIONAL(type, MDField, ); \
5330 OPTIONAL(defaulted, MDBoolField, ); \
5331 REQUIRED(value, MDField, );
5332
5333 PARSE_MD_FIELDS();
5334 #undef VISIT_MD_FIELDS
5335
5336 Result = GET_OR_DISTINCT(
5337 DITemplateValueParameter,
5338 (Context, tag.Val, name.Val, type.Val, defaulted.Val, value.Val));
5339 return false;
5340 }
5341
5342 /// parseDIGlobalVariable:
5343 /// ::= !DIGlobalVariable(scope: !0, name: "foo", linkageName: "foo",
5344 /// file: !1, line: 7, type: !2, isLocal: false,
5345 /// isDefinition: true, templateParams: !3,
5346 /// declaration: !4, align: 8)
parseDIGlobalVariable(MDNode * & Result,bool IsDistinct)5347 bool LLParser::parseDIGlobalVariable(MDNode *&Result, bool IsDistinct) {
5348 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5349 OPTIONAL(name, MDStringField, (/* AllowEmpty */ false)); \
5350 OPTIONAL(scope, MDField, ); \
5351 OPTIONAL(linkageName, MDStringField, ); \
5352 OPTIONAL(file, MDField, ); \
5353 OPTIONAL(line, LineField, ); \
5354 OPTIONAL(type, MDField, ); \
5355 OPTIONAL(isLocal, MDBoolField, ); \
5356 OPTIONAL(isDefinition, MDBoolField, (true)); \
5357 OPTIONAL(templateParams, MDField, ); \
5358 OPTIONAL(declaration, MDField, ); \
5359 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5360 OPTIONAL(annotations, MDField, );
5361 PARSE_MD_FIELDS();
5362 #undef VISIT_MD_FIELDS
5363
5364 Result =
5365 GET_OR_DISTINCT(DIGlobalVariable,
5366 (Context, scope.Val, name.Val, linkageName.Val, file.Val,
5367 line.Val, type.Val, isLocal.Val, isDefinition.Val,
5368 declaration.Val, templateParams.Val, align.Val,
5369 annotations.Val));
5370 return false;
5371 }
5372
5373 /// parseDILocalVariable:
5374 /// ::= !DILocalVariable(arg: 7, scope: !0, name: "foo",
5375 /// file: !1, line: 7, type: !2, arg: 2, flags: 7,
5376 /// align: 8)
5377 /// ::= !DILocalVariable(scope: !0, name: "foo",
5378 /// file: !1, line: 7, type: !2, arg: 2, flags: 7,
5379 /// align: 8)
parseDILocalVariable(MDNode * & Result,bool IsDistinct)5380 bool LLParser::parseDILocalVariable(MDNode *&Result, bool IsDistinct) {
5381 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5382 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
5383 OPTIONAL(name, MDStringField, ); \
5384 OPTIONAL(arg, MDUnsignedField, (0, UINT16_MAX)); \
5385 OPTIONAL(file, MDField, ); \
5386 OPTIONAL(line, LineField, ); \
5387 OPTIONAL(type, MDField, ); \
5388 OPTIONAL(flags, DIFlagField, ); \
5389 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5390 OPTIONAL(annotations, MDField, );
5391 PARSE_MD_FIELDS();
5392 #undef VISIT_MD_FIELDS
5393
5394 Result = GET_OR_DISTINCT(DILocalVariable,
5395 (Context, scope.Val, name.Val, file.Val, line.Val,
5396 type.Val, arg.Val, flags.Val, align.Val,
5397 annotations.Val));
5398 return false;
5399 }
5400
5401 /// parseDILabel:
5402 /// ::= !DILabel(scope: !0, name: "foo", file: !1, line: 7)
parseDILabel(MDNode * & Result,bool IsDistinct)5403 bool LLParser::parseDILabel(MDNode *&Result, bool IsDistinct) {
5404 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5405 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
5406 REQUIRED(name, MDStringField, ); \
5407 REQUIRED(file, MDField, ); \
5408 REQUIRED(line, LineField, );
5409 PARSE_MD_FIELDS();
5410 #undef VISIT_MD_FIELDS
5411
5412 Result = GET_OR_DISTINCT(DILabel,
5413 (Context, scope.Val, name.Val, file.Val, line.Val));
5414 return false;
5415 }
5416
5417 /// parseDIExpression:
5418 /// ::= !DIExpression(0, 7, -1)
parseDIExpression(MDNode * & Result,bool IsDistinct)5419 bool LLParser::parseDIExpression(MDNode *&Result, bool IsDistinct) {
5420 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
5421 Lex.Lex();
5422
5423 if (parseToken(lltok::lparen, "expected '(' here"))
5424 return true;
5425
5426 SmallVector<uint64_t, 8> Elements;
5427 if (Lex.getKind() != lltok::rparen)
5428 do {
5429 if (Lex.getKind() == lltok::DwarfOp) {
5430 if (unsigned Op = dwarf::getOperationEncoding(Lex.getStrVal())) {
5431 Lex.Lex();
5432 Elements.push_back(Op);
5433 continue;
5434 }
5435 return tokError(Twine("invalid DWARF op '") + Lex.getStrVal() + "'");
5436 }
5437
5438 if (Lex.getKind() == lltok::DwarfAttEncoding) {
5439 if (unsigned Op = dwarf::getAttributeEncoding(Lex.getStrVal())) {
5440 Lex.Lex();
5441 Elements.push_back(Op);
5442 continue;
5443 }
5444 return tokError(Twine("invalid DWARF attribute encoding '") +
5445 Lex.getStrVal() + "'");
5446 }
5447
5448 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
5449 return tokError("expected unsigned integer");
5450
5451 auto &U = Lex.getAPSIntVal();
5452 if (U.ugt(UINT64_MAX))
5453 return tokError("element too large, limit is " + Twine(UINT64_MAX));
5454 Elements.push_back(U.getZExtValue());
5455 Lex.Lex();
5456 } while (EatIfPresent(lltok::comma));
5457
5458 if (parseToken(lltok::rparen, "expected ')' here"))
5459 return true;
5460
5461 Result = GET_OR_DISTINCT(DIExpression, (Context, Elements));
5462 return false;
5463 }
5464
parseDIArgList(MDNode * & Result,bool IsDistinct)5465 bool LLParser::parseDIArgList(MDNode *&Result, bool IsDistinct) {
5466 return parseDIArgList(Result, IsDistinct, nullptr);
5467 }
5468 /// ParseDIArgList:
5469 /// ::= !DIArgList(i32 7, i64 %0)
parseDIArgList(MDNode * & Result,bool IsDistinct,PerFunctionState * PFS)5470 bool LLParser::parseDIArgList(MDNode *&Result, bool IsDistinct,
5471 PerFunctionState *PFS) {
5472 assert(PFS && "Expected valid function state");
5473 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
5474 Lex.Lex();
5475
5476 if (parseToken(lltok::lparen, "expected '(' here"))
5477 return true;
5478
5479 SmallVector<ValueAsMetadata *, 4> Args;
5480 if (Lex.getKind() != lltok::rparen)
5481 do {
5482 Metadata *MD;
5483 if (parseValueAsMetadata(MD, "expected value-as-metadata operand", PFS))
5484 return true;
5485 Args.push_back(dyn_cast<ValueAsMetadata>(MD));
5486 } while (EatIfPresent(lltok::comma));
5487
5488 if (parseToken(lltok::rparen, "expected ')' here"))
5489 return true;
5490
5491 Result = GET_OR_DISTINCT(DIArgList, (Context, Args));
5492 return false;
5493 }
5494
5495 /// parseDIGlobalVariableExpression:
5496 /// ::= !DIGlobalVariableExpression(var: !0, expr: !1)
parseDIGlobalVariableExpression(MDNode * & Result,bool IsDistinct)5497 bool LLParser::parseDIGlobalVariableExpression(MDNode *&Result,
5498 bool IsDistinct) {
5499 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5500 REQUIRED(var, MDField, ); \
5501 REQUIRED(expr, MDField, );
5502 PARSE_MD_FIELDS();
5503 #undef VISIT_MD_FIELDS
5504
5505 Result =
5506 GET_OR_DISTINCT(DIGlobalVariableExpression, (Context, var.Val, expr.Val));
5507 return false;
5508 }
5509
5510 /// parseDIObjCProperty:
5511 /// ::= !DIObjCProperty(name: "foo", file: !1, line: 7, setter: "setFoo",
5512 /// getter: "getFoo", attributes: 7, type: !2)
parseDIObjCProperty(MDNode * & Result,bool IsDistinct)5513 bool LLParser::parseDIObjCProperty(MDNode *&Result, bool IsDistinct) {
5514 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5515 OPTIONAL(name, MDStringField, ); \
5516 OPTIONAL(file, MDField, ); \
5517 OPTIONAL(line, LineField, ); \
5518 OPTIONAL(setter, MDStringField, ); \
5519 OPTIONAL(getter, MDStringField, ); \
5520 OPTIONAL(attributes, MDUnsignedField, (0, UINT32_MAX)); \
5521 OPTIONAL(type, MDField, );
5522 PARSE_MD_FIELDS();
5523 #undef VISIT_MD_FIELDS
5524
5525 Result = GET_OR_DISTINCT(DIObjCProperty,
5526 (Context, name.Val, file.Val, line.Val, setter.Val,
5527 getter.Val, attributes.Val, type.Val));
5528 return false;
5529 }
5530
5531 /// parseDIImportedEntity:
5532 /// ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1,
5533 /// line: 7, name: "foo", elements: !2)
parseDIImportedEntity(MDNode * & Result,bool IsDistinct)5534 bool LLParser::parseDIImportedEntity(MDNode *&Result, bool IsDistinct) {
5535 #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5536 REQUIRED(tag, DwarfTagField, ); \
5537 REQUIRED(scope, MDField, ); \
5538 OPTIONAL(entity, MDField, ); \
5539 OPTIONAL(file, MDField, ); \
5540 OPTIONAL(line, LineField, ); \
5541 OPTIONAL(name, MDStringField, ); \
5542 OPTIONAL(elements, MDField, );
5543 PARSE_MD_FIELDS();
5544 #undef VISIT_MD_FIELDS
5545
5546 Result = GET_OR_DISTINCT(DIImportedEntity,
5547 (Context, tag.Val, scope.Val, entity.Val, file.Val,
5548 line.Val, name.Val, elements.Val));
5549 return false;
5550 }
5551
5552 #undef PARSE_MD_FIELD
5553 #undef NOP_FIELD
5554 #undef REQUIRE_FIELD
5555 #undef DECLARE_FIELD
5556
5557 /// parseMetadataAsValue
5558 /// ::= metadata i32 %local
5559 /// ::= metadata i32 @global
5560 /// ::= metadata i32 7
5561 /// ::= metadata !0
5562 /// ::= metadata !{...}
5563 /// ::= metadata !"string"
parseMetadataAsValue(Value * & V,PerFunctionState & PFS)5564 bool LLParser::parseMetadataAsValue(Value *&V, PerFunctionState &PFS) {
5565 // Note: the type 'metadata' has already been parsed.
5566 Metadata *MD;
5567 if (parseMetadata(MD, &PFS))
5568 return true;
5569
5570 V = MetadataAsValue::get(Context, MD);
5571 return false;
5572 }
5573
5574 /// parseValueAsMetadata
5575 /// ::= i32 %local
5576 /// ::= i32 @global
5577 /// ::= i32 7
parseValueAsMetadata(Metadata * & MD,const Twine & TypeMsg,PerFunctionState * PFS)5578 bool LLParser::parseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
5579 PerFunctionState *PFS) {
5580 Type *Ty;
5581 LocTy Loc;
5582 if (parseType(Ty, TypeMsg, Loc))
5583 return true;
5584 if (Ty->isMetadataTy())
5585 return error(Loc, "invalid metadata-value-metadata roundtrip");
5586
5587 Value *V;
5588 if (parseValue(Ty, V, PFS))
5589 return true;
5590
5591 MD = ValueAsMetadata::get(V);
5592 return false;
5593 }
5594
5595 /// parseMetadata
5596 /// ::= i32 %local
5597 /// ::= i32 @global
5598 /// ::= i32 7
5599 /// ::= !42
5600 /// ::= !{...}
5601 /// ::= !"string"
5602 /// ::= !DILocation(...)
parseMetadata(Metadata * & MD,PerFunctionState * PFS)5603 bool LLParser::parseMetadata(Metadata *&MD, PerFunctionState *PFS) {
5604 if (Lex.getKind() == lltok::MetadataVar) {
5605 MDNode *N;
5606 // DIArgLists are a special case, as they are a list of ValueAsMetadata and
5607 // so parsing this requires a Function State.
5608 if (Lex.getStrVal() == "DIArgList") {
5609 if (parseDIArgList(N, false, PFS))
5610 return true;
5611 } else if (parseSpecializedMDNode(N)) {
5612 return true;
5613 }
5614 MD = N;
5615 return false;
5616 }
5617
5618 // ValueAsMetadata:
5619 // <type> <value>
5620 if (Lex.getKind() != lltok::exclaim)
5621 return parseValueAsMetadata(MD, "expected metadata operand", PFS);
5622
5623 // '!'.
5624 assert(Lex.getKind() == lltok::exclaim && "Expected '!' here");
5625 Lex.Lex();
5626
5627 // MDString:
5628 // ::= '!' STRINGCONSTANT
5629 if (Lex.getKind() == lltok::StringConstant) {
5630 MDString *S;
5631 if (parseMDString(S))
5632 return true;
5633 MD = S;
5634 return false;
5635 }
5636
5637 // MDNode:
5638 // !{ ... }
5639 // !7
5640 MDNode *N;
5641 if (parseMDNodeTail(N))
5642 return true;
5643 MD = N;
5644 return false;
5645 }
5646
5647 //===----------------------------------------------------------------------===//
5648 // Function Parsing.
5649 //===----------------------------------------------------------------------===//
5650
convertValIDToValue(Type * Ty,ValID & ID,Value * & V,PerFunctionState * PFS)5651 bool LLParser::convertValIDToValue(Type *Ty, ValID &ID, Value *&V,
5652 PerFunctionState *PFS) {
5653 if (Ty->isFunctionTy())
5654 return error(ID.Loc, "functions are not values, refer to them as pointers");
5655
5656 switch (ID.Kind) {
5657 case ValID::t_LocalID:
5658 if (!PFS)
5659 return error(ID.Loc, "invalid use of function-local name");
5660 V = PFS->getVal(ID.UIntVal, Ty, ID.Loc);
5661 return V == nullptr;
5662 case ValID::t_LocalName:
5663 if (!PFS)
5664 return error(ID.Loc, "invalid use of function-local name");
5665 V = PFS->getVal(ID.StrVal, Ty, ID.Loc);
5666 return V == nullptr;
5667 case ValID::t_InlineAsm: {
5668 if (!ID.FTy)
5669 return error(ID.Loc, "invalid type for inline asm constraint string");
5670 if (Error Err = InlineAsm::verify(ID.FTy, ID.StrVal2))
5671 return error(ID.Loc, toString(std::move(Err)));
5672 V = InlineAsm::get(
5673 ID.FTy, ID.StrVal, ID.StrVal2, ID.UIntVal & 1, (ID.UIntVal >> 1) & 1,
5674 InlineAsm::AsmDialect((ID.UIntVal >> 2) & 1), (ID.UIntVal >> 3) & 1);
5675 return false;
5676 }
5677 case ValID::t_GlobalName:
5678 V = getGlobalVal(ID.StrVal, Ty, ID.Loc);
5679 if (V && ID.NoCFI)
5680 V = NoCFIValue::get(cast<GlobalValue>(V));
5681 return V == nullptr;
5682 case ValID::t_GlobalID:
5683 V = getGlobalVal(ID.UIntVal, Ty, ID.Loc);
5684 if (V && ID.NoCFI)
5685 V = NoCFIValue::get(cast<GlobalValue>(V));
5686 return V == nullptr;
5687 case ValID::t_APSInt:
5688 if (!Ty->isIntegerTy())
5689 return error(ID.Loc, "integer constant must have integer type");
5690 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
5691 V = ConstantInt::get(Context, ID.APSIntVal);
5692 return false;
5693 case ValID::t_APFloat:
5694 if (!Ty->isFloatingPointTy() ||
5695 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
5696 return error(ID.Loc, "floating point constant invalid for type");
5697
5698 // The lexer has no type info, so builds all half, bfloat, float, and double
5699 // FP constants as double. Fix this here. Long double does not need this.
5700 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble()) {
5701 // Check for signaling before potentially converting and losing that info.
5702 bool IsSNAN = ID.APFloatVal.isSignaling();
5703 bool Ignored;
5704 if (Ty->isHalfTy())
5705 ID.APFloatVal.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven,
5706 &Ignored);
5707 else if (Ty->isBFloatTy())
5708 ID.APFloatVal.convert(APFloat::BFloat(), APFloat::rmNearestTiesToEven,
5709 &Ignored);
5710 else if (Ty->isFloatTy())
5711 ID.APFloatVal.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
5712 &Ignored);
5713 if (IsSNAN) {
5714 // The convert call above may quiet an SNaN, so manufacture another
5715 // SNaN. The bitcast works because the payload (significand) parameter
5716 // is truncated to fit.
5717 APInt Payload = ID.APFloatVal.bitcastToAPInt();
5718 ID.APFloatVal = APFloat::getSNaN(ID.APFloatVal.getSemantics(),
5719 ID.APFloatVal.isNegative(), &Payload);
5720 }
5721 }
5722 V = ConstantFP::get(Context, ID.APFloatVal);
5723
5724 if (V->getType() != Ty)
5725 return error(ID.Loc, "floating point constant does not have type '" +
5726 getTypeString(Ty) + "'");
5727
5728 return false;
5729 case ValID::t_Null:
5730 if (!Ty->isPointerTy())
5731 return error(ID.Loc, "null must be a pointer type");
5732 V = ConstantPointerNull::get(cast<PointerType>(Ty));
5733 return false;
5734 case ValID::t_Undef:
5735 // FIXME: LabelTy should not be a first-class type.
5736 if (!Ty->isFirstClassType() || Ty->isLabelTy())
5737 return error(ID.Loc, "invalid type for undef constant");
5738 V = UndefValue::get(Ty);
5739 return false;
5740 case ValID::t_EmptyArray:
5741 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
5742 return error(ID.Loc, "invalid empty array initializer");
5743 V = UndefValue::get(Ty);
5744 return false;
5745 case ValID::t_Zero:
5746 // FIXME: LabelTy should not be a first-class type.
5747 if (!Ty->isFirstClassType() || Ty->isLabelTy())
5748 return error(ID.Loc, "invalid type for null constant");
5749 if (auto *TETy = dyn_cast<TargetExtType>(Ty))
5750 if (!TETy->hasProperty(TargetExtType::HasZeroInit))
5751 return error(ID.Loc, "invalid type for null constant");
5752 V = Constant::getNullValue(Ty);
5753 return false;
5754 case ValID::t_None:
5755 if (!Ty->isTokenTy())
5756 return error(ID.Loc, "invalid type for none constant");
5757 V = Constant::getNullValue(Ty);
5758 return false;
5759 case ValID::t_Poison:
5760 // FIXME: LabelTy should not be a first-class type.
5761 if (!Ty->isFirstClassType() || Ty->isLabelTy())
5762 return error(ID.Loc, "invalid type for poison constant");
5763 V = PoisonValue::get(Ty);
5764 return false;
5765 case ValID::t_Constant:
5766 if (ID.ConstantVal->getType() != Ty)
5767 return error(ID.Loc, "constant expression type mismatch: got type '" +
5768 getTypeString(ID.ConstantVal->getType()) +
5769 "' but expected '" + getTypeString(Ty) + "'");
5770 V = ID.ConstantVal;
5771 return false;
5772 case ValID::t_ConstantStruct:
5773 case ValID::t_PackedConstantStruct:
5774 if (StructType *ST = dyn_cast<StructType>(Ty)) {
5775 if (ST->getNumElements() != ID.UIntVal)
5776 return error(ID.Loc,
5777 "initializer with struct type has wrong # elements");
5778 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
5779 return error(ID.Loc, "packed'ness of initializer and type don't match");
5780
5781 // Verify that the elements are compatible with the structtype.
5782 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
5783 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
5784 return error(
5785 ID.Loc,
5786 "element " + Twine(i) +
5787 " of struct initializer doesn't match struct element type");
5788
5789 V = ConstantStruct::get(
5790 ST, ArrayRef(ID.ConstantStructElts.get(), ID.UIntVal));
5791 } else
5792 return error(ID.Loc, "constant expression type mismatch");
5793 return false;
5794 }
5795 llvm_unreachable("Invalid ValID");
5796 }
5797
parseConstantValue(Type * Ty,Constant * & C)5798 bool LLParser::parseConstantValue(Type *Ty, Constant *&C) {
5799 C = nullptr;
5800 ValID ID;
5801 auto Loc = Lex.getLoc();
5802 if (parseValID(ID, /*PFS=*/nullptr))
5803 return true;
5804 switch (ID.Kind) {
5805 case ValID::t_APSInt:
5806 case ValID::t_APFloat:
5807 case ValID::t_Undef:
5808 case ValID::t_Constant:
5809 case ValID::t_ConstantStruct:
5810 case ValID::t_PackedConstantStruct: {
5811 Value *V;
5812 if (convertValIDToValue(Ty, ID, V, /*PFS=*/nullptr))
5813 return true;
5814 assert(isa<Constant>(V) && "Expected a constant value");
5815 C = cast<Constant>(V);
5816 return false;
5817 }
5818 case ValID::t_Null:
5819 C = Constant::getNullValue(Ty);
5820 return false;
5821 default:
5822 return error(Loc, "expected a constant value");
5823 }
5824 }
5825
parseValue(Type * Ty,Value * & V,PerFunctionState * PFS)5826 bool LLParser::parseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
5827 V = nullptr;
5828 ValID ID;
5829 return parseValID(ID, PFS, Ty) ||
5830 convertValIDToValue(Ty, ID, V, PFS);
5831 }
5832
parseTypeAndValue(Value * & V,PerFunctionState * PFS)5833 bool LLParser::parseTypeAndValue(Value *&V, PerFunctionState *PFS) {
5834 Type *Ty = nullptr;
5835 return parseType(Ty) || parseValue(Ty, V, PFS);
5836 }
5837
parseTypeAndBasicBlock(BasicBlock * & BB,LocTy & Loc,PerFunctionState & PFS)5838 bool LLParser::parseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
5839 PerFunctionState &PFS) {
5840 Value *V;
5841 Loc = Lex.getLoc();
5842 if (parseTypeAndValue(V, PFS))
5843 return true;
5844 if (!isa<BasicBlock>(V))
5845 return error(Loc, "expected a basic block");
5846 BB = cast<BasicBlock>(V);
5847 return false;
5848 }
5849
5850 /// FunctionHeader
5851 /// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
5852 /// OptionalCallingConv OptRetAttrs OptUnnamedAddr Type GlobalName
5853 /// '(' ArgList ')' OptAddrSpace OptFuncAttrs OptSection OptionalAlign
5854 /// OptGC OptionalPrefix OptionalPrologue OptPersonalityFn
parseFunctionHeader(Function * & Fn,bool IsDefine)5855 bool LLParser::parseFunctionHeader(Function *&Fn, bool IsDefine) {
5856 // parse the linkage.
5857 LocTy LinkageLoc = Lex.getLoc();
5858 unsigned Linkage;
5859 unsigned Visibility;
5860 unsigned DLLStorageClass;
5861 bool DSOLocal;
5862 AttrBuilder RetAttrs(M->getContext());
5863 unsigned CC;
5864 bool HasLinkage;
5865 Type *RetType = nullptr;
5866 LocTy RetTypeLoc = Lex.getLoc();
5867 if (parseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
5868 DSOLocal) ||
5869 parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
5870 parseType(RetType, RetTypeLoc, true /*void allowed*/))
5871 return true;
5872
5873 // Verify that the linkage is ok.
5874 switch ((GlobalValue::LinkageTypes)Linkage) {
5875 case GlobalValue::ExternalLinkage:
5876 break; // always ok.
5877 case GlobalValue::ExternalWeakLinkage:
5878 if (IsDefine)
5879 return error(LinkageLoc, "invalid linkage for function definition");
5880 break;
5881 case GlobalValue::PrivateLinkage:
5882 case GlobalValue::InternalLinkage:
5883 case GlobalValue::AvailableExternallyLinkage:
5884 case GlobalValue::LinkOnceAnyLinkage:
5885 case GlobalValue::LinkOnceODRLinkage:
5886 case GlobalValue::WeakAnyLinkage:
5887 case GlobalValue::WeakODRLinkage:
5888 if (!IsDefine)
5889 return error(LinkageLoc, "invalid linkage for function declaration");
5890 break;
5891 case GlobalValue::AppendingLinkage:
5892 case GlobalValue::CommonLinkage:
5893 return error(LinkageLoc, "invalid function linkage type");
5894 }
5895
5896 if (!isValidVisibilityForLinkage(Visibility, Linkage))
5897 return error(LinkageLoc,
5898 "symbol with local linkage must have default visibility");
5899
5900 if (!isValidDLLStorageClassForLinkage(DLLStorageClass, Linkage))
5901 return error(LinkageLoc,
5902 "symbol with local linkage cannot have a DLL storage class");
5903
5904 if (!FunctionType::isValidReturnType(RetType))
5905 return error(RetTypeLoc, "invalid function return type");
5906
5907 LocTy NameLoc = Lex.getLoc();
5908
5909 std::string FunctionName;
5910 if (Lex.getKind() == lltok::GlobalVar) {
5911 FunctionName = Lex.getStrVal();
5912 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
5913 unsigned NameID = Lex.getUIntVal();
5914
5915 if (NameID != NumberedVals.size())
5916 return tokError("function expected to be numbered '%" +
5917 Twine(NumberedVals.size()) + "'");
5918 } else {
5919 return tokError("expected function name");
5920 }
5921
5922 Lex.Lex();
5923
5924 if (Lex.getKind() != lltok::lparen)
5925 return tokError("expected '(' in function argument list");
5926
5927 SmallVector<ArgInfo, 8> ArgList;
5928 bool IsVarArg;
5929 AttrBuilder FuncAttrs(M->getContext());
5930 std::vector<unsigned> FwdRefAttrGrps;
5931 LocTy BuiltinLoc;
5932 std::string Section;
5933 std::string Partition;
5934 MaybeAlign Alignment;
5935 std::string GC;
5936 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
5937 unsigned AddrSpace = 0;
5938 Constant *Prefix = nullptr;
5939 Constant *Prologue = nullptr;
5940 Constant *PersonalityFn = nullptr;
5941 Comdat *C;
5942
5943 if (parseArgumentList(ArgList, IsVarArg) ||
5944 parseOptionalUnnamedAddr(UnnamedAddr) ||
5945 parseOptionalProgramAddrSpace(AddrSpace) ||
5946 parseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
5947 BuiltinLoc) ||
5948 (EatIfPresent(lltok::kw_section) && parseStringConstant(Section)) ||
5949 (EatIfPresent(lltok::kw_partition) && parseStringConstant(Partition)) ||
5950 parseOptionalComdat(FunctionName, C) ||
5951 parseOptionalAlignment(Alignment) ||
5952 (EatIfPresent(lltok::kw_gc) && parseStringConstant(GC)) ||
5953 (EatIfPresent(lltok::kw_prefix) && parseGlobalTypeAndValue(Prefix)) ||
5954 (EatIfPresent(lltok::kw_prologue) && parseGlobalTypeAndValue(Prologue)) ||
5955 (EatIfPresent(lltok::kw_personality) &&
5956 parseGlobalTypeAndValue(PersonalityFn)))
5957 return true;
5958
5959 if (FuncAttrs.contains(Attribute::Builtin))
5960 return error(BuiltinLoc, "'builtin' attribute not valid on function");
5961
5962 // If the alignment was parsed as an attribute, move to the alignment field.
5963 if (MaybeAlign A = FuncAttrs.getAlignment()) {
5964 Alignment = A;
5965 FuncAttrs.removeAttribute(Attribute::Alignment);
5966 }
5967
5968 // Okay, if we got here, the function is syntactically valid. Convert types
5969 // and do semantic checks.
5970 std::vector<Type*> ParamTypeList;
5971 SmallVector<AttributeSet, 8> Attrs;
5972
5973 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
5974 ParamTypeList.push_back(ArgList[i].Ty);
5975 Attrs.push_back(ArgList[i].Attrs);
5976 }
5977
5978 AttributeList PAL =
5979 AttributeList::get(Context, AttributeSet::get(Context, FuncAttrs),
5980 AttributeSet::get(Context, RetAttrs), Attrs);
5981
5982 if (PAL.hasParamAttr(0, Attribute::StructRet) && !RetType->isVoidTy())
5983 return error(RetTypeLoc, "functions with 'sret' argument must return void");
5984
5985 FunctionType *FT = FunctionType::get(RetType, ParamTypeList, IsVarArg);
5986 PointerType *PFT = PointerType::get(FT, AddrSpace);
5987
5988 Fn = nullptr;
5989 GlobalValue *FwdFn = nullptr;
5990 if (!FunctionName.empty()) {
5991 // If this was a definition of a forward reference, remove the definition
5992 // from the forward reference table and fill in the forward ref.
5993 auto FRVI = ForwardRefVals.find(FunctionName);
5994 if (FRVI != ForwardRefVals.end()) {
5995 FwdFn = FRVI->second.first;
5996 if (!FwdFn->getType()->isOpaque() &&
5997 !FwdFn->getType()->getNonOpaquePointerElementType()->isFunctionTy())
5998 return error(FRVI->second.second, "invalid forward reference to "
5999 "function as global value!");
6000 if (FwdFn->getType() != PFT)
6001 return error(FRVI->second.second,
6002 "invalid forward reference to "
6003 "function '" +
6004 FunctionName +
6005 "' with wrong type: "
6006 "expected '" +
6007 getTypeString(PFT) + "' but was '" +
6008 getTypeString(FwdFn->getType()) + "'");
6009 ForwardRefVals.erase(FRVI);
6010 } else if ((Fn = M->getFunction(FunctionName))) {
6011 // Reject redefinitions.
6012 return error(NameLoc,
6013 "invalid redefinition of function '" + FunctionName + "'");
6014 } else if (M->getNamedValue(FunctionName)) {
6015 return error(NameLoc, "redefinition of function '@" + FunctionName + "'");
6016 }
6017
6018 } else {
6019 // If this is a definition of a forward referenced function, make sure the
6020 // types agree.
6021 auto I = ForwardRefValIDs.find(NumberedVals.size());
6022 if (I != ForwardRefValIDs.end()) {
6023 FwdFn = I->second.first;
6024 if (FwdFn->getType() != PFT)
6025 return error(NameLoc, "type of definition and forward reference of '@" +
6026 Twine(NumberedVals.size()) +
6027 "' disagree: "
6028 "expected '" +
6029 getTypeString(PFT) + "' but was '" +
6030 getTypeString(FwdFn->getType()) + "'");
6031 ForwardRefValIDs.erase(I);
6032 }
6033 }
6034
6035 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, AddrSpace,
6036 FunctionName, M);
6037
6038 assert(Fn->getAddressSpace() == AddrSpace && "Created function in wrong AS");
6039
6040 if (FunctionName.empty())
6041 NumberedVals.push_back(Fn);
6042
6043 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
6044 maybeSetDSOLocal(DSOLocal, *Fn);
6045 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
6046 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
6047 Fn->setCallingConv(CC);
6048 Fn->setAttributes(PAL);
6049 Fn->setUnnamedAddr(UnnamedAddr);
6050 Fn->setAlignment(MaybeAlign(Alignment));
6051 Fn->setSection(Section);
6052 Fn->setPartition(Partition);
6053 Fn->setComdat(C);
6054 Fn->setPersonalityFn(PersonalityFn);
6055 if (!GC.empty()) Fn->setGC(GC);
6056 Fn->setPrefixData(Prefix);
6057 Fn->setPrologueData(Prologue);
6058 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
6059
6060 // Add all of the arguments we parsed to the function.
6061 Function::arg_iterator ArgIt = Fn->arg_begin();
6062 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
6063 // If the argument has a name, insert it into the argument symbol table.
6064 if (ArgList[i].Name.empty()) continue;
6065
6066 // Set the name, if it conflicted, it will be auto-renamed.
6067 ArgIt->setName(ArgList[i].Name);
6068
6069 if (ArgIt->getName() != ArgList[i].Name)
6070 return error(ArgList[i].Loc,
6071 "redefinition of argument '%" + ArgList[i].Name + "'");
6072 }
6073
6074 if (FwdFn) {
6075 FwdFn->replaceAllUsesWith(Fn);
6076 FwdFn->eraseFromParent();
6077 }
6078
6079 if (IsDefine)
6080 return false;
6081
6082 // Check the declaration has no block address forward references.
6083 ValID ID;
6084 if (FunctionName.empty()) {
6085 ID.Kind = ValID::t_GlobalID;
6086 ID.UIntVal = NumberedVals.size() - 1;
6087 } else {
6088 ID.Kind = ValID::t_GlobalName;
6089 ID.StrVal = FunctionName;
6090 }
6091 auto Blocks = ForwardRefBlockAddresses.find(ID);
6092 if (Blocks != ForwardRefBlockAddresses.end())
6093 return error(Blocks->first.Loc,
6094 "cannot take blockaddress inside a declaration");
6095 return false;
6096 }
6097
resolveForwardRefBlockAddresses()6098 bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() {
6099 ValID ID;
6100 if (FunctionNumber == -1) {
6101 ID.Kind = ValID::t_GlobalName;
6102 ID.StrVal = std::string(F.getName());
6103 } else {
6104 ID.Kind = ValID::t_GlobalID;
6105 ID.UIntVal = FunctionNumber;
6106 }
6107
6108 auto Blocks = P.ForwardRefBlockAddresses.find(ID);
6109 if (Blocks == P.ForwardRefBlockAddresses.end())
6110 return false;
6111
6112 for (const auto &I : Blocks->second) {
6113 const ValID &BBID = I.first;
6114 GlobalValue *GV = I.second;
6115
6116 assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) &&
6117 "Expected local id or name");
6118 BasicBlock *BB;
6119 if (BBID.Kind == ValID::t_LocalName)
6120 BB = getBB(BBID.StrVal, BBID.Loc);
6121 else
6122 BB = getBB(BBID.UIntVal, BBID.Loc);
6123 if (!BB)
6124 return P.error(BBID.Loc, "referenced value is not a basic block");
6125
6126 Value *ResolvedVal = BlockAddress::get(&F, BB);
6127 ResolvedVal = P.checkValidVariableType(BBID.Loc, BBID.StrVal, GV->getType(),
6128 ResolvedVal);
6129 if (!ResolvedVal)
6130 return true;
6131 GV->replaceAllUsesWith(ResolvedVal);
6132 GV->eraseFromParent();
6133 }
6134
6135 P.ForwardRefBlockAddresses.erase(Blocks);
6136 return false;
6137 }
6138
6139 /// parseFunctionBody
6140 /// ::= '{' BasicBlock+ UseListOrderDirective* '}'
parseFunctionBody(Function & Fn)6141 bool LLParser::parseFunctionBody(Function &Fn) {
6142 if (Lex.getKind() != lltok::lbrace)
6143 return tokError("expected '{' in function body");
6144 Lex.Lex(); // eat the {.
6145
6146 int FunctionNumber = -1;
6147 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
6148
6149 PerFunctionState PFS(*this, Fn, FunctionNumber);
6150
6151 // Resolve block addresses and allow basic blocks to be forward-declared
6152 // within this function.
6153 if (PFS.resolveForwardRefBlockAddresses())
6154 return true;
6155 SaveAndRestore ScopeExit(BlockAddressPFS, &PFS);
6156
6157 // We need at least one basic block.
6158 if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder)
6159 return tokError("function body requires at least one basic block");
6160
6161 while (Lex.getKind() != lltok::rbrace &&
6162 Lex.getKind() != lltok::kw_uselistorder)
6163 if (parseBasicBlock(PFS))
6164 return true;
6165
6166 while (Lex.getKind() != lltok::rbrace)
6167 if (parseUseListOrder(&PFS))
6168 return true;
6169
6170 // Eat the }.
6171 Lex.Lex();
6172
6173 // Verify function is ok.
6174 return PFS.finishFunction();
6175 }
6176
6177 /// parseBasicBlock
6178 /// ::= (LabelStr|LabelID)? Instruction*
parseBasicBlock(PerFunctionState & PFS)6179 bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
6180 // If this basic block starts out with a name, remember it.
6181 std::string Name;
6182 int NameID = -1;
6183 LocTy NameLoc = Lex.getLoc();
6184 if (Lex.getKind() == lltok::LabelStr) {
6185 Name = Lex.getStrVal();
6186 Lex.Lex();
6187 } else if (Lex.getKind() == lltok::LabelID) {
6188 NameID = Lex.getUIntVal();
6189 Lex.Lex();
6190 }
6191
6192 BasicBlock *BB = PFS.defineBB(Name, NameID, NameLoc);
6193 if (!BB)
6194 return true;
6195
6196 std::string NameStr;
6197
6198 // parse the instructions in this block until we get a terminator.
6199 Instruction *Inst;
6200 do {
6201 // This instruction may have three possibilities for a name: a) none
6202 // specified, b) name specified "%foo =", c) number specified: "%4 =".
6203 LocTy NameLoc = Lex.getLoc();
6204 int NameID = -1;
6205 NameStr = "";
6206
6207 if (Lex.getKind() == lltok::LocalVarID) {
6208 NameID = Lex.getUIntVal();
6209 Lex.Lex();
6210 if (parseToken(lltok::equal, "expected '=' after instruction id"))
6211 return true;
6212 } else if (Lex.getKind() == lltok::LocalVar) {
6213 NameStr = Lex.getStrVal();
6214 Lex.Lex();
6215 if (parseToken(lltok::equal, "expected '=' after instruction name"))
6216 return true;
6217 }
6218
6219 switch (parseInstruction(Inst, BB, PFS)) {
6220 default:
6221 llvm_unreachable("Unknown parseInstruction result!");
6222 case InstError: return true;
6223 case InstNormal:
6224 Inst->insertInto(BB, BB->end());
6225
6226 // With a normal result, we check to see if the instruction is followed by
6227 // a comma and metadata.
6228 if (EatIfPresent(lltok::comma))
6229 if (parseInstructionMetadata(*Inst))
6230 return true;
6231 break;
6232 case InstExtraComma:
6233 Inst->insertInto(BB, BB->end());
6234
6235 // If the instruction parser ate an extra comma at the end of it, it
6236 // *must* be followed by metadata.
6237 if (parseInstructionMetadata(*Inst))
6238 return true;
6239 break;
6240 }
6241
6242 // Set the name on the instruction.
6243 if (PFS.setInstName(NameID, NameStr, NameLoc, Inst))
6244 return true;
6245 } while (!Inst->isTerminator());
6246
6247 return false;
6248 }
6249
6250 //===----------------------------------------------------------------------===//
6251 // Instruction Parsing.
6252 //===----------------------------------------------------------------------===//
6253
6254 /// parseInstruction - parse one of the many different instructions.
6255 ///
parseInstruction(Instruction * & Inst,BasicBlock * BB,PerFunctionState & PFS)6256 int LLParser::parseInstruction(Instruction *&Inst, BasicBlock *BB,
6257 PerFunctionState &PFS) {
6258 lltok::Kind Token = Lex.getKind();
6259 if (Token == lltok::Eof)
6260 return tokError("found end of file when expecting more instructions");
6261 LocTy Loc = Lex.getLoc();
6262 unsigned KeywordVal = Lex.getUIntVal();
6263 Lex.Lex(); // Eat the keyword.
6264
6265 switch (Token) {
6266 default:
6267 return error(Loc, "expected instruction opcode");
6268 // Terminator Instructions.
6269 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
6270 case lltok::kw_ret:
6271 return parseRet(Inst, BB, PFS);
6272 case lltok::kw_br:
6273 return parseBr(Inst, PFS);
6274 case lltok::kw_switch:
6275 return parseSwitch(Inst, PFS);
6276 case lltok::kw_indirectbr:
6277 return parseIndirectBr(Inst, PFS);
6278 case lltok::kw_invoke:
6279 return parseInvoke(Inst, PFS);
6280 case lltok::kw_resume:
6281 return parseResume(Inst, PFS);
6282 case lltok::kw_cleanupret:
6283 return parseCleanupRet(Inst, PFS);
6284 case lltok::kw_catchret:
6285 return parseCatchRet(Inst, PFS);
6286 case lltok::kw_catchswitch:
6287 return parseCatchSwitch(Inst, PFS);
6288 case lltok::kw_catchpad:
6289 return parseCatchPad(Inst, PFS);
6290 case lltok::kw_cleanuppad:
6291 return parseCleanupPad(Inst, PFS);
6292 case lltok::kw_callbr:
6293 return parseCallBr(Inst, PFS);
6294 // Unary Operators.
6295 case lltok::kw_fneg: {
6296 FastMathFlags FMF = EatFastMathFlagsIfPresent();
6297 int Res = parseUnaryOp(Inst, PFS, KeywordVal, /*IsFP*/ true);
6298 if (Res != 0)
6299 return Res;
6300 if (FMF.any())
6301 Inst->setFastMathFlags(FMF);
6302 return false;
6303 }
6304 // Binary Operators.
6305 case lltok::kw_add:
6306 case lltok::kw_sub:
6307 case lltok::kw_mul:
6308 case lltok::kw_shl: {
6309 bool NUW = EatIfPresent(lltok::kw_nuw);
6310 bool NSW = EatIfPresent(lltok::kw_nsw);
6311 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
6312
6313 if (parseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/ false))
6314 return true;
6315
6316 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
6317 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
6318 return false;
6319 }
6320 case lltok::kw_fadd:
6321 case lltok::kw_fsub:
6322 case lltok::kw_fmul:
6323 case lltok::kw_fdiv:
6324 case lltok::kw_frem: {
6325 FastMathFlags FMF = EatFastMathFlagsIfPresent();
6326 int Res = parseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/ true);
6327 if (Res != 0)
6328 return Res;
6329 if (FMF.any())
6330 Inst->setFastMathFlags(FMF);
6331 return 0;
6332 }
6333
6334 case lltok::kw_sdiv:
6335 case lltok::kw_udiv:
6336 case lltok::kw_lshr:
6337 case lltok::kw_ashr: {
6338 bool Exact = EatIfPresent(lltok::kw_exact);
6339
6340 if (parseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/ false))
6341 return true;
6342 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
6343 return false;
6344 }
6345
6346 case lltok::kw_urem:
6347 case lltok::kw_srem:
6348 return parseArithmetic(Inst, PFS, KeywordVal,
6349 /*IsFP*/ false);
6350 case lltok::kw_and:
6351 case lltok::kw_or:
6352 case lltok::kw_xor:
6353 return parseLogical(Inst, PFS, KeywordVal);
6354 case lltok::kw_icmp:
6355 return parseCompare(Inst, PFS, KeywordVal);
6356 case lltok::kw_fcmp: {
6357 FastMathFlags FMF = EatFastMathFlagsIfPresent();
6358 int Res = parseCompare(Inst, PFS, KeywordVal);
6359 if (Res != 0)
6360 return Res;
6361 if (FMF.any())
6362 Inst->setFastMathFlags(FMF);
6363 return 0;
6364 }
6365
6366 // Casts.
6367 case lltok::kw_trunc:
6368 case lltok::kw_zext:
6369 case lltok::kw_sext:
6370 case lltok::kw_fptrunc:
6371 case lltok::kw_fpext:
6372 case lltok::kw_bitcast:
6373 case lltok::kw_addrspacecast:
6374 case lltok::kw_uitofp:
6375 case lltok::kw_sitofp:
6376 case lltok::kw_fptoui:
6377 case lltok::kw_fptosi:
6378 case lltok::kw_inttoptr:
6379 case lltok::kw_ptrtoint:
6380 return parseCast(Inst, PFS, KeywordVal);
6381 // Other.
6382 case lltok::kw_select: {
6383 FastMathFlags FMF = EatFastMathFlagsIfPresent();
6384 int Res = parseSelect(Inst, PFS);
6385 if (Res != 0)
6386 return Res;
6387 if (FMF.any()) {
6388 if (!isa<FPMathOperator>(Inst))
6389 return error(Loc, "fast-math-flags specified for select without "
6390 "floating-point scalar or vector return type");
6391 Inst->setFastMathFlags(FMF);
6392 }
6393 return 0;
6394 }
6395 case lltok::kw_va_arg:
6396 return parseVAArg(Inst, PFS);
6397 case lltok::kw_extractelement:
6398 return parseExtractElement(Inst, PFS);
6399 case lltok::kw_insertelement:
6400 return parseInsertElement(Inst, PFS);
6401 case lltok::kw_shufflevector:
6402 return parseShuffleVector(Inst, PFS);
6403 case lltok::kw_phi: {
6404 FastMathFlags FMF = EatFastMathFlagsIfPresent();
6405 int Res = parsePHI(Inst, PFS);
6406 if (Res != 0)
6407 return Res;
6408 if (FMF.any()) {
6409 if (!isa<FPMathOperator>(Inst))
6410 return error(Loc, "fast-math-flags specified for phi without "
6411 "floating-point scalar or vector return type");
6412 Inst->setFastMathFlags(FMF);
6413 }
6414 return 0;
6415 }
6416 case lltok::kw_landingpad:
6417 return parseLandingPad(Inst, PFS);
6418 case lltok::kw_freeze:
6419 return parseFreeze(Inst, PFS);
6420 // Call.
6421 case lltok::kw_call:
6422 return parseCall(Inst, PFS, CallInst::TCK_None);
6423 case lltok::kw_tail:
6424 return parseCall(Inst, PFS, CallInst::TCK_Tail);
6425 case lltok::kw_musttail:
6426 return parseCall(Inst, PFS, CallInst::TCK_MustTail);
6427 case lltok::kw_notail:
6428 return parseCall(Inst, PFS, CallInst::TCK_NoTail);
6429 // Memory.
6430 case lltok::kw_alloca:
6431 return parseAlloc(Inst, PFS);
6432 case lltok::kw_load:
6433 return parseLoad(Inst, PFS);
6434 case lltok::kw_store:
6435 return parseStore(Inst, PFS);
6436 case lltok::kw_cmpxchg:
6437 return parseCmpXchg(Inst, PFS);
6438 case lltok::kw_atomicrmw:
6439 return parseAtomicRMW(Inst, PFS);
6440 case lltok::kw_fence:
6441 return parseFence(Inst, PFS);
6442 case lltok::kw_getelementptr:
6443 return parseGetElementPtr(Inst, PFS);
6444 case lltok::kw_extractvalue:
6445 return parseExtractValue(Inst, PFS);
6446 case lltok::kw_insertvalue:
6447 return parseInsertValue(Inst, PFS);
6448 }
6449 }
6450
6451 /// parseCmpPredicate - parse an integer or fp predicate, based on Kind.
parseCmpPredicate(unsigned & P,unsigned Opc)6452 bool LLParser::parseCmpPredicate(unsigned &P, unsigned Opc) {
6453 if (Opc == Instruction::FCmp) {
6454 switch (Lex.getKind()) {
6455 default:
6456 return tokError("expected fcmp predicate (e.g. 'oeq')");
6457 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
6458 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
6459 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
6460 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
6461 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
6462 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
6463 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
6464 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
6465 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
6466 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
6467 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
6468 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
6469 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
6470 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
6471 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
6472 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
6473 }
6474 } else {
6475 switch (Lex.getKind()) {
6476 default:
6477 return tokError("expected icmp predicate (e.g. 'eq')");
6478 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
6479 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
6480 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
6481 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
6482 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
6483 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
6484 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
6485 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
6486 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
6487 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
6488 }
6489 }
6490 Lex.Lex();
6491 return false;
6492 }
6493
6494 //===----------------------------------------------------------------------===//
6495 // Terminator Instructions.
6496 //===----------------------------------------------------------------------===//
6497
6498 /// parseRet - parse a return instruction.
6499 /// ::= 'ret' void (',' !dbg, !1)*
6500 /// ::= 'ret' TypeAndValue (',' !dbg, !1)*
parseRet(Instruction * & Inst,BasicBlock * BB,PerFunctionState & PFS)6501 bool LLParser::parseRet(Instruction *&Inst, BasicBlock *BB,
6502 PerFunctionState &PFS) {
6503 SMLoc TypeLoc = Lex.getLoc();
6504 Type *Ty = nullptr;
6505 if (parseType(Ty, true /*void allowed*/))
6506 return true;
6507
6508 Type *ResType = PFS.getFunction().getReturnType();
6509
6510 if (Ty->isVoidTy()) {
6511 if (!ResType->isVoidTy())
6512 return error(TypeLoc, "value doesn't match function result type '" +
6513 getTypeString(ResType) + "'");
6514
6515 Inst = ReturnInst::Create(Context);
6516 return false;
6517 }
6518
6519 Value *RV;
6520 if (parseValue(Ty, RV, PFS))
6521 return true;
6522
6523 if (ResType != RV->getType())
6524 return error(TypeLoc, "value doesn't match function result type '" +
6525 getTypeString(ResType) + "'");
6526
6527 Inst = ReturnInst::Create(Context, RV);
6528 return false;
6529 }
6530
6531 /// parseBr
6532 /// ::= 'br' TypeAndValue
6533 /// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
parseBr(Instruction * & Inst,PerFunctionState & PFS)6534 bool LLParser::parseBr(Instruction *&Inst, PerFunctionState &PFS) {
6535 LocTy Loc, Loc2;
6536 Value *Op0;
6537 BasicBlock *Op1, *Op2;
6538 if (parseTypeAndValue(Op0, Loc, PFS))
6539 return true;
6540
6541 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
6542 Inst = BranchInst::Create(BB);
6543 return false;
6544 }
6545
6546 if (Op0->getType() != Type::getInt1Ty(Context))
6547 return error(Loc, "branch condition must have 'i1' type");
6548
6549 if (parseToken(lltok::comma, "expected ',' after branch condition") ||
6550 parseTypeAndBasicBlock(Op1, Loc, PFS) ||
6551 parseToken(lltok::comma, "expected ',' after true destination") ||
6552 parseTypeAndBasicBlock(Op2, Loc2, PFS))
6553 return true;
6554
6555 Inst = BranchInst::Create(Op1, Op2, Op0);
6556 return false;
6557 }
6558
6559 /// parseSwitch
6560 /// Instruction
6561 /// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
6562 /// JumpTable
6563 /// ::= (TypeAndValue ',' TypeAndValue)*
parseSwitch(Instruction * & Inst,PerFunctionState & PFS)6564 bool LLParser::parseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
6565 LocTy CondLoc, BBLoc;
6566 Value *Cond;
6567 BasicBlock *DefaultBB;
6568 if (parseTypeAndValue(Cond, CondLoc, PFS) ||
6569 parseToken(lltok::comma, "expected ',' after switch condition") ||
6570 parseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
6571 parseToken(lltok::lsquare, "expected '[' with switch table"))
6572 return true;
6573
6574 if (!Cond->getType()->isIntegerTy())
6575 return error(CondLoc, "switch condition must have integer type");
6576
6577 // parse the jump table pairs.
6578 SmallPtrSet<Value*, 32> SeenCases;
6579 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
6580 while (Lex.getKind() != lltok::rsquare) {
6581 Value *Constant;
6582 BasicBlock *DestBB;
6583
6584 if (parseTypeAndValue(Constant, CondLoc, PFS) ||
6585 parseToken(lltok::comma, "expected ',' after case value") ||
6586 parseTypeAndBasicBlock(DestBB, PFS))
6587 return true;
6588
6589 if (!SeenCases.insert(Constant).second)
6590 return error(CondLoc, "duplicate case value in switch");
6591 if (!isa<ConstantInt>(Constant))
6592 return error(CondLoc, "case value is not a constant integer");
6593
6594 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
6595 }
6596
6597 Lex.Lex(); // Eat the ']'.
6598
6599 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
6600 for (unsigned i = 0, e = Table.size(); i != e; ++i)
6601 SI->addCase(Table[i].first, Table[i].second);
6602 Inst = SI;
6603 return false;
6604 }
6605
6606 /// parseIndirectBr
6607 /// Instruction
6608 /// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
parseIndirectBr(Instruction * & Inst,PerFunctionState & PFS)6609 bool LLParser::parseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
6610 LocTy AddrLoc;
6611 Value *Address;
6612 if (parseTypeAndValue(Address, AddrLoc, PFS) ||
6613 parseToken(lltok::comma, "expected ',' after indirectbr address") ||
6614 parseToken(lltok::lsquare, "expected '[' with indirectbr"))
6615 return true;
6616
6617 if (!Address->getType()->isPointerTy())
6618 return error(AddrLoc, "indirectbr address must have pointer type");
6619
6620 // parse the destination list.
6621 SmallVector<BasicBlock*, 16> DestList;
6622
6623 if (Lex.getKind() != lltok::rsquare) {
6624 BasicBlock *DestBB;
6625 if (parseTypeAndBasicBlock(DestBB, PFS))
6626 return true;
6627 DestList.push_back(DestBB);
6628
6629 while (EatIfPresent(lltok::comma)) {
6630 if (parseTypeAndBasicBlock(DestBB, PFS))
6631 return true;
6632 DestList.push_back(DestBB);
6633 }
6634 }
6635
6636 if (parseToken(lltok::rsquare, "expected ']' at end of block list"))
6637 return true;
6638
6639 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
6640 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
6641 IBI->addDestination(DestList[i]);
6642 Inst = IBI;
6643 return false;
6644 }
6645
6646 // If RetType is a non-function pointer type, then this is the short syntax
6647 // for the call, which means that RetType is just the return type. Infer the
6648 // rest of the function argument types from the arguments that are present.
resolveFunctionType(Type * RetType,const SmallVector<ParamInfo,16> & ArgList,FunctionType * & FuncTy)6649 bool LLParser::resolveFunctionType(Type *RetType,
6650 const SmallVector<ParamInfo, 16> &ArgList,
6651 FunctionType *&FuncTy) {
6652 FuncTy = dyn_cast<FunctionType>(RetType);
6653 if (!FuncTy) {
6654 // Pull out the types of all of the arguments...
6655 std::vector<Type*> ParamTypes;
6656 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
6657 ParamTypes.push_back(ArgList[i].V->getType());
6658
6659 if (!FunctionType::isValidReturnType(RetType))
6660 return true;
6661
6662 FuncTy = FunctionType::get(RetType, ParamTypes, false);
6663 }
6664 return false;
6665 }
6666
6667 /// parseInvoke
6668 /// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
6669 /// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
parseInvoke(Instruction * & Inst,PerFunctionState & PFS)6670 bool LLParser::parseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
6671 LocTy CallLoc = Lex.getLoc();
6672 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
6673 std::vector<unsigned> FwdRefAttrGrps;
6674 LocTy NoBuiltinLoc;
6675 unsigned CC;
6676 unsigned InvokeAddrSpace;
6677 Type *RetType = nullptr;
6678 LocTy RetTypeLoc;
6679 ValID CalleeID;
6680 SmallVector<ParamInfo, 16> ArgList;
6681 SmallVector<OperandBundleDef, 2> BundleList;
6682
6683 BasicBlock *NormalBB, *UnwindBB;
6684 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
6685 parseOptionalProgramAddrSpace(InvokeAddrSpace) ||
6686 parseType(RetType, RetTypeLoc, true /*void allowed*/) ||
6687 parseValID(CalleeID, &PFS) || parseParameterList(ArgList, PFS) ||
6688 parseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
6689 NoBuiltinLoc) ||
6690 parseOptionalOperandBundles(BundleList, PFS) ||
6691 parseToken(lltok::kw_to, "expected 'to' in invoke") ||
6692 parseTypeAndBasicBlock(NormalBB, PFS) ||
6693 parseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
6694 parseTypeAndBasicBlock(UnwindBB, PFS))
6695 return true;
6696
6697 // If RetType is a non-function pointer type, then this is the short syntax
6698 // for the call, which means that RetType is just the return type. Infer the
6699 // rest of the function argument types from the arguments that are present.
6700 FunctionType *Ty;
6701 if (resolveFunctionType(RetType, ArgList, Ty))
6702 return error(RetTypeLoc, "Invalid result type for LLVM function");
6703
6704 CalleeID.FTy = Ty;
6705
6706 // Look up the callee.
6707 Value *Callee;
6708 if (convertValIDToValue(PointerType::get(Ty, InvokeAddrSpace), CalleeID,
6709 Callee, &PFS))
6710 return true;
6711
6712 // Set up the Attribute for the function.
6713 SmallVector<Value *, 8> Args;
6714 SmallVector<AttributeSet, 8> ArgAttrs;
6715
6716 // Loop through FunctionType's arguments and ensure they are specified
6717 // correctly. Also, gather any parameter attributes.
6718 FunctionType::param_iterator I = Ty->param_begin();
6719 FunctionType::param_iterator E = Ty->param_end();
6720 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
6721 Type *ExpectedTy = nullptr;
6722 if (I != E) {
6723 ExpectedTy = *I++;
6724 } else if (!Ty->isVarArg()) {
6725 return error(ArgList[i].Loc, "too many arguments specified");
6726 }
6727
6728 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
6729 return error(ArgList[i].Loc, "argument is not of expected type '" +
6730 getTypeString(ExpectedTy) + "'");
6731 Args.push_back(ArgList[i].V);
6732 ArgAttrs.push_back(ArgList[i].Attrs);
6733 }
6734
6735 if (I != E)
6736 return error(CallLoc, "not enough parameters specified for call");
6737
6738 // Finish off the Attribute and check them
6739 AttributeList PAL =
6740 AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
6741 AttributeSet::get(Context, RetAttrs), ArgAttrs);
6742
6743 InvokeInst *II =
6744 InvokeInst::Create(Ty, Callee, NormalBB, UnwindBB, Args, BundleList);
6745 II->setCallingConv(CC);
6746 II->setAttributes(PAL);
6747 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
6748 Inst = II;
6749 return false;
6750 }
6751
6752 /// parseResume
6753 /// ::= 'resume' TypeAndValue
parseResume(Instruction * & Inst,PerFunctionState & PFS)6754 bool LLParser::parseResume(Instruction *&Inst, PerFunctionState &PFS) {
6755 Value *Exn; LocTy ExnLoc;
6756 if (parseTypeAndValue(Exn, ExnLoc, PFS))
6757 return true;
6758
6759 ResumeInst *RI = ResumeInst::Create(Exn);
6760 Inst = RI;
6761 return false;
6762 }
6763
parseExceptionArgs(SmallVectorImpl<Value * > & Args,PerFunctionState & PFS)6764 bool LLParser::parseExceptionArgs(SmallVectorImpl<Value *> &Args,
6765 PerFunctionState &PFS) {
6766 if (parseToken(lltok::lsquare, "expected '[' in catchpad/cleanuppad"))
6767 return true;
6768
6769 while (Lex.getKind() != lltok::rsquare) {
6770 // If this isn't the first argument, we need a comma.
6771 if (!Args.empty() &&
6772 parseToken(lltok::comma, "expected ',' in argument list"))
6773 return true;
6774
6775 // parse the argument.
6776 LocTy ArgLoc;
6777 Type *ArgTy = nullptr;
6778 if (parseType(ArgTy, ArgLoc))
6779 return true;
6780
6781 Value *V;
6782 if (ArgTy->isMetadataTy()) {
6783 if (parseMetadataAsValue(V, PFS))
6784 return true;
6785 } else {
6786 if (parseValue(ArgTy, V, PFS))
6787 return true;
6788 }
6789 Args.push_back(V);
6790 }
6791
6792 Lex.Lex(); // Lex the ']'.
6793 return false;
6794 }
6795
6796 /// parseCleanupRet
6797 /// ::= 'cleanupret' from Value unwind ('to' 'caller' | TypeAndValue)
parseCleanupRet(Instruction * & Inst,PerFunctionState & PFS)6798 bool LLParser::parseCleanupRet(Instruction *&Inst, PerFunctionState &PFS) {
6799 Value *CleanupPad = nullptr;
6800
6801 if (parseToken(lltok::kw_from, "expected 'from' after cleanupret"))
6802 return true;
6803
6804 if (parseValue(Type::getTokenTy(Context), CleanupPad, PFS))
6805 return true;
6806
6807 if (parseToken(lltok::kw_unwind, "expected 'unwind' in cleanupret"))
6808 return true;
6809
6810 BasicBlock *UnwindBB = nullptr;
6811 if (Lex.getKind() == lltok::kw_to) {
6812 Lex.Lex();
6813 if (parseToken(lltok::kw_caller, "expected 'caller' in cleanupret"))
6814 return true;
6815 } else {
6816 if (parseTypeAndBasicBlock(UnwindBB, PFS)) {
6817 return true;
6818 }
6819 }
6820
6821 Inst = CleanupReturnInst::Create(CleanupPad, UnwindBB);
6822 return false;
6823 }
6824
6825 /// parseCatchRet
6826 /// ::= 'catchret' from Parent Value 'to' TypeAndValue
parseCatchRet(Instruction * & Inst,PerFunctionState & PFS)6827 bool LLParser::parseCatchRet(Instruction *&Inst, PerFunctionState &PFS) {
6828 Value *CatchPad = nullptr;
6829
6830 if (parseToken(lltok::kw_from, "expected 'from' after catchret"))
6831 return true;
6832
6833 if (parseValue(Type::getTokenTy(Context), CatchPad, PFS))
6834 return true;
6835
6836 BasicBlock *BB;
6837 if (parseToken(lltok::kw_to, "expected 'to' in catchret") ||
6838 parseTypeAndBasicBlock(BB, PFS))
6839 return true;
6840
6841 Inst = CatchReturnInst::Create(CatchPad, BB);
6842 return false;
6843 }
6844
6845 /// parseCatchSwitch
6846 /// ::= 'catchswitch' within Parent
parseCatchSwitch(Instruction * & Inst,PerFunctionState & PFS)6847 bool LLParser::parseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS) {
6848 Value *ParentPad;
6849
6850 if (parseToken(lltok::kw_within, "expected 'within' after catchswitch"))
6851 return true;
6852
6853 if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
6854 Lex.getKind() != lltok::LocalVarID)
6855 return tokError("expected scope value for catchswitch");
6856
6857 if (parseValue(Type::getTokenTy(Context), ParentPad, PFS))
6858 return true;
6859
6860 if (parseToken(lltok::lsquare, "expected '[' with catchswitch labels"))
6861 return true;
6862
6863 SmallVector<BasicBlock *, 32> Table;
6864 do {
6865 BasicBlock *DestBB;
6866 if (parseTypeAndBasicBlock(DestBB, PFS))
6867 return true;
6868 Table.push_back(DestBB);
6869 } while (EatIfPresent(lltok::comma));
6870
6871 if (parseToken(lltok::rsquare, "expected ']' after catchswitch labels"))
6872 return true;
6873
6874 if (parseToken(lltok::kw_unwind, "expected 'unwind' after catchswitch scope"))
6875 return true;
6876
6877 BasicBlock *UnwindBB = nullptr;
6878 if (EatIfPresent(lltok::kw_to)) {
6879 if (parseToken(lltok::kw_caller, "expected 'caller' in catchswitch"))
6880 return true;
6881 } else {
6882 if (parseTypeAndBasicBlock(UnwindBB, PFS))
6883 return true;
6884 }
6885
6886 auto *CatchSwitch =
6887 CatchSwitchInst::Create(ParentPad, UnwindBB, Table.size());
6888 for (BasicBlock *DestBB : Table)
6889 CatchSwitch->addHandler(DestBB);
6890 Inst = CatchSwitch;
6891 return false;
6892 }
6893
6894 /// parseCatchPad
6895 /// ::= 'catchpad' ParamList 'to' TypeAndValue 'unwind' TypeAndValue
parseCatchPad(Instruction * & Inst,PerFunctionState & PFS)6896 bool LLParser::parseCatchPad(Instruction *&Inst, PerFunctionState &PFS) {
6897 Value *CatchSwitch = nullptr;
6898
6899 if (parseToken(lltok::kw_within, "expected 'within' after catchpad"))
6900 return true;
6901
6902 if (Lex.getKind() != lltok::LocalVar && Lex.getKind() != lltok::LocalVarID)
6903 return tokError("expected scope value for catchpad");
6904
6905 if (parseValue(Type::getTokenTy(Context), CatchSwitch, PFS))
6906 return true;
6907
6908 SmallVector<Value *, 8> Args;
6909 if (parseExceptionArgs(Args, PFS))
6910 return true;
6911
6912 Inst = CatchPadInst::Create(CatchSwitch, Args);
6913 return false;
6914 }
6915
6916 /// parseCleanupPad
6917 /// ::= 'cleanuppad' within Parent ParamList
parseCleanupPad(Instruction * & Inst,PerFunctionState & PFS)6918 bool LLParser::parseCleanupPad(Instruction *&Inst, PerFunctionState &PFS) {
6919 Value *ParentPad = nullptr;
6920
6921 if (parseToken(lltok::kw_within, "expected 'within' after cleanuppad"))
6922 return true;
6923
6924 if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
6925 Lex.getKind() != lltok::LocalVarID)
6926 return tokError("expected scope value for cleanuppad");
6927
6928 if (parseValue(Type::getTokenTy(Context), ParentPad, PFS))
6929 return true;
6930
6931 SmallVector<Value *, 8> Args;
6932 if (parseExceptionArgs(Args, PFS))
6933 return true;
6934
6935 Inst = CleanupPadInst::Create(ParentPad, Args);
6936 return false;
6937 }
6938
6939 //===----------------------------------------------------------------------===//
6940 // Unary Operators.
6941 //===----------------------------------------------------------------------===//
6942
6943 /// parseUnaryOp
6944 /// ::= UnaryOp TypeAndValue ',' Value
6945 ///
6946 /// If IsFP is false, then any integer operand is allowed, if it is true, any fp
6947 /// operand is allowed.
parseUnaryOp(Instruction * & Inst,PerFunctionState & PFS,unsigned Opc,bool IsFP)6948 bool LLParser::parseUnaryOp(Instruction *&Inst, PerFunctionState &PFS,
6949 unsigned Opc, bool IsFP) {
6950 LocTy Loc; Value *LHS;
6951 if (parseTypeAndValue(LHS, Loc, PFS))
6952 return true;
6953
6954 bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy()
6955 : LHS->getType()->isIntOrIntVectorTy();
6956
6957 if (!Valid)
6958 return error(Loc, "invalid operand type for instruction");
6959
6960 Inst = UnaryOperator::Create((Instruction::UnaryOps)Opc, LHS);
6961 return false;
6962 }
6963
6964 /// parseCallBr
6965 /// ::= 'callbr' OptionalCallingConv OptionalAttrs Type Value ParamList
6966 /// OptionalAttrs OptionalOperandBundles 'to' TypeAndValue
6967 /// '[' LabelList ']'
parseCallBr(Instruction * & Inst,PerFunctionState & PFS)6968 bool LLParser::parseCallBr(Instruction *&Inst, PerFunctionState &PFS) {
6969 LocTy CallLoc = Lex.getLoc();
6970 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
6971 std::vector<unsigned> FwdRefAttrGrps;
6972 LocTy NoBuiltinLoc;
6973 unsigned CC;
6974 Type *RetType = nullptr;
6975 LocTy RetTypeLoc;
6976 ValID CalleeID;
6977 SmallVector<ParamInfo, 16> ArgList;
6978 SmallVector<OperandBundleDef, 2> BundleList;
6979
6980 BasicBlock *DefaultDest;
6981 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
6982 parseType(RetType, RetTypeLoc, true /*void allowed*/) ||
6983 parseValID(CalleeID, &PFS) || parseParameterList(ArgList, PFS) ||
6984 parseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
6985 NoBuiltinLoc) ||
6986 parseOptionalOperandBundles(BundleList, PFS) ||
6987 parseToken(lltok::kw_to, "expected 'to' in callbr") ||
6988 parseTypeAndBasicBlock(DefaultDest, PFS) ||
6989 parseToken(lltok::lsquare, "expected '[' in callbr"))
6990 return true;
6991
6992 // parse the destination list.
6993 SmallVector<BasicBlock *, 16> IndirectDests;
6994
6995 if (Lex.getKind() != lltok::rsquare) {
6996 BasicBlock *DestBB;
6997 if (parseTypeAndBasicBlock(DestBB, PFS))
6998 return true;
6999 IndirectDests.push_back(DestBB);
7000
7001 while (EatIfPresent(lltok::comma)) {
7002 if (parseTypeAndBasicBlock(DestBB, PFS))
7003 return true;
7004 IndirectDests.push_back(DestBB);
7005 }
7006 }
7007
7008 if (parseToken(lltok::rsquare, "expected ']' at end of block list"))
7009 return true;
7010
7011 // If RetType is a non-function pointer type, then this is the short syntax
7012 // for the call, which means that RetType is just the return type. Infer the
7013 // rest of the function argument types from the arguments that are present.
7014 FunctionType *Ty;
7015 if (resolveFunctionType(RetType, ArgList, Ty))
7016 return error(RetTypeLoc, "Invalid result type for LLVM function");
7017
7018 CalleeID.FTy = Ty;
7019
7020 // Look up the callee.
7021 Value *Callee;
7022 if (convertValIDToValue(PointerType::getUnqual(Ty), CalleeID, Callee, &PFS))
7023 return true;
7024
7025 // Set up the Attribute for the function.
7026 SmallVector<Value *, 8> Args;
7027 SmallVector<AttributeSet, 8> ArgAttrs;
7028
7029 // Loop through FunctionType's arguments and ensure they are specified
7030 // correctly. Also, gather any parameter attributes.
7031 FunctionType::param_iterator I = Ty->param_begin();
7032 FunctionType::param_iterator E = Ty->param_end();
7033 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
7034 Type *ExpectedTy = nullptr;
7035 if (I != E) {
7036 ExpectedTy = *I++;
7037 } else if (!Ty->isVarArg()) {
7038 return error(ArgList[i].Loc, "too many arguments specified");
7039 }
7040
7041 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
7042 return error(ArgList[i].Loc, "argument is not of expected type '" +
7043 getTypeString(ExpectedTy) + "'");
7044 Args.push_back(ArgList[i].V);
7045 ArgAttrs.push_back(ArgList[i].Attrs);
7046 }
7047
7048 if (I != E)
7049 return error(CallLoc, "not enough parameters specified for call");
7050
7051 // Finish off the Attribute and check them
7052 AttributeList PAL =
7053 AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
7054 AttributeSet::get(Context, RetAttrs), ArgAttrs);
7055
7056 CallBrInst *CBI =
7057 CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
7058 BundleList);
7059 CBI->setCallingConv(CC);
7060 CBI->setAttributes(PAL);
7061 ForwardRefAttrGroups[CBI] = FwdRefAttrGrps;
7062 Inst = CBI;
7063 return false;
7064 }
7065
7066 //===----------------------------------------------------------------------===//
7067 // Binary Operators.
7068 //===----------------------------------------------------------------------===//
7069
7070 /// parseArithmetic
7071 /// ::= ArithmeticOps TypeAndValue ',' Value
7072 ///
7073 /// If IsFP is false, then any integer operand is allowed, if it is true, any fp
7074 /// operand is allowed.
parseArithmetic(Instruction * & Inst,PerFunctionState & PFS,unsigned Opc,bool IsFP)7075 bool LLParser::parseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
7076 unsigned Opc, bool IsFP) {
7077 LocTy Loc; Value *LHS, *RHS;
7078 if (parseTypeAndValue(LHS, Loc, PFS) ||
7079 parseToken(lltok::comma, "expected ',' in arithmetic operation") ||
7080 parseValue(LHS->getType(), RHS, PFS))
7081 return true;
7082
7083 bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy()
7084 : LHS->getType()->isIntOrIntVectorTy();
7085
7086 if (!Valid)
7087 return error(Loc, "invalid operand type for instruction");
7088
7089 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
7090 return false;
7091 }
7092
7093 /// parseLogical
7094 /// ::= ArithmeticOps TypeAndValue ',' Value {
parseLogical(Instruction * & Inst,PerFunctionState & PFS,unsigned Opc)7095 bool LLParser::parseLogical(Instruction *&Inst, PerFunctionState &PFS,
7096 unsigned Opc) {
7097 LocTy Loc; Value *LHS, *RHS;
7098 if (parseTypeAndValue(LHS, Loc, PFS) ||
7099 parseToken(lltok::comma, "expected ',' in logical operation") ||
7100 parseValue(LHS->getType(), RHS, PFS))
7101 return true;
7102
7103 if (!LHS->getType()->isIntOrIntVectorTy())
7104 return error(Loc,
7105 "instruction requires integer or integer vector operands");
7106
7107 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
7108 return false;
7109 }
7110
7111 /// parseCompare
7112 /// ::= 'icmp' IPredicates TypeAndValue ',' Value
7113 /// ::= 'fcmp' FPredicates TypeAndValue ',' Value
parseCompare(Instruction * & Inst,PerFunctionState & PFS,unsigned Opc)7114 bool LLParser::parseCompare(Instruction *&Inst, PerFunctionState &PFS,
7115 unsigned Opc) {
7116 // parse the integer/fp comparison predicate.
7117 LocTy Loc;
7118 unsigned Pred;
7119 Value *LHS, *RHS;
7120 if (parseCmpPredicate(Pred, Opc) || parseTypeAndValue(LHS, Loc, PFS) ||
7121 parseToken(lltok::comma, "expected ',' after compare value") ||
7122 parseValue(LHS->getType(), RHS, PFS))
7123 return true;
7124
7125 if (Opc == Instruction::FCmp) {
7126 if (!LHS->getType()->isFPOrFPVectorTy())
7127 return error(Loc, "fcmp requires floating point operands");
7128 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
7129 } else {
7130 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
7131 if (!LHS->getType()->isIntOrIntVectorTy() &&
7132 !LHS->getType()->isPtrOrPtrVectorTy())
7133 return error(Loc, "icmp requires integer operands");
7134 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
7135 }
7136 return false;
7137 }
7138
7139 //===----------------------------------------------------------------------===//
7140 // Other Instructions.
7141 //===----------------------------------------------------------------------===//
7142
7143 /// parseCast
7144 /// ::= CastOpc TypeAndValue 'to' Type
parseCast(Instruction * & Inst,PerFunctionState & PFS,unsigned Opc)7145 bool LLParser::parseCast(Instruction *&Inst, PerFunctionState &PFS,
7146 unsigned Opc) {
7147 LocTy Loc;
7148 Value *Op;
7149 Type *DestTy = nullptr;
7150 if (parseTypeAndValue(Op, Loc, PFS) ||
7151 parseToken(lltok::kw_to, "expected 'to' after cast value") ||
7152 parseType(DestTy))
7153 return true;
7154
7155 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
7156 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
7157 return error(Loc, "invalid cast opcode for cast from '" +
7158 getTypeString(Op->getType()) + "' to '" +
7159 getTypeString(DestTy) + "'");
7160 }
7161 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
7162 return false;
7163 }
7164
7165 /// parseSelect
7166 /// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
parseSelect(Instruction * & Inst,PerFunctionState & PFS)7167 bool LLParser::parseSelect(Instruction *&Inst, PerFunctionState &PFS) {
7168 LocTy Loc;
7169 Value *Op0, *Op1, *Op2;
7170 if (parseTypeAndValue(Op0, Loc, PFS) ||
7171 parseToken(lltok::comma, "expected ',' after select condition") ||
7172 parseTypeAndValue(Op1, PFS) ||
7173 parseToken(lltok::comma, "expected ',' after select value") ||
7174 parseTypeAndValue(Op2, PFS))
7175 return true;
7176
7177 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
7178 return error(Loc, Reason);
7179
7180 Inst = SelectInst::Create(Op0, Op1, Op2);
7181 return false;
7182 }
7183
7184 /// parseVAArg
7185 /// ::= 'va_arg' TypeAndValue ',' Type
parseVAArg(Instruction * & Inst,PerFunctionState & PFS)7186 bool LLParser::parseVAArg(Instruction *&Inst, PerFunctionState &PFS) {
7187 Value *Op;
7188 Type *EltTy = nullptr;
7189 LocTy TypeLoc;
7190 if (parseTypeAndValue(Op, PFS) ||
7191 parseToken(lltok::comma, "expected ',' after vaarg operand") ||
7192 parseType(EltTy, TypeLoc))
7193 return true;
7194
7195 if (!EltTy->isFirstClassType())
7196 return error(TypeLoc, "va_arg requires operand with first class type");
7197
7198 Inst = new VAArgInst(Op, EltTy);
7199 return false;
7200 }
7201
7202 /// parseExtractElement
7203 /// ::= 'extractelement' TypeAndValue ',' TypeAndValue
parseExtractElement(Instruction * & Inst,PerFunctionState & PFS)7204 bool LLParser::parseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
7205 LocTy Loc;
7206 Value *Op0, *Op1;
7207 if (parseTypeAndValue(Op0, Loc, PFS) ||
7208 parseToken(lltok::comma, "expected ',' after extract value") ||
7209 parseTypeAndValue(Op1, PFS))
7210 return true;
7211
7212 if (!ExtractElementInst::isValidOperands(Op0, Op1))
7213 return error(Loc, "invalid extractelement operands");
7214
7215 Inst = ExtractElementInst::Create(Op0, Op1);
7216 return false;
7217 }
7218
7219 /// parseInsertElement
7220 /// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
parseInsertElement(Instruction * & Inst,PerFunctionState & PFS)7221 bool LLParser::parseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
7222 LocTy Loc;
7223 Value *Op0, *Op1, *Op2;
7224 if (parseTypeAndValue(Op0, Loc, PFS) ||
7225 parseToken(lltok::comma, "expected ',' after insertelement value") ||
7226 parseTypeAndValue(Op1, PFS) ||
7227 parseToken(lltok::comma, "expected ',' after insertelement value") ||
7228 parseTypeAndValue(Op2, PFS))
7229 return true;
7230
7231 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
7232 return error(Loc, "invalid insertelement operands");
7233
7234 Inst = InsertElementInst::Create(Op0, Op1, Op2);
7235 return false;
7236 }
7237
7238 /// parseShuffleVector
7239 /// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
parseShuffleVector(Instruction * & Inst,PerFunctionState & PFS)7240 bool LLParser::parseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
7241 LocTy Loc;
7242 Value *Op0, *Op1, *Op2;
7243 if (parseTypeAndValue(Op0, Loc, PFS) ||
7244 parseToken(lltok::comma, "expected ',' after shuffle mask") ||
7245 parseTypeAndValue(Op1, PFS) ||
7246 parseToken(lltok::comma, "expected ',' after shuffle value") ||
7247 parseTypeAndValue(Op2, PFS))
7248 return true;
7249
7250 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
7251 return error(Loc, "invalid shufflevector operands");
7252
7253 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
7254 return false;
7255 }
7256
7257 /// parsePHI
7258 /// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
parsePHI(Instruction * & Inst,PerFunctionState & PFS)7259 int LLParser::parsePHI(Instruction *&Inst, PerFunctionState &PFS) {
7260 Type *Ty = nullptr; LocTy TypeLoc;
7261 Value *Op0, *Op1;
7262
7263 if (parseType(Ty, TypeLoc))
7264 return true;
7265
7266 if (!Ty->isFirstClassType())
7267 return error(TypeLoc, "phi node must have first class type");
7268
7269 bool First = true;
7270 bool AteExtraComma = false;
7271 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
7272
7273 while (true) {
7274 if (First) {
7275 if (Lex.getKind() != lltok::lsquare)
7276 break;
7277 First = false;
7278 } else if (!EatIfPresent(lltok::comma))
7279 break;
7280
7281 if (Lex.getKind() == lltok::MetadataVar) {
7282 AteExtraComma = true;
7283 break;
7284 }
7285
7286 if (parseToken(lltok::lsquare, "expected '[' in phi value list") ||
7287 parseValue(Ty, Op0, PFS) ||
7288 parseToken(lltok::comma, "expected ',' after insertelement value") ||
7289 parseValue(Type::getLabelTy(Context), Op1, PFS) ||
7290 parseToken(lltok::rsquare, "expected ']' in phi value list"))
7291 return true;
7292
7293 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
7294 }
7295
7296 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
7297 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
7298 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
7299 Inst = PN;
7300 return AteExtraComma ? InstExtraComma : InstNormal;
7301 }
7302
7303 /// parseLandingPad
7304 /// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
7305 /// Clause
7306 /// ::= 'catch' TypeAndValue
7307 /// ::= 'filter'
7308 /// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
parseLandingPad(Instruction * & Inst,PerFunctionState & PFS)7309 bool LLParser::parseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
7310 Type *Ty = nullptr; LocTy TyLoc;
7311
7312 if (parseType(Ty, TyLoc))
7313 return true;
7314
7315 std::unique_ptr<LandingPadInst> LP(LandingPadInst::Create(Ty, 0));
7316 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
7317
7318 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
7319 LandingPadInst::ClauseType CT;
7320 if (EatIfPresent(lltok::kw_catch))
7321 CT = LandingPadInst::Catch;
7322 else if (EatIfPresent(lltok::kw_filter))
7323 CT = LandingPadInst::Filter;
7324 else
7325 return tokError("expected 'catch' or 'filter' clause type");
7326
7327 Value *V;
7328 LocTy VLoc;
7329 if (parseTypeAndValue(V, VLoc, PFS))
7330 return true;
7331
7332 // A 'catch' type expects a non-array constant. A filter clause expects an
7333 // array constant.
7334 if (CT == LandingPadInst::Catch) {
7335 if (isa<ArrayType>(V->getType()))
7336 error(VLoc, "'catch' clause has an invalid type");
7337 } else {
7338 if (!isa<ArrayType>(V->getType()))
7339 error(VLoc, "'filter' clause has an invalid type");
7340 }
7341
7342 Constant *CV = dyn_cast<Constant>(V);
7343 if (!CV)
7344 return error(VLoc, "clause argument must be a constant");
7345 LP->addClause(CV);
7346 }
7347
7348 Inst = LP.release();
7349 return false;
7350 }
7351
7352 /// parseFreeze
7353 /// ::= 'freeze' Type Value
parseFreeze(Instruction * & Inst,PerFunctionState & PFS)7354 bool LLParser::parseFreeze(Instruction *&Inst, PerFunctionState &PFS) {
7355 LocTy Loc;
7356 Value *Op;
7357 if (parseTypeAndValue(Op, Loc, PFS))
7358 return true;
7359
7360 Inst = new FreezeInst(Op);
7361 return false;
7362 }
7363
7364 /// parseCall
7365 /// ::= 'call' OptionalFastMathFlags OptionalCallingConv
7366 /// OptionalAttrs Type Value ParameterList OptionalAttrs
7367 /// ::= 'tail' 'call' OptionalFastMathFlags OptionalCallingConv
7368 /// OptionalAttrs Type Value ParameterList OptionalAttrs
7369 /// ::= 'musttail' 'call' OptionalFastMathFlags OptionalCallingConv
7370 /// OptionalAttrs Type Value ParameterList OptionalAttrs
7371 /// ::= 'notail' 'call' OptionalFastMathFlags OptionalCallingConv
7372 /// OptionalAttrs Type Value ParameterList OptionalAttrs
parseCall(Instruction * & Inst,PerFunctionState & PFS,CallInst::TailCallKind TCK)7373 bool LLParser::parseCall(Instruction *&Inst, PerFunctionState &PFS,
7374 CallInst::TailCallKind TCK) {
7375 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
7376 std::vector<unsigned> FwdRefAttrGrps;
7377 LocTy BuiltinLoc;
7378 unsigned CallAddrSpace;
7379 unsigned CC;
7380 Type *RetType = nullptr;
7381 LocTy RetTypeLoc;
7382 ValID CalleeID;
7383 SmallVector<ParamInfo, 16> ArgList;
7384 SmallVector<OperandBundleDef, 2> BundleList;
7385 LocTy CallLoc = Lex.getLoc();
7386
7387 if (TCK != CallInst::TCK_None &&
7388 parseToken(lltok::kw_call,
7389 "expected 'tail call', 'musttail call', or 'notail call'"))
7390 return true;
7391
7392 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7393
7394 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
7395 parseOptionalProgramAddrSpace(CallAddrSpace) ||
7396 parseType(RetType, RetTypeLoc, true /*void allowed*/) ||
7397 parseValID(CalleeID, &PFS) ||
7398 parseParameterList(ArgList, PFS, TCK == CallInst::TCK_MustTail,
7399 PFS.getFunction().isVarArg()) ||
7400 parseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false, BuiltinLoc) ||
7401 parseOptionalOperandBundles(BundleList, PFS))
7402 return true;
7403
7404 // If RetType is a non-function pointer type, then this is the short syntax
7405 // for the call, which means that RetType is just the return type. Infer the
7406 // rest of the function argument types from the arguments that are present.
7407 FunctionType *Ty;
7408 if (resolveFunctionType(RetType, ArgList, Ty))
7409 return error(RetTypeLoc, "Invalid result type for LLVM function");
7410
7411 CalleeID.FTy = Ty;
7412
7413 // Look up the callee.
7414 Value *Callee;
7415 if (convertValIDToValue(PointerType::get(Ty, CallAddrSpace), CalleeID, Callee,
7416 &PFS))
7417 return true;
7418
7419 // Set up the Attribute for the function.
7420 SmallVector<AttributeSet, 8> Attrs;
7421
7422 SmallVector<Value*, 8> Args;
7423
7424 // Loop through FunctionType's arguments and ensure they are specified
7425 // correctly. Also, gather any parameter attributes.
7426 FunctionType::param_iterator I = Ty->param_begin();
7427 FunctionType::param_iterator E = Ty->param_end();
7428 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
7429 Type *ExpectedTy = nullptr;
7430 if (I != E) {
7431 ExpectedTy = *I++;
7432 } else if (!Ty->isVarArg()) {
7433 return error(ArgList[i].Loc, "too many arguments specified");
7434 }
7435
7436 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
7437 return error(ArgList[i].Loc, "argument is not of expected type '" +
7438 getTypeString(ExpectedTy) + "'");
7439 Args.push_back(ArgList[i].V);
7440 Attrs.push_back(ArgList[i].Attrs);
7441 }
7442
7443 if (I != E)
7444 return error(CallLoc, "not enough parameters specified for call");
7445
7446 // Finish off the Attribute and check them
7447 AttributeList PAL =
7448 AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
7449 AttributeSet::get(Context, RetAttrs), Attrs);
7450
7451 CallInst *CI = CallInst::Create(Ty, Callee, Args, BundleList);
7452 CI->setTailCallKind(TCK);
7453 CI->setCallingConv(CC);
7454 if (FMF.any()) {
7455 if (!isa<FPMathOperator>(CI)) {
7456 CI->deleteValue();
7457 return error(CallLoc, "fast-math-flags specified for call without "
7458 "floating-point scalar or vector return type");
7459 }
7460 CI->setFastMathFlags(FMF);
7461 }
7462 CI->setAttributes(PAL);
7463 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
7464 Inst = CI;
7465 return false;
7466 }
7467
7468 //===----------------------------------------------------------------------===//
7469 // Memory Instructions.
7470 //===----------------------------------------------------------------------===//
7471
7472 /// parseAlloc
7473 /// ::= 'alloca' 'inalloca'? 'swifterror'? Type (',' TypeAndValue)?
7474 /// (',' 'align' i32)? (',', 'addrspace(n))?
parseAlloc(Instruction * & Inst,PerFunctionState & PFS)7475 int LLParser::parseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
7476 Value *Size = nullptr;
7477 LocTy SizeLoc, TyLoc, ASLoc;
7478 MaybeAlign Alignment;
7479 unsigned AddrSpace = 0;
7480 Type *Ty = nullptr;
7481
7482 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
7483 bool IsSwiftError = EatIfPresent(lltok::kw_swifterror);
7484
7485 if (parseType(Ty, TyLoc))
7486 return true;
7487
7488 if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
7489 return error(TyLoc, "invalid type for alloca");
7490
7491 bool AteExtraComma = false;
7492 if (EatIfPresent(lltok::comma)) {
7493 if (Lex.getKind() == lltok::kw_align) {
7494 if (parseOptionalAlignment(Alignment))
7495 return true;
7496 if (parseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
7497 return true;
7498 } else if (Lex.getKind() == lltok::kw_addrspace) {
7499 ASLoc = Lex.getLoc();
7500 if (parseOptionalAddrSpace(AddrSpace))
7501 return true;
7502 } else if (Lex.getKind() == lltok::MetadataVar) {
7503 AteExtraComma = true;
7504 } else {
7505 if (parseTypeAndValue(Size, SizeLoc, PFS))
7506 return true;
7507 if (EatIfPresent(lltok::comma)) {
7508 if (Lex.getKind() == lltok::kw_align) {
7509 if (parseOptionalAlignment(Alignment))
7510 return true;
7511 if (parseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
7512 return true;
7513 } else if (Lex.getKind() == lltok::kw_addrspace) {
7514 ASLoc = Lex.getLoc();
7515 if (parseOptionalAddrSpace(AddrSpace))
7516 return true;
7517 } else if (Lex.getKind() == lltok::MetadataVar) {
7518 AteExtraComma = true;
7519 }
7520 }
7521 }
7522 }
7523
7524 if (Size && !Size->getType()->isIntegerTy())
7525 return error(SizeLoc, "element count must have integer type");
7526
7527 SmallPtrSet<Type *, 4> Visited;
7528 if (!Alignment && !Ty->isSized(&Visited))
7529 return error(TyLoc, "Cannot allocate unsized type");
7530 if (!Alignment)
7531 Alignment = M->getDataLayout().getPrefTypeAlign(Ty);
7532 AllocaInst *AI = new AllocaInst(Ty, AddrSpace, Size, *Alignment);
7533 AI->setUsedWithInAlloca(IsInAlloca);
7534 AI->setSwiftError(IsSwiftError);
7535 Inst = AI;
7536 return AteExtraComma ? InstExtraComma : InstNormal;
7537 }
7538
7539 /// parseLoad
7540 /// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
7541 /// ::= 'load' 'atomic' 'volatile'? TypeAndValue
7542 /// 'singlethread'? AtomicOrdering (',' 'align' i32)?
parseLoad(Instruction * & Inst,PerFunctionState & PFS)7543 int LLParser::parseLoad(Instruction *&Inst, PerFunctionState &PFS) {
7544 Value *Val; LocTy Loc;
7545 MaybeAlign Alignment;
7546 bool AteExtraComma = false;
7547 bool isAtomic = false;
7548 AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
7549 SyncScope::ID SSID = SyncScope::System;
7550
7551 if (Lex.getKind() == lltok::kw_atomic) {
7552 isAtomic = true;
7553 Lex.Lex();
7554 }
7555
7556 bool isVolatile = false;
7557 if (Lex.getKind() == lltok::kw_volatile) {
7558 isVolatile = true;
7559 Lex.Lex();
7560 }
7561
7562 Type *Ty;
7563 LocTy ExplicitTypeLoc = Lex.getLoc();
7564 if (parseType(Ty) ||
7565 parseToken(lltok::comma, "expected comma after load's type") ||
7566 parseTypeAndValue(Val, Loc, PFS) ||
7567 parseScopeAndOrdering(isAtomic, SSID, Ordering) ||
7568 parseOptionalCommaAlign(Alignment, AteExtraComma))
7569 return true;
7570
7571 if (!Val->getType()->isPointerTy() || !Ty->isFirstClassType())
7572 return error(Loc, "load operand must be a pointer to a first class type");
7573 if (isAtomic && !Alignment)
7574 return error(Loc, "atomic load must have explicit non-zero alignment");
7575 if (Ordering == AtomicOrdering::Release ||
7576 Ordering == AtomicOrdering::AcquireRelease)
7577 return error(Loc, "atomic load cannot use Release ordering");
7578
7579 if (!cast<PointerType>(Val->getType())->isOpaqueOrPointeeTypeMatches(Ty)) {
7580 return error(
7581 ExplicitTypeLoc,
7582 typeComparisonErrorMessage(
7583 "explicit pointee type doesn't match operand's pointee type", Ty,
7584 Val->getType()->getNonOpaquePointerElementType()));
7585 }
7586 SmallPtrSet<Type *, 4> Visited;
7587 if (!Alignment && !Ty->isSized(&Visited))
7588 return error(ExplicitTypeLoc, "loading unsized types is not allowed");
7589 if (!Alignment)
7590 Alignment = M->getDataLayout().getABITypeAlign(Ty);
7591 Inst = new LoadInst(Ty, Val, "", isVolatile, *Alignment, Ordering, SSID);
7592 return AteExtraComma ? InstExtraComma : InstNormal;
7593 }
7594
7595 /// parseStore
7596
7597 /// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
7598 /// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
7599 /// 'singlethread'? AtomicOrdering (',' 'align' i32)?
parseStore(Instruction * & Inst,PerFunctionState & PFS)7600 int LLParser::parseStore(Instruction *&Inst, PerFunctionState &PFS) {
7601 Value *Val, *Ptr; LocTy Loc, PtrLoc;
7602 MaybeAlign Alignment;
7603 bool AteExtraComma = false;
7604 bool isAtomic = false;
7605 AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
7606 SyncScope::ID SSID = SyncScope::System;
7607
7608 if (Lex.getKind() == lltok::kw_atomic) {
7609 isAtomic = true;
7610 Lex.Lex();
7611 }
7612
7613 bool isVolatile = false;
7614 if (Lex.getKind() == lltok::kw_volatile) {
7615 isVolatile = true;
7616 Lex.Lex();
7617 }
7618
7619 if (parseTypeAndValue(Val, Loc, PFS) ||
7620 parseToken(lltok::comma, "expected ',' after store operand") ||
7621 parseTypeAndValue(Ptr, PtrLoc, PFS) ||
7622 parseScopeAndOrdering(isAtomic, SSID, Ordering) ||
7623 parseOptionalCommaAlign(Alignment, AteExtraComma))
7624 return true;
7625
7626 if (!Ptr->getType()->isPointerTy())
7627 return error(PtrLoc, "store operand must be a pointer");
7628 if (!Val->getType()->isFirstClassType())
7629 return error(Loc, "store operand must be a first class value");
7630 if (!cast<PointerType>(Ptr->getType())
7631 ->isOpaqueOrPointeeTypeMatches(Val->getType()))
7632 return error(Loc, "stored value and pointer type do not match");
7633 if (isAtomic && !Alignment)
7634 return error(Loc, "atomic store must have explicit non-zero alignment");
7635 if (Ordering == AtomicOrdering::Acquire ||
7636 Ordering == AtomicOrdering::AcquireRelease)
7637 return error(Loc, "atomic store cannot use Acquire ordering");
7638 SmallPtrSet<Type *, 4> Visited;
7639 if (!Alignment && !Val->getType()->isSized(&Visited))
7640 return error(Loc, "storing unsized types is not allowed");
7641 if (!Alignment)
7642 Alignment = M->getDataLayout().getABITypeAlign(Val->getType());
7643
7644 Inst = new StoreInst(Val, Ptr, isVolatile, *Alignment, Ordering, SSID);
7645 return AteExtraComma ? InstExtraComma : InstNormal;
7646 }
7647
7648 /// parseCmpXchg
7649 /// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
7650 /// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering ','
7651 /// 'Align'?
parseCmpXchg(Instruction * & Inst,PerFunctionState & PFS)7652 int LLParser::parseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
7653 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
7654 bool AteExtraComma = false;
7655 AtomicOrdering SuccessOrdering = AtomicOrdering::NotAtomic;
7656 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic;
7657 SyncScope::ID SSID = SyncScope::System;
7658 bool isVolatile = false;
7659 bool isWeak = false;
7660 MaybeAlign Alignment;
7661
7662 if (EatIfPresent(lltok::kw_weak))
7663 isWeak = true;
7664
7665 if (EatIfPresent(lltok::kw_volatile))
7666 isVolatile = true;
7667
7668 if (parseTypeAndValue(Ptr, PtrLoc, PFS) ||
7669 parseToken(lltok::comma, "expected ',' after cmpxchg address") ||
7670 parseTypeAndValue(Cmp, CmpLoc, PFS) ||
7671 parseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
7672 parseTypeAndValue(New, NewLoc, PFS) ||
7673 parseScopeAndOrdering(true /*Always atomic*/, SSID, SuccessOrdering) ||
7674 parseOrdering(FailureOrdering) ||
7675 parseOptionalCommaAlign(Alignment, AteExtraComma))
7676 return true;
7677
7678 if (!AtomicCmpXchgInst::isValidSuccessOrdering(SuccessOrdering))
7679 return tokError("invalid cmpxchg success ordering");
7680 if (!AtomicCmpXchgInst::isValidFailureOrdering(FailureOrdering))
7681 return tokError("invalid cmpxchg failure ordering");
7682 if (!Ptr->getType()->isPointerTy())
7683 return error(PtrLoc, "cmpxchg operand must be a pointer");
7684 if (!cast<PointerType>(Ptr->getType())
7685 ->isOpaqueOrPointeeTypeMatches(Cmp->getType()))
7686 return error(CmpLoc, "compare value and pointer type do not match");
7687 if (!cast<PointerType>(Ptr->getType())
7688 ->isOpaqueOrPointeeTypeMatches(New->getType()))
7689 return error(NewLoc, "new value and pointer type do not match");
7690 if (Cmp->getType() != New->getType())
7691 return error(NewLoc, "compare value and new value type do not match");
7692 if (!New->getType()->isFirstClassType())
7693 return error(NewLoc, "cmpxchg operand must be a first class value");
7694
7695 const Align DefaultAlignment(
7696 PFS.getFunction().getParent()->getDataLayout().getTypeStoreSize(
7697 Cmp->getType()));
7698
7699 AtomicCmpXchgInst *CXI =
7700 new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment.value_or(DefaultAlignment),
7701 SuccessOrdering, FailureOrdering, SSID);
7702 CXI->setVolatile(isVolatile);
7703 CXI->setWeak(isWeak);
7704
7705 Inst = CXI;
7706 return AteExtraComma ? InstExtraComma : InstNormal;
7707 }
7708
7709 /// parseAtomicRMW
7710 /// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
7711 /// 'singlethread'? AtomicOrdering
parseAtomicRMW(Instruction * & Inst,PerFunctionState & PFS)7712 int LLParser::parseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
7713 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
7714 bool AteExtraComma = false;
7715 AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
7716 SyncScope::ID SSID = SyncScope::System;
7717 bool isVolatile = false;
7718 bool IsFP = false;
7719 AtomicRMWInst::BinOp Operation;
7720 MaybeAlign Alignment;
7721
7722 if (EatIfPresent(lltok::kw_volatile))
7723 isVolatile = true;
7724
7725 switch (Lex.getKind()) {
7726 default:
7727 return tokError("expected binary operation in atomicrmw");
7728 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
7729 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
7730 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
7731 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
7732 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
7733 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
7734 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
7735 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
7736 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
7737 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
7738 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
7739 case lltok::kw_uinc_wrap:
7740 Operation = AtomicRMWInst::UIncWrap;
7741 break;
7742 case lltok::kw_udec_wrap:
7743 Operation = AtomicRMWInst::UDecWrap;
7744 break;
7745 case lltok::kw_fadd:
7746 Operation = AtomicRMWInst::FAdd;
7747 IsFP = true;
7748 break;
7749 case lltok::kw_fsub:
7750 Operation = AtomicRMWInst::FSub;
7751 IsFP = true;
7752 break;
7753 case lltok::kw_fmax:
7754 Operation = AtomicRMWInst::FMax;
7755 IsFP = true;
7756 break;
7757 case lltok::kw_fmin:
7758 Operation = AtomicRMWInst::FMin;
7759 IsFP = true;
7760 break;
7761 }
7762 Lex.Lex(); // Eat the operation.
7763
7764 if (parseTypeAndValue(Ptr, PtrLoc, PFS) ||
7765 parseToken(lltok::comma, "expected ',' after atomicrmw address") ||
7766 parseTypeAndValue(Val, ValLoc, PFS) ||
7767 parseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering) ||
7768 parseOptionalCommaAlign(Alignment, AteExtraComma))
7769 return true;
7770
7771 if (Ordering == AtomicOrdering::Unordered)
7772 return tokError("atomicrmw cannot be unordered");
7773 if (!Ptr->getType()->isPointerTy())
7774 return error(PtrLoc, "atomicrmw operand must be a pointer");
7775 if (!cast<PointerType>(Ptr->getType())
7776 ->isOpaqueOrPointeeTypeMatches(Val->getType()))
7777 return error(ValLoc, "atomicrmw value and pointer type do not match");
7778
7779 if (Operation == AtomicRMWInst::Xchg) {
7780 if (!Val->getType()->isIntegerTy() &&
7781 !Val->getType()->isFloatingPointTy() &&
7782 !Val->getType()->isPointerTy()) {
7783 return error(
7784 ValLoc,
7785 "atomicrmw " + AtomicRMWInst::getOperationName(Operation) +
7786 " operand must be an integer, floating point, or pointer type");
7787 }
7788 } else if (IsFP) {
7789 if (!Val->getType()->isFloatingPointTy()) {
7790 return error(ValLoc, "atomicrmw " +
7791 AtomicRMWInst::getOperationName(Operation) +
7792 " operand must be a floating point type");
7793 }
7794 } else {
7795 if (!Val->getType()->isIntegerTy()) {
7796 return error(ValLoc, "atomicrmw " +
7797 AtomicRMWInst::getOperationName(Operation) +
7798 " operand must be an integer");
7799 }
7800 }
7801
7802 unsigned Size =
7803 PFS.getFunction().getParent()->getDataLayout().getTypeStoreSizeInBits(
7804 Val->getType());
7805 if (Size < 8 || (Size & (Size - 1)))
7806 return error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
7807 " integer");
7808 const Align DefaultAlignment(
7809 PFS.getFunction().getParent()->getDataLayout().getTypeStoreSize(
7810 Val->getType()));
7811 AtomicRMWInst *RMWI =
7812 new AtomicRMWInst(Operation, Ptr, Val,
7813 Alignment.value_or(DefaultAlignment), Ordering, SSID);
7814 RMWI->setVolatile(isVolatile);
7815 Inst = RMWI;
7816 return AteExtraComma ? InstExtraComma : InstNormal;
7817 }
7818
7819 /// parseFence
7820 /// ::= 'fence' 'singlethread'? AtomicOrdering
parseFence(Instruction * & Inst,PerFunctionState & PFS)7821 int LLParser::parseFence(Instruction *&Inst, PerFunctionState &PFS) {
7822 AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
7823 SyncScope::ID SSID = SyncScope::System;
7824 if (parseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering))
7825 return true;
7826
7827 if (Ordering == AtomicOrdering::Unordered)
7828 return tokError("fence cannot be unordered");
7829 if (Ordering == AtomicOrdering::Monotonic)
7830 return tokError("fence cannot be monotonic");
7831
7832 Inst = new FenceInst(Context, Ordering, SSID);
7833 return InstNormal;
7834 }
7835
7836 /// parseGetElementPtr
7837 /// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
parseGetElementPtr(Instruction * & Inst,PerFunctionState & PFS)7838 int LLParser::parseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
7839 Value *Ptr = nullptr;
7840 Value *Val = nullptr;
7841 LocTy Loc, EltLoc;
7842
7843 bool InBounds = EatIfPresent(lltok::kw_inbounds);
7844
7845 Type *Ty = nullptr;
7846 LocTy ExplicitTypeLoc = Lex.getLoc();
7847 if (parseType(Ty) ||
7848 parseToken(lltok::comma, "expected comma after getelementptr's type") ||
7849 parseTypeAndValue(Ptr, Loc, PFS))
7850 return true;
7851
7852 Type *BaseType = Ptr->getType();
7853 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
7854 if (!BasePointerType)
7855 return error(Loc, "base of getelementptr must be a pointer");
7856
7857 if (!BasePointerType->isOpaqueOrPointeeTypeMatches(Ty)) {
7858 return error(
7859 ExplicitTypeLoc,
7860 typeComparisonErrorMessage(
7861 "explicit pointee type doesn't match operand's pointee type", Ty,
7862 BasePointerType->getNonOpaquePointerElementType()));
7863 }
7864
7865 SmallVector<Value*, 16> Indices;
7866 bool AteExtraComma = false;
7867 // GEP returns a vector of pointers if at least one of parameters is a vector.
7868 // All vector parameters should have the same vector width.
7869 ElementCount GEPWidth = BaseType->isVectorTy()
7870 ? cast<VectorType>(BaseType)->getElementCount()
7871 : ElementCount::getFixed(0);
7872
7873 while (EatIfPresent(lltok::comma)) {
7874 if (Lex.getKind() == lltok::MetadataVar) {
7875 AteExtraComma = true;
7876 break;
7877 }
7878 if (parseTypeAndValue(Val, EltLoc, PFS))
7879 return true;
7880 if (!Val->getType()->isIntOrIntVectorTy())
7881 return error(EltLoc, "getelementptr index must be an integer");
7882
7883 if (auto *ValVTy = dyn_cast<VectorType>(Val->getType())) {
7884 ElementCount ValNumEl = ValVTy->getElementCount();
7885 if (GEPWidth != ElementCount::getFixed(0) && GEPWidth != ValNumEl)
7886 return error(
7887 EltLoc,
7888 "getelementptr vector index has a wrong number of elements");
7889 GEPWidth = ValNumEl;
7890 }
7891 Indices.push_back(Val);
7892 }
7893
7894 SmallPtrSet<Type*, 4> Visited;
7895 if (!Indices.empty() && !Ty->isSized(&Visited))
7896 return error(Loc, "base element of getelementptr must be sized");
7897
7898 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
7899 return error(Loc, "invalid getelementptr indices");
7900 Inst = GetElementPtrInst::Create(Ty, Ptr, Indices);
7901 if (InBounds)
7902 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
7903 return AteExtraComma ? InstExtraComma : InstNormal;
7904 }
7905
7906 /// parseExtractValue
7907 /// ::= 'extractvalue' TypeAndValue (',' uint32)+
parseExtractValue(Instruction * & Inst,PerFunctionState & PFS)7908 int LLParser::parseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
7909 Value *Val; LocTy Loc;
7910 SmallVector<unsigned, 4> Indices;
7911 bool AteExtraComma;
7912 if (parseTypeAndValue(Val, Loc, PFS) ||
7913 parseIndexList(Indices, AteExtraComma))
7914 return true;
7915
7916 if (!Val->getType()->isAggregateType())
7917 return error(Loc, "extractvalue operand must be aggregate type");
7918
7919 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
7920 return error(Loc, "invalid indices for extractvalue");
7921 Inst = ExtractValueInst::Create(Val, Indices);
7922 return AteExtraComma ? InstExtraComma : InstNormal;
7923 }
7924
7925 /// parseInsertValue
7926 /// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
parseInsertValue(Instruction * & Inst,PerFunctionState & PFS)7927 int LLParser::parseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
7928 Value *Val0, *Val1; LocTy Loc0, Loc1;
7929 SmallVector<unsigned, 4> Indices;
7930 bool AteExtraComma;
7931 if (parseTypeAndValue(Val0, Loc0, PFS) ||
7932 parseToken(lltok::comma, "expected comma after insertvalue operand") ||
7933 parseTypeAndValue(Val1, Loc1, PFS) ||
7934 parseIndexList(Indices, AteExtraComma))
7935 return true;
7936
7937 if (!Val0->getType()->isAggregateType())
7938 return error(Loc0, "insertvalue operand must be aggregate type");
7939
7940 Type *IndexedType = ExtractValueInst::getIndexedType(Val0->getType(), Indices);
7941 if (!IndexedType)
7942 return error(Loc0, "invalid indices for insertvalue");
7943 if (IndexedType != Val1->getType())
7944 return error(Loc1, "insertvalue operand and field disagree in type: '" +
7945 getTypeString(Val1->getType()) + "' instead of '" +
7946 getTypeString(IndexedType) + "'");
7947 Inst = InsertValueInst::Create(Val0, Val1, Indices);
7948 return AteExtraComma ? InstExtraComma : InstNormal;
7949 }
7950
7951 //===----------------------------------------------------------------------===//
7952 // Embedded metadata.
7953 //===----------------------------------------------------------------------===//
7954
7955 /// parseMDNodeVector
7956 /// ::= { Element (',' Element)* }
7957 /// Element
7958 /// ::= 'null' | TypeAndValue
parseMDNodeVector(SmallVectorImpl<Metadata * > & Elts)7959 bool LLParser::parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
7960 if (parseToken(lltok::lbrace, "expected '{' here"))
7961 return true;
7962
7963 // Check for an empty list.
7964 if (EatIfPresent(lltok::rbrace))
7965 return false;
7966
7967 do {
7968 // Null is a special case since it is typeless.
7969 if (EatIfPresent(lltok::kw_null)) {
7970 Elts.push_back(nullptr);
7971 continue;
7972 }
7973
7974 Metadata *MD;
7975 if (parseMetadata(MD, nullptr))
7976 return true;
7977 Elts.push_back(MD);
7978 } while (EatIfPresent(lltok::comma));
7979
7980 return parseToken(lltok::rbrace, "expected end of metadata node");
7981 }
7982
7983 //===----------------------------------------------------------------------===//
7984 // Use-list order directives.
7985 //===----------------------------------------------------------------------===//
sortUseListOrder(Value * V,ArrayRef<unsigned> Indexes,SMLoc Loc)7986 bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes,
7987 SMLoc Loc) {
7988 if (V->use_empty())
7989 return error(Loc, "value has no uses");
7990
7991 unsigned NumUses = 0;
7992 SmallDenseMap<const Use *, unsigned, 16> Order;
7993 for (const Use &U : V->uses()) {
7994 if (++NumUses > Indexes.size())
7995 break;
7996 Order[&U] = Indexes[NumUses - 1];
7997 }
7998 if (NumUses < 2)
7999 return error(Loc, "value only has one use");
8000 if (Order.size() != Indexes.size() || NumUses > Indexes.size())
8001 return error(Loc,
8002 "wrong number of indexes, expected " + Twine(V->getNumUses()));
8003
8004 V->sortUseList([&](const Use &L, const Use &R) {
8005 return Order.lookup(&L) < Order.lookup(&R);
8006 });
8007 return false;
8008 }
8009
8010 /// parseUseListOrderIndexes
8011 /// ::= '{' uint32 (',' uint32)+ '}'
parseUseListOrderIndexes(SmallVectorImpl<unsigned> & Indexes)8012 bool LLParser::parseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) {
8013 SMLoc Loc = Lex.getLoc();
8014 if (parseToken(lltok::lbrace, "expected '{' here"))
8015 return true;
8016 if (Lex.getKind() == lltok::rbrace)
8017 return Lex.Error("expected non-empty list of uselistorder indexes");
8018
8019 // Use Offset, Max, and IsOrdered to check consistency of indexes. The
8020 // indexes should be distinct numbers in the range [0, size-1], and should
8021 // not be in order.
8022 unsigned Offset = 0;
8023 unsigned Max = 0;
8024 bool IsOrdered = true;
8025 assert(Indexes.empty() && "Expected empty order vector");
8026 do {
8027 unsigned Index;
8028 if (parseUInt32(Index))
8029 return true;
8030
8031 // Update consistency checks.
8032 Offset += Index - Indexes.size();
8033 Max = std::max(Max, Index);
8034 IsOrdered &= Index == Indexes.size();
8035
8036 Indexes.push_back(Index);
8037 } while (EatIfPresent(lltok::comma));
8038
8039 if (parseToken(lltok::rbrace, "expected '}' here"))
8040 return true;
8041
8042 if (Indexes.size() < 2)
8043 return error(Loc, "expected >= 2 uselistorder indexes");
8044 if (Offset != 0 || Max >= Indexes.size())
8045 return error(Loc,
8046 "expected distinct uselistorder indexes in range [0, size)");
8047 if (IsOrdered)
8048 return error(Loc, "expected uselistorder indexes to change the order");
8049
8050 return false;
8051 }
8052
8053 /// parseUseListOrder
8054 /// ::= 'uselistorder' Type Value ',' UseListOrderIndexes
parseUseListOrder(PerFunctionState * PFS)8055 bool LLParser::parseUseListOrder(PerFunctionState *PFS) {
8056 SMLoc Loc = Lex.getLoc();
8057 if (parseToken(lltok::kw_uselistorder, "expected uselistorder directive"))
8058 return true;
8059
8060 Value *V;
8061 SmallVector<unsigned, 16> Indexes;
8062 if (parseTypeAndValue(V, PFS) ||
8063 parseToken(lltok::comma, "expected comma in uselistorder directive") ||
8064 parseUseListOrderIndexes(Indexes))
8065 return true;
8066
8067 return sortUseListOrder(V, Indexes, Loc);
8068 }
8069
8070 /// parseUseListOrderBB
8071 /// ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes
parseUseListOrderBB()8072 bool LLParser::parseUseListOrderBB() {
8073 assert(Lex.getKind() == lltok::kw_uselistorder_bb);
8074 SMLoc Loc = Lex.getLoc();
8075 Lex.Lex();
8076
8077 ValID Fn, Label;
8078 SmallVector<unsigned, 16> Indexes;
8079 if (parseValID(Fn, /*PFS=*/nullptr) ||
8080 parseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
8081 parseValID(Label, /*PFS=*/nullptr) ||
8082 parseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
8083 parseUseListOrderIndexes(Indexes))
8084 return true;
8085
8086 // Check the function.
8087 GlobalValue *GV;
8088 if (Fn.Kind == ValID::t_GlobalName)
8089 GV = M->getNamedValue(Fn.StrVal);
8090 else if (Fn.Kind == ValID::t_GlobalID)
8091 GV = Fn.UIntVal < NumberedVals.size() ? NumberedVals[Fn.UIntVal] : nullptr;
8092 else
8093 return error(Fn.Loc, "expected function name in uselistorder_bb");
8094 if (!GV)
8095 return error(Fn.Loc,
8096 "invalid function forward reference in uselistorder_bb");
8097 auto *F = dyn_cast<Function>(GV);
8098 if (!F)
8099 return error(Fn.Loc, "expected function name in uselistorder_bb");
8100 if (F->isDeclaration())
8101 return error(Fn.Loc, "invalid declaration in uselistorder_bb");
8102
8103 // Check the basic block.
8104 if (Label.Kind == ValID::t_LocalID)
8105 return error(Label.Loc, "invalid numeric label in uselistorder_bb");
8106 if (Label.Kind != ValID::t_LocalName)
8107 return error(Label.Loc, "expected basic block name in uselistorder_bb");
8108 Value *V = F->getValueSymbolTable()->lookup(Label.StrVal);
8109 if (!V)
8110 return error(Label.Loc, "invalid basic block in uselistorder_bb");
8111 if (!isa<BasicBlock>(V))
8112 return error(Label.Loc, "expected basic block in uselistorder_bb");
8113
8114 return sortUseListOrder(V, Indexes, Loc);
8115 }
8116
8117 /// ModuleEntry
8118 /// ::= 'module' ':' '(' 'path' ':' STRINGCONSTANT ',' 'hash' ':' Hash ')'
8119 /// Hash ::= '(' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ')'
parseModuleEntry(unsigned ID)8120 bool LLParser::parseModuleEntry(unsigned ID) {
8121 assert(Lex.getKind() == lltok::kw_module);
8122 Lex.Lex();
8123
8124 std::string Path;
8125 if (parseToken(lltok::colon, "expected ':' here") ||
8126 parseToken(lltok::lparen, "expected '(' here") ||
8127 parseToken(lltok::kw_path, "expected 'path' here") ||
8128 parseToken(lltok::colon, "expected ':' here") ||
8129 parseStringConstant(Path) ||
8130 parseToken(lltok::comma, "expected ',' here") ||
8131 parseToken(lltok::kw_hash, "expected 'hash' here") ||
8132 parseToken(lltok::colon, "expected ':' here") ||
8133 parseToken(lltok::lparen, "expected '(' here"))
8134 return true;
8135
8136 ModuleHash Hash;
8137 if (parseUInt32(Hash[0]) || parseToken(lltok::comma, "expected ',' here") ||
8138 parseUInt32(Hash[1]) || parseToken(lltok::comma, "expected ',' here") ||
8139 parseUInt32(Hash[2]) || parseToken(lltok::comma, "expected ',' here") ||
8140 parseUInt32(Hash[3]) || parseToken(lltok::comma, "expected ',' here") ||
8141 parseUInt32(Hash[4]))
8142 return true;
8143
8144 if (parseToken(lltok::rparen, "expected ')' here") ||
8145 parseToken(lltok::rparen, "expected ')' here"))
8146 return true;
8147
8148 auto ModuleEntry = Index->addModule(Path, ID, Hash);
8149 ModuleIdMap[ID] = ModuleEntry->first();
8150
8151 return false;
8152 }
8153
8154 /// TypeIdEntry
8155 /// ::= 'typeid' ':' '(' 'name' ':' STRINGCONSTANT ',' TypeIdSummary ')'
parseTypeIdEntry(unsigned ID)8156 bool LLParser::parseTypeIdEntry(unsigned ID) {
8157 assert(Lex.getKind() == lltok::kw_typeid);
8158 Lex.Lex();
8159
8160 std::string Name;
8161 if (parseToken(lltok::colon, "expected ':' here") ||
8162 parseToken(lltok::lparen, "expected '(' here") ||
8163 parseToken(lltok::kw_name, "expected 'name' here") ||
8164 parseToken(lltok::colon, "expected ':' here") ||
8165 parseStringConstant(Name))
8166 return true;
8167
8168 TypeIdSummary &TIS = Index->getOrInsertTypeIdSummary(Name);
8169 if (parseToken(lltok::comma, "expected ',' here") ||
8170 parseTypeIdSummary(TIS) || parseToken(lltok::rparen, "expected ')' here"))
8171 return true;
8172
8173 // Check if this ID was forward referenced, and if so, update the
8174 // corresponding GUIDs.
8175 auto FwdRefTIDs = ForwardRefTypeIds.find(ID);
8176 if (FwdRefTIDs != ForwardRefTypeIds.end()) {
8177 for (auto TIDRef : FwdRefTIDs->second) {
8178 assert(!*TIDRef.first &&
8179 "Forward referenced type id GUID expected to be 0");
8180 *TIDRef.first = GlobalValue::getGUID(Name);
8181 }
8182 ForwardRefTypeIds.erase(FwdRefTIDs);
8183 }
8184
8185 return false;
8186 }
8187
8188 /// TypeIdSummary
8189 /// ::= 'summary' ':' '(' TypeTestResolution [',' OptionalWpdResolutions]? ')'
parseTypeIdSummary(TypeIdSummary & TIS)8190 bool LLParser::parseTypeIdSummary(TypeIdSummary &TIS) {
8191 if (parseToken(lltok::kw_summary, "expected 'summary' here") ||
8192 parseToken(lltok::colon, "expected ':' here") ||
8193 parseToken(lltok::lparen, "expected '(' here") ||
8194 parseTypeTestResolution(TIS.TTRes))
8195 return true;
8196
8197 if (EatIfPresent(lltok::comma)) {
8198 // Expect optional wpdResolutions field
8199 if (parseOptionalWpdResolutions(TIS.WPDRes))
8200 return true;
8201 }
8202
8203 if (parseToken(lltok::rparen, "expected ')' here"))
8204 return true;
8205
8206 return false;
8207 }
8208
8209 static ValueInfo EmptyVI =
8210 ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8);
8211
8212 /// TypeIdCompatibleVtableEntry
8213 /// ::= 'typeidCompatibleVTable' ':' '(' 'name' ':' STRINGCONSTANT ','
8214 /// TypeIdCompatibleVtableInfo
8215 /// ')'
parseTypeIdCompatibleVtableEntry(unsigned ID)8216 bool LLParser::parseTypeIdCompatibleVtableEntry(unsigned ID) {
8217 assert(Lex.getKind() == lltok::kw_typeidCompatibleVTable);
8218 Lex.Lex();
8219
8220 std::string Name;
8221 if (parseToken(lltok::colon, "expected ':' here") ||
8222 parseToken(lltok::lparen, "expected '(' here") ||
8223 parseToken(lltok::kw_name, "expected 'name' here") ||
8224 parseToken(lltok::colon, "expected ':' here") ||
8225 parseStringConstant(Name))
8226 return true;
8227
8228 TypeIdCompatibleVtableInfo &TI =
8229 Index->getOrInsertTypeIdCompatibleVtableSummary(Name);
8230 if (parseToken(lltok::comma, "expected ',' here") ||
8231 parseToken(lltok::kw_summary, "expected 'summary' here") ||
8232 parseToken(lltok::colon, "expected ':' here") ||
8233 parseToken(lltok::lparen, "expected '(' here"))
8234 return true;
8235
8236 IdToIndexMapType IdToIndexMap;
8237 // parse each call edge
8238 do {
8239 uint64_t Offset;
8240 if (parseToken(lltok::lparen, "expected '(' here") ||
8241 parseToken(lltok::kw_offset, "expected 'offset' here") ||
8242 parseToken(lltok::colon, "expected ':' here") || parseUInt64(Offset) ||
8243 parseToken(lltok::comma, "expected ',' here"))
8244 return true;
8245
8246 LocTy Loc = Lex.getLoc();
8247 unsigned GVId;
8248 ValueInfo VI;
8249 if (parseGVReference(VI, GVId))
8250 return true;
8251
8252 // Keep track of the TypeIdCompatibleVtableInfo array index needing a
8253 // forward reference. We will save the location of the ValueInfo needing an
8254 // update, but can only do so once the std::vector is finalized.
8255 if (VI == EmptyVI)
8256 IdToIndexMap[GVId].push_back(std::make_pair(TI.size(), Loc));
8257 TI.push_back({Offset, VI});
8258
8259 if (parseToken(lltok::rparen, "expected ')' in call"))
8260 return true;
8261 } while (EatIfPresent(lltok::comma));
8262
8263 // Now that the TI vector is finalized, it is safe to save the locations
8264 // of any forward GV references that need updating later.
8265 for (auto I : IdToIndexMap) {
8266 auto &Infos = ForwardRefValueInfos[I.first];
8267 for (auto P : I.second) {
8268 assert(TI[P.first].VTableVI == EmptyVI &&
8269 "Forward referenced ValueInfo expected to be empty");
8270 Infos.emplace_back(&TI[P.first].VTableVI, P.second);
8271 }
8272 }
8273
8274 if (parseToken(lltok::rparen, "expected ')' here") ||
8275 parseToken(lltok::rparen, "expected ')' here"))
8276 return true;
8277
8278 // Check if this ID was forward referenced, and if so, update the
8279 // corresponding GUIDs.
8280 auto FwdRefTIDs = ForwardRefTypeIds.find(ID);
8281 if (FwdRefTIDs != ForwardRefTypeIds.end()) {
8282 for (auto TIDRef : FwdRefTIDs->second) {
8283 assert(!*TIDRef.first &&
8284 "Forward referenced type id GUID expected to be 0");
8285 *TIDRef.first = GlobalValue::getGUID(Name);
8286 }
8287 ForwardRefTypeIds.erase(FwdRefTIDs);
8288 }
8289
8290 return false;
8291 }
8292
8293 /// TypeTestResolution
8294 /// ::= 'typeTestRes' ':' '(' 'kind' ':'
8295 /// ( 'unsat' | 'byteArray' | 'inline' | 'single' | 'allOnes' ) ','
8296 /// 'sizeM1BitWidth' ':' SizeM1BitWidth [',' 'alignLog2' ':' UInt64]?
8297 /// [',' 'sizeM1' ':' UInt64]? [',' 'bitMask' ':' UInt8]?
8298 /// [',' 'inlinesBits' ':' UInt64]? ')'
parseTypeTestResolution(TypeTestResolution & TTRes)8299 bool LLParser::parseTypeTestResolution(TypeTestResolution &TTRes) {
8300 if (parseToken(lltok::kw_typeTestRes, "expected 'typeTestRes' here") ||
8301 parseToken(lltok::colon, "expected ':' here") ||
8302 parseToken(lltok::lparen, "expected '(' here") ||
8303 parseToken(lltok::kw_kind, "expected 'kind' here") ||
8304 parseToken(lltok::colon, "expected ':' here"))
8305 return true;
8306
8307 switch (Lex.getKind()) {
8308 case lltok::kw_unknown:
8309 TTRes.TheKind = TypeTestResolution::Unknown;
8310 break;
8311 case lltok::kw_unsat:
8312 TTRes.TheKind = TypeTestResolution::Unsat;
8313 break;
8314 case lltok::kw_byteArray:
8315 TTRes.TheKind = TypeTestResolution::ByteArray;
8316 break;
8317 case lltok::kw_inline:
8318 TTRes.TheKind = TypeTestResolution::Inline;
8319 break;
8320 case lltok::kw_single:
8321 TTRes.TheKind = TypeTestResolution::Single;
8322 break;
8323 case lltok::kw_allOnes:
8324 TTRes.TheKind = TypeTestResolution::AllOnes;
8325 break;
8326 default:
8327 return error(Lex.getLoc(), "unexpected TypeTestResolution kind");
8328 }
8329 Lex.Lex();
8330
8331 if (parseToken(lltok::comma, "expected ',' here") ||
8332 parseToken(lltok::kw_sizeM1BitWidth, "expected 'sizeM1BitWidth' here") ||
8333 parseToken(lltok::colon, "expected ':' here") ||
8334 parseUInt32(TTRes.SizeM1BitWidth))
8335 return true;
8336
8337 // parse optional fields
8338 while (EatIfPresent(lltok::comma)) {
8339 switch (Lex.getKind()) {
8340 case lltok::kw_alignLog2:
8341 Lex.Lex();
8342 if (parseToken(lltok::colon, "expected ':'") ||
8343 parseUInt64(TTRes.AlignLog2))
8344 return true;
8345 break;
8346 case lltok::kw_sizeM1:
8347 Lex.Lex();
8348 if (parseToken(lltok::colon, "expected ':'") || parseUInt64(TTRes.SizeM1))
8349 return true;
8350 break;
8351 case lltok::kw_bitMask: {
8352 unsigned Val;
8353 Lex.Lex();
8354 if (parseToken(lltok::colon, "expected ':'") || parseUInt32(Val))
8355 return true;
8356 assert(Val <= 0xff);
8357 TTRes.BitMask = (uint8_t)Val;
8358 break;
8359 }
8360 case lltok::kw_inlineBits:
8361 Lex.Lex();
8362 if (parseToken(lltok::colon, "expected ':'") ||
8363 parseUInt64(TTRes.InlineBits))
8364 return true;
8365 break;
8366 default:
8367 return error(Lex.getLoc(), "expected optional TypeTestResolution field");
8368 }
8369 }
8370
8371 if (parseToken(lltok::rparen, "expected ')' here"))
8372 return true;
8373
8374 return false;
8375 }
8376
8377 /// OptionalWpdResolutions
8378 /// ::= 'wpsResolutions' ':' '(' WpdResolution [',' WpdResolution]* ')'
8379 /// WpdResolution ::= '(' 'offset' ':' UInt64 ',' WpdRes ')'
parseOptionalWpdResolutions(std::map<uint64_t,WholeProgramDevirtResolution> & WPDResMap)8380 bool LLParser::parseOptionalWpdResolutions(
8381 std::map<uint64_t, WholeProgramDevirtResolution> &WPDResMap) {
8382 if (parseToken(lltok::kw_wpdResolutions, "expected 'wpdResolutions' here") ||
8383 parseToken(lltok::colon, "expected ':' here") ||
8384 parseToken(lltok::lparen, "expected '(' here"))
8385 return true;
8386
8387 do {
8388 uint64_t Offset;
8389 WholeProgramDevirtResolution WPDRes;
8390 if (parseToken(lltok::lparen, "expected '(' here") ||
8391 parseToken(lltok::kw_offset, "expected 'offset' here") ||
8392 parseToken(lltok::colon, "expected ':' here") || parseUInt64(Offset) ||
8393 parseToken(lltok::comma, "expected ',' here") || parseWpdRes(WPDRes) ||
8394 parseToken(lltok::rparen, "expected ')' here"))
8395 return true;
8396 WPDResMap[Offset] = WPDRes;
8397 } while (EatIfPresent(lltok::comma));
8398
8399 if (parseToken(lltok::rparen, "expected ')' here"))
8400 return true;
8401
8402 return false;
8403 }
8404
8405 /// WpdRes
8406 /// ::= 'wpdRes' ':' '(' 'kind' ':' 'indir'
8407 /// [',' OptionalResByArg]? ')'
8408 /// ::= 'wpdRes' ':' '(' 'kind' ':' 'singleImpl'
8409 /// ',' 'singleImplName' ':' STRINGCONSTANT ','
8410 /// [',' OptionalResByArg]? ')'
8411 /// ::= 'wpdRes' ':' '(' 'kind' ':' 'branchFunnel'
8412 /// [',' OptionalResByArg]? ')'
parseWpdRes(WholeProgramDevirtResolution & WPDRes)8413 bool LLParser::parseWpdRes(WholeProgramDevirtResolution &WPDRes) {
8414 if (parseToken(lltok::kw_wpdRes, "expected 'wpdRes' here") ||
8415 parseToken(lltok::colon, "expected ':' here") ||
8416 parseToken(lltok::lparen, "expected '(' here") ||
8417 parseToken(lltok::kw_kind, "expected 'kind' here") ||
8418 parseToken(lltok::colon, "expected ':' here"))
8419 return true;
8420
8421 switch (Lex.getKind()) {
8422 case lltok::kw_indir:
8423 WPDRes.TheKind = WholeProgramDevirtResolution::Indir;
8424 break;
8425 case lltok::kw_singleImpl:
8426 WPDRes.TheKind = WholeProgramDevirtResolution::SingleImpl;
8427 break;
8428 case lltok::kw_branchFunnel:
8429 WPDRes.TheKind = WholeProgramDevirtResolution::BranchFunnel;
8430 break;
8431 default:
8432 return error(Lex.getLoc(), "unexpected WholeProgramDevirtResolution kind");
8433 }
8434 Lex.Lex();
8435
8436 // parse optional fields
8437 while (EatIfPresent(lltok::comma)) {
8438 switch (Lex.getKind()) {
8439 case lltok::kw_singleImplName:
8440 Lex.Lex();
8441 if (parseToken(lltok::colon, "expected ':' here") ||
8442 parseStringConstant(WPDRes.SingleImplName))
8443 return true;
8444 break;
8445 case lltok::kw_resByArg:
8446 if (parseOptionalResByArg(WPDRes.ResByArg))
8447 return true;
8448 break;
8449 default:
8450 return error(Lex.getLoc(),
8451 "expected optional WholeProgramDevirtResolution field");
8452 }
8453 }
8454
8455 if (parseToken(lltok::rparen, "expected ')' here"))
8456 return true;
8457
8458 return false;
8459 }
8460
8461 /// OptionalResByArg
8462 /// ::= 'wpdRes' ':' '(' ResByArg[, ResByArg]* ')'
8463 /// ResByArg ::= Args ',' 'byArg' ':' '(' 'kind' ':'
8464 /// ( 'indir' | 'uniformRetVal' | 'UniqueRetVal' |
8465 /// 'virtualConstProp' )
8466 /// [',' 'info' ':' UInt64]? [',' 'byte' ':' UInt32]?
8467 /// [',' 'bit' ':' UInt32]? ')'
parseOptionalResByArg(std::map<std::vector<uint64_t>,WholeProgramDevirtResolution::ByArg> & ResByArg)8468 bool LLParser::parseOptionalResByArg(
8469 std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg>
8470 &ResByArg) {
8471 if (parseToken(lltok::kw_resByArg, "expected 'resByArg' here") ||
8472 parseToken(lltok::colon, "expected ':' here") ||
8473 parseToken(lltok::lparen, "expected '(' here"))
8474 return true;
8475
8476 do {
8477 std::vector<uint64_t> Args;
8478 if (parseArgs(Args) || parseToken(lltok::comma, "expected ',' here") ||
8479 parseToken(lltok::kw_byArg, "expected 'byArg here") ||
8480 parseToken(lltok::colon, "expected ':' here") ||
8481 parseToken(lltok::lparen, "expected '(' here") ||
8482 parseToken(lltok::kw_kind, "expected 'kind' here") ||
8483 parseToken(lltok::colon, "expected ':' here"))
8484 return true;
8485
8486 WholeProgramDevirtResolution::ByArg ByArg;
8487 switch (Lex.getKind()) {
8488 case lltok::kw_indir:
8489 ByArg.TheKind = WholeProgramDevirtResolution::ByArg::Indir;
8490 break;
8491 case lltok::kw_uniformRetVal:
8492 ByArg.TheKind = WholeProgramDevirtResolution::ByArg::UniformRetVal;
8493 break;
8494 case lltok::kw_uniqueRetVal:
8495 ByArg.TheKind = WholeProgramDevirtResolution::ByArg::UniqueRetVal;
8496 break;
8497 case lltok::kw_virtualConstProp:
8498 ByArg.TheKind = WholeProgramDevirtResolution::ByArg::VirtualConstProp;
8499 break;
8500 default:
8501 return error(Lex.getLoc(),
8502 "unexpected WholeProgramDevirtResolution::ByArg kind");
8503 }
8504 Lex.Lex();
8505
8506 // parse optional fields
8507 while (EatIfPresent(lltok::comma)) {
8508 switch (Lex.getKind()) {
8509 case lltok::kw_info:
8510 Lex.Lex();
8511 if (parseToken(lltok::colon, "expected ':' here") ||
8512 parseUInt64(ByArg.Info))
8513 return true;
8514 break;
8515 case lltok::kw_byte:
8516 Lex.Lex();
8517 if (parseToken(lltok::colon, "expected ':' here") ||
8518 parseUInt32(ByArg.Byte))
8519 return true;
8520 break;
8521 case lltok::kw_bit:
8522 Lex.Lex();
8523 if (parseToken(lltok::colon, "expected ':' here") ||
8524 parseUInt32(ByArg.Bit))
8525 return true;
8526 break;
8527 default:
8528 return error(Lex.getLoc(),
8529 "expected optional whole program devirt field");
8530 }
8531 }
8532
8533 if (parseToken(lltok::rparen, "expected ')' here"))
8534 return true;
8535
8536 ResByArg[Args] = ByArg;
8537 } while (EatIfPresent(lltok::comma));
8538
8539 if (parseToken(lltok::rparen, "expected ')' here"))
8540 return true;
8541
8542 return false;
8543 }
8544
8545 /// OptionalResByArg
8546 /// ::= 'args' ':' '(' UInt64[, UInt64]* ')'
parseArgs(std::vector<uint64_t> & Args)8547 bool LLParser::parseArgs(std::vector<uint64_t> &Args) {
8548 if (parseToken(lltok::kw_args, "expected 'args' here") ||
8549 parseToken(lltok::colon, "expected ':' here") ||
8550 parseToken(lltok::lparen, "expected '(' here"))
8551 return true;
8552
8553 do {
8554 uint64_t Val;
8555 if (parseUInt64(Val))
8556 return true;
8557 Args.push_back(Val);
8558 } while (EatIfPresent(lltok::comma));
8559
8560 if (parseToken(lltok::rparen, "expected ')' here"))
8561 return true;
8562
8563 return false;
8564 }
8565
8566 static const auto FwdVIRef = (GlobalValueSummaryMapTy::value_type *)-8;
8567
resolveFwdRef(ValueInfo * Fwd,ValueInfo & Resolved)8568 static void resolveFwdRef(ValueInfo *Fwd, ValueInfo &Resolved) {
8569 bool ReadOnly = Fwd->isReadOnly();
8570 bool WriteOnly = Fwd->isWriteOnly();
8571 assert(!(ReadOnly && WriteOnly));
8572 *Fwd = Resolved;
8573 if (ReadOnly)
8574 Fwd->setReadOnly();
8575 if (WriteOnly)
8576 Fwd->setWriteOnly();
8577 }
8578
8579 /// Stores the given Name/GUID and associated summary into the Index.
8580 /// Also updates any forward references to the associated entry ID.
addGlobalValueToIndex(std::string Name,GlobalValue::GUID GUID,GlobalValue::LinkageTypes Linkage,unsigned ID,std::unique_ptr<GlobalValueSummary> Summary)8581 void LLParser::addGlobalValueToIndex(
8582 std::string Name, GlobalValue::GUID GUID, GlobalValue::LinkageTypes Linkage,
8583 unsigned ID, std::unique_ptr<GlobalValueSummary> Summary) {
8584 // First create the ValueInfo utilizing the Name or GUID.
8585 ValueInfo VI;
8586 if (GUID != 0) {
8587 assert(Name.empty());
8588 VI = Index->getOrInsertValueInfo(GUID);
8589 } else {
8590 assert(!Name.empty());
8591 if (M) {
8592 auto *GV = M->getNamedValue(Name);
8593 assert(GV);
8594 VI = Index->getOrInsertValueInfo(GV);
8595 } else {
8596 assert(
8597 (!GlobalValue::isLocalLinkage(Linkage) || !SourceFileName.empty()) &&
8598 "Need a source_filename to compute GUID for local");
8599 GUID = GlobalValue::getGUID(
8600 GlobalValue::getGlobalIdentifier(Name, Linkage, SourceFileName));
8601 VI = Index->getOrInsertValueInfo(GUID, Index->saveString(Name));
8602 }
8603 }
8604
8605 // Resolve forward references from calls/refs
8606 auto FwdRefVIs = ForwardRefValueInfos.find(ID);
8607 if (FwdRefVIs != ForwardRefValueInfos.end()) {
8608 for (auto VIRef : FwdRefVIs->second) {
8609 assert(VIRef.first->getRef() == FwdVIRef &&
8610 "Forward referenced ValueInfo expected to be empty");
8611 resolveFwdRef(VIRef.first, VI);
8612 }
8613 ForwardRefValueInfos.erase(FwdRefVIs);
8614 }
8615
8616 // Resolve forward references from aliases
8617 auto FwdRefAliasees = ForwardRefAliasees.find(ID);
8618 if (FwdRefAliasees != ForwardRefAliasees.end()) {
8619 for (auto AliaseeRef : FwdRefAliasees->second) {
8620 assert(!AliaseeRef.first->hasAliasee() &&
8621 "Forward referencing alias already has aliasee");
8622 assert(Summary && "Aliasee must be a definition");
8623 AliaseeRef.first->setAliasee(VI, Summary.get());
8624 }
8625 ForwardRefAliasees.erase(FwdRefAliasees);
8626 }
8627
8628 // Add the summary if one was provided.
8629 if (Summary)
8630 Index->addGlobalValueSummary(VI, std::move(Summary));
8631
8632 // Save the associated ValueInfo for use in later references by ID.
8633 if (ID == NumberedValueInfos.size())
8634 NumberedValueInfos.push_back(VI);
8635 else {
8636 // Handle non-continuous numbers (to make test simplification easier).
8637 if (ID > NumberedValueInfos.size())
8638 NumberedValueInfos.resize(ID + 1);
8639 NumberedValueInfos[ID] = VI;
8640 }
8641 }
8642
8643 /// parseSummaryIndexFlags
8644 /// ::= 'flags' ':' UInt64
parseSummaryIndexFlags()8645 bool LLParser::parseSummaryIndexFlags() {
8646 assert(Lex.getKind() == lltok::kw_flags);
8647 Lex.Lex();
8648
8649 if (parseToken(lltok::colon, "expected ':' here"))
8650 return true;
8651 uint64_t Flags;
8652 if (parseUInt64(Flags))
8653 return true;
8654 if (Index)
8655 Index->setFlags(Flags);
8656 return false;
8657 }
8658
8659 /// parseBlockCount
8660 /// ::= 'blockcount' ':' UInt64
parseBlockCount()8661 bool LLParser::parseBlockCount() {
8662 assert(Lex.getKind() == lltok::kw_blockcount);
8663 Lex.Lex();
8664
8665 if (parseToken(lltok::colon, "expected ':' here"))
8666 return true;
8667 uint64_t BlockCount;
8668 if (parseUInt64(BlockCount))
8669 return true;
8670 if (Index)
8671 Index->setBlockCount(BlockCount);
8672 return false;
8673 }
8674
8675 /// parseGVEntry
8676 /// ::= 'gv' ':' '(' ('name' ':' STRINGCONSTANT | 'guid' ':' UInt64)
8677 /// [',' 'summaries' ':' Summary[',' Summary]* ]? ')'
8678 /// Summary ::= '(' (FunctionSummary | VariableSummary | AliasSummary) ')'
parseGVEntry(unsigned ID)8679 bool LLParser::parseGVEntry(unsigned ID) {
8680 assert(Lex.getKind() == lltok::kw_gv);
8681 Lex.Lex();
8682
8683 if (parseToken(lltok::colon, "expected ':' here") ||
8684 parseToken(lltok::lparen, "expected '(' here"))
8685 return true;
8686
8687 std::string Name;
8688 GlobalValue::GUID GUID = 0;
8689 switch (Lex.getKind()) {
8690 case lltok::kw_name:
8691 Lex.Lex();
8692 if (parseToken(lltok::colon, "expected ':' here") ||
8693 parseStringConstant(Name))
8694 return true;
8695 // Can't create GUID/ValueInfo until we have the linkage.
8696 break;
8697 case lltok::kw_guid:
8698 Lex.Lex();
8699 if (parseToken(lltok::colon, "expected ':' here") || parseUInt64(GUID))
8700 return true;
8701 break;
8702 default:
8703 return error(Lex.getLoc(), "expected name or guid tag");
8704 }
8705
8706 if (!EatIfPresent(lltok::comma)) {
8707 // No summaries. Wrap up.
8708 if (parseToken(lltok::rparen, "expected ')' here"))
8709 return true;
8710 // This was created for a call to an external or indirect target.
8711 // A GUID with no summary came from a VALUE_GUID record, dummy GUID
8712 // created for indirect calls with VP. A Name with no GUID came from
8713 // an external definition. We pass ExternalLinkage since that is only
8714 // used when the GUID must be computed from Name, and in that case
8715 // the symbol must have external linkage.
8716 addGlobalValueToIndex(Name, GUID, GlobalValue::ExternalLinkage, ID,
8717 nullptr);
8718 return false;
8719 }
8720
8721 // Have a list of summaries
8722 if (parseToken(lltok::kw_summaries, "expected 'summaries' here") ||
8723 parseToken(lltok::colon, "expected ':' here") ||
8724 parseToken(lltok::lparen, "expected '(' here"))
8725 return true;
8726 do {
8727 switch (Lex.getKind()) {
8728 case lltok::kw_function:
8729 if (parseFunctionSummary(Name, GUID, ID))
8730 return true;
8731 break;
8732 case lltok::kw_variable:
8733 if (parseVariableSummary(Name, GUID, ID))
8734 return true;
8735 break;
8736 case lltok::kw_alias:
8737 if (parseAliasSummary(Name, GUID, ID))
8738 return true;
8739 break;
8740 default:
8741 return error(Lex.getLoc(), "expected summary type");
8742 }
8743 } while (EatIfPresent(lltok::comma));
8744
8745 if (parseToken(lltok::rparen, "expected ')' here") ||
8746 parseToken(lltok::rparen, "expected ')' here"))
8747 return true;
8748
8749 return false;
8750 }
8751
8752 /// FunctionSummary
8753 /// ::= 'function' ':' '(' 'module' ':' ModuleReference ',' GVFlags
8754 /// ',' 'insts' ':' UInt32 [',' OptionalFFlags]? [',' OptionalCalls]?
8755 /// [',' OptionalTypeIdInfo]? [',' OptionalParamAccesses]?
8756 /// [',' OptionalRefs]? ')'
parseFunctionSummary(std::string Name,GlobalValue::GUID GUID,unsigned ID)8757 bool LLParser::parseFunctionSummary(std::string Name, GlobalValue::GUID GUID,
8758 unsigned ID) {
8759 assert(Lex.getKind() == lltok::kw_function);
8760 Lex.Lex();
8761
8762 StringRef ModulePath;
8763 GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
8764 GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility,
8765 /*NotEligibleToImport=*/false,
8766 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false);
8767 unsigned InstCount;
8768 std::vector<FunctionSummary::EdgeTy> Calls;
8769 FunctionSummary::TypeIdInfo TypeIdInfo;
8770 std::vector<FunctionSummary::ParamAccess> ParamAccesses;
8771 std::vector<ValueInfo> Refs;
8772 std::vector<CallsiteInfo> Callsites;
8773 std::vector<AllocInfo> Allocs;
8774 // Default is all-zeros (conservative values).
8775 FunctionSummary::FFlags FFlags = {};
8776 if (parseToken(lltok::colon, "expected ':' here") ||
8777 parseToken(lltok::lparen, "expected '(' here") ||
8778 parseModuleReference(ModulePath) ||
8779 parseToken(lltok::comma, "expected ',' here") || parseGVFlags(GVFlags) ||
8780 parseToken(lltok::comma, "expected ',' here") ||
8781 parseToken(lltok::kw_insts, "expected 'insts' here") ||
8782 parseToken(lltok::colon, "expected ':' here") || parseUInt32(InstCount))
8783 return true;
8784
8785 // parse optional fields
8786 while (EatIfPresent(lltok::comma)) {
8787 switch (Lex.getKind()) {
8788 case lltok::kw_funcFlags:
8789 if (parseOptionalFFlags(FFlags))
8790 return true;
8791 break;
8792 case lltok::kw_calls:
8793 if (parseOptionalCalls(Calls))
8794 return true;
8795 break;
8796 case lltok::kw_typeIdInfo:
8797 if (parseOptionalTypeIdInfo(TypeIdInfo))
8798 return true;
8799 break;
8800 case lltok::kw_refs:
8801 if (parseOptionalRefs(Refs))
8802 return true;
8803 break;
8804 case lltok::kw_params:
8805 if (parseOptionalParamAccesses(ParamAccesses))
8806 return true;
8807 break;
8808 case lltok::kw_allocs:
8809 if (parseOptionalAllocs(Allocs))
8810 return true;
8811 break;
8812 case lltok::kw_callsites:
8813 if (parseOptionalCallsites(Callsites))
8814 return true;
8815 break;
8816 default:
8817 return error(Lex.getLoc(), "expected optional function summary field");
8818 }
8819 }
8820
8821 if (parseToken(lltok::rparen, "expected ')' here"))
8822 return true;
8823
8824 auto FS = std::make_unique<FunctionSummary>(
8825 GVFlags, InstCount, FFlags, /*EntryCount=*/0, std::move(Refs),
8826 std::move(Calls), std::move(TypeIdInfo.TypeTests),
8827 std::move(TypeIdInfo.TypeTestAssumeVCalls),
8828 std::move(TypeIdInfo.TypeCheckedLoadVCalls),
8829 std::move(TypeIdInfo.TypeTestAssumeConstVCalls),
8830 std::move(TypeIdInfo.TypeCheckedLoadConstVCalls),
8831 std::move(ParamAccesses), std::move(Callsites), std::move(Allocs));
8832
8833 FS->setModulePath(ModulePath);
8834
8835 addGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
8836 ID, std::move(FS));
8837
8838 return false;
8839 }
8840
8841 /// VariableSummary
8842 /// ::= 'variable' ':' '(' 'module' ':' ModuleReference ',' GVFlags
8843 /// [',' OptionalRefs]? ')'
parseVariableSummary(std::string Name,GlobalValue::GUID GUID,unsigned ID)8844 bool LLParser::parseVariableSummary(std::string Name, GlobalValue::GUID GUID,
8845 unsigned ID) {
8846 assert(Lex.getKind() == lltok::kw_variable);
8847 Lex.Lex();
8848
8849 StringRef ModulePath;
8850 GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
8851 GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility,
8852 /*NotEligibleToImport=*/false,
8853 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false);
8854 GlobalVarSummary::GVarFlags GVarFlags(/*ReadOnly*/ false,
8855 /* WriteOnly */ false,
8856 /* Constant */ false,
8857 GlobalObject::VCallVisibilityPublic);
8858 std::vector<ValueInfo> Refs;
8859 VTableFuncList VTableFuncs;
8860 if (parseToken(lltok::colon, "expected ':' here") ||
8861 parseToken(lltok::lparen, "expected '(' here") ||
8862 parseModuleReference(ModulePath) ||
8863 parseToken(lltok::comma, "expected ',' here") || parseGVFlags(GVFlags) ||
8864 parseToken(lltok::comma, "expected ',' here") ||
8865 parseGVarFlags(GVarFlags))
8866 return true;
8867
8868 // parse optional fields
8869 while (EatIfPresent(lltok::comma)) {
8870 switch (Lex.getKind()) {
8871 case lltok::kw_vTableFuncs:
8872 if (parseOptionalVTableFuncs(VTableFuncs))
8873 return true;
8874 break;
8875 case lltok::kw_refs:
8876 if (parseOptionalRefs(Refs))
8877 return true;
8878 break;
8879 default:
8880 return error(Lex.getLoc(), "expected optional variable summary field");
8881 }
8882 }
8883
8884 if (parseToken(lltok::rparen, "expected ')' here"))
8885 return true;
8886
8887 auto GS =
8888 std::make_unique<GlobalVarSummary>(GVFlags, GVarFlags, std::move(Refs));
8889
8890 GS->setModulePath(ModulePath);
8891 GS->setVTableFuncs(std::move(VTableFuncs));
8892
8893 addGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
8894 ID, std::move(GS));
8895
8896 return false;
8897 }
8898
8899 /// AliasSummary
8900 /// ::= 'alias' ':' '(' 'module' ':' ModuleReference ',' GVFlags ','
8901 /// 'aliasee' ':' GVReference ')'
parseAliasSummary(std::string Name,GlobalValue::GUID GUID,unsigned ID)8902 bool LLParser::parseAliasSummary(std::string Name, GlobalValue::GUID GUID,
8903 unsigned ID) {
8904 assert(Lex.getKind() == lltok::kw_alias);
8905 LocTy Loc = Lex.getLoc();
8906 Lex.Lex();
8907
8908 StringRef ModulePath;
8909 GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
8910 GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility,
8911 /*NotEligibleToImport=*/false,
8912 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false);
8913 if (parseToken(lltok::colon, "expected ':' here") ||
8914 parseToken(lltok::lparen, "expected '(' here") ||
8915 parseModuleReference(ModulePath) ||
8916 parseToken(lltok::comma, "expected ',' here") || parseGVFlags(GVFlags) ||
8917 parseToken(lltok::comma, "expected ',' here") ||
8918 parseToken(lltok::kw_aliasee, "expected 'aliasee' here") ||
8919 parseToken(lltok::colon, "expected ':' here"))
8920 return true;
8921
8922 ValueInfo AliaseeVI;
8923 unsigned GVId;
8924 if (parseGVReference(AliaseeVI, GVId))
8925 return true;
8926
8927 if (parseToken(lltok::rparen, "expected ')' here"))
8928 return true;
8929
8930 auto AS = std::make_unique<AliasSummary>(GVFlags);
8931
8932 AS->setModulePath(ModulePath);
8933
8934 // Record forward reference if the aliasee is not parsed yet.
8935 if (AliaseeVI.getRef() == FwdVIRef) {
8936 ForwardRefAliasees[GVId].emplace_back(AS.get(), Loc);
8937 } else {
8938 auto Summary = Index->findSummaryInModule(AliaseeVI, ModulePath);
8939 assert(Summary && "Aliasee must be a definition");
8940 AS->setAliasee(AliaseeVI, Summary);
8941 }
8942
8943 addGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
8944 ID, std::move(AS));
8945
8946 return false;
8947 }
8948
8949 /// Flag
8950 /// ::= [0|1]
parseFlag(unsigned & Val)8951 bool LLParser::parseFlag(unsigned &Val) {
8952 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
8953 return tokError("expected integer");
8954 Val = (unsigned)Lex.getAPSIntVal().getBoolValue();
8955 Lex.Lex();
8956 return false;
8957 }
8958
8959 /// OptionalFFlags
8960 /// := 'funcFlags' ':' '(' ['readNone' ':' Flag]?
8961 /// [',' 'readOnly' ':' Flag]? [',' 'noRecurse' ':' Flag]?
8962 /// [',' 'returnDoesNotAlias' ':' Flag]? ')'
8963 /// [',' 'noInline' ':' Flag]? ')'
8964 /// [',' 'alwaysInline' ':' Flag]? ')'
8965 /// [',' 'noUnwind' ':' Flag]? ')'
8966 /// [',' 'mayThrow' ':' Flag]? ')'
8967 /// [',' 'hasUnknownCall' ':' Flag]? ')'
8968 /// [',' 'mustBeUnreachable' ':' Flag]? ')'
8969
parseOptionalFFlags(FunctionSummary::FFlags & FFlags)8970 bool LLParser::parseOptionalFFlags(FunctionSummary::FFlags &FFlags) {
8971 assert(Lex.getKind() == lltok::kw_funcFlags);
8972 Lex.Lex();
8973
8974 if (parseToken(lltok::colon, "expected ':' in funcFlags") ||
8975 parseToken(lltok::lparen, "expected '(' in funcFlags"))
8976 return true;
8977
8978 do {
8979 unsigned Val = 0;
8980 switch (Lex.getKind()) {
8981 case lltok::kw_readNone:
8982 Lex.Lex();
8983 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
8984 return true;
8985 FFlags.ReadNone = Val;
8986 break;
8987 case lltok::kw_readOnly:
8988 Lex.Lex();
8989 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
8990 return true;
8991 FFlags.ReadOnly = Val;
8992 break;
8993 case lltok::kw_noRecurse:
8994 Lex.Lex();
8995 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
8996 return true;
8997 FFlags.NoRecurse = Val;
8998 break;
8999 case lltok::kw_returnDoesNotAlias:
9000 Lex.Lex();
9001 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9002 return true;
9003 FFlags.ReturnDoesNotAlias = Val;
9004 break;
9005 case lltok::kw_noInline:
9006 Lex.Lex();
9007 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9008 return true;
9009 FFlags.NoInline = Val;
9010 break;
9011 case lltok::kw_alwaysInline:
9012 Lex.Lex();
9013 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9014 return true;
9015 FFlags.AlwaysInline = Val;
9016 break;
9017 case lltok::kw_noUnwind:
9018 Lex.Lex();
9019 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9020 return true;
9021 FFlags.NoUnwind = Val;
9022 break;
9023 case lltok::kw_mayThrow:
9024 Lex.Lex();
9025 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9026 return true;
9027 FFlags.MayThrow = Val;
9028 break;
9029 case lltok::kw_hasUnknownCall:
9030 Lex.Lex();
9031 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9032 return true;
9033 FFlags.HasUnknownCall = Val;
9034 break;
9035 case lltok::kw_mustBeUnreachable:
9036 Lex.Lex();
9037 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9038 return true;
9039 FFlags.MustBeUnreachable = Val;
9040 break;
9041 default:
9042 return error(Lex.getLoc(), "expected function flag type");
9043 }
9044 } while (EatIfPresent(lltok::comma));
9045
9046 if (parseToken(lltok::rparen, "expected ')' in funcFlags"))
9047 return true;
9048
9049 return false;
9050 }
9051
9052 /// OptionalCalls
9053 /// := 'calls' ':' '(' Call [',' Call]* ')'
9054 /// Call ::= '(' 'callee' ':' GVReference
9055 /// [( ',' 'hotness' ':' Hotness | ',' 'relbf' ':' UInt32 )]? ')'
parseOptionalCalls(std::vector<FunctionSummary::EdgeTy> & Calls)9056 bool LLParser::parseOptionalCalls(std::vector<FunctionSummary::EdgeTy> &Calls) {
9057 assert(Lex.getKind() == lltok::kw_calls);
9058 Lex.Lex();
9059
9060 if (parseToken(lltok::colon, "expected ':' in calls") ||
9061 parseToken(lltok::lparen, "expected '(' in calls"))
9062 return true;
9063
9064 IdToIndexMapType IdToIndexMap;
9065 // parse each call edge
9066 do {
9067 ValueInfo VI;
9068 if (parseToken(lltok::lparen, "expected '(' in call") ||
9069 parseToken(lltok::kw_callee, "expected 'callee' in call") ||
9070 parseToken(lltok::colon, "expected ':'"))
9071 return true;
9072
9073 LocTy Loc = Lex.getLoc();
9074 unsigned GVId;
9075 if (parseGVReference(VI, GVId))
9076 return true;
9077
9078 CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown;
9079 unsigned RelBF = 0;
9080 if (EatIfPresent(lltok::comma)) {
9081 // Expect either hotness or relbf
9082 if (EatIfPresent(lltok::kw_hotness)) {
9083 if (parseToken(lltok::colon, "expected ':'") || parseHotness(Hotness))
9084 return true;
9085 } else {
9086 if (parseToken(lltok::kw_relbf, "expected relbf") ||
9087 parseToken(lltok::colon, "expected ':'") || parseUInt32(RelBF))
9088 return true;
9089 }
9090 }
9091 // Keep track of the Call array index needing a forward reference.
9092 // We will save the location of the ValueInfo needing an update, but
9093 // can only do so once the std::vector is finalized.
9094 if (VI.getRef() == FwdVIRef)
9095 IdToIndexMap[GVId].push_back(std::make_pair(Calls.size(), Loc));
9096 Calls.push_back(FunctionSummary::EdgeTy{VI, CalleeInfo(Hotness, RelBF)});
9097
9098 if (parseToken(lltok::rparen, "expected ')' in call"))
9099 return true;
9100 } while (EatIfPresent(lltok::comma));
9101
9102 // Now that the Calls vector is finalized, it is safe to save the locations
9103 // of any forward GV references that need updating later.
9104 for (auto I : IdToIndexMap) {
9105 auto &Infos = ForwardRefValueInfos[I.first];
9106 for (auto P : I.second) {
9107 assert(Calls[P.first].first.getRef() == FwdVIRef &&
9108 "Forward referenced ValueInfo expected to be empty");
9109 Infos.emplace_back(&Calls[P.first].first, P.second);
9110 }
9111 }
9112
9113 if (parseToken(lltok::rparen, "expected ')' in calls"))
9114 return true;
9115
9116 return false;
9117 }
9118
9119 /// Hotness
9120 /// := ('unknown'|'cold'|'none'|'hot'|'critical')
parseHotness(CalleeInfo::HotnessType & Hotness)9121 bool LLParser::parseHotness(CalleeInfo::HotnessType &Hotness) {
9122 switch (Lex.getKind()) {
9123 case lltok::kw_unknown:
9124 Hotness = CalleeInfo::HotnessType::Unknown;
9125 break;
9126 case lltok::kw_cold:
9127 Hotness = CalleeInfo::HotnessType::Cold;
9128 break;
9129 case lltok::kw_none:
9130 Hotness = CalleeInfo::HotnessType::None;
9131 break;
9132 case lltok::kw_hot:
9133 Hotness = CalleeInfo::HotnessType::Hot;
9134 break;
9135 case lltok::kw_critical:
9136 Hotness = CalleeInfo::HotnessType::Critical;
9137 break;
9138 default:
9139 return error(Lex.getLoc(), "invalid call edge hotness");
9140 }
9141 Lex.Lex();
9142 return false;
9143 }
9144
9145 /// OptionalVTableFuncs
9146 /// := 'vTableFuncs' ':' '(' VTableFunc [',' VTableFunc]* ')'
9147 /// VTableFunc ::= '(' 'virtFunc' ':' GVReference ',' 'offset' ':' UInt64 ')'
parseOptionalVTableFuncs(VTableFuncList & VTableFuncs)9148 bool LLParser::parseOptionalVTableFuncs(VTableFuncList &VTableFuncs) {
9149 assert(Lex.getKind() == lltok::kw_vTableFuncs);
9150 Lex.Lex();
9151
9152 if (parseToken(lltok::colon, "expected ':' in vTableFuncs") ||
9153 parseToken(lltok::lparen, "expected '(' in vTableFuncs"))
9154 return true;
9155
9156 IdToIndexMapType IdToIndexMap;
9157 // parse each virtual function pair
9158 do {
9159 ValueInfo VI;
9160 if (parseToken(lltok::lparen, "expected '(' in vTableFunc") ||
9161 parseToken(lltok::kw_virtFunc, "expected 'callee' in vTableFunc") ||
9162 parseToken(lltok::colon, "expected ':'"))
9163 return true;
9164
9165 LocTy Loc = Lex.getLoc();
9166 unsigned GVId;
9167 if (parseGVReference(VI, GVId))
9168 return true;
9169
9170 uint64_t Offset;
9171 if (parseToken(lltok::comma, "expected comma") ||
9172 parseToken(lltok::kw_offset, "expected offset") ||
9173 parseToken(lltok::colon, "expected ':'") || parseUInt64(Offset))
9174 return true;
9175
9176 // Keep track of the VTableFuncs array index needing a forward reference.
9177 // We will save the location of the ValueInfo needing an update, but
9178 // can only do so once the std::vector is finalized.
9179 if (VI == EmptyVI)
9180 IdToIndexMap[GVId].push_back(std::make_pair(VTableFuncs.size(), Loc));
9181 VTableFuncs.push_back({VI, Offset});
9182
9183 if (parseToken(lltok::rparen, "expected ')' in vTableFunc"))
9184 return true;
9185 } while (EatIfPresent(lltok::comma));
9186
9187 // Now that the VTableFuncs vector is finalized, it is safe to save the
9188 // locations of any forward GV references that need updating later.
9189 for (auto I : IdToIndexMap) {
9190 auto &Infos = ForwardRefValueInfos[I.first];
9191 for (auto P : I.second) {
9192 assert(VTableFuncs[P.first].FuncVI == EmptyVI &&
9193 "Forward referenced ValueInfo expected to be empty");
9194 Infos.emplace_back(&VTableFuncs[P.first].FuncVI, P.second);
9195 }
9196 }
9197
9198 if (parseToken(lltok::rparen, "expected ')' in vTableFuncs"))
9199 return true;
9200
9201 return false;
9202 }
9203
9204 /// ParamNo := 'param' ':' UInt64
parseParamNo(uint64_t & ParamNo)9205 bool LLParser::parseParamNo(uint64_t &ParamNo) {
9206 if (parseToken(lltok::kw_param, "expected 'param' here") ||
9207 parseToken(lltok::colon, "expected ':' here") || parseUInt64(ParamNo))
9208 return true;
9209 return false;
9210 }
9211
9212 /// ParamAccessOffset := 'offset' ':' '[' APSINTVAL ',' APSINTVAL ']'
parseParamAccessOffset(ConstantRange & Range)9213 bool LLParser::parseParamAccessOffset(ConstantRange &Range) {
9214 APSInt Lower;
9215 APSInt Upper;
9216 auto ParseAPSInt = [&](APSInt &Val) {
9217 if (Lex.getKind() != lltok::APSInt)
9218 return tokError("expected integer");
9219 Val = Lex.getAPSIntVal();
9220 Val = Val.extOrTrunc(FunctionSummary::ParamAccess::RangeWidth);
9221 Val.setIsSigned(true);
9222 Lex.Lex();
9223 return false;
9224 };
9225 if (parseToken(lltok::kw_offset, "expected 'offset' here") ||
9226 parseToken(lltok::colon, "expected ':' here") ||
9227 parseToken(lltok::lsquare, "expected '[' here") || ParseAPSInt(Lower) ||
9228 parseToken(lltok::comma, "expected ',' here") || ParseAPSInt(Upper) ||
9229 parseToken(lltok::rsquare, "expected ']' here"))
9230 return true;
9231
9232 ++Upper;
9233 Range =
9234 (Lower == Upper && !Lower.isMaxValue())
9235 ? ConstantRange::getEmpty(FunctionSummary::ParamAccess::RangeWidth)
9236 : ConstantRange(Lower, Upper);
9237
9238 return false;
9239 }
9240
9241 /// ParamAccessCall
9242 /// := '(' 'callee' ':' GVReference ',' ParamNo ',' ParamAccessOffset ')'
parseParamAccessCall(FunctionSummary::ParamAccess::Call & Call,IdLocListType & IdLocList)9243 bool LLParser::parseParamAccessCall(FunctionSummary::ParamAccess::Call &Call,
9244 IdLocListType &IdLocList) {
9245 if (parseToken(lltok::lparen, "expected '(' here") ||
9246 parseToken(lltok::kw_callee, "expected 'callee' here") ||
9247 parseToken(lltok::colon, "expected ':' here"))
9248 return true;
9249
9250 unsigned GVId;
9251 ValueInfo VI;
9252 LocTy Loc = Lex.getLoc();
9253 if (parseGVReference(VI, GVId))
9254 return true;
9255
9256 Call.Callee = VI;
9257 IdLocList.emplace_back(GVId, Loc);
9258
9259 if (parseToken(lltok::comma, "expected ',' here") ||
9260 parseParamNo(Call.ParamNo) ||
9261 parseToken(lltok::comma, "expected ',' here") ||
9262 parseParamAccessOffset(Call.Offsets))
9263 return true;
9264
9265 if (parseToken(lltok::rparen, "expected ')' here"))
9266 return true;
9267
9268 return false;
9269 }
9270
9271 /// ParamAccess
9272 /// := '(' ParamNo ',' ParamAccessOffset [',' OptionalParamAccessCalls]? ')'
9273 /// OptionalParamAccessCalls := '(' Call [',' Call]* ')'
parseParamAccess(FunctionSummary::ParamAccess & Param,IdLocListType & IdLocList)9274 bool LLParser::parseParamAccess(FunctionSummary::ParamAccess &Param,
9275 IdLocListType &IdLocList) {
9276 if (parseToken(lltok::lparen, "expected '(' here") ||
9277 parseParamNo(Param.ParamNo) ||
9278 parseToken(lltok::comma, "expected ',' here") ||
9279 parseParamAccessOffset(Param.Use))
9280 return true;
9281
9282 if (EatIfPresent(lltok::comma)) {
9283 if (parseToken(lltok::kw_calls, "expected 'calls' here") ||
9284 parseToken(lltok::colon, "expected ':' here") ||
9285 parseToken(lltok::lparen, "expected '(' here"))
9286 return true;
9287 do {
9288 FunctionSummary::ParamAccess::Call Call;
9289 if (parseParamAccessCall(Call, IdLocList))
9290 return true;
9291 Param.Calls.push_back(Call);
9292 } while (EatIfPresent(lltok::comma));
9293
9294 if (parseToken(lltok::rparen, "expected ')' here"))
9295 return true;
9296 }
9297
9298 if (parseToken(lltok::rparen, "expected ')' here"))
9299 return true;
9300
9301 return false;
9302 }
9303
9304 /// OptionalParamAccesses
9305 /// := 'params' ':' '(' ParamAccess [',' ParamAccess]* ')'
parseOptionalParamAccesses(std::vector<FunctionSummary::ParamAccess> & Params)9306 bool LLParser::parseOptionalParamAccesses(
9307 std::vector<FunctionSummary::ParamAccess> &Params) {
9308 assert(Lex.getKind() == lltok::kw_params);
9309 Lex.Lex();
9310
9311 if (parseToken(lltok::colon, "expected ':' here") ||
9312 parseToken(lltok::lparen, "expected '(' here"))
9313 return true;
9314
9315 IdLocListType VContexts;
9316 size_t CallsNum = 0;
9317 do {
9318 FunctionSummary::ParamAccess ParamAccess;
9319 if (parseParamAccess(ParamAccess, VContexts))
9320 return true;
9321 CallsNum += ParamAccess.Calls.size();
9322 assert(VContexts.size() == CallsNum);
9323 (void)CallsNum;
9324 Params.emplace_back(std::move(ParamAccess));
9325 } while (EatIfPresent(lltok::comma));
9326
9327 if (parseToken(lltok::rparen, "expected ')' here"))
9328 return true;
9329
9330 // Now that the Params is finalized, it is safe to save the locations
9331 // of any forward GV references that need updating later.
9332 IdLocListType::const_iterator ItContext = VContexts.begin();
9333 for (auto &PA : Params) {
9334 for (auto &C : PA.Calls) {
9335 if (C.Callee.getRef() == FwdVIRef)
9336 ForwardRefValueInfos[ItContext->first].emplace_back(&C.Callee,
9337 ItContext->second);
9338 ++ItContext;
9339 }
9340 }
9341 assert(ItContext == VContexts.end());
9342
9343 return false;
9344 }
9345
9346 /// OptionalRefs
9347 /// := 'refs' ':' '(' GVReference [',' GVReference]* ')'
parseOptionalRefs(std::vector<ValueInfo> & Refs)9348 bool LLParser::parseOptionalRefs(std::vector<ValueInfo> &Refs) {
9349 assert(Lex.getKind() == lltok::kw_refs);
9350 Lex.Lex();
9351
9352 if (parseToken(lltok::colon, "expected ':' in refs") ||
9353 parseToken(lltok::lparen, "expected '(' in refs"))
9354 return true;
9355
9356 struct ValueContext {
9357 ValueInfo VI;
9358 unsigned GVId;
9359 LocTy Loc;
9360 };
9361 std::vector<ValueContext> VContexts;
9362 // parse each ref edge
9363 do {
9364 ValueContext VC;
9365 VC.Loc = Lex.getLoc();
9366 if (parseGVReference(VC.VI, VC.GVId))
9367 return true;
9368 VContexts.push_back(VC);
9369 } while (EatIfPresent(lltok::comma));
9370
9371 // Sort value contexts so that ones with writeonly
9372 // and readonly ValueInfo are at the end of VContexts vector.
9373 // See FunctionSummary::specialRefCounts()
9374 llvm::sort(VContexts, [](const ValueContext &VC1, const ValueContext &VC2) {
9375 return VC1.VI.getAccessSpecifier() < VC2.VI.getAccessSpecifier();
9376 });
9377
9378 IdToIndexMapType IdToIndexMap;
9379 for (auto &VC : VContexts) {
9380 // Keep track of the Refs array index needing a forward reference.
9381 // We will save the location of the ValueInfo needing an update, but
9382 // can only do so once the std::vector is finalized.
9383 if (VC.VI.getRef() == FwdVIRef)
9384 IdToIndexMap[VC.GVId].push_back(std::make_pair(Refs.size(), VC.Loc));
9385 Refs.push_back(VC.VI);
9386 }
9387
9388 // Now that the Refs vector is finalized, it is safe to save the locations
9389 // of any forward GV references that need updating later.
9390 for (auto I : IdToIndexMap) {
9391 auto &Infos = ForwardRefValueInfos[I.first];
9392 for (auto P : I.second) {
9393 assert(Refs[P.first].getRef() == FwdVIRef &&
9394 "Forward referenced ValueInfo expected to be empty");
9395 Infos.emplace_back(&Refs[P.first], P.second);
9396 }
9397 }
9398
9399 if (parseToken(lltok::rparen, "expected ')' in refs"))
9400 return true;
9401
9402 return false;
9403 }
9404
9405 /// OptionalTypeIdInfo
9406 /// := 'typeidinfo' ':' '(' [',' TypeTests]? [',' TypeTestAssumeVCalls]?
9407 /// [',' TypeCheckedLoadVCalls]? [',' TypeTestAssumeConstVCalls]?
9408 /// [',' TypeCheckedLoadConstVCalls]? ')'
parseOptionalTypeIdInfo(FunctionSummary::TypeIdInfo & TypeIdInfo)9409 bool LLParser::parseOptionalTypeIdInfo(
9410 FunctionSummary::TypeIdInfo &TypeIdInfo) {
9411 assert(Lex.getKind() == lltok::kw_typeIdInfo);
9412 Lex.Lex();
9413
9414 if (parseToken(lltok::colon, "expected ':' here") ||
9415 parseToken(lltok::lparen, "expected '(' in typeIdInfo"))
9416 return true;
9417
9418 do {
9419 switch (Lex.getKind()) {
9420 case lltok::kw_typeTests:
9421 if (parseTypeTests(TypeIdInfo.TypeTests))
9422 return true;
9423 break;
9424 case lltok::kw_typeTestAssumeVCalls:
9425 if (parseVFuncIdList(lltok::kw_typeTestAssumeVCalls,
9426 TypeIdInfo.TypeTestAssumeVCalls))
9427 return true;
9428 break;
9429 case lltok::kw_typeCheckedLoadVCalls:
9430 if (parseVFuncIdList(lltok::kw_typeCheckedLoadVCalls,
9431 TypeIdInfo.TypeCheckedLoadVCalls))
9432 return true;
9433 break;
9434 case lltok::kw_typeTestAssumeConstVCalls:
9435 if (parseConstVCallList(lltok::kw_typeTestAssumeConstVCalls,
9436 TypeIdInfo.TypeTestAssumeConstVCalls))
9437 return true;
9438 break;
9439 case lltok::kw_typeCheckedLoadConstVCalls:
9440 if (parseConstVCallList(lltok::kw_typeCheckedLoadConstVCalls,
9441 TypeIdInfo.TypeCheckedLoadConstVCalls))
9442 return true;
9443 break;
9444 default:
9445 return error(Lex.getLoc(), "invalid typeIdInfo list type");
9446 }
9447 } while (EatIfPresent(lltok::comma));
9448
9449 if (parseToken(lltok::rparen, "expected ')' in typeIdInfo"))
9450 return true;
9451
9452 return false;
9453 }
9454
9455 /// TypeTests
9456 /// ::= 'typeTests' ':' '(' (SummaryID | UInt64)
9457 /// [',' (SummaryID | UInt64)]* ')'
parseTypeTests(std::vector<GlobalValue::GUID> & TypeTests)9458 bool LLParser::parseTypeTests(std::vector<GlobalValue::GUID> &TypeTests) {
9459 assert(Lex.getKind() == lltok::kw_typeTests);
9460 Lex.Lex();
9461
9462 if (parseToken(lltok::colon, "expected ':' here") ||
9463 parseToken(lltok::lparen, "expected '(' in typeIdInfo"))
9464 return true;
9465
9466 IdToIndexMapType IdToIndexMap;
9467 do {
9468 GlobalValue::GUID GUID = 0;
9469 if (Lex.getKind() == lltok::SummaryID) {
9470 unsigned ID = Lex.getUIntVal();
9471 LocTy Loc = Lex.getLoc();
9472 // Keep track of the TypeTests array index needing a forward reference.
9473 // We will save the location of the GUID needing an update, but
9474 // can only do so once the std::vector is finalized.
9475 IdToIndexMap[ID].push_back(std::make_pair(TypeTests.size(), Loc));
9476 Lex.Lex();
9477 } else if (parseUInt64(GUID))
9478 return true;
9479 TypeTests.push_back(GUID);
9480 } while (EatIfPresent(lltok::comma));
9481
9482 // Now that the TypeTests vector is finalized, it is safe to save the
9483 // locations of any forward GV references that need updating later.
9484 for (auto I : IdToIndexMap) {
9485 auto &Ids = ForwardRefTypeIds[I.first];
9486 for (auto P : I.second) {
9487 assert(TypeTests[P.first] == 0 &&
9488 "Forward referenced type id GUID expected to be 0");
9489 Ids.emplace_back(&TypeTests[P.first], P.second);
9490 }
9491 }
9492
9493 if (parseToken(lltok::rparen, "expected ')' in typeIdInfo"))
9494 return true;
9495
9496 return false;
9497 }
9498
9499 /// VFuncIdList
9500 /// ::= Kind ':' '(' VFuncId [',' VFuncId]* ')'
parseVFuncIdList(lltok::Kind Kind,std::vector<FunctionSummary::VFuncId> & VFuncIdList)9501 bool LLParser::parseVFuncIdList(
9502 lltok::Kind Kind, std::vector<FunctionSummary::VFuncId> &VFuncIdList) {
9503 assert(Lex.getKind() == Kind);
9504 Lex.Lex();
9505
9506 if (parseToken(lltok::colon, "expected ':' here") ||
9507 parseToken(lltok::lparen, "expected '(' here"))
9508 return true;
9509
9510 IdToIndexMapType IdToIndexMap;
9511 do {
9512 FunctionSummary::VFuncId VFuncId;
9513 if (parseVFuncId(VFuncId, IdToIndexMap, VFuncIdList.size()))
9514 return true;
9515 VFuncIdList.push_back(VFuncId);
9516 } while (EatIfPresent(lltok::comma));
9517
9518 if (parseToken(lltok::rparen, "expected ')' here"))
9519 return true;
9520
9521 // Now that the VFuncIdList vector is finalized, it is safe to save the
9522 // locations of any forward GV references that need updating later.
9523 for (auto I : IdToIndexMap) {
9524 auto &Ids = ForwardRefTypeIds[I.first];
9525 for (auto P : I.second) {
9526 assert(VFuncIdList[P.first].GUID == 0 &&
9527 "Forward referenced type id GUID expected to be 0");
9528 Ids.emplace_back(&VFuncIdList[P.first].GUID, P.second);
9529 }
9530 }
9531
9532 return false;
9533 }
9534
9535 /// ConstVCallList
9536 /// ::= Kind ':' '(' ConstVCall [',' ConstVCall]* ')'
parseConstVCallList(lltok::Kind Kind,std::vector<FunctionSummary::ConstVCall> & ConstVCallList)9537 bool LLParser::parseConstVCallList(
9538 lltok::Kind Kind,
9539 std::vector<FunctionSummary::ConstVCall> &ConstVCallList) {
9540 assert(Lex.getKind() == Kind);
9541 Lex.Lex();
9542
9543 if (parseToken(lltok::colon, "expected ':' here") ||
9544 parseToken(lltok::lparen, "expected '(' here"))
9545 return true;
9546
9547 IdToIndexMapType IdToIndexMap;
9548 do {
9549 FunctionSummary::ConstVCall ConstVCall;
9550 if (parseConstVCall(ConstVCall, IdToIndexMap, ConstVCallList.size()))
9551 return true;
9552 ConstVCallList.push_back(ConstVCall);
9553 } while (EatIfPresent(lltok::comma));
9554
9555 if (parseToken(lltok::rparen, "expected ')' here"))
9556 return true;
9557
9558 // Now that the ConstVCallList vector is finalized, it is safe to save the
9559 // locations of any forward GV references that need updating later.
9560 for (auto I : IdToIndexMap) {
9561 auto &Ids = ForwardRefTypeIds[I.first];
9562 for (auto P : I.second) {
9563 assert(ConstVCallList[P.first].VFunc.GUID == 0 &&
9564 "Forward referenced type id GUID expected to be 0");
9565 Ids.emplace_back(&ConstVCallList[P.first].VFunc.GUID, P.second);
9566 }
9567 }
9568
9569 return false;
9570 }
9571
9572 /// ConstVCall
9573 /// ::= '(' VFuncId ',' Args ')'
parseConstVCall(FunctionSummary::ConstVCall & ConstVCall,IdToIndexMapType & IdToIndexMap,unsigned Index)9574 bool LLParser::parseConstVCall(FunctionSummary::ConstVCall &ConstVCall,
9575 IdToIndexMapType &IdToIndexMap, unsigned Index) {
9576 if (parseToken(lltok::lparen, "expected '(' here") ||
9577 parseVFuncId(ConstVCall.VFunc, IdToIndexMap, Index))
9578 return true;
9579
9580 if (EatIfPresent(lltok::comma))
9581 if (parseArgs(ConstVCall.Args))
9582 return true;
9583
9584 if (parseToken(lltok::rparen, "expected ')' here"))
9585 return true;
9586
9587 return false;
9588 }
9589
9590 /// VFuncId
9591 /// ::= 'vFuncId' ':' '(' (SummaryID | 'guid' ':' UInt64) ','
9592 /// 'offset' ':' UInt64 ')'
parseVFuncId(FunctionSummary::VFuncId & VFuncId,IdToIndexMapType & IdToIndexMap,unsigned Index)9593 bool LLParser::parseVFuncId(FunctionSummary::VFuncId &VFuncId,
9594 IdToIndexMapType &IdToIndexMap, unsigned Index) {
9595 assert(Lex.getKind() == lltok::kw_vFuncId);
9596 Lex.Lex();
9597
9598 if (parseToken(lltok::colon, "expected ':' here") ||
9599 parseToken(lltok::lparen, "expected '(' here"))
9600 return true;
9601
9602 if (Lex.getKind() == lltok::SummaryID) {
9603 VFuncId.GUID = 0;
9604 unsigned ID = Lex.getUIntVal();
9605 LocTy Loc = Lex.getLoc();
9606 // Keep track of the array index needing a forward reference.
9607 // We will save the location of the GUID needing an update, but
9608 // can only do so once the caller's std::vector is finalized.
9609 IdToIndexMap[ID].push_back(std::make_pair(Index, Loc));
9610 Lex.Lex();
9611 } else if (parseToken(lltok::kw_guid, "expected 'guid' here") ||
9612 parseToken(lltok::colon, "expected ':' here") ||
9613 parseUInt64(VFuncId.GUID))
9614 return true;
9615
9616 if (parseToken(lltok::comma, "expected ',' here") ||
9617 parseToken(lltok::kw_offset, "expected 'offset' here") ||
9618 parseToken(lltok::colon, "expected ':' here") ||
9619 parseUInt64(VFuncId.Offset) ||
9620 parseToken(lltok::rparen, "expected ')' here"))
9621 return true;
9622
9623 return false;
9624 }
9625
9626 /// GVFlags
9627 /// ::= 'flags' ':' '(' 'linkage' ':' OptionalLinkageAux ','
9628 /// 'visibility' ':' Flag 'notEligibleToImport' ':' Flag ','
9629 /// 'live' ':' Flag ',' 'dsoLocal' ':' Flag ','
9630 /// 'canAutoHide' ':' Flag ',' ')'
parseGVFlags(GlobalValueSummary::GVFlags & GVFlags)9631 bool LLParser::parseGVFlags(GlobalValueSummary::GVFlags &GVFlags) {
9632 assert(Lex.getKind() == lltok::kw_flags);
9633 Lex.Lex();
9634
9635 if (parseToken(lltok::colon, "expected ':' here") ||
9636 parseToken(lltok::lparen, "expected '(' here"))
9637 return true;
9638
9639 do {
9640 unsigned Flag = 0;
9641 switch (Lex.getKind()) {
9642 case lltok::kw_linkage:
9643 Lex.Lex();
9644 if (parseToken(lltok::colon, "expected ':'"))
9645 return true;
9646 bool HasLinkage;
9647 GVFlags.Linkage = parseOptionalLinkageAux(Lex.getKind(), HasLinkage);
9648 assert(HasLinkage && "Linkage not optional in summary entry");
9649 Lex.Lex();
9650 break;
9651 case lltok::kw_visibility:
9652 Lex.Lex();
9653 if (parseToken(lltok::colon, "expected ':'"))
9654 return true;
9655 parseOptionalVisibility(Flag);
9656 GVFlags.Visibility = Flag;
9657 break;
9658 case lltok::kw_notEligibleToImport:
9659 Lex.Lex();
9660 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
9661 return true;
9662 GVFlags.NotEligibleToImport = Flag;
9663 break;
9664 case lltok::kw_live:
9665 Lex.Lex();
9666 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
9667 return true;
9668 GVFlags.Live = Flag;
9669 break;
9670 case lltok::kw_dsoLocal:
9671 Lex.Lex();
9672 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
9673 return true;
9674 GVFlags.DSOLocal = Flag;
9675 break;
9676 case lltok::kw_canAutoHide:
9677 Lex.Lex();
9678 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
9679 return true;
9680 GVFlags.CanAutoHide = Flag;
9681 break;
9682 default:
9683 return error(Lex.getLoc(), "expected gv flag type");
9684 }
9685 } while (EatIfPresent(lltok::comma));
9686
9687 if (parseToken(lltok::rparen, "expected ')' here"))
9688 return true;
9689
9690 return false;
9691 }
9692
9693 /// GVarFlags
9694 /// ::= 'varFlags' ':' '(' 'readonly' ':' Flag
9695 /// ',' 'writeonly' ':' Flag
9696 /// ',' 'constant' ':' Flag ')'
parseGVarFlags(GlobalVarSummary::GVarFlags & GVarFlags)9697 bool LLParser::parseGVarFlags(GlobalVarSummary::GVarFlags &GVarFlags) {
9698 assert(Lex.getKind() == lltok::kw_varFlags);
9699 Lex.Lex();
9700
9701 if (parseToken(lltok::colon, "expected ':' here") ||
9702 parseToken(lltok::lparen, "expected '(' here"))
9703 return true;
9704
9705 auto ParseRest = [this](unsigned int &Val) {
9706 Lex.Lex();
9707 if (parseToken(lltok::colon, "expected ':'"))
9708 return true;
9709 return parseFlag(Val);
9710 };
9711
9712 do {
9713 unsigned Flag = 0;
9714 switch (Lex.getKind()) {
9715 case lltok::kw_readonly:
9716 if (ParseRest(Flag))
9717 return true;
9718 GVarFlags.MaybeReadOnly = Flag;
9719 break;
9720 case lltok::kw_writeonly:
9721 if (ParseRest(Flag))
9722 return true;
9723 GVarFlags.MaybeWriteOnly = Flag;
9724 break;
9725 case lltok::kw_constant:
9726 if (ParseRest(Flag))
9727 return true;
9728 GVarFlags.Constant = Flag;
9729 break;
9730 case lltok::kw_vcall_visibility:
9731 if (ParseRest(Flag))
9732 return true;
9733 GVarFlags.VCallVisibility = Flag;
9734 break;
9735 default:
9736 return error(Lex.getLoc(), "expected gvar flag type");
9737 }
9738 } while (EatIfPresent(lltok::comma));
9739 return parseToken(lltok::rparen, "expected ')' here");
9740 }
9741
9742 /// ModuleReference
9743 /// ::= 'module' ':' UInt
parseModuleReference(StringRef & ModulePath)9744 bool LLParser::parseModuleReference(StringRef &ModulePath) {
9745 // parse module id.
9746 if (parseToken(lltok::kw_module, "expected 'module' here") ||
9747 parseToken(lltok::colon, "expected ':' here") ||
9748 parseToken(lltok::SummaryID, "expected module ID"))
9749 return true;
9750
9751 unsigned ModuleID = Lex.getUIntVal();
9752 auto I = ModuleIdMap.find(ModuleID);
9753 // We should have already parsed all module IDs
9754 assert(I != ModuleIdMap.end());
9755 ModulePath = I->second;
9756 return false;
9757 }
9758
9759 /// GVReference
9760 /// ::= SummaryID
parseGVReference(ValueInfo & VI,unsigned & GVId)9761 bool LLParser::parseGVReference(ValueInfo &VI, unsigned &GVId) {
9762 bool WriteOnly = false, ReadOnly = EatIfPresent(lltok::kw_readonly);
9763 if (!ReadOnly)
9764 WriteOnly = EatIfPresent(lltok::kw_writeonly);
9765 if (parseToken(lltok::SummaryID, "expected GV ID"))
9766 return true;
9767
9768 GVId = Lex.getUIntVal();
9769 // Check if we already have a VI for this GV
9770 if (GVId < NumberedValueInfos.size()) {
9771 assert(NumberedValueInfos[GVId].getRef() != FwdVIRef);
9772 VI = NumberedValueInfos[GVId];
9773 } else
9774 // We will create a forward reference to the stored location.
9775 VI = ValueInfo(false, FwdVIRef);
9776
9777 if (ReadOnly)
9778 VI.setReadOnly();
9779 if (WriteOnly)
9780 VI.setWriteOnly();
9781 return false;
9782 }
9783
9784 /// OptionalAllocs
9785 /// := 'allocs' ':' '(' Alloc [',' Alloc]* ')'
9786 /// Alloc ::= '(' 'versions' ':' '(' Version [',' Version]* ')'
9787 /// ',' MemProfs ')'
9788 /// Version ::= UInt32
parseOptionalAllocs(std::vector<AllocInfo> & Allocs)9789 bool LLParser::parseOptionalAllocs(std::vector<AllocInfo> &Allocs) {
9790 assert(Lex.getKind() == lltok::kw_allocs);
9791 Lex.Lex();
9792
9793 if (parseToken(lltok::colon, "expected ':' in allocs") ||
9794 parseToken(lltok::lparen, "expected '(' in allocs"))
9795 return true;
9796
9797 // parse each alloc
9798 do {
9799 if (parseToken(lltok::lparen, "expected '(' in alloc") ||
9800 parseToken(lltok::kw_versions, "expected 'versions' in alloc") ||
9801 parseToken(lltok::colon, "expected ':'") ||
9802 parseToken(lltok::lparen, "expected '(' in versions"))
9803 return true;
9804
9805 SmallVector<uint8_t> Versions;
9806 do {
9807 uint8_t V = 0;
9808 if (parseAllocType(V))
9809 return true;
9810 Versions.push_back(V);
9811 } while (EatIfPresent(lltok::comma));
9812
9813 if (parseToken(lltok::rparen, "expected ')' in versions") ||
9814 parseToken(lltok::comma, "expected ',' in alloc"))
9815 return true;
9816
9817 std::vector<MIBInfo> MIBs;
9818 if (parseMemProfs(MIBs))
9819 return true;
9820
9821 Allocs.push_back({Versions, MIBs});
9822
9823 if (parseToken(lltok::rparen, "expected ')' in alloc"))
9824 return true;
9825 } while (EatIfPresent(lltok::comma));
9826
9827 if (parseToken(lltok::rparen, "expected ')' in allocs"))
9828 return true;
9829
9830 return false;
9831 }
9832
9833 /// MemProfs
9834 /// := 'memProf' ':' '(' MemProf [',' MemProf]* ')'
9835 /// MemProf ::= '(' 'type' ':' AllocType
9836 /// ',' 'stackIds' ':' '(' StackId [',' StackId]* ')' ')'
9837 /// StackId ::= UInt64
parseMemProfs(std::vector<MIBInfo> & MIBs)9838 bool LLParser::parseMemProfs(std::vector<MIBInfo> &MIBs) {
9839 assert(Lex.getKind() == lltok::kw_memProf);
9840 Lex.Lex();
9841
9842 if (parseToken(lltok::colon, "expected ':' in memprof") ||
9843 parseToken(lltok::lparen, "expected '(' in memprof"))
9844 return true;
9845
9846 // parse each MIB
9847 do {
9848 if (parseToken(lltok::lparen, "expected '(' in memprof") ||
9849 parseToken(lltok::kw_type, "expected 'type' in memprof") ||
9850 parseToken(lltok::colon, "expected ':'"))
9851 return true;
9852
9853 uint8_t AllocType;
9854 if (parseAllocType(AllocType))
9855 return true;
9856
9857 if (parseToken(lltok::comma, "expected ',' in memprof") ||
9858 parseToken(lltok::kw_stackIds, "expected 'stackIds' in memprof") ||
9859 parseToken(lltok::colon, "expected ':'") ||
9860 parseToken(lltok::lparen, "expected '(' in stackIds"))
9861 return true;
9862
9863 SmallVector<unsigned> StackIdIndices;
9864 do {
9865 uint64_t StackId = 0;
9866 if (parseUInt64(StackId))
9867 return true;
9868 StackIdIndices.push_back(Index->addOrGetStackIdIndex(StackId));
9869 } while (EatIfPresent(lltok::comma));
9870
9871 if (parseToken(lltok::rparen, "expected ')' in stackIds"))
9872 return true;
9873
9874 MIBs.push_back({(AllocationType)AllocType, StackIdIndices});
9875
9876 if (parseToken(lltok::rparen, "expected ')' in memprof"))
9877 return true;
9878 } while (EatIfPresent(lltok::comma));
9879
9880 if (parseToken(lltok::rparen, "expected ')' in memprof"))
9881 return true;
9882
9883 return false;
9884 }
9885
9886 /// AllocType
9887 /// := ('none'|'notcold'|'cold'|'notcoldandcold')
parseAllocType(uint8_t & AllocType)9888 bool LLParser::parseAllocType(uint8_t &AllocType) {
9889 switch (Lex.getKind()) {
9890 case lltok::kw_none:
9891 AllocType = (uint8_t)AllocationType::None;
9892 break;
9893 case lltok::kw_notcold:
9894 AllocType = (uint8_t)AllocationType::NotCold;
9895 break;
9896 case lltok::kw_cold:
9897 AllocType = (uint8_t)AllocationType::Cold;
9898 break;
9899 case lltok::kw_notcoldandcold:
9900 AllocType =
9901 (uint8_t)AllocationType::NotCold | (uint8_t)AllocationType::Cold;
9902 break;
9903 default:
9904 return error(Lex.getLoc(), "invalid alloc type");
9905 }
9906 Lex.Lex();
9907 return false;
9908 }
9909
9910 /// OptionalCallsites
9911 /// := 'callsites' ':' '(' Callsite [',' Callsite]* ')'
9912 /// Callsite ::= '(' 'callee' ':' GVReference
9913 /// ',' 'clones' ':' '(' Version [',' Version]* ')'
9914 /// ',' 'stackIds' ':' '(' StackId [',' StackId]* ')' ')'
9915 /// Version ::= UInt32
9916 /// StackId ::= UInt64
parseOptionalCallsites(std::vector<CallsiteInfo> & Callsites)9917 bool LLParser::parseOptionalCallsites(std::vector<CallsiteInfo> &Callsites) {
9918 assert(Lex.getKind() == lltok::kw_callsites);
9919 Lex.Lex();
9920
9921 if (parseToken(lltok::colon, "expected ':' in callsites") ||
9922 parseToken(lltok::lparen, "expected '(' in callsites"))
9923 return true;
9924
9925 IdToIndexMapType IdToIndexMap;
9926 // parse each callsite
9927 do {
9928 if (parseToken(lltok::lparen, "expected '(' in callsite") ||
9929 parseToken(lltok::kw_callee, "expected 'callee' in callsite") ||
9930 parseToken(lltok::colon, "expected ':'"))
9931 return true;
9932
9933 ValueInfo VI;
9934 unsigned GVId = 0;
9935 LocTy Loc = Lex.getLoc();
9936 if (!EatIfPresent(lltok::kw_null)) {
9937 if (parseGVReference(VI, GVId))
9938 return true;
9939 }
9940
9941 if (parseToken(lltok::comma, "expected ',' in callsite") ||
9942 parseToken(lltok::kw_clones, "expected 'clones' in callsite") ||
9943 parseToken(lltok::colon, "expected ':'") ||
9944 parseToken(lltok::lparen, "expected '(' in clones"))
9945 return true;
9946
9947 SmallVector<unsigned> Clones;
9948 do {
9949 unsigned V = 0;
9950 if (parseUInt32(V))
9951 return true;
9952 Clones.push_back(V);
9953 } while (EatIfPresent(lltok::comma));
9954
9955 if (parseToken(lltok::rparen, "expected ')' in clones") ||
9956 parseToken(lltok::comma, "expected ',' in callsite") ||
9957 parseToken(lltok::kw_stackIds, "expected 'stackIds' in callsite") ||
9958 parseToken(lltok::colon, "expected ':'") ||
9959 parseToken(lltok::lparen, "expected '(' in stackIds"))
9960 return true;
9961
9962 SmallVector<unsigned> StackIdIndices;
9963 do {
9964 uint64_t StackId = 0;
9965 if (parseUInt64(StackId))
9966 return true;
9967 StackIdIndices.push_back(Index->addOrGetStackIdIndex(StackId));
9968 } while (EatIfPresent(lltok::comma));
9969
9970 if (parseToken(lltok::rparen, "expected ')' in stackIds"))
9971 return true;
9972
9973 // Keep track of the Callsites array index needing a forward reference.
9974 // We will save the location of the ValueInfo needing an update, but
9975 // can only do so once the SmallVector is finalized.
9976 if (VI.getRef() == FwdVIRef)
9977 IdToIndexMap[GVId].push_back(std::make_pair(Callsites.size(), Loc));
9978 Callsites.push_back({VI, Clones, StackIdIndices});
9979
9980 if (parseToken(lltok::rparen, "expected ')' in callsite"))
9981 return true;
9982 } while (EatIfPresent(lltok::comma));
9983
9984 // Now that the Callsites vector is finalized, it is safe to save the
9985 // locations of any forward GV references that need updating later.
9986 for (auto I : IdToIndexMap) {
9987 auto &Infos = ForwardRefValueInfos[I.first];
9988 for (auto P : I.second) {
9989 assert(Callsites[P.first].Callee.getRef() == FwdVIRef &&
9990 "Forward referenced ValueInfo expected to be empty");
9991 Infos.emplace_back(&Callsites[P.first].Callee, P.second);
9992 }
9993 }
9994
9995 if (parseToken(lltok::rparen, "expected ')' in callsites"))
9996 return true;
9997
9998 return false;
9999 }
10000