https://github.com/akkartik/mu/blob/main/linux/402time.mu
 1 type timespec {
 2   tv_sec: int
 3   tv_nsec: int
 4 }
 5 
 6 # return time in seconds since epoch
 7 # TODO: y2038
 8 fn time -> _/eax: int {
 9   var t: timespec
10   var clock/ebx: int <- copy 0/CLOCK_MONOTONIC
11   var t-addr/ecx: (addr timespec) <- address t
12   syscall_clock_gettime
13   var t-secs-addr/ecx: (addr int) <- get t-addr, tv_sec
14   var secs/eax: int <- copy *t-secs-addr
15   return secs
16 }
17 
18 # return time in nanoseconds since epoch
19 fn ntime -> _/eax: int {
20   var t: timespec
21   var clock/ebx: int <- copy 0/CLOCK_MONOTONIC
22   var t-addr/ecx: (addr timespec) <- address t
23   syscall_clock_gettime
24   var t-nsecs-addr/ecx: (addr int) <- get t-addr, tv_nsec
25   var nsecs/eax: int <- copy *t-nsecs-addr
26   return nsecs
27 }
28 
29 # nsecs must be less than 999999999 or 0x3b9ac9ff nanoseconds
30 fn sleep secs: int, nsecs: int {
31   var t: timespec
32   # initialize t
33   var tmp/eax: (addr int) <- get t, tv_sec
34   var tmp2/ecx: int <- copy secs
35   copy-to *tmp, tmp2
36   tmp <- get t, tv_nsec
37   tmp2 <- copy nsecs
38   copy-to *tmp, tmp2
39   # perform the syscall
40   var t-addr/ebx: (addr timespec) <- address t
41   var rem-addr/ecx: (addr timespec) <- copy 0
42   syscall_nanosleep
43 }