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/STLExtras.h" |
17 | #include "llvm/ADT/ScopeExit.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/InstIterator.h" |
36 | #include "llvm/IR/Instructions.h" |
37 | #include "llvm/IR/IntrinsicInst.h" |
38 | #include "llvm/IR/Intrinsics.h" |
39 | #include "llvm/IR/LLVMContext.h" |
40 | #include "llvm/IR/Metadata.h" |
41 | #include "llvm/IR/Module.h" |
42 | #include "llvm/IR/Operator.h" |
43 | #include "llvm/IR/Value.h" |
44 | #include "llvm/IR/ValueSymbolTable.h" |
45 | #include "llvm/Support/Casting.h" |
46 | #include "llvm/Support/ErrorHandling.h" |
47 | #include "llvm/Support/MathExtras.h" |
48 | #include "llvm/Support/ModRef.h" |
49 | #include "llvm/Support/SaveAndRestore.h" |
50 | #include "llvm/Support/raw_ostream.h" |
51 | #include <algorithm> |
52 | #include <cassert> |
53 | #include <cstring> |
54 | #include <optional> |
55 | #include <vector> |
56 | |
57 | using namespace llvm; |
58 | |
59 | static cl::opt<bool> AllowIncompleteIR( |
60 | "allow-incomplete-ir" , cl::init(Val: false), cl::Hidden, |
61 | cl::desc( |
62 | "Allow incomplete IR on a best effort basis (references to unknown " |
63 | "metadata will be dropped)" )); |
64 | |
65 | extern llvm::cl::opt<bool> UseNewDbgInfoFormat; |
66 | extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat; |
67 | extern bool WriteNewDbgInfoFormatToBitcode; |
68 | extern cl::opt<bool> WriteNewDbgInfoFormat; |
69 | |
70 | static std::string getTypeString(Type *T) { |
71 | std::string Result; |
72 | raw_string_ostream Tmp(Result); |
73 | Tmp << *T; |
74 | return Tmp.str(); |
75 | } |
76 | |
77 | // Whatever debug info format we parsed, we should convert to the expected debug |
78 | // info format immediately afterwards. |
79 | bool LLParser::finalizeDebugInfoFormat(Module *M) { |
80 | // We should have already returned an error if we observed both intrinsics and |
81 | // records in this IR. |
82 | assert(!(SeenNewDbgInfoFormat && SeenOldDbgInfoFormat) && |
83 | "Mixed debug intrinsics/records seen without a parsing error?" ); |
84 | if (PreserveInputDbgFormat == cl::boolOrDefault::BOU_TRUE) { |
85 | UseNewDbgInfoFormat = SeenNewDbgInfoFormat; |
86 | WriteNewDbgInfoFormatToBitcode = SeenNewDbgInfoFormat; |
87 | WriteNewDbgInfoFormat = SeenNewDbgInfoFormat; |
88 | } else if (M) { |
89 | M->setIsNewDbgInfoFormat(false); |
90 | } |
91 | return false; |
92 | } |
93 | |
94 | /// Run: module ::= toplevelentity* |
95 | bool LLParser::Run(bool UpgradeDebugInfo, |
96 | DataLayoutCallbackTy DataLayoutCallback) { |
97 | // Prime the lexer. |
98 | Lex.Lex(); |
99 | |
100 | if (Context.shouldDiscardValueNames()) |
101 | return error( |
102 | L: Lex.getLoc(), |
103 | Msg: "Can't read textual IR with a Context that discards named Values" ); |
104 | |
105 | if (M) { |
106 | if (parseTargetDefinitions(DataLayoutCallback)) |
107 | return true; |
108 | } |
109 | |
110 | return parseTopLevelEntities() || validateEndOfModule(UpgradeDebugInfo) || |
111 | validateEndOfIndex() || finalizeDebugInfoFormat(M); |
112 | } |
113 | |
114 | bool LLParser::parseStandaloneConstantValue(Constant *&C, |
115 | const SlotMapping *Slots) { |
116 | restoreParsingState(Slots); |
117 | Lex.Lex(); |
118 | |
119 | Type *Ty = nullptr; |
120 | if (parseType(Result&: Ty) || parseConstantValue(Ty, C)) |
121 | return true; |
122 | if (Lex.getKind() != lltok::Eof) |
123 | return error(L: Lex.getLoc(), Msg: "expected end of string" ); |
124 | return false; |
125 | } |
126 | |
127 | bool LLParser::parseTypeAtBeginning(Type *&Ty, unsigned &Read, |
128 | const SlotMapping *Slots) { |
129 | restoreParsingState(Slots); |
130 | Lex.Lex(); |
131 | |
132 | Read = 0; |
133 | SMLoc Start = Lex.getLoc(); |
134 | Ty = nullptr; |
135 | if (parseType(Result&: Ty)) |
136 | return true; |
137 | SMLoc End = Lex.getLoc(); |
138 | Read = End.getPointer() - Start.getPointer(); |
139 | |
140 | return false; |
141 | } |
142 | |
143 | void LLParser::restoreParsingState(const SlotMapping *Slots) { |
144 | if (!Slots) |
145 | return; |
146 | NumberedVals = Slots->GlobalValues; |
147 | NumberedMetadata = Slots->MetadataNodes; |
148 | for (const auto &I : Slots->NamedTypes) |
149 | NamedTypes.insert( |
150 | KV: std::make_pair(x: I.getKey(), y: std::make_pair(x: I.second, y: LocTy()))); |
151 | for (const auto &I : Slots->Types) |
152 | NumberedTypes.insert( |
153 | x: std::make_pair(x: I.first, y: std::make_pair(x: I.second, y: LocTy()))); |
154 | } |
155 | |
156 | static void dropIntrinsicWithUnknownMetadataArgument(IntrinsicInst *II) { |
157 | // White-list intrinsics that are safe to drop. |
158 | if (!isa<DbgInfoIntrinsic>(Val: II) && |
159 | II->getIntrinsicID() != Intrinsic::experimental_noalias_scope_decl) |
160 | return; |
161 | |
162 | SmallVector<MetadataAsValue *> MVs; |
163 | for (Value *V : II->args()) |
164 | if (auto *MV = dyn_cast<MetadataAsValue>(Val: V)) |
165 | if (auto *MD = dyn_cast<MDNode>(Val: MV->getMetadata())) |
166 | if (MD->isTemporary()) |
167 | MVs.push_back(Elt: MV); |
168 | |
169 | if (!MVs.empty()) { |
170 | assert(II->use_empty() && "Cannot have uses" ); |
171 | II->eraseFromParent(); |
172 | |
173 | // Also remove no longer used MetadataAsValue wrappers. |
174 | for (MetadataAsValue *MV : MVs) |
175 | if (MV->use_empty()) |
176 | delete MV; |
177 | } |
178 | } |
179 | |
180 | void LLParser::dropUnknownMetadataReferences() { |
181 | auto Pred = [](unsigned MDKind, MDNode *Node) { return Node->isTemporary(); }; |
182 | for (Function &F : *M) { |
183 | F.eraseMetadataIf(Pred); |
184 | for (Instruction &I : make_early_inc_range(Range: instructions(F))) { |
185 | I.eraseMetadataIf(Pred); |
186 | |
187 | if (auto *II = dyn_cast<IntrinsicInst>(Val: &I)) |
188 | dropIntrinsicWithUnknownMetadataArgument(II); |
189 | } |
190 | } |
191 | |
192 | for (GlobalVariable &GV : M->globals()) |
193 | GV.eraseMetadataIf(Pred); |
194 | |
195 | for (const auto &[ID, Info] : make_early_inc_range(Range&: ForwardRefMDNodes)) { |
196 | // Check whether there is only a single use left, which would be in our |
197 | // own NumberedMetadata. |
198 | if (Info.first->getNumTemporaryUses() == 1) { |
199 | NumberedMetadata.erase(x: ID); |
200 | ForwardRefMDNodes.erase(x: ID); |
201 | } |
202 | } |
203 | } |
204 | |
205 | /// validateEndOfModule - Do final validity and basic correctness checks at the |
206 | /// end of the module. |
207 | bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) { |
208 | if (!M) |
209 | return false; |
210 | // Handle any function attribute group forward references. |
211 | for (const auto &RAG : ForwardRefAttrGroups) { |
212 | Value *V = RAG.first; |
213 | const std::vector<unsigned> &Attrs = RAG.second; |
214 | AttrBuilder B(Context); |
215 | |
216 | for (const auto &Attr : Attrs) { |
217 | auto R = NumberedAttrBuilders.find(x: Attr); |
218 | if (R != NumberedAttrBuilders.end()) |
219 | B.merge(B: R->second); |
220 | } |
221 | |
222 | if (Function *Fn = dyn_cast<Function>(Val: V)) { |
223 | AttributeList AS = Fn->getAttributes(); |
224 | AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs()); |
225 | AS = AS.removeFnAttributes(C&: Context); |
226 | |
227 | FnAttrs.merge(B); |
228 | |
229 | // If the alignment was parsed as an attribute, move to the alignment |
230 | // field. |
231 | if (MaybeAlign A = FnAttrs.getAlignment()) { |
232 | Fn->setAlignment(*A); |
233 | FnAttrs.removeAttribute(Attribute::Alignment); |
234 | } |
235 | |
236 | AS = AS.addFnAttributes(C&: Context, B: FnAttrs); |
237 | Fn->setAttributes(AS); |
238 | } else if (CallInst *CI = dyn_cast<CallInst>(Val: V)) { |
239 | AttributeList AS = CI->getAttributes(); |
240 | AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs()); |
241 | AS = AS.removeFnAttributes(C&: Context); |
242 | FnAttrs.merge(B); |
243 | AS = AS.addFnAttributes(C&: Context, B: FnAttrs); |
244 | CI->setAttributes(AS); |
245 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(Val: V)) { |
246 | AttributeList AS = II->getAttributes(); |
247 | AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs()); |
248 | AS = AS.removeFnAttributes(C&: Context); |
249 | FnAttrs.merge(B); |
250 | AS = AS.addFnAttributes(C&: Context, B: FnAttrs); |
251 | II->setAttributes(AS); |
252 | } else if (CallBrInst *CBI = dyn_cast<CallBrInst>(Val: V)) { |
253 | AttributeList AS = CBI->getAttributes(); |
254 | AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs()); |
255 | AS = AS.removeFnAttributes(C&: Context); |
256 | FnAttrs.merge(B); |
257 | AS = AS.addFnAttributes(C&: Context, B: FnAttrs); |
258 | CBI->setAttributes(AS); |
259 | } else if (auto *GV = dyn_cast<GlobalVariable>(Val: V)) { |
260 | AttrBuilder Attrs(M->getContext(), GV->getAttributes()); |
261 | Attrs.merge(B); |
262 | GV->setAttributes(AttributeSet::get(C&: Context,B: Attrs)); |
263 | } else { |
264 | llvm_unreachable("invalid object with forward attribute group reference" ); |
265 | } |
266 | } |
267 | |
268 | // If there are entries in ForwardRefBlockAddresses at this point, the |
269 | // function was never defined. |
270 | if (!ForwardRefBlockAddresses.empty()) |
271 | return error(L: ForwardRefBlockAddresses.begin()->first.Loc, |
272 | Msg: "expected function name in blockaddress" ); |
273 | |
274 | auto ResolveForwardRefDSOLocalEquivalents = [&](const ValID &GVRef, |
275 | GlobalValue *FwdRef) { |
276 | GlobalValue *GV = nullptr; |
277 | if (GVRef.Kind == ValID::t_GlobalName) { |
278 | GV = M->getNamedValue(Name: GVRef.StrVal); |
279 | } else { |
280 | GV = NumberedVals.get(ID: GVRef.UIntVal); |
281 | } |
282 | |
283 | if (!GV) |
284 | return error(L: GVRef.Loc, Msg: "unknown function '" + GVRef.StrVal + |
285 | "' referenced by dso_local_equivalent" ); |
286 | |
287 | if (!GV->getValueType()->isFunctionTy()) |
288 | return error(L: GVRef.Loc, |
289 | Msg: "expected a function, alias to function, or ifunc " |
290 | "in dso_local_equivalent" ); |
291 | |
292 | auto *Equiv = DSOLocalEquivalent::get(GV); |
293 | FwdRef->replaceAllUsesWith(V: Equiv); |
294 | FwdRef->eraseFromParent(); |
295 | return false; |
296 | }; |
297 | |
298 | // If there are entries in ForwardRefDSOLocalEquivalentIDs/Names at this |
299 | // point, they are references after the function was defined. Resolve those |
300 | // now. |
301 | for (auto &Iter : ForwardRefDSOLocalEquivalentIDs) { |
302 | if (ResolveForwardRefDSOLocalEquivalents(Iter.first, Iter.second)) |
303 | return true; |
304 | } |
305 | for (auto &Iter : ForwardRefDSOLocalEquivalentNames) { |
306 | if (ResolveForwardRefDSOLocalEquivalents(Iter.first, Iter.second)) |
307 | return true; |
308 | } |
309 | ForwardRefDSOLocalEquivalentIDs.clear(); |
310 | ForwardRefDSOLocalEquivalentNames.clear(); |
311 | |
312 | for (const auto &NT : NumberedTypes) |
313 | if (NT.second.second.isValid()) |
314 | return error(L: NT.second.second, |
315 | Msg: "use of undefined type '%" + Twine(NT.first) + "'" ); |
316 | |
317 | for (StringMap<std::pair<Type*, LocTy> >::iterator I = |
318 | NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) |
319 | if (I->second.second.isValid()) |
320 | return error(L: I->second.second, |
321 | Msg: "use of undefined type named '" + I->getKey() + "'" ); |
322 | |
323 | if (!ForwardRefComdats.empty()) |
324 | return error(L: ForwardRefComdats.begin()->second, |
325 | Msg: "use of undefined comdat '$" + |
326 | ForwardRefComdats.begin()->first + "'" ); |
327 | |
328 | for (const auto &[Name, Info] : make_early_inc_range(Range&: ForwardRefVals)) { |
329 | if (StringRef(Name).starts_with(Prefix: "llvm." )) { |
330 | Intrinsic::ID IID = Function::lookupIntrinsicID(Name); |
331 | if (IID == Intrinsic::not_intrinsic) |
332 | // Don't do anything for unknown intrinsics. |
333 | continue; |
334 | |
335 | // Automatically create declarations for intrinsics. Intrinsics can only |
336 | // be called directly, so the call function type directly determines the |
337 | // declaration function type. |
338 | // |
339 | // Additionally, automatically add the required mangling suffix to the |
340 | // intrinsic name. This means that we may replace a single forward |
341 | // declaration with multiple functions here. |
342 | for (Use &U : make_early_inc_range(Range: Info.first->uses())) { |
343 | auto *CB = dyn_cast<CallBase>(Val: U.getUser()); |
344 | if (!CB || !CB->isCallee(U: &U)) |
345 | return error(L: Info.second, Msg: "intrinsic can only be used as callee" ); |
346 | |
347 | SmallVector<Type *> OverloadTys; |
348 | if (!Intrinsic::getIntrinsicSignature(IID, FT: CB->getFunctionType(), |
349 | ArgTys&: OverloadTys)) |
350 | return error(L: Info.second, Msg: "invalid intrinsic signature" ); |
351 | |
352 | U.set(Intrinsic::getDeclaration(M, id: IID, Tys: OverloadTys)); |
353 | } |
354 | |
355 | Info.first->eraseFromParent(); |
356 | ForwardRefVals.erase(x: Name); |
357 | continue; |
358 | } |
359 | |
360 | // If incomplete IR is allowed, also add declarations for |
361 | // non-intrinsics. |
362 | if (!AllowIncompleteIR) |
363 | continue; |
364 | |
365 | auto GetCommonFunctionType = [](Value *V) -> FunctionType * { |
366 | FunctionType *FTy = nullptr; |
367 | for (Use &U : V->uses()) { |
368 | auto *CB = dyn_cast<CallBase>(Val: U.getUser()); |
369 | if (!CB || !CB->isCallee(U: &U) || (FTy && FTy != CB->getFunctionType())) |
370 | return nullptr; |
371 | FTy = CB->getFunctionType(); |
372 | } |
373 | return FTy; |
374 | }; |
375 | |
376 | // First check whether this global is only used in calls with the same |
377 | // type, in which case we'll insert a function. Otherwise, fall back to |
378 | // using a dummy i8 type. |
379 | Type *Ty = GetCommonFunctionType(Info.first); |
380 | if (!Ty) |
381 | Ty = Type::getInt8Ty(C&: Context); |
382 | |
383 | GlobalValue *GV; |
384 | if (auto *FTy = dyn_cast<FunctionType>(Val: Ty)) |
385 | GV = Function::Create(Ty: FTy, Linkage: GlobalValue::ExternalLinkage, N: Name, M); |
386 | else |
387 | GV = new GlobalVariable(*M, Ty, /*isConstant*/ false, |
388 | GlobalValue::ExternalLinkage, |
389 | /*Initializer*/ nullptr, Name); |
390 | Info.first->replaceAllUsesWith(V: GV); |
391 | Info.first->eraseFromParent(); |
392 | ForwardRefVals.erase(x: Name); |
393 | } |
394 | |
395 | if (!ForwardRefVals.empty()) |
396 | return error(L: ForwardRefVals.begin()->second.second, |
397 | Msg: "use of undefined value '@" + ForwardRefVals.begin()->first + |
398 | "'" ); |
399 | |
400 | if (!ForwardRefValIDs.empty()) |
401 | return error(L: ForwardRefValIDs.begin()->second.second, |
402 | Msg: "use of undefined value '@" + |
403 | Twine(ForwardRefValIDs.begin()->first) + "'" ); |
404 | |
405 | if (AllowIncompleteIR && !ForwardRefMDNodes.empty()) |
406 | dropUnknownMetadataReferences(); |
407 | |
408 | if (!ForwardRefMDNodes.empty()) |
409 | return error(L: ForwardRefMDNodes.begin()->second.second, |
410 | Msg: "use of undefined metadata '!" + |
411 | Twine(ForwardRefMDNodes.begin()->first) + "'" ); |
412 | |
413 | // Resolve metadata cycles. |
414 | for (auto &N : NumberedMetadata) { |
415 | if (N.second && !N.second->isResolved()) |
416 | N.second->resolveCycles(); |
417 | } |
418 | |
419 | for (auto *Inst : InstsWithTBAATag) { |
420 | MDNode *MD = Inst->getMetadata(KindID: LLVMContext::MD_tbaa); |
421 | // With incomplete IR, the tbaa metadata may have been dropped. |
422 | if (!AllowIncompleteIR) |
423 | assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag" ); |
424 | if (MD) { |
425 | auto *UpgradedMD = UpgradeTBAANode(TBAANode&: *MD); |
426 | if (MD != UpgradedMD) |
427 | Inst->setMetadata(KindID: LLVMContext::MD_tbaa, Node: UpgradedMD); |
428 | } |
429 | } |
430 | |
431 | // Look for intrinsic functions and CallInst that need to be upgraded. We use |
432 | // make_early_inc_range here because we may remove some functions. |
433 | for (Function &F : llvm::make_early_inc_range(Range&: *M)) |
434 | UpgradeCallsToIntrinsic(F: &F); |
435 | |
436 | if (UpgradeDebugInfo) |
437 | llvm::UpgradeDebugInfo(M&: *M); |
438 | |
439 | UpgradeModuleFlags(M&: *M); |
440 | UpgradeSectionAttributes(M&: *M); |
441 | |
442 | if (!Slots) |
443 | return false; |
444 | // Initialize the slot mapping. |
445 | // Because by this point we've parsed and validated everything, we can "steal" |
446 | // the mapping from LLParser as it doesn't need it anymore. |
447 | Slots->GlobalValues = std::move(NumberedVals); |
448 | Slots->MetadataNodes = std::move(NumberedMetadata); |
449 | for (const auto &I : NamedTypes) |
450 | Slots->NamedTypes.insert(KV: std::make_pair(x: I.getKey(), y: I.second.first)); |
451 | for (const auto &I : NumberedTypes) |
452 | Slots->Types.insert(x: std::make_pair(x: I.first, y: I.second.first)); |
453 | |
454 | return false; |
455 | } |
456 | |
457 | /// Do final validity and basic correctness checks at the end of the index. |
458 | bool LLParser::validateEndOfIndex() { |
459 | if (!Index) |
460 | return false; |
461 | |
462 | if (!ForwardRefValueInfos.empty()) |
463 | return error(L: ForwardRefValueInfos.begin()->second.front().second, |
464 | Msg: "use of undefined summary '^" + |
465 | Twine(ForwardRefValueInfos.begin()->first) + "'" ); |
466 | |
467 | if (!ForwardRefAliasees.empty()) |
468 | return error(L: ForwardRefAliasees.begin()->second.front().second, |
469 | Msg: "use of undefined summary '^" + |
470 | Twine(ForwardRefAliasees.begin()->first) + "'" ); |
471 | |
472 | if (!ForwardRefTypeIds.empty()) |
473 | return error(L: ForwardRefTypeIds.begin()->second.front().second, |
474 | Msg: "use of undefined type id summary '^" + |
475 | Twine(ForwardRefTypeIds.begin()->first) + "'" ); |
476 | |
477 | return false; |
478 | } |
479 | |
480 | //===----------------------------------------------------------------------===// |
481 | // Top-Level Entities |
482 | //===----------------------------------------------------------------------===// |
483 | |
484 | bool LLParser::parseTargetDefinitions(DataLayoutCallbackTy DataLayoutCallback) { |
485 | // Delay parsing of the data layout string until the target triple is known. |
486 | // Then, pass both the the target triple and the tentative data layout string |
487 | // to DataLayoutCallback, allowing to override the DL string. |
488 | // This enables importing modules with invalid DL strings. |
489 | std::string TentativeDLStr = M->getDataLayoutStr(); |
490 | LocTy DLStrLoc; |
491 | |
492 | bool Done = false; |
493 | while (!Done) { |
494 | switch (Lex.getKind()) { |
495 | case lltok::kw_target: |
496 | if (parseTargetDefinition(TentativeDLStr, DLStrLoc)) |
497 | return true; |
498 | break; |
499 | case lltok::kw_source_filename: |
500 | if (parseSourceFileName()) |
501 | return true; |
502 | break; |
503 | default: |
504 | Done = true; |
505 | } |
506 | } |
507 | // Run the override callback to potentially change the data layout string, and |
508 | // parse the data layout string. |
509 | if (auto LayoutOverride = |
510 | DataLayoutCallback(M->getTargetTriple(), TentativeDLStr)) { |
511 | TentativeDLStr = *LayoutOverride; |
512 | DLStrLoc = {}; |
513 | } |
514 | Expected<DataLayout> MaybeDL = DataLayout::parse(LayoutDescription: TentativeDLStr); |
515 | if (!MaybeDL) |
516 | return error(L: DLStrLoc, Msg: toString(E: MaybeDL.takeError())); |
517 | M->setDataLayout(MaybeDL.get()); |
518 | return false; |
519 | } |
520 | |
521 | bool LLParser::parseTopLevelEntities() { |
522 | // If there is no Module, then parse just the summary index entries. |
523 | if (!M) { |
524 | while (true) { |
525 | switch (Lex.getKind()) { |
526 | case lltok::Eof: |
527 | return false; |
528 | case lltok::SummaryID: |
529 | if (parseSummaryEntry()) |
530 | return true; |
531 | break; |
532 | case lltok::kw_source_filename: |
533 | if (parseSourceFileName()) |
534 | return true; |
535 | break; |
536 | default: |
537 | // Skip everything else |
538 | Lex.Lex(); |
539 | } |
540 | } |
541 | } |
542 | while (true) { |
543 | switch (Lex.getKind()) { |
544 | default: |
545 | return tokError(Msg: "expected top-level entity" ); |
546 | case lltok::Eof: return false; |
547 | case lltok::kw_declare: |
548 | if (parseDeclare()) |
549 | return true; |
550 | break; |
551 | case lltok::kw_define: |
552 | if (parseDefine()) |
553 | return true; |
554 | break; |
555 | case lltok::kw_module: |
556 | if (parseModuleAsm()) |
557 | return true; |
558 | break; |
559 | case lltok::LocalVarID: |
560 | if (parseUnnamedType()) |
561 | return true; |
562 | break; |
563 | case lltok::LocalVar: |
564 | if (parseNamedType()) |
565 | return true; |
566 | break; |
567 | case lltok::GlobalID: |
568 | if (parseUnnamedGlobal()) |
569 | return true; |
570 | break; |
571 | case lltok::GlobalVar: |
572 | if (parseNamedGlobal()) |
573 | return true; |
574 | break; |
575 | case lltok::ComdatVar: if (parseComdat()) return true; break; |
576 | case lltok::exclaim: |
577 | if (parseStandaloneMetadata()) |
578 | return true; |
579 | break; |
580 | case lltok::SummaryID: |
581 | if (parseSummaryEntry()) |
582 | return true; |
583 | break; |
584 | case lltok::MetadataVar: |
585 | if (parseNamedMetadata()) |
586 | return true; |
587 | break; |
588 | case lltok::kw_attributes: |
589 | if (parseUnnamedAttrGrp()) |
590 | return true; |
591 | break; |
592 | case lltok::kw_uselistorder: |
593 | if (parseUseListOrder()) |
594 | return true; |
595 | break; |
596 | case lltok::kw_uselistorder_bb: |
597 | if (parseUseListOrderBB()) |
598 | return true; |
599 | break; |
600 | } |
601 | } |
602 | } |
603 | |
604 | /// toplevelentity |
605 | /// ::= 'module' 'asm' STRINGCONSTANT |
606 | bool LLParser::parseModuleAsm() { |
607 | assert(Lex.getKind() == lltok::kw_module); |
608 | Lex.Lex(); |
609 | |
610 | std::string AsmStr; |
611 | if (parseToken(T: lltok::kw_asm, ErrMsg: "expected 'module asm'" ) || |
612 | parseStringConstant(Result&: AsmStr)) |
613 | return true; |
614 | |
615 | M->appendModuleInlineAsm(Asm: AsmStr); |
616 | return false; |
617 | } |
618 | |
619 | /// toplevelentity |
620 | /// ::= 'target' 'triple' '=' STRINGCONSTANT |
621 | /// ::= 'target' 'datalayout' '=' STRINGCONSTANT |
622 | bool LLParser::parseTargetDefinition(std::string &TentativeDLStr, |
623 | LocTy &DLStrLoc) { |
624 | assert(Lex.getKind() == lltok::kw_target); |
625 | std::string Str; |
626 | switch (Lex.Lex()) { |
627 | default: |
628 | return tokError(Msg: "unknown target property" ); |
629 | case lltok::kw_triple: |
630 | Lex.Lex(); |
631 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after target triple" ) || |
632 | parseStringConstant(Result&: Str)) |
633 | return true; |
634 | M->setTargetTriple(Str); |
635 | return false; |
636 | case lltok::kw_datalayout: |
637 | Lex.Lex(); |
638 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after target datalayout" )) |
639 | return true; |
640 | DLStrLoc = Lex.getLoc(); |
641 | if (parseStringConstant(Result&: TentativeDLStr)) |
642 | return true; |
643 | return false; |
644 | } |
645 | } |
646 | |
647 | /// toplevelentity |
648 | /// ::= 'source_filename' '=' STRINGCONSTANT |
649 | bool LLParser::parseSourceFileName() { |
650 | assert(Lex.getKind() == lltok::kw_source_filename); |
651 | Lex.Lex(); |
652 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after source_filename" ) || |
653 | parseStringConstant(Result&: SourceFileName)) |
654 | return true; |
655 | if (M) |
656 | M->setSourceFileName(SourceFileName); |
657 | return false; |
658 | } |
659 | |
660 | /// parseUnnamedType: |
661 | /// ::= LocalVarID '=' 'type' type |
662 | bool LLParser::parseUnnamedType() { |
663 | LocTy TypeLoc = Lex.getLoc(); |
664 | unsigned TypeID = Lex.getUIntVal(); |
665 | Lex.Lex(); // eat LocalVarID; |
666 | |
667 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after name" ) || |
668 | parseToken(T: lltok::kw_type, ErrMsg: "expected 'type' after '='" )) |
669 | return true; |
670 | |
671 | Type *Result = nullptr; |
672 | if (parseStructDefinition(TypeLoc, Name: "" , Entry&: NumberedTypes[TypeID], ResultTy&: Result)) |
673 | return true; |
674 | |
675 | if (!isa<StructType>(Val: Result)) { |
676 | std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID]; |
677 | if (Entry.first) |
678 | return error(L: TypeLoc, Msg: "non-struct types may not be recursive" ); |
679 | Entry.first = Result; |
680 | Entry.second = SMLoc(); |
681 | } |
682 | |
683 | return false; |
684 | } |
685 | |
686 | /// toplevelentity |
687 | /// ::= LocalVar '=' 'type' type |
688 | bool LLParser::parseNamedType() { |
689 | std::string Name = Lex.getStrVal(); |
690 | LocTy NameLoc = Lex.getLoc(); |
691 | Lex.Lex(); // eat LocalVar. |
692 | |
693 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after name" ) || |
694 | parseToken(T: lltok::kw_type, ErrMsg: "expected 'type' after name" )) |
695 | return true; |
696 | |
697 | Type *Result = nullptr; |
698 | if (parseStructDefinition(TypeLoc: NameLoc, Name, Entry&: NamedTypes[Name], ResultTy&: Result)) |
699 | return true; |
700 | |
701 | if (!isa<StructType>(Val: Result)) { |
702 | std::pair<Type*, LocTy> &Entry = NamedTypes[Name]; |
703 | if (Entry.first) |
704 | return error(L: NameLoc, Msg: "non-struct types may not be recursive" ); |
705 | Entry.first = Result; |
706 | Entry.second = SMLoc(); |
707 | } |
708 | |
709 | return false; |
710 | } |
711 | |
712 | /// toplevelentity |
713 | /// ::= 'declare' FunctionHeader |
714 | bool LLParser::parseDeclare() { |
715 | assert(Lex.getKind() == lltok::kw_declare); |
716 | Lex.Lex(); |
717 | |
718 | std::vector<std::pair<unsigned, MDNode *>> MDs; |
719 | while (Lex.getKind() == lltok::MetadataVar) { |
720 | unsigned MDK; |
721 | MDNode *N; |
722 | if (parseMetadataAttachment(Kind&: MDK, MD&: N)) |
723 | return true; |
724 | MDs.push_back(x: {MDK, N}); |
725 | } |
726 | |
727 | Function *F; |
728 | unsigned FunctionNumber = -1; |
729 | SmallVector<unsigned> UnnamedArgNums; |
730 | if (parseFunctionHeader(Fn&: F, IsDefine: false, FunctionNumber, UnnamedArgNums)) |
731 | return true; |
732 | for (auto &MD : MDs) |
733 | F->addMetadata(KindID: MD.first, MD&: *MD.second); |
734 | return false; |
735 | } |
736 | |
737 | /// toplevelentity |
738 | /// ::= 'define' FunctionHeader (!dbg !56)* '{' ... |
739 | bool LLParser::parseDefine() { |
740 | assert(Lex.getKind() == lltok::kw_define); |
741 | Lex.Lex(); |
742 | |
743 | Function *F; |
744 | unsigned FunctionNumber = -1; |
745 | SmallVector<unsigned> UnnamedArgNums; |
746 | return parseFunctionHeader(Fn&: F, IsDefine: true, FunctionNumber, UnnamedArgNums) || |
747 | parseOptionalFunctionMetadata(F&: *F) || |
748 | parseFunctionBody(Fn&: *F, FunctionNumber, UnnamedArgNums); |
749 | } |
750 | |
751 | /// parseGlobalType |
752 | /// ::= 'constant' |
753 | /// ::= 'global' |
754 | bool LLParser::parseGlobalType(bool &IsConstant) { |
755 | if (Lex.getKind() == lltok::kw_constant) |
756 | IsConstant = true; |
757 | else if (Lex.getKind() == lltok::kw_global) |
758 | IsConstant = false; |
759 | else { |
760 | IsConstant = false; |
761 | return tokError(Msg: "expected 'global' or 'constant'" ); |
762 | } |
763 | Lex.Lex(); |
764 | return false; |
765 | } |
766 | |
767 | bool LLParser::parseOptionalUnnamedAddr( |
768 | GlobalVariable::UnnamedAddr &UnnamedAddr) { |
769 | if (EatIfPresent(T: lltok::kw_unnamed_addr)) |
770 | UnnamedAddr = GlobalValue::UnnamedAddr::Global; |
771 | else if (EatIfPresent(T: lltok::kw_local_unnamed_addr)) |
772 | UnnamedAddr = GlobalValue::UnnamedAddr::Local; |
773 | else |
774 | UnnamedAddr = GlobalValue::UnnamedAddr::None; |
775 | return false; |
776 | } |
777 | |
778 | /// parseUnnamedGlobal: |
779 | /// OptionalVisibility (ALIAS | IFUNC) ... |
780 | /// OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility |
781 | /// OptionalDLLStorageClass |
782 | /// ... -> global variable |
783 | /// GlobalID '=' OptionalVisibility (ALIAS | IFUNC) ... |
784 | /// GlobalID '=' OptionalLinkage OptionalPreemptionSpecifier |
785 | /// OptionalVisibility |
786 | /// OptionalDLLStorageClass |
787 | /// ... -> global variable |
788 | bool LLParser::parseUnnamedGlobal() { |
789 | unsigned VarID; |
790 | std::string Name; |
791 | LocTy NameLoc = Lex.getLoc(); |
792 | |
793 | // Handle the GlobalID form. |
794 | if (Lex.getKind() == lltok::GlobalID) { |
795 | VarID = Lex.getUIntVal(); |
796 | if (checkValueID(L: NameLoc, Kind: "global" , Prefix: "@" , NextID: NumberedVals.getNext(), ID: VarID)) |
797 | return true; |
798 | |
799 | Lex.Lex(); // eat GlobalID; |
800 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after name" )) |
801 | return true; |
802 | } else { |
803 | VarID = NumberedVals.getNext(); |
804 | } |
805 | |
806 | bool HasLinkage; |
807 | unsigned Linkage, Visibility, DLLStorageClass; |
808 | bool DSOLocal; |
809 | GlobalVariable::ThreadLocalMode TLM; |
810 | GlobalVariable::UnnamedAddr UnnamedAddr; |
811 | if (parseOptionalLinkage(Res&: Linkage, HasLinkage, Visibility, DLLStorageClass, |
812 | DSOLocal) || |
813 | parseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr)) |
814 | return true; |
815 | |
816 | switch (Lex.getKind()) { |
817 | default: |
818 | return parseGlobal(Name, NameID: VarID, NameLoc, Linkage, HasLinkage, Visibility, |
819 | DLLStorageClass, DSOLocal, TLM, UnnamedAddr); |
820 | case lltok::kw_alias: |
821 | case lltok::kw_ifunc: |
822 | return parseAliasOrIFunc(Name, NameID: VarID, NameLoc, L: Linkage, Visibility, |
823 | DLLStorageClass, DSOLocal, TLM, UnnamedAddr); |
824 | } |
825 | } |
826 | |
827 | /// parseNamedGlobal: |
828 | /// GlobalVar '=' OptionalVisibility (ALIAS | IFUNC) ... |
829 | /// GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier |
830 | /// OptionalVisibility OptionalDLLStorageClass |
831 | /// ... -> global variable |
832 | bool LLParser::parseNamedGlobal() { |
833 | assert(Lex.getKind() == lltok::GlobalVar); |
834 | LocTy NameLoc = Lex.getLoc(); |
835 | std::string Name = Lex.getStrVal(); |
836 | Lex.Lex(); |
837 | |
838 | bool HasLinkage; |
839 | unsigned Linkage, Visibility, DLLStorageClass; |
840 | bool DSOLocal; |
841 | GlobalVariable::ThreadLocalMode TLM; |
842 | GlobalVariable::UnnamedAddr UnnamedAddr; |
843 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' in global variable" ) || |
844 | parseOptionalLinkage(Res&: Linkage, HasLinkage, Visibility, DLLStorageClass, |
845 | DSOLocal) || |
846 | parseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr)) |
847 | return true; |
848 | |
849 | switch (Lex.getKind()) { |
850 | default: |
851 | return parseGlobal(Name, NameID: -1, NameLoc, Linkage, HasLinkage, Visibility, |
852 | DLLStorageClass, DSOLocal, TLM, UnnamedAddr); |
853 | case lltok::kw_alias: |
854 | case lltok::kw_ifunc: |
855 | return parseAliasOrIFunc(Name, NameID: -1, NameLoc, L: Linkage, Visibility, |
856 | DLLStorageClass, DSOLocal, TLM, UnnamedAddr); |
857 | } |
858 | } |
859 | |
860 | bool LLParser::parseComdat() { |
861 | assert(Lex.getKind() == lltok::ComdatVar); |
862 | std::string Name = Lex.getStrVal(); |
863 | LocTy NameLoc = Lex.getLoc(); |
864 | Lex.Lex(); |
865 | |
866 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here" )) |
867 | return true; |
868 | |
869 | if (parseToken(T: lltok::kw_comdat, ErrMsg: "expected comdat keyword" )) |
870 | return tokError(Msg: "expected comdat type" ); |
871 | |
872 | Comdat::SelectionKind SK; |
873 | switch (Lex.getKind()) { |
874 | default: |
875 | return tokError(Msg: "unknown selection kind" ); |
876 | case lltok::kw_any: |
877 | SK = Comdat::Any; |
878 | break; |
879 | case lltok::kw_exactmatch: |
880 | SK = Comdat::ExactMatch; |
881 | break; |
882 | case lltok::kw_largest: |
883 | SK = Comdat::Largest; |
884 | break; |
885 | case lltok::kw_nodeduplicate: |
886 | SK = Comdat::NoDeduplicate; |
887 | break; |
888 | case lltok::kw_samesize: |
889 | SK = Comdat::SameSize; |
890 | break; |
891 | } |
892 | Lex.Lex(); |
893 | |
894 | // See if the comdat was forward referenced, if so, use the comdat. |
895 | Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable(); |
896 | Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Key: Name); |
897 | if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(x: Name)) |
898 | return error(L: NameLoc, Msg: "redefinition of comdat '$" + Name + "'" ); |
899 | |
900 | Comdat *C; |
901 | if (I != ComdatSymTab.end()) |
902 | C = &I->second; |
903 | else |
904 | C = M->getOrInsertComdat(Name); |
905 | C->setSelectionKind(SK); |
906 | |
907 | return false; |
908 | } |
909 | |
910 | // MDString: |
911 | // ::= '!' STRINGCONSTANT |
912 | bool LLParser::parseMDString(MDString *&Result) { |
913 | std::string Str; |
914 | if (parseStringConstant(Result&: Str)) |
915 | return true; |
916 | Result = MDString::get(Context, Str); |
917 | return false; |
918 | } |
919 | |
920 | // MDNode: |
921 | // ::= '!' MDNodeNumber |
922 | bool LLParser::parseMDNodeID(MDNode *&Result) { |
923 | // !{ ..., !42, ... } |
924 | LocTy IDLoc = Lex.getLoc(); |
925 | unsigned MID = 0; |
926 | if (parseUInt32(Val&: MID)) |
927 | return true; |
928 | |
929 | // If not a forward reference, just return it now. |
930 | if (NumberedMetadata.count(x: MID)) { |
931 | Result = NumberedMetadata[MID]; |
932 | return false; |
933 | } |
934 | |
935 | // Otherwise, create MDNode forward reference. |
936 | auto &FwdRef = ForwardRefMDNodes[MID]; |
937 | FwdRef = std::make_pair(x: MDTuple::getTemporary(Context, MDs: std::nullopt), y&: IDLoc); |
938 | |
939 | Result = FwdRef.first.get(); |
940 | NumberedMetadata[MID].reset(MD: Result); |
941 | return false; |
942 | } |
943 | |
944 | /// parseNamedMetadata: |
945 | /// !foo = !{ !1, !2 } |
946 | bool LLParser::parseNamedMetadata() { |
947 | assert(Lex.getKind() == lltok::MetadataVar); |
948 | std::string Name = Lex.getStrVal(); |
949 | Lex.Lex(); |
950 | |
951 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here" ) || |
952 | parseToken(T: lltok::exclaim, ErrMsg: "Expected '!' here" ) || |
953 | parseToken(T: lltok::lbrace, ErrMsg: "Expected '{' here" )) |
954 | return true; |
955 | |
956 | NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name); |
957 | if (Lex.getKind() != lltok::rbrace) |
958 | do { |
959 | MDNode *N = nullptr; |
960 | // parse DIExpressions inline as a special case. They are still MDNodes, |
961 | // so they can still appear in named metadata. Remove this logic if they |
962 | // become plain Metadata. |
963 | if (Lex.getKind() == lltok::MetadataVar && |
964 | Lex.getStrVal() == "DIExpression" ) { |
965 | if (parseDIExpression(Result&: N, /*IsDistinct=*/false)) |
966 | return true; |
967 | // DIArgLists should only appear inline in a function, as they may |
968 | // contain LocalAsMetadata arguments which require a function context. |
969 | } else if (Lex.getKind() == lltok::MetadataVar && |
970 | Lex.getStrVal() == "DIArgList" ) { |
971 | return tokError(Msg: "found DIArgList outside of function" ); |
972 | } else if (parseToken(T: lltok::exclaim, ErrMsg: "Expected '!' here" ) || |
973 | parseMDNodeID(Result&: N)) { |
974 | return true; |
975 | } |
976 | NMD->addOperand(M: N); |
977 | } while (EatIfPresent(T: lltok::comma)); |
978 | |
979 | return parseToken(T: lltok::rbrace, ErrMsg: "expected end of metadata node" ); |
980 | } |
981 | |
982 | /// parseStandaloneMetadata: |
983 | /// !42 = !{...} |
984 | bool LLParser::parseStandaloneMetadata() { |
985 | assert(Lex.getKind() == lltok::exclaim); |
986 | Lex.Lex(); |
987 | unsigned MetadataID = 0; |
988 | |
989 | MDNode *Init; |
990 | if (parseUInt32(Val&: MetadataID) || parseToken(T: lltok::equal, ErrMsg: "expected '=' here" )) |
991 | return true; |
992 | |
993 | // Detect common error, from old metadata syntax. |
994 | if (Lex.getKind() == lltok::Type) |
995 | return tokError(Msg: "unexpected type in metadata definition" ); |
996 | |
997 | bool IsDistinct = EatIfPresent(T: lltok::kw_distinct); |
998 | if (Lex.getKind() == lltok::MetadataVar) { |
999 | if (parseSpecializedMDNode(N&: Init, IsDistinct)) |
1000 | return true; |
1001 | } else if (parseToken(T: lltok::exclaim, ErrMsg: "Expected '!' here" ) || |
1002 | parseMDTuple(MD&: Init, IsDistinct)) |
1003 | return true; |
1004 | |
1005 | // See if this was forward referenced, if so, handle it. |
1006 | auto FI = ForwardRefMDNodes.find(x: MetadataID); |
1007 | if (FI != ForwardRefMDNodes.end()) { |
1008 | auto *ToReplace = FI->second.first.get(); |
1009 | // DIAssignID has its own special forward-reference "replacement" for |
1010 | // attachments (the temporary attachments are never actually attached). |
1011 | if (isa<DIAssignID>(Val: Init)) { |
1012 | for (auto *Inst : TempDIAssignIDAttachments[ToReplace]) { |
1013 | assert(!Inst->getMetadata(LLVMContext::MD_DIAssignID) && |
1014 | "Inst unexpectedly already has DIAssignID attachment" ); |
1015 | Inst->setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: Init); |
1016 | } |
1017 | } |
1018 | |
1019 | ToReplace->replaceAllUsesWith(MD: Init); |
1020 | ForwardRefMDNodes.erase(position: FI); |
1021 | |
1022 | assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work" ); |
1023 | } else { |
1024 | if (NumberedMetadata.count(x: MetadataID)) |
1025 | return tokError(Msg: "Metadata id is already used" ); |
1026 | NumberedMetadata[MetadataID].reset(MD: Init); |
1027 | } |
1028 | |
1029 | return false; |
1030 | } |
1031 | |
1032 | // Skips a single module summary entry. |
1033 | bool LLParser::skipModuleSummaryEntry() { |
1034 | // Each module summary entry consists of a tag for the entry |
1035 | // type, followed by a colon, then the fields which may be surrounded by |
1036 | // nested sets of parentheses. The "tag:" looks like a Label. Once parsing |
1037 | // support is in place we will look for the tokens corresponding to the |
1038 | // expected tags. |
1039 | if (Lex.getKind() != lltok::kw_gv && Lex.getKind() != lltok::kw_module && |
1040 | Lex.getKind() != lltok::kw_typeid && Lex.getKind() != lltok::kw_flags && |
1041 | Lex.getKind() != lltok::kw_blockcount) |
1042 | return tokError( |
1043 | Msg: "Expected 'gv', 'module', 'typeid', 'flags' or 'blockcount' at the " |
1044 | "start of summary entry" ); |
1045 | if (Lex.getKind() == lltok::kw_flags) |
1046 | return parseSummaryIndexFlags(); |
1047 | if (Lex.getKind() == lltok::kw_blockcount) |
1048 | return parseBlockCount(); |
1049 | Lex.Lex(); |
1050 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' at start of summary entry" ) || |
1051 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' at start of summary entry" )) |
1052 | return true; |
1053 | // Now walk through the parenthesized entry, until the number of open |
1054 | // parentheses goes back down to 0 (the first '(' was parsed above). |
1055 | unsigned NumOpenParen = 1; |
1056 | do { |
1057 | switch (Lex.getKind()) { |
1058 | case lltok::lparen: |
1059 | NumOpenParen++; |
1060 | break; |
1061 | case lltok::rparen: |
1062 | NumOpenParen--; |
1063 | break; |
1064 | case lltok::Eof: |
1065 | return tokError(Msg: "found end of file while parsing summary entry" ); |
1066 | default: |
1067 | // Skip everything in between parentheses. |
1068 | break; |
1069 | } |
1070 | Lex.Lex(); |
1071 | } while (NumOpenParen > 0); |
1072 | return false; |
1073 | } |
1074 | |
1075 | /// SummaryEntry |
1076 | /// ::= SummaryID '=' GVEntry | ModuleEntry | TypeIdEntry |
1077 | bool LLParser::parseSummaryEntry() { |
1078 | assert(Lex.getKind() == lltok::SummaryID); |
1079 | unsigned SummaryID = Lex.getUIntVal(); |
1080 | |
1081 | // For summary entries, colons should be treated as distinct tokens, |
1082 | // not an indication of the end of a label token. |
1083 | Lex.setIgnoreColonInIdentifiers(true); |
1084 | |
1085 | Lex.Lex(); |
1086 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here" )) |
1087 | return true; |
1088 | |
1089 | // If we don't have an index object, skip the summary entry. |
1090 | if (!Index) |
1091 | return skipModuleSummaryEntry(); |
1092 | |
1093 | bool result = false; |
1094 | switch (Lex.getKind()) { |
1095 | case lltok::kw_gv: |
1096 | result = parseGVEntry(ID: SummaryID); |
1097 | break; |
1098 | case lltok::kw_module: |
1099 | result = parseModuleEntry(ID: SummaryID); |
1100 | break; |
1101 | case lltok::kw_typeid: |
1102 | result = parseTypeIdEntry(ID: SummaryID); |
1103 | break; |
1104 | case lltok::kw_typeidCompatibleVTable: |
1105 | result = parseTypeIdCompatibleVtableEntry(ID: SummaryID); |
1106 | break; |
1107 | case lltok::kw_flags: |
1108 | result = parseSummaryIndexFlags(); |
1109 | break; |
1110 | case lltok::kw_blockcount: |
1111 | result = parseBlockCount(); |
1112 | break; |
1113 | default: |
1114 | result = error(L: Lex.getLoc(), Msg: "unexpected summary kind" ); |
1115 | break; |
1116 | } |
1117 | Lex.setIgnoreColonInIdentifiers(false); |
1118 | return result; |
1119 | } |
1120 | |
1121 | static bool isValidVisibilityForLinkage(unsigned V, unsigned L) { |
1122 | return !GlobalValue::isLocalLinkage(Linkage: (GlobalValue::LinkageTypes)L) || |
1123 | (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility; |
1124 | } |
1125 | static bool isValidDLLStorageClassForLinkage(unsigned S, unsigned L) { |
1126 | return !GlobalValue::isLocalLinkage(Linkage: (GlobalValue::LinkageTypes)L) || |
1127 | (GlobalValue::DLLStorageClassTypes)S == GlobalValue::DefaultStorageClass; |
1128 | } |
1129 | |
1130 | // If there was an explicit dso_local, update GV. In the absence of an explicit |
1131 | // dso_local we keep the default value. |
1132 | static void maybeSetDSOLocal(bool DSOLocal, GlobalValue &GV) { |
1133 | if (DSOLocal) |
1134 | GV.setDSOLocal(true); |
1135 | } |
1136 | |
1137 | /// parseAliasOrIFunc: |
1138 | /// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier |
1139 | /// OptionalVisibility OptionalDLLStorageClass |
1140 | /// OptionalThreadLocal OptionalUnnamedAddr |
1141 | /// 'alias|ifunc' AliaseeOrResolver SymbolAttrs* |
1142 | /// |
1143 | /// AliaseeOrResolver |
1144 | /// ::= TypeAndValue |
1145 | /// |
1146 | /// SymbolAttrs |
1147 | /// ::= ',' 'partition' StringConstant |
1148 | /// |
1149 | /// Everything through OptionalUnnamedAddr has already been parsed. |
1150 | /// |
1151 | bool LLParser::parseAliasOrIFunc(const std::string &Name, unsigned NameID, |
1152 | LocTy NameLoc, unsigned L, unsigned Visibility, |
1153 | unsigned DLLStorageClass, bool DSOLocal, |
1154 | GlobalVariable::ThreadLocalMode TLM, |
1155 | GlobalVariable::UnnamedAddr UnnamedAddr) { |
1156 | bool IsAlias; |
1157 | if (Lex.getKind() == lltok::kw_alias) |
1158 | IsAlias = true; |
1159 | else if (Lex.getKind() == lltok::kw_ifunc) |
1160 | IsAlias = false; |
1161 | else |
1162 | llvm_unreachable("Not an alias or ifunc!" ); |
1163 | Lex.Lex(); |
1164 | |
1165 | GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L; |
1166 | |
1167 | if(IsAlias && !GlobalAlias::isValidLinkage(L: Linkage)) |
1168 | return error(L: NameLoc, Msg: "invalid linkage type for alias" ); |
1169 | |
1170 | if (!isValidVisibilityForLinkage(V: Visibility, L)) |
1171 | return error(L: NameLoc, |
1172 | Msg: "symbol with local linkage must have default visibility" ); |
1173 | |
1174 | if (!isValidDLLStorageClassForLinkage(S: DLLStorageClass, L)) |
1175 | return error(L: NameLoc, |
1176 | Msg: "symbol with local linkage cannot have a DLL storage class" ); |
1177 | |
1178 | Type *Ty; |
1179 | LocTy ExplicitTypeLoc = Lex.getLoc(); |
1180 | if (parseType(Result&: Ty) || |
1181 | parseToken(T: lltok::comma, ErrMsg: "expected comma after alias or ifunc's type" )) |
1182 | return true; |
1183 | |
1184 | Constant *Aliasee; |
1185 | LocTy AliaseeLoc = Lex.getLoc(); |
1186 | if (Lex.getKind() != lltok::kw_bitcast && |
1187 | Lex.getKind() != lltok::kw_getelementptr && |
1188 | Lex.getKind() != lltok::kw_addrspacecast && |
1189 | Lex.getKind() != lltok::kw_inttoptr) { |
1190 | if (parseGlobalTypeAndValue(V&: Aliasee)) |
1191 | return true; |
1192 | } else { |
1193 | // The bitcast dest type is not present, it is implied by the dest type. |
1194 | ValID ID; |
1195 | if (parseValID(ID, /*PFS=*/nullptr)) |
1196 | return true; |
1197 | if (ID.Kind != ValID::t_Constant) |
1198 | return error(L: AliaseeLoc, Msg: "invalid aliasee" ); |
1199 | Aliasee = ID.ConstantVal; |
1200 | } |
1201 | |
1202 | Type *AliaseeType = Aliasee->getType(); |
1203 | auto *PTy = dyn_cast<PointerType>(Val: AliaseeType); |
1204 | if (!PTy) |
1205 | return error(L: AliaseeLoc, Msg: "An alias or ifunc must have pointer type" ); |
1206 | unsigned AddrSpace = PTy->getAddressSpace(); |
1207 | |
1208 | GlobalValue *GVal = nullptr; |
1209 | |
1210 | // See if the alias was forward referenced, if so, prepare to replace the |
1211 | // forward reference. |
1212 | if (!Name.empty()) { |
1213 | auto I = ForwardRefVals.find(x: Name); |
1214 | if (I != ForwardRefVals.end()) { |
1215 | GVal = I->second.first; |
1216 | ForwardRefVals.erase(x: Name); |
1217 | } else if (M->getNamedValue(Name)) { |
1218 | return error(L: NameLoc, Msg: "redefinition of global '@" + Name + "'" ); |
1219 | } |
1220 | } else { |
1221 | auto I = ForwardRefValIDs.find(x: NameID); |
1222 | if (I != ForwardRefValIDs.end()) { |
1223 | GVal = I->second.first; |
1224 | ForwardRefValIDs.erase(position: I); |
1225 | } |
1226 | } |
1227 | |
1228 | // Okay, create the alias/ifunc but do not insert it into the module yet. |
1229 | std::unique_ptr<GlobalAlias> GA; |
1230 | std::unique_ptr<GlobalIFunc> GI; |
1231 | GlobalValue *GV; |
1232 | if (IsAlias) { |
1233 | GA.reset(p: GlobalAlias::create(Ty, AddressSpace: AddrSpace, |
1234 | Linkage: (GlobalValue::LinkageTypes)Linkage, Name, |
1235 | Aliasee, /*Parent*/ nullptr)); |
1236 | GV = GA.get(); |
1237 | } else { |
1238 | GI.reset(p: GlobalIFunc::create(Ty, AddressSpace: AddrSpace, |
1239 | Linkage: (GlobalValue::LinkageTypes)Linkage, Name, |
1240 | Resolver: Aliasee, /*Parent*/ nullptr)); |
1241 | GV = GI.get(); |
1242 | } |
1243 | GV->setThreadLocalMode(TLM); |
1244 | GV->setVisibility((GlobalValue::VisibilityTypes)Visibility); |
1245 | GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass); |
1246 | GV->setUnnamedAddr(UnnamedAddr); |
1247 | maybeSetDSOLocal(DSOLocal, GV&: *GV); |
1248 | |
1249 | // At this point we've parsed everything except for the IndirectSymbolAttrs. |
1250 | // Now parse them if there are any. |
1251 | while (Lex.getKind() == lltok::comma) { |
1252 | Lex.Lex(); |
1253 | |
1254 | if (Lex.getKind() == lltok::kw_partition) { |
1255 | Lex.Lex(); |
1256 | GV->setPartition(Lex.getStrVal()); |
1257 | if (parseToken(T: lltok::StringConstant, ErrMsg: "expected partition string" )) |
1258 | return true; |
1259 | } else { |
1260 | return tokError(Msg: "unknown alias or ifunc property!" ); |
1261 | } |
1262 | } |
1263 | |
1264 | if (Name.empty()) |
1265 | NumberedVals.add(ID: NameID, V: GV); |
1266 | |
1267 | if (GVal) { |
1268 | // Verify that types agree. |
1269 | if (GVal->getType() != GV->getType()) |
1270 | return error( |
1271 | L: ExplicitTypeLoc, |
1272 | Msg: "forward reference and definition of alias have different types" ); |
1273 | |
1274 | // If they agree, just RAUW the old value with the alias and remove the |
1275 | // forward ref info. |
1276 | GVal->replaceAllUsesWith(V: GV); |
1277 | GVal->eraseFromParent(); |
1278 | } |
1279 | |
1280 | // Insert into the module, we know its name won't collide now. |
1281 | if (IsAlias) |
1282 | M->insertAlias(Alias: GA.release()); |
1283 | else |
1284 | M->insertIFunc(IFunc: GI.release()); |
1285 | assert(GV->getName() == Name && "Should not be a name conflict!" ); |
1286 | |
1287 | return false; |
1288 | } |
1289 | |
1290 | static bool isSanitizer(lltok::Kind Kind) { |
1291 | switch (Kind) { |
1292 | case lltok::kw_no_sanitize_address: |
1293 | case lltok::kw_no_sanitize_hwaddress: |
1294 | case lltok::kw_sanitize_memtag: |
1295 | case lltok::kw_sanitize_address_dyninit: |
1296 | return true; |
1297 | default: |
1298 | return false; |
1299 | } |
1300 | } |
1301 | |
1302 | bool LLParser::parseSanitizer(GlobalVariable *GV) { |
1303 | using SanitizerMetadata = GlobalValue::SanitizerMetadata; |
1304 | SanitizerMetadata Meta; |
1305 | if (GV->hasSanitizerMetadata()) |
1306 | Meta = GV->getSanitizerMetadata(); |
1307 | |
1308 | switch (Lex.getKind()) { |
1309 | case lltok::kw_no_sanitize_address: |
1310 | Meta.NoAddress = true; |
1311 | break; |
1312 | case lltok::kw_no_sanitize_hwaddress: |
1313 | Meta.NoHWAddress = true; |
1314 | break; |
1315 | case lltok::kw_sanitize_memtag: |
1316 | Meta.Memtag = true; |
1317 | break; |
1318 | case lltok::kw_sanitize_address_dyninit: |
1319 | Meta.IsDynInit = true; |
1320 | break; |
1321 | default: |
1322 | return tokError(Msg: "non-sanitizer token passed to LLParser::parseSanitizer()" ); |
1323 | } |
1324 | GV->setSanitizerMetadata(Meta); |
1325 | Lex.Lex(); |
1326 | return false; |
1327 | } |
1328 | |
1329 | /// parseGlobal |
1330 | /// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier |
1331 | /// OptionalVisibility OptionalDLLStorageClass |
1332 | /// OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace |
1333 | /// OptionalExternallyInitialized GlobalType Type Const OptionalAttrs |
1334 | /// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility |
1335 | /// OptionalDLLStorageClass OptionalThreadLocal OptionalUnnamedAddr |
1336 | /// OptionalAddrSpace OptionalExternallyInitialized GlobalType Type |
1337 | /// Const OptionalAttrs |
1338 | /// |
1339 | /// Everything up to and including OptionalUnnamedAddr has been parsed |
1340 | /// already. |
1341 | /// |
1342 | bool LLParser::parseGlobal(const std::string &Name, unsigned NameID, |
1343 | LocTy NameLoc, unsigned Linkage, bool HasLinkage, |
1344 | unsigned Visibility, unsigned DLLStorageClass, |
1345 | bool DSOLocal, GlobalVariable::ThreadLocalMode TLM, |
1346 | GlobalVariable::UnnamedAddr UnnamedAddr) { |
1347 | if (!isValidVisibilityForLinkage(V: Visibility, L: Linkage)) |
1348 | return error(L: NameLoc, |
1349 | Msg: "symbol with local linkage must have default visibility" ); |
1350 | |
1351 | if (!isValidDLLStorageClassForLinkage(S: DLLStorageClass, L: Linkage)) |
1352 | return error(L: NameLoc, |
1353 | Msg: "symbol with local linkage cannot have a DLL storage class" ); |
1354 | |
1355 | unsigned AddrSpace; |
1356 | bool IsConstant, IsExternallyInitialized; |
1357 | LocTy IsExternallyInitializedLoc; |
1358 | LocTy TyLoc; |
1359 | |
1360 | Type *Ty = nullptr; |
1361 | if (parseOptionalAddrSpace(AddrSpace) || |
1362 | parseOptionalToken(T: lltok::kw_externally_initialized, |
1363 | Present&: IsExternallyInitialized, |
1364 | Loc: &IsExternallyInitializedLoc) || |
1365 | parseGlobalType(IsConstant) || parseType(Result&: Ty, Loc&: TyLoc)) |
1366 | return true; |
1367 | |
1368 | // If the linkage is specified and is external, then no initializer is |
1369 | // present. |
1370 | Constant *Init = nullptr; |
1371 | if (!HasLinkage || |
1372 | !GlobalValue::isValidDeclarationLinkage( |
1373 | Linkage: (GlobalValue::LinkageTypes)Linkage)) { |
1374 | if (parseGlobalValue(Ty, C&: Init)) |
1375 | return true; |
1376 | } |
1377 | |
1378 | if (Ty->isFunctionTy() || !PointerType::isValidElementType(ElemTy: Ty)) |
1379 | return error(L: TyLoc, Msg: "invalid type for global variable" ); |
1380 | |
1381 | GlobalValue *GVal = nullptr; |
1382 | |
1383 | // See if the global was forward referenced, if so, use the global. |
1384 | if (!Name.empty()) { |
1385 | auto I = ForwardRefVals.find(x: Name); |
1386 | if (I != ForwardRefVals.end()) { |
1387 | GVal = I->second.first; |
1388 | ForwardRefVals.erase(position: I); |
1389 | } else if (M->getNamedValue(Name)) { |
1390 | return error(L: NameLoc, Msg: "redefinition of global '@" + Name + "'" ); |
1391 | } |
1392 | } else { |
1393 | // Handle @"", where a name is syntactically specified, but semantically |
1394 | // missing. |
1395 | if (NameID == (unsigned)-1) |
1396 | NameID = NumberedVals.getNext(); |
1397 | |
1398 | auto I = ForwardRefValIDs.find(x: NameID); |
1399 | if (I != ForwardRefValIDs.end()) { |
1400 | GVal = I->second.first; |
1401 | ForwardRefValIDs.erase(position: I); |
1402 | } |
1403 | } |
1404 | |
1405 | GlobalVariable *GV = new GlobalVariable( |
1406 | *M, Ty, false, GlobalValue::ExternalLinkage, nullptr, Name, nullptr, |
1407 | GlobalVariable::NotThreadLocal, AddrSpace); |
1408 | |
1409 | if (Name.empty()) |
1410 | NumberedVals.add(ID: NameID, V: GV); |
1411 | |
1412 | // Set the parsed properties on the global. |
1413 | if (Init) |
1414 | GV->setInitializer(Init); |
1415 | GV->setConstant(IsConstant); |
1416 | GV->setLinkage((GlobalValue::LinkageTypes)Linkage); |
1417 | maybeSetDSOLocal(DSOLocal, GV&: *GV); |
1418 | GV->setVisibility((GlobalValue::VisibilityTypes)Visibility); |
1419 | GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass); |
1420 | GV->setExternallyInitialized(IsExternallyInitialized); |
1421 | GV->setThreadLocalMode(TLM); |
1422 | GV->setUnnamedAddr(UnnamedAddr); |
1423 | |
1424 | if (GVal) { |
1425 | if (GVal->getAddressSpace() != AddrSpace) |
1426 | return error( |
1427 | L: TyLoc, |
1428 | Msg: "forward reference and definition of global have different types" ); |
1429 | |
1430 | GVal->replaceAllUsesWith(V: GV); |
1431 | GVal->eraseFromParent(); |
1432 | } |
1433 | |
1434 | // parse attributes on the global. |
1435 | while (Lex.getKind() == lltok::comma) { |
1436 | Lex.Lex(); |
1437 | |
1438 | if (Lex.getKind() == lltok::kw_section) { |
1439 | Lex.Lex(); |
1440 | GV->setSection(Lex.getStrVal()); |
1441 | if (parseToken(T: lltok::StringConstant, ErrMsg: "expected global section string" )) |
1442 | return true; |
1443 | } else if (Lex.getKind() == lltok::kw_partition) { |
1444 | Lex.Lex(); |
1445 | GV->setPartition(Lex.getStrVal()); |
1446 | if (parseToken(T: lltok::StringConstant, ErrMsg: "expected partition string" )) |
1447 | return true; |
1448 | } else if (Lex.getKind() == lltok::kw_align) { |
1449 | MaybeAlign Alignment; |
1450 | if (parseOptionalAlignment(Alignment)) |
1451 | return true; |
1452 | if (Alignment) |
1453 | GV->setAlignment(*Alignment); |
1454 | } else if (Lex.getKind() == lltok::kw_code_model) { |
1455 | CodeModel::Model CodeModel; |
1456 | if (parseOptionalCodeModel(model&: CodeModel)) |
1457 | return true; |
1458 | GV->setCodeModel(CodeModel); |
1459 | } else if (Lex.getKind() == lltok::MetadataVar) { |
1460 | if (parseGlobalObjectMetadataAttachment(GO&: *GV)) |
1461 | return true; |
1462 | } else if (isSanitizer(Kind: Lex.getKind())) { |
1463 | if (parseSanitizer(GV)) |
1464 | return true; |
1465 | } else { |
1466 | Comdat *C; |
1467 | if (parseOptionalComdat(GlobalName: Name, C)) |
1468 | return true; |
1469 | if (C) |
1470 | GV->setComdat(C); |
1471 | else |
1472 | return tokError(Msg: "unknown global variable property!" ); |
1473 | } |
1474 | } |
1475 | |
1476 | AttrBuilder Attrs(M->getContext()); |
1477 | LocTy BuiltinLoc; |
1478 | std::vector<unsigned> FwdRefAttrGrps; |
1479 | if (parseFnAttributeValuePairs(B&: Attrs, FwdRefAttrGrps, inAttrGrp: false, BuiltinLoc)) |
1480 | return true; |
1481 | if (Attrs.hasAttributes() || !FwdRefAttrGrps.empty()) { |
1482 | GV->setAttributes(AttributeSet::get(C&: Context, B: Attrs)); |
1483 | ForwardRefAttrGroups[GV] = FwdRefAttrGrps; |
1484 | } |
1485 | |
1486 | return false; |
1487 | } |
1488 | |
1489 | /// parseUnnamedAttrGrp |
1490 | /// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}' |
1491 | bool LLParser::parseUnnamedAttrGrp() { |
1492 | assert(Lex.getKind() == lltok::kw_attributes); |
1493 | LocTy AttrGrpLoc = Lex.getLoc(); |
1494 | Lex.Lex(); |
1495 | |
1496 | if (Lex.getKind() != lltok::AttrGrpID) |
1497 | return tokError(Msg: "expected attribute group id" ); |
1498 | |
1499 | unsigned VarID = Lex.getUIntVal(); |
1500 | std::vector<unsigned> unused; |
1501 | LocTy BuiltinLoc; |
1502 | Lex.Lex(); |
1503 | |
1504 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here" ) || |
1505 | parseToken(T: lltok::lbrace, ErrMsg: "expected '{' here" )) |
1506 | return true; |
1507 | |
1508 | auto R = NumberedAttrBuilders.find(x: VarID); |
1509 | if (R == NumberedAttrBuilders.end()) |
1510 | R = NumberedAttrBuilders.emplace(args&: VarID, args: AttrBuilder(M->getContext())).first; |
1511 | |
1512 | if (parseFnAttributeValuePairs(B&: R->second, FwdRefAttrGrps&: unused, inAttrGrp: true, BuiltinLoc) || |
1513 | parseToken(T: lltok::rbrace, ErrMsg: "expected end of attribute group" )) |
1514 | return true; |
1515 | |
1516 | if (!R->second.hasAttributes()) |
1517 | return error(L: AttrGrpLoc, Msg: "attribute group has no attributes" ); |
1518 | |
1519 | return false; |
1520 | } |
1521 | |
1522 | static Attribute::AttrKind tokenToAttribute(lltok::Kind Kind) { |
1523 | switch (Kind) { |
1524 | #define GET_ATTR_NAMES |
1525 | #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \ |
1526 | case lltok::kw_##DISPLAY_NAME: \ |
1527 | return Attribute::ENUM_NAME; |
1528 | #include "llvm/IR/Attributes.inc" |
1529 | default: |
1530 | return Attribute::None; |
1531 | } |
1532 | } |
1533 | |
1534 | bool LLParser::parseEnumAttribute(Attribute::AttrKind Attr, AttrBuilder &B, |
1535 | bool InAttrGroup) { |
1536 | if (Attribute::isTypeAttrKind(Kind: Attr)) |
1537 | return parseRequiredTypeAttr(B, AttrToken: Lex.getKind(), AttrKind: Attr); |
1538 | |
1539 | switch (Attr) { |
1540 | case Attribute::Alignment: { |
1541 | MaybeAlign Alignment; |
1542 | if (InAttrGroup) { |
1543 | uint32_t Value = 0; |
1544 | Lex.Lex(); |
1545 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here" ) || parseUInt32(Val&: Value)) |
1546 | return true; |
1547 | Alignment = Align(Value); |
1548 | } else { |
1549 | if (parseOptionalAlignment(Alignment, AllowParens: true)) |
1550 | return true; |
1551 | } |
1552 | B.addAlignmentAttr(Align: Alignment); |
1553 | return false; |
1554 | } |
1555 | case Attribute::StackAlignment: { |
1556 | unsigned Alignment; |
1557 | if (InAttrGroup) { |
1558 | Lex.Lex(); |
1559 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here" ) || |
1560 | parseUInt32(Val&: Alignment)) |
1561 | return true; |
1562 | } else { |
1563 | if (parseOptionalStackAlignment(Alignment)) |
1564 | return true; |
1565 | } |
1566 | B.addStackAlignmentAttr(Align: Alignment); |
1567 | return false; |
1568 | } |
1569 | case Attribute::AllocSize: { |
1570 | unsigned ElemSizeArg; |
1571 | std::optional<unsigned> NumElemsArg; |
1572 | if (parseAllocSizeArguments(BaseSizeArg&: ElemSizeArg, HowManyArg&: NumElemsArg)) |
1573 | return true; |
1574 | B.addAllocSizeAttr(ElemSizeArg, NumElemsArg); |
1575 | return false; |
1576 | } |
1577 | case Attribute::VScaleRange: { |
1578 | unsigned MinValue, MaxValue; |
1579 | if (parseVScaleRangeArguments(MinValue, MaxValue)) |
1580 | return true; |
1581 | B.addVScaleRangeAttr(MinValue, |
1582 | MaxValue: MaxValue > 0 ? MaxValue : std::optional<unsigned>()); |
1583 | return false; |
1584 | } |
1585 | case Attribute::Dereferenceable: { |
1586 | uint64_t Bytes; |
1587 | if (parseOptionalDerefAttrBytes(lltok::AttrKind: kw_dereferenceable, Bytes)) |
1588 | return true; |
1589 | B.addDereferenceableAttr(Bytes); |
1590 | return false; |
1591 | } |
1592 | case Attribute::DereferenceableOrNull: { |
1593 | uint64_t Bytes; |
1594 | if (parseOptionalDerefAttrBytes(lltok::AttrKind: kw_dereferenceable_or_null, Bytes)) |
1595 | return true; |
1596 | B.addDereferenceableOrNullAttr(Bytes); |
1597 | return false; |
1598 | } |
1599 | case Attribute::UWTable: { |
1600 | UWTableKind Kind; |
1601 | if (parseOptionalUWTableKind(Kind)) |
1602 | return true; |
1603 | B.addUWTableAttr(Kind); |
1604 | return false; |
1605 | } |
1606 | case Attribute::AllocKind: { |
1607 | AllocFnKind Kind = AllocFnKind::Unknown; |
1608 | if (parseAllocKind(Kind)) |
1609 | return true; |
1610 | B.addAllocKindAttr(Kind); |
1611 | return false; |
1612 | } |
1613 | case Attribute::Memory: { |
1614 | std::optional<MemoryEffects> ME = parseMemoryAttr(); |
1615 | if (!ME) |
1616 | return true; |
1617 | B.addMemoryAttr(ME: *ME); |
1618 | return false; |
1619 | } |
1620 | case Attribute::NoFPClass: { |
1621 | if (FPClassTest NoFPClass = |
1622 | static_cast<FPClassTest>(parseNoFPClassAttr())) { |
1623 | B.addNoFPClassAttr(NoFPClassMask: NoFPClass); |
1624 | return false; |
1625 | } |
1626 | |
1627 | return true; |
1628 | } |
1629 | case Attribute::Range: |
1630 | return parseRangeAttr(B); |
1631 | default: |
1632 | B.addAttribute(Val: Attr); |
1633 | Lex.Lex(); |
1634 | return false; |
1635 | } |
1636 | } |
1637 | |
1638 | static bool upgradeMemoryAttr(MemoryEffects &ME, lltok::Kind Kind) { |
1639 | switch (Kind) { |
1640 | case lltok::kw_readnone: |
1641 | ME &= MemoryEffects::none(); |
1642 | return true; |
1643 | case lltok::kw_readonly: |
1644 | ME &= MemoryEffects::readOnly(); |
1645 | return true; |
1646 | case lltok::kw_writeonly: |
1647 | ME &= MemoryEffects::writeOnly(); |
1648 | return true; |
1649 | case lltok::kw_argmemonly: |
1650 | ME &= MemoryEffects::argMemOnly(); |
1651 | return true; |
1652 | case lltok::kw_inaccessiblememonly: |
1653 | ME &= MemoryEffects::inaccessibleMemOnly(); |
1654 | return true; |
1655 | case lltok::kw_inaccessiblemem_or_argmemonly: |
1656 | ME &= MemoryEffects::inaccessibleOrArgMemOnly(); |
1657 | return true; |
1658 | default: |
1659 | return false; |
1660 | } |
1661 | } |
1662 | |
1663 | /// parseFnAttributeValuePairs |
1664 | /// ::= <attr> | <attr> '=' <value> |
1665 | bool LLParser::parseFnAttributeValuePairs(AttrBuilder &B, |
1666 | std::vector<unsigned> &FwdRefAttrGrps, |
1667 | bool InAttrGrp, LocTy &BuiltinLoc) { |
1668 | bool HaveError = false; |
1669 | |
1670 | B.clear(); |
1671 | |
1672 | MemoryEffects ME = MemoryEffects::unknown(); |
1673 | while (true) { |
1674 | lltok::Kind Token = Lex.getKind(); |
1675 | if (Token == lltok::rbrace) |
1676 | break; // Finished. |
1677 | |
1678 | if (Token == lltok::StringConstant) { |
1679 | if (parseStringAttribute(B)) |
1680 | return true; |
1681 | continue; |
1682 | } |
1683 | |
1684 | if (Token == lltok::AttrGrpID) { |
1685 | // Allow a function to reference an attribute group: |
1686 | // |
1687 | // define void @foo() #1 { ... } |
1688 | if (InAttrGrp) { |
1689 | HaveError |= error( |
1690 | L: Lex.getLoc(), |
1691 | Msg: "cannot have an attribute group reference in an attribute group" ); |
1692 | } else { |
1693 | // Save the reference to the attribute group. We'll fill it in later. |
1694 | FwdRefAttrGrps.push_back(x: Lex.getUIntVal()); |
1695 | } |
1696 | Lex.Lex(); |
1697 | continue; |
1698 | } |
1699 | |
1700 | SMLoc Loc = Lex.getLoc(); |
1701 | if (Token == lltok::kw_builtin) |
1702 | BuiltinLoc = Loc; |
1703 | |
1704 | if (upgradeMemoryAttr(ME, Kind: Token)) { |
1705 | Lex.Lex(); |
1706 | continue; |
1707 | } |
1708 | |
1709 | Attribute::AttrKind Attr = tokenToAttribute(Kind: Token); |
1710 | if (Attr == Attribute::None) { |
1711 | if (!InAttrGrp) |
1712 | break; |
1713 | return error(L: Lex.getLoc(), Msg: "unterminated attribute group" ); |
1714 | } |
1715 | |
1716 | if (parseEnumAttribute(Attr, B, InAttrGroup: InAttrGrp)) |
1717 | return true; |
1718 | |
1719 | // As a hack, we allow function alignment to be initially parsed as an |
1720 | // attribute on a function declaration/definition or added to an attribute |
1721 | // group and later moved to the alignment field. |
1722 | if (!Attribute::canUseAsFnAttr(Kind: Attr) && Attr != Attribute::Alignment) |
1723 | HaveError |= error(L: Loc, Msg: "this attribute does not apply to functions" ); |
1724 | } |
1725 | |
1726 | if (ME != MemoryEffects::unknown()) |
1727 | B.addMemoryAttr(ME); |
1728 | return HaveError; |
1729 | } |
1730 | |
1731 | //===----------------------------------------------------------------------===// |
1732 | // GlobalValue Reference/Resolution Routines. |
1733 | //===----------------------------------------------------------------------===// |
1734 | |
1735 | static inline GlobalValue *createGlobalFwdRef(Module *M, PointerType *PTy) { |
1736 | // The used global type does not matter. We will later RAUW it with a |
1737 | // global/function of the correct type. |
1738 | return new GlobalVariable(*M, Type::getInt8Ty(C&: M->getContext()), false, |
1739 | GlobalValue::ExternalWeakLinkage, nullptr, "" , |
1740 | nullptr, GlobalVariable::NotThreadLocal, |
1741 | PTy->getAddressSpace()); |
1742 | } |
1743 | |
1744 | Value *LLParser::checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty, |
1745 | Value *Val) { |
1746 | Type *ValTy = Val->getType(); |
1747 | if (ValTy == Ty) |
1748 | return Val; |
1749 | if (Ty->isLabelTy()) |
1750 | error(L: Loc, Msg: "'" + Name + "' is not a basic block" ); |
1751 | else |
1752 | error(L: Loc, Msg: "'" + Name + "' defined with type '" + |
1753 | getTypeString(T: Val->getType()) + "' but expected '" + |
1754 | getTypeString(T: Ty) + "'" ); |
1755 | return nullptr; |
1756 | } |
1757 | |
1758 | /// getGlobalVal - Get a value with the specified name or ID, creating a |
1759 | /// forward reference record if needed. This can return null if the value |
1760 | /// exists but does not have the right type. |
1761 | GlobalValue *LLParser::getGlobalVal(const std::string &Name, Type *Ty, |
1762 | LocTy Loc) { |
1763 | PointerType *PTy = dyn_cast<PointerType>(Val: Ty); |
1764 | if (!PTy) { |
1765 | error(L: Loc, Msg: "global variable reference must have pointer type" ); |
1766 | return nullptr; |
1767 | } |
1768 | |
1769 | // Look this name up in the normal function symbol table. |
1770 | GlobalValue *Val = |
1771 | cast_or_null<GlobalValue>(Val: M->getValueSymbolTable().lookup(Name)); |
1772 | |
1773 | // If this is a forward reference for the value, see if we already created a |
1774 | // forward ref record. |
1775 | if (!Val) { |
1776 | auto I = ForwardRefVals.find(x: Name); |
1777 | if (I != ForwardRefVals.end()) |
1778 | Val = I->second.first; |
1779 | } |
1780 | |
1781 | // If we have the value in the symbol table or fwd-ref table, return it. |
1782 | if (Val) |
1783 | return cast_or_null<GlobalValue>( |
1784 | Val: checkValidVariableType(Loc, Name: "@" + Name, Ty, Val)); |
1785 | |
1786 | // Otherwise, create a new forward reference for this value and remember it. |
1787 | GlobalValue *FwdVal = createGlobalFwdRef(M, PTy); |
1788 | ForwardRefVals[Name] = std::make_pair(x&: FwdVal, y&: Loc); |
1789 | return FwdVal; |
1790 | } |
1791 | |
1792 | GlobalValue *LLParser::getGlobalVal(unsigned ID, Type *Ty, LocTy Loc) { |
1793 | PointerType *PTy = dyn_cast<PointerType>(Val: Ty); |
1794 | if (!PTy) { |
1795 | error(L: Loc, Msg: "global variable reference must have pointer type" ); |
1796 | return nullptr; |
1797 | } |
1798 | |
1799 | GlobalValue *Val = NumberedVals.get(ID); |
1800 | |
1801 | // If this is a forward reference for the value, see if we already created a |
1802 | // forward ref record. |
1803 | if (!Val) { |
1804 | auto I = ForwardRefValIDs.find(x: ID); |
1805 | if (I != ForwardRefValIDs.end()) |
1806 | Val = I->second.first; |
1807 | } |
1808 | |
1809 | // If we have the value in the symbol table or fwd-ref table, return it. |
1810 | if (Val) |
1811 | return cast_or_null<GlobalValue>( |
1812 | Val: checkValidVariableType(Loc, Name: "@" + Twine(ID), Ty, Val)); |
1813 | |
1814 | // Otherwise, create a new forward reference for this value and remember it. |
1815 | GlobalValue *FwdVal = createGlobalFwdRef(M, PTy); |
1816 | ForwardRefValIDs[ID] = std::make_pair(x&: FwdVal, y&: Loc); |
1817 | return FwdVal; |
1818 | } |
1819 | |
1820 | //===----------------------------------------------------------------------===// |
1821 | // Comdat Reference/Resolution Routines. |
1822 | //===----------------------------------------------------------------------===// |
1823 | |
1824 | Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) { |
1825 | // Look this name up in the comdat symbol table. |
1826 | Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable(); |
1827 | Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Key: Name); |
1828 | if (I != ComdatSymTab.end()) |
1829 | return &I->second; |
1830 | |
1831 | // Otherwise, create a new forward reference for this value and remember it. |
1832 | Comdat *C = M->getOrInsertComdat(Name); |
1833 | ForwardRefComdats[Name] = Loc; |
1834 | return C; |
1835 | } |
1836 | |
1837 | //===----------------------------------------------------------------------===// |
1838 | // Helper Routines. |
1839 | //===----------------------------------------------------------------------===// |
1840 | |
1841 | /// parseToken - If the current token has the specified kind, eat it and return |
1842 | /// success. Otherwise, emit the specified error and return failure. |
1843 | bool LLParser::parseToken(lltok::Kind T, const char *ErrMsg) { |
1844 | if (Lex.getKind() != T) |
1845 | return tokError(Msg: ErrMsg); |
1846 | Lex.Lex(); |
1847 | return false; |
1848 | } |
1849 | |
1850 | /// parseStringConstant |
1851 | /// ::= StringConstant |
1852 | bool LLParser::parseStringConstant(std::string &Result) { |
1853 | if (Lex.getKind() != lltok::StringConstant) |
1854 | return tokError(Msg: "expected string constant" ); |
1855 | Result = Lex.getStrVal(); |
1856 | Lex.Lex(); |
1857 | return false; |
1858 | } |
1859 | |
1860 | /// parseUInt32 |
1861 | /// ::= uint32 |
1862 | bool LLParser::parseUInt32(uint32_t &Val) { |
1863 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
1864 | return tokError(Msg: "expected integer" ); |
1865 | uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(Limit: 0xFFFFFFFFULL+1); |
1866 | if (Val64 != unsigned(Val64)) |
1867 | return tokError(Msg: "expected 32-bit integer (too large)" ); |
1868 | Val = Val64; |
1869 | Lex.Lex(); |
1870 | return false; |
1871 | } |
1872 | |
1873 | /// parseUInt64 |
1874 | /// ::= uint64 |
1875 | bool LLParser::parseUInt64(uint64_t &Val) { |
1876 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
1877 | return tokError(Msg: "expected integer" ); |
1878 | Val = Lex.getAPSIntVal().getLimitedValue(); |
1879 | Lex.Lex(); |
1880 | return false; |
1881 | } |
1882 | |
1883 | /// parseTLSModel |
1884 | /// := 'localdynamic' |
1885 | /// := 'initialexec' |
1886 | /// := 'localexec' |
1887 | bool LLParser::parseTLSModel(GlobalVariable::ThreadLocalMode &TLM) { |
1888 | switch (Lex.getKind()) { |
1889 | default: |
1890 | return tokError(Msg: "expected localdynamic, initialexec or localexec" ); |
1891 | case lltok::kw_localdynamic: |
1892 | TLM = GlobalVariable::LocalDynamicTLSModel; |
1893 | break; |
1894 | case lltok::kw_initialexec: |
1895 | TLM = GlobalVariable::InitialExecTLSModel; |
1896 | break; |
1897 | case lltok::kw_localexec: |
1898 | TLM = GlobalVariable::LocalExecTLSModel; |
1899 | break; |
1900 | } |
1901 | |
1902 | Lex.Lex(); |
1903 | return false; |
1904 | } |
1905 | |
1906 | /// parseOptionalThreadLocal |
1907 | /// := /*empty*/ |
1908 | /// := 'thread_local' |
1909 | /// := 'thread_local' '(' tlsmodel ')' |
1910 | bool LLParser::parseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) { |
1911 | TLM = GlobalVariable::NotThreadLocal; |
1912 | if (!EatIfPresent(T: lltok::kw_thread_local)) |
1913 | return false; |
1914 | |
1915 | TLM = GlobalVariable::GeneralDynamicTLSModel; |
1916 | if (Lex.getKind() == lltok::lparen) { |
1917 | Lex.Lex(); |
1918 | return parseTLSModel(TLM) || |
1919 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' after thread local model" ); |
1920 | } |
1921 | return false; |
1922 | } |
1923 | |
1924 | /// parseOptionalAddrSpace |
1925 | /// := /*empty*/ |
1926 | /// := 'addrspace' '(' uint32 ')' |
1927 | bool LLParser::parseOptionalAddrSpace(unsigned &AddrSpace, unsigned DefaultAS) { |
1928 | AddrSpace = DefaultAS; |
1929 | if (!EatIfPresent(T: lltok::kw_addrspace)) |
1930 | return false; |
1931 | |
1932 | auto ParseAddrspaceValue = [&](unsigned &AddrSpace) -> bool { |
1933 | if (Lex.getKind() == lltok::StringConstant) { |
1934 | auto AddrSpaceStr = Lex.getStrVal(); |
1935 | if (AddrSpaceStr == "A" ) { |
1936 | AddrSpace = M->getDataLayout().getAllocaAddrSpace(); |
1937 | } else if (AddrSpaceStr == "G" ) { |
1938 | AddrSpace = M->getDataLayout().getDefaultGlobalsAddressSpace(); |
1939 | } else if (AddrSpaceStr == "P" ) { |
1940 | AddrSpace = M->getDataLayout().getProgramAddressSpace(); |
1941 | } else { |
1942 | return tokError(Msg: "invalid symbolic addrspace '" + AddrSpaceStr + "'" ); |
1943 | } |
1944 | Lex.Lex(); |
1945 | return false; |
1946 | } |
1947 | if (Lex.getKind() != lltok::APSInt) |
1948 | return tokError(Msg: "expected integer or string constant" ); |
1949 | SMLoc Loc = Lex.getLoc(); |
1950 | if (parseUInt32(Val&: AddrSpace)) |
1951 | return true; |
1952 | if (!isUInt<24>(x: AddrSpace)) |
1953 | return error(L: Loc, Msg: "invalid address space, must be a 24-bit integer" ); |
1954 | return false; |
1955 | }; |
1956 | |
1957 | return parseToken(T: lltok::lparen, ErrMsg: "expected '(' in address space" ) || |
1958 | ParseAddrspaceValue(AddrSpace) || |
1959 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' in address space" ); |
1960 | } |
1961 | |
1962 | /// parseStringAttribute |
1963 | /// := StringConstant |
1964 | /// := StringConstant '=' StringConstant |
1965 | bool LLParser::parseStringAttribute(AttrBuilder &B) { |
1966 | std::string Attr = Lex.getStrVal(); |
1967 | Lex.Lex(); |
1968 | std::string Val; |
1969 | if (EatIfPresent(T: lltok::equal) && parseStringConstant(Result&: Val)) |
1970 | return true; |
1971 | B.addAttribute(A: Attr, V: Val); |
1972 | return false; |
1973 | } |
1974 | |
1975 | /// Parse a potentially empty list of parameter or return attributes. |
1976 | bool LLParser::parseOptionalParamOrReturnAttrs(AttrBuilder &B, bool IsParam) { |
1977 | bool HaveError = false; |
1978 | |
1979 | B.clear(); |
1980 | |
1981 | while (true) { |
1982 | lltok::Kind Token = Lex.getKind(); |
1983 | if (Token == lltok::StringConstant) { |
1984 | if (parseStringAttribute(B)) |
1985 | return true; |
1986 | continue; |
1987 | } |
1988 | |
1989 | SMLoc Loc = Lex.getLoc(); |
1990 | Attribute::AttrKind Attr = tokenToAttribute(Kind: Token); |
1991 | if (Attr == Attribute::None) |
1992 | return HaveError; |
1993 | |
1994 | if (parseEnumAttribute(Attr, B, /* InAttrGroup */ false)) |
1995 | return true; |
1996 | |
1997 | if (IsParam && !Attribute::canUseAsParamAttr(Kind: Attr)) |
1998 | HaveError |= error(L: Loc, Msg: "this attribute does not apply to parameters" ); |
1999 | if (!IsParam && !Attribute::canUseAsRetAttr(Kind: Attr)) |
2000 | HaveError |= error(L: Loc, Msg: "this attribute does not apply to return values" ); |
2001 | } |
2002 | } |
2003 | |
2004 | static unsigned parseOptionalLinkageAux(lltok::Kind Kind, bool &HasLinkage) { |
2005 | HasLinkage = true; |
2006 | switch (Kind) { |
2007 | default: |
2008 | HasLinkage = false; |
2009 | return GlobalValue::ExternalLinkage; |
2010 | case lltok::kw_private: |
2011 | return GlobalValue::PrivateLinkage; |
2012 | case lltok::kw_internal: |
2013 | return GlobalValue::InternalLinkage; |
2014 | case lltok::kw_weak: |
2015 | return GlobalValue::WeakAnyLinkage; |
2016 | case lltok::kw_weak_odr: |
2017 | return GlobalValue::WeakODRLinkage; |
2018 | case lltok::kw_linkonce: |
2019 | return GlobalValue::LinkOnceAnyLinkage; |
2020 | case lltok::kw_linkonce_odr: |
2021 | return GlobalValue::LinkOnceODRLinkage; |
2022 | case lltok::kw_available_externally: |
2023 | return GlobalValue::AvailableExternallyLinkage; |
2024 | case lltok::kw_appending: |
2025 | return GlobalValue::AppendingLinkage; |
2026 | case lltok::kw_common: |
2027 | return GlobalValue::CommonLinkage; |
2028 | case lltok::kw_extern_weak: |
2029 | return GlobalValue::ExternalWeakLinkage; |
2030 | case lltok::kw_external: |
2031 | return GlobalValue::ExternalLinkage; |
2032 | } |
2033 | } |
2034 | |
2035 | /// parseOptionalLinkage |
2036 | /// ::= /*empty*/ |
2037 | /// ::= 'private' |
2038 | /// ::= 'internal' |
2039 | /// ::= 'weak' |
2040 | /// ::= 'weak_odr' |
2041 | /// ::= 'linkonce' |
2042 | /// ::= 'linkonce_odr' |
2043 | /// ::= 'available_externally' |
2044 | /// ::= 'appending' |
2045 | /// ::= 'common' |
2046 | /// ::= 'extern_weak' |
2047 | /// ::= 'external' |
2048 | bool LLParser::parseOptionalLinkage(unsigned &Res, bool &HasLinkage, |
2049 | unsigned &Visibility, |
2050 | unsigned &DLLStorageClass, bool &DSOLocal) { |
2051 | Res = parseOptionalLinkageAux(Kind: Lex.getKind(), HasLinkage); |
2052 | if (HasLinkage) |
2053 | Lex.Lex(); |
2054 | parseOptionalDSOLocal(DSOLocal); |
2055 | parseOptionalVisibility(Res&: Visibility); |
2056 | parseOptionalDLLStorageClass(Res&: DLLStorageClass); |
2057 | |
2058 | if (DSOLocal && DLLStorageClass == GlobalValue::DLLImportStorageClass) { |
2059 | return error(L: Lex.getLoc(), Msg: "dso_location and DLL-StorageClass mismatch" ); |
2060 | } |
2061 | |
2062 | return false; |
2063 | } |
2064 | |
2065 | void LLParser::parseOptionalDSOLocal(bool &DSOLocal) { |
2066 | switch (Lex.getKind()) { |
2067 | default: |
2068 | DSOLocal = false; |
2069 | break; |
2070 | case lltok::kw_dso_local: |
2071 | DSOLocal = true; |
2072 | Lex.Lex(); |
2073 | break; |
2074 | case lltok::kw_dso_preemptable: |
2075 | DSOLocal = false; |
2076 | Lex.Lex(); |
2077 | break; |
2078 | } |
2079 | } |
2080 | |
2081 | /// parseOptionalVisibility |
2082 | /// ::= /*empty*/ |
2083 | /// ::= 'default' |
2084 | /// ::= 'hidden' |
2085 | /// ::= 'protected' |
2086 | /// |
2087 | void LLParser::parseOptionalVisibility(unsigned &Res) { |
2088 | switch (Lex.getKind()) { |
2089 | default: |
2090 | Res = GlobalValue::DefaultVisibility; |
2091 | return; |
2092 | case lltok::kw_default: |
2093 | Res = GlobalValue::DefaultVisibility; |
2094 | break; |
2095 | case lltok::kw_hidden: |
2096 | Res = GlobalValue::HiddenVisibility; |
2097 | break; |
2098 | case lltok::kw_protected: |
2099 | Res = GlobalValue::ProtectedVisibility; |
2100 | break; |
2101 | } |
2102 | Lex.Lex(); |
2103 | } |
2104 | |
2105 | bool LLParser::parseOptionalImportType(lltok::Kind Kind, |
2106 | GlobalValueSummary::ImportKind &Res) { |
2107 | switch (Kind) { |
2108 | default: |
2109 | return tokError(Msg: "unknown import kind. Expect definition or declaration." ); |
2110 | case lltok::kw_definition: |
2111 | Res = GlobalValueSummary::Definition; |
2112 | return false; |
2113 | case lltok::kw_declaration: |
2114 | Res = GlobalValueSummary::Declaration; |
2115 | return false; |
2116 | } |
2117 | } |
2118 | |
2119 | /// parseOptionalDLLStorageClass |
2120 | /// ::= /*empty*/ |
2121 | /// ::= 'dllimport' |
2122 | /// ::= 'dllexport' |
2123 | /// |
2124 | void LLParser::parseOptionalDLLStorageClass(unsigned &Res) { |
2125 | switch (Lex.getKind()) { |
2126 | default: |
2127 | Res = GlobalValue::DefaultStorageClass; |
2128 | return; |
2129 | case lltok::kw_dllimport: |
2130 | Res = GlobalValue::DLLImportStorageClass; |
2131 | break; |
2132 | case lltok::kw_dllexport: |
2133 | Res = GlobalValue::DLLExportStorageClass; |
2134 | break; |
2135 | } |
2136 | Lex.Lex(); |
2137 | } |
2138 | |
2139 | /// parseOptionalCallingConv |
2140 | /// ::= /*empty*/ |
2141 | /// ::= 'ccc' |
2142 | /// ::= 'fastcc' |
2143 | /// ::= 'intel_ocl_bicc' |
2144 | /// ::= 'coldcc' |
2145 | /// ::= 'cfguard_checkcc' |
2146 | /// ::= 'x86_stdcallcc' |
2147 | /// ::= 'x86_fastcallcc' |
2148 | /// ::= 'x86_thiscallcc' |
2149 | /// ::= 'x86_vectorcallcc' |
2150 | /// ::= 'arm_apcscc' |
2151 | /// ::= 'arm_aapcscc' |
2152 | /// ::= 'arm_aapcs_vfpcc' |
2153 | /// ::= 'aarch64_vector_pcs' |
2154 | /// ::= 'aarch64_sve_vector_pcs' |
2155 | /// ::= 'aarch64_sme_preservemost_from_x0' |
2156 | /// ::= 'aarch64_sme_preservemost_from_x2' |
2157 | /// ::= 'msp430_intrcc' |
2158 | /// ::= 'avr_intrcc' |
2159 | /// ::= 'avr_signalcc' |
2160 | /// ::= 'ptx_kernel' |
2161 | /// ::= 'ptx_device' |
2162 | /// ::= 'spir_func' |
2163 | /// ::= 'spir_kernel' |
2164 | /// ::= 'x86_64_sysvcc' |
2165 | /// ::= 'win64cc' |
2166 | /// ::= 'anyregcc' |
2167 | /// ::= 'preserve_mostcc' |
2168 | /// ::= 'preserve_allcc' |
2169 | /// ::= 'preserve_nonecc' |
2170 | /// ::= 'ghccc' |
2171 | /// ::= 'swiftcc' |
2172 | /// ::= 'swifttailcc' |
2173 | /// ::= 'x86_intrcc' |
2174 | /// ::= 'hhvmcc' |
2175 | /// ::= 'hhvm_ccc' |
2176 | /// ::= 'cxx_fast_tlscc' |
2177 | /// ::= 'amdgpu_vs' |
2178 | /// ::= 'amdgpu_ls' |
2179 | /// ::= 'amdgpu_hs' |
2180 | /// ::= 'amdgpu_es' |
2181 | /// ::= 'amdgpu_gs' |
2182 | /// ::= 'amdgpu_ps' |
2183 | /// ::= 'amdgpu_cs' |
2184 | /// ::= 'amdgpu_cs_chain' |
2185 | /// ::= 'amdgpu_cs_chain_preserve' |
2186 | /// ::= 'amdgpu_kernel' |
2187 | /// ::= 'tailcc' |
2188 | /// ::= 'm68k_rtdcc' |
2189 | /// ::= 'graalcc' |
2190 | /// ::= 'riscv_vector_cc' |
2191 | /// ::= 'cc' UINT |
2192 | /// |
2193 | bool LLParser::parseOptionalCallingConv(unsigned &CC) { |
2194 | switch (Lex.getKind()) { |
2195 | default: CC = CallingConv::C; return false; |
2196 | case lltok::kw_ccc: CC = CallingConv::C; break; |
2197 | case lltok::kw_fastcc: CC = CallingConv::Fast; break; |
2198 | case lltok::kw_coldcc: CC = CallingConv::Cold; break; |
2199 | case lltok::kw_cfguard_checkcc: CC = CallingConv::CFGuard_Check; break; |
2200 | case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break; |
2201 | case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break; |
2202 | case lltok::kw_x86_regcallcc: CC = CallingConv::X86_RegCall; break; |
2203 | case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break; |
2204 | case lltok::kw_x86_vectorcallcc:CC = CallingConv::X86_VectorCall; break; |
2205 | case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break; |
2206 | case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break; |
2207 | case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break; |
2208 | case lltok::kw_aarch64_vector_pcs:CC = CallingConv::AArch64_VectorCall; break; |
2209 | case lltok::kw_aarch64_sve_vector_pcs: |
2210 | CC = CallingConv::AArch64_SVE_VectorCall; |
2211 | break; |
2212 | case lltok::kw_aarch64_sme_preservemost_from_x0: |
2213 | CC = CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0; |
2214 | break; |
2215 | case lltok::kw_aarch64_sme_preservemost_from_x2: |
2216 | CC = CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2; |
2217 | break; |
2218 | case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break; |
2219 | case lltok::kw_avr_intrcc: CC = CallingConv::AVR_INTR; break; |
2220 | case lltok::kw_avr_signalcc: CC = CallingConv::AVR_SIGNAL; break; |
2221 | case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break; |
2222 | case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break; |
2223 | case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break; |
2224 | case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break; |
2225 | case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break; |
2226 | case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break; |
2227 | case lltok::kw_win64cc: CC = CallingConv::Win64; break; |
2228 | case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break; |
2229 | case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break; |
2230 | case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break; |
2231 | case lltok::kw_preserve_nonecc:CC = CallingConv::PreserveNone; break; |
2232 | case lltok::kw_ghccc: CC = CallingConv::GHC; break; |
2233 | case lltok::kw_swiftcc: CC = CallingConv::Swift; break; |
2234 | case lltok::kw_swifttailcc: CC = CallingConv::SwiftTail; break; |
2235 | case lltok::kw_x86_intrcc: CC = CallingConv::X86_INTR; break; |
2236 | case lltok::kw_hhvmcc: |
2237 | CC = CallingConv::DUMMY_HHVM; |
2238 | break; |
2239 | case lltok::kw_hhvm_ccc: |
2240 | CC = CallingConv::DUMMY_HHVM_C; |
2241 | break; |
2242 | case lltok::kw_cxx_fast_tlscc: CC = CallingConv::CXX_FAST_TLS; break; |
2243 | case lltok::kw_amdgpu_vs: CC = CallingConv::AMDGPU_VS; break; |
2244 | case lltok::kw_amdgpu_gfx: CC = CallingConv::AMDGPU_Gfx; break; |
2245 | case lltok::kw_amdgpu_ls: CC = CallingConv::AMDGPU_LS; break; |
2246 | case lltok::kw_amdgpu_hs: CC = CallingConv::AMDGPU_HS; break; |
2247 | case lltok::kw_amdgpu_es: CC = CallingConv::AMDGPU_ES; break; |
2248 | case lltok::kw_amdgpu_gs: CC = CallingConv::AMDGPU_GS; break; |
2249 | case lltok::kw_amdgpu_ps: CC = CallingConv::AMDGPU_PS; break; |
2250 | case lltok::kw_amdgpu_cs: CC = CallingConv::AMDGPU_CS; break; |
2251 | case lltok::kw_amdgpu_cs_chain: |
2252 | CC = CallingConv::AMDGPU_CS_Chain; |
2253 | break; |
2254 | case lltok::kw_amdgpu_cs_chain_preserve: |
2255 | CC = CallingConv::AMDGPU_CS_ChainPreserve; |
2256 | break; |
2257 | case lltok::kw_amdgpu_kernel: CC = CallingConv::AMDGPU_KERNEL; break; |
2258 | case lltok::kw_tailcc: CC = CallingConv::Tail; break; |
2259 | case lltok::kw_m68k_rtdcc: CC = CallingConv::M68k_RTD; break; |
2260 | case lltok::kw_graalcc: CC = CallingConv::GRAAL; break; |
2261 | case lltok::kw_riscv_vector_cc: |
2262 | CC = CallingConv::RISCV_VectorCall; |
2263 | break; |
2264 | case lltok::kw_cc: { |
2265 | Lex.Lex(); |
2266 | return parseUInt32(Val&: CC); |
2267 | } |
2268 | } |
2269 | |
2270 | Lex.Lex(); |
2271 | return false; |
2272 | } |
2273 | |
2274 | /// parseMetadataAttachment |
2275 | /// ::= !dbg !42 |
2276 | bool LLParser::parseMetadataAttachment(unsigned &Kind, MDNode *&MD) { |
2277 | assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata attachment" ); |
2278 | |
2279 | std::string Name = Lex.getStrVal(); |
2280 | Kind = M->getMDKindID(Name); |
2281 | Lex.Lex(); |
2282 | |
2283 | return parseMDNode(N&: MD); |
2284 | } |
2285 | |
2286 | /// parseInstructionMetadata |
2287 | /// ::= !dbg !42 (',' !dbg !57)* |
2288 | bool LLParser::parseInstructionMetadata(Instruction &Inst) { |
2289 | do { |
2290 | if (Lex.getKind() != lltok::MetadataVar) |
2291 | return tokError(Msg: "expected metadata after comma" ); |
2292 | |
2293 | unsigned MDK; |
2294 | MDNode *N; |
2295 | if (parseMetadataAttachment(Kind&: MDK, MD&: N)) |
2296 | return true; |
2297 | |
2298 | if (MDK == LLVMContext::MD_DIAssignID) |
2299 | TempDIAssignIDAttachments[N].push_back(Elt: &Inst); |
2300 | else |
2301 | Inst.setMetadata(KindID: MDK, Node: N); |
2302 | |
2303 | if (MDK == LLVMContext::MD_tbaa) |
2304 | InstsWithTBAATag.push_back(Elt: &Inst); |
2305 | |
2306 | // If this is the end of the list, we're done. |
2307 | } while (EatIfPresent(T: lltok::comma)); |
2308 | return false; |
2309 | } |
2310 | |
2311 | /// parseGlobalObjectMetadataAttachment |
2312 | /// ::= !dbg !57 |
2313 | bool LLParser::parseGlobalObjectMetadataAttachment(GlobalObject &GO) { |
2314 | unsigned MDK; |
2315 | MDNode *N; |
2316 | if (parseMetadataAttachment(Kind&: MDK, MD&: N)) |
2317 | return true; |
2318 | |
2319 | GO.addMetadata(KindID: MDK, MD&: *N); |
2320 | return false; |
2321 | } |
2322 | |
2323 | /// parseOptionalFunctionMetadata |
2324 | /// ::= (!dbg !57)* |
2325 | bool LLParser::parseOptionalFunctionMetadata(Function &F) { |
2326 | while (Lex.getKind() == lltok::MetadataVar) |
2327 | if (parseGlobalObjectMetadataAttachment(GO&: F)) |
2328 | return true; |
2329 | return false; |
2330 | } |
2331 | |
2332 | /// parseOptionalAlignment |
2333 | /// ::= /* empty */ |
2334 | /// ::= 'align' 4 |
2335 | bool LLParser::parseOptionalAlignment(MaybeAlign &Alignment, bool AllowParens) { |
2336 | Alignment = std::nullopt; |
2337 | if (!EatIfPresent(T: lltok::kw_align)) |
2338 | return false; |
2339 | LocTy AlignLoc = Lex.getLoc(); |
2340 | uint64_t Value = 0; |
2341 | |
2342 | LocTy ParenLoc = Lex.getLoc(); |
2343 | bool HaveParens = false; |
2344 | if (AllowParens) { |
2345 | if (EatIfPresent(T: lltok::lparen)) |
2346 | HaveParens = true; |
2347 | } |
2348 | |
2349 | if (parseUInt64(Val&: Value)) |
2350 | return true; |
2351 | |
2352 | if (HaveParens && !EatIfPresent(T: lltok::rparen)) |
2353 | return error(L: ParenLoc, Msg: "expected ')'" ); |
2354 | |
2355 | if (!isPowerOf2_64(Value)) |
2356 | return error(L: AlignLoc, Msg: "alignment is not a power of two" ); |
2357 | if (Value > Value::MaximumAlignment) |
2358 | return error(L: AlignLoc, Msg: "huge alignments are not supported yet" ); |
2359 | Alignment = Align(Value); |
2360 | return false; |
2361 | } |
2362 | |
2363 | /// parseOptionalCodeModel |
2364 | /// ::= /* empty */ |
2365 | /// ::= 'code_model' "large" |
2366 | bool LLParser::parseOptionalCodeModel(CodeModel::Model &model) { |
2367 | Lex.Lex(); |
2368 | auto StrVal = Lex.getStrVal(); |
2369 | auto ErrMsg = "expected global code model string" ; |
2370 | if (StrVal == "tiny" ) |
2371 | model = CodeModel::Tiny; |
2372 | else if (StrVal == "small" ) |
2373 | model = CodeModel::Small; |
2374 | else if (StrVal == "kernel" ) |
2375 | model = CodeModel::Kernel; |
2376 | else if (StrVal == "medium" ) |
2377 | model = CodeModel::Medium; |
2378 | else if (StrVal == "large" ) |
2379 | model = CodeModel::Large; |
2380 | else |
2381 | return tokError(Msg: ErrMsg); |
2382 | if (parseToken(T: lltok::StringConstant, ErrMsg)) |
2383 | return true; |
2384 | return false; |
2385 | } |
2386 | |
2387 | /// parseOptionalDerefAttrBytes |
2388 | /// ::= /* empty */ |
2389 | /// ::= AttrKind '(' 4 ')' |
2390 | /// |
2391 | /// where AttrKind is either 'dereferenceable' or 'dereferenceable_or_null'. |
2392 | bool LLParser::parseOptionalDerefAttrBytes(lltok::Kind AttrKind, |
2393 | uint64_t &Bytes) { |
2394 | assert((AttrKind == lltok::kw_dereferenceable || |
2395 | AttrKind == lltok::kw_dereferenceable_or_null) && |
2396 | "contract!" ); |
2397 | |
2398 | Bytes = 0; |
2399 | if (!EatIfPresent(T: AttrKind)) |
2400 | return false; |
2401 | LocTy ParenLoc = Lex.getLoc(); |
2402 | if (!EatIfPresent(T: lltok::lparen)) |
2403 | return error(L: ParenLoc, Msg: "expected '('" ); |
2404 | LocTy DerefLoc = Lex.getLoc(); |
2405 | if (parseUInt64(Val&: Bytes)) |
2406 | return true; |
2407 | ParenLoc = Lex.getLoc(); |
2408 | if (!EatIfPresent(T: lltok::rparen)) |
2409 | return error(L: ParenLoc, Msg: "expected ')'" ); |
2410 | if (!Bytes) |
2411 | return error(L: DerefLoc, Msg: "dereferenceable bytes must be non-zero" ); |
2412 | return false; |
2413 | } |
2414 | |
2415 | bool LLParser::parseOptionalUWTableKind(UWTableKind &Kind) { |
2416 | Lex.Lex(); |
2417 | Kind = UWTableKind::Default; |
2418 | if (!EatIfPresent(T: lltok::lparen)) |
2419 | return false; |
2420 | LocTy KindLoc = Lex.getLoc(); |
2421 | if (Lex.getKind() == lltok::kw_sync) |
2422 | Kind = UWTableKind::Sync; |
2423 | else if (Lex.getKind() == lltok::kw_async) |
2424 | Kind = UWTableKind::Async; |
2425 | else |
2426 | return error(L: KindLoc, Msg: "expected unwind table kind" ); |
2427 | Lex.Lex(); |
2428 | return parseToken(T: lltok::rparen, ErrMsg: "expected ')'" ); |
2429 | } |
2430 | |
2431 | bool LLParser::parseAllocKind(AllocFnKind &Kind) { |
2432 | Lex.Lex(); |
2433 | LocTy ParenLoc = Lex.getLoc(); |
2434 | if (!EatIfPresent(T: lltok::lparen)) |
2435 | return error(L: ParenLoc, Msg: "expected '('" ); |
2436 | LocTy KindLoc = Lex.getLoc(); |
2437 | std::string Arg; |
2438 | if (parseStringConstant(Result&: Arg)) |
2439 | return error(L: KindLoc, Msg: "expected allockind value" ); |
2440 | for (StringRef A : llvm::split(Str: Arg, Separator: "," )) { |
2441 | if (A == "alloc" ) { |
2442 | Kind |= AllocFnKind::Alloc; |
2443 | } else if (A == "realloc" ) { |
2444 | Kind |= AllocFnKind::Realloc; |
2445 | } else if (A == "free" ) { |
2446 | Kind |= AllocFnKind::Free; |
2447 | } else if (A == "uninitialized" ) { |
2448 | Kind |= AllocFnKind::Uninitialized; |
2449 | } else if (A == "zeroed" ) { |
2450 | Kind |= AllocFnKind::Zeroed; |
2451 | } else if (A == "aligned" ) { |
2452 | Kind |= AllocFnKind::Aligned; |
2453 | } else { |
2454 | return error(L: KindLoc, Msg: Twine("unknown allockind " ) + A); |
2455 | } |
2456 | } |
2457 | ParenLoc = Lex.getLoc(); |
2458 | if (!EatIfPresent(T: lltok::rparen)) |
2459 | return error(L: ParenLoc, Msg: "expected ')'" ); |
2460 | if (Kind == AllocFnKind::Unknown) |
2461 | return error(L: KindLoc, Msg: "expected allockind value" ); |
2462 | return false; |
2463 | } |
2464 | |
2465 | static std::optional<MemoryEffects::Location> keywordToLoc(lltok::Kind Tok) { |
2466 | switch (Tok) { |
2467 | case lltok::kw_argmem: |
2468 | return IRMemLocation::ArgMem; |
2469 | case lltok::kw_inaccessiblemem: |
2470 | return IRMemLocation::InaccessibleMem; |
2471 | default: |
2472 | return std::nullopt; |
2473 | } |
2474 | } |
2475 | |
2476 | static std::optional<ModRefInfo> keywordToModRef(lltok::Kind Tok) { |
2477 | switch (Tok) { |
2478 | case lltok::kw_none: |
2479 | return ModRefInfo::NoModRef; |
2480 | case lltok::kw_read: |
2481 | return ModRefInfo::Ref; |
2482 | case lltok::kw_write: |
2483 | return ModRefInfo::Mod; |
2484 | case lltok::kw_readwrite: |
2485 | return ModRefInfo::ModRef; |
2486 | default: |
2487 | return std::nullopt; |
2488 | } |
2489 | } |
2490 | |
2491 | std::optional<MemoryEffects> LLParser::parseMemoryAttr() { |
2492 | MemoryEffects ME = MemoryEffects::none(); |
2493 | |
2494 | // We use syntax like memory(argmem: read), so the colon should not be |
2495 | // interpreted as a label terminator. |
2496 | Lex.setIgnoreColonInIdentifiers(true); |
2497 | auto _ = make_scope_exit(F: [&] { Lex.setIgnoreColonInIdentifiers(false); }); |
2498 | |
2499 | Lex.Lex(); |
2500 | if (!EatIfPresent(T: lltok::lparen)) { |
2501 | tokError(Msg: "expected '('" ); |
2502 | return std::nullopt; |
2503 | } |
2504 | |
2505 | bool SeenLoc = false; |
2506 | do { |
2507 | std::optional<IRMemLocation> Loc = keywordToLoc(Tok: Lex.getKind()); |
2508 | if (Loc) { |
2509 | Lex.Lex(); |
2510 | if (!EatIfPresent(T: lltok::colon)) { |
2511 | tokError(Msg: "expected ':' after location" ); |
2512 | return std::nullopt; |
2513 | } |
2514 | } |
2515 | |
2516 | std::optional<ModRefInfo> MR = keywordToModRef(Tok: Lex.getKind()); |
2517 | if (!MR) { |
2518 | if (!Loc) |
2519 | tokError(Msg: "expected memory location (argmem, inaccessiblemem) " |
2520 | "or access kind (none, read, write, readwrite)" ); |
2521 | else |
2522 | tokError(Msg: "expected access kind (none, read, write, readwrite)" ); |
2523 | return std::nullopt; |
2524 | } |
2525 | |
2526 | Lex.Lex(); |
2527 | if (Loc) { |
2528 | SeenLoc = true; |
2529 | ME = ME.getWithModRef(Loc: *Loc, MR: *MR); |
2530 | } else { |
2531 | if (SeenLoc) { |
2532 | tokError(Msg: "default access kind must be specified first" ); |
2533 | return std::nullopt; |
2534 | } |
2535 | ME = MemoryEffects(*MR); |
2536 | } |
2537 | |
2538 | if (EatIfPresent(T: lltok::rparen)) |
2539 | return ME; |
2540 | } while (EatIfPresent(T: lltok::comma)); |
2541 | |
2542 | tokError(Msg: "unterminated memory attribute" ); |
2543 | return std::nullopt; |
2544 | } |
2545 | |
2546 | static unsigned keywordToFPClassTest(lltok::Kind Tok) { |
2547 | switch (Tok) { |
2548 | case lltok::kw_all: |
2549 | return fcAllFlags; |
2550 | case lltok::kw_nan: |
2551 | return fcNan; |
2552 | case lltok::kw_snan: |
2553 | return fcSNan; |
2554 | case lltok::kw_qnan: |
2555 | return fcQNan; |
2556 | case lltok::kw_inf: |
2557 | return fcInf; |
2558 | case lltok::kw_ninf: |
2559 | return fcNegInf; |
2560 | case lltok::kw_pinf: |
2561 | return fcPosInf; |
2562 | case lltok::kw_norm: |
2563 | return fcNormal; |
2564 | case lltok::kw_nnorm: |
2565 | return fcNegNormal; |
2566 | case lltok::kw_pnorm: |
2567 | return fcPosNormal; |
2568 | case lltok::kw_sub: |
2569 | return fcSubnormal; |
2570 | case lltok::kw_nsub: |
2571 | return fcNegSubnormal; |
2572 | case lltok::kw_psub: |
2573 | return fcPosSubnormal; |
2574 | case lltok::kw_zero: |
2575 | return fcZero; |
2576 | case lltok::kw_nzero: |
2577 | return fcNegZero; |
2578 | case lltok::kw_pzero: |
2579 | return fcPosZero; |
2580 | default: |
2581 | return 0; |
2582 | } |
2583 | } |
2584 | |
2585 | unsigned LLParser::parseNoFPClassAttr() { |
2586 | unsigned Mask = fcNone; |
2587 | |
2588 | Lex.Lex(); |
2589 | if (!EatIfPresent(T: lltok::lparen)) { |
2590 | tokError(Msg: "expected '('" ); |
2591 | return 0; |
2592 | } |
2593 | |
2594 | do { |
2595 | uint64_t Value = 0; |
2596 | unsigned TestMask = keywordToFPClassTest(Tok: Lex.getKind()); |
2597 | if (TestMask != 0) { |
2598 | Mask |= TestMask; |
2599 | // TODO: Disallow overlapping masks to avoid copy paste errors |
2600 | } else if (Mask == 0 && Lex.getKind() == lltok::APSInt && |
2601 | !parseUInt64(Val&: Value)) { |
2602 | if (Value == 0 || (Value & ~static_cast<unsigned>(fcAllFlags)) != 0) { |
2603 | error(L: Lex.getLoc(), Msg: "invalid mask value for 'nofpclass'" ); |
2604 | return 0; |
2605 | } |
2606 | |
2607 | if (!EatIfPresent(T: lltok::rparen)) { |
2608 | error(L: Lex.getLoc(), Msg: "expected ')'" ); |
2609 | return 0; |
2610 | } |
2611 | |
2612 | return Value; |
2613 | } else { |
2614 | error(L: Lex.getLoc(), Msg: "expected nofpclass test mask" ); |
2615 | return 0; |
2616 | } |
2617 | |
2618 | Lex.Lex(); |
2619 | if (EatIfPresent(T: lltok::rparen)) |
2620 | return Mask; |
2621 | } while (1); |
2622 | |
2623 | llvm_unreachable("unterminated nofpclass attribute" ); |
2624 | } |
2625 | |
2626 | /// parseOptionalCommaAlign |
2627 | /// ::= |
2628 | /// ::= ',' align 4 |
2629 | /// |
2630 | /// This returns with AteExtraComma set to true if it ate an excess comma at the |
2631 | /// end. |
2632 | bool LLParser::parseOptionalCommaAlign(MaybeAlign &Alignment, |
2633 | bool &) { |
2634 | AteExtraComma = false; |
2635 | while (EatIfPresent(T: lltok::comma)) { |
2636 | // Metadata at the end is an early exit. |
2637 | if (Lex.getKind() == lltok::MetadataVar) { |
2638 | AteExtraComma = true; |
2639 | return false; |
2640 | } |
2641 | |
2642 | if (Lex.getKind() != lltok::kw_align) |
2643 | return error(L: Lex.getLoc(), Msg: "expected metadata or 'align'" ); |
2644 | |
2645 | if (parseOptionalAlignment(Alignment)) |
2646 | return true; |
2647 | } |
2648 | |
2649 | return false; |
2650 | } |
2651 | |
2652 | /// parseOptionalCommaAddrSpace |
2653 | /// ::= |
2654 | /// ::= ',' addrspace(1) |
2655 | /// |
2656 | /// This returns with AteExtraComma set to true if it ate an excess comma at the |
2657 | /// end. |
2658 | bool LLParser::parseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc, |
2659 | bool &) { |
2660 | AteExtraComma = false; |
2661 | while (EatIfPresent(T: lltok::comma)) { |
2662 | // Metadata at the end is an early exit. |
2663 | if (Lex.getKind() == lltok::MetadataVar) { |
2664 | AteExtraComma = true; |
2665 | return false; |
2666 | } |
2667 | |
2668 | Loc = Lex.getLoc(); |
2669 | if (Lex.getKind() != lltok::kw_addrspace) |
2670 | return error(L: Lex.getLoc(), Msg: "expected metadata or 'addrspace'" ); |
2671 | |
2672 | if (parseOptionalAddrSpace(AddrSpace)) |
2673 | return true; |
2674 | } |
2675 | |
2676 | return false; |
2677 | } |
2678 | |
2679 | bool LLParser::parseAllocSizeArguments(unsigned &BaseSizeArg, |
2680 | std::optional<unsigned> &HowManyArg) { |
2681 | Lex.Lex(); |
2682 | |
2683 | auto StartParen = Lex.getLoc(); |
2684 | if (!EatIfPresent(T: lltok::lparen)) |
2685 | return error(L: StartParen, Msg: "expected '('" ); |
2686 | |
2687 | if (parseUInt32(Val&: BaseSizeArg)) |
2688 | return true; |
2689 | |
2690 | if (EatIfPresent(T: lltok::comma)) { |
2691 | auto HowManyAt = Lex.getLoc(); |
2692 | unsigned HowMany; |
2693 | if (parseUInt32(Val&: HowMany)) |
2694 | return true; |
2695 | if (HowMany == BaseSizeArg) |
2696 | return error(L: HowManyAt, |
2697 | Msg: "'allocsize' indices can't refer to the same parameter" ); |
2698 | HowManyArg = HowMany; |
2699 | } else |
2700 | HowManyArg = std::nullopt; |
2701 | |
2702 | auto EndParen = Lex.getLoc(); |
2703 | if (!EatIfPresent(T: lltok::rparen)) |
2704 | return error(L: EndParen, Msg: "expected ')'" ); |
2705 | return false; |
2706 | } |
2707 | |
2708 | bool LLParser::parseVScaleRangeArguments(unsigned &MinValue, |
2709 | unsigned &MaxValue) { |
2710 | Lex.Lex(); |
2711 | |
2712 | auto StartParen = Lex.getLoc(); |
2713 | if (!EatIfPresent(T: lltok::lparen)) |
2714 | return error(L: StartParen, Msg: "expected '('" ); |
2715 | |
2716 | if (parseUInt32(Val&: MinValue)) |
2717 | return true; |
2718 | |
2719 | if (EatIfPresent(T: lltok::comma)) { |
2720 | if (parseUInt32(Val&: MaxValue)) |
2721 | return true; |
2722 | } else |
2723 | MaxValue = MinValue; |
2724 | |
2725 | auto EndParen = Lex.getLoc(); |
2726 | if (!EatIfPresent(T: lltok::rparen)) |
2727 | return error(L: EndParen, Msg: "expected ')'" ); |
2728 | return false; |
2729 | } |
2730 | |
2731 | /// parseScopeAndOrdering |
2732 | /// if isAtomic: ::= SyncScope? AtomicOrdering |
2733 | /// else: ::= |
2734 | /// |
2735 | /// This sets Scope and Ordering to the parsed values. |
2736 | bool LLParser::parseScopeAndOrdering(bool IsAtomic, SyncScope::ID &SSID, |
2737 | AtomicOrdering &Ordering) { |
2738 | if (!IsAtomic) |
2739 | return false; |
2740 | |
2741 | return parseScope(SSID) || parseOrdering(Ordering); |
2742 | } |
2743 | |
2744 | /// parseScope |
2745 | /// ::= syncscope("singlethread" | "<target scope>")? |
2746 | /// |
2747 | /// This sets synchronization scope ID to the ID of the parsed value. |
2748 | bool LLParser::parseScope(SyncScope::ID &SSID) { |
2749 | SSID = SyncScope::System; |
2750 | if (EatIfPresent(T: lltok::kw_syncscope)) { |
2751 | auto StartParenAt = Lex.getLoc(); |
2752 | if (!EatIfPresent(T: lltok::lparen)) |
2753 | return error(L: StartParenAt, Msg: "Expected '(' in syncscope" ); |
2754 | |
2755 | std::string SSN; |
2756 | auto SSNAt = Lex.getLoc(); |
2757 | if (parseStringConstant(Result&: SSN)) |
2758 | return error(L: SSNAt, Msg: "Expected synchronization scope name" ); |
2759 | |
2760 | auto EndParenAt = Lex.getLoc(); |
2761 | if (!EatIfPresent(T: lltok::rparen)) |
2762 | return error(L: EndParenAt, Msg: "Expected ')' in syncscope" ); |
2763 | |
2764 | SSID = Context.getOrInsertSyncScopeID(SSN); |
2765 | } |
2766 | |
2767 | return false; |
2768 | } |
2769 | |
2770 | /// parseOrdering |
2771 | /// ::= AtomicOrdering |
2772 | /// |
2773 | /// This sets Ordering to the parsed value. |
2774 | bool LLParser::parseOrdering(AtomicOrdering &Ordering) { |
2775 | switch (Lex.getKind()) { |
2776 | default: |
2777 | return tokError(Msg: "Expected ordering on atomic instruction" ); |
2778 | case lltok::kw_unordered: Ordering = AtomicOrdering::Unordered; break; |
2779 | case lltok::kw_monotonic: Ordering = AtomicOrdering::Monotonic; break; |
2780 | // Not specified yet: |
2781 | // case lltok::kw_consume: Ordering = AtomicOrdering::Consume; break; |
2782 | case lltok::kw_acquire: Ordering = AtomicOrdering::Acquire; break; |
2783 | case lltok::kw_release: Ordering = AtomicOrdering::Release; break; |
2784 | case lltok::kw_acq_rel: Ordering = AtomicOrdering::AcquireRelease; break; |
2785 | case lltok::kw_seq_cst: |
2786 | Ordering = AtomicOrdering::SequentiallyConsistent; |
2787 | break; |
2788 | } |
2789 | Lex.Lex(); |
2790 | return false; |
2791 | } |
2792 | |
2793 | /// parseOptionalStackAlignment |
2794 | /// ::= /* empty */ |
2795 | /// ::= 'alignstack' '(' 4 ')' |
2796 | bool LLParser::parseOptionalStackAlignment(unsigned &Alignment) { |
2797 | Alignment = 0; |
2798 | if (!EatIfPresent(T: lltok::kw_alignstack)) |
2799 | return false; |
2800 | LocTy ParenLoc = Lex.getLoc(); |
2801 | if (!EatIfPresent(T: lltok::lparen)) |
2802 | return error(L: ParenLoc, Msg: "expected '('" ); |
2803 | LocTy AlignLoc = Lex.getLoc(); |
2804 | if (parseUInt32(Val&: Alignment)) |
2805 | return true; |
2806 | ParenLoc = Lex.getLoc(); |
2807 | if (!EatIfPresent(T: lltok::rparen)) |
2808 | return error(L: ParenLoc, Msg: "expected ')'" ); |
2809 | if (!isPowerOf2_32(Value: Alignment)) |
2810 | return error(L: AlignLoc, Msg: "stack alignment is not a power of two" ); |
2811 | return false; |
2812 | } |
2813 | |
2814 | /// parseIndexList - This parses the index list for an insert/extractvalue |
2815 | /// instruction. This sets AteExtraComma in the case where we eat an extra |
2816 | /// comma at the end of the line and find that it is followed by metadata. |
2817 | /// Clients that don't allow metadata can call the version of this function that |
2818 | /// only takes one argument. |
2819 | /// |
2820 | /// parseIndexList |
2821 | /// ::= (',' uint32)+ |
2822 | /// |
2823 | bool LLParser::parseIndexList(SmallVectorImpl<unsigned> &Indices, |
2824 | bool &) { |
2825 | AteExtraComma = false; |
2826 | |
2827 | if (Lex.getKind() != lltok::comma) |
2828 | return tokError(Msg: "expected ',' as start of index list" ); |
2829 | |
2830 | while (EatIfPresent(T: lltok::comma)) { |
2831 | if (Lex.getKind() == lltok::MetadataVar) { |
2832 | if (Indices.empty()) |
2833 | return tokError(Msg: "expected index" ); |
2834 | AteExtraComma = true; |
2835 | return false; |
2836 | } |
2837 | unsigned Idx = 0; |
2838 | if (parseUInt32(Val&: Idx)) |
2839 | return true; |
2840 | Indices.push_back(Elt: Idx); |
2841 | } |
2842 | |
2843 | return false; |
2844 | } |
2845 | |
2846 | //===----------------------------------------------------------------------===// |
2847 | // Type Parsing. |
2848 | //===----------------------------------------------------------------------===// |
2849 | |
2850 | /// parseType - parse a type. |
2851 | bool LLParser::parseType(Type *&Result, const Twine &Msg, bool AllowVoid) { |
2852 | SMLoc TypeLoc = Lex.getLoc(); |
2853 | switch (Lex.getKind()) { |
2854 | default: |
2855 | return tokError(Msg); |
2856 | case lltok::Type: |
2857 | // Type ::= 'float' | 'void' (etc) |
2858 | Result = Lex.getTyVal(); |
2859 | Lex.Lex(); |
2860 | |
2861 | // Handle "ptr" opaque pointer type. |
2862 | // |
2863 | // Type ::= ptr ('addrspace' '(' uint32 ')')? |
2864 | if (Result->isPointerTy()) { |
2865 | unsigned AddrSpace; |
2866 | if (parseOptionalAddrSpace(AddrSpace)) |
2867 | return true; |
2868 | Result = PointerType::get(C&: getContext(), AddressSpace: AddrSpace); |
2869 | |
2870 | // Give a nice error for 'ptr*'. |
2871 | if (Lex.getKind() == lltok::star) |
2872 | return tokError(Msg: "ptr* is invalid - use ptr instead" ); |
2873 | |
2874 | // Fall through to parsing the type suffixes only if this 'ptr' is a |
2875 | // function return. Otherwise, return success, implicitly rejecting other |
2876 | // suffixes. |
2877 | if (Lex.getKind() != lltok::lparen) |
2878 | return false; |
2879 | } |
2880 | break; |
2881 | case lltok::kw_target: { |
2882 | // Type ::= TargetExtType |
2883 | if (parseTargetExtType(Result)) |
2884 | return true; |
2885 | break; |
2886 | } |
2887 | case lltok::lbrace: |
2888 | // Type ::= StructType |
2889 | if (parseAnonStructType(Result, Packed: false)) |
2890 | return true; |
2891 | break; |
2892 | case lltok::lsquare: |
2893 | // Type ::= '[' ... ']' |
2894 | Lex.Lex(); // eat the lsquare. |
2895 | if (parseArrayVectorType(Result, IsVector: false)) |
2896 | return true; |
2897 | break; |
2898 | case lltok::less: // Either vector or packed struct. |
2899 | // Type ::= '<' ... '>' |
2900 | Lex.Lex(); |
2901 | if (Lex.getKind() == lltok::lbrace) { |
2902 | if (parseAnonStructType(Result, Packed: true) || |
2903 | parseToken(T: lltok::greater, ErrMsg: "expected '>' at end of packed struct" )) |
2904 | return true; |
2905 | } else if (parseArrayVectorType(Result, IsVector: true)) |
2906 | return true; |
2907 | break; |
2908 | case lltok::LocalVar: { |
2909 | // Type ::= %foo |
2910 | std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()]; |
2911 | |
2912 | // If the type hasn't been defined yet, create a forward definition and |
2913 | // remember where that forward def'n was seen (in case it never is defined). |
2914 | if (!Entry.first) { |
2915 | Entry.first = StructType::create(Context, Name: Lex.getStrVal()); |
2916 | Entry.second = Lex.getLoc(); |
2917 | } |
2918 | Result = Entry.first; |
2919 | Lex.Lex(); |
2920 | break; |
2921 | } |
2922 | |
2923 | case lltok::LocalVarID: { |
2924 | // Type ::= %4 |
2925 | std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()]; |
2926 | |
2927 | // If the type hasn't been defined yet, create a forward definition and |
2928 | // remember where that forward def'n was seen (in case it never is defined). |
2929 | if (!Entry.first) { |
2930 | Entry.first = StructType::create(Context); |
2931 | Entry.second = Lex.getLoc(); |
2932 | } |
2933 | Result = Entry.first; |
2934 | Lex.Lex(); |
2935 | break; |
2936 | } |
2937 | } |
2938 | |
2939 | // parse the type suffixes. |
2940 | while (true) { |
2941 | switch (Lex.getKind()) { |
2942 | // End of type. |
2943 | default: |
2944 | if (!AllowVoid && Result->isVoidTy()) |
2945 | return error(L: TypeLoc, Msg: "void type only allowed for function results" ); |
2946 | return false; |
2947 | |
2948 | // Type ::= Type '*' |
2949 | case lltok::star: |
2950 | if (Result->isLabelTy()) |
2951 | return tokError(Msg: "basic block pointers are invalid" ); |
2952 | if (Result->isVoidTy()) |
2953 | return tokError(Msg: "pointers to void are invalid - use i8* instead" ); |
2954 | if (!PointerType::isValidElementType(ElemTy: Result)) |
2955 | return tokError(Msg: "pointer to this type is invalid" ); |
2956 | Result = PointerType::getUnqual(ElementType: Result); |
2957 | Lex.Lex(); |
2958 | break; |
2959 | |
2960 | // Type ::= Type 'addrspace' '(' uint32 ')' '*' |
2961 | case lltok::kw_addrspace: { |
2962 | if (Result->isLabelTy()) |
2963 | return tokError(Msg: "basic block pointers are invalid" ); |
2964 | if (Result->isVoidTy()) |
2965 | return tokError(Msg: "pointers to void are invalid; use i8* instead" ); |
2966 | if (!PointerType::isValidElementType(ElemTy: Result)) |
2967 | return tokError(Msg: "pointer to this type is invalid" ); |
2968 | unsigned AddrSpace; |
2969 | if (parseOptionalAddrSpace(AddrSpace) || |
2970 | parseToken(T: lltok::star, ErrMsg: "expected '*' in address space" )) |
2971 | return true; |
2972 | |
2973 | Result = PointerType::get(ElementType: Result, AddressSpace: AddrSpace); |
2974 | break; |
2975 | } |
2976 | |
2977 | /// Types '(' ArgTypeListI ')' OptFuncAttrs |
2978 | case lltok::lparen: |
2979 | if (parseFunctionType(Result)) |
2980 | return true; |
2981 | break; |
2982 | } |
2983 | } |
2984 | } |
2985 | |
2986 | /// parseParameterList |
2987 | /// ::= '(' ')' |
2988 | /// ::= '(' Arg (',' Arg)* ')' |
2989 | /// Arg |
2990 | /// ::= Type OptionalAttributes Value OptionalAttributes |
2991 | bool LLParser::parseParameterList(SmallVectorImpl<ParamInfo> &ArgList, |
2992 | PerFunctionState &PFS, bool IsMustTailCall, |
2993 | bool InVarArgsFunc) { |
2994 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in call" )) |
2995 | return true; |
2996 | |
2997 | while (Lex.getKind() != lltok::rparen) { |
2998 | // If this isn't the first argument, we need a comma. |
2999 | if (!ArgList.empty() && |
3000 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in argument list" )) |
3001 | return true; |
3002 | |
3003 | // parse an ellipsis if this is a musttail call in a variadic function. |
3004 | if (Lex.getKind() == lltok::dotdotdot) { |
3005 | const char *Msg = "unexpected ellipsis in argument list for " ; |
3006 | if (!IsMustTailCall) |
3007 | return tokError(Msg: Twine(Msg) + "non-musttail call" ); |
3008 | if (!InVarArgsFunc) |
3009 | return tokError(Msg: Twine(Msg) + "musttail call in non-varargs function" ); |
3010 | Lex.Lex(); // Lex the '...', it is purely for readability. |
3011 | return parseToken(T: lltok::rparen, ErrMsg: "expected ')' at end of argument list" ); |
3012 | } |
3013 | |
3014 | // parse the argument. |
3015 | LocTy ArgLoc; |
3016 | Type *ArgTy = nullptr; |
3017 | Value *V; |
3018 | if (parseType(Result&: ArgTy, Loc&: ArgLoc)) |
3019 | return true; |
3020 | |
3021 | AttrBuilder ArgAttrs(M->getContext()); |
3022 | |
3023 | if (ArgTy->isMetadataTy()) { |
3024 | if (parseMetadataAsValue(V, PFS)) |
3025 | return true; |
3026 | } else { |
3027 | // Otherwise, handle normal operands. |
3028 | if (parseOptionalParamAttrs(B&: ArgAttrs) || parseValue(Ty: ArgTy, V, PFS)) |
3029 | return true; |
3030 | } |
3031 | ArgList.push_back(Elt: ParamInfo( |
3032 | ArgLoc, V, AttributeSet::get(C&: V->getContext(), B: ArgAttrs))); |
3033 | } |
3034 | |
3035 | if (IsMustTailCall && InVarArgsFunc) |
3036 | return tokError(Msg: "expected '...' at end of argument list for musttail call " |
3037 | "in varargs function" ); |
3038 | |
3039 | Lex.Lex(); // Lex the ')'. |
3040 | return false; |
3041 | } |
3042 | |
3043 | /// parseRequiredTypeAttr |
3044 | /// ::= attrname(<ty>) |
3045 | bool LLParser::parseRequiredTypeAttr(AttrBuilder &B, lltok::Kind AttrToken, |
3046 | Attribute::AttrKind AttrKind) { |
3047 | Type *Ty = nullptr; |
3048 | if (!EatIfPresent(T: AttrToken)) |
3049 | return true; |
3050 | if (!EatIfPresent(T: lltok::lparen)) |
3051 | return error(L: Lex.getLoc(), Msg: "expected '('" ); |
3052 | if (parseType(Result&: Ty)) |
3053 | return true; |
3054 | if (!EatIfPresent(T: lltok::rparen)) |
3055 | return error(L: Lex.getLoc(), Msg: "expected ')'" ); |
3056 | |
3057 | B.addTypeAttr(Kind: AttrKind, Ty); |
3058 | return false; |
3059 | } |
3060 | |
3061 | /// parseRangeAttr |
3062 | /// ::= range(<ty> <n>,<n>) |
3063 | bool LLParser::parseRangeAttr(AttrBuilder &B) { |
3064 | Lex.Lex(); |
3065 | |
3066 | APInt Lower; |
3067 | APInt Upper; |
3068 | Type *Ty = nullptr; |
3069 | LocTy TyLoc; |
3070 | |
3071 | auto ParseAPSInt = [&](unsigned BitWidth, APInt &Val) { |
3072 | if (Lex.getKind() != lltok::APSInt) |
3073 | return tokError(Msg: "expected integer" ); |
3074 | if (Lex.getAPSIntVal().getBitWidth() > BitWidth) |
3075 | return tokError( |
3076 | Msg: "integer is too large for the bit width of specified type" ); |
3077 | Val = Lex.getAPSIntVal().extend(width: BitWidth); |
3078 | Lex.Lex(); |
3079 | return false; |
3080 | }; |
3081 | |
3082 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '('" ) || parseType(Result&: Ty, Loc&: TyLoc)) |
3083 | return true; |
3084 | if (!Ty->isIntegerTy()) |
3085 | return error(L: TyLoc, Msg: "the range must have integer type!" ); |
3086 | |
3087 | unsigned BitWidth = Ty->getPrimitiveSizeInBits(); |
3088 | |
3089 | if (ParseAPSInt(BitWidth, Lower) || |
3090 | parseToken(T: lltok::comma, ErrMsg: "expected ','" ) || ParseAPSInt(BitWidth, Upper)) |
3091 | return true; |
3092 | if (Lower == Upper) |
3093 | return tokError(Msg: "the range should not represent the full or empty set!" ); |
3094 | |
3095 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')'" )) |
3096 | return true; |
3097 | |
3098 | B.addRangeAttr(CR: ConstantRange(Lower, Upper)); |
3099 | return false; |
3100 | } |
3101 | |
3102 | /// parseOptionalOperandBundles |
3103 | /// ::= /*empty*/ |
3104 | /// ::= '[' OperandBundle [, OperandBundle ]* ']' |
3105 | /// |
3106 | /// OperandBundle |
3107 | /// ::= bundle-tag '(' ')' |
3108 | /// ::= bundle-tag '(' Type Value [, Type Value ]* ')' |
3109 | /// |
3110 | /// bundle-tag ::= String Constant |
3111 | bool LLParser::parseOptionalOperandBundles( |
3112 | SmallVectorImpl<OperandBundleDef> &BundleList, PerFunctionState &PFS) { |
3113 | LocTy BeginLoc = Lex.getLoc(); |
3114 | if (!EatIfPresent(T: lltok::lsquare)) |
3115 | return false; |
3116 | |
3117 | while (Lex.getKind() != lltok::rsquare) { |
3118 | // If this isn't the first operand bundle, we need a comma. |
3119 | if (!BundleList.empty() && |
3120 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in input list" )) |
3121 | return true; |
3122 | |
3123 | std::string Tag; |
3124 | if (parseStringConstant(Result&: Tag)) |
3125 | return true; |
3126 | |
3127 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in operand bundle" )) |
3128 | return true; |
3129 | |
3130 | std::vector<Value *> Inputs; |
3131 | while (Lex.getKind() != lltok::rparen) { |
3132 | // If this isn't the first input, we need a comma. |
3133 | if (!Inputs.empty() && |
3134 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in input list" )) |
3135 | return true; |
3136 | |
3137 | Type *Ty = nullptr; |
3138 | Value *Input = nullptr; |
3139 | if (parseType(Result&: Ty) || parseValue(Ty, V&: Input, PFS)) |
3140 | return true; |
3141 | Inputs.push_back(x: Input); |
3142 | } |
3143 | |
3144 | BundleList.emplace_back(Args: std::move(Tag), Args: std::move(Inputs)); |
3145 | |
3146 | Lex.Lex(); // Lex the ')'. |
3147 | } |
3148 | |
3149 | if (BundleList.empty()) |
3150 | return error(L: BeginLoc, Msg: "operand bundle set must not be empty" ); |
3151 | |
3152 | Lex.Lex(); // Lex the ']'. |
3153 | return false; |
3154 | } |
3155 | |
3156 | bool LLParser::checkValueID(LocTy Loc, StringRef Kind, StringRef Prefix, |
3157 | unsigned NextID, unsigned ID) const { |
3158 | if (ID < NextID) |
3159 | return error(L: Loc, Msg: Kind + " expected to be numbered '" + Prefix + |
3160 | Twine(NextID) + "' or greater" ); |
3161 | |
3162 | return false; |
3163 | } |
3164 | |
3165 | /// parseArgumentList - parse the argument list for a function type or function |
3166 | /// prototype. |
3167 | /// ::= '(' ArgTypeListI ')' |
3168 | /// ArgTypeListI |
3169 | /// ::= /*empty*/ |
3170 | /// ::= '...' |
3171 | /// ::= ArgTypeList ',' '...' |
3172 | /// ::= ArgType (',' ArgType)* |
3173 | /// |
3174 | bool LLParser::parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, |
3175 | SmallVectorImpl<unsigned> &UnnamedArgNums, |
3176 | bool &IsVarArg) { |
3177 | unsigned CurValID = 0; |
3178 | IsVarArg = false; |
3179 | assert(Lex.getKind() == lltok::lparen); |
3180 | Lex.Lex(); // eat the (. |
3181 | |
3182 | if (Lex.getKind() != lltok::rparen) { |
3183 | do { |
3184 | // Handle ... at end of arg list. |
3185 | if (EatIfPresent(T: lltok::dotdotdot)) { |
3186 | IsVarArg = true; |
3187 | break; |
3188 | } |
3189 | |
3190 | // Otherwise must be an argument type. |
3191 | LocTy TypeLoc = Lex.getLoc(); |
3192 | Type *ArgTy = nullptr; |
3193 | AttrBuilder Attrs(M->getContext()); |
3194 | if (parseType(Result&: ArgTy) || parseOptionalParamAttrs(B&: Attrs)) |
3195 | return true; |
3196 | |
3197 | if (ArgTy->isVoidTy()) |
3198 | return error(L: TypeLoc, Msg: "argument can not have void type" ); |
3199 | |
3200 | std::string Name; |
3201 | if (Lex.getKind() == lltok::LocalVar) { |
3202 | Name = Lex.getStrVal(); |
3203 | Lex.Lex(); |
3204 | } else { |
3205 | unsigned ArgID; |
3206 | if (Lex.getKind() == lltok::LocalVarID) { |
3207 | ArgID = Lex.getUIntVal(); |
3208 | if (checkValueID(Loc: TypeLoc, Kind: "argument" , Prefix: "%" , NextID: CurValID, ID: ArgID)) |
3209 | return true; |
3210 | Lex.Lex(); |
3211 | } else { |
3212 | ArgID = CurValID; |
3213 | } |
3214 | UnnamedArgNums.push_back(Elt: ArgID); |
3215 | CurValID = ArgID + 1; |
3216 | } |
3217 | |
3218 | if (!ArgTy->isFirstClassType()) |
3219 | return error(L: TypeLoc, Msg: "invalid type for function argument" ); |
3220 | |
3221 | ArgList.emplace_back(Args&: TypeLoc, Args&: ArgTy, |
3222 | Args: AttributeSet::get(C&: ArgTy->getContext(), B: Attrs), |
3223 | Args: std::move(Name)); |
3224 | } while (EatIfPresent(T: lltok::comma)); |
3225 | } |
3226 | |
3227 | return parseToken(T: lltok::rparen, ErrMsg: "expected ')' at end of argument list" ); |
3228 | } |
3229 | |
3230 | /// parseFunctionType |
3231 | /// ::= Type ArgumentList OptionalAttrs |
3232 | bool LLParser::parseFunctionType(Type *&Result) { |
3233 | assert(Lex.getKind() == lltok::lparen); |
3234 | |
3235 | if (!FunctionType::isValidReturnType(RetTy: Result)) |
3236 | return tokError(Msg: "invalid function return type" ); |
3237 | |
3238 | SmallVector<ArgInfo, 8> ArgList; |
3239 | bool IsVarArg; |
3240 | SmallVector<unsigned> UnnamedArgNums; |
3241 | if (parseArgumentList(ArgList, UnnamedArgNums, IsVarArg)) |
3242 | return true; |
3243 | |
3244 | // Reject names on the arguments lists. |
3245 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
3246 | if (!ArgList[i].Name.empty()) |
3247 | return error(L: ArgList[i].Loc, Msg: "argument name invalid in function type" ); |
3248 | if (ArgList[i].Attrs.hasAttributes()) |
3249 | return error(L: ArgList[i].Loc, |
3250 | Msg: "argument attributes invalid in function type" ); |
3251 | } |
3252 | |
3253 | SmallVector<Type*, 16> ArgListTy; |
3254 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) |
3255 | ArgListTy.push_back(Elt: ArgList[i].Ty); |
3256 | |
3257 | Result = FunctionType::get(Result, Params: ArgListTy, isVarArg: IsVarArg); |
3258 | return false; |
3259 | } |
3260 | |
3261 | /// parseAnonStructType - parse an anonymous struct type, which is inlined into |
3262 | /// other structs. |
3263 | bool LLParser::parseAnonStructType(Type *&Result, bool Packed) { |
3264 | SmallVector<Type*, 8> Elts; |
3265 | if (parseStructBody(Body&: Elts)) |
3266 | return true; |
3267 | |
3268 | Result = StructType::get(Context, Elements: Elts, isPacked: Packed); |
3269 | return false; |
3270 | } |
3271 | |
3272 | /// parseStructDefinition - parse a struct in a 'type' definition. |
3273 | bool LLParser::parseStructDefinition(SMLoc TypeLoc, StringRef Name, |
3274 | std::pair<Type *, LocTy> &Entry, |
3275 | Type *&ResultTy) { |
3276 | // If the type was already defined, diagnose the redefinition. |
3277 | if (Entry.first && !Entry.second.isValid()) |
3278 | return error(L: TypeLoc, Msg: "redefinition of type" ); |
3279 | |
3280 | // If we have opaque, just return without filling in the definition for the |
3281 | // struct. This counts as a definition as far as the .ll file goes. |
3282 | if (EatIfPresent(T: lltok::kw_opaque)) { |
3283 | // This type is being defined, so clear the location to indicate this. |
3284 | Entry.second = SMLoc(); |
3285 | |
3286 | // If this type number has never been uttered, create it. |
3287 | if (!Entry.first) |
3288 | Entry.first = StructType::create(Context, Name); |
3289 | ResultTy = Entry.first; |
3290 | return false; |
3291 | } |
3292 | |
3293 | // If the type starts with '<', then it is either a packed struct or a vector. |
3294 | bool isPacked = EatIfPresent(T: lltok::less); |
3295 | |
3296 | // If we don't have a struct, then we have a random type alias, which we |
3297 | // accept for compatibility with old files. These types are not allowed to be |
3298 | // forward referenced and not allowed to be recursive. |
3299 | if (Lex.getKind() != lltok::lbrace) { |
3300 | if (Entry.first) |
3301 | return error(L: TypeLoc, Msg: "forward references to non-struct type" ); |
3302 | |
3303 | ResultTy = nullptr; |
3304 | if (isPacked) |
3305 | return parseArrayVectorType(Result&: ResultTy, IsVector: true); |
3306 | return parseType(Result&: ResultTy); |
3307 | } |
3308 | |
3309 | // This type is being defined, so clear the location to indicate this. |
3310 | Entry.second = SMLoc(); |
3311 | |
3312 | // If this type number has never been uttered, create it. |
3313 | if (!Entry.first) |
3314 | Entry.first = StructType::create(Context, Name); |
3315 | |
3316 | StructType *STy = cast<StructType>(Val: Entry.first); |
3317 | |
3318 | SmallVector<Type*, 8> Body; |
3319 | if (parseStructBody(Body) || |
3320 | (isPacked && parseToken(T: lltok::greater, ErrMsg: "expected '>' in packed struct" ))) |
3321 | return true; |
3322 | |
3323 | STy->setBody(Elements: Body, isPacked); |
3324 | ResultTy = STy; |
3325 | return false; |
3326 | } |
3327 | |
3328 | /// parseStructType: Handles packed and unpacked types. </> parsed elsewhere. |
3329 | /// StructType |
3330 | /// ::= '{' '}' |
3331 | /// ::= '{' Type (',' Type)* '}' |
3332 | /// ::= '<' '{' '}' '>' |
3333 | /// ::= '<' '{' Type (',' Type)* '}' '>' |
3334 | bool LLParser::parseStructBody(SmallVectorImpl<Type *> &Body) { |
3335 | assert(Lex.getKind() == lltok::lbrace); |
3336 | Lex.Lex(); // Consume the '{' |
3337 | |
3338 | // Handle the empty struct. |
3339 | if (EatIfPresent(T: lltok::rbrace)) |
3340 | return false; |
3341 | |
3342 | LocTy EltTyLoc = Lex.getLoc(); |
3343 | Type *Ty = nullptr; |
3344 | if (parseType(Result&: Ty)) |
3345 | return true; |
3346 | Body.push_back(Elt: Ty); |
3347 | |
3348 | if (!StructType::isValidElementType(ElemTy: Ty)) |
3349 | return error(L: EltTyLoc, Msg: "invalid element type for struct" ); |
3350 | |
3351 | while (EatIfPresent(T: lltok::comma)) { |
3352 | EltTyLoc = Lex.getLoc(); |
3353 | if (parseType(Result&: Ty)) |
3354 | return true; |
3355 | |
3356 | if (!StructType::isValidElementType(ElemTy: Ty)) |
3357 | return error(L: EltTyLoc, Msg: "invalid element type for struct" ); |
3358 | |
3359 | Body.push_back(Elt: Ty); |
3360 | } |
3361 | |
3362 | return parseToken(T: lltok::rbrace, ErrMsg: "expected '}' at end of struct" ); |
3363 | } |
3364 | |
3365 | /// parseArrayVectorType - parse an array or vector type, assuming the first |
3366 | /// token has already been consumed. |
3367 | /// Type |
3368 | /// ::= '[' APSINTVAL 'x' Types ']' |
3369 | /// ::= '<' APSINTVAL 'x' Types '>' |
3370 | /// ::= '<' 'vscale' 'x' APSINTVAL 'x' Types '>' |
3371 | bool LLParser::parseArrayVectorType(Type *&Result, bool IsVector) { |
3372 | bool Scalable = false; |
3373 | |
3374 | if (IsVector && Lex.getKind() == lltok::kw_vscale) { |
3375 | Lex.Lex(); // consume the 'vscale' |
3376 | if (parseToken(T: lltok::kw_x, ErrMsg: "expected 'x' after vscale" )) |
3377 | return true; |
3378 | |
3379 | Scalable = true; |
3380 | } |
3381 | |
3382 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() || |
3383 | Lex.getAPSIntVal().getBitWidth() > 64) |
3384 | return tokError(Msg: "expected number in address space" ); |
3385 | |
3386 | LocTy SizeLoc = Lex.getLoc(); |
3387 | uint64_t Size = Lex.getAPSIntVal().getZExtValue(); |
3388 | Lex.Lex(); |
3389 | |
3390 | if (parseToken(T: lltok::kw_x, ErrMsg: "expected 'x' after element count" )) |
3391 | return true; |
3392 | |
3393 | LocTy TypeLoc = Lex.getLoc(); |
3394 | Type *EltTy = nullptr; |
3395 | if (parseType(Result&: EltTy)) |
3396 | return true; |
3397 | |
3398 | if (parseToken(T: IsVector ? lltok::greater : lltok::rsquare, |
3399 | ErrMsg: "expected end of sequential type" )) |
3400 | return true; |
3401 | |
3402 | if (IsVector) { |
3403 | if (Size == 0) |
3404 | return error(L: SizeLoc, Msg: "zero element vector is illegal" ); |
3405 | if ((unsigned)Size != Size) |
3406 | return error(L: SizeLoc, Msg: "size too large for vector" ); |
3407 | if (!VectorType::isValidElementType(ElemTy: EltTy)) |
3408 | return error(L: TypeLoc, Msg: "invalid vector element type" ); |
3409 | Result = VectorType::get(ElementType: EltTy, NumElements: unsigned(Size), Scalable); |
3410 | } else { |
3411 | if (!ArrayType::isValidElementType(ElemTy: EltTy)) |
3412 | return error(L: TypeLoc, Msg: "invalid array element type" ); |
3413 | Result = ArrayType::get(ElementType: EltTy, NumElements: Size); |
3414 | } |
3415 | return false; |
3416 | } |
3417 | |
3418 | /// parseTargetExtType - handle target extension type syntax |
3419 | /// TargetExtType |
3420 | /// ::= 'target' '(' STRINGCONSTANT TargetExtTypeParams TargetExtIntParams ')' |
3421 | /// |
3422 | /// TargetExtTypeParams |
3423 | /// ::= /*empty*/ |
3424 | /// ::= ',' Type TargetExtTypeParams |
3425 | /// |
3426 | /// TargetExtIntParams |
3427 | /// ::= /*empty*/ |
3428 | /// ::= ',' uint32 TargetExtIntParams |
3429 | bool LLParser::parseTargetExtType(Type *&Result) { |
3430 | Lex.Lex(); // Eat the 'target' keyword. |
3431 | |
3432 | // Get the mandatory type name. |
3433 | std::string TypeName; |
3434 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in target extension type" ) || |
3435 | parseStringConstant(Result&: TypeName)) |
3436 | return true; |
3437 | |
3438 | // Parse all of the integer and type parameters at the same time; the use of |
3439 | // SeenInt will allow us to catch cases where type parameters follow integer |
3440 | // parameters. |
3441 | SmallVector<Type *> TypeParams; |
3442 | SmallVector<unsigned> IntParams; |
3443 | bool SeenInt = false; |
3444 | while (Lex.getKind() == lltok::comma) { |
3445 | Lex.Lex(); // Eat the comma. |
3446 | |
3447 | if (Lex.getKind() == lltok::APSInt) { |
3448 | SeenInt = true; |
3449 | unsigned IntVal; |
3450 | if (parseUInt32(Val&: IntVal)) |
3451 | return true; |
3452 | IntParams.push_back(Elt: IntVal); |
3453 | } else if (SeenInt) { |
3454 | // The only other kind of parameter we support is type parameters, which |
3455 | // must precede the integer parameters. This is therefore an error. |
3456 | return tokError(Msg: "expected uint32 param" ); |
3457 | } else { |
3458 | Type *TypeParam; |
3459 | if (parseType(Result&: TypeParam, /*AllowVoid=*/true)) |
3460 | return true; |
3461 | TypeParams.push_back(Elt: TypeParam); |
3462 | } |
3463 | } |
3464 | |
3465 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in target extension type" )) |
3466 | return true; |
3467 | |
3468 | Result = TargetExtType::get(Context, Name: TypeName, Types: TypeParams, Ints: IntParams); |
3469 | return false; |
3470 | } |
3471 | |
3472 | //===----------------------------------------------------------------------===// |
3473 | // Function Semantic Analysis. |
3474 | //===----------------------------------------------------------------------===// |
3475 | |
3476 | LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f, |
3477 | int functionNumber, |
3478 | ArrayRef<unsigned> UnnamedArgNums) |
3479 | : P(p), F(f), FunctionNumber(functionNumber) { |
3480 | |
3481 | // Insert unnamed arguments into the NumberedVals list. |
3482 | auto It = UnnamedArgNums.begin(); |
3483 | for (Argument &A : F.args()) { |
3484 | if (!A.hasName()) { |
3485 | unsigned ArgNum = *It++; |
3486 | NumberedVals.add(ID: ArgNum, V: &A); |
3487 | } |
3488 | } |
3489 | } |
3490 | |
3491 | LLParser::PerFunctionState::~PerFunctionState() { |
3492 | // If there were any forward referenced non-basicblock values, delete them. |
3493 | |
3494 | for (const auto &P : ForwardRefVals) { |
3495 | if (isa<BasicBlock>(Val: P.second.first)) |
3496 | continue; |
3497 | P.second.first->replaceAllUsesWith( |
3498 | V: UndefValue::get(T: P.second.first->getType())); |
3499 | P.second.first->deleteValue(); |
3500 | } |
3501 | |
3502 | for (const auto &P : ForwardRefValIDs) { |
3503 | if (isa<BasicBlock>(Val: P.second.first)) |
3504 | continue; |
3505 | P.second.first->replaceAllUsesWith( |
3506 | V: UndefValue::get(T: P.second.first->getType())); |
3507 | P.second.first->deleteValue(); |
3508 | } |
3509 | } |
3510 | |
3511 | bool LLParser::PerFunctionState::finishFunction() { |
3512 | if (!ForwardRefVals.empty()) |
3513 | return P.error(L: ForwardRefVals.begin()->second.second, |
3514 | Msg: "use of undefined value '%" + ForwardRefVals.begin()->first + |
3515 | "'" ); |
3516 | if (!ForwardRefValIDs.empty()) |
3517 | return P.error(L: ForwardRefValIDs.begin()->second.second, |
3518 | Msg: "use of undefined value '%" + |
3519 | Twine(ForwardRefValIDs.begin()->first) + "'" ); |
3520 | return false; |
3521 | } |
3522 | |
3523 | /// getVal - Get a value with the specified name or ID, creating a |
3524 | /// forward reference record if needed. This can return null if the value |
3525 | /// exists but does not have the right type. |
3526 | Value *LLParser::PerFunctionState::getVal(const std::string &Name, Type *Ty, |
3527 | LocTy Loc) { |
3528 | // Look this name up in the normal function symbol table. |
3529 | Value *Val = F.getValueSymbolTable()->lookup(Name); |
3530 | |
3531 | // If this is a forward reference for the value, see if we already created a |
3532 | // forward ref record. |
3533 | if (!Val) { |
3534 | auto I = ForwardRefVals.find(x: Name); |
3535 | if (I != ForwardRefVals.end()) |
3536 | Val = I->second.first; |
3537 | } |
3538 | |
3539 | // If we have the value in the symbol table or fwd-ref table, return it. |
3540 | if (Val) |
3541 | return P.checkValidVariableType(Loc, Name: "%" + Name, Ty, Val); |
3542 | |
3543 | // Don't make placeholders with invalid type. |
3544 | if (!Ty->isFirstClassType()) { |
3545 | P.error(L: Loc, Msg: "invalid use of a non-first-class type" ); |
3546 | return nullptr; |
3547 | } |
3548 | |
3549 | // Otherwise, create a new forward reference for this value and remember it. |
3550 | Value *FwdVal; |
3551 | if (Ty->isLabelTy()) { |
3552 | FwdVal = BasicBlock::Create(Context&: F.getContext(), Name, Parent: &F); |
3553 | } else { |
3554 | FwdVal = new Argument(Ty, Name); |
3555 | } |
3556 | if (FwdVal->getName() != Name) { |
3557 | P.error(L: Loc, Msg: "name is too long which can result in name collisions, " |
3558 | "consider making the name shorter or " |
3559 | "increasing -non-global-value-max-name-size" ); |
3560 | return nullptr; |
3561 | } |
3562 | |
3563 | ForwardRefVals[Name] = std::make_pair(x&: FwdVal, y&: Loc); |
3564 | return FwdVal; |
3565 | } |
3566 | |
3567 | Value *LLParser::PerFunctionState::getVal(unsigned ID, Type *Ty, LocTy Loc) { |
3568 | // Look this name up in the normal function symbol table. |
3569 | Value *Val = NumberedVals.get(ID); |
3570 | |
3571 | // If this is a forward reference for the value, see if we already created a |
3572 | // forward ref record. |
3573 | if (!Val) { |
3574 | auto I = ForwardRefValIDs.find(x: ID); |
3575 | if (I != ForwardRefValIDs.end()) |
3576 | Val = I->second.first; |
3577 | } |
3578 | |
3579 | // If we have the value in the symbol table or fwd-ref table, return it. |
3580 | if (Val) |
3581 | return P.checkValidVariableType(Loc, Name: "%" + Twine(ID), Ty, Val); |
3582 | |
3583 | if (!Ty->isFirstClassType()) { |
3584 | P.error(L: Loc, Msg: "invalid use of a non-first-class type" ); |
3585 | return nullptr; |
3586 | } |
3587 | |
3588 | // Otherwise, create a new forward reference for this value and remember it. |
3589 | Value *FwdVal; |
3590 | if (Ty->isLabelTy()) { |
3591 | FwdVal = BasicBlock::Create(Context&: F.getContext(), Name: "" , Parent: &F); |
3592 | } else { |
3593 | FwdVal = new Argument(Ty); |
3594 | } |
3595 | |
3596 | ForwardRefValIDs[ID] = std::make_pair(x&: FwdVal, y&: Loc); |
3597 | return FwdVal; |
3598 | } |
3599 | |
3600 | /// setInstName - After an instruction is parsed and inserted into its |
3601 | /// basic block, this installs its name. |
3602 | bool LLParser::PerFunctionState::setInstName(int NameID, |
3603 | const std::string &NameStr, |
3604 | LocTy NameLoc, Instruction *Inst) { |
3605 | // If this instruction has void type, it cannot have a name or ID specified. |
3606 | if (Inst->getType()->isVoidTy()) { |
3607 | if (NameID != -1 || !NameStr.empty()) |
3608 | return P.error(L: NameLoc, Msg: "instructions returning void cannot have a name" ); |
3609 | return false; |
3610 | } |
3611 | |
3612 | // If this was a numbered instruction, verify that the instruction is the |
3613 | // expected value and resolve any forward references. |
3614 | if (NameStr.empty()) { |
3615 | // If neither a name nor an ID was specified, just use the next ID. |
3616 | if (NameID == -1) |
3617 | NameID = NumberedVals.getNext(); |
3618 | |
3619 | if (P.checkValueID(Loc: NameLoc, Kind: "instruction" , Prefix: "%" , NextID: NumberedVals.getNext(), |
3620 | ID: NameID)) |
3621 | return true; |
3622 | |
3623 | auto FI = ForwardRefValIDs.find(x: NameID); |
3624 | if (FI != ForwardRefValIDs.end()) { |
3625 | Value *Sentinel = FI->second.first; |
3626 | if (Sentinel->getType() != Inst->getType()) |
3627 | return P.error(L: NameLoc, Msg: "instruction forward referenced with type '" + |
3628 | getTypeString(T: FI->second.first->getType()) + |
3629 | "'" ); |
3630 | |
3631 | Sentinel->replaceAllUsesWith(V: Inst); |
3632 | Sentinel->deleteValue(); |
3633 | ForwardRefValIDs.erase(position: FI); |
3634 | } |
3635 | |
3636 | NumberedVals.add(ID: NameID, V: Inst); |
3637 | return false; |
3638 | } |
3639 | |
3640 | // Otherwise, the instruction had a name. Resolve forward refs and set it. |
3641 | auto FI = ForwardRefVals.find(x: NameStr); |
3642 | if (FI != ForwardRefVals.end()) { |
3643 | Value *Sentinel = FI->second.first; |
3644 | if (Sentinel->getType() != Inst->getType()) |
3645 | return P.error(L: NameLoc, Msg: "instruction forward referenced with type '" + |
3646 | getTypeString(T: FI->second.first->getType()) + |
3647 | "'" ); |
3648 | |
3649 | Sentinel->replaceAllUsesWith(V: Inst); |
3650 | Sentinel->deleteValue(); |
3651 | ForwardRefVals.erase(position: FI); |
3652 | } |
3653 | |
3654 | // Set the name on the instruction. |
3655 | Inst->setName(NameStr); |
3656 | |
3657 | if (Inst->getName() != NameStr) |
3658 | return P.error(L: NameLoc, Msg: "multiple definition of local value named '" + |
3659 | NameStr + "'" ); |
3660 | return false; |
3661 | } |
3662 | |
3663 | /// getBB - Get a basic block with the specified name or ID, creating a |
3664 | /// forward reference record if needed. |
3665 | BasicBlock *LLParser::PerFunctionState::getBB(const std::string &Name, |
3666 | LocTy Loc) { |
3667 | return dyn_cast_or_null<BasicBlock>( |
3668 | Val: getVal(Name, Ty: Type::getLabelTy(C&: F.getContext()), Loc)); |
3669 | } |
3670 | |
3671 | BasicBlock *LLParser::PerFunctionState::getBB(unsigned ID, LocTy Loc) { |
3672 | return dyn_cast_or_null<BasicBlock>( |
3673 | Val: getVal(ID, Ty: Type::getLabelTy(C&: F.getContext()), Loc)); |
3674 | } |
3675 | |
3676 | /// defineBB - Define the specified basic block, which is either named or |
3677 | /// unnamed. If there is an error, this returns null otherwise it returns |
3678 | /// the block being defined. |
3679 | BasicBlock *LLParser::PerFunctionState::defineBB(const std::string &Name, |
3680 | int NameID, LocTy Loc) { |
3681 | BasicBlock *BB; |
3682 | if (Name.empty()) { |
3683 | if (NameID != -1) { |
3684 | if (P.checkValueID(Loc, Kind: "label" , Prefix: "" , NextID: NumberedVals.getNext(), ID: NameID)) |
3685 | return nullptr; |
3686 | } else { |
3687 | NameID = NumberedVals.getNext(); |
3688 | } |
3689 | BB = getBB(ID: NameID, Loc); |
3690 | if (!BB) { |
3691 | P.error(L: Loc, Msg: "unable to create block numbered '" + Twine(NameID) + "'" ); |
3692 | return nullptr; |
3693 | } |
3694 | } else { |
3695 | BB = getBB(Name, Loc); |
3696 | if (!BB) { |
3697 | P.error(L: Loc, Msg: "unable to create block named '" + Name + "'" ); |
3698 | return nullptr; |
3699 | } |
3700 | } |
3701 | |
3702 | // Move the block to the end of the function. Forward ref'd blocks are |
3703 | // inserted wherever they happen to be referenced. |
3704 | F.splice(ToIt: F.end(), FromF: &F, FromIt: BB->getIterator()); |
3705 | |
3706 | // Remove the block from forward ref sets. |
3707 | if (Name.empty()) { |
3708 | ForwardRefValIDs.erase(x: NameID); |
3709 | NumberedVals.add(ID: NameID, V: BB); |
3710 | } else { |
3711 | // BB forward references are already in the function symbol table. |
3712 | ForwardRefVals.erase(x: Name); |
3713 | } |
3714 | |
3715 | return BB; |
3716 | } |
3717 | |
3718 | //===----------------------------------------------------------------------===// |
3719 | // Constants. |
3720 | //===----------------------------------------------------------------------===// |
3721 | |
3722 | /// parseValID - parse an abstract value that doesn't necessarily have a |
3723 | /// type implied. For example, if we parse "4" we don't know what integer type |
3724 | /// it has. The value will later be combined with its type and checked for |
3725 | /// basic correctness. PFS is used to convert function-local operands of |
3726 | /// metadata (since metadata operands are not just parsed here but also |
3727 | /// converted to values). PFS can be null when we are not parsing metadata |
3728 | /// values inside a function. |
3729 | bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) { |
3730 | ID.Loc = Lex.getLoc(); |
3731 | switch (Lex.getKind()) { |
3732 | default: |
3733 | return tokError(Msg: "expected value token" ); |
3734 | case lltok::GlobalID: // @42 |
3735 | ID.UIntVal = Lex.getUIntVal(); |
3736 | ID.Kind = ValID::t_GlobalID; |
3737 | break; |
3738 | case lltok::GlobalVar: // @foo |
3739 | ID.StrVal = Lex.getStrVal(); |
3740 | ID.Kind = ValID::t_GlobalName; |
3741 | break; |
3742 | case lltok::LocalVarID: // %42 |
3743 | ID.UIntVal = Lex.getUIntVal(); |
3744 | ID.Kind = ValID::t_LocalID; |
3745 | break; |
3746 | case lltok::LocalVar: // %foo |
3747 | ID.StrVal = Lex.getStrVal(); |
3748 | ID.Kind = ValID::t_LocalName; |
3749 | break; |
3750 | case lltok::APSInt: |
3751 | ID.APSIntVal = Lex.getAPSIntVal(); |
3752 | ID.Kind = ValID::t_APSInt; |
3753 | break; |
3754 | case lltok::APFloat: |
3755 | ID.APFloatVal = Lex.getAPFloatVal(); |
3756 | ID.Kind = ValID::t_APFloat; |
3757 | break; |
3758 | case lltok::kw_true: |
3759 | ID.ConstantVal = ConstantInt::getTrue(Context); |
3760 | ID.Kind = ValID::t_Constant; |
3761 | break; |
3762 | case lltok::kw_false: |
3763 | ID.ConstantVal = ConstantInt::getFalse(Context); |
3764 | ID.Kind = ValID::t_Constant; |
3765 | break; |
3766 | case lltok::kw_null: ID.Kind = ValID::t_Null; break; |
3767 | case lltok::kw_undef: ID.Kind = ValID::t_Undef; break; |
3768 | case lltok::kw_poison: ID.Kind = ValID::t_Poison; break; |
3769 | case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break; |
3770 | case lltok::kw_none: ID.Kind = ValID::t_None; break; |
3771 | |
3772 | case lltok::lbrace: { |
3773 | // ValID ::= '{' ConstVector '}' |
3774 | Lex.Lex(); |
3775 | SmallVector<Constant*, 16> Elts; |
3776 | if (parseGlobalValueVector(Elts) || |
3777 | parseToken(T: lltok::rbrace, ErrMsg: "expected end of struct constant" )) |
3778 | return true; |
3779 | |
3780 | ID.ConstantStructElts = std::make_unique<Constant *[]>(num: Elts.size()); |
3781 | ID.UIntVal = Elts.size(); |
3782 | memcpy(dest: ID.ConstantStructElts.get(), src: Elts.data(), |
3783 | n: Elts.size() * sizeof(Elts[0])); |
3784 | ID.Kind = ValID::t_ConstantStruct; |
3785 | return false; |
3786 | } |
3787 | case lltok::less: { |
3788 | // ValID ::= '<' ConstVector '>' --> Vector. |
3789 | // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct. |
3790 | Lex.Lex(); |
3791 | bool isPackedStruct = EatIfPresent(T: lltok::lbrace); |
3792 | |
3793 | SmallVector<Constant*, 16> Elts; |
3794 | LocTy FirstEltLoc = Lex.getLoc(); |
3795 | if (parseGlobalValueVector(Elts) || |
3796 | (isPackedStruct && |
3797 | parseToken(T: lltok::rbrace, ErrMsg: "expected end of packed struct" )) || |
3798 | parseToken(T: lltok::greater, ErrMsg: "expected end of constant" )) |
3799 | return true; |
3800 | |
3801 | if (isPackedStruct) { |
3802 | ID.ConstantStructElts = std::make_unique<Constant *[]>(num: Elts.size()); |
3803 | memcpy(dest: ID.ConstantStructElts.get(), src: Elts.data(), |
3804 | n: Elts.size() * sizeof(Elts[0])); |
3805 | ID.UIntVal = Elts.size(); |
3806 | ID.Kind = ValID::t_PackedConstantStruct; |
3807 | return false; |
3808 | } |
3809 | |
3810 | if (Elts.empty()) |
3811 | return error(L: ID.Loc, Msg: "constant vector must not be empty" ); |
3812 | |
3813 | if (!Elts[0]->getType()->isIntegerTy() && |
3814 | !Elts[0]->getType()->isFloatingPointTy() && |
3815 | !Elts[0]->getType()->isPointerTy()) |
3816 | return error( |
3817 | L: FirstEltLoc, |
3818 | Msg: "vector elements must have integer, pointer or floating point type" ); |
3819 | |
3820 | // Verify that all the vector elements have the same type. |
3821 | for (unsigned i = 1, e = Elts.size(); i != e; ++i) |
3822 | if (Elts[i]->getType() != Elts[0]->getType()) |
3823 | return error(L: FirstEltLoc, Msg: "vector element #" + Twine(i) + |
3824 | " is not of type '" + |
3825 | getTypeString(T: Elts[0]->getType())); |
3826 | |
3827 | ID.ConstantVal = ConstantVector::get(V: Elts); |
3828 | ID.Kind = ValID::t_Constant; |
3829 | return false; |
3830 | } |
3831 | case lltok::lsquare: { // Array Constant |
3832 | Lex.Lex(); |
3833 | SmallVector<Constant*, 16> Elts; |
3834 | LocTy FirstEltLoc = Lex.getLoc(); |
3835 | if (parseGlobalValueVector(Elts) || |
3836 | parseToken(T: lltok::rsquare, ErrMsg: "expected end of array constant" )) |
3837 | return true; |
3838 | |
3839 | // Handle empty element. |
3840 | if (Elts.empty()) { |
3841 | // Use undef instead of an array because it's inconvenient to determine |
3842 | // the element type at this point, there being no elements to examine. |
3843 | ID.Kind = ValID::t_EmptyArray; |
3844 | return false; |
3845 | } |
3846 | |
3847 | if (!Elts[0]->getType()->isFirstClassType()) |
3848 | return error(L: FirstEltLoc, Msg: "invalid array element type: " + |
3849 | getTypeString(T: Elts[0]->getType())); |
3850 | |
3851 | ArrayType *ATy = ArrayType::get(ElementType: Elts[0]->getType(), NumElements: Elts.size()); |
3852 | |
3853 | // Verify all elements are correct type! |
3854 | for (unsigned i = 0, e = Elts.size(); i != e; ++i) { |
3855 | if (Elts[i]->getType() != Elts[0]->getType()) |
3856 | return error(L: FirstEltLoc, Msg: "array element #" + Twine(i) + |
3857 | " is not of type '" + |
3858 | getTypeString(T: Elts[0]->getType())); |
3859 | } |
3860 | |
3861 | ID.ConstantVal = ConstantArray::get(T: ATy, V: Elts); |
3862 | ID.Kind = ValID::t_Constant; |
3863 | return false; |
3864 | } |
3865 | case lltok::kw_c: // c "foo" |
3866 | Lex.Lex(); |
3867 | ID.ConstantVal = ConstantDataArray::getString(Context, Initializer: Lex.getStrVal(), |
3868 | AddNull: false); |
3869 | if (parseToken(T: lltok::StringConstant, ErrMsg: "expected string" )) |
3870 | return true; |
3871 | ID.Kind = ValID::t_Constant; |
3872 | return false; |
3873 | |
3874 | case lltok::kw_asm: { |
3875 | // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ',' |
3876 | // STRINGCONSTANT |
3877 | bool HasSideEffect, AlignStack, AsmDialect, CanThrow; |
3878 | Lex.Lex(); |
3879 | if (parseOptionalToken(T: lltok::kw_sideeffect, Present&: HasSideEffect) || |
3880 | parseOptionalToken(T: lltok::kw_alignstack, Present&: AlignStack) || |
3881 | parseOptionalToken(T: lltok::kw_inteldialect, Present&: AsmDialect) || |
3882 | parseOptionalToken(T: lltok::kw_unwind, Present&: CanThrow) || |
3883 | parseStringConstant(Result&: ID.StrVal) || |
3884 | parseToken(T: lltok::comma, ErrMsg: "expected comma in inline asm expression" ) || |
3885 | parseToken(T: lltok::StringConstant, ErrMsg: "expected constraint string" )) |
3886 | return true; |
3887 | ID.StrVal2 = Lex.getStrVal(); |
3888 | ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack) << 1) | |
3889 | (unsigned(AsmDialect) << 2) | (unsigned(CanThrow) << 3); |
3890 | ID.Kind = ValID::t_InlineAsm; |
3891 | return false; |
3892 | } |
3893 | |
3894 | case lltok::kw_blockaddress: { |
3895 | // ValID ::= 'blockaddress' '(' @foo ',' %bar ')' |
3896 | Lex.Lex(); |
3897 | |
3898 | ValID Fn, Label; |
3899 | |
3900 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in block address expression" ) || |
3901 | parseValID(ID&: Fn, PFS) || |
3902 | parseToken(T: lltok::comma, |
3903 | ErrMsg: "expected comma in block address expression" ) || |
3904 | parseValID(ID&: Label, PFS) || |
3905 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' in block address expression" )) |
3906 | return true; |
3907 | |
3908 | if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName) |
3909 | return error(L: Fn.Loc, Msg: "expected function name in blockaddress" ); |
3910 | if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName) |
3911 | return error(L: Label.Loc, Msg: "expected basic block name in blockaddress" ); |
3912 | |
3913 | // Try to find the function (but skip it if it's forward-referenced). |
3914 | GlobalValue *GV = nullptr; |
3915 | if (Fn.Kind == ValID::t_GlobalID) { |
3916 | GV = NumberedVals.get(ID: Fn.UIntVal); |
3917 | } else if (!ForwardRefVals.count(x: Fn.StrVal)) { |
3918 | GV = M->getNamedValue(Name: Fn.StrVal); |
3919 | } |
3920 | Function *F = nullptr; |
3921 | if (GV) { |
3922 | // Confirm that it's actually a function with a definition. |
3923 | if (!isa<Function>(Val: GV)) |
3924 | return error(L: Fn.Loc, Msg: "expected function name in blockaddress" ); |
3925 | F = cast<Function>(Val: GV); |
3926 | if (F->isDeclaration()) |
3927 | return error(L: Fn.Loc, Msg: "cannot take blockaddress inside a declaration" ); |
3928 | } |
3929 | |
3930 | if (!F) { |
3931 | // Make a global variable as a placeholder for this reference. |
3932 | GlobalValue *&FwdRef = |
3933 | ForwardRefBlockAddresses.insert(x: std::make_pair( |
3934 | x: std::move(Fn), |
3935 | y: std::map<ValID, GlobalValue *>())) |
3936 | .first->second.insert(x: std::make_pair(x: std::move(Label), y: nullptr)) |
3937 | .first->second; |
3938 | if (!FwdRef) { |
3939 | unsigned FwdDeclAS; |
3940 | if (ExpectedTy) { |
3941 | // If we know the type that the blockaddress is being assigned to, |
3942 | // we can use the address space of that type. |
3943 | if (!ExpectedTy->isPointerTy()) |
3944 | return error(L: ID.Loc, |
3945 | Msg: "type of blockaddress must be a pointer and not '" + |
3946 | getTypeString(T: ExpectedTy) + "'" ); |
3947 | FwdDeclAS = ExpectedTy->getPointerAddressSpace(); |
3948 | } else if (PFS) { |
3949 | // Otherwise, we default the address space of the current function. |
3950 | FwdDeclAS = PFS->getFunction().getAddressSpace(); |
3951 | } else { |
3952 | llvm_unreachable("Unknown address space for blockaddress" ); |
3953 | } |
3954 | FwdRef = new GlobalVariable( |
3955 | *M, Type::getInt8Ty(C&: Context), false, GlobalValue::InternalLinkage, |
3956 | nullptr, "" , nullptr, GlobalValue::NotThreadLocal, FwdDeclAS); |
3957 | } |
3958 | |
3959 | ID.ConstantVal = FwdRef; |
3960 | ID.Kind = ValID::t_Constant; |
3961 | return false; |
3962 | } |
3963 | |
3964 | // We found the function; now find the basic block. Don't use PFS, since we |
3965 | // might be inside a constant expression. |
3966 | BasicBlock *BB; |
3967 | if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) { |
3968 | if (Label.Kind == ValID::t_LocalID) |
3969 | BB = BlockAddressPFS->getBB(ID: Label.UIntVal, Loc: Label.Loc); |
3970 | else |
3971 | BB = BlockAddressPFS->getBB(Name: Label.StrVal, Loc: Label.Loc); |
3972 | if (!BB) |
3973 | return error(L: Label.Loc, Msg: "referenced value is not a basic block" ); |
3974 | } else { |
3975 | if (Label.Kind == ValID::t_LocalID) |
3976 | return error(L: Label.Loc, Msg: "cannot take address of numeric label after " |
3977 | "the function is defined" ); |
3978 | BB = dyn_cast_or_null<BasicBlock>( |
3979 | Val: F->getValueSymbolTable()->lookup(Name: Label.StrVal)); |
3980 | if (!BB) |
3981 | return error(L: Label.Loc, Msg: "referenced value is not a basic block" ); |
3982 | } |
3983 | |
3984 | ID.ConstantVal = BlockAddress::get(F, BB); |
3985 | ID.Kind = ValID::t_Constant; |
3986 | return false; |
3987 | } |
3988 | |
3989 | case lltok::kw_dso_local_equivalent: { |
3990 | // ValID ::= 'dso_local_equivalent' @foo |
3991 | Lex.Lex(); |
3992 | |
3993 | ValID Fn; |
3994 | |
3995 | if (parseValID(ID&: Fn, PFS)) |
3996 | return true; |
3997 | |
3998 | if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName) |
3999 | return error(L: Fn.Loc, |
4000 | Msg: "expected global value name in dso_local_equivalent" ); |
4001 | |
4002 | // Try to find the function (but skip it if it's forward-referenced). |
4003 | GlobalValue *GV = nullptr; |
4004 | if (Fn.Kind == ValID::t_GlobalID) { |
4005 | GV = NumberedVals.get(ID: Fn.UIntVal); |
4006 | } else if (!ForwardRefVals.count(x: Fn.StrVal)) { |
4007 | GV = M->getNamedValue(Name: Fn.StrVal); |
4008 | } |
4009 | |
4010 | if (!GV) { |
4011 | // Make a placeholder global variable as a placeholder for this reference. |
4012 | auto &FwdRefMap = (Fn.Kind == ValID::t_GlobalID) |
4013 | ? ForwardRefDSOLocalEquivalentIDs |
4014 | : ForwardRefDSOLocalEquivalentNames; |
4015 | GlobalValue *&FwdRef = FwdRefMap.try_emplace(k: Fn, args: nullptr).first->second; |
4016 | if (!FwdRef) { |
4017 | FwdRef = new GlobalVariable(*M, Type::getInt8Ty(C&: Context), false, |
4018 | GlobalValue::InternalLinkage, nullptr, "" , |
4019 | nullptr, GlobalValue::NotThreadLocal); |
4020 | } |
4021 | |
4022 | ID.ConstantVal = FwdRef; |
4023 | ID.Kind = ValID::t_Constant; |
4024 | return false; |
4025 | } |
4026 | |
4027 | if (!GV->getValueType()->isFunctionTy()) |
4028 | return error(L: Fn.Loc, Msg: "expected a function, alias to function, or ifunc " |
4029 | "in dso_local_equivalent" ); |
4030 | |
4031 | ID.ConstantVal = DSOLocalEquivalent::get(GV); |
4032 | ID.Kind = ValID::t_Constant; |
4033 | return false; |
4034 | } |
4035 | |
4036 | case lltok::kw_no_cfi: { |
4037 | // ValID ::= 'no_cfi' @foo |
4038 | Lex.Lex(); |
4039 | |
4040 | if (parseValID(ID, PFS)) |
4041 | return true; |
4042 | |
4043 | if (ID.Kind != ValID::t_GlobalID && ID.Kind != ValID::t_GlobalName) |
4044 | return error(L: ID.Loc, Msg: "expected global value name in no_cfi" ); |
4045 | |
4046 | ID.NoCFI = true; |
4047 | return false; |
4048 | } |
4049 | |
4050 | case lltok::kw_trunc: |
4051 | case lltok::kw_bitcast: |
4052 | case lltok::kw_addrspacecast: |
4053 | case lltok::kw_inttoptr: |
4054 | case lltok::kw_ptrtoint: { |
4055 | unsigned Opc = Lex.getUIntVal(); |
4056 | Type *DestTy = nullptr; |
4057 | Constant *SrcVal; |
4058 | Lex.Lex(); |
4059 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' after constantexpr cast" ) || |
4060 | parseGlobalTypeAndValue(V&: SrcVal) || |
4061 | parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' in constantexpr cast" ) || |
4062 | parseType(Result&: DestTy) || |
4063 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' at end of constantexpr cast" )) |
4064 | return true; |
4065 | if (!CastInst::castIsValid(op: (Instruction::CastOps)Opc, S: SrcVal, DstTy: DestTy)) |
4066 | return error(L: ID.Loc, Msg: "invalid cast opcode for cast from '" + |
4067 | getTypeString(T: SrcVal->getType()) + "' to '" + |
4068 | getTypeString(T: DestTy) + "'" ); |
4069 | ID.ConstantVal = ConstantExpr::getCast(ops: (Instruction::CastOps)Opc, |
4070 | C: SrcVal, Ty: DestTy); |
4071 | ID.Kind = ValID::t_Constant; |
4072 | return false; |
4073 | } |
4074 | case lltok::kw_extractvalue: |
4075 | return error(L: ID.Loc, Msg: "extractvalue constexprs are no longer supported" ); |
4076 | case lltok::kw_insertvalue: |
4077 | return error(L: ID.Loc, Msg: "insertvalue constexprs are no longer supported" ); |
4078 | case lltok::kw_udiv: |
4079 | return error(L: ID.Loc, Msg: "udiv constexprs are no longer supported" ); |
4080 | case lltok::kw_sdiv: |
4081 | return error(L: ID.Loc, Msg: "sdiv constexprs are no longer supported" ); |
4082 | case lltok::kw_urem: |
4083 | return error(L: ID.Loc, Msg: "urem constexprs are no longer supported" ); |
4084 | case lltok::kw_srem: |
4085 | return error(L: ID.Loc, Msg: "srem constexprs are no longer supported" ); |
4086 | case lltok::kw_fadd: |
4087 | return error(L: ID.Loc, Msg: "fadd constexprs are no longer supported" ); |
4088 | case lltok::kw_fsub: |
4089 | return error(L: ID.Loc, Msg: "fsub constexprs are no longer supported" ); |
4090 | case lltok::kw_fmul: |
4091 | return error(L: ID.Loc, Msg: "fmul constexprs are no longer supported" ); |
4092 | case lltok::kw_fdiv: |
4093 | return error(L: ID.Loc, Msg: "fdiv constexprs are no longer supported" ); |
4094 | case lltok::kw_frem: |
4095 | return error(L: ID.Loc, Msg: "frem constexprs are no longer supported" ); |
4096 | case lltok::kw_and: |
4097 | return error(L: ID.Loc, Msg: "and constexprs are no longer supported" ); |
4098 | case lltok::kw_or: |
4099 | return error(L: ID.Loc, Msg: "or constexprs are no longer supported" ); |
4100 | case lltok::kw_lshr: |
4101 | return error(L: ID.Loc, Msg: "lshr constexprs are no longer supported" ); |
4102 | case lltok::kw_ashr: |
4103 | return error(L: ID.Loc, Msg: "ashr constexprs are no longer supported" ); |
4104 | case lltok::kw_fneg: |
4105 | return error(L: ID.Loc, Msg: "fneg constexprs are no longer supported" ); |
4106 | case lltok::kw_select: |
4107 | return error(L: ID.Loc, Msg: "select constexprs are no longer supported" ); |
4108 | case lltok::kw_zext: |
4109 | return error(L: ID.Loc, Msg: "zext constexprs are no longer supported" ); |
4110 | case lltok::kw_sext: |
4111 | return error(L: ID.Loc, Msg: "sext constexprs are no longer supported" ); |
4112 | case lltok::kw_fptrunc: |
4113 | return error(L: ID.Loc, Msg: "fptrunc constexprs are no longer supported" ); |
4114 | case lltok::kw_fpext: |
4115 | return error(L: ID.Loc, Msg: "fpext constexprs are no longer supported" ); |
4116 | case lltok::kw_uitofp: |
4117 | return error(L: ID.Loc, Msg: "uitofp constexprs are no longer supported" ); |
4118 | case lltok::kw_sitofp: |
4119 | return error(L: ID.Loc, Msg: "sitofp constexprs are no longer supported" ); |
4120 | case lltok::kw_fptoui: |
4121 | return error(L: ID.Loc, Msg: "fptoui constexprs are no longer supported" ); |
4122 | case lltok::kw_fptosi: |
4123 | return error(L: ID.Loc, Msg: "fptosi constexprs are no longer supported" ); |
4124 | case lltok::kw_icmp: |
4125 | case lltok::kw_fcmp: { |
4126 | unsigned PredVal, Opc = Lex.getUIntVal(); |
4127 | Constant *Val0, *Val1; |
4128 | Lex.Lex(); |
4129 | if (parseCmpPredicate(P&: PredVal, Opc) || |
4130 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in compare constantexpr" ) || |
4131 | parseGlobalTypeAndValue(V&: Val0) || |
4132 | parseToken(T: lltok::comma, ErrMsg: "expected comma in compare constantexpr" ) || |
4133 | parseGlobalTypeAndValue(V&: Val1) || |
4134 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' in compare constantexpr" )) |
4135 | return true; |
4136 | |
4137 | if (Val0->getType() != Val1->getType()) |
4138 | return error(L: ID.Loc, Msg: "compare operands must have the same type" ); |
4139 | |
4140 | CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal; |
4141 | |
4142 | if (Opc == Instruction::FCmp) { |
4143 | if (!Val0->getType()->isFPOrFPVectorTy()) |
4144 | return error(L: ID.Loc, Msg: "fcmp requires floating point operands" ); |
4145 | ID.ConstantVal = ConstantExpr::getFCmp(pred: Pred, LHS: Val0, RHS: Val1); |
4146 | } else { |
4147 | assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!" ); |
4148 | if (!Val0->getType()->isIntOrIntVectorTy() && |
4149 | !Val0->getType()->isPtrOrPtrVectorTy()) |
4150 | return error(L: ID.Loc, Msg: "icmp requires pointer or integer operands" ); |
4151 | ID.ConstantVal = ConstantExpr::getICmp(pred: Pred, LHS: Val0, RHS: Val1); |
4152 | } |
4153 | ID.Kind = ValID::t_Constant; |
4154 | return false; |
4155 | } |
4156 | |
4157 | // Binary Operators. |
4158 | case lltok::kw_add: |
4159 | case lltok::kw_sub: |
4160 | case lltok::kw_mul: |
4161 | case lltok::kw_shl: |
4162 | case lltok::kw_xor: { |
4163 | bool NUW = false; |
4164 | bool NSW = false; |
4165 | unsigned Opc = Lex.getUIntVal(); |
4166 | Constant *Val0, *Val1; |
4167 | Lex.Lex(); |
4168 | if (Opc == Instruction::Add || Opc == Instruction::Sub || |
4169 | Opc == Instruction::Mul || Opc == Instruction::Shl) { |
4170 | if (EatIfPresent(T: lltok::kw_nuw)) |
4171 | NUW = true; |
4172 | if (EatIfPresent(T: lltok::kw_nsw)) { |
4173 | NSW = true; |
4174 | if (EatIfPresent(T: lltok::kw_nuw)) |
4175 | NUW = true; |
4176 | } |
4177 | } |
4178 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in binary constantexpr" ) || |
4179 | parseGlobalTypeAndValue(V&: Val0) || |
4180 | parseToken(T: lltok::comma, ErrMsg: "expected comma in binary constantexpr" ) || |
4181 | parseGlobalTypeAndValue(V&: Val1) || |
4182 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' in binary constantexpr" )) |
4183 | return true; |
4184 | if (Val0->getType() != Val1->getType()) |
4185 | return error(L: ID.Loc, Msg: "operands of constexpr must have same type" ); |
4186 | // Check that the type is valid for the operator. |
4187 | if (!Val0->getType()->isIntOrIntVectorTy()) |
4188 | return error(L: ID.Loc, |
4189 | Msg: "constexpr requires integer or integer vector operands" ); |
4190 | unsigned Flags = 0; |
4191 | if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap; |
4192 | if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap; |
4193 | ID.ConstantVal = ConstantExpr::get(Opcode: Opc, C1: Val0, C2: Val1, Flags); |
4194 | ID.Kind = ValID::t_Constant; |
4195 | return false; |
4196 | } |
4197 | |
4198 | case lltok::kw_splat: { |
4199 | Lex.Lex(); |
4200 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' after vector splat" )) |
4201 | return true; |
4202 | Constant *C; |
4203 | if (parseGlobalTypeAndValue(V&: C)) |
4204 | return true; |
4205 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' at end of vector splat" )) |
4206 | return true; |
4207 | |
4208 | ID.ConstantVal = C; |
4209 | ID.Kind = ValID::t_ConstantSplat; |
4210 | return false; |
4211 | } |
4212 | |
4213 | case lltok::kw_getelementptr: |
4214 | case lltok::kw_shufflevector: |
4215 | case lltok::kw_insertelement: |
4216 | case lltok::kw_extractelement: { |
4217 | unsigned Opc = Lex.getUIntVal(); |
4218 | SmallVector<Constant*, 16> Elts; |
4219 | bool InBounds = false; |
4220 | bool HasInRange = false; |
4221 | APSInt InRangeStart; |
4222 | APSInt InRangeEnd; |
4223 | Type *Ty; |
4224 | Lex.Lex(); |
4225 | |
4226 | if (Opc == Instruction::GetElementPtr) { |
4227 | InBounds = EatIfPresent(T: lltok::kw_inbounds); |
4228 | if (EatIfPresent(T: lltok::kw_inrange)) { |
4229 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '('" )) |
4230 | return true; |
4231 | if (Lex.getKind() != lltok::APSInt) |
4232 | return tokError(Msg: "expected integer" ); |
4233 | InRangeStart = Lex.getAPSIntVal(); |
4234 | Lex.Lex(); |
4235 | if (parseToken(T: lltok::comma, ErrMsg: "expected ','" )) |
4236 | return true; |
4237 | if (Lex.getKind() != lltok::APSInt) |
4238 | return tokError(Msg: "expected integer" ); |
4239 | InRangeEnd = Lex.getAPSIntVal(); |
4240 | Lex.Lex(); |
4241 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')'" )) |
4242 | return true; |
4243 | HasInRange = true; |
4244 | } |
4245 | } |
4246 | |
4247 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in constantexpr" )) |
4248 | return true; |
4249 | |
4250 | if (Opc == Instruction::GetElementPtr) { |
4251 | if (parseType(Result&: Ty) || |
4252 | parseToken(T: lltok::comma, ErrMsg: "expected comma after getelementptr's type" )) |
4253 | return true; |
4254 | } |
4255 | |
4256 | if (parseGlobalValueVector(Elts) || |
4257 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' in constantexpr" )) |
4258 | return true; |
4259 | |
4260 | if (Opc == Instruction::GetElementPtr) { |
4261 | if (Elts.size() == 0 || |
4262 | !Elts[0]->getType()->isPtrOrPtrVectorTy()) |
4263 | return error(L: ID.Loc, Msg: "base of getelementptr must be a pointer" ); |
4264 | |
4265 | Type *BaseType = Elts[0]->getType(); |
4266 | std::optional<ConstantRange> InRange; |
4267 | if (HasInRange) { |
4268 | unsigned IndexWidth = |
4269 | M->getDataLayout().getIndexTypeSizeInBits(Ty: BaseType); |
4270 | InRangeStart = InRangeStart.extOrTrunc(width: IndexWidth); |
4271 | InRangeEnd = InRangeEnd.extOrTrunc(width: IndexWidth); |
4272 | if (InRangeStart.sge(RHS: InRangeEnd)) |
4273 | return error(L: ID.Loc, Msg: "expected end to be larger than start" ); |
4274 | InRange = ConstantRange::getNonEmpty(Lower: InRangeStart, Upper: InRangeEnd); |
4275 | } |
4276 | |
4277 | unsigned GEPWidth = |
4278 | BaseType->isVectorTy() |
4279 | ? cast<FixedVectorType>(Val: BaseType)->getNumElements() |
4280 | : 0; |
4281 | |
4282 | ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); |
4283 | for (Constant *Val : Indices) { |
4284 | Type *ValTy = Val->getType(); |
4285 | if (!ValTy->isIntOrIntVectorTy()) |
4286 | return error(L: ID.Loc, Msg: "getelementptr index must be an integer" ); |
4287 | if (auto *ValVTy = dyn_cast<VectorType>(Val: ValTy)) { |
4288 | unsigned ValNumEl = cast<FixedVectorType>(Val: ValVTy)->getNumElements(); |
4289 | if (GEPWidth && (ValNumEl != GEPWidth)) |
4290 | return error( |
4291 | L: ID.Loc, |
4292 | Msg: "getelementptr vector index has a wrong number of elements" ); |
4293 | // GEPWidth may have been unknown because the base is a scalar, |
4294 | // but it is known now. |
4295 | GEPWidth = ValNumEl; |
4296 | } |
4297 | } |
4298 | |
4299 | SmallPtrSet<Type*, 4> Visited; |
4300 | if (!Indices.empty() && !Ty->isSized(Visited: &Visited)) |
4301 | return error(L: ID.Loc, Msg: "base element of getelementptr must be sized" ); |
4302 | |
4303 | if (!GetElementPtrInst::getIndexedType(Ty, IdxList: Indices)) |
4304 | return error(L: ID.Loc, Msg: "invalid getelementptr indices" ); |
4305 | |
4306 | ID.ConstantVal = ConstantExpr::getGetElementPtr(Ty, C: Elts[0], IdxList: Indices, |
4307 | InBounds, InRange); |
4308 | } else if (Opc == Instruction::ShuffleVector) { |
4309 | if (Elts.size() != 3) |
4310 | return error(L: ID.Loc, Msg: "expected three operands to shufflevector" ); |
4311 | if (!ShuffleVectorInst::isValidOperands(V1: Elts[0], V2: Elts[1], Mask: Elts[2])) |
4312 | return error(L: ID.Loc, Msg: "invalid operands to shufflevector" ); |
4313 | SmallVector<int, 16> Mask; |
4314 | ShuffleVectorInst::getShuffleMask(Mask: cast<Constant>(Val: Elts[2]), Result&: Mask); |
4315 | ID.ConstantVal = ConstantExpr::getShuffleVector(V1: Elts[0], V2: Elts[1], Mask); |
4316 | } else if (Opc == Instruction::ExtractElement) { |
4317 | if (Elts.size() != 2) |
4318 | return error(L: ID.Loc, Msg: "expected two operands to extractelement" ); |
4319 | if (!ExtractElementInst::isValidOperands(Vec: Elts[0], Idx: Elts[1])) |
4320 | return error(L: ID.Loc, Msg: "invalid extractelement operands" ); |
4321 | ID.ConstantVal = ConstantExpr::getExtractElement(Vec: Elts[0], Idx: Elts[1]); |
4322 | } else { |
4323 | assert(Opc == Instruction::InsertElement && "Unknown opcode" ); |
4324 | if (Elts.size() != 3) |
4325 | return error(L: ID.Loc, Msg: "expected three operands to insertelement" ); |
4326 | if (!InsertElementInst::isValidOperands(Vec: Elts[0], NewElt: Elts[1], Idx: Elts[2])) |
4327 | return error(L: ID.Loc, Msg: "invalid insertelement operands" ); |
4328 | ID.ConstantVal = |
4329 | ConstantExpr::getInsertElement(Vec: Elts[0], Elt: Elts[1],Idx: Elts[2]); |
4330 | } |
4331 | |
4332 | ID.Kind = ValID::t_Constant; |
4333 | return false; |
4334 | } |
4335 | } |
4336 | |
4337 | Lex.Lex(); |
4338 | return false; |
4339 | } |
4340 | |
4341 | /// parseGlobalValue - parse a global value with the specified type. |
4342 | bool LLParser::parseGlobalValue(Type *Ty, Constant *&C) { |
4343 | C = nullptr; |
4344 | ValID ID; |
4345 | Value *V = nullptr; |
4346 | bool Parsed = parseValID(ID, /*PFS=*/nullptr, ExpectedTy: Ty) || |
4347 | convertValIDToValue(Ty, ID, V, PFS: nullptr); |
4348 | if (V && !(C = dyn_cast<Constant>(Val: V))) |
4349 | return error(L: ID.Loc, Msg: "global values must be constants" ); |
4350 | return Parsed; |
4351 | } |
4352 | |
4353 | bool LLParser::parseGlobalTypeAndValue(Constant *&V) { |
4354 | Type *Ty = nullptr; |
4355 | return parseType(Result&: Ty) || parseGlobalValue(Ty, C&: V); |
4356 | } |
4357 | |
4358 | bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) { |
4359 | C = nullptr; |
4360 | |
4361 | LocTy KwLoc = Lex.getLoc(); |
4362 | if (!EatIfPresent(T: lltok::kw_comdat)) |
4363 | return false; |
4364 | |
4365 | if (EatIfPresent(T: lltok::lparen)) { |
4366 | if (Lex.getKind() != lltok::ComdatVar) |
4367 | return tokError(Msg: "expected comdat variable" ); |
4368 | C = getComdat(Name: Lex.getStrVal(), Loc: Lex.getLoc()); |
4369 | Lex.Lex(); |
4370 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' after comdat var" )) |
4371 | return true; |
4372 | } else { |
4373 | if (GlobalName.empty()) |
4374 | return tokError(Msg: "comdat cannot be unnamed" ); |
4375 | C = getComdat(Name: std::string(GlobalName), Loc: KwLoc); |
4376 | } |
4377 | |
4378 | return false; |
4379 | } |
4380 | |
4381 | /// parseGlobalValueVector |
4382 | /// ::= /*empty*/ |
4383 | /// ::= TypeAndValue (',' TypeAndValue)* |
4384 | bool LLParser::parseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) { |
4385 | // Empty list. |
4386 | if (Lex.getKind() == lltok::rbrace || |
4387 | Lex.getKind() == lltok::rsquare || |
4388 | Lex.getKind() == lltok::greater || |
4389 | Lex.getKind() == lltok::rparen) |
4390 | return false; |
4391 | |
4392 | do { |
4393 | // Let the caller deal with inrange. |
4394 | if (Lex.getKind() == lltok::kw_inrange) |
4395 | return false; |
4396 | |
4397 | Constant *C; |
4398 | if (parseGlobalTypeAndValue(V&: C)) |
4399 | return true; |
4400 | Elts.push_back(Elt: C); |
4401 | } while (EatIfPresent(T: lltok::comma)); |
4402 | |
4403 | return false; |
4404 | } |
4405 | |
4406 | bool LLParser::parseMDTuple(MDNode *&MD, bool IsDistinct) { |
4407 | SmallVector<Metadata *, 16> Elts; |
4408 | if (parseMDNodeVector(Elts)) |
4409 | return true; |
4410 | |
4411 | MD = (IsDistinct ? MDTuple::getDistinct : MDTuple::get)(Context, Elts); |
4412 | return false; |
4413 | } |
4414 | |
4415 | /// MDNode: |
4416 | /// ::= !{ ... } |
4417 | /// ::= !7 |
4418 | /// ::= !DILocation(...) |
4419 | bool LLParser::parseMDNode(MDNode *&N) { |
4420 | if (Lex.getKind() == lltok::MetadataVar) |
4421 | return parseSpecializedMDNode(N); |
4422 | |
4423 | return parseToken(T: lltok::exclaim, ErrMsg: "expected '!' here" ) || parseMDNodeTail(N); |
4424 | } |
4425 | |
4426 | bool LLParser::parseMDNodeTail(MDNode *&N) { |
4427 | // !{ ... } |
4428 | if (Lex.getKind() == lltok::lbrace) |
4429 | return parseMDTuple(MD&: N); |
4430 | |
4431 | // !42 |
4432 | return parseMDNodeID(Result&: N); |
4433 | } |
4434 | |
4435 | namespace { |
4436 | |
4437 | /// Structure to represent an optional metadata field. |
4438 | template <class FieldTy> struct MDFieldImpl { |
4439 | typedef MDFieldImpl ImplTy; |
4440 | FieldTy Val; |
4441 | bool Seen; |
4442 | |
4443 | void assign(FieldTy Val) { |
4444 | Seen = true; |
4445 | this->Val = std::move(Val); |
4446 | } |
4447 | |
4448 | explicit MDFieldImpl(FieldTy Default) |
4449 | : Val(std::move(Default)), Seen(false) {} |
4450 | }; |
4451 | |
4452 | /// Structure to represent an optional metadata field that |
4453 | /// can be of either type (A or B) and encapsulates the |
4454 | /// MD<typeofA>Field and MD<typeofB>Field structs, so not |
4455 | /// to reimplement the specifics for representing each Field. |
4456 | template <class FieldTypeA, class FieldTypeB> struct MDEitherFieldImpl { |
4457 | typedef MDEitherFieldImpl<FieldTypeA, FieldTypeB> ImplTy; |
4458 | FieldTypeA A; |
4459 | FieldTypeB B; |
4460 | bool Seen; |
4461 | |
4462 | enum { |
4463 | IsInvalid = 0, |
4464 | IsTypeA = 1, |
4465 | IsTypeB = 2 |
4466 | } WhatIs; |
4467 | |
4468 | void assign(FieldTypeA A) { |
4469 | Seen = true; |
4470 | this->A = std::move(A); |
4471 | WhatIs = IsTypeA; |
4472 | } |
4473 | |
4474 | void assign(FieldTypeB B) { |
4475 | Seen = true; |
4476 | this->B = std::move(B); |
4477 | WhatIs = IsTypeB; |
4478 | } |
4479 | |
4480 | explicit MDEitherFieldImpl(FieldTypeA DefaultA, FieldTypeB DefaultB) |
4481 | : A(std::move(DefaultA)), B(std::move(DefaultB)), Seen(false), |
4482 | WhatIs(IsInvalid) {} |
4483 | }; |
4484 | |
4485 | struct MDUnsignedField : public MDFieldImpl<uint64_t> { |
4486 | uint64_t Max; |
4487 | |
4488 | MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX) |
4489 | : ImplTy(Default), Max(Max) {} |
4490 | }; |
4491 | |
4492 | struct LineField : public MDUnsignedField { |
4493 | LineField() : MDUnsignedField(0, UINT32_MAX) {} |
4494 | }; |
4495 | |
4496 | struct ColumnField : public MDUnsignedField { |
4497 | ColumnField() : MDUnsignedField(0, UINT16_MAX) {} |
4498 | }; |
4499 | |
4500 | struct DwarfTagField : public MDUnsignedField { |
4501 | DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {} |
4502 | DwarfTagField(dwarf::Tag DefaultTag) |
4503 | : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {} |
4504 | }; |
4505 | |
4506 | struct DwarfMacinfoTypeField : public MDUnsignedField { |
4507 | DwarfMacinfoTypeField() : MDUnsignedField(0, dwarf::DW_MACINFO_vendor_ext) {} |
4508 | DwarfMacinfoTypeField(dwarf::MacinfoRecordType DefaultType) |
4509 | : MDUnsignedField(DefaultType, dwarf::DW_MACINFO_vendor_ext) {} |
4510 | }; |
4511 | |
4512 | struct DwarfAttEncodingField : public MDUnsignedField { |
4513 | DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {} |
4514 | }; |
4515 | |
4516 | struct DwarfVirtualityField : public MDUnsignedField { |
4517 | DwarfVirtualityField() : MDUnsignedField(0, dwarf::DW_VIRTUALITY_max) {} |
4518 | }; |
4519 | |
4520 | struct DwarfLangField : public MDUnsignedField { |
4521 | DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {} |
4522 | }; |
4523 | |
4524 | struct DwarfCCField : public MDUnsignedField { |
4525 | DwarfCCField() : MDUnsignedField(0, dwarf::DW_CC_hi_user) {} |
4526 | }; |
4527 | |
4528 | struct EmissionKindField : public MDUnsignedField { |
4529 | EmissionKindField() : MDUnsignedField(0, DICompileUnit::LastEmissionKind) {} |
4530 | }; |
4531 | |
4532 | struct NameTableKindField : public MDUnsignedField { |
4533 | NameTableKindField() |
4534 | : MDUnsignedField( |
4535 | 0, (unsigned) |
4536 | DICompileUnit::DebugNameTableKind::LastDebugNameTableKind) {} |
4537 | }; |
4538 | |
4539 | struct DIFlagField : public MDFieldImpl<DINode::DIFlags> { |
4540 | DIFlagField() : MDFieldImpl(DINode::FlagZero) {} |
4541 | }; |
4542 | |
4543 | struct DISPFlagField : public MDFieldImpl<DISubprogram::DISPFlags> { |
4544 | DISPFlagField() : MDFieldImpl(DISubprogram::SPFlagZero) {} |
4545 | }; |
4546 | |
4547 | struct MDAPSIntField : public MDFieldImpl<APSInt> { |
4548 | MDAPSIntField() : ImplTy(APSInt()) {} |
4549 | }; |
4550 | |
4551 | struct MDSignedField : public MDFieldImpl<int64_t> { |
4552 | int64_t Min = INT64_MIN; |
4553 | int64_t Max = INT64_MAX; |
4554 | |
4555 | MDSignedField(int64_t Default = 0) |
4556 | : ImplTy(Default) {} |
4557 | MDSignedField(int64_t Default, int64_t Min, int64_t Max) |
4558 | : ImplTy(Default), Min(Min), Max(Max) {} |
4559 | }; |
4560 | |
4561 | struct MDBoolField : public MDFieldImpl<bool> { |
4562 | MDBoolField(bool Default = false) : ImplTy(Default) {} |
4563 | }; |
4564 | |
4565 | struct MDField : public MDFieldImpl<Metadata *> { |
4566 | bool AllowNull; |
4567 | |
4568 | MDField(bool AllowNull = true) : ImplTy(nullptr), AllowNull(AllowNull) {} |
4569 | }; |
4570 | |
4571 | struct MDStringField : public MDFieldImpl<MDString *> { |
4572 | bool AllowEmpty; |
4573 | MDStringField(bool AllowEmpty = true) |
4574 | : ImplTy(nullptr), AllowEmpty(AllowEmpty) {} |
4575 | }; |
4576 | |
4577 | struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> { |
4578 | MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {} |
4579 | }; |
4580 | |
4581 | struct ChecksumKindField : public MDFieldImpl<DIFile::ChecksumKind> { |
4582 | ChecksumKindField(DIFile::ChecksumKind CSKind) : ImplTy(CSKind) {} |
4583 | }; |
4584 | |
4585 | struct MDSignedOrMDField : MDEitherFieldImpl<MDSignedField, MDField> { |
4586 | MDSignedOrMDField(int64_t Default = 0, bool AllowNull = true) |
4587 | : ImplTy(MDSignedField(Default), MDField(AllowNull)) {} |
4588 | |
4589 | MDSignedOrMDField(int64_t Default, int64_t Min, int64_t Max, |
4590 | bool AllowNull = true) |
4591 | : ImplTy(MDSignedField(Default, Min, Max), MDField(AllowNull)) {} |
4592 | |
4593 | bool isMDSignedField() const { return WhatIs == IsTypeA; } |
4594 | bool isMDField() const { return WhatIs == IsTypeB; } |
4595 | int64_t getMDSignedValue() const { |
4596 | assert(isMDSignedField() && "Wrong field type" ); |
4597 | return A.Val; |
4598 | } |
4599 | Metadata *getMDFieldValue() const { |
4600 | assert(isMDField() && "Wrong field type" ); |
4601 | return B.Val; |
4602 | } |
4603 | }; |
4604 | |
4605 | } // end anonymous namespace |
4606 | |
4607 | namespace llvm { |
4608 | |
4609 | template <> |
4610 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDAPSIntField &Result) { |
4611 | if (Lex.getKind() != lltok::APSInt) |
4612 | return tokError(Msg: "expected integer" ); |
4613 | |
4614 | Result.assign(Val: Lex.getAPSIntVal()); |
4615 | Lex.Lex(); |
4616 | return false; |
4617 | } |
4618 | |
4619 | template <> |
4620 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
4621 | MDUnsignedField &Result) { |
4622 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
4623 | return tokError(Msg: "expected unsigned integer" ); |
4624 | |
4625 | auto &U = Lex.getAPSIntVal(); |
4626 | if (U.ugt(RHS: Result.Max)) |
4627 | return tokError(Msg: "value for '" + Name + "' too large, limit is " + |
4628 | Twine(Result.Max)); |
4629 | Result.assign(Val: U.getZExtValue()); |
4630 | assert(Result.Val <= Result.Max && "Expected value in range" ); |
4631 | Lex.Lex(); |
4632 | return false; |
4633 | } |
4634 | |
4635 | template <> |
4636 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, LineField &Result) { |
4637 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
4638 | } |
4639 | template <> |
4640 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, ColumnField &Result) { |
4641 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
4642 | } |
4643 | |
4644 | template <> |
4645 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) { |
4646 | if (Lex.getKind() == lltok::APSInt) |
4647 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
4648 | |
4649 | if (Lex.getKind() != lltok::DwarfTag) |
4650 | return tokError(Msg: "expected DWARF tag" ); |
4651 | |
4652 | unsigned Tag = dwarf::getTag(TagString: Lex.getStrVal()); |
4653 | if (Tag == dwarf::DW_TAG_invalid) |
4654 | return tokError(Msg: "invalid DWARF tag" + Twine(" '" ) + Lex.getStrVal() + "'" ); |
4655 | assert(Tag <= Result.Max && "Expected valid DWARF tag" ); |
4656 | |
4657 | Result.assign(Val: Tag); |
4658 | Lex.Lex(); |
4659 | return false; |
4660 | } |
4661 | |
4662 | template <> |
4663 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
4664 | DwarfMacinfoTypeField &Result) { |
4665 | if (Lex.getKind() == lltok::APSInt) |
4666 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
4667 | |
4668 | if (Lex.getKind() != lltok::DwarfMacinfo) |
4669 | return tokError(Msg: "expected DWARF macinfo type" ); |
4670 | |
4671 | unsigned Macinfo = dwarf::getMacinfo(MacinfoString: Lex.getStrVal()); |
4672 | if (Macinfo == dwarf::DW_MACINFO_invalid) |
4673 | return tokError(Msg: "invalid DWARF macinfo type" + Twine(" '" ) + |
4674 | Lex.getStrVal() + "'" ); |
4675 | assert(Macinfo <= Result.Max && "Expected valid DWARF macinfo type" ); |
4676 | |
4677 | Result.assign(Val: Macinfo); |
4678 | Lex.Lex(); |
4679 | return false; |
4680 | } |
4681 | |
4682 | template <> |
4683 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
4684 | DwarfVirtualityField &Result) { |
4685 | if (Lex.getKind() == lltok::APSInt) |
4686 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
4687 | |
4688 | if (Lex.getKind() != lltok::DwarfVirtuality) |
4689 | return tokError(Msg: "expected DWARF virtuality code" ); |
4690 | |
4691 | unsigned Virtuality = dwarf::getVirtuality(VirtualityString: Lex.getStrVal()); |
4692 | if (Virtuality == dwarf::DW_VIRTUALITY_invalid) |
4693 | return tokError(Msg: "invalid DWARF virtuality code" + Twine(" '" ) + |
4694 | Lex.getStrVal() + "'" ); |
4695 | assert(Virtuality <= Result.Max && "Expected valid DWARF virtuality code" ); |
4696 | Result.assign(Val: Virtuality); |
4697 | Lex.Lex(); |
4698 | return false; |
4699 | } |
4700 | |
4701 | template <> |
4702 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) { |
4703 | if (Lex.getKind() == lltok::APSInt) |
4704 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
4705 | |
4706 | if (Lex.getKind() != lltok::DwarfLang) |
4707 | return tokError(Msg: "expected DWARF language" ); |
4708 | |
4709 | unsigned Lang = dwarf::getLanguage(LanguageString: Lex.getStrVal()); |
4710 | if (!Lang) |
4711 | return tokError(Msg: "invalid DWARF language" + Twine(" '" ) + Lex.getStrVal() + |
4712 | "'" ); |
4713 | assert(Lang <= Result.Max && "Expected valid DWARF language" ); |
4714 | Result.assign(Val: Lang); |
4715 | Lex.Lex(); |
4716 | return false; |
4717 | } |
4718 | |
4719 | template <> |
4720 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfCCField &Result) { |
4721 | if (Lex.getKind() == lltok::APSInt) |
4722 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
4723 | |
4724 | if (Lex.getKind() != lltok::DwarfCC) |
4725 | return tokError(Msg: "expected DWARF calling convention" ); |
4726 | |
4727 | unsigned CC = dwarf::getCallingConvention(LanguageString: Lex.getStrVal()); |
4728 | if (!CC) |
4729 | return tokError(Msg: "invalid DWARF calling convention" + Twine(" '" ) + |
4730 | Lex.getStrVal() + "'" ); |
4731 | assert(CC <= Result.Max && "Expected valid DWARF calling convention" ); |
4732 | Result.assign(Val: CC); |
4733 | Lex.Lex(); |
4734 | return false; |
4735 | } |
4736 | |
4737 | template <> |
4738 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
4739 | EmissionKindField &Result) { |
4740 | if (Lex.getKind() == lltok::APSInt) |
4741 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
4742 | |
4743 | if (Lex.getKind() != lltok::EmissionKind) |
4744 | return tokError(Msg: "expected emission kind" ); |
4745 | |
4746 | auto Kind = DICompileUnit::getEmissionKind(Str: Lex.getStrVal()); |
4747 | if (!Kind) |
4748 | return tokError(Msg: "invalid emission kind" + Twine(" '" ) + Lex.getStrVal() + |
4749 | "'" ); |
4750 | assert(*Kind <= Result.Max && "Expected valid emission kind" ); |
4751 | Result.assign(Val: *Kind); |
4752 | Lex.Lex(); |
4753 | return false; |
4754 | } |
4755 | |
4756 | template <> |
4757 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
4758 | NameTableKindField &Result) { |
4759 | if (Lex.getKind() == lltok::APSInt) |
4760 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
4761 | |
4762 | if (Lex.getKind() != lltok::NameTableKind) |
4763 | return tokError(Msg: "expected nameTable kind" ); |
4764 | |
4765 | auto Kind = DICompileUnit::getNameTableKind(Str: Lex.getStrVal()); |
4766 | if (!Kind) |
4767 | return tokError(Msg: "invalid nameTable kind" + Twine(" '" ) + Lex.getStrVal() + |
4768 | "'" ); |
4769 | assert(((unsigned)*Kind) <= Result.Max && "Expected valid nameTable kind" ); |
4770 | Result.assign(Val: (unsigned)*Kind); |
4771 | Lex.Lex(); |
4772 | return false; |
4773 | } |
4774 | |
4775 | template <> |
4776 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
4777 | DwarfAttEncodingField &Result) { |
4778 | if (Lex.getKind() == lltok::APSInt) |
4779 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
4780 | |
4781 | if (Lex.getKind() != lltok::DwarfAttEncoding) |
4782 | return tokError(Msg: "expected DWARF type attribute encoding" ); |
4783 | |
4784 | unsigned Encoding = dwarf::getAttributeEncoding(EncodingString: Lex.getStrVal()); |
4785 | if (!Encoding) |
4786 | return tokError(Msg: "invalid DWARF type attribute encoding" + Twine(" '" ) + |
4787 | Lex.getStrVal() + "'" ); |
4788 | assert(Encoding <= Result.Max && "Expected valid DWARF language" ); |
4789 | Result.assign(Val: Encoding); |
4790 | Lex.Lex(); |
4791 | return false; |
4792 | } |
4793 | |
4794 | /// DIFlagField |
4795 | /// ::= uint32 |
4796 | /// ::= DIFlagVector |
4797 | /// ::= DIFlagVector '|' DIFlagFwdDecl '|' uint32 '|' DIFlagPublic |
4798 | template <> |
4799 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, DIFlagField &Result) { |
4800 | |
4801 | // parser for a single flag. |
4802 | auto parseFlag = [&](DINode::DIFlags &Val) { |
4803 | if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) { |
4804 | uint32_t TempVal = static_cast<uint32_t>(Val); |
4805 | bool Res = parseUInt32(Val&: TempVal); |
4806 | Val = static_cast<DINode::DIFlags>(TempVal); |
4807 | return Res; |
4808 | } |
4809 | |
4810 | if (Lex.getKind() != lltok::DIFlag) |
4811 | return tokError(Msg: "expected debug info flag" ); |
4812 | |
4813 | Val = DINode::getFlag(Flag: Lex.getStrVal()); |
4814 | if (!Val) |
4815 | return tokError(Msg: Twine("invalid debug info flag '" ) + Lex.getStrVal() + |
4816 | "'" ); |
4817 | Lex.Lex(); |
4818 | return false; |
4819 | }; |
4820 | |
4821 | // parse the flags and combine them together. |
4822 | DINode::DIFlags Combined = DINode::FlagZero; |
4823 | do { |
4824 | DINode::DIFlags Val; |
4825 | if (parseFlag(Val)) |
4826 | return true; |
4827 | Combined |= Val; |
4828 | } while (EatIfPresent(T: lltok::bar)); |
4829 | |
4830 | Result.assign(Val: Combined); |
4831 | return false; |
4832 | } |
4833 | |
4834 | /// DISPFlagField |
4835 | /// ::= uint32 |
4836 | /// ::= DISPFlagVector |
4837 | /// ::= DISPFlagVector '|' DISPFlag* '|' uint32 |
4838 | template <> |
4839 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, DISPFlagField &Result) { |
4840 | |
4841 | // parser for a single flag. |
4842 | auto parseFlag = [&](DISubprogram::DISPFlags &Val) { |
4843 | if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) { |
4844 | uint32_t TempVal = static_cast<uint32_t>(Val); |
4845 | bool Res = parseUInt32(Val&: TempVal); |
4846 | Val = static_cast<DISubprogram::DISPFlags>(TempVal); |
4847 | return Res; |
4848 | } |
4849 | |
4850 | if (Lex.getKind() != lltok::DISPFlag) |
4851 | return tokError(Msg: "expected debug info flag" ); |
4852 | |
4853 | Val = DISubprogram::getFlag(Flag: Lex.getStrVal()); |
4854 | if (!Val) |
4855 | return tokError(Msg: Twine("invalid subprogram debug info flag '" ) + |
4856 | Lex.getStrVal() + "'" ); |
4857 | Lex.Lex(); |
4858 | return false; |
4859 | }; |
4860 | |
4861 | // parse the flags and combine them together. |
4862 | DISubprogram::DISPFlags Combined = DISubprogram::SPFlagZero; |
4863 | do { |
4864 | DISubprogram::DISPFlags Val; |
4865 | if (parseFlag(Val)) |
4866 | return true; |
4867 | Combined |= Val; |
4868 | } while (EatIfPresent(T: lltok::bar)); |
4869 | |
4870 | Result.assign(Val: Combined); |
4871 | return false; |
4872 | } |
4873 | |
4874 | template <> |
4875 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDSignedField &Result) { |
4876 | if (Lex.getKind() != lltok::APSInt) |
4877 | return tokError(Msg: "expected signed integer" ); |
4878 | |
4879 | auto &S = Lex.getAPSIntVal(); |
4880 | if (S < Result.Min) |
4881 | return tokError(Msg: "value for '" + Name + "' too small, limit is " + |
4882 | Twine(Result.Min)); |
4883 | if (S > Result.Max) |
4884 | return tokError(Msg: "value for '" + Name + "' too large, limit is " + |
4885 | Twine(Result.Max)); |
4886 | Result.assign(Val: S.getExtValue()); |
4887 | assert(Result.Val >= Result.Min && "Expected value in range" ); |
4888 | assert(Result.Val <= Result.Max && "Expected value in range" ); |
4889 | Lex.Lex(); |
4890 | return false; |
4891 | } |
4892 | |
4893 | template <> |
4894 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) { |
4895 | switch (Lex.getKind()) { |
4896 | default: |
4897 | return tokError(Msg: "expected 'true' or 'false'" ); |
4898 | case lltok::kw_true: |
4899 | Result.assign(Val: true); |
4900 | break; |
4901 | case lltok::kw_false: |
4902 | Result.assign(Val: false); |
4903 | break; |
4904 | } |
4905 | Lex.Lex(); |
4906 | return false; |
4907 | } |
4908 | |
4909 | template <> |
4910 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDField &Result) { |
4911 | if (Lex.getKind() == lltok::kw_null) { |
4912 | if (!Result.AllowNull) |
4913 | return tokError(Msg: "'" + Name + "' cannot be null" ); |
4914 | Lex.Lex(); |
4915 | Result.assign(Val: nullptr); |
4916 | return false; |
4917 | } |
4918 | |
4919 | Metadata *MD; |
4920 | if (parseMetadata(MD, PFS: nullptr)) |
4921 | return true; |
4922 | |
4923 | Result.assign(Val: MD); |
4924 | return false; |
4925 | } |
4926 | |
4927 | template <> |
4928 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
4929 | MDSignedOrMDField &Result) { |
4930 | // Try to parse a signed int. |
4931 | if (Lex.getKind() == lltok::APSInt) { |
4932 | MDSignedField Res = Result.A; |
4933 | if (!parseMDField(Loc, Name, Result&: Res)) { |
4934 | Result.assign(A: Res); |
4935 | return false; |
4936 | } |
4937 | return true; |
4938 | } |
4939 | |
4940 | // Otherwise, try to parse as an MDField. |
4941 | MDField Res = Result.B; |
4942 | if (!parseMDField(Loc, Name, Result&: Res)) { |
4943 | Result.assign(B: Res); |
4944 | return false; |
4945 | } |
4946 | |
4947 | return true; |
4948 | } |
4949 | |
4950 | template <> |
4951 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDStringField &Result) { |
4952 | LocTy ValueLoc = Lex.getLoc(); |
4953 | std::string S; |
4954 | if (parseStringConstant(Result&: S)) |
4955 | return true; |
4956 | |
4957 | if (!Result.AllowEmpty && S.empty()) |
4958 | return error(L: ValueLoc, Msg: "'" + Name + "' cannot be empty" ); |
4959 | |
4960 | Result.assign(Val: S.empty() ? nullptr : MDString::get(Context, Str: S)); |
4961 | return false; |
4962 | } |
4963 | |
4964 | template <> |
4965 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) { |
4966 | SmallVector<Metadata *, 4> MDs; |
4967 | if (parseMDNodeVector(Elts&: MDs)) |
4968 | return true; |
4969 | |
4970 | Result.assign(Val: std::move(MDs)); |
4971 | return false; |
4972 | } |
4973 | |
4974 | template <> |
4975 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
4976 | ChecksumKindField &Result) { |
4977 | std::optional<DIFile::ChecksumKind> CSKind = |
4978 | DIFile::getChecksumKind(CSKindStr: Lex.getStrVal()); |
4979 | |
4980 | if (Lex.getKind() != lltok::ChecksumKind || !CSKind) |
4981 | return tokError(Msg: "invalid checksum kind" + Twine(" '" ) + Lex.getStrVal() + |
4982 | "'" ); |
4983 | |
4984 | Result.assign(Val: *CSKind); |
4985 | Lex.Lex(); |
4986 | return false; |
4987 | } |
4988 | |
4989 | } // end namespace llvm |
4990 | |
4991 | template <class ParserTy> |
4992 | bool LLParser::parseMDFieldsImplBody(ParserTy ParseField) { |
4993 | do { |
4994 | if (Lex.getKind() != lltok::LabelStr) |
4995 | return tokError(Msg: "expected field label here" ); |
4996 | |
4997 | if (ParseField()) |
4998 | return true; |
4999 | } while (EatIfPresent(T: lltok::comma)); |
5000 | |
5001 | return false; |
5002 | } |
5003 | |
5004 | template <class ParserTy> |
5005 | bool LLParser::parseMDFieldsImpl(ParserTy ParseField, LocTy &ClosingLoc) { |
5006 | assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name" ); |
5007 | Lex.Lex(); |
5008 | |
5009 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
5010 | return true; |
5011 | if (Lex.getKind() != lltok::rparen) |
5012 | if (parseMDFieldsImplBody(ParseField)) |
5013 | return true; |
5014 | |
5015 | ClosingLoc = Lex.getLoc(); |
5016 | return parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" ); |
5017 | } |
5018 | |
5019 | template <class FieldTy> |
5020 | bool LLParser::parseMDField(StringRef Name, FieldTy &Result) { |
5021 | if (Result.Seen) |
5022 | return tokError(Msg: "field '" + Name + "' cannot be specified more than once" ); |
5023 | |
5024 | LocTy Loc = Lex.getLoc(); |
5025 | Lex.Lex(); |
5026 | return parseMDField(Loc, Name, Result); |
5027 | } |
5028 | |
5029 | bool LLParser::parseSpecializedMDNode(MDNode *&N, bool IsDistinct) { |
5030 | assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name" ); |
5031 | |
5032 | #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \ |
5033 | if (Lex.getStrVal() == #CLASS) \ |
5034 | return parse##CLASS(N, IsDistinct); |
5035 | #include "llvm/IR/Metadata.def" |
5036 | |
5037 | return tokError(Msg: "expected metadata type" ); |
5038 | } |
5039 | |
5040 | #define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT |
5041 | #define NOP_FIELD(NAME, TYPE, INIT) |
5042 | #define REQUIRE_FIELD(NAME, TYPE, INIT) \ |
5043 | if (!NAME.Seen) \ |
5044 | return error(ClosingLoc, "missing required field '" #NAME "'"); |
5045 | #define PARSE_MD_FIELD(NAME, TYPE, DEFAULT) \ |
5046 | if (Lex.getStrVal() == #NAME) \ |
5047 | return parseMDField(#NAME, NAME); |
5048 | #define PARSE_MD_FIELDS() \ |
5049 | VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD) \ |
5050 | do { \ |
5051 | LocTy ClosingLoc; \ |
5052 | if (parseMDFieldsImpl( \ |
5053 | [&]() -> bool { \ |
5054 | VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD) \ |
5055 | return tokError(Twine("invalid field '") + Lex.getStrVal() + \ |
5056 | "'"); \ |
5057 | }, \ |
5058 | ClosingLoc)) \ |
5059 | return true; \ |
5060 | VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD) \ |
5061 | } while (false) |
5062 | #define GET_OR_DISTINCT(CLASS, ARGS) \ |
5063 | (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS) |
5064 | |
5065 | /// parseDILocationFields: |
5066 | /// ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6, |
5067 | /// isImplicitCode: true) |
5068 | bool LLParser::parseDILocation(MDNode *&Result, bool IsDistinct) { |
5069 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5070 | OPTIONAL(line, LineField, ); \ |
5071 | OPTIONAL(column, ColumnField, ); \ |
5072 | REQUIRED(scope, MDField, (/* AllowNull */ false)); \ |
5073 | OPTIONAL(inlinedAt, MDField, ); \ |
5074 | OPTIONAL(isImplicitCode, MDBoolField, (false)); |
5075 | PARSE_MD_FIELDS(); |
5076 | #undef VISIT_MD_FIELDS |
5077 | |
5078 | Result = |
5079 | GET_OR_DISTINCT(DILocation, (Context, line.Val, column.Val, scope.Val, |
5080 | inlinedAt.Val, isImplicitCode.Val)); |
5081 | return false; |
5082 | } |
5083 | |
5084 | /// parseDIAssignID: |
5085 | /// ::= distinct !DIAssignID() |
5086 | bool LLParser::parseDIAssignID(MDNode *&Result, bool IsDistinct) { |
5087 | if (!IsDistinct) |
5088 | return Lex.Error(Msg: "missing 'distinct', required for !DIAssignID()" ); |
5089 | |
5090 | Lex.Lex(); |
5091 | |
5092 | // Now eat the parens. |
5093 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
5094 | return true; |
5095 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
5096 | return true; |
5097 | |
5098 | Result = DIAssignID::getDistinct(Context); |
5099 | return false; |
5100 | } |
5101 | |
5102 | /// parseGenericDINode: |
5103 | /// ::= !GenericDINode(tag: 15, header: "...", operands: {...}) |
5104 | bool LLParser::parseGenericDINode(MDNode *&Result, bool IsDistinct) { |
5105 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5106 | REQUIRED(tag, DwarfTagField, ); \ |
5107 | OPTIONAL(, MDStringField, ); \ |
5108 | OPTIONAL(operands, MDFieldList, ); |
5109 | PARSE_MD_FIELDS(); |
5110 | #undef VISIT_MD_FIELDS |
5111 | |
5112 | Result = GET_OR_DISTINCT(GenericDINode, |
5113 | (Context, tag.Val, header.Val, operands.Val)); |
5114 | return false; |
5115 | } |
5116 | |
5117 | /// parseDISubrange: |
5118 | /// ::= !DISubrange(count: 30, lowerBound: 2) |
5119 | /// ::= !DISubrange(count: !node, lowerBound: 2) |
5120 | /// ::= !DISubrange(lowerBound: !node1, upperBound: !node2, stride: !node3) |
5121 | bool LLParser::parseDISubrange(MDNode *&Result, bool IsDistinct) { |
5122 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5123 | OPTIONAL(count, MDSignedOrMDField, (-1, -1, INT64_MAX, false)); \ |
5124 | OPTIONAL(lowerBound, MDSignedOrMDField, ); \ |
5125 | OPTIONAL(upperBound, MDSignedOrMDField, ); \ |
5126 | OPTIONAL(stride, MDSignedOrMDField, ); |
5127 | PARSE_MD_FIELDS(); |
5128 | #undef VISIT_MD_FIELDS |
5129 | |
5130 | Metadata *Count = nullptr; |
5131 | Metadata *LowerBound = nullptr; |
5132 | Metadata *UpperBound = nullptr; |
5133 | Metadata *Stride = nullptr; |
5134 | |
5135 | auto convToMetadata = [&](MDSignedOrMDField Bound) -> Metadata * { |
5136 | if (Bound.isMDSignedField()) |
5137 | return ConstantAsMetadata::get(C: ConstantInt::getSigned( |
5138 | Ty: Type::getInt64Ty(C&: Context), V: Bound.getMDSignedValue())); |
5139 | if (Bound.isMDField()) |
5140 | return Bound.getMDFieldValue(); |
5141 | return nullptr; |
5142 | }; |
5143 | |
5144 | Count = convToMetadata(count); |
5145 | LowerBound = convToMetadata(lowerBound); |
5146 | UpperBound = convToMetadata(upperBound); |
5147 | Stride = convToMetadata(stride); |
5148 | |
5149 | Result = GET_OR_DISTINCT(DISubrange, |
5150 | (Context, Count, LowerBound, UpperBound, Stride)); |
5151 | |
5152 | return false; |
5153 | } |
5154 | |
5155 | /// parseDIGenericSubrange: |
5156 | /// ::= !DIGenericSubrange(lowerBound: !node1, upperBound: !node2, stride: |
5157 | /// !node3) |
5158 | bool LLParser::parseDIGenericSubrange(MDNode *&Result, bool IsDistinct) { |
5159 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5160 | OPTIONAL(count, MDSignedOrMDField, ); \ |
5161 | OPTIONAL(lowerBound, MDSignedOrMDField, ); \ |
5162 | OPTIONAL(upperBound, MDSignedOrMDField, ); \ |
5163 | OPTIONAL(stride, MDSignedOrMDField, ); |
5164 | PARSE_MD_FIELDS(); |
5165 | #undef VISIT_MD_FIELDS |
5166 | |
5167 | auto ConvToMetadata = [&](MDSignedOrMDField Bound) -> Metadata * { |
5168 | if (Bound.isMDSignedField()) |
5169 | return DIExpression::get( |
5170 | Context, Elements: {dwarf::DW_OP_consts, |
5171 | static_cast<uint64_t>(Bound.getMDSignedValue())}); |
5172 | if (Bound.isMDField()) |
5173 | return Bound.getMDFieldValue(); |
5174 | return nullptr; |
5175 | }; |
5176 | |
5177 | Metadata *Count = ConvToMetadata(count); |
5178 | Metadata *LowerBound = ConvToMetadata(lowerBound); |
5179 | Metadata *UpperBound = ConvToMetadata(upperBound); |
5180 | Metadata *Stride = ConvToMetadata(stride); |
5181 | |
5182 | Result = GET_OR_DISTINCT(DIGenericSubrange, |
5183 | (Context, Count, LowerBound, UpperBound, Stride)); |
5184 | |
5185 | return false; |
5186 | } |
5187 | |
5188 | /// parseDIEnumerator: |
5189 | /// ::= !DIEnumerator(value: 30, isUnsigned: true, name: "SomeKind") |
5190 | bool LLParser::parseDIEnumerator(MDNode *&Result, bool IsDistinct) { |
5191 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5192 | REQUIRED(name, MDStringField, ); \ |
5193 | REQUIRED(value, MDAPSIntField, ); \ |
5194 | OPTIONAL(isUnsigned, MDBoolField, (false)); |
5195 | PARSE_MD_FIELDS(); |
5196 | #undef VISIT_MD_FIELDS |
5197 | |
5198 | if (isUnsigned.Val && value.Val.isNegative()) |
5199 | return tokError(Msg: "unsigned enumerator with negative value" ); |
5200 | |
5201 | APSInt Value(value.Val); |
5202 | // Add a leading zero so that unsigned values with the msb set are not |
5203 | // mistaken for negative values when used for signed enumerators. |
5204 | if (!isUnsigned.Val && value.Val.isUnsigned() && value.Val.isSignBitSet()) |
5205 | Value = Value.zext(width: Value.getBitWidth() + 1); |
5206 | |
5207 | Result = |
5208 | GET_OR_DISTINCT(DIEnumerator, (Context, Value, isUnsigned.Val, name.Val)); |
5209 | |
5210 | return false; |
5211 | } |
5212 | |
5213 | /// parseDIBasicType: |
5214 | /// ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32, |
5215 | /// encoding: DW_ATE_encoding, flags: 0) |
5216 | bool LLParser::parseDIBasicType(MDNode *&Result, bool IsDistinct) { |
5217 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5218 | OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \ |
5219 | OPTIONAL(name, MDStringField, ); \ |
5220 | OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \ |
5221 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
5222 | OPTIONAL(encoding, DwarfAttEncodingField, ); \ |
5223 | OPTIONAL(flags, DIFlagField, ); |
5224 | PARSE_MD_FIELDS(); |
5225 | #undef VISIT_MD_FIELDS |
5226 | |
5227 | Result = GET_OR_DISTINCT(DIBasicType, (Context, tag.Val, name.Val, size.Val, |
5228 | align.Val, encoding.Val, flags.Val)); |
5229 | return false; |
5230 | } |
5231 | |
5232 | /// parseDIStringType: |
5233 | /// ::= !DIStringType(name: "character(4)", size: 32, align: 32) |
5234 | bool LLParser::parseDIStringType(MDNode *&Result, bool IsDistinct) { |
5235 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5236 | OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_string_type)); \ |
5237 | OPTIONAL(name, MDStringField, ); \ |
5238 | OPTIONAL(stringLength, MDField, ); \ |
5239 | OPTIONAL(stringLengthExpression, MDField, ); \ |
5240 | OPTIONAL(stringLocationExpression, MDField, ); \ |
5241 | OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \ |
5242 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
5243 | OPTIONAL(encoding, DwarfAttEncodingField, ); |
5244 | PARSE_MD_FIELDS(); |
5245 | #undef VISIT_MD_FIELDS |
5246 | |
5247 | Result = GET_OR_DISTINCT( |
5248 | DIStringType, |
5249 | (Context, tag.Val, name.Val, stringLength.Val, stringLengthExpression.Val, |
5250 | stringLocationExpression.Val, size.Val, align.Val, encoding.Val)); |
5251 | return false; |
5252 | } |
5253 | |
5254 | /// parseDIDerivedType: |
5255 | /// ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0, |
5256 | /// line: 7, scope: !1, baseType: !2, size: 32, |
5257 | /// align: 32, offset: 0, flags: 0, extraData: !3, |
5258 | /// dwarfAddressSpace: 3, ptrAuthKey: 1, |
5259 | /// ptrAuthIsAddressDiscriminated: true, |
5260 | /// ptrAuthExtraDiscriminator: 0x1234, |
5261 | /// ptrAuthIsaPointer: 1, ptrAuthAuthenticatesNullValues:1 |
5262 | /// ) |
5263 | bool LLParser::parseDIDerivedType(MDNode *&Result, bool IsDistinct) { |
5264 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5265 | REQUIRED(tag, DwarfTagField, ); \ |
5266 | OPTIONAL(name, MDStringField, ); \ |
5267 | OPTIONAL(file, MDField, ); \ |
5268 | OPTIONAL(line, LineField, ); \ |
5269 | OPTIONAL(scope, MDField, ); \ |
5270 | REQUIRED(baseType, MDField, ); \ |
5271 | OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \ |
5272 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
5273 | OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \ |
5274 | OPTIONAL(flags, DIFlagField, ); \ |
5275 | OPTIONAL(, MDField, ); \ |
5276 | OPTIONAL(dwarfAddressSpace, MDUnsignedField, (UINT32_MAX, UINT32_MAX)); \ |
5277 | OPTIONAL(annotations, MDField, ); \ |
5278 | OPTIONAL(ptrAuthKey, MDUnsignedField, (0, 7)); \ |
5279 | OPTIONAL(ptrAuthIsAddressDiscriminated, MDBoolField, ); \ |
5280 | OPTIONAL(, MDUnsignedField, (0, 0xffff)); \ |
5281 | OPTIONAL(ptrAuthIsaPointer, MDBoolField, ); \ |
5282 | OPTIONAL(ptrAuthAuthenticatesNullValues, MDBoolField, ); |
5283 | PARSE_MD_FIELDS(); |
5284 | #undef VISIT_MD_FIELDS |
5285 | |
5286 | std::optional<unsigned> DWARFAddressSpace; |
5287 | if (dwarfAddressSpace.Val != UINT32_MAX) |
5288 | DWARFAddressSpace = dwarfAddressSpace.Val; |
5289 | std::optional<DIDerivedType::PtrAuthData> PtrAuthData; |
5290 | if (ptrAuthKey.Val) |
5291 | PtrAuthData.emplace( |
5292 | args: (unsigned)ptrAuthKey.Val, args&: ptrAuthIsAddressDiscriminated.Val, |
5293 | args: (unsigned)ptrAuthExtraDiscriminator.Val, args&: ptrAuthIsaPointer.Val, |
5294 | args&: ptrAuthAuthenticatesNullValues.Val); |
5295 | |
5296 | Result = GET_OR_DISTINCT(DIDerivedType, |
5297 | (Context, tag.Val, name.Val, file.Val, line.Val, |
5298 | scope.Val, baseType.Val, size.Val, align.Val, |
5299 | offset.Val, DWARFAddressSpace, PtrAuthData, |
5300 | flags.Val, extraData.Val, annotations.Val)); |
5301 | return false; |
5302 | } |
5303 | |
5304 | bool LLParser::parseDICompositeType(MDNode *&Result, bool IsDistinct) { |
5305 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5306 | REQUIRED(tag, DwarfTagField, ); \ |
5307 | OPTIONAL(name, MDStringField, ); \ |
5308 | OPTIONAL(file, MDField, ); \ |
5309 | OPTIONAL(line, LineField, ); \ |
5310 | OPTIONAL(scope, MDField, ); \ |
5311 | OPTIONAL(baseType, MDField, ); \ |
5312 | OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \ |
5313 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
5314 | OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \ |
5315 | OPTIONAL(flags, DIFlagField, ); \ |
5316 | OPTIONAL(elements, MDField, ); \ |
5317 | OPTIONAL(runtimeLang, DwarfLangField, ); \ |
5318 | OPTIONAL(vtableHolder, MDField, ); \ |
5319 | OPTIONAL(templateParams, MDField, ); \ |
5320 | OPTIONAL(identifier, MDStringField, ); \ |
5321 | OPTIONAL(discriminator, MDField, ); \ |
5322 | OPTIONAL(dataLocation, MDField, ); \ |
5323 | OPTIONAL(associated, MDField, ); \ |
5324 | OPTIONAL(allocated, MDField, ); \ |
5325 | OPTIONAL(rank, MDSignedOrMDField, ); \ |
5326 | OPTIONAL(annotations, MDField, ); |
5327 | PARSE_MD_FIELDS(); |
5328 | #undef VISIT_MD_FIELDS |
5329 | |
5330 | Metadata *Rank = nullptr; |
5331 | if (rank.isMDSignedField()) |
5332 | Rank = ConstantAsMetadata::get(C: ConstantInt::getSigned( |
5333 | Ty: Type::getInt64Ty(C&: Context), V: rank.getMDSignedValue())); |
5334 | else if (rank.isMDField()) |
5335 | Rank = rank.getMDFieldValue(); |
5336 | |
5337 | // If this has an identifier try to build an ODR type. |
5338 | if (identifier.Val) |
5339 | if (auto *CT = DICompositeType::buildODRType( |
5340 | Context, Identifier&: *identifier.Val, Tag: tag.Val, Name: name.Val, File: file.Val, Line: line.Val, |
5341 | Scope: scope.Val, BaseType: baseType.Val, SizeInBits: size.Val, AlignInBits: align.Val, OffsetInBits: offset.Val, Flags: flags.Val, |
5342 | Elements: elements.Val, RuntimeLang: runtimeLang.Val, VTableHolder: vtableHolder.Val, TemplateParams: templateParams.Val, |
5343 | Discriminator: discriminator.Val, DataLocation: dataLocation.Val, Associated: associated.Val, Allocated: allocated.Val, |
5344 | Rank, Annotations: annotations.Val)) { |
5345 | Result = CT; |
5346 | return false; |
5347 | } |
5348 | |
5349 | // Create a new node, and save it in the context if it belongs in the type |
5350 | // map. |
5351 | Result = GET_OR_DISTINCT( |
5352 | DICompositeType, |
5353 | (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val, |
5354 | size.Val, align.Val, offset.Val, flags.Val, elements.Val, |
5355 | runtimeLang.Val, vtableHolder.Val, templateParams.Val, identifier.Val, |
5356 | discriminator.Val, dataLocation.Val, associated.Val, allocated.Val, Rank, |
5357 | annotations.Val)); |
5358 | return false; |
5359 | } |
5360 | |
5361 | bool LLParser::parseDISubroutineType(MDNode *&Result, bool IsDistinct) { |
5362 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5363 | OPTIONAL(flags, DIFlagField, ); \ |
5364 | OPTIONAL(cc, DwarfCCField, ); \ |
5365 | REQUIRED(types, MDField, ); |
5366 | PARSE_MD_FIELDS(); |
5367 | #undef VISIT_MD_FIELDS |
5368 | |
5369 | Result = GET_OR_DISTINCT(DISubroutineType, |
5370 | (Context, flags.Val, cc.Val, types.Val)); |
5371 | return false; |
5372 | } |
5373 | |
5374 | /// parseDIFileType: |
5375 | /// ::= !DIFileType(filename: "path/to/file", directory: "/path/to/dir", |
5376 | /// checksumkind: CSK_MD5, |
5377 | /// checksum: "000102030405060708090a0b0c0d0e0f", |
5378 | /// source: "source file contents") |
5379 | bool LLParser::parseDIFile(MDNode *&Result, bool IsDistinct) { |
5380 | // The default constructed value for checksumkind is required, but will never |
5381 | // be used, as the parser checks if the field was actually Seen before using |
5382 | // the Val. |
5383 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5384 | REQUIRED(filename, MDStringField, ); \ |
5385 | REQUIRED(directory, MDStringField, ); \ |
5386 | OPTIONAL(checksumkind, ChecksumKindField, (DIFile::CSK_MD5)); \ |
5387 | OPTIONAL(checksum, MDStringField, ); \ |
5388 | OPTIONAL(source, MDStringField, ); |
5389 | PARSE_MD_FIELDS(); |
5390 | #undef VISIT_MD_FIELDS |
5391 | |
5392 | std::optional<DIFile::ChecksumInfo<MDString *>> OptChecksum; |
5393 | if (checksumkind.Seen && checksum.Seen) |
5394 | OptChecksum.emplace(args&: checksumkind.Val, args&: checksum.Val); |
5395 | else if (checksumkind.Seen || checksum.Seen) |
5396 | return Lex.Error(Msg: "'checksumkind' and 'checksum' must be provided together" ); |
5397 | |
5398 | MDString *Source = nullptr; |
5399 | if (source.Seen) |
5400 | Source = source.Val; |
5401 | Result = GET_OR_DISTINCT( |
5402 | DIFile, (Context, filename.Val, directory.Val, OptChecksum, Source)); |
5403 | return false; |
5404 | } |
5405 | |
5406 | /// parseDICompileUnit: |
5407 | /// ::= !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang", |
5408 | /// isOptimized: true, flags: "-O2", runtimeVersion: 1, |
5409 | /// splitDebugFilename: "abc.debug", |
5410 | /// emissionKind: FullDebug, enums: !1, retainedTypes: !2, |
5411 | /// globals: !4, imports: !5, macros: !6, dwoId: 0x0abcd, |
5412 | /// sysroot: "/", sdk: "MacOSX.sdk") |
5413 | bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) { |
5414 | if (!IsDistinct) |
5415 | return Lex.Error(Msg: "missing 'distinct', required for !DICompileUnit" ); |
5416 | |
5417 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5418 | REQUIRED(language, DwarfLangField, ); \ |
5419 | REQUIRED(file, MDField, (/* AllowNull */ false)); \ |
5420 | OPTIONAL(producer, MDStringField, ); \ |
5421 | OPTIONAL(isOptimized, MDBoolField, ); \ |
5422 | OPTIONAL(flags, MDStringField, ); \ |
5423 | OPTIONAL(runtimeVersion, MDUnsignedField, (0, UINT32_MAX)); \ |
5424 | OPTIONAL(splitDebugFilename, MDStringField, ); \ |
5425 | OPTIONAL(emissionKind, EmissionKindField, ); \ |
5426 | OPTIONAL(enums, MDField, ); \ |
5427 | OPTIONAL(retainedTypes, MDField, ); \ |
5428 | OPTIONAL(globals, MDField, ); \ |
5429 | OPTIONAL(imports, MDField, ); \ |
5430 | OPTIONAL(macros, MDField, ); \ |
5431 | OPTIONAL(dwoId, MDUnsignedField, ); \ |
5432 | OPTIONAL(splitDebugInlining, MDBoolField, = true); \ |
5433 | OPTIONAL(debugInfoForProfiling, MDBoolField, = false); \ |
5434 | OPTIONAL(nameTableKind, NameTableKindField, ); \ |
5435 | OPTIONAL(rangesBaseAddress, MDBoolField, = false); \ |
5436 | OPTIONAL(sysroot, MDStringField, ); \ |
5437 | OPTIONAL(sdk, MDStringField, ); |
5438 | PARSE_MD_FIELDS(); |
5439 | #undef VISIT_MD_FIELDS |
5440 | |
5441 | Result = DICompileUnit::getDistinct( |
5442 | Context, SourceLanguage: language.Val, File: file.Val, Producer: producer.Val, IsOptimized: isOptimized.Val, Flags: flags.Val, |
5443 | RuntimeVersion: runtimeVersion.Val, SplitDebugFilename: splitDebugFilename.Val, EmissionKind: emissionKind.Val, EnumTypes: enums.Val, |
5444 | RetainedTypes: retainedTypes.Val, GlobalVariables: globals.Val, ImportedEntities: imports.Val, Macros: macros.Val, DWOId: dwoId.Val, |
5445 | SplitDebugInlining: splitDebugInlining.Val, DebugInfoForProfiling: debugInfoForProfiling.Val, NameTableKind: nameTableKind.Val, |
5446 | RangesBaseAddress: rangesBaseAddress.Val, SysRoot: sysroot.Val, SDK: sdk.Val); |
5447 | return false; |
5448 | } |
5449 | |
5450 | /// parseDISubprogram: |
5451 | /// ::= !DISubprogram(scope: !0, name: "foo", linkageName: "_Zfoo", |
5452 | /// file: !1, line: 7, type: !2, isLocal: false, |
5453 | /// isDefinition: true, scopeLine: 8, containingType: !3, |
5454 | /// virtuality: DW_VIRTUALTIY_pure_virtual, |
5455 | /// virtualIndex: 10, thisAdjustment: 4, flags: 11, |
5456 | /// spFlags: 10, isOptimized: false, templateParams: !4, |
5457 | /// declaration: !5, retainedNodes: !6, thrownTypes: !7, |
5458 | /// annotations: !8) |
5459 | bool LLParser::parseDISubprogram(MDNode *&Result, bool IsDistinct) { |
5460 | auto Loc = Lex.getLoc(); |
5461 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5462 | OPTIONAL(scope, MDField, ); \ |
5463 | OPTIONAL(name, MDStringField, ); \ |
5464 | OPTIONAL(linkageName, MDStringField, ); \ |
5465 | OPTIONAL(file, MDField, ); \ |
5466 | OPTIONAL(line, LineField, ); \ |
5467 | OPTIONAL(type, MDField, ); \ |
5468 | OPTIONAL(isLocal, MDBoolField, ); \ |
5469 | OPTIONAL(isDefinition, MDBoolField, (true)); \ |
5470 | OPTIONAL(scopeLine, LineField, ); \ |
5471 | OPTIONAL(containingType, MDField, ); \ |
5472 | OPTIONAL(virtuality, DwarfVirtualityField, ); \ |
5473 | OPTIONAL(virtualIndex, MDUnsignedField, (0, UINT32_MAX)); \ |
5474 | OPTIONAL(thisAdjustment, MDSignedField, (0, INT32_MIN, INT32_MAX)); \ |
5475 | OPTIONAL(flags, DIFlagField, ); \ |
5476 | OPTIONAL(spFlags, DISPFlagField, ); \ |
5477 | OPTIONAL(isOptimized, MDBoolField, ); \ |
5478 | OPTIONAL(unit, MDField, ); \ |
5479 | OPTIONAL(templateParams, MDField, ); \ |
5480 | OPTIONAL(declaration, MDField, ); \ |
5481 | OPTIONAL(retainedNodes, MDField, ); \ |
5482 | OPTIONAL(thrownTypes, MDField, ); \ |
5483 | OPTIONAL(annotations, MDField, ); \ |
5484 | OPTIONAL(targetFuncName, MDStringField, ); |
5485 | PARSE_MD_FIELDS(); |
5486 | #undef VISIT_MD_FIELDS |
5487 | |
5488 | // An explicit spFlags field takes precedence over individual fields in |
5489 | // older IR versions. |
5490 | DISubprogram::DISPFlags SPFlags = |
5491 | spFlags.Seen ? spFlags.Val |
5492 | : DISubprogram::toSPFlags(IsLocalToUnit: isLocal.Val, IsDefinition: isDefinition.Val, |
5493 | IsOptimized: isOptimized.Val, Virtuality: virtuality.Val); |
5494 | if ((SPFlags & DISubprogram::SPFlagDefinition) && !IsDistinct) |
5495 | return Lex.Error( |
5496 | ErrorLoc: Loc, |
5497 | Msg: "missing 'distinct', required for !DISubprogram that is a Definition" ); |
5498 | Result = GET_OR_DISTINCT( |
5499 | DISubprogram, |
5500 | (Context, scope.Val, name.Val, linkageName.Val, file.Val, line.Val, |
5501 | type.Val, scopeLine.Val, containingType.Val, virtualIndex.Val, |
5502 | thisAdjustment.Val, flags.Val, SPFlags, unit.Val, templateParams.Val, |
5503 | declaration.Val, retainedNodes.Val, thrownTypes.Val, annotations.Val, |
5504 | targetFuncName.Val)); |
5505 | return false; |
5506 | } |
5507 | |
5508 | /// parseDILexicalBlock: |
5509 | /// ::= !DILexicalBlock(scope: !0, file: !2, line: 7, column: 9) |
5510 | bool LLParser::parseDILexicalBlock(MDNode *&Result, bool IsDistinct) { |
5511 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5512 | REQUIRED(scope, MDField, (/* AllowNull */ false)); \ |
5513 | OPTIONAL(file, MDField, ); \ |
5514 | OPTIONAL(line, LineField, ); \ |
5515 | OPTIONAL(column, ColumnField, ); |
5516 | PARSE_MD_FIELDS(); |
5517 | #undef VISIT_MD_FIELDS |
5518 | |
5519 | Result = GET_OR_DISTINCT( |
5520 | DILexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val)); |
5521 | return false; |
5522 | } |
5523 | |
5524 | /// parseDILexicalBlockFile: |
5525 | /// ::= !DILexicalBlockFile(scope: !0, file: !2, discriminator: 9) |
5526 | bool LLParser::parseDILexicalBlockFile(MDNode *&Result, bool IsDistinct) { |
5527 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5528 | REQUIRED(scope, MDField, (/* AllowNull */ false)); \ |
5529 | OPTIONAL(file, MDField, ); \ |
5530 | REQUIRED(discriminator, MDUnsignedField, (0, UINT32_MAX)); |
5531 | PARSE_MD_FIELDS(); |
5532 | #undef VISIT_MD_FIELDS |
5533 | |
5534 | Result = GET_OR_DISTINCT(DILexicalBlockFile, |
5535 | (Context, scope.Val, file.Val, discriminator.Val)); |
5536 | return false; |
5537 | } |
5538 | |
5539 | /// parseDICommonBlock: |
5540 | /// ::= !DICommonBlock(scope: !0, file: !2, name: "COMMON name", line: 9) |
5541 | bool LLParser::parseDICommonBlock(MDNode *&Result, bool IsDistinct) { |
5542 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5543 | REQUIRED(scope, MDField, ); \ |
5544 | OPTIONAL(declaration, MDField, ); \ |
5545 | OPTIONAL(name, MDStringField, ); \ |
5546 | OPTIONAL(file, MDField, ); \ |
5547 | OPTIONAL(line, LineField, ); |
5548 | PARSE_MD_FIELDS(); |
5549 | #undef VISIT_MD_FIELDS |
5550 | |
5551 | Result = GET_OR_DISTINCT(DICommonBlock, |
5552 | (Context, scope.Val, declaration.Val, name.Val, |
5553 | file.Val, line.Val)); |
5554 | return false; |
5555 | } |
5556 | |
5557 | /// parseDINamespace: |
5558 | /// ::= !DINamespace(scope: !0, file: !2, name: "SomeNamespace", line: 9) |
5559 | bool LLParser::parseDINamespace(MDNode *&Result, bool IsDistinct) { |
5560 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5561 | REQUIRED(scope, MDField, ); \ |
5562 | OPTIONAL(name, MDStringField, ); \ |
5563 | OPTIONAL(exportSymbols, MDBoolField, ); |
5564 | PARSE_MD_FIELDS(); |
5565 | #undef VISIT_MD_FIELDS |
5566 | |
5567 | Result = GET_OR_DISTINCT(DINamespace, |
5568 | (Context, scope.Val, name.Val, exportSymbols.Val)); |
5569 | return false; |
5570 | } |
5571 | |
5572 | /// parseDIMacro: |
5573 | /// ::= !DIMacro(macinfo: type, line: 9, name: "SomeMacro", value: |
5574 | /// "SomeValue") |
5575 | bool LLParser::parseDIMacro(MDNode *&Result, bool IsDistinct) { |
5576 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5577 | REQUIRED(type, DwarfMacinfoTypeField, ); \ |
5578 | OPTIONAL(line, LineField, ); \ |
5579 | REQUIRED(name, MDStringField, ); \ |
5580 | OPTIONAL(value, MDStringField, ); |
5581 | PARSE_MD_FIELDS(); |
5582 | #undef VISIT_MD_FIELDS |
5583 | |
5584 | Result = GET_OR_DISTINCT(DIMacro, |
5585 | (Context, type.Val, line.Val, name.Val, value.Val)); |
5586 | return false; |
5587 | } |
5588 | |
5589 | /// parseDIMacroFile: |
5590 | /// ::= !DIMacroFile(line: 9, file: !2, nodes: !3) |
5591 | bool LLParser::parseDIMacroFile(MDNode *&Result, bool IsDistinct) { |
5592 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5593 | OPTIONAL(type, DwarfMacinfoTypeField, (dwarf::DW_MACINFO_start_file)); \ |
5594 | OPTIONAL(line, LineField, ); \ |
5595 | REQUIRED(file, MDField, ); \ |
5596 | OPTIONAL(nodes, MDField, ); |
5597 | PARSE_MD_FIELDS(); |
5598 | #undef VISIT_MD_FIELDS |
5599 | |
5600 | Result = GET_OR_DISTINCT(DIMacroFile, |
5601 | (Context, type.Val, line.Val, file.Val, nodes.Val)); |
5602 | return false; |
5603 | } |
5604 | |
5605 | /// parseDIModule: |
5606 | /// ::= !DIModule(scope: !0, name: "SomeModule", configMacros: |
5607 | /// "-DNDEBUG", includePath: "/usr/include", apinotes: "module.apinotes", |
5608 | /// file: !1, line: 4, isDecl: false) |
5609 | bool LLParser::parseDIModule(MDNode *&Result, bool IsDistinct) { |
5610 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5611 | REQUIRED(scope, MDField, ); \ |
5612 | REQUIRED(name, MDStringField, ); \ |
5613 | OPTIONAL(configMacros, MDStringField, ); \ |
5614 | OPTIONAL(includePath, MDStringField, ); \ |
5615 | OPTIONAL(apinotes, MDStringField, ); \ |
5616 | OPTIONAL(file, MDField, ); \ |
5617 | OPTIONAL(line, LineField, ); \ |
5618 | OPTIONAL(isDecl, MDBoolField, ); |
5619 | PARSE_MD_FIELDS(); |
5620 | #undef VISIT_MD_FIELDS |
5621 | |
5622 | Result = GET_OR_DISTINCT(DIModule, (Context, file.Val, scope.Val, name.Val, |
5623 | configMacros.Val, includePath.Val, |
5624 | apinotes.Val, line.Val, isDecl.Val)); |
5625 | return false; |
5626 | } |
5627 | |
5628 | /// parseDITemplateTypeParameter: |
5629 | /// ::= !DITemplateTypeParameter(name: "Ty", type: !1, defaulted: false) |
5630 | bool LLParser::parseDITemplateTypeParameter(MDNode *&Result, bool IsDistinct) { |
5631 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5632 | OPTIONAL(name, MDStringField, ); \ |
5633 | REQUIRED(type, MDField, ); \ |
5634 | OPTIONAL(defaulted, MDBoolField, ); |
5635 | PARSE_MD_FIELDS(); |
5636 | #undef VISIT_MD_FIELDS |
5637 | |
5638 | Result = GET_OR_DISTINCT(DITemplateTypeParameter, |
5639 | (Context, name.Val, type.Val, defaulted.Val)); |
5640 | return false; |
5641 | } |
5642 | |
5643 | /// parseDITemplateValueParameter: |
5644 | /// ::= !DITemplateValueParameter(tag: DW_TAG_template_value_parameter, |
5645 | /// name: "V", type: !1, defaulted: false, |
5646 | /// value: i32 7) |
5647 | bool LLParser::parseDITemplateValueParameter(MDNode *&Result, bool IsDistinct) { |
5648 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5649 | OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_template_value_parameter)); \ |
5650 | OPTIONAL(name, MDStringField, ); \ |
5651 | OPTIONAL(type, MDField, ); \ |
5652 | OPTIONAL(defaulted, MDBoolField, ); \ |
5653 | REQUIRED(value, MDField, ); |
5654 | |
5655 | PARSE_MD_FIELDS(); |
5656 | #undef VISIT_MD_FIELDS |
5657 | |
5658 | Result = GET_OR_DISTINCT( |
5659 | DITemplateValueParameter, |
5660 | (Context, tag.Val, name.Val, type.Val, defaulted.Val, value.Val)); |
5661 | return false; |
5662 | } |
5663 | |
5664 | /// parseDIGlobalVariable: |
5665 | /// ::= !DIGlobalVariable(scope: !0, name: "foo", linkageName: "foo", |
5666 | /// file: !1, line: 7, type: !2, isLocal: false, |
5667 | /// isDefinition: true, templateParams: !3, |
5668 | /// declaration: !4, align: 8) |
5669 | bool LLParser::parseDIGlobalVariable(MDNode *&Result, bool IsDistinct) { |
5670 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5671 | OPTIONAL(name, MDStringField, (/* AllowEmpty */ false)); \ |
5672 | OPTIONAL(scope, MDField, ); \ |
5673 | OPTIONAL(linkageName, MDStringField, ); \ |
5674 | OPTIONAL(file, MDField, ); \ |
5675 | OPTIONAL(line, LineField, ); \ |
5676 | OPTIONAL(type, MDField, ); \ |
5677 | OPTIONAL(isLocal, MDBoolField, ); \ |
5678 | OPTIONAL(isDefinition, MDBoolField, (true)); \ |
5679 | OPTIONAL(templateParams, MDField, ); \ |
5680 | OPTIONAL(declaration, MDField, ); \ |
5681 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
5682 | OPTIONAL(annotations, MDField, ); |
5683 | PARSE_MD_FIELDS(); |
5684 | #undef VISIT_MD_FIELDS |
5685 | |
5686 | Result = |
5687 | GET_OR_DISTINCT(DIGlobalVariable, |
5688 | (Context, scope.Val, name.Val, linkageName.Val, file.Val, |
5689 | line.Val, type.Val, isLocal.Val, isDefinition.Val, |
5690 | declaration.Val, templateParams.Val, align.Val, |
5691 | annotations.Val)); |
5692 | return false; |
5693 | } |
5694 | |
5695 | /// parseDILocalVariable: |
5696 | /// ::= !DILocalVariable(arg: 7, scope: !0, name: "foo", |
5697 | /// file: !1, line: 7, type: !2, arg: 2, flags: 7, |
5698 | /// align: 8) |
5699 | /// ::= !DILocalVariable(scope: !0, name: "foo", |
5700 | /// file: !1, line: 7, type: !2, arg: 2, flags: 7, |
5701 | /// align: 8) |
5702 | bool LLParser::parseDILocalVariable(MDNode *&Result, bool IsDistinct) { |
5703 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5704 | REQUIRED(scope, MDField, (/* AllowNull */ false)); \ |
5705 | OPTIONAL(name, MDStringField, ); \ |
5706 | OPTIONAL(arg, MDUnsignedField, (0, UINT16_MAX)); \ |
5707 | OPTIONAL(file, MDField, ); \ |
5708 | OPTIONAL(line, LineField, ); \ |
5709 | OPTIONAL(type, MDField, ); \ |
5710 | OPTIONAL(flags, DIFlagField, ); \ |
5711 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
5712 | OPTIONAL(annotations, MDField, ); |
5713 | PARSE_MD_FIELDS(); |
5714 | #undef VISIT_MD_FIELDS |
5715 | |
5716 | Result = GET_OR_DISTINCT(DILocalVariable, |
5717 | (Context, scope.Val, name.Val, file.Val, line.Val, |
5718 | type.Val, arg.Val, flags.Val, align.Val, |
5719 | annotations.Val)); |
5720 | return false; |
5721 | } |
5722 | |
5723 | /// parseDILabel: |
5724 | /// ::= !DILabel(scope: !0, name: "foo", file: !1, line: 7) |
5725 | bool LLParser::parseDILabel(MDNode *&Result, bool IsDistinct) { |
5726 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5727 | REQUIRED(scope, MDField, (/* AllowNull */ false)); \ |
5728 | REQUIRED(name, MDStringField, ); \ |
5729 | REQUIRED(file, MDField, ); \ |
5730 | REQUIRED(line, LineField, ); |
5731 | PARSE_MD_FIELDS(); |
5732 | #undef VISIT_MD_FIELDS |
5733 | |
5734 | Result = GET_OR_DISTINCT(DILabel, |
5735 | (Context, scope.Val, name.Val, file.Val, line.Val)); |
5736 | return false; |
5737 | } |
5738 | |
5739 | /// parseDIExpression: |
5740 | /// ::= !DIExpression(0, 7, -1) |
5741 | bool LLParser::parseDIExpression(MDNode *&Result, bool IsDistinct) { |
5742 | assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name" ); |
5743 | Lex.Lex(); |
5744 | |
5745 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
5746 | return true; |
5747 | |
5748 | SmallVector<uint64_t, 8> Elements; |
5749 | if (Lex.getKind() != lltok::rparen) |
5750 | do { |
5751 | if (Lex.getKind() == lltok::DwarfOp) { |
5752 | if (unsigned Op = dwarf::getOperationEncoding(OperationEncodingString: Lex.getStrVal())) { |
5753 | Lex.Lex(); |
5754 | Elements.push_back(Elt: Op); |
5755 | continue; |
5756 | } |
5757 | return tokError(Msg: Twine("invalid DWARF op '" ) + Lex.getStrVal() + "'" ); |
5758 | } |
5759 | |
5760 | if (Lex.getKind() == lltok::DwarfAttEncoding) { |
5761 | if (unsigned Op = dwarf::getAttributeEncoding(EncodingString: Lex.getStrVal())) { |
5762 | Lex.Lex(); |
5763 | Elements.push_back(Elt: Op); |
5764 | continue; |
5765 | } |
5766 | return tokError(Msg: Twine("invalid DWARF attribute encoding '" ) + |
5767 | Lex.getStrVal() + "'" ); |
5768 | } |
5769 | |
5770 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
5771 | return tokError(Msg: "expected unsigned integer" ); |
5772 | |
5773 | auto &U = Lex.getAPSIntVal(); |
5774 | if (U.ugt(UINT64_MAX)) |
5775 | return tokError(Msg: "element too large, limit is " + Twine(UINT64_MAX)); |
5776 | Elements.push_back(Elt: U.getZExtValue()); |
5777 | Lex.Lex(); |
5778 | } while (EatIfPresent(T: lltok::comma)); |
5779 | |
5780 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
5781 | return true; |
5782 | |
5783 | Result = GET_OR_DISTINCT(DIExpression, (Context, Elements)); |
5784 | return false; |
5785 | } |
5786 | |
5787 | /// ParseDIArgList: |
5788 | /// ::= !DIArgList(i32 7, i64 %0) |
5789 | bool LLParser::parseDIArgList(Metadata *&MD, PerFunctionState *PFS) { |
5790 | assert(PFS && "Expected valid function state" ); |
5791 | assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name" ); |
5792 | Lex.Lex(); |
5793 | |
5794 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
5795 | return true; |
5796 | |
5797 | SmallVector<ValueAsMetadata *, 4> Args; |
5798 | if (Lex.getKind() != lltok::rparen) |
5799 | do { |
5800 | Metadata *MD; |
5801 | if (parseValueAsMetadata(MD, TypeMsg: "expected value-as-metadata operand" , PFS)) |
5802 | return true; |
5803 | Args.push_back(Elt: dyn_cast<ValueAsMetadata>(Val: MD)); |
5804 | } while (EatIfPresent(T: lltok::comma)); |
5805 | |
5806 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
5807 | return true; |
5808 | |
5809 | MD = DIArgList::get(Context, Args); |
5810 | return false; |
5811 | } |
5812 | |
5813 | /// parseDIGlobalVariableExpression: |
5814 | /// ::= !DIGlobalVariableExpression(var: !0, expr: !1) |
5815 | bool LLParser::parseDIGlobalVariableExpression(MDNode *&Result, |
5816 | bool IsDistinct) { |
5817 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5818 | REQUIRED(var, MDField, ); \ |
5819 | REQUIRED(expr, MDField, ); |
5820 | PARSE_MD_FIELDS(); |
5821 | #undef VISIT_MD_FIELDS |
5822 | |
5823 | Result = |
5824 | GET_OR_DISTINCT(DIGlobalVariableExpression, (Context, var.Val, expr.Val)); |
5825 | return false; |
5826 | } |
5827 | |
5828 | /// parseDIObjCProperty: |
5829 | /// ::= !DIObjCProperty(name: "foo", file: !1, line: 7, setter: "setFoo", |
5830 | /// getter: "getFoo", attributes: 7, type: !2) |
5831 | bool LLParser::parseDIObjCProperty(MDNode *&Result, bool IsDistinct) { |
5832 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5833 | OPTIONAL(name, MDStringField, ); \ |
5834 | OPTIONAL(file, MDField, ); \ |
5835 | OPTIONAL(line, LineField, ); \ |
5836 | OPTIONAL(setter, MDStringField, ); \ |
5837 | OPTIONAL(getter, MDStringField, ); \ |
5838 | OPTIONAL(attributes, MDUnsignedField, (0, UINT32_MAX)); \ |
5839 | OPTIONAL(type, MDField, ); |
5840 | PARSE_MD_FIELDS(); |
5841 | #undef VISIT_MD_FIELDS |
5842 | |
5843 | Result = GET_OR_DISTINCT(DIObjCProperty, |
5844 | (Context, name.Val, file.Val, line.Val, setter.Val, |
5845 | getter.Val, attributes.Val, type.Val)); |
5846 | return false; |
5847 | } |
5848 | |
5849 | /// parseDIImportedEntity: |
5850 | /// ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1, |
5851 | /// line: 7, name: "foo", elements: !2) |
5852 | bool LLParser::parseDIImportedEntity(MDNode *&Result, bool IsDistinct) { |
5853 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
5854 | REQUIRED(tag, DwarfTagField, ); \ |
5855 | REQUIRED(scope, MDField, ); \ |
5856 | OPTIONAL(entity, MDField, ); \ |
5857 | OPTIONAL(file, MDField, ); \ |
5858 | OPTIONAL(line, LineField, ); \ |
5859 | OPTIONAL(name, MDStringField, ); \ |
5860 | OPTIONAL(elements, MDField, ); |
5861 | PARSE_MD_FIELDS(); |
5862 | #undef VISIT_MD_FIELDS |
5863 | |
5864 | Result = GET_OR_DISTINCT(DIImportedEntity, |
5865 | (Context, tag.Val, scope.Val, entity.Val, file.Val, |
5866 | line.Val, name.Val, elements.Val)); |
5867 | return false; |
5868 | } |
5869 | |
5870 | #undef PARSE_MD_FIELD |
5871 | #undef NOP_FIELD |
5872 | #undef REQUIRE_FIELD |
5873 | #undef DECLARE_FIELD |
5874 | |
5875 | /// parseMetadataAsValue |
5876 | /// ::= metadata i32 %local |
5877 | /// ::= metadata i32 @global |
5878 | /// ::= metadata i32 7 |
5879 | /// ::= metadata !0 |
5880 | /// ::= metadata !{...} |
5881 | /// ::= metadata !"string" |
5882 | bool LLParser::parseMetadataAsValue(Value *&V, PerFunctionState &PFS) { |
5883 | // Note: the type 'metadata' has already been parsed. |
5884 | Metadata *MD; |
5885 | if (parseMetadata(MD, PFS: &PFS)) |
5886 | return true; |
5887 | |
5888 | V = MetadataAsValue::get(Context, MD); |
5889 | return false; |
5890 | } |
5891 | |
5892 | /// parseValueAsMetadata |
5893 | /// ::= i32 %local |
5894 | /// ::= i32 @global |
5895 | /// ::= i32 7 |
5896 | bool LLParser::parseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg, |
5897 | PerFunctionState *PFS) { |
5898 | Type *Ty; |
5899 | LocTy Loc; |
5900 | if (parseType(Result&: Ty, Msg: TypeMsg, Loc)) |
5901 | return true; |
5902 | if (Ty->isMetadataTy()) |
5903 | return error(L: Loc, Msg: "invalid metadata-value-metadata roundtrip" ); |
5904 | |
5905 | Value *V; |
5906 | if (parseValue(Ty, V, PFS)) |
5907 | return true; |
5908 | |
5909 | MD = ValueAsMetadata::get(V); |
5910 | return false; |
5911 | } |
5912 | |
5913 | /// parseMetadata |
5914 | /// ::= i32 %local |
5915 | /// ::= i32 @global |
5916 | /// ::= i32 7 |
5917 | /// ::= !42 |
5918 | /// ::= !{...} |
5919 | /// ::= !"string" |
5920 | /// ::= !DILocation(...) |
5921 | bool LLParser::parseMetadata(Metadata *&MD, PerFunctionState *PFS) { |
5922 | if (Lex.getKind() == lltok::MetadataVar) { |
5923 | // DIArgLists are a special case, as they are a list of ValueAsMetadata and |
5924 | // so parsing this requires a Function State. |
5925 | if (Lex.getStrVal() == "DIArgList" ) { |
5926 | Metadata *AL; |
5927 | if (parseDIArgList(MD&: AL, PFS)) |
5928 | return true; |
5929 | MD = AL; |
5930 | return false; |
5931 | } |
5932 | MDNode *N; |
5933 | if (parseSpecializedMDNode(N)) { |
5934 | return true; |
5935 | } |
5936 | MD = N; |
5937 | return false; |
5938 | } |
5939 | |
5940 | // ValueAsMetadata: |
5941 | // <type> <value> |
5942 | if (Lex.getKind() != lltok::exclaim) |
5943 | return parseValueAsMetadata(MD, TypeMsg: "expected metadata operand" , PFS); |
5944 | |
5945 | // '!'. |
5946 | assert(Lex.getKind() == lltok::exclaim && "Expected '!' here" ); |
5947 | Lex.Lex(); |
5948 | |
5949 | // MDString: |
5950 | // ::= '!' STRINGCONSTANT |
5951 | if (Lex.getKind() == lltok::StringConstant) { |
5952 | MDString *S; |
5953 | if (parseMDString(Result&: S)) |
5954 | return true; |
5955 | MD = S; |
5956 | return false; |
5957 | } |
5958 | |
5959 | // MDNode: |
5960 | // !{ ... } |
5961 | // !7 |
5962 | MDNode *N; |
5963 | if (parseMDNodeTail(N)) |
5964 | return true; |
5965 | MD = N; |
5966 | return false; |
5967 | } |
5968 | |
5969 | //===----------------------------------------------------------------------===// |
5970 | // Function Parsing. |
5971 | //===----------------------------------------------------------------------===// |
5972 | |
5973 | bool LLParser::convertValIDToValue(Type *Ty, ValID &ID, Value *&V, |
5974 | PerFunctionState *PFS) { |
5975 | if (Ty->isFunctionTy()) |
5976 | return error(L: ID.Loc, Msg: "functions are not values, refer to them as pointers" ); |
5977 | |
5978 | switch (ID.Kind) { |
5979 | case ValID::t_LocalID: |
5980 | if (!PFS) |
5981 | return error(L: ID.Loc, Msg: "invalid use of function-local name" ); |
5982 | V = PFS->getVal(ID: ID.UIntVal, Ty, Loc: ID.Loc); |
5983 | return V == nullptr; |
5984 | case ValID::t_LocalName: |
5985 | if (!PFS) |
5986 | return error(L: ID.Loc, Msg: "invalid use of function-local name" ); |
5987 | V = PFS->getVal(Name: ID.StrVal, Ty, Loc: ID.Loc); |
5988 | return V == nullptr; |
5989 | case ValID::t_InlineAsm: { |
5990 | if (!ID.FTy) |
5991 | return error(L: ID.Loc, Msg: "invalid type for inline asm constraint string" ); |
5992 | if (Error Err = InlineAsm::verify(Ty: ID.FTy, Constraints: ID.StrVal2)) |
5993 | return error(L: ID.Loc, Msg: toString(E: std::move(Err))); |
5994 | V = InlineAsm::get( |
5995 | Ty: ID.FTy, AsmString: ID.StrVal, Constraints: ID.StrVal2, hasSideEffects: ID.UIntVal & 1, isAlignStack: (ID.UIntVal >> 1) & 1, |
5996 | asmDialect: InlineAsm::AsmDialect((ID.UIntVal >> 2) & 1), canThrow: (ID.UIntVal >> 3) & 1); |
5997 | return false; |
5998 | } |
5999 | case ValID::t_GlobalName: |
6000 | V = getGlobalVal(Name: ID.StrVal, Ty, Loc: ID.Loc); |
6001 | if (V && ID.NoCFI) |
6002 | V = NoCFIValue::get(GV: cast<GlobalValue>(Val: V)); |
6003 | return V == nullptr; |
6004 | case ValID::t_GlobalID: |
6005 | V = getGlobalVal(ID: ID.UIntVal, Ty, Loc: ID.Loc); |
6006 | if (V && ID.NoCFI) |
6007 | V = NoCFIValue::get(GV: cast<GlobalValue>(Val: V)); |
6008 | return V == nullptr; |
6009 | case ValID::t_APSInt: |
6010 | if (!Ty->isIntegerTy()) |
6011 | return error(L: ID.Loc, Msg: "integer constant must have integer type" ); |
6012 | ID.APSIntVal = ID.APSIntVal.extOrTrunc(width: Ty->getPrimitiveSizeInBits()); |
6013 | V = ConstantInt::get(Context, V: ID.APSIntVal); |
6014 | return false; |
6015 | case ValID::t_APFloat: |
6016 | if (!Ty->isFloatingPointTy() || |
6017 | !ConstantFP::isValueValidForType(Ty, V: ID.APFloatVal)) |
6018 | return error(L: ID.Loc, Msg: "floating point constant invalid for type" ); |
6019 | |
6020 | // The lexer has no type info, so builds all half, bfloat, float, and double |
6021 | // FP constants as double. Fix this here. Long double does not need this. |
6022 | if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble()) { |
6023 | // Check for signaling before potentially converting and losing that info. |
6024 | bool IsSNAN = ID.APFloatVal.isSignaling(); |
6025 | bool Ignored; |
6026 | if (Ty->isHalfTy()) |
6027 | ID.APFloatVal.convert(ToSemantics: APFloat::IEEEhalf(), RM: APFloat::rmNearestTiesToEven, |
6028 | losesInfo: &Ignored); |
6029 | else if (Ty->isBFloatTy()) |
6030 | ID.APFloatVal.convert(ToSemantics: APFloat::BFloat(), RM: APFloat::rmNearestTiesToEven, |
6031 | losesInfo: &Ignored); |
6032 | else if (Ty->isFloatTy()) |
6033 | ID.APFloatVal.convert(ToSemantics: APFloat::IEEEsingle(), RM: APFloat::rmNearestTiesToEven, |
6034 | losesInfo: &Ignored); |
6035 | if (IsSNAN) { |
6036 | // The convert call above may quiet an SNaN, so manufacture another |
6037 | // SNaN. The bitcast works because the payload (significand) parameter |
6038 | // is truncated to fit. |
6039 | APInt Payload = ID.APFloatVal.bitcastToAPInt(); |
6040 | ID.APFloatVal = APFloat::getSNaN(Sem: ID.APFloatVal.getSemantics(), |
6041 | Negative: ID.APFloatVal.isNegative(), payload: &Payload); |
6042 | } |
6043 | } |
6044 | V = ConstantFP::get(Context, V: ID.APFloatVal); |
6045 | |
6046 | if (V->getType() != Ty) |
6047 | return error(L: ID.Loc, Msg: "floating point constant does not have type '" + |
6048 | getTypeString(T: Ty) + "'" ); |
6049 | |
6050 | return false; |
6051 | case ValID::t_Null: |
6052 | if (!Ty->isPointerTy()) |
6053 | return error(L: ID.Loc, Msg: "null must be a pointer type" ); |
6054 | V = ConstantPointerNull::get(T: cast<PointerType>(Val: Ty)); |
6055 | return false; |
6056 | case ValID::t_Undef: |
6057 | // FIXME: LabelTy should not be a first-class type. |
6058 | if (!Ty->isFirstClassType() || Ty->isLabelTy()) |
6059 | return error(L: ID.Loc, Msg: "invalid type for undef constant" ); |
6060 | V = UndefValue::get(T: Ty); |
6061 | return false; |
6062 | case ValID::t_EmptyArray: |
6063 | if (!Ty->isArrayTy() || cast<ArrayType>(Val: Ty)->getNumElements() != 0) |
6064 | return error(L: ID.Loc, Msg: "invalid empty array initializer" ); |
6065 | V = UndefValue::get(T: Ty); |
6066 | return false; |
6067 | case ValID::t_Zero: |
6068 | // FIXME: LabelTy should not be a first-class type. |
6069 | if (!Ty->isFirstClassType() || Ty->isLabelTy()) |
6070 | return error(L: ID.Loc, Msg: "invalid type for null constant" ); |
6071 | if (auto *TETy = dyn_cast<TargetExtType>(Val: Ty)) |
6072 | if (!TETy->hasProperty(Prop: TargetExtType::HasZeroInit)) |
6073 | return error(L: ID.Loc, Msg: "invalid type for null constant" ); |
6074 | V = Constant::getNullValue(Ty); |
6075 | return false; |
6076 | case ValID::t_None: |
6077 | if (!Ty->isTokenTy()) |
6078 | return error(L: ID.Loc, Msg: "invalid type for none constant" ); |
6079 | V = Constant::getNullValue(Ty); |
6080 | return false; |
6081 | case ValID::t_Poison: |
6082 | // FIXME: LabelTy should not be a first-class type. |
6083 | if (!Ty->isFirstClassType() || Ty->isLabelTy()) |
6084 | return error(L: ID.Loc, Msg: "invalid type for poison constant" ); |
6085 | V = PoisonValue::get(T: Ty); |
6086 | return false; |
6087 | case ValID::t_Constant: |
6088 | if (ID.ConstantVal->getType() != Ty) |
6089 | return error(L: ID.Loc, Msg: "constant expression type mismatch: got type '" + |
6090 | getTypeString(T: ID.ConstantVal->getType()) + |
6091 | "' but expected '" + getTypeString(T: Ty) + "'" ); |
6092 | V = ID.ConstantVal; |
6093 | return false; |
6094 | case ValID::t_ConstantSplat: |
6095 | if (!Ty->isVectorTy()) |
6096 | return error(L: ID.Loc, Msg: "vector constant must have vector type" ); |
6097 | if (ID.ConstantVal->getType() != Ty->getScalarType()) |
6098 | return error(L: ID.Loc, Msg: "constant expression type mismatch: got type '" + |
6099 | getTypeString(T: ID.ConstantVal->getType()) + |
6100 | "' but expected '" + |
6101 | getTypeString(T: Ty->getScalarType()) + "'" ); |
6102 | V = ConstantVector::getSplat(EC: cast<VectorType>(Val: Ty)->getElementCount(), |
6103 | Elt: ID.ConstantVal); |
6104 | return false; |
6105 | case ValID::t_ConstantStruct: |
6106 | case ValID::t_PackedConstantStruct: |
6107 | if (StructType *ST = dyn_cast<StructType>(Val: Ty)) { |
6108 | if (ST->getNumElements() != ID.UIntVal) |
6109 | return error(L: ID.Loc, |
6110 | Msg: "initializer with struct type has wrong # elements" ); |
6111 | if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct)) |
6112 | return error(L: ID.Loc, Msg: "packed'ness of initializer and type don't match" ); |
6113 | |
6114 | // Verify that the elements are compatible with the structtype. |
6115 | for (unsigned i = 0, e = ID.UIntVal; i != e; ++i) |
6116 | if (ID.ConstantStructElts[i]->getType() != ST->getElementType(N: i)) |
6117 | return error( |
6118 | L: ID.Loc, |
6119 | Msg: "element " + Twine(i) + |
6120 | " of struct initializer doesn't match struct element type" ); |
6121 | |
6122 | V = ConstantStruct::get( |
6123 | T: ST, V: ArrayRef(ID.ConstantStructElts.get(), ID.UIntVal)); |
6124 | } else |
6125 | return error(L: ID.Loc, Msg: "constant expression type mismatch" ); |
6126 | return false; |
6127 | } |
6128 | llvm_unreachable("Invalid ValID" ); |
6129 | } |
6130 | |
6131 | bool LLParser::parseConstantValue(Type *Ty, Constant *&C) { |
6132 | C = nullptr; |
6133 | ValID ID; |
6134 | auto Loc = Lex.getLoc(); |
6135 | if (parseValID(ID, /*PFS=*/nullptr)) |
6136 | return true; |
6137 | switch (ID.Kind) { |
6138 | case ValID::t_APSInt: |
6139 | case ValID::t_APFloat: |
6140 | case ValID::t_Undef: |
6141 | case ValID::t_Constant: |
6142 | case ValID::t_ConstantSplat: |
6143 | case ValID::t_ConstantStruct: |
6144 | case ValID::t_PackedConstantStruct: { |
6145 | Value *V; |
6146 | if (convertValIDToValue(Ty, ID, V, /*PFS=*/nullptr)) |
6147 | return true; |
6148 | assert(isa<Constant>(V) && "Expected a constant value" ); |
6149 | C = cast<Constant>(Val: V); |
6150 | return false; |
6151 | } |
6152 | case ValID::t_Null: |
6153 | C = Constant::getNullValue(Ty); |
6154 | return false; |
6155 | default: |
6156 | return error(L: Loc, Msg: "expected a constant value" ); |
6157 | } |
6158 | } |
6159 | |
6160 | bool LLParser::parseValue(Type *Ty, Value *&V, PerFunctionState *PFS) { |
6161 | V = nullptr; |
6162 | ValID ID; |
6163 | return parseValID(ID, PFS, ExpectedTy: Ty) || |
6164 | convertValIDToValue(Ty, ID, V, PFS); |
6165 | } |
6166 | |
6167 | bool LLParser::parseTypeAndValue(Value *&V, PerFunctionState *PFS) { |
6168 | Type *Ty = nullptr; |
6169 | return parseType(Result&: Ty) || parseValue(Ty, V, PFS); |
6170 | } |
6171 | |
6172 | bool LLParser::parseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, |
6173 | PerFunctionState &PFS) { |
6174 | Value *V; |
6175 | Loc = Lex.getLoc(); |
6176 | if (parseTypeAndValue(V, PFS)) |
6177 | return true; |
6178 | if (!isa<BasicBlock>(Val: V)) |
6179 | return error(L: Loc, Msg: "expected a basic block" ); |
6180 | BB = cast<BasicBlock>(Val: V); |
6181 | return false; |
6182 | } |
6183 | |
6184 | bool isOldDbgFormatIntrinsic(StringRef Name) { |
6185 | // Exit early for the common (non-debug-intrinsic) case. |
6186 | // We can make this the only check when we begin supporting all "llvm.dbg" |
6187 | // intrinsics in the new debug info format. |
6188 | if (!Name.starts_with(Prefix: "llvm.dbg." )) |
6189 | return false; |
6190 | Intrinsic::ID FnID = Function::lookupIntrinsicID(Name); |
6191 | return FnID == Intrinsic::dbg_declare || FnID == Intrinsic::dbg_value || |
6192 | FnID == Intrinsic::dbg_assign; |
6193 | } |
6194 | |
6195 | /// FunctionHeader |
6196 | /// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility |
6197 | /// OptionalCallingConv OptRetAttrs OptUnnamedAddr Type GlobalName |
6198 | /// '(' ArgList ')' OptAddrSpace OptFuncAttrs OptSection OptionalAlign |
6199 | /// OptGC OptionalPrefix OptionalPrologue OptPersonalityFn |
6200 | bool LLParser::(Function *&Fn, bool IsDefine, |
6201 | unsigned &FunctionNumber, |
6202 | SmallVectorImpl<unsigned> &UnnamedArgNums) { |
6203 | // parse the linkage. |
6204 | LocTy LinkageLoc = Lex.getLoc(); |
6205 | unsigned Linkage; |
6206 | unsigned Visibility; |
6207 | unsigned DLLStorageClass; |
6208 | bool DSOLocal; |
6209 | AttrBuilder RetAttrs(M->getContext()); |
6210 | unsigned CC; |
6211 | bool HasLinkage; |
6212 | Type *RetType = nullptr; |
6213 | LocTy RetTypeLoc = Lex.getLoc(); |
6214 | if (parseOptionalLinkage(Res&: Linkage, HasLinkage, Visibility, DLLStorageClass, |
6215 | DSOLocal) || |
6216 | parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(B&: RetAttrs) || |
6217 | parseType(Result&: RetType, Loc&: RetTypeLoc, AllowVoid: true /*void allowed*/)) |
6218 | return true; |
6219 | |
6220 | // Verify that the linkage is ok. |
6221 | switch ((GlobalValue::LinkageTypes)Linkage) { |
6222 | case GlobalValue::ExternalLinkage: |
6223 | break; // always ok. |
6224 | case GlobalValue::ExternalWeakLinkage: |
6225 | if (IsDefine) |
6226 | return error(L: LinkageLoc, Msg: "invalid linkage for function definition" ); |
6227 | break; |
6228 | case GlobalValue::PrivateLinkage: |
6229 | case GlobalValue::InternalLinkage: |
6230 | case GlobalValue::AvailableExternallyLinkage: |
6231 | case GlobalValue::LinkOnceAnyLinkage: |
6232 | case GlobalValue::LinkOnceODRLinkage: |
6233 | case GlobalValue::WeakAnyLinkage: |
6234 | case GlobalValue::WeakODRLinkage: |
6235 | if (!IsDefine) |
6236 | return error(L: LinkageLoc, Msg: "invalid linkage for function declaration" ); |
6237 | break; |
6238 | case GlobalValue::AppendingLinkage: |
6239 | case GlobalValue::CommonLinkage: |
6240 | return error(L: LinkageLoc, Msg: "invalid function linkage type" ); |
6241 | } |
6242 | |
6243 | if (!isValidVisibilityForLinkage(V: Visibility, L: Linkage)) |
6244 | return error(L: LinkageLoc, |
6245 | Msg: "symbol with local linkage must have default visibility" ); |
6246 | |
6247 | if (!isValidDLLStorageClassForLinkage(S: DLLStorageClass, L: Linkage)) |
6248 | return error(L: LinkageLoc, |
6249 | Msg: "symbol with local linkage cannot have a DLL storage class" ); |
6250 | |
6251 | if (!FunctionType::isValidReturnType(RetTy: RetType)) |
6252 | return error(L: RetTypeLoc, Msg: "invalid function return type" ); |
6253 | |
6254 | LocTy NameLoc = Lex.getLoc(); |
6255 | |
6256 | std::string FunctionName; |
6257 | if (Lex.getKind() == lltok::GlobalVar) { |
6258 | FunctionName = Lex.getStrVal(); |
6259 | } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok. |
6260 | FunctionNumber = Lex.getUIntVal(); |
6261 | if (checkValueID(Loc: NameLoc, Kind: "function" , Prefix: "@" , NextID: NumberedVals.getNext(), |
6262 | ID: FunctionNumber)) |
6263 | return true; |
6264 | } else { |
6265 | return tokError(Msg: "expected function name" ); |
6266 | } |
6267 | |
6268 | Lex.Lex(); |
6269 | |
6270 | if (Lex.getKind() != lltok::lparen) |
6271 | return tokError(Msg: "expected '(' in function argument list" ); |
6272 | |
6273 | SmallVector<ArgInfo, 8> ArgList; |
6274 | bool IsVarArg; |
6275 | AttrBuilder FuncAttrs(M->getContext()); |
6276 | std::vector<unsigned> FwdRefAttrGrps; |
6277 | LocTy BuiltinLoc; |
6278 | std::string Section; |
6279 | std::string Partition; |
6280 | MaybeAlign Alignment; |
6281 | std::string GC; |
6282 | GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; |
6283 | unsigned AddrSpace = 0; |
6284 | Constant *Prefix = nullptr; |
6285 | Constant *Prologue = nullptr; |
6286 | Constant *PersonalityFn = nullptr; |
6287 | Comdat *C; |
6288 | |
6289 | if (parseArgumentList(ArgList, UnnamedArgNums, IsVarArg) || |
6290 | parseOptionalUnnamedAddr(UnnamedAddr) || |
6291 | parseOptionalProgramAddrSpace(AddrSpace) || |
6292 | parseFnAttributeValuePairs(B&: FuncAttrs, FwdRefAttrGrps, InAttrGrp: false, |
6293 | BuiltinLoc) || |
6294 | (EatIfPresent(T: lltok::kw_section) && parseStringConstant(Result&: Section)) || |
6295 | (EatIfPresent(T: lltok::kw_partition) && parseStringConstant(Result&: Partition)) || |
6296 | parseOptionalComdat(GlobalName: FunctionName, C) || |
6297 | parseOptionalAlignment(Alignment) || |
6298 | (EatIfPresent(T: lltok::kw_gc) && parseStringConstant(Result&: GC)) || |
6299 | (EatIfPresent(T: lltok::kw_prefix) && parseGlobalTypeAndValue(V&: Prefix)) || |
6300 | (EatIfPresent(T: lltok::kw_prologue) && parseGlobalTypeAndValue(V&: Prologue)) || |
6301 | (EatIfPresent(T: lltok::kw_personality) && |
6302 | parseGlobalTypeAndValue(V&: PersonalityFn))) |
6303 | return true; |
6304 | |
6305 | if (FuncAttrs.contains(Attribute::Builtin)) |
6306 | return error(L: BuiltinLoc, Msg: "'builtin' attribute not valid on function" ); |
6307 | |
6308 | // If the alignment was parsed as an attribute, move to the alignment field. |
6309 | if (MaybeAlign A = FuncAttrs.getAlignment()) { |
6310 | Alignment = A; |
6311 | FuncAttrs.removeAttribute(Attribute::Alignment); |
6312 | } |
6313 | |
6314 | // Okay, if we got here, the function is syntactically valid. Convert types |
6315 | // and do semantic checks. |
6316 | std::vector<Type*> ParamTypeList; |
6317 | SmallVector<AttributeSet, 8> Attrs; |
6318 | |
6319 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
6320 | ParamTypeList.push_back(x: ArgList[i].Ty); |
6321 | Attrs.push_back(Elt: ArgList[i].Attrs); |
6322 | } |
6323 | |
6324 | AttributeList PAL = |
6325 | AttributeList::get(C&: Context, FnAttrs: AttributeSet::get(C&: Context, B: FuncAttrs), |
6326 | RetAttrs: AttributeSet::get(C&: Context, B: RetAttrs), ArgAttrs: Attrs); |
6327 | |
6328 | if (PAL.hasParamAttr(0, Attribute::StructRet) && !RetType->isVoidTy()) |
6329 | return error(L: RetTypeLoc, Msg: "functions with 'sret' argument must return void" ); |
6330 | |
6331 | FunctionType *FT = FunctionType::get(Result: RetType, Params: ParamTypeList, isVarArg: IsVarArg); |
6332 | PointerType *PFT = PointerType::get(ElementType: FT, AddressSpace: AddrSpace); |
6333 | |
6334 | Fn = nullptr; |
6335 | GlobalValue *FwdFn = nullptr; |
6336 | if (!FunctionName.empty()) { |
6337 | // If this was a definition of a forward reference, remove the definition |
6338 | // from the forward reference table and fill in the forward ref. |
6339 | auto FRVI = ForwardRefVals.find(x: FunctionName); |
6340 | if (FRVI != ForwardRefVals.end()) { |
6341 | FwdFn = FRVI->second.first; |
6342 | if (FwdFn->getType() != PFT) |
6343 | return error(L: FRVI->second.second, |
6344 | Msg: "invalid forward reference to " |
6345 | "function '" + |
6346 | FunctionName + |
6347 | "' with wrong type: " |
6348 | "expected '" + |
6349 | getTypeString(T: PFT) + "' but was '" + |
6350 | getTypeString(T: FwdFn->getType()) + "'" ); |
6351 | ForwardRefVals.erase(position: FRVI); |
6352 | } else if ((Fn = M->getFunction(Name: FunctionName))) { |
6353 | // Reject redefinitions. |
6354 | return error(L: NameLoc, |
6355 | Msg: "invalid redefinition of function '" + FunctionName + "'" ); |
6356 | } else if (M->getNamedValue(Name: FunctionName)) { |
6357 | return error(L: NameLoc, Msg: "redefinition of function '@" + FunctionName + "'" ); |
6358 | } |
6359 | |
6360 | } else { |
6361 | // Handle @"", where a name is syntactically specified, but semantically |
6362 | // missing. |
6363 | if (FunctionNumber == (unsigned)-1) |
6364 | FunctionNumber = NumberedVals.getNext(); |
6365 | |
6366 | // If this is a definition of a forward referenced function, make sure the |
6367 | // types agree. |
6368 | auto I = ForwardRefValIDs.find(x: FunctionNumber); |
6369 | if (I != ForwardRefValIDs.end()) { |
6370 | FwdFn = I->second.first; |
6371 | if (FwdFn->getType() != PFT) |
6372 | return error(L: NameLoc, Msg: "type of definition and forward reference of '@" + |
6373 | Twine(FunctionNumber) + |
6374 | "' disagree: " |
6375 | "expected '" + |
6376 | getTypeString(T: PFT) + "' but was '" + |
6377 | getTypeString(T: FwdFn->getType()) + "'" ); |
6378 | ForwardRefValIDs.erase(position: I); |
6379 | } |
6380 | } |
6381 | |
6382 | Fn = Function::Create(Ty: FT, Linkage: GlobalValue::ExternalLinkage, AddrSpace, |
6383 | N: FunctionName, M); |
6384 | |
6385 | assert(Fn->getAddressSpace() == AddrSpace && "Created function in wrong AS" ); |
6386 | |
6387 | if (FunctionName.empty()) |
6388 | NumberedVals.add(ID: FunctionNumber, V: Fn); |
6389 | |
6390 | Fn->setLinkage((GlobalValue::LinkageTypes)Linkage); |
6391 | maybeSetDSOLocal(DSOLocal, GV&: *Fn); |
6392 | Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility); |
6393 | Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass); |
6394 | Fn->setCallingConv(CC); |
6395 | Fn->setAttributes(PAL); |
6396 | Fn->setUnnamedAddr(UnnamedAddr); |
6397 | if (Alignment) |
6398 | Fn->setAlignment(*Alignment); |
6399 | Fn->setSection(Section); |
6400 | Fn->setPartition(Partition); |
6401 | Fn->setComdat(C); |
6402 | Fn->setPersonalityFn(PersonalityFn); |
6403 | if (!GC.empty()) Fn->setGC(GC); |
6404 | Fn->setPrefixData(Prefix); |
6405 | Fn->setPrologueData(Prologue); |
6406 | ForwardRefAttrGroups[Fn] = FwdRefAttrGrps; |
6407 | |
6408 | // Add all of the arguments we parsed to the function. |
6409 | Function::arg_iterator ArgIt = Fn->arg_begin(); |
6410 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) { |
6411 | // If the argument has a name, insert it into the argument symbol table. |
6412 | if (ArgList[i].Name.empty()) continue; |
6413 | |
6414 | // Set the name, if it conflicted, it will be auto-renamed. |
6415 | ArgIt->setName(ArgList[i].Name); |
6416 | |
6417 | if (ArgIt->getName() != ArgList[i].Name) |
6418 | return error(L: ArgList[i].Loc, |
6419 | Msg: "redefinition of argument '%" + ArgList[i].Name + "'" ); |
6420 | } |
6421 | |
6422 | if (FwdFn) { |
6423 | FwdFn->replaceAllUsesWith(V: Fn); |
6424 | FwdFn->eraseFromParent(); |
6425 | } |
6426 | |
6427 | if (IsDefine) |
6428 | return false; |
6429 | |
6430 | // Check the declaration has no block address forward references. |
6431 | ValID ID; |
6432 | if (FunctionName.empty()) { |
6433 | ID.Kind = ValID::t_GlobalID; |
6434 | ID.UIntVal = FunctionNumber; |
6435 | } else { |
6436 | ID.Kind = ValID::t_GlobalName; |
6437 | ID.StrVal = FunctionName; |
6438 | } |
6439 | auto Blocks = ForwardRefBlockAddresses.find(x: ID); |
6440 | if (Blocks != ForwardRefBlockAddresses.end()) |
6441 | return error(L: Blocks->first.Loc, |
6442 | Msg: "cannot take blockaddress inside a declaration" ); |
6443 | return false; |
6444 | } |
6445 | |
6446 | bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() { |
6447 | ValID ID; |
6448 | if (FunctionNumber == -1) { |
6449 | ID.Kind = ValID::t_GlobalName; |
6450 | ID.StrVal = std::string(F.getName()); |
6451 | } else { |
6452 | ID.Kind = ValID::t_GlobalID; |
6453 | ID.UIntVal = FunctionNumber; |
6454 | } |
6455 | |
6456 | auto Blocks = P.ForwardRefBlockAddresses.find(x: ID); |
6457 | if (Blocks == P.ForwardRefBlockAddresses.end()) |
6458 | return false; |
6459 | |
6460 | for (const auto &I : Blocks->second) { |
6461 | const ValID &BBID = I.first; |
6462 | GlobalValue *GV = I.second; |
6463 | |
6464 | assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) && |
6465 | "Expected local id or name" ); |
6466 | BasicBlock *BB; |
6467 | if (BBID.Kind == ValID::t_LocalName) |
6468 | BB = getBB(Name: BBID.StrVal, Loc: BBID.Loc); |
6469 | else |
6470 | BB = getBB(ID: BBID.UIntVal, Loc: BBID.Loc); |
6471 | if (!BB) |
6472 | return P.error(L: BBID.Loc, Msg: "referenced value is not a basic block" ); |
6473 | |
6474 | Value *ResolvedVal = BlockAddress::get(F: &F, BB); |
6475 | ResolvedVal = P.checkValidVariableType(Loc: BBID.Loc, Name: BBID.StrVal, Ty: GV->getType(), |
6476 | Val: ResolvedVal); |
6477 | if (!ResolvedVal) |
6478 | return true; |
6479 | GV->replaceAllUsesWith(V: ResolvedVal); |
6480 | GV->eraseFromParent(); |
6481 | } |
6482 | |
6483 | P.ForwardRefBlockAddresses.erase(position: Blocks); |
6484 | return false; |
6485 | } |
6486 | |
6487 | /// parseFunctionBody |
6488 | /// ::= '{' BasicBlock+ UseListOrderDirective* '}' |
6489 | bool LLParser::parseFunctionBody(Function &Fn, unsigned FunctionNumber, |
6490 | ArrayRef<unsigned> UnnamedArgNums) { |
6491 | if (Lex.getKind() != lltok::lbrace) |
6492 | return tokError(Msg: "expected '{' in function body" ); |
6493 | Lex.Lex(); // eat the {. |
6494 | |
6495 | PerFunctionState PFS(*this, Fn, FunctionNumber, UnnamedArgNums); |
6496 | |
6497 | // Resolve block addresses and allow basic blocks to be forward-declared |
6498 | // within this function. |
6499 | if (PFS.resolveForwardRefBlockAddresses()) |
6500 | return true; |
6501 | SaveAndRestore ScopeExit(BlockAddressPFS, &PFS); |
6502 | |
6503 | // We need at least one basic block. |
6504 | if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder) |
6505 | return tokError(Msg: "function body requires at least one basic block" ); |
6506 | |
6507 | while (Lex.getKind() != lltok::rbrace && |
6508 | Lex.getKind() != lltok::kw_uselistorder) |
6509 | if (parseBasicBlock(PFS)) |
6510 | return true; |
6511 | |
6512 | while (Lex.getKind() != lltok::rbrace) |
6513 | if (parseUseListOrder(PFS: &PFS)) |
6514 | return true; |
6515 | |
6516 | // Eat the }. |
6517 | Lex.Lex(); |
6518 | |
6519 | // Verify function is ok. |
6520 | return PFS.finishFunction(); |
6521 | } |
6522 | |
6523 | /// parseBasicBlock |
6524 | /// ::= (LabelStr|LabelID)? Instruction* |
6525 | bool LLParser::parseBasicBlock(PerFunctionState &PFS) { |
6526 | // If this basic block starts out with a name, remember it. |
6527 | std::string Name; |
6528 | int NameID = -1; |
6529 | LocTy NameLoc = Lex.getLoc(); |
6530 | if (Lex.getKind() == lltok::LabelStr) { |
6531 | Name = Lex.getStrVal(); |
6532 | Lex.Lex(); |
6533 | } else if (Lex.getKind() == lltok::LabelID) { |
6534 | NameID = Lex.getUIntVal(); |
6535 | Lex.Lex(); |
6536 | } |
6537 | |
6538 | BasicBlock *BB = PFS.defineBB(Name, NameID, Loc: NameLoc); |
6539 | if (!BB) |
6540 | return true; |
6541 | |
6542 | std::string NameStr; |
6543 | |
6544 | // Parse the instructions and debug values in this block until we get a |
6545 | // terminator. |
6546 | Instruction *Inst; |
6547 | auto DeleteDbgRecord = [](DbgRecord *DR) { DR->deleteRecord(); }; |
6548 | using DbgRecordPtr = std::unique_ptr<DbgRecord, decltype(DeleteDbgRecord)>; |
6549 | SmallVector<DbgRecordPtr> TrailingDbgRecord; |
6550 | do { |
6551 | // Handle debug records first - there should always be an instruction |
6552 | // following the debug records, i.e. they cannot appear after the block |
6553 | // terminator. |
6554 | while (Lex.getKind() == lltok::hash) { |
6555 | if (SeenOldDbgInfoFormat) |
6556 | return error(L: Lex.getLoc(), Msg: "debug record should not appear in a module " |
6557 | "containing debug info intrinsics" ); |
6558 | if (!SeenNewDbgInfoFormat) |
6559 | M->setNewDbgInfoFormatFlag(true); |
6560 | SeenNewDbgInfoFormat = true; |
6561 | Lex.Lex(); |
6562 | |
6563 | DbgRecord *DR; |
6564 | if (parseDebugRecord(DR, PFS)) |
6565 | return true; |
6566 | TrailingDbgRecord.emplace_back(Args&: DR, Args&: DeleteDbgRecord); |
6567 | } |
6568 | |
6569 | // This instruction may have three possibilities for a name: a) none |
6570 | // specified, b) name specified "%foo =", c) number specified: "%4 =". |
6571 | LocTy NameLoc = Lex.getLoc(); |
6572 | int NameID = -1; |
6573 | NameStr = "" ; |
6574 | |
6575 | if (Lex.getKind() == lltok::LocalVarID) { |
6576 | NameID = Lex.getUIntVal(); |
6577 | Lex.Lex(); |
6578 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after instruction id" )) |
6579 | return true; |
6580 | } else if (Lex.getKind() == lltok::LocalVar) { |
6581 | NameStr = Lex.getStrVal(); |
6582 | Lex.Lex(); |
6583 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after instruction name" )) |
6584 | return true; |
6585 | } |
6586 | |
6587 | switch (parseInstruction(Inst, BB, PFS)) { |
6588 | default: |
6589 | llvm_unreachable("Unknown parseInstruction result!" ); |
6590 | case InstError: return true; |
6591 | case InstNormal: |
6592 | Inst->insertInto(ParentBB: BB, It: BB->end()); |
6593 | |
6594 | // With a normal result, we check to see if the instruction is followed by |
6595 | // a comma and metadata. |
6596 | if (EatIfPresent(T: lltok::comma)) |
6597 | if (parseInstructionMetadata(Inst&: *Inst)) |
6598 | return true; |
6599 | break; |
6600 | case InstExtraComma: |
6601 | Inst->insertInto(ParentBB: BB, It: BB->end()); |
6602 | |
6603 | // If the instruction parser ate an extra comma at the end of it, it |
6604 | // *must* be followed by metadata. |
6605 | if (parseInstructionMetadata(Inst&: *Inst)) |
6606 | return true; |
6607 | break; |
6608 | } |
6609 | |
6610 | // Set the name on the instruction. |
6611 | if (PFS.setInstName(NameID, NameStr, NameLoc, Inst)) |
6612 | return true; |
6613 | |
6614 | // Attach any preceding debug values to this instruction. |
6615 | for (DbgRecordPtr &DR : TrailingDbgRecord) |
6616 | BB->insertDbgRecordBefore(DR: DR.release(), Here: Inst->getIterator()); |
6617 | TrailingDbgRecord.clear(); |
6618 | } while (!Inst->isTerminator()); |
6619 | |
6620 | assert(TrailingDbgRecord.empty() && |
6621 | "All debug values should have been attached to an instruction." ); |
6622 | |
6623 | return false; |
6624 | } |
6625 | |
6626 | /// parseDebugRecord |
6627 | /// ::= #dbg_label '(' MDNode ')' |
6628 | /// ::= #dbg_type '(' Metadata ',' MDNode ',' Metadata ',' |
6629 | /// (MDNode ',' Metadata ',' Metadata ',')? MDNode ')' |
6630 | bool LLParser::parseDebugRecord(DbgRecord *&DR, PerFunctionState &PFS) { |
6631 | using RecordKind = DbgRecord::Kind; |
6632 | using LocType = DbgVariableRecord::LocationType; |
6633 | LocTy DVRLoc = Lex.getLoc(); |
6634 | if (Lex.getKind() != lltok::DbgRecordType) |
6635 | return error(L: DVRLoc, Msg: "expected debug record type here" ); |
6636 | RecordKind RecordType = StringSwitch<RecordKind>(Lex.getStrVal()) |
6637 | .Case(S: "declare" , Value: RecordKind::ValueKind) |
6638 | .Case(S: "value" , Value: RecordKind::ValueKind) |
6639 | .Case(S: "assign" , Value: RecordKind::ValueKind) |
6640 | .Case(S: "label" , Value: RecordKind::LabelKind); |
6641 | |
6642 | // Parsing labels is trivial; parse here and early exit, otherwise go into the |
6643 | // full DbgVariableRecord processing stage. |
6644 | if (RecordType == RecordKind::LabelKind) { |
6645 | Lex.Lex(); |
6646 | if (parseToken(T: lltok::lparen, ErrMsg: "Expected '(' here" )) |
6647 | return true; |
6648 | MDNode *Label; |
6649 | if (parseMDNode(N&: Label)) |
6650 | return true; |
6651 | if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here" )) |
6652 | return true; |
6653 | MDNode *DbgLoc; |
6654 | if (parseMDNode(N&: DbgLoc)) |
6655 | return true; |
6656 | if (parseToken(T: lltok::rparen, ErrMsg: "Expected ')' here" )) |
6657 | return true; |
6658 | DR = DbgLabelRecord::createUnresolvedDbgLabelRecord(Label, DL: DbgLoc); |
6659 | return false; |
6660 | } |
6661 | |
6662 | LocType ValueType = StringSwitch<LocType>(Lex.getStrVal()) |
6663 | .Case(S: "declare" , Value: LocType::Declare) |
6664 | .Case(S: "value" , Value: LocType::Value) |
6665 | .Case(S: "assign" , Value: LocType::Assign); |
6666 | |
6667 | Lex.Lex(); |
6668 | if (parseToken(T: lltok::lparen, ErrMsg: "Expected '(' here" )) |
6669 | return true; |
6670 | |
6671 | // Parse Value field. |
6672 | Metadata *ValLocMD; |
6673 | if (parseMetadata(MD&: ValLocMD, PFS: &PFS)) |
6674 | return true; |
6675 | if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here" )) |
6676 | return true; |
6677 | |
6678 | // Parse Variable field. |
6679 | MDNode *Variable; |
6680 | if (parseMDNode(N&: Variable)) |
6681 | return true; |
6682 | if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here" )) |
6683 | return true; |
6684 | |
6685 | // Parse Expression field. |
6686 | MDNode *Expression; |
6687 | if (parseMDNode(N&: Expression)) |
6688 | return true; |
6689 | if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here" )) |
6690 | return true; |
6691 | |
6692 | // Parse additional fields for #dbg_assign. |
6693 | MDNode *AssignID = nullptr; |
6694 | Metadata *AddressLocation = nullptr; |
6695 | MDNode *AddressExpression = nullptr; |
6696 | if (ValueType == LocType::Assign) { |
6697 | // Parse DIAssignID. |
6698 | if (parseMDNode(N&: AssignID)) |
6699 | return true; |
6700 | if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here" )) |
6701 | return true; |
6702 | |
6703 | // Parse address ValueAsMetadata. |
6704 | if (parseMetadata(MD&: AddressLocation, PFS: &PFS)) |
6705 | return true; |
6706 | if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here" )) |
6707 | return true; |
6708 | |
6709 | // Parse address DIExpression. |
6710 | if (parseMDNode(N&: AddressExpression)) |
6711 | return true; |
6712 | if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here" )) |
6713 | return true; |
6714 | } |
6715 | |
6716 | /// Parse DILocation. |
6717 | MDNode *DebugLoc; |
6718 | if (parseMDNode(N&: DebugLoc)) |
6719 | return true; |
6720 | |
6721 | if (parseToken(T: lltok::rparen, ErrMsg: "Expected ')' here" )) |
6722 | return true; |
6723 | DR = DbgVariableRecord::createUnresolvedDbgVariableRecord( |
6724 | Type: ValueType, Val: ValLocMD, Variable, Expression, AssignID, Address: AddressLocation, |
6725 | AddressExpression, DI: DebugLoc); |
6726 | return false; |
6727 | } |
6728 | //===----------------------------------------------------------------------===// |
6729 | // Instruction Parsing. |
6730 | //===----------------------------------------------------------------------===// |
6731 | |
6732 | /// parseInstruction - parse one of the many different instructions. |
6733 | /// |
6734 | int LLParser::parseInstruction(Instruction *&Inst, BasicBlock *BB, |
6735 | PerFunctionState &PFS) { |
6736 | lltok::Kind Token = Lex.getKind(); |
6737 | if (Token == lltok::Eof) |
6738 | return tokError(Msg: "found end of file when expecting more instructions" ); |
6739 | LocTy Loc = Lex.getLoc(); |
6740 | unsigned KeywordVal = Lex.getUIntVal(); |
6741 | Lex.Lex(); // Eat the keyword. |
6742 | |
6743 | switch (Token) { |
6744 | default: |
6745 | return error(L: Loc, Msg: "expected instruction opcode" ); |
6746 | // Terminator Instructions. |
6747 | case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false; |
6748 | case lltok::kw_ret: |
6749 | return parseRet(Inst, BB, PFS); |
6750 | case lltok::kw_br: |
6751 | return parseBr(Inst, PFS); |
6752 | case lltok::kw_switch: |
6753 | return parseSwitch(Inst, PFS); |
6754 | case lltok::kw_indirectbr: |
6755 | return parseIndirectBr(Inst, PFS); |
6756 | case lltok::kw_invoke: |
6757 | return parseInvoke(Inst, PFS); |
6758 | case lltok::kw_resume: |
6759 | return parseResume(Inst, PFS); |
6760 | case lltok::kw_cleanupret: |
6761 | return parseCleanupRet(Inst, PFS); |
6762 | case lltok::kw_catchret: |
6763 | return parseCatchRet(Inst, PFS); |
6764 | case lltok::kw_catchswitch: |
6765 | return parseCatchSwitch(Inst, PFS); |
6766 | case lltok::kw_catchpad: |
6767 | return parseCatchPad(Inst, PFS); |
6768 | case lltok::kw_cleanuppad: |
6769 | return parseCleanupPad(Inst, PFS); |
6770 | case lltok::kw_callbr: |
6771 | return parseCallBr(Inst, PFS); |
6772 | // Unary Operators. |
6773 | case lltok::kw_fneg: { |
6774 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
6775 | int Res = parseUnaryOp(Inst, PFS, Opc: KeywordVal, /*IsFP*/ true); |
6776 | if (Res != 0) |
6777 | return Res; |
6778 | if (FMF.any()) |
6779 | Inst->setFastMathFlags(FMF); |
6780 | return false; |
6781 | } |
6782 | // Binary Operators. |
6783 | case lltok::kw_add: |
6784 | case lltok::kw_sub: |
6785 | case lltok::kw_mul: |
6786 | case lltok::kw_shl: { |
6787 | bool NUW = EatIfPresent(T: lltok::kw_nuw); |
6788 | bool NSW = EatIfPresent(T: lltok::kw_nsw); |
6789 | if (!NUW) NUW = EatIfPresent(T: lltok::kw_nuw); |
6790 | |
6791 | if (parseArithmetic(Inst, PFS, Opc: KeywordVal, /*IsFP*/ false)) |
6792 | return true; |
6793 | |
6794 | if (NUW) cast<BinaryOperator>(Val: Inst)->setHasNoUnsignedWrap(true); |
6795 | if (NSW) cast<BinaryOperator>(Val: Inst)->setHasNoSignedWrap(true); |
6796 | return false; |
6797 | } |
6798 | case lltok::kw_fadd: |
6799 | case lltok::kw_fsub: |
6800 | case lltok::kw_fmul: |
6801 | case lltok::kw_fdiv: |
6802 | case lltok::kw_frem: { |
6803 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
6804 | int Res = parseArithmetic(Inst, PFS, Opc: KeywordVal, /*IsFP*/ true); |
6805 | if (Res != 0) |
6806 | return Res; |
6807 | if (FMF.any()) |
6808 | Inst->setFastMathFlags(FMF); |
6809 | return 0; |
6810 | } |
6811 | |
6812 | case lltok::kw_sdiv: |
6813 | case lltok::kw_udiv: |
6814 | case lltok::kw_lshr: |
6815 | case lltok::kw_ashr: { |
6816 | bool Exact = EatIfPresent(T: lltok::kw_exact); |
6817 | |
6818 | if (parseArithmetic(Inst, PFS, Opc: KeywordVal, /*IsFP*/ false)) |
6819 | return true; |
6820 | if (Exact) cast<BinaryOperator>(Val: Inst)->setIsExact(true); |
6821 | return false; |
6822 | } |
6823 | |
6824 | case lltok::kw_urem: |
6825 | case lltok::kw_srem: |
6826 | return parseArithmetic(Inst, PFS, Opc: KeywordVal, |
6827 | /*IsFP*/ false); |
6828 | case lltok::kw_or: { |
6829 | bool Disjoint = EatIfPresent(T: lltok::kw_disjoint); |
6830 | if (parseLogical(Inst, PFS, Opc: KeywordVal)) |
6831 | return true; |
6832 | if (Disjoint) |
6833 | cast<PossiblyDisjointInst>(Val: Inst)->setIsDisjoint(true); |
6834 | return false; |
6835 | } |
6836 | case lltok::kw_and: |
6837 | case lltok::kw_xor: |
6838 | return parseLogical(Inst, PFS, Opc: KeywordVal); |
6839 | case lltok::kw_icmp: |
6840 | return parseCompare(Inst, PFS, Opc: KeywordVal); |
6841 | case lltok::kw_fcmp: { |
6842 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
6843 | int Res = parseCompare(Inst, PFS, Opc: KeywordVal); |
6844 | if (Res != 0) |
6845 | return Res; |
6846 | if (FMF.any()) |
6847 | Inst->setFastMathFlags(FMF); |
6848 | return 0; |
6849 | } |
6850 | |
6851 | // Casts. |
6852 | case lltok::kw_uitofp: |
6853 | case lltok::kw_zext: { |
6854 | bool NonNeg = EatIfPresent(T: lltok::kw_nneg); |
6855 | bool Res = parseCast(Inst, PFS, Opc: KeywordVal); |
6856 | if (Res != 0) |
6857 | return Res; |
6858 | if (NonNeg) |
6859 | Inst->setNonNeg(); |
6860 | return 0; |
6861 | } |
6862 | case lltok::kw_trunc: { |
6863 | bool NUW = EatIfPresent(T: lltok::kw_nuw); |
6864 | bool NSW = EatIfPresent(T: lltok::kw_nsw); |
6865 | if (!NUW) |
6866 | NUW = EatIfPresent(T: lltok::kw_nuw); |
6867 | if (parseCast(Inst, PFS, Opc: KeywordVal)) |
6868 | return true; |
6869 | if (NUW) |
6870 | cast<TruncInst>(Val: Inst)->setHasNoUnsignedWrap(true); |
6871 | if (NSW) |
6872 | cast<TruncInst>(Val: Inst)->setHasNoSignedWrap(true); |
6873 | return false; |
6874 | } |
6875 | case lltok::kw_sext: |
6876 | case lltok::kw_fptrunc: |
6877 | case lltok::kw_fpext: |
6878 | case lltok::kw_bitcast: |
6879 | case lltok::kw_addrspacecast: |
6880 | case lltok::kw_sitofp: |
6881 | case lltok::kw_fptoui: |
6882 | case lltok::kw_fptosi: |
6883 | case lltok::kw_inttoptr: |
6884 | case lltok::kw_ptrtoint: |
6885 | return parseCast(Inst, PFS, Opc: KeywordVal); |
6886 | // Other. |
6887 | case lltok::kw_select: { |
6888 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
6889 | int Res = parseSelect(Inst, PFS); |
6890 | if (Res != 0) |
6891 | return Res; |
6892 | if (FMF.any()) { |
6893 | if (!isa<FPMathOperator>(Val: Inst)) |
6894 | return error(L: Loc, Msg: "fast-math-flags specified for select without " |
6895 | "floating-point scalar or vector return type" ); |
6896 | Inst->setFastMathFlags(FMF); |
6897 | } |
6898 | return 0; |
6899 | } |
6900 | case lltok::kw_va_arg: |
6901 | return parseVAArg(Inst, PFS); |
6902 | case lltok::kw_extractelement: |
6903 | return parseExtractElement(Inst, PFS); |
6904 | case lltok::kw_insertelement: |
6905 | return parseInsertElement(Inst, PFS); |
6906 | case lltok::kw_shufflevector: |
6907 | return parseShuffleVector(Inst, PFS); |
6908 | case lltok::kw_phi: { |
6909 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
6910 | int Res = parsePHI(Inst, PFS); |
6911 | if (Res != 0) |
6912 | return Res; |
6913 | if (FMF.any()) { |
6914 | if (!isa<FPMathOperator>(Val: Inst)) |
6915 | return error(L: Loc, Msg: "fast-math-flags specified for phi without " |
6916 | "floating-point scalar or vector return type" ); |
6917 | Inst->setFastMathFlags(FMF); |
6918 | } |
6919 | return 0; |
6920 | } |
6921 | case lltok::kw_landingpad: |
6922 | return parseLandingPad(Inst, PFS); |
6923 | case lltok::kw_freeze: |
6924 | return parseFreeze(I&: Inst, PFS); |
6925 | // Call. |
6926 | case lltok::kw_call: |
6927 | return parseCall(Inst, PFS, TCK: CallInst::TCK_None); |
6928 | case lltok::kw_tail: |
6929 | return parseCall(Inst, PFS, TCK: CallInst::TCK_Tail); |
6930 | case lltok::kw_musttail: |
6931 | return parseCall(Inst, PFS, TCK: CallInst::TCK_MustTail); |
6932 | case lltok::kw_notail: |
6933 | return parseCall(Inst, PFS, TCK: CallInst::TCK_NoTail); |
6934 | // Memory. |
6935 | case lltok::kw_alloca: |
6936 | return parseAlloc(Inst, PFS); |
6937 | case lltok::kw_load: |
6938 | return parseLoad(Inst, PFS); |
6939 | case lltok::kw_store: |
6940 | return parseStore(Inst, PFS); |
6941 | case lltok::kw_cmpxchg: |
6942 | return parseCmpXchg(Inst, PFS); |
6943 | case lltok::kw_atomicrmw: |
6944 | return parseAtomicRMW(Inst, PFS); |
6945 | case lltok::kw_fence: |
6946 | return parseFence(Inst, PFS); |
6947 | case lltok::kw_getelementptr: |
6948 | return parseGetElementPtr(Inst, PFS); |
6949 | case lltok::kw_extractvalue: |
6950 | return parseExtractValue(Inst, PFS); |
6951 | case lltok::kw_insertvalue: |
6952 | return parseInsertValue(Inst, PFS); |
6953 | } |
6954 | } |
6955 | |
6956 | /// parseCmpPredicate - parse an integer or fp predicate, based on Kind. |
6957 | bool LLParser::parseCmpPredicate(unsigned &P, unsigned Opc) { |
6958 | if (Opc == Instruction::FCmp) { |
6959 | switch (Lex.getKind()) { |
6960 | default: |
6961 | return tokError(Msg: "expected fcmp predicate (e.g. 'oeq')" ); |
6962 | case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break; |
6963 | case lltok::kw_one: P = CmpInst::FCMP_ONE; break; |
6964 | case lltok::kw_olt: P = CmpInst::FCMP_OLT; break; |
6965 | case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break; |
6966 | case lltok::kw_ole: P = CmpInst::FCMP_OLE; break; |
6967 | case lltok::kw_oge: P = CmpInst::FCMP_OGE; break; |
6968 | case lltok::kw_ord: P = CmpInst::FCMP_ORD; break; |
6969 | case lltok::kw_uno: P = CmpInst::FCMP_UNO; break; |
6970 | case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break; |
6971 | case lltok::kw_une: P = CmpInst::FCMP_UNE; break; |
6972 | case lltok::kw_ult: P = CmpInst::FCMP_ULT; break; |
6973 | case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break; |
6974 | case lltok::kw_ule: P = CmpInst::FCMP_ULE; break; |
6975 | case lltok::kw_uge: P = CmpInst::FCMP_UGE; break; |
6976 | case lltok::kw_true: P = CmpInst::FCMP_TRUE; break; |
6977 | case lltok::kw_false: P = CmpInst::FCMP_FALSE; break; |
6978 | } |
6979 | } else { |
6980 | switch (Lex.getKind()) { |
6981 | default: |
6982 | return tokError(Msg: "expected icmp predicate (e.g. 'eq')" ); |
6983 | case lltok::kw_eq: P = CmpInst::ICMP_EQ; break; |
6984 | case lltok::kw_ne: P = CmpInst::ICMP_NE; break; |
6985 | case lltok::kw_slt: P = CmpInst::ICMP_SLT; break; |
6986 | case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break; |
6987 | case lltok::kw_sle: P = CmpInst::ICMP_SLE; break; |
6988 | case lltok::kw_sge: P = CmpInst::ICMP_SGE; break; |
6989 | case lltok::kw_ult: P = CmpInst::ICMP_ULT; break; |
6990 | case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break; |
6991 | case lltok::kw_ule: P = CmpInst::ICMP_ULE; break; |
6992 | case lltok::kw_uge: P = CmpInst::ICMP_UGE; break; |
6993 | } |
6994 | } |
6995 | Lex.Lex(); |
6996 | return false; |
6997 | } |
6998 | |
6999 | //===----------------------------------------------------------------------===// |
7000 | // Terminator Instructions. |
7001 | //===----------------------------------------------------------------------===// |
7002 | |
7003 | /// parseRet - parse a return instruction. |
7004 | /// ::= 'ret' void (',' !dbg, !1)* |
7005 | /// ::= 'ret' TypeAndValue (',' !dbg, !1)* |
7006 | bool LLParser::parseRet(Instruction *&Inst, BasicBlock *BB, |
7007 | PerFunctionState &PFS) { |
7008 | SMLoc TypeLoc = Lex.getLoc(); |
7009 | Type *Ty = nullptr; |
7010 | if (parseType(Result&: Ty, AllowVoid: true /*void allowed*/)) |
7011 | return true; |
7012 | |
7013 | Type *ResType = PFS.getFunction().getReturnType(); |
7014 | |
7015 | if (Ty->isVoidTy()) { |
7016 | if (!ResType->isVoidTy()) |
7017 | return error(L: TypeLoc, Msg: "value doesn't match function result type '" + |
7018 | getTypeString(T: ResType) + "'" ); |
7019 | |
7020 | Inst = ReturnInst::Create(C&: Context); |
7021 | return false; |
7022 | } |
7023 | |
7024 | Value *RV; |
7025 | if (parseValue(Ty, V&: RV, PFS)) |
7026 | return true; |
7027 | |
7028 | if (ResType != RV->getType()) |
7029 | return error(L: TypeLoc, Msg: "value doesn't match function result type '" + |
7030 | getTypeString(T: ResType) + "'" ); |
7031 | |
7032 | Inst = ReturnInst::Create(C&: Context, retVal: RV); |
7033 | return false; |
7034 | } |
7035 | |
7036 | /// parseBr |
7037 | /// ::= 'br' TypeAndValue |
7038 | /// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
7039 | bool LLParser::parseBr(Instruction *&Inst, PerFunctionState &PFS) { |
7040 | LocTy Loc, Loc2; |
7041 | Value *Op0; |
7042 | BasicBlock *Op1, *Op2; |
7043 | if (parseTypeAndValue(V&: Op0, Loc, PFS)) |
7044 | return true; |
7045 | |
7046 | if (BasicBlock *BB = dyn_cast<BasicBlock>(Val: Op0)) { |
7047 | Inst = BranchInst::Create(IfTrue: BB); |
7048 | return false; |
7049 | } |
7050 | |
7051 | if (Op0->getType() != Type::getInt1Ty(C&: Context)) |
7052 | return error(L: Loc, Msg: "branch condition must have 'i1' type" ); |
7053 | |
7054 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' after branch condition" ) || |
7055 | parseTypeAndBasicBlock(BB&: Op1, Loc, PFS) || |
7056 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after true destination" ) || |
7057 | parseTypeAndBasicBlock(BB&: Op2, Loc&: Loc2, PFS)) |
7058 | return true; |
7059 | |
7060 | Inst = BranchInst::Create(IfTrue: Op1, IfFalse: Op2, Cond: Op0); |
7061 | return false; |
7062 | } |
7063 | |
7064 | /// parseSwitch |
7065 | /// Instruction |
7066 | /// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']' |
7067 | /// JumpTable |
7068 | /// ::= (TypeAndValue ',' TypeAndValue)* |
7069 | bool LLParser::parseSwitch(Instruction *&Inst, PerFunctionState &PFS) { |
7070 | LocTy CondLoc, BBLoc; |
7071 | Value *Cond; |
7072 | BasicBlock *DefaultBB; |
7073 | if (parseTypeAndValue(V&: Cond, Loc&: CondLoc, PFS) || |
7074 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after switch condition" ) || |
7075 | parseTypeAndBasicBlock(BB&: DefaultBB, Loc&: BBLoc, PFS) || |
7076 | parseToken(T: lltok::lsquare, ErrMsg: "expected '[' with switch table" )) |
7077 | return true; |
7078 | |
7079 | if (!Cond->getType()->isIntegerTy()) |
7080 | return error(L: CondLoc, Msg: "switch condition must have integer type" ); |
7081 | |
7082 | // parse the jump table pairs. |
7083 | SmallPtrSet<Value*, 32> SeenCases; |
7084 | SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table; |
7085 | while (Lex.getKind() != lltok::rsquare) { |
7086 | Value *Constant; |
7087 | BasicBlock *DestBB; |
7088 | |
7089 | if (parseTypeAndValue(V&: Constant, Loc&: CondLoc, PFS) || |
7090 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after case value" ) || |
7091 | parseTypeAndBasicBlock(BB&: DestBB, PFS)) |
7092 | return true; |
7093 | |
7094 | if (!SeenCases.insert(Ptr: Constant).second) |
7095 | return error(L: CondLoc, Msg: "duplicate case value in switch" ); |
7096 | if (!isa<ConstantInt>(Val: Constant)) |
7097 | return error(L: CondLoc, Msg: "case value is not a constant integer" ); |
7098 | |
7099 | Table.push_back(Elt: std::make_pair(x: cast<ConstantInt>(Val: Constant), y&: DestBB)); |
7100 | } |
7101 | |
7102 | Lex.Lex(); // Eat the ']'. |
7103 | |
7104 | SwitchInst *SI = SwitchInst::Create(Value: Cond, Default: DefaultBB, NumCases: Table.size()); |
7105 | for (unsigned i = 0, e = Table.size(); i != e; ++i) |
7106 | SI->addCase(OnVal: Table[i].first, Dest: Table[i].second); |
7107 | Inst = SI; |
7108 | return false; |
7109 | } |
7110 | |
7111 | /// parseIndirectBr |
7112 | /// Instruction |
7113 | /// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']' |
7114 | bool LLParser::parseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) { |
7115 | LocTy AddrLoc; |
7116 | Value *Address; |
7117 | if (parseTypeAndValue(V&: Address, Loc&: AddrLoc, PFS) || |
7118 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after indirectbr address" ) || |
7119 | parseToken(T: lltok::lsquare, ErrMsg: "expected '[' with indirectbr" )) |
7120 | return true; |
7121 | |
7122 | if (!Address->getType()->isPointerTy()) |
7123 | return error(L: AddrLoc, Msg: "indirectbr address must have pointer type" ); |
7124 | |
7125 | // parse the destination list. |
7126 | SmallVector<BasicBlock*, 16> DestList; |
7127 | |
7128 | if (Lex.getKind() != lltok::rsquare) { |
7129 | BasicBlock *DestBB; |
7130 | if (parseTypeAndBasicBlock(BB&: DestBB, PFS)) |
7131 | return true; |
7132 | DestList.push_back(Elt: DestBB); |
7133 | |
7134 | while (EatIfPresent(T: lltok::comma)) { |
7135 | if (parseTypeAndBasicBlock(BB&: DestBB, PFS)) |
7136 | return true; |
7137 | DestList.push_back(Elt: DestBB); |
7138 | } |
7139 | } |
7140 | |
7141 | if (parseToken(T: lltok::rsquare, ErrMsg: "expected ']' at end of block list" )) |
7142 | return true; |
7143 | |
7144 | IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests: DestList.size()); |
7145 | for (unsigned i = 0, e = DestList.size(); i != e; ++i) |
7146 | IBI->addDestination(Dest: DestList[i]); |
7147 | Inst = IBI; |
7148 | return false; |
7149 | } |
7150 | |
7151 | // If RetType is a non-function pointer type, then this is the short syntax |
7152 | // for the call, which means that RetType is just the return type. Infer the |
7153 | // rest of the function argument types from the arguments that are present. |
7154 | bool LLParser::resolveFunctionType(Type *RetType, |
7155 | const SmallVector<ParamInfo, 16> &ArgList, |
7156 | FunctionType *&FuncTy) { |
7157 | FuncTy = dyn_cast<FunctionType>(Val: RetType); |
7158 | if (!FuncTy) { |
7159 | // Pull out the types of all of the arguments... |
7160 | std::vector<Type*> ParamTypes; |
7161 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) |
7162 | ParamTypes.push_back(x: ArgList[i].V->getType()); |
7163 | |
7164 | if (!FunctionType::isValidReturnType(RetTy: RetType)) |
7165 | return true; |
7166 | |
7167 | FuncTy = FunctionType::get(Result: RetType, Params: ParamTypes, isVarArg: false); |
7168 | } |
7169 | return false; |
7170 | } |
7171 | |
7172 | /// parseInvoke |
7173 | /// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList |
7174 | /// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue |
7175 | bool LLParser::parseInvoke(Instruction *&Inst, PerFunctionState &PFS) { |
7176 | LocTy CallLoc = Lex.getLoc(); |
7177 | AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext()); |
7178 | std::vector<unsigned> FwdRefAttrGrps; |
7179 | LocTy NoBuiltinLoc; |
7180 | unsigned CC; |
7181 | unsigned InvokeAddrSpace; |
7182 | Type *RetType = nullptr; |
7183 | LocTy RetTypeLoc; |
7184 | ValID CalleeID; |
7185 | SmallVector<ParamInfo, 16> ArgList; |
7186 | SmallVector<OperandBundleDef, 2> BundleList; |
7187 | |
7188 | BasicBlock *NormalBB, *UnwindBB; |
7189 | if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(B&: RetAttrs) || |
7190 | parseOptionalProgramAddrSpace(AddrSpace&: InvokeAddrSpace) || |
7191 | parseType(Result&: RetType, Loc&: RetTypeLoc, AllowVoid: true /*void allowed*/) || |
7192 | parseValID(ID&: CalleeID, PFS: &PFS) || parseParameterList(ArgList, PFS) || |
7193 | parseFnAttributeValuePairs(B&: FnAttrs, FwdRefAttrGrps, InAttrGrp: false, |
7194 | BuiltinLoc&: NoBuiltinLoc) || |
7195 | parseOptionalOperandBundles(BundleList, PFS) || |
7196 | parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' in invoke" ) || |
7197 | parseTypeAndBasicBlock(BB&: NormalBB, PFS) || |
7198 | parseToken(T: lltok::kw_unwind, ErrMsg: "expected 'unwind' in invoke" ) || |
7199 | parseTypeAndBasicBlock(BB&: UnwindBB, PFS)) |
7200 | return true; |
7201 | |
7202 | // If RetType is a non-function pointer type, then this is the short syntax |
7203 | // for the call, which means that RetType is just the return type. Infer the |
7204 | // rest of the function argument types from the arguments that are present. |
7205 | FunctionType *Ty; |
7206 | if (resolveFunctionType(RetType, ArgList, FuncTy&: Ty)) |
7207 | return error(L: RetTypeLoc, Msg: "Invalid result type for LLVM function" ); |
7208 | |
7209 | CalleeID.FTy = Ty; |
7210 | |
7211 | // Look up the callee. |
7212 | Value *Callee; |
7213 | if (convertValIDToValue(Ty: PointerType::get(ElementType: Ty, AddressSpace: InvokeAddrSpace), ID&: CalleeID, |
7214 | V&: Callee, PFS: &PFS)) |
7215 | return true; |
7216 | |
7217 | // Set up the Attribute for the function. |
7218 | SmallVector<Value *, 8> Args; |
7219 | SmallVector<AttributeSet, 8> ArgAttrs; |
7220 | |
7221 | // Loop through FunctionType's arguments and ensure they are specified |
7222 | // correctly. Also, gather any parameter attributes. |
7223 | FunctionType::param_iterator I = Ty->param_begin(); |
7224 | FunctionType::param_iterator E = Ty->param_end(); |
7225 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
7226 | Type *ExpectedTy = nullptr; |
7227 | if (I != E) { |
7228 | ExpectedTy = *I++; |
7229 | } else if (!Ty->isVarArg()) { |
7230 | return error(L: ArgList[i].Loc, Msg: "too many arguments specified" ); |
7231 | } |
7232 | |
7233 | if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) |
7234 | return error(L: ArgList[i].Loc, Msg: "argument is not of expected type '" + |
7235 | getTypeString(T: ExpectedTy) + "'" ); |
7236 | Args.push_back(Elt: ArgList[i].V); |
7237 | ArgAttrs.push_back(Elt: ArgList[i].Attrs); |
7238 | } |
7239 | |
7240 | if (I != E) |
7241 | return error(L: CallLoc, Msg: "not enough parameters specified for call" ); |
7242 | |
7243 | // Finish off the Attribute and check them |
7244 | AttributeList PAL = |
7245 | AttributeList::get(C&: Context, FnAttrs: AttributeSet::get(C&: Context, B: FnAttrs), |
7246 | RetAttrs: AttributeSet::get(C&: Context, B: RetAttrs), ArgAttrs); |
7247 | |
7248 | InvokeInst *II = |
7249 | InvokeInst::Create(Ty, Func: Callee, IfNormal: NormalBB, IfException: UnwindBB, Args, Bundles: BundleList); |
7250 | II->setCallingConv(CC); |
7251 | II->setAttributes(PAL); |
7252 | ForwardRefAttrGroups[II] = FwdRefAttrGrps; |
7253 | Inst = II; |
7254 | return false; |
7255 | } |
7256 | |
7257 | /// parseResume |
7258 | /// ::= 'resume' TypeAndValue |
7259 | bool LLParser::parseResume(Instruction *&Inst, PerFunctionState &PFS) { |
7260 | Value *Exn; LocTy ExnLoc; |
7261 | if (parseTypeAndValue(V&: Exn, Loc&: ExnLoc, PFS)) |
7262 | return true; |
7263 | |
7264 | ResumeInst *RI = ResumeInst::Create(Exn); |
7265 | Inst = RI; |
7266 | return false; |
7267 | } |
7268 | |
7269 | bool LLParser::parseExceptionArgs(SmallVectorImpl<Value *> &Args, |
7270 | PerFunctionState &PFS) { |
7271 | if (parseToken(T: lltok::lsquare, ErrMsg: "expected '[' in catchpad/cleanuppad" )) |
7272 | return true; |
7273 | |
7274 | while (Lex.getKind() != lltok::rsquare) { |
7275 | // If this isn't the first argument, we need a comma. |
7276 | if (!Args.empty() && |
7277 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in argument list" )) |
7278 | return true; |
7279 | |
7280 | // parse the argument. |
7281 | LocTy ArgLoc; |
7282 | Type *ArgTy = nullptr; |
7283 | if (parseType(Result&: ArgTy, Loc&: ArgLoc)) |
7284 | return true; |
7285 | |
7286 | Value *V; |
7287 | if (ArgTy->isMetadataTy()) { |
7288 | if (parseMetadataAsValue(V, PFS)) |
7289 | return true; |
7290 | } else { |
7291 | if (parseValue(Ty: ArgTy, V, PFS)) |
7292 | return true; |
7293 | } |
7294 | Args.push_back(Elt: V); |
7295 | } |
7296 | |
7297 | Lex.Lex(); // Lex the ']'. |
7298 | return false; |
7299 | } |
7300 | |
7301 | /// parseCleanupRet |
7302 | /// ::= 'cleanupret' from Value unwind ('to' 'caller' | TypeAndValue) |
7303 | bool LLParser::parseCleanupRet(Instruction *&Inst, PerFunctionState &PFS) { |
7304 | Value *CleanupPad = nullptr; |
7305 | |
7306 | if (parseToken(T: lltok::kw_from, ErrMsg: "expected 'from' after cleanupret" )) |
7307 | return true; |
7308 | |
7309 | if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: CleanupPad, PFS)) |
7310 | return true; |
7311 | |
7312 | if (parseToken(T: lltok::kw_unwind, ErrMsg: "expected 'unwind' in cleanupret" )) |
7313 | return true; |
7314 | |
7315 | BasicBlock *UnwindBB = nullptr; |
7316 | if (Lex.getKind() == lltok::kw_to) { |
7317 | Lex.Lex(); |
7318 | if (parseToken(T: lltok::kw_caller, ErrMsg: "expected 'caller' in cleanupret" )) |
7319 | return true; |
7320 | } else { |
7321 | if (parseTypeAndBasicBlock(BB&: UnwindBB, PFS)) { |
7322 | return true; |
7323 | } |
7324 | } |
7325 | |
7326 | Inst = CleanupReturnInst::Create(CleanupPad, UnwindBB); |
7327 | return false; |
7328 | } |
7329 | |
7330 | /// parseCatchRet |
7331 | /// ::= 'catchret' from Parent Value 'to' TypeAndValue |
7332 | bool LLParser::parseCatchRet(Instruction *&Inst, PerFunctionState &PFS) { |
7333 | Value *CatchPad = nullptr; |
7334 | |
7335 | if (parseToken(T: lltok::kw_from, ErrMsg: "expected 'from' after catchret" )) |
7336 | return true; |
7337 | |
7338 | if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: CatchPad, PFS)) |
7339 | return true; |
7340 | |
7341 | BasicBlock *BB; |
7342 | if (parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' in catchret" ) || |
7343 | parseTypeAndBasicBlock(BB, PFS)) |
7344 | return true; |
7345 | |
7346 | Inst = CatchReturnInst::Create(CatchPad, BB); |
7347 | return false; |
7348 | } |
7349 | |
7350 | /// parseCatchSwitch |
7351 | /// ::= 'catchswitch' within Parent |
7352 | bool LLParser::parseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS) { |
7353 | Value *ParentPad; |
7354 | |
7355 | if (parseToken(T: lltok::kw_within, ErrMsg: "expected 'within' after catchswitch" )) |
7356 | return true; |
7357 | |
7358 | if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar && |
7359 | Lex.getKind() != lltok::LocalVarID) |
7360 | return tokError(Msg: "expected scope value for catchswitch" ); |
7361 | |
7362 | if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: ParentPad, PFS)) |
7363 | return true; |
7364 | |
7365 | if (parseToken(T: lltok::lsquare, ErrMsg: "expected '[' with catchswitch labels" )) |
7366 | return true; |
7367 | |
7368 | SmallVector<BasicBlock *, 32> Table; |
7369 | do { |
7370 | BasicBlock *DestBB; |
7371 | if (parseTypeAndBasicBlock(BB&: DestBB, PFS)) |
7372 | return true; |
7373 | Table.push_back(Elt: DestBB); |
7374 | } while (EatIfPresent(T: lltok::comma)); |
7375 | |
7376 | if (parseToken(T: lltok::rsquare, ErrMsg: "expected ']' after catchswitch labels" )) |
7377 | return true; |
7378 | |
7379 | if (parseToken(T: lltok::kw_unwind, ErrMsg: "expected 'unwind' after catchswitch scope" )) |
7380 | return true; |
7381 | |
7382 | BasicBlock *UnwindBB = nullptr; |
7383 | if (EatIfPresent(T: lltok::kw_to)) { |
7384 | if (parseToken(T: lltok::kw_caller, ErrMsg: "expected 'caller' in catchswitch" )) |
7385 | return true; |
7386 | } else { |
7387 | if (parseTypeAndBasicBlock(BB&: UnwindBB, PFS)) |
7388 | return true; |
7389 | } |
7390 | |
7391 | auto *CatchSwitch = |
7392 | CatchSwitchInst::Create(ParentPad, UnwindDest: UnwindBB, NumHandlers: Table.size()); |
7393 | for (BasicBlock *DestBB : Table) |
7394 | CatchSwitch->addHandler(Dest: DestBB); |
7395 | Inst = CatchSwitch; |
7396 | return false; |
7397 | } |
7398 | |
7399 | /// parseCatchPad |
7400 | /// ::= 'catchpad' ParamList 'to' TypeAndValue 'unwind' TypeAndValue |
7401 | bool LLParser::parseCatchPad(Instruction *&Inst, PerFunctionState &PFS) { |
7402 | Value *CatchSwitch = nullptr; |
7403 | |
7404 | if (parseToken(T: lltok::kw_within, ErrMsg: "expected 'within' after catchpad" )) |
7405 | return true; |
7406 | |
7407 | if (Lex.getKind() != lltok::LocalVar && Lex.getKind() != lltok::LocalVarID) |
7408 | return tokError(Msg: "expected scope value for catchpad" ); |
7409 | |
7410 | if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: CatchSwitch, PFS)) |
7411 | return true; |
7412 | |
7413 | SmallVector<Value *, 8> Args; |
7414 | if (parseExceptionArgs(Args, PFS)) |
7415 | return true; |
7416 | |
7417 | Inst = CatchPadInst::Create(CatchSwitch, Args); |
7418 | return false; |
7419 | } |
7420 | |
7421 | /// parseCleanupPad |
7422 | /// ::= 'cleanuppad' within Parent ParamList |
7423 | bool LLParser::parseCleanupPad(Instruction *&Inst, PerFunctionState &PFS) { |
7424 | Value *ParentPad = nullptr; |
7425 | |
7426 | if (parseToken(T: lltok::kw_within, ErrMsg: "expected 'within' after cleanuppad" )) |
7427 | return true; |
7428 | |
7429 | if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar && |
7430 | Lex.getKind() != lltok::LocalVarID) |
7431 | return tokError(Msg: "expected scope value for cleanuppad" ); |
7432 | |
7433 | if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: ParentPad, PFS)) |
7434 | return true; |
7435 | |
7436 | SmallVector<Value *, 8> Args; |
7437 | if (parseExceptionArgs(Args, PFS)) |
7438 | return true; |
7439 | |
7440 | Inst = CleanupPadInst::Create(ParentPad, Args); |
7441 | return false; |
7442 | } |
7443 | |
7444 | //===----------------------------------------------------------------------===// |
7445 | // Unary Operators. |
7446 | //===----------------------------------------------------------------------===// |
7447 | |
7448 | /// parseUnaryOp |
7449 | /// ::= UnaryOp TypeAndValue ',' Value |
7450 | /// |
7451 | /// If IsFP is false, then any integer operand is allowed, if it is true, any fp |
7452 | /// operand is allowed. |
7453 | bool LLParser::parseUnaryOp(Instruction *&Inst, PerFunctionState &PFS, |
7454 | unsigned Opc, bool IsFP) { |
7455 | LocTy Loc; Value *LHS; |
7456 | if (parseTypeAndValue(V&: LHS, Loc, PFS)) |
7457 | return true; |
7458 | |
7459 | bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy() |
7460 | : LHS->getType()->isIntOrIntVectorTy(); |
7461 | |
7462 | if (!Valid) |
7463 | return error(L: Loc, Msg: "invalid operand type for instruction" ); |
7464 | |
7465 | Inst = UnaryOperator::Create(Op: (Instruction::UnaryOps)Opc, S: LHS); |
7466 | return false; |
7467 | } |
7468 | |
7469 | /// parseCallBr |
7470 | /// ::= 'callbr' OptionalCallingConv OptionalAttrs Type Value ParamList |
7471 | /// OptionalAttrs OptionalOperandBundles 'to' TypeAndValue |
7472 | /// '[' LabelList ']' |
7473 | bool LLParser::parseCallBr(Instruction *&Inst, PerFunctionState &PFS) { |
7474 | LocTy CallLoc = Lex.getLoc(); |
7475 | AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext()); |
7476 | std::vector<unsigned> FwdRefAttrGrps; |
7477 | LocTy NoBuiltinLoc; |
7478 | unsigned CC; |
7479 | Type *RetType = nullptr; |
7480 | LocTy RetTypeLoc; |
7481 | ValID CalleeID; |
7482 | SmallVector<ParamInfo, 16> ArgList; |
7483 | SmallVector<OperandBundleDef, 2> BundleList; |
7484 | |
7485 | BasicBlock *DefaultDest; |
7486 | if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(B&: RetAttrs) || |
7487 | parseType(Result&: RetType, Loc&: RetTypeLoc, AllowVoid: true /*void allowed*/) || |
7488 | parseValID(ID&: CalleeID, PFS: &PFS) || parseParameterList(ArgList, PFS) || |
7489 | parseFnAttributeValuePairs(B&: FnAttrs, FwdRefAttrGrps, InAttrGrp: false, |
7490 | BuiltinLoc&: NoBuiltinLoc) || |
7491 | parseOptionalOperandBundles(BundleList, PFS) || |
7492 | parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' in callbr" ) || |
7493 | parseTypeAndBasicBlock(BB&: DefaultDest, PFS) || |
7494 | parseToken(T: lltok::lsquare, ErrMsg: "expected '[' in callbr" )) |
7495 | return true; |
7496 | |
7497 | // parse the destination list. |
7498 | SmallVector<BasicBlock *, 16> IndirectDests; |
7499 | |
7500 | if (Lex.getKind() != lltok::rsquare) { |
7501 | BasicBlock *DestBB; |
7502 | if (parseTypeAndBasicBlock(BB&: DestBB, PFS)) |
7503 | return true; |
7504 | IndirectDests.push_back(Elt: DestBB); |
7505 | |
7506 | while (EatIfPresent(T: lltok::comma)) { |
7507 | if (parseTypeAndBasicBlock(BB&: DestBB, PFS)) |
7508 | return true; |
7509 | IndirectDests.push_back(Elt: DestBB); |
7510 | } |
7511 | } |
7512 | |
7513 | if (parseToken(T: lltok::rsquare, ErrMsg: "expected ']' at end of block list" )) |
7514 | return true; |
7515 | |
7516 | // If RetType is a non-function pointer type, then this is the short syntax |
7517 | // for the call, which means that RetType is just the return type. Infer the |
7518 | // rest of the function argument types from the arguments that are present. |
7519 | FunctionType *Ty; |
7520 | if (resolveFunctionType(RetType, ArgList, FuncTy&: Ty)) |
7521 | return error(L: RetTypeLoc, Msg: "Invalid result type for LLVM function" ); |
7522 | |
7523 | CalleeID.FTy = Ty; |
7524 | |
7525 | // Look up the callee. |
7526 | Value *Callee; |
7527 | if (convertValIDToValue(Ty: PointerType::getUnqual(ElementType: Ty), ID&: CalleeID, V&: Callee, PFS: &PFS)) |
7528 | return true; |
7529 | |
7530 | // Set up the Attribute for the function. |
7531 | SmallVector<Value *, 8> Args; |
7532 | SmallVector<AttributeSet, 8> ArgAttrs; |
7533 | |
7534 | // Loop through FunctionType's arguments and ensure they are specified |
7535 | // correctly. Also, gather any parameter attributes. |
7536 | FunctionType::param_iterator I = Ty->param_begin(); |
7537 | FunctionType::param_iterator E = Ty->param_end(); |
7538 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
7539 | Type *ExpectedTy = nullptr; |
7540 | if (I != E) { |
7541 | ExpectedTy = *I++; |
7542 | } else if (!Ty->isVarArg()) { |
7543 | return error(L: ArgList[i].Loc, Msg: "too many arguments specified" ); |
7544 | } |
7545 | |
7546 | if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) |
7547 | return error(L: ArgList[i].Loc, Msg: "argument is not of expected type '" + |
7548 | getTypeString(T: ExpectedTy) + "'" ); |
7549 | Args.push_back(Elt: ArgList[i].V); |
7550 | ArgAttrs.push_back(Elt: ArgList[i].Attrs); |
7551 | } |
7552 | |
7553 | if (I != E) |
7554 | return error(L: CallLoc, Msg: "not enough parameters specified for call" ); |
7555 | |
7556 | // Finish off the Attribute and check them |
7557 | AttributeList PAL = |
7558 | AttributeList::get(C&: Context, FnAttrs: AttributeSet::get(C&: Context, B: FnAttrs), |
7559 | RetAttrs: AttributeSet::get(C&: Context, B: RetAttrs), ArgAttrs); |
7560 | |
7561 | CallBrInst *CBI = |
7562 | CallBrInst::Create(Ty, Func: Callee, DefaultDest, IndirectDests, Args, |
7563 | Bundles: BundleList); |
7564 | CBI->setCallingConv(CC); |
7565 | CBI->setAttributes(PAL); |
7566 | ForwardRefAttrGroups[CBI] = FwdRefAttrGrps; |
7567 | Inst = CBI; |
7568 | return false; |
7569 | } |
7570 | |
7571 | //===----------------------------------------------------------------------===// |
7572 | // Binary Operators. |
7573 | //===----------------------------------------------------------------------===// |
7574 | |
7575 | /// parseArithmetic |
7576 | /// ::= ArithmeticOps TypeAndValue ',' Value |
7577 | /// |
7578 | /// If IsFP is false, then any integer operand is allowed, if it is true, any fp |
7579 | /// operand is allowed. |
7580 | bool LLParser::parseArithmetic(Instruction *&Inst, PerFunctionState &PFS, |
7581 | unsigned Opc, bool IsFP) { |
7582 | LocTy Loc; Value *LHS, *RHS; |
7583 | if (parseTypeAndValue(V&: LHS, Loc, PFS) || |
7584 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in arithmetic operation" ) || |
7585 | parseValue(Ty: LHS->getType(), V&: RHS, PFS)) |
7586 | return true; |
7587 | |
7588 | bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy() |
7589 | : LHS->getType()->isIntOrIntVectorTy(); |
7590 | |
7591 | if (!Valid) |
7592 | return error(L: Loc, Msg: "invalid operand type for instruction" ); |
7593 | |
7594 | Inst = BinaryOperator::Create(Op: (Instruction::BinaryOps)Opc, S1: LHS, S2: RHS); |
7595 | return false; |
7596 | } |
7597 | |
7598 | /// parseLogical |
7599 | /// ::= ArithmeticOps TypeAndValue ',' Value { |
7600 | bool LLParser::parseLogical(Instruction *&Inst, PerFunctionState &PFS, |
7601 | unsigned Opc) { |
7602 | LocTy Loc; Value *LHS, *RHS; |
7603 | if (parseTypeAndValue(V&: LHS, Loc, PFS) || |
7604 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in logical operation" ) || |
7605 | parseValue(Ty: LHS->getType(), V&: RHS, PFS)) |
7606 | return true; |
7607 | |
7608 | if (!LHS->getType()->isIntOrIntVectorTy()) |
7609 | return error(L: Loc, |
7610 | Msg: "instruction requires integer or integer vector operands" ); |
7611 | |
7612 | Inst = BinaryOperator::Create(Op: (Instruction::BinaryOps)Opc, S1: LHS, S2: RHS); |
7613 | return false; |
7614 | } |
7615 | |
7616 | /// parseCompare |
7617 | /// ::= 'icmp' IPredicates TypeAndValue ',' Value |
7618 | /// ::= 'fcmp' FPredicates TypeAndValue ',' Value |
7619 | bool LLParser::parseCompare(Instruction *&Inst, PerFunctionState &PFS, |
7620 | unsigned Opc) { |
7621 | // parse the integer/fp comparison predicate. |
7622 | LocTy Loc; |
7623 | unsigned Pred; |
7624 | Value *LHS, *RHS; |
7625 | if (parseCmpPredicate(P&: Pred, Opc) || parseTypeAndValue(V&: LHS, Loc, PFS) || |
7626 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after compare value" ) || |
7627 | parseValue(Ty: LHS->getType(), V&: RHS, PFS)) |
7628 | return true; |
7629 | |
7630 | if (Opc == Instruction::FCmp) { |
7631 | if (!LHS->getType()->isFPOrFPVectorTy()) |
7632 | return error(L: Loc, Msg: "fcmp requires floating point operands" ); |
7633 | Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS); |
7634 | } else { |
7635 | assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!" ); |
7636 | if (!LHS->getType()->isIntOrIntVectorTy() && |
7637 | !LHS->getType()->isPtrOrPtrVectorTy()) |
7638 | return error(L: Loc, Msg: "icmp requires integer operands" ); |
7639 | Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS); |
7640 | } |
7641 | return false; |
7642 | } |
7643 | |
7644 | //===----------------------------------------------------------------------===// |
7645 | // Other Instructions. |
7646 | //===----------------------------------------------------------------------===// |
7647 | |
7648 | /// parseCast |
7649 | /// ::= CastOpc TypeAndValue 'to' Type |
7650 | bool LLParser::parseCast(Instruction *&Inst, PerFunctionState &PFS, |
7651 | unsigned Opc) { |
7652 | LocTy Loc; |
7653 | Value *Op; |
7654 | Type *DestTy = nullptr; |
7655 | if (parseTypeAndValue(V&: Op, Loc, PFS) || |
7656 | parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' after cast value" ) || |
7657 | parseType(Result&: DestTy)) |
7658 | return true; |
7659 | |
7660 | if (!CastInst::castIsValid(op: (Instruction::CastOps)Opc, S: Op, DstTy: DestTy)) { |
7661 | CastInst::castIsValid(op: (Instruction::CastOps)Opc, S: Op, DstTy: DestTy); |
7662 | return error(L: Loc, Msg: "invalid cast opcode for cast from '" + |
7663 | getTypeString(T: Op->getType()) + "' to '" + |
7664 | getTypeString(T: DestTy) + "'" ); |
7665 | } |
7666 | Inst = CastInst::Create((Instruction::CastOps)Opc, S: Op, Ty: DestTy); |
7667 | return false; |
7668 | } |
7669 | |
7670 | /// parseSelect |
7671 | /// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
7672 | bool LLParser::parseSelect(Instruction *&Inst, PerFunctionState &PFS) { |
7673 | LocTy Loc; |
7674 | Value *Op0, *Op1, *Op2; |
7675 | if (parseTypeAndValue(V&: Op0, Loc, PFS) || |
7676 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after select condition" ) || |
7677 | parseTypeAndValue(V&: Op1, PFS) || |
7678 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after select value" ) || |
7679 | parseTypeAndValue(V&: Op2, PFS)) |
7680 | return true; |
7681 | |
7682 | if (const char *Reason = SelectInst::areInvalidOperands(Cond: Op0, True: Op1, False: Op2)) |
7683 | return error(L: Loc, Msg: Reason); |
7684 | |
7685 | Inst = SelectInst::Create(C: Op0, S1: Op1, S2: Op2); |
7686 | return false; |
7687 | } |
7688 | |
7689 | /// parseVAArg |
7690 | /// ::= 'va_arg' TypeAndValue ',' Type |
7691 | bool LLParser::parseVAArg(Instruction *&Inst, PerFunctionState &PFS) { |
7692 | Value *Op; |
7693 | Type *EltTy = nullptr; |
7694 | LocTy TypeLoc; |
7695 | if (parseTypeAndValue(V&: Op, PFS) || |
7696 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after vaarg operand" ) || |
7697 | parseType(Result&: EltTy, Loc&: TypeLoc)) |
7698 | return true; |
7699 | |
7700 | if (!EltTy->isFirstClassType()) |
7701 | return error(L: TypeLoc, Msg: "va_arg requires operand with first class type" ); |
7702 | |
7703 | Inst = new VAArgInst(Op, EltTy); |
7704 | return false; |
7705 | } |
7706 | |
7707 | /// parseExtractElement |
7708 | /// ::= 'extractelement' TypeAndValue ',' TypeAndValue |
7709 | bool LLParser::(Instruction *&Inst, PerFunctionState &PFS) { |
7710 | LocTy Loc; |
7711 | Value *Op0, *Op1; |
7712 | if (parseTypeAndValue(V&: Op0, Loc, PFS) || |
7713 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after extract value" ) || |
7714 | parseTypeAndValue(V&: Op1, PFS)) |
7715 | return true; |
7716 | |
7717 | if (!ExtractElementInst::isValidOperands(Vec: Op0, Idx: Op1)) |
7718 | return error(L: Loc, Msg: "invalid extractelement operands" ); |
7719 | |
7720 | Inst = ExtractElementInst::Create(Vec: Op0, Idx: Op1); |
7721 | return false; |
7722 | } |
7723 | |
7724 | /// parseInsertElement |
7725 | /// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
7726 | bool LLParser::parseInsertElement(Instruction *&Inst, PerFunctionState &PFS) { |
7727 | LocTy Loc; |
7728 | Value *Op0, *Op1, *Op2; |
7729 | if (parseTypeAndValue(V&: Op0, Loc, PFS) || |
7730 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after insertelement value" ) || |
7731 | parseTypeAndValue(V&: Op1, PFS) || |
7732 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after insertelement value" ) || |
7733 | parseTypeAndValue(V&: Op2, PFS)) |
7734 | return true; |
7735 | |
7736 | if (!InsertElementInst::isValidOperands(Vec: Op0, NewElt: Op1, Idx: Op2)) |
7737 | return error(L: Loc, Msg: "invalid insertelement operands" ); |
7738 | |
7739 | Inst = InsertElementInst::Create(Vec: Op0, NewElt: Op1, Idx: Op2); |
7740 | return false; |
7741 | } |
7742 | |
7743 | /// parseShuffleVector |
7744 | /// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
7745 | bool LLParser::parseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) { |
7746 | LocTy Loc; |
7747 | Value *Op0, *Op1, *Op2; |
7748 | if (parseTypeAndValue(V&: Op0, Loc, PFS) || |
7749 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after shuffle mask" ) || |
7750 | parseTypeAndValue(V&: Op1, PFS) || |
7751 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after shuffle value" ) || |
7752 | parseTypeAndValue(V&: Op2, PFS)) |
7753 | return true; |
7754 | |
7755 | if (!ShuffleVectorInst::isValidOperands(V1: Op0, V2: Op1, Mask: Op2)) |
7756 | return error(L: Loc, Msg: "invalid shufflevector operands" ); |
7757 | |
7758 | Inst = new ShuffleVectorInst(Op0, Op1, Op2); |
7759 | return false; |
7760 | } |
7761 | |
7762 | /// parsePHI |
7763 | /// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')* |
7764 | int LLParser::parsePHI(Instruction *&Inst, PerFunctionState &PFS) { |
7765 | Type *Ty = nullptr; LocTy TypeLoc; |
7766 | Value *Op0, *Op1; |
7767 | |
7768 | if (parseType(Result&: Ty, Loc&: TypeLoc)) |
7769 | return true; |
7770 | |
7771 | if (!Ty->isFirstClassType()) |
7772 | return error(L: TypeLoc, Msg: "phi node must have first class type" ); |
7773 | |
7774 | bool First = true; |
7775 | bool = false; |
7776 | SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals; |
7777 | |
7778 | while (true) { |
7779 | if (First) { |
7780 | if (Lex.getKind() != lltok::lsquare) |
7781 | break; |
7782 | First = false; |
7783 | } else if (!EatIfPresent(T: lltok::comma)) |
7784 | break; |
7785 | |
7786 | if (Lex.getKind() == lltok::MetadataVar) { |
7787 | AteExtraComma = true; |
7788 | break; |
7789 | } |
7790 | |
7791 | if (parseToken(T: lltok::lsquare, ErrMsg: "expected '[' in phi value list" ) || |
7792 | parseValue(Ty, V&: Op0, PFS) || |
7793 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after insertelement value" ) || |
7794 | parseValue(Ty: Type::getLabelTy(C&: Context), V&: Op1, PFS) || |
7795 | parseToken(T: lltok::rsquare, ErrMsg: "expected ']' in phi value list" )) |
7796 | return true; |
7797 | |
7798 | PHIVals.push_back(Elt: std::make_pair(x&: Op0, y: cast<BasicBlock>(Val: Op1))); |
7799 | } |
7800 | |
7801 | PHINode *PN = PHINode::Create(Ty, NumReservedValues: PHIVals.size()); |
7802 | for (unsigned i = 0, e = PHIVals.size(); i != e; ++i) |
7803 | PN->addIncoming(V: PHIVals[i].first, BB: PHIVals[i].second); |
7804 | Inst = PN; |
7805 | return AteExtraComma ? InstExtraComma : InstNormal; |
7806 | } |
7807 | |
7808 | /// parseLandingPad |
7809 | /// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+ |
7810 | /// Clause |
7811 | /// ::= 'catch' TypeAndValue |
7812 | /// ::= 'filter' |
7813 | /// ::= 'filter' TypeAndValue ( ',' TypeAndValue )* |
7814 | bool LLParser::parseLandingPad(Instruction *&Inst, PerFunctionState &PFS) { |
7815 | Type *Ty = nullptr; LocTy TyLoc; |
7816 | |
7817 | if (parseType(Result&: Ty, Loc&: TyLoc)) |
7818 | return true; |
7819 | |
7820 | std::unique_ptr<LandingPadInst> LP(LandingPadInst::Create(RetTy: Ty, NumReservedClauses: 0)); |
7821 | LP->setCleanup(EatIfPresent(T: lltok::kw_cleanup)); |
7822 | |
7823 | while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){ |
7824 | LandingPadInst::ClauseType CT; |
7825 | if (EatIfPresent(T: lltok::kw_catch)) |
7826 | CT = LandingPadInst::Catch; |
7827 | else if (EatIfPresent(T: lltok::kw_filter)) |
7828 | CT = LandingPadInst::Filter; |
7829 | else |
7830 | return tokError(Msg: "expected 'catch' or 'filter' clause type" ); |
7831 | |
7832 | Value *V; |
7833 | LocTy VLoc; |
7834 | if (parseTypeAndValue(V, Loc&: VLoc, PFS)) |
7835 | return true; |
7836 | |
7837 | // A 'catch' type expects a non-array constant. A filter clause expects an |
7838 | // array constant. |
7839 | if (CT == LandingPadInst::Catch) { |
7840 | if (isa<ArrayType>(Val: V->getType())) |
7841 | error(L: VLoc, Msg: "'catch' clause has an invalid type" ); |
7842 | } else { |
7843 | if (!isa<ArrayType>(Val: V->getType())) |
7844 | error(L: VLoc, Msg: "'filter' clause has an invalid type" ); |
7845 | } |
7846 | |
7847 | Constant *CV = dyn_cast<Constant>(Val: V); |
7848 | if (!CV) |
7849 | return error(L: VLoc, Msg: "clause argument must be a constant" ); |
7850 | LP->addClause(ClauseVal: CV); |
7851 | } |
7852 | |
7853 | Inst = LP.release(); |
7854 | return false; |
7855 | } |
7856 | |
7857 | /// parseFreeze |
7858 | /// ::= 'freeze' Type Value |
7859 | bool LLParser::parseFreeze(Instruction *&Inst, PerFunctionState &PFS) { |
7860 | LocTy Loc; |
7861 | Value *Op; |
7862 | if (parseTypeAndValue(V&: Op, Loc, PFS)) |
7863 | return true; |
7864 | |
7865 | Inst = new FreezeInst(Op); |
7866 | return false; |
7867 | } |
7868 | |
7869 | /// parseCall |
7870 | /// ::= 'call' OptionalFastMathFlags OptionalCallingConv |
7871 | /// OptionalAttrs Type Value ParameterList OptionalAttrs |
7872 | /// ::= 'tail' 'call' OptionalFastMathFlags OptionalCallingConv |
7873 | /// OptionalAttrs Type Value ParameterList OptionalAttrs |
7874 | /// ::= 'musttail' 'call' OptionalFastMathFlags OptionalCallingConv |
7875 | /// OptionalAttrs Type Value ParameterList OptionalAttrs |
7876 | /// ::= 'notail' 'call' OptionalFastMathFlags OptionalCallingConv |
7877 | /// OptionalAttrs Type Value ParameterList OptionalAttrs |
7878 | bool LLParser::parseCall(Instruction *&Inst, PerFunctionState &PFS, |
7879 | CallInst::TailCallKind TCK) { |
7880 | AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext()); |
7881 | std::vector<unsigned> FwdRefAttrGrps; |
7882 | LocTy BuiltinLoc; |
7883 | unsigned CallAddrSpace; |
7884 | unsigned CC; |
7885 | Type *RetType = nullptr; |
7886 | LocTy RetTypeLoc; |
7887 | ValID CalleeID; |
7888 | SmallVector<ParamInfo, 16> ArgList; |
7889 | SmallVector<OperandBundleDef, 2> BundleList; |
7890 | LocTy CallLoc = Lex.getLoc(); |
7891 | |
7892 | if (TCK != CallInst::TCK_None && |
7893 | parseToken(T: lltok::kw_call, |
7894 | ErrMsg: "expected 'tail call', 'musttail call', or 'notail call'" )) |
7895 | return true; |
7896 | |
7897 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
7898 | |
7899 | if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(B&: RetAttrs) || |
7900 | parseOptionalProgramAddrSpace(AddrSpace&: CallAddrSpace) || |
7901 | parseType(Result&: RetType, Loc&: RetTypeLoc, AllowVoid: true /*void allowed*/) || |
7902 | parseValID(ID&: CalleeID, PFS: &PFS) || |
7903 | parseParameterList(ArgList, PFS, IsMustTailCall: TCK == CallInst::TCK_MustTail, |
7904 | InVarArgsFunc: PFS.getFunction().isVarArg()) || |
7905 | parseFnAttributeValuePairs(B&: FnAttrs, FwdRefAttrGrps, InAttrGrp: false, BuiltinLoc) || |
7906 | parseOptionalOperandBundles(BundleList, PFS)) |
7907 | return true; |
7908 | |
7909 | // If RetType is a non-function pointer type, then this is the short syntax |
7910 | // for the call, which means that RetType is just the return type. Infer the |
7911 | // rest of the function argument types from the arguments that are present. |
7912 | FunctionType *Ty; |
7913 | if (resolveFunctionType(RetType, ArgList, FuncTy&: Ty)) |
7914 | return error(L: RetTypeLoc, Msg: "Invalid result type for LLVM function" ); |
7915 | |
7916 | CalleeID.FTy = Ty; |
7917 | |
7918 | // Look up the callee. |
7919 | Value *Callee; |
7920 | if (convertValIDToValue(Ty: PointerType::get(ElementType: Ty, AddressSpace: CallAddrSpace), ID&: CalleeID, V&: Callee, |
7921 | PFS: &PFS)) |
7922 | return true; |
7923 | |
7924 | // Set up the Attribute for the function. |
7925 | SmallVector<AttributeSet, 8> Attrs; |
7926 | |
7927 | SmallVector<Value*, 8> Args; |
7928 | |
7929 | // Loop through FunctionType's arguments and ensure they are specified |
7930 | // correctly. Also, gather any parameter attributes. |
7931 | FunctionType::param_iterator I = Ty->param_begin(); |
7932 | FunctionType::param_iterator E = Ty->param_end(); |
7933 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) { |
7934 | Type *ExpectedTy = nullptr; |
7935 | if (I != E) { |
7936 | ExpectedTy = *I++; |
7937 | } else if (!Ty->isVarArg()) { |
7938 | return error(L: ArgList[i].Loc, Msg: "too many arguments specified" ); |
7939 | } |
7940 | |
7941 | if (ExpectedTy && ExpectedTy != ArgList[i].V->getType()) |
7942 | return error(L: ArgList[i].Loc, Msg: "argument is not of expected type '" + |
7943 | getTypeString(T: ExpectedTy) + "'" ); |
7944 | Args.push_back(Elt: ArgList[i].V); |
7945 | Attrs.push_back(Elt: ArgList[i].Attrs); |
7946 | } |
7947 | |
7948 | if (I != E) |
7949 | return error(L: CallLoc, Msg: "not enough parameters specified for call" ); |
7950 | |
7951 | // Finish off the Attribute and check them |
7952 | AttributeList PAL = |
7953 | AttributeList::get(C&: Context, FnAttrs: AttributeSet::get(C&: Context, B: FnAttrs), |
7954 | RetAttrs: AttributeSet::get(C&: Context, B: RetAttrs), ArgAttrs: Attrs); |
7955 | |
7956 | CallInst *CI = CallInst::Create(Ty, Func: Callee, Args, Bundles: BundleList); |
7957 | CI->setTailCallKind(TCK); |
7958 | CI->setCallingConv(CC); |
7959 | if (FMF.any()) { |
7960 | if (!isa<FPMathOperator>(Val: CI)) { |
7961 | CI->deleteValue(); |
7962 | return error(L: CallLoc, Msg: "fast-math-flags specified for call without " |
7963 | "floating-point scalar or vector return type" ); |
7964 | } |
7965 | CI->setFastMathFlags(FMF); |
7966 | } |
7967 | |
7968 | if (CalleeID.Kind == ValID::t_GlobalName && |
7969 | isOldDbgFormatIntrinsic(Name: CalleeID.StrVal)) { |
7970 | if (SeenNewDbgInfoFormat) { |
7971 | CI->deleteValue(); |
7972 | return error(L: CallLoc, Msg: "llvm.dbg intrinsic should not appear in a module " |
7973 | "using non-intrinsic debug info" ); |
7974 | } |
7975 | if (!SeenOldDbgInfoFormat) |
7976 | M->setNewDbgInfoFormatFlag(false); |
7977 | SeenOldDbgInfoFormat = true; |
7978 | } |
7979 | CI->setAttributes(PAL); |
7980 | ForwardRefAttrGroups[CI] = FwdRefAttrGrps; |
7981 | Inst = CI; |
7982 | return false; |
7983 | } |
7984 | |
7985 | //===----------------------------------------------------------------------===// |
7986 | // Memory Instructions. |
7987 | //===----------------------------------------------------------------------===// |
7988 | |
7989 | /// parseAlloc |
7990 | /// ::= 'alloca' 'inalloca'? 'swifterror'? Type (',' TypeAndValue)? |
7991 | /// (',' 'align' i32)? (',', 'addrspace(n))? |
7992 | int LLParser::parseAlloc(Instruction *&Inst, PerFunctionState &PFS) { |
7993 | Value *Size = nullptr; |
7994 | LocTy SizeLoc, TyLoc, ASLoc; |
7995 | MaybeAlign Alignment; |
7996 | unsigned AddrSpace = 0; |
7997 | Type *Ty = nullptr; |
7998 | |
7999 | bool IsInAlloca = EatIfPresent(T: lltok::kw_inalloca); |
8000 | bool IsSwiftError = EatIfPresent(T: lltok::kw_swifterror); |
8001 | |
8002 | if (parseType(Result&: Ty, Loc&: TyLoc)) |
8003 | return true; |
8004 | |
8005 | if (Ty->isFunctionTy() || !PointerType::isValidElementType(ElemTy: Ty)) |
8006 | return error(L: TyLoc, Msg: "invalid type for alloca" ); |
8007 | |
8008 | bool = false; |
8009 | if (EatIfPresent(T: lltok::comma)) { |
8010 | if (Lex.getKind() == lltok::kw_align) { |
8011 | if (parseOptionalAlignment(Alignment)) |
8012 | return true; |
8013 | if (parseOptionalCommaAddrSpace(AddrSpace, Loc&: ASLoc, AteExtraComma)) |
8014 | return true; |
8015 | } else if (Lex.getKind() == lltok::kw_addrspace) { |
8016 | ASLoc = Lex.getLoc(); |
8017 | if (parseOptionalAddrSpace(AddrSpace)) |
8018 | return true; |
8019 | } else if (Lex.getKind() == lltok::MetadataVar) { |
8020 | AteExtraComma = true; |
8021 | } else { |
8022 | if (parseTypeAndValue(V&: Size, Loc&: SizeLoc, PFS)) |
8023 | return true; |
8024 | if (EatIfPresent(T: lltok::comma)) { |
8025 | if (Lex.getKind() == lltok::kw_align) { |
8026 | if (parseOptionalAlignment(Alignment)) |
8027 | return true; |
8028 | if (parseOptionalCommaAddrSpace(AddrSpace, Loc&: ASLoc, AteExtraComma)) |
8029 | return true; |
8030 | } else if (Lex.getKind() == lltok::kw_addrspace) { |
8031 | ASLoc = Lex.getLoc(); |
8032 | if (parseOptionalAddrSpace(AddrSpace)) |
8033 | return true; |
8034 | } else if (Lex.getKind() == lltok::MetadataVar) { |
8035 | AteExtraComma = true; |
8036 | } |
8037 | } |
8038 | } |
8039 | } |
8040 | |
8041 | if (Size && !Size->getType()->isIntegerTy()) |
8042 | return error(L: SizeLoc, Msg: "element count must have integer type" ); |
8043 | |
8044 | SmallPtrSet<Type *, 4> Visited; |
8045 | if (!Alignment && !Ty->isSized(Visited: &Visited)) |
8046 | return error(L: TyLoc, Msg: "Cannot allocate unsized type" ); |
8047 | if (!Alignment) |
8048 | Alignment = M->getDataLayout().getPrefTypeAlign(Ty); |
8049 | AllocaInst *AI = new AllocaInst(Ty, AddrSpace, Size, *Alignment); |
8050 | AI->setUsedWithInAlloca(IsInAlloca); |
8051 | AI->setSwiftError(IsSwiftError); |
8052 | Inst = AI; |
8053 | return AteExtraComma ? InstExtraComma : InstNormal; |
8054 | } |
8055 | |
8056 | /// parseLoad |
8057 | /// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)? |
8058 | /// ::= 'load' 'atomic' 'volatile'? TypeAndValue |
8059 | /// 'singlethread'? AtomicOrdering (',' 'align' i32)? |
8060 | int LLParser::parseLoad(Instruction *&Inst, PerFunctionState &PFS) { |
8061 | Value *Val; LocTy Loc; |
8062 | MaybeAlign Alignment; |
8063 | bool = false; |
8064 | bool isAtomic = false; |
8065 | AtomicOrdering Ordering = AtomicOrdering::NotAtomic; |
8066 | SyncScope::ID SSID = SyncScope::System; |
8067 | |
8068 | if (Lex.getKind() == lltok::kw_atomic) { |
8069 | isAtomic = true; |
8070 | Lex.Lex(); |
8071 | } |
8072 | |
8073 | bool isVolatile = false; |
8074 | if (Lex.getKind() == lltok::kw_volatile) { |
8075 | isVolatile = true; |
8076 | Lex.Lex(); |
8077 | } |
8078 | |
8079 | Type *Ty; |
8080 | LocTy ExplicitTypeLoc = Lex.getLoc(); |
8081 | if (parseType(Result&: Ty) || |
8082 | parseToken(T: lltok::comma, ErrMsg: "expected comma after load's type" ) || |
8083 | parseTypeAndValue(V&: Val, Loc, PFS) || |
8084 | parseScopeAndOrdering(IsAtomic: isAtomic, SSID, Ordering) || |
8085 | parseOptionalCommaAlign(Alignment, AteExtraComma)) |
8086 | return true; |
8087 | |
8088 | if (!Val->getType()->isPointerTy() || !Ty->isFirstClassType()) |
8089 | return error(L: Loc, Msg: "load operand must be a pointer to a first class type" ); |
8090 | if (isAtomic && !Alignment) |
8091 | return error(L: Loc, Msg: "atomic load must have explicit non-zero alignment" ); |
8092 | if (Ordering == AtomicOrdering::Release || |
8093 | Ordering == AtomicOrdering::AcquireRelease) |
8094 | return error(L: Loc, Msg: "atomic load cannot use Release ordering" ); |
8095 | |
8096 | SmallPtrSet<Type *, 4> Visited; |
8097 | if (!Alignment && !Ty->isSized(Visited: &Visited)) |
8098 | return error(L: ExplicitTypeLoc, Msg: "loading unsized types is not allowed" ); |
8099 | if (!Alignment) |
8100 | Alignment = M->getDataLayout().getABITypeAlign(Ty); |
8101 | Inst = new LoadInst(Ty, Val, "" , isVolatile, *Alignment, Ordering, SSID); |
8102 | return AteExtraComma ? InstExtraComma : InstNormal; |
8103 | } |
8104 | |
8105 | /// parseStore |
8106 | |
8107 | /// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)? |
8108 | /// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue |
8109 | /// 'singlethread'? AtomicOrdering (',' 'align' i32)? |
8110 | int LLParser::parseStore(Instruction *&Inst, PerFunctionState &PFS) { |
8111 | Value *Val, *Ptr; LocTy Loc, PtrLoc; |
8112 | MaybeAlign Alignment; |
8113 | bool = false; |
8114 | bool isAtomic = false; |
8115 | AtomicOrdering Ordering = AtomicOrdering::NotAtomic; |
8116 | SyncScope::ID SSID = SyncScope::System; |
8117 | |
8118 | if (Lex.getKind() == lltok::kw_atomic) { |
8119 | isAtomic = true; |
8120 | Lex.Lex(); |
8121 | } |
8122 | |
8123 | bool isVolatile = false; |
8124 | if (Lex.getKind() == lltok::kw_volatile) { |
8125 | isVolatile = true; |
8126 | Lex.Lex(); |
8127 | } |
8128 | |
8129 | if (parseTypeAndValue(V&: Val, Loc, PFS) || |
8130 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after store operand" ) || |
8131 | parseTypeAndValue(V&: Ptr, Loc&: PtrLoc, PFS) || |
8132 | parseScopeAndOrdering(IsAtomic: isAtomic, SSID, Ordering) || |
8133 | parseOptionalCommaAlign(Alignment, AteExtraComma)) |
8134 | return true; |
8135 | |
8136 | if (!Ptr->getType()->isPointerTy()) |
8137 | return error(L: PtrLoc, Msg: "store operand must be a pointer" ); |
8138 | if (!Val->getType()->isFirstClassType()) |
8139 | return error(L: Loc, Msg: "store operand must be a first class value" ); |
8140 | if (isAtomic && !Alignment) |
8141 | return error(L: Loc, Msg: "atomic store must have explicit non-zero alignment" ); |
8142 | if (Ordering == AtomicOrdering::Acquire || |
8143 | Ordering == AtomicOrdering::AcquireRelease) |
8144 | return error(L: Loc, Msg: "atomic store cannot use Acquire ordering" ); |
8145 | SmallPtrSet<Type *, 4> Visited; |
8146 | if (!Alignment && !Val->getType()->isSized(Visited: &Visited)) |
8147 | return error(L: Loc, Msg: "storing unsized types is not allowed" ); |
8148 | if (!Alignment) |
8149 | Alignment = M->getDataLayout().getABITypeAlign(Ty: Val->getType()); |
8150 | |
8151 | Inst = new StoreInst(Val, Ptr, isVolatile, *Alignment, Ordering, SSID); |
8152 | return AteExtraComma ? InstExtraComma : InstNormal; |
8153 | } |
8154 | |
8155 | /// parseCmpXchg |
8156 | /// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ',' |
8157 | /// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering ',' |
8158 | /// 'Align'? |
8159 | int LLParser::parseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) { |
8160 | Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc; |
8161 | bool = false; |
8162 | AtomicOrdering SuccessOrdering = AtomicOrdering::NotAtomic; |
8163 | AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic; |
8164 | SyncScope::ID SSID = SyncScope::System; |
8165 | bool isVolatile = false; |
8166 | bool isWeak = false; |
8167 | MaybeAlign Alignment; |
8168 | |
8169 | if (EatIfPresent(T: lltok::kw_weak)) |
8170 | isWeak = true; |
8171 | |
8172 | if (EatIfPresent(T: lltok::kw_volatile)) |
8173 | isVolatile = true; |
8174 | |
8175 | if (parseTypeAndValue(V&: Ptr, Loc&: PtrLoc, PFS) || |
8176 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after cmpxchg address" ) || |
8177 | parseTypeAndValue(V&: Cmp, Loc&: CmpLoc, PFS) || |
8178 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after cmpxchg cmp operand" ) || |
8179 | parseTypeAndValue(V&: New, Loc&: NewLoc, PFS) || |
8180 | parseScopeAndOrdering(IsAtomic: true /*Always atomic*/, SSID, Ordering&: SuccessOrdering) || |
8181 | parseOrdering(Ordering&: FailureOrdering) || |
8182 | parseOptionalCommaAlign(Alignment, AteExtraComma)) |
8183 | return true; |
8184 | |
8185 | if (!AtomicCmpXchgInst::isValidSuccessOrdering(Ordering: SuccessOrdering)) |
8186 | return tokError(Msg: "invalid cmpxchg success ordering" ); |
8187 | if (!AtomicCmpXchgInst::isValidFailureOrdering(Ordering: FailureOrdering)) |
8188 | return tokError(Msg: "invalid cmpxchg failure ordering" ); |
8189 | if (!Ptr->getType()->isPointerTy()) |
8190 | return error(L: PtrLoc, Msg: "cmpxchg operand must be a pointer" ); |
8191 | if (Cmp->getType() != New->getType()) |
8192 | return error(L: NewLoc, Msg: "compare value and new value type do not match" ); |
8193 | if (!New->getType()->isFirstClassType()) |
8194 | return error(L: NewLoc, Msg: "cmpxchg operand must be a first class value" ); |
8195 | |
8196 | const Align DefaultAlignment( |
8197 | PFS.getFunction().getParent()->getDataLayout().getTypeStoreSize( |
8198 | Ty: Cmp->getType())); |
8199 | |
8200 | AtomicCmpXchgInst *CXI = |
8201 | new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment.value_or(u: DefaultAlignment), |
8202 | SuccessOrdering, FailureOrdering, SSID); |
8203 | CXI->setVolatile(isVolatile); |
8204 | CXI->setWeak(isWeak); |
8205 | |
8206 | Inst = CXI; |
8207 | return AteExtraComma ? InstExtraComma : InstNormal; |
8208 | } |
8209 | |
8210 | /// parseAtomicRMW |
8211 | /// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue |
8212 | /// 'singlethread'? AtomicOrdering |
8213 | int LLParser::parseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) { |
8214 | Value *Ptr, *Val; LocTy PtrLoc, ValLoc; |
8215 | bool = false; |
8216 | AtomicOrdering Ordering = AtomicOrdering::NotAtomic; |
8217 | SyncScope::ID SSID = SyncScope::System; |
8218 | bool isVolatile = false; |
8219 | bool IsFP = false; |
8220 | AtomicRMWInst::BinOp Operation; |
8221 | MaybeAlign Alignment; |
8222 | |
8223 | if (EatIfPresent(T: lltok::kw_volatile)) |
8224 | isVolatile = true; |
8225 | |
8226 | switch (Lex.getKind()) { |
8227 | default: |
8228 | return tokError(Msg: "expected binary operation in atomicrmw" ); |
8229 | case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break; |
8230 | case lltok::kw_add: Operation = AtomicRMWInst::Add; break; |
8231 | case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break; |
8232 | case lltok::kw_and: Operation = AtomicRMWInst::And; break; |
8233 | case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break; |
8234 | case lltok::kw_or: Operation = AtomicRMWInst::Or; break; |
8235 | case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break; |
8236 | case lltok::kw_max: Operation = AtomicRMWInst::Max; break; |
8237 | case lltok::kw_min: Operation = AtomicRMWInst::Min; break; |
8238 | case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break; |
8239 | case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break; |
8240 | case lltok::kw_uinc_wrap: |
8241 | Operation = AtomicRMWInst::UIncWrap; |
8242 | break; |
8243 | case lltok::kw_udec_wrap: |
8244 | Operation = AtomicRMWInst::UDecWrap; |
8245 | break; |
8246 | case lltok::kw_fadd: |
8247 | Operation = AtomicRMWInst::FAdd; |
8248 | IsFP = true; |
8249 | break; |
8250 | case lltok::kw_fsub: |
8251 | Operation = AtomicRMWInst::FSub; |
8252 | IsFP = true; |
8253 | break; |
8254 | case lltok::kw_fmax: |
8255 | Operation = AtomicRMWInst::FMax; |
8256 | IsFP = true; |
8257 | break; |
8258 | case lltok::kw_fmin: |
8259 | Operation = AtomicRMWInst::FMin; |
8260 | IsFP = true; |
8261 | break; |
8262 | } |
8263 | Lex.Lex(); // Eat the operation. |
8264 | |
8265 | if (parseTypeAndValue(V&: Ptr, Loc&: PtrLoc, PFS) || |
8266 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after atomicrmw address" ) || |
8267 | parseTypeAndValue(V&: Val, Loc&: ValLoc, PFS) || |
8268 | parseScopeAndOrdering(IsAtomic: true /*Always atomic*/, SSID, Ordering) || |
8269 | parseOptionalCommaAlign(Alignment, AteExtraComma)) |
8270 | return true; |
8271 | |
8272 | if (Ordering == AtomicOrdering::Unordered) |
8273 | return tokError(Msg: "atomicrmw cannot be unordered" ); |
8274 | if (!Ptr->getType()->isPointerTy()) |
8275 | return error(L: PtrLoc, Msg: "atomicrmw operand must be a pointer" ); |
8276 | if (Val->getType()->isScalableTy()) |
8277 | return error(L: ValLoc, Msg: "atomicrmw operand may not be scalable" ); |
8278 | |
8279 | if (Operation == AtomicRMWInst::Xchg) { |
8280 | if (!Val->getType()->isIntegerTy() && |
8281 | !Val->getType()->isFloatingPointTy() && |
8282 | !Val->getType()->isPointerTy()) { |
8283 | return error( |
8284 | L: ValLoc, |
8285 | Msg: "atomicrmw " + AtomicRMWInst::getOperationName(Op: Operation) + |
8286 | " operand must be an integer, floating point, or pointer type" ); |
8287 | } |
8288 | } else if (IsFP) { |
8289 | if (!Val->getType()->isFPOrFPVectorTy()) { |
8290 | return error(L: ValLoc, Msg: "atomicrmw " + |
8291 | AtomicRMWInst::getOperationName(Op: Operation) + |
8292 | " operand must be a floating point type" ); |
8293 | } |
8294 | } else { |
8295 | if (!Val->getType()->isIntegerTy()) { |
8296 | return error(L: ValLoc, Msg: "atomicrmw " + |
8297 | AtomicRMWInst::getOperationName(Op: Operation) + |
8298 | " operand must be an integer" ); |
8299 | } |
8300 | } |
8301 | |
8302 | unsigned Size = |
8303 | PFS.getFunction().getParent()->getDataLayout().getTypeStoreSizeInBits( |
8304 | Ty: Val->getType()); |
8305 | if (Size < 8 || (Size & (Size - 1))) |
8306 | return error(L: ValLoc, Msg: "atomicrmw operand must be power-of-two byte-sized" |
8307 | " integer" ); |
8308 | const Align DefaultAlignment( |
8309 | PFS.getFunction().getParent()->getDataLayout().getTypeStoreSize( |
8310 | Ty: Val->getType())); |
8311 | AtomicRMWInst *RMWI = |
8312 | new AtomicRMWInst(Operation, Ptr, Val, |
8313 | Alignment.value_or(u: DefaultAlignment), Ordering, SSID); |
8314 | RMWI->setVolatile(isVolatile); |
8315 | Inst = RMWI; |
8316 | return AteExtraComma ? InstExtraComma : InstNormal; |
8317 | } |
8318 | |
8319 | /// parseFence |
8320 | /// ::= 'fence' 'singlethread'? AtomicOrdering |
8321 | int LLParser::parseFence(Instruction *&Inst, PerFunctionState &PFS) { |
8322 | AtomicOrdering Ordering = AtomicOrdering::NotAtomic; |
8323 | SyncScope::ID SSID = SyncScope::System; |
8324 | if (parseScopeAndOrdering(IsAtomic: true /*Always atomic*/, SSID, Ordering)) |
8325 | return true; |
8326 | |
8327 | if (Ordering == AtomicOrdering::Unordered) |
8328 | return tokError(Msg: "fence cannot be unordered" ); |
8329 | if (Ordering == AtomicOrdering::Monotonic) |
8330 | return tokError(Msg: "fence cannot be monotonic" ); |
8331 | |
8332 | Inst = new FenceInst(Context, Ordering, SSID); |
8333 | return InstNormal; |
8334 | } |
8335 | |
8336 | /// parseGetElementPtr |
8337 | /// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)* |
8338 | int LLParser::parseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) { |
8339 | Value *Ptr = nullptr; |
8340 | Value *Val = nullptr; |
8341 | LocTy Loc, EltLoc; |
8342 | |
8343 | bool InBounds = EatIfPresent(T: lltok::kw_inbounds); |
8344 | |
8345 | Type *Ty = nullptr; |
8346 | if (parseType(Result&: Ty) || |
8347 | parseToken(T: lltok::comma, ErrMsg: "expected comma after getelementptr's type" ) || |
8348 | parseTypeAndValue(V&: Ptr, Loc, PFS)) |
8349 | return true; |
8350 | |
8351 | Type *BaseType = Ptr->getType(); |
8352 | PointerType *BasePointerType = dyn_cast<PointerType>(Val: BaseType->getScalarType()); |
8353 | if (!BasePointerType) |
8354 | return error(L: Loc, Msg: "base of getelementptr must be a pointer" ); |
8355 | |
8356 | SmallVector<Value*, 16> Indices; |
8357 | bool = false; |
8358 | // GEP returns a vector of pointers if at least one of parameters is a vector. |
8359 | // All vector parameters should have the same vector width. |
8360 | ElementCount GEPWidth = BaseType->isVectorTy() |
8361 | ? cast<VectorType>(Val: BaseType)->getElementCount() |
8362 | : ElementCount::getFixed(MinVal: 0); |
8363 | |
8364 | while (EatIfPresent(T: lltok::comma)) { |
8365 | if (Lex.getKind() == lltok::MetadataVar) { |
8366 | AteExtraComma = true; |
8367 | break; |
8368 | } |
8369 | if (parseTypeAndValue(V&: Val, Loc&: EltLoc, PFS)) |
8370 | return true; |
8371 | if (!Val->getType()->isIntOrIntVectorTy()) |
8372 | return error(L: EltLoc, Msg: "getelementptr index must be an integer" ); |
8373 | |
8374 | if (auto *ValVTy = dyn_cast<VectorType>(Val: Val->getType())) { |
8375 | ElementCount ValNumEl = ValVTy->getElementCount(); |
8376 | if (GEPWidth != ElementCount::getFixed(MinVal: 0) && GEPWidth != ValNumEl) |
8377 | return error( |
8378 | L: EltLoc, |
8379 | Msg: "getelementptr vector index has a wrong number of elements" ); |
8380 | GEPWidth = ValNumEl; |
8381 | } |
8382 | Indices.push_back(Elt: Val); |
8383 | } |
8384 | |
8385 | SmallPtrSet<Type*, 4> Visited; |
8386 | if (!Indices.empty() && !Ty->isSized(Visited: &Visited)) |
8387 | return error(L: Loc, Msg: "base element of getelementptr must be sized" ); |
8388 | |
8389 | auto *STy = dyn_cast<StructType>(Val: Ty); |
8390 | if (STy && STy->containsScalableVectorType()) |
8391 | return error(L: Loc, Msg: "getelementptr cannot target structure that contains " |
8392 | "scalable vector type" ); |
8393 | |
8394 | if (!GetElementPtrInst::getIndexedType(Ty, IdxList: Indices)) |
8395 | return error(L: Loc, Msg: "invalid getelementptr indices" ); |
8396 | Inst = GetElementPtrInst::Create(PointeeType: Ty, Ptr, IdxList: Indices); |
8397 | if (InBounds) |
8398 | cast<GetElementPtrInst>(Val: Inst)->setIsInBounds(true); |
8399 | return AteExtraComma ? InstExtraComma : InstNormal; |
8400 | } |
8401 | |
8402 | /// parseExtractValue |
8403 | /// ::= 'extractvalue' TypeAndValue (',' uint32)+ |
8404 | int LLParser::(Instruction *&Inst, PerFunctionState &PFS) { |
8405 | Value *Val; LocTy Loc; |
8406 | SmallVector<unsigned, 4> Indices; |
8407 | bool ; |
8408 | if (parseTypeAndValue(V&: Val, Loc, PFS) || |
8409 | parseIndexList(Indices, AteExtraComma)) |
8410 | return true; |
8411 | |
8412 | if (!Val->getType()->isAggregateType()) |
8413 | return error(L: Loc, Msg: "extractvalue operand must be aggregate type" ); |
8414 | |
8415 | if (!ExtractValueInst::getIndexedType(Agg: Val->getType(), Idxs: Indices)) |
8416 | return error(L: Loc, Msg: "invalid indices for extractvalue" ); |
8417 | Inst = ExtractValueInst::Create(Agg: Val, Idxs: Indices); |
8418 | return AteExtraComma ? InstExtraComma : InstNormal; |
8419 | } |
8420 | |
8421 | /// parseInsertValue |
8422 | /// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+ |
8423 | int LLParser::parseInsertValue(Instruction *&Inst, PerFunctionState &PFS) { |
8424 | Value *Val0, *Val1; LocTy Loc0, Loc1; |
8425 | SmallVector<unsigned, 4> Indices; |
8426 | bool ; |
8427 | if (parseTypeAndValue(V&: Val0, Loc&: Loc0, PFS) || |
8428 | parseToken(T: lltok::comma, ErrMsg: "expected comma after insertvalue operand" ) || |
8429 | parseTypeAndValue(V&: Val1, Loc&: Loc1, PFS) || |
8430 | parseIndexList(Indices, AteExtraComma)) |
8431 | return true; |
8432 | |
8433 | if (!Val0->getType()->isAggregateType()) |
8434 | return error(L: Loc0, Msg: "insertvalue operand must be aggregate type" ); |
8435 | |
8436 | Type *IndexedType = ExtractValueInst::getIndexedType(Agg: Val0->getType(), Idxs: Indices); |
8437 | if (!IndexedType) |
8438 | return error(L: Loc0, Msg: "invalid indices for insertvalue" ); |
8439 | if (IndexedType != Val1->getType()) |
8440 | return error(L: Loc1, Msg: "insertvalue operand and field disagree in type: '" + |
8441 | getTypeString(T: Val1->getType()) + "' instead of '" + |
8442 | getTypeString(T: IndexedType) + "'" ); |
8443 | Inst = InsertValueInst::Create(Agg: Val0, Val: Val1, Idxs: Indices); |
8444 | return AteExtraComma ? InstExtraComma : InstNormal; |
8445 | } |
8446 | |
8447 | //===----------------------------------------------------------------------===// |
8448 | // Embedded metadata. |
8449 | //===----------------------------------------------------------------------===// |
8450 | |
8451 | /// parseMDNodeVector |
8452 | /// ::= { Element (',' Element)* } |
8453 | /// Element |
8454 | /// ::= 'null' | Metadata |
8455 | bool LLParser::parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) { |
8456 | if (parseToken(T: lltok::lbrace, ErrMsg: "expected '{' here" )) |
8457 | return true; |
8458 | |
8459 | // Check for an empty list. |
8460 | if (EatIfPresent(T: lltok::rbrace)) |
8461 | return false; |
8462 | |
8463 | do { |
8464 | if (EatIfPresent(T: lltok::kw_null)) { |
8465 | Elts.push_back(Elt: nullptr); |
8466 | continue; |
8467 | } |
8468 | |
8469 | Metadata *MD; |
8470 | if (parseMetadata(MD, PFS: nullptr)) |
8471 | return true; |
8472 | Elts.push_back(Elt: MD); |
8473 | } while (EatIfPresent(T: lltok::comma)); |
8474 | |
8475 | return parseToken(T: lltok::rbrace, ErrMsg: "expected end of metadata node" ); |
8476 | } |
8477 | |
8478 | //===----------------------------------------------------------------------===// |
8479 | // Use-list order directives. |
8480 | //===----------------------------------------------------------------------===// |
8481 | bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, |
8482 | SMLoc Loc) { |
8483 | if (V->use_empty()) |
8484 | return error(L: Loc, Msg: "value has no uses" ); |
8485 | |
8486 | unsigned NumUses = 0; |
8487 | SmallDenseMap<const Use *, unsigned, 16> Order; |
8488 | for (const Use &U : V->uses()) { |
8489 | if (++NumUses > Indexes.size()) |
8490 | break; |
8491 | Order[&U] = Indexes[NumUses - 1]; |
8492 | } |
8493 | if (NumUses < 2) |
8494 | return error(L: Loc, Msg: "value only has one use" ); |
8495 | if (Order.size() != Indexes.size() || NumUses > Indexes.size()) |
8496 | return error(L: Loc, |
8497 | Msg: "wrong number of indexes, expected " + Twine(V->getNumUses())); |
8498 | |
8499 | V->sortUseList(Cmp: [&](const Use &L, const Use &R) { |
8500 | return Order.lookup(Val: &L) < Order.lookup(Val: &R); |
8501 | }); |
8502 | return false; |
8503 | } |
8504 | |
8505 | /// parseUseListOrderIndexes |
8506 | /// ::= '{' uint32 (',' uint32)+ '}' |
8507 | bool LLParser::parseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) { |
8508 | SMLoc Loc = Lex.getLoc(); |
8509 | if (parseToken(T: lltok::lbrace, ErrMsg: "expected '{' here" )) |
8510 | return true; |
8511 | if (Lex.getKind() == lltok::rbrace) |
8512 | return Lex.Error(Msg: "expected non-empty list of uselistorder indexes" ); |
8513 | |
8514 | // Use Offset, Max, and IsOrdered to check consistency of indexes. The |
8515 | // indexes should be distinct numbers in the range [0, size-1], and should |
8516 | // not be in order. |
8517 | unsigned Offset = 0; |
8518 | unsigned Max = 0; |
8519 | bool IsOrdered = true; |
8520 | assert(Indexes.empty() && "Expected empty order vector" ); |
8521 | do { |
8522 | unsigned Index; |
8523 | if (parseUInt32(Val&: Index)) |
8524 | return true; |
8525 | |
8526 | // Update consistency checks. |
8527 | Offset += Index - Indexes.size(); |
8528 | Max = std::max(a: Max, b: Index); |
8529 | IsOrdered &= Index == Indexes.size(); |
8530 | |
8531 | Indexes.push_back(Elt: Index); |
8532 | } while (EatIfPresent(T: lltok::comma)); |
8533 | |
8534 | if (parseToken(T: lltok::rbrace, ErrMsg: "expected '}' here" )) |
8535 | return true; |
8536 | |
8537 | if (Indexes.size() < 2) |
8538 | return error(L: Loc, Msg: "expected >= 2 uselistorder indexes" ); |
8539 | if (Offset != 0 || Max >= Indexes.size()) |
8540 | return error(L: Loc, |
8541 | Msg: "expected distinct uselistorder indexes in range [0, size)" ); |
8542 | if (IsOrdered) |
8543 | return error(L: Loc, Msg: "expected uselistorder indexes to change the order" ); |
8544 | |
8545 | return false; |
8546 | } |
8547 | |
8548 | /// parseUseListOrder |
8549 | /// ::= 'uselistorder' Type Value ',' UseListOrderIndexes |
8550 | bool LLParser::parseUseListOrder(PerFunctionState *PFS) { |
8551 | SMLoc Loc = Lex.getLoc(); |
8552 | if (parseToken(T: lltok::kw_uselistorder, ErrMsg: "expected uselistorder directive" )) |
8553 | return true; |
8554 | |
8555 | Value *V; |
8556 | SmallVector<unsigned, 16> Indexes; |
8557 | if (parseTypeAndValue(V, PFS) || |
8558 | parseToken(T: lltok::comma, ErrMsg: "expected comma in uselistorder directive" ) || |
8559 | parseUseListOrderIndexes(Indexes)) |
8560 | return true; |
8561 | |
8562 | return sortUseListOrder(V, Indexes, Loc); |
8563 | } |
8564 | |
8565 | /// parseUseListOrderBB |
8566 | /// ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes |
8567 | bool LLParser::parseUseListOrderBB() { |
8568 | assert(Lex.getKind() == lltok::kw_uselistorder_bb); |
8569 | SMLoc Loc = Lex.getLoc(); |
8570 | Lex.Lex(); |
8571 | |
8572 | ValID Fn, Label; |
8573 | SmallVector<unsigned, 16> Indexes; |
8574 | if (parseValID(ID&: Fn, /*PFS=*/nullptr) || |
8575 | parseToken(T: lltok::comma, ErrMsg: "expected comma in uselistorder_bb directive" ) || |
8576 | parseValID(ID&: Label, /*PFS=*/nullptr) || |
8577 | parseToken(T: lltok::comma, ErrMsg: "expected comma in uselistorder_bb directive" ) || |
8578 | parseUseListOrderIndexes(Indexes)) |
8579 | return true; |
8580 | |
8581 | // Check the function. |
8582 | GlobalValue *GV; |
8583 | if (Fn.Kind == ValID::t_GlobalName) |
8584 | GV = M->getNamedValue(Name: Fn.StrVal); |
8585 | else if (Fn.Kind == ValID::t_GlobalID) |
8586 | GV = NumberedVals.get(ID: Fn.UIntVal); |
8587 | else |
8588 | return error(L: Fn.Loc, Msg: "expected function name in uselistorder_bb" ); |
8589 | if (!GV) |
8590 | return error(L: Fn.Loc, |
8591 | Msg: "invalid function forward reference in uselistorder_bb" ); |
8592 | auto *F = dyn_cast<Function>(Val: GV); |
8593 | if (!F) |
8594 | return error(L: Fn.Loc, Msg: "expected function name in uselistorder_bb" ); |
8595 | if (F->isDeclaration()) |
8596 | return error(L: Fn.Loc, Msg: "invalid declaration in uselistorder_bb" ); |
8597 | |
8598 | // Check the basic block. |
8599 | if (Label.Kind == ValID::t_LocalID) |
8600 | return error(L: Label.Loc, Msg: "invalid numeric label in uselistorder_bb" ); |
8601 | if (Label.Kind != ValID::t_LocalName) |
8602 | return error(L: Label.Loc, Msg: "expected basic block name in uselistorder_bb" ); |
8603 | Value *V = F->getValueSymbolTable()->lookup(Name: Label.StrVal); |
8604 | if (!V) |
8605 | return error(L: Label.Loc, Msg: "invalid basic block in uselistorder_bb" ); |
8606 | if (!isa<BasicBlock>(Val: V)) |
8607 | return error(L: Label.Loc, Msg: "expected basic block in uselistorder_bb" ); |
8608 | |
8609 | return sortUseListOrder(V, Indexes, Loc); |
8610 | } |
8611 | |
8612 | /// ModuleEntry |
8613 | /// ::= 'module' ':' '(' 'path' ':' STRINGCONSTANT ',' 'hash' ':' Hash ')' |
8614 | /// Hash ::= '(' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ')' |
8615 | bool LLParser::parseModuleEntry(unsigned ID) { |
8616 | assert(Lex.getKind() == lltok::kw_module); |
8617 | Lex.Lex(); |
8618 | |
8619 | std::string Path; |
8620 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8621 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
8622 | parseToken(T: lltok::kw_path, ErrMsg: "expected 'path' here" ) || |
8623 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8624 | parseStringConstant(Result&: Path) || |
8625 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
8626 | parseToken(T: lltok::kw_hash, ErrMsg: "expected 'hash' here" ) || |
8627 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8628 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
8629 | return true; |
8630 | |
8631 | ModuleHash Hash; |
8632 | if (parseUInt32(Val&: Hash[0]) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
8633 | parseUInt32(Val&: Hash[1]) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
8634 | parseUInt32(Val&: Hash[2]) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
8635 | parseUInt32(Val&: Hash[3]) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
8636 | parseUInt32(Val&: Hash[4])) |
8637 | return true; |
8638 | |
8639 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" ) || |
8640 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
8641 | return true; |
8642 | |
8643 | auto ModuleEntry = Index->addModule(ModPath: Path, Hash); |
8644 | ModuleIdMap[ID] = ModuleEntry->first(); |
8645 | |
8646 | return false; |
8647 | } |
8648 | |
8649 | /// TypeIdEntry |
8650 | /// ::= 'typeid' ':' '(' 'name' ':' STRINGCONSTANT ',' TypeIdSummary ')' |
8651 | bool LLParser::parseTypeIdEntry(unsigned ID) { |
8652 | assert(Lex.getKind() == lltok::kw_typeid); |
8653 | Lex.Lex(); |
8654 | |
8655 | std::string Name; |
8656 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8657 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
8658 | parseToken(T: lltok::kw_name, ErrMsg: "expected 'name' here" ) || |
8659 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8660 | parseStringConstant(Result&: Name)) |
8661 | return true; |
8662 | |
8663 | TypeIdSummary &TIS = Index->getOrInsertTypeIdSummary(TypeId: Name); |
8664 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
8665 | parseTypeIdSummary(TIS) || parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
8666 | return true; |
8667 | |
8668 | // Check if this ID was forward referenced, and if so, update the |
8669 | // corresponding GUIDs. |
8670 | auto FwdRefTIDs = ForwardRefTypeIds.find(x: ID); |
8671 | if (FwdRefTIDs != ForwardRefTypeIds.end()) { |
8672 | for (auto TIDRef : FwdRefTIDs->second) { |
8673 | assert(!*TIDRef.first && |
8674 | "Forward referenced type id GUID expected to be 0" ); |
8675 | *TIDRef.first = GlobalValue::getGUID(GlobalName: Name); |
8676 | } |
8677 | ForwardRefTypeIds.erase(position: FwdRefTIDs); |
8678 | } |
8679 | |
8680 | return false; |
8681 | } |
8682 | |
8683 | /// TypeIdSummary |
8684 | /// ::= 'summary' ':' '(' TypeTestResolution [',' OptionalWpdResolutions]? ')' |
8685 | bool LLParser::parseTypeIdSummary(TypeIdSummary &TIS) { |
8686 | if (parseToken(T: lltok::kw_summary, ErrMsg: "expected 'summary' here" ) || |
8687 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8688 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
8689 | parseTypeTestResolution(TTRes&: TIS.TTRes)) |
8690 | return true; |
8691 | |
8692 | if (EatIfPresent(T: lltok::comma)) { |
8693 | // Expect optional wpdResolutions field |
8694 | if (parseOptionalWpdResolutions(WPDResMap&: TIS.WPDRes)) |
8695 | return true; |
8696 | } |
8697 | |
8698 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
8699 | return true; |
8700 | |
8701 | return false; |
8702 | } |
8703 | |
8704 | static ValueInfo EmptyVI = |
8705 | ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8); |
8706 | |
8707 | /// TypeIdCompatibleVtableEntry |
8708 | /// ::= 'typeidCompatibleVTable' ':' '(' 'name' ':' STRINGCONSTANT ',' |
8709 | /// TypeIdCompatibleVtableInfo |
8710 | /// ')' |
8711 | bool LLParser::parseTypeIdCompatibleVtableEntry(unsigned ID) { |
8712 | assert(Lex.getKind() == lltok::kw_typeidCompatibleVTable); |
8713 | Lex.Lex(); |
8714 | |
8715 | std::string Name; |
8716 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8717 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
8718 | parseToken(T: lltok::kw_name, ErrMsg: "expected 'name' here" ) || |
8719 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8720 | parseStringConstant(Result&: Name)) |
8721 | return true; |
8722 | |
8723 | TypeIdCompatibleVtableInfo &TI = |
8724 | Index->getOrInsertTypeIdCompatibleVtableSummary(TypeId: Name); |
8725 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
8726 | parseToken(T: lltok::kw_summary, ErrMsg: "expected 'summary' here" ) || |
8727 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8728 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
8729 | return true; |
8730 | |
8731 | IdToIndexMapType IdToIndexMap; |
8732 | // parse each call edge |
8733 | do { |
8734 | uint64_t Offset; |
8735 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
8736 | parseToken(T: lltok::kw_offset, ErrMsg: "expected 'offset' here" ) || |
8737 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || parseUInt64(Val&: Offset) || |
8738 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" )) |
8739 | return true; |
8740 | |
8741 | LocTy Loc = Lex.getLoc(); |
8742 | unsigned GVId; |
8743 | ValueInfo VI; |
8744 | if (parseGVReference(VI, GVId)) |
8745 | return true; |
8746 | |
8747 | // Keep track of the TypeIdCompatibleVtableInfo array index needing a |
8748 | // forward reference. We will save the location of the ValueInfo needing an |
8749 | // update, but can only do so once the std::vector is finalized. |
8750 | if (VI == EmptyVI) |
8751 | IdToIndexMap[GVId].push_back(x: std::make_pair(x: TI.size(), y&: Loc)); |
8752 | TI.push_back(x: {Offset, VI}); |
8753 | |
8754 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in call" )) |
8755 | return true; |
8756 | } while (EatIfPresent(T: lltok::comma)); |
8757 | |
8758 | // Now that the TI vector is finalized, it is safe to save the locations |
8759 | // of any forward GV references that need updating later. |
8760 | for (auto I : IdToIndexMap) { |
8761 | auto &Infos = ForwardRefValueInfos[I.first]; |
8762 | for (auto P : I.second) { |
8763 | assert(TI[P.first].VTableVI == EmptyVI && |
8764 | "Forward referenced ValueInfo expected to be empty" ); |
8765 | Infos.emplace_back(args: &TI[P.first].VTableVI, args&: P.second); |
8766 | } |
8767 | } |
8768 | |
8769 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" ) || |
8770 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
8771 | return true; |
8772 | |
8773 | // Check if this ID was forward referenced, and if so, update the |
8774 | // corresponding GUIDs. |
8775 | auto FwdRefTIDs = ForwardRefTypeIds.find(x: ID); |
8776 | if (FwdRefTIDs != ForwardRefTypeIds.end()) { |
8777 | for (auto TIDRef : FwdRefTIDs->second) { |
8778 | assert(!*TIDRef.first && |
8779 | "Forward referenced type id GUID expected to be 0" ); |
8780 | *TIDRef.first = GlobalValue::getGUID(GlobalName: Name); |
8781 | } |
8782 | ForwardRefTypeIds.erase(position: FwdRefTIDs); |
8783 | } |
8784 | |
8785 | return false; |
8786 | } |
8787 | |
8788 | /// TypeTestResolution |
8789 | /// ::= 'typeTestRes' ':' '(' 'kind' ':' |
8790 | /// ( 'unsat' | 'byteArray' | 'inline' | 'single' | 'allOnes' ) ',' |
8791 | /// 'sizeM1BitWidth' ':' SizeM1BitWidth [',' 'alignLog2' ':' UInt64]? |
8792 | /// [',' 'sizeM1' ':' UInt64]? [',' 'bitMask' ':' UInt8]? |
8793 | /// [',' 'inlinesBits' ':' UInt64]? ')' |
8794 | bool LLParser::parseTypeTestResolution(TypeTestResolution &TTRes) { |
8795 | if (parseToken(T: lltok::kw_typeTestRes, ErrMsg: "expected 'typeTestRes' here" ) || |
8796 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8797 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
8798 | parseToken(T: lltok::kw_kind, ErrMsg: "expected 'kind' here" ) || |
8799 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" )) |
8800 | return true; |
8801 | |
8802 | switch (Lex.getKind()) { |
8803 | case lltok::kw_unknown: |
8804 | TTRes.TheKind = TypeTestResolution::Unknown; |
8805 | break; |
8806 | case lltok::kw_unsat: |
8807 | TTRes.TheKind = TypeTestResolution::Unsat; |
8808 | break; |
8809 | case lltok::kw_byteArray: |
8810 | TTRes.TheKind = TypeTestResolution::ByteArray; |
8811 | break; |
8812 | case lltok::kw_inline: |
8813 | TTRes.TheKind = TypeTestResolution::Inline; |
8814 | break; |
8815 | case lltok::kw_single: |
8816 | TTRes.TheKind = TypeTestResolution::Single; |
8817 | break; |
8818 | case lltok::kw_allOnes: |
8819 | TTRes.TheKind = TypeTestResolution::AllOnes; |
8820 | break; |
8821 | default: |
8822 | return error(L: Lex.getLoc(), Msg: "unexpected TypeTestResolution kind" ); |
8823 | } |
8824 | Lex.Lex(); |
8825 | |
8826 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
8827 | parseToken(T: lltok::kw_sizeM1BitWidth, ErrMsg: "expected 'sizeM1BitWidth' here" ) || |
8828 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8829 | parseUInt32(Val&: TTRes.SizeM1BitWidth)) |
8830 | return true; |
8831 | |
8832 | // parse optional fields |
8833 | while (EatIfPresent(T: lltok::comma)) { |
8834 | switch (Lex.getKind()) { |
8835 | case lltok::kw_alignLog2: |
8836 | Lex.Lex(); |
8837 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || |
8838 | parseUInt64(Val&: TTRes.AlignLog2)) |
8839 | return true; |
8840 | break; |
8841 | case lltok::kw_sizeM1: |
8842 | Lex.Lex(); |
8843 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseUInt64(Val&: TTRes.SizeM1)) |
8844 | return true; |
8845 | break; |
8846 | case lltok::kw_bitMask: { |
8847 | unsigned Val; |
8848 | Lex.Lex(); |
8849 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseUInt32(Val)) |
8850 | return true; |
8851 | assert(Val <= 0xff); |
8852 | TTRes.BitMask = (uint8_t)Val; |
8853 | break; |
8854 | } |
8855 | case lltok::kw_inlineBits: |
8856 | Lex.Lex(); |
8857 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || |
8858 | parseUInt64(Val&: TTRes.InlineBits)) |
8859 | return true; |
8860 | break; |
8861 | default: |
8862 | return error(L: Lex.getLoc(), Msg: "expected optional TypeTestResolution field" ); |
8863 | } |
8864 | } |
8865 | |
8866 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
8867 | return true; |
8868 | |
8869 | return false; |
8870 | } |
8871 | |
8872 | /// OptionalWpdResolutions |
8873 | /// ::= 'wpsResolutions' ':' '(' WpdResolution [',' WpdResolution]* ')' |
8874 | /// WpdResolution ::= '(' 'offset' ':' UInt64 ',' WpdRes ')' |
8875 | bool LLParser::parseOptionalWpdResolutions( |
8876 | std::map<uint64_t, WholeProgramDevirtResolution> &WPDResMap) { |
8877 | if (parseToken(T: lltok::kw_wpdResolutions, ErrMsg: "expected 'wpdResolutions' here" ) || |
8878 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8879 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
8880 | return true; |
8881 | |
8882 | do { |
8883 | uint64_t Offset; |
8884 | WholeProgramDevirtResolution WPDRes; |
8885 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
8886 | parseToken(T: lltok::kw_offset, ErrMsg: "expected 'offset' here" ) || |
8887 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || parseUInt64(Val&: Offset) || |
8888 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || parseWpdRes(WPDRes) || |
8889 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
8890 | return true; |
8891 | WPDResMap[Offset] = WPDRes; |
8892 | } while (EatIfPresent(T: lltok::comma)); |
8893 | |
8894 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
8895 | return true; |
8896 | |
8897 | return false; |
8898 | } |
8899 | |
8900 | /// WpdRes |
8901 | /// ::= 'wpdRes' ':' '(' 'kind' ':' 'indir' |
8902 | /// [',' OptionalResByArg]? ')' |
8903 | /// ::= 'wpdRes' ':' '(' 'kind' ':' 'singleImpl' |
8904 | /// ',' 'singleImplName' ':' STRINGCONSTANT ',' |
8905 | /// [',' OptionalResByArg]? ')' |
8906 | /// ::= 'wpdRes' ':' '(' 'kind' ':' 'branchFunnel' |
8907 | /// [',' OptionalResByArg]? ')' |
8908 | bool LLParser::parseWpdRes(WholeProgramDevirtResolution &WPDRes) { |
8909 | if (parseToken(T: lltok::kw_wpdRes, ErrMsg: "expected 'wpdRes' here" ) || |
8910 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8911 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
8912 | parseToken(T: lltok::kw_kind, ErrMsg: "expected 'kind' here" ) || |
8913 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" )) |
8914 | return true; |
8915 | |
8916 | switch (Lex.getKind()) { |
8917 | case lltok::kw_indir: |
8918 | WPDRes.TheKind = WholeProgramDevirtResolution::Indir; |
8919 | break; |
8920 | case lltok::kw_singleImpl: |
8921 | WPDRes.TheKind = WholeProgramDevirtResolution::SingleImpl; |
8922 | break; |
8923 | case lltok::kw_branchFunnel: |
8924 | WPDRes.TheKind = WholeProgramDevirtResolution::BranchFunnel; |
8925 | break; |
8926 | default: |
8927 | return error(L: Lex.getLoc(), Msg: "unexpected WholeProgramDevirtResolution kind" ); |
8928 | } |
8929 | Lex.Lex(); |
8930 | |
8931 | // parse optional fields |
8932 | while (EatIfPresent(T: lltok::comma)) { |
8933 | switch (Lex.getKind()) { |
8934 | case lltok::kw_singleImplName: |
8935 | Lex.Lex(); |
8936 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8937 | parseStringConstant(Result&: WPDRes.SingleImplName)) |
8938 | return true; |
8939 | break; |
8940 | case lltok::kw_resByArg: |
8941 | if (parseOptionalResByArg(ResByArg&: WPDRes.ResByArg)) |
8942 | return true; |
8943 | break; |
8944 | default: |
8945 | return error(L: Lex.getLoc(), |
8946 | Msg: "expected optional WholeProgramDevirtResolution field" ); |
8947 | } |
8948 | } |
8949 | |
8950 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
8951 | return true; |
8952 | |
8953 | return false; |
8954 | } |
8955 | |
8956 | /// OptionalResByArg |
8957 | /// ::= 'wpdRes' ':' '(' ResByArg[, ResByArg]* ')' |
8958 | /// ResByArg ::= Args ',' 'byArg' ':' '(' 'kind' ':' |
8959 | /// ( 'indir' | 'uniformRetVal' | 'UniqueRetVal' | |
8960 | /// 'virtualConstProp' ) |
8961 | /// [',' 'info' ':' UInt64]? [',' 'byte' ':' UInt32]? |
8962 | /// [',' 'bit' ':' UInt32]? ')' |
8963 | bool LLParser::parseOptionalResByArg( |
8964 | std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg> |
8965 | &ResByArg) { |
8966 | if (parseToken(T: lltok::kw_resByArg, ErrMsg: "expected 'resByArg' here" ) || |
8967 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8968 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
8969 | return true; |
8970 | |
8971 | do { |
8972 | std::vector<uint64_t> Args; |
8973 | if (parseArgs(Args) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
8974 | parseToken(T: lltok::kw_byArg, ErrMsg: "expected 'byArg here" ) || |
8975 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
8976 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
8977 | parseToken(T: lltok::kw_kind, ErrMsg: "expected 'kind' here" ) || |
8978 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" )) |
8979 | return true; |
8980 | |
8981 | WholeProgramDevirtResolution::ByArg ByArg; |
8982 | switch (Lex.getKind()) { |
8983 | case lltok::kw_indir: |
8984 | ByArg.TheKind = WholeProgramDevirtResolution::ByArg::Indir; |
8985 | break; |
8986 | case lltok::kw_uniformRetVal: |
8987 | ByArg.TheKind = WholeProgramDevirtResolution::ByArg::UniformRetVal; |
8988 | break; |
8989 | case lltok::kw_uniqueRetVal: |
8990 | ByArg.TheKind = WholeProgramDevirtResolution::ByArg::UniqueRetVal; |
8991 | break; |
8992 | case lltok::kw_virtualConstProp: |
8993 | ByArg.TheKind = WholeProgramDevirtResolution::ByArg::VirtualConstProp; |
8994 | break; |
8995 | default: |
8996 | return error(L: Lex.getLoc(), |
8997 | Msg: "unexpected WholeProgramDevirtResolution::ByArg kind" ); |
8998 | } |
8999 | Lex.Lex(); |
9000 | |
9001 | // parse optional fields |
9002 | while (EatIfPresent(T: lltok::comma)) { |
9003 | switch (Lex.getKind()) { |
9004 | case lltok::kw_info: |
9005 | Lex.Lex(); |
9006 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9007 | parseUInt64(Val&: ByArg.Info)) |
9008 | return true; |
9009 | break; |
9010 | case lltok::kw_byte: |
9011 | Lex.Lex(); |
9012 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9013 | parseUInt32(Val&: ByArg.Byte)) |
9014 | return true; |
9015 | break; |
9016 | case lltok::kw_bit: |
9017 | Lex.Lex(); |
9018 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9019 | parseUInt32(Val&: ByArg.Bit)) |
9020 | return true; |
9021 | break; |
9022 | default: |
9023 | return error(L: Lex.getLoc(), |
9024 | Msg: "expected optional whole program devirt field" ); |
9025 | } |
9026 | } |
9027 | |
9028 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
9029 | return true; |
9030 | |
9031 | ResByArg[Args] = ByArg; |
9032 | } while (EatIfPresent(T: lltok::comma)); |
9033 | |
9034 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
9035 | return true; |
9036 | |
9037 | return false; |
9038 | } |
9039 | |
9040 | /// OptionalResByArg |
9041 | /// ::= 'args' ':' '(' UInt64[, UInt64]* ')' |
9042 | bool LLParser::parseArgs(std::vector<uint64_t> &Args) { |
9043 | if (parseToken(T: lltok::kw_args, ErrMsg: "expected 'args' here" ) || |
9044 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9045 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
9046 | return true; |
9047 | |
9048 | do { |
9049 | uint64_t Val; |
9050 | if (parseUInt64(Val)) |
9051 | return true; |
9052 | Args.push_back(x: Val); |
9053 | } while (EatIfPresent(T: lltok::comma)); |
9054 | |
9055 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
9056 | return true; |
9057 | |
9058 | return false; |
9059 | } |
9060 | |
9061 | static const auto FwdVIRef = (GlobalValueSummaryMapTy::value_type *)-8; |
9062 | |
9063 | static void resolveFwdRef(ValueInfo *Fwd, ValueInfo &Resolved) { |
9064 | bool ReadOnly = Fwd->isReadOnly(); |
9065 | bool WriteOnly = Fwd->isWriteOnly(); |
9066 | assert(!(ReadOnly && WriteOnly)); |
9067 | *Fwd = Resolved; |
9068 | if (ReadOnly) |
9069 | Fwd->setReadOnly(); |
9070 | if (WriteOnly) |
9071 | Fwd->setWriteOnly(); |
9072 | } |
9073 | |
9074 | /// Stores the given Name/GUID and associated summary into the Index. |
9075 | /// Also updates any forward references to the associated entry ID. |
9076 | bool LLParser::addGlobalValueToIndex( |
9077 | std::string Name, GlobalValue::GUID GUID, GlobalValue::LinkageTypes Linkage, |
9078 | unsigned ID, std::unique_ptr<GlobalValueSummary> Summary, LocTy Loc) { |
9079 | // First create the ValueInfo utilizing the Name or GUID. |
9080 | ValueInfo VI; |
9081 | if (GUID != 0) { |
9082 | assert(Name.empty()); |
9083 | VI = Index->getOrInsertValueInfo(GUID); |
9084 | } else { |
9085 | assert(!Name.empty()); |
9086 | if (M) { |
9087 | auto *GV = M->getNamedValue(Name); |
9088 | if (!GV) |
9089 | return error(L: Loc, Msg: "Reference to undefined global \"" + Name + "\"" ); |
9090 | |
9091 | VI = Index->getOrInsertValueInfo(GV); |
9092 | } else { |
9093 | assert( |
9094 | (!GlobalValue::isLocalLinkage(Linkage) || !SourceFileName.empty()) && |
9095 | "Need a source_filename to compute GUID for local" ); |
9096 | GUID = GlobalValue::getGUID( |
9097 | GlobalName: GlobalValue::getGlobalIdentifier(Name, Linkage, FileName: SourceFileName)); |
9098 | VI = Index->getOrInsertValueInfo(GUID, Name: Index->saveString(String: Name)); |
9099 | } |
9100 | } |
9101 | |
9102 | // Resolve forward references from calls/refs |
9103 | auto FwdRefVIs = ForwardRefValueInfos.find(x: ID); |
9104 | if (FwdRefVIs != ForwardRefValueInfos.end()) { |
9105 | for (auto VIRef : FwdRefVIs->second) { |
9106 | assert(VIRef.first->getRef() == FwdVIRef && |
9107 | "Forward referenced ValueInfo expected to be empty" ); |
9108 | resolveFwdRef(Fwd: VIRef.first, Resolved&: VI); |
9109 | } |
9110 | ForwardRefValueInfos.erase(position: FwdRefVIs); |
9111 | } |
9112 | |
9113 | // Resolve forward references from aliases |
9114 | auto FwdRefAliasees = ForwardRefAliasees.find(x: ID); |
9115 | if (FwdRefAliasees != ForwardRefAliasees.end()) { |
9116 | for (auto AliaseeRef : FwdRefAliasees->second) { |
9117 | assert(!AliaseeRef.first->hasAliasee() && |
9118 | "Forward referencing alias already has aliasee" ); |
9119 | assert(Summary && "Aliasee must be a definition" ); |
9120 | AliaseeRef.first->setAliasee(AliaseeVI&: VI, Aliasee: Summary.get()); |
9121 | } |
9122 | ForwardRefAliasees.erase(position: FwdRefAliasees); |
9123 | } |
9124 | |
9125 | // Add the summary if one was provided. |
9126 | if (Summary) |
9127 | Index->addGlobalValueSummary(VI, Summary: std::move(Summary)); |
9128 | |
9129 | // Save the associated ValueInfo for use in later references by ID. |
9130 | if (ID == NumberedValueInfos.size()) |
9131 | NumberedValueInfos.push_back(x: VI); |
9132 | else { |
9133 | // Handle non-continuous numbers (to make test simplification easier). |
9134 | if (ID > NumberedValueInfos.size()) |
9135 | NumberedValueInfos.resize(new_size: ID + 1); |
9136 | NumberedValueInfos[ID] = VI; |
9137 | } |
9138 | |
9139 | return false; |
9140 | } |
9141 | |
9142 | /// parseSummaryIndexFlags |
9143 | /// ::= 'flags' ':' UInt64 |
9144 | bool LLParser::parseSummaryIndexFlags() { |
9145 | assert(Lex.getKind() == lltok::kw_flags); |
9146 | Lex.Lex(); |
9147 | |
9148 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" )) |
9149 | return true; |
9150 | uint64_t Flags; |
9151 | if (parseUInt64(Val&: Flags)) |
9152 | return true; |
9153 | if (Index) |
9154 | Index->setFlags(Flags); |
9155 | return false; |
9156 | } |
9157 | |
9158 | /// parseBlockCount |
9159 | /// ::= 'blockcount' ':' UInt64 |
9160 | bool LLParser::parseBlockCount() { |
9161 | assert(Lex.getKind() == lltok::kw_blockcount); |
9162 | Lex.Lex(); |
9163 | |
9164 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" )) |
9165 | return true; |
9166 | uint64_t BlockCount; |
9167 | if (parseUInt64(Val&: BlockCount)) |
9168 | return true; |
9169 | if (Index) |
9170 | Index->setBlockCount(BlockCount); |
9171 | return false; |
9172 | } |
9173 | |
9174 | /// parseGVEntry |
9175 | /// ::= 'gv' ':' '(' ('name' ':' STRINGCONSTANT | 'guid' ':' UInt64) |
9176 | /// [',' 'summaries' ':' Summary[',' Summary]* ]? ')' |
9177 | /// Summary ::= '(' (FunctionSummary | VariableSummary | AliasSummary) ')' |
9178 | bool LLParser::parseGVEntry(unsigned ID) { |
9179 | assert(Lex.getKind() == lltok::kw_gv); |
9180 | Lex.Lex(); |
9181 | |
9182 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9183 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
9184 | return true; |
9185 | |
9186 | LocTy Loc = Lex.getLoc(); |
9187 | std::string Name; |
9188 | GlobalValue::GUID GUID = 0; |
9189 | switch (Lex.getKind()) { |
9190 | case lltok::kw_name: |
9191 | Lex.Lex(); |
9192 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9193 | parseStringConstant(Result&: Name)) |
9194 | return true; |
9195 | // Can't create GUID/ValueInfo until we have the linkage. |
9196 | break; |
9197 | case lltok::kw_guid: |
9198 | Lex.Lex(); |
9199 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || parseUInt64(Val&: GUID)) |
9200 | return true; |
9201 | break; |
9202 | default: |
9203 | return error(L: Lex.getLoc(), Msg: "expected name or guid tag" ); |
9204 | } |
9205 | |
9206 | if (!EatIfPresent(T: lltok::comma)) { |
9207 | // No summaries. Wrap up. |
9208 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
9209 | return true; |
9210 | // This was created for a call to an external or indirect target. |
9211 | // A GUID with no summary came from a VALUE_GUID record, dummy GUID |
9212 | // created for indirect calls with VP. A Name with no GUID came from |
9213 | // an external definition. We pass ExternalLinkage since that is only |
9214 | // used when the GUID must be computed from Name, and in that case |
9215 | // the symbol must have external linkage. |
9216 | return addGlobalValueToIndex(Name, GUID, Linkage: GlobalValue::ExternalLinkage, ID, |
9217 | Summary: nullptr, Loc); |
9218 | } |
9219 | |
9220 | // Have a list of summaries |
9221 | if (parseToken(T: lltok::kw_summaries, ErrMsg: "expected 'summaries' here" ) || |
9222 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9223 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
9224 | return true; |
9225 | do { |
9226 | switch (Lex.getKind()) { |
9227 | case lltok::kw_function: |
9228 | if (parseFunctionSummary(Name, GUID, ID)) |
9229 | return true; |
9230 | break; |
9231 | case lltok::kw_variable: |
9232 | if (parseVariableSummary(Name, GUID, ID)) |
9233 | return true; |
9234 | break; |
9235 | case lltok::kw_alias: |
9236 | if (parseAliasSummary(Name, GUID, ID)) |
9237 | return true; |
9238 | break; |
9239 | default: |
9240 | return error(L: Lex.getLoc(), Msg: "expected summary type" ); |
9241 | } |
9242 | } while (EatIfPresent(T: lltok::comma)); |
9243 | |
9244 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" ) || |
9245 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
9246 | return true; |
9247 | |
9248 | return false; |
9249 | } |
9250 | |
9251 | /// FunctionSummary |
9252 | /// ::= 'function' ':' '(' 'module' ':' ModuleReference ',' GVFlags |
9253 | /// ',' 'insts' ':' UInt32 [',' OptionalFFlags]? [',' OptionalCalls]? |
9254 | /// [',' OptionalTypeIdInfo]? [',' OptionalParamAccesses]? |
9255 | /// [',' OptionalRefs]? ')' |
9256 | bool LLParser::parseFunctionSummary(std::string Name, GlobalValue::GUID GUID, |
9257 | unsigned ID) { |
9258 | LocTy Loc = Lex.getLoc(); |
9259 | assert(Lex.getKind() == lltok::kw_function); |
9260 | Lex.Lex(); |
9261 | |
9262 | StringRef ModulePath; |
9263 | GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags( |
9264 | GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility, |
9265 | /*NotEligibleToImport=*/false, |
9266 | /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false, |
9267 | GlobalValueSummary::Definition); |
9268 | unsigned InstCount; |
9269 | std::vector<FunctionSummary::EdgeTy> Calls; |
9270 | FunctionSummary::TypeIdInfo TypeIdInfo; |
9271 | std::vector<FunctionSummary::ParamAccess> ParamAccesses; |
9272 | std::vector<ValueInfo> Refs; |
9273 | std::vector<CallsiteInfo> Callsites; |
9274 | std::vector<AllocInfo> Allocs; |
9275 | // Default is all-zeros (conservative values). |
9276 | FunctionSummary::FFlags FFlags = {}; |
9277 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9278 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
9279 | parseModuleReference(ModulePath) || |
9280 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || parseGVFlags(GVFlags) || |
9281 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
9282 | parseToken(T: lltok::kw_insts, ErrMsg: "expected 'insts' here" ) || |
9283 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || parseUInt32(Val&: InstCount)) |
9284 | return true; |
9285 | |
9286 | // parse optional fields |
9287 | while (EatIfPresent(T: lltok::comma)) { |
9288 | switch (Lex.getKind()) { |
9289 | case lltok::kw_funcFlags: |
9290 | if (parseOptionalFFlags(FFlags)) |
9291 | return true; |
9292 | break; |
9293 | case lltok::kw_calls: |
9294 | if (parseOptionalCalls(Calls)) |
9295 | return true; |
9296 | break; |
9297 | case lltok::kw_typeIdInfo: |
9298 | if (parseOptionalTypeIdInfo(TypeIdInfo)) |
9299 | return true; |
9300 | break; |
9301 | case lltok::kw_refs: |
9302 | if (parseOptionalRefs(Refs)) |
9303 | return true; |
9304 | break; |
9305 | case lltok::kw_params: |
9306 | if (parseOptionalParamAccesses(Params&: ParamAccesses)) |
9307 | return true; |
9308 | break; |
9309 | case lltok::kw_allocs: |
9310 | if (parseOptionalAllocs(Allocs)) |
9311 | return true; |
9312 | break; |
9313 | case lltok::kw_callsites: |
9314 | if (parseOptionalCallsites(Callsites)) |
9315 | return true; |
9316 | break; |
9317 | default: |
9318 | return error(L: Lex.getLoc(), Msg: "expected optional function summary field" ); |
9319 | } |
9320 | } |
9321 | |
9322 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
9323 | return true; |
9324 | |
9325 | auto FS = std::make_unique<FunctionSummary>( |
9326 | args&: GVFlags, args&: InstCount, args&: FFlags, /*EntryCount=*/args: 0, args: std::move(Refs), |
9327 | args: std::move(Calls), args: std::move(TypeIdInfo.TypeTests), |
9328 | args: std::move(TypeIdInfo.TypeTestAssumeVCalls), |
9329 | args: std::move(TypeIdInfo.TypeCheckedLoadVCalls), |
9330 | args: std::move(TypeIdInfo.TypeTestAssumeConstVCalls), |
9331 | args: std::move(TypeIdInfo.TypeCheckedLoadConstVCalls), |
9332 | args: std::move(ParamAccesses), args: std::move(Callsites), args: std::move(Allocs)); |
9333 | |
9334 | FS->setModulePath(ModulePath); |
9335 | |
9336 | return addGlobalValueToIndex(Name, GUID, |
9337 | Linkage: (GlobalValue::LinkageTypes)GVFlags.Linkage, ID, |
9338 | Summary: std::move(FS), Loc); |
9339 | } |
9340 | |
9341 | /// VariableSummary |
9342 | /// ::= 'variable' ':' '(' 'module' ':' ModuleReference ',' GVFlags |
9343 | /// [',' OptionalRefs]? ')' |
9344 | bool LLParser::parseVariableSummary(std::string Name, GlobalValue::GUID GUID, |
9345 | unsigned ID) { |
9346 | LocTy Loc = Lex.getLoc(); |
9347 | assert(Lex.getKind() == lltok::kw_variable); |
9348 | Lex.Lex(); |
9349 | |
9350 | StringRef ModulePath; |
9351 | GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags( |
9352 | GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility, |
9353 | /*NotEligibleToImport=*/false, |
9354 | /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false, |
9355 | GlobalValueSummary::Definition); |
9356 | GlobalVarSummary::GVarFlags GVarFlags(/*ReadOnly*/ false, |
9357 | /* WriteOnly */ false, |
9358 | /* Constant */ false, |
9359 | GlobalObject::VCallVisibilityPublic); |
9360 | std::vector<ValueInfo> Refs; |
9361 | VTableFuncList VTableFuncs; |
9362 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9363 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
9364 | parseModuleReference(ModulePath) || |
9365 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || parseGVFlags(GVFlags) || |
9366 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
9367 | parseGVarFlags(GVarFlags)) |
9368 | return true; |
9369 | |
9370 | // parse optional fields |
9371 | while (EatIfPresent(T: lltok::comma)) { |
9372 | switch (Lex.getKind()) { |
9373 | case lltok::kw_vTableFuncs: |
9374 | if (parseOptionalVTableFuncs(VTableFuncs)) |
9375 | return true; |
9376 | break; |
9377 | case lltok::kw_refs: |
9378 | if (parseOptionalRefs(Refs)) |
9379 | return true; |
9380 | break; |
9381 | default: |
9382 | return error(L: Lex.getLoc(), Msg: "expected optional variable summary field" ); |
9383 | } |
9384 | } |
9385 | |
9386 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
9387 | return true; |
9388 | |
9389 | auto GS = |
9390 | std::make_unique<GlobalVarSummary>(args&: GVFlags, args&: GVarFlags, args: std::move(Refs)); |
9391 | |
9392 | GS->setModulePath(ModulePath); |
9393 | GS->setVTableFuncs(std::move(VTableFuncs)); |
9394 | |
9395 | return addGlobalValueToIndex(Name, GUID, |
9396 | Linkage: (GlobalValue::LinkageTypes)GVFlags.Linkage, ID, |
9397 | Summary: std::move(GS), Loc); |
9398 | } |
9399 | |
9400 | /// AliasSummary |
9401 | /// ::= 'alias' ':' '(' 'module' ':' ModuleReference ',' GVFlags ',' |
9402 | /// 'aliasee' ':' GVReference ')' |
9403 | bool LLParser::parseAliasSummary(std::string Name, GlobalValue::GUID GUID, |
9404 | unsigned ID) { |
9405 | assert(Lex.getKind() == lltok::kw_alias); |
9406 | LocTy Loc = Lex.getLoc(); |
9407 | Lex.Lex(); |
9408 | |
9409 | StringRef ModulePath; |
9410 | GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags( |
9411 | GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility, |
9412 | /*NotEligibleToImport=*/false, |
9413 | /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false, |
9414 | GlobalValueSummary::Definition); |
9415 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9416 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
9417 | parseModuleReference(ModulePath) || |
9418 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || parseGVFlags(GVFlags) || |
9419 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
9420 | parseToken(T: lltok::kw_aliasee, ErrMsg: "expected 'aliasee' here" ) || |
9421 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" )) |
9422 | return true; |
9423 | |
9424 | ValueInfo AliaseeVI; |
9425 | unsigned GVId; |
9426 | if (parseGVReference(VI&: AliaseeVI, GVId)) |
9427 | return true; |
9428 | |
9429 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
9430 | return true; |
9431 | |
9432 | auto AS = std::make_unique<AliasSummary>(args&: GVFlags); |
9433 | |
9434 | AS->setModulePath(ModulePath); |
9435 | |
9436 | // Record forward reference if the aliasee is not parsed yet. |
9437 | if (AliaseeVI.getRef() == FwdVIRef) { |
9438 | ForwardRefAliasees[GVId].emplace_back(args: AS.get(), args&: Loc); |
9439 | } else { |
9440 | auto Summary = Index->findSummaryInModule(VI: AliaseeVI, ModuleId: ModulePath); |
9441 | assert(Summary && "Aliasee must be a definition" ); |
9442 | AS->setAliasee(AliaseeVI, Aliasee: Summary); |
9443 | } |
9444 | |
9445 | return addGlobalValueToIndex(Name, GUID, |
9446 | Linkage: (GlobalValue::LinkageTypes)GVFlags.Linkage, ID, |
9447 | Summary: std::move(AS), Loc); |
9448 | } |
9449 | |
9450 | /// Flag |
9451 | /// ::= [0|1] |
9452 | bool LLParser::parseFlag(unsigned &Val) { |
9453 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
9454 | return tokError(Msg: "expected integer" ); |
9455 | Val = (unsigned)Lex.getAPSIntVal().getBoolValue(); |
9456 | Lex.Lex(); |
9457 | return false; |
9458 | } |
9459 | |
9460 | /// OptionalFFlags |
9461 | /// := 'funcFlags' ':' '(' ['readNone' ':' Flag]? |
9462 | /// [',' 'readOnly' ':' Flag]? [',' 'noRecurse' ':' Flag]? |
9463 | /// [',' 'returnDoesNotAlias' ':' Flag]? ')' |
9464 | /// [',' 'noInline' ':' Flag]? ')' |
9465 | /// [',' 'alwaysInline' ':' Flag]? ')' |
9466 | /// [',' 'noUnwind' ':' Flag]? ')' |
9467 | /// [',' 'mayThrow' ':' Flag]? ')' |
9468 | /// [',' 'hasUnknownCall' ':' Flag]? ')' |
9469 | /// [',' 'mustBeUnreachable' ':' Flag]? ')' |
9470 | |
9471 | bool LLParser::parseOptionalFFlags(FunctionSummary::FFlags &FFlags) { |
9472 | assert(Lex.getKind() == lltok::kw_funcFlags); |
9473 | Lex.Lex(); |
9474 | |
9475 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in funcFlags" ) || |
9476 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in funcFlags" )) |
9477 | return true; |
9478 | |
9479 | do { |
9480 | unsigned Val = 0; |
9481 | switch (Lex.getKind()) { |
9482 | case lltok::kw_readNone: |
9483 | Lex.Lex(); |
9484 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
9485 | return true; |
9486 | FFlags.ReadNone = Val; |
9487 | break; |
9488 | case lltok::kw_readOnly: |
9489 | Lex.Lex(); |
9490 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
9491 | return true; |
9492 | FFlags.ReadOnly = Val; |
9493 | break; |
9494 | case lltok::kw_noRecurse: |
9495 | Lex.Lex(); |
9496 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
9497 | return true; |
9498 | FFlags.NoRecurse = Val; |
9499 | break; |
9500 | case lltok::kw_returnDoesNotAlias: |
9501 | Lex.Lex(); |
9502 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
9503 | return true; |
9504 | FFlags.ReturnDoesNotAlias = Val; |
9505 | break; |
9506 | case lltok::kw_noInline: |
9507 | Lex.Lex(); |
9508 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
9509 | return true; |
9510 | FFlags.NoInline = Val; |
9511 | break; |
9512 | case lltok::kw_alwaysInline: |
9513 | Lex.Lex(); |
9514 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
9515 | return true; |
9516 | FFlags.AlwaysInline = Val; |
9517 | break; |
9518 | case lltok::kw_noUnwind: |
9519 | Lex.Lex(); |
9520 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
9521 | return true; |
9522 | FFlags.NoUnwind = Val; |
9523 | break; |
9524 | case lltok::kw_mayThrow: |
9525 | Lex.Lex(); |
9526 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
9527 | return true; |
9528 | FFlags.MayThrow = Val; |
9529 | break; |
9530 | case lltok::kw_hasUnknownCall: |
9531 | Lex.Lex(); |
9532 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
9533 | return true; |
9534 | FFlags.HasUnknownCall = Val; |
9535 | break; |
9536 | case lltok::kw_mustBeUnreachable: |
9537 | Lex.Lex(); |
9538 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
9539 | return true; |
9540 | FFlags.MustBeUnreachable = Val; |
9541 | break; |
9542 | default: |
9543 | return error(L: Lex.getLoc(), Msg: "expected function flag type" ); |
9544 | } |
9545 | } while (EatIfPresent(T: lltok::comma)); |
9546 | |
9547 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in funcFlags" )) |
9548 | return true; |
9549 | |
9550 | return false; |
9551 | } |
9552 | |
9553 | /// OptionalCalls |
9554 | /// := 'calls' ':' '(' Call [',' Call]* ')' |
9555 | /// Call ::= '(' 'callee' ':' GVReference |
9556 | /// [( ',' 'hotness' ':' Hotness | ',' 'relbf' ':' UInt32 )]? |
9557 | /// [ ',' 'tail' ]? ')' |
9558 | bool LLParser::parseOptionalCalls(std::vector<FunctionSummary::EdgeTy> &Calls) { |
9559 | assert(Lex.getKind() == lltok::kw_calls); |
9560 | Lex.Lex(); |
9561 | |
9562 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in calls" ) || |
9563 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in calls" )) |
9564 | return true; |
9565 | |
9566 | IdToIndexMapType IdToIndexMap; |
9567 | // parse each call edge |
9568 | do { |
9569 | ValueInfo VI; |
9570 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in call" ) || |
9571 | parseToken(T: lltok::kw_callee, ErrMsg: "expected 'callee' in call" ) || |
9572 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
9573 | return true; |
9574 | |
9575 | LocTy Loc = Lex.getLoc(); |
9576 | unsigned GVId; |
9577 | if (parseGVReference(VI, GVId)) |
9578 | return true; |
9579 | |
9580 | CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown; |
9581 | unsigned RelBF = 0; |
9582 | unsigned HasTailCall = false; |
9583 | |
9584 | // parse optional fields |
9585 | while (EatIfPresent(T: lltok::comma)) { |
9586 | switch (Lex.getKind()) { |
9587 | case lltok::kw_hotness: |
9588 | Lex.Lex(); |
9589 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseHotness(Hotness)) |
9590 | return true; |
9591 | break; |
9592 | case lltok::kw_relbf: |
9593 | Lex.Lex(); |
9594 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseUInt32(Val&: RelBF)) |
9595 | return true; |
9596 | break; |
9597 | case lltok::kw_tail: |
9598 | Lex.Lex(); |
9599 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val&: HasTailCall)) |
9600 | return true; |
9601 | break; |
9602 | default: |
9603 | return error(L: Lex.getLoc(), Msg: "expected hotness, relbf, or tail" ); |
9604 | } |
9605 | } |
9606 | if (Hotness != CalleeInfo::HotnessType::Unknown && RelBF > 0) |
9607 | return tokError(Msg: "Expected only one of hotness or relbf" ); |
9608 | // Keep track of the Call array index needing a forward reference. |
9609 | // We will save the location of the ValueInfo needing an update, but |
9610 | // can only do so once the std::vector is finalized. |
9611 | if (VI.getRef() == FwdVIRef) |
9612 | IdToIndexMap[GVId].push_back(x: std::make_pair(x: Calls.size(), y&: Loc)); |
9613 | Calls.push_back( |
9614 | x: FunctionSummary::EdgeTy{VI, CalleeInfo(Hotness, HasTailCall, RelBF)}); |
9615 | |
9616 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in call" )) |
9617 | return true; |
9618 | } while (EatIfPresent(T: lltok::comma)); |
9619 | |
9620 | // Now that the Calls vector is finalized, it is safe to save the locations |
9621 | // of any forward GV references that need updating later. |
9622 | for (auto I : IdToIndexMap) { |
9623 | auto &Infos = ForwardRefValueInfos[I.first]; |
9624 | for (auto P : I.second) { |
9625 | assert(Calls[P.first].first.getRef() == FwdVIRef && |
9626 | "Forward referenced ValueInfo expected to be empty" ); |
9627 | Infos.emplace_back(args: &Calls[P.first].first, args&: P.second); |
9628 | } |
9629 | } |
9630 | |
9631 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in calls" )) |
9632 | return true; |
9633 | |
9634 | return false; |
9635 | } |
9636 | |
9637 | /// Hotness |
9638 | /// := ('unknown'|'cold'|'none'|'hot'|'critical') |
9639 | bool LLParser::parseHotness(CalleeInfo::HotnessType &Hotness) { |
9640 | switch (Lex.getKind()) { |
9641 | case lltok::kw_unknown: |
9642 | Hotness = CalleeInfo::HotnessType::Unknown; |
9643 | break; |
9644 | case lltok::kw_cold: |
9645 | Hotness = CalleeInfo::HotnessType::Cold; |
9646 | break; |
9647 | case lltok::kw_none: |
9648 | Hotness = CalleeInfo::HotnessType::None; |
9649 | break; |
9650 | case lltok::kw_hot: |
9651 | Hotness = CalleeInfo::HotnessType::Hot; |
9652 | break; |
9653 | case lltok::kw_critical: |
9654 | Hotness = CalleeInfo::HotnessType::Critical; |
9655 | break; |
9656 | default: |
9657 | return error(L: Lex.getLoc(), Msg: "invalid call edge hotness" ); |
9658 | } |
9659 | Lex.Lex(); |
9660 | return false; |
9661 | } |
9662 | |
9663 | /// OptionalVTableFuncs |
9664 | /// := 'vTableFuncs' ':' '(' VTableFunc [',' VTableFunc]* ')' |
9665 | /// VTableFunc ::= '(' 'virtFunc' ':' GVReference ',' 'offset' ':' UInt64 ')' |
9666 | bool LLParser::parseOptionalVTableFuncs(VTableFuncList &VTableFuncs) { |
9667 | assert(Lex.getKind() == lltok::kw_vTableFuncs); |
9668 | Lex.Lex(); |
9669 | |
9670 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in vTableFuncs" ) || |
9671 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in vTableFuncs" )) |
9672 | return true; |
9673 | |
9674 | IdToIndexMapType IdToIndexMap; |
9675 | // parse each virtual function pair |
9676 | do { |
9677 | ValueInfo VI; |
9678 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in vTableFunc" ) || |
9679 | parseToken(T: lltok::kw_virtFunc, ErrMsg: "expected 'callee' in vTableFunc" ) || |
9680 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
9681 | return true; |
9682 | |
9683 | LocTy Loc = Lex.getLoc(); |
9684 | unsigned GVId; |
9685 | if (parseGVReference(VI, GVId)) |
9686 | return true; |
9687 | |
9688 | uint64_t Offset; |
9689 | if (parseToken(T: lltok::comma, ErrMsg: "expected comma" ) || |
9690 | parseToken(T: lltok::kw_offset, ErrMsg: "expected offset" ) || |
9691 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseUInt64(Val&: Offset)) |
9692 | return true; |
9693 | |
9694 | // Keep track of the VTableFuncs array index needing a forward reference. |
9695 | // We will save the location of the ValueInfo needing an update, but |
9696 | // can only do so once the std::vector is finalized. |
9697 | if (VI == EmptyVI) |
9698 | IdToIndexMap[GVId].push_back(x: std::make_pair(x: VTableFuncs.size(), y&: Loc)); |
9699 | VTableFuncs.push_back(x: {VI, Offset}); |
9700 | |
9701 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in vTableFunc" )) |
9702 | return true; |
9703 | } while (EatIfPresent(T: lltok::comma)); |
9704 | |
9705 | // Now that the VTableFuncs vector is finalized, it is safe to save the |
9706 | // locations of any forward GV references that need updating later. |
9707 | for (auto I : IdToIndexMap) { |
9708 | auto &Infos = ForwardRefValueInfos[I.first]; |
9709 | for (auto P : I.second) { |
9710 | assert(VTableFuncs[P.first].FuncVI == EmptyVI && |
9711 | "Forward referenced ValueInfo expected to be empty" ); |
9712 | Infos.emplace_back(args: &VTableFuncs[P.first].FuncVI, args&: P.second); |
9713 | } |
9714 | } |
9715 | |
9716 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in vTableFuncs" )) |
9717 | return true; |
9718 | |
9719 | return false; |
9720 | } |
9721 | |
9722 | /// ParamNo := 'param' ':' UInt64 |
9723 | bool LLParser::parseParamNo(uint64_t &ParamNo) { |
9724 | if (parseToken(T: lltok::kw_param, ErrMsg: "expected 'param' here" ) || |
9725 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || parseUInt64(Val&: ParamNo)) |
9726 | return true; |
9727 | return false; |
9728 | } |
9729 | |
9730 | /// ParamAccessOffset := 'offset' ':' '[' APSINTVAL ',' APSINTVAL ']' |
9731 | bool LLParser::parseParamAccessOffset(ConstantRange &Range) { |
9732 | APSInt Lower; |
9733 | APSInt Upper; |
9734 | auto ParseAPSInt = [&](APSInt &Val) { |
9735 | if (Lex.getKind() != lltok::APSInt) |
9736 | return tokError(Msg: "expected integer" ); |
9737 | Val = Lex.getAPSIntVal(); |
9738 | Val = Val.extOrTrunc(width: FunctionSummary::ParamAccess::RangeWidth); |
9739 | Val.setIsSigned(true); |
9740 | Lex.Lex(); |
9741 | return false; |
9742 | }; |
9743 | if (parseToken(T: lltok::kw_offset, ErrMsg: "expected 'offset' here" ) || |
9744 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9745 | parseToken(T: lltok::lsquare, ErrMsg: "expected '[' here" ) || ParseAPSInt(Lower) || |
9746 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || ParseAPSInt(Upper) || |
9747 | parseToken(T: lltok::rsquare, ErrMsg: "expected ']' here" )) |
9748 | return true; |
9749 | |
9750 | ++Upper; |
9751 | Range = |
9752 | (Lower == Upper && !Lower.isMaxValue()) |
9753 | ? ConstantRange::getEmpty(BitWidth: FunctionSummary::ParamAccess::RangeWidth) |
9754 | : ConstantRange(Lower, Upper); |
9755 | |
9756 | return false; |
9757 | } |
9758 | |
9759 | /// ParamAccessCall |
9760 | /// := '(' 'callee' ':' GVReference ',' ParamNo ',' ParamAccessOffset ')' |
9761 | bool LLParser::parseParamAccessCall(FunctionSummary::ParamAccess::Call &Call, |
9762 | IdLocListType &IdLocList) { |
9763 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
9764 | parseToken(T: lltok::kw_callee, ErrMsg: "expected 'callee' here" ) || |
9765 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" )) |
9766 | return true; |
9767 | |
9768 | unsigned GVId; |
9769 | ValueInfo VI; |
9770 | LocTy Loc = Lex.getLoc(); |
9771 | if (parseGVReference(VI, GVId)) |
9772 | return true; |
9773 | |
9774 | Call.Callee = VI; |
9775 | IdLocList.emplace_back(args&: GVId, args&: Loc); |
9776 | |
9777 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
9778 | parseParamNo(ParamNo&: Call.ParamNo) || |
9779 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
9780 | parseParamAccessOffset(Range&: Call.Offsets)) |
9781 | return true; |
9782 | |
9783 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
9784 | return true; |
9785 | |
9786 | return false; |
9787 | } |
9788 | |
9789 | /// ParamAccess |
9790 | /// := '(' ParamNo ',' ParamAccessOffset [',' OptionalParamAccessCalls]? ')' |
9791 | /// OptionalParamAccessCalls := '(' Call [',' Call]* ')' |
9792 | bool LLParser::parseParamAccess(FunctionSummary::ParamAccess &Param, |
9793 | IdLocListType &IdLocList) { |
9794 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
9795 | parseParamNo(ParamNo&: Param.ParamNo) || |
9796 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
9797 | parseParamAccessOffset(Range&: Param.Use)) |
9798 | return true; |
9799 | |
9800 | if (EatIfPresent(T: lltok::comma)) { |
9801 | if (parseToken(T: lltok::kw_calls, ErrMsg: "expected 'calls' here" ) || |
9802 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9803 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
9804 | return true; |
9805 | do { |
9806 | FunctionSummary::ParamAccess::Call Call; |
9807 | if (parseParamAccessCall(Call, IdLocList)) |
9808 | return true; |
9809 | Param.Calls.push_back(x: Call); |
9810 | } while (EatIfPresent(T: lltok::comma)); |
9811 | |
9812 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
9813 | return true; |
9814 | } |
9815 | |
9816 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
9817 | return true; |
9818 | |
9819 | return false; |
9820 | } |
9821 | |
9822 | /// OptionalParamAccesses |
9823 | /// := 'params' ':' '(' ParamAccess [',' ParamAccess]* ')' |
9824 | bool LLParser::parseOptionalParamAccesses( |
9825 | std::vector<FunctionSummary::ParamAccess> &Params) { |
9826 | assert(Lex.getKind() == lltok::kw_params); |
9827 | Lex.Lex(); |
9828 | |
9829 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9830 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
9831 | return true; |
9832 | |
9833 | IdLocListType VContexts; |
9834 | size_t CallsNum = 0; |
9835 | do { |
9836 | FunctionSummary::ParamAccess ParamAccess; |
9837 | if (parseParamAccess(Param&: ParamAccess, IdLocList&: VContexts)) |
9838 | return true; |
9839 | CallsNum += ParamAccess.Calls.size(); |
9840 | assert(VContexts.size() == CallsNum); |
9841 | (void)CallsNum; |
9842 | Params.emplace_back(args: std::move(ParamAccess)); |
9843 | } while (EatIfPresent(T: lltok::comma)); |
9844 | |
9845 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
9846 | return true; |
9847 | |
9848 | // Now that the Params is finalized, it is safe to save the locations |
9849 | // of any forward GV references that need updating later. |
9850 | IdLocListType::const_iterator ItContext = VContexts.begin(); |
9851 | for (auto &PA : Params) { |
9852 | for (auto &C : PA.Calls) { |
9853 | if (C.Callee.getRef() == FwdVIRef) |
9854 | ForwardRefValueInfos[ItContext->first].emplace_back(args: &C.Callee, |
9855 | args: ItContext->second); |
9856 | ++ItContext; |
9857 | } |
9858 | } |
9859 | assert(ItContext == VContexts.end()); |
9860 | |
9861 | return false; |
9862 | } |
9863 | |
9864 | /// OptionalRefs |
9865 | /// := 'refs' ':' '(' GVReference [',' GVReference]* ')' |
9866 | bool LLParser::parseOptionalRefs(std::vector<ValueInfo> &Refs) { |
9867 | assert(Lex.getKind() == lltok::kw_refs); |
9868 | Lex.Lex(); |
9869 | |
9870 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in refs" ) || |
9871 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in refs" )) |
9872 | return true; |
9873 | |
9874 | struct ValueContext { |
9875 | ValueInfo VI; |
9876 | unsigned GVId; |
9877 | LocTy Loc; |
9878 | }; |
9879 | std::vector<ValueContext> VContexts; |
9880 | // parse each ref edge |
9881 | do { |
9882 | ValueContext VC; |
9883 | VC.Loc = Lex.getLoc(); |
9884 | if (parseGVReference(VI&: VC.VI, GVId&: VC.GVId)) |
9885 | return true; |
9886 | VContexts.push_back(x: VC); |
9887 | } while (EatIfPresent(T: lltok::comma)); |
9888 | |
9889 | // Sort value contexts so that ones with writeonly |
9890 | // and readonly ValueInfo are at the end of VContexts vector. |
9891 | // See FunctionSummary::specialRefCounts() |
9892 | llvm::sort(C&: VContexts, Comp: [](const ValueContext &VC1, const ValueContext &VC2) { |
9893 | return VC1.VI.getAccessSpecifier() < VC2.VI.getAccessSpecifier(); |
9894 | }); |
9895 | |
9896 | IdToIndexMapType IdToIndexMap; |
9897 | for (auto &VC : VContexts) { |
9898 | // Keep track of the Refs array index needing a forward reference. |
9899 | // We will save the location of the ValueInfo needing an update, but |
9900 | // can only do so once the std::vector is finalized. |
9901 | if (VC.VI.getRef() == FwdVIRef) |
9902 | IdToIndexMap[VC.GVId].push_back(x: std::make_pair(x: Refs.size(), y&: VC.Loc)); |
9903 | Refs.push_back(x: VC.VI); |
9904 | } |
9905 | |
9906 | // Now that the Refs vector is finalized, it is safe to save the locations |
9907 | // of any forward GV references that need updating later. |
9908 | for (auto I : IdToIndexMap) { |
9909 | auto &Infos = ForwardRefValueInfos[I.first]; |
9910 | for (auto P : I.second) { |
9911 | assert(Refs[P.first].getRef() == FwdVIRef && |
9912 | "Forward referenced ValueInfo expected to be empty" ); |
9913 | Infos.emplace_back(args: &Refs[P.first], args&: P.second); |
9914 | } |
9915 | } |
9916 | |
9917 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in refs" )) |
9918 | return true; |
9919 | |
9920 | return false; |
9921 | } |
9922 | |
9923 | /// OptionalTypeIdInfo |
9924 | /// := 'typeidinfo' ':' '(' [',' TypeTests]? [',' TypeTestAssumeVCalls]? |
9925 | /// [',' TypeCheckedLoadVCalls]? [',' TypeTestAssumeConstVCalls]? |
9926 | /// [',' TypeCheckedLoadConstVCalls]? ')' |
9927 | bool LLParser::parseOptionalTypeIdInfo( |
9928 | FunctionSummary::TypeIdInfo &TypeIdInfo) { |
9929 | assert(Lex.getKind() == lltok::kw_typeIdInfo); |
9930 | Lex.Lex(); |
9931 | |
9932 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9933 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in typeIdInfo" )) |
9934 | return true; |
9935 | |
9936 | do { |
9937 | switch (Lex.getKind()) { |
9938 | case lltok::kw_typeTests: |
9939 | if (parseTypeTests(TypeTests&: TypeIdInfo.TypeTests)) |
9940 | return true; |
9941 | break; |
9942 | case lltok::kw_typeTestAssumeVCalls: |
9943 | if (parseVFuncIdList(Kind: lltok::kw_typeTestAssumeVCalls, |
9944 | VFuncIdList&: TypeIdInfo.TypeTestAssumeVCalls)) |
9945 | return true; |
9946 | break; |
9947 | case lltok::kw_typeCheckedLoadVCalls: |
9948 | if (parseVFuncIdList(Kind: lltok::kw_typeCheckedLoadVCalls, |
9949 | VFuncIdList&: TypeIdInfo.TypeCheckedLoadVCalls)) |
9950 | return true; |
9951 | break; |
9952 | case lltok::kw_typeTestAssumeConstVCalls: |
9953 | if (parseConstVCallList(Kind: lltok::kw_typeTestAssumeConstVCalls, |
9954 | ConstVCallList&: TypeIdInfo.TypeTestAssumeConstVCalls)) |
9955 | return true; |
9956 | break; |
9957 | case lltok::kw_typeCheckedLoadConstVCalls: |
9958 | if (parseConstVCallList(Kind: lltok::kw_typeCheckedLoadConstVCalls, |
9959 | ConstVCallList&: TypeIdInfo.TypeCheckedLoadConstVCalls)) |
9960 | return true; |
9961 | break; |
9962 | default: |
9963 | return error(L: Lex.getLoc(), Msg: "invalid typeIdInfo list type" ); |
9964 | } |
9965 | } while (EatIfPresent(T: lltok::comma)); |
9966 | |
9967 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in typeIdInfo" )) |
9968 | return true; |
9969 | |
9970 | return false; |
9971 | } |
9972 | |
9973 | /// TypeTests |
9974 | /// ::= 'typeTests' ':' '(' (SummaryID | UInt64) |
9975 | /// [',' (SummaryID | UInt64)]* ')' |
9976 | bool LLParser::parseTypeTests(std::vector<GlobalValue::GUID> &TypeTests) { |
9977 | assert(Lex.getKind() == lltok::kw_typeTests); |
9978 | Lex.Lex(); |
9979 | |
9980 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
9981 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in typeIdInfo" )) |
9982 | return true; |
9983 | |
9984 | IdToIndexMapType IdToIndexMap; |
9985 | do { |
9986 | GlobalValue::GUID GUID = 0; |
9987 | if (Lex.getKind() == lltok::SummaryID) { |
9988 | unsigned ID = Lex.getUIntVal(); |
9989 | LocTy Loc = Lex.getLoc(); |
9990 | // Keep track of the TypeTests array index needing a forward reference. |
9991 | // We will save the location of the GUID needing an update, but |
9992 | // can only do so once the std::vector is finalized. |
9993 | IdToIndexMap[ID].push_back(x: std::make_pair(x: TypeTests.size(), y&: Loc)); |
9994 | Lex.Lex(); |
9995 | } else if (parseUInt64(Val&: GUID)) |
9996 | return true; |
9997 | TypeTests.push_back(x: GUID); |
9998 | } while (EatIfPresent(T: lltok::comma)); |
9999 | |
10000 | // Now that the TypeTests vector is finalized, it is safe to save the |
10001 | // locations of any forward GV references that need updating later. |
10002 | for (auto I : IdToIndexMap) { |
10003 | auto &Ids = ForwardRefTypeIds[I.first]; |
10004 | for (auto P : I.second) { |
10005 | assert(TypeTests[P.first] == 0 && |
10006 | "Forward referenced type id GUID expected to be 0" ); |
10007 | Ids.emplace_back(args: &TypeTests[P.first], args&: P.second); |
10008 | } |
10009 | } |
10010 | |
10011 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in typeIdInfo" )) |
10012 | return true; |
10013 | |
10014 | return false; |
10015 | } |
10016 | |
10017 | /// VFuncIdList |
10018 | /// ::= Kind ':' '(' VFuncId [',' VFuncId]* ')' |
10019 | bool LLParser::parseVFuncIdList( |
10020 | lltok::Kind Kind, std::vector<FunctionSummary::VFuncId> &VFuncIdList) { |
10021 | assert(Lex.getKind() == Kind); |
10022 | Lex.Lex(); |
10023 | |
10024 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
10025 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
10026 | return true; |
10027 | |
10028 | IdToIndexMapType IdToIndexMap; |
10029 | do { |
10030 | FunctionSummary::VFuncId VFuncId; |
10031 | if (parseVFuncId(VFuncId, IdToIndexMap, Index: VFuncIdList.size())) |
10032 | return true; |
10033 | VFuncIdList.push_back(x: VFuncId); |
10034 | } while (EatIfPresent(T: lltok::comma)); |
10035 | |
10036 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
10037 | return true; |
10038 | |
10039 | // Now that the VFuncIdList vector is finalized, it is safe to save the |
10040 | // locations of any forward GV references that need updating later. |
10041 | for (auto I : IdToIndexMap) { |
10042 | auto &Ids = ForwardRefTypeIds[I.first]; |
10043 | for (auto P : I.second) { |
10044 | assert(VFuncIdList[P.first].GUID == 0 && |
10045 | "Forward referenced type id GUID expected to be 0" ); |
10046 | Ids.emplace_back(args: &VFuncIdList[P.first].GUID, args&: P.second); |
10047 | } |
10048 | } |
10049 | |
10050 | return false; |
10051 | } |
10052 | |
10053 | /// ConstVCallList |
10054 | /// ::= Kind ':' '(' ConstVCall [',' ConstVCall]* ')' |
10055 | bool LLParser::parseConstVCallList( |
10056 | lltok::Kind Kind, |
10057 | std::vector<FunctionSummary::ConstVCall> &ConstVCallList) { |
10058 | assert(Lex.getKind() == Kind); |
10059 | Lex.Lex(); |
10060 | |
10061 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
10062 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
10063 | return true; |
10064 | |
10065 | IdToIndexMapType IdToIndexMap; |
10066 | do { |
10067 | FunctionSummary::ConstVCall ConstVCall; |
10068 | if (parseConstVCall(ConstVCall, IdToIndexMap, Index: ConstVCallList.size())) |
10069 | return true; |
10070 | ConstVCallList.push_back(x: ConstVCall); |
10071 | } while (EatIfPresent(T: lltok::comma)); |
10072 | |
10073 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
10074 | return true; |
10075 | |
10076 | // Now that the ConstVCallList vector is finalized, it is safe to save the |
10077 | // locations of any forward GV references that need updating later. |
10078 | for (auto I : IdToIndexMap) { |
10079 | auto &Ids = ForwardRefTypeIds[I.first]; |
10080 | for (auto P : I.second) { |
10081 | assert(ConstVCallList[P.first].VFunc.GUID == 0 && |
10082 | "Forward referenced type id GUID expected to be 0" ); |
10083 | Ids.emplace_back(args: &ConstVCallList[P.first].VFunc.GUID, args&: P.second); |
10084 | } |
10085 | } |
10086 | |
10087 | return false; |
10088 | } |
10089 | |
10090 | /// ConstVCall |
10091 | /// ::= '(' VFuncId ',' Args ')' |
10092 | bool LLParser::parseConstVCall(FunctionSummary::ConstVCall &ConstVCall, |
10093 | IdToIndexMapType &IdToIndexMap, unsigned Index) { |
10094 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
10095 | parseVFuncId(VFuncId&: ConstVCall.VFunc, IdToIndexMap, Index)) |
10096 | return true; |
10097 | |
10098 | if (EatIfPresent(T: lltok::comma)) |
10099 | if (parseArgs(Args&: ConstVCall.Args)) |
10100 | return true; |
10101 | |
10102 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
10103 | return true; |
10104 | |
10105 | return false; |
10106 | } |
10107 | |
10108 | /// VFuncId |
10109 | /// ::= 'vFuncId' ':' '(' (SummaryID | 'guid' ':' UInt64) ',' |
10110 | /// 'offset' ':' UInt64 ')' |
10111 | bool LLParser::parseVFuncId(FunctionSummary::VFuncId &VFuncId, |
10112 | IdToIndexMapType &IdToIndexMap, unsigned Index) { |
10113 | assert(Lex.getKind() == lltok::kw_vFuncId); |
10114 | Lex.Lex(); |
10115 | |
10116 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
10117 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
10118 | return true; |
10119 | |
10120 | if (Lex.getKind() == lltok::SummaryID) { |
10121 | VFuncId.GUID = 0; |
10122 | unsigned ID = Lex.getUIntVal(); |
10123 | LocTy Loc = Lex.getLoc(); |
10124 | // Keep track of the array index needing a forward reference. |
10125 | // We will save the location of the GUID needing an update, but |
10126 | // can only do so once the caller's std::vector is finalized. |
10127 | IdToIndexMap[ID].push_back(x: std::make_pair(x&: Index, y&: Loc)); |
10128 | Lex.Lex(); |
10129 | } else if (parseToken(T: lltok::kw_guid, ErrMsg: "expected 'guid' here" ) || |
10130 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
10131 | parseUInt64(Val&: VFuncId.GUID)) |
10132 | return true; |
10133 | |
10134 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
10135 | parseToken(T: lltok::kw_offset, ErrMsg: "expected 'offset' here" ) || |
10136 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
10137 | parseUInt64(Val&: VFuncId.Offset) || |
10138 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
10139 | return true; |
10140 | |
10141 | return false; |
10142 | } |
10143 | |
10144 | /// GVFlags |
10145 | /// ::= 'flags' ':' '(' 'linkage' ':' OptionalLinkageAux ',' |
10146 | /// 'visibility' ':' Flag 'notEligibleToImport' ':' Flag ',' |
10147 | /// 'live' ':' Flag ',' 'dsoLocal' ':' Flag ',' |
10148 | /// 'canAutoHide' ':' Flag ',' ')' |
10149 | bool LLParser::parseGVFlags(GlobalValueSummary::GVFlags &GVFlags) { |
10150 | assert(Lex.getKind() == lltok::kw_flags); |
10151 | Lex.Lex(); |
10152 | |
10153 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
10154 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
10155 | return true; |
10156 | |
10157 | do { |
10158 | unsigned Flag = 0; |
10159 | switch (Lex.getKind()) { |
10160 | case lltok::kw_linkage: |
10161 | Lex.Lex(); |
10162 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
10163 | return true; |
10164 | bool HasLinkage; |
10165 | GVFlags.Linkage = parseOptionalLinkageAux(Kind: Lex.getKind(), HasLinkage); |
10166 | assert(HasLinkage && "Linkage not optional in summary entry" ); |
10167 | Lex.Lex(); |
10168 | break; |
10169 | case lltok::kw_visibility: |
10170 | Lex.Lex(); |
10171 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
10172 | return true; |
10173 | parseOptionalVisibility(Res&: Flag); |
10174 | GVFlags.Visibility = Flag; |
10175 | break; |
10176 | case lltok::kw_notEligibleToImport: |
10177 | Lex.Lex(); |
10178 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val&: Flag)) |
10179 | return true; |
10180 | GVFlags.NotEligibleToImport = Flag; |
10181 | break; |
10182 | case lltok::kw_live: |
10183 | Lex.Lex(); |
10184 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val&: Flag)) |
10185 | return true; |
10186 | GVFlags.Live = Flag; |
10187 | break; |
10188 | case lltok::kw_dsoLocal: |
10189 | Lex.Lex(); |
10190 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val&: Flag)) |
10191 | return true; |
10192 | GVFlags.DSOLocal = Flag; |
10193 | break; |
10194 | case lltok::kw_canAutoHide: |
10195 | Lex.Lex(); |
10196 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val&: Flag)) |
10197 | return true; |
10198 | GVFlags.CanAutoHide = Flag; |
10199 | break; |
10200 | case lltok::kw_importType: |
10201 | Lex.Lex(); |
10202 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
10203 | return true; |
10204 | GlobalValueSummary::ImportKind IK; |
10205 | if (parseOptionalImportType(Kind: Lex.getKind(), Res&: IK)) |
10206 | return true; |
10207 | GVFlags.ImportType = static_cast<unsigned>(IK); |
10208 | Lex.Lex(); |
10209 | break; |
10210 | default: |
10211 | return error(L: Lex.getLoc(), Msg: "expected gv flag type" ); |
10212 | } |
10213 | } while (EatIfPresent(T: lltok::comma)); |
10214 | |
10215 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
10216 | return true; |
10217 | |
10218 | return false; |
10219 | } |
10220 | |
10221 | /// GVarFlags |
10222 | /// ::= 'varFlags' ':' '(' 'readonly' ':' Flag |
10223 | /// ',' 'writeonly' ':' Flag |
10224 | /// ',' 'constant' ':' Flag ')' |
10225 | bool LLParser::parseGVarFlags(GlobalVarSummary::GVarFlags &GVarFlags) { |
10226 | assert(Lex.getKind() == lltok::kw_varFlags); |
10227 | Lex.Lex(); |
10228 | |
10229 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
10230 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
10231 | return true; |
10232 | |
10233 | auto ParseRest = [this](unsigned int &Val) { |
10234 | Lex.Lex(); |
10235 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
10236 | return true; |
10237 | return parseFlag(Val); |
10238 | }; |
10239 | |
10240 | do { |
10241 | unsigned Flag = 0; |
10242 | switch (Lex.getKind()) { |
10243 | case lltok::kw_readonly: |
10244 | if (ParseRest(Flag)) |
10245 | return true; |
10246 | GVarFlags.MaybeReadOnly = Flag; |
10247 | break; |
10248 | case lltok::kw_writeonly: |
10249 | if (ParseRest(Flag)) |
10250 | return true; |
10251 | GVarFlags.MaybeWriteOnly = Flag; |
10252 | break; |
10253 | case lltok::kw_constant: |
10254 | if (ParseRest(Flag)) |
10255 | return true; |
10256 | GVarFlags.Constant = Flag; |
10257 | break; |
10258 | case lltok::kw_vcall_visibility: |
10259 | if (ParseRest(Flag)) |
10260 | return true; |
10261 | GVarFlags.VCallVisibility = Flag; |
10262 | break; |
10263 | default: |
10264 | return error(L: Lex.getLoc(), Msg: "expected gvar flag type" ); |
10265 | } |
10266 | } while (EatIfPresent(T: lltok::comma)); |
10267 | return parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" ); |
10268 | } |
10269 | |
10270 | /// ModuleReference |
10271 | /// ::= 'module' ':' UInt |
10272 | bool LLParser::parseModuleReference(StringRef &ModulePath) { |
10273 | // parse module id. |
10274 | if (parseToken(T: lltok::kw_module, ErrMsg: "expected 'module' here" ) || |
10275 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
10276 | parseToken(T: lltok::SummaryID, ErrMsg: "expected module ID" )) |
10277 | return true; |
10278 | |
10279 | unsigned ModuleID = Lex.getUIntVal(); |
10280 | auto I = ModuleIdMap.find(x: ModuleID); |
10281 | // We should have already parsed all module IDs |
10282 | assert(I != ModuleIdMap.end()); |
10283 | ModulePath = I->second; |
10284 | return false; |
10285 | } |
10286 | |
10287 | /// GVReference |
10288 | /// ::= SummaryID |
10289 | bool LLParser::parseGVReference(ValueInfo &VI, unsigned &GVId) { |
10290 | bool WriteOnly = false, ReadOnly = EatIfPresent(T: lltok::kw_readonly); |
10291 | if (!ReadOnly) |
10292 | WriteOnly = EatIfPresent(lltok::T: kw_writeonly); |
10293 | if (parseToken(T: lltok::SummaryID, ErrMsg: "expected GV ID" )) |
10294 | return true; |
10295 | |
10296 | GVId = Lex.getUIntVal(); |
10297 | // Check if we already have a VI for this GV |
10298 | if (GVId < NumberedValueInfos.size() && NumberedValueInfos[GVId]) { |
10299 | assert(NumberedValueInfos[GVId].getRef() != FwdVIRef); |
10300 | VI = NumberedValueInfos[GVId]; |
10301 | } else |
10302 | // We will create a forward reference to the stored location. |
10303 | VI = ValueInfo(false, FwdVIRef); |
10304 | |
10305 | if (ReadOnly) |
10306 | VI.setReadOnly(); |
10307 | if (WriteOnly) |
10308 | VI.setWriteOnly(); |
10309 | return false; |
10310 | } |
10311 | |
10312 | /// OptionalAllocs |
10313 | /// := 'allocs' ':' '(' Alloc [',' Alloc]* ')' |
10314 | /// Alloc ::= '(' 'versions' ':' '(' Version [',' Version]* ')' |
10315 | /// ',' MemProfs ')' |
10316 | /// Version ::= UInt32 |
10317 | bool LLParser::parseOptionalAllocs(std::vector<AllocInfo> &Allocs) { |
10318 | assert(Lex.getKind() == lltok::kw_allocs); |
10319 | Lex.Lex(); |
10320 | |
10321 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in allocs" ) || |
10322 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in allocs" )) |
10323 | return true; |
10324 | |
10325 | // parse each alloc |
10326 | do { |
10327 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in alloc" ) || |
10328 | parseToken(T: lltok::kw_versions, ErrMsg: "expected 'versions' in alloc" ) || |
10329 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || |
10330 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in versions" )) |
10331 | return true; |
10332 | |
10333 | SmallVector<uint8_t> Versions; |
10334 | do { |
10335 | uint8_t V = 0; |
10336 | if (parseAllocType(AllocType&: V)) |
10337 | return true; |
10338 | Versions.push_back(Elt: V); |
10339 | } while (EatIfPresent(T: lltok::comma)); |
10340 | |
10341 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in versions" ) || |
10342 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in alloc" )) |
10343 | return true; |
10344 | |
10345 | std::vector<MIBInfo> MIBs; |
10346 | if (parseMemProfs(MIBs)) |
10347 | return true; |
10348 | |
10349 | Allocs.push_back(x: {Versions, MIBs}); |
10350 | |
10351 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in alloc" )) |
10352 | return true; |
10353 | } while (EatIfPresent(T: lltok::comma)); |
10354 | |
10355 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in allocs" )) |
10356 | return true; |
10357 | |
10358 | return false; |
10359 | } |
10360 | |
10361 | /// MemProfs |
10362 | /// := 'memProf' ':' '(' MemProf [',' MemProf]* ')' |
10363 | /// MemProf ::= '(' 'type' ':' AllocType |
10364 | /// ',' 'stackIds' ':' '(' StackId [',' StackId]* ')' ')' |
10365 | /// StackId ::= UInt64 |
10366 | bool LLParser::parseMemProfs(std::vector<MIBInfo> &MIBs) { |
10367 | assert(Lex.getKind() == lltok::kw_memProf); |
10368 | Lex.Lex(); |
10369 | |
10370 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in memprof" ) || |
10371 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in memprof" )) |
10372 | return true; |
10373 | |
10374 | // parse each MIB |
10375 | do { |
10376 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in memprof" ) || |
10377 | parseToken(T: lltok::kw_type, ErrMsg: "expected 'type' in memprof" ) || |
10378 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
10379 | return true; |
10380 | |
10381 | uint8_t AllocType; |
10382 | if (parseAllocType(AllocType)) |
10383 | return true; |
10384 | |
10385 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' in memprof" ) || |
10386 | parseToken(T: lltok::kw_stackIds, ErrMsg: "expected 'stackIds' in memprof" ) || |
10387 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || |
10388 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in stackIds" )) |
10389 | return true; |
10390 | |
10391 | SmallVector<unsigned> StackIdIndices; |
10392 | do { |
10393 | uint64_t StackId = 0; |
10394 | if (parseUInt64(Val&: StackId)) |
10395 | return true; |
10396 | StackIdIndices.push_back(Elt: Index->addOrGetStackIdIndex(StackId)); |
10397 | } while (EatIfPresent(T: lltok::comma)); |
10398 | |
10399 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in stackIds" )) |
10400 | return true; |
10401 | |
10402 | MIBs.push_back(x: {(AllocationType)AllocType, StackIdIndices}); |
10403 | |
10404 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in memprof" )) |
10405 | return true; |
10406 | } while (EatIfPresent(T: lltok::comma)); |
10407 | |
10408 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in memprof" )) |
10409 | return true; |
10410 | |
10411 | return false; |
10412 | } |
10413 | |
10414 | /// AllocType |
10415 | /// := ('none'|'notcold'|'cold'|'hot') |
10416 | bool LLParser::parseAllocType(uint8_t &AllocType) { |
10417 | switch (Lex.getKind()) { |
10418 | case lltok::kw_none: |
10419 | AllocType = (uint8_t)AllocationType::None; |
10420 | break; |
10421 | case lltok::kw_notcold: |
10422 | AllocType = (uint8_t)AllocationType::NotCold; |
10423 | break; |
10424 | case lltok::kw_cold: |
10425 | AllocType = (uint8_t)AllocationType::Cold; |
10426 | break; |
10427 | case lltok::kw_hot: |
10428 | AllocType = (uint8_t)AllocationType::Hot; |
10429 | break; |
10430 | default: |
10431 | return error(L: Lex.getLoc(), Msg: "invalid alloc type" ); |
10432 | } |
10433 | Lex.Lex(); |
10434 | return false; |
10435 | } |
10436 | |
10437 | /// OptionalCallsites |
10438 | /// := 'callsites' ':' '(' Callsite [',' Callsite]* ')' |
10439 | /// Callsite ::= '(' 'callee' ':' GVReference |
10440 | /// ',' 'clones' ':' '(' Version [',' Version]* ')' |
10441 | /// ',' 'stackIds' ':' '(' StackId [',' StackId]* ')' ')' |
10442 | /// Version ::= UInt32 |
10443 | /// StackId ::= UInt64 |
10444 | bool LLParser::parseOptionalCallsites(std::vector<CallsiteInfo> &Callsites) { |
10445 | assert(Lex.getKind() == lltok::kw_callsites); |
10446 | Lex.Lex(); |
10447 | |
10448 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in callsites" ) || |
10449 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in callsites" )) |
10450 | return true; |
10451 | |
10452 | IdToIndexMapType IdToIndexMap; |
10453 | // parse each callsite |
10454 | do { |
10455 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in callsite" ) || |
10456 | parseToken(T: lltok::kw_callee, ErrMsg: "expected 'callee' in callsite" ) || |
10457 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
10458 | return true; |
10459 | |
10460 | ValueInfo VI; |
10461 | unsigned GVId = 0; |
10462 | LocTy Loc = Lex.getLoc(); |
10463 | if (!EatIfPresent(T: lltok::kw_null)) { |
10464 | if (parseGVReference(VI, GVId)) |
10465 | return true; |
10466 | } |
10467 | |
10468 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' in callsite" ) || |
10469 | parseToken(T: lltok::kw_clones, ErrMsg: "expected 'clones' in callsite" ) || |
10470 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || |
10471 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in clones" )) |
10472 | return true; |
10473 | |
10474 | SmallVector<unsigned> Clones; |
10475 | do { |
10476 | unsigned V = 0; |
10477 | if (parseUInt32(Val&: V)) |
10478 | return true; |
10479 | Clones.push_back(Elt: V); |
10480 | } while (EatIfPresent(T: lltok::comma)); |
10481 | |
10482 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in clones" ) || |
10483 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in callsite" ) || |
10484 | parseToken(T: lltok::kw_stackIds, ErrMsg: "expected 'stackIds' in callsite" ) || |
10485 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || |
10486 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in stackIds" )) |
10487 | return true; |
10488 | |
10489 | SmallVector<unsigned> StackIdIndices; |
10490 | do { |
10491 | uint64_t StackId = 0; |
10492 | if (parseUInt64(Val&: StackId)) |
10493 | return true; |
10494 | StackIdIndices.push_back(Elt: Index->addOrGetStackIdIndex(StackId)); |
10495 | } while (EatIfPresent(T: lltok::comma)); |
10496 | |
10497 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in stackIds" )) |
10498 | return true; |
10499 | |
10500 | // Keep track of the Callsites array index needing a forward reference. |
10501 | // We will save the location of the ValueInfo needing an update, but |
10502 | // can only do so once the SmallVector is finalized. |
10503 | if (VI.getRef() == FwdVIRef) |
10504 | IdToIndexMap[GVId].push_back(x: std::make_pair(x: Callsites.size(), y&: Loc)); |
10505 | Callsites.push_back(x: {VI, Clones, StackIdIndices}); |
10506 | |
10507 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in callsite" )) |
10508 | return true; |
10509 | } while (EatIfPresent(T: lltok::comma)); |
10510 | |
10511 | // Now that the Callsites vector is finalized, it is safe to save the |
10512 | // locations of any forward GV references that need updating later. |
10513 | for (auto I : IdToIndexMap) { |
10514 | auto &Infos = ForwardRefValueInfos[I.first]; |
10515 | for (auto P : I.second) { |
10516 | assert(Callsites[P.first].Callee.getRef() == FwdVIRef && |
10517 | "Forward referenced ValueInfo expected to be empty" ); |
10518 | Infos.emplace_back(args: &Callsites[P.first].Callee, args&: P.second); |
10519 | } |
10520 | } |
10521 | |
10522 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in callsites" )) |
10523 | return true; |
10524 | |
10525 | return false; |
10526 | } |
10527 | |