Instruction.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. Instruction::Instruction(Type type, size_t length)
  11. : m_type(type)
  12. , m_length(length)
  13. {
  14. VERIFY(length <= NumericLimits<u32>::max());
  15. }
  16. void Instruction::destroy(Instruction& instruction)
  17. {
  18. #define __BYTECODE_OP(op) \
  19. case Type::op: \
  20. static_cast<Op::op&>(instruction).~op(); \
  21. return;
  22. switch (instruction.type()) {
  23. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  24. default:
  25. VERIFY_NOT_REACHED();
  26. }
  27. #undef __BYTECODE_OP
  28. }
  29. void Instruction::visit_labels(Function<void(JS::Bytecode::Label&)> visitor)
  30. {
  31. #define __BYTECODE_OP(op) \
  32. case Type::op: \
  33. static_cast<Op::op&>(*this).visit_labels_impl(move(visitor)); \
  34. return;
  35. switch (type()) {
  36. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  37. default:
  38. VERIFY_NOT_REACHED();
  39. }
  40. #undef __BYTECODE_OP
  41. }
  42. UnrealizedSourceRange InstructionStreamIterator::source_range() const
  43. {
  44. VERIFY(m_executable);
  45. auto record = dereference().source_record();
  46. return {
  47. .source_code = m_executable->source_code,
  48. .start_offset = record.source_start_offset,
  49. .end_offset = record.source_end_offset,
  50. };
  51. }
  52. RefPtr<SourceCode> InstructionStreamIterator::source_code() const
  53. {
  54. return m_executable ? m_executable->source_code.ptr() : nullptr;
  55. }
  56. Operand::Operand(Register reg)
  57. : m_type(Type::Register)
  58. , m_index(reg.index())
  59. {
  60. }
  61. }