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// The ABI features are only supported with C++11 or later.
28#if __cplusplus >= 201103L
29// This should not be defined manually.
30#define VIXL_HAS_ABI_SUPPORT
31#elif defined(VIXL_HAS_ABI_SUPPORT)
32#error "The ABI support requires C++11 or later."
33#endif
34
35#ifdef VIXL_HAS_ABI_SUPPORT
36
37#ifndef VIXL_AARCH64_ABI_AARCH64_H_
38#define VIXL_AARCH64_ABI_AARCH64_H_
39
40#include <algorithm>
41#include <type_traits>
42
43#include "../globals-vixl.h"
44
45#include "instructions-aarch64.h"
46#include "operands-aarch64.h"
47
48namespace vixl {
49namespace aarch64 {
50
51// Class describing the AArch64 procedure call standard, as defined in "ARM
52// Procedure Call Standard for the ARM 64-bit Architecture (AArch64)",
53// release 1.0 (AAPCS below).
54//
55// The stages in the comments match the description in that document.
56//
57// Stage B does not apply to arguments handled by this class.
58class ABI {
59 public:
60  explicit ABI(Register stack_pointer = sp) : stack_pointer_(stack_pointer) {
61    // Stage A - Initialization
62    Reset();
63  }
64
65  void Reset() {
66    NGRN_ = 0;
67    NSRN_ = 0;
68    stack_offset_ = 0;
69  }
70
71  int GetStackSpaceRequired() { return stack_offset_; }
72
73  // The logic is described in section 5.5 of the AAPCS.
74  template <typename T>
75  GenericOperand GetReturnGenericOperand() const {
76    ABI abi(stack_pointer_);
77    GenericOperand result = abi.GetNextParameterGenericOperand<T>();
78    VIXL_ASSERT(result.IsCPURegister());
79    return result;
80  }
81
82  // The logic is described in section 5.4.2 of the AAPCS.
83  // The `GenericOperand` returned describes the location reserved for the
84  // argument from the point of view of the callee.
85  template <typename T>
86  GenericOperand GetNextParameterGenericOperand() {
87    const bool is_floating_point_type = std::is_floating_point<T>::value;
88    const bool is_integral_type = std::is_integral<T>::value;
89    const bool is_pointer_type = std::is_pointer<T>::value;
90    int type_alignment = std::alignment_of<T>::value;
91
92    // We only support basic types.
93    VIXL_ASSERT(is_floating_point_type || is_integral_type || is_pointer_type);
94
95    // To ensure we get the correct type of operand when simulating on a 32-bit
96    // host, force the size of pointer types to the native AArch64 pointer size.
97    unsigned size = is_pointer_type ? 8 : sizeof(T);
98    // The size of the 'operand' reserved for the argument.
99    unsigned operand_size = AlignUp(size, kWRegSizeInBytes);
100    if (size > 8) {
101      VIXL_UNIMPLEMENTED();
102      return GenericOperand();
103    }
104
105    // Stage C.1
106    if (is_floating_point_type && (NSRN_ < 8)) {
107      return GenericOperand(FPRegister(NSRN_++, size * kBitsPerByte));
108    }
109    // Stages C.2, C.3, and C.4: Unsupported. Caught by the assertions above.
110    // Stages C.5 and C.6
111    if (is_floating_point_type) {
112      VIXL_STATIC_ASSERT(
113          !is_floating_point_type ||
114          (std::is_same<T, float>::value || std::is_same<T, double>::value));
115      int offset = stack_offset_;
116      stack_offset_ += 8;
117      return GenericOperand(MemOperand(stack_pointer_, offset), operand_size);
118    }
119    // Stage C.7
120    if ((is_integral_type || is_pointer_type) && (size <= 8) && (NGRN_ < 8)) {
121      return GenericOperand(Register(NGRN_++, operand_size * kBitsPerByte));
122    }
123    // Stage C.8
124    if (type_alignment == 16) {
125      NGRN_ = AlignUp(NGRN_, 2);
126    }
127    // Stage C.9
128    if (is_integral_type && (size == 16) && (NGRN_ < 7)) {
129      VIXL_UNIMPLEMENTED();
130      return GenericOperand();
131    }
132    // Stage C.10: Unsupported. Caught by the assertions above.
133    // Stage C.11
134    NGRN_ = 8;
135    // Stage C.12
136    stack_offset_ = AlignUp(stack_offset_, std::max(type_alignment, 8));
137    // Stage C.13: Unsupported. Caught by the assertions above.
138    // Stage C.14
139    VIXL_ASSERT(size <= 8u);
140    size = std::max(size, 8u);
141    int offset = stack_offset_;
142    stack_offset_ += size;
143    return GenericOperand(MemOperand(stack_pointer_, offset), operand_size);
144  }
145
146 private:
147  Register stack_pointer_;
148  // Next General-purpose Register Number.
149  int NGRN_;
150  // Next SIMD and Floating-point Register Number.
151  int NSRN_;
152  // The acronym "NSAA" used in the standard refers to the "Next Stacked
153  // Argument Address". Here we deal with offsets from the stack pointer.
154  int stack_offset_;
155};
156
157template <>
158inline GenericOperand ABI::GetReturnGenericOperand<void>() const {
159  return GenericOperand();
160}
161}
162}  // namespace vixl::aarch64
163
164#endif  // VIXL_AARCH64_ABI_AARCH64_H_
165
166#endif  // VIXL_HAS_ABI_SUPPORT
167