COP4020 Programming Languages. Compilers and Interpreters Prof. Robert van Engelen

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

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Today s objectives. CSE401: Introduction to Compiler Construction. What is a compiler? Administrative Details. Why study compilers?

CS 11 C track: lecture 1

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Python Programming: An Introduction to Computer Science

Chapter 1. Introduction to Computers and C++ Programming. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

implement language system

Last class. n Scheme. n Equality testing. n eq? vs. equal? n Higher-order functions. n map, foldr, foldl. n Tail recursion

n Some thoughts on software development n The idea of a calculator n Using a grammar n Expression evaluation n Program organization n Analysis

Solutions to Final COMS W4115 Programming Languages and Translators Monday, May 4, :10-5:25pm, 309 Havemeyer

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Analysis of Algorithms

High-Order Language APPLICATION LEVEL HIGH-ORDER LANGUAGE LEVEL ASSEMBLY LEVEL OPERATING SYSTEM LEVEL INSTRUCTION SET ARCHITECTURE LEVEL

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

An Environment for the Reverse Engineering of Executable Programs. Cristina Cifuentes. Department of Computer Science, University of Tasmania

Appendix D. Controller Implementation

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis

Data Structures and Algorithms. Analysis of Algorithms

n Haskell n Syntax n Lazy evaluation n Static typing and type inference n Algebraic data types n Pattern matching n Type classes

Outline. Research Definition. Motivation. Foundation of Reverse Engineering. Dynamic Analysis and Design Pattern Detection in Java Programs

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 3 DB-Gateways

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs

Chapter 5. Functions for All Subtasks. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Last Class. Announcements. Lecture Outline. Types. Structural Equivalence. Type Equivalence. Read: Scott, Chapters 7 and 8. T2 y; x = y; n Types

Structuring Redundancy for Fault Tolerance. CSE 598D: Fault Tolerant Software

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

CMSC Computer Architecture Lecture 12: Virtual Memory. Prof. Yanjing Li University of Chicago

COSC 1P03. Ch 7 Recursion. Introduction to Data Structures 8.1

Bayesian approach to reliability modelling for a probability of failure on demand parameter

CS : Programming for Non-Majors, Summer 2007 Programming Project #3: Two Little Calculations Due by 12:00pm (noon) Wednesday June

CS 111 Green: Program Design I Lecture 27: Speed (cont.); parting thoughts

MR-2010I %MktBSize Macro 989. %MktBSize Macro

Definitions. Error. A wrong decision made during software development

Chapter 11 Web-based Information Systems

Τεχνολογία Λογισμικού

Analysis of Algorithms

CST-402(T): Language Processors

Threads and Concurrency in Java: Part 1

Data diverse software fault tolerance techniques

Threads and Concurrency in Java: Part 1

Keywords Software Architecture, Object-oriented metrics, Reliability, Reusability, Coupling evaluator, Cohesion, efficiency

CMPT 125 Assignment 2 Solutions

Behavioral Modeling in Verilog

Chapter 10. Defining Classes. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

MetaML and multi-stage programming with explicit annotations

CS553 Lecture Reuse Optimization: Common SubExpr Elim 3

Analysis of Algorithms

n Haskell n Covered syntax, lazy evaluation, static typing n Algebraic data types and pattern matching n Type classes n Monads and more n Types

Goals of this Lecture Activity Diagram Example

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000.

Lecture 1. Topics. Principles of programming languages (2007) Lecture 1. What makes programming languages such an interesting subject?

Code Review Defects. Authors: Mika V. Mäntylä and Casper Lassenius Original version: 4 Sep, 2007 Made available online: 24 April, 2013

10/23/18. File class in Java. Scanner reminder. Files. Opening a file for reading. Scanner reminder. File Input and Output

EE University of Minnesota. Midterm Exam #1. Prof. Matthew O'Keefe TA: Eric Seppanen. Department of Electrical and Computer Engineering

ARM. Microcontroller Development Tools. ARM RealView C/C++ Compilation Tools with MicroLib. Easy-to-use IDE Supports Complete Development Cycle

Working of the Compilers

Programming with Shared Memory PART II. HPC Spring 2017 Prof. Robert van Engelen

From last week. Lecture 5. Outline. Principles of programming languages

Abstract. Chapter 4 Computation. Overview 8/13/18. Bjarne Stroustrup Note:

LECTURE 2. Compilers and Interpreters

NAG Library Function Document nag_fft_hermitian (c06ebc)

GE FUNDAMENTALS OF COMPUTING AND PROGRAMMING UNIT III

n Bottom-up (LR) parsing n Characteristic Finite State Machine (CFSM) n SLR(1) parsing table n Conflicts in SLR(1) n LR parsing variants

Python Programming: An Introduction to Computer Science

Lecture 5. Counting Sort / Radix Sort

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design

LECTURE 3. Compiler Phases

Speeding-up dynamic programming in sequence alignment

Multi-Threading. Hyper-, Multi-, and Simultaneous Thread Execution

Chapter 8. Strings and Vectors. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

Chapter 8. Strings and Vectors. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. Chapter 4. The Processor Advanced Issues

ΕΠΛ 605 Εργαστήριο 5. Παναγιώτα Νικολάου 11/10/18. Slides from: Rajagopalan Desikan, Doug Burger, Stephen Keckler, Todd Austin

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

EE 459/500 HDL Based Digital Design with Programmable Logic. Lecture 13 Control and Sequencing: Hardwired and Microprogrammed Control

n Maurice Wilkes, 1949 n Organize software to minimize errors. n Eliminate most of the errors we made anyway.

Chapter 2. C++ Basics. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Optimum Device Support

Princeton University COS 217: Introduction to Programming Systems Fall 2005 Final Exam Answers

BEA Tuxedo. Using the BEA Tuxedo System on Windows NT

Shape Analysis and Applications 1

Task scenarios Outline. Scenarios in Knowledge Extraction. Proposed Framework for Scenario to Design Diagram Transformation

Elementary Educational Computer

A Generalized Set Theoretic Approach for Time and Space Complexity Analysis of Algorithms and Functions

Isn t It Time You Got Faster, Quicker?

A SOFTWARE MODEL FOR THE MULTILAYER PERCEPTRON

Contextual Analysis. Overview of Lecture 3. Ch 4 Syntactic Analysis. Mededelingen

Examples and Applications of Binary Search

CS 111: Program Design I Lecture # 7: First Loop, Web Crawler, Functions

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames

Transcription:

COP4020 mig Laguages Compilers ad Iterpreters Prof. Robert va Egele

Overview Commo compiler ad iterpreter cofiguratios Virtual machies Itegrated developmet eviromets Compiler phases Lexical aalysis Sytax aalysis Sematic aalysis Itermediate (machie-idepedet) code geeratio Itermediate code optimizatio Target (machie-depedet) code geeratio Target code optimizatio COP4020 Fall 2016 2

Compilers versus Iterpreters The compiler versus iterpreter implemetatio is ofte fuzzy Oe ca view a iterpreter as a virtual machie that executes highlevel code Java is compiled to bytecode Java bytecode is iterpreted by the Java virtual machie (JVM) or traslated to machie code by a just-i-time compiler (JIT) A processor (CPU) ca be viewed as a implemetatio i hardware of a virtual machie (e.g. bytecode ca be executed i hardware) Some programmig laguages caot be purely compiled ito machie code aloe Some laguages allow programs to rewrite/add code to the code base dyamically Some laguages allow programs to traslate data to code for executio (iterpretatio) COP4020 Fall 2016 3

Compilers versus Iterpreters Compilers try to be as smart as possible to fix decisios that ca be take at compile time to avoid to geerate code that makes this decisio at ru time Type checkig at compile time vs. rutime Static allocatio Static likig Code optimizatio Compilatio leads to better performace i geeral Allocatio of variables without variable lookup at ru time Aggressive code optimizatio to exploit hardware features Iterpretatio facilitates iteractive debuggig ad testig Iterpretatio leads to better diagostics of a programmig problem Procedures ca be ivoked from commad lie by a user Variable values ca be ispected ad modified by a user COP4020 Fall 2016 4

Compilatio Compilatio is the coceptual process of traslatig source code ito a CPU-executable biary target code Compiler rus o the same platform X as the target code Source Compiler Target Debug o X Compile o X Iput Target Output Ru o X COP4020 Fall 2016 5

Cross Compilatio Compiler rus o platform X, target code rus o platform Y Source Cross Compiler Compile o X Target Copy to Y Debug o X (= emulate Y) Iput Target Ru o Y Output COP4020 Fall 2016 6

Iterpretatio Iterpretatio is the coceptual process of ruig highlevel code by a iterpreter Source Iput Iterpreter Output COP4020 Fall 2016 7

Virtual Machies A virtual machie executes a istructio stream i software Adopted by Pascal, Java, Smalltalk-80, C#, fuctioal ad logic laguages, ad some scriptig laguages Pascal compilers geerate P-code that ca be iterpreted or compiled ito object code Java compilers geerate bytecode that is iterpreted by the Java virtual machie (JVM) The JVM may traslate bytecode ito machie code by just-itime (JIT) compilatio COP4020 Fall 2016 8

Compilatio ad Executio o Virtual Machies Compiler geerates itermediate program Virtual machie iterprets the itermediate program Source Compiler Compile o X Itermediate Ru o VM Iput Virtual Machie Ru o X, Y, Z, Output COP4020 Fall 2016 9

Pure Compilatio ad Static Likig Adopted by the typical Fortra systems Library routies are separately liked (merged) with the object code of the program Source Compiler Icomplete Object Code exter pritf(); _pritf _fget _fsca Static Library Object Code Liker Biary Executable COP4020 Fall 2016 10

Compilatio, Assembly, ad Static Likig Facilitates debuggig of the compiler Source Compiler Assembly exter pritf(); Assembler _pritf _fget _fsca Static Library Object Code Liker Biary Executable COP4020 Fall 2016 11

Compilatio, Assembly, ad Dyamic Likig Dyamic libraries (DLL,.so,.dylib) are liked at ru-time by the OS (via stubs i the executable) Source exter pritf(); Compiler Assembly Assembler Shared Dyamic Libraries _pritf, _fget, _fsca, Iput Icomplete Executable Output COP4020 Fall 2016 12

Preprocessig Most C ad C++ compilers use a preprocessor to import header files ad expad macros Source Preprocessor Modified Source #iclude <stdio.h> #defie N 99 for (i=0; i<n; i++) Compiler for (i=0; i<99; i++) Assembly or Object Code COP4020 Fall 2016 13

The CPP Preprocessor Early C++ compilers used the CPP preprocessor to geerated C code for compilatio C++ Source Code C++ Preprocessor C Source Code C Compiler Assembly or Object Code COP4020 Fall 2016 14

Itegrated Developmet Eviromets mig tools fuctio together i cocert Editors Compilers/preprocessors/iterpreters Debuggers Emulators Assemblers Likers Advatages Tools ad compilatio stages are hidde Automatic source-code depedecy checkig Debuggig made simpler Editor with search facilities Examples Smalltalk-80, Eclipse, MS VisualStudio, Borlad COP4020 Fall 2016 15

Compilatio Phases ad Passes Compilatio of a program proceeds through a fixed series of phases Each phase use a (itermediate) form of the program produced by a earlier phase Subsequet phases operate o lower-level code represetatios Each phase may cosist of a umber of passes over the program represetatio Pascal, FORTRAN, C laguages desiged for oe-pass compilatio, which explais the eed for fuctio prototypes Sigle-pass compilers eed less memory to operate Java ad ADA are multi-pass COP4020 Fall 2016 16

Compiler Frot- ad Back-ed Frot ed aalysis Source program (character stream) Scaer (lexical aalysis) Tokes Parser (sytax aalysis) Parse tree Sematic Aalysis ad Itermediate Code Geeratio Abstract sytax tree or other itermediate form Back ed sythesis Abstract sytax tree or other itermediate form Machie- Idepedet Code Improvemet Modified itermediate form Target Code Geeratio Assembly or object code Machie-Specific Code Improvemet Modified assembly or object code COP4020 Fall 2016 17

Scaer: Lexical Aalysis Lexical aalysis breaks up a program ito tokes program gcd (iput, output); var i, j : iteger; begi read (i, j); while i <> j do if i > j the i := i - j else j := j - i; writel (i) ed. program gcd ( iput, output ) ; var i, j : iteger ; begi read ( i, j ) ; while i <> j do if i > j the i := i - j else j := i - i ; writel ( i ) ed. COP4020 Fall 2016 18

Cotext-Free Grammars A cotext-free grammar defies the sytax of a programmig laguage The sytax defies the sytactic categories for laguage costructs Statemets Expressios Declaratios Categories are subdivided ito more detailed categories A Statemet is a For-statemet If-statemet Assigmet <statemet> ::= <for-statemet> <if-statemet> <assigmet> <for-statemet> ::= for ( <expressio> ; <expressio> ; <expressio> ) <statemet> <assigmet> ::= <idetifier> := <expressio> COP4020 Fall 2016 19

Example: Micro Pascal <> ::= program <id> ( <id> <More_ids> ) ; <Block>. <Block> ::= <Variables> begi <Stmt> <More_Stmts> ed <More_ids> ::=, <id> <More_ids> ε <Variables> ::= var <id> <More_ids> : <Type> ; <More_Variables> ε <More_Variables> ::= <id> <More_ids> : <Type> ; <More_Variables> ε <Stmt> ::= <id> := <Exp> if <Exp> the <Stmt> else <Stmt> while <Exp> do <Stmt> begi <Stmt> <More_Stmts> ed <Exp> ::= <um> <id> <Exp> + <Exp> <Exp> - <Exp> COP4020 Fall 2016 20

Parser: Sytax Aalysis Parsig orgaizes tokes ito a hierarchy called a parse tree (more about this later) Essetially, a grammar of a laguage defies the structure of the parse tree, which i tur describes the program structure A sytax error is produced by a compiler whe the parse tree caot be costructed for a program COP4020 Fall 2016 21

Sematic Aalysis Sematic aalysis is applied by a compiler to discover the meaig of a program by aalyzig its parse tree or abstract sytax tree Static sematic checks are performed at compile time Type checkig Every variable is declared before used Idetifiers are used i appropriate cotexts Check subroutie call argumets Check labels Dyamic sematic checks are performed at ru time, ad the compiler produces code that performs these checks Array subscript values are withi bouds Arithmetic errors, e.g. divisio by zero Poiters are ot derefereced uless poitig to valid object A variable is used but has't bee iitialized Whe a check fails at ru time, a exceptio is raised COP4020 Fall 2016 22

Sematic Aalysis ad Strog Typig A laguage is strogly typed "if (type) errors are always detected" Errors are either detected at compile time or at ru time Examples of such errors are listed o previous slide Laguages that are strogly typed are Ada, Java, ML, Haskell Laguages that are ot strogly typed are Fortra, Pascal, C/C+ +, Lisp Strog typig makes laguage safe ad easier to use, but potetially slower because of dyamic sematic checks I some laguages, most (type) errors are detected late at ru time which is detrimetal to reliability e.g. early Basic, Lisp, Prolog, some script laguages COP4020 Fall 2016 23

Code Geeratio ad Itermediate Code Forms A typical itermediate form of code produced by the sematic aalyzer is a abstract sytax tree (AST) The AST is aotated with useful iformatio such as poiters to the symbol table etry of idetifiers Example AST for the gcd program i Pascal COP4020 Fall 2016 24

Target Code Geeratio ad Optimizatio The AST with the aotated iformatio is traversed by the compiler to geerate a low-level itermediate form of code, close to assembly This machie-idepedet itermediate form is optimized From the machie-idepedet form assembly or object code is geerated by the compiler This machie-specific code is optimized to exploit specific hardware features COP4020 Fall 2016 25