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 / osh / host / osh_core.c

90 lines · 3451 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/* osh_core.c -- see osh_core.h. */
18#include "osh_core.h"
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include "osc_front.h"
25#include "osc_interp.h"
26
27static const char *const k_entry[OSH_U_COUNT] = {"lex_run", "parse_run", "expand_run"};
28
29int osh_core_init(OshCore *c, const char *const src[OSH_U_COUNT], const size_t len[OSH_U_COUNT], char *err, size_t errsz)
30{
31 memset(c, 0, sizeof *c);
32 for (int i = 0; i < OSH_U_COUNT; i++) {
33 OshCoreUnit *u = &c->u[i];
34 u->U = calloc(1, sizeof *u->U);
35 u->RI = calloc(1, sizeof *u->RI);
36 u->RN = calloc(1, sizeof *u->RN);
37 if (!u->U || !u->RI || !u->RN) { snprintf(err, errsz, "out of memory"); return -1; }
38 OscDiag d;
39 if (osc_compile(src[i], len[i], u->U, &d, NULL) != 0) {
40 snprintf(err, errsz, "%s unit refused: %s line %u: %s", k_entry[i], osc_diag_kind_name(d.kind), d.line, d.message);
41 return -1;
42 }
43 char e2[200];
44 if (osc_ir_validate(u->U, e2, sizeof e2) != 0) { snprintf(err, errsz, "%s unit invalid: %s", k_entry[i], e2); return -1; }
45 u->fi = -1;
46 for (int f = 0; f < u->U->nfuncs; f++)
47 if (strcmp(u->U->funcs[f].name, k_entry[i]) == 0) u->fi = f;
48 if (u->fi < 0) { snprintf(err, errsz, "no entry %s", k_entry[i]); return -1; }
49 if (osc_cg_compile(u->U, &u->code, e2, sizeof e2) != 0) { snprintf(err, errsz, "%s codegen: %s", k_entry[i], e2); return -1; }
50 int nm = osc_native_map(&u->nm, u->code.code, u->code.len);
51 if (nm != 0) { snprintf(err, errsz, "%s native map failed (%d)", k_entry[i], nm); return -1; }
52 osc_rt_init(u->RI);
53 osc_rt_init(u->RN);
54 }
55 return 0;
56}
57
58void osh_core_free(OshCore *c)
59{
60 for (int i = 0; i < OSH_U_COUNT; i++) {
61 OshCoreUnit *u = &c->u[i];
62 if (u->code.code) {
63 osc_native_unmap(&u->nm);
64 osc_cg_free(&u->code);
65 }
66 free(u->U);
67 free(u->RI);
68 free(u->RN);
69 }
70 memset(c, 0, sizeof *c);
71}
72
73uint64_t osh_core_call(OshCore *c, int unit, int native, const uint8_t *inp, size_t n, uint64_t *w, size_t wn)
74{
75 OshCoreUnit *u = &c->u[unit];
76 uint64_t args[4] = {(uint64_t)(uintptr_t)inp, n, (uint64_t)(uintptr_t)w, wn}, ret = 0;
77 int trap;
78 if (osc_ir_slice_args_ok(&u->U->funcs[u->fi], args, 4) != 0) { c->faults++; return OSH_CORE_FAULT; }
79 if (!native) {
80 osc_rt_reset(u->RI);
81 trap = osc_interp_run_prevalidated(u->U, u->fi, args, 4, u->RI, &ret);
82 c->calls_interp++;
83 } else {
84 osc_rt_reset(u->RN);
85 trap = osc_native_call(u->U, u->fi, &u->nm, u->code.entry[u->fi], u->RN, args, 4, &ret);
86 c->calls_native++;
87 }
88 if (trap != 0) { c->faults++; return OSH_CORE_FAULT; }
89 return ret;
90}