<- r"(
code LDCONST 1
LDCONST 2
ADD
RETURN
)"
<- asm(code)
bc bc
<bytecode: 0x13c77be80>
eval(bc)
[1] 3
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()
.
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.
<- r"(
code LDCONST 1
LDCONST 2
ADD
RETURN
)"
<- asm(code)
bc bc
<bytecode: 0x13c77be80>
eval(bc)
[1] 3
<- r"(
code LDCONST 1
LDCONST 2
ADD
RETURN
)"
<- parse_code(code)
bcdf 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
<- compile_bcdf(bcdf)
bc bc
<bytecode: 0x13ccbdde8>
eval(bc)
[1] 3