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


4.14.2.1 TC-5 Primitive Samples

This example is probably the simplest Tiger program.

0

File 4.52: 0.tig

$ tc --hir-display 0.tig
/* == High Level Intermediate representation. == */
# Routine: _main
label main
# Prologue
# Body
seq
  sxp
    const 0
  sxp
    const 0
seq end
# Epilogue
label end

Example 4.69: tc --hir-display 0.tig

You should then probably try to make more difficult programs with literals only. Arithmetics is one of the easiest tasks.

1 + 2 * 3

File 4.53: arith.tig

$ tc -H arith.tig
/* == High Level Intermediate representation. == */
# Routine: _main
label main
# Prologue
# Body
seq
  sxp
    binop add
      const 1
      binop mul
        const 2
        const 3
  sxp
    const 0
seq end
# Epilogue
label end

Example 4.70: tc -H arith.tig

Use havm to exercise your output.

$ tc -H arith.tig >arith.hir

Example 4.71: tc -H arith.tig >arith.hir

$ havm arith.hir

Example 4.72: havm arith.hir

Unfortunately, without actually printing something, you won’t see the final result, which means you need to implement function calls. Fortunately, you can ask havm for a verbose execution:

$ havm --trace arith.hir
error→checkingLow
error→plaining
error→unparsing
error→checking
error→evaling
error→ call ( name main ) []
error→9.6-9.13:  const 1
error→11.8-11.15:  const 2
error→12.8-12.15:  const 3
error→10.6-12.15:  binop mul 2 3
error→8.4-12.15:  binop add 1 6
error→7.2-12.15:  sxp 7
error→14.4-14.11:  const 0
error→13.2-14.11:  sxp 0
error→ end call ( name main ) [] = 0

Example 4.73: havm --trace arith.hir

If you look carefully, you will find an ‘sxp 7’ in there...


Then you are encouraged to implement control structures.

if 101 then 102 else 103

File 4.54: if-101.tig

$ tc -H if-101.tig
/* == High Level Intermediate representation. == */
# Routine: _main
label main
# Prologue
# Body
seq
  seq
    cjump ne
      const 101
      const 0
      name l0
      name l1
    label l0
    sxp
      const 102
    jump
      name l2
    label l1
    sxp
      const 103
    label l2
  seq end
  sxp
    const 0
seq end
# Epilogue
label end

Example 4.74: tc -H if-101.tig

And even more difficult control structure uses:

while 101
  do (if 102 then break)

File 4.55: while-101.tig

$ tc -H while-101.tig
/* == High Level Intermediate representation. == */
# Routine: _main
label main
# Prologue
# Body
seq
  seq
    label l1
    cjump ne
      const 101
      const 0
      name l2
      name l0
    label l2
    seq
      cjump ne
        const 102
        const 0
        name l3
        name l4
      label l3
      jump
        name l0
      jump
        name l5
      label l4
      sxp
        const 0
      label l5
    seq end
    jump
      name l1
    label l0
  seq end
  sxp
    const 0
seq end
# Epilogue
label end

Example 4.75: tc -H while-101.tig

Beware that HAVM has some known bugs with its handling of break, see HAVM Bugs.


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