Compilation 2007 The What and Why of Compilers Michael I. Schwartzbach BRICS, University of Aarhus.

Slides:



Advertisements
Similar presentations
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Gerardo Schneider Department of Informatics University of Oslo December 2008.
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
CSc 453 Compilers & Systems Software Saumya Debray The University of Arizona Tucson, AZ
CS105 INTRODUCTION TO COMPUTER CONCEPTS INTRO TO PROGRAMMING Instructor: Cuong (Charlie) Pham.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Comp 205: Comparative Programming Languages Imperative Programming Languages Functional Programming Languages Semantics Other Paradigms Lecture notes,
Summer 02-03Programming Language Concepts1 Programming Language Concepts (CS 360) Lecture 1: Overview, Grammars, and Little Languages Jeremy R. Johnson.
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
1 Programming Languages Marjan Sirjani 2 1- The Study of Programming Languages The purpose of language is simply that it must convey meaning. (Confucius)
CS 415: Programming Languages Chapter 1 Aaron Bloomfield Fall 2005.
Programming Languages Lecture 12. What is Programming?  The process of telling the computer what to do  Also known as coding.
History of Programming Languages
Introduction to Programming Language CS105 Programming Language First-generation: Machine language Second-generation: Assembly language Third-generation:
High-Level Programming Languages: C++
(1.1) COEN 171 Programming Languages Winter 2000 Ron Danielson.
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
Chapter 1. Introduction.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
COMPUTER PROGRAMS AND LANGUAGES Chapter 4. Developing a computer program Programs are a set (series) of instructions Programmers determine The instructions.
©Xiaoying Gao, Peter Andreae First Java Program COMP 102 #2 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
First Java Program COMP 102 #2 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
By Neng-Fa Zhou1 Evolution of programming languages –Machine language –Assembly language –Sub-routines and loop (Fortran) –Procedures and recursion (Algol,
1 Introduction to JVM Based on material produced by Bill Venners.
1 Programming Language History and Evolution In Text: Chapter 2.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Analysis of Programming Language Vladimir Viies Lembit Jürimägi Tallinna.
CSc 453 Compilers & Systems Software Saumya Debray The University of Arizona Tucson, AZ
Language Translation A programming language processor is any system that manipulates programs expressed in a PL A source program in some source language.
Programming Languages
Copyright © Genetic Computer School 2008 Computer Systems Architecture SA 7- 0 Lesson 7 Memory Management.
Chap. 10, Intermediate Representations J. H. Wang Dec. 14, 2015.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Programming Languages
Introduction to Compilers Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY.
int k = Integer.MAX_VALUE; k++; int k = Integer.MAX_VALUE; k++; What happens when the following code executes? byte b = someFile.readByte(); b = (byte)(b.
CPS120: Introduction to Computer Science Variables and Constants.
Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.
CSCE 314 Programming Languages
20-Feb-16 javap. Bytecode Java is compiled into bytecode, which is intermediate between Java and a “real” assembly language To implement Java, it is only.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
CSC 4181 Compiler Construction
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Programming Language Theory 2014, 1 Chapter 1 :: Introduction Origin : Michael L. Scott School of Computer & Information Engineering,
Welcome! Simone Campanoni
Objective of the course Understanding the fundamentals of the compilation technique Assist you in writing you own compiler (or any part of compiler)
a medium allowing humans and computers to communicate an abstraction of the real world a notation for expressing algorithms the set of all syntactically.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Programming Language History and Evolution
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Concepts of Programming Languages
Problem Solving Using C: Orientation & Lecture 1
Comp 205: Comparative Programming Languages
课程名 编译原理 Compiling Techniques
Programming Language History and Evolution
Evolution of programming languages
Chap. 6 :: Control Flow Michael L. Scott.
Problem Solving.
CS105 Introduction to Computer Concepts Intro to programming
Overview of Programming Paradigms
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
School of Computer & Information Engineering,
A Tour of Language Implementation
CS105 Introduction to Computer Concepts Intro to programming
Presentation transcript:

Compilation 2007 The What and Why of Compilers Michael I. Schwartzbach BRICS, University of Aarhus

2 The What and Why of Compilers What is a Compiler?  A program that: translates from one programming language to another preserves the semantics (in some sense)  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

3 The What and Why of Compilers New Programming Languages

4 The What and Why of Compilers Famous Programming Languages 1952 Autocoder 1956 IPL 1954 Fortran 1958 Lisp 1960 Algol 1960 Cobol 1964 APL 1964 PL/ BCPL 1967 Simula Snobol Algol Pascal 1970 SQL 1971 Forth 1972 Prolog 1972 Smalltalk 1973 C 1973 ML 1975 Scheme 1979 Modula Ada 1981 C SML 1985 Eiffel 1987 Caml 1987 Perl 1988 Oberon 1990 Haskell 1991 Python 1994 Dylan 1994 Java 1997 JavaScript 1997 Ruby 1998 Erlang 2000 C# 2001 AspectJ 2006 C 

5 The What and Why of Compilers Famous Programming Languages 1952 Autocoder 1956 IPL 1954 Fortran 1958 Lisp 1960 Algol 1960 Cobol 1964 APL 1964 PL/ BCPL 1967 Simula Snobol Algol Pascal 1970 SQL 1971 Forth 1972 Prolog 1972 Smalltalk 1973 C 1973 ML 1975 Scheme 1979 Modula Ada 1981 C SML 1985 Eiffel 1987 Caml 1987 Perl 1988 Oberon 1990 Haskell 1991 Python 1994 Dylan 1994 Java 1997 JavaScript 1997 Ruby 1998 Erlang 2000 C# 2001 AspectJ 2006 C 

6 The What and Why of Compilers Domain-Specific Languages  Extend software design  Are concrete artifacts that permit better: representation optimization analysis  Replace low-level programs and libraries  Exist in tens of thousands  Require full-scale compiler technology

7 The What and Why of Compilers Some Interesting DSL Compilers  LaTeX document descriptions → PostScript  Esterel reactive systems → hardware designs  BPEL4WS flow algebras → Web services  Nyquist musical compositions → sounds  SQL queries → evaluation plans

8 The What and Why of Compilers The FORTRAN Compiler  Implemented in 1957  The world's first compiler  Motivated by the economics of programming  Had to overcome deep skepticism  Focused on efficiency of the generated code  Pioneered many concepts and techniques  Revolutionized computer programming

9 The What and Why of Compilers How Good Are Compilers? /* naive */ for (i = 0; i < N; i++) { a[i] = a[i] * 2000; a[i] = a[i] / 10000; } /* expert */ b = a; for (i = 0; i < N; i++) { *b = *b * 2000; *b = *b / 10000; b++; }

10 The What and Why of Compilers Better Than We Are! /* naive */ for (i = 0; i < N; i++) a[i] = a[i] * 2000; a[i] = a[i] / 10000; } /* expert */ b = a; for (i = 0; i < N; i++) *b = *b * 2000; *b = *b / 10000; b++; } looplevelsparcmipsalpha naive naive -O naive -O expert expert -O expert -O

11 The What and Why of Compilers Better Than We Are! /* naive */ for (i = 0; i < N; i++) a[i] = a[i] * 2000; a[i] = a[i] / 10000; } /* expert */ b = a; for (i = 0; i < N; i++) *b = *b * 2000; *b = *b / 10000; b++; } looplevelsparcmipsalpha naive naive -O naive -O expert expert -O expert -O

12 The What and Why of Compilers Phases of Modern Compilers  preprocessing  scanning  parsing  desugaring  weeding  environments  linking  type checking  static analysis  resource allocation  code generation  code optimization  code emission  assembly

13 The What and Why of Compilers Compiler Architecure  Compiler phases suggest a modular design  Aspect-oriented programming is elegant  Many phases can be generated automatically  Tools range from industrial to experimental  Compilers divide into frontends and backends  Both may be retargeted

14 The What and Why of Compilers A Tiny Java Method public void printNumber(int number, int base) throws Exception { String ns = ""; if (number == 0) { ns = "0"; } else { while (number > 0) { ns = (number % base) + ns; number = number / base; } System.out.print(ns+"\n"); return; }

15 The What and Why of Compilers Stream of Tokens keyword: public keyword: void identifier: printNumber symbol: ( keyword: int identifier: number symbol:, keyword: int identifier: base symbol: ) keyword: throws identifier: Exception symbol: { identifier: String identifier: ns symbol: = constant: "" symbol: ; keyword: if symbol: ( identifier: number symbol: == constant: 0 symbol: ) symbol: { identifier: ns symbol: = constant: "0" symbol: ; symbol: } keyword: else symbol: { keyword: while symbol: ( identifier: number symbol: > constant: 0 symbol: ) symbol: { identifier: ns symbol: = symbol: ( identifier: number symbol: % identifier: base symbol: ) symbol: + identifier: ns symbol: ; identifier: number symbol: = identifier: number symbol: / identifier: base symbol: ; symbol: }...

16 The What and Why of Compilers Abstract Syntax Tree method printNumber type void args int number int base sequence decl type String name ns const "" if == lvalue number const 0 assign lvalue ns const "0" while > lvalue number const 0 sequence assign lvalue ns + % lvalue number lvalue base lvalue ns lvalue number / lvalue number lvalue base System.out..print + lvalue ns const \n

17 The What and Why of Compilers Name and Type Linking method printNumber type void args int number int base sequence decl type Strin g name ns const "" if == lvalue number const 0 assign lvalue ns const "0" while > lvalue number const 0 sequence assign lvalue ns + % lvalue number lvalue base lvalue ns lvalue number / lvalue number lvalue base System.out..print + lvalue ns const \n

18 The What and Why of Compilers Type Checking method printNumber type void args int number int base sequence decl type String name ns const "" if == lvalue number const 0 assign lvalue ns const "0" while > lvalue number const 0 sequence assign lvalue ns concat % lvalue number lvalue base lvalue ns lvalue number / lvalue number lvalue base System.out..print concat lvalue ns const \n int String int boolean String int

19 The What and Why of Compilers Type Checking Transformations method printNumber type void args int number int base sequence decl type String name ns const "" if == lvalue number const 0 assign lvalue ns const "0" while > lvalue number const 0 sequence assign lvalue ns % lvalue number lvalue base lvalue ns lvalue number / lvalue number lvalue base System.out..print lvalue ns const \n int String int boolean String int concat Operator + is transformed into concat

20 The What and Why of Compilers Resource Allocation method printNumber type void args int number int base sequence decl type String name ns const "" if == lvalue number const 0 assign lvalue ns const "0" while > lvalue number const 0 sequence assign lvalue ns concat % lvalue number lvalue base lvalue ns lvalue number / lvalue number lvalue base System.out..print concat+ lvalue ns const \n int String int boolean String int 12 3

21 The What and Why of Compilers Bytecode Generation.method public printNumber(II)V.throws java/lang/Exception.limit stack 4.limit locals 4 ldc "" dup astore_3 pop iload_1 iconst_0 if_icmpeq true_2... stop_1: aload_3 invokestatic java/lang/String/valueOf(Ljava/lang/Object;)Ljava/lang/String; ldc "\012" invokevirtual java/lang/String/concat(Ljava/lang/String;)Ljava/lang/String; getstatic java/lang/System/out Ljava/io/PrintStream; swap invokevirtual java/io/PrintStream/print(Ljava/lang/Object;)V return.end method 64 instructions

22 The What and Why of Compilers Bytecode Optimization.method public printNumber(II)V.throws java/lang/Exception.limit stack 4.limit locals 4 ldc "" astore_3 iload_1 iconst_0 if_icmpne start_4 ldc "0" astore_3... stop_1: aload_3 invokestatic java/lang/String/valueOf(Ljava/lang/Object;)Ljava/lang/String; ldc "\012" invokevirtual java/lang/String/concat(Ljava/lang/String;)Ljava/lang/String; getstatic java/lang/System/out Ljava/io/PrintStream; swap invokevirtual java/io/PrintStream/print(Ljava/lang/Object;)V return.end method 40 instructions

23 The What and Why of Compilers Code Assembly printNumber.class: cafe babe d a f 6c61 6e67 2f49 6e a f 6c61 6e67 2f4f 626a c69 6e69 743e c b c 6a f6c 616e 672f e67 3b29 4c6a f 6c61 6e67 2f e 673b 0c e0a 001f f6e f a53 6f c c00 0c a f6c 616e 672f e e 756c 6c a e f e 672e 6a01 000b e 744e 756d f 496e f e 670c f f e 670a a00 1f c6a f 6c61 6e67 2f e 673b c b d a b700 14b d c e 571b 039f a b 121b 594e 57a b03 a a abb 001f 591b 1c70 b700 0ab6 001d 2d59 c600 06a b6 001a 594e 571b 1c6c 593c 57a7 ffcf b1b e

24 The What and Why of Compilers Bootstrapping Compilers (1/4)  We are given: a machine language M a programming language L  We need a compiler: C[L → M; M]  The direct approach is hard and difficult source target implementation

25 The What and Why of Compilers Bootstrapping Compilers (2/4)  We define: L  is a simple subset of L M  is naive and inefficient M code  It is easy to implement: C 1 [L  → M  ; M  ]  And in parallel: C 2 [L →M; L  ]

26 The What and Why of Compilers Bootstrapping Compilers (3/4)  Combining the two compilers we get: C 1 (C 2 )[L →M; M  ]  A final combination gives us what we want: (C 1 (C 2 ))(C 2 )[L → M; M]

27 The What and Why of Compilers Bootstrapping Compilers (4/4)  We now want a compiler for another language K  We first program a compiler: C 3 [K →M; L]  The new compiler is then obtained as: ((C 1 (C 2 ))(C 2 ))(C 3 )[K → M; M]  And so on...