PageRenderTime 37ms CodeModel.GetById 20ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/js/src/methodjit/TrampolineCompiler.cpp

http://github.com/zpao/v8monkey
C++ | 158 lines | 86 code | 19 blank | 53 comment | 9 complexity | d875fd01be47497e735997b4f0b1fd87 MD5 | raw file
  1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2 * vim: set ts=4 sw=4 et tw=99:
  3 *
  4 * ***** BEGIN LICENSE BLOCK *****
  5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  6 *
  7 * The contents of this file are subject to the Mozilla Public License Version
  8 * 1.1 (the "License"); you may not use this file except in compliance with
  9 * the License. You may obtain a copy of the License at
 10 * http://www.mozilla.org/MPL/
 11 *
 12 * Software distributed under the License is distributed on an "AS IS" basis,
 13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 14 * for the specific language governing rights and limitations under the
 15 * License.
 16 *
 17 * The Original Code is Mozilla Jaegermonkey.
 18 *
 19 * The Initial Developer of the Original Code is the Mozilla Foundation.
 20 *
 21 * Portions created by the Initial Developer are Copyright (C) 2010
 22 * the Initial Developer. All Rights Reserved.
 23 *
 24 * Contributor(s):
 25 *   Andrew Drake <drakedevel@gmail.com>
 26 *
 27 * Alternatively, the contents of this file may be used under the terms of
 28 * either the GNU General Public License Version 2 or later (the "GPL"), or
 29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 30 * in which case the provisions of the GPL or the LGPL are applicable instead
 31 * of those above. If you wish to allow use of your version of this file only
 32 * under the terms of either the GPL or the LGPL, and not to allow others to
 33 * use your version of this file under the terms of the MPL, indicate your
 34 * decision by deleting the provisions above and replace them with the notice
 35 * and other provisions required by the GPL or the LGPL. If you do not delete
 36 * the provisions above, a recipient may use your version of this file under
 37 * the terms of any one of the MPL, the GPL or the LGPL.
 38 *
 39 * ***** END LICENSE BLOCK ***** */
 40
 41#include "TrampolineCompiler.h"
 42#include "StubCalls.h"
 43#include "assembler/assembler/LinkBuffer.h"
 44#include "assembler/jit/ExecutableAllocator.h"
 45
 46namespace js {
 47namespace mjit {
 48
 49#define CHECK_RESULT(x) if (!(x)) return false
 50#define COMPILE(which, pool, how) CHECK_RESULT(compileTrampoline(&(which), &pool, how))
 51#define RELEASE(which, pool) JS_BEGIN_MACRO \
 52    which = NULL;                           \
 53    if (pool)                               \
 54        pool->release();                    \
 55    pool = NULL;                            \
 56JS_END_MACRO
 57
 58typedef JSC::MacroAssembler::Address Address;
 59typedef JSC::MacroAssembler::Label Label;
 60typedef JSC::MacroAssembler::Jump Jump;
 61typedef JSC::MacroAssembler::ImmPtr ImmPtr;
 62typedef JSC::MacroAssembler::Imm32 Imm32;
 63typedef JSC::MacroAssembler::Address Address;
 64
 65bool
 66TrampolineCompiler::compile()
 67{
 68#ifdef JS_METHODJIT_SPEW
 69    JMCheckLogging();
 70#endif
 71
 72    COMPILE(trampolines->forceReturn, trampolines->forceReturnPool, generateForceReturn);
 73#if (defined(JS_NO_FASTCALL) && defined(JS_CPU_X86)) || defined(_WIN64)
 74    COMPILE(trampolines->forceReturnFast, trampolines->forceReturnFastPool, generateForceReturnFast);
 75#endif
 76
 77    return true;
 78}
 79
 80void
 81TrampolineCompiler::release(Trampolines *tramps)
 82{
 83    RELEASE(tramps->forceReturn, tramps->forceReturnPool);
 84#if (defined(JS_NO_FASTCALL) && defined(JS_CPU_X86)) || defined(_WIN64)
 85    RELEASE(tramps->forceReturnFast, tramps->forceReturnFastPool);
 86#endif
 87}
 88
 89bool
 90TrampolineCompiler::compileTrampoline(Trampolines::TrampolinePtr *where,
 91                                      JSC::ExecutablePool **poolp, TrampolineGenerator generator)
 92{
 93    Assembler masm;
 94
 95    Label entry = masm.label();
 96    CHECK_RESULT(generator(masm));
 97    JS_ASSERT(entry.isSet());
 98
 99    bool ok;
100    JSC::LinkBuffer buffer(&masm, execAlloc, poolp, &ok, JSC::METHOD_CODE);
101    if (!ok) 
102        return false;
103    masm.finalize(buffer);
104    uint8_t *result = (uint8_t*)buffer.finalizeCodeAddendum().dataLocation();
105    *where = JS_DATA_TO_FUNC_PTR(Trampolines::TrampolinePtr, result + masm.distanceOf(entry));
106
107    return true;
108}
109
110/*
111 * This is shamelessly copied from emitReturn, but with several changes:
112 * - There was always at least one inline call.
113 * - We don't know if there are activation objects or a script with nesting
114 *   state whose active frames need adjustment, so we always stub the epilogue.
115 * - We don't know where we came from, so we don't know frame depth or PC.
116 * - There is no stub buffer.
117 */
118bool
119TrampolineCompiler::generateForceReturn(Assembler &masm)
120{
121    /* The JSStackFrame register may have been clobbered while returning, reload it. */
122    masm.loadPtr(FrameAddress(VMFrame::offsetOfFp), JSFrameReg);
123
124    /* Perform the frame epilogue. */
125    masm.fallibleVMCall(true, JS_FUNC_TO_DATA_PTR(void *, stubs::AnyFrameEpilogue), NULL, NULL, 0);
126
127    /* Store any known return value */
128    masm.loadValueAsComponents(UndefinedValue(), JSReturnReg_Type, JSReturnReg_Data);
129    Jump rvalClear = masm.branchTest32(Assembler::Zero,
130                                       FrameFlagsAddress(), Imm32(StackFrame::HAS_RVAL));
131    Address rvalAddress(JSFrameReg, StackFrame::offsetOfReturnValue());
132    masm.loadValueAsComponents(rvalAddress, JSReturnReg_Type, JSReturnReg_Data);
133    rvalClear.linkTo(masm.label(), &masm);
134
135    /* Return to the caller */
136    masm.loadPtr(Address(JSFrameReg, StackFrame::offsetOfNcode()), Registers::ReturnReg);
137    masm.jump(Registers::ReturnReg);
138    return true;
139}
140
141#if (defined(JS_NO_FASTCALL) && defined(JS_CPU_X86)) || defined(_WIN64)
142bool
143TrampolineCompiler::generateForceReturnFast(Assembler &masm)
144{
145#ifdef _WIN64
146    masm.addPtr(Imm32(32), Registers::StackPointer);
147#else
148    // In case of no fast call, when we change the return address,
149    // we need to make sure add esp by 8.
150    masm.addPtr(Imm32(16), Registers::StackPointer);
151#endif
152    return generateForceReturn(masm);
153}
154#endif
155
156} /* namespace mjit */
157} /* namespace js */
158