interplane.Get Omega
Read in your language

babel

aien-dev/babel

Brought by drakestapleton

branch main Branches Tags History

babel / src / 40-gen.om

595 lines · 15529 bytes Raw Blame History
1note babel 40-gen: target packaging, differential drivers and generated tests.
2note Adapter outputs are stored verbatim under units/; everything here is babel glue: re-exports
3note under the source names, drivers that read "id n" then n vectors and print one result per line,
4note and tests whose expected values are the original C function's outputs.
5global Cv ulong 1
6global Co ulong 1
7text Comma ", "
8text ArgA "a"
9text ArgX "x["
10text ArgXe "]"
11text GRs1 "[package]\nname = \""
12text GRs2 "\"\nversion = \"0.1.0\"\nedition = \"2021\"\npublish = false\n\n[lib]\npath = \"src/lib.rs\"\n\n[[bin]]\nname = \"babel_diff\"\npath = \"src/bin/babel_diff.rs\"\n"
13text GRsL "//! Generated by babel. Every `units/<name>.rs` is the verbatim output of the omega-mv\n//! `rust-export` adapter (profile S1, c17-uint64-pure-v1); this file only re-exports\n//! each adapter function `calc` under its source name. See ../REPORT.md.\n\n"
14text GRsM1 "#[path = \"units/"
15text GRsM2 ".rs\"]\npub mod "
16text GRsM3 ";\npub use "
17text GRsM4 "::calc as "
18text GRsM5 ";\n\n"
19text GRsB1 "//! Generated by babel: differential driver.\n//! stdin: \"id n\" then n vectors of decimal u64; stdout: one result per line.\nuse std::io::Read;\n\nfn take(it: &mut std::str::SplitAsciiWhitespace<'_>) -> u64 {\n it.next().expect(\"vector value\").parse().expect(\"u64\")\n}\n\nfn main() {\n let mut s = String::new();\n std::io::stdin().read_to_string(&mut s).expect(\"stdin\");\n let mut it = s.split_ascii_whitespace();\n let id: u64 = match it.next() {\n Some(t) => t.parse().expect(\"id\"),\n None => return,\n };\n let n = take(&mut it);\n let mut out = String::new();\n for _ in 0..n {\n let r: u64 = match id {\n"
20text GRsB2 " => {\n"
21text GRsB3 " let a"
22text GRsB4 " = take(&mut it);\n"
23text GRsB5 " "
24text GRsB6 "\n }\n"
25text GRsB7 " _ => panic!(\"unknown unit id {id}\"),\n };\n out.push_str(&r.to_string());\n out.push('\\n');\n }\n print!(\"{out}\");\n}\n"
26text Ind12 " "
27text Ind8 " "
28text GRsT0 "//! Generated by babel: the expected values are the outputs of the ORIGINAL C functions\n//! (compiled by cc from the input, verbatim) on the vectors in ../diff/<name>/vectors.txt.\n"
29text GRsT1 "\n#[test]\nfn "
30text GRsT2 "_matches_original_c() {\n let cases: &[(&[u64], u64)] = &[\n"
31text GRsT3 " ];\n for (a, want) in cases {\n assert_eq!("
32text GRsT4 ", *want, \"inputs {:?}\", a);\n }\n}\n"
33text GRsC1 "(&["
34text GRsC2 "], "
35text GRsC3 "),\n"
36text Dc2 "::"
37text LPar "("
38text RPar ")"
39text GPyI1 "\"\"\"Generated by babel. Each units/<name>.py is the verbatim output of the omega-mv\npython-export adapter (profile S1); this file only re-exports `calc` under the source name.\"\"\"\n\n"
40text GPyI2 "from .units."
41text GPyI3 " import calc as "
42text GPyI4 "\n__all__ = [\n"
43text GPyI5 " \""
44text GPyI6 "\",\n"
45text GPyI7 "]\n"
46text GPyU "\"\"\"Verbatim python-export adapter outputs (see ../../REPORT.md).\"\"\"\n"
47text GPyD1 "\"\"\"Generated by babel: differential driver.\n\nstdin: \"id n\" then n vectors of decimal u64; stdout: one result per line.\n\"\"\"\nimport sys\n\nimport "
48text GPyD2 "\n\nUNITS = {\n"
49text GPyD3 " "
50text GPyD4 ": ("
51text GPyD5 "),\n"
52text GPyD6 "}\n\n\ndef main():\n data = sys.stdin.read().split()\n if not data:\n return\n fn, k = UNITS[int(data[0])]\n n = int(data[1])\n pos = 2\n out = []\n for _ in range(n):\n args = [int(x) for x in data[pos:pos + k]]\n pos += k\n out.append(\"%d\\n\" % fn(*args))\n sys.stdout.write(\"\".join(out))\n\n\nmain()\n"
53text Dot "."
54text GPyT1 "\"\"\"Generated by babel: expected values are the outputs of the ORIGINAL C functions\n(compiled by cc from the input, verbatim) on the vectors in ../diff/<name>/vectors.txt.\n\"\"\"\nimport unittest\n\nimport "
55text GPyT2 "\n\n\nclass Differential(unittest.TestCase):\n"
56text GPyT3 " def test_"
57text GPyT4 "(self):\n cases = [\n"
58text GPyT5 " ]\n for args, want in cases:\n self.assertEqual("
59text GPyT6 "(*args), want, args)\n\n"
60text GPyT7 "\nif __name__ == \"__main__\":\n unittest.main()\n"
61text GPyC1 " (("
62text GPyC2 "), "
63text GPyC3 "),\n"
64text CommaO ","
65text GCh1 "/* Generated by babel: prototypes of the verbatim c-export adapter outputs in units/. */\n#ifndef BABEL_GENERATED_H\n#define BABEL_GENERATED_H\n#include <stdint.h>\n\n"
66text GCh2 "uint64_t "
67text GCh3 "("
68text GChV "void"
69text GChU "uint64_t"
70text GCh4 ");\n"
71text GCh5 "\n#endif\n"
72text GCd1 "/* Generated by babel: differential driver.\n * stdin: \"id n\" then n vectors of decimal u64; stdout: one result per line. */\n#include <stdio.h>\n#include \""
73text GCd2 ".h\"\n\nint main(void) {\n unsigned long long id, n, a[6] = {0};\n if (scanf(\"%llu %llu\", &id, &n) != 2) return 0;\n for (unsigned long long i = 0; i < n; i++) {\n uint64_t r;\n switch (id) {\n"
74text GCd3 " case "
75text GCd4 ": {\n for (int k = 0; k < "
76text GCd5 "; k++) if (scanf(\"%llu\", &a[k]) != 1) return 2;\n r = "
77text GCd6 ");\n break;\n }\n"
78text GCd7 " default: return 3;\n }\n printf(\"%llu\\n\", (unsigned long long)r);\n }\n return 0;\n}\n"
79text ArgAk "a["
80text GOr1 "/* Generated by babel: differential oracle. The function below is the ORIGINAL C source,\n * verbatim from "
81text GOr2 ", followed by a driver.\n * stdin: \"id n\" then n vectors of decimal u64; stdout: one result per line. */\n#include <stdint.h>\n#include <stdio.h>\n\n"
82text GOr3 "\n\nint main(void) {\n unsigned long long id, n, a[6] = {0};\n if (scanf(\"%llu %llu\", &id, &n) != 2) return 0;\n (void)id;\n for (unsigned long long i = 0; i < n; i++) {\n for (int k = 0; k < "
83text GOr4 "; k++) if (scanf(\"%llu\", &a[k]) != 1) return 2;\n printf(\"%llu\\n\", (unsigned long long)"
84text GOr5 "));\n }\n return 0;\n}\n"
85text GCp1 "// Generated by babel: differential driver for one verbatim cpp-export output.\n// stdin: \"id n\" then n vectors of decimal u64; stdout: one result per line.\n#include <stdio.h>\n#include \"../units/"
86text GCp2 ".cpp\"\n"
87text Colon ":"
88text Dash "-"
89text Calc "calc"
90text GZl1 "//! Generated by babel: re-exports the verbatim zig-export outputs in units/ under source names.\n"
91text GZl2 "pub const "
92text GZl3 " = @import(\"units/"
93text GZl4 ".zig\").calc;\n"
94text GZd1 "//! Generated by babel: differential driver.\n//! stdin: \"id n\" then n vectors of decimal u64; stdout: one result per line.\nconst lib = @import(\"lib.zig\");\nextern \"c\" fn scanf(fmt: [*:0]const u8, ...) c_int;\nextern \"c\" fn printf(fmt: [*:0]const u8, ...) c_int;\n\npub fn main() void {\n var id: u64 = 0;\n var n: u64 = 0;\n if (scanf(\"%llu %llu\", &id, &n) != 2) return;\n var x: [6]u64 = .{ 0, 0, 0, 0, 0, 0 };\n _ = &x;\n var i: u64 = 0;\n while (i < n) : (i += 1) {\n const r: u64 = switch (id) {\n"
95text GZd2 " => blk: {\n for (0.."
96text GZd3 ") |k| {\n if (scanf(\"%llu\", &x[k]) != 1) return;\n }\n break :blk lib."
97text GZd4 ");\n },\n"
98text GZd5 " else => return,\n };\n _ = printf(\"%llu\\n\", r);\n }\n}\n"
99text GGm1 "module "
100text GGm2 "\n\ngo 1.22\n"
101text GGd1 "// Code generated by babel. DO NOT EDIT.\n// Differential driver for the verbatim go-export output in calc.go.\n// stdin: \"id n\" then n vectors of decimal u64; stdout: one result per line.\npackage main\n\nimport (\n\t\"bufio\"\n\t\"fmt\"\n\t\"os\"\n)\n\nfunc main() {\n\tin := bufio.NewReader(os.Stdin)\n\tout := bufio.NewWriter(os.Stdout)\n\tdefer out.Flush()\n\tvar id, n uint64\n\tif _, err := fmt.Fscan(in, &id, &n); err != nil {\n\t\treturn\n\t}\n\tvar x [6]uint64\n\tfor i := uint64(0); i < n; i++ {\n\t\tfor k := 0; k < "
102text GGd2 "; k++ {\n\t\t\tif _, err := fmt.Fscan(in, &x[k]); err != nil {\n\t\t\t\tos.Exit(2)\n\t\t\t}\n\t\t}\n\t\tfmt.Fprintln(out, calc("
103text GGd3 "))\n\t}\n}\n"
104text GOm1 "\nnote babel differential driver: reads \"id n\" then n vectors of decimal ulong, prints one result per line\nglobal BbIn uchar 1048576\nglobal BbOut uchar 1048576\nglobal BbPos ulong 1\nglobal BbLen ulong 1\nglobal BbOl ulong 1\nfn babel_next to ulong\np = get BbPos 0\nn = get BbLen 0\nwhile lt p n bound 999999\nc = get BbIn p\nif ge c 48\nif le c 57\nbreak\nend\nend\np = add p 1\nend\nv = ulong 0\nwhile lt p n bound 64\nc = get BbIn p\nif lt c 48\nbreak\nend\nif gt c 57\nbreak\nend\nv = add mul v 10 ulong sub c 48\np = add p 1\nend\nput BbPos 0 p\nret v\nend\nfn babel_put v ulong to void\nd = new uchar 24 0\nk = ulong 0\nx = v\nwhile true bound 30\nput d k uchar add 48 rem x 10\nk = add k 1\nx = div x 10\nif eq x 0\nbreak\nend\nend\no = get BbOl 0\nwhile gt k 0 bound 30\nk = sub k 1\nput BbOut o get d k\no = add o 1\nend\nput BbOut o 10\nput BbOl 0 add o 1\nend\nfn main to int\nn = ulong 0\nwhile lt n len BbIn bound 999999\nr = read 0 BbIn n sub len BbIn n\nif le r 0\nbreak\nend\nn = add n ulong r\nend\nput BbLen 0 n\nix = babel_next\ncnt = babel_next\nfor j ulong cnt\n"
105text GOm2 " = babel_next\n"
106text GOm3 "r = "
107text GOm4 "babel_put r\nend\nw = write 1 BbOut 0 get BbOl 0\nret 0\nend\n"
108text Space " "
109
110note "pre0<sep>pre1..." for k arguments, with a closing text after each index
111note (omc 2c824805 passes a garbage length for an empty text, so there is no empty-post form)
112fn bargs pre view uchar * post view uchar * k ulong to void
113for i ulong k
114if gt i 0
115bs Comma
116end
117bs pre
118bu i
119bs post
120end
121end
122
123fn bargs0 pre view uchar * k ulong to void
124for i ulong k
125if gt i 0
126bs Comma
127end
128bs pre
129bu i
130end
131end
132
133fn bpkg to void
134bpl get Pg0 0 get Pgn 0
135end
136
137note write the arena from m to the file <out>/<s>; resets the arena to m
138fn wout m ulong s view uchar * to long
139c = mark
140bouts s
141p = mark
142r = wfile m c c p
143reset m
144ret r
145end
146
147note same with <out>/<s><name><t>
148fn woutn m ulong s view uchar * u ulong t view uchar * to long
149c = mark
150bouts s
151bname u
152bs t
153p = mark
154r = wfile m c c p
155reset m
156ret r
157end
158
159fn exported u ulong to bool
160ret eq get Ust u 7
161end
162
163text PCargo "rust/Cargo.toml"
164text PRsLib "rust/src/lib.rs"
165text PRsBin "rust/src/bin/babel_diff.rs"
166text PRsTst "rust/tests/differential.rs"
167
168fn genrust to void
169m = mark
170bs GRs1
171bpkg
172bs GRs2
173w = wout m PCargo
174bs GRsL
175for u ulong get Nu 0
176if exported u
177bs GRsM1
178bname u
179bs GRsM2
180bname u
181bs GRsM3
182bname u
183bs GRsM4
184bname u
185bs GRsM5
186end
187end
188w = wout m PRsLib
189bs GRsB1
190for u ulong get Nu 0
191if exported u
192k = get Uar u
193bs Ind12
194bu get Uid u
195bs GRsB2
196for i ulong k
197bs GRsB3
198bu i
199bs GRsB4
200end
201bs GRsB5
202bpkg
203bs Dc2
204bname u
205bs LPar
206bargs0 ArgA k
207bs RPar
208bs GRsB6
209end
210end
211bs GRsB7
212w = wout m PRsBin
213end
214
215
216note one generated case per vector: rust "(&[a, b], r),", python "((a, b), r),"
217fn bcases u ulong py bool to void
218put Cv 0 get Uv0 u
219put Co 0 get Uo0 u
220ve = add get Uv0 u get Uvn u
221oe = add get Uo0 u get Uon u
222skipln ve
223k = get Uar u
224for j ulong get Unv u
225bcase k py ve oe
226end
227end
228
229fn bcase k ulong py bool ve ulong oe ulong to void
230if py
231bs GPyC1
232else
233bs Ind8
234bs GRsC1
235end
236v = get Cv 0
237e = lnend v ve
238for i ulong sub e v
239c = get Pl add v i
240if eq c 32
241bs Comma
242else
243bc c
244end
245end
246if py
247if eq k 1
248bs CommaO
249end
250bs GPyC2
251else
252bs GRsC2
253end
254put Cv 0 add e 1
255o = get Co 0
256f = lnend o oe
257bpl o sub f o
258put Co 0 add f 1
259if py
260bs GPyC3
261else
262bs GRsC3
263end
264end
265
266fn lnend o ulong e ulong to ulong
267i = o
268while lt i e bound 999999
269if eq get Pl i 10
270ret i
271end
272i = add i 1
273end
274ret e
275end
276
277fn skipln e ulong to void
278put Cv 0 add lnend get Cv 0 e 1
279end
280
281note tests may only use vectors whose oracle run succeeded (Uon > 0)
282fn hascases u ulong to bool
283ret gt get Uon u 0
284end
285
286fn anycase to bool
287for u ulong get Nu 0
288if hascases u
289ret true
290end
291end
292ret false
293end
294
295fn genrstest to void
296m = mark
297bs GRsT0
298for u ulong get Nu 0
299if hascases u
300bs GRsT1
301bname u
302bs GRsT2
303bcases u false
304bs GRsT3
305bpkg
306bs Dc2
307bname u
308bs LPar
309bargs ArgAk ArgXe get Uar u
310bs RPar
311bs GRsT4
312end
313end
314w = wout m PRsTst
315end
316
317text PPyMain "python/babel_diff.py"
318text PPyTst "python/test_differential.py"
319text PInit "/__init__.py"
320text PUInit "/units/__init__.py"
321
322fn genpy to void
323m = mark
324bs GPyI1
325for u ulong get Nu 0
326if exported u
327bs GPyI2
328bname u
329bs GPyI3
330bname u
331bs NL
332end
333end
334bs GPyI4
335for u ulong get Nu 0
336if exported u
337bs GPyI5
338bname u
339bs GPyI6
340end
341end
342bs GPyI7
343pyfile m PInit
344bs GPyU
345pyfile m PUInit
346bs GPyD1
347bpkg
348bs GPyD2
349for u ulong get Nu 0
350if exported u
351bs GPyD3
352bu get Uid u
353bs GPyD4
354bpkg
355bs Dot
356bname u
357bs Comma
358bu get Uar u
359bs GPyD5
360end
361end
362bs GPyD6
363w = wout m PPyMain
364end
365
366note write the arena from m to <out>/python/<pkg><s>
367fn pyfile m ulong s view uchar * to void
368c = mark
369bouts DPy
370bpkg
371bs s
372p = mark
373r = wfile m c c p
374reset m
375end
376
377fn genpytest to void
378m = mark
379bs GPyT1
380bpkg
381bs GPyT2
382for u ulong get Nu 0
383if hascases u
384bs GPyT3
385bname u
386bs GPyT4
387bcases u true
388bs GPyT5
389bpkg
390bs Dot
391bname u
392bs GPyT6
393end
394end
395bs GPyT7
396w = wout m PPyTst
397end
398
399text PCMain "c/babel_diff.c"
400text PCDir "c/"
401text ExHdr ".h"
402
403fn genc to void
404m = mark
405bs GCh1
406for u ulong get Nu 0
407if exported u
408bs GCh2
409bpl get Ua0 u get Uan u
410bs GCh3
411k = get Uar u
412if eq k 0
413bs GChV
414end
415for i ulong k
416if gt i 0
417bs Comma
418end
419bs GChU
420end
421bs GCh4
422end
423end
424bs GCh5
425c = mark
426bouts PCDir
427bpkg
428bs ExHdr
429p = mark
430w = wfile m c c p
431reset m
432bs GCd1
433bpkg
434bs GCd2
435for u ulong get Nu 0
436if exported u
437bs GCd3
438bu get Uid u
439bs GCd4
440bu get Uar u
441bs GCd5
442bpl get Ua0 u get Uan u
443bs LPar
444bargs ArgAk ArgXe get Uar u
445bs GCd6
446end
447end
448bs GCd7
449w = wout m PCMain
450end
451
452note per-unit C++ driver cpp/diff/<name>.cpp
453text PCpD "cpp/diff/"
454fn gencpp to void
455for u ulong get Nu 0
456if exported u
457m = mark
458bs GCp1
459bname u
460bs GCp2
461bdrvmain get Uar u Calc
462w = woutn m PCpD u Ext_cp
463end
464end
465end
466
467note the shared single-function C/C++ main used by the oracle and the C++ drivers
468fn bdrvmain k ulong fname view uchar * to void
469bs GOr3
470bu k
471bs GOr4
472bs fname
473bs LPar
474bargs ArgAk ArgXe k
475bs GOr5
476end
477
478text PZLib "zig/lib.zig"
479text PZMain "zig/babel_diff.zig"
480fn genzig to void
481m = mark
482bs GZl1
483for u ulong get Nu 0
484if exported u
485bs GZl2
486bname u
487bs GZl3
488bname u
489bs GZl4
490end
491end
492w = wout m PZLib
493bs GZd1
494for u ulong get Nu 0
495if exported u
496bs Ind12
497bu get Uid u
498bs GZd2
499bu get Uar u
500bs GZd3
501bname u
502bs LPar
503bargs ArgX ArgXe get Uar u
504bs GZd4
505end
506end
507bs GZd5
508w = wout m PZMain
509end
510
511text PGoMod "go/go.mod"
512text PGoMain "/main.go"
513fn gengo to void
514m = mark
515bs GGm1
516bpkg
517bs GGm2
518w = wout m PGoMod
519for u ulong get Nu 0
520if exported u
521bs GGd1
522bu get Uar u
523bs GGd2
524bargs ArgX ArgXe get Uar u
525bs GGd3
526w = woutn m UGo u PGoMain
527end
528end
529end
530
531note diff/<name>/oracle.c: the original function text verbatim plus the shared driver
532text PDiff "diff/"
533text POrc "/oracle.c"
534text POmd "/driver.om"
535fn genoracle u ulong to void
536m = mark
537bs GOr1
538fpath get Ufi u
539bs Colon
540bu get Ul0 u
541bs Dash
542bu get Ul1 u
543bs GOr2
544bpl get Ut0 u get Utn u
545bdrvmainn u
546w = woutn m PDiff u POrc
547end
548
549fn bdrvmainn u ulong to void
550k = get Uar u
551bs GOr3
552bu k
553bs GOr4
554bname u
555bs LPar
556bargs ArgAk ArgXe k
557bs GOr5
558end
559
560fn fpath fi ulong to void
561bpl get F0 fi get Fl fi
562end
563
564note diff/<name>/driver.om: the lowered unit (omega/<name>.om) followed by a program-mode main
565fn genomdrv u ulong to bool
566m = mark
567bouts DOmega
568bname u
569bs ExOm
570e = mark
571r = rfile m e
572reset m
573if lt r 0
574ret false
575end
576bfb 0 get Fn 0
577bs GOm1
578k = get Uar u
579for i ulong k
580bs ArgA
581bu i
582bs GOm2
583end
584bs GOm3
585bpl get Uw0 u get Uwn u
586for i ulong k
587bs Space
588bs ArgA
589bu i
590end
591bs NL
592bs GOm4
593w = woutn m PDiff u POmd
594ret true
595end