Next: TC-5 Optimizing Cascading If, Up: TC-5 Samples [Contents][Index]
This example is probably the simplest Tiger program.
0
$ 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
You should then probably try to make more difficult programs with literals only. Arithmetics is one of the easiest tasks.
1 + 2 * 3
$ 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
Use havm to exercise your output.
$ tc -H arith.tig >arith.hir
$ 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
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
$ 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
And even more difficult control structure uses:
while 101 do (if 102 then break)
$ 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
Beware that HAVM has some known bugs with its handling of
break, see HAVM Bugs.
Next: TC-5 Optimizing Cascading If, Up: TC-5 Samples [Contents][Index]