interplane.Get Omega
Read in your language

pageunit

Project brought through Interplane

Brought by drakestapleton

branch main Branches Tags History

pageunit / enc.om

208 lines · 5459 bytes Raw Blame History
1om2
2note enc: page text on stdin becomes an om2 unit on stdout
3note the emitted unit holds one text record per input line and its main writes the same bytes back
4note exit 0 ok, 2 read error, 3 input of 1000000 bytes or more, 4 control byte refused (below 32 other
5note than tab and newline, or 127), 5 over 8000 lines, 6 write error
6note out is a reserved word in om2 units so the output pool is ob
7note pinned compiler: omc sha256 f4fa7951 (omega-mv b2ad967e seed/out/omc); omc 2c824805 refuses
8note this unit (error 8 on the bound 1000000 of slurp). See tools/pageunit/omc.sh.
9note
10note Why 8000 lines and 1000000 bytes (stated 2026-11-11; both are consequences of the compiler pin,
11note not choices, and are to be lifted when the compiler lifts them):
12note - lines: omc f4fa7951 holds the texts of a unit and the locals of the function being compiled
13note in one table of 8192 names (8192 texts and a bare main compile, 8193 are error 14). It also
14note refuses an om2 function whose emitted code reaches 262144 bytes (error 14; an emit call with a
15note text argument takes 40 bytes, so one function holds 6552 of them) and a function with more
16note than 8192 trap sites. So the unit puts at most 1000 calls in each part function p00 .. p07 and
17note main calls the parts; with emit's 4 locals the text count may reach 8188, and enc stops at
18note 8000 (exit 5) so every unit it writes compiles.
19note - bytes: the pin has no heap, so enc's input pool inp and output pool ob are globals of fixed
20note size chosen at compile time; enc refuses 1000000 bytes or more (exit 3) instead of overrunning.
21note tests/pageunit/run.sh alarms when a real page reaches 80 percent of either number (6400 lines or
22note 800000 bytes), so a page that approaches a refusal is noticed before it happens.
23note
24note Output bound (no input traps or overflows ob). Let n < 1000000 be the input bytes and R <= 8000
25note the records, P = ceil(R / 1000) <= 8 the parts. Each record costs 14 bytes around its body
26note (text t, 4 digits, space quote, quote newline) and 11 bytes for its call (emit t, 4 digits,
27note newline); every input byte becomes at most 2 body bytes (quote, backslash, tab and newline are
28note escaped as 2, every other accepted byte is copied). Each part costs 15 bytes for its fn line,
29note 4 for its end and 4 for its call in main. So
30note size <= C + 2 n + 25 R + 23 P <= C + 1999998 + 200000 + 184 = C + 2200182
31note where C = |HEAD| + |EMITA| + |EMITB| + |MAINH| + |TAIL| = 139 + 80 + 71 + 15 + 10 = 315 bytes,
32note so size <= 2200497 < 4194304 = |ob|. The input made only of quotes and newlines with 8000
33note records reaches the bound exactly (gen w in tests/pageunit: 2200497 bytes).
34note Loop bounds: slurp reads at least one byte per pass until it reads 0, at most 1000000 bytes,
35note so at most 1000001 passes; flush writes at least one byte per pass of at most |ob| bytes.
36global inp uchar 1048576
37global ob uchar 4194304
38global st ulong 1
39text HEAD "om2\nnote cortex page unit generated by tools/pageunit/enc.om\nnote compile with omc f4fa7951 and run: stdout is the page text byte for byte\n"
40text TXT "text t"
41text QOPEN " \""
42text QEND "\"\n"
43text EMITA "fn emit a view uchar * to void\nn = len a\no = ulong 0\nwhile lt o n bound 1000000\n"
44text EMITB "k = write 1 a o sub n o\nif le k 0\nexit 1\nend\no = add o ulong k\nend\nend\n"
45text PARTA "fn p"
46text PARTB " to void\n"
47text PEND "end\n"
48text CALLP "p"
49text MAINH "fn main to int\n"
50text CALL "emit t"
51text NL "\n"
52text TAIL "ret 0\nend\n"
53fn putb c uchar to void
54p = get st 0
55put ob p c
56put st 0 add p 1
57end
58fn puts a view uchar * to void
59n = len a
60i = ulong 0
61while lt i n bound 4096
62putb get a i
63i = add i 1
64end
65end
66fn digit v ulong d ulong to uchar
67q = rem div v d 10
68ret uchar add q 48
69end
70fn putnum v ulong to void
71putb digit v 1000
72putb digit v 100
73putb digit v 10
74putb digit v 1
75end
76fn putpart v ulong to void
77putb digit v 10
78putb digit v 1
79end
80fn slurp to long
81note every pass reads at least one byte or ends the loop; at most 1000000 bytes are accepted
82n = ulong 0
83go = ulong 1
84err = long 0
85while eq go 1 bound 1000001
86k = read 0 inp n sub 1000000 n
87if lt k 0
88err = k
89go = ulong 0
90else
91if eq k 0
92go = ulong 0
93else
94n = add n ulong k
95end
96end
97end
98if lt err 0
99ret err
100end
101ret long n
102end
103fn flush to int
104n = get st 0
105o = ulong 0
106while lt o n bound 4194304
107k = write 1 ob o sub n o
108if le k 0
109ret 6
110end
111o = add o ulong k
112end
113ret 0
114end
115fn calls cnt ulong to void
116note part functions of at most 1000 emit calls each, then main calling every part
117j = ulong 0
118pn = ulong 0
119while lt j cnt bound 8000
120if eq rem j 1000 0
121puts PARTA
122putpart pn
123puts PARTB
124end
125puts CALL
126putnum j
127puts NL
128j = add j 1
129if eq rem j 1000 0
130puts PEND
131pn = add pn 1
132end
133end
134if ne rem cnt 1000 0
135puts PEND
136pn = add pn 1
137end
138puts MAINH
139k = ulong 0
140while lt k pn bound 8
141puts CALLP
142putpart k
143puts NL
144k = add k 1
145end
146puts TAIL
147end
148fn main to int
149got = slurp
150if lt got 0
151ret 2
152end
153n = ulong got
154if ge n 1000000
155ret 3
156end
157puts HEAD
158i = ulong 0
159cnt = ulong 0
160opn = ulong 0
161while lt i n bound 1000000
162c = get inp i
163if eq opn 0
164if ge cnt 8000
165ret 5
166end
167puts TXT
168putnum cnt
169puts QOPEN
170opn = ulong 1
171end
172if eq c 10
173putb 92
174putb 110
175puts QEND
176opn = ulong 0
177cnt = add cnt 1
178else
179if eq c 9
180putb 92
181putb 116
182else
183if lt c 32
184ret 4
185end
186if eq c 127
187ret 4
188end
189if eq c 34
190putb 92
191end
192if eq c 92
193putb 92
194end
195putb c
196end
197end
198i = add i 1
199end
200if eq opn 1
201puts QEND
202cnt = add cnt 1
203end
204puts EMITA
205puts EMITB
206calls cnt
207ret flush
208end