blob: 85710d0811bfbdfd1f67b351e263a94561f1ad56 [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
20#include "base/logging.h"
21#include "base/macros.h"
22
23namespace art {
24
25class Assembler;
26class AssemblerBuffer;
27class AssemblerFixup;
28
Andreas Gampe85b62f22015-09-09 13:15:38 -070029namespace arm64 {
30 class Arm64Assembler;
Andreas Gampedeae7db2017-05-30 09:56:41 -070031} // namespace arm64
Andreas Gampe85b62f22015-09-09 13:15:38 -070032namespace mips {
33 class MipsAssembler;
Andreas Gampedeae7db2017-05-30 09:56:41 -070034} // namespace mips
Andreas Gampe85b62f22015-09-09 13:15:38 -070035namespace mips64 {
36 class Mips64Assembler;
Andreas Gampedeae7db2017-05-30 09:56:41 -070037} // namespace mips64
Andreas Gampe85b62f22015-09-09 13:15:38 -070038namespace x86 {
39 class X86Assembler;
40 class NearLabel;
Andreas Gampedeae7db2017-05-30 09:56:41 -070041} // namespace x86
Andreas Gampe85b62f22015-09-09 13:15:38 -070042namespace x86_64 {
43 class X86_64Assembler;
44 class NearLabel;
Andreas Gampedeae7db2017-05-30 09:56:41 -070045} // namespace x86_64
Andreas Gampe85b62f22015-09-09 13:15:38 -070046
47class ExternalLabel {
48 public:
49 ExternalLabel(const char* name_in, uintptr_t address_in)
50 : name_(name_in), address_(address_in) {
51 DCHECK(name_in != nullptr);
52 }
53
54 const char* name() const { return name_; }
55 uintptr_t address() const {
56 return address_;
57 }
58
59 private:
60 const char* name_;
61 const uintptr_t address_;
62};
63
64class Label {
65 public:
66 Label() : position_(0) {}
67
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020068 Label(Label&& src)
69 : position_(src.position_) {
70 // We must unlink/unbind the src label when moving; if not, calling the destructor on
71 // the src label would fail.
72 src.position_ = 0;
73 }
74
Andreas Gampe85b62f22015-09-09 13:15:38 -070075 ~Label() {
76 // Assert if label is being destroyed with unresolved branches pending.
77 CHECK(!IsLinked());
78 }
79
80 // Returns the position for bound and linked labels. Cannot be used
81 // for unused labels.
82 int Position() const {
83 CHECK(!IsUnused());
84 return IsBound() ? -position_ - sizeof(void*) : position_ - sizeof(void*);
85 }
86
87 int LinkPosition() const {
88 CHECK(IsLinked());
89 return position_ - sizeof(void*);
90 }
91
92 bool IsBound() const { return position_ < 0; }
93 bool IsUnused() const { return position_ == 0; }
94 bool IsLinked() const { return position_ > 0; }
95
96 private:
97 int position_;
98
99 void Reinitialize() {
100 position_ = 0;
101 }
102
103 void BindTo(int position) {
104 CHECK(!IsBound());
105 position_ = -position - sizeof(void*);
106 CHECK(IsBound());
107 }
108
109 void LinkTo(int position) {
110 CHECK(!IsBound());
111 position_ = position + sizeof(void*);
112 CHECK(IsLinked());
113 }
114
Andreas Gampe85b62f22015-09-09 13:15:38 -0700115 friend class arm64::Arm64Assembler;
116 friend class mips::MipsAssembler;
117 friend class mips64::Mips64Assembler;
118 friend class x86::X86Assembler;
119 friend class x86::NearLabel;
120 friend class x86_64::X86_64Assembler;
121 friend class x86_64::NearLabel;
122
123 DISALLOW_COPY_AND_ASSIGN(Label);
124};
125
126} // namespace art
127
128#endif // ART_COMPILER_UTILS_LABEL_H_