design choices in a compiler course - aucs.au.dk/~mis/cc2008.pdf · cc 2008: design choices in a...

78
1 Michael I. Schwartzbach University of Aarhus Design Choices in A Compiler Course or How To Make Undergraduates Love Formal Notation

Upload: trinhhanh

Post on 31-Jan-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

1

Michael I. SchwartzbachUniversity of Aarhus

Design Choices in A Compiler Courseor

How To Make Undergraduates Love Formal Notation

Page 2: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

2

Michael I. SchwartzbachUniversity of Aarhus

of

in

Page 3: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

3

CC 2008: Design Choices in A Compiler Course

The Undergraduate Compiler CourseThe Undergraduate Compiler Course

A culmination of the Bachelor program:• software engineering• formal languages• algorithms and data structures• logic• machine architecture

A rite of passageA peach for the right teacher

Page 4: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

4

CC 2008: Design Choices in A Compiler Course

Design ChoicesDesign Choices

ProjectsSource languageTarget languageImplementation languageSpecifications and formalizationTools and technologySoftware engineeringSkeleton codeTestingGroup workExams and grading

Based on an unscientificstudy of around 50 compiler courses world-wide.

Page 5: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

5

CC 2008: Design Choices in A Compiler Course

ProjectsProjects

Most courses are focused around a project:• learning-by-doing• implement a working compiler

Page 6: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

6

CC 2008: Design Choices in A Compiler Course

ProjectsProjects

Most courses are focused around a project:• learning-by-doing• implement a working compiler

Project architectures:• monolithic• unrelated assignments• implicit phase slicing (milestones)• explicit phase slicing (invariants)

Page 7: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

7

CC 2008: Design Choices in A Compiler Course

Source LanguageSource Language

Never a complete language (Java, C#, Haskell):• subtle details are viewed as distracting• redundant features increase grunt work

Page 8: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

8

CC 2008: Design Choices in A Compiler Course

Source LanguageSource Language

Never a complete language (Java, C#, Haskell):• subtle details are distracting• redundant features increase grunt work

Scaled-down or invented languages:• essentially the same features• an irresistible urge to tinker (and to make bad puns)

Page 9: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

9

CC 2008: Design Choices in A Compiler Course

Source LanguageSource Language

Never a complete language (Java, C#, Haskell):• subtle details are distracting• redundant features increase grunt work

Scaled-down or invented languages:• essentially the same features• an irresistible urge to tinker (and to make bad puns)

Tiny

Cool

Uncool

MinC

MicroGCL

Tiger

Iota

PCAT

Tiny-C

SampleCLake

B-flatDL07

Simple

Ada/CS

ice9

ALLCOT

F05

Z#Cool

MiniCaml

MiniPascal Pascalito

MiniOberon

SOOP

SIMP

Tigris

Minila

C--

μOCCAM

MLPolyR

DejlispDecaf

Irish CoffeeEspresso

TinyJava

MiniJava

MicroJava

Fjava Javelet

StaticJava

CSX

j--

Jack

Joos

Page 10: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

10

CC 2008: Design Choices in A Compiler Course

DomainDomain--Specific Source LanguageSpecific Source Language

Students design an application language:• graphics• robots• game playing

A realistic scenario for applying their skillsBut easy to omit important featuresAnd a Babylonic confusion for the teacher

Page 11: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

11

CC 2008: Design Choices in A Compiler Course

Target LanguageTarget Language

Much less variation:

• JVM• .NET• P-code

• X86• SPARC• MIPS (SPIM)

• C-code

easy street

roughing it

Page 12: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

12

CC 2008: Design Choices in A Compiler Course

Implementation LanguageImplementation Language

Whatever the students are familiar with:• Java, C#, ML, C++

Same as source language for elegance?

Different from source language for exposure?

A book parameter!

Page 13: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

13

CC 2008: Design Choices in A Compiler Course

Specifications and FormalizationSpecifications and Formalization

Need to specify:• syntax and semantics of source and target language• translation between these

Formalizations are rarely used (unloved?)Define the source as a syntactic subset:• actually quite a brittle approach, easy to get wrong

Students get exposed to huge specifications:• probably a healthy experience

Page 14: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

14

CC 2008: Design Choices in A Compiler Course

Tools and TechnologyTools and Technology

Most phases of a compiler may be automated:• but they are generally not...

Only scanning and parsing tools are used:• Lex/Flex and Yacc/Bison still rule supremely• More modern Java and C# tools are coming strong

Modern tools are often better:• integrated scanner and parser• AST support• expressive power beyond LALR(1)

Page 15: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

15

CC 2008: Design Choices in A Compiler Course

Software EngineeringSoftware Engineering

Opportunity for showcasing tools and techniques:• IDEs• code sharing• versioning• testing• programming patterns

But the old-school charm is still abundant:• vi• csh

Page 16: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

16

CC 2008: Design Choices in A Compiler Course

Skeleton CodeSkeleton Code

Students need a starting point:• structure• uniformity• adjusting the work load

A complete compiler with gaps everywhereA working compiler for a subset of the languageA full implementation of some phases

Page 17: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

17

CC 2008: Design Choices in A Compiler Course

TestingTesting

Testing a compiler is a challenge

Dump an annotated AST after each phase

Explicit phases perhaps enable a testing harness

Black-box testing of complete compiler• if also often used as part of the grading

Page 18: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

18

CC 2008: Design Choices in A Compiler Course

Group WorkGroup Work

Larger projects require project groups:• collaboration• planning• communication• one guy only fetching sodas

The ideal group size is 3• fits well with other studies of programming

Plan ahead what to do when a group breaks up

Page 19: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

19

CC 2008: Design Choices in A Compiler Course

Exams and GradingExams and Grading

Final grade is always a weighted sum:• project hand-ins• midterms• final exams

The project is perhaps weighted too low:• 25% to 50% for group projects (median 40%)• 70% to 90% for individual projects (median 70%)

The final exam is typically a written test• focusing on the underlying theory

Page 20: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

20

CC 2008: Design Choices in A Compiler Course

The dOvs CourseThe dOvs Course

The undergraduate compiler class at Aarhus U.In the final year of the B.Sc. program15 weeks durationA mandatory course, 80 students each yearSucceeds a course on formal languages

Redesigned in 2005:• explicit attention to the above design choices• some radical (and fun) ideas have been tried out

Page 21: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

21

CC 2008: Design Choices in A Compiler Course

A Case for Large LanguagesA Case for Large Languages

The source language is most of Java 1.3:• no package private declarations• some missing control structures• some odds and ends removed

This is the Joos2 language for extra credit

The basic Joos1 language further omits:• instance and static initializers• multi-dimensional arrays• interface declarations

Page 22: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

22

CC 2008: Design Choices in A Compiler Course

Benefits of A Large LanguageBenefits of A Large Language

The obvious benefits:• realistic insights into a full working compiler• sense of achievement and empowerment• street credibility

More features exposed than in toy languages:• modularization• well-formedness requirements• static analyses

Page 23: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

23

CC 2008: Design Choices in A Compiler Course

A Large SkeletonA Large Skeleton

Our model Joos2 compiler is 12,207 lines of code• plus 64,290 lines generated by SableCC

The skeleton compiler is 8,466 lines of code• of which 5,000 lines define the JVM target code

The ideal group must write 3,741 lines of code• in practice, they write between 5,000 and 10,000 lines

Also, they get a complete Joos0 compiler

Page 24: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

24

CC 2008: Design Choices in A Compiler Course

Frontend vs. BackendFrontend vs. Backend

A frontend-heavy compiler course:• much time is spent on formal languages• LALR(1) parsing tables is a cornerstone (up to 50%)• typically generates simple VM code

A backend-heavy compiler course:• much time is spent on code generation• register allocation, code selection, optimization• typically uses a simple, static source language

Page 25: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

25

CC 2008: Design Choices in A Compiler Course

MiddleMiddle--End?End?

The middle-end of a compiler deals with:• semantic analysis• symbol tables, type checking, static analysis

The dOvs course is middle-end-heavy:• LALR(1) tables are lightly covered• The skeleton contains a grammar with only few gaps• The target language is Java bytecode

Page 26: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

26

CC 2008: Design Choices in A Compiler Course

The Java MiddleThe Java Middle--EndEnd

About 60% of the language specification:• non LALR(1) syntactic restrictions• building the global class library• resolving import declarations• checking class hierarchy well-formedness• disambiguating compound names• type checking• constant folding• reachability analysis• definite assignment analysis

Page 27: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

27

CC 2008: Design Choices in A Compiler Course

Understanding Joos2Understanding Joos2

Joos2 is a syntactic subset of Java 1.3• and Joos1 is a syntactic subset of Joos2

• and Joos0 is a syntactic subset of Joos1

These languages hinge on understanding Java

The JLS is a difficult document:• an avalanche of details• hypnotically boring prose• compiler phases cut across many chapters

Page 28: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

28

CC 2008: Design Choices in A Compiler Course

Using FormalismsUsing Formalisms

Most undergraduates hate formal notation

In particular when it seems artificial:

x: intS(x): int

0: int

x+0 = x

x+S(y) = S(x+y) x = x

x = yy = x

x = y y = zx = z

Page 29: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

29

CC 2008: Design Choices in A Compiler Course

Learning to Love Formal NotationLearning to Love Formal Notation

Page 30: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

30

CC 2008: Design Choices in A Compiler Course

Learning to Love Formal NotationLearning to Love Formal Notation

JLS is a nightmare to read

Page 31: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

31

CC 2008: Design Choices in A Compiler Course

Learning to Love Formal NotationLearning to Love Formal Notation

JLS is a nightmare to read

You desperately need to write your code

Page 32: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

32

CC 2008: Design Choices in A Compiler Course

Learning to Love Formal NotationLearning to Love Formal Notation

JLS is a nightmare to read

You desperately need to write your code

Formal notation explains the JLS

Page 33: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

33

CC 2008: Design Choices in A Compiler Course

Learning to Love Formal NotationLearning to Love Formal Notation

JLS is a nightmare to read

You desperately need to write your code

Formal notation explains the JLS

Formal notation is a template for your code

Page 34: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

34

CC 2008: Design Choices in A Compiler Course

Learning to Love Formal NotationLearning to Love Formal Notation

JLS is a nightmare to read

You desperately need to write your code

Formal notation explains the JLS

Formal notation is a template for your code

Formal notation is your friend in need!

Page 35: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

35

CC 2008: Design Choices in A Compiler Course

Learning to Love Formal NotationLearning to Love Formal Notation

JLS is a nightmare to read

You desperately need to write your code

Formal notation explains the JLS

Formal notation is a template for your code

Formal notation is your friend in need!

Psych 101

Page 36: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

36

CC 2008: Design Choices in A Compiler Course

Definite Assigment: JLS StyleDefinite Assigment: JLS Style

The definite unassignment analysis of loop statements raises a special problem. Consider the statement while (e) S. In order to determine whether V is definitely unassigned within some subexpression of e, we need to determine whether V is definitely unassigned before e. One might argue, by analogy with the rule for definite assignment, that V is definitely unassigned before e iff it is definitely unassigned before the while statement. However, such a rule is inadequate for our purposes. If e evaluates to true, the statement S will be executed. Later, if V is assigned by S, then in the following iteration(s) V will have already been assigned when e is evaluated. Under the rule suggested above, it would be possible to assign V multiple times, which is exactly what we have sought to avoid by introducing these rules. A revised rule would be: V is definitely unassigned before e iff it is definitely unassigned before the while statement and definitely unassigned after S. However, when we formulate the rule for S, we find: V is definitely unassigned before S iff it is definitely unassigned after e when true. This leads to a circularity. In effect, V is definitely unassigned before the loop condition e only if it is unassigned after the loop as a whole! We break this vicious circle using a hypothetical analysis of the loop condition and body. For example, if we assume that V is definitely unassigned before e (regardless of whether V really is definitely unassigned before e), and can then prove that V was definitely unassigned after e then we know that e does not assign V.

474 lines of pure poetry

Page 37: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

37

CC 2008: Design Choices in A Compiler Course

Definite Assignment: Formal NotationDefinite Assignment: Formal Notation

while(E) S:B[[E]] = B[[while(E) S]]B[[S]] = At[[E]]A[[while(E) S]] = Af[[E]]

x = E:B[[E]] = B[[x = E]]A[[x = E]] = A[[E]] ∪ {x}

E1 && E2:B[[E1]] = B[[E1 && E2]]B[[E2]] = At[[E1]]At[[E1 && E2]] = At[[E2]]Af[[E1 && E2]] = Af[[E1]] ∩ Af[[E2]]A[[E1 && E2]] = At[[E1 && E2]] ∩ Af[[E1 && E2]]

Page 38: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

38

CC 2008: Design Choices in A Compiler Course

Definite Assignment: CodeDefinite Assignment: Code

public @Override void caseABinopExp(ABinopExp exp){exp.getLeft().before = new HashSet<ALocalDecl>(exp.before);if (exp.getBinop() instanceof ALazyAndBinop){

exp.getLeft().apply(this);exp.getRight().before = new HashSet<ALocalDecl>(exp.getLeft().aftertrue);exp.getRight().apply(this);exp.aftertrue = new HashSet<ALocalDecl>(exp.getRight().aftertrue);exp.afterfalse = intersection(exp.getLeft().afterfalse, exp.getRight().afterfalse);exp.after = intersection(exp.aftertrue, exp.afterfalse);return;

}if (exp.getBinop() instanceof ALazyOrBinop){

exp.getLeft().apply(this);exp.getRight().before = new HashSet<ALocalDecl>(exp.getLeft().afterfalse);exp.getRight().apply(this);exp.aftertrue = intersection(exp.getLeft().aftertrue, exp.getRight().aftertrue);exp.afterfalse = new HashSet<ALocalDecl>(exp.getRight().afterfalse);exp.after = intersection(exp.aftertrue, exp.afterfalse);return;

}exp.getLeft().apply(this);exp.getRight().before = new HashSet<ALocalDecl>(exp.getLeft().after);exp.getRight().apply(this);exp.after = new HashSet<ALocalDecl>(exp.getRight().after);exp.aftertrue = new HashSet<ALocalDecl>(exp.after);exp.afterfalse = new HashSet<ALocalDecl>(exp.after);

}

Page 39: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

39

CC 2008: Design Choices in A Compiler Course

Type RulesType Rules

C,L,X,σ |- E: τ1 (num(τ1)∧num(τ2)) ∨ τ1:=τ2 ∨ τ2:=τ1

C, L, X, σ |- (τ2)E: τ2

C,L,X,σ |- E: D C,L,X,σ |- Ei: σi

D |- τ m(τ1,...,τk) Y τi:= σi

∀ D |- γ m(γ1,..., γk) Z : (∀i: γi:= σi) ⇒ (∀i: γi:= τi)

C, L, X, σ |- E.m(E1,...,Ek): τ

Page 40: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

40

CC 2008: Design Choices in A Compiler Course

Inductive Rules for The Class HierarchyInductive Rules for The Class Hierarchy

S∈SUPER(T) m∈CONTAIN(S)

nodecl(T,m) abstract∉mods(m)

m∈INHERIT(T)

S∈SUPER(T) m∈CONTAIN(S)

nodecl(T,m) abstract∈mods(m) allabs(T,m)

m∈INHERIT(T)

S∈SUPER(T) m∈CONTAIN(S)

S'∈SUPER(T) m'∈CONTAIN(S')

nodecl(T,m) sig(m)=sig(m')

abstract∉mods(m) abstract∈mods(m')

(m,m')∈REPLACE

S∈SUPER(T) f∈CONTAIN(S)

∀f'∈ DECLARE(T): name(f')≠name(f)

f∈INHERIT(T)

m∈DECLARE(T) S∈SUPER(T)

m'∈CONTAIN(S) sig(m)=sig(m')

(m,m')∈REPLACE

Page 41: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

41

CC 2008: Design Choices in A Compiler Course

Logical Rules for WellLogical Rules for Well--FormednessFormedness

1) ¬ ∃ T: T < T2) ∀ m,m'∈DECLARE(T): m ≠m' ⇒ sig(m)≠sig(m')3) ∀ m,m'∈CONTAIN(T): sig(m)=sig(m') ⇒ type(m)=type(m')4) ∀ m∈CONTAIN(T): abstract∈mods(m) ⇒ abstract∈mods(T) 5) ∀ (m,m')∈REPLACE: static∈mods(m) ⇔ static∈mods(m') 6) ∀ (m,m')∈REPLACE: type(m)=type(m')7) ∀ (m,m')∈REPLACE: public∈mods(m') ⇒ public∈mods(m) 8) ∀ (m,m')∈REPLACE: ∀e∈except(m): ∃e'∈except(m'): e≤e'9) ∀ (m,m')∈REPLACE: final∉mods(m')10) ∀ f,f'∈ DECLARE(T): f ≠ f' ⇒ name(f) ≠ name(f')11) ∀ S,S'∈ SUPER(T):

∀f∈CONTAIN(S), f'∈CONTAIN(S'):name(f) = name(f') ⇒ decl(f) = decl(f')

Page 42: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

42

CC 2008: Design Choices in A Compiler Course

Autogenerated CodeAutogenerated Code

The SableCC tool generates an AST• through inductive translations from the parse tree• lots of code: 64,290 lines for Joos2

Page 43: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

43

CC 2008: Design Choices in A Compiler Course

Autogenerated CodeAutogenerated Code

The SableCC tool generates an AST• through inductive translations from the parse tree• lots of code: 64,290 lines for Joos2

The remaining phases adddecorations to this tree

But how to extend theautogenerated code?

Page 44: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

44

CC 2008: Design Choices in A Compiler Course

Explicit Phase SlicingExplicit Phase Slicing

A simple use of AspectJ solves the problem

The decorations become intertype declarations:• fields, methods, interfaces

Each phase is then in one self-contained file• per traversal of the AST

This greatly simplifies hand-ins!

Page 45: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

45

CC 2008: Design Choices in A Compiler Course

The TypeChecking PhaseThe TypeChecking Phase

public aspect TypeChecking extends DepthFirstAdapter {/** The static type of the expression */public PType PExp.type;

/** The static type of the lvalue */public PType PLvalue.type;

/** The declaration of the field referenced in this lvalue */public AFieldDecl AStaticFieldLvalue.field_decl;

/** The declaration of the field referenced in this lvalue */public AFieldDecl ANonstaticFieldLvalue.field_decl;

/** The declaration of the method invoked by this expression */public AMethodDecl AStaticInvokeExp.method_decl;

/** The declaration of the method invoked by this expression */public AMethodDecl ANonstaticInvokeExp.method_decl;

/** The declaration of the constructor invoked by this expression */public AConstructorDecl ANewExp.constructor_decl;

/** The declaration of the constructor invoked by this statement */public AConstructorDecl ASuperStm.constructor_decl;

/** The declaration of the constructor invoked by this statement */public AConstructorDecl AThisStm.constructor_decl;

...

}

Page 46: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

46

CC 2008: Design Choices in A Compiler Course

Online Code RepositoryOnline Code Repository

Page 47: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

47

CC 2008: Design Choices in A Compiler Course

Mixing CompilersMixing Compilers

parser

environments

type linking

hierarchy

disambiguation

type checking

reachability

definite assignment

resources

code generation

optimization

parser

environments

type linking

hierarchy

disambiguation

type checking

reachability

definite assignment

resources

code generation

optimization

Page 48: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

48

CC 2008: Design Choices in A Compiler Course

A test suite of 1,149 source programs• produced through organic growth

Each tests a very specific feature or bug

Works on a subset of your phasesThese are mixed with our model compilerThis allows testing of partial compilers

Runs on a farm of 17 dedicated servers

WebWeb--Based Unit TestingBased Unit Testing

Page 49: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

49

CC 2008: Design Choices in A Compiler Course

Starting A Unit TestStarting A Unit Test

Page 50: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

50

CC 2008: Design Choices in A Compiler Course

Unit Test ResultsUnit Test Results

Page 51: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

51

CC 2008: Design Choices in A Compiler Course

Unit Test ResultsUnit Test Results

public class J1_A_FieldInitialization_NonConstant_Before {public int b = this.a; // b = 0public int a = new Integer(22).intValue(); // a = 22

public J1_A_FieldInitialization_NonConstant_Before() {}

public static int test() {J1_A_FieldInitialization_NonConstant_Before j = new J1_A_FieldInitialization_NonConstant_Before();return j.a + j.b + 101;

}}

***** Compiler exit status is 0 (SUCCESS : expected no errors)***** Assembling: /users/mis/public_html/dOvs/06/testcases/J1_A_FieldInitialization_NonConstant_Before.j***** Assembler exit status is 0 (SUCCESS)***** Running: J1_A_FieldInitialization_NonConstant_Before.test()***** Test case failed with status 145 (INCORRECT)

Page 52: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

52

CC 2008: Design Choices in A Compiler Course

Incremental FeedbackIncremental Feedback

The online test driver gives immediate feedback

The webboard is staffed:• 12% answered within 5 minutes• 42% answered within 1 hour• 94% answered within 12 hours

Hand-ins are graded continuously:• incremental alignment

Page 53: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

53

CC 2008: Design Choices in A Compiler Course

Evaluating a PhaseEvaluating a Phase

Page 54: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

54

CC 2008: Design Choices in A Compiler Course

Big Brother is WatchingBig Brother is Watching

All activity is monitored and logged:• file uploads• test runs and results• invocations of ajc• webboard postings

Interesting feedback for the teacher

Data-mining for academic dishonesty:• catch the act of cheating, not just the result

Page 55: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

55

CC 2008: Design Choices in A Compiler Course

The Activity GraphThe Activity Graph

Weighted sum of test runs, compilations, and postings on the webboard

Page 56: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

56

CC 2008: Design Choices in A Compiler Course

The Activity GraphThe Activity Graph

ideal case

Page 57: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

57

CC 2008: Design Choices in A Compiler Course

The Activity GraphThe Activity Graph

ideal case

worst case

Page 58: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

58

CC 2008: Design Choices in A Compiler Course

The Green StruggleThe Green Struggle

One line per group

Sorted by greennessGreen indicates thepercentage of unittests that succeeded

One snapshot every 10 minutes

Page 59: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

59

CC 2008: Design Choices in A Compiler Course

The Green StruggleThe Green Struggle

The last 2.5 days towards the TypeChecking deadline

Page 60: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

60

CC 2008: Design Choices in A Compiler Course

The Peephole ContestThe Peephole Contest

Reduce bytecodes with peephole patternsA domain-specific language for patternsThe –O flag runs a fixed-point driverThe best groups invest 100 man-hoursUnsound patterns are removedThe prize is a T-shirt:

Page 61: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

61

CC 2008: Design Choices in A Compiler Course

A DSL for Peephole PatternsA DSL for Peephole Patterns

pattern dup_istore_pop x:x ~ dup

istore (i0)pop

-> 3 istore (i0)

pattern constant_iadd_residue x:x ~ ldc_int (i0)

iaddldc_int (i1)iadd

-> 4 ldc_int (i0+i1)iadd

pattern goto_goto x:x ~ goto (l0)&& target l0 ~ goto (l1)&& ! (target l1 ~ goto (l2))&& ! (target l1 ~ label (l3))

-> 1 goto (l1)

pattern pr85 x:x ~ dup

if (c0,l1)&& target l1 ~ dup

if (c1,l2)y: *

&& c0 == negate c1&& target y ~ goto (l3)&& !(target l3 ~ goto (l4))&& !(target l3 ~ label (l4))&& !(target l3 ~ dup if (c2,lw0))

-> 2 dupif (c0,l3)

Page 62: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

62

CC 2008: Design Choices in A Compiler Course

A DSL for Peephole PatternsA DSL for Peephole Patterns

pattern dup_istore_pop x:x ~ dup

istore (i0)pop

-> 3 istore (i0)

pattern constant_iadd_residue x:x ~ ldc_int (i0)

iaddldc_int (i1)iadd

-> 4 ldc_int (i0+i1)iadd

pattern goto_goto x:x ~ goto (l0)&& target l0 ~ goto (l1)&& ! (target l1 ~ goto (l2))&& ! (target l1 ~ label (l3))

-> 1 goto (l1)

pattern pr85 x:x ~ dup

if (c0,l1)&& target l1 ~ dup

if (c1,l2)y: *

&& c0 == negate c1&& target y ~ goto (l3)&& !(target l3 ~ goto (l4))&& !(target l3 ~ label (l4))&& !(target l3 ~ dup if (c2,lw0))

-> 2 dupif (c0,l3)

Termination function

Page 63: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

63

CC 2008: Design Choices in A Compiler Course

Exams and GradingExams and Grading

The project is weighted 70%• the group members share this part• 75% hand-ins, 25% functional test

A multiple choice test is weighted 30%• this secures an individual component of the grade

0

2

4

6

8

10

12

14

16

18

20

F FX E D C B A

Page 64: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

64

CC 2008: Design Choices in A Compiler Course

Multiple Choice Test?Multiple Choice Test?

They are somewhat unfairly maligned:• can only test superficial learning• a favorite of the lazy teacher• chosen to be easy to grade

Page 65: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

65

CC 2008: Design Choices in A Compiler Course

Multiple Choice Test?Multiple Choice Test?

They are somewhat unfairly maligned:• can only test superficial learning• a favorite of the lazy teacher• chosen to be easy to grade

Not necessarily so:• can test learning at any level• requires a lot of work from the teacher• chosen to give fair grades

Page 66: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

66

CC 2008: Design Choices in A Compiler Course

Intelligent QuestionsIntelligent QuestionsConsider the method invocation A.B(1,2,3).To which category can A not belong?

a A class name. b A static field name.c A non-static field name.d A local name.e A package name.f A formal name.

Which Java code could correspondto the following JVM code?

iload 1ldc_int 1iadddupistore 2

a x = x + y; b x = x + 1;c x = x + 2;d x = y + 1;

Consider a scanner defined by thefollowing sequence of regularexpressions:

”xyzzy”[xy]*[z]+[xy]+[xyz]

What is the first token generatedfor the string: xyzzyyzzyy ?

a xyzzy b xyzzyy c xyze xyzzyyzzyy

Page 67: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

67

CC 2008: Design Choices in A Compiler Course

Intelligent QuestionsIntelligent QuestionsConsider the method invocation A.B(1,2,3).To which category can A not belong?

a A class name. b A static field name.c A non-static field name.d A local name.e A package name.f A formal name.

Which Java code could correspondto the following JVM code?

iload 1ldc_int 1iadddupistore 2

a x = x + y; b x = x + 1;c x = x + 2;d x = y + 1;

Consider a scanner defined by thefollowing sequence of regularexpressions:

”xyzzy”[xy]*[z]+[xy]+[xyz]

What is the first token generatedfor the string: xyzzyyzzyy ?

a xyzzy b xyzzyy c xyze xyzzyyzzyy

Page 68: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

68

CC 2008: Design Choices in A Compiler Course

Scoring Multiple Choice TestsScoring Multiple Choice Tests

How should answers be scored?• partial knowledge is allowed by multiple marks

Page 69: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

69

CC 2008: Design Choices in A Compiler Course

Scoring Multiple Choice TestsScoring Multiple Choice Tests

How should answers be scored?• partial knowledge is allowed by multiple marks

Five reasonable axioms:1. Marking all or no answers gives nothing

Page 70: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

70

CC 2008: Design Choices in A Compiler Course

Scoring Multiple Choice TestsScoring Multiple Choice Tests

How should answers be scored?• partial knowledge is allowed by multiple marks

Five reasonable axioms:1. Marking all or no answers gives nothing2. Answering correctly is better than answering incorrectly

Page 71: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

71

CC 2008: Design Choices in A Compiler Course

Scoring Multiple Choice TestsScoring Multiple Choice Tests

How should answers be scored?• partial knowledge is allowed by multiple marks

Five reasonable axioms:1. Marking all or no answers gives nothing2. Answering correctly is better than answering incorrectly3. If correctness is unchanged, fewer marks are no worse

Page 72: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

72

CC 2008: Design Choices in A Compiler Course

Scoring Multiple Choice TestsScoring Multiple Choice Tests

How should answers be scored?• partial knowledge is allowed by multiple marks

Five reasonable axioms:1. Marking all or no answers gives nothing2. Answering correctly is better than answering incorrectly3. If correctness is unchanged, fewer marks are no worse4. Combining questions and answers changes nothing

Page 73: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

73

CC 2008: Design Choices in A Compiler Course

Scoring Multiple Choice TestsScoring Multiple Choice Tests

How should answers be scored?• partial knowledge is allowed by multiple marks

Five reasonable axioms:1. Marking all or no answers gives nothing2. Answering correctly is better than answering incorrectly3. If correctness is unchanged, fewer marks are no worse4. Combining questions and answers changes nothing

What is a newt?

a A small animalb A unit of gravity

What is the correct URL encoding of ”100% pure”?

a 100%+pureb 100+%+purec 100%25+pure

What is a newt and what is the correct URL encoding of ”100% pure”?

a A small animal and 100%+pureb A small animal and 100+%+purec A small animal and 100%25+pured A unit of gravity and 100%+puree A unit of gravity and 100+%+puref A unit of gravity and 100%25+pure

Page 74: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

74

CC 2008: Design Choices in A Compiler Course

Scoring Multiple Choice TestsScoring Multiple Choice Tests

How should answers be scored?• partial knowledge is allowed by multiple marks

Five reasonable axioms:1. Marking all or no answers gives nothing2. Answering correctly is better than answering incorrectly3. If correctness is unchanged, fewer marks are no worse4. Combining questions and answers changes nothing5. The expected outcome from guessing is zero

Page 75: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

75

CC 2008: Design Choices in A Compiler Course

0 if a = 0 or a = kscore = log(k/a) if one is correct

−a/(k−a)log(k/a) otherwise

A Singular Choice for Multiple ChoiceA Singular Choice for Multiple Choice

k = number of optionsa = number of marks

Page 76: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

76

CC 2008: Design Choices in A Compiler Course

Practicalities of Multiple Choice TestsPracticalities of Multiple Choice Tests

In each copy that is handed out:• permute the questions randomly• permute the answers for each question randomly

This allows close seatings:

Page 77: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

77

CC 2008: Design Choices in A Compiler Course

Separating StudentsSeparating Students

The multiple choice test works!

It separates final grades of group members:• up to two grades difference• correlates with TA predictions

Page 78: Design Choices in A Compiler Course - AUcs.au.dk/~mis/CC2008.pdf · CC 2008: Design Choices in A Compiler Course Definite Assigment: JLS Style ... mis

78

CC 2008: Design Choices in A Compiler Course

ConclusionsConclusions

Solve big problems using big toolsFocus on the middle-endDevelop a nifty infrastructureProvide lots of feedbackMake the students work really, really hardTry something novel and fun!

Thanks to:Aske Simon ChristensenJohnni WintherJanus Dam Nielsen