1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "gtest/gtest.h"
18 
19 #include <cfenv>  // FE rounding modes
20 #include <cstdint>
21 #include <initializer_list>
22 #include <tuple>
23 
24 #include "berberis/assembler/machine_code.h"
25 #include "berberis/guest_state/guest_addr.h"
26 #include "berberis/guest_state/guest_state.h"
27 #include "berberis/heavy_optimizer/riscv64/heavy_optimize_region.h"
28 #include "berberis/runtime_primitives/memory_region_reservation.h"
29 #include "berberis/test_utils/scoped_exec_region.h"
30 #include "berberis/test_utils/testing_run_generated_code.h"
31 
32 #include "berberis/intrinsics/intrinsics_float.h"
33 
34 namespace berberis {
35 
36 namespace {
37 
38 template <uint8_t kInsnSize = 4>
RunOneInstruction(ThreadState * state,GuestAddr stop_pc)39 bool RunOneInstruction(ThreadState* state, GuestAddr stop_pc) {
40   MachineCode machine_code;
41   auto [new_addr, success, number_of_instructions] =
42       HeavyOptimizeRegion(state->cpu.insn_addr,
43                           &machine_code,
44                           HeavyOptimizeParams{
45                               .max_number_of_instructions = 1,
46                           });
47   if (!success) {
48     return false;
49   }
50 
51   if (number_of_instructions != 1) {
52     return false;
53   }
54 
55   ScopedExecRegion exec(&machine_code);
56 
57   TestingRunGeneratedCode(state, exec.get(), stop_pc);
58   return true;
59 }
60 
61 #define TESTSUITE Riscv64HeavyOptimizerInsnTest
62 #define TESTING_HEAVY_OPTIMIZER
63 
64 #include "berberis/test_utils/insn_tests_riscv64-inl.h"
65 
66 #undef TESTING_HEAVY_OPTIMIZER
67 #undef TESTSUITE
68 
69 }  // namespace
70 
71 }  // namespace berberis
72