Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 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 | |
| 9 | namespace v8 { |
| 10 | namespace internal { |
| 11 | |
| 12 | #define RUNTIME_ASSERT(value) \ |
| 13 | if (!(value)) return isolate->ThrowIllegalOperation(); |
| 14 | |
| 15 | #define RUNTIME_ASSERT_HANDLIFIED(value, T) \ |
| 16 | if (!(value)) { \ |
| 17 | isolate->ThrowIllegalOperation(); \ |
| 18 | return MaybeHandle<T>(); \ |
| 19 | } |
| 20 | |
| 21 | // Cast the given object to a value of the specified type and store |
| 22 | // it in a variable with the given name. If the object is not of the |
| 23 | // expected type call IllegalOperation and return. |
| 24 | #define CONVERT_ARG_CHECKED(Type, name, index) \ |
| 25 | RUNTIME_ASSERT(args[index]->Is##Type()); \ |
| 26 | Type* name = Type::cast(args[index]); |
| 27 | |
| 28 | #define CONVERT_ARG_HANDLE_CHECKED(Type, name, index) \ |
| 29 | RUNTIME_ASSERT(args[index]->Is##Type()); \ |
| 30 | Handle<Type> name = args.at<Type>(index); |
| 31 | |
| 32 | #define CONVERT_NUMBER_ARG_HANDLE_CHECKED(name, index) \ |
| 33 | RUNTIME_ASSERT(args[index]->IsNumber()); \ |
| 34 | Handle<Object> name = args.at<Object>(index); |
| 35 | |
| 36 | // Cast the given object to a boolean and store it in a variable with |
| 37 | // the given name. If the object is not a boolean call IllegalOperation |
| 38 | // and return. |
| 39 | #define CONVERT_BOOLEAN_ARG_CHECKED(name, index) \ |
| 40 | RUNTIME_ASSERT(args[index]->IsBoolean()); \ |
| 41 | bool name = args[index]->IsTrue(); |
| 42 | |
| 43 | // Cast the given argument to a Smi and store its value in an int variable |
| 44 | // with the given name. If the argument is not a Smi call IllegalOperation |
| 45 | // and return. |
| 46 | #define CONVERT_SMI_ARG_CHECKED(name, index) \ |
| 47 | RUNTIME_ASSERT(args[index]->IsSmi()); \ |
| 48 | int name = args.smi_at(index); |
| 49 | |
| 50 | // Cast the given argument to a double and store it in a variable with |
| 51 | // the given name. If the argument is not a number (as opposed to |
| 52 | // the number not-a-number) call IllegalOperation and return. |
| 53 | #define CONVERT_DOUBLE_ARG_CHECKED(name, index) \ |
| 54 | RUNTIME_ASSERT(args[index]->IsNumber()); \ |
| 55 | double name = args.number_at(index); |
| 56 | |
| 57 | // Call the specified converter on the object *comand store the result in |
| 58 | // a variable of the specified type with the given name. If the |
| 59 | // object is not a Number call IllegalOperation and return. |
| 60 | #define CONVERT_NUMBER_CHECKED(type, name, Type, obj) \ |
| 61 | RUNTIME_ASSERT(obj->IsNumber()); \ |
| 62 | type name = NumberTo##Type(obj); |
| 63 | |
| 64 | |
| 65 | // Cast the given argument to PropertyDetails and store its value in a |
| 66 | // variable with the given name. If the argument is not a Smi call |
| 67 | // IllegalOperation and return. |
| 68 | #define CONVERT_PROPERTY_DETAILS_CHECKED(name, index) \ |
| 69 | RUNTIME_ASSERT(args[index]->IsSmi()); \ |
| 70 | PropertyDetails name = PropertyDetails(Smi::cast(args[index])); |
| 71 | |
| 72 | |
| 73 | // Assert that the given argument has a valid value for a StrictMode |
| 74 | // and store it in a StrictMode variable with the given name. |
| 75 | #define CONVERT_STRICT_MODE_ARG_CHECKED(name, index) \ |
| 76 | RUNTIME_ASSERT(args[index]->IsSmi()); \ |
| 77 | RUNTIME_ASSERT(args.smi_at(index) == STRICT || \ |
| 78 | args.smi_at(index) == SLOPPY); \ |
| 79 | StrictMode name = static_cast<StrictMode>(args.smi_at(index)); |
| 80 | |
| 81 | |
| 82 | // Assert that the given argument is a number within the Int32 range |
| 83 | // and convert it to int32_t. If the argument is not an Int32 call |
| 84 | // IllegalOperation and return. |
| 85 | #define CONVERT_INT32_ARG_CHECKED(name, index) \ |
| 86 | RUNTIME_ASSERT(args[index]->IsNumber()); \ |
| 87 | int32_t name = 0; \ |
| 88 | RUNTIME_ASSERT(args[index]->ToInt32(&name)); |
| 89 | |
| 90 | |
| 91 | // A mechanism to return a pair of Object pointers in registers (if possible). |
| 92 | // How this is achieved is calling convention-dependent. |
| 93 | // All currently supported x86 compiles uses calling conventions that are cdecl |
| 94 | // variants where a 64-bit value is returned in two 32-bit registers |
| 95 | // (edx:eax on ia32, r1:r0 on ARM). |
| 96 | // In AMD-64 calling convention a struct of two pointers is returned in rdx:rax. |
| 97 | // In Win64 calling convention, a struct of two pointers is returned in memory, |
| 98 | // allocated by the caller, and passed as a pointer in a hidden first parameter. |
| 99 | #ifdef V8_HOST_ARCH_64_BIT |
| 100 | struct ObjectPair { |
| 101 | Object* x; |
| 102 | Object* y; |
| 103 | }; |
| 104 | |
| 105 | |
| 106 | static inline ObjectPair MakePair(Object* x, Object* y) { |
| 107 | ObjectPair result = {x, y}; |
| 108 | // Pointers x and y returned in rax and rdx, in AMD-x64-abi. |
| 109 | // In Win64 they are assigned to a hidden first argument. |
| 110 | return result; |
| 111 | } |
| 112 | #elif V8_TARGET_ARCH_X64 && V8_TARGET_ARCH_32_BIT |
| 113 | // For x32 a 128-bit struct return is done as rax and rdx from the ObjectPair |
| 114 | // are used in the full codegen and Crankshaft compiler. An alternative is |
| 115 | // using uint64_t and modifying full codegen and Crankshaft compiler. |
| 116 | struct ObjectPair { |
| 117 | Object* x; |
| 118 | uint32_t x_upper; |
| 119 | Object* y; |
| 120 | uint32_t y_upper; |
| 121 | }; |
| 122 | |
| 123 | |
| 124 | static inline ObjectPair MakePair(Object* x, Object* y) { |
| 125 | ObjectPair result = {x, 0, y, 0}; |
| 126 | // Pointers x and y returned in rax and rdx, in x32-abi. |
| 127 | return result; |
| 128 | } |
| 129 | #else |
| 130 | typedef uint64_t ObjectPair; |
| 131 | static inline ObjectPair MakePair(Object* x, Object* y) { |
| 132 | #if defined(V8_TARGET_LITTLE_ENDIAN) |
| 133 | return reinterpret_cast<uint32_t>(x) | |
| 134 | (reinterpret_cast<ObjectPair>(y) << 32); |
| 135 | #elif defined(V8_TARGET_BIG_ENDIAN) |
| 136 | return reinterpret_cast<uint32_t>(y) | |
| 137 | (reinterpret_cast<ObjectPair>(x) << 32); |
| 138 | #else |
| 139 | #error Unknown endianness |
| 140 | #endif |
| 141 | } |
| 142 | #endif |
| 143 | |
| 144 | } |
| 145 | } // namespace v8::internal |
| 146 | |
| 147 | #endif // V8_RUNTIME_RUNTIME_UTILS_H_ |