Home | History | Annotate | Download | only in runtime
      1 // Copyright 2014 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef V8_RUNTIME_RUNTIME_UTILS_H_
      6 #define V8_RUNTIME_RUNTIME_UTILS_H_
      7 
      8 #include "src/base/logging.h"
      9 #include "src/runtime/runtime.h"
     10 
     11 namespace v8 {
     12 namespace internal {
     13 
     14 // Cast the given object to a value of the specified type and store
     15 // it in a variable with the given name.  If the object is not of the
     16 // expected type we crash safely.
     17 #define CONVERT_ARG_CHECKED(Type, name, index) \
     18   CHECK(args[index]->Is##Type());              \
     19   Type* name = Type::cast(args[index]);
     20 
     21 #define CONVERT_ARG_HANDLE_CHECKED(Type, name, index) \
     22   CHECK(args[index]->Is##Type());                     \
     23   Handle<Type> name = args.at<Type>(index);
     24 
     25 #define CONVERT_NUMBER_ARG_HANDLE_CHECKED(name, index) \
     26   CHECK(args[index]->IsNumber());                      \
     27   Handle<Object> name = args.at<Object>(index);
     28 
     29 // Cast the given object to a boolean and store it in a variable with
     30 // the given name.  If the object is not a boolean we crash safely.
     31 #define CONVERT_BOOLEAN_ARG_CHECKED(name, index) \
     32   CHECK(args[index]->IsBoolean());               \
     33   bool name = args[index]->IsTrue(isolate);
     34 
     35 // Cast the given argument to a Smi and store its value in an int variable
     36 // with the given name.  If the argument is not a Smi we crash safely.
     37 #define CONVERT_SMI_ARG_CHECKED(name, index) \
     38   CHECK(args[index]->IsSmi());               \
     39   int name = args.smi_at(index);
     40 
     41 // Cast the given argument to a double and store it in a variable with
     42 // the given name.  If the argument is not a number (as opposed to
     43 // the number not-a-number) we crash safely.
     44 #define CONVERT_DOUBLE_ARG_CHECKED(name, index) \
     45   CHECK(args[index]->IsNumber());               \
     46   double name = args.number_at(index);
     47 
     48 // Cast the given argument to a size_t and store its value in a variable with
     49 // the given name.  If the argument is not a size_t we crash safely.
     50 #define CONVERT_SIZE_ARG_CHECKED(name, index)            \
     51   CHECK(args[index]->IsNumber());                        \
     52   Handle<Object> name##_object = args.at<Object>(index); \
     53   size_t name = 0;                                       \
     54   CHECK(TryNumberToSize(*name##_object, &name));
     55 
     56 // Call the specified converter on the object *comand store the result in
     57 // a variable of the specified type with the given name.  If the
     58 // object is not a Number we crash safely.
     59 #define CONVERT_NUMBER_CHECKED(type, name, Type, obj) \
     60   CHECK(obj->IsNumber());                             \
     61   type name = NumberTo##Type(obj);
     62 
     63 // Cast the given argument to PropertyDetails and store its value in a
     64 // variable with the given name.  If the argument is not a Smi we crash safely.
     65 #define CONVERT_PROPERTY_DETAILS_CHECKED(name, index) \
     66   CHECK(args[index]->IsSmi());                        \
     67   PropertyDetails name = PropertyDetails(Smi::cast(args[index]));
     68 
     69 // Assert that the given argument has a valid value for a LanguageMode
     70 // and store it in a LanguageMode variable with the given name.
     71 #define CONVERT_LANGUAGE_MODE_ARG_CHECKED(name, index) \
     72   CHECK(args[index]->IsNumber());                      \
     73   int32_t __tmp_##name = 0;                            \
     74   CHECK(args[index]->ToInt32(&__tmp_##name));          \
     75   CHECK(is_valid_language_mode(__tmp_##name));         \
     76   LanguageMode name = static_cast<LanguageMode>(__tmp_##name);
     77 
     78 // Assert that the given argument is a number within the Int32 range
     79 // and convert it to int32_t.  If the argument is not an Int32 we crash safely.
     80 #define CONVERT_INT32_ARG_CHECKED(name, index) \
     81   CHECK(args[index]->IsNumber());              \
     82   int32_t name = 0;                            \
     83   CHECK(args[index]->ToInt32(&name));
     84 
     85 // Assert that the given argument is a number within the Uint32 range
     86 // and convert it to uint32_t.  If the argument is not an Uint32 call
     87 // IllegalOperation and return.
     88 #define CONVERT_UINT32_ARG_CHECKED(name, index) \
     89   CHECK(args[index]->IsNumber());               \
     90   uint32_t name = 0;                            \
     91   CHECK(args[index]->ToUint32(&name));
     92 
     93 // Cast the given argument to PropertyAttributes and store its value in a
     94 // variable with the given name.  If the argument is not a Smi or the
     95 // enum value is out of range, we crash safely.
     96 #define CONVERT_PROPERTY_ATTRIBUTES_CHECKED(name, index)                     \
     97   CHECK(args[index]->IsSmi());                                               \
     98   CHECK((args.smi_at(index) & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); \
     99   PropertyAttributes name = static_cast<PropertyAttributes>(args.smi_at(index));
    100 
    101 // A mechanism to return a pair of Object pointers in registers (if possible).
    102 // How this is achieved is calling convention-dependent.
    103 // All currently supported x86 compiles uses calling conventions that are cdecl
    104 // variants where a 64-bit value is returned in two 32-bit registers
    105 // (edx:eax on ia32, r1:r0 on ARM).
    106 // In AMD-64 calling convention a struct of two pointers is returned in rdx:rax.
    107 // In Win64 calling convention, a struct of two pointers is returned in memory,
    108 // allocated by the caller, and passed as a pointer in a hidden first parameter.
    109 #ifdef V8_HOST_ARCH_64_BIT
    110 struct ObjectPair {
    111   Object* x;
    112   Object* y;
    113 };
    114 
    115 
    116 static inline ObjectPair MakePair(Object* x, Object* y) {
    117   ObjectPair result = {x, y};
    118   // Pointers x and y returned in rax and rdx, in AMD-x64-abi.
    119   // In Win64 they are assigned to a hidden first argument.
    120   return result;
    121 }
    122 #elif V8_TARGET_ARCH_X64 && V8_TARGET_ARCH_32_BIT
    123 // For x32 a 128-bit struct return is done as rax and rdx from the ObjectPair
    124 // are used in the full codegen and Crankshaft compiler. An alternative is
    125 // using uint64_t and modifying full codegen and Crankshaft compiler.
    126 struct ObjectPair {
    127   Object* x;
    128   uint32_t x_upper;
    129   Object* y;
    130   uint32_t y_upper;
    131 };
    132 
    133 
    134 static inline ObjectPair MakePair(Object* x, Object* y) {
    135   ObjectPair result = {x, 0, y, 0};
    136   // Pointers x and y returned in rax and rdx, in x32-abi.
    137   return result;
    138 }
    139 #else
    140 typedef uint64_t ObjectPair;
    141 static inline ObjectPair MakePair(Object* x, Object* y) {
    142 #if defined(V8_TARGET_LITTLE_ENDIAN)
    143   return reinterpret_cast<uint32_t>(x) |
    144          (reinterpret_cast<ObjectPair>(y) << 32);
    145 #elif defined(V8_TARGET_BIG_ENDIAN)
    146   return reinterpret_cast<uint32_t>(y) |
    147          (reinterpret_cast<ObjectPair>(x) << 32);
    148 #else
    149 #error Unknown endianness
    150 #endif
    151 }
    152 #endif
    153 
    154 
    155 // A mechanism to return a triple of Object pointers. In all calling
    156 // conventions, a struct of two pointers is returned in memory,
    157 // allocated by the caller, and passed as a pointer in a hidden first parameter.
    158 struct ObjectTriple {
    159   Object* x;
    160   Object* y;
    161   Object* z;
    162 };
    163 
    164 static inline ObjectTriple MakeTriple(Object* x, Object* y, Object* z) {
    165   ObjectTriple result = {x, y, z};
    166   // ObjectTriple is assigned to a hidden first argument.
    167   return result;
    168 }
    169 
    170 }  // namespace internal
    171 }  // namespace v8
    172 
    173 #endif  // V8_RUNTIME_RUNTIME_UTILS_H_
    174