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 / omega_program.h

191 lines · 8818 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#ifndef OMEGA_PROGRAM_H
18#define OMEGA_PROGRAM_H
19
20#include "omega_types.h"
21#include "omega_realize.h"
22#include "omega_verify.h"
23#include <stdbool.h>
24
25typedef struct {
26 uint32_t insn_count;
27 uint32_t reg_pressure;
28 uint32_t memory_bytes;
29 uint32_t latency_cycles;
30} OmegaCost;
31
32#define OMEGA_PROGRAM_MAX_STEPS 64
33
34typedef struct {
35 TypeTag input_type;
36 uint16_t input_width;
37 TypeTag output_type;
38 uint16_t output_width;
39 SemanticId precondition_id;
40 SemanticId postcondition_id;
41 char precondition[64];
42 char postcondition[64]; /* display text; for a composition, derived and not canonical */
43 /* Composition (spec/program-identity.md 2.4): the flattened, ordered list of the
44 * component (leaf) postcondition ids, in application order. postcondition_id is then
45 * the canonical sequence constraint over this list, so composition is associative.
46 * post_leaf_count == 0 means the postcondition is a leaf. */
47 uint16_t post_leaf_count;
48 SemanticId post_leaves[OMEGA_PROGRAM_MAX_STEPS];
49} OmegaContract;
50
51/* Construct canonical constraint SemanticId from ConstraintKind and annotation */
52int omega_build_constraint_id(ConstraintKind kind, const char *annotation, SemanticId *out_id);
53
54/* Canonical semantic body (spec/program-identity.md 2.1): an ordered chain of
55 * unary steps, innermost first: f(x) = op_n(...op_1(x, imm_1)..., imm_n).
56 * Stored by value so OmegaProgram stays safe to copy. */
57#define OMEGA_PROGRAM_ID_DOMAIN "omega.program.v2"
58
59typedef struct {
60 uint8_t op; /* OpCode: OP_ADD OP_SUB OP_MUL OP_AND OP_OR */
61 uint64_t imm;
62} OmegaProgramStep;
63
64typedef struct {
65 bool has_body; /* false: meaning unknown; the program has no identity */
66 uint16_t step_count;
67 OmegaProgramStep steps[OMEGA_PROGRAM_MAX_STEPS];
68} OmegaProgramBody;
69
70typedef struct {
71 SemanticId program_id;
72 char name[64]; /* metadata: not part of the identity */
73 OmegaProgramBody body; /* semantic: part of the identity */
74 OmegaContract contract;
75 OmegaCost cost;
76 OmegaGraph *graph;
77 RealizationObject realization;
78 bool is_realized;
79 bool is_verified;
80} OmegaProgram;
81
82typedef struct {
83 SemanticId task_id;
84 char description[128];
85 OmegaContract target_contract;
86 OmegaCost cost_budget;
87 size_t example_count;
88 uint64_t inputs[16];
89 uint64_t expected_outputs[16];
90} SynthesisTask;
91
92/* Initialize empty program */
93void omega_program_init(OmegaProgram *prog, const char *name);
94
95/* Free resources owned by program */
96void omega_program_destroy(OmegaProgram *prog);
97
98/* Program identity v2 (spec/program-identity.md):
99 * SHA256("omega.program.v2" 0x00 || body_root_id || in_type_id || out_type_id || pre_id || post_id)
100 * body_root_id is the id of the root APPLY of the body lowered with the existing
101 * canonical builders. Name, cost and realization are metadata and not hashed.
102 * Returns -1 and zeroes program_id when the program has no body (no identity). */
103int omega_program_compute_id(OmegaProgram *prog);
104
105/* Verified Crumb contract_id (aien-protocols specs/verified-crumb/SPEC.md 4.2):
106 * SHA256("aien.vc1.contract.v1" 0x00 || in_type_id || out_type_id || pre_id || post_id)
107 * the same four components the program id hashes, with the same type ids. 0 or -1. */
108int omega_program_contract_id(const OmegaProgram *prog, uint8_t out[32]);
109
110
111/* The canonical body root id alone (the SemanticId of the body's root object). */
112int omega_program_body_root_id(const OmegaProgram *prog, SemanticId *out_root);
113
114/* ---- V0 program realization (spec/program-realization.md) ----------------
115 * Pipeline: canonical body -> realize_check (verified semantic op sequence) ->
116 * schedule (machine-aware choice, never meaning) -> AArch64 bytes via the one encoder
117 * (aarch64_encoder.c) -> verification -> RealizationId. */
118typedef enum {
119 OMEGA_SCHED_SEQUENTIAL = 0, /* one scratch register X1, load then op, per step */
120 OMEGA_SCHED_PRELOAD = 1 /* blocks of constants preloaded into X1..X15, then ops */
121} OmegaRealizeSchedule;
122#define OMEGA_PRELOAD_REGS 15 /* X1..X15: caller-saved scratch only (never X16-X30) */
123
124/* Fail-closed V0 gate: body present, unsigned in==out type, width 8/16/32/64, every step
125 * ADD SUB MUL AND OR with a constant that fits the width, program_id == v2 id of body +
126 * contract. 0 ok (width in *out_width), -1 refused with a reason in why. */
127int omega_program_realize_check(const OmegaProgram *prog, uint16_t *out_width, char *why, size_t why_len);
128
129/* Emit a schedule for body at width (width < 64 appends one AND with the width mask).
130 * Constants: MOVZ chunk0 then MOVK for each nonzero higher 16-bit chunk (full 64 bits). */
131int omega_program_emit_schedule(const OmegaProgramBody *body, uint16_t width, OmegaRealizeSchedule sched,
132 uint8_t *code, size_t *code_len, size_t max_len);
133
134/* Independent semantic evaluator: lowers the body to the canonical objects the program
135 * id binds and evaluates them with omega_eval_pure_binary_uint (no code involved).
136 * Every xs[i] must lie in the declared input domain [0, 2^w). 0 ok, -1 refused. */
137int omega_program_eval(const OmegaProgram *prog, const uint64_t *xs, size_t n, uint64_t *ys);
138
139/* omega_program_realize with a reason: 0 ok, -2 refused by the V0 gate (why set), -1 error. */
140int omega_program_realize_ex(OmegaProgram *prog, char *why, size_t why_len);
141
142/* Lift a realization in the builder's rigid unary template
143 * (movz x1, c0 [; movk x1, ck, lsl 16k for each nonzero chunk k] ; op x0, x0, x1)* ; ret
144 * back to a body. Confirmed by re-emitting the steps and requiring byte equality.
145 * Returns 0 and fills out_body on success, -1 if the code is not in the template. */
146int omega_program_lift_body(const uint8_t *code, size_t code_len, OmegaProgramBody *out_body);
147
148/* Emit the canonical realization of a u64 body (the template above = the SEQUENTIAL schedule). */
149int omega_program_emit_body(const OmegaProgramBody *body, uint8_t *code, size_t *code_len, size_t max_len);
150
151/* Canonical sequence postcondition id over an ordered leaf list (n >= 1): n == 1 is the
152 * leaf itself; otherwise the right fold Seq(l1, Seq(l2, ... ln)) of KIND_CONSTRAINT
153 * objects with attribute "omega.compose" = "seq" and a CONST_POSTCONDITION constraint
154 * whose payload is (head id || tail id). */
155int omega_contract_post_seq_id(const SemanticId *leaves, uint16_t n, SemanticId *out);
156
157/* Validate contract conformance */
158int omega_program_validate_contract(const OmegaProgram *prog, char *err_msg, size_t err_msg_len);
159
160/* Compose two programs: C(x) = B(A(x))
161 * Validates OutType(A) == InType(B)
162 * Derives C's contract and cost: Cost(C) = Cost(A) + Cost(B)
163 */
164int omega_program_compose(const OmegaProgram *a, const OmegaProgram *b, OmegaProgram *out_c, char *err_msg, size_t err_msg_len);
165
166/* Canonical machine-independent AArch64 realization compiled from the program body:
167 * V0 gate, SEQUENTIAL schedule, realization.semantic_id = program_id, OMG_R0 id, V0
168 * structural check. Sets is_realized (not is_verified). 0 ok, nonzero refused. The
169 * machine-bound (triple id) path is omega_synthesize_realization. */
170int omega_program_realize(OmegaProgram *prog);
171
172/* Verify program using M7 verification engine (V0, V1, V2) */
173int omega_program_verify(OmegaProgram *prog, VerifyReport *report);
174
175/* Execute program natively: returns 0 on success, writes result to out_val */
176int omega_program_exec(const OmegaProgram *prog, uint64_t in_val, uint64_t *out_val);
177
178/* Create canonical linear unary program: f(x) = op(x, imm)
179 * Supported: OP_ADD, OP_SUB, OP_MUL, OP_AND, OP_OR
180 */
181int omega_program_build_unary_op(OmegaProgram *prog, const char *name, OpCode op, uint64_t imm);
182
183/* Create and evaluate a synthesis task */
184int omega_task_init(SynthesisTask *task, const char *desc,
185 TypeTag in_type, uint16_t in_width,
186 TypeTag out_type, uint16_t out_width,
187 const uint64_t *inputs, const uint64_t *outputs, size_t count);
188
189int omega_task_evaluate_candidate(const SynthesisTask *task, const OmegaProgram *candidate, bool *out_solved);
190
191#endif /* OMEGA_PROGRAM_H */