• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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_LABEL_H_
6 #define V8_LABEL_H_
7 
8 #include "src/base/macros.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 // -----------------------------------------------------------------------------
14 // Labels represent pc locations; they are typically jump or call targets.
15 // After declaration, a label can be freely used to denote known or (yet)
16 // unknown pc location. Assembler::bind() is used to bind a label to the
17 // current pc. A label can be bound only once.
18 
19 class Label {
20  public:
21   enum Distance {
22     kNear,  // near jump: 8 bit displacement (signed)
23     kFar    // far jump: 32 bit displacement (signed)
24   };
25 
26   Label() = default;
27 
28 // On ARM64, the Assembler keeps track of pointers to Labels to resolve
29 // branches to distant targets. Copying labels would confuse the Assembler.
30 // On other platforms, allow move construction.
31 #if !V8_TARGET_ARCH_ARM64
32 // In debug builds, the old Label has to be cleared in order to avoid a DCHECK
33 // failure in it's destructor.
34 #ifdef DEBUG
Label(Label && other)35   Label(Label&& other) V8_NOEXCEPT { *this = std::move(other); }
36   Label& operator=(Label&& other) V8_NOEXCEPT {
37     pos_ = other.pos_;
38     near_link_pos_ = other.near_link_pos_;
39     other.Unuse();
40     other.UnuseNear();
41     return *this;
42   }
43 #else
44   Label(Label&&) V8_NOEXCEPT = default;
45   Label& operator=(Label&&) V8_NOEXCEPT = default;
46 #endif
47 #endif
48 
~Label()49   V8_INLINE ~Label() {
50     DCHECK(!is_linked());
51     DCHECK(!is_near_linked());
52   }
53 
Unuse()54   V8_INLINE void Unuse() { pos_ = 0; }
UnuseNear()55   V8_INLINE void UnuseNear() { near_link_pos_ = 0; }
56 
is_bound()57   V8_INLINE bool is_bound() const { return pos_ < 0; }
is_unused()58   V8_INLINE bool is_unused() const { return pos_ == 0 && near_link_pos_ == 0; }
is_linked()59   V8_INLINE bool is_linked() const { return pos_ > 0; }
is_near_linked()60   V8_INLINE bool is_near_linked() const { return near_link_pos_ > 0; }
61 
62   // Returns the position of bound or linked labels. Cannot be used
63   // for unused labels.
pos()64   int pos() const {
65     if (pos_ < 0) return -pos_ - 1;
66     if (pos_ > 0) return pos_ - 1;
67     UNREACHABLE();
68   }
69 
near_link_pos()70   int near_link_pos() const { return near_link_pos_ - 1; }
71 
72  private:
73   // pos_ encodes both the binding state (via its sign)
74   // and the binding position (via its value) of a label.
75   //
76   // pos_ <  0  bound label, pos() returns the jump target position
77   // pos_ == 0  unused label
78   // pos_ >  0  linked label, pos() returns the last reference position
79   int pos_ = 0;
80 
81   // Behaves like |pos_| in the "> 0" case, but for near jumps to this label.
82   int near_link_pos_ = 0;
83 
bind_to(int pos)84   void bind_to(int pos) {
85     pos_ = -pos - 1;
86     DCHECK(is_bound());
87   }
88   void link_to(int pos, Distance distance = kFar) {
89     if (distance == kNear) {
90       near_link_pos_ = pos + 1;
91       DCHECK(is_near_linked());
92     } else {
93       pos_ = pos + 1;
94       DCHECK(is_linked());
95     }
96   }
97 
98   friend class Assembler;
99   friend class Displacement;
100   friend class RegExpMacroAssemblerIrregexp;
101 
102   // Disallow copy construction and assignment, but allow move construction and
103   // move assignment on selected platforms (see above).
104   DISALLOW_COPY_AND_ASSIGN(Label);
105 };
106 
107 }  // namespace internal
108 }  // namespace v8
109 
110 #endif  // V8_LABEL_H_
111