Home | History | Annotate | Download | only in utils
      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 
     29 namespace arm64 {
     30   class Arm64Assembler;
     31 }  // namespace arm64
     32 namespace mips {
     33   class MipsAssembler;
     34 }  // namespace mips
     35 namespace mips64 {
     36   class Mips64Assembler;
     37 }  // namespace mips64
     38 namespace x86 {
     39   class X86Assembler;
     40   class NearLabel;
     41 }  // namespace x86
     42 namespace x86_64 {
     43   class X86_64Assembler;
     44   class NearLabel;
     45 }  // namespace x86_64
     46 
     47 class 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 
     64 class Label {
     65  public:
     66   Label() : position_(0) {}
     67 
     68   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 
     75   ~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 
    115   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_
    129