Loading Bytecode
In addition to running
Parrot
bytecode on the command line, you can also load precompiled bytecode
directly into your PASM source file. The
load_bytecode opcode takes a single argument: the
name of the bytecode file to load. So, if you create a file named
file.pasm containing a single subroutine:
# file.pasm .pcc_sub _sub2: # .pcc_sub stores a global sub print "in sub2\n" invoke P1
and compile it to bytecode using the -o
command-line switch:
$ parrot -o file.pbc file.pasm
You can then load the compiled bytecode into
main.pasm and directly call the subroutine
defined in file.pasm:
# main.pasm _main: load_bytecode "file.pbc" # compiled file.pasm find_global P0, "_sub2" invokecc end
The load_bytecode opcode also works with source
files, as long as Parrot has a compiler registered for that type of
file:
# main2.pasm _main: load_bytecode "file.pasm" # PASM source code find_global P0, "_sub2" invokecc end
Subroutines marked with @LOAD run as soon as
they’re loaded (before
load_bytecode returns), rather than waiting to be
called. A subroutine marked with @MAIN will always
run first, no matter what name you give it or where you define it in
the file.
# file3.pasm .pcc_sub @LOAD _entry: # mark the sub as to be run print "file3\n" invoke P1 # return # main3.pasm _first: # first is never invoked print "never\n" invoke P1 .pcc_sub @MAIN _main: # because _main is marked as the print "main\n" # MAIN entry of program execution load_bytecode "file3.pasm" print "back\n" end
This example ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access