|
Line 0
a/Source/JavaScriptCore/wasm/WasmJITCompiler.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2015 Apple Inc. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* 1. Redistributions of source code must retain the above copyright |
| 8 |
* notice, this list of conditions and the following disclaimer. |
| 9 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer in the |
| 11 |
* documentation and/or other materials provided with the distribution. |
| 12 |
* |
| 13 |
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 |
*/ |
| 25 |
|
| 26 |
/* |
| 27 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 28 |
you may not use this file except in compliance with the License. |
| 29 |
You may obtain a copy of the License at |
| 30 |
|
| 31 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 32 |
|
| 33 |
Unless required by applicable law or agreed to in writing, software |
| 34 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 35 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 36 |
See the License for the specific language governing permissions and |
| 37 |
limitations under the License. |
| 38 |
*/ |
| 39 |
|
| 40 |
#include "config.h" |
| 41 |
#include "WasmJITCompiler.h" |
| 42 |
|
| 43 |
#include "JITCode.h" |
| 44 |
#include "JSCJSValueInlines.h" |
| 45 |
#include "LinkBuffer.h" |
| 46 |
#include "MaxFrameExtentForSlowPathCall.h" |
| 47 |
#include "Register.h" |
| 48 |
|
| 49 |
namespace JSC { |
| 50 |
|
| 51 |
namespace Wasm { |
| 52 |
|
| 53 |
class JumpScope { |
| 54 |
public: |
| 55 |
JumpScope(MacroAssembler* macroAssembler) |
| 56 |
: m_macroAssembler(macroAssembler) |
| 57 |
, m_linked(false) |
| 58 |
{ |
| 59 |
} |
| 60 |
|
| 61 |
JumpScope(MacroAssembler* macroAssembler, const MacroAssembler::Label& label) |
| 62 |
: m_macroAssembler(macroAssembler) |
| 63 |
, m_linked(true) |
| 64 |
, m_label(label) |
| 65 |
{ |
| 66 |
} |
| 67 |
|
| 68 |
void jump() |
| 69 |
{ |
| 70 |
if (m_linked) |
| 71 |
m_macroAssembler->jump(m_label); |
| 72 |
else |
| 73 |
m_jumpList.append(m_macroAssembler->jump()); |
| 74 |
} |
| 75 |
|
| 76 |
void link() |
| 77 |
{ |
| 78 |
ASSERT(!m_linked); |
| 79 |
m_jumpList.link(m_macroAssembler); |
| 80 |
} |
| 81 |
|
| 82 |
private: |
| 83 |
MacroAssembler* m_macroAssembler; |
| 84 |
bool m_linked; |
| 85 |
MacroAssembler::Label m_label; |
| 86 |
MacroAssembler::JumpList m_jumpList; |
| 87 |
}; |
| 88 |
|
| 89 |
enum class LinearMemoryType : unsigned { |
| 90 |
I8, |
| 91 |
S8, |
| 92 |
U8, |
| 93 |
I16, |
| 94 |
S16, |
| 95 |
U16, |
| 96 |
I32, |
| 97 |
F32, |
| 98 |
F64 |
| 99 |
}; |
| 100 |
|
| 101 |
RefPtr<JITCode> JITCompiler::compileFunction(VM& vm, size_t functionIndex) |
| 102 |
{ |
| 103 |
emitFunctionPrologue(); |
| 104 |
emitPutImmediateToCallFrameHeader(m_codeBlock, JSStack::CodeBlock); |
| 105 |
|
| 106 |
const Signature& signature = m_module->m_signatures[m_module->m_funcSignatures[functionIndex]]; |
| 107 |
m_currentReturnType = signature.returnType; |
| 108 |
parseArguments(signature); |
| 109 |
|
| 110 |
// FIXME: Calculate the actual height. (Be careful about calls.) |
| 111 |
m_stackHeight = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), signature.arguments.size() + 8) * sizeof(Register); |
| 112 |
allocateSpaceOnStack(); |
| 113 |
|
| 114 |
parseVars(); |
| 115 |
parseStmtList(); |
| 116 |
|
| 117 |
// FIXME: Skip these if the last statement is a return statement. |
| 118 |
move(TrustedImm64(JSValue::encode(jsUndefined())), GPRInfo::regT0); // FIXME: Should we return undefined? |
| 119 |
deallocateSpaceOnStack(); |
| 120 |
emitFunctionEpilogue(); |
| 121 |
ret(); |
| 122 |
|
| 123 |
LinkBuffer patchBuffer(vm, *this, 0, JITCompilationMustSucceed); |
| 124 |
MacroAssemblerCodePtr withArityCheck; |
| 125 |
CodeRef result = FINALIZE_CODE(patchBuffer, ("Baseline JIT code for WebAssembly")); |
| 126 |
return adoptRef(new DirectJITCode(result, withArityCheck, JITCode::BaselineJIT)); |
| 127 |
} |
| 128 |
|
| 129 |
void JITCompiler::allocateSpaceOnStack() |
| 130 |
{ |
| 131 |
addPtr(TrustedImm32(-m_stackHeight), stackPointerRegister); |
| 132 |
} |
| 133 |
|
| 134 |
void JITCompiler::deallocateSpaceOnStack() |
| 135 |
{ |
| 136 |
addPtr(TrustedImm32(m_stackHeight), stackPointerRegister); |
| 137 |
} |
| 138 |
|
| 139 |
void JITCompiler::parseArguments(const Signature& signature) |
| 140 |
{ |
| 141 |
size_t argumentCount = signature.arguments.size(); |
| 142 |
for (uint32_t argIndex = 0; argIndex < argumentCount; ++argIndex) { |
| 143 |
Type type = signature.arguments[argIndex]; |
| 144 |
m_currentLocalTypes.append(type); |
| 145 |
switch (type) { |
| 146 |
case Type::I32: |
| 147 |
load32(Address(GPRInfo::callFrameRegister, (argIndex + 1 + CallFrame::thisArgumentOffset()) * sizeof(Register)), GPRInfo::regT0); |
| 148 |
store32(GPRInfo::regT0, stackAddress(argIndex)); |
| 149 |
break; |
| 150 |
case Type::F32: |
| 151 |
RELEASE_ASSERT_NOT_REACHED(); |
| 152 |
case Type::F64: |
| 153 |
load64(Address(GPRInfo::callFrameRegister, (argIndex + 1 + CallFrame::thisArgumentOffset()) * sizeof(Register)), GPRInfo::regT0); |
| 154 |
unboxDoubleWithoutAssertions(GPRInfo::regT0, FPRInfo::fpRegT0); |
| 155 |
storeDouble(FPRInfo::fpRegT0, stackAddress(argIndex)); |
| 156 |
break; |
| 157 |
default: |
| 158 |
RELEASE_ASSERT_NOT_REACHED(); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
void JITCompiler::parseVars() |
| 164 |
{ |
| 165 |
uint32_t numberOfI32Vars = 0; |
| 166 |
uint32_t numberOfF32Vars = 0; |
| 167 |
uint32_t numberOfF64Vars = 0; |
| 168 |
|
| 169 |
VarTypes varTypes; |
| 170 |
VarTypesWithImm varTypesWithImm; |
| 171 |
uint8_t imm; |
| 172 |
if (m_module->m_reader.code(&varTypes, &varTypesWithImm, &imm)) { |
| 173 |
if (varTypes & VarTypes::I32) |
| 174 |
numberOfI32Vars = m_module->m_reader.immU32(); |
| 175 |
if (varTypes & VarTypes::F32) |
| 176 |
numberOfF32Vars = m_module->m_reader.immU32(); |
| 177 |
if (varTypes & VarTypes::F64) |
| 178 |
numberOfF64Vars = m_module->m_reader.immU32(); |
| 179 |
} else |
| 180 |
numberOfI32Vars = imm; |
| 181 |
|
| 182 |
uint32_t numberOfVars = numberOfI32Vars + numberOfF32Vars + numberOfF64Vars; |
| 183 |
if (numberOfVars > 0) { |
| 184 |
uint32_t localIndex = m_currentLocalTypes.size(); |
| 185 |
for (uint32_t i = 0; i < numberOfI32Vars; ++i) { |
| 186 |
m_currentLocalTypes.append(Type::I32); |
| 187 |
store32(TrustedImm32(0), stackAddress(localIndex++)); |
| 188 |
} |
| 189 |
for (uint32_t i = 0; i < numberOfF32Vars; ++i) { |
| 190 |
m_currentLocalTypes.append(Type::F32); |
| 191 |
store32(TrustedImm32(0), stackAddress(localIndex++)); |
| 192 |
} |
| 193 |
for (uint32_t i = 0; i < numberOfF64Vars; ++i) { |
| 194 |
m_currentLocalTypes.append(Type::F64); |
| 195 |
store64(TrustedImm64(0), stackAddress(localIndex++)); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
void JITCompiler::parseStmt() |
| 201 |
{ |
| 202 |
Stmt stmt; |
| 203 |
StmtWithImm stmtWithImm; |
| 204 |
uint8_t imm; |
| 205 |
if (m_module->m_reader.code(&stmt, &stmtWithImm, &imm)) { |
| 206 |
switch (stmt) { |
| 207 |
case Stmt::SetLoc: |
| 208 |
parseSetLocal(0); |
| 209 |
break; |
| 210 |
case Stmt::SetGlo: |
| 211 |
RELEASE_ASSERT_NOT_REACHED(); |
| 212 |
case Stmt::I32Store8: |
| 213 |
parseStore(ReturnType::I32, LinearMemoryType::I8, false, 0); |
| 214 |
break; |
| 215 |
case Stmt::I32StoreOff8: |
| 216 |
parseStore(ReturnType::I32, LinearMemoryType::I8, true, 0); |
| 217 |
break; |
| 218 |
case Stmt::I32Store16: |
| 219 |
parseStore(ReturnType::I32, LinearMemoryType::I16, false, 0); |
| 220 |
break; |
| 221 |
case Stmt::I32StoreOff16: |
| 222 |
parseStore(ReturnType::I32, LinearMemoryType::I16, true, 0); |
| 223 |
break; |
| 224 |
case Stmt::I32Store32: |
| 225 |
parseStore(ReturnType::I32, LinearMemoryType::I32, false, 0); |
| 226 |
break; |
| 227 |
case Stmt::I32StoreOff32: |
| 228 |
parseStore(ReturnType::I32, LinearMemoryType::I32, true, 0); |
| 229 |
break; |
| 230 |
case Stmt::F32Store: |
| 231 |
parseStore(ReturnType::F32, LinearMemoryType::F32, false, 0); |
| 232 |
break; |
| 233 |
case Stmt::F32StoreOff: |
| 234 |
parseStore(ReturnType::F32, LinearMemoryType::F32, true, 0); |
| 235 |
break; |
| 236 |
case Stmt::F64Store: |
| 237 |
parseStore(ReturnType::F64, LinearMemoryType::F64, false, 0); |
| 238 |
break; |
| 239 |
case Stmt::F64StoreOff: |
| 240 |
parseStore(ReturnType::F64, LinearMemoryType::F64, true, 0); |
| 241 |
break; |
| 242 |
case Stmt::CallInt: |
| 243 |
parseCallInternal(ReturnType::Void, 0); |
| 244 |
break; |
| 245 |
case Stmt::CallInd: |
| 246 |
RELEASE_ASSERT_NOT_REACHED(); |
| 247 |
case Stmt::CallImp: |
| 248 |
parseCallImport(ReturnType::Void, 0); // FIXME: Is it really Void? |
| 249 |
break; |
| 250 |
case Stmt::Ret: |
| 251 |
parseReturnStmt(); |
| 252 |
return; |
| 253 |
case Stmt::Block: |
| 254 |
parseBlockStmt(); |
| 255 |
return; |
| 256 |
case Stmt::IfThen: |
| 257 |
parseIfStmt(); |
| 258 |
return; |
| 259 |
case Stmt::IfElse: |
| 260 |
parseIfElseStmt(); |
| 261 |
return; |
| 262 |
case Stmt::While: |
| 263 |
parseWhileStmt(); |
| 264 |
return; |
| 265 |
case Stmt::Do: |
| 266 |
parseDoStmt(); |
| 267 |
return; |
| 268 |
case Stmt::Label: |
| 269 |
parseLabelStmt(); |
| 270 |
return; |
| 271 |
case Stmt::Break: |
| 272 |
parseBreakStmt(); |
| 273 |
return; |
| 274 |
case Stmt::BreakLabel: |
| 275 |
parseBreakLabelStmt(); |
| 276 |
return; |
| 277 |
case Stmt::Continue: |
| 278 |
parseContinueStmt(); |
| 279 |
break; |
| 280 |
case Stmt::ContinueLabel: |
| 281 |
parseContinueLabelStmt(); |
| 282 |
return; |
| 283 |
case Stmt::Switch: |
| 284 |
parseSwitchStmt(); |
| 285 |
break; |
| 286 |
default: |
| 287 |
RELEASE_ASSERT_NOT_REACHED(); |
| 288 |
} |
| 289 |
} else { |
| 290 |
switch (stmtWithImm) { |
| 291 |
case StmtWithImm::SetLoc: |
| 292 |
parseSetLocal(imm, 0); |
| 293 |
break; |
| 294 |
case StmtWithImm::SetGlo: |
| 295 |
default: |
| 296 |
RELEASE_ASSERT_NOT_REACHED(); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
void JITCompiler::parseGetLocal(int dst) |
| 302 |
{ |
| 303 |
uint32_t imm = m_module->m_reader.immU32(); |
| 304 |
switch (m_currentLocalTypes[imm]) { |
| 305 |
case Type::I32: |
| 306 |
case Type::F32: |
| 307 |
load32(stackAddress(imm), GPRInfo::regT0); |
| 308 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 309 |
break; |
| 310 |
case Type::F64: |
| 311 |
load64(stackAddress(imm), GPRInfo::regT0); |
| 312 |
store64(GPRInfo::regT0, temporaryAddress(dst)); |
| 313 |
break; |
| 314 |
default: |
| 315 |
RELEASE_ASSERT_NOT_REACHED(); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
void JITCompiler::parseSetLocal(uint32_t imm, int dst) |
| 320 |
{ |
| 321 |
Type type = m_currentLocalTypes[imm]; |
| 322 |
parseExpr(ReturnType(type), dst); |
| 323 |
switch (type) { |
| 324 |
case Type::I32: |
| 325 |
case Type::F32: |
| 326 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 327 |
store32(GPRInfo::regT0, stackAddress(imm)); |
| 328 |
break; |
| 329 |
case Type::F64: |
| 330 |
load64(temporaryAddress(dst), GPRInfo::regT0); |
| 331 |
store64(GPRInfo::regT0, stackAddress(imm)); |
| 332 |
break; |
| 333 |
default: |
| 334 |
RELEASE_ASSERT_NOT_REACHED(); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
void JITCompiler::parseSetLocal(int dst) |
| 339 |
{ |
| 340 |
parseSetLocal(m_module->m_reader.immU32(), dst); |
| 341 |
} |
| 342 |
|
| 343 |
static const void* JSC_HOST_CALL getArrayBufferData(Module* module) |
| 344 |
{ |
| 345 |
// FIXME: Do we really have to do this? |
| 346 |
return module->arrayBufferData(); |
| 347 |
} |
| 348 |
|
| 349 |
void JITCompiler::parseArrayBufferOffset(bool hasOffset, int dst) |
| 350 |
{ |
| 351 |
// TODO: Check for out-of-bounds access. |
| 352 |
uint32_t offset = hasOffset ? m_module->m_reader.immU32() : 0; |
| 353 |
uint32_t u32; |
| 354 |
if (m_module->m_reader.ifI32Lit(m_module->m_constantI32s, &u32)) |
| 355 |
store32(TrustedImm32(u32 + offset), temporaryAddress(dst)); |
| 356 |
else { |
| 357 |
parseExpr(ReturnType::I32, dst); |
| 358 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 359 |
if (offset) |
| 360 |
add32(TrustedImm32(offset), GPRInfo::regT0); |
| 361 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 362 |
} |
| 363 |
|
| 364 |
if (maxFrameExtentForSlowPathCall) |
| 365 |
addPtr(TrustedImm32(-maxFrameExtentForSlowPathCall), stackPointerRegister); |
| 366 |
setupArguments(TrustedImmPtr(m_module)); |
| 367 |
move(TrustedImmPtr(bitwise_cast<void*>(getArrayBufferData)), GPRInfo::nonArgGPR0); |
| 368 |
call(GPRInfo::nonArgGPR0); |
| 369 |
if (maxFrameExtentForSlowPathCall) |
| 370 |
addPtr(TrustedImm32(maxFrameExtentForSlowPathCall), stackPointerRegister); |
| 371 |
} |
| 372 |
|
| 373 |
void JITCompiler::parseLoad(ReturnType returnType, LinearMemoryType linearMemoryType, bool hasOffset, int dst) |
| 374 |
{ |
| 375 |
parseArrayBufferOffset(hasOffset, dst); |
| 376 |
load32(temporaryAddress(dst), GPRInfo::regT1); |
| 377 |
BaseIndex address = BaseIndex(GPRInfo::returnValueGPR, GPRInfo::regT1, TimesOne); |
| 378 |
|
| 379 |
switch (returnType) { |
| 380 |
case ReturnType::I32: |
| 381 |
switch (linearMemoryType) { |
| 382 |
case LinearMemoryType::S8: |
| 383 |
load8SignedExtendTo32(address, GPRInfo::regT0); |
| 384 |
break; |
| 385 |
case LinearMemoryType::U8: |
| 386 |
load8(address, GPRInfo::regT0); |
| 387 |
break; |
| 388 |
case LinearMemoryType::S16: |
| 389 |
load16SignedExtendTo32(address, GPRInfo::regT0); |
| 390 |
break; |
| 391 |
case LinearMemoryType::U16: |
| 392 |
load16(address, GPRInfo::regT0); |
| 393 |
break; |
| 394 |
case LinearMemoryType::I32: |
| 395 |
load32(address, GPRInfo::regT0); |
| 396 |
break; |
| 397 |
default: |
| 398 |
ASSERT_NOT_REACHED(); |
| 399 |
} |
| 400 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 401 |
break; |
| 402 |
case ReturnType::F32: |
| 403 |
load32(address, GPRInfo::regT0); |
| 404 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 405 |
break; |
| 406 |
case ReturnType::F64: |
| 407 |
load64(address, GPRInfo::regT0); |
| 408 |
store64(GPRInfo::regT0, temporaryAddress(dst)); |
| 409 |
break; |
| 410 |
default: |
| 411 |
RELEASE_ASSERT_NOT_REACHED(); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
void JITCompiler::parseStore(ReturnType returnType, LinearMemoryType linearMemoryType, bool hasOffset, int dst) |
| 416 |
{ |
| 417 |
parseArrayBufferOffset(hasOffset, dst + 1); |
| 418 |
storePtr(GPRInfo::returnValueGPR, temporaryAddress(dst)); |
| 419 |
|
| 420 |
parseExpr(returnType, dst + 2); |
| 421 |
|
| 422 |
loadPtr(temporaryAddress(dst), GPRInfo::regT1); |
| 423 |
load32(temporaryAddress(dst + 1), GPRInfo::regT2); |
| 424 |
BaseIndex address = BaseIndex(GPRInfo::regT1, GPRInfo::regT2, TimesOne); |
| 425 |
|
| 426 |
switch (returnType) { |
| 427 |
case ReturnType::I32: |
| 428 |
load32(temporaryAddress(dst + 2), GPRInfo::regT0); |
| 429 |
switch (linearMemoryType) { |
| 430 |
case LinearMemoryType::I8: |
| 431 |
store8(GPRInfo::regT0, address); |
| 432 |
break; |
| 433 |
case LinearMemoryType::I16: |
| 434 |
store16(GPRInfo::regT0, address); |
| 435 |
break; |
| 436 |
case LinearMemoryType::I32: |
| 437 |
store32(GPRInfo::regT0, address); |
| 438 |
break; |
| 439 |
default: |
| 440 |
ASSERT_NOT_REACHED(); |
| 441 |
} |
| 442 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 443 |
break; |
| 444 |
case ReturnType::F32: |
| 445 |
load32(temporaryAddress(dst + 2), GPRInfo::regT0); |
| 446 |
store32(GPRInfo::regT0, address); |
| 447 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 448 |
break; |
| 449 |
case ReturnType::F64: |
| 450 |
load64(temporaryAddress(dst + 2), GPRInfo::regT0); |
| 451 |
store64(GPRInfo::regT0, address); |
| 452 |
store64(GPRInfo::regT0, temporaryAddress(dst)); |
| 453 |
break; |
| 454 |
default: |
| 455 |
RELEASE_ASSERT_NOT_REACHED(); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
void JITCompiler::parseReturnStmt() |
| 460 |
{ |
| 461 |
switch (m_currentReturnType) { |
| 462 |
case ReturnType::I32: |
| 463 |
parseExpr(ReturnType::I32, 0); |
| 464 |
load32(temporaryAddress(0), GPRInfo::returnValueGPR); |
| 465 |
or64(GPRInfo::tagTypeNumberRegister, GPRInfo::returnValueGPR); |
| 466 |
break; |
| 467 |
case ReturnType::F32: |
| 468 |
parseExpr(ReturnType::F32, 0); |
| 469 |
loadDouble(temporaryAddress(0), FPRInfo::fpRegT0); // FIXME: This loads 64 bits! |
| 470 |
convertFloatToDouble(FPRInfo::fpRegT0, FPRInfo::fpRegT0); |
| 471 |
boxDouble(FPRInfo::fpRegT0, GPRInfo::returnValueGPR); |
| 472 |
break; |
| 473 |
case ReturnType::F64: |
| 474 |
parseExpr(ReturnType::F64, 0); |
| 475 |
loadDouble(temporaryAddress(0), FPRInfo::fpRegT0); |
| 476 |
boxDouble(FPRInfo::fpRegT0, GPRInfo::returnValueGPR); |
| 477 |
break; |
| 478 |
case ReturnType::Void: |
| 479 |
break; |
| 480 |
} |
| 481 |
|
| 482 |
deallocateSpaceOnStack(); |
| 483 |
emitFunctionEpilogue(); |
| 484 |
ret(); |
| 485 |
} |
| 486 |
|
| 487 |
void JITCompiler::parseStmtList() |
| 488 |
{ |
| 489 |
uint32_t numberOfStmts = m_module->m_reader.immU32(); |
| 490 |
for (uint32_t i = 0; i < numberOfStmts; ++i) |
| 491 |
parseStmt(); |
| 492 |
} |
| 493 |
|
| 494 |
void JITCompiler::parseBlockStmt() |
| 495 |
{ |
| 496 |
parseStmtList(); |
| 497 |
} |
| 498 |
|
| 499 |
void JITCompiler::parseIfStmt() |
| 500 |
{ |
| 501 |
parseExpr(ReturnType::I32, 0); |
| 502 |
load32(temporaryAddress(0), GPRInfo::regT0); |
| 503 |
Jump end = branchTest32(Zero, GPRInfo::regT0); |
| 504 |
parseStmt(); |
| 505 |
end.link(this); |
| 506 |
} |
| 507 |
|
| 508 |
void JITCompiler::parseIfElseStmt() |
| 509 |
{ |
| 510 |
parseExpr(ReturnType::I32, 0); |
| 511 |
load32(temporaryAddress(0), GPRInfo::regT0); |
| 512 |
Jump els = branchTest32(Zero, GPRInfo::regT0); |
| 513 |
parseStmt(); |
| 514 |
Jump end = jump(); |
| 515 |
els.link(this); |
| 516 |
parseStmt(); |
| 517 |
end.link(this); |
| 518 |
} |
| 519 |
|
| 520 |
void JITCompiler::parseWhileStmt() |
| 521 |
{ |
| 522 |
JumpScope continueJumpScope(this, label()); |
| 523 |
pushContinueJumpScope(&continueJumpScope); |
| 524 |
|
| 525 |
JumpList breakJumpList; |
| 526 |
pushBreakJumpList(&breakJumpList); |
| 527 |
|
| 528 |
parseExpr(ReturnType::I32, 0); |
| 529 |
load32(temporaryAddress(0), GPRInfo::regT0); |
| 530 |
Jump end = branchTest32(Zero, GPRInfo::regT0); |
| 531 |
parseStmt(); |
| 532 |
continueJumpScope.jump(); |
| 533 |
end.link(this); |
| 534 |
breakJumpList.link(this); |
| 535 |
|
| 536 |
popBreakJumpList(); |
| 537 |
|
| 538 |
popContinueJumpScope(); |
| 539 |
} |
| 540 |
|
| 541 |
void JITCompiler::parseDoStmt() |
| 542 |
{ |
| 543 |
JumpScope continueJumpScope(this); |
| 544 |
pushContinueJumpScope(&continueJumpScope); |
| 545 |
|
| 546 |
JumpList breakJumpList; |
| 547 |
pushBreakJumpList(&breakJumpList); |
| 548 |
|
| 549 |
Label start = label(); |
| 550 |
parseStmt(); |
| 551 |
continueJumpScope.link(); |
| 552 |
parseExpr(ReturnType::I32, 0); |
| 553 |
load32(temporaryAddress(0), GPRInfo::regT0); |
| 554 |
Jump jumpToStart = branchTest32(NonZero, GPRInfo::regT0); |
| 555 |
jumpToStart.linkTo(start, this); |
| 556 |
breakJumpList.link(this); |
| 557 |
|
| 558 |
popBreakJumpList(); |
| 559 |
|
| 560 |
popContinueJumpScope(); |
| 561 |
} |
| 562 |
|
| 563 |
void JITCompiler::parseLabelStmt() |
| 564 |
{ |
| 565 |
Label continueLabel = label(); |
| 566 |
JumpList breakJumpList; |
| 567 |
pushContinueLabelLabel(&continueLabel); |
| 568 |
pushBreakLabelJumpList(&breakJumpList); |
| 569 |
parseStmt(); |
| 570 |
breakJumpList.link(this); |
| 571 |
popBreakLabelJumpList(); |
| 572 |
popContinueLabelLabel(); |
| 573 |
} |
| 574 |
|
| 575 |
void JITCompiler::parseBreakStmt() |
| 576 |
{ |
| 577 |
topBreakJumpList()->append(jump()); |
| 578 |
} |
| 579 |
|
| 580 |
void JITCompiler::parseBreakLabelStmt() |
| 581 |
{ |
| 582 |
uint32_t index = m_module->m_reader.immU32(); |
| 583 |
breakLabelJumpList(index)->append(jump()); |
| 584 |
} |
| 585 |
|
| 586 |
void JITCompiler::parseContinueStmt() |
| 587 |
{ |
| 588 |
topContinueJumpScope()->jump(); |
| 589 |
} |
| 590 |
|
| 591 |
void JITCompiler::parseContinueLabelStmt() |
| 592 |
{ |
| 593 |
uint32_t index = m_module->m_reader.immU32(); |
| 594 |
jump(*continueLabelLabel(index)); |
| 595 |
} |
| 596 |
|
| 597 |
void JITCompiler::parseSwitchStmt() |
| 598 |
{ |
| 599 |
JumpList breakJumpList; |
| 600 |
pushBreakJumpList(&breakJumpList); |
| 601 |
|
| 602 |
uint32_t numberOfCases = m_module->m_reader.immU32(); |
| 603 |
parseExpr(ReturnType::I32, 0); |
| 604 |
load32(temporaryAddress(0), GPRInfo::regT0); |
| 605 |
|
| 606 |
Jump table = jump(); |
| 607 |
|
| 608 |
Vector<Label> labels; |
| 609 |
Vector<int32_t> imms; |
| 610 |
Label defaultLabel; |
| 611 |
bool hasDefault = false; |
| 612 |
|
| 613 |
for (uint32_t i = 0; i < numberOfCases; ++i) { |
| 614 |
switch (m_module->m_reader.switchCase()) { |
| 615 |
case SwitchCase::Case0: |
| 616 |
labels.append(label()); |
| 617 |
imms.append(m_module->m_reader.immS32()); |
| 618 |
break; |
| 619 |
case SwitchCase::Case1: |
| 620 |
labels.append(label()); |
| 621 |
imms.append(m_module->m_reader.immS32()); |
| 622 |
parseStmt(); |
| 623 |
break; |
| 624 |
case SwitchCase::CaseN: |
| 625 |
labels.append(label()); |
| 626 |
imms.append(m_module->m_reader.immS32()); |
| 627 |
parseStmtList(); |
| 628 |
break; |
| 629 |
case SwitchCase::Default0: |
| 630 |
defaultLabel = label(); |
| 631 |
hasDefault = true; |
| 632 |
break; |
| 633 |
case SwitchCase::Default1: |
| 634 |
defaultLabel = label(); |
| 635 |
hasDefault = true; |
| 636 |
parseStmt(); |
| 637 |
break; |
| 638 |
case SwitchCase::DefaultN: |
| 639 |
defaultLabel = label(); |
| 640 |
hasDefault = true; |
| 641 |
parseStmtList(); |
| 642 |
break; |
| 643 |
default: |
| 644 |
RELEASE_ASSERT_NOT_REACHED(); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
Jump end = jump(); |
| 649 |
table.link(this); |
| 650 |
|
| 651 |
for (size_t i = 0; i < labels.size(); ++i) { |
| 652 |
Jump jmp = branch32(Equal, GPRInfo::regT0, TrustedImm32(imms[i])); |
| 653 |
jmp.linkTo(labels[i], this); |
| 654 |
} |
| 655 |
if (hasDefault) |
| 656 |
jump(defaultLabel); |
| 657 |
|
| 658 |
end.link(this); |
| 659 |
|
| 660 |
breakJumpList.link(this); |
| 661 |
popBreakJumpList(); |
| 662 |
} |
| 663 |
|
| 664 |
void JITCompiler::parseExpr(ReturnType returnType, int dst) |
| 665 |
{ |
| 666 |
switch (returnType) { |
| 667 |
case ReturnType::I32: |
| 668 |
parseExprI32(dst); |
| 669 |
break; |
| 670 |
case ReturnType::F64: |
| 671 |
parseExprF64(dst); |
| 672 |
break; |
| 673 |
case ReturnType::Void: |
| 674 |
parseExprVoid(dst); |
| 675 |
break; |
| 676 |
default: |
| 677 |
RELEASE_ASSERT_NOT_REACHED(); |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
void JITCompiler::parseExprI32(int dst) |
| 682 |
{ |
| 683 |
I32 i32; |
| 684 |
I32WithImm i32WithImm; |
| 685 |
uint8_t imm; |
| 686 |
|
| 687 |
if (m_module->m_reader.code(&i32, &i32WithImm, &imm)) { |
| 688 |
switch (i32) { |
| 689 |
case I32::LitPool: |
| 690 |
store32(TrustedImm32(m_module->m_constantI32s[m_module->m_reader.immU32()]), temporaryAddress(dst)); |
| 691 |
break; |
| 692 |
case I32::LitImm: |
| 693 |
store32(TrustedImm32(m_module->m_reader.immU32()), temporaryAddress(dst)); |
| 694 |
break; |
| 695 |
case I32::GetLoc: |
| 696 |
parseGetLocal(dst); |
| 697 |
break; |
| 698 |
case I32::GetGlo: |
| 699 |
RELEASE_ASSERT_NOT_REACHED(); |
| 700 |
case I32::SetLoc: |
| 701 |
parseSetLocal(dst); |
| 702 |
break; |
| 703 |
case I32::SetGlo: |
| 704 |
RELEASE_ASSERT_NOT_REACHED(); |
| 705 |
case I32::SLoad8: |
| 706 |
parseLoad(ReturnType::I32, LinearMemoryType::S8, false, dst); |
| 707 |
break; |
| 708 |
case I32::SLoadOff8: |
| 709 |
parseLoad(ReturnType::I32, LinearMemoryType::S8, true, dst); |
| 710 |
break; |
| 711 |
case I32::ULoad8: |
| 712 |
parseLoad(ReturnType::I32, LinearMemoryType::U8, false, dst); |
| 713 |
break; |
| 714 |
case I32::ULoadOff8: |
| 715 |
parseLoad(ReturnType::I32, LinearMemoryType::U8, true, dst); |
| 716 |
break; |
| 717 |
case I32::SLoad16: |
| 718 |
parseLoad(ReturnType::I32, LinearMemoryType::S16, false, dst); |
| 719 |
break; |
| 720 |
case I32::SLoadOff16: |
| 721 |
parseLoad(ReturnType::I32, LinearMemoryType::S16, true, dst); |
| 722 |
break; |
| 723 |
case I32::ULoad16: |
| 724 |
parseLoad(ReturnType::I32, LinearMemoryType::U16, false, dst); |
| 725 |
break; |
| 726 |
case I32::ULoadOff16: |
| 727 |
parseLoad(ReturnType::I32, LinearMemoryType::U16, true, dst); |
| 728 |
break; |
| 729 |
case I32::Load32: |
| 730 |
parseLoad(ReturnType::I32, LinearMemoryType::I32, false, dst); |
| 731 |
break; |
| 732 |
case I32::LoadOff32: |
| 733 |
parseLoad(ReturnType::I32, LinearMemoryType::I32, true, dst); |
| 734 |
break; |
| 735 |
case I32::Store8: |
| 736 |
parseStore(ReturnType::I32, LinearMemoryType::I8, false, dst); |
| 737 |
break; |
| 738 |
case I32::StoreOff8: |
| 739 |
parseStore(ReturnType::I32, LinearMemoryType::I8, true, dst); |
| 740 |
break; |
| 741 |
case I32::Store16: |
| 742 |
parseStore(ReturnType::I32, LinearMemoryType::I16, false, dst); |
| 743 |
break; |
| 744 |
case I32::StoreOff16: |
| 745 |
parseStore(ReturnType::I32, LinearMemoryType::I16, true, dst); |
| 746 |
break; |
| 747 |
case I32::Store32: |
| 748 |
parseStore(ReturnType::I32, LinearMemoryType::I32, false, dst); |
| 749 |
break; |
| 750 |
case I32::StoreOff32: |
| 751 |
parseStore(ReturnType::I32, LinearMemoryType::I32, true, dst); |
| 752 |
break; |
| 753 |
case I32::CallInt: |
| 754 |
parseCallInternal(ReturnType::I32, dst); |
| 755 |
break; |
| 756 |
case I32::CallInd: |
| 757 |
RELEASE_ASSERT_NOT_REACHED(); |
| 758 |
case I32::CallImp: |
| 759 |
parseCallImport(ReturnType::I32, dst); |
| 760 |
break; |
| 761 |
case I32::Cond: |
| 762 |
parseCond(ReturnType::I32, dst); |
| 763 |
break; |
| 764 |
case I32::Comma: |
| 765 |
parseComma(ReturnType::I32, dst); |
| 766 |
break; |
| 767 |
case I32::FromF32: |
| 768 |
RELEASE_ASSERT_NOT_REACHED(); |
| 769 |
case I32::FromF64: |
| 770 |
parseExprF64(dst); |
| 771 |
loadDouble(temporaryAddress(dst), FPRInfo::fpRegT0); |
| 772 |
truncateDoubleToInt32(FPRInfo::fpRegT0, GPRInfo::regT0); |
| 773 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 774 |
break; |
| 775 |
case I32::Neg: |
| 776 |
case I32::BitNot: |
| 777 |
case I32::Clz: |
| 778 |
case I32::Abs: |
| 779 |
parseUnaryOp(static_cast<uint8_t>(i32), ReturnType::I32, dst); |
| 780 |
break; |
| 781 |
case I32::Add: |
| 782 |
case I32::Sub: |
| 783 |
case I32::Mul: |
| 784 |
case I32::SDiv: |
| 785 |
case I32::SMod: |
| 786 |
case I32::BitOr: |
| 787 |
case I32::BitAnd: |
| 788 |
case I32::BitXor: |
| 789 |
case I32::Lsh: |
| 790 |
case I32::ArithRsh: |
| 791 |
case I32::LogicRsh: |
| 792 |
parseBinaryOp(static_cast<uint8_t>(i32), ReturnType::I32, dst); |
| 793 |
break; |
| 794 |
case I32::UDiv: |
| 795 |
case I32::UMod: |
| 796 |
case I32::LogicNot: |
| 797 |
RELEASE_ASSERT_NOT_REACHED(); |
| 798 |
case I32::EqI32: |
| 799 |
parseRelationalOpI32(Equal, dst); |
| 800 |
break; |
| 801 |
case I32::EqF32: |
| 802 |
RELEASE_ASSERT_NOT_REACHED(); |
| 803 |
case I32::EqF64: |
| 804 |
parseRelationalOpF64(DoubleEqual, dst); |
| 805 |
break; |
| 806 |
case I32::NEqI32: |
| 807 |
parseRelationalOpI32(NotEqual, dst); |
| 808 |
break; |
| 809 |
case I32::NEqF32: |
| 810 |
RELEASE_ASSERT_NOT_REACHED(); |
| 811 |
case I32::NEqF64: |
| 812 |
parseRelationalOpF64(DoubleNotEqual, dst); |
| 813 |
break; |
| 814 |
case I32::SLeThI32: |
| 815 |
parseRelationalOpI32(LessThan, dst); |
| 816 |
break; |
| 817 |
case I32::ULeThI32: |
| 818 |
parseRelationalOpI32(Below, dst); |
| 819 |
break; |
| 820 |
case I32::LeThF32: |
| 821 |
RELEASE_ASSERT_NOT_REACHED(); |
| 822 |
case I32::LeThF64: |
| 823 |
parseRelationalOpF64(DoubleLessThan, dst); |
| 824 |
break; |
| 825 |
case I32::SLeEqI32: |
| 826 |
parseRelationalOpI32(LessThanOrEqual, dst); |
| 827 |
break; |
| 828 |
case I32::ULeEqI32: |
| 829 |
parseRelationalOpI32(BelowOrEqual, dst); |
| 830 |
break; |
| 831 |
case I32::LeEqF32: |
| 832 |
RELEASE_ASSERT_NOT_REACHED(); |
| 833 |
case I32::LeEqF64: |
| 834 |
parseRelationalOpF64(DoubleLessThanOrEqual, dst); |
| 835 |
break; |
| 836 |
case I32::SGrThI32: |
| 837 |
parseRelationalOpI32(GreaterThan, dst); |
| 838 |
break; |
| 839 |
case I32::UGrThI32: |
| 840 |
parseRelationalOpI32(Above, dst); |
| 841 |
break; |
| 842 |
case I32::GrThF32: |
| 843 |
RELEASE_ASSERT_NOT_REACHED(); |
| 844 |
case I32::GrThF64: |
| 845 |
parseRelationalOpF64(DoubleGreaterThan, dst); |
| 846 |
break; |
| 847 |
case I32::SGrEqI32: |
| 848 |
parseRelationalOpI32(GreaterThanOrEqual, dst); |
| 849 |
break; |
| 850 |
case I32::UGrEqI32: |
| 851 |
parseRelationalOpI32(AboveOrEqual, dst); |
| 852 |
break; |
| 853 |
case I32::GrEqF32: |
| 854 |
RELEASE_ASSERT_NOT_REACHED(); |
| 855 |
case I32::GrEqF64: |
| 856 |
parseRelationalOpF64(DoubleGreaterThanOrEqual, dst); |
| 857 |
break; |
| 858 |
case I32::SMin: |
| 859 |
parseMinMaxI32(LessThanOrEqual, dst); |
| 860 |
break; |
| 861 |
case I32::UMin: |
| 862 |
parseMinMaxI32(BelowOrEqual, dst); |
| 863 |
break; |
| 864 |
case I32::SMax: |
| 865 |
parseMinMaxI32(GreaterThanOrEqual, dst); |
| 866 |
break; |
| 867 |
case I32::UMax: |
| 868 |
parseMinMaxI32(AboveOrEqual, dst); |
| 869 |
break; |
| 870 |
default: |
| 871 |
RELEASE_ASSERT_NOT_REACHED(); |
| 872 |
} |
| 873 |
} else { |
| 874 |
switch (i32WithImm) { |
| 875 |
case I32WithImm::LitImm: |
| 876 |
store32(TrustedImm32(imm), temporaryAddress(dst)); |
| 877 |
break; |
| 878 |
case I32WithImm::LitPool: |
| 879 |
store32(TrustedImm32(m_module->m_constantI32s[imm]), temporaryAddress(dst)); |
| 880 |
break; |
| 881 |
case I32WithImm::GetLoc: |
| 882 |
load32(stackAddress(imm), GPRInfo::regT0); |
| 883 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 884 |
break; |
| 885 |
default: |
| 886 |
RELEASE_ASSERT_NOT_REACHED(); |
| 887 |
} |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
void JITCompiler::parseExprF64(int dst) |
| 892 |
{ |
| 893 |
F64 f64; |
| 894 |
F64WithImm f64WithImm; |
| 895 |
uint8_t imm; |
| 896 |
|
| 897 |
if (m_module->m_reader.code(&f64, &f64WithImm, &imm)) { |
| 898 |
switch (f64) { |
| 899 |
case F64::LitPool: |
| 900 |
store64(TrustedImm64(reinterpretDoubleToInt64(m_module->m_constantF64s[m_module->m_reader.immU32()])), temporaryAddress(dst)); |
| 901 |
break; |
| 902 |
case F64::LitImm: |
| 903 |
store64(TrustedImm64(reinterpretDoubleToInt64(m_module->m_reader.fixedWidth<double>())), temporaryAddress(dst)); |
| 904 |
break; |
| 905 |
case F64::Load: |
| 906 |
parseLoad(ReturnType::F64, LinearMemoryType::F64, false, dst); |
| 907 |
break; |
| 908 |
case F64::LoadOff: |
| 909 |
parseLoad(ReturnType::F64, LinearMemoryType::F64, true, dst); |
| 910 |
break; |
| 911 |
case F64::Store: |
| 912 |
parseStore(ReturnType::F64, LinearMemoryType::F64, false, dst); |
| 913 |
break; |
| 914 |
case F64::StoreOff: |
| 915 |
parseStore(ReturnType::F64, LinearMemoryType::F64, true, dst); |
| 916 |
break; |
| 917 |
case F64::CallInt: |
| 918 |
parseCallInternal(ReturnType::F64, dst); |
| 919 |
break; |
| 920 |
case F64::CallImp: |
| 921 |
parseCallImport(ReturnType::F64, dst); |
| 922 |
break; |
| 923 |
case F64::Cond: |
| 924 |
parseCond(ReturnType::F64, dst); |
| 925 |
break; |
| 926 |
case F64::Comma: |
| 927 |
parseComma(ReturnType::F64, dst); |
| 928 |
break; |
| 929 |
case F64::FromS32: |
| 930 |
parseExprI32(dst); |
| 931 |
convertInt32ToDouble(temporaryAddress(dst), FPRInfo::fpRegT0); |
| 932 |
storeDouble(FPRInfo::fpRegT0, temporaryAddress(dst)); |
| 933 |
break; |
| 934 |
case F64::FromU32: |
| 935 |
case F64::FromF32: |
| 936 |
RELEASE_ASSERT_NOT_REACHED(); |
| 937 |
case F64::Neg: |
| 938 |
case F64::Abs: |
| 939 |
case F64::Ceil: |
| 940 |
case F64::Floor: |
| 941 |
case F64::Sqrt: |
| 942 |
case F64::Cos: |
| 943 |
case F64::Sin: |
| 944 |
case F64::Tan: |
| 945 |
case F64::ACos: |
| 946 |
case F64::ASin: |
| 947 |
case F64::ATan: |
| 948 |
case F64::Exp: |
| 949 |
case F64::Ln: |
| 950 |
parseUnaryOp(static_cast<uint8_t>(f64), ReturnType::F64, dst); |
| 951 |
break; |
| 952 |
case F64::Add: |
| 953 |
case F64::Sub: |
| 954 |
case F64::Mul: |
| 955 |
case F64::Div: |
| 956 |
case F64::ATan2: |
| 957 |
case F64::Pow: |
| 958 |
parseBinaryOp(static_cast<uint8_t>(f64), ReturnType::F64, dst); |
| 959 |
break; |
| 960 |
case F64::Mod: |
| 961 |
case F64::Min: |
| 962 |
case F64::Max: |
| 963 |
default: |
| 964 |
RELEASE_ASSERT_NOT_REACHED(); |
| 965 |
} |
| 966 |
} else { |
| 967 |
switch (f64WithImm) { |
| 968 |
case F64WithImm::LitPool: |
| 969 |
store64(TrustedImm64(reinterpretDoubleToInt64(m_module->m_constantF64s[imm])), temporaryAddress(dst)); |
| 970 |
break; |
| 971 |
case F64WithImm::GetLoc: |
| 972 |
// FIXME: loadDouble/storeDouble? |
| 973 |
load64(stackAddress(imm), GPRInfo::regT0); |
| 974 |
store64(GPRInfo::regT0, temporaryAddress(dst)); |
| 975 |
break; |
| 976 |
default: |
| 977 |
RELEASE_ASSERT_NOT_REACHED(); |
| 978 |
} |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
void JITCompiler::parseExprVoid(int dst) |
| 983 |
{ |
| 984 |
switch (m_module->m_reader.voidExpr()) { |
| 985 |
case Void::CallInt: |
| 986 |
parseCallInternal(ReturnType::Void, dst); |
| 987 |
break; |
| 988 |
case Void::CallInd: |
| 989 |
RELEASE_ASSERT_NOT_REACHED(); |
| 990 |
case Void::CallImp: |
| 991 |
parseCallImport(ReturnType::Void, dst); |
| 992 |
break; |
| 993 |
default: |
| 994 |
RELEASE_ASSERT_NOT_REACHED(); |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
static EncodedJSValue JSC_HOST_CALL getInternalFunction(ExecState*, Module* module, uint32_t functionIndex) |
| 999 |
{ |
| 1000 |
// TODO: Use these two lines? |
| 1001 |
// VM* vm = exec->vm(); |
| 1002 |
// NativeCallFrameTracer tracer(vm, exec); |
| 1003 |
|
| 1004 |
return JSValue::encode(module->function(functionIndex)); |
| 1005 |
} |
| 1006 |
|
| 1007 |
void JITCompiler::parseCallInternal(ReturnType returnType, int dst) |
| 1008 |
{ |
| 1009 |
uint32_t functionIndex = m_module->m_reader.immU32(); |
| 1010 |
const Signature& signature = m_module->m_signatures[m_module->m_funcSignatures[functionIndex]]; |
| 1011 |
const Vector<Type>& arguments = signature.arguments; |
| 1012 |
size_t argumentCount = arguments.size(); |
| 1013 |
// FIXME: returnType is redundant with signature.returnType |
| 1014 |
|
| 1015 |
parseCallArguments(arguments, dst); |
| 1016 |
|
| 1017 |
store32(TrustedImm32(argumentCount + 1), Address(stackPointerRegister, JSStack::ArgumentCount * static_cast<int>(sizeof(Register)) + PayloadOffset - sizeof(CallerFrameAndPC))); |
| 1018 |
|
| 1019 |
if (maxFrameExtentForSlowPathCall) |
| 1020 |
addPtr(TrustedImm32(-maxFrameExtentForSlowPathCall), stackPointerRegister); |
| 1021 |
setupArgumentsWithExecState(TrustedImmPtr(m_module), TrustedImm32(functionIndex)); |
| 1022 |
move(TrustedImmPtr(bitwise_cast<void*>(getInternalFunction)), GPRInfo::nonArgGPR0); |
| 1023 |
call(GPRInfo::nonArgGPR0); |
| 1024 |
if (maxFrameExtentForSlowPathCall) |
| 1025 |
addPtr(TrustedImm32(maxFrameExtentForSlowPathCall), stackPointerRegister); |
| 1026 |
|
| 1027 |
move(GPRInfo::returnValueGPR, GPRInfo::regT0); // regT0 holds callee. |
| 1028 |
store64(GPRInfo::regT0, Address(stackPointerRegister, JSStack::Callee * static_cast<int>(sizeof(Register)) - sizeof(CallerFrameAndPC))); |
| 1029 |
|
| 1030 |
callFunctionSlowCase(returnType, dst); |
| 1031 |
} |
| 1032 |
|
| 1033 |
static EncodedJSValue JSC_HOST_CALL getImportedFunction(ExecState* exec, Module* module, uint32_t funcImportIndex) |
| 1034 |
{ |
| 1035 |
// TODO: Use these two lines? |
| 1036 |
// VM* vm = exec->vm(); |
| 1037 |
// NativeCallFrameTracer tracer(vm, exec); |
| 1038 |
|
| 1039 |
const String& functionImportName = module->functionImportName(funcImportIndex); |
| 1040 |
|
| 1041 |
JSGlobalObject* globalObject = exec->lexicalGlobalObject(); |
| 1042 |
Identifier identifier = Identifier::fromString(&exec->vm(), functionImportName); |
| 1043 |
PropertySlot slot(globalObject); |
| 1044 |
|
| 1045 |
if (!globalObject->getPropertySlot(exec, identifier, slot)) { |
| 1046 |
// TODO: Do something! |
| 1047 |
fprintf(stderr, "Can't find a function named \"%s\".\n", functionImportName.utf8().data()); |
| 1048 |
abort(); |
| 1049 |
} |
| 1050 |
|
| 1051 |
JSValue value = slot.getValue(exec, identifier); |
| 1052 |
if (!value.isFunction()) { |
| 1053 |
fprintf(stderr, "\"%s\" is not a function.\n", functionImportName.utf8().data()); |
| 1054 |
abort(); |
| 1055 |
} |
| 1056 |
|
| 1057 |
return JSValue::encode(value); |
| 1058 |
} |
| 1059 |
|
| 1060 |
void JITCompiler::parseCallImport(ReturnType returnType, int dst) |
| 1061 |
{ |
| 1062 |
uint32_t funcImportSignatureIndex = m_module->m_reader.immU32(); |
| 1063 |
const Module::FuncImportSignature& funcImportSignature = m_module->m_funcImportSignatures[funcImportSignatureIndex]; |
| 1064 |
const Signature& signature = m_module->m_signatures[funcImportSignature.signatureIndex]; |
| 1065 |
const Vector<Type>& arguments = signature.arguments; |
| 1066 |
size_t argumentCount = arguments.size(); |
| 1067 |
// FIXME: returnType is redundant with signature.returnType |
| 1068 |
|
| 1069 |
parseCallArguments(arguments, dst); |
| 1070 |
|
| 1071 |
store32(TrustedImm32(argumentCount + 1), Address(stackPointerRegister, JSStack::ArgumentCount * static_cast<int>(sizeof(Register)) + PayloadOffset - sizeof(CallerFrameAndPC))); |
| 1072 |
|
| 1073 |
if (maxFrameExtentForSlowPathCall) |
| 1074 |
addPtr(TrustedImm32(-maxFrameExtentForSlowPathCall), stackPointerRegister); |
| 1075 |
setupArgumentsWithExecState(TrustedImmPtr(m_module), TrustedImm32(funcImportSignature.funcImportIndex)); |
| 1076 |
move(TrustedImmPtr(bitwise_cast<void*>(getImportedFunction)), GPRInfo::nonArgGPR0); |
| 1077 |
call(GPRInfo::nonArgGPR0); |
| 1078 |
if (maxFrameExtentForSlowPathCall) |
| 1079 |
addPtr(TrustedImm32(maxFrameExtentForSlowPathCall), stackPointerRegister); |
| 1080 |
|
| 1081 |
move(GPRInfo::returnValueGPR, GPRInfo::regT0); // regT0 holds callee. |
| 1082 |
store64(GPRInfo::regT0, Address(stackPointerRegister, JSStack::Callee * static_cast<int>(sizeof(Register)) - sizeof(CallerFrameAndPC))); |
| 1083 |
|
| 1084 |
callFunctionSlowCase(returnType, dst); |
| 1085 |
} |
| 1086 |
|
| 1087 |
void JITCompiler::callFunctionSlowCase(ReturnType returnType, int dst) |
| 1088 |
{ |
| 1089 |
ThunkGenerator generator = linkThunkGeneratorFor(CodeForCall, RegisterPreservationNotRequired); |
| 1090 |
|
| 1091 |
move(TrustedImmPtr(nullptr), GPRInfo::regT2); // TODO: Use CallLinkInfo |
| 1092 |
|
| 1093 |
CodePtr ptr = m_vm->getCTIStub(generator).code(); |
| 1094 |
|
| 1095 |
move(TrustedImmPtr(ptr.executableAddress()), GPRInfo::nonArgGPR0); |
| 1096 |
|
| 1097 |
call(GPRInfo::nonArgGPR0); |
| 1098 |
|
| 1099 |
addPtr(TrustedImm32(-m_stackHeight), GPRInfo::callFrameRegister, stackPointerRegister); // FIXME: hacky |
| 1100 |
|
| 1101 |
// FIXME: Can we really use returnValueGPR? |
| 1102 |
switch (returnType) { |
| 1103 |
case ReturnType::I32: |
| 1104 |
store32(GPRInfo::returnValueGPR, temporaryAddress(dst)); |
| 1105 |
break; |
| 1106 |
case ReturnType::F64: |
| 1107 |
unboxDoubleWithoutAssertions(GPRInfo::returnValueGPR, FPRInfo::fpRegT0); |
| 1108 |
storeDouble(FPRInfo::fpRegT0, temporaryAddress(dst)); |
| 1109 |
break; |
| 1110 |
case ReturnType::Void: |
| 1111 |
break; // TODO: Sure? |
| 1112 |
default: |
| 1113 |
ASSERT_NOT_REACHED(); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
void JITCompiler::parseCallArguments(const Vector<Type>& arguments, int dst) |
| 1118 |
{ |
| 1119 |
size_t argumentCount = arguments.size(); |
| 1120 |
size_t offset = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), dst + argumentCount + m_currentLocalTypes.size()) - m_currentLocalTypes.size(); // FIXME: Better name |
| 1121 |
|
| 1122 |
for (size_t i = 0; i < argumentCount; ++i) { |
| 1123 |
switch (arguments[i]) { |
| 1124 |
case Type::I32: |
| 1125 |
parseExpr(ReturnType::I32, offset); |
| 1126 |
load32(temporaryAddress(offset), GPRInfo::regT0); |
| 1127 |
or64(GPRInfo::tagTypeNumberRegister, GPRInfo::regT0); |
| 1128 |
store64(GPRInfo::regT0, temporaryAddress(offset - i - 1)); |
| 1129 |
break; |
| 1130 |
case Type::F64: |
| 1131 |
parseExpr(ReturnType::F64, offset); |
| 1132 |
loadDouble(temporaryAddress(offset), FPRInfo::fpRegT0); |
| 1133 |
boxDouble(FPRInfo::fpRegT0, GPRInfo::regT0); |
| 1134 |
store64(GPRInfo::regT0, temporaryAddress(offset - i - 1)); |
| 1135 |
break; |
| 1136 |
default: |
| 1137 |
ASSERT_NOT_REACHED(); |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
store64(TrustedImm64(JSValue::encode(jsUndefined())), temporaryAddress(offset)); |
| 1142 |
|
| 1143 |
addPtr(TrustedImm32(-(offset + m_currentLocalTypes.size() + 3) * 8 - 8), GPRInfo::callFrameRegister, stackPointerRegister); // FIXME: hacky |
| 1144 |
} |
| 1145 |
|
| 1146 |
void JITCompiler::callDoubleFunction(double (*function)(double), FPRegisterID src, FPRegisterID dst) |
| 1147 |
{ |
| 1148 |
if (maxFrameExtentForSlowPathCall) |
| 1149 |
addPtr(TrustedImm32(-maxFrameExtentForSlowPathCall), stackPointerRegister); |
| 1150 |
setupArguments(src); |
| 1151 |
move(TrustedImmPtr(bitwise_cast<void*>(function)), GPRInfo::nonArgGPR0); |
| 1152 |
call(GPRInfo::nonArgGPR0); |
| 1153 |
if (maxFrameExtentForSlowPathCall) |
| 1154 |
addPtr(TrustedImm32(maxFrameExtentForSlowPathCall), stackPointerRegister); |
| 1155 |
|
| 1156 |
// FIXME: only works on x86 |
| 1157 |
moveDouble(FPRInfo::returnValueFPR, dst); |
| 1158 |
} |
| 1159 |
|
| 1160 |
void JITCompiler::callDoubleFunction(double (*function)(double, double), FPRegisterID src1, FPRegisterID src2, FPRegisterID dst) |
| 1161 |
{ |
| 1162 |
if (maxFrameExtentForSlowPathCall) |
| 1163 |
addPtr(TrustedImm32(-maxFrameExtentForSlowPathCall), stackPointerRegister); |
| 1164 |
setupArguments(src1, src2); |
| 1165 |
move(TrustedImmPtr(bitwise_cast<void*>(function)), GPRInfo::nonArgGPR0); |
| 1166 |
call(GPRInfo::nonArgGPR0); |
| 1167 |
if (maxFrameExtentForSlowPathCall) |
| 1168 |
addPtr(TrustedImm32(maxFrameExtentForSlowPathCall), stackPointerRegister); |
| 1169 |
|
| 1170 |
// FIXME: only works on x86 |
| 1171 |
moveDouble(FPRInfo::returnValueFPR, dst); |
| 1172 |
} |
| 1173 |
|
| 1174 |
void JITCompiler::parseUnaryOp(uint8_t op, ReturnType returnType, int dst) |
| 1175 |
{ |
| 1176 |
parseExpr(returnType, dst); |
| 1177 |
switch (returnType) { |
| 1178 |
case ReturnType::I32: |
| 1179 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 1180 |
switch (static_cast<I32>(op)) { |
| 1181 |
case I32::Neg: |
| 1182 |
neg32(GPRInfo::regT0); |
| 1183 |
break; |
| 1184 |
case I32::BitNot: |
| 1185 |
xor32(TrustedImm32(-1), GPRInfo::regT0); |
| 1186 |
break; |
| 1187 |
case I32::Clz: |
| 1188 |
countLeadingZeros32(GPRInfo::regT0, GPRInfo::regT0); |
| 1189 |
break; |
| 1190 |
case I32::Abs: { |
| 1191 |
Jump end = branchTest32(PositiveOrZero, GPRInfo::regT0); |
| 1192 |
neg32(GPRInfo::regT0); |
| 1193 |
end.link(this); |
| 1194 |
break; |
| 1195 |
} |
| 1196 |
default: |
| 1197 |
ASSERT_NOT_REACHED(); |
| 1198 |
} |
| 1199 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 1200 |
break; |
| 1201 |
case ReturnType::F64: |
| 1202 |
loadDouble(temporaryAddress(dst), FPRInfo::fpRegT0); |
| 1203 |
switch (static_cast<F64>(op)) { |
| 1204 |
case F64::Neg: |
| 1205 |
negateDouble(FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 1206 |
break; |
| 1207 |
case F64::Abs: |
| 1208 |
absDouble(FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 1209 |
break; |
| 1210 |
case F64::Ceil: |
| 1211 |
callDoubleFunction(ceil, FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 1212 |
break; |
| 1213 |
case F64::Floor: |
| 1214 |
callDoubleFunction(floor, FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 1215 |
break; |
| 1216 |
case F64::Sqrt: |
| 1217 |
sqrtDouble(FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 1218 |
break; |
| 1219 |
case F64::Cos: |
| 1220 |
callDoubleFunction(cos, FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 1221 |
break; |
| 1222 |
case F64::Sin: |
| 1223 |
callDoubleFunction(sin, FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 1224 |
break; |
| 1225 |
case F64::Tan: |
| 1226 |
callDoubleFunction(tan, FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 1227 |
break; |
| 1228 |
case F64::ACos: |
| 1229 |
callDoubleFunction(acos, FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 1230 |
break; |
| 1231 |
case F64::ASin: |
| 1232 |
callDoubleFunction(asin, FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 1233 |
break; |
| 1234 |
case F64::ATan: |
| 1235 |
callDoubleFunction(atan, FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 1236 |
break; |
| 1237 |
case F64::Exp: |
| 1238 |
callDoubleFunction(exp, FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 1239 |
break; |
| 1240 |
case F64::Ln: |
| 1241 |
callDoubleFunction(log, FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 1242 |
break; |
| 1243 |
default: |
| 1244 |
ASSERT_NOT_REACHED(); |
| 1245 |
} |
| 1246 |
storeDouble(FPRInfo::fpRegT1, temporaryAddress(dst)); |
| 1247 |
break; |
| 1248 |
default: |
| 1249 |
RELEASE_ASSERT_NOT_REACHED(); |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
void JITCompiler::parseBinaryOp(uint8_t op, ReturnType returnType, int dst) |
| 1254 |
{ |
| 1255 |
parseExpr(returnType, dst); |
| 1256 |
parseExpr(returnType, dst + 1); |
| 1257 |
switch (returnType) { |
| 1258 |
case ReturnType::I32: |
| 1259 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 1260 |
load32(temporaryAddress(dst + 1), GPRInfo::regT1); |
| 1261 |
switch (static_cast<I32>(op)) { |
| 1262 |
case I32::Add: |
| 1263 |
add32(GPRInfo::regT1, GPRInfo::regT0); |
| 1264 |
break; |
| 1265 |
case I32::Sub: |
| 1266 |
sub32(GPRInfo::regT1, GPRInfo::regT0); |
| 1267 |
break; |
| 1268 |
case I32::Mul: |
| 1269 |
mul32(GPRInfo::regT1, GPRInfo::regT0); |
| 1270 |
break; |
| 1271 |
case I32::SDiv: |
| 1272 |
move(GPRInfo::regT1, GPRInfo::regT2); |
| 1273 |
m_assembler.cdq(); |
| 1274 |
m_assembler.idivl_r(GPRInfo::regT2); |
| 1275 |
break; |
| 1276 |
case I32::SMod: |
| 1277 |
move(GPRInfo::regT1, GPRInfo::regT2); |
| 1278 |
m_assembler.cdq(); |
| 1279 |
m_assembler.idivl_r(GPRInfo::regT2); |
| 1280 |
move(GPRInfo::regT1, GPRInfo::regT0); |
| 1281 |
break; |
| 1282 |
case I32::BitOr: |
| 1283 |
or32(GPRInfo::regT1, GPRInfo::regT0); |
| 1284 |
break; |
| 1285 |
case I32::BitAnd: |
| 1286 |
and32(GPRInfo::regT1, GPRInfo::regT0); |
| 1287 |
break; |
| 1288 |
case I32::BitXor: |
| 1289 |
xor32(GPRInfo::regT1, GPRInfo::regT0); |
| 1290 |
break; |
| 1291 |
case I32::Lsh: |
| 1292 |
lshift32(GPRInfo::regT1, GPRInfo::regT0); |
| 1293 |
break; |
| 1294 |
case I32::ArithRsh: |
| 1295 |
rshift32(GPRInfo::regT1, GPRInfo::regT0); |
| 1296 |
break; |
| 1297 |
case I32::LogicRsh: |
| 1298 |
urshift32(GPRInfo::regT1, GPRInfo::regT0); |
| 1299 |
break; |
| 1300 |
default: |
| 1301 |
ASSERT_NOT_REACHED(); |
| 1302 |
} |
| 1303 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 1304 |
break; |
| 1305 |
case ReturnType::F64: |
| 1306 |
loadDouble(temporaryAddress(dst), FPRInfo::fpRegT0); |
| 1307 |
loadDouble(temporaryAddress(dst + 1), FPRInfo::fpRegT1); |
| 1308 |
switch (static_cast<F64>(op)) { |
| 1309 |
case F64::Add: |
| 1310 |
addDouble(FPRInfo::fpRegT1, FPRInfo::fpRegT0); |
| 1311 |
break; |
| 1312 |
case F64::Sub: |
| 1313 |
subDouble(FPRInfo::fpRegT1, FPRInfo::fpRegT0); |
| 1314 |
break; |
| 1315 |
case F64::Mul: |
| 1316 |
mulDouble(FPRInfo::fpRegT1, FPRInfo::fpRegT0); |
| 1317 |
break; |
| 1318 |
case F64::Div: |
| 1319 |
divDouble(FPRInfo::fpRegT1, FPRInfo::fpRegT0); |
| 1320 |
break; |
| 1321 |
case F64::ATan2: |
| 1322 |
callDoubleFunction(atan2, FPRInfo::fpRegT0, FPRInfo::fpRegT1, FPRInfo::fpRegT0); |
| 1323 |
break; |
| 1324 |
case F64::Pow: |
| 1325 |
callDoubleFunction(pow, FPRInfo::fpRegT0, FPRInfo::fpRegT1, FPRInfo::fpRegT0); |
| 1326 |
break; |
| 1327 |
default: |
| 1328 |
ASSERT_NOT_REACHED(); |
| 1329 |
} |
| 1330 |
storeDouble(FPRInfo::fpRegT0, temporaryAddress(dst)); |
| 1331 |
break; |
| 1332 |
default: |
| 1333 |
RELEASE_ASSERT_NOT_REACHED(); |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
void JITCompiler::parseRelationalOpI32(RelationalCondition condition, int dst) |
| 1338 |
{ |
| 1339 |
parseExpr(ReturnType::I32, dst); |
| 1340 |
parseExpr(ReturnType::I32, dst + 1); |
| 1341 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 1342 |
load32(temporaryAddress(dst + 1), GPRInfo::regT1); |
| 1343 |
compare32(condition, GPRInfo::regT0, GPRInfo::regT1, GPRInfo::regT0); |
| 1344 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 1345 |
} |
| 1346 |
|
| 1347 |
void JITCompiler::parseRelationalOpF64(DoubleCondition condition, int dst) |
| 1348 |
{ |
| 1349 |
parseExpr(ReturnType::F64, dst); |
| 1350 |
parseExpr(ReturnType::F64, dst + 1); |
| 1351 |
loadDouble(temporaryAddress(dst), FPRInfo::fpRegT0); |
| 1352 |
loadDouble(temporaryAddress(dst + 1), FPRInfo::fpRegT1); |
| 1353 |
Jump trueCase = branchDouble(condition, FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 1354 |
store32(TrustedImm32(0), temporaryAddress(dst)); |
| 1355 |
Jump end = jump(); |
| 1356 |
trueCase.link(this); |
| 1357 |
store32(TrustedImm32(1), temporaryAddress(dst)); |
| 1358 |
end.link(this); |
| 1359 |
} |
| 1360 |
|
| 1361 |
void JITCompiler::parseComma(ReturnType returnType, int dst) |
| 1362 |
{ |
| 1363 |
parseExpr(m_module->m_reader.returnType(), dst); |
| 1364 |
parseExpr(returnType, dst); |
| 1365 |
} |
| 1366 |
|
| 1367 |
void JITCompiler::parseCond(ReturnType returnType, int dst) |
| 1368 |
{ |
| 1369 |
parseExpr(ReturnType::I32, dst); |
| 1370 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 1371 |
Jump els = branchTest32(Zero, GPRInfo::regT0); |
| 1372 |
parseExpr(returnType, dst); |
| 1373 |
Jump end = jump(); |
| 1374 |
els.link(this); |
| 1375 |
parseExpr(returnType, dst); |
| 1376 |
end.link(this); |
| 1377 |
} |
| 1378 |
|
| 1379 |
void JITCompiler::parseMinMaxI32(RelationalCondition condition, int dst) |
| 1380 |
{ |
| 1381 |
uint32_t numberOfArguments = m_module->m_reader.immU32(); |
| 1382 |
RELEASE_ASSERT(numberOfArguments >= 1); |
| 1383 |
parseExpr(ReturnType::I32, dst); |
| 1384 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 1385 |
for (uint32_t i = 1; i < numberOfArguments; ++i) { |
| 1386 |
parseExpr(ReturnType::I32, dst); |
| 1387 |
load32(temporaryAddress(dst), GPRInfo::regT1); |
| 1388 |
Jump end = branch32(condition, GPRInfo::regT0, GPRInfo::regT1); |
| 1389 |
move(GPRInfo::regT1, GPRInfo::regT0); |
| 1390 |
end.link(this); |
| 1391 |
} |
| 1392 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 1393 |
} |
| 1394 |
|
| 1395 |
} } // namespace JSC::Wasm |