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 CodeLocationDataLabel32; 41 class CodeLocationDataLabelPtr; 42 43 // The CodeLocation* types are all pretty much do-nothing wrappers around 44 // CodePtr (or MacroAssemblerCodePtr, to give it its full name). These 45 // classes only exist to provide type-safety when linking and patching code. 46 // 47 // The one new piece of functionallity introduced by these classes is the 48 // ability to create (or put another way, to re-discover) another CodeLocation 49 // at an offset from one you already know. When patching code to optimize it 50 // we often want to patch a number of instructions that are short, fixed 51 // offsets apart. To reduce memory overhead we will only retain a pointer to 52 // one of the instructions, and we will use the *AtOffset methods provided by 53 // CodeLocationCommon to find the other points in the code to modify. 54 class CodeLocationCommon : public MacroAssemblerCodePtr { 55 public: 56 CodeLocationInstruction instructionAtOffset(int offset); 57 CodeLocationLabel labelAtOffset(int offset); 58 CodeLocationJump jumpAtOffset(int offset); 59 CodeLocationCall callAtOffset(int offset); 60 CodeLocationNearCall nearCallAtOffset(int offset); 61 CodeLocationDataLabelPtr dataLabelPtrAtOffset(int offset); 62 CodeLocationDataLabel32 dataLabel32AtOffset(int offset); 63 64 protected: 65 CodeLocationCommon() 66 { 67 } 68 69 CodeLocationCommon(MacroAssemblerCodePtr location) 70 : MacroAssemblerCodePtr(location) 71 { 72 } 73 }; 74 75 class CodeLocationInstruction : public CodeLocationCommon { 76 public: 77 CodeLocationInstruction() {} 78 explicit CodeLocationInstruction(MacroAssemblerCodePtr location) 79 : CodeLocationCommon(location) {} 80 explicit CodeLocationInstruction(void* location) 81 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {} 82 }; 83 84 class CodeLocationLabel : public CodeLocationCommon { 85 public: 86 CodeLocationLabel() {} 87 explicit CodeLocationLabel(MacroAssemblerCodePtr location) 88 : CodeLocationCommon(location) {} 89 explicit CodeLocationLabel(void* location) 90 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {} 91 }; 92 93 class CodeLocationJump : public CodeLocationCommon { 94 public: 95 CodeLocationJump() {} 96 explicit CodeLocationJump(MacroAssemblerCodePtr location) 97 : CodeLocationCommon(location) {} 98 explicit CodeLocationJump(void* location) 99 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {} 100 }; 101 102 class CodeLocationCall : public CodeLocationCommon { 103 public: 104 CodeLocationCall() {} 105 explicit CodeLocationCall(MacroAssemblerCodePtr location) 106 : CodeLocationCommon(location) {} 107 explicit CodeLocationCall(void* location) 108 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {} 109 }; 110 111 class CodeLocationNearCall : public CodeLocationCommon { 112 public: 113 CodeLocationNearCall() {} 114 explicit CodeLocationNearCall(MacroAssemblerCodePtr location) 115 : CodeLocationCommon(location) {} 116 explicit CodeLocationNearCall(void* location) 117 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {} 118 }; 119 120 class CodeLocationDataLabel32 : public CodeLocationCommon { 121 public: 122 CodeLocationDataLabel32() {} 123 explicit CodeLocationDataLabel32(MacroAssemblerCodePtr location) 124 : CodeLocationCommon(location) {} 125 explicit CodeLocationDataLabel32(void* location) 126 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {} 127 }; 128 129 class CodeLocationDataLabelPtr : public CodeLocationCommon { 130 public: 131 CodeLocationDataLabelPtr() {} 132 explicit CodeLocationDataLabelPtr(MacroAssemblerCodePtr location) 133 : CodeLocationCommon(location) {} 134 explicit CodeLocationDataLabelPtr(void* location) 135 : CodeLocationCommon(MacroAssemblerCodePtr(location)) {} 136 }; 137 138 inline CodeLocationInstruction CodeLocationCommon::instructionAtOffset(int offset) 139 { 140 ASSERT_VALID_CODE_OFFSET(offset); 141 return CodeLocationInstruction(reinterpret_cast<char*>(dataLocation()) + offset); 142 } 143 144 inline CodeLocationLabel CodeLocationCommon::labelAtOffset(int offset) 145 { 146 ASSERT_VALID_CODE_OFFSET(offset); 147 return CodeLocationLabel(reinterpret_cast<char*>(dataLocation()) + offset); 148 } 149 150 inline CodeLocationJump CodeLocationCommon::jumpAtOffset(int offset) 151 { 152 ASSERT_VALID_CODE_OFFSET(offset); 153 return CodeLocationJump(reinterpret_cast<char*>(dataLocation()) + offset); 154 } 155 156 inline CodeLocationCall CodeLocationCommon::callAtOffset(int offset) 157 { 158 ASSERT_VALID_CODE_OFFSET(offset); 159 return CodeLocationCall(reinterpret_cast<char*>(dataLocation()) + offset); 160 } 161 162 inline CodeLocationNearCall CodeLocationCommon::nearCallAtOffset(int offset) 163 { 164 ASSERT_VALID_CODE_OFFSET(offset); 165 return CodeLocationNearCall(reinterpret_cast<char*>(dataLocation()) + offset); 166 } 167 168 inline CodeLocationDataLabelPtr CodeLocationCommon::dataLabelPtrAtOffset(int offset) 169 { 170 ASSERT_VALID_CODE_OFFSET(offset); 171 return CodeLocationDataLabelPtr(reinterpret_cast<char*>(dataLocation()) + offset); 172 } 173 174 inline CodeLocationDataLabel32 CodeLocationCommon::dataLabel32AtOffset(int offset) 175 { 176 ASSERT_VALID_CODE_OFFSET(offset); 177 return CodeLocationDataLabel32(reinterpret_cast<char*>(dataLocation()) + offset); 178 } 179 180 } // namespace JSC 181 182 #endif // ENABLE(ASSEMBLER) 183 184 #endif // CodeLocation_h 185