Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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.h" |
| 6 | #include "src/macro-assembler.h" |
| 7 | |
| 8 | #include "src/wasm/wasm-module.h" |
| 9 | |
| 10 | #include "src/compiler/linkage.h" |
| 11 | |
| 12 | #include "src/zone.h" |
| 13 | |
| 14 | namespace v8 { |
| 15 | namespace internal { |
| 16 | // TODO(titzer): this should not be in the WASM namespace. |
| 17 | namespace wasm { |
| 18 | |
| 19 | using compiler::LocationSignature; |
| 20 | using compiler::CallDescriptor; |
| 21 | using compiler::LinkageLocation; |
| 22 | |
| 23 | namespace { |
| 24 | MachineType MachineTypeFor(LocalType type) { |
| 25 | switch (type) { |
| 26 | case kAstI32: |
| 27 | return MachineType::Int32(); |
| 28 | case kAstI64: |
| 29 | return MachineType::Int64(); |
| 30 | case kAstF64: |
| 31 | return MachineType::Float64(); |
| 32 | case kAstF32: |
| 33 | return MachineType::Float32(); |
| 34 | default: |
| 35 | UNREACHABLE(); |
| 36 | return MachineType::AnyTagged(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | |
| 41 | // Platform-specific configuration for C calling convention. |
| 42 | LinkageLocation regloc(Register reg) { |
| 43 | return LinkageLocation::ForRegister(reg.code()); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | LinkageLocation regloc(DoubleRegister reg) { |
| 48 | return LinkageLocation::ForRegister(reg.code()); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | LinkageLocation stackloc(int i) { |
| 53 | return LinkageLocation::ForCallerFrameSlot(i); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | #if V8_TARGET_ARCH_IA32 |
| 58 | // =========================================================================== |
| 59 | // == ia32 =================================================================== |
| 60 | // =========================================================================== |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 61 | #define GP_PARAM_REGISTERS eax, edx, ecx, ebx, esi |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 62 | #define GP_RETURN_REGISTERS eax, edx |
| 63 | #define FP_PARAM_REGISTERS xmm1, xmm2, xmm3, xmm4, xmm5, xmm6 |
| 64 | #define FP_RETURN_REGISTERS xmm1, xmm2 |
| 65 | |
| 66 | #elif V8_TARGET_ARCH_X64 |
| 67 | // =========================================================================== |
| 68 | // == x64 ==================================================================== |
| 69 | // =========================================================================== |
| 70 | #define GP_PARAM_REGISTERS rax, rdx, rcx, rbx, rsi, rdi |
| 71 | #define GP_RETURN_REGISTERS rax, rdx |
| 72 | #define FP_PARAM_REGISTERS xmm1, xmm2, xmm3, xmm4, xmm5, xmm6 |
| 73 | #define FP_RETURN_REGISTERS xmm1, xmm2 |
| 74 | |
| 75 | #elif V8_TARGET_ARCH_X87 |
| 76 | // =========================================================================== |
| 77 | // == x87 ==================================================================== |
| 78 | // =========================================================================== |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 79 | #define GP_PARAM_REGISTERS eax, edx, ecx, ebx, esi |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 80 | #define GP_RETURN_REGISTERS eax, edx |
| 81 | #define FP_RETURN_REGISTERS stX_0 |
| 82 | |
| 83 | #elif V8_TARGET_ARCH_ARM |
| 84 | // =========================================================================== |
| 85 | // == arm ==================================================================== |
| 86 | // =========================================================================== |
| 87 | #define GP_PARAM_REGISTERS r0, r1, r2, r3 |
| 88 | #define GP_RETURN_REGISTERS r0, r1 |
| 89 | #define FP_PARAM_REGISTERS d0, d1, d2, d3, d4, d5, d6, d7 |
| 90 | #define FP_RETURN_REGISTERS d0, d1 |
| 91 | |
| 92 | #elif V8_TARGET_ARCH_ARM64 |
| 93 | // =========================================================================== |
| 94 | // == arm64 ==================================================================== |
| 95 | // =========================================================================== |
| 96 | #define GP_PARAM_REGISTERS x0, x1, x2, x3, x4, x5, x6, x7 |
| 97 | #define GP_RETURN_REGISTERS x0, x1 |
| 98 | #define FP_PARAM_REGISTERS d0, d1, d2, d3, d4, d5, d6, d7 |
| 99 | #define FP_RETURN_REGISTERS d0, d1 |
| 100 | |
| 101 | #elif V8_TARGET_ARCH_MIPS |
| 102 | // =========================================================================== |
| 103 | // == mips =================================================================== |
| 104 | // =========================================================================== |
| 105 | #define GP_PARAM_REGISTERS a0, a1, a2, a3 |
| 106 | #define GP_RETURN_REGISTERS v0, v1 |
| 107 | #define FP_PARAM_REGISTERS f2, f4, f6, f8, f10, f12, f14 |
| 108 | #define FP_RETURN_REGISTERS f2, f4 |
| 109 | |
| 110 | #elif V8_TARGET_ARCH_MIPS64 |
| 111 | // =========================================================================== |
| 112 | // == mips64 ================================================================= |
| 113 | // =========================================================================== |
| 114 | #define GP_PARAM_REGISTERS a0, a1, a2, a3, a4, a5, a6, a7 |
| 115 | #define GP_RETURN_REGISTERS v0, v1 |
| 116 | #define FP_PARAM_REGISTERS f2, f4, f6, f8, f10, f12, f14 |
| 117 | #define FP_RETURN_REGISTERS f2, f4 |
| 118 | |
| 119 | #elif V8_TARGET_ARCH_PPC || V8_TARGET_ARCH_PPC64 |
| 120 | // =========================================================================== |
| 121 | // == ppc & ppc64 ============================================================ |
| 122 | // =========================================================================== |
| 123 | #define GP_PARAM_REGISTERS r3, r4, r5, r6, r7, r8, r9, r10 |
| 124 | #define GP_RETURN_REGISTERS r3, r4 |
| 125 | #define FP_PARAM_REGISTERS d1, d2, d3, d4, d5, d6, d7, d8 |
| 126 | #define FP_RETURN_REGISTERS d1, d2 |
| 127 | |
| 128 | #else |
| 129 | // =========================================================================== |
| 130 | // == unknown ================================================================ |
| 131 | // =========================================================================== |
| 132 | // Don't define anything. We'll just always use the stack. |
| 133 | #endif |
| 134 | |
| 135 | |
| 136 | // Helper for allocating either an GP or FP reg, or the next stack slot. |
| 137 | struct Allocator { |
| 138 | Allocator(const Register* gp, int gpc, const DoubleRegister* fp, int fpc) |
| 139 | : gp_count(gpc), |
| 140 | gp_offset(0), |
| 141 | gp_regs(gp), |
| 142 | fp_count(fpc), |
| 143 | fp_offset(0), |
| 144 | fp_regs(fp), |
| 145 | stack_offset(0) {} |
| 146 | |
| 147 | int gp_count; |
| 148 | int gp_offset; |
| 149 | const Register* gp_regs; |
| 150 | |
| 151 | int fp_count; |
| 152 | int fp_offset; |
| 153 | const DoubleRegister* fp_regs; |
| 154 | |
| 155 | int stack_offset; |
| 156 | |
| 157 | LinkageLocation Next(LocalType type) { |
| 158 | if (IsFloatingPoint(type)) { |
| 159 | // Allocate a floating point register/stack location. |
| 160 | if (fp_offset < fp_count) { |
| 161 | return regloc(fp_regs[fp_offset++]); |
| 162 | } else { |
| 163 | int offset = -1 - stack_offset; |
| 164 | stack_offset += Words(type); |
| 165 | return stackloc(offset); |
| 166 | } |
| 167 | } else { |
| 168 | // Allocate a general purpose register/stack location. |
| 169 | if (gp_offset < gp_count) { |
| 170 | return regloc(gp_regs[gp_offset++]); |
| 171 | } else { |
| 172 | int offset = -1 - stack_offset; |
| 173 | stack_offset += Words(type); |
| 174 | return stackloc(offset); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | bool IsFloatingPoint(LocalType type) { |
| 179 | return type == kAstF32 || type == kAstF64; |
| 180 | } |
| 181 | int Words(LocalType type) { |
| 182 | // The code generation for pushing parameters on the stack does not |
| 183 | // distinguish between float32 and float64. Therefore also float32 needs |
| 184 | // two words. |
| 185 | if (kPointerSize < 8 && |
| 186 | (type == kAstI64 || type == kAstF64 || type == kAstF32)) { |
| 187 | return 2; |
| 188 | } |
| 189 | return 1; |
| 190 | } |
| 191 | }; |
| 192 | } // namespace |
| 193 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 194 | static Allocator GetReturnRegisters() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 195 | #ifdef GP_RETURN_REGISTERS |
| 196 | static const Register kGPReturnRegisters[] = {GP_RETURN_REGISTERS}; |
| 197 | static const int kGPReturnRegistersCount = |
| 198 | static_cast<int>(arraysize(kGPReturnRegisters)); |
| 199 | #else |
| 200 | static const Register* kGPReturnRegisters = nullptr; |
| 201 | static const int kGPReturnRegistersCount = 0; |
| 202 | #endif |
| 203 | |
| 204 | #ifdef FP_RETURN_REGISTERS |
| 205 | static const DoubleRegister kFPReturnRegisters[] = {FP_RETURN_REGISTERS}; |
| 206 | static const int kFPReturnRegistersCount = |
| 207 | static_cast<int>(arraysize(kFPReturnRegisters)); |
| 208 | #else |
| 209 | static const DoubleRegister* kFPReturnRegisters = nullptr; |
| 210 | static const int kFPReturnRegistersCount = 0; |
| 211 | #endif |
| 212 | |
| 213 | Allocator rets(kGPReturnRegisters, kGPReturnRegistersCount, |
| 214 | kFPReturnRegisters, kFPReturnRegistersCount); |
| 215 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 216 | return rets; |
| 217 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 218 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 219 | static Allocator GetParameterRegisters() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 220 | #ifdef GP_PARAM_REGISTERS |
| 221 | static const Register kGPParamRegisters[] = {GP_PARAM_REGISTERS}; |
| 222 | static const int kGPParamRegistersCount = |
| 223 | static_cast<int>(arraysize(kGPParamRegisters)); |
| 224 | #else |
| 225 | static const Register* kGPParamRegisters = nullptr; |
| 226 | static const int kGPParamRegistersCount = 0; |
| 227 | #endif |
| 228 | |
| 229 | #ifdef FP_PARAM_REGISTERS |
| 230 | static const DoubleRegister kFPParamRegisters[] = {FP_PARAM_REGISTERS}; |
| 231 | static const int kFPParamRegistersCount = |
| 232 | static_cast<int>(arraysize(kFPParamRegisters)); |
| 233 | #else |
| 234 | static const DoubleRegister* kFPParamRegisters = nullptr; |
| 235 | static const int kFPParamRegistersCount = 0; |
| 236 | #endif |
| 237 | |
| 238 | Allocator params(kGPParamRegisters, kGPParamRegistersCount, kFPParamRegisters, |
| 239 | kFPParamRegistersCount); |
| 240 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 241 | return params; |
| 242 | } |
| 243 | |
| 244 | // General code uses the above configuration data. |
| 245 | CallDescriptor* ModuleEnv::GetWasmCallDescriptor(Zone* zone, |
| 246 | FunctionSig* fsig) { |
| 247 | MachineSignature::Builder msig(zone, fsig->return_count(), |
| 248 | fsig->parameter_count()); |
| 249 | LocationSignature::Builder locations(zone, fsig->return_count(), |
| 250 | fsig->parameter_count()); |
| 251 | |
| 252 | Allocator rets = GetReturnRegisters(); |
| 253 | |
| 254 | // Add return location(s). |
| 255 | const int return_count = static_cast<int>(locations.return_count_); |
| 256 | for (int i = 0; i < return_count; i++) { |
| 257 | LocalType ret = fsig->GetReturn(i); |
| 258 | msig.AddReturn(MachineTypeFor(ret)); |
| 259 | locations.AddReturn(rets.Next(ret)); |
| 260 | } |
| 261 | |
| 262 | Allocator params = GetParameterRegisters(); |
| 263 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 264 | // Add register and/or stack parameter(s). |
| 265 | const int parameter_count = static_cast<int>(fsig->parameter_count()); |
| 266 | for (int i = 0; i < parameter_count; i++) { |
| 267 | LocalType param = fsig->GetParam(i); |
| 268 | msig.AddParam(MachineTypeFor(param)); |
| 269 | locations.AddParam(params.Next(param)); |
| 270 | } |
| 271 | |
| 272 | const RegList kCalleeSaveRegisters = 0; |
| 273 | const RegList kCalleeSaveFPRegisters = 0; |
| 274 | |
| 275 | // The target for WASM calls is always a code object. |
| 276 | MachineType target_type = MachineType::AnyTagged(); |
| 277 | LinkageLocation target_loc = LinkageLocation::ForAnyRegister(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 278 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 279 | return new (zone) CallDescriptor( // -- |
| 280 | CallDescriptor::kCallCodeObject, // kind |
| 281 | target_type, // target MachineType |
| 282 | target_loc, // target location |
| 283 | msig.Build(), // machine_sig |
| 284 | locations.Build(), // location_sig |
| 285 | params.stack_offset, // stack_parameter_count |
| 286 | compiler::Operator::kNoProperties, // properties |
| 287 | kCalleeSaveRegisters, // callee-saved registers |
| 288 | kCalleeSaveFPRegisters, // callee-saved fp regs |
| 289 | CallDescriptor::kUseNativeStack, // flags |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 290 | "wasm-call"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 291 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 292 | |
| 293 | CallDescriptor* ModuleEnv::GetI32WasmCallDescriptor( |
| 294 | Zone* zone, CallDescriptor* descriptor) { |
| 295 | const MachineSignature* signature = descriptor->GetMachineSignature(); |
| 296 | size_t parameter_count = signature->parameter_count(); |
| 297 | size_t return_count = signature->return_count(); |
| 298 | for (size_t i = 0; i < signature->parameter_count(); i++) { |
| 299 | if (signature->GetParam(i) == MachineType::Int64()) { |
| 300 | // For each int64 input we get two int32 inputs. |
| 301 | parameter_count++; |
| 302 | } |
| 303 | } |
| 304 | for (size_t i = 0; i < signature->return_count(); i++) { |
| 305 | if (signature->GetReturn(i) == MachineType::Int64()) { |
| 306 | // For each int64 return we get two int32 returns. |
| 307 | return_count++; |
| 308 | } |
| 309 | } |
| 310 | if (parameter_count == signature->parameter_count() && |
| 311 | return_count == signature->return_count()) { |
| 312 | // If there is no int64 parameter or return value, we can just return the |
| 313 | // original descriptor. |
| 314 | return descriptor; |
| 315 | } |
| 316 | |
| 317 | MachineSignature::Builder msig(zone, return_count, parameter_count); |
| 318 | LocationSignature::Builder locations(zone, return_count, parameter_count); |
| 319 | |
| 320 | Allocator rets = GetReturnRegisters(); |
| 321 | |
| 322 | for (size_t i = 0; i < signature->return_count(); i++) { |
| 323 | if (signature->GetReturn(i) == MachineType::Int64()) { |
| 324 | // For each int64 return we get two int32 returns. |
| 325 | msig.AddReturn(MachineType::Int32()); |
| 326 | msig.AddReturn(MachineType::Int32()); |
| 327 | locations.AddReturn(rets.Next(MachineRepresentation::kWord32)); |
| 328 | locations.AddReturn(rets.Next(MachineRepresentation::kWord32)); |
| 329 | } else { |
| 330 | msig.AddReturn(signature->GetReturn(i)); |
| 331 | locations.AddReturn(rets.Next(signature->GetReturn(i).representation())); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | Allocator params = GetParameterRegisters(); |
| 336 | |
| 337 | for (size_t i = 0; i < signature->parameter_count(); i++) { |
| 338 | if (signature->GetParam(i) == MachineType::Int64()) { |
| 339 | // For each int64 input we get two int32 inputs. |
| 340 | msig.AddParam(MachineType::Int32()); |
| 341 | msig.AddParam(MachineType::Int32()); |
| 342 | locations.AddParam(params.Next(MachineRepresentation::kWord32)); |
| 343 | locations.AddParam(params.Next(MachineRepresentation::kWord32)); |
| 344 | } else { |
| 345 | msig.AddParam(signature->GetParam(i)); |
| 346 | locations.AddParam(params.Next(signature->GetParam(i).representation())); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | return new (zone) CallDescriptor( // -- |
| 351 | descriptor->kind(), // kind |
| 352 | descriptor->GetInputType(0), // target MachineType |
| 353 | descriptor->GetInputLocation(0), // target location |
| 354 | msig.Build(), // machine_sig |
| 355 | locations.Build(), // location_sig |
| 356 | params.stack_offset, // stack_parameter_count |
| 357 | descriptor->properties(), // properties |
| 358 | descriptor->CalleeSavedRegisters(), // callee-saved registers |
| 359 | descriptor->CalleeSavedFPRegisters(), // callee-saved fp regs |
| 360 | descriptor->flags(), // flags |
| 361 | descriptor->debug_name()); |
| 362 | |
| 363 | return descriptor; |
| 364 | } |
| 365 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 366 | } // namespace wasm |
| 367 | } // namespace internal |
| 368 | } // namespace v8 |