This article needs attention from an expert in Free Software. Please add a reason or a talk parameter to this template to explain the issue with the article. |
The Perl virtual machine is a stack-based process virtual machine implemented as opcodes interpreter which runs a previously compiled Perl programs. Opcodes interpreter is is a part of Perl interpreter which contains also compiler (lexer, parser and optimizer) in one executable file, commonly /usr/bin/perl on various Unix-like systems or perl.exe on Microsoft Windows systems.
Implementation
The Perl compiler outputs a compiled program into memory as an internal structure which can be represented as tree graph which each node represents an opcode. Each opcode has next node and sibling node, so the opcode tree can be drawed as basic OP tree starting from root node or as flat OP list in the order they would normally execute from start node. Opcodes tree can be mapped to the actual source code, so it is possible to decompile to high-level source code.[1]
Perl's opcodes interpreter is implemented as tree walker which travels by opcode tree in execute order from start node. Each opcode is assigned with internal pp_opname function, i.e. say opcode calls pp_say function of internal Perl API.
The phase of compiling the Perl program is hidden for end user, but it can be exposed with various B:: Perl modules which provides an access to internal API of compilator and opcode walker.[2]
An example of compiled simple Hello world program with a help of B::Concise Perl module, dumped in execute order:
$ perl -MO=Concise,-exec -E 'say "Hello, world!"'
1 <0> enter
2 <;> nextstate(main 46 -e:1) v:%,{
3 <0> pushmark s
4 <$> const[PV "Hello, world!"] s
5 <@> say vK
6 <@> leave[1 ref] vKP/REFC
Some opcodes (entereval, dofile, require) call Perl compiler functions which generate another opcodes in the same Perl virtual machine.
Other implementations
There is no standarization for Perl language and Perl virtual machine. The internal API should be considered as non-stable and changes from version to version. The Perl virtual machine is tied closely to compiler. These things make very hard to reimplement Perl virtual machine.
The most known and most stable implementation is a B::C Perl module[3] which translates opcodes tree to representation in C language and adds own tree walker.
Another implementation is an Acme::Perl::VM Perl module[4] which is an implementation coded in Perl language only but it is still tied with original Perl virtual machine via B:: modules.