| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* |
| 4 |
* Copyright 2015 Mozilla Foundation |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "wasm/WasmStubs.h" |
| 20 |
|
| 21 |
#include <algorithm> |
| 22 |
#include <iterator> |
| 23 |
#include <type_traits> |
| 24 |
|
| 25 |
#include "jit/ABIArgGenerator.h" |
| 26 |
#include "jit/JitFrames.h" |
| 27 |
#include "jit/RegisterAllocator.h" |
| 28 |
#include "js/Printf.h" |
| 29 |
#include "util/Memory.h" |
| 30 |
#include "wasm/WasmCode.h" |
| 31 |
#include "wasm/WasmGenerator.h" |
| 32 |
#include "wasm/WasmInstance.h" |
| 33 |
|
| 34 |
#include "jit/MacroAssembler-inl.h" |
| 35 |
#include "wasm/WasmInstance-inl.h" |
| 36 |
|
| 37 |
using namespace js; |
| 38 |
using namespace js::jit; |
| 39 |
using namespace js::wasm; |
| 40 |
|
| 41 |
using mozilla::DebugOnly; |
| 42 |
using mozilla::Maybe; |
| 43 |
using mozilla::Nothing; |
| 44 |
using mozilla::Some; |
| 45 |
|
| 46 |
using MIRTypeVector = Vector<jit::MIRType, 8, SystemAllocPolicy>; |
| 47 |
using ABIArgMIRTypeIter = jit::ABIArgIter<MIRTypeVector>; |
| 48 |
|
| 49 |
/*****************************************************************************/ |
| 50 |
// ABIResultIter implementation |
| 51 |
|
| 52 |
static uint32_t ResultStackSize(ValType type) { |
| 53 |
switch (type.kind()) { |
| 54 |
case ValType::I32: |
| 55 |
return ABIResult::StackSizeOfInt32; |
| 56 |
case ValType::I64: |
| 57 |
return ABIResult::StackSizeOfInt64; |
| 58 |
case ValType::F32: |
| 59 |
return ABIResult::StackSizeOfFloat; |
| 60 |
case ValType::F64: |
| 61 |
return ABIResult::StackSizeOfDouble; |
| 62 |
#ifdef ENABLE_WASM_SIMD |
| 63 |
case ValType::V128: |
| 64 |
return ABIResult::StackSizeOfV128; |
| 65 |
#endif |
| 66 |
case ValType::Ref: |
| 67 |
return ABIResult::StackSizeOfPtr; |
| 68 |
default: |
| 69 |
MOZ_CRASH("Unexpected result type"); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// Compute the size of the stack slot that the wasm ABI requires be allocated |
| 74 |
// for a particular MIRType. Note that this sometimes differs from the |
| 75 |
// MIRType's natural size. See also ResultStackSize above and ABIResult::size() |
| 76 |
// and ABIResultIter below. |
| 77 |
|
| 78 |
uint32_t js::wasm::MIRTypeToABIResultSize(jit::MIRType type) { |
| 79 |
switch (type) { |
| 80 |
case MIRType::Int32: |
| 81 |
return ABIResult::StackSizeOfInt32; |
| 82 |
case MIRType::Int64: |
| 83 |
return ABIResult::StackSizeOfInt64; |
| 84 |
case MIRType::Float32: |
| 85 |
return ABIResult::StackSizeOfFloat; |
| 86 |
case MIRType::Double: |
| 87 |
return ABIResult::StackSizeOfDouble; |
| 88 |
#ifdef ENABLE_WASM_SIMD |
| 89 |
case MIRType::Simd128: |
| 90 |
return ABIResult::StackSizeOfV128; |
| 91 |
#endif |
| 92 |
case MIRType::Pointer: |
| 93 |
case MIRType::WasmAnyRef: |
| 94 |
return ABIResult::StackSizeOfPtr; |
| 95 |
default: |
| 96 |
MOZ_CRASH("MIRTypeToABIResultSize - unhandled case"); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
uint32_t ABIResult::size() const { return ResultStackSize(type()); } |
| 101 |
|
| 102 |
void ABIResultIter::settleRegister(ValType type) { |
| 103 |
MOZ_ASSERT(!done()); |
| 104 |
MOZ_ASSERT_IF(direction_ == Next, index() < MaxRegisterResults); |
| 105 |
MOZ_ASSERT_IF(direction_ == Prev, index() >= count_ - MaxRegisterResults); |
| 106 |
static_assert(MaxRegisterResults == 1, "expected a single register result"); |
| 107 |
|
| 108 |
switch (type.kind()) { |
| 109 |
case ValType::I32: |
| 110 |
cur_ = ABIResult(type, ReturnReg); |
| 111 |
break; |
| 112 |
case ValType::I64: |
| 113 |
cur_ = ABIResult(type, ReturnReg64); |
| 114 |
break; |
| 115 |
case ValType::F32: |
| 116 |
cur_ = ABIResult(type, ReturnFloat32Reg); |
| 117 |
break; |
| 118 |
case ValType::F64: |
| 119 |
cur_ = ABIResult(type, ReturnDoubleReg); |
| 120 |
break; |
| 121 |
case ValType::Ref: |
| 122 |
cur_ = ABIResult(type, ReturnReg); |
| 123 |
break; |
| 124 |
#ifdef ENABLE_WASM_SIMD |
| 125 |
case ValType::V128: |
| 126 |
cur_ = ABIResult(type, ReturnSimd128Reg); |
| 127 |
break; |
| 128 |
#endif |
| 129 |
default: |
| 130 |
MOZ_CRASH("Unexpected result type"); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
void ABIResultIter::settleNext() { |
| 135 |
MOZ_ASSERT(direction_ == Next); |
| 136 |
MOZ_ASSERT(!done()); |
| 137 |
|
| 138 |
uint32_t typeIndex = count_ - index_ - 1; |
| 139 |
ValType type = type_[typeIndex]; |
| 140 |
|
| 141 |
if (index_ < MaxRegisterResults) { |
| 142 |
settleRegister(type); |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
cur_ = ABIResult(type, nextStackOffset_); |
| 147 |
nextStackOffset_ += ResultStackSize(type); |
| 148 |
} |
| 149 |
|
| 150 |
void ABIResultIter::settlePrev() { |
| 151 |
MOZ_ASSERT(direction_ == Prev); |
| 152 |
MOZ_ASSERT(!done()); |
| 153 |
uint32_t typeIndex = index_; |
| 154 |
ValType type = type_[typeIndex]; |
| 155 |
|
| 156 |
if (count_ - index_ - 1 < MaxRegisterResults) { |
| 157 |
settleRegister(type); |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
uint32_t size = ResultStackSize(type); |
| 162 |
MOZ_ASSERT(nextStackOffset_ >= size); |
| 163 |
nextStackOffset_ -= size; |
| 164 |
cur_ = ABIResult(type, nextStackOffset_); |
| 165 |
} |
| 166 |
|
| 167 |
#ifdef WASM_CODEGEN_DEBUG |
| 168 |
template <class Closure> |
| 169 |
static void GenPrint(DebugChannel channel, MacroAssembler& masm, |
| 170 |
const Maybe<Register>& taken, Closure passArgAndCall) { |
| 171 |
if (!IsCodegenDebugEnabled(channel)) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
AllocatableRegisterSet regs(RegisterSet::All()); |
| 176 |
LiveRegisterSet save(regs.asLiveSet()); |
| 177 |
masm.PushRegsInMask(save); |
| 178 |
|
| 179 |
if (taken) { |
| 180 |
regs.take(taken.value()); |
| 181 |
} |
| 182 |
Register temp = regs.takeAnyGeneral(); |
| 183 |
|
| 184 |
{ |
| 185 |
MOZ_ASSERT(MaybeGetJitContext(), |
| 186 |
"codegen debug checks require a jit context"); |
| 187 |
# ifdef JS_CODEGEN_ARM64 |
| 188 |
if (IsCompilingWasm()) { |
| 189 |
masm.setupWasmABICall(); |
| 190 |
} else { |
| 191 |
// JS ARM64 has an extra stack pointer which is not managed in WASM. |
| 192 |
masm.setupUnalignedABICall(temp); |
| 193 |
} |
| 194 |
# else |
| 195 |
masm.setupUnalignedABICall(temp); |
| 196 |
# endif |
| 197 |
passArgAndCall(IsCompilingWasm(), temp); |
| 198 |
} |
| 199 |
|
| 200 |
masm.PopRegsInMask(save); |
| 201 |
} |
| 202 |
|
| 203 |
static void GenPrintf(DebugChannel channel, MacroAssembler& masm, |
| 204 |
const char* fmt, ...) { |
| 205 |
va_list ap; |
| 206 |
va_start(ap, fmt); |
| 207 |
UniqueChars str = JS_vsmprintf(fmt, ap); |
| 208 |
va_end(ap); |
| 209 |
|
| 210 |
GenPrint(channel, masm, Nothing(), [&](bool inWasm, Register temp) { |
| 211 |
// If we've gone this far, it means we're actually using the debugging |
| 212 |
// strings. In this case, we leak them! This is only for debugging, and |
| 213 |
// doing the right thing is cumbersome (in Ion, it'd mean add a vec of |
| 214 |
// strings to the IonScript; in wasm, it'd mean add it to the current |
| 215 |
// Module and serialize it properly). |
| 216 |
const char* text = str.release(); |
| 217 |
|
| 218 |
masm.movePtr(ImmPtr((void*)text, ImmPtr::NoCheckToken()), temp); |
| 219 |
masm.passABIArg(temp); |
| 220 |
if (inWasm) { |
| 221 |
masm.callDebugWithABI(SymbolicAddress::PrintText); |
| 222 |
} else { |
| 223 |
using Fn = void (*)(const char* output); |
| 224 |
masm.callWithABI<Fn, PrintText>(ABIType::General, |
| 225 |
CheckUnsafeCallWithABI::DontCheckOther); |
| 226 |
} |
| 227 |
}); |
| 228 |
} |
| 229 |
|
| 230 |
static void GenPrintIsize(DebugChannel channel, MacroAssembler& masm, |
| 231 |
const Register& src) { |
| 232 |
GenPrint(channel, masm, Some(src), [&](bool inWasm, Register _temp) { |
| 233 |
masm.passABIArg(src); |
| 234 |
if (inWasm) { |
| 235 |
masm.callDebugWithABI(SymbolicAddress::PrintI32); |
| 236 |
} else { |
| 237 |
using Fn = void (*)(int32_t val); |
| 238 |
masm.callWithABI<Fn, PrintI32>(ABIType::General, |
| 239 |
CheckUnsafeCallWithABI::DontCheckOther); |
| 240 |
} |
| 241 |
}); |
| 242 |
} |
| 243 |
|
| 244 |
static void GenPrintPtr(DebugChannel channel, MacroAssembler& masm, |
| 245 |
const Register& src) { |
| 246 |
GenPrint(channel, masm, Some(src), [&](bool inWasm, Register _temp) { |
| 247 |
masm.passABIArg(src); |
| 248 |
if (inWasm) { |
| 249 |
masm.callDebugWithABI(SymbolicAddress::PrintPtr); |
| 250 |
} else { |
| 251 |
using Fn = void (*)(uint8_t* val); |
| 252 |
masm.callWithABI<Fn, PrintPtr>(ABIType::General, |
| 253 |
CheckUnsafeCallWithABI::DontCheckOther); |
| 254 |
} |
| 255 |
}); |
| 256 |
} |
| 257 |
|
| 258 |
static void GenPrintI64(DebugChannel channel, MacroAssembler& masm, |
| 259 |
const Register64& src) { |
| 260 |
# if JS_BITS_PER_WORD == 64 |
| 261 |
GenPrintf(channel, masm, "i64 "); |
| 262 |
GenPrintIsize(channel, masm, src.reg); |
| 263 |
# else |
| 264 |
GenPrintf(channel, masm, "i64("); |
| 265 |
GenPrintIsize(channel, masm, src.low); |
| 266 |
GenPrintIsize(channel, masm, src.high); |
| 267 |
GenPrintf(channel, masm, ") "); |
| 268 |
# endif |
| 269 |
} |
| 270 |
|
| 271 |
static void GenPrintF32(DebugChannel channel, MacroAssembler& masm, |
| 272 |
const FloatRegister& src) { |
| 273 |
GenPrint(channel, masm, Nothing(), [&](bool inWasm, Register temp) { |
| 274 |
masm.passABIArg(src, ABIType::Float32); |
| 275 |
if (inWasm) { |
| 276 |
masm.callDebugWithABI(SymbolicAddress::PrintF32); |
| 277 |
} else { |
| 278 |
using Fn = void (*)(float val); |
| 279 |
masm.callWithABI<Fn, PrintF32>(ABIType::General, |
| 280 |
CheckUnsafeCallWithABI::DontCheckOther); |
| 281 |
} |
| 282 |
}); |
| 283 |
} |
| 284 |
|
| 285 |
static void GenPrintF64(DebugChannel channel, MacroAssembler& masm, |
| 286 |
const FloatRegister& src) { |
| 287 |
GenPrint(channel, masm, Nothing(), [&](bool inWasm, Register temp) { |
| 288 |
masm.passABIArg(src, ABIType::Float64); |
| 289 |
if (inWasm) { |
| 290 |
masm.callDebugWithABI(SymbolicAddress::PrintF64); |
| 291 |
} else { |
| 292 |
using Fn = void (*)(double val); |
| 293 |
masm.callWithABI<Fn, PrintF64>(ABIType::General, |
| 294 |
CheckUnsafeCallWithABI::DontCheckOther); |
| 295 |
} |
| 296 |
}); |
| 297 |
} |
| 298 |
|
| 299 |
# ifdef ENABLE_WASM_SIMD |
| 300 |
static void GenPrintV128(DebugChannel channel, MacroAssembler& masm, |
| 301 |
const FloatRegister& src) { |
| 302 |
// TODO: We might try to do something meaningful here once SIMD data are |
| 303 |
// aligned and hence C++-ABI compliant. For now, just make ourselves visible. |
| 304 |
GenPrintf(channel, masm, "v128"); |
| 305 |
} |
| 306 |
# endif |
| 307 |
#else |
| 308 |
static void GenPrintf(DebugChannel channel, MacroAssembler& masm, |
| 309 |
const char* fmt, ...) {} |
| 310 |
static void GenPrintIsize(DebugChannel channel, MacroAssembler& masm, |
| 311 |
const Register& src) {} |
| 312 |
static void GenPrintPtr(DebugChannel channel, MacroAssembler& masm, |
| 313 |
const Register& src) {} |
| 314 |
static void GenPrintI64(DebugChannel channel, MacroAssembler& masm, |
| 315 |
const Register64& src) {} |
| 316 |
static void GenPrintF32(DebugChannel channel, MacroAssembler& masm, |
| 317 |
const FloatRegister& src) {} |
| 318 |
static void GenPrintF64(DebugChannel channel, MacroAssembler& masm, |
| 319 |
const FloatRegister& src) {} |
| 320 |
# ifdef ENABLE_WASM_SIMD |
| 321 |
static void GenPrintV128(DebugChannel channel, MacroAssembler& masm, |
| 322 |
const FloatRegister& src) {} |
| 323 |
# endif |
| 324 |
#endif |
| 325 |
|
| 326 |
static bool FinishOffsets(MacroAssembler& masm, Offsets* offsets) { |
| 327 |
// On old ARM hardware, constant pools could be inserted and they need to |
| 328 |
// be flushed before considering the size of the masm. |
| 329 |
masm.flushBuffer(); |
| 330 |
offsets->end = masm.size(); |
| 331 |
return !masm.oom(); |
| 332 |
} |
| 333 |
|
| 334 |
static void AssertStackAlignment(MacroAssembler& masm, uint32_t alignment, |
| 335 |
uint32_t addBeforeAssert = 0) { |
| 336 |
MOZ_ASSERT( |
| 337 |
(sizeof(Frame) + masm.framePushed() + addBeforeAssert) % alignment == 0); |
| 338 |
masm.assertStackAlignment(alignment, addBeforeAssert); |
| 339 |
} |
| 340 |
|
| 341 |
template <class VectorT, template <class VecT> class ABIArgIterT> |
| 342 |
static unsigned StackArgBytesHelper(const VectorT& args) { |
| 343 |
ABIArgIterT<VectorT> iter(args); |
| 344 |
while (!iter.done()) { |
| 345 |
iter++; |
| 346 |
} |
| 347 |
return iter.stackBytesConsumedSoFar(); |
| 348 |
} |
| 349 |
|
| 350 |
template <class VectorT> |
| 351 |
static unsigned StackArgBytesForNativeABI(const VectorT& args) { |
| 352 |
return StackArgBytesHelper<VectorT, ABIArgIter>(args); |
| 353 |
} |
| 354 |
|
| 355 |
template <class VectorT> |
| 356 |
static unsigned StackArgBytesForWasmABI(const VectorT& args) { |
| 357 |
return StackArgBytesHelper<VectorT, WasmABIArgIter>(args); |
| 358 |
} |
| 359 |
|
| 360 |
static unsigned StackArgBytesForWasmABI(const FuncType& funcType) { |
| 361 |
ArgTypeVector args(funcType); |
| 362 |
return StackArgBytesForWasmABI(args); |
| 363 |
} |
| 364 |
|
| 365 |
static void SetupABIArguments(MacroAssembler& masm, const FuncExport& fe, |
| 366 |
const FuncType& funcType, Register argv, |
| 367 |
Register scratch) { |
| 368 |
// Copy parameters out of argv and into the registers/stack-slots specified by |
| 369 |
// the wasm ABI. |
| 370 |
// |
| 371 |
// SetupABIArguments are only used for C++ -> wasm calls through callExport(), |
| 372 |
// and V128 and Ref types (other than externref) are not currently allowed. |
| 373 |
ArgTypeVector args(funcType); |
| 374 |
for (WasmABIArgIter iter(args); !iter.done(); iter++) { |
| 375 |
unsigned argOffset = iter.index() * sizeof(ExportArg); |
| 376 |
Address src(argv, argOffset); |
| 377 |
MIRType type = iter.mirType(); |
| 378 |
switch (iter->kind()) { |
| 379 |
case ABIArg::GPR: |
| 380 |
if (type == MIRType::Int32) { |
| 381 |
masm.load32(src, iter->gpr()); |
| 382 |
} else if (type == MIRType::Int64) { |
| 383 |
masm.load64(src, iter->gpr64()); |
| 384 |
} else if (type == MIRType::WasmAnyRef) { |
| 385 |
masm.loadPtr(src, iter->gpr()); |
| 386 |
} else if (type == MIRType::StackResults) { |
| 387 |
MOZ_ASSERT(args.isSyntheticStackResultPointerArg(iter.index())); |
| 388 |
masm.loadPtr(src, iter->gpr()); |
| 389 |
} else { |
| 390 |
MOZ_CRASH("unknown GPR type"); |
| 391 |
} |
| 392 |
break; |
| 393 |
#ifdef JS_CODEGEN_REGISTER_PAIR |
| 394 |
case ABIArg::GPR_PAIR: |
| 395 |
if (type == MIRType::Int64) { |
| 396 |
masm.load64(src, iter->gpr64()); |
| 397 |
} else { |
| 398 |
MOZ_CRASH("wasm uses hardfp for function calls."); |
| 399 |
} |
| 400 |
break; |
| 401 |
#endif |
| 402 |
case ABIArg::FPU: { |
| 403 |
static_assert(sizeof(ExportArg) >= jit::Simd128DataSize, |
| 404 |
"ExportArg must be big enough to store SIMD values"); |
| 405 |
switch (type) { |
| 406 |
case MIRType::Double: |
| 407 |
masm.loadDouble(src, iter->fpu()); |
| 408 |
break; |
| 409 |
case MIRType::Float32: |
| 410 |
masm.loadFloat32(src, iter->fpu()); |
| 411 |
break; |
| 412 |
case MIRType::Simd128: |
| 413 |
#ifdef ENABLE_WASM_SIMD |
| 414 |
// This is only used by the testing invoke path, |
| 415 |
// wasmLosslessInvoke, and is guarded against in normal JS-API |
| 416 |
// call paths. |
| 417 |
masm.loadUnalignedSimd128(src, iter->fpu()); |
| 418 |
break; |
| 419 |
#else |
| 420 |
MOZ_CRASH("V128 not supported in SetupABIArguments"); |
| 421 |
#endif |
| 422 |
default: |
| 423 |
MOZ_CRASH("unexpected FPU type"); |
| 424 |
break; |
| 425 |
} |
| 426 |
break; |
| 427 |
} |
| 428 |
case ABIArg::Stack: |
| 429 |
switch (type) { |
| 430 |
case MIRType::Int32: |
| 431 |
masm.load32(src, scratch); |
| 432 |
masm.storePtr(scratch, Address(masm.getStackPointer(), |
| 433 |
iter->offsetFromArgBase())); |
| 434 |
break; |
| 435 |
case MIRType::Int64: { |
| 436 |
RegisterOrSP sp = masm.getStackPointer(); |
| 437 |
masm.copy64(src, Address(sp, iter->offsetFromArgBase()), scratch); |
| 438 |
break; |
| 439 |
} |
| 440 |
case MIRType::WasmAnyRef: |
| 441 |
masm.loadPtr(src, scratch); |
| 442 |
masm.storePtr(scratch, Address(masm.getStackPointer(), |
| 443 |
iter->offsetFromArgBase())); |
| 444 |
break; |
| 445 |
case MIRType::Double: { |
| 446 |
ScratchDoubleScope fpscratch(masm); |
| 447 |
masm.loadDouble(src, fpscratch); |
| 448 |
masm.storeDouble(fpscratch, Address(masm.getStackPointer(), |
| 449 |
iter->offsetFromArgBase())); |
| 450 |
break; |
| 451 |
} |
| 452 |
case MIRType::Float32: { |
| 453 |
ScratchFloat32Scope fpscratch(masm); |
| 454 |
masm.loadFloat32(src, fpscratch); |
| 455 |
masm.storeFloat32(fpscratch, Address(masm.getStackPointer(), |
| 456 |
iter->offsetFromArgBase())); |
| 457 |
break; |
| 458 |
} |
| 459 |
case MIRType::Simd128: { |
| 460 |
#ifdef ENABLE_WASM_SIMD |
| 461 |
// This is only used by the testing invoke path, |
| 462 |
// wasmLosslessInvoke, and is guarded against in normal JS-API |
| 463 |
// call paths. |
| 464 |
ScratchSimd128Scope fpscratch(masm); |
| 465 |
masm.loadUnalignedSimd128(src, fpscratch); |
| 466 |
masm.storeUnalignedSimd128( |
| 467 |
fpscratch, |
| 468 |
Address(masm.getStackPointer(), iter->offsetFromArgBase())); |
| 469 |
break; |
| 470 |
#else |
| 471 |
MOZ_CRASH("V128 not supported in SetupABIArguments"); |
| 472 |
#endif |
| 473 |
} |
| 474 |
case MIRType::StackResults: { |
| 475 |
MOZ_ASSERT(args.isSyntheticStackResultPointerArg(iter.index())); |
| 476 |
masm.loadPtr(src, scratch); |
| 477 |
masm.storePtr(scratch, Address(masm.getStackPointer(), |
| 478 |
iter->offsetFromArgBase())); |
| 479 |
break; |
| 480 |
} |
| 481 |
default: |
| 482 |
MOZ_CRASH("unexpected stack arg type"); |
| 483 |
} |
| 484 |
break; |
| 485 |
case ABIArg::Uninitialized: |
| 486 |
MOZ_CRASH("Uninitialized ABIArg kind"); |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
static void StoreRegisterResult(MacroAssembler& masm, const FuncExport& fe, |
| 492 |
const FuncType& funcType, Register loc) { |
| 493 |
ResultType results = ResultType::Vector(funcType.results()); |
| 494 |
DebugOnly<bool> sawRegisterResult = false; |
| 495 |
for (ABIResultIter iter(results); !iter.done(); iter.next()) { |
| 496 |
const ABIResult& result = iter.cur(); |
| 497 |
if (result.inRegister()) { |
| 498 |
MOZ_ASSERT(!sawRegisterResult); |
| 499 |
sawRegisterResult = true; |
| 500 |
switch (result.type().kind()) { |
| 501 |
case ValType::I32: |
| 502 |
masm.store32(result.gpr(), Address(loc, 0)); |
| 503 |
break; |
| 504 |
case ValType::I64: |
| 505 |
masm.store64(result.gpr64(), Address(loc, 0)); |
| 506 |
break; |
| 507 |
case ValType::V128: |
| 508 |
#ifdef ENABLE_WASM_SIMD |
| 509 |
masm.storeUnalignedSimd128(result.fpr(), Address(loc, 0)); |
| 510 |
break; |
| 511 |
#else |
| 512 |
MOZ_CRASH("V128 not supported in StoreABIReturn"); |
| 513 |
#endif |
| 514 |
case ValType::F32: |
| 515 |
masm.storeFloat32(result.fpr(), Address(loc, 0)); |
| 516 |
break; |
| 517 |
case ValType::F64: |
| 518 |
masm.storeDouble(result.fpr(), Address(loc, 0)); |
| 519 |
break; |
| 520 |
case ValType::Ref: |
| 521 |
masm.storePtr(result.gpr(), Address(loc, 0)); |
| 522 |
break; |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
MOZ_ASSERT(sawRegisterResult == (results.length() > 0)); |
| 527 |
} |
| 528 |
|
| 529 |
#if defined(JS_CODEGEN_ARM) |
| 530 |
// The ARM system ABI also includes d15 & s31 in the non volatile float |
| 531 |
// registers. Also exclude lr (a.k.a. r14) as we preserve it manually. |
| 532 |
static const LiveRegisterSet NonVolatileRegs = LiveRegisterSet( |
| 533 |
GeneralRegisterSet(Registers::NonVolatileMask & |
| 534 |
~(Registers::SetType(1) << Registers::lr)), |
| 535 |
FloatRegisterSet(FloatRegisters::NonVolatileMask | |
| 536 |
(FloatRegisters::SetType(1) << FloatRegisters::d15) | |
| 537 |
(FloatRegisters::SetType(1) << FloatRegisters::s31))); |
| 538 |
#elif defined(JS_CODEGEN_ARM64) |
| 539 |
// Exclude the Link Register (x30) because it is preserved manually. |
| 540 |
// |
| 541 |
// Include x16 (scratch) to make a 16-byte aligned amount of integer registers. |
| 542 |
// Include d31 (scratch) to make a 16-byte aligned amount of floating registers. |
| 543 |
static const LiveRegisterSet NonVolatileRegs = LiveRegisterSet( |
| 544 |
GeneralRegisterSet((Registers::NonVolatileMask & |
| 545 |
~(Registers::SetType(1) << Registers::lr)) | |
| 546 |
(Registers::SetType(1) << Registers::x16)), |
| 547 |
FloatRegisterSet(FloatRegisters::NonVolatileMask | |
| 548 |
FloatRegisters::NonAllocatableMask)); |
| 549 |
#else |
| 550 |
static const LiveRegisterSet NonVolatileRegs = |
| 551 |
LiveRegisterSet(GeneralRegisterSet(Registers::NonVolatileMask), |
| 552 |
FloatRegisterSet(FloatRegisters::NonVolatileMask)); |
| 553 |
#endif |
| 554 |
|
| 555 |
#ifdef JS_CODEGEN_ARM64 |
| 556 |
static const unsigned WasmPushSize = 16; |
| 557 |
#else |
| 558 |
static const unsigned WasmPushSize = sizeof(void*); |
| 559 |
#endif |
| 560 |
|
| 561 |
static void AssertExpectedSP(MacroAssembler& masm) { |
| 562 |
#ifdef JS_CODEGEN_ARM64 |
| 563 |
MOZ_ASSERT(sp.Is(masm.GetStackPointer64())); |
| 564 |
# ifdef DEBUG |
| 565 |
// Since we're asserting that SP is the currently active stack pointer, |
| 566 |
// let's also in effect assert that PSP is dead -- by setting it to 1, so as |
| 567 |
// to cause to cause any attempts to use it to segfault in an easily |
| 568 |
// identifiable way. |
| 569 |
masm.asVIXL().Mov(PseudoStackPointer64, 1); |
| 570 |
# endif |
| 571 |
#endif |
| 572 |
} |
| 573 |
|
| 574 |
template <class Operand> |
| 575 |
static void WasmPush(MacroAssembler& masm, const Operand& op) { |
| 576 |
#ifdef JS_CODEGEN_ARM64 |
| 577 |
// Allocate a pad word so that SP can remain properly aligned. |op| will be |
| 578 |
// written at the lower-addressed of the two words pushed here. |
| 579 |
masm.reserveStack(WasmPushSize); |
| 580 |
masm.storePtr(op, Address(masm.getStackPointer(), 0)); |
| 581 |
#else |
| 582 |
masm.Push(op); |
| 583 |
#endif |
| 584 |
} |
| 585 |
|
| 586 |
static void WasmPop(MacroAssembler& masm, Register r) { |
| 587 |
#ifdef JS_CODEGEN_ARM64 |
| 588 |
// Also pop the pad word allocated by WasmPush. |
| 589 |
masm.loadPtr(Address(masm.getStackPointer(), 0), r); |
| 590 |
masm.freeStack(WasmPushSize); |
| 591 |
#else |
| 592 |
masm.Pop(r); |
| 593 |
#endif |
| 594 |
} |
| 595 |
|
| 596 |
static void MoveSPForJitABI(MacroAssembler& masm) { |
| 597 |
#ifdef JS_CODEGEN_ARM64 |
| 598 |
masm.moveStackPtrTo(PseudoStackPointer); |
| 599 |
#endif |
| 600 |
} |
| 601 |
|
| 602 |
static void CallFuncExport(MacroAssembler& masm, const FuncExport& fe, |
| 603 |
const Maybe<ImmPtr>& funcPtr) { |
| 604 |
MOZ_ASSERT(fe.hasEagerStubs() == !funcPtr); |
| 605 |
MoveSPForJitABI(masm); |
| 606 |
if (funcPtr) { |
| 607 |
masm.call(*funcPtr); |
| 608 |
} else { |
| 609 |
masm.call(CallSiteDesc(CallSiteKind::Func), fe.funcIndex()); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
// Generate a stub that enters wasm from a C++ caller via the native ABI. The |
| 614 |
// signature of the entry point is Module::ExportFuncPtr. The exported wasm |
| 615 |
// function has an ABI derived from its specific signature, so this function |
| 616 |
// must map from the ABI of ExportFuncPtr to the export's signature's ABI. |
| 617 |
static bool GenerateInterpEntry(MacroAssembler& masm, const FuncExport& fe, |
| 618 |
const FuncType& funcType, |
| 619 |
const Maybe<ImmPtr>& funcPtr, |
| 620 |
Offsets* offsets) { |
| 621 |
AutoCreatedBy acb(masm, "GenerateInterpEntry"); |
| 622 |
|
| 623 |
AssertExpectedSP(masm); |
| 624 |
|
| 625 |
// UBSAN expects that the word before a C++ function pointer is readable for |
| 626 |
// some sort of generated assertion. |
| 627 |
// |
| 628 |
// These interp entry points can sometimes be output at the beginning of a |
| 629 |
// code page allocation, which will cause access violations when called with |
| 630 |
// UBSAN enabled. |
| 631 |
// |
| 632 |
// Insert some padding in this case by inserting a breakpoint before we align |
| 633 |
// our code. This breakpoint will misalign the code buffer (which was aligned |
| 634 |
// due to being at the beginning of the buffer), which will then be aligned |
| 635 |
// and have at least one word of padding before this entry point. |
| 636 |
if (masm.currentOffset() == 0) { |
| 637 |
masm.breakpoint(); |
| 638 |
} |
| 639 |
|
| 640 |
masm.haltingAlign(CodeAlignment); |
| 641 |
|
| 642 |
// Double check that the first word is available for UBSAN; see above. |
| 643 |
static_assert(CodeAlignment >= sizeof(uintptr_t)); |
| 644 |
MOZ_ASSERT_IF(!masm.oom(), masm.currentOffset() >= sizeof(uintptr_t)); |
| 645 |
|
| 646 |
offsets->begin = masm.currentOffset(); |
| 647 |
|
| 648 |
// Save the return address if it wasn't already saved by the call insn. |
| 649 |
#ifdef JS_USE_LINK_REGISTER |
| 650 |
# if defined(JS_CODEGEN_ARM) || defined(JS_CODEGEN_MIPS64) || \ |
| 651 |
defined(JS_CODEGEN_LOONG64) || defined(JS_CODEGEN_RISCV64) |
| 652 |
masm.pushReturnAddress(); |
| 653 |
# elif defined(JS_CODEGEN_ARM64) |
| 654 |
// WasmPush updates framePushed() unlike pushReturnAddress(), but that's |
| 655 |
// cancelled by the setFramePushed() below. |
| 656 |
WasmPush(masm, lr); |
| 657 |
# else |
| 658 |
MOZ_CRASH("Implement this"); |
| 659 |
# endif |
| 660 |
#endif |
| 661 |
|
| 662 |
// Save all caller non-volatile registers before we clobber them here and in |
| 663 |
// the wasm callee (which does not preserve non-volatile registers). |
| 664 |
masm.setFramePushed(0); |
| 665 |
masm.PushRegsInMask(NonVolatileRegs); |
| 666 |
|
| 667 |
const unsigned nonVolatileRegsPushSize = |
| 668 |
MacroAssembler::PushRegsInMaskSizeInBytes(NonVolatileRegs); |
| 669 |
|
| 670 |
MOZ_ASSERT(masm.framePushed() == nonVolatileRegsPushSize); |
| 671 |
|
| 672 |
// Put the 'argv' argument into a non-argument/return/instance register so |
| 673 |
// that we can use 'argv' while we fill in the arguments for the wasm callee. |
| 674 |
// Use a second non-argument/return register as temporary scratch. |
| 675 |
Register argv = ABINonArgReturnReg0; |
| 676 |
Register scratch = ABINonArgReturnReg1; |
| 677 |
|
| 678 |
// scratch := SP |
| 679 |
masm.moveStackPtrTo(scratch); |
| 680 |
|
| 681 |
// Dynamically align the stack since ABIStackAlignment is not necessarily |
| 682 |
// WasmStackAlignment. Preserve SP so it can be restored after the call. |
| 683 |
#ifdef JS_CODEGEN_ARM64 |
| 684 |
static_assert(WasmStackAlignment == 16, "ARM64 SP alignment"); |
| 685 |
#else |
| 686 |
masm.andToStackPtr(Imm32(~(WasmStackAlignment - 1))); |
| 687 |
#endif |
| 688 |
masm.assertStackAlignment(WasmStackAlignment); |
| 689 |
|
| 690 |
// Create a fake frame: just previous RA and an FP. |
| 691 |
const size_t FakeFrameSize = 2 * sizeof(void*); |
| 692 |
#ifdef JS_CODEGEN_ARM64 |
| 693 |
masm.Ldr(ARMRegister(ABINonArgReturnReg0, 64), |
| 694 |
MemOperand(ARMRegister(scratch, 64), nonVolatileRegsPushSize)); |
| 695 |
#else |
| 696 |
masm.Push(Address(scratch, nonVolatileRegsPushSize)); |
| 697 |
#endif |
| 698 |
// Store fake wasm register state. Ensure the frame pointer passed by the C++ |
| 699 |
// caller doesn't have the ExitFPTag bit set to not confuse frame iterators. |
| 700 |
// This bit shouldn't be set if C++ code is using frame pointers, so this has |
| 701 |
// no effect on native stack unwinders. |
| 702 |
masm.andPtr(Imm32(int32_t(~ExitFPTag)), FramePointer); |
| 703 |
#ifdef JS_CODEGEN_ARM64 |
| 704 |
masm.asVIXL().Push(ARMRegister(ABINonArgReturnReg0, 64), |
| 705 |
ARMRegister(FramePointer, 64)); |
| 706 |
masm.moveStackPtrTo(FramePointer); |
| 707 |
#else |
| 708 |
masm.Push(FramePointer); |
| 709 |
#endif |
| 710 |
|
| 711 |
masm.moveStackPtrTo(FramePointer); |
| 712 |
masm.setFramePushed(0); |
| 713 |
#ifdef JS_CODEGEN_ARM64 |
| 714 |
DebugOnly<size_t> fakeFramePushed = 0; |
| 715 |
#else |
| 716 |
DebugOnly<size_t> fakeFramePushed = sizeof(void*); |
| 717 |
masm.Push(scratch); |
| 718 |
#endif |
| 719 |
|
| 720 |
// Read the arguments of wasm::ExportFuncPtr according to the native ABI. |
| 721 |
// The entry stub's frame is 1 word. |
| 722 |
const unsigned argBase = sizeof(void*) + nonVolatileRegsPushSize; |
| 723 |
ABIArgGenerator abi; |
| 724 |
ABIArg arg; |
| 725 |
|
| 726 |
// arg 1: ExportArg* |
| 727 |
arg = abi.next(MIRType::Pointer); |
| 728 |
if (arg.kind() == ABIArg::GPR) { |
| 729 |
masm.movePtr(arg.gpr(), argv); |
| 730 |
} else { |
| 731 |
masm.loadPtr(Address(scratch, argBase + arg.offsetFromArgBase()), argv); |
| 732 |
} |
| 733 |
|
| 734 |
// Arg 2: Instance* |
| 735 |
arg = abi.next(MIRType::Pointer); |
| 736 |
if (arg.kind() == ABIArg::GPR) { |
| 737 |
masm.movePtr(arg.gpr(), InstanceReg); |
| 738 |
} else { |
| 739 |
masm.loadPtr(Address(scratch, argBase + arg.offsetFromArgBase()), |
| 740 |
InstanceReg); |
| 741 |
} |
| 742 |
|
| 743 |
WasmPush(masm, InstanceReg); |
| 744 |
|
| 745 |
// Save 'argv' on the stack so that we can recover it after the call. |
| 746 |
WasmPush(masm, argv); |
| 747 |
|
| 748 |
MOZ_ASSERT(masm.framePushed() == 2 * WasmPushSize + fakeFramePushed, |
| 749 |
"expected instance, argv, and fake frame"); |
| 750 |
uint32_t frameSizeBeforeCall = masm.framePushed(); |
| 751 |
|
| 752 |
// Align (missing) results area to WasmStackAlignment boudary. Return calls |
| 753 |
// expect arguments to not overlap with results or other slots. |
| 754 |
unsigned aligned = |
| 755 |
AlignBytes(masm.framePushed() + FakeFrameSize, WasmStackAlignment); |
| 756 |
masm.reserveStack(aligned - masm.framePushed() + FakeFrameSize); |
| 757 |
|
| 758 |
// Reserve stack space for the wasm call. |
| 759 |
unsigned argDecrement = StackDecrementForCall( |
| 760 |
WasmStackAlignment, aligned, StackArgBytesForWasmABI(funcType)); |
| 761 |
masm.reserveStack(argDecrement); |
| 762 |
|
| 763 |
// Copy parameters out of argv and into the wasm ABI registers/stack-slots. |
| 764 |
SetupABIArguments(masm, fe, funcType, argv, scratch); |
| 765 |
|
| 766 |
masm.loadWasmPinnedRegsFromInstance(mozilla::Nothing()); |
| 767 |
|
| 768 |
masm.storePtr(InstanceReg, Address(masm.getStackPointer(), |
| 769 |
WasmCalleeInstanceOffsetBeforeCall)); |
| 770 |
|
| 771 |
// Call into the real function. Note that, due to the throw stub, fp, instance |
| 772 |
// and pinned registers may be clobbered. |
| 773 |
masm.assertStackAlignment(WasmStackAlignment); |
| 774 |
CallFuncExport(masm, fe, funcPtr); |
| 775 |
masm.assertStackAlignment(WasmStackAlignment); |
| 776 |
|
| 777 |
// Set the return value based on whether InstanceReg is the |
| 778 |
// InterpFailInstanceReg magic value (set by the exception handler). |
| 779 |
Label success, join; |
| 780 |
masm.branchPtr(Assembler::NotEqual, InstanceReg, Imm32(InterpFailInstanceReg), |
| 781 |
&success); |
| 782 |
masm.move32(Imm32(false), scratch); |
| 783 |
masm.jump(&join); |
| 784 |
masm.bind(&success); |
| 785 |
masm.move32(Imm32(true), scratch); |
| 786 |
masm.bind(&join); |
| 787 |
|
| 788 |
// Pop the arguments pushed after the dynamic alignment. |
| 789 |
masm.setFramePushed(frameSizeBeforeCall); |
| 790 |
masm.freeStackTo(frameSizeBeforeCall); |
| 791 |
|
| 792 |
// Recover the 'argv' pointer which was saved before aligning the stack. |
| 793 |
WasmPop(masm, argv); |
| 794 |
|
| 795 |
WasmPop(masm, InstanceReg); |
| 796 |
|
| 797 |
// Pop the stack pointer to its value right before dynamic alignment. |
| 798 |
#ifdef JS_CODEGEN_ARM64 |
| 799 |
static_assert(WasmStackAlignment == 16, "ARM64 SP alignment"); |
| 800 |
masm.setFramePushed(FakeFrameSize); |
| 801 |
masm.freeStack(FakeFrameSize); |
| 802 |
#else |
| 803 |
masm.PopStackPtr(); |
| 804 |
#endif |
| 805 |
|
| 806 |
// Store the register result, if any, in argv[0]. |
| 807 |
// No widening is required, as the value leaves ReturnReg. |
| 808 |
StoreRegisterResult(masm, fe, funcType, argv); |
| 809 |
|
| 810 |
masm.move32(scratch, ReturnReg); |
| 811 |
|
| 812 |
// Restore clobbered non-volatile registers of the caller. |
| 813 |
masm.setFramePushed(nonVolatileRegsPushSize); |
| 814 |
masm.PopRegsInMask(NonVolatileRegs); |
| 815 |
MOZ_ASSERT(masm.framePushed() == 0); |
| 816 |
|
| 817 |
#if defined(JS_CODEGEN_ARM64) |
| 818 |
masm.setFramePushed(WasmPushSize); |
| 819 |
WasmPop(masm, lr); |
| 820 |
masm.abiret(); |
| 821 |
#else |
| 822 |
masm.ret(); |
| 823 |
#endif |
| 824 |
|
| 825 |
return FinishOffsets(masm, offsets); |
| 826 |
} |
| 827 |
|
| 828 |
#ifdef JS_PUNBOX64 |
| 829 |
static const ValueOperand ScratchValIonEntry = ValueOperand(ABINonArgReg0); |
| 830 |
#else |
| 831 |
static const ValueOperand ScratchValIonEntry = |
| 832 |
ValueOperand(ABINonArgReg0, ABINonArgReg1); |
| 833 |
#endif |
| 834 |
static const Register ScratchIonEntry = ABINonArgReg2; |
| 835 |
|
| 836 |
static void CallSymbolicAddress(MacroAssembler& masm, bool isAbsolute, |
| 837 |
SymbolicAddress sym) { |
| 838 |
if (isAbsolute) { |
| 839 |
masm.call(ImmPtr(SymbolicAddressTarget(sym), ImmPtr::NoCheckToken())); |
| 840 |
} else { |
| 841 |
masm.call(sym); |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
// Load instance's instance from the callee. |
| 846 |
static void GenerateJitEntryLoadInstance(MacroAssembler& masm) { |
| 847 |
// ScratchIonEntry := callee => JSFunction* |
| 848 |
unsigned offset = JitFrameLayout::offsetOfCalleeToken(); |
| 849 |
masm.loadFunctionFromCalleeToken(Address(FramePointer, offset), |
| 850 |
ScratchIonEntry); |
| 851 |
|
| 852 |
// ScratchIonEntry := callee->getExtendedSlot(WASM_INSTANCE_SLOT)->toPrivate() |
| 853 |
// => Instance* |
| 854 |
offset = FunctionExtended::offsetOfExtendedSlot( |
| 855 |
FunctionExtended::WASM_INSTANCE_SLOT); |
| 856 |
masm.loadPrivate(Address(ScratchIonEntry, offset), InstanceReg); |
| 857 |
} |
| 858 |
|
| 859 |
// Creates a JS fake exit frame for wasm, so the frame iterators just use |
| 860 |
// JSJit frame iteration. |
| 861 |
// |
| 862 |
// Note: the caller must ensure InstanceReg is valid. |
| 863 |
static void GenerateJitEntryThrow(MacroAssembler& masm) { |
| 864 |
AssertExpectedSP(masm); |
| 865 |
|
| 866 |
MOZ_ASSERT(masm.framePushed() == 0); |
| 867 |
MoveSPForJitABI(masm); |
| 868 |
|
| 869 |
masm.loadPtr(Address(InstanceReg, Instance::offsetOfCx()), ScratchIonEntry); |
| 870 |
masm.enterFakeExitFrameForWasm(ScratchIonEntry, ScratchIonEntry, |
| 871 |
ExitFrameType::WasmGenericJitEntry); |
| 872 |
|
| 873 |
masm.loadPtr(Address(InstanceReg, Instance::offsetOfJSJitExceptionHandler()), |
| 874 |
ScratchIonEntry); |
| 875 |
masm.jump(ScratchIonEntry); |
| 876 |
} |
| 877 |
|
| 878 |
// Helper function for allocating a BigInt and initializing it from an I64 in |
| 879 |
// GenerateJitEntry. The return result is written to scratch. |
| 880 |
// |
| 881 |
// Note that this will create a new frame and must not - in its current form - |
| 882 |
// be called from a context where there is already another stub frame on the |
| 883 |
// stack, as that confuses unwinding during profiling. This was a problem for |
| 884 |
// its use from GenerateImportJitExit, see bug 1754258. Therefore, |
| 885 |
// FuncType::canHaveJitExit prevents the present function from being called for |
| 886 |
// exits. |
| 887 |
static void GenerateBigIntInitialization(MacroAssembler& masm, |
| 888 |
unsigned bytesPushedByPrologue, |
| 889 |
Register64 input, Register scratch, |
| 890 |
const FuncExport& fe, Label* fail) { |
| 891 |
#if JS_BITS_PER_WORD == 32 |
| 892 |
MOZ_ASSERT(input.low != scratch); |
| 893 |
MOZ_ASSERT(input.high != scratch); |
| 894 |
#else |
| 895 |
MOZ_ASSERT(input.reg != scratch); |
| 896 |
#endif |
| 897 |
|
| 898 |
MOZ_ASSERT(masm.framePushed() == 0); |
| 899 |
|
| 900 |
// We need to avoid clobbering other argument registers and the input. |
| 901 |
AllocatableRegisterSet regs(RegisterSet::Volatile()); |
| 902 |
LiveRegisterSet save(regs.asLiveSet()); |
| 903 |
masm.PushRegsInMask(save); |
| 904 |
|
| 905 |
unsigned frameSize = StackDecrementForCall( |
| 906 |
ABIStackAlignment, masm.framePushed() + bytesPushedByPrologue, 0); |
| 907 |
masm.reserveStack(frameSize); |
| 908 |
masm.assertStackAlignment(ABIStackAlignment); |
| 909 |
|
| 910 |
CallSymbolicAddress(masm, !fe.hasEagerStubs(), |
| 911 |
SymbolicAddress::AllocateBigInt); |
| 912 |
masm.storeCallPointerResult(scratch); |
| 913 |
|
| 914 |
masm.assertStackAlignment(ABIStackAlignment); |
| 915 |
masm.freeStack(frameSize); |
| 916 |
|
| 917 |
LiveRegisterSet ignore; |
| 918 |
ignore.add(scratch); |
| 919 |
masm.PopRegsInMaskIgnore(save, ignore); |
| 920 |
|
| 921 |
masm.branchTestPtr(Assembler::Zero, scratch, scratch, fail); |
| 922 |
masm.initializeBigInt64(Scalar::BigInt64, scratch, input); |
| 923 |
} |
| 924 |
|
| 925 |
// Generate a stub that enters wasm from a jit code caller via the jit ABI. |
| 926 |
// |
| 927 |
// ARM64 note: This does not save the PseudoStackPointer so we must be sure to |
| 928 |
// recompute it on every return path, be it normal return or exception return. |
| 929 |
// The JIT code we return to assumes it is correct. |
| 930 |
|
| 931 |
static bool GenerateJitEntry(MacroAssembler& masm, size_t funcExportIndex, |
| 932 |
const FuncExport& fe, const FuncType& funcType, |
| 933 |
const Maybe<ImmPtr>& funcPtr, |
| 934 |
CallableOffsets* offsets) { |
| 935 |
AutoCreatedBy acb(masm, "GenerateJitEntry"); |
| 936 |
|
| 937 |
AssertExpectedSP(masm); |
| 938 |
|
| 939 |
RegisterOrSP sp = masm.getStackPointer(); |
| 940 |
|
| 941 |
GenerateJitEntryPrologue(masm, offsets); |
| 942 |
|
| 943 |
// The jit caller has set up the following stack layout (sp grows to the |
| 944 |
// left): |
| 945 |
// <-- retAddr | descriptor | callee | argc | this | arg1..N |
| 946 |
// |
| 947 |
// GenerateJitEntryPrologue has additionally pushed the caller's frame |
| 948 |
// pointer. The stack pointer is now JitStackAlignment-aligned. |
| 949 |
// |
| 950 |
// We initialize an ExitFooterFrame (with ExitFrameType::WasmGenericJitEntry) |
| 951 |
// immediately below the frame pointer to ensure FP is a valid JS JIT exit |
| 952 |
// frame. |
| 953 |
|
| 954 |
MOZ_ASSERT(masm.framePushed() == 0); |
| 955 |
|
| 956 |
if (funcType.hasUnexposableArgOrRet()) { |
| 957 |
GenerateJitEntryLoadInstance(masm); |
| 958 |
CallSymbolicAddress(masm, !fe.hasEagerStubs(), |
| 959 |
SymbolicAddress::ReportV128JSCall); |
| 960 |
GenerateJitEntryThrow(masm); |
| 961 |
return FinishOffsets(masm, offsets); |
| 962 |
} |
| 963 |
|
| 964 |
// Avoid overlapping aligned stack arguments area with ExitFooterFrame. |
| 965 |
const unsigned AlignedExitFooterFrameSize = |
| 966 |
AlignBytes(ExitFooterFrame::Size(), WasmStackAlignment); |
| 967 |
unsigned normalBytesNeeded = |
| 968 |
AlignedExitFooterFrameSize + StackArgBytesForWasmABI(funcType); |
| 969 |
|
| 970 |
MIRTypeVector coerceArgTypes; |
| 971 |
MOZ_ALWAYS_TRUE(coerceArgTypes.append(MIRType::Int32)); |
| 972 |
MOZ_ALWAYS_TRUE(coerceArgTypes.append(MIRType::Pointer)); |
| 973 |
MOZ_ALWAYS_TRUE(coerceArgTypes.append(MIRType::Pointer)); |
| 974 |
unsigned oolBytesNeeded = |
| 975 |
AlignedExitFooterFrameSize + StackArgBytesForWasmABI(coerceArgTypes); |
| 976 |
|
| 977 |
unsigned bytesNeeded = std::max(normalBytesNeeded, oolBytesNeeded); |
| 978 |
|
| 979 |
// Note the jit caller ensures the stack is aligned *after* the call |
| 980 |
// instruction. |
| 981 |
unsigned frameSize = StackDecrementForCall(WasmStackAlignment, |
| 982 |
masm.framePushed(), bytesNeeded); |
| 983 |
|
| 984 |
// Reserve stack space for wasm ABI arguments, set up like this: |
| 985 |
// <-- ABI args | padding |
| 986 |
masm.reserveStack(frameSize); |
| 987 |
|
| 988 |
MOZ_ASSERT(masm.framePushed() == frameSize); |
| 989 |
|
| 990 |
// Initialize the ExitFooterFrame. |
| 991 |
static_assert(ExitFooterFrame::Size() == sizeof(uintptr_t)); |
| 992 |
masm.storePtr(ImmWord(uint32_t(ExitFrameType::WasmGenericJitEntry)), |
| 993 |
Address(FramePointer, -int32_t(ExitFooterFrame::Size()))); |
| 994 |
|
| 995 |
GenerateJitEntryLoadInstance(masm); |
| 996 |
|
| 997 |
FloatRegister scratchF = ABINonArgDoubleReg; |
| 998 |
Register scratchG = ScratchIonEntry; |
| 999 |
ValueOperand scratchV = ScratchValIonEntry; |
| 1000 |
|
| 1001 |
GenPrintf(DebugChannel::Function, masm, "wasm-function[%d]; arguments ", |
| 1002 |
fe.funcIndex()); |
| 1003 |
|
| 1004 |
// We do two loops: |
| 1005 |
// - one loop up-front will make sure that all the Value tags fit the |
| 1006 |
// expected signature argument types. If at least one inline conversion |
| 1007 |
// fails, we just jump to the OOL path which will call into C++. Inline |
| 1008 |
// conversions are ordered in the way we expect them to happen the most. |
| 1009 |
// - the second loop will unbox the arguments into the right registers. |
| 1010 |
Label oolCall; |
| 1011 |
for (size_t i = 0; i < funcType.args().length(); i++) { |
| 1012 |
Address jitArgAddr(FramePointer, JitFrameLayout::offsetOfActualArg(i)); |
| 1013 |
masm.loadValue(jitArgAddr, scratchV); |
| 1014 |
|
| 1015 |
Label next; |
| 1016 |
switch (funcType.args()[i].kind()) { |
| 1017 |
case ValType::I32: { |
| 1018 |
Label isDouble, isUndefinedOrNull, isBoolean; |
| 1019 |
{ |
| 1020 |
ScratchTagScope tag(masm, scratchV); |
| 1021 |
masm.splitTagForTest(scratchV, tag); |
| 1022 |
|
| 1023 |
// For int32 inputs, just skip. |
| 1024 |
masm.branchTestInt32(Assembler::Equal, tag, &next); |
| 1025 |
|
| 1026 |
masm.branchTestDouble(Assembler::Equal, tag, &isDouble); |
| 1027 |
masm.branchTestUndefined(Assembler::Equal, tag, &isUndefinedOrNull); |
| 1028 |
masm.branchTestNull(Assembler::Equal, tag, &isUndefinedOrNull); |
| 1029 |
masm.branchTestBoolean(Assembler::Equal, tag, &isBoolean); |
| 1030 |
|
| 1031 |
// Other types (symbol, object, strings) go to the C++ call. |
| 1032 |
masm.jump(&oolCall); |
| 1033 |
} |
| 1034 |
|
| 1035 |
Label storeBack; |
| 1036 |
|
| 1037 |
// For double inputs, unbox, truncate and store back. |
| 1038 |
masm.bind(&isDouble); |
| 1039 |
{ |
| 1040 |
masm.unboxDouble(scratchV, scratchF); |
| 1041 |
masm.branchTruncateDoubleMaybeModUint32(scratchF, scratchG, &oolCall); |
| 1042 |
masm.jump(&storeBack); |
| 1043 |
} |
| 1044 |
|
| 1045 |
// For null or undefined, store 0. |
| 1046 |
masm.bind(&isUndefinedOrNull); |
| 1047 |
{ |
| 1048 |
masm.storeValue(Int32Value(0), jitArgAddr); |
| 1049 |
masm.jump(&next); |
| 1050 |
} |
| 1051 |
|
| 1052 |
// For booleans, store the number value back. |
| 1053 |
masm.bind(&isBoolean); |
| 1054 |
masm.unboxBoolean(scratchV, scratchG); |
| 1055 |
// fallthrough: |
| 1056 |
|
| 1057 |
masm.bind(&storeBack); |
| 1058 |
masm.storeValue(JSVAL_TYPE_INT32, scratchG, jitArgAddr); |
| 1059 |
break; |
| 1060 |
} |
| 1061 |
case ValType::I64: { |
| 1062 |
// For BigInt inputs, just skip. Otherwise go to C++ for other |
| 1063 |
// types that require creating a new BigInt or erroring. |
| 1064 |
masm.branchTestBigInt(Assembler::NotEqual, scratchV, &oolCall); |
| 1065 |
break; |
| 1066 |
} |
| 1067 |
case ValType::F32: |
| 1068 |
case ValType::F64: { |
| 1069 |
// Note we can reuse the same code for f32/f64 here, since for the |
| 1070 |
// case of f32, the conversion of f64 to f32 will happen in the |
| 1071 |
// second loop. |
| 1072 |
|
| 1073 |
Label isInt32OrBoolean, isUndefined, isNull; |
| 1074 |
{ |
| 1075 |
ScratchTagScope tag(masm, scratchV); |
| 1076 |
masm.splitTagForTest(scratchV, tag); |
| 1077 |
|
| 1078 |
// For double inputs, just skip. |
| 1079 |
masm.branchTestDouble(Assembler::Equal, tag, &next); |
| 1080 |
|
| 1081 |
masm.branchTestInt32(Assembler::Equal, tag, &isInt32OrBoolean); |
| 1082 |
masm.branchTestUndefined(Assembler::Equal, tag, &isUndefined); |
| 1083 |
masm.branchTestNull(Assembler::Equal, tag, &isNull); |
| 1084 |
masm.branchTestBoolean(Assembler::Equal, tag, &isInt32OrBoolean); |
| 1085 |
|
| 1086 |
// Other types (symbol, object, strings) go to the C++ call. |
| 1087 |
masm.jump(&oolCall); |
| 1088 |
} |
| 1089 |
|
| 1090 |
// For int32 and boolean inputs, convert and rebox. |
| 1091 |
masm.bind(&isInt32OrBoolean); |
| 1092 |
{ |
| 1093 |
masm.convertInt32ToDouble(scratchV.payloadOrValueReg(), scratchF); |
| 1094 |
masm.boxDouble(scratchF, jitArgAddr); |
| 1095 |
masm.jump(&next); |
| 1096 |
} |
| 1097 |
|
| 1098 |
// For undefined (missing argument), store NaN. |
| 1099 |
masm.bind(&isUndefined); |
| 1100 |
{ |
| 1101 |
masm.storeValue(DoubleValue(JS::GenericNaN()), jitArgAddr); |
| 1102 |
masm.jump(&next); |
| 1103 |
} |
| 1104 |
|
| 1105 |
// +null is 0. |
| 1106 |
masm.bind(&isNull); |
| 1107 |
{ |
| 1108 |
masm.storeValue(DoubleValue(0.), jitArgAddr); |
| 1109 |
} |
| 1110 |
break; |
| 1111 |
} |
| 1112 |
case ValType::Ref: { |
| 1113 |
// Guarded against by temporarilyUnsupportedReftypeForEntry() |
| 1114 |
MOZ_RELEASE_ASSERT(funcType.args()[i].refType().isExtern()); |
| 1115 |
masm.branchValueConvertsToWasmAnyRefInline(scratchV, scratchG, scratchF, |
| 1116 |
&next); |
| 1117 |
masm.jump(&oolCall); |
| 1118 |
break; |
| 1119 |
} |
| 1120 |
case ValType::V128: { |
| 1121 |
// Guarded against by hasUnexposableArgOrRet() |
| 1122 |
MOZ_CRASH("unexpected argument type when calling from the jit"); |
| 1123 |
} |
| 1124 |
default: { |
| 1125 |
MOZ_CRASH("unexpected argument type when calling from the jit"); |
| 1126 |
} |
| 1127 |
} |
| 1128 |
masm.nopAlign(CodeAlignment); |
| 1129 |
masm.bind(&next); |
| 1130 |
} |
| 1131 |
|
| 1132 |
Label rejoinBeforeCall; |
| 1133 |
masm.bind(&rejoinBeforeCall); |
| 1134 |
|
| 1135 |
// Convert all the expected values to unboxed values on the stack. |
| 1136 |
ArgTypeVector args(funcType); |
| 1137 |
for (WasmABIArgIter iter(args); !iter.done(); iter++) { |
| 1138 |
Address argv(FramePointer, JitFrameLayout::offsetOfActualArg(iter.index())); |
| 1139 |
bool isStackArg = iter->kind() == ABIArg::Stack; |
| 1140 |
switch (iter.mirType()) { |
| 1141 |
case MIRType::Int32: { |
| 1142 |
Register target = isStackArg ? ScratchIonEntry : iter->gpr(); |
| 1143 |
masm.unboxInt32(argv, target); |
| 1144 |
GenPrintIsize(DebugChannel::Function, masm, target); |
| 1145 |
if (isStackArg) { |
| 1146 |
masm.storePtr(target, Address(sp, iter->offsetFromArgBase())); |
| 1147 |
} |
| 1148 |
break; |
| 1149 |
} |
| 1150 |
case MIRType::Int64: { |
| 1151 |
// The coercion has provided a BigInt value by this point, which |
| 1152 |
// we need to convert to an I64 here. |
| 1153 |
if (isStackArg) { |
| 1154 |
Address dst(sp, iter->offsetFromArgBase()); |
| 1155 |
Register src = scratchV.payloadOrValueReg(); |
| 1156 |
#if JS_BITS_PER_WORD == 64 |
| 1157 |
Register64 scratch64(scratchG); |
| 1158 |
#else |
| 1159 |
Register64 scratch64(scratchG, ABINonArgReg3); |
| 1160 |
#endif |
| 1161 |
masm.unboxBigInt(argv, src); |
| 1162 |
masm.loadBigInt64(src, scratch64); |
| 1163 |
GenPrintI64(DebugChannel::Function, masm, scratch64); |
| 1164 |
masm.store64(scratch64, dst); |
| 1165 |
} else { |
| 1166 |
Register src = scratchG; |
| 1167 |
Register64 target = iter->gpr64(); |
| 1168 |
masm.unboxBigInt(argv, src); |
| 1169 |
masm.loadBigInt64(src, target); |
| 1170 |
GenPrintI64(DebugChannel::Function, masm, target); |
| 1171 |
} |
| 1172 |
break; |
| 1173 |
} |
| 1174 |
case MIRType::Float32: { |
| 1175 |
FloatRegister target = isStackArg ? ABINonArgDoubleReg : iter->fpu(); |
| 1176 |
masm.unboxDouble(argv, ABINonArgDoubleReg); |
| 1177 |
masm.convertDoubleToFloat32(ABINonArgDoubleReg, target); |
| 1178 |
GenPrintF32(DebugChannel::Function, masm, target.asSingle()); |
| 1179 |
if (isStackArg) { |
| 1180 |
masm.storeFloat32(target, Address(sp, iter->offsetFromArgBase())); |
| 1181 |
} |
| 1182 |
break; |
| 1183 |
} |
| 1184 |
case MIRType::Double: { |
| 1185 |
FloatRegister target = isStackArg ? ABINonArgDoubleReg : iter->fpu(); |
| 1186 |
masm.unboxDouble(argv, target); |
| 1187 |
GenPrintF64(DebugChannel::Function, masm, target); |
| 1188 |
if (isStackArg) { |
| 1189 |
masm.storeDouble(target, Address(sp, iter->offsetFromArgBase())); |
| 1190 |
} |
| 1191 |
break; |
| 1192 |
} |
| 1193 |
case MIRType::WasmAnyRef: { |
| 1194 |
ValueOperand src = ScratchValIonEntry; |
| 1195 |
Register target = isStackArg ? ScratchIonEntry : iter->gpr(); |
| 1196 |
masm.loadValue(argv, src); |
| 1197 |
// The loop before should ensure that all values that require boxing |
| 1198 |
// have been taken care of. |
| 1199 |
Label join; |
| 1200 |
Label fail; |
| 1201 |
masm.convertValueToWasmAnyRef(src, target, scratchF, &fail); |
| 1202 |
masm.jump(&join); |
| 1203 |
masm.bind(&fail); |
| 1204 |
masm.breakpoint(); |
| 1205 |
masm.bind(&join); |
| 1206 |
GenPrintPtr(DebugChannel::Function, masm, target); |
| 1207 |
if (isStackArg) { |
| 1208 |
masm.storePtr(target, Address(sp, iter->offsetFromArgBase())); |
| 1209 |
} |
| 1210 |
break; |
| 1211 |
} |
| 1212 |
default: { |
| 1213 |
MOZ_CRASH("unexpected input argument when calling from jit"); |
| 1214 |
} |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
GenPrintf(DebugChannel::Function, masm, "\n"); |
| 1219 |
|
| 1220 |
// Setup wasm register state. |
| 1221 |
masm.loadWasmPinnedRegsFromInstance(mozilla::Nothing()); |
| 1222 |
|
| 1223 |
masm.storePtr(InstanceReg, Address(masm.getStackPointer(), |
| 1224 |
WasmCalleeInstanceOffsetBeforeCall)); |
| 1225 |
|
| 1226 |
// Call into the real function. |
| 1227 |
masm.assertStackAlignment(WasmStackAlignment); |
| 1228 |
CallFuncExport(masm, fe, funcPtr); |
| 1229 |
masm.assertStackAlignment(WasmStackAlignment); |
| 1230 |
|
| 1231 |
GenPrintf(DebugChannel::Function, masm, "wasm-function[%d]; returns ", |
| 1232 |
fe.funcIndex()); |
| 1233 |
|
| 1234 |
// Pop frame. |
| 1235 |
masm.moveToStackPtr(FramePointer); |
| 1236 |
masm.setFramePushed(0); |
| 1237 |
|
| 1238 |
// Store the return value in the JSReturnOperand. |
| 1239 |
Label exception; |
| 1240 |
const ValTypeVector& results = funcType.results(); |
| 1241 |
if (results.length() == 0) { |
| 1242 |
GenPrintf(DebugChannel::Function, masm, "void"); |
| 1243 |
masm.moveValue(UndefinedValue(), JSReturnOperand); |
| 1244 |
} else { |
| 1245 |
MOZ_ASSERT(results.length() == 1, "multi-value return to JS unimplemented"); |
| 1246 |
switch (results[0].kind()) { |
| 1247 |
case ValType::I32: |
| 1248 |
GenPrintIsize(DebugChannel::Function, masm, ReturnReg); |
| 1249 |
#ifdef JS_64BIT |
| 1250 |
// boxNonDouble requires a widened int32 value. |
| 1251 |
masm.widenInt32(ReturnReg); |
| 1252 |
#endif |
| 1253 |
masm.boxNonDouble(JSVAL_TYPE_INT32, ReturnReg, JSReturnOperand); |
| 1254 |
break; |
| 1255 |
case ValType::F32: { |
| 1256 |
masm.canonicalizeFloat(ReturnFloat32Reg); |
| 1257 |
masm.convertFloat32ToDouble(ReturnFloat32Reg, ReturnDoubleReg); |
| 1258 |
GenPrintF64(DebugChannel::Function, masm, ReturnDoubleReg); |
| 1259 |
ScratchDoubleScope fpscratch(masm); |
| 1260 |
masm.boxDouble(ReturnDoubleReg, JSReturnOperand, fpscratch); |
| 1261 |
break; |
| 1262 |
} |
| 1263 |
case ValType::F64: { |
| 1264 |
masm.canonicalizeDouble(ReturnDoubleReg); |
| 1265 |
GenPrintF64(DebugChannel::Function, masm, ReturnDoubleReg); |
| 1266 |
ScratchDoubleScope fpscratch(masm); |
| 1267 |
masm.boxDouble(ReturnDoubleReg, JSReturnOperand, fpscratch); |
| 1268 |
break; |
| 1269 |
} |
| 1270 |
case ValType::I64: { |
| 1271 |
GenPrintI64(DebugChannel::Function, masm, ReturnReg64); |
| 1272 |
MOZ_ASSERT(masm.framePushed() == 0); |
| 1273 |
GenerateBigIntInitialization(masm, 0, ReturnReg64, scratchG, fe, |
| 1274 |
&exception); |
| 1275 |
masm.boxNonDouble(JSVAL_TYPE_BIGINT, scratchG, JSReturnOperand); |
| 1276 |
break; |
| 1277 |
} |
| 1278 |
case ValType::V128: { |
| 1279 |
MOZ_CRASH("unexpected return type when calling from ion to wasm"); |
| 1280 |
} |
| 1281 |
case ValType::Ref: { |
| 1282 |
GenPrintPtr(DebugChannel::Import, masm, ReturnReg); |
| 1283 |
masm.convertWasmAnyRefToValue(InstanceReg, ReturnReg, JSReturnOperand, |
| 1284 |
WasmJitEntryReturnScratch); |
| 1285 |
break; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
} |
| 1289 |
|
| 1290 |
GenPrintf(DebugChannel::Function, masm, "\n"); |
| 1291 |
|
| 1292 |
AssertExpectedSP(masm); |
| 1293 |
GenerateJitEntryEpilogue(masm, offsets); |
| 1294 |
MOZ_ASSERT(masm.framePushed() == 0); |
| 1295 |
|
| 1296 |
// Generate an OOL call to the C++ conversion path. |
| 1297 |
bool hasFallThroughForException = false; |
| 1298 |
if (oolCall.used()) { |
| 1299 |
masm.bind(&oolCall); |
| 1300 |
masm.setFramePushed(frameSize); |
| 1301 |
|
| 1302 |
// Baseline and Ion call C++ runtime via BuiltinThunk with wasm abi, so to |
| 1303 |
// unify the BuiltinThunk's interface we call it here with wasm abi. |
| 1304 |
jit::WasmABIArgIter<MIRTypeVector> argsIter(coerceArgTypes); |
| 1305 |
|
| 1306 |
// argument 0: function index. |
| 1307 |
if (argsIter->kind() == ABIArg::GPR) { |
| 1308 |
masm.movePtr(ImmWord(fe.funcIndex()), argsIter->gpr()); |
| 1309 |
} else { |
| 1310 |
masm.storePtr(ImmWord(fe.funcIndex()), |
| 1311 |
Address(sp, argsIter->offsetFromArgBase())); |
| 1312 |
} |
| 1313 |
argsIter++; |
| 1314 |
|
| 1315 |
// argument 1: instance |
| 1316 |
if (argsIter->kind() == ABIArg::GPR) { |
| 1317 |
masm.movePtr(InstanceReg, argsIter->gpr()); |
| 1318 |
} else { |
| 1319 |
masm.storePtr(InstanceReg, Address(sp, argsIter->offsetFromArgBase())); |
| 1320 |
} |
| 1321 |
argsIter++; |
| 1322 |
|
| 1323 |
// argument 2: effective address of start of argv |
| 1324 |
Address argv(FramePointer, JitFrameLayout::offsetOfActualArgs()); |
| 1325 |
if (argsIter->kind() == ABIArg::GPR) { |
| 1326 |
masm.computeEffectiveAddress(argv, argsIter->gpr()); |
| 1327 |
} else { |
| 1328 |
masm.computeEffectiveAddress(argv, ScratchIonEntry); |
| 1329 |
masm.storePtr(ScratchIonEntry, |
| 1330 |
Address(sp, argsIter->offsetFromArgBase())); |
| 1331 |
} |
| 1332 |
argsIter++; |
| 1333 |
MOZ_ASSERT(argsIter.done()); |
| 1334 |
|
| 1335 |
masm.assertStackAlignment(ABIStackAlignment); |
| 1336 |
CallSymbolicAddress(masm, !fe.hasEagerStubs(), |
| 1337 |
SymbolicAddress::CoerceInPlace_JitEntry); |
| 1338 |
masm.assertStackAlignment(ABIStackAlignment); |
| 1339 |
|
| 1340 |
// No widening is required, as the return value is used as a bool. |
| 1341 |
masm.branchTest32(Assembler::NonZero, ReturnReg, ReturnReg, |
| 1342 |
&rejoinBeforeCall); |
| 1343 |
|
| 1344 |
MOZ_ASSERT(masm.framePushed() == frameSize); |
| 1345 |
masm.freeStack(frameSize); |
| 1346 |
hasFallThroughForException = true; |
| 1347 |
} |
| 1348 |
|
| 1349 |
if (exception.used() || hasFallThroughForException) { |
| 1350 |
masm.bind(&exception); |
| 1351 |
MOZ_ASSERT(masm.framePushed() == 0); |
| 1352 |
GenerateJitEntryThrow(masm); |
| 1353 |
} |
| 1354 |
|
| 1355 |
return FinishOffsets(masm, offsets); |
| 1356 |
} |
| 1357 |
|
| 1358 |
void wasm::GenerateDirectCallFromJit(MacroAssembler& masm, const FuncExport& fe, |
| 1359 |
const Instance& inst, |
| 1360 |
const JitCallStackArgVector& stackArgs, |
| 1361 |
Register scratch, uint32_t* callOffset) { |
| 1362 |
MOZ_ASSERT(!IsCompilingWasm()); |
| 1363 |
|
| 1364 |
const FuncType& funcType = inst.codeMeta().getFuncType(fe.funcIndex()); |
| 1365 |
|
| 1366 |
size_t framePushedAtStart = masm.framePushed(); |
| 1367 |
|
| 1368 |
// Note, if code here pushes a reference value into the frame for its own |
| 1369 |
// purposes (and not just as an argument to the callee) then the frame must be |
| 1370 |
// traced in TraceJitExitFrame, see the case there for DirectWasmJitCall. The |
| 1371 |
// callee will trace values that are pushed as arguments, however. |
| 1372 |
|
| 1373 |
// Push a special frame descriptor that indicates the frame size so we can |
| 1374 |
// directly iterate from the current JIT frame without an extra call. |
| 1375 |
// Note: buildFakeExitFrame pushes an ExitFrameLayout containing the current |
| 1376 |
// frame pointer. We also use this to restore the frame pointer after the |
| 1377 |
// call. |
| 1378 |
*callOffset = masm.buildFakeExitFrame(scratch); |
| 1379 |
// FP := ExitFrameLayout* |
| 1380 |
masm.moveStackPtrTo(FramePointer); |
| 1381 |
size_t framePushedAtFakeFrame = masm.framePushed(); |
| 1382 |
masm.setFramePushed(0); |
| 1383 |
masm.loadJSContext(scratch); |
| 1384 |
masm.enterFakeExitFrame(scratch, scratch, ExitFrameType::DirectWasmJitCall); |
| 1385 |
|
| 1386 |
static_assert(ExitFrameLayout::SizeWithFooter() % WasmStackAlignment == 0); |
| 1387 |
MOZ_ASSERT( |
| 1388 |
(masm.framePushed() + framePushedAtFakeFrame) % WasmStackAlignment == 0); |
| 1389 |
|
| 1390 |
// Move stack arguments to their final locations. |
| 1391 |
unsigned bytesNeeded = StackArgBytesForWasmABI(funcType); |
| 1392 |
bytesNeeded = StackDecrementForCall( |
| 1393 |
WasmStackAlignment, framePushedAtFakeFrame + masm.framePushed(), |
| 1394 |
bytesNeeded); |
| 1395 |
if (bytesNeeded) { |
| 1396 |
masm.reserveStack(bytesNeeded); |
| 1397 |
} |
| 1398 |
size_t fakeFramePushed = masm.framePushed(); |
| 1399 |
|
| 1400 |
GenPrintf(DebugChannel::Function, masm, "wasm-function[%d]; arguments ", |
| 1401 |
fe.funcIndex()); |
| 1402 |
|
| 1403 |
ArgTypeVector args(funcType); |
| 1404 |
for (WasmABIArgIter iter(args); !iter.done(); iter++) { |
| 1405 |
MOZ_ASSERT_IF(iter->kind() == ABIArg::GPR, iter->gpr() != scratch); |
| 1406 |
MOZ_ASSERT_IF(iter->kind() == ABIArg::GPR, iter->gpr() != FramePointer); |
| 1407 |
if (iter->kind() != ABIArg::Stack) { |
| 1408 |
switch (iter.mirType()) { |
| 1409 |
case MIRType::Int32: |
| 1410 |
GenPrintIsize(DebugChannel::Function, masm, iter->gpr()); |
| 1411 |
break; |
| 1412 |
case MIRType::Int64: |
| 1413 |
GenPrintI64(DebugChannel::Function, masm, iter->gpr64()); |
| 1414 |
break; |
| 1415 |
case MIRType::Float32: |
| 1416 |
GenPrintF32(DebugChannel::Function, masm, iter->fpu()); |
| 1417 |
break; |
| 1418 |
case MIRType::Double: |
| 1419 |
GenPrintF64(DebugChannel::Function, masm, iter->fpu()); |
| 1420 |
break; |
| 1421 |
case MIRType::WasmAnyRef: |
| 1422 |
GenPrintPtr(DebugChannel::Function, masm, iter->gpr()); |
| 1423 |
break; |
| 1424 |
case MIRType::StackResults: |
| 1425 |
MOZ_ASSERT(args.isSyntheticStackResultPointerArg(iter.index())); |
| 1426 |
GenPrintPtr(DebugChannel::Function, masm, iter->gpr()); |
| 1427 |
break; |
| 1428 |
default: |
| 1429 |
MOZ_CRASH("ion to wasm fast path can only handle i32/f32/f64"); |
| 1430 |
} |
| 1431 |
continue; |
| 1432 |
} |
| 1433 |
|
| 1434 |
Address dst(masm.getStackPointer(), iter->offsetFromArgBase()); |
| 1435 |
|
| 1436 |
const JitCallStackArg& stackArg = stackArgs[iter.index()]; |
| 1437 |
switch (stackArg.tag()) { |
| 1438 |
case JitCallStackArg::Tag::Imm32: |
| 1439 |
GenPrintf(DebugChannel::Function, masm, "%d ", stackArg.imm32()); |
| 1440 |
masm.storePtr(ImmWord(stackArg.imm32()), dst); |
| 1441 |
break; |
| 1442 |
case JitCallStackArg::Tag::GPR: |
| 1443 |
MOZ_ASSERT(stackArg.gpr() != scratch); |
| 1444 |
MOZ_ASSERT(stackArg.gpr() != FramePointer); |
| 1445 |
GenPrintIsize(DebugChannel::Function, masm, stackArg.gpr()); |
| 1446 |
masm.storePtr(stackArg.gpr(), dst); |
| 1447 |
break; |
| 1448 |
case JitCallStackArg::Tag::FPU: |
| 1449 |
switch (iter.mirType()) { |
| 1450 |
case MIRType::Double: |
| 1451 |
GenPrintF64(DebugChannel::Function, masm, stackArg.fpu()); |
| 1452 |
masm.storeDouble(stackArg.fpu(), dst); |
| 1453 |
break; |
| 1454 |
case MIRType::Float32: |
| 1455 |
GenPrintF32(DebugChannel::Function, masm, stackArg.fpu()); |
| 1456 |
masm.storeFloat32(stackArg.fpu(), dst); |
| 1457 |
break; |
| 1458 |
default: |
| 1459 |
MOZ_CRASH( |
| 1460 |
"unexpected MIR type for a float register in wasm fast call"); |
| 1461 |
} |
| 1462 |
break; |
| 1463 |
case JitCallStackArg::Tag::Address: { |
| 1464 |
// The address offsets were valid *before* we pushed our frame. |
| 1465 |
Address src = stackArg.addr(); |
| 1466 |
MOZ_ASSERT(src.base == masm.getStackPointer()); |
| 1467 |
src.offset += int32_t(framePushedAtFakeFrame + fakeFramePushed - |
| 1468 |
framePushedAtStart); |
| 1469 |
switch (iter.mirType()) { |
| 1470 |
case MIRType::Double: { |
| 1471 |
ScratchDoubleScope fpscratch(masm); |
| 1472 |
GenPrintF64(DebugChannel::Function, masm, fpscratch); |
| 1473 |
masm.loadDouble(src, fpscratch); |
| 1474 |
masm.storeDouble(fpscratch, dst); |
| 1475 |
break; |
| 1476 |
} |
| 1477 |
case MIRType::Float32: { |
| 1478 |
ScratchFloat32Scope fpscratch(masm); |
| 1479 |
masm.loadFloat32(src, fpscratch); |
| 1480 |
GenPrintF32(DebugChannel::Function, masm, fpscratch); |
| 1481 |
masm.storeFloat32(fpscratch, dst); |
| 1482 |
break; |
| 1483 |
} |
| 1484 |
case MIRType::Int32: { |
| 1485 |
masm.loadPtr(src, scratch); |
| 1486 |
GenPrintIsize(DebugChannel::Function, masm, scratch); |
| 1487 |
masm.storePtr(scratch, dst); |
| 1488 |
break; |
| 1489 |
} |
| 1490 |
case MIRType::WasmAnyRef: { |
| 1491 |
masm.loadPtr(src, scratch); |
| 1492 |
GenPrintPtr(DebugChannel::Function, masm, scratch); |
| 1493 |
masm.storePtr(scratch, dst); |
| 1494 |
break; |
| 1495 |
} |
| 1496 |
case MIRType::StackResults: { |
| 1497 |
MOZ_CRASH("multi-value in ion to wasm fast path unimplemented"); |
| 1498 |
} |
| 1499 |
default: { |
| 1500 |
MOZ_CRASH("unexpected MIR type for a stack slot in wasm fast call"); |
| 1501 |
} |
| 1502 |
} |
| 1503 |
break; |
| 1504 |
} |
| 1505 |
case JitCallStackArg::Tag::Undefined: { |
| 1506 |
MOZ_CRASH("can't happen because of arg.kind() check"); |
| 1507 |
} |
| 1508 |
} |
| 1509 |
} |
| 1510 |
|
| 1511 |
GenPrintf(DebugChannel::Function, masm, "\n"); |
| 1512 |
|
| 1513 |
// Load instance; from now on, InstanceReg is live. |
| 1514 |
masm.movePtr(ImmPtr(&inst), InstanceReg); |
| 1515 |
masm.storePtr(InstanceReg, Address(masm.getStackPointer(), |
| 1516 |
WasmCalleeInstanceOffsetBeforeCall)); |
| 1517 |
masm.loadWasmPinnedRegsFromInstance(mozilla::Nothing()); |
| 1518 |
|
| 1519 |
// Actual call. |
| 1520 |
const CodeBlock& codeBlock = inst.code().funcCodeBlock(fe.funcIndex()); |
| 1521 |
const CodeRange& codeRange = codeBlock.codeRange(fe); |
| 1522 |
void* callee = const_cast<uint8_t*>(codeBlock.base()) + |
| 1523 |
codeRange.funcUncheckedCallEntry(); |
| 1524 |
|
| 1525 |
masm.assertStackAlignment(WasmStackAlignment); |
| 1526 |
MoveSPForJitABI(masm); |
| 1527 |
masm.callJit(ImmPtr(callee)); |
| 1528 |
#ifdef JS_CODEGEN_ARM64 |
| 1529 |
// WASM does not always keep PSP in sync with SP. So reinitialize it as it |
| 1530 |
// might be clobbered either by WASM or by any C++ calls within. |
| 1531 |
masm.initPseudoStackPtr(); |
| 1532 |
#endif |
| 1533 |
masm.freeStackTo(fakeFramePushed); |
| 1534 |
masm.assertStackAlignment(WasmStackAlignment); |
| 1535 |
|
| 1536 |
// Store the return value in the appropriate place. |
| 1537 |
GenPrintf(DebugChannel::Function, masm, "wasm-function[%d]; returns ", |
| 1538 |
fe.funcIndex()); |
| 1539 |
const ValTypeVector& results = funcType.results(); |
| 1540 |
if (results.length() == 0) { |
| 1541 |
masm.moveValue(UndefinedValue(), JSReturnOperand); |
| 1542 |
GenPrintf(DebugChannel::Function, masm, "void"); |
| 1543 |
} else { |
| 1544 |
MOZ_ASSERT(results.length() == 1, "multi-value return to JS unimplemented"); |
| 1545 |
switch (results[0].kind()) { |
| 1546 |
case wasm::ValType::I32: |
| 1547 |
// The return value is in ReturnReg, which is what Ion expects. |
| 1548 |
GenPrintIsize(DebugChannel::Function, masm, ReturnReg); |
| 1549 |
#ifdef JS_64BIT |
| 1550 |
masm.widenInt32(ReturnReg); |
| 1551 |
#endif |
| 1552 |
break; |
| 1553 |
case wasm::ValType::I64: |
| 1554 |
// The return value is in ReturnReg64, which is what Ion expects. |
| 1555 |
GenPrintI64(DebugChannel::Function, masm, ReturnReg64); |
| 1556 |
break; |
| 1557 |
case wasm::ValType::F32: |
| 1558 |
masm.canonicalizeFloat(ReturnFloat32Reg); |
| 1559 |
GenPrintF32(DebugChannel::Function, masm, ReturnFloat32Reg); |
| 1560 |
break; |
| 1561 |
case wasm::ValType::F64: |
| 1562 |
masm.canonicalizeDouble(ReturnDoubleReg); |
| 1563 |
GenPrintF64(DebugChannel::Function, masm, ReturnDoubleReg); |
| 1564 |
break; |
| 1565 |
case wasm::ValType::Ref: |
| 1566 |
GenPrintPtr(DebugChannel::Import, masm, ReturnReg); |
| 1567 |
// The call to wasm above preserves the InstanceReg, we don't |
| 1568 |
// need to reload it here. |
| 1569 |
masm.convertWasmAnyRefToValue(InstanceReg, ReturnReg, JSReturnOperand, |
| 1570 |
WasmJitEntryReturnScratch); |
| 1571 |
break; |
| 1572 |
case wasm::ValType::V128: |
| 1573 |
MOZ_CRASH("unexpected return type when calling from ion to wasm"); |
| 1574 |
} |
| 1575 |
} |
| 1576 |
|
| 1577 |
GenPrintf(DebugChannel::Function, masm, "\n"); |
| 1578 |
|
| 1579 |
// Restore the frame pointer. |
| 1580 |
masm.loadPtr(Address(FramePointer, 0), FramePointer); |
| 1581 |
masm.setFramePushed(fakeFramePushed + framePushedAtFakeFrame); |
| 1582 |
|
| 1583 |
// Free args + frame descriptor. |
| 1584 |
masm.leaveExitFrame(bytesNeeded + ExitFrameLayout::Size()); |
| 1585 |
|
| 1586 |
MOZ_ASSERT(framePushedAtStart == masm.framePushed()); |
| 1587 |
} |
| 1588 |
|
| 1589 |
static void StackCopy(MacroAssembler& masm, MIRType type, Register scratch, |
| 1590 |
Address src, Address dst) { |
| 1591 |
if (type == MIRType::Int32) { |
| 1592 |
masm.load32(src, scratch); |
| 1593 |
GenPrintIsize(DebugChannel::Import, masm, scratch); |
| 1594 |
masm.store32(scratch, dst); |
| 1595 |
} else if (type == MIRType::Int64) { |
| 1596 |
#if JS_BITS_PER_WORD == 32 |
| 1597 |
MOZ_RELEASE_ASSERT(src.base != scratch && dst.base != scratch); |
| 1598 |
GenPrintf(DebugChannel::Import, masm, "i64("); |
| 1599 |
masm.load32(LowWord(src), scratch); |
| 1600 |
GenPrintIsize(DebugChannel::Import, masm, scratch); |
| 1601 |
masm.store32(scratch, LowWord(dst)); |
| 1602 |
masm.load32(HighWord(src), scratch); |
| 1603 |
GenPrintIsize(DebugChannel::Import, masm, scratch); |
| 1604 |
masm.store32(scratch, HighWord(dst)); |
| 1605 |
GenPrintf(DebugChannel::Import, masm, ") "); |
| 1606 |
#else |
| 1607 |
Register64 scratch64(scratch); |
| 1608 |
masm.load64(src, scratch64); |
| 1609 |
GenPrintIsize(DebugChannel::Import, masm, scratch); |
| 1610 |
masm.store64(scratch64, dst); |
| 1611 |
#endif |
| 1612 |
} else if (type == MIRType::WasmAnyRef || type == MIRType::Pointer || |
| 1613 |
type == MIRType::StackResults) { |
| 1614 |
masm.loadPtr(src, scratch); |
| 1615 |
GenPrintPtr(DebugChannel::Import, masm, scratch); |
| 1616 |
masm.storePtr(scratch, dst); |
| 1617 |
} else if (type == MIRType::Float32) { |
| 1618 |
ScratchFloat32Scope fpscratch(masm); |
| 1619 |
masm.loadFloat32(src, fpscratch); |
| 1620 |
GenPrintF32(DebugChannel::Import, masm, fpscratch); |
| 1621 |
masm.storeFloat32(fpscratch, dst); |
| 1622 |
} else if (type == MIRType::Double) { |
| 1623 |
ScratchDoubleScope fpscratch(masm); |
| 1624 |
masm.loadDouble(src, fpscratch); |
| 1625 |
GenPrintF64(DebugChannel::Import, masm, fpscratch); |
| 1626 |
masm.storeDouble(fpscratch, dst); |
| 1627 |
#ifdef ENABLE_WASM_SIMD |
| 1628 |
} else if (type == MIRType::Simd128) { |
| 1629 |
ScratchSimd128Scope fpscratch(masm); |
| 1630 |
masm.loadUnalignedSimd128(src, fpscratch); |
| 1631 |
GenPrintV128(DebugChannel::Import, masm, fpscratch); |
| 1632 |
masm.storeUnalignedSimd128(fpscratch, dst); |
| 1633 |
#endif |
| 1634 |
} else { |
| 1635 |
MOZ_CRASH("StackCopy: unexpected type"); |
| 1636 |
} |
| 1637 |
} |
| 1638 |
|
| 1639 |
static void FillArgumentArrayForInterpExit(MacroAssembler& masm, |
| 1640 |
unsigned funcImportIndex, |
| 1641 |
const FuncType& funcType, |
| 1642 |
unsigned argOffset, |
| 1643 |
Register scratch) { |
| 1644 |
// This is `sizeof(FrameWithInstances) - ShadowStackSpace` because the latter |
| 1645 |
// is accounted for by the ABIArgIter. |
| 1646 |
const unsigned offsetFromFPToCallerStackArgs = |
| 1647 |
sizeof(FrameWithInstances) - jit::ShadowStackSpace; |
| 1648 |
|
| 1649 |
GenPrintf(DebugChannel::Import, masm, "wasm-import[%u]; arguments ", |
| 1650 |
funcImportIndex); |
| 1651 |
|
| 1652 |
ArgTypeVector args(funcType); |
| 1653 |
for (ABIArgIter i(args); !i.done(); i++) { |
| 1654 |
Address dst(masm.getStackPointer(), argOffset + i.index() * sizeof(Value)); |
| 1655 |
|
| 1656 |
MIRType type = i.mirType(); |
| 1657 |
MOZ_ASSERT(args.isSyntheticStackResultPointerArg(i.index()) == |
| 1658 |
(type == MIRType::StackResults)); |
| 1659 |
switch (i->kind()) { |
| 1660 |
case ABIArg::GPR: |
| 1661 |
if (type == MIRType::Int32) { |
| 1662 |
GenPrintIsize(DebugChannel::Import, masm, i->gpr()); |
| 1663 |
masm.store32(i->gpr(), dst); |
| 1664 |
} else if (type == MIRType::Int64) { |
| 1665 |
GenPrintI64(DebugChannel::Import, masm, i->gpr64()); |
| 1666 |
masm.store64(i->gpr64(), dst); |
| 1667 |
} else if (type == MIRType::WasmAnyRef) { |
| 1668 |
GenPrintPtr(DebugChannel::Import, masm, i->gpr()); |
| 1669 |
masm.storePtr(i->gpr(), dst); |
| 1670 |
} else if (type == MIRType::StackResults) { |
| 1671 |
GenPrintPtr(DebugChannel::Import, masm, i->gpr()); |
| 1672 |
masm.storePtr(i->gpr(), dst); |
| 1673 |
} else { |
| 1674 |
MOZ_CRASH( |
| 1675 |
"FillArgumentArrayForInterpExit, ABIArg::GPR: unexpected type"); |
| 1676 |
} |
| 1677 |
break; |
| 1678 |
#ifdef JS_CODEGEN_REGISTER_PAIR |
| 1679 |
case ABIArg::GPR_PAIR: |
| 1680 |
if (type == MIRType::Int64) { |
| 1681 |
GenPrintI64(DebugChannel::Import, masm, i->gpr64()); |
| 1682 |
masm.store64(i->gpr64(), dst); |
| 1683 |
} else { |
| 1684 |
MOZ_CRASH("wasm uses hardfp for function calls."); |
| 1685 |
} |
| 1686 |
break; |
| 1687 |
#endif |
| 1688 |
case ABIArg::FPU: { |
| 1689 |
FloatRegister srcReg = i->fpu(); |
| 1690 |
if (type == MIRType::Double) { |
| 1691 |
GenPrintF64(DebugChannel::Import, masm, srcReg); |
| 1692 |
masm.storeDouble(srcReg, dst); |
| 1693 |
} else if (type == MIRType::Float32) { |
| 1694 |
// Preserve the NaN pattern in the input. |
| 1695 |
GenPrintF32(DebugChannel::Import, masm, srcReg); |
| 1696 |
masm.storeFloat32(srcReg, dst); |
| 1697 |
} else if (type == MIRType::Simd128) { |
| 1698 |
// The value should never escape; the call will be stopped later as |
| 1699 |
// the import is being called. But we should generate something sane |
| 1700 |
// here for the boxed case since a debugger or the stack walker may |
| 1701 |
// observe something. |
| 1702 |
ScratchDoubleScope dscratch(masm); |
| 1703 |
masm.loadConstantDouble(0, dscratch); |
| 1704 |
GenPrintF64(DebugChannel::Import, masm, dscratch); |
| 1705 |
masm.storeDouble(dscratch, dst); |
| 1706 |
} else { |
| 1707 |
MOZ_CRASH("Unknown MIRType in wasm exit stub"); |
| 1708 |
} |
| 1709 |
break; |
| 1710 |
} |
| 1711 |
case ABIArg::Stack: { |
| 1712 |
Address src(FramePointer, |
| 1713 |
offsetFromFPToCallerStackArgs + i->offsetFromArgBase()); |
| 1714 |
if (type == MIRType::Simd128) { |
| 1715 |
// As above. StackCopy does not know this trick. |
| 1716 |
ScratchDoubleScope dscratch(masm); |
| 1717 |
masm.loadConstantDouble(0, dscratch); |
| 1718 |
GenPrintF64(DebugChannel::Import, masm, dscratch); |
| 1719 |
masm.storeDouble(dscratch, dst); |
| 1720 |
} else { |
| 1721 |
StackCopy(masm, type, scratch, src, dst); |
| 1722 |
} |
| 1723 |
break; |
| 1724 |
} |
| 1725 |
case ABIArg::Uninitialized: |
| 1726 |
MOZ_CRASH("Uninitialized ABIArg kind"); |
| 1727 |
} |
| 1728 |
} |
| 1729 |
GenPrintf(DebugChannel::Import, masm, "\n"); |
| 1730 |
} |
| 1731 |
|
| 1732 |
// Note, this may destroy the values in incoming argument registers as a result |
| 1733 |
// of Spectre mitigation. |
| 1734 |
static void FillArgumentArrayForJitExit(MacroAssembler& masm, Register instance, |
| 1735 |
unsigned funcImportIndex, |
| 1736 |
const FuncType& funcType, |
| 1737 |
unsigned argOffset, Register scratch, |
| 1738 |
Register scratch2, Label* throwLabel) { |
| 1739 |
MOZ_ASSERT(scratch != scratch2); |
| 1740 |
|
| 1741 |
// This is `sizeof(FrameWithInstances) - ShadowStackSpace` because the latter |
| 1742 |
// is accounted for by the ABIArgIter. |
| 1743 |
const unsigned offsetFromFPToCallerStackArgs = |
| 1744 |
sizeof(FrameWithInstances) - jit::ShadowStackSpace; |
| 1745 |
|
| 1746 |
// This loop does not root the values that are being constructed in |
| 1747 |
// for the arguments. Allocations that are generated by code either |
| 1748 |
// in the loop or called from it should be NoGC allocations. |
| 1749 |
GenPrintf(DebugChannel::Import, masm, "wasm-import[%u]; arguments ", |
| 1750 |
funcImportIndex); |
| 1751 |
|
| 1752 |
ArgTypeVector args(funcType); |
| 1753 |
for (ABIArgIter i(args); !i.done(); i++) { |
| 1754 |
Address dst(masm.getStackPointer(), argOffset + i.index() * sizeof(Value)); |
| 1755 |
|
| 1756 |
MIRType type = i.mirType(); |
| 1757 |
MOZ_ASSERT(args.isSyntheticStackResultPointerArg(i.index()) == |
| 1758 |
(type == MIRType::StackResults)); |
| 1759 |
switch (i->kind()) { |
| 1760 |
case ABIArg::GPR: |
| 1761 |
if (type == MIRType::Int32) { |
| 1762 |
GenPrintIsize(DebugChannel::Import, masm, i->gpr()); |
| 1763 |
masm.storeValue(JSVAL_TYPE_INT32, i->gpr(), dst); |
| 1764 |
} else if (type == MIRType::Int64) { |
| 1765 |
// FuncType::canHaveJitExit should prevent this. Also see comments |
| 1766 |
// at GenerateBigIntInitialization. |
| 1767 |
MOZ_CRASH("Should not happen"); |
| 1768 |
} else if (type == MIRType::WasmAnyRef) { |
| 1769 |
// This works also for FuncRef because it is distinguishable from |
| 1770 |
// a boxed AnyRef. |
| 1771 |
masm.movePtr(i->gpr(), scratch2); |
| 1772 |
GenPrintPtr(DebugChannel::Import, masm, scratch2); |
| 1773 |
masm.convertWasmAnyRefToValue(instance, scratch2, dst, scratch); |
| 1774 |
} else if (type == MIRType::StackResults) { |
| 1775 |
MOZ_CRASH("Multi-result exit to JIT unimplemented"); |
| 1776 |
} else { |
| 1777 |
MOZ_CRASH( |
| 1778 |
"FillArgumentArrayForJitExit, ABIArg::GPR: unexpected type"); |
| 1779 |
} |
| 1780 |
break; |
| 1781 |
#ifdef JS_CODEGEN_REGISTER_PAIR |
| 1782 |
case ABIArg::GPR_PAIR: |
| 1783 |
if (type == MIRType::Int64) { |
| 1784 |
// FuncType::canHaveJitExit should prevent this. Also see comments |
| 1785 |
// at GenerateBigIntInitialization. |
| 1786 |
MOZ_CRASH("Should not happen"); |
| 1787 |
} else { |
| 1788 |
MOZ_CRASH("wasm uses hardfp for function calls."); |
| 1789 |
} |
| 1790 |
break; |
| 1791 |
#endif |
| 1792 |
case ABIArg::FPU: { |
| 1793 |
FloatRegister srcReg = i->fpu(); |
| 1794 |
if (type == MIRType::Double) { |
| 1795 |
// Preserve the NaN pattern in the input. |
| 1796 |
ScratchDoubleScope fpscratch(masm); |
| 1797 |
masm.moveDouble(srcReg, fpscratch); |
| 1798 |
masm.canonicalizeDouble(fpscratch); |
| 1799 |
GenPrintF64(DebugChannel::Import, masm, fpscratch); |
| 1800 |
masm.boxDouble(fpscratch, dst); |
| 1801 |
} else if (type == MIRType::Float32) { |
| 1802 |
// JS::Values can't store Float32, so convert to a Double. |
| 1803 |
ScratchDoubleScope fpscratch(masm); |
| 1804 |
masm.convertFloat32ToDouble(srcReg, fpscratch); |
| 1805 |
masm.canonicalizeDouble(fpscratch); |
| 1806 |
GenPrintF64(DebugChannel::Import, masm, fpscratch); |
| 1807 |
masm.boxDouble(fpscratch, dst); |
| 1808 |
} else if (type == MIRType::Simd128) { |
| 1809 |
// The value should never escape; the call will be stopped later as |
| 1810 |
// the import is being called. But we should generate something sane |
| 1811 |
// here for the boxed case since a debugger or the stack walker may |
| 1812 |
// observe something. |
| 1813 |
ScratchDoubleScope dscratch(masm); |
| 1814 |
masm.loadConstantDouble(0, dscratch); |
| 1815 |
GenPrintF64(DebugChannel::Import, masm, dscratch); |
| 1816 |
masm.boxDouble(dscratch, dst); |
| 1817 |
} else { |
| 1818 |
MOZ_CRASH("Unknown MIRType in wasm exit stub"); |
| 1819 |
} |
| 1820 |
break; |
| 1821 |
} |
| 1822 |
case ABIArg::Stack: { |
| 1823 |
Address src(FramePointer, |
| 1824 |
offsetFromFPToCallerStackArgs + i->offsetFromArgBase()); |
| 1825 |
if (type == MIRType::Int32) { |
| 1826 |
masm.load32(src, scratch); |
| 1827 |
GenPrintIsize(DebugChannel::Import, masm, scratch); |
| 1828 |
masm.storeValue(JSVAL_TYPE_INT32, scratch, dst); |
| 1829 |
} else if (type == MIRType::Int64) { |
| 1830 |
// FuncType::canHaveJitExit should prevent this. Also see comments |
| 1831 |
// at GenerateBigIntInitialization. |
| 1832 |
MOZ_CRASH("Should not happen"); |
| 1833 |
} else if (type == MIRType::WasmAnyRef) { |
| 1834 |
// This works also for FuncRef because it is distinguishable from a |
| 1835 |
// boxed AnyRef. |
| 1836 |
masm.loadPtr(src, scratch); |
| 1837 |
GenPrintPtr(DebugChannel::Import, masm, scratch); |
| 1838 |
masm.convertWasmAnyRefToValue(instance, scratch, dst, scratch2); |
| 1839 |
} else if (IsFloatingPointType(type)) { |
| 1840 |
ScratchDoubleScope dscratch(masm); |
| 1841 |
FloatRegister fscratch = dscratch.asSingle(); |
| 1842 |
if (type == MIRType::Float32) { |
| 1843 |
masm.loadFloat32(src, fscratch); |
| 1844 |
masm.convertFloat32ToDouble(fscratch, dscratch); |
| 1845 |
} else { |
| 1846 |
masm.loadDouble(src, dscratch); |
| 1847 |
} |
| 1848 |
masm.canonicalizeDouble(dscratch); |
| 1849 |
GenPrintF64(DebugChannel::Import, masm, dscratch); |
| 1850 |
masm.boxDouble(dscratch, dst); |
| 1851 |
} else if (type == MIRType::Simd128) { |
| 1852 |
// The value should never escape; the call will be stopped later as |
| 1853 |
// the import is being called. But we should generate something |
| 1854 |
// sane here for the boxed case since a debugger or the stack walker |
| 1855 |
// may observe something. |
| 1856 |
ScratchDoubleScope dscratch(masm); |
| 1857 |
masm.loadConstantDouble(0, dscratch); |
| 1858 |
GenPrintF64(DebugChannel::Import, masm, dscratch); |
| 1859 |
masm.boxDouble(dscratch, dst); |
| 1860 |
} else { |
| 1861 |
MOZ_CRASH( |
| 1862 |
"FillArgumentArrayForJitExit, ABIArg::Stack: unexpected type"); |
| 1863 |
} |
| 1864 |
break; |
| 1865 |
} |
| 1866 |
case ABIArg::Uninitialized: |
| 1867 |
MOZ_CRASH("Uninitialized ABIArg kind"); |
| 1868 |
} |
| 1869 |
} |
| 1870 |
GenPrintf(DebugChannel::Import, masm, "\n"); |
| 1871 |
} |
| 1872 |
|
| 1873 |
static bool AddStackCheckForImportFunctionEntry(jit::MacroAssembler& masm, |
| 1874 |
unsigned reserve, |
| 1875 |
const FuncType& funcType, |
| 1876 |
StackMaps* stackMaps) { |
| 1877 |
std::pair<CodeOffset, uint32_t> pair = |
| 1878 |
masm.wasmReserveStackChecked(reserve, TrapSiteDesc()); |
| 1879 |
|
| 1880 |
// Attempt to create stack maps for masm.wasmReserveStackChecked. |
| 1881 |
ArgTypeVector argTypes(funcType); |
| 1882 |
RegisterOffsets trapExitLayout; |
| 1883 |
size_t trapExitLayoutNumWords; |
| 1884 |
GenerateTrapExitRegisterOffsets(&trapExitLayout, &trapExitLayoutNumWords); |
| 1885 |
CodeOffset trapInsnOffset = pair.first; |
| 1886 |
size_t nBytesReservedBeforeTrap = pair.second; |
| 1887 |
size_t nInboundStackArgBytes = StackArgAreaSizeUnaligned(argTypes); |
| 1888 |
wasm::StackMap* stackMap = nullptr; |
| 1889 |
if (!CreateStackMapForFunctionEntryTrap( |
| 1890 |
argTypes, trapExitLayout, trapExitLayoutNumWords, |
| 1891 |
nBytesReservedBeforeTrap, nInboundStackArgBytes, &stackMap)) { |
| 1892 |
return false; |
| 1893 |
} |
| 1894 |
|
| 1895 |
// In debug builds, we'll always have a stack map, even if there are no |
| 1896 |
// refs to track. |
| 1897 |
MOZ_ASSERT(stackMap); |
| 1898 |
if (stackMap && !stackMaps->add(trapInsnOffset.offset(), stackMap)) { |
| 1899 |
stackMap->destroy(); |
| 1900 |
return false; |
| 1901 |
} |
| 1902 |
return true; |
| 1903 |
} |
| 1904 |
|
| 1905 |
// Generate a wrapper function with the standard intra-wasm call ABI which |
| 1906 |
// simply calls an import. This wrapper function allows any import to be treated |
| 1907 |
// like a normal wasm function for the purposes of exports and table calls. In |
| 1908 |
// particular, the wrapper function provides: |
| 1909 |
// - a table entry, so JS imports can be put into tables |
| 1910 |
// - normal entries, so that, if the import is re-exported, an entry stub can |
| 1911 |
// be generated and called without any special cases |
| 1912 |
static bool GenerateImportFunction(jit::MacroAssembler& masm, |
| 1913 |
uint32_t funcImportInstanceOffset, |
| 1914 |
const FuncType& funcType, |
| 1915 |
CallIndirectId callIndirectId, |
| 1916 |
FuncOffsets* offsets, StackMaps* stackMaps) { |
| 1917 |
AutoCreatedBy acb(masm, "wasm::GenerateImportFunction"); |
| 1918 |
|
| 1919 |
AssertExpectedSP(masm); |
| 1920 |
|
| 1921 |
GenerateFunctionPrologue(masm, callIndirectId, Nothing(), offsets); |
| 1922 |
|
| 1923 |
MOZ_ASSERT(masm.framePushed() == 0); |
| 1924 |
const unsigned sizeOfInstanceSlot = sizeof(void*); |
| 1925 |
unsigned framePushed = StackDecrementForCall( |
| 1926 |
WasmStackAlignment, |
| 1927 |
sizeof(Frame), // pushed by prologue |
| 1928 |
StackArgBytesForWasmABI(funcType) + sizeOfInstanceSlot); |
| 1929 |
|
| 1930 |
if (!AddStackCheckForImportFunctionEntry(masm, framePushed, funcType, |
| 1931 |
stackMaps)) { |
| 1932 |
return false; |
| 1933 |
} |
| 1934 |
|
| 1935 |
MOZ_ASSERT(masm.framePushed() == framePushed); |
| 1936 |
|
| 1937 |
masm.storePtr(InstanceReg, Address(masm.getStackPointer(), |
| 1938 |
framePushed - sizeOfInstanceSlot)); |
| 1939 |
|
| 1940 |
// The argument register state is already setup by our caller. We just need |
| 1941 |
// to be sure not to clobber it before the call. |
| 1942 |
Register scratch = ABINonArgReg0; |
| 1943 |
|
| 1944 |
// Copy our frame's stack arguments to the callee frame's stack argument. |
| 1945 |
// |
| 1946 |
// Note offsetFromFPToCallerStackArgs is sizeof(Frame) because the |
| 1947 |
// WasmABIArgIter accounts for both the ShadowStackSpace and the instance |
| 1948 |
// fields of FrameWithInstances. |
| 1949 |
unsigned offsetFromFPToCallerStackArgs = sizeof(Frame); |
| 1950 |
ArgTypeVector args(funcType); |
| 1951 |
for (WasmABIArgIter i(args); !i.done(); i++) { |
| 1952 |
if (i->kind() != ABIArg::Stack) { |
| 1953 |
continue; |
| 1954 |
} |
| 1955 |
|
| 1956 |
Address src(FramePointer, |
| 1957 |
offsetFromFPToCallerStackArgs + i->offsetFromArgBase()); |
| 1958 |
Address dst(masm.getStackPointer(), i->offsetFromArgBase()); |
| 1959 |
GenPrintf(DebugChannel::Import, masm, |
| 1960 |
"calling exotic import function with arguments: "); |
| 1961 |
StackCopy(masm, i.mirType(), scratch, src, dst); |
| 1962 |
GenPrintf(DebugChannel::Import, masm, "\n"); |
| 1963 |
} |
| 1964 |
|
| 1965 |
// Call the import exit stub. |
| 1966 |
CallSiteDesc desc(CallSiteKind::Import); |
| 1967 |
MoveSPForJitABI(masm); |
| 1968 |
masm.wasmCallImport(desc, CalleeDesc::import(funcImportInstanceOffset)); |
| 1969 |
|
| 1970 |
// Restore the instance register and pinned regs, per wasm function ABI. |
| 1971 |
masm.loadPtr( |
| 1972 |
Address(masm.getStackPointer(), framePushed - sizeOfInstanceSlot), |
| 1973 |
InstanceReg); |
| 1974 |
masm.loadWasmPinnedRegsFromInstance(mozilla::Nothing()); |
| 1975 |
|
| 1976 |
// Restore cx->realm. |
| 1977 |
masm.switchToWasmInstanceRealm(ABINonArgReturnReg0, ABINonArgReturnReg1); |
| 1978 |
|
| 1979 |
GenerateFunctionEpilogue(masm, framePushed, offsets); |
| 1980 |
return FinishOffsets(masm, offsets); |
| 1981 |
} |
| 1982 |
|
| 1983 |
static const unsigned STUBS_LIFO_DEFAULT_CHUNK_SIZE = 4 * 1024; |
| 1984 |
|
| 1985 |
// Generate a stub that is called via the internal ABI derived from the |
| 1986 |
// signature of the import and calls into an appropriate callImport C++ |
| 1987 |
// function, having boxed all the ABI arguments into a homogeneous Value array. |
| 1988 |
static bool GenerateImportInterpExit(MacroAssembler& masm, const FuncImport& fi, |
| 1989 |
const FuncType& funcType, |
| 1990 |
uint32_t funcImportIndex, |
| 1991 |
Label* throwLabel, |
| 1992 |
CallableOffsets* offsets) { |
| 1993 |
AutoCreatedBy acb(masm, "GenerateImportInterpExit"); |
| 1994 |
|
| 1995 |
AssertExpectedSP(masm); |
| 1996 |
masm.setFramePushed(0); |
| 1997 |
|
| 1998 |
// Argument types for Instance::callImport_*: |
| 1999 |
static const MIRType typeArray[] = {MIRType::Pointer, // Instance* |
| 2000 |
MIRType::Pointer, // funcImportIndex |
| 2001 |
MIRType::Int32, // argc |
| 2002 |
MIRType::Pointer}; // argv |
| 2003 |
MIRTypeVector invokeArgTypes; |
| 2004 |
MOZ_ALWAYS_TRUE(invokeArgTypes.append(typeArray, std::size(typeArray))); |
| 2005 |
|
| 2006 |
// At the point of the call, the stack layout is: |
| 2007 |
// |
| 2008 |
// | stack args | padding | argv[] | padding | retaddr | caller stack | ... |
| 2009 |
// ^ |
| 2010 |
// +-- sp |
| 2011 |
// |
| 2012 |
// The padding between stack args and argv ensures that argv is aligned on a |
| 2013 |
// Value boundary. The padding between argv and retaddr ensures that sp is |
| 2014 |
// aligned. The caller stack includes a ShadowStackArea and the instance |
| 2015 |
// fields before the args, see WasmFrame.h. |
| 2016 |
// |
| 2017 |
// The 'double' alignment is correct since the argv[] is a Value array. |
| 2018 |
unsigned argOffset = |
| 2019 |
AlignBytes(StackArgBytesForNativeABI(invokeArgTypes), sizeof(double)); |
| 2020 |
// The abiArgCount includes a stack result pointer argument if needed. |
| 2021 |
unsigned abiArgCount = ArgTypeVector(funcType).lengthWithStackResults(); |
| 2022 |
unsigned argBytes = std::max<size_t>(1, abiArgCount) * sizeof(Value); |
| 2023 |
unsigned framePushed = |
| 2024 |
StackDecrementForCall(ABIStackAlignment, |
| 2025 |
sizeof(Frame), // pushed by prologue |
| 2026 |
argOffset + argBytes); |
| 2027 |
|
| 2028 |
GenerateExitPrologue(masm, framePushed, ExitReason::Fixed::ImportInterp, |
| 2029 |
offsets); |
| 2030 |
|
| 2031 |
// Fill the argument array. |
| 2032 |
Register scratch = ABINonArgReturnReg0; |
| 2033 |
FillArgumentArrayForInterpExit(masm, funcImportIndex, funcType, argOffset, |
| 2034 |
scratch); |
| 2035 |
|
| 2036 |
// Prepare the arguments for the call to Instance::callImport_*. |
| 2037 |
ABIArgMIRTypeIter i(invokeArgTypes); |
| 2038 |
|
| 2039 |
// argument 0: Instance* |
| 2040 |
if (i->kind() == ABIArg::GPR) { |
| 2041 |
masm.movePtr(InstanceReg, i->gpr()); |
| 2042 |
} else { |
| 2043 |
masm.storePtr(InstanceReg, |
| 2044 |
Address(masm.getStackPointer(), i->offsetFromArgBase())); |
| 2045 |
} |
| 2046 |
i++; |
| 2047 |
|
| 2048 |
// argument 1: funcImportIndex |
| 2049 |
if (i->kind() == ABIArg::GPR) { |
| 2050 |
masm.mov(ImmWord(funcImportIndex), i->gpr()); |
| 2051 |
} else { |
| 2052 |
masm.store32(Imm32(funcImportIndex), |
| 2053 |
Address(masm.getStackPointer(), i->offsetFromArgBase())); |
| 2054 |
} |
| 2055 |
i++; |
| 2056 |
|
| 2057 |
// argument 2: argc |
| 2058 |
unsigned argc = abiArgCount; |
| 2059 |
if (i->kind() == ABIArg::GPR) { |
| 2060 |
masm.mov(ImmWord(argc), i->gpr()); |
| 2061 |
} else { |
| 2062 |
masm.store32(Imm32(argc), |
| 2063 |
Address(masm.getStackPointer(), i->offsetFromArgBase())); |
| 2064 |
} |
| 2065 |
i++; |
| 2066 |
|
| 2067 |
// argument 3: argv |
| 2068 |
Address argv(masm.getStackPointer(), argOffset); |
| 2069 |
if (i->kind() == ABIArg::GPR) { |
| 2070 |
masm.computeEffectiveAddress(argv, i->gpr()); |
| 2071 |
} else { |
| 2072 |
masm.computeEffectiveAddress(argv, scratch); |
| 2073 |
masm.storePtr(scratch, |
| 2074 |
Address(masm.getStackPointer(), i->offsetFromArgBase())); |
| 2075 |
} |
| 2076 |
i++; |
| 2077 |
MOZ_ASSERT(i.done()); |
| 2078 |
|
| 2079 |
// Make the call, test whether it succeeded, and extract the return value. |
| 2080 |
AssertStackAlignment(masm, ABIStackAlignment); |
| 2081 |
masm.call(SymbolicAddress::CallImport_General); |
| 2082 |
masm.branchTest32(Assembler::Zero, ReturnReg, ReturnReg, throwLabel); |
| 2083 |
|
| 2084 |
ResultType resultType = ResultType::Vector(funcType.results()); |
| 2085 |
ValType registerResultType; |
| 2086 |
for (ABIResultIter iter(resultType); !iter.done(); iter.next()) { |
| 2087 |
if (iter.cur().inRegister()) { |
| 2088 |
MOZ_ASSERT(!registerResultType.isValid()); |
| 2089 |
registerResultType = iter.cur().type(); |
| 2090 |
} |
| 2091 |
} |
| 2092 |
if (!registerResultType.isValid()) { |
| 2093 |
GenPrintf(DebugChannel::Import, masm, "wasm-import[%u]; returns ", |
| 2094 |
funcImportIndex); |
| 2095 |
GenPrintf(DebugChannel::Import, masm, "void"); |
| 2096 |
} else { |
| 2097 |
switch (registerResultType.kind()) { |
| 2098 |
case ValType::I32: |
| 2099 |
masm.load32(argv, ReturnReg); |
| 2100 |
// No widening is required, as we know the value comes from an i32 load. |
| 2101 |
GenPrintf(DebugChannel::Import, masm, "wasm-import[%u]; returns ", |
| 2102 |
funcImportIndex); |
| 2103 |
GenPrintIsize(DebugChannel::Import, masm, ReturnReg); |
| 2104 |
break; |
| 2105 |
case ValType::I64: |
| 2106 |
masm.load64(argv, ReturnReg64); |
| 2107 |
GenPrintf(DebugChannel::Import, masm, "wasm-import[%u]; returns ", |
| 2108 |
funcImportIndex); |
| 2109 |
GenPrintI64(DebugChannel::Import, masm, ReturnReg64); |
| 2110 |
break; |
| 2111 |
case ValType::V128: |
| 2112 |
// Note, CallImport_Rtt/V128 currently always throws, so we should never |
| 2113 |
// reach this point. |
| 2114 |
masm.breakpoint(); |
| 2115 |
break; |
| 2116 |
case ValType::F32: |
| 2117 |
masm.loadFloat32(argv, ReturnFloat32Reg); |
| 2118 |
GenPrintf(DebugChannel::Import, masm, "wasm-import[%u]; returns ", |
| 2119 |
funcImportIndex); |
| 2120 |
GenPrintF32(DebugChannel::Import, masm, ReturnFloat32Reg); |
| 2121 |
break; |
| 2122 |
case ValType::F64: |
| 2123 |
masm.loadDouble(argv, ReturnDoubleReg); |
| 2124 |
GenPrintf(DebugChannel::Import, masm, "wasm-import[%u]; returns ", |
| 2125 |
funcImportIndex); |
| 2126 |
GenPrintF64(DebugChannel::Import, masm, ReturnDoubleReg); |
| 2127 |
break; |
| 2128 |
case ValType::Ref: |
| 2129 |
masm.loadPtr(argv, ReturnReg); |
| 2130 |
GenPrintf(DebugChannel::Import, masm, "wasm-import[%u]; returns ", |
| 2131 |
funcImportIndex); |
| 2132 |
GenPrintPtr(DebugChannel::Import, masm, ReturnReg); |
| 2133 |
break; |
| 2134 |
} |
| 2135 |
} |
| 2136 |
|
| 2137 |
GenPrintf(DebugChannel::Import, masm, "\n"); |
| 2138 |
|
| 2139 |
// The native ABI preserves the instance, heap and global registers since they |
| 2140 |
// are non-volatile. |
| 2141 |
MOZ_ASSERT(NonVolatileRegs.has(InstanceReg)); |
| 2142 |
#if defined(JS_CODEGEN_X64) || defined(JS_CODEGEN_ARM) || \ |
| 2143 |
defined(JS_CODEGEN_ARM64) || defined(JS_CODEGEN_MIPS64) || \ |
| 2144 |
defined(JS_CODEGEN_LOONG64) || defined(JS_CODEGEN_RISCV64) |
| 2145 |
MOZ_ASSERT(NonVolatileRegs.has(HeapReg)); |
| 2146 |
#endif |
| 2147 |
|
| 2148 |
GenerateExitEpilogue(masm, framePushed, ExitReason::Fixed::ImportInterp, |
| 2149 |
offsets); |
| 2150 |
|
| 2151 |
return FinishOffsets(masm, offsets); |
| 2152 |
} |
| 2153 |
|
| 2154 |
// Generate a stub that is called via the internal ABI derived from the |
| 2155 |
// signature of the import and calls into a compatible JIT function, |
| 2156 |
// having boxed all the ABI arguments into the JIT stack frame layout. |
| 2157 |
static bool GenerateImportJitExit(MacroAssembler& masm, |
| 2158 |
uint32_t funcImportInstanceOffset, |
| 2159 |
const FuncType& funcType, |
| 2160 |
unsigned funcImportIndex, |
| 2161 |
uint32_t fallbackOffset, Label* throwLabel, |
| 2162 |
ImportOffsets* offsets) { |
| 2163 |
AutoCreatedBy acb(masm, "GenerateImportJitExit"); |
| 2164 |
|
| 2165 |
AssertExpectedSP(masm); |
| 2166 |
masm.setFramePushed(0); |
| 2167 |
|
| 2168 |
// JIT calls use the following stack layout: |
| 2169 |
// |
| 2170 |
// | WasmToJSJitFrameLayout | this | arg1..N | saved instance | ... |
| 2171 |
// ^ |
| 2172 |
// +-- sp |
| 2173 |
// |
| 2174 |
// The JIT ABI requires that sp be JitStackAlignment-aligned after pushing |
| 2175 |
// the return address and frame pointer. |
| 2176 |
static_assert(WasmStackAlignment >= JitStackAlignment, "subsumes"); |
| 2177 |
const unsigned sizeOfInstanceSlot = sizeof(void*); |
| 2178 |
const unsigned sizeOfRetAddrAndFP = 2 * sizeof(void*); |
| 2179 |
const unsigned sizeOfPreFrame = |
| 2180 |
WasmToJSJitFrameLayout::Size() - sizeOfRetAddrAndFP; |
| 2181 |
const unsigned sizeOfThisAndArgs = |
| 2182 |
(1 + funcType.args().length()) * sizeof(Value); |
| 2183 |
const unsigned totalJitFrameBytes = sizeOfRetAddrAndFP + sizeOfPreFrame + |
| 2184 |
sizeOfThisAndArgs + sizeOfInstanceSlot; |
| 2185 |
const unsigned jitFramePushed = |
| 2186 |
StackDecrementForCall(JitStackAlignment, |
| 2187 |
sizeof(Frame), // pushed by prologue |
| 2188 |
totalJitFrameBytes) - |
| 2189 |
sizeOfRetAddrAndFP; |
| 2190 |
|
| 2191 |
GenerateJitExitPrologue(masm, jitFramePushed, fallbackOffset, offsets); |
| 2192 |
|
| 2193 |
// 1. Descriptor. |
| 2194 |
unsigned argc = funcType.args().length(); |
| 2195 |
size_t argOffset = 0; |
| 2196 |
uint32_t descriptor = |
| 2197 |
MakeFrameDescriptorForJitCall(FrameType::WasmToJSJit, argc); |
| 2198 |
masm.storePtr(ImmWord(uintptr_t(descriptor)), |
| 2199 |
Address(masm.getStackPointer(), argOffset)); |
| 2200 |
argOffset += sizeof(size_t); |
| 2201 |
|
| 2202 |
// 2. Callee, part 1 -- need the callee register for argument filling, so |
| 2203 |
// record offset here and set up callee later. |
| 2204 |
size_t calleeArgOffset = argOffset; |
| 2205 |
argOffset += sizeof(size_t); |
| 2206 |
MOZ_ASSERT(argOffset == sizeOfPreFrame); |
| 2207 |
|
| 2208 |
// 3. |this| value. |
| 2209 |
masm.storeValue(UndefinedValue(), Address(masm.getStackPointer(), argOffset)); |
| 2210 |
argOffset += sizeof(Value); |
| 2211 |
|
| 2212 |
// 4. Fill the arguments. |
| 2213 |
Register scratch = ABINonArgReturnReg1; // Repeatedly clobbered |
| 2214 |
Register scratch2 = ABINonArgReturnReg0; // Reused as callee below |
| 2215 |
FillArgumentArrayForJitExit(masm, InstanceReg, funcImportIndex, funcType, |
| 2216 |
argOffset, scratch, scratch2, throwLabel); |
| 2217 |
argOffset += funcType.args().length() * sizeof(Value); |
| 2218 |
MOZ_ASSERT(argOffset == sizeOfThisAndArgs + sizeOfPreFrame); |
| 2219 |
|
| 2220 |
// Preserve instance because the JIT callee clobbers it. |
| 2221 |
const size_t savedInstanceOffset = argOffset; |
| 2222 |
masm.storePtr(InstanceReg, |
| 2223 |
Address(masm.getStackPointer(), savedInstanceOffset)); |
| 2224 |
|
| 2225 |
// 2. Callee, part 2 -- now that the register is free, set up the callee. |
| 2226 |
Register callee = ABINonArgReturnReg0; // Live until call |
| 2227 |
|
| 2228 |
// 2.1. Get the callee. This must be a JSFunction if we're using this JIT |
| 2229 |
// exit. |
| 2230 |
masm.loadPtr( |
| 2231 |
Address(InstanceReg, Instance::offsetInData( |
| 2232 |
funcImportInstanceOffset + |
| 2233 |
offsetof(FuncImportInstanceData, callable))), |
| 2234 |
callee); |
| 2235 |
|
| 2236 |
// 2.2. Save callee. |
| 2237 |
masm.storePtr(callee, Address(masm.getStackPointer(), calleeArgOffset)); |
| 2238 |
|
| 2239 |
// 5. Check if we need to rectify arguments. |
| 2240 |
masm.loadFunctionArgCount(callee, scratch); |
| 2241 |
|
| 2242 |
Label rectify; |
| 2243 |
masm.branch32(Assembler::Above, scratch, Imm32(funcType.args().length()), |
| 2244 |
&rectify); |
| 2245 |
|
| 2246 |
// 6. If we haven't rectified arguments, load callee executable entry point. |
| 2247 |
|
| 2248 |
masm.loadJitCodeRaw(callee, callee); |
| 2249 |
|
| 2250 |
Label rejoinBeforeCall; |
| 2251 |
masm.bind(&rejoinBeforeCall); |
| 2252 |
|
| 2253 |
AssertStackAlignment(masm, JitStackAlignment, sizeOfRetAddrAndFP); |
| 2254 |
#ifdef JS_CODEGEN_ARM64 |
| 2255 |
AssertExpectedSP(masm); |
| 2256 |
// Manually resync PSP. Omitting this causes eg tests/wasm/import-export.js |
| 2257 |
// to segfault. |
| 2258 |
masm.moveStackPtrTo(PseudoStackPointer); |
| 2259 |
#endif |
| 2260 |
masm.callJitNoProfiler(callee); |
| 2261 |
|
| 2262 |
// Note that there might be a GC thing in the JSReturnOperand now. |
| 2263 |
// In all the code paths from here: |
| 2264 |
// - either the value is unboxed because it was a primitive and we don't |
| 2265 |
// need to worry about rooting anymore. |
| 2266 |
// - or the value needs to be rooted, but nothing can cause a GC between |
| 2267 |
// here and CoerceInPlace, which roots before coercing to a primitive. |
| 2268 |
|
| 2269 |
// The JIT callee clobbers all registers other than the frame pointer, so |
| 2270 |
// restore InstanceReg here. |
| 2271 |
AssertStackAlignment(masm, JitStackAlignment, sizeOfRetAddrAndFP); |
| 2272 |
masm.loadPtr(Address(masm.getStackPointer(), savedInstanceOffset), |
| 2273 |
InstanceReg); |
| 2274 |
|
| 2275 |
// The frame was aligned for the JIT ABI such that |
| 2276 |
// (sp - 2 * sizeof(void*)) % JitStackAlignment == 0 |
| 2277 |
// But now we possibly want to call one of several different C++ functions, |
| 2278 |
// so subtract 2 * sizeof(void*) so that sp is aligned for an ABI call. |
| 2279 |
static_assert(ABIStackAlignment <= JitStackAlignment, "subsumes"); |
| 2280 |
masm.reserveStack(sizeOfRetAddrAndFP); |
| 2281 |
unsigned nativeFramePushed = masm.framePushed(); |
| 2282 |
AssertStackAlignment(masm, ABIStackAlignment); |
| 2283 |
|
| 2284 |
#ifdef DEBUG |
| 2285 |
{ |
| 2286 |
Label ok; |
| 2287 |
masm.branchTestMagic(Assembler::NotEqual, JSReturnOperand, &ok); |
| 2288 |
masm.breakpoint(); |
| 2289 |
masm.bind(&ok); |
| 2290 |
} |
| 2291 |
#endif |
| 2292 |
|
| 2293 |
GenPrintf(DebugChannel::Import, masm, "wasm-import[%u]; returns ", |
| 2294 |
funcImportIndex); |
| 2295 |
|
| 2296 |
Label oolConvert; |
| 2297 |
const ValTypeVector& results = funcType.results(); |
| 2298 |
if (results.length() == 0) { |
| 2299 |
GenPrintf(DebugChannel::Import, masm, "void"); |
| 2300 |
} else { |
| 2301 |
MOZ_ASSERT(results.length() == 1, "multi-value return unimplemented"); |
| 2302 |
switch (results[0].kind()) { |
| 2303 |
case ValType::I32: |
| 2304 |
// No widening is required, as the return value does not come to us in |
| 2305 |
// ReturnReg. |
| 2306 |
masm.truncateValueToInt32(JSReturnOperand, ReturnDoubleReg, ReturnReg, |
| 2307 |
&oolConvert); |
| 2308 |
GenPrintIsize(DebugChannel::Import, masm, ReturnReg); |
| 2309 |
break; |
| 2310 |
case ValType::I64: |
| 2311 |
// No fastpath for now, go immediately to ool case |
| 2312 |
masm.jump(&oolConvert); |
| 2313 |
break; |
| 2314 |
case ValType::V128: |
| 2315 |
// Unreachable as callImport should not call the stub. |
| 2316 |
masm.breakpoint(); |
| 2317 |
break; |
| 2318 |
case ValType::F32: |
| 2319 |
masm.convertValueToFloat32(JSReturnOperand, ReturnFloat32Reg, |
| 2320 |
&oolConvert); |
| 2321 |
GenPrintF32(DebugChannel::Import, masm, ReturnFloat32Reg); |
| 2322 |
break; |
| 2323 |
case ValType::F64: |
| 2324 |
masm.convertValueToDouble(JSReturnOperand, ReturnDoubleReg, |
| 2325 |
&oolConvert); |
| 2326 |
GenPrintF64(DebugChannel::Import, masm, ReturnDoubleReg); |
| 2327 |
break; |
| 2328 |
case ValType::Ref: |
| 2329 |
// Guarded by temporarilyUnsupportedReftypeForExit() |
| 2330 |
MOZ_RELEASE_ASSERT(results[0].refType().isExtern()); |
| 2331 |
masm.convertValueToWasmAnyRef(JSReturnOperand, ReturnReg, |
| 2332 |
ABINonArgDoubleReg, &oolConvert); |
| 2333 |
GenPrintPtr(DebugChannel::Import, masm, ReturnReg); |
| 2334 |
break; |
| 2335 |
} |
| 2336 |
} |
| 2337 |
|
| 2338 |
GenPrintf(DebugChannel::Import, masm, "\n"); |
| 2339 |
|
| 2340 |
Label done; |
| 2341 |
masm.bind(&done); |
| 2342 |
|
| 2343 |
GenerateJitExitEpilogue(masm, masm.framePushed(), offsets); |
| 2344 |
|
| 2345 |
{ |
| 2346 |
// Call the arguments rectifier. |
| 2347 |
masm.bind(&rectify); |
| 2348 |
masm.loadPtr(Address(InstanceReg, Instance::offsetOfJSJitArgsRectifier()), |
| 2349 |
callee); |
| 2350 |
masm.jump(&rejoinBeforeCall); |
| 2351 |
} |
| 2352 |
|
| 2353 |
if (oolConvert.used()) { |
| 2354 |
masm.bind(&oolConvert); |
| 2355 |
masm.setFramePushed(nativeFramePushed); |
| 2356 |
|
| 2357 |
// Coercion calls use the following stack layout (sp grows to the left): |
| 2358 |
// | args | padding | Value argv[1] | padding | exit Frame | |
| 2359 |
MIRTypeVector coerceArgTypes; |
| 2360 |
MOZ_ALWAYS_TRUE(coerceArgTypes.append(MIRType::Pointer)); |
| 2361 |
unsigned offsetToCoerceArgv = |
| 2362 |
AlignBytes(StackArgBytesForNativeABI(coerceArgTypes), sizeof(Value)); |
| 2363 |
MOZ_ASSERT(nativeFramePushed >= offsetToCoerceArgv + sizeof(Value)); |
| 2364 |
AssertStackAlignment(masm, ABIStackAlignment); |
| 2365 |
|
| 2366 |
// Store return value into argv[0]. |
| 2367 |
masm.storeValue(JSReturnOperand, |
| 2368 |
Address(masm.getStackPointer(), offsetToCoerceArgv)); |
| 2369 |
|
| 2370 |
// From this point, it's safe to reuse the scratch register (which |
| 2371 |
// might be part of the JSReturnOperand). |
| 2372 |
|
| 2373 |
// The JIT might have clobbered exitFP at this point. Since there's |
| 2374 |
// going to be a CoerceInPlace call, pretend we're still doing the JIT |
| 2375 |
// call by restoring our tagged exitFP. |
| 2376 |
SetExitFP(masm, ExitReason::Fixed::ImportJit, scratch); |
| 2377 |
|
| 2378 |
// argument 0: argv |
| 2379 |
ABIArgMIRTypeIter i(coerceArgTypes); |
| 2380 |
Address argv(masm.getStackPointer(), offsetToCoerceArgv); |
| 2381 |
if (i->kind() == ABIArg::GPR) { |
| 2382 |
masm.computeEffectiveAddress(argv, i->gpr()); |
| 2383 |
} else { |
| 2384 |
masm.computeEffectiveAddress(argv, scratch); |
| 2385 |
masm.storePtr(scratch, |
| 2386 |
Address(masm.getStackPointer(), i->offsetFromArgBase())); |
| 2387 |
} |
| 2388 |
i++; |
| 2389 |
MOZ_ASSERT(i.done()); |
| 2390 |
|
| 2391 |
// Call coercion function. Note that right after the call, the value of |
| 2392 |
// FP is correct because FP is non-volatile in the native ABI. |
| 2393 |
AssertStackAlignment(masm, ABIStackAlignment); |
| 2394 |
const ValTypeVector& results = funcType.results(); |
| 2395 |
if (results.length() > 0) { |
| 2396 |
// NOTE that once there can be more than one result and we can box some of |
| 2397 |
// the results (as we must for AnyRef), pointer and already-boxed results |
| 2398 |
// must be rooted while subsequent results are boxed. |
| 2399 |
MOZ_ASSERT(results.length() == 1, "multi-value return unimplemented"); |
| 2400 |
switch (results[0].kind()) { |
| 2401 |
case ValType::I32: |
| 2402 |
masm.call(SymbolicAddress::CoerceInPlace_ToInt32); |
| 2403 |
masm.branchTest32(Assembler::Zero, ReturnReg, ReturnReg, throwLabel); |
| 2404 |
masm.unboxInt32(Address(masm.getStackPointer(), offsetToCoerceArgv), |
| 2405 |
ReturnReg); |
| 2406 |
// No widening is required, as we generate a known-good value in a |
| 2407 |
// safe way here. |
| 2408 |
break; |
| 2409 |
case ValType::I64: { |
| 2410 |
masm.call(SymbolicAddress::CoerceInPlace_ToBigInt); |
| 2411 |
masm.branchTest32(Assembler::Zero, ReturnReg, ReturnReg, throwLabel); |
| 2412 |
Address argv(masm.getStackPointer(), offsetToCoerceArgv); |
| 2413 |
masm.unboxBigInt(argv, scratch); |
| 2414 |
masm.loadBigInt64(scratch, ReturnReg64); |
| 2415 |
break; |
| 2416 |
} |
| 2417 |
case ValType::F64: |
| 2418 |
case ValType::F32: |
| 2419 |
masm.call(SymbolicAddress::CoerceInPlace_ToNumber); |
| 2420 |
masm.branchTest32(Assembler::Zero, ReturnReg, ReturnReg, throwLabel); |
| 2421 |
masm.unboxDouble(Address(masm.getStackPointer(), offsetToCoerceArgv), |
| 2422 |
ReturnDoubleReg); |
| 2423 |
if (results[0].kind() == ValType::F32) { |
| 2424 |
masm.convertDoubleToFloat32(ReturnDoubleReg, ReturnFloat32Reg); |
| 2425 |
} |
| 2426 |
break; |
| 2427 |
case ValType::Ref: |
| 2428 |
// Guarded by temporarilyUnsupportedReftypeForExit() |
| 2429 |
MOZ_RELEASE_ASSERT(results[0].refType().isExtern()); |
| 2430 |
masm.call(SymbolicAddress::BoxValue_Anyref); |
| 2431 |
masm.branchWasmAnyRefIsNull(true, ReturnReg, throwLabel); |
| 2432 |
break; |
| 2433 |
default: |
| 2434 |
MOZ_CRASH("Unsupported convert type"); |
| 2435 |
} |
| 2436 |
} |
| 2437 |
|
| 2438 |
// Maintain the invariant that exitFP is either unset or not set to a |
| 2439 |
// wasm tagged exitFP, per the jit exit contract. |
| 2440 |
ClearExitFP(masm, scratch); |
| 2441 |
|
| 2442 |
masm.jump(&done); |
| 2443 |
masm.setFramePushed(0); |
| 2444 |
} |
| 2445 |
|
| 2446 |
MOZ_ASSERT(masm.framePushed() == 0); |
| 2447 |
|
| 2448 |
return FinishOffsets(masm, offsets); |
| 2449 |
} |
| 2450 |
|
| 2451 |
struct ABIFunctionArgs { |
| 2452 |
ABIFunctionType abiType; |
| 2453 |
size_t len; |
| 2454 |
|
| 2455 |
explicit ABIFunctionArgs(ABIFunctionType sig) |
| 2456 |
: abiType(ABIFunctionType(sig >> ABITypeArgShift)) { |
| 2457 |
len = 0; |
| 2458 |
uint64_t i = uint64_t(abiType); |
| 2459 |
while (i) { |
| 2460 |
i = i >> ABITypeArgShift; |
| 2461 |
len++; |
| 2462 |
} |
| 2463 |
} |
| 2464 |
|
| 2465 |
size_t length() const { return len; } |
| 2466 |
|
| 2467 |
MIRType operator[](size_t i) const { |
| 2468 |
MOZ_ASSERT(i < len); |
| 2469 |
uint64_t abi = uint64_t(abiType); |
| 2470 |
size_t argAtLSB = len - 1; |
| 2471 |
while (argAtLSB != i) { |
| 2472 |
abi = abi >> ABITypeArgShift; |
| 2473 |
argAtLSB--; |
| 2474 |
} |
| 2475 |
return ToMIRType(ABIType(abi & ABITypeArgMask)); |
| 2476 |
} |
| 2477 |
}; |
| 2478 |
|
| 2479 |
bool wasm::GenerateBuiltinThunk(MacroAssembler& masm, ABIFunctionType abiType, |
| 2480 |
ExitReason exitReason, void* funcPtr, |
| 2481 |
CallableOffsets* offsets) { |
| 2482 |
AssertExpectedSP(masm); |
| 2483 |
masm.setFramePushed(0); |
| 2484 |
|
| 2485 |
ABIFunctionArgs args(abiType); |
| 2486 |
uint32_t framePushed = |
| 2487 |
StackDecrementForCall(ABIStackAlignment, |
| 2488 |
sizeof(Frame), // pushed by prologue |
| 2489 |
StackArgBytesForNativeABI(args)); |
| 2490 |
|
| 2491 |
GenerateExitPrologue(masm, framePushed, exitReason, offsets); |
| 2492 |
|
| 2493 |
// Copy out and convert caller arguments, if needed. |
| 2494 |
|
| 2495 |
// This is `sizeof(FrameWithInstances) - ShadowStackSpace` because the latter |
| 2496 |
// is accounted for by the ABIArgIter. |
| 2497 |
unsigned offsetFromFPToCallerStackArgs = |
| 2498 |
sizeof(FrameWithInstances) - jit::ShadowStackSpace; |
| 2499 |
Register scratch = ABINonArgReturnReg0; |
| 2500 |
for (ABIArgIter i(args); !i.done(); i++) { |
| 2501 |
if (i->argInRegister()) { |
| 2502 |
#ifdef JS_CODEGEN_ARM |
| 2503 |
// Non hard-fp passes the args values in GPRs. |
| 2504 |
if (!ARMFlags::UseHardFpABI() && IsFloatingPointType(i.mirType())) { |
| 2505 |
FloatRegister input = i->fpu(); |
| 2506 |
if (i.mirType() == MIRType::Float32) { |
| 2507 |
masm.ma_vxfer(input, Register::FromCode(input.id())); |
| 2508 |
} else if (i.mirType() == MIRType::Double) { |
| 2509 |
uint32_t regId = input.singleOverlay().id(); |
| 2510 |
masm.ma_vxfer(input, Register::FromCode(regId), |
| 2511 |
Register::FromCode(regId + 1)); |
| 2512 |
} |
| 2513 |
} |
| 2514 |
#endif |
| 2515 |
continue; |
| 2516 |
} |
| 2517 |
|
| 2518 |
Address src(FramePointer, |
| 2519 |
offsetFromFPToCallerStackArgs + i->offsetFromArgBase()); |
| 2520 |
Address dst(masm.getStackPointer(), i->offsetFromArgBase()); |
| 2521 |
StackCopy(masm, i.mirType(), scratch, src, dst); |
| 2522 |
} |
| 2523 |
|
| 2524 |
AssertStackAlignment(masm, ABIStackAlignment); |
| 2525 |
MoveSPForJitABI(masm); |
| 2526 |
masm.call(ImmPtr(funcPtr, ImmPtr::NoCheckToken())); |
| 2527 |
|
| 2528 |
#if defined(JS_CODEGEN_X64) |
| 2529 |
// No widening is required, as the caller will widen. |
| 2530 |
#elif defined(JS_CODEGEN_X86) |
| 2531 |
// x86 passes the return value on the x87 FP stack. |
| 2532 |
Operand op(esp, 0); |
| 2533 |
MIRType retType = ToMIRType(ABIType( |
| 2534 |
std::underlying_type_t<ABIFunctionType>(abiType) & ABITypeArgMask)); |
| 2535 |
if (retType == MIRType::Float32) { |
| 2536 |
masm.fstp32(op); |
| 2537 |
masm.loadFloat32(op, ReturnFloat32Reg); |
| 2538 |
} else if (retType == MIRType::Double) { |
| 2539 |
masm.fstp(op); |
| 2540 |
masm.loadDouble(op, ReturnDoubleReg); |
| 2541 |
} |
| 2542 |
#elif defined(JS_CODEGEN_ARM) |
| 2543 |
// Non hard-fp passes the return values in GPRs. |
| 2544 |
MIRType retType = ToMIRType(ABIType( |
| 2545 |
std::underlying_type_t<ABIFunctionType>(abiType) & ABITypeArgMask)); |
| 2546 |
if (!ARMFlags::UseHardFpABI() && IsFloatingPointType(retType)) { |
| 2547 |
masm.ma_vxfer(r0, r1, d0); |
| 2548 |
} |
| 2549 |
#endif |
| 2550 |
|
| 2551 |
GenerateExitEpilogue(masm, framePushed, exitReason, offsets); |
| 2552 |
return FinishOffsets(masm, offsets); |
| 2553 |
} |
| 2554 |
|
| 2555 |
#if defined(JS_CODEGEN_ARM) |
| 2556 |
static const LiveRegisterSet RegsToPreserve( |
| 2557 |
GeneralRegisterSet(Registers::AllMask & |
| 2558 |
~((Registers::SetType(1) << Registers::sp) | |
| 2559 |
(Registers::SetType(1) << Registers::pc))), |
| 2560 |
FloatRegisterSet(FloatRegisters::AllDoubleMask)); |
| 2561 |
# ifdef ENABLE_WASM_SIMD |
| 2562 |
# error "high lanes of SIMD registers need to be saved too." |
| 2563 |
# endif |
| 2564 |
#elif defined(JS_CODEGEN_MIPS64) |
| 2565 |
static const LiveRegisterSet RegsToPreserve( |
| 2566 |
GeneralRegisterSet(Registers::AllMask & |
| 2567 |
~((Registers::SetType(1) << Registers::k0) | |
| 2568 |
(Registers::SetType(1) << Registers::k1) | |
| 2569 |
(Registers::SetType(1) << Registers::sp) | |
| 2570 |
(Registers::SetType(1) << Registers::zero))), |
| 2571 |
FloatRegisterSet(FloatRegisters::AllDoubleMask)); |
| 2572 |
# ifdef ENABLE_WASM_SIMD |
| 2573 |
# error "high lanes of SIMD registers need to be saved too." |
| 2574 |
# endif |
| 2575 |
#elif defined(JS_CODEGEN_LOONG64) |
| 2576 |
static const LiveRegisterSet RegsToPreserve( |
| 2577 |
GeneralRegisterSet(Registers::AllMask & |
| 2578 |
~((uint32_t(1) << Registers::tp) | |
| 2579 |
(uint32_t(1) << Registers::fp) | |
| 2580 |
(uint32_t(1) << Registers::sp) | |
| 2581 |
(uint32_t(1) << Registers::zero))), |
| 2582 |
FloatRegisterSet(FloatRegisters::AllDoubleMask)); |
| 2583 |
# ifdef ENABLE_WASM_SIMD |
| 2584 |
# error "high lanes of SIMD registers need to be saved too." |
| 2585 |
# endif |
| 2586 |
#elif defined(JS_CODEGEN_RISCV64) |
| 2587 |
static const LiveRegisterSet RegsToPreserve( |
| 2588 |
GeneralRegisterSet(Registers::AllMask & |
| 2589 |
~((uint32_t(1) << Registers::tp) | |
| 2590 |
(uint32_t(1) << Registers::fp) | |
| 2591 |
(uint32_t(1) << Registers::sp) | |
| 2592 |
(uint32_t(1) << Registers::zero))), |
| 2593 |
FloatRegisterSet(FloatRegisters::AllDoubleMask)); |
| 2594 |
# ifdef ENABLE_WASM_SIMD |
| 2595 |
# error "high lanes of SIMD registers need to be saved too." |
| 2596 |
# endif |
| 2597 |
#elif defined(JS_CODEGEN_ARM64) |
| 2598 |
// We assume that traps do not happen while lr is live. This both ensures that |
| 2599 |
// the size of RegsToPreserve is a multiple of 2 (preserving WasmStackAlignment) |
| 2600 |
// and gives us a register to clobber in the return path. |
| 2601 |
static const LiveRegisterSet RegsToPreserve( |
| 2602 |
GeneralRegisterSet(Registers::AllMask & |
| 2603 |
~((Registers::SetType(1) << RealStackPointer.code()) | |
| 2604 |
(Registers::SetType(1) << Registers::lr))), |
| 2605 |
# ifdef ENABLE_WASM_SIMD |
| 2606 |
FloatRegisterSet(FloatRegisters::AllSimd128Mask)); |
| 2607 |
# else |
| 2608 |
// If SIMD is not enabled, it's pointless to save/restore the upper 64 |
| 2609 |
// bits of each vector register. |
| 2610 |
FloatRegisterSet(FloatRegisters::AllDoubleMask)); |
| 2611 |
# endif |
| 2612 |
#elif defined(JS_CODEGEN_X86) || defined(JS_CODEGEN_X64) |
| 2613 |
// It's correct to use FloatRegisters::AllMask even when SIMD is not enabled; |
| 2614 |
// PushRegsInMask strips out the high lanes of the XMM registers in this case, |
| 2615 |
// while the singles will be stripped as they are aliased by the larger doubles. |
| 2616 |
static const LiveRegisterSet RegsToPreserve( |
| 2617 |
GeneralRegisterSet(Registers::AllMask & |
| 2618 |
~(Registers::SetType(1) << Registers::StackPointer)), |
| 2619 |
FloatRegisterSet(FloatRegisters::AllMask)); |
| 2620 |
#else |
| 2621 |
static const LiveRegisterSet RegsToPreserve( |
| 2622 |
GeneralRegisterSet(0), FloatRegisterSet(FloatRegisters::AllDoubleMask)); |
| 2623 |
# ifdef ENABLE_WASM_SIMD |
| 2624 |
# error "no SIMD support" |
| 2625 |
# endif |
| 2626 |
#endif |
| 2627 |
|
| 2628 |
// Generate a RegisterOffsets which describes the locations of the GPRs as saved |
| 2629 |
// by GenerateTrapExit. FP registers are ignored. Note that the values |
| 2630 |
// stored in the RegisterOffsets are offsets in words downwards from the top of |
| 2631 |
// the save area. That is, a higher value implies a lower address. |
| 2632 |
void wasm::GenerateTrapExitRegisterOffsets(RegisterOffsets* offsets, |
| 2633 |
size_t* numWords) { |
| 2634 |
// This is the number of words pushed by the initial WasmPush(). |
| 2635 |
*numWords = WasmPushSize / sizeof(void*); |
| 2636 |
MOZ_ASSERT(*numWords == TrapExitDummyValueOffsetFromTop + 1); |
| 2637 |
|
| 2638 |
// And these correspond to the PushRegsInMask() that immediately follows. |
| 2639 |
for (GeneralRegisterBackwardIterator iter(RegsToPreserve.gprs()); iter.more(); |
| 2640 |
++iter) { |
| 2641 |
offsets->setOffset(*iter, *numWords); |
| 2642 |
(*numWords)++; |
| 2643 |
} |
| 2644 |
} |
| 2645 |
|
| 2646 |
// Generate a stub which calls WasmReportTrap() and can be executed by having |
| 2647 |
// the signal handler redirect PC from any trapping instruction. |
| 2648 |
static bool GenerateTrapExit(MacroAssembler& masm, Label* throwLabel, |
| 2649 |
Offsets* offsets) { |
| 2650 |
AssertExpectedSP(masm); |
| 2651 |
masm.haltingAlign(CodeAlignment); |
| 2652 |
|
| 2653 |
masm.setFramePushed(0); |
| 2654 |
|
| 2655 |
offsets->begin = masm.currentOffset(); |
| 2656 |
|
| 2657 |
// Traps can only happen at well-defined program points. However, since |
| 2658 |
// traps may resume and the optimal assumption for the surrounding code is |
| 2659 |
// that registers are not clobbered, we need to preserve all registers in |
| 2660 |
// the trap exit. One simplifying assumption is that flags may be clobbered. |
| 2661 |
// Push a dummy word to use as return address below. |
| 2662 |
WasmPush(masm, ImmWord(TrapExitDummyValue)); |
| 2663 |
unsigned framePushedBeforePreserve = masm.framePushed(); |
| 2664 |
masm.PushRegsInMask(RegsToPreserve); |
| 2665 |
unsigned offsetOfReturnWord = masm.framePushed() - framePushedBeforePreserve; |
| 2666 |
|
| 2667 |
// We know that StackPointer is word-aligned, but not necessarily |
| 2668 |
// stack-aligned, so we need to align it dynamically. |
| 2669 |
Register preAlignStackPointer = ABINonVolatileReg; |
| 2670 |
masm.moveStackPtrTo(preAlignStackPointer); |
| 2671 |
masm.andToStackPtr(Imm32(~(ABIStackAlignment - 1))); |
| 2672 |
if (ShadowStackSpace) { |
| 2673 |
masm.subFromStackPtr(Imm32(ShadowStackSpace)); |
| 2674 |
} |
| 2675 |
|
| 2676 |
masm.assertStackAlignment(ABIStackAlignment); |
| 2677 |
masm.call(SymbolicAddress::HandleTrap); |
| 2678 |
|
| 2679 |
// WasmHandleTrap returns null if control should transfer to the throw stub. |
| 2680 |
masm.branchTestPtr(Assembler::Zero, ReturnReg, ReturnReg, throwLabel); |
| 2681 |
|
| 2682 |
// Otherwise, the return value is the TrapData::resumePC we must jump to. |
| 2683 |
// We must restore register state before jumping, which will clobber |
| 2684 |
// ReturnReg, so store ReturnReg in the above-reserved stack slot which we |
| 2685 |
// use to jump to via ret. |
| 2686 |
masm.moveToStackPtr(preAlignStackPointer); |
| 2687 |
masm.storePtr(ReturnReg, Address(masm.getStackPointer(), offsetOfReturnWord)); |
| 2688 |
masm.PopRegsInMask(RegsToPreserve); |
| 2689 |
#ifdef JS_CODEGEN_ARM64 |
| 2690 |
WasmPop(masm, lr); |
| 2691 |
masm.abiret(); |
| 2692 |
#else |
| 2693 |
masm.ret(); |
| 2694 |
#endif |
| 2695 |
|
| 2696 |
return FinishOffsets(masm, offsets); |
| 2697 |
} |
| 2698 |
|
| 2699 |
static void ClobberWasmRegsForLongJmp(MacroAssembler& masm, Register jumpReg) { |
| 2700 |
// Get the set of all registers that are allocatable in wasm functions |
| 2701 |
AllocatableGeneralRegisterSet gprs(GeneralRegisterSet::All()); |
| 2702 |
RegisterAllocator::takeWasmRegisters(gprs); |
| 2703 |
// Remove the instance register from this set as landing pads require it to be |
| 2704 |
// valid |
| 2705 |
gprs.take(InstanceReg); |
| 2706 |
// Remove a specified register that will be used for the longjmp |
| 2707 |
gprs.take(jumpReg); |
| 2708 |
// Set all of these registers to zero |
| 2709 |
for (GeneralRegisterIterator iter(gprs.asLiveSet()); iter.more(); ++iter) { |
| 2710 |
Register reg = *iter; |
| 2711 |
masm.xorPtr(reg, reg); |
| 2712 |
} |
| 2713 |
|
| 2714 |
// Get the set of all floating point registers that are allocatable in wasm |
| 2715 |
// functions |
| 2716 |
AllocatableFloatRegisterSet fprs(FloatRegisterSet::All()); |
| 2717 |
// Set all of these registers to NaN. We attempt for this to be a signalling |
| 2718 |
// NaN, but the bit format for signalling NaNs are implementation defined |
| 2719 |
// and so this is just best effort. |
| 2720 |
Maybe<FloatRegister> regNaN; |
| 2721 |
for (FloatRegisterIterator iter(fprs.asLiveSet()); iter.more(); ++iter) { |
| 2722 |
FloatRegister reg = *iter; |
| 2723 |
if (!reg.isDouble()) { |
| 2724 |
continue; |
| 2725 |
} |
| 2726 |
if (regNaN) { |
| 2727 |
masm.moveDouble(*regNaN, reg); |
| 2728 |
continue; |
| 2729 |
} |
| 2730 |
masm.loadConstantDouble(std::numeric_limits<double>::signaling_NaN(), reg); |
| 2731 |
regNaN = Some(reg); |
| 2732 |
} |
| 2733 |
} |
| 2734 |
|
| 2735 |
// Generates code to jump to a Wasm catch handler after unwinding the stack. |
| 2736 |
// The |rfe| register stores a pointer to the ResumeFromException struct |
| 2737 |
// allocated on the stack. |
| 2738 |
void wasm::GenerateJumpToCatchHandler(MacroAssembler& masm, Register rfe, |
| 2739 |
Register scratch1, Register scratch2) { |
| 2740 |
masm.loadPtr(Address(rfe, ResumeFromException::offsetOfInstance()), |
| 2741 |
InstanceReg); |
| 2742 |
masm.loadWasmPinnedRegsFromInstance(mozilla::Nothing()); |
| 2743 |
masm.switchToWasmInstanceRealm(scratch1, scratch2); |
| 2744 |
masm.loadPtr(Address(rfe, ResumeFromException::offsetOfTarget()), scratch1); |
| 2745 |
masm.loadPtr(Address(rfe, ResumeFromException::offsetOfFramePointer()), |
| 2746 |
FramePointer); |
| 2747 |
masm.loadStackPtr(Address(rfe, ResumeFromException::offsetOfStackPointer())); |
| 2748 |
MoveSPForJitABI(masm); |
| 2749 |
ClobberWasmRegsForLongJmp(masm, scratch1); |
| 2750 |
masm.jump(scratch1); |
| 2751 |
} |
| 2752 |
|
| 2753 |
// Generate a stub that calls the C++ exception handler. |
| 2754 |
static bool GenerateThrowStub(MacroAssembler& masm, Label* throwLabel, |
| 2755 |
Offsets* offsets) { |
| 2756 |
Register scratch1 = ABINonArgReturnReg0; |
| 2757 |
|
| 2758 |
AssertExpectedSP(masm); |
| 2759 |
masm.haltingAlign(CodeAlignment); |
| 2760 |
masm.setFramePushed(0); |
| 2761 |
|
| 2762 |
masm.bind(throwLabel); |
| 2763 |
|
| 2764 |
offsets->begin = masm.currentOffset(); |
| 2765 |
|
| 2766 |
// Conservatively, the stack pointer can be unaligned and we must align it |
| 2767 |
// dynamically. |
| 2768 |
masm.andToStackPtr(Imm32(~(ABIStackAlignment - 1))); |
| 2769 |
if (ShadowStackSpace) { |
| 2770 |
masm.subFromStackPtr(Imm32(ShadowStackSpace)); |
| 2771 |
} |
| 2772 |
|
| 2773 |
// Allocate space for exception or regular resume information. |
| 2774 |
masm.reserveStack(sizeof(jit::ResumeFromException)); |
| 2775 |
masm.moveStackPtrTo(scratch1); |
| 2776 |
|
| 2777 |
MIRTypeVector handleThrowTypes; |
| 2778 |
MOZ_ALWAYS_TRUE(handleThrowTypes.append(MIRType::Pointer)); |
| 2779 |
|
| 2780 |
unsigned frameSize = |
| 2781 |
StackDecrementForCall(ABIStackAlignment, masm.framePushed(), |
| 2782 |
StackArgBytesForNativeABI(handleThrowTypes)); |
| 2783 |
masm.reserveStack(frameSize); |
| 2784 |
masm.assertStackAlignment(ABIStackAlignment); |
| 2785 |
|
| 2786 |
ABIArgMIRTypeIter i(handleThrowTypes); |
| 2787 |
if (i->kind() == ABIArg::GPR) { |
| 2788 |
masm.movePtr(scratch1, i->gpr()); |
| 2789 |
} else { |
| 2790 |
masm.storePtr(scratch1, |
| 2791 |
Address(masm.getStackPointer(), i->offsetFromArgBase())); |
| 2792 |
} |
| 2793 |
i++; |
| 2794 |
MOZ_ASSERT(i.done()); |
| 2795 |
|
| 2796 |
// WasmHandleThrow unwinds JitActivation::wasmExitFP() and initializes the |
| 2797 |
// ResumeFromException struct we allocated on the stack. |
| 2798 |
// |
| 2799 |
// It returns the address of the JIT's exception handler trampoline that we |
| 2800 |
// should jump to. This trampoline will return to the interpreter entry or |
| 2801 |
// jump to a catch handler. |
| 2802 |
masm.call(SymbolicAddress::HandleThrow); |
| 2803 |
|
| 2804 |
// Ensure the ResumeFromException struct is on top of the stack. |
| 2805 |
masm.freeStack(frameSize); |
| 2806 |
|
| 2807 |
// Jump to the "return value check" code of the JIT's exception handler |
| 2808 |
// trampoline. On ARM64 ensure PSP matches SP. |
| 2809 |
#ifdef JS_CODEGEN_ARM64 |
| 2810 |
masm.Mov(PseudoStackPointer64, sp); |
| 2811 |
#endif |
| 2812 |
masm.jump(ReturnReg); |
| 2813 |
|
| 2814 |
return FinishOffsets(masm, offsets); |
| 2815 |
} |
| 2816 |
|
| 2817 |
static const LiveRegisterSet AllAllocatableRegs = |
| 2818 |
LiveRegisterSet(GeneralRegisterSet(Registers::AllocatableMask), |
| 2819 |
FloatRegisterSet(FloatRegisters::AllMask)); |
| 2820 |
|
| 2821 |
// Generate a stub that handles toggleable enter/leave frame traps or |
| 2822 |
// breakpoints. The stub records the frame pointer (via GenerateExitPrologue) |
| 2823 |
// and saves most of registers, so as to not affect the code generated by |
| 2824 |
// WasmBaselineCompile. |
| 2825 |
static bool GenerateDebugStub(MacroAssembler& masm, Label* throwLabel, |
| 2826 |
CallableOffsets* offsets) { |
| 2827 |
AssertExpectedSP(masm); |
| 2828 |
masm.haltingAlign(CodeAlignment); |
| 2829 |
masm.setFramePushed(0); |
| 2830 |
|
| 2831 |
GenerateExitPrologue(masm, 0, ExitReason::Fixed::DebugStub, offsets); |
| 2832 |
|
| 2833 |
// Save all registers used between baseline compiler operations. |
| 2834 |
masm.PushRegsInMask(AllAllocatableRegs); |
| 2835 |
|
| 2836 |
uint32_t framePushed = masm.framePushed(); |
| 2837 |
|
| 2838 |
// This method might be called with unaligned stack -- aligning and |
| 2839 |
// saving old stack pointer at the top. |
| 2840 |
#ifdef JS_CODEGEN_ARM64 |
| 2841 |
// On ARM64 however the stack is always aligned. |
| 2842 |
static_assert(ABIStackAlignment == 16, "ARM64 SP alignment"); |
| 2843 |
#else |
| 2844 |
Register scratch = ABINonArgReturnReg0; |
| 2845 |
masm.moveStackPtrTo(scratch); |
| 2846 |
masm.subFromStackPtr(Imm32(sizeof(intptr_t))); |
| 2847 |
masm.andToStackPtr(Imm32(~(ABIStackAlignment - 1))); |
| 2848 |
masm.storePtr(scratch, Address(masm.getStackPointer(), 0)); |
| 2849 |
#endif |
| 2850 |
|
| 2851 |
if (ShadowStackSpace) { |
| 2852 |
masm.subFromStackPtr(Imm32(ShadowStackSpace)); |
| 2853 |
} |
| 2854 |
masm.assertStackAlignment(ABIStackAlignment); |
| 2855 |
masm.call(SymbolicAddress::HandleDebugTrap); |
| 2856 |
|
| 2857 |
masm.branchIfFalseBool(ReturnReg, throwLabel); |
| 2858 |
|
| 2859 |
if (ShadowStackSpace) { |
| 2860 |
masm.addToStackPtr(Imm32(ShadowStackSpace)); |
| 2861 |
} |
| 2862 |
#ifndef JS_CODEGEN_ARM64 |
| 2863 |
masm.Pop(scratch); |
| 2864 |
masm.moveToStackPtr(scratch); |
| 2865 |
#endif |
| 2866 |
|
| 2867 |
MOZ_ASSERT(NonVolatileRegs.has(InstanceReg)); |
| 2868 |
masm.loadWasmPinnedRegsFromInstance(mozilla::Nothing()); |
| 2869 |
|
| 2870 |
masm.setFramePushed(framePushed); |
| 2871 |
masm.PopRegsInMask(AllAllocatableRegs); |
| 2872 |
|
| 2873 |
GenerateExitEpilogue(masm, 0, ExitReason::Fixed::DebugStub, offsets); |
| 2874 |
|
| 2875 |
return FinishOffsets(masm, offsets); |
| 2876 |
} |
| 2877 |
|
| 2878 |
static bool GenerateRequestTierUpStub(MacroAssembler& masm, |
| 2879 |
CallableOffsets* offsets) { |
| 2880 |
// This is similar to GenerateDebugStub. As with that routine, all registers |
| 2881 |
// are saved, we call out to a C++ helper, then restore the registers. The |
| 2882 |
// helper can't fail, though. |
| 2883 |
// |
| 2884 |
// On entry to (the code generated by) this routine, we expect the requesting |
| 2885 |
// instance pointer to be in InstanceReg, regardless of the platform. |
| 2886 |
|
| 2887 |
AutoCreatedBy acb(masm, "GenerateRequestTierUpStub"); |
| 2888 |
AssertExpectedSP(masm); |
| 2889 |
masm.haltingAlign(CodeAlignment); |
| 2890 |
masm.setFramePushed(0); |
| 2891 |
|
| 2892 |
GenerateExitPrologue(masm, 0, ExitReason::Fixed::RequestTierUp, offsets); |
| 2893 |
|
| 2894 |
// Save all registers used between baseline compiler operations. |
| 2895 |
masm.PushRegsInMask(AllAllocatableRegs); |
| 2896 |
|
| 2897 |
uint32_t framePushed = masm.framePushed(); |
| 2898 |
|
| 2899 |
// This method might be called with unaligned stack -- aligning and |
| 2900 |
// saving old stack pointer at the top. |
| 2901 |
#ifdef JS_CODEGEN_ARM64 |
| 2902 |
// On ARM64 however the stack is always aligned. |
| 2903 |
static_assert(ABIStackAlignment == 16, "ARM64 SP alignment"); |
| 2904 |
#else |
| 2905 |
Register scratch = ABINonArgReturnReg0; |
| 2906 |
masm.moveStackPtrTo(scratch); |
| 2907 |
masm.subFromStackPtr(Imm32(sizeof(intptr_t))); |
| 2908 |
masm.andToStackPtr(Imm32(~(ABIStackAlignment - 1))); |
| 2909 |
masm.storePtr(scratch, Address(masm.getStackPointer(), 0)); |
| 2910 |
#endif |
| 2911 |
|
| 2912 |
if (ShadowStackSpace > 0) { |
| 2913 |
masm.subFromStackPtr(Imm32(ShadowStackSpace)); |
| 2914 |
} |
| 2915 |
masm.assertStackAlignment(ABIStackAlignment); |
| 2916 |
|
| 2917 |
// Pass InstanceReg as the first (and only) arg to the C++ routine. We |
| 2918 |
// expect that the only target to pass the first integer arg in memory is |
| 2919 |
// x86_32, and handle that specially. |
| 2920 |
ABIArgGenerator abi; |
| 2921 |
ABIArg arg = abi.next(MIRType::Pointer); |
| 2922 |
#ifndef JS_CODEGEN_X86 |
| 2923 |
// The arg rides in a reg. |
| 2924 |
MOZ_RELEASE_ASSERT(arg.kind() == ABIArg::GPR); |
| 2925 |
masm.movePtr(InstanceReg, arg.gpr()); |
| 2926 |
#else |
| 2927 |
// Ensure we don't need to consider ShadowStackSpace. |
| 2928 |
static_assert(ShadowStackSpace == 0); |
| 2929 |
// Ensure the ABIArgGenerator is consistent with the code generation |
| 2930 |
// assumptions we make here. |
| 2931 |
MOZ_RELEASE_ASSERT(arg.kind() == ABIArg::Stack && |
| 2932 |
arg.offsetFromArgBase() == 0); |
| 2933 |
// Get the arg on the stack without messing up the stack alignment. |
| 2934 |
masm.subFromStackPtr(Imm32(12)); |
| 2935 |
masm.push(InstanceReg); |
| 2936 |
#endif |
| 2937 |
|
| 2938 |
masm.call(SymbolicAddress::HandleRequestTierUp); |
| 2939 |
// The call can't fail (meaning, if it does fail, we ignore that) |
| 2940 |
|
| 2941 |
#ifdef JS_CODEGEN_X86 |
| 2942 |
// Remove the arg and padding we just pushed. |
| 2943 |
masm.addToStackPtr(Imm32(16)); |
| 2944 |
#endif |
| 2945 |
|
| 2946 |
if (ShadowStackSpace > 0) { |
| 2947 |
masm.addToStackPtr(Imm32(ShadowStackSpace)); |
| 2948 |
} |
| 2949 |
#ifndef JS_CODEGEN_ARM64 |
| 2950 |
masm.Pop(scratch); |
| 2951 |
masm.moveToStackPtr(scratch); |
| 2952 |
#endif |
| 2953 |
|
| 2954 |
masm.setFramePushed(framePushed); |
| 2955 |
masm.PopRegsInMask(AllAllocatableRegs); |
| 2956 |
|
| 2957 |
GenerateExitEpilogue(masm, 0, ExitReason::Fixed::RequestTierUp, offsets); |
| 2958 |
|
| 2959 |
return FinishOffsets(masm, offsets); |
| 2960 |
} |
| 2961 |
|
| 2962 |
static bool GenerateUpdateCallRefMetricsStub(MacroAssembler& masm, |
| 2963 |
CallableOffsets* offsets) { |
| 2964 |
// This is a stub which is entirely self-contained -- it calls no other |
| 2965 |
// functions, cannot fail, and creates a minimal stack frame. It can only |
| 2966 |
// use three registers, `regMetrics`, `regFuncRef` and `regScratch`, as set |
| 2967 |
// up below, and as described in BaseCompiler::updateCallRefMetrics. All |
| 2968 |
// other registers must remain unchanged. Also, we may read InstanceReg. |
| 2969 |
// |
| 2970 |
// `regMetrics` (the CallRefMetrics*) should satisfy |
| 2971 |
// CallRefMetrics::invariantsOK() both on entry to and exit from the code |
| 2972 |
// generated here. |
| 2973 |
|
| 2974 |
// `regMetrics` and `regFuncRef` are live at entry, but not `regScratch`. |
| 2975 |
const Register regMetrics = WasmCallRefCallScratchReg0; // CallRefMetrics* |
| 2976 |
const Register regFuncRef = WasmCallRefCallScratchReg1; // FuncExtended* |
| 2977 |
const Register regScratch = WasmCallRefCallScratchReg2; // scratch |
| 2978 |
|
| 2979 |
// At entry to the stub, `regMetrics` points at the CallRefMetrics, |
| 2980 |
// `regFuncRef` points at the FunctionExtended, `regScratch` is available as |
| 2981 |
// scratch, `regFuncRef` is known to be non-null, and, if the target0/count0 |
| 2982 |
// slot is in use, it is known not to match that slot. The call may or may |
| 2983 |
// not be cross-instance. |
| 2984 |
|
| 2985 |
// Briefly, what we generate here is: |
| 2986 |
// |
| 2987 |
// assert(regFuncRef is non-null) |
| 2988 |
// |
| 2989 |
// if (regFuncRef is a cross instance call) { |
| 2990 |
// regMetrics->countOther++; |
| 2991 |
// return; |
| 2992 |
// } |
| 2993 |
// |
| 2994 |
// assert(regFuncRef != regMetrics->targets[0]); |
| 2995 |
// |
| 2996 |
// for (i = 1; i < NUM_SLOTS; i++) { |
| 2997 |
// if (regFuncRef == regMetrics->targets[i]) { |
| 2998 |
// regMetrics->counts[i]++; |
| 2999 |
// if (regMetrics->counts[i-1] <u regMetrics->counts[i]) { |
| 3000 |
// // swap regMetrics->counts[i-1]/[i] and |
| 3001 |
// regMetrics->targets[i-1]/[i] |
| 3002 |
// } |
| 3003 |
// return; |
| 3004 |
// } |
| 3005 |
// } |
| 3006 |
// |
| 3007 |
// for (i = 0; i < NUM_SLOTS; i++) { |
| 3008 |
// if (regMetrics->targets[i] is nullptr) { |
| 3009 |
// regMetrics->targets[i] = regFuncRef; |
| 3010 |
// regMetrics->counts[i] = 1; |
| 3011 |
// return; |
| 3012 |
// } |
| 3013 |
// } |
| 3014 |
// |
| 3015 |
// regMetrics->countsOther++; |
| 3016 |
// return; |
| 3017 |
// |
| 3018 |
// And the loops are unrolled. |
| 3019 |
|
| 3020 |
// Frame setup and unwinding: we generate the absolute minimal frame setup |
| 3021 |
// (`push FP; FP := SP` / `pop FP; ret`). There is no register save/restore |
| 3022 |
// in the frame. The routine created here is a leaf and will neither trap |
| 3023 |
// nor invoke GC, so the unwindability requirements are minimal -- only the |
| 3024 |
// profiler will need to be able to unwind through it. |
| 3025 |
|
| 3026 |
// See declaration of CallRefMetrics for comments about assignments of |
| 3027 |
// funcrefs to `CallRefMetrics::targets[]` fields. |
| 3028 |
|
| 3029 |
AutoCreatedBy acb(masm, "GenerateUpdateCallRefMetricsStub"); |
| 3030 |
Label ret; |
| 3031 |
|
| 3032 |
AssertExpectedSP(masm); |
| 3033 |
masm.haltingAlign(CodeAlignment); |
| 3034 |
masm.setFramePushed(0); |
| 3035 |
|
| 3036 |
GenerateMinimalPrologue(masm, &offsets->begin); |
| 3037 |
|
| 3038 |
#ifdef DEBUG |
| 3039 |
// Assertion: we know the target is non-null at entry, because the in-line |
| 3040 |
// code created by BaseCompiler::callRef handles that case. |
| 3041 |
// if (regFuncRef == nullptr) { |
| 3042 |
// crash; |
| 3043 |
// } |
| 3044 |
Label after1; |
| 3045 |
masm.branchWasmAnyRefIsNull(/*isNull=*/false, regFuncRef, &after1); |
| 3046 |
|
| 3047 |
masm.breakpoint(); |
| 3048 |
|
| 3049 |
masm.bind(&after1); |
| 3050 |
#endif |
| 3051 |
|
| 3052 |
// If it is a cross-instance call, add it to the `countOther` bin. |
| 3053 |
// regScratch = regFuncRef->instance; |
| 3054 |
// if (regScratch != thisInstance) { |
| 3055 |
// regScratch = regMetrics->countOther; |
| 3056 |
// regScratch++; |
| 3057 |
// regMetrics->countOther = regScratch; |
| 3058 |
// return; |
| 3059 |
// } |
| 3060 |
Label after2; |
| 3061 |
const size_t offsetOfInstanceSlot = FunctionExtended::offsetOfExtendedSlot( |
| 3062 |
FunctionExtended::WASM_INSTANCE_SLOT); |
| 3063 |
masm.loadPtr(Address(regFuncRef, offsetOfInstanceSlot), regScratch); |
| 3064 |
masm.branchPtr(Assembler::Equal, InstanceReg, regScratch, &after2); |
| 3065 |
// |
| 3066 |
const size_t offsetOfCountOther = CallRefMetrics::offsetOfCountOther(); |
| 3067 |
masm.load32(Address(regMetrics, offsetOfCountOther), regScratch); |
| 3068 |
masm.add32(Imm32(1), regScratch); |
| 3069 |
masm.store32(regScratch, Address(regMetrics, offsetOfCountOther)); |
| 3070 |
masm.jump(&ret); |
| 3071 |
// |
| 3072 |
masm.bind(&after2); |
| 3073 |
|
| 3074 |
#ifdef DEBUG |
| 3075 |
// Assertion: we know it can't be a hit at slot zero, because the inline code |
| 3076 |
// also handles that case. |
| 3077 |
// regScratch = regMetrics->targets[0]; |
| 3078 |
// if (regScratch == regFuncRef) { |
| 3079 |
// crash; |
| 3080 |
// } |
| 3081 |
Label after3; |
| 3082 |
const size_t offsetOfTarget0 = CallRefMetrics::offsetOfTarget(0); |
| 3083 |
masm.loadPtr(Address(regMetrics, offsetOfTarget0), regScratch); |
| 3084 |
masm.branchPtr(Assembler::NotEqual, regScratch, regFuncRef, &after3); |
| 3085 |
|
| 3086 |
masm.breakpoint(); |
| 3087 |
|
| 3088 |
masm.bind(&after3); |
| 3089 |
#endif |
| 3090 |
|
| 3091 |
// If it matches slot one, increment count, swap with slot zero if needed |
| 3092 |
// regScratch = regMetrics->targets[1]; |
| 3093 |
// if (regFuncRef == regScratch) { |
| 3094 |
// // We need a second temp register (regScratch being the first). |
| 3095 |
// // We no longer need regFuncRef so use that as the second temp. |
| 3096 |
// regScratch = regMetrics->counts[0]; |
| 3097 |
// regFuncRef = regMetrics->counts[1]; |
| 3098 |
// regFuncRef++; |
| 3099 |
// regMetrics->counts[1] = regFuncRef; |
| 3100 |
// if (regScratch <u regFuncRef) { |
| 3101 |
// // regScratch and regFuncRef |
| 3102 |
// // are regMetrics->counts[0] and [1] respectively |
| 3103 |
// regMetrics->counts[0] = regFuncRef; |
| 3104 |
// regMetrics->counts[1] = regScratch; |
| 3105 |
// regScratch = regMetrics->targets[0]; |
| 3106 |
// regFuncRef = regMetrics->targets[1]; |
| 3107 |
// regMetrics->targets[0] = regFuncRef; |
| 3108 |
// regMetrics->targets[1] = regScratch; |
| 3109 |
// } |
| 3110 |
// return; |
| 3111 |
// } |
| 3112 |
// and the same for slots 2, 3, 4, etc |
| 3113 |
for (size_t i = 1; i < CallRefMetrics::NUM_SLOTS; i++) { |
| 3114 |
Label after4; |
| 3115 |
masm.loadPtr(Address(regMetrics, CallRefMetrics::offsetOfTarget(i)), |
| 3116 |
regScratch); |
| 3117 |
masm.branchPtr(Assembler::NotEqual, regFuncRef, regScratch, &after4); |
| 3118 |
|
| 3119 |
masm.load32(Address(regMetrics, CallRefMetrics::offsetOfCount(i - 1)), |
| 3120 |
regScratch); |
| 3121 |
masm.load32(Address(regMetrics, CallRefMetrics::offsetOfCount(i)), |
| 3122 |
regFuncRef); |
| 3123 |
masm.add32(Imm32(1), regFuncRef); |
| 3124 |
masm.store32(regFuncRef, |
| 3125 |
Address(regMetrics, CallRefMetrics::offsetOfCount(i))); |
| 3126 |
masm.branch32(Assembler::AboveOrEqual, regScratch, regFuncRef, &ret); |
| 3127 |
|
| 3128 |
masm.store32(regFuncRef, |
| 3129 |
Address(regMetrics, CallRefMetrics::offsetOfCount(i - 1))); |
| 3130 |
masm.store32(regScratch, |
| 3131 |
Address(regMetrics, CallRefMetrics::offsetOfCount(i))); |
| 3132 |
masm.loadPtr(Address(regMetrics, CallRefMetrics::offsetOfTarget(i - 1)), |
| 3133 |
regScratch); |
| 3134 |
masm.loadPtr(Address(regMetrics, CallRefMetrics::offsetOfTarget(i)), |
| 3135 |
regFuncRef); |
| 3136 |
masm.storePtr(regFuncRef, |
| 3137 |
Address(regMetrics, CallRefMetrics::offsetOfTarget(i - 1))); |
| 3138 |
masm.storePtr(regScratch, |
| 3139 |
Address(regMetrics, CallRefMetrics::offsetOfTarget(i))); |
| 3140 |
masm.jump(&ret); |
| 3141 |
|
| 3142 |
masm.bind(&after4); |
| 3143 |
} |
| 3144 |
|
| 3145 |
// Not found. Use the first unused slot, if available. This assumes that T |
| 3146 |
// is non-null; but that is assured us on entry (and asserted above). See |
| 3147 |
// CallRefMetrics::invariantsOK. |
| 3148 |
// if (regMetrics->targets[0] == nullptr) { |
| 3149 |
// regMetrics->targets[0] = regFuncRef; |
| 3150 |
// regMetrics->counts[0] = 1; |
| 3151 |
// return; |
| 3152 |
// } |
| 3153 |
// and the same for slots 1, 2, 3, 4, etc |
| 3154 |
for (size_t i = 0; i < CallRefMetrics::NUM_SLOTS; i++) { |
| 3155 |
Label after5; |
| 3156 |
masm.loadPtr(Address(regMetrics, CallRefMetrics::offsetOfTarget(i)), |
| 3157 |
regScratch); |
| 3158 |
masm.branchWasmAnyRefIsNull(/*isNull=*/false, regScratch, &after5); |
| 3159 |
|
| 3160 |
masm.storePtr(regFuncRef, |
| 3161 |
Address(regMetrics, CallRefMetrics::offsetOfTarget(i))); |
| 3162 |
masm.store32(Imm32(1), |
| 3163 |
Address(regMetrics, CallRefMetrics::offsetOfCount(i))); |
| 3164 |
masm.jump(&ret); |
| 3165 |
|
| 3166 |
masm.bind(&after5); |
| 3167 |
} |
| 3168 |
|
| 3169 |
// Not found, and we don't have a slot with which to track this new target |
| 3170 |
// individually. Instead just increment the "all others" bin. |
| 3171 |
// regScratch = regMetrics->countOther; |
| 3172 |
// regScratch++; |
| 3173 |
// regMetrics->countOther = regScratch; |
| 3174 |
// return; |
| 3175 |
masm.load32(Address(regMetrics, CallRefMetrics::offsetOfCountOther()), |
| 3176 |
regScratch); |
| 3177 |
masm.add32(Imm32(1), regScratch); |
| 3178 |
masm.store32(regScratch, |
| 3179 |
Address(regMetrics, CallRefMetrics::offsetOfCountOther())); |
| 3180 |
|
| 3181 |
masm.bind(&ret); |
| 3182 |
|
| 3183 |
MOZ_ASSERT(masm.framePushed() == 0); |
| 3184 |
GenerateMinimalEpilogue(masm, &offsets->ret); |
| 3185 |
|
| 3186 |
return FinishOffsets(masm, offsets); |
| 3187 |
} |
| 3188 |
|
| 3189 |
bool wasm::GenerateEntryStubs(const CodeMetadata& codeMeta, |
| 3190 |
const FuncExportVector& exports, |
| 3191 |
CompiledCode* code) { |
| 3192 |
LifoAlloc lifo(STUBS_LIFO_DEFAULT_CHUNK_SIZE, js::MallocArena); |
| 3193 |
TempAllocator alloc(&lifo); |
| 3194 |
JitContext jcx; |
| 3195 |
WasmMacroAssembler masm(alloc); |
| 3196 |
AutoCreatedBy acb(masm, "wasm::GenerateEntryStubs"); |
| 3197 |
|
| 3198 |
// Swap in already-allocated empty vectors to avoid malloc/free. |
| 3199 |
if (!code->swap(masm)) { |
| 3200 |
return false; |
| 3201 |
} |
| 3202 |
|
| 3203 |
JitSpew(JitSpew_Codegen, "# Emitting wasm export stubs"); |
| 3204 |
|
| 3205 |
Maybe<ImmPtr> noAbsolute; |
| 3206 |
for (size_t i = 0; i < exports.length(); i++) { |
| 3207 |
const FuncExport& fe = exports[i]; |
| 3208 |
const FuncType& funcType = codeMeta.getFuncType(fe.funcIndex()); |
| 3209 |
if (!fe.hasEagerStubs()) { |
| 3210 |
continue; |
| 3211 |
} |
| 3212 |
if (!GenerateEntryStubs(masm, i, fe, funcType, noAbsolute, |
| 3213 |
codeMeta.isAsmJS(), &code->codeRanges)) { |
| 3214 |
return false; |
| 3215 |
} |
| 3216 |
} |
| 3217 |
|
| 3218 |
masm.finish(); |
| 3219 |
if (masm.oom()) { |
| 3220 |
return false; |
| 3221 |
} |
| 3222 |
|
| 3223 |
return code->swap(masm); |
| 3224 |
} |
| 3225 |
|
| 3226 |
bool wasm::GenerateEntryStubs(MacroAssembler& masm, size_t funcExportIndex, |
| 3227 |
const FuncExport& fe, const FuncType& funcType, |
| 3228 |
const Maybe<ImmPtr>& callee, bool isAsmJS, |
| 3229 |
CodeRangeVector* codeRanges) { |
| 3230 |
MOZ_ASSERT(!callee == fe.hasEagerStubs()); |
| 3231 |
MOZ_ASSERT_IF(isAsmJS, fe.hasEagerStubs()); |
| 3232 |
|
| 3233 |
Offsets offsets; |
| 3234 |
if (!GenerateInterpEntry(masm, fe, funcType, callee, &offsets)) { |
| 3235 |
return false; |
| 3236 |
} |
| 3237 |
if (!codeRanges->emplaceBack(CodeRange::InterpEntry, fe.funcIndex(), |
| 3238 |
offsets)) { |
| 3239 |
return false; |
| 3240 |
} |
| 3241 |
|
| 3242 |
if (isAsmJS || !funcType.canHaveJitEntry()) { |
| 3243 |
return true; |
| 3244 |
} |
| 3245 |
|
| 3246 |
CallableOffsets jitOffsets; |
| 3247 |
if (!GenerateJitEntry(masm, funcExportIndex, fe, funcType, callee, |
| 3248 |
&jitOffsets)) { |
| 3249 |
return false; |
| 3250 |
} |
| 3251 |
return codeRanges->emplaceBack(CodeRange::JitEntry, fe.funcIndex(), |
| 3252 |
jitOffsets); |
| 3253 |
} |
| 3254 |
|
| 3255 |
bool wasm::GenerateProvisionalLazyJitEntryStub(MacroAssembler& masm, |
| 3256 |
Offsets* offsets) { |
| 3257 |
AssertExpectedSP(masm); |
| 3258 |
masm.setFramePushed(0); |
| 3259 |
offsets->begin = masm.currentOffset(); |
| 3260 |
|
| 3261 |
#ifdef JS_CODEGEN_ARM64 |
| 3262 |
// Unaligned ABI calls require SP+PSP, but our mode here is SP-only |
| 3263 |
masm.SetStackPointer64(PseudoStackPointer64); |
| 3264 |
masm.Mov(PseudoStackPointer64, sp); |
| 3265 |
#endif |
| 3266 |
|
| 3267 |
#ifdef JS_USE_LINK_REGISTER |
| 3268 |
masm.pushReturnAddress(); |
| 3269 |
#endif |
| 3270 |
|
| 3271 |
AllocatableGeneralRegisterSet regs(GeneralRegisterSet::Volatile()); |
| 3272 |
Register temp = regs.takeAny(); |
| 3273 |
|
| 3274 |
using Fn = void* (*)(); |
| 3275 |
masm.setupUnalignedABICall(temp); |
| 3276 |
masm.callWithABI<Fn, GetContextSensitiveInterpreterStub>( |
| 3277 |
ABIType::General, CheckUnsafeCallWithABI::DontCheckHasExitFrame); |
| 3278 |
|
| 3279 |
#ifdef JS_USE_LINK_REGISTER |
| 3280 |
masm.popReturnAddress(); |
| 3281 |
#endif |
| 3282 |
|
| 3283 |
masm.jump(ReturnReg); |
| 3284 |
|
| 3285 |
#ifdef JS_CODEGEN_ARM64 |
| 3286 |
// Undo the SP+PSP mode |
| 3287 |
masm.SetStackPointer64(sp); |
| 3288 |
#endif |
| 3289 |
|
| 3290 |
return FinishOffsets(masm, offsets); |
| 3291 |
} |
| 3292 |
|
| 3293 |
bool wasm::GenerateStubs(const CodeMetadata& codeMeta, |
| 3294 |
const FuncImportVector& imports, |
| 3295 |
const FuncExportVector& exports, CompiledCode* code) { |
| 3296 |
LifoAlloc lifo(STUBS_LIFO_DEFAULT_CHUNK_SIZE, js::MallocArena); |
| 3297 |
TempAllocator alloc(&lifo); |
| 3298 |
JitContext jcx; |
| 3299 |
WasmMacroAssembler masm(alloc); |
| 3300 |
AutoCreatedBy acb(masm, "wasm::GenerateStubs"); |
| 3301 |
|
| 3302 |
// Swap in already-allocated empty vectors to avoid malloc/free. |
| 3303 |
if (!code->swap(masm)) { |
| 3304 |
return false; |
| 3305 |
} |
| 3306 |
|
| 3307 |
Label throwLabel; |
| 3308 |
|
| 3309 |
JitSpew(JitSpew_Codegen, "# Emitting wasm import stubs"); |
| 3310 |
|
| 3311 |
for (uint32_t funcIndex = 0; funcIndex < imports.length(); funcIndex++) { |
| 3312 |
const FuncImport& fi = imports[funcIndex]; |
| 3313 |
const FuncType& funcType = codeMeta.getFuncType(funcIndex); |
| 3314 |
|
| 3315 |
CallIndirectId callIndirectId = |
| 3316 |
CallIndirectId::forFunc(codeMeta, funcIndex); |
| 3317 |
|
| 3318 |
FuncOffsets wrapperOffsets; |
| 3319 |
if (!GenerateImportFunction( |
| 3320 |
masm, codeMeta.offsetOfFuncImportInstanceData(funcIndex), funcType, |
| 3321 |
callIndirectId, &wrapperOffsets, &code->stackMaps)) { |
| 3322 |
return false; |
| 3323 |
} |
| 3324 |
if (!code->codeRanges.emplaceBack(funcIndex, wrapperOffsets, |
| 3325 |
/* hasUnwindInfo = */ false)) { |
| 3326 |
return false; |
| 3327 |
} |
| 3328 |
|
| 3329 |
CallableOffsets interpOffsets; |
| 3330 |
if (!GenerateImportInterpExit(masm, fi, funcType, funcIndex, &throwLabel, |
| 3331 |
&interpOffsets)) { |
| 3332 |
return false; |
| 3333 |
} |
| 3334 |
if (!code->codeRanges.emplaceBack(CodeRange::ImportInterpExit, funcIndex, |
| 3335 |
interpOffsets)) { |
| 3336 |
return false; |
| 3337 |
} |
| 3338 |
|
| 3339 |
// Skip if the function does not have a signature that allows for a JIT |
| 3340 |
// exit. |
| 3341 |
if (!funcType.canHaveJitExit()) { |
| 3342 |
continue; |
| 3343 |
} |
| 3344 |
|
| 3345 |
ImportOffsets jitOffsets; |
| 3346 |
if (!GenerateImportJitExit( |
| 3347 |
masm, codeMeta.offsetOfFuncImportInstanceData(funcIndex), funcType, |
| 3348 |
funcIndex, interpOffsets.begin, &throwLabel, &jitOffsets)) { |
| 3349 |
return false; |
| 3350 |
} |
| 3351 |
if (!code->codeRanges.emplaceBack(CodeRange::ImportJitExit, funcIndex, |
| 3352 |
jitOffsets)) { |
| 3353 |
return false; |
| 3354 |
} |
| 3355 |
} |
| 3356 |
|
| 3357 |
JitSpew(JitSpew_Codegen, "# Emitting wasm entry stubs"); |
| 3358 |
|
| 3359 |
Maybe<ImmPtr> noAbsolute; |
| 3360 |
for (size_t i = 0; i < exports.length(); i++) { |
| 3361 |
const FuncExport& fe = exports[i]; |
| 3362 |
const FuncType& funcType = codeMeta.getFuncType(fe.funcIndex()); |
| 3363 |
if (!fe.hasEagerStubs()) { |
| 3364 |
continue; |
| 3365 |
} |
| 3366 |
if (!GenerateEntryStubs(masm, i, fe, funcType, noAbsolute, |
| 3367 |
codeMeta.isAsmJS(), &code->codeRanges)) { |
| 3368 |
return false; |
| 3369 |
} |
| 3370 |
} |
| 3371 |
|
| 3372 |
JitSpew(JitSpew_Codegen, "# Emitting wasm trap, debug and throw stubs"); |
| 3373 |
|
| 3374 |
Offsets offsets; |
| 3375 |
|
| 3376 |
if (!GenerateTrapExit(masm, &throwLabel, &offsets)) { |
| 3377 |
return false; |
| 3378 |
} |
| 3379 |
if (!code->codeRanges.emplaceBack(CodeRange::TrapExit, offsets)) { |
| 3380 |
return false; |
| 3381 |
} |
| 3382 |
|
| 3383 |
CallableOffsets callableOffsets; |
| 3384 |
if (!GenerateDebugStub(masm, &throwLabel, &callableOffsets)) { |
| 3385 |
return false; |
| 3386 |
} |
| 3387 |
if (!code->codeRanges.emplaceBack(CodeRange::DebugStub, callableOffsets)) { |
| 3388 |
return false; |
| 3389 |
} |
| 3390 |
|
| 3391 |
if (!GenerateRequestTierUpStub(masm, &callableOffsets)) { |
| 3392 |
return false; |
| 3393 |
} |
| 3394 |
if (!code->codeRanges.emplaceBack(CodeRange::RequestTierUpStub, |
| 3395 |
callableOffsets)) { |
| 3396 |
return false; |
| 3397 |
} |
| 3398 |
|
| 3399 |
if (!GenerateUpdateCallRefMetricsStub(masm, &callableOffsets)) { |
| 3400 |
return false; |
| 3401 |
} |
| 3402 |
if (!code->codeRanges.emplaceBack(CodeRange::UpdateCallRefMetricsStub, |
| 3403 |
callableOffsets)) { |
| 3404 |
return false; |
| 3405 |
} |
| 3406 |
|
| 3407 |
if (!GenerateThrowStub(masm, &throwLabel, &offsets)) { |
| 3408 |
return false; |
| 3409 |
} |
| 3410 |
if (!code->codeRanges.emplaceBack(CodeRange::Throw, offsets)) { |
| 3411 |
return false; |
| 3412 |
} |
| 3413 |
|
| 3414 |
masm.finish(); |
| 3415 |
if (masm.oom()) { |
| 3416 |
return false; |
| 3417 |
} |
| 3418 |
|
| 3419 |
return code->swap(masm); |
| 3420 |
} |