blob: d835c63443be6fc446dc19eab07db73a20c9f7bd [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;
Alexey Frunzea663d9d2017-07-31 18:43:18 -070034 class MipsLabel;
Andreas Gampedeae7db2017-05-30 09:56:41 -070035} // namespace mips
Andreas Gampe85b62f22015-09-09 13:15:38 -070036namespace mips64 {
37 class Mips64Assembler;
Alexey Frunzea663d9d2017-07-31 18:43:18 -070038 class Mips64Label;
Andreas Gampedeae7db2017-05-30 09:56:41 -070039} // namespace mips64
Andreas Gampe85b62f22015-09-09 13:15:38 -070040namespace x86 {
41 class X86Assembler;
42 class NearLabel;
Andreas Gampedeae7db2017-05-30 09:56:41 -070043} // namespace x86
Andreas Gampe85b62f22015-09-09 13:15:38 -070044namespace x86_64 {
45 class X86_64Assembler;
46 class NearLabel;
Andreas Gampedeae7db2017-05-30 09:56:41 -070047} // namespace x86_64
Andreas Gampe85b62f22015-09-09 13:15:38 -070048
49class ExternalLabel {
50 public:
51 ExternalLabel(const char* name_in, uintptr_t address_in)
52 : name_(name_in), address_(address_in) {
53 DCHECK(name_in != nullptr);
54 }
55
56 const char* name() const { return name_; }
57 uintptr_t address() const {
58 return address_;
59 }
60
61 private:
62 const char* name_;
63 const uintptr_t address_;
64};
65
66class Label {
67 public:
68 Label() : position_(0) {}
69
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020070 Label(Label&& src)
71 : position_(src.position_) {
72 // We must unlink/unbind the src label when moving; if not, calling the destructor on
73 // the src label would fail.
74 src.position_ = 0;
75 }
76
Andreas Gampe85b62f22015-09-09 13:15:38 -070077 ~Label() {
78 // Assert if label is being destroyed with unresolved branches pending.
79 CHECK(!IsLinked());
80 }
81
82 // Returns the position for bound and linked labels. Cannot be used
83 // for unused labels.
84 int Position() const {
85 CHECK(!IsUnused());
86 return IsBound() ? -position_ - sizeof(void*) : position_ - sizeof(void*);
87 }
88
89 int LinkPosition() const {
90 CHECK(IsLinked());
91 return position_ - sizeof(void*);
92 }
93
94 bool IsBound() const { return position_ < 0; }
95 bool IsUnused() const { return position_ == 0; }
96 bool IsLinked() const { return position_ > 0; }
97
98 private:
99 int position_;
100
101 void Reinitialize() {
102 position_ = 0;
103 }
104
105 void BindTo(int position) {
106 CHECK(!IsBound());
107 position_ = -position - sizeof(void*);
108 CHECK(IsBound());
109 }
110
111 void LinkTo(int position) {
112 CHECK(!IsBound());
113 position_ = position + sizeof(void*);
114 CHECK(IsLinked());
115 }
116
Andreas Gampe85b62f22015-09-09 13:15:38 -0700117 friend class arm64::Arm64Assembler;
118 friend class mips::MipsAssembler;
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700119 friend class mips::MipsLabel;
Andreas Gampe85b62f22015-09-09 13:15:38 -0700120 friend class mips64::Mips64Assembler;
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700121 friend class mips64::Mips64Label;
Andreas Gampe85b62f22015-09-09 13:15:38 -0700122 friend class x86::X86Assembler;
123 friend class x86::NearLabel;
124 friend class x86_64::X86_64Assembler;
125 friend class x86_64::NearLabel;
126
127 DISALLOW_COPY_AND_ASSIGN(Label);
128};
129
130} // namespace art
131
132#endif // ART_COMPILER_UTILS_LABEL_H_