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 / visor / canonical_om2.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Pure seeded OM-2 canonical encoding & SemanticId primitives backing src/omega_canonical.c. |
| 3 | om2 |
| 4 | |
| 5 | note Per-kind object version: KIND_EFFECT (8) -> 2, all others -> 1. |
| 6 | export fn om_canon_obj_ver kind ulong to ulong |
| 7 | if eq kind 8 |
| 8 | ret 2 |
| 9 | end |
| 10 | ret 1 |
| 11 | end |
| 12 | |
| 13 | note Check header (version, kind): 0 OK, -2 legacy v1 effect refused, -1 version mismatch. |
| 14 | export fn om_canon_chk_hdr ver ulong kind ulong to long |
| 15 | if and eq kind 8 eq ver 1 |
| 16 | ret -2 |
| 17 | end |
| 18 | if ne ver om_canon_obj_ver kind |
| 19 | ret -1 |
| 20 | end |
| 21 | ret 0 |
| 22 | end |
| 23 | |
| 24 | note Lexicographical comparison of two 32-byte SemanticIds (-1, 0, 1). |
| 25 | export fn om_canon_cmp_id a view uchar 32 b view uchar 32 to long |
| 26 | for i ulong 32 |
| 27 | va = ulong get a i |
| 28 | vb = ulong get b i |
| 29 | if lt va vb |
| 30 | ret -1 |
| 31 | end |
| 32 | if gt va vb |
| 33 | ret 1 |
| 34 | end |
| 35 | end |
| 36 | ret 0 |
| 37 | end |
| 38 | |
| 39 | fn hex_nib v ulong to uchar |
| 40 | if lt v 10 |
| 41 | ret uchar add v 48 |
| 42 | end |
| 43 | ret uchar add v 87 |
| 44 | end |
| 45 | |
| 46 | note Format 32-byte SemanticId as 64 lowercase hex chars + NUL terminator. |
| 47 | export fn om_canon_hex_id id view uchar 32 dst edit uchar 65 to void |
| 48 | for i ulong 32 |
| 49 | b = ulong get id i |
| 50 | put dst mul i 2 hex_nib shr b 4 |
| 51 | put dst add mul i 2 1 hex_nib and b 15 |
| 52 | end |
| 53 | put dst 64 0 |
| 54 | end |
| 55 | |
| 56 | fn hex_val c ulong to long |
| 57 | if and ge c 48 le c 57 |
| 58 | ret long sub c 48 |
| 59 | end |
| 60 | if and ge c 97 le c 102 |
| 61 | ret long add sub c 97 10 |
| 62 | end |
| 63 | if and ge c 65 le c 70 |
| 64 | ret long add sub c 65 10 |
| 65 | end |
| 66 | ret -1 |
| 67 | end |
| 68 | |
| 69 | note Parse 64-char hex string into 32-byte SemanticId. Returns 0 on success, -1 on refusal. |
| 70 | export fn om_canon_parse_hex hx view uchar * n ulong dst edit uchar 32 to long |
| 71 | if ne n 64 |
| 72 | ret -1 |
| 73 | end |
| 74 | for i ulong 32 |
| 75 | hi = hex_val ulong get hx mul i 2 |
| 76 | lo = hex_val ulong get hx add mul i 2 1 |
| 77 | if or lt hi 0 lt lo 0 |
| 78 | ret -1 |
| 79 | end |
| 80 | put dst i uchar or shl ulong hi 4 ulong lo |
| 81 | end |
| 82 | ret 0 |
| 83 | end |
| 84 | |
| 85 | note Write canonical OMG0 header (6 bytes) into dst[0..5]. Returns 6 or -1 if mlen < 6. |
| 86 | export fn om_canon_write_hdr kind ulong dst edit uchar * mlen ulong to long |
| 87 | if or lt mlen 6 lt len dst 6 |
| 88 | ret -1 |
| 89 | end |
| 90 | put dst 0 79 |
| 91 | put dst 1 77 |
| 92 | put dst 2 71 |
| 93 | put dst 3 48 |
| 94 | put dst 4 uchar om_canon_obj_ver kind |
| 95 | put dst 5 uchar and kind 255 |
| 96 | ret 6 |
| 97 | end |
| 98 | |
| 99 | note Write big-endian u16 at dst[pos..pos+1]. Returns pos+2 or -1. |
| 100 | export fn om_canon_write_u16be v ulong dst edit uchar * pos ulong mlen ulong to long |
| 101 | if or gt add pos 2 mlen gt add pos 2 len dst |
| 102 | ret -1 |
| 103 | end |
| 104 | put dst pos uchar and shr v 8 255 |
| 105 | put dst add pos 1 uchar and v 255 |
| 106 | ret long add pos 2 |
| 107 | end |
| 108 | |
| 109 | note Write big-endian u32 at dst[pos..pos+3]. Returns pos+4 or -1. |
| 110 | export fn om_canon_write_u32be v ulong dst edit uchar * pos ulong mlen ulong to long |
| 111 | if or gt add pos 4 mlen gt add pos 4 len dst |
| 112 | ret -1 |
| 113 | end |
| 114 | put dst pos uchar and shr v 24 255 |
| 115 | put dst add pos 1 uchar and shr v 16 255 |
| 116 | put dst add pos 2 uchar and shr v 8 255 |
| 117 | put dst add pos 3 uchar and v 255 |
| 118 | ret long add pos 4 |
| 119 | end |
| 120 | |
| 121 | note Compare two 64-byte NUL-terminated attribute keys at slots ia and ib in keys[32*64]. |
| 122 | fn cmp_attr_key keys view uchar 2048 ia ulong ib ulong to long |
| 123 | oa = mul ia 64 |
| 124 | ob = mul ib 64 |
| 125 | for k ulong 64 |
| 126 | ca = ulong get keys add oa k |
| 127 | cb = ulong get keys add ob k |
| 128 | if lt ca cb |
| 129 | ret -1 |
| 130 | end |
| 131 | if gt ca cb |
| 132 | ret 1 |
| 133 | end |
| 134 | if eq ca 0 |
| 135 | ret 0 |
| 136 | end |
| 137 | end |
| 138 | ret 0 |
| 139 | end |
| 140 | |
| 141 | note Stable insertion sort of attribute indices [0..n-1] by key ascending. |
| 142 | export fn om_canon_sort_attr keys view uchar 2048 n ulong order edit ushort 32 to long |
| 143 | if gt n 32 |
| 144 | ret -1 |
| 145 | end |
| 146 | for i ulong n |
| 147 | put order i ushort i |
| 148 | end |
| 149 | i = ulong 1 |
| 150 | while lt i n |
| 151 | kidx = ulong get order i |
| 152 | j = long sub i 1 |
| 153 | while ge j 0 |
| 154 | prev = ulong get order ulong j |
| 155 | if le cmp_attr_key keys prev kidx 0 |
| 156 | break |
| 157 | end |
| 158 | put order ulong add j 1 ushort prev |
| 159 | j = sub j 1 |
| 160 | end |
| 161 | put order ulong add j 1 ushort kidx |
| 162 | i = add i 1 |
| 163 | end |
| 164 | ret 0 |
| 165 | end |
| 166 | |
| 167 | note Compare two relations by (kind, 32-byte target_id). |
| 168 | fn cmp_rel kinds view ushort 64 targets view uchar 2048 ia ulong ib ulong to long |
| 169 | ka = ulong get kinds ia |
| 170 | kb = ulong get kinds ib |
| 171 | if lt ka kb |
| 172 | ret -1 |
| 173 | end |
| 174 | if gt ka kb |
| 175 | ret 1 |
| 176 | end |
| 177 | oa = mul ia 32 |
| 178 | ob = mul ib 32 |
| 179 | for k ulong 32 |
| 180 | ba = ulong get targets add oa k |
| 181 | bb = ulong get targets add ob k |
| 182 | if lt ba bb |
| 183 | ret -1 |
| 184 | end |
| 185 | if gt ba bb |
| 186 | ret 1 |
| 187 | end |
| 188 | end |
| 189 | ret 0 |
| 190 | end |
| 191 | |
| 192 | note Stable insertion sort of relation indices [0..n-1] by (kind, target_id). |
| 193 | export fn om_canon_sort_rel kinds view ushort 64 targets view uchar 2048 n ulong order edit ushort 64 to long |
| 194 | if gt n 64 |
| 195 | ret -1 |
| 196 | end |
| 197 | for i ulong n |
| 198 | put order i ushort i |
| 199 | end |
| 200 | i = ulong 1 |
| 201 | while lt i n |
| 202 | cur = ulong get order i |
| 203 | j = long sub i 1 |
| 204 | while ge j 0 |
| 205 | prev = ulong get order ulong j |
| 206 | if le cmp_rel kinds targets prev cur 0 |
| 207 | break |
| 208 | end |
| 209 | put order ulong add j 1 ushort prev |
| 210 | j = sub j 1 |
| 211 | end |
| 212 | put order ulong add j 1 ushort cur |
| 213 | i = add i 1 |
| 214 | end |
| 215 | ret 0 |
| 216 | end |
| 217 | |
| 218 | fn cmp_const_pay payloads view uchar 4096 oa ulong ob ulong ml ulong to long |
| 219 | for k ulong ml |
| 220 | ba = ulong get payloads add oa k |
| 221 | bb = ulong get payloads add ob k |
| 222 | if lt ba bb |
| 223 | ret -1 |
| 224 | end |
| 225 | if gt ba bb |
| 226 | ret 1 |
| 227 | end |
| 228 | end |
| 229 | ret 0 |
| 230 | end |
| 231 | |
| 232 | note Compare two constraints by (kind, payload bytes, payload_len). meta packs [2*i]=kind, [2*i+1]=len. |
| 233 | fn cmp_const meta view ushort 64 payloads view uchar 4096 ia ulong ib ulong to long |
| 234 | ka = ulong get meta mul ia 2 |
| 235 | kb = ulong get meta mul ib 2 |
| 236 | if lt ka kb |
| 237 | ret -1 |
| 238 | end |
| 239 | if gt ka kb |
| 240 | ret 1 |
| 241 | end |
| 242 | la = ulong get meta add mul ia 2 1 |
| 243 | lb = ulong get meta add mul ib 2 1 |
| 244 | ml = la |
| 245 | if lt lb ml |
| 246 | ml = lb |
| 247 | end |
| 248 | c = cmp_const_pay payloads mul ia 128 mul ib 128 ml |
| 249 | if ne c 0 |
| 250 | ret c |
| 251 | end |
| 252 | if lt la lb |
| 253 | ret -1 |
| 254 | end |
| 255 | if gt la lb |
| 256 | ret 1 |
| 257 | end |
| 258 | ret 0 |
| 259 | end |
| 260 | |
| 261 | note Stable insertion sort of constraint indices [0..n-1]. |
| 262 | export fn om_canon_sort_const meta view ushort 64 payloads view uchar 4096 n ulong order edit ushort 32 to long |
| 263 | if gt n 32 |
| 264 | ret -1 |
| 265 | end |
| 266 | for i ulong n |
| 267 | put order i ushort i |
| 268 | end |
| 269 | i = ulong 1 |
| 270 | while lt i n |
| 271 | cur = ulong get order i |
| 272 | j = long sub i 1 |
| 273 | while ge j 0 |
| 274 | prev = ulong get order ulong j |
| 275 | if le cmp_const meta payloads prev cur 0 |
| 276 | break |
| 277 | end |
| 278 | put order ulong add j 1 ushort prev |
| 279 | j = sub j 1 |
| 280 | end |
| 281 | put order ulong add j 1 ushort cur |
| 282 | i = add i 1 |
| 283 | end |
| 284 | ret 0 |
| 285 | end |