1 //===- VarLenCodeEmitterGen.h - CEG for variable-length insts ---*- 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 declare the CodeEmitterGen component for variable-length
10 // instructions. See the .cpp file for more details.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_UTILS_TABLEGEN_VARLENCODEEMITTERGEN_H
15 #define LLVM_UTILS_TABLEGEN_VARLENCODEEMITTERGEN_H
16 
17 #include "llvm/TableGen/Record.h"
18 
19 namespace llvm {
20 
21 struct EncodingSegment {
22   unsigned BitWidth;
23   const Init *Value;
24   StringRef CustomEncoder = "";
25   StringRef CustomDecoder = "";
26 };
27 
28 class VarLenInst {
29   const RecordVal *TheDef;
30   size_t NumBits;
31 
32   // Set if any of the segment is not fixed value.
33   bool HasDynamicSegment;
34 
35   SmallVector<EncodingSegment, 4> Segments;
36 
37   void buildRec(const DagInit *DI);
38 
39 public:
VarLenInst()40   VarLenInst() : TheDef(nullptr), NumBits(0U), HasDynamicSegment(false) {}
41 
42   explicit VarLenInst(const DagInit *DI, const RecordVal *TheDef);
43 
44   /// Number of bits
size()45   size_t size() const { return NumBits; }
46 
47   using const_iterator = decltype(Segments)::const_iterator;
48 
begin()49   const_iterator begin() const { return Segments.begin(); }
end()50   const_iterator end() const { return Segments.end(); }
getNumSegments()51   size_t getNumSegments() const { return Segments.size(); }
52 
isFixedValueOnly()53   bool isFixedValueOnly() const { return !HasDynamicSegment; }
54 };
55 
56 void emitVarLenCodeEmitter(RecordKeeper &R, raw_ostream &OS);
57 
58 } // end namespace llvm
59 #endif
60