Noeud « Previous »: T5 Builtin Calls Samples, Noeud « Up »: T5 Samples



4.7.2.4 T5 Samples with Variables

Here is an example which demonstrates the usefulness of information about escapes: when escaping variables are not computed, they are all stored on the stack:

     let var a := 1
         var b := 2
         var c := 3
     in
       a := 2;
       c := a + b + c;
       print_int (c);
       print ("\n")
     end
     File 83: vars.tig
     $ tc -H vars.tig
     /* == High Level Intermediate representation. == */

     label l0
             "\n"
     # Routine: Main
     label Main
     # Prologue
     move
         temp t0
         temp fp
     move
         temp fp
         temp sp
     move
         temp sp
         binop (-)
             temp sp
             const 12
     # Body
     seq
         move
             mem
                 temp $fp
             const 1
         seq
             move
                 mem
                     binop (+)
                         temp $fp
                         const -4
                 const 2
             seq
                 move
                     mem
                         binop (+)
                             temp $fp
                             const -8
                     const 3
                 seq
                     move
                         mem
                             temp $fp
                         const 2
                     move
                         mem
                             binop (+)
                                 temp $fp
                                 const -8
                         binop (+)
                             binop (+)
                                 mem
                                     temp $fp
                                 mem
                                     binop (+)
                                         temp $fp
                                         const -4
                             mem
                                 binop (+)
                                     temp $fp
                                     const -8
                     sxp
                         call
                             name print_int
                             mem
                                 binop (+)
                                     temp $fp
                                     const -8
                         call end
                     sxp
                         call
                             name print
                             name l0
                         call end
                 seq end
             seq end
         seq end
     seq end
     # Epilogue
     move
         temp sp
         temp fp
     move
         temp fp
         temp t0
     label end
     Example 84: tc -H vars.tig

But once escaping variable computation implemented, we know none escape in this example, hence they can be stored in temporaries:

     $ tc -eH vars.tig
     /* == High Level Intermediate representation. == */

     label l0
             "\n"
     # Routine: Main
     label Main
     # Prologue
     # Body
     seq
         move
             temp t0
             const 1
         seq
             move
                 temp t1
                 const 2
             seq
                 move
                     temp t2
                     const 3
                 seq
                     move
                         temp t0
                         const 2
                     move
                         temp t2
                         binop (+)
                             binop (+)
                                 temp t0
                                 temp t1
                             temp t2
                     sxp
                         call
                             name print_int
                             temp t2
                         call end
                     sxp
                         call
                             name print
                             name l0
                         call end
                 seq end
             seq end
         seq end
     seq end
     # Epilogue
     label end
     Example 85: tc -eH vars.tig
     $ tc -eH vars.tig >vars.hir
     Example 86: tc -eH vars.tig >vars.hir
     $ havm vars.hir
     7
     Example 87: havm vars.hir

Then, you should implement the declaration of functions:

     let function fact (i: int) : int =
         if i = 0 then 1
                  else i * fact (i - 1)
     in
       print_int (fact (15));
       print ("\n")
     end
     File 88: fact15.tig
     $ tc -H fact15.tig
     /* == High Level Intermediate representation. == */
     # Routine: fact
     label l0
     # Prologue
     move
         temp t1
         temp fp
     move
         temp fp
         temp sp
     move
         temp sp
         binop (-)
             temp sp
             const 8
     move
         mem
             temp $fp
         temp i0
     move
         mem
             binop (+)
                 temp $fp
                 const -4
         temp i1
     # Body
     move
         temp $v0
         eseq
         seq
             cjump eq
                 mem
                     binop (+)
                         temp $fp
                         const -4
                 const 0
                 name l1
                 name l2
             label l1
             move
                 temp t0
                 const 1
             jump
                 name l3
             label l2
             move
                 temp t0
                 binop (*)
                     mem
                         binop (+)
                             temp $fp
                             const -4
                     call
                         name l0
                         mem
                             temp $fp
                         binop (-)
                             mem
                                 binop (+)
                                     temp $fp
                                     const -4
                             const 1
                     call end
             label l3
         seq end
             temp t0
     # Epilogue
     move
         temp sp
         temp fp
     move
         temp fp
         temp t1
     label end


     label l4
             "\n"
     # Routine: Main
     label Main
     # Prologue
     # Body
     seq
         sxp
             call
                 name print_int
                 call
                     name l0
                     temp $fp
                     const 15
                 call end
             call end
         sxp
             call
                 name print
                 name l4
             call end
     seq end
     # Epilogue
     label end
     Example 89: tc -H fact15.tig
     $ tc -H fact15.tig >fact15.hir
     Example 90: tc -H fact15.tig >fact15.hir
     $ havm fact15.hir
     2004310016
     Example 91: havm fact15.hir

And finally, you should support escaping variables. Voir Example 43.

     $ tc -eH variable-escapes.tig
     /* == High Level Intermediate representation. == */

     label l0
             "I rule the world!\n"

     label l1
             "Peace on Earth for humans of good will.\n"
     # Routine: print_slogan
     label l2
     # Prologue
     move
         temp t2
         temp fp
     move
         temp fp
         temp sp
     move
         temp sp
         binop (-)
             temp sp
             const 4
     move
         mem
             temp $fp
         temp i0
     move
         temp t1
         temp i1
     # Body
     seq
         sxp
             call
                 name print
                 temp t1
             call end
         sxp
             call
                 name print
                 mem
                     mem
                         temp $fp
             call end
     seq end
     # Epilogue
     move
         temp sp
         temp fp
     move
         temp fp
         temp t2
     label end

     # Routine: Main
     label Main
     # Prologue
     move
         temp t3
         temp fp
     move
         temp fp
         temp sp
     move
         temp sp
         binop (-)
             temp sp
             const 4
     # Body
     seq
         move
             mem
                 temp $fp
             name l0
         seq
             move
                 temp t0
                 name l1
             sxp
                 call
                     name l2
                     temp $fp
                     temp t0
                 call end
         seq end
     seq end
     # Epilogue
     move
         temp sp
         temp fp
     move
         temp fp
         temp t3
     label end
     Example 92: tc -eH variable-escapes.tig