1 /*
2 * Copyright (c) 2021 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
18 #include <chrono>
19 #include <cmath>
20 #include <limits>
21 #include <string>
22 #include <string_view>
23 #include <type_traits>
24
25 #include "libpandabase/macros.h"
26 #include "libpandabase/utils/logger.h"
27 #include "libpandabase/utils/span.h"
28 #include "libpandabase/utils/time.h"
29 #include "runtime/include/exceptions.h"
30 #include "runtime/include/class_linker.h"
31 #include "runtime/include/coretypes/array.h"
32 #include "runtime/include/coretypes/string.h"
33 #include "runtime/include/panda_vm.h"
34 #include "runtime/include/runtime.h"
35 #include "runtime/include/thread.h"
36 #include "runtime/include/thread_status.h"
37 #include "runtime/interpreter/frame.h"
38 #include "utils/math_helpers.h"
39
40 namespace panda::intrinsics {
41
IsInfF64(double v)42 extern "C" uint8_t IsInfF64(double v)
43 {
44 return static_cast<uint8_t>(std::isinf(v));
45 }
46
IsInfF32(float v)47 uint8_t IsInfF32(float v)
48 {
49 return static_cast<uint8_t>(std::isinf(v));
50 }
51
AbsI32(int32_t v)52 int32_t AbsI32(int32_t v)
53 {
54 return std::abs(v);
55 }
56
AbsI64(int64_t v)57 int64_t AbsI64(int64_t v)
58 {
59 return std::abs(v);
60 }
61
AbsF32(float v)62 float AbsF32(float v)
63 {
64 return std::abs(v);
65 }
66
AbsF64(double v)67 double AbsF64(double v)
68 {
69 return std::abs(v);
70 }
71
SinF32(float v)72 float SinF32(float v)
73 {
74 return std::sin(v);
75 }
76
SinF64(double v)77 double SinF64(double v)
78 {
79 return std::sin(v);
80 }
81
CosF32(float v)82 float CosF32(float v)
83 {
84 return std::cos(v);
85 }
86
CosF64(double v)87 double CosF64(double v)
88 {
89 return std::cos(v);
90 }
91
PowF32(float base,float exp)92 float PowF32(float base, float exp)
93 {
94 return std::pow(base, exp);
95 }
96
PowF64(double base,double exp)97 double PowF64(double base, double exp)
98 {
99 return std::pow(base, exp);
100 }
101
SqrtF32(float v)102 float SqrtF32(float v)
103 {
104 return std::sqrt(v);
105 }
106
SqrtF64(double v)107 double SqrtF64(double v)
108 {
109 return std::sqrt(v);
110 }
111
MinI32(int32_t a,int32_t b)112 int32_t MinI32(int32_t a, int32_t b)
113 {
114 return std::min(a, b);
115 }
116
MinI64(int64_t a,int64_t b)117 int64_t MinI64(int64_t a, int64_t b)
118 {
119 return std::min(a, b);
120 }
121
MinF32(float a,float b)122 float MinF32(float a, float b)
123 {
124 return panda::helpers::math::min(a, b);
125 }
126
MinF64(double a,double b)127 double MinF64(double a, double b)
128 {
129 return panda::helpers::math::min(a, b);
130 }
131
MaxI32(int32_t a,int32_t b)132 int32_t MaxI32(int32_t a, int32_t b)
133 {
134 return std::max(a, b);
135 }
136
MaxI64(int64_t a,int64_t b)137 int64_t MaxI64(int64_t a, int64_t b)
138 {
139 return std::max(a, b);
140 }
141
MaxF32(float a,float b)142 float MaxF32(float a, float b)
143 {
144 return panda::helpers::math::max(a, b);
145 }
146
MaxF64(double a,double b)147 double MaxF64(double a, double b)
148 {
149 return panda::helpers::math::max(a, b);
150 }
151
152 template <bool is_err, class T>
Print(T v)153 void Print(T v)
154 {
155 if (is_err) {
156 std::cerr << v;
157 } else {
158 std::cout << v;
159 }
160 }
161
162 template <bool is_err, class T>
PrintW(T v)163 void PrintW(T v)
164 {
165 if (is_err) {
166 std::wcerr << v;
167 } else {
168 std::wcout << v;
169 }
170 }
171
172 template <bool is_err>
PrintStringInternal(coretypes::String * v)173 void PrintStringInternal(coretypes::String *v)
174 {
175 if (v->IsUtf16()) {
176 Span<const char16_t> sp(reinterpret_cast<const char16_t *>(v->GetDataUtf16()), v->GetLength());
177 for (wchar_t c : sp) {
178 PrintW<is_err>(c);
179 }
180 } else {
181 Span<const char> sp(reinterpret_cast<const char *>(v->GetDataMUtf8()), v->GetLength());
182 for (char c : sp) {
183 Print<is_err>(c);
184 }
185 }
186 }
187
PrintString(coretypes::String * v)188 void PrintString(coretypes::String *v)
189 {
190 PrintStringInternal<false>(v);
191 }
192
PrintF32(float v)193 void PrintF32(float v)
194 {
195 Print<false>(v);
196 }
197
PrintF64(double v)198 void PrintF64(double v)
199 {
200 Print<false>(v);
201 }
202
PrintI32(int32_t v)203 void PrintI32(int32_t v)
204 {
205 Print<false>(v);
206 }
207
PrintU32(uint32_t v)208 void PrintU32(uint32_t v)
209 {
210 Print<false>(v);
211 }
212
PrintI64(int64_t v)213 void PrintI64(int64_t v)
214 {
215 Print<false>(v);
216 }
217
PrintU64(uint64_t v)218 void PrintU64(uint64_t v)
219 {
220 Print<false>(v);
221 }
222
NanoTime()223 int64_t NanoTime()
224 {
225 return time::GetCurrentTimeInNanos();
226 }
227
Assert(uint8_t cond)228 void Assert(uint8_t cond)
229 {
230 if (cond == 0) {
231 Runtime::Abort();
232 }
233 }
234
UnknownIntrinsic()235 void UnknownIntrinsic()
236 {
237 std::cerr << "UnknownIntrinsic\n";
238 Runtime::Abort();
239 }
240
AssertPrint(uint8_t cond,coretypes::String * s)241 void AssertPrint(uint8_t cond, coretypes::String *s)
242 {
243 if (cond == 0) {
244 PrintStringInternal<true>(s);
245 Runtime::Abort();
246 }
247 }
248
ConvertStringToI32(coretypes::String * s)249 int32_t ConvertStringToI32(coretypes::String *s)
250 {
251 return static_cast<int32_t>(PandaStringToLL(ConvertToString(s)));
252 }
253
ConvertStringToU32(coretypes::String * s)254 uint32_t ConvertStringToU32(coretypes::String *s)
255 {
256 return static_cast<uint32_t>(PandaStringToULL(ConvertToString(s)));
257 }
258
ConvertStringToI64(coretypes::String * s)259 int64_t ConvertStringToI64(coretypes::String *s)
260 {
261 return static_cast<int64_t>(PandaStringToLL(ConvertToString(s)));
262 }
263
ConvertStringToU64(coretypes::String * s)264 uint64_t ConvertStringToU64(coretypes::String *s)
265 {
266 return static_cast<uint64_t>(PandaStringToULL(ConvertToString(s)));
267 }
268
ConvertStringToF32(coretypes::String * s)269 float ConvertStringToF32(coretypes::String *s)
270 {
271 return PandaStringToF(ConvertToString(s));
272 }
273
ConvertStringToF64(coretypes::String * s)274 double ConvertStringToF64(coretypes::String *s)
275 {
276 return PandaStringToD(ConvertToString(s));
277 }
278
279 // Need for java.lang.Runtime
280 // it is explicit function in java.lang.Runtime class
RuntimeExit(int32_t status)281 static void RuntimeExit(int32_t status)
282 {
283 Runtime::Halt(status);
284 }
285
SystemExit(int32_t status)286 void SystemExit(int32_t status)
287 {
288 RuntimeExit(status);
289 }
290
ObjectMonitorEnter(ObjectHeader * header)291 void ObjectMonitorEnter(ObjectHeader *header)
292 {
293 if (header == nullptr) {
294 panda::ThrowNullPointerException();
295 return;
296 }
297 auto res = Monitor::MonitorEnter(header);
298 // Expected results: OK, ILLEGAL
299 ASSERT(res != Monitor::State::INTERRUPTED);
300 if (UNLIKELY(res != Monitor::State::OK)) {
301 // This should never happen
302 LOG(FATAL, RUNTIME) << "MonitorEnter for " << std::hex << header << " returned Illegal state!";
303 }
304 }
305
ObjectMonitorExit(ObjectHeader * header)306 void ObjectMonitorExit(ObjectHeader *header)
307 {
308 if (header == nullptr) {
309 panda::ThrowNullPointerException();
310 return;
311 }
312 auto res = Monitor::MonitorExit(header);
313 // Expected results: OK, ILLEGAL
314 ASSERT(res != Monitor::State::INTERRUPTED);
315 if (res == Monitor::State::ILLEGAL) {
316 PandaStringStream ss;
317 ss << "MonitorExit for object " << std::hex << header << " returned Illegal state";
318 panda::ThrowIllegalMonitorStateException(ss.str());
319 }
320 }
321
ObjectWait(ObjectHeader * header)322 void ObjectWait(ObjectHeader *header)
323 {
324 Monitor::State state = Monitor::Wait(header, ThreadStatus::IS_WAITING, 0, 0);
325 LOG_IF(state == Monitor::State::ILLEGAL, FATAL, RUNTIME) << "Monitor::Wait() failed";
326 }
327
ObjectTimedWait(ObjectHeader * header,uint64_t timeout)328 void ObjectTimedWait(ObjectHeader *header, uint64_t timeout)
329 {
330 Monitor::State state = Monitor::Wait(header, ThreadStatus::IS_TIMED_WAITING, timeout, 0);
331 LOG_IF(state == Monitor::State::ILLEGAL, FATAL, RUNTIME) << "Monitor::Wait() failed";
332 }
333
ObjectTimedWaitNanos(ObjectHeader * header,uint64_t timeout,uint64_t nanos)334 void ObjectTimedWaitNanos(ObjectHeader *header, uint64_t timeout, uint64_t nanos)
335 {
336 Monitor::State state = Monitor::Wait(header, ThreadStatus::IS_TIMED_WAITING, timeout, nanos);
337 LOG_IF(state == Monitor::State::ILLEGAL, FATAL, RUNTIME) << "Monitor::Wait() failed";
338 }
339
ObjectNotify(ObjectHeader * header)340 void ObjectNotify(ObjectHeader *header)
341 {
342 Monitor::State state = Monitor::Notify(header);
343 LOG_IF(state != Monitor::State::OK, FATAL, RUNTIME) << "Monitor::Notify() failed";
344 }
345
ObjectNotifyAll(ObjectHeader * header)346 void ObjectNotifyAll(ObjectHeader *header)
347 {
348 Monitor::State state = Monitor::NotifyAll(header);
349 LOG_IF(state != Monitor::State::OK, FATAL, RUNTIME) << "Monitor::NotifyAll() failed";
350 }
351
352 } // namespace panda::intrinsics
353
354 #include <intrinsics_gen.h>
355