Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / assembler / assembler / RepatchBuffer.h
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sw=4 et tw=79:
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * Copyright (C) 2009 Apple Inc. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
27  * 
28  * ***** END LICENSE BLOCK ***** */
29
30 #ifndef RepatchBuffer_h
31 #define RepatchBuffer_h
32
33 #include "assembler/wtf/Platform.h"
34
35 #if ENABLE_ASSEMBLER
36
37 #include <assembler/MacroAssembler.h>
38 #include <moco/MocoStubs.h> //MOCO
39
40 namespace JSC {
41
42 // RepatchBuffer:
43 //
44 // This class is used to modify code after code generation has been completed,
45 // and after the code has potentially already been executed.  This mechanism is
46 // used to apply optimizations to the code.
47 //
48 class RepatchBuffer {
49     typedef MacroAssemblerCodePtr CodePtr;
50
51 public:
52     RepatchBuffer(const MacroAssemblerCodeRef &ref)
53     {
54         m_start = ref.m_code.executableAddress();
55         m_size = ref.m_size;
56         mprot = true;
57
58         if (mprot)
59             ExecutableAllocator::makeWritable(m_start, m_size);
60     }
61
62     RepatchBuffer(const JITCode &code)
63     {
64         m_start = code.start();
65         m_size = code.size();
66         mprot = true;
67
68         if (mprot)
69             ExecutableAllocator::makeWritable(m_start, m_size);
70     }
71
72     ~RepatchBuffer()
73     {
74         if (mprot)
75             ExecutableAllocator::makeExecutable(m_start, m_size);
76     }
77
78     void relink(CodeLocationJump jump, CodeLocationLabel destination)
79     {
80         MacroAssembler::repatchJump(jump, destination);
81     }
82
83     bool canRelink(CodeLocationJump jump, CodeLocationLabel destination)
84     {
85         return MacroAssembler::canRepatchJump(jump, destination);
86     }
87
88     void relink(CodeLocationCall call, CodeLocationLabel destination)
89     {
90         MacroAssembler::repatchCall(call, destination);
91     }
92
93     void relink(CodeLocationCall call, FunctionPtr destination)
94     {
95         MacroAssembler::repatchCall(call, destination);
96     }
97
98     void relink(CodeLocationNearCall nearCall, CodePtr destination)
99     {
100         MacroAssembler::repatchNearCall(nearCall, CodeLocationLabel(destination));
101     }
102
103     void relink(CodeLocationNearCall nearCall, CodeLocationLabel destination)
104     {
105         MacroAssembler::repatchNearCall(nearCall, destination);
106     }
107
108     void repatch(CodeLocationDataLabel32 dataLabel32, int32_t value)
109     {
110         MacroAssembler::repatchInt32(dataLabel32, value);
111     }
112
113     void repatch(CodeLocationDataLabelPtr dataLabelPtr, void* value)
114     {
115         MacroAssembler::repatchPointer(dataLabelPtr, value);
116     }
117
118     void repatchLoadPtrToLEA(CodeLocationInstruction instruction)
119     {
120         MacroAssembler::repatchLoadPtrToLEA(instruction);
121     }
122
123     void repatchLEAToLoadPtr(CodeLocationInstruction instruction)
124     {
125         MacroAssembler::repatchLEAToLoadPtr(instruction);
126     }
127
128     void relinkCallerToTrampoline(ReturnAddressPtr returnAddress, CodeLocationLabel label)
129     {
130         relink(CodeLocationCall(CodePtr(returnAddress)), label);
131     }
132     
133     void relinkCallerToTrampoline(ReturnAddressPtr returnAddress, CodePtr newCalleeFunction)
134     {
135         relinkCallerToTrampoline(returnAddress, CodeLocationLabel(newCalleeFunction));
136     }
137
138     void relinkCallerToFunction(ReturnAddressPtr returnAddress, FunctionPtr function)
139     {
140         relink(CodeLocationCall(CodePtr(returnAddress)), function);
141     }
142     
143     void relinkNearCallerToTrampoline(ReturnAddressPtr returnAddress, CodeLocationLabel label)
144     {
145         relink(CodeLocationNearCall(CodePtr(returnAddress)), label);
146     }
147     
148     void relinkNearCallerToTrampoline(ReturnAddressPtr returnAddress, CodePtr newCalleeFunction)
149     {
150         relinkNearCallerToTrampoline(returnAddress, CodeLocationLabel(newCalleeFunction));
151     }
152
153 protected:
154     void* m_start;
155     size_t m_size;
156     bool mprot;
157 };
158
159 } // namespace JSC
160
161 #endif // ENABLE(ASSEMBLER)
162
163 #endif // RepatchBuffer_h