Typed Assembly Languages COS 441, Fall 2004 Frances Spalding Based on slides from Dave Walker and Greg Morrisett.

Slides:



Advertisements
Similar presentations
Type Analysis and Typed Compilation Stephanie Weirich Cornell University.
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Foundational Certified Code in a Metalogical Framework Karl Crary and Susmit Sarkar Carnegie Mellon University.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Program Representations. Representing programs Goals.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Runtime Environments Source language issues Storage organization
CS 536 Spring Code generation I Lecture 20.
STAL David Walker (joint work with Karl Crary, Neal Glew and Greg Morrisett)
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Intro to Computer Architecture
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
Chapter 9: Subprogram Control
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.
7/13/20151 Topic 3: Run-Time Environment Memory Model Activation Record Call Convention Storage Allocation Runtime Stack and Heap Garbage Collection.
Chapter 8 :: Subroutines and Control Abstraction
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Activation Records (in Tiger) CS 471 October 24, 2007.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
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.
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
CSC 8505 Compiler Construction Runtime Environments.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Compiler Construction Code Generation Activation Records
Overview of Back-end for CComp Zhaopeng Li Software Security Lab. June 8, 2009.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Run-Time Environments Chapter 7
Names and Attributes Names are a key programming language feature
© Craig Zilles (adapted from slides by Howard Huang)
Run-time organization
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Unit IV Code Generation
MIPS Instructions.
The University of Adelaide, School of Computer Science
UNIT V Run Time Environments.
Languages and Compilers (SProg og Oversættere)
Program and memory layout
Lecture 6: Assembly Programs
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Typed Assembly Languages COS 441, Fall 2004 Frances Spalding Based on slides from Dave Walker and Greg Morrisett

12/10/2004 COS441 - Typed Assembly Languages 2 Type Safety Progress: if a state is well-typed then it is not stuck If  ` e:  then either e is a value or 9 e’ such that e !* e’ Preservation: each step in evaluation preserves typing If  ` e:  and e !* e’ then  ` e’: 

12/10/2004 COS441 - Typed Assembly Languages 3 High Level vs. Low Level If the source language has been proved to be type safe, what do we know about the safety of the code we actually run? Nothing! What if the compiler produced a typed assembly language?

12/10/2004 COS441 - Typed Assembly Languages 4 Proof Carrying Code The code producer provides a proof of safety along with the executable The code consumer checks that proof before executing the code A typing derivation of the low-level code can be a safety proof

12/10/2004 COS441 - Typed Assembly Languages 5 High-level code for factorial

12/10/2004 COS441 - Typed Assembly Languages 6 Assembly code for factorial

12/10/2004 COS441 - Typed Assembly Languages 7 TAL 0 Syntax Registers: r 1, r 2, … Labels: L, … Integers: n Operands: v ::= r | n | L Arithmetic Ops: aop ::= add | sub | mul | … Branch Ops: bop ::= beq | bgt | … Instructions:  ::= aop r d, r s, v | bop r, v | mov r, v Blocks: B ::= jmp v |  ; B

12/10/2004 COS441 - Typed Assembly Languages 8 TAL 0 Dynamic Semantics Machine State  = (H,R,B)  H maps labels to basic blocks  R maps registers to values (integers and labels)  B is a basic block (corresponding to the current program counter) Model evaluation as a transition function mapping machine states to machine states:   

12/10/2004 COS441 - Typed Assembly Languages 9 TAL 0 Dynamic Semantics

12/10/2004 COS441 - Typed Assembly Languages 10 TAL 0 Static Semantics  ::= int |  ! {}  ::= {r 1 :  1, r 2 :  2, …} Code labels have type {r 1 :  1, r 2 :  2, …} ! {} To jump to code with this type, r 1 must contain a value of type , etc Notice that functions never return – the code block always ends with a jump to another label

12/10/2004 COS441 - Typed Assembly Languages 11 Example Program with Types

12/10/2004 COS441 - Typed Assembly Languages 12 Type Checking Basics We need to keep track of:  The types of the registers at each point in the code  The types of the labels on the code Heap Types:  will map labels to label types Register Types:  will map registers to types

12/10/2004 COS441 - Typed Assembly Languages 13 Basic Typing Judgments  ;  ` n: int  ;  ` r:  (r)  ;  ` L :  (L)

12/10/2004 COS441 - Typed Assembly Languages 14 Subtyping on Register Files Our program will never crash if the register file contains more values than necessary to satisfy some typing precondition Width subtyping: {r 1 :  1,…,r i-1 :  i-1,r i :  i } <: {r 1 :  1,…,r i-1 :  i-1 }

12/10/2004 COS441 - Typed Assembly Languages 15 Subtyping on Code Types Like ordinary function types, code types are contravariant  ’ <:   ! {} <:  ’ ! {}

12/10/2004 COS441 - Typed Assembly Languages 16 Typing Instructions The typing judgment for instructions describes the registers on input to the function and the registers on output  `  :  1   2  is invariant. The types of heap objects cannot change as the program executes

12/10/2004 COS441 - Typed Assembly Languages 17 Typing Instructions  ` r s : int  ;  ` v : int  ` add r d, r s, v :  !  [r d := int]  ` r : int  ;  ` v :  ! {}  ` ble r, v :  !   ` v :   ` mov r d, v :  !  [r d :=  ]

12/10/2004 COS441 - Typed Assembly Languages 18 Basic Block Typing All basic blocks end with jump instructions  ;  ` v :  ! {}  ` jmp v :  ! {} Instruction Sequences  `  :  1 !  2  ` B :  2 ! {}  `  ;B :  1 ! {}

12/10/2004 COS441 - Typed Assembly Languages 19 Heap and Register File Typing Heap Typing Dom(H)=Dom(  ) 8 L 2 Dom(H).  ` H(L):  (L) ` H :  Register File Typing 8 r 2 Dom(  ).  ;  ` R(r) :  (r)  ` R : 

12/10/2004 COS441 - Typed Assembly Languages 20 Machine Typing Main Typing Judgment ` H :   ` R :   ` B :  ! {} ` (H,R,B)

12/10/2004 COS441 - Typed Assembly Languages 21 Type Safety for TAL 0 Progress: If `  1 then 9  2 such that  1   2 Preservation If `  1 and  1   2 then `  2

12/10/2004 COS441 - Typed Assembly Languages 22 Limitations of TAL 0 What about situations where we don’t care which specific type is in a register?  Eg. A function that swaps the contents of two registers We can use polymorphic types to express this.

12/10/2004 COS441 - Typed Assembly Languages 23 TAL 1 : Polymorphism  ::= … |  8 , . { r 1 : , r 2 : , r 31 : {r 1 : , r 2 :  } ! {}} ! {}  Describes a function that swaps the values in r 1 and r 2 for values of any two types To jump to polymorphic functions, first explicitly instantiate type variables: v[  ]

12/10/2004 COS441 - Typed Assembly Languages 24 Polymorphism Example

12/10/2004 COS441 - Typed Assembly Languages 25 Callee-Saved Registers We can use polymorphism to implement callee-saved registers  We’d like to be able to enforce that a function doesn’t change certain registers. In other words, when it returns, certain registers have the same values as when it started 8 . { r 2 : int, …, r 5 : , …., r 31 : {r 1 : int, …, r 5 : , …} ! {} } ! {}  Why does this enforce the invariant we want?

12/10/2004 COS441 - Typed Assembly Languages 26 TAL 1 Semantics Changes to the static semantics to support polymorphism  Add the judgment  `  ok  Modify the other judgments to take this into account (just like homework 8!) Changes to the dynamic semantics  Add rule to do instantiation (H, R, jmp v[  ])  (H, R, B[  /  ])

12/10/2004 COS441 - Typed Assembly Languages 27 Full TAL Syntax

12/10/2004 COS441 - Typed Assembly Languages 28 Limitations of TAL Almost every compiler uses a stack  As storage space for local variables and other temporaries when we run out of space in registers  To keep track of control flow Control Links Activation Links

12/10/2004 COS441 - Typed Assembly Languages 29 STAL: Add a Stack Machine States: (H, R, S, B)  Stack S ::= nil | v :: S  A designated register sp points to the top of the stack  ::= salloc n | sfree n | sld r d, n | sst r s, n  push v ´ salloc 1; sst v, 1  pop v ´ sld r, 1; sfree 1

12/10/2004 COS441 - Typed Assembly Languages 30 Factorial Example

12/10/2004 COS441 - Typed Assembly Languages 31 Extensions for STAL Stack types  ::= nil |  ::  |   nil represents the empty stack   : :  represents a stack v::S where v:  and S:    is a stack variable which we can use to describe an unknown stack tail Use polymorphism 8 . {sp: int:: , r 1 : int} ! {}

12/10/2004 COS441 - Typed Assembly Languages 32 Stack Instruction Typing Stack Allocation  ;  ` salloc n :  [sp :=  ]!  [sp := ?::…::?::  ] Stack Free  ;  ` sfree :  [sp :=  1 ::…::  n ::  ]!  [sp :=  ]

12/10/2004 COS441 - Typed Assembly Languages 33 Stack Instruction Typing Stack load  ( sp) =  1 :: … ::  n ::   ;  ` sld r, n :  !  [r :=  n ] Stack store  ;  ;  ` v :   (sp) =  1 :: … ::  n ::   ;  ` sst v, n :  !  [sp :=  1 :: … ::  ::  ]

12/10/2004 COS441 - Typed Assembly Languages 34 STAL Features Polymorphism and polymorphic recursion are crucial for encoding standard procedure call/return We didn’t have to bake in the notion of procedure call/return. Basic jumps were good enough Can combine features to encode up a variety of calling conventions  Arguments on stack or in registers?  Results on stack or in registers?  Return address? Caller pops? Callee pops?  Caller saves? Callee saves?

12/10/2004 COS441 - Typed Assembly Languages 35 STAL Syntax

12/10/2004 COS441 - Typed Assembly Languages 36 General Limitations of TAL/STAL More functionality can be added to TAL There are still some limitations  Must know the order of data allocated on the stack sp:     3  Must distinguish between heap and stack pointers Can’t have a function that takes in two generic int pointers and adds their contents

12/10/2004 COS441 - Typed Assembly Languages 37 For more information on TAL/STAL From System F to Typed Assembly Language. G. Morrisett, D. Walker, K. Crary, and N. Glew. From System F to Typed Assembly Language. Stack-Based Typed Assembly Language. G. Morrisett, K. Crary, N. Glew, D. Walker. Stack-Based Typed Assembly Language.

12/10/2004 COS441 - Typed Assembly Languages 38 What Makes Stack Typing So Tricky? In the heap, we use the abstraction that a location never changes type  The garbage collector handles the reuse When modeling instructions like push/pop, we have to break this abstraction and use strong updates to change types Aliasing can make this unsound if we’re not careful

12/10/2004 COS441 - Typed Assembly Languages 39 My Research A different approach to typed assembly language Use logic to describe what memory looks like before and after each instruction (r 1 ) int) ­ (r 2 ) S(ℓ)) ­ (ℓ ) bool) load [r 2 ], r 1 (r 1 ) int) ­ (r 2 ) S(ℓ)) ­ (ℓ ) int)

12/10/2004 COS441 - Typed Assembly Languages 40 Modeling Stack “Evolution” Keep track of the different “versions” of each stack location and how they are related We can do this using a pair of a tree and a pointer to a node in the tree

12/10/2004 COS441 - Typed Assembly Languages 41 TOP k3k4 k6 S t a c k D e p t h Time k2k5 k1 ℓ2ℓ2 ℓ0ℓ0 ℓ1ℓ1 ℓ3ℓ3 …

12/10/2004 COS441 - Typed Assembly Languages 42 Tagged Locations All locations are “tagged” with their version number  (r 1 ) S(k 3. ℓ 2 )) is different from (r 1 ) S(k 6. ℓ 2 )) The tags act as “guards” on the location  Before accessing a tagged location, you must prove that it is “live”  In other words, the tag must be on the path between the root and Top

12/10/2004 COS441 - Typed Assembly Languages 43 For more information… Certifying Compilation for a Language with Stack Allocation [Draft]Certifying Compilation for a Language with Stack Allocation [Draft]. L. Jia, F. Spalding, D. Walker, N. Glew.