parrot -- "one bytecode to rule them all"
DESCRIPTION
http://workshop.perl.pt/ptpw2008/TRANSCRIPT
”one bytecode to rule them all”
Nuno Carvalho <[email protected]>
Portuguese Perl Workshop 2008
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Overview
Definition
Parrot is a virtual machine designed to compile and executebytecode for dynamic languages.
Parrot is a register-based, bytecode-driven, object-oriented,dynamically typed, self-modifying, asynchronous interpreter.
initially created to run Perl6
Core Design Principles
speed
stability
abstraction
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Overview
Definition
Parrot is a virtual machine designed to compile and executebytecode for dynamic languages.
Parrot is a register-based, bytecode-driven, object-oriented,dynamically typed, self-modifying, asynchronous interpreter.
initially created to run Perl6
Core Design Principles
speed
stability
abstraction
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Insight
Software CPU
register based (four type of registers)
also uses stacks
no operands limit to opcodes
Core Data Types
integers
strings
floating-point
PMCs (Parrot Magic Cookie)
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Insight
Software CPU
register based (four type of registers)
also uses stacks
no operands limit to opcodes
Core Data Types
integers
strings
floating-point
PMCs (Parrot Magic Cookie)
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Insight
Some Interesting Features
complex object and class model system
exception handling
events (signals)
garbage collection
MMD (Multi Method Dispatching)
multiple concurrency models
unicode support
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Parrot’s Architecture
Interpretation Flow
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
PASM, PIR And PBC
PASM
Parrot Assembly
traditional low-level features
also has advanced features
lexical, global variables, objects, etc
PIR
Parrot Intermediate Representation
high level features
named variables, etc
it still isn’t a HLL
PBC
Parrot Bytecode
sequence of instructions in a binary format
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
PASM, PIR And PBC
PASM
Parrot Assembly
traditional low-level features
also has advanced features
lexical, global variables, objects, etc
PIR
Parrot Intermediate Representation
high level features
named variables, etc
it still isn’t a HLL
PBC
Parrot Bytecode
sequence of instructions in a binary format
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
PASM, PIR And PBC
PASM
Parrot Assembly
traditional low-level features
also has advanced features
lexical, global variables, objects, etc
PIR
Parrot Intermediate Representation
high level features
named variables, etc
it still isn’t a HLL
PBC
Parrot Bytecode
sequence of instructions in a binary format
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Code Samples
PASM
lt P0,10,branchset I2,P0dec P0
PIR
cost = minimum(a,b,c)matrix[i;j] = costj += 1if j <= n goto inner_cycle
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Code Samples
PASM
lt P0,10,branchset I2,P0dec P0
PIR
cost = minimum(a,b,c)matrix[i;j] = costj += 1if j <= n goto inner_cycle
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Get Your Feet Wet
Checking Out
~$ svn co https://svn.perl.org/parrot/trunk parrot
Building
~/parrot$ perl Configure.pl~/parrot$ make
Testing
~/parrot$ make test
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Get Your Feet Wet
Checking Out
~$ svn co https://svn.perl.org/parrot/trunk parrot
Building
~/parrot$ perl Configure.pl~/parrot$ make
Testing
~/parrot$ make test
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Get Your Feet Wet
Checking Out
~$ svn co https://svn.perl.org/parrot/trunk parrot
Building
~/parrot$ perl Configure.pl~/parrot$ make
Testing
~/parrot$ make test
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
’Hello world!’
hello.pir
.sub hello :mainsay ’Hello world!’
.end
linux x86
$ ./parrot hello.pirHello world!
hello.pbc
$ ./parrot --output-pbc --output=hello.pbc \hello.pir
$ ./parrot hello.pbc
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
’Hello world!’
hello.pir
.sub hello :mainsay ’Hello world!’
.end
linux x86
$ ./parrot hello.pirHello world!
hello.pbc
$ ./parrot --output-pbc --output=hello.pbc \hello.pir
$ ./parrot hello.pbc
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
’Hello world!’
hello.pir
.sub hello :mainsay ’Hello world!’
.end
linux x86
$ ./parrot hello.pirHello world!
hello.pbc
$ ./parrot --output-pbc --output=hello.pbc \hello.pir
$ ./parrot hello.pbc
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Simple Benchmark
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Parrot Compiler Toolkit
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Parrot Compiler Toolkit
What?
Set of powerfull tools and engines that are used to quickly andeasily craft compilers for Parrot.
Why?
”There’s an odd misconception in the computing world that writingcompilers is hard. This view is fueled by the fact that we don’twrite compilers very often. People used to think writing CGI codewas hard. Well, it is hard, if you do it in C without any tools.”
by Allison Randall
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Parrot Compiler Toolkit
What?
Set of powerfull tools and engines that are used to quickly andeasily craft compilers for Parrot.
Why?
”There’s an odd misconception in the computing world that writingcompilers is hard. This view is fueled by the fact that we don’twrite compilers very often. People used to think writing CGI codewas hard. Well, it is hard, if you do it in C without any tools.”
by Allison Randall
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Interpretation Flow
Four stages of PCT based compilers
1 source to parse tree
2 parse tree to PAST
3 PAST to POST
4 POST to PIR
PAST
Parrot Abstract Syntax Tree
abstract syntax tree nodes that represent common languageconstructs
POST
Parrot Opcode Syntax Tree
lower abstraction level
each node represents a instruction or label
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Interpretation Flow
Four stages of PCT based compilers
1 source to parse tree
2 parse tree to PAST
3 PAST to POST
4 POST to PIR
PAST
Parrot Abstract Syntax Tree
abstract syntax tree nodes that represent common languageconstructs
POST
Parrot Opcode Syntax Tree
lower abstraction level
each node represents a instruction or label
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Interpretation Flow
Four stages of PCT based compilers
1 source to parse tree
2 parse tree to PAST
3 PAST to POST
4 POST to PIR
PAST
Parrot Abstract Syntax Tree
abstract syntax tree nodes that represent common languageconstructs
POST
Parrot Opcode Syntax Tree
lower abstraction level
each node represents a instruction or label
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Create A New Language
Create Files
$ perl tools/dev/mk_language_shell.pl mspcreating languages/msp/config/makefiles/creating languages/msp/config/makefiles/root.increating languages/msp/msp.pir(...)$ ls languages/msp/config Makefile msp.pir src t
Build
$ cd languages/msp$ make$ make test(...)All tests successful.
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Create A New Language
Create Files
$ perl tools/dev/mk_language_shell.pl mspcreating languages/msp/config/makefiles/creating languages/msp/config/makefiles/root.increating languages/msp/msp.pir(...)$ ls languages/msp/config Makefile msp.pir src t
Build
$ cd languages/msp$ make$ make test(...)All tests successful.
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Running Your Compiler
Execute a script
$ ../../parrot msp.pbc script.msp
Run in interactive mode
$ ../../parrot msp.pbcmsp version 0.1
msp>
Default compilation trees
$ ../../parrot msp.pbc --target=parse script.msp
$ ../../parrot msp.pbc --target=past script.msp
$ ../../parrot msp.pbc --target=post script.msp
$ ../../parrot msp.pbc --target=pir script.msp
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Running Your Compiler
Execute a script
$ ../../parrot msp.pbc script.msp
Run in interactive mode
$ ../../parrot msp.pbcmsp version 0.1
msp>
Default compilation trees
$ ../../parrot msp.pbc --target=parse script.msp
$ ../../parrot msp.pbc --target=past script.msp
$ ../../parrot msp.pbc --target=post script.msp
$ ../../parrot msp.pbc --target=pir script.msp
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Running Your Compiler
Execute a script
$ ../../parrot msp.pbc script.msp
Run in interactive mode
$ ../../parrot msp.pbcmsp version 0.1
msp>
Default compilation trees
$ ../../parrot msp.pbc --target=parse script.msp
$ ../../parrot msp.pbc --target=past script.msp
$ ../../parrot msp.pbc --target=post script.msp
$ ../../parrot msp.pbc --target=pir script.msp
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Extending Your Language
grammar.pg
rule say_statement {’say’ <value> [ ’,’ <value> ]* ’;’ {*}
}
actions.pm
method say_statement($/) {my $past := PAST::Op.new( :name(’say’),
:pasttype(’call’), :node( $/ ) );for $<value> {
$past.push( $( $_ ) );}make $past;
}
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Extending Your Language
grammar.pg
rule say_statement {’say’ <value> [ ’,’ <value> ]* ’;’ {*}
}
actions.pm
method say_statement($/) {my $past := PAST::Op.new( :name(’say’),
:pasttype(’call’), :node( $/ ) );for $<value> {
$past.push( $( $_ ) );}make $past;
}
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Extending Your Language
grammar.pg
rule if_statement {’if’ <expression> ’then’ <block> ’;’ {*}
}
actions.pm
method if_statement($/) {my $cond := $( $<expression> );my $then := $( $<block> );my $past := PAST::Op.new( $cond, $then,
:pasttype(’if’),:node($/) );
make $past;}
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Extending Your Language
grammar.pg
rule if_statement {’if’ <expression> ’then’ <block> ’;’ {*}
}
actions.pm
method if_statement($/) {my $cond := $( $<expression> );my $then := $( $<block> );my $past := PAST::Op.new( $cond, $then,
:pasttype(’if’),:node($/) );
make $past;}
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Final Notes
Conclusion
Parrot is a virtual machine
Parrot Compiler Toolkit
target multiple languages (or brand new)
implement Perl6 (rakudo)
It’s a work in progress.
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Final Notes
Conclusion
Parrot is a virtual machine
Parrot Compiler Toolkit
target multiple languages (or brand new)
implement Perl6 (rakudo)
It’s a work in progress.
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”
Questions
http://www.parrotcode.org/
Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”