Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:16

0001 //===--------------------- CodeEmitter.h ------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 ///
0010 /// A utility class used to compute instruction encodings. It buffers encodings
0011 /// for later usage. It exposes a simple API to compute and get the encodings as
0012 /// StringRef.
0013 //
0014 //===----------------------------------------------------------------------===//
0015 
0016 #ifndef LLVM_MCA_CODEEMITTER_H
0017 #define LLVM_MCA_CODEEMITTER_H
0018 
0019 #include "llvm/ADT/ArrayRef.h"
0020 #include "llvm/ADT/SmallString.h"
0021 #include "llvm/ADT/StringRef.h"
0022 #include "llvm/MC/MCAsmBackend.h"
0023 #include "llvm/MC/MCCodeEmitter.h"
0024 #include "llvm/MC/MCInst.h"
0025 #include "llvm/MC/MCSubtargetInfo.h"
0026 
0027 namespace llvm {
0028 namespace mca {
0029 
0030 /// A utility class used to compute instruction encodings for a code region.
0031 ///
0032 /// It provides a simple API to compute and return instruction encodings as
0033 /// strings. Encodings are cached internally for later usage.
0034 class CodeEmitter {
0035   const MCSubtargetInfo &STI;
0036   const MCAsmBackend &MAB;
0037   const MCCodeEmitter &MCE;
0038 
0039   SmallString<256> Code;
0040   ArrayRef<MCInst> Sequence;
0041 
0042   // An EncodingInfo pair stores <base, length> information.  Base (i.e. first)
0043   // is an index to the `Code`. Length (i.e. second) is the encoding size.
0044   using EncodingInfo = std::pair<unsigned, unsigned>;
0045 
0046   // A cache of encodings.
0047   SmallVector<EncodingInfo, 16> Encodings;
0048 
0049   EncodingInfo getOrCreateEncodingInfo(unsigned MCID);
0050 
0051 public:
0052   CodeEmitter(const MCSubtargetInfo &ST, const MCAsmBackend &AB,
0053               const MCCodeEmitter &CE, ArrayRef<MCInst> S)
0054       : STI(ST), MAB(AB), MCE(CE), Sequence(S), Encodings(S.size()) {}
0055 
0056   StringRef getEncoding(unsigned MCID) {
0057     EncodingInfo EI = getOrCreateEncodingInfo(MCID);
0058     return StringRef(&Code[EI.first], EI.second);
0059   }
0060 };
0061 
0062 } // namespace mca
0063 } // namespace llvm
0064 
0065 #endif // LLVM_MCA_CODEEMITTER_H