1 /*
2  * Copyright (C) 2009 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef CodeLocation_h
27 #define CodeLocation_h
28 
29 #include "MacroAssemblerCodeRef.h"
30 
31 #if ENABLE(ASSEMBLER)
32 
33 namespace JSC {
34 
35 class CodeLocationInstruction;
36 class CodeLocationLabel;
37 class CodeLocationJump;
38 class CodeLocationCall;
39 class CodeLocationNearCall;
40 class CodeLocationDataLabelCompact;
41 class CodeLocationDataLabel32;
42 class CodeLocationDataLabelPtr;
43 class CodeLocationConvertibleLoad;
44 
45 // The CodeLocation* types are all pretty much do-nothing wrappers around
46 // CodePtr (or MacroAssemblerCodePtr, to give it its full name).  These
47 // classes only exist to provide type-safety when linking and patching code.
48 //
49 // The one new piece of functionallity introduced by these classes is the
50 // ability to create (or put another way, to re-discover) another CodeLocation
51 // at an offset from one you already know.  When patching code to optimize it
52 // we often want to patch a number of instructions that are short, fixed
53 // offsets apart.  To reduce memory overhead we will only retain a pointer to
54 // one of the instructions, and we will use the *AtOffset methods provided by
55 // CodeLocationCommon to find the other points in the code to modify.
56 class CodeLocationCommon : public MacroAssemblerCodePtr {
57 public:
58     CodeLocationInstruction instructionAtOffset(int offset);
59     CodeLocationLabel labelAtOffset(int offset);
60     CodeLocationJump jumpAtOffset(int offset);
61     CodeLocationCall callAtOffset(int offset);
62     CodeLocationNearCall nearCallAtOffset(int offset);
63     CodeLocationDataLabelPtr dataLabelPtrAtOffset(int offset);
64     CodeLocationDataLabel32 dataLabel32AtOffset(int offset);
65     CodeLocationDataLabelCompact dataLabelCompactAtOffset(int offset);
66     CodeLocationConvertibleLoad convertibleLoadAtOffset(int offset);
67 
68 protected:
CodeLocationCommon()69     CodeLocationCommon()
70     {
71     }
72 
CodeLocationCommon(MacroAssemblerCodePtr location)73     CodeLocationCommon(MacroAssemblerCodePtr location)
74         : MacroAssemblerCodePtr(location)
75     {
76     }
77 };
78 
79 class CodeLocationInstruction : public CodeLocationCommon {
80 public:
CodeLocationInstruction()81     CodeLocationInstruction() {}
CodeLocationInstruction(MacroAssemblerCodePtr location)82     explicit CodeLocationInstruction(MacroAssemblerCodePtr location)
83         : CodeLocationCommon(location) {}
CodeLocationInstruction(void * location)84     explicit CodeLocationInstruction(void* location)
85         : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
86 };
87 
88 class CodeLocationLabel : public CodeLocationCommon {
89 public:
CodeLocationLabel()90     CodeLocationLabel() {}
CodeLocationLabel(MacroAssemblerCodePtr location)91     explicit CodeLocationLabel(MacroAssemblerCodePtr location)
92         : CodeLocationCommon(location) {}
CodeLocationLabel(void * location)93     explicit CodeLocationLabel(void* location)
94         : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
95 };
96 
97 class CodeLocationJump : public CodeLocationCommon {
98 public:
CodeLocationJump()99     CodeLocationJump() {}
CodeLocationJump(MacroAssemblerCodePtr location)100     explicit CodeLocationJump(MacroAssemblerCodePtr location)
101         : CodeLocationCommon(location) {}
CodeLocationJump(void * location)102     explicit CodeLocationJump(void* location)
103         : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
104 };
105 
106 class CodeLocationCall : public CodeLocationCommon {
107 public:
CodeLocationCall()108     CodeLocationCall() {}
CodeLocationCall(MacroAssemblerCodePtr location)109     explicit CodeLocationCall(MacroAssemblerCodePtr location)
110         : CodeLocationCommon(location) {}
CodeLocationCall(void * location)111     explicit CodeLocationCall(void* location)
112         : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
113 };
114 
115 class CodeLocationNearCall : public CodeLocationCommon {
116 public:
CodeLocationNearCall()117     CodeLocationNearCall() {}
CodeLocationNearCall(MacroAssemblerCodePtr location)118     explicit CodeLocationNearCall(MacroAssemblerCodePtr location)
119         : CodeLocationCommon(location) {}
CodeLocationNearCall(void * location)120     explicit CodeLocationNearCall(void* location)
121         : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
122 };
123 
124 class CodeLocationDataLabel32 : public CodeLocationCommon {
125 public:
CodeLocationDataLabel32()126     CodeLocationDataLabel32() {}
CodeLocationDataLabel32(MacroAssemblerCodePtr location)127     explicit CodeLocationDataLabel32(MacroAssemblerCodePtr location)
128         : CodeLocationCommon(location) {}
CodeLocationDataLabel32(void * location)129     explicit CodeLocationDataLabel32(void* location)
130         : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
131 };
132 
133 class CodeLocationDataLabelCompact : public CodeLocationCommon {
134 public:
CodeLocationDataLabelCompact()135     CodeLocationDataLabelCompact() { }
CodeLocationDataLabelCompact(MacroAssemblerCodePtr location)136     explicit CodeLocationDataLabelCompact(MacroAssemblerCodePtr location)
137         : CodeLocationCommon(location) { }
CodeLocationDataLabelCompact(void * location)138     explicit CodeLocationDataLabelCompact(void* location)
139         : CodeLocationCommon(MacroAssemblerCodePtr(location)) { }
140 };
141 
142 class CodeLocationDataLabelPtr : public CodeLocationCommon {
143 public:
CodeLocationDataLabelPtr()144     CodeLocationDataLabelPtr() {}
CodeLocationDataLabelPtr(MacroAssemblerCodePtr location)145     explicit CodeLocationDataLabelPtr(MacroAssemblerCodePtr location)
146         : CodeLocationCommon(location) {}
CodeLocationDataLabelPtr(void * location)147     explicit CodeLocationDataLabelPtr(void* location)
148         : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
149 };
150 
151 class CodeLocationConvertibleLoad : public CodeLocationCommon {
152 public:
CodeLocationConvertibleLoad()153     CodeLocationConvertibleLoad() { }
CodeLocationConvertibleLoad(MacroAssemblerCodePtr location)154     explicit CodeLocationConvertibleLoad(MacroAssemblerCodePtr location)
155         : CodeLocationCommon(location) { }
CodeLocationConvertibleLoad(void * location)156     explicit CodeLocationConvertibleLoad(void* location)
157         : CodeLocationCommon(MacroAssemblerCodePtr(location)) { }
158 };
159 
instructionAtOffset(int offset)160 inline CodeLocationInstruction CodeLocationCommon::instructionAtOffset(int offset)
161 {
162     ASSERT_VALID_CODE_OFFSET(offset);
163     return CodeLocationInstruction(reinterpret_cast<char*>(dataLocation()) + offset);
164 }
165 
labelAtOffset(int offset)166 inline CodeLocationLabel CodeLocationCommon::labelAtOffset(int offset)
167 {
168     ASSERT_VALID_CODE_OFFSET(offset);
169     return CodeLocationLabel(reinterpret_cast<char*>(dataLocation()) + offset);
170 }
171 
jumpAtOffset(int offset)172 inline CodeLocationJump CodeLocationCommon::jumpAtOffset(int offset)
173 {
174     ASSERT_VALID_CODE_OFFSET(offset);
175     return CodeLocationJump(reinterpret_cast<char*>(dataLocation()) + offset);
176 }
177 
callAtOffset(int offset)178 inline CodeLocationCall CodeLocationCommon::callAtOffset(int offset)
179 {
180     ASSERT_VALID_CODE_OFFSET(offset);
181     return CodeLocationCall(reinterpret_cast<char*>(dataLocation()) + offset);
182 }
183 
nearCallAtOffset(int offset)184 inline CodeLocationNearCall CodeLocationCommon::nearCallAtOffset(int offset)
185 {
186     ASSERT_VALID_CODE_OFFSET(offset);
187     return CodeLocationNearCall(reinterpret_cast<char*>(dataLocation()) + offset);
188 }
189 
dataLabelPtrAtOffset(int offset)190 inline CodeLocationDataLabelPtr CodeLocationCommon::dataLabelPtrAtOffset(int offset)
191 {
192     ASSERT_VALID_CODE_OFFSET(offset);
193     return CodeLocationDataLabelPtr(reinterpret_cast<char*>(dataLocation()) + offset);
194 }
195 
dataLabel32AtOffset(int offset)196 inline CodeLocationDataLabel32 CodeLocationCommon::dataLabel32AtOffset(int offset)
197 {
198     ASSERT_VALID_CODE_OFFSET(offset);
199     return CodeLocationDataLabel32(reinterpret_cast<char*>(dataLocation()) + offset);
200 }
201 
dataLabelCompactAtOffset(int offset)202 inline CodeLocationDataLabelCompact CodeLocationCommon::dataLabelCompactAtOffset(int offset)
203 {
204     ASSERT_VALID_CODE_OFFSET(offset);
205     return CodeLocationDataLabelCompact(reinterpret_cast<char*>(dataLocation()) + offset);
206 }
207 
convertibleLoadAtOffset(int offset)208 inline CodeLocationConvertibleLoad CodeLocationCommon::convertibleLoadAtOffset(int offset)
209 {
210     ASSERT_VALID_CODE_OFFSET(offset);
211     return CodeLocationConvertibleLoad(reinterpret_cast<char*>(dataLocation()) + offset);
212 }
213 
214 } // namespace JSC
215 
216 #endif // ENABLE(ASSEMBLER)
217 
218 #endif // CodeLocation_h
219