diff --git a/PRIMITIVES.md b/PRIMITIVES.md index 450cdd4..9a6fcdf 100644 --- a/PRIMITIVES.md +++ b/PRIMITIVES.md @@ -128,8 +128,6 @@ added to them too. * Integer square root, which rounds down. * `newline` * Print a newline. - * `now` - * Get the milliseconds past the epoch. * `not` * If the supplied value is `nil` return 1, otherwise return `nil`. * `nth` @@ -186,6 +184,7 @@ added to them too. * Return the number of objects stored within our heap. + ## Standard Library The standard library consists of routines, and helpers, which are written in 100% `slisp` itself. @@ -279,6 +278,8 @@ The implementation of these primitives can be found in the file [stdlib.slisp](s * Return the list of natural numbers 1 to N. * `neg?` * Return true if the number is negative. +* `now` + * Get the milliseconds past the epoch. * `odd?` * Return 1 if the given number is odd, nil otherwise. * `one?` diff --git a/compiler/template.tmpl b/compiler/template.tmpl index 9599269..7a7e0d1 100644 --- a/compiler/template.tmpl +++ b/compiler/template.tmpl @@ -61,7 +61,6 @@ SYS_write equ 1 SYS_exit equ 60 -SYS_clock_gettime equ 228 SYS_prlimit64 equ 302 ;; stack size fetch @@ -1885,33 +1884,6 @@ FUNC fn_sys_not -;; Get milliseconds since epoch -FUNC fn_sys_now - sub rsp, 16 ; Reserve space for struct timespec - - mov eax, SYS_clock_gettime - mov edi, 0 ; CLOCK_REALTIME - mov rsi, rsp - syscall - - ; Convert to milliseconds: - ; ms = tv_sec * 1000 + tv_nsec / 1,000,000 - mov rax, [rsp] ; tv_sec - imul rax, rax, 1000 - mov r8, rax ; Save seconds in ms - mov rax, [rsp+8] ; tv_nsec - xor edx, edx - mov ecx, 1000000 - div rcx ; RAX = tv_nsec / 1,000,000 - add rax, r8 ; Total milliseconds - add rsp, 16 ; Free stack space - - ; RAX now contains milliseconds since the Unix epoch - TAG_INTEGER_REG rax - ret - - - ;; Get the Nth item from the list FUNC fn_sys_nth mov rbx,rdi ; type-check arg1: list diff --git a/inception.lisp b/inception.lisp index 84146aa..9c84a95 100644 --- a/inception.lisp +++ b/inception.lisp @@ -317,7 +317,6 @@ (register-builtin "sys_multiply" (lambda (args) (sys_multiply (car args) (cadr args)))) (register-builtin "sys_newline" (lambda (args) (sys_newline))) (register-builtin "sys_not" (lambda (args) (sys_not (car args)))) - (register-builtin "sys_now" (lambda (args) (sys_now))) (register-builtin "sys_nth!" (lambda (args) (sys_nth! (car args) (cadr args) (caddr args)))) (register-builtin "sys_nth" (lambda (args) (sys_nth (car args) (cadr args)))) (register-builtin "sys_or" (lambda (args) (sys_or (car args) (cadr args)))) diff --git a/stdlib.slisp b/stdlib.slisp index 3e2759f..58469ce 100644 --- a/stdlib.slisp +++ b/stdlib.slisp @@ -123,10 +123,6 @@ reap orphaned memory." "Return true when given NIL, otherwise return NIL" (sys_not x)) -(defun now () - "Return the number of milliseconds since the Unix EPOCH." - (sys_now)) - (defun isqrt(x) "Perform an integer square root of the given number." (sys_isqrt x)) @@ -162,10 +158,6 @@ reap orphaned memory." "Load all packages which are embedded in our run-time." (map (lambda (p) (require p)) (packages))) -(defun rmdir (str) - "Remove the given directory." - (sys_rmdir str)) - (defun split (str c) "Split a string on the given character. @@ -248,11 +240,6 @@ The first argument is the number of the function to call, after that the argumen "Invoke a kernel system-call with six arguments." (sys_syscall a b c d e f)) -(defun unlink (str) - "Delete the given file from the filesystem. - -Use (rmdir) to remove directories instead of files." - (sys_unlink str)) ;;; 1. Compatibility @@ -647,6 +634,9 @@ This returns a list of the form (TYPE SIZE MODE)." (defconst SYS_unlink 87) (defun unlink (path) + "Delete the given file from the filesystem. + +Use (rmdir) to remove directories instead of files." (syscall SYS_unlink path)) @@ -1187,6 +1177,16 @@ Uses (environment) to get all environmental variables, and filters." (getenv-loop name (cdr xs)))) nil)) +(defconst SYS_clock_gettime 228) + +(defun now () + "Return the number of milliseconds since the Unix EPOCH." + (let ((buffer (alloc 16))) + (syscall SYS_clock_gettime 0 buffer) + (let ((tv_sec (u64 buffer 0)) + (tv_nsec (u64 buffer 8))) + (+ (* tv_sec 1000) (/ tv_nsec 1000000))))) + (defun putc (c) "Write the single specified character to STDOUT."