Instruction.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.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. template<typename Op>
  37. concept HasVariableLength = Op::IsVariableLength;
  38. template<typename Op>
  39. concept HasFixedLength = !Op::IsVariableLength;
  40. template<HasVariableLength Op>
  41. size_t get_length_impl(Op const& op)
  42. {
  43. return op.length_impl();
  44. }
  45. // Function template for types without a length_impl method
  46. template<HasFixedLength Op>
  47. size_t get_length_impl(Op const&)
  48. {
  49. return sizeof(Op);
  50. }
  51. size_t Instruction::length() const
  52. {
  53. #define __BYTECODE_OP(op) \
  54. case Type::op: { \
  55. auto& typed_op = static_cast<Op::op const&>(*this); \
  56. return get_length_impl(typed_op); \
  57. }
  58. switch (type()) {
  59. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  60. default:
  61. VERIFY_NOT_REACHED();
  62. }
  63. #undef __BYTECODE_OP
  64. }
  65. UnrealizedSourceRange InstructionStreamIterator::source_range() const
  66. {
  67. VERIFY(m_executable);
  68. auto record = m_executable->source_map.get(offset()).value();
  69. return {
  70. .source_code = m_executable->source_code,
  71. .start_offset = record.source_start_offset,
  72. .end_offset = record.source_end_offset,
  73. };
  74. }
  75. RefPtr<SourceCode> InstructionStreamIterator::source_code() const
  76. {
  77. return m_executable ? m_executable->source_code.ptr() : nullptr;
  78. }
  79. Operand::Operand(Register reg)
  80. : m_type(Type::Register)
  81. , m_index(reg.index())
  82. {
  83. }
  84. }