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 }; 26 27 class VarLenInst { 28 const RecordVal *TheDef; 29 size_t NumBits; 30 31 // Set if any of the segment is not fixed value. 32 bool HasDynamicSegment; 33 34 SmallVector<EncodingSegment, 4> Segments; 35 36 void buildRec(const DagInit *DI); 37 38 StringRef getCustomEncoderName(const Init *EI) const { 39 if (const auto *DI = dyn_cast<DagInit>(EI)) { 40 if (DI->getNumArgs() && isa<StringInit>(DI->getArg(0))) 41 return cast<StringInit>(DI->getArg(0))->getValue(); 42 } 43 return ""; 44 } 45 46 public: 47 VarLenInst() : TheDef(nullptr), NumBits(0U), HasDynamicSegment(false) {} 48 49 explicit VarLenInst(const DagInit *DI, const RecordVal *TheDef); 50 51 /// Number of bits 52 size_t size() const { return NumBits; } 53 54 using const_iterator = decltype(Segments)::const_iterator; 55 56 const_iterator begin() const { return Segments.begin(); } 57 const_iterator end() const { return Segments.end(); } 58 size_t getNumSegments() const { return Segments.size(); } 59 60 bool isFixedValueOnly() const { return !HasDynamicSegment; } 61 }; 62 63 void emitVarLenCodeEmitter(RecordKeeper &R, raw_ostream &OS); 64 65 } // end namespace llvm 66 #endif 67