Parrot -- "one bytecode to rule them all"

37
”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”

description

http://workshop.perl.pt/ptpw2008/

Transcript of Parrot -- "one bytecode to rule them all"

Page 1: Parrot -- "one bytecode to rule them all"

”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”

Page 2: 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”

Page 3: 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”

Page 4: 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”

Page 5: 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”

Page 6: 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”

Page 7: Parrot -- "one bytecode to rule them all"

Parrot’s Architecture

Interpretation Flow

Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”

Page 8: 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”

Page 9: 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”

Page 10: 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”

Page 11: 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”

Page 12: 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”

Page 13: 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”

Page 14: 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”

Page 15: 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”

Page 16: 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”

Page 17: 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”

Page 18: 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”

Page 19: Parrot -- "one bytecode to rule them all"

Simple Benchmark

Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”

Page 20: Parrot -- "one bytecode to rule them all"

Parrot Compiler Toolkit

Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”

Page 21: 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”

Page 22: 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”

Page 23: 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”

Page 24: 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”

Page 25: 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”

Page 26: 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”

Page 27: 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”

Page 28: 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”

Page 29: 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”

Page 30: 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”

Page 31: 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”

Page 32: 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”

Page 33: 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”

Page 34: 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”

Page 35: 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”

Page 36: 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”

Page 37: Parrot -- "one bytecode to rule them all"

Questions

http://www.parrotcode.org/

Portuguese Perl Workshop 2008 Parrot ”one bytecode to rule them all”