1 /*
2  * Copyright (c) 2023 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 "intrinsics.h"
17 #include "mir_module.h"
18 #include "mir_type.h"
19 #include "mir_builder.h"
20 
21 namespace maple {
22 MIRType *IntrinDesc::jsValueType = nullptr;
23 MIRModule *IntrinDesc::mirModule = nullptr;
24 IntrinDesc IntrinDesc::intrinTable[INTRN_LAST + 1] = {
25 #define DEF_MIR_INTRINSIC(X, NAME, RETURN_TYPE, ...) \
26     {(NAME), {(RETURN_TYPE), ##__VA_ARGS__}},
27 #include "intrinsics.def"
28 #undef DEF_MIR_INTRINSIC
29 };
30 
InitMIRModule(MIRModule *mod)31 void IntrinDesc::InitMIRModule(MIRModule *mod)
32 {
33     mirModule = mod;
34 }
35 
GetTypeFromArgTy(IntrinArgType argType) const36 MIRType *IntrinDesc::GetTypeFromArgTy(IntrinArgType argType) const
37 {
38     switch (argType) {
39         case kArgTyVoid:
40             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_void));
41         case kArgTyI8:
42             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_i8));
43         case kArgTyI16:
44             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_i16));
45         case kArgTyI32:
46             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_i32));
47         case kArgTyI64:
48             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_i64));
49         case kArgTyU8:
50             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_u8));
51         case kArgTyU16:
52             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_u16));
53         case kArgTyU32:
54             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_u32));
55         case kArgTyU64:
56             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_u64));
57         case kArgTyU1:
58             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_u1));
59         case kArgTyPtr:
60             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_ptr));
61         case kArgTyRef:
62             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_ref));
63         case kArgTyA64:
64             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_a64));
65         case kArgTyF32:
66             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_f32));
67         case kArgTyF64:
68             return GlobalTables::GetTypeTable().GetTypeFromTyIdx(static_cast<TyIdx>(PTY_f64));
69         default:
70             return nullptr;
71     }
72 }
73 
GetArgType(uint32 index) const74 MIRType *IntrinDesc::GetArgType(uint32 index) const
75 {
76     // 0 is the arg of return type
77     CHECK_FATAL(index < kMaxArgsNum, "index out of range");
78     return GetTypeFromArgTy(argTypes[index + 1]);
79 }
80 
81 MIRType *IntrinDesc::GetReturnType() const
82 {
83     return GetTypeFromArgTy(argTypes[0]);
84 }
85 }  // namespace maple
86