summaryrefslogtreecommitdiff
path: root/js/src/methodjit/ICRepatcher.h
blob: 59d8e78a0ccc8fbcdabd0790697f3885f18d51b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=4 sw=4 et tw=99:
 *
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
 * May 28, 2008.
 *
 * The Initial Developer of the Original Code is
 *   Brendan Eich <brendan@mozilla.org>
 *
 * Contributor(s):
 *   David Mandelin <dmandelin@mozilla.com>
 *   David Anderson <danderson@mozilla.com>
 *   Chris Leary <cdleary@mozilla.com>
 *   Jacob Bramley <Jacob.Bramely@arm.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either of the GNU General Public License Version 2 or later (the "GPL"),
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#if !defined jsjaeger_icrepatcher_h__ && defined JS_METHODJIT
#define jsjaeger_icrepatcher_h__

#include "assembler/assembler/RepatchBuffer.h"
#include "assembler/moco/MocoStubs.h"
#include "methodjit/ICChecker.h"

namespace js {
namespace mjit {
namespace ic {

class Repatcher : public JSC::RepatchBuffer
{
    typedef JSC::CodeLocationLabel  CodeLocationLabel;
    typedef JSC::CodeLocationCall   CodeLocationCall;
    typedef JSC::FunctionPtr        FunctionPtr;

    CodeLocationLabel label;

  public:
    explicit Repatcher(JITScript *js)
      : JSC::RepatchBuffer(js->code), label(js->code.m_code.executableAddress())
    { }

    explicit Repatcher(const JSC::JITCode &code)
      : JSC::RepatchBuffer(code), label(code.start())
    { }

    using JSC::RepatchBuffer::relink;

    /* Patch a stub call. */
    void relink(CodeLocationCall call, FunctionPtr stub) {
#if defined JS_CPU_X64 || defined JS_CPU_X86
        JSC::RepatchBuffer::relink(call, stub);
#elif defined JS_CPU_ARM
        /*
         * Stub calls on ARM look like this:
         *
         *                  ldr     ip, =stub
         * call label ->    ldr     r8, =JaegerStubVeneer
         *                  blx     r8
         *
         * ARM has to run stub calls through a veneer in order for THROW to
         * work properly. The address that must be patched is the load into
         * 'ip', not the load into 'r8'.
         */
        CheckIsStubCall(call.labelAtOffset(0));
        JSC::RepatchBuffer::relink(call.callAtOffset(-4), stub);
#else
# error
#endif
    }

    /* Patch the offset of a Value load emitted by loadValueWithAddressOffsetPatch. */
    void patchAddressOffsetForValueLoad(CodeLocationLabel label, uint32 offset) {
#if defined JS_CPU_X64 || defined JS_CPU_ARM
        repatch(label.dataLabel32AtOffset(0), offset);
#elif defined JS_CPU_X86
        static const unsigned LOAD_TYPE_OFFSET = 6;
        static const unsigned LOAD_DATA_OFFSET = 12;

        /*
         * We have the following sequence to patch:
         *
         *      mov     <offset+4>($base), %<type>
         *      mov     <offset+0>($base), %<data>
         */
        repatch(label.dataLabel32AtOffset(LOAD_DATA_OFFSET), offset);
        repatch(label.dataLabel32AtOffset(LOAD_TYPE_OFFSET), offset + 4);
#else
# error
#endif
    }

    void patchAddressOffsetForValueStore(CodeLocationLabel label, uint32 offset, bool typeConst) {
#if defined JS_CPU_ARM || defined JS_CPU_X64
        (void) typeConst;
        repatch(label.dataLabel32AtOffset(0), offset);
#elif defined JS_CPU_X86
        static const unsigned STORE_TYPE_OFFSET = 6;
        static const unsigned STORE_DATA_CONST_TYPE_OFFSET = 16;
        static const unsigned STORE_DATA_TYPE_OFFSET = 12;

        /*
         * The type is stored first, then the payload. Both stores can vary in
         * size, depending on whether or not the data is a constant in the
         * instruction stream (though only the first store matters for the
         * purpose of locating both offsets for patching).
         *
         * We have one of the following sequences to patch. Offsets are located
         * before 6B into a given move instruction, but the mov instructions
         * carrying constant payloads are 10B wide overall.
         *
         *  typeConst=false, dataConst=false
         *      mov     %<type>, <offset+4>($base)  ; Length is 6
         *      mov     %<data>, <offset+0>($base)  ; Offset @ len(prev) + 6 = 12
         *  typeConst=true, dataConst=false
         *      mov     $<type>, <offset+4>($base)  ; Length is 10
         *      mov     %<data>, <offset+0>($base)  ; Offset @ len(prev) + 6 = 16
         *  typeConst=true, dataConst=true
         *      mov     $<type>, <offset+4>($base)  ; Length is 10
         *      mov     $<data>, <offset+0>($base)  ; Offset @ len(prev) + 6 = 16
         *
         * Note that we only need to know whether type is const to determine the
         * correct patch offsets. In all cases, the label points to the start
         * of the sequence.
         */
        repatch(label.dataLabel32AtOffset(STORE_TYPE_OFFSET), offset + 4);

        unsigned payloadOffset = typeConst ? STORE_DATA_CONST_TYPE_OFFSET : STORE_DATA_TYPE_OFFSET;
        repatch(label.dataLabel32AtOffset(payloadOffset), offset);
#else
# error
#endif
    }
};

} /* namespace ic */
} /* namespace mjit */
} /* namespace js */

#endif