Presentation is loading. Please wait.

Presentation is loading. Please wait.

5.3 Machine-Independent Compiler Features

Similar presentations


Presentation on theme: "5.3 Machine-Independent Compiler Features"— Presentation transcript:

1 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

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

3 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

4 Structured variables (cont.)
Compiler

5 Code generation for array reference
Compiler

6 Code generation for array reference (cont.)
Compiler

7 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

8 Elimination the common subexpression
Compiler

9 Elimination the common subexpression (cont.)
Compiler

10 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

11 Elimination the loop invariants (cont.)
Compiler

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

13 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

14 Reduction in strength of an operation
Compiler

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

16 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

17 Recursive invocation of a procedure using static storage allocation
Compiler

18 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

19 Recursive invocation of a procedure using automatic storage allocation
Compiler

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

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

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

23 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

24 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

25 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

26 Nesting of blocks in a source program
Compiler

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

28 Use of display for above procedure
Compiler

29 Use of display for above procedure (cont.)
Compiler

30 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

31 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

32 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

33 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

34 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

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


Download ppt "5.3 Machine-Independent Compiler Features"

Similar presentations


Ads by Google