Alexandre Rames | 919e3fe | 2016-10-14 09:07:54 +0100 | [diff] [blame] | 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 | |
Anton Kirilov | 088b01f | 2022-09-27 14:27:38 +0100 | [diff] [blame] | 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 | |
Alexandre Rames | 919e3fe | 2016-10-14 09:07:54 +0100 | [diff] [blame] | 38 | namespace vixl { |
Pierre Langlois | f4ba40f | 2017-01-19 16:06:26 +0000 | [diff] [blame] | 39 | |
| 40 | class CodeBufferCheckScope; |
| 41 | |
Alexandre Rames | 919e3fe | 2016-10-14 09:07:54 +0100 | [diff] [blame] | 42 | namespace internal { |
| 43 | |
| 44 | class AssemblerBase { |
| 45 | public: |
Alexandre Rames | 9dd6fa3 | 2016-10-12 13:26:54 +0100 | [diff] [blame] | 46 | AssemblerBase() : allow_assembler_(false) {} |
| 47 | explicit AssemblerBase(size_t capacity) |
| 48 | : buffer_(capacity), allow_assembler_(false) {} |
| 49 | AssemblerBase(byte* buffer, size_t capacity) |
| 50 | : buffer_(buffer, capacity), allow_assembler_(false) {} |
| 51 | |
| 52 | virtual ~AssemblerBase() {} |
Alexandre Rames | 919e3fe | 2016-10-14 09:07:54 +0100 | [diff] [blame] | 53 | |
Alexandre Rames | 6a049f9 | 2016-09-21 14:55:05 +0100 | [diff] [blame] | 54 | // Finalize a code buffer of generated instructions. This function must be |
| 55 | // called before executing or copying code from the buffer. |
| 56 | void FinalizeCode() { GetBuffer()->SetClean(); } |
| 57 | |
| 58 | ptrdiff_t GetCursorOffset() const { return GetBuffer().GetCursorOffset(); } |
| 59 | |
| 60 | // Return the address of the cursor. |
| 61 | template <typename T> |
| 62 | T GetCursorAddress() const { |
| 63 | VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t)); |
| 64 | return GetBuffer().GetOffsetAddress<T>(GetCursorOffset()); |
| 65 | } |
| 66 | |
| 67 | size_t GetSizeOfCodeGenerated() const { return GetCursorOffset(); } |
| 68 | |
Alexandre Rames | 9dd6fa3 | 2016-10-12 13:26:54 +0100 | [diff] [blame] | 69 | // Accessors. |
Alexandre Rames | 919e3fe | 2016-10-14 09:07:54 +0100 | [diff] [blame] | 70 | CodeBuffer* GetBuffer() { return &buffer_; } |
| 71 | const CodeBuffer& GetBuffer() const { return buffer_; } |
Alexandre Rames | 9dd6fa3 | 2016-10-12 13:26:54 +0100 | [diff] [blame] | 72 | bool AllowAssembler() const { return allow_assembler_; } |
Alexandre Rames | 919e3fe | 2016-10-14 09:07:54 +0100 | [diff] [blame] | 73 | |
| 74 | protected: |
Pierre Langlois | f4ba40f | 2017-01-19 16:06:26 +0000 | [diff] [blame] | 75 | void SetAllowAssembler(bool allow) { allow_assembler_ = allow; } |
| 76 | |
| 77 | // CodeBufferCheckScope must be able to temporarily allow the assembler. |
| 78 | friend class vixl::CodeBufferCheckScope; |
| 79 | |
Alexandre Rames | 919e3fe | 2016-10-14 09:07:54 +0100 | [diff] [blame] | 80 | // Buffer where the code is emitted. |
| 81 | CodeBuffer buffer_; |
Alexandre Rames | 6a049f9 | 2016-09-21 14:55:05 +0100 | [diff] [blame] | 82 | |
Alexandre Rames | 9dd6fa3 | 2016-10-12 13:26:54 +0100 | [diff] [blame] | 83 | private: |
| 84 | bool allow_assembler_; |
| 85 | |
Alexandre Rames | 6a049f9 | 2016-09-21 14:55:05 +0100 | [diff] [blame] | 86 | public: |
| 87 | // Deprecated public interface. |
| 88 | |
| 89 | // Return the address of an offset in the buffer. |
| 90 | template <typename T> |
| 91 | VIXL_DEPRECATED("GetBuffer().GetOffsetAddress<T>(offset)", |
| 92 | T GetOffsetAddress(ptrdiff_t offset) const) { |
| 93 | return GetBuffer().GetOffsetAddress<T>(offset); |
| 94 | } |
| 95 | |
| 96 | // Return the address of the start of the buffer. |
| 97 | template <typename T> |
| 98 | VIXL_DEPRECATED("GetBuffer().GetStartAddress<T>()", |
| 99 | T GetStartAddress() const) { |
| 100 | return GetBuffer().GetOffsetAddress<T>(0); |
| 101 | } |
Alexandre Rames | 919e3fe | 2016-10-14 09:07:54 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | } // namespace internal |
| 105 | } // namespace vixl |
| 106 | |
| 107 | #endif // VIXL_ASSEMBLER_BASE_H |