Implementing bounds checking is quite simple: have the program die when the program accesses an invalid subscript in an array. For instance, the following code “succeeds” with a non-bounds-checking compiler.
let type int_array = array of int
var size := 2
var arr1 := int_array [size] of 0
var arr2 := int_array [size] of 0
var two := 2
var m_one := -1
in
arr1[two] := 3;
arr2[m_one] := -1;
print_int (arr1[1]);
print ("\n");
print_int (arr2[0]);
print ("\n")
end
File 93: bounds-violation.tig
$ tc -H bounds-violation.tig >bounds-violation.hir Example 94: tc -H bounds-violation.tig >bounds-violation.hir
$ havm bounds-violation.hir -1 3 Example 95: havm bounds-violation.hir
When run with --bounds-checking, your compiler produces code that diagnoses such cases, and exits with status 120. Something like:
error-->bounds-violation.tig:8.2-17: index out of arr1 bounds (0 .. 1): 2 =>120