Home | History | Annotate | Download | only in interpreter
      1 // Copyright 2015 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef V8_INTERPRETER_BYTECODE_LABEL_H_
      6 #define V8_INTERPRETER_BYTECODE_LABEL_H_
      7 
      8 namespace v8 {
      9 namespace internal {
     10 namespace interpreter {
     11 
     12 // A label representing a branch target in a bytecode array. When a
     13 // label is bound, it represents a known position in the bytecode
     14 // array. For labels that are forward references there can be at most
     15 // one reference whilst it is unbound.
     16 class BytecodeLabel final {
     17  public:
     18   BytecodeLabel() : bound_(false), offset_(kInvalidOffset) {}
     19 
     20   bool is_bound() const { return bound_; }
     21   size_t offset() const { return offset_; }
     22 
     23  private:
     24   static const size_t kInvalidOffset = static_cast<size_t>(-1);
     25 
     26   void bind_to(size_t offset) {
     27     DCHECK(!bound_ && offset != kInvalidOffset);
     28     offset_ = offset;
     29     bound_ = true;
     30   }
     31 
     32   void set_referrer(size_t offset) {
     33     DCHECK(!bound_ && offset != kInvalidOffset && offset_ == kInvalidOffset);
     34     offset_ = offset;
     35   }
     36 
     37   bool is_forward_target() const {
     38     return offset() != kInvalidOffset && !is_bound();
     39   }
     40 
     41   // There are three states for a label:
     42   //                    bound_   offset_
     43   //  UNSET             false    kInvalidOffset
     44   //  FORWARD_TARGET    false    Offset of referring jump
     45   //  BACKWARD_TARGET    true    Offset of label in bytecode array when bound
     46   bool bound_;
     47   size_t offset_;
     48 
     49   friend class BytecodeArrayWriter;
     50 };
     51 
     52 }  // namespace interpreter
     53 }  // namespace internal
     54 }  // namespace v8
     55 
     56 #endif  // V8_INTERPRETER_BYTECODE_LABEL_H_
     57