Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / methodjit / ICRepatcher.h
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 SpiderMonkey JavaScript 1.9 code, released
18  * May 28, 2008.
19  *
20  * The Initial Developer of the Original Code is
21  *   Brendan Eich <brendan@mozilla.org>
22  *
23  * Contributor(s):
24  *   David Mandelin <dmandelin@mozilla.com>
25  *   David Anderson <danderson@mozilla.com>
26  *   Chris Leary <cdleary@mozilla.com>
27  *   Jacob Bramley <Jacob.Bramely@arm.com>
28  *
29  * Alternatively, the contents of this file may be used under the terms of
30  * either of the GNU General Public License Version 2 or later (the "GPL"),
31  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32  * in which case the provisions of the GPL or the LGPL are applicable instead
33  * of those above. If you wish to allow use of your version of this file only
34  * under the terms of either the GPL or the LGPL, and not to allow others to
35  * use your version of this file under the terms of the MPL, indicate your
36  * decision by deleting the provisions above and replace them with the notice
37  * and other provisions required by the GPL or the LGPL. If you do not delete
38  * the provisions above, a recipient may use your version of this file under
39  * the terms of any one of the MPL, the GPL or the LGPL.
40  *
41  * ***** END LICENSE BLOCK ***** */
42
43 #if !defined jsjaeger_icrepatcher_h__ && defined JS_METHODJIT
44 #define jsjaeger_icrepatcher_h__
45
46 #include "assembler/assembler/RepatchBuffer.h"
47 #include "assembler/moco/MocoStubs.h"
48 #include "methodjit/ICChecker.h"
49
50 namespace js {
51 namespace mjit {
52 namespace ic {
53
54 class Repatcher : public JSC::RepatchBuffer
55 {
56     typedef JSC::CodeLocationLabel  CodeLocationLabel;
57     typedef JSC::CodeLocationCall   CodeLocationCall;
58     typedef JSC::FunctionPtr        FunctionPtr;
59
60     CodeLocationLabel label;
61
62   public:
63     explicit Repatcher(JITScript *js)
64       : JSC::RepatchBuffer(js->code), label(js->code.m_code.executableAddress())
65     { }
66
67     explicit Repatcher(const JSC::JITCode &code)
68       : JSC::RepatchBuffer(code), label(code.start())
69     { }
70
71     using JSC::RepatchBuffer::relink;
72
73     /* Patch a stub call. */
74     void relink(CodeLocationCall call, FunctionPtr stub) {
75 #if defined JS_CPU_X64 || defined JS_CPU_X86
76         JSC::RepatchBuffer::relink(call, stub);
77 #elif defined JS_CPU_ARM
78         /*
79          * Stub calls on ARM look like this:
80          *
81          *                  ldr     ip, =stub
82          * call label ->    ldr     r8, =JaegerStubVeneer
83          *                  blx     r8
84          *
85          * ARM has to run stub calls through a veneer in order for THROW to
86          * work properly. The address that must be patched is the load into
87          * 'ip', not the load into 'r8'.
88          */
89         CheckIsStubCall(call.labelAtOffset(0));
90         JSC::RepatchBuffer::relink(call.callAtOffset(-4), stub);
91 #else
92 # error
93 #endif
94     }
95
96     /* Patch the offset of a Value load emitted by loadValueWithAddressOffsetPatch. */
97     void patchAddressOffsetForValueLoad(CodeLocationLabel label, uint32 offset) {
98 #if defined JS_CPU_X64 || defined JS_CPU_ARM
99         repatch(label.dataLabel32AtOffset(0), offset);
100 #elif defined JS_CPU_X86
101         static const unsigned LOAD_TYPE_OFFSET = 6;
102         static const unsigned LOAD_DATA_OFFSET = 12;
103
104         /*
105          * We have the following sequence to patch:
106          *
107          *      mov     <offset+4>($base), %<type>
108          *      mov     <offset+0>($base), %<data>
109          */
110         repatch(label.dataLabel32AtOffset(LOAD_DATA_OFFSET), offset);
111         repatch(label.dataLabel32AtOffset(LOAD_TYPE_OFFSET), offset + 4);
112 #else
113 # error
114 #endif
115     }
116
117     void patchAddressOffsetForValueStore(CodeLocationLabel label, uint32 offset, bool typeConst) {
118 #if defined JS_CPU_ARM || defined JS_CPU_X64
119         (void) typeConst;
120         repatch(label.dataLabel32AtOffset(0), offset);
121 #elif defined JS_CPU_X86
122         static const unsigned STORE_TYPE_OFFSET = 6;
123         static const unsigned STORE_DATA_CONST_TYPE_OFFSET = 16;
124         static const unsigned STORE_DATA_TYPE_OFFSET = 12;
125
126         /*
127          * The type is stored first, then the payload. Both stores can vary in
128          * size, depending on whether or not the data is a constant in the
129          * instruction stream (though only the first store matters for the
130          * purpose of locating both offsets for patching).
131          *
132          * We have one of the following sequences to patch. Offsets are located
133          * before 6B into a given move instruction, but the mov instructions
134          * carrying constant payloads are 10B wide overall.
135          *
136          *  typeConst=false, dataConst=false
137          *      mov     %<type>, <offset+4>($base)  ; Length is 6
138          *      mov     %<data>, <offset+0>($base)  ; Offset @ len(prev) + 6 = 12
139          *  typeConst=true, dataConst=false
140          *      mov     $<type>, <offset+4>($base)  ; Length is 10
141          *      mov     %<data>, <offset+0>($base)  ; Offset @ len(prev) + 6 = 16
142          *  typeConst=true, dataConst=true
143          *      mov     $<type>, <offset+4>($base)  ; Length is 10
144          *      mov     $<data>, <offset+0>($base)  ; Offset @ len(prev) + 6 = 16
145          *
146          * Note that we only need to know whether type is const to determine the
147          * correct patch offsets. In all cases, the label points to the start
148          * of the sequence.
149          */
150         repatch(label.dataLabel32AtOffset(STORE_TYPE_OFFSET), offset + 4);
151
152         unsigned payloadOffset = typeConst ? STORE_DATA_CONST_TYPE_OFFSET : STORE_DATA_TYPE_OFFSET;
153         repatch(label.dataLabel32AtOffset(payloadOffset), offset);
154 #else
155 # error
156 #endif
157     }
158 };
159
160 } /* namespace ic */
161 } /* namespace mjit */
162 } /* namespace js */
163
164 #endif