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 / omega_emit / typediff.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * typediff.c -- differential test input for omega_emit (omega#445): for every op of the op-list schema |
| 19 | * and every combination of operand types from a fixed list, builds a one-op unit in memory, asks the |
| 20 | * OSC IR validator and decoder (src/compiler/osc_ir.c) whether they accept it, and writes the same unit |
| 21 | * as an op-list for omega_emit: |
| 22 | * typediff <outdir> |
| 23 | * writes <outdir>/<id>.json for every case and <outdir>/<id>.oscir (canonical bytes) for every case |
| 24 | * the decoder accepts, and prints one line per case: "<id> ACCEPT|REFUSE". |
| 25 | * test_omega_emit.sh then requires: omega_emit emits every ACCEPT case byte-identically (and lift -> |
| 26 | * emit of its bytes is byte-identical), and refuses every REFUSE case. The unit: optional callee "g" |
| 27 | * (for call), then "f" whose params are the read operands (so every read register is written) and |
| 28 | * whose locals are the written operands; one block: the op, then ret. |
| 29 | */ |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | #include "osc_ir.h" |
| 35 | |
| 36 | typedef struct { const char *name; OscType t; } Ty; |
| 37 | #define SC(s) {OSC_T_##s, OSC_REF_NONE, OSC_T_VOID, 0, 0} |
| 38 | #define RF(k, e, n) {OSC_T_REF, OSC_REF_##k, OSC_T_##e, n, 0} |
| 39 | static const Ty TY[] = { |
| 40 | {"bool", SC(BOOL)}, {"u8", SC(U8)}, {"i32", SC(I32)}, {"u64", SC(U64)}, |
| 41 | {"own:u32:1", RF(OWN, U32, 1)}, {"shared:u32:1", RF(SHARED, U32, 1)}, {"mut:u32:1", RF(MUT, U32, 1)}, |
| 42 | {"own:u32:2", RF(OWN, U32, 2)}, {"shared:u8:1", RF(SHARED, U8, 1)}, {"u32", SC(U32)}, |
| 43 | }; |
| 44 | #define NT ((int)(sizeof TY / sizeof TY[0])) |
| 45 | |
| 46 | static OscUnit U; |
| 47 | static const char *g_out; |
| 48 | static int g_n; |
| 49 | |
| 50 | typedef struct { |
| 51 | const char *op; /* schema op name */ |
| 52 | uint8_t code, sub; |
| 53 | int nread, write; /* read operand registers (params), written register (local) */ |
| 54 | const char *rkey[3]; /* JSON keys of the read operands, in order */ |
| 55 | } Shape; |
| 56 | |
| 57 | static void put_type(FILE *f, const Ty *t) { fprintf(f, "\"%s\"", t->name); } |
| 58 | |
| 59 | /* one case: types[0..nread-1] = read operands, wt = written type or -1; extra = op-specific */ |
| 60 | static void emit_case(const Shape *s, const int *rt, int wt, int pt, int rett, int fret) |
| 61 | { |
| 62 | memset(&U, 0, sizeof U); |
| 63 | int fi = 0; |
| 64 | char id[96]; |
| 65 | snprintf(id, sizeof id, "%s", s->op); |
| 66 | for (int k = 0; k < s->nread; k++) snprintf(id + strlen(id), sizeof id - strlen(id), "_%d", rt[k]); |
| 67 | if (wt >= 0) snprintf(id + strlen(id), sizeof id - strlen(id), "_w%d", wt); |
| 68 | if (pt >= 0) snprintf(id + strlen(id), sizeof id - strlen(id), "_p%d", pt); |
| 69 | if (rett >= 0) snprintf(id + strlen(id), sizeof id - strlen(id), "_r%d", rett); |
| 70 | if (fret >= 0) snprintf(id + strlen(id), sizeof id - strlen(id), "_f%d", fret); |
| 71 | char path[512]; |
| 72 | snprintf(path, sizeof path, "%s/%s.json", g_out, id); |
| 73 | FILE *j = fopen(path, "w"); |
| 74 | if (!j) { perror(path); exit(2); } |
| 75 | fprintf(j, "{\"functions\": ["); |
| 76 | if (!strcmp(s->op, "call")) { |
| 77 | /* g(p: TY[pt]) -> TY[rett] or void: one block returning a fresh constant of its return type */ |
| 78 | OscFunc *g = &U.funcs[0]; |
| 79 | strcpy(g->name, "g"); |
| 80 | g->nparams = 1; g->vtype[0] = TY[pt].t; g->nvregs = 1; |
| 81 | g->ret.s = OSC_T_VOID; |
| 82 | fprintf(j, "{\"name\": \"g\", \"params\": ["); put_type(j, &TY[pt]); fprintf(j, "], \"ret\": "); |
| 83 | if (rett >= 0) { |
| 84 | g->ret = TY[rett].t; g->vtype[1] = TY[rett].t; g->nvregs = 2; |
| 85 | g->insns[0] = (OscInsn){.op = OSC_I_CONST, .dst = 1, .a = -1, .b = -1, .c = -1, .blk_t = -1, .blk_f = -1, .callee = -1, .imm = 0}; |
| 86 | g->insns[1] = (OscInsn){.op = OSC_I_RET, .dst = -1, .a = 1, .b = -1, .c = -1, .blk_t = -1, .blk_f = -1, .callee = -1}; |
| 87 | g->ninsns = 2; |
| 88 | put_type(j, &TY[rett]); |
| 89 | fprintf(j, ", \"locals\": ["); put_type(j, &TY[rett]); |
| 90 | fprintf(j, "], \"blocks\": [[{\"op\": \"const\", \"dst\": 1, \"value\": %s}, {\"op\": \"ret\", \"a\": 1}]]}, ", |
| 91 | TY[rett].t.s == OSC_T_BOOL ? "false" : "0"); |
| 92 | } else { |
| 93 | g->insns[0] = (OscInsn){.op = OSC_I_RET, .dst = -1, .a = -1, .b = -1, .c = -1, .blk_t = -1, .blk_f = -1, .callee = -1}; |
| 94 | g->ninsns = 1; |
| 95 | fprintf(j, "\"void\", \"locals\": [], \"blocks\": [[{\"op\": \"ret\"}]]}, "); |
| 96 | } |
| 97 | g->nblocks = 1; g->blocks[0] = (OscBlock){0, g->ninsns}; |
| 98 | fi = 1; |
| 99 | } |
| 100 | OscFunc *f = &U.funcs[fi]; |
| 101 | U.nfuncs = (uint16_t)(fi + 1); |
| 102 | strcpy(f->name, "f"); |
| 103 | f->nparams = (uint8_t)s->nread; |
| 104 | for (int k = 0; k < s->nread; k++) f->vtype[k] = TY[rt[k]].t; |
| 105 | f->nvregs = (uint16_t)s->nread; |
| 106 | int dst = -1; |
| 107 | if (wt >= 0) { dst = f->nvregs; f->vtype[f->nvregs++] = TY[wt].t; } |
| 108 | f->ret.s = OSC_T_VOID; |
| 109 | if (fret >= 0) f->ret = TY[fret].t; |
| 110 | OscInsn in = {.op = s->code, .sub = s->sub, .dst = (int16_t)dst, .a = -1, .b = -1, .c = -1, .blk_t = -1, .blk_f = -1, .callee = -1}; |
| 111 | int16_t *slot[3] = {&in.a, &in.b, &in.c}; |
| 112 | for (int k = 0; k < s->nread; k++) *slot[k] = (int16_t)k; |
| 113 | if (s->code == OSC_I_CALL) { in.callee = 0; in.nargs = 1; in.args[0] = 0; in.a = -1; } |
| 114 | if (s->code == OSC_I_CBR) { in.blk_t = 1; in.blk_f = 1; } |
| 115 | f->insns[0] = in; |
| 116 | int n = 1; |
| 117 | if (s->code != OSC_I_RET && s->code != OSC_I_CBR) { |
| 118 | f->insns[n++] = (OscInsn){.op = OSC_I_RET, .dst = -1, .a = -1, .b = -1, .c = -1, .blk_t = -1, .blk_f = -1, .callee = -1}; |
| 119 | } |
| 120 | f->ninsns = (uint32_t)n; |
| 121 | f->blocks[0] = (OscBlock){0, (uint32_t)n}; |
| 122 | f->nblocks = 1; |
| 123 | if (s->code == OSC_I_CBR) { |
| 124 | f->insns[1] = (OscInsn){.op = OSC_I_RET, .dst = -1, .a = -1, .b = -1, .c = -1, .blk_t = -1, .blk_f = -1, .callee = -1}; |
| 125 | f->ninsns = 2; f->blocks[0] = (OscBlock){0, 1}; f->blocks[1] = (OscBlock){1, 1}; f->nblocks = 2; |
| 126 | } |
| 127 | /* JSON of f */ |
| 128 | fprintf(j, "{\"name\": \"f\", \"params\": ["); |
| 129 | for (int k = 0; k < s->nread; k++) { if (k) fprintf(j, ", "); put_type(j, &TY[rt[k]]); } |
| 130 | fprintf(j, "], \"ret\": "); |
| 131 | if (fret >= 0) put_type(j, &TY[fret]); else fprintf(j, "\"void\""); |
| 132 | fprintf(j, ", \"locals\": ["); |
| 133 | if (wt >= 0) put_type(j, &TY[wt]); |
| 134 | fprintf(j, "], \"blocks\": [[{\"op\": \"%s\"", s->op); |
| 135 | if (s->code == OSC_I_CONST) fprintf(j, ", \"dst\": %d, \"value\": 0", dst); |
| 136 | else if (s->code == OSC_I_CALL) { |
| 137 | fprintf(j, ", \"func\": \"g\", \"args\": [0]"); |
| 138 | if (wt >= 0) fprintf(j, ", \"dst\": %d", dst); |
| 139 | } else { |
| 140 | if (wt >= 0) fprintf(j, ", \"dst\": %d", dst); |
| 141 | for (int k = 0; k < s->nread; k++) fprintf(j, ", \"%s\": %d", s->rkey[k], k); |
| 142 | } |
| 143 | if (s->code == OSC_I_CBR) fprintf(j, ", \"then\": 1, \"else\": 1}], [{\"op\": \"ret\"}]]}"); |
| 144 | else if (s->code == OSC_I_RET) fprintf(j, "}]]}"); |
| 145 | else fprintf(j, "}, {\"op\": \"ret\"}]]}"); |
| 146 | fprintf(j, "]}\n"); |
| 147 | fclose(j); |
| 148 | |
| 149 | static uint8_t buf[OSC_IR_ENCODED_MAX]; |
| 150 | static OscUnit back; |
| 151 | size_t len = 0; |
| 152 | int ok = !osc_ir_encode(&U, buf, sizeof buf, &len) && !osc_ir_decode(buf, len, &back); |
| 153 | if (ok) { |
| 154 | snprintf(path, sizeof path, "%s/%s.oscir", g_out, id); |
| 155 | FILE *b = fopen(path, "wb"); |
| 156 | if (!b || fwrite(buf, 1, len, b) != len || fclose(b)) { perror(path); exit(2); } |
| 157 | } |
| 158 | printf("%s %s\n", id, ok ? "ACCEPT" : "REFUSE"); |
| 159 | g_n++; |
| 160 | } |
| 161 | |
| 162 | int main(int argc, char **argv) |
| 163 | { |
| 164 | if (argc != 2) { fprintf(stderr, "usage: typediff <outdir>\n"); return 2; } |
| 165 | g_out = argv[1]; |
| 166 | static const Shape S[] = { |
| 167 | {"mov", OSC_I_MOV, 0, 1, 1, {"a"}}, |
| 168 | {"add", OSC_I_BIN, OSC_B_ADD, 2, 1, {"a", "b"}}, {"div", OSC_I_BIN, OSC_B_DIV, 2, 1, {"a", "b"}}, |
| 169 | {"and", OSC_I_BIN, OSC_B_AND, 2, 1, {"a", "b"}}, {"shl", OSC_I_BIN, OSC_B_SHL, 2, 1, {"a", "b"}}, |
| 170 | {"neg", OSC_I_UN, OSC_U_NEG, 1, 1, {"a"}}, {"bnot", OSC_I_UN, OSC_U_BNOT, 1, 1, {"a"}}, |
| 171 | {"not", OSC_I_UN, OSC_U_LNOT, 1, 1, {"a"}}, |
| 172 | {"eq", OSC_I_CMP, OSC_C_EQ, 2, 1, {"a", "b"}}, {"lt", OSC_I_CMP, OSC_C_LT, 2, 1, {"a", "b"}}, |
| 173 | {"cast", OSC_I_CAST, 0, 1, 1, {"a"}}, |
| 174 | {"alloc", OSC_I_ALLOC, 0, 1, 1, {"init"}}, |
| 175 | {"release", OSC_I_RELEASE, 0, 1, 0, {"arr"}}, |
| 176 | {"load", OSC_I_LOAD, 0, 2, 1, {"arr", "index"}}, |
| 177 | {"store", OSC_I_STORE, 0, 3, 0, {"arr", "index", "value"}}, |
| 178 | {"cbr", OSC_I_CBR, 0, 1, 0, {"cond"}}, |
| 179 | {"ret", OSC_I_RET, 0, 1, 0, {"a"}}, |
| 180 | }; |
| 181 | for (size_t si = 0; si < sizeof S / sizeof S[0]; si++) { |
| 182 | const Shape *s = &S[si]; |
| 183 | int rt[3] = {0, 0, 0}; |
| 184 | int nw = s->write ? NT : 1; |
| 185 | int total = 1; |
| 186 | for (int k = 0; k < s->nread; k++) total *= NT; |
| 187 | for (int c = 0; c < total; c++) { |
| 188 | int x = c; |
| 189 | for (int k = 0; k < s->nread; k++) { rt[k] = x % NT; x /= NT; } |
| 190 | if (s->code == OSC_I_RET) { |
| 191 | for (int fr = 0; fr < NT; fr++) if (TY[fr].t.s != OSC_T_REF) emit_case(s, rt, -1, -1, -1, fr); |
| 192 | continue; |
| 193 | } |
| 194 | for (int w = 0; w < nw; w++) emit_case(s, rt, s->write ? w : -1, -1, -1, -1); |
| 195 | } |
| 196 | } |
| 197 | /* const: every destination type */ |
| 198 | static const Shape K = {"const", OSC_I_CONST, 0, 0, 1, {0}}; |
| 199 | for (int w = 0; w < NT; w++) { |
| 200 | /* JSON value 0 / false; refs must be refused by both */ |
| 201 | int rt[3] = {0}; |
| 202 | emit_case(&K, rt, w, -1, -1, -1); |
| 203 | } |
| 204 | /* call: parameter type x argument type, and return type x dst type (or void) */ |
| 205 | static const Shape C = {"call", OSC_I_CALL, 0, 1, 1, {0}}; |
| 206 | static const Shape CV = {"call", OSC_I_CALL, 0, 1, 0, {0}}; |
| 207 | for (int p = 0; p < NT; p++) |
| 208 | for (int a = 0; a < NT; a++) { |
| 209 | int rt[3] = {a}; |
| 210 | emit_case(&CV, rt, -1, p, -1, -1); |
| 211 | } |
| 212 | for (int r = 0; r < NT; r++) { |
| 213 | if (TY[r].t.s == OSC_T_REF) continue; |
| 214 | for (int w = 0; w < NT; w++) { |
| 215 | int rt[3] = {9}; /* argument u32 to a u32 parameter */ |
| 216 | emit_case(&C, rt, w, 9, r, -1); |
| 217 | } |
| 218 | } |
| 219 | fprintf(stderr, "typediff: %d cases\n", g_n); |
| 220 | return 0; |
| 221 | } |