TACi: Three-Address Code Interpreter (version 1.0)

Similar documents
& Simulator. Three-Address Instructions. Three-Address Instructions. Three-Address Instructions. Structure of an Assembly Program.

COMPILER CONSTRUCTION (Intermediate Code: three address, control flow)

Intermediate Code Generation

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

Intermediate Representa.on

Compiling Techniques

Introduction to Programming Using Java (98-388)

Intermediate Representations & Symbol Tables

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

Lexical Considerations

CA4003 Compiler Construction Assignment Language Definition

Concepts Introduced in Chapter 6

Motivation was to facilitate development of systems software, especially OS development.

Basics of Java Programming

CMPSC 160 Translation of Programming Languages. Three-Address Code

Intermediate Code Generation

Intermediate Representations

Motivation was to facilitate development of systems software, especially OS development.

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

1 Lexical Considerations

Intermediate Code Generation

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Decaf Language Reference Manual

Quick Reference Guide

Full file at

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Chapter 2: Data and Expressions

Lecture 3. More About C

Language Reference Manual simplicity

Chapter 2: Using Data

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

(Not Quite) Minijava

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS LIR Language Specification and microlir Simulator Version 1.2

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Concepts Introduced in Chapter 6

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Semantic actions for expressions

A flow chart is a graphical or symbolic representation of a process.

6.096 Introduction to C++ January (IAP) 2009

CA Compiler Construction

Fall Compiler Principles Lecture 5: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

UNIT- 3 Introduction to C++

General issues. Section 9.1. Compiler Construction: Code Generation p. 1/18

Chapter 2: Data and Expressions

ECE232: Hardware Organization and Design

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

INTERMEDIATE REPRESENTATIONS RTL EXAMPLE

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Quick Reference Guide

Formal Languages and Compilers Lecture X Intermediate Code Generation

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Preview from Notesale.co.uk Page 6 of 52

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Operators. Java operators are classified into three categories:

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Introduction to Programming (Java) 2/12

Chapter 2: Data and Expressions

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

Computer Components. Software{ User Programs. Operating System. Hardware

Decaf Language Reference

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

COMP-520 GoLite Tutorial

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Compiler construction 2009

Java Basic Programming Constructs

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

CSc 453. Compilers and Systems Software. 13 : Intermediate Code I. Department of Computer Science University of Arizona

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Homework #3 CS2255 Fall 2012

We can emit stack-machine-style code for expressions via recursion

Intermediate Code Generation

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Language Reference Manual

CSE 504: Compiler Design. Runtime Environments

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Programming Language Concepts: Lecture 2

Fall Compiler Principles Lecture 6: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Part I Part 1 Expressions

Lexical Considerations

CS112 Lecture: Primitive Types, Operators, Strings

Short Notes of CS201

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Compiler Construction I

Computer Organisation CS303

The PCAT Programming Language Reference Manual

Transcription:

TACi: Three-Address Code Interpreter (version 1.0) David Sinclair September 23, 2018 1 Introduction TACi is an interpreter for Three-Address Code, the common intermediate representation (IR) used in compilers. TACi is written in Java using the compiler tools JavaCC and JJTree. There are many variation of Three-Address Code and this document describes the version of Three-Address Code supported by TACi. 2 Usage To run the Three-Address Code interpreter on a file called file.tac, type: java -jar TACi.jar file.tac TACi can be invoked with the -d flag to run in debug mode. In this mode TACi will display the structure of the parsed AST, the final value of each variable, a list of the labels (entry points) in the program and the final location that each pointer references. 3 Three-Address Code In Three-address Code there is at most one operator on the right-hand of an instruction. Hence, in Three-Address Code, the valid instructions for expressions are: x = y op z x = op y x = y Complex expressions in the source language can be translated in a sequence of Three-Address Code instructions using temporary variables. For example w = x+y z would be translated to: 1

t1 = y - z w = x + t1 Three-Address Code is built on the concepts of addresses and instructions. An address can be a Name or a Constant. The Names can include temporary variables created by a compiler, which are usually removed in subsequent optimization processes. In bf TACi Names are not explicitly typed. They are typed by the values assigned to them. The supported types in TACi are: integers A whole (non-fractional) number in the range 2,147,483,648 to 2,147,483,647 inclusive. floats Real numbers in the range 1.79769313486231570x10 308 to 1.79769313486231570x10 308 approximately. booleans true or false strings 3.1 Instructions A sequence of alphanumeric characters (including the space character, the apostrophe character, the exclamation character and the backslash character) enclosed in double quotes, e.g. "Hello there!". Each instruction is on a separate line. 3.1.1 Arithmetic Instructions x = y op z Assignmentswhereop isabinaryarithmetic (e.g. +,-,*,/) or binary logical (e.g. &&, ) operation. x = op y Assignments where op is a unary operation. Currently only logical negation is supported. x = y Copy instructions 3.1.2 Branches name: goto L if x relop y goto L Defines name as a label. A label must be defined by itself on a line. Every TACi program must have a main: label. Unconditional jump to label L Conditional jump where control is passed to label L if x relop y is true and relop is a binary relational operator (e.g. >, >=, ==,!=, <, <=, etc.). Otherwise control passes to the next instruction. ifz x relop y goto L Conditional jump where control is passed to label L if x relop y is false and relop is a binary relational operator (e.g. >, >=, ==,!=, <, <=, etc.). Otherwise control passes to the next instruction. 2

3.1.3 Functions param x n... paramx 1 x = getparam n call p, n y = call p, n return return x The arguments to procedure and function calls are defined by the param instructions. Parameters are placed on the stack in reverse order. Returns a copy of the n-th parameter from the stack. An invocation of procedure p that takes n arguments. After the call to p the n parameters are cleared from the stack. An invocation of function p that takes n arguments. The result of the call to p is returned and stored in y. After the call to p the n parameters are cleared from the stack. Passes control to the instruction following the call instruction that invoked the procedure p. Passes control to the instruction following the call instruction that invoked the function p. The value of x returned. 3.1.4 Arrays TACi supports arrays by allowing an array indexed operation as a valid name. Hence, reading from an array and writing to an array are valid Three-Address Code instructions. x = p[i] + 3 Reading from an array by an index value. p[j] = x op y Writing the result of x op y into an array by index value Arrays are declared using.data directive, e.g. p.data 24 This example declares p as an array of size 24. Array indices start at 0. Accesses to arrays are bounds-checked by TACi to ensure that they are between 0 and (size 1) inclusive. Out-of-bounds array accesses generate a runtime exception. TACi make no assumption on the size of each element in the array. You should assume that each element of the declared array holds a byte. Then if the targeted architectures store an integer in 4 bytes, to store the value 1 in the i-th element of an array called p you should use t1 = i * 4 p[t1] = 1 where t1 is a temporary variable. 3

3.1.5 Pointers TACi supports pointers and basic pointer arithmetic. x = &y stores the address of y in x. x is a pointer to y. z = *x The contents of what x points to can be accessed by x. x is a name in TACi. *x = y op z Stores the result of y op z in the variable that x points to. If a pointer is assigned the address of an array, it will point to the first element in the array. Hence x = &p and x = &[p0] are equivalent. Basic pointer arithmetic using addition and subtraction are supported. 3.2 Library Procedures and Functions TACi supports some basic library procedures and functions. _exit _read _print _exit takes no arguments and exits the parsed program. _read takes no arguments and returns the next item read from the console. _print takes one argument (from the stack) and displays it on the console. _println _println behaves as _print but also adds a newline character after displaying its argument. 3.3 Comments Comments use the C++ style. They either begin with /* and end with */, or they begin with // and continue to the end of the current line. 4 Example The following is an example of a program to calculate the greatest common divisor written in Three-Address Code. mod: // modulus function, returns parameter 1 mod parameter 2 mx = getparam 1 my = getparam 2 mt1 = mx / my mt2 = mt1 * my mt3 = mx - mt2 return mt3 4

gcd: // greatest common devisor function, Euclid s algorithm ga = getparam 1 gb = getparam 2 gwb: ifz gb!= 0 goto gwe gt = gb param gb param ga gb = call mod, 2 ga = gt goto gwb gwe: return ga main: s1 = "Enter 1st number " param s1 x = call _read, 0 s2 = "Enter 2nd number " param s2 y = call _read, 0 param y param x answer = call gcd, 2 os = "Answer is " param os param answer call _println, 1 call _exit, 0 Assuming the file is called gcd.tac, then it an be interpreted as follows. $ java -jar TACi.jar gcd.tac Three Address Code Interpreter (TACi) v1.0 TAC source gcd.tac parsed successfully Enter 1st number 1071 Enter 2nd number 462 Answer is 21 $ 5