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 / om / tls / test / acvp / acvpio.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note acvpio.om: record reader and output writer shared by the ACVP drivers (acvp.om, mlkemacvp.om). |
| 3 | note Composition: acvpio.om, then the driver (which defines drrec a b c), then acvpmain.om (fn main). |
| 4 | note No library calls here. |
| 5 | |
| 6 | global inp uchar 16777216 |
| 7 | global outb uchar 1048576 |
| 8 | global st ulong 16 |
| 9 | global one uchar 1 |
| 10 | text hexd "0123456789ABCDEF" |
| 11 | text tbad "BAD" |
| 12 | text tio "acvp: cannot read input or input over 16 MiB\n" |
| 13 | |
| 14 | note st: 0 input length, 1 cursor, 2 line number, 3 malformed flag, 4 line start, 5 records, |
| 15 | note 6 malformed records, 7 output fill |
| 16 | |
| 17 | fn drslurp to int |
| 18 | t = ulong 0 |
| 19 | while true bound 999999 |
| 20 | room = sub len inp t |
| 21 | if eq room 0 |
| 22 | r = read 0 one 0 1 |
| 23 | if ne r 0 |
| 24 | ret 2 |
| 25 | end |
| 26 | break |
| 27 | end |
| 28 | r = read 0 inp t room |
| 29 | if lt r 0 |
| 30 | ret 2 |
| 31 | end |
| 32 | if eq r 0 |
| 33 | break |
| 34 | end |
| 35 | t = add t ulong r |
| 36 | end |
| 37 | put st 0 t |
| 38 | ret 0 |
| 39 | end |
| 40 | |
| 41 | fn oflush to void |
| 42 | n = get st 7 |
| 43 | o = ulong 0 |
| 44 | while lt o n bound 999999 |
| 45 | r = write 1 outb o sub n o |
| 46 | if le r 0 |
| 47 | break |
| 48 | end |
| 49 | o = add o ulong r |
| 50 | end |
| 51 | put st 7 0 |
| 52 | end |
| 53 | |
| 54 | fn obyte c ulong to void |
| 55 | if ge get st 7 len outb |
| 56 | oflush |
| 57 | end |
| 58 | put outb get st 7 uchar c |
| 59 | put st 7 add get st 7 1 |
| 60 | end |
| 61 | |
| 62 | fn ohex a view uchar * to void |
| 63 | obyte 32 |
| 64 | if eq len a 0 |
| 65 | obyte 45 |
| 66 | ret |
| 67 | end |
| 68 | for i ulong len a |
| 69 | v = ulong get a i |
| 70 | obyte ulong get hexd shr v 4 |
| 71 | obyte ulong get hexd and v 15 |
| 72 | end |
| 73 | end |
| 74 | |
| 75 | fn odec v ulong to void |
| 76 | b = new uchar 24 0 |
| 77 | x = v |
| 78 | k = ulong 0 |
| 79 | while true bound 21 |
| 80 | put b sub 23 k uchar add 48 rem x 10 |
| 81 | k = add k 1 |
| 82 | x = div x 10 |
| 83 | if eq x 0 |
| 84 | break |
| 85 | end |
| 86 | end |
| 87 | for i ulong k |
| 88 | obyte ulong get b add sub 24 k i |
| 89 | end |
| 90 | end |
| 91 | |
| 92 | fn ofld v ulong to void |
| 93 | obyte 32 |
| 94 | odec v |
| 95 | end |
| 96 | |
| 97 | fn drpeek to ulong |
| 98 | c = get st 1 |
| 99 | if ge c get st 0 |
| 100 | ret 256 |
| 101 | end |
| 102 | ret ulong get inp c |
| 103 | end |
| 104 | |
| 105 | fn dradv to void |
| 106 | put st 1 add get st 1 1 |
| 107 | end |
| 108 | |
| 109 | fn drbad to void |
| 110 | put st 3 1 |
| 111 | end |
| 112 | |
| 113 | fn isbad to bool |
| 114 | ret eq get st 3 1 |
| 115 | end |
| 116 | |
| 117 | fn drhexv c ulong to ulong |
| 118 | if ge c 48 |
| 119 | if le c 57 |
| 120 | ret sub c 48 |
| 121 | end |
| 122 | end |
| 123 | if ge c 97 |
| 124 | if le c 102 |
| 125 | ret sub c 87 |
| 126 | end |
| 127 | end |
| 128 | if ge c 65 |
| 129 | if le c 70 |
| 130 | ret sub c 55 |
| 131 | end |
| 132 | end |
| 133 | ret 16 |
| 134 | end |
| 135 | |
| 136 | fn drbyte to uchar |
| 137 | hi = drhexv drpeek |
| 138 | dradv |
| 139 | lo = drhexv drpeek |
| 140 | dradv |
| 141 | if gt hi 15 |
| 142 | drbad |
| 143 | ret 0 |
| 144 | end |
| 145 | if gt lo 15 |
| 146 | drbad |
| 147 | ret 0 |
| 148 | end |
| 149 | ret uchar or shl hi 4 lo |
| 150 | end |
| 151 | |
| 152 | fn drsp to void |
| 153 | if ne drpeek 32 |
| 154 | drbad |
| 155 | ret |
| 156 | end |
| 157 | dradv |
| 158 | end |
| 159 | |
| 160 | note a decimal number (at most 19 digits) after one space |
| 161 | fn drnum to ulong |
| 162 | drsp |
| 163 | v = ulong 0 |
| 164 | k = ulong 0 |
| 165 | while true bound 21 |
| 166 | c = drpeek |
| 167 | if lt c 48 |
| 168 | break |
| 169 | end |
| 170 | if gt c 57 |
| 171 | break |
| 172 | end |
| 173 | v = add mul v 10 sub c 48 |
| 174 | k = add k 1 |
| 175 | dradv |
| 176 | end |
| 177 | if eq k 0 |
| 178 | drbad |
| 179 | end |
| 180 | if gt k 19 |
| 181 | drbad |
| 182 | end |
| 183 | ret v |
| 184 | end |
| 185 | |
| 186 | note one space, then hex bytes into a[0..n) (or "-"); returns n |
| 187 | fn drhex a edit uchar * to ulong |
| 188 | drsp |
| 189 | if eq drpeek 45 |
| 190 | dradv |
| 191 | ret 0 |
| 192 | end |
| 193 | n = ulong 0 |
| 194 | while true bound 999999 |
| 195 | if gt drhexv drpeek 15 |
| 196 | break |
| 197 | end |
| 198 | if ge n len a |
| 199 | drbad |
| 200 | ret n |
| 201 | end |
| 202 | put a n drbyte |
| 203 | n = add n 1 |
| 204 | end |
| 205 | if eq n 0 |
| 206 | drbad |
| 207 | end |
| 208 | ret n |
| 209 | end |
| 210 | |
| 211 | fn dreor to void |
| 212 | if eq drpeek 13 |
| 213 | dradv |
| 214 | end |
| 215 | c = drpeek |
| 216 | if eq c 10 |
| 217 | dradv |
| 218 | ret |
| 219 | end |
| 220 | if ne c 256 |
| 221 | drbad |
| 222 | end |
| 223 | end |
| 224 | |
| 225 | fn drskip to void |
| 226 | while true bound 999999 |
| 227 | c = drpeek |
| 228 | if eq c 256 |
| 229 | ret |
| 230 | end |
| 231 | dradv |
| 232 | if eq c 10 |
| 233 | ret |
| 234 | end |
| 235 | end |
| 236 | end |
| 237 | |
| 238 | note the record's ID: printed first, kept in st 8 for the BAD line |
| 239 | fn drid to void |
| 240 | id = drnum |
| 241 | put st 8 id |
| 242 | odec id |
| 243 | end |
| 244 |