Compilers and Interpreters

Similar documents
Compiling Techniques

High-level View of a Compiler

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

Introduction to Compiler

The View from 35,000 Feet

CS 132 Compiler Construction

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

Overview of a Compiler

Compiler Construction

What do Compilers Produce?

Front End. Hwansoo Han

Formats of Translated Programs

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

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

Compiling Regular Expressions COMP360

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

The Structure of a Syntax-Directed Compiler

Compilers. Lecture 2 Overview. (original slides by Sam

Overview of a Compiler

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

1DL321: Kompilatorteknik I (Compiler Design 1)

Overview of a Compiler

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

CS 4201 Compilers 2014/2015 Handout: Lab 1

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

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

Compilers and Code Optimization EDOARDO FUSELLA

The Structure of a Compiler

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

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

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

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

Undergraduate Compilers in a Day

When do We Run a Compiler?

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

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

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

CST-402(T): Language Processors

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

Compiler Design (40-414)

CS606- compiler instruction Solved MCQS From Midterm Papers

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

Introduction to Compiler Design

CS 352: Compilers: Principles and Practice

Lexical Scanning COMP360

The Structure of a Syntax-Directed Compiler

Compiler Construction 1. Introduction. Oscar Nierstrasz

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

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

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

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

LECTURE 3. Compiler Phases

Compilers. History of Compilers. A compiler allows programmers to ignore the machine-dependent details of programming.

Academic Formalities. CS Modern Compilers: Theory and Practise. Images of the day. What, When and Why of Compilers

CS 321 IV. Overview of Compilation


CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Compiler Construction 1. Introduction. Oscar Nierstrasz

SYSTEMS PROGRAMMING. Srimanta Pal. Associate Professor Indian Statistical Institute Kolkata OXFORD UNIVERSITY PRESS

Academic Formalities. CS Language Translators. Compilers A Sangam. What, When and Why of Compilers

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Pioneering Compiler Design

Outline. Lecture 17: Putting it all together. Example (input program) How to make the computer understand? Example (Output assembly code) Fall 2002

CS 406/534 Compiler Construction Putting It All Together

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

CS 415 Midterm Exam Spring 2002

CS 314 Principles of Programming Languages

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

Introduction to Compilers

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

Theory and Compiling COMP360

Part 5 Program Analysis Principles and Techniques

Overview of the Class

Group A Assignment 3(2)

Compiler Design. Lecture 1

CS415 Compilers. Lexical Analysis

COMPILER DESIGN LECTURE NOTES

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

Goals for course What is a compiler and briefly how does it work? Review everything you should know from 330 and before

CS 314 Principles of Programming Languages. Lecture 3

Optimizing Finite Automata

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

Group B Assignment 9. Code generation using DAG. Title of Assignment: Problem Definition: Code generation using DAG / labeled tree.

Introduction to Compiler Construction

Formal Languages and Compilers Lecture I: Introduction to Compilers

SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT I - LEXICAL ANALYSIS PART - A

Appendix A The DL Language

Lexical analysis. Syntactical analysis. Semantical analysis. Intermediate code generation. Optimization. Code generation. Target specific optimization

A simple syntax-directed

Introduction. Compilers and Interpreters

Why are there so many programming languages?

Compiler Code Generation COMP360

4. An interpreter is a program that

COSE312: Compilers. Lecture 1 Overview of Compilers

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

Semantic Analysis. Lecture 9. February 7, 2018

Working of the Compilers

CSE 3302 Programming Languages Lecture 2: Syntax

Introduction to Lexical Analysis

Homework & Announcements

Transcription:

Overview Roadmap Language Translators: Interpreters & Compilers Context of a compiler Phases of a compiler Compiler Construction tools Terminology How related to other CS Goals of a good compiler 1

Compilers and Interpreters What is a compiler? A program that translates an executable program in one language into an executable program in another language What is an interpreter? A program that reads an executable program and produces the results of executing that program Implementation of Languages C family is typically compiled Scheme is typically interpreted Java is translated to bytecodes - interpreted by virtual machine or compiled into native code JIT compilers on demand 2

Overview of interpreters and compilers interpreter program output source program or representation data Locus of control - in interpreter, not program Compiler has distinct translation and execution phase source program compiler Some program representation data program output 3

Interpreters Advantages Easier to debug code - better diagnostics More flexible - can modify code & type of variables Machine independent - portable Disadvantages Execution overhead speed - text or some representation of program is continually reexamined - 10:1 ratio; Space overhead - program representation plus code of interpreter Compilers Advantages Execution faster Exploit architecture Disadvantages Processing time for preprocessing Complexity 4

Relation to Other CS Artificial intelligence Algorithms Theory Systems Architecture Greedy algorithms, Genetic algorithms Heuristic search techniques Graph algorithms, union-find Dynamic programming DFAs & PDAs, pattern matching Fixed-point algorithms Allocation & naming, Synchronization, locality Memory hierarchy management Functional units & pipelines Instruction set use 5

From Your Experience You have used several compilers. What qualities do you want in a compiler that you buy? 1. Correct Code 2. Output runs fast 3. Compiler runs fast 4. Compile time proportional to program size 5. Support for separate compilation 6. Good diagnostics for syntax errors 7. Works well with debugger 8. Good diagnostics for flow anomalies 9. Good diagnostics for storage leaks 10. Consistent, predictable optimization 11. Runs on different machines 6

High-level View of a Compiler Source code Compiler Object code Errors Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code) Must agree with OS & linker on format for object code Big step up from assembly language use higher level notations 7

Conceptual phases of compiler Lexical Analysis (scanner) Syntax analysis (parser) Semantic Analysis Code optimization Code generation Sequence of tokens Intermediate code - 1 Intermediate code 2 Optimized code Target code Front End machine independent language dependent Middle Back End machine dependent language independent 8

Traditional Two-pass Compiler Source code Front End Back End Object code Errors Allow 2 passes: Use an intermediate representation () Front end maps legal source code into Back end maps into target machine code Admits multiple front ends & multiple passes (better code) 9

The Front End Source code Scanner tokens Parser Errors Responsibilities Recognize legal (& illegal) programs Report errors in a useful way Produce & preliminary storage map Shape the code for the back end Much of front end construction can be automated 10

Lexical Analysis/Scanner Purpose: recognize words - smallest unit Analyze string of characters from source - left to right to recognize units Example: Character string - lexeme Type of lexical entity - token Smallest unit above letters Max:= initial * late + 60 Lexemes: max, :=, initial, *, late, +, 60 Tokens: Id Id Id := * + Int Must recognize blanks, other characters such as %, $, etc 11

Syntax Analyzer - Parser Parsing similar to diagramming a natural language sentence This line is a long sentence article noun verb article adj. noun subject object sentence Parsing If x = = y then z = l; else z = 2 if id eq id then id ass con else id ass con assign assign pred then part else part statement Lecture 1: CS 2210 12 Fall 2002

Semantic Analysis Once structure is understood, determine the meaning using the structure. Checks performed to ensure components fit together meaningfully information is added to structures limited analysis to catch inconsistencies - e.g., type checking Put semantic meaning in structure - produce intermediate form - - many forms of easier to generate machine code from can be different levels of - descending levels of abstraction Highest is source Lowest is target code 13

The Back End/Code Generation Instruction Selection Instruction Scheduling Register Allocation Machine code Errors Responsibilities Translate into target machine code Choose instructions to implement each operation Decide which value to keep in registers Ensure conformance with system interfaces Automation has been much less successful in the back end 14

The Back End Instruction Selection Instruction Scheduling Register Allocation Machine code Errors Instruction Selection Produce fast, compact code Take advantage of target features such as addressing modes Usually viewed as a pattern matching problem ad hoc methods, pattern matching, dynamic programming Depends on architecture - CISC, RISC 15

The Back End Instruction Selection Instruction Scheduling Register Allocation Machine code Errors Instruction Scheduling Avoid hardware stalls and interlocks Use all functional units productively Can increase lifetime of variables (changing the allocation) Optimal scheduling is NP-Complete in nearly all cases Good heuristic techniques are well understood 16

The Back End Instruction Selection Instruction Scheduling Register Allocation Machine code Errors Register allocation Have each value in a register when it is used Manage a limited set of resources Can change instruction choices & insert LOADs & STOREs Optimal allocation is NP-Complete Compilers approximate solutions to NP-Complete problems 17

Code Generation what kind of code Produce target code - various forms of target code 1. Assembly Code - symbolic instruction and addresses Easier but not done in modern compilers - assembler slow 2. Relocatable format Binary form except external references, instruction addresses and data addresses not bound to address Need linker and loader Both assembly & relocatable allow program modules to be separately compiled 3. Another language 18

Traditional Three-pass Compiler Source Code Front End Middle End Back End Machine code Errors Code Improvement (or Optimization) Analyzes and rewrites (or transforms) Primary goal is to reduce running time of the compiled code May also improve space, power consumption, Must preserve meaning of the code Measured by values of named variables 19

The Optimizer (or Middle End) Opt Opt Opt... Opt 1 2 3 n Errors Modern optimizers are structured as a series of passes Typical Transformations Discover & propagate some constant value Move a computation to a less frequently executed place Specialize some computation based on context Discover a redundant computation & remove it Remove useless or unreachable code Encode an idiom in some particularly efficient form 20

Code Optimization Modify program representation so that the program runs faster uses less memory uses less power in general, reduce the resources consumed e.g., constant propagation and folding Y:= 3 X:= Y + 4 optimizes to X:= 7 21

Symbol Table Manager Collect and maintain information about id s attributes e.g., storage allocation, type, scope, number and type of parameters Usually cuts across all phases - lexical, parsing and semantic, code optimization, code generation Phase add information - lexical, parsing and semantic Phases use information - code optimization, code generation Debuggers uses some form of symbol table Error Reporting Phases deal with errors - 1st 3 phases handled bulk of errors Lots of success here 22

Distinction between phases and passes Passes - number of times through a program representation 1 - passes, 2 - passes, multiple passes Languages become more complex - more passes Phases - conceptual and sometimes physical stages Symbol table coordinating information between phases However, phases are not completely separate - semantic phase must do things that syntax phase should do if it could Some interaction possible: optimization and code generation - what optimizer does affects code generator 23

Scanner generator Compiler tools Generate lexical analyzer from specification of tokens based on regular expressions Examples: Lex, Flex, JLex Parser generator Generate parser from specification of syntactical structure using BNF grammars Example: YACC, Bison, CUP What about compiler generator? How do you specify semantics that is useful for compiler? How do you specify the architecture? How do you specify optimizations? 24