Noeud « Next »: , Noeud « Previous »: T5 Optimizing Cascading If, Noeud « Up »: T5 Samples



4.7.2.3 T5 Builtin Calls Samples

But the game becomes more interesting when you implement function calls (which is easier than compiling functions). print_int is probably the first builtin to implement:

     (print_int (101); print ("\n"))
     File 76: print-101.tig
     $ tc -H print-101.tig >print-101.hir
     Example 77: tc -H print-101.tig >print-101.hir
     $ havm print-101.hir
     101
     Example 78: havm print-101.hir

Complex values, arrays and records, also need calls to the runtime system:

     let type list = { h: int, t: list }
         var list := list { h = 1, t = list { h = 2, t = nil } }
     in
       print_int (list.t.h); print ("\n")
     end
     File 79: print-list.tig
     $ tc -H print-list.tig
     /* == High Level Intermediate representation. == */

     label l0
             "\n"
     # Routine: Main
     label Main
     # Prologue
     move
         temp t2
         temp fp
     move
         temp fp
         temp sp
     move
         temp sp
         binop (-)
             temp sp
             const 4
     # Body
     seq
         move
             mem
                 temp $fp
             eseq
             seq
                 move
                     temp t1
                     call
                         name malloc
                         const 8
                     call end
                 move
                     mem
                         binop (+)
                             temp t1
                             const 0
                     const 1
                 move
                     mem
                         binop (+)
                             temp t1
                             const 4
                     eseq
                     seq
                         move
                             temp t0
                             call
                                 name malloc
                                 const 8
                             call end
                         move
                             mem
                                 binop (+)
                                     temp t0
                                     const 0
                             const 2
                         move
                             mem
                                 binop (+)
                                     temp t0
                                     const 4
                             const 0
                     seq end
                         temp t0
             seq end
                 temp t1
         seq
             sxp
                 call
                     name print_int
                     mem
                         binop (+)
                             mem
                                 binop (+)
                                     mem
                                         temp $fp
                                     const 4
                             const 0
                 call end
             sxp
                 call
                     name print
                     name l0
                 call end
         seq end
     seq end
     # Epilogue
     move
         temp sp
         temp fp
     move
         temp fp
         temp t2
     label end
     Example 80: tc -H print-list.tig
     $ tc -H print-list.tig >print-list.hir
     Example 81: tc -H print-list.tig >print-list.hir
     $ havm print-list.hir
     2
     Example 82: havm print-list.hir