1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "ecmascript/compiler/baseline/baseline_stub_builder.h"
17#include "ecmascript/compiler/access_object_stub_builder.h"
18#include "ecmascript/global_env.h"
19#include "ecmascript/js_async_generator_object.h"
20#include "ecmascript/js_arguments.h"
21#include "ecmascript/js_function.h"
22#include "ecmascript/js_generator_object.h"
23#include "ecmascript/compiler/stub_builder-inl.h"
24
25namespace panda::ecmascript::kungfu {
26using namespace panda::ecmascript;
27
28GateRef BaselineStubBuilder::GetHomeObjectFromFunction(GateRef function)
29{
30    return Load(VariableType::JS_POINTER(), function, IntPtr(JSFunction::HOME_OBJECT_OFFSET));
31}
32
33GateRef BaselineStubBuilder::GetResumeModeFromGeneratorObject(GateRef obj)
34{
35    GateRef bitfieldOffset = IntPtr(JSGeneratorObject::BIT_FIELD_OFFSET);
36    GateRef bitfield = Load(VariableType::INT32(), obj, bitfieldOffset);
37    return Int32And(
38        Int32LSR(bitfield, Int32(JSGeneratorObject::ResumeModeBits::START_BIT)),
39        Int32((1LU << JSGeneratorObject::ResumeModeBits::SIZE) - 1));
40}
41
42GateRef BaselineStubBuilder::GetResumeModeFromAsyncGeneratorObject(GateRef obj)
43{
44    GateRef bitfieldOffset = IntPtr(JSAsyncGeneratorObject::BIT_FIELD_OFFSET);
45    GateRef bitfield = Load(VariableType::INT32(), obj, bitfieldOffset);
46    return Int32And(
47        Int32LSR(bitfield, Int32(JSAsyncGeneratorObject::ResumeModeBits::START_BIT)),
48        Int32((1LU << JSAsyncGeneratorObject::ResumeModeBits::SIZE) - 1));
49}
50
51GateRef BaselineStubBuilder::GetLastLeaveFrame(GateRef glue)
52{
53    bool isArch32 = GetEnvironment()->Is32Bit();
54    GateRef spOffset = IntPtr(JSThread::GlueData::GetLeaveFrameOffset(isArch32));
55    return Load(VariableType::NATIVE_POINTER(), glue, spOffset);
56}
57
58GateRef BaselineStubBuilder::GetProfileTypeInfoFromFunction(GateRef function)
59{
60    GateRef raw = Load(VariableType::JS_POINTER(), function, IntPtr(JSFunction::RAW_PROFILE_TYPE_INFO_OFFSET));
61    return Load(VariableType::JS_POINTER(), raw, IntPtr(ProfileTypeInfoCell::VALUE_OFFSET));
62}
63
64GateRef BaselineStubBuilder::GetModuleFromFunction(GateRef function)
65{
66    return Load(VariableType::JS_POINTER(), function, IntPtr(JSFunction::ECMA_MODULE_OFFSET));
67}
68
69}  // namespace panda::ecmascript::kungfu
70