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-wasm-gen.h"
6
7 #include "src/builtins/builtins-utils-gen.h"
8 #include "src/codegen/code-stub-assembler.h"
9 #include "src/codegen/interface-descriptors.h"
10 #include "src/objects/objects-inl.h"
11 #include "src/wasm/wasm-objects.h"
12
13 namespace v8 {
14 namespace internal {
15
LoadInstanceFromFrame()16 TNode<WasmInstanceObject> WasmBuiltinsAssembler::LoadInstanceFromFrame() {
17 return CAST(LoadFromParentFrame(WasmFrameConstants::kWasmInstanceOffset));
18 }
19
LoadContextFromInstance(TNode<WasmInstanceObject> instance)20 TNode<NativeContext> WasmBuiltinsAssembler::LoadContextFromInstance(
21 TNode<WasmInstanceObject> instance) {
22 return CAST(Load(MachineType::AnyTagged(), instance,
23 IntPtrConstant(WasmInstanceObject::kNativeContextOffset -
24 kHeapObjectTag)));
25 }
26
LoadTablesFromInstance(TNode<WasmInstanceObject> instance)27 TNode<FixedArray> WasmBuiltinsAssembler::LoadTablesFromInstance(
28 TNode<WasmInstanceObject> instance) {
29 return LoadObjectField<FixedArray>(instance,
30 WasmInstanceObject::kTablesOffset);
31 }
32
LoadInternalFunctionsFromInstance(TNode<WasmInstanceObject> instance)33 TNode<FixedArray> WasmBuiltinsAssembler::LoadInternalFunctionsFromInstance(
34 TNode<WasmInstanceObject> instance) {
35 return LoadObjectField<FixedArray>(
36 instance, WasmInstanceObject::kWasmInternalFunctionsOffset);
37 }
38
LoadManagedObjectMapsFromInstance(TNode<WasmInstanceObject> instance)39 TNode<FixedArray> WasmBuiltinsAssembler::LoadManagedObjectMapsFromInstance(
40 TNode<WasmInstanceObject> instance) {
41 return LoadObjectField<FixedArray>(
42 instance, WasmInstanceObject::kManagedObjectMapsOffset);
43 }
44
TF_BUILTIN(WasmFloat32ToNumber,WasmBuiltinsAssembler)45 TF_BUILTIN(WasmFloat32ToNumber, WasmBuiltinsAssembler) {
46 auto val = UncheckedParameter<Float32T>(Descriptor::kValue);
47 Return(ChangeFloat32ToTagged(val));
48 }
49
TF_BUILTIN(WasmFloat64ToNumber,WasmBuiltinsAssembler)50 TF_BUILTIN(WasmFloat64ToNumber, WasmBuiltinsAssembler) {
51 auto val = UncheckedParameter<Float64T>(Descriptor::kValue);
52 Return(ChangeFloat64ToTagged(val));
53 }
54
TF_BUILTIN(WasmI32AtomicWait32,WasmBuiltinsAssembler)55 TF_BUILTIN(WasmI32AtomicWait32, WasmBuiltinsAssembler) {
56 if (!Is32()) {
57 Unreachable();
58 return;
59 }
60
61 auto address = UncheckedParameter<Uint32T>(Descriptor::kAddress);
62 TNode<Number> address_number = ChangeUint32ToTagged(address);
63
64 auto expected_value = UncheckedParameter<Int32T>(Descriptor::kExpectedValue);
65 TNode<Number> expected_value_number = ChangeInt32ToTagged(expected_value);
66
67 auto timeout_low = UncheckedParameter<IntPtrT>(Descriptor::kTimeoutLow);
68 auto timeout_high = UncheckedParameter<IntPtrT>(Descriptor::kTimeoutHigh);
69 TNode<BigInt> timeout = BigIntFromInt32Pair(timeout_low, timeout_high);
70
71 TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
72 TNode<Context> context = LoadContextFromInstance(instance);
73
74 TNode<Smi> result_smi =
75 CAST(CallRuntime(Runtime::kWasmI32AtomicWait, context, instance,
76 address_number, expected_value_number, timeout));
77 Return(Unsigned(SmiToInt32(result_smi)));
78 }
79
TF_BUILTIN(WasmI64AtomicWait32,WasmBuiltinsAssembler)80 TF_BUILTIN(WasmI64AtomicWait32, WasmBuiltinsAssembler) {
81 if (!Is32()) {
82 Unreachable();
83 return;
84 }
85
86 auto address = UncheckedParameter<Uint32T>(Descriptor::kAddress);
87 TNode<Number> address_number = ChangeUint32ToTagged(address);
88
89 auto expected_value_low =
90 UncheckedParameter<IntPtrT>(Descriptor::kExpectedValueLow);
91 auto expected_value_high =
92 UncheckedParameter<IntPtrT>(Descriptor::kExpectedValueHigh);
93 TNode<BigInt> expected_value =
94 BigIntFromInt32Pair(expected_value_low, expected_value_high);
95
96 auto timeout_low = UncheckedParameter<IntPtrT>(Descriptor::kTimeoutLow);
97 auto timeout_high = UncheckedParameter<IntPtrT>(Descriptor::kTimeoutHigh);
98 TNode<BigInt> timeout = BigIntFromInt32Pair(timeout_low, timeout_high);
99
100 TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
101 TNode<Context> context = LoadContextFromInstance(instance);
102
103 TNode<Smi> result_smi =
104 CAST(CallRuntime(Runtime::kWasmI64AtomicWait, context, instance,
105 address_number, expected_value, timeout));
106 Return(Unsigned(SmiToInt32(result_smi)));
107 }
108
TF_BUILTIN(JSToWasmLazyDeoptContinuation,WasmBuiltinsAssembler)109 TF_BUILTIN(JSToWasmLazyDeoptContinuation, WasmBuiltinsAssembler) {
110 // Reset thread_in_wasm_flag.
111 TNode<ExternalReference> thread_in_wasm_flag_address_address =
112 ExternalConstant(
113 ExternalReference::thread_in_wasm_flag_address_address(isolate()));
114 auto thread_in_wasm_flag_address =
115 Load<RawPtrT>(thread_in_wasm_flag_address_address);
116 StoreNoWriteBarrier(MachineRepresentation::kWord32,
117 thread_in_wasm_flag_address, Int32Constant(0));
118
119 // Return the argument.
120 auto value = Parameter<Object>(Descriptor::kArgument);
121 Return(value);
122 }
123
124 } // namespace internal
125 } // namespace v8
126