Parrot Allison Randal The Perl Foundation & O'Reilly Media, - - PowerPoint PPT Presentation

parrot
SMART_READER_LITE
LIVE PREVIEW

Parrot Allison Randal The Perl Foundation & O'Reilly Media, - - PowerPoint PPT Presentation

Parrot Allison Randal The Perl Foundation & O'Reilly Media, Inc. There's an odd misconception in the computing world that writing compilers is hard. This view is fueled by the fact that we don't write compilers very often. People used to


slide-1
SLIDE 1

Parrot

Allison Randal The Perl Foundation & O'Reilly Media, Inc.

slide-2
SLIDE 2

There's an odd misconception in the computing world that writing compilers is hard. This view is fueled by the fact that we don't write compilers very often. People used to think writing CGI code was hard. Well, it is hard, if you do it in C without any tools.

slide-3
SLIDE 3

Parrot VM

Ruby Python Perl 5 Perl 6 PHP

slide-4
SLIDE 4

Parrot VM

Dylan Forth Tcl Ruby Scheme Python Perl 5 TeX macro Perl 6 PHP Objective-C Lua Javascript Invent your language here.

slide-5
SLIDE 5

Dynamic Languages

  • Runtime vs. compile-time
  • Extend code (eval, load)
  • Define classes
  • Alter type system
  • Higher-order functions
  • Closures, Continuations, Coroutines
slide-6
SLIDE 6

Why?

  • Revolution
  • Powerful tools
  • Portability
  • Interoperability
  • Drive innovation
slide-7
SLIDE 7

Register-based

  • Stack operations
slide-8
SLIDE 8

Register-based

  • Stack operations

14

slide-9
SLIDE 9

Register-based

  • Stack operations

9 14

slide-10
SLIDE 10

Register-based

  • Stack operations

9 14 add

slide-11
SLIDE 11

Register-based

  • Stack operations

23

slide-12
SLIDE 12

Register-based

  • Stack operations
  • Register operations

9 14

slide-13
SLIDE 13

Register-based

  • Stack operations
  • Register operations

9 14 add

slide-14
SLIDE 14

Register-based

  • Stack operations
  • Register operations

9 14 23

slide-15
SLIDE 15

Register-based

  • Stack operations
  • Register operations
  • Fewer instructions
  • Hardware registers
  • Register spilling
  • Flexible register sets
slide-16
SLIDE 16

Continuation Passing Style

  • Stack-based control flow
slide-17
SLIDE 17

Continuation Passing Style

  • Stack-based control flow

foo

slide-18
SLIDE 18

Continuation Passing Style

  • Stack-based control flow

return addr foo

slide-19
SLIDE 19

Continuation Passing Style

  • Stack-based control flow

9 return addr foo 12

slide-20
SLIDE 20

Continuation Passing Style

  • Stack-based control flow

return addr

slide-21
SLIDE 21

Continuation Passing Style

  • Stack-based control flow
slide-22
SLIDE 22

Continuation Passing Style

  • Stack-based control flow
  • Continuation-based

control flow

Context: main

slide-23
SLIDE 23

Continuation Passing Style

  • Stack-based control flow
  • Continuation-based

control flow

Context: main Context: foo

slide-24
SLIDE 24

Continuation Passing Style

  • Stack-based control flow
  • Continuation-based

control flow

Continuation Context: main Context: foo

slide-25
SLIDE 25

Continuation Passing Style

  • Stack-based control flow
  • Continuation-based

control flow

Continuation Context: main Context: foo Context: bar

slide-26
SLIDE 26

Continuation

Continuation Passing Style

  • Stack-based control flow
  • Continuation-based

control flow

Continuation Context: main Context: foo Context: bar

slide-27
SLIDE 27

Continuation

Continuation Passing Style

  • Stack-based control flow
  • Continuation-based

control flow

  • Deeply nested contexts
  • Tail recursion

Continuation Context: main Context: foo Context: bar

slide-28
SLIDE 28

Parrot VM

PASM (assembly language) Parser Grammar Engine (PGE) Parrot Compiler Toolkit (PCT)

I/O GC Events Threads OO Unicode Exceptions IMCC STM JIT

PIR (intermediate representation)

NQP PAST HLLCompiler

slide-29
SLIDE 29

PASM

  • Assembly language
  • Simple syntax
  • add I0, I1, I2
  • Human-readable bytecode
slide-30
SLIDE 30

PIR

  • Syntactic sugar
  • $I0 = $I1 + $I2
  • Named variables
  • .local int myvar
  • $I0 = myvar + 5
  • Sub and method calls
  • result = object.'method'($I0)
slide-31
SLIDE 31

NQP

  • Not Quite P(erl|ython|HP|uby)
  • Lightweight language
  • $a := 1;
  • print($a, "\n");
  • Compiler tools
  • $past := PAST::Op.new( :name('printnl') );
slide-32
SLIDE 32

Parser Grammar Engine

  • Regular expressions
  • Recursive descent
  • Operator precedence parser
slide-33
SLIDE 33

HLLCompiler

  • Base library
  • Quick start
  • Common features
slide-34
SLIDE 34

Pynie

  • Download
  • http://www.parrotcode.org
  • Build
  • $ perl Configure.PL
  • $ make test
  • Language
  • $ cd languages/pynie
  • $ make test
slide-35
SLIDE 35

Pynie

  • hello.py
  • print "Hello, World!"
  • Run
  • $ parrot pynie.pir hello.py
slide-36
SLIDE 36

pynie.pir

  • 67 lines
  • Half documentation
  • c = hllcompiler.new()
  • c.language('Pynie')
  • c.parsegrammar('Pynie::Grammar')

c.parseactions('Pynie::Grammar::Actions')

slide-37
SLIDE 37

Grammar.pg

  • Parser
  • token stmt_list {
  • <simple_stmt> [ ';' <simple_stmt> ]* ';'?
  • {*}
  • }
  • Familiar?
  • stmt_list ::=
  • simple_stmt (";" simple_stmt)* [";"]
slide-38
SLIDE 38

Actions.pct

  • Transform to AST
  • method identifier($/) {
  • make PAST::Var.new( :name( ~$/ ),
  • :scope('package'),
  • :node($/)
  • );
  • }
slide-39
SLIDE 39

Value Transformation

42

slide-40
SLIDE 40

Value Transformation

42 \d+ Parser grammar rule “integer” token integer { \d+ }

slide-41
SLIDE 41

Value Transformation

42 \d+ Parser grammar rule “integer” <integer> value: 42 Parse tree

slide-42
SLIDE 42

Value Transformation

42 \d+ Parser grammar rule “integer” <integer> value: 42 Parse tree integer Transform rule

method integer($/) {...}

slide-43
SLIDE 43

Value Transformation

42 \d+ Parser grammar rule “integer” <integer> value: 42 Parse tree integer Transform rule <PAST::Val> value: 42 returns: Integer AST node

slide-44
SLIDE 44

Value Transformation

42 \d+ Parser grammar rule “integer” <integer> value: 42 Parse tree integer AST tree grammar rule AST node PAST::Val Transform rule

  • st = self.as_post(ast)

<PAST::Val> value: 42 returns: Integer

slide-45
SLIDE 45

Value Transformation

42 \d+ Parser grammar rule “integer” <integer> value: 42 Parse tree integer Transform rule <PAST::Val> value: 42 returns: Integer AST node PAST::Val Transform rule <POST::Op> result: 1 OST node

slide-46
SLIDE 46

Value Transformation

42 \d+ Parser grammar rule “integer” <integer> value: 42 Parse tree integer Transform rule <PAST::Val> value: 42 returns: Integer AST node PAST::Val Transform rule <POST::Op> result: 1 OST node POST::Val Transform rule

self.pir(ost)

slide-47
SLIDE 47

Value Transformation

42 \d+ Parser grammar rule “integer” <integer> value: 42 Parse tree integer Transform rule <PAST::Val> value: 42 returns: Integer AST node PAST::Val Transform rule <POST::Op> result: 1 OST node POST::Val 42 Transform rule

slide-48
SLIDE 48

Operator Transformation

6 * 9

slide-49
SLIDE 49

Operator Transformation

6 * 9 infix:* Parser grammar OPP rule

proto infix:* is looser(prefix:+) {...}

slide-50
SLIDE 50

Operator Transformation

6 * 9 infix:* Parser grammar OPP rule <expr> type: 'infix:*' Parse tree <integer> value: 6 <integer> value: 9

slide-51
SLIDE 51

Operator Transformation

<expr> type: 'infix:*' Parse tree <integer> value: 6 <integer> value: 9

slide-52
SLIDE 52

Operator Transformation

<expr> type: 'infix:*' Parse tree expr Transform rule <integer> value: 6 <integer> value: 9

slide-53
SLIDE 53

Operator Transformation

<expr> type: 'infix:*' Parse tree expr Transform rule <integer> value: 6 <integer> value: 9 AST tree <PAST::Op> name: infix:* <PAST::Val> value: 6 returns: Integer <PAST::Val> value: 9 returns: Integer

slide-54
SLIDE 54

Operator Transformation

AST tree <PAST::Op> name: infix:* <PAST::Val> value: 6 returns: Integer <PAST::Val> value: 9 returns: Integer

slide-55
SLIDE 55

Operator Transformation

PAST::Op Transform rule AST tree <PAST::Op> name: infix:* <PAST::Val> value: 6 returns: Integer <PAST::Val> value: 9 returns: Integer

slide-56
SLIDE 56

Operator Transformation

PAST::Op Transform rule AST tree <PAST::Op> name: infix:* <PAST::Val> value: 6 returns: Integer <PAST::Val> value: 9 returns: Integer OST tree <POST::Ops> <POST::Ops> result: 6 <POST::Ops> result: 9 <POST::Op> name: n_mul variable setup <POST::Ops> result: $P1

slide-57
SLIDE 57

Operator Transformation

  • .sub _main :main
  • new $P1, 'Integer'
  • new $P2, 'Integer'
  • set $P2, 6
  • new $P3, 'Integer'
  • set $P3, 9
  • mul $P1, $P2, $P3
  • .end
slide-58
SLIDE 58

Examples

  • In the Parrot distribution:
  • examples/tutorial/*.pir
slide-59
SLIDE 59

Questions?

  • Further Reading

– “Continuations and advanced flow control”

by Jonathan Bartlett

<http://www.ibm.com/developerworks/linux/library/l- advflow.html>

– “The case for virtual register machines” by

Brian Davis, et al.

<http://portal.acm.org/citation.cfm?id=858575>