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/. */
7 #include "jit/Linker.h"
9 #include "jit/JitZone.h"
10 #include "util/Memory.h"
12 #include "gc/StoreBuffer-inl.h"
17 JitCode
* Linker::newCode(JSContext
* cx
, CodeKind kind
) {
18 JS::AutoAssertNoGC
nogc(cx
);
23 static const size_t ExecutableAllocatorAlignment
= sizeof(void*);
24 static_assert(CodeAlignment
>= ExecutableAllocatorAlignment
,
25 "Unexpected alignment requirements");
27 // We require enough bytes for the code, header, and worst-case alignment
29 size_t bytesNeeded
= masm
.bytesNeeded() + sizeof(JitCodeHeader
) +
30 (CodeAlignment
- ExecutableAllocatorAlignment
);
31 if (bytesNeeded
>= MAX_BUFFER_SIZE
) {
35 // ExecutableAllocator requires bytesNeeded to be aligned.
36 bytesNeeded
= AlignBytes(bytesNeeded
, ExecutableAllocatorAlignment
);
38 JitZone
* jitZone
= cx
->zone()->getJitZone(cx
);
40 // Note: don't call fail(cx) here, getJitZone reports OOM.
46 (uint8_t*)jitZone
->execAlloc().alloc(cx
, bytesNeeded
, &pool
, kind
);
51 // The JitCodeHeader will be stored right before the code buffer.
52 uint8_t* codeStart
= result
+ sizeof(JitCodeHeader
);
54 // Bump the code up to a nice alignment.
55 codeStart
= (uint8_t*)AlignBytes((uintptr_t)codeStart
, CodeAlignment
);
56 MOZ_ASSERT(codeStart
+ masm
.bytesNeeded() <= result
+ bytesNeeded
);
57 uint32_t headerSize
= codeStart
- result
;
59 JitCode::New
<NoGC
>(cx
, codeStart
, bytesNeeded
, headerSize
, pool
, kind
);
66 awjcf
.emplace(result
, bytesNeeded
);
67 if (!awjcf
->makeWritable()) {
72 if (masm
.embedsNurseryPointers()) {
73 cx
->runtime()->gc
.storeBuffer().putWholeCell(code
);