Lecture Code Generation for WLM

Similar documents
Warm Up Problem. Write any equivalent MIPS program corresponding to the following code: int wain ( int a, int b){ int c = a + b; return c*a; }

CS241 Final Exam Review

Lecture Bottom-Up Parsing

A clarification on terminology: Recognizer: accepts or rejects strings in a language. Parser: recognizes and generates parse trees (imminent topic)

CSE 340 Fall 2014 Project 4

CSE 582 Autumn 2002 Exam Sample Solution

Sticker. CS 241 Fall 2009 Final Exam. Draft of 6 December 01:43 hrs

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

CS 406: Syntax Directed Translation

Project Compiler. CS031 TA Help Session November 28, 2011

Lecture 20 Sections Wed, Apr 1, 2009

CS 2210 Programming Project (Part IV)

CS 536 Midterm Exam Spring 2013

CMSC 350: COMPILER DESIGN

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

TML Language Reference Manual

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

2. λ is a regular expression and denotes the set {λ} 4. If r and s are regular expressions denoting the languages R and S, respectively

Question Marks 1 /16 2 /13 3 /12 4 /15 5 /8 6 /15 7 /8 8 /5 9 /8 Total /100

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

CSE P 501 Exam 8/5/04

Introduction to Compiler Design

Intermediate Representations

Yacc Yet Another Compiler Compiler

Topic 3: Syntax Analysis I

Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.

ECS 142 Project: Code generation hints

CSE 582 Autumn 2002 Exam 11/26/02

SOURCE LANGUAGE DESCRIPTION

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

CSE P 501 Exam 11/17/05 Sample Solution

CS 403: Scanning and Parsing

Lexical Analysis. Textbook:Modern Compiler Design Chapter 2.1

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

Getting started with LLVM using Swift. Alex Denisov,

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

GAWK Language Reference Manual

Compiler Theory. (Semantic Analysis and Run-Time Environments)

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Lecture 8: Context Free Grammars

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

Defining syntax using CFGs

Syntax Analysis Check syntax and construct abstract syntax tree

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

G Programming Languages - Fall 2012

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

Compilers. Bottom-up Parsing. (original slides by Sam

Simple Lexical Analyzer

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Syntax-Directed Translation. Introduction

Syntax Analysis Part I

UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. M.Sc. in Computational Science & Engineering

Lab 2 Tutorial (An Informative Guide)

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

Fall Compiler Principles Lecture 5: Parsing part 4. Roman Manevich Ben-Gurion University

Bottom-Up Parsing. Lecture 11-12

B E C Y. Reference Manual

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

CMPT 379 Compilers. Parse trees

Syntax-Directed Translation. Lecture 14

Abstract Syntax Trees Synthetic and Inherited Attributes

PLT 4115 LRM: JaTesté

Lecture 7: Deterministic Bottom-Up Parsing

CS 432 Fall Mike Lam, Professor. Code Generation

Conflicts in LR Parsing and More LR Parsing Types

PLT Project Language Reference Manual

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Compiler construction in4303 lecture 3

COP5621 Exam 3 - Spring 2005

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Function Definition Syntax Tree

Midterm II CS164, Spring 2006

Compiler construction lecture 3

Parsing III. (Top-down parsing: recursive descent & LL(1) )

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

LL(1) predictive parsing

Fall Compiler Principles Lecture 4: Parsing part 3. Roman Manevich Ben-Gurion University of the Negev

Semantic actions for expressions

Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson

CSci 450, Org. of Programming Languages. Chapter 12 Expression Language Compilation

In One Slide. Outline. LR Parsing. Table Construction

Monday, September 13, Parsers

Bottom-Up Parsing. Lecture 11-12

PRACTICAL CLASS: Flex & Bison

The input of your program is specified by the following context-free grammar:

Wednesday, September 9, 15. Parsers

Program Analysis ( 软件源代码分析技术 ) ZHENG LI ( 李征 )

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

LL(1) predictive parsing

LECTURE 11. Semantic Analysis and Yacc

UNIT IV INTERMEDIATE CODE GENERATION

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.

Introduction to Programming Using Java (98-388)

CIT 3136 Lecture 7. Top-Down Parsing

Lecture #31: Code Generation

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

Table-Driven Top-Down Parsers

LR Parsing. Table Construction

Transcription:

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: 2

Code Generation Issues Correctness Ease of writing compiler Efficiency of the compiler Efficiency of the compiled code 3

Code Generation via Syntax-Directed Translation Fundamental idea: traverse the parse tree and gather information When generating the code for a node, it will rely on the code of children Crucial point: when discussing combining code in the following slides, we will use code(x)+code(y) to indicate put the code of X first followed by the code of Y. Do not use string concatenation or its equivalent to do this! Your running time will blow up! Create the final code by a tree traversal! 4

Initial conventions Before we get into the actual code generation, we should map out a plan. In particular, we should have some conventions which we will follow. These include: parameters to wain are in $1 and $2; return value into $3 we will use some registers for storing special constants we will use some registers to hold temporary values we will use some registers to hold other special non-constant values 5

First rule procedure INT ID LPAREN params RPAREN LBRACE dcls statements RETURN expr SEMI RBRACE 6

Three more simple rules statements ɛ expr term term factor In general, when α, β N: α β 7

A9P2 Recall that the input must be semantically valid. What do the WLM programs look like? int wain(int a, int b) { return 0; } (where 0 could be any other number). What does the equivalent MIPS code look like? We will see that we will produce many more lines than this in our final solution, but more on that as we continue... 8

A9P3 Recall that the input must be semantically valid. What do the WLM programs look like? int wain(int a, int b) { int wain(int a, int b) { return a; return b; } } What to the parse trees look like? What do the equivalent MIPS programs look like? 9

Changes to the symbol table 10

How to store variables Option A: Variables in Registers One variable per register, stored somehow in symbol table. Problems: Option B: Variables in RAM using.word Each variable x in WLP4 program corresponds to label x in MIPS. Example: Problems: 11

Option C Option C: Variables on Stack Suppose we have n variables How to get n? Picture in RAM Where is the ith variable? What about $30 changing? 12

A special note about parameters in $1 and $2 13

Some conventions Remember we have: $0 $1 $2 $3 $30 $31 plus $4 $11 $29 14

Design decisions Look for opportunities to generalize and abstract away details. Specifically: push pop getvariable... 15

Code Structure Prologue Generated Code Epilogue 16

Wain Structure Prologue Generated Code Epilogue 17

One more rule (A9P4) factor LPAREN expr RPAREN What this gives us: 18

A9P4: Full expressions int wain(int a, int b) { return a+b; } int wain(int a, int b) { return a+b+a; } 19

A9P4: Parse Tree 20

A9P4: Pseudocode 21

A9P5: getchar factor pcall pcall ID LPAREN RPAREN where ID must be getchar 22

Where is getchar? Solution 1: put it as a procedure after wain Solution 2: inline the procedure Short-term solutions may not lead to long-term solutions 23

A9P6: putchar statements statements statement statement pcall SEMI pcall ID LPAREN expr RPAREN 24

Other advice comments everywhere! rule-based structure of your code each grammar rule will represent one function/procedure/step in our compilation process the parse tree indicates which subtrees need to produce the code before the entire code has been produced read the WHOLE assignment before beginning and PLAN ahead test, test, test 25

Declarations (A10P1) dcls dcls dcl BECOMES NUM SEMI 26

Giving variables values (A10P1) statement ID BECOMES expr SEMI 27

Booleans (A10P2) The only spot where booleans are allowed are in control structures. Rule: test expr LT expr Conventions: MIPS code: 28

while loop (A10P2) statement WHILE LPAREN test RPAREN LBRACE statements RBRACE MIPS code: Long-range problem: 29

if statement (A10P4) statement IF test... statements... ELSE... statements... Two choices: what code to place first 30

Back to booleans (A10P3) Rule: test expr GT expr Rule: test expr NE expr 31

Finishing Booleans (A10P3) Rule: test expr EQ expr Rule: test expr GE expr Rule: test expr LE expr 32