Back to Fluent Bit

The new bytecode

lib/nghttp2-1.65.0/third-party/mruby/doc/internal/opcode.md

5.0.411.4 KB
Original Source

The new bytecode

We will reimplement the VM to use 8bit instruction code. By bytecode, we mean real byte code. The whole purpose is reducing the memory consumption of mruby VM.

Instructions

Instructions are bytes. There can be 256 instructions. Currently, we have 106 instructions. Instructions can take 0 to 3 operands.

operands

The size of operands can be either 8bits, 16bits or 24bits. In the table.1 below, the second field describes the size of operands.

  • B: 8bit
  • S: 16bit
  • W: 24bit

If the first and second operands are of type B (8bits), they may be extended to 16bits by the operand extension instruction immediately preceding them. See also OP_EXT1, OP_EXT2 and OP_EXT3.

table.1 Instruction Table

Instruction NameOperand typeSemantics
OP_NOP-no operation
OP_MOVEBBR(a) = R(b)
OP_LOADLBBR(a) = Pool(b)
OP_LOADIBBR(a) = mrb_int(b)
OP_LOADINEGBBR(a) = mrb_int(-b)
OP_LOADI__1BR(a) = mrb_int(-1)
OP_LOADI_0BR(a) = mrb_int(0)
OP_LOADI_1BR(a) = mrb_int(1)
OP_LOADI_2BR(a) = mrb_int(2)
OP_LOADI_3BR(a) = mrb_int(3)
OP_LOADI_4BR(a) = mrb_int(4)
OP_LOADI_5BR(a) = mrb_int(5)
OP_LOADI_6BR(a) = mrb_int(6)
OP_LOADI_7BR(a) = mrb_int(7)
OP_LOADI16BSR(a) = mrb_int(b)
OP_LOADI32BSSR(a) = mrb_int((b<<16)+c)
OP_LOADSYMBBR(a) = Syms(b)
OP_LOADNILBR(a) = nil
OP_LOADSELFBR(a) = self
OP_LOADTBR(a) = true
OP_LOADFBR(a) = false
OP_GETGVBBR(a) = getglobal(Syms(b))
OP_SETGVBBsetglobal(Syms(b), R(a))
OP_GETSVBBR(a) = Special[Syms(b)]
OP_SETSVBBSpecial[Syms(b)] = R(a)
OP_GETIVBBR(a) = ivget(Syms(b))
OP_SETIVBBivset(Syms(b),R(a))
OP_GETCVBBR(a) = cvget(Syms(b))
OP_SETCVBBcvset(Syms(b),R(a))
OP_GETCONSTBBR(a) = constget(Syms(b))
OP_SETCONSTBBconstset(Syms(b),R(a))
OP_GETMCNSTBBR(a) = R(a)::Syms(b)
OP_SETMCNSTBBR(a+1)::Syms(b) = R(a)
OP_GETUPVARBBBR(a) = uvget(b,c)
OP_SETUPVARBBBuvset(b,c,R(a))
OP_GETIDXBR(a) = R(a)[R(a+1)]
OP_SETIDXBR(a)[R(a+1)] = R(a+2)
OP_JMPSpc+=a
OP_JMPIFBSif R(a) pc+=b
OP_JMPNOTBSif !R(a) pc+=b
OP_JMPNILBSif R(a)==nil pc+=b
OP_JMPUWSunwind_and_jump_to(a)
OP_EXCEPTBR(a) = exc
OP_RESCUEBBR(b) = R(a).isa?(R(b))
OP_RAISEIFBraise(R(a)) if R(a)
OP_SSENDBBBR(a) = self.send(Syms(b),R(a+1)..,R(a+n+1):R(a+n+2)..) (c=n|k<<4)
OP_SSENDBBBBR(a) = self.send(Syms(b),R(a+1)..,R(a+n+1):R(a+n+2)..,&R(a+n+2k+1))
OP_SENDBBBR(a) = R(a).send(Syms(b),R(a+1)..,R(a+n+1):R(a+n+2)..) (c=n|k<<4)
OP_SENDBBBBR(a) = R(a).send(Syms(b),R(a+1)..,R(a+n+1):R(a+n+2)..,&R(a+n+2k+1))
OP_CALL-self.call(*, **, &) (But overlay the current call frame; tailcall)
OP_SUPERBBR(a) = super(R(a+1),... ,R(a+b+1))
OP_ARGARYBSR(a) = argument array (16=m5:r1:m5:d1:lv4)
OP_ENTERWarg setup according to flags (23=m5:o5:r1:m5:k5:d1:b1)
OP_KEY_PBBR(a) = kdict.key?(Syms(b))
OP_KEYEND-raise unless kdict.empty?
OP_KARGBBR(a) = kdict[Syms(b)]; kdict.delete(Syms(b))
OP_RETURNBreturn R(a) (normal)
OP_RETURN_BLKBreturn R(a) (in-block return)
OP_BREAKBbreak R(a)
OP_BLKPUSHBSR(a) = block (16=m5:r1:m5:d1:lv4)
OP_ADDBR(a) = R(a)+R(a+1)
OP_ADDIBBR(a) = R(a)+mrb_int(b)
OP_SUBBR(a) = R(a)-R(a+1)
OP_SUBIBBR(a) = R(a)-mrb_int(b)
OP_MULBR(a) = R(a)*R(a+1)
OP_DIVBR(a) = R(a)/R(a+1)
OP_EQBR(a) = R(a)==R(a+1)
OP_LTBR(a) = R(a)<R(a+1)
OP_LEBR(a) = R(a)<=R(a+1)
OP_GTBR(a) = R(a)>R(a+1)
OP_GEBR(a) = R(a)>=R(a+1)
OP_ARRAYBBR(a) = ary_new(R(a),R(a+1)..R(a+b))
OP_ARRAY2BBBR(a) = ary_new(R(b),R(b+1)..R(b+c))
OP_ARYCATBary_cat(R(a),R(a+1))
OP_ARYPUSHBBary_push(R(a),R(a+1)..R(a+b))
OP_ARYSPLATBR(a) = ary_splat(R(a))
OP_AREFBBBR(a) = R(b)[c]
OP_ASETBBBR(b)[c] = R(a)
OP_APOSTBBB*R(a),R(a+1)..R(a+c) = R(a)[b..]
OP_INTERNBR(a) = intern(R(a))
OP_SYMBOLBBR(a) = intern(Pool(b))
OP_STRINGBBR(a) = str_dup(Pool(b))
OP_STRCATBstr_cat(R(a),R(a+1))
OP_HASHBBR(a) = hash_new(R(a),R(a+1)..R(a+b*2-1))
OP_HASHADDBBhash_push(R(a),R(a+1)..R(a+b*2))
OP_HASHCATBR(a) = hash_cat(R(a),R(a+1))
OP_LAMBDABBR(a) = lambda(Irep(b),OP_L_LAMBDA)
OP_BLOCKBBR(a) = lambda(Irep(b),OP_L_BLOCK)
OP_METHODBBR(a) = lambda(Irep(b),OP_L_METHOD)
OP_RANGE_INCBR(a) = range_new(R(a),R(a+1),FALSE)
OP_RANGE_EXCBR(a) = range_new(R(a),R(a+1),TRUE)
OP_OCLASSBR(a) = ::Object
OP_CLASSBBR(a) = newclass(R(a),Syms(b),R(a+1))
OP_MODULEBBR(a) = newmodule(R(a),Syms(b))
OP_EXECBBR(a) = blockexec(R(a),Irep(b))
OP_DEFBBR(a).newmethod(Syms(b),R(a+1)); R(a) = Syms(b)
OP_ALIASBBalias_method(target_class,Syms(a),Syms(b))
OP_UNDEFBundef_method(target_class,Syms(a))
OP_SCLASSBR(a) = R(a).singleton_class
OP_TCLASSBR(a) = target_class
OP_DEBUGBBBprint a,b,c
OP_ERRBraise(LocalJumpError, Pool(a))
OP_EXT1-make 1st operand (a) 16bit
OP_EXT2-make 2nd operand (b) 16bit
OP_EXT3-make 1st and 2nd operands 16bit
OP_STOP-stop VM