Upload
dustin-day
View
266
Download
5
Tags:
Embed Size (px)
Citation preview
Compiler1
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.
Compiler2
Structured variables: Arrays Records Strings Sets
E.g., A: Array[1..10] of Integer…A[I] := 5
Structured variables
Compiler3
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)
Compiler4
Structured variables (cont.)
Compiler5
Code generation for array reference
Compiler6
Code generation for array reference (cont.)
Compiler7
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
Compiler8
Elimination the common subexpression
Compiler9
Elimination the common subexpression (cont.)
Compiler10
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.
Compiler11
Elimination the loop invariants (cont.)
Compiler12
Elimination the loop invariants (cont.)
Compare the total number of quadruples operations among original codes and the code with loop invariant eliminated.
Compiler13
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]
Compiler14
Reduction in strength of an operation
Compiler15
Reduction in strength of an operation (cont.)
Note: Addition is faster than multiplication on the target machine.
Compiler16
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 allocationIt 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.
Compiler17
Recursive invocation of a procedure using
static storage allocation
Compiler18
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.
Compiler19
Recursive invocation of a procedure using automatic storage allocation
Compiler20
Recursive invocation of a procedure using automatic storage allocation (cont.)
Compiler21
Recursive invocation of a procedure using automatic storage allocation (cont.)
Compiler22
Recursive invocation of a procedure using automatic storage allocation (cont.)
Compiler23
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.
Compiler24
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)
Compiler25
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.
Compiler26
Nesting of blocks in a source program
Compiler27
Nesting of blocks in a source program (CONT.)
Compiler28
Use of display for above procedure
Compiler29
Use of display for above procedure (cont.)
Compiler30
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 * ZCase I : X, Y, Z are all IntegerCaseII: Some of them are Integer variables, others are Real.
Compiler31
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.
Compiler32
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).
Compiler33
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.
Compiler34
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.
Compiler35
Compiler Design Options – Compiler-compilers (cont.)
The advantages of using a compiler-compiler Ease of compiler construction. Ease of testing.