1*82d56013Sjoerg //===----------------------- View.cpp ---------------------------*- C++ -*-===//
2*82d56013Sjoerg //
3*82d56013Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*82d56013Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*82d56013Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*82d56013Sjoerg //
7*82d56013Sjoerg //===----------------------------------------------------------------------===//
8*82d56013Sjoerg /// \file
9*82d56013Sjoerg ///
10*82d56013Sjoerg /// This file defines the member functions of the class InstructionView.
11*82d56013Sjoerg ///
12*82d56013Sjoerg //===----------------------------------------------------------------------===//
13*82d56013Sjoerg
14*82d56013Sjoerg #include <sstream>
15*82d56013Sjoerg #include "Views/InstructionView.h"
16*82d56013Sjoerg #include "llvm/MC/MCInst.h"
17*82d56013Sjoerg #include "llvm/MC/MCSubtargetInfo.h"
18*82d56013Sjoerg
19*82d56013Sjoerg namespace llvm {
20*82d56013Sjoerg namespace mca {
21*82d56013Sjoerg
printInstructionString(const llvm::MCInst & MCI) const22*82d56013Sjoerg StringRef InstructionView::printInstructionString(const llvm::MCInst &MCI) const {
23*82d56013Sjoerg InstructionString = "";
24*82d56013Sjoerg MCIP.printInst(&MCI, 0, "", STI, InstrStream);
25*82d56013Sjoerg InstrStream.flush();
26*82d56013Sjoerg // Remove any tabs or spaces at the beginning of the instruction.
27*82d56013Sjoerg return StringRef(InstructionString).ltrim();
28*82d56013Sjoerg }
29*82d56013Sjoerg
toJSON() const30*82d56013Sjoerg json::Value InstructionView::toJSON() const {
31*82d56013Sjoerg json::Object JO;
32*82d56013Sjoerg json::Array SourceInfo;
33*82d56013Sjoerg for (const auto &MCI : getSource()) {
34*82d56013Sjoerg StringRef Instruction = printInstructionString(MCI);
35*82d56013Sjoerg SourceInfo.push_back(Instruction.str());
36*82d56013Sjoerg }
37*82d56013Sjoerg JO.try_emplace("Instructions", std::move(SourceInfo));
38*82d56013Sjoerg
39*82d56013Sjoerg json::Array Resources;
40*82d56013Sjoerg const MCSchedModel &SM = STI.getSchedModel();
41*82d56013Sjoerg for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) {
42*82d56013Sjoerg const MCProcResourceDesc &ProcResource = *SM.getProcResource(I);
43*82d56013Sjoerg unsigned NumUnits = ProcResource.NumUnits;
44*82d56013Sjoerg // Skip groups and invalid resources with zero units.
45*82d56013Sjoerg if (ProcResource.SubUnitsIdxBegin || !NumUnits)
46*82d56013Sjoerg continue;
47*82d56013Sjoerg for (unsigned J = 0; J < NumUnits; ++J) {
48*82d56013Sjoerg std::stringstream ResNameStream;
49*82d56013Sjoerg ResNameStream << ProcResource.Name;
50*82d56013Sjoerg if (NumUnits > 1)
51*82d56013Sjoerg ResNameStream << "." << J;
52*82d56013Sjoerg Resources.push_back(ResNameStream.str());
53*82d56013Sjoerg }
54*82d56013Sjoerg }
55*82d56013Sjoerg JO.try_emplace("Resources", json::Object({{"CPUName", MCPU}, {"Resources", std::move(Resources)}}));
56*82d56013Sjoerg
57*82d56013Sjoerg return JO;
58*82d56013Sjoerg }
59*82d56013Sjoerg } // namespace mca
60*82d56013Sjoerg } // namespace llvm
61