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 / tools / omega_emit / omega_emit.c

798 lines · 39803 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/*
18 * omega_emit.c -- constrained structured emission of OSC binary IR (aien-spark/omega#445, stage 2 of
19 * the one-layer plan). A model emits an op list (JSON, schema in tools/omega_emit/SCHEMA.md); this
20 * tool checks every step against the OSC IR rules as it reads it, refuses the first invalid step with
21 * a reason, and writes the canonical OSC binary IR (osc_ir_encode, src/compiler/osc_ir.h) only after
22 * the whole unit passes osc_ir_validate and the bytes round-trip through the strict reader
23 * (osc_ir_decode -> osc_ir_encode gives the same bytes).
24 *
25 * omega_emit emit <oplist.json> <out.oscir> exit 0 written; 1 refused (one line on stdout:
26 * "REFUSED <stage>: <reason>"); 2 usage / I/O
27 * omega_emit lift <in.oscir> canonical IR -> op list JSON on stdout (exit 1 if the
28 * unit uses something the schema does not cover)
29 *
30 * Stages in a refusal: json (not strict JSON), schema (shape: keys, kinds, names), step (one op breaks
31 * an IR rule; the message names function, block and op), validator (osc_ir_validate on the whole unit,
32 * e.g. a register used before it is defined), encoding (encode/decode round trip; never expected).
33 * Nothing is repaired or guessed: the first defect wins.
34 */
35#include <errno.h>
36#include <inttypes.h>
37#include <stdarg.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41
42#include "osc_ir.h"
43
44#define MAX_IN (1u << 20)
45#define MAX_DEPTH 64
46
47/* ============================================================================== refusal */
48
49static char g_why[512];
50static int fail(const char *stage, const char *fmt, ...)
51{
52 char m[400];
53 va_list ap;
54 va_start(ap, fmt);
55 vsnprintf(m, sizeof m, fmt, ap);
56 va_end(ap);
57 snprintf(g_why, sizeof g_why, "%s: %s", stage, m);
58 return -1;
59}
60
61/* ================================================================================= JSON */
62
63enum { J_OBJ = 1, J_ARR, J_STR, J_NUM, J_TRUE, J_FALSE, J_NULL };
64
65typedef struct J {
66 int t;
67 size_t off; /* byte offset in the input (for messages) */
68 char *s; /* J_STR: decoded text (ASCII); J_NUM: raw text */
69 size_t n; /* children */
70 struct J **kid;
71 char **key; /* J_OBJ keys */
72} J;
73
74typedef struct { const char *p; size_t n, i; int depth; } P;
75
76static void *xmalloc(size_t n)
77{
78 void *p = calloc(1, n ? n : 1);
79 if (!p) { fprintf(stderr, "omega_emit: out of memory\n"); exit(2); }
80 return p;
81}
82
83static void ws(P *p) { while (p->i < p->n && (p->p[p->i] == ' ' || p->p[p->i] == '\t' || p->p[p->i] == '\n' || p->p[p->i] == '\r')) p->i++; }
84
85static J *jnew(int t, size_t off) { J *j = xmalloc(sizeof *j); j->t = t; j->off = off; return j; }
86
87static void jadd(J *j, J *k, char *key)
88{
89 if ((j->n & (j->n - 1)) == 0) { /* grow at powers of two */
90 size_t cap = j->n ? j->n * 2 : 1;
91 J **nk = realloc(j->kid, cap * sizeof *nk);
92 char **ky = realloc(j->key, cap * sizeof *ky);
93 if (!nk || !ky) { fprintf(stderr, "omega_emit: out of memory\n"); exit(2); }
94 j->kid = nk; j->key = ky;
95 }
96 j->kid[j->n] = k; j->key[j->n] = key; j->n++;
97}
98
99static J *jvalue(P *p);
100
101static char *jstring(P *p)
102{
103 /* p->p[p->i] == '"' */
104 size_t start = ++p->i, cap = 16, n = 0;
105 char *o = xmalloc(cap);
106 (void)start;
107 for (;;) {
108 if (p->i >= p->n) { fail("json", "unterminated string at byte %zu", p->i); free(o); return NULL; }
109 unsigned char c = (unsigned char)p->p[p->i++];
110 if (c == '"') break;
111 if (c < 0x20 || c > 0x7e) { fail("json", "control or non-ASCII byte in a string at byte %zu", p->i - 1); free(o); return NULL; }
112 if (c == '\\') {
113 if (p->i >= p->n) { fail("json", "unterminated escape at byte %zu", p->i); free(o); return NULL; }
114 char e = p->p[p->i++];
115 if (e == '"' || e == '\\' || e == '/') c = (unsigned char)e;
116 else if (e == 'n') c = '\n';
117 else if (e == 't') c = '\t';
118 else if (e == 'r') c = '\r';
119 else if (e == 'b') c = '\b';
120 else if (e == 'f') c = '\f';
121 else if (e == 'u') {
122 unsigned v = 0;
123 for (int k = 0; k < 4; k++) {
124 char h = p->i < p->n ? p->p[p->i++] : 0;
125 int d = (h >= '0' && h <= '9') ? h - '0' : (h >= 'a' && h <= 'f') ? h - 'a' + 10 : (h >= 'A' && h <= 'F') ? h - 'A' + 10 : -1;
126 if (d < 0) { fail("json", "bad \\u escape at byte %zu", p->i - 1); free(o); return NULL; }
127 v = v * 16 + (unsigned)d;
128 }
129 if (v < 0x20 || v > 0x7e) { fail("json", "\\u escape outside printable ASCII at byte %zu", p->i - 6); free(o); return NULL; }
130 c = (unsigned char)v;
131 } else { fail("json", "bad escape \\%c at byte %zu", e, p->i - 1); free(o); return NULL; }
132 }
133 if (n + 1 >= cap) { cap *= 2; char *no = realloc(o, cap); if (!no) { free(o); return NULL; } o = no; }
134 o[n++] = (char)c;
135 }
136 o[n] = 0;
137 return o;
138}
139
140static J *jvalue(P *p)
141{
142 ws(p);
143 if (p->i >= p->n) { fail("json", "unexpected end of input"); return NULL; }
144 if (++p->depth > MAX_DEPTH) { fail("json", "nesting deeper than %d at byte %zu", MAX_DEPTH, p->i); return NULL; }
145 char c = p->p[p->i];
146 J *j = NULL;
147 if (c == '{') {
148 j = jnew(J_OBJ, p->i++);
149 ws(p);
150 if (p->i < p->n && p->p[p->i] == '}') { p->i++; goto done; }
151 for (;;) {
152 ws(p);
153 if (p->i >= p->n || p->p[p->i] != '"') { fail("json", "expected a key string at byte %zu", p->i); return NULL; }
154 size_t koff = p->i;
155 char *k = jstring(p);
156 if (!k) return NULL;
157 for (size_t q = 0; q < j->n; q++)
158 if (!strcmp(j->key[q], k)) { fail("json", "duplicate key \"%s\" at byte %zu", k, koff); return NULL; }
159 ws(p);
160 if (p->i >= p->n || p->p[p->i] != ':') { fail("json", "expected ':' at byte %zu", p->i); return NULL; }
161 p->i++;
162 J *v = jvalue(p);
163 if (!v) return NULL;
164 jadd(j, v, k);
165 ws(p);
166 if (p->i < p->n && p->p[p->i] == ',') { p->i++; continue; }
167 if (p->i < p->n && p->p[p->i] == '}') { p->i++; break; }
168 fail("json", "expected ',' or '}' at byte %zu", p->i);
169 return NULL;
170 }
171 } else if (c == '[') {
172 j = jnew(J_ARR, p->i++);
173 ws(p);
174 if (p->i < p->n && p->p[p->i] == ']') { p->i++; goto done; }
175 for (;;) {
176 J *v = jvalue(p);
177 if (!v) return NULL;
178 jadd(j, v, NULL);
179 ws(p);
180 if (p->i < p->n && p->p[p->i] == ',') { p->i++; continue; }
181 if (p->i < p->n && p->p[p->i] == ']') { p->i++; break; }
182 fail("json", "expected ',' or ']' at byte %zu", p->i);
183 return NULL;
184 }
185 } else if (c == '"') {
186 j = jnew(J_STR, p->i);
187 if (!(j->s = jstring(p))) return NULL;
188 } else if (c == '-' || (c >= '0' && c <= '9')) {
189 j = jnew(J_NUM, p->i);
190 size_t s = p->i;
191 if (p->p[p->i] == '-') p->i++;
192 if (p->i >= p->n || p->p[p->i] < '0' || p->p[p->i] > '9') { fail("json", "bad number at byte %zu", s); return NULL; }
193 if (p->p[p->i] == '0' && p->i + 1 < p->n && p->p[p->i + 1] >= '0' && p->p[p->i + 1] <= '9') { fail("json", "number with a leading zero at byte %zu", s); return NULL; }
194 while (p->i < p->n && ((p->p[p->i] >= '0' && p->p[p->i] <= '9') || p->p[p->i] == '.' || p->p[p->i] == 'e' ||
195 p->p[p->i] == 'E' || p->p[p->i] == '+' || p->p[p->i] == '-'))
196 p->i++;
197 j->s = xmalloc(p->i - s + 1);
198 memcpy(j->s, p->p + s, p->i - s);
199 } else if (p->n - p->i >= 4 && !memcmp(p->p + p->i, "true", 4)) { j = jnew(J_TRUE, p->i); p->i += 4; }
200 else if (p->n - p->i >= 5 && !memcmp(p->p + p->i, "false", 5)) { j = jnew(J_FALSE, p->i); p->i += 5; }
201 else if (p->n - p->i >= 4 && !memcmp(p->p + p->i, "null", 4)) { j = jnew(J_NULL, p->i); p->i += 4; }
202 else { fail("json", "unexpected character '%c' at byte %zu", c >= 0x20 && c < 0x7f ? c : '?', p->i); return NULL; }
203done:
204 p->depth--;
205 return j;
206}
207
208static J *jparse(const char *s, size_t n)
209{
210 P p = {s, n, 0, 0};
211 J *j = jvalue(&p);
212 if (!j) return NULL;
213 ws(&p);
214 if (p.i != n) { fail("json", "trailing data after the JSON value at byte %zu", p.i); return NULL; }
215 return j;
216}
217
218static J *jget(const J *o, const char *k)
219{
220 for (size_t i = 0; i < o->n; i++) if (!strcmp(o->key[i], k)) return o->kid[i];
221 return NULL;
222}
223
224static const char *jtname(int t)
225{
226 static const char *const n[] = {"?", "object", "array", "string", "number", "true", "false", "null"};
227 return n[t >= 1 && t <= 7 ? t : 0];
228}
229
230/* Exact integer from a JSON number: [-]digits only (no fraction or exponent). 0 ok. */
231static int jint(const J *j, int *neg, uint64_t *mag)
232{
233 if (j->t != J_NUM) return -1;
234 const char *s = j->s;
235 *neg = 0;
236 if (*s == '-') { *neg = 1; s++; }
237 if (!*s) return -1;
238 uint64_t v = 0;
239 for (; *s; s++) {
240 if (*s < '0' || *s > '9') return -1;
241 if (v > (UINT64_MAX - (uint64_t)(*s - '0')) / 10) return -1;
242 v = v * 10 + (uint64_t)(*s - '0');
243 }
244 *mag = v;
245 return 0;
246}
247
248/* ================================================================================ types */
249
250static const char *const k_scal[] = {"void", "bool", "u8", "u16", "u32", "u64", "i8", "i16", "i32", "i64"};
251static const char *const k_refk[] = {"", "own", "shared", "mut"};
252
253static int scalar_from(const char *s)
254{
255 for (int i = 1; i <= 9; i++) if (!strcmp(s, k_scal[i])) return i;
256 return -1;
257}
258
259/* "u32" | "own:u32:8" | "shared:u32:8" | "mut:u32:8" */
260static int type_from(const char *s, OscType *t)
261{
262 memset(t, 0, sizeof *t);
263 int sc = scalar_from(s);
264 if (sc > 0) { t->s = (OscScalar)sc; return 0; }
265 const char *c1 = strchr(s, ':');
266 if (!c1) return -1;
267 const char *c2 = strchr(c1 + 1, ':');
268 if (!c2) return -1;
269 int rk = 0;
270 for (int k = 1; k <= 3; k++) if ((size_t)(c1 - s) == strlen(k_refk[k]) && !strncmp(s, k_refk[k], (size_t)(c1 - s))) rk = k;
271 if (!rk) return -1;
272 char el[8];
273 size_t el_n = (size_t)(c2 - c1 - 1);
274 if (el_n == 0 || el_n >= sizeof el) return -1;
275 memcpy(el, c1 + 1, el_n); el[el_n] = 0;
276 int e = scalar_from(el);
277 if (e < 0) return -1;
278 const char *d = c2 + 1;
279 if (!*d || (d[0] == '0') || strlen(d) > 2) return -1;
280 unsigned len = 0;
281 for (; *d; d++) { if (*d < '0' || *d > '9') return -1; len = len * 10 + (unsigned)(*d - '0'); }
282 if (len < 1 || len > OSC_MAX_ARRAY_LEN) return -1;
283 t->s = OSC_T_REF; t->ref = (OscRefKind)rk; t->elem = (OscScalar)e; t->len = (uint16_t)len;
284 return 0;
285}
286
287static void type_name(const OscType *t, char *o, size_t n)
288{
289 if (t->s == OSC_T_REF && t->sid == 0 && t->ref >= 1 && t->ref <= 3 && t->elem >= 1 && t->elem <= 9)
290 snprintf(o, n, "%s:%s:%u", k_refk[t->ref], k_scal[t->elem], t->len);
291 else if (t->s <= OSC_T_I64 && t->ref == 0 && t->len == 0 && t->sid == 0) snprintf(o, n, "%s", k_scal[t->s]);
292 else snprintf(o, n, "?");
293}
294
295static int is_int(const OscType *t) { return t->s >= OSC_T_U8 && t->s <= OSC_T_I64 && t->ref == 0; }
296static int is_scalar(const OscType *t) { return t->s >= OSC_T_BOOL && t->s <= OSC_T_I64 && t->ref == 0; }
297static int is_arr(const OscType *t) { return t->s == OSC_T_REF && t->sid == 0; }
298/* may array register `a` be bound to array slot `p` (mov dst or call parameter)? osc_ir.c ref_bindable */
299static int ref_ok(const OscType *p, const OscType *a)
300{
301 return is_arr(p) && is_arr(a) && a->elem == p->elem && a->len == p->len &&
302 (p->ref == OSC_REF_SHARED || (p->ref == OSC_REF_MUT && a->ref != OSC_REF_SHARED) ||
303 (p->ref == OSC_REF_OWN && a->ref == OSC_REF_OWN));
304}
305static int teq(const OscType *a, const OscType *b) { return a->s == b->s && a->ref == b->ref && a->elem == b->elem && a->len == b->len && a->sid == b->sid; }
306
307/* ============================================================================== op table */
308
309typedef struct {
310 const char *name;
311 uint8_t op, sub;
312 const char *fields[5]; /* required fields in addition to "op" (NULL-terminated) */
313 const char *opt; /* one optional field (or NULL) */
314} OpDef;
315
316static const OpDef k_ops[] = {
317 {"const", OSC_I_CONST, 0, {"dst", "value"}, NULL},
318 {"mov", OSC_I_MOV, 0, {"dst", "a"}, NULL},
319 {"add", OSC_I_BIN, OSC_B_ADD, {"dst", "a", "b"}, NULL},
320 {"sub", OSC_I_BIN, OSC_B_SUB, {"dst", "a", "b"}, NULL},
321 {"mul", OSC_I_BIN, OSC_B_MUL, {"dst", "a", "b"}, NULL},
322 {"div", OSC_I_BIN, OSC_B_DIV, {"dst", "a", "b"}, NULL},
323 {"rem", OSC_I_BIN, OSC_B_REM, {"dst", "a", "b"}, NULL},
324 {"and", OSC_I_BIN, OSC_B_AND, {"dst", "a", "b"}, NULL},
325 {"or", OSC_I_BIN, OSC_B_OR, {"dst", "a", "b"}, NULL},
326 {"xor", OSC_I_BIN, OSC_B_XOR, {"dst", "a", "b"}, NULL},
327 {"shl", OSC_I_BIN, OSC_B_SHL, {"dst", "a", "b"}, NULL},
328 {"shr", OSC_I_BIN, OSC_B_SHR, {"dst", "a", "b"}, NULL},
329 {"neg", OSC_I_UN, OSC_U_NEG, {"dst", "a"}, NULL},
330 {"bnot", OSC_I_UN, OSC_U_BNOT, {"dst", "a"}, NULL},
331 {"not", OSC_I_UN, OSC_U_LNOT, {"dst", "a"}, NULL},
332 {"eq", OSC_I_CMP, OSC_C_EQ, {"dst", "a", "b"}, NULL},
333 {"ne", OSC_I_CMP, OSC_C_NE, {"dst", "a", "b"}, NULL},
334 {"lt", OSC_I_CMP, OSC_C_LT, {"dst", "a", "b"}, NULL},
335 {"le", OSC_I_CMP, OSC_C_LE, {"dst", "a", "b"}, NULL},
336 {"gt", OSC_I_CMP, OSC_C_GT, {"dst", "a", "b"}, NULL},
337 {"ge", OSC_I_CMP, OSC_C_GE, {"dst", "a", "b"}, NULL},
338 {"cast", OSC_I_CAST, 0, {"dst", "a"}, NULL},
339 {"alloc", OSC_I_ALLOC, 0, {"dst", "init"}, NULL},
340 {"release", OSC_I_RELEASE, 0, {"arr"}, NULL},
341 {"load", OSC_I_LOAD, 0, {"dst", "arr", "index"}, NULL},
342 {"store", OSC_I_STORE, 0, {"arr", "index", "value"}, NULL},
343 {"call", OSC_I_CALL, 0, {"func", "args"}, "dst"},
344 {"trap", OSC_I_TRAP, 0, {"code"}, NULL},
345 {"br", OSC_I_BR, 0, {"to"}, NULL},
346 {"cbr", OSC_I_CBR, 0, {"cond", "then", "else"}, NULL},
347 {"ret", OSC_I_RET, 0, {NULL}, "a"},
348};
349#define NOPS (sizeof k_ops / sizeof k_ops[0])
350
351static const OpDef *opdef_of(uint8_t op, uint8_t sub)
352{
353 for (size_t i = 0; i < NOPS; i++)
354 if (k_ops[i].op == op && (k_ops[i].sub == sub || (op != OSC_I_BIN && op != OSC_I_UN && op != OSC_I_CMP))) return &k_ops[i];
355 return NULL;
356}
357
358/* ================================================================================== emit */
359
360static OscUnit g_u;
361
362typedef struct {
363 const char *fname;
364 unsigned fi, blk, k; /* step position, for messages */
365 const char *opname;
366 OscFunc *f;
367 unsigned nblocks;
368} Ctx;
369
370#define STEP(...) fail("step", __VA_ARGS__)
371#define AT "function %s block %u op %u (%s): "
372#define ATV c->fname, c->blk, c->k, c->opname
373
374static int reg_field(Ctx *c, const J *o, const char *k, int16_t *out)
375{
376 const J *j = jget(o, k);
377 int neg; uint64_t v;
378 if (!j || jint(j, &neg, &v) || neg) return STEP(AT "\"%s\" must be a register number (0 or more)", ATV, k);
379 if (v >= c->f->nvregs) return STEP(AT "\"%s\" is register %" PRIu64 " but this function has registers 0..%u", ATV, k, v, c->f->nvregs - 1u);
380 *out = (int16_t)v;
381 return 0;
382}
383
384static int blk_field(Ctx *c, const J *o, const char *k, int16_t *out)
385{
386 const J *j = jget(o, k);
387 int neg; uint64_t v;
388 if (!j || jint(j, &neg, &v) || neg) return STEP(AT "\"%s\" must be a block number (0 or more)", ATV, k);
389 if (v >= c->nblocks) return STEP(AT "\"%s\" is block %" PRIu64 " but this function has blocks 0..%u", ATV, k, v, c->nblocks - 1u);
390 *out = (int16_t)v;
391 return 0;
392}
393
394static const char *tn(const OscType *t)
395{
396 static char b[4][32];
397 static int i;
398 i = (i + 1) & 3;
399 type_name(t, b[i], sizeof b[i]);
400 return b[i];
401}
402
403static int find_func(const char *name, unsigned before)
404{
405 for (unsigned i = 0; i < before; i++) if (!strcmp(g_u.funcs[i].name, name)) return (int)i;
406 return -1;
407}
408
409/* One step: parse the op object, check it against the IR rules, fill *in. */
410static int step(Ctx *c, const J *o, int last, OscInsn *in)
411{
412 c->opname = "?";
413 if (o->t != J_OBJ) return STEP("function %s block %u op %u: an op must be an object, not %s", c->fname, c->blk, c->k, jtname(o->t));
414 const J *jo = jget(o, "op");
415 if (!jo || jo->t != J_STR) return STEP("function %s block %u op %u: missing \"op\" string", c->fname, c->blk, c->k);
416 const OpDef *d = NULL;
417 for (size_t i = 0; i < NOPS; i++) if (!strcmp(k_ops[i].name, jo->s)) d = &k_ops[i];
418 if (!d) return STEP("function %s block %u op %u: unknown op \"%s\"", c->fname, c->blk, c->k, jo->s);
419 c->opname = d->name;
420 /* exactly the declared fields */
421 for (size_t i = 0; i < o->n; i++) {
422 const char *k = o->key[i];
423 int ok = !strcmp(k, "op") || (d->opt && !strcmp(k, d->opt));
424 for (int f = 0; f < 5 && d->fields[f]; f++) if (!strcmp(k, d->fields[f])) ok = 1;
425 if (!ok) return STEP(AT "unknown field \"%s\"", ATV, k);
426 }
427 for (int f = 0; f < 5 && d->fields[f]; f++)
428 if (!jget(o, d->fields[f])) return STEP(AT "missing field \"%s\"", ATV, d->fields[f]);
429 memset(in, 0, sizeof *in);
430 in->dst = in->a = in->b = in->c = in->blk_t = in->blk_f = in->callee = -1;
431 in->op = d->op; in->sub = d->sub;
432 const OscType *T = c->f->vtype;
433 int term = d->op == OSC_I_BR || d->op == OSC_I_CBR || d->op == OSC_I_RET;
434 if (term && !last) return STEP(AT "br, cbr and ret end a block: this one is followed by more ops", ATV);
435 if (!term && last) return STEP(AT "the last op of a block must be br, cbr or ret", ATV);
436 switch (d->op) {
437 case OSC_I_CONST: {
438 if (reg_field(c, o, "dst", &in->dst)) return -1;
439 const OscType *t = &T[in->dst];
440 if (!is_scalar(t)) return STEP(AT "dst r%d is %s; const needs a scalar register", ATV, in->dst, tn(t));
441 const J *v = jget(o, "value");
442 int neg; uint64_t m;
443 if (t->s == OSC_T_BOOL) {
444 if (v->t == J_TRUE) in->imm = 1;
445 else if (v->t == J_FALSE) in->imm = 0;
446 else if (!jint(v, &neg, &m) && !neg && m <= 1) in->imm = m;
447 else return STEP(AT "a bool value is true, false, 0 or 1", ATV);
448 break;
449 }
450 if (jint(v, &neg, &m)) return STEP(AT "\"value\" must be a whole decimal number", ATV);
451 unsigned w = osc_scalar_width(t->s);
452 if (!osc_scalar_signed(t->s)) {
453 if (neg && m) return STEP(AT "value -%" PRIu64 " is negative; r%d is %s", ATV, m, in->dst, tn(t));
454 if (w < 64 && (m >> w)) return STEP(AT "value %" PRIu64 " does not fit %s", ATV, m, tn(t));
455 in->imm = m;
456 } else {
457 uint64_t lim = (uint64_t)1 << (w - 1);
458 if (neg ? m > lim : m >= lim) return STEP(AT "value %s%" PRIu64 " does not fit %s", ATV, neg ? "-" : "", m, tn(t));
459 in->imm = neg ? (uint64_t)0 - m : m; /* canonical: sign-extended to 64 bits */
460 }
461 break;
462 }
463 case OSC_I_MOV:
464 if (reg_field(c, o, "dst", &in->dst) || reg_field(c, o, "a", &in->a)) return -1;
465 if (is_arr(&T[in->dst])) { /* EMIT:mov-ref: binding rule of osc_ir.c ref_bindable, as for call arguments */
466 if (!ref_ok(&T[in->dst], &T[in->a]))
467 return STEP(AT "mov to r%d (%s) from r%d (%s): an array register takes an own: (move), own: or mut: (into mut:), or any array (into shared:) of the same element type and length",
468 ATV, in->dst, tn(&T[in->dst]), in->a, tn(&T[in->a]));
469 } else if (!is_scalar(&T[in->dst]) || !teq(&T[in->dst], &T[in->a]))
470 return STEP(AT "mov needs two scalar registers of the same type (r%d is %s, r%d is %s)", ATV, in->dst, tn(&T[in->dst]), in->a, tn(&T[in->a]));
471 break;
472 case OSC_I_BIN: {
473 if (reg_field(c, o, "dst", &in->dst) || reg_field(c, o, "a", &in->a) || reg_field(c, o, "b", &in->b)) return -1;
474 const OscType *D = &T[in->dst], *A = &T[in->a], *B = &T[in->b];
475 if (!is_scalar(D) || !teq(D, A)) return STEP(AT "dst r%d (%s) and a r%d (%s) must have the same scalar type", ATV, in->dst, tn(D), in->a, tn(A));
476 if (in->sub == OSC_B_SHL || in->sub == OSC_B_SHR) {
477 if (!is_int(A) || !is_int(B)) return STEP(AT "shifts need integer registers (a is %s, b is %s)", ATV, tn(A), tn(B));
478 } else if (in->sub == OSC_B_AND || in->sub == OSC_B_OR || in->sub == OSC_B_XOR) {
479 if (!teq(A, B)) return STEP(AT "a r%d (%s) and b r%d (%s) must have the same type", ATV, in->a, tn(A), in->b, tn(B));
480 } else if (!is_int(A) || !teq(A, B))
481 return STEP(AT "arithmetic needs a and b of the same integer type (a r%d is %s, b r%d is %s)", ATV, in->a, tn(A), in->b, tn(B));
482 break;
483 }
484 case OSC_I_UN: {
485 if (reg_field(c, o, "dst", &in->dst) || reg_field(c, o, "a", &in->a)) return -1;
486 const OscType *D = &T[in->dst], *A = &T[in->a];
487 if (!is_scalar(D) || !teq(D, A)) return STEP(AT "dst r%d (%s) and a r%d (%s) must have the same scalar type", ATV, in->dst, tn(D), in->a, tn(A));
488 if (in->sub == OSC_U_LNOT && A->s != OSC_T_BOOL) return STEP(AT "not needs a bool register (r%d is %s)", ATV, in->a, tn(A));
489 if (in->sub != OSC_U_LNOT && !is_int(A)) return STEP(AT "%s needs an integer register (r%d is %s)", ATV, d->name, in->a, tn(A));
490 break;
491 }
492 case OSC_I_CMP: {
493 if (reg_field(c, o, "dst", &in->dst) || reg_field(c, o, "a", &in->a) || reg_field(c, o, "b", &in->b)) return -1;
494 if (T[in->dst].s != OSC_T_BOOL || !is_scalar(&T[in->dst])) return STEP(AT "dst r%d is %s; a comparison writes a bool register", ATV, in->dst, tn(&T[in->dst]));
495 if (!is_scalar(&T[in->a]) || !teq(&T[in->a], &T[in->b]))
496 return STEP(AT "a r%d (%s) and b r%d (%s) must have the same scalar type", ATV, in->a, tn(&T[in->a]), in->b, tn(&T[in->b]));
497 break;
498 }
499 case OSC_I_CAST:
500 if (reg_field(c, o, "dst", &in->dst) || reg_field(c, o, "a", &in->a)) return -1;
501 if (!is_int(&T[in->dst]) || !is_int(&T[in->a]))
502 return STEP(AT "cast converts between integer registers (dst is %s, a is %s)", ATV, tn(&T[in->dst]), tn(&T[in->a]));
503 break;
504 case OSC_I_ALLOC:
505 if (reg_field(c, o, "dst", &in->dst) || reg_field(c, o, "init", &in->a)) return -1;
506 if (!is_arr(&T[in->dst]) || T[in->dst].ref != OSC_REF_OWN) return STEP(AT "dst r%d is %s; alloc writes an own:<type>:<len> register", ATV, in->dst, tn(&T[in->dst]));
507 if (!is_scalar(&T[in->a]) || T[in->a].s != T[in->dst].elem) return STEP(AT "init r%d is %s; it must be the element type of r%d", ATV, in->a, tn(&T[in->a]), in->dst);
508 break;
509 case OSC_I_RELEASE:
510 if (reg_field(c, o, "arr", &in->a)) return -1;
511 if (!is_arr(&T[in->a]) || T[in->a].ref != OSC_REF_OWN) return STEP(AT "arr r%d is %s; release needs an own:<type>:<len> register", ATV, in->a, tn(&T[in->a]));
512 break;
513 case OSC_I_LOAD:
514 if (reg_field(c, o, "dst", &in->dst) || reg_field(c, o, "arr", &in->a) || reg_field(c, o, "index", &in->b)) return -1;
515 if (!is_arr(&T[in->a])) return STEP(AT "arr r%d is %s, not an array", ATV, in->a, tn(&T[in->a]));
516 if (!is_int(&T[in->b])) return STEP(AT "index r%d is %s, not an integer", ATV, in->b, tn(&T[in->b]));
517 if (!is_scalar(&T[in->dst]) || T[in->dst].s != T[in->a].elem) return STEP(AT "dst r%d is %s; it must be the element type of r%d", ATV, in->dst, tn(&T[in->dst]), in->a);
518 break;
519 case OSC_I_STORE:
520 if (reg_field(c, o, "arr", &in->a) || reg_field(c, o, "index", &in->b) || reg_field(c, o, "value", &in->c)) return -1;
521 if (!is_arr(&T[in->a]) || T[in->a].ref == OSC_REF_SHARED) return STEP(AT "arr r%d is %s; store needs an own: or mut: array", ATV, in->a, tn(&T[in->a]));
522 if (!is_int(&T[in->b])) return STEP(AT "index r%d is %s, not an integer", ATV, in->b, tn(&T[in->b]));
523 if (!is_scalar(&T[in->c]) || T[in->c].s != T[in->a].elem) return STEP(AT "value r%d is %s; it must be the element type of r%d", ATV, in->c, tn(&T[in->c]), in->a);
524 break;
525 case OSC_I_CALL: {
526 const J *fn = jget(o, "func"), *args = jget(o, "args");
527 if (fn->t != J_STR) return STEP(AT "\"func\" must be the name of an earlier function", ATV);
528 int g = find_func(fn->s, c->fi);
529 if (g < 0) return STEP(AT "no function named \"%s\" before this one (calls go to earlier functions only)", ATV, fn->s);
530 in->callee = (int16_t)g;
531 const OscFunc *G = &g_u.funcs[g];
532 if (args->t != J_ARR) return STEP(AT "\"args\" must be an array of register numbers", ATV);
533 if (args->n != G->nparams) return STEP(AT "%s takes %u arguments, got %zu", ATV, G->name, G->nparams, args->n);
534 in->nargs = (uint8_t)args->n;
535 for (size_t i = 0; i < args->n; i++) {
536 int neg; uint64_t v;
537 if (jint(args->kid[i], &neg, &v) || neg || v >= c->f->nvregs) return STEP(AT "args[%zu] is not a register of this function", ATV, i);
538 in->args[i] = (int16_t)v;
539 const OscType *p = &G->vtype[i], *a = &T[v];
540 int ok = is_scalar(p) ? teq(p, a) : ref_ok(p, a);
541 if (!ok) return STEP(AT "args[%zu] r%" PRIu64 " is %s; %s parameter %zu is %s", ATV, i, v, tn(a), G->name, i, tn(p));
542 }
543 const J *dj = jget(o, "dst");
544 if (G->ret.s == OSC_T_VOID) {
545 if (dj) return STEP(AT "%s returns nothing: leave out \"dst\"", ATV, G->name);
546 } else {
547 if (!dj) return STEP(AT "%s returns %s: \"dst\" is required", ATV, G->name, tn(&G->ret));
548 if (reg_field(c, o, "dst", &in->dst)) return -1;
549 if (!teq(&T[in->dst], &G->ret)) return STEP(AT "dst r%d is %s; %s returns %s", ATV, in->dst, tn(&T[in->dst]), G->name, tn(&G->ret));
550 }
551 break;
552 }
553 case OSC_I_TRAP: {
554 int neg; uint64_t v;
555 if (jint(jget(o, "code"), &neg, &v) || neg || v < 1 || v > OSC_TRAP_MAX) return STEP(AT "trap code must be 1..%d", ATV, OSC_TRAP_MAX);
556 in->imm = v;
557 break;
558 }
559 case OSC_I_BR:
560 if (blk_field(c, o, "to", &in->blk_t)) return -1;
561 break;
562 case OSC_I_CBR:
563 if (reg_field(c, o, "cond", &in->a) || blk_field(c, o, "then", &in->blk_t) || blk_field(c, o, "else", &in->blk_f)) return -1;
564 if (T[in->a].s != OSC_T_BOOL || !is_scalar(&T[in->a])) return STEP(AT "cond r%d is %s, not bool", ATV, in->a, tn(&T[in->a]));
565 break;
566 case OSC_I_RET: {
567 const J *aj = jget(o, "a");
568 if (c->f->ret.s == OSC_T_VOID) {
569 if (aj) return STEP(AT "this function returns nothing: leave out \"a\"", ATV);
570 } else {
571 if (!aj) return STEP(AT "this function returns %s: \"a\" is required", ATV, tn(&c->f->ret));
572 if (reg_field(c, o, "a", &in->a)) return -1;
573 if (!teq(&T[in->a], &c->f->ret)) return STEP(AT "r%d is %s; this function returns %s", ATV, in->a, tn(&T[in->a]), tn(&c->f->ret));
574 }
575 break;
576 }
577 }
578 return 0;
579}
580
581static int name_ok(const char *s)
582{
583 size_t n = strlen(s);
584 if (!n || n > OSC_NAME_MAX - 1 || !((s[0] >= 'a' && s[0] <= 'z') || (s[0] >= 'A' && s[0] <= 'Z') || s[0] == '_')) return 0;
585 for (size_t i = 1; i < n; i++)
586 if (!((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '0' && s[i] <= '9') || s[i] == '_')) return 0;
587 return 1;
588}
589
590static int only_keys(const J *o, const char *where, const char *const *ks)
591{
592 for (size_t i = 0; i < o->n; i++) {
593 int ok = 0;
594 for (int k = 0; ks[k]; k++) if (!strcmp(o->key[i], ks[k])) ok = 1;
595 if (!ok) return fail("schema", "%s: unknown key \"%s\"", where, o->key[i]);
596 }
597 for (int k = 0; ks[k]; k++) if (!jget(o, ks[k])) return fail("schema", "%s: missing key \"%s\"", where, ks[k]);
598 return 0;
599}
600
601static int build(const J *root)
602{
603 memset(&g_u, 0, sizeof g_u);
604 static const char *const top[] = {"functions", NULL};
605 static const char *const fk[] = {"name", "params", "ret", "locals", "blocks", NULL};
606 if (root->t != J_OBJ) return fail("schema", "the top level must be an object {\"functions\": [...]}");
607 if (only_keys(root, "top level", top)) return -1;
608 const J *fs = jget(root, "functions");
609 if (fs->t != J_ARR || fs->n < 1 || fs->n > OSC_MAX_FUNCS) return fail("schema", "\"functions\" must be an array of 1..%d functions", OSC_MAX_FUNCS);
610 for (size_t fi = 0; fi < fs->n; fi++) {
611 const J *F = fs->kid[fi];
612 char where[96];
613 snprintf(where, sizeof where, "functions[%zu]", fi);
614 if (F->t != J_OBJ) return fail("schema", "%s must be an object", where);
615 const J *nm = jget(F, "name");
616 if (nm && nm->t == J_STR && name_ok(nm->s)) snprintf(where, sizeof where, "function %s", nm->s);
617 if (only_keys(F, where, fk)) return -1;
618 if (nm->t != J_STR || !name_ok(nm->s)) return fail("schema", "%s: \"name\" must match [A-Za-z_][A-Za-z0-9_]{0,62}", where);
619 if (find_func(nm->s, (unsigned)fi) >= 0) return fail("schema", "%s: a function with this name already exists", where);
620 OscFunc *f = &g_u.funcs[fi];
621 snprintf(f->name, sizeof f->name, "%s", nm->s);
622 const J *ps = jget(F, "params"), *rt = jget(F, "ret"), *ls = jget(F, "locals"), *bs = jget(F, "blocks");
623 if (ps->t != J_ARR || ps->n > OSC_MAX_PARAMS) return fail("schema", "%s: \"params\" must be an array of 0..%d types", where, OSC_MAX_PARAMS);
624 if (ls->t != J_ARR) return fail("schema", "%s: \"locals\" must be an array of types", where);
625 if (ps->n + ls->n > OSC_MAX_VREGS) return fail("schema", "%s: %zu registers (params + locals) > %d", where, ps->n + ls->n, OSC_MAX_VREGS);
626 f->nparams = (uint8_t)ps->n;
627 f->nvregs = (uint16_t)(ps->n + ls->n);
628 for (size_t r = 0; r < ps->n + ls->n; r++) {
629 const J *t = r < ps->n ? ps->kid[r] : ls->kid[r - ps->n];
630 if (t->t != J_STR || type_from(t->s, &f->vtype[r]))
631 return fail("schema", "%s: register r%zu: type must be bool, u8..u64, i8..i64, or own|shared|mut:<type>:<1..64>", where, r);
632 }
633 if (rt->t != J_STR) return fail("schema", "%s: \"ret\" must be a type name or \"void\"", where);
634 if (!strcmp(rt->s, "void")) f->ret.s = OSC_T_VOID;
635 else if (scalar_from(rt->s) > 0) f->ret.s = (OscScalar)scalar_from(rt->s);
636 else return fail("schema", "%s: \"ret\" must be bool, u8..u64, i8..i64 or void", where);
637 if (bs->t != J_ARR || bs->n < 1 || bs->n > OSC_MAX_BLOCKS) return fail("schema", "%s: \"blocks\" must be an array of 1..%d blocks", where, OSC_MAX_BLOCKS);
638 g_u.nfuncs = (uint16_t)(fi + 1);
639 Ctx c = {f->name, (unsigned)fi, 0, 0, "?", f, (unsigned)bs->n};
640 for (size_t b = 0; b < bs->n; b++) {
641 const J *B = bs->kid[b];
642 if (B->t != J_ARR || B->n < 1) return fail("schema", "%s: block %zu must be a non-empty array of ops", where, b);
643 f->blocks[b].first = f->ninsns;
644 f->blocks[b].count = (uint32_t)B->n;
645 for (size_t k = 0; k < B->n; k++) {
646 if (f->ninsns >= OSC_MAX_INSNS) return fail("schema", "%s: more than %d ops", where, OSC_MAX_INSNS);
647 c.blk = (unsigned)b; c.k = (unsigned)k;
648 if (step(&c, B->kid[k], k + 1 == B->n, &f->insns[f->ninsns])) return -1;
649 f->ninsns++;
650 }
651 }
652 f->nblocks = (uint16_t)bs->n;
653 }
654 return 0;
655}
656
657/* "func F insn I: msg" -> "function <name> block B op K: msg" */
658static void where_insn(const char *err, char *o, size_t n)
659{
660 int fi; unsigned ii; int used = 0;
661 if (sscanf(err, "func %d insn %u: %n", &fi, &ii, &used) == 2 && used && fi >= 0 && fi < g_u.nfuncs) {
662 const OscFunc *f = &g_u.funcs[fi];
663 for (unsigned b = 0; b < f->nblocks; b++)
664 if (ii >= f->blocks[b].first && ii < f->blocks[b].first + f->blocks[b].count) {
665 const OpDef *d = opdef_of(f->insns[ii].op, f->insns[ii].sub);
666 snprintf(o, n, "function %s block %u op %u (%s): %s", f->name, b, ii - f->blocks[b].first, d ? d->name : "?", err + used);
667 return;
668 }
669 }
670 snprintf(o, n, "%s", err);
671}
672
673static uint8_t *slurp(const char *path, size_t max, size_t *len)
674{
675 FILE *f = fopen(path, "rb");
676 if (!f) return NULL;
677 uint8_t *b = xmalloc(max + 1);
678 size_t n = fread(b, 1, max + 1, f);
679 fclose(f);
680 if (n > max) { free(b); errno = EFBIG; return NULL; }
681 b[n] = 0;
682 *len = n;
683 return b;
684}
685
686static int cmd_emit(const char *in, const char *out)
687{
688 size_t n;
689 uint8_t *src = slurp(in, MAX_IN, &n);
690 if (!src) { printf("REFUSED input: cannot read %s or it is over %u bytes\n", in, MAX_IN); return 1; }
691 J *root = jparse((const char *)src, n);
692 if (!root || build(root)) { printf("REFUSED %s\n", g_why); return 1; }
693 char err[256] = "";
694 if (osc_ir_validate(&g_u, err, sizeof err)) {
695 char w[400];
696 where_insn(err, w, sizeof w);
697 printf("REFUSED validator: %s\n", w);
698 return 1;
699 }
700 static uint8_t buf[OSC_IR_ENCODED_MAX], buf2[OSC_IR_ENCODED_MAX];
701 static OscUnit back;
702 size_t len = 0, len2 = 0;
703 if (osc_ir_encode(&g_u, buf, sizeof buf, &len)) { printf("REFUSED encoding: osc_ir_encode refused the unit\n"); return 1; }
704 int rc = osc_ir_decode(buf, len, &back);
705 if (rc) { printf("REFUSED encoding: osc_ir_decode refused the canonical bytes (%s)\n", osc_ir_refusal_name(rc)); return 1; }
706 if (osc_ir_encode(&back, buf2, sizeof buf2, &len2) || len2 != len || memcmp(buf, buf2, len)) { /* EMIT:roundtrip */
707 printf("REFUSED encoding: decode -> encode is not byte-identical\n");
708 return 1;
709 }
710 char tmp[4096];
711 snprintf(tmp, sizeof tmp, "%s.tmp", out);
712 FILE *f = fopen(tmp, "wb");
713 if (!f || fwrite(buf, 1, len, f) != len || fclose(f)) { fprintf(stderr, "omega_emit: cannot write %s\n", tmp); return 2; }
714 if (rename(tmp, out)) { fprintf(stderr, "omega_emit: cannot rename to %s\n", out); return 2; }
715 printf("OK bytes=%zu functions=%u\n", len, g_u.nfuncs);
716 return 0;
717}
718
719/* ================================================================================== lift */
720
721static int cmd_lift(const char *in)
722{
723 size_t n;
724 uint8_t *b = slurp(in, OSC_IR_ENCODED_MAX, &n);
725 if (!b) { fprintf(stderr, "omega_emit: cannot read %s\n", in); return 2; }
726 static OscUnit u;
727 int rc = osc_ir_decode(b, n, &u);
728 if (rc) { printf("REFUSED decoder: %s\n", osc_ir_refusal_name(rc)); return 1; }
729 if (u.nstructs) { printf("REFUSED lift: structs are not in the op-list schema\n"); return 1; }
730 /* check everything first: lift prints nothing for a unit it cannot express */
731 for (unsigned fi = 0; fi < u.nfuncs; fi++) {
732 const OscFunc *f = &u.funcs[fi];
733 if (f->requires_text[0] || f->ensures_text[0]) { printf("REFUSED lift: function %s carries requires/ensures text\n", f->name); return 1; }
734 for (unsigned r = 0; r < f->nvregs; r++) {
735 char t[32];
736 type_name(&f->vtype[r], t, sizeof t);
737 if (!strcmp(t, "?")) { printf("REFUSED lift: function %s register r%u has a type outside the schema\n", f->name, r); return 1; }
738 }
739 for (unsigned i = 0; i < f->ninsns; i++)
740 if (!opdef_of(f->insns[i].op, f->insns[i].sub)) { printf("REFUSED lift: function %s uses op %u, outside the schema\n", f->name, f->insns[i].op); return 1; }
741 }
742 printf("{\"functions\": [\n");
743 for (unsigned fi = 0; fi < u.nfuncs; fi++) {
744 const OscFunc *f = &u.funcs[fi];
745 char t[32];
746 printf(" {\"name\": \"%s\", \"params\": [", f->name);
747 for (unsigned r = 0; r < f->nparams; r++) { type_name(&f->vtype[r], t, sizeof t); printf("%s\"%s\"", r ? ", " : "", t); }
748 printf("], \"ret\": \"%s\", \"locals\": [", f->ret.s == OSC_T_VOID ? "void" : k_scal[f->ret.s]);
749 for (unsigned r = f->nparams; r < f->nvregs; r++) { type_name(&f->vtype[r], t, sizeof t); printf("%s\"%s\"", r > f->nparams ? ", " : "", t); }
750 printf("],\n \"blocks\": [\n");
751 for (unsigned bi = 0; bi < f->nblocks; bi++) {
752 printf(" [\n");
753 for (uint32_t i = f->blocks[bi].first; i < f->blocks[bi].first + f->blocks[bi].count; i++) {
754 const OscInsn *x = &f->insns[i];
755 const OpDef *d = opdef_of(x->op, x->sub);
756 printf(" {\"op\": \"%s\"", d->name);
757 switch (x->op) {
758 case OSC_I_CONST: {
759 OscScalar s = f->vtype[x->dst].s;
760 if (s == OSC_T_BOOL) printf(", \"dst\": %d, \"value\": %s", x->dst, x->imm ? "true" : "false");
761 else if (osc_scalar_signed(s)) printf(", \"dst\": %d, \"value\": %" PRId64, x->dst, (int64_t)x->imm);
762 else printf(", \"dst\": %d, \"value\": %" PRIu64, x->dst, x->imm); /* EMIT:lift-const */
763 break;
764 }
765 case OSC_I_MOV: case OSC_I_UN: case OSC_I_CAST: printf(", \"dst\": %d, \"a\": %d", x->dst, x->a); break;
766 case OSC_I_BIN: case OSC_I_CMP: printf(", \"dst\": %d, \"a\": %d, \"b\": %d", x->dst, x->a, x->b); break;
767 case OSC_I_ALLOC: printf(", \"dst\": %d, \"init\": %d", x->dst, x->a); break;
768 case OSC_I_RELEASE: printf(", \"arr\": %d", x->a); break;
769 case OSC_I_LOAD: printf(", \"dst\": %d, \"arr\": %d, \"index\": %d", x->dst, x->a, x->b); break;
770 case OSC_I_STORE: printf(", \"arr\": %d, \"index\": %d, \"value\": %d", x->a, x->b, x->c); break;
771 case OSC_I_CALL:
772 if (x->dst >= 0) printf(", \"dst\": %d", x->dst);
773 printf(", \"func\": \"%s\", \"args\": [", u.funcs[x->callee].name);
774 for (unsigned k = 0; k < x->nargs; k++) printf("%s%d", k ? ", " : "", x->args[k]);
775 printf("]");
776 break;
777 case OSC_I_TRAP: printf(", \"code\": %" PRIu64, x->imm); break;
778 case OSC_I_BR: printf(", \"to\": %d", x->blk_t); break;
779 case OSC_I_CBR: printf(", \"cond\": %d, \"then\": %d, \"else\": %d", x->a, x->blk_t, x->blk_f); break;
780 case OSC_I_RET: if (x->a >= 0) printf(", \"a\": %d", x->a); break;
781 }
782 printf("}%s\n", i + 1 < f->blocks[bi].first + f->blocks[bi].count ? "," : "");
783 }
784 printf(" ]%s\n", bi + 1 < f->nblocks ? "," : "");
785 }
786 printf(" ]}%s\n", fi + 1 < u.nfuncs ? "," : "");
787 }
788 printf("]}\n");
789 return 0;
790}
791
792int main(int argc, char **argv)
793{
794 if (argc == 4 && !strcmp(argv[1], "emit")) return cmd_emit(argv[2], argv[3]);
795 if (argc == 3 && !strcmp(argv[1], "lift")) return cmd_lift(argv[2]);
796 fprintf(stderr, "usage: omega_emit emit <oplist.json> <out.oscir> | omega_emit lift <in.oscir>\n");
797 return 2;
798}