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 / seed / explain / explain.om
| 1 | note explain: run the seeded compiler on one source file and, when it refuses the file, say in words why. |
| 2 | note Usage: explain OMC SRC OUT. OMC is the compiler (seed/out/omc), SRC the .om file, OUT the program |
| 3 | note file to write. The compiler's own stderr line "omc: line L: error C" is read from a pipe and shown as |
| 4 | note "SRC:L: error C: MEANING" and then the source line itself; any later lines (an om2 diag record) |
| 5 | note follow unchanged. The exit status is the compiler's own code, so scripts that test it keep working. |
| 6 | note explain adds 64 (usage), 65 (a file could not be opened or the compiler could not be started) and |
| 7 | note 66 (the compiler was stopped by a signal). |
| 8 | note The compiler and its exact diagnostic line are unchanged (gate15, gate5 and OM-2 match it byte for byte). |
| 9 | note MEANS is generated from seed/omc/error-codes.reg by seed/explain/gate.sh table: never edit it by hand; |
| 10 | note the gate fails when it differs from the registry. Line i of MEANS is omc code i (1..30). |
| 11 | om2 |
| 12 | text means "the compiler's standard input could not be read\nthe compiler's standard output could not be written\nunknown name or field, or a word this mode does not provide\nthe line ends where a word or value is needed\nextra words after a complete statement\nmalformed number or token\ntype mismatch\nnumber out of range for its type or place\nword or declaration in a place that does not allow it\nduplicate name\nbare number without a type from its context\nregister pool exhausted\nname too long (63 bytes; 8 for an abstract, a layout, a layout field, or any name in IR mode)\ncapacity exceeded (the host refused to grow the compiler's arena or its memory ceiling OMC_MEMORY_MAX was reached, value nesting past omc's own stack, the program image, or an OM-2 expansion bound)\nmissing end\nsyntax: unknown or misplaced word\nline after ret, break, continue, trap or exit in the same block\na function with a result can reach its end\nglobal passed as an argument, or one array twice in a call\nwrite to a view or a text\nconstant index outside the array\nmalformed text literal\nsize capacity\nAArch64 branch displacement out of range\nunknown target, an AArch64-only path on another target, or an x86efi refusal (bare unit, Linux host word, efi OP literal outside 0..14)\nthe compiler's environment entry OMC_MEMORY_MAX is not empty, 0 or 1 to 19 decimal digits below 2^63, or an entry is too long to read whole\nfresh element read before it is definitely written, or fresh array passed on\nabstract type: seal outside its constructor, or an unsupported holds\nexternal layout out of bounds or misplaced\ncontract syntax misplaced or not supported here (IR mode included)\n" |
| 13 | text usage "usage: explain OMC SRC OUT\n" |
| 14 | text nofile "explain: cannot open " |
| 15 | text norun "explain: the compiler could not be started\n" |
| 16 | text sigmsg "explain: the compiler was stopped by signal " |
| 17 | text oddmsg "explain: the compiler's message is not in the form omc: line L: error C\n" |
| 18 | text okmsg ": compiled, program written to " |
| 19 | text pre "omc: line " |
| 20 | text mid ": error " |
| 21 | text eofmsg "(end of file)" |
| 22 | text more " ..." |
| 23 | global pk uchar 4096 |
| 24 | global spath uchar 4096 |
| 25 | global spn ulong 1 |
| 26 | global opath uchar 4096 |
| 27 | global opn ulong 1 |
| 28 | global src uchar 8388608 |
| 29 | global srcn ulong 1 |
| 30 | global ebuf uchar 4096 |
| 31 | global en ulong 1 |
| 32 | global junk uchar 4096 |
| 33 | global obuf uchar 16384 |
| 34 | global oc ulong 1 |
| 35 | global fds uchar 8 |
| 36 | global st uchar 4 |
| 37 | global gl ulong 1 |
| 38 | global gc ulong 1 |
| 39 | global gr ulong 1 |
| 40 | |
| 41 | note ---- output: one buffer, flushed whole to a descriptor ---- |
| 42 | fn oputb b uchar to void |
| 43 | m = get oc 0 |
| 44 | if lt m len obuf |
| 45 | put obuf m b |
| 46 | put oc 0 add m 1 |
| 47 | end |
| 48 | end |
| 49 | |
| 50 | fn oputt t view uchar * to void |
| 51 | for i ulong len t |
| 52 | oputb get t i |
| 53 | end |
| 54 | end |
| 55 | |
| 56 | fn oputn v ulong to void |
| 57 | d = new uchar 20 0 |
| 58 | i = ulong 0 |
| 59 | x = v |
| 60 | put d 0 uchar add rem x 10 48 |
| 61 | x = div x 10 |
| 62 | i = add i 1 |
| 63 | while gt x 0 bound 20 |
| 64 | put d i uchar add rem x 10 48 |
| 65 | x = div x 10 |
| 66 | i = add i 1 |
| 67 | end |
| 68 | j = i |
| 69 | while gt j 0 bound 20 |
| 70 | j = sub j 1 |
| 71 | oputb get d j |
| 72 | end |
| 73 | end |
| 74 | |
| 75 | fn osrc to void |
| 76 | for i ulong get spn 0 |
| 77 | oputb get spath i |
| 78 | end |
| 79 | end |
| 80 | |
| 81 | fn oout to void |
| 82 | for i ulong get opn 0 |
| 83 | oputb get opath i |
| 84 | end |
| 85 | end |
| 86 | |
| 87 | note oflush: every pass writes at least one byte of at most len obuf, so len obuf passes suffice. |
| 88 | fn oflush fd int to void |
| 89 | off = ulong 0 |
| 90 | n = get oc 0 |
| 91 | while lt off n bound 16384 |
| 92 | w = write fd obuf off sub n off |
| 93 | if le w 0 |
| 94 | put oc 0 0 |
| 95 | ret |
| 96 | end |
| 97 | off = add off ulong w |
| 98 | end |
| 99 | put oc 0 0 |
| 100 | end |
| 101 | |
| 102 | fn quit t view uchar * code int to void |
| 103 | oputt t |
| 104 | oflush 2 |
| 105 | exit code |
| 106 | end |
| 107 | |
| 108 | note ---- arguments ---- |
| 109 | note mkpk: the exec packet for argv[1]: u16 count 1, u16 length, the path bytes (OMEGESE section 10). |
| 110 | fn mkpk to ulong |
| 111 | n = argread 1 pk 4 4092 |
| 112 | if le n 0 |
| 113 | ret 0 |
| 114 | end |
| 115 | put pk 0 1 |
| 116 | put pk 1 0 |
| 117 | put pk 2 uchar and n 255 |
| 118 | put pk 3 uchar shr n 8 |
| 119 | ret add ulong n 4 |
| 120 | end |
| 121 | |
| 122 | fn args to void |
| 123 | if ne argcnt 4 |
| 124 | quit usage 64 |
| 125 | end |
| 126 | n = argread 2 spath 0 len spath |
| 127 | if le n 0 |
| 128 | quit usage 64 |
| 129 | end |
| 130 | put spn 0 ulong n |
| 131 | n = argread 3 opath 0 len opath |
| 132 | if le n 0 |
| 133 | quit usage 64 |
| 134 | end |
| 135 | put opn 0 ulong n |
| 136 | end |
| 137 | |
| 138 | fn cantopen p ulong to void |
| 139 | oputt nofile |
| 140 | if eq p 1 |
| 141 | osrc |
| 142 | else |
| 143 | oout |
| 144 | end |
| 145 | oputb 10 |
| 146 | oflush 2 |
| 147 | exit 65 |
| 148 | end |
| 149 | |
| 150 | note ---- source text, read only to show the refused line ---- |
| 151 | note readsrc: a regular file returns every requested byte until its end, so passes stay far below the |
| 152 | note bound; a source larger than the buffer is shown only as far as it was read. |
| 153 | fn readsrc to void |
| 154 | fd = fopen spath 0 get spn 0 0 0 |
| 155 | if lt fd 0 |
| 156 | cantopen 1 |
| 157 | end |
| 158 | off = ulong 0 |
| 159 | k = long 1 |
| 160 | while and gt k 0 lt off len src bound 999999 |
| 161 | k = read int fd src off sub len src off |
| 162 | if gt k 0 |
| 163 | off = add off ulong k |
| 164 | end |
| 165 | end |
| 166 | put srcn 0 off |
| 167 | r = fclose int fd |
| 168 | end |
| 169 | |
| 170 | fn fdword o ulong to int |
| 171 | x = ulong 0 |
| 172 | for i ulong 4 |
| 173 | v = ulong get fds add o i |
| 174 | x = or x shl v mul i 8 |
| 175 | end |
| 176 | ret int x |
| 177 | end |
| 178 | |
| 179 | note ---- run the compiler: stdin SRC, stdout OUT, stderr into a pipe ---- |
| 180 | fn compile pl ulong to void |
| 181 | sfd = fopen spath 0 get spn 0 0 0 |
| 182 | if lt sfd 0 |
| 183 | cantopen 1 |
| 184 | end |
| 185 | ofd = fopen opath 0 get opn 0 577 448 |
| 186 | if lt ofd 0 |
| 187 | cantopen 2 |
| 188 | end |
| 189 | r = pipefds fds 0 len fds 524288 |
| 190 | if ne r 0 |
| 191 | quit norun 65 |
| 192 | end |
| 193 | rd = fdword 0 |
| 194 | wr = fdword 4 |
| 195 | pid = fork |
| 196 | if lt pid 0 |
| 197 | quit norun 65 |
| 198 | end |
| 199 | if eq pid 0 |
| 200 | q = dupfd int sfd 0 0 |
| 201 | q = dupfd int ofd 1 0 |
| 202 | q = dupfd wr 2 0 |
| 203 | q = execprog pk 0 pl |
| 204 | w = write int 2 norun ulong 0 len norun |
| 205 | exit int 99 |
| 206 | end |
| 207 | r = fclose int sfd |
| 208 | r = fclose int ofd |
| 209 | r = fclose wr |
| 210 | drain rd |
| 211 | r = fclose rd |
| 212 | r = waitpid int pid st 0 len st 0 |
| 213 | if ne r pid |
| 214 | quit norun 65 |
| 215 | end |
| 216 | end |
| 217 | |
| 218 | note drain: keep the first len ebuf bytes, read and drop the rest so the child never blocks on a full pipe. |
| 219 | fn drain rd int to void |
| 220 | off = ulong 0 |
| 221 | k = long 1 |
| 222 | while gt k 0 bound 999999 |
| 223 | if lt off len ebuf |
| 224 | k = read rd ebuf off sub len ebuf off |
| 225 | if gt k 0 |
| 226 | off = add off ulong k |
| 227 | end |
| 228 | else |
| 229 | k = read rd junk 0 len junk |
| 230 | end |
| 231 | end |
| 232 | put en 0 off |
| 233 | end |
| 234 | |
| 235 | note ---- read "omc: line L: error C" ---- |
| 236 | fn digits p0 ulong to ulong |
| 237 | v = ulong 0 |
| 238 | p = p0 |
| 239 | while and lt p get en 0 lt p add p0 20 bound 20 |
| 240 | c = get ebuf p |
| 241 | if or lt c 48 gt c 57 |
| 242 | break |
| 243 | end |
| 244 | v = add mul v 10 ulong sub c 48 |
| 245 | p = add p 1 |
| 246 | end |
| 247 | put gl 0 p |
| 248 | ret v |
| 249 | end |
| 250 | |
| 251 | fn hasat t view uchar * p0 ulong to bool |
| 252 | if gt add p0 len t get en 0 |
| 253 | ret false |
| 254 | end |
| 255 | for i ulong len t |
| 256 | if ne get ebuf add p0 i get t i |
| 257 | ret false |
| 258 | end |
| 259 | end |
| 260 | ret true |
| 261 | end |
| 262 | |
| 263 | fn parse to bool |
| 264 | if not hasat pre 0 |
| 265 | ret false |
| 266 | end |
| 267 | s = len pre |
| 268 | l = digits s |
| 269 | e = get gl 0 |
| 270 | if or eq e s not hasat mid e |
| 271 | ret false |
| 272 | end |
| 273 | s = add e len mid |
| 274 | c = digits s |
| 275 | e = get gl 0 |
| 276 | if eq e s |
| 277 | ret false |
| 278 | end |
| 279 | if ne e get en 0 |
| 280 | if ne get ebuf e 10 |
| 281 | ret false |
| 282 | end |
| 283 | end |
| 284 | put gr 0 add e 1 |
| 285 | put gl 0 l |
| 286 | put gc 0 c |
| 287 | ret true |
| 288 | end |
| 289 | |
| 290 | note ---- explain ---- |
| 291 | fn omean c ulong to void |
| 292 | n = ulong 1 |
| 293 | for i ulong len means |
| 294 | b = get means i |
| 295 | if eq b 10 |
| 296 | n = add n 1 |
| 297 | else |
| 298 | if eq n c |
| 299 | oputb b |
| 300 | end |
| 301 | end |
| 302 | end |
| 303 | end |
| 304 | |
| 305 | note lstart: offset of the first byte of line l (1-based), or srcn when the file has no such line. |
| 306 | fn lstart l ulong to ulong |
| 307 | n = ulong 1 |
| 308 | i = ulong 0 |
| 309 | m = get srcn 0 |
| 310 | while and lt i m lt n l bound 8388608 |
| 311 | if eq get src i 10 |
| 312 | n = add n 1 |
| 313 | end |
| 314 | i = add i 1 |
| 315 | end |
| 316 | if lt n l |
| 317 | ret m |
| 318 | end |
| 319 | ret i |
| 320 | end |
| 321 | |
| 322 | fn oline l ulong to void |
| 323 | for i ulong 4 |
| 324 | oputb 32 |
| 325 | end |
| 326 | oputn l |
| 327 | oputb 32 |
| 328 | oputb 124 |
| 329 | oputb 32 |
| 330 | s = lstart l |
| 331 | m = get srcn 0 |
| 332 | if ge s m |
| 333 | oputt eofmsg |
| 334 | oputb 10 |
| 335 | ret |
| 336 | end |
| 337 | i = s |
| 338 | while and lt i m lt sub i s 160 bound 160 |
| 339 | b = get src i |
| 340 | if eq b 10 |
| 341 | break |
| 342 | end |
| 343 | oputb b |
| 344 | i = add i 1 |
| 345 | end |
| 346 | if and lt i m ne get src i 10 |
| 347 | oputt more |
| 348 | end |
| 349 | oputb 10 |
| 350 | end |
| 351 | |
| 352 | fn isnorun to bool |
| 353 | if ne get en 0 len norun |
| 354 | ret false |
| 355 | end |
| 356 | ret hasat norun 0 |
| 357 | end |
| 358 | |
| 359 | note rest: the lines after the human line (an om2 diag record), passed through unchanged. |
| 360 | fn rest to void |
| 361 | i = get gr 0 |
| 362 | while lt i get en 0 bound 4096 |
| 363 | oputb get ebuf i |
| 364 | i = add i 1 |
| 365 | end |
| 366 | end |
| 367 | |
| 368 | fn raw to void |
| 369 | for i ulong get en 0 |
| 370 | oputb get ebuf i |
| 371 | end |
| 372 | n = get en 0 |
| 373 | if gt n 0 |
| 374 | if ne get ebuf sub n 1 10 |
| 375 | oputb 10 |
| 376 | end |
| 377 | end |
| 378 | end |
| 379 | |
| 380 | fn main to int |
| 381 | args |
| 382 | pl = mkpk |
| 383 | if eq pl 0 |
| 384 | quit usage 64 |
| 385 | end |
| 386 | readsrc |
| 387 | compile pl |
| 388 | sig = and get st 0 127 |
| 389 | if ne sig 0 |
| 390 | oputt sigmsg |
| 391 | oputn ulong sig |
| 392 | oputb 10 |
| 393 | oflush 2 |
| 394 | ret 66 |
| 395 | end |
| 396 | code = get st 1 |
| 397 | if and eq code 99 isnorun |
| 398 | quit norun 65 |
| 399 | end |
| 400 | if eq code 0 |
| 401 | raw |
| 402 | osrc |
| 403 | oputt okmsg |
| 404 | oout |
| 405 | oputb 10 |
| 406 | oflush 1 |
| 407 | ret 0 |
| 408 | end |
| 409 | if not parse |
| 410 | raw |
| 411 | oputt oddmsg |
| 412 | oflush 2 |
| 413 | ret int code |
| 414 | end |
| 415 | if or ne get gc 0 ulong code gt get gc 0 30 |
| 416 | raw |
| 417 | oputt oddmsg |
| 418 | oflush 2 |
| 419 | ret int code |
| 420 | end |
| 421 | osrc |
| 422 | oputb 58 |
| 423 | oputn get gl 0 |
| 424 | oputt mid |
| 425 | oputn get gc 0 |
| 426 | oputb 58 |
| 427 | oputb 32 |
| 428 | omean get gc 0 |
| 429 | oputb 10 |
| 430 | oline get gl 0 |
| 431 | rest |
| 432 | oflush 2 |
| 433 | ret int code |
| 434 | end |