Download pdf - Parrot Compiler Tools

Transcript
Page 1: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Parrot Compiler Tools

Kazutake [email protected]

Page 2: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Introduction

2001年 開発がスタート 2005年 Pugs がリリース 2006年 Perl6 いよいよリリースか 2008年 現在 Parrot version 0.6.1

Parrot is Dead ?

Page 3: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

What is Parrot Compiler Tools (PCT) ? Parrot VM 上でコンパイラを作成するための

ツール Rakudo は PCT を使って実装されている Parrot がターゲットとする動的言語は、全てPCT を使って実装できる

誰でも簡単にコンパイラを作成できる

Page 4: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Parrot VM

Perl6

All Dynamic language is compiled into Parrot bytecode

Perl5 Ruby Python

Page 5: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Parrot VM

Perl6

All Dynamic language is compiled into Parrot bytecode

Perl5 Ruby Python

俺言語

Page 6: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

What is Parrot ?

Register-based VM 動的言語 (Perl, Ruby etc) が動作可能な機能を提供する

4種類のレジスタを持つ (In, Nn, Sn, Pn)

Parrot Intermediate Representation (PIR) Parrot Assembly language (PASM)

Page 7: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Parrot Registers

Integers (I) Numbers (N) Strings (S) PMCs (P)

– Parrot Magic Cookies– String,Array, Hash, Object などを表現するためのレジスタ

Page 8: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Parrot Registers

I0 integer register #0N1 number of floating point register #1S2 string register #2P3 PMC register #3

Page 9: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Parrot Assembly language (PASM) Parrot のアセンブリ言語 Parrot バイトコードのテキスト表現

Page 10: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Parrot Assembly language (PASM)set I0, 1set S0, "Foo"set S1, S0set S0, "Bar"print S1 # Fooprint S0 # Barnew P0, 'String'set P0, "Baz"print P0 # Bazend

Page 11: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Parrot Intermediate Representation (PIR) PASM のシンタックスシュガー PASM よりは高級言語のような構文でコードを書くことができる

Page 12: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Parrot Intermediate Representation (PIR).sub 'main' :main .param pmc args

$P0 = compreg 'C99' $P1 = $P0.'command_line'(args).end

Page 13: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Parrot Compiler Tools

Parrot Grammar Engine (PGE) Parrot Abstract Syntax Tree (PAST) Parrot Opcode Syntax Tree (POST) Not Quite Perl(6) (NQP)

Page 14: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Compilation Phase

ソースコードからソースツリーを作る ソースツリーから PAST を作る PAST から POST を作る POST から PIR を作る

Page 15: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Parrot Grammar Engine(PGE) Perl6 の Rule を解釈する正規表現エンジン Rule は 言語の文法規則を記述することができる Rule で記述された文法から言語パーサーを生成する 文法規則に “{*}“ を見つけるとマッチした Rule と同じ名前のサブルーチンを実行する

実行されたサブルーチンは Parse Actions と呼ばれる

Page 16: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Perl6 Rule

grammar C99::Grammar is PCT::Grammar;

token TOP {

^

<external_declaration>+

[ $ || <.panic: Syntax error> ]

{*}

}

rule external_declaration {

| <declaration> {*} #= declaration

| <function_definition> {*} #= function_definition

}

Page 17: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Parse Actions

class C99::Grammar::Actions;

method TOP($/) {

for $<external_declaration> {

my $fun := $( $_ );

if $fun.name() eq 'main' {

make $fun;

}

}}

method external_declaration($/, $key) {

make $( $/{$key} );}

Page 18: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Not Quite Perl(6) (NQP)

Perl6 のサブセット Parse Actions を記述するための言語

Page 19: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Parrot Abstract Syntax Tree (PAST) 汎用的な AST のノードを定義したクラスライブラリ Parse Actions 内で PAST を作る プログラムに対して 完全な PAST を作ることができれば コンパイルは完了

PAST::Node, PAST::Val, PAST::Var…

Page 20: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Let’s Getting Started!

$ svn co https://svn.perl.org/parrot/trunk parrot$ cd parrot$ perl Configure.pl$ make$ make test

Page 21: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Generate a Language Stub

$ perl tools/dev/mk_language_shell.pl <language> <location>

例えば Foo 言語を language 配下に作成するには

以下のコマンドを実行する

$ perl tools/dev/mk_language_shell.pl Foo language/foo

Page 22: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Generate a Language Stub

config/gen/languages.pm 内の $ laguages を編集する

$languages = qq{ : : WMLScript Zcode Foo # add } unless defined $languages;

Page 23: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Generate a Language Stub

$ perl Configure.pl$ cd language/foo$ make$ make test

Page 24: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Source Tree foo/

/config/makefiles/root.in

/src/

/parser/

/actions.pm # Parse Actions を記述するファイル NQP で記述する

/grammar.pg # Perl6 の Rule で記述された文法規則

/builtins/

/say.pir # 組み込み関数を実装するためのファイル PIR で記述する

/t/

/00-sanity.t # テストファイル

/harness

/foo.pir # コンパイラのメインルーチン

Page 25: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Writing Code

say “Hello, Foo!”;

Page 26: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Executes

$ ../../parrot foo.pbc test.fooHello, Foo!

Page 27: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Next Step

言語の文法を変更する– foo/src/parser/grammar.pg– foo/src/parser/actions.pm

組み込み関数を追加する– foo/src/builtins/xxx.pir

Page 28: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Open the grammar.pg

Perl6 の Rule で構文規則を書く– http://dev.perl.org/perl6/doc/design/syn/S05.html

構文規則全体は grammar として定義 個別の構文規則は Rule と Token で書く Top level の Rule は “TOP” token として書く Rule にマッチしたときにアクションを実行する場合

“{*}”という特別なトークンを書く

Page 29: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Open the actions.pm

NQP を使って書く Actions クラスのメソッドとしてアクションを定義 grammar.pg の “{*}” が見つかった時

現在の Rule と同名のメソッドが呼び出される メソッドの最初の引数 ($/) には マッチした Rule の

Match Object が渡される

Page 30: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

grammar.pggrammar C99::Grammar is PCT::Grammar;

token TOP {

^

<external_declaration>+

[ $ || <.panic: Syntax error> ]

{*}

}

rule external_declaration {

| <declaration> {*} #= declaration

| <function_definition> {*} #= function_definition

}

Page 31: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

actions.pmclass C99::Grammar::Actions;

method TOP($/) {

for $<external_declaration> {

my $fun := $( $_ );

if $fun.name() eq 'main' {

make $fun;

}

}}

method external_declaration($/, $key) {

make $( $/{$key} );}

Page 32: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

NQP Syntax

$/ は Match オブジェクトを表す $<expression> で $/ からキャプチャーされた Match オブジェクトを名前で取得できる

$( $x ) と書くと $x をスカラーコンテキストで評価する make コマンドで Match オブジェクトに PAST ノードを追加する

my $past := PAST::Op.new( :node($/) );

Page 33: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

PAST Nodes

PAST::Node PAST::Block PAST::Stmts PAST::Var PAST::Val PAST::VarList PAST::Op

Page 34: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Advanced Topics

Scope Management Operator precedence Calling Conventions

Page 35: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

References

docs/pct/*.pdd http://planet.parrotcode.org/ http://www.parrotblog.org/ http://www.parrotcode.org/

Page 36: Parrot Compiler Tools

2008/05/15 YAPC::Asia 2008

Thank you !


Recommended