Compiler construction in4303 lecture 8

Similar documents
Compiler construction in4303 lecture 9

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

CS 432 Fall Mike Lam, Professor. Code Generation

ECS 142 Project: Code generation hints

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Code Generation for Control Flow

Compiler construction in4303 answers

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Intermediate Code Generation

Register Allocation. Introduction Local Register Allocators

CS415 Compilers. Intermediate Represeation & Code Generation

Semantic actions for expressions

CS 2210 Programming Project (Part IV)

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

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

The role of semantic analysis in a compiler

Code Generation. Lecture 12

Code Generation. Lecture 19

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Code Generation. Lecture 30

Compilers CS S-07 Building Abstract Assembly

Abstract Interpretation Continued

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CSE 504: Compiler Design. Code Generation

Today's Topics. CISC 458 Winter J.R. Cordy

Summary: Direct Code Generation

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

Compilers CS S-08 Code Generation

Compiler Architecture

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

Lecture 12: Conditional Expressions and Local Binding

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

MIPS Procedure Calls. Lecture 6 CS301

Compilers and computer architecture: A realistic compiler to MIPS

Chapter 20: Binary Trees

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

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

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Building Interpreters

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

Course Administration

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Agenda. CSE P 501 Compilers. Big Picture. Compiler Organization. Intermediate Representations. IR for Code Generation. CSE P 501 Au05 N-1

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Problem with Scanning an Infix Expression

Topics Power tends to corrupt; absolute power corrupts absolutely. Computer Organization CS Data Representation

Intermediate Representations

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

More On Syntax Directed Translation

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

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Compiler construction lecture 3

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Code Generation. Dragon: Ch (Just part of it) Holub: Ch 6.

Assignment 11: functions, calling conventions, and the stack

Lecture 4: MIPS Instruction Set

Compiling Code, Procedures and Stacks

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

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

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

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

Problem Score Max Score 1 Syntax directed translation & type

Compiler Optimization

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

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.

Compiler Construction D7011E

Compiling Techniques

Project Compiler. CS031 TA Help Session November 28, 2011

[0569] p 0318 garbage

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

Intermediate Representations

CS5363 Final Review. cs5363 1

CS 4110 Programming Languages & Logics. Lecture 35 Typed Assembly Language

CMSC430 Spring 2014 Midterm 2 Solutions

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

Static Checking and Intermediate Code Generation Pat Morin COMP 3002

Thomas Polzer Institut für Technische Informatik

Intermediate Code Generation

Lecture08: Scope and Lexical Address

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

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.

Intermediate Code Generation

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

WYSE Academic Challenge Computer Science Test (State) 2015 Solution Set

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

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

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

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Java Identifiers, Data Types & Variables

CS 3360 Design and Implementation of Programming Languages. Exam 1

Intermediate Representations & Symbol Tables

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

LECTURE 3. Compiler Phases

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Tizen/Artik IoT Lecture Chapter 3. JerryScript Parser & VM

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

Control Instructions

Transcription:

Compiler construction in4303 lecture 8 Interpretation Simple code generation Chapter 4 4.2.4

Overview intermediate code program text front-end interpretation code generation code selection register allocation instruction ordering annotated AST intermediate code generation interpreter back-end assembly

Intermediate code language independent no structured types, only basic types (char, int, float) no structured control flow, only (un)conditional jumps linear format three-address code Java byte code z := x y

Interpretation recursive interpretation operates directly on the AST [attribute grammar] simple to write thorough error checks very slow: 1000x execution time of compiled code iterative interpretation operates on intermediate code good error checking slow: 100x

Recursive interpretation function per node type implement semantics visit children status indicator normal mode return mode (value) jump mode (label) exception mode (name)

Recursive interpretation PROCEDURE Elaborate if statement (If node): SET Result TO Evaluate condition (If node.condition); IF Status.mode /= Normal mode: RETURN; IF Result.type /= Boolean: ERROR "Condition in if-statement is not of type Boolean"; RETURN; IF Result.boolean.value = True: Elaborate statement (If node.then part); ELSE // Is there an else-part at all? IF If node.else part /= No node: Elaborate statement (If node.else part);

Self-identifying data must handle user-defined data types value = pointer to type descriptor + array of subvalues example: complex number re: 3.0 im: 4.0

Complex numbers value: v type: size: value: v v 2 type: value: type: value: 3.0 4.0 name: class: field: name: type: next: RECORD name: class: type number: complex_number name: type: next: im BASIC 3 re real

Iterative interpretation operates on threaded AST IF active node pointer (similar to PC) condition THEN ELSE flat loop over a case statement FI

WHILE Active node.type /= End of program type: SELECT Active node.type: CASE... CASE If type: // We arrive here after the condition has been evaluated; // the Boolean result is on the working stack. SET Value TO Pop working stack (); IF Value.boolean.value = True: SET Active node TO Active node.true successor; ELSE Value.boolean.value = False: IF Active node.false successor /= No node: SET Active node TO Active node.false successor; ELSE Active node.false successor = No node: SET Active node TO Active node.successor; CASE...

Iterative interpretation data structures implemented as arrays global data of the source program stack for storing local variables shadow memory to store properties status: (un)initialized data access: read-only / read-write data type: data / code pointer /...

Code generation tree rewriting replace nodes and subtrees of the AST by target code segments produce a linear sequence of instructions from the rewritten AST example code: p := p + 5 target: RISC machine program text front-end annotated AST back-end assembly

Register machine instructions Instruction Action Tree pattern Load_Const c, R n R n := c R n c Load_Mem Store_Mem x, R n R n, x R n := x x := R n R n x := x R n Add_Reg Sub_Reg R m, R n R m, R n R n := R n + R m R n := R n - R m R n + R n + Mul_Reg R m, R n R n := R n * R m R n R m R m R n

Tree rewriting for p := p + 5 := Store_Mem R 1, p p + Add_Reg R 2, R 1 p 5 Load_Mem p, R 2 Load_Const 5, R 1 linearize instructions: depth-first traversal Load_Mem p, R 2 Load_Const 5, R 1 Add_Reg R 2, R 1 Store_Mem R 1, p

Code generation main issues: code selection which template? register allocation too few! instruction ordering optimal code generation is NP-complete consider small parts of the AST simplify target machine use conventions

Simple code generation consider one AST node at a time two simplistic target machines pure register machine pure stack machine stack frame vars SP BP

Stack machine instructions Instruction Push_Const Push_Local Store_Local c i i Action SP++; stack[sp] = c; SP++; stack[sp] = stack[bp+i]; stack[bp+i] = stack[sp]; SP--; Add_Top2 Sub_Top2 Mul_Top2 stack[sp-1] = stack[sp-1] + stack[sp]; SP--; stack[sp-1] = stack[sp-1] - stack[sp]; SP--; stack[sp-1] = stack[sp-1] * stack[sp]; SP--;

Simple code generation for a stack machine example: b*b 4*a*c threaded AST - * * b b 4 * a c

Simple code generation for a stack machine example: b*b 4*a*c rewritten AST Sub_Top2 - Mul_Top2 * Mul_Top2 * Push_Local #b Push_Local #b Mul_Top2 Push_Const 4 Push_Local #a Push_Local #c Mul_Top2 Mul_Top2 Sub_Top2 Push_Local b #b Push_Local b #b Push_Const 4 4 Mul_Top2 * Push_Local a #a Push_Local c #c

Depth-first code generation for a stack machine PROCEDURE Generate code (Node): SELECT Node.type: CASE Constant type: Emit ("Push_Const" Node.value); CASE LocalVar type: Emit ("Push_Local" Node.number); CASE StoreLocal type: Emit ("Store_Local" Node.number); CASE Add type: Generate code (Node.left); Generate code (Node.right); Emit ("Add_Top2"); CASE Subtract type: Generate code (Node.left); Generate code (Node.right); Emit ("Sub_Top2"); CASE Multiply type:

Simple code generation for a register machine consider one AST node at a time similar to stack machine: depth-first register allocation each AST node leaves its result in a register specify target register when processing a subtree AND the set of free registers free registers: R target+1 R 32 Add_Reg R m, R n R n + R n R m

Depth-first code generation for a register machine PROCEDURE Generate code (Node, a register number Target): SELECT Node.type: CASE Constant type: Emit ("Load_Const " Node.value ",R" Target); CASE Variable type: Emit ("Load_Mem " Node.address ",R" Target); CASE... CASE Add type: Generate code (Node.left, Target); Generate code (Node.right, Target+1); Emit ("Add_Reg R" Target+1 ",R" Target); CASE...

Exercise (5 min.) generate code for the expression - b*b 4*a*c b * * b 4 * on a register machine with 32 registers numbered R1.. R32 a c

Answers

Weighted register allocation registers are scarce, depthfirst traversal is not optimal b - * * 2 registers b 4 3 registers evaluate heaviest subtree first a * c Load_Mem b, R1 Load_Mem b, R2 Mul_Reg R2, R1 Load_Const 4, R2 Load_Mem a, R3 Load_Mem c, R4 Mul_Reg R4, R3 Mul_Reg R3, R2 Sub_Reg R2, R1

Computing weights FUNCTION Weight of (Node) RETURNING an integer: SELECT Node.type: CASE Constant type: RETURN 1; CASE Variable type: RETURN 1; CASE... CASE Add type: SET Required left TO Weight of (Node.left); SET Required right TO Weight of (Node.right); IF Required left > Required right: RETURN Required left; IF Required left < Required right: RETURN Required right; // Required left = Required right RETURN Required left + 1; CASE...

Exercise (4 min.) determine the number of registers needed to evaluate the expression a + a*2 + a*(b+3)

Answers

Register spilling too few registers? spill registers in memory, to be retrieved later heuristic: select subtree that uses all registers, and replace it by a temporary example: b*b 4*a*c b 2 registers 1 tmp 2 1 1-32 * * b 4 1 2 * 2 a 1 c 1

Register spilling Load_Mem b, R1 Load_Mem b, R2 Mul_Reg R2, R1 Store_Mem R1, tmp Load_Mem a, R1 Load_Mem c, R2 Mul_Reg R2, R1 Load_Const 4, R2 Mul_Reg R1, R2 Load_Mem tmp, R1 Sub_Reg R2, R1 b 1 tmp 2 1 1-32 * * b 4 1 2 a 1 * 2 c 1