11cb0ef41Sopenharmony_ci// Copyright 2017 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/builtins/builtins-wasm-gen.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "src/builtins/builtins-utils-gen.h"
81cb0ef41Sopenharmony_ci#include "src/codegen/code-stub-assembler.h"
91cb0ef41Sopenharmony_ci#include "src/codegen/interface-descriptors.h"
101cb0ef41Sopenharmony_ci#include "src/objects/objects-inl.h"
111cb0ef41Sopenharmony_ci#include "src/wasm/wasm-objects.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace v8 {
141cb0ef41Sopenharmony_cinamespace internal {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciTNode<WasmInstanceObject> WasmBuiltinsAssembler::LoadInstanceFromFrame() {
171cb0ef41Sopenharmony_ci  return CAST(LoadFromParentFrame(WasmFrameConstants::kWasmInstanceOffset));
181cb0ef41Sopenharmony_ci}
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciTNode<NativeContext> WasmBuiltinsAssembler::LoadContextFromInstance(
211cb0ef41Sopenharmony_ci    TNode<WasmInstanceObject> instance) {
221cb0ef41Sopenharmony_ci  return CAST(Load(MachineType::AnyTagged(), instance,
231cb0ef41Sopenharmony_ci                   IntPtrConstant(WasmInstanceObject::kNativeContextOffset -
241cb0ef41Sopenharmony_ci                                  kHeapObjectTag)));
251cb0ef41Sopenharmony_ci}
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciTNode<FixedArray> WasmBuiltinsAssembler::LoadTablesFromInstance(
281cb0ef41Sopenharmony_ci    TNode<WasmInstanceObject> instance) {
291cb0ef41Sopenharmony_ci  return LoadObjectField<FixedArray>(instance,
301cb0ef41Sopenharmony_ci                                     WasmInstanceObject::kTablesOffset);
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciTNode<FixedArray> WasmBuiltinsAssembler::LoadInternalFunctionsFromInstance(
341cb0ef41Sopenharmony_ci    TNode<WasmInstanceObject> instance) {
351cb0ef41Sopenharmony_ci  return LoadObjectField<FixedArray>(
361cb0ef41Sopenharmony_ci      instance, WasmInstanceObject::kWasmInternalFunctionsOffset);
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciTNode<FixedArray> WasmBuiltinsAssembler::LoadManagedObjectMapsFromInstance(
401cb0ef41Sopenharmony_ci    TNode<WasmInstanceObject> instance) {
411cb0ef41Sopenharmony_ci  return LoadObjectField<FixedArray>(
421cb0ef41Sopenharmony_ci      instance, WasmInstanceObject::kManagedObjectMapsOffset);
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciTF_BUILTIN(WasmFloat32ToNumber, WasmBuiltinsAssembler) {
461cb0ef41Sopenharmony_ci  auto val = UncheckedParameter<Float32T>(Descriptor::kValue);
471cb0ef41Sopenharmony_ci  Return(ChangeFloat32ToTagged(val));
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciTF_BUILTIN(WasmFloat64ToNumber, WasmBuiltinsAssembler) {
511cb0ef41Sopenharmony_ci  auto val = UncheckedParameter<Float64T>(Descriptor::kValue);
521cb0ef41Sopenharmony_ci  Return(ChangeFloat64ToTagged(val));
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ciTF_BUILTIN(WasmI32AtomicWait32, WasmBuiltinsAssembler) {
561cb0ef41Sopenharmony_ci  if (!Is32()) {
571cb0ef41Sopenharmony_ci    Unreachable();
581cb0ef41Sopenharmony_ci    return;
591cb0ef41Sopenharmony_ci  }
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  auto address = UncheckedParameter<Uint32T>(Descriptor::kAddress);
621cb0ef41Sopenharmony_ci  TNode<Number> address_number = ChangeUint32ToTagged(address);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  auto expected_value = UncheckedParameter<Int32T>(Descriptor::kExpectedValue);
651cb0ef41Sopenharmony_ci  TNode<Number> expected_value_number = ChangeInt32ToTagged(expected_value);
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  auto timeout_low = UncheckedParameter<IntPtrT>(Descriptor::kTimeoutLow);
681cb0ef41Sopenharmony_ci  auto timeout_high = UncheckedParameter<IntPtrT>(Descriptor::kTimeoutHigh);
691cb0ef41Sopenharmony_ci  TNode<BigInt> timeout = BigIntFromInt32Pair(timeout_low, timeout_high);
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
721cb0ef41Sopenharmony_ci  TNode<Context> context = LoadContextFromInstance(instance);
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  TNode<Smi> result_smi =
751cb0ef41Sopenharmony_ci      CAST(CallRuntime(Runtime::kWasmI32AtomicWait, context, instance,
761cb0ef41Sopenharmony_ci                       address_number, expected_value_number, timeout));
771cb0ef41Sopenharmony_ci  Return(Unsigned(SmiToInt32(result_smi)));
781cb0ef41Sopenharmony_ci}
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ciTF_BUILTIN(WasmI64AtomicWait32, WasmBuiltinsAssembler) {
811cb0ef41Sopenharmony_ci  if (!Is32()) {
821cb0ef41Sopenharmony_ci    Unreachable();
831cb0ef41Sopenharmony_ci    return;
841cb0ef41Sopenharmony_ci  }
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  auto address = UncheckedParameter<Uint32T>(Descriptor::kAddress);
871cb0ef41Sopenharmony_ci  TNode<Number> address_number = ChangeUint32ToTagged(address);
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  auto expected_value_low =
901cb0ef41Sopenharmony_ci      UncheckedParameter<IntPtrT>(Descriptor::kExpectedValueLow);
911cb0ef41Sopenharmony_ci  auto expected_value_high =
921cb0ef41Sopenharmony_ci      UncheckedParameter<IntPtrT>(Descriptor::kExpectedValueHigh);
931cb0ef41Sopenharmony_ci  TNode<BigInt> expected_value =
941cb0ef41Sopenharmony_ci      BigIntFromInt32Pair(expected_value_low, expected_value_high);
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  auto timeout_low = UncheckedParameter<IntPtrT>(Descriptor::kTimeoutLow);
971cb0ef41Sopenharmony_ci  auto timeout_high = UncheckedParameter<IntPtrT>(Descriptor::kTimeoutHigh);
981cb0ef41Sopenharmony_ci  TNode<BigInt> timeout = BigIntFromInt32Pair(timeout_low, timeout_high);
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci  TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
1011cb0ef41Sopenharmony_ci  TNode<Context> context = LoadContextFromInstance(instance);
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci  TNode<Smi> result_smi =
1041cb0ef41Sopenharmony_ci      CAST(CallRuntime(Runtime::kWasmI64AtomicWait, context, instance,
1051cb0ef41Sopenharmony_ci                       address_number, expected_value, timeout));
1061cb0ef41Sopenharmony_ci  Return(Unsigned(SmiToInt32(result_smi)));
1071cb0ef41Sopenharmony_ci}
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ciTF_BUILTIN(JSToWasmLazyDeoptContinuation, WasmBuiltinsAssembler) {
1101cb0ef41Sopenharmony_ci  // Reset thread_in_wasm_flag.
1111cb0ef41Sopenharmony_ci  TNode<ExternalReference> thread_in_wasm_flag_address_address =
1121cb0ef41Sopenharmony_ci      ExternalConstant(
1131cb0ef41Sopenharmony_ci          ExternalReference::thread_in_wasm_flag_address_address(isolate()));
1141cb0ef41Sopenharmony_ci  auto thread_in_wasm_flag_address =
1151cb0ef41Sopenharmony_ci      Load<RawPtrT>(thread_in_wasm_flag_address_address);
1161cb0ef41Sopenharmony_ci  StoreNoWriteBarrier(MachineRepresentation::kWord32,
1171cb0ef41Sopenharmony_ci                      thread_in_wasm_flag_address, Int32Constant(0));
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  // Return the argument.
1201cb0ef41Sopenharmony_ci  auto value = Parameter<Object>(Descriptor::kArgument);
1211cb0ef41Sopenharmony_ci  Return(value);
1221cb0ef41Sopenharmony_ci}
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci}  // namespace internal
1251cb0ef41Sopenharmony_ci}  // namespace v8
126