1a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// Copyright 2016, VIXL authors
2a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// All rights reserved.
3a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames//
4a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// Redistribution and use in source and binary forms, with or without
5a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// modification, are permitted provided that the following conditions are met:
6a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames//
7a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames//   * Redistributions of source code must retain the above copyright notice,
8a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames//     this list of conditions and the following disclaimer.
9a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames//   * Redistributions in binary form must reproduce the above copyright notice,
10a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames//     this list of conditions and the following disclaimer in the documentation
11a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames//     and/or other materials provided with the distribution.
12a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames//   * Neither the name of ARM Limited nor the names of its contributors may be
13a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames//     used to endorse or promote products derived from this software without
14a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames//     specific prior written permission.
15a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames//
16a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
27a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// The ABI features are only supported with C++11 or later.
28a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames#if __cplusplus >= 201103L
29a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// This should not be defined manually.
30a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames#define VIXL_HAS_ABI_SUPPORT
31a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames#elif defined(VIXL_HAS_ABI_SUPPORT)
32a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames#error "The ABI support requires C++11 or later."
33a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames#endif
34a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
35a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames#ifdef VIXL_HAS_ABI_SUPPORT
36a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
37a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames#ifndef VIXL_AARCH64_ABI_AARCH64_H_
38a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames#define VIXL_AARCH64_ABI_AARCH64_H_
39a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
40a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames#include <algorithm>
41a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames#include <type_traits>
42a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
43b49bdb7996e603555eba4c8b56c7325e3e737ab6Alexandre Rames#include "../globals-vixl.h"
44a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
45b49bdb7996e603555eba4c8b56c7325e3e737ab6Alexandre Rames#include "instructions-aarch64.h"
46b49bdb7996e603555eba4c8b56c7325e3e737ab6Alexandre Rames#include "operands-aarch64.h"
47a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
48a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Ramesnamespace vixl {
49a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Ramesnamespace aarch64 {
50a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
51a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// Class describing the AArch64 procedure call standard, as defined in "ARM
52a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// Procedure Call Standard for the ARM 64-bit Architecture (AArch64)",
53a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// release 1.0 (AAPCS below).
54a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames//
55a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// The stages in the comments match the description in that document.
56a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames//
57a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames// Stage B does not apply to arguments handled by this class.
58a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Ramesclass ABI {
59a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames public:
60a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  explicit ABI(Register stack_pointer = sp) : stack_pointer_(stack_pointer) {
61a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // Stage A - Initialization
62a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    Reset();
63a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  }
64a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
65a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  void Reset() {
66a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    NGRN_ = 0;
67a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    NSRN_ = 0;
68a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    stack_offset_ = 0;
69a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  }
70a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
71a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  int GetStackSpaceRequired() { return stack_offset_; }
72a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
73a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  // The logic is described in section 5.5 of the AAPCS.
74a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  template <typename T>
75a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  GenericOperand GetReturnGenericOperand() const {
76a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    ABI abi(stack_pointer_);
77a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    GenericOperand result = abi.GetNextParameterGenericOperand<T>();
78a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    VIXL_ASSERT(result.IsCPURegister());
79a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    return result;
80a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  }
81a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
82a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  // The logic is described in section 5.4.2 of the AAPCS.
83a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  // The `GenericOperand` returned describes the location reserved for the
84a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  // argument from the point of view of the callee.
85a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  template <typename T>
86a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  GenericOperand GetNextParameterGenericOperand() {
87a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    const bool is_floating_point_type = std::is_floating_point<T>::value;
88a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    const bool is_integral_type = std::is_integral<T>::value;
89a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    const bool is_pointer_type = std::is_pointer<T>::value;
9003b5c8da537b525f20414f3f5280948691eafb7eAlexandre Rames    int type_alignment = std::alignment_of<T>::value;
91a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
92a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // We only support basic types.
93a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    VIXL_ASSERT(is_floating_point_type || is_integral_type || is_pointer_type);
94a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
95a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // To ensure we get the correct type of operand when simulating on a 32-bit
96a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // host, force the size of pointer types to the native AArch64 pointer size.
97a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    unsigned size = is_pointer_type ? 8 : sizeof(T);
98a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // The size of the 'operand' reserved for the argument.
99a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    unsigned operand_size = AlignUp(size, kWRegSizeInBytes);
100a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    if (size > 8) {
101a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames      VIXL_UNIMPLEMENTED();
102a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames      return GenericOperand();
103a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    }
104a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
105a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // Stage C.1
106a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    if (is_floating_point_type && (NSRN_ < 8)) {
107a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames      return GenericOperand(FPRegister(NSRN_++, size * kBitsPerByte));
108a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    }
109a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // Stages C.2, C.3, and C.4: Unsupported. Caught by the assertions above.
110a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // Stages C.5 and C.6
111a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    if (is_floating_point_type) {
112a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames      VIXL_STATIC_ASSERT(
113a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames          !is_floating_point_type ||
114a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames          (std::is_same<T, float>::value || std::is_same<T, double>::value));
115a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames      int offset = stack_offset_;
116a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames      stack_offset_ += 8;
117a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames      return GenericOperand(MemOperand(stack_pointer_, offset), operand_size);
118a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    }
119a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // Stage C.7
120a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    if ((is_integral_type || is_pointer_type) && (size <= 8) && (NGRN_ < 8)) {
121a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames      return GenericOperand(Register(NGRN_++, operand_size * kBitsPerByte));
122a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    }
123a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // Stage C.8
124a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    if (type_alignment == 16) {
125a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames      NGRN_ = AlignUp(NGRN_, 2);
126a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    }
127a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // Stage C.9
128a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    if (is_integral_type && (size == 16) && (NGRN_ < 7)) {
129a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames      VIXL_UNIMPLEMENTED();
130a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames      return GenericOperand();
131a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    }
132a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // Stage C.10: Unsupported. Caught by the assertions above.
133a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // Stage C.11
134a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    NGRN_ = 8;
135a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // Stage C.12
13603b5c8da537b525f20414f3f5280948691eafb7eAlexandre Rames    stack_offset_ = AlignUp(stack_offset_, std::max(type_alignment, 8));
137a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // Stage C.13: Unsupported. Caught by the assertions above.
138a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    // Stage C.14
139a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    VIXL_ASSERT(size <= 8u);
140a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    size = std::max(size, 8u);
141a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    int offset = stack_offset_;
142a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    stack_offset_ += size;
143a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames    return GenericOperand(MemOperand(stack_pointer_, offset), operand_size);
144a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  }
145a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
146a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames private:
147a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  Register stack_pointer_;
148a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  // Next General-purpose Register Number.
149a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  int NGRN_;
150a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  // Next SIMD and Floating-point Register Number.
151a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  int NSRN_;
152a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  // The acronym "NSAA" used in the standard refers to the "Next Stacked
153a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  // Argument Address". Here we deal with offsets from the stack pointer.
154a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  int stack_offset_;
155a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames};
156a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
157a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Ramestemplate <>
158a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Ramesinline GenericOperand ABI::GetReturnGenericOperand<void>() const {
159a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames  return GenericOperand();
160a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames}
161a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames}
162a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames}  // namespace vixl::aarch64
163a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
164a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames#endif  // VIXL_AARCH64_ABI_AARCH64_H_
165a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames
166a9e580275aa98958d8712e8e7cb79481ac83001fAlexandre Rames#endif  // VIXL_HAS_ABI_SUPPORT
167