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 / tools / exp445 / arms / bytes.md
| 1 | REPRESENTATION: bytes |
| 2 | |
| 3 | Write the program as OSC binary IR (spec/one-layer-format.md), spelled as hexadecimal: two hex |
| 4 | digits per byte (0-9, a-f or A-F); spaces and line breaks between bytes are ignored; nothing else |
| 5 | is allowed. The bytes are read by a strict decoder: any byte out of place refuses the whole program. |
| 6 | |
| 7 | All integers are little-endian, no padding. Use format version 1 (no struct table). |
| 8 | |
| 9 | Unit: |
| 10 | 7 bytes magic 4f 53 43 31 49 52 00 |
| 11 | 1 byte version: 01 |
| 12 | 2 bytes number of functions, 1..32, then each function record |
| 13 | |
| 14 | Function record: |
| 15 | 1 + n name: length n (1..63), then n ASCII bytes [A-Za-z_][A-Za-z0-9_]*, unique in the unit |
| 16 | 1 number of parameters P (0..6) |
| 17 | type return type |
| 18 | 2 number of registers R (P..480), then R types: registers 0..P-1 are the parameters in |
| 19 | order, registers P..R-1 are locals |
| 20 | 2 number of blocks B (1..512), then per block: 4 bytes index of its first instruction, |
| 21 | 4 bytes instruction count. Blocks cover the instructions in order without gaps: |
| 22 | block 0 starts at 0, each next block starts where the previous ended. Block 0 is the entry. |
| 23 | 4 number of instructions, then the instructions |
| 24 | 2 00 00 (empty "requires" text) |
| 25 | 2 00 00 (empty "ensures" text) |
| 26 | |
| 27 | Type: one byte: 0 void (return type only), 1 bool, 2 u8, 3 u16, 4 u32, 5 u64, 6 i8, 7 i16, 8 i32, |
| 28 | 9 i64; or an array reference: 0a, then kind (1 own, 2 shared, 3 mut), element type byte (1..9), |
| 29 | 2 bytes length (1..64). |
| 30 | |
| 31 | Instruction: one opcode byte, then its fields. r = 2-byte register / block / function index; |
| 32 | ff ff means "none". |
| 33 | 01 CONST r dst, 8 bytes value (the value as a 64-bit number: unsigned zero-extended, signed |
| 34 | sign-extended two's complement, bool 0 or 1) |
| 35 | 02 MOV r dst, r a dst = a |
| 36 | 03 BIN 1 sub, r dst, r a, r b sub: 1 add 2 sub 3 mul 4 div 5 rem 6 and 7 or 8 xor 9 shl 10 shr |
| 37 | 04 UN 1 sub, r dst, r a sub: 1 neg 2 bitwise-not 3 logical-not (bool) |
| 38 | 05 CMP 1 sub, r dst, r a, r b sub: 1 eq 2 ne 3 lt 4 le 5 gt 6 ge; dst is bool |
| 39 | 06 CAST r dst, r a dst = a converted to dst's integer type |
| 40 | 07 ALLOC r dst, r a dst (own array) = new array, every element = a |
| 41 | 08 RELEASE r a give back own array a |
| 42 | 09 LOAD r dst, r a, r b dst = a[b] |
| 43 | 0a STORE r a, r b, r c a[b] = c (a is own or mut) |
| 44 | 0b CALL r dst (ff ff for a void callee), r function index (an EARLIER function), 1 nargs, |
| 45 | nargs x r arg |
| 46 | 0c TRAP 1 code (1..14) stop with that trap |
| 47 | 0d BR r block continue at block |
| 48 | 0e CBR r a (bool), r then-block, r else-block |
| 49 | 0f RET r a (ff ff in a void function) |
| 50 | |
| 51 | Rules: every block ends with exactly one BR, CBR or RET, and only its last instruction is one |
| 52 | (a TRAP does not end a block). Types agree (BIN/CMP: a and b the same type; BIN and UN: dst the |
| 53 | same type as a). Every register is read only after it is written on every path to the read |
| 54 | (parameters count as written). No recursion: a function calls only functions before it. |
| 55 | |
| 56 | Example. Task: function clamp_sub, parameters u32 u32, returns u32: a - b when a >= b, else 0. |
| 57 | Registers: 0 a (u32), 1 b (u32), 2 bool, 3 u32, 4 u32. |
| 58 | |
| 59 | ``` |
| 60 | 4f5343314952000101000963 6c616d705f737562 |
| 61 | 02 04 0500 04 04 01 04 04 |
| 62 | 0300 00000000 02000000 02000000 02000000 04000000 02000000 |
| 63 | 06000000 |
| 64 | 05 06 0200 0000 0100 |
| 65 | 0e 0200 0100 0200 |
| 66 | 03 02 0300 0000 0100 |
| 67 | 0f 0300 |
| 68 | 01 0400 0000000000000000 |
| 69 | 0f 0400 |
| 70 | 0000 0000 |
| 71 | ``` |
| 72 | |
| 73 | (Line by line: magic, version 1, one function, name "clamp_sub"; 2 params, returns u32, |
| 74 | 5 registers u32 u32 bool u32 u32; 3 blocks at 0+2, 2+2, 4+2; 6 instructions; CMP ge r2 = r0 >= r1; |
| 75 | CBR r2 -> block 1 else block 2; BIN sub r3 = r0 - r1; RET r3; CONST r4 = 0; RET r4; empty contracts.) |