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