An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Similar documents
Topic 3-a. Calling Convention 2/29/2008 1

CA Compiler Construction

Run-time Environments

Run-time Environments

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

SPIM Procedure Calls

CSE Lecture In Class Example Handout

MIPS Functions and the Runtime Stack

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

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

Run-time Environment

Functions and Procedures

MIPS Procedure Calls. Lecture 6 CS301

ECE232: Hardware Organization and Design

CS64 Week 5 Lecture 1. Kyle Dewey

Course Administration

Lecture 7: Procedures and Program Execution Preview

The Activation Record (AR)

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

COSE312: Compilers. Lecture 1 Overview of Compilers

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

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

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

Lecture 7: Procedures

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

An Overview of Compilation

Code Generation. Lecture 19

THEORY OF COMPILATION

Overview of Compiler. A. Introduction

Compiling Regular Expressions COMP360

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Compilers and computer architecture: A realistic compiler to MIPS

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Instruction Set Architecture

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

Review of Activation Frames. FP of caller Y X Return value A B C

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

COE608: Computer Organization and Architecture

LECTURE 3. Compiler Phases

EE 361 University of Hawaii Fall

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

CSE Lecture In Class Example Handout

Lecture 5. Announcements: Today: Finish up functions in MIPS

Storage in Programs. largest. address. address

MIPS Assembly (Functions)

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

Project Compiler. CS031 TA Help Session November 28, 2011

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

Midterm II CS164, Spring 2006

CS/COE1541: Introduction to Computer Architecture

Today. Putting it all together

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

CS61C : Machine Structures

UCB CS61C : Machine Structures

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

Syntax-Directed Translation. Lecture 14

Procedures and Stacks

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

Functions in MIPS. Functions in MIPS 1

CS 4201 Compilers 2014/2015 Handout: Lab 1

Summer 2003 Lecture 14 07/02/03

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

Lecture 5: Procedure Calls

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

G Programming Languages - Fall 2012

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

ECE 30 Introduction to Computer Engineering

6.004 Tutorial Problems L3 Procedures and Stacks

System Software Assignment 1 Runtime Support for Procedures

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

CS143 - Written Assignment 4 Reference Solutions

CSC258: Computer Organization. Functions and the Compiler Tool Chain

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

The role of semantic analysis in a compiler

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

ECE260: Fundamentals of Computer Engineering

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language

Compiler Architecture

Lecture 5: Procedure Calls

Lecture08: Scope and Lexical Address

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

Topic 7: Activation Records

ECE331: Hardware Organization and Design

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

Code Generation. Lecture 12

CS61C : Machine Structures

Compiling Techniques

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions

MIPS%Assembly% E155%

Generation. representation to the machine

Transcription:

An Overview to Compiler Design 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Outline An Overview of Compiler Structure Front End Middle End Back End 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 2

Reading Slides and Lecture Notes Aho,Lam,Sethi,Ullman: Chapter 1.4 ~ 1.5 Chapter 2.1 ~ 2.7 Chapter 3.1 ~ 3.5 Chapter 4.1 ~ 4.5 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 3

A Review on Compiler Structure/Design Overall structure Front-end: lexical and syntax analysis Middle-end: machine independent code analysis and (scalar, and sometimes loop nest ) optimization Back-end: Machine dependent code analysis and optimization 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 4

Motivation for Compiler optimization Source Program COMPILER Machine Code Input program example: int foo( ) { int x; return (x + x) ; } 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 5

Output Assembly Code (non-optimized) optimized) foo: sub $sp, $sp, 8 sw $fp, 8($sp) add $fp, $sp, 8 sw $ra, -4($fp) add $t0, $a0 $a0 move $v0, $t0 lw $ra, -4($fp) lw $fp, 0($fp) add $sp, $sp, 8 jr $ra 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 6

Output Assembly Code (non-optimized) optimized) foo: sub $sp, $sp, 8 % Push stack frame sw $fp, 8($sp) % Save old frame pointer add $fp, $sp, 8 % Set new frame pointer sw $ra, -4($fp) % Save return address add $t0, $a0 $a0 % Addition move $v0, $t0 % Copy return value lw $ra, -4($fp) % Restore return address lw $fp, 0($fp) % Restore frame pointer add $sp, $sp, 8 % Pop stack frame jr $ra % Jump to return address 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 7

Output Assembly Code -- Revisited (non-optimized) optimized) foo: sub $sp, $sp, 8 % Push stack frame (for what?) sw $fp, 8($sp) % Save old frame pointer (for what?) add $fp, $sp, 8 % Set new frame pointer sw $ra, -4($fp) % Save return address (for what?) add $t0, $a0, $a0 % Addition move $v0, $t0 % Copy return value lw $ra, -4($fp) % Restore return address lw $fp, 0($fp) % Restore frame pointer add $sp, $sp, 8 % Pop stack frame jr $ra % Jump to return address 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 8

Output Assembly Code (optimized) foo: add $v0, $a0, $a0 % Set result jr $ra % Jump to return address 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 9

Runtime Memory Organization Entry point for procedure 1 Entry point for procedure 2 Entry point for procedure n code for procedure 1 code for procedure 2 code for procedure n Code memory code area global/static area stack free space heap frame-1 frame-2 frame-3 Runtime stack Space for arguments (parameters) Space for bookkeeping Information, including Return address Space in local data Space for local temporaries A stack frame Address space 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 10

Phases of a Compiler Source program Lexical Analyzer (Scanner) Tokens Syntax Analyzer (Parser) Parse tree Semantic Analyzer Abstract Syntax Tree w/ Attributes Intermediate-code Generator Non-optimized Intermediate Code Intermediate-code Optimizer Optimized Intermediate Code Target-code Generator/Opt Target machine code 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 11

Low-level Model Lexical analyzer Parser Semantic analyzer Translator Optimizer String of characters String of tokens Parse tree Abstract syntax tree Low-level intermediate code Low-level intermediate code Final assembly Relocatable object module or runnable machine code Two models of compiler structures (Muchnick, pp. 08) Mixed-level Model Lexical analyzer Parser Parse tree Semantic analyzer Abstract Syntax tree Intermediate-code generator Optimizer Code generator Postpass optimizer String of characters String of tokens Medium-level intermediate code Medium-level intermediate code Low-level intermediate code Relocatable object module or runnable machine code 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 12

A Good Compiler Infrastructure Needed A modern View Front end Interprocedural Analysis and Optimization Good IR Loop Nest Optimization and Parallelization Global (Scalar) Optimization Middle-End Backend Code Generation 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 13