Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/omega_core.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / omega_core.c` 474 lines ยท 16948 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_core.h" 18 #include "omega_canonical.h" 19 #include 20 #include 21 #include 22 23 OmegaGraph* omega_graph_create(void) { 24 OmegaGraph *g = (OmegaGraph*)calloc(1, sizeof(OmegaGraph)); 25 return g; 26 } 27 28 void omega_graph_destroy(OmegaGraph *g) { 29 if (g) free(g); 30 } 31 32 OmegaObject* omega_graph_add_object(OmegaGraph *g, SemanticKind kind) { 33 if (!g || g->object_count >= OMEGA_MAX_GRAPH_OBJECTS) return NULL; 34 OmegaObject *obj = &g->objects[g->object_count++]; 35 memset(obj, 0, sizeof(OmegaObject)); 36 obj->kind = kind; 37 return obj; 38 } 39 40 OmegaObject* omega_graph_find_object(OmegaGraph *g, const SemanticId *id) { 41 if (!g || !id) return NULL; 42 for (uint16_t i = 0; i < g->object_count; ++i) { 43 if (g->objects[i].has_id && omega_compare_semantic_id(&g->objects[i].id, id) == 0) { 44 return &g->objects[i]; 45 } 46 } 47 return NULL; 48 } 49 50 const OmegaObject* omega_graph_find_object_const(const OmegaGraph *g, const SemanticId *id) { 51 if (!g || !id) return NULL; 52 for (uint16_t i = 0; i < g->object_count; ++i) { 53 if (g->objects[i].has_id && omega_compare_semantic_id(&g->objects[i].id, id) == 0) { 54 return &g->objects[i]; 55 } 56 } 57 return NULL; 58 } 59 60 int omega_object_add_attribute(OmegaObject *obj, const char *key, const uint8_t *val, uint16_t val_len) { 61 if (!obj || !key || obj->attr_count >= OMEGA_MAX_ATTRIBUTES) return -1; 62 /* Fields are part of the semantic identity: refuse what does not fit. */ 63 size_t key_len = 0; 64 while (key_len < OMEGA_MAX_KEY_LEN && key[key_len] != '\0') key_len++; 65 if (key_len >= OMEGA_MAX_KEY_LEN || val_len > OMEGA_MAX_VAL_LEN) return -1; 66 OmegaAttribute *attr = &obj->attributes[obj->attr_count++]; 67 memcpy(attr->key, key, key_len); 68 attr->key[key_len] = '\0'; 69 if (val && val_len > 0) { 70 memcpy(attr->value, val, val_len); 71 attr->val_len = val_len; 72 } else { 73 attr->val_len = 0; 74 } 75 return 0; 76 } 77 78 int omega_object_add_relation(OmegaObject *obj, RelationKind kind, const SemanticId *target) { 79 if (!obj || !target || obj->rel_count >= OMEGA_MAX_RELATIONS) return -1; 80 OmegaRelation *rel = &obj->relations[obj->rel_count++]; 81 rel->kind = (uint16_t)kind; 82 memcpy(rel->target_id.bytes, target->bytes, OMEGA_ID_BYTES); 83 return 0; 84 } 85 86 int omega_object_add_constraint(OmegaObject *obj, ConstraintKind kind, const uint8_t *payload, uint16_t payload_len) { 87 if (!obj || obj->const_count >= OMEGA_MAX_CONSTRAINTS) return -1; 88 if (payload_len > sizeof(obj->constraints[0].payload)) return -1; 89 OmegaConstraint *c = &obj->constraints[obj->const_count++]; 90 c->kind = (uint16_t)kind; 91 c->payload_len = payload_len; 92 if (payload && c->payload_len > 0) { 93 memcpy(c->payload, payload, c->payload_len); 94 } 95 return 0; 96 } 97 98 OmegaObject* omega_build_type_unit(OmegaGraph *g) { 99 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE); 100 if (!obj) return NULL; 101 TypePayload tp; 102 memset(&tp, 0, sizeof(tp)); 103 tp.tag = TYPE_UNIT; 104 memcpy(obj->payload, &tp, sizeof(tp)); 105 obj->payload_len = sizeof(tp); 106 omega_compute_semantic_id(obj); 107 return obj; 108 } 109 110 OmegaObject* omega_build_type_bool(OmegaGraph *g) { 111 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE); 112 if (!obj) return NULL; 113 TypePayload tp; 114 memset(&tp, 0, sizeof(tp)); 115 tp.tag = TYPE_BOOL; 116 memcpy(obj->payload, &tp, sizeof(tp)); 117 obj->payload_len = sizeof(tp); 118 omega_compute_semantic_id(obj); 119 return obj; 120 } 121 122 OmegaObject* omega_build_type_uint(OmegaGraph *g, uint16_t width) { 123 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE); 124 if (!obj) return NULL; 125 TypePayload tp; 126 memset(&tp, 0, sizeof(tp)); 127 tp.tag = TYPE_UNSIGNED_INT; 128 tp.width = width; 129 memcpy(obj->payload, &tp, sizeof(tp)); 130 obj->payload_len = sizeof(tp); 131 omega_compute_semantic_id(obj); 132 return obj; 133 } 134 135 OmegaObject* omega_build_type_signed_int(OmegaGraph *g, uint16_t width) { 136 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE); 137 if (!obj) return NULL; 138 TypePayload tp; 139 memset(&tp, 0, sizeof(tp)); 140 tp.tag = TYPE_SIGNED_INT; 141 tp.width = width; 142 memcpy(obj->payload, &tp, sizeof(tp)); 143 obj->payload_len = sizeof(tp); 144 omega_compute_semantic_id(obj); 145 return obj; 146 } 147 148 OmegaObject* omega_build_type_fp32(OmegaGraph *g) { 149 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE); 150 if (!obj) return NULL; 151 TypePayload tp; 152 memset(&tp, 0, sizeof(tp)); 153 tp.tag = TYPE_FP32; 154 tp.width = 32; 155 memcpy(obj->payload, &tp, sizeof(tp)); 156 obj->payload_len = sizeof(tp); 157 omega_compute_semantic_id(obj); 158 return obj; 159 } 160 161 OmegaObject* omega_build_type_bitvector(OmegaGraph *g, uint16_t width) { 162 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE); 163 if (!obj) return NULL; 164 TypePayload tp; 165 memset(&tp, 0, sizeof(tp)); 166 tp.tag = TYPE_BITVECTOR; 167 tp.width = width; 168 memcpy(obj->payload, &tp, sizeof(tp)); 169 obj->payload_len = sizeof(tp); 170 omega_compute_semantic_id(obj); 171 return obj; 172 } 173 174 OmegaObject* omega_build_type_byte(OmegaGraph *g) { 175 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE); 176 if (!obj) return NULL; 177 TypePayload tp; 178 memset(&tp, 0, sizeof(tp)); 179 tp.tag = TYPE_BYTE; 180 tp.width = 8; 181 memcpy(obj->payload, &tp, sizeof(tp)); 182 obj->payload_len = sizeof(tp); 183 omega_compute_semantic_id(obj); 184 return obj; 185 } 186 187 OmegaObject* omega_build_type_sequence(OmegaGraph *g, const SemanticId *elem_type, uint32_t len) { 188 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE); 189 if (!obj) return NULL; 190 TypePayload tp; 191 memset(&tp, 0, sizeof(tp)); 192 tp.tag = TYPE_SEQUENCE; 193 tp.length = len; 194 if (elem_type) memcpy(tp.elem_type.bytes, elem_type->bytes, OMEGA_ID_BYTES); 195 memcpy(obj->payload, &tp, sizeof(tp)); 196 obj->payload_len = sizeof(tp); 197 omega_compute_semantic_id(obj); 198 return obj; 199 } 200 201 OmegaObject* omega_build_type_cap_ref(OmegaGraph *g) { 202 OmegaObject *obj = omega_graph_add_object(g, KIND_TYPE); 203 if (!obj) return NULL; 204 TypePayload tp; 205 memset(&tp, 0, sizeof(tp)); 206 tp.tag = TYPE_CAPABILITY_REF; 207 memcpy(obj->payload, &tp, sizeof(tp)); 208 obj->payload_len = sizeof(tp); 209 omega_compute_semantic_id(obj); 210 return obj; 211 } 212 213 OmegaObject* omega_build_val_bool(OmegaGraph *g, const SemanticId *bool_type_id, bool val) { 214 OmegaObject *obj = omega_graph_add_object(g, KIND_VALUE); 215 if (!obj) return NULL; 216 ValuePayload vp; 217 memset(&vp, 0, sizeof(vp)); 218 if (bool_type_id) memcpy(vp.type_id.bytes, bool_type_id->bytes, OMEGA_ID_BYTES); 219 vp.byte_len = 1; 220 vp.bytes[0] = val ? 1 : 0; 221 memcpy(obj->payload, &vp, sizeof(vp)); 222 obj->payload_len = sizeof(vp); 223 omega_compute_semantic_id(obj); 224 return obj; 225 } 226 227 OmegaObject* omega_build_val_uint(OmegaGraph *g, const SemanticId *uint_type_id, uint16_t width, uint64_t val) { 228 OmegaObject *obj = omega_graph_add_object(g, KIND_VALUE); 229 if (!obj) return NULL; 230 ValuePayload vp; 231 memset(&vp, 0, sizeof(vp)); 232 if (uint_type_id) memcpy(vp.type_id.bytes, uint_type_id->bytes, OMEGA_ID_BYTES); 233 uint16_t nbytes = (width + 7) / 8; 234 if (nbytes == 0) nbytes = 1; 235 if (nbytes > 64) nbytes = 64; 236 vp.byte_len = nbytes; 237 for (int i = 0; i < nbytes; ++i) { 238 int shift = (nbytes - 1 - i) * 8; 239 if (shift < 64) { 240 vp.bytes[i] = (uint8_t)((val >> shift) & 0xff); 241 } else { 242 vp.bytes[i] = 0; 243 } 244 } 245 memcpy(obj->payload, &vp, sizeof(vp)); 246 obj->payload_len = sizeof(vp); 247 omega_compute_semantic_id(obj); 248 return obj; 249 } 250 251 OmegaObject* omega_build_param(OmegaGraph *g, const SemanticId *type_id, uint16_t index) { 252 OmegaObject *obj = omega_graph_add_object(g, KIND_VALUE); 253 if (!obj) return NULL; 254 uint8_t idx[2] = { (uint8_t)(index >> 8), (uint8_t)index }; 255 if (omega_object_add_attribute(obj, "omega.param", idx, sizeof idx) != 0) return NULL; 256 ValuePayload vp; 257 memset(&vp, 0, sizeof(vp)); 258 if (type_id) memcpy(vp.type_id.bytes, type_id->bytes, OMEGA_ID_BYTES); 259 vp.byte_len = 0; 260 memcpy(obj->payload, &vp, sizeof(vp)); 261 obj->payload_len = sizeof(vp); 262 omega_compute_semantic_id(obj); 263 return obj; 264 } 265 266 OmegaObject* omega_build_op_binary(OmegaGraph *g, OpCode op, OverflowPolicy ov, const SemanticId *type_id) { 267 OmegaObject *obj = omega_graph_add_object(g, KIND_OPERATION); 268 if (!obj) return NULL; 269 OperationPayload opp; 270 memset(&opp, 0, sizeof(opp)); 271 opp.opcode = op; 272 opp.overflow = ov; 273 if (type_id) { 274 memcpy(opp.type_id.bytes, type_id->bytes, OMEGA_ID_BYTES); 275 opp.arity = 2; 276 memcpy(opp.input_types[0].bytes, type_id->bytes, OMEGA_ID_BYTES); 277 memcpy(opp.input_types[1].bytes, type_id->bytes, OMEGA_ID_BYTES); 278 memcpy(opp.output_type.bytes, type_id->bytes, OMEGA_ID_BYTES); 279 } 280 memcpy(obj->payload, &opp, sizeof(opp)); 281 obj->payload_len = sizeof(opp); 282 omega_compute_semantic_id(obj); 283 return obj; 284 } 285 286 OmegaObject* omega_build_apply(OmegaGraph *g, const SemanticId *op_id, const SemanticId *arg1, const SemanticId *arg2) { 287 OmegaObject *obj = omega_graph_add_object(g, KIND_OPERATION); 288 if (!obj) return NULL; 289 ApplyPayload app; 290 memset(&app, 0, sizeof(app)); 291 if (op_id) memcpy(app.op_id.bytes, op_id->bytes, OMEGA_ID_BYTES); 292 app.operand_count = 2; 293 if (arg1) memcpy(app.operands[0].bytes, arg1->bytes, OMEGA_ID_BYTES); 294 if (arg2) memcpy(app.operands[1].bytes, arg2->bytes, OMEGA_ID_BYTES); 295 memcpy(obj->payload, &app, sizeof(app)); 296 obj->payload_len = sizeof(app); 297 omega_compute_semantic_id(obj); 298 return obj; 299 } 300 301 /* ---- effect payload v2 (spec/effect-cap64-migration.md) ---- */ 302 303 static void eff_put16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)(v >> 8); p[1] = (uint8_t)v; } 304 static void eff_put32(uint8_t *p, uint32_t v) { 305 for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (24 - 8 * i)); 306 } 307 static void eff_put64(uint8_t *p, uint64_t v) { 308 for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (56 - 8 * i)); 309 } 310 static uint16_t eff_get16(const uint8_t *p) { return (uint16_t)(((uint16_t)p[0] << 8) | p[1]); } 311 static uint32_t eff_get32(const uint8_t *p) { 312 uint32_t v = 0; 313 for (int i = 0; i < 4; i++) v = (v << 8) | p[i]; 314 return v; 315 } 316 static uint64_t eff_get64(const uint8_t *p) { 317 uint64_t v = 0; 318 for (int i = 0; i < 8; i++) v = (v << 8) | p[i]; 319 return v; 320 } 321 322 enum { 323 EFF_OFF_RESOURCE = 0x00, EFF_OFF_OPERATION = 0x02, EFF_OFF_SLOT = 0x04, 324 EFF_OFF_GENERATION = 0x08, EFF_OFF_CAP_REF = 0x10, EFF_OFF_PARAM_LEN = 0x30, 325 EFF_OFF_PARAMS = 0x32 326 }; 327 _Static_assert(EFF_OFF_PARAMS + OMEGA_EFFECT_PARAM_MAX == OMEGA_EFFECT_PAYLOAD_LEN, 328 "effect payload v2 is 178 bytes"); 329 _Static_assert(OMEGA_EFFECT_PAYLOAD_LEN != OMEGA_EFFECT_LEGACY_V1_LEN, "v1 and v2 lengths differ"); 330 331 int omega_effect_payload_encode(const EffectPayload *eff, uint8_t out[OMEGA_EFFECT_PAYLOAD_LEN]) { 332 if (!eff || !out) return OMEGA_EFFECT_ERR_ARG; 333 eff_put16(out + EFF_OFF_RESOURCE, eff->resource_class); 334 eff_put16(out + EFF_OFF_OPERATION, eff->operation_code); 335 eff_put32(out + EFF_OFF_SLOT, eff->capability_slot); 336 eff_put64(out + EFF_OFF_GENERATION, eff->capability_generation); 337 memcpy(out + EFF_OFF_CAP_REF, eff->capability_ref.bytes, OMEGA_ID_BYTES); 338 eff_put16(out + EFF_OFF_PARAM_LEN, eff->param_len); 339 memcpy(out + EFF_OFF_PARAMS, eff->param_bytes, OMEGA_EFFECT_PARAM_MAX); 340 return OMEGA_EFFECT_OK; 341 } 342 343 int omega_effect_payload_decode(const uint8_t *bytes, size_t len, EffectPayload *out) { 344 if (out) memset(out, 0, sizeof *out); 345 if (!bytes || !out) return OMEGA_EFFECT_ERR_ARG; 346 if (len == OMEGA_EFFECT_LEGACY_V1_LEN) return OMEGA_EFFECT_ERR_LEGACY_V1; 347 if (len != OMEGA_EFFECT_PAYLOAD_LEN) return OMEGA_EFFECT_ERR_LENGTH; 348 EffectPayload e; 349 memset(&e, 0, sizeof e); 350 e.resource_class = eff_get16(bytes + EFF_OFF_RESOURCE); 351 e.operation_code = eff_get16(bytes + EFF_OFF_OPERATION); 352 e.capability_slot = eff_get32(bytes + EFF_OFF_SLOT); 353 e.capability_generation = eff_get64(bytes + EFF_OFF_GENERATION); 354 memcpy(e.capability_ref.bytes, bytes + EFF_OFF_CAP_REF, OMEGA_ID_BYTES); 355 e.param_len = eff_get16(bytes + EFF_OFF_PARAM_LEN); 356 if (e.resource_class == 0) return OMEGA_EFFECT_ERR_RESOURCE; 357 if (e.param_len > OMEGA_EFFECT_PARAM_MAX) return OMEGA_EFFECT_ERR_PARAM_LEN; 358 for (size_t i = e.param_len; i < OMEGA_EFFECT_PARAM_MAX; i++) 359 if (bytes[EFF_OFF_PARAMS + i] != 0) return OMEGA_EFFECT_ERR_PADDING; 360 memcpy(e.param_bytes, bytes + EFF_OFF_PARAMS, e.param_len); 361 *out = e; 362 return OMEGA_EFFECT_OK; 363 } 364 365 int omega_effect_write(OmegaObject *obj, const EffectPayload *eff) { 366 if (!obj || !eff || obj->kind != KIND_EFFECT) return OMEGA_EFFECT_ERR_ARG; 367 int rc = omega_effect_payload_encode(eff, obj->payload); 368 if (rc != OMEGA_EFFECT_OK) return rc; 369 obj->payload_len = OMEGA_EFFECT_PAYLOAD_LEN; 370 return OMEGA_EFFECT_OK; 371 } 372 373 int omega_effect_read(const OmegaObject *obj, EffectPayload *out) { 374 if (out) memset(out, 0, sizeof *out); 375 if (!obj || !out || obj->kind != KIND_EFFECT) return OMEGA_EFFECT_ERR_ARG; 376 if (obj->payload_len > OMEGA_MAX_PAYLOAD_LEN) return OMEGA_EFFECT_ERR_LENGTH; 377 return omega_effect_payload_decode(obj->payload, obj->payload_len, out); 378 } 379 380 const char *omega_effect_strerror(int code) { 381 switch (code) { 382 case OMEGA_EFFECT_OK: return "ok"; 383 case OMEGA_EFFECT_ERR_ARG: return "not an effect object"; 384 case OMEGA_EFFECT_ERR_LENGTH: return "effect payload length is not 178 bytes"; 385 case OMEGA_EFFECT_ERR_LEGACY_V1: return "legacy v1 effect payload (32-bit generation) refused"; 386 case OMEGA_EFFECT_ERR_RESOURCE: return "effect resource class is 0 (invalid resource)"; 387 case OMEGA_EFFECT_ERR_PARAM_LEN: return "effect param_len exceeds 128"; 388 case OMEGA_EFFECT_ERR_PADDING: return "effect parameter padding is not zero"; 389 default: return "unknown effect error"; 390 } 391 } 392 393 OmegaObject* omega_build_effect(OmegaGraph *g, uint16_t res_class, uint16_t op_code, uint32_t cap_slot, uint64_t cap_gen) { 394 OmegaObject *obj = omega_graph_add_object(g, KIND_EFFECT); 395 if (!obj) return NULL; 396 EffectPayload eff; 397 memset(&eff, 0, sizeof(eff)); 398 eff.resource_class = res_class; 399 eff.operation_code = op_code; 400 eff.capability_slot = cap_slot; 401 eff.capability_generation = cap_gen; 402 omega_effect_write(obj, &eff); 403 omega_compute_semantic_id(obj); 404 return obj; 405 } 406 407 int omega_eval_pure_binary_uint(OpCode op, OverflowPolicy ov, uint16_t width, uint64_t a, uint64_t b, uint64_t *out_res) { 408 if (!out_res) return -1; 409 uint64_t mask = (width >= 64) ? 0xFFFFFFFFFFFFFFFFULL : ((1ULL << width) - 1ULL); 410 a &= mask; 411 b &= mask; 412 413 uint64_t res = 0; 414 bool overflowed = false; 415 416 switch (op) { 417 case OP_ADD: { 418 res = a + b; 419 if (res < a || (res & ~mask) != 0) { 420 overflowed = true; 421 } 422 break; 423 } 424 case OP_SUB: { 425 if (a < b) { 426 overflowed = true; 427 res = (a + (mask + 1)) - b; 428 } else { 429 res = a - b; 430 } 431 break; 432 } 433 case OP_MUL: { 434 __uint128_t full = (__uint128_t)a * (__uint128_t)b; 435 if ((full >> width) != 0) { 436 overflowed = true; 437 } 438 res = (uint64_t)(full & mask); 439 break; 440 } 441 case OP_DIV: { 442 if (b == 0) return -1; /* Division by zero failure */ 443 res = a / b; 444 break; 445 } 446 case OP_EQUAL: { 447 *out_res = (a == b) ? 1 : 0; 448 return 0; 449 } 450 case OP_LESS_THAN: { 451 *out_res = (a < b) ? 1 : 0; 452 return 0; 453 } 454 case OP_AND: { 455 res = a & b; 456 break; 457 } 458 case OP_OR: { 459 res = a | b; 460 break; 461 } 462 default: 463 return -1; 464 } 465 466 if (overflowed) { 467 if (ov == OVERFLOW_FAIL_CLOSED) return -2; 468 if (ov == OVERFLOW_SATURATE) res = mask; 469 /* OVERFLOW_WRAP naturally applies mask */ 470 } 471 472 *out_res = res & mask; 473 return 0; 474 }