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