Skip to content

Commit

Permalink
Modify Makefiles for TriCore architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
sidneyp authored and imbillow committed Apr 13, 2023
1 parent 7449ebc commit 6374a77
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 9 deletions.
31 changes: 28 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ option(CAPSTONE_ARCHITECTURE_DEFAULT "Whether architectures are enabled by defau
option(CAPSTONE_DEBUG "Whether to enable extra debug assertions" OFF)
option(CAPSTONE_INSTALL "Generate install target" ${PROJECT_IS_TOP_LEVEL})

set(SUPPORTED_ARCHITECTURES ARM ARM64 M68K MIPS PPC SPARC SYSZ XCORE X86 TMS320C64X M680X EVM MOS65XX WASM BPF RISCV SH)
set(SUPPORTED_ARCHITECTURE_LABELS ARM ARM64 M68K MIPS PowerPC Sparc SystemZ XCore x86 TMS320C64x M680x EVM MOS65XX WASM BPF RISCV SH)
set(SUPPORTED_ARCHITECTURES ARM ARM64 M68K MIPS PPC SPARC SYSZ XCORE X86 TMS320C64X M680X EVM MOS65XX WASM BPF RISCV SH TriCore)
set(SUPPORTED_ARCHITECTURE_LABELS ARM ARM64 M68K MIPS PowerPC Sparc SystemZ XCore x86 TMS320C64x M680x EVM MOS65XX WASM BPF RISCV SH TriCore)

list(LENGTH SUPPORTED_ARCHITECTURES count)
math(EXPR count "${count}-1")
Expand Down Expand Up @@ -135,6 +135,7 @@ set(HEADERS_COMMON
include/capstone/bpf.h
include/capstone/riscv.h
include/capstone/sh.h
include/capstone/tricore.h
include/capstone/platform.h
)

Expand Down Expand Up @@ -533,7 +534,27 @@ if(CAPSTONE_SH_SUPPORT)
set(TEST_SOURCES ${TEST_SOURCES} test_sh.c)
endif()

if(CAPSTONE_OSXKERNEL_SUPPORT)
if (CAPSTONE_TRICORE_SUPPORT)
add_definitions(-DCAPSTONE_HAS_TRICORE)
set(SOURCES_TRICORE
arch/TriCore/TriCoreDisassembler.c
arch/TriCore/TriCoreInstPrinter.c
arch/TriCore/TriCoreMapping.c
arch/TriCore/TriCoreModule.c
)
set(HEADERS_TRICORE
arch/TriCore/TriCoreDisassembler.h
arch/TriCore/TriCoreGenAsmWriter.inc
arch/TriCore/TriCoreGenDisassemblerTables.inc
arch/TriCore/TriCoreGenInstrInfo.inc
arch/TriCore/TriCoreGenRegisterInfo.inc
arch/TriCore/TriCoreInstPrinter.h
arch/TriCore/TriCoreMapping.h
)
set(TEST_SOURCES ${TEST_SOURCES} test_tricore.c)
endif ()

if (CAPSTONE_OSXKERNEL_SUPPORT)
add_definitions(-DCAPSTONE_HAS_OSXKERNEL)
endif()

Expand All @@ -556,6 +577,7 @@ set(ALL_SOURCES
${SOURCES_BPF}
${SOURCES_RISCV}
${SOURCES_SH}
${SOURCES_TRICORE}
)

set(ALL_HEADERS
Expand All @@ -578,6 +600,7 @@ set(ALL_HEADERS
${HEADERS_BPF}
${HEADERS_RISCV}
${HEADERS_SH}
${HEADERS_TRICORE}
)

## properties
Expand Down Expand Up @@ -640,6 +663,7 @@ source_group("Source\\MOS65XX" FILES ${SOURCES_MOS65XX})
source_group("Source\\BPF" FILES ${SOURCES_BPF})
source_group("Source\\RISCV" FILES ${SOURCES_RISCV})
source_group("Source\\SH" FILES ${SOURCES_SH})
source_group("Source\\TriCore" FILES ${SOURCES_TRICORE})

source_group("Include\\Common" FILES ${HEADERS_COMMON})
source_group("Include\\Engine" FILES ${HEADERS_ENGINE})
Expand All @@ -660,6 +684,7 @@ source_group("Include\\MOS65XX" FILES ${HEADERS_MOS65XX})
source_group("Include\\BPF" FILES ${HEADERS_BPF})
source_group("Include\\RISCV" FILES ${HEADERS_RISCV})
source_group("Include\\SH" FILES ${HEADERS_SH})
source_group("Include\\TriCore" FILES ${HEADERS_TRICORE})

## installation
if(CAPSTONE_INSTALL)
Expand Down
1 change: 1 addition & 0 deletions COMPILE.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Capstone requires no prerequisite packages, so it is easy to compile & install.
/usr/include/capstone/wasm.h
/usr/include/capstone/x86.h
/usr/include/capstone/xcore.h
/usr/include/capstone/tricore.h
/usr/lib/libcapstone.a
/usr/lib/libcapstone.so (for Linux/*nix), or /usr/lib/libcapstone.dylib (OSX)

Expand Down
1 change: 1 addition & 0 deletions COMPILE_CMAKE.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Get CMake for free from http://www.cmake.org.
- CAPSTONE_SPARC_SUPPORT: support Sparc. Run cmake with -DCAPSTONE_SPARC_SUPPORT=0 to remove Sparc.
- CAPSTONE_SYSZ_SUPPORT: support SystemZ. Run cmake with -DCAPSTONE_SYSZ_SUPPORT=0 to remove SystemZ.
- CAPSTONE_XCORE_SUPPORT: support XCore. Run cmake with -DCAPSTONE_XCORE_SUPPORT=0 to remove XCore.
- CAPSTONE_TRICORE_SUPPORT: support TriCore. Run cmake with -DCAPSTONE_TRICORE_SUPPORT=0 to remove TriCore.
- CAPSTONE_X86_SUPPORT: support X86. Run cmake with -DCAPSTONE_X86_SUPPORT=0 to remove X86.
- CAPSTONE_X86_TMS320C64X: support TMS320C64X. Run cmake with -DCAPSTONE_TMS320C64X_SUPPORT=0 to remove TMS320C64X.
- CAPSTONE_X86_M680X: support M680X. Run cmake with -DCAPSTONE_M680X_SUPPORT=0 to remove M680X.
Expand Down
1 change: 1 addition & 0 deletions COMPILE_MSVC.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ versions, and Windows Driver Kit 8.1 Update 1 or newer versions are required.
- CAPSTONE_HAS_SYSZ: support SystemZ. Delete this to remove SystemZ support.
- CAPSTONE_HAS_X86: support X86. Delete this to remove X86 support.
- CAPSTONE_HAS_XCORE: support XCore. Delete this to remove XCore support.
- CAPSTONE_HAS_TRICORE: support TriCore. Delete this to remove TriCore support.

By default, all 9 architectures are compiled in.

Expand Down
19 changes: 18 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,27 @@ ifneq (,$(findstring bpf,$(CAPSTONE_ARCHS)))
LIBOBJ_BPF += $(LIBSRC_BPF:%.c=$(OBJDIR)/%.o)
endif

DEP_TRICORE =
DEP_TRICORE += arch/TriCore/TriCoreGenAsmWriter.inc
DEP_TRICORE += arch/TriCore/TriCoreGenInstrInfo.inc
DEP_TRICORE += arch/TriCore/TriCoreGenDisassemblerTables.inc
DEP_TRICORE += arch/TriCore/TriCoreGenRegisterInfo.inc

LIBOBJ_TRICORE =
ifneq (,$(findstring tricore,$(CAPSTONE_ARCHS)))
CFLAGS += -DCAPSTONE_HAS_TRICORE
LIBOBJ_TRICORE += $(OBJDIR)/arch/TriCore/TriCoreDisassembler.o
LIBOBJ_TRICORE += $(OBJDIR)/arch/TriCore/TriCoreInstPrinter.o
LIBOBJ_TRICORE += $(OBJDIR)/arch/TriCore/TriCoreMapping.o
LIBOBJ_TRICORE += $(OBJDIR)/arch/TriCore/TriCoreModule.o
endif


LIBOBJ =
LIBOBJ += $(OBJDIR)/cs.o $(OBJDIR)/utils.o $(OBJDIR)/SStream.o $(OBJDIR)/MCInstrDesc.o $(OBJDIR)/MCRegisterInfo.o
LIBOBJ += $(LIBOBJ_ARM) $(LIBOBJ_ARM64) $(LIBOBJ_M68K) $(LIBOBJ_MIPS) $(LIBOBJ_PPC) $(LIBOBJ_RISCV) $(LIBOBJ_SPARC) $(LIBOBJ_SYSZ)
LIBOBJ += $(LIBOBJ_X86) $(LIBOBJ_XCORE) $(LIBOBJ_TMS320C64X) $(LIBOBJ_M680X) $(LIBOBJ_EVM) $(LIBOBJ_MOS65XX) $(LIBOBJ_WASM) $(LIBOBJ_BPF)
LIBOBJ += $(LIBOBJ_TRICORE)
LIBOBJ += $(OBJDIR)/MCInst.o


Expand Down Expand Up @@ -445,6 +461,7 @@ $(LIBOBJ_RISCV): $(DEP_RISCV)
$(LIBOBJ_WASM): $(DEP_WASM)
$(LIBOBJ_MOS65XX): $(DEP_MOS65XX)
$(LIBOBJ_BPF): $(DEP_BPF)
$(LIBOBJ_TRICORE): $(DEP_TRICORE)

ifeq ($(CAPSTONE_STATIC),yes)
$(ARCHIVE): $(LIBOBJ)
Expand Down Expand Up @@ -530,7 +547,7 @@ dist:
git archive --format=tar.gz --prefix=capstone-$(DIST_VERSION)/ $(TAG) > capstone-$(DIST_VERSION).tgz
git archive --format=zip --prefix=capstone-$(DIST_VERSION)/ $(TAG) > capstone-$(DIST_VERSION).zip

TESTS = test_basic test_detail test_arm test_arm64 test_m68k test_mips test_ppc test_sparc
TESTS = test_basic test_detail test_arm test_arm64 test_m68k test_mips test_ppc test_sparc
TESTS += test_systemz test_x86 test_xcore test_iter test_evm test_riscv test_mos65xx test_wasm test_bpf
TESTS += test_basic.static test_detail.static test_arm.static test_arm64.static
TESTS += test_m68k.static test_mips.static test_ppc.static test_sparc.static
Expand Down
10 changes: 10 additions & 0 deletions arch/TriCore/TriCoreModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by aya on 3/4/23.
//

#ifndef CAPSTONE_TRICODEMODULE_H
#define CAPSTONE_TRICODEMODULE_H

// TODO: Add tricore function declarations here

#endif // CAPSTONE_TRICODEMODULE_H
2 changes: 1 addition & 1 deletion config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
################################################################################
# Specify which archs you want to compile in. By default, we build all archs.

CAPSTONE_ARCHS ?= arm aarch64 m68k mips powerpc sparc systemz x86 xcore tms320c64x m680x evm riscv mos65xx wasm bpf
CAPSTONE_ARCHS ?= arm aarch64 m68k mips powerpc sparc systemz x86 xcore tms320c64x m680x evm riscv mos65xx wasm bpf tricore


################################################################################
Expand Down
27 changes: 24 additions & 3 deletions cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#include "arch/MOS65XX/MOS65XXModule.h"
#include "arch/BPF/BPFModule.h"
#include "arch/SH/SHModule.h"
#include "arch/TriCore/TriCoreModule.h"

static const struct {
// constructor initialization
Expand Down Expand Up @@ -298,6 +299,9 @@ static const uint32_t all_arch = 0
#ifdef CAPSTONE_HAS_SH
| (1 << CS_ARCH_SH)
#endif
#ifdef CAPSTONE_HAS_TRICORE
| (1 << CS_ARCH_TRICORE)
#endif
;

#if defined(CAPSTONE_USE_SYS_DYN_MEM)
Expand Down Expand Up @@ -368,9 +372,9 @@ bool CAPSTONE_API cs_support(int query)
(1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE) |
(1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X) |
(1 << CS_ARCH_M680X) | (1 << CS_ARCH_EVM) |
(1 << CS_ARCH_RISCV) | (1 << CS_ARCH_MOS65XX) |
(1 << CS_ARCH_WASM) | (1 << CS_ARCH_BPF) |
(1 << CS_ARCH_SH));
(1 << CS_ARCH_RISCV) | (1 << CS_ARCH_MOS65XX) |
(1 << CS_ARCH_WASM) | (1 << CS_ARCH_BPF)) |
(1 << CS_ARCH_SH) | (1 << CS_ARCH_TRICORE);

if ((unsigned int)query < CS_ARCH_MAX)
return all_arch & (1 << query);
Expand Down Expand Up @@ -674,6 +678,10 @@ static uint8_t skipdata_size(cs_struct *handle)
return 4;
case CS_ARCH_SH:
return 2;
case CS_ARCH_TRICORE:
// TriCore instruction's length can be 2 or 4 bytes,
// so we just skip 2 bytes
return 2;
}
}

Expand Down Expand Up @@ -1405,6 +1413,11 @@ int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
if (insn->detail->riscv.operands[i].type == (riscv_op_type)op_type)
count++;
break;
case CS_ARCH_TRICORE:
for (i = 0; i < insn->detail->tricore.op_count; i++)
if (insn->detail->tricore.operands[i].type == (tricore_op_type)op_type)
count++;
break;
}

return count;
Expand Down Expand Up @@ -1506,6 +1519,14 @@ int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
return i;
}
break;
case CS_ARCH_TRICORE:
for (i = 0; i < insn->detail->tricore.op_count; i++) {
if (insn->detail->tricore.operands[i].type == (tricore_op_type)op_type)
count++;
if (count == post)
return i;
}
break;
case CS_ARCH_M68K:
for (i = 0; i < insn->detail->m68k.op_count; i++) {
if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
Expand Down
14 changes: 13 additions & 1 deletion include/capstone/capstone.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ typedef size_t csh;

/// Architecture type
typedef enum cs_arch {
CS_ARCH_ARM = 0, // ARM architecture (including Thumb, Thumb-2)
CS_ARCH_ARM64, // ARM-64, also called AArch64
CS_ARCH_MIPS, // Mips architecture
CS_ARCH_X86, // X86 architecture (including x86 & x86-64)
CS_ARCH_PPC, // PowerPC architecture
CS_ARCH_SPARC, // Sparc architecture
CS_ARCH_SYSZ, // SystemZ architecture
CS_ARCH_XCORE, // XCore architecture
CS_ARCH_ARM = 0, ///< ARM architecture (including Thumb, Thumb-2)
CS_ARCH_ARM64, ///< ARM-64, also called AArch64
CS_ARCH_MIPS, ///< Mips architecture
Expand All @@ -89,6 +97,7 @@ typedef enum cs_arch {
CS_ARCH_BPF, ///< Berkeley Packet Filter architecture (including eBPF)
CS_ARCH_RISCV, ///< RISCV architecture
CS_ARCH_SH, ///< SH architecture
CS_ARCH_TRICORE, // TriCore architecture
CS_ARCH_MAX,
CS_ARCH_ALL = 0xFFFF, // All architectures - for cs_support()
} cs_arch;
Expand Down Expand Up @@ -151,7 +160,7 @@ typedef enum cs_mode {
CS_MODE_MOS65XX_65C02 = 1 << 2, ///< MOS65XXX WDC 65c02
CS_MODE_MOS65XX_W65C02 = 1 << 3, ///< MOS65XXX WDC W65c02
CS_MODE_MOS65XX_65816 = 1 << 4, ///< MOS65XXX WDC 65816, 8-bit m/x
CS_MODE_MOS65XX_65816_LONG_M = (1 << 5), ///< MOS65XXX WDC 65816, 16-bit m, 8-bit x
CS_MODE_MOS65XX_65816_LONG_M = (1 << 5), ///< MOS65XXX WDC 65816, 16-bit m, 8-bit x
CS_MODE_MOS65XX_65816_LONG_X = (1 << 6), ///< MOS65XXX WDC 65816, 8-bit m, 16-bit x
CS_MODE_MOS65XX_65816_LONG_MX = CS_MODE_MOS65XX_65816_LONG_M | CS_MODE_MOS65XX_65816_LONG_X,
CS_MODE_SH2 = 1 << 1, ///< SH2
Expand Down Expand Up @@ -289,6 +298,7 @@ typedef struct cs_opt_skipdata {
/// WASM: 1 bytes.
/// MOS65XX: 1 bytes.
/// BPF: 8 bytes.
/// TriCore: 2 bytes.
cs_skipdata_cb_t callback; // default value is NULL

/// User-defined data to be passed to @callback function pointer.
Expand All @@ -313,6 +323,7 @@ typedef struct cs_opt_skipdata {
#include "mos65xx.h"
#include "bpf.h"
#include "sh.h"
#include "tricore.h"

/// NOTE: All information in cs_detail is only available when CS_OPT_DETAIL = CS_OPT_ON
/// Initialized as memset(., 0, offsetof(cs_detail, ARCH)+sizeof(cs_ARCH))
Expand Down Expand Up @@ -348,6 +359,7 @@ typedef struct cs_detail {
cs_bpf bpf; ///< Berkeley Packet Filter architecture (including eBPF)
cs_riscv riscv; ///< RISCV architecture
cs_sh sh; ///< SH architecture
cs_tricore tricore; // XCore architecture
};
} cs_detail;

Expand Down
3 changes: 3 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ ifneq (,$(findstring bpf,$(CAPSTONE_ARCHS)))
CFLAGS += -DCAPSTONE_HAS_BPF
SOURCES += test_bpf.c
endif
ifneq (,$(findstring tricore,$(CAPSTONE_ARCHS)))
SOURCES += test_tricore.c
endif

OBJS = $(addprefix $(OBJDIR)/,$(SOURCES:.c=.o))
BINARY = $(addprefix $(TESTDIR)/,$(SOURCES:.c=$(BIN_EXT)))
Expand Down
Loading

0 comments on commit 6374a77

Please sign in to comment.