11cb0ef41Sopenharmony_ci// Copyright 2017 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_INTERPRETER_BYTECODE_JUMP_TABLE_H_
61cb0ef41Sopenharmony_ci#define V8_INTERPRETER_BYTECODE_JUMP_TABLE_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/utils/bit-vector.h"
91cb0ef41Sopenharmony_ci#include "src/zone/zone.h"
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cinamespace v8 {
121cb0ef41Sopenharmony_cinamespace internal {
131cb0ef41Sopenharmony_cinamespace interpreter {
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciclass ConstantArrayBuilder;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci// A jump table for a set of targets in a bytecode array. When an entry in the
181cb0ef41Sopenharmony_ci// table is bound, it represents a known position in the bytecode array. If no
191cb0ef41Sopenharmony_ci// entries match, the switch falls through.
201cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE BytecodeJumpTable final : public ZoneObject {
211cb0ef41Sopenharmony_ci public:
221cb0ef41Sopenharmony_ci  // Constructs a new BytecodeJumpTable starting at |constant_pool_index|, with
231cb0ef41Sopenharmony_ci  // the given |size|, where the case values of the table start at
241cb0ef41Sopenharmony_ci  // |case_value_base|.
251cb0ef41Sopenharmony_ci  BytecodeJumpTable(size_t constant_pool_index, int size, int case_value_base,
261cb0ef41Sopenharmony_ci                    Zone* zone)
271cb0ef41Sopenharmony_ci      :
281cb0ef41Sopenharmony_ci#ifdef DEBUG
291cb0ef41Sopenharmony_ci        bound_(size, zone),
301cb0ef41Sopenharmony_ci#endif
311cb0ef41Sopenharmony_ci        constant_pool_index_(constant_pool_index),
321cb0ef41Sopenharmony_ci        switch_bytecode_offset_(kInvalidOffset),
331cb0ef41Sopenharmony_ci        size_(size),
341cb0ef41Sopenharmony_ci        case_value_base_(case_value_base) {
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  size_t constant_pool_index() const { return constant_pool_index_; }
381cb0ef41Sopenharmony_ci  size_t switch_bytecode_offset() const { return switch_bytecode_offset_; }
391cb0ef41Sopenharmony_ci  int case_value_base() const { return case_value_base_; }
401cb0ef41Sopenharmony_ci  int size() const { return size_; }
411cb0ef41Sopenharmony_ci#ifdef DEBUG
421cb0ef41Sopenharmony_ci  bool is_bound(int case_value) const {
431cb0ef41Sopenharmony_ci    DCHECK_GE(case_value, case_value_base_);
441cb0ef41Sopenharmony_ci    DCHECK_LT(case_value, case_value_base_ + size());
451cb0ef41Sopenharmony_ci    return bound_.Contains(case_value - case_value_base_);
461cb0ef41Sopenharmony_ci  }
471cb0ef41Sopenharmony_ci#endif
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  size_t ConstantPoolEntryFor(int case_value) {
501cb0ef41Sopenharmony_ci    DCHECK_GE(case_value, case_value_base_);
511cb0ef41Sopenharmony_ci    return constant_pool_index_ + case_value - case_value_base_;
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci private:
551cb0ef41Sopenharmony_ci  static const size_t kInvalidIndex = static_cast<size_t>(-1);
561cb0ef41Sopenharmony_ci  static const size_t kInvalidOffset = static_cast<size_t>(-1);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  void mark_bound(int case_value) {
591cb0ef41Sopenharmony_ci#ifdef DEBUG
601cb0ef41Sopenharmony_ci    DCHECK_GE(case_value, case_value_base_);
611cb0ef41Sopenharmony_ci    DCHECK_LT(case_value, case_value_base_ + size());
621cb0ef41Sopenharmony_ci    bound_.Add(case_value - case_value_base_);
631cb0ef41Sopenharmony_ci#endif
641cb0ef41Sopenharmony_ci  }
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  void set_switch_bytecode_offset(size_t offset) {
671cb0ef41Sopenharmony_ci    DCHECK_EQ(switch_bytecode_offset_, kInvalidOffset);
681cb0ef41Sopenharmony_ci    switch_bytecode_offset_ = offset;
691cb0ef41Sopenharmony_ci  }
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci#ifdef DEBUG
721cb0ef41Sopenharmony_ci  // This bit vector is only used for DCHECKS, so only store the field in debug
731cb0ef41Sopenharmony_ci  // builds.
741cb0ef41Sopenharmony_ci  BitVector bound_;
751cb0ef41Sopenharmony_ci#endif
761cb0ef41Sopenharmony_ci  size_t constant_pool_index_;
771cb0ef41Sopenharmony_ci  size_t switch_bytecode_offset_;
781cb0ef41Sopenharmony_ci  int size_;
791cb0ef41Sopenharmony_ci  int case_value_base_;
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  friend class BytecodeArrayWriter;
821cb0ef41Sopenharmony_ci};
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci}  // namespace interpreter
851cb0ef41Sopenharmony_ci}  // namespace internal
861cb0ef41Sopenharmony_ci}  // namespace v8
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci#endif  // V8_INTERPRETER_BYTECODE_JUMP_TABLE_H_
89