1//===- BitCodes.h - Enum values for the bitstream format --------*- 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 header defines bitstream enum values.
10//
11// The enum values defined in this file should be considered permanent.  If
12// new features are added, they should have values added at the end of the
13// respective lists.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_BITSTREAM_BITCODES_H
18#define LLVM_BITSTREAM_BITCODES_H
19
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringExtras.h"
22#include "llvm/Bitstream/BitCodeEnums.h"
23#include "llvm/Support/DataTypes.h"
24#include "llvm/Support/ErrorHandling.h"
25#include <cassert>
26
27namespace llvm {
28/// BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
29/// This is actually a union of two different things:
30///   1. It could be a literal integer value ("the operand is always 17").
31///   2. It could be an encoding specification ("this operand encoded like so").
32///
33class BitCodeAbbrevOp {
34  uint64_t Val;           // A literal value or data for an encoding.
35  bool IsLiteral : 1;     // Indicate whether this is a literal value or not.
36  unsigned Enc   : 3;     // The encoding to use.
37public:
38  enum Encoding {
39    Fixed = 1,  // A fixed width field, Val specifies number of bits.
40    VBR   = 2,  // A VBR field where Val specifies the width of each chunk.
41    Array = 3,  // A sequence of fields, next field species elt encoding.
42    Char6 = 4,  // A 6-bit fixed field which maps to [a-zA-Z0-9._].
43    Blob  = 5   // 32-bit aligned array of 8-bit characters.
44  };
45
46  static bool isValidEncoding(uint64_t E) {
47    return E >= 1 && E <= 5;
48  }
49
50  explicit BitCodeAbbrevOp(uint64_t V) :  Val(V), IsLiteral(true) {}
51  explicit BitCodeAbbrevOp(Encoding E, uint64_t Data = 0)
52    : Val(Data), IsLiteral(false), Enc(E) {}
53
54  bool isLiteral() const  { return IsLiteral; }
55  bool isEncoding() const { return !IsLiteral; }
56
57  // Accessors for literals.
58  uint64_t getLiteralValue() const { assert(isLiteral()); return Val; }
59
60  // Accessors for encoding info.
61  Encoding getEncoding() const { assert(isEncoding()); return (Encoding)Enc; }
62  uint64_t getEncodingData() const {
63    assert(isEncoding() && hasEncodingData());
64    return Val;
65  }
66
67  bool hasEncodingData() const { return hasEncodingData(getEncoding()); }
68  static bool hasEncodingData(Encoding E) {
69    switch (E) {
70    case Fixed:
71    case VBR:
72      return true;
73    case Array:
74    case Char6:
75    case Blob:
76      return false;
77    }
78    report_fatal_error("Invalid encoding");
79  }
80
81  /// isChar6 - Return true if this character is legal in the Char6 encoding.
82  static bool isChar6(char C) { return isAlnum(C) || C == '.' || C == '_'; }
83  static unsigned EncodeChar6(char C) {
84    if (C >= 'a' && C <= 'z') return C-'a';
85    if (C >= 'A' && C <= 'Z') return C-'A'+26;
86    if (C >= '0' && C <= '9') return C-'0'+26+26;
87    if (C == '.')             return 62;
88    if (C == '_')             return 63;
89    llvm_unreachable("Not a value Char6 character!");
90  }
91
92  static char DecodeChar6(unsigned V) {
93    assert((V & ~63) == 0 && "Not a Char6 encoded character!");
94    return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._"
95        [V];
96  }
97
98};
99
100/// BitCodeAbbrev - This class represents an abbreviation record.  An
101/// abbreviation allows a complex record that has redundancy to be stored in a
102/// specialized format instead of the fully-general, fully-vbr, format.
103class BitCodeAbbrev {
104  SmallVector<BitCodeAbbrevOp, 32> OperandList;
105
106public:
107  BitCodeAbbrev() = default;
108
109  explicit BitCodeAbbrev(std::initializer_list<BitCodeAbbrevOp> OperandList)
110      : OperandList(OperandList) {}
111
112  unsigned getNumOperandInfos() const {
113    return static_cast<unsigned>(OperandList.size());
114  }
115  const BitCodeAbbrevOp &getOperandInfo(unsigned N) const {
116    return OperandList[N];
117  }
118
119  void Add(const BitCodeAbbrevOp &OpInfo) {
120    OperandList.push_back(OpInfo);
121  }
122};
123} // namespace llvm
124
125#endif
126