Imperative Programming

Relevanta dokument
VHDL Basics. Component model Code model Entity Architecture Identifiers and objects Operations for relations. Bengt Oelmann -- copyright

Isolda Purchase - EDI

Support Manual HoistLocatel Electronic Locks

Preschool Kindergarten

Installation av F13 Bråvalla

Isometries of the plane

GU / Chalmers Campus Lindholmen Tentamen Programutveckling LEU 482 / TIG167

DVG C01 TENTAMEN I PROGRAMSPRÅK PROGRAMMING LANGUAGES EXAMINATION :15-13: 15

Vad kännetecknar en god klass. Vad kännetecknar en god klass. F12 Nested & Inner Classes

Föreläsning 4 IS1300 Inbyggda system

Authentication Context QC Statement. Stefan Santesson, 3xA Security AB

Recitation 4. 2-D arrays. Exceptions

12.6 Heat equation, Wave equation

Styrteknik : Funktioner och funktionsblock

Scalable Dynamic Analysis of Binary Code

Viktig information för transmittrar med option /A1 Gold-Plated Diaphragm

Styrteknik: Binära tal, talsystem och koder D3:1

Make a speech. How to make the perfect speech. söndag 6 oktober 13

Schenker Privpak AB Telefon VAT Nr. SE Schenker ABs ansvarsbestämmelser, identiska med Box 905 Faxnr Säte: Borås

Beijer Electronics AB 2000, MA00336A,

Urban Runoff in Denser Environments. Tom Richman, ASLA, AICP

Health café. Self help groups. Learning café. Focus on support to people with chronic diseases and their families

1. Varje bevissteg ska motiveras formellt (informella bevis ger 0 poang)

SVENSK STANDARD SS-EN ISO 19108:2005/AC:2015

Module 1: Functions, Limits, Continuity

Workplan Food. Spring term 2016 Year 7. Name:

Vässa kraven och förbättra samarbetet med hjälp av Behaviour Driven Development Anna Fallqvist Eriksson

Problem som kan uppkomma vid registrering av ansökan

Grafisk teknik IMCDP IMCDP IMCDP. IMCDP(filter) Sasan Gooran (HT 2006) Assumptions:

1. Compute the following matrix: (2 p) 2. Compute the determinant of the following matrix: (2 p)


Solutions to exam in SF1811 Optimization, June 3, 2014

Grafisk teknik IMCDP. Sasan Gooran (HT 2006) Assumptions:

Grundkurs Programmering

FÖRBERED UNDERLAG FÖR BEDÖMNING SÅ HÄR



Webbreg öppen: 26/ /

Webbregistrering pa kurs och termin

Questionnaire for visa applicants Appendix A

Writing with context. Att skriva med sammanhang

Quick Start Guide Snabbguide

Michael Q. Jones & Matt B. Pedersen University of Nevada Las Vegas

Ett hållbart boende A sustainable living. Mikael Hassel. Handledare/ Supervisor. Examiner. Katarina Lundeberg/Fredric Benesch

Grafisk teknik. Sasan Gooran (HT 2006)

EXTERNAL ASSESSMENT SAMPLE TASKS SWEDISH BREAKTHROUGH LSPSWEB/0Y09

ARC 32. Tvättställsblandare/Basin Mixer. inr.se

Boiler with heatpump / Värmepumpsberedare

Kvalitetsarbete I Landstinget i Kalmar län. 24 oktober 2007 Eva Arvidsson

Lösenordsportalen Hosted by UNIT4 For instructions in English, see further down in this document

Application Note SW

INSTALLATION INSTRUCTIONS

Pedagogisk planering. Ron Chlebek. Centralt Innehåll. Svenska/Engelska. Lego Mindstorms. Syfte: Matematik

Swedish adaptation of ISO TC 211 Quality principles. Erik Stenborg

Skyddande av frågebanken

This exam consists of four problems. The maximum sum of points is 20. The marks 3, 4 and 5 require a minimum

Oförstörande provning (NDT) i Del M Subpart F/Del 145-organisationer


SkillGuide. Bruksanvisning. Svenska

A metadata registry for Japanese construction field

Examensarbete Introduk)on - Slutsatser Anne Håkansson annehak@kth.se Studierektor Examensarbeten ICT-skolan, KTH

Theory 1. Summer Term 2010

Säkerhetsfunktioner rstå varandra? Finns behov av att avvika från normal säkerhetsfunktion s vissa betingelser under uppstart, ändringar i processen

Senaste trenderna från testforskningen: Passar de industrin? Robert Feldt,

Information technology Open Document Format for Office Applications (OpenDocument) v1.0 (ISO/IEC 26300:2006, IDT) SWEDISH STANDARDS INSTITUTE

Questionnaire on Nurses Feeling for Hospital Odors

Beslut om bolaget skall gå i likvidation eller driva verksamheten vidare.

RUP är en omfattande process, ett processramverk. RUP bör införas stegvis. RUP måste anpassas. till organisationen till projektet

2.1 Installation of driver using Internet Installation of driver from disk... 3

Adding active and blended learning to an introductory mechanics course

Förändrade förväntningar

Methods to increase work-related activities within the curricula. S Nyberg and Pr U Edlund KTH SoTL 2017

Quicksort. Koffman & Wolfgang kapitel 8, avsnitt 9

Luftfartsavdelningen Sektionen för flygutbildning MANUALER VÄLKOMNA EN KORT SAMMANFATTNING AV INNEHÅLLET I RESPEKTIVE MANUAL

electiaprotect GSM SEQURITY SYSTEM Vesta EZ Home Application SMART SECURITY SYSTEMS! SVENSKA ios Android

SOA One Year Later and With a Business Perspective. BEA Education VNUG 2006

Materialplanering och styrning på grundnivå. 7,5 högskolepoäng

Studieteknik för universitetet 2. Books in English and annat på svenska

Collaborative Product Development:

Uttagning för D21E och H21E

Förskola i Bromma- Examensarbete. Henrik Westling. Supervisor. Examiner

Module 6: Integrals and applications

Hur fattar samhället beslut när forskarna är oeniga?

Högskolan i Skövde (SK, JS) Svensk version Tentamen i matematik

Provlektion Just Stuff B Textbook Just Stuff B Workbook

F ξ (x) = f(y, x)dydx = 1. We say that a random variable ξ has a distribution F (x), if. F (x) =

ASSEMBLY INSTRUCTIONS SCALE SQUARE - STANDARD

The Finite Element Method, FHL064

Every visitor coming to the this website can subscribe for the newsletter by entering respective address and desired city.

Mönster. Ulf Cederling Växjö University Slide 1

CM FORUM. Introduktion till. Configuration Management (CM) / Konfigurationsledning. Tobias Ljungkvist

samhälle Susanna Öhman

LULEÅ TEKNISKA UNIVERSITET

Accomodations at Anfasteröd Gårdsvik, Ljungskile

Design Service Goal. Hantering av demonterbara delar som ingår i Fatigue Critical Baseline Structure List. Presentatör

Tentamen MMG610 Diskret Matematik, GU

Exempel på uppgifter från 2010, 2011 och 2012 års ämnesprov i matematik för årskurs 3. Engelsk version

Discovering!!!!! Swedish ÅÄÖ. EPISODE 6 Norrlänningar and numbers Misi.se

c a OP b Digitalteknik och Datorarkitektur 5hp ALU Design Principle 1 - Simplicity favors regularity add $15, $8, $11

Chapter 1 : Who do you think you are?

Transkript:

CS 345 Imperative Programming Vitaly Shmatikov slide 1

Reading Assignment Mitchell, Chapter 5.1-2 C Reference Manual, Chapter 8 slide 2

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

Elements of Imperative Programs Data type definitions Variable declarations (usually typed) Expressions and assignment statements Control flow statements (usually structured) Lexical scopes and blocks Goal: provide locality of reference Declarations and definitions of procedures and functions (i.e., parameterized blocks) slide 4

Variable Declarations Typed 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? Variable 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 5

Variables: Locations and Values When a variable is declared, it is bound to some memory location and becomes its identifier Location could be in global, heap, or stack storage l-value: memory location (address) r-value: value stored at the memory location identified by l-value Assignment: 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 6

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

Variables and Assignment On 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 An 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 8

l-values and r-values (1) Any 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 Literal constants Have r-values, but not l-values Variables Have both r-values and l-values Example: x=x*y means compute rval(x)*rval(y) and store it in lval(x) slide 9

l-values and r-values (2) Pointer variables Their r-values are l-values of another variable Intuition: the value of a pointer is an address Overriding 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 10

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

Turing-Complete Mini-Language Integer variables, values, operations Assignment If Go To slide 12

Structured Control Flow Control flow in imperative languages is most often designed to be sequential Instructions executed in order they are written Some also support concurrent execution (Java) Program 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 13

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

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

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

Iteration Definite Indefinite 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 17

Iteration Constructs in C while (condition) stmt; while (condition) { stmt; stmt; ; } do stmt while (condition); do { stmt; stmt; ; } while (condition); for (<initialize>; <test>; <step>) stmt; Restricted form of while loop same as <initialize>; while (<test>) { stmt; <step> } for (<initialize>; <test>; <step>) { stmt; stmt; ; } slide 18

Breaking Out Of A Loop in C slide 19

Forced Loop Re-Entry in C slide 20

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

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

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

Memory Management Registers, Code segment, Program counter Ignore registers (for our purposes) and details of instruction set Data 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 24

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

Inline Blocks Activation record Data structure stored on run-time stack Contains space for local variables { 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 May need space for variables and intermediate results like (x+y), (x-y) slide 26

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

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 x y x+y x-y Control link 0 1 Control link z -1 Environment pointer 1-1 slide 28