| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "jit/JitContext.h" |
| 8 |
|
| 9 |
#include "mozilla/Assertions.h" |
| 10 |
#include "mozilla/ThreadLocal.h" |
| 11 |
|
| 12 |
#include <stdlib.h> |
| 13 |
|
| 14 |
#include "jit/CacheIRSpewer.h" |
| 15 |
#include "jit/CompileWrappers.h" |
| 16 |
#include "jit/Ion.h" |
| 17 |
#include "jit/JitCode.h" |
| 18 |
#include "jit/JitOptions.h" |
| 19 |
#include "jit/JitSpewer.h" |
| 20 |
#include "jit/MacroAssembler.h" |
| 21 |
#include "jit/PerfSpewer.h" |
| 22 |
#include "js/HeapAPI.h" |
| 23 |
#include "vm/JSContext.h" |
| 24 |
|
| 25 |
#ifdef JS_CODEGEN_ARM64 |
| 26 |
# include "jit/arm64/vixl/Cpu-vixl.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
#if defined(ANDROID) |
| 30 |
# include <sys/system_properties.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
using namespace js; |
| 34 |
using namespace js::jit; |
| 35 |
|
| 36 |
namespace js::jit { |
| 37 |
class TempAllocator; |
| 38 |
} |
| 39 |
|
| 40 |
// Assert that JitCode is gc::Cell aligned. |
| 41 |
static_assert(sizeof(JitCode) % gc::CellAlignBytes == 0); |
| 42 |
|
| 43 |
static MOZ_THREAD_LOCAL(JitContext*) TlsJitContext; |
| 44 |
|
| 45 |
static JitContext* CurrentJitContext() { |
| 46 |
if (!TlsJitContext.init()) { |
| 47 |
return nullptr; |
| 48 |
} |
| 49 |
return TlsJitContext.get(); |
| 50 |
} |
| 51 |
|
| 52 |
void jit::SetJitContext(JitContext* ctx) { |
| 53 |
MOZ_ASSERT(!TlsJitContext.get()); |
| 54 |
TlsJitContext.set(ctx); |
| 55 |
} |
| 56 |
|
| 57 |
JitContext* jit::GetJitContext() { |
| 58 |
MOZ_ASSERT(CurrentJitContext()); |
| 59 |
return CurrentJitContext(); |
| 60 |
} |
| 61 |
|
| 62 |
JitContext* jit::MaybeGetJitContext() { return CurrentJitContext(); } |
| 63 |
|
| 64 |
JitContext::JitContext(CompileRuntime* rt) : runtime(rt) { |
| 65 |
MOZ_ASSERT(rt); |
| 66 |
SetJitContext(this); |
| 67 |
} |
| 68 |
|
| 69 |
JitContext::JitContext(JSContext* cx) |
| 70 |
: cx(cx), runtime(CompileRuntime::get(cx->runtime())) { |
| 71 |
SetJitContext(this); |
| 72 |
} |
| 73 |
|
| 74 |
JitContext::JitContext() { |
| 75 |
#ifdef DEBUG |
| 76 |
isCompilingWasm_ = true; |
| 77 |
#endif |
| 78 |
SetJitContext(this); |
| 79 |
} |
| 80 |
|
| 81 |
JitContext::~JitContext() { |
| 82 |
MOZ_ASSERT(TlsJitContext.get() == this); |
| 83 |
TlsJitContext.set(nullptr); |
| 84 |
} |
| 85 |
|
| 86 |
bool jit::InitializeJit() { |
| 87 |
if (!TlsJitContext.init()) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
CheckLogging(); |
| 92 |
|
| 93 |
#ifdef JS_CACHEIR_SPEW |
| 94 |
const char* env = getenv("CACHEIR_LOGS"); |
| 95 |
if (env && env[0] && env[0] != '0') { |
| 96 |
CacheIRSpewer::singleton().init(env); |
| 97 |
} |
| 98 |
#endif |
| 99 |
|
| 100 |
#if defined(JS_CODEGEN_X86) || defined(JS_CODEGEN_X64) |
| 101 |
// Compute flags. |
| 102 |
js::jit::CPUInfo::ComputeFlags(); |
| 103 |
#endif |
| 104 |
|
| 105 |
#if defined(JS_CODEGEN_ARM) |
| 106 |
ARMFlags::Init(); |
| 107 |
#endif |
| 108 |
|
| 109 |
#ifdef JS_CODEGEN_ARM64 |
| 110 |
// Initialize instruction cache flushing. |
| 111 |
vixl::CPU::SetUp(); |
| 112 |
#endif |
| 113 |
|
| 114 |
#ifndef JS_CODEGEN_NONE |
| 115 |
MOZ_ASSERT(js::jit::CPUFlagsHaveBeenComputed()); |
| 116 |
#endif |
| 117 |
|
| 118 |
// Note: jit flags need to be initialized after the ARMFlags::Init call above. |
| 119 |
// This is the final point where we can set disableJitBackend = true, before |
| 120 |
// we use this flag below with the HasJitBackend call. |
| 121 |
if (!MacroAssembler::SupportsFloatingPoint()) { |
| 122 |
JitOptions.disableJitBackend = true; |
| 123 |
} |
| 124 |
JitOptions.supportsUnalignedAccesses = |
| 125 |
MacroAssembler::SupportsUnalignedAccesses(); |
| 126 |
|
| 127 |
if (HasJitBackend()) { |
| 128 |
if (!InitProcessExecutableMemory()) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
PerfSpewer::Init(); |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
void jit::ShutdownJit() { |
| 138 |
if (HasJitBackend() && !JSRuntime::hasLiveRuntimes()) { |
| 139 |
ReleaseProcessExecutableMemory(); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
bool jit::JitSupportsWasmSimd() { |
| 144 |
#if defined(ENABLE_WASM_SIMD) |
| 145 |
return js::jit::MacroAssembler::SupportsWasmSimd(); |
| 146 |
#else |
| 147 |
return false; |
| 148 |
#endif |
| 149 |
} |
| 150 |
|
| 151 |
bool jit::JitSupportsAtomics() { |
| 152 |
#if defined(JS_CODEGEN_ARM) |
| 153 |
// Bug 1146902, bug 1077318: Enable Ion inlining of Atomics |
| 154 |
// operations on ARM only when the CPU has byte, halfword, and |
| 155 |
// doubleword load-exclusive and store-exclusive instructions, |
| 156 |
// until we can add support for systems that don't have those. |
| 157 |
return js::jit::ARMFlags::HasLDSTREXBHD(); |
| 158 |
#else |
| 159 |
return true; |
| 160 |
#endif |
| 161 |
} |