|
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 |
#include "config.h" |
| 27 |
#include "WasmJITCompiler.h" |
| 28 |
|
| 29 |
#include "JITCode.h" |
| 30 |
#include "JSCallee.h" |
| 31 |
#include "LinkBuffer.h" |
| 32 |
#include "ProtoCallFrame.h" |
| 33 |
|
| 34 |
namespace JSC { |
| 35 |
|
| 36 |
namespace Wasm { |
| 37 |
|
| 38 |
class JumpScope { |
| 39 |
public: |
| 40 |
JumpScope(MacroAssembler* macroAssembler) |
| 41 |
: m_macroAssembler(macroAssembler) |
| 42 |
, m_linked(false) |
| 43 |
{ |
| 44 |
} |
| 45 |
|
| 46 |
JumpScope(MacroAssembler* macroAssembler, const MacroAssembler::Label& label) |
| 47 |
: m_macroAssembler(macroAssembler) |
| 48 |
, m_linked(true) |
| 49 |
, m_label(label) |
| 50 |
{ |
| 51 |
} |
| 52 |
|
| 53 |
void jump() |
| 54 |
{ |
| 55 |
if (m_linked) |
| 56 |
m_macroAssembler->jump(m_label); |
| 57 |
else |
| 58 |
m_jumpList.append(m_macroAssembler->jump()); |
| 59 |
} |
| 60 |
|
| 61 |
void link() |
| 62 |
{ |
| 63 |
ASSERT(!m_linked); |
| 64 |
m_jumpList.link(m_macroAssembler); |
| 65 |
} |
| 66 |
|
| 67 |
private: |
| 68 |
MacroAssembler* m_macroAssembler; |
| 69 |
bool m_linked; |
| 70 |
MacroAssembler::Label m_label; |
| 71 |
MacroAssembler::JumpList m_jumpList; |
| 72 |
}; |
| 73 |
|
| 74 |
JSValue JITCompiler::compileAndRun(ExecState* exec, const Vector<char>& buffer) |
| 75 |
{ |
| 76 |
VM& vm = exec->vm(); |
| 77 |
RefPtr<JITCode> jitCode = compile(vm, buffer); |
| 78 |
|
| 79 |
ProtoCallFrame protoCallFrame; |
| 80 |
protoCallFrame.init(0, JSCallee::create(vm, exec->lexicalGlobalObject(), exec->lexicalGlobalObject()), jsUndefined(), 1); |
| 81 |
|
| 82 |
return jitCode->execute(&vm, &protoCallFrame); |
| 83 |
} |
| 84 |
|
| 85 |
RefPtr<JITCode> JITCompiler::compile(VM& vm, const Vector<char>& buffer) |
| 86 |
{ |
| 87 |
emitFunctionPrologue(); |
| 88 |
|
| 89 |
m_read = In(reinterpret_cast<const uint8_t*>(buffer.data())); |
| 90 |
if (m_read.fixedWidth<uint32_t>() != MagicNumber) |
| 91 |
abort(); |
| 92 |
m_read.fixedWidth<uint32_t>(); // out_size |
| 93 |
unpack(); |
| 94 |
|
| 95 |
boxInt52(GPRInfo::returnValueGPR, GPRInfo::returnValueGPR, GPRInfo::regT0, FPRInfo::fpRegT0); |
| 96 |
emitFunctionEpilogue(); |
| 97 |
ret(); |
| 98 |
|
| 99 |
JITCompilationEffort effort = JITCompilationMustSucceed; |
| 100 |
LinkBuffer patchBuffer(vm, *this, 0, effort); |
| 101 |
|
| 102 |
MacroAssemblerCodePtr withArityCheck; |
| 103 |
CodeRef result = FINALIZE_CODE(patchBuffer, ("Baseline JIT code for WebAssembly")); |
| 104 |
|
| 105 |
// TODO: should create a new jitType |
| 106 |
return adoptRef(new DirectJITCode(result, withArityCheck, JITCode::BaselineJIT)); |
| 107 |
} |
| 108 |
|
| 109 |
void JITCompiler::unpack() |
| 110 |
{ |
| 111 |
readConstantPoolSection(); |
| 112 |
readSignatureSection(); |
| 113 |
readFunctionImportSection(); |
| 114 |
readGlobalSection(); |
| 115 |
readFunctionDeclarationSection(); |
| 116 |
readFunctionPointerTables(); |
| 117 |
readFunctionDefinitionSection(); |
| 118 |
readExportSection(); |
| 119 |
} |
| 120 |
|
| 121 |
void JITCompiler::readSignatureSection() |
| 122 |
{ |
| 123 |
uint32_t numSignatures = m_read.immU32(); |
| 124 |
Vector<Signature> signatures(numSignatures); |
| 125 |
for (uint32_t signatureIndex = 0; signatureIndex < numSignatures; signatureIndex++) { |
| 126 |
RType ret = m_read.rtype(); |
| 127 |
uint32_t numArgs = m_read.immU32(); |
| 128 |
Signature signature(ret, numArgs); |
| 129 |
for (uint32_t argumentIndex = 0; argumentIndex < numArgs; argumentIndex++) |
| 130 |
signature.args[argumentIndex] = m_read.type(); |
| 131 |
signatures[signatureIndex] = WTF::move(signature); |
| 132 |
} |
| 133 |
|
| 134 |
m_signatures = WTF::move(signatures); |
| 135 |
} |
| 136 |
|
| 137 |
void JITCompiler::readFunctionImportSection() |
| 138 |
{ |
| 139 |
uint32_t numFuncImps = m_read.immU32(); |
| 140 |
uint32_t numFuncImportSignatures = m_read.immU32(); |
| 141 |
|
| 142 |
Vector<FuncImportSignature> funcImportSignatures(numFuncImportSignatures); |
| 143 |
FuncImportSignature* funcImportSignature = funcImportSignatures.data(); |
| 144 |
for (uint32_t func_imp_i = 0; func_imp_i < numFuncImps; func_imp_i++) { |
| 145 |
while (m_read.singleChar()) { } |
| 146 |
|
| 147 |
uint32_t numSigs = m_read.immU32(); |
| 148 |
for (uint32_t i = 0; i < numSigs; i++) { |
| 149 |
funcImportSignature->signatureIndex = m_read.immU32(); |
| 150 |
funcImportSignature->funcImportIndex = func_imp_i; |
| 151 |
funcImportSignature++; |
| 152 |
} |
| 153 |
} |
| 154 |
assert(funcImportSignature == funcImportSignatures.data() + numFuncImportSignatures); |
| 155 |
|
| 156 |
m_funcImportSignatures = WTF::move(funcImportSignatures); |
| 157 |
} |
| 158 |
|
| 159 |
void JITCompiler::readGlobalSection() |
| 160 |
{ |
| 161 |
uint32_t num_i32_zero = m_read.immU32(); |
| 162 |
uint32_t num_f32_zero = m_read.immU32(); |
| 163 |
uint32_t num_f64_zero = m_read.immU32(); |
| 164 |
uint32_t num_i32_import = m_read.immU32(); |
| 165 |
uint32_t num_f32_import = m_read.immU32(); |
| 166 |
uint32_t num_f64_import = m_read.immU32(); |
| 167 |
|
| 168 |
uint32_t numGlobalVars = num_i32_zero + num_f32_zero + num_f64_zero + num_i32_import + num_f32_import + num_f64_import; |
| 169 |
|
| 170 |
Vector<Type> globalTypes(numGlobalVars); |
| 171 |
size_t globalTypeIndex = 0; |
| 172 |
for (uint32_t i = 0; i < num_i32_zero; i++) { |
| 173 |
globalTypes[globalTypeIndex++] = Type::I32; |
| 174 |
} |
| 175 |
for (uint32_t i = 0; i < num_f32_zero; i++) { |
| 176 |
globalTypes[globalTypeIndex++] = Type::F32; |
| 177 |
} |
| 178 |
for (uint32_t i = 0; i < num_f64_zero; i++) { |
| 179 |
globalTypes[globalTypeIndex++] = Type::F64; |
| 180 |
} |
| 181 |
for (uint32_t i = 0; i < num_i32_import; i++) { |
| 182 |
while (m_read.singleChar()) { } |
| 183 |
globalTypes[globalTypeIndex++] = Type::I32; |
| 184 |
} |
| 185 |
for (uint32_t i = 0; i < num_f32_import; i++) { |
| 186 |
while (m_read.singleChar()) { } |
| 187 |
globalTypes[globalTypeIndex++] = Type::F32; |
| 188 |
} |
| 189 |
for (uint32_t i = 0; i < num_f64_import; i++) { |
| 190 |
while (m_read.singleChar()) { } |
| 191 |
globalTypes[globalTypeIndex++] = Type::F64; |
| 192 |
} |
| 193 |
m_globalTypes = WTF::move(globalTypes); |
| 194 |
} |
| 195 |
|
| 196 |
void JITCompiler::readConstantPoolSection() |
| 197 |
{ |
| 198 |
uint32_t numI32s = m_read.immU32(); |
| 199 |
uint32_t numF32s = m_read.immU32(); |
| 200 |
uint32_t numF64s = m_read.immU32(); |
| 201 |
|
| 202 |
Vector<uint32_t> i32s(numI32s); |
| 203 |
for (uint32_t i = 0; i < numI32s; i++) |
| 204 |
i32s[i] = m_read.immU32(); |
| 205 |
|
| 206 |
Vector<float> f32s(numF32s); |
| 207 |
for (uint32_t i = 0; i < numF32s; i++) |
| 208 |
f32s[i] = m_read.fixedWidth<float>(); |
| 209 |
|
| 210 |
Vector<double> f64s(numF64s); |
| 211 |
for (uint32_t i = 0; i < numF64s; i++) |
| 212 |
f64s[i] = m_read.fixedWidth<double>(); |
| 213 |
|
| 214 |
m_i32s = WTF::move(i32s); |
| 215 |
m_f32s = WTF::move(f32s); |
| 216 |
m_f64s = WTF::move(f64s); |
| 217 |
} |
| 218 |
|
| 219 |
void JITCompiler::readFunctionDeclarationSection() |
| 220 |
{ |
| 221 |
uint32_t numFuncs = m_read.immU32(); |
| 222 |
Vector<uint32_t> funcSigs(numFuncs); |
| 223 |
for (uint32_t i = 0; i != numFuncs; i++) |
| 224 |
funcSigs[i] = m_read.immU32(); |
| 225 |
|
| 226 |
m_funcSigs = WTF::move(funcSigs); |
| 227 |
} |
| 228 |
|
| 229 |
void JITCompiler::readFunctionPointerTables() |
| 230 |
{ |
| 231 |
uint32_t numFuncPtrTables = m_read.immU32(); |
| 232 |
Vector<FuncPtrTable> funcPtrTables(numFuncPtrTables); |
| 233 |
for (uint32_t i = 0; i != numFuncPtrTables; i++) { |
| 234 |
funcPtrTables[i].signatureIndex = m_read.immU32(); |
| 235 |
uint32_t num_elements = m_read.immU32(); |
| 236 |
Vector<uint32_t> elements(num_elements); |
| 237 |
for (uint32_t j = 0; j != num_elements; j++) |
| 238 |
elements[j] = m_read.immU32(); |
| 239 |
funcPtrTables[i].elements = WTF::move(elements); |
| 240 |
} |
| 241 |
|
| 242 |
m_funcPtrTables = WTF::move(funcPtrTables); |
| 243 |
} |
| 244 |
|
| 245 |
void JITCompiler::readFunctionDefinitionSection() |
| 246 |
{ |
| 247 |
for (size_t i = 0; i < m_funcSigs.size(); i++) |
| 248 |
readFunctionDefinition(i); |
| 249 |
} |
| 250 |
|
| 251 |
void JITCompiler::readExportSection() |
| 252 |
{ |
| 253 |
switch (m_read.exportFormat()) { |
| 254 |
case ExportFormat::Default: |
| 255 |
m_read.immU32(); |
| 256 |
break; |
| 257 |
case ExportFormat::Record: |
| 258 |
if (uint32_t num_exports = m_read.immU32()) { |
| 259 |
for (uint32_t export_index = 0;;) { |
| 260 |
while (m_read.singleChar()) { } |
| 261 |
m_read.immU32(); |
| 262 |
if (++export_index == num_exports) |
| 263 |
break; |
| 264 |
} |
| 265 |
} |
| 266 |
break; |
| 267 |
default: |
| 268 |
RELEASE_ASSERT_NOT_REACHED(); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
void JITCompiler::readFunctionDefinition(size_t funcIndex) |
| 273 |
{ |
| 274 |
const Signature& signature = m_signatures[m_funcSigs[funcIndex]]; |
| 275 |
m_currentReturnType = signature.ret; |
| 276 |
m_currentLocalTypes.clear(); |
| 277 |
readArgs(signature); |
| 278 |
readVars(); |
| 279 |
stmt_list(0); |
| 280 |
} |
| 281 |
|
| 282 |
void JITCompiler::readArgs(const Signature& signature) |
| 283 |
{ |
| 284 |
size_t numArgs = signature.args.size(); |
| 285 |
for (uint32_t argIndex = 0; argIndex < numArgs; argIndex++) { |
| 286 |
Type type = signature.args[argIndex]; |
| 287 |
m_currentLocalTypes.append(type); |
| 288 |
switch (type) { |
| 289 |
case Type::I32: |
| 290 |
case Type::F32: |
| 291 |
case Type::F64: |
| 292 |
break; |
| 293 |
default: |
| 294 |
RELEASE_ASSERT_NOT_REACHED(); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
void JITCompiler::readVars() |
| 300 |
{ |
| 301 |
uint32_t numI32Vars = 0; |
| 302 |
uint32_t numF32Vars = 0; |
| 303 |
uint32_t numF64Vars = 0; |
| 304 |
|
| 305 |
VarTypes varTypes; |
| 306 |
VarTypesWithImm varTypesWithImm; |
| 307 |
uint8_t imm; |
| 308 |
if (m_read.code(&varTypes, &varTypesWithImm, &imm)) { |
| 309 |
if (varTypes & VarTypes::I32) |
| 310 |
numI32Vars = m_read.immU32(); |
| 311 |
if (varTypes & VarTypes::F32) |
| 312 |
numF32Vars = m_read.immU32(); |
| 313 |
if (varTypes & VarTypes::F64) |
| 314 |
numF64Vars = m_read.immU32(); |
| 315 |
} else { |
| 316 |
numI32Vars = imm; |
| 317 |
} |
| 318 |
|
| 319 |
// FIXME: we need to initialize these to zeroes |
| 320 |
|
| 321 |
uint32_t num_vars = numI32Vars + numF32Vars + numF64Vars; |
| 322 |
if (num_vars > 0) { |
| 323 |
uint32_t localIndex = m_currentLocalTypes.size(); |
| 324 |
for (uint32_t i = 0; i < numI32Vars; i++) { |
| 325 |
m_currentLocalTypes.append(Type::I32); |
| 326 |
store32(TrustedImm32(0), stackAddress(localIndex++)); |
| 327 |
} |
| 328 |
for (uint32_t i = 0; i < numF32Vars; i++) { |
| 329 |
m_currentLocalTypes.append(Type::F32); |
| 330 |
store32(TrustedImm32(0), stackAddress(localIndex++)); // FIXME: float? |
| 331 |
} |
| 332 |
for (uint32_t i = 0; i < numF64Vars; i++) { |
| 333 |
m_currentLocalTypes.append(Type::F64); |
| 334 |
store64(TrustedImm64(0), stackAddress(localIndex++)); // FIXME: float? |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
void JITCompiler::stmt(int dst) |
| 340 |
{ |
| 341 |
Stmt stmt; |
| 342 |
StmtWithImm stmtWithImm; |
| 343 |
uint8_t imm; |
| 344 |
if (m_read.code(&stmt, &stmtWithImm, &imm)) { |
| 345 |
switch (stmt) { |
| 346 |
case Stmt::SetLoc: |
| 347 |
set_local(dst); |
| 348 |
break; |
| 349 |
case Stmt::SetGlo: |
| 350 |
case Stmt::I32Store8: |
| 351 |
case Stmt::I32StoreOff8: |
| 352 |
case Stmt::I32Store16: |
| 353 |
case Stmt::I32StoreOff16: |
| 354 |
case Stmt::I32Store32: |
| 355 |
case Stmt::I32StoreOff32: |
| 356 |
case Stmt::F32Store: |
| 357 |
case Stmt::F32StoreOff: |
| 358 |
case Stmt::F64Store: |
| 359 |
case Stmt::F64StoreOff: |
| 360 |
case Stmt::CallInt: |
| 361 |
case Stmt::CallInd: |
| 362 |
case Stmt::CallImp: |
| 363 |
RELEASE_ASSERT_NOT_REACHED(); |
| 364 |
case Stmt::Ret: |
| 365 |
return_stmt(); |
| 366 |
return; |
| 367 |
case Stmt::Block: |
| 368 |
block_stmt(dst); return; |
| 369 |
case Stmt::IfThen: |
| 370 |
if_stmt(dst); |
| 371 |
return; |
| 372 |
case Stmt::IfElse: |
| 373 |
if_else_stmt(dst); |
| 374 |
return; |
| 375 |
case Stmt::While: |
| 376 |
while_stmt(dst); |
| 377 |
return; |
| 378 |
case Stmt::Do: |
| 379 |
do_stmt(dst); |
| 380 |
return; |
| 381 |
case Stmt::Label: |
| 382 |
label_stmt(dst); |
| 383 |
return; |
| 384 |
case Stmt::Break: |
| 385 |
topBreakJumpList()->append(jump()); |
| 386 |
return; |
| 387 |
case Stmt::BreakLabel: |
| 388 |
break_label_stmt(); |
| 389 |
return; |
| 390 |
case Stmt::Continue: |
| 391 |
topContinueJumpScope()->jump(); |
| 392 |
break; |
| 393 |
case Stmt::ContinueLabel: |
| 394 |
continue_label_stmt(); |
| 395 |
return; |
| 396 |
case Stmt::Switch: |
| 397 |
switch_stmt(dst); |
| 398 |
break; |
| 399 |
default: |
| 400 |
RELEASE_ASSERT_NOT_REACHED(); |
| 401 |
} |
| 402 |
} else { |
| 403 |
switch (stmtWithImm) { |
| 404 |
case StmtWithImm::SetLoc: |
| 405 |
set_local(imm, dst); |
| 406 |
break; |
| 407 |
case StmtWithImm::SetGlo: |
| 408 |
default: |
| 409 |
RELEASE_ASSERT_NOT_REACHED(); |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
void JITCompiler::get_local(int dst) |
| 415 |
{ |
| 416 |
// FIXME: type |
| 417 |
uint32_t imm = m_read.immU32(); |
| 418 |
load32(stackAddress(imm), GPRInfo::regT0); |
| 419 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 420 |
} |
| 421 |
|
| 422 |
void JITCompiler::set_local(uint32_t imm, int dst) |
| 423 |
{ |
| 424 |
Type type = m_currentLocalTypes[imm]; // FIXME: Should we receive the Type from the parent? |
| 425 |
|
| 426 |
expr(RType(type), dst); |
| 427 |
|
| 428 |
switch (type) { |
| 429 |
case Type::I32: { |
| 430 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 431 |
store32(GPRInfo::regT0, stackAddress(imm)); |
| 432 |
break; |
| 433 |
} |
| 434 |
case Type::F32: |
| 435 |
case Type::F64: |
| 436 |
default: |
| 437 |
RELEASE_ASSERT_NOT_REACHED(); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
void JITCompiler::set_local(int dst) |
| 442 |
{ |
| 443 |
set_local(m_read.immU32(), dst); |
| 444 |
} |
| 445 |
|
| 446 |
void JITCompiler::return_stmt() |
| 447 |
{ |
| 448 |
// FIXME: hacky |
| 449 |
switch (m_currentReturnType) { |
| 450 |
case RType::I32: |
| 451 |
signed_expr(Signedness::Signed, 0); |
| 452 |
load32(temporaryAddress(0), GPRInfo::returnValueGPR); |
| 453 |
break; |
| 454 |
case RType::F32: |
| 455 |
expr(RType::F32, 0); |
| 456 |
load32(temporaryAddress(0), GPRInfo::returnValueGPR); |
| 457 |
break; |
| 458 |
case RType::F64: |
| 459 |
expr(RType::F64, 0); |
| 460 |
load64(temporaryAddress(0), GPRInfo::returnValueGPR); |
| 461 |
break; |
| 462 |
case RType::Void: |
| 463 |
break; |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
void JITCompiler::stmt_list(int dst) |
| 468 |
{ |
| 469 |
uint32_t num_stmts = m_read.immU32(); |
| 470 |
for (uint32_t i = 0; i < num_stmts; i++) |
| 471 |
stmt(dst); |
| 472 |
} |
| 473 |
|
| 474 |
void JITCompiler::block_stmt(int dst) |
| 475 |
{ |
| 476 |
stmt_list(dst); |
| 477 |
} |
| 478 |
|
| 479 |
void JITCompiler::if_stmt(int dst) |
| 480 |
{ |
| 481 |
expr(RType::I32, dst); |
| 482 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 483 |
Jump end = branchTest32(Zero, GPRInfo::regT0); |
| 484 |
stmt(dst); |
| 485 |
end.link(this); |
| 486 |
} |
| 487 |
|
| 488 |
void JITCompiler::if_else_stmt(int dst) |
| 489 |
{ |
| 490 |
expr(RType::I32, dst); |
| 491 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 492 |
Jump els = branchTest32(Zero, GPRInfo::regT0); |
| 493 |
stmt(dst); |
| 494 |
Jump end = jump(); |
| 495 |
els.link(this); |
| 496 |
stmt(dst); |
| 497 |
end.link(this); |
| 498 |
} |
| 499 |
|
| 500 |
void JITCompiler::while_stmt(int dst) |
| 501 |
{ |
| 502 |
JumpScope continueJumpScope(this, label()); |
| 503 |
pushContinueJumpScope(&continueJumpScope); |
| 504 |
|
| 505 |
JumpList breakJumpList; |
| 506 |
pushBreakJumpList(&breakJumpList); |
| 507 |
|
| 508 |
expr(RType::I32, dst); |
| 509 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 510 |
Jump end = branchTest32(Zero, GPRInfo::regT0); |
| 511 |
stmt(dst); |
| 512 |
continueJumpScope.jump(); |
| 513 |
end.link(this); |
| 514 |
breakJumpList.link(this); |
| 515 |
|
| 516 |
popBreakJumpList(); |
| 517 |
|
| 518 |
popContinueJumpScope(); |
| 519 |
} |
| 520 |
|
| 521 |
void JITCompiler::do_stmt(int dst) |
| 522 |
{ |
| 523 |
JumpScope continueJumpScope(this); |
| 524 |
pushContinueJumpScope(&continueJumpScope); |
| 525 |
|
| 526 |
JumpList breakJumpList; |
| 527 |
pushBreakJumpList(&breakJumpList); |
| 528 |
|
| 529 |
Label start = label(); |
| 530 |
stmt(dst); |
| 531 |
continueJumpScope.link(); |
| 532 |
expr(RType::I32, dst); |
| 533 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 534 |
Jump jumpToStart = branchTest32(NonZero, GPRInfo::regT0); |
| 535 |
jumpToStart.linkTo(start, this); |
| 536 |
breakJumpList.link(this); |
| 537 |
|
| 538 |
popBreakJumpList(); |
| 539 |
|
| 540 |
popContinueJumpScope(); |
| 541 |
} |
| 542 |
|
| 543 |
void JITCompiler::label_stmt(int dst) |
| 544 |
{ |
| 545 |
Label continueLabel = label(); |
| 546 |
JumpList breakJumpList; |
| 547 |
pushContinueLabelLabel(&continueLabel); |
| 548 |
pushBreakLabelJumpList(&breakJumpList); |
| 549 |
stmt(dst); |
| 550 |
breakJumpList.link(this); |
| 551 |
popBreakLabelJumpList(); |
| 552 |
popContinueLabelLabel(); |
| 553 |
} |
| 554 |
|
| 555 |
void JITCompiler::break_label_stmt() |
| 556 |
{ |
| 557 |
uint32_t index = m_read.immU32(); |
| 558 |
breakLabelJumpList(index)->append(jump()); |
| 559 |
} |
| 560 |
|
| 561 |
void JITCompiler::continue_label_stmt() |
| 562 |
{ |
| 563 |
uint32_t index = m_read.immU32(); |
| 564 |
jump(*continueLabelLabel(index)); |
| 565 |
} |
| 566 |
|
| 567 |
void JITCompiler::switch_stmt(int dst) |
| 568 |
{ |
| 569 |
JumpList breakJumpList; |
| 570 |
pushBreakJumpList(&breakJumpList); |
| 571 |
|
| 572 |
uint32_t numCases = m_read.immU32(); |
| 573 |
signed_expr(Signedness::Signed, dst); |
| 574 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 575 |
|
| 576 |
Jump table = jump(); |
| 577 |
|
| 578 |
Vector<Label> labels; |
| 579 |
Vector<int32_t> imms; |
| 580 |
Label defaultLabel; |
| 581 |
bool hasDefault = false; |
| 582 |
|
| 583 |
for (uint32_t i = 0; i < numCases; i++) { |
| 584 |
switch (m_read.switchCase()) { |
| 585 |
case SwitchCase::Case0: |
| 586 |
imms.append(m_read.immS32()); |
| 587 |
labels.append(label()); |
| 588 |
break; |
| 589 |
case SwitchCase::Case1: |
| 590 |
imms.append(m_read.immS32()); |
| 591 |
labels.append(label()); |
| 592 |
stmt(dst); |
| 593 |
break; |
| 594 |
case SwitchCase::CaseN: |
| 595 |
imms.append(m_read.immS32()); |
| 596 |
labels.append(label()); |
| 597 |
stmt_list(dst); |
| 598 |
break; |
| 599 |
case SwitchCase::Default0: |
| 600 |
defaultLabel = label(); |
| 601 |
hasDefault = true; |
| 602 |
break; |
| 603 |
case SwitchCase::Default1: |
| 604 |
defaultLabel = label(); |
| 605 |
hasDefault = true; |
| 606 |
stmt(dst); |
| 607 |
break; |
| 608 |
case SwitchCase::DefaultN: |
| 609 |
defaultLabel = label(); |
| 610 |
hasDefault = true; |
| 611 |
stmt_list(dst); |
| 612 |
break; |
| 613 |
default: |
| 614 |
abort(); |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
Jump end = jump(); |
| 619 |
table.link(this); |
| 620 |
|
| 621 |
for (size_t i = 0; i < labels.size(); ++i) { |
| 622 |
Jump j = branch32(Equal, GPRInfo::regT0, TrustedImm32(imms[i])); |
| 623 |
j.linkTo(labels[i], this); |
| 624 |
} |
| 625 |
if (hasDefault) { |
| 626 |
jump(defaultLabel); |
| 627 |
} |
| 628 |
|
| 629 |
end.link(this); |
| 630 |
|
| 631 |
breakJumpList.link(this); |
| 632 |
popBreakJumpList(); |
| 633 |
} |
| 634 |
|
| 635 |
void JITCompiler::expr(RType rType, int dst) |
| 636 |
{ |
| 637 |
I32 i32; |
| 638 |
I32WithImm i32_with_imm; |
| 639 |
uint8_t imm; |
| 640 |
|
| 641 |
if (rType != RType::I32) { |
| 642 |
RELEASE_ASSERT_NOT_REACHED(); |
| 643 |
} |
| 644 |
|
| 645 |
if (m_read.code(&i32, &i32_with_imm, &imm)) { |
| 646 |
switch (i32) { |
| 647 |
case I32::LitImm: |
| 648 |
store32(TrustedImm32(m_read.immU32()), temporaryAddress(dst)); |
| 649 |
break; |
| 650 |
case I32::LitPool: |
| 651 |
case I32::GetLoc: |
| 652 |
get_local(dst); // TODO: should pass I32 as RType |
| 653 |
break; |
| 654 |
case I32::GetGlo: |
| 655 |
RELEASE_ASSERT_NOT_REACHED(); |
| 656 |
case I32::SetLoc: |
| 657 |
set_local(dst); // TODO: should pass I32 as RType |
| 658 |
break; |
| 659 |
case I32::SetGlo: |
| 660 |
case I32::SLoad8: |
| 661 |
case I32::SLoadOff8: |
| 662 |
case I32::ULoad8: |
| 663 |
case I32::ULoadOff8: |
| 664 |
case I32::SLoad16: |
| 665 |
case I32::SLoadOff16: |
| 666 |
case I32::ULoad16: |
| 667 |
case I32::ULoadOff16: |
| 668 |
case I32::Load32: |
| 669 |
case I32::LoadOff32: |
| 670 |
case I32::Store8: |
| 671 |
case I32::StoreOff8: |
| 672 |
case I32::Store16: |
| 673 |
case I32::StoreOff16: |
| 674 |
case I32::Store32: |
| 675 |
case I32::StoreOff32: |
| 676 |
case I32::CallInt: |
| 677 |
case I32::CallInd: |
| 678 |
case I32::CallImp: |
| 679 |
RELEASE_ASSERT_NOT_REACHED(); |
| 680 |
case I32::Cond: |
| 681 |
cond(RType::I32, dst); |
| 682 |
break; |
| 683 |
case I32::Comma: |
| 684 |
comma(RType::I32, dst); |
| 685 |
break; |
| 686 |
case I32::FromF32: |
| 687 |
case I32::FromF64: |
| 688 |
case I32::Neg: |
| 689 |
RELEASE_ASSERT_NOT_REACHED(); |
| 690 |
case I32::Add: |
| 691 |
add_sub('+', RType::I32, dst); |
| 692 |
break; |
| 693 |
case I32::Sub: |
| 694 |
add_sub('-', RType::I32, dst); |
| 695 |
break; |
| 696 |
case I32::Mul: |
| 697 |
mul_i32(dst); |
| 698 |
break; |
| 699 |
case I32::SDiv: |
| 700 |
case I32::UDiv: |
| 701 |
case I32::SMod: |
| 702 |
case I32::UMod: |
| 703 |
case I32::BitNot: |
| 704 |
case I32::BitOr: |
| 705 |
case I32::BitAnd: |
| 706 |
case I32::BitXor: |
| 707 |
case I32::Lsh: |
| 708 |
case I32::ArithRsh: |
| 709 |
case I32::LogicRsh: |
| 710 |
case I32::Clz: |
| 711 |
case I32::LogicNot: |
| 712 |
RELEASE_ASSERT_NOT_REACHED(); |
| 713 |
case I32::EqI32: |
| 714 |
rel_i32(Signedness::Signed, "==", dst); |
| 715 |
break; |
| 716 |
case I32::EqF32: |
| 717 |
case I32::EqF64: |
| 718 |
RELEASE_ASSERT_NOT_REACHED(); |
| 719 |
case I32::NEqI32: |
| 720 |
rel_i32(Signedness::Signed, "!=", dst); |
| 721 |
break; |
| 722 |
case I32::NEqF32: |
| 723 |
case I32::NEqF64: |
| 724 |
RELEASE_ASSERT_NOT_REACHED(); |
| 725 |
case I32::SLeThI32: |
| 726 |
rel_i32(Signedness::Signed, "<", dst); |
| 727 |
break; |
| 728 |
case I32::ULeThI32: |
| 729 |
rel_i32(Signedness::Unsigned, "<", dst); |
| 730 |
break; |
| 731 |
case I32::LeThF32: |
| 732 |
case I32::LeThF64: |
| 733 |
RELEASE_ASSERT_NOT_REACHED(); |
| 734 |
case I32::SLeEqI32: |
| 735 |
rel_i32(Signedness::Signed, "<=", dst); |
| 736 |
break; |
| 737 |
case I32::ULeEqI32: |
| 738 |
rel_i32(Signedness::Unsigned, "<=", dst); |
| 739 |
break; |
| 740 |
case I32::LeEqF32: |
| 741 |
case I32::LeEqF64: |
| 742 |
RELEASE_ASSERT_NOT_REACHED(); |
| 743 |
case I32::SGrThI32: |
| 744 |
rel_i32(Signedness::Signed, ">", dst); |
| 745 |
break; |
| 746 |
case I32::UGrThI32: |
| 747 |
rel_i32(Signedness::Unsigned, ">", dst); |
| 748 |
break; |
| 749 |
case I32::GrThF32: |
| 750 |
case I32::GrThF64: |
| 751 |
RELEASE_ASSERT_NOT_REACHED(); |
| 752 |
case I32::SGrEqI32: |
| 753 |
rel_i32(Signedness::Signed, ">=", dst); |
| 754 |
break; |
| 755 |
case I32::UGrEqI32: |
| 756 |
rel_i32(Signedness::Unsigned, ">=", dst); |
| 757 |
break; |
| 758 |
case I32::GrEqF32: |
| 759 |
case I32::GrEqF64: |
| 760 |
case I32::SMin: |
| 761 |
case I32::UMin: |
| 762 |
case I32::SMax: |
| 763 |
case I32::UMax: |
| 764 |
case I32::Abs: |
| 765 |
default: |
| 766 |
RELEASE_ASSERT_NOT_REACHED(); |
| 767 |
} |
| 768 |
} else { |
| 769 |
switch (i32_with_imm) { |
| 770 |
case I32WithImm::LitImm: |
| 771 |
store32(TrustedImm32(imm), temporaryAddress(dst)); |
| 772 |
break; |
| 773 |
case I32WithImm::LitPool: |
| 774 |
store32(TrustedImm32(m_i32s[imm]), temporaryAddress(dst)); |
| 775 |
break; |
| 776 |
case I32WithImm::GetLoc: |
| 777 |
load32(stackAddress(imm), GPRInfo::regT0); |
| 778 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 779 |
break; |
| 780 |
default: |
| 781 |
RELEASE_ASSERT_NOT_REACHED(); |
| 782 |
} |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
void JITCompiler::signed_expr(Signedness signedness, int dst) |
| 787 |
{ |
| 788 |
uint32_t u32; |
| 789 |
if (m_read.ifI32Lit(m_i32s, &u32)) { |
| 790 |
if (signedness == Signedness::Signed) { |
| 791 |
store32(TrustedImm32(u32), temporaryAddress(dst)); |
| 792 |
} else { |
| 793 |
store32(TrustedImm32(u32), temporaryAddress(dst)); // FIXME: unsigned |
| 794 |
//RELEASE_ASSERT_NOT_REACHED(); |
| 795 |
} |
| 796 |
} else if (signedness == Signedness::Signed) { |
| 797 |
//expr<RType::I32>(Ctx::ToI32); |
| 798 |
//s.write().ascii("|0"); |
| 799 |
expr(RType::I32, dst); |
| 800 |
} else { |
| 801 |
//expr<RType::I32>(Ctx::ToI32); |
| 802 |
//s.write().ascii(">>>0"); |
| 803 |
expr(RType::I32, dst); // FIXME: unsigned |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
void JITCompiler::add_sub(char op, RType rType, int dst) |
| 808 |
{ |
| 809 |
expr(rType, dst); |
| 810 |
expr(rType, dst + 1); |
| 811 |
if (op == '+') { |
| 812 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 813 |
load32(temporaryAddress(dst + 1), GPRInfo::regT1); |
| 814 |
add32(GPRInfo::regT1, GPRInfo::regT0); |
| 815 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 816 |
} else { |
| 817 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 818 |
load32(temporaryAddress(dst + 1), GPRInfo::regT1); |
| 819 |
sub32(GPRInfo::regT1, GPRInfo::regT0); |
| 820 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
void JITCompiler::mul_i32(int dst) |
| 825 |
{ |
| 826 |
expr(RType::I32, dst); |
| 827 |
expr(RType::I32, dst + 1); |
| 828 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 829 |
load32(temporaryAddress(dst + 1), GPRInfo::regT1); |
| 830 |
mul32(GPRInfo::regT1, GPRInfo::regT0); |
| 831 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 832 |
} |
| 833 |
|
| 834 |
void JITCompiler::rel_i32(Signedness signedness, const char* op, int dst) |
| 835 |
{ |
| 836 |
signed_expr(signedness, dst); |
| 837 |
signed_expr(signedness, dst + 1); |
| 838 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 839 |
load32(temporaryAddress(dst + 1), GPRInfo::regT1); |
| 840 |
RelationalCondition condition; |
| 841 |
if (op[0] == '=' && op[1] == '=' && op[2] == 0) { |
| 842 |
condition = Equal; |
| 843 |
} else if (op[0] == '!' && op[1] == '=' && op[2] == 0) { |
| 844 |
condition = NotEqual; |
| 845 |
} else if (op[0] == '>' && op[1] == 0) { |
| 846 |
condition = GreaterThan; |
| 847 |
} else if (op[0] == '<' && op[1] == 0) { |
| 848 |
condition = LessThan; |
| 849 |
} else if (op[0] == '>' && op[1] == '=' && op[2] == 0) { |
| 850 |
condition = GreaterThanOrEqual; |
| 851 |
} else if (op[0] == '<' && op[1] == '=' && op[2] == 0) { |
| 852 |
condition = LessThanOrEqual; |
| 853 |
} else { |
| 854 |
RELEASE_ASSERT_NOT_REACHED(); |
| 855 |
} |
| 856 |
compare32(condition, GPRInfo::regT0, GPRInfo::regT1, GPRInfo::regT0); |
| 857 |
store32(GPRInfo::regT0, temporaryAddress(dst)); |
| 858 |
} |
| 859 |
|
| 860 |
void JITCompiler::comma(RType rType, int dst) |
| 861 |
{ |
| 862 |
expr(m_read.rtype(), dst); |
| 863 |
expr(rType, dst); |
| 864 |
} |
| 865 |
|
| 866 |
void JITCompiler::cond(RType rType, int dst) |
| 867 |
{ |
| 868 |
expr(RType::I32, dst); |
| 869 |
load32(temporaryAddress(dst), GPRInfo::regT0); |
| 870 |
Jump els = branchTest32(Zero, GPRInfo::regT0); |
| 871 |
expr(rType, dst); |
| 872 |
Jump end = jump(); |
| 873 |
els.link(this); |
| 874 |
expr(rType, dst); |
| 875 |
end.link(this); |
| 876 |
} |
| 877 |
|
| 878 |
JSValue compileAndRun(ExecState* exec, const Vector<char>& buffer) |
| 879 |
{ |
| 880 |
JITCompiler compiler(&exec->vm()); |
| 881 |
return compiler.compileAndRun(exec, buffer); |
| 882 |
} |
| 883 |
|
| 884 |
} } // namespace JSC::Wasm |