1//===----------------------- InstructionView.cpp ----------------*- C++ -*-===//
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/// \file
9///
10/// This file defines the member functions of the class InstructionView.
11///
12//===----------------------------------------------------------------------===//
13
14#include "Views/InstructionView.h"
15#include "llvm/MC/MCInst.h"
16#include "llvm/MC/MCInstPrinter.h"
17#include "llvm/MC/MCSubtargetInfo.h"
18
19namespace llvm {
20namespace mca {
21
22InstructionView::~InstructionView() = default;
23
24StringRef
25InstructionView::printInstructionString(const llvm::MCInst &MCI) const {
26 InstructionString = "";
27 MCIP.printInst(MI: &MCI, Address: 0, Annot: "", STI, OS&: InstrStream);
28 // Remove any tabs or spaces at the beginning of the instruction.
29 return StringRef(InstructionString).ltrim();
30}
31
32json::Value InstructionView::toJSON() const {
33 json::Array SourceInfo;
34 for (const auto &MCI : getSource()) {
35 StringRef Instruction = printInstructionString(MCI);
36 SourceInfo.push_back(E: Instruction.str());
37 }
38 return SourceInfo;
39}
40
41} // namespace mca
42} // namespace llvm
43