Interpretation (Chapter 8) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.

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

Instruction Set Design
1 Languages and Compilers (SProg og Oversættere) Code Generation.
Names and Bindings.
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.
Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Ch.8: Interpretation Spring 2008 Marco Valtorta.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Tentative Schedule 20/12 Interpreter+ Code Generation 27/12 Code Generation for Control Flow 3/1 Activation Records 10/1 Program Analysis 17/1 Register.
Chapter 4 Processor Technology and Architecture. Chapter goals Describe CPU instruction and execution cycles Explain how primitive CPU instructions are.
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.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
3-1 3 Compilers and interpreters  Compilers and other translators  Interpreters  Tombstone diagrams  Real vs virtual machines  Interpretive compilers.
CSc 453 Interpreters & Interpretation Saumya Debray The University of Arizona Tucson.
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.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CS 355 – Programming Languages
Compilation (Chapter 3) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Machine Instruction Characteristics
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
CSC 310 – Imperative Programming Languages, Spring, 2009 Virtual Machines and Threaded Intermediate Code (instead of PR Chapter 5 on Target Machine Architecture)
ITEC 352 Lecture 12 ISA(3). Review Buses Memory ALU Registers Process of compiling.
CS 147 June 13, 2001 Levels of Programming Languages Svetlana Velyutina.
4-1 4 Interpretation  Overview  Virtual machine interpretation  Case study: SVM  Case study: SVM interpreter in Java Programming Languages 3 © 2012.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Introduction (Chapter 1) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
CPS 506 Comparative Programming Languages Syntax Specification.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Language Translation A programming language processor is any system that manipulates programs expressed in a PL A source program in some source language.
Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Programming Languages
Instructions. Portability In addition to making hardware backward compatible, we have also made software portable. In describing software, “portable”
Review (Chapter 9) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
Language Implementation Overview John Keyser Spring 2016.
1 Languages and Compilers (SProg og Oversættere) Lecture 9 (1) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
Code Generation (Chapter 7) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
RealTimeSystems Lab Jong-Koo, Lim
Computer Architecture Instruction Set Architecture
Course Overview PART I: overview material PART II: inside a compiler
Architecture Review Instruction Set Architecture
William Stallings Computer Organization and Architecture 8th Edition
About Instructions Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter PHY 201 (Blum)
CSCE Fall 2013 Prof. Jennifer L. Welch.
CSc 453 Interpreters & Interpretation
Processor Organization and Architecture
Programming Languages 2nd edition Tucker and Noonan
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.
Languages and Compilers (SProg og Oversættere)
CSCE Fall 2012 Prof. Jennifer L. Welch.
Focus of the Course Object-Oriented Software Development
Course Overview PART I: overview material PART II: inside a compiler
Course Overview PART I: overview material PART II: inside a compiler
A Level Computer Science Topic 5: Computer Architecture and Assembly
Course Overview PART I: overview material PART II: inside a compiler
Computer Architecture and System Programming Laboratory
Languages and Compilers (SProg og Oversættere)
Presentation transcript:

Interpretation (Chapter 8) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II: inside a compiler 4Syntax analysis 5Contextual analysis 6Runtime organization 7Code generation PART III: conclusion 8Interpretation 9Review

Interpretation (Chapter 8) 2 Why Interpretation? Compiler: Large overhead before the code can be run Alternative: Direct interpretation of the code (immediate execution, no time-consuming compilation) Applications: –Interactive systems (SQL, shell, etc) –Simple programming languages (Basic, etc) –Scripting languages (Perl, Python, etc) –Programming languages with special requirements (Scheme, Prolog, Smalltalk, etc) –Write once, run once

Interpretation (Chapter 8) 3 Two Kinds of Interpreters Iterative interpretation: Well suited for quite simple languages, and fast (at most 10 times slower than compiled languages) Recursive interpretation: Well suited for more complex languages, but slower (up to 100 times slower than compiled languages)

Interpretation (Chapter 8) 4 Compilation and Interpretation Due to the slow speed of recursive interpretation, complex languages (such as Java) are often compiled to simpler languages (such as JVM) that can be interpreted iteratively Tetris JVMJava Tetris x86 Java-->JVM x86 JVM PPC JVM Tetris

Interpretation (Chapter 8) 5 Iterative Interpretation of Machine Code General pattern for iterative interpreters: while (true) { fetch( ); analyze( ); execute( ); } Simulate machine with: –Memory (use arrays for storing code and data) –I/O (directly) –CPU (use variables for registers)

Interpretation (Chapter 8) 6 Iterative Interpretation of Machine Code Fetch: get the next instruction from the code store array at the position pointed to by the instruction pointer; also increment instruction pointer Analyze: separate the instruction into an opcode and its operands Execute: use a switch statement with one case per each opcode; update memory and registers as specified by the particular instruction

Interpretation (Chapter 8) 7 Hypo: a Hypothetical Abstract Machine 4096-word code store and 4096-word data store PC: program counter (register), initially 0 ACC: general purpose accumulator (register), initially 0 4-bit opcode and 12-bit operand Instruction set: OpcodeInstructionMeaning 0STORE dword at address d := ACC 1LOAD dACC := word at address d 2LOADL dACC := d 3ADD dACC := ACC + word at address d 4SUB dACC := ACC – word at address d 5JUMP dPC := d 6JUMPZ dif ACC = 0 then PC := d 7HALTstop execution

Interpretation (Chapter 8) 8 Implementation of Hypo in Java public class HypoInstruction { public byte op; // opcode field public short d; // operand field public static final byte // possible opcodes STOREop=0, LOADop=1, LOADLop=2, ADDop =3, SUBop =4, JUMPop=5, JUMPZop=6, HALTop=7; }

Interpretation (Chapter 8) 9 Implementation of Hypo in Java public class HypoState { public static final short CODESIZE=4096; public static final short DATASIZE=4096; public HypoInstruction[ ] code=new HypoInstruction[CODESIZE]; public short[ ] data=new short[DATASIZE]; public short PC; public short ACC; public byte status; public static final byte RUNNING=0, HALTED=1, FAILED=2; }

Interpretation (Chapter 8) 10 Implementation of Hypo in Java public class HypoInterpreter extends HypoState { public void load( ) {...} // load program into memory public void emulate( ) { PC=0; ACC=0; status=RUNNING; do { // fetch: HypoInstruction instr=code[PC++]; // analyze: byte op=instr.op; byte d=instr.d; // execute: switch (op) {... // see details on next page } } while (status==RUNNING); }

Interpretation (Chapter 8) 11 Implementation of Hypo in Java // execute switch (op) { case STOREop: data[d]=ACC; break; case LOADop: ACC=data[d]; break; case LOADLop: ACC=d; break; case ADDop: ACC+=data[d]; break; case SUBop: ACC-=data[d]; break; case JUMPop: PC=d; break; case JUMPZop: if (ACC==0) PC=d; break; case HALTop: status=HALTED; break; default: status=FAILED; }

Interpretation (Chapter 8) 12 Iterative Interpretation of Mini-Basic Programming languages can be interpreted iteratively unless they have recursive syntactic structures such as Command ::= if Expression then Command else Command EBNF for Mini-Basic: Program::=Command* Command::=Variable = Expression |read Variable |write Variable |go Label |if Expression RelationalOp Expression go Label |stop

Interpretation (Chapter 8) 13 Iterative Interpretation of Mini-Basic EBNF for Mini-Basic (continued): Expression::=PrimaryExpression |Expression ArithmeticOp PrimaryExpression PrimaryExpression::=Numeral |Variable | ( Expression ) ArithmeticOp::= + | – | * | / RelationalOp::= = | \= | | = = Variable::= a | b | c | … | z Label::=Digit Digit* Numeral::= The symbol Numeral denotes floating-point literals 26 predefined variables: a, b, c, …, z

Interpretation (Chapter 8) 14 Mini-Basic Interpreter Mini-Basic example code: 0read a 1b=a/2 2go 4 3b=(a/b+b)/2 4d=b*b–a 5if d>=0 go 7 6d=0–d 7if d>=0.01 go 3 8write b 9stop

Interpretation (Chapter 8) 15 Mini-Basic Interpreter Mini-Basic abstract machine: –Data store: array of 26 floating-point values –Code store: array of commands –Possible representations for each command: Character string (yields slowest execution) Sequence of tokens (good compromise) AST (yields slowest response time)

Interpretation (Chapter 8) 16 Implementing a Mini-Basic Interpreter in Java class Token { byte kind; String spelling; } class ScannedCommand { Token[ ] tokens; } public abstract class Command { public void execute (MiniBasicState state); }

Interpretation (Chapter 8) 17 Implementing a Mini-Basic Interpreter in Java public class MiniBasicState { public static final short CODESIZE=4096; public static final short DATASIZE=26; public ScannedCommand[ ] code=new ScannedCommand[CODESIZE]; public float[ ] data=new float[DATASIZE]; public short PC; public byte status; public static final byte RUNNING=0, HALTED=1, FAILED=2; }

Interpretation (Chapter 8) 18 Implementing a Mini-Basic Interpreter in Java public class MiniBasicInterpreter extends MiniBasicState { public void load( ) {...} // load program into memory public static Command parse(ScannedCommand scannedCom) {...} // return a Command AST public void run( ) { PC=0; status=RUNNING; do { // fetch: ScannedCommand scannedCom=code[PC++]; // analyze: Command analyzedCom=parse(scannedCom); // execute: analyzedCom.execute((MiniBasicState) this); } while (status==RUNNING); }

Interpretation (Chapter 8) 19 Implementing a Mini-Basic Interpreter in Java public class AssignCommand extends Command { byte V; // left side Expression E; // right side public void Execute(MiniBasicState state) { state.data[V]=E.evaluate(state); } public class GoCommand extends Command { short L; // destination label public void Execute(MiniBasicState state) { state.PC=L; } // ReadCommand, WriteCommand, IfCommand, StopCommand, Expression, etc.

Interpretation (Chapter 8) 20 Recursive Interpretation Recursively defined languages cannot be interpreted iteratively (fetch-analyze-execute), because each command can contain any number of other commands Both analysis and execution must be recursive (similar to the parsing phase when compiling a high-level language) Hence, the entire analysis must precede the entire execution: –Step 1: Fetch and analyze (recursively) –Step 2: Execute (recursively) Execution is a traversal of the decorated AST, hence we can use a new visitor class Values (variables and constants) are handled internally

Interpretation (Chapter 8) 21 Recursive Interpretation of Mini-Triangle public abstract class Value { } public class IntValue extends Value { public short i; } public class BoolValue extends Value { public boolean b; } public class UndefinedValue extends Value { }

Interpretation (Chapter 8) 22 Recursive Interpretation of Mini-Triangle public class MiniTriangleState { public static final short DATASIZE=...; Program program; // code store is the decorated AST Value[ ] data=new Value[DATASIZE]; public byte status; public static final byte RUNNING=0, HALTED=1, FAILED=2; }

Interpretation (Chapter 8) 23 Recursive Interpretation of Mini-Triangle public class MiniTriangleProcessor extends MiniTriangleState implements Visitor { public void fetchAnalyze( ) { Parser parser=new Parser(...); Checker checker=new Checker(...); StorageAllocator allocator= new StorageAllocator( ); program=parser.parse( ); checker.check(program); allocator.allocateAddresses(program); } public void run( ) { program.C.visit(this,null); } }

Interpretation (Chapter 8) 24 Recursive Interpretation of Mini-Triangle public Object visitIfCommand( IfCommand com, Object arg) { BoolValue val=(BoolValue) com.E.visit(this,null); if (val.b) com.C1.visit(this,null); else com.C2.visit(this,null); return null; }

Interpretation (Chapter 8) 25 Recursive Interpretation of Mini-Triangle public Object visitConstDeclaration( ConstDeclaration decl, Object arg) { KnownAddress entity=(KnownAddress) decl.entity; Value val=(Value)decl.E.visit(this,null); data[entity.address]=val; return null; }

Interpretation (Chapter 8) 26 Case Study: TAM Interpreter Variable for each register Array for code store (Instruction data type) Array for data store (short type; used for both stack and heap) Iterative interpretation similar to Hypo Addressing: private static short relative ( short d, byte r) { switch(r) { case SBr: return d+SB; case LBr: return d+LB; case L1r: return d+data[LB]; case L2r: return d+data[data[LB]];... }

Interpretation (Chapter 8) 27 Case Study: TAM Interpreter Use of addressing: switch(op) { case LOADop: { // push onto stack short addr=relative(d,r); data[ST++]=data[addr]; break; } case STOREop: { // pop from stack short addr=relative(d,r); data[addr]=data[––ST]; break; }... }

Interpretation (Chapter 8) 28 Usage of the TAM Interpreter First write a Triangle program. Assume it is stored in a file “ example.tri ” within the same folder that contains the Triangle and TAM subfolders. Next compile your Triangle program. The command shown below produces an equivalent TAM program in the default file “ obj.tam ”: java Triangle/Compiler example.tri To run this TAM program: java TAM/Interpreter obj.tam To view the TAM program in human-readable form: java TAM/Disassembler obj.tam

Interpretation (Chapter 8) 29 For further information More details about all these interpreters (Hypo, Mini-Basic, Mini-Triangle, TAM) can be found in the textbook