xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-mca/Views/InstructionView.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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 
19 namespace llvm {
20 namespace mca {
21 
22 InstructionView::~InstructionView() = default;
23 
24 StringRef
25 InstructionView::printInstructionString(const llvm::MCInst &MCI) const {
26   InstructionString = "";
27   MCIP.printInst(&MCI, 0, "", STI, InstrStream);
28   InstrStream.flush();
29   // Remove any tabs or spaces at the beginning of the instruction.
30   return StringRef(InstructionString).ltrim();
31 }
32 
33 json::Value InstructionView::toJSON() const {
34   json::Array SourceInfo;
35   for (const auto &MCI : getSource()) {
36     StringRef Instruction = printInstructionString(MCI);
37     SourceInfo.push_back(Instruction.str());
38   }
39   return SourceInfo;
40 }
41 
42 } // namespace mca
43 } // namespace llvm
44