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 / src / omega_realize.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 | #include "omega_realize.h" |
| 18 | #include "aarch64_encoder.h" |
| 19 | #include "omega_core.h" |
| 20 | #include "omega_canonical.h" |
| 21 | #include "sha256.h" |
| 22 | #include <string.h> |
| 23 | |
| 24 | static void write_u32_be(uint8_t *p, uint32_t v) { |
| 25 | p[0] = (uint8_t)((v >> 24) & 0xff); |
| 26 | p[1] = (uint8_t)((v >> 16) & 0xff); |
| 27 | p[2] = (uint8_t)((v >> 8) & 0xff); |
| 28 | p[3] = (uint8_t)(v & 0xff); |
| 29 | } |
| 30 | |
| 31 | int omega_compute_realization_id(RealizationObject *real) { |
| 32 | if (!real || real->code_len > sizeof(real->code_bytes)) return -1; |
| 33 | uint8_t buf[AARCH64_MAX_CODE_BYTES + 64]; |
| 34 | size_t pos = 0; |
| 35 | |
| 36 | /* Magic "OMG_R0" */ |
| 37 | buf[pos++] = 0x4F; |
| 38 | buf[pos++] = 0x4D; |
| 39 | buf[pos++] = 0x47; |
| 40 | buf[pos++] = 0x5F; |
| 41 | buf[pos++] = 0x52; |
| 42 | buf[pos++] = 0x30; |
| 43 | |
| 44 | buf[pos++] = real->target_profile; |
| 45 | memcpy(&buf[pos], real->semantic_id.bytes, OMEGA_ID_BYTES); |
| 46 | pos += OMEGA_ID_BYTES; |
| 47 | |
| 48 | write_u32_be(&buf[pos], (uint32_t)real->code_len); |
| 49 | pos += 4; |
| 50 | |
| 51 | if (real->code_len > 0) { |
| 52 | memcpy(&buf[pos], real->code_bytes, real->code_len); |
| 53 | pos += real->code_len; |
| 54 | } |
| 55 | |
| 56 | sha256_hash(buf, pos, real->realization_id.bytes); |
| 57 | real->has_id = true; |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int omega_build_f_add_sub_graph(OmegaGraph *g, SemanticId *out_semantic_id) { |
| 62 | if (!g || !out_semantic_id) return -1; |
| 63 | |
| 64 | OmegaObject *t_u64 = omega_build_type_uint(g, 64); |
| 65 | OmegaObject *va = omega_build_val_uint(g, &t_u64->id, 64, 7); |
| 66 | OmegaObject *vb = omega_build_val_uint(g, &t_u64->id, 64, 11); |
| 67 | OmegaObject *vc = omega_build_val_uint(g, &t_u64->id, 64, 3); |
| 68 | |
| 69 | OmegaObject *op_add = omega_build_op_binary(g, OP_ADD, OVERFLOW_WRAP, &t_u64->id); |
| 70 | OmegaObject *app_add = omega_build_apply(g, &op_add->id, &va->id, &vb->id); |
| 71 | |
| 72 | OmegaObject *op_sub = omega_build_op_binary(g, OP_SUB, OVERFLOW_WRAP, &t_u64->id); |
| 73 | OmegaObject *app_sub = omega_build_apply(g, &op_sub->id, &app_add->id, &vc->id); |
| 74 | |
| 75 | *out_semantic_id = app_sub->id; |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | int omega_realize_f_add_sub(const OmegaGraph *g, const SemanticId *root_apply_id, RealizationObject *out_real) { |
| 80 | if (!g || !root_apply_id || !out_real) return -1; |
| 81 | memset(out_real, 0, sizeof(RealizationObject)); |
| 82 | |
| 83 | out_real->semantic_id = *root_apply_id; |
| 84 | out_real->target_profile = AARCH64_PROFILE_V8A_BAREMETAL; |
| 85 | out_real->entry_offset = 0; |
| 86 | |
| 87 | size_t pos = 0; |
| 88 | /* Synthesize: |
| 89 | * ADD X0, X0, X1 (X0 = a + b) |
| 90 | * SUB X0, X0, X2 (X0 = (a + b) - c) |
| 91 | * RET |
| 92 | */ |
| 93 | if (aarch64_emit_add_reg(out_real->code_bytes, &pos, sizeof(out_real->code_bytes), true, REG_X0, REG_X0, REG_X1) != 0) return -1; |
| 94 | if (aarch64_emit_sub_reg(out_real->code_bytes, &pos, sizeof(out_real->code_bytes), true, REG_X0, REG_X0, REG_X2) != 0) return -1; |
| 95 | if (aarch64_emit_ret(out_real->code_bytes, &pos, sizeof(out_real->code_bytes)) != 0) return -1; |
| 96 | |
| 97 | out_real->code_len = pos; |
| 98 | omega_compute_realization_id(out_real); |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | int omega_realize_pure_binary(const OmegaGraph *g, const SemanticId *op_id, RealizationObject *out_real) { |
| 103 | if (!g || !op_id || !out_real) return -1; |
| 104 | const OmegaObject *obj = omega_graph_find_object_const(g, op_id); |
| 105 | if (!obj || obj->kind != KIND_OPERATION || obj->payload_len < sizeof(OperationPayload)) return -1; |
| 106 | |
| 107 | const OperationPayload *opp = (const OperationPayload*)obj->payload; |
| 108 | memset(out_real, 0, sizeof(RealizationObject)); |
| 109 | out_real->semantic_id = *op_id; |
| 110 | out_real->target_profile = AARCH64_PROFILE_V8A_BAREMETAL; |
| 111 | |
| 112 | size_t pos = 0; |
| 113 | switch (opp->opcode) { |
| 114 | case OP_ADD: |
| 115 | aarch64_emit_add_reg(out_real->code_bytes, &pos, sizeof(out_real->code_bytes), true, REG_X0, REG_X0, REG_X1); |
| 116 | break; |
| 117 | case OP_SUB: |
| 118 | aarch64_emit_sub_reg(out_real->code_bytes, &pos, sizeof(out_real->code_bytes), true, REG_X0, REG_X0, REG_X1); |
| 119 | break; |
| 120 | case OP_MUL: |
| 121 | aarch64_emit_mul_reg(out_real->code_bytes, &pos, sizeof(out_real->code_bytes), true, REG_X0, REG_X0, REG_X1); |
| 122 | break; |
| 123 | case OP_AND: |
| 124 | aarch64_emit_and_reg(out_real->code_bytes, &pos, sizeof(out_real->code_bytes), true, REG_X0, REG_X0, REG_X1); |
| 125 | break; |
| 126 | case OP_OR: |
| 127 | aarch64_emit_orr_reg(out_real->code_bytes, &pos, sizeof(out_real->code_bytes), true, REG_X0, REG_X0, REG_X1); |
| 128 | break; |
| 129 | default: |
| 130 | return -1; |
| 131 | } |
| 132 | aarch64_emit_ret(out_real->code_bytes, &pos, sizeof(out_real->code_bytes)); |
| 133 | out_real->code_len = pos; |
| 134 | omega_compute_realization_id(out_real); |
| 135 | return 0; |
| 136 | } |