COMP3290 Workshop 9 - Three Address Code

Document Sample
COMP3290 Workshop 9 - Three Address Code Powered By Docstoc
					                                Outline
           What is Three Address Code?
                       Use in Compilers
                             Generation




                COMP3290
      Workshop 9 - Three Address Code

     Joshua M. Clulow (jmc@newcastle.edu.au)


                          23 September 2008




Joshua M. Clulow (jmc@newcastle.edu.au)   COMP3290 - Three Address Code
                                          Outline
                     What is Three Address Code?
                                 Use in Compilers
                                       Generation


Outline




      What is Three Address Code?
      What makes it particularly useful as an intermediate
      representation in many compilers?
      Sketch a method for generating Three Address Code from
      simple expressions.




          Joshua M. Clulow (jmc@newcastle.edu.au)   COMP3290 - Three Address Code
                                       Outline
                  What is Three Address Code?    Description
                              Use in Compilers   Example Statements
                                    Generation


What is Three Address Code?



     A simple instruction set with instructions of the form:
     R := A op B
     All instructions are 4-tuples with attributes:
           Operator
           Operand A
           Operand B
           Result
     Not all instruction types will use all 4 attributes




       Joshua M. Clulow (jmc@newcastle.edu.au)   COMP3290 - Three Address Code
                                       Outline
                  What is Three Address Code?    Description
                              Use in Compilers   Example Statements
                                    Generation


Example TAC Statements


     Three Address Coding is a technique, not a specific
     instruction set
     Some sample instructions and their associated 4-tuples:


              Three Address Code                                           4-Tuple
      Statement             Type                                   Op       A    B    Res
      t2 := x + 5       Computation                                +         x    5   t2
      t9 := a / c       Computation                                 /        a    c   t9
      y := t1               Copy                                  copy      t1         y
      goto Lbl3      Unconditional Branch                         goto     Lbl3



       Joshua M. Clulow (jmc@newcastle.edu.au)   COMP3290 - Three Address Code
                                        Outline
                   What is Three Address Code?
                               Use in Compilers
                                     Generation


An Intermediate Representation (IR) for Compilers



       Front-end                    Three Address Code                     Back-end
     (input language)                                                (target architecture)



      Decouples input language processing from code generation
      Ignores machine-specific issues like register availability
      Compiler more easily retargetable
      Enables some machine-independent code optimisation
      Machine-specific code generation is simpler


        Joshua M. Clulow (jmc@newcastle.edu.au)   COMP3290 - Three Address Code
                                        Outline
                   What is Three Address Code?    The Process
                               Use in Compilers   An Example
                                     Generation


Generation - The Process

  An expression tree can easily be converted into a
  Three Address Code representation:
      Depth-first traversal of syntax tree nodes for expression
            Ensures operations are expressed prior to their dependants
      Assign each expression node a temporary output ‘variable’
      eg. t3
      Append to the output a TAC 4-tuple for each node:
            Operator reflects the node’s operation
            Operands are literals, symbols or temporary variables from
            child nodes
            Result is the temporary variable created for this node



        Joshua M. Clulow (jmc@newcastle.edu.au)   COMP3290 - Three Address Code
                                         Outline
                    What is Three Address Code?    The Process
                                Use in Compilers   An Example
                                      Generation


Generation - An Example

  Take, for example, the expression:

                          y = 2 / b + (a + 12) * x;

  This expression has the syntax tree:

                                      assign
                            y                      +
                                        /                    *

                                  2            b       +           x
                                                   a        12


         Joshua M. Clulow (jmc@newcastle.edu.au)   COMP3290 - Three Address Code
                                         Outline
                    What is Three Address Code?    The Process
                                Use in Compilers   An Example
                                      Generation


Generation - An Example


  The conversion process would produce the following output:


Syntax Tree:                                       Three Address Code:

                                                   t1 := 2 / b


          /
     2          b




         Joshua M. Clulow (jmc@newcastle.edu.au)   COMP3290 - Three Address Code
                                         Outline
                    What is Three Address Code?    The Process
                                Use in Compilers   An Example
                                      Generation


Generation - An Example


  The conversion process would produce the following output:


Syntax Tree:                                       Three Address Code:

                                                   t1 := 2 / b
                                                   t2 := a + 12
          /
     2          b             +

                        a           12


         Joshua M. Clulow (jmc@newcastle.edu.au)   COMP3290 - Three Address Code
                                         Outline
                    What is Three Address Code?    The Process
                                Use in Compilers   An Example
                                      Generation


Generation - An Example


  The conversion process would produce the following output:


Syntax Tree:                                       Three Address Code:

                                                   t1 := 2 / b
                                                   t2 := a + 12
                                                   t3 := t2 * x
          /                           *
     2          b             +               x

                        a           12


         Joshua M. Clulow (jmc@newcastle.edu.au)   COMP3290 - Three Address Code
                                         Outline
                    What is Three Address Code?    The Process
                                Use in Compilers   An Example
                                      Generation


Generation - An Example


  The conversion process would produce the following output:


Syntax Tree:                                       Three Address Code:

                                                   t1    :=      2 / b
                       +                           t2    :=      a + 12
                                                   t3    :=      t2 * x
          /                           *
                                                   t4    :=      t3 + t1
     2          b             +               x

                        a           12


         Joshua M. Clulow (jmc@newcastle.edu.au)   COMP3290 - Three Address Code
                                              Outline
                         What is Three Address Code?    The Process
                                     Use in Compilers   An Example
                                           Generation


Generation - An Example


     The conversion process would produce the following output:


Syntax Tree:                                            Three Address Code:
            assign
                                                        t1 := 2 / b
 y                          +                           t2 := a + 12
                                                        t3 := t2 * x
              /                            *
                                                        t4 := t3 + t1
        2            b             +               x    y := t4

                             a          12


             Joshua M. Clulow (jmc@newcastle.edu.au)    COMP3290 - Three Address Code
                                       Outline
                  What is Three Address Code?
                              Use in Compilers
                                    Generation


References

     Retargetable Compiler Code Generation,
     M. Ganapathi and C.N. Fischer and J.L. Hennessy.
     http://doi.acm.org/10.1145/356893.356897
     (1982)
     Algorithms for Compiler Design,
     O.G. Kakde.
     Cengage Charles River Media.
     (2002)
     http://en.wikipedia.org/wiki/Three_address_code
     (6 July 2008, at 15:36)
     http://compilers.iecc.com/comparch/article/00-09-124
     (Retrieved 20 September 2008)


       Joshua M. Clulow (jmc@newcastle.edu.au)   COMP3290 - Three Address Code
                                Outline
           What is Three Address Code?
                       Use in Compilers
                             Generation




                                      fin




Joshua M. Clulow (jmc@newcastle.edu.au)   COMP3290 - Three Address Code

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:104
posted:4/8/2010
language:English
pages:14
Description: COMP3290 Workshop 9 - Three Address Code