jonathan worthingtonfirst steps pge (perl 6 grammar engine) parrot compiler toolkit (ast, code gen.)...

53
Rakudo The story of a compiler Jonathan Worthington

Upload: others

Post on 13-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Rakudo

The story of a compiler

Jonathan

Worthington

Page 2: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Rakudo

Development

History

Page 3: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Started by Patrick

Michaud around 2005

Not called Rakudo until

around January 2008;

was just “Perl 6 on

Parrot”

Page 4: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Key insight:

Perl 6 should be parsed

by Perl 6

Perl 6 should be largely

implemented in Perl 6

(or a subset of it)

Page 5: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

First Steps

Page 6: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

First Steps

PGE (Perl 6 Grammar Engine)

Page 7: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

First Steps

PGE (Perl 6 Grammar Engine)

Parrot Compiler Toolkit (AST, code gen.)

Page 8: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

First Steps

PGE (Perl 6 Grammar Engine)

Parrot Compiler Toolkit (AST, code gen.)

Not Quite Perl

Page 9: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

First Steps

PGE (Perl 6 Grammar Engine)

Parrot Compiler Toolkit (AST, code gen.)

Not Quite Perl

Rakudo Perl 6 Compiler

Page 10: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

In summer 2008...

I went to OSCON

Met Patrick Michaud for

the first time

Here’s what happened...

Page 11: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Jonathan

At some party...

Page 12: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Jonathan

At some party...

Hey, beer!

Page 13: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Jonathan

At some party...

Hey, more beer!

Page 14: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Jonathan

At some party...

Implementing junctions in Perl 6 sounds interesting. I’ll have a go at it.

Patrick, and others...

Page 15: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Tip:

If you don’t know how

hard it is to implement

something...

...be very careful about

saying you will do it.

Page 16: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Junctions

Type system

Multiple dispatch

Signature binding Classes

Page 17: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Hack hack hack...

Over the following year we

implemented many, many

features.

Good progress, but...

Page 18: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Problems

Significant changes to parsing,

thanks to STD arriving

PGE and PCT didn’t integrate so

well, creating hard to fix bugs

Complexity was making it hard to

make more progress

Page 19: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

ng

“next generation”

Page 20: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

ng

“next generation”

Parrot Compiler Toolkit(AST, regex compilation, code generation)

Page 21: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

ng

“next generation”

Parrot Compiler Toolkit(AST, regex compilation, code generation)

Not Quite Perl(Now bootstrapping)

Page 22: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

ng

“next generation”

Parrot Compiler Toolkit(AST, regex compilation, code generation)

Not Quite Perl(Now bootstrapping)

Rakudo Perl 6 Compiler

Page 23: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

ng

Fixed many long-standing issues

Also was the first time lazy lists

were introduced to Rakudo

A lot of progress, but some

regressions from “alpha” (the

original branch)

Page 24: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Lazy Lists

Really hard to add lazy lists to

Perl without surprising people in

bad ways

Too lazy weird action at a

distance

Not lazy enough uses to much

memory, or hangs too easily

Page 25: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Lazy Lists

After several designs that failed

to work, settled on an immutable

iterator model

Resolved the majority of the

semantic issues

Initial implementation slow

Page 26: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Rakudo

Star

Page 27: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Distributions

Users tend to want more than

just a compiler – they want some

modules, module installation

tools, documentation, etc.

We borrow the notion of

“distributions” from Linux

Page 28: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Rakudo Star

Our first series of distribution

releases

Aim: attract a wider user base

Separate release schedule and

release managers from compiler

releases

Page 29: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Did Well On Features

Chained Comparisons Junctions

Classes Signatures Grammars

Perl 6 Regexes Multi-dispatch

Lazy Lists Series Operator

Roles Introspection Traits

Meta-Operators Feeds MAIN

Smart-matching Modules

Native Library Calls Book

Page 30: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Got more...

Users

Modules

Bug Reports

Contributors

Page 31: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

But it wasn’t all good news...

Page 32: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Most things run slowly.

Some run glacially slowly.

Page 33: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

High memory usage - both base

amount and when running

Page 34: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Various unhelpful errors and

failure modes

Page 35: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Make it work

THEN

Make it fast

Page 36: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

The quick way to implement

a feature with the correct

semantics is very rarely

the optimal one.

Didn't want to waste time

making the wrong thing fast.

Correct FastHard

Page 37: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Now the development

focus is changing.

Many implemented features

now relatively stable.

Missing features aren't our

main adoption blocker, but

speed and memory usage are.

Page 38: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Current

Work

Page 39: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

alpha ng nom

We are here

Page 40: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

nom

“new object model”

Replaces the core objects

implementations with something

that performs far better

Both speed improvements and

memory usage reduction

Page 41: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

“nom” branch

Rebuild primitives on top of the

new object model

In parallel, a big cleanup of the

setting and many performance

improvements there too

Many more fixes, a few new

features

Page 42: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Why is current Rakudo

slow?

Various primitives are slow,

meaning that everything runs

slowly

No optimizer, and not enough

information at compile time to

write a good one

Page 43: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Performance

Improvements

Two sets of performance

improvements

First set is just from starting to

use the new object model

Second set will come from the

optimizer that we will build

Page 44: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Status

Going very well, but still some

work to come

Aim to deliver compiler release

from “nom” branch in July

Rakudo Star distribution release

with it should come in August

Page 45: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Inside

Rakudo

Page 46: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

CompilerGrammar, Actions,

Symbol Table, Module Loader

MetamodelClasses, roles,subset types,OO bootstrap

VM GlueSignature binderMulti-dispatcher

Low level guts

CORE.settingOperators

Built-in classesBuilt-in functions

Languages: NQP C Perl 6

Page 47: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

The Next

Year

Page 48: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Optimizer

Key optimizations:

Statically deciding multi-dispatch

Inlining (especially operators)

Type Inference

Aim to deliver an optimizer by the

October release

Page 49: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

LTM

Many performance improvements

so far have aimed at runtime

performance

Longest Token Matching support

will make Perl 6 grammars parse

faster – meaning that we can

parse Perl 6 faster

Page 50: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Bugs and Stability

Focus on fixing bugs, and

keeping the bug queue to a

reasonable size

Focus on providing a stable

platform for developing modules

and applications

Page 51: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Backends

Want Rakudo to run on and

generate code for multiple VMs

Already some initial work for the

.Net CLR / Mono, and for the JVM

Also interest in targeting v8

(Javascript)

Page 52: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Thank you!

Page 53: Jonathan WorthingtonFirst Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit (AST, code gen.) Not Quite Perl. First Steps PGE (Perl 6 Grammar Engine) Parrot Compiler Toolkit

Questions?

More on Perl 6: perl6.org

Rakudo: rakudo.org

Blog: 6guts.wordpress.com

Slides: jnthn.net/articles.shtml