1 /*
2 * Copyright (c) 2024-2025 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 #ifndef ABCKIT_INST_BUILDER_INL_H
17 #define ABCKIT_INST_BUILDER_INL_H
18
19 #include "abckit_inst_builder.h"
20 #include <inst_builder_abckit_intrinsics.inc>
21
22 namespace ark::compiler {
23
24 template <bool IS_ACC_WRITE>
AbcKitBuildLoadObject(const BytecodeInstruction * bcInst,DataType::Type type)25 void AbcKitInstBuilder::AbcKitBuildLoadObject(const BytecodeInstruction *bcInst, DataType::Type type)
26 {
27 BuildAbcKitLoadObjectIntrinsic<IS_ACC_WRITE>(bcInst, type);
28 }
29
30 template <bool IS_ACC_READ>
AbcKitBuildStoreObject(const BytecodeInstruction * bcInst,DataType::Type type)31 void AbcKitInstBuilder::AbcKitBuildStoreObject(const BytecodeInstruction *bcInst, DataType::Type type)
32 {
33 BuildAbcKitStoreObjectIntrinsic<IS_ACC_READ>(bcInst, type);
34 }
35
BuildIsNullValue(const BytecodeInstruction * bcInst)36 void AbcKitInstBuilder::BuildIsNullValue(const BytecodeInstruction *bcInst)
37 {
38 BuildDefaultAbcKitIntrinsic(bcInst, ark::compiler::RuntimeInterface::IntrinsicId::INTRINSIC_ABCKIT_IS_NULL_VALUE);
39 }
40
BuildLoadStatic(const BytecodeInstruction * bcInst,DataType::Type type)41 void AbcKitInstBuilder::BuildLoadStatic(const BytecodeInstruction *bcInst, DataType::Type type)
42 {
43 BuildAbcKitLoadStaticIntrinsic(bcInst, type);
44 }
45
BuildStoreStatic(const BytecodeInstruction * bcInst,DataType::Type type)46 void AbcKitInstBuilder::BuildStoreStatic(const BytecodeInstruction *bcInst, DataType::Type type)
47 {
48 BuildAbcKitStoreStaticIntrinsic(bcInst, type);
49 }
50
BuildLoadArray(const BytecodeInstruction * bcInst,DataType::Type type)51 void AbcKitInstBuilder::BuildLoadArray(const BytecodeInstruction *bcInst, DataType::Type type)
52 {
53 BuildAbcKitLoadArrayIntrinsic(bcInst, type);
54 }
55
BuildLoadConstArray(const BytecodeInstruction * bcInst)56 void AbcKitInstBuilder::BuildLoadConstArray(const BytecodeInstruction *bcInst)
57 {
58 BuildDefaultAbcKitIntrinsic(bcInst, RuntimeInterface::IntrinsicId::INTRINSIC_ABCKIT_LOAD_CONST_ARRAY);
59 }
60
BuildStoreArray(const BytecodeInstruction * bcInst,DataType::Type type)61 void AbcKitInstBuilder::BuildStoreArray(const BytecodeInstruction *bcInst, DataType::Type type)
62 {
63 BuildAbcKitStoreArrayIntrinsic(bcInst, type);
64 }
65
BuildNewArray(const BytecodeInstruction * bcInst)66 void AbcKitInstBuilder::BuildNewArray(const BytecodeInstruction *bcInst)
67 {
68 BuildDefaultAbcKitIntrinsic(bcInst, ark::compiler::RuntimeInterface::IntrinsicId::INTRINSIC_ABCKIT_NEW_ARRAY);
69 }
70
BuildNewObject(const BytecodeInstruction * bcInst)71 void AbcKitInstBuilder::BuildNewObject(const BytecodeInstruction *bcInst)
72 {
73 BuildDefaultAbcKitIntrinsic(bcInst, ark::compiler::RuntimeInterface::IntrinsicId::INTRINSIC_ABCKIT_NEW_OBJECT);
74 }
75
BuildInitObject(const BytecodeInstruction * bcInst,bool isRange)76 void AbcKitInstBuilder::BuildInitObject(const BytecodeInstruction *bcInst, bool isRange)
77 {
78 BuildAbcKitInitObjectIntrinsic(bcInst, isRange);
79 }
80
BuildCheckCast(const BytecodeInstruction * bcInst)81 void AbcKitInstBuilder::BuildCheckCast(const BytecodeInstruction *bcInst)
82 {
83 BuildDefaultAbcKitIntrinsic(bcInst, ark::compiler::RuntimeInterface::IntrinsicId::INTRINSIC_ABCKIT_CHECK_CAST);
84 }
85
BuildIsInstance(const BytecodeInstruction * bcInst)86 void AbcKitInstBuilder::BuildIsInstance(const BytecodeInstruction *bcInst)
87 {
88 BuildDefaultAbcKitIntrinsic(bcInst, ark::compiler::RuntimeInterface::IntrinsicId::INTRINSIC_ABCKIT_IS_INSTANCE);
89 }
90
BuildThrow(const BytecodeInstruction * bcInst)91 void AbcKitInstBuilder::BuildThrow(const BytecodeInstruction *bcInst)
92 {
93 BuildDefaultAbcKitIntrinsic(bcInst, ark::compiler::RuntimeInterface::IntrinsicId::INTRINSIC_ABCKIT_THROW);
94 }
95
96 // NOLINTNEXTLINE(misc-definitions-in-headers)
97 template <Opcode OPCODE>
AbcKitBuildLoadFromPool(const BytecodeInstruction * bcInst)98 void AbcKitInstBuilder::AbcKitBuildLoadFromPool(const BytecodeInstruction *bcInst)
99 {
100 auto method = GetGraph()->GetMethod();
101 uint32_t typeId;
102 Inst *inst;
103 // NOLINTNEXTLINE(readability-magic-numbers,readability-braces-around-statements)
104 if constexpr (OPCODE == Opcode::LoadType) {
105 auto typeIndex = bcInst->GetId(0).AsIndex();
106 typeId = GetRuntime()->ResolveTypeIndex(method, typeIndex);
107 if (GetRuntime()->ResolveType(method, typeId) == nullptr) {
108 inst = GetGraph()->CreateInstUnresolvedLoadType(DataType::REFERENCE, GetPc(bcInst->GetAddress()));
109 if (!GetGraph()->IsAotMode() && !GetGraph()->IsBytecodeOptimizer()) {
110 GetRuntime()->GetUnresolvedTypes()->AddTableSlot(method, typeId,
111 UnresolvedTypesInterface::SlotKind::MANAGED_CLASS);
112 }
113 } else {
114 inst = GetGraph()->CreateInstLoadType(DataType::REFERENCE, GetPc(bcInst->GetAddress()));
115 }
116 // NOLINTNEXTLINE(readability-misleading-indentation)
117 } else {
118 // NOLINTNEXTLINE(readability-magic-numbers)
119 static_assert(OPCODE == Opcode::LoadString);
120 BuildDefaultAbcKitIntrinsic(bcInst, ark::compiler::RuntimeInterface::IntrinsicId::INTRINSIC_ABCKIT_LOAD_STRING);
121 return;
122 }
123 if (!GetGraph()->IsDynamicMethod() || GetGraph()->IsBytecodeOptimizer()) {
124 // Create SaveState instruction
125 auto saveState = CreateSaveState(Opcode::SaveState, GetPc(bcInst->GetAddress()));
126 inst->SetInput(0, saveState);
127 static_cast<LoadFromPool *>(inst)->SetTypeId(typeId);
128 static_cast<LoadFromPool *>(inst)->SetMethod(method);
129 AddInstruction(saveState);
130 } else {
131 inst->SetInput(0, GetEnvDefinition(CONST_POOL_IDX));
132 inst->CastToLoadFromConstantPool()->SetTypeId(typeId);
133 inst->CastToLoadFromConstantPool()->SetMethod(method);
134 }
135
136 AddInstruction(inst);
137 UpdateDefinitionAcc(inst);
138 // NOLINTNEXTLINE(readability-magic-numbers,readability-braces-around-statements,bugprone-suspicious-semicolon)
139 if constexpr (OPCODE == Opcode::LoadString) {
140 if (GetGraph()->IsDynamicMethod() && GetGraph()->IsBytecodeOptimizer()) {
141 BuildCastToAnyString(bcInst);
142 }
143 }
144 }
145
146 } // namespace ark::compiler
147
148 #endif // ABCKIT_INST_BUILDER_INL_H
149