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