1 | //===--------------------- CodeEmitter.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 | /// \file |
9 | /// |
10 | /// A utility class used to compute instruction encodings. It buffers encodings |
11 | /// for later usage. It exposes a simple API to compute and get the encodings as |
12 | /// StringRef. |
13 | // |
14 | //===----------------------------------------------------------------------===// |
15 | |
16 | #ifndef LLVM_MCA_CODEEMITTER_H |
17 | #define LLVM_MCA_CODEEMITTER_H |
18 | |
19 | #include "llvm/ADT/ArrayRef.h" |
20 | #include "llvm/ADT/SmallString.h" |
21 | #include "llvm/ADT/StringRef.h" |
22 | #include "llvm/MC/MCAsmBackend.h" |
23 | #include "llvm/MC/MCCodeEmitter.h" |
24 | #include "llvm/MC/MCInst.h" |
25 | #include "llvm/MC/MCSubtargetInfo.h" |
26 | #include "llvm/Support/Compiler.h" |
27 | |
28 | namespace llvm { |
29 | namespace mca { |
30 | |
31 | /// A utility class used to compute instruction encodings for a code region. |
32 | /// |
33 | /// It provides a simple API to compute and return instruction encodings as |
34 | /// strings. Encodings are cached internally for later usage. |
35 | class CodeEmitter { |
36 | const MCSubtargetInfo &STI; |
37 | const MCAsmBackend &MAB; |
38 | const MCCodeEmitter &MCE; |
39 | |
40 | SmallString<256> Code; |
41 | ArrayRef<MCInst> Sequence; |
42 | |
43 | // An EncodingInfo pair stores <base, length> information. Base (i.e. first) |
44 | // is an index to the `Code`. Length (i.e. second) is the encoding size. |
45 | using EncodingInfo = std::pair<unsigned, unsigned>; |
46 | |
47 | // A cache of encodings. |
48 | SmallVector<EncodingInfo, 16> Encodings; |
49 | |
50 | LLVM_ABI EncodingInfo getOrCreateEncodingInfo(unsigned MCID); |
51 | |
52 | public: |
53 | CodeEmitter(const MCSubtargetInfo &ST, const MCAsmBackend &AB, |
54 | const MCCodeEmitter &CE, ArrayRef<MCInst> S) |
55 | : STI(ST), MAB(AB), MCE(CE), Sequence(S), Encodings(S.size()) {} |
56 | |
57 | StringRef getEncoding(unsigned MCID) { |
58 | EncodingInfo EI = getOrCreateEncodingInfo(MCID); |
59 | return StringRef(&Code[EI.first], EI.second); |
60 | } |
61 | }; |
62 | |
63 | } // namespace mca |
64 | } // namespace llvm |
65 | |
66 | #endif // LLVM_MCA_CODEEMITTER_H |
67 | |