Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
19895d4
Refactoring RISCV: INC generatione done, RISCV added to arch_config.j…
moste00 Jul 16, 2025
ef5ee9b
the C++ translator runs successfully and first output under arch/RISC…
moste00 Jul 18, 2025
bcb4be3
fixed Disassembler.c, in the process of fixing InstPrinter
moste00 Jul 19, 2025
50b9d9a
more compile errors fixed, RISCVInstPrinter error-free
moste00 Jul 26, 2025
a47a67e
plugins compiles and runs, several test failures were fixed, current …
moste00 Aug 1, 2025
a601b36
compressed instructions compression and uncompression logic was gener…
moste00 Aug 23, 2025
df700f0
massive fix handling various issues, test failures down to 35
moste00 Sep 5, 2025
f422714
test failures down to 2, related to inline option arch directives
moste00 Sep 5, 2025
9a4a8e2
added options to `cstool`, and started working on details test, 39/10…
moste00 Sep 16, 2025
046308f
more successful details test, added CSR operand type and changed csto…
moste00 Sep 24, 2025
440825e
more progress
moste00 Oct 10, 2025
4ce5e34
failures decreased to 2
moste00 Oct 15, 2025
250f91d
milestone: test failures 0
moste00 Oct 15, 2025
331ff17
add additional files generated from ASUpdate and update CMakeLists
moste00 Oct 17, 2025
f616aea
fix most issues tests except the one about c_srli allowing illegal sh…
moste00 Oct 19, 2025
082a238
fixed c_srli bug by updating the generated files from a fixed LLVM ve…
moste00 Oct 22, 2025
cdff665
made branch addresses absolute
moste00 Nov 10, 2025
a6b2604
fix DIET build by #ifdef-ing code that uses struct fields not availab…
moste00 Nov 10, 2025
378a2b3
fix Python build failure
moste00 Nov 14, 2025
9d2ae24
fix formatting
moste00 Nov 16, 2025
80dedbe
fixed formatting 2
moste00 Nov 16, 2025
c00ceb1
Remove the call group from JAL and JALR instructions because they're …
moste00 Nov 17, 2025
528ed62
final formatting issue
moste00 Nov 17, 2025
6b3d2f0
add the option for real details even for aliases
moste00 Nov 29, 2025
d30985b
various fixes and adding double imms
moste00 Dec 24, 2025
30e0532
fix bad python module FP comparison code and formatting
moste00 Dec 25, 2025
47861b2
another formatting fix
moste00 Dec 25, 2025
5c59a78
fix NAN issue on ARM
moste00 Dec 25, 2025
4db5b98
fix typo in RISCV_getFeatureBits in enum names, and fix the tests tha…
moste00 Dec 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
21 changes: 17 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -599,25 +599,38 @@ endif()
if(CAPSTONE_RISCV_SUPPORT)
add_definitions(-DCAPSTONE_HAS_RISCV)
set(SOURCES_RISCV
arch/RISCV/RISCVBaseInfo.c
arch/RISCV/RISCVDisassembler.c
arch/RISCV/RISCVDisassemblerExtension.c
arch/RISCV/RISCVInstPrinter.c
arch/RISCV/RISCVMapping.c
arch/RISCV/RISCVModule.c
)
set(HEADERS_RISCV
arch/RISCV/RISCVBaseInfo.h
arch/RISCV/RISCVDisassembler.h
arch/RISCV/RISCVDisassemblerExtension.h
arch/RISCV/RISCVInstPrinter.h
arch/RISCV/RISCVMapping.h
arch/RISCV/RISCVModule.h
arch/RISCV/RISCVLinkage.h
arch/RISCV/RISCVGenAsmWriter.inc
arch/RISCV/RISCVGenCSAliasMnemMap.inc
arch/RISCV/RISCVGenCSFeatureName.inc
arch/RISCV/RISCVGenCSMappingInsn.inc
arch/RISCV/RISCVGenCSMappingInsnName.inc
arch/RISCV/RISCVGenCSMappingInsnOp.inc
arch/RISCV/RISCVGenCSOpGroup.inc
arch/RISCV/RISCVGenCSSystemOperandsEnum.inc
arch/RISCV/RISCVGenDisassemblerTables.inc
arch/RISCV/RISCVGenInsnNameMaps.inc
arch/RISCV/RISCVGenInstrInfo.inc
arch/RISCV/RISCVGenRegisterInfo.inc
arch/RISCV/RISCVGenSubtargetInfo.inc
arch/RISCV/RISCVMappingInsn.inc
arch/RISCV/RISCVMappingInsnOp.inc
arch/RISCV/RISCVGenSystemOperands.inc
arch/RISCV/RISCVGenCompressedInstructionsInfo.inc
arch/RISCV/RISCVGenCSAliasEnum.inc
arch/RISCV/RISCVGenCSFeatureEnum.inc
arch/RISCV/RISCVGenCSInsnEnum.inc
arch/RISCV/RISCVGenCSRegEnum.inc
)
endif()

Expand Down
5 changes: 5 additions & 0 deletions MCInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern bool Mips_getFeatureBits(unsigned int mode, unsigned int feature);
extern bool AArch64_getFeatureBits(unsigned int mode, unsigned int feature);
extern bool TriCore_getFeatureBits(unsigned int mode, unsigned int feature);
extern bool Sparc_getFeatureBits(unsigned int mode, unsigned int feature);
extern bool RISCV_getFeatureBits(unsigned int mode, unsigned int feature);

static bool testFeatureBits(const MCInst *MI, uint32_t Value)
{
Expand Down Expand Up @@ -42,6 +43,10 @@ static bool testFeatureBits(const MCInst *MI, uint32_t Value)
#ifdef CAPSTONE_HAS_SPARC
case CS_ARCH_SPARC:
return Sparc_getFeatureBits(MI->csh->mode, Value);
#endif
#ifdef CAPSTONE_HAS_RISCV
case CS_ARCH_RISCV:
return RISCV_getFeatureBits(MI->csh->mode, Value);
#endif
}
}
Expand Down
2 changes: 1 addition & 1 deletion Mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ unsigned short insn_find(const insn_map *m, unsigned int max, unsigned int id,
unsigned int find_cs_id(unsigned MC_Opcode, const insn_map *imap,
unsigned imap_size);

#define MAX_NO_DATA_TYPES 16
#define MAX_NO_DATA_TYPES 32

///< A LLVM<->CS Mapping entry of an MCOperand.
typedef struct {
Expand Down
11 changes: 11 additions & 0 deletions MathExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ static inline bool isIntN(unsigned N, int64_t x)
(-(INT64_C(1) << (N - 1)) <= x && x < (INT64_C(1) << (N - 1)));
}

/// isShiftedIntN - Checks if a signed integer is an N bit number shifted left by S.
static inline bool isShiftedIntN(unsigned N, unsigned S, int64_t x)
{
return isIntN(N + S, x) && (x % (UINT64_C(1) << S) == 0);
}

static inline bool isShiftedUIntN(unsigned N, unsigned S, uint64_t x)
{
return isUIntN(N + S, x) && (x % (UINT64_C(1) << S) == 0);
}

/// isMask_32 - This function returns true if the argument is a sequence of ones
/// starting at the least significant bit with the remainder zero (32 bit
/// version). Ex. isMask_32(0x0000FFFFU) == true.
Expand Down
7 changes: 7 additions & 0 deletions SStream.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,13 @@ void printFloat(SStream *ss, float val)
SStream_concat(ss, "%e", val);
}

void printfFloat(SStream *ss, const char *fmt, float val)
{
assert(ss);
SSTREAM_RETURN_IF_CLOSED(ss);
SStream_concat(ss, fmt, val);
}

void printFloatBang(SStream *ss, float val)
{
assert(ss);
Expand Down
2 changes: 2 additions & 0 deletions SStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ void printInt32BangDec(SStream *O, int32_t val);

void printFloat(SStream *O, float val);

void printfFloat(SStream *ss, const char *fmt, float val);

void printFloatBang(SStream *O, float val);

void printExpr(SStream *O, uint64_t val);
Expand Down
140 changes: 140 additions & 0 deletions arch/RISCV/RISCVBaseInfo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/* Capstone Disassembly Engine, http://www.capstone-engine.org */
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2022, */
/* Rot127 <unisono@quyllur.org> 2022-2023 */
/* Automatically translated source file from LLVM. */

/* LLVM-commit: <commit> */
/* LLVM-tag: <tag> */

/* Only small edits allowed. */
/* For multiple similar edits, please create a Patch for the translator. */

/* Capstone's C++ file translator: */
/* https://github.com/capstone-engine/capstone/tree/next/suite/auto-sync */

//===-- RISCVBaseInfo.cpp - Top level definitions for RISC-V MC -----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file contains small standalone enum definitions for the RISC-V target
// useful for the compiler back-end and the MC libraries.
//
//===----------------------------------------------------------------------===//

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <capstone/platform.h>

#include "RISCVBaseInfo.h"

#define CONCAT(a, b) CONCAT_(a, b)
#define CONCAT_(a, b) a##_##b

typedef struct {
unsigned value;
bool isFractional;
} VLMULDecodeResult;
VLMULDecodeResult decodeVLMUL(RISCVII_VLMUL VLMUL)
{
switch (VLMUL) {
default:
CS_ASSERT(0 && "Unexpected LMUL value!");
case RISCVII_LMUL_1:
case RISCVII_LMUL_2:
case RISCVII_LMUL_4:
case RISCVII_LMUL_8: {
VLMULDecodeResult result = { .value = 1 << (unsigned)(VLMUL),
.isFractional = false };
return result;
}
case RISCVII_LMUL_F2:
case RISCVII_LMUL_F4:
case RISCVII_LMUL_F8: {
VLMULDecodeResult result = { .value = 1 << (8 -
(unsigned)(VLMUL)),
.isFractional = true };
return result;
}
}
}

void printVType(unsigned VType, SStream *OS)
{
unsigned Sew = RISCVVType_getSEW(VType);
SStream_concat(OS, "%s", "e");
printUInt64(OS, Sew);

unsigned LMul;
bool Fractional;
VLMULDecodeResult result = decodeVLMUL(RISCVVType_getVLMUL(VType));
LMul = result.value;
Fractional = result.isFractional;

if (Fractional)
SStream_concat0(OS, ", mf");
else
SStream_concat0(OS, ", m");
printUInt64(OS, LMul);

if (RISCVVType_isTailAgnostic(VType))
SStream_concat0(OS, ", ta");
else
SStream_concat0(OS, ", tu");

if (RISCVVType_isMaskAgnostic(VType))
SStream_concat0(OS, ", ma");
else
SStream_concat0(OS, ", mu");
}

typedef struct {
uint8_t first;
uint8_t second;
} LoadFP32ImmArrElement;

// Lookup table for fli.s for entries 2-31.
static const LoadFP32ImmArrElement LoadFP32ImmArr[] = {
{ 0b01101111, 0b00 }, { 0b01110000, 0b00 }, { 0b01110111, 0b00 },
{ 0b01111000, 0b00 }, { 0b01111011, 0b00 }, { 0b01111100, 0b00 },
{ 0b01111101, 0b00 }, { 0b01111101, 0b01 }, { 0b01111101, 0b10 },
{ 0b01111101, 0b11 }, { 0b01111110, 0b00 }, { 0b01111110, 0b01 },
{ 0b01111110, 0b10 }, { 0b01111110, 0b11 }, { 0b01111111, 0b00 },
{ 0b01111111, 0b01 }, { 0b01111111, 0b10 }, { 0b01111111, 0b11 },
{ 0b10000000, 0b00 }, { 0b10000000, 0b01 }, { 0b10000000, 0b10 },
{ 0b10000001, 0b00 }, { 0b10000010, 0b00 }, { 0b10000011, 0b00 },
{ 0b10000110, 0b00 }, { 0b10000111, 0b00 }, { 0b10001110, 0b00 },
{ 0b10001111, 0b00 }, { 0b11111111, 0b00 }, { 0b11111111, 0b10 },
};

float getFPImm(unsigned Imm)
{
CS_ASSERT(Imm != 1 && Imm != 30 && Imm != 31 &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imm can still be greater than the array boundaries.
Please check this as well.

"Unsupported immediate");

// Entry 0 is -1.0, the only negative value. Entry 16 is 1.0.
uint32_t Sign = 0;
if (Imm == 0) {
Sign = 0b1;
Imm = 16;
}

uint32_t Exp = LoadFP32ImmArr[Imm - 2].first;
uint32_t Mantissa = LoadFP32ImmArr[Imm - 2].second;

uint32_t I = Sign << 31 | Exp << 23 | Mantissa << 21;
float result;
memcpy(&result, &I, sizeof(float));
return result;
}

void RISCVZC_printSpimm(int64_t Spimm, SStream *OS)
{
printInt32(OS, Spimm);
}

// namespace llvm
Loading
Loading