Compilation 2012 The What and Why of Compilers

Similar documents
Concepts in Programming Languages

Topic I. Introduction and motivation References: Chapter 1 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Languages october 22, 2017 Éric Lévénez < FORTRAN III end-1958 FORTRAN II FORTRAN I october 1956

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Programming Languages, Summary CSC419; Odelia Schwartz

Introduction to Computer Science I

A Tour of Language Implementation

Principles in Programming: Orientation & Lecture 1. SWE2004: Principles in Programming Spring 2014 Euiseong Seo

Low-Level Languages. Computer Programs and Programming Languages

Compilation 2012 Code Generation

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

Programming Language Concepts 1982, 1987, Outline. Period

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

Lecture 1: Course Introduction

Programming Languages

COMP 201: Principles of Programming

Welcome to Introduction to Computer Science I

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

02 B The Java Virtual Machine

Com S 541. Programming Languages I

Lecture 09. Ada to Software Engineering. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

CSCI 3136 Principles of Programming Languages

Why are there so many programming languages?

Introduction. A. Bellaachia Page: 1

Introduction to Programming Languages. CSE 307 Principles of Programming Languages Stony Brook University

Principles in Programming: Orientation & Lecture 1. SWE2004: Principles in Programming Spring 2015 Euiseong Seo

Lecture 1: Course Introduction

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

Outline. Introduction to Programming (in C++) Introduction. First program in C++ Programming examples

LECTURE 18. Control Flow

Discovering Computers 2008

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

Introduction to Scientific Computing Languages

Introduction to Scientific Computing Languages

Expressions and Assignment

Compilation

The Java Virtual Machine

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

COMP3131/9102: Programming Languages and Compilers

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Names, Scopes, and Bindings II. Hwansoo Han

PLAGIARISM. Administrivia. Course home page: Introduction to Programming Languages and Compilers

Introduction to Programming Languages and Compilers. CS164 11:00-12:00 MWF 306 Soda

CS A331 Programming Language Concepts

CS 3304 Comparative Languages. Lecture 1: Introduction

Discovering Computers Chapter 13 Programming Languages and Program Development

Final-Term Papers Solved MCQS with Reference

Language Translation, History. CS152. Chris Pollett. Sep. 3, 2008.

Programmiersprachen (Programming Languages)

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

Compilation I. Hwansoo Han

CS2303 C14 Systems Programming Concepts. Bob Kinicki

CSc 520. Course Outline (Subject to change) Course Outline (Subject to change)... Principles of Programming Languages. Christian Collberg

Under the Hood: The Java Virtual Machine. Lecture 23 CS2110 Fall 2008

Compilers and interpreters

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Chapter 2 Preview. Preview. History of Programming Languages. History of Programming Languages. History of Programming Languages

Lecture 6 Introduction to Objects and Classes

22c:111 Programming Language Concepts. Fall Types I

COSC Software Engineering. Lecture 23: Multilingual and Component Programming

Run-time Program Management. Hwansoo Han

Programming Paradigms

COMP 520 Fall 2013 Code generation (1) Code generation

Chapter 5. Names, Bindings, and Scopes

Imperative Programming

Introduction to.net, C#, and Visual Studio. Part I. Administrivia. Administrivia. Course Structure. Final Project. Part II. What is.net?

Under the Hood: The Java Virtual Machine. Problem: Too Many Platforms! Compiling for Different Platforms. Compiling for Different Platforms

CSE 431S Final Review. Washington University Spring 2013

CSE 307: Principles of Programming Languages

HANDLING NONLOCAL REFERENCES

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Tutorial 3: Code Generation

Concepts of Programming Languages

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

Logic Programming II & Revision

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Concepts Introduced in Chapter 7

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

Programming Languages 1. Introduction. Oscar Nierstrasz

COSC 2P90 Programming Languages & Object-Orientation

Modern Stored Procedures Using GraalVM

Chapter 5 Names, Binding, Type Checking and Scopes

Types and Type Inference

Course outline. CSE 341: Programming Languages. Why study programming languages? Course motivation and objectives. 1 lecture: Concepts

Thanks! Review. Course Goals. General Themes in this Course. There are many programming languages. Teaching Assistants. John Mitchell.

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

CS508-Modern Programming Solved MCQ(S) From Midterm Papers (1 TO 22 Lectures) BY Arslan

Where do we go from here?

CSc 372 Comparative Programming Languages

Compilation 2009 Domain-Specific Languages Syntax Extensions

Space Exploration EECS /25

CSC 533: Organization of Programming Languages. Spring 2005

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

CS321 Languages and Compiler Design I. Winter 2012 Lecture 1

Compiler Construction D7011E

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

COMPILER DESIGN LECTURE NOTES

Transcription:

Compilation 2012 The What and Why of Compilers Jan Midtgaard Michael I. Schwartzbach Aarhus University

What is a Compiler? A program that: tralates from one programming language to another preserves the semantics (in some see) a compiler also implicitly defines a semantics but it is hard to reason about programs in terms of a compiler-d semantics Each language must have at least one compiler (or erpreter) It is often the case that: the source language is high-level the target language is low-level 2

New Programming Languages 1952-2006 350 http://hopl.murdoch.edu.au/ 300 250 200 150 100 50 0 1952 1969 1988 2006 3

Famous Programming Languages 1952 Autocoder 1956 IPL 1954 Fortran 1958 Lisp 1960 Algol 1960 Cobol 1964 APL 1964 PL/1 1965 BCPL 1967 Simula 67 1967 Snobol 4 1968 Algol 68 1970 Pascal 1970 SQL 1971 Forth 1972 Prolog 1972 Smalltalk 1973 C 1973 ML 1975 Scheme 1979 Modula-2 1980 Ada 1981 C++ 1984 SML 1985 Eiffel 1987 Caml 1987 Perl 1988 Oberon 1990 Haskell 1991 Python 1994 Dylan 1994 Java 1996 OCaml 1997 JavaScript 1997 Ruby 1998 Erlang 2000 C# 2001 AspectJ 2006 C 4

Famous Programming Languages 1952 Autocoder 1956 IPL 1954 Fortran 1958 Lisp 1960 Algol 1960 Cobol 1964 APL 1964 PL/1 1965 BCPL 1967 Simula 67 1967 Snobol 4 1968 Algol 68 1970 Pascal 1970 SQL 1971 Forth 1972 Prolog 1972 Smalltalk 1973 C 1973 ML 1975 Scheme 1979 Modula-2 1980 Ada 1981 C++ 1984 SML 1985 Eiffel 1987 Caml 1987 Perl 1988 Oberon 1990 Haskell 1991 Python 1994 Dylan 1994 Java 1996 OCaml 1997 JavaScript 1997 Ruby 1998 Erlang 2000 C# 2001 AspectJ 2006 C 5

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

Some Interesting DSL Compilers LaTeX document descriptio PostScript Esterel reactive systems hardware desig WS-BPEL flow algebras Web services Nyquist musical compositio sounds SQL queries evaluation pla 7

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 8

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++; } 9

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++; } loop level sparc mips alpha naive 20.5 21.6 7.9 naive -O1 8.8 12.3 3.3 naive -O2 7.9 11.2 3.0 expert 19.5 17.6 7.6 expert -O1 12.4 15.4 4.1 expert -O2 10.7 12.9 3.9 10

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++; } loop level sparc mips alpha naive 20.5 21.6 7.9 naive -O1 8.8 12.3 3.3 naive -O2 7.9 11.2 3.0 expert 19.5 17.6 7.6 expert -O1 12.4 15.4 4.1 expert -O2 10.7 12.9 3.9 11

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

Compiler Architecure Compiler phases suggest a modular design: one phase = one module Many phases can be generated automatically Tools range from industrial to experimental Compilers divide o frontends and backends Both may be retargeted 13

A Tiny Java Method public void prnumber(, ) throws Exception { = ""; if ( == 0) { = "0"; } else { while ( > 0) { = ( % ) + ; = / ; } } System.out.pr(+"\n"); return; } 14

Stream of Toke keyword: public keyword: void identifier: prnumber symbol: ( keyword: identifier: symbol:, keyword: identifier: symbol: ) keyword: throws identifier: Exception symbol: { identifier: identifier: symbol: = ant: "" symbol: ; keyword: if symbol: ( identifier: symbol: == ant: 0 symbol: ) symbol: { identifier: symbol: = ant: "0" symbol: ; symbol: } keyword: else symbol: { keyword: while symbol: ( identifier: symbol: > ant: 0 symbol: ) symbol: { identifier: symbol: = symbol: ( identifier: symbol: % identifier: symbol: ) symbol: + identifier: symbol: ; identifier: symbol: = identifier: symbol: / identifier: symbol: ; symbol: }... 15

Abstract Syntax Tree method prnumber args type void sequence decl if System.out..pr type "" == while + name 0 "0" > sequence \n 0 + / % 16

Name and Type Linking method prnumber args type void sequence decl if System.out..pr type "" == while + name 0 "0" > sequence \n 0 + / % 17

18 Type Checking method prnumber type void args sequence decl type name "" if == 0 "0" while > 0 sequence concat % / System.out..pr concat \n boolean boolean

Type Checking Traformatio method prnumber args type void sequence decl if System.out..pr == type "" boolean while concat name 0 "0" > boolean sequence \n 0 concat / Operator + is traformed o concat % 19

20 Resource Allocation method prnumber type void args sequence decl type name "" if == 0 "0" while > 0 sequence concat % / System.out..pr concat+ \n boolean boolean 1 2 3

Bytecode Generation.method public prnumber(ii)v.throws java/lang/exception.limit stack 4.limit locals 4 ldc "" dup astore_3 pop 64 itructio iload_1 i_0 if_icmpeq true_2... stop_1: aload_3 invokestatic java/lang//valueof(ljava/lang/object;)ljava/lang/; ldc "\012" invokevirtual java/lang//concat(ljava/lang/;)ljava/lang/; getstatic java/lang/system/out Ljava/io/PrStream; swap invokevirtual java/io/prstream/pr(ljava/lang/object;)v return.end method 21

Bytecode Optimization.method public prnumber(ii)v.throws java/lang/exception.limit stack 4.limit locals 4 ldc "" astore_3 iload_1 i_0 40 itructio if_icmpne start_4 ldc "0" astore_3... stop_1: aload_3 invokestatic java/lang//valueof(ljava/lang/object;)ljava/lang/; ldc "\012" invokevirtual java/lang//concat(ljava/lang/;)ljava/lang/; getstatic java/lang/system/out Ljava/io/PrStream; swap invokevirtual java/io/prstream/pr(ljava/lang/object;)v return.end method 22

Code Assembly prnumber.class: cafe babe 0003 002d 0023 0100 116a 6176 612f 6c61 6e67 2f49 6e74 6567 6572 0100 106a 6176 612f 6c61 6e67 2f4f 626a 6563 7401 0005 2849 4929 5601 0006 3c69 6e69 743e 0700 020c 0004 000b 0700 1101 0026 284c 6a61 7661 2f6c 616e 672f 5374 7269 6e67 3b29 4c6a 6176 612f 6c61 6e67 2f53 7472 696e 673b 0c00 1900 1e0a 001f 0018 0100 0328 2956 0100 0663 6f6e 6361 7401 0004 436f 6465 0100 0a53 6f75 7263 6546 696c 6501 0004 2849 2956 0c00 0c00 0801 0010 6a61 7661 2f6c 616e 672f 5374 7269 6e67 0100 046e 756c 6c08 0012 0a00 0500 0601 0011 496e 7465 6765 7254 6f53 7472 696e 672e 6a01 000b 7072 696e 744e 756d 6265 7201 000f 496e 7465 6765 7254 6f53 7472 696e 670c 0004 000f 0100 0874 6f53 7472 696e 670a 0007 0010 0800 2007 0017 0a00 1f00 0901 0014 2829 4c6a 6176 612f 6c61 6e67 2f53 7472 696e 673b 0700 0101 0001 3008 0022 0100 0000 2100 1c00 0500 0000 0000 0200 0100 0400 0b00 0100 0d00 0000 1100 0100 0100 0000 052a b700 14b1 0000 0000 0001 0016 0003 0001 000d 0000 005c 0004 0004 0000 0050 1221 594e 571b 039f 0007 03a7 0004 0499 000b 121b 594e 57a7 0037 1b03 a300 0703 a700 0404 9900 2abb 001f 591b 1c70 b700 0ab6 001d 2d59 c600 06a7 0006 5712 13b6 001a 594e 571b 1c6c 593c 57a7 ffcf b1b1 0000 0000 0001 000e 0000 0002 0015 23

Virtual Machines and Compilers Bytecode is just an ermediate representation of the program The VM is part of the compilation process But it is useful to separate the compilation process o (front-end) compiler and VM N languages and M platforms require only N (front-end) compilers and M virtual machines, rather than N*M monolithic compilers Enables adaptive optimization techniques VMs are quite complex pieces of software, too Have their own sophisticated architecture Register allocation, memory management, adaptive optimization, But in this course we will not say much about how VMs work

Bootstrapping Compilers (1/4) We are given: a machine language M a programming language L We need a compiler: source (this is called a T-diagram) L M M target implementation The direct approach is hard and difficult 25

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: L M M And in parallel: L M L 26

Bootstrapping Compilers (3/4) Combining the two compilers we get: L M L M input program L L M M output program M A final combination gives us what we want: L M L M L L M M M 27

Bootstrapping Compilers (4/4) We now want a compiler for another language K We first program a compiler: K M L The new compiler is then obtained as: K M K M L L M M And so on... M 28