|
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 |
|
| 48 |
namespace JSC { |
| 49 |
|
| 50 |
namespace Wasm { |
| 51 |
|
| 52 |
class JumpScope { |
| 53 |
public: |
| 54 |
JumpScope(MacroAssembler* macroAssembler) |
| 55 |
: m_macroAssembler(macroAssembler) |
| 56 |
, m_linked(false) |
| 57 |
{ |
| 58 |
} |
| 59 |
|
| 60 |
JumpScope(MacroAssembler* macroAssembler, const MacroAssembler::Label& label) |
| 61 |
: m_macroAssembler(macroAssembler) |
| 62 |
, m_linked(true) |
| 63 |
, m_label(label) |
| 64 |
{ |
| 65 |
} |
| 66 |
|
| 67 |
void jump() |
| 68 |
{ |
| 69 |
if (m_linked) |
| 70 |
m_macroAssembler->jump(m_label); |
| 71 |
else |
| 72 |
m_jumpList.append(m_macroAssembler->jump()); |
| 73 |
} |
| 74 |
|
| 75 |
void link() |
| 76 |
{ |
| 77 |
ASSERT(!m_linked); |
| 78 |
m_jumpList.link(m_macroAssembler); |
| 79 |
} |
| 80 |
|
| 81 |
private: |
| 82 |
MacroAssembler* m_macroAssembler; |
| 83 |
bool m_linked; |
| 84 |
MacroAssembler::Label m_label; |
| 85 |
MacroAssembler::JumpList m_jumpList; |
| 86 |
}; |
| 87 |
|
| 88 |
RefPtr<JITCode> JITCompiler::compileFunction(VM& vm, size_t functionIndex) |
| 89 |
{ |
| 90 |
emitFunctionPrologue(); |
| 91 |
allocateSpaceOnStack(); |
| 92 |
|
| 93 |
const Signature& signature = m_module->m_signatures[m_module->m_funcSignatures[functionIndex]]; |
| 94 |
m_currentReturnType = signature.ret; |
| 95 |
m_currentLocalTypes.clear(); |
| 96 |
parseArguments(signature); |
| 97 |
parseVars(); |
| 98 |
parseStmtList(); |
| 99 |
|
| 100 |
LinkBuffer patchBuffer(vm, *this, 0, JITCompilationMustSucceed); |
| 101 |
|
| 102 |
for (auto iter = m_calls.begin(); iter != m_calls.end(); ++iter) |
| 103 |
patchBuffer.link(iter->first, FunctionPtr(iter->second)); |
| 104 |
|
| 105 |
MacroAssemblerCodePtr withArityCheck; |
| 106 |
CodeRef result = FINALIZE_CODE(patchBuffer, ("Baseline JIT code for WebAssembly")); |
| 107 |
return adoptRef(new DirectJITCode(result, withArityCheck, JITCode::BaselineJIT)); |
| 108 |
} |
| 109 |
|
| 110 |
void JITCompiler::allocateSpaceOnStack() |
| 111 |
{ |
| 112 |
m_stackHeight = 32; // FIXME: calculate the actual height |
| 113 |
addPtr(TrustedImm32(-m_stackHeight), stackPointerRegister); |
| 114 |
} |
| 115 |
|
| 116 |
void JITCompiler::deallocateSpaceOnStack() |
| 117 |
{ |
| 118 |
addPtr(TrustedImm32(m_stackHeight), stackPointerRegister); |
| 119 |
} |
| 120 |
|
| 121 |
void JITCompiler::parseArguments(const Signature& signature) |
| 122 |
{ |
| 123 |
size_t argumentCount = signature.arguments.size(); |
| 124 |
for (uint32_t argIndex = 0; argIndex < argumentCount; ++argIndex) { |
| 125 |
Type type = signature.arguments[argIndex]; |
| 126 |
m_currentLocalTypes.append(type); |
| 127 |
switch (type) { |
| 128 |
case Type::I32: |
| 129 |
case Type::F32: |
| 130 |
case Type::F64: |
| 131 |
break; |
| 132 |
default: |
| 133 |
RELEASE_ASSERT_NOT_REACHED(); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void JITCompiler::parseVars() |
| 139 |
{ |
| 140 |
uint32_t numberOfI32Vars = 0; |
| 141 |
uint32_t numberOfF32Vars = 0; |
| 142 |
uint32_t numberOfF64Vars = 0; |
| 143 |
|
| 144 |
VarTypes varTypes; |
| 145 |
VarTypesWithImm varTypesWithImm; |
| 146 |
uint8_t imm; |
| 147 |
if (m_module->m_read.code(&varTypes, &varTypesWithImm, &imm)) { |
| 148 |
if (varTypes & VarTypes::I32) |
| 149 |
numberOfI32Vars = m_module->m_read.immU32(); |
| 150 |
if (varTypes & VarTypes::F32) |
| 151 |
numberOfF32Vars = m_module->m_read.immU32(); |
| 152 |
if (varTypes & VarTypes::F64) |
| 153 |
numberOfF64Vars = m_module->m_read.immU32(); |
| 154 |
} else |
| 155 |
numberOfI32Vars = imm; |
| 156 |
|
| 157 |
uint32_t numberOfVars = numberOfI32Vars + numberOfF32Vars + numberOfF64Vars; |
| 158 |
if (numberOfVars > 0) { |
| 159 |
uint32_t localIndex = m_currentLocalTypes.size(); |
| 160 |
for (uint32_t i = 0; i < numberOfI32Vars; ++i) { |
| 161 |
m_currentLocalTypes.append(Type::I32); |
| 162 |
store32(TrustedImm32(0), stackAddress(localIndex++)); |
| 163 |
} |
| 164 |
for (uint32_t i = 0; i < numberOfF32Vars; ++i) { |
| 165 |
m_currentLocalTypes.append(Type::F32); |
| 166 |
store32(TrustedImm32(0), stackAddress(localIndex++)); |
| 167 |
} |
| 168 |
for (uint32_t i = 0; i < numberOfF64Vars; ++i) { |
| 169 |
m_currentLocalTypes.append(Type::F64); |
| 170 |
store64(TrustedImm64(0), stackAddress(localIndex++)); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
void JITCompiler::parseStmt() |
| 176 |
{ |
| 177 |
Stmt stmt; |
| 178 |
StmtWithImm stmtWithImm; |
| 179 |
uint8_t imm; |
| 180 |
if (m_module->m_read.code(&stmt, &stmtWithImm, &imm)) { |
| 181 |
switch (stmt) { |
| 182 |
case Stmt::SetLoc: |
| 183 |
parseSetLocal(0); |
| 184 |
break; |
| 185 |
case Stmt::SetGlo: |
| 186 |
case Stmt::I32Store8: |
| 187 |
case Stmt::I32StoreOff8: |
| 188 |
case Stmt::I32Store16: |
| 189 |
case Stmt::I32StoreOff16: |
| 190 |
case Stmt::I32Store32: |
| 191 |
case Stmt::I32StoreOff32: |
| 192 |
case Stmt::F32Store: |
| 193 |
case Stmt::F32StoreOff: |
| 194 |
case Stmt::F64Store: |
| 195 |
case Stmt::F64StoreOff: |
| 196 |
case Stmt::CallInt: |
| 197 |
case Stmt::CallInd: |
| 198 |
case Stmt::CallImp: |
| 199 |
RELEASE_ASSERT_NOT_REACHED(); |
| 200 |
case Stmt::Ret: |
| 201 |
parseReturnStmt(); |
| 202 |
return; |
| 203 |
case Stmt::Block: |
| 204 |
parseBlockStmt(); |
| 205 |
return; |
| 206 |
case Stmt::IfThen: |
| 207 |
parseIfStmt(); |
| 208 |
return; |
| 209 |
case Stmt::IfElse: |
| 210 |
parseIfElseStmt(); |
| 211 |
return; |
| 212 |
case Stmt::While: |
| 213 |
parseWhileStmt(); |
| 214 |
return; |
| 215 |
case Stmt::Do: |
| 216 |
parseDoStmt(); |
| 217 |
return; |
| 218 |
case Stmt::Label: |
| 219 |
parseLabelStmt(); |
| 220 |
return; |
| 221 |
case Stmt::Break: |
| 222 |
parseBreakStmt(); |
| 223 |
return; |
| 224 |
case Stmt::BreakLabel: |
| 225 |
parseBreakLabelStmt(); |
| 226 |
return; |
| 227 |
case Stmt::Continue: |
| 228 |
parseContinueStmt(); |
| 229 |
break; |
| 230 |
case Stmt::ContinueLabel: |
| 231 |
parseContinueLabelStmt(); |
| 232 |
return; |
| 233 |
case Stmt::Switch: |
| 234 |
parseSwitchStmt(); |
| 235 |
break; |
| 236 |
default: |
| 237 |
RELEASE_ASSERT_NOT_REACHED(); |
| 238 |
} |
| 239 |
} else { |
| 240 |
switch (stmtWithImm) { |
| 241 |
case StmtWithImm::SetLoc: |
| 242 |
parseSetLocal(imm, 0); |
| 243 |
break; |
| 244 |
case StmtWithImm::SetGlo: |
| 245 |
default: |
| 246 |
RELEASE_ASSERT_NOT_REACHED(); |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
void JITCompiler::parseGetLocal(int dst) |
| 252 |
{ |
| 253 |
uint32_t imm = m_module->m_read.immU32(); |
| 254 |
switch (m_currentLocalTypes[imm]) { |
| 255 |
case Type::I32: |
| 256 |
case Type::F32: // FIXME: loadFloat/storeFloat? |
| 257 |
load32(stackAddress(imm), GPRInfo::regT0); |
| 258 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 259 |
break; |
| 260 |
case Type::F64: // FIXME: loadDouble/storeDouble? |
| 261 |
load64(stackAddress(imm), GPRInfo::regT0); |
| 262 |
store64(GPRInfo::regT0, temporaryAddress(dst)); |
| 263 |
break; |
| 264 |
default: |
| 265 |
RELEASE_ASSERT_NOT_REACHED(); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
void JITCompiler::parseSetLocal(uint32_t imm, int dst) |
| 270 |
{ |
| 271 |
Type type = m_currentLocalTypes[imm]; |
| 272 |
parseExpr(RType(type), dst); |
| 273 |
switch (type) { |
| 274 |
case Type::I32: |
| 275 |
case Type::F32: |
| 276 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 277 |
store32(GPRInfo::regT0, stackAddress(imm)); |
| 278 |
break; |
| 279 |
case Type::F64: |
| 280 |
load64(temporaryAddress(dst), GPRInfo::regT0); |
| 281 |
store64(GPRInfo::regT0, stackAddress(imm)); |
| 282 |
break; |
| 283 |
default: |
| 284 |
RELEASE_ASSERT_NOT_REACHED(); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
void JITCompiler::parseSetLocal(int dst) |
| 289 |
{ |
| 290 |
parseSetLocal(m_module->m_read.immU32(), dst); |
| 291 |
} |
| 292 |
|
| 293 |
void JITCompiler::parseReturnStmt() |
| 294 |
{ |
| 295 |
switch (m_currentReturnType) { |
| 296 |
case RType::I32: |
| 297 |
parseExpr(RType::I32, 0); |
| 298 |
load32(temporaryAddress(0), GPRInfo::returnValueGPR); |
| 299 |
boxInt52(GPRInfo::returnValueGPR, GPRInfo::returnValueGPR, GPRInfo::regT0, FPRInfo::fpRegT0); |
| 300 |
break; |
| 301 |
case RType::F32: |
| 302 |
parseExpr(RType::F32, 0); |
| 303 |
loadDouble(temporaryAddress(0), FPRInfo::fpRegT0); // FIXME: hacky! This loads 64 bits! |
| 304 |
convertFloatToDouble(FPRInfo::fpRegT0, FPRInfo::fpRegT0); |
| 305 |
boxDouble(FPRInfo::fpRegT0, GPRInfo::returnValueGPR); |
| 306 |
break; |
| 307 |
case RType::F64: |
| 308 |
parseExpr(RType::F64, 0); |
| 309 |
loadDouble(temporaryAddress(0), FPRInfo::fpRegT0); |
| 310 |
boxDouble(FPRInfo::fpRegT0, GPRInfo::returnValueGPR); |
| 311 |
break; |
| 312 |
case RType::Void: |
| 313 |
break; |
| 314 |
} |
| 315 |
|
| 316 |
deallocateSpaceOnStack(); |
| 317 |
emitFunctionEpilogue(); |
| 318 |
ret(); |
| 319 |
} |
| 320 |
|
| 321 |
void JITCompiler::parseStmtList() |
| 322 |
{ |
| 323 |
uint32_t numberOfStmts = m_module->m_read.immU32(); |
| 324 |
for (uint32_t i = 0; i < numberOfStmts; ++i) |
| 325 |
parseStmt(); |
| 326 |
} |
| 327 |
|
| 328 |
void JITCompiler::parseBlockStmt() |
| 329 |
{ |
| 330 |
parseStmtList(); |
| 331 |
} |
| 332 |
|
| 333 |
void JITCompiler::parseIfStmt() |
| 334 |
{ |
| 335 |
parseExpr(RType::I32, 0); |
| 336 |
load32(temporaryAddress(0), GPRInfo::regT0); |
| 337 |
Jump end = branchTest32(Zero, GPRInfo::regT0); |
| 338 |
parseStmt(); |
| 339 |
end.link(this); |
| 340 |
} |
| 341 |
|
| 342 |
void JITCompiler::parseIfElseStmt() |
| 343 |
{ |
| 344 |
parseExpr(RType::I32, 0); |
| 345 |
load32(temporaryAddress(0), GPRInfo::regT0); |
| 346 |
Jump els = branchTest32(Zero, GPRInfo::regT0); |
| 347 |
parseStmt(); |
| 348 |
Jump end = jump(); |
| 349 |
els.link(this); |
| 350 |
parseStmt(); |
| 351 |
end.link(this); |
| 352 |
} |
| 353 |
|
| 354 |
void JITCompiler::parseWhileStmt() |
| 355 |
{ |
| 356 |
JumpScope continueJumpScope(this, label()); |
| 357 |
pushContinueJumpScope(&continueJumpScope); |
| 358 |
|
| 359 |
JumpList breakJumpList; |
| 360 |
pushBreakJumpList(&breakJumpList); |
| 361 |
|
| 362 |
parseExpr(RType::I32, 0); |
| 363 |
load32(temporaryAddress(0), GPRInfo::regT0); |
| 364 |
Jump end = branchTest32(Zero, GPRInfo::regT0); |
| 365 |
parseStmt(); |
| 366 |
continueJumpScope.jump(); |
| 367 |
end.link(this); |
| 368 |
breakJumpList.link(this); |
| 369 |
|
| 370 |
popBreakJumpList(); |
| 371 |
|
| 372 |
popContinueJumpScope(); |
| 373 |
} |
| 374 |
|
| 375 |
void JITCompiler::parseDoStmt() |
| 376 |
{ |
| 377 |
JumpScope continueJumpScope(this); |
| 378 |
pushContinueJumpScope(&continueJumpScope); |
| 379 |
|
| 380 |
JumpList breakJumpList; |
| 381 |
pushBreakJumpList(&breakJumpList); |
| 382 |
|
| 383 |
Label start = label(); |
| 384 |
parseStmt(); |
| 385 |
continueJumpScope.link(); |
| 386 |
parseExpr(RType::I32, 0); |
| 387 |
load32(temporaryAddress(0), GPRInfo::regT0); |
| 388 |
Jump jumpToStart = branchTest32(NonZero, GPRInfo::regT0); |
| 389 |
jumpToStart.linkTo(start, this); |
| 390 |
breakJumpList.link(this); |
| 391 |
|
| 392 |
popBreakJumpList(); |
| 393 |
|
| 394 |
popContinueJumpScope(); |
| 395 |
} |
| 396 |
|
| 397 |
void JITCompiler::parseLabelStmt() |
| 398 |
{ |
| 399 |
Label continueLabel = label(); |
| 400 |
JumpList breakJumpList; |
| 401 |
pushContinueLabelLabel(&continueLabel); |
| 402 |
pushBreakLabelJumpList(&breakJumpList); |
| 403 |
parseStmt(); |
| 404 |
breakJumpList.link(this); |
| 405 |
popBreakLabelJumpList(); |
| 406 |
popContinueLabelLabel(); |
| 407 |
} |
| 408 |
|
| 409 |
void JITCompiler::parseBreakStmt() |
| 410 |
{ |
| 411 |
topBreakJumpList()->append(jump()); |
| 412 |
} |
| 413 |
|
| 414 |
void JITCompiler::parseBreakLabelStmt() |
| 415 |
{ |
| 416 |
uint32_t index = m_module->m_read.immU32(); |
| 417 |
breakLabelJumpList(index)->append(jump()); |
| 418 |
} |
| 419 |
|
| 420 |
void JITCompiler::parseContinueStmt() |
| 421 |
{ |
| 422 |
topContinueJumpScope()->jump(); |
| 423 |
} |
| 424 |
|
| 425 |
void JITCompiler::parseContinueLabelStmt() |
| 426 |
{ |
| 427 |
uint32_t index = m_module->m_read.immU32(); |
| 428 |
jump(*continueLabelLabel(index)); |
| 429 |
} |
| 430 |
|
| 431 |
void JITCompiler::parseSwitchStmt() |
| 432 |
{ |
| 433 |
JumpList breakJumpList; |
| 434 |
pushBreakJumpList(&breakJumpList); |
| 435 |
|
| 436 |
uint32_t numberOfCases = m_module->m_read.immU32(); |
| 437 |
parseExpr(RType::I32, 0); |
| 438 |
load32(temporaryAddress(0), GPRInfo::regT0); |
| 439 |
|
| 440 |
Jump table = jump(); |
| 441 |
|
| 442 |
Vector<Label> labels; |
| 443 |
Vector<int32_t> imms; |
| 444 |
Label defaultLabel; |
| 445 |
bool hasDefault = false; |
| 446 |
|
| 447 |
for (uint32_t i = 0; i < numberOfCases; ++i) { |
| 448 |
switch (m_module->m_read.switchCase()) { |
| 449 |
case SwitchCase::Case0: |
| 450 |
labels.append(label()); |
| 451 |
imms.append(m_module->m_read.immS32()); |
| 452 |
break; |
| 453 |
case SwitchCase::Case1: |
| 454 |
labels.append(label()); |
| 455 |
imms.append(m_module->m_read.immS32()); |
| 456 |
parseStmt(); |
| 457 |
break; |
| 458 |
case SwitchCase::CaseN: |
| 459 |
labels.append(label()); |
| 460 |
imms.append(m_module->m_read.immS32()); |
| 461 |
parseStmtList(); |
| 462 |
break; |
| 463 |
case SwitchCase::Default0: |
| 464 |
defaultLabel = label(); |
| 465 |
hasDefault = true; |
| 466 |
break; |
| 467 |
case SwitchCase::Default1: |
| 468 |
defaultLabel = label(); |
| 469 |
hasDefault = true; |
| 470 |
parseStmt(); |
| 471 |
break; |
| 472 |
case SwitchCase::DefaultN: |
| 473 |
defaultLabel = label(); |
| 474 |
hasDefault = true; |
| 475 |
parseStmtList(); |
| 476 |
break; |
| 477 |
default: |
| 478 |
RELEASE_ASSERT_NOT_REACHED(); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
Jump end = jump(); |
| 483 |
table.link(this); |
| 484 |
|
| 485 |
for (size_t i = 0; i < labels.size(); ++i) { |
| 486 |
Jump jmp = branch32(Equal, GPRInfo::regT0, TrustedImm32(imms[i])); |
| 487 |
jmp.linkTo(labels[i], this); |
| 488 |
} |
| 489 |
if (hasDefault) |
| 490 |
jump(defaultLabel); |
| 491 |
|
| 492 |
end.link(this); |
| 493 |
|
| 494 |
breakJumpList.link(this); |
| 495 |
popBreakJumpList(); |
| 496 |
} |
| 497 |
|
| 498 |
void JITCompiler::parseExpr(RType rType, int dst) |
| 499 |
{ |
| 500 |
switch (rType) { |
| 501 |
case RType::I32: |
| 502 |
parseExprI32(dst); |
| 503 |
break; |
| 504 |
case RType::F64: |
| 505 |
parseExprF64(dst); |
| 506 |
break; |
| 507 |
default: |
| 508 |
RELEASE_ASSERT_NOT_REACHED(); |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
void JITCompiler::parseExprI32(int dst) |
| 513 |
{ |
| 514 |
I32 i32; |
| 515 |
I32WithImm i32WithImm; |
| 516 |
uint8_t imm; |
| 517 |
|
| 518 |
if (m_module->m_read.code(&i32, &i32WithImm, &imm)) { |
| 519 |
switch (i32) { |
| 520 |
case I32::LitImm: |
| 521 |
store32(TrustedImm32(m_module->m_read.immU32()), temporaryAddress(dst)); |
| 522 |
break; |
| 523 |
case I32::LitPool: |
| 524 |
case I32::GetLoc: |
| 525 |
parseGetLocal(dst); |
| 526 |
break; |
| 527 |
case I32::GetGlo: |
| 528 |
RELEASE_ASSERT_NOT_REACHED(); |
| 529 |
case I32::SetLoc: |
| 530 |
parseSetLocal(dst); |
| 531 |
break; |
| 532 |
case I32::SetGlo: |
| 533 |
case I32::SLoad8: |
| 534 |
case I32::SLoadOff8: |
| 535 |
case I32::ULoad8: |
| 536 |
case I32::ULoadOff8: |
| 537 |
case I32::SLoad16: |
| 538 |
case I32::SLoadOff16: |
| 539 |
case I32::ULoad16: |
| 540 |
case I32::ULoadOff16: |
| 541 |
case I32::Load32: |
| 542 |
case I32::LoadOff32: |
| 543 |
case I32::Store8: |
| 544 |
case I32::StoreOff8: |
| 545 |
case I32::Store16: |
| 546 |
case I32::StoreOff16: |
| 547 |
case I32::Store32: |
| 548 |
case I32::StoreOff32: |
| 549 |
RELEASE_ASSERT_NOT_REACHED(); |
| 550 |
case I32::CallInt: |
| 551 |
parseCallInternal(RType::I32, dst); |
| 552 |
break; |
| 553 |
case I32::CallInd: |
| 554 |
case I32::CallImp: |
| 555 |
RELEASE_ASSERT_NOT_REACHED(); |
| 556 |
case I32::Cond: |
| 557 |
parseCond(RType::I32, dst); |
| 558 |
break; |
| 559 |
case I32::Comma: |
| 560 |
parseComma(RType::I32, dst); |
| 561 |
break; |
| 562 |
case I32::FromF32: |
| 563 |
RELEASE_ASSERT_NOT_REACHED(); |
| 564 |
case I32::FromF64: |
| 565 |
parseExprF64(dst); |
| 566 |
loadDouble(temporaryAddress(dst), FPRInfo::fpRegT0); |
| 567 |
truncateDoubleToInt32(FPRInfo::fpRegT0, GPRInfo::regT0); |
| 568 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 569 |
break; |
| 570 |
case I32::Neg: |
| 571 |
parseUnaryOp(static_cast<uint8_t>(i32), RType::I32, dst); |
| 572 |
break; |
| 573 |
case I32::Add: |
| 574 |
case I32::Sub: |
| 575 |
case I32::Mul: |
| 576 |
parseBinaryOp(static_cast<uint8_t>(i32), RType::I32, dst); |
| 577 |
break; |
| 578 |
case I32::SDiv: |
| 579 |
case I32::UDiv: |
| 580 |
case I32::SMod: |
| 581 |
case I32::UMod: |
| 582 |
RELEASE_ASSERT_NOT_REACHED(); |
| 583 |
case I32::BitNot: |
| 584 |
parseUnaryOp(static_cast<uint8_t>(i32), RType::I32, dst); |
| 585 |
break; |
| 586 |
case I32::BitOr: |
| 587 |
case I32::BitAnd: |
| 588 |
case I32::BitXor: |
| 589 |
parseBinaryOp(static_cast<uint8_t>(i32), RType::I32, dst); |
| 590 |
break; |
| 591 |
case I32::Lsh: |
| 592 |
case I32::ArithRsh: |
| 593 |
case I32::LogicRsh: |
| 594 |
case I32::Clz: |
| 595 |
case I32::LogicNot: |
| 596 |
RELEASE_ASSERT_NOT_REACHED(); |
| 597 |
case I32::EqI32: |
| 598 |
parseRelationalOpI32(Equal, RType::I32, dst); |
| 599 |
break; |
| 600 |
case I32::EqF32: |
| 601 |
RELEASE_ASSERT_NOT_REACHED(); |
| 602 |
case I32::EqF64: |
| 603 |
parseRelationalOpF64(DoubleEqual, RType::F64, dst); |
| 604 |
break; |
| 605 |
case I32::NEqI32: |
| 606 |
parseRelationalOpI32(NotEqual, RType::I32, dst); |
| 607 |
break; |
| 608 |
case I32::NEqF32: |
| 609 |
RELEASE_ASSERT_NOT_REACHED(); |
| 610 |
case I32::NEqF64: |
| 611 |
parseRelationalOpF64(DoubleNotEqual, RType::F64, dst); |
| 612 |
break; |
| 613 |
case I32::SLeThI32: |
| 614 |
parseRelationalOpI32(LessThan, RType::I32, dst); |
| 615 |
break; |
| 616 |
case I32::ULeThI32: |
| 617 |
parseRelationalOpI32(Below, RType::I32, dst); |
| 618 |
break; |
| 619 |
case I32::LeThF32: |
| 620 |
RELEASE_ASSERT_NOT_REACHED(); |
| 621 |
case I32::LeThF64: |
| 622 |
parseRelationalOpF64(DoubleLessThan, RType::F64, dst); |
| 623 |
break; |
| 624 |
case I32::SLeEqI32: |
| 625 |
parseRelationalOpI32(LessThanOrEqual, RType::I32, dst); |
| 626 |
break; |
| 627 |
case I32::ULeEqI32: |
| 628 |
parseRelationalOpI32(BelowOrEqual, RType::I32, dst); |
| 629 |
break; |
| 630 |
case I32::LeEqF32: |
| 631 |
RELEASE_ASSERT_NOT_REACHED(); |
| 632 |
case I32::LeEqF64: |
| 633 |
parseRelationalOpF64(DoubleLessThanOrEqual, RType::F64, dst); |
| 634 |
break; |
| 635 |
case I32::SGrThI32: |
| 636 |
parseRelationalOpI32(GreaterThan, RType::I32, dst); |
| 637 |
break; |
| 638 |
case I32::UGrThI32: |
| 639 |
parseRelationalOpI32(Above, RType::I32, dst); |
| 640 |
break; |
| 641 |
case I32::GrThF32: |
| 642 |
RELEASE_ASSERT_NOT_REACHED(); |
| 643 |
case I32::GrThF64: |
| 644 |
parseRelationalOpF64(DoubleGreaterThan, RType::F64, dst); |
| 645 |
break; |
| 646 |
case I32::SGrEqI32: |
| 647 |
parseRelationalOpI32(GreaterThanOrEqual, RType::I32, dst); |
| 648 |
break; |
| 649 |
case I32::UGrEqI32: |
| 650 |
parseRelationalOpI32(AboveOrEqual, RType::I32, dst); |
| 651 |
break; |
| 652 |
case I32::GrEqF32: |
| 653 |
RELEASE_ASSERT_NOT_REACHED(); |
| 654 |
case I32::GrEqF64: |
| 655 |
parseRelationalOpF64(DoubleGreaterThanOrEqual, RType::F64, dst); |
| 656 |
break; |
| 657 |
case I32::SMin: |
| 658 |
case I32::UMin: |
| 659 |
case I32::SMax: |
| 660 |
case I32::UMax: |
| 661 |
case I32::Abs: |
| 662 |
default: |
| 663 |
RELEASE_ASSERT_NOT_REACHED(); |
| 664 |
} |
| 665 |
} else { |
| 666 |
switch (i32WithImm) { |
| 667 |
case I32WithImm::LitImm: |
| 668 |
store32(TrustedImm32(imm), temporaryAddress(dst)); |
| 669 |
break; |
| 670 |
case I32WithImm::LitPool: |
| 671 |
store32(TrustedImm32(m_module->m_constantI32s[imm]), temporaryAddress(dst)); |
| 672 |
break; |
| 673 |
case I32WithImm::GetLoc: |
| 674 |
load32(stackAddress(imm), GPRInfo::regT0); |
| 675 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 676 |
break; |
| 677 |
default: |
| 678 |
RELEASE_ASSERT_NOT_REACHED(); |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
void JITCompiler::parseExprF64(int dst) |
| 684 |
{ |
| 685 |
F64 f64; |
| 686 |
F64WithImm f64WithImm; |
| 687 |
uint8_t imm; |
| 688 |
|
| 689 |
if (m_module->m_read.code(&f64, &f64WithImm, &imm)) { |
| 690 |
switch (f64) { |
| 691 |
case F64::LitImm: |
| 692 |
store64(TrustedImm64(reinterpretDoubleToInt64(m_module->m_read.fixedWidth<double>())), temporaryAddress(dst)); |
| 693 |
break; |
| 694 |
case F64::Neg: |
| 695 |
parseUnaryOp(static_cast<uint8_t>(f64), RType::F64, dst); |
| 696 |
break; |
| 697 |
case F64::Add: |
| 698 |
case F64::Sub: |
| 699 |
case F64::Mul: |
| 700 |
case F64::Div: |
| 701 |
parseBinaryOp(static_cast<uint8_t>(f64), RType::F64, dst); |
| 702 |
break; |
| 703 |
case F64::Sqrt: |
| 704 |
parseUnaryOp(static_cast<uint8_t>(f64), RType::F64, dst); |
| 705 |
break; |
| 706 |
default: |
| 707 |
RELEASE_ASSERT_NOT_REACHED(); |
| 708 |
} |
| 709 |
} else { |
| 710 |
switch (f64WithImm) { |
| 711 |
case F64WithImm::LitPool: |
| 712 |
store64(TrustedImm64(reinterpretDoubleToInt64(m_module->m_constantF64s[imm])), temporaryAddress(dst)); |
| 713 |
break; |
| 714 |
case F64WithImm::GetLoc: |
| 715 |
// FIXME: loadDouble/storeDouble? |
| 716 |
load64(stackAddress(imm), GPRInfo::regT0); |
| 717 |
store64(GPRInfo::regT0, temporaryAddress(dst)); |
| 718 |
break; |
| 719 |
default: |
| 720 |
RELEASE_ASSERT_NOT_REACHED(); |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
static int32_t callInternalFunction(ExecState* exec, Module* module, uint32_t functionIndex) |
| 726 |
{ |
| 727 |
// FIXME: Do we have to use these two lines? |
| 728 |
// VM* vm = exec->vm(); |
| 729 |
// NativeCallFrameTracer tracer(vm, exec); |
| 730 |
|
| 731 |
// FIXME: We shouldn't unbox the value here! |
| 732 |
return module->callInternalFunction(exec, functionIndex).asInt32(); |
| 733 |
} |
| 734 |
|
| 735 |
void JITCompiler::parseCallInternal(RType rType, int dst) |
| 736 |
{ |
| 737 |
ASSERT(rType == RType::I32); |
| 738 |
uint32_t functionIndex = m_module->m_read.immU32(); |
| 739 |
const Vector<Type>& arguments = m_module->m_signatures[m_module->m_funcSignatures[functionIndex]].arguments; |
| 740 |
|
| 741 |
// TODO: actually pass arguments |
| 742 |
for (size_t i = 0; i < arguments.size(); ++i) { |
| 743 |
switch (arguments[i]) { |
| 744 |
case Type::I32: |
| 745 |
parseExpr(RType::I32, dst); |
| 746 |
break; |
| 747 |
case Type::F64: |
| 748 |
parseExpr(RType::F64, dst); |
| 749 |
break; |
| 750 |
default: |
| 751 |
ASSERT_NOT_REACHED(); |
| 752 |
} |
| 753 |
} |
| 754 |
if (maxFrameExtentForSlowPathCall) |
| 755 |
addPtr(TrustedImm32(-maxFrameExtentForSlowPathCall), stackPointerRegister); |
| 756 |
setupArgumentsWithExecState(TrustedImmPtr(m_module), TrustedImm32(functionIndex)); |
| 757 |
appendCall(callInternalFunction); |
| 758 |
if (maxFrameExtentForSlowPathCall) |
| 759 |
addPtr(TrustedImm32(maxFrameExtentForSlowPathCall), stackPointerRegister); |
| 760 |
|
| 761 |
// FIXME: Can we really use returnValueGPR? |
| 762 |
store32(GPRInfo::returnValueGPR, temporaryAddress(dst)); |
| 763 |
} |
| 764 |
|
| 765 |
void JITCompiler::parseBinaryOp(uint8_t op, RType rType, int dst) |
| 766 |
{ |
| 767 |
parseExpr(rType, dst); |
| 768 |
parseExpr(rType, dst + 1); |
| 769 |
switch (rType) { |
| 770 |
case RType::I32: |
| 771 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 772 |
load32(temporaryAddress(dst + 1), GPRInfo::regT1); |
| 773 |
switch (static_cast<I32>(op)) { |
| 774 |
case I32::Add: |
| 775 |
add32(GPRInfo::regT1, GPRInfo::regT0); |
| 776 |
break; |
| 777 |
case I32::Sub: |
| 778 |
sub32(GPRInfo::regT1, GPRInfo::regT0); |
| 779 |
break; |
| 780 |
case I32::Mul: |
| 781 |
mul32(GPRInfo::regT1, GPRInfo::regT0); |
| 782 |
break; |
| 783 |
case I32::BitOr: |
| 784 |
or32(GPRInfo::regT1, GPRInfo::regT0); |
| 785 |
break; |
| 786 |
case I32::BitAnd: |
| 787 |
and32(GPRInfo::regT1, GPRInfo::regT0); |
| 788 |
break; |
| 789 |
case I32::BitXor: |
| 790 |
xor32(GPRInfo::regT1, GPRInfo::regT0); |
| 791 |
break; |
| 792 |
default: |
| 793 |
ASSERT_NOT_REACHED(); |
| 794 |
} |
| 795 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 796 |
break; |
| 797 |
case RType::F64: |
| 798 |
loadDouble(temporaryAddress(dst), FPRInfo::fpRegT0); |
| 799 |
loadDouble(temporaryAddress(dst + 1), FPRInfo::fpRegT1); |
| 800 |
switch (static_cast<F64>(op)) { |
| 801 |
case F64::Add: |
| 802 |
addDouble(FPRInfo::fpRegT1, FPRInfo::fpRegT0); |
| 803 |
break; |
| 804 |
case F64::Sub: |
| 805 |
subDouble(FPRInfo::fpRegT1, FPRInfo::fpRegT0); |
| 806 |
break; |
| 807 |
case F64::Mul: |
| 808 |
mulDouble(FPRInfo::fpRegT1, FPRInfo::fpRegT0); |
| 809 |
break; |
| 810 |
case F64::Div: |
| 811 |
divDouble(FPRInfo::fpRegT1, FPRInfo::fpRegT0); |
| 812 |
break; |
| 813 |
default: |
| 814 |
ASSERT_NOT_REACHED(); |
| 815 |
} |
| 816 |
storeDouble(FPRInfo::fpRegT0, temporaryAddress(dst)); |
| 817 |
break; |
| 818 |
default: |
| 819 |
RELEASE_ASSERT_NOT_REACHED(); |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
void JITCompiler::parseUnaryOp(uint8_t op, RType rType, int dst) |
| 824 |
{ |
| 825 |
parseExpr(rType, dst); |
| 826 |
switch (rType) { |
| 827 |
case RType::I32: |
| 828 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 829 |
switch (static_cast<I32>(op)) { |
| 830 |
case I32::Neg: |
| 831 |
neg32(GPRInfo::regT0); |
| 832 |
break; |
| 833 |
case I32::BitNot: |
| 834 |
xor32(TrustedImm32(-1), GPRInfo::regT0); |
| 835 |
break; |
| 836 |
default: |
| 837 |
ASSERT_NOT_REACHED(); |
| 838 |
} |
| 839 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 840 |
break; |
| 841 |
case RType::F64: |
| 842 |
loadDouble(temporaryAddress(dst), FPRInfo::fpRegT0); |
| 843 |
switch (static_cast<F64>(op)) { |
| 844 |
case F64::Neg: |
| 845 |
negateDouble(FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 846 |
break; |
| 847 |
case F64::Sqrt: |
| 848 |
sqrtDouble(FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 849 |
break; |
| 850 |
default: |
| 851 |
ASSERT_NOT_REACHED(); |
| 852 |
} |
| 853 |
storeDouble(FPRInfo::fpRegT1, temporaryAddress(dst)); |
| 854 |
break; |
| 855 |
default: |
| 856 |
RELEASE_ASSERT_NOT_REACHED(); |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
void JITCompiler::parseRelationalOpI32(RelationalCondition condition, RType operandType, int dst) |
| 861 |
{ |
| 862 |
parseExpr(operandType, dst); |
| 863 |
parseExpr(operandType, dst + 1); |
| 864 |
switch (operandType) { |
| 865 |
case RType::I32: |
| 866 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 867 |
load32(temporaryAddress(dst + 1), GPRInfo::regT1); |
| 868 |
compare32(condition, GPRInfo::regT0, GPRInfo::regT1, GPRInfo::regT0); |
| 869 |
break; |
| 870 |
default: |
| 871 |
RELEASE_ASSERT_NOT_REACHED(); |
| 872 |
} |
| 873 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 874 |
} |
| 875 |
|
| 876 |
void JITCompiler::parseRelationalOpF64(DoubleCondition condition, RType operandType, int dst) |
| 877 |
{ |
| 878 |
parseExpr(operandType, dst); |
| 879 |
parseExpr(operandType, dst + 1); |
| 880 |
switch (operandType) { |
| 881 |
case RType::F64: { |
| 882 |
loadDouble(temporaryAddress(dst), FPRInfo::fpRegT0); |
| 883 |
loadDouble(temporaryAddress(dst + 1), FPRInfo::fpRegT1); |
| 884 |
Jump trueCase = branchDouble(condition, FPRInfo::fpRegT0, FPRInfo::fpRegT1); |
| 885 |
store32(TrustedImm32(0), temporaryAddress(dst)); |
| 886 |
Jump end = jump(); |
| 887 |
trueCase.link(this); |
| 888 |
store32(TrustedImm32(1), temporaryAddress(dst)); |
| 889 |
end.link(this); |
| 890 |
break; |
| 891 |
} |
| 892 |
default: |
| 893 |
RELEASE_ASSERT_NOT_REACHED(); |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
void JITCompiler::parseComma(RType rType, int dst) |
| 898 |
{ |
| 899 |
parseExpr(m_module->m_read.rtype(), dst); |
| 900 |
parseExpr(rType, dst); |
| 901 |
} |
| 902 |
|
| 903 |
void JITCompiler::parseCond(RType rType, int dst) |
| 904 |
{ |
| 905 |
parseExpr(RType::I32, dst); |
| 906 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 907 |
Jump els = branchTest32(Zero, GPRInfo::regT0); |
| 908 |
parseExpr(rType, dst); |
| 909 |
Jump end = jump(); |
| 910 |
els.link(this); |
| 911 |
parseExpr(rType, dst); |
| 912 |
end.link(this); |
| 913 |
} |
| 914 |
|
| 915 |
} } // namespace JSC::Wasm |