35
Compiler 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.

Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Embed Size (px)

Citation preview

Page 1: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

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.

Page 2: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler2

Structured variables: Arrays Records Strings Sets

E.g., A: Array[1..10] of Integer…A[I] := 5

Structured variables

Page 3: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

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)

Page 4: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler4

Structured variables (cont.)

Page 5: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler5

Code generation for array reference

Page 6: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler6

Code generation for array reference (cont.)

Page 7: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

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

Page 8: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler8

Elimination the common subexpression

Page 9: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler9

Elimination the common subexpression (cont.)

Page 10: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

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.

Page 11: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler11

Elimination the loop invariants (cont.)

Page 12: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler12

Elimination the loop invariants (cont.)

Compare the total number of quadruples operations among original codes and the code with loop invariant eliminated.

Page 13: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

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]

Page 14: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler14

Reduction in strength of an operation

Page 15: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler15

Reduction in strength of an operation (cont.)

Note: Addition is faster than multiplication on the target machine.

Page 16: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

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.

Page 17: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler17

Recursive invocation of a procedure using

static storage allocation

Page 18: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r 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.

Page 19: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler19

Recursive invocation of a procedure using automatic storage allocation

Page 20: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler20

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

Page 21: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler21

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

Page 22: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler22

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

Page 23: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

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.

Page 24: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

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)

Page 25: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

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.

Page 26: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler26

Nesting of blocks in a source program

Page 27: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler27

Nesting of blocks in a source program (CONT.)

Page 28: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler28

Use of display for above procedure

Page 29: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler29

Use of display for above procedure (cont.)

Page 30: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

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.

Page 31: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

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.

Page 32: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

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).

Page 33: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

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.

Page 34: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

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.

Page 35: Compiler1 5.3 Machine-Independent Compiler Features r Methods for handling structured variables such as array. r Code optimization. r Storage allocation

Compiler35

Compiler Design Options – Compiler-compilers (cont.)

The advantages of using a compiler-compiler Ease of compiler construction. Ease of testing.