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 / tools / omega_ref / omega_ref.c
| 1 | /* |
| 2 | * omega_ref.c -- REFERENCE INTERPRETER for the om4 language (Omegese with the systems words of |
| 3 | * docs/one_layer/OMEGESE.md section 9). An ORACLE ONLY: it sits outside the seed chain and never makes a |
| 4 | * byte of compiler machine code itself. Written from OMEGESE.md (sections 3, 4 and 9) and seed/README.md; seed/rung4/om4.s and |
| 5 | * seed/omc/ were not read while writing the original interpreter. The current |
| 6 | * maintenance reads the compiler to identify used language forms; it never |
| 7 | * imports its code generation or expected output (tools/omega_ref/README.md). |
| 8 | * |
| 9 | * It runs a program by its meaning (no machine code): values, arrays, pools, texts, read/write/exit and |
| 10 | * the traps with their numbers, with the same input and output streams as the ELF om4 makes. |
| 11 | * |
| 12 | * omega_ref check <prog> 0 when accepted, else the refusal number (as om4's exit code) |
| 13 | * omega_ref run <prog> program mode: run main; stdin/stdout/exit status as the ELF |
| 14 | * (trap n -> exit 100+n); a refused program exits 125 |
| 15 | * omega_ref call <prog> <entry> <inputs> function mode: one line per input, as seed/check/runner.c |
| 16 | * prints them: value TAB raw-x0, or "TRAP n TAB -" |
| 17 | * |
| 18 | * Refusal numbers: the ones section 9 names, plus om3's (seed/rung3/gate3.sh): 3 unknown name, 4 too few |
| 19 | * values, 5 extra words, 6 bad number, 8 number outside its type, 10 a name already in use, 11 two bare |
| 20 | * numbers in one op, 15 a function without end. Where the spec gives no number the choice is marked |
| 21 | * "spec silent" here and listed in README.md (decision 19 proposals). |
| 22 | */ |
| 23 | #define _GNU_SOURCE |
| 24 | #include <errno.h> |
| 25 | #include <inttypes.h> |
| 26 | #include <limits.h> |
| 27 | #include <pthread.h> |
| 28 | #include <setjmp.h> |
| 29 | #include <stdarg.h> |
| 30 | #include <stdint.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <sys/mman.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | /* ---------- types ---------- */ |
| 38 | enum { T_NONE, T_BOOL, T_U8, T_U16, T_U32, T_U64, T_I8, T_I16, T_I32, T_I64, T_VOID }; |
| 39 | static const char *TNAME[] = {"?", "bool", "uchar", "ushort", "uint", "ulong", "byte", "short", "int", "long", "void"}; |
| 40 | static const char *TIR[] = {0, 0, "u8", "u16", "u32", "u64", "i8", "i16", "i32", "i64", 0}; |
| 41 | static int twidth(int t) { static const int w[] = {0, 1, 8, 16, 32, 64, 8, 16, 32, 64, 0}; return w[t]; } |
| 42 | static int tsigned(int t) { return t >= T_I8 && t <= T_I64; } |
| 43 | static int tnum(int t) { return t >= T_U8 && t <= T_I64; } |
| 44 | static int tbytes(int t) { return t == T_BOOL ? 1 : twidth(t) / 8; } |
| 45 | |
| 46 | /* canonical form: signed sign-extended to 64 bits, unsigned zero-extended, bool 0/1 */ |
| 47 | static uint64_t canon(int t, uint64_t x) |
| 48 | { |
| 49 | int w = twidth(t); |
| 50 | if (t == T_BOOL) return x & 1; |
| 51 | if (w >= 64) return x; |
| 52 | if (tsigned(t)) { uint64_t m = 1ULL << (w - 1); x &= (1ULL << w) - 1; return (x ^ m) - m; } |
| 53 | return x & ((1ULL << w) - 1); |
| 54 | } |
| 55 | static int64_t smin(int t) { return twidth(t) == 64 ? INT64_MIN : -(INT64_C(1) << (twidth(t) - 1)); } |
| 56 | static int64_t smax(int t) { return twidth(t) == 64 ? INT64_MAX : (INT64_C(1) << (twidth(t) - 1)) - 1; } |
| 57 | static uint64_t umax(int t) { return t == T_BOOL ? 1 : twidth(t) == 64 ? UINT64_MAX : (UINT64_C(1) << twidth(t)) - 1; } |
| 58 | |
| 59 | /* ---------- refusals and traps ---------- */ |
| 60 | static int cur_line; |
| 61 | static int refuse_exit; /* nonzero: every refusal exits with this instead of its number */ |
| 62 | static void refuse(int code, const char *fmt, ...) |
| 63 | { |
| 64 | va_list ap; |
| 65 | va_start(ap, fmt); |
| 66 | fprintf(stderr, "omega_ref: line %d: error %d: ", cur_line, code); |
| 67 | vfprintf(stderr, fmt, ap); |
| 68 | fputc('\n', stderr); |
| 69 | va_end(ap); |
| 70 | fflush(stderr); |
| 71 | _exit(refuse_exit ? refuse_exit : code); |
| 72 | } |
| 73 | |
| 74 | static jmp_buf trap_jb; |
| 75 | static int trap_code; |
| 76 | static void trap(int code) { trap_code = code; longjmp(trap_jb, 1); } |
| 77 | |
| 78 | /* ---------- capacities (section 9.6) ---------- */ |
| 79 | /* Tables grow with source size. Allocation/size failures are oracle failures, |
| 80 | * never fabricated language refusals. The remaining limits below are language |
| 81 | * rules for fixed arrays, call slots and loop/stack execution. */ |
| 82 | static void *resize(void *old, size_t count, size_t width) |
| 83 | { |
| 84 | if (width && count > SIZE_MAX / width) { fputs("omega_ref: allocation overflow\n", stderr); exit(2); } |
| 85 | void *p = realloc(old, count * width); |
| 86 | if (!p && count) { perror("omega_ref: allocation"); exit(2); } |
| 87 | return p; |
| 88 | } |
| 89 | static void *grow(void *old, int *capacity, int used, size_t width) |
| 90 | { |
| 91 | if (used < *capacity) return old; |
| 92 | if (used == INT_MAX) { fputs("omega_ref: index representation exhausted\n", stderr); exit(2); } |
| 93 | int n = *capacity > INT_MAX / 2 ? INT_MAX : *capacity ? *capacity * 2 : 16; |
| 94 | void *p = resize(old, (size_t)n, width); |
| 95 | memset((char *)p + (size_t)*capacity * width, 0, (size_t)(n - *capacity) * width); |
| 96 | *capacity = n; |
| 97 | return p; |
| 98 | } |
| 99 | #define MAX_POOL_ELEMS 16777216u |
| 100 | #define MAX_POOL_BYTES (1ull << 30) |
| 101 | #define MAX_LOCAL_ARR_BYTES (4ull << 20) |
| 102 | #define STACK_BYTES (8ull << 20) |
| 103 | #define MAX_PARAMS 64 |
| 104 | #define LOOP_MAX 1000000ull |
| 105 | |
| 106 | /* ---------- arrays ---------- */ |
| 107 | typedef struct { |
| 108 | uint8_t *data; |
| 109 | int ety; |
| 110 | uint64_t len; |
| 111 | int ro; /* text or view */ |
| 112 | } Arr; |
| 113 | |
| 114 | static uint64_t arr_get(const Arr *a, uint64_t i) |
| 115 | { |
| 116 | uint8_t *p = a->data + i * tbytes(a->ety); |
| 117 | uint64_t v = 0; |
| 118 | switch (tbytes(a->ety)) { |
| 119 | case 1: v = *p; break; |
| 120 | case 2: { uint16_t x; memcpy(&x, p, 2); v = x; } break; |
| 121 | case 4: { uint32_t x; memcpy(&x, p, 4); v = x; } break; |
| 122 | default: memcpy(&v, p, 8); |
| 123 | } |
| 124 | return canon(a->ety, v); |
| 125 | } |
| 126 | static void arr_put(Arr *a, uint64_t i, uint64_t v) |
| 127 | { |
| 128 | uint8_t *p = a->data + i * tbytes(a->ety); |
| 129 | switch (tbytes(a->ety)) { |
| 130 | case 1: *p = (uint8_t)v; break; |
| 131 | case 2: { uint16_t x = (uint16_t)v; memcpy(p, &x, 2); } break; |
| 132 | case 4: { uint32_t x = (uint32_t)v; memcpy(p, &x, 4); } break; |
| 133 | default: memcpy(p, &v, 8); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /* ---------- program representation ---------- */ |
| 138 | enum { AK_GLOBAL, AK_LOCAL }; /* where an array reference lives */ |
| 139 | typedef struct { int kind, idx; } ARef; |
| 140 | |
| 141 | enum { |
| 142 | N_NUM, N_VAR, N_BIN, N_CMP, N_NEG, N_NOT, N_CONV, N_GET, N_LEN, N_READ, N_WRITE, N_CALL, N_BARE, N_ENLARGE, N_ENVCNT, N_ENVREAD |
| 143 | }; |
| 144 | enum { OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_REM, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR, |
| 145 | OP_EQ, OP_NE, OP_LT, OP_LE, OP_GT, OP_GE }; |
| 146 | static const char *OPW[] = {"add", "sub", "mul", "div", "rem", "and", "or", "xor", "shl", "shr", |
| 147 | "eq", "ne", "lt", "le", "gt", "ge"}; |
| 148 | |
| 149 | typedef struct Node { |
| 150 | int kind, op, ty; /* ty T_NONE while a bare number has no type yet */ |
| 151 | int bare; /* a bare number, or an op over bare numbers, still waiting for its type */ |
| 152 | uint64_t num; /* N_NUM: canonical value; N_BARE: magnitude */ |
| 153 | int neg; /* N_BARE: minus sign */ |
| 154 | int slot; /* N_VAR */ |
| 155 | ARef arr; /* N_GET N_LEN N_READ N_WRITE */ |
| 156 | int fn; /* N_CALL */ |
| 157 | struct Node *a, *b, *c, *d; |
| 158 | struct Node **args; /* N_CALL: scalar args (NULL for array args) */ |
| 159 | ARef *aargs; /* N_CALL: array args */ |
| 160 | int line; |
| 161 | } Node; |
| 162 | |
| 163 | enum { S_ASSIGN, S_NEW, S_IF, S_WHILE, S_FOR, S_BREAK, S_RET, S_PUT, S_FREE, S_TRAP, S_EXIT, S_CALL }; |
| 164 | typedef struct Stmt { |
| 165 | int kind, line; |
| 166 | int slot; /* S_ASSIGN S_NEW S_FOR */ |
| 167 | Node *v, *v2; /* value / cond / count / index, value */ |
| 168 | ARef arr; /* S_PUT */ |
| 169 | int ety; /* S_NEW */ |
| 170 | uint64_t n; /* S_NEW length, S_WHILE bound, S_FOR literal count, S_TRAP code */ |
| 171 | int count_kind;/* S_FOR: 0 literal, 1 a name (read every pass), 2 a value (computed once) */ |
| 172 | int ty; /* S_FOR loop type */ |
| 173 | struct Stmt *body, *els, *next; |
| 174 | } Stmt; |
| 175 | |
| 176 | enum { PK_SCALAR, PK_VIEW, PK_EDIT }; |
| 177 | typedef struct { |
| 178 | char name[64]; |
| 179 | int np; |
| 180 | int pkind[MAX_PARAMS], pty[MAX_PARAMS]; |
| 181 | uint64_t plen[MAX_PARAMS]; |
| 182 | int ret; |
| 183 | Stmt *body; |
| 184 | int nslots; |
| 185 | int *callees, ncallees, capcallees; |
| 186 | int has_call; /* a non-leaf function: the stack model charges it a frame */ |
| 187 | int done; /* its end has been read */ |
| 188 | int sig_line; /* the line of its fn line (the names pass, 9.2) */ |
| 189 | } Func; |
| 190 | |
| 191 | typedef struct { char name[64]; int ety; uint64_t len; int is_text; Arr arr; } Global; |
| 192 | |
| 193 | static Func *funcs; |
| 194 | static int capfuncs; |
| 195 | static int nfuncs; |
| 196 | /* Function call edges are stored sparsely in each function. */ |
| 197 | static Global *globals; |
| 198 | static int nglobals, capglobals; |
| 199 | static uint64_t pool_bytes; |
| 200 | static int program_mode; |
| 201 | static int first_global_line; /* a pool or text in function mode is refused at the end (as om4 does) */ |
| 202 | |
| 203 | /* ---------- lexing: lines and words ---------- */ |
| 204 | static char *src; |
| 205 | static size_t srclen; |
| 206 | static char **W; |
| 207 | static char *wbuf; |
| 208 | static int nw, wi; |
| 209 | |
| 210 | static int is_reserved(const char *w); |
| 211 | static int split_words(const char *line, size_t n) |
| 212 | { |
| 213 | if (n > INT_MAX) { fputs("omega_ref: line representation exhausted\n", stderr); exit(2); } |
| 214 | /* words are separated by spaces; ( and ) are words of their own even against a name */ |
| 215 | char *buf = wbuf; |
| 216 | size_t o = 0; |
| 217 | nw = 0; |
| 218 | size_t i = 0; |
| 219 | while (i < n) { |
| 220 | char ch = line[i]; |
| 221 | if (ch == ' ' || ch == '\t' || ch == '\r') { i++; continue; } |
| 222 | W[nw++] = buf + o; |
| 223 | if (ch == '(' || ch == ')') { buf[o++] = ch; buf[o++] = 0; i++; continue; } |
| 224 | while (i < n && line[i] != ' ' && line[i] != '\t' && line[i] != '\r' && line[i] != '(' && line[i] != ')') |
| 225 | buf[o++] = line[i++]; |
| 226 | buf[o++] = 0; |
| 227 | } |
| 228 | wi = 0; |
| 229 | return nw; |
| 230 | } |
| 231 | static const char *peekw(void) { return wi < nw ? W[wi] : NULL; } |
| 232 | static const char *nextw(void) { return wi < nw ? W[wi++] : NULL; } |
| 233 | |
| 234 | static int type_word(const char *w) |
| 235 | { |
| 236 | for (int t = T_BOOL; t <= T_I64; t++) { |
| 237 | if (!strcmp(w, TNAME[t])) return t; |
| 238 | if (TIR[t] && !strcmp(w, TIR[t])) return t; |
| 239 | } |
| 240 | return T_NONE; |
| 241 | } |
| 242 | static int op_word(const char *w) |
| 243 | { |
| 244 | for (int i = 0; i < 16; i++) if (!strcmp(w, OPW[i])) return i; |
| 245 | return -1; |
| 246 | } |
| 247 | static const char *KEYWORDS[] = {"fn", "to", "void", "end", "if", "else", "while", "bound", "for", "break", |
| 248 | "ret", "put", "free", "trap", "global", "text", "new", "view", "edit", "own", |
| 249 | "get", "len", "read", "write", "exit", "note", "true", "false", "neg", "not", |
| 250 | "enlarge", "envcnt", "envread", "=", "(", ")", NULL}; |
| 251 | static int is_reserved(const char *w) |
| 252 | { |
| 253 | for (int i = 0; KEYWORDS[i]; i++) if (!strcmp(w, KEYWORDS[i])) return 1; |
| 254 | return op_word(w) >= 0 || type_word(w) != T_NONE; |
| 255 | } |
| 256 | static int is_name(const char *w) |
| 257 | { |
| 258 | if (!((w[0] >= 'a' && w[0] <= 'z') || (w[0] >= 'A' && w[0] <= 'Z') || w[0] == '_')) return 0; |
| 259 | for (const char *p = w; *p; p++) |
| 260 | if (!((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || *p == '_')) return 0; |
| 261 | return 1; |
| 262 | } |
| 263 | static int looks_number(const char *w) { return (w[0] >= '0' && w[0] <= '9') || w[0] == '-'; } |
| 264 | /* decimal digits, optional minus, no leading zeros, no -0, fits 64 bits (as u64 or as i64) */ |
| 265 | static void parse_number(const char *w, int *neg, uint64_t *mag) |
| 266 | { |
| 267 | const char *p = w; |
| 268 | *neg = 0; |
| 269 | if (*p == '-') { *neg = 1; p++; } |
| 270 | if (!*p) refuse(6, "bad number '%s'", w); |
| 271 | if (p[0] == '0' && p[1]) refuse(6, "bad number '%s' (leading zero)", w); |
| 272 | if (strlen(p) > 20) refuse(6, "number '%s' has more than 20 digits", w); |
| 273 | uint64_t v = 0; |
| 274 | for (; *p; p++) { |
| 275 | if (*p < '0' || *p > '9') refuse(6, "bad number '%s'", w); |
| 276 | unsigned d = (unsigned)(*p - '0'); |
| 277 | if (v > (UINT64_MAX - d) / 10) refuse(8, "number '%s' outside every type", w); |
| 278 | v = v * 10 + d; |
| 279 | } |
| 280 | if (*neg && v == 0) refuse(6, "bad number '-0'"); |
| 281 | if (*neg && v > (uint64_t)INT64_MAX + 1) refuse(8, "number '%s' outside every type", w); |
| 282 | *mag = v; |
| 283 | } |
| 284 | static int fits(int t, int neg, uint64_t mag) |
| 285 | { |
| 286 | if (t == T_BOOL) return !neg && mag <= 1; |
| 287 | if (tsigned(t)) return neg ? mag <= (uint64_t)smax(t) + 1 : mag <= (uint64_t)smax(t); |
| 288 | return !neg && mag <= umax(t); |
| 289 | } |
| 290 | static uint64_t num_value(int neg, uint64_t mag) { return neg ? (uint64_t)0 - mag : mag; } |
| 291 | |
| 292 | /* ---------- scopes ---------- */ |
| 293 | enum { K_SCALAR, K_OWN, K_VIEW, K_EDIT }; |
| 294 | typedef struct { char name[64]; int kind, ty, slot, depth, is_loop; uint64_t len; } Name; |
| 295 | static Name *names; |
| 296 | static int capnames; |
| 297 | static int nnames; |
| 298 | static int depth; /* open blocks inside the current function (0 = its outermost block) */ |
| 299 | static Func *curf; |
| 300 | static int nslots; |
| 301 | |
| 302 | static int find_func(const char *w) |
| 303 | { |
| 304 | for (int i = 0; i < nfuncs; i++) if (!strcmp(funcs[i].name, w)) return i; |
| 305 | return -1; |
| 306 | } |
| 307 | static int find_global(const char *w) |
| 308 | { |
| 309 | for (int i = 0; i < nglobals; i++) if (!strcmp(globals[i].name, w)) return i; |
| 310 | return -1; |
| 311 | } |
| 312 | static Name *find_name(const char *w) |
| 313 | { |
| 314 | for (int i = nnames - 1; i >= 0; i--) if (!strcmp(names[i].name, w)) return &names[i]; |
| 315 | return NULL; |
| 316 | } |
| 317 | static void check_new_name(const char *w, int is_fn) |
| 318 | { |
| 319 | if (!is_name(w) || is_reserved(w)) refuse(16, "'%s' cannot be a name", w); /* a word out of place: gap G25 */ |
| 320 | (void)is_fn; |
| 321 | size_t n = strlen(w); |
| 322 | if (n > 63) refuse(13, "name '%s' too long", w); |
| 323 | if (find_func(w) >= 0 || find_global(w) >= 0 || find_name(w)) refuse(10, "'%s' is already in use", w); |
| 324 | } |
| 325 | static Name *add_name(const char *w, int kind, int ty, uint64_t len, int is_loop) |
| 326 | { |
| 327 | check_new_name(w, 0); |
| 328 | names = grow(names, &capnames, nnames, sizeof *names); |
| 329 | Name *nm = &names[nnames++]; |
| 330 | snprintf(nm->name, sizeof nm->name, "%s", w); |
| 331 | nm->kind = kind; nm->ty = ty; nm->len = len; nm->depth = depth; nm->is_loop = is_loop; |
| 332 | if (nslots == INT_MAX) { fputs("omega_ref: slot representation exhausted\n", stderr); exit(2); } |
| 333 | nm->slot = nslots++; |
| 334 | return nm; |
| 335 | } |
| 336 | static void close_scope(int d) |
| 337 | { |
| 338 | while (nnames > 0 && names[nnames - 1].depth >= d) nnames--; |
| 339 | } |
| 340 | |
| 341 | /* ---------- values ---------- */ |
| 342 | static Node *new_node(int kind) |
| 343 | { |
| 344 | Node *n = calloc(1, sizeof *n); |
| 345 | if (!n) { perror("omega_ref"); exit(2); } |
| 346 | n->kind = kind; n->line = cur_line; |
| 347 | return n; |
| 348 | } |
| 349 | |
| 350 | /* give a bare value its type (a number, or an op over bare numbers); the number must fit */ |
| 351 | static void fix_bare(Node *n, int t) |
| 352 | { |
| 353 | if (!n->bare) return; |
| 354 | n->bare = 0; |
| 355 | n->ty = t; |
| 356 | switch (n->kind) { |
| 357 | case N_BARE: |
| 358 | /* a bare number takes the type its place needs (s.7); the bool numbers are 0 and 1 (s.6), any other is 8 as |
| 359 | * for a number outside its type (om4: `while 1` runs, `while 255` is 8; gap G28) */ |
| 360 | if (t == T_BOOL) { if (n->neg || n->num > 1) refuse(8, "number %s%" PRIu64 " outside bool (0 or 1)", n->neg ? "-" : "", n->num); } |
| 361 | else if (!tnum(t)) refuse(7, "a number where a %s is needed", TNAME[t]); |
| 362 | else if (!fits(t, n->neg, n->num)) refuse(8, "number %s%" PRIu64 " outside %s", n->neg ? "-" : "", n->num, TNAME[t]); |
| 363 | n->num = canon(t, num_value(n->neg, n->num)); |
| 364 | n->kind = N_NUM; |
| 365 | break; |
| 366 | case N_NEG: case N_NOT: |
| 367 | fix_bare(n->a, t); |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | static int nest; |
| 373 | static int enlargement_assignment; |
| 374 | static Node *parse_value(int want); |
| 375 | |
| 376 | static ARef parse_array_ref(const char *w, int *ety, uint64_t *len, int *ro, int *kindout) |
| 377 | { |
| 378 | ARef r; |
| 379 | if (!w) refuse(4, "an array name is missing"); |
| 380 | if (!is_name(w)) refuse(16, "'%s' where an array name belongs", w); /* gap G25 */ |
| 381 | if (is_reserved(w)) refuse(3, "'%s' is no array name", w); /* gap G25 */ |
| 382 | Name *nm = find_name(w); |
| 383 | if (nm && nm->kind != K_SCALAR) { |
| 384 | r.kind = AK_LOCAL; r.idx = nm->slot; |
| 385 | *ety = nm->ty; *len = nm->len; *ro = nm->kind == K_VIEW; *kindout = nm->kind; |
| 386 | return r; |
| 387 | } |
| 388 | int g = find_global(w); |
| 389 | if (g >= 0) { |
| 390 | r.kind = AK_GLOBAL; r.idx = g; |
| 391 | *ety = globals[g].ety; *len = globals[g].len; *ro = globals[g].is_text; *kindout = -1 - globals[g].is_text; |
| 392 | return r; |
| 393 | } |
| 394 | if (nm || find_func(w) >= 0) refuse(7, "'%s' is not an array", w); |
| 395 | refuse(3, "unknown name '%s'", w); |
| 396 | return r; |
| 397 | } |
| 398 | |
| 399 | /* the binary op both values agree on; a bare one takes the other's type */ |
| 400 | static int unify(Node *a, Node *b, const char *opw) |
| 401 | { |
| 402 | if (a->bare && b->bare) refuse(11, "%s has two bare numbers", opw); |
| 403 | if (a->bare) fix_bare(a, b->ty); |
| 404 | else if (b->bare) fix_bare(b, a->ty); |
| 405 | if (a->ty != b->ty) refuse(7, "%s of %s and %s (no implicit conversions)", opw, TNAME[a->ty], TNAME[b->ty]); |
| 406 | return a->ty; |
| 407 | } |
| 408 | |
| 409 | static Node *parse_call(int fi, int want_void) |
| 410 | { |
| 411 | Func *f = &funcs[fi]; |
| 412 | if (!want_void && f->ret == T_VOID) refuse(7, "void function '%s' used as a value", f->name); |
| 413 | if (want_void && f->ret != T_VOID) refuse(7, "value function '%s' called as a line", f->name); |
| 414 | curf->has_call = 1; |
| 415 | /* function mode (9.2): a function calling itself is refused here, at the call; a cycle through other |
| 416 | * functions at the end of the unit, when every body has been read (om4 does both so: gap G26) */ |
| 417 | if (!program_mode && curf == f) refuse(9, "'%s' calls itself in function mode", f->name); |
| 418 | curf->callees = grow(curf->callees, &curf->capcallees, curf->ncallees, sizeof *curf->callees); |
| 419 | curf->callees[curf->ncallees++] = fi; |
| 420 | Node *n = new_node(N_CALL); |
| 421 | n->fn = fi; n->ty = f->ret; |
| 422 | n->args = calloc(f->np + 1, sizeof *n->args); |
| 423 | n->aargs = calloc(f->np + 1, sizeof *n->aargs); |
| 424 | for (int i = 0; i < f->np; i++) { |
| 425 | if (f->pkind[i] == PK_SCALAR) { |
| 426 | if (!peekw()) refuse(4, "%s has %d values after it; it takes %d", f->name, i, f->np); |
| 427 | n->args[i] = parse_value(f->pty[i]); |
| 428 | fix_bare(n->args[i], f->pty[i]); |
| 429 | if (n->args[i]->ty != f->pty[i]) refuse(7, "argument %d of %s is %s, wants %s", i + 1, f->name, TNAME[n->args[i]->ty], TNAME[f->pty[i]]); |
| 430 | continue; |
| 431 | } |
| 432 | const char *w = nextw(); |
| 433 | if (!w) refuse(4, "%s has %d values after it; it takes %d", f->name, i, f->np); |
| 434 | int as = -1; /* view/edit written before the argument */ |
| 435 | if (!strcmp(w, "view")) as = PK_VIEW; |
| 436 | else if (!strcmp(w, "edit")) as = PK_EDIT; |
| 437 | else if (!strcmp(w, "own")) refuse(9, "own is not in the subset"); |
| 438 | if (as >= 0) { w = nextw(); if (!w) refuse(4, "an array name is missing"); } |
| 439 | int ety, ro, kind; uint64_t len; |
| 440 | { int g = find_global(w); if (g >= 0 && !globals[g].is_text) refuse(19, "a global pool '%s' is never passed", w); } /* a text may go to a view (gap G6) */ |
| 441 | ARef r = parse_array_ref(w, &ety, &len, &ro, &kind); |
| 442 | if (ety != f->pty[i] || len != f->plen[i]) refuse(7, "argument %d of %s: %s %" PRIu64 ", wants %s %" PRIu64, i + 1, f->name, TNAME[ety], len, TNAME[f->pty[i]], f->plen[i]); |
| 443 | if (as == PK_EDIT && ro) refuse(20, "'%s' is read-only, passed as edit", w); |
| 444 | if (f->pkind[i] == PK_EDIT && (ro || as == PK_VIEW)) refuse(20, "'%s' passed to an edit parameter is read-only", w); |
| 445 | for (int j = 0; j < i; j++) |
| 446 | if (f->pkind[j] != PK_SCALAR && n->aargs[j].kind == r.kind && n->aargs[j].idx == r.idx) |
| 447 | refuse(19, "'%s' passed twice in one call", w); |
| 448 | n->aargs[i] = r; |
| 449 | } |
| 450 | return n; |
| 451 | } |
| 452 | |
| 453 | /* want: the type the place needs (T_NONE when the place has none); the caller checks the type */ |
| 454 | static Node *parse_value(int want) |
| 455 | { |
| 456 | const char *w = nextw(); |
| 457 | if (!w) refuse(4, "a value is missing"); |
| 458 | ++nest; |
| 459 | Node *n = NULL; |
| 460 | int op, t; |
| 461 | if (!strcmp(w, "(")) { |
| 462 | n = parse_value(want); |
| 463 | const char *c = nextw(); |
| 464 | if (!c || strcmp(c, ")")) refuse(c ? 5 : 4, "a ( without its )"); /* spec silent on the number */ |
| 465 | } else if (!strcmp(w, ")")) { |
| 466 | refuse(5, "a ) without its ("); |
| 467 | } else if (looks_number(w)) { |
| 468 | n = new_node(N_BARE); |
| 469 | parse_number(w, &n->neg, &n->num); |
| 470 | n->bare = 1; n->ty = T_NONE; |
| 471 | } else if (!strcmp(w, "true") || !strcmp(w, "false")) { |
| 472 | n = new_node(N_NUM); n->ty = T_BOOL; n->num = w[0] == 't'; |
| 473 | } else if ((op = op_word(w)) >= 0) { |
| 474 | Node *a = parse_value(T_NONE), *b; |
| 475 | if (op == OP_SHL || op == OP_SHR) { |
| 476 | b = parse_value(T_NONE); |
| 477 | if (a->bare && b->bare) refuse(11, "%s has two bare numbers", w); |
| 478 | if (a->bare) fix_bare(a, b->ty); |
| 479 | if (b->bare) fix_bare(b, a->ty); |
| 480 | if (!tnum(a->ty) || !tnum(b->ty)) refuse(7, "%s of %s by %s", w, TNAME[a->ty], TNAME[b->ty]); |
| 481 | n = new_node(N_BIN); n->ty = a->ty; |
| 482 | } else { |
| 483 | b = parse_value(T_NONE); |
| 484 | t = unify(a, b, w); |
| 485 | if (op >= OP_EQ) { |
| 486 | n = new_node(N_CMP); n->ty = T_BOOL; |
| 487 | if (t == T_BOOL && op != OP_EQ && op != OP_NE) refuse(7, "%s of bool", w); |
| 488 | } else { |
| 489 | n = new_node(N_BIN); n->ty = t; |
| 490 | if (t == T_BOOL && op != OP_AND && op != OP_OR && op != OP_XOR) refuse(7, "%s of bool", w); |
| 491 | } |
| 492 | } |
| 493 | n->op = op; n->a = a; n->b = b; |
| 494 | } else if (!strcmp(w, "neg") || !strcmp(w, "not")) { |
| 495 | Node *a = parse_value(want); |
| 496 | n = new_node(w[1] == 'e' ? N_NEG : N_NOT); |
| 497 | n->a = a; n->ty = a->ty; n->bare = a->bare; |
| 498 | if (!a->bare && (n->kind == N_NEG ? !tnum(a->ty) : !(tnum(a->ty) || a->ty == T_BOOL))) |
| 499 | refuse(7, "%s of %s", w, TNAME[a->ty]); |
| 500 | } else if ((t = type_word(w)) != T_NONE) { |
| 501 | Node *a = parse_value(t); |
| 502 | if (a->bare) { fix_bare(a, t); n = a; } /* a typed number */ |
| 503 | else { |
| 504 | if (t == T_BOOL || a->ty == T_BOOL) refuse(9, "no conversion from or to bool (gap G2)"); |
| 505 | if (!tnum(a->ty)) refuse(7, "%s of %s", w, TNAME[a->ty]); |
| 506 | n = new_node(N_CONV); n->a = a; n->ty = t; /* a conversion: trap 5 when outside */ |
| 507 | } |
| 508 | } else if (!strcmp(w, "get")) { |
| 509 | n = new_node(N_GET); |
| 510 | int ro, kind; uint64_t len; |
| 511 | n->arr = parse_array_ref(nextw(), &n->ty, &len, &ro, &kind); |
| 512 | n->a = parse_value(T_NONE); |
| 513 | if (n->a->bare) { |
| 514 | if (n->a->neg) refuse(8, "a negative index number (gap G7)"); |
| 515 | if (len != UINT64_MAX && n->a->num >= len) refuse(21, "index %s%" PRIu64 " outside an array of %" PRIu64, n->a->neg ? "-" : "", n->a->num, len); |
| 516 | fix_bare(n->a, T_U64); |
| 517 | } |
| 518 | if (!tnum(n->a->ty)) refuse(7, "an index of type %s", TNAME[n->a->ty]); |
| 519 | } else if (!strcmp(w, "len")) { |
| 520 | n = new_node(N_LEN); |
| 521 | int ety, ro, kind; uint64_t len; |
| 522 | n->arr = parse_array_ref(nextw(), &ety, &len, &ro, &kind); |
| 523 | n->ty = T_U64; n->num = len; |
| 524 | } else if (!strcmp(w, "read") || !strcmp(w, "write")) { |
| 525 | n = new_node(w[0] == 'r' ? N_READ : N_WRITE); |
| 526 | n->a = parse_value(T_I32); |
| 527 | fix_bare(n->a, T_I32); |
| 528 | if (n->a->ty != T_I32) refuse(7, "%s fd is %s, wants int", w, TNAME[n->a->ty]); |
| 529 | int ety, ro, kind; uint64_t len; |
| 530 | n->arr = parse_array_ref(nextw(), &ety, &len, &ro, &kind); |
| 531 | if (n->kind == N_READ && ro) refuse(20, "read into a read-only array"); |
| 532 | if (ety != T_U8) refuse(7, "%s needs an array of uchar", w); /* spec: "must be an array of uchar for the counts to be bytes" */ |
| 533 | n->b = parse_value(T_U64); fix_bare(n->b, T_U64); |
| 534 | n->c = parse_value(T_U64); fix_bare(n->c, T_U64); |
| 535 | if (n->b->ty != T_U64 || n->c->ty != T_U64) refuse(7, "%s off and n are ulong", w); |
| 536 | n->ty = T_I64; |
| 537 | } else if (!strcmp(w, "enlarge")) { |
| 538 | if (!enlargement_assignment || nest != 1) refuse(9, "enlarge only as a whole assignment"); |
| 539 | n = new_node(N_ENLARGE); n->ty = T_I64; |
| 540 | const char *pw = nextw(); |
| 541 | int gi = pw ? find_global(pw) : -1; |
| 542 | if (gi < 0 || globals[gi].len != UINT64_MAX) refuse(7, "enlarge needs a run-time pool"); |
| 543 | n->arr.kind = AK_GLOBAL; n->arr.idx = gi; |
| 544 | n->a = parse_value(T_U64); fix_bare(n->a, T_U64); |
| 545 | if (n->a->ty != T_U64) refuse(7, "enlarge length is ulong"); |
| 546 | } else if (!strcmp(w, "envcnt")) { |
| 547 | n = new_node(N_ENVCNT); n->ty = T_I64; |
| 548 | } else if (!strcmp(w, "envread")) { |
| 549 | n = new_node(N_ENVREAD); n->ty = T_I64; |
| 550 | n->a = parse_value(T_U64); fix_bare(n->a, T_U64); |
| 551 | int ety, ro, kind; uint64_t len; |
| 552 | n->arr = parse_array_ref(nextw(), &ety, &len, &ro, &kind); |
| 553 | if (ro) refuse(20, "envread into a read-only array"); |
| 554 | if (ety != T_U8 || len == UINT64_MAX) refuse(7, "envread needs a fixed uchar array"); |
| 555 | n->b = parse_value(T_U64); fix_bare(n->b, T_U64); |
| 556 | n->c = parse_value(T_U64); fix_bare(n->c, T_U64); |
| 557 | if (n->a->ty != T_U64 || n->b->ty != T_U64 || n->c->ty != T_U64) refuse(7, "envread index, offset, capacity are ulong"); |
| 558 | } else if (!strcmp(w, "new")) { |
| 559 | refuse(9, "new only as the whole value of a line in the outermost block"); |
| 560 | } else if (!strcmp(w, "own")) { |
| 561 | refuse(9, "own is not in the subset"); |
| 562 | } else if (!strcmp(w, "view") || !strcmp(w, "edit")) { |
| 563 | refuse(16, "%s only before a call argument", w); /* gap G25 */ |
| 564 | } else { |
| 565 | int fi = find_func(w); |
| 566 | Name *nm = find_name(w); |
| 567 | if (nm) { |
| 568 | if (nm->kind != K_SCALAR) refuse(7, "array '%s' used as a value", w); |
| 569 | n = new_node(N_VAR); n->slot = nm->slot; n->ty = nm->ty; |
| 570 | } else if (fi >= 0) { |
| 571 | n = parse_call(fi, 0); |
| 572 | } else if (find_global(w) >= 0) { |
| 573 | refuse(7, "array '%s' used as a value", w); |
| 574 | } else if (is_name(w) && !is_reserved(w)) { |
| 575 | refuse(3, "unknown name '%s'", w); |
| 576 | } else { |
| 577 | refuse(looks_number(w) ? 6 : 16, "'%s' is not a value", w); /* gap G25 */ |
| 578 | } |
| 579 | } |
| 580 | nest--; |
| 581 | return n; |
| 582 | } |
| 583 | |
| 584 | /* a whole value for a place of type want (T_NONE: the value decides) */ |
| 585 | /* the type a place needs is checked at the end of the line, after its extra words (as om4 does, gap G25) */ |
| 586 | static struct { Node *n; int want; const char *place; } pend[4]; |
| 587 | static int npend; |
| 588 | static void place_check(Node *n, int want, const char *place) |
| 589 | { |
| 590 | if (n->bare) { |
| 591 | if (want == T_NONE) refuse(11, "a bare number has no type here; write it typed (%s)", place); /* gap G3 */ |
| 592 | fix_bare(n, want); |
| 593 | } |
| 594 | if (want != T_NONE && n->ty != want) refuse(7, "%s is %s, wants %s", place, TNAME[n->ty], TNAME[want]); |
| 595 | } |
| 596 | static Node *value_for(int want, const char *place) |
| 597 | { |
| 598 | nest = 0; |
| 599 | Node *n = parse_value(want); |
| 600 | if (n->bare && want == T_NONE) refuse(11, "a bare number has no type here; write it typed (%s)", place); /* at once, as om4: G3, G25 */ |
| 601 | pend[npend].n = n; pend[npend].want = want; pend[npend].place = place; npend++; |
| 602 | return n; |
| 603 | } |
| 604 | static void line_end(void) |
| 605 | { |
| 606 | if (wi < nw) refuse(5, "extra words at the end of the line: '%s'", W[wi]); |
| 607 | for (int i = 0; i < npend; i++) place_check(pend[i].n, pend[i].want, pend[i].place); |
| 608 | npend = 0; |
| 609 | } |
| 610 | |
| 611 | /* ---------- statements ---------- */ |
| 612 | static Stmt *new_stmt(int kind) |
| 613 | { |
| 614 | Stmt *s = calloc(1, sizeof *s); |
| 615 | if (!s) { perror("omega_ref"); exit(2); } |
| 616 | s->kind = kind; s->line = cur_line; |
| 617 | return s; |
| 618 | } |
| 619 | |
| 620 | typedef struct { |
| 621 | int kind; /* 0 function body, 1 if, 2 else, 3 while, 4 for */ |
| 622 | Stmt *owner; |
| 623 | Stmt **tail; /* where the next statement goes */ |
| 624 | int ended; /* the last line of this block ends the path */ |
| 625 | int then_ended; /* if/else: the then-branch ended its path */ |
| 626 | int name_mark; |
| 627 | } Block; |
| 628 | static Block *blocks; |
| 629 | static int capblocks; |
| 630 | static int nblocks; |
| 631 | static int local_arr_bytes_ok(uint64_t add); |
| 632 | static uint64_t local_arr_bytes; |
| 633 | |
| 634 | static void push_stmt(Stmt *s) |
| 635 | { |
| 636 | Block *b = &blocks[nblocks - 1]; |
| 637 | if (b->ended) refuse(17, "a line after ret, exit or trap in the same block"); |
| 638 | *b->tail = s; |
| 639 | b->tail = &s->next; |
| 640 | } |
| 641 | static void open_block(int kind, Stmt *owner, Stmt **tail) |
| 642 | { |
| 643 | blocks = grow(blocks, &capblocks, nblocks, sizeof *blocks); |
| 644 | Block *b = &blocks[nblocks++]; |
| 645 | b->kind = kind; b->owner = owner; b->tail = tail; b->ended = 0; b->then_ended = 0; |
| 646 | depth++; |
| 647 | b->name_mark = depth; |
| 648 | } |
| 649 | |
| 650 | static void parse_statement(void) |
| 651 | { |
| 652 | enlargement_assignment = 0; |
| 653 | const char *w = nextw(); |
| 654 | Block *top = &blocks[nblocks - 1]; |
| 655 | /* a number never starts a line: 16 before anything else about the line (om4 on mutant t_conv.om.m17: a number |
| 656 | * on the line after ret is 16, not 17; a word like `read` there is 17, mutant r5b_fwd.om.m7); gap G25 */ |
| 657 | if (looks_number(w)) refuse(16, "'%s' cannot start a line", w); |
| 658 | if (top->ended && strcmp(w, "end") && strcmp(w, "else")) refuse(17, "a line after ret or exit in the same block"); |
| 659 | if (!strcmp(w, "end")) { |
| 660 | line_end(); |
| 661 | int ended = top->ended; |
| 662 | close_scope(depth); |
| 663 | depth--; |
| 664 | if (top->kind == 0) { |
| 665 | if (!ended && curf->ret != T_VOID) refuse(18, "function '%s' returns a value and its end can be reached", curf->name); |
| 666 | curf->done = 1; |
| 667 | curf->nslots = nslots; |
| 668 | nblocks--; |
| 669 | return; |
| 670 | } |
| 671 | nblocks--; |
| 672 | Block *outer = &blocks[nblocks - 1]; |
| 673 | /* an if/else whose both branches end the path ends it too; a loop or a lone if never does */ |
| 674 | if (top->kind == 2 && ended && top->then_ended) outer->ended = 1; |
| 675 | return; |
| 676 | } |
| 677 | if (!strcmp(w, "else")) { |
| 678 | if (top->kind != 1) refuse(15, "else without if"); /* gap G25 */ |
| 679 | line_end(); |
| 680 | int ended = top->ended; |
| 681 | Stmt *ifs = top->owner; |
| 682 | close_scope(depth); |
| 683 | top->kind = 2; top->ended = 0; top->then_ended = ended; |
| 684 | top->tail = &ifs->els; |
| 685 | return; |
| 686 | } |
| 687 | if (!strcmp(w, "note")) return; |
| 688 | if (!strcmp(w, "if") || !strcmp(w, "while")) { |
| 689 | Stmt *s = new_stmt(w[0] == 'i' ? S_IF : S_WHILE); |
| 690 | s->v = value_for(T_BOOL, "a condition"); |
| 691 | npend--; place_check(s->v, T_BOOL, "a condition"); /* at once, before extra words: om4 gives 7 for `if r 1` with r int (G29) */ |
| 692 | if (s->kind == S_WHILE) { |
| 693 | s->n = LOOP_MAX; |
| 694 | if (peekw() && !strcmp(peekw(), "bound")) { |
| 695 | wi++; |
| 696 | const char *b = nextw(); |
| 697 | if (!b) refuse(4, "bound needs a number"); |
| 698 | int neg; uint64_t mag; |
| 699 | if (is_reserved(b) || (!is_name(b) && !looks_number(b))) refuse(16, "'%s' where the bound number belongs", b); /* gap G25 */ |
| 700 | if (!looks_number(b)) refuse(6, "bound needs a number"); |
| 701 | parse_number(b, &neg, &mag); |
| 702 | if (neg || mag < 1 || mag > INT64_MAX) refuse(8, "bound %s outside 1..INT64_MAX", b); |
| 703 | s->n = mag; |
| 704 | } |
| 705 | } |
| 706 | line_end(); |
| 707 | push_stmt(s); |
| 708 | open_block(s->kind == S_IF ? 1 : 3, s, &s->body); |
| 709 | return; |
| 710 | } |
| 711 | if (!strcmp(w, "for")) { |
| 712 | Stmt *s = new_stmt(S_FOR); |
| 713 | const char *nmw = nextw(); |
| 714 | const char *tw = nextw(); |
| 715 | if (!nmw || !tw) refuse(4, "for needs a name, a type and a count"); |
| 716 | int t = type_word(tw); |
| 717 | if (t == T_NONE) refuse(16, "for needs a type"); /* gap G25 */ |
| 718 | if (!tnum(t)) refuse(7, "for needs a whole-number type"); |
| 719 | s->ty = t; |
| 720 | const char *cw = peekw(); |
| 721 | if (!cw) refuse(4, "for needs a count"); |
| 722 | if (looks_number(cw) && wi + 1 == nw) { |
| 723 | int neg; uint64_t mag; |
| 724 | wi++; |
| 725 | parse_number(cw, &neg, &mag); |
| 726 | if (!fits(t, neg, mag)) refuse(8, "count %s outside %s", cw, TNAME[t]); |
| 727 | s->count_kind = 0; s->n = mag; |
| 728 | } else { |
| 729 | Name *nm = find_name(cw); |
| 730 | s->count_kind = (nm && nm->kind == K_SCALAR && wi + 1 == nw) ? 1 : 2; |
| 731 | s->v = value_for(t, "a for count"); |
| 732 | } |
| 733 | line_end(); |
| 734 | push_stmt(s); |
| 735 | open_block(4, s, &s->body); |
| 736 | Name *lv = add_name(nmw, K_SCALAR, t, 0, 1); |
| 737 | s->slot = lv->slot; |
| 738 | return; |
| 739 | } |
| 740 | if (!strcmp(w, "break")) { |
| 741 | refuse(9, "break is not among om4's words (gap G1)"); |
| 742 | line_end(); |
| 743 | int inloop = 0; |
| 744 | for (int i = nblocks - 1; i >= 0; i--) if (blocks[i].kind >= 3) inloop = 1; |
| 745 | if (!inloop) refuse(9, "break outside a loop"); /* spec silent */ |
| 746 | push_stmt(new_stmt(S_BREAK)); |
| 747 | return; |
| 748 | } |
| 749 | if (!strcmp(w, "ret")) { |
| 750 | Stmt *s = new_stmt(S_RET); |
| 751 | if (curf->ret == T_VOID) { |
| 752 | if (peekw()) refuse(7, "a void function returns no value"); |
| 753 | } else { |
| 754 | if (!peekw()) refuse(4, "ret needs a %s", TNAME[curf->ret]); /* gap G25 */ |
| 755 | s->v = value_for(curf->ret, "the returned value"); |
| 756 | } |
| 757 | line_end(); |
| 758 | push_stmt(s); |
| 759 | blocks[nblocks - 1].ended = 1; |
| 760 | return; |
| 761 | } |
| 762 | if (!strcmp(w, "trap")) { |
| 763 | refuse(9, "trap is not among om4's words (gap G1)"); |
| 764 | Stmt *s = new_stmt(S_TRAP); |
| 765 | const char *c = nextw(); |
| 766 | if (!c) refuse(4, "trap needs a code"); |
| 767 | int neg; uint64_t mag; |
| 768 | if (!looks_number(c)) refuse(6, "trap needs a number"); |
| 769 | parse_number(c, &neg, &mag); |
| 770 | if (neg || mag < 1 || mag > 14) refuse(8, "trap code %s outside 1..14", c); |
| 771 | s->n = mag; |
| 772 | line_end(); |
| 773 | push_stmt(s); |
| 774 | blocks[nblocks - 1].ended = 1; |
| 775 | return; |
| 776 | } |
| 777 | if (!strcmp(w, "exit")) { |
| 778 | Stmt *s = new_stmt(S_EXIT); |
| 779 | s->v = value_for(T_I32, "exit's value"); |
| 780 | line_end(); |
| 781 | push_stmt(s); |
| 782 | blocks[nblocks - 1].ended = 1; |
| 783 | return; |
| 784 | } |
| 785 | if (!strcmp(w, "put")) { |
| 786 | Stmt *s = new_stmt(S_PUT); |
| 787 | int ety, ro, kind; uint64_t len; |
| 788 | s->arr = parse_array_ref(nextw(), &ety, &len, &ro, &kind); |
| 789 | if (ro) refuse(20, "put into a read-only array"); |
| 790 | nest = 0; |
| 791 | s->v = parse_value(T_NONE); |
| 792 | if (s->v->bare) { |
| 793 | if (s->v->neg) refuse(8, "a negative index number (gap G7)"); |
| 794 | if (len != UINT64_MAX && s->v->num >= len) refuse(21, "index outside an array of %" PRIu64, len); |
| 795 | fix_bare(s->v, T_U64); |
| 796 | } |
| 797 | if (!tnum(s->v->ty)) refuse(7, "an index of type %s", TNAME[s->v->ty]); |
| 798 | s->v2 = value_for(ety, "the stored value"); |
| 799 | line_end(); |
| 800 | push_stmt(s); |
| 801 | return; |
| 802 | } |
| 803 | if (!strcmp(w, "free")) { |
| 804 | refuse(9, "there is no free: nothing is given back by hand (decision 24)"); |
| 805 | const char *a = nextw(); |
| 806 | if (!a) refuse(4, "free needs an array"); |
| 807 | Name *nm = find_name(a); |
| 808 | if (!nm || nm->kind != K_OWN) { |
| 809 | if (nm || find_global(a) >= 0) refuse(7, "free of '%s', not a local array", a); /* spec silent */ |
| 810 | refuse(3, "unknown name '%s'", a); |
| 811 | } |
| 812 | line_end(); |
| 813 | push_stmt(new_stmt(S_FREE)); |
| 814 | return; |
| 815 | } |
| 816 | if (!strcmp(w, "own")) refuse(9, "own is not in the subset"); |
| 817 | int fi = find_func(w); |
| 818 | if (fi >= 0) { |
| 819 | Stmt *s = new_stmt(S_CALL); |
| 820 | nest = 0; |
| 821 | s->v = parse_call(fi, 1); |
| 822 | line_end(); |
| 823 | push_stmt(s); |
| 824 | return; |
| 825 | } |
| 826 | /* <name> [=] <value> */ |
| 827 | if (find_global(w) >= 0) refuse(7, "'%s' is an array; write its elements with put", w); /* spec silent */ |
| 828 | if (!is_name(w) || is_reserved(w)) { |
| 829 | if (looks_number(w)) { int ng; uint64_t mg; parse_number(w, &ng, &mg); } |
| 830 | refuse(16, "'%s' cannot start a line", w); /* gap G25 */ |
| 831 | } |
| 832 | if (peekw() && !strcmp(peekw(), "=")) wi++; |
| 833 | if (!peekw()) refuse(4, "'%s' needs a value", w); |
| 834 | Name *nm = find_name(w); |
| 835 | if (!strcmp(peekw(), "new")) { |
| 836 | wi++; |
| 837 | if (nm) refuse(10, "'%s' is already in use", w); |
| 838 | if (depth != 1) refuse(9, "new only in a function's outermost block"); |
| 839 | const char *tw = nextw(), *lw = nextw(); |
| 840 | if (!tw || !lw) refuse(4, "new needs a type, a length and a value"); |
| 841 | int t = type_word(tw); |
| 842 | if (t == T_NONE) refuse(16, "new needs a type"); /* gap G25 */ |
| 843 | if (!tnum(t)) refuse(7, "new needs a whole-number element type"); |
| 844 | int neg; uint64_t mag; |
| 845 | if (!looks_number(lw)) refuse(16, "new needs its length written as a number"); |
| 846 | parse_number(lw, &neg, &mag); |
| 847 | if (neg || mag < 1) refuse(8, "array length %s", lw); |
| 848 | if (mag > MAX_LOCAL_ARR_BYTES / (uint64_t)tbytes(t) || !local_arr_bytes_ok(mag * (uint64_t)tbytes(t))) |
| 849 | refuse(23, "local arrays over 4 MiB"); |
| 850 | Stmt *s = new_stmt(S_NEW); |
| 851 | s->ety = t; s->n = mag; |
| 852 | s->v = value_for(t, "the initial value"); |
| 853 | line_end(); |
| 854 | push_stmt(s); |
| 855 | Name *a = add_name(w, K_OWN, t, mag, 0); |
| 856 | s->slot = a->slot; |
| 857 | return; |
| 858 | } |
| 859 | Stmt *s = new_stmt(S_ASSIGN); |
| 860 | enlargement_assignment = !strcmp(peekw(), "enlarge"); |
| 861 | if (nm) { |
| 862 | if (nm->kind != K_SCALAR) refuse(7, "'%s' is an array", w); |
| 863 | s->v = value_for(nm->ty, "the value"); |
| 864 | s->slot = nm->slot; |
| 865 | line_end(); |
| 866 | push_stmt(s); |
| 867 | } else { |
| 868 | check_new_name(w, 0); |
| 869 | s->v = value_for(T_NONE, "a new name's value"); |
| 870 | line_end(); |
| 871 | if (s->v->ty == T_VOID) refuse(7, "a void call has no value"); |
| 872 | push_stmt(s); |
| 873 | Name *a = add_name(w, K_SCALAR, s->v->ty, 0, 0); |
| 874 | s->slot = a->slot; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | static int local_arr_bytes_ok(uint64_t add) |
| 879 | { |
| 880 | local_arr_bytes += add; |
| 881 | return local_arr_bytes <= MAX_LOCAL_ARR_BYTES; |
| 882 | } |
| 883 | |
| 884 | /* The names pass (9.2): every fn line's signature is read before any code, so a call may name a function |
| 885 | * written later. prepass: the signature is read into f (refusals in the signature are reported here, in |
| 886 | * input order, before any body). Not prepass: the same line is read again when the body pass reaches it, |
| 887 | * to put its parameters in scope. */ |
| 888 | static void parse_fn_signature(Func *f, int prepass) |
| 889 | { |
| 890 | const char *nmw = nextw(); |
| 891 | if (!nmw) refuse(4, "fn needs a name"); |
| 892 | if (prepass) { |
| 893 | if (!is_name(nmw) || is_reserved(nmw)) refuse(16, "'%s' cannot be a name", nmw); /* gap G25 */ |
| 894 | if (strlen(nmw) > 63) refuse(13, "name '%s' too long", nmw); |
| 895 | if (find_func(nmw) >= 0) refuse(10, "'%s' is already in use", nmw); |
| 896 | memset(f, 0, sizeof *f); |
| 897 | snprintf(f->name, sizeof f->name, "%s", nmw); |
| 898 | f->sig_line = cur_line; |
| 899 | } else { |
| 900 | if (find_global(nmw) >= 0) refuse(10, "'%s' is already in use", nmw); /* a pool or text written before it */ |
| 901 | f->np = 0; |
| 902 | } |
| 903 | curf = f; |
| 904 | nnames = 0; nslots = 0; depth = 0; local_arr_bytes = 0; |
| 905 | int is_main = !strcmp(nmw, "main"); |
| 906 | for (;;) { |
| 907 | const char *pw = nextw(); |
| 908 | if (!pw) refuse(4, "fn %s needs 'to' and a return type", nmw); |
| 909 | if (!strcmp(pw, "to")) break; |
| 910 | if (f->np >= 6) refuse(9, "more than 6 parameters (9.2)"); /* om4: 9 at the fn line */ |
| 911 | if (!strcmp(pw, nmw)) refuse(10, "parameter '%s' has the function's name", pw); |
| 912 | check_new_name(pw, 0); |
| 913 | if (is_main) refuse(9, "main has no parameters"); |
| 914 | for (int j = 0; j < nnames; j++) if (!strcmp(names[j].name, pw)) refuse(10, "two parameters named '%s'", pw); |
| 915 | const char *tw = nextw(); |
| 916 | if (!tw) refuse(4, "parameter '%s' needs a type", pw); |
| 917 | int i = f->np++; |
| 918 | if (!strcmp(tw, "view") || !strcmp(tw, "edit")) { |
| 919 | f->pkind[i] = tw[0] == 'v' ? PK_VIEW : PK_EDIT; |
| 920 | const char *et = nextw(), *lw = peekw(); |
| 921 | int t = et ? type_word(et) : T_NONE; |
| 922 | if (t == T_NONE) refuse(16, "an array parameter needs a type"); /* gap G25 */ |
| 923 | if (!tnum(t)) refuse(7, "an array parameter needs a whole-number element type"); |
| 924 | if (!lw || !looks_number(lw)) refuse(16, "array parameter '%s' without its length", pw); |
| 925 | wi++; |
| 926 | int neg; uint64_t mag; |
| 927 | parse_number(lw, &neg, &mag); |
| 928 | if (neg || mag < 1 || mag > MAX_POOL_ELEMS) refuse(8, "array length %s", lw); /* spec silent on the upper limit */ |
| 929 | f->pty[i] = t; f->plen[i] = mag; |
| 930 | add_name(pw, f->pkind[i] == PK_VIEW ? K_VIEW : K_EDIT, t, mag, 0); |
| 931 | } else if (!strcmp(tw, "own")) { |
| 932 | refuse(9, "own is not in the subset"); |
| 933 | } else { |
| 934 | int t = type_word(tw); |
| 935 | if (t == T_NONE) refuse(16, "'%s' is not a type", tw); /* gap G25 */ |
| 936 | f->pkind[i] = PK_SCALAR; f->pty[i] = t; |
| 937 | add_name(pw, K_SCALAR, t, 0, 0); |
| 938 | } |
| 939 | } |
| 940 | const char *rw = nextw(); |
| 941 | if (!rw) refuse(4, "fn %s needs a return type", nmw); |
| 942 | if (!strcmp(rw, "void")) f->ret = T_VOID; |
| 943 | else if ((f->ret = type_word(rw)) == T_NONE) refuse(16, "'%s' is not a type", rw); |
| 944 | if (is_main && f->ret != T_VOID && f->ret != T_BOOL && !tnum(f->ret)) /* bool allowed: gap G4 */ refuse(9, "main returns a whole number or void"); |
| 945 | line_end(); |
| 946 | if (prepass) return; |
| 947 | nblocks = 0; |
| 948 | open_block(0, NULL, &f->body); |
| 949 | } |
| 950 | |
| 951 | static int fn_seen; /* fn lines the body pass has reached, in order: the k-th is funcs[k] (the names pass read them all) */ |
| 952 | static void parse_fn_header(void) |
| 953 | { |
| 954 | parse_fn_signature(&funcs[fn_seen++], 0); |
| 955 | } |
| 956 | |
| 957 | /* 9.2: in function mode a call cycle, a function calling itself included, is error 9 */ |
| 958 | static int *on_path, *visited; |
| 959 | static int find_cycle(int f) |
| 960 | { |
| 961 | if (on_path[f]) return f; |
| 962 | if (visited[f]) return -1; |
| 963 | visited[f] = on_path[f] = 1; |
| 964 | for (int i = 0; i < funcs[f].ncallees; i++) { |
| 965 | int c = find_cycle(funcs[f].callees[i]); |
| 966 | if (c >= 0) return c; |
| 967 | } |
| 968 | on_path[f] = 0; |
| 969 | return -1; |
| 970 | } |
| 971 | |
| 972 | static void parse_global(int is_text, const char *line, size_t n) |
| 973 | { |
| 974 | if (!nglobals) first_global_line = cur_line; |
| 975 | const char *nmw = nextw(); |
| 976 | if (!nmw) refuse(4, "%s needs a name", is_text ? "text" : "global"); |
| 977 | check_new_name(nmw, 0); |
| 978 | globals = grow(globals, &capglobals, nglobals, sizeof *globals); |
| 979 | Global *g = &globals[nglobals]; |
| 980 | memset(g, 0, sizeof *g); |
| 981 | snprintf(g->name, sizeof g->name, "%s", nmw); |
| 982 | if (!is_text) { |
| 983 | const char *tw = nextw(), *lw = nextw(); |
| 984 | int t = tw ? type_word(tw) : T_NONE; |
| 985 | if (!tw) refuse(4, "global needs a type and a length"); |
| 986 | if (t == T_NONE) refuse(16, "a pool needs a type"); /* gap G25 */ |
| 987 | if (!tnum(t)) refuse(7, "a pool of whole numbers"); |
| 988 | if (lw && !strcmp(lw, "*")) { |
| 989 | line_end(); |
| 990 | g->ety = t; g->len = UINT64_MAX; |
| 991 | g->arr.ety = t; |
| 992 | nglobals++; |
| 993 | return; |
| 994 | } |
| 995 | if (!lw || !looks_number(lw)) refuse(16, "pool '%s' without its length", nmw); |
| 996 | int neg; uint64_t mag; |
| 997 | parse_number(lw, &neg, &mag); |
| 998 | if (neg || mag < 1) refuse(8, "pool length %s", lw); /* spec silent */ |
| 999 | if (mag > MAX_POOL_ELEMS) refuse(23, "pool over 16777216 elements"); |
| 1000 | line_end(); |
| 1001 | uint64_t bytes = mag * (uint64_t)tbytes(t); |
| 1002 | if (pool_bytes + bytes > MAX_POOL_BYTES) refuse(23, "pools over 1 GiB"); |
| 1003 | pool_bytes += bytes; |
| 1004 | g->ety = t; g->len = mag; |
| 1005 | g->arr.data = mmap(NULL, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); |
| 1006 | if (g->arr.data == MAP_FAILED) { perror("omega_ref: pool"); exit(2); } |
| 1007 | g->arr.ety = t; g->arr.len = mag; g->arr.ro = 0; |
| 1008 | } else { |
| 1009 | /* the bytes between the quotes, from the raw line */ |
| 1010 | const char *q = memchr(line, '"', n); |
| 1011 | if (!q) refuse(22, "text '%s' needs its bytes in quotes", nmw); |
| 1012 | const char *p = q + 1, *e = line + n; |
| 1013 | uint8_t *buf = resize(NULL, n + 1, 1); |
| 1014 | size_t len = 0; |
| 1015 | int closed = 0; |
| 1016 | while (p < e) { |
| 1017 | char c = *p++; |
| 1018 | if (c == '"') { closed = 1; break; } |
| 1019 | if (c == '\\') { |
| 1020 | if (p >= e) break; |
| 1021 | char x = *p++; |
| 1022 | if (x == 'n') c = 10; else if (x == 't') c = 9; else if (x == '\\') c = 92; else if (x == '"') c = 34; |
| 1023 | else refuse(22, "escape '\\%c' has no meaning", x); |
| 1024 | } |
| 1025 | buf[len++] = (uint8_t)c; |
| 1026 | } |
| 1027 | if (!closed) refuse(22, "text '%s' has no end quote", nmw); |
| 1028 | /* the name must be the only word between text and the quote; nothing after the end quote */ |
| 1029 | for (const char *r = p; r < e; r++) if (*r != ' ' && *r != '\t' && *r != '\r') refuse(5, "extra words after a text"); |
| 1030 | g->is_text = 1; g->ety = T_U8; g->len = len; |
| 1031 | g->arr.data = buf; g->arr.ety = T_U8; g->arr.len = len; g->arr.ro = 1; |
| 1032 | } |
| 1033 | nglobals++; |
| 1034 | } |
| 1035 | |
| 1036 | static void parse_program(void) |
| 1037 | { |
| 1038 | /* program mode: a line `fn main` anywhere (9.1). The names pass (9.2): every line that starts with fn |
| 1039 | * has its signature read before any code, so a call may name a function written later. The pass |
| 1040 | * knows nothing of blocks: a fn line inside a function that lacks its end is read here like any |
| 1041 | * other, and the body pass refuses it (15) when it gets there (gap G27: observed on om4's mutants). */ |
| 1042 | size_t pos = 0; |
| 1043 | cur_line = 0; |
| 1044 | while (pos < srclen) { |
| 1045 | const char *ls = src + pos, *nl = memchr(ls, '\n', srclen - pos); |
| 1046 | size_t n = nl ? (size_t)(nl - ls) : srclen - pos; |
| 1047 | pos += n + 1; |
| 1048 | cur_line++; |
| 1049 | split_words(ls, n); |
| 1050 | if (nw == 0 || strcmp(W[0], "fn")) continue; |
| 1051 | if (nw >= 2 && !strcmp(W[1], "main")) program_mode = 1; |
| 1052 | funcs = grow(funcs, &capfuncs, nfuncs, sizeof *funcs); |
| 1053 | wi = 1; |
| 1054 | parse_fn_signature(&funcs[nfuncs], 1); |
| 1055 | nfuncs++; |
| 1056 | } |
| 1057 | on_path = resize(NULL, (size_t)nfuncs, sizeof *on_path); |
| 1058 | visited = resize(NULL, (size_t)nfuncs, sizeof *visited); |
| 1059 | memset(on_path, 0, (size_t)nfuncs * sizeof *on_path); |
| 1060 | memset(visited, 0, (size_t)nfuncs * sizeof *visited); |
| 1061 | nnames = 0; nslots = 0; depth = 0; curf = NULL; |
| 1062 | pos = 0; |
| 1063 | cur_line = 0; |
| 1064 | int infn = 0; |
| 1065 | while (pos < srclen) { |
| 1066 | const char *ls = src + pos, *nl = memchr(ls, '\n', srclen - pos); |
| 1067 | size_t n = nl ? (size_t)(nl - ls) : srclen - pos; |
| 1068 | pos += n + 1; |
| 1069 | cur_line++; |
| 1070 | split_words(ls, n); |
| 1071 | if (nw == 0) continue; |
| 1072 | const char *w = W[0]; |
| 1073 | if (!infn) { |
| 1074 | wi = 1; |
| 1075 | if (!strcmp(w, "fn")) { parse_fn_header(); infn = 1; } |
| 1076 | else if (!strcmp(w, "global")) parse_global(0, ls, n); |
| 1077 | else if (!strcmp(w, "text")) parse_global(1, ls, n); |
| 1078 | else if (!strcmp(w, "note")) continue; |
| 1079 | else if (looks_number(w)) { int ng; uint64_t mg; parse_number(w, &ng, &mg); refuse(16, "a number outside a function"); } |
| 1080 | else if (is_name(w) && !is_reserved(w) && strlen(w) > 8) refuse(13, "name '%s' too long", w); /* om4 reads it as a name first: gap G25 */ |
| 1081 | else refuse(16, "'%s' outside a function", w); |
| 1082 | continue; |
| 1083 | } |
| 1084 | if (!strcmp(w, "fn") || !strcmp(w, "global") || !strcmp(w, "text")) |
| 1085 | refuse(15, "function '%s' has no end", curf->name); |
| 1086 | parse_statement(); |
| 1087 | if (curf->done) infn = 0; |
| 1088 | } |
| 1089 | if (infn) refuse(15, "function '%s' has no end", curf->name); |
| 1090 | if (!program_mode && nglobals) { cur_line = first_global_line; refuse(9, "a pool or text only in program mode (no fn main)"); } |
| 1091 | if (!program_mode) |
| 1092 | for (int f = 0; f < nfuncs; f++) { |
| 1093 | int c = find_cycle(f); |
| 1094 | if (c >= 0) { /* reported at the end of the input, the line after the last (as om4: gap G26) */ |
| 1095 | cur_line = 1; for (size_t i = 0; i < srclen; i++) if (src[i] == '\n') cur_line++; |
| 1096 | refuse(9, "'%s' is in a call cycle in function mode", funcs[c].name); |
| 1097 | } |
| 1098 | } |
| 1099 | if (nfuncs == 0) refuse(16, "no function"); /* spec silent */ |
| 1100 | } |
| 1101 | |
| 1102 | /* ---------- running ---------- */ |
| 1103 | typedef struct Frame { |
| 1104 | uint64_t *v; |
| 1105 | Arr **a; |
| 1106 | } Frame; |
| 1107 | |
| 1108 | static uint64_t stack_used; |
| 1109 | static uint64_t eval(Node *n, Frame *fr); |
| 1110 | static Arr *aref(ARef r, Frame *fr) { return r.kind == AK_GLOBAL ? &globals[r.idx].arr : fr->a[r.idx]; } |
| 1111 | |
| 1112 | static uint64_t check_index(int ity, uint64_t i, uint64_t len) |
| 1113 | { |
| 1114 | if (tsigned(ity) && (int64_t)i < 0) trap(3); |
| 1115 | if (i >= len) trap(3); |
| 1116 | return i; |
| 1117 | } |
| 1118 | |
| 1119 | static uint64_t arith(int op, int t, uint64_t x, uint64_t y) |
| 1120 | { |
| 1121 | int w = twidth(t); |
| 1122 | if (t == T_BOOL) { |
| 1123 | switch (op) { case OP_AND: return x & y; case OP_OR: return x | y; default: return x ^ y; } |
| 1124 | } |
| 1125 | if (!tsigned(t)) { |
| 1126 | switch (op) { |
| 1127 | case OP_ADD: return canon(t, x + y); |
| 1128 | case OP_SUB: return canon(t, x - y); |
| 1129 | case OP_MUL: return canon(t, x * y); |
| 1130 | case OP_DIV: if (!y) trap(2); return x / y; |
| 1131 | case OP_REM: if (!y) trap(2); return x % y; |
| 1132 | case OP_AND: return x & y; |
| 1133 | case OP_OR: return x | y; |
| 1134 | case OP_XOR: return x ^ y; |
| 1135 | } |
| 1136 | } |
| 1137 | int64_t a = (int64_t)x, b = (int64_t)y, r; |
| 1138 | __int128 e; |
| 1139 | switch (op) { |
| 1140 | case OP_ADD: e = (__int128)a + b; break; |
| 1141 | case OP_SUB: e = (__int128)a - b; break; |
| 1142 | case OP_MUL: e = (__int128)a * b; break; |
| 1143 | case OP_DIV: |
| 1144 | if (!b) trap(2); |
| 1145 | if (a == smin(t) && b == -1) trap(1); |
| 1146 | return canon(t, (uint64_t)(a / b)); |
| 1147 | case OP_REM: |
| 1148 | if (!b) trap(2); |
| 1149 | if (b == -1) return 0; |
| 1150 | return canon(t, (uint64_t)(a % b)); |
| 1151 | case OP_AND: return x & y; |
| 1152 | case OP_OR: return x | y; |
| 1153 | case OP_XOR: return x ^ y; |
| 1154 | default: e = 0; |
| 1155 | } |
| 1156 | (void)w; |
| 1157 | if (e < smin(t) || e > smax(t)) trap(1); |
| 1158 | r = (int64_t)e; |
| 1159 | return canon(t, (uint64_t)r); |
| 1160 | } |
| 1161 | |
| 1162 | static uint64_t shift(int op, int t, uint64_t x, int st, uint64_t s) |
| 1163 | { |
| 1164 | int w = twidth(t); |
| 1165 | if ((tsigned(st) && (int64_t)s < 0) || s >= (uint64_t)w) trap(7); |
| 1166 | if (op == OP_SHR) { |
| 1167 | if (tsigned(t)) return canon(t, (uint64_t)((int64_t)x >> s)); |
| 1168 | return x >> s; |
| 1169 | } |
| 1170 | if (!tsigned(t)) return canon(t, x << s); |
| 1171 | int64_t a = (int64_t)x; |
| 1172 | __int128 e = (__int128)a * ((__int128)1 << s); |
| 1173 | if (e < smin(t) || e > smax(t)) trap(1); |
| 1174 | return canon(t, (uint64_t)(int64_t)e); |
| 1175 | } |
| 1176 | |
| 1177 | static uint64_t compare(int op, int t, uint64_t x, uint64_t y) |
| 1178 | { |
| 1179 | int lt, eq = x == y; |
| 1180 | if (tsigned(t)) lt = (int64_t)x < (int64_t)y; else lt = x < y; |
| 1181 | switch (op) { |
| 1182 | case OP_EQ: return eq; |
| 1183 | case OP_NE: return !eq; |
| 1184 | case OP_LT: return lt; |
| 1185 | case OP_LE: return lt || eq; |
| 1186 | case OP_GT: return !lt && !eq; |
| 1187 | default: return !lt; |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | static uint64_t convert(int to, int from, uint64_t x) |
| 1192 | { |
| 1193 | if (tsigned(from) && (int64_t)x < 0) { |
| 1194 | if (!tsigned(to) || (int64_t)x < smin(to)) trap(5); |
| 1195 | return x; |
| 1196 | } |
| 1197 | if (x > (tsigned(to) ? (uint64_t)smax(to) : umax(to))) trap(5); |
| 1198 | return x; |
| 1199 | } |
| 1200 | |
| 1201 | static uint64_t call_fn(int fi, uint64_t *sargs, Arr **aargs); |
| 1202 | |
| 1203 | static uint64_t eval(Node *n, Frame *fr) |
| 1204 | { |
| 1205 | uint64_t x, y, z; |
| 1206 | switch (n->kind) { |
| 1207 | case N_NUM: return n->num; |
| 1208 | case N_VAR: return fr->v[n->slot]; |
| 1209 | case N_BIN: |
| 1210 | x = eval(n->a, fr); |
| 1211 | y = eval(n->b, fr); |
| 1212 | if (n->op == OP_SHL || n->op == OP_SHR) return shift(n->op, n->ty, x, n->b->ty, y); |
| 1213 | return arith(n->op, n->ty, x, y); |
| 1214 | case N_CMP: |
| 1215 | x = eval(n->a, fr); |
| 1216 | y = eval(n->b, fr); |
| 1217 | return compare(n->op, n->a->ty, x, y); |
| 1218 | case N_NEG: |
| 1219 | x = eval(n->a, fr); |
| 1220 | if (tsigned(n->ty)) { if ((int64_t)x == smin(n->ty)) trap(1); return canon(n->ty, (uint64_t)-(int64_t)x); } |
| 1221 | return canon(n->ty, (uint64_t)0 - x); |
| 1222 | case N_NOT: |
| 1223 | x = eval(n->a, fr); |
| 1224 | return n->ty == T_BOOL ? !x : canon(n->ty, ~x); |
| 1225 | case N_CONV: |
| 1226 | x = eval(n->a, fr); |
| 1227 | return convert(n->ty, n->a->ty, x); |
| 1228 | case N_GET: { |
| 1229 | Arr *a = aref(n->arr, fr); |
| 1230 | x = eval(n->a, fr); |
| 1231 | return arr_get(a, check_index(n->a->ty, x, a->len)); |
| 1232 | } |
| 1233 | case N_LEN: return aref(n->arr, fr)->len; |
| 1234 | case N_ENLARGE: { |
| 1235 | Arr *a = aref(n->arr, fr); |
| 1236 | x = eval(n->a, fr); |
| 1237 | if (x <= a->len) return 0; |
| 1238 | size_t width = (size_t)tbytes(a->ety); |
| 1239 | if (x > (uint64_t)INT64_MAX / width) return (uint64_t)(int64_t)-EINVAL; |
| 1240 | size_t bytes = (size_t)x * width; |
| 1241 | void *p = a->len ? mremap(a->data, (size_t)a->len * width, bytes, MREMAP_MAYMOVE) |
| 1242 | : mmap(NULL, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 1243 | if (p == MAP_FAILED) return (uint64_t)(int64_t)-errno; |
| 1244 | a->data = p; a->len = x; |
| 1245 | return 0; |
| 1246 | } |
| 1247 | case N_ENVCNT: { |
| 1248 | uint64_t count = 0; |
| 1249 | while (environ[count]) count++; |
| 1250 | return count; |
| 1251 | } |
| 1252 | case N_ENVREAD: { |
| 1253 | x = eval(n->a, fr); y = eval(n->b, fr); z = eval(n->c, fr); |
| 1254 | Arr *a = aref(n->arr, fr); |
| 1255 | if (z > a->len || y > a->len - z) trap(3); |
| 1256 | uint64_t count = 0; |
| 1257 | while (environ[count]) count++; |
| 1258 | if (x >= count) trap(3); |
| 1259 | size_t length = strlen(environ[x]); |
| 1260 | if (length > z) return (uint64_t)(int64_t)-ENOBUFS; |
| 1261 | memcpy(a->data + y, environ[x], length); |
| 1262 | return length; |
| 1263 | } |
| 1264 | case N_READ: case N_WRITE: { |
| 1265 | Arr *a = aref(n->arr, fr); |
| 1266 | x = eval(n->a, fr); |
| 1267 | y = eval(n->b, fr); |
| 1268 | z = eval(n->c, fr); |
| 1269 | if (!(z <= a->len && y <= a->len - z)) trap(3); |
| 1270 | ssize_t r = n->kind == N_READ ? read((int)(int32_t)x, a->data + y, z) : write((int)(int32_t)x, a->data + y, z); |
| 1271 | return r < 0 ? (uint64_t)(int64_t)-errno : (uint64_t)r; |
| 1272 | } |
| 1273 | case N_CALL: { |
| 1274 | Func *f = &funcs[n->fn]; |
| 1275 | uint64_t sargs[MAX_PARAMS]; |
| 1276 | Arr *aargs[MAX_PARAMS]; |
| 1277 | for (int i = 0; i < f->np; i++) { |
| 1278 | if (f->pkind[i] == PK_SCALAR) { sargs[i] = eval(n->args[i], fr); aargs[i] = NULL; } |
| 1279 | else aargs[i] = aref(n->aargs[i], fr); |
| 1280 | } |
| 1281 | return call_fn(n->fn, sargs, aargs); |
| 1282 | } |
| 1283 | } |
| 1284 | fprintf(stderr, "omega_ref: internal: node kind %d\n", n->kind); |
| 1285 | exit(2); |
| 1286 | } |
| 1287 | |
| 1288 | enum { C_NORMAL, C_BREAK, C_RET }; |
| 1289 | |
| 1290 | static int exec(Stmt *s, Frame *fr, uint64_t *ret, uint64_t *frame_extra) |
| 1291 | { |
| 1292 | for (; s; s = s->next) { |
| 1293 | switch (s->kind) { |
| 1294 | case S_ASSIGN: fr->v[s->slot] = eval(s->v, fr); break; |
| 1295 | case S_NEW: { |
| 1296 | uint64_t bytes = s->n * (uint64_t)tbytes(s->ety); |
| 1297 | uint64_t charge = (bytes + 15) & ~(uint64_t)15; |
| 1298 | if (stack_used + charge > STACK_BYTES) trap(6); |
| 1299 | uint64_t init = eval(s->v, fr); |
| 1300 | stack_used += charge; *frame_extra += charge; |
| 1301 | Arr *a = resize(NULL, 1, sizeof *a); |
| 1302 | a->data = resize(NULL, bytes ? bytes : 1, 1); |
| 1303 | a->ety = s->ety; a->len = s->n; a->ro = 0; |
| 1304 | for (uint64_t i = 0; i < s->n; i++) arr_put(a, i, init); |
| 1305 | fr->a[s->slot] = a; |
| 1306 | break; |
| 1307 | } |
| 1308 | case S_IF: { |
| 1309 | int c = exec(eval(s->v, fr) ? s->body : s->els, fr, ret, frame_extra); |
| 1310 | if (c != C_NORMAL) return c; |
| 1311 | break; |
| 1312 | } |
| 1313 | case S_WHILE: { |
| 1314 | uint64_t passes = 0; |
| 1315 | while (eval(s->v, fr)) { |
| 1316 | if (passes >= s->n) trap(4); |
| 1317 | passes++; |
| 1318 | int c = exec(s->body, fr, ret, frame_extra); |
| 1319 | if (c == C_BREAK) break; |
| 1320 | if (c == C_RET) return c; |
| 1321 | } |
| 1322 | break; |
| 1323 | } |
| 1324 | case S_FOR: { |
| 1325 | uint64_t count = 0, passes = 0; |
| 1326 | if (s->count_kind == 0) count = s->n; |
| 1327 | else if (s->count_kind == 2) count = eval(s->v, fr); |
| 1328 | fr->v[s->slot] = 0; |
| 1329 | for (;;) { |
| 1330 | if (s->count_kind == 1) count = eval(s->v, fr); |
| 1331 | if (!compare(OP_LT, s->ty, fr->v[s->slot], count)) break; |
| 1332 | if (s->count_kind != 0) { if (passes >= LOOP_MAX) trap(4); passes++; } |
| 1333 | int c = exec(s->body, fr, ret, frame_extra); |
| 1334 | if (c == C_BREAK) break; |
| 1335 | if (c == C_RET) return c; |
| 1336 | fr->v[s->slot] = arith(OP_ADD, s->ty, fr->v[s->slot], 1); |
| 1337 | } |
| 1338 | break; |
| 1339 | } |
| 1340 | case S_BREAK: return C_BREAK; |
| 1341 | case S_RET: if (s->v) *ret = eval(s->v, fr); return C_RET; |
| 1342 | case S_PUT: { |
| 1343 | Arr *a = aref(s->arr, fr); |
| 1344 | uint64_t i = eval(s->v, fr); |
| 1345 | uint64_t v = eval(s->v2, fr); |
| 1346 | arr_put(a, check_index(s->v->ty, i, a->len), v); |
| 1347 | break; |
| 1348 | } |
| 1349 | case S_FREE: break; |
| 1350 | case S_TRAP: trap((int)s->n); break; |
| 1351 | case S_EXIT: { |
| 1352 | uint64_t v = eval(s->v, fr); |
| 1353 | _exit((int)(v & 255)); |
| 1354 | } |
| 1355 | case S_CALL: eval(s->v, fr); break; |
| 1356 | } |
| 1357 | } |
| 1358 | return C_NORMAL; |
| 1359 | } |
| 1360 | |
| 1361 | static uint64_t call_fn(int fi, uint64_t *sargs, Arr **aargs) |
| 1362 | { |
| 1363 | Func *f = &funcs[fi]; |
| 1364 | uint64_t charge = f->has_call ? 16 : 0; /* the smallest frame a non-leaf call can have (spec silent) */ |
| 1365 | if (program_mode && stack_used + charge > STACK_BYTES) trap(6); |
| 1366 | stack_used += charge; |
| 1367 | uint64_t vals[(size_t)f->nslots + 1]; |
| 1368 | Arr *arrs[(size_t)f->nslots + 1]; |
| 1369 | Arr views[MAX_PARAMS]; |
| 1370 | memset(vals, 0, sizeof vals); |
| 1371 | memset(arrs, 0, sizeof arrs); |
| 1372 | Frame fr = {vals, arrs}; |
| 1373 | for (int i = 0; i < f->np; i++) { |
| 1374 | if (f->pkind[i] == PK_SCALAR) vals[i] = sargs[i]; |
| 1375 | else { |
| 1376 | views[i] = *aargs[i]; |
| 1377 | if (f->pkind[i] == PK_VIEW) views[i].ro = 1; |
| 1378 | arrs[i] = &views[i]; |
| 1379 | } |
| 1380 | } |
| 1381 | uint64_t ret = 0, extra = 0; |
| 1382 | exec(f->body, &fr, &ret, &extra); |
| 1383 | for (int i = f->np; i < f->nslots; i++) if (arrs[i]) { free(arrs[i]->data); free(arrs[i]); } |
| 1384 | stack_used -= charge + extra; |
| 1385 | return ret; |
| 1386 | } |
| 1387 | |
| 1388 | /* ---------- driver ---------- */ |
| 1389 | static int argc_g; |
| 1390 | static char **argv_g; |
| 1391 | static int result; |
| 1392 | |
| 1393 | static void load(const char *path) |
| 1394 | { |
| 1395 | FILE *f = fopen(path, "rb"); |
| 1396 | if (!f) { perror(path); exit(2); } |
| 1397 | size_t capacity = 65536; |
| 1398 | src = resize(NULL, capacity, 1); |
| 1399 | for (;;) { |
| 1400 | srclen += fread(src + srclen, 1, capacity - srclen, f); |
| 1401 | if (ferror(f)) { perror("omega_ref: input"); exit(2); } |
| 1402 | if (feof(f)) break; |
| 1403 | if (capacity > (SIZE_MAX - 4) / 2) { fputs("omega_ref: input size overflow\n", stderr); exit(2); } |
| 1404 | capacity *= 2; |
| 1405 | src = resize(src, capacity, 1); |
| 1406 | } |
| 1407 | fclose(f); |
| 1408 | if (srclen > (SIZE_MAX - 4) / 2) exit(2); |
| 1409 | W = resize(NULL, srclen + 2, sizeof *W); |
| 1410 | wbuf = resize(NULL, srclen * 2 + 4, 1); |
| 1411 | if (!W || !wbuf) { perror("omega_ref"); exit(2); } |
| 1412 | } |
| 1413 | |
| 1414 | static void *run_main(void *unused) |
| 1415 | { |
| 1416 | (void)unused; |
| 1417 | const char *cmd = argv_g[1]; |
| 1418 | if (!strcmp(cmd, "check")) { |
| 1419 | load(argv_g[2]); |
| 1420 | parse_program(); |
| 1421 | result = 0; |
| 1422 | return NULL; |
| 1423 | } |
| 1424 | if (!strcmp(cmd, "run")) { |
| 1425 | load(argv_g[2]); |
| 1426 | refuse_exit = 125; /* a refused program exits 125, not its refusal number */ |
| 1427 | parse_program(); |
| 1428 | static int mi; mi = find_func("main"); |
| 1429 | if (!program_mode || mi < 0) { fprintf(stderr, "omega_ref: run needs fn main\n"); exit(125); } |
| 1430 | if (setjmp(trap_jb)) { result = 100 + trap_code; return NULL; } |
| 1431 | uint64_t v = call_fn(mi, NULL, NULL); |
| 1432 | result = funcs[mi].ret == T_VOID ? 0 : (int)(v & 255); |
| 1433 | return NULL; |
| 1434 | } |
| 1435 | if (!strcmp(cmd, "call")) { |
| 1436 | load(argv_g[2]); |
| 1437 | parse_program(); |
| 1438 | static int fi; fi = find_func(argv_g[3]); |
| 1439 | if (fi < 0) { fprintf(stderr, "omega_ref: no function %s\n", argv_g[3]); exit(2); } |
| 1440 | Func *f = &funcs[fi]; |
| 1441 | FILE *in = fopen(argv_g[4], "r"); |
| 1442 | if (!in) { perror(argv_g[4]); exit(2); } |
| 1443 | char line[512]; |
| 1444 | while (fgets(line, sizeof line, in)) { |
| 1445 | uint64_t a[MAX_PARAMS] = {0}; |
| 1446 | char *p = line; |
| 1447 | for (int i = 0; i < f->np; i++) { |
| 1448 | if (f->pkind[i] != PK_SCALAR) { fprintf(stderr, "omega_ref: call with an array parameter\n"); exit(2); } |
| 1449 | a[i] = tsigned(f->pty[i]) ? (uint64_t)strtoll(p, &p, 10) : strtoull(p, &p, 10); |
| 1450 | if (a[i] != canon(f->pty[i], a[i])) { fprintf(stderr, "omega_ref: input out of range: %s", line); exit(2); } |
| 1451 | } |
| 1452 | stack_used = 0; |
| 1453 | if (setjmp(trap_jb)) { printf("TRAP %d\t-\n", trap_code); continue; } |
| 1454 | uint64_t r = call_fn(fi, a, NULL); |
| 1455 | if (tsigned(f->ret)) printf("%" PRId64, (int64_t)r); else printf("%" PRIu64, r); |
| 1456 | printf("\t0x%016" PRIx64 "\n", r); |
| 1457 | } |
| 1458 | fflush(stdout); |
| 1459 | result = 0; |
| 1460 | return NULL; |
| 1461 | } |
| 1462 | fprintf(stderr, "omega_ref: unknown command %s\n", cmd); |
| 1463 | exit(2); |
| 1464 | } |
| 1465 | |
| 1466 | int main(int argc, char **argv) |
| 1467 | { |
| 1468 | argc_g = argc; argv_g = argv; |
| 1469 | if (argc < 3 || (!strcmp(argv[1], "call") && argc != 5)) { |
| 1470 | fprintf(stderr, "usage: omega_ref check|run <prog> | omega_ref call <prog> <entry> <inputs>\n"); |
| 1471 | return 2; |
| 1472 | } |
| 1473 | /* a big thread stack: deep Omega recursion becomes deep C recursion here */ |
| 1474 | pthread_attr_t at; |
| 1475 | pthread_attr_init(&at); |
| 1476 | pthread_attr_setstacksize(&at, (size_t)4 << 30); |
| 1477 | pthread_t th; |
| 1478 | if (pthread_create(&th, &at, run_main, NULL)) { perror("omega_ref: thread"); return 2; } |
| 1479 | pthread_join(th, NULL); |
| 1480 | fflush(stdout); |
| 1481 | _exit(result); |
| 1482 | } |