| 1 |
# The new bytecode |
| 2 |
|
| 3 |
We will reimplement the VM to use 8bit instruction code. By |
| 4 |
bytecode, we mean real byte code. The whole purpose is |
| 5 |
reducing the memory consumption of mruby VM. |
| 6 |
|
| 7 |
# Instructions |
| 8 |
|
| 9 |
Instructions are bytes. There can be 256 instructions. Currently, we |
| 10 |
have 106 instructions. Instructions can take 0 to 3 operands. |
| 11 |
|
| 12 |
## operands |
| 13 |
|
| 14 |
The size of operands can be either 8bits, 16bits or 24bits. |
| 15 |
In the table.1 below, the second field describes the size |
| 16 |
of operands. |
| 17 |
|
| 18 |
- B: 8bit |
| 19 |
- S: 16bit |
| 20 |
- W: 24bit |
| 21 |
|
| 22 |
If the first and second operands are of type `B` (8bits), they may be |
| 23 |
extended to 16bits by the operand extension instruction immediately |
| 24 |
preceding them. |
| 25 |
See also `OP_EXT1`, `OP_EXT2` and `OP_EXT3`. |
| 26 |
|
| 27 |
## table.1 Instruction Table |
| 28 |
|
| 29 |
| Instruction Name | Operand type | Semantics | |
| 30 |
|--------------------|----------------|------------------------------------------------------------| |
| 31 |
| `OP_NOP` | `-` | `no operation` | |
| 32 |
| `OP_MOVE` | `BB` | `R(a) = R(b)` | |
| 33 |
| `OP_LOADL` | `BB` | `R(a) = Pool(b)` | |
| 34 |
| `OP_LOADI` | `BB` | `R(a) = mrb_int(b)` | |
| 35 |
| `OP_LOADINEG` | `BB` | `R(a) = mrb_int(-b)` | |
| 36 |
| `OP_LOADI__1` | `B` | `R(a) = mrb_int(-1)` | |
| 37 |
| `OP_LOADI_0` | `B` | `R(a) = mrb_int(0)` | |
| 38 |
| `OP_LOADI_1` | `B` | `R(a) = mrb_int(1)` | |
| 39 |
| `OP_LOADI_2` | `B` | `R(a) = mrb_int(2)` | |
| 40 |
| `OP_LOADI_3` | `B` | `R(a) = mrb_int(3)` | |
| 41 |
| `OP_LOADI_4` | `B` | `R(a) = mrb_int(4)` | |
| 42 |
| `OP_LOADI_5` | `B` | `R(a) = mrb_int(5)` | |
| 43 |
| `OP_LOADI_6` | `B` | `R(a) = mrb_int(6)` | |
| 44 |
| `OP_LOADI_7` | `B` | `R(a) = mrb_int(7)` | |
| 45 |
| `OP_LOADI16` | `BS` | `R(a) = mrb_int(b)` | |
| 46 |
| `OP_LOADI32` | `BSS` | `R(a) = mrb_int((b<<16)+c)` | |
| 47 |
| `OP_LOADSYM` | `BB` | `R(a) = Syms(b)` | |
| 48 |
| `OP_LOADNIL` | `B` | `R(a) = nil` | |
| 49 |
| `OP_LOADSELF` | `B` | `R(a) = self` | |
| 50 |
| `OP_LOADT` | `B` | `R(a) = true` | |
| 51 |
| `OP_LOADF` | `B` | `R(a) = false` | |
| 52 |
| `OP_GETGV` | `BB` | `R(a) = getglobal(Syms(b))` | |
| 53 |
| `OP_SETGV` | `BB` | `setglobal(Syms(b), R(a))` | |
| 54 |
| `OP_GETSV` | `BB` | `R(a) = Special[Syms(b)]` | |
| 55 |
| `OP_SETSV` | `BB` | `Special[Syms(b)] = R(a)` | |
| 56 |
| `OP_GETIV` | `BB` | `R(a) = ivget(Syms(b))` | |
| 57 |
| `OP_SETIV` | `BB` | `ivset(Syms(b),R(a))` | |
| 58 |
| `OP_GETCV` | `BB` | `R(a) = cvget(Syms(b))` | |
| 59 |
| `OP_SETCV` | `BB` | `cvset(Syms(b),R(a))` | |
| 60 |
| `OP_GETCONST` | `BB` | `R(a) = constget(Syms(b))` | |
| 61 |
| `OP_SETCONST` | `BB` | `constset(Syms(b),R(a))` | |
| 62 |
| `OP_GETMCNST` | `BB` | `R(a) = R(a)::Syms(b)` | |
| 63 |
| `OP_SETMCNST` | `BB` | `R(a+1)::Syms(b) = R(a)` | |
| 64 |
| `OP_GETUPVAR` | `BBB` | `R(a) = uvget(b,c)` | |
| 65 |
| `OP_SETUPVAR` | `BBB` | `uvset(b,c,R(a))` | |
| 66 |
| `OP_GETIDX` | `B` | `R(a) = R(a)[R(a+1)]` | |
| 67 |
| `OP_SETIDX` | `B` | `R(a)[R(a+1)] = R(a+2)` | |
| 68 |
| `OP_JMP` | `S` | `pc+=a` | |
| 69 |
| `OP_JMPIF` | `BS` | `if R(a) pc+=b` | |
| 70 |
| `OP_JMPNOT` | `BS` | `if !R(a) pc+=b` | |
| 71 |
| `OP_JMPNIL` | `BS` | `if R(a)==nil pc+=b` | |
| 72 |
| `OP_JMPUW` | `S` | `unwind_and_jump_to(a)` | |
| 73 |
| `OP_EXCEPT` | `B` | `R(a) = exc` | |
| 74 |
| `OP_RESCUE` | `BB` | `R(b) = R(a).isa?(R(b))` | |
| 75 |
| `OP_RAISEIF` | `B` | `raise(R(a)) if R(a)` | |
| 76 |
| `OP_SSEND` | `BBB` | `R(a) = self.send(Syms(b),R(a+1)..,R(a+n+1):R(a+n+2)..) (c=n\|k<<4)` | |
| 77 |
| `OP_SSENDB` | `BBB` | `R(a) = self.send(Syms(b),R(a+1)..,R(a+n+1):R(a+n+2)..,&R(a+n+2k+1))` | |
| 78 |
| `OP_SEND` | `BBB` | `R(a) = R(a).send(Syms(b),R(a+1)..,R(a+n+1):R(a+n+2)..) (c=n\|k<<4)` | |
| 79 |
| `OP_SENDB` | `BBB` | `R(a) = R(a).send(Syms(b),R(a+1)..,R(a+n+1):R(a+n+2)..,&R(a+n+2k+1))` | |
| 80 |
| `OP_CALL` | `-` | `self.call(*, **, &) (But overlay the current call frame; tailcall)` | |
| 81 |
| `OP_SUPER` | `BB` | `R(a) = super(R(a+1),... ,R(a+b+1))` | |
| 82 |
| `OP_ARGARY` | `BS` | `R(a) = argument array (16=m5:r1:m5:d1:lv4)` | |
| 83 |
| `OP_ENTER` | `W` | `arg setup according to flags (23=m5:o5:r1:m5:k5:d1:b1)` | |
| 84 |
| `OP_KEY_P` | `BB` | `R(a) = kdict.key?(Syms(b))` | |
| 85 |
| `OP_KEYEND` | `-` | `raise unless kdict.empty?` | |
| 86 |
| `OP_KARG` | `BB` | `R(a) = kdict[Syms(b)]; kdict.delete(Syms(b))` | |
| 87 |
| `OP_RETURN` | `B` | `return R(a) (normal)` | |
| 88 |
| `OP_RETURN_BLK` | `B` | `return R(a) (in-block return)` | |
| 89 |
| `OP_BREAK` | `B` | `break R(a)` | |
| 90 |
| `OP_BLKPUSH` | `BS` | `R(a) = block (16=m5:r1:m5:d1:lv4)` | |
| 91 |
| `OP_ADD` | `B` | `R(a) = R(a)+R(a+1)` | |
| 92 |
| `OP_ADDI` | `BB` | `R(a) = R(a)+mrb_int(b)` | |
| 93 |
| `OP_SUB` | `B` | `R(a) = R(a)-R(a+1)` | |
| 94 |
| `OP_SUBI` | `BB` | `R(a) = R(a)-mrb_int(b)` | |
| 95 |
| `OP_MUL` | `B` | `R(a) = R(a)*R(a+1)` | |
| 96 |
| `OP_DIV` | `B` | `R(a) = R(a)/R(a+1)` | |
| 97 |
| `OP_EQ` | `B` | `R(a) = R(a)==R(a+1)` | |
| 98 |
| `OP_LT` | `B` | `R(a) = R(a)<R(a+1)` | |
| 99 |
| `OP_LE` | `B` | `R(a) = R(a)<=R(a+1)` | |
| 100 |
| `OP_GT` | `B` | `R(a) = R(a)>R(a+1)` | |
| 101 |
| `OP_GE` | `B` | `R(a) = R(a)>=R(a+1)` | |
| 102 |
| `OP_ARRAY` | `BB` | `R(a) = ary_new(R(a),R(a+1)..R(a+b))` | |
| 103 |
| `OP_ARRAY2` | `BBB` | `R(a) = ary_new(R(b),R(b+1)..R(b+c))` | |
| 104 |
| `OP_ARYCAT` | `B` | `ary_cat(R(a),R(a+1))` | |
| 105 |
| `OP_ARYPUSH` | `BB` | `ary_push(R(a),R(a+1)..R(a+b))` | |
| 106 |
| `OP_ARYSPLAT` | `B` | `R(a) = ary_splat(R(a))` | |
| 107 |
| `OP_AREF` | `BBB` | `R(a) = R(b)[c]` | |
| 108 |
| `OP_ASET` | `BBB` | `R(b)[c] = R(a)` | |
| 109 |
| `OP_APOST` | `BBB` | `*R(a),R(a+1)..R(a+c) = R(a)[b..]` | |
| 110 |
| `OP_INTERN` | `B` | `R(a) = intern(R(a))` | |
| 111 |
| `OP_SYMBOL` | `BB` | `R(a) = intern(Pool(b))` | |
| 112 |
| `OP_STRING` | `BB` | `R(a) = str_dup(Pool(b))` | |
| 113 |
| `OP_STRCAT` | `B` | `str_cat(R(a),R(a+1))` | |
| 114 |
| `OP_HASH` | `BB` | `R(a) = hash_new(R(a),R(a+1)..R(a+b*2-1))` | |
| 115 |
| `OP_HASHADD` | `BB` | `hash_push(R(a),R(a+1)..R(a+b*2))` | |
| 116 |
| `OP_HASHCAT` | `B` | `R(a) = hash_cat(R(a),R(a+1))` | |
| 117 |
| `OP_LAMBDA` | `BB` | `R(a) = lambda(Irep(b),OP_L_LAMBDA)` | |
| 118 |
| `OP_BLOCK` | `BB` | `R(a) = lambda(Irep(b),OP_L_BLOCK)` | |
| 119 |
| `OP_METHOD` | `BB` | `R(a) = lambda(Irep(b),OP_L_METHOD)` | |
| 120 |
| `OP_RANGE_INC` | `B` | `R(a) = range_new(R(a),R(a+1),FALSE)` | |
| 121 |
| `OP_RANGE_EXC` | `B` | `R(a) = range_new(R(a),R(a+1),TRUE)` | |
| 122 |
| `OP_OCLASS` | `B` | `R(a) = ::Object` | |
| 123 |
| `OP_CLASS` | `BB` | `R(a) = newclass(R(a),Syms(b),R(a+1))` | |
| 124 |
| `OP_MODULE` | `BB` | `R(a) = newmodule(R(a),Syms(b))` | |
| 125 |
| `OP_EXEC` | `BB` | `R(a) = blockexec(R(a),Irep(b))` | |
| 126 |
| `OP_DEF` | `BB` | `R(a).newmethod(Syms(b),R(a+1)); R(a) = Syms(b)` | |
| 127 |
| `OP_ALIAS` | `BB` | `alias_method(target_class,Syms(a),Syms(b))` | |
| 128 |
| `OP_UNDEF` | `B` | `undef_method(target_class,Syms(a))` | |
| 129 |
| `OP_SCLASS` | `B` | `R(a) = R(a).singleton_class` | |
| 130 |
| `OP_TCLASS` | `B` | `R(a) = target_class` | |
| 131 |
| `OP_DEBUG` | `BBB` | `print a,b,c` | |
| 132 |
| `OP_ERR` | `B` | `raise(LocalJumpError, Pool(a))` | |
| 133 |
| `OP_EXT1` | `-` | `make 1st operand (a) 16bit` | |
| 134 |
| `OP_EXT2` | `-` | `make 2nd operand (b) 16bit` | |
| 135 |
| `OP_EXT3` | `-` | `make 1st and 2nd operands 16bit` | |
| 136 |
| `OP_STOP` | `-` | `stop VM` | |