Lecture 2: Translators & Virtual Machines

Similar documents
Lecture 1: Introduction

CSCI 3136 Principles of Programming Languages

1/20/2016 INTRODUCTION

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

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

CA Compiler Construction

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Lab Experience 17. Programming Language Translation

Compiler Construction

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Semester Review. CSC 301, Fall 2015

Lecture 9. Semantic Analysis Scoping and Symbol Table

Principles of Programming Languages Topic: Introduction Professor Louis Steinberg

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

Chapter 5 Names, Bindings, Type Checking, and Scopes

Programming Languages

03 - Lexical Analysis

Semantic Analysis: Types and Type Checking

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

2) Write in detail the issues in the design of code generator.

Levels of Programming Languages. Gerald Penn CSC 324

Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.

Programming Languages

Symbol Tables. Introduction

CSC Software II: Principles of Programming Languages

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Lexical analysis FORMAL LANGUAGES AND COMPILERS. Floriano Scioscia. Formal Languages and Compilers A.Y. 2015/2016

Functional Programming

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

1.1 WHAT IS A PROGRAMMING LANGUAGE?

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

Compiler Construction

Introduction. Compiler Design CSE 504. Overview. Programming problems are easier to solve in high-level languages

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

Compiler I: Syntax Analysis Human Thought

Language Processing Systems

Memory management basics (1) Requirements (1) Objectives. Operating Systems Part of E1.9 - Principles of Computers and Software Engineering

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

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

CSE 307: Principles of Programming Languages

Moving from CS 61A Scheme to CS 61B Java

CSE 130 Programming Language Principles & Paradigms

History OOP languages Year Language 1967 Simula Smalltalk

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

How to make the computer understand? Lecture 15: Putting it all together. Example (Output assembly code) Example (input program) Anatomy of a Computer

Programming Language Concepts for Software Developers

What is a programming language?

The programming language C. sws1 1

Programming Languages

Image credits:

McGraw-Hill The McGraw-Hill Companies, Inc.,

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

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

CS 51 Intro to CS. Art Lee. September 2, 2014

Chapter 6: Programming Languages

Programming Language Pragmatics

COMPUTER SCIENCE. 1. Computer Fundamentals and Applications

æ A collection of interrelated and persistent data èusually referred to as the database èdbèè.

High level code and machine code

Ch. 10 Software Development. (Computer Programming)

C Compiler Targeting the Java Virtual Machine

CS 40 Computing for the Web

Ed. v1.0 PROGRAMMING LANGUAGES WORKING PAPER DRAFT PROGRAMMING LANGUAGES. Ed. v1.0

Programming Project 1: Lexical Analyzer (Scanner)

Announcements FORTRAN ALGOL COBOL. Simula & Smalltalk. Programming Languages

Chapter 13: Program Development and Programming Languages

PROBLEMS (Cap. 4 - Istruzioni macchina)

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Query optimization. DBMS Architecture. Query optimizer. Query optimizer.

Software: Systems and. Application Software. Software and Hardware. Types of Software. Software can represent 75% or more of the total cost of an IS.

Module: Software Instruction Scheduling Part I

Scanning and parsing. Topics. Announcements Pick a partner by Monday Makeup lecture will be on Monday August 29th at 3pm

Programming Language Inter-conversion

Stack Allocation. Run-Time Data Structures. Static Structures

Departamento de Investigación. LaST: Language Study Tool. Nº 143 Edgard Lindner y Enrique Molinari Coordinación: Graciela Matich

Sources: On the Web: Slides will be available on:

Chapter 13: Program Development and Programming Languages

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

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

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

CS143 Handout 18 Summer 2012 July 16 th, 2012 Semantic Analysis

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Lecture Outline. Stack machines The MIPS assembly language. Code Generation (I)

Machine-Code Generation for Functions

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Algorithm & Flowchart & Pseudo code. Staff Incharge: S.Sasirekha

Object-Oriented Software Specification in Programming Language Design and Implementation

Parameter passing in LISP

ATSBA: Advanced Technologies Supporting Business Areas. Programming with Java. 1 Overview and Introduction

1 The Java Virtual Machine

Concepts of Programming Languages: A Unified Approach. Karl Abrahamson

How To Understand Programming Languages And Programming Languages

Programming Assignment II Due Date: See online CISC 672 schedule Individual Assignment

Instructor Özgür ZEYDAN BEU Dept. of Enve. Eng. CIV 112 Computer Programming Lecture Notes (1)

The C Programming Language course syllabus associate level

Programming Languages CIS 443

Parsing Technology and its role in Legacy Modernization. A Metaware White Paper

Transcription:

Programming Languages Lecture 2: Translators & Virtual Machines Benjamin J. Keller Department of Computer Science, Virginia Tech

Programming Languages Lecture 5 Translators 2 Lecture Outline Abstraction Translators Compilers Interpreters Semantics

Programming Languages Lecture 5 Translators 3 Languages as Abstraction Programming language creates a virtual machine for programmer Dijkstra: Originally we were obligated to write programs so that a computer could execute them. Now we write the programs and the computer has the obligation to understand and execute them. Progress in programming languages characterized by increasing support of abstraction

Programming Languages Lecture 5 Translators 4 Virtual Machines Machine language provides raw machine to execute programs Even machine language abstracts away from physical nature of computation Higher level language presents programmer with a more abstract model of a computer Language creates the illusion of a more sophisticated virtual machine Program Data Virtual Machine Results

Programming Languages Lecture 5 Translators 5 Virtual Machines (cont) Virtual machines built-up using translators and other virtual machines C++ Virtual Machine Assembler Virtual Machine C++ Program C++ Compiler Assembly Program Assembler Machine Program Physical Computer Data (Virtual) Machine language is instruction set supported by translator Virtual machine determined by implementation

Programming Languages Lecture 5 Translators 6 Problems Possibilities Different implementors have different conceptions of virtual machine Different computers Implementors may choose different ways to simulate virtual machine Combined can lead to different implementations (on same machine) How can we ensure that all implementations have same semantics?

Programming Languages Lecture 5 Translators 7 Explicit Virtual Machines Pascal P-code and P-machine Modula-2 M-code and M-machine Java Bytecode and Java virtual machine Prolog and Warren Abstract Machine

Programming Languages Lecture 5 Translators 8 Translators Some high-level languages have special purpose processors (LISP machine) Most high-level languages must be translated Two types of translators interpreter compiler Usually combination of the two

Programming Languages Lecture 5 Translators 9 Pure Interpreter Simulate virtual machine Repeat Get next statement Determine action(s) Call routines to perform action(s) Until done Source Program Data Interpreter Virtual Mach. Results

Programming Languages Lecture 5 Translators 10 Pure Compiler Executing program 1. Translates all units of program into object code (e.g., machine language) 2. Link into single relocatable machine code 3. Load into Memory Source Program Source Virtual Machine Compiler Object Program Object Computer Results Data

Programming Languages Lecture 5 Translators 11 Compilation vs Interpretation Compiler Each statement translated once Must compile Faster execution Only object code in memory when executing Object file likely large Interpreter Translate only if executed Run immediately Allows more supportive environment Interpreter in memory Source likely smaller

Programming Languages Lecture 5 Translators 12 Mixed Translation Rare to have pure compiler or pure interpreter Typically compile into easier to interpret form Example: abstract syntax tree (compact representation of parse tree) Usually go farther into intermediate code (Java Bytecode) and then interpret

Programming Languages Lecture 5 Translators 13 Compilation Two primary phases 1. Analysis: tokenize, parse, generate simple intermediate code (type checking) 2. Synthesis: optimization (instructions in context), code generation, linking and loading To build portable compiler, separate into front- and back-end Front-end does analysis and some optimization of intermediate code Back-end does code generation and full optimization

Programming Languages Lecture 5 Translators 14 Analysis Phase 1. Lexical analysis break program into lexemes (identifiers, operation symbols, keywords, punctuation, comments, etc.) Convert lexeme into token consisting of lexeme and token type. 2. Syntactical analysis Use formal grammar to parse program and build parse tree (maybe implicitly). Add identifiers to symbol table. 3. Semantic analysis Update symbol table (e.g., add type info). Insert implicit information (resolve overloaded operations), error detection, type checking. Traverse parse tree generating intermediate code.

Programming Languages Lecture 5 Translators 15 Synthesis Phase 1. Optimization Find adjacent store-reload instructions, evaluate common sub-expressions, move static code out of loops, allocate registers, etc. 2. Code generation Generate assembly or machine code (sometimes C) 3. Linking and Loading Resolve external references between separately compiled units. Make sure that all references are relative to beginning of program. Load program into memory.

Programming Languages Lecture 5 Translators 16 Compiler Structure Source Program Lexical Analysis Lexical Items Syntactic Analysis Parse Tree Semantic Analysis Intermediate Code Symbol & Other Tables Optimization Optimized Intermediate Code Code Generation Object Code Linking & Loading Executable Object Program Object Code

Programming Languages Lecture 5 Translators 17 Symbol Table Contains 1. identifier names 2. kind of identifier (variable, array, procedure, formal parameter) 3. type 4. visibility Used to check for errors and code generation. May be thrown away at end of compilation Kept for error reporting or dynamic name generation

Programming Languages Lecture 5 Translators 18 Semantics Meaning of program Formal semantics (operational) How would an interpreter for the language work on a virtual machine? Work with virtual/abstract machine when discussing semantics of programming language constructs. Represent data and code portions of memory Instruction pointer, ip, incremented after each instruction if not explicitly modified Run program by loading it into memory and initializing ip to beginning of program.

Programming Languages Lecture 5 Translators 19 Official Language Definitions Standardize syntax and semantics to promote portability All compilers should accept the same programs All legal programs should give the same answers (modulo differences in arithmetic, etc.) Designed for compiler writers and as programmer reference

Programming Languages Lecture 5 Translators 20 Official Definitions (cont) Often better to standardize after gaining implementation experience (Ada standard before implemented) Common Lisp, Scheme, ML now standardized, Fortran 90, C++ Standards include good formal definitions of syntax, but semantics still too hard Backus promised formal semantics in Algol 60 report still waiting!