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 / seed / tensix / irrv32.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note OSC IR v1 scalar uint unit to an RV32IM elementwise kernel for a Tensix baby RISC-V core. |
| 3 | note Wire authority spec/one-layer-format.md. Encodings: RISC-V unprivileged ISA, RV32I and M. |
| 4 | note Input: one function of two uint params returning uint, one block, CONST MOV BIN RET only. |
| 5 | note Output: raw little-endian RV32 words of the loop out[i] = f(a[i], b[i]) for i below n. |
| 6 | note Kernel ABI: a0 = a, a1 = b, a2 = out, a3 = n (32-bit words), return through ra. |
| 7 | note Vreg k maps to x5 x6 x7 for k below 3, then x14 x15 x16 x17; at most 7 vregs. |
| 8 | note Supported BIN: add sub mul and or xor (u32 wraps, as RV32 does). div rem shl shr are |
| 9 | note refused because Omega traps on zero divisors and shift range and this kernel has no trap path. |
| 10 | note Buffered output: every refusal happens before any stdout write. |
| 11 | global input uchar 65536 |
| 12 | global output uchar 4096 |
| 13 | note state slots: 0 input cursor, 1 input size, 2 output size, 3 nvregs, 4 ninsns, 5 defined mask |
| 14 | global state ulong 8 |
| 15 | global ops ulong 64 |
| 16 | global subs ulong 64 |
| 17 | global dest ulong 64 |
| 18 | global arg0 ulong 64 |
| 19 | global arg1 ulong 64 |
| 20 | global immed ulong 64 |
| 21 | text emagic "irrv32: MAGIC\n" |
| 22 | text ever "irrv32: VERSION\n" |
| 23 | text etrunc "irrv32: TRUNCATED\n" |
| 24 | text ecap "irrv32: CAPACITY\n" |
| 25 | text eop "irrv32: UNSUPPORTED_OP\n" |
| 26 | text eshape "irrv32: UNSUPPORTED_SHAPE\n" |
| 27 | text einval "irrv32: INVALID\n" |
| 28 | text etrail "irrv32: TRAILING\n" |
| 29 | text eio "irrv32: IO\n" |
| 30 | fn refuse code int to void |
| 31 | k = long 0 |
| 32 | if eq code 20 |
| 33 | k = write 2 emagic 0 len emagic |
| 34 | end |
| 35 | if eq code 21 |
| 36 | k = write 2 ever 0 len ever |
| 37 | end |
| 38 | if eq code 22 |
| 39 | k = write 2 etrunc 0 len etrunc |
| 40 | end |
| 41 | if eq code 23 |
| 42 | k = write 2 ecap 0 len ecap |
| 43 | end |
| 44 | if eq code 24 |
| 45 | k = write 2 eop 0 len eop |
| 46 | end |
| 47 | if eq code 25 |
| 48 | k = write 2 eshape 0 len eshape |
| 49 | end |
| 50 | if eq code 26 |
| 51 | k = write 2 einval 0 len einval |
| 52 | end |
| 53 | if eq code 27 |
| 54 | k = write 2 etrail 0 len etrail |
| 55 | end |
| 56 | if eq code 28 |
| 57 | k = write 2 eio 0 len eio |
| 58 | end |
| 59 | exit code |
| 60 | end |
| 61 | fn need ok bool to void |
| 62 | if not ok |
| 63 | refuse 26 |
| 64 | end |
| 65 | end |
| 66 | fn rd n ulong to ulong |
| 67 | p = get state 0 |
| 68 | if gt n sub get state 1 p |
| 69 | refuse 22 |
| 70 | end |
| 71 | v = ulong 0 |
| 72 | for j ulong n |
| 73 | v = or v shl ulong get input add p j mul j 8 |
| 74 | end |
| 75 | put state 0 add p n |
| 76 | ret v |
| 77 | end |
| 78 | fn vr to ulong |
| 79 | v = rd 2 |
| 80 | need lt v get state 3 |
| 81 | ret v |
| 82 | end |
| 83 | fn isdef v ulong to bool |
| 84 | ret ne and get state 5 shl ulong 1 v 0 |
| 85 | end |
| 86 | fn def v ulong to void |
| 87 | put state 5 or get state 5 shl ulong 1 v |
| 88 | end |
| 89 | fn reg v ulong to ulong |
| 90 | if lt v 3 |
| 91 | ret add v 5 |
| 92 | end |
| 93 | ret add v 11 |
| 94 | end |
| 95 | fn emitw w ulong to void |
| 96 | p = get state 2 |
| 97 | if gt add p 4 len output |
| 98 | refuse 23 |
| 99 | end |
| 100 | for j ulong 4 |
| 101 | put output add p j uchar and shr w mul j 8 255 |
| 102 | end |
| 103 | put state 2 add p 4 |
| 104 | end |
| 105 | note R-type: funct7 rs2 rs1 funct3 rd, opcode 51 (OP). |
| 106 | fn rtype f7 ulong rs2 ulong rs1 ulong f3 ulong dr ulong to void |
| 107 | w = or shl f7 25 shl rs2 20 |
| 108 | w = or w or shl rs1 15 shl f3 12 |
| 109 | w = or w or shl dr 7 51 |
| 110 | emitw w |
| 111 | end |
| 112 | note I-type: imm is the low 12 bits of the two's-complement immediate (4095 is -1). |
| 113 | fn itype imm ulong rs1 ulong f3 ulong dr ulong op ulong to void |
| 114 | w = or shl and imm 4095 20 shl rs1 15 |
| 115 | w = or w or shl f3 12 shl dr 7 |
| 116 | w = or w op |
| 117 | emitw w |
| 118 | end |
| 119 | note S-type: store word rs2 to imm(rs1), opcode 35 (STORE). |
| 120 | fn stype imm ulong rs2 ulong rs1 ulong f3 ulong to void |
| 121 | w = or shl shr imm 5 25 shl rs2 20 |
| 122 | w = or w or shl rs1 15 shl f3 12 |
| 123 | w = or w or shl and imm 31 7 35 |
| 124 | emitw w |
| 125 | end |
| 126 | note B-type: off is the 13-bit two's-complement byte offset (8192 minus k is -k), opcode 99. |
| 127 | fn btype off ulong rs1 ulong rs2 ulong f3 ulong to void |
| 128 | b12 = and shr off 12 1 |
| 129 | b11 = and shr off 11 1 |
| 130 | b10 = and shr off 5 63 |
| 131 | b4 = and shr off 1 15 |
| 132 | w = or shl b12 31 shl b10 25 |
| 133 | w = or w or shl rs2 20 shl rs1 15 |
| 134 | w = or w or shl f3 12 shl b4 8 |
| 135 | w = or w or shl b11 7 99 |
| 136 | emitw w |
| 137 | end |
| 138 | note U-type: 20-bit upper immediate, opcode 55 (LUI). |
| 139 | fn utype imm ulong dr ulong op ulong to void |
| 140 | emitw or shl and imm 1048575 12 or shl dr 7 op |
| 141 | end |
| 142 | fn insn i ulong to void |
| 143 | op = rd 1 |
| 144 | put ops i op |
| 145 | known = or or eq op 1 eq op 2 or eq op 3 eq op 15 |
| 146 | if not known |
| 147 | refuse 24 |
| 148 | end |
| 149 | if eq op 1 |
| 150 | d = vr |
| 151 | v = rd 8 |
| 152 | if gt v 4294967295 |
| 153 | refuse 26 |
| 154 | end |
| 155 | put dest i d |
| 156 | put immed i v |
| 157 | def d |
| 158 | end |
| 159 | if eq op 2 |
| 160 | d = vr |
| 161 | a = vr |
| 162 | need isdef a |
| 163 | put dest i d |
| 164 | put arg0 i a |
| 165 | def d |
| 166 | end |
| 167 | if eq op 3 |
| 168 | s = rd 1 |
| 169 | d = vr |
| 170 | a = vr |
| 171 | b = vr |
| 172 | ok = or or eq s 1 eq s 2 eq s 3 |
| 173 | ok = or ok or or eq s 6 eq s 7 eq s 8 |
| 174 | if not ok |
| 175 | refuse 24 |
| 176 | end |
| 177 | need isdef a |
| 178 | need isdef b |
| 179 | put subs i s |
| 180 | put dest i d |
| 181 | put arg0 i a |
| 182 | put arg1 i b |
| 183 | def d |
| 184 | end |
| 185 | if eq op 15 |
| 186 | a = vr |
| 187 | need isdef a |
| 188 | put arg0 i a |
| 189 | end |
| 190 | end |
| 191 | fn skip n ulong to void |
| 192 | for j ulong n |
| 193 | k = rd 1 |
| 194 | end |
| 195 | end |
| 196 | fn loadfn to void |
| 197 | skip rd 1 |
| 198 | if ne rd 1 2 |
| 199 | refuse 25 |
| 200 | end |
| 201 | if ne rd 1 4 |
| 202 | refuse 25 |
| 203 | end |
| 204 | nv = rd 2 |
| 205 | if gt nv 7 |
| 206 | refuse 23 |
| 207 | end |
| 208 | need ge nv 2 |
| 209 | put state 3 nv |
| 210 | for j ulong nv |
| 211 | if ne rd 1 4 |
| 212 | refuse 25 |
| 213 | end |
| 214 | end |
| 215 | if ne rd 2 1 |
| 216 | refuse 24 |
| 217 | end |
| 218 | bf = rd 4 |
| 219 | bc = rd 4 |
| 220 | ni = rd 4 |
| 221 | if gt ni 64 |
| 222 | refuse 23 |
| 223 | end |
| 224 | need gt ni 0 |
| 225 | need eq bf 0 |
| 226 | need eq bc ni |
| 227 | put state 4 ni |
| 228 | put state 5 3 |
| 229 | for i ulong ni |
| 230 | insn i |
| 231 | end |
| 232 | skip rd 2 |
| 233 | skip rd 2 |
| 234 | for i ulong ni |
| 235 | isret = eq get ops i 15 |
| 236 | last = eq i sub ni 1 |
| 237 | if ne isret last |
| 238 | refuse 24 |
| 239 | end |
| 240 | end |
| 241 | end |
| 242 | fn kernel to void |
| 243 | ni = get state 4 |
| 244 | nl = ulong 0 |
| 245 | for i ulong ni |
| 246 | op = get ops i |
| 247 | if eq op 1 |
| 248 | nl = add nl 2 |
| 249 | end |
| 250 | if or eq op 2 eq op 3 |
| 251 | nl = add nl 1 |
| 252 | end |
| 253 | end |
| 254 | note beq a3, x0, done: forward 4 * (9 + body words) bytes |
| 255 | btype mul 4 add 9 nl 13 0 0 |
| 256 | note lw t0, 0(a0); lw t1, 0(a1): the two params, vregs 0 and 1 |
| 257 | itype 0 10 2 5 3 |
| 258 | itype 0 11 2 6 3 |
| 259 | rv = ulong 0 |
| 260 | for i ulong ni |
| 261 | op = get ops i |
| 262 | d = reg get dest i |
| 263 | if eq op 1 |
| 264 | v = get immed i |
| 265 | utype shr add v 2048 12 d 55 |
| 266 | itype and v 4095 d 0 d 19 |
| 267 | end |
| 268 | if eq op 2 |
| 269 | itype 0 reg get arg0 i 0 d 19 |
| 270 | end |
| 271 | if eq op 3 |
| 272 | s = get subs i |
| 273 | f7 = ulong 0 |
| 274 | f3 = ulong 0 |
| 275 | if eq s 2 |
| 276 | f7 = ulong 32 |
| 277 | end |
| 278 | if eq s 3 |
| 279 | f7 = ulong 1 |
| 280 | end |
| 281 | if eq s 6 |
| 282 | f3 = ulong 7 |
| 283 | end |
| 284 | if eq s 7 |
| 285 | f3 = ulong 6 |
| 286 | end |
| 287 | if eq s 8 |
| 288 | f3 = ulong 4 |
| 289 | end |
| 290 | rtype f7 reg get arg1 i reg get arg0 i f3 d |
| 291 | end |
| 292 | if eq op 15 |
| 293 | rv = reg get arg0 i |
| 294 | end |
| 295 | end |
| 296 | note sw result, 0(a2); advance a0 a1 a2 by 4; a3 -= 1; bne a3, x0, loop; jalr x0, 0(ra) |
| 297 | stype 0 rv 12 2 |
| 298 | itype 4 10 0 10 19 |
| 299 | itype 4 11 0 11 19 |
| 300 | itype 4 12 0 12 19 |
| 301 | itype 4095 13 0 13 19 |
| 302 | btype sub 8192 mul 4 add 7 nl 13 0 1 |
| 303 | itype 0 1 0 0 103 |
| 304 | end |
| 305 | fn main to int |
| 306 | n = ulong 0 |
| 307 | for attempt ulong 65536 |
| 308 | if eq n len input |
| 309 | refuse 23 |
| 310 | end |
| 311 | k = read 0 input n sub len input n |
| 312 | if lt k 0 |
| 313 | refuse 28 |
| 314 | end |
| 315 | if eq k 0 |
| 316 | break |
| 317 | end |
| 318 | n = add n ulong k |
| 319 | end |
| 320 | if eq n len input |
| 321 | refuse 23 |
| 322 | end |
| 323 | put state 1 n |
| 324 | magic = new uchar 7 0 |
| 325 | put magic 0 79 |
| 326 | put magic 1 83 |
| 327 | put magic 2 67 |
| 328 | put magic 3 49 |
| 329 | put magic 4 73 |
| 330 | put magic 5 82 |
| 331 | for j ulong 7 |
| 332 | v = rd 1 |
| 333 | if ne v ulong get magic j |
| 334 | refuse 20 |
| 335 | end |
| 336 | end |
| 337 | if ne rd 1 1 |
| 338 | refuse 21 |
| 339 | end |
| 340 | if ne rd 2 1 |
| 341 | refuse 25 |
| 342 | end |
| 343 | loadfn |
| 344 | if ne get state 0 n |
| 345 | refuse 27 |
| 346 | end |
| 347 | kernel |
| 348 | p = ulong 0 |
| 349 | for attempt ulong 4096 |
| 350 | if eq p get state 2 |
| 351 | break |
| 352 | end |
| 353 | k = write 1 output p sub get state 2 p |
| 354 | if le k 0 |
| 355 | refuse 28 |
| 356 | end |
| 357 | p = add p ulong k |
| 358 | end |
| 359 | ret 0 |
| 360 | end |