1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
default Order dec
$include <flow.sail>
$include <arith.sail>
$include <vector_dec.sail>
$include <string.sail>
$include <exception_basic.sail>
val highest_set_bit : forall ('n : Int), 'n >= 0. bits('n) -> int
overload ~ = {not_bool}
function highest_set_bit x = {
foreach (i from ('n - 1) to 0 by 1 in dec)
if x[i] == bitone then return(i) else ();
return -1
}
val throws : unit -> unit effect {escape}
function throws() = throw Exception()
register R : int
/*
* Modelling bug described in commit 45554f2893667d951e39c8049631a986c1683857
*/
val fetch_and_execute : unit -> bool effect {escape, wreg, rreg}
function fetch_and_execute() =
{
R = R + 1;
try {
if (R >= 3) then {
throws()
};
true
} catch {
Exception() => false
}
}
val main : unit -> unit effect {escape, wreg, rreg}
function main() =
{
R = 0;
/*
* Expect: "i = 1", "i = 2" and "i = 3"
*/
foreach (i from 1 to 3) {
print_int("i = ", i)
};
/*
* Expect: "i = 3", "i = 2" and "i = 1"
*/
foreach (i from 3 to 1 by 1 in dec) {
print_int("i = ", i)
};
assert(highest_set_bit(0b1) == 0);
assert(highest_set_bit(0b0010) == 1);
assert(highest_set_bit(0b10000) == 4);
assert(highest_set_bit(0x8000_0000_0000_0000) == 63);
assert(highest_set_bit(0x1_0000_0000_0000_0000) == 64);
/*
* Expect: "j = 1", "j = 2" and "j = 3"
*/
j : int = 0;
while j < 3 do {
j = j + 1;
print_int("j = ", j);
};
/*
* Expect: "k = 0x01", "k = 0x02" and "k = 0x03"
*/
k : bits(8) = 0x00;
while unsigned(k) < 3 do {
k = k + 1;
print_bits("k = ", k);
};
/*
* Expect: "Caught"
*/
try {
while true do {
throw Exception();
assert(false, "Unreachable")
}
} catch {
Exception() => print_endline("Caught")
};
/*
* Expect: "Looping"
*/
caught : bool = false;
while ~(caught) do {
try {
print_endline("Looping");
throw Exception();
assert(false, "Unreachable")
} catch {
Exception() => caught = true
}
};
/*
* Expect "Caught inner exception", "Caught outer exception"
*/
try {
try throw Exception()
catch {
Exception() => {
print_endline("Caught inner exception");
throw Exception()
}
}
} catch {
Exception() => print_endline("Caught outer exception")
};
/*
* Expect "Outer handler"
*/
try {
try throw Exception()
catch {
/* Exception in catch guard will throw to outer handler */
Exception() if { throw Exception(); false } => {
assert(false, "Unreachable");
throw Exception()
},
_ => assert(false, "Unreachable")
}
} catch {
Exception() => print_endline("Outer handler")
};
/*
* Expect "Outer handler"
*/
try {
try throws()
catch {
/* Exception in catch guard will throw to outer handler */
Exception() if { throws(); true } => {
assert(false, "Unreachable");
throws()
},
_ => assert(false, "Unreachable")
}
} catch {
Exception() => print_endline("Outer handler")
};
/*
* Expect "Once"
*/
repeat {
print_endline("Once")
} until true;
/*
* Expect "Once"
*/
once : bool = false;
repeat {
print_endline("Once");
try throw Exception() catch {
_ => once = true,
_ => once = false
}
} until once;
/*
* Expect "ok"
*/
if try true catch { _ => false } then {
print_endline("ok")
};
/*
* Expect "ok"
*/
if try throw Exception() catch { _ => true } then {
print_endline("ok")
};
/*
* Expect "R = 3"
*/
while fetch_and_execute() do ();
print_int("R = ", R);
}
|