|
| 1 | +/* |
| 2 | + * Test direct br / br_if optimisation (Phase B2). |
| 3 | + * |
| 4 | + * Exercises: |
| 5 | + * - JMP forward (becomes br <depth>) |
| 6 | + * - JMP_CMP both-forward (becomes br_if <depth_taken> / br <depth_not_taken>) |
| 7 | + * - JMP_CMP forward-taken, backward-not-taken (br_if <depth>; fallback dispatch) |
| 8 | + * - JMP backward (must still go through loop dispatch) |
| 9 | + * - Deeply nested forward jumps (depth > 1) |
| 10 | + * - Single-block functions (halt depth = 0) |
| 11 | + */ |
| 12 | + |
| 13 | +/* ---- helpers ----------------------------------------------------------- */ |
| 14 | +static int g_acc; /* global accumulator */ |
| 15 | + |
| 16 | +/* Single-block function: only RET, no jumps */ |
| 17 | +static int identity(int x) { return x; } |
| 18 | + |
| 19 | +/* Backward jump: simple loop (while) */ |
| 20 | +static int sum_to(int n) |
| 21 | +{ |
| 22 | + int s = 0; |
| 23 | + while (n > 0) { s += n; n--; } |
| 24 | + return s; |
| 25 | +} |
| 26 | + |
| 27 | +/* Forward JMP_CMP: if / else */ |
| 28 | +static int abs_val(int x) |
| 29 | +{ |
| 30 | + if (x < 0) return -x; |
| 31 | + return x; |
| 32 | +} |
| 33 | + |
| 34 | +/* Deeply nested forward jumps: short-circuit || chain */ |
| 35 | +static int any_nonzero(int a, int b, int c, int d, int e) |
| 36 | +{ |
| 37 | + if (a != 0 || b != 0 || c != 0 || d != 0 || e != 0) |
| 38 | + return 1; |
| 39 | + return 0; |
| 40 | +} |
| 41 | + |
| 42 | +/* Mixed forward / backward: loop with early break */ |
| 43 | +static int first_ge(const int *arr, int n, int thresh) |
| 44 | +{ |
| 45 | + int i; |
| 46 | + for (i = 0; i < n; i++) { |
| 47 | + if (arr[i] >= thresh) |
| 48 | + return arr[i]; |
| 49 | + } |
| 50 | + return -1; |
| 51 | +} |
| 52 | + |
| 53 | +/* Cascaded if/else-if producing many forward jumps */ |
| 54 | +static int classify(int x) |
| 55 | +{ |
| 56 | + if (x < 0) return -1; |
| 57 | + if (x == 0) return 0; |
| 58 | + if (x < 10) return 1; |
| 59 | + if (x < 100) return 2; |
| 60 | + return 3; |
| 61 | +} |
| 62 | + |
| 63 | +/* Nested loops with break + continue (backward + forward mix) */ |
| 64 | +static int nested_loop(void) |
| 65 | +{ |
| 66 | + int total = 0; |
| 67 | + int i, j; |
| 68 | + for (i = 0; i < 5; i++) { |
| 69 | + if (i == 3) continue; /* forward jump */ |
| 70 | + for (j = 0; j < 4; j++) { |
| 71 | + if (j == 2) break; /* forward jump */ |
| 72 | + total += i * 10 + j; |
| 73 | + } |
| 74 | + } |
| 75 | + return total; |
| 76 | +} |
| 77 | + |
| 78 | +/* ---- main -------------------------------------------------------------- */ |
| 79 | +int main(void) |
| 80 | +{ |
| 81 | + int err = 0; |
| 82 | + static int arr[] = { 3, 7, 1, 9, 5 }; |
| 83 | + |
| 84 | + /* single-block function */ |
| 85 | + if (identity(42) != 42) err |= 1; |
| 86 | + |
| 87 | + /* backward loop */ |
| 88 | + if (sum_to(10) != 55) err |= 2; |
| 89 | + |
| 90 | + /* forward branch (if/else) */ |
| 91 | + if (abs_val(-7) != 7 || abs_val(7) != 7) err |= 4; |
| 92 | + |
| 93 | + /* deeply nested forward (short-circuit ||) */ |
| 94 | + if (any_nonzero(0,0,0,0,0) != 0) err |= 8; |
| 95 | + if (any_nonzero(0,0,0,0,1) != 1) err |= 16; |
| 96 | + if (any_nonzero(1,0,0,0,0) != 1) err |= 32; |
| 97 | + |
| 98 | + /* loop with early break (forward + backward) */ |
| 99 | + if (first_ge(arr, 5, 8) != 9) err |= 64; |
| 100 | + if (first_ge(arr, 5, 99) != -1) err |= 128; |
| 101 | + |
| 102 | + /* cascaded if/else-if */ |
| 103 | + if (classify(-5) != -1) err |= 256; |
| 104 | + if (classify(0) != 0) err |= 512; |
| 105 | + if (classify(5) != 1) err |= 1024; |
| 106 | + if (classify(50) != 2) err |= 2048; |
| 107 | + if (classify(500)!= 3) err |= 4096; |
| 108 | + |
| 109 | + /* nested loop with break + continue */ |
| 110 | + if (nested_loop() != 144) err |= 8192; |
| 111 | + |
| 112 | + return err; |
| 113 | +} |
0 commit comments