summaryrefslogtreecommitdiff
path: root/deps/v8/src/interpreter/bytecode-jump-table.h
blob: b0a36cadbbe3c3748e5335ea6ac4b6f0cd4a8fec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_INTERPRETER_BYTECODE_JUMP_TABLE_H_
#define V8_INTERPRETER_BYTECODE_JUMP_TABLE_H_

#include "src/bit-vector.h"
#include "src/zone/zone.h"

namespace v8 {
namespace internal {
namespace interpreter {

class ConstantArrayBuilder;

// A jump table for a set of targets in a bytecode array. When an entry in the
// table is bound, it represents a known position in the bytecode array. If no
// entries match, the switch falls through.
class V8_EXPORT_PRIVATE BytecodeJumpTable final : public ZoneObject {
 public:
  // Constructs a new BytecodeJumpTable starting at |constant_pool_index|, with
  // the given |size|, where the case values of the table start at
  // |case_value_base|.
  BytecodeJumpTable(size_t constant_pool_index, int size, int case_value_base,
                    Zone* zone)
      :
#ifdef DEBUG
        bound_(size, zone),
#endif
        constant_pool_index_(constant_pool_index),
        switch_bytecode_offset_(kInvalidOffset),
        size_(size),
        case_value_base_(case_value_base) {
  }

  size_t constant_pool_index() const { return constant_pool_index_; }
  size_t switch_bytecode_offset() const { return switch_bytecode_offset_; }
  int case_value_base() const { return case_value_base_; }
  int size() const { return size_; }
#ifdef DEBUG
  bool is_bound(int case_value) const {
    DCHECK_GE(case_value, case_value_base_);
    DCHECK_LT(case_value, case_value_base_ + size());
    return bound_.Contains(case_value - case_value_base_);
  }
#endif

  size_t ConstantPoolEntryFor(int case_value) {
    DCHECK_GE(case_value, case_value_base_);
    return constant_pool_index_ + case_value - case_value_base_;
  }

 private:
  static const size_t kInvalidIndex = static_cast<size_t>(-1);
  static const size_t kInvalidOffset = static_cast<size_t>(-1);

  void mark_bound(int case_value) {
#ifdef DEBUG
    DCHECK_GE(case_value, case_value_base_);
    DCHECK_LT(case_value, case_value_base_ + size());
    bound_.Add(case_value - case_value_base_);
#endif
  }

  void set_switch_bytecode_offset(size_t offset) {
    DCHECK_EQ(switch_bytecode_offset_, kInvalidOffset);
    switch_bytecode_offset_ = offset;
  }

#ifdef DEBUG
  // This bit vector is only used for DCHECKS, so only store the field in debug
  // builds.
  BitVector bound_;
#endif
  size_t constant_pool_index_;
  size_t switch_bytecode_offset_;
  int size_;
  int case_value_base_;

  friend class BytecodeArrayWriter;
};

}  // namespace interpreter
}  // namespace internal
}  // namespace v8

#endif  // V8_INTERPRETER_BYTECODE_JUMP_TABLE_H_