HOME

TheInfoList



OR:

The Perl virtual machine is a
stack-based Stack-oriented programming is a programming paradigm that relies on one or more stacks to manipulate data and/or pass parameters. Programming constructs in other programming languages need to be modified for use in a stack-oriented system. Most ...
process virtual machine In computing, a virtual machine (VM) is the virtualization or emulation of a computer system. Virtual machines are based on computer architectures and provide the functionality of a physical computer. Their implementations may involve special ...
implemented as an
opcode In computing, an opcode (abbreviated from operation code) is an enumerated value that specifies the operation to be performed. Opcodes are employed in hardware devices such as arithmetic logic units (ALUs), central processing units (CPUs), and ...
s
interpreter Interpreting is translation from a spoken or signed language into another language, usually in real time to facilitate live communication. It is distinguished from the translation of a written text, which can be more deliberative and make use o ...
which runs previously compiled programs written in the
Perl Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language". Perl was developed ...
language. The opcodes interpreter is a part of the Perl interpreter, which also contains a
compiler In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
( lexer,
parser Parsing, syntax analysis, or syntactic analysis is a process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar by breaking it into parts. The term '' ...
and optimizer) in one executable file, commonly /usr/bin/perl on various
Unix-like A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
systems or perl.exe on
Microsoft Windows Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
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 typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (''alias'') for another data type, but does not create a new type, except in the obscure case of a qualified typedef of ...
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
decompile A decompiler is a computer program that translates an executable file back into high-level source code. Unlike a compiler, which converts high-level code into machine code, a decompiler performs the reverse process. While disassemblers translate e ...
to high-level source code. Perl's opcodes interpreter is implemented as a tree walker which travels the opcode tree in execution 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 a Perl program is hidden from the end user, but it can be exposed with the B Perl module or other specialized modules, as the B::Concise Perl module. An example of a simple compiled
Hello world Hello World may refer to: * "Hello, World!" program, a computer program that outputs or displays the message "Hello, World!" Music * "Hello World!" (composition), song by the Iamus computer * "Hello World" (Tremeloes song), 1969 * "Hello World" ...
program dumped in execute order (with the B::Concise Perl module): $ perl -MO=Concise,-exec -E 'say "Hello, world!"' 1 <0> enter 2 <;> nextstate(main 46 -e:1) v:%,{ 3 <0> pushmark s 4 <$> const V "Hello, world!"s 5 <@> say vK 6 <@> leave refvKP/REFC 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 The structure of the Perl programming language encompasses both the syntactical rules of the language and the general ways in which programs are organized. Perl's design philosophy is expressed in the commonly cited motto "there's more than one way ...
. 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 typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (''alias'') for another data type, but does not create a new type, except in the obscure case of a qualified typedef of ...
s. The internal data structures can be examined with the B Perl module or other specialized tools like the Devel::Peek Perl module.


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 count In computer science, reference counting is a programming technique of storing the number of Reference (computer science), references, Pointer (computer programming), pointers, or Handle (computing), handles to a resource, such as an object, a bl ...
-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 A regular expression (shortened as regex or regexp), sometimes referred to as rational expression, is a sequence of characters that specifies a match pattern in text. Usually such patterns are used by string-searching algorithms for "find" ...
(''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 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 standardization for the Perl language and Perl virtual machine. The internal API is considered non-stable and changes from version to version. The Perl virtual machine is tied closely to the compiler. The most known and most stable implementation is the B::C Perl module 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 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 Application virtualization software refers to both application virtual machines and software responsible for implementing them. Application virtual machines are typically used to allow application bytecode to run portably on many different comput ...


References


External links


The Perl internals: running stageThe "B" op tree.
{{DEFAULTSORT:Perl Virtual Machine Perl Stack-based virtual machines