Compiler Construction Lecture 17 Mapping Variables to Memory.

Slides:



Advertisements
Similar presentations
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Virtual Machines Matthew Dwyer 324E Nichols Hall
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
CS 536 Spring Code generation I Lecture 20.
Run-Time Storage Organization
Run time vs. Compile time
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Consider With x = 10 we may proceed as (10-1) = 9 (10-7) = 3 (9*3) = 27 (10-11) = -1 27/(-1) = -27 Writing intermediates on paper.
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
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.
5/6/99 Ashish Sabharwal1 JVM Architecture n Local storage area –Randomly accessible –Just like standard RAM –Stores variables (eg. an array) –Have to specify.
1 Software Testing and Quality Assurance Lecture 31 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
CSc 453 Interpreters & Interpretation Saumya Debray The University of Arizona Tucson.
Recursion and Implementation of Functions
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
RISC vs. CISC By Chiam D Cook Cs 147 spring 08. CISC Complex Instruction Set Computer –Large number of complex instructions –Low level –Facilitate the.
Intro to Java The Java Virtual Machine. What is the JVM  a software emulation of a hypothetical computing machine that runs Java bytecodes (Java compiler.
COP4020 Programming Languages
A data structure model: basic representation of data, such as integers, logic values, and characters homogeneous data structures, such as arrays and stringsheterogeneous.
Implement High-level Program Language on JVM CSCE 531 ZHONGHAO LIU ZHONGHAO LIU XIAO LIN.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
SPL/2010 StackVsHeap. SPL/2010 Objectives ● Memory management ● central shared resource in multiprocessing RTE ● memory models that are used in Java and.
ITEC 352 Lecture 20 JVM Intro. Functions + Assembly Review Questions? Project due today Activation record –How is it used?
Lecture 10 : Introduction to Java Virtual Machine
Runtime Environments Compiler Construction Chapter 7.
Roopa.T PESIT, Bangalore. Source and Credits Dalvik VM, Dan Bornstein Google IO 2008 The Dalvik virtual machine Architecture by David Ehringer.
Compiler Construction
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Crosscutting Issues: The Rôle of Compilers Architects must be aware of current compiler technology Compiler Architecture.
Programming Languages
Cs 147 Spring 2010 Meg Genoar. History Started to emerge in mid-1970s 1988 – RISC took over workstation market.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
Programming Languages and Paradigms Activation Records in Java.
More Examples of Abstract Interpretation Register Allocation.
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
RealTimeSystems Lab Jong-Koo, Lim
©SoftMoore ConsultingSlide 1 The CPRL Virtual Machine.
Storage Allocation Mechanisms
Run-time organization
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Abstract Interpretation Continued
Lecture 4: MIPS Instruction Set
CSc 453 Interpreters & Interpretation
Memory Allocation CS 217.
The University of Adelaide, School of Computer Science
Recursion and Implementation of Functions
Program and memory layout
Program and memory layout
Program and memory layout
Program and memory layout
Lecture 4: Instruction Set Design/Pipelining
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
CSc 453 Interpreters & Interpretation
Run-time environments
Presentation transcript:

Compiler Construction Lecture 17 Mapping Variables to Memory

Original and Target Program have Different Views of Program State Original program: – local variables given by names (any number of them) – each procedure execution has fresh space for its variables (even if it is recursive) – fields given by names Java Virtual Machine – local variables given by slots (0,1,2,…), any number – intermediate values stored in operand stack – each procedure call gets fresh slots and stack – fields given by names and object references Machine code: program state is a large arrays of bytes and a finite number of registers

Compilation Performs Automated Data Refinement x = y + 7 * z iload 2 iconst 7 iload 3 imul iadd istore 1 x  0 y  5 z  8 1  0 2  5 3  8 x  61 y  5 z  8 1  61 2  5 3  8 Data Refinement Function R R maps s to c and s’ to c’ [[x = y + 7 * z]] = s s’ c c' If interpreting program P leads from s to s’ then running the compiled code [[P]] leads from R(s) to R(s’) P:

Inductive Argument for Correctness R x = y + 7 * z x  0 y  5 z  8 x  61 y  5 z  8 s1s1 s2s2 y = z + 1 R x  61 y  9 z  8 s3s3 R [[x = y + 7 * z]] 0  0 1  5 2  8 0  61 1  5 2  8 c1c1 c2c2 iload 3; iconst 1; iadd; istore 2 0  61 1  9 2  8 c3c3 (R may need to be a relation, not just function)

A Simple Theorem P : S  S is a program meaning function P c : C  C is meaning function for the compiled program R : S  Cis data representation function Let s n+1 = P(s n ), n = 0,1,… be interpreted execution Let c n+1 = P(c n ), n = 0,1,… be compiled execution Theorem: If – c 0 = R(s 0 ) – for all s, P c (R(s)) = R(P(s)) then c n = R(c n ) for all n. Proof: immediate, by induction. R is often called simulation relation.

Example of a Simple R Let the received, the parameters, and local variables, in their order of declaration, be x 1, x 2 … x n Then R maps program state with only integers like this: x 1  v 1 x 2  v 2 x 3  v 3 … x n  v n 0  v 1 1  v 2 2  v 3 … (n-1)  v n R

R for Booleans Let the received, the parameters, and local variables, in their order of declaration, be x 1, x 2 … x n Then R maps program state like this, where x 1 and x 2 are integers but x 3 and x 4 are Booleans: x 1  3 x 2  9 x 3  true x 4  false 0  3 1  9 2  1 3  0 R

R that depends on Program Point def main(x:Int) { var res, y, z: Int if (x>0) { y = x + 1 res = y } else { z = -x - 10 res = z } … } x  v 1 res  v 2 y  v 3 z  v 4 R 0  v 1 1  v 2 2  v 3 3  v 4 x  v 1 res  v 2 y  v 3 z  v 4 R1R1 0  v 1 1  v 2 2  v 3 x  v 1 res  v 2 y  v 3 z  v 4 R2R2 0  v 1 1  v 2 2  v 4 Map y,z to same slot. Consume fewer slots!

Packing Variables into Memory If values are not used at the same time, we can store them in the same place This technique arises in – Register allocation: store frequently used values in a bounded number of fast registers – ‘malloc’ and ‘free’ manual memory management: free releases memory to be used for later objects – Garbage collection, e.g. for JVM, and.NET as well as languages that run on top of them (e.g. Scala)

Register Machines Better for most purposes than stack machines – closer to modern CPUs (RISC architecture) – closer to control-flow graphs – simpler than stack machine Example: ARM architectureARM architecture From article on RISC architectures:article on RISC architectures “The ARM architecture dominates the market for high performance, low power, low cost embedded systems (typically 100–500 MHz in 2008). ARM Ltd., which licenses intellectual property rather than manufacturing chips, reported 10 billion licensed chips shipped in early 2008 [7]. ARM is deployed in countless mobile devices such as: Samsung Galaxy (ARM11), Apple iPods (custom ARM7TDMI SoC) Apple iPhone (Samsung ARM1176JZF), Palm and PocketPC PDAs and smartphones (Marvell XScale family, Samsung SC ARM9), Nintendo Game Boy Advance (ARM7TDMI), Nintendo DS (ARM7TDMI, ARM946E-S), Sony Network Walkman (Sony in-house ARM based chip)Samsung Galaxy Directly Addressable RAM (large - GB, slow) R0,R1,…,R31 A few fast registers

Basic Instructions of Register Machines R i  Mem[R j ]load Mem[R j ]  R i store R i  R j * R k compute: for an operation * Efficient register machine code uses as few loads and stores as possible.

State Mapped to Register Machine Both dynamically allocated heap and stack expand – heap need not be contiguous can request more memory from the OS if needed – stack grows downwards Heap is more general: Can allocate, read/write, and deallocate, in any order Garbage Collector does deallocation automatically – Must be able to find free space among used one, group free blocks into larger ones (compaction),… Stack is more efficient: allocation is simple: increment, decrement top of stack pointer (SP) is often a register if stack grows towards smaller addresses: – to allocate N bytes on stack (push): SP := SP - N – to deallocate N bytes on stack (pop): SP := SP + N Stack Heap Constants Static Globals free memory SP 0 50kb 10MB 1 GB Exact picture may depend on hardware and OS

JVM vs General Register Machine Code R1  Mem[SP] SP = SP + 4 R2  Mem[SP] R2  R1 * R2 Mem[SP]  R2 imul JVM: Register Machine: