Coverage Report

Created: 2023-02-22 06:51

/src/hermes/include/hermes/Inst/InstDecode.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 *
4
 * This source code is licensed under the MIT license found in the
5
 * LICENSE file in the root directory of this source tree.
6
 */
7
8
//===----------------------------------------------------------------------===//
9
/// \file
10
/// Utilities for decoding instructions into a universal representation. To be
11
/// used only for debugging.
12
//===----------------------------------------------------------------------===//
13
#ifndef HERMES_INST_INSTDECODE_H
14
#define HERMES_INST_INSTDECODE_H
15
16
#include "hermes/Inst/Inst.h"
17
18
namespace llvh {
19
class raw_ostream;
20
}
21
22
namespace hermes {
23
namespace inst {
24
25
/// An enum for every possible instruction operand type.
26
enum class OperandType : uint8_t {
27
#define DEFINE_OPERAND_TYPE(name, type) name,
28
#include "hermes/BCGen/HBC/BytecodeList.def"
29
};
30
31
0
inline constexpr bool isOperandTypeFloating(OperandType opType) {
32
0
  return opType == OperandType::Double;
33
0
}
34
0
inline constexpr bool isOperandTypeInteger(OperandType opType) {
35
0
  return !isOperandTypeFloating(opType);
36
0
}
37
38
/// The maximum number of operands used by any instruction.
39
constexpr unsigned INST_MAX_OPERANDS = 6;
40
41
/// Metadata describing a single instructions: its size, number of operands
42
/// and the type of each operand.
43
struct InstMetaData {
44
  /// The instruction opcode.
45
  OpCode opCode;
46
  /// Size of the entire instruction in bytes.
47
  uint8_t size;
48
  /// Number of operands.
49
  uint8_t numOperands;
50
  /// The type of each operand.
51
  OperandType operandType[INST_MAX_OPERANDS];
52
};
53
54
/// A union combining all possible types of operand values.
55
union OperandValue {
56
  double floating;
57
  int64_t integer;
58
59
  /// Set the appropriate union member depending on the supplied argument type.
60
  template <typename T>
61
0
  inline void set(T val) {
62
0
    if (std::is_integral<T>::value)
63
0
      integer = (int64_t)val;
64
0
    else
65
0
      floating = (double)val;
66
0
  }
Unexecuted instantiation: void hermes::inst::OperandValue::set<unsigned char>(unsigned char)
Unexecuted instantiation: void hermes::inst::OperandValue::set<unsigned short>(unsigned short)
Unexecuted instantiation: void hermes::inst::OperandValue::set<unsigned int>(unsigned int)
Unexecuted instantiation: void hermes::inst::OperandValue::set<int>(int)
Unexecuted instantiation: void hermes::inst::OperandValue::set<double>(double)
Unexecuted instantiation: void hermes::inst::OperandValue::set<signed char>(signed char)
67
};
68
69
/// The information in a specific instance of an instruction, decoded in a way
70
/// that makes it easier to access by algorithms that are not specialized by
71
/// instruction.
72
struct DecodedInstruction {
73
  InstMetaData meta;
74
  OperandValue operandValue[INST_MAX_OPERANDS];
75
};
76
77
/// \return the size of the specified instruction in bytes.
78
uint8_t getInstSize(OpCode opCode);
79
80
/// \return the size of the specified operand type in bytes.
81
uint8_t getOperandSize(OperandType type);
82
83
/// \return the name of the instruction as string.
84
llvh::StringRef getOpCodeString(OpCode opCode);
85
86
/// \return the metadata for the specified opcode \p opCode.
87
InstMetaData getInstMetaData(OpCode opCode);
88
89
/// \return the decoded form of the specified instruction instance \p inst.
90
DecodedInstruction decodeInstruction(const Inst *inst);
91
92
llvh::raw_ostream &operator<<(
93
    llvh::raw_ostream &OS,
94
    const DecodedInstruction &decoded);
95
96
/// Print a single operand to the specified stream.
97
void dumpOperand(llvh::raw_ostream &OS, OperandType type, OperandValue value);
98
99
} // namespace inst
100
} // namespace hermes
101
102
#endif // HERMES_INST_INSTDECODE_H