@@ -11,29 +11,30 @@ struct Compiler {
1111impl Compiler {
1212 fn new ( ) -> Compiler {
1313 let mut opcode_list: BiMap < & ' static str , i32 > = BiMap :: new ( ) ;
14- opcode_list. insert ( "OP_0" , 0x00 ) ;
15- opcode_list. insert ( "OP_1" , 0x51 ) ;
16- opcode_list. insert ( "OP_2" , 0x52 ) ;
17- opcode_list. insert ( "OP_3" , 0x53 ) ;
18- opcode_list. insert ( "OP_4" , 0x54 ) ;
19- opcode_list. insert ( "OP_5" , 0x55 ) ;
20- opcode_list. insert ( "OP_6" , 0x56 ) ;
21- opcode_list. insert ( "OP_7" , 0x57 ) ;
22- opcode_list. insert ( "OP_8" , 0x58 ) ;
23- opcode_list. insert ( "OP_9" , 0x59 ) ;
24- opcode_list. insert ( "OP_10" , 0x5a ) ;
25- opcode_list. insert ( "OP_11" , 0x5b ) ;
26- opcode_list. insert ( "OP_12" , 0x5c ) ;
27- opcode_list. insert ( "OP_13" , 0x5d ) ;
28- opcode_list. insert ( "OP_14" , 0x5e ) ;
29- opcode_list. insert ( "OP_15" , 0x5f ) ;
30- opcode_list. insert ( "OP_16" , 0x60 ) ;
31- opcode_list. insert ( "OP_NOP" , 0x61 ) ;
32- opcode_list. insert ( "OP_DUP" , 0x76 ) ;
33- opcode_list. insert ( "OP_IF" , 0x63 ) ;
34- opcode_list. insert ( "OP_NOTIF" , 0x64 ) ;
35- opcode_list. insert ( "OP_ELSE" , 0x67 ) ;
36- opcode_list. insert ( "OP_ENDIF" , 0x68 ) ;
14+ opcode_list. insert ( "OP_0" , 0x00 ) ;
15+ opcode_list. insert ( "OP_1NEGATE" , 0x4f ) ;
16+ opcode_list. insert ( "OP_1" , 0x51 ) ;
17+ opcode_list. insert ( "OP_2" , 0x52 ) ;
18+ opcode_list. insert ( "OP_3" , 0x53 ) ;
19+ opcode_list. insert ( "OP_4" , 0x54 ) ;
20+ opcode_list. insert ( "OP_5" , 0x55 ) ;
21+ opcode_list. insert ( "OP_6" , 0x56 ) ;
22+ opcode_list. insert ( "OP_7" , 0x57 ) ;
23+ opcode_list. insert ( "OP_8" , 0x58 ) ;
24+ opcode_list. insert ( "OP_9" , 0x59 ) ;
25+ opcode_list. insert ( "OP_10" , 0x5a ) ;
26+ opcode_list. insert ( "OP_11" , 0x5b ) ;
27+ opcode_list. insert ( "OP_12" , 0x5c ) ;
28+ opcode_list. insert ( "OP_13" , 0x5d ) ;
29+ opcode_list. insert ( "OP_14" , 0x5e ) ;
30+ opcode_list. insert ( "OP_15" , 0x5f ) ;
31+ opcode_list. insert ( "OP_16" , 0x60 ) ;
32+ opcode_list. insert ( "OP_NOP" , 0x61 ) ;
33+ opcode_list. insert ( "OP_DUP" , 0x76 ) ;
34+ opcode_list. insert ( "OP_IF" , 0x63 ) ;
35+ opcode_list. insert ( "OP_NOTIF" , 0x64 ) ;
36+ opcode_list. insert ( "OP_ELSE" , 0x67 ) ;
37+ opcode_list. insert ( "OP_ENDIF" , 0x68 ) ;
3738
3839 let mut opcode_alias_list: BiMap < & ' static str , & ' static str > = BiMap :: new ( ) ;
3940 opcode_alias_list. insert ( "OP_FALSE" , "OP_0" ) ;
@@ -124,28 +125,29 @@ impl<'borrow_code_lifetime> VM<'borrow_code_lifetime> {
124125 fn step ( & mut self ) -> i32 {
125126
126127 let compiler = Compiler :: new ( ) ;
127- if self . codes [ self . pc ] == compiler. compile_single ( "OP_0" ) { self . op_pushnumber ( 0 ) ; }
128- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_1" ) { self . op_pushnumber ( 1 ) ; }
129- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_2" ) { self . op_pushnumber ( 2 ) ; }
130- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_3" ) { self . op_pushnumber ( 3 ) ; }
131- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_4" ) { self . op_pushnumber ( 4 ) ; }
132- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_5" ) { self . op_pushnumber ( 5 ) ; }
133- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_6" ) { self . op_pushnumber ( 6 ) ; }
134- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_7" ) { self . op_pushnumber ( 7 ) ; }
135- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_8" ) { self . op_pushnumber ( 8 ) ; }
136- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_9" ) { self . op_pushnumber ( 9 ) ; }
137- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_10" ) { self . op_pushnumber ( 10 ) ; }
138- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_11" ) { self . op_pushnumber ( 11 ) ; }
139- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_12" ) { self . op_pushnumber ( 12 ) ; }
140- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_13" ) { self . op_pushnumber ( 13 ) ; }
141- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_14" ) { self . op_pushnumber ( 14 ) ; }
142- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_15" ) { self . op_pushnumber ( 15 ) ; }
143- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_16" ) { self . op_pushnumber ( 16 ) ; }
144- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_NOP" ) { self . op_nop ( ) ; }
145- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_DUP" ) { self . op_dup ( ) ; }
146- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_IF" ) { self . op_if ( ) ; }
147- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_ENDIF" ) { return 1 ; }
148- else if self . codes [ self . pc ] == compiler. compile_single ( "OP_ELSE" ) { return 1 ; }
128+ if self . codes [ self . pc ] == compiler. compile_single ( "OP_0" ) { self . op_pushnumber ( 0 ) ; }
129+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_1NEGATE" ) { self . op_pushnumber ( -1 ) ; }
130+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_1" ) { self . op_pushnumber ( 1 ) ; }
131+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_2" ) { self . op_pushnumber ( 2 ) ; }
132+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_3" ) { self . op_pushnumber ( 3 ) ; }
133+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_4" ) { self . op_pushnumber ( 4 ) ; }
134+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_5" ) { self . op_pushnumber ( 5 ) ; }
135+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_6" ) { self . op_pushnumber ( 6 ) ; }
136+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_7" ) { self . op_pushnumber ( 7 ) ; }
137+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_8" ) { self . op_pushnumber ( 8 ) ; }
138+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_9" ) { self . op_pushnumber ( 9 ) ; }
139+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_10" ) { self . op_pushnumber ( 10 ) ; }
140+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_11" ) { self . op_pushnumber ( 11 ) ; }
141+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_12" ) { self . op_pushnumber ( 12 ) ; }
142+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_13" ) { self . op_pushnumber ( 13 ) ; }
143+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_14" ) { self . op_pushnumber ( 14 ) ; }
144+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_15" ) { self . op_pushnumber ( 15 ) ; }
145+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_16" ) { self . op_pushnumber ( 16 ) ; }
146+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_NOP" ) { self . op_nop ( ) ; }
147+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_DUP" ) { self . op_dup ( ) ; }
148+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_IF" ) { self . op_if ( ) ; }
149+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_ENDIF" ) { return 1 ; }
150+ else if self . codes [ self . pc ] == compiler. compile_single ( "OP_ELSE" ) { return 1 ; }
149151 else { panic ! ( "[VM] The opcode {:#x} is not implemented yet," , self . codes[ self . pc] ) ; }
150152
151153 return 0 ;
@@ -226,12 +228,11 @@ fn main() {
226228 "OP_ELSE" ,
227229 "OP_4" ,
228230 "OP_ENDIF" ,
229- "OP_5 " ] ) ;
231+ "OP_1NEGATE " ] ) ;
230232
231233 let mut stack: Vec < i32 > = vec ! [ ] ;
232234 let mut vm = VM :: new ( & bytecode, & mut stack, 0 ) ;
233235 vm. dump ( ) ;
234236 vm. run ( ) ;
235237
236- //vm.dump();
237238}
0 commit comments