/src/capstonenext/arch/RISCV/RISCVBaseInfo.c
Line | Count | Source |
1 | | /* Capstone Disassembly Engine, http://www.capstone-engine.org */ |
2 | | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2022, */ |
3 | | /* Rot127 <unisono@quyllur.org> 2022-2023 */ |
4 | | /* Automatically translated source file from LLVM. */ |
5 | | |
6 | | /* LLVM-commit: <commit> */ |
7 | | /* LLVM-tag: <tag> */ |
8 | | |
9 | | /* Only small edits allowed. */ |
10 | | /* For multiple similar edits, please create a Patch for the translator. */ |
11 | | |
12 | | /* Capstone's C++ file translator: */ |
13 | | /* https://github.com/capstone-engine/capstone/tree/next/suite/auto-sync */ |
14 | | |
15 | | //===-- RISCVBaseInfo.cpp - Top level definitions for RISC-V MC -----------===// |
16 | | // |
17 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
18 | | // See https://llvm.org/LICENSE.txt for license information. |
19 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
20 | | // |
21 | | //===----------------------------------------------------------------------===// |
22 | | // |
23 | | // This file contains small standalone enum definitions for the RISC-V target |
24 | | // useful for the compiler back-end and the MC libraries. |
25 | | // |
26 | | //===----------------------------------------------------------------------===// |
27 | | |
28 | | #include <stdio.h> |
29 | | #include <string.h> |
30 | | #include <stdlib.h> |
31 | | #include <capstone/platform.h> |
32 | | |
33 | | #include "RISCVBaseInfo.h" |
34 | | |
35 | | #define CONCAT(a, b) CONCAT_(a, b) |
36 | | #define CONCAT_(a, b) a##_##b |
37 | | |
38 | | typedef struct { |
39 | | unsigned value; |
40 | | bool isFractional; |
41 | | } VLMULDecodeResult; |
42 | | VLMULDecodeResult decodeVLMUL(RISCVII_VLMUL VLMUL) |
43 | 368 | { |
44 | 368 | switch (VLMUL) { |
45 | 0 | default: |
46 | 0 | CS_ASSERT(0 && "Unexpected LMUL value!"); |
47 | 8 | case RISCVII_LMUL_1: |
48 | 33 | case RISCVII_LMUL_2: |
49 | 112 | case RISCVII_LMUL_4: |
50 | 113 | case RISCVII_LMUL_8: { |
51 | 113 | VLMULDecodeResult result = { .value = 1 << (unsigned)(VLMUL), |
52 | 113 | .isFractional = false }; |
53 | 113 | return result; |
54 | 112 | } |
55 | 10 | case RISCVII_LMUL_F2: |
56 | 130 | case RISCVII_LMUL_F4: |
57 | 255 | case RISCVII_LMUL_F8: { |
58 | 255 | VLMULDecodeResult result = { .value = 1 << (8 - |
59 | 255 | (unsigned)(VLMUL)), |
60 | 255 | .isFractional = true }; |
61 | 255 | return result; |
62 | 130 | } |
63 | 368 | } |
64 | 368 | } |
65 | | |
66 | | void printVType(unsigned VType, SStream *OS) |
67 | 368 | { |
68 | 368 | unsigned Sew = RISCVVType_getSEW(VType); |
69 | 368 | SStream_concat(OS, "%s", "e"); |
70 | 368 | printUInt64(OS, Sew); |
71 | | |
72 | 368 | unsigned LMul; |
73 | 368 | bool Fractional; |
74 | 368 | VLMULDecodeResult result = decodeVLMUL(RISCVVType_getVLMUL(VType)); |
75 | 368 | LMul = result.value; |
76 | 368 | Fractional = result.isFractional; |
77 | | |
78 | 368 | if (Fractional) |
79 | 255 | SStream_concat0(OS, ", mf"); |
80 | 113 | else |
81 | 113 | SStream_concat0(OS, ", m"); |
82 | 368 | printUInt64(OS, LMul); |
83 | | |
84 | 368 | if (RISCVVType_isTailAgnostic(VType)) |
85 | 223 | SStream_concat0(OS, ", ta"); |
86 | 145 | else |
87 | 145 | SStream_concat0(OS, ", tu"); |
88 | | |
89 | 368 | if (RISCVVType_isMaskAgnostic(VType)) |
90 | 126 | SStream_concat0(OS, ", ma"); |
91 | 242 | else |
92 | 242 | SStream_concat0(OS, ", mu"); |
93 | 368 | } |
94 | | |
95 | | typedef struct { |
96 | | uint8_t first; |
97 | | uint8_t second; |
98 | | } LoadFP32ImmArrElement; |
99 | | |
100 | | // Lookup table for fli.s for entries 2-31. |
101 | | static const LoadFP32ImmArrElement LoadFP32ImmArr[] = { |
102 | | { 0x6f, 0x00 }, { 0x70, 0x00 }, { 0x77, 0x00 }, { 0x78, 0x00 }, |
103 | | { 0x7b, 0x00 }, { 0x7c, 0x00 }, { 0x7d, 0x00 }, { 0x7d, 0x01 }, |
104 | | { 0x7d, 0x02 }, { 0x7d, 0x03 }, { 0x7e, 0x00 }, { 0x7e, 0x01 }, |
105 | | { 0x7e, 0x02 }, { 0x7e, 0x03 }, { 0x7f, 0x00 }, { 0x7f, 0x01 }, |
106 | | { 0x7f, 0x02 }, { 0x7f, 0x03 }, { 0x80, 0x00 }, { 0x80, 0x01 }, |
107 | | { 0x80, 0x02 }, { 0x81, 0x00 }, { 0x82, 0x00 }, { 0x83, 0x00 }, |
108 | | { 0x86, 0x00 }, { 0x87, 0x00 }, { 0x8e, 0x00 }, { 0x8f, 0x00 }, |
109 | | { 0xff, 0x00 }, { 0xff, 0x02 }, |
110 | | }; |
111 | | |
112 | | float getFPImm(unsigned Imm) |
113 | 148 | { |
114 | 148 | CS_ASSERT(Imm != 1 && Imm != 30 && Imm != 31 && |
115 | 148 | "Unsupported immediate"); |
116 | 148 | CS_ASSERT((Imm == 0 || (Imm >= 2 && Imm < 30)) && |
117 | 148 | "Unsupported immediate"); |
118 | | // Entry 0 is -1.0, the only negative value. Entry 16 is 1.0. |
119 | 148 | uint32_t Sign = 0; |
120 | 148 | if (Imm == 0) { |
121 | 66 | Sign = 0x01; |
122 | 66 | Imm = 16; |
123 | 66 | } |
124 | | |
125 | 148 | uint32_t Exp = LoadFP32ImmArr[Imm - 2].first; |
126 | 148 | uint32_t Mantissa = LoadFP32ImmArr[Imm - 2].second; |
127 | | |
128 | 148 | uint32_t I = Sign << 31 | Exp << 23 | Mantissa << 21; |
129 | 148 | float result; |
130 | 148 | memcpy(&result, &I, sizeof(float)); |
131 | 148 | return result; |
132 | 148 | } |
133 | | |
134 | | void RISCVZC_printSpimm(int64_t Spimm, SStream *OS) |
135 | 0 | { |
136 | 0 | printInt32(OS, Spimm); |
137 | 0 | } |
138 | | |
139 | | // namespace llvm |