interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / language / omega_lower.c

507 lines · 23220 bytes Raw Blame History
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/* omega_lower.c -- Omega surface language V0 lowering. See omega_lower.h.
18 *
19 * Builds objects only through the existing omega_build_* / omega_program_*
20 * builders. No evaluator lives here. */
21#include "omega_lower.h"
22#include "omega_core.h"
23#include "omega_canonical.h"
24
25#include <stdio.h>
26#include <string.h>
27
28#define V0_OVERFLOW OVERFLOW_WRAP /* matches src/omega_realize.c for u64 ops */
29#define V0_MAX_PROGRAM_STEPS 32
30#define V0_MAX_PROGRAM_IMM 0xFFFFFFFFull /* build_unary_op loads MOVZ + one MOVK */
31
32typedef struct {
33 OmegaGraph *g;
34 const VisorBindings *b;
35 const VisorBinding *last;
36 const OmegaAst *ast;
37 char *err;
38 size_t n;
39} Lower;
40
41static int lfail(char *err, size_t n, uint32_t col, int rc, const char *msg) {
42 if (err && n) snprintf(err, n, "column %u: %s", (unsigned)col, msg);
43 return rc;
44}
45
46static bool same_type(const OmegaLangType *a, const OmegaLangType *b) {
47 return a->tag == b->tag && a->width == b->width;
48}
49
50/* If the object just appended equals an earlier one, drop it and reuse the earlier. */
51static OmegaObject *dedupe(OmegaGraph *g, OmegaObject *o) {
52 if (!o || !g->object_count) return o;
53 for (uint16_t i = 0; i + 1 < g->object_count; i++) {
54 if (g->objects[i].has_id && omega_compare_semantic_id(&g->objects[i].id, &o->id) == 0) {
55 g->object_count--;
56 return &g->objects[i];
57 }
58 }
59 return o;
60}
61
62static int capacity_fail(Lower *L, uint32_t col) {
63 return lfail(L->err, L->n, col, -3, "the session graph is full (256 objects); use `clear` to start over");
64}
65
66static int build_type(Lower *L, const OmegaLangType *t, uint32_t col, SemanticId *out) {
67 OmegaObject *o = (t->tag == TYPE_BOOL) ? omega_build_type_bool(L->g)
68 : omega_build_type_uint(L->g, t->width);
69 o = dedupe(L->g, o);
70 if (!o || !o->has_id) return capacity_fail(L, col);
71 *out = o->id;
72 return 0;
73}
74
75/* Read the V0 type of an existing object: VALUE -> its type, APPLY -> its op's output type. */
76static int type_of_object(const OmegaGraph *g, const SemanticId *id, OmegaLangType *out, SemanticId *type_id) {
77 const OmegaObject *o = omega_graph_find_object_const(g, id);
78 if (!o) return -1;
79 SemanticId tid;
80 if (o->kind == KIND_VALUE && o->payload_len == sizeof(ValuePayload)) {
81 ValuePayload vp;
82 memcpy(&vp, o->payload, sizeof vp);
83 tid = vp.type_id;
84 } else if (o->kind == KIND_OPERATION && o->payload_len == sizeof(ApplyPayload)) {
85 ApplyPayload ap;
86 memcpy(&ap, o->payload, sizeof ap);
87 const OmegaObject *op = omega_graph_find_object_const(g, &ap.op_id);
88 if (!op || op->kind != KIND_OPERATION || op->payload_len != sizeof(OperationPayload)) return -1;
89 OperationPayload opp;
90 memcpy(&opp, op->payload, sizeof opp);
91 tid = opp.output_type;
92 } else {
93 return -1;
94 }
95 const OmegaObject *t = omega_graph_find_object_const(g, &tid);
96 if (!t || t->kind != KIND_TYPE || t->payload_len != sizeof(TypePayload)) return -1;
97 TypePayload tp;
98 memcpy(&tp, t->payload, sizeof tp);
99 if (tp.tag == TYPE_BOOL) { out->tag = TYPE_BOOL; out->width = 1; }
100 else if (tp.tag == TYPE_UNSIGNED_INT &&
101 (tp.width == 8 || tp.width == 16 || tp.width == 32 || tp.width == 64)) {
102 out->tag = TYPE_UNSIGNED_INT; out->width = tp.width;
103 } else {
104 return -1;
105 }
106 if (type_id) *type_id = tid;
107 return 0;
108}
109
110static int resolve_name(Lower *L, const OmegaAstNode *nd, SemanticId *id, OmegaLangType *ty) {
111 const VisorBinding *vb = NULL;
112 char msg[200];
113 if (strcmp(nd->name, "_") == 0) {
114 vb = L->last;
115 } else if (L->b) {
116 for (size_t i = 0; i < L->b->count && i < VISOR_MAX_BINDINGS; i++) {
117 if (strcmp(L->b->items[i].name, nd->name) == 0) { vb = &L->b->items[i]; break; }
118 }
119 }
120 if (!vb) {
121 snprintf(msg, sizeof msg, "unknown name '%s' (define it first, e.g. let %s: u64 = 7)", nd->name, nd->name);
122 return lfail(L->err, L->n, nd->col, -2, msg);
123 }
124 if (vb->kind == VISOR_BIND_PROGRAM) {
125 snprintf(msg, sizeof msg, "'%s' is a program; using programs inside expressions is not supported in V0", nd->name);
126 return lfail(L->err, L->n, nd->col, -2, msg);
127 }
128 if (vb->kind != VISOR_BIND_OBJECT) {
129 snprintf(msg, sizeof msg, "'%s' is not a value (only integer and bool values can be used in V0)", nd->name);
130 return lfail(L->err, L->n, nd->col, -2, msg);
131 }
132 if (type_of_object(L->g, &vb->id, ty, NULL) != 0) {
133 snprintf(msg, sizeof msg, "'%s' does not name a V0 integer or bool value in this graph", nd->name);
134 return lfail(L->err, L->n, nd->col, -2, msg);
135 }
136 *id = vb->id;
137 return 0;
138}
139
140static bool is_comparison(OmegaTokKind k) {
141 return k == OTOK_EQEQ || k == OTOK_NE || k == OTOK_LT || k == OTOK_LE || k == OTOK_GT || k == OTOK_GE;
142}
143
144static int comparison_fail(Lower *L, const OmegaAstNode *nd) {
145 return lfail(L->err, L->n, nd->col, -2,
146 "comparisons are not supported in V0 expressions (the existing builder would type the result "
147 "as the operand type, not bool)");
148}
149
150/* 1 = type known, 0 = only untyped literals, <0 = error. */
151static int infer(Lower *L, int idx, OmegaLangType *out) {
152 const OmegaAstNode *nd = &L->ast->nodes[idx];
153 switch (nd->kind) {
154 case OAST_INT: return 0;
155 case OAST_BOOL: out->tag = TYPE_BOOL; out->width = 1; return 1;
156 case OAST_ASCRIBE: *out = nd->type; return 1;
157 case OAST_NAME: {
158 SemanticId id;
159 int rc = resolve_name(L, nd, &id, out);
160 return rc ? rc : 1;
161 }
162 case OAST_BINARY: {
163 if (is_comparison(nd->op)) return comparison_fail(L, nd);
164 int r = infer(L, nd->lhs, out);
165 if (r != 0) return r;
166 return infer(L, nd->rhs, out);
167 }
168 }
169 return lfail(L->err, L->n, nd->col, -1, "internal: unknown syntax node");
170}
171
172static OpCode opcode_of(OmegaTokKind k) {
173 switch (k) {
174 case OTOK_PLUS: return OP_ADD;
175 case OTOK_MINUS: return OP_SUB;
176 case OTOK_STAR: return OP_MUL;
177 case OTOK_SLASH: return OP_DIV;
178 case OTOK_AMP: return OP_AND;
179 case OTOK_PIPE: return OP_OR;
180 default: return OP_INVALID;
181 }
182}
183
184static int mismatch(Lower *L, uint32_t col, const char *what, const OmegaLangType *have, const OmegaLangType *want) {
185 char msg[200];
186 snprintf(msg, sizeof msg, "%s is %s but %s is required here (V0 never converts between types)",
187 what, omega_language_type_text(have), omega_language_type_text(want));
188 return lfail(L->err, L->n, col, -2, msg);
189}
190
191/* Is node idx a literal (possibly ascribed)? Returns its value. */
192static bool literal_value(const OmegaAst *a, int idx, uint64_t *v) {
193 const OmegaAstNode *nd = &a->nodes[idx];
194 if (nd->kind == OAST_ASCRIBE) nd = &a->nodes[nd->lhs];
195 if (nd->kind != OAST_INT) return false;
196 *v = nd->value;
197 return true;
198}
199
200static int lower_expr(Lower *L, int idx, const OmegaLangType *expected,
201 SemanticId *out_id, OmegaLangType *out_ty, SemanticId *out_tid) {
202 const OmegaAstNode *nd = &L->ast->nodes[idx];
203 int rc;
204 char msg[200];
205 switch (nd->kind) {
206 case OAST_INT: {
207 if (!expected)
208 return lfail(L->err, L->n, nd->col, -2,
209 "ambiguous width: a bare number has no type; write it as `7: u64` or `(7: u64)`");
210 if (expected->tag != TYPE_UNSIGNED_INT)
211 return lfail(L->err, L->n, nd->col, -2, "a number is used where a bool is required");
212 if (expected->width < 64 && (nd->value >> expected->width) != 0) {
213 snprintf(msg, sizeof msg, "literal %llu does not fit in %s",
214 (unsigned long long)nd->value, omega_language_type_text(expected));
215 return lfail(L->err, L->n, nd->col, -2, msg);
216 }
217 SemanticId tid;
218 if ((rc = build_type(L, expected, nd->col, &tid))) return rc;
219 OmegaObject *v = dedupe(L->g, omega_build_val_uint(L->g, &tid, expected->width, nd->value));
220 if (!v || !v->has_id) return capacity_fail(L, nd->col);
221 *out_id = v->id; *out_ty = *expected; *out_tid = tid;
222 return 0;
223 }
224 case OAST_BOOL: {
225 OmegaLangType bt = { TYPE_BOOL, 1 };
226 if (expected && !same_type(expected, &bt)) return mismatch(L, nd->col, "this value", &bt, expected);
227 SemanticId tid;
228 if ((rc = build_type(L, &bt, nd->col, &tid))) return rc;
229 OmegaObject *v = dedupe(L->g, omega_build_val_bool(L->g, &tid, nd->bval));
230 if (!v || !v->has_id) return capacity_fail(L, nd->col);
231 *out_id = v->id; *out_ty = bt; *out_tid = tid;
232 return 0;
233 }
234 case OAST_NAME: {
235 SemanticId id;
236 OmegaLangType ty;
237 if ((rc = resolve_name(L, nd, &id, &ty))) return rc;
238 if (expected && !same_type(expected, &ty)) {
239 snprintf(msg, sizeof msg, "'%s'", nd->name);
240 return mismatch(L, nd->col, msg, &ty, expected);
241 }
242 SemanticId tid;
243 if (type_of_object(L->g, &id, &ty, &tid) != 0)
244 return lfail(L->err, L->n, nd->col, -2, "internal: binding type vanished");
245 *out_id = id; *out_ty = ty; *out_tid = tid;
246 return 0;
247 }
248 case OAST_ASCRIBE:
249 if (expected && !same_type(expected, &nd->type))
250 return mismatch(L, nd->col, "this annotation", &nd->type, expected);
251 return lower_expr(L, nd->lhs, &nd->type, out_id, out_ty, out_tid);
252 case OAST_BINARY: {
253 if (is_comparison(nd->op)) return comparison_fail(L, nd);
254 OpCode opc = opcode_of(nd->op);
255 if (opc == OP_INVALID) return lfail(L->err, L->n, nd->col, -1, "internal: unknown operator");
256 OmegaLangType t;
257 if (expected) {
258 t = *expected;
259 } else {
260 int r = infer(L, idx, &t);
261 if (r < 0) return r;
262 if (r == 0)
263 return lfail(L->err, L->n, nd->col, -2,
264 "ambiguous width: no operand has a type; write e.g. `(1: u64) + 2`");
265 }
266 if (t.tag != TYPE_UNSIGNED_INT) {
267 snprintf(msg, sizeof msg, "'%s' on bool is not supported in V0 (integers only)",
268 omega_language_tok_text(nd->op));
269 return lfail(L->err, L->n, nd->col, -2, msg);
270 }
271 uint64_t dv;
272 if (opc == OP_DIV && literal_value(L->ast, nd->rhs, &dv) && dv == 0)
273 return lfail(L->err, L->n, nd->col, -2, "division by the constant 0 is undefined");
274 SemanticId lid, rid, ltid, rtid, tid;
275 OmegaLangType lt, rt;
276 if ((rc = lower_expr(L, nd->lhs, &t, &lid, &lt, &ltid))) return rc;
277 if ((rc = lower_expr(L, nd->rhs, &t, &rid, &rt, &rtid))) return rc;
278 if ((rc = build_type(L, &t, nd->col, &tid))) return rc;
279 OmegaObject *op = dedupe(L->g, omega_build_op_binary(L->g, opc, V0_OVERFLOW, &tid));
280 if (!op || !op->has_id) return capacity_fail(L, nd->col);
281 SemanticId op_id = op->id;
282 OmegaObject *ap = dedupe(L->g, omega_build_apply(L->g, &op_id, &lid, &rid));
283 if (!ap || !ap->has_id) return capacity_fail(L, nd->col);
284 *out_id = ap->id; *out_ty = t; *out_tid = tid;
285 return 0;
286 }
287 }
288 return lfail(L->err, L->n, nd->col, -1, "internal: unknown syntax node");
289}
290
291int omega_language_lower_to_graph(OmegaGraph *g, const VisorBindings *b, const VisorBinding *last,
292 const OmegaAst *ast, SemanticId *out_id, OmegaLangResult *info,
293 char *err, size_t err_len) {
294 if (err && err_len) err[0] = '\0';
295 if (!g || !ast || !out_id) return lfail(err, err_len, 0, -1, "internal: bad lowering arguments");
296 if (ast->stmt == OSTMT_FN)
297 return lfail(err, err_len, ast->stmt_col, -2, "fn lowers to a program, not a graph object");
298 if ((ast->stmt != OSTMT_EXPR && ast->stmt != OSTMT_LET) || ast->root < 0 ||
299 (size_t)ast->root >= ast->node_count)
300 return lfail(err, err_len, ast->stmt_col, -1, "nothing to lower");
301 Lower L = { g, b, last, ast, err, err_len };
302 uint16_t saved = g->object_count;
303 SemanticId id, tid;
304 OmegaLangType ty;
305 int rc = lower_expr(&L, ast->root, ast->stmt == OSTMT_LET ? &ast->let_type : NULL, &id, &ty, &tid);
306 if (rc) {
307 if (g->object_count > saved)
308 memset(&g->objects[saved], 0, (size_t)(g->object_count - saved) * sizeof(OmegaObject));
309 g->object_count = saved;
310 return rc;
311 }
312 *out_id = id;
313 if (info) {
314 info->kind = ast->stmt == OSTMT_LET ? OMEGA_LANG_BINDING : OMEGA_LANG_EXPRESSION;
315 info->id = id;
316 info->type_id = tid;
317 snprintf(info->type_text, sizeof info->type_text, "%s", omega_language_type_text(&ty));
318 }
319 return 0;
320}
321
322int omega_language_lower_program(const OmegaAst *ast, OmegaProgram *out, char *err, size_t err_len) {
323 if (err && err_len) err[0] = '\0';
324 if (!ast || !out) return lfail(err, err_len, 0, -1, "internal: bad lowering arguments");
325 if (ast->stmt != OSTMT_FN || ast->root < 0)
326 return lfail(err, err_len, ast->stmt_col, -1, "not a fn definition");
327 const OmegaLangType u64 = { TYPE_UNSIGNED_INT, 64 };
328 if (!same_type(&ast->param_type, &u64) || !same_type(&ast->ret_type, &u64))
329 return lfail(err, err_len, ast->stmt_col, -2,
330 "V0 functions must be (x: u64) -> u64 (the existing program model is fixed at 64-bit)");
331
332 struct { OpCode op; uint64_t imm; } steps[V0_MAX_PROGRAM_STEPS];
333 size_t count = 0;
334 int idx = ast->root;
335 char msg[200];
336 while (1) {
337 const OmegaAstNode *nd = &ast->nodes[idx];
338 if (nd->kind == OAST_ASCRIBE) {
339 if (!same_type(&nd->type, &u64))
340 return lfail(err, err_len, nd->col, -2, "inside a V0 function everything is u64");
341 idx = nd->lhs;
342 continue;
343 }
344 if (nd->kind != OAST_BINARY) break;
345 if (is_comparison(nd->op))
346 return lfail(err, err_len, nd->col, -2, "comparisons are not supported in V0 function bodies");
347 if (nd->op == OTOK_SLASH)
348 return lfail(err, err_len, nd->col, -2,
349 "'/' is not in the program model's operation set (+ - * & |)");
350 OpCode opc = opcode_of(nd->op);
351 bool commutative = (opc == OP_ADD || opc == OP_MUL || opc == OP_AND || opc == OP_OR);
352 uint64_t imm;
353 int next_idx;
354 const OmegaAstNode *r = &ast->nodes[nd->rhs], *l = &ast->nodes[nd->lhs];
355 if ((r->kind == OAST_ASCRIBE && !same_type(&r->type, &u64)) ||
356 (l->kind == OAST_ASCRIBE && !same_type(&l->type, &u64)))
357 return lfail(err, err_len, nd->col, -2, "inside a V0 function everything is u64");
358 if (literal_value(ast, nd->rhs, &imm)) next_idx = nd->lhs;
359 else if (commutative && literal_value(ast, nd->lhs, &imm)) next_idx = nd->rhs;
360 else
361 return lfail(err, err_len, nd->col, -2,
362 "function body must be a chain like (x * 2) + 1: each step applies + - * & | "
363 "to the running value and one constant");
364 if (imm > V0_MAX_PROGRAM_IMM) {
365 snprintf(msg, sizeof msg,
366 "constant %llu is wider than 32 bits; the program builder can only load 32-bit constants",
367 (unsigned long long)imm);
368 return lfail(err, err_len, nd->col, -2, msg);
369 }
370 if (count >= V0_MAX_PROGRAM_STEPS)
371 return lfail(err, err_len, nd->col, -3, "function body has more than 32 steps");
372 steps[count].op = opc;
373 steps[count].imm = imm;
374 count++;
375 idx = next_idx;
376 }
377 const OmegaAstNode *leaf = &ast->nodes[idx];
378 if (leaf->kind == OAST_NAME && strcmp(leaf->name, ast->name) == 0)
379 return lfail(err, err_len, leaf->col, -2, "recursion is not supported in V0");
380 if (leaf->kind != OAST_NAME || strcmp(leaf->name, ast->param) != 0)
381 return lfail(err, err_len, leaf->col, -2,
382 "function body must start from its parameter and use only constants "
383 "(no other names, no constant-only bodies)");
384 if (count == 0)
385 return lfail(err, err_len, leaf->col, -2,
386 "function body must apply at least one operation to the parameter");
387
388 /* steps[] is outermost-first; build innermost-first. */
389 OmegaProgram acc, step, tmp;
390 if (omega_program_build_unary_op(&acc, ast->name, steps[count - 1].op, steps[count - 1].imm) != 0)
391 return lfail(err, err_len, ast->stmt_col, -2, "program builder rejected the first step");
392 for (size_t i = count - 1; i-- > 0;) {
393 if (omega_program_build_unary_op(&step, ast->name, steps[i].op, steps[i].imm) != 0)
394 return lfail(err, err_len, ast->stmt_col, -2, "program builder rejected a step");
395 char cerr[160] = {0};
396 if (omega_program_compose(&acc, &step, &tmp, cerr, sizeof cerr) != 0) {
397 snprintf(msg, sizeof msg, "program composition failed: %.150s", cerr);
398 return lfail(err, err_len, ast->stmt_col, strstr(cerr, "overflow") ? -3 : -2, msg);
399 }
400 acc = tmp;
401 }
402 snprintf(acc.name, sizeof acc.name, "%s", ast->name);
403 snprintf(acc.contract.precondition, sizeof acc.contract.precondition, "%s", ast->requires_text);
404 snprintf(acc.contract.postcondition, sizeof acc.contract.postcondition, "%s", ast->ensures_text);
405 acc.contract.post_leaf_count = 0; /* the fn's ensures clause is one leaf */
406 if (omega_build_constraint_id(CONST_PRECONDITION, acc.contract.precondition, &acc.contract.precondition_id) ||
407 omega_build_constraint_id(CONST_POSTCONDITION, acc.contract.postcondition, &acc.contract.postcondition_id) ||
408 omega_program_compute_id(&acc))
409 return lfail(err, err_len, ast->stmt_col, -2, "could not compute the program's ids");
410 *out = acc;
411 return 0;
412}
413
414static bool binding_slot_available(const VisorSession *s, const char *name) {
415 for (size_t i = 0; i < s->bindings.count; i++)
416 if (strcmp(s->bindings.items[i].name, name) == 0) return true;
417 return s->bindings.count < VISOR_MAX_BINDINGS;
418}
419
420int omega_language_eval_line(VisorSession *s, const char *line, OmegaLangResult *out,
421 char *err, size_t err_len) {
422 if (err && err_len) err[0] = '\0';
423 if (!out) return lfail(err, err_len, 0, -1, "internal: no result slot");
424 memset(out, 0, sizeof(*out));
425 out->kind = OMEGA_LANG_NONE;
426 out->program_index = -1;
427 if (!s || !s->graph || !line) return lfail(err, err_len, 0, -1, "internal: no session");
428
429 OmegaAst ast;
430 int rc = omega_language_parse_line(line, &ast, err, err_len);
431 if (rc) return rc;
432
433 switch (ast.stmt) {
434 case OSTMT_EMPTY:
435 return 0;
436 case OSTMT_EXPR:
437 return omega_language_lower_to_graph(s->graph, &s->bindings, s->has_last ? &s->last : NULL,
438 &ast, &out->id, out, err, err_len);
439 case OSTMT_LET: {
440 if (!binding_slot_available(s, ast.name))
441 return lfail(err, err_len, ast.stmt_col, -3, "binding table is full (64 names)");
442 uint16_t saved = s->graph->object_count;
443 OmegaLangResult r;
444 memset(&r, 0, sizeof r);
445 rc = omega_language_lower_to_graph(s->graph, &s->bindings, s->has_last ? &s->last : NULL,
446 &ast, &r.id, &r, err, err_len);
447 if (rc) return rc;
448 if (visor_binding_set(s, ast.name, VISOR_BIND_OBJECT, &r.id, -1) != 0) {
449 s->graph->object_count = saved;
450 return lfail(err, err_len, ast.stmt_col, -2, "the session refused this binding name");
451 }
452 *out = r;
453 out->kind = OMEGA_LANG_BINDING;
454 out->program_index = -1;
455 snprintf(out->name, sizeof out->name, "%s", ast.name);
456 return 0;
457 }
458 case OSTMT_FN: {
459 if (!binding_slot_available(s, ast.name))
460 return lfail(err, err_len, ast.stmt_col, -3, "binding table is full (64 names)");
461 OmegaProgram prog;
462 rc = omega_language_lower_program(&ast, &prog, err, err_len);
463 if (rc) return rc;
464 /* The program id (v2, spec/program-identity.md) binds the canonical body
465 * and the contract, not the name: an equal id is the same program and
466 * is reused (same rule as graph dedupe), even under another name. A
467 * different body gets a different id, so redefining a name simply adds
468 * the new program and rebinds the name. Equal id with different code
469 * would mean the builder emitted two realizations for one body: an
470 * internal invariant violation, refused. */
471 int pidx = -1;
472 for (size_t i = 0; i < s->program_count; i++) {
473 const OmegaProgram *q = &s->programs[i];
474 if (omega_compare_semantic_id(&q->program_id, &prog.program_id) != 0) continue;
475 if (q->realization.code_len != prog.realization.code_len ||
476 memcmp(q->realization.code_bytes, prog.realization.code_bytes, prog.realization.code_len) != 0)
477 return lfail(err, err_len, ast.stmt_col, -2,
478 "internal: program id matches an existing program but its realization differs "
479 "(builder invariant violated)");
480 pidx = (int)i;
481 break;
482 }
483 bool added = false;
484 if (pidx < 0) {
485 if (s->program_count >= VISOR_MAX_PROGRAMS)
486 return lfail(err, err_len, ast.stmt_col, -3, "program table is full (16 programs)");
487 pidx = visor_program_add(s, &prog);
488 added = true;
489 }
490 if (pidx < 0) return lfail(err, err_len, ast.stmt_col, -3, "program table is full (16 programs)");
491 if (visor_binding_set(s, ast.name, VISOR_BIND_PROGRAM, &prog.program_id, pidx) != 0) {
492 if (added) {
493 s->program_count--;
494 memset(&s->programs[s->program_count], 0, sizeof(OmegaProgram));
495 }
496 return lfail(err, err_len, ast.stmt_col, -2, "the session refused this program name");
497 }
498 out->kind = OMEGA_LANG_PROGRAM;
499 out->id = prog.program_id;
500 out->program_index = pidx;
501 snprintf(out->name, sizeof out->name, "%s", ast.name);
502 snprintf(out->type_text, sizeof out->type_text, "u64 -> u64");
503 return 0;
504 }
505 }
506 return lfail(err, err_len, 0, -1, "internal: unknown statement");
507}