Perl virtual machine: Difference between revisions

Content deleted Content added
D3xter (talk | contribs)
No edit summary
D3xter (talk | contribs)
More about implementation
Line 1:
{{Expert-subject|Free Software}}
The '''Perl virtual machine''' is a [[Stack machine|stack-based]] [[Application virtual machine|process virtual machine]] implemented as [[opcode|opcodes]] [[Interpreter (computing)|interpreter]] which runs a previously compiled [[Perl]] programs. ItOpcodes interpreter is is a part of Perl interpreter which contains also [[Compiler|compiler]] ([[Lexical analysis|lexer]], [[Parsing|parser]] and [[Compiler optimization|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 [[Decompiler|decompile]] to high-level source code.<ref>{{cite web | url=http://perldoc.perl.org/B/Deparse.html | title=B::Deparse - Perl compiler backend to produce perl code}}</ref>
 
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.<ref>{{cite web | url=http://perldoc.perl.org/B/Concise.html | title=B::Concise - Walk Perl syntax tree, printing concise info about ops}}</ref>
 
An example of compiled simple [[Hello world]] program with a help of B::Concise Perl module, dumped in execute order:
 
<source lang="bash">
$ 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
</source>
 
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<ref>{{cite web | url=http://search.cpan.org/perldoc?B::C | title=B::C - Perl compiler's C backend}}</ref> which translates opcodes tree to representation in C language and adds own tree walker.
 
Another implementation is an Acme::Perl::VM Perl module<ref>{{cite web | url=http://search.cpan.org/perldoc?Acme::Perl::VM | title=Acme::Perl::VM - A Perl5 Virtual Machine in Pure Perl (APVM)}}</ref> which is an implementation coded in Perl language only but it is still tied with original Perl virtual machine via B:: modules.
 
==See also==
* [[Comparison of application virtual machines]]
 
==References==
{{Reflist|2}}
 
==External links==
*[http://perldoc.perl.org/perlhack.html#Running The Perl internals: running stage]
*[http://perldoc.perl.org/perlguts.html Introduction to the Perl API]
*[http://search.cpan.org/perldoc?perloptree The "B" op tree.]
 
{{Perl}}