Compiler Design 1. Introduction to Programming Language Design and to Compilation

Similar documents
1DL321: Kompilatorteknik I (Compiler Design 1) Introduction to Programming Language Design and to Compilation

1DL321: Kompilatorteknik I (Compiler Design 1)

1DL321: Kompilatorteknik I (Compiler Design 1) Introduction to Programming Language Design and to Compilation

Introduction to Programming Languages and Compilers. CS164 11:00-12:30 TT 10 Evans. UPRM ICOM 4029 (Adapted from: Prof. Necula UCB CS 164)

PLAGIARISM. Administrivia. Compilers. CS143 11:00-12:15TT B03 Gates. Text. Staff. Instructor. TAs. Office hours, contact info on 143 web site

Administrivia. Compilers. CS143 10:30-11:50TT Gates B01. Text. Staff. Instructor. TAs. The Purple Dragon Book. Alex Aiken. Aho, Lam, Sethi & Ullman

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

Programming Language Design and Implementation. Cunning Plan. Your Host For The Semester. Wes Weimer TR 9:30-10:45 MEC 214. Who Are We?

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

ICOM 4036 Spring 2004

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Compilers. Lecture 2 Overview. (original slides by Sam

Introduction. Compilers and Interpreters

Programming Language Design and Implementation. Wes Weimer MW 2:00 3:15 Thornton E-303

Introduction to Compilers

Compilers and Interpreters

CS131: Programming Languages and Compilers. Spring 2017

Principles of Programming Languages. Lecture Outline

Introduction to Lexical Analysis

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

Lecture 1: Course Introduction

Lecture 1: Course Introduction

trends in programming languages

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

Compilers. Introduction. Yannis Smaragdakis, U. Athens (original slides by Sam

Introduction to Lexical Analysis

Formal Languages and Compilers Lecture I: Introduction to Compilers

CS 415 Midterm Exam Spring 2002

Lexical Analysis. Lecture 2-4

Compilers. Prerequisites

Syntax. A. Bellaachia Page: 1

Chapter 1 Preliminaries

Chapter 1. Preliminaries

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Pioneering Compiler Design

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

CS321 Languages and Compiler Design I. Winter 2012 Lecture 1

Compiling Techniques

Introduction. CS 2210 Compiler Design Wonsun Ahn

Programmiersprachen (Programming Languages)

CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Introduction to Compiler Construction

Administrivia. Lexical Analysis. Lecture 2-4. Outline. The Structure of a Compiler. Informal sketch of lexical analysis. Issues in lexical analysis

Introduction to Compiler Construction

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

Introduction to Compiler Construction

CS 4120 and 5120 are really the same course. CS 4121 (5121) is required! Outline CS 4120 / 4121 CS 5120/ = 5 & 0 = 1. Course Information

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Lecture 16: Static Semantics Overview 1

Lexical Analysis. Chapter 2

Semester Review CSC 301

Lexical Analysis. Lecture 3-4

CS 406/534 Compiler Construction Putting It All Together

Software Design Fundamentals. CSCE Lecture 11-09/27/2016

CS 3360 Design and Implementation of Programming Languages. Exam 1

Chapter 1. Preliminaries

CS101 Introduction to Programming Languages and Compilers

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

LECTURE 3. Compiler Phases

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Concepts of Programming Languages

CSE 3302 Programming Languages Lecture 2: Syntax

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

CS 321 IV. Overview of Compilation

CS 415 Midterm Exam Spring SOLUTION

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

Chapter 1. Preview. Reason for Studying OPL. Language Evaluation Criteria. Programming Domains

Introduction. A. Bellaachia Page: 1

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

LESSON 13: LANGUAGE TRANSLATION

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

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

Compilers. Pierre Geurts

COS 140: Foundations of Computer Science

Programming Languages, Summary CSC419; Odelia Schwartz

Introduction to Compiler Design

A Continuation based Programming Language for Embedded Systems

Compiler Construction D7011E

Introduction to Parsing Ambiguity and Syntax Errors

CS 3360 Design and Implementation of Programming Languages. Exam 1

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

CS Exam #1-100 points Spring 2011

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

Introduction to Parsing Ambiguity and Syntax Errors

Outline. Programming Languages 1/16/18 PROGRAMMING LANGUAGE FOUNDATIONS AND HISTORY. Current

LECTURE NOTES ON COMPILER DESIGN P a g e 2

SLIDE 2. At the beginning of the lecture, we answer question: On what platform the system will work when discussing this subject?

Working of the Compilers

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

CMSC 350: COMPILER DESIGN

CS 113: Introduction to

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

CSCE 314 Programming Languages. Type System

CS 132 Compiler Construction

CS453 Compiler Construction

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Transcription:

Compiler Design 1 Introduction to Programming Language Design and to Compilation

Administrivia Lecturer: Kostis Sagonas (Hus 1, 352) Course home page: http://user.it.uu.se/~kostis/teaching/kt1-11 If you want to be enrolled in the course, send mail with your name and UU account to: kostis@it.uu.se Assistant: Stavros Aronis (stavros.aronis@it.uu.se) responsible for the lessons and the assignments Compiler Design 1 (2011) 2

Course Structure Course has theoretical and practical aspects Need both in programming languages! Written examination = theory (4 points) Assignments = practice (1 point) Electronic hand-in to the assistant before the corresponding deadline Compiler Design 1 (2011) 3

Course Literature Compiler Design 1 (2011) 4

Academic Honesty For assignments you are allowed to work in pairs (but no threesomes/foursomes/...) Don t use work from uncited sources Including old assignments PLAGIARISM Compiler Design 1 (2011) 5

The Compiler Project A follow-up course that will be taught by Sven-Olof Nyström in period 3 Compiler Design 1 (2011) 6

How are Languages Implemented? Two major strategies: Interpreters (older, less studied) Compilers (newer, much more studied) Interpreters run programs as is Little or no preprocessing Compilers do extensive preprocessing Compiler Design 1 (2011) 7

Language Implementations Batch compilation systems dominate gcc Some languages are primarily interpreted Java bytecode Postscript Some environments (e.g. Lisp) provide both Interpreter for development Compiler for production Compiler Design 1 (2011) 8

(Short) History of High-Level Languages 1953 IBM develops the 701 Till then, all programming done in assembly Problem: Software costs exceeded hardware costs! John Backus: Speedcoding An interpreter Ran 10-20 times slower than hand-written assembly Compiler Design 1 (2011) 9

FORTRAN I 1954 IBM develops the 704 John Backus Idea: translate high-level code to assembly Many thought this impossible Had already failed in other projects 1954-7 FORTRAN I project By 1958, >50% of all software is in FORTRAN Cut development time dramatically (2 weeks 2 hours) Compiler Design 1 (2011) 10

FORTRAN I The first compiler Produced code almost as good as hand-written Huge impact on computer science Led to an enormous body of theoretical work Modern compilers preserve the outlines of the FORTRAN I compiler Compiler Design 1 (2011) 11

The Structure of a Compiler 1. Lexical Analysis 2. Syntax Analysis 3. Semantic Analysis 4. IR Optimization 5. Code Generation 6. Low-level Optimization The first 3, at least, can be understood by analogy to how humans comprehend English. Compiler Design 1 (2011) 12

Lexical Analysis First step: recognize words. Smallest unit above letters This is a sentence. Note the Capital T (start of sentence symbol) Blank (word separator) Period. (end of sentence symbol) Compiler Design 1 (2011) 13

More Lexical Analysis Lexical analysis is not trivial. Consider: ist his ase nte nce Plus, programming languages are typically more cryptic than English: *p->f ++ = -.12345e-5 Compiler Design 1 (2011) 14

And More Lexical Analysis Lexical analyzer divides program text into words or tokens if (x == y) then z = 1; else z = 2; Units: if, (, x, ==, y, ), then, z, =, 1, ;, else, z, =, 2, ; Compiler Design 1 (2011) 15

Parsing Once words are understood, the next step is to understand the sentence structure Parsing = Diagramming Sentences The diagram is a tree Compiler Design 1 (2011) 16

Diagramming a Sentence (1) This line is a longer sentence article noun verb article adjective noun noun phrase noun phrase verb phrase sentence Compiler Design 1 (2011) 17

Diagramming a Sentence (2) This line is a longer sentence article noun verb article adjective noun subject object sentence Compiler Design 1 (2011) 18

Parsing Programs Parsing program expressions is the same Consider: If (x == y) then z = 1; else z = 2; Diagrammed: x == y z = 1 z = 2 relation predicate assignment then-stmt if-then-else assignment else-stmt Compiler Design 1 (2011) 19

Semantic Analysis Once sentence structure is understood, we can try to understand its meaning But meaning is too hard for compilers Most compilers perform limited analysis to catch inconsistencies Some optimizing compilers do more analysis to improve the performance of the program Compiler Design 1 (2011) 20

Semantic Analysis in English Example: Jack said Jerry left his assignment at home. What does his refer to? Jack or Jerry? Even worse: Jack said Jack left his assignment at home? How many Jacks are there? Which one left the assignment? Compiler Design 1 (2011) 21

Semantic Analysis in Programming Languages Programming languages define strict rules to avoid such ambiguities This C++ code prints 4 ; the inner definition is used { } int Jack = 3; { int Jack = 4; cout << Jack; } Compiler Design 1 (2011) 22

More Semantic Analysis Compilers perform many semantic checks besides variable bindings Example: Arnold left her homework at home. A type mismatch between her and Arnold; we know they are different people Presumably Arnold is male Compiler Design 1 (2011) 23

Optimization No strong counterpart in English, but akin to editing Automatically modify programs so that they Run faster Use less memory/power In general, conserve some resource more economically The compilers project has no optimization component for those interested there is KT2! Compiler Design 1 (2011) 24

Optimization Example X = Y * 0 is the same as X = 0 NO! Valid for integers, but not for floating point numbers Compiler Design 1 (2011) 25

Code Generation Produces assembly code (usually) A translation into another language Analogous to human translation Compiler Design 1 (2011) 26

Intermediate Languages Many compilers perform translations between successive intermediate forms All but first and last are intermediate languages internal to the compiler Typically there is one IL IL s generally ordered in descending level of abstraction Highest is source Lowest is assembly Compiler Design 1 (2011) 27

Intermediate Languages (Cont.) IL s are useful because lower levels expose features hidden by higher levels registers memory/frame layout etc. But lower levels obscure high-level meaning Compiler Design 1 (2011) 28

Issues Compiling is almost this simple, but there are many pitfalls Example: How are erroneous programs handled? Language design has big impact on compiler Determines what is easy and hard to compile Course theme: many trade-offs in language design Compiler Design 1 (2011) 29

Compilers Today The overall structure of almost every compiler adheres to our outline The proportions have changed since FORTRAN Early: lexical analysis, parsing most complex, expensive Today: semantic analysis and optimization dominate all other phases; lexing and parsing are well-understood and cheap Compiler Design 1 (2011) 30

Current Trends in Compilation Compilation for speed is less interesting. But: scientific programs advanced processors (Digital Signal Processors, advanced speculative architectures, GPUs) Ideas from compilation used for improving code reliability: memory safety detecting data races... Compiler Design 1 (2011) 31

Programming Language Economics Programming languages are designed to fill a void enable a previously difficult/impossible application orthogonal to language design quality (almost) Programming training is the dominant cost Languages with a big user base are replaced rarely Popular languages become ossified but it is easy to start in a new niche... Compiler Design 1 (2011) 32

Why so many Programming Languages? Application domains have distinctive (and sometimes conflicting) needs Examples: Scientific computing: High performance Business: report generation Artificial intelligence: symbolic computation Systems programming: efficient low-level access Other special purpose languages... Compiler Design 1 (2011) 33

Topic: Language Design No universally accepted metrics for design A good language is one people use NO! Is COBOL the best language? Good language design is hard Compiler Design 1 (2011) 34

Language Evaluation Criteria Characteristic Criteria Readability Writeability Reliability Simplicity YES YES YES Data types YES YES YES Syntax design YES YES YES Abstraction YES YES Expressivity YES YES Type checking YES Exceptions YES Compiler Design 1 (2011) 35

History of Ideas: Abstraction Abstraction = detached from concrete details Necessary for building software systems Modes of abstraction: Via languages/compilers higher-level code; few machine dependencies Via subroutines abstract interface to behavior Via modules export interfaces which hide implementation Via abstract data types bundle data with its operations Compiler Design 1 (2011) 36

History of Ideas: Types Originally, languages had only few types FORTRAN: scalars, arrays LISP: no static type distinctions Realization: types help provide code documentation allow the programmer to express abstraction allow the compiler to check among many frequent errors and sometimes guarantee various forms of safety More recently: experiments with various forms of parameterization best developed in functional languages Compiler Design 1 (2011) 37

History of Ideas: Reuse Exploits common patterns in software development Goal: mass produced software components Reuse is difficult Two popular approaches (combined in C++) Type parameterization (List(Int) & List(Double)) Class and inheritance: C++ derived classes Inheritance allows: specialization of existing abstractions extension, modification and information hiding Compiler Design 1 (2011) 38

Current Trends Language design Many new special-purpose languages Popular languages to stay Compilers More needed and more complex Driven by increasing gap between new languages new architectures Venerable and healthy area Compiler Design 1 (2011) 39

Why study Compiler Design? Increase knowledge of common programming constructs and their properties Improve understanding of program execution Increase ability to learn new languages Learn how to build a large and reliable system Learn new (programming) techniques See many basic CS concepts at work Compiler Design 1 (2011) 40