Home | History | Annotate | Download | only in src
      1 // Copyright 2016, VIXL authors
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are met:
      6 //
      7 //   * Redistributions of source code must retain the above copyright notice,
      8 //     this list of conditions and the following disclaimer.
      9 //   * Redistributions in binary form must reproduce the above copyright notice,
     10 //     this list of conditions and the following disclaimer in the documentation
     11 //     and/or other materials provided with the distribution.
     12 //   * Neither the name of ARM Limited nor the names of its contributors may be
     13 //     used to endorse or promote products derived from this software without
     14 //     specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
     17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
     20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26 
     27 #ifndef VIXL_CODE_BUFFER_H
     28 #define VIXL_CODE_BUFFER_H
     29 
     30 #include <cstring>
     31 
     32 #include "globals-vixl.h"
     33 #include "utils-vixl.h"
     34 
     35 namespace vixl {
     36 
     37 class CodeBuffer {
     38  public:
     39   static const size_t kDefaultCapacity = 4 * KBytes;
     40 
     41   explicit CodeBuffer(size_t capacity = kDefaultCapacity);
     42   CodeBuffer(byte* buffer, size_t capacity);
     43   ~CodeBuffer();
     44 
     45   void Reset();
     46 
     47 #ifdef VIXL_CODE_BUFFER_MMAP
     48   void SetExecutable();
     49   void SetWritable();
     50 #else
     51   // These require page-aligned memory blocks, which we can only guarantee with
     52   // mmap.
     53   VIXL_NO_RETURN_IN_DEBUG_MODE void SetExecutable() { VIXL_UNIMPLEMENTED(); }
     54   VIXL_NO_RETURN_IN_DEBUG_MODE void SetWritable() { VIXL_UNIMPLEMENTED(); }
     55 #endif
     56 
     57   ptrdiff_t GetOffsetFrom(ptrdiff_t offset) const {
     58     ptrdiff_t cursor_offset = cursor_ - buffer_;
     59     VIXL_ASSERT((offset >= 0) && (offset <= cursor_offset));
     60     return cursor_offset - offset;
     61   }
     62   VIXL_DEPRECATED("GetOffsetFrom",
     63                   ptrdiff_t OffsetFrom(ptrdiff_t offset) const) {
     64     return GetOffsetFrom(offset);
     65   }
     66 
     67   ptrdiff_t GetCursorOffset() const { return GetOffsetFrom(0); }
     68   VIXL_DEPRECATED("GetCursorOffset", ptrdiff_t CursorOffset() const) {
     69     return GetCursorOffset();
     70   }
     71 
     72   void Rewind(ptrdiff_t offset) {
     73     byte* rewound_cursor = buffer_ + offset;
     74     VIXL_ASSERT((buffer_ <= rewound_cursor) && (rewound_cursor <= cursor_));
     75     cursor_ = rewound_cursor;
     76   }
     77 
     78   template <typename T>
     79   T GetOffsetAddress(ptrdiff_t offset) const {
     80     VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t));
     81     VIXL_ASSERT((offset >= 0) && (offset <= (cursor_ - buffer_)));
     82     return reinterpret_cast<T>(buffer_ + offset);
     83   }
     84 
     85   // Return the address of the start or end of the buffer.
     86   template <typename T>
     87   T GetStartAddress() const {
     88     VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t));
     89     return GetOffsetAddress<T>(0);
     90   }
     91   template <typename T>
     92   T GetEndAddress() const {
     93     VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t));
     94     return GetOffsetAddress<T>(GetCapacity());
     95   }
     96 
     97   size_t GetRemainingBytes() const {
     98     VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_)));
     99     return (buffer_ + capacity_) - cursor_;
    100   }
    101   VIXL_DEPRECATED("GetRemainingBytes", size_t RemainingBytes() const) {
    102     return GetRemainingBytes();
    103   }
    104 
    105   size_t GetSizeInBytes() const {
    106     VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_)));
    107     return cursor_ - buffer_;
    108   }
    109 
    110   // A code buffer can emit:
    111   //  * 16 or 32-bit data: instruction and constant.
    112   //  * 64-bit data: constant.
    113   //  * string: debug info.
    114   void Emit16(uint16_t data) { Emit(data); }
    115 
    116   void Emit32(uint32_t data) { Emit(data); }
    117 
    118   void Emit64(uint64_t data) { Emit(data); }
    119 
    120   void EmitString(const char* string);
    121 
    122   void EmitData(const void* data, size_t size);
    123 
    124   template <typename T>
    125   void Emit(T value) {
    126     VIXL_ASSERT(HasSpaceFor(sizeof(value)));
    127     dirty_ = true;
    128     memcpy(cursor_, &value, sizeof(value));
    129     cursor_ += sizeof(value);
    130   }
    131 
    132   void UpdateData(size_t offset, const void* data, size_t size);
    133 
    134   // Align to 32bit.
    135   void Align();
    136 
    137   bool Is16bitAligned() const { return IsAligned<2>(cursor_); }
    138 
    139   bool Is32bitAligned() const { return IsAligned<4>(cursor_); }
    140 
    141   size_t GetCapacity() const { return capacity_; }
    142   VIXL_DEPRECATED("GetCapacity", size_t capacity() const) {
    143     return GetCapacity();
    144   }
    145 
    146   bool IsManaged() const { return managed_; }
    147 
    148   void Grow(size_t new_capacity);
    149 
    150   bool IsDirty() const { return dirty_; }
    151 
    152   void SetClean() { dirty_ = false; }
    153 
    154   bool HasSpaceFor(size_t amount) const {
    155     return GetRemainingBytes() >= amount;
    156   }
    157 
    158   void EnsureSpaceFor(size_t amount, bool* has_grown) {
    159     bool is_full = !HasSpaceFor(amount);
    160     if (is_full) Grow(capacity_ * 2 + amount);
    161     VIXL_ASSERT(has_grown != NULL);
    162     *has_grown = is_full;
    163   }
    164   void EnsureSpaceFor(size_t amount) {
    165     bool dummy;
    166     EnsureSpaceFor(amount, &dummy);
    167   }
    168 
    169  private:
    170   // Backing store of the buffer.
    171   byte* buffer_;
    172   // If true the backing store is allocated and deallocated by the buffer. The
    173   // backing store can then grow on demand. If false the backing store is
    174   // provided by the user and cannot be resized internally.
    175   bool managed_;
    176   // Pointer to the next location to be written.
    177   byte* cursor_;
    178   // True if there has been any write since the buffer was created or cleaned.
    179   bool dirty_;
    180   // Capacity in bytes of the backing store.
    181   size_t capacity_;
    182 };
    183 
    184 }  // namespace vixl
    185 
    186 #endif  // VIXL_CODE_BUFFER_H
    187