Skip to content

Commit ce229be

Browse files
authored
Merge pull request #549 from null-cell/master
Adds RISCV support to Keystone
2 parents 1856935 + 5199a5a commit ce229be

File tree

85 files changed

+15235
-20
lines changed

Some content is hidden

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

85 files changed

+15235
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ samples/sample
2323
_sample*.txt
2424
tmp
2525
MANIFEST
26+
.vscode
2627

2728
bindings/python/dist/
2829
bindings/python/src/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Keystone Engine
88
Keystone is a lightweight multi-platform, multi-architecture assembler framework.
99
It offers some unparalleled features:
1010

11-
- Multi-architecture, with support for Arm, Arm64 (AArch64/Armv8), Ethereum Virtual Machine, Hexagon, Mips, PowerPC, Sparc, SystemZ & X86 (include 16/32/64bit).
11+
- Multi-architecture, with support for Arm, Arm64 (AArch64/Armv8), Ethereum Virtual Machine, Hexagon, Mips, PowerPC, RISC-V, Sparc, SystemZ & X86 (include 16/32/64bit).
1212
- Clean/simple/lightweight/intuitive architecture-neutral API.
1313
- Implemented in C/C++ languages, with bindings for Java, Masm, C#, PowerShell, Perl, Python, NodeJS, Ruby, Go, Rust, Haskell, VB6 & OCaml available.
1414
- Native support for Windows & \*nix (with Mac OSX, Linux, \*BSD & Solaris confirmed).

bindings/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SAMPLE_MIPS = $(TMPDIR)/sample_mips
1010
SAMPLE_M68K = $(TMPDIR)/sample_m68k
1111
SAMPLE_SPARC = $(TMPDIR)/sample_sparc
1212
SAMPLE_X86 = $(TMPDIR)/sample_x86
13+
SAMPLE_RISCV = $(TMPDIR)/sample_riscv
1314

1415
.PHONY: all expected python rust go nodejs ruby
1516

@@ -34,6 +35,7 @@ expected:
3435
../samples/sample_sparc > $(SAMPLE_SPARC)_e
3536
../samples/sample_m68k > $(SAMPLE_M68K)_e
3637
../samples/sample_x86 > $(SAMPLE_X86)_e
38+
../samples/sample_riscv > $(SAMPLE_RISCV)_e
3739

3840
python: FORCE
3941
cd python && $(MAKE)
@@ -43,6 +45,7 @@ python: FORCE
4345
python python/sample_sparc.py > $(SAMPLE_SPARC)_o
4446
python python/sample_m68k.py > $(SAMPLE_M68K)_o
4547
python python/sample_x86.py > $(SAMPLE_X86)_o
48+
python python/sample_riscv.py > $(SAMPLE_RISCV)_o
4649
$(MAKE) sample_diff
4750

4851
ruby: FORCE
@@ -67,6 +70,7 @@ sample_diff: FORCE
6770
$(DIFF) $(SAMPLE_SPARC)_e $(SAMPLE_SPARC)_o
6871
$(DIFF) $(SAMPLE_M68K)_e $(SAMPLE_M68K)_o
6972
$(DIFF) $(SAMPLE_X86)_e $(SAMPLE_X86)_o
73+
$(DIFF) $(SAMPLE_RISCV)_e $(SAMPLE_RISCV)_o
7074

7175
clean:
7276
rm -rf $(TMPDIR)

bindings/const_generator.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# NOTE: this reflects the value of KS_ERR_ASM_xxx in keystone.h
99
ks_err_val = { 'KS_ERR_ASM': '128', 'KS_ERR_ASM_ARCH': '512' }
1010

11-
include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'sparc.h', 'ppc.h', 'systemz.h', 'hexagon.h', 'evm.h', 'keystone.h' ]
11+
include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h', 'sparc.h', 'riscv.h', 'ppc.h', 'systemz.h', 'hexagon.h', 'evm.h', 'keystone.h' ]
1212

1313
def CamelCase(s):
1414
# return re.sub(r'(\w)+\_?', lambda m:m.group(0).capitalize(), s)
@@ -40,6 +40,7 @@ def CamelCase(s):
4040
'mips.h': 'keystone',
4141
'x86.h': 'keystone',
4242
'sparc.h': 'keystone',
43+
'riscv.h': 'keystone',
4344
'systemz.h': 'keystone',
4445
'ppc.h': 'keystone',
4546
'hexagon.h': 'keystone',
@@ -112,6 +113,7 @@ def CamelCase(s):
112113
'mips.h': 'mips',
113114
'x86.h': 'x86',
114115
'sparc.h': 'sparc',
116+
'riscv.h': 'riscv',
115117
'systemz.h': 'systemz',
116118
'ppc.h': 'ppc',
117119
'hexagon.h': 'hexagon',
@@ -174,6 +176,7 @@ def CamelCase(s):
174176
'mips.h': 'mips',
175177
'x86.h': 'x86',
176178
'sparc.h': 'sparc',
179+
'riscv.h': 'riscv',
177180
'systemz.h': 'systemz',
178181
'ppc.h': 'ppc',
179182
'hexagon.h': 'hexagon',
@@ -199,6 +202,7 @@ def CamelCase(s):
199202
'mips.h': 'mips',
200203
'x86.h': 'x86',
201204
'sparc.h': 'sparc',
205+
'riscv.h': 'riscv',
202206
'systemz.h': 'systemz',
203207
'ppc.h': 'ppc',
204208
'hexagon.h': 'hexagon',
@@ -224,6 +228,7 @@ def CamelCase(s):
224228
'mips.h': 'mips',
225229
'x86.h': 'x86',
226230
'sparc.h': 'sparc',
231+
'riscv.h': 'riscv.h',
227232
'systemz.h': 'systemz',
228233
'ppc.h': 'ppc',
229234
'hexagon.h': 'hexagon',
@@ -250,6 +255,7 @@ def CamelCase(s):
250255
'mips.h': 'mips',
251256
'x86.h': 'x86',
252257
'sparc.h': 'sparc',
258+
'riscv.h': 'riscv',
253259
'systemz.h': 'systemz',
254260
'ppc.h': 'ppc',
255261
'hexagon.h': 'hexagon',
@@ -387,7 +393,7 @@ def gen(lang):
387393

388394
for prefix in consts.keys():
389395
outfile = open(templ['out_file'] % prefix, 'wb') # open as binary prevents windows newlines
390-
outfile.write (templ['header'] % prefix)
396+
outfile.write (str.encode(templ['header'] % prefix))
391397

392398
for rule in rules:
393399
regex = rule['regex']
@@ -403,18 +409,18 @@ def gen(lang):
403409
continue
404410

405411
if rule.get('pre'):
406-
outfile.write(rule.get('pre').format(CamelCase(prefix)))
412+
outfile.write(str.encode(rule.get('pre').format(CamelCase(prefix))))
407413

408414
for const in consts2:
409415
lhs_strip = const[0]
410416
rhs = const[1]
411417
outfile.write(rule['line_format'].format(rule['fn'](lhs_strip), rhs, lhs_strip).encode("utf-8"))
412418

413419
if rule.get('post'):
414-
outfile.write (rule.get('post'))
415-
outfile.write ('\n')
420+
outfile.write(str.encode (rule.get('post')))
421+
outfile.write(str.encode ('\n'))
416422

417-
outfile.write (templ['footer'])
423+
outfile.write(str.encode(templ['footer']))
418424
outfile.close()
419425

420426
def main():

bindings/go/keystone/keystone_const.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const (
2323
MODE_PPC32 Mode = 4
2424
MODE_PPC64 Mode = 8
2525
MODE_QPX Mode = 16
26+
MODE_RISCV32 Mode = 4
27+
MODE_RISCV64 Mode = 8
2628
MODE_SPARC32 Mode = 4
2729
MODE_SPARC64 Mode = 8
2830
MODE_V9 Mode = 16
@@ -38,7 +40,8 @@ const (
3840
ARCH_SYSTEMZ Architecture = 7
3941
ARCH_HEXAGON Architecture = 8
4042
ARCH_EVM Architecture = 9
41-
ARCH_MAX Architecture = 10
43+
ARCH_RISCV Architecture = 10
44+
ARCH_MAX Architecture = 11
4245
)
4346

4447
const (
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package keystone
2+
// For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [riscv_const.go]
3+
4+
const (
5+
ERR_ASM_RISCV_INVALIDOPERAND Error = 512
6+
ERR_ASM_RISCV_MISSINGFEATURE Error = 513
7+
ERR_ASM_RISCV_MNEMONICFAIL Error = 514
8+
)
9+

bindings/nodejs/consts/keystone.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ module.exports.ARCH_SPARC = 6
1313
module.exports.ARCH_SYSTEMZ = 7
1414
module.exports.ARCH_HEXAGON = 8
1515
module.exports.ARCH_EVM = 9
16-
module.exports.ARCH_MAX = 10
16+
module.exports.ARCH_RISCV = 10
17+
module.exports.ARCH_MAX = 11
1718
module.exports.MODE_LITTLE_ENDIAN = 0
1819
module.exports.MODE_BIG_ENDIAN = 1073741824
1920
module.exports.MODE_ARM = 1
@@ -30,6 +31,8 @@ module.exports.MODE_64 = 8
3031
module.exports.MODE_PPC32 = 4
3132
module.exports.MODE_PPC64 = 8
3233
module.exports.MODE_QPX = 16
34+
module.exports.MODE_RISCV32 = 4
35+
module.exports.MODE_RISCV64 = 8
3336
module.exports.MODE_SPARC32 = 4
3437
module.exports.MODE_SPARC64 = 8
3538
module.exports.MODE_V9 = 16

bindings/nodejs/consts/riscv.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [riscv_const.js]
2+
module.exports.ERR_ASM_RISCV_INVALIDOPERAND = 512
3+
module.exports.ERR_ASM_RISCV_MISSINGFEATURE = 513
4+
module.exports.ERR_ASM_RISCV_MNEMONICFAIL = 514

bindings/powershell/Keystone/Const/keystone_h.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
KS_ARCH_SYSTEMZ = 7,
1414
KS_ARCH_HEXAGON = 8,
1515
KS_ARCH_EVM = 9,
16-
KS_ARCH_MAX = 10,
16+
KS_ARCH_RISCV = 10,
17+
KS_ARCH_MAX = 11,
1718
KS_MODE_LITTLE_ENDIAN = 0,
1819
KS_MODE_BIG_ENDIAN = 1073741824,
1920
KS_MODE_ARM = 1,
@@ -30,6 +31,8 @@
3031
KS_MODE_PPC32 = 4,
3132
KS_MODE_PPC64 = 8,
3233
KS_MODE_QPX = 16,
34+
KS_MODE_RISCV32 = 4,
35+
KS_MODE_RISCV64 = 8,
3336
KS_MODE_SPARC32 = 4,
3437
KS_MODE_SPARC64 = 8,
3538
KS_MODE_V9 = 16,

bindings/python/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ else
66
OBJDIR = $(abspath $(BUILDDIR))/obj/bindings/python
77
endif
88

9+
910
.PHONY: gen_const install install3 clean
1011

1112
gen_const:

0 commit comments

Comments
 (0)