compiler generator by malj

VIEWS: 26 PAGES: 35

									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.




                                                 1   Compiler
Structured variables

 Structured variables:
    Arrays
    Records
    Strings
    Sets



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



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


                                                       3   Compiler
Structured variables (cont.)




                               4   Compiler
Code generation for array reference




                                      5   Compiler
Code generation for array reference (cont.)




                                              6   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




                                                 7   Compiler
Elimination the common subexpression




                                       8   Compiler
Elimination the common subexpression (cont.)




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




                                                10   Compiler
Elimination the loop invariants (cont.)




                                          11   Compiler
Elimination the loop invariants (cont.)

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




                                                12   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]


                                       13   Compiler
Reduction in strength of an operation




                                        14   Compiler
Reduction in strength of an operation (cont.)




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

                                                                   15   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.
                                                       16   Compiler
Recursive invocation of a procedure using
static storage allocation




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




                                                         18   Compiler
Recursive invocation of a procedure using
automatic storage allocation




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




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




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




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


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

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

                                                            25   Compiler
Nesting of blocks in a source program




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




                                           27   Compiler
Use of display for above procedure




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




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

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




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




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



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




                                                       34   Compiler
Compiler Design Options – Compiler-compilers (cont.)

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




                                                35   Compiler

								
To top