un compilateur... comment ça marche?

22
Les compilateurs... comment ça marche ? Dinesh Bolkensteyn @dbolkensteyn 14 janvier 2014

Upload: dinesh-bolkensteyn

Post on 07-Jul-2015

182 views

Category:

Technology


0 download

DESCRIPTION

Les compilateurs nous ennuient tous les jours avec des messages d'erreurs en chinois... alors qu'ils ne font que leur travail, et souvent le font à la perfection ;-) (Re)découvrez comment les compilateurs fonctionnent en interne et ainsi à devenir plus indulgent à leur égard.

TRANSCRIPT

Page 1: Un compilateur... comment ça marche?

Les compilateurs... comment ça marche ?

Dinesh Bolkensteyn@dbolkensteyn

14 janvier 2014

Page 2: Un compilateur... comment ça marche?

La face visible

public static <T> Collection<T>

identity(Collection<?> param) {

return param;

}

Page 3: Un compilateur... comment ça marche?

La face visible

public static <T> Collection<T>

identity(Collection<??> param) {

return param;

}

incompatible types

found : java.util.Collection<capture#42 of ?>

required: java.util.Collection<T>

Page 4: Un compilateur... comment ça marche?

Ou moins visible...

Page 5: Un compilateur... comment ça marche?

Ou moins visible...

Page 6: Un compilateur... comment ça marche?

Architecture

Front End

● Syntaxe● Sémantique

Back End

● Optimisation● Génération de code

Page 7: Un compilateur... comment ça marche?

Front End

1. Lexer

2. Parser

3. Résolution des noms

4. Vérification des types

Page 8: Un compilateur... comment ça marche?

Front End : Lexer

foo + 42 * bar

Page 9: Un compilateur... comment ça marche?

Front End : Lexer

foo + 42 * bar

1. ID(« foo »)

2. PLUS(« + »)

3. INTEGER(« 42 »)

4. STAR(« * »)

5. ID(« bar »)

Page 10: Un compilateur... comment ça marche?

Front End : Lexer

foo + 42 * bar

1. ID(« foo »)

2. PLUS(« + »)

3. INTEGER(« 42 »)

4. STAR(« * »)

5. ID(« bar »)

Expressions régulières

● ID : [a-zA-Z][a-zA-Z0-9]*+

● INTEGER : [0-9]++

● PLUS : \+

● STAR : \*

● WHITESPACES : [ \r\n]++

Page 11: Un compilateur... comment ça marche?

Front End : Parser

foo + 42 * bar

Page 12: Un compilateur... comment ça marche?

Front End : Parser

foo + 42 * bar

Page 13: Un compilateur... comment ça marche?

Front End : Parser

foo + 42 * bar Grammaire formelle

● ADD :=

MUL {PLUS MUL}

MUL

● MUL :=

PRIM {STAR PRIM}

PRIM

● PRIM := ID | INTEGER

Page 14: Un compilateur... comment ça marche?

Front End : Parser

foo + 42 * bar Fonctions récursives

add() {

Expr left = mul() ;

accept(PLUS) ;

Expr right = mul() ;

return new Add(

left, right)

}

Page 15: Un compilateur... comment ça marche?

Front End : Résolution des noms

class Example {

public Example(

int foo) {

this.foo =

foo;

}

int foo;

}

Page 16: Un compilateur... comment ça marche?

Front End : Résolution des noms

class Example {

public Example(

int foo) {

this.foo =

foo;

}

int foo;

}

Phase 1:

Class(

fields => { foo }

constructors = {

Constructor(

parameters = { foo }

)

}

)

Page 17: Un compilateur... comment ça marche?

Front End : Résolution des noms

class Example {

public Example(

int foo) {

this.foo = // field

foo; // parameter

}

int foo;

}

Résultat phase 1:

Class(

fields => { foo }

constructors = {

Constructor(

parameters = { foo }

)

}

)

Page 18: Un compilateur... comment ça marche?

Front End : Vérifications des types

int a = 0 ; // OK

int a = 10 / 3 ; // OK

int a = 10.0 / 3 ; // KO

Page 19: Un compilateur... comment ça marche?

Front End : Vérifications des types

int a = 0 ; // OK

int a = 10 / 3 ; // OK

int a = 10.0 / 3 ; // KO

Règles de type● e1 : int, e2 : int

-> e1 + e2 : int● e1 : double, e2 : int

-> e1 + e2 : double● etc.

Page 20: Un compilateur... comment ça marche?

Back End

1. Optimisation

2. Génération de code

Page 21: Un compilateur... comment ça marche?

Et ça va bien plus loin...

● Inférence des types: val foo = ... ;● C++ template « meta-programming » :

template <int I>

int recursive(void) {

return recursive<I - 1>();

}

Page 22: Un compilateur... comment ça marche?

Les compilateurs... comment ça marche ?

Dinesh Bolkensteyn@dbolkensteyn

Merci !