1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/assembler-inl.h"
6 #include "src/macro-assembler.h"
7 
8 #include "src/compiler/linkage.h"
9 
10 #include "src/zone/zone.h"
11 
12 namespace v8 {
13 namespace internal {
14 namespace compiler {
15 
16 namespace {
17 
18 // Platform-specific configuration for C calling convention.
19 #if V8_TARGET_ARCH_IA32
20 // ===========================================================================
21 // == ia32 ===================================================================
22 // ===========================================================================
23 #define CALLEE_SAVE_REGISTERS esi.bit() | edi.bit() | ebx.bit()
24 
25 #elif V8_TARGET_ARCH_X64
26 // ===========================================================================
27 // == x64 ====================================================================
28 // ===========================================================================
29 
30 #ifdef _WIN64
31 // == x64 windows ============================================================
32 #define STACK_SHADOW_WORDS 4
33 #define PARAM_REGISTERS rcx, rdx, r8, r9
34 #define CALLEE_SAVE_REGISTERS                                             \
35   rbx.bit() | rdi.bit() | rsi.bit() | r12.bit() | r13.bit() | r14.bit() | \
36       r15.bit()
37 #define CALLEE_SAVE_FP_REGISTERS                                        \
38   (1 << xmm6.code()) | (1 << xmm7.code()) | (1 << xmm8.code()) |        \
39       (1 << xmm9.code()) | (1 << xmm10.code()) | (1 << xmm11.code()) |  \
40       (1 << xmm12.code()) | (1 << xmm13.code()) | (1 << xmm14.code()) | \
41       (1 << xmm15.code())
42 #else
43 // == x64 other ==============================================================
44 #define PARAM_REGISTERS rdi, rsi, rdx, rcx, r8, r9
45 #define CALLEE_SAVE_REGISTERS \
46   rbx.bit() | r12.bit() | r13.bit() | r14.bit() | r15.bit()
47 #endif
48 
49 #elif V8_TARGET_ARCH_ARM
50 // ===========================================================================
51 // == arm ====================================================================
52 // ===========================================================================
53 #define PARAM_REGISTERS r0, r1, r2, r3
54 #define CALLEE_SAVE_REGISTERS \
55   r4.bit() | r5.bit() | r6.bit() | r7.bit() | r8.bit() | r9.bit() | r10.bit()
56 #define CALLEE_SAVE_FP_REGISTERS                                  \
57   (1 << d8.code()) | (1 << d9.code()) | (1 << d10.code()) |       \
58       (1 << d11.code()) | (1 << d12.code()) | (1 << d13.code()) | \
59       (1 << d14.code()) | (1 << d15.code())
60 
61 
62 #elif V8_TARGET_ARCH_ARM64
63 // ===========================================================================
64 // == arm64 ====================================================================
65 // ===========================================================================
66 #define PARAM_REGISTERS x0, x1, x2, x3, x4, x5, x6, x7
67 #define CALLEE_SAVE_REGISTERS                                     \
68   (1 << x19.code()) | (1 << x20.code()) | (1 << x21.code()) |     \
69       (1 << x22.code()) | (1 << x23.code()) | (1 << x24.code()) | \
70       (1 << x25.code()) | (1 << x26.code()) | (1 << x27.code()) | \
71       (1 << x28.code()) | (1 << x29.code()) | (1 << x30.code())
72 
73 
74 #define CALLEE_SAVE_FP_REGISTERS                                  \
75   (1 << d8.code()) | (1 << d9.code()) | (1 << d10.code()) |       \
76       (1 << d11.code()) | (1 << d12.code()) | (1 << d13.code()) | \
77       (1 << d14.code()) | (1 << d15.code())
78 
79 #elif V8_TARGET_ARCH_MIPS
80 // ===========================================================================
81 // == mips ===================================================================
82 // ===========================================================================
83 #define STACK_SHADOW_WORDS 4
84 #define PARAM_REGISTERS a0, a1, a2, a3
85 #define CALLEE_SAVE_REGISTERS                                                  \
86   s0.bit() | s1.bit() | s2.bit() | s3.bit() | s4.bit() | s5.bit() | s6.bit() | \
87       s7.bit()
88 #define CALLEE_SAVE_FP_REGISTERS \
89   f20.bit() | f22.bit() | f24.bit() | f26.bit() | f28.bit() | f30.bit()
90 
91 #elif V8_TARGET_ARCH_MIPS64
92 // ===========================================================================
93 // == mips64 =================================================================
94 // ===========================================================================
95 #define PARAM_REGISTERS a0, a1, a2, a3, a4, a5, a6, a7
96 #define CALLEE_SAVE_REGISTERS                                                  \
97   s0.bit() | s1.bit() | s2.bit() | s3.bit() | s4.bit() | s5.bit() | s6.bit() | \
98       s7.bit()
99 #define CALLEE_SAVE_FP_REGISTERS \
100   f20.bit() | f22.bit() | f24.bit() | f26.bit() | f28.bit() | f30.bit()
101 
102 #elif V8_TARGET_ARCH_PPC || V8_TARGET_ARCH_PPC64
103 // ===========================================================================
104 // == ppc & ppc64 ============================================================
105 // ===========================================================================
106 #define PARAM_REGISTERS r3, r4, r5, r6, r7, r8, r9, r10
107 #define CALLEE_SAVE_REGISTERS                                                 \
108   r14.bit() | r15.bit() | r16.bit() | r17.bit() | r18.bit() | r19.bit() |     \
109       r20.bit() | r21.bit() | r22.bit() | r23.bit() | r24.bit() | r25.bit() | \
110       r26.bit() | r27.bit() | r28.bit() | r29.bit() | r30.bit()
111 #define CALLEE_SAVE_FP_REGISTERS                                              \
112   d14.bit() | d15.bit() | d16.bit() | d17.bit() | d18.bit() | d19.bit() |     \
113       d20.bit() | d21.bit() | d22.bit() | d23.bit() | d24.bit() | d25.bit() | \
114       d26.bit() | d27.bit() | d28.bit() | d29.bit() | d30.bit() | d31.bit()
115 
116 #elif V8_TARGET_ARCH_S390X
117 // ===========================================================================
118 // == s390x ==================================================================
119 // ===========================================================================
120 #define PARAM_REGISTERS r2, r3, r4, r5, r6
121 #define CALLEE_SAVE_REGISTERS \
122   r6.bit() | r7.bit() | r8.bit() | r9.bit() | r10.bit() | ip.bit() | r13.bit()
123 #define CALLEE_SAVE_FP_REGISTERS                                        \
124   d8.bit() | d9.bit() | d10.bit() | d11.bit() | d12.bit() | d13.bit() | \
125       d14.bit() | d15.bit()
126 
127 #elif V8_TARGET_ARCH_S390
128 // ===========================================================================
129 // == s390 ===================================================================
130 // ===========================================================================
131 #define PARAM_REGISTERS r2, r3, r4, r5, r6
132 #define CALLEE_SAVE_REGISTERS \
133   r6.bit() | r7.bit() | r8.bit() | r9.bit() | r10.bit() | ip.bit() | r13.bit()
134 #define CALLEE_SAVE_FP_REGISTERS (d4.bit() | d6.bit())
135 
136 #else
137 // ===========================================================================
138 // == unknown ================================================================
139 // ===========================================================================
140 #define UNSUPPORTED_C_LINKAGE 1
141 #endif
142 }  // namespace
143 
144 
145 // General code uses the above configuration data.
GetSimplifiedCDescriptor(Zone * zone,const MachineSignature * msig,bool set_initialize_root_flag)146 CallDescriptor* Linkage::GetSimplifiedCDescriptor(
147     Zone* zone, const MachineSignature* msig, bool set_initialize_root_flag) {
148   DCHECK_LE(msig->parameter_count(), static_cast<size_t>(kMaxCParameters));
149 
150   LocationSignature::Builder locations(zone, msig->return_count(),
151                                        msig->parameter_count());
152   // Check the types of the signature.
153   // Currently no floating point parameters or returns are allowed because
154   // on ia32, the FP top of stack is involved.
155   for (size_t i = 0; i < msig->return_count(); i++) {
156     MachineRepresentation rep = msig->GetReturn(i).representation();
157     CHECK_NE(MachineRepresentation::kFloat32, rep);
158     CHECK_NE(MachineRepresentation::kFloat64, rep);
159   }
160   for (size_t i = 0; i < msig->parameter_count(); i++) {
161     MachineRepresentation rep = msig->GetParam(i).representation();
162     CHECK_NE(MachineRepresentation::kFloat32, rep);
163     CHECK_NE(MachineRepresentation::kFloat64, rep);
164   }
165 
166 #ifdef UNSUPPORTED_C_LINKAGE
167   // This method should not be called on unknown architectures.
168   FATAL("requested C call descriptor on unsupported architecture");
169   return nullptr;
170 #endif
171 
172   // Add return location(s).
173   CHECK_GE(2, locations.return_count_);
174 
175   if (locations.return_count_ > 0) {
176     locations.AddReturn(LinkageLocation::ForRegister(kReturnRegister0.code(),
177                                                      msig->GetReturn(0)));
178   }
179   if (locations.return_count_ > 1) {
180     locations.AddReturn(LinkageLocation::ForRegister(kReturnRegister1.code(),
181                                                      msig->GetReturn(1)));
182   }
183 
184   const int parameter_count = static_cast<int>(msig->parameter_count());
185 
186 #ifdef PARAM_REGISTERS
187   const v8::internal::Register kParamRegisters[] = {PARAM_REGISTERS};
188   const int kParamRegisterCount = static_cast<int>(arraysize(kParamRegisters));
189 #else
190   const v8::internal::Register* kParamRegisters = nullptr;
191   const int kParamRegisterCount = 0;
192 #endif
193 
194 #ifdef STACK_SHADOW_WORDS
195   int stack_offset = STACK_SHADOW_WORDS;
196 #else
197   int stack_offset = 0;
198 #endif
199   // Add register and/or stack parameter(s).
200   for (int i = 0; i < parameter_count; i++) {
201     if (i < kParamRegisterCount) {
202       locations.AddParam(LinkageLocation::ForRegister(kParamRegisters[i].code(),
203                                                       msig->GetParam(i)));
204     } else {
205       locations.AddParam(LinkageLocation::ForCallerFrameSlot(
206           -1 - stack_offset, msig->GetParam(i)));
207       stack_offset++;
208     }
209   }
210 
211 #ifdef CALLEE_SAVE_REGISTERS
212   const RegList kCalleeSaveRegisters = CALLEE_SAVE_REGISTERS;
213 #else
214   const RegList kCalleeSaveRegisters = 0;
215 #endif
216 
217 #ifdef CALLEE_SAVE_FP_REGISTERS
218   const RegList kCalleeSaveFPRegisters = CALLEE_SAVE_FP_REGISTERS;
219 #else
220   const RegList kCalleeSaveFPRegisters = 0;
221 #endif
222 
223   // The target for C calls is always an address (i.e. machine pointer).
224   MachineType target_type = MachineType::Pointer();
225   LinkageLocation target_loc = LinkageLocation::ForAnyRegister(target_type);
226   CallDescriptor::Flags flags = CallDescriptor::kNoFlags;
227   if (set_initialize_root_flag) {
228     flags |= CallDescriptor::kInitializeRootRegister;
229   }
230 
231   return new (zone) CallDescriptor(  // --
232       CallDescriptor::kCallAddress,  // kind
233       target_type,                   // target MachineType
234       target_loc,                    // target location
235       locations.Build(),             // location_sig
236       0,                             // stack_parameter_count
237       Operator::kNoThrow,            // properties
238       kCalleeSaveRegisters,          // callee-saved registers
239       kCalleeSaveFPRegisters,        // callee-saved fp regs
240       flags, "c-call");
241 }
242 
243 }  // namespace compiler
244 }  // namespace internal
245 }  // namespace v8
246