blob: 9586a1996a14354a6f1a9520bc02e78688c25e68 [file] [log] [blame]
Andreas Gampe85b62f22015-09-09 13:15:38 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_UTILS_LABEL_H_
18#define ART_COMPILER_UTILS_LABEL_H_
19
Andreas Gampe57943812017-12-06 21:39:13 -080020#include <android-base/logging.h>
21#include <android-base/macros.h>
Andreas Gampe85b62f22015-09-09 13:15:38 -070022
23namespace art {
24
25class Assembler;
26class AssemblerBuffer;
27class AssemblerFixup;
28
Andreas Gampe85b62f22015-09-09 13:15:38 -070029namespace arm64 {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080030class Arm64Assembler;
Andreas Gampedeae7db2017-05-30 09:56:41 -070031} // namespace arm64
Andreas Gampe85b62f22015-09-09 13:15:38 -070032namespace x86 {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080033class X86Assembler;
34class NearLabel;
Andreas Gampedeae7db2017-05-30 09:56:41 -070035} // namespace x86
Andreas Gampe85b62f22015-09-09 13:15:38 -070036namespace x86_64 {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080037class X86_64Assembler;
38class NearLabel;
Andreas Gampedeae7db2017-05-30 09:56:41 -070039} // namespace x86_64
Andreas Gampe85b62f22015-09-09 13:15:38 -070040
41class ExternalLabel {
42 public:
43 ExternalLabel(const char* name_in, uintptr_t address_in)
44 : name_(name_in), address_(address_in) {
45 DCHECK(name_in != nullptr);
46 }
47
48 const char* name() const { return name_; }
49 uintptr_t address() const {
50 return address_;
51 }
52
53 private:
54 const char* name_;
55 const uintptr_t address_;
56};
57
58class Label {
59 public:
60 Label() : position_(0) {}
61
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020062 Label(Label&& src)
63 : position_(src.position_) {
64 // We must unlink/unbind the src label when moving; if not, calling the destructor on
65 // the src label would fail.
66 src.position_ = 0;
67 }
68
Andreas Gampe85b62f22015-09-09 13:15:38 -070069 ~Label() {
70 // Assert if label is being destroyed with unresolved branches pending.
71 CHECK(!IsLinked());
72 }
73
74 // Returns the position for bound and linked labels. Cannot be used
75 // for unused labels.
76 int Position() const {
77 CHECK(!IsUnused());
78 return IsBound() ? -position_ - sizeof(void*) : position_ - sizeof(void*);
79 }
80
81 int LinkPosition() const {
82 CHECK(IsLinked());
83 return position_ - sizeof(void*);
84 }
85
86 bool IsBound() const { return position_ < 0; }
87 bool IsUnused() const { return position_ == 0; }
88 bool IsLinked() const { return position_ > 0; }
89
90 private:
91 int position_;
92
93 void Reinitialize() {
94 position_ = 0;
95 }
96
97 void BindTo(int position) {
98 CHECK(!IsBound());
99 position_ = -position - sizeof(void*);
100 CHECK(IsBound());
101 }
102
103 void LinkTo(int position) {
104 CHECK(!IsBound());
105 position_ = position + sizeof(void*);
106 CHECK(IsLinked());
107 }
108
Andreas Gampe85b62f22015-09-09 13:15:38 -0700109 friend class arm64::Arm64Assembler;
Andreas Gampe85b62f22015-09-09 13:15:38 -0700110 friend class x86::X86Assembler;
111 friend class x86::NearLabel;
112 friend class x86_64::X86_64Assembler;
113 friend class x86_64::NearLabel;
114
115 DISALLOW_COPY_AND_ASSIGN(Label);
116};
117
118} // namespace art
119
120#endif // ART_COMPILER_UTILS_LABEL_H_