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