CS 314 Principles of Programming Languages. Lecture 11

Similar documents
CS 314 Principles of Programming Languages

Class Information ANNOUCEMENTS

CS 314 Principles of Programming Languages. Lecture 13

Programming Languages

CS 314 Principles of Programming Languages. Lecture 9

G Programming Languages - Fall 2012

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

CS 314 Principles of Programming Languages

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

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

Lecture 7: Binding Time and Storage

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

EL2310 Scientific Programming

CSC 1600 Memory Layout for Unix Processes"

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

ECE 15B COMPUTER ORGANIZATION

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Please refer to the turn-in procedure document on the website for instructions on the turn-in procedure.

Name, Scope, and Binding. Outline [1]

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Chap. 8 :: Subroutines and Control Abstraction

Kurt Schmidt. October 30, 2018

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Dynamic Data Structures. CSCI 112: Programming in C

Binding and Variables

Intermediate Programming, Spring 2017*

The compilation process is driven by the syntactic structure of the program as discovered by the parser

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

Semantic Analysis and Type Checking

G Programming Languages - Fall 2012

5. Semantic Analysis!

Language comparison. C has pointers. Java has references. C++ has pointers and references

multiple variables having the same value multiple variables having the same identifier multiple uses of the same variable

Chapter 5. Names, Bindings, and Scopes

Lecture 2: C Programm

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS558 Programming Languages

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

In Java we have the keyword null, which is the value of an uninitialized reference type

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

Homework #3 CS2255 Fall 2012

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

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

Runtime Support for Algol-Like Languages Comp 412

211: Computer Architecture Summer 2016

Lecture 3: C Programm

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Contents of Lecture 3

Programming Languages

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

CS61C : Machine Structures

Operating System Labs. Yuanbin Wu

CS 415 Midterm Exam Spring 2002

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

Final CSE 131B Spring 2004

Class Information ANNOUCEMENTS

EC 413 Computer Organization

Lecture 8 Dynamic Memory Allocation

Chapter 5 Names, Bindings, Type Checking, and Scopes

Run Time Environments

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

SYSC 2006 C Winter 2012

Arrays, Pointers and Memory Management

Lectures 13 & 14. memory management

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

Chapter 9 :: Subroutines and Control Abstraction

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Memory Management and Run-Time Systems

Operational Semantics. One-Slide Summary. Lecture Outline

Run-Time Environments

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

CS 345. Functions. Vitaly Shmatikov. slide 1

Procedure and Object- Oriented Abstraction

[0569] p 0318 garbage

CS558 Programming Languages

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

CS61C : Machine Structures

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

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

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

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

CS558 Programming Languages

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

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

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

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

CSE 333 Lecture 2 Memory

CS61, Fall 2012 Section 2 Notes

Concepts Introduced in Chapter 7

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

CS61C : Machine Structures

Lectures 5-6: Introduction to C

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Transcription:

CS 314 Principles of Programming Languages Lecture 11 Zheng Zhang Department of Computer Science Rutgers University Wednesday 12 th October, 2016 Zheng Zhang 1 eddy.zhengzhang@cs.rutgers.edu

Class Information Homework 4 due this Friday 11:55pm. Project 1 is posted, due Sunday 10/23 11:55pm. Zheng Zhang 2 eddy.zhengzhang@cs.rutgers.edu

Project 1: Overview compiler tinyl program example: test1 compile example: compile test1 tinyl.out output always "tinyl.out" optimizer RISC machine code example: tinyl.out optimize example: optimize < tinyl.out RISC machine code output to stdout RISC machine code example: tinyl.out virtual machine run input and output of execution Zheng Zhang 3 eddy.zhengzhang@cs.rutgers.edu

Project 1: Dead Code Elimination Redundant Code Elimination: Eliminate code without changing the semantics of the program. If the execution of an operation or instruction does not contribute to the input/output behavior of the program, the instruction is considered dead code and therefore can be eliminated. Example: Original Code LOADI Rx #c1 LOADI Ry #c2 LOADI Rz #c3 ADD R1 Rx Ry MUL R2 Rx Ry STORE a R1 WRITE a Optimized Code LOADI Rx #c1 LOADI Ry #c2 ADD R1 Rx Ry STORE a R1 WRITE a See project description for more details. Zheng Zhang 4 eddy.zhengzhang@cs.rutgers.edu

Review: Program Memory Layout Review: Storage Management Static objects are given an absolute address that is retained throughout program s execution Stack objects are allocated and deallocated in last-in, first-out order, usually in conjunction with subroutine calls and returns. Heap objects may be allocated and deallocated at arbitrary times. stack heap uninitialized data bss initialized data data text Zheng Zhang 5 eddy.zhengzhang@cs.rutgers.edu

Maintaining Free List allocate: continuous block of memory; remove space from free list (here: linked list). free: return to free list after coalescing with adjacent free storage (if possible); may initiate compaction. free space pointer ALLOCATE free space pointer FREE free space pointer free space pointer Zheng Zhang 6 eddy.zhengzhang@cs.rutgers.edu

Review: What went wrong? Aliasing and freeing memory #include <stdio.h> #include <stdlib.h> int main(void) { int *a = NULL; int *b = NULL; int *c = NULL; a = (int *) malloc(sizeof(int)); b = a; *a = 12; printf("%x %x: %d\n", &a, a, *a); printf("%x %x: %d\n", &b, b, *b); free(a); printf("%x %x: %d\n", &b, b, *b); c = (int *) malloc(sizeof(int)); *c = 10; printf("%x %x: %d\n", &c, c, *c); printf("%x %x: %d\n", &b, b, *b); } > a.out effff60c 209d0: 12 effff608 209d0: 12 effff608 209d0: 12 effff604 209d0: 10 effff608 209d0: 10 Zheng Zhang 7 eddy.zhengzhang@cs.rutgers.edu

What went wrong? Use a subroutine to create an object #include <stdio.h> #include <stdlib.h> /* TYPE DEFINITION */ typedef struct cell listcell; struct cell { int num; listcell *next; }; listcell *head = NULL; listcell *create_listcell() { listcell new; new.num = -1; new.next = NULL; return &new; } int main (void) { head = create_listcell(); printf("head->num = %d\n", head->num); } \em > gcc stack.c ~ stack.c: In function create\_listcell : ~ stack.c:17: warning: function returns address of local variable >./a.out head->num = -1 Zheng Zhang 8 eddy.zhengzhang@cs.rutgers.edu

What went wrong? Use a subroutine to create an object: malloc #include <stdio.h> #include <stdlib.h> /* TYPE DEFINITION */ typedef struct cell listcell; struct cell { int num; listcell *next; }; listcell *head = NULL; listcell *create_listcell() { listcell *new; new = (listcell *) malloc(sizeof(listcell)); new->num = -1; new->next = NULL; return new; } int main (void) { head = create_listcell(); printf("head->num = %d\n", head->num); } > gcc heap.c >./a.out head num = -1 Zheng Zhang 9 eddy.zhengzhang@cs.rutgers.edu

Review: Pointers and Arrays in C Pointers and arrays are similar in C: array name is pointer to a[0]: after int a[10]; int *pa; pa = &a[0]; pa and a have the same semantics pointer arithmetic is array indexing pa+1 and a+1 point to a[1] exception: an array name is not a variable a++ is ILLEGAL a=pa is ILLEGAL (pa=a is LEGAL!) Zheng Zhang 10 eddy.zhengzhang@cs.rutgers.edu

Names, Bindings, and Memory Scott: Chap. 3.1-3.4; ALSU Chap. 7.1-7.3 What s in a name? each name means something! denotes a programming language construct has associated attributes (e.g.: type, memory location, read/write permission, storage class, access restrictions, etc.) has a meaning, i.e., represents a semantic object (e.g.: a type description, an integer value, a function value, a memory address, etc. ) Zheng Zhang 11 eddy.zhengzhang@cs.rutgers.edu

Names, Bindings, and Memory Binding association of a name with the thing it names (e.g., a name and a memory location, a function name and its meaning, a name and a value) Compile time during compilation process static (e.g.: macro expansion, type definitions) Link time separately compiled modules/files are joined together by the linker (e.g., adding the standard library routines for I/O (stdio.h), external variables) Run time when program executes dynamic Compiler needs bindings to know meaning of names during translation (and execution). Zheng Zhang 12 eddy.zhengzhang@cs.rutgers.edu

Binding Times - Choices Early binding times more efficient (faster) at runtime Late binding times more flexible (postpone binding decision until more information is available) Examples of static binding (early): functions in C types in C Examples of dynamic binding (late): method calls in Java dynamic typing in JavaScript, Scheme Note: dynamic linking is somewhat in between static and dynamic binding; the funtion signature has to be known (static), but the implementation is linked and loaded at run time (dynamic). Zheng Zhang 13 eddy.zhengzhang@cs.rutgers.edu

How to Maintain Bindings symbol table: maintained by compiler during compilation names attributes environment: maintained by compiler generated code during program execution names memory locations Questions How long do bindings for a name hold in a program? What initiates a binding? What ends a binding? Zheng Zhang 14 eddy.zhengzhang@cs.rutgers.edu

Scope Example Algol-like Programming Languages program L; var n: char; {n declared in L} procedure W; begin write(n); {n referenced in W} end; procedure D; var n: char; {n declared in D} begin n:= D ; {n referenced in D} W end; begin n:= L ; {n referenced in L} W; D end. Zheng Zhang 15 eddy.zhengzhang@cs.rutgers.edu

Lexical Scope Non-local variables are associated with declarations at compile time Find the smallest block syntactically enclosing the reference and containing a declaration of the variable Example: The reference to n in W is associated with the declaration of n in L The output is? Zheng Zhang 16 eddy.zhengzhang@cs.rutgers.edu

Dynamic Scope Non-local variables are associated with declarations at run time Find the most recent, currently active run-time stack frame containing a declaration of the variable Example: The reference to n in W is associated with two different declarations at two different times The output is? Zheng Zhang 17 eddy.zhengzhang@cs.rutgers.edu

Procedure Activations Begins when control enters activation (call) Ends when control returns from activation Example: Zheng Zhang 18 eddy.zhengzhang@cs.rutgers.edu

Stack Frame, Activation Record Scott: Chap. 9.1-9.3; ALSU Chap. 7.1-7.3 Run-time stack contains frames for main program and each active procedure. Each stack frame includes: 1. Pointer to stack frame of caller (control link for stack maintainance and Direction of stack growth dynamic scoping) (lower addresses) 2. Return address (within calling procedure) fp 3. Mechanism to find non-local variables (access link for lexical scoping) 4. Storage for parameters, local variables, and final values sp Arguments to called routines Temporaries Local variables Saved regs., static link Saved fp Return address (Arguments from caller) Current frame Previous (calling) frame Zheng Zhang 19 eddy.zhengzhang@cs.rutgers.edu

Next Lecture Things to do: Start working on the project. Due Sunday October 23. Read Scott: Chap. 3.1-3.4, 9.1-9.3 ; Next time: Access link and display, parameter passing styles and their implementation. Zheng Zhang 20 eddy.zhengzhang@cs.rutgers.edu