2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef CodeLocation_h
27 #define CodeLocation_h
29 #include "MacroAssemblerCodeRef.h"
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;
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.
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 {
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);
73 CodeLocationCommon(MacroAssemblerCodePtr location)
74 : MacroAssemblerCodePtr(location)
79 class CodeLocationInstruction : public CodeLocationCommon {
81 CodeLocationInstruction() {}
82 explicit CodeLocationInstruction(MacroAssemblerCodePtr location)
83 : CodeLocationCommon(location) {}
84 explicit CodeLocationInstruction(void* location)
85 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
88 class CodeLocationLabel : public CodeLocationCommon {
90 CodeLocationLabel() {}
91 explicit CodeLocationLabel(MacroAssemblerCodePtr location)
92 : CodeLocationCommon(location) {}
93 explicit CodeLocationLabel(void* location)
94 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
97 class CodeLocationJump : public CodeLocationCommon {
100 explicit CodeLocationJump(MacroAssemblerCodePtr location)
101 : CodeLocationCommon(location) {}
102 explicit CodeLocationJump(void* location)
103 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
106 class CodeLocationCall : public CodeLocationCommon {
108 CodeLocationCall() {}
109 explicit CodeLocationCall(MacroAssemblerCodePtr location)
110 : CodeLocationCommon(location) {}
111 explicit CodeLocationCall(void* location)
112 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
115 class CodeLocationNearCall : public CodeLocationCommon {
117 CodeLocationNearCall() {}
118 explicit CodeLocationNearCall(MacroAssemblerCodePtr location)
119 : CodeLocationCommon(location) {}
120 explicit CodeLocationNearCall(void* location)
121 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
124 class CodeLocationDataLabel32 : public CodeLocationCommon {
126 CodeLocationDataLabel32() {}
127 explicit CodeLocationDataLabel32(MacroAssemblerCodePtr location)
128 : CodeLocationCommon(location) {}
129 explicit CodeLocationDataLabel32(void* location)
130 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
133 class CodeLocationDataLabelCompact : public CodeLocationCommon {
135 CodeLocationDataLabelCompact() { }
136 explicit CodeLocationDataLabelCompact(MacroAssemblerCodePtr location)
137 : CodeLocationCommon(location) { }
138 explicit CodeLocationDataLabelCompact(void* location)
139 : CodeLocationCommon(MacroAssemblerCodePtr(location)) { }
142 class CodeLocationDataLabelPtr : public CodeLocationCommon {
144 CodeLocationDataLabelPtr() {}
145 explicit CodeLocationDataLabelPtr(MacroAssemblerCodePtr location)
146 : CodeLocationCommon(location) {}
147 explicit CodeLocationDataLabelPtr(void* location)
148 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
151 class CodeLocationConvertibleLoad : public CodeLocationCommon {
153 CodeLocationConvertibleLoad() { }
154 explicit CodeLocationConvertibleLoad(MacroAssemblerCodePtr location)
155 : CodeLocationCommon(location) { }
156 explicit CodeLocationConvertibleLoad(void* location)
157 : CodeLocationCommon(MacroAssemblerCodePtr(location)) { }
160 inline CodeLocationInstruction CodeLocationCommon::instructionAtOffset(int offset)
162 ASSERT_VALID_CODE_OFFSET(offset);
163 return CodeLocationInstruction(reinterpret_cast<char*>(dataLocation()) + offset);
166 inline CodeLocationLabel CodeLocationCommon::labelAtOffset(int offset)
168 ASSERT_VALID_CODE_OFFSET(offset);
169 return CodeLocationLabel(reinterpret_cast<char*>(dataLocation()) + offset);
172 inline CodeLocationJump CodeLocationCommon::jumpAtOffset(int offset)
174 ASSERT_VALID_CODE_OFFSET(offset);
175 return CodeLocationJump(reinterpret_cast<char*>(dataLocation()) + offset);
178 inline CodeLocationCall CodeLocationCommon::callAtOffset(int offset)
180 ASSERT_VALID_CODE_OFFSET(offset);
181 return CodeLocationCall(reinterpret_cast<char*>(dataLocation()) + offset);
184 inline CodeLocationNearCall CodeLocationCommon::nearCallAtOffset(int offset)
186 ASSERT_VALID_CODE_OFFSET(offset);
187 return CodeLocationNearCall(reinterpret_cast<char*>(dataLocation()) + offset);
190 inline CodeLocationDataLabelPtr CodeLocationCommon::dataLabelPtrAtOffset(int offset)
192 ASSERT_VALID_CODE_OFFSET(offset);
193 return CodeLocationDataLabelPtr(reinterpret_cast<char*>(dataLocation()) + offset);
196 inline CodeLocationDataLabel32 CodeLocationCommon::dataLabel32AtOffset(int offset)
198 ASSERT_VALID_CODE_OFFSET(offset);
199 return CodeLocationDataLabel32(reinterpret_cast<char*>(dataLocation()) + offset);
202 inline CodeLocationDataLabelCompact CodeLocationCommon::dataLabelCompactAtOffset(int offset)
204 ASSERT_VALID_CODE_OFFSET(offset);
205 return CodeLocationDataLabelCompact(reinterpret_cast<char*>(dataLocation()) + offset);
208 inline CodeLocationConvertibleLoad CodeLocationCommon::convertibleLoadAtOffset(int offset)
210 ASSERT_VALID_CODE_OFFSET(offset);
211 return CodeLocationConvertibleLoad(reinterpret_cast<char*>(dataLocation()) + offset);
216 #endif // ENABLE(ASSEMBLER)
218 #endif // CodeLocation_h