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 / compiler / osc_flow.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note OSC statement reachability and IR basic-block partitioning. |
| 3 | note bridge c-define OSC_FLOW_RETURNS 1 |
| 4 | note bridge c-define OSC_FLOW_UNREACHABLE 2 |
| 5 | note bridge c-define OSC_BLOCK_BAD_RANGE 1 |
| 6 | note bridge c-define OSC_BLOCK_OVERLAP 2 |
| 7 | note bridge c-define OSC_BLOCK_NO_TERMINATOR 3 |
| 8 | note bridge c-define OSC_BLOCK_EARLY_TERMINATOR 4 |
| 9 | note bridge c-define OSC_BLOCK_UNCOVERED 5 |
| 10 | om2 |
| 11 | |
| 12 | const node_size ulong 104 |
| 13 | const block_node ulong 3 |
| 14 | const if_node ulong 10 |
| 15 | const while_node ulong 11 |
| 16 | const for_node ulong 12 |
| 17 | const return_node ulong 13 |
| 18 | const arena_node ulong 27 |
| 19 | const pool_node ulong 28 |
| 20 | const absent ulong 4294967295 |
| 21 | const ir_insn_size ulong 48 |
| 22 | const ir_br uchar 13 |
| 23 | const ir_ret uchar 15 |
| 24 | |
| 25 | fn read_le bytes view uchar * pos ulong width ulong to ulong |
| 26 | value = ulong 0 |
| 27 | for i ulong width |
| 28 | value = or value shl ulong get bytes add pos i mul i 8 |
| 29 | end |
| 30 | ret value |
| 31 | end |
| 32 | |
| 33 | fn summarize_stmt nodes view uchar * id ulong flow edit uchar * to void |
| 34 | base = mul id node_size |
| 35 | kind = read_le nodes base 2 |
| 36 | returns = uchar 0 |
| 37 | if eq kind return_node |
| 38 | returns = 1 |
| 39 | end |
| 40 | if or eq kind arena_node eq kind pool_node |
| 41 | returns = get flow read_le nodes add base 16 4 |
| 42 | end |
| 43 | if eq kind if_node |
| 44 | other = read_le nodes add base 20 4 |
| 45 | if ne other absent |
| 46 | then_id = read_le nodes add base 16 4 |
| 47 | returns = and get flow other get flow then_id |
| 48 | end |
| 49 | end |
| 50 | if eq kind block_node |
| 51 | stmt = read_le nodes add base 12 4 |
| 52 | while ne stmt absent |
| 53 | if ne returns 0 |
| 54 | put flow stmt or get flow stmt uchar 2 |
| 55 | else |
| 56 | returns = get flow stmt |
| 57 | end |
| 58 | stmt = read_le nodes add mul stmt node_size 24 4 |
| 59 | end |
| 60 | end |
| 61 | put flow id returns |
| 62 | end |
| 63 | |
| 64 | note Visit only statements, recording parents before their children. Reversing |
| 65 | note this order computes each child's return status before its parent needs it. |
| 66 | note A loop may execute zero times even if its entire body returns. |
| 67 | export fn osc_statement_flow nodes view uchar * order edit ulong * flow edit uchar * to void |
| 68 | requires eq len nodes mul len flow node_size |
| 69 | requires ge len order len flow |
| 70 | note The caller puts the function body in order[0]. |
| 71 | count = ulong 1 |
| 72 | cursor = ulong 0 |
| 73 | while lt cursor count |
| 74 | base = mul get order cursor node_size |
| 75 | cursor = add cursor 1 |
| 76 | kind = read_le nodes base 2 |
| 77 | if eq kind block_node |
| 78 | stmt = read_le nodes add base 12 4 |
| 79 | while ne stmt absent |
| 80 | put order count stmt |
| 81 | count = add count 1 |
| 82 | stmt = read_le nodes add mul stmt node_size 24 4 |
| 83 | end |
| 84 | else |
| 85 | loop_body = or eq kind while_node eq kind for_node |
| 86 | region_body = or eq kind arena_node eq kind pool_node |
| 87 | if or eq kind if_node or loop_body region_body |
| 88 | put order count read_le nodes add base 16 4 |
| 89 | count = add count 1 |
| 90 | if eq kind if_node |
| 91 | other = read_le nodes add base 20 4 |
| 92 | if ne other absent |
| 93 | put order count other |
| 94 | count = add count 1 |
| 95 | end |
| 96 | end |
| 97 | end |
| 98 | end |
| 99 | end |
| 100 | for step ulong count |
| 101 | id = get order sub sub count 1 step |
| 102 | summarize_stmt nodes id flow |
| 103 | end |
| 104 | end |
| 105 | |
| 106 | fn check_block insns view uchar * cover edit uchar 4096 first ulong count ulong location edit ulong 1 to ulong |
| 107 | if or eq count 0 gt add first count div len insns ir_insn_size |
| 108 | ret 1 |
| 109 | end |
| 110 | for offset ulong count |
| 111 | i = add first offset |
| 112 | if ne get cover i 0 |
| 113 | put location 0 i |
| 114 | ret 2 |
| 115 | end |
| 116 | put cover i 1 |
| 117 | op = get insns mul i ir_insn_size |
| 118 | term = and ge op ir_br le op ir_ret |
| 119 | if eq add offset 1 count |
| 120 | if eq term false |
| 121 | ret 3 |
| 122 | end |
| 123 | else |
| 124 | if term |
| 125 | ret 4 |
| 126 | end |
| 127 | end |
| 128 | end |
| 129 | ret 0 |
| 130 | end |
| 131 | |
| 132 | note Block order determines the first diagnostic. Report its block index for a |
| 133 | note bad range or terminator and its instruction index for a coverage error. |
| 134 | export fn osc_block_partition blocks view uchar * insns view uchar * location out ulong 1 to ulong |
| 135 | requires eq rem len blocks 8 0 |
| 136 | requires eq rem len insns ir_insn_size 0 |
| 137 | requires le div len insns ir_insn_size 4096 |
| 138 | cover = new uchar 4096 0 |
| 139 | ninsns = div len insns ir_insn_size |
| 140 | for b ulong div len blocks 8 |
| 141 | base = mul b 8 |
| 142 | first = read_le blocks base 4 |
| 143 | count = read_le blocks add base 4 4 |
| 144 | put location 0 b |
| 145 | failure = check_block insns cover first count location |
| 146 | if ne failure 0 |
| 147 | ret failure |
| 148 | end |
| 149 | end |
| 150 | for i ulong ninsns |
| 151 | if eq get cover i 0 |
| 152 | put location 0 i |
| 153 | ret 5 |
| 154 | end |
| 155 | end |
| 156 | put location 0 0 |
| 157 | ret 0 |
| 158 | end |
| 159 | |
| 160 | note Each transfer record holds a block's generated register bitset followed |
| 161 | note by its two successor indices, with the block count meaning no successor. |
| 162 | note Propagate IN union GEN along each edge. Intersection at the destination |
| 163 | note retains only registers defined on every incoming path. |
| 164 | fn meet_words inputs edit ulong * generated view ulong * source ulong dest ulong to bool |
| 165 | changed = false |
| 166 | for k ulong len generated |
| 167 | outgoing = or get inputs add source k get generated k |
| 168 | old = get inputs add dest k |
| 169 | restricted = and old outgoing |
| 170 | if ne restricted old |
| 171 | put inputs add dest k restricted |
| 172 | changed = true |
| 173 | end |
| 174 | end |
| 175 | ret changed |
| 176 | end |
| 177 | |
| 178 | fn meet_edge inputs edit ulong * record view ulong * source ulong successor ulong to bool |
| 179 | width = sub len record 2 |
| 180 | target = get record add width successor |
| 181 | if eq target div len inputs width |
| 182 | ret false |
| 183 | end |
| 184 | ret meet_words inputs part record 0 width source mul target width |
| 185 | end |
| 186 | |
| 187 | export fn osc_definite_inputs transfers view ulong * inputs edit ulong * params ulong width ulong to void |
| 188 | requires gt width 0 |
| 189 | requires eq rem len inputs width 0 |
| 190 | requires ge len inputs width |
| 191 | requires eq len transfers mul div len inputs width add width 2 |
| 192 | requires le params 6 |
| 193 | blocks = div len inputs width |
| 194 | for i ulong len inputs |
| 195 | put inputs i 0xffffffffffffffff |
| 196 | end |
| 197 | for k ulong width |
| 198 | put inputs k 0 |
| 199 | end |
| 200 | put inputs 0 sub shl ulong 1 params 1 |
| 201 | changed = true |
| 202 | while changed |
| 203 | changed = false |
| 204 | for b ulong blocks |
| 205 | record = mul b add width 2 |
| 206 | for successor ulong 2 |
| 207 | if meet_edge inputs part transfers record add width 2 mul b width successor |
| 208 | changed = true |
| 209 | end |
| 210 | end |
| 211 | end |
| 212 | end |
| 213 | end |
| 214 | |
| 215 | fn copy_insn source view ulong 24576 dest edit ulong 24576 src ulong dst ulong to void |
| 216 | for k ulong 6 |
| 217 | put dest add dst k get source add src k |
| 218 | end |
| 219 | end |
| 220 | |
| 221 | note Stable counting sort of emitted instructions by block, preserving both |
| 222 | note block creation order and instruction emission order within each block. |
| 223 | export fn osc_layout_blocks tags view short * source view ulong 24576 dest edit ulong 24576 blocks edit uint 1024 nblocks ulong to void |
| 224 | requires le nblocks 512 |
| 225 | requires le len tags 4096 |
| 226 | for i ulong len tags |
| 227 | label = get tags i |
| 228 | if or lt label 0 ge long label long nblocks |
| 229 | trap 3 |
| 230 | end |
| 231 | end |
| 232 | for b ulong nblocks |
| 233 | put blocks add mul b 2 1 0 |
| 234 | end |
| 235 | for i ulong len tags |
| 236 | pos = add mul ulong get tags i 2 1 |
| 237 | put blocks pos add get blocks pos 1 |
| 238 | end |
| 239 | first = uint 0 |
| 240 | for b ulong nblocks |
| 241 | pos = mul b 2 |
| 242 | put blocks pos first |
| 243 | first = add first get blocks add pos 1 |
| 244 | put blocks add pos 1 0 |
| 245 | end |
| 246 | for i ulong len tags |
| 247 | pos = mul ulong get tags i 2 |
| 248 | count = get blocks add pos 1 |
| 249 | dst = mul ulong add get blocks pos count 6 |
| 250 | put blocks add pos 1 add count 1 |
| 251 | copy_insn source dest mul i 6 dst |
| 252 | end |
| 253 | end |
| 254 | |
| 255 | note OscInsn stores signed 16-bit register indices. A decoded value with |
| 256 | note the high bit set is absent or invalid. validate_insn checks every |
| 257 | note mandatory operand before the resource and dataflow walks. |
| 258 | const IR_CONST uchar 1 |
| 259 | const IR_MOV uchar 2 |
| 260 | const IR_BIN uchar 3 |
| 261 | const IR_UN uchar 4 |
| 262 | const IR_CMP uchar 5 |
| 263 | const IR_CAST uchar 6 |
| 264 | const IR_ALLOC uchar 7 |
| 265 | const IR_RELEASE uchar 8 |
| 266 | const IR_LOAD uchar 9 |
| 267 | const IR_STORE uchar 10 |
| 268 | const IR_CALL uchar 11 |
| 269 | const IR_CBR uchar 14 |
| 270 | const IR_FLOAD uchar 16 |
| 271 | const IR_FSTORE uchar 17 |
| 272 | const IR_AOPEN uchar 18 |
| 273 | const IR_AALLOC uchar 19 |
| 274 | const IR_ADESTROY uchar 20 |
| 275 | const IR_POPEN uchar 21 |
| 276 | const IR_PCLOSE uchar 22 |
| 277 | const IR_HALLOC uchar 23 |
| 278 | const IR_HGEN uchar 24 |
| 279 | const IR_HFREE uchar 25 |
| 280 | const IR_HLOAD uchar 26 |
| 281 | const IR_HSTORE uchar 27 |
| 282 | const IR_SLOAD uchar 28 |
| 283 | const IR_SSTORE uchar 29 |
| 284 | const NO_REG ulong 65535 |
| 285 | |
| 286 | fn ir_def insn view uchar * to ulong |
| 287 | op = get insn 0 |
| 288 | if and ge op IR_CONST le op IR_ALLOC |
| 289 | ret read_le insn 2 2 |
| 290 | end |
| 291 | if or eq op IR_LOAD or eq op IR_CALL eq op IR_FLOAD |
| 292 | ret read_le insn 2 2 |
| 293 | end |
| 294 | if or eq op IR_AOPEN or eq op IR_AALLOC eq op IR_POPEN |
| 295 | ret read_le insn 2 2 |
| 296 | end |
| 297 | if or eq op IR_HALLOC or eq op IR_HGEN or eq op IR_HLOAD eq op IR_SLOAD |
| 298 | ret read_le insn 2 2 |
| 299 | end |
| 300 | ret NO_REG |
| 301 | end |
| 302 | |
| 303 | note Return uses in diagnostic order. Fixed slots match OSC_MAX_PARAMS. |
| 304 | fn ir_uses insn view uchar * uses edit ushort 6 to ulong |
| 305 | op = get insn 0 |
| 306 | a = ushort read_le insn 4 2 |
| 307 | b = ushort read_le insn 6 2 |
| 308 | c = ushort read_le insn 8 2 |
| 309 | put uses 0 a |
| 310 | if or eq op IR_MOV or eq op IR_UN or eq op IR_CAST eq op IR_ALLOC |
| 311 | ret 1 |
| 312 | end |
| 313 | if or eq op IR_RELEASE or eq op IR_CBR or eq op IR_ADESTROY eq op IR_PCLOSE |
| 314 | ret 1 |
| 315 | end |
| 316 | if eq op ir_ret |
| 317 | if lt a 32768 |
| 318 | ret 1 |
| 319 | end |
| 320 | ret 0 |
| 321 | end |
| 322 | put uses 1 b |
| 323 | if or eq op IR_AALLOC or eq op IR_HALLOC eq op IR_HGEN |
| 324 | ret 2 |
| 325 | end |
| 326 | if or eq op IR_BIN or eq op IR_CMP eq op IR_LOAD |
| 327 | ret 2 |
| 328 | end |
| 329 | put uses 2 c |
| 330 | if or eq op IR_STORE or eq op IR_HFREE eq op IR_HLOAD |
| 331 | ret 3 |
| 332 | end |
| 333 | if eq op IR_HSTORE |
| 334 | put uses 3 ushort read_le insn 18 2 |
| 335 | ret 4 |
| 336 | end |
| 337 | if or eq op IR_SLOAD eq op IR_SSTORE |
| 338 | put uses 1 add a 1 |
| 339 | put uses 2 b |
| 340 | if eq op IR_SSTORE |
| 341 | put uses 3 c |
| 342 | ret 4 |
| 343 | end |
| 344 | ret 3 |
| 345 | end |
| 346 | if or eq op IR_FLOAD eq op IR_FSTORE |
| 347 | count = ulong 1 |
| 348 | if lt b 32768 |
| 349 | count = 2 |
| 350 | end |
| 351 | if eq op IR_FSTORE |
| 352 | put uses count c |
| 353 | count = add count 1 |
| 354 | end |
| 355 | ret count |
| 356 | end |
| 357 | if eq op IR_CALL |
| 358 | count = ulong get insn 16 |
| 359 | if gt count 6 |
| 360 | count = 6 |
| 361 | end |
| 362 | for i ulong count |
| 363 | put uses i ushort read_le insn add 18 mul i 2 2 |
| 364 | end |
| 365 | ret count |
| 366 | end |
| 367 | ret 0 |
| 368 | end |
| 369 | |
| 370 | export fn osc_ir_operands insn view uchar 48 uses edit ushort 6 to ulong |
| 371 | ret ir_uses insn uses |
| 372 | end |
| 373 | |
| 374 | fn reads_marked insn view uchar * marks view uchar * to bool |
| 375 | uses = new ushort 6 0 |
| 376 | count = ir_uses insn uses |
| 377 | for i ulong count |
| 378 | if ne get marks ulong get uses i 0 |
| 379 | ret true |
| 380 | end |
| 381 | end |
| 382 | ret false |
| 383 | end |
| 384 | |
| 385 | note These passes follow the validator's public refusal order. Collect all |
| 386 | note defining instructions first because a handle may occur in earlier blocks. |
| 387 | fn arena_use insn view uchar * arenas view uchar * to ulong |
| 388 | op = get insn 0 |
| 389 | dst = ir_def insn |
| 390 | if and ne op IR_AOPEN lt dst len arenas |
| 391 | if ne get arenas dst 0 |
| 392 | ret 3 |
| 393 | end |
| 394 | end |
| 395 | if eq op IR_AALLOC |
| 396 | handle = read_le insn 6 2 |
| 397 | init = read_le insn 4 2 |
| 398 | if or eq get arenas handle 0 ne get arenas init 0 |
| 399 | ret 4 |
| 400 | end |
| 401 | else |
| 402 | if eq op IR_ADESTROY |
| 403 | if eq get arenas read_le insn 4 2 0 |
| 404 | ret 5 |
| 405 | end |
| 406 | else |
| 407 | if reads_marked insn arenas |
| 408 | ret 6 |
| 409 | end |
| 410 | end |
| 411 | end |
| 412 | ret 0 |
| 413 | end |
| 414 | |
| 415 | fn check_store_value pools view uchar * types view uchar * pool ulong value ulong to ulong |
| 416 | if ne get pools value 0 |
| 417 | ret 14 |
| 418 | end |
| 419 | if ne get types value get pools pool |
| 420 | ret 14 |
| 421 | end |
| 422 | ret 0 |
| 423 | end |
| 424 | |
| 425 | fn pool_use insn view uchar * pools view uchar * types view uchar * to ulong |
| 426 | op = get insn 0 |
| 427 | dst = ir_def insn |
| 428 | if and ne op IR_POPEN lt dst len pools |
| 429 | if ne get pools dst 0 |
| 430 | ret 8 |
| 431 | end |
| 432 | end |
| 433 | a = read_le insn 4 2 |
| 434 | b = read_le insn 6 2 |
| 435 | if eq op IR_PCLOSE |
| 436 | if eq get pools a 0 |
| 437 | ret 9 |
| 438 | end |
| 439 | ret 0 |
| 440 | end |
| 441 | if eq op IR_HALLOC |
| 442 | if or eq get pools b 0 ne get pools a 0 |
| 443 | ret 10 |
| 444 | end |
| 445 | if ne get types a get pools b |
| 446 | ret 11 |
| 447 | end |
| 448 | ret 0 |
| 449 | end |
| 450 | if and ge op IR_HGEN le op IR_HSTORE |
| 451 | if or eq get pools a 0 ne get pools b 0 |
| 452 | ret 12 |
| 453 | end |
| 454 | if ne op IR_HGEN |
| 455 | if ne get pools read_le insn 8 2 0 |
| 456 | ret 12 |
| 457 | end |
| 458 | end |
| 459 | if eq op IR_HLOAD |
| 460 | if ne get types dst get pools a |
| 461 | ret 13 |
| 462 | end |
| 463 | end |
| 464 | if eq op IR_HSTORE |
| 465 | ret check_store_value pools types a read_le insn 18 2 |
| 466 | end |
| 467 | ret 0 |
| 468 | end |
| 469 | if reads_marked insn pools |
| 470 | ret 15 |
| 471 | end |
| 472 | ret 0 |
| 473 | end |
| 474 | |
| 475 | fn slice_defs insns view uchar * slices view uchar 480 to ulong |
| 476 | for i ulong div len insns ir_insn_size |
| 477 | dst = ir_def part insns mul i ir_insn_size ir_insn_size |
| 478 | if lt dst len slices |
| 479 | if ne get slices dst 0 |
| 480 | ret or shl i 8 1 |
| 481 | end |
| 482 | end |
| 483 | end |
| 484 | ret 0 |
| 485 | end |
| 486 | |
| 487 | fn check_arenas insns view uchar * arenas edit uchar 480 params ulong to ulong |
| 488 | for i ulong div len insns ir_insn_size |
| 489 | start = mul i ir_insn_size |
| 490 | if eq get insns start IR_AOPEN |
| 491 | dst = read_le insns add start 2 2 |
| 492 | if or ne get arenas dst 0 lt dst params |
| 493 | ret or shl i 8 2 |
| 494 | end |
| 495 | put arenas dst 1 |
| 496 | end |
| 497 | end |
| 498 | for i ulong div len insns ir_insn_size |
| 499 | status = arena_use part insns mul i ir_insn_size ir_insn_size arenas |
| 500 | if ne status 0 |
| 501 | ret or shl i 8 status |
| 502 | end |
| 503 | end |
| 504 | ret 0 |
| 505 | end |
| 506 | |
| 507 | fn check_pools insns view uchar * arenas view uchar 480 types view uchar 480 params ulong to ulong |
| 508 | pools = new uchar 480 0 |
| 509 | for i ulong div len insns ir_insn_size |
| 510 | start = mul i ir_insn_size |
| 511 | if eq get insns start IR_POPEN |
| 512 | dst = read_le insns add start 2 2 |
| 513 | used = or get pools dst get arenas dst |
| 514 | if or ne used 0 lt dst params |
| 515 | ret or shl i 8 7 |
| 516 | end |
| 517 | put pools dst get insns add start 1 |
| 518 | end |
| 519 | end |
| 520 | for i ulong div len insns ir_insn_size |
| 521 | status = pool_use part insns mul i ir_insn_size ir_insn_size pools types |
| 522 | if ne status 0 |
| 523 | ret or shl i 8 status |
| 524 | end |
| 525 | end |
| 526 | ret 0 |
| 527 | end |
| 528 | |
| 529 | note Return the first refused instruction above the low diagnostic byte. |
| 530 | export fn osc_ir_resources insns view uchar * types view uchar 480 slices view uchar 480 params ulong to ulong |
| 531 | requires eq rem len insns ir_insn_size 0 |
| 532 | arenas = new uchar 480 0 |
| 533 | status = slice_defs insns slices |
| 534 | if ne status 0 |
| 535 | ret status |
| 536 | end |
| 537 | status = check_arenas insns arenas params |
| 538 | if ne status 0 |
| 539 | ret status |
| 540 | end |
| 541 | ret check_pools insns arenas types params |
| 542 | end |
| 543 | |
| 544 | fn mark_def bits edit ulong * dst ulong to void |
| 545 | if ne dst NO_REG |
| 546 | slot = div dst 64 |
| 547 | mask = shl ulong 1 rem dst 64 |
| 548 | value = or get bits slot mask |
| 549 | put bits slot value |
| 550 | end |
| 551 | end |
| 552 | |
| 553 | fn register_gen insns view uchar * bits edit ulong * to void |
| 554 | for j ulong len bits |
| 555 | put bits j 0 |
| 556 | end |
| 557 | for j ulong div len insns ir_insn_size |
| 558 | dst = ir_def part insns mul j ir_insn_size ir_insn_size |
| 559 | mark_def bits dst |
| 560 | end |
| 561 | end |
| 562 | |
| 563 | fn record_edges term view uchar * record edit ulong * absent_block ulong to void |
| 564 | width = sub len record 2 |
| 565 | first = absent_block |
| 566 | other = absent_block |
| 567 | if ne get term 0 ir_ret |
| 568 | first = read_le term 10 2 |
| 569 | if eq get term 0 IR_CBR |
| 570 | other = read_le term 12 2 |
| 571 | end |
| 572 | end |
| 573 | put record width first |
| 574 | put record add width 1 other |
| 575 | end |
| 576 | |
| 577 | export fn osc_ir_block_transfer insns view uchar * record edit ulong * blocks ulong to void |
| 578 | register_gen insns part record 0 sub len record 2 |
| 579 | record_edges part insns sub len insns ir_insn_size ir_insn_size record blocks |
| 580 | end |
| 581 | |
| 582 | fn first_missing insn view uchar * available view ulong * to ulong |
| 583 | uses = new ushort 6 0 |
| 584 | count = ir_uses insn uses |
| 585 | for i ulong count |
| 586 | reg = ulong get uses i |
| 587 | word = get available div reg 64 |
| 588 | if eq and shr word rem reg 64 1 0 |
| 589 | ret add reg 1 |
| 590 | end |
| 591 | end |
| 592 | ret 0 |
| 593 | end |
| 594 | |
| 595 | export fn osc_ir_block_reads insns view uchar * available edit ulong * to ulong |
| 596 | for j ulong div len insns ir_insn_size |
| 597 | base = mul j ir_insn_size |
| 598 | missing = first_missing part insns base ir_insn_size available |
| 599 | if ne missing 0 |
| 600 | ret or shl j 16 missing |
| 601 | end |
| 602 | dst = ir_def part insns base ir_insn_size |
| 603 | mark_def available dst |
| 604 | end |
| 605 | ret 0 |
| 606 | end |
| 607 | |
| 608 | note Version 6 structured contracts are postfix programs over value scalars. |
| 609 | note The scalar type tags and term discriminants come from osc_ir.h. |
| 610 | const CT_PARAM uchar 1 |
| 611 | const CT_RESULT uchar 2 |
| 612 | const CT_CONST uchar 3 |
| 613 | const CT_BIN uchar 4 |
| 614 | const CT_UN uchar 5 |
| 615 | const CT_CMP uchar 6 |
| 616 | const CT_CAST uchar 7 |
| 617 | const T_BOOL uchar 1 |
| 618 | const T_U8 uchar 2 |
| 619 | const T_U64 uchar 5 |
| 620 | const T_I8 uchar 6 |
| 621 | const T_I64 uchar 9 |
| 622 | |
| 623 | export fn osc_ir_canonical scalar ulong value ulong to ulong |
| 624 | if eq scalar ulong T_BOOL |
| 625 | if lt value 2 |
| 626 | ret 1 |
| 627 | end |
| 628 | ret 0 |
| 629 | end |
| 630 | if or lt scalar ulong T_U8 gt scalar ulong T_I64 |
| 631 | ret 0 |
| 632 | end |
| 633 | width = shl ulong 8 and sub scalar ulong T_U8 3 |
| 634 | if eq width 64 |
| 635 | ret 1 |
| 636 | end |
| 637 | limit = shl ulong 1 width |
| 638 | bits = and value sub limit 1 |
| 639 | if ge scalar ulong T_I8 |
| 640 | sign = shr limit 1 |
| 641 | bits = sub xor bits sign sign |
| 642 | end |
| 643 | if eq bits value |
| 644 | ret 1 |
| 645 | end |
| 646 | ret 0 |
| 647 | end |
| 648 | |
| 649 | fn term_operator op uchar subop uchar stack edit uchar 64 depth ulong to ulong |
| 650 | if eq depth 0 |
| 651 | ret 0 |
| 652 | end |
| 653 | b = get stack sub depth 1 |
| 654 | if eq op CT_UN |
| 655 | if eq subop 3 |
| 656 | if eq b T_BOOL |
| 657 | ret depth |
| 658 | end |
| 659 | else |
| 660 | if and ne b T_BOOL or eq subop 1 eq subop 2 |
| 661 | ret depth |
| 662 | end |
| 663 | end |
| 664 | ret 0 |
| 665 | end |
| 666 | if eq op CT_CAST |
| 667 | if or eq b T_BOOL or lt subop T_U8 gt subop T_I64 |
| 668 | ret 0 |
| 669 | end |
| 670 | put stack sub depth 1 subop |
| 671 | ret depth |
| 672 | end |
| 673 | if lt depth 2 |
| 674 | ret 0 |
| 675 | end |
| 676 | a = get stack sub depth 2 |
| 677 | if eq op CT_CMP |
| 678 | if or ne a b or lt subop 1 gt subop 6 |
| 679 | ret 0 |
| 680 | end |
| 681 | put stack sub depth 2 T_BOOL |
| 682 | ret sub depth 1 |
| 683 | end |
| 684 | if or ne op CT_BIN or lt subop 1 gt subop 10 |
| 685 | ret 0 |
| 686 | end |
| 687 | if ge subop 9 |
| 688 | if or eq a T_BOOL eq b T_BOOL |
| 689 | ret 0 |
| 690 | end |
| 691 | else |
| 692 | if or ne a b and lt subop 6 eq a T_BOOL |
| 693 | ret 0 |
| 694 | end |
| 695 | end |
| 696 | ret sub depth 1 |
| 697 | end |
| 698 | |
| 699 | fn contract_term term view uchar * params view uchar 256 result uchar stack edit uchar 64 depth ulong to ulong |
| 700 | op = get term 0 |
| 701 | subop = get term 1 |
| 702 | arg = ulong get term 2 |
| 703 | imm = read_le term 8 8 |
| 704 | if eq op CT_PARAM |
| 705 | if or ne subop 0 or ne imm 0 ge arg len params |
| 706 | ret 0 |
| 707 | end |
| 708 | ty = get params arg |
| 709 | if eq ty 0 |
| 710 | ret 0 |
| 711 | end |
| 712 | put stack depth ty |
| 713 | ret add depth 1 |
| 714 | end |
| 715 | if ne arg 0 |
| 716 | ret 0 |
| 717 | end |
| 718 | if eq op CT_CONST |
| 719 | if eq osc_ir_canonical ulong subop imm 0 |
| 720 | ret 0 |
| 721 | end |
| 722 | put stack depth subop |
| 723 | ret add depth 1 |
| 724 | end |
| 725 | if ne imm 0 |
| 726 | ret 0 |
| 727 | end |
| 728 | if eq op CT_RESULT |
| 729 | if or ne subop 0 eq result 0 |
| 730 | ret 0 |
| 731 | end |
| 732 | put stack depth result |
| 733 | ret add depth 1 |
| 734 | end |
| 735 | ret term_operator op subop stack depth |
| 736 | end |
| 737 | |
| 738 | export fn osc_ir_contract terms view uchar * params view uchar 256 result ulong to ulong |
| 739 | if eq len terms 0 |
| 740 | ret 1 |
| 741 | end |
| 742 | if or ne rem len terms 16 0 gt div len terms 16 64 |
| 743 | ret 0 |
| 744 | end |
| 745 | stack = new uchar 64 0 |
| 746 | depth = ulong 0 |
| 747 | for i ulong div len terms 16 |
| 748 | depth = contract_term part terms mul i 16 16 params uchar result stack depth |
| 749 | if eq depth 0 |
| 750 | ret 0 |
| 751 | end |
| 752 | end |
| 753 | if and eq depth 1 eq get stack 0 T_BOOL |
| 754 | ret 1 |
| 755 | end |
| 756 | ret 0 |
| 757 | end |
| 758 | |
| 759 | note Start a fresh IR instruction with absent register, target and callee |
| 760 | note fields. The result signals that the current block has terminated. |
| 761 | export fn osc_begin_insn record out uchar 48 opcode ulong line ulong to ulong |
| 762 | for i ulong 48 |
| 763 | put record i 0 |
| 764 | end |
| 765 | for i ulong 14 |
| 766 | put record add 2 i 255 |
| 767 | end |
| 768 | put record 0 uchar opcode |
| 769 | for i ulong 4 |
| 770 | put record add 40 i uchar and shr line mul i 8 255 |
| 771 | end |
| 772 | if and ge opcode ulong ir_br le opcode ulong ir_ret |
| 773 | ret 1 |
| 774 | end |
| 775 | ret 0 |
| 776 | end |