blob: 52a35c74cafc703a9bd1bb2a783fd7222a790b73 [file] [log] [blame]
Alexandre Rames919e3fe2016-10-14 09:07:54 +01001// 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
32namespace vixl {
33namespace internal {
34
35class AssemblerBase {
36 public:
37 AssemblerBase() {}
38 explicit AssemblerBase(size_t capacity) : buffer_(capacity) {}
39 AssemblerBase(byte* buffer, size_t capacity) : buffer_(buffer, capacity) {}
40
Alexandre Rames6a049f92016-09-21 14:55:05 +010041 // Finalize a code buffer of generated instructions. This function must be
42 // called before executing or copying code from the buffer.
43 void FinalizeCode() { GetBuffer()->SetClean(); }
44
45 ptrdiff_t GetCursorOffset() const { return GetBuffer().GetCursorOffset(); }
46
47 // Return the address of the cursor.
48 template <typename T>
49 T GetCursorAddress() const {
50 VIXL_STATIC_ASSERT(sizeof(T) >= sizeof(uintptr_t));
51 return GetBuffer().GetOffsetAddress<T>(GetCursorOffset());
52 }
53
54 size_t GetSizeOfCodeGenerated() const { return GetCursorOffset(); }
55
Alexandre Rames919e3fe2016-10-14 09:07:54 +010056 CodeBuffer* GetBuffer() { return &buffer_; }
57 const CodeBuffer& GetBuffer() const { return buffer_; }
58
59 protected:
60 // Buffer where the code is emitted.
61 CodeBuffer buffer_;
Alexandre Rames6a049f92016-09-21 14:55:05 +010062
63 public:
64 // Deprecated public interface.
65
66 // Return the address of an offset in the buffer.
67 template <typename T>
68 VIXL_DEPRECATED("GetBuffer().GetOffsetAddress<T>(offset)",
69 T GetOffsetAddress(ptrdiff_t offset) const) {
70 return GetBuffer().GetOffsetAddress<T>(offset);
71 }
72
73 // Return the address of the start of the buffer.
74 template <typename T>
75 VIXL_DEPRECATED("GetBuffer().GetStartAddress<T>()",
76 T GetStartAddress() const) {
77 return GetBuffer().GetOffsetAddress<T>(0);
78 }
Alexandre Rames919e3fe2016-10-14 09:07:54 +010079};
80
81} // namespace internal
82} // namespace vixl
83
84#endif // VIXL_ASSEMBLER_BASE_H