1 | //===- MCAsmParserExtension.cpp - Asm Parser Hooks ------------------------===// |
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 | #include "llvm/MC/MCParser/MCAsmParserExtension.h" |
10 | #include "llvm/MC/MCContext.h" |
11 | #include "llvm/MC/MCExpr.h" |
12 | #include "llvm/MC/MCParser/AsmLexer.h" |
13 | #include "llvm/MC/MCStreamer.h" |
14 | |
15 | using namespace llvm; |
16 | |
17 | MCAsmParserExtension::MCAsmParserExtension() = default; |
18 | |
19 | MCAsmParserExtension::~MCAsmParserExtension() = default; |
20 | |
21 | void MCAsmParserExtension::Initialize(MCAsmParser &Parser) { |
22 | this->Parser = &Parser; |
23 | } |
24 | |
25 | /// parseDirectiveCGProfile |
26 | /// ::= .cg_profile identifier, identifier, <number> |
27 | bool MCAsmParserExtension::parseDirectiveCGProfile(StringRef, SMLoc) { |
28 | StringRef From; |
29 | SMLoc FromLoc = getLexer().getLoc(); |
30 | if (getParser().parseIdentifier(Res&: From)) |
31 | return TokError(Msg: "expected identifier in directive" ); |
32 | |
33 | if (getLexer().isNot(K: AsmToken::Comma)) |
34 | return TokError(Msg: "expected a comma" ); |
35 | Lex(); |
36 | |
37 | StringRef To; |
38 | SMLoc ToLoc = getLexer().getLoc(); |
39 | if (getParser().parseIdentifier(Res&: To)) |
40 | return TokError(Msg: "expected identifier in directive" ); |
41 | |
42 | if (getLexer().isNot(K: AsmToken::Comma)) |
43 | return TokError(Msg: "expected a comma" ); |
44 | Lex(); |
45 | |
46 | int64_t Count; |
47 | if (getParser().parseIntToken(V&: Count)) |
48 | return true; |
49 | |
50 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
51 | return TokError(Msg: "unexpected token in directive" ); |
52 | |
53 | MCSymbol *FromSym = getContext().getOrCreateSymbol(Name: From); |
54 | MCSymbol *ToSym = getContext().getOrCreateSymbol(Name: To); |
55 | |
56 | getStreamer().emitCGProfileEntry( |
57 | From: MCSymbolRefExpr::create(Symbol: FromSym, Ctx&: getContext(), Loc: FromLoc), |
58 | To: MCSymbolRefExpr::create(Symbol: ToSym, Ctx&: getContext(), Loc: ToLoc), Count); |
59 | return false; |
60 | } |
61 | |
62 | bool MCAsmParserExtension::maybeParseUniqueID(int64_t &UniqueID) { |
63 | AsmLexer &L = getLexer(); |
64 | if (L.isNot(K: AsmToken::Comma)) |
65 | return false; |
66 | Lex(); |
67 | StringRef UniqueStr; |
68 | if (getParser().parseIdentifier(Res&: UniqueStr)) |
69 | return TokError(Msg: "expected identifier" ); |
70 | if (UniqueStr != "unique" ) |
71 | return TokError(Msg: "expected 'unique'" ); |
72 | if (L.isNot(K: AsmToken::Comma)) |
73 | return TokError(Msg: "expected commma" ); |
74 | Lex(); |
75 | if (getParser().parseAbsoluteExpression(Res&: UniqueID)) |
76 | return true; |
77 | if (UniqueID < 0) |
78 | return TokError(Msg: "unique id must be positive" ); |
79 | if (!isUInt<32>(x: UniqueID) || UniqueID == ~0U) |
80 | return TokError(Msg: "unique id is too large" ); |
81 | return false; |
82 | } |
83 | |