1 /* 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 <android-base/logging.h> 21 #include <android-base/macros.h> 22 23 #include "base/macros.h" 24 25 namespace art HIDDEN { 26 27 class Assembler; 28 class AssemblerBuffer; 29 class AssemblerFixup; 30 31 namespace arm64 { 32 class Arm64Assembler; 33 } // namespace arm64 34 namespace x86 { 35 class X86Assembler; 36 class NearLabel; 37 } // namespace x86 38 namespace x86_64 { 39 class X86_64Assembler; 40 class NearLabel; 41 } // namespace x86_64 42 43 class ExternalLabel { 44 public: ExternalLabel(const char * name_in,uintptr_t address_in)45 ExternalLabel(const char* name_in, uintptr_t address_in) 46 : name_(name_in), address_(address_in) { 47 DCHECK(name_in != nullptr); 48 } 49 name()50 const char* name() const { return name_; } address()51 uintptr_t address() const { 52 return address_; 53 } 54 55 private: 56 const char* name_; 57 const uintptr_t address_; 58 }; 59 60 class Label { 61 public: Label()62 Label() : position_(0) {} 63 Label(Label && src)64 Label(Label&& src) noexcept 65 : position_(src.position_) { 66 // We must unlink/unbind the src label when moving; if not, calling the destructor on 67 // the src label would fail. 68 src.position_ = 0; 69 } 70 ~Label()71 ~Label() { 72 // Assert if label is being destroyed with unresolved branches pending. 73 CHECK(!IsLinked()); 74 } 75 76 // Returns the position for bound and linked labels. Cannot be used 77 // for unused labels. Position()78 int Position() const { 79 CHECK(!IsUnused()); 80 return IsBound() ? -position_ - sizeof(void*) : position_ - sizeof(void*); 81 } 82 LinkPosition()83 int LinkPosition() const { 84 CHECK(IsLinked()); 85 return position_ - sizeof(void*); 86 } 87 IsBound()88 bool IsBound() const { return position_ < 0; } IsUnused()89 bool IsUnused() const { return position_ == 0; } IsLinked()90 bool IsLinked() const { return position_ > 0; } 91 92 private: 93 int position_; 94 Reinitialize()95 void Reinitialize() { 96 position_ = 0; 97 } 98 BindTo(int position)99 void BindTo(int position) { 100 CHECK(!IsBound()); 101 position_ = -position - sizeof(void*); 102 CHECK(IsBound()); 103 } 104 LinkTo(int position)105 void LinkTo(int position) { 106 CHECK(!IsBound()); 107 position_ = position + sizeof(void*); 108 CHECK(IsLinked()); 109 } 110 111 friend class arm64::Arm64Assembler; 112 friend class x86::X86Assembler; 113 friend class x86::NearLabel; 114 friend class x86_64::X86_64Assembler; 115 friend class x86_64::NearLabel; 116 117 DISALLOW_COPY_AND_ASSIGN(Label); 118 }; 119 120 } // namespace art 121 122 #endif // ART_COMPILER_UTILS_LABEL_H_ 123