LECTURE 2. Compilers and Interpreters

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

23/02/15. Compile, execute, debug. Advanced Programming THE JAVA PLATFORM

Compilation I. Hwansoo Han

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

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

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

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

Chapter 11 Introduction to Programming in C

Chapter 2. Operating-System Structures

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

Introduction to Java Programming

Why are there so many programming languages?

Programming Languages

Programming 1 - Honors

Course introduction. Advanced Compiler Construction Michel Schinz

CHAPTER 2: SYSTEM STRUCTURES. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Full file at

Chapter 2: Operating-System Structures. Operating System Concepts 9 th Edit9on

assembler Machine Code Object Files linker Executable File

Chapter 2: Operating-System Structures

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Programming 1. Lecture 1 COP 3014 Fall August 28, 2017

VA Smalltalk 9: Exploring the next-gen LLVM-based virtual machine

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

Just-In-Time Compilation

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

Chapter 2: Operating-System Structures. Operating System Concepts 9 th Edition

Software Development. Integrated Software Environment

Chapter 3: Operating-System Structures

Chapter 2: Operating-System Structures

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Course Syllabus [1/2]

Introduction. CS 2210 Compiler Design Wonsun Ahn

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

Chapter Overview. Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 1: Basic Concepts. Printing this Slide Show

Principles of Programming Languages. Lecture Outline

OS and Computer Architecture. Chapter 3: Operating-System Structures. Common System Components. Process Management

History of Compilers The term

Computers in Engineering COMP 208. Computer Structure. Computer Architecture. Computer Structure Michael A. Hawker

Low-Level Languages. Computer Programs and Programming Languages

Compiler Design Spring 2018

Optimization Techniques

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

Compiling Regular Expressions COMP360

Chapter 3: Operating-System Structures

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

Question Points Score Total: 100

Design & Implementation Overview

Chapter 11 Introduction to Programming in C

Lecture 1 - Introduction (Class Notes)

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Compilers and Interpreters

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Chapter 11 Introduction to Programming in C

Last class: OS and Architecture. OS and Computer Architecture

Last class: OS and Architecture. Chapter 3: Operating-System Structures. OS and Computer Architecture. Common System Components

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

Introduction to Java. Lecture 1 COP 3252 Summer May 16, 2017

Concepts of Programming Languages

Operating System Services. User Services. System Operation Services. User Operating System Interface - CLI. A View of Operating System Services

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CS 360 Programming Languages Interpreters

LESSON 13: LANGUAGE TRANSLATION

CS 415 Midterm Exam Spring 2002

Chapter 2: Operating-System Structures

Lecture 1 Introduction. Computing platforms Novosibirsk State University University of Hertfordshire D. Irtegov, A.Shafarenko 2018

Chapter 2: Operating-System Structures

CMPT 379 Compilers. Anoop Sarkar.

The role of semantic analysis in a compiler

Technology in Action. Chapter Topics (cont.) Chapter Topics. Reasons for Software Programming. Information Systems 10/29/2010

UNIT 2. OPERATING SYSTEM STRUCTURES

Operating- System Structures

Chapter 11 Introduction to Programming in C

How Software Executes

Programming Languages FILS Andrei Vasilateanu

CS201- Lecture 7 IA32 Data Access and Operations Part II

Chapter 11 Introduction to Programming in C

CS 415 Midterm Exam Fall 2003

Chapter 2: Operating-System Structures. Operating System Concepts Essentials 8 th Edition

Chapter 2 Operating-System Structures

Compilers Crash Course

Chapter 12. Microcontroller Application Development Tools

Basic Data Types. CS429: Computer Organization and Architecture. Array Allocation. Array Access

Chapter 2: Operating-System

Four Components of a Computer System

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Lesson 01 Introduction

Chapter 3: Operating-System Structures

Chapter 11 Introduction to Programming in C

Introduction to Programming: Variables and Objects. HORT Lecture 7 Instructor: Kranthi Varala

Making Address Spaces Smaller

Chapter 2: Operating-System Structures

CS 536. Class Meets. Introduction to Programming Languages and Compilers. Instructor. Key Dates. Teaching Assistant. Charles N. Fischer.

CS429: Computer Organization and Architecture

Introduction. Lecture 1 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

CSC180: Lecture 2. Wael Aboulsaadat.

COMPILER DESIGN. For COMPUTER SCIENCE

Jazelle ARM. By: Adrian Cretzu & Sabine Loebner

Compiler Theory Introduction and Course Outline Sandro Spina Department of Computer Science

Transcription:

LECTURE 2 Compilers and Interpreters

COMPILATION AND INTERPRETATION Programs written in high-level languages can be run in two ways. Compiled into an executable program written in machine language for the target machine. Directly interpreted and the execution is simulated by the interpreter.

COMPILATION AND INTERPRETATION Let s say we have the following statement: A[i][j] = 1; How can we execute this statement?

COMPILATION AND INTERPRETATION A[i][j] = 1; An Approach: Create a software environment that understands 2-dimensional arrays and the language. To execute the statement, it just puts 1 in the array entry A[i][j]. This is interpretation since the software environment understands the language and performs the operations specified by interpreting the statements.

COMPILATION AND INTERPRETATION A[i][j] = 1; Another Approach: Translate the statements into native machine language (or assembly language) and then run the program. This is compilation, g++ produces the following assembly for this statement: salq addq leaq addq salq addq movl $2, %rax %rcx, %rax 0(,%rax,4), %rdx %rdx, %rax $2, %rax %rsi, %rax $1, A(,%rax,4)

COMPILATION AND INTERPRETATION How is a C++ program executed on linprog? g++ try.cpp compiling the program into machine code../a.out running the machine code. How is a python program executed? python try.py The program just runs, no compilation phase. The program python is the software environment that understands python language. The program try.py is executed (interpreted) within the environment. In general, which approach is more efficient?

COMPILATION AND INTERPRETATION How is a C++ program executed on linprog? g++ try.cpp compiling the program into machine code../a.out running the machine code. How is a python program executed? python try.py The program just runs, no compilation phase. The program python is the software environment that understands python language. The program try.py is executed (interpreted) within the environment. In general, which approach is more efficient? Compilation is always more efficient! Interpretation provides more functionality.

COMPILATION At the highest level of abstraction, compiling looks like this: Source Program Compiler The compiler translated the high-level source program into a target program in the machine s language (object code). Input Target Program Output

COMPILERS At the highest level of abstraction, compiling looks like this: Source Program Compiler At some arbitrary later time, the user can tell the operating system to run the target program. Input Target Program Output

WHAT IS A COMPILER? The compiler itself is also a machine language program, typically created by compiling some other high-level program.

INTERPRETERS Interpretation generally looks like this: Source Program Interpreter Interpreters are necessary for the execution of the application. Interpreters essentially create a VM whose machine language is the high-level programming language. Output Input

COMPILATION VS. INTERPRETATION Compilers attempt to make decisions at compile time to avoid them at run time. Type checking at compile time vs. runtime. Static allocation. Static linking. Code optimization. Compilation leads to better performance in general. Allocation of variables without variable lookup at run time. Aggressive code optimization to exploit hardware features.

COMPILATION VS. INTERPRETATION So why use interpreted languages? Interpretation leads to greater flexibility, easier debugging, and better features. Fundamental characteristics can be decided at run time. Example: some_input = raw_input( Please type something: ) perfectly valid Python Lisp and Prolog can write new pieces of code and execute them on the fly.

MIXING COMPILATION AND INTERPRETATION How do you choose? Don t worry you don t have to. Kinda. Typically, most languages are implemented using a mixture of both approaches. Source Program Translator Intermediate Program Virtual Machine Output Input

VIRTUAL MACHINES Virtual machines are typically software emulations of a machine. System virtual machines emulate entire platforms. Language virtual machines support a single process. We re mostly concerned with these. An important example is the Java Virtual Machine (JVM). Can execute any executable that is compiled into Java bytecode. Technically, your CPU can be viewed as an implementation in hardware of a virtual machine (e.g. bytecode can be executed in hardware).

MIXING COMPILATION AND INTERPRETATION Practically speaking, there are two aspects that distinguish what we consider compilation from interpretation. Thorough Analysis Compilation requires a thorough analysis of the code. Non-trivial Transformation Compilation generates intermediate representations that typically do not resemble the source code.

PRACTICAL IMPLEMENTATION STRATEGIES Preceprocessing Initial translation step. Slightly modifies source code to be interpreted more efficiently. Removing comments and whitespace, grouping characters into tokens, etc. C preprocessor can modify portions of code itself conditional compilation. Linking Linkers merge necessary library routines to create the final executable. Fortran implementations come closest to pure compilation with the exception of a linking step. Fortran Program Compiler Machine Language Library Routines Linker Final Machine Language Program

PRACTICAL IMPLEMENTATION STRATEGIES Post-Compilation Assembly Many compilers translate the source code into assembly rather than machine language. Changes in machine language won t affect source code. Assembly is easier to read (for debugging purposes). Source-to-source Translation Compiling source code into another high-level language. Early C++ programs were compiled into C, which was compiled into assembly. Source Program Compiler Assembly Language Assembler Machine Language

PRACTICAL IMPLEMENTATION STRATEGIES Bootstrapping What comes first, the language or the compiler? Let s say you want to build a compiler for Java that is written in Java (self-hosting), but we only have a C compiler. Write a very simple compiler for a small subset of Java in a small subset of C. Hand-translate the compiler into Java. Run the translated code through the C-written compiler. Now you have a Java compiler written in Java. Repeat, extending the compiler to accept a larger subset of Java.

PRACTICAL IMPLEMENTATION STRATEGIES Dynamic and Just-in-time Compilation Dynamic compilation is the delay of compilation until the last possible moment. JIT is a subset of Dynamic Compilation and combines traditional compilation with interpretation (only the source code bytecode occurs ahead of time). JIT compilation combines the speed of compiled code with the flexibility of interpretation, with the overhead of both methods combined.

COMPILATION So, clearly compilation is not so rigidly defined as we might have expected. As stated before, it suffices to say that compilation is the translation of a nontrivial language to another non-trivial language, with thorough analysis of the input.

INTEGRATED DEVELOPMENT ENVIRONMENTS With all that said, programming tools function together in concert. Editors Compilers/Preprocessors/Interpreters Debuggers Emulators Assemblers Linkers Advantages Tools and compilation stages are hidden. You ve been programming for a while now, did you know about all these compilation methods? Automatic source-code dependency checking. Debugging made simpler. Editor with search facilities.

NEXT LECTURE Compiler Phases