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