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 arm {
     30   class ArmAssembler;
     31   class Arm32Assembler;
     32   class Thumb2Assembler;
     33 }
     34 namespace arm64 {
     35   class Arm64Assembler;
     36 }
     37 namespace mips {
     38   class MipsAssembler;
     39 }
     40 namespace mips64 {
     41   class Mips64Assembler;
     42 }
     43 namespace x86 {
     44   class X86Assembler;
     45   class NearLabel;
     46 }
     47 namespace x86_64 {
     48   class X86_64Assembler;
     49   class NearLabel;
     50 }
     51 
     52 class 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 
     69 class Label {
     70  public:
     71   Label() : position_(0) {}
     72 
     73   Label(Label&& src)
     74       : position_(src.position_) {
     75     // We must unlink/unbind the src label when moving; if not, calling the destructor on
     76     // the src label would fail.
     77     src.position_ = 0;
     78   }
     79 
     80   ~Label() {
     81     // Assert if label is being destroyed with unresolved branches pending.
     82     CHECK(!IsLinked());
     83   }
     84 
     85   // Returns the position for bound and linked labels. Cannot be used
     86   // for unused labels.
     87   int Position() const {
     88     CHECK(!IsUnused());
     89     return IsBound() ? -position_ - sizeof(void*) : position_ - sizeof(void*);
     90   }
     91 
     92   int LinkPosition() const {
     93     CHECK(IsLinked());
     94     return position_ - sizeof(void*);
     95   }
     96 
     97   bool IsBound() const { return position_ < 0; }
     98   bool IsUnused() const { return position_ == 0; }
     99   bool IsLinked() const { return position_ > 0; }
    100 
    101  private:
    102   int position_;
    103 
    104   void Reinitialize() {
    105     position_ = 0;
    106   }
    107 
    108   void BindTo(int position) {
    109     CHECK(!IsBound());
    110     position_ = -position - sizeof(void*);
    111     CHECK(IsBound());
    112   }
    113 
    114   void LinkTo(int position) {
    115     CHECK(!IsBound());
    116     position_ = position + sizeof(void*);
    117     CHECK(IsLinked());
    118   }
    119 
    120   friend class arm::ArmAssembler;
    121   friend class arm::Arm32Assembler;
    122   friend class arm::Thumb2Assembler;
    123   friend class arm64::Arm64Assembler;
    124   friend class mips::MipsAssembler;
    125   friend class mips64::Mips64Assembler;
    126   friend class x86::X86Assembler;
    127   friend class x86::NearLabel;
    128   friend class x86_64::X86_64Assembler;
    129   friend class x86_64::NearLabel;
    130 
    131   DISALLOW_COPY_AND_ASSIGN(Label);
    132 };
    133 
    134 }  // namespace art
    135 
    136 #endif  // ART_COMPILER_UTILS_LABEL_H_
    137