Skip to content

Commit 0bd1955

Browse files
Initial auto-sync LoongArch support
- Accompanied llvm changes: capstone-engine/llvm-capstone#45 - MC Tests are generated from llvm - Instruction groups are implemented - Register accesses are implemented - Memory operands are handled for memory instructions - Code are formatted using clang-format of LLVM 17 Co-authored-by: CoA <1109673069@qq.com>
1 parent fe60b13 commit 0bd1955

File tree

227 files changed

+60674
-53
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

227 files changed

+60674
-53
lines changed

.github/labeler.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ HPPA:
6464
- suite/MC/HPPA/*
6565
- tests/test_hppa.c
6666

67+
LoongArch:
68+
- arch/LoongArch/*
69+
- cstool/cstool_loongarch.c
70+
- include/capstone/loongarch.h
71+
- suite/MC/LoongArch/*
72+
- tests/test_loongarch.c
73+
6774
M680X:
6875
- arch/M680X/*
6976
- cstool/cstool_m680x.c

.github/workflows/auto-sync.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ jobs:
4646
./src/autosync/ASUpdater.py -d -a Alpha -s IncGen
4747
./src/autosync/ASUpdater.py -d -a ARM -s IncGen
4848
./src/autosync/ASUpdater.py -d -a PPC -s IncGen
49+
./src/autosync/ASUpdater.py -d -a LoongArch -s IncGen
4950
5051
- name: CppTranslator - Patch tests
5152
run: |
@@ -60,6 +61,7 @@ jobs:
6061
./src/autosync/ASUpdater.py --ci -d -a AArch64 -s Translate
6162
./src/autosync/ASUpdater.py --ci -d -a ARM -s Translate
6263
./src/autosync/ASUpdater.py --ci -d -a PPC -s Translate
64+
./src/autosync/ASUpdater.py --ci -d -a LoongArch -s Translate
6365
6466
- name: Test Header patcher
6567
run: |

CMakeLists.txt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ if(APPLE AND NOT CAPSTONE_BUILD_MACOS_THIN)
7474
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")
7575
endif()
7676

77-
set(SUPPORTED_ARCHITECTURES ARM AARCH64 M68K MIPS PPC SPARC SYSZ XCORE X86 TMS320C64X M680X EVM MOS65XX WASM BPF RISCV SH TRICORE ALPHA HPPA)
78-
set(SUPPORTED_ARCHITECTURE_LABELS ARM AARCH64 M68K MIPS PowerPC Sparc SystemZ XCore x86 TMS320C64x M680x EVM MOS65XX WASM BPF RISCV SH TriCore Alpha HPPA)
77+
set(SUPPORTED_ARCHITECTURES ARM AARCH64 M68K MIPS PPC SPARC SYSZ XCORE X86 TMS320C64X M680X EVM MOS65XX WASM BPF RISCV SH TRICORE ALPHA HPPA LOONGARCH)
78+
set(SUPPORTED_ARCHITECTURE_LABELS ARM AARCH64 M68K MIPS PowerPC Sparc SystemZ XCore x86 TMS320C64x M680x EVM MOS65XX WASM BPF RISCV SH TriCore Alpha HPPA LoongArch)
7979

8080
list(LENGTH SUPPORTED_ARCHITECTURES count)
8181
math(EXPR count "${count}-1")
@@ -180,6 +180,7 @@ set(HEADERS_COMMON
180180
include/capstone/sh.h
181181
include/capstone/alpha.h
182182
include/capstone/hppa.h
183+
include/capstone/loongarch.h
183184
)
184185

185186
set(TEST_SOURCES test_basic.c test_detail.c test_skipdata.c test_iter.c)
@@ -646,6 +647,24 @@ if(CAPSTONE_HPPA_SUPPORT)
646647
set(TEST_SOURCES ${TEST_SOURCES} test_hppa.c)
647648
endif()
648649

650+
if (CAPSTONE_LOONGARCH_SUPPORT)
651+
add_definitions(-DCAPSTONE_HAS_LOONGARCH)
652+
set(SOURCES_LOONGARCH
653+
arch/LoongArch/LoongArchDisassembler.c
654+
arch/LoongArch/LoongArchDisassemblerExtension.c
655+
arch/LoongArch/LoongArchInstPrinter.c
656+
arch/LoongArch/LoongArchMapping.c
657+
arch/LoongArch/LoongArchModule.c
658+
)
659+
set(HEADERS_LOONGARCH
660+
arch/LoongArch/LoongArchInstPrinter.h
661+
arch/LoongArch/LoongArchMapping.h
662+
arch/LoongArch/LoongArchModule.h
663+
arch/LoongArch/LoongArchLinkage.h
664+
)
665+
set(TEST_SOURCES ${TEST_SOURCES} test_loongarch.c)
666+
endif ()
667+
649668
if (CAPSTONE_OSXKERNEL_SUPPORT)
650669
add_definitions(-DCAPSTONE_HAS_OSXKERNEL)
651670
endif()
@@ -672,6 +691,7 @@ set(ALL_SOURCES
672691
${SOURCES_TRICORE}
673692
${SOURCES_ALPHA}
674693
${SOURCES_HPPA}
694+
${SOURCES_LOONGARCH}
675695
)
676696

677697
set(ALL_HEADERS
@@ -697,6 +717,7 @@ set(ALL_HEADERS
697717
${HEADERS_TRICORE}
698718
${HEADERS_ALPHA}
699719
${HEADERS_HPPA}
720+
${HEADERS_LOONGARCH}
700721
)
701722

702723
## properties
@@ -762,6 +783,7 @@ source_group("Source\\SH" FILES ${SOURCES_SH})
762783
source_group("Source\\TriCore" FILES ${SOURCES_TRICORE})
763784
source_group("Source\\Alpha" FILES ${SOURCES_ALPHA})
764785
source_group("Source\\HPPA" FILES ${SOURCES_HPPA})
786+
source_group("Source\\LoongArch" FILES ${SOURCES_LOONGARCH})
765787

766788
source_group("Include\\Common" FILES ${HEADERS_COMMON})
767789
source_group("Include\\Engine" FILES ${HEADERS_ENGINE})
@@ -785,6 +807,7 @@ source_group("Include\\SH" FILES ${HEADERS_SH})
785807
source_group("Include\\TriCore" FILES ${HEADERS_TRICORE})
786808
source_group("Include\\Alpha" FILES ${HEADERS_ALPHA})
787809
source_group("Include\\HPPA" FILES ${HEADERS_HPPA})
810+
source_group("Include\\LoongArch" FILES ${HEADERS_LOONGARCH})
788811

789812
## installation
790813
if(CAPSTONE_INSTALL)

COMPILE.TXT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Capstone requires no prerequisite packages, so it is easy to compile & install.
9090
/usr/include/capstone/capstone.h
9191
/usr/include/capstone/evm.h
9292
/usr/include/capstone/hppa.h
93+
/usr/include/capstone/loongarch.h
9394
/usr/include/capstone/m680x.h
9495
/usr/include/capstone/m68k.h
9596
/usr/include/capstone/mips.h

COMPILE_CMAKE.TXT

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Get CMake for free from http://www.cmake.org.
2323
- CAPSTONE_AARCH64_SUPPORT: support AARCH64. Run cmake with -DCAPSTONE_AARCH64_SUPPORT=0 to remove AARCH64.
2424
- CAPSTONE_ALPHA_SUPPORT: support Alpha. Run cmake with -DCAPSTONE_ALPHA_SUPPORT=0 to remove Alpha.
2525
- CAPSTONE_HPPA_SUPPORT: support HPPA. Run cmake with -DCAPSTONE_HPPA_SUPPORT=0 to remove HPPA.
26+
- CAPSTONE_LOONGARCH_SUPPORT: support LoongArch. Run cmake with -DCAPSTONE_LOONGARCH_SUPPORT=0 to remove LoongArch.
2627
- CAPSTONE_M680X_SUPPORT: support M680X. Run cmake with -DCAPSTONE_M680X_SUPPORT=0 to remove M680X.
2728
- CAPSTONE_M68K_SUPPORT: support M68K. Run cmake with -DCAPSTONE_M68K_SUPPORT=0 to remove M68K.
2829
- CAPSTONE_MIPS_SUPPORT: support Mips. Run cmake with -DCAPSTONE_MIPS_SUPPORT=0 to remove Mips.
@@ -118,7 +119,7 @@ Get CMake for free from http://www.cmake.org.
118119

119120
Will just target the x86 architecture. The list of available architectures is: ARM,
120121
AARCH64, M68K, MIPS, PowerPC, Sparc, SystemZ, XCore, x86, TMS320C64x, M680x, EVM, MOS65XX,
121-
WASM, BPF, RISCV, Alpha, HPPA.
122+
WASM, BPF, RISCV, Alpha, HPPA, LoongArch.
122123

123124
(4) You can also create an installation image with cmake, by using the 'install' target.
124125
Use:

CREDITS.TXT

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,5 @@ fanfuqiang & citypw & porto703 : RISCV architecture.
8888
Josh "blacktop" Maine: Arm64 architecture improvements.
8989
Finn Wilkinson: AArch64 update to Armv9.2-a (SME + SVE2 support)
9090
Billow & Sidneyp : TriCore architecture.
91-
Dmitry Sibirtsev: Alpha & HPPA architecture.
91+
Dmitry Sibirtsev: Alpha & HPPA architecture.
92+
Jiajie Chen & Yanglin Xun: LoongArch architecture.

MCInst.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
typedef struct MCInst MCInst;
2727
typedef struct cs_struct cs_struct;
2828
typedef struct MCOperand MCOperand;
29+
typedef unsigned MCRegister;
2930

3031
/// MCOperand - Instances of this class represent operands of the MCInst class.
3132
/// This is a simple discriminated union.

Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,21 @@ ifneq (,$(findstring hppa,$(CAPSTONE_ARCHS)))
345345
LIBOBJ_HPPA += $(LIBSRC_HPPA:%.c=$(OBJDIR)/%.o)
346346
endif
347347

348+
DEP_LOONGARCH =
349+
DEP_LOONGARCH += $(wildcard arch/LoongArch/LoongArch*.inc)
350+
351+
LIBOBJ_LOONGARCH =
352+
ifneq (,$(findstring loongarch,$(CAPSTONE_ARCHS)))
353+
CFLAGS += -DCAPSTONE_HAS_LOONGARCH
354+
LIBSRC_LOONGARCH += $(wildcard arch/LoongArch/LoongArch*.c)
355+
LIBOBJ_LOONGARCH += $(LIBSRC_LOONGARCH:%.c=$(OBJDIR)/%.o)
356+
endif
348357

349358
LIBOBJ =
350359
LIBOBJ += $(OBJDIR)/cs.o $(OBJDIR)/utils.o $(OBJDIR)/SStream.o $(OBJDIR)/MCInstrDesc.o $(OBJDIR)/MCRegisterInfo.o $(OBJDIR)/MCInst.o $(OBJDIR)/MCInstPrinter.o $(OBJDIR)/Mapping.o
351360
LIBOBJ += $(LIBOBJ_ARM) $(LIBOBJ_AARCH64) $(LIBOBJ_M68K) $(LIBOBJ_MIPS) $(LIBOBJ_PPC) $(LIBOBJ_RISCV) $(LIBOBJ_SPARC) $(LIBOBJ_SYSZ) $(LIBOBJ_SH)
352361
LIBOBJ += $(LIBOBJ_X86) $(LIBOBJ_XCORE) $(LIBOBJ_TMS320C64X) $(LIBOBJ_M680X) $(LIBOBJ_EVM) $(LIBOBJ_MOS65XX) $(LIBOBJ_WASM) $(LIBOBJ_BPF)
353-
LIBOBJ += $(LIBOBJ_TRICORE) $(LIBOBJ_ALPHA) $(LIBOBJ_HPPA)
362+
LIBOBJ += $(LIBOBJ_TRICORE) $(LIBOBJ_ALPHA) $(LIBOBJ_HPPA) $(LIBOBJ_LOONGARCH)
354363

355364

356365
ifeq ($(PKG_EXTRA),)
@@ -488,6 +497,7 @@ $(LIBOBJ_BPF): $(DEP_BPF)
488497
$(LIBOBJ_TRICORE): $(DEP_TRICORE)
489498
$(LIBOBJ_ALPHA): $(DEP_ALPHA)
490499
$(LIBOBJ_HPPA): $(DEP_HPPA)
500+
$(LIBOBJ_LOONGARCH): $(DEP_LOONGARCH)
491501

492502
ifeq ($(CAPSTONE_STATIC),yes)
493503
$(ARCHIVE): $(LIBOBJ)
@@ -575,11 +585,13 @@ dist:
575585

576586
TESTS = test_basic test_detail test_arm test_aarch64 test_m68k test_mips test_ppc test_sparc test_tricore test_hppa
577587
TESTS += test_systemz test_x86 test_xcore test_iter test_evm test_riscv test_mos65xx test_wasm test_bpf test_alpha
588+
TESTS += test_loongarch
578589
TESTS += test_basic.static test_detail.static test_arm.static test_aarch64.static
579590
TESTS += test_m68k.static test_mips.static test_ppc.static test_sparc.static
580591
TESTS += test_systemz.static test_x86.static test_xcore.static test_m680x.static
581592
TESTS += test_skipdata test_skipdata.static test_iter.static test_evm.static test_riscv.static
582593
TESTS += test_mos65xx.static test_wasm.static test_bpf.static test_alpha.static test_hppa.static
594+
TESTS += test_loongarch.static
583595

584596
check: $(TESTS)
585597

Mapping.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ DEFINE_get_detail_op(tricore, TriCore);
335335
DEFINE_get_detail_op(aarch64, AArch64);
336336
DEFINE_get_detail_op(alpha, Alpha);
337337
DEFINE_get_detail_op(hppa, HPPA);
338+
DEFINE_get_detail_op(loongarch, LoongArch);
338339

339340
/// Returns true if for this architecture the
340341
/// alias operands should be filled.

Mapping.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ typedef struct insn_map {
3030
bool indirect_branch; // indirect branch instruction?
3131
union {
3232
ppc_suppl_info ppc;
33+
loongarch_suppl_info loongarch;
3334
} suppl_info; // Supplementary information for each instruction.
3435
#endif
3536
} insn_map;
@@ -125,6 +126,7 @@ DECL_get_detail_op(tricore, TriCore);
125126
DECL_get_detail_op(aarch64, AArch64);
126127
DECL_get_detail_op(alpha, Alpha);
127128
DECL_get_detail_op(hppa, HPPA);
129+
DECL_get_detail_op(loongarch, LoongArch);
128130

129131
/// Increments the detail->arch.op_count by one.
130132
#define DEFINE_inc_detail_op_count(arch, ARCH) \
@@ -152,6 +154,8 @@ DEFINE_inc_detail_op_count(alpha, Alpha);
152154
DEFINE_dec_detail_op_count(alpha, Alpha);
153155
DEFINE_inc_detail_op_count(hppa, HPPA);
154156
DEFINE_dec_detail_op_count(hppa, HPPA);
157+
DEFINE_inc_detail_op_count(loongarch, LoongArch);
158+
DEFINE_dec_detail_op_count(loongarch, LoongArch);
155159

156160
/// Returns true if a memory operand is currently edited.
157161
static inline bool doing_mem(const MCInst *MI)
@@ -179,6 +183,7 @@ DEFINE_get_arch_detail(tricore, TriCore);
179183
DEFINE_get_arch_detail(aarch64, AArch64);
180184
DEFINE_get_arch_detail(alpha, Alpha);
181185
DEFINE_get_arch_detail(hppa, HPPA);
186+
DEFINE_get_arch_detail(loongarch, LoongArch);
182187

183188
static inline bool detail_is_set(const MCInst *MI)
184189
{

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Created by Nguyen Anh Quynh, then developed and maintained by a small community,
1616
Capstone offers some unparalleled features:
1717

1818
- Support multiple hardware architectures: ARM, AArch64, Alpha, BPF, Ethereum VM,
19-
HP PA-RISC (HPPA), M68K, M680X, Mips, MOS65XX, PPC, RISC-V(rv32G/rv64G), SH, Sparc,
20-
SystemZ, TMS320C64X, TriCore, Webassembly, XCore and X86 (16, 32, 64).
19+
LoongArch, HP PA-RISC (HPPA), M68K, M680X, Mips, MOS65XX, PPC, RISC-V(rv32G/rv64G), SH,
20+
Sparc, SystemZ, TMS320C64X, TriCore, Webassembly, XCore and X86 (16, 32, 64).
2121

2222
- Having clean/simple/lightweight/intuitive architecture-neutral API.
2323

0 commit comments

Comments
 (0)