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 / visor / visor_semantic.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 | /* |
| 18 | * visor_semantic.c -- Omega Visor V1, lane 1: read-only semantic inspection. |
| 19 | * See visor_semantic.h for the contract. Nothing here writes to the graph. |
| 20 | */ |
| 21 | #include "visor_semantic.h" |
| 22 | #include "visor.h" |
| 23 | #include "omega_core.h" |
| 24 | #include "omega_canonical.h" |
| 25 | #include "sha256.h" |
| 26 | |
| 27 | #include <stdarg.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | /* ------------------------------------------------------------------ names */ |
| 32 | |
| 33 | const char *visor_semantic_opcode_name(unsigned op) { |
| 34 | static const char *const names[] = { |
| 35 | "INVALID", "IDENTITY", "CONSTANT", "ADD", "SUB", "MUL", "DIV", "EQUAL", |
| 36 | "LESS_THAN", "AND", "OR", "NOT", "SELECT", "CONCAT", "SLICE", "COMPILE" |
| 37 | }; |
| 38 | return op < sizeof(names) / sizeof(names[0]) ? names[op] : "UNKNOWN"; |
| 39 | } |
| 40 | |
| 41 | const char *visor_semantic_overflow_name(unsigned ov) { |
| 42 | static const char *const names[] = { "DEFAULT", "WRAP", "SATURATE", "FAIL_CLOSED" }; |
| 43 | return ov < sizeof(names) / sizeof(names[0]) ? names[ov] : "UNKNOWN"; |
| 44 | } |
| 45 | |
| 46 | const char *visor_semantic_relation_name(unsigned kind) { |
| 47 | static const char *const names[] = { |
| 48 | "INVALID", "EQUAL", "NOT_EQUAL", "LESS_THAN", "CONTAINS", "SUBSET_OF", |
| 49 | "DEPENDS_ON", "DERIVED_FROM", "SATISFIES", "EQUIVALENT_TO" |
| 50 | }; |
| 51 | return kind < sizeof(names) / sizeof(names[0]) ? names[kind] : "UNKNOWN"; |
| 52 | } |
| 53 | |
| 54 | const char *visor_semantic_constraint_name(unsigned kind) { |
| 55 | static const char *const names[] = { |
| 56 | "INVALID", "EQUALITY", "INEQUALITY", "RANGE", "TYPE", "LENGTH", |
| 57 | "CONTAINMENT", "PRECONDITION", "POSTCONDITION", "INVARIANT" |
| 58 | }; |
| 59 | return kind < sizeof(names) / sizeof(names[0]) ? names[kind] : "UNKNOWN"; |
| 60 | } |
| 61 | |
| 62 | const char *visor_semantic_shape_name(VisorShape s) { |
| 63 | switch (s) { |
| 64 | case VISOR_SHAPE_TYPE: return "TYPE"; |
| 65 | case VISOR_SHAPE_VALUE: return "VALUE"; |
| 66 | case VISOR_SHAPE_OPERATION: return "OPERATION"; |
| 67 | case VISOR_SHAPE_APPLY: return "APPLY"; |
| 68 | case VISOR_SHAPE_OPAQUE: return "OPAQUE"; |
| 69 | default: return "NONE"; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | static void copy_name(char *dst, size_t n, const char *src) { |
| 74 | snprintf(dst, n, "%s", src); |
| 75 | } |
| 76 | |
| 77 | static bool id_is_zero(const SemanticId *id) { |
| 78 | for (int i = 0; i < OMEGA_ID_BYTES; ++i) |
| 79 | if (id->bytes[i]) return false; |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | static bool id_eq(const SemanticId *a, const SemanticId *b) { |
| 84 | return memcmp(a->bytes, b->bytes, OMEGA_ID_BYTES) == 0; |
| 85 | } |
| 86 | |
| 87 | /* ------------------------------------------------ bounded structural parse */ |
| 88 | |
| 89 | typedef struct { |
| 90 | VisorShape shape; |
| 91 | TypePayload tp; |
| 92 | ValuePayload vp; |
| 93 | OperationPayload op; |
| 94 | ApplyPayload ap; |
| 95 | size_t canonical_len; |
| 96 | uint8_t canonical_hash[32]; |
| 97 | } Parsed; |
| 98 | |
| 99 | /* Checks every bound BEFORE any field is trusted, decodes the kind payload by |
| 100 | * memcpy, and recomputes (without storing) the canonical hash. 0 ok, -2 malformed. */ |
| 101 | static int parse_object(const OmegaObject *o, Parsed *p, char *reason, size_t rn) { |
| 102 | memset(p, 0, sizeof(*p)); |
| 103 | unsigned kind = (unsigned)o->kind; |
| 104 | if (kind < KIND_VALUE || kind > KIND_PROOF) { |
| 105 | snprintf(reason, rn, "invalid kind 0x%02x", kind); |
| 106 | return -2; |
| 107 | } |
| 108 | if (o->attr_count > OMEGA_MAX_ATTRIBUTES) { snprintf(reason, rn, "attr_count %u exceeds %d", o->attr_count, OMEGA_MAX_ATTRIBUTES); return -2; } |
| 109 | if (o->rel_count > OMEGA_MAX_RELATIONS) { snprintf(reason, rn, "rel_count %u exceeds %d", o->rel_count, OMEGA_MAX_RELATIONS); return -2; } |
| 110 | if (o->const_count > OMEGA_MAX_CONSTRAINTS) { snprintf(reason, rn, "const_count %u exceeds %d", o->const_count, OMEGA_MAX_CONSTRAINTS); return -2; } |
| 111 | if (o->payload_len > OMEGA_MAX_PAYLOAD_LEN) { snprintf(reason, rn, "payload_len %u exceeds %d", (unsigned)o->payload_len, OMEGA_MAX_PAYLOAD_LEN); return -2; } |
| 112 | for (uint16_t i = 0; i < o->attr_count; ++i) { |
| 113 | if (!memchr(o->attributes[i].key, 0, OMEGA_MAX_KEY_LEN)) { snprintf(reason, rn, "attribute %u key not terminated", i); return -2; } |
| 114 | if (o->attributes[i].val_len > OMEGA_MAX_VAL_LEN) { snprintf(reason, rn, "attribute %u val_len %u too long", i, o->attributes[i].val_len); return -2; } |
| 115 | } |
| 116 | for (uint16_t i = 0; i < o->const_count; ++i) { |
| 117 | if (o->constraints[i].payload_len > sizeof(o->constraints[i].payload)) { |
| 118 | snprintf(reason, rn, "constraint %u payload_len %u too long", i, o->constraints[i].payload_len); |
| 119 | return -2; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | switch (kind) { |
| 124 | case KIND_TYPE: |
| 125 | if (o->payload_len < sizeof(TypePayload)) { snprintf(reason, rn, "TYPE payload_len %u < %zu", (unsigned)o->payload_len, sizeof(TypePayload)); return -2; } |
| 126 | memcpy(&p->tp, o->payload, sizeof(TypePayload)); |
| 127 | if ((unsigned)p->tp.tag == TYPE_INVALID || (unsigned)p->tp.tag > TYPE_FP32) { snprintf(reason, rn, "invalid type tag 0x%02x", (unsigned)p->tp.tag); return -2; } |
| 128 | p->shape = VISOR_SHAPE_TYPE; |
| 129 | break; |
| 130 | case KIND_VALUE: |
| 131 | if (o->payload_len < sizeof(ValuePayload)) { snprintf(reason, rn, "VALUE payload_len %u < %zu", (unsigned)o->payload_len, sizeof(ValuePayload)); return -2; } |
| 132 | memcpy(&p->vp, o->payload, sizeof(ValuePayload)); |
| 133 | if (p->vp.byte_len > sizeof(p->vp.bytes)) { snprintf(reason, rn, "VALUE byte_len %u > %zu", p->vp.byte_len, sizeof(p->vp.bytes)); return -2; } |
| 134 | p->shape = VISOR_SHAPE_VALUE; |
| 135 | break; |
| 136 | case KIND_OPERATION: |
| 137 | if (o->payload_len >= sizeof(OperationPayload)) { |
| 138 | memcpy(&p->op, o->payload, sizeof(OperationPayload)); |
| 139 | if ((unsigned)p->op.opcode == OP_INVALID || (unsigned)p->op.opcode > OP_CONVERT) { snprintf(reason, rn, "invalid opcode 0x%02x", (unsigned)p->op.opcode); return -2; } |
| 140 | if ((unsigned)p->op.overflow == OVERFLOW_DEFAULT || (unsigned)p->op.overflow > OVERFLOW_FAIL_CLOSED) { snprintf(reason, rn, "invalid overflow policy 0x%02x", (unsigned)p->op.overflow); return -2; } |
| 141 | if (p->op.arity > 4) { snprintf(reason, rn, "arity %u > 4", p->op.arity); return -2; } |
| 142 | p->shape = VISOR_SHAPE_OPERATION; |
| 143 | } else if (o->payload_len >= sizeof(ApplyPayload)) { |
| 144 | memcpy(&p->ap, o->payload, sizeof(ApplyPayload)); |
| 145 | if (p->ap.operand_count > 4) { snprintf(reason, rn, "apply operand_count %u > 4", p->ap.operand_count); return -2; } |
| 146 | p->shape = VISOR_SHAPE_APPLY; |
| 147 | } else { |
| 148 | snprintf(reason, rn, "OPERATION payload_len %u < %zu (apply) ", (unsigned)o->payload_len, sizeof(ApplyPayload)); |
| 149 | return -2; |
| 150 | } |
| 151 | break; |
| 152 | default: |
| 153 | p->shape = VISOR_SHAPE_OPAQUE; |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | uint8_t buf[4096]; |
| 158 | if (omega_canonical_encode(o, buf, sizeof(buf), &p->canonical_len) != 0) { |
| 159 | snprintf(reason, rn, "canonical encoding failed"); |
| 160 | return -2; |
| 161 | } |
| 162 | sha256_hash(buf, p->canonical_len, p->canonical_hash); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static const OmegaObject *find(const OmegaGraph *g, const SemanticId *id) { |
| 167 | return omega_graph_find_object_const(g, id); |
| 168 | } |
| 169 | |
| 170 | /* Lookup + parse + id consistency. 0 ok, -1 missing, -2 malformed. */ |
| 171 | static int load(const OmegaGraph *g, const SemanticId *id, const OmegaObject **obj, Parsed *p, |
| 172 | char *reason, size_t rn) { |
| 173 | if (!g || !id) return -1; |
| 174 | const OmegaObject *o = find(g, id); |
| 175 | if (!o) return -1; |
| 176 | *obj = o; |
| 177 | int rc = parse_object(o, p, reason, rn); |
| 178 | if (rc != 0) return rc; |
| 179 | if (memcmp(p->canonical_hash, o->id.bytes, OMEGA_ID_BYTES) != 0) { |
| 180 | snprintf(reason, rn, "stored id does not match canonical encoding"); |
| 181 | return -2; |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /* -------------------------------------------------------------- type text */ |
| 187 | |
| 188 | static int type_text_of(const OmegaGraph *g, const SemanticId *type_id, char *out, size_t n, int depth) { |
| 189 | const OmegaObject *o; |
| 190 | Parsed p; |
| 191 | char reason[VISOR_SEM_REASON_LEN]; |
| 192 | if (depth > 8 || load(g, type_id, &o, &p, reason, sizeof(reason)) != 0 || p.shape != VISOR_SHAPE_TYPE) { |
| 193 | snprintf(out, n, "?"); |
| 194 | return -1; |
| 195 | } |
| 196 | const TypePayload *t = &p.tp; |
| 197 | switch ((unsigned)t->tag) { |
| 198 | case TYPE_UNIT: snprintf(out, n, "unit"); break; |
| 199 | case TYPE_BOOL: snprintf(out, n, "bool"); break; |
| 200 | case TYPE_UNSIGNED_INT: snprintf(out, n, "u%u", t->width); break; |
| 201 | case TYPE_SIGNED_INT: snprintf(out, n, "i%u", t->width); break; |
| 202 | case TYPE_BITVECTOR: snprintf(out, n, "bitvector<%u>", t->width); break; |
| 203 | case TYPE_BYTE: snprintf(out, n, "byte"); break; |
| 204 | case TYPE_SEQUENCE: { |
| 205 | char elem[VISOR_SEM_TYPE_TEXT]; |
| 206 | type_text_of(g, &t->elem_type, elem, sizeof(elem), depth + 1); |
| 207 | snprintf(out, n, "seq<%.64s,%u>", elem, (unsigned)t->length); |
| 208 | break; |
| 209 | } |
| 210 | case TYPE_TUPLE: snprintf(out, n, "tuple"); break; |
| 211 | case TYPE_ADDRESS: snprintf(out, n, "addr<%u>", t->width); break; |
| 212 | case TYPE_RESOURCE: snprintf(out, n, "resource"); break; |
| 213 | case TYPE_CAPABILITY_REF: snprintf(out, n, "cap_ref"); break; |
| 214 | case TYPE_EFFECT_INTENT_REF: snprintf(out, n, "effect_intent_ref"); break; |
| 215 | case TYPE_EFFECT_RECEIPT_REF: snprintf(out, n, "effect_receipt_ref"); break; |
| 216 | case TYPE_FP32: snprintf(out, n, "fp32"); break; |
| 217 | default: snprintf(out, n, "?"); return -1; |
| 218 | } |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | /* Integer width of a type usable by the u64 evaluator (uint/bitvector/byte, 1..64). */ |
| 223 | static int int_width_of(const OmegaGraph *g, const SemanticId *type_id, uint16_t *w, bool *is_bool) { |
| 224 | const OmegaObject *o; |
| 225 | Parsed p; |
| 226 | char reason[VISOR_SEM_REASON_LEN]; |
| 227 | if (load(g, type_id, &o, &p, reason, sizeof(reason)) != 0 || p.shape != VISOR_SHAPE_TYPE) return -1; |
| 228 | *is_bool = false; |
| 229 | switch ((unsigned)p.tp.tag) { |
| 230 | case TYPE_UNSIGNED_INT: |
| 231 | case TYPE_BITVECTOR: *w = p.tp.width; break; |
| 232 | case TYPE_BYTE: *w = 8; break; |
| 233 | case TYPE_BOOL: *w = 1; *is_bool = true; break; |
| 234 | default: return -1; |
| 235 | } |
| 236 | if (*w < 1 || *w > 64) return -1; |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /* Decode a parsed VALUE against its type. 0 ok, -1 not decodable. */ |
| 241 | static int decode_value(const OmegaGraph *g, const ValuePayload *vp, uint64_t *out, bool *is_bool) { |
| 242 | uint16_t w; |
| 243 | if (int_width_of(g, &vp->type_id, &w, is_bool) != 0) return -1; |
| 244 | if (*is_bool) { |
| 245 | if (vp->byte_len != 1 || vp->bytes[0] > 1) return -1; |
| 246 | *out = vp->bytes[0]; |
| 247 | return 0; |
| 248 | } |
| 249 | if (vp->byte_len != (uint16_t)((w + 7) / 8)) return -1; |
| 250 | uint64_t v = 0; |
| 251 | for (uint16_t i = 0; i < vp->byte_len; ++i) v = (v << 8) | vp->bytes[i]; |
| 252 | uint64_t mask = (w >= 64) ? ~0ULL : ((1ULL << w) - 1ULL); |
| 253 | if (v & ~mask) return -1; |
| 254 | *out = v; |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | /* Result type id per shape. 0 ok, -1 none. */ |
| 259 | static int result_type_id(const OmegaGraph *g, const Parsed *p, SemanticId *out) { |
| 260 | switch (p->shape) { |
| 261 | case VISOR_SHAPE_VALUE: *out = p->vp.type_id; break; |
| 262 | case VISOR_SHAPE_OPERATION: *out = p->op.output_type; break; |
| 263 | case VISOR_SHAPE_APPLY: { |
| 264 | const OmegaObject *o; |
| 265 | Parsed op; |
| 266 | char reason[VISOR_SEM_REASON_LEN]; |
| 267 | if (load(g, &p->ap.op_id, &o, &op, reason, sizeof(reason)) != 0 || op.shape != VISOR_SHAPE_OPERATION) return -1; |
| 268 | *out = op.op.output_type; |
| 269 | break; |
| 270 | } |
| 271 | default: return -1; |
| 272 | } |
| 273 | return id_is_zero(out) ? -1 : 0; |
| 274 | } |
| 275 | |
| 276 | /* ----------------------------------------------------------- dependencies */ |
| 277 | |
| 278 | typedef struct { |
| 279 | uint16_t count; |
| 280 | SemanticId ids[VISOR_SEM_MAX_DEPS]; |
| 281 | char roles[VISOR_SEM_MAX_DEPS][VISOR_SEM_NAME_LEN]; |
| 282 | } DepList; |
| 283 | |
| 284 | static void dep_push(DepList *d, const SemanticId *id, const char *role) { |
| 285 | if (id_is_zero(id) || d->count >= VISOR_SEM_MAX_DEPS) return; |
| 286 | for (uint16_t i = 0; i < d->count; ++i) |
| 287 | if (id_eq(&d->ids[i], id)) return; |
| 288 | d->ids[d->count] = *id; |
| 289 | copy_name(d->roles[d->count], VISOR_SEM_NAME_LEN, role); |
| 290 | d->count++; |
| 291 | } |
| 292 | |
| 293 | /* Requires a successfully parsed object. */ |
| 294 | static void collect_deps(const OmegaObject *o, const Parsed *p, DepList *d) { |
| 295 | char role[VISOR_SEM_NAME_LEN]; |
| 296 | d->count = 0; |
| 297 | for (uint16_t i = 0; i < o->rel_count; ++i) { |
| 298 | snprintf(role, sizeof(role), "rel:%s", visor_semantic_relation_name(o->relations[i].kind)); |
| 299 | dep_push(d, &o->relations[i].target_id, role); |
| 300 | } |
| 301 | switch (p->shape) { |
| 302 | case VISOR_SHAPE_TYPE: |
| 303 | if ((unsigned)p->tp.tag == TYPE_SEQUENCE) dep_push(d, &p->tp.elem_type, "elem"); |
| 304 | break; |
| 305 | case VISOR_SHAPE_VALUE: |
| 306 | dep_push(d, &p->vp.type_id, "type"); |
| 307 | break; |
| 308 | case VISOR_SHAPE_OPERATION: |
| 309 | dep_push(d, &p->op.type_id, "type"); |
| 310 | for (uint8_t i = 0; i < p->op.arity; ++i) { |
| 311 | snprintf(role, sizeof(role), "input%u", i); |
| 312 | dep_push(d, &p->op.input_types[i], role); |
| 313 | } |
| 314 | dep_push(d, &p->op.output_type, "output"); |
| 315 | break; |
| 316 | case VISOR_SHAPE_APPLY: |
| 317 | dep_push(d, &p->ap.op_id, "op"); |
| 318 | for (uint8_t i = 0; i < p->ap.operand_count; ++i) { |
| 319 | snprintf(role, sizeof(role), "operand%u", i); |
| 320 | dep_push(d, &p->ap.operands[i], role); |
| 321 | } |
| 322 | break; |
| 323 | default: |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /* ---------------------------------------------------------------- inspect */ |
| 329 | |
| 330 | static bool printable(const uint8_t *b, size_t n) { |
| 331 | for (size_t i = 0; i < n; ++i) |
| 332 | if (b[i] < 0x20 || b[i] > 0x7e) return false; |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | int visor_semantic_inspect(const OmegaGraph *g, const SemanticId *id, VisorObjectView *out) { |
| 337 | if (!g || !id || !out) return -1; |
| 338 | const OmegaObject *o = find(g, id); |
| 339 | if (!o) return -1; |
| 340 | |
| 341 | memset(out, 0, sizeof(*out)); |
| 342 | out->id = o->id; |
| 343 | visor_format_id(&o->id, out->id_text); |
| 344 | out->kind = (uint8_t)o->kind; |
| 345 | copy_name(out->kind_name, sizeof(out->kind_name), visor_kind_name(o->kind)); |
| 346 | |
| 347 | Parsed p; |
| 348 | const OmegaObject *dummy; |
| 349 | int rc = load(g, id, &dummy, &p, out->malformed_reason, sizeof(out->malformed_reason)); |
| 350 | if (rc != 0) { |
| 351 | out->status = -2; |
| 352 | copy_name(out->shape_name, sizeof(out->shape_name), visor_semantic_shape_name(VISOR_SHAPE_NONE)); |
| 353 | return -2; |
| 354 | } |
| 355 | out->status = 0; |
| 356 | out->shape = p.shape; |
| 357 | copy_name(out->shape_name, sizeof(out->shape_name), visor_semantic_shape_name(p.shape)); |
| 358 | |
| 359 | switch (p.shape) { |
| 360 | case VISOR_SHAPE_TYPE: |
| 361 | out->type_tag = (uint8_t)p.tp.tag; |
| 362 | out->type_width = p.tp.width; |
| 363 | out->type_length = p.tp.length; |
| 364 | if ((unsigned)p.tp.tag == TYPE_SEQUENCE && !id_is_zero(&p.tp.elem_type)) { |
| 365 | out->has_elem_type = true; |
| 366 | out->elem_type = p.tp.elem_type; |
| 367 | } |
| 368 | type_text_of(g, &o->id, out->type_text, sizeof(out->type_text), 0); |
| 369 | break; |
| 370 | case VISOR_SHAPE_VALUE: |
| 371 | out->value_byte_len = p.vp.byte_len; |
| 372 | memcpy(out->value_bytes, p.vp.bytes, p.vp.byte_len); |
| 373 | type_text_of(g, &p.vp.type_id, out->type_text, sizeof(out->type_text), 0); |
| 374 | if (decode_value(g, &p.vp, &out->value_u64, &out->value_is_bool) == 0) out->has_u64 = true; |
| 375 | else out->value_is_bool = false; |
| 376 | break; |
| 377 | case VISOR_SHAPE_OPERATION: { |
| 378 | out->opcode = (uint8_t)p.op.opcode; |
| 379 | copy_name(out->opcode_name, sizeof(out->opcode_name), visor_semantic_opcode_name(p.op.opcode)); |
| 380 | out->overflow = (uint8_t)p.op.overflow; |
| 381 | copy_name(out->overflow_name, sizeof(out->overflow_name), visor_semantic_overflow_name(p.op.overflow)); |
| 382 | out->arity = p.op.arity; |
| 383 | out->op_type_id = p.op.type_id; |
| 384 | memcpy(out->input_types, p.op.input_types, sizeof(out->input_types)); |
| 385 | out->output_type = p.op.output_type; |
| 386 | char sig[VISOR_SEM_TYPE_TEXT] = "fn("; |
| 387 | size_t len = 3; |
| 388 | for (uint8_t i = 0; i < p.op.arity; ++i) { |
| 389 | char t[24]; |
| 390 | type_text_of(g, &p.op.input_types[i], t, sizeof(t), 0); |
| 391 | len += (size_t)snprintf(sig + len, sizeof(sig) - len, "%s%s", i ? "," : "", t); |
| 392 | if (len >= sizeof(sig)) len = sizeof(sig) - 1; |
| 393 | } |
| 394 | char t[24]; |
| 395 | type_text_of(g, &p.op.output_type, t, sizeof(t), 0); |
| 396 | snprintf(sig + len, sizeof(sig) - len, ")->%s", t); |
| 397 | copy_name(out->type_text, sizeof(out->type_text), sig); |
| 398 | break; |
| 399 | } |
| 400 | case VISOR_SHAPE_APPLY: { |
| 401 | out->apply_op = p.ap.op_id; |
| 402 | out->operand_count = p.ap.operand_count; |
| 403 | memcpy(out->operands, p.ap.operands, sizeof(out->operands)); |
| 404 | const OmegaObject *op_obj; |
| 405 | Parsed op; |
| 406 | char reason[VISOR_SEM_REASON_LEN]; |
| 407 | if (load(g, &p.ap.op_id, &op_obj, &op, reason, sizeof(reason)) == 0 && op.shape == VISOR_SHAPE_OPERATION) { |
| 408 | out->opcode = (uint8_t)op.op.opcode; |
| 409 | copy_name(out->opcode_name, sizeof(out->opcode_name), visor_semantic_opcode_name(op.op.opcode)); |
| 410 | out->overflow = (uint8_t)op.op.overflow; |
| 411 | copy_name(out->overflow_name, sizeof(out->overflow_name), visor_semantic_overflow_name(op.op.overflow)); |
| 412 | out->arity = op.op.arity; |
| 413 | } |
| 414 | SemanticId rt; |
| 415 | if (result_type_id(g, &p, &rt) == 0) type_text_of(g, &rt, out->type_text, sizeof(out->type_text), 0); |
| 416 | else copy_name(out->type_text, sizeof(out->type_text), "?"); |
| 417 | break; |
| 418 | } |
| 419 | default: |
| 420 | copy_name(out->type_text, sizeof(out->type_text), "-"); |
| 421 | break; |
| 422 | } |
| 423 | SemanticId rt; |
| 424 | if (result_type_id(g, &p, &rt) == 0) { |
| 425 | out->has_type_id = true; |
| 426 | out->type_id = rt; |
| 427 | } |
| 428 | |
| 429 | out->attr_count = o->attr_count; |
| 430 | for (uint16_t i = 0; i < o->attr_count; ++i) { |
| 431 | const OmegaAttribute *a = &o->attributes[i]; |
| 432 | copy_name(out->attrs[i].key, sizeof(out->attrs[i].key), a->key); |
| 433 | out->attrs[i].len = a->val_len; |
| 434 | memcpy(out->attrs[i].value, a->value, a->val_len); |
| 435 | out->attrs[i].is_utf8 = printable(a->value, a->val_len); |
| 436 | } |
| 437 | out->rel_count = o->rel_count; |
| 438 | for (uint16_t i = 0; i < o->rel_count; ++i) { |
| 439 | out->rels[i].kind = o->relations[i].kind; |
| 440 | copy_name(out->rels[i].kind_name, sizeof(out->rels[i].kind_name), visor_semantic_relation_name(o->relations[i].kind)); |
| 441 | out->rels[i].target = o->relations[i].target_id; |
| 442 | out->rels[i].resolved = find(g, &o->relations[i].target_id) != NULL; |
| 443 | } |
| 444 | out->const_count = o->const_count; |
| 445 | for (uint16_t i = 0; i < o->const_count; ++i) { |
| 446 | out->consts[i].kind = o->constraints[i].kind; |
| 447 | copy_name(out->consts[i].kind_name, sizeof(out->consts[i].kind_name), visor_semantic_constraint_name(o->constraints[i].kind)); |
| 448 | out->consts[i].len = o->constraints[i].payload_len; |
| 449 | memcpy(out->consts[i].payload, o->constraints[i].payload, o->constraints[i].payload_len); |
| 450 | } |
| 451 | |
| 452 | out->canonical_len = p.canonical_len; |
| 453 | { |
| 454 | SemanticId h; |
| 455 | memcpy(h.bytes, p.canonical_hash, OMEGA_ID_BYTES); |
| 456 | omega_hex_semantic_id(&h, out->canonical_sha256); |
| 457 | out->canonical_id_matches = id_eq(&h, &o->id); |
| 458 | } |
| 459 | |
| 460 | DepList d; |
| 461 | collect_deps(o, &p, &d); |
| 462 | out->dep_count = d.count; |
| 463 | for (uint16_t i = 0; i < d.count; ++i) { |
| 464 | out->deps[i].id = d.ids[i]; |
| 465 | copy_name(out->deps[i].role, sizeof(out->deps[i].role), d.roles[i]); |
| 466 | out->deps[i].resolved = find(g, &d.ids[i]) != NULL; |
| 467 | if (!out->deps[i].resolved) out->dangling_count++; |
| 468 | } |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | /* -------------------------------------------------------------- formatting */ |
| 473 | |
| 474 | typedef struct { |
| 475 | char *p; |
| 476 | size_t n; |
| 477 | size_t len; |
| 478 | bool overflow; |
| 479 | } Buf; |
| 480 | |
| 481 | static void bprintf(Buf *b, const char *fmt, ...) __attribute__((format(printf, 2, 3))); |
| 482 | static void bprintf(Buf *b, const char *fmt, ...) { |
| 483 | if (b->overflow) return; |
| 484 | va_list ap; |
| 485 | va_start(ap, fmt); |
| 486 | int w = vsnprintf(b->p + b->len, b->n - b->len, fmt, ap); |
| 487 | va_end(ap); |
| 488 | if (w < 0 || (size_t)w >= b->n - b->len) { |
| 489 | b->overflow = true; |
| 490 | b->p[b->len] = '\0'; |
| 491 | return; |
| 492 | } |
| 493 | b->len += (size_t)w; |
| 494 | } |
| 495 | |
| 496 | static void bhex(Buf *b, const uint8_t *d, size_t n) { |
| 497 | for (size_t i = 0; i < n && !b->overflow; ++i) bprintf(b, "%02x", d[i]); |
| 498 | } |
| 499 | |
| 500 | static void bid(Buf *b, const SemanticId *id) { |
| 501 | char t[72]; |
| 502 | visor_format_id(id, t); |
| 503 | bprintf(b, "%s", t); |
| 504 | } |
| 505 | |
| 506 | static int bfinish(Buf *b) { |
| 507 | if (b->overflow) { |
| 508 | if (b->n) b->p[0] = '\0'; |
| 509 | return -1; |
| 510 | } |
| 511 | return (int)b->len; |
| 512 | } |
| 513 | |
| 514 | int visor_semantic_format_text(const VisorObjectView *v, char *out, size_t n) { |
| 515 | if (!v || !out || n == 0) return -1; |
| 516 | Buf b = { out, n, 0, false }; |
| 517 | out[0] = '\0'; |
| 518 | bprintf(&b, "id: %s\n", v->id_text); |
| 519 | bprintf(&b, "kind: %s\n", v->kind_name); |
| 520 | if (v->status != 0) { |
| 521 | bprintf(&b, "status: MALFORMED (%s)\n", v->malformed_reason); |
| 522 | return bfinish(&b); |
| 523 | } |
| 524 | bprintf(&b, "status: ok\n"); |
| 525 | bprintf(&b, "shape: %s\n", v->shape_name); |
| 526 | bprintf(&b, "type: %s\n", v->type_text); |
| 527 | if (v->has_type_id) { bprintf(&b, "type_id: "); bid(&b, &v->type_id); bprintf(&b, "\n"); } |
| 528 | switch (v->shape) { |
| 529 | case VISOR_SHAPE_TYPE: |
| 530 | bprintf(&b, "tag: %u\nwidth: %u\nlength: %u\n", v->type_tag, v->type_width, (unsigned)v->type_length); |
| 531 | if (v->has_elem_type) { bprintf(&b, "elem_type: "); bid(&b, &v->elem_type); bprintf(&b, "\n"); } |
| 532 | break; |
| 533 | case VISOR_SHAPE_VALUE: |
| 534 | bprintf(&b, "value_bytes: "); |
| 535 | bhex(&b, v->value_bytes, v->value_byte_len); |
| 536 | bprintf(&b, "\n"); |
| 537 | if (v->has_u64) { |
| 538 | if (v->value_is_bool) bprintf(&b, "value: %s\n", v->value_u64 ? "true" : "false"); |
| 539 | else bprintf(&b, "value: %llu\n", (unsigned long long)v->value_u64); |
| 540 | } else { |
| 541 | bprintf(&b, "value: (not decodable as u64)\n"); |
| 542 | } |
| 543 | break; |
| 544 | case VISOR_SHAPE_OPERATION: |
| 545 | bprintf(&b, "opcode: %s\noverflow: %s\narity: %u\n", v->opcode_name, v->overflow_name, v->arity); |
| 546 | bprintf(&b, "op_type: "); bid(&b, &v->op_type_id); bprintf(&b, "\n"); |
| 547 | for (uint8_t i = 0; i < v->arity && i < 4; ++i) { |
| 548 | bprintf(&b, "input%u: ", i); bid(&b, &v->input_types[i]); bprintf(&b, "\n"); |
| 549 | } |
| 550 | bprintf(&b, "output: "); bid(&b, &v->output_type); bprintf(&b, "\n"); |
| 551 | break; |
| 552 | case VISOR_SHAPE_APPLY: |
| 553 | bprintf(&b, "op: "); bid(&b, &v->apply_op); bprintf(&b, "\n"); |
| 554 | bprintf(&b, "opcode: %s\noverflow: %s\n", v->opcode_name[0] ? v->opcode_name : "?", |
| 555 | v->overflow_name[0] ? v->overflow_name : "?"); |
| 556 | bprintf(&b, "operands: %u\n", v->operand_count); |
| 557 | for (uint8_t i = 0; i < v->operand_count && i < 4; ++i) { |
| 558 | bprintf(&b, " [%u] ", i); bid(&b, &v->operands[i]); bprintf(&b, "\n"); |
| 559 | } |
| 560 | break; |
| 561 | default: |
| 562 | break; |
| 563 | } |
| 564 | bprintf(&b, "attributes: %u\n", v->attr_count); |
| 565 | for (uint16_t i = 0; i < v->attr_count; ++i) { |
| 566 | const VisorAttrView *a = &v->attrs[i]; |
| 567 | bprintf(&b, " [%u] %s = hex:", i, a->key); |
| 568 | bhex(&b, a->value, a->len); |
| 569 | if (a->is_utf8) bprintf(&b, " utf8:\"%.*s\"", (int)a->len, (const char *)a->value); |
| 570 | bprintf(&b, "\n"); |
| 571 | } |
| 572 | bprintf(&b, "relations: %u\n", v->rel_count); |
| 573 | for (uint16_t i = 0; i < v->rel_count; ++i) { |
| 574 | bprintf(&b, " [%u] %s ", i, v->rels[i].kind_name); |
| 575 | bid(&b, &v->rels[i].target); |
| 576 | bprintf(&b, "%s\n", v->rels[i].resolved ? "" : " (DANGLING)"); |
| 577 | } |
| 578 | bprintf(&b, "constraints: %u\n", v->const_count); |
| 579 | for (uint16_t i = 0; i < v->const_count; ++i) { |
| 580 | bprintf(&b, " [%u] %s len=%u hex:", i, v->consts[i].kind_name, v->consts[i].len); |
| 581 | bhex(&b, v->consts[i].payload, v->consts[i].len); |
| 582 | bprintf(&b, "\n"); |
| 583 | } |
| 584 | bprintf(&b, "canonical_len: %zu\ncanonical_sha256: %s\ncanonical_id_matches: %s\n", |
| 585 | v->canonical_len, v->canonical_sha256, v->canonical_id_matches ? "yes" : "no"); |
| 586 | bprintf(&b, "dependencies: %u (dangling %u)\n", v->dep_count, v->dangling_count); |
| 587 | for (uint16_t i = 0; i < v->dep_count; ++i) { |
| 588 | bprintf(&b, " [%u] %s ", i, v->deps[i].role); |
| 589 | bid(&b, &v->deps[i].id); |
| 590 | bprintf(&b, "%s\n", v->deps[i].resolved ? "" : " (DANGLING)"); |
| 591 | } |
| 592 | return bfinish(&b); |
| 593 | } |
| 594 | |
| 595 | static void bjstr(Buf *b, const char *s, size_t n) { |
| 596 | bprintf(b, "\""); |
| 597 | for (size_t i = 0; i < n && !b->overflow; ++i) { |
| 598 | unsigned char c = (unsigned char)s[i]; |
| 599 | if (c == '"') bprintf(b, "\\\""); |
| 600 | else if (c == '\\') bprintf(b, "\\\\"); |
| 601 | else if (c < 0x20 || c > 0x7e) bprintf(b, "\\u%04x", c); |
| 602 | else bprintf(b, "%c", c); |
| 603 | } |
| 604 | bprintf(b, "\""); |
| 605 | } |
| 606 | |
| 607 | static void bjs(Buf *b, const char *s) { bjstr(b, s, strlen(s)); } |
| 608 | |
| 609 | static void bjid(Buf *b, const SemanticId *id) { |
| 610 | char t[72]; |
| 611 | visor_format_id(id, t); |
| 612 | bprintf(b, "\"%s\"", t); |
| 613 | } |
| 614 | |
| 615 | static void bjhex(Buf *b, const uint8_t *d, size_t n) { |
| 616 | bprintf(b, "\""); |
| 617 | bhex(b, d, n); |
| 618 | bprintf(b, "\""); |
| 619 | } |
| 620 | |
| 621 | int visor_semantic_format_json(const VisorObjectView *v, char *out, size_t n) { |
| 622 | if (!v || !out || n == 0) return -1; |
| 623 | Buf b = { out, n, 0, false }; |
| 624 | out[0] = '\0'; |
| 625 | bprintf(&b, "{\"id\":\"%s\",\"kind\":", v->id_text); |
| 626 | bjs(&b, v->kind_name); |
| 627 | if (v->status != 0) { |
| 628 | bprintf(&b, ",\"status\":\"malformed\",\"reason\":"); |
| 629 | bjs(&b, v->malformed_reason); |
| 630 | bprintf(&b, "}"); |
| 631 | return bfinish(&b); |
| 632 | } |
| 633 | bprintf(&b, ",\"status\":\"ok\",\"shape\":"); |
| 634 | bjs(&b, v->shape_name); |
| 635 | bprintf(&b, ",\"type\":"); |
| 636 | bjs(&b, v->type_text); |
| 637 | bprintf(&b, ",\"type_id\":"); |
| 638 | if (v->has_type_id) bjid(&b, &v->type_id); else bprintf(&b, "null"); |
| 639 | switch (v->shape) { |
| 640 | case VISOR_SHAPE_TYPE: |
| 641 | bprintf(&b, ",\"tag\":%u,\"width\":%u,\"length\":%u,\"elem_type\":", v->type_tag, v->type_width, (unsigned)v->type_length); |
| 642 | if (v->has_elem_type) bjid(&b, &v->elem_type); else bprintf(&b, "null"); |
| 643 | break; |
| 644 | case VISOR_SHAPE_VALUE: |
| 645 | bprintf(&b, ",\"value_bytes\":"); |
| 646 | bjhex(&b, v->value_bytes, v->value_byte_len); |
| 647 | bprintf(&b, ",\"value\":"); |
| 648 | if (!v->has_u64) bprintf(&b, "null"); |
| 649 | else if (v->value_is_bool) bprintf(&b, "%s", v->value_u64 ? "true" : "false"); |
| 650 | else bprintf(&b, "\"%llu\"", (unsigned long long)v->value_u64); /* string: u64 exceeds JSON-safe ints */ |
| 651 | break; |
| 652 | case VISOR_SHAPE_OPERATION: |
| 653 | bprintf(&b, ",\"opcode\":"); bjs(&b, v->opcode_name); |
| 654 | bprintf(&b, ",\"overflow\":"); bjs(&b, v->overflow_name); |
| 655 | bprintf(&b, ",\"arity\":%u,\"op_type\":", v->arity); |
| 656 | bjid(&b, &v->op_type_id); |
| 657 | bprintf(&b, ",\"inputs\":["); |
| 658 | for (uint8_t i = 0; i < v->arity && i < 4; ++i) { if (i) bprintf(&b, ","); bjid(&b, &v->input_types[i]); } |
| 659 | bprintf(&b, "],\"output\":"); |
| 660 | bjid(&b, &v->output_type); |
| 661 | break; |
| 662 | case VISOR_SHAPE_APPLY: |
| 663 | bprintf(&b, ",\"op\":"); bjid(&b, &v->apply_op); |
| 664 | bprintf(&b, ",\"opcode\":"); if (v->opcode_name[0]) bjs(&b, v->opcode_name); else bprintf(&b, "null"); |
| 665 | bprintf(&b, ",\"overflow\":"); if (v->overflow_name[0]) bjs(&b, v->overflow_name); else bprintf(&b, "null"); |
| 666 | bprintf(&b, ",\"operands\":["); |
| 667 | for (uint8_t i = 0; i < v->operand_count && i < 4; ++i) { if (i) bprintf(&b, ","); bjid(&b, &v->operands[i]); } |
| 668 | bprintf(&b, "]"); |
| 669 | break; |
| 670 | default: |
| 671 | break; |
| 672 | } |
| 673 | bprintf(&b, ",\"attributes\":["); |
| 674 | for (uint16_t i = 0; i < v->attr_count; ++i) { |
| 675 | const VisorAttrView *a = &v->attrs[i]; |
| 676 | if (i) bprintf(&b, ","); |
| 677 | bprintf(&b, "{\"key\":"); |
| 678 | bjs(&b, a->key); |
| 679 | bprintf(&b, ",\"hex\":"); |
| 680 | bjhex(&b, a->value, a->len); |
| 681 | if (a->is_utf8) { bprintf(&b, ",\"utf8\":"); bjstr(&b, (const char *)a->value, a->len); } |
| 682 | bprintf(&b, "}"); |
| 683 | } |
| 684 | bprintf(&b, "],\"relations\":["); |
| 685 | for (uint16_t i = 0; i < v->rel_count; ++i) { |
| 686 | if (i) bprintf(&b, ","); |
| 687 | bprintf(&b, "{\"kind\":"); |
| 688 | bjs(&b, v->rels[i].kind_name); |
| 689 | bprintf(&b, ",\"target\":"); |
| 690 | bjid(&b, &v->rels[i].target); |
| 691 | bprintf(&b, ",\"resolved\":%s}", v->rels[i].resolved ? "true" : "false"); |
| 692 | } |
| 693 | bprintf(&b, "],\"constraints\":["); |
| 694 | for (uint16_t i = 0; i < v->const_count; ++i) { |
| 695 | if (i) bprintf(&b, ","); |
| 696 | bprintf(&b, "{\"kind\":"); |
| 697 | bjs(&b, v->consts[i].kind_name); |
| 698 | bprintf(&b, ",\"len\":%u,\"hex\":", v->consts[i].len); |
| 699 | bjhex(&b, v->consts[i].payload, v->consts[i].len); |
| 700 | bprintf(&b, "}"); |
| 701 | } |
| 702 | bprintf(&b, "],\"canonical\":{\"len\":%zu,\"sha256\":\"%s\",\"id_matches\":%s}", |
| 703 | v->canonical_len, v->canonical_sha256, v->canonical_id_matches ? "true" : "false"); |
| 704 | bprintf(&b, ",\"dependencies\":["); |
| 705 | for (uint16_t i = 0; i < v->dep_count; ++i) { |
| 706 | if (i) bprintf(&b, ","); |
| 707 | bprintf(&b, "{\"role\":"); |
| 708 | bjs(&b, v->deps[i].role); |
| 709 | bprintf(&b, ",\"id\":"); |
| 710 | bjid(&b, &v->deps[i].id); |
| 711 | bprintf(&b, ",\"resolved\":%s}", v->deps[i].resolved ? "true" : "false"); |
| 712 | } |
| 713 | bprintf(&b, "],\"dangling\":%u}", v->dangling_count); |
| 714 | return bfinish(&b); |
| 715 | } |
| 716 | |
| 717 | /* ------------------------------------------------------------------ type_of */ |
| 718 | |
| 719 | int visor_semantic_type_of(const OmegaGraph *g, const SemanticId *id, SemanticId *out_type_id, |
| 720 | char *type_text, size_t n) { |
| 721 | const OmegaObject *o; |
| 722 | Parsed p; |
| 723 | char reason[VISOR_SEM_REASON_LEN]; |
| 724 | int rc = load(g, id, &o, &p, reason, sizeof(reason)); |
| 725 | if (rc != 0) return rc; |
| 726 | SemanticId t; |
| 727 | if (result_type_id(g, &p, &t) != 0) return -1; |
| 728 | char txt[VISOR_SEM_TYPE_TEXT]; |
| 729 | if (type_text_of(g, &t, txt, sizeof(txt), 0) != 0) return -1; /* unresolved type: fail closed */ |
| 730 | if (out_type_id) *out_type_id = t; |
| 731 | if (type_text && n) snprintf(type_text, n, "%s", txt); |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | /* ------------------------------------------------------------- apply/value */ |
| 736 | |
| 737 | int visor_semantic_apply_parts(const OmegaGraph *g, const SemanticId *apply_id, SemanticId *out_op, |
| 738 | SemanticId *out_operands, size_t *out_count) { |
| 739 | const OmegaObject *o; |
| 740 | Parsed p; |
| 741 | char reason[VISOR_SEM_REASON_LEN]; |
| 742 | int rc = load(g, apply_id, &o, &p, reason, sizeof(reason)); |
| 743 | if (rc != 0) return rc; |
| 744 | if (p.shape != VISOR_SHAPE_APPLY) return -1; |
| 745 | if (out_op) *out_op = p.ap.op_id; |
| 746 | if (out_operands) memcpy(out_operands, p.ap.operands, sizeof(SemanticId) * p.ap.operand_count); |
| 747 | if (out_count) *out_count = p.ap.operand_count; |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | int visor_semantic_value_u64(const OmegaGraph *g, const SemanticId *value_id, uint64_t *out) { |
| 752 | const OmegaObject *o; |
| 753 | Parsed p; |
| 754 | char reason[VISOR_SEM_REASON_LEN]; |
| 755 | if (!out) return -1; |
| 756 | int rc = load(g, value_id, &o, &p, reason, sizeof(reason)); |
| 757 | if (rc != 0) return rc; |
| 758 | if (p.shape != VISOR_SHAPE_VALUE) return -1; |
| 759 | bool is_bool; |
| 760 | uint64_t v; |
| 761 | if (decode_value(g, &p.vp, &v, &is_bool) != 0) return -1; |
| 762 | *out = v; |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | /* -------------------------------------------------------------------- eval */ |
| 767 | |
| 768 | static int eval_rec(const OmegaGraph *g, const SemanticId *id, uint64_t *out, int depth) { |
| 769 | if (depth > VISOR_SEM_EVAL_DEPTH) return -1; |
| 770 | const OmegaObject *o; |
| 771 | Parsed p; |
| 772 | char reason[VISOR_SEM_REASON_LEN]; |
| 773 | int rc = load(g, id, &o, &p, reason, sizeof(reason)); |
| 774 | if (rc != 0) return rc; |
| 775 | if (p.shape == VISOR_SHAPE_VALUE) { |
| 776 | bool is_bool; |
| 777 | return decode_value(g, &p.vp, out, &is_bool) == 0 ? 0 : -1; |
| 778 | } |
| 779 | if (p.shape != VISOR_SHAPE_APPLY) return -1; |
| 780 | |
| 781 | const OmegaObject *op_obj; |
| 782 | Parsed op; |
| 783 | rc = load(g, &p.ap.op_id, &op_obj, &op, reason, sizeof(reason)); |
| 784 | if (rc != 0) return rc; |
| 785 | if (op.shape != VISOR_SHAPE_OPERATION) return -1; |
| 786 | if (op.op.arity != 2 || p.ap.operand_count != 2) return -1; |
| 787 | |
| 788 | uint16_t width; |
| 789 | bool is_bool; |
| 790 | if (int_width_of(g, &op.op.type_id, &width, &is_bool) != 0 || is_bool) return -1; |
| 791 | |
| 792 | uint64_t args[2]; |
| 793 | for (int i = 0; i < 2; ++i) { |
| 794 | /* operand type must be the operation's declared input type */ |
| 795 | const OmegaObject *arg_obj; |
| 796 | Parsed arg; |
| 797 | rc = load(g, &p.ap.operands[i], &arg_obj, &arg, reason, sizeof(reason)); |
| 798 | if (rc != 0) return rc; |
| 799 | SemanticId at; |
| 800 | if (result_type_id(g, &arg, &at) != 0 || !id_eq(&at, &op.op.input_types[i])) return -1; |
| 801 | rc = eval_rec(g, &p.ap.operands[i], &args[i], depth + 1); |
| 802 | if (rc != 0) return rc; |
| 803 | } |
| 804 | uint64_t r; |
| 805 | rc = omega_eval_pure_binary_uint(op.op.opcode, op.op.overflow, width, args[0], args[1], &r); |
| 806 | if (rc != 0) return -3; |
| 807 | *out = r; |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | int visor_semantic_eval_u64(const OmegaGraph *g, const SemanticId *id, uint64_t *out) { |
| 812 | if (!g || !id || !out) return -1; |
| 813 | uint64_t r; |
| 814 | int rc = eval_rec(g, id, &r, 0); |
| 815 | if (rc == 0) *out = r; |
| 816 | return rc; |
| 817 | } |
| 818 | |
| 819 | /* ------------------------------------------------------------- graph text */ |
| 820 | |
| 821 | static void short_id(const SemanticId *id, char out[16]) { |
| 822 | char hex[65]; |
| 823 | omega_hex_semantic_id(id, hex); |
| 824 | snprintf(out, 16, "%.12s", hex); |
| 825 | } |
| 826 | |
| 827 | static void summary_line(const OmegaGraph *g, const OmegaObject *o, const Parsed *p, Buf *b) { |
| 828 | char t[VISOR_SEM_TYPE_TEXT]; |
| 829 | switch (p->shape) { |
| 830 | case VISOR_SHAPE_TYPE: |
| 831 | type_text_of(g, &o->id, t, sizeof(t), 0); |
| 832 | bprintf(b, "type %s", t); |
| 833 | break; |
| 834 | case VISOR_SHAPE_VALUE: { |
| 835 | uint64_t v; |
| 836 | bool is_bool; |
| 837 | type_text_of(g, &p->vp.type_id, t, sizeof(t), 0); |
| 838 | if (decode_value(g, &p->vp, &v, &is_bool) == 0) { |
| 839 | if (is_bool) bprintf(b, "value %s : %s", v ? "true" : "false", t); |
| 840 | else bprintf(b, "value %llu : %s", (unsigned long long)v, t); |
| 841 | } else { |
| 842 | bprintf(b, "value hex:"); |
| 843 | bhex(b, p->vp.bytes, p->vp.byte_len); |
| 844 | bprintf(b, " : %s", t); |
| 845 | } |
| 846 | break; |
| 847 | } |
| 848 | case VISOR_SHAPE_OPERATION: |
| 849 | type_text_of(g, &p->op.output_type, t, sizeof(t), 0); |
| 850 | bprintf(b, "op %s %s arity=%u -> %s", visor_semantic_opcode_name(p->op.opcode), |
| 851 | visor_semantic_overflow_name(p->op.overflow), p->op.arity, t); |
| 852 | break; |
| 853 | case VISOR_SHAPE_APPLY: { |
| 854 | const OmegaObject *op_obj; |
| 855 | Parsed op; |
| 856 | char reason[VISOR_SEM_REASON_LEN]; |
| 857 | const char *name = "?"; |
| 858 | if (load(g, &p->ap.op_id, &op_obj, &op, reason, sizeof(reason)) == 0 && op.shape == VISOR_SHAPE_OPERATION) |
| 859 | name = visor_semantic_opcode_name(op.op.opcode); |
| 860 | bprintf(b, "apply %s(", name); |
| 861 | for (uint8_t i = 0; i < p->ap.operand_count; ++i) { |
| 862 | char s[16]; |
| 863 | short_id(&p->ap.operands[i], s); |
| 864 | bprintf(b, "%s%s", i ? "," : "", s); |
| 865 | } |
| 866 | SemanticId rt; |
| 867 | if (result_type_id(g, p, &rt) == 0) type_text_of(g, &rt, t, sizeof(t), 0); |
| 868 | else snprintf(t, sizeof(t), "?"); |
| 869 | bprintf(b, ") : %s", t); |
| 870 | break; |
| 871 | } |
| 872 | default: |
| 873 | bprintf(b, "payload_len=%u", (unsigned)o->payload_len); |
| 874 | break; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | static int graph_rec(const OmegaGraph *g, const SemanticId *id, int depth, bool *visited, Buf *b) { |
| 879 | if (depth > VISOR_SEM_GRAPH_DEPTH) return -1; |
| 880 | const OmegaObject *o = find(g, id); |
| 881 | if (o && visited[(size_t)(o - g->objects)]) return 0; /* dedupe: each object printed once */ |
| 882 | for (int i = 0; i < depth; ++i) bprintf(b, " "); |
| 883 | if (!o) { |
| 884 | bprintf(b, "MISSING "); |
| 885 | bid(b, id); |
| 886 | bprintf(b, "\n"); |
| 887 | return 0; |
| 888 | } |
| 889 | size_t idx = (size_t)(o - g->objects); |
| 890 | Parsed p; |
| 891 | char reason[VISOR_SEM_REASON_LEN]; |
| 892 | const OmegaObject *dummy; |
| 893 | if (load(g, id, &dummy, &p, reason, sizeof(reason)) != 0) { |
| 894 | bprintf(b, "MALFORMED "); |
| 895 | bid(b, id); |
| 896 | bprintf(b, " %s\n", reason); |
| 897 | visited[idx] = true; |
| 898 | return 0; |
| 899 | } |
| 900 | bprintf(b, "%s ", visor_kind_name(o->kind)); |
| 901 | bid(b, id); |
| 902 | bprintf(b, " "); |
| 903 | summary_line(g, o, &p, b); |
| 904 | bprintf(b, "\n"); |
| 905 | visited[idx] = true; |
| 906 | DepList d; |
| 907 | collect_deps(o, &p, &d); |
| 908 | for (uint16_t i = 0; i < d.count; ++i) { |
| 909 | int rc = graph_rec(g, &d.ids[i], depth + 1, visited, b); |
| 910 | if (rc != 0) return rc; |
| 911 | } |
| 912 | return 0; |
| 913 | } |
| 914 | |
| 915 | int visor_semantic_graph_text(const OmegaGraph *g, const SemanticId *root, char *out, size_t n) { |
| 916 | if (!g || !root || !out || n == 0) return -1; |
| 917 | out[0] = '\0'; |
| 918 | if (!find(g, root)) return -1; |
| 919 | bool visited[OMEGA_MAX_GRAPH_OBJECTS]; |
| 920 | memset(visited, 0, sizeof(visited)); |
| 921 | Buf b = { out, n, 0, false }; |
| 922 | int rc = graph_rec(g, root, 0, visited, &b); |
| 923 | if (rc != 0) { |
| 924 | out[0] = '\0'; |
| 925 | return -1; |
| 926 | } |
| 927 | return bfinish(&b); |
| 928 | } |