• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef PANDA_PLUGINS_ETS_RUNTIME_TOOLING_HELPERS_H
17 #define PANDA_PLUGINS_ETS_RUNTIME_TOOLING_HELPERS_H
18 
19 #include <string_view>
20 
21 #include "include/tooling/pt_thread.h"
22 #include "include/tooling/vreg_value.h"
23 #include "libpandabase/utils/bit_utils.h"
24 #include "type.h"
25 #include "types/ets_primitives.h"
26 
27 namespace ark::ets::tooling {
28 
29 template <typename T>
30 struct EtsTypeName {
31 };
32 
33 // CC-OFFNXT(G.PRE.02) code generation
34 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
35 #define ETS_TYPE_NAME(TYPE_NAME)                             \
36     template <>                                              \
37     struct EtsTypeName<TYPE_NAME> {                          \
38         static constexpr std::string_view NAME = #TYPE_NAME; \
39     }
40 
41 ETS_TYPE_NAME(EtsBoolean);
42 ETS_TYPE_NAME(EtsByte);
43 ETS_TYPE_NAME(EtsShort);
44 ETS_TYPE_NAME(EtsChar);
45 ETS_TYPE_NAME(EtsInt);
46 ETS_TYPE_NAME(EtsFloat);
47 ETS_TYPE_NAME(EtsDouble);
48 ETS_TYPE_NAME(EtsLong);
49 
50 #undef ETS_TYPE_NAME
51 
52 template <>
53 struct EtsTypeName<ObjectHeader *> {
54     static constexpr std::string_view NAME = "EtsObject";
55 };
56 
57 template <typename T, typename std::enable_if_t<std::is_same_v<EtsBoolean, T> || std::is_same_v<EtsByte, T> ||
58                                                     std::is_same_v<EtsShort, T> || std::is_same_v<EtsChar, T> ||
59                                                     std::is_same_v<EtsInt, T> || std::is_same_v<EtsLong, T>,
60                                                 int> = 0>
61 constexpr T VRegValueToEtsValue(ark::tooling::VRegValue value)
62 {
63     return static_cast<T>(value.GetValue());
64 }
65 
66 template <typename T, typename std::enable_if_t<std::is_same_v<EtsFloat, T>, int> = 0>
67 constexpr EtsFloat VRegValueToEtsValue(ark::tooling::VRegValue value)
68 {
69     size_t bitsCount = sizeof(EtsFloat) * 8U;
70     return bit_cast<EtsFloat>(
71         static_cast<int32_t>(ExtractBits(static_cast<uint64_t>(value.GetValue()), 0U, bitsCount)));
72 }
73 
74 template <typename T, typename std::enable_if_t<std::is_same_v<EtsDouble, T>, int> = 0>
75 constexpr EtsDouble VRegValueToEtsValue(ark::tooling::VRegValue value)
76 {
77     return bit_cast<EtsDouble>(value.GetValue());
78 }
79 
80 template <typename T, typename std::enable_if_t<std::is_same_v<ObjectHeader *, T>, int> = 0>
81 constexpr ObjectHeader *VRegValueToEtsValue(ark::tooling::VRegValue value)
82 {
83     return reinterpret_cast<ObjectHeader *>(value.GetValue());
84 }
85 
86 template <typename T, typename std::enable_if_t<std::is_same_v<EtsBoolean, T> || std::is_same_v<EtsByte, T> ||
87                                                     std::is_same_v<EtsShort, T> || std::is_same_v<EtsChar, T> ||
88                                                     std::is_same_v<EtsInt, T> || std::is_same_v<EtsLong, T>,
89                                                 int> = 0>
90 constexpr ark::tooling::VRegValue EtsValueToVRegValue(T value)
91 {
92     return ark::tooling::VRegValue(value);
93 }
94 
95 template <typename T, typename std::enable_if_t<std::is_same_v<EtsFloat, T>, int> = 0>
96 constexpr ark::tooling::VRegValue EtsValueToVRegValue(EtsFloat value)
97 {
98     return ark::tooling::VRegValue(bit_cast<int32_t>(value));
99 }
100 
101 template <typename T, typename std::enable_if_t<std::is_same_v<EtsDouble, T>, int> = 0>
102 constexpr ark::tooling::VRegValue EtsValueToVRegValue(EtsDouble value)
103 {
104     return ark::tooling::VRegValue(bit_cast<int64_t>(value));
105 }
106 
107 template <typename T, typename std::enable_if_t<std::is_same_v<ObjectHeader *, T>, int> = 0>
108 constexpr ark::tooling::VRegValue EtsValueToVRegValue(ObjectHeader *value)
109 {
110     return ark::tooling::VRegValue(reinterpret_cast<int64_t>(value));
111 }
112 
113 inline ark::tooling::PtThread CoroutineToPtThread(EtsCoroutine *coroutine)
114 {
115     return ark::tooling::PtThread(coroutine);
116 }
117 
118 }  // namespace ark::ets::tooling
119 
120 #endif  // PANDA_PLUGINS_ETS_RUNTIME_TOOLING_HELPERS_H
121