Overview. Why Pointers?

Similar documents
Operational Semantics. One-Slide Summary. Lecture Outline

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

Operational Semantics of Cool

(Refer Slide Time: 4:00)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While

Lecture Notes on Memory Layout

Pointers in C/C++ 1 Memory Addresses 2

CSE 303: Concepts and Tools for Software Development

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

Assignment 4: Semantics

G Programming Languages - Fall 2012

Operational Semantics

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Functional Programming Lecture 1: Introduction

CS 206 Introduction to Computer Science II

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Sixth lecture; classes, objects, reference operator.

Singly linked lists in C.


Variables. Substitution

To figure this out we need a more precise understanding of how ML works

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Formal Specification and Verification

Lecture 3: Recursion; Structural Induction

Lecture 1: Overview

Software Engineering using Formal Methods

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

CS 11 Haskell track: lecture 5. This week: State monads

Intro to Algorithms. Professor Kevin Gold

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I

CS558 Programming Languages

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Lecture Notes on Interfaces

These are notes for the third lecture; if statements and loops.

Programming in C ++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lecture 13 Notes Sets

(Refer Slide Time: 06:01)

CS558 Programming Languages

Chamberlin and Boyce - SEQUEL: A Structured English Query Language

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

Lecture 13 Notes Sets

Introduction to Axiomatic Semantics

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

Variables and Bindings

Goals of this Lecture

CS558 Programming Languages

Lecture Transcript While and Do While Statements in C++

Computer Science II Lecture 1 Introduction and Background

Compilers and computer architecture: A realistic compiler to MIPS

D Programming Language

CSE 341: Programming Languages

Separation Logic. COMP2600 Formal Methods for Software Engineering. Rajeev Goré. Australian National University Semester 2, 2016

CSE 307: Principles of Programming Languages

Software Engineering using Formal Methods

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

QUIZ. What is wrong with this code that uses default arguments?

Compilers. Prerequisites

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

CS1 Lecture 3 Jan. 18, 2019

Comp 151. Control structures.

COMP 2355 Introduction to Systems Programming

Lecture 3. Lecture

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

LECTURE 3. Compiler Phases

An aside. Lecture 14: Last time

Lecture 9 Stacks & Queues

Programming Languages

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Flow Control in C++ 1 CS 16: Solving Problems with Computers I Lecture #4

Example: Computing prime numbers

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

CS1 Lecture 3 Jan. 22, 2018

Software Design and Analysis for Engineers

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Homework #2 Think in C, Write in Assembly

CS 206 Introduction to Computer Science II

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

Begin at the beginning

Rules and syntax for inheritance. The boring stuff

Garbage Collection (1)

Transcription:

Overview CS345H: Programming Languages Lecture 16: Imperative Languages II Thomas Dillig Last time, we have seen how we can give meaning to a simple imperative Specifically, we wrote operational semantics for the IMP1 Today: How to give semantics to more feature-rich imperative s Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 1/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 2/30 Pointers Why Pointers? In the IMP1, perhaps the biggest missing feature is pointers A pointer is a reference to a memory location Pointers are naturally supported by hardware through load and store instructions In fact, pretty much all code turns into pointer manipulation at the assembly level What are pointers good for? Call-by-reference in a call-by-value Clever and efficient data structures Avoid copying of data if it can be shared It is not uncommon for pointers to to be 100x faster than copying data! For this reason, pointers are essential for most performance-critical task. Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 3/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 4/30 A Simple Pointer Language Operational Semantics with Pointers Let us consider the following simple with pointers we will call IMP2: P ε P 1 ; P 1 S S if(c ) then S 1 else s 2 fi id = e id = e while(c ) do S od id = alloc e id e 1 + e 2 e 1 e 2 int id C e 1 e 2 e 1 = e 2 notc C 1 and C 2 This is the same as IMP1, just with a load and store operation Here, I am using C syntax for loading and storing We want to give operational semantics to this But how can we handle pointers? Recall: So far, we only had a environment. The environment mapped variables to values But how can we look up the value of a pointer? Addition: Alloc allocates fresh memory Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 5/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 6/30 1

Operational Semantics with Pointers Cont. Idea: Add one level of indirection in the environment. We used to have one environment that maps variables to values Now, we will have: An environment E mapping variables to addresses A store S mapping addresses to values stored at this address The store is emulating memory when executing a program! The Store This means that our operational semantics will now be of the form E, S Specifically, expression rules will be of the form: E, S e : v Conditional rules are of the form: E, S e : bool And statement rules are of the form: E, S e : E, S Statements now both change the environment and the store! Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 7/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 8/30 The Store in Action The alloc Statement Let start with expressions and take a look at the rule for id Recall, in IMP1 the operational semantics for id just returned E(id) Now, let s write the same rule for IMP2: v = S(l 1 ) E, S id : v Intended semantics of alloc: Return a fresh address in S that is not used by anyone else Here are the operational semantics of alloc: l f fresh S = S[l f 0] S = S [E(v) l f ] E, S id = alloc : E, S Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 9/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 10/30 Load in IMP2 Next: The load expression What do we have to do to load a value? Look up the address l 1 of the variable in E Look up the value of l1 in S as v 1 Store in IMP2 Next: The store statement id = e What do we have to do to store a value? Look up the address l1 of the variable v in E Look up the value of l1 in S as l 2 Look up the value of v 1 in S Change the value of l2 in S to e s value Here is the rule for load: v 1 = S(l 1 ) v 2 = S(v 1 ) E, S id : v 2 Here is the rule for store: E, S e : v l 2 = S(l 1 ) S = S[l 2 v] E, S id = e : E, S Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 11/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 12/30 2

Storage for Variables So far, we have been sloppy about the storage associated with variables Specifically, we have assumed that every variable can be looked up in E But this is clearly not the case unless some rule adds them to E! Question: How can we solve this problem? Storage for Variables Cont Solution 1: Two cases for each rule where we use a variables One case if the variable is already in E One case if variable is not yet in E Solution 2: Add variable declarations to our Specifically, add a declare id statement Semantics of declare id: l f fresh E = E[id l f ] E, S declare id : S, E This is the solution preferred by most imperative s Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 13/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 14/30 Aliasing Aliasing Cont. As soon as we allow pointers, we also allow aliasing Two pointers alias if they point to the same memory location Here is a simple example program: declare x, y; x = alloc; y = x; *x = 3; *y = 4; What is the value of *x? In one sense, aliasing is great. In fact, many the cases where pointers are really useful involve some kind of aliasing However, in another sense, aliasing is awful Because of aliasing, storing a value into any location can potentially change every other location s value! This is very bad news for any kind of expressive type system Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 15/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 16/30 Run-time errors Even More Features Another popular feature of imperative s: arrays Question: What kind of new run-time errors can happen in IMP2? Run-time errors everywhere! This is another typical side effect of adding pointers to a Array is nothing but a list of values Indexed by position Corresponds to a contiguous region of memory Popular because fast Accessing an element only requires adding to the base pointer Can perform in-place updates of values Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 17/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 18/30 3

Arrays Arrays Important: What is called an array in Python is not what we are talking about here! Python arrays are lists of values These lists can even contain elements of different type We are talking about the C/Java style array here Important: What is called an array in Python is not what we are talking about here! Python arrays are lists of values These lists can even contain elements of different type We are talking about the C/Java style array here Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 19/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 20/30 Array Language Semantics of IMP3 Consider the following modified we will call IMP3: P ε P 1 ; P 1 S S if(c ) then S 1 else s 2 fi id = e id[e 1 ] = e 2 while(c ) do S od id = alloc declare id e id e 1 + e 2 e 1 e 2 int id[e] C e 1 e 2 e 1 = e 2 not C C 1 and C 2 Observe that load and store are replaces with array load and store pointer arrays are a generalization of pointers The only new statements are array load and array store They replace load and store from IMP2 Question: How can we emulate pointer load and store in IMP3? Answer: Pointer load id = *e is the same as id = e[0] Pointer store *id = e is the same as id[0] = e Also, assume that alloc allocates arrays of infinite size Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 21/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 22/30 On to the Operational Semantics Load in IMP3 What do we have to do to load a value in an array? Fortunately, the only change from IMP2 are the array load and store Specifically, how do we process id[e]? Evaluate e to v i Therefore, we only need to write two new rules Look up the address l 1 of the variable id in E First order of business: Array load Look up the value of l1 in S as v 1 Add the index v i to v 1 as v 2 Look up the value of l2 in S Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 23/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 24/30 4

Load in IMP3 Store in IMP3 Here is the rule for load: E, S e : v i v 1 = S(l 1 ) v 2 = v 1 + v i v 3 = S(v 2 ) E, S id[e] : v 3 Next: The store statement id[e 1 ] = e 2 What do we have to do to store a value? Evaluate e2 to v i Look up the address l 1 of the variable v in E Look up the value of l1 in S as l 2 Observe how this is a generalization of the earlier rule for pointer load Change the value of l 2 + v i in S to e 1 s value Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 25/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 26/30 Store in IMP3 Cont. Arrays Discussion Here is the rule for store: E, S e 1 : v i E, S e 2 : v l 2 = S(l 1 ) l 3 = l 2 + v i S = S[l 3 v] E, S id[e 1 ] = e 2 : E, S Again, this is a direct generalization of the store rule in IMP3 We have seen how to add pointer arrays to an imperative However, it is also possible to add arrays without introducing pointers In this case, it is possible to get away without using a store You will write semantics for an array without pointers on the homework Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 27/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 28/30 Further Features The imperative s we studied still lack many features of real s Some features we did not discuss: How to handle different types Casting Expressions with side effects (e.g., i++) But we covered all the important basics! Further Features Cont. If you want to add a new feature, first think if you need more information! Sometimes, you need another mapping (like environment E and store S) In general, there are many correct ways to add features to operational semantics But your goal is to add them cleanly! If you think a little, you can now write semantics for any missing feature Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 29/30 Thomas Dillig, CS345H: Programming Languages Lecture 16: Imperative Languages II 30/30 5