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-101.tig
(print_int(101); print("\n"))
tc -H print-101.tig > print-101.hir
$ tc -H print-101.tig > print-101.hir

$ echo $?
0
havm print-101.hir
$ havm print-101.hir
101
$ echo $?
0

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

print-array.tig
let
  type ints = array of int
  var  ints := ints [51] of 42
in
  print_int(ints[ints[0]]); print("\n")
end
tc -H 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
$ echo $?
0
tc -H print-array.tig > print-array.hir
$ tc -H print-array.tig > print-array.hir

$ echo $?
0
havm print-array.hir
$ havm print-array.hir
42
$ echo $?
0

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

print-record.tig
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
tc -H print-record.tig
$ tc -H print-record.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 sub
    temp sp
    const 4
# Body
seq
  seq
    move
      mem
        temp fp
      eseq
      seq
        move
          temp t1
          call
            name malloc
            const 8
          call end
        move
          mem
            temp t1
          const 1
        move
          mem
            binop add
              temp t1
              const 4
          eseq
          seq
            move
              temp t0
              call
                name malloc
                const 8
              call end
            move
              mem
                temp t0
              const 2
            move
              mem
                binop add
                  temp t0
                  const 4
              const 0
          seq end
            temp t0
      seq end
        temp t1
    seq
      sxp
        call
          name print_int
          mem
            mem
              binop add
                mem
                  temp fp
                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 t2
label end
$ echo $?
0
tc -H print-record.tig > print-record.hir
$ tc -H print-record.tig > print-record.hir

$ echo $?
0
havm print-record.hir
$ havm print-record.hir
2
$ echo $?
0