Perl virtual machine: Difference between revisions

Content deleted Content added
D3xter (talk | contribs)
More about implementation
Data structures
Line 4:
==Implementation==
 
===Opcodes===
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. Opcodes are represented internaly by [[typedef|typedefs]]. 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 variousB Perl module<ref name="B">{{cite web | url=http:://search.cpan.org/perldoc?B | title=B - The Perl Compiler Backend}}</ref> or other specialized modules which provides an access to internal API of compilator and opcode walker. like B::Concise Perl module<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:
Line 24 ⟶ 25:
Some opcodes (entereval, dofile, require) call Perl compiler functions which generate another opcodes in the same Perl virtual machine.
 
===OtherData implementationsstructures===
Perl VM data structures are represented internally by [[typedef|typedefs]].
 
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 simple signed integer type (''IV''), an unsigned integer (''IV''), a floating point number (''NV'') and string (''PV'').
 
Perl uses a 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 made available for reuse.
 
Other typedefs are Glob Value (''GV'') which contains references to the various objects, Code Value (''CV'') which contains a reference to Perl subroutine, I/O Handler (''IO''), a reference to [[Regular expression|regular expression]] (''REGEXP''; ''RV'' in Perl before 5.11) and a reference to compiled format for output record (''FM'').
 
Special Hash Value is ''stash'', a hash that contains all variables that are defined within a package. Each value in this hash table is a Glob Value (''GV'').
 
The internal data structures can be examined with B Perl module<ref name="B" /> or other specialized tools like Devel::Peek Perl module<ref>{{cite web | url=http://search.cpan.org/perldoc?Devel::Peek | title=Devel::Peek - A data debugging tool for the XS programmer}}</ref>.
 
===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.