CS 3723: Supplemental Notes on Compilers and Interpreters, 2/14/2008. Compilers and Interpreters

Similar documents
1. Overview of the Java Language

Jonathan Worthington Scarborough Linux User Group

C# and Other Languages

2 Introduction to Java. Introduction to Programming 1 1

System Structures. Services Interface Structure

Section 1.4. Java s Magic: Bytecode, Java Virtual Machine, JIT,

Characteristics of Java (Optional) Y. Daniel Liang Supplement for Introduction to Java Programming

Example of Standard API

Restraining Execution Environments

Java and Real Time Storage Applications

Monitoring, Tracing, Debugging (Under Construction)

CSCI 3136 Principles of Programming Languages

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

The Hotspot Java Virtual Machine: Memory and Architecture

02 B The Java Virtual Machine

9/11/15. What is Programming? CSCI 209: Software Development. Discussion: What Is Good Software? Characteristics of Good Software?

Lesson 06: Basics of Software Development (W02D2

The Design of the Inferno Virtual Machine. Introduction

n Introduction n Art of programming language design n Programming language spectrum n Why study programming languages? n Overview of compilation

Evolution of the Major Programming Languages

Chapter 3 Operating-System Structures

Programming Languages

CPS221 Lecture: Operating System Structure; Virtual Machines

Chapter 13 Computer Programs and Programming Languages. Discovering Computers Your Interactive Guide to the Digital World

Chapter 3: Operating-System Structures. Common System Components

Using jvmstat and visualgc to Solve Memory Management Problems

General Introduction

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

Chapter 2 System Structures

1001ICT Introduction To Programming Lecture Notes

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Replication on Virtual Machines

VOC Documentation. Release 0.1. Russell Keith-Magee

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Levels of Programming Languages. Gerald Penn CSC 324

Apache Jakarta Tomcat

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Multi-core Programming System Overview

- Applet java appaiono di frequente nelle pagine web - Come funziona l'interprete contenuto in ogni browser di un certo livello? - Per approfondire

Online Recruitment System 1. INTRODUCTION

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

1/20/2016 INTRODUCTION

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

3 SOFTWARE AND PROGRAMMING LANGUAGES

Chapter 1 Fundamentals of Java Programming

CS 3530 Operating Systems. L02 OS Intro Part 1 Dr. Ken Hoganson

What is a programming language?

INTRODUCTION TO JAVA PROGRAMMING LANGUAGE

HEP data analysis using jhepwork and Java

Chapter 3: Operating-System Structures. System Components Operating System Services System Calls System Programs System Structure Virtual Machines

Effective Java Programming. efficient software development

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

Developing Embedded Software in Java Part 1: Technology and Architecture

File S1: Supplementary Information of CloudDOE

Building Applications Using Micro Focus COBOL

What Perl Programmers Should Know About Java

CS3600 SYSTEMS AND NETWORKS

A Comparison of Programming Languages for Graphical User Interface Programming

Android Programming and Security

Performance Measurement of Dynamically Compiled Java Executions

12/22/11. } Android by Pearson Education, Inc. All Rights Reserved by Pearson Education, Inc. All Rights Reserved.

PHP vs. Java. In this paper, I am not discussing following two issues since each is currently hotly debated in various communities:

Performance Tools for Parallel Java Environments

Components of a Computing System. What is an Operating System? Resources. Abstract Resources. Goals of an OS. System Software

CrossPlatform ASP.NET with Mono. Daniel López Ridruejo

Programming Languages

CSCI E 98: Managed Environments for the Execution of Programs

ODROID Multithreading in Android

Suh yun Ki m (KIS T) (KIS suhyunk@.com

Chapter 3.2 C++, Java, and Scripting Languages. The major programming languages used in game development.

Fachbereich Informatik und Elektrotechnik SunSPOT. Ubiquitous Computing. Ubiquitous Computing, Helmut Dispert

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

Java Application Development using Eclipse. Jezz Kelway Java Technology Centre, z/os Service IBM Hursley Park Labs, United Kingdom

Kernel Types System Calls. Operating Systems. Autumn 2013 CS4023

Can You Trust Your JVM Diagnostic Tools?

Programming Languages

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Cloud Computing. Up until now

Dynamic Web Programming BUILDING WEB APPLICATIONS USING ASP.NET, AJAX AND JAVASCRIPT

Figure 1: Graphical example of a mergesort 1.

Embedded Software Development

Java CPD (I) Frans Coenen Department of Computer Science

Witango Application Server 6. Installation Guide for OS X

Java Garbage Collection Basics

Java on z/os. Agenda. Java runtime environments on z/os. Java SDK 5 and 6. Java System Resource Integration. Java Backend Integration

Ch. 10 Software Development. (Computer Programming)

INT322. By the end of this week you will: (1)understand the interaction between a browser, web server, web script, interpreter, and database server.

Homeland Security Red Teaming

Creating a Java application using Perfect Developer and the Java Develo...

Parrot in a Nutshell. Dan Sugalski dan@sidhe.org. Parrot in a nutshell 1

Introducing the.net Framework 4.0

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

Tutorial 5: Developing Java applications

An Introduction to Computer Science and Computer Organization Comp 150 Fall 2008

Transcription:

CS 3723: Supplemental Notes on Compilers and Interpreters, 2/14/2008 Compilers and Interpreters Most programs a written in high-level programming languages that are not directly implemented by any real hardware machine. So it is not possible to simply dump the source code onto a computer and expect it to run. There are two basic approaches to bridge the gap between high-level programming languages and machine code: compilation and interpretation. Traditional Compilation. With compilation, some source program P written in some source language L is translated into an equivalent program P in some target language L. The program that does this translation is called a compiler. Typically, the source language L is some high-level programming language (e.g., C), and the target language L is the machine code of some hardware machine. The output program P would then be an executable that can run directly on the hardware. In this way, P is both the output of the compiler program and a program itself. After compilation P can be executed (independently of the compiler) to process input and produce output: Source Program (P) Traditional Compiler Machine Code Program (P ) Interpretation. An alternative approach to program execution is interpretation. An interpreter is a program that executes another program P written in some programming language L by simulating L and directly performing the tasks required by the statements in P while P is running. With an interpreter, there is no independent executable output P. The interpreter itself is the only program that runs directly on L. It knows how to simulate the execution each kind of statement in L, and so it can be used to perform the tasks for each of the statements in P as they would be performed on L. 1

For a non-interactive program P, the interpreter can be seen as taking two inputs (P and the input for P) and directly producing an output. This output must be the same as P would produce if it were executing directly on L: Source Program (P) Interpreter Comparison of Compilation and Interpretation. Compilations happens once (each time the source is modified) whereas interpretation continues throughout every execution of a program. So for long running programs, compilation is almost always faster. In addition, many compilers expend effort to analyze programs and rearrange and optimize the code so that it runs even faster. (A good interpreter might execute a program about 20 slower than the compiled and optimized machine code for the same program.) On the other hand, this translation and optimization takes time, so if you are developing a program, you might get faster turnaround time with a interpreter. In addition, with a optimizing compiler the program that gets executed does not always perform operations in the order that they appear in the program, whereas interpretation executes each statement as it appears in the program. Thus, interpreted programs are often easier to debug. Another issue is the amount of runtime overhead required to implement various features. Languages like Lisp, SmallTalk, and most scripting languages tend to require a lot of decisions and actions to take place in the background that are not explicitly mentioned in the code. One example of this we ve already discussed is garbage collection, but other example include the way variable names are mapped to their contents, complex built-in data types, dynamic loading of new program code, and reflection. In general the runtime performance overhead of these features is the same whether the program is compiled or interpreted, so in more dynamic languages where lots of these decisions occur at runtime, interpretation will not be significantly slower than compilation. These languages are typically implemented with an interpreter and are sometimes called interpreted languages even though a compiler could be written for them. On the other hand, languages that can be analyzed easily and for which many decisions can be decided during compilation (e.g., Fortran, C, C++) benefit from compilation; these are often called compiled languages even though an interpreter could be written for them. 2

Virtual Machines Modern execution environments, however, blur the distinction between compilers and interpreters. On the one hand, modern compilers process provide an ability to separately compile various program modules, and operating systems provide facilities for dynamically loading and linking the modules together when the program starts or even as the program runs. On the other hand, interpreters have evolved into complex virtual machines that can improve performance by selectively compiling parts of the program that it is executing. In addition, a hybrid compilation/interpretation process is often used where the source program is first compiled to an intermediate bytecode, which is then interpreted and/or compiled by a virtual machine. As a concrete example, consider Java or more specifically, Sun s reference implementation of the Java, the Java platform standard edition. There are executable programs that are of interest: javac and java. javac stands for Java compiler but it does not produce executable machine language instead it translates from the Java source programming language into a bytecode for the Java Virtual Machine. 1 A separate stream of bytecode instructions is created for each method and the bytecode for all of the methods in a given class are collected into a Java class file. These class files are the output of compilation, but they are also the input to the Java Virtual Machine (the java executable). From the outside the Java Virtual Machine (JVM) looks a lot like our standard interpreter but instead of having a source programming language as input, it has bytecode as input: Source Code compiler (javac) Bytecode Class File JVM (java) Internally, though things are even more complicated. The Java Virtual Machine loads class files into memory on a class-by-class basis but then executes bytecode stored in the classes on a method-by-method basis. It usually interprets the bytecode for a method the first few times the method is called. After a method is called many times, however, the virtual machine will decide to compile that method to native machine code and then execute that machine code directly. 1 Although, at one point, Sun did actually build a computer chip that could run Java Bytecode as its native machine code instruction set. 3

This process of compiling parts of the program as needed while the program is running is known as Just-in-time (JIT) compilation: Java Class File Class Loader Java Virtual Machine Interpreter Bytecode of a Method JIT Compiler Machine Code for a Method Reflection Garbage Collection In addition to a compiler and/or interpreter, a Java Virtual Machine also includes other runtime facilities that support features such as dynamic class loading, reflection, and garbage collection. Dynamic Loading and Eval One of the more interesting facilities provided by the Java Virtual Machine is the ability to load class files into the virtual machine and link them dynamically to a running program This dynamic class loading happens automatically the first time each class is used, but can also be explicitly requested by the running program to, for example, download a new plugin over the internet and integrate into the running program. Slightly less flexible versions of dynamic loading and linking are provided for traditional compiled languages (e.g., C) by most modern operating systems (e.g., Linux) and through calls to system libraries (e.g., dlopen). An even more flexible version of dynamic loading is available in many interpreted languages. Lisp/Scheme provides an eval function. This function takes a list containing Lisp/Scheme code as an argument ( l e t ( ( code ( cons + ( cons 1 ( cons 1 ( ) ) ) ) ) ) ( cons code ( eval code ) ) ) will evaluate to: ((+ 1 1). 2). 4

Similar statements/operators are provided by most Unix Shells (... and $(...)) and by scripting languages such as Perl (eval "...";). These tend to use program source code stored in strings rather than as lists. This facility is incredibly powerful and can be used in many of the same ways higher-order functions can be used. But it is also dangerous, since the code being incorporated by eval can be anything. Letting user input from an untrustworthy source into the an eval string is an easy way to introduce security vulnerability. For more information: Mitchell, 4.1; Scott, Programming Language Pragmatics, 2nd ed.; Lindholm and Yellin, Java Virtual Machine Specification, 2nd ed.; dlopen man page 5