Content deleted Content added
Data structures |
m Bot: http → https |
||
(31 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
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 21 ⟶ 20:
5 <@> say vK
6 <@> leave[1 ref] vKP/REFC
</syntaxhighlight>
Some opcodes (entereval, dofile, require) call Perl compiler functions which in turn generate
===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 in the ''padlist''.
===Data structures===
Perl VM data structures are represented internally by [[typedef
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=
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'').▼
====data types====
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.▼
▲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''),
▲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
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'').▼
▲Other typedefs are Glob Value (''GV'') which
====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====
▲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>.
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 52 ⟶ 80:
==External links==
*[
*[
*[
{{Perl}}
{{DEFAULTSORT:Perl Virtual Machine}}
[[Category:
[[Category:Stack-based virtual machines]]
|