Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / methodjit / TrampolineCompiler.cpp
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
45 namespace js {
46 namespace mjit {
47
48 #define CHECK_RESULT(x) if (!(x)) return false
49 #define COMPILE(which, pool, how) CHECK_RESULT(compileTrampoline(&(which), &pool, how))
50 #define RELEASE(which, pool) JS_BEGIN_MACRO \
51     which = NULL;                           \
52     if (pool)                               \
53         pool->release();                    \
54     pool = NULL;                            \
55 JS_END_MACRO
56
57 typedef JSC::MacroAssembler::Address Address;
58 typedef JSC::MacroAssembler::Label Label;
59 typedef JSC::MacroAssembler::Jump Jump;
60 typedef JSC::MacroAssembler::ImmPtr ImmPtr;
61 typedef JSC::MacroAssembler::Imm32 Imm32;
62 typedef JSC::MacroAssembler::Address Address;
63
64 bool
65 TrampolineCompiler::compile()
66 {
67 #ifdef JS_METHODJIT_SPEW
68     JMCheckLogging();
69 #endif
70
71     COMPILE(trampolines->forceReturn, trampolines->forceReturnPool, generateForceReturn);
72 #if (defined(JS_NO_FASTCALL) && defined(JS_CPU_X86)) || defined(_WIN64)
73     COMPILE(trampolines->forceReturnFast, trampolines->forceReturnFastPool, generateForceReturnFast);
74 #endif
75
76     return true;
77 }
78
79 void
80 TrampolineCompiler::release(Trampolines *tramps)
81 {
82     RELEASE(tramps->forceReturn, tramps->forceReturnPool);
83 #if (defined(JS_NO_FASTCALL) && defined(JS_CPU_X86)) || defined(_WIN64)
84     RELEASE(tramps->forceReturnFast, tramps->forceReturnFastPool);
85 #endif
86 }
87
88 bool
89 TrampolineCompiler::compileTrampoline(Trampolines::TrampolinePtr *where, JSC::ExecutablePool **pool,
90                                       TrampolineGenerator generator)
91 {
92     Assembler masm;
93
94     Label entry = masm.label();
95     CHECK_RESULT(generator(masm));
96     JS_ASSERT(entry.isValid());
97
98     *pool = execPool->poolForSize(masm.size());
99     if (!*pool)
100         return false;
101
102     JSC::LinkBuffer buffer(&masm, *pool);
103     masm.finalize(buffer);
104     uint8 *result = (uint8*)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 is a call object, so we always check.
114  * - We don't know where we came from, so we don't know frame depth or PC.
115  * - There is no stub buffer.
116  */
117 bool
118 TrampolineCompiler::generateForceReturn(Assembler &masm)
119 {
120     /* if (hasArgsObj() || hasCallObj()) stubs::PutActivationObjects() */
121     Jump noActObjs = masm.branchTest32(Assembler::Zero, FrameFlagsAddress(),
122                                        Imm32(JSFRAME_HAS_CALL_OBJ | JSFRAME_HAS_ARGS_OBJ));
123     masm.fallibleVMCall(JS_FUNC_TO_DATA_PTR(void *, stubs::PutActivationObjects), NULL, 0);
124     noActObjs.linkTo(masm.label(), &masm);
125
126     /* Store any known return value */
127     masm.loadValueAsComponents(UndefinedValue(), JSReturnReg_Type, JSReturnReg_Data);
128     Jump rvalClear = masm.branchTest32(Assembler::Zero,
129                                        FrameFlagsAddress(), Imm32(JSFRAME_HAS_RVAL));
130     Address rvalAddress(JSFrameReg, JSStackFrame::offsetOfReturnValue());
131     masm.loadValueAsComponents(rvalAddress, JSReturnReg_Type, JSReturnReg_Data);
132     rvalClear.linkTo(masm.label(), &masm);
133
134     /* Return to the caller */
135     masm.loadPtr(Address(JSFrameReg, JSStackFrame::offsetOfncode()), Registers::ReturnReg);
136     masm.jump(Registers::ReturnReg);
137     return true;
138 }
139
140 #if (defined(JS_NO_FASTCALL) && defined(JS_CPU_X86)) || defined(_WIN64)
141 bool
142 TrampolineCompiler::generateForceReturnFast(Assembler &masm)
143 {
144 #ifdef _WIN64
145     masm.addPtr(Imm32(32), Registers::StackPointer);
146 #else
147     // In case of no fast call, when we change the return address,
148     // we need to make sure add esp by 8.
149     masm.addPtr(Imm32(16), Registers::StackPointer);
150 #endif
151     return generateForceReturn(masm);
152 }
153 #endif
154
155 } /* namespace mjit */
156 } /* namespace js */
157