blob: ff4a1a433358cc826424cd6b0e3050ba2a7507af [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
29namespace arm {
30 class ArmAssembler;
31 class Arm32Assembler;
32 class Thumb2Assembler;
33}
34namespace arm64 {
35 class Arm64Assembler;
36}
37namespace mips {
38 class MipsAssembler;
39}
40namespace mips64 {
41 class Mips64Assembler;
42}
43namespace x86 {
44 class X86Assembler;
45 class NearLabel;
46}
47namespace x86_64 {
48 class X86_64Assembler;
49 class NearLabel;
50}
51
52class ExternalLabel {
53 public:
54 ExternalLabel(const char* name_in, uintptr_t address_in)
55 : name_(name_in), address_(address_in) {
56 DCHECK(name_in != nullptr);
57 }
58
59 const char* name() const { return name_; }
60 uintptr_t address() const {
61 return address_;
62 }
63
64 private:
65 const char* name_;
66 const uintptr_t address_;
67};
68
69class Label {
70 public:
71 Label() : position_(0) {}
72
73 ~Label() {
74 // Assert if label is being destroyed with unresolved branches pending.
75 CHECK(!IsLinked());
76 }
77
78 // Returns the position for bound and linked labels. Cannot be used
79 // for unused labels.
80 int Position() const {
81 CHECK(!IsUnused());
82 return IsBound() ? -position_ - sizeof(void*) : position_ - sizeof(void*);
83 }
84
85 int LinkPosition() const {
86 CHECK(IsLinked());
87 return position_ - sizeof(void*);
88 }
89
90 bool IsBound() const { return position_ < 0; }
91 bool IsUnused() const { return position_ == 0; }
92 bool IsLinked() const { return position_ > 0; }
93
94 private:
95 int position_;
96
97 void Reinitialize() {
98 position_ = 0;
99 }
100
101 void BindTo(int position) {
102 CHECK(!IsBound());
103 position_ = -position - sizeof(void*);
104 CHECK(IsBound());
105 }
106
107 void LinkTo(int position) {
108 CHECK(!IsBound());
109 position_ = position + sizeof(void*);
110 CHECK(IsLinked());
111 }
112
113 friend class arm::ArmAssembler;
114 friend class arm::Arm32Assembler;
115 friend class arm::Thumb2Assembler;
116 friend class arm64::Arm64Assembler;
117 friend class mips::MipsAssembler;
118 friend class mips64::Mips64Assembler;
119 friend class x86::X86Assembler;
120 friend class x86::NearLabel;
121 friend class x86_64::X86_64Assembler;
122 friend class x86_64::NearLabel;
123
124 DISALLOW_COPY_AND_ASSIGN(Label);
125};
126
127} // namespace art
128
129#endif // ART_COMPILER_UTILS_LABEL_H_