4  Assembling bytecode

After exploring the disassembly process (Section 3), it is possible to take the output and re-compile it into an executable bytecode object.

Compiling bytecode assembly to an R bytecode object is achieved using rbytecode::asm().

It is also possible to compile bytecode in two steps using parse_code() to produce a bcdf, and then compile the bcdf data.frame to a bytecode object using compile_bcdf().

4.1 Compiling bytecode - simple example

asm() accepts a single string of bytecode instructions (one instruction per line).

The input is parsed and compiled into bytecode using (internal) parts of base R’s {compiler} package.

code <- r"(
LDCONST 1
LDCONST 2
ADD
RETURN
)"

bc <- asm(code)
bc
<bytecode: 0x13c77be80>
eval(bc)
[1] 3

4.2 Compiling bytecode - two-step process

code <- r"(
LDCONST 1
LDCONST 2
ADD
RETURN
)"

bcdf <- parse_code(code)
bcdf
  line depth pc opcode      op args expr
1    2     0  1     16 LDCONST    1 NULL
2    3     0  3     16 LDCONST    2 NULL
3    4     0  5     44     ADD NULL NULL
4    5     0  7      1  RETURN NULL NULL
bc <- compile_bcdf(bcdf)
bc
<bytecode: 0x13ccbdde8>
eval(bc)
[1] 3

4.3 Next steps

  • Read the allowed syntax for bytecode assembly Section 6
  • Explore the reference information about each of the instructions Section 9