INF5110 Compiler Construction

Similar documents
Compiler construction

Formal Languages and Compilers Lecture I: Introduction to Compilers

Compiler Design (40-414)

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

CST-402(T): Language Processors

Compiler construction

Introduction to Compiler Construction

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

Introduction to Compiler Construction

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

Compiler construction

Compilers Crash Course

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Compiling Regular Expressions COMP360

COMPILER DESIGN LECTURE NOTES

Compiler Design Overview. Compiler Design 1

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

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

CS Compiler Construction West Virginia fall semester 2014 August 18, 2014 syllabus 1.0

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

COMP 3002: Compiler Construction. Pat Morin School of Computer Science

CA Compiler Construction

Introduction to Compiler Construction

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

Working of the Compilers

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY SCHOOL OF COMPUTING DEPARTMENT OF CSE COURSE PLAN

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

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

Compiling Techniques

COMPILER DESIGN. For COMPUTER SCIENCE

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY

Compiler Construction

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Philadelphia University Faculty of Information Technology Department of Computer Science --- Semester, 2007/2008. Course Syllabus

Introduction. Compilers and Interpreters

Introduction to Compiler Construction

CSE4305: Compilers for Algorithmic Languages CSE5317: Design and Construction of Compilers

CSE4305: Compilers for Algorithmic Languages CSE5317: Design and Construction of Compilers

Lexical Scanning COMP360

Compiler Construction LECTURE # 1

Introduction to Compiler Construction

COMP455: COMPILER AND LANGUAGE DESIGN. Dr. Alaa Aljanaby University of Nizwa Spring 2013

LECTURE 3. Compiler Phases

Principles of Compiler Construction ( )

CS 415 Midterm Exam Spring 2002

CS 4201 Compilers 2014/2015 Handout: Lab 1


Evaluation Scheme L T P Total Credit Theory Mid Sem Exam

CS131: Programming Languages and Compilers. Spring 2017

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

Compiler, Assembler, and Linker

Introduction to Lexical Analysis

General Course Information. Catalogue Description. Objectives

Finding People and Information (1) G53CMP: Lecture 1. Aims and Motivation (1) Finding People and Information (2)

Terminology & Basic Concepts

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

CSE 582 Autumn 2002 Exam 11/26/02

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

CS426 Compiler Construction Fall 2006

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Compilers. Prerequisites

Overview of Compiler. A. Introduction

TDDD55 - Compilers and Interpreters Lesson 3

Compilers. Pierre Geurts

CSE4305: Compilers for Algorithmic Languages CSE5317: Design and Construction of Compilers

Compiler Construction

CS 415 Midterm Exam Spring SOLUTION

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

LECTURE NOTES ON COMPILER DESIGN P a g e 2

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

An Overview of Compilation

What do Compilers Produce?

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Principles of Compiler Construction ( )

Compilers. Lecture 2 Overview. (original slides by Sam

Compiler Construction

A Simple Syntax-Directed Translator

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Syntax-Directed Translation. Lecture 14

Syntax and Grammars 1 / 21

HOLY ANGEL UNIVERSITY COLLEGE OF INFORMATION AND COMMUNICATIONS TECHNOLOGY COMPILER THEORY COURSE SYLLABUS

CS202 Compiler Construction. Christian Skalka. Course prerequisites. Solid programming skills a must.

Undergraduate Compilers in a Day

Why are there so many programming languages?


Writing Evaluators MIF08. Laure Gonnord

INTRODUCTION PRINCIPLES OF PROGRAMMING LANGUAGES. Norbert Zeh Winter Dalhousie University 1/10

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Compilers. Computer Science 431

Crafting a Compiler with C (II) Compiler V. S. Interpreter

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

CMPE 152 Compiler Design

Formats of Translated Programs

COMP3131/9102: Programming Languages and Compilers

Compiler Construction Lecture 1: Introduction Winter Semester 2018/19 Thomas Noll Software Modeling and Verification Group RWTH Aachen University

Transcription:

INF5110 Compiler Construction Introduction Spring 2016 1 / 33

Outline 1. Introduction Introduction Compiler architecture & phases Bootstrapping and cross-compilation 2 / 33

Outline 1. Introduction Introduction Compiler architecture & phases Bootstrapping and cross-compilation 3 / 33

Course info Course presenters: Martin Steffen (msteffen@ifi.uio.no) Stein Krogdahl (stein@ifi.uio.no) Birger Møller-Pedersen (birger@ifi.uio.no) Eyvind Wærstad Axelsen (oblig-ansvarlig, eyvinda@ifi.uio.no) Course s web-page http://www.uio.no/studier/emner/matnat/ifi/inf5110 overview over the course, pensum (watch for updates) various announcements, beskjeder, etc. 4 / 33

Course material and plan The material is based largely on [Louden, 1997], but also other sources will play a role. A classic is the dragon book [Aho et al., 1986] see also Errata list at http://www.cs.sjsu.edu/~louden/cmptext/ approx. 3 hours teaching per week mandatory assignments (= obligs ) O1 published mid-february, deadline mid-march O2 published beginning of April, deadline beginning of May group work up-to 3 people recommended. Please inform us about such planned group collaboration slides: see updates on the net exam: 8th June, 14:30, 4 hours. 5 / 33

Motivation: What is CC good for? not everyone is actually building a full-blown compiler, but fundamental concepts and techniques in CC most, if not basically all, software reads, processes/transforms and outputs data often involves techniques central to CC Understanding compilers deeper understanding of programming language(s) new language (domain specific, graphical, new language paradigms and constructs... ) CC & their principles will never be out-of-fashion. 6 / 33

Outline 1. Introduction Introduction Compiler architecture & phases Bootstrapping and cross-compilation 7 / 33

Architecture of a typical compiler Figure: Structure of a typical compiler 8 / 33

Anatomy of a compiler 9 / 33

Pre-processor either separate program or integrated into compiler nowadays: C-style preprocessing mostly seen as hack grafted on top of a compiler. 1 examples (see next slide): file inclusion 2 macro definition and expansion 3 conditional code/compilation: Note: #if is not the same as the if-programming-language construct. problem: often messes up the line numbers 1 C-preprocessing is still considered sometimes a useful hack, otherwise it would not be around... But it does not naturally encourage elegant and well-structured code, just quick fixes for some situations. 2 the single most primitive way of composing programs split into separate pieces into one program. 3 Compare also to the \newcommand-mechanism in L A TEX or the analogous \def-command in the more primitive TEX-language. 10 / 33

C-style preprocessor examples #include <f i l e n a m e > Listing 1: file inclusion #v a r d e f #a = 5 ; #c = #a+1... #i f (#a < #b ).. #e l s e... #e n d i f Listing 2: Conditional compilation 11 / 33

C-style preprocessor: macros #macrodef h entdata (#1,#2) #1 #2 (#1) #enddef... #h e n t data ( k a r i, p e r ) k a r i per (k a r i ) Listing 3: Macros 12 / 33

Scanner (lexer... ) input: the program text ( = string, char stream, or similar) task divide and classify into tokens, and remove blanks, newlines, comments.. theory: finite state automata, regular languages 13 / 33

Scanner: illustration a [ i n d e x ] = 4 + 2 lexeme token class value a identifier "a" [ left bracket index identifier "index" ] right bracket = assignment 4 number "4" + plus sign 2 number "2" 14 / 33

Scanner: illustration a [ i n d e x ] = 4 + 2 lexeme token class value a identifier 2 [ left bracket index identifier 21 ] right bracket = assignment 4 number 4 + plus sign 2 number 2 0 1 2 "a". 21 "index" 22. 15 / 33

Parser 16 / 33

a[index] = 4 + 2: parse tree/syntax tree expr assign-expr expr = expr subscript expr additive expr expr [ expr ] expr + expr identifier a identifier index number 4 number 2 17 / 33

a[index] = 4 + 2: abstract syntax tree assign-expr subscript expr additive expr identifier a identifier index number 2 number 4 18 / 33

(One typical) Result of semantic analysis one standard, general outcome of semantic analysis: annotated or decorated AST additional info (non context-free): bindings for declarations (static) type information assign-expr :? subscript-expr :int additive-expr :int identifier :array of int identifier :int number :int number :int a :array of int index :int 4 :int 2 :int here: identifiers looked up wrt. declaration 4, 2: due to their form, basic types. 19 / 33

Optimization at source-code level assign-expr subscript expr number 6 identifier a identifier index t = 4+2; a[index] = t; t = 6; a[index] = t; a[index] = 6; 20 / 33

Code generation & optimization MOV R0, i n d e x ; ; v a l u e o f i n d e x > R0 MUL R0, 2 ; ; double v a l u e o f R0 MOV R1, &a ; ; a d d r e s s o f a > R1 ADD R1, R0 ; ; add R0 t o R1 MOV R1, 6 ; ; c o n s t 6 > a d d r e s s i n R1 MOV R0, i n d e x ; ; v a l u e o f i n d e x > R0 SHL R0 ; ; double v a l u e i n R0 MOV &a [ R0 ], 6 ; ; c o n s t 6 > a d d r e s s a+r0 many optimizations possible potentially difficult to automatize 4, based on a formal description of language and machine platform dependent 4 not that one has much of a choice. Difficult or not, no one wants to optimize generated machine code by hand.... 21 / 33

Anatomy of a compiler (2) 22 / 33

Misc. notions front-end vs. back-end, analysis vs. synthesis separate compilation how to handle errors? data handling and management at run-time (static, stack, heap), garbage collection? language can be compiled in one pass? E.g. C and Pascal: declarations must precede use no longer too crucial, enough memory available compiler assisting tool and infra structure, e.g. debuggers profiling project management, editors build support... 23 / 33

Compiler vs. interpeter Compilation classically: source code machine code for given machine different forms of machine code (for 1 machine): executable relocatable textual assembler code full interpretation directly executed from program code/syntax tree often used for command languages, interacting with OS etc. speed typically 10 100 slower than compilation compilation to intermediate code which is interpreted used in e.g. Java, Smalltalk,.... intermediate code: designed for efficient execution (byte code in Java) executed on a simple interpreter (JVM in Java) typically 3 30 times slower than direct compilation 24 / 33

More recent compiler technologies Memory has become cheap (thus comparatively large) keep whole program in main memory, while compiling OO has become rather popular special challenges & optimizations Java compiler generates byte code part of the program can be dynamically loaded during run-time concurrency, multi-core graphical languages (UML, etc), meta-models besides grammars 25 / 33

Outline 1. Introduction Introduction Compiler architecture & phases Bootstrapping and cross-compilation 26 / 33

Compiling from source to target on host tombstone diagrams (or T-diagrams).... 27 / 33

Two ways to compose T-diagrams 28 / 33

Using an old language and its compiler for write a compiler for a new one 29 / 33

Pulling oneself up on one s own bootstraps bootstrap (verb, trans.): to promote or develop... with little or no assistance Merriam-Webster 30 / 33

Bootstrapping 2 31 / 33

Porting & cross compilation 32 / 33

References I [Aho et al., 1986] Aho, A. V., Sethi, R., and Ullman, J. D. (1986). Compilers: Principles, Techniques and Tools. Addison-Wesley. [Louden, 1997] Louden, K. (1997). Compiler Construction, Principles and Practice. PWS Publishing. 33 / 33