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 #ifndef RISCV64_TO_X86_64_NDK_TRANSLATION_INTRINSICS_TEXT_ASSEMBLER_H_
18 #define RISCV64_TO_X86_64_NDK_TRANSLATION_INTRINSICS_TEXT_ASSEMBLER_H_
19 
20 #include <stdio.h>
21 
22 #include "berberis/intrinsics/all_to_x86_32_or_x86_64/text_assembler_x86_32_and_x86_64.h"
23 
24 namespace berberis {
25 
26 class TextAssembler : public x86_32_and_x86_64::TextAssembler<TextAssembler> {
27  public:
28   using BaseAssembler = x86_32_and_x86_64::TextAssembler<TextAssembler>;
29   using FinalAssembler = TextAssembler;
30 
TextAssembler(int indent,FILE * out)31   TextAssembler(int indent, FILE* out) : BaseAssembler(indent, out) {}
32 
33 // Instructions.
34 #include "gen_text_assembler_x86_64-inl.h"  // NOLINT generated file
35 
36   // Unhide Movq(Mem, XMMReg) and Movq(XMMReg, Mem) hidden by Movq(Reg, Imm) and many others.
37   using BaseAssembler::Movq;
38 
39   static constexpr char kArchName[] = "riscv64";
40   static constexpr char kArchGuard[] = "RISCV64_TO_X86_64";
41   static constexpr char kNamespaceName[] = "berberis";
42 
43  protected:
44   using RegisterDefaultBit = RegisterTemplate<kRsp, 'q'>;
45 
46  private:
47   TextAssembler() = delete;
48   TextAssembler(const TextAssembler&) = delete;
49   TextAssembler(TextAssembler&&) = delete;
50   void operator=(const TextAssembler&) = delete;
51   void operator=(TextAssembler&&) = delete;
52   using DerivedAssemblerType = TextAssembler;
53 
54   friend BaseAssembler;
55 };
56 
MakeGetSetFPEnvironment(FILE * out)57 void MakeGetSetFPEnvironment(FILE* out) {
58   fprintf(out,
59           R"STRING(
60 // On platforms that we care about (Bionic, GLibc, MUSL, even x86-64 MacOS) exceptions are
61 // taken directly from x86 status word or MXCSR.
62 //
63 // The only exception seems to be MSVC and it can be detected with this simple check.
64 #if (FE_INVALID == 0x01) && (FE_DIVBYZERO == 0x04) && (FE_OVERFLOW == 0x08) && \
65     (FE_UNDERFLOW == 0x10) && (FE_INEXACT == 0x20)
66 
67 inline std::tuple<uint64_t> FeGetExceptions() {
68   return reinterpret_cast<const char*>(&constants_pool::kBerberisMacroAssemblerConstants)
69       [%1$d + fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW | FE_INEXACT)];
70 }
71 
72 inline void FeSetExceptions(uint64_t exceptions) {
73   const fexcept_t x87_flag = reinterpret_cast<const char*>(
74       &constants_pool::kBerberisMacroAssemblerConstants)[%2$d + exceptions];
75   fesetexceptflag(&x87_flag, FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW | FE_INEXACT);
76 }
77 
78 inline void FeSetExceptionsImm(uint8_t exceptions) {
79   FeSetExceptions(exceptions);
80 }
81 
82 #else
83 
84 #error Unsupported libc.
85 
86 #endif
87 )STRING",
88           constants_pool::GetOffset(constants_pool::kX87ToRiscVExceptions),
89           constants_pool::GetOffset(constants_pool::kRiscVToX87Exceptions));
90 }
91 
MakeExtraGuestFunctions(FILE * out)92 void MakeExtraGuestFunctions(FILE* out) {
93   MakeGetSetFPEnvironment(out);
94 }
95 
96 }  // namespace berberis
97 
98 #endif  // RISCV64_TO_X86_64_NDK_TRANSLATION_INTRINSICS_TEXT_ASSEMBLER_H_
99