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_ASSEMBLER_BASE_H
28 #define VIXL_ASSEMBLER_BASE_H
29 
30 #include "code-buffer-vixl.h"
31 
32 // Microsoft Visual C++ defines a `mvn` macro that conflicts with our own
33 // definition.
34 #if defined(_MSC_VER) && defined(mvn)
35 #undef mvn
36 #endif
37 
38 namespace vixl {
39 
40 class CodeBufferCheckScope;
41 
42 namespace internal {
43 
44 class AssemblerBase {
45  public:
AssemblerBase()46   AssemblerBase() : allow_assembler_(true) {}
47 #ifdef PANDA_BUILD
48   AssemblerBase(size_t capacity) = delete;
49 #else
AssemblerBase(size_t capacity)50   explicit AssemblerBase(size_t capacity)
51       : buffer_(capacity), allow_assembler_(false) {}
52 #endif
AssemblerBase(byte* buffer, size_t capacity)53   AssemblerBase(byte* buffer, size_t capacity)
54       : buffer_(buffer, capacity), allow_assembler_(true) {}
55 
~AssemblerBase()56   virtual ~AssemblerBase() {}
57 
IsValid() const58   bool IsValid() const {
59     return buffer_.IsValid();
60   }
61 
62   // Finalize a code buffer of generated instructions. This function must be
63   // called before executing or copying code from the buffer.
FinalizeCode()64   void FinalizeCode() { GetBuffer()->SetClean(); }
65 
GetCursorOffset() const66   ptrdiff_t GetCursorOffset() const { return GetBuffer().GetCursorOffset(); }
67 
68   // Return the address of the cursor.
69   template <typename T>
GetCursorAddress() const70   T GetCursorAddress() const {
71     VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t));
72     return GetBuffer().GetOffsetAddress<T>(GetCursorOffset());
73   }
74 
GetSizeOfCodeGenerated() const75   size_t GetSizeOfCodeGenerated() const { return GetCursorOffset(); }
76 
77   // Accessors.
GetBuffer()78   CodeBuffer* GetBuffer() { return &buffer_; }
GetBuffer() const79   const CodeBuffer& GetBuffer() const { return buffer_; }
AllowAssembler() const80   bool AllowAssembler() const { return allow_assembler_; }
81 
82  protected:
SetAllowAssembler(bool allow)83   void SetAllowAssembler(bool allow) { allow_assembler_ = allow; }
84 
85   // CodeBufferCheckScope must be able to temporarily allow the assembler.
86   friend class vixl::CodeBufferCheckScope;
87 
88   // Buffer where the code is emitted.
89   CodeBuffer buffer_;
90 
91  private:
92   bool allow_assembler_;
93 
94  public:
95   // Deprecated public interface.
96 
97   // Return the address of an offset in the buffer.
98   template <typename T>
99   VIXL_DEPRECATED("GetBuffer().GetOffsetAddress<T>(offset)",
100                   T GetOffsetAddress(ptrdiff_t offset) const) {
101     return GetBuffer().GetOffsetAddress<T>(offset);
102   }
103 
104   // Return the address of the start of the buffer.
105   template <typename T>
106   VIXL_DEPRECATED("GetBuffer().GetStartAddress<T>()",
107                   T GetStartAddress() const) {
108     return GetBuffer().GetOffsetAddress<T>(0);
109   }
110 };
111 
112 }  // namespace internal
113 }  // namespace vixl
114 
115 #endif  // VIXL_ASSEMBLER_BASE_H
116