1 //===-- llvm/CodeGen/MIRFormatter.h -----------------------------*- 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 //
9 // This file contains the declaration of the MIRFormatter class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CODEGEN_MIRFORMATTER_H
14 #define LLVM_CODEGEN_MIRFORMATTER_H
15 
16 #include "llvm/CodeGen/PseudoSourceValue.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <cstdint>
20 #include <optional>
21 
22 namespace llvm {
23 
24 class MachineFunction;
25 class MachineInstr;
26 class ModuleSlotTracker;
27 struct PerFunctionMIParsingState;
28 class Twine;
29 class Value;
30 
31 /// MIRFormater - Interface to format MIR operand based on target
32 class MIRFormatter {
33 public:
34   typedef function_ref<bool(StringRef::iterator Loc, const Twine &)>
35       ErrorCallbackType;
36 
37   MIRFormatter() = default;
38   virtual ~MIRFormatter() = default;
39 
40   /// Implement target specific printing for machine operand immediate value, so
41   /// that we can have more meaningful mnemonic than a 64-bit integer. Passing
42   /// std::nullopt to OpIdx means the index is unknown.
43   virtual void printImm(raw_ostream &OS, const MachineInstr &MI,
44                         std::optional<unsigned> OpIdx, int64_t Imm) const {
45     OS << Imm;
46   }
47 
48   /// Implement target specific parsing of immediate mnemonics. The mnemonic is
49   /// dot seperated strings.
50   virtual bool parseImmMnemonic(const unsigned OpCode, const unsigned OpIdx,
51                                 StringRef Src, int64_t &Imm,
52                                 ErrorCallbackType ErrorCallback) const {
53     llvm_unreachable("target did not implement parsing MIR immediate mnemonic");
54   }
55 
56   /// Implement target specific printing of target custom pseudo source value.
57   /// Default implementation is not necessarily the correct MIR serialization
58   /// format.
59   virtual void
60   printCustomPseudoSourceValue(raw_ostream &OS, ModuleSlotTracker &MST,
61                                const PseudoSourceValue &PSV) const {
62     PSV.printCustom(OS);
63   }
64 
65   /// Implement target specific parsing of target custom pseudo source value.
66   virtual bool parseCustomPseudoSourceValue(
67       StringRef Src, MachineFunction &MF, PerFunctionMIParsingState &PFS,
68       const PseudoSourceValue *&PSV, ErrorCallbackType ErrorCallback) const {
69     llvm_unreachable(
70         "target did not implement parsing MIR custom pseudo source value");
71   }
72 
73   /// Helper functions to print IR value as MIR serialization format which will
74   /// be useful for target specific printer, e.g. for printing IR value in
75   /// custom pseudo source value.
76   static void printIRValue(raw_ostream &OS, const Value &V,
77                            ModuleSlotTracker &MST);
78 
79   /// Helper functions to parse IR value from MIR serialization format which
80   /// will be useful for target specific parser, e.g. for parsing IR value for
81   /// custom pseudo source value.
82   static bool parseIRValue(StringRef Src, MachineFunction &MF,
83                            PerFunctionMIParsingState &PFS, const Value *&V,
84                            ErrorCallbackType ErrorCallback);
85 };
86 
87 } // end namespace llvm
88 
89 #endif
90