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 / tests / checker2 / producer / osc_emit_ir.c

59 lines · 2482 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/* osc_emit_ir: PRODUCER-side helper for the checker2 tests (omega#446). Not part of checker2.
18 *
19 * `oscc <file> OUT` writes IR bytes only when the unit also fits an aienos unit container, which
20 * refuses reference parameters; this helper writes the canonical IR bytes of every unit the
21 * producer front end accepts, through the producer's public API only (osc_compile, osc_ir_encode
22 * from src/compiler/osc_front.h and osc_ir.h). It is linked with the producer library, never with
23 * checker2. The test compares its digest line with `oscc <file>` ir_sha256 so both are the same bytes.
24 *
25 * Usage: osc_emit_ir <file.osc> <out.ir> exit 0 written, 1 producer rejected, 2 usage / I/O. */
26#include <stdio.h>
27#include <stdlib.h>
28
29#include "osc_front.h"
30#include "sha256.h"
31
32int main(int argc, char **argv)
33{
34 if (argc != 3) { fprintf(stderr, "usage: osc_emit_ir <file.osc> <out.ir>\n"); return 2; }
35 FILE *f = fopen(argv[1], "rb");
36 if (!f) { perror(argv[1]); return 2; }
37 static char src[1 << 20];
38 size_t n = fread(src, 1, sizeof src - 1, f);
39 fclose(f);
40 src[n] = 0;
41 OscUnit *u = calloc(1, sizeof *u);
42 OscDiag d;
43 if (!u) return 2;
44 if (osc_compile(src, n, u, &d, NULL) != 0) { printf("producer=REJECT\n"); free(u); return 1; }
45 size_t len = 0;
46 if (osc_ir_encode(u, NULL, 0, &len) != 0 && len == 0) { free(u); return 2; }
47 uint8_t *buf = malloc(len);
48 if (!buf || osc_ir_encode(u, buf, len, &len) != 0) { free(u); free(buf); return 2; }
49 uint8_t h[32];
50 sha256_hash(buf, len, h);
51 printf("producer=ACCEPT ir_sha256=");
52 for (int i = 0; i < 32; i++) printf("%02x", h[i]);
53 printf("\n");
54 FILE *o = fopen(argv[2], "wb");
55 if (!o || fwrite(buf, 1, len, o) != len || fclose(o) != 0) { perror(argv[2]); return 2; }
56 free(buf);
57 free(u);
58 return 0;
59}