interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / apps / interplane-forge / tools / bring / src / plat-macos.om

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