Programs as Data 6 Imperative languages, environment and store, micro-c

Similar documents
CS:3820 Programming Language Concepts

Programs as Data 8 A stack machine for micro-c; compiling micro-c to stack machine code

Programs as data first-order functional language type checking

Programs as data Parsing cont d; first-order functional language, type checking

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

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

Compiler Techniques MN1 The nano-c Language

Undergraduate Compilers in a Day

Begin at the beginning

The Substitution Model

The SPL Programming Language Reference Manual

Lexical and Syntax Analysis

FUNCTIONAL AND LOGIC PROGRAMS

A Tour of Language Implementation

Recap: Functions as first-class values

CS 314 Principles of Programming Languages. Lecture 9

LECTURE 3. Compiler Phases

The Substitution Model. Nate Foster Spring 2018

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

ASTs, Objective CAML, and Ocamlyacc

CS 345. Functions. Vitaly Shmatikov. slide 1

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Syntax. A. Bellaachia Page: 1

The PCAT Programming Language Reference Manual

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Programming Languages Third Edition. Chapter 7 Basic Semantics

CSCI-GA Scripting Languages

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Compilers. Compiler Construction Tutorial The Front-end

CS558 Programming Languages

Writing Evaluators MIF08. Laure Gonnord

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Programmiersprachen (Programming Languages)

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

Lecture 15 CIS 341: COMPILERS

COP4020 Programming Assignment 1 - Spring 2011

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

Names, Bindings, Scopes

CS558 Programming Languages

References and pointers

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

CS558 Programming Languages

Review of the C Programming Language

Topic IV. Block-structured procedural languages Algol and Pascal. References:

Modules, Structs, Hashes, and Operational Semantics

Intermediate Code Generation

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Programming language seminar The F# language. Peter Sestoft Wednesday Course plan

NOTE: Answer ANY FOUR of the following 6 sections:

Types and Type Inference

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

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

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Compiler construction 2002 week 5

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

G Programming Languages - Fall 2012

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

CS 360 Programming Languages Interpreters

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Lecture 14 Sections Mon, Mar 2, 2009

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

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

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Functional Programming. Big Picture. Design of Programming Languages

Introduction to Programming Using Java (98-388)

CS153: Compilers Lecture 16: Local Optimization II

Programming Language Basics

JVM ByteCode Interpreter

CSE 307: Principles of Programming Languages

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Weeks 6&7: Procedures and Parameter Passing

G Programming Languages - Fall 2012

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

CS 415 Midterm Exam Spring 2002

Introduction to Scientific Computing and Problem Solving

Introduction to Computer Science Midterm 3 Fall, Points

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Programming Languages

1 Lexical Considerations

PLT 4115 LRM: JaTesté

CSCI 2041: Advanced Language Processing

Denotational Semantics. Domain Theory

CS 242. Fundamentals. Reading: See last slide

Some Advanced ML Features

Expressions and Assignment

Variables and Bindings

Compiler construction in4020 lecture 5

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Project 2: Scheme Interpreter

EI326 ENGINEERING PRACTICE & TECHNICAL INNOVATION (III-G) Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Functional Parsers

Comp 311: Sample Midterm Examination

Transcription:

Programs as Data 6 Imperative languages, environment and store, micro-c Peter Sestoft Monday 2012-10-01* www.itu.dk 1

Course overview Today A naïve imperative language C concepts Pointers and pointer arithmetics, arrays Lvalue and rvalue Parameter passing by value and by reference Expression statements Micro-C, a subset of C abstract syntax lexing and parsing interpretation www.itu.dk 2

The overall course plan F# and functional programming Interpreting an expression language Lexing and parsing tools Interpreting a functional language, micro-ml Higher-order functions Type checking and type inference Interpreting an imperative language, micro-c Compiling micro-c to stack machine code Real-world abstract machines: JVM and.net Garbage collection techniques Continuations, exceptions and backtracking (Programs that generate programs, Scheme) Or maybe Scala, a functional/oo language on JVM www.itu.dk 3

A naive-store imperative language Naive store model: a variable name maps to an integer value so store is just a runtime environment sum = 0; for i = 0 to 100 do sum = sum + i; i sum 100 5050 i = 1; sum = 0; while sum < 10000 do begin sum = sum + i; i = 1 + i; end; i sum 142 10011 www.itu.dk 4

Naïve-store statement execution, 1 Executing a statement gives a new store Assignment x=e updates the store Expressions do not affect the store let rec exec stmt (store : naivestore) : naivestore = match stmt with Asgn(x, e) -> setsto store (x, eval e store) If(e1, stmt1, stmt2) -> if eval e1 store <> 0 then exec stmt1 store else exec stmt2 store... Update store at x with value of e www.itu.dk 5

Naïve-store statement execution, 2 A block {s 1 ; ; s n } executes s 1 then s 2 Example: exec (Block [s 1 ; s 2 ]) store = loop [s 1 ; s 2 ] store = exec s2 (exec s1 store) let rec exec stmt (store : naivestore) : naivestore = match stmt with Block stmts -> let rec loop ss sto = match ss with [] -> sto s1::sr -> loop sr (exec s1 sto) loop stmts store...

Naïve-store statement execution, 3 for and while update the store sequentially let rec exec stmt (store : naivestore) : naivestore = match stmt with... For(x, estart, estop, stmt) ->... While(e, stmt) -> let rec loop sto = if eval e sto = 0 then sto else loop (exec stmt sto) loop store www.itu.dk 7

Environment and store, micro-c The naive model cannot describe pointers and variable aliasing A more realistic store model: Environment maps a variable name to an address Store maps address to value i: 42 sum: 44 Environment Store 41 42 43 44 45 100 5050 www.itu.dk 8

The essence of C: Pointers Main innovations of C (1972) over Algol 60: Structs, as in COBOL and Pascal Pointers, pointer arithmetics, pointer types, array indexing as pointer indexing Syntax: { } for blocks, as in C++, Java, C# p: 42 sum: 44 *(p+1) 41 42 43 44 45 44 5050 Very different from Java and C#, which have no pointer arithmetics, but garbage collection

Desirable language features C C++ F#/ML Smtalk Haskell Java C# Garbage collection Exceptions Bounds checks Static types Generic types (para. polym.) Pattern matching Reflection Refl. on type parameters Anonymous functions (λ) Streams Lazy eval.

C pointer basics A pointer p refers to a storage location The dereference expression *p means: the content of the location (rvalue) as in *p + 4 the storage location itself (lvalue), as in *p = x+4 The pointer that points to x is &x Pointer arithmetics: *(p+1) is the location just after *p If p equals &a[0] then *(p+i) equals p[i] equals a[i], so an array is a pointer Strange fact: a[2] can be written 2[a] too www.itu.dk 11

Using pointers for return values Example ex5.c, computing square(x): void main(int n) {... int r; square(n, &r); print r; } for input void square(int i, int *rp) { *rp = i * i; } for return value: a pointer to where to put the result www.itu.dk 12

Recursion and return values Computing factorial with MicroC/ex9.c void main(int i) { int r; fac(i, &r); print r; } void fac(int n, int *res) { if (n == 0) *res = 1; else { int tmp; fac(n-1, &tmp); *res = tmp * n; } } n is input parameter res is output parameter: a pointer to where to put the result tmp holds the result of the recursive call &tmp gets a pointer to tmp www.itu.dk 13

Storage model for micro-c The store is an indexable stack Bottom: global variables at fixed addresses Plus, a stack of activation records globals main fac(3) fac(2) fac(1) fac(0) An activation record is an executing function return address and other administrative data parameters and local variables temporary results admin. data params+locals temps

Lvalue and rvalue of an expression Rvalue is normal value, right-hand side of assignment: 17, true Lvalue is location, left-hand side of assignment: x, a[2] In assignment e1=e2, expression e1 must have lvalue Has lvalue Has rvalue x yes yes a[2] yes yes *p yes yes x+2 no yes &x no yes Where else must an expression have lvalue in C#? In C? www.itu.dk 16

Call-by-value and call-by-reference, C# by value by reference int a = 11; int b = 22; swapv(a, b); swapr(ref a, ref b); static void swapv(int x, int y) { int tmp = x; x = y; y = tmp; } static void swapr(ref int x, ref int y) { int tmp = x; x = y; y = tmp; } store 41 42 43 44 11 addresses 22 22 11 45 a: 41 b: 42 x: 43 y: 44 tmp: 45 x: 41 y: 42 tmp: 43 11

C variable declarations Declaration int n int *p int ia[3] int *ipa[4] int (*iap)[3] int *(*ipap)[4] Meaning n is an integer p is a pointer to integer ia is array of 3 integers ipa is array of 4 pointers to integers iap is pointer to array of 3 integers ipap is pointer to array of 4 pointers to ints Unix program cdecl or www.cdecl.org may help: cdecl> explain int *(*ipap)[4] declare ipap as pointer to array 4 of pointer to int cdecl> declare n as array 7 of pointer to pointer to int int **n[7] www.itu.dk 18

Micro-C array layout An array int arr[4] consists of its 4 int elements a pointer to arr[0] arr: 46 41 42 43 44 45 46 47 67 71 73 79 42 This is the uniform array representation of B Real C treats array parameters and local arrays differently; complicates compiler Strachey s CPL -> Richards s BCPL -> B -> C www.itu.dk 19

Types Micro-C syntactic concepts int TypI int *x TypP(TypI) int x[4] TypA(TypI, Some 4) Expressions (*p + 1) * 12 Statements if (x!=0) y = 1/x; Declarations of global or local variables int x; of global functions void swap(int *x, int *y) {... } www.itu.dk 20

Micro-C abstract syntax type typ = TypI (* Type int *) TypC (* Type char *) TypA of typ * int option (* Array type *) TypP of typ (* Pointer type *) and expr = Access of access (* x or *p or a[e] *) Assign of access * expr (* x=e or *p=e or a[e]=e *) Addr of access (* &x or &*p or &a[e] *) CstI of int (* Constant *) Prim1 of string * expr (* Unary primitive operator *) Prim2 of string * expr * expr (* Binary primitive operator *) Andalso of expr * expr (* Sequential and *) Orelse of expr * expr (* Sequential or *) Call of string * expr list (* Function call f(...) *) and access = AccVar of string (* Variable access x *) AccDeref of expr (* Pointer dereferencing *p *) AccIndex of access * expr (* Array indexing a[e] *) and stmt = If of expr * stmt * stmt (* Conditional *) While of expr * stmt (* While loop *) Expr of expr (* Expression statement e; *) Return of expr option (* Return from method *) Block of stmtordec list (* Block: grouping and scope *) and stmtordec = Dec of typ * string (* Local variable declaration *) Stmt of stmt (* A statement *) and topdec = Fundec of typ option * string * (typ * string) list * stmt Vardec of typ * string and program = Prog of topdec list

Lexer specification for micro-c New: endline comments // blah blah and delimited comments if (x /* y? */) rule Token = parse... "//" { EndLineComment lexbuf; Token lexbuf } "/*" { Comment lexbuf; Token lexbuf } and EndLineComment = parse ['\n' '\r'] { () } (eof '\026') { () } _ { EndLineComment lexbuf } and Comment = parse "/*" { Comment lexbuf; Comment lexbuf } "*/" { () } ['\n' '\r'] { Comment lexbuf } (eof '\026') { lexererror lexbuf "Unterminated" } _ { Comment lexbuf }

Parsing C variable declarations Hard, declarations are mixfix: int *x[4] Parser trick: Parse a variable declaration as a type followed by a variable description: int *x[4] TypI (fun t -> TypA(TypP(t),Some 4), x ) Parse var description to get pair (f, x) of type function f, and variable name x Apply f to the declared type to get type of x Vardec(TypA(TypP(TypI),Some 4), x ) www.itu.dk 23

Interpreter data: Interpreting micro-c locenv, environment mapping local variable names to store addresses gloenv, environment mapping global variable names to store addresses, and global function names to (parameter list, body statement) store, mapping addresses to (integer) values Main interpreter functions: exec: stmt -> locenv -> gloenv -> store -> store eval: expr -> locenv -> gloenv -> store -> int * store access: access -> locenv -> gloenv -> store -> address * store www.itu.dk 24

Micro-C statement execution As for the naïve language, but two envs: let rec exec stmt locenv gloenv store : store = match stmt with If(e, stmt1, stmt2) -> let (v, store1) = eval e locenv gloenv store if v<>0 then exec stmt1 locenv gloenv store1 else exec stmt2 locenv gloenv store1 While(e, body) -> let rec loop store1 = let (v, store2) = eval e locenv gloenv store1 if v<>0 then loop (exec body locenv gloenv store2) else store2 loop store...

Expression statements in C, C++, Java and C# The assignment statement x = 2+y; is really an expression x = 2+y followed by a semicolon The semicolon means: ignore value Value: none Effect: change x Value: 2+y Effect: change x let rec exec stmt locenv gloenv store : store = match stmt with... Expr e -> let (_, store1) = eval e locenv gloenv store store1 Evaluate expression then ignore its value

Micro-C expression evaluation, 1 Evaluation of an expression takes local and global env and a store gives a resulting rvalue and a new store and eval e locenv gloenv store : int * store = match e with... CstI i -> (i, store) Prim2(ope, e1, e2) -> let (i1, store1) = eval e1 locenv gloenv store let (i2, store2) = eval e2 locenv gloenv store1 let res = match ope with "*" -> i1 * i2 "+" -> i1 + i2... (res, store2)

Micro-C expression evaluation, 2 To evaluate access expression x, *p, arr[i] find its lvalue, as an address loc look up the rvalue in the store, as store1[loc] To evaluate &e just evaluate e as lvalue return the lvalue rvalue and eval e locenv gloenv store : int * store = match e with Access acc -> let (loc, store1) = access acc locenv gloenv store (getsto store1 loc, store1) Addr acc -> access acc locenv gloenv store...

Micro-C access evaluation, to lvalue A variable x is looked up in environment A dereferencing *e just evaluates e to an address An array indexing arr[idx] evaluates arr to address a, then gets aval=store[a] evaluates e to rvalue index i returns address (aval+i) lvalue and access acc locenv gloenv store : int * store = match acc with AccVar x -> (lookup (fst locenv) x, store) AccDeref e -> eval e locenv gloenv store AccIndex(acc, idx) -> let (a, store1) = access acc locenv gloenv store let aval = getsto store1 a let (i, store2) = eval idx locenv gloenv store1 (aval + i, store2)

Operators &x and *p are inverses The address-of operator &e evaluates e to its lvalue returns the lvalue (address) as if it were an rvalue The dereferencing operator *e evaluates e to its rvalue returns the rvalue as if it were an lvalue It follows that &(*e) equals e that *(&e) equals e, provided e has lvalue www.itu.dk 30

Micro-C, interpreter and compiler This lecture: Interpretation of micro-c ex1.c Input Micro-C program run in Interp.fs Output Next lecture: Compilation of micro-c ex1.c P.class ex1.out Input Micro-C program Compiler Machine code Machine.java Output

Reading and homework This week s lecture: PLCSD chapter 7 Strachey: Fundamental Concepts Kernighan & Ritchie: The C programming language, chapter 5.1-5.5 Next lecture PLCSD chapter 8 www.itu.dk 32