omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / om / runtime / rx_regulate.om
| 1 | om2 |
| 2 | note rx_regulate.om -- ADAPTIVE-M4 bounded resource regulation in pure OM-2. |
| 3 | note Replaces the pure decision, envelope validation, policy derivation, |
| 4 | note bounds checking, and state-transition logic of src/runtime/rx_regulate.c. |
| 5 | note |
| 6 | note Packed env[16] layout (ulong): |
| 7 | note 0 slots_min 1 slots_max |
| 8 | note 2 memory_min 3 memory_max |
| 9 | note 4 energy_min 5 energy_max |
| 10 | note 6 base_slots 7 base_memory 8 base_energy |
| 11 | note 9 contention_hi 10 contention_lo |
| 12 | note 11 pressure_hi 12 pressure_lo |
| 13 | note 13 min_dwell |
| 14 | note |
| 15 | note Regimes: 0 = NOMINAL, 1 = EXPAND, 2 = PROTECT. |
| 16 | |
| 17 | const REG_NOMINAL ulong 0 |
| 18 | const REG_EXPAND ulong 1 |
| 19 | const REG_PROTECT ulong 2 |
| 20 | const U32_MAX ulong 4294967295 |
| 21 | |
| 22 | fn decide_impl env view ulong 16 regime ulong dwell ulong contention ulong pressure ulong to ulong |
| 23 | min_dw = get env 13 |
| 24 | settled = ge dwell min_dw |
| 25 | p_hi = get env 11 |
| 26 | if ge pressure p_hi |
| 27 | ret REG_PROTECT |
| 28 | end |
| 29 | if eq regime REG_PROTECT |
| 30 | p_lo = get env 12 |
| 31 | if not settled |
| 32 | ret REG_PROTECT |
| 33 | end |
| 34 | if gt pressure p_lo |
| 35 | ret REG_PROTECT |
| 36 | end |
| 37 | c_hi = get env 9 |
| 38 | if ge contention c_hi |
| 39 | ret REG_EXPAND |
| 40 | end |
| 41 | ret REG_NOMINAL |
| 42 | end |
| 43 | if eq regime REG_EXPAND |
| 44 | c_lo = get env 10 |
| 45 | if not settled |
| 46 | ret REG_EXPAND |
| 47 | end |
| 48 | if gt contention c_lo |
| 49 | ret REG_EXPAND |
| 50 | end |
| 51 | ret REG_NOMINAL |
| 52 | end |
| 53 | c_hi2 = get env 9 |
| 54 | if ge contention c_hi2 |
| 55 | ret REG_EXPAND |
| 56 | end |
| 57 | ret REG_NOMINAL |
| 58 | end |
| 59 | |
| 60 | fn policy_fill env view ulong 16 regime ulong dst edit ulong 4 to void |
| 61 | if eq regime REG_EXPAND |
| 62 | put dst 0 get env 1 |
| 63 | put dst 1 get env 3 |
| 64 | put dst 2 get env 5 |
| 65 | ret |
| 66 | end |
| 67 | if eq regime REG_PROTECT |
| 68 | put dst 0 get env 0 |
| 69 | put dst 1 get env 2 |
| 70 | put dst 2 get env 4 |
| 71 | ret |
| 72 | end |
| 73 | put dst 0 get env 6 |
| 74 | put dst 1 get env 7 |
| 75 | put dst 2 get env 8 |
| 76 | end |
| 77 | |
| 78 | note Returns 0 (RX_OK) when envelope is consistent, -1 (RX_ERR_ARG) otherwise. |
| 79 | export fn rx_reg_envelope_ok env view ulong 16 to long |
| 80 | smin = get env 0 |
| 81 | smax = get env 1 |
| 82 | if eq smin 0 |
| 83 | ret -1 |
| 84 | end |
| 85 | if gt smin smax |
| 86 | ret -1 |
| 87 | end |
| 88 | if gt smax U32_MAX |
| 89 | ret -1 |
| 90 | end |
| 91 | mmin = get env 2 |
| 92 | mmax = get env 3 |
| 93 | if gt mmin mmax |
| 94 | ret -1 |
| 95 | end |
| 96 | emin = get env 4 |
| 97 | emax = get env 5 |
| 98 | if gt emin emax |
| 99 | ret -1 |
| 100 | end |
| 101 | bs = get env 6 |
| 102 | if lt bs smin |
| 103 | ret -1 |
| 104 | end |
| 105 | if gt bs smax |
| 106 | ret -1 |
| 107 | end |
| 108 | bm = get env 7 |
| 109 | if lt bm mmin |
| 110 | ret -1 |
| 111 | end |
| 112 | if gt bm mmax |
| 113 | ret -1 |
| 114 | end |
| 115 | be = get env 8 |
| 116 | if lt be emin |
| 117 | ret -1 |
| 118 | end |
| 119 | if gt be emax |
| 120 | ret -1 |
| 121 | end |
| 122 | chi = get env 9 |
| 123 | clo = get env 10 |
| 124 | if ge clo chi |
| 125 | ret -1 |
| 126 | end |
| 127 | phi = get env 11 |
| 128 | plo = get env 12 |
| 129 | if ge plo phi |
| 130 | ret -1 |
| 131 | end |
| 132 | if gt phi 100 |
| 133 | ret -1 |
| 134 | end |
| 135 | mdw = get env 13 |
| 136 | if eq mdw 0 |
| 137 | ret -1 |
| 138 | end |
| 139 | ret 0 |
| 140 | end |
| 141 | |
| 142 | note Pure regime decision with hysteresis and dwell. |
| 143 | export fn rx_reg_decide env view ulong 16 regime ulong dwell ulong contention ulong pressure ulong to ulong |
| 144 | ret decide_impl env regime dwell contention pressure |
| 145 | end |
| 146 | |
| 147 | note Write the (slots, memory, energy) budget for regime into dst[0..2]. |
| 148 | export fn rx_reg_policy_for env view ulong 16 regime ulong dst edit ulong 4 to void |
| 149 | policy_fill env regime dst |
| 150 | end |
| 151 | |
| 152 | note Validate a policy object's fields[0..7] against the fixed envelope. |
| 153 | note Returns 0 (RX_OK) or -20 (RX_ERR_BOUNDS). |
| 154 | export fn rx_reg_apply_check env view ulong 16 pol view ulong 8 to long |
| 155 | slots = get pol 1 |
| 156 | if gt slots U32_MAX |
| 157 | ret -20 |
| 158 | end |
| 159 | if lt slots get env 0 |
| 160 | ret -20 |
| 161 | end |
| 162 | if gt slots get env 1 |
| 163 | ret -20 |
| 164 | end |
| 165 | mem = get pol 2 |
| 166 | if lt mem get env 2 |
| 167 | ret -20 |
| 168 | end |
| 169 | if gt mem get env 3 |
| 170 | ret -20 |
| 171 | end |
| 172 | eng = get pol 3 |
| 173 | if lt eng get env 4 |
| 174 | ret -20 |
| 175 | end |
| 176 | if gt eng get env 5 |
| 177 | ret -20 |
| 178 | end |
| 179 | reg = get pol 4 |
| 180 | if gt reg REG_PROTECT |
| 181 | ret -20 |
| 182 | end |
| 183 | ret 0 |
| 184 | end |
| 185 | |
| 186 | note Complete reaction step (fn_regulate): |
| 187 | note returns 0 = no-op (cseq == pol[5]) |
| 188 | note returns 1 = same regime (dst[5]=cseq, dst[6]=dwell+1) |
| 189 | note returns 2 = regime flip (dst[0..7] populated with new policy fields) |
| 190 | export fn rx_reg_step env view ulong 16 cond view ulong 8 pol view ulong 8 dst edit ulong 8 to ulong |
| 191 | cseq = get cond 0 |
| 192 | if eq cseq get pol 5 |
| 193 | ret 0 |
| 194 | end |
| 195 | regime = get pol 4 |
| 196 | dwell = get pol 6 |
| 197 | nxt = decide_impl env regime dwell get cond 1 get cond 3 |
| 198 | put dst 5 cseq |
| 199 | if eq nxt regime |
| 200 | put dst 6 add dwell 1 |
| 201 | ret 1 |
| 202 | end |
| 203 | b = new ulong 4 0 |
| 204 | policy_fill env nxt b |
| 205 | put dst 0 add get pol 0 1 |
| 206 | put dst 1 get b 0 |
| 207 | put dst 2 get b 1 |
| 208 | put dst 3 get b 2 |
| 209 | put dst 4 nxt |
| 210 | put dst 6 0 |
| 211 | put dst 7 add get pol 7 1 |
| 212 | ret 2 |
| 213 | end |