author Jan de Mooij <jdemooij@mozilla.com>
Tue, 28 Jan 2014 15:33:56 +0100
changeset 181628 fe720ff03f43f96d4142491820884c9d3dae5de6
parent 177184 3c5539b5b8f52a90f4ba6200b1790b02ee0b1261
child 190044 53139214dcf3fc3b886bb913a2b4b301e5e70bf4
permissions -rw-r--r--
Bug 959597 part 2 - Use JS_CODEGEN_* instead of JS_CPU_* for JIT backend code. r=nbp

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef jit_IonLinker_h
#define jit_IonLinker_h

#include "jscntxt.h"
#include "jscompartment.h"
#include "jsgc.h"

#include "assembler/jit/ExecutableAllocator.h"
#include "jit/IonCode.h"
#include "jit/IonMacroAssembler.h"
#include "jit/JitCompartment.h"

namespace js {
namespace jit {

class Linker
    MacroAssembler &masm;

    JitCode *fail(JSContext *cx) {
        return nullptr;

    template <AllowGC allowGC>
    JitCode *newCode(JSContext *cx, JSC::ExecutableAllocator *execAlloc, JSC::CodeKind kind) {
        JS_ASSERT(kind == JSC::ION_CODE ||
                  kind == JSC::BASELINE_CODE ||
                  kind == JSC::OTHER_CODE);
        JS_ASSERT(masm.numAsmJSAbsoluteLinks() == 0);

        gc::AutoSuppressGC suppressGC(cx);
        if (masm.oom())
            return fail(cx);

        JSC::ExecutablePool *pool;
        size_t bytesNeeded = masm.bytesNeeded() + sizeof(JitCode *) + CodeAlignment;
        if (bytesNeeded >= MAX_BUFFER_SIZE)
            return fail(cx);

        uint8_t *result = (uint8_t *)execAlloc->alloc(bytesNeeded, &pool, kind);
        if (!result)
            return fail(cx);

        // The JitCode pointer will be stored right before the code buffer.
        uint8_t *codeStart = result + sizeof(JitCode *);

        // Bump the code up to a nice alignment.
        codeStart = (uint8_t *)AlignBytes((uintptr_t)codeStart, CodeAlignment);
        uint32_t headerSize = codeStart - result;
        JitCode *code = JitCode::New<allowGC>(cx, codeStart,
                                              bytesNeeded - headerSize, pool);
        if (!code)
            return nullptr;
        if (masm.oom())
            return fail(cx);
        if (masm.embedsNurseryPointers())
        return code;

    Linker(MacroAssembler &masm)
      : masm(masm)

    template <AllowGC allowGC>
    JitCode *newCode(JSContext *cx, JSC::CodeKind kind) {
        return newCode<allowGC>(cx, cx->compartment()->jitCompartment()->execAlloc(), kind);

    JitCode *newCodeForIonScript(JSContext *cx) {
        // ARM does not yet use implicit interrupt checks, see bug 864220.
        return newCode<CanGC>(cx, JSC::ION_CODE);
        // The caller must lock the runtime against operation callback triggers,
        // as the triggering thread may use the executable allocator below.

        JSC::ExecutableAllocator *alloc = cx->runtime()->jitRuntime()->getIonAlloc(cx);
        if (!alloc)
            return nullptr;

        return newCode<CanGC>(cx, alloc, JSC::ION_CODE);

} // namespace jit
} // namespace js

#endif /* jit_IonLinker_h */