CS453 Visitor patterns Type checking and Code Generation

Size: px
Start display at page:

Download "CS453 Visitor patterns Type checking and Code Generation"

Transcription

1 CS453 Visitor patterns Type checking and Code Generation

2 Plan for Today Using visitor design pattern for PA3 Type checking for PA3 Code generation for PA3 CS453 Lecture Building ASTs and Visitor Design Pattern 2

3 Visit, In, Out When visiting the AST, we encounter a node for the first time (In encounter) and we encounter the node for the last time (Out encounter). These encounters are often associated with certain actions: Visitor::visitXYZ(node) {! }! inxyz(node);! for each child c of node in left to right order! c.accept(this);! outxyz(node);! inxyz is called when the node is first encountered in the DFLR walk, and outxyz is called when the node is left behind in the DFLR walk. This is often sufficient for code generation purposes (+,-,*,setpixel), but not always: (if, while, &&). WHY NOT? CS453 Lecture Building ASTs and Visitor Design Pattern 3

4 Example Use of the visitor design pattern // in driver: ast_root.accept(new AVRgenVisitor(outfilehandle)); // in AST class MulExp public void accept(visitor v) { v.visitmulexp(this); } // in class DepthFirstVisitor public void inmulexp(mulexp node) { defaultin(node); } public void outmulexp(mulexp node) { defaultout(node); } public void visitmulexp(mulexp node){ inmulexp(node); if(node.getlexp()!= null) node.getlexp().accept(this); if(node.getrexp()!= null) node.getrexp().accept(this); outmulexp(node); } // in type checker and code generator ç This is YOUR job public void outmulexp(mulexp node) { // overrides default // gen code to pop operands, do the *, push the result } CS453 Lecture Building ASTs and Visitor Design Pattern 4

5 Big Picture PA2 Syntax-directed expression evaluation Syntax-directed code generation PA3 Syntax-directed AST creation Visitors for creating the dot file for visualization (provided) Visitor for checking types Visitor for generating code Later assignments Visitor for building a symbol table Visitor for allocating memory for variables Visitor for doing register allocation CS453 Lecture Building ASTs and Visitor Design Pattern 5

6 Code Structure In driver, first call the parser to get an AST: mj_ast_parser parser = new mj_ast_parser(lexer); ast.node.node ast_root = (ast.node.node)parser.parse().value; Next create a dot file for the AST for debugging purposes: java.io.printstream astout = new java.io.printstream( ); ast_root.accept(new DotVisitor(new PrintWriter(astout))); Finally, create Type-Checker and an AVRgenVisitor instances: java.io.printstream avrsout = new java.io.printstream( ); symtable.symtable globalst = new symtable.symtable(); ast_root.accept(new CheckTypes(globalST)); ast_root.accept(new AVRgenVisitor(new PrintWriter(avrsout))); CS453 Lecture Building ASTs and Visitor Design Pattern 6

7 Code Structure cont - CheckTypes and AVRgenVisitor inherit from DepthFirstVisitor. - The first thing to do is move all of the code generation for the main prologue and epilogue into the AVRgenVisitor. - The Meggy.setPixel() code generation should occur in outmeggysetpixel(). - Code generation for expressions: use the AVR run-time stack. Code that evaluates an expression should expects its operands and leaves its result on the top of the run-time stack (RTS), e.g. outfalseexp outtrueexp ldi r24, 0 ldi r24,1 push r24 push r24 - The DFLR visitor can use the outop method to generate code for an operator, because code for the children has just been generated and has left the result(s) on the RTS CS453 Lecture Building ASTs and Visitor Design Pattern 7

8 Code Generation with a Visitor Program class Byte {! public static void main(string[] whatever){! Meggy.setPixel(! // Byte multiplication: Byte x Byte -> Int! }! }! (byte)( (byte)1*(byte)2 ),! // Mixed type expression: Byte x Int -> Int! (byte)( (byte)3 + 4 ), Meggy.Color.WHITE );! MainClass BlockStatement MeggySetPixel ByteCast ByteCast ColorLiteral Meggy.Color.WHITE MulExp PlusExp ByteCast ByteCast ByteCast IntLiteral 4 IntLiteral 1 IntLiteral 2 IntLiteral 3 CS453 Lecture Building ASTs and Visitor Design Pattern 8

9 Type Checking Java allows mixing numeric types. For Meggy-Java this means that many operators allow mixing byte and int byte a = 0, b = 1;! byte b2 = (byte)-b;! byte wrong1 = -b;! byte sum = (byte)(a + 5);! byte wrong2 = a+5;! int ok = - - -b;! // but check out! int wrong3 = ---b;! int j, i = 2;! if (i==b) j = i+b; else j=(byte)((byte)i+b);! CS453 Lecture Building ASTs and Visitor Design Pattern 9

10 Type Checking: valid operand types, result types Signature of an operation (or function) intype 1 x intype 2 x intype n à outtype How to determine valid intypes and resulting outtype? Use given reference compiler MJ.jar, or MJ_PA3.jar Go back to PA1 and create example test programs Let s look at an example CS453 Lecture Building ASTs and Visitor Design Pattern 10

11 If Statement code generation When the visitor encounters ifstmt, inif and outif do not suffice. WHY? - We need more complex control: if / \ B S1 S2 We need to handle the if statement with a visit statement so we can control the order that code is generated for its children, using branches, jumps and labels. First, code needs to be generated for the condition (the result of the condition evaluation has been pushed on the RTS) followed by branching instructions, the then block, control to jump over else block, then the else block, and then the end label. CS453 Lecture Building ASTs and Visitor Design Pattern 11

12 Branches and jumps An AVR detail: as you know from PA1, conditional branches can only go so far in the code, and code generated, e.g for then or else block is not bounded and thus can exceed that limit. Therefore we have to use jmp sometimes. Notice: breq is replaced with with a brne followed by a jmp to handle this cp r24, r25 #WANT breq MJ_L6 brne MJ_L7 jmp MJ_L6 MJ_L7:... inbounded stretch of code MJ_L6: CS453 Lecture Building ASTs and Visitor Design Pattern 12

13 Not: there is no not in AVR, but there is xor truth table for not and xor x y!x x xor y We can implement NOT x with x XOR 1 : outnotexp pop r24! ldi r22, 1! eor r24,r22! push r24! CS453 Lecture Building ASTs and Visitor Design Pattern 13

14 While statement while / \ B S What is the wiring logic? SLbl:! eval B on stack! if false jump to endlbl! gen Code for S! jump to Slbl! endlbl:! CS453 Lecture Building ASTs and Visitor Design Pattern 14

15 Short circuited (wired) AND, equals Similar to the If Statement and While Statement, code generation will need to be implemented in the visitandexp() B1 && / \ B2 can be implemented as: if (B1) return B2 else return false equalexp, the equality operator == Just like in plus and minus, we need to take the mixed type semantics of Java into account, by promoting a byte (1 register) to an int (register pair), making sure the int value correctly preserves the sign (remember the meggy AVR quiz). CS453 Lecture Building ASTs and Visitor Design Pattern 15

CS453 CLASSES, VARIABLES, ASSIGNMENTS

CS453 CLASSES, VARIABLES, ASSIGNMENTS CS453 CLASSES, VARIABLES, ASSIGNMENTS CS453 Lecture Code Generation for Classes 1 PA6 new in MeggyJava member / instance variables local variables assignments let s go check out the new MeggyJava grammar

More information

Plan. Regression testing: Demo of how to use the regress.sh script.

Plan. Regression testing: Demo of how to use the regress.sh script. Plan PA3 and PA4 Look at PA3 peer reviews and some code. PA3 demos Make sure to indicate group(s) you were in and groups you are building off of in README. You must cite other people s code if you use

More information

AVR and MeggyJr Simple

AVR and MeggyJr Simple AVR and MeggyJr Simple Today Finish AVR assembly especially for PA3ifdots.java Meggy Jr Simple run-time library CS453 Lecture AVR Assembly and Meggy Jr Simple 1 AVR Instruction Set Architecture, or Assembly

More information

Implementing Classes, Arrays, and Assignments

Implementing Classes, Arrays, and Assignments Implementing Classes, Arrays, and Assignments Logistics PA4 peer reviews are due Saturday HW9 is due Monday PA5 is due December 5th Will talk about monad implementation at some point, until then check

More information

Implementing Classes and Assignments

Implementing Classes and Assignments Implementing Classes and Assignments Logistics Quiz 5 has been posted PA5 Overview today, will be posted Monday night, Due May 4 th HW4 will also be posted Monday night, Due April 27 th Will talk about

More information

CS453 Arrays in Java in general

CS453 Arrays in Java in general Plan for the day - - Arrays in general -- Array descriptors -- Type checking for arrays -- Code gen for arrays CS453 Arrays in Java in general CS453 Lecture Arrays 1 Arrays An array is a collection of

More information

CS453 Compiler Construction

CS453 Compiler Construction CS453 Compiler Construction Original Design: Michelle Strout Instructor: Wim Bohm wim.bohm@gmail.com, bohm@cs.colostate.edu Computer Science Building 344 Office hour: Monday 1-2pm TA: Andy Stone aistone@gmail.com,

More information

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

More information

Project Compiler. CS031 TA Help Session November 28, 2011

Project Compiler. CS031 TA Help Session November 28, 2011 Project Compiler CS031 TA Help Session November 28, 2011 Motivation Generally, it s easier to program in higher-level languages than in assembly. Our goal is to automate the conversion from a higher-level

More information

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

SEMANTIC ANALYSIS TYPES AND DECLARATIONS SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether

More information

COMP2121: Microprocessors and Interfacing

COMP2121: Microprocessors and Interfacing Interfacing Lecture 9: Program Control Instructions http://www.cse.unsw.edu.au/~cs2121 Lecturer: Hui Wu Session 1, 2006 Program control instructions in AVR Stacks Overview Sample AVR assembly programs

More information

Syntax Errors; Static Semantics

Syntax Errors; Static Semantics Dealing with Syntax Errors Syntax Errors; Static Semantics Lecture 14 (from notes by R. Bodik) One purpose of the parser is to filter out errors that show up in parsing Later stages should not have to

More information

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo 1 Lecture Overview AVR ISA AVR Instructions & Programming (I) Basic construct implementation 2 Atmel AVR 8-bit RISC architecture

More information

CS 432 Fall Mike Lam, Professor. Code Generation

CS 432 Fall Mike Lam, Professor. Code Generation CS 432 Fall 2015 Mike Lam, Professor Code Generation Compilers "Back end" Source code Tokens Syntax tree Machine code char data[20]; int main() { float x = 42.0; return 7; } 7f 45 4c 46 01 01 01 00 00

More information

Practical Malware Analysis

Practical Malware Analysis Practical Malware Analysis Ch 4: A Crash Course in x86 Disassembly Revised 1-16-7 Basic Techniques Basic static analysis Looks at malware from the outside Basic dynamic analysis Only shows you how the

More information

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo 1 Lecture Overview AVR ISA AVR Instructions & Programming (I) Basic construct implementation 2 Atmel AVR 8-bit RISC architecture

More information

CS 553 Compiler Construction Fall 2009 Project #1 Adding doubles to MiniJava Due September 8, 2009

CS 553 Compiler Construction Fall 2009 Project #1 Adding doubles to MiniJava Due September 8, 2009 CS 553 Compiler Construction Fall 2009 Project #1 Adding doubles to MiniJava Due September 8, 2009 In this assignment you will extend the MiniJava language and compiler to enable the double data type.

More information

AVR ISA & AVR Programming (I)

AVR ISA & AVR Programming (I) AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo Week 1 1 Lecture Overview AVR ISA AVR Instructions & Programming (I) Basic construct implementation Week 1 2 1 Atmel AVR 8-bit

More information

CSE 401 Final Exam. December 16, 2010

CSE 401 Final Exam. December 16, 2010 CSE 401 Final Exam December 16, 2010 Name You may have one sheet of handwritten notes plus the handwritten notes from the midterm. You may also use information about MiniJava, the compiler, and so forth

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A

More information

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 21: Generating Pentium Code 10 March 08 CS 412/413 Spring 2008 Introduction to Compilers 1 Simple Code Generation Three-address code makes it

More information

Code Generation. Lecture 12

Code Generation. Lecture 12 Code Generation Lecture 12 1 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

CSE 582 Autumn 2002 Exam Sample Solution

CSE 582 Autumn 2002 Exam Sample Solution Question 1. (10 points) Regular expressions. Describe the set of strings that are generated by the each of the following regular expressions. a) (a (bc)* d)+ One or more of the string a or the string d

More information

Semantic actions for expressions

Semantic actions for expressions Semantic actions for expressions Semantic actions Semantic actions are routines called as productions (or parts of productions) are recognized Actions work together to build up intermediate representations

More information

Summary: Direct Code Generation

Summary: Direct Code Generation Summary: Direct Code Generation 1 Direct Code Generation Code generation involves the generation of the target representation (object code) from the annotated parse tree (or Abstract Syntactic Tree, AST)

More information

Digital Systems - Mini AVR 3

Digital Systems - Mini AVR 3 Digital Systems - Mini AVR 3 Pere Palà - Alexis López itic http://itic.cat April 2016 Enhancing the processor Up to now, we have a linear program flow Suitable for a pre-planned task Unable to react to

More information

Languages and Compiler Design II IR Code Generation I

Languages and Compiler Design II IR Code Generation I Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 4/16/2010 PSU CS322 HM 1 Agenda Grammar G1

More information

CS415 Compilers. Intermediate Represeation & Code Generation

CS415 Compilers. Intermediate Represeation & Code Generation CS415 Compilers Intermediate Represeation & Code Generation These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Review - Types of Intermediate Representations

More information

CS453 Register Allocation

CS453 Register Allocation CS453 Register Allocation Quick quiz In C, how can r=(a+b)+(c+d) be evaluated? e.g. as follows? t1 = a+d t2 = b+c r = t1+t2 (page 207 of ANSI and traditional C ref. manual 3 rd edition) How about in Java?

More information

BM214E Object Oriented Programming Lecture 4

BM214E Object Oriented Programming Lecture 4 BM214E Object Oriented Programming Lecture 4 Computer Numbers Integers (byte, short, int, long) whole numbers exact relatively limited in magnitude (~10 19 ) Floating Point (float, double) fractional often

More information

Writing Evaluators MIF08. Laure Gonnord

Writing Evaluators MIF08. Laure Gonnord Writing Evaluators MIF08 Laure Gonnord Laure.Gonnord@univ-lyon1.fr Evaluators, what for? Outline 1 Evaluators, what for? 2 Implementation Laure Gonnord (Lyon1/FST) Writing Evaluators 2 / 21 Evaluators,

More information

CSE 5317 Midterm Examination 4 March Solutions

CSE 5317 Midterm Examination 4 March Solutions CSE 5317 Midterm Examination 4 March 2010 1. / [20 pts] Solutions (parts a j; -1 point for each wrong answer, 0 points for each blank answer, 2 point for each correct answer. Therefore, the score for this

More information

CPS2000 Compiler Theory & Practice

CPS2000 Compiler Theory & Practice CPS2000 Compiler Theory & Practice Notes on Handcrafting a Parser Gordon Mangion Source File Compiler Lexical Analyser Keyword Table Abstract Syntax Tree Parser Symbol Table? Error Module? Abstract Syntax

More information

LECTURE 3. Compiler Phases

LECTURE 3. Compiler Phases LECTURE 3 Compiler Phases COMPILER PHASES Compilation of a program proceeds through a fixed series of phases. Each phase uses an (intermediate) form of the program produced by an earlier phase. Subsequent

More information

Code Generation. Lecture 30

Code Generation. Lecture 30 Code Generation Lecture 30 (based on slides by R. Bodik) 11/14/06 Prof. Hilfinger CS164 Lecture 30 1 Lecture Outline Stack machines The MIPS assembly language The x86 assembly language A simple source

More information

(Not Quite) Minijava

(Not Quite) Minijava (Not Quite) Minijava CMCS22620, Spring 2004 April 5, 2004 1 Syntax program mainclass classdecl mainclass class identifier { public static void main ( String [] identifier ) block } classdecl class identifier

More information

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

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline. The Main Idea of Today s Lecture Code Generation We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

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

We can emit stack-machine-style code for expressions via recursion Code Generation The Main Idea of Today s Lecture We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

CSC 467 Lecture 13-14: Semantic Analysis

CSC 467 Lecture 13-14: Semantic Analysis CSC 467 Lecture 13-14: Semantic Analysis Recall Parsing is to translate token stream to parse tree Today How to build trees: syntax direction translation How to add information to trees: semantic analysis

More information

Assembly Programming (III)

Assembly Programming (III) Assembly Programming (III) Lecturer: Annie Guo S2, 2006 COMP9032 Week6 1 Lecture Overview Stack and stack operations Functions and function calls Calling conventions S2, 2006 COMP9032 Week6 2 What is stack?

More information

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis? Anatomy of a Compiler Program (character stream) Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analysis Parse Tree Intermediate Code Generator Intermediate Code Optimizer Code Generator

More information

Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu

Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu 1 Lecture overview Stack and stack operations Functions and function calls Calling conventions 2 Stack What is stack?

More information

Procedures and Stacks

Procedures and Stacks Procedures and Stacks Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T. March 15, 2018 L10-1 Announcements Schedule has shifted due to snow day Quiz 2 is now on Thu 4/12 (one week later)

More information

Lecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating

Lecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating 1 ecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating records for arrays o Short-circuiting Or o If statement

More information

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15 Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL

More information

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators Course Name: Advanced Java Lecture 2 Topics to be covered Variables Operators Variables -Introduction A variables can be considered as a name given to the location in memory where values are stored. One

More information

Definition of AST AST and Static Code Analysis Design Patterns. AST Features Applicability of AST JTB JJTree

Definition of AST AST and Static Code Analysis Design Patterns. AST Features Applicability of AST JTB JJTree Definition of AST AST and Design Patterns Błaej Pietrzak blazej.pietrzak@cs.put.poznan.pl AST Features Applicability of AST JTB JJTree Parse Tree Input: id*(id+id) E -> E + E E -> E * E E -> ( E ) E ->

More information

CS 4120 Introduction to Compilers

CS 4120 Introduction to Compilers CS 4120 Introduction to Compilers Andrew Myers Cornell University Lecture 12: Modules, Type representations, Visitors 1 Structuring Analysis Analysis is a traversal of AST Technique used in lecture: recursion

More information

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function Module 8: Atmega32 Stack & Subroutine Stack Pointer Subroutine Call function Stack Stack o Stack is a section of RAM used by the CPU to store information temporarily (i.e. data or address). o The CPU needs

More information

Expressions & Flow Control

Expressions & Flow Control Objectives Distinguish between instance and local variables 4 Expressions & Flow Control Describe how instance variables are initialized Identify and correct a Possible reference before assignment compiler

More information

CSCE 314 Programming Languages. Type System

CSCE 314 Programming Languages. Type System CSCE 314 Programming Languages Type System Dr. Hyunyoung Lee 1 Names Names refer to different kinds of entities in programs, such as variables, functions, classes, templates, modules,.... Names can be

More information

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam Compilers Intermediate representations and code generation Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Today Intermediate representations and code generation Scanner Parser Semantic

More information

COMP3131/9102: Programming Languages and Compilers

COMP3131/9102: Programming Languages and Compilers COMP3131/9102: Programming Languages and Compilers Jingling Xue School of Computer Science and Engineering The University of New South Wales Sydney, NSW 2052, Australia http://www.cse.unsw.edu.au/~cs3131

More information

Lecture 3: C Programm

Lecture 3: C Programm 0 3 E CS 1 Lecture 3: C Programm ing Reading Quiz Note the intimidating red border! 2 A variable is: A. an area in memory that is reserved at run time to hold a value of particular type B. an area in memory

More information

Lecture #31: Code Generation

Lecture #31: Code Generation Lecture #31: Code Generation [This lecture adopted in part from notes by R. Bodik] Last modified: Tue Apr 10 18:47:46 2018 CS164: Lecture #31 1 Intermediate Languages and Machine Languages From trees such

More information

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.

More information

Semantic Analysis. Lecture 9. February 7, 2018

Semantic Analysis. Lecture 9. February 7, 2018 Semantic Analysis Lecture 9 February 7, 2018 Midterm 1 Compiler Stages 12 / 14 COOL Programming 10 / 12 Regular Languages 26 / 30 Context-free Languages 17 / 21 Parsing 20 / 23 Extra Credit 4 / 6 Average

More information

CSE 401/M501 Compilers

CSE 401/M501 Compilers CSE 401/M501 Compilers ASTs, Modularity, and the Visitor Pattern Hal Perkins Autumn 2018 UW CSE 401/M501 Autumn 2018 H-1 Agenda Today: AST operations: modularity and encapsulation Visitor pattern: basic

More information

Functions. Ray Seyfarth. August 4, Bit Intel Assembly Language c 2011 Ray Seyfarth

Functions. Ray Seyfarth. August 4, Bit Intel Assembly Language c 2011 Ray Seyfarth Functions Ray Seyfarth August 4, 2011 Functions We will write C compatible function C++ can also call C functions using extern "C" {...} It is generally not sensible to write complete assembly programs

More information

CS 406: Syntax Directed Translation

CS 406: Syntax Directed Translation CS 406: Syntax Directed Translation Stefan D. Bruda Winter 2015 SYNTAX DIRECTED TRANSLATION Syntax-directed translation the source language translation is completely driven by the parser The parsing process

More information

1.00 Lecture 8. Using An Existing Class, cont.

1.00 Lecture 8. Using An Existing Class, cont. .00 Lecture 8 Classes, continued Reading for next time: Big Java: sections 7.9 Using An Existing Class, cont. From last time: is a Java class used by the BusTransfer class BusTransfer uses objects: First

More information

Compilers and computer architecture: A realistic compiler to MIPS

Compilers and computer architecture: A realistic compiler to MIPS 1 / 1 Compilers and computer architecture: A realistic compiler to MIPS Martin Berger November 2017 Recall the function of compilers 2 / 1 3 / 1 Recall the structure of compilers Source program Lexical

More information

EE 308: Microcontrollers

EE 308: Microcontrollers EE 308: Microcontrollers Assmbly Language Part I Aly El-Osery Electrical Engineering Department New Mexico Institute of Mining and Technology Socorro, New Mexico, USA January 30, 2018 Aly El-Osery (NMT)

More information

CS 2210 Programming Project (Part IV)

CS 2210 Programming Project (Part IV) CS 2210 Programming Project (Part IV) April 25, 2018 Code Generation This project is intended to give you experience in writing a code generator as well as bring together the various issues of code generation

More information

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines Lecture Outline Code Generation Lecture 30 (based on slides by R. Bodik) Stack machines The MIPS assembly language The x86 assembly language A simple source language Stack-machine implementation of the

More information

Software Protection: How to Crack Programs, and Defend Against Cracking Lecture 3: Program Analysis Moscow State University, Spring 2014

Software Protection: How to Crack Programs, and Defend Against Cracking Lecture 3: Program Analysis Moscow State University, Spring 2014 Software Protection: How to Crack Programs, and Defend Against Cracking Lecture 3: Program Analysis Moscow State University, Spring 2014 Christian Collberg University of Arizona www.cs.arizona.edu/ collberg

More information

Winter Compiler Construction T10 IR part 3 + Activation records. Today. LIR language

Winter Compiler Construction T10 IR part 3 + Activation records. Today. LIR language Winter 2006-2007 Compiler Construction T10 IR part 3 + Activation records Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University Today ic IC Language Lexical Analysis Syntax Analysis

More information

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division CS 164 Spring 2009 P. N. Hilfinger CS 164: Final Examination (corrected) Name: Login: You have

More information

Code Generation. Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1

Code Generation. Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1 Code Generation Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1 Lecture Outline Stack machines The MIPS assembly language The x86 assembly language A simple source language Stack-machine implementation

More information

Semantic Analysis. Compiler Architecture

Semantic Analysis. Compiler Architecture Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Source Compiler Architecture Front End Scanner (lexical tokens Parser (syntax Parse tree Semantic Analysis

More information

ECE232: Hardware Organization and Design

ECE232: Hardware Organization and Design ECE232: Hardware Organization and Design Lecture 4: Logic Operations and Introduction to Conditionals Adapted from Computer Organization and Design, Patterson & Hennessy, UCB Overview Previously examined

More information

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d) CMSC 330: Organization of Programming Languages Tail Calls A tail call is a function call that is the last thing a function does before it returns let add x y = x + y let f z = add z z (* tail call *)

More information

Compilers CS S-07 Building Abstract Assembly

Compilers CS S-07 Building Abstract Assembly Compilers CS414-2017S-07 Building Abstract Assembly David Galles Department of Computer Science University of San Francisco 07-0: Abstract Assembly Trees Once we have analyzed the AST, we can start to

More information

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 4 Robert Grimm, New York University 1 Review Last week Control Structures Selection Loops 2 Outline Subprograms Calling Sequences Parameter Passing

More information

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto Data Structures and Algorithms in Compiler Optimization Comp314 Lecture Dave Peixotto 1 What is a compiler Compilers translate between program representations Interpreters evaluate their input to produce

More information

CS 61c: Great Ideas in Computer Architecture

CS 61c: Great Ideas in Computer Architecture MIPS Functions July 1, 2014 Review I RISC Design Principles Smaller is faster: 32 registers, fewer instructions Keep it simple: rigid syntax, fixed instruction length MIPS Registers: $s0-$s7,$t0-$t9, $0

More information

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis The Compiler So Far Overview of Semantic Analysis Adapted from Lectures by Profs. Alex Aiken and George Necula (UCB) Lexical analysis Detects inputs with illegal tokens Parsing Detects inputs with ill-formed

More information

CSE 413 Winter 2001 Final Exam Sample Solution

CSE 413 Winter 2001 Final Exam Sample Solution Question 1. (12 points, 4 each) Regular expressions. (a) Describe the set of strings generated by the regular expression ((xy*x) (yx*y))* In any order, 0 or more pairs of x s with 0 or more y s between

More information

Code Generation. Lecture 19

Code Generation. Lecture 19 Code Generation Lecture 19 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

G Compiler Construction Lecture 12: Code Generation I. Mohamed Zahran (aka Z)

G Compiler Construction Lecture 12: Code Generation I. Mohamed Zahran (aka Z) G22.2130-001 Compiler Construction Lecture 12: Code Generation I Mohamed Zahran (aka Z) mzahran@cs.nyu.edu semantically equivalent + symbol table Requirements Preserve semantic meaning of source program

More information

CSE 401/M501 Compilers

CSE 401/M501 Compilers CSE 401/M501 Compilers x86-64, Running MiniJava, Basic Code Generation and Bootstrapping Hal Perkins Autumn 2018 UW CSE 401/M501 Autumn 2018 M-1 Running MiniJava Programs To run a MiniJava program Space

More information

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

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13 Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

More information

Lecture Code Generation for WLM

Lecture Code Generation for WLM Lecture 17+18 Code Generation for WLM CS 241: Foundations of Sequential Programs Winter 2018 Troy Vasiga et al University of Waterloo 1 Code Generation (A9/A10) Input: Output: Number of different outputs:

More information

CSE P 501 Exam 8/5/04 Sample Solution. 1. (10 points) Write a regular expression or regular expressions that generate the following sets of strings.

CSE P 501 Exam 8/5/04 Sample Solution. 1. (10 points) Write a regular expression or regular expressions that generate the following sets of strings. 1. (10 points) Write a regular ression or regular ressions that generate the following sets of strings. (a) (5 points) All strings containing a s, b s, and c s with at least one a and at least one b. [abc]*a[abc]*b[abc]*

More information

Plan for Today. Safe Programming Languages. What is a secure programming language?

Plan for Today. Safe Programming Languages. What is a secure programming language? cs2220: Engineering Software Class 19: Java Security Java Security Plan for Today Java Byte s () and Verification Fall 2010 UVa David Evans Reminder: Project Team Requests are due before midnight tomorrow

More information

JJTree. The Compilation Task. Automated? JJTree. An easier way to create an Abstract Syntax Tree

JJTree. The Compilation Task. Automated? JJTree. An easier way to create an Abstract Syntax Tree JJTree An easier way to create an Abstract Syntax Tree The Compilation Task Input character stream Lexer Token stream Parser Abstract Syntax Tree Analyser Annotated AST Code Generator Code CC&P 2003 1

More information

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine Lecture Outline Code Generation Lecture 12 Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. CMSC 131: Chapter 28 Final Review: What you learned this semester The Big Picture Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. Java

More information

Intermediate Code, Object Representation, Type-Based Optimization

Intermediate Code, Object Representation, Type-Based Optimization CS 301 Spring 2016 Meetings March 14 Intermediate Code, Object Representation, Type-Based Optimization Plan Source Program Lexical Syntax Semantic Intermediate Code Generation Machine- Independent Optimization

More information

Project 2 Interpreter for Snail. 2 The Snail Programming Language

Project 2 Interpreter for Snail. 2 The Snail Programming Language CSCI 2400 Models of Computation Project 2 Interpreter for Snail 1 Overview In this assignment you will use the parser generator yacc to construct an interpreter for a language called Snail containing the

More information

CS 406/534 Compiler Construction Putting It All Together

CS 406/534 Compiler Construction Putting It All Together CS 406/534 Compiler Construction Putting It All Together Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

More information

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

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram: The CPU and Memory How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram: 1 Registers A register is a permanent storage location within

More information

EC 413 Computer Organization

EC 413 Computer Organization EC 413 Computer Organization Review I Prof. Michel A. Kinsy Computing: The Art of Abstraction Application Algorithm Programming Language Operating System/Virtual Machine Instruction Set Architecture (ISA)

More information

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we

More information

Outline. More optimizations for our interpreter. Types for objects

Outline. More optimizations for our interpreter. Types for objects Outline More optimizations for our interpreter Types for objects Optimization Eliminate tree walks: object creation, method calls fish initialize get_ grow eat colorfish color set_color get_color pickyfish

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

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

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Language Processing Systems Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Semantic Analysis Compiler Architecture Front End Back End Source language Scanner (lexical analysis)

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information