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 / apps / interplane-forge / tools / bring / src / plat-macos.om
| 1 | note tools/bring/src/plat-macos.om: macOS arm64 platform for ng `@ng a64 macho` (unit begins |
| 2 | note `target macos`). Every system operation goes through libSystem with dlsym (libfn 1) and libcall |
| 3 | note (a64/DESIGN.md s13): eight integer registers, no stack arguments, so no variadic argument is ever |
| 4 | note passed. open() is called only with its two fixed arguments (no O_CREAT); files are created with |
| 5 | note creat(path, mode) (truncate) or fopen(path, "wx") + fchmod (exclusive). The process start is |
| 6 | note posix_spawn with POSIX_SPAWN_CLOEXEC_DEFAULT, stdio bound by dup2 actions and an empty |
| 7 | note environment, like execprog on Linux. Flags are Darwin values: O_NOFOLLOW 256, O_DIRECTORY |
| 8 | note 1048576, O_CLOEXEC 16777216. |
| 9 | text tmsyms "open close lstat fstat pread pwrite lseek ftruncate unlink rmdir mkdir chdir fchdir getcwd getpid usleep creat fopen fileno dup fclose fchmod fdopendir readdir closedir dirfd memcpy strlen __error _NSGetArgc _NSGetArgv _NSGetEnviron posix_spawn posix_spawn_file_actions_init posix_spawn_file_actions_adddup2 posix_spawn_file_actions_destroy posix_spawnattr_init posix_spawnattr_setflags posix_spawnattr_destroy waitpid " |
| 10 | text tmwx "wx" |
| 11 | text tmnosym "macOS library function not found" |
| 12 | global gmfn ulong 40 |
| 13 | |
| 14 | fn mp64 b edit uchar 128 o ulong v ulong to void |
| 15 | for i ulong 8 |
| 16 | put b add o i uchar and shr v mul i 8 255 |
| 17 | end |
| 18 | end |
| 19 | |
| 20 | note msym: the address of library function k (the k-th name of tmsyms), found once with dlsym |
| 21 | fn msym k ulong to ulong |
| 22 | f = get gmfn k |
| 23 | if ne f 0 |
| 24 | ret f |
| 25 | end |
| 26 | nm = new uchar 64 0 |
| 27 | rg = new uchar 128 0 |
| 28 | w = ulong 0 |
| 29 | s = ulong 0 |
| 30 | for i ulong len tmsyms |
| 31 | if eq get tmsyms i 32 |
| 32 | if eq w k |
| 33 | for j ulong sub i s bound 63 |
| 34 | put nm j get tmsyms add s j |
| 35 | end |
| 36 | put nm sub i s 0 |
| 37 | mp64 rg 0 18446744073709551614 |
| 38 | mp64 rg 8 ulong libptr nm 0 64 |
| 39 | f = ulong libcall ulong libfn 1 rg 0 128 |
| 40 | if eq f 0 |
| 41 | die tmnosym |
| 42 | end |
| 43 | put gmfn k f |
| 44 | ret f |
| 45 | end |
| 46 | w = add w 1 |
| 47 | s = add i 1 |
| 48 | end |
| 49 | end |
| 50 | die tmnosym |
| 51 | ret 0 |
| 52 | end |
| 53 | |
| 54 | fn mc3 k ulong a ulong b ulong c ulong to long |
| 55 | rg = new uchar 128 0 |
| 56 | mp64 rg 0 a |
| 57 | mp64 rg 8 b |
| 58 | mp64 rg 16 c |
| 59 | ret long libcall msym k rg 0 128 |
| 60 | end |
| 61 | |
| 62 | fn mc6 k ulong a ulong b ulong c ulong d ulong e ulong to long |
| 63 | rg = new uchar 128 0 |
| 64 | mp64 rg 0 a |
| 65 | mp64 rg 8 b |
| 66 | mp64 rg 16 c |
| 67 | mp64 rg 24 d |
| 68 | mp64 rg 32 e |
| 69 | ret long libcall msym k rg 0 128 |
| 70 | end |
| 71 | |
| 72 | note merr: -errno (Darwin numbering; EEXIST is 17 as on Linux) |
| 73 | fn merr to long |
| 74 | p = mc3 28 0 0 0 |
| 75 | b = new uchar 8 0 |
| 76 | x = mc3 26 ulong libptr b 0 8 ulong p 4 |
| 77 | ret neg long rd32 b 0 |
| 78 | end |
| 79 | |
| 80 | note mres: a libc result: -1 becomes -errno |
| 81 | fn mres r long to long |
| 82 | if eq int r -1 |
| 83 | ret merr |
| 84 | end |
| 85 | ret r |
| 86 | end |
| 87 | |
| 88 | note mpath: p NUL-terminated in c; returns its address |
| 89 | fn mpath p view uchar * c edit uchar 4097 to ulong |
| 90 | if gt len p 4095 |
| 91 | die terrio |
| 92 | end |
| 93 | for i ulong len p bound 4096 |
| 94 | if eq get p i 0 |
| 95 | die terrio |
| 96 | end |
| 97 | put c i get p i |
| 98 | end |
| 99 | put c len p 0 |
| 100 | ret ulong libptr c 0 4097 |
| 101 | end |
| 102 | |
| 103 | fn pl_open p view uchar * how ulong mode int to long |
| 104 | c = new uchar 4097 0 |
| 105 | a = mpath p c |
| 106 | if eq how 2 |
| 107 | ret mres mc3 16 a ulong mode 0 |
| 108 | end |
| 109 | if eq how 3 |
| 110 | ret mexcl a mode |
| 111 | end |
| 112 | fl = ulong 16777472 |
| 113 | if eq how 1 |
| 114 | fl = ulong 17826048 |
| 115 | end |
| 116 | if eq how 4 |
| 117 | fl = ulong 16777216 |
| 118 | end |
| 119 | ret mres mc3 0 a fl 0 |
| 120 | end |
| 121 | fn mexcl a ulong mode int to long |
| 122 | m = new uchar 4 0 |
| 123 | for i ulong 2 |
| 124 | put m i get tmwx i |
| 125 | end |
| 126 | f = mc3 17 a ulong libptr m 0 4 0 |
| 127 | if eq f 0 |
| 128 | ret merr |
| 129 | end |
| 130 | fd = mc3 18 ulong f 0 0 |
| 131 | nfd = mres mc3 19 ulong fd 0 0 |
| 132 | x = mc3 20 ulong f 0 0 |
| 133 | if lt nfd 0 |
| 134 | ret nfd |
| 135 | end |
| 136 | x = mc3 21 ulong nfd ulong mode 0 |
| 137 | ret nfd |
| 138 | end |
| 139 | fn pl_close fd int to void |
| 140 | x = mc3 1 ulong fd 0 0 |
| 141 | end |
| 142 | fn pl_unlink p view uchar * to long |
| 143 | c = new uchar 4097 0 |
| 144 | ret mres mc3 8 mpath p c 0 0 |
| 145 | end |
| 146 | fn pl_rmdir p view uchar * to long |
| 147 | c = new uchar 4097 0 |
| 148 | ret mres mc3 9 mpath p c 0 0 |
| 149 | end |
| 150 | fn pl_mkdir p view uchar * mode int to long |
| 151 | c = new uchar 4097 0 |
| 152 | ret mres mc3 10 mpath p c ulong mode 0 |
| 153 | end |
| 154 | fn pl_cd p view uchar * to long |
| 155 | c = new uchar 4097 0 |
| 156 | ret mres mc3 11 mpath p c 0 0 |
| 157 | end |
| 158 | fn pl_fcd fd int to long |
| 159 | ret mres mc3 12 ulong fd 0 0 |
| 160 | end |
| 161 | note directories: the handle is a DIR* (fdopendir of an O_DIRECTORY|O_NOFOLLOW descriptor), 0 on error |
| 162 | fn pl_dopen p view uchar * to ulong |
| 163 | fd = pl_open p 1 0 |
| 164 | if lt fd 0 |
| 165 | ret 0 |
| 166 | end |
| 167 | d = mc3 22 ulong fd 0 0 |
| 168 | if eq d 0 |
| 169 | pl_close int fd |
| 170 | end |
| 171 | ret ulong d |
| 172 | end |
| 173 | fn pl_dfd h ulong to int |
| 174 | ret int mc3 25 h 0 0 |
| 175 | end |
| 176 | fn pl_dclose h ulong to void |
| 177 | x = mc3 24 h 0 0 |
| 178 | end |
| 179 | fn pl_dname to ulong |
| 180 | ret 19 |
| 181 | end |
| 182 | fn pl_dlen b view uchar * o ulong to ulong |
| 183 | ret rd16 b add o 16 |
| 184 | end |
| 185 | note pl_dents: readdir records into b as the walk reads them: u16 record length at 16, the name at 19, |
| 186 | note a zero byte after it. A record is at most 275 bytes, so b always takes the next one. |
| 187 | fn pl_dents h ulong b edit uchar * to long |
| 188 | n = ulong 0 |
| 189 | hd = new uchar 24 0 |
| 190 | while le add n 280 len b bound 9223372036854775807 |
| 191 | p = mc3 23 h 0 0 |
| 192 | if eq p 0 |
| 193 | break |
| 194 | end |
| 195 | x = mc3 26 ulong libptr hd 0 24 ulong p 24 |
| 196 | nl = rd16 hd 18 |
| 197 | if gt nl 255 |
| 198 | ret -36 |
| 199 | end |
| 200 | rl = add add 19 nl 1 |
| 201 | for i ulong 19 |
| 202 | put b add n i 0 |
| 203 | end |
| 204 | wr16 b add n 16 rl |
| 205 | x = mc3 26 ulong libptr b add n 19 nl add ulong p 21 nl |
| 206 | put b add add n 19 nl 0 |
| 207 | n = add n rl |
| 208 | end |
| 209 | ret long n |
| 210 | end |
| 211 | fn pl_stat p view uchar * b edit uchar * to long |
| 212 | c = new uchar 4097 0 |
| 213 | if lt len b 144 |
| 214 | die terrio |
| 215 | end |
| 216 | ret mres mc3 2 mpath p c ulong libptr b 0 144 0 |
| 217 | end |
| 218 | fn pl_fstat fd int b edit uchar * to long |
| 219 | if lt len b 144 |
| 220 | die terrio |
| 221 | end |
| 222 | ret mres mc3 3 ulong fd ulong libptr b 0 144 0 |
| 223 | end |
| 224 | fn pl_mode b view uchar * to ulong |
| 225 | ret rd16 b 4 |
| 226 | end |
| 227 | fn pl_size b view uchar * to ulong |
| 228 | ret rd64 b 96 |
| 229 | end |
| 230 | fn pl_readat fd int b edit uchar * pos ulong to long |
| 231 | if eq len b 0 |
| 232 | ret 0 |
| 233 | end |
| 234 | ret mres mc6 4 ulong fd ulong libptr b 0 len b len b pos 0 |
| 235 | end |
| 236 | fn pl_seek fd int pos ulong to long |
| 237 | ret mres mc3 6 ulong fd pos 0 |
| 238 | end |
| 239 | fn pl_skip fd int n ulong to long |
| 240 | ret mres mc3 6 ulong fd n 1 |
| 241 | end |
| 242 | fn pl_trunc fd int n ulong to long |
| 243 | ret mres mc3 7 ulong fd n 0 |
| 244 | end |
| 245 | fn pl_cwd o edit uchar * to ulong |
| 246 | r = mc3 13 ulong libptr o 0 len o len o 0 |
| 247 | if eq r 0 |
| 248 | die terrio |
| 249 | end |
| 250 | for i ulong len o bound 9223372036854775807 |
| 251 | if eq get o i 0 |
| 252 | ret i |
| 253 | end |
| 254 | end |
| 255 | die terrio |
| 256 | ret 0 |
| 257 | end |
| 258 | fn pl_pid to long |
| 259 | ret mc3 14 0 0 0 |
| 260 | end |
| 261 | fn pl_sleep to void |
| 262 | x = mc3 15 1000000 0 0 |
| 263 | end |
| 264 | note mptr: the 8-byte value at address a |
| 265 | fn mptr a ulong to ulong |
| 266 | b = new uchar 8 0 |
| 267 | x = mc3 26 ulong libptr b 0 8 a 8 |
| 268 | ret rd64 b 0 |
| 269 | end |
| 270 | note mstr: the C string at address a into b; its length, or -105 when b is too short |
| 271 | fn mstr a ulong b edit uchar * to long |
| 272 | n = ulong mc3 27 a 0 0 |
| 273 | if gt n len b |
| 274 | ret -105 |
| 275 | end |
| 276 | if eq n 0 |
| 277 | ret 0 |
| 278 | end |
| 279 | x = mc3 26 ulong libptr b 0 n a n |
| 280 | ret long n |
| 281 | end |
| 282 | fn pl_argc to long |
| 283 | p = mc3 29 0 0 0 |
| 284 | b = new uchar 8 0 |
| 285 | x = mc3 26 ulong libptr b 0 8 ulong p 4 |
| 286 | ret long rd32 b 0 |
| 287 | end |
| 288 | fn pl_arg i ulong b edit uchar * to long |
| 289 | if ge i ulong pl_argc |
| 290 | ret -1 |
| 291 | end |
| 292 | av = mptr ulong mc3 30 0 0 0 |
| 293 | ret mstr mptr add av mul i 8 b |
| 294 | end |
| 295 | fn pl_envc to long |
| 296 | ev = mptr ulong mc3 31 0 0 0 |
| 297 | n = ulong 0 |
| 298 | while ne mptr add ev mul n 8 0 bound 9223372036854775807 |
| 299 | n = add n 1 |
| 300 | end |
| 301 | ret long n |
| 302 | end |
| 303 | fn pl_env i ulong b edit uchar * to long |
| 304 | ev = mptr ulong mc3 31 0 0 0 |
| 305 | ret mstr mptr add ev mul i 8 b |
| 306 | end |
| 307 | note pl_run: posix_spawn the packed list a (as execprog) with stdin/stdout/stderr bound to the |
| 308 | note three descriptors, every other descriptor closed, an empty environment; waits for it |
| 309 | fn pl_run a view uchar * inf int outf int errf int to long |
| 310 | sb = new uchar 4160 0 |
| 311 | pv = new uchar 528 0 |
| 312 | cnt = rd16 a 0 |
| 313 | if or eq cnt 0 gt cnt 64 |
| 314 | ret -1 |
| 315 | end |
| 316 | base = ulong libptr sb 0 4160 |
| 317 | o = ulong 2 |
| 318 | w = ulong 0 |
| 319 | for k ulong cnt bound 64 |
| 320 | l = rd16 a o |
| 321 | for j ulong l bound 4096 |
| 322 | put sb add w j get a add add o 2 j |
| 323 | end |
| 324 | put sb add w l 0 |
| 325 | wr64 pv mul k 8 add base w |
| 326 | w = add add w l 1 |
| 327 | o = add add o 2 l |
| 328 | end |
| 329 | wr64 pv mul cnt 8 0 |
| 330 | wr64 pv 520 0 |
| 331 | ret mspawn sb pv cnt inf outf errf |
| 332 | end |
| 333 | fn mspawn sb edit uchar 4160 pv edit uchar 528 cnt ulong inf int outf int errf int to long |
| 334 | fa = new uchar 64 0 |
| 335 | atr = new uchar 64 0 |
| 336 | pid = new uchar 8 0 |
| 337 | st = new uchar 8 0 |
| 338 | fap = ulong libptr fa 0 64 |
| 339 | atp = ulong libptr atr 0 64 |
| 340 | x = mc3 33 fap 0 0 |
| 341 | x = mc3 34 fap ulong inf 0 |
| 342 | x = mc3 34 fap ulong outf 1 |
| 343 | x = mc3 34 fap ulong errf 2 |
| 344 | x = mc3 36 atp 0 0 |
| 345 | x = mc3 37 atp 16384 0 |
| 346 | pp = ulong libptr pv 0 528 |
| 347 | rg = new uchar 128 0 |
| 348 | mp64 rg 0 ulong libptr pid 0 8 |
| 349 | mp64 rg 8 ulong libptr sb 0 4160 |
| 350 | mp64 rg 16 fap |
| 351 | mp64 rg 24 atp |
| 352 | mp64 rg 32 pp |
| 353 | mp64 rg 40 add pp mul cnt 8 |
| 354 | r = long libcall msym 32 rg 0 128 |
| 355 | x = mc3 35 fap 0 0 |
| 356 | x = mc3 38 atp 0 0 |
| 357 | if ne r 0 |
| 358 | ret -1 |
| 359 | end |
| 360 | p = rd32 pid 0 |
| 361 | while true bound 9223372036854775807 |
| 362 | w = mc3 39 p ulong libptr st 0 8 0 |
| 363 | if eq int w -1 |
| 364 | if eq merr -4 |
| 365 | continue |
| 366 | end |
| 367 | ret -1 |
| 368 | end |
| 369 | break |
| 370 | end |
| 371 | v = rd32 st 0 |
| 372 | if ne and v 127 0 |
| 373 | ret -1 |
| 374 | end |
| 375 | ret long and shr v 8 255 |
| 376 | end |