OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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-utils-gen.h" |
| 6 #include "src/builtins/builtins.h" |
| 7 #include "src/code-stub-assembler.h" |
| 8 #include "src/objects.h" |
| 9 |
| 10 namespace v8 { |
| 11 namespace internal { |
| 12 |
| 13 using compiler::Node; |
| 14 |
| 15 class SharedArrayBufferBuiltinsAssembler : public CodeStubAssembler { |
| 16 public: |
| 17 explicit SharedArrayBufferBuiltinsAssembler( |
| 18 compiler::CodeAssemblerState* state) |
| 19 : CodeStubAssembler(state) {} |
| 20 |
| 21 protected: |
| 22 void ValidateSharedTypedArray(Node* tagged, Node* context, |
| 23 Node** out_instance_type, |
| 24 Node** out_backing_store); |
| 25 Node* ConvertTaggedAtomicIndexToWord32(Node* tagged, Node* context, |
| 26 Node** number_index); |
| 27 void ValidateAtomicIndex(Node* index_word, Node* array_length_word, |
| 28 Node* context); |
| 29 }; |
| 30 |
| 31 void SharedArrayBufferBuiltinsAssembler::ValidateSharedTypedArray( |
| 32 Node* tagged, Node* context, Node** out_instance_type, |
| 33 Node** out_backing_store) { |
| 34 Label not_float_or_clamped(this), invalid(this); |
| 35 |
| 36 // Fail if it is not a heap object. |
| 37 GotoIf(TaggedIsSmi(tagged), &invalid); |
| 38 |
| 39 // Fail if the array's instance type is not JSTypedArray. |
| 40 GotoIf(Word32NotEqual(LoadInstanceType(tagged), |
| 41 Int32Constant(JS_TYPED_ARRAY_TYPE)), |
| 42 &invalid); |
| 43 |
| 44 // Fail if the array's JSArrayBuffer is not shared. |
| 45 Node* array_buffer = LoadObjectField(tagged, JSTypedArray::kBufferOffset); |
| 46 Node* bitfield = LoadObjectField(array_buffer, JSArrayBuffer::kBitFieldOffset, |
| 47 MachineType::Uint32()); |
| 48 GotoIfNot(IsSetWord32<JSArrayBuffer::IsShared>(bitfield), &invalid); |
| 49 |
| 50 // Fail if the array's element type is float32, float64 or clamped. |
| 51 Node* elements_instance_type = |
| 52 LoadInstanceType(LoadObjectField(tagged, JSObject::kElementsOffset)); |
| 53 STATIC_ASSERT(FIXED_INT8_ARRAY_TYPE < FIXED_FLOAT32_ARRAY_TYPE); |
| 54 STATIC_ASSERT(FIXED_INT16_ARRAY_TYPE < FIXED_FLOAT32_ARRAY_TYPE); |
| 55 STATIC_ASSERT(FIXED_INT32_ARRAY_TYPE < FIXED_FLOAT32_ARRAY_TYPE); |
| 56 STATIC_ASSERT(FIXED_UINT8_ARRAY_TYPE < FIXED_FLOAT32_ARRAY_TYPE); |
| 57 STATIC_ASSERT(FIXED_UINT16_ARRAY_TYPE < FIXED_FLOAT32_ARRAY_TYPE); |
| 58 STATIC_ASSERT(FIXED_UINT32_ARRAY_TYPE < FIXED_FLOAT32_ARRAY_TYPE); |
| 59 Branch(Int32LessThan(elements_instance_type, |
| 60 Int32Constant(FIXED_FLOAT32_ARRAY_TYPE)), |
| 61 ¬_float_or_clamped, &invalid); |
| 62 |
| 63 Bind(&invalid); |
| 64 { |
| 65 CallRuntime(Runtime::kThrowNotIntegerSharedTypedArrayError, context, |
| 66 tagged); |
| 67 Unreachable(); |
| 68 } |
| 69 |
| 70 Bind(¬_float_or_clamped); |
| 71 *out_instance_type = elements_instance_type; |
| 72 |
| 73 Node* backing_store = |
| 74 LoadObjectField(array_buffer, JSArrayBuffer::kBackingStoreOffset); |
| 75 Node* byte_offset = ChangeUint32ToWord(TruncateTaggedToWord32( |
| 76 context, LoadObjectField(tagged, JSArrayBufferView::kByteOffsetOffset))); |
| 77 *out_backing_store = |
| 78 IntPtrAdd(BitcastTaggedToWord(backing_store), byte_offset); |
| 79 } |
| 80 |
| 81 // https://tc39.github.io/ecmascript_sharedmem/shmem.html#Atomics.ValidateAtomic
Access |
| 82 Node* SharedArrayBufferBuiltinsAssembler::ConvertTaggedAtomicIndexToWord32( |
| 83 Node* tagged, Node* context, Node** number_index) { |
| 84 Variable var_result(this, MachineRepresentation::kWord32); |
| 85 |
| 86 // TODO(jkummerow): Skip ToNumber call when |tagged| is a number already. |
| 87 // Maybe this can be unified with other tagged-to-index conversions? |
| 88 // Why does this return an int32, and not an intptr? |
| 89 // Why is there the additional |number_index| output parameter? |
| 90 Callable to_number = CodeFactory::ToNumber(isolate()); |
| 91 *number_index = CallStub(to_number, context, tagged); |
| 92 Label done(this, &var_result); |
| 93 |
| 94 Label if_numberissmi(this), if_numberisnotsmi(this); |
| 95 Branch(TaggedIsSmi(*number_index), &if_numberissmi, &if_numberisnotsmi); |
| 96 |
| 97 Bind(&if_numberissmi); |
| 98 { |
| 99 var_result.Bind(SmiToWord32(*number_index)); |
| 100 Goto(&done); |
| 101 } |
| 102 |
| 103 Bind(&if_numberisnotsmi); |
| 104 { |
| 105 Node* number_index_value = LoadHeapNumberValue(*number_index); |
| 106 Node* access_index = TruncateFloat64ToWord32(number_index_value); |
| 107 Node* test_index = ChangeInt32ToFloat64(access_index); |
| 108 |
| 109 Label if_indexesareequal(this), if_indexesarenotequal(this); |
| 110 Branch(Float64Equal(number_index_value, test_index), &if_indexesareequal, |
| 111 &if_indexesarenotequal); |
| 112 |
| 113 Bind(&if_indexesareequal); |
| 114 { |
| 115 var_result.Bind(access_index); |
| 116 Goto(&done); |
| 117 } |
| 118 |
| 119 Bind(&if_indexesarenotequal); |
| 120 { |
| 121 CallRuntime(Runtime::kThrowInvalidAtomicAccessIndexError, context); |
| 122 Unreachable(); |
| 123 } |
| 124 } |
| 125 |
| 126 Bind(&done); |
| 127 return var_result.value(); |
| 128 } |
| 129 |
| 130 void SharedArrayBufferBuiltinsAssembler::ValidateAtomicIndex( |
| 131 Node* index_word, Node* array_length_word, Node* context) { |
| 132 // Check if the index is in bounds. If not, throw RangeError. |
| 133 Label check_passed(this); |
| 134 GotoIf(Uint32LessThan(index_word, array_length_word), &check_passed); |
| 135 |
| 136 CallRuntime(Runtime::kThrowInvalidAtomicAccessIndexError, context); |
| 137 Unreachable(); |
| 138 |
| 139 Bind(&check_passed); |
| 140 } |
| 141 |
| 142 TF_BUILTIN(AtomicsLoad, SharedArrayBufferBuiltinsAssembler) { |
| 143 Node* array = Parameter(1); |
| 144 Node* index = Parameter(2); |
| 145 Node* context = Parameter(3 + 2); |
| 146 |
| 147 Node* instance_type; |
| 148 Node* backing_store; |
| 149 ValidateSharedTypedArray(array, context, &instance_type, &backing_store); |
| 150 |
| 151 Node* index_integer; |
| 152 Node* index_word32 = |
| 153 ConvertTaggedAtomicIndexToWord32(index, context, &index_integer); |
| 154 Node* array_length_word32 = TruncateTaggedToWord32( |
| 155 context, LoadObjectField(array, JSTypedArray::kLengthOffset)); |
| 156 ValidateAtomicIndex(index_word32, array_length_word32, context); |
| 157 Node* index_word = ChangeUint32ToWord(index_word32); |
| 158 |
| 159 Label i8(this), u8(this), i16(this), u16(this), i32(this), u32(this), |
| 160 other(this); |
| 161 int32_t case_values[] = { |
| 162 FIXED_INT8_ARRAY_TYPE, FIXED_UINT8_ARRAY_TYPE, FIXED_INT16_ARRAY_TYPE, |
| 163 FIXED_UINT16_ARRAY_TYPE, FIXED_INT32_ARRAY_TYPE, FIXED_UINT32_ARRAY_TYPE, |
| 164 }; |
| 165 Label* case_labels[] = { |
| 166 &i8, &u8, &i16, &u16, &i32, &u32, |
| 167 }; |
| 168 Switch(instance_type, &other, case_values, case_labels, |
| 169 arraysize(case_labels)); |
| 170 |
| 171 Bind(&i8); |
| 172 Return(SmiFromWord32( |
| 173 AtomicLoad(MachineType::Int8(), backing_store, index_word))); |
| 174 |
| 175 Bind(&u8); |
| 176 Return(SmiFromWord32( |
| 177 AtomicLoad(MachineType::Uint8(), backing_store, index_word))); |
| 178 |
| 179 Bind(&i16); |
| 180 Return(SmiFromWord32( |
| 181 AtomicLoad(MachineType::Int16(), backing_store, WordShl(index_word, 1)))); |
| 182 |
| 183 Bind(&u16); |
| 184 Return(SmiFromWord32(AtomicLoad(MachineType::Uint16(), backing_store, |
| 185 WordShl(index_word, 1)))); |
| 186 |
| 187 Bind(&i32); |
| 188 Return(ChangeInt32ToTagged( |
| 189 AtomicLoad(MachineType::Int32(), backing_store, WordShl(index_word, 2)))); |
| 190 |
| 191 Bind(&u32); |
| 192 Return(ChangeUint32ToTagged(AtomicLoad(MachineType::Uint32(), backing_store, |
| 193 WordShl(index_word, 2)))); |
| 194 |
| 195 // This shouldn't happen, we've already validated the type. |
| 196 Bind(&other); |
| 197 Unreachable(); |
| 198 } |
| 199 |
| 200 TF_BUILTIN(AtomicsStore, SharedArrayBufferBuiltinsAssembler) { |
| 201 Node* array = Parameter(1); |
| 202 Node* index = Parameter(2); |
| 203 Node* value = Parameter(3); |
| 204 Node* context = Parameter(4 + 2); |
| 205 |
| 206 Node* instance_type; |
| 207 Node* backing_store; |
| 208 ValidateSharedTypedArray(array, context, &instance_type, &backing_store); |
| 209 |
| 210 Node* index_integer; |
| 211 Node* index_word32 = |
| 212 ConvertTaggedAtomicIndexToWord32(index, context, &index_integer); |
| 213 Node* array_length_word32 = TruncateTaggedToWord32( |
| 214 context, LoadObjectField(array, JSTypedArray::kLengthOffset)); |
| 215 ValidateAtomicIndex(index_word32, array_length_word32, context); |
| 216 Node* index_word = ChangeUint32ToWord(index_word32); |
| 217 |
| 218 Node* value_integer = ToInteger(context, value); |
| 219 Node* value_word32 = TruncateTaggedToWord32(context, value_integer); |
| 220 |
| 221 Label u8(this), u16(this), u32(this), other(this); |
| 222 int32_t case_values[] = { |
| 223 FIXED_INT8_ARRAY_TYPE, FIXED_UINT8_ARRAY_TYPE, FIXED_INT16_ARRAY_TYPE, |
| 224 FIXED_UINT16_ARRAY_TYPE, FIXED_INT32_ARRAY_TYPE, FIXED_UINT32_ARRAY_TYPE, |
| 225 }; |
| 226 Label* case_labels[] = { |
| 227 &u8, &u8, &u16, &u16, &u32, &u32, |
| 228 }; |
| 229 Switch(instance_type, &other, case_values, case_labels, |
| 230 arraysize(case_labels)); |
| 231 |
| 232 Bind(&u8); |
| 233 AtomicStore(MachineRepresentation::kWord8, backing_store, index_word, |
| 234 value_word32); |
| 235 Return(value_integer); |
| 236 |
| 237 Bind(&u16); |
| 238 AtomicStore(MachineRepresentation::kWord16, backing_store, |
| 239 WordShl(index_word, 1), value_word32); |
| 240 Return(value_integer); |
| 241 |
| 242 Bind(&u32); |
| 243 AtomicStore(MachineRepresentation::kWord32, backing_store, |
| 244 WordShl(index_word, 2), value_word32); |
| 245 Return(value_integer); |
| 246 |
| 247 // This shouldn't happen, we've already validated the type. |
| 248 Bind(&other); |
| 249 Unreachable(); |
| 250 } |
| 251 |
| 252 TF_BUILTIN(AtomicsExchange, SharedArrayBufferBuiltinsAssembler) { |
| 253 Node* array = Parameter(1); |
| 254 Node* index = Parameter(2); |
| 255 Node* value = Parameter(3); |
| 256 Node* context = Parameter(4 + 2); |
| 257 |
| 258 Node* instance_type; |
| 259 Node* backing_store; |
| 260 ValidateSharedTypedArray(array, context, &instance_type, &backing_store); |
| 261 |
| 262 Node* index_integer; |
| 263 Node* index_word32 = |
| 264 ConvertTaggedAtomicIndexToWord32(index, context, &index_integer); |
| 265 Node* array_length_word32 = TruncateTaggedToWord32( |
| 266 context, LoadObjectField(array, JSTypedArray::kLengthOffset)); |
| 267 ValidateAtomicIndex(index_word32, array_length_word32, context); |
| 268 |
| 269 Node* value_integer = ToInteger(context, value); |
| 270 |
| 271 #if V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_PPC64 || \ |
| 272 V8_TARGET_ARCH_PPC |
| 273 Return(CallRuntime(Runtime::kAtomicsExchange, context, array, index_integer, |
| 274 value_integer)); |
| 275 #else |
| 276 Node* index_word = ChangeUint32ToWord(index_word32); |
| 277 |
| 278 Node* value_word32 = TruncateTaggedToWord32(context, value_integer); |
| 279 |
| 280 Label i8(this), u8(this), i16(this), u16(this), i32(this), u32(this), |
| 281 other(this); |
| 282 int32_t case_values[] = { |
| 283 FIXED_INT8_ARRAY_TYPE, FIXED_UINT8_ARRAY_TYPE, FIXED_INT16_ARRAY_TYPE, |
| 284 FIXED_UINT16_ARRAY_TYPE, FIXED_INT32_ARRAY_TYPE, FIXED_UINT32_ARRAY_TYPE, |
| 285 }; |
| 286 Label* case_labels[] = { |
| 287 &i8, &u8, &i16, &u16, &i32, &u32, |
| 288 }; |
| 289 Switch(instance_type, &other, case_values, case_labels, |
| 290 arraysize(case_labels)); |
| 291 |
| 292 Bind(&i8); |
| 293 Return(SmiFromWord32(AtomicExchange(MachineType::Int8(), backing_store, |
| 294 index_word, value_word32))); |
| 295 |
| 296 Bind(&u8); |
| 297 Return(SmiFromWord32(AtomicExchange(MachineType::Uint8(), backing_store, |
| 298 index_word, value_word32))); |
| 299 |
| 300 Bind(&i16); |
| 301 Return(SmiFromWord32(AtomicExchange(MachineType::Int16(), backing_store, |
| 302 WordShl(index_word, 1), value_word32))); |
| 303 |
| 304 Bind(&u16); |
| 305 Return(SmiFromWord32(AtomicExchange(MachineType::Uint16(), backing_store, |
| 306 WordShl(index_word, 1), value_word32))); |
| 307 |
| 308 Bind(&i32); |
| 309 Return(ChangeInt32ToTagged(AtomicExchange(MachineType::Int32(), backing_store, |
| 310 WordShl(index_word, 2), |
| 311 value_word32))); |
| 312 |
| 313 Bind(&u32); |
| 314 Return(ChangeUint32ToTagged( |
| 315 AtomicExchange(MachineType::Uint32(), backing_store, |
| 316 WordShl(index_word, 2), value_word32))); |
| 317 |
| 318 // This shouldn't happen, we've already validated the type. |
| 319 Bind(&other); |
| 320 Unreachable(); |
| 321 #endif // V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_PPC64 |
| 322 // || V8_TARGET_ARCH_PPC || V8_TARGET_ARCH_S390 || V8_TARGET_ARCH_S390X |
| 323 } |
| 324 |
| 325 } // namespace internal |
| 326 } // namespace v8 |
OLD | NEW |