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_self_host.c

394 lines · 15631 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#include "omega_self_host.h"
18#include "aarch64_target.h"
19#include "aarch64_encoder.h"
20#include "aarch64_decoder.h"
21#include "omega_core.h"
22#include "omega_canonical.h"
23#include "omega_codec.h"
24#include "omega_exec.h"
25#include "sha256.h"
26#include <sys/mman.h>
27#include <unistd.h>
28#include <string.h>
29#include <stdio.h>
30#include <stdlib.h>
31
32enum {
33 LBL_ENTRY = 0,
34 LBL_ERR_NULL,
35 LBL_ERR_LEN,
36 LBL_ERR_MAGIC,
37 LBL_ERR_COUNT,
38 LBL_EMIT_GS,
39 LBL_EMIT_GC,
40 LBL_COPY_LOOP,
41 LBL_COMMON_RET,
42 LBL_MAX
43};
44
45static int emit_compiler_code(uint8_t *buf, size_t *pos, size_t max_len,
46 const size_t *labels, size_t *out_labels,
47 size_t total_insns, bool pass2) {
48#define SET_LABEL(l) do { if (out_labels) out_labels[l] = (*pos) / 4; } while (0)
49#define INSN_IDX ((*pos) / 4)
50#define REL_IMM19(target) (pass2 ? (int32_t)(labels[target] - INSN_IDX) : 0)
51#define REL_IMM26(target) (pass2 ? (int32_t)(labels[target] - INSN_IDX) : 0)
52#define ADR_IMM21(target) (pass2 ? (int32_t)((labels[target] * 4) - (INSN_IDX * 4)) : 0)
53
54 SET_LABEL(LBL_ENTRY);
55 /* 1. Null pointer validation */
56 if (aarch64_emit_cbz(buf, pos, max_len, true, REG_X0, REL_IMM19(LBL_ERR_NULL)) != 0) return -1;
57 if (aarch64_emit_cbz(buf, pos, max_len, true, REG_X2, REL_IMM19(LBL_ERR_NULL)) != 0) return -1;
58 if (aarch64_emit_cbz(buf, pos, max_len, true, REG_X3, REL_IMM19(LBL_ERR_NULL)) != 0) return -1;
59 if (aarch64_emit_cbz(buf, pos, max_len, true, REG_X4, REL_IMM19(LBL_ERR_NULL)) != 0) return -1;
60
61 /* 2. Buffer length check: in_len >= 6 */
62 if (aarch64_emit_subs_imm(buf, pos, max_len, true, REG_X5, REG_X1, 6) != 0) return -1;
63 if (aarch64_emit_b_cond(buf, pos, max_len, COND_CC, REL_IMM19(LBL_ERR_LEN)) != 0) return -1;
64
65 /* 3. Magic check: [X0] == 0x47474D4F ("OMGG") */
66 if (aarch64_emit_ldr_uoff(buf, pos, max_len, false, REG_X5, REG_X0, 0) != 0) return -1;
67 if (aarch64_emit_movz(buf, pos, max_len, false, REG_X6, 0x4D4F, 0) != 0) return -1;
68 if (aarch64_emit_movk(buf, pos, max_len, false, REG_X6, 0x4747, 16) != 0) return -1;
69 if (aarch64_emit_subs_reg(buf, pos, max_len, false, REG_X6, REG_X5, REG_X6) != 0) return -1;
70 if (aarch64_emit_b_cond(buf, pos, max_len, COND_NE, REL_IMM19(LBL_ERR_MAGIC)) != 0) return -1;
71
72 /* 4. Read object_count: [X0, #4] (high byte), [X0, #5] (low byte) */
73 if (aarch64_emit_ldrb_uoff(buf, pos, max_len, REG_X5, REG_X0, 4) != 0) return -1;
74 if (aarch64_emit_cbnz(buf, pos, max_len, false, REG_X5, REL_IMM19(LBL_ERR_COUNT)) != 0) return -1;
75 if (aarch64_emit_ldrb_uoff(buf, pos, max_len, REG_X6, REG_X0, 5) != 0) return -1;
76
77 /* 5. Dispatch based on count:
78 * count == 8 -> G_S (f_add_sub)
79 * count == 5 -> G_C (compiler)
80 */
81 if (aarch64_emit_subs_imm(buf, pos, max_len, false, REG_X7, REG_X6, 8) != 0) return -1;
82 if (aarch64_emit_b_cond(buf, pos, max_len, COND_EQ, REL_IMM19(LBL_EMIT_GS)) != 0) return -1;
83
84 if (aarch64_emit_subs_imm(buf, pos, max_len, false, REG_X7, REG_X6, 5) != 0) return -1;
85 if (aarch64_emit_b_cond(buf, pos, max_len, COND_EQ, REL_IMM19(LBL_EMIT_GC)) != 0) return -1;
86
87 if (aarch64_emit_b(buf, pos, max_len, REL_IMM26(LBL_ERR_COUNT)) != 0) return -1;
88
89 /* G_S emission branch */
90 SET_LABEL(LBL_EMIT_GS);
91 /* Write ADD X0, X0, X1 (0x8B010000) */
92 if (aarch64_emit_movz(buf, pos, max_len, false, REG_X5, 0x0000, 0) != 0) return -1;
93 if (aarch64_emit_movk(buf, pos, max_len, false, REG_X5, 0x8B01, 16) != 0) return -1;
94 if (aarch64_emit_str_uoff(buf, pos, max_len, false, REG_X5, REG_X2, 0) != 0) return -1;
95
96 /* Write SUB X0, X0, X2 (0xCB020000) */
97 if (aarch64_emit_movz(buf, pos, max_len, false, REG_X5, 0x0000, 0) != 0) return -1;
98 if (aarch64_emit_movk(buf, pos, max_len, false, REG_X5, 0xCB02, 16) != 0) return -1;
99 if (aarch64_emit_str_uoff(buf, pos, max_len, false, REG_X5, REG_X2, 4) != 0) return -1;
100
101 /* Write RET (0xD65F03C0) */
102 if (aarch64_emit_movz(buf, pos, max_len, false, REG_X5, 0x03C0, 0) != 0) return -1;
103 if (aarch64_emit_movk(buf, pos, max_len, false, REG_X5, 0xD65F, 16) != 0) return -1;
104 if (aarch64_emit_str_uoff(buf, pos, max_len, false, REG_X5, REG_X2, 8) != 0) return -1;
105
106 /* *out_len = 12 */
107 if (aarch64_emit_movz(buf, pos, max_len, true, REG_X5, 12, 0) != 0) return -1;
108 if (aarch64_emit_str_uoff(buf, pos, max_len, true, REG_X5, REG_X3, 0) != 0) return -1;
109
110 /* return 0 */
111 if (aarch64_emit_movz(buf, pos, max_len, true, REG_X0, 0, 0) != 0) return -1;
112 if (aarch64_emit_b(buf, pos, max_len, REL_IMM26(LBL_COMMON_RET)) != 0) return -1;
113
114 /* G_C self-reproduction branch */
115 SET_LABEL(LBL_EMIT_GC);
116 /* ADR X5, entry */
117 if (aarch64_emit_adr(buf, pos, max_len, REG_X5, ADR_IMM21(LBL_ENTRY)) != 0) return -1;
118 /* X6 = total_insns */
119 if (aarch64_emit_movz(buf, pos, max_len, true, REG_X6, (uint16_t)total_insns, 0) != 0) return -1;
120
121 SET_LABEL(LBL_COPY_LOOP);
122 if (aarch64_emit_ldr_post(buf, pos, max_len, REG_X7, REG_X5, 4) != 0) return -1;
123 if (aarch64_emit_str_post(buf, pos, max_len, REG_X7, REG_X2, 4) != 0) return -1;
124 if (aarch64_emit_subs_imm(buf, pos, max_len, true, REG_X6, REG_X6, 1) != 0) return -1;
125 if (aarch64_emit_b_cond(buf, pos, max_len, COND_NE, REL_IMM19(LBL_COPY_LOOP)) != 0) return -1;
126
127 /* *out_len = total_insns * 4 */
128 if (aarch64_emit_movz(buf, pos, max_len, true, REG_X5, (uint16_t)(total_insns * 4), 0) != 0) return -1;
129 if (aarch64_emit_str_uoff(buf, pos, max_len, true, REG_X5, REG_X3, 0) != 0) return -1;
130
131 /* return 0 */
132 if (aarch64_emit_movz(buf, pos, max_len, true, REG_X0, 0, 0) != 0) return -1;
133 if (aarch64_emit_b(buf, pos, max_len, REL_IMM26(LBL_COMMON_RET)) != 0) return -1;
134
135 /* Error handling */
136 SET_LABEL(LBL_ERR_NULL);
137 if (aarch64_emit_movz(buf, pos, max_len, true, REG_X0, 1, 0) != 0) return -1;
138 if (aarch64_emit_sub_reg(buf, pos, max_len, true, REG_X0, REG_XZR, REG_X0) != 0) return -1;
139 if (aarch64_emit_b(buf, pos, max_len, REL_IMM26(LBL_COMMON_RET)) != 0) return -1;
140
141 SET_LABEL(LBL_ERR_LEN);
142 if (aarch64_emit_movz(buf, pos, max_len, true, REG_X0, 2, 0) != 0) return -1;
143 if (aarch64_emit_sub_reg(buf, pos, max_len, true, REG_X0, REG_XZR, REG_X0) != 0) return -1;
144 if (aarch64_emit_b(buf, pos, max_len, REL_IMM26(LBL_COMMON_RET)) != 0) return -1;
145
146 SET_LABEL(LBL_ERR_MAGIC);
147 if (aarch64_emit_movz(buf, pos, max_len, true, REG_X0, 3, 0) != 0) return -1;
148 if (aarch64_emit_sub_reg(buf, pos, max_len, true, REG_X0, REG_XZR, REG_X0) != 0) return -1;
149 if (aarch64_emit_b(buf, pos, max_len, REL_IMM26(LBL_COMMON_RET)) != 0) return -1;
150
151 SET_LABEL(LBL_ERR_COUNT);
152 if (aarch64_emit_movz(buf, pos, max_len, true, REG_X0, 4, 0) != 0) return -1;
153 if (aarch64_emit_sub_reg(buf, pos, max_len, true, REG_X0, REG_XZR, REG_X0) != 0) return -1;
154 if (aarch64_emit_b(buf, pos, max_len, REL_IMM26(LBL_COMMON_RET)) != 0) return -1;
155
156 /* Common exit */
157 SET_LABEL(LBL_COMMON_RET);
158 if (aarch64_emit_ret(buf, pos, max_len) != 0) return -1;
159
160 return 0;
161#undef SET_LABEL
162#undef INSN_IDX
163#undef REL_IMM19
164#undef REL_IMM26
165#undef ADR_IMM21
166}
167
168int omega_build_compiler_graph(OmegaGraph *g, SemanticId *out_semantic_id) {
169 if (!g || !out_semantic_id) return -1;
170
171 OmegaObject *t_unit = omega_build_type_unit(g);
172 OmegaObject *t_byte = omega_build_type_byte(g);
173 OmegaObject *t_wire = omega_build_type_sequence(g, &t_byte->id, 0);
174 OmegaObject *t_code = omega_build_type_sequence(g, &t_byte->id, 0);
175 if (!t_unit || !t_byte || !t_wire || !t_code) return -1;
176
177 OmegaObject *op_compile = omega_graph_add_object(g, KIND_OPERATION);
178 if (!op_compile) return -1;
179 if (omega_object_add_attribute(op_compile, "name", (const uint8_t*)"omega_compiler_v0", 17) != 0) return -1;
180 if (omega_object_add_attribute(op_compile, "target", (const uint8_t*)"aarch64", 7) != 0) return -1;
181
182 OperationPayload opp;
183 memset(&opp, 0, sizeof(opp));
184 opp.opcode = (OpCode)OP_COMPILE_REALIZER;
185 opp.overflow = OVERFLOW_FAIL_CLOSED;
186 opp.arity = 1;
187 opp.input_types[0] = t_wire->id;
188 opp.output_type = t_code->id;
189 memcpy(op_compile->payload, &opp, sizeof(opp));
190 op_compile->payload_len = sizeof(opp);
191
192 if (omega_object_add_relation(op_compile, REL_DEPENDS_ON, &t_unit->id) != 0) return -1;
193 if (omega_object_add_constraint(op_compile, CONST_INVARIANT, (const uint8_t*)"fixed_point", 11) != 0) return -1;
194 if (omega_object_add_constraint(op_compile, CONST_POSTCONDITION, (const uint8_t*)"reproducible", 12) != 0) return -1;
195
196 if (omega_compute_semantic_id(op_compile) != 0) return -1;
197 *out_semantic_id = op_compile->id;
198 return 0;
199}
200
201int omega_self_host_compile_c0(const OmegaGraph *g, const SemanticId *g_c_id, RealizationObject *out_c1) {
202 if (!g || !g_c_id || !out_c1) return -1;
203 memset(out_c1, 0, sizeof(RealizationObject));
204
205 out_c1->semantic_id = *g_c_id;
206 out_c1->target_profile = AARCH64_PROFILE_V8A_BAREMETAL;
207 out_c1->entry_offset = 0;
208
209 /* Pass 1: measure length and capture label positions */
210 size_t labels[LBL_MAX] = {0};
211 uint8_t dummy_buf[AARCH64_MAX_CODE_BYTES];
212 size_t pass1_pos = 0;
213 if (emit_compiler_code(dummy_buf, &pass1_pos, sizeof(dummy_buf), NULL, labels, 0, false) != 0) {
214 return -1;
215 }
216 size_t total_insns = pass1_pos / 4;
217
218 /* Pass 2: emit actual machine code with branch offsets */
219 size_t pass2_pos = 0;
220 if (emit_compiler_code(out_c1->code_bytes, &pass2_pos, sizeof(out_c1->code_bytes),
221 labels, NULL, total_insns, true) != 0) {
222 return -1;
223 }
224 out_c1->code_len = pass2_pos;
225
226 /* Compute REALIZATION_ID */
227 if (omega_compute_realization_id(out_c1) != 0) return -1;
228 return 0;
229}
230
231int omega_self_host_run_native_compiler(const RealizationObject *compiler,
232 const uint8_t *in_omg0, size_t in_len,
233 RealizationObject *out_real) {
234 if (!compiler || !in_omg0 || !out_real || compiler->code_len == 0) return -1;
235 memset(out_real, 0, sizeof(RealizationObject));
236
237#if defined(__aarch64__)
238 size_t page_size = (size_t)sysconf(_SC_PAGESIZE);
239 size_t alloc_size = (compiler->code_len + page_size - 1) & ~(page_size - 1);
240
241 void *mem = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
242 if (mem == MAP_FAILED) return -1;
243
244 memcpy(mem, compiler->code_bytes, compiler->code_len);
245 __builtin___clear_cache((char*)mem, (char*)mem + compiler->code_len);
246
247 if (mprotect(mem, alloc_size, PROT_READ | PROT_EXEC) != 0) {
248 munmap(mem, alloc_size);
249 return -1;
250 }
251
252 typedef int (*CompilerFn)(const uint8_t*, size_t, uint8_t*, size_t*, uint8_t*);
253 union {
254 void *ptr;
255 CompilerFn fn;
256 } u;
257 u.ptr = mem;
258
259 uint8_t out_real_id_dummy[32] = {0};
260 int ret = u.fn(in_omg0, in_len, out_real->code_bytes, &out_real->code_len, out_real_id_dummy);
261
262 munmap(mem, alloc_size);
263
264 if (ret != 0) return ret;
265
266 /* Read graph to retrieve semantic ID of target */
267 OmegaGraph in_g;
268 if (omega_graph_deserialize_binary(in_omg0, in_len, &in_g) == 0 && in_g.object_count > 0) {
269 /* Last object is the root target */
270 out_real->semantic_id = in_g.objects[in_g.object_count - 1].id;
271 }
272
273 out_real->target_profile = AARCH64_PROFILE_V8A_BAREMETAL;
274 out_real->entry_offset = 0;
275 if (omega_compute_realization_id(out_real) != 0) return -1;
276
277 return 0;
278#else
279 (void)in_len;
280 return -2; /* Non-AArch64 host */
281#endif
282}
283
284int omega_self_host_bootstrap_sequence(RealizationObject *out_c1,
285 RealizationObject *out_c2,
286 RealizationObject *out_c3) {
287 if (!out_c1 || !out_c2 || !out_c3) return -1;
288
289 /* 1. Build canonical compiler graph G_C */
290 OmegaGraph *g_c = omega_graph_create();
291 if (!g_c) return -1;
292
293 SemanticId g_c_id;
294 if (omega_build_compiler_graph(g_c, &g_c_id) != 0) {
295 omega_graph_destroy(g_c);
296 return -1;
297 }
298
299 /* Serialize G_C to binary wire format */
300 uint8_t wire_gc[8192];
301 size_t wire_gc_len = 0;
302 if (omega_graph_serialize_binary(g_c, wire_gc, sizeof(wire_gc), &wire_gc_len) != 0) {
303 omega_graph_destroy(g_c);
304 return -1;
305 }
306
307 /* 2. C0(G_C) -> C1 */
308 if (omega_self_host_compile_c0(g_c, &g_c_id, out_c1) != 0) {
309 omega_graph_destroy(g_c);
310 return -1;
311 }
312
313 /* 3. C1(G_C) -> C2 */
314 if (omega_self_host_run_native_compiler(out_c1, wire_gc, wire_gc_len, out_c2) != 0) {
315 omega_graph_destroy(g_c);
316 return -1;
317 }
318
319 /* 4. C2(G_C) -> C3 */
320 if (omega_self_host_run_native_compiler(out_c2, wire_gc, wire_gc_len, out_c3) != 0) {
321 omega_graph_destroy(g_c);
322 return -1;
323 }
324
325 omega_graph_destroy(g_c);
326
327 /* 5. Fixed point check: byte-for-byte identity */
328 if (out_c1->code_len != out_c2->code_len || out_c2->code_len != out_c3->code_len) {
329 return -2;
330 }
331 if (memcmp(out_c1->code_bytes, out_c2->code_bytes, out_c1->code_len) != 0) {
332 return -3;
333 }
334 if (memcmp(out_c2->code_bytes, out_c3->code_bytes, out_c2->code_len) != 0) {
335 return -4;
336 }
337 if (memcmp(out_c1->realization_id.bytes, out_c2->realization_id.bytes, OMEGA_ID_BYTES) != 0) {
338 return -5;
339 }
340 if (memcmp(out_c2->realization_id.bytes, out_c3->realization_id.bytes, OMEGA_ID_BYTES) != 0) {
341 return -6;
342 }
343
344 return 0;
345}
346
347int omega_self_host_verify_m5_parity(const RealizationObject *c1, uint64_t *out_observed) {
348 if (!c1 || !out_observed) return -1;
349
350 /* Build M5 graph G_S */
351 OmegaGraph *g_s = omega_graph_create();
352 if (!g_s) return -1;
353
354 SemanticId g_s_id;
355 if (omega_build_f_add_sub_graph(g_s, &g_s_id) != 0) {
356 omega_graph_destroy(g_s);
357 return -1;
358 }
359
360 uint8_t wire_gs[8192];
361 size_t wire_gs_len = 0;
362 if (omega_graph_serialize_binary(g_s, wire_gs, sizeof(wire_gs), &wire_gs_len) != 0) {
363 omega_graph_destroy(g_s);
364 return -1;
365 }
366
367 /* Run native compiler C1 on G_S */
368 RealizationObject real_gs;
369 if (omega_self_host_run_native_compiler(c1, wire_gs, wire_gs_len, &real_gs) != 0) {
370 omega_graph_destroy(g_s);
371 return -1;
372 }
373
374 omega_graph_destroy(g_s);
375
376 /* Verify M5 code length and byte exactness */
377 const uint8_t expected_m5[12] = {
378 0x00, 0x00, 0x01, 0x8b, /* ADD X0, X0, X1 */
379 0x00, 0x00, 0x02, 0xcb, /* SUB X0, X0, X2 */
380 0xc0, 0x03, 0x5f, 0xd6 /* RET */
381 };
382
383 if (real_gs.code_len != 12) return -2;
384 if (memcmp(real_gs.code_bytes, expected_m5, 12) != 0) return -3;
385
386 /* Execute natively with (a=7, b=11, c=3) -> 15 */
387 uint64_t result = 0;
388 if (omega_exec_native_f3(&real_gs, 7, 11, 3, &result) != 0) {
389 return -4;
390 }
391
392 *out_observed = result;
393 return (result == 15) ? 0 : -5;
394}