fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | 5e0bc743 | 2017-10-08 01:10:19 +0000 | [diff] [blame] | 2 | * Copyright (C) 2013-2017 Apple Inc. All rights reserved. |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 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 "FTLOSREntry.h" |
| 28 | |
| 29 | #include "CallFrame.h" |
| 30 | #include "CodeBlock.h" |
| 31 | #include "DFGJITCode.h" |
| 32 | #include "FTLForOSREntryJITCode.h" |
commit-queue@webkit.org | a936a26 | 2014-01-14 15:20:19 +0000 | [diff] [blame] | 33 | #include "OperandsInlines.h" |
fpizlo@apple.com | fb7eff2 | 2014-02-11 01:45:50 +0000 | [diff] [blame] | 34 | #include "JSCInlines.h" |
mark.lam@apple.com | 504b1cc | 2016-07-11 20:48:15 +0000 | [diff] [blame] | 35 | #include "VMInlines.h" |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 36 | |
| 37 | #if ENABLE(FTL_JIT) |
| 38 | |
| 39 | namespace JSC { namespace FTL { |
| 40 | |
ap@apple.com | 5e7b4d5 | 2015-08-11 23:50:02 +0000 | [diff] [blame] | 41 | SUPPRESS_ASAN |
commit-queue@webkit.org | 485b53c | 2017-01-27 01:33:38 +0000 | [diff] [blame] | 42 | void* prepareOSREntry( |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 43 | ExecState* exec, CodeBlock* dfgCodeBlock, CodeBlock* entryCodeBlock, |
| 44 | unsigned bytecodeIndex, unsigned streamIndex) |
| 45 | { |
| 46 | VM& vm = exec->vm(); |
| 47 | CodeBlock* baseline = dfgCodeBlock->baselineVersion(); |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 48 | ExecutableBase* executable = dfgCodeBlock->ownerExecutable(); |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 49 | DFG::JITCode* dfgCode = dfgCodeBlock->jitCode()->dfg(); |
| 50 | ForOSREntryJITCode* entryCode = entryCodeBlock->jitCode()->ftlForOSREntry(); |
sbarati@apple.com | b1b3857 | 2019-05-07 17:41:42 +0000 | [diff] [blame] | 51 | |
| 52 | if (!entryCode->dfgCommon()->isStillValid) { |
| 53 | dfgCode->clearOSREntryBlockAndResetThresholds(dfgCodeBlock); |
| 54 | return 0; |
| 55 | } |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 56 | |
| 57 | if (Options::verboseOSR()) { |
| 58 | dataLog( |
| 59 | "FTL OSR from ", *dfgCodeBlock, " to ", *entryCodeBlock, " at bc#", |
| 60 | bytecodeIndex, ".\n"); |
| 61 | } |
| 62 | |
fpizlo@apple.com | 2c4a7e9 | 2014-08-06 05:27:46 +0000 | [diff] [blame] | 63 | if (bytecodeIndex) |
| 64 | jsCast<ScriptExecutable*>(executable)->setDidTryToEnterInLoop(true); |
| 65 | |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 66 | if (bytecodeIndex != entryCode->bytecodeIndex()) { |
| 67 | if (Options::verboseOSR()) |
fpizlo@apple.com | 2c4a7e9 | 2014-08-06 05:27:46 +0000 | [diff] [blame] | 68 | dataLog(" OSR failed because we don't have an entrypoint for bc#", bytecodeIndex, "; ours is for bc#", entryCode->bytecodeIndex(), "\n"); |
commit-queue@webkit.org | 485b53c | 2017-01-27 01:33:38 +0000 | [diff] [blame] | 69 | return 0; |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 70 | } |
| 71 | |
ysuzuki@apple.com | 76c0116 | 2019-02-28 06:25:23 +0000 | [diff] [blame] | 72 | Operands<Optional<JSValue>> values; |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 73 | dfgCode->reconstruct( |
| 74 | exec, dfgCodeBlock, CodeOrigin(bytecodeIndex), streamIndex, values); |
| 75 | |
| 76 | if (Options::verboseOSR()) |
| 77 | dataLog(" Values at entry: ", values, "\n"); |
| 78 | |
commit-queue@webkit.org | 2cb9c25 | 2016-12-13 19:38:13 +0000 | [diff] [blame] | 79 | for (int argument = values.numberOfArguments(); argument--;) { |
ap@apple.com | 5e7b4d5 | 2015-08-11 23:50:02 +0000 | [diff] [blame] | 80 | JSValue valueOnStack = exec->r(virtualRegisterForArgument(argument).offset()).asanUnsafeJSValue(); |
ysuzuki@apple.com | 76c0116 | 2019-02-28 06:25:23 +0000 | [diff] [blame] | 81 | Optional<JSValue> reconstructedValue = values.argument(argument); |
| 82 | if ((reconstructedValue && valueOnStack == reconstructedValue.value()) || !argument) |
fpizlo@apple.com | a14dfc0 | 2014-01-30 23:00:16 +0000 | [diff] [blame] | 83 | continue; |
rmorisset@apple.com | ab64b6c | 2017-10-09 16:49:13 +0000 | [diff] [blame] | 84 | dataLog("Mismatch between reconstructed values and the value on the stack for argument arg", argument, " for ", *entryCodeBlock, " at bc#", bytecodeIndex, ":\n"); |
fpizlo@apple.com | a14dfc0 | 2014-01-30 23:00:16 +0000 | [diff] [blame] | 85 | dataLog(" Value on stack: ", valueOnStack, "\n"); |
| 86 | dataLog(" Reconstructed value: ", reconstructedValue, "\n"); |
| 87 | RELEASE_ASSERT_NOT_REACHED(); |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | RELEASE_ASSERT( |
utatane.tea@gmail.com | 90754e2 | 2018-07-08 17:59:25 +0000 | [diff] [blame] | 91 | static_cast<int>(values.numberOfLocals()) == baseline->numCalleeLocals()); |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 92 | |
| 93 | EncodedJSValue* scratch = static_cast<EncodedJSValue*>( |
| 94 | entryCode->entryBuffer()->dataBuffer()); |
| 95 | |
ysuzuki@apple.com | 76c0116 | 2019-02-28 06:25:23 +0000 | [diff] [blame] | 96 | for (int local = values.numberOfLocals(); local--;) { |
| 97 | Optional<JSValue> value = values.local(local); |
| 98 | if (value) |
| 99 | scratch[local] = JSValue::encode(value.value()); |
| 100 | else |
| 101 | scratch[local] = JSValue::encode(JSValue()); |
| 102 | } |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 103 | |
fpizlo@apple.com | 81bb8bb | 2013-11-23 00:36:55 +0000 | [diff] [blame] | 104 | int stackFrameSize = entryCode->common.requiredRegisterCountForExecutionAndExit(); |
mark.lam@apple.com | 504b1cc | 2016-07-11 20:48:15 +0000 | [diff] [blame] | 105 | if (UNLIKELY(!vm.ensureStackCapacityFor(&exec->registers()[virtualRegisterForLocal(stackFrameSize - 1).offset()]))) { |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 106 | if (Options::verboseOSR()) |
fpizlo@apple.com | 81bb8bb | 2013-11-23 00:36:55 +0000 | [diff] [blame] | 107 | dataLog(" OSR failed because stack growth failed.\n"); |
commit-queue@webkit.org | 485b53c | 2017-01-27 01:33:38 +0000 | [diff] [blame] | 108 | return 0; |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | exec->setCodeBlock(entryCodeBlock); |
commit-queue@webkit.org | 2cb9c25 | 2016-12-13 19:38:13 +0000 | [diff] [blame] | 112 | |
| 113 | void* result = entryCode->addressForCall(ArityCheckNotRequired).executableAddress(); |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 114 | if (Options::verboseOSR()) |
fpizlo@apple.com | 080f528 | 2017-03-16 21:19:23 +0000 | [diff] [blame] | 115 | dataLog(" Entry will succeed, going to address ", RawPointer(result), "\n"); |
fpizlo@apple.com | 532f1e5 | 2013-09-04 06:26:04 +0000 | [diff] [blame] | 116 | |
| 117 | return result; |
| 118 | } |
| 119 | |
| 120 | } } // namespace JSC::FTL |
| 121 | |
| 122 | #endif // ENABLE(FTL_JIT) |
| 123 | |
| 124 | |