5.3 Machine-Independent Compiler Features

Slides:



Advertisements
Similar presentations
Symbol Table.
Advertisements

Intermediate Code Generation
Course Outline Traditional Static Program Analysis Software Testing
1 Chapter 8: Code Generation. 2 Generating Instructions from Three-address Code Example: D = (A*B)+C =* A B T1 =+ T1 C T2 = T2 D.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
The Assembly Language Level
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
CS 536 Spring Run-time organization Lecture 19.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
ISBN Chapter 10 Implementing Subprograms.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Source language issues Storage organization
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
Run-time Environment and Program Organization
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
Improving Code Generation Honors Compilers April 16 th 2002.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
System Software Chih-Shun Hsu
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Compilers -- Basic functions
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
COP4020 Programming Languages
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
JIT in webkit. What’s JIT See time_compilation for more info. time_compilation.
Runtime Environments Compiler Construction Chapter 7.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Compiler Construction
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Chapter 7 Runtime Environments. Relationships between names and data objects As execution proceeds, the same name can denote different data objects Procedures,
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
Basic Semantics Associating meaning with language entities.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
ISBN Chapter 10 Implementing Subprograms.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
COMP3190: Principle of Programming Languages
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
CSC 8505 Compiler Construction Runtime Environments.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
ISBN Chapter 10 Implementing Subprograms.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Compiler Summary Lexical analysis—scanner – String of characters  token – Finite automata. Syntactical analysis – parser – Sequence of tokens –> language.
Code Optimization Overview and Examples
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Implementing Subprograms
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compilers.
Run-time organization
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
UNIT V Run Time Environments.
Intermediate Code Generation
Review: What is an activation record?
Code Optimization.
Presentation transcript:

5.3 Machine-Independent Compiler Features Methods for handling structured variables such as array. Code optimization. Storage allocation for the compiled program. Compiling a block-structured language. Compiler

Structured variables Structured variables: Arrays Records Strings Sets E.g., A: Array[1..10] of Integer … A[I] := 5 Compiler

Structured variables (cont.) One-dimension array declaration A : Array[1..10] of Integer A : Array[l..u] of Integer  u-l+1 words of storage Two-dimension array declaration B : Array[0..3, 1..6] of Integer B : Array [l1..u1, l2..u2] of Integer  the number of words to be allocated is given by (u1-l1+1)*(u2-l2+1) Compiler

Structured variables (cont.) Compiler

Code generation for array reference Compiler

Code generation for array reference (cont.) Compiler

Machine-Independent Code Optimization Concept illustrated by quadruple verbal description. - I #1 i1 Code optimization techniques Elimination the common subexpressions Elimination the loop invariants Reduction in strength Compiler

Elimination the common subexpression Compiler

Elimination the common subexpression (cont.) Compiler

Elimination the loop invariants Subexpressions within a loop whose values do not change from one iteration of the loop to the next. Thus, their values can be computed once, before the loop is entered, rather than being recalculated for each iteration. E.g., 2*J It can be detect by analyzing the flow graph. Compiler

Elimination the loop invariants (cont.) Compiler

Elimination the loop invariants (cont.) Compare the total number of quadruples operations among original codes and the code with loop invariant eliminated. Compiler

Another optimization techniques Rewriting the source program. E.g., For I := 1 To 10 Do X[I , 2*J-1] := Y[I , 2*J]  T1 := 2*J; T2 := T1 – 1; For I := 1 To 10 Do X[I , T2] := Y[I , T1] Compiler

Reduction in strength of an operation Compiler

Reduction in strength of an operation (cont.) Note: Addition is faster than multiplication on the target machine. Compiler

Storage Allocation Static allocation vs. dynamic allocation Static allocation Temporary variables, including the one used to save the return address, were also assigned fixed addresses within the program. This type of storage assignment is called static allocation. Dynamic allocation It is necessary to preserve the previous values of any variables used by subroutine, including parameters, temporaries, return addresses, register save areas, etc. It can be accomplished with a dynamic storage allocation technique. Compiler

Recursive invocation of a procedure using static storage allocation Compiler

Storage Allocation (cont.) The dynamic storage allocation technique. Each procedure call creates an activation record that contains storage for all the variables used by the procedure. Activation records are typically allocated on a stack. Compiler

Recursive invocation of a procedure using automatic storage allocation Compiler

Recursive invocation of a procedure using automatic storage allocation (cont.) Compiler

Recursive invocation of a procedure using automatic storage allocation (cont.) Compiler

Recursive invocation of a procedure using automatic storage allocation (cont.) Compiler

Storage Allocation (cont.) The compiler must generate additional code to manage the activation records themselves. Prologue At the beginning of each procedure there must be code to create a new activation record, linking it to the previous one and setting the appropriate pointers. Epilogue At the end of the procedure, there must be code to delete the current activation record, resetting pointers as needed. Compiler

Storage Allocation (cont.) Other types of dynamic storage allocation. E.g., FORTRAN 90, ALLOCATE ( Matrix( Rows, Columns )) DEALLOCATE ( Matrix ) Pascal, NEW(P) DISPOSE(P) C, MALLOC(size) FREE(P) Compiler

Block-Structured Languages A block is a portion of a program that has the ability to declare its own identifiers. E.g., procedure Blocks may be nested within other blocks. When a reference to an identifier appears in the source program, the compiler must first check the symbol table for a definition of that identifier by the current block. If no such definition is found, the compiler looks for a surrounds that, and so on. Compiler

Nesting of blocks in a source program Compiler

Nesting of blocks in a source program (CONT.) Compiler

Use of display for above procedure Compiler

Use of display for above procedure (cont.) Compiler

Compiler Design Options – division into passes Multi-pass compiler Code optimization forward reference problem (required multi-pass) In Pascal, declarations of variables must appear in the program before the statements that use these variables. In FORTRAN, declarations may appear at the beginning of the program; any variable that is not declared is assigned characteristic by default. For example, X := Y * Z Case I : X, Y, Z are all Integer CaseII: Some of them are Integer variables, others are Real. Compiler

Compiler Design Options – division into passes (cont.) Single-pass vs. Multi-pass compiler Speed of compilation is the major concerned  one-pass Speed of execution is the major concerned  multi-pass The advantage of multi-pass compiler Could incorporate sophisticated code-optimization techniques. Shorten the overall time required for compiler construction. Consumption limited system resources. Compiler

Compiler Design Options – P-code compiler P-code compiler (also called bytecode compiler) The source program is analyzed and converted into an intermediate form, which is a machine language for a hypothetical computer (pseudo-machine or p-machine). Compiler

Compiler Design Options – P-code compiler (cont.) The advantages of P-code compiler Portability of software It is not necessary for the compiler to generate different code for different computers. The source version of the compiler is compiled into p-code; this p-code can then be interpreted on another computer. Easy to write a new compiler for each different machine. The p-code object program is much smaller than a corresponding machine code program. Compiler

Compiler Design Options – Compiler-compilers A compiler-compiler is a software tool that can be used to help in the task of compiler construction. Also called compiler generator or translator-writing system. Compiler

Compiler Design Options – Compiler-compilers (cont.) The advantages of using a compiler-compiler Ease of compiler construction. Ease of testing. Compiler