Instruction.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Bytecode/Executable.h>
  7. #include <LibJS/Bytecode/Instruction.h>
  8. #include <LibJS/Bytecode/Op.h>
  9. namespace JS::Bytecode {
  10. void Instruction::destroy(Instruction& instruction)
  11. {
  12. #define __BYTECODE_OP(op) \
  13. case Type::op: \
  14. static_cast<Op::op&>(instruction).~op(); \
  15. return;
  16. switch (instruction.type()) {
  17. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  18. default:
  19. VERIFY_NOT_REACHED();
  20. }
  21. #undef __BYTECODE_OP
  22. }
  23. void Instruction::visit_labels(Function<void(JS::Bytecode::Label&)> visitor)
  24. {
  25. #define __BYTECODE_OP(op) \
  26. case Type::op: \
  27. static_cast<Op::op&>(*this).visit_labels_impl(move(visitor)); \
  28. return;
  29. switch (type()) {
  30. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  31. default:
  32. VERIFY_NOT_REACHED();
  33. }
  34. #undef __BYTECODE_OP
  35. }
  36. void Instruction::visit_operands(Function<void(JS::Bytecode::Operand&)> visitor)
  37. {
  38. #define __BYTECODE_OP(op) \
  39. case Type::op: \
  40. static_cast<Op::op&>(*this).visit_operands_impl(move(visitor)); \
  41. return;
  42. switch (type()) {
  43. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  44. default:
  45. VERIFY_NOT_REACHED();
  46. }
  47. #undef __BYTECODE_OP
  48. }
  49. template<typename Op>
  50. concept HasVariableLength = Op::IsVariableLength;
  51. template<typename Op>
  52. concept HasFixedLength = !Op::IsVariableLength;
  53. template<HasVariableLength Op>
  54. size_t get_length_impl(Op const& op)
  55. {
  56. return op.length_impl();
  57. }
  58. // Function template for types without a length_impl method
  59. template<HasFixedLength Op>
  60. size_t get_length_impl(Op const&)
  61. {
  62. return sizeof(Op);
  63. }
  64. size_t Instruction::length() const
  65. {
  66. #define __BYTECODE_OP(op) \
  67. case Type::op: { \
  68. auto& typed_op = static_cast<Op::op const&>(*this); \
  69. return get_length_impl(typed_op); \
  70. }
  71. switch (type()) {
  72. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  73. default:
  74. VERIFY_NOT_REACHED();
  75. }
  76. #undef __BYTECODE_OP
  77. }
  78. UnrealizedSourceRange InstructionStreamIterator::source_range() const
  79. {
  80. VERIFY(m_executable);
  81. auto record = m_executable->source_map.get(offset()).value();
  82. return {
  83. .source_code = m_executable->source_code,
  84. .start_offset = record.source_start_offset,
  85. .end_offset = record.source_end_offset,
  86. };
  87. }
  88. RefPtr<SourceCode> InstructionStreamIterator::source_code() const
  89. {
  90. return m_executable ? m_executable->source_code.ptr() : nullptr;
  91. }
  92. Operand::Operand(Register reg)
  93. : m_type(Type::Register)
  94. , m_index(reg.index())
  95. {
  96. }
  97. }