• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014, ARM Limited
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 <string.h>
31 #include "vixl/globals.h"
32 
33 namespace vixl {
34 
35 class CodeBuffer {
36  public:
37   explicit CodeBuffer(size_t capacity = 4 * KBytes);
38   CodeBuffer(void* buffer, size_t capacity);
39   ~CodeBuffer();
40 
41   void Reset();
42 
OffsetFrom(ptrdiff_t offset)43   ptrdiff_t OffsetFrom(ptrdiff_t offset) const {
44     ptrdiff_t cursor_offset = cursor_ - buffer_;
45     VIXL_ASSERT((offset >= 0) && (offset <= cursor_offset));
46     return cursor_offset - offset;
47   }
48 
CursorOffset()49   ptrdiff_t CursorOffset() const {
50     return OffsetFrom(0);
51   }
52 
53   template <typename T>
GetOffsetAddress(ptrdiff_t offset)54   T GetOffsetAddress(ptrdiff_t offset) const {
55     VIXL_ASSERT((offset >= 0) && (offset <= (cursor_ - buffer_)));
56     return reinterpret_cast<T>(buffer_ + offset);
57   }
58 
RemainingBytes()59   size_t RemainingBytes() const {
60     VIXL_ASSERT((cursor_ >= buffer_) && (cursor_ <= (buffer_ + capacity_)));
61     return (buffer_ + capacity_) - cursor_;
62   }
63 
64   // A code buffer can emit:
65   //  * 32-bit data: instruction and constant.
66   //  * 64-bit data: constant.
67   //  * string: debug info.
Emit32(uint32_t data)68   void Emit32(uint32_t data) { Emit(data); }
69 
Emit64(uint64_t data)70   void Emit64(uint64_t data) { Emit(data); }
71 
72   void EmitString(const char* string);
73 
74   // Align to kInstructionSize.
75   void Align();
76 
capacity()77   size_t capacity() const { return capacity_; }
78 
IsManaged()79   bool IsManaged() const { return managed_; }
80 
81   void Grow(size_t new_capacity);
82 
IsDirty()83   bool IsDirty() const { return dirty_; }
84 
SetClean()85   void SetClean() { dirty_ = false; }
86 
87  private:
88   template <typename T>
Emit(T value)89   void Emit(T value) {
90     VIXL_ASSERT(RemainingBytes() >= sizeof(value));
91     dirty_ = true;
92     memcpy(cursor_, &value, sizeof(value));
93     cursor_ += sizeof(value);
94   }
95 
96   // Backing store of the buffer.
97   byte* buffer_;
98   // If true the backing store is allocated and deallocated by the buffer. The
99   // backing store can then grow on demand. If false the backing store is
100   // provided by the user and cannot be resized internally.
101   bool managed_;
102   // Pointer to the next location to be written.
103   byte* cursor_;
104   // True if there has been any write since the buffer was created or cleaned.
105   bool dirty_;
106   // Capacity in bytes of the backing store.
107   size_t capacity_;
108 };
109 
110 }  // namespace vixl
111 
112 #endif  // VIXL_CODE_BUFFER_H
113 
114