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 / seed / explain / explain.om

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