Slide 1 Vitaly Shmatikov CS 345 Imperative Programming.

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

The University of Adelaide, School of Computer Science
Programming Languages and Paradigms
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
1 Compiler Construction Intermediate Code Generation.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
CS 536 Spring Run-time organization Lecture 19.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Dr. Muhammed Al-Mulhem ICS Chapter 7 Scope, Functions, and Storage Management.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Runtime Environments Source language issues Storage organization
CS 536 Spring Code generation I Lecture 20.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Scope, Function Calls and Storage Management John Mitchell CS
Chapter 9: Subprogram Control
Intermediate Code. Local Optimizations
Run-time Environment and Program Organization
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Scope, Function Calls and Storage Management John Mitchell CS
Slide 1 Vitaly Shmatikov CS 345 Scope and Activation Records.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Imperative Programming
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
CS 2104 Prog. Lang. Concepts Subprograms
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Names. 2 Variables  binding is an association between an entity (such as a variable) and a property (such as its value). A binding is static if the association.
Basic Semantics Associating meaning with language entities.
Programming Languages and Paradigms Imperative Programming.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
SE424 Languages with Context A Block Structured Language.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Slide 1 WEEK 8 Imperative Programming Original by Vitaly Shmatikov.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
7. Runtime Environments Zhang Zhizheng
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Slide 1 Vitaly Shmatikov CS 345 Exceptions. slide 2 Reading Assignment uMitchell, Chapter 8.2.
Names, Scope, and Bindings Programming Languages and Paradigms.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Programming Model CS 333 Sam Houston State University Dr. Tim McGuire.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Scope, Function Calls and Storage Management John Mitchell CS Reading: Chapter 7, Concepts in Programming Languages.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Names and Attributes Names are a key programming language feature
Run-time organization
Chapter 9 :: Subroutines and Control Abstraction
Memory Allocation CS 217.
UNIT V Run Time Environments.
Languages and Compilers (SProg og Oversættere)
Scope, Function Calls and Storage Management
SPL – PS2 C++ Memory Handling.
Presentation transcript:

slide 1 Vitaly Shmatikov CS 345 Imperative Programming

slide 2 Reading Assignment uMitchell, Chapter uC Reference Manual, Chapter 8

slide 3 Imperative Programming uOldest and most popular paradigm Fortran, Algol, C, Java … uMirrors computer architecture In a von Neumann machine, memory holds instructions and data uKey operation: assignment Side effect: updating state (i.e., memory) of the machine uControl-flow statements Conditional and unconditional (GO TO) branches, loops

slide 4 Elements of Imperative Programs uData type definitions uVariable declarations (usually typed) uExpressions and assignment statements uControl flow statements (usually structured) uLexical scopes and blocks Goal: provide locality of reference uDeclarations and definitions of procedures and functions (i.e., parameterized blocks)

slide 5 Variable Declarations uTyped variable declarations restrict the values that a variable may assume during program execution Built-in types (int, char …) or user-defined Initialization: Java integers to 0. What about C? uVariable size How much space needed to hold values of this variable? –C on a 32-bit machine: sizeof(char) = 1 byte, sizeof(short) = 2 bytes, sizeof(int) = 4 bytes, sizeof(char*) = 4 bytes (why?) –What about this user-defined datatype:

slide 6 Variables: Locations and Values uWhen a variable is declared, it is bound to some memory location and becomes its identifier Location could be in global, heap, or stack storage ul-value: memory location (address) ur-value: value stored at the memory location identified by l-value uAssignment: A (target) = B (expression) Destructive update: overwrites the memory location identified by A with a value of expression B –What if a variable appears on both sides of assignment?

slide 7 Copy vs. Reference Semantics uCopy semantics: expression is evaluated to a value, which is copied to the target Used by imperative languages uReference semantics: expression is evaluated to an object, whose pointer is copied to the target Used by object-oriented languages

slide 8 Variables and Assignment uOn the RHS of an assignment, use the variable’s r-value; on the LHS, use its l-value Example: x = x+1 Meaning: “get r-value of x, add 1, store the result into the l-value of x” uAn expression that does not have an l-value cannot appear on the LHS of an assignment What expressions don’t have l-values? –Examples: 1=x+1, ++x++ (why?) –What about a[1] = x+1, where a is an array? Why?

slide 9 l-Values and r-Values (1) uAny expression or assignment statement in an imperative language can be understood in terms of l-values and r-values of variables involved In C, also helps with complex pointer dereferencing and pointer arithmetic uLiteral constants Have r-values, but not l-values uVariables Have both r-values and l-values Example: x=x*y means “compute rval(x)*rval(y) and store it in lval(x)”

slide 10 l-Values and r-Values (2) uPointer variables Their r-values are l-values of another variable –Intuition: the value of a pointer is an address uOverriding r-value and l-value computation in C &x always returns l-value of x *p always return r-value of p –If p is a pointer, this is an l-value of another variable What are the values of p and x at this point?

slide 11 l-Values and r-Values (3) uDeclared functions and procedures Have l-values, but no r-values

slide 12 Turing-Complete Mini-Language uInteger variables, values, operations uAssignment uIf uGo To

slide 13 Structured Control Flow uControl flow in imperative languages is most often designed to be sequential Instructions executed in order they are written Some also support concurrent execution (Java) uProgram is structured if control flow is evident from syntactic (static) structure of program text Big idea: programmers can reason about dynamic execution of a program by just analyzing program text Eliminate complexity by creating language constructs for common control-flow “patterns” –Iteration, selection, procedures/functions

slide 14 Fortran Control Structure 10 IF (X.GT ) GO TO X = -X IF (X.LT ) GO TO IF (X*Y.LT ) GO TO 30 X = X-Y-Y 30 X = X+Y CONTINUE X = A Y = B-A GO TO 11 … Similar structure may occur in assembly code

slide 15 Historical Debate uDijkstra, “GO TO Statement Considered Harmful” Letter to Editor, Comm. ACM, March 1968 Linked from the course website uKnuth, “Structured Prog. with Go To Statements” You can use goto, but do so in structured way … uContinued discussion Welch, “GOTO (Considered Harmful) n, n is Odd” uGeneral questions Do syntactic rules force good programming style? Can they help?

slide 16 Modern Style uStandard constructs that structure jumps if … then … else … end while … do … end for … { … } case … uGroup code in logical blocks uAvoid explicit jumps (except function return) uCannot jump into the middle of a block or function body

slide 17 Iteration uDefinite uIndefinite Termination depends on a dynamically computed value How do we know statically (i.e., before we run the program) that the loop will terminate, i.e., that n will eventually become less than or equal to 0?

slide 18 Iteration Constructs in C while (condition) stmt; while (condition) { stmt; stmt; …; } do stmt while (condition); do { stmt; stmt; …; } while (condition); for ( ; ; ) stmt; –Restricted form of “while” loop – same as ; while ( ) { stmt; } for ( ; ; ) { stmt; stmt; …; }

slide 19 “Breaking Out” Of A Loop in C

slide 20 Forced Loop Re-Entry in C

slide 21 Block-Structured Languages uNested blocks with local variables { int x = 2; { int y = 3; x = y+2; } Storage management –Enter block: allocate space for variables –Exit block: some or all space may be deallocated new variables declared in nested blocks inner block outer block local variable global variable

slide 22 Blocks in Common Languages uExamples C, JavaScript * { … } Algol begin … end ML let … in … end uTwo forms of blocks Inline blocks Blocks associated with functions or procedures –We’ll talk about these later * JavaScript functions provides blocks

slide 23 Simplified Machine Model Registers Environment pointer Program counter DataCode Heap Stack

slide 24 Memory Management uRegisters, Code segment, Program counter Ignore registers (for our purposes) and details of instruction set uData segment Stack contains data related to block entry/exit Heap contains data of varying lifetime Environment pointer points to current stack position –Block entry: add new activation record to stack –Block exit: remove most recent activation record

slide 25 Scope and Lifetime uScope Region of program text where declaration is visible uLifetime Period of time when location is allocated to program Inner declaration of x hides outer one (“hole in scope”) Lifetime of outer x includes time when inner block is executed Lifetime  scope { int x = … ; { int y = … ; { int x = … ; …. };

slide 26 Inline Blocks uActivation record Data structure stored on run-time stack Contains space for local variables May need space for variables and intermediate results like (x+y), (x-y ) { int x=0; int y=x+1; { int z=(x+y)*(x-y); }; Push record with space for x, y Set values of x, y Push record for inner block Set value of z Pop record for inner block Pop record for outer block

slide 27 Activation Record For Inline Block uControl link Pointer to previous record on stack uPush record on stack Set new control link to point to old env ptr Set env ptr to new record uPop record off stack Follow control link of current record to reset environment pointer Control link Local variables Intermediate results Control link Local variables Intermediate results Environment pointer In practice, can be optimized away

slide 28 Example { int x=0; int y=x+1; { int z=(x+y)*(x-y); }; Push record with space for x, y Set values of x, y Push record for inner block Set value of z Pop record for inner block Pop record for outer block Control link x y 0 1 x+y x-y Environment pointer 1 Control link z