Principles of Programming Languages

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

6. Names, Scopes, and Bindings

NAMES, SCOPES AND BINDING A REVIEW OF THE CONCEPTS

Names and Abstractions: What s in a Name?

Principles of Programming Languages

G Programming Languages - Fall 2012

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Chapter 3:: Names, Scopes, and Bindings

Programming Languages

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Name, Scope, and Binding. Outline [1]

Chapter 5. Names, Bindings, and Scopes

Programming Languages Third Edition. Chapter 7 Basic Semantics

Concepts Introduced in Chapter 7

Chapter 5 Names, Binding, Type Checking and Scopes

MIDTERM REVIEW. Lectures 1-15

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Research opportuni/es with me

Run-time Environments

Run-time Environments

Informatica 3 Syntax and Semantics

Lecture 7: Binding Time and Storage

Chapter 4: Memory. Taylor & Francis Adair Dingle All Rights Reserved

Short Notes of CS201

Implementing Subroutines. Outline [1]

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

CS201 - Introduction to Programming Glossary By

G Programming Languages - Fall 2012

CSC 533: Organization of Programming Languages. Spring 2005

Imperative Programming

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Programming Languages, Summary CSC419; Odelia Schwartz

Principles of Programming Languages

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

Programmiersprachen (Programming Languages)

CST-402(T): Language Processors

Programming Languages 2nd edition Tucker and Noonan"

Memory Management. CS449 Fall 2017

One Ring to rule them all, One Ring to bring them all,

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Principles of Programming Languages

Run Time Environments

Principles of Programming Languages

Chapter 5 Names, Bindings, Type Checking, and Scopes

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation

COS 140: Foundations of Computer Science

Principles of Programming Languages

TYPES, VALUES AND DECLARATIONS

W1005 Intro to CS and Programming in MATLAB. Brief History of Compu?ng. Fall 2014 Instructor: Ilia Vovsha. hip://

Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

Memory Management and Run-Time Systems

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

CS 415 Midterm Exam Spring SOLUTION

Semester Review CSC 301

Fundamentals of Programming Languages

CS558 Programming Languages. Winter 2013 Lecture 3

10. Abstract Data Types

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

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Binding and Variables

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

CS 101: Computer Programming and Utilization

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

EDAN65: Compilers, Lecture 13 Run;me systems for object- oriented languages. Görel Hedin Revised:

References and pointers

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Semantic Analysis and Type Checking

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

CSE Opera,ng System Principles

Principles of Programming Languages

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Concepts of Programming Languages

Chapter 8 :: Composite Types

Types and Type Inference

names names identifiers variables subroutines constants

NOTE: Answer ANY FOUR of the following 6 sections:

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

Principles of Programming Languages

CS 3360 Design and Implementation of Programming Languages. Exam 1

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Chapter 3:: Names, Scopes, and Bindings (cont.)

Names, Bindings, Scopes

CS 314 Principles of Programming Languages. Lecture 11

Chapter 3:: Names, Scopes, and Bindings (cont.)

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Dynamic Languages. CSE 501 Spring 15. With materials adopted from John Mitchell

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Types II. Hwansoo Han

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Run-time Environments - 2

Ways to implement a language

Genericity. Philippe Collet. Master 1 IFI Interna3onal h9p://dep3nfo.unice.fr/twiki/bin/view/minfo/sofeng1314. P.

CS 415 Midterm Exam Spring 2002

A Func'onal Space Model

Transcription:

Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 18! Bootstrapping Names in programming languages Binding <mes Object alloca<on: sta<c 1

Compilers, graphically Three languages involved in wri<ng a compiler Source Language (S) Target Language (T) Implementa<on Language (I) T- Diagram: S T If I = T we have a Host Compiler I If S, T, and I are all different, we have a Cross- Compiler 2

Composing compilers Compiling a compiler we get a new one: the result is described by composing T- diagrams S I I S Example: S Pascal I C 68000 A compiler of S to can be wrien in any language having a host compiler for 3

Composing compilers Compiling a compiler we get a new one: the result is described by composing T- diagrams S T I I S T S I I S Example: S Pascal I C 68000 4

Bootstrapping Bootstrapping: techniques which use par<al/inefficient compiler versions to generate complete/beer ones OUen compiling a translator programmed in its own language Why wri<ng a compiler in its own language? it is a non- trivial test of the language being compiled compiler development can be done in the higher level language being compiled. improvements to the compiler s back- end improve not only general purpose programs but also the compiler itself it is a comprehensive consistency check as it should be able to reproduce its own object code 5

Compilers: Portability Criteria Portability Retargetability Rehostability A retargetable compiler is one that can be modified easily to generate code for a new target language A rehostable compiler is one that can be moved easily to run on a new machine A portable compiler may not be as efficient as a compiler designed for a specific machine, because we cannot make any specific assump<on about the target machine 6

Using Bootstrapping to port a compiler We have a host compiler/interpreter of L for Write a compiler of L to N in language L itself Example: L Pascal P- code L N L L L N L N L N L L N N 7

Bootstrapping to op<mize a compiler The efficiency of programs and compilers: Efficiency of programs: memory usage run<me Efficiency of compilers: Efficiency of the compiler itself Efficiency of the emied code Idea: Start from a simple compiler (genera<ng inefficient code) and develop more sophis<cated version of it. We can use bootstrapping to improve performance of the compiler. 8

Bootstrapping to op<mize a compiler We have a host compiler of ADA to Write an op<mizing compiler of ADA to in ADA ADA * ADA ADA ADA * ADA * ADA * ADA ADA * * 9

Full Bootstrapping A full bootstrap is necessary when building a new compiler from scratch. Example: We want to implement an Ada compiler for machine. We don t have access to any Ada compiler Idea: Ada is very large, we will implement the compiler in a subset of Ada (call it Ada 0 ) and bootstrap it from a subset of Ada compiler in another language (e.g. C) 10

Full Bootstrapping (2) Step 1: build a compiler of Ada 0 to in another language, say C Ada 0 Step 2: compile it using a host compiler of C for C Ada 0 Ada 0 v1 C C Note: new versions would depend on the C compiled for 11

Full Bootstrapping (3) Step 3: Build another compiler of Ada 0 in Ada 0 Ada 0 Ada 0 Step 4: compile it using the Ada 0 compiler for Ada 0 Ada 0 v2 Ada 0 Ada 0 v1 Note: C compiler is no more necessary 12

Full Bootstrapping (4) Step 5: Build a full compiler of Ada in Ada 0 Ada Ada 0 Step 4: compile it using the second Ada 0 compiler for Ada Ada Ada 0 Ada 0 v2 Future versions of the compiler can be wrien directly in Ada 13

Names, Binding and Scope: Summary Abstrac<ons and names Binding <me Object life<me Object storage management Sta<c alloca<on Stack alloca<on Heap alloca<on 14

Name and abstrac<on Names used by programmers to refer to variables, constants, opera<ons, types, Names are fundamental for abstrac<on mechanisms Control abstrac<on: Subrou<nes (procedures and func<ons) allow programmers to focus on manageable subset of program text, hiding implementa<on details Control flow constructs (if- then, while, for, return) hide low- level machine ops Data abstrac<on: Object- oriented classes hide data representa<on details behind a set of opera<ons Abstrac<on in the context of high- level programming languages refers to the degree or level of working with code and data Enhances the level of machine- independence 15

Binding Time A binding is an association between a name and an entity An entity that can have an associated name is called denotable Binding time is the time at which a decision is made to create a name entity binding (the actual binding can be created later): Language design time: the design of specific program constructs (syntax), primitive types, and meaning (semantics) Language implementation time: fixation of implementation constants such as numeric precision, run-time memory sizes, max identifier name length, number and types of built-in exceptions, etc. (if not fixed by the language specification) 16

Binding Time (2) Program writing time: the programmer s choice of algorithms and data structures Compile time: the time of translation of high-level constructs to machine code and choice of memory layout for data objects Link time: the time at which multiple object codes (machine code files) and libraries are combined into one executable (e.g. external names are bound) Load time: when the operating system loads the executable in memory (e.g. physical addresses of static data) Run time: when a program executes 17

Binding Time Examples Language design: Syntax (names grammar) if (a>0) b:=a; (C syntax style) if a>0 then b:=a end if (Ada syntax style) Keywords (names builtins) class (C++ and Java), endif or end if (Fortran, space insignificant) Reserved words (names special constructs) main (C), writeln (Pascal) eaning of operators (operator operation) + (add), % (mod), ** (power) Built-in primitive types (type name type) float, short, int, long, string 18

Binding Time Examples (cont d) Language implementa<on Internal representa<on of types and literals (type byte encoding, if not specified by language) 3.1 (IEEE 754) and "foo bar" (\0 terminated or embedded string length) Storage alloca<on method for variables (sta<c/stack/ heap) Compile <me The specific type of a variable in a declara<on (name type) Storage alloca<on mechanism for a global or local variable (name alloca<on mechanism) 19

Binding Time Examples (cont d) Linker Linking calls to static library routines (function address) printf (in libc) erging and linking multiple object codes into one executable Loader Loading executable in memory and adjusting absolute addresses ostly in older systems that do not have virtual memory Run time Dynamic linking of libraries (library function library code) DLL, dylib Nonstatic allocation of space for variable (variable address) Stack and heap 20

The Effect of Binding Time Early binding,mes (before run <me) are associated with greater efficiency and clarity of program code Compilers make implementa<on decisions at compile <me (avoiding to generate code that makes the decision at run <me) Syntax and sta<c seman<cs checking is performed only once at compile <me and does not impose any run- <me overheads Late binding,mes (at run <me) are associated with greater flexibility (but may leave programmers some<mes guessing what s going on) Interpreters allow programs to be extended at run <me Languages such as Smalltalk- 80 with polymorphic types allow variable names to refer to objects of mul<ple types at run <me ethod binding in object- oriented languages must be late to support dynamic binding Usually stauc means before run<me, dynamic at run<me 21

Binding Life<me versus Object Life<me Key events in object life<me: Object crea<on Crea<on of bindings The object is manipulated via its binding Deac<va<on and reac<va<on of (temporarily invisible) bindings Destruc<on of bindings Destruc<on of objects Binding lifeume: <me between crea<on and destruc<on of binding to object Example: a pointer variable is set to the address of an object Example: a formal argument is bound to an actual argument Object lifeume: <me between crea<on and destruc<on of an object 22

Binding Life<me versus Object Life<me (cont d) Bindings are temporarily invisible when code is executed where the binding (name object) is out of scope emory leak: object never destroyed (binding to object may have been destroyed, rendering access impossible) Dangling reference: object destroyed before binding is destroyed Garbage collection: prevents these allocation/deallocation problems 23

C++ Example { } SomeClass* myobject = new SomeClass;... { OtherClass myobject;... // the myobject name is bound to other object... }... // myobject binding is visible again... myobject->action() // myobject in action(): // the name is not in scope // but object is bound to this delete myobject;...... // myobject is a dangling reference 24

Object Storage Objects (program data and code) have to be stored in memory during their life<me StaUc objects have an absolute storage address that is retained throughout the execu<on of the program Global variables and data Subrou<ne code and class method code Stack objects are allocated in last- in first- out order, usually in conjunc<on with subrou<ne calls and returns Actual arguments passed by value to a subrou<ne Local variables of a subrou<ne Heap objects may be allocated and deallocated at arbitrary <mes, but require an expensive storage management algorithm Example: Lisp lists Example: Java class instances are always stored on the heap 25

Typical Program and Data Layout in emory Upper addr Virtual memory address space 0000 stack heap static data code Program code is at the bottom of the memory region (code section) The code section is protected from run-time modification by the OS Static data objects are stored in the static region Stack grows downward Heap grows upward 26

Sta<c Alloca<on Program code is sta<cally allocated in most implementa<ons of impera<ve languages Sta<cally allocated variables are history sensiuve Global variables keep state during en<re program life<me Sta<c local variables in C func<ons keep state across func<on invoca<ons Sta<c data members are shared by objects and keep state during program life<me Advantage of sta<cally allocated object is the fast access due to absolute addressing of the object So why not allocate local variables sta<cally? Problem: sta<c alloca<on of local variables cannot be used for recursive subrou<nes: each new func<on instan<a<on needs fresh locals 27

Sta<c Alloca<on in Fortran 77 Temporary storage (e.g. for expression evaluation) Local variables Bookkeeping (e.g. saved CPU registers) Return address Subroutine arguments and returns Typical static subroutine frame layout Fortran 77 has no recursion Global and local variables are statically allocated as decided by the compiler Global and local variables are referenced at absolute addresses Avoids overhead of creation and destruction of local objects for every subroutine call Each subroutine in the program has a subroutine frame that is statically allocated This subroutine frame stores all subroutine-relevant data that is needed to execute 28

Stack Alloca<on Each instance of a subrou<ne that is ac<ve has a subrou&ne frame (some<mes called ac&va&on record) on the run- <me stack Compiler generates subrou<ne calling sequence to setup frame, call the rou<ne, and to destroy the frame auerwards ethod invoca<on works the same way, but in addi<on methods are typically dynamically bound Subrou<ne frame layouts vary between languages, implementa<ons, and machine plaqorms 29