Perl virtual machine: Difference between revisions

Content deleted Content added
Topbanana (talk | contribs)
m Link repair: Category:Free software articles needing expert attention -> Category:Free Software articles needing expert attention - You can help!
m Bot: http → https
 
(19 intermediate revisions by 16 users not shown)
Line 1:
The '''Perl virtual machine''' is a [[Stack machine|stack-based]] [[Application virtual machine|process virtual machine]] implemented as an [[opcode]]s [[Interpreter (computing)|interpreter]] which runs previously compiled programs written in the [[Perl]] programslanguage. The opcodes interpreter is a part of the Perl interpreter, which also contains a [[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.
{{Expert-subject|Free Software|date=March 2011}}
The '''Perl virtual machine''' is a [[Stack machine|stack-based]] [[Application virtual machine|process virtual machine]] implemented as an [[opcode]]s [[Interpreter (computing)|interpreter]] which runs previously compiled [[Perl]] programs. The opcodes interpreter is a part of the Perl interpreter, which also contains a [[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==
 
===Opcodes===
The Perl compiler outputs a compiled program into memory as an internal structure which can be represented as a tree graph in which each node represents an opcode. Opcodes are represented internally by [[typedef]]s. Each opcode has ''next'' / ''other'' and ''first'' / ''sibling'' pointers, so the opcode tree can be drawn as a 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 source code, so it is possible to [[Decompiler|decompile]] to high-level source code.<ref>{{cite web | url=httphttps://perldoc.perl.org/B/Deparse.html | title=B::Deparse - Perl compiler backend to produce perl code}}</ref>
 
Perl's opcodes interpreter is implemented as a tree walker which travels bythe opcode tree in executeexecution order from the start node, following the ''next'' or ''other'' pointers. Each opcode has a function pointer to a pp_''opname'' function, i.e. the ''say'' opcode calls the ''pp_say'' function of internal Perl API.
 
The phase of compiling thea Perl program is hidden forfrom the end user, but it can be exposed with the B Perl module<ref name="B">{{cite web | url=httphttps://search.cpanmetacpan.org/perldoc?module/B | title=B - The Perl Compiler Backend}}</ref> or other specialized modules, likeas the B::Concise Perl module.<ref>{{cite web | url=httphttps://perldoc.perl.org/B/Concise.html | title=B::Concise - Walk Perl syntax tree, printing concise info about ops}}</ref>.
 
An example of compileda simple compiled [["Hello, World!" program|Hello world]] program withdumped ain helpexecute oforder (with the B::Concise Perl module, dumped in execute order):
 
<sourcesyntaxhighlight lang="bashconsole">
$ perl -MO=Concise,-exec -E 'say "Hello, world!"'
1 <0> enter
2 <;> nextstate(main 46 -e:1) v:%,{
3 <0> pushmark s
Line 20:
5 <@> say vK
6 <@> leave[1 ref] vKP/REFC
</syntaxhighlight>
</source>
 
Some opcodes (entereval, dofile, require) call Perl compiler functions which in turn generate other opcodes in the same Perl virtual machine.
 
===Variables===
Perl variables can be global, dynamic (''local'' keyword), or lexical (''my'' and ''our'' keywords).
 
Global variables are accessible via the stash and the corresponding [[typeglob]].
 
Local variables are the same as global variables but a special opcode is generated to save its value on the ''savestack'' and restore it later.
 
Lexical variables are stored onin the ''padlist''.
 
===Data structures===
Perl VM data structures are represented internally by [[typedef]]s.
 
The internal data structures can be examined with the B Perl module<ref name="B" /> or other specialized tools like the Devel::Peek Perl module.<ref>{{cite web | url=httphttps://search.cpanmetacpan.org/perldoc?module/Devel::Peek | title=Devel::Peek - A data debugging tool for the XS programmer}}</ref>.
 
====data types====
Perl has three typedefs that handle Perl's three main data types: Scalar Value (''SV''), Array Value (''AV''), Hash Value (''HV''). Perl uses a special typedef for the simple signed integer type (''IV''), an unsigned integerintegers (''IVUV''), a floating point numbernumbers (''NV'') and stringstrings (''PV'').
 
Perl uses a [[Reference counting|reference count]]-driven garbage collection mechanism. SVs, AVs, or HVs start their life with a reference count of 1. If the reference count of a data value ever drops to 0, then it will be destroyed and its memory is made available for reuse.
 
Other typedefs are Glob Value (''GV'') which containscontain named references to the various objects, Code Value (''CV'') which containscontain a reference to a Perl subroutine, I/O Handler (''IO''), a reference to [[regular expression]] (''REGEXP''; ''RV'' in Perl before 5.11), reference to compiled format for output record (''FM'') and simple reference which is a special type of scalar that point to other data types (''RV'').
 
====stash====
Line 52:
 
===Stacks===
Perl has a number of stacks to store things it's currentlyis working on.
 
====Argument stack====
Line 61:
 
====Save stack====
This stack is used for saving and restoring a valuevalues of dynamically [[Scope (programming)|scoped]] local variablevariables.
 
====Scope stack====
This stack stores information about the actual scope and it is used only for debugging purposes.
 
===Other implementations===
There is no standarizationstandardization for the Perl language and Perl virtual machine. The internal API should beis considered as non-stable and changes from version to version. The Perl virtual machine is tied closely to the compiler. These things make very hard to reimplement Perl virtual machine.
 
The most known and most stable implementation is athe B::C Perl module<ref>{{cite web | url=httphttps://search.cpanmetacpan.org/perldoc?module/B::C | title=B::C - Perl compiler's C backend}}</ref> which translates opcodes tree to a representation in the C programming language and adds its own tree walker.
 
Another implementation is an Acme::Perl::VM Perl module<ref>{{cite web | url=httphttps://search.cpanmetacpan.org/perldoc?module/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==
Line 80:
 
==External links==
*[httphttps://perldoc.perl.org/perlhack.html#Running The Perl internals: running stage]
*[httphttps://perldoc.perl.org/perlguts.html Introduction to the Perl API]
*[httphttps://search.cpanmetacpan.org/perldoc?module/RURBAN/B-C-1.42/perloptree.pod The "B" op tree.]
 
{{Perl}}
Line 88:
{{DEFAULTSORT:Perl Virtual Machine}}
[[Category:Perl]]
[[Category:VirtualStack-based virtual machines]]