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
Symbol Table.
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
The University of Adelaide, School of Computer Science
Programming Languages and Paradigms
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
1 CS 201 Compiler Construction Machine Code Generation.
Intermediate Representation III. 2 PAs PA2 deadline is
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.
Code Generation Steve Johnson. May 23, 2005Copyright (c) Stephen C. Johnson The Problem Given an expression tree and a machine architecture, generate.
1 Compiler Construction Intermediate Code Generation.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
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.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
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.
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 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.
4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)
Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4.
CSc 453 Interpreters & Interpretation Saumya Debray The University of Arizona Tucson.
MIPS coding. SPIM Some links can be found such as:
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
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.
CSC 310 – Imperative Programming Languages, Spring, 2009 Virtual Machines and Threaded Intermediate Code (instead of PR Chapter 5 on Target Machine Architecture)
1 Introduction to JVM Based on material produced by Bill Venners.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Introduction to code generation.
ITEC 352 Lecture 12 ISA(3). Review Buses Memory ALU Registers Process of compiling.
Chapter 7 Syntax-Directed Compilation (AST & Target Code) 1.
Programming Languages and Paradigms Imperative Programming.
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.
Code Generation CPSC 388 Ellen Walker Hiram College.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
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.
RealTimeSystems Lab Jong-Koo, Lim
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.
CS 404 Introduction to Compiler Design
Mooly Sagiv html://
Intermediate code Jakub Yaghob
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler design Introduction to code generation
Intermediate Code Generation
CSc 453 Interpreters & Interpretation
Chapter 6 Intermediate-Code Generation
Compiler Design 21. Intermediate Code Generation
Compiler Design 21. Intermediate Code Generation
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 t1 = cond then_statements if not t1 goto else_label else quadruples for then_statements else_statements ; goto endif_label else_label: 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 aggregates exponentiation Inline expansion dispatching calls Calls to run-time routines storage management 64-bit arithmetic threads and tasks calendar, time,

Aggregates A : array (1..N, 1.. N) of integer; -- N non-static A := ( (1 => 0, others => 15), others => (others => -1)); Becomes: A (1, 1) := 0; for J in 2.. N loop A (1, J) := 15; for J in 2.. N for K in 1.. N loop A (J, K) := -1; end loop;

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

Dispatching calls class Thing { virtual void modify (int x) …} Thing* this_one ( ); … this_one => modify (42); Modify has an implicit parameter (this) Call depends on runtime class of designated object Each object has a pointer to vtabl. Each virtual method has a known offset in vtabl Call becomes: (this_one=>vtabl (7)) (this_one, 42); Multiple inheritance requires more elaborate data structure

Interface calls in Java Different classes can implement an interface method The method has the same name and signature, but a different position in the vtabl of each class Locating the method during dispatching must be done by sequential search and string comparison: runtime code Just-in-time compilation (JIT) can cache the result of such a search for subsequent use.