OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "src/builtins/builtins.h" |
| 6 #include "src/builtins/builtins-utils.h" |
| 7 |
| 8 #include "src/code-factory.h" |
| 9 |
| 10 namespace v8 { |
| 11 namespace internal { |
| 12 |
| 13 // ES7 sharedmem 6.3.4.1 get SharedArrayBuffer.prototype.byteLength |
| 14 BUILTIN(SharedArrayBufferPrototypeGetByteLength) { |
| 15 HandleScope scope(isolate); |
| 16 CHECK_RECEIVER(JSArrayBuffer, array_buffer, |
| 17 "get SharedArrayBuffer.prototype.byteLength"); |
| 18 if (!array_buffer->is_shared()) { |
| 19 THROW_NEW_ERROR_RETURN_FAILURE( |
| 20 isolate, NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, |
| 21 isolate->factory()->NewStringFromAsciiChecked( |
| 22 "get SharedArrayBuffer.prototype.byteLength"), |
| 23 args.receiver())); |
| 24 } |
| 25 return array_buffer->byte_length(); |
| 26 } |
| 27 |
| 28 namespace { |
| 29 |
| 30 void ValidateSharedTypedArray(CodeStubAssembler* a, compiler::Node* tagged, |
| 31 compiler::Node* context, |
| 32 compiler::Node** out_instance_type, |
| 33 compiler::Node** out_backing_store) { |
| 34 using namespace compiler; |
| 35 CodeStubAssembler::Label is_smi(a), not_smi(a), is_typed_array(a), |
| 36 not_typed_array(a), is_shared(a), not_shared(a), is_float_or_clamped(a), |
| 37 not_float_or_clamped(a), invalid(a); |
| 38 |
| 39 // Fail if it is not a heap object. |
| 40 a->Branch(a->WordIsSmi(tagged), &is_smi, ¬_smi); |
| 41 a->Bind(&is_smi); |
| 42 a->Goto(&invalid); |
| 43 |
| 44 // Fail if the array's instance type is not JSTypedArray. |
| 45 a->Bind(¬_smi); |
| 46 a->Branch(a->WordEqual(a->LoadInstanceType(tagged), |
| 47 a->Int32Constant(JS_TYPED_ARRAY_TYPE)), |
| 48 &is_typed_array, ¬_typed_array); |
| 49 a->Bind(¬_typed_array); |
| 50 a->Goto(&invalid); |
| 51 |
| 52 // Fail if the array's JSArrayBuffer is not shared. |
| 53 a->Bind(&is_typed_array); |
| 54 Node* array_buffer = a->LoadObjectField(tagged, JSTypedArray::kBufferOffset); |
| 55 Node* is_buffer_shared = a->BitFieldDecode<JSArrayBuffer::IsShared>( |
| 56 a->LoadObjectField(array_buffer, JSArrayBuffer::kBitFieldSlot)); |
| 57 a->Branch(is_buffer_shared, &is_shared, ¬_shared); |
| 58 a->Bind(¬_shared); |
| 59 a->Goto(&invalid); |
| 60 |
| 61 // Fail if the array's element type is float32, float64 or clamped. |
| 62 a->Bind(&is_shared); |
| 63 Node* elements_instance_type = a->LoadInstanceType( |
| 64 a->LoadObjectField(tagged, JSObject::kElementsOffset)); |
| 65 STATIC_ASSERT(FIXED_INT8_ARRAY_TYPE < FIXED_FLOAT32_ARRAY_TYPE); |
| 66 STATIC_ASSERT(FIXED_INT16_ARRAY_TYPE < FIXED_FLOAT32_ARRAY_TYPE); |
| 67 STATIC_ASSERT(FIXED_INT32_ARRAY_TYPE < FIXED_FLOAT32_ARRAY_TYPE); |
| 68 STATIC_ASSERT(FIXED_UINT8_ARRAY_TYPE < FIXED_FLOAT32_ARRAY_TYPE); |
| 69 STATIC_ASSERT(FIXED_UINT16_ARRAY_TYPE < FIXED_FLOAT32_ARRAY_TYPE); |
| 70 STATIC_ASSERT(FIXED_UINT32_ARRAY_TYPE < FIXED_FLOAT32_ARRAY_TYPE); |
| 71 a->Branch(a->Int32LessThan(elements_instance_type, |
| 72 a->Int32Constant(FIXED_FLOAT32_ARRAY_TYPE)), |
| 73 ¬_float_or_clamped, &is_float_or_clamped); |
| 74 a->Bind(&is_float_or_clamped); |
| 75 a->Goto(&invalid); |
| 76 |
| 77 a->Bind(&invalid); |
| 78 a->CallRuntime(Runtime::kThrowNotIntegerSharedTypedArrayError, context, |
| 79 tagged); |
| 80 a->Return(a->UndefinedConstant()); |
| 81 |
| 82 a->Bind(¬_float_or_clamped); |
| 83 *out_instance_type = elements_instance_type; |
| 84 |
| 85 Node* backing_store = |
| 86 a->LoadObjectField(array_buffer, JSArrayBuffer::kBackingStoreOffset); |
| 87 Node* byte_offset = a->ChangeUint32ToWord(a->TruncateTaggedToWord32( |
| 88 context, |
| 89 a->LoadObjectField(tagged, JSArrayBufferView::kByteOffsetOffset))); |
| 90 *out_backing_store = a->IntPtrAdd(backing_store, byte_offset); |
| 91 } |
| 92 |
| 93 // https://tc39.github.io/ecmascript_sharedmem/shmem.html#Atomics.ValidateAtomic
Access |
| 94 compiler::Node* ConvertTaggedAtomicIndexToWord32(CodeStubAssembler* a, |
| 95 compiler::Node* tagged, |
| 96 compiler::Node* context) { |
| 97 using namespace compiler; |
| 98 CodeStubAssembler::Variable var_result(a, MachineRepresentation::kWord32); |
| 99 |
| 100 Callable to_number = CodeFactory::ToNumber(a->isolate()); |
| 101 Node* number_index = a->CallStub(to_number, context, tagged); |
| 102 CodeStubAssembler::Label done(a, &var_result); |
| 103 |
| 104 CodeStubAssembler::Label if_numberissmi(a), if_numberisnotsmi(a); |
| 105 a->Branch(a->WordIsSmi(number_index), &if_numberissmi, &if_numberisnotsmi); |
| 106 |
| 107 a->Bind(&if_numberissmi); |
| 108 { |
| 109 var_result.Bind(a->SmiToWord32(number_index)); |
| 110 a->Goto(&done); |
| 111 } |
| 112 |
| 113 a->Bind(&if_numberisnotsmi); |
| 114 { |
| 115 Node* number_index_value = a->LoadHeapNumberValue(number_index); |
| 116 Node* access_index = a->TruncateFloat64ToWord32(number_index_value); |
| 117 Node* test_index = a->ChangeInt32ToFloat64(access_index); |
| 118 |
| 119 CodeStubAssembler::Label if_indexesareequal(a), if_indexesarenotequal(a); |
| 120 a->Branch(a->Float64Equal(number_index_value, test_index), |
| 121 &if_indexesareequal, &if_indexesarenotequal); |
| 122 |
| 123 a->Bind(&if_indexesareequal); |
| 124 { |
| 125 var_result.Bind(access_index); |
| 126 a->Goto(&done); |
| 127 } |
| 128 |
| 129 a->Bind(&if_indexesarenotequal); |
| 130 a->Return( |
| 131 a->CallRuntime(Runtime::kThrowInvalidAtomicAccessIndexError, context)); |
| 132 } |
| 133 |
| 134 a->Bind(&done); |
| 135 return var_result.value(); |
| 136 } |
| 137 |
| 138 void ValidateAtomicIndex(CodeStubAssembler* a, compiler::Node* index_word, |
| 139 compiler::Node* array_length_word, |
| 140 compiler::Node* context) { |
| 141 using namespace compiler; |
| 142 // Check if the index is in bounds. If not, throw RangeError. |
| 143 CodeStubAssembler::Label if_inbounds(a), if_notinbounds(a); |
| 144 a->Branch( |
| 145 a->WordOr(a->Int32LessThan(index_word, a->Int32Constant(0)), |
| 146 a->Int32GreaterThanOrEqual(index_word, array_length_word)), |
| 147 &if_notinbounds, &if_inbounds); |
| 148 a->Bind(&if_notinbounds); |
| 149 a->Return( |
| 150 a->CallRuntime(Runtime::kThrowInvalidAtomicAccessIndexError, context)); |
| 151 a->Bind(&if_inbounds); |
| 152 } |
| 153 |
| 154 } // anonymous namespace |
| 155 |
| 156 void Builtins::Generate_AtomicsLoad(CodeStubAssembler* a) { |
| 157 using namespace compiler; |
| 158 Node* array = a->Parameter(1); |
| 159 Node* index = a->Parameter(2); |
| 160 Node* context = a->Parameter(3 + 2); |
| 161 |
| 162 Node* instance_type; |
| 163 Node* backing_store; |
| 164 ValidateSharedTypedArray(a, array, context, &instance_type, &backing_store); |
| 165 |
| 166 Node* index_word32 = ConvertTaggedAtomicIndexToWord32(a, index, context); |
| 167 Node* array_length_word32 = a->TruncateTaggedToWord32( |
| 168 context, a->LoadObjectField(array, JSTypedArray::kLengthOffset)); |
| 169 ValidateAtomicIndex(a, index_word32, array_length_word32, context); |
| 170 Node* index_word = a->ChangeUint32ToWord(index_word32); |
| 171 |
| 172 CodeStubAssembler::Label i8(a), u8(a), i16(a), u16(a), i32(a), u32(a), |
| 173 other(a); |
| 174 int32_t case_values[] = { |
| 175 FIXED_INT8_ARRAY_TYPE, FIXED_UINT8_ARRAY_TYPE, FIXED_INT16_ARRAY_TYPE, |
| 176 FIXED_UINT16_ARRAY_TYPE, FIXED_INT32_ARRAY_TYPE, FIXED_UINT32_ARRAY_TYPE, |
| 177 }; |
| 178 CodeStubAssembler::Label* case_labels[] = { |
| 179 &i8, &u8, &i16, &u16, &i32, &u32, |
| 180 }; |
| 181 a->Switch(instance_type, &other, case_values, case_labels, |
| 182 arraysize(case_labels)); |
| 183 |
| 184 a->Bind(&i8); |
| 185 a->Return( |
| 186 a->SmiTag(a->AtomicLoad(MachineType::Int8(), backing_store, index_word))); |
| 187 |
| 188 a->Bind(&u8); |
| 189 a->Return(a->SmiTag( |
| 190 a->AtomicLoad(MachineType::Uint8(), backing_store, index_word))); |
| 191 |
| 192 a->Bind(&i16); |
| 193 a->Return(a->SmiTag(a->AtomicLoad(MachineType::Int16(), backing_store, |
| 194 a->WordShl(index_word, 1)))); |
| 195 |
| 196 a->Bind(&u16); |
| 197 a->Return(a->SmiTag(a->AtomicLoad(MachineType::Uint16(), backing_store, |
| 198 a->WordShl(index_word, 1)))); |
| 199 |
| 200 a->Bind(&i32); |
| 201 a->Return(a->ChangeInt32ToTagged(a->AtomicLoad( |
| 202 MachineType::Int32(), backing_store, a->WordShl(index_word, 2)))); |
| 203 |
| 204 a->Bind(&u32); |
| 205 a->Return(a->ChangeUint32ToTagged(a->AtomicLoad( |
| 206 MachineType::Uint32(), backing_store, a->WordShl(index_word, 2)))); |
| 207 |
| 208 // This shouldn't happen, we've already validated the type. |
| 209 a->Bind(&other); |
| 210 a->Return(a->Int32Constant(0)); |
| 211 } |
| 212 |
| 213 void Builtins::Generate_AtomicsStore(CodeStubAssembler* a) { |
| 214 using namespace compiler; |
| 215 Node* array = a->Parameter(1); |
| 216 Node* index = a->Parameter(2); |
| 217 Node* value = a->Parameter(3); |
| 218 Node* context = a->Parameter(4 + 2); |
| 219 |
| 220 Node* instance_type; |
| 221 Node* backing_store; |
| 222 ValidateSharedTypedArray(a, array, context, &instance_type, &backing_store); |
| 223 |
| 224 Node* index_word32 = ConvertTaggedAtomicIndexToWord32(a, index, context); |
| 225 Node* array_length_word32 = a->TruncateTaggedToWord32( |
| 226 context, a->LoadObjectField(array, JSTypedArray::kLengthOffset)); |
| 227 ValidateAtomicIndex(a, index_word32, array_length_word32, context); |
| 228 Node* index_word = a->ChangeUint32ToWord(index_word32); |
| 229 |
| 230 Callable to_integer = CodeFactory::ToInteger(a->isolate()); |
| 231 Node* value_integer = a->CallStub(to_integer, context, value); |
| 232 Node* value_word32 = a->TruncateTaggedToWord32(context, value_integer); |
| 233 |
| 234 CodeStubAssembler::Label u8(a), u16(a), u32(a), other(a); |
| 235 int32_t case_values[] = { |
| 236 FIXED_INT8_ARRAY_TYPE, FIXED_UINT8_ARRAY_TYPE, FIXED_INT16_ARRAY_TYPE, |
| 237 FIXED_UINT16_ARRAY_TYPE, FIXED_INT32_ARRAY_TYPE, FIXED_UINT32_ARRAY_TYPE, |
| 238 }; |
| 239 CodeStubAssembler::Label* case_labels[] = { |
| 240 &u8, &u8, &u16, &u16, &u32, &u32, |
| 241 }; |
| 242 a->Switch(instance_type, &other, case_values, case_labels, |
| 243 arraysize(case_labels)); |
| 244 |
| 245 a->Bind(&u8); |
| 246 a->AtomicStore(MachineRepresentation::kWord8, backing_store, index_word, |
| 247 value_word32); |
| 248 a->Return(value_integer); |
| 249 |
| 250 a->Bind(&u16); |
| 251 a->SmiTag(a->AtomicStore(MachineRepresentation::kWord16, backing_store, |
| 252 a->WordShl(index_word, 1), value_word32)); |
| 253 a->Return(value_integer); |
| 254 |
| 255 a->Bind(&u32); |
| 256 a->AtomicStore(MachineRepresentation::kWord32, backing_store, |
| 257 a->WordShl(index_word, 2), value_word32); |
| 258 a->Return(value_integer); |
| 259 |
| 260 // This shouldn't happen, we've already validated the type. |
| 261 a->Bind(&other); |
| 262 a->Return(a->Int32Constant(0)); |
| 263 } |
| 264 |
| 265 } // namespace internal |
| 266 } // namespace v8 |
OLD | NEW |