diff --git a/PRIMITIVES.md b/PRIMITIVES.md index 297f000..2014985 100644 --- a/PRIMITIVES.md +++ b/PRIMITIVES.md @@ -162,8 +162,6 @@ added to them too. * Print the specified integer. * `printstr` * Print the given string. - * `random` - * Return a random integer between zero and N. * `split` * Split a string by the given character, and return a list of "(before after)". Return nil if the character isn't found. * `split-all` @@ -301,6 +299,8 @@ The implementation of these primitives can be found in the file [stdlib.slisp](s * Print "anything". * `println` * Print "anything" by invoking `print`, then outputting a newline. +* `random` + * Return a random integer between zero and N-1. * `range` * Return a list of numbers between the given start/end, using the specified step-size. * `read-line` diff --git a/compiler/template.tmpl b/compiler/template.tmpl index 88b8a79..b44190c 100644 --- a/compiler/template.tmpl +++ b/compiler/template.tmpl @@ -87,7 +87,6 @@ SYS_wait4 equ 61 SYS_getdents64 equ 217 SYS_clock_gettime equ 228 SYS_prlimit64 equ 302 -SYS_getrandom equ 318 ;; (entries) O_RDONLY equ 0 @@ -2395,34 +2394,6 @@ FUNC fn_sys_putc ret - -;; Generate a random number 0-N -FUNC fn_sys_random - mov rbx, rdi ; type-check arg1 - GET_TAG_BITS rbx - cmp rbx, TAG_ID_INTEGER - jne type_error - - UNTAG_REG rdi - push rdi ; push the limit - - mov rax, SYS_getrandom - lea rdi, [random_buffer] ; random value location - mov rsi, 4 ; number of bytes - xor rdx, rdx ; flags = 0 - syscall - - pop rcx ; limit the value - mov eax, [random_buffer] - xor edx, edx - div ecx - - mov rax, rdx - TAG_INTEGER_REG rax - ret - - - ;; system helper FUNC fn_sys_run mov rbx, rdi ; type check diff --git a/inception.lisp b/inception.lisp index cd68295..0de4047 100644 --- a/inception.lisp +++ b/inception.lisp @@ -330,7 +330,6 @@ (register-builtin "sys_packages" (lambda (args) (sys_packages ))) (register-builtin "sys_plus" (lambda (args) (sys_plus (car args) (cadr args)))) (register-builtin "sys_putc" (lambda (args) (sys_putc (car args)))) - (register-builtin "sys_random" (lambda (args) (sys_random (car args)))) (register-builtin "sys_run" (lambda (args) (sys_run (car args) (cadr args)))) (register-builtin "sys_split" (lambda (args) (sys_split (car args) (cadr args)))) (register-builtin "sys_sqrt" (lambda (args) (sys_sqrt (car args)))) diff --git a/stdlib.slisp b/stdlib.slisp index 5a8a0f4..75932b3 100644 --- a/stdlib.slisp +++ b/stdlib.slisp @@ -174,10 +174,6 @@ It is assumed the filehandle came from fopen, but nil is also accepted." "Write the single specified character to STDOUT." (sys_putc c)) -(defun random (n) - "Return a random number 0-N." - (sys_random n)) - ;; NOP: This is a special form in our compiler ;; However if we make this then we can pretend ;; we load packages. @@ -1035,6 +1031,15 @@ Uses (environment) to get all environmental variables, and filters." (getenv-loop name (cdr xs)))) nil)) + +(defconst SYS_getrandom 318) + +(defun random (n) + "Return a random number between zero and N-1, (both zero and N-1 are valid return values.)" + (let ((buf "skx/slisp")) + (syscall SYS_getrandom buf 8) + (% (abs (u64 buf 0)) n))) + (defun read-line () "Read and return a line of text from STDIN" (let ((text "")