What is a Compiler? What is a Compiler? � A program that: Compilation 2007 Compilation 2007 • translates from one programming language to another The What and Why of What and Why of The • preserves the semantics (in some sense) Compilers Compilers � Each language must have at least one compiler � It is often the case that: • the source language is high-level • the target language is low-level Michael I. Schwartzbach BRICS, University of Aarhus The What and Why of Compilers 2 New Programming Languages 1952- -2006 2006 Famous Programming Languages New Programming Languages 1952 Famous Programming Languages http://hopl.murdoch.edu.au/ 1952 Autocoder 1970 SQL 1987 Perl 350 1956 IPL 1971 Forth 1988 Oberon 300 1954 Fortran 1972 Prolog 1990 Haskell 1958 Lisp 1972 Smalltalk 1991 Python 250 1960 Algol 1973 C 1994 Dylan 200 1960 Cobol 1973 ML 1994 Java 1964 APL 1975 Scheme 1997 JavaScript 150 1964 PL/1 1979 Modula-2 1997 Ruby 100 1965 BCPL 1980 Ada 1998 Erlang 1967 Simula 67 1981 C++ 2000 C# 50 1967 Snobol 4 1984 SML 2001 AspectJ 0 1968 Algol 68 1985 Eiffel 2006 C ω 1952 1979 1988 2006 1970 Pascal 1987 Caml The What and Why of Compilers 3 The What and Why of Compilers 4 1
Famous Programming Languages Domain- -Specific Languages Specific Languages Famous Programming Languages Domain 1952 Autocoder 1970 SQL 1987 Perl � Extend software design 1956 IPL 1971 Forth 1988 Oberon � Are concrete artifacts that permit better: 1954 Fortran 1972 Prolog 1990 Haskell 1958 Lisp 1972 Smalltalk 1991 Python • representation 1960 Algol 1973 C 1994 Dylan • optimization 1960 Cobol 1973 ML 1994 Java • analysis 1964 APL 1975 Scheme 1997 JavaScript � Replace low-level programs and libraries 1964 PL/1 1979 Modula-2 1997 Ruby 1965 BCPL 1980 Ada 1998 Erlang � Exist in tens of thousands 1967 Simula 67 1981 C++ 2000 C# � Require full-scale compiler technology 1967 Snobol 4 1984 SML 2001 AspectJ 1968 Algol 68 1985 Eiffel 2006 C ω 1970 Pascal 1987 Caml The What and Why of Compilers 5 The What and Why of Compilers 6 Some Interesting DSL Compilers The FORTRAN Compiler Some Interesting DSL Compilers The FORTRAN Compiler � LaTeX � Implemented in 1957 • document descriptions → PostScript � The world's first compiler � Esterel � Motivated by the economics of programming • reactive systems → hardware designs � Had to overcome deep skepticism � BPEL4WS � Focused on efficiency of the generated code • flow algebras → Web services � Pioneered many concepts and techniques � Nyquist � Revolutionized computer programming • musical compositions → sounds � SQL • queries → evaluation plans The What and Why of Compilers 7 The What and Why of Compilers 8 2
How Good Are Compilers? Better Than We Are! How Good Are Compilers? Better Than We Are! /* naive */ /* naive */ loop level sparc mips alpha for (i = 0; i < N; i++) { for (i = 0; i < N; i++) a[i] = a[i] * 2000; a[i] = a[i] * 2000; naive 20.5 21.6 7.9 a[i] = a[i] / 10000; a[i] = a[i] / 10000; naive 8.8 12.3 3.3 -O1 } } naive -O2 7.9 11.2 3.0 /* expert */ /* expert */ expert 19.5 17.6 7.6 b = a; b = a; expert -O1 12.4 15.4 4.1 for (i = 0; i < N; i++) { for (i = 0; i < N; i++) *b = *b * 2000; *b = *b * 2000; expert -O2 10.7 12.9 3.9 *b = *b / 10000; *b = *b / 10000; b++; b++; } } The What and Why of Compilers 9 The What and Why of Compilers 10 Better Than We Are! Phases of Modern Compilers Better Than We Are! Phases of Modern Compilers /* naive */ � preprocessing � type checking loop level sparc mips alpha for (i = 0; i < N; i++) a[i] = a[i] * 2000; � scanning � static analysis naive 20.5 21.6 7.9 a[i] = a[i] / 10000; � parsing � resource allocation naive 8.8 12.3 3.3 -O1 } � desugaring � code generation naive -O2 7.9 11.2 3.0 /* expert */ � weeding � code optimization expert 19.5 17.6 7.6 b = a; expert 12.4 15.4 4.1 � environments � code emission -O1 for (i = 0; i < N; i++) *b = *b * 2000; expert -O2 10.7 12.9 3.9 � linking � assembly *b = *b / 10000; b++; } The What and Why of Compilers 11 The What and Why of Compilers 12 3
Compiler Architecure A Tiny Java Method Compiler Architecure A Tiny Java Method public void printNumber(int number, int base) � Compiler phases suggest a modular design throws Exception { String ns = ""; � Aspect-oriented programming is elegant if (number == 0) { ns = "0"; � Many phases can be generated automatically } else { while (number > 0) { � Tools range from industrial to experimental ns = (number % base) + ns; number = number / base; } � Compilers divide into frontends and backends } � Both may be retargeted System.out.print(ns+"\n"); return; } The What and Why of Compilers 13 The What and Why of Compilers 14 Stream of Tokens Abstract Syntax Tree Stream of Tokens Abstract Syntax Tree keyword: public symbol: ( symbol: { method printNumber keyword: void identifier: number identifier: ns args identifier: printNumber symbol: == symbol: = type void symbol: ( constant: 0 symbol: ( sequence int int keyword: int symbol: ) identifier: number number base identifier: number symbol: { symbol: % if decl symbol: , identifier: ns identifier: base System.out..print keyword: int symbol: = symbol: ) == type const + identifier: base constant: "0" symbol: + assign while String "" symbol: ) symbol: ; identifier: ns lvalue const name keyword: throws symbol: } symbol: ; lvalue lvalue ns \n const const > sequence ns number ns identifier: Exception keyword: else identifier: number 0 "0" symbol: { symbol: { symbol: = assign lvalue const assign identifier: String keyword: while identifier: number number 0 lvalue identifier: ns symbol: ( symbol: / / + number symbol: = identifier: number identifier: base lvalue ns constant: "" symbol: > symbol: ; % lvalue lvalue lvalue ns symbol: ; constant: 0 symbol: } number base keyword: if symbol: ) ... lvalue lvalue number base The What and Why of Compilers 15 The What and Why of Compilers 16 4
Name and Type Linking Type Checking Name and Type Linking Type Checking method method printNumber printNumber args args type type void void sequence int int sequence int int number base number base if if decl decl System.out..print System.out..print == == boolean type const + type const concat assign while assign while String "" String "" Strin String g lvalue const lvalue const name name lvalue lvalue lvalue lvalue ns \n ns \n const const > sequence const const > sequence ns ns number ns number ns boolean String String 0 "0" 0 "0" int String int String assign assign lvalue const assign lvalue const assign int number 0 number 0 String lvalue lvalue / int int / + concat number number String int lvalue lvalue int ns ns % lvalue % lvalue lvalue lvalue lvalue lvalue String ns ns int number base number base String int int lvalue lvalue lvalue lvalue number base number base int int The What and Why of Compilers 17 The What and Why of Compilers 18 Type Checking Transformations Resource Allocation Type Checking Transformations Resource Allocation method method printNumber printNumber args args type type void void 1 2 sequence int int sequence int int number base number base if if decl decl System.out..print System.out..print == == type const boolean concat type const boolean concat+ assign while assign while String String String "" String "" String String lvalue const lvalue const 3 name name lvalue lvalue lvalue lvalue ns \n ns \n const const > sequence const const > sequence ns ns number ns number ns boolean String String boolean String String 0 "0" 0 "0" int String int String int String int String assign assign lvalue const assign lvalue const assign int int number 0 String number 0 String lvalue lvalue int int / int int / concat concat number number String int String int lvalue int lvalue int ns ns % lvalue % lvalue lvalue lvalue lvalue lvalue String String ns ns int int number base number base Operator + is transformed into concat String String int int int int lvalue lvalue lvalue lvalue number base number base int int int int The What and Why of Compilers 19 The What and Why of Compilers 20 5
Recommend
More recommend