Content deleted Content added
More about implementation |
m Bot: http → https |
||
(32 intermediate revisions by 25 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
▲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. Opcodes 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==
===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
Perl's opcodes interpreter is implemented as a tree walker which travels
The phase of compiling
An example of
<
$ 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>
Some opcodes (entereval, dofile, require) call Perl compiler functions which in turn generate
===
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]].
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.▼
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 in 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=https://metacpan.org/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''), unsigned integers (''UV''), floating point numbers (''NV'') and strings (''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 drops to 0, then it will be destroyed and its memory is made available for reuse.
Other typedefs are Glob Value (''GV'') which contain named references to various objects, Code Value (''CV'') which contain 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====
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'').
====padlist====
Special Array Value is ''padlist'' which is an array of array. Its 0th element to an AV containing all lexical variable names (with prefix symbols) used within that subroutine. The padlist's first element points to a scratchpad AV, whose elements contain the values corresponding to the lexical variables named in the 0th row. Another elements of padlist are created when the subroutine recurses or new thread is created.
===Stacks===
Perl has a number of stacks to store things it is working on.
====Argument stack====
Arguments are passed to opcode and returned from opcode using the argument stack. The typical way to handle arguments is to pop them off the stack, and then push the result back onto the stack.
====Mark stack====
This stack saves bookmarks to locations in the argument stack usable by each function so the functions doesn't necessarily get the whole argument stack to itself.
====Save stack====
This stack is used for saving and restoring values of dynamically [[Scope (programming)|scoped]] local variables.
====Scope stack====
This stack stores information about the actual scope and it is used only for debugging purposes.
===Other implementations===
▲There is no
The most known and most stable implementation is
Another implementation is an Acme::Perl::VM Perl module<ref>{{cite web | url=
==See also==
Line 39 ⟶ 80:
==External links==
*[
*[
*[
{{Perl}}
{{DEFAULTSORT:Perl Virtual Machine}}
[[Category:
[[Category:Stack-based virtual machines]]
|