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 :
46 : namespace js {
47 : namespace 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; \
56 : JS_END_MACRO
57 :
58 : typedef JSC::MacroAssembler::Address Address;
59 : typedef JSC::MacroAssembler::Label Label;
60 : typedef JSC::MacroAssembler::Jump Jump;
61 : typedef JSC::MacroAssembler::ImmPtr ImmPtr;
62 : typedef JSC::MacroAssembler::Imm32 Imm32;
63 : typedef JSC::MacroAssembler::Address Address;
64 :
65 : bool
66 11826 : TrampolineCompiler::compile()
67 : {
68 : #ifdef JS_METHODJIT_SPEW
69 11826 : JMCheckLogging();
70 : #endif
71 :
72 11826 : 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 11826 : return true;
78 : }
79 :
80 : void
81 11826 : TrampolineCompiler::release(Trampolines *tramps)
82 : {
83 11826 : 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 11826 : }
88 :
89 : bool
90 11826 : TrampolineCompiler::compileTrampoline(Trampolines::TrampolinePtr *where,
91 : JSC::ExecutablePool **poolp, TrampolineGenerator generator)
92 : {
93 23652 : Assembler masm;
94 :
95 11826 : Label entry = masm.label();
96 11826 : CHECK_RESULT(generator(masm));
97 11826 : JS_ASSERT(entry.isSet());
98 :
99 : bool ok;
100 23652 : JSC::LinkBuffer buffer(&masm, execAlloc, poolp, &ok, JSC::METHOD_CODE);
101 11826 : if (!ok)
102 0 : return false;
103 11826 : masm.finalize(buffer);
104 11826 : uint8_t *result = (uint8_t*)buffer.finalizeCodeAddendum().dataLocation();
105 11826 : *where = JS_DATA_TO_FUNC_PTR(Trampolines::TrampolinePtr, result + masm.distanceOf(entry));
106 :
107 11826 : 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 : */
118 : bool
119 11826 : TrampolineCompiler::generateForceReturn(Assembler &masm)
120 : {
121 : /* The JSStackFrame register may have been clobbered while returning, reload it. */
122 11826 : masm.loadPtr(FrameAddress(VMFrame::offsetOfFp), JSFrameReg);
123 :
124 : /* Perform the frame epilogue. */
125 11826 : masm.fallibleVMCall(true, JS_FUNC_TO_DATA_PTR(void *, stubs::AnyFrameEpilogue), NULL, NULL, 0);
126 :
127 : /* Store any known return value */
128 11826 : masm.loadValueAsComponents(UndefinedValue(), JSReturnReg_Type, JSReturnReg_Data);
129 : Jump rvalClear = masm.branchTest32(Assembler::Zero,
130 11826 : FrameFlagsAddress(), Imm32(StackFrame::HAS_RVAL));
131 11826 : Address rvalAddress(JSFrameReg, StackFrame::offsetOfReturnValue());
132 11826 : masm.loadValueAsComponents(rvalAddress, JSReturnReg_Type, JSReturnReg_Data);
133 11826 : rvalClear.linkTo(masm.label(), &masm);
134 :
135 : /* Return to the caller */
136 11826 : masm.loadPtr(Address(JSFrameReg, StackFrame::offsetOfNcode()), Registers::ReturnReg);
137 11826 : masm.jump(Registers::ReturnReg);
138 11826 : return true;
139 : }
140 :
141 : #if (defined(JS_NO_FASTCALL) && defined(JS_CPU_X86)) || defined(_WIN64)
142 : bool
143 : TrampolineCompiler::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 :
|