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