1//===-- MCTargetAsmParser.cpp - Target Assembly Parser --------------------===//
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/MCTargetAsmParser.h"
10#include "llvm/MC/MCContext.h"
11#include "llvm/MC/MCObjectStreamer.h"
12#include "llvm/MC/MCRegister.h"
13
14using namespace llvm;
15
16MCTargetAsmParser::MCTargetAsmParser(const MCSubtargetInfo &STI,
17 const MCInstrInfo &MII)
18 : STI(&STI), MII(MII) {}
19
20MCTargetAsmParser::~MCTargetAsmParser() = default;
21
22MCSubtargetInfo &MCTargetAsmParser::copySTI() {
23 MCSubtargetInfo &STICopy = getContext().getSubtargetCopy(STI: getSTI());
24 STI = &STICopy;
25 // The returned STI will likely be modified. Create a new fragment to prevent
26 // mixing STI values within a fragment.
27 auto &S = getStreamer();
28 if (S.isObj() && S.getCurrentFragment())
29 static_cast<MCObjectStreamer &>(S).newFragment();
30 return STICopy;
31}
32
33const MCSubtargetInfo &MCTargetAsmParser::getSTI() const {
34 return *STI;
35}
36
37ParseStatus MCTargetAsmParser::parseDirective(AsmToken DirectiveID) {
38 SMLoc StartTokLoc = getTok().getLoc();
39 // Delegate to ParseDirective by default for transition period. Once the
40 // transition is over, this method should just return NoMatch.
41 bool Res = ParseDirective(DirectiveID);
42
43 // Some targets erroneously report success after emitting an error.
44 if (getParser().hasPendingError())
45 return ParseStatus::Failure;
46
47 // ParseDirective returns true if there was an error or if the directive is
48 // not target-specific. Disambiguate the two cases by comparing position of
49 // the lexer before and after calling the method: if no tokens were consumed,
50 // there was no match, otherwise there was a failure.
51 if (!Res)
52 return ParseStatus::Success;
53 if (getTok().getLoc() != StartTokLoc)
54 return ParseStatus::Failure;
55 return ParseStatus::NoMatch;
56}
57
58bool MCTargetAsmParser::areEqualRegs(const MCParsedAsmOperand &Op1,
59 const MCParsedAsmOperand &Op2) const {
60 return Op1.isReg() && Op2.isReg() && Op1.getReg() == Op2.getReg();
61}
62