Next: , Previous: , Up: TC-5 Samples   [Contents][Index]


4.14.2.3 TC-5 Builtin Calls Samples

The game becomes more interesting with primitive calls (which are easier to compile than function definitions and function calls).

(print_int(101); print("\n"))

File 4.57: print-101.tig

$ tc -H print-101.tig >print-101.hir

Example 4.82: tc -H print-101.tig >print-101.hir

$ havm print-101.hir
101

Example 4.83: havm print-101.hir

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

let
  type ints = array of int
  var  ints := ints [51] of 42
in
  print_int(ints[ints[0]]); print("\n")
end

File 4.58: print-array.tig

$ tc -H print-array.tig
/* == High Level Intermediate representation. == */
label l0
        "\n"
# Routine: _main
label main
# Prologue
move
  temp t1
  temp fp
move
  temp fp
  temp sp
move
  temp sp
  binop sub
    temp sp
    const 4
# Body
seq
  seq
    move
      mem
        temp fp
      eseq
        move
          temp t0
          call
            name init_array
            const 51
            const 42
          call end
        temp t0
    seq
      sxp
        call
          name print_int
          mem
            binop add
              mem
                temp fp
              binop mul
                mem
                  binop add
                    mem
                      temp fp
                    binop mul
                      const 0
                      const 4
                const 4
        call end
      sxp
        call
          name print
          name l0
        call end
    seq end
  seq end
  sxp
    const 0
seq end
# Epilogue
move
  temp sp
  temp fp
move
  temp fp
  temp t1
label end

Example 4.84: tc -H print-array.tig

$ tc -H print-array.tig >print-array.hir

Example 4.85: tc -H print-array.tig >print-array.hir

$ havm print-array.hir
42

Example 4.86: havm print-array.hir

The case of record is more subtle. Think carefully about the following example

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 4.59: print-record.tig


Next: , Previous: , Up: TC-5 Samples   [Contents][Index]