4-1 4 Interpretation  Overview  Virtual machine interpretation  Case study: SVM  Case study: SVM interpreter in Java Programming Languages 3 © 2012.

Slides:



Advertisements
Similar presentations
Euripides Montagne University of Central Florida (Summer 2011)
Advertisements

8 VM code generation Aspects of code generation Address allocation
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 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,
1 Registers and MAL - Part I. Motivation So far there are some details that we have ignored instructions can have different formats most computers have.
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:
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved The Microarchitecture Level.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
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.
8051 ASSEMBLY LANGUAGE PROGRAMMING
TCSS 372A Computer Architecture. Getting Started Get acquainted (take pictures) Review Web Page (
CSc 453 Interpreters & Interpretation Saumya Debray The University of Arizona Tucson.
Computer Science 210 Computer Organization The Instruction Execution Cycle.
Machine Instruction Characteristics
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Runtime Environments Compiler Construction Chapter 7.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Assembly Language A Brief Introduction. Unit Learning Goals CPU architecture. Basic Assembler Commands High level Programming  Assembler  Machine Language.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
5-1 5 Compilation  Overview  Compilation phases –syntactic analysis –contextual analysis –code generation  Abstract syntax trees  Case study: Fun language.
Important Concepts  Parts of the CPU  Arithmetic/Logic Unit  Control Unit  Registers  Program Counter  Instruction Register  Fetch/Decode/Execute.
Lecture 4: MIPS Instruction Set
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Concurrency Properties. Correctness In sequential programs, rerunning a program with the same input will always give the same result, so it makes sense.
COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE Lecture 21 & 22 Processor Organization Register Organization Course Instructor: Engr. Aisha Danish.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
Operand Addressing And Instruction Representation Tutorial 3.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
©SoftMoore ConsultingSlide 1 The CPRL Virtual Machine.
Computer Architecture & Operations I
Lecture 7 Macro Review Stack Frames
Lecture 5: Procedure Calls
Overview of Instruction Set Architectures
Computer Architecture Instruction Set Architecture
The Stack.
Computer Architecture Instruction Set Architecture
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
A Closer Look at Instruction Set Architectures
Computer Organization and Assembly Language (COAL)
CS 11 C track: lecture 8 Last week: hash tables, C preprocessor
The Processor and Machine Language
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Programming Languages (CS 550) Mini Language Compiler
Code Generation.
CSc 453 Interpreters & Interpretation
ECE232: Hardware Organization and Design
Subroutines and the Stack
Understanding Program Address Space
Introduction to Micro Controllers & Embedded System Design
Classification of instructions
Computer Instructions
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
Course Overview PART I: overview material PART II: inside a compiler
CSc 453 Interpreters & Interpretation
Programming Languages (CS 360) Mini Language Compiler
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

4-1 4 Interpretation  Overview  Virtual machine interpretation  Case study: SVM  Case study: SVM interpreter in Java Programming Languages 3 © 2012 David A Watt, University of Glasgow

4-2 Overview  Recall: An S interpreter accepts code expressed in language S, and immediately executes that code.  Assume that the code to be interpreted is just a sequence of simple instructions (including conditional/unconditional jumps).  The interpreter works as follows: –First it initializes the state. –Then it repeatedly fetches, analyses, and executes the next instruction. –Executing an instruction updates the state as required.

4-3 Virtual machine interpretation  Virtual machine code typically consists of: –load/store instructions –arithmetic/logical instructions –conditional/unconditional jumps –call/return instructions –etc.  The virtual machine state typically consists of: –storage (code, data) –registers (status, program counter, stack pointer, etc.).

4-4 Case study: SVM (1)  SVM (Simple Virtual Machine) will be used as a case study in this course.  SVM is suitable for executing programs in simple imperative PLs.  For a full description, see SVM Specification (available from the PL3 Moodle page).

4-5 Case study: SVM (2)  Source code and corresponding SVM code: code to execute ‘ p = 10*p; ’ code to evaluate ‘ p < n ’ p = 1; while (p < n) p = 10*p; 0: 3: 6: 9: 12: 13: 16: 19: 22: 23: 26: 29: LOADC 1 STOREG 2 LOADG 2 LOADG 1 COMPLT JUMPF 29 LOADC 10 LOADG 2 MULT STOREG 2 JUMP 6 HALT assume that n and p are located at global addresses 1 and 2

4-6 Case study: SVM (3)  SVM storage: –the code store is a fixed array of bytes, providing space for instructions –the data store is a fixed array of words, providing a stack to contain global and local data.  SVM main registers: –pc (program counter) points to the next instruction to be executed –sp (stack pointer) points to the top of the stack –fp (frame pointer) points to the base of the topmost frame (see §14) –status indicates whether the programming is running, failed, or halted.

4-7 Case study: SVM (4)  Illustration of code store:  Each instruction occupies 1, 2, or 3 bytes. pc … … 30 LOADC 1STOREG 2LOADG 2LOADG 1 COMPLT JUMPF 29 unused HALT

4-8 Case study: SVM (5)  Illustration of data store (simplified): … sp global data unused sp global data unused stacked data …

4-9 Case study: SVM (6)  SVM instruction set (simplified): Op- code Mnem- onic Behaviour 6 ADD pop w 2 ; pop w 1 ; push (w 1 +w 2 ) 7 SUB pop w 2 ; pop w 1 ; push (w 1 –w 2 ) 8 MUL pop w 2 ; pop w 1 ; push (w 1 ×w 2 ) 9 DIV pop w 2 ; pop w 1 ; push (w 1 /w 2 ) 10 CMPEQ pop w 2 ; pop w 1 ; push (if w 1 =w 2 then 1 else 0) 11 CMPLT pop w 2 ; pop w 1 ; push (if w 1 <w 2 then 1 else 0) 14 INV pop w; push (if w=0 then 1 else 0)

4-10 Case study: SVM (7)  SVM instruction set (continued): Op- code MnemonicBehaviour 0 LOADG d w ← word at address d; push w 1 STOREG d pop w; word at address d ← w 4 LOADC v push v 16 HALT status ← halted 17 JUMP c pc ← c 18 JUMPF c pop w; if w = 0 then pc ← c 19 JUMPT c pop w; if w ≠ 0 then pc ← c

4-11 Case study: SVM (8)  The top of the stack is used for evaluating expressions.  E.g., evaluating (7+3)*(5-2) : 7 data sp 7 3 data 10 data 10 5 data data 10 3 data 30 data LOADC 7LOADC 3 ADD LOADC 5LOADC 2 SUBMUL

4-12 Writing an interpreter  Interpreters are commonly written in C or Java.  In such an interpreter: –the virtual machine state is represented by a group of variables –each instruction is executed by inspecting and/or updating the virtual machine state.

4-13 Case study: SVM interpreter in Java (1)  Representation of instructions: final byte LOADG = 0, STOREG = 1, LOADL = 2, STOREL = 3, LOADC = 4, ADD = 6, SUB = 7, MUL = 8, DIV = 9, CMPEQ = 10, CMPLT = 12, CMPGT = 13, INV = 14, INC = 14, HALT = 16, JUMP = 17, JUMPF = 18, JUMPT = 19, …;

4-14 Case study: SVM interpreter in Java (2)  Representation of the virtual machine state: byte[] code; // code store int[] data; // data store int pc, cl, sp, fp, // registers status; final byte RUNNING = 0, FAILED = 1, HALTED = 2;

4-15 Case study: SVM interpreter in Java (3)  The interpreter initializes the state, then repeatedly fetches and executes instructions: void interpret () { // Initialize the state: status = RUNNING; sp = 0; fp = 0; pc = 0; do { // Fetch the next instruction: byte opcode = code[pc++]; // Execute this instruction: … } while (status == RUNNING); }

4-16 Case study: SVM interpreter in Java (4)  To execute an instruction, first inspect its opcode: // Execute this instruction: switch (opcode) { case LOADG: … case STOREG: … … case ADD: … case CMPLT: … … case HALT: … case JUMP: … case JUMPT: … … }

4-17 Case study: SVM interpreter in Java (5)  Executing arithmetic/logical instructions: case ADD: { int w2 = data[--sp]; int w1 = data[--sp]; data[sp++] = w1 + w2; break; } case CMPLT: { int w2 = data[--sp]; int w1 = data[--sp]; data[sp++] = (w1 < w2 ? 1 : 0); break; }

4-18 Case study: SVM interpreter in Java (6)  Executing load/store instructions: case LOADG: { int d = code[pc++]<<8 | code[pc++]; data[sp++] = data[d]; break; } case STOREG: { int d = code[pc++]<<8 | code[pc++]; data[d] = data[--sp]; break; } fetch 2-byte operand

4-19 Case study: SVM interpreter in Java (7)  Executing jump/halt instructions: case HALT: { status = HALTED; break; } case JUMP: { int c = …; pc = c; break; } case JUMPT: { int c = …; int w = data[--sp]; if (w != 0) pc = c; break; } fetch 2-byte operand