Next: TC-5 Samples with Variables, Previous: TC-5 Optimizing Cascading If, Up: TC-5 Samples [Contents][Index]
The game becomes more interesting with primitive calls (which are easier to compile than function definitions and function calls).
(print_int(101); print("\n"))
$ tc -H print-101.tig >print-101.hir
$ havm print-101.hir 101
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
$ 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
$ tc -H print-array.tig >print-array.hir
$ havm print-array.hir 42
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
Next: TC-5 Samples with Variables, Previous: TC-5 Optimizing Cascading If, Up: TC-5 Samples [Contents][Index]