Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
aa8e587
Refactoring RISCV: INC generatione done, RISCV added to arch_config.j…
moste00 Jul 16, 2025
fb08d9d
the C++ translator runs successfully and first output under arch/RISC…
moste00 Jul 18, 2025
0c2cc3b
fixed Disassembler.c, in the process of fixing InstPrinter
moste00 Jul 19, 2025
ad513b1
more compile errors fixed, RISCVInstPrinter error-free
moste00 Jul 26, 2025
8a9626c
plugins compiles and runs, several test failures were fixed, current …
moste00 Aug 1, 2025
a275efd
compressed instructions compression and uncompression logic was gener…
moste00 Aug 23, 2025
cfc5ef8
massive fix handling various issues, test failures down to 35
moste00 Sep 5, 2025
012099c
test failures down to 2, related to inline option arch directives
moste00 Sep 5, 2025
b4d9e1b
added options to `cstool`, and started working on details test, 39/10…
moste00 Sep 16, 2025
e074c3d
more successful details test, added CSR operand type and changed csto…
moste00 Sep 24, 2025
0949896
more progress
moste00 Oct 10, 2025
0d8581c
failures decreased to 2
moste00 Oct 15, 2025
e1eaf2e
milestone: test failures 0
moste00 Oct 15, 2025
1d59145
add additional files generated from ASUpdate and update CMakeLists
moste00 Oct 17, 2025
e15a39b
fix most issues tests except the one about c_srli allowing illegal sh…
moste00 Oct 19, 2025
7f45480
fixed c_srli bug by updating the generated files from a fixed LLVM ve…
moste00 Oct 22, 2025
7117a43
made branch addresses absolute
moste00 Nov 10, 2025
6e3587d
fix DIET build by #ifdef-ing code that uses struct fields not availab…
moste00 Nov 10, 2025
f99aa52
fix Python build failure
moste00 Nov 14, 2025
fd662c8
fix formatting
moste00 Nov 16, 2025
1a87d2e
fixed formatting 2
moste00 Nov 16, 2025
bd20573
Remove the call group from JAL and JALR instructions because they're …
moste00 Nov 17, 2025
3083242
final formatting issue
moste00 Nov 17, 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 @@ -596,25 +596,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 @@ -506,6 +506,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 <[email protected]>, 2013-2022, */
/* Rot127 <[email protected]> 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