1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=79:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Copyright (C) 2009 Apple Inc. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
28 * ***** END LICENSE BLOCK ***** */
30 #ifndef CodeLocation_h
31 #define CodeLocation_h
33 #include "assembler/wtf/Platform.h"
34 #include "assembler/assembler/MacroAssemblerCodeRef.h"
40 class CodeLocationInstruction;
41 class CodeLocationLabel;
42 class CodeLocationJump;
43 class CodeLocationCall;
44 class CodeLocationNearCall;
45 class CodeLocationDataLabel32;
46 class CodeLocationDataLabelPtr;
48 // The CodeLocation* types are all pretty much do-nothing wrappers around
49 // CodePtr (or MacroAssemblerCodePtr, to give it its full name). These
50 // classes only exist to provide type-safety when linking and patching code.
52 // The one new piece of functionallity introduced by these classes is the
53 // ability to create (or put another way, to re-discover) another CodeLocation
54 // at an offset from one you already know. When patching code to optimize it
55 // we often want to patch a number of instructions that are short, fixed
56 // offsets apart. To reduce memory overhead we will only retain a pointer to
57 // one of the instructions, and we will use the *AtOffset methods provided by
58 // CodeLocationCommon to find the other points in the code to modify.
59 class CodeLocationCommon : public MacroAssemblerCodePtr {
61 CodeLocationInstruction instructionAtOffset(int offset);
62 CodeLocationLabel labelAtOffset(int offset);
63 CodeLocationJump jumpAtOffset(int offset);
64 CodeLocationCall callAtOffset(int offset);
65 CodeLocationNearCall nearCallAtOffset(int offset);
66 CodeLocationDataLabelPtr dataLabelPtrAtOffset(int offset);
67 CodeLocationDataLabel32 dataLabel32AtOffset(int offset);
74 CodeLocationCommon(MacroAssemblerCodePtr location)
75 : MacroAssemblerCodePtr(location)
80 class CodeLocationInstruction : public CodeLocationCommon {
82 CodeLocationInstruction() {}
83 explicit CodeLocationInstruction(MacroAssemblerCodePtr location)
84 : CodeLocationCommon(location) {}
85 explicit CodeLocationInstruction(void* location)
86 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
89 class CodeLocationLabel : public CodeLocationCommon {
91 CodeLocationLabel() {}
92 explicit CodeLocationLabel(MacroAssemblerCodePtr location)
93 : CodeLocationCommon(location) {}
94 explicit CodeLocationLabel(void* location)
95 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
98 class CodeLocationJump : public CodeLocationCommon {
100 CodeLocationJump() {}
101 explicit CodeLocationJump(MacroAssemblerCodePtr location)
102 : CodeLocationCommon(location) {}
103 explicit CodeLocationJump(void* location)
104 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
107 class CodeLocationCall : public CodeLocationCommon {
109 CodeLocationCall() {}
110 explicit CodeLocationCall(MacroAssemblerCodePtr location)
111 : CodeLocationCommon(location) {}
112 explicit CodeLocationCall(void* location)
113 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
116 class CodeLocationNearCall : public CodeLocationCommon {
118 CodeLocationNearCall() {}
119 explicit CodeLocationNearCall(MacroAssemblerCodePtr location)
120 : CodeLocationCommon(location) {}
121 explicit CodeLocationNearCall(void* location)
122 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
125 class CodeLocationDataLabel32 : public CodeLocationCommon {
127 CodeLocationDataLabel32() {}
128 explicit CodeLocationDataLabel32(MacroAssemblerCodePtr location)
129 : CodeLocationCommon(location) {}
130 explicit CodeLocationDataLabel32(void* location)
131 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
134 class CodeLocationDataLabelPtr : public CodeLocationCommon {
136 CodeLocationDataLabelPtr() {}
137 explicit CodeLocationDataLabelPtr(MacroAssemblerCodePtr location)
138 : CodeLocationCommon(location) {}
139 explicit CodeLocationDataLabelPtr(void* location)
140 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {}
143 inline CodeLocationInstruction CodeLocationCommon::instructionAtOffset(int offset)
145 ASSERT_VALID_CODE_OFFSET(offset);
146 return CodeLocationInstruction(reinterpret_cast<char*>(dataLocation()) + offset);
149 inline CodeLocationLabel CodeLocationCommon::labelAtOffset(int offset)
151 ASSERT_VALID_CODE_OFFSET(offset);
152 return CodeLocationLabel(reinterpret_cast<char*>(dataLocation()) + offset);
155 inline CodeLocationJump CodeLocationCommon::jumpAtOffset(int offset)
157 ASSERT_VALID_CODE_OFFSET(offset);
158 return CodeLocationJump(reinterpret_cast<char*>(dataLocation()) + offset);
161 inline CodeLocationCall CodeLocationCommon::callAtOffset(int offset)
163 ASSERT_VALID_CODE_OFFSET(offset);
164 return CodeLocationCall(reinterpret_cast<char*>(dataLocation()) + offset);
167 inline CodeLocationNearCall CodeLocationCommon::nearCallAtOffset(int offset)
169 ASSERT_VALID_CODE_OFFSET(offset);
170 return CodeLocationNearCall(reinterpret_cast<char*>(dataLocation()) + offset);
173 inline CodeLocationDataLabelPtr CodeLocationCommon::dataLabelPtrAtOffset(int offset)
175 ASSERT_VALID_CODE_OFFSET(offset);
176 return CodeLocationDataLabelPtr(reinterpret_cast<char*>(dataLocation()) + offset);
179 inline CodeLocationDataLabel32 CodeLocationCommon::dataLabel32AtOffset(int offset)
181 ASSERT_VALID_CODE_OFFSET(offset);
182 return CodeLocationDataLabel32(reinterpret_cast<char*>(dataLocation()) + offset);
187 #endif // ENABLE(ASSEMBLER)
189 #endif // CodeLocation_h