Syntax Directed Translation 66.648 Compiler Design Lecture (03/16//98) Computer Science Rensselaer Polytechnic.

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

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.
Intermediate Code Generation
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
1 Compiler Construction Intermediate Code Generation.
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CS 153: Concepts of Compiler Design November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Lab 9 Java Bytecode & The Jasmin Assembler
Syntax Directed Translation
CS 536 Spring Code generation I Lecture 20.
Procedure calls (1) The fact: Most programming languages support the concept of procedures (methods). Each method has its own local variables that are.
Compiler Summary Mooly Sagiv html://
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.
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
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.
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
CSc 453 Interpreters & Interpretation Saumya Debray The University of Arizona Tucson.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
The Java Virtual Machine 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
1 October 1, October 1, 2015October 1, 2015October 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
Lecture 10 : Introduction to Java Virtual Machine
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
1 Introduction to JVM Based on material produced by Bill Venners.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
5-1 Chapter 5 - Languages and the Machine Department of Information Technology, Radford University ITEC 352 Computer Organization Principles of Computer.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
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.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
Introduction to Code Generation and Intermediate Representations
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
Programming Languages
More on MIPS programs n SPIM does not support everything supported by a general MIPS assembler. For example, –.end doesn’t work Use j $ra –.macro doesn’t.
Winter Compilers Software Eng. Dept. – Ort Braude Compiling Assignments and Expressions Lecturer: Esti Stein brd4.ort.org.il/~esti2.
Chap. 10, Intermediate Representations J. H. Wang Dec. 14, 2015.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 18: Code Safety and Virtual Machines
CS 153: Concepts of Compiler Design November 18 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.
Code generation exercises. Function body Transform the following code into java bytecode: def middle(small: Int, big: Int): Int = { val mid = small +
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Recap: Printing Trees into Bytecodes To evaluate e 1 *e 2 interpreter –evaluates e 1 –evaluates e 2 –combines the result using * Compiler for e 1 *e 2.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
RealTimeSystems Lab Jong-Koo, Lim
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
The Java Virtual Machine (JVM)
A Simple Syntax-Directed Translator
CS216: Program and Data Representation
Computer Architecture and Organization Miles Murdocca and Vincent Heuring Chapter 4 – The Instruction Set Architecture.
CS 153: Concepts of Compiler Design November 2 Class Meeting
Java Virtual Machine (JVM)
Java Byte Codes (0xCAFEBABE) cs205: engineering software
Lecture 19: 0xCAFEBABE (Java Byte Codes) CS201j: Engineering Software
Byte Code Verification
Course Overview PART I: overview material PART II: inside a compiler
Compiler Design 21. Intermediate Code Generation
CS 153: Concepts of Compiler Design November 6 Class Meeting
Compiler Design 21. Intermediate Code Generation
CMPE 152: Compiler Design April 16 Class Meeting
CSc 453 Interpreters & Interpretation
Presentation transcript:

Syntax Directed Translation Compiler Design Lecture (03/16//98) Computer Science Rensselaer Polytechnic

Lecture Outline Syntax Directed Translation Syntax Directed Translation Java Virtual Machine Java Virtual Machine Examples Examples Administration Administration

Phases of a Compiler 1. Lexical Analyzer (Scanner) Takes source Program and Converts into tokens Takes source Program and Converts into tokens 2. Syntax Analyzer (Parser) Takes tokens and constructs a parse tree. 3. Semantic Analyzer Takes a parse tree and constructs an abstract syntax tree with attributes.

Phases of a Compiler- Contd 4. Syntax Directed Translation Takes an abstract syntax tree and produces an Interpreter code (Translation output) 5. Intermediate-code Generator Takes an abstract syntax tree and produces un- optimized Intermediate code.

Syntax Directed Translation Scheme A syntax directed translation scheme is a syntax directed definition in which the net effect of semantic actions is to print out a translation of the input to a desired output form. This is accomplished by including “emit” statements in semantic actions that write out text fragments of the output, as well as string- valued attributes that compute text fragments to be fed into emit statements.

1. Postfix and Prefix notations: We have already seen how to generate them. Let us generate Java Byte code. E -> E’+’ E { emit(“iadd”);} E-> E ‘* ‘ E { emit(“imul”);} E-> T T -> ICONST { emit(“sipush ICONST.string);} T-> ‘(‘ E ‘)’ Examples

We now present (Read Java Virtual Machine Spec) a simple stack machine and illustrate how to generate code for it via syntax-directed translations. The abstract machine code for an expression simulates a stack evaluation of the postfix representation for the expression. Expression evaluation proceeds by processing the postfix representation from left to right. Abstract Stack Machine

Evaluation 1. Pushing each operand onto the stack when encountered. 2. Evaluating a k-ary operator by using the value located k-1 positions below the top of the stack as the leftmost operand, and so on, till the value on the top of the stack is used as the rightmost operand. 3. After the evaluation, all k operands are popped from the stack, and the result is pushed onto the stack (or there could be a side-effect)

Example Stmt -> ID ‘=‘ expr { stmt.t = expr.t || ‘istore a’} applied to a = 3*b -c bipush 3 iload b imul iload c isub istore a

Java Virtual Machine Analogous to the abstract stack machine, the Java Virtual machine is an abstract processor architecture that defines the behavior of Java Bytecode programs. The stack (in JVM) is referred to as the operand stack or value stack. Operands are fetched from the stack and the result is pushed back on to the stack. Advantages: VM code is compact as the operands need not be explicitly named.

Data Types The int data type ca nold 32 bit signed integers in the range -2^31 to 2^(31) -1. The long data type can hold 64 bit signed integers. Integer instructions in the Java VM are also used to operate on Boolean values. Other data types that Java VM supports are byte, short, float, double. (Your project should handle at least three data types).

Selected Java VM Instructions Java VM instructions are typed I.e., the operator explicitly specifies what operand types it expects. Expression Evaluation sipush n push a 2 byte signed int on to stack iload vload/push a local variable v istore vstore top of stack onto local var v iaddpop tow elements and push their sum isubpop two elements and push their difference

Selected Java VM Instructions Imul pop two elements and push their product iandpop two lements and push their bitwise and ior inegpop top element and push its negation lcmppop two elements (64 bit integers), push the comparison result. 1 if Vs[0]<vs[1], 0 if vs[0]=vs[1] otherwise -1. I2L convert integers to long L2i

Selected Java VM Instructions Branches: GOTO Lunconditional transfer to label l ifeq Ltransfer to label L if top of stack is 0 ifnetransfer to label L if top of stack !=0 Call/Return: Each method/procedure has memory space allocated to hold local variables (vars register), an operand stack (optop register) and an execution environment (frame register)

Selected Java VM Instructions Invokestatic p invoke method p. pop args from stack as initial values of formal parameters (actual parameters are pushed before calling). Returnreturn from current procedure ireturn return from current procedure with integer value on top of stack. Areturn return from current procedure with object reference return value on top of stack.

Selected Java VM Instructions Array Manipulation: Java VM has an object data type reference to arrays and objects newarray int create a new arrae of integers using the top of the stack as the size. Pop the stack and push a reference to the newly created array. Iaload pop array subscript expression on top of stack and array pointer (next stack element). Push value contained in this array element. iastore

Selected Java VM Instructions Object Manipulation new ccreate a new instance of class C (using heap) and push the reference onto stack. Getfield f push value from object field f of object pointed by object reference at the top of stack. Putfield fstore value from vs[1] into field f of object pointed by the object reference vs[0]

Selected Java VM Instructions Simplifying Instructions: ldc constant is a macro which will generate either bipush or sipush depending on c. For the project, we will use the java assembler of Jason Hunt (washington university). Advantages: No need to worry about binary class files. They get generated automatically.Named local variables. Labels instead of byte offsets.

Byte Code (JVM Instructions) No-arg operand: (instructions needing no arguments hence take only one byte.) examples: aaload, aastore,aconsta_null, aload_0, aload_1, areturn, arraylength, astore_0, athrow, baload, iaload, imul etc One-arg operand: bipush, sipush,ldc etc methodref op: invokestatic, invokenonvirtual, invokevirtual

Byte Code (JVM Instructions) Fieldref_arg_op: getfield, getstaic, putfield, pustatic. Class_arg_op: checkcast, instanceof, new labelarg_op (instructions that use labels) goto, ifeq, ifne, jsr, jsr_w etc Localvar_arg_op: iload, fload, aload, istore

Translating an if statement Stmt -> if expr then stmt1 { out = newlabel(); stmt.t = expr.t|| ‘ifnnonnull’ || out || stmt1.t || ‘label’ out: ‘nop’ } example: if ( a +90==7) { x = x+1; x = x+3;}

Translating a while statement Stmt -> WHILE (expr) stmt1 { in =newlabel(); out= neewlabel(); stmt.t = ‘label’ || in|| ‘nop’ || expr.t || ‘ifnonnull’|| out|| stmt1.t || ‘goto’ || in|| ‘label’ || out } stmt.t = ‘label’ || in|| ‘nop’ || expr.t || ‘ifnonnull’|| out|| stmt1.t || ‘goto’ || in|| ‘label’ || out }

Comments and Feedback Project 3 is out. Please start working. PLEASE do not wait for the due date to come. We are looking at the relevant portion of Java. Please keep studying this material.