Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 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 "base/logging.h" |
| 21 | #include "base/macros.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | class Assembler; |
| 26 | class AssemblerBuffer; |
| 27 | class AssemblerFixup; |
| 28 | |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 29 | namespace arm64 { |
| 30 | class Arm64Assembler; |
Andreas Gampe | deae7db | 2017-05-30 09:56:41 -0700 | [diff] [blame] | 31 | } // namespace arm64 |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 32 | namespace mips { |
| 33 | class MipsAssembler; |
Alexey Frunze | a663d9d | 2017-07-31 18:43:18 -0700 | [diff] [blame] | 34 | class MipsLabel; |
Andreas Gampe | deae7db | 2017-05-30 09:56:41 -0700 | [diff] [blame] | 35 | } // namespace mips |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 36 | namespace mips64 { |
| 37 | class Mips64Assembler; |
Alexey Frunze | a663d9d | 2017-07-31 18:43:18 -0700 | [diff] [blame] | 38 | class Mips64Label; |
Andreas Gampe | deae7db | 2017-05-30 09:56:41 -0700 | [diff] [blame] | 39 | } // namespace mips64 |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 40 | namespace x86 { |
| 41 | class X86Assembler; |
| 42 | class NearLabel; |
Andreas Gampe | deae7db | 2017-05-30 09:56:41 -0700 | [diff] [blame] | 43 | } // namespace x86 |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 44 | namespace x86_64 { |
| 45 | class X86_64Assembler; |
| 46 | class NearLabel; |
Andreas Gampe | deae7db | 2017-05-30 09:56:41 -0700 | [diff] [blame] | 47 | } // namespace x86_64 |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 48 | |
| 49 | class 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 | |
| 66 | class Label { |
| 67 | public: |
| 68 | Label() : position_(0) {} |
| 69 | |
Goran Jakovljevic | 8c434dc | 2015-08-26 14:39:44 +0200 | [diff] [blame] | 70 | 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 Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 77 | ~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 Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 117 | friend class arm64::Arm64Assembler; |
| 118 | friend class mips::MipsAssembler; |
Alexey Frunze | a663d9d | 2017-07-31 18:43:18 -0700 | [diff] [blame] | 119 | friend class mips::MipsLabel; |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 120 | friend class mips64::Mips64Assembler; |
Alexey Frunze | a663d9d | 2017-07-31 18:43:18 -0700 | [diff] [blame] | 121 | friend class mips64::Mips64Label; |
Andreas Gampe | 85b62f2 | 2015-09-09 13:15:38 -0700 | [diff] [blame] | 122 | 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_ |