JavaScriptCore/assembler/MacroAssemblerCodeRef.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 MacroAssemblerCodeRef_h
       
    27 #define MacroAssemblerCodeRef_h
       
    28 
       
    29 #include "ExecutableAllocator.h"
       
    30 #include "PassRefPtr.h"
       
    31 #include "RefPtr.h"
       
    32 #include "UnusedParam.h"
       
    33 
       
    34 #if ENABLE(ASSEMBLER)
       
    35 
       
    36 // ASSERT_VALID_CODE_POINTER checks that ptr is a non-null pointer, and that it is a valid
       
    37 // instruction address on the platform (for example, check any alignment requirements).
       
    38 #if CPU(ARM_THUMB2)
       
    39 // ARM/thumb instructions must be 16-bit aligned, but all code pointers to be loaded
       
    40 // into the processor are decorated with the bottom bit set, indicating that this is
       
    41 // thumb code (as oposed to 32-bit traditional ARM).  The first test checks for both
       
    42 // decorated and undectorated null, and the second test ensures that the pointer is
       
    43 // decorated.
       
    44 #define ASSERT_VALID_CODE_POINTER(ptr) \
       
    45     ASSERT(reinterpret_cast<intptr_t>(ptr) & ~1); \
       
    46     ASSERT(reinterpret_cast<intptr_t>(ptr) & 1)
       
    47 #define ASSERT_VALID_CODE_OFFSET(offset) \
       
    48     ASSERT(!(offset & 1)) // Must be multiple of 2.
       
    49 #else
       
    50 #define ASSERT_VALID_CODE_POINTER(ptr) \
       
    51     ASSERT(ptr)
       
    52 #define ASSERT_VALID_CODE_OFFSET(offset) // Anything goes!
       
    53 #endif
       
    54 
       
    55 namespace JSC {
       
    56 
       
    57 // FunctionPtr:
       
    58 //
       
    59 // FunctionPtr should be used to wrap pointers to C/C++ functions in JSC
       
    60 // (particularly, the stub functions).
       
    61 class FunctionPtr {
       
    62 public:
       
    63     FunctionPtr()
       
    64         : m_value(0)
       
    65     {
       
    66     }
       
    67 
       
    68     template<typename FunctionType>
       
    69     explicit FunctionPtr(FunctionType* value)
       
    70 #if COMPILER(RVCT)
       
    71      // RVTC compiler needs C-style cast as it fails with the following error
       
    72      // Error:  #694: reinterpret_cast cannot cast away const or other type qualifiers
       
    73         : m_value((void*)(value))
       
    74 #else
       
    75         : m_value(reinterpret_cast<void*>(value))
       
    76 #endif
       
    77     {
       
    78         ASSERT_VALID_CODE_POINTER(m_value);
       
    79     }
       
    80 
       
    81     void* value() const { return m_value; }
       
    82     void* executableAddress() const { return m_value; }
       
    83 
       
    84 
       
    85 private:
       
    86     void* m_value;
       
    87 };
       
    88 
       
    89 // ReturnAddressPtr:
       
    90 //
       
    91 // ReturnAddressPtr should be used to wrap return addresses generated by processor
       
    92 // 'call' instructions exectued in JIT code.  We use return addresses to look up
       
    93 // exception and optimization information, and to repatch the call instruction
       
    94 // that is the source of the return address.
       
    95 class ReturnAddressPtr {
       
    96 public:
       
    97     ReturnAddressPtr()
       
    98         : m_value(0)
       
    99     {
       
   100     }
       
   101 
       
   102     explicit ReturnAddressPtr(void* value)
       
   103         : m_value(value)
       
   104     {
       
   105         ASSERT_VALID_CODE_POINTER(m_value);
       
   106     }
       
   107 
       
   108     explicit ReturnAddressPtr(FunctionPtr function)
       
   109         : m_value(function.value())
       
   110     {
       
   111         ASSERT_VALID_CODE_POINTER(m_value);
       
   112     }
       
   113 
       
   114     void* value() const { return m_value; }
       
   115 
       
   116 private:
       
   117     void* m_value;
       
   118 };
       
   119 
       
   120 // MacroAssemblerCodePtr:
       
   121 //
       
   122 // MacroAssemblerCodePtr should be used to wrap pointers to JIT generated code.
       
   123 class MacroAssemblerCodePtr {
       
   124 public:
       
   125     MacroAssemblerCodePtr()
       
   126         : m_value(0)
       
   127     {
       
   128     }
       
   129 
       
   130     explicit MacroAssemblerCodePtr(void* value)
       
   131 #if CPU(ARM_THUMB2)
       
   132         // Decorate the pointer as a thumb code pointer.
       
   133         : m_value(reinterpret_cast<char*>(value) + 1)
       
   134 #else
       
   135         : m_value(value)
       
   136 #endif
       
   137     {
       
   138         ASSERT_VALID_CODE_POINTER(m_value);
       
   139     }
       
   140 
       
   141     explicit MacroAssemblerCodePtr(ReturnAddressPtr ra)
       
   142         : m_value(ra.value())
       
   143     {
       
   144         ASSERT_VALID_CODE_POINTER(m_value);
       
   145     }
       
   146 
       
   147     void* executableAddress() const { return m_value; }
       
   148 #if CPU(ARM_THUMB2)
       
   149     // To use this pointer as a data address remove the decoration.
       
   150     void* dataLocation() const { ASSERT_VALID_CODE_POINTER(m_value); return reinterpret_cast<char*>(m_value) - 1; }
       
   151 #else
       
   152     void* dataLocation() const { ASSERT_VALID_CODE_POINTER(m_value); return m_value; }
       
   153 #endif
       
   154 
       
   155     bool operator!()
       
   156     {
       
   157         return !m_value;
       
   158     }
       
   159 
       
   160 private:
       
   161     void* m_value;
       
   162 };
       
   163 
       
   164 // MacroAssemblerCodeRef:
       
   165 //
       
   166 // A reference to a section of JIT generated code.  A CodeRef consists of a
       
   167 // pointer to the code, and a ref pointer to the pool from within which it
       
   168 // was allocated.
       
   169 class MacroAssemblerCodeRef {
       
   170 public:
       
   171     MacroAssemblerCodeRef()
       
   172         : m_size(0)
       
   173     {
       
   174     }
       
   175 
       
   176     MacroAssemblerCodeRef(void* code, PassRefPtr<ExecutablePool> executablePool, size_t size)
       
   177         : m_code(code)
       
   178         , m_executablePool(executablePool)
       
   179         , m_size(size)
       
   180     {
       
   181     }
       
   182 
       
   183     MacroAssemblerCodePtr m_code;
       
   184     RefPtr<ExecutablePool> m_executablePool;
       
   185     size_t m_size;
       
   186 };
       
   187 
       
   188 } // namespace JSC
       
   189 
       
   190 #endif // ENABLE(ASSEMBLER)
       
   191 
       
   192 #endif // MacroAssemblerCodeRef_h