Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.

Slides:



Advertisements
Similar presentations
Goal: Write Programs in Assembly
Advertisements

1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Intermediate Code Generation
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
1 CS 201 Compiler Construction Machine Code Generation.
Princess Sumaya Univ. Computer Engineering Dept. Chapter 2: IT Students.
8. Code Generation. Generate executable code for a target machine that is a faithful representation of the semantics of the source code Depends not only.
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
Intermediate Representations Saumya Debray Dept. of Computer Science The University of Arizona Tucson, AZ
Code Generation Steve Johnson. May 23, 2005Copyright (c) Stephen C. Johnson The Problem Given an expression tree and a machine architecture, generate.
8 Intermediate code generation
1 Compiler Construction Intermediate Code Generation.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Chapter 14: Building a Runnable Program Chapter 14: Building a runnable program 14.1 Back-End Compiler Structure 14.2 Intermediate Forms 14.3 Code.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
CS 536 Spring Code generation I Lecture 20.
Code Generation Simple Register Allocation Mooly Sagiv html:// Chapter
Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter
Arithmetic Expression Consider the expression arithmetic expression: (a – b) + ((c + d) + (e * f)) that can be represented as the following tree.
Tentative Schedule 20/12 Interpreter+ Code Generation 27/12 Code Generation for Control Flow 3/1 Activation Records 10/1 Program Analysis 17/1 Register.
Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4.
Chapter 8 . Sequence Control
Code Generation Mooly Sagiv html:// Chapter 4.
Prof. Fateman CS 164 Lecture 201 Virtual Machine Structure Lecture 20.
Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Improving Code Generation Honors Compilers April 16 th 2002.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Code Generation Mooly Sagiv html:// Chapter 4.
CSc 453 Interpreters & Interpretation Saumya Debray The University of Arizona Tucson.
Part II: Addressing Modes
MIPS coding. SPIM Some links can be found such as:
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
Programmer's view on Computer Architecture by Istvan Haller.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Chapter 7 Syntax-Directed Compilation (AST & Target Code) 1.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
Introduction to Code Generation and Intermediate Representations
Programming Languages
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
Operand Addressing And Instruction Representation Cs355-Chapter 6.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
More on MIPS programs n SPIM does not support everything supported by a general MIPS assembler. For example, –.end doesn’t work Use j $ra –.macro doesn’t.
Intermediate Code Representations
Code Generation CPSC 388 Ellen Walker Hiram College.
Code Generation How to produce intermediate or target code.
Code Generation for Control Flow Mooly Sagiv html:// Chapter
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building.
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
CS 404 Introduction to Compiler Design
Intermediate code Jakub Yaghob
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Chapter 9. Intermediate Languages
Intermediate Code Generation
Code Generation Part I Chapter 9
CSc 453 Interpreters & Interpretation
Code Generation Part I Chapter 8 (1st ed. Ch.9)
Code Generation Part I Chapter 9
CSc 453 Interpreters & Interpretation
Presentation transcript:

Intermediate code generation

Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract machine (e.g. the JVM) threaded code, or anything in between. Common representation of intermediate code depend on target machine: 0-address code for stack machines 2-address code for machines with memory-register operations 3-address code (quadruples) for RISC architectures In all cases, another top-down tree traversal

Intermediate code for if_statements if cond then quadruples for cond, result in t1 if not t1 goto else_label then_statements quadruples for then_statements else goto endif_label else_label: else_statements ; quadruples for else_statements; end if; endif_label: Generate labels Generate quadruples for some descendant node Place label in code May need label for then_statements, for short-circuit conditions

Intermediate code for elsif parts For each alternative, place in code the current else_label, and generate a new one. All alternatives inherit the end label from parent. if cond then s1 t1 = cond1 if not t1 goto else_label1 quadruples for s1 goto endif_label elsif cond2 then s2 else_label1: t2 = cond2 if not t2 goto else_label2 quadruples for s2 goto endif_label else s4 else_label2: quadruples for s4 end if; endif_label:

Code generation for while loops Generate two labels: start_loop, end_loop while (cond) { start_loop: if (!cond) goto end_loop s1; quadruples for s1 if (cond2) break; if (cond2) goto end_loop s2; quadruples for s2 if (cond3) continue; if (cond3) goto start_loop: s3; quadruples for s3 }; goto start_loop end_loop:

Intermediate code for numeric loops Semantics: loop not executed if range is null, so must test before first pass. for J in expr1..expr2 loop J = expr1 start_label: if J > expr2 goto end_label S1 quadruples for S1 end loop; J = J +1 goto start_label end_label:

Place test at end to utilize loop instruction for K in expr1.. Expr2 loop t1 = expr1 t2 = expr2 K = t1 - 1 goto test_label start_label: S1; quadruples for S1 end loop; test_label: K = K + 1 if K > t2 goto end_label goto start_label: end_label:

Intermediate code for short-circuit expressions Short-circuit expressions are treated as control structures if B1 or else B2 then S1… -- if (B1 || B2) { S1..  if B1 goto then_label  if not B2 goto else_label  then_label:  Quadruples for S1  else_label: Inherit target labels from enclosing control structure Generate additional labels for composite short-circuits

Intermediate code for case statements If range is small and most cases are defined, create jump table as array of code addresses, and generate indirect jump. table label1, label2 … case x is jumpi x table when up: y := 0; label1: y = 0 goto end_case when down : y := 1; label2: y = 1 goto end_case end case ; end_case:

Code generation for expressions: stack machines Zero-address instructions: push, pop, arithmetic Binary operations remove two entries, push one. d = b 2 – 4 a c load b -- load from memory dupl -- duplicate value on top of stack mult push a push c mult push_const 4 -- push constant on stack mult sub store d

Code generation for expressions on stack machines To evaluate a variable: load its value To evaluate a constant: push its literal value To evaluate an expression Evaluate left operand Evaluate right operand Apply operator

Quadruples for expressions Create new temporaries for each intermediate result: infinite number of registers Better model: assume finite number of registers Select one register to hold result Compute first operand into reserved register R1 Compute second operand using remaining registers Compute result into R1 To minimize number of registers needed, compute larger expression first. Simple implementation: use stack for available registers.

Computing the minimum number of registers (Aho-Sethi) For a constant: return 1 For a variable: return 1 For a tree: min1 = minimum for left_operand min2 = minimum for right_operand if min1 /= min2 then return max (min1, min2) else return min1 + 1 Optimal register use: Compute weight of each node At each step, compute subtree with larger weight

example b 2 – 4 a c needs 3 registers. Naïve left-to right optimal load b, R1 load b, R1 load b, R2 load b, R2 mul R1, R2, R1 mul R1, R2, R1 load 4, R2 load a, R2 load a, R3 load c, R3 load c, R4 mul R2, R3, R2 mul R3, R4, R3 load 4, R3 mul R2, R3, R2 mul R2, R3, R2 sub R1, R2 sub R1, R2

Code generation for more complex constructs Tree transformations exponentiation Inline expansion dispatching calls Calls to run-time routines storage management 64-bit arithmetic threads and tasks calendar, time,

Exponentiation Simple cases are computed efficiently: Y := x ** 2; x = x * x Y := x ** 4; T1 = x * x x = x * x General case requires run-time support: Y := x ** n; Y = exp_float (x, n) exp_float is part of runtime library linked with user program