Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5a83b4d

Browse files
add syscall instruction in Rust
1 parent5f63275 commit5a83b4d

File tree

9 files changed

+500
-19
lines changed

9 files changed

+500
-19
lines changed

‎compiler‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ use constant IS_INTEGER => 0x44;
8888
#
8989
use constantNOP_OP=> 0x50;
9090
use constantREG_STORE=> 0x51;
91+
use constantSYSCALL=> 0x52;
9192

9293

9394
#
@@ -376,6 +377,17 @@ while ( my $file = shift )
376377
} );
377378
}
378379
}
380+
elsif ($line =~/^\s*syscall\s+([^\s]+)\s*/ )
381+
{
382+
383+
# syscall
384+
my$dest =$1;
385+
$dest =hex($dest)if ($dest =~/0x/i );
386+
387+
print$outchr SYSCALL;
388+
print$outchr$dest;
389+
$offset += 2;
390+
}
379391
elsif ($line =~
380392
/^\s*(add|and|sub|mul|div|or|xor|concat)\s+#([0-9]+)\s*,\s*#([0-9]+)\s*,\s*#([0-9]+)/
381393
)

‎os/build.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ fn main() {
1919
// The input header we would like to generate
2020
// bindings for.
2121
.header("simple-vm.h")
22+
.header("simple-vm-opcodes.h")
2223
// Tell cargo to invalidate the built crate whenever any of the
2324
// included header files changed.
24-
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
25+
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
2526
// Finish the builder and generate the bindings.
2627
.generate()
2728
// Unwrap the Result and panic on failure.

‎os/simple-vm-opcodes.h‎

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/**
2+
* simple-vm-opcodes.h - Definitions of our opcode-handlers.
3+
*
4+
* Copyright (c) 2014 by Steve Kemp. All rights reserved.
5+
*
6+
**
7+
*
8+
* This program is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation; version 2 dated June, 1991, or (at your
11+
* option) any later version.
12+
*
13+
* On Debian GNU/Linux systems, the complete text of version 2 of the GNU
14+
* General Public License can be found in `/usr/share/common-licenses/GPL-2'
15+
*
16+
**
17+
*
18+
*/
19+
20+
21+
#ifndefSIMPLE_VM_OPCODES_H
22+
#defineSIMPLE_VM_OPCODES_H 1
23+
24+
25+
/**
26+
* This ENUM stores the values of our opcodes.
27+
*
28+
* This is less painful to update than having a series of #define instructions,
29+
* and ensure that we have unique and incrementing values for the various
30+
* opcodes.
31+
*
32+
* Opcodes are configured in bunches of 16, with instructions broadly
33+
* grouped together by class/type.
34+
*
35+
*/
36+
enumopcode_values {
37+
38+
/**
39+
* Early opcodes.
40+
*/
41+
EXIT=0,
42+
INT_STORE,
43+
INT_PRINT,
44+
INT_TOSTRING,
45+
INT_RANDOM,
46+
47+
/**
48+
* Jump operations.
49+
*/
50+
JUMP_TO=0x10,
51+
JUMP_Z,
52+
JUMP_NZ,
53+
54+
/**
55+
* Math operations.
56+
*/
57+
XOR=0x20,
58+
ADD,
59+
SUB,
60+
MUL,
61+
DIV,
62+
INC,
63+
DEC,
64+
AND,
65+
OR,
66+
67+
/**
68+
* String operations.
69+
*/
70+
STRING_STORE=0x30,
71+
STRING_PRINT,
72+
STRING_CONCAT,
73+
STRING_SYSTEM,
74+
STRING_TOINT,
75+
76+
/**
77+
* Comparison/Test operations.
78+
*/
79+
CMP_REG=0x40,
80+
CMP_IMMEDIATE,
81+
CMP_STRING,
82+
IS_STRING,
83+
IS_INTEGER,
84+
85+
86+
/**
87+
* Misc.
88+
*/
89+
NOP=0x50,
90+
STORE_REG,
91+
92+
/**
93+
* PEEK/POKE operations.
94+
*/
95+
PEEK=0x60,
96+
POKE,
97+
MEMCPY,
98+
99+
/**
100+
* Stack operations.
101+
*/
102+
STACK_PUSH=0x70,
103+
STACK_POP,
104+
STACK_RET,
105+
STACK_CALL
106+
};
107+
108+
109+
110+
/* 0x00 - 0x0F */
111+
voidop_exit(structsvm*in);
112+
voidop_int_store(structsvm*in);
113+
voidop_int_print(structsvm*in);
114+
voidop_int_tostring(structsvm*in);
115+
voidop_int_random(structsvm*in);
116+
117+
/* 0x10 - 0x1F */
118+
voidop_jump_to(structsvm*in);
119+
voidop_jump_z(structsvm*in);
120+
voidop_jump_nz(structsvm*in);
121+
122+
/* 0x20 - 0x2F */
123+
voidop_xor(structsvm*in);
124+
voidop_or(structsvm*in);
125+
voidop_add(structsvm*in);
126+
voidop_and(structsvm*in);
127+
voidop_sub(structsvm*in);
128+
voidop_mul(structsvm*in);
129+
voidop_div(structsvm*in);
130+
voidop_inc(structsvm*in);
131+
voidop_dec(structsvm*in);
132+
133+
/* 0x30 - 0x3F */
134+
voidop_string_store(structsvm*in);
135+
voidop_string_print(structsvm*in);
136+
voidop_string_concat(structsvm*in);
137+
voidop_string_system(structsvm*in);
138+
voidop_string_toint(structsvm*in);
139+
140+
/* 0x40 - 0x4F */
141+
voidop_cmp_reg(structsvm*in);
142+
voidop_cmp_immediate(structsvm*in);
143+
voidop_cmp_string(structsvm*in);
144+
voidop_is_string(structsvm*in);
145+
voidop_is_integer(structsvm*in);
146+
147+
/* 0x50 - 0x5F */
148+
voidop_nop(structsvm*in);
149+
voidop_reg_store(structsvm*in);
150+
151+
152+
/* 0x60 - 0x6F */
153+
voidop_peek(structsvm*in);
154+
voidop_poke(structsvm*in);
155+
voidop_memcpy(structsvm*in);
156+
157+
158+
/* 0x70 - 0x7F */
159+
voidop_stack_push(structsvm*in);
160+
voidop_stack_pop(structsvm*in);
161+
voidop_stack_ret(structsvm*in);
162+
voidop_stack_call(structsvm*in);
163+
164+
165+
166+
/**
167+
* Initialization function.
168+
*/
169+
voidopcode_init(structsvm*cpu);
170+
171+
#endif/* SIMPLE_VM_OPCODES_H */

‎os/src/lib.rs‎

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,54 @@
44
pubmod bindings{
55
include!(concat!(env!("OUT_DIR"),"/bindings.rs"));
66
}
7-
use bindings::{svm_freeas _svm_free, svm_newas _svm_new, svm_runas _svm_run};
7+
use bindings::{
8+
registers_INTEGER, registers_STRING, svm_default_error_handler, svm_freeas _svm_free,
9+
svm_newas _svm_new, svm_runas _svm_run,
10+
};
11+
use std::ffi::{CStr,CString};
12+
use std::os::raw::c_char;
813

914
pubuse bindings::svm_tasSvm;
10-
pubfnsvm_new(code:&[u8],memsize:u32) ->*mutSvm{
11-
let pcode:*mutu8 = code.as_ptr()as*mutu8;
12-
unsafe{
13-
let svm:*mutSvm =_svm_new(pcode, memsize);
14-
svm
15+
implSvm{
16+
pubfnnew(code:&[u8],memsize:u32) ->&mutSvm{
17+
let pcode:*mutu8 = code.as_ptr()as*mutu8;
18+
unsafe{
19+
let svm:*mutSvm =_svm_new(pcode, memsize);
20+
&mut*svm
21+
}
1522
}
16-
}
17-
pubfnsvm_run(svm:*mutSvm){
18-
unsafe{
19-
_svm_run(svm);
23+
pubfnrun(&mutself){
24+
unsafe{
25+
_svm_run(self);
26+
}
2027
}
21-
}
2228

23-
pubfnsvm_free(svm:*mutSvm){
24-
unsafe{
25-
_svm_free(svm);
29+
pubfnfree(&mutself){
30+
unsafe{
31+
_svm_free(self);
32+
}
33+
}
34+
pubfnget_int_reg(&mutself,reg:usize) ->u32{
35+
ifself.registers[reg].type_ == registers_INTEGER{
36+
unsafe{
37+
returnself.registers[reg].content.integer;
38+
}
39+
}
40+
self.default_error_handler("The register doesn't contain an integer");
41+
0
42+
}
43+
pubfnget_string_reg(&mutself,reg:usize) ->&str{
44+
ifself.registers[reg].type_ == registers_STRING{
45+
returnunsafe{CStr::from_ptr(self.registers[reg].content.string)}
46+
.to_str()
47+
.unwrap();
48+
}
49+
self.default_error_handler("The register doesn't contain an string");
50+
""
51+
}
52+
pubfndefault_error_handler(&mutself,msg:&str){
53+
let c_str =CString::new(msg).unwrap();
54+
let c_msg:*mutc_char = c_str.as_ptr()as*mutc_char;
55+
unsafe{svm_default_error_handler(selfas*mutSelf, c_msg)}
2656
}
2757
}

‎os/src/main.rs‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
use os::{svm_free, svm_new, svm_run,Svm};
1+
use os::Svm;
22
use std::env::args;
33
use std::fs::read;
44

5+
unsafeextern"C"fnop_syscall(svm:*mutSvm){
6+
syscall(&mut*svm);
7+
(*svm).ip +=1;
8+
}
9+
fnsyscall(svm:&mutSvm){
10+
println!("Bytecode length: {}", svm.size);
11+
println!("Value of #1: {}", svm.get_int_reg(1));
12+
svm.registers[1].content.integer = svm.get_int_reg(1)*2;
13+
println!("Value of #1: {}", svm.get_int_reg(1));
14+
}
15+
516
fnmain(){
617
for arginargs().skip(1){
718
let contents:&[u8] =&read(arg).expect("Failed to read file");
8-
let svm:*mutSvm =svm_new(contents,200);
9-
svm_run(svm);
10-
svm_free(svm);
19+
let svm:&mutSvm =Svm::new(contents,200);
20+
svm.opcodes[0x52] =Some(op_syscall);
21+
Svm::run(svm);
22+
Svm::free(svm);
1123
}
1224
}

‎os/testsys.disasm‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
2+
│00000000│ 01 01 02 00 02 01 52 02 ┊ 02 01 │•••⋄••R•┊•• │
3+
└────────┴─────────────────────────┴─────────────────────────┴────────┴────────┘
4+
01 (store) 01 (#1) 02 (0x02) 00 (int16 pad)
5+
02 (print_int) 01 (#1)
6+
52 (syscall) 02 (0x02)
7+
02 (print_int) 01 (#1)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp