Primus Lisp Documentation

Table of Contents

1 Packages

2 Package bap

2.1 Constants

2.1.1 false

;; File "init.lisp", line 29, characters 0-55
(defconstant false 0:1 "false is another name for 0:1")

2.1.2 nil

;; File "init.lisp", line 30, characters 0-55
(defconstant nil false "nil is another name for false")

2.1.3 true

;; File "init.lisp", line 28, characters 0-54
(defconstant true 1:1  "true is another name for 1:1")

2.2 Functions

2.2.1 abort

;; File "stdlib.lisp", line 23, characters 0-103
(defun abort ()
  "terminates program with exit code 1"
  (declare (external "abort"))
  (exit-with 1))

2.2.2 abs

;; File "stdlib.lisp", line 38, characters 0-77
(defun abs (x)
  (declare (external "abs"))
  (if (is-negative x) (neg x) x))

2.2.3 ascii-is-alpha

;; File "ascii.lisp", line 31, characters 0-88
(defun ascii-is-alpha (c)
  (declare (external "isalpha"))
  (< (- (logor c 32) ?a) 26))

2.2.4 ascii-is-alphanum

;; File "ascii.lisp", line 26, characters 0-113
(defun ascii-is-alphanum (c)
  (declare (external "isalnum"))
  (or (ascii-is-alpha c)
      (ascii-is-digit c)))

2.2.5 ascii-is-digit

;; File "ascii.lisp", line 21, characters 0-157
(defun ascii-is-digit (s)
  "(ascii-is-digit s) is true if S is an ascii representation of decimal digit"
  (declare (external "isdigit"))
  (< (- s ?0) 10))

2.2.6 ascii-is-lower

;; File "ascii.lisp", line 39, characters 0-77
(defun ascii-is-lower (c)
  (declare (external "islower"))
  (< (- c ?a) 26))

2.2.7 ascii-is-special

;; File "ascii.lisp", line 3, characters 0-104
(defun ascii-is-special (s)
  "(ascii-special S) is true if S is an ascii special character"
  (< s 32))

2.2.8 ascii-is-upper

;; File "ascii.lisp", line 35, characters 0-77
(defun ascii-is-upper (c)
  (declare (external "isupper"))
  (< (- c ?A) 26))

2.2.9 ascii-is-whitespace

;; File "ascii.lisp", line 7, characters 0-156
(defun ascii-is-whitespace (s)
  "(ascii-is-whitespace S) is true if S is \t, \n, \r, or SPACE"
  (or (= s 9)
      (= s 10)
      (= s 13)
      (= s 32)))

2.2.10 ascii-sign

;; File "ascii.lisp", line 14, characters 0-121
(defun ascii-sign (s)
  "(ascii-sign S) is 1 if S is +, -1 if it -, or 0 otherwise"
  (case s
    ?- -1
    ?+  1
    0))

2.2.11 ascii-to-lower

;; File "ascii.lisp", line 43, characters 0-100
(defun ascii-to-lower (c)
  (declare (external "tolower"))
  (if (ascii-is-upper c) (logor c 32) c))

2.2.12 ascii-to-upper

;; File "ascii.lisp", line 47, characters 0-103
(defun ascii-to-upper (c)
  (declare (external "toupper"))
  (if (ascii-is-lower c) (logand c 0x5f) c))

2.2.13 atexit

;; File "stdlib.lisp", line 34, characters 0-70
(defun atexit (cb)
  (declare (external "atexit" "__cxa_atexit"))
  0)

2.2.14 atoi

;; File "atoi.lisp", line 31, characters 0-93
(defun atoi  (s)
  (declare (visibility :public) (external "atoi"))
  (make-converter int s))

2.2.15 atol

;; File "atoi.lisp", line 35, characters 0-94
(defun atol  (s)
  (declare (visibility :public) (external "atol"))
  (make-converter long s))

2.2.16 atoll

;; File "atoi.lisp", line 39, characters 0-100
(defun atoll (s)
  (declare (visibility :public) (external "atoll"))
  (make-converter long-long s))

2.2.17 bindtextdomain

;; File "libintl.lisp", line 3, characters 0-76
(defun bindtextdomain (_ dir)
  (declare (external "bindtextdomain"))
  dir)

2.2.18 brk

;; File "simple-memory-allocator.lisp", line 69, characters 0-66
(defun brk (val)
  (declare (external "brk"))
  (set brk val)
  0)

2.2.19 calloc

;; File "simple-memory-allocator.lisp", line 107, characters 0-202
(defun calloc (n s)
  "allocates memory and initializes it with zero"
  (declare (external "calloc"))
  (let ((*malloc-initialize-memory* true)
	(*malloc-initial-value* 0))
    (malloc (* n s))))

2.2.20 char

;; File "types.lisp", line 44, characters 0-67
(defun char ()  (declare (context (abi eabi))) (model-ilp32 'char))

2.2.21 coerce

;; File "init.lisp", line 178, characters 0-81
(defun coerce (type v)
  (if (/= (word-width v) type) (extract (-1 type) 0 v) v))

2.2.22 copy-byte

;; File "memory.lisp", line 9, characters 0-134
(defun copy-byte (dst src)
  "(copy-byte DST SRC) copies byte from
   the address SRC to DST."
  (memory-write dst (memory-read src)))

2.2.23 double

;; File "types.lisp", line 100, characters 0-20
(defun double () 64)

2.2.24 errno-location

;; File "errno.lisp", line 13, characters 0-116
(defun errno-location ()
  (declare (visibility :public)
	   (external "__errno_location"))
  errno-location)

2.2.25 exit

;; File "stdlib.lisp", line 29, characters 0-76
(defun exit (code)
  (declare (external "exit" "_exit"))
  (exit-with code))

2.2.26 fflush

;; File "stdio.lisp", line 28, characters 0-88
(defun fflush (s)
  (declare (external "fflush" "fflush_unlocked"))
  (channel-flush s))

2.2.27 fgetc

;; File "stdio.lisp", line 106, characters 0-118
(defun fgetc (stream)
  (declare (external "fgetc" "getc" "fgetc_unlocked" "getc_unlocked"))
  (channel-input stream))

2.2.28 fgets

;; File "stdio.lisp", line 115, characters 0-557
(defun fgets (ptr len str)
  (declare (external "fgets" "fgets_unlocked"))
  (if (= len 0) (terminate-string-and-return-null ptr)
    (let ((i 0)
	  (n (-1 len))
	  (continue true))
      (while (and continue (< i n))
	(let ((c (fgetc str)))
	  (if (= c -1)
	      (set continue false)
	    (memory-write (+ ptr i) (cast char c))
	    (set continue (/= c 0xA))
	    (incr i))))
      (if (= i 0)
	  (terminate-string-and-return-null ptr)
      (memory-write (+ ptr (min n i)) 0:8)
      ptr))))

2.2.29 fileno

;; File "stdio.lisp", line 42, characters 0-64
(defun fileno (stream)
  (declare (external "fileno"))
  stream)

2.2.30 float

;; File "types.lisp", line 101, characters 0-19
(defun float () 32)

2.2.31 fopen

;; File "stdio.lisp", line 37, characters 0-136
(defun fopen (path mode)
  (declare (external "fopen" "open" "fdopen"))
  (let ((file (channel-open path)))
    (if (< file 0) 0 file)))

2.2.32 fputc

;; File "stdio.lisp", line 7, characters 0-146
(defun fputc (char stream)
  (declare (external "fputc" "putc" "fputs_unlocked putc_unlocked"))
  (if (= 0 (channel-output stream char)) char -1))

2.2.33 fputs

;; File "stdio.lisp", line 15, characters 0-225
(defun fputs (p stream)
  (declare (external "fputs" "fputs_unlocked"))
  (while (not (points-to-null p))
    (fputc (cast int (memory-read p)) stream)
    (incr p))
  (let ((r (fputc 0xA stream)))
    (fflush stream)
    r))

2.2.34 fread

;; File "stdio.lisp", line 92, characters 0-208
(defun fread (ptr size n stream)
  (declare (external "fread" "fread_unlocked"))
  (let ((i 0))
    (while (and
	    (< i n)
	    (= size (input-item ptr size i stream)))
      (incr i))
    i))

2.2.35 free

;; File "simple-memory-allocator.lisp", line 103, characters 0-87
(defun free (p)
  "frees the memory region pointed by P"
  (declare (external "free")))

2.2.36 fwrite

;; File "stdio.lisp", line 63, characters 0-204
(defun fwrite (buf size n stream)
  (declare (external "fwrite" "fwrite_unlocked"))
  (let ((i 0))
    (while (and (< i n)
		(= size (output-item buf size i stream)))
      (incr i))
    i))

2.2.37 getchar

;; File "stdio.lisp", line 149, characters 0-97
(defun getchar ()
  (declare (external "getchar" "getchar_unlocked"))
  (fgetc *standard-input*))

2.2.38 getenv

;; File "stdlib.lisp", line 9, characters 0-452
(defun getenv (name)
  "finds a value of an environment variable with the given name"
  (declare (external "getenv" "secure_getenv"))
  (let ((p environ)
	(n (strlen name)))
    (while (and (not (points-to-null p))
		(/= (memcmp (read-word ptr_t p) name n) 0))
      (set p (ptr+1 ptr_t p)))
    (if (not (points-to-null p))
	(let ((p (read-word ptr_t p)))
	  (if (= (memory-read (+ p n)) ?=) (+ p n 1) 0))
      0)))

2.2.39 getopt

;; File "getopt.lisp", line 64, characters 0-688
(defun getopt (argc argv opts)
  (declare
   (visibility :public)
   (external "getopt"))
  (when (= 0 optind)
    (set optind 1)
    (set last-ofs 0))
  (if (getopt-finished argc argv) -1
    (if (getopt-nearly-finished argv)
	(prog (incr optind) -1)
      (let ((p 0) )
	(getopt-update-optopt argv last-ofs)
	(set p (strchr opts optopt))
	(if p
	    (prog
	     (when (points-to-null p)
	       (prog (incr optind) (getopt argc argv opts)))
	     (if (getopt-expects-argument p)
		 (getopt-with-argument argv opts p last-ofs)
	       (prog (incr last-ofs) optopt)))
	   (incr optind)
	   ??)))))

2.2.40 getopt_long

;; File "getopt.lisp", line 88, characters 0-149
(defun getopt_long (argc argv opts _ _)
  (declare
   (visibility :public)
   (external "getopt_long" "getopt_long_only"))
  (getopt argc argv opts))

2.2.41 getpagesize

;; File "unistd.lisp", line 13, characters 0-73
(defun getpagesize ()
  (declare (external "getpagesize"))
  (* 64 1024))

2.2.42 gets

;; File "stdio.lisp", line 133, characters 0-370
(defun gets (ptr)
  (declare (external "gets"))
  (let ((str *standard-input*)
	(i 0)
	(continue true))
    (while continue
      (let ((c (fgetc str)))
	(if (= c -1)
	    (set continue false)
	  (memory-write (+ ptr i) (cast char c))
	  (set continue (/= c 0xA))
	  (incr i))))
    (memory-write (+ ptr i) 0:8)
    ptr))

2.2.43 int

;; File "types.lisp", line 46, characters 0-66
(defun int ()   (declare (context (abi eabi))) (model-ilp32 'int))

2.2.44 int32_t

;; File "types.lisp", line 97, characters 0-21
(defun int32_t () 32)

2.2.45 int64_t

;; File "types.lisp", line 98, characters 0-21
(defun int64_t () 64)

2.2.46 ioctl

;; File "unistd.lisp", line 7, characters 0-50
(defun ioctl (_ _) (declare (external "ioctl")) 0)

2.2.47 isatty

;; File "unistd.lisp", line 3, characters 0-62
(defun isatty (fd)
  (declare (external "isatty"))
  (< fd 3))

2.2.48 lconv

;; File "locale.lisp", line 17, characters 0-55
(defun lconv ()
  (declare (external "lconv"))
  LCONV)

2.2.49 long

;; File "types.lisp", line 47, characters 0-67
(defun long ()  (declare (context (abi eabi))) (model-ilp32 'long))

2.2.50 long-long

;; File "types.lisp", line 95, characters 0-33
(defun long-long () (word-width))

2.2.51 longjmp

;; File "setjmp.lisp", line 11, characters 0-103
(defun longjmp (_ _)
  (declare (external "longjmp" "_longjmp" "siglongjmp" "_siglongjmp"))
  (exit 1))

2.2.52 malloc

;; File "simple-memory-allocator.lisp", line 52, characters 0-618
(defun malloc (n)
  "allocates a memory region of size N"
  (declare (external "malloc"))
  (if (= n 0) *malloc-zero-sentinel*
    (if (malloc-will-reach-limit n) 0
      (malloc/grow-arena-if-needed n)
      (+= *malloc/total-bytes-allocated* n)
      (let ((header-size (sizeof int))
	    (chunk-size (+ n (* 2 *malloc-guard-edges*) header-size))
	    (ptr *malloc/brk*))
	(malloc/initialize ptr chunk-size)
	(+= *malloc/brk* chunk-size)
	(malloc/fill-edges ptr chunk-size)
	(+= ptr *malloc-guard-edges*)
	(malloc/put-chunk-size ptr n)
	(+ ptr header-size)))))

2.2.53 memcasecmp

;; File "string.lisp", line 152, characters 0-265
(defun memcasecmp (p1 p2 n)
  (let ((res 0) (i 0))
    (while (and (< i n) (not res))
      (set res (compare
		(ascii-to-lower (cast int (memory-read p1)))
		(ascii-to-lower (cast int (memory-read p2)))))
      (incr p1 p2 i))
    res))

2.2.54 memccpy

;; File "string.lisp", line 53, characters 0-271
(defun memccpy (dst src c len)
  (declare (external "memccpy"))
  (let ((found 0))
    (while (and len (not found))
      (copy-byte dst src)
      (incr src)
      (decr len)
      (when (points-to char dst c)
	(set found (+ 1 dst)))
      (incr dst))
    found))

2.2.55 memchr

;; File "string.lisp", line 74, characters 0-84
(defun memchr (p c n)
  (declare (external "memchr"))
  (find-character incr p c n))

2.2.56 memcmp

;; File "string.lisp", line 127, characters 0-205
(defun memcmp (p1 p2 n)
  (declare (external "memcmp"))
  (let ((res 0) (i 0))
    (while (and (< i n) (not res))
      (set res (compare (memory-read p1) (memory-read p2)))
      (incr p1 p2 i))
    res))

2.2.57 memcpy

;; File "string.lisp", line 49, characters 0-87
(defun memcpy (dst src len)
  (declare (external "memcpy"))
  (copy-right dst src len))

2.2.58 memmove

;; File "string.lisp", line 39, characters 0-238
(defun memmove (dst src len)
  (declare (external "memmove"))
  (let ((a dst) (b src))
    (when (/= src dst)
      (if (> src dst) (copy-right a b len)
	(+= a (-1 len))
	(+= b (-1 len))
	(copy-left a b len))))
  dst)

2.2.59 memrchr

;; File "string.lisp", line 79, characters 0-86
(defun memrchr (p c n)
  (declare (external "memrchr"))
  (find-character decr p c n))

2.2.60 memset

;; File "string.lisp", line 118, characters 0-143
(defun memset (p c n)
  (declare (external "memset"))
  (let ((p p))
    (while n
      (memory-write p c)
      (incr p)
      (decr n)))
  p)

2.2.61 model-ilp32

;; File "types.lisp", line 3, characters 0-103
(defun model-ilp32 (type)
  (case type
    'char 8
    'short 16
    'int 32
    'long 32
    'ptr 32))

2.2.62 model-ilp64

;; File "types.lisp", line 19, characters 0-103
(defun model-ilp64 (type)
  (case type
    'char 8
    'short 16
    'int 64
    'long 64
    'ptr 64))

2.2.63 model-llp64

;; File "types.lisp", line 27, characters 0-103
(defun model-llp64 (type)
  (case type
    'char 8
    'short 16
    'int 32
    'long 32
    'ptr 64))

2.2.64 model-lp32

;; File "types.lisp", line 11, characters 0-102
(defun model-lp32 (type)
  (case type
    'char 8
    'short 16
    'int 16
    'long 32
    'ptr 32))

2.2.65 model-lp64

;; File "types.lisp", line 35, characters 0-102
(defun model-lp64 (type)
  (case type
    'char 8
    'short 16
    'int 32
    'long 64
    'ptr 64))

2.2.66 open3

;; File "stdio.lisp", line 46, characters 0-81
(defun open3 (path flags mode)
  (declare (external "open"))
  (fopen path mode))

2.2.67 output-item

;; File "stdio.lisp", line 55, characters 0-170
(defun output-item (buf size item fd)
  (let ((i 0))
    (while (and
	    (< i size)
	    (output-item-nth-char buf size item fd i))
      (incr i))
    i))

2.2.68 output-item-nth-char

;; File "stdio.lisp", line 50, characters 0-131
(defun output-item-nth-char (ptr size item fd i)
  (= 0 (channel-output
	fd
	(memory-read (+ ptr (* size item) i)))))

2.2.69 points-to-null

;; File "memory.lisp", line 5, characters 0-109
(defun points-to-null (p)
  "(points-to-null P) true if P points to a zero byte"
  (is-zero (memory-read p)))

2.2.70 ptr_t

;; File "types.lisp", line 48, characters 0-66
(defun ptr_t () (declare (context (abi eabi))) (model-ilp32 'ptr))

2.2.71 putchar

;; File "stdio.lisp", line 11, characters 0-107
(defun putchar (char)
  (declare (external "putchar" "putchar_unlocked"))
  (fputc char *standard-output*))

2.2.72 puts

;; File "stdio.lisp", line 24, characters 0-92
(defun puts (p)
  (declare (external "puts" "puts_unlocked"))
  (fputs p *standard-output*))

2.2.73 read

;; File "stdio.lisp", line 101, characters 0-74
(defun read (fd buf n)
  (declare (external "read"))
  (fread buf 1 n fd))

2.2.74 realloc

;; File "simple-memory-allocator.lisp", line 78, characters 0-166
(defun realloc (ptr len)
  (declare (external "realloc"))
  (if (not ptr) (malloc len)
    (if (not len) (realloc/as-free ptr)
      (realloc/update-chunk ptr len))))

2.2.75 sbrk

;; File "simple-memory-allocator.lisp", line 74, characters 0-75
(defun sbrk (increment)
  (declare (external "sbrk"))
  (+= brk increment))

2.2.76 setjmp

;; File "setjmp.lisp", line 3, characters 0-64
(defun setjmp (_)
  (declare (external "setjmp" "_setjmp"))
  0)

2.2.77 setlocale

;; File "locale.lisp", line 13, characters 0-72
(defun setlocale (_ locale)
  (declare (external "setlocale"))
  locale)

2.2.78 short

;; File "types.lisp", line 45, characters 0-68
(defun short () (declare (context (abi eabi))) (model-ilp32 'short))

2.2.79 sigsetjmp

;; File "setjmp.lisp", line 7, characters 0-75
(defun sigsetjmp (_ _)
  (declare (external "sigsetjmp" "_sigsetjmp"))
  0)

2.2.80 stpcpy

;; File "string.lisp", line 243, characters 0-136
(defun stpcpy (dst src)
  (declare (external "stpcpy"))
  (let ((len (strlen src)))
    (+ (memcpy dst src (+1 len)) (cast ptr_t len))))

2.2.81 strcasecmp

;; File "string.lisp", line 167, characters 0-160
(defun strcasecmp (p1 p2)
  (declare (external "strcasecmp"))
  (strncasecmp p1 p2 (min (strlen/with-null p1)
			  (strlen/with-null p2))))

2.2.82 strcasestr

;; File "string.lisp", line 185, characters 0-109
(defun strcasestr (hay needle)
  (declare (external "strcasestr"))
  (find-substring strncasecmp hay needle))

2.2.83 strcat

;; File "string.lisp", line 107, characters 0-111
(defun strcat (dst src)
  (declare (external "strcat"))
  (strcpy (+ dst (cast ptr_t (strlen dst))) src)
  dst)

2.2.84 strchr

;; File "string.lisp", line 84, characters 0-92
(defun strchr (p c)
  (declare (external "strchr" "index"))
  (memchr p c (+ (strlen p) 1)))

2.2.85 strchrnul

;; File "string.lisp", line 92, characters 0-111
(defun strchrnul (p c)
  (declare (external "strchrnul"))
  (let ((p (strchr p c)))
    (if p p (strchr p 0))))

2.2.86 strcmp

;; File "string.lisp", line 147, characters 0-152
(defun strcmp (s1 s2)
  (declare (external "strcmp" "strcoll"))
  (memcmp s1 s2 (min (strlen/with-null s1)
		     (strlen/with-null s2))))

2.2.87 strcpy

;; File "string.lisp", line 16, characters 0-180
(defun strcpy (dst src)
  (declare (external "strcpy"))
  (let ((dst dst))
    (while (not (points-to-null src))
      (copy-byte-shift dst src))
    (memory-write dst 0:8))
  dst)

2.2.88 strcspn

;; File "string.lisp", line 196, characters 0-176
(defun strcspn (str set)
  (declare (external "strcspn"))
  (let ((len 0))
    (until (or (points-to-null str)
	       (strpbrk str set))
      (incr str len))
    len))

2.2.89 strdup

;; File "string.lisp", line 33, characters 0-126
(defun strdup (src)
  (declare (external "strdup"))
  (let ((dst (malloc (+1 (strlen src)))))
    (and dst (strcpy dst src))))

2.2.90 strlen

;; File "string.lisp", line 8, characters 0-132
(defun strlen (p)
  (declare (external "strlen"))
  (let ((len 0))
    (while (not (points-to-null p))
      (incr len p))
    len))

2.2.91 strncasecmp

;; File "string.lisp", line 161, characters 0-189
(defun strncasecmp (p1 p2 n)
  (declare (external "strncasecmp"))
  (memcasecmp p1 p2 (min n
			 (strlen/with-null p1)
			 (strlen/with-null p2))))

2.2.92 strncat

;; File "string.lisp", line 112, characters 0-122
(defun strncat (dst src len)
  (declare (external "strncat"))
  (strncpy (+ dst (cast ptr_t (strlen dst))) src len)
  dst)

2.2.93 strncmp

;; File "string.lisp", line 141, characters 0-169
(defun strncmp (s1 s2 n)
  (declare (external "strncmp"))
  (memcmp s1 s2 (min n
		     (strlen/with-null s1)
		     (strlen/with-null s2))))

2.2.94 strncpy

;; File "string.lisp", line 24, characters 0-213
(defun strncpy (dst src len)
  (declare (external "strncpy"))
  (let ((dst dst))
    (while (and len (not (points-to-null src)))
      (decr len)
      (copy-byte-shift dst src))
    (memory-write dst 0:8))
  dst)

2.2.95 strpbrk

;; File "string.lisp", line 97, characters 0-248
(defun strpbrk (str set)
  (declare (external "strpbrk"))
  (let ((p set) (found 0))
    (while (and
	    (not (points-to-null p))
	    (not found))
      (set found (strchr str (cast int (memory-read p))))
      (incr p))
    found))

2.2.96 strrchr

;; File "string.lisp", line 88, characters 0-96
(defun strrchr (p c)
  (declare (external "strrchr" "rindex"))
  (memrchr p c (+ (strlen p) 1)))

2.2.97 strsep

;; File "string.lisp", line 205, characters 0-307
(defun strsep (strp sep)
  (declare (external "strsep"))
  (let ((str (read-word ptr_t strp))
	(pos (and str (+ str (strcspn str sep)))))
    (when str
      (if (points-to-null pos)
	  (write-word ptr_t strp 0)
	(memory-write pos 0)
	(write-word ptr_t strp (+1 pos))))
    str))

2.2.98 strstr

;; File "string.lisp", line 181, characters 0-97
(defun strstr (hay needle)
  (declare (external "strstr"))
  (find-substring strncmp hay needle))

2.2.99 strtok

;; File "string.lisp", line 231, characters 0-194
(defun strtok (str sep)
  (declare (external "strtok"))
  (unless *strtok-static-storage*
    (set *strtok-static-storage* (malloc (sizeof ptr_t))))
  (strtok_r str sep *strtok-static-storage*))

2.2.100 strtok_r

;; File "string.lisp", line 217, characters 0-346
(defun strtok_r (str sep ptr)
  (declare (external "strtok_r"))
  (when str (write-word ptr_t ptr str))
  (if (not ptr) ptr
    (let ((str (read-word ptr_t ptr))
	  (del (+ str (strspn str sep)))
	  (str (+ del (strcspn del sep))))
      (if (points-to-null del) 0
	(write-word ptr_t ptr str)
	(memory-write del 0)))))

2.2.101 strxfrm

;; File "string.lisp", line 238, characters 0-92
(defun strxfrm (dst src len)
  (declare (external "strxfrm"))
  (strncpy dst src len)
  len)

2.2.102 textdomain

;; File "libintl.lisp", line 7, characters 0-66
(defun textdomain (dom)
  (declare (external "textdomain"))
  dom)

2.2.103 write

;; File "stdio.lisp", line 71, characters 0-161
(defun write (fd buf cnt)
  (declare (external "write"))
  (let ((written (fwrite buf 1 cnt fd))
	(failure (channel-flush fd)))
    (or failure written)))

2.3 Macros

2.3.1 +1

;; File "init.lisp", line 89, characters 0-65
(defmacro +1 (x)
  "(+1 x) returns the successor of X"
  (+ x 1))

2.3.2 +=

;; File "init.lisp", line 81, characters 0-89
(defmacro += (x y)
  "(+= X Y) assigns a sum of X and Y to variable X"
  (set x (+ x y)))

2.3.3 -1

;; File "init.lisp", line 85, characters 0-67
(defmacro -1 (x)
  "(-1 X) returns the predecessor of X"
  (- x 1))

2.3.4 and

;; File "init.lisp", line 106, characters 0-194
(defmacro and (x)
  "(and X Y ...) evaluates expressions X,Y,... until the first
  expression that returns false. The value of the whole form is the
  value of the last evaluated expression." x)

2.3.5 array-get

;; File "pointers.lisp", line 67, characters 0-153
(defmacro array-get (t p n)
  "(array-get T P N) gets the N-th element of the
   array of elements of type T, pointed by P"
  (read-word t (ptr+ t p n)))

2.3.6 array-set

;; File "pointers.lisp", line 72, characters 0-205
(defmacro array-set (t p n w)
  "(array-set T P N W) sets to W the N-th element of the array of
   elements of type T, pointed by P. Returns a pointer to the next
   element"
  (write-word t (ptr+ p n) w))

2.3.7 assert

;; File "init.lisp", line 136, characters 0-164
(defmacro assert (c)
  "(assert COND) terminates program if COND is false"
  (when (not c)
    (msg "Assertion (assert $0) failed" c)
    (error "Assert_failure")))

2.3.8 assert-msg

;; File "init.lisp", line 142, characters 0-170
(defmacro assert-msg (c s)
  "(assert-msg c s) allows you to assert a condition and print a message on failure"
  (when (not c)
    (msg s)
    (error "Assert_failure")))

2.3.9 bitwidth

;; File "init.lisp", line 176, characters 0-33
(defmacro bitwidth (type) (type))

2.3.10 case

;; File "init.lisp", line 51, characters 0-783
(defmacro case (k x xs)
  "(case K K1 X1 K2 X2 ... Kn Xn [DEF]) evaluates K then consequently
   evaluates keys K1 ...Kn until a key Ki such that (= K Ki) is found.
   If such key is found then the whole form evaluates to Xi.
   If no matching key was found, then if the number of keys is equal to
   the number of expressions, nil is returned, otherwise, i.e., if an
   extra expression DEF is provided, then it becomes the value of the
   whole form.

   Examples:

       (defun dispatch-with-default-case (c)
	 (case c
	   1 'hello
	   2 'cruel
	   3 'world
	   'unknown))

       (defun dispatch-with-no-default (c)
	 (case c
	   1 'hello
	   2 'cruel
	   3 'world))"
  (let ((key k))
    (case/dispatch key x xs)))

2.3.11 case/dispatch

;; File "init.lisp", line 170, characters 0-32
(defmacro case/dispatch (k x) x)

2.3.12 cast

;; File "init.lisp", line 175, characters 0-51
(defmacro cast (type x) (extract  (-1 (type)) 0 x))

2.3.13 compare

;; File "init.lisp", line 131, characters 0-134
(defmacro compare (x y)
  "(compare X Y) returns 0 if X = Y, a negative value if X<Y
   and a positive value if X>Y"
  (sign (- x y)))

2.3.14 copy-byte-shift

;; File "memory.lisp", line 14, characters 0-177
(defmacro copy-byte-shift (dst src)
  "(copy-byte-shift DST SRC) copies byte from
   SRC to DST and increments SRC and DST."
  (prog (copy-byte dst src)
	(incr dst src)))

2.3.15 copy-byte-shift-left

;; File "memory.lisp", line 20, characters 0-187
(defmacro copy-byte-shift-left (dst src)
  "(copy-byte-shift-left DST SRC) copies
   byte from DST to SRC and decrements SRC and DST."
  (prog (copy-byte dst src)
	(decr dst src)))

2.3.16 copy-left

;; File "memory.lisp", line 39, characters 0-162
(defmacro copy-left (dst src len)
  "(copy-left DST SRC LEN) copies LEN bytes
    from SRC to DST (right to left)"
  (make-copy copy-byte-shift-left dst src len))

2.3.17 copy-right

;; File "memory.lisp", line 34, characters 0-159
(defmacro copy-right (dst src len)
  "(copy-right DST SRC LEN) copies LEN bytes
    from SRC to DST (left to right)"
  (make-copy copy-byte-shift dst src len))

2.3.18 decr

;; File "init.lisp", line 100, characters 0-111
(defmacro decr (x)
  "(decr X Y ...) decrements the value bound with the variables X, Y, ..."
  (set x (-1 x)))

2.3.19 endian

;; File "pointers.lisp", line 16, characters 0-154
(defmacro endian (f x y)
  "(endian F X Y) expands to (F Y X) if applied
   in the little endian context"
  (declare (context (endian little)))
  (f y x))

2.3.20 fold

;; File "init.lisp", line 154, characters 0-85
(defmacro fold (f a x)
  "(fold F A X Y ...) expands to (F (F A X) Y) ..."
  (f a x))

2.3.21 incr

;; File "init.lisp", line 93, characters 0-111
(defmacro incr (x)
  "(incr X Y ...) increments the value bound with the variables X, Y, ..."
  (set x (+1 x)))

2.3.22 is-in

;; File "init.lisp", line 148, characters 0-102
(defmacro is-in (x y)
  "(is-in X A B C ...) returns true if X is equal A or B or C or ..."
  (= x y))

2.3.23 make-copy

;; File "memory.lisp", line 26, characters 0-146
(defmacro make-copy (copy-byte dst src len)
  "<internal>"
  (let ((ret dst))
    (while len
      (decr len)
      (copy-byte dst src))
    ret))

2.3.24 max

;; File "init.lisp", line 164, characters 0-85
(defmacro max (x)
  "(max X Y ...) returns the upper bound of the (X,Y,...) set"
  x)

2.3.25 min

;; File "init.lisp", line 159, characters 0-89
(defmacro min (x)
  "(min X Y ...) returns the lower bound of the (X,Y,...) set"
      x)

2.3.26 non-zero

;; File "init.lisp", line 77, characters 0-85
(defmacro non-zero (x)
  "(non-zero X) is true if X is not zero"
  (not (is-zero x)))

2.3.27 nth-byte-of-word

;; File "pointers.lisp", line 28, characters 0-284
(defmacro nth-byte-of-word (t i x)
  "(nth-byte-of-word T N X) returns N-th byte
   of the word X that has type T"
  (let ((n (sizeof t))
	(j (endian - n i))
	(k (if (< j n) (- j 1) (+ j n)))
	(hi (- (* 8 (+ k 1)) 1))
	(lo (* k 8)))
    (extract hi lo x)))

2.3.28 or

;; File "init.lisp", line 113, characters 0-346
(defmacro or (x xs)
  "(or <expr> ...) evaluates a sequence of expressions EXPR from left
   to right until it meets the first expression that evaluates to the
   truth value, that will become the value of the whole form. If no
   expression returned the truth value, then the result of the whole
   form is 0:1"
  (let ((r x)) (if r r (or xs))))

2.3.29 points-to

;; File "pointers.lisp", line 62, characters 0-152
(defmacro points-to (t p v)
  "(points-to T P V) return true if t P points
  to a value of type T that is equal to V."
  (= (read-word t p) (cast t v)))

2.3.30 ptr+

;; File "pointers.lisp", line 6, characters 0-124
(defmacro ptr+ (t p n)
  "(ptr+ T P N) increments N times the
    pointer P to a value of type T."
  (+ p (* (sizeof t) n)))

2.3.31 ptr+1

;; File "pointers.lisp", line 11, characters 0-112
(defmacro ptr+1 (type p)
  "(ptr+1 T P) increments the pointer P to
   a value of to type T."
  (ptr+ type p 1))

2.3.32 read-word

;; File "pointers.lisp", line 38, characters 0-252
(defmacro read-word (t a)
  "(read-word T A) reads a word of type T at address A"
  (let ((p a)
	(x (memory-read p))
	(n (-1 (sizeof t))))
    (while n
      (incr p)
      (decr n)
      (set x (endian concat x (memory-read p))))
    x))

2.3.33 set$

;; File "init.lisp", line 122, characters 0-152
(defmacro set$ (s x)
  "(set$ s x) set the value of the symbol s to x, returns x.
   Like set but without implicit quotation."
  (set-symbol-value s x))

2.3.34 sign

;; File "init.lisp", line 127, characters 0-106
(defmacro sign (x)
  "returns 1 if X > 0, 0 if X = 0, and -1 if X < 0"
  (if (< x 0) -1 (if (> x 0) 1 0)))

2.3.35 sizeof

;; File "init.lisp", line 177, characters 0-46
(defmacro sizeof (type) (/ (bitwidth type) 8))

2.3.36 strspn

;; File "string.lisp", line 189, characters 0-136
(defmacro strspn (str set)
  (declare (external "strspn"))
  (let ((len 0))
    (while (strpbrk str set)
      (incr str len))
    len))

2.3.37 unless

;; File "init.lisp", line 38, characters 0-267
(defmacro unless (cnd body)
  "(unless CND BODY) if CND evaluates to false, then BODY is evaluated and
   the value of the last expression in BODY becomes the value of the
   whole expression. Otherwise, if CND evaluates to true, nil is returned."
  (if cnd () body))

2.3.38 until

;; File "init.lisp", line 44, characters 0-349
(defmacro until (c b)
  "(until COND BODY) if COND evaluates to true, then the whole expression
   evaluates to nil and BODY is not evaluated. Otherwise, if COND evaluates
   to false, then BODY is evaluated until COND evaluates to true and the value
   of the last evaluation of BODY becomes the value of the whole expression."
  (while (not c) b))

2.3.39 when

;; File "init.lisp", line 32, characters 0-270
(defmacro when (cnd body)
  "(when CND BODY) if CND evaluates to true, then BODY is evaluated and
   the value of the last expression in BODY becomes the value of the
   whole expression. Otherwise, if CND evaluates to false, nil is returned."
  (if cnd (prog body) ()))

2.3.40 write-word

;; File "pointers.lisp", line 49, characters 0-329
(defmacro write-word (t a x)
  "(write-word T A X) writes the word X of type T to address A
  returns an address that points to the first byte that follows
  the just written word.
  "
  (let ((p a)
	(n (sizeof t))
	(i 0))
    (while (< i n)
      (memory-write p (nth-byte-of-word t i x))
      (incr p i))
    p))

2.4 Methods

2.4.1 init

;; File "locale.lisp", line 9, characters 0-61
(defmethod init ()
  (set LCONV brk)
  (+= brk *lconv-size*))

2.4.2 machine-kill

;; File "stdio.lisp", line 153, characters 0-105
(defmethod primus:machine-kill ()
  (channel-flush *standard-output*)
  (channel-flush *standard-error*))

2.5 Parameters

2.5.1 *lconv-size*

;; File "locale.lisp", line 5, characters 0-49
(defparameter *lconv-size* (* 20 (sizeof ptr_t)))

2.5.2 *malloc-arena-end*

;; File "simple-memory-allocator.lisp", line 21, characters 0-82
(defparameter *malloc-arena-end* brk
  "the starting address of the malloc arena")

2.5.3 *malloc-arena-initial-size*

;; File "simple-memory-allocator.lisp", line 14, characters 0-149
(defparameter *malloc-arena-initial-size* 0x40000
  "the maximum number of bytes totally allocated by malloc,
   if not set, then there is no limit")

2.5.4 *malloc-arena-start*

;; File "simple-memory-allocator.lisp", line 18, characters 0-84
(defparameter *malloc-arena-start* brk
  "the starting address of the malloc arena")

2.5.5 *malloc-guard-edges*

;; File "simple-memory-allocator.lisp", line 24, characters 0-119
(defparameter *malloc-guard-edges* 0
  "if not nil, then add padding of the specified size
   around allocated chunks")

2.5.6 *malloc-guard-pattern*

;; File "simple-memory-allocator.lisp", line 28, characters 0-91
(defparameter *malloc-guard-pattern* 0xA5
  "a byte that will be used to fill guard edges")

2.5.7 *malloc-initial-value*

;; File "simple-memory-allocator.lisp", line 45, characters 0-91
(defparameter *malloc-initial-value* 0
  "initialize allocated memory with the said value")

2.5.8 *malloc-initialize-memory*

;; File "simple-memory-allocator.lisp", line 34, characters 0-120
(defparameter *malloc-initialize-memory* false
  "if true then initialize allocated memory with *malloc-initial-value*")

2.5.9 *malloc-max-arena-size*

;; File "simple-memory-allocator.lisp", line 10, characters 0-141
(defparameter *malloc-max-arena-size* nil
  "the maximum number of bytes totally allocated by malloc,
   if not set, then there is no limit")

2.5.10 *malloc-max-chunk-size*

;; File "simple-memory-allocator.lisp", line 6, characters 0-124
(defparameter *malloc-max-chunk-size* nil
  "the maximum size of a single memory chunk,
   if nil then there is no limit. ")

2.5.11 *malloc-uniform-max-value*

;; File "simple-memory-allocator.lisp", line 41, characters 0-180
(defparameter *malloc-uniform-max-value* nil
  "if set then defines the lower bound of the uniformely distributed
   random value that is used to represent an unitialized memory ")

2.5.12 *malloc-uniform-min-value*

;; File "simple-memory-allocator.lisp", line 37, characters 0-180
(defparameter *malloc-uniform-min-value* nil
  "if set then defines the lower bound of the uniformely distributed
   random value that is used to represent an unitialized memory ")

2.5.13 *malloc-zero-sentinel*

;; File "simple-memory-allocator.lisp", line 31, characters 0-84
(defparameter *malloc-zero-sentinel* 0
  "a pointer that is returned by (malloc 0)")

2.5.14 *malloc/brk*

;; File "simple-memory-allocator.lisp", line 48, characters 0-31
(defparameter *malloc/brk* brk)

2.5.15 *malloc/total-bytes-allocated*

;; File "simple-memory-allocator.lisp", line 50, characters 0-47
(defparameter *malloc/total-bytes-allocated* 0)

2.5.16 *strtok-static-storage*

;; File "string.lisp", line 229, characters 0-40
(defparameter *strtok-static-storage* 0)

2.6 Primitives

2.6.1 *

(* X Y Z …) returns the product of arguments or 0 if the list of arguments is empty

2.6.2 +

(+ X Y …) returns the sum of arguments, or 0 if there are no arguments,

2.6.3 -

(- X Y Z …) returns X - Y - Z - …, or 0 if there are no arguments.

2.6.4 /

(/ X Y Z …) returns X / Y / Z / … or 0 if the list of arguments is empty

2.6.5 /=

(/= X Y Z …) returns true if at least one argument is not equal to another argument. Returns false if the list of arguments is empty

2.6.6 <

(< X Y Z …) is true if the list of arguments is an strict ascending chain or if it is empty

2.6.7 <=

(< X Y Z …) is true if the list of arguments is an ascending chain or if it is empty

2.6.8 =

(= X Y Z …) returns true if all arguments are equal. True if the list of arguments is empty

2.6.9 >

(< X Y Z …) is true if the list of arguments is a strict descending chain or if it is empty

2.6.10 >=

(< X Y Z …) is true if the list of arguments is a descending chain or if it is empty

2.6.11 all-static-constant

(all-static-constant X Y ..) is true if X,Y,… are static constants. A value is a static constant if it was initialized from a constant value or computed from static constant values.

2.6.12 arshift

(arshift X N) arithmetically shifts X right by N bits

2.6.13 channel-close

(channel-close DESCR) closes a channel that has the specified descriptor DESCR. If no such channel exists, then returns -1. Otherwise returns 0. The descriptor of the closed channel will be reused by the consequent calls to `channel-open'. If the channel had any data associated with it and not yet flushed, then the data is discarded.

2.6.14 channel-flush

(channel-flush DESCR) forces data that were written to a channel that has the descriptor DESCR to be outputted to the associated destination. Returns -1 if no such channel exists or if in case of an IO error.

2.6.15 channel-input

(channel-input DESC) reads one byte from a channel that has the descriptor DESC. Returns -1 if no such channel exists, or if any IO error occurs, if the channel is not readable, or if the channel is in the end-of-file condition.

2.6.16 channel-open

(channel-open PTR) creates a new channel that is associated with a null-terminated path pointed by PTR. Returns a non-negative channel descriptor, if the channel subsystem have a mapping from the obtained path to a physical file and this file is accessible. Otherwise returns a negative value.

2.6.17 channel-output

(channel-output DESCR CHAR …) outputs one or more characters to a channel that has the descriptor DESCR. Returns -1 if no such channel exits, if a channel is not writable, or if any IO error occurs in an associated physical file. Otherwise, returns 0. Note: the channel system is buffered, and the actual IO operation (as well as errors) could be delayed until (channel-flush DESCR) is called.

2.6.18 concat

(concat X Y Z …) concatenates words X, Y, Z, … into one big word

2.6.19 dict-add

(dict-add DIC KEY DATA) associates DATA with KEY in the dictionary DIC. Returns KEY.

2.6.20 dict-del

(dict-del DIC KEY) deletes any association with KEY in the dictionary DIC

2.6.21 dict-first

(dict-first DIC) is the first key in DIC or nil if DIC is empty.

2.6.22 dict-get

(dict-get DIC KEY) returns data associated with KEY in the dictionary DIC, and returns NIL if either DIC doesn't exist on no data are associated

2.6.23 dict-has

(dict-has DIC KEY) returns T if the dictionary DIC has the key KEY

2.6.24 dict-length

(dict-first DIC) is the total number of keys in DIC.

2.6.25 dict-next

(dict-next DIC KEY) returns the next key after KEY Returns nil if KEY was the last key in the dictionary. Could be used together with DICT-FIRST for iterating.

2.6.26 exec-addr

(exec-addr D) passes the control flow to D and never returns

2.6.27 exec-symbol

(exec-symbol D) passes the control flow to D and never returns

2.6.28 exit-with

(exit-with N) terminates program with the exit codeN

2.6.29 extract

(extract HI LO X) extracts bits from HI to LO (including both) from the word X

2.6.30 get-current-program-counter

(get-current-program-counter) returns current program cunnter

2.6.31 ieee754-abs

applies abs to the operand

2.6.32 ieee754-acos

applies acos to the operand

2.6.33 ieee754-add

reduces the list of operands with add

2.6.34 ieee754-asin

applies asin to the operand

2.6.35 ieee754-atan

applies atan to the operand

2.6.36 ieee754-atan2

reduces the list of operands with atan2

2.6.37 ieee754-ceil

applies ceil to the operand

2.6.38 ieee754-cos

applies cos to the operand

2.6.39 ieee754-cosh

applies cosh to the operand

2.6.40 ieee754-cti

truncates to the nearest integer

2.6.41 ieee754-div

reduces the list of operands with div

2.6.42 ieee754-eq

returns true if all operands are ordered with the eq order

2.6.43 ieee754-exp

applies exp to the operand

2.6.44 ieee754-expm1

applies expm1 to the operand

2.6.45 ieee754-floor

applies floor to the operand

2.6.46 ieee754-ge

returns true if all operands are ordered with the ge order

2.6.47 ieee754-gt

returns true if all operands are ordered with the gt order

2.6.48 ieee754-hypot

reduces the list of operands with hypot

2.6.49 ieee754-is-nan

checks if is-nan holds

2.6.50 ieee754-le

returns true if all operands are ordered with the le order

2.6.51 ieee754-log

applies log to the operand

2.6.52 ieee754-log10

applies log10 to the operand

2.6.53 ieee754-log1p

applies log1p to the operand

2.6.54 ieee754-lt

returns true if all operands are ordered with the lt order

2.6.55 ieee754-mod

reduces the list of operands with mod

2.6.56 ieee754-mul

reduces the list of operands with mul

2.6.57 ieee754-ne

returns true if all operands are ordered with the ne order

2.6.58 ieee754-neg

applies neg to the operand

2.6.59 ieee754-pos

applies pos to the operand

2.6.60 ieee754-pow

reduces the list of operands with pow

2.6.61 ieee754-sin

applies sin to the operand

2.6.62 ieee754-sinh

applies sinh to the operand

2.6.63 ieee754-sqrt

applies sqrt to the operand

2.6.64 ieee754-sub

reduces the list of operands with sub

2.6.65 ieee754-tan

applies tan to the operand

2.6.66 ieee754-tanh

applies tanh to the operand

2.6.67 incident-location

2.6.68 incident-report

2.6.69 invoke-subroutine

(invoke-subroutine addr args …) calls the subroutine at the specified address.

2.6.70 is-negative

(is-negative X Y …) returns true if all arguments are negative

2.6.71 is-positive

(is-positive X Y …) returns true if all arguments are positive

2.6.72 is-zero

(is-zero X Y …) returns true if all arguments are zeros

2.6.73 lnot

(lnot X) returns the one complement of X

2.6.74 logand

(logand X Y Z …) returns X & Y & Z & … or 0 if the list of arguments is empty, where & is the bitwise AND operation. Returns ~0 if the list of arguments is empty

2.6.75 logor

(logor X Y Z …) returns X | Y | Z | … or 0 if the list of arguments is empty, where | is the bitwise OR operation

2.6.76 logxor

(logxor X Y Z …) returns X ^ Y ^ Z ^ … or 0 if the list of arguments is empty, where ^ is the bitwise XOR operation

2.6.77 lshift

(lshift X N) logically shifts X left by N bits

2.6.78 memory-allocate

(memory-allocate P N V?) maps memory region [P,P+N), if V is provided, then fills the newly mapped region with the value V

2.6.79 memory-read

(memory-read A) loads one byte from the address A

2.6.80 memory-write

(memory-write A X) stores by X to A

2.6.81 mod

(mod X Y Z …) returns X % Y % Z % … or 0 if the list of arguments is empty, where % is the modulo operation

2.6.82 neg

(neg X) returns the two complement of X

2.6.83 not

(not X) returns true if X is zero

2.6.84 points-to-static-data

(points-to-static-data PTR LEN) is true iff (all-static-constant *PTR .. *(PTR+LEN-1))

2.6.85 reg-name

(reg-name N) returns the name of the register with the index N

2.6.86 region-contains

(region-contains ID X) return the region in ID that has X. Returns the lower bound of the first region that contains value X in the set of regions with the given ID. Returns nil otherwise. Returns nil if a set with the given ID doesn't exist.

2.6.87 region-count

(region-count ID) the total number of regions in the set ID. Counts the number of regions (including intersecting) stored in the set of regions referenced by the symbol ID. Returns nil if there is no set with the given ID, otherwise returns the number of regions in that set.

2.6.88 region-create

(region-create ID LOWER UPPER) adds [LOWER,UPPER] to the set ID. Adds a region denoted with the interval [LOWER,UPPER] to the set of regions denoted by the symbol ID. Values LOWER and UPPER are included into the interval. If the set of regions ID doesn't exist, then it is created.

2.6.89 region-lower

(region-lower ID X) the lower bound of region that contains X. Returns nil if ID doesn't exist or if it doesn't have a region that includes X. This fucntion is an alias for REGION-CONTAINS. See also, REGION-UPPER.

2.6.90 region-move

(region-move DST SRC P) moves all regions that contain the point P from the set SRC to the set DST. Returns nil if SRC didn't contain any such region, otherwise returns t.

2.6.91 region-upper

(region-upper ID X) the upper bound of the region that contains X. Returns the upper bound of the region that contains point X or nil if there is no such region or such set of regions. See also, REGION-LOWER.

2.6.92 rshift

(rshift X N) logically shifts X right by N bits

2.6.93 s/

(s/ X Y Z …) returns X s/ Y s/ Z s/ … or 0 if the list of arguments is empty, where s/ is the signed division operation

2.6.94 set-symbol-value

(set-symbol-value S X) sets the value of the symbol S to X. Returns X

2.6.95 signed-mod

(signed-mod X Y Z …) returns X % Y % Z % … or 0 if the list of arguments is empty, where % is the signed modulo operation

2.6.96 symbol-concat

(symbol-concat X Y Z …) returns a new symbol that is a concatenation of symbols X,Y,Z,…

2.6.97 symbol-of-string

(symbol-of-string ptr) returns a symbol from a null-terminated string.

2.6.98 taint-get-direct

(taint-get-direct K X) returns the direct taint of the kind K associatedwith the value X, or nil if there is no such taint

2.6.99 taint-get-indirect

(taint-get-indirect K X) returns the indirect taint of the kind K associated with the value X, or nil if there is no such taint

2.6.100 taint-introduce-directly

(taint-introduce-directly K X) introduces a new taint of the kind K that is directly associated with the value X

2.6.101 taint-introduce-indirectly

(taint-introduce-indirectly K X N) introduces a new taint of the kind K that is indirectly associated with X pointing to an object of the size N

2.6.102 taint-kind

(taint-kind t) returns the kind of the taint T.

2.6.103 taint-policy-select

(taint-policy-select K P) selects the taint propagation policy P for the taints of the kind K

2.6.104 taint-policy-set-default

(taint-policy-set-default P) makes P the default taint propagation policy.

2.6.105 taint-sanitize-direct

(taint-sanitize-direct K X) removes any direct taint of the kind K that is directly associated with the value X

2.6.106 taint-sanitize-indirect

(taint-sanitize-indirect K X) removes any direct taint of the kind K that is indirectly associated with the value X

2.6.107 word-width

(word-width) returns machine word width in bits

2.7 Signals

2.7.1 call

(call NAME X Y …) is emitted when a call to a function with the symbolic NAME occurs with the specified list of arguments X,Y,…

2.7.2 call-return

(call-return NAME X Y … R) is emitted when a call to a function with the symbolic NAME returns with the specified list of arguments X,Y,… and return value R.

2.7.3 fini

(fini) occurs when the Primus Machine is finished

2.7.4 init

(init) occurs when the Primus Machine is initialized

2.7.5 interrupt

(interrupt N) is emitted when the hardware interrupt N occurs

2.7.6 jumping

(jumping C D) is emitted before jump to D occurs under the condition C

2.7.7 loaded

(loaded A X) is emitted when X is loaded from A

2.7.8 loading

(loading A) is emitted before load from A occurs

2.7.9 machine-kill

(machine-kill) occurs when Machine is killed and could be used for machine cleanup/teardown and analysis summaries. The machine is in the resticted mode in the body of the methods.

2.7.10 pc-changed

(pc-change PC) is emitted when PC is updated

2.7.11 read

(read V X) is emitted when X is read from V

2.7.12 stored

(stored A X) is emitted when X is stored to A

2.7.13 storing

(storing A) is emitted before store to A occurs

2.7.14 system-stop

(system-stop NAME) occurs when the system with the given name finished its execution. The machine is in the restricted mode in the body of the methods

2.7.15 taint-finalize

(taint-finalize T L) is emitted when the taint T is finilized while still live if L is true or dead if T is false.

2.7.16 written

(written V X) is emitted when X is written to V

3 Package core

3.1 Constants

3.1.1 false

;; File "init.lisp", line 29, characters 0-55
(defconstant false 0:1 "false is another name for 0:1")

3.1.2 nil

;; File "init.lisp", line 30, characters 0-55
(defconstant nil false "nil is another name for false")

3.1.3 true

;; File "init.lisp", line 28, characters 0-54
(defconstant true 1:1  "true is another name for 1:1")

3.2 Functions

3.2.1 coerce

;; File "init.lisp", line 178, characters 0-81
(defun coerce (type v)
  (if (/= (word-width v) type) (extract (-1 type) 0 v) v))

3.2.2 copy-byte

;; File "memory.lisp", line 9, characters 0-134
(defun copy-byte (dst src)
  "(copy-byte DST SRC) copies byte from
   the address SRC to DST."
  (memory-write dst (memory-read src)))

3.2.3 points-to-null

;; File "memory.lisp", line 5, characters 0-109
(defun points-to-null (p)
  "(points-to-null P) true if P points to a zero byte"
  (is-zero (memory-read p)))

3.3 Macros

3.3.1 +1

;; File "init.lisp", line 89, characters 0-65
(defmacro +1 (x)
  "(+1 x) returns the successor of X"
  (+ x 1))

3.3.2 +=

;; File "init.lisp", line 81, characters 0-89
(defmacro += (x y)
  "(+= X Y) assigns a sum of X and Y to variable X"
  (set x (+ x y)))

3.3.3 -1

;; File "init.lisp", line 85, characters 0-67
(defmacro -1 (x)
  "(-1 X) returns the predecessor of X"
  (- x 1))

3.3.4 and

;; File "init.lisp", line 110, characters 0-55
(defmacro and (x xs)
  (let ((r x)) (if r (and xs) r)))

3.3.5 array-get

;; File "pointers.lisp", line 67, characters 0-153
(defmacro array-get (t p n)
  "(array-get T P N) gets the N-th element of the
   array of elements of type T, pointed by P"
  (read-word t (ptr+ t p n)))

3.3.6 array-set

;; File "pointers.lisp", line 72, characters 0-205
(defmacro array-set (t p n w)
  "(array-set T P N W) sets to W the N-th element of the array of
   elements of type T, pointed by P. Returns a pointer to the next
   element"
  (write-word t (ptr+ p n) w))

3.3.7 assert

;; File "init.lisp", line 136, characters 0-164
(defmacro assert (c)
  "(assert COND) terminates program if COND is false"
  (when (not c)
    (msg "Assertion (assert $0) failed" c)
    (error "Assert_failure")))

3.3.8 assert-msg

;; File "init.lisp", line 142, characters 0-170
(defmacro assert-msg (c s)
  "(assert-msg c s) allows you to assert a condition and print a message on failure"
  (when (not c)
    (msg s)
    (error "Assert_failure")))

3.3.9 bitwidth

;; File "init.lisp", line 176, characters 0-33
(defmacro bitwidth (type) (type))

3.3.10 case

;; File "init.lisp", line 51, characters 0-783
(defmacro case (k x xs)
  "(case K K1 X1 K2 X2 ... Kn Xn [DEF]) evaluates K then consequently
   evaluates keys K1 ...Kn until a key Ki such that (= K Ki) is found.
   If such key is found then the whole form evaluates to Xi.
   If no matching key was found, then if the number of keys is equal to
   the number of expressions, nil is returned, otherwise, i.e., if an
   extra expression DEF is provided, then it becomes the value of the
   whole form.

   Examples:

       (defun dispatch-with-default-case (c)
	 (case c
	   1 'hello
	   2 'cruel
	   3 'world
	   'unknown))

       (defun dispatch-with-no-default (c)
	 (case c
	   1 'hello
	   2 'cruel
	   3 'world))"
  (let ((key k))
    (case/dispatch key x xs)))

3.3.11 case/dispatch

;; File "init.lisp", line 172, characters 0-75
(defmacro case/dispatch (k k' x xs)
  (if (= k k') x (case/dispatch k xs)))

3.3.12 cast

;; File "init.lisp", line 175, characters 0-51
(defmacro cast (type x) (extract  (-1 (type)) 0 x))

3.3.13 compare

;; File "init.lisp", line 131, characters 0-134
(defmacro compare (x y)
  "(compare X Y) returns 0 if X = Y, a negative value if X<Y
   and a positive value if X>Y"
  (sign (- x y)))

3.3.14 copy-byte-shift

;; File "memory.lisp", line 14, characters 0-177
(defmacro copy-byte-shift (dst src)
  "(copy-byte-shift DST SRC) copies byte from
   SRC to DST and increments SRC and DST."
  (prog (copy-byte dst src)
	(incr dst src)))

3.3.15 copy-byte-shift-left

;; File "memory.lisp", line 20, characters 0-187
(defmacro copy-byte-shift-left (dst src)
  "(copy-byte-shift-left DST SRC) copies
   byte from DST to SRC and decrements SRC and DST."
  (prog (copy-byte dst src)
	(decr dst src)))

3.3.16 copy-left

;; File "memory.lisp", line 39, characters 0-162
(defmacro copy-left (dst src len)
  "(copy-left DST SRC LEN) copies LEN bytes
    from SRC to DST (right to left)"
  (make-copy copy-byte-shift-left dst src len))

3.3.17 copy-right

;; File "memory.lisp", line 34, characters 0-159
(defmacro copy-right (dst src len)
  "(copy-right DST SRC LEN) copies LEN bytes
    from SRC to DST (left to right)"
  (make-copy copy-byte-shift dst src len))

3.3.18 decr

;; File "init.lisp", line 103, characters 0-50
(defmacro decr (x xs)
  (prog (decr x) (decr xs)))

3.3.19 endian

;; File "pointers.lisp", line 22, characters 0-148
(defmacro endian (f x y)
  "(endian F X Y) expands to (F X Y) if applied
   in the big endian context"
  (declare (context (endian big)))
  (f x y))

3.3.20 fold

;; File "init.lisp", line 157, characters 0-46
(defmacro fold (f a x xs) (fold f (f a x) xs))

3.3.21 incr

;; File "init.lisp", line 97, characters 0-50
(defmacro incr (x xs)
  (prog (incr x) (incr xs)))

3.3.22 is-in

;; File "init.lisp", line 151, characters 0-57
(defmacro is-in (x y ys)
  (or (is-in x y) (is-in x ys)))

3.3.23 make-copy

;; File "memory.lisp", line 26, characters 0-146
(defmacro make-copy (copy-byte dst src len)
  "<internal>"
  (let ((ret dst))
    (while len
      (decr len)
      (copy-byte dst src))
    ret))

3.3.24 max

;; File "init.lisp", line 168, characters 0-42
(defmacro max (x y ys) (max (max x y) ys))

3.3.25 min

;; File "init.lisp", line 163, characters 0-42
(defmacro min (x y ys) (min (min x y) ys))

3.3.26 non-zero

;; File "init.lisp", line 77, characters 0-85
(defmacro non-zero (x)
  "(non-zero X) is true if X is not zero"
  (not (is-zero x)))

3.3.27 nth-byte-of-word

;; File "pointers.lisp", line 28, characters 0-284
(defmacro nth-byte-of-word (t i x)
  "(nth-byte-of-word T N X) returns N-th byte
   of the word X that has type T"
  (let ((n (sizeof t))
	(j (endian - n i))
	(k (if (< j n) (- j 1) (+ j n)))
	(hi (- (* 8 (+ k 1)) 1))
	(lo (* k 8)))
    (extract hi lo x)))

3.3.28 or

;; File "init.lisp", line 120, characters 0-19
(defmacro or (x) x)

3.3.29 points-to

;; File "pointers.lisp", line 62, characters 0-152
(defmacro points-to (t p v)
  "(points-to T P V) return true if t P points
  to a value of type T that is equal to V."
  (= (read-word t p) (cast t v)))

3.3.30 ptr+

;; File "pointers.lisp", line 6, characters 0-124
(defmacro ptr+ (t p n)
  "(ptr+ T P N) increments N times the
    pointer P to a value of type T."
  (+ p (* (sizeof t) n)))

3.3.31 ptr+1

;; File "pointers.lisp", line 11, characters 0-112
(defmacro ptr+1 (type p)
  "(ptr+1 T P) increments the pointer P to
   a value of to type T."
  (ptr+ type p 1))

3.3.32 read-word

;; File "pointers.lisp", line 38, characters 0-252
(defmacro read-word (t a)
  "(read-word T A) reads a word of type T at address A"
  (let ((p a)
	(x (memory-read p))
	(n (-1 (sizeof t))))
    (while n
      (incr p)
      (decr n)
      (set x (endian concat x (memory-read p))))
    x))

3.3.33 set$

;; File "init.lisp", line 122, characters 0-152
(defmacro set$ (s x)
  "(set$ s x) set the value of the symbol s to x, returns x.
   Like set but without implicit quotation."
  (set-symbol-value s x))

3.3.34 sign

;; File "init.lisp", line 127, characters 0-106
(defmacro sign (x)
  "returns 1 if X > 0, 0 if X = 0, and -1 if X < 0"
  (if (< x 0) -1 (if (> x 0) 1 0)))

3.3.35 sizeof

;; File "init.lisp", line 177, characters 0-46
(defmacro sizeof (type) (/ (bitwidth type) 8))

3.3.36 unless

;; File "init.lisp", line 38, characters 0-267
(defmacro unless (cnd body)
  "(unless CND BODY) if CND evaluates to false, then BODY is evaluated and
   the value of the last expression in BODY becomes the value of the
   whole expression. Otherwise, if CND evaluates to true, nil is returned."
  (if cnd () body))

3.3.37 until

;; File "init.lisp", line 44, characters 0-349
(defmacro until (c b)
  "(until COND BODY) if COND evaluates to true, then the whole expression
   evaluates to nil and BODY is not evaluated. Otherwise, if COND evaluates
   to false, then BODY is evaluated until COND evaluates to true and the value
   of the last evaluation of BODY becomes the value of the whole expression."
  (while (not c) b))

3.3.38 when

;; File "init.lisp", line 32, characters 0-270
(defmacro when (cnd body)
  "(when CND BODY) if CND evaluates to true, then BODY is evaluated and
   the value of the last expression in BODY becomes the value of the
   whole expression. Otherwise, if CND evaluates to false, nil is returned."
  (if cnd (prog body) ()))

3.3.39 write-word

;; File "pointers.lisp", line 49, characters 0-329
(defmacro write-word (t a x)
  "(write-word T A X) writes the word X of type T to address A
  returns an address that points to the first byte that follows
  the just written word.
  "
  (let ((p a)
	(n (sizeof t))
	(i 0))
    (while (< i n)
      (memory-write p (nth-byte-of-word t i x))
      (incr p i))
    p))

3.4 Primitives

3.4.1 *

(* X Y Z …) returns the product of arguments or 0 if the list of arguments is empty

3.4.2 +

(+ X Y …) returns the sum of arguments, or 0 if there are no arguments,

3.4.3 -

(- X Y Z …) returns X - Y - Z - …, or 0 if there are no arguments.

3.4.4 /

(/ X Y Z …) returns X / Y / Z / … or 0 if the list of arguments is empty

3.4.5 /=

(/= X Y Z …) returns true if at least one argument is not equal to another argument. Returns false if the list of arguments is empty

3.4.6 <

(< X Y Z …) is true if the list of arguments is an strict ascending chain or if it is empty

3.4.7 <=

(< X Y Z …) is true if the list of arguments is an ascending chain or if it is empty

3.4.8 =

(= X Y Z …) returns true if all arguments are equal. True if the list of arguments is empty

3.4.9 >

(< X Y Z …) is true if the list of arguments is a strict descending chain or if it is empty

3.4.10 >=

(< X Y Z …) is true if the list of arguments is a descending chain or if it is empty

3.4.11 all-static-constant

(all-static-constant X Y ..) is true if X,Y,… are static constants. A value is a static constant if it was initialized from a constant value or computed from static constant values.

3.4.12 arshift

(arshift X N) arithmetically shifts X right by N bits

3.4.13 channel-close

(channel-close DESCR) closes a channel that has the specified descriptor DESCR. If no such channel exists, then returns -1. Otherwise returns 0. The descriptor of the closed channel will be reused by the consequent calls to `channel-open'. If the channel had any data associated with it and not yet flushed, then the data is discarded.

3.4.14 channel-flush

(channel-flush DESCR) forces data that were written to a channel that has the descriptor DESCR to be outputted to the associated destination. Returns -1 if no such channel exists or if in case of an IO error.

3.4.15 channel-input

(channel-input DESC) reads one byte from a channel that has the descriptor DESC. Returns -1 if no such channel exists, or if any IO error occurs, if the channel is not readable, or if the channel is in the end-of-file condition.

3.4.16 channel-open

(channel-open PTR) creates a new channel that is associated with a null-terminated path pointed by PTR. Returns a non-negative channel descriptor, if the channel subsystem have a mapping from the obtained path to a physical file and this file is accessible. Otherwise returns a negative value.

3.4.17 channel-output

(channel-output DESCR CHAR …) outputs one or more characters to a channel that has the descriptor DESCR. Returns -1 if no such channel exits, if a channel is not writable, or if any IO error occurs in an associated physical file. Otherwise, returns 0. Note: the channel system is buffered, and the actual IO operation (as well as errors) could be delayed until (channel-flush DESCR) is called.

3.4.18 concat

(concat X Y Z …) concatenates words X, Y, Z, … into one big word

3.4.19 dict-add

(dict-add DIC KEY DATA) associates DATA with KEY in the dictionary DIC. Returns KEY.

3.4.20 dict-del

(dict-del DIC KEY) deletes any association with KEY in the dictionary DIC

3.4.21 dict-first

(dict-first DIC) is the first key in DIC or nil if DIC is empty.

3.4.22 dict-get

(dict-get DIC KEY) returns data associated with KEY in the dictionary DIC, and returns NIL if either DIC doesn't exist on no data are associated

3.4.23 dict-has

(dict-has DIC KEY) returns T if the dictionary DIC has the key KEY

3.4.24 dict-length

(dict-first DIC) is the total number of keys in DIC.

3.4.25 dict-next

(dict-next DIC KEY) returns the next key after KEY Returns nil if KEY was the last key in the dictionary. Could be used together with DICT-FIRST for iterating.

3.4.26 exec-addr

(exec-addr D) passes the control flow to D and never returns

3.4.27 exec-symbol

(exec-symbol D) passes the control flow to D and never returns

3.4.28 exit-with

(exit-with N) terminates program with the exit codeN

3.4.29 extract

(extract HI LO X) extracts bits from HI to LO (including both) from the word X

3.4.30 get-current-program-counter

(get-current-program-counter) returns current program cunnter

3.4.31 ieee754-abs

applies abs to the operand

3.4.32 ieee754-acos

applies acos to the operand

3.4.33 ieee754-add

reduces the list of operands with add

3.4.34 ieee754-asin

applies asin to the operand

3.4.35 ieee754-atan

applies atan to the operand

3.4.36 ieee754-atan2

reduces the list of operands with atan2

3.4.37 ieee754-ceil

applies ceil to the operand

3.4.38 ieee754-cos

applies cos to the operand

3.4.39 ieee754-cosh

applies cosh to the operand

3.4.40 ieee754-cti

truncates to the nearest integer

3.4.41 ieee754-div

reduces the list of operands with div

3.4.42 ieee754-eq

returns true if all operands are ordered with the eq order

3.4.43 ieee754-exp

applies exp to the operand

3.4.44 ieee754-expm1

applies expm1 to the operand

3.4.45 ieee754-floor

applies floor to the operand

3.4.46 ieee754-ge

returns true if all operands are ordered with the ge order

3.4.47 ieee754-gt

returns true if all operands are ordered with the gt order

3.4.48 ieee754-hypot

reduces the list of operands with hypot

3.4.49 ieee754-is-nan

checks if is-nan holds

3.4.50 ieee754-le

returns true if all operands are ordered with the le order

3.4.51 ieee754-log

applies log to the operand

3.4.52 ieee754-log10

applies log10 to the operand

3.4.53 ieee754-log1p

applies log1p to the operand

3.4.54 ieee754-lt

returns true if all operands are ordered with the lt order

3.4.55 ieee754-mod

reduces the list of operands with mod

3.4.56 ieee754-mul

reduces the list of operands with mul

3.4.57 ieee754-ne

returns true if all operands are ordered with the ne order

3.4.58 ieee754-neg

applies neg to the operand

3.4.59 ieee754-pos

applies pos to the operand

3.4.60 ieee754-pow

reduces the list of operands with pow

3.4.61 ieee754-sin

applies sin to the operand

3.4.62 ieee754-sinh

applies sinh to the operand

3.4.63 ieee754-sqrt

applies sqrt to the operand

3.4.64 ieee754-sub

reduces the list of operands with sub

3.4.65 ieee754-tan

applies tan to the operand

3.4.66 ieee754-tanh

applies tanh to the operand

3.4.67 incident-location

3.4.68 incident-report

3.4.69 invoke-subroutine

(invoke-subroutine addr args …) calls the subroutine at the specified address.

3.4.70 is-negative

(is-negative X Y …) returns true if all arguments are negative

3.4.71 is-positive

(is-positive X Y …) returns true if all arguments are positive

3.4.72 is-zero

(is-zero X Y …) returns true if all arguments are zeros

3.4.73 lnot

(lnot X) returns the one complement of X

3.4.74 logand

(logand X Y Z …) returns X & Y & Z & … or 0 if the list of arguments is empty, where & is the bitwise AND operation. Returns ~0 if the list of arguments is empty

3.4.75 logor

(logor X Y Z …) returns X | Y | Z | … or 0 if the list of arguments is empty, where | is the bitwise OR operation

3.4.76 logxor

(logxor X Y Z …) returns X ^ Y ^ Z ^ … or 0 if the list of arguments is empty, where ^ is the bitwise XOR operation

3.4.77 lshift

(lshift X N) logically shifts X left by N bits

3.4.78 memory-allocate

(memory-allocate P N V?) maps memory region [P,P+N), if V is provided, then fills the newly mapped region with the value V

3.4.79 memory-read

(memory-read A) loads one byte from the address A

3.4.80 memory-write

(memory-write A X) stores by X to A

3.4.81 mod

(mod X Y Z …) returns X % Y % Z % … or 0 if the list of arguments is empty, where % is the modulo operation

3.4.82 neg

(neg X) returns the two complement of X

3.4.83 not

(not X) returns true if X is zero

3.4.84 points-to-static-data

(points-to-static-data PTR LEN) is true iff (all-static-constant *PTR .. *(PTR+LEN-1))

3.4.85 reg-name

(reg-name N) returns the name of the register with the index N

3.4.86 region-contains

(region-contains ID X) return the region in ID that has X. Returns the lower bound of the first region that contains value X in the set of regions with the given ID. Returns nil otherwise. Returns nil if a set with the given ID doesn't exist.

3.4.87 region-count

(region-count ID) the total number of regions in the set ID. Counts the number of regions (including intersecting) stored in the set of regions referenced by the symbol ID. Returns nil if there is no set with the given ID, otherwise returns the number of regions in that set.

3.4.88 region-create

(region-create ID LOWER UPPER) adds [LOWER,UPPER] to the set ID. Adds a region denoted with the interval [LOWER,UPPER] to the set of regions denoted by the symbol ID. Values LOWER and UPPER are included into the interval. If the set of regions ID doesn't exist, then it is created.

3.4.89 region-lower

(region-lower ID X) the lower bound of region that contains X. Returns nil if ID doesn't exist or if it doesn't have a region that includes X. This fucntion is an alias for REGION-CONTAINS. See also, REGION-UPPER.

3.4.90 region-move

(region-move DST SRC P) moves all regions that contain the point P from the set SRC to the set DST. Returns nil if SRC didn't contain any such region, otherwise returns t.

3.4.91 region-upper

(region-upper ID X) the upper bound of the region that contains X. Returns the upper bound of the region that contains point X or nil if there is no such region or such set of regions. See also, REGION-LOWER.

3.4.92 rshift

(rshift X N) logically shifts X right by N bits

3.4.93 s/

(s/ X Y Z …) returns X s/ Y s/ Z s/ … or 0 if the list of arguments is empty, where s/ is the signed division operation

3.4.94 set-symbol-value

(set-symbol-value S X) sets the value of the symbol S to X. Returns X

3.4.95 signed-mod

(signed-mod X Y Z …) returns X % Y % Z % … or 0 if the list of arguments is empty, where % is the signed modulo operation

3.4.96 symbol-concat

(symbol-concat X Y Z …) returns a new symbol that is a concatenation of symbols X,Y,Z,…

3.4.97 symbol-of-string

(symbol-of-string ptr) returns a symbol from a null-terminated string.

3.4.98 taint-get-direct

(taint-get-direct K X) returns the direct taint of the kind K associatedwith the value X, or nil if there is no such taint

3.4.99 taint-get-indirect

(taint-get-indirect K X) returns the indirect taint of the kind K associated with the value X, or nil if there is no such taint

3.4.100 taint-introduce-directly

(taint-introduce-directly K X) introduces a new taint of the kind K that is directly associated with the value X

3.4.101 taint-introduce-indirectly

(taint-introduce-indirectly K X N) introduces a new taint of the kind K that is indirectly associated with X pointing to an object of the size N

3.4.102 taint-kind

(taint-kind t) returns the kind of the taint T.

3.4.103 taint-policy-select

(taint-policy-select K P) selects the taint propagation policy P for the taints of the kind K

3.4.104 taint-policy-set-default

(taint-policy-set-default P) makes P the default taint propagation policy.

3.4.105 taint-sanitize-direct

(taint-sanitize-direct K X) removes any direct taint of the kind K that is directly associated with the value X

3.4.106 taint-sanitize-indirect

(taint-sanitize-indirect K X) removes any direct taint of the kind K that is indirectly associated with the value X

3.4.107 word-width

(word-width) returns machine word width in bits

4 Package external

4.1 Functions

4.1.1 __cxa_atexit

;; File "stdlib.lisp", line 34, characters 0-70
(defun atexit (cb)
  (declare (external "atexit" "__cxa_atexit"))
  0)

4.1.2 __errno_location

;; File "errno.lisp", line 13, characters 0-116
(defun errno-location ()
  (declare (visibility :public)
	   (external "__errno_location"))
  errno-location)

4.1.3 __libc_csu_fini

;; File "libc-init.lisp", line 73, characters 0-72
(defun fini ()
  (declare (external "__libc_csu_fini"))
  (exit-with 0))

4.1.4 __libc_init

;; File "libc-init.lisp", line 52, characters 0-155
(defun init (args on-exit main)
  "bionic initialization function"
  (declare (external "__libc_init"))
  (invoke-subroutine main args (ptr+1 ptr_t args)))

4.1.5 __libc_start_main

;; File "libc-init.lisp", line 85, characters 0-256
(defun init (main argc argv auxv)
  "GNU libc initialization stub"
  (declare (external "__libc_start_main")
	   (context (target "mips64") (abi "gnu")))
  (set R25 main)
  (set R31 @__libc_csu_fini)
  (exit-with (invoke-subroutine main argc argv)))

4.1.6 __security_init_cookie

;; File "libc-init.lisp", line 57, characters 0-198
(defun security-init-cookie ()
  "Windows CRT buffer overrun protection"
  (declare (external "__security_init_cookie")
	   (context (abi "ms"))) ; actually we should overload by runtime
  0)

4.1.7 _exit

;; File "stdlib.lisp", line 29, characters 0-76
(defun exit (code)
  (declare (external "exit" "_exit"))
  (exit-with code))

4.1.8 _longjmp

;; File "setjmp.lisp", line 11, characters 0-103
(defun longjmp (_ _)
  (declare (external "longjmp" "_longjmp" "siglongjmp" "_siglongjmp"))
  (exit 1))

4.1.9 _setjmp

;; File "setjmp.lisp", line 3, characters 0-64
(defun setjmp (_)
  (declare (external "setjmp" "_setjmp"))
  0)

4.1.10 _siglongjmp

;; File "setjmp.lisp", line 11, characters 0-103
(defun longjmp (_ _)
  (declare (external "longjmp" "_longjmp" "siglongjmp" "_siglongjmp"))
  (exit 1))

4.1.11 _sigsetjmp

;; File "setjmp.lisp", line 7, characters 0-75
(defun sigsetjmp (_ _)
  (declare (external "sigsetjmp" "_sigsetjmp"))
  0)

4.1.12 abort

;; File "stdlib.lisp", line 23, characters 0-103
(defun abort ()
  "terminates program with exit code 1"
  (declare (external "abort"))
  (exit-with 1))

4.1.13 abs

;; File "stdlib.lisp", line 38, characters 0-77
(defun abs (x)
  (declare (external "abs"))
  (if (is-negative x) (neg x) x))

4.1.14 atexit

;; File "stdlib.lisp", line 34, characters 0-70
(defun atexit (cb)
  (declare (external "atexit" "__cxa_atexit"))
  0)

4.1.15 atoi

;; File "atoi.lisp", line 31, characters 0-93
(defun atoi  (s)
  (declare (visibility :public) (external "atoi"))
  (make-converter int s))

4.1.16 atol

;; File "atoi.lisp", line 35, characters 0-94
(defun atol  (s)
  (declare (visibility :public) (external "atol"))
  (make-converter long s))

4.1.17 atoll

;; File "atoi.lisp", line 39, characters 0-100
(defun atoll (s)
  (declare (visibility :public) (external "atoll"))
  (make-converter long-long s))

4.1.18 bindtextdomain

;; File "libintl.lisp", line 3, characters 0-76
(defun bindtextdomain (_ dir)
  (declare (external "bindtextdomain"))
  dir)

4.1.19 brk

;; File "simple-memory-allocator.lisp", line 69, characters 0-66
(defun brk (val)
  (declare (external "brk"))
  (set brk val)
  0)

4.1.20 calloc

;; File "simple-memory-allocator.lisp", line 107, characters 0-202
(defun calloc (n s)
  "allocates memory and initializes it with zero"
  (declare (external "calloc"))
  (let ((*malloc-initialize-memory* true)
	(*malloc-initial-value* 0))
    (malloc (* n s))))

4.1.21 exit

;; File "stdlib.lisp", line 29, characters 0-76
(defun exit (code)
  (declare (external "exit" "_exit"))
  (exit-with code))

4.1.22 fdopen

;; File "stdio.lisp", line 37, characters 0-136
(defun fopen (path mode)
  (declare (external "fopen" "open" "fdopen"))
  (let ((file (channel-open path)))
    (if (< file 0) 0 file)))

4.1.23 fflush

;; File "stdio.lisp", line 28, characters 0-88
(defun fflush (s)
  (declare (external "fflush" "fflush_unlocked"))
  (channel-flush s))

4.1.24 fflush_unlocked

;; File "stdio.lisp", line 28, characters 0-88
(defun fflush (s)
  (declare (external "fflush" "fflush_unlocked"))
  (channel-flush s))

4.1.25 fgetc

;; File "stdio.lisp", line 106, characters 0-118
(defun fgetc (stream)
  (declare (external "fgetc" "getc" "fgetc_unlocked" "getc_unlocked"))
  (channel-input stream))

4.1.26 fgetc_unlocked

;; File "stdio.lisp", line 106, characters 0-118
(defun fgetc (stream)
  (declare (external "fgetc" "getc" "fgetc_unlocked" "getc_unlocked"))
  (channel-input stream))

4.1.27 fgets

;; File "stdio.lisp", line 115, characters 0-557
(defun fgets (ptr len str)
  (declare (external "fgets" "fgets_unlocked"))
  (if (= len 0) (terminate-string-and-return-null ptr)
    (let ((i 0)
	  (n (-1 len))
	  (continue true))
      (while (and continue (< i n))
	(let ((c (fgetc str)))
	  (if (= c -1)
	      (set continue false)
	    (memory-write (+ ptr i) (cast char c))
	    (set continue (/= c 0xA))
	    (incr i))))
      (if (= i 0)
	  (terminate-string-and-return-null ptr)
      (memory-write (+ ptr (min n i)) 0:8)
      ptr))))

4.1.28 fgets_unlocked

;; File "stdio.lisp", line 115, characters 0-557
(defun fgets (ptr len str)
  (declare (external "fgets" "fgets_unlocked"))
  (if (= len 0) (terminate-string-and-return-null ptr)
    (let ((i 0)
	  (n (-1 len))
	  (continue true))
      (while (and continue (< i n))
	(let ((c (fgetc str)))
	  (if (= c -1)
	      (set continue false)
	    (memory-write (+ ptr i) (cast char c))
	    (set continue (/= c 0xA))
	    (incr i))))
      (if (= i 0)
	  (terminate-string-and-return-null ptr)
      (memory-write (+ ptr (min n i)) 0:8)
      ptr))))

4.1.29 fileno

;; File "stdio.lisp", line 42, characters 0-64
(defun fileno (stream)
  (declare (external "fileno"))
  stream)

4.1.30 fopen

;; File "stdio.lisp", line 37, characters 0-136
(defun fopen (path mode)
  (declare (external "fopen" "open" "fdopen"))
  (let ((file (channel-open path)))
    (if (< file 0) 0 file)))

4.1.31 fputc

;; File "stdio.lisp", line 7, characters 0-146
(defun fputc (char stream)
  (declare (external "fputc" "putc" "fputs_unlocked putc_unlocked"))
  (if (= 0 (channel-output stream char)) char -1))

4.1.32 fputs

;; File "stdio.lisp", line 15, characters 0-225
(defun fputs (p stream)
  (declare (external "fputs" "fputs_unlocked"))
  (while (not (points-to-null p))
    (fputc (cast int (memory-read p)) stream)
    (incr p))
  (let ((r (fputc 0xA stream)))
    (fflush stream)
    r))

4.1.33 fputs_unlocked

;; File "stdio.lisp", line 15, characters 0-225
(defun fputs (p stream)
  (declare (external "fputs" "fputs_unlocked"))
  (while (not (points-to-null p))
    (fputc (cast int (memory-read p)) stream)
    (incr p))
  (let ((r (fputc 0xA stream)))
    (fflush stream)
    r))

4.1.34 fputs_unlocked putc_unlocked

;; File "stdio.lisp", line 7, characters 0-146
(defun fputc (char stream)
  (declare (external "fputc" "putc" "fputs_unlocked putc_unlocked"))
  (if (= 0 (channel-output stream char)) char -1))

4.1.35 fread

;; File "stdio.lisp", line 92, characters 0-208
(defun fread (ptr size n stream)
  (declare (external "fread" "fread_unlocked"))
  (let ((i 0))
    (while (and
	    (< i n)
	    (= size (input-item ptr size i stream)))
      (incr i))
    i))

4.1.36 fread_unlocked

;; File "stdio.lisp", line 92, characters 0-208
(defun fread (ptr size n stream)
  (declare (external "fread" "fread_unlocked"))
  (let ((i 0))
    (while (and
	    (< i n)
	    (= size (input-item ptr size i stream)))
      (incr i))
    i))

4.1.37 free

;; File "simple-memory-allocator.lisp", line 103, characters 0-87
(defun free (p)
  "frees the memory region pointed by P"
  (declare (external "free")))

4.1.38 fwrite

;; File "stdio.lisp", line 63, characters 0-204
(defun fwrite (buf size n stream)
  (declare (external "fwrite" "fwrite_unlocked"))
  (let ((i 0))
    (while (and (< i n)
		(= size (output-item buf size i stream)))
      (incr i))
    i))

4.1.39 fwrite_unlocked

;; File "stdio.lisp", line 63, characters 0-204
(defun fwrite (buf size n stream)
  (declare (external "fwrite" "fwrite_unlocked"))
  (let ((i 0))
    (while (and (< i n)
		(= size (output-item buf size i stream)))
      (incr i))
    i))

4.1.40 getc

;; File "stdio.lisp", line 106, characters 0-118
(defun fgetc (stream)
  (declare (external "fgetc" "getc" "fgetc_unlocked" "getc_unlocked"))
  (channel-input stream))

4.1.41 getc_unlocked

;; File "stdio.lisp", line 106, characters 0-118
(defun fgetc (stream)
  (declare (external "fgetc" "getc" "fgetc_unlocked" "getc_unlocked"))
  (channel-input stream))

4.1.42 getchar

;; File "stdio.lisp", line 149, characters 0-97
(defun getchar ()
  (declare (external "getchar" "getchar_unlocked"))
  (fgetc *standard-input*))

4.1.43 getchar_unlocked

;; File "stdio.lisp", line 149, characters 0-97
(defun getchar ()
  (declare (external "getchar" "getchar_unlocked"))
  (fgetc *standard-input*))

4.1.44 getenv

;; File "stdlib.lisp", line 9, characters 0-452
(defun getenv (name)
  "finds a value of an environment variable with the given name"
  (declare (external "getenv" "secure_getenv"))
  (let ((p environ)
	(n (strlen name)))
    (while (and (not (points-to-null p))
		(/= (memcmp (read-word ptr_t p) name n) 0))
      (set p (ptr+1 ptr_t p)))
    (if (not (points-to-null p))
	(let ((p (read-word ptr_t p)))
	  (if (= (memory-read (+ p n)) ?=) (+ p n 1) 0))
      0)))

4.1.45 getopt

;; File "getopt.lisp", line 64, characters 0-688
(defun getopt (argc argv opts)
  (declare
   (visibility :public)
   (external "getopt"))
  (when (= 0 optind)
    (set optind 1)
    (set last-ofs 0))
  (if (getopt-finished argc argv) -1
    (if (getopt-nearly-finished argv)
	(prog (incr optind) -1)
      (let ((p 0) )
	(getopt-update-optopt argv last-ofs)
	(set p (strchr opts optopt))
	(if p
	    (prog
	     (when (points-to-null p)
	       (prog (incr optind) (getopt argc argv opts)))
	     (if (getopt-expects-argument p)
		 (getopt-with-argument argv opts p last-ofs)
	       (prog (incr last-ofs) optopt)))
	   (incr optind)
	   ??)))))

4.1.46 getopt_long

;; File "getopt.lisp", line 88, characters 0-149
(defun getopt_long (argc argv opts _ _)
  (declare
   (visibility :public)
   (external "getopt_long" "getopt_long_only"))
  (getopt argc argv opts))

4.1.47 getopt_long_only

;; File "getopt.lisp", line 88, characters 0-149
(defun getopt_long (argc argv opts _ _)
  (declare
   (visibility :public)
   (external "getopt_long" "getopt_long_only"))
  (getopt argc argv opts))

4.1.48 getpagesize

;; File "unistd.lisp", line 13, characters 0-73
(defun getpagesize ()
  (declare (external "getpagesize"))
  (* 64 1024))

4.1.49 gets

;; File "stdio.lisp", line 133, characters 0-370
(defun gets (ptr)
  (declare (external "gets"))
  (let ((str *standard-input*)
	(i 0)
	(continue true))
    (while continue
      (let ((c (fgetc str)))
	(if (= c -1)
	    (set continue false)
	  (memory-write (+ ptr i) (cast char c))
	  (set continue (/= c 0xA))
	  (incr i))))
    (memory-write (+ ptr i) 0:8)
    ptr))

4.1.50 index

;; File "string.lisp", line 84, characters 0-92
(defun strchr (p c)
  (declare (external "strchr" "index"))
  (memchr p c (+ (strlen p) 1)))

4.1.51 ioctl

;; File "unistd.lisp", line 11, characters 0-58
(defun ioctl (_ _ _ _ _ _) (declare (external "ioctl")) 0)

4.1.52 isalnum

;; File "ascii.lisp", line 26, characters 0-113
(defun ascii-is-alphanum (c)
  (declare (external "isalnum"))
  (or (ascii-is-alpha c)
      (ascii-is-digit c)))

4.1.53 isalpha

;; File "ascii.lisp", line 31, characters 0-88
(defun ascii-is-alpha (c)
  (declare (external "isalpha"))
  (< (- (logor c 32) ?a) 26))

4.1.54 isatty

;; File "unistd.lisp", line 3, characters 0-62
(defun isatty (fd)
  (declare (external "isatty"))
  (< fd 3))

4.1.55 isdigit

;; File "ascii.lisp", line 21, characters 0-157
(defun ascii-is-digit (s)
  "(ascii-is-digit s) is true if S is an ascii representation of decimal digit"
  (declare (external "isdigit"))
  (< (- s ?0) 10))

4.1.56 islower

;; File "ascii.lisp", line 39, characters 0-77
(defun ascii-is-lower (c)
  (declare (external "islower"))
  (< (- c ?a) 26))

4.1.57 isupper

;; File "ascii.lisp", line 35, characters 0-77
(defun ascii-is-upper (c)
  (declare (external "isupper"))
  (< (- c ?A) 26))

4.1.58 lconv

;; File "locale.lisp", line 17, characters 0-55
(defun lconv ()
  (declare (external "lconv"))
  LCONV)

4.1.59 longjmp

;; File "setjmp.lisp", line 11, characters 0-103
(defun longjmp (_ _)
  (declare (external "longjmp" "_longjmp" "siglongjmp" "_siglongjmp"))
  (exit 1))

4.1.60 malloc

;; File "simple-memory-allocator.lisp", line 52, characters 0-618
(defun malloc (n)
  "allocates a memory region of size N"
  (declare (external "malloc"))
  (if (= n 0) *malloc-zero-sentinel*
    (if (malloc-will-reach-limit n) 0
      (malloc/grow-arena-if-needed n)
      (+= *malloc/total-bytes-allocated* n)
      (let ((header-size (sizeof int))
	    (chunk-size (+ n (* 2 *malloc-guard-edges*) header-size))
	    (ptr *malloc/brk*))
	(malloc/initialize ptr chunk-size)
	(+= *malloc/brk* chunk-size)
	(malloc/fill-edges ptr chunk-size)
	(+= ptr *malloc-guard-edges*)
	(malloc/put-chunk-size ptr n)
	(+ ptr header-size)))))

4.1.61 memccpy

;; File "string.lisp", line 53, characters 0-271
(defun memccpy (dst src c len)
  (declare (external "memccpy"))
  (let ((found 0))
    (while (and len (not found))
      (copy-byte dst src)
      (incr src)
      (decr len)
      (when (points-to char dst c)
	(set found (+ 1 dst)))
      (incr dst))
    found))

4.1.62 memchr

;; File "string.lisp", line 74, characters 0-84
(defun memchr (p c n)
  (declare (external "memchr"))
  (find-character incr p c n))

4.1.63 memcmp

;; File "string.lisp", line 127, characters 0-205
(defun memcmp (p1 p2 n)
  (declare (external "memcmp"))
  (let ((res 0) (i 0))
    (while (and (< i n) (not res))
      (set res (compare (memory-read p1) (memory-read p2)))
      (incr p1 p2 i))
    res))

4.1.64 memcpy

;; File "string.lisp", line 49, characters 0-87
(defun memcpy (dst src len)
  (declare (external "memcpy"))
  (copy-right dst src len))

4.1.65 memmove

;; File "string.lisp", line 39, characters 0-238
(defun memmove (dst src len)
  (declare (external "memmove"))
  (let ((a dst) (b src))
    (when (/= src dst)
      (if (> src dst) (copy-right a b len)
	(+= a (-1 len))
	(+= b (-1 len))
	(copy-left a b len))))
  dst)

4.1.66 memrchr

;; File "string.lisp", line 79, characters 0-86
(defun memrchr (p c n)
  (declare (external "memrchr"))
  (find-character decr p c n))

4.1.67 memset

;; File "string.lisp", line 118, characters 0-143
(defun memset (p c n)
  (declare (external "memset"))
  (let ((p p))
    (while n
      (memory-write p c)
      (incr p)
      (decr n)))
  p)

4.1.68 open

;; File "stdio.lisp", line 37, characters 0-136
(defun fopen (path mode)
  (declare (external "fopen" "open" "fdopen"))
  (let ((file (channel-open path)))
    (if (< file 0) 0 file)))

4.1.69 putc

;; File "stdio.lisp", line 7, characters 0-146
(defun fputc (char stream)
  (declare (external "fputc" "putc" "fputs_unlocked putc_unlocked"))
  (if (= 0 (channel-output stream char)) char -1))

4.1.70 putchar

;; File "stdio.lisp", line 11, characters 0-107
(defun putchar (char)
  (declare (external "putchar" "putchar_unlocked"))
  (fputc char *standard-output*))

4.1.71 putchar_unlocked

;; File "stdio.lisp", line 11, characters 0-107
(defun putchar (char)
  (declare (external "putchar" "putchar_unlocked"))
  (fputc char *standard-output*))

4.1.72 puts

;; File "stdio.lisp", line 24, characters 0-92
(defun puts (p)
  (declare (external "puts" "puts_unlocked"))
  (fputs p *standard-output*))

4.1.73 puts_unlocked

;; File "stdio.lisp", line 24, characters 0-92
(defun puts (p)
  (declare (external "puts" "puts_unlocked"))
  (fputs p *standard-output*))

4.1.74 read

;; File "stdio.lisp", line 101, characters 0-74
(defun read (fd buf n)
  (declare (external "read"))
  (fread buf 1 n fd))

4.1.75 realloc

;; File "simple-memory-allocator.lisp", line 78, characters 0-166
(defun realloc (ptr len)
  (declare (external "realloc"))
  (if (not ptr) (malloc len)
    (if (not len) (realloc/as-free ptr)
      (realloc/update-chunk ptr len))))

4.1.76 rindex

;; File "string.lisp", line 88, characters 0-96
(defun strrchr (p c)
  (declare (external "strrchr" "rindex"))
  (memrchr p c (+ (strlen p) 1)))

4.1.77 sbrk

;; File "simple-memory-allocator.lisp", line 74, characters 0-75
(defun sbrk (increment)
  (declare (external "sbrk"))
  (+= brk increment))

4.1.78 secure_getenv

;; File "stdlib.lisp", line 9, characters 0-452
(defun getenv (name)
  "finds a value of an environment variable with the given name"
  (declare (external "getenv" "secure_getenv"))
  (let ((p environ)
	(n (strlen name)))
    (while (and (not (points-to-null p))
		(/= (memcmp (read-word ptr_t p) name n) 0))
      (set p (ptr+1 ptr_t p)))
    (if (not (points-to-null p))
	(let ((p (read-word ptr_t p)))
	  (if (= (memory-read (+ p n)) ?=) (+ p n 1) 0))
      0)))

4.1.79 setjmp

;; File "setjmp.lisp", line 3, characters 0-64
(defun setjmp (_)
  (declare (external "setjmp" "_setjmp"))
  0)

4.1.80 setlocale

;; File "locale.lisp", line 13, characters 0-72
(defun setlocale (_ locale)
  (declare (external "setlocale"))
  locale)

4.1.81 siglongjmp

;; File "setjmp.lisp", line 11, characters 0-103
(defun longjmp (_ _)
  (declare (external "longjmp" "_longjmp" "siglongjmp" "_siglongjmp"))
  (exit 1))

4.1.82 sigsetjmp

;; File "setjmp.lisp", line 7, characters 0-75
(defun sigsetjmp (_ _)
  (declare (external "sigsetjmp" "_sigsetjmp"))
  0)

4.1.83 stpcpy

;; File "string.lisp", line 243, characters 0-136
(defun stpcpy (dst src)
  (declare (external "stpcpy"))
  (let ((len (strlen src)))
    (+ (memcpy dst src (+1 len)) (cast ptr_t len))))

4.1.84 strcasecmp

;; File "string.lisp", line 167, characters 0-160
(defun strcasecmp (p1 p2)
  (declare (external "strcasecmp"))
  (strncasecmp p1 p2 (min (strlen/with-null p1)
			  (strlen/with-null p2))))

4.1.85 strcasestr

;; File "string.lisp", line 185, characters 0-109
(defun strcasestr (hay needle)
  (declare (external "strcasestr"))
  (find-substring strncasecmp hay needle))

4.1.86 strcat

;; File "string.lisp", line 107, characters 0-111
(defun strcat (dst src)
  (declare (external "strcat"))
  (strcpy (+ dst (cast ptr_t (strlen dst))) src)
  dst)

4.1.87 strchr

;; File "string.lisp", line 84, characters 0-92
(defun strchr (p c)
  (declare (external "strchr" "index"))
  (memchr p c (+ (strlen p) 1)))

4.1.88 strchrnul

;; File "string.lisp", line 92, characters 0-111
(defun strchrnul (p c)
  (declare (external "strchrnul"))
  (let ((p (strchr p c)))
    (if p p (strchr p 0))))

4.1.89 strcmp

;; File "string.lisp", line 147, characters 0-152
(defun strcmp (s1 s2)
  (declare (external "strcmp" "strcoll"))
  (memcmp s1 s2 (min (strlen/with-null s1)
		     (strlen/with-null s2))))

4.1.90 strcoll

;; File "string.lisp", line 147, characters 0-152
(defun strcmp (s1 s2)
  (declare (external "strcmp" "strcoll"))
  (memcmp s1 s2 (min (strlen/with-null s1)
		     (strlen/with-null s2))))

4.1.91 strcpy

;; File "string.lisp", line 16, characters 0-180
(defun strcpy (dst src)
  (declare (external "strcpy"))
  (let ((dst dst))
    (while (not (points-to-null src))
      (copy-byte-shift dst src))
    (memory-write dst 0:8))
  dst)

4.1.92 strcspn

;; File "string.lisp", line 196, characters 0-176
(defun strcspn (str set)
  (declare (external "strcspn"))
  (let ((len 0))
    (until (or (points-to-null str)
	       (strpbrk str set))
      (incr str len))
    len))

4.1.93 strdup

;; File "string.lisp", line 33, characters 0-126
(defun strdup (src)
  (declare (external "strdup"))
  (let ((dst (malloc (+1 (strlen src)))))
    (and dst (strcpy dst src))))

4.1.94 strlen

;; File "string.lisp", line 8, characters 0-132
(defun strlen (p)
  (declare (external "strlen"))
  (let ((len 0))
    (while (not (points-to-null p))
      (incr len p))
    len))

4.1.95 strncasecmp

;; File "string.lisp", line 161, characters 0-189
(defun strncasecmp (p1 p2 n)
  (declare (external "strncasecmp"))
  (memcasecmp p1 p2 (min n
			 (strlen/with-null p1)
			 (strlen/with-null p2))))

4.1.96 strncat

;; File "string.lisp", line 112, characters 0-122
(defun strncat (dst src len)
  (declare (external "strncat"))
  (strncpy (+ dst (cast ptr_t (strlen dst))) src len)
  dst)

4.1.97 strncmp

;; File "string.lisp", line 141, characters 0-169
(defun strncmp (s1 s2 n)
  (declare (external "strncmp"))
  (memcmp s1 s2 (min n
		     (strlen/with-null s1)
		     (strlen/with-null s2))))

4.1.98 strncpy

;; File "string.lisp", line 24, characters 0-213
(defun strncpy (dst src len)
  (declare (external "strncpy"))
  (let ((dst dst))
    (while (and len (not (points-to-null src)))
      (decr len)
      (copy-byte-shift dst src))
    (memory-write dst 0:8))
  dst)

4.1.99 strpbrk

;; File "string.lisp", line 97, characters 0-248
(defun strpbrk (str set)
  (declare (external "strpbrk"))
  (let ((p set) (found 0))
    (while (and
	    (not (points-to-null p))
	    (not found))
      (set found (strchr str (cast int (memory-read p))))
      (incr p))
    found))

4.1.100 strrchr

;; File "string.lisp", line 88, characters 0-96
(defun strrchr (p c)
  (declare (external "strrchr" "rindex"))
  (memrchr p c (+ (strlen p) 1)))

4.1.101 strsep

;; File "string.lisp", line 205, characters 0-307
(defun strsep (strp sep)
  (declare (external "strsep"))
  (let ((str (read-word ptr_t strp))
	(pos (and str (+ str (strcspn str sep)))))
    (when str
      (if (points-to-null pos)
	  (write-word ptr_t strp 0)
	(memory-write pos 0)
	(write-word ptr_t strp (+1 pos))))
    str))

4.1.102 strstr

;; File "string.lisp", line 181, characters 0-97
(defun strstr (hay needle)
  (declare (external "strstr"))
  (find-substring strncmp hay needle))

4.1.103 strtok

;; File "string.lisp", line 231, characters 0-194
(defun strtok (str sep)
  (declare (external "strtok"))
  (unless *strtok-static-storage*
    (set *strtok-static-storage* (malloc (sizeof ptr_t))))
  (strtok_r str sep *strtok-static-storage*))

4.1.104 strtok_r

;; File "string.lisp", line 217, characters 0-346
(defun strtok_r (str sep ptr)
  (declare (external "strtok_r"))
  (when str (write-word ptr_t ptr str))
  (if (not ptr) ptr
    (let ((str (read-word ptr_t ptr))
	  (del (+ str (strspn str sep)))
	  (str (+ del (strcspn del sep))))
      (if (points-to-null del) 0
	(write-word ptr_t ptr str)
	(memory-write del 0)))))

4.1.105 strxfrm

;; File "string.lisp", line 238, characters 0-92
(defun strxfrm (dst src len)
  (declare (external "strxfrm"))
  (strncpy dst src len)
  len)

4.1.106 textdomain

;; File "libintl.lisp", line 7, characters 0-66
(defun textdomain (dom)
  (declare (external "textdomain"))
  dom)

4.1.107 tolower

;; File "ascii.lisp", line 43, characters 0-100
(defun ascii-to-lower (c)
  (declare (external "tolower"))
  (if (ascii-is-upper c) (logor c 32) c))

4.1.108 toupper

;; File "ascii.lisp", line 47, characters 0-103
(defun ascii-to-upper (c)
  (declare (external "toupper"))
  (if (ascii-is-lower c) (logand c 0x5f) c))

4.1.109 write

;; File "stdio.lisp", line 71, characters 0-161
(defun write (fd buf cnt)
  (declare (external "write"))
  (let ((written (fwrite buf 1 cnt fd))
	(failure (channel-flush fd)))
    (or failure written)))

5 Package posix

5.1 Constants

5.1.1 false

;; File "init.lisp", line 29, characters 0-55
(defconstant false 0:1 "false is another name for 0:1")

5.1.2 nil

;; File "init.lisp", line 30, characters 0-55
(defconstant nil false "nil is another name for false")

5.1.3 true

;; File "init.lisp", line 28, characters 0-54
(defconstant true 1:1  "true is another name for 1:1")

5.2 Functions

5.2.1 abort

;; File "stdlib.lisp", line 23, characters 0-103
(defun abort ()
  "terminates program with exit code 1"
  (declare (external "abort"))
  (exit-with 1))

5.2.2 abs

;; File "stdlib.lisp", line 38, characters 0-77
(defun abs (x)
  (declare (external "abs"))
  (if (is-negative x) (neg x) x))

5.2.3 ascii-is-alpha

;; File "ascii.lisp", line 31, characters 0-88
(defun ascii-is-alpha (c)
  (declare (external "isalpha"))
  (< (- (logor c 32) ?a) 26))

5.2.4 ascii-is-alphanum

;; File "ascii.lisp", line 26, characters 0-113
(defun ascii-is-alphanum (c)
  (declare (external "isalnum"))
  (or (ascii-is-alpha c)
      (ascii-is-digit c)))

5.2.5 ascii-is-digit

;; File "ascii.lisp", line 21, characters 0-157
(defun ascii-is-digit (s)
  "(ascii-is-digit s) is true if S is an ascii representation of decimal digit"
  (declare (external "isdigit"))
  (< (- s ?0) 10))

5.2.6 ascii-is-lower

;; File "ascii.lisp", line 39, characters 0-77
(defun ascii-is-lower (c)
  (declare (external "islower"))
  (< (- c ?a) 26))

5.2.7 ascii-is-special

;; File "ascii.lisp", line 3, characters 0-104
(defun ascii-is-special (s)
  "(ascii-special S) is true if S is an ascii special character"
  (< s 32))

5.2.8 ascii-is-upper

;; File "ascii.lisp", line 35, characters 0-77
(defun ascii-is-upper (c)
  (declare (external "isupper"))
  (< (- c ?A) 26))

5.2.9 ascii-is-whitespace

;; File "ascii.lisp", line 7, characters 0-156
(defun ascii-is-whitespace (s)
  "(ascii-is-whitespace S) is true if S is \t, \n, \r, or SPACE"
  (or (= s 9)
      (= s 10)
      (= s 13)
      (= s 32)))

5.2.10 ascii-sign

;; File "ascii.lisp", line 14, characters 0-121
(defun ascii-sign (s)
  "(ascii-sign S) is 1 if S is +, -1 if it -, or 0 otherwise"
  (case s
    ?- -1
    ?+  1
    0))

5.2.11 ascii-to-lower

;; File "ascii.lisp", line 43, characters 0-100
(defun ascii-to-lower (c)
  (declare (external "tolower"))
  (if (ascii-is-upper c) (logor c 32) c))

5.2.12 ascii-to-upper

;; File "ascii.lisp", line 47, characters 0-103
(defun ascii-to-upper (c)
  (declare (external "toupper"))
  (if (ascii-is-lower c) (logand c 0x5f) c))

5.2.13 atexit

;; File "stdlib.lisp", line 34, characters 0-70
(defun atexit (cb)
  (declare (external "atexit" "__cxa_atexit"))
  0)

5.2.14 atoi

;; File "atoi.lisp", line 31, characters 0-93
(defun atoi  (s)
  (declare (visibility :public) (external "atoi"))
  (make-converter int s))

5.2.15 atoi-prefix

;; File "atoi.lisp", line 10, characters 0-75
(defun atoi-prefix (s)
  (or (ascii-is-special s) (ascii-is-whitespace s)))

5.2.16 atoi-read-digit

;; File "atoi.lisp", line 13, characters 0-65
(defun atoi-read-digit (s)
  (cast ptr_t (- (memory-read s) ?0)))

5.2.17 atol

;; File "atoi.lisp", line 35, characters 0-94
(defun atol  (s)
  (declare (visibility :public) (external "atol"))
  (make-converter long s))

5.2.18 atoll

;; File "atoi.lisp", line 39, characters 0-100
(defun atoll (s)
  (declare (visibility :public) (external "atoll"))
  (make-converter long-long s))

5.2.19 bindtextdomain

;; File "libintl.lisp", line 3, characters 0-76
(defun bindtextdomain (_ dir)
  (declare (external "bindtextdomain"))
  dir)

5.2.20 brk

;; File "simple-memory-allocator.lisp", line 69, characters 0-66
(defun brk (val)
  (declare (external "brk"))
  (set brk val)
  0)

5.2.21 calloc

;; File "simple-memory-allocator.lisp", line 107, characters 0-202
(defun calloc (n s)
  "allocates memory and initializes it with zero"
  (declare (external "calloc"))
  (let ((*malloc-initialize-memory* true)
	(*malloc-initial-value* 0))
    (malloc (* n s))))

5.2.22 char

;; File "types.lisp", line 44, characters 0-67
(defun char ()  (declare (context (abi eabi))) (model-ilp32 'char))

5.2.23 coerce

;; File "init.lisp", line 178, characters 0-81
(defun coerce (type v)
  (if (/= (word-width v) type) (extract (-1 type) 0 v) v))

5.2.24 copy-byte

;; File "memory.lisp", line 9, characters 0-134
(defun copy-byte (dst src)
  "(copy-byte DST SRC) copies byte from
   the address SRC to DST."
  (memory-write dst (memory-read src)))

5.2.25 double

;; File "types.lisp", line 100, characters 0-20
(defun double () 64)

5.2.26 errno-location

;; File "errno.lisp", line 13, characters 0-116
(defun errno-location ()
  (declare (visibility :public)
	   (external "__errno_location"))
  errno-location)

5.2.27 exit

;; File "stdlib.lisp", line 29, characters 0-76
(defun exit (code)
  (declare (external "exit" "_exit"))
  (exit-with code))

5.2.28 fflush

;; File "stdio.lisp", line 28, characters 0-88
(defun fflush (s)
  (declare (external "fflush" "fflush_unlocked"))
  (channel-flush s))

5.2.29 fgetc

;; File "stdio.lisp", line 106, characters 0-118
(defun fgetc (stream)
  (declare (external "fgetc" "getc" "fgetc_unlocked" "getc_unlocked"))
  (channel-input stream))

5.2.30 fgets

;; File "stdio.lisp", line 115, characters 0-557
(defun fgets (ptr len str)
  (declare (external "fgets" "fgets_unlocked"))
  (if (= len 0) (terminate-string-and-return-null ptr)
    (let ((i 0)
	  (n (-1 len))
	  (continue true))
      (while (and continue (< i n))
	(let ((c (fgetc str)))
	  (if (= c -1)
	      (set continue false)
	    (memory-write (+ ptr i) (cast char c))
	    (set continue (/= c 0xA))
	    (incr i))))
      (if (= i 0)
	  (terminate-string-and-return-null ptr)
      (memory-write (+ ptr (min n i)) 0:8)
      ptr))))

5.2.31 fileno

;; File "stdio.lisp", line 42, characters 0-64
(defun fileno (stream)
  (declare (external "fileno"))
  stream)

5.2.32 fini

;; File "libc-init.lisp", line 73, characters 0-72
(defun fini ()
  (declare (external "__libc_csu_fini"))
  (exit-with 0))

5.2.33 float

;; File "types.lisp", line 101, characters 0-19
(defun float () 32)

5.2.34 fopen

;; File "stdio.lisp", line 37, characters 0-136
(defun fopen (path mode)
  (declare (external "fopen" "open" "fdopen"))
  (let ((file (channel-open path)))
    (if (< file 0) 0 file)))

5.2.35 fputc

;; File "stdio.lisp", line 7, characters 0-146
(defun fputc (char stream)
  (declare (external "fputc" "putc" "fputs_unlocked putc_unlocked"))
  (if (= 0 (channel-output stream char)) char -1))

5.2.36 fputs

;; File "stdio.lisp", line 15, characters 0-225
(defun fputs (p stream)
  (declare (external "fputs" "fputs_unlocked"))
  (while (not (points-to-null p))
    (fputc (cast int (memory-read p)) stream)
    (incr p))
  (let ((r (fputc 0xA stream)))
    (fflush stream)
    r))

5.2.37 fread

;; File "stdio.lisp", line 92, characters 0-208
(defun fread (ptr size n stream)
  (declare (external "fread" "fread_unlocked"))
  (let ((i 0))
    (while (and
	    (< i n)
	    (= size (input-item ptr size i stream)))
      (incr i))
    i))

5.2.38 free

;; File "simple-memory-allocator.lisp", line 103, characters 0-87
(defun free (p)
  "frees the memory region pointed by P"
  (declare (external "free")))

5.2.39 fwrite

;; File "stdio.lisp", line 63, characters 0-204
(defun fwrite (buf size n stream)
  (declare (external "fwrite" "fwrite_unlocked"))
  (let ((i 0))
    (while (and (< i n)
		(= size (output-item buf size i stream)))
      (incr i))
    i))

5.2.40 getchar

;; File "stdio.lisp", line 149, characters 0-97
(defun getchar ()
  (declare (external "getchar" "getchar_unlocked"))
  (fgetc *standard-input*))

5.2.41 getenv

;; File "stdlib.lisp", line 9, characters 0-452
(defun getenv (name)
  "finds a value of an environment variable with the given name"
  (declare (external "getenv" "secure_getenv"))
  (let ((p environ)
	(n (strlen name)))
    (while (and (not (points-to-null p))
		(/= (memcmp (read-word ptr_t p) name n) 0))
      (set p (ptr+1 ptr_t p)))
    (if (not (points-to-null p))
	(let ((p (read-word ptr_t p)))
	  (if (= (memory-read (+ p n)) ?=) (+ p n 1) 0))
      0)))

5.2.42 getopt

;; File "getopt.lisp", line 64, characters 0-688
(defun getopt (argc argv opts)
  (declare
   (visibility :public)
   (external "getopt"))
  (when (= 0 optind)
    (set optind 1)
    (set last-ofs 0))
  (if (getopt-finished argc argv) -1
    (if (getopt-nearly-finished argv)
	(prog (incr optind) -1)
      (let ((p 0) )
	(getopt-update-optopt argv last-ofs)
	(set p (strchr opts optopt))
	(if p
	    (prog
	     (when (points-to-null p)
	       (prog (incr optind) (getopt argc argv opts)))
	     (if (getopt-expects-argument p)
		 (getopt-with-argument argv opts p last-ofs)
	       (prog (incr last-ofs) optopt)))
	   (incr optind)
	   ??)))))

5.2.43 getopt-arg

;; File "getopt.lisp", line 9, characters 0-57
(defun getopt-arg (argv)
  (array-get ptr_t argv optind))

5.2.44 getopt-arg-char

;; File "getopt.lisp", line 12, characters 0-71
(defun getopt-arg-char (argv n)
  (array-get char (getopt-arg argv) n))

5.2.45 getopt-expects-argument

;; File "getopt.lisp", line 45, characters 0-71
(defun getopt-expects-argument (p)
  (points-to-colon (ptr+ char 1 p)))

5.2.46 getopt-finished

;; File "getopt.lisp", line 20, characters 0-180
(defun getopt-finished (argc argv)
  (or (> optind argc)
      (is-zero (getopt-arg argv))
      (not (points-to-dash (getopt-arg argv)))
      (points-to-null (getopt-arg argv))))

5.2.47 getopt-found

;; File "getopt.lisp", line 37, characters 0-123
(defun getopt-found (argv p last-ofs)
  (or (points-to-colon (ptr+ char 2 p))
      (getopt-arg-char argv (+ 2 last-ofs))))

5.2.48 getopt-missing-argument

;; File "getopt.lisp", line 48, characters 0-34
(defun getopt-missing-argument ())

5.2.49 getopt-nearly-finished

;; File "getopt.lisp", line 26, characters 0-154
(defun getopt-nearly-finished (argv)
  (let ((p (getopt-arg argv)))
    (and (points-to-dash (ptr+ char p 1))
	 (points-to-null (ptr+ char p 2)))))

5.2.50 getopt-no-argument

;; File "getopt.lisp", line 50, characters 0-127
(defun getopt-no-argument (argv opts)
  (incr optind)
  (when (not (points-to-colon opts))
    (getopt-missing-argument))
  ?:)

5.2.51 getopt-reset-optarg-if-needed

;; File "getopt.lisp", line 41, characters 0-136
(defun getopt-reset-optarg-if-needed (argv last-ofs)
  (when (points-to-null (getopt-arg-char argv (+ 2 last-ofs)))
    (set optarg 0)))

5.2.52 getopt-update-optarg

;; File "getopt.lisp", line 34, characters 0-85
(defun getopt-update-optarg (argv)
  (set optarg (array-get ptr_t argv (+1 optind))))

5.2.53 getopt-update-optopt

;; File "getopt.lisp", line 31, characters 0-96
(defun getopt-update-optopt (argv last-ofs)
  (set optopt (getopt-arg-char argv (+1 last-ofs))))

5.2.54 getopt-with-argument

;; File "getopt.lisp", line 56, characters 0-248
(defun getopt-with-argument (argv opts p last-ofs)
  (when (getopt-found argv p last-ofs)
    (getopt-reset-optarg-if-needed argv last-ofs))
  (getopt-update-optarg argv)
  (when (is-zero optarg)
    (getopt-no-argument argv opts))
  (incr optind))

5.2.55 getopt_long

;; File "getopt.lisp", line 88, characters 0-149
(defun getopt_long (argc argv opts _ _)
  (declare
   (visibility :public)
   (external "getopt_long" "getopt_long_only"))
  (getopt argc argv opts))

5.2.56 getpagesize

;; File "unistd.lisp", line 13, characters 0-73
(defun getpagesize ()
  (declare (external "getpagesize"))
  (* 64 1024))

5.2.57 gets

;; File "stdio.lisp", line 133, characters 0-370
(defun gets (ptr)
  (declare (external "gets"))
  (let ((str *standard-input*)
	(i 0)
	(continue true))
    (while continue
      (let ((c (fgetc str)))
	(if (= c -1)
	    (set continue false)
	  (memory-write (+ ptr i) (cast char c))
	  (set continue (/= c 0xA))
	  (incr i))))
    (memory-write (+ ptr i) 0:8)
    ptr))

5.2.58 init

;; File "libc-init.lisp", line 7, characters 0-159
(defun init (main argc argv auxv)
  "GNU libc initialization stub"
  (declare (external "__libc_start_main"))
  (exit-with (invoke-subroutine main argc argv)))

5.2.59 input-item

;; File "stdio.lisp", line 84, characters 0-194
(defun input-item (buf size item fd)
  (declare (visibility :private))
  (let ((i 0))
    (while (and (< i size)
		(input-item-nth-char buf size item fd i))
      (incr i))
    i))

5.2.60 input-item-nth-char

;; File "stdio.lisp", line 77, characters 0-202
(defun input-item-nth-char (ptr size item desc i)
  (declare (visibility :private))
  (let ((c (fgetc desc)))
    (if (= c -1) 0
	(memory-write (+ ptr (* size item) i) (cast char c))
	1)))

5.2.61 int

;; File "types.lisp", line 46, characters 0-66
(defun int ()   (declare (context (abi eabi))) (model-ilp32 'int))

5.2.62 int32_t

;; File "types.lisp", line 97, characters 0-21
(defun int32_t () 32)

5.2.63 int64_t

;; File "types.lisp", line 98, characters 0-21
(defun int64_t () 64)

5.2.64 ioctl

;; File "unistd.lisp", line 7, characters 0-50
(defun ioctl (_ _) (declare (external "ioctl")) 0)

5.2.65 isatty

;; File "unistd.lisp", line 3, characters 0-62
(defun isatty (fd)
  (declare (external "isatty"))
  (< fd 3))

5.2.66 lconv

;; File "locale.lisp", line 17, characters 0-55
(defun lconv ()
  (declare (external "lconv"))
  LCONV)

5.2.67 long

;; File "types.lisp", line 47, characters 0-67
(defun long ()  (declare (context (abi eabi))) (model-ilp32 'long))

5.2.68 long-long

;; File "types.lisp", line 95, characters 0-33
(defun long-long () (word-width))

5.2.69 longjmp

;; File "setjmp.lisp", line 11, characters 0-103
(defun longjmp (_ _)
  (declare (external "longjmp" "_longjmp" "siglongjmp" "_siglongjmp"))
  (exit 1))

5.2.70 malloc

;; File "simple-memory-allocator.lisp", line 52, characters 0-618
(defun malloc (n)
  "allocates a memory region of size N"
  (declare (external "malloc"))
  (if (= n 0) *malloc-zero-sentinel*
    (if (malloc-will-reach-limit n) 0
      (malloc/grow-arena-if-needed n)
      (+= *malloc/total-bytes-allocated* n)
      (let ((header-size (sizeof int))
	    (chunk-size (+ n (* 2 *malloc-guard-edges*) header-size))
	    (ptr *malloc/brk*))
	(malloc/initialize ptr chunk-size)
	(+= *malloc/brk* chunk-size)
	(malloc/fill-edges ptr chunk-size)
	(+= ptr *malloc-guard-edges*)
	(malloc/put-chunk-size ptr n)
	(+ ptr header-size)))))

5.2.71 malloc-heap-size

;; File "simple-memory-allocator.lisp", line 115, characters 0-94
(defun malloc-heap-size ()
  (declare (visibility :private))
  *malloc/total-bytes-allocated*)

5.2.72 malloc-will-reach-limit

;; File "simple-memory-allocator.lisp", line 120, characters 0-241
(defun malloc-will-reach-limit (n)
  (declare (visibility :private))
  (or (and *malloc-max-chunk-size*
	   (> n *malloc-max-chunk-size*))
      (and *malloc-max-arena-size*
	   (> (malloc-heap-size) *malloc-max-arena-size*))))

5.2.73 malloc/allocate-arena

;; File "simple-memory-allocator.lisp", line 138, characters 0-464
(defun malloc/allocate-arena (len)
  (declare (visibility :private))
  (set *malloc-arena-start* brk)
  (+= brk len)
  (set *malloc-arena-end* brk)
  (if *malloc-initialize-memory*
      (memory-allocate *malloc-arena-start*
		       len
		       *malloc-initial-value*)
    (memory-allocate *malloc-arena-start*
		     len
		     *malloc-uniform-min-value*
		     *malloc-uniform-max-value*)))

5.2.74 malloc/fill-edges

;; File "simple-memory-allocator.lisp", line 127, characters 0-298
(defun malloc/fill-edges (ptr n)
  (declare (visibility :private))
  (when *malloc-guard-edges*
    (memset ptr
	    *malloc-guard-pattern*
	    *malloc-guard-edges*)
    (memset (- (+ ptr n) *malloc-guard-edges*)
	    *malloc-guard-pattern*
	    *malloc-guard-edges*)))

5.2.75 malloc/get-chunk-size

;; File "simple-memory-allocator.lisp", line 177, characters 0-108
(defun malloc/get-chunk-size (ptr)
  (declare (visibility :private))
  (read-word int (- ptr (sizeof int))))

5.2.76 malloc/grow-arena-if-needed

;; File "simple-memory-allocator.lisp", line 162, characters 0-232
(defun malloc/grow-arena-if-needed (len)
  (declare (visibility :private))
  (let ((free-space (- *malloc-arena-end* *malloc/brk*)))
    (when (> len free-space)
      (malloc/allocate-arena (max *malloc-arena-initial-size* len)))))

5.2.77 malloc/initialize

;; File "simple-memory-allocator.lisp", line 152, characters 0-374
(defun malloc/initialize (ptr len)
  (declare (visibility :private))
  (if *malloc-initialize-memory*
      (memory-allocate ptr len *malloc-initial-value*)
    (when (or *malloc-uniform-min-value*
	      *malloc-uniform-max-value*)
      (memory-allocate ptr len
		       *malloc-uniform-min-value*
		       *malloc-uniform-max-value*))))

5.2.78 malloc/put-chunk-size

;; File "simple-memory-allocator.lisp", line 173, characters 0-100
(defun malloc/put-chunk-size (ptr len)
  (declare (visibility :private))
  (write-word int ptr len))

5.2.79 memcasecmp

;; File "string.lisp", line 152, characters 0-265
(defun memcasecmp (p1 p2 n)
  (let ((res 0) (i 0))
    (while (and (< i n) (not res))
      (set res (compare
		(ascii-to-lower (cast int (memory-read p1)))
		(ascii-to-lower (cast int (memory-read p2)))))
      (incr p1 p2 i))
    res))

5.2.80 memccpy

;; File "string.lisp", line 53, characters 0-271
(defun memccpy (dst src c len)
  (declare (external "memccpy"))
  (let ((found 0))
    (while (and len (not found))
      (copy-byte dst src)
      (incr src)
      (decr len)
      (when (points-to char dst c)
	(set found (+ 1 dst)))
      (incr dst))
    found))

5.2.81 memchr

;; File "string.lisp", line 74, characters 0-84
(defun memchr (p c n)
  (declare (external "memchr"))
  (find-character incr p c n))

5.2.82 memcmp

;; File "string.lisp", line 127, characters 0-205
(defun memcmp (p1 p2 n)
  (declare (external "memcmp"))
  (let ((res 0) (i 0))
    (while (and (< i n) (not res))
      (set res (compare (memory-read p1) (memory-read p2)))
      (incr p1 p2 i))
    res))

5.2.83 memcpy

;; File "string.lisp", line 49, characters 0-87
(defun memcpy (dst src len)
  (declare (external "memcpy"))
  (copy-right dst src len))

5.2.84 memmove

;; File "string.lisp", line 39, characters 0-238
(defun memmove (dst src len)
  (declare (external "memmove"))
  (let ((a dst) (b src))
    (when (/= src dst)
      (if (> src dst) (copy-right a b len)
	(+= a (-1 len))
	(+= b (-1 len))
	(copy-left a b len))))
  dst)

5.2.85 memrchr

;; File "string.lisp", line 79, characters 0-86
(defun memrchr (p c n)
  (declare (external "memrchr"))
  (find-character decr p c n))

5.2.86 memset

;; File "string.lisp", line 118, characters 0-143
(defun memset (p c n)
  (declare (external "memset"))
  (let ((p p))
    (while n
      (memory-write p c)
      (incr p)
      (decr n)))
  p)

5.2.87 model-ilp32

;; File "types.lisp", line 3, characters 0-103
(defun model-ilp32 (type)
  (case type
    'char 8
    'short 16
    'int 32
    'long 32
    'ptr 32))

5.2.88 model-ilp64

;; File "types.lisp", line 19, characters 0-103
(defun model-ilp64 (type)
  (case type
    'char 8
    'short 16
    'int 64
    'long 64
    'ptr 64))

5.2.89 model-llp64

;; File "types.lisp", line 27, characters 0-103
(defun model-llp64 (type)
  (case type
    'char 8
    'short 16
    'int 32
    'long 32
    'ptr 64))

5.2.90 model-lp32

;; File "types.lisp", line 11, characters 0-102
(defun model-lp32 (type)
  (case type
    'char 8
    'short 16
    'int 16
    'long 32
    'ptr 32))

5.2.91 model-lp64

;; File "types.lisp", line 35, characters 0-102
(defun model-lp64 (type)
  (case type
    'char 8
    'short 16
    'int 32
    'long 64
    'ptr 64))

5.2.92 open3

;; File "stdio.lisp", line 46, characters 0-81
(defun open3 (path flags mode)
  (declare (external "open"))
  (fopen path mode))

5.2.93 output-item

;; File "stdio.lisp", line 55, characters 0-170
(defun output-item (buf size item fd)
  (let ((i 0))
    (while (and
	    (< i size)
	    (output-item-nth-char buf size item fd i))
      (incr i))
    i))

5.2.94 output-item-nth-char

;; File "stdio.lisp", line 50, characters 0-131
(defun output-item-nth-char (ptr size item fd i)
  (= 0 (channel-output
	fd
	(memory-read (+ ptr (* size item) i)))))

5.2.95 points-to-colon

;; File "getopt.lisp", line 17, characters 0-51
(defun points-to-colon (p)
  (points-to char p ?:))

5.2.96 points-to-dash

;; File "getopt.lisp", line 15, characters 0-50
(defun points-to-dash (p)
  (points-to char p ?-))

5.2.97 points-to-null

;; File "memory.lisp", line 5, characters 0-109
(defun points-to-null (p)
  "(points-to-null P) true if P points to a zero byte"
  (is-zero (memory-read p)))

5.2.98 ptr_t

;; File "types.lisp", line 48, characters 0-66
(defun ptr_t () (declare (context (abi eabi))) (model-ilp32 'ptr))

5.2.99 putchar

;; File "stdio.lisp", line 11, characters 0-107
(defun putchar (char)
  (declare (external "putchar" "putchar_unlocked"))
  (fputc char *standard-output*))

5.2.100 puts

;; File "stdio.lisp", line 24, characters 0-92
(defun puts (p)
  (declare (external "puts" "puts_unlocked"))
  (fputs p *standard-output*))

5.2.101 read

;; File "stdio.lisp", line 101, characters 0-74
(defun read (fd buf n)
  (declare (external "read"))
  (fread buf 1 n fd))

5.2.102 read-ascii-word

;; File "atoi.lisp", line 16, characters 0-292
(defun read-ascii-word (s)
  (skip-all atoi-prefix s)
  (let ((v 0)
	(sign (ascii-sign (memory-read s))))
    (if (= sign 0)
	(set sign 1)
      (incr s))
    (while (ascii-is-digit (memory-read s))
      (set v (+ (* v 10) (atoi-read-digit s)))
      (incr s))
    (* sign v)))

5.2.103 realloc

;; File "simple-memory-allocator.lisp", line 78, characters 0-166
(defun realloc (ptr len)
  (declare (external "realloc"))
  (if (not ptr) (malloc len)
    (if (not len) (realloc/as-free ptr)
      (realloc/update-chunk ptr len))))

5.2.104 realloc/as-free

;; File "simple-memory-allocator.lisp", line 96, characters 0-101
(defun realloc/as-free (ptr)
  (declare (visibility :private))
  (free ptr)
  *malloc-zero-sentinel*)

5.2.105 realloc/shrink-chunk

;; File "simple-memory-allocator.lisp", line 168, characters 0-112
(defun realloc/shrink-chunk (ptr len)
  (declare (visibility :private))
  (malloc/put-chunk-size ptr len)
  ptr)

5.2.106 realloc/update-chunk

;; File "simple-memory-allocator.lisp", line 86, characters 0-349
(defun realloc/update-chunk (old-ptr new-len)
  (declare (visibility :private))
  (let ((old-len (malloc/get-chunk-size old-ptr)))
    (if (>= old-len new-len) (realloc/shrink-chunk old-ptr new-len)
      (let ((new-ptr (malloc new-len)))
	(when new-ptr
	  (memcpy new-ptr old-ptr old-len)
	  (free old-ptr))
	new-ptr))))

5.2.107 riscv-reset-LR-to-prevent-infinite-loop

;; File "libc-init.lisp", line 20, characters 0-199
(defun riscv-reset-LR-to-prevent-infinite-loop (main argv base other)
  (declare (context (target riscv))
	   (advice :before __libc_start_main)
	   (visibility :private))
  (set X1 0))

5.2.108 sbrk

;; File "simple-memory-allocator.lisp", line 74, characters 0-75
(defun sbrk (increment)
  (declare (external "sbrk"))
  (+= brk increment))

5.2.109 security-init-cookie

;; File "libc-init.lisp", line 57, characters 0-198
(defun security-init-cookie ()
  "Windows CRT buffer overrun protection"
  (declare (external "__security_init_cookie")
	   (context (abi "ms"))) ; actually we should overload by runtime
  0)

5.2.110 setjmp

;; File "setjmp.lisp", line 3, characters 0-64
(defun setjmp (_)
  (declare (external "setjmp" "_setjmp"))
  0)

5.2.111 setlocale

;; File "locale.lisp", line 13, characters 0-72
(defun setlocale (_ locale)
  (declare (external "setlocale"))
  locale)

5.2.112 setup-thread-local-storage

;; File "libc-init.lisp", line 27, characters 0-626
(defun setup-thread-local-storage ()
  (declare (context (abi "sysv"))
	   (global x86-64:FS_BASE))
  (let ((tcb-size (+ (* 6 (sizeof ptr_t))
		     (* 2 (sizeof int)))))
    (set FS_BASE brk)
    ;; tcbhead_t structure
    (memory-allocate brk tcb-size 0)
    (write-word ptr_t brk brk) ; tcb
    (write-word ptr_t (+ brk 0x28) 0xDEADBEEFBEAFDEAD) ; stack_guard
    ;; ptmalloc structure
    (memory-allocate (- brk 0x28) (sizeof ptr_t) 0) ; arena
    (memory-allocate (- brk 0x38) (sizeof ptr_t) 0) ; freelist
    ;; misc
    (memory-allocate (- brk 0x40) (sizeof int) 0) ; errno
    (+= brk tcb-size)))

5.2.113 short

;; File "types.lisp", line 45, characters 0-68
(defun short () (declare (context (abi eabi))) (model-ilp32 'short))

5.2.114 sigsetjmp

;; File "setjmp.lisp", line 7, characters 0-75
(defun sigsetjmp (_ _)
  (declare (external "sigsetjmp" "_sigsetjmp"))
  0)

5.2.115 stpcpy

;; File "string.lisp", line 243, characters 0-136
(defun stpcpy (dst src)
  (declare (external "stpcpy"))
  (let ((len (strlen src)))
    (+ (memcpy dst src (+1 len)) (cast ptr_t len))))

5.2.116 strcasecmp

;; File "string.lisp", line 167, characters 0-160
(defun strcasecmp (p1 p2)
  (declare (external "strcasecmp"))
  (strncasecmp p1 p2 (min (strlen/with-null p1)
			  (strlen/with-null p2))))

5.2.117 strcasestr

;; File "string.lisp", line 185, characters 0-109
(defun strcasestr (hay needle)
  (declare (external "strcasestr"))
  (find-substring strncasecmp hay needle))

5.2.118 strcat

;; File "string.lisp", line 107, characters 0-111
(defun strcat (dst src)
  (declare (external "strcat"))
  (strcpy (+ dst (cast ptr_t (strlen dst))) src)
  dst)

5.2.119 strchr

;; File "string.lisp", line 84, characters 0-92
(defun strchr (p c)
  (declare (external "strchr" "index"))
  (memchr p c (+ (strlen p) 1)))

5.2.120 strchrnul

;; File "string.lisp", line 92, characters 0-111
(defun strchrnul (p c)
  (declare (external "strchrnul"))
  (let ((p (strchr p c)))
    (if p p (strchr p 0))))

5.2.121 strcmp

;; File "string.lisp", line 147, characters 0-152
(defun strcmp (s1 s2)
  (declare (external "strcmp" "strcoll"))
  (memcmp s1 s2 (min (strlen/with-null s1)
		     (strlen/with-null s2))))

5.2.122 strcpy

;; File "string.lisp", line 16, characters 0-180
(defun strcpy (dst src)
  (declare (external "strcpy"))
  (let ((dst dst))
    (while (not (points-to-null src))
      (copy-byte-shift dst src))
    (memory-write dst 0:8))
  dst)

5.2.123 strcspn

;; File "string.lisp", line 196, characters 0-176
(defun strcspn (str set)
  (declare (external "strcspn"))
  (let ((len 0))
    (until (or (points-to-null str)
	       (strpbrk str set))
      (incr str len))
    len))

5.2.124 strdup

;; File "string.lisp", line 33, characters 0-126
(defun strdup (src)
  (declare (external "strdup"))
  (let ((dst (malloc (+1 (strlen src)))))
    (and dst (strcpy dst src))))

5.2.125 strlen

;; File "string.lisp", line 8, characters 0-132
(defun strlen (p)
  (declare (external "strlen"))
  (let ((len 0))
    (while (not (points-to-null p))
      (incr len p))
    len))

5.2.126 strlen/with-null

;; File "string.lisp", line 135, characters 0-163
(defun strlen/with-null (s)
  "returns a length of the string S
   (including the terminating null character)"
  (declare (visibility :private))
  (+1 (strlen s)))

5.2.127 strncasecmp

;; File "string.lisp", line 161, characters 0-189
(defun strncasecmp (p1 p2 n)
  (declare (external "strncasecmp"))
  (memcasecmp p1 p2 (min n
			 (strlen/with-null p1)
			 (strlen/with-null p2))))

5.2.128 strncat

;; File "string.lisp", line 112, characters 0-122
(defun strncat (dst src len)
  (declare (external "strncat"))
  (strncpy (+ dst (cast ptr_t (strlen dst))) src len)
  dst)

5.2.129 strncmp

;; File "string.lisp", line 141, characters 0-169
(defun strncmp (s1 s2 n)
  (declare (external "strncmp"))
  (memcmp s1 s2 (min n
		     (strlen/with-null s1)
		     (strlen/with-null s2))))

5.2.130 strncpy

;; File "string.lisp", line 24, characters 0-213
(defun strncpy (dst src len)
  (declare (external "strncpy"))
  (let ((dst dst))
    (while (and len (not (points-to-null src)))
      (decr len)
      (copy-byte-shift dst src))
    (memory-write dst 0:8))
  dst)

5.2.131 strpbrk

;; File "string.lisp", line 97, characters 0-248
(defun strpbrk (str set)
  (declare (external "strpbrk"))
  (let ((p set) (found 0))
    (while (and
	    (not (points-to-null p))
	    (not found))
      (set found (strchr str (cast int (memory-read p))))
      (incr p))
    found))

5.2.132 strrchr

;; File "string.lisp", line 88, characters 0-96
(defun strrchr (p c)
  (declare (external "strrchr" "rindex"))
  (memrchr p c (+ (strlen p) 1)))

5.2.133 strsep

;; File "string.lisp", line 205, characters 0-307
(defun strsep (strp sep)
  (declare (external "strsep"))
  (let ((str (read-word ptr_t strp))
	(pos (and str (+ str (strcspn str sep)))))
    (when str
      (if (points-to-null pos)
	  (write-word ptr_t strp 0)
	(memory-write pos 0)
	(write-word ptr_t strp (+1 pos))))
    str))

5.2.134 strstr

;; File "string.lisp", line 181, characters 0-97
(defun strstr (hay needle)
  (declare (external "strstr"))
  (find-substring strncmp hay needle))

5.2.135 strtok

;; File "string.lisp", line 231, characters 0-194
(defun strtok (str sep)
  (declare (external "strtok"))
  (unless *strtok-static-storage*
    (set *strtok-static-storage* (malloc (sizeof ptr_t))))
  (strtok_r str sep *strtok-static-storage*))

5.2.136 strtok_r

;; File "string.lisp", line 217, characters 0-346
(defun strtok_r (str sep ptr)
  (declare (external "strtok_r"))
  (when str (write-word ptr_t ptr str))
  (if (not ptr) ptr
    (let ((str (read-word ptr_t ptr))
	  (del (+ str (strspn str sep)))
	  (str (+ del (strcspn del sep))))
      (if (points-to-null del) 0
	(write-word ptr_t ptr str)
	(memory-write del 0)))))

5.2.137 strxfrm

;; File "string.lisp", line 238, characters 0-92
(defun strxfrm (dst src len)
  (declare (external "strxfrm"))
  (strncpy dst src len)
  len)

5.2.138 terminate-string-and-return-null

;; File "stdio.lisp", line 110, characters 0-109
(defun terminate-string-and-return-null (ptr)
  (declare (visibility :private))
  (memory-write ptr 0:8)
  0)

5.2.139 textdomain

;; File "libintl.lisp", line 7, characters 0-66
(defun textdomain (dom)
  (declare (external "textdomain"))
  dom)

5.2.140 write

;; File "stdio.lisp", line 71, characters 0-161
(defun write (fd buf cnt)
  (declare (external "write"))
  (let ((written (fwrite buf 1 cnt fd))
	(failure (channel-flush fd)))
    (or failure written)))

5.3 Macros

5.3.1 +1

;; File "init.lisp", line 89, characters 0-65
(defmacro +1 (x)
  "(+1 x) returns the successor of X"
  (+ x 1))

5.3.2 +=

;; File "init.lisp", line 81, characters 0-89
(defmacro += (x y)
  "(+= X Y) assigns a sum of X and Y to variable X"
  (set x (+ x y)))

5.3.3 -1

;; File "init.lisp", line 85, characters 0-67
(defmacro -1 (x)
  "(-1 X) returns the predecessor of X"
  (- x 1))

5.3.4 and

;; File "init.lisp", line 106, characters 0-194
(defmacro and (x)
  "(and X Y ...) evaluates expressions X,Y,... until the first
  expression that returns false. The value of the whole form is the
  value of the last evaluated expression." x)

5.3.5 array-get

;; File "pointers.lisp", line 67, characters 0-153
(defmacro array-get (t p n)
  "(array-get T P N) gets the N-th element of the
   array of elements of type T, pointed by P"
  (read-word t (ptr+ t p n)))

5.3.6 array-set

;; File "pointers.lisp", line 72, characters 0-205
(defmacro array-set (t p n w)
  "(array-set T P N W) sets to W the N-th element of the array of
   elements of type T, pointed by P. Returns a pointer to the next
   element"
  (write-word t (ptr+ p n) w))

5.3.7 assert

;; File "init.lisp", line 136, characters 0-164
(defmacro assert (c)
  "(assert COND) terminates program if COND is false"
  (when (not c)
    (msg "Assertion (assert $0) failed" c)
    (error "Assert_failure")))

5.3.8 assert-msg

;; File "init.lisp", line 142, characters 0-170
(defmacro assert-msg (c s)
  "(assert-msg c s) allows you to assert a condition and print a message on failure"
  (when (not c)
    (msg s)
    (error "Assert_failure")))

5.3.9 bitwidth

;; File "init.lisp", line 176, characters 0-33
(defmacro bitwidth (type) (type))

5.3.10 case

;; File "init.lisp", line 51, characters 0-783
(defmacro case (k x xs)
  "(case K K1 X1 K2 X2 ... Kn Xn [DEF]) evaluates K then consequently
   evaluates keys K1 ...Kn until a key Ki such that (= K Ki) is found.
   If such key is found then the whole form evaluates to Xi.
   If no matching key was found, then if the number of keys is equal to
   the number of expressions, nil is returned, otherwise, i.e., if an
   extra expression DEF is provided, then it becomes the value of the
   whole form.

   Examples:

       (defun dispatch-with-default-case (c)
	 (case c
	   1 'hello
	   2 'cruel
	   3 'world
	   'unknown))

       (defun dispatch-with-no-default (c)
	 (case c
	   1 'hello
	   2 'cruel
	   3 'world))"
  (let ((key k))
    (case/dispatch key x xs)))

5.3.11 case/dispatch

;; File "init.lisp", line 170, characters 0-32
(defmacro case/dispatch (k x) x)

5.3.12 cast

;; File "init.lisp", line 175, characters 0-51
(defmacro cast (type x) (extract  (-1 (type)) 0 x))

5.3.13 compare

;; File "init.lisp", line 131, characters 0-134
(defmacro compare (x y)
  "(compare X Y) returns 0 if X = Y, a negative value if X<Y
   and a positive value if X>Y"
  (sign (- x y)))

5.3.14 copy-byte-shift

;; File "memory.lisp", line 14, characters 0-177
(defmacro copy-byte-shift (dst src)
  "(copy-byte-shift DST SRC) copies byte from
   SRC to DST and increments SRC and DST."
  (prog (copy-byte dst src)
	(incr dst src)))

5.3.15 copy-byte-shift-left

;; File "memory.lisp", line 20, characters 0-187
(defmacro copy-byte-shift-left (dst src)
  "(copy-byte-shift-left DST SRC) copies
   byte from DST to SRC and decrements SRC and DST."
  (prog (copy-byte dst src)
	(decr dst src)))

5.3.16 copy-left

;; File "memory.lisp", line 39, characters 0-162
(defmacro copy-left (dst src len)
  "(copy-left DST SRC LEN) copies LEN bytes
    from SRC to DST (right to left)"
  (make-copy copy-byte-shift-left dst src len))

5.3.17 copy-right

;; File "memory.lisp", line 34, characters 0-159
(defmacro copy-right (dst src len)
  "(copy-right DST SRC LEN) copies LEN bytes
    from SRC to DST (left to right)"
  (make-copy copy-byte-shift dst src len))

5.3.18 decr

;; File "init.lisp", line 100, characters 0-111
(defmacro decr (x)
  "(decr X Y ...) decrements the value bound with the variables X, Y, ..."
  (set x (-1 x)))

5.3.19 endian

;; File "pointers.lisp", line 16, characters 0-154
(defmacro endian (f x y)
  "(endian F X Y) expands to (F Y X) if applied
   in the little endian context"
  (declare (context (endian little)))
  (f y x))

5.3.20 find-character

;; File "string.lisp", line 66, characters 0-197
(defmacro find-character (dir p c n)
  (declare (visibility :private))
  (prog
      (while (and n (not (points-to char p c)))
	(decr n)
	(dir p))
     (if (points-to char p c) p 0)))

5.3.21 find-substring

;; File "string.lisp", line 173, characters 0-240
(defmacro find-substring (compare hay needle)
  (declare (visibility :private))
  (let ((found 0)
	(n (strlen needle)))
    (while (and (memory-read hay) (not found))
      (set found (not (compare hay needle n)))
      (incr hay))))

5.3.22 fold

;; File "init.lisp", line 154, characters 0-85
(defmacro fold (f a x)
  "(fold F A X Y ...) expands to (F (F A X) Y) ..."
  (f a x))

5.3.23 incr

;; File "init.lisp", line 93, characters 0-111
(defmacro incr (x)
  "(incr X Y ...) increments the value bound with the variables X, Y, ..."
  (set x (+1 x)))

5.3.24 is-in

;; File "init.lisp", line 148, characters 0-102
(defmacro is-in (x y)
  "(is-in X A B C ...) returns true if X is equal A or B or C or ..."
  (= x y))

5.3.25 make-converter

;; File "atoi.lisp", line 28, characters 0-68
(defmacro make-converter (type s)
  (cast type (read-ascii-word s)))

5.3.26 make-copy

;; File "memory.lisp", line 26, characters 0-146
(defmacro make-copy (copy-byte dst src len)
  "<internal>"
  (let ((ret dst))
    (while len
      (decr len)
      (copy-byte dst src))
    ret))

5.3.27 max

;; File "init.lisp", line 164, characters 0-85
(defmacro max (x)
  "(max X Y ...) returns the upper bound of the (X,Y,...) set"
  x)

5.3.28 min

;; File "init.lisp", line 159, characters 0-89
(defmacro min (x)
  "(min X Y ...) returns the lower bound of the (X,Y,...) set"
      x)

5.3.29 non-zero

;; File "init.lisp", line 77, characters 0-85
(defmacro non-zero (x)
  "(non-zero X) is true if X is not zero"
  (not (is-zero x)))

5.3.30 nth-byte-of-word

;; File "pointers.lisp", line 28, characters 0-284
(defmacro nth-byte-of-word (t i x)
  "(nth-byte-of-word T N X) returns N-th byte
   of the word X that has type T"
  (let ((n (sizeof t))
	(j (endian - n i))
	(k (if (< j n) (- j 1) (+ j n)))
	(hi (- (* 8 (+ k 1)) 1))
	(lo (* k 8)))
    (extract hi lo x)))

5.3.31 or

;; File "init.lisp", line 113, characters 0-346
(defmacro or (x xs)
  "(or <expr> ...) evaluates a sequence of expressions EXPR from left
   to right until it meets the first expression that evaluates to the
   truth value, that will become the value of the whole form. If no
   expression returned the truth value, then the result of the whole
   form is 0:1"
  (let ((r x)) (if r r (or xs))))

5.3.32 points-to

;; File "pointers.lisp", line 62, characters 0-152
(defmacro points-to (t p v)
  "(points-to T P V) return true if t P points
  to a value of type T that is equal to V."
  (= (read-word t p) (cast t v)))

5.3.33 ptr+

;; File "pointers.lisp", line 6, characters 0-124
(defmacro ptr+ (t p n)
  "(ptr+ T P N) increments N times the
    pointer P to a value of type T."
  (+ p (* (sizeof t) n)))

5.3.34 ptr+1

;; File "pointers.lisp", line 11, characters 0-112
(defmacro ptr+1 (type p)
  "(ptr+1 T P) increments the pointer P to
   a value of to type T."
  (ptr+ type p 1))

5.3.35 read-word

;; File "pointers.lisp", line 38, characters 0-252
(defmacro read-word (t a)
  "(read-word T A) reads a word of type T at address A"
  (let ((p a)
	(x (memory-read p))
	(n (-1 (sizeof t))))
    (while n
      (incr p)
      (decr n)
      (set x (endian concat x (memory-read p))))
    x))

5.3.36 set$

;; File "init.lisp", line 122, characters 0-152
(defmacro set$ (s x)
  "(set$ s x) set the value of the symbol s to x, returns x.
   Like set but without implicit quotation."
  (set-symbol-value s x))

5.3.37 sign

;; File "init.lisp", line 127, characters 0-106
(defmacro sign (x)
  "returns 1 if X > 0, 0 if X = 0, and -1 if X < 0"
  (if (< x 0) -1 (if (> x 0) 1 0)))

5.3.38 sizeof

;; File "init.lisp", line 177, characters 0-46
(defmacro sizeof (type) (/ (bitwidth type) 8))

5.3.39 skip-all

;; File "atoi.lisp", line 7, characters 0-70
(defmacro skip-all (pred s)
  (while (pred (memory-read s)) (incr s)))

5.3.40 strspn

;; File "string.lisp", line 189, characters 0-136
(defmacro strspn (str set)
  (declare (external "strspn"))
  (let ((len 0))
    (while (strpbrk str set)
      (incr str len))
    len))

5.3.41 unless

;; File "init.lisp", line 38, characters 0-267
(defmacro unless (cnd body)
  "(unless CND BODY) if CND evaluates to false, then BODY is evaluated and
   the value of the last expression in BODY becomes the value of the
   whole expression. Otherwise, if CND evaluates to true, nil is returned."
  (if cnd () body))

5.3.42 until

;; File "init.lisp", line 44, characters 0-349
(defmacro until (c b)
  "(until COND BODY) if COND evaluates to true, then the whole expression
   evaluates to nil and BODY is not evaluated. Otherwise, if COND evaluates
   to false, then BODY is evaluated until COND evaluates to true and the value
   of the last evaluation of BODY becomes the value of the whole expression."
  (while (not c) b))

5.3.43 when

;; File "init.lisp", line 32, characters 0-270
(defmacro when (cnd body)
  "(when CND BODY) if CND evaluates to true, then BODY is evaluated and
   the value of the last expression in BODY becomes the value of the
   whole expression. Otherwise, if CND evaluates to false, nil is returned."
  (if cnd (prog body) ()))

5.3.44 write-word

;; File "pointers.lisp", line 49, characters 0-329
(defmacro write-word (t a x)
  "(write-word T A X) writes the word X of type T to address A
  returns an address that points to the first byte that follows
  the just written word.
  "
  (let ((p a)
	(n (sizeof t))
	(i 0))
    (while (< i n)
      (memory-write p (nth-byte-of-word t i x))
      (incr p i))
    p))

5.4 Methods

5.4.1 init

;; File "locale.lisp", line 9, characters 0-61
(defmethod init ()
  (set LCONV brk)
  (+= brk *lconv-size*))

5.4.2 machine-kill

;; File "stdio.lisp", line 153, characters 0-105
(defmethod primus:machine-kill ()
  (channel-flush *standard-output*)
  (channel-flush *standard-error*))

5.5 Parameters

5.5.1 *lconv-size*

;; File "locale.lisp", line 5, characters 0-49
(defparameter *lconv-size* (* 20 (sizeof ptr_t)))

5.5.2 *malloc-arena-end*

;; File "simple-memory-allocator.lisp", line 21, characters 0-82
(defparameter *malloc-arena-end* brk
  "the starting address of the malloc arena")

5.5.3 *malloc-arena-initial-size*

;; File "simple-memory-allocator.lisp", line 14, characters 0-149
(defparameter *malloc-arena-initial-size* 0x40000
  "the maximum number of bytes totally allocated by malloc,
   if not set, then there is no limit")

5.5.4 *malloc-arena-start*

;; File "simple-memory-allocator.lisp", line 18, characters 0-84
(defparameter *malloc-arena-start* brk
  "the starting address of the malloc arena")

5.5.5 *malloc-guard-edges*

;; File "simple-memory-allocator.lisp", line 24, characters 0-119
(defparameter *malloc-guard-edges* 0
  "if not nil, then add padding of the specified size
   around allocated chunks")

5.5.6 *malloc-guard-pattern*

;; File "simple-memory-allocator.lisp", line 28, characters 0-91
(defparameter *malloc-guard-pattern* 0xA5
  "a byte that will be used to fill guard edges")

5.5.7 *malloc-initial-value*

;; File "simple-memory-allocator.lisp", line 45, characters 0-91
(defparameter *malloc-initial-value* 0
  "initialize allocated memory with the said value")

5.5.8 *malloc-initialize-memory*

;; File "simple-memory-allocator.lisp", line 34, characters 0-120
(defparameter *malloc-initialize-memory* false
  "if true then initialize allocated memory with *malloc-initial-value*")

5.5.9 *malloc-max-arena-size*

;; File "simple-memory-allocator.lisp", line 10, characters 0-141
(defparameter *malloc-max-arena-size* nil
  "the maximum number of bytes totally allocated by malloc,
   if not set, then there is no limit")

5.5.10 *malloc-max-chunk-size*

;; File "simple-memory-allocator.lisp", line 6, characters 0-124
(defparameter *malloc-max-chunk-size* nil
  "the maximum size of a single memory chunk,
   if nil then there is no limit. ")

5.5.11 *malloc-uniform-max-value*

;; File "simple-memory-allocator.lisp", line 41, characters 0-180
(defparameter *malloc-uniform-max-value* nil
  "if set then defines the lower bound of the uniformely distributed
   random value that is used to represent an unitialized memory ")

5.5.12 *malloc-uniform-min-value*

;; File "simple-memory-allocator.lisp", line 37, characters 0-180
(defparameter *malloc-uniform-min-value* nil
  "if set then defines the lower bound of the uniformely distributed
   random value that is used to represent an unitialized memory ")

5.5.13 *malloc-zero-sentinel*

;; File "simple-memory-allocator.lisp", line 31, characters 0-84
(defparameter *malloc-zero-sentinel* 0
  "a pointer that is returned by (malloc 0)")

5.5.14 *malloc/brk*

;; File "simple-memory-allocator.lisp", line 48, characters 0-31
(defparameter *malloc/brk* brk)

5.5.15 *malloc/total-bytes-allocated*

;; File "simple-memory-allocator.lisp", line 50, characters 0-47
(defparameter *malloc/total-bytes-allocated* 0)

5.5.16 *strtok-static-storage*

;; File "string.lisp", line 229, characters 0-40
(defparameter *strtok-static-storage* 0)

5.6 Primitives

5.6.1 *

(* X Y Z …) returns the product of arguments or 0 if the list of arguments is empty

5.6.2 +

(+ X Y …) returns the sum of arguments, or 0 if there are no arguments,

5.6.3 -

(- X Y Z …) returns X - Y - Z - …, or 0 if there are no arguments.

5.6.4 /

(/ X Y Z …) returns X / Y / Z / … or 0 if the list of arguments is empty

5.6.5 /=

(/= X Y Z …) returns true if at least one argument is not equal to another argument. Returns false if the list of arguments is empty

5.6.6 <

(< X Y Z …) is true if the list of arguments is an strict ascending chain or if it is empty

5.6.7 <=

(< X Y Z …) is true if the list of arguments is an ascending chain or if it is empty

5.6.8 =

(= X Y Z …) returns true if all arguments are equal. True if the list of arguments is empty

5.6.9 >

(< X Y Z …) is true if the list of arguments is a strict descending chain or if it is empty

5.6.10 >=

(< X Y Z …) is true if the list of arguments is a descending chain or if it is empty

5.6.11 all-static-constant

(all-static-constant X Y ..) is true if X,Y,… are static constants. A value is a static constant if it was initialized from a constant value or computed from static constant values.

5.6.12 arshift

(arshift X N) arithmetically shifts X right by N bits

5.6.13 channel-close

(channel-close DESCR) closes a channel that has the specified descriptor DESCR. If no such channel exists, then returns -1. Otherwise returns 0. The descriptor of the closed channel will be reused by the consequent calls to `channel-open'. If the channel had any data associated with it and not yet flushed, then the data is discarded.

5.6.14 channel-flush

(channel-flush DESCR) forces data that were written to a channel that has the descriptor DESCR to be outputted to the associated destination. Returns -1 if no such channel exists or if in case of an IO error.

5.6.15 channel-input

(channel-input DESC) reads one byte from a channel that has the descriptor DESC. Returns -1 if no such channel exists, or if any IO error occurs, if the channel is not readable, or if the channel is in the end-of-file condition.

5.6.16 channel-open

(channel-open PTR) creates a new channel that is associated with a null-terminated path pointed by PTR. Returns a non-negative channel descriptor, if the channel subsystem have a mapping from the obtained path to a physical file and this file is accessible. Otherwise returns a negative value.

5.6.17 channel-output

(channel-output DESCR CHAR …) outputs one or more characters to a channel that has the descriptor DESCR. Returns -1 if no such channel exits, if a channel is not writable, or if any IO error occurs in an associated physical file. Otherwise, returns 0. Note: the channel system is buffered, and the actual IO operation (as well as errors) could be delayed until (channel-flush DESCR) is called.

5.6.18 concat

(concat X Y Z …) concatenates words X, Y, Z, … into one big word

5.6.19 dict-add

(dict-add DIC KEY DATA) associates DATA with KEY in the dictionary DIC. Returns KEY.

5.6.20 dict-del

(dict-del DIC KEY) deletes any association with KEY in the dictionary DIC

5.6.21 dict-first

(dict-first DIC) is the first key in DIC or nil if DIC is empty.

5.6.22 dict-get

(dict-get DIC KEY) returns data associated with KEY in the dictionary DIC, and returns NIL if either DIC doesn't exist on no data are associated

5.6.23 dict-has

(dict-has DIC KEY) returns T if the dictionary DIC has the key KEY

5.6.24 dict-length

(dict-first DIC) is the total number of keys in DIC.

5.6.25 dict-next

(dict-next DIC KEY) returns the next key after KEY Returns nil if KEY was the last key in the dictionary. Could be used together with DICT-FIRST for iterating.

5.6.26 exec-addr

(exec-addr D) passes the control flow to D and never returns

5.6.27 exec-symbol

(exec-symbol D) passes the control flow to D and never returns

5.6.28 exit-with

(exit-with N) terminates program with the exit codeN

5.6.29 extract

(extract HI LO X) extracts bits from HI to LO (including both) from the word X

5.6.30 get-current-program-counter

(get-current-program-counter) returns current program cunnter

5.6.31 ieee754-abs

applies abs to the operand

5.6.32 ieee754-acos

applies acos to the operand

5.6.33 ieee754-add

reduces the list of operands with add

5.6.34 ieee754-asin

applies asin to the operand

5.6.35 ieee754-atan

applies atan to the operand

5.6.36 ieee754-atan2

reduces the list of operands with atan2

5.6.37 ieee754-ceil

applies ceil to the operand

5.6.38 ieee754-cos

applies cos to the operand

5.6.39 ieee754-cosh

applies cosh to the operand

5.6.40 ieee754-cti

truncates to the nearest integer

5.6.41 ieee754-div

reduces the list of operands with div

5.6.42 ieee754-eq

returns true if all operands are ordered with the eq order

5.6.43 ieee754-exp

applies exp to the operand

5.6.44 ieee754-expm1

applies expm1 to the operand

5.6.45 ieee754-floor

applies floor to the operand

5.6.46 ieee754-ge

returns true if all operands are ordered with the ge order

5.6.47 ieee754-gt

returns true if all operands are ordered with the gt order

5.6.48 ieee754-hypot

reduces the list of operands with hypot

5.6.49 ieee754-is-nan

checks if is-nan holds

5.6.50 ieee754-le

returns true if all operands are ordered with the le order

5.6.51 ieee754-log

applies log to the operand

5.6.52 ieee754-log10

applies log10 to the operand

5.6.53 ieee754-log1p

applies log1p to the operand

5.6.54 ieee754-lt

returns true if all operands are ordered with the lt order

5.6.55 ieee754-mod

reduces the list of operands with mod

5.6.56 ieee754-mul

reduces the list of operands with mul

5.6.57 ieee754-ne

returns true if all operands are ordered with the ne order

5.6.58 ieee754-neg

applies neg to the operand

5.6.59 ieee754-pos

applies pos to the operand

5.6.60 ieee754-pow

reduces the list of operands with pow

5.6.61 ieee754-sin

applies sin to the operand

5.6.62 ieee754-sinh

applies sinh to the operand

5.6.63 ieee754-sqrt

applies sqrt to the operand

5.6.64 ieee754-sub

reduces the list of operands with sub

5.6.65 ieee754-tan

applies tan to the operand

5.6.66 ieee754-tanh

applies tanh to the operand

5.6.67 incident-location

5.6.68 incident-report

5.6.69 invoke-subroutine

(invoke-subroutine addr args …) calls the subroutine at the specified address.

5.6.70 is-negative

(is-negative X Y …) returns true if all arguments are negative

5.6.71 is-positive

(is-positive X Y …) returns true if all arguments are positive

5.6.72 is-zero

(is-zero X Y …) returns true if all arguments are zeros

5.6.73 lnot

(lnot X) returns the one complement of X

5.6.74 logand

(logand X Y Z …) returns X & Y & Z & … or 0 if the list of arguments is empty, where & is the bitwise AND operation. Returns ~0 if the list of arguments is empty

5.6.75 logor

(logor X Y Z …) returns X | Y | Z | … or 0 if the list of arguments is empty, where | is the bitwise OR operation

5.6.76 logxor

(logxor X Y Z …) returns X ^ Y ^ Z ^ … or 0 if the list of arguments is empty, where ^ is the bitwise XOR operation

5.6.77 lshift

(lshift X N) logically shifts X left by N bits

5.6.78 memory-allocate

(memory-allocate P N V?) maps memory region [P,P+N), if V is provided, then fills the newly mapped region with the value V

5.6.79 memory-read

(memory-read A) loads one byte from the address A

5.6.80 memory-write

(memory-write A X) stores by X to A

5.6.81 mod

(mod X Y Z …) returns X % Y % Z % … or 0 if the list of arguments is empty, where % is the modulo operation

5.6.82 neg

(neg X) returns the two complement of X

5.6.83 not

(not X) returns true if X is zero

5.6.84 points-to-static-data

(points-to-static-data PTR LEN) is true iff (all-static-constant *PTR .. *(PTR+LEN-1))

5.6.85 reg-name

(reg-name N) returns the name of the register with the index N

5.6.86 region-contains

(region-contains ID X) return the region in ID that has X. Returns the lower bound of the first region that contains value X in the set of regions with the given ID. Returns nil otherwise. Returns nil if a set with the given ID doesn't exist.

5.6.87 region-count

(region-count ID) the total number of regions in the set ID. Counts the number of regions (including intersecting) stored in the set of regions referenced by the symbol ID. Returns nil if there is no set with the given ID, otherwise returns the number of regions in that set.

5.6.88 region-create

(region-create ID LOWER UPPER) adds [LOWER,UPPER] to the set ID. Adds a region denoted with the interval [LOWER,UPPER] to the set of regions denoted by the symbol ID. Values LOWER and UPPER are included into the interval. If the set of regions ID doesn't exist, then it is created.

5.6.89 region-lower

(region-lower ID X) the lower bound of region that contains X. Returns nil if ID doesn't exist or if it doesn't have a region that includes X. This fucntion is an alias for REGION-CONTAINS. See also, REGION-UPPER.

5.6.90 region-move

(region-move DST SRC P) moves all regions that contain the point P from the set SRC to the set DST. Returns nil if SRC didn't contain any such region, otherwise returns t.

5.6.91 region-upper

(region-upper ID X) the upper bound of the region that contains X. Returns the upper bound of the region that contains point X or nil if there is no such region or such set of regions. See also, REGION-LOWER.

5.6.92 rshift

(rshift X N) logically shifts X right by N bits

5.6.93 s/

(s/ X Y Z …) returns X s/ Y s/ Z s/ … or 0 if the list of arguments is empty, where s/ is the signed division operation

5.6.94 set-symbol-value

(set-symbol-value S X) sets the value of the symbol S to X. Returns X

5.6.95 signed-mod

(signed-mod X Y Z …) returns X % Y % Z % … or 0 if the list of arguments is empty, where % is the signed modulo operation

5.6.96 symbol-concat

(symbol-concat X Y Z …) returns a new symbol that is a concatenation of symbols X,Y,Z,…

5.6.97 symbol-of-string

(symbol-of-string ptr) returns a symbol from a null-terminated string.

5.6.98 taint-get-direct

(taint-get-direct K X) returns the direct taint of the kind K associatedwith the value X, or nil if there is no such taint

5.6.99 taint-get-indirect

(taint-get-indirect K X) returns the indirect taint of the kind K associated with the value X, or nil if there is no such taint

5.6.100 taint-introduce-directly

(taint-introduce-directly K X) introduces a new taint of the kind K that is directly associated with the value X

5.6.101 taint-introduce-indirectly

(taint-introduce-indirectly K X N) introduces a new taint of the kind K that is indirectly associated with X pointing to an object of the size N

5.6.102 taint-kind

(taint-kind t) returns the kind of the taint T.

5.6.103 taint-policy-select

(taint-policy-select K P) selects the taint propagation policy P for the taints of the kind K

5.6.104 taint-policy-set-default

(taint-policy-set-default P) makes P the default taint propagation policy.

5.6.105 taint-sanitize-direct

(taint-sanitize-direct K X) removes any direct taint of the kind K that is directly associated with the value X

5.6.106 taint-sanitize-indirect

(taint-sanitize-indirect K X) removes any direct taint of the kind K that is indirectly associated with the value X

5.6.107 word-width

(word-width) returns machine word width in bits

5.7 Signals

5.7.1 call

(call NAME X Y …) is emitted when a call to a function with the symbolic NAME occurs with the specified list of arguments X,Y,…

5.7.2 call-return

(call-return NAME X Y … R) is emitted when a call to a function with the symbolic NAME returns with the specified list of arguments X,Y,… and return value R.

5.7.3 fini

(fini) occurs when the Primus Machine is finished

5.7.4 init

(init) occurs when the Primus Machine is initialized

5.7.5 interrupt

(interrupt N) is emitted when the hardware interrupt N occurs

5.7.6 jumping

(jumping C D) is emitted before jump to D occurs under the condition C

5.7.7 loaded

(loaded A X) is emitted when X is loaded from A

5.7.8 loading

(loading A) is emitted before load from A occurs

5.7.9 machine-kill

(machine-kill) occurs when Machine is killed and could be used for machine cleanup/teardown and analysis summaries. The machine is in the resticted mode in the body of the methods.

5.7.10 pc-changed

(pc-change PC) is emitted when PC is updated

5.7.11 read

(read V X) is emitted when X is read from V

5.7.12 stored

(stored A X) is emitted when X is stored to A

5.7.13 storing

(storing A) is emitted before store to A occurs

5.7.14 system-stop

(system-stop NAME) occurs when the system with the given name finished its execution. The machine is in the restricted mode in the body of the methods

5.7.15 taint-finalize

(taint-finalize T L) is emitted when the taint T is finilized while still live if L is true or dead if T is false.

5.7.16 written

(written V X) is emitted when X is written to V

6 Package primus

6.1 Constants

6.1.1 false

;; File "init.lisp", line 29, characters 0-55
(defconstant false 0:1 "false is another name for 0:1")

6.1.2 nil

;; File "init.lisp", line 30, characters 0-55
(defconstant nil false "nil is another name for false")

6.1.3 true

;; File "init.lisp", line 28, characters 0-54
(defconstant true 1:1  "true is another name for 1:1")

6.2 Functions

6.2.1 coerce

;; File "init.lisp", line 178, characters 0-81
(defun coerce (type v)
  (if (/= (word-width v) type) (extract (-1 type) 0 v) v))

6.2.2 copy-byte

;; File "memory.lisp", line 9, characters 0-134
(defun copy-byte (dst src)
  "(copy-byte DST SRC) copies byte from
   the address SRC to DST."
  (memory-write dst (memory-read src)))

6.2.3 points-to-null

;; File "memory.lisp", line 5, characters 0-109
(defun points-to-null (p)
  "(points-to-null P) true if P points to a zero byte"
  (is-zero (memory-read p)))

6.3 Macros

6.3.1 +1

;; File "init.lisp", line 89, characters 0-65
(defmacro +1 (x)
  "(+1 x) returns the successor of X"
  (+ x 1))

6.3.2 +=

;; File "init.lisp", line 81, characters 0-89
(defmacro += (x y)
  "(+= X Y) assigns a sum of X and Y to variable X"
  (set x (+ x y)))

6.3.3 -1

;; File "init.lisp", line 85, characters 0-67
(defmacro -1 (x)
  "(-1 X) returns the predecessor of X"
  (- x 1))

6.3.4 and

;; File "init.lisp", line 110, characters 0-55
(defmacro and (x xs)
  (let ((r x)) (if r (and xs) r)))

6.3.5 array-get

;; File "pointers.lisp", line 67, characters 0-153
(defmacro array-get (t p n)
  "(array-get T P N) gets the N-th element of the
   array of elements of type T, pointed by P"
  (read-word t (ptr+ t p n)))

6.3.6 array-set

;; File "pointers.lisp", line 72, characters 0-205
(defmacro array-set (t p n w)
  "(array-set T P N W) sets to W the N-th element of the array of
   elements of type T, pointed by P. Returns a pointer to the next
   element"
  (write-word t (ptr+ p n) w))

6.3.7 assert

;; File "init.lisp", line 136, characters 0-164
(defmacro assert (c)
  "(assert COND) terminates program if COND is false"
  (when (not c)
    (msg "Assertion (assert $0) failed" c)
    (error "Assert_failure")))

6.3.8 assert-msg

;; File "init.lisp", line 142, characters 0-170
(defmacro assert-msg (c s)
  "(assert-msg c s) allows you to assert a condition and print a message on failure"
  (when (not c)
    (msg s)
    (error "Assert_failure")))

6.3.9 bitwidth

;; File "init.lisp", line 176, characters 0-33
(defmacro bitwidth (type) (type))

6.3.10 case

;; File "init.lisp", line 51, characters 0-783
(defmacro case (k x xs)
  "(case K K1 X1 K2 X2 ... Kn Xn [DEF]) evaluates K then consequently
   evaluates keys K1 ...Kn until a key Ki such that (= K Ki) is found.
   If such key is found then the whole form evaluates to Xi.
   If no matching key was found, then if the number of keys is equal to
   the number of expressions, nil is returned, otherwise, i.e., if an
   extra expression DEF is provided, then it becomes the value of the
   whole form.

   Examples:

       (defun dispatch-with-default-case (c)
	 (case c
	   1 'hello
	   2 'cruel
	   3 'world
	   'unknown))

       (defun dispatch-with-no-default (c)
	 (case c
	   1 'hello
	   2 'cruel
	   3 'world))"
  (let ((key k))
    (case/dispatch key x xs)))

6.3.11 case/dispatch

;; File "init.lisp", line 172, characters 0-75
(defmacro case/dispatch (k k' x xs)
  (if (= k k') x (case/dispatch k xs)))

6.3.12 cast

;; File "init.lisp", line 175, characters 0-51
(defmacro cast (type x) (extract  (-1 (type)) 0 x))

6.3.13 compare

;; File "init.lisp", line 131, characters 0-134
(defmacro compare (x y)
  "(compare X Y) returns 0 if X = Y, a negative value if X<Y
   and a positive value if X>Y"
  (sign (- x y)))

6.3.14 copy-byte-shift

;; File "memory.lisp", line 14, characters 0-177
(defmacro copy-byte-shift (dst src)
  "(copy-byte-shift DST SRC) copies byte from
   SRC to DST and increments SRC and DST."
  (prog (copy-byte dst src)
	(incr dst src)))

6.3.15 copy-byte-shift-left

;; File "memory.lisp", line 20, characters 0-187
(defmacro copy-byte-shift-left (dst src)
  "(copy-byte-shift-left DST SRC) copies
   byte from DST to SRC and decrements SRC and DST."
  (prog (copy-byte dst src)
	(decr dst src)))

6.3.16 copy-left

;; File "memory.lisp", line 39, characters 0-162
(defmacro copy-left (dst src len)
  "(copy-left DST SRC LEN) copies LEN bytes
    from SRC to DST (right to left)"
  (make-copy copy-byte-shift-left dst src len))

6.3.17 copy-right

;; File "memory.lisp", line 34, characters 0-159
(defmacro copy-right (dst src len)
  "(copy-right DST SRC LEN) copies LEN bytes
    from SRC to DST (left to right)"
  (make-copy copy-byte-shift dst src len))

6.3.18 decr

;; File "init.lisp", line 103, characters 0-50
(defmacro decr (x xs)
  (prog (decr x) (decr xs)))

6.3.19 endian

;; File "pointers.lisp", line 22, characters 0-148
(defmacro endian (f x y)
  "(endian F X Y) expands to (F X Y) if applied
   in the big endian context"
  (declare (context (endian big)))
  (f x y))

6.3.20 fold

;; File "init.lisp", line 157, characters 0-46
(defmacro fold (f a x xs) (fold f (f a x) xs))

6.3.21 incr

;; File "init.lisp", line 97, characters 0-50
(defmacro incr (x xs)
  (prog (incr x) (incr xs)))

6.3.22 is-in

;; File "init.lisp", line 151, characters 0-57
(defmacro is-in (x y ys)
  (or (is-in x y) (is-in x ys)))

6.3.23 make-copy

;; File "memory.lisp", line 26, characters 0-146
(defmacro make-copy (copy-byte dst src len)
  "<internal>"
  (let ((ret dst))
    (while len
      (decr len)
      (copy-byte dst src))
    ret))

6.3.24 max

;; File "init.lisp", line 168, characters 0-42
(defmacro max (x y ys) (max (max x y) ys))

6.3.25 min

;; File "init.lisp", line 163, characters 0-42
(defmacro min (x y ys) (min (min x y) ys))

6.3.26 non-zero

;; File "init.lisp", line 77, characters 0-85
(defmacro non-zero (x)
  "(non-zero X) is true if X is not zero"
  (not (is-zero x)))

6.3.27 nth-byte-of-word

;; File "pointers.lisp", line 28, characters 0-284
(defmacro nth-byte-of-word (t i x)
  "(nth-byte-of-word T N X) returns N-th byte
   of the word X that has type T"
  (let ((n (sizeof t))
	(j (endian - n i))
	(k (if (< j n) (- j 1) (+ j n)))
	(hi (- (* 8 (+ k 1)) 1))
	(lo (* k 8)))
    (extract hi lo x)))

6.3.28 or

;; File "init.lisp", line 120, characters 0-19
(defmacro or (x) x)

6.3.29 points-to

;; File "pointers.lisp", line 62, characters 0-152
(defmacro points-to (t p v)
  "(points-to T P V) return true if t P points
  to a value of type T that is equal to V."
  (= (read-word t p) (cast t v)))

6.3.30 ptr+

;; File "pointers.lisp", line 6, characters 0-124
(defmacro ptr+ (t p n)
  "(ptr+ T P N) increments N times the
    pointer P to a value of type T."
  (+ p (* (sizeof t) n)))

6.3.31 ptr+1

;; File "pointers.lisp", line 11, characters 0-112
(defmacro ptr+1 (type p)
  "(ptr+1 T P) increments the pointer P to
   a value of to type T."
  (ptr+ type p 1))

6.3.32 read-word

;; File "pointers.lisp", line 38, characters 0-252
(defmacro read-word (t a)
  "(read-word T A) reads a word of type T at address A"
  (let ((p a)
	(x (memory-read p))
	(n (-1 (sizeof t))))
    (while n
      (incr p)
      (decr n)
      (set x (endian concat x (memory-read p))))
    x))

6.3.33 set$

;; File "init.lisp", line 122, characters 0-152
(defmacro set$ (s x)
  "(set$ s x) set the value of the symbol s to x, returns x.
   Like set but without implicit quotation."
  (set-symbol-value s x))

6.3.34 sign

;; File "init.lisp", line 127, characters 0-106
(defmacro sign (x)
  "returns 1 if X > 0, 0 if X = 0, and -1 if X < 0"
  (if (< x 0) -1 (if (> x 0) 1 0)))

6.3.35 sizeof

;; File "init.lisp", line 177, characters 0-46
(defmacro sizeof (type) (/ (bitwidth type) 8))

6.3.36 unless

;; File "init.lisp", line 38, characters 0-267
(defmacro unless (cnd body)
  "(unless CND BODY) if CND evaluates to false, then BODY is evaluated and
   the value of the last expression in BODY becomes the value of the
   whole expression. Otherwise, if CND evaluates to true, nil is returned."
  (if cnd () body))

6.3.37 until

;; File "init.lisp", line 44, characters 0-349
(defmacro until (c b)
  "(until COND BODY) if COND evaluates to true, then the whole expression
   evaluates to nil and BODY is not evaluated. Otherwise, if COND evaluates
   to false, then BODY is evaluated until COND evaluates to true and the value
   of the last evaluation of BODY becomes the value of the whole expression."
  (while (not c) b))

6.3.38 when

;; File "init.lisp", line 32, characters 0-270
(defmacro when (cnd body)
  "(when CND BODY) if CND evaluates to true, then BODY is evaluated and
   the value of the last expression in BODY becomes the value of the
   whole expression. Otherwise, if CND evaluates to false, nil is returned."
  (if cnd (prog body) ()))

6.3.39 write-word

;; File "pointers.lisp", line 49, characters 0-329
(defmacro write-word (t a x)
  "(write-word T A X) writes the word X of type T to address A
  returns an address that points to the first byte that follows
  the just written word.
  "
  (let ((p a)
	(n (sizeof t))
	(i 0))
    (while (< i n)
      (memory-write p (nth-byte-of-word t i x))
      (incr p i))
    p))

6.4 Methods

6.4.1 init

;; File "locale.lisp", line 9, characters 0-61
(defmethod init ()
  (set LCONV brk)
  (+= brk *lconv-size*))

6.4.2 machine-kill

;; File "stdio.lisp", line 153, characters 0-105
(defmethod primus:machine-kill ()
  (channel-flush *standard-output*)
  (channel-flush *standard-error*))

6.5 Primitives

6.5.1 *

(* X Y Z …) returns the product of arguments or 0 if the list of arguments is empty

6.5.2 +

(+ X Y …) returns the sum of arguments, or 0 if there are no arguments,

6.5.3 -

(- X Y Z …) returns X - Y - Z - …, or 0 if there are no arguments.

6.5.4 /

(/ X Y Z …) returns X / Y / Z / … or 0 if the list of arguments is empty

6.5.5 /=

(/= X Y Z …) returns true if at least one argument is not equal to another argument. Returns false if the list of arguments is empty

6.5.6 <

(< X Y Z …) is true if the list of arguments is an strict ascending chain or if it is empty

6.5.7 <=

(< X Y Z …) is true if the list of arguments is an ascending chain or if it is empty

6.5.8 =

(= X Y Z …) returns true if all arguments are equal. True if the list of arguments is empty

6.5.9 >

(< X Y Z …) is true if the list of arguments is a strict descending chain or if it is empty

6.5.10 >=

(< X Y Z …) is true if the list of arguments is a descending chain or if it is empty

6.5.11 all-static-constant

(all-static-constant X Y ..) is true if X,Y,… are static constants. A value is a static constant if it was initialized from a constant value or computed from static constant values.

6.5.12 arshift

(arshift X N) arithmetically shifts X right by N bits

6.5.13 channel-close

(channel-close DESCR) closes a channel that has the specified descriptor DESCR. If no such channel exists, then returns -1. Otherwise returns 0. The descriptor of the closed channel will be reused by the consequent calls to `channel-open'. If the channel had any data associated with it and not yet flushed, then the data is discarded.

6.5.14 channel-flush

(channel-flush DESCR) forces data that were written to a channel that has the descriptor DESCR to be outputted to the associated destination. Returns -1 if no such channel exists or if in case of an IO error.

6.5.15 channel-input

(channel-input DESC) reads one byte from a channel that has the descriptor DESC. Returns -1 if no such channel exists, or if any IO error occurs, if the channel is not readable, or if the channel is in the end-of-file condition.

6.5.16 channel-open

(channel-open PTR) creates a new channel that is associated with a null-terminated path pointed by PTR. Returns a non-negative channel descriptor, if the channel subsystem have a mapping from the obtained path to a physical file and this file is accessible. Otherwise returns a negative value.

6.5.17 channel-output

(channel-output DESCR CHAR …) outputs one or more characters to a channel that has the descriptor DESCR. Returns -1 if no such channel exits, if a channel is not writable, or if any IO error occurs in an associated physical file. Otherwise, returns 0. Note: the channel system is buffered, and the actual IO operation (as well as errors) could be delayed until (channel-flush DESCR) is called.

6.5.18 concat

(concat X Y Z …) concatenates words X, Y, Z, … into one big word

6.5.19 dict-add

(dict-add DIC KEY DATA) associates DATA with KEY in the dictionary DIC. Returns KEY.

6.5.20 dict-del

(dict-del DIC KEY) deletes any association with KEY in the dictionary DIC

6.5.21 dict-first

(dict-first DIC) is the first key in DIC or nil if DIC is empty.

6.5.22 dict-get

(dict-get DIC KEY) returns data associated with KEY in the dictionary DIC, and returns NIL if either DIC doesn't exist on no data are associated

6.5.23 dict-has

(dict-has DIC KEY) returns T if the dictionary DIC has the key KEY

6.5.24 dict-length

(dict-first DIC) is the total number of keys in DIC.

6.5.25 dict-next

(dict-next DIC KEY) returns the next key after KEY Returns nil if KEY was the last key in the dictionary. Could be used together with DICT-FIRST for iterating.

6.5.26 exec-addr

(exec-addr D) passes the control flow to D and never returns

6.5.27 exec-symbol

(exec-symbol D) passes the control flow to D and never returns

6.5.28 exit-with

(exit-with N) terminates program with the exit codeN

6.5.29 extract

(extract HI LO X) extracts bits from HI to LO (including both) from the word X

6.5.30 get-current-program-counter

(get-current-program-counter) returns current program cunnter

6.5.31 ieee754-abs

applies abs to the operand

6.5.32 ieee754-acos

applies acos to the operand

6.5.33 ieee754-add

reduces the list of operands with add

6.5.34 ieee754-asin

applies asin to the operand

6.5.35 ieee754-atan

applies atan to the operand

6.5.36 ieee754-atan2

reduces the list of operands with atan2

6.5.37 ieee754-ceil

applies ceil to the operand

6.5.38 ieee754-cos

applies cos to the operand

6.5.39 ieee754-cosh

applies cosh to the operand

6.5.40 ieee754-cti

truncates to the nearest integer

6.5.41 ieee754-div

reduces the list of operands with div

6.5.42 ieee754-eq

returns true if all operands are ordered with the eq order

6.5.43 ieee754-exp

applies exp to the operand

6.5.44 ieee754-expm1

applies expm1 to the operand

6.5.45 ieee754-floor

applies floor to the operand

6.5.46 ieee754-ge

returns true if all operands are ordered with the ge order

6.5.47 ieee754-gt

returns true if all operands are ordered with the gt order

6.5.48 ieee754-hypot

reduces the list of operands with hypot

6.5.49 ieee754-is-nan

checks if is-nan holds

6.5.50 ieee754-le

returns true if all operands are ordered with the le order

6.5.51 ieee754-log

applies log to the operand

6.5.52 ieee754-log10

applies log10 to the operand

6.5.53 ieee754-log1p

applies log1p to the operand

6.5.54 ieee754-lt

returns true if all operands are ordered with the lt order

6.5.55 ieee754-mod

reduces the list of operands with mod

6.5.56 ieee754-mul

reduces the list of operands with mul

6.5.57 ieee754-ne

returns true if all operands are ordered with the ne order

6.5.58 ieee754-neg

applies neg to the operand

6.5.59 ieee754-pos

applies pos to the operand

6.5.60 ieee754-pow

reduces the list of operands with pow

6.5.61 ieee754-sin

applies sin to the operand

6.5.62 ieee754-sinh

applies sinh to the operand

6.5.63 ieee754-sqrt

applies sqrt to the operand

6.5.64 ieee754-sub

reduces the list of operands with sub

6.5.65 ieee754-tan

applies tan to the operand

6.5.66 ieee754-tanh

applies tanh to the operand

6.5.67 incident-location

6.5.68 incident-report

6.5.69 invoke-subroutine

(invoke-subroutine addr args …) calls the subroutine at the specified address.

6.5.70 is-negative

(is-negative X Y …) returns true if all arguments are negative

6.5.71 is-positive

(is-positive X Y …) returns true if all arguments are positive

6.5.72 is-zero

(is-zero X Y …) returns true if all arguments are zeros

6.5.73 lnot

(lnot X) returns the one complement of X

6.5.74 logand

(logand X Y Z …) returns X & Y & Z & … or 0 if the list of arguments is empty, where & is the bitwise AND operation. Returns ~0 if the list of arguments is empty

6.5.75 logor

(logor X Y Z …) returns X | Y | Z | … or 0 if the list of arguments is empty, where | is the bitwise OR operation

6.5.76 logxor

(logxor X Y Z …) returns X ^ Y ^ Z ^ … or 0 if the list of arguments is empty, where ^ is the bitwise XOR operation

6.5.77 lshift

(lshift X N) logically shifts X left by N bits

6.5.78 memory-allocate

(memory-allocate P N V?) maps memory region [P,P+N), if V is provided, then fills the newly mapped region with the value V

6.5.79 memory-read

(memory-read A) loads one byte from the address A

6.5.80 memory-write

(memory-write A X) stores by X to A

6.5.81 mod

(mod X Y Z …) returns X % Y % Z % … or 0 if the list of arguments is empty, where % is the modulo operation

6.5.82 neg

(neg X) returns the two complement of X

6.5.83 not

(not X) returns true if X is zero

6.5.84 points-to-static-data

(points-to-static-data PTR LEN) is true iff (all-static-constant *PTR .. *(PTR+LEN-1))

6.5.85 reg-name

(reg-name N) returns the name of the register with the index N

6.5.86 region-contains

(region-contains ID X) return the region in ID that has X. Returns the lower bound of the first region that contains value X in the set of regions with the given ID. Returns nil otherwise. Returns nil if a set with the given ID doesn't exist.

6.5.87 region-count

(region-count ID) the total number of regions in the set ID. Counts the number of regions (including intersecting) stored in the set of regions referenced by the symbol ID. Returns nil if there is no set with the given ID, otherwise returns the number of regions in that set.

6.5.88 region-create

(region-create ID LOWER UPPER) adds [LOWER,UPPER] to the set ID. Adds a region denoted with the interval [LOWER,UPPER] to the set of regions denoted by the symbol ID. Values LOWER and UPPER are included into the interval. If the set of regions ID doesn't exist, then it is created.

6.5.89 region-lower

(region-lower ID X) the lower bound of region that contains X. Returns nil if ID doesn't exist or if it doesn't have a region that includes X. This fucntion is an alias for REGION-CONTAINS. See also, REGION-UPPER.

6.5.90 region-move

(region-move DST SRC P) moves all regions that contain the point P from the set SRC to the set DST. Returns nil if SRC didn't contain any such region, otherwise returns t.

6.5.91 region-upper

(region-upper ID X) the upper bound of the region that contains X. Returns the upper bound of the region that contains point X or nil if there is no such region or such set of regions. See also, REGION-LOWER.

6.5.92 rshift

(rshift X N) logically shifts X right by N bits

6.5.93 s/

(s/ X Y Z …) returns X s/ Y s/ Z s/ … or 0 if the list of arguments is empty, where s/ is the signed division operation

6.5.94 set-symbol-value

(set-symbol-value S X) sets the value of the symbol S to X. Returns X

6.5.95 signed-mod

(signed-mod X Y Z …) returns X % Y % Z % … or 0 if the list of arguments is empty, where % is the signed modulo operation

6.5.96 symbol-concat

(symbol-concat X Y Z …) returns a new symbol that is a concatenation of symbols X,Y,Z,…

6.5.97 symbol-of-string

(symbol-of-string ptr) returns a symbol from a null-terminated string.

6.5.98 taint-get-direct

(taint-get-direct K X) returns the direct taint of the kind K associatedwith the value X, or nil if there is no such taint

6.5.99 taint-get-indirect

(taint-get-indirect K X) returns the indirect taint of the kind K associated with the value X, or nil if there is no such taint

6.5.100 taint-introduce-directly

(taint-introduce-directly K X) introduces a new taint of the kind K that is directly associated with the value X

6.5.101 taint-introduce-indirectly

(taint-introduce-indirectly K X N) introduces a new taint of the kind K that is indirectly associated with X pointing to an object of the size N

6.5.102 taint-kind

(taint-kind t) returns the kind of the taint T.

6.5.103 taint-policy-select

(taint-policy-select K P) selects the taint propagation policy P for the taints of the kind K

6.5.104 taint-policy-set-default

(taint-policy-set-default P) makes P the default taint propagation policy.

6.5.105 taint-sanitize-direct

(taint-sanitize-direct K X) removes any direct taint of the kind K that is directly associated with the value X

6.5.106 taint-sanitize-indirect

(taint-sanitize-indirect K X) removes any direct taint of the kind K that is indirectly associated with the value X

6.5.107 word-width

(word-width) returns machine word width in bits

6.6 Signals

6.6.1 call

(call NAME X Y …) is emitted when a call to a function with the symbolic NAME occurs with the specified list of arguments X,Y,…

6.6.2 call-return

(call-return NAME X Y … R) is emitted when a call to a function with the symbolic NAME returns with the specified list of arguments X,Y,… and return value R.

6.6.3 fini

(fini) occurs when the Primus Machine is finished

6.6.4 init

(init) occurs when the Primus Machine is initialized

6.6.5 interrupt

(interrupt N) is emitted when the hardware interrupt N occurs

6.6.6 jumping

(jumping C D) is emitted before jump to D occurs under the condition C

6.6.7 loaded

(loaded A X) is emitted when X is loaded from A

6.6.8 loading

(loading A) is emitted before load from A occurs

6.6.9 machine-kill

(machine-kill) occurs when Machine is killed and could be used for machine cleanup/teardown and analysis summaries. The machine is in the resticted mode in the body of the methods.

6.6.10 pc-changed

(pc-change PC) is emitted when PC is updated

6.6.11 read

(read V X) is emitted when X is read from V

6.6.12 stored

(stored A X) is emitted when X is stored to A

6.6.13 storing

(storing A) is emitted before store to A occurs

6.6.14 system-stop

(system-stop NAME) occurs when the system with the given name finished its execution. The machine is in the restricted mode in the body of the methods

6.6.15 taint-finalize

(taint-finalize T L) is emitted when the taint T is finilized while still live if L is true or dead if T is false.

6.6.16 written

(written V X) is emitted when X is written to V

7 Package user

7.1 Constants

7.1.1 false

;; File "init.lisp", line 29, characters 0-55
(defconstant false 0:1 "false is another name for 0:1")

7.1.2 nil

;; File "init.lisp", line 30, characters 0-55
(defconstant nil false "nil is another name for false")

7.1.3 true

;; File "init.lisp", line 28, characters 0-54
(defconstant true 1:1  "true is another name for 1:1")

7.2 Functions

7.2.1 abort

;; File "stdlib.lisp", line 23, characters 0-103
(defun abort ()
  "terminates program with exit code 1"
  (declare (external "abort"))
  (exit-with 1))

7.2.2 abs

;; File "stdlib.lisp", line 38, characters 0-77
(defun abs (x)
  (declare (external "abs"))
  (if (is-negative x) (neg x) x))

7.2.3 ascii-is-alpha

;; File "ascii.lisp", line 31, characters 0-88
(defun ascii-is-alpha (c)
  (declare (external "isalpha"))
  (< (- (logor c 32) ?a) 26))

7.2.4 ascii-is-alphanum

;; File "ascii.lisp", line 26, characters 0-113
(defun ascii-is-alphanum (c)
  (declare (external "isalnum"))
  (or (ascii-is-alpha c)
      (ascii-is-digit c)))

7.2.5 ascii-is-digit

;; File "ascii.lisp", line 21, characters 0-157
(defun ascii-is-digit (s)
  "(ascii-is-digit s) is true if S is an ascii representation of decimal digit"
  (declare (external "isdigit"))
  (< (- s ?0) 10))

7.2.6 ascii-is-lower

;; File "ascii.lisp", line 39, characters 0-77
(defun ascii-is-lower (c)
  (declare (external "islower"))
  (< (- c ?a) 26))

7.2.7 ascii-is-special

;; File "ascii.lisp", line 3, characters 0-104
(defun ascii-is-special (s)
  "(ascii-special S) is true if S is an ascii special character"
  (< s 32))

7.2.8 ascii-is-upper

;; File "ascii.lisp", line 35, characters 0-77
(defun ascii-is-upper (c)
  (declare (external "isupper"))
  (< (- c ?A) 26))

7.2.9 ascii-is-whitespace

;; File "ascii.lisp", line 7, characters 0-156
(defun ascii-is-whitespace (s)
  "(ascii-is-whitespace S) is true if S is \t, \n, \r, or SPACE"
  (or (= s 9)
      (= s 10)
      (= s 13)
      (= s 32)))

7.2.10 ascii-sign

;; File "ascii.lisp", line 14, characters 0-121
(defun ascii-sign (s)
  "(ascii-sign S) is 1 if S is +, -1 if it -, or 0 otherwise"
  (case s
    ?- -1
    ?+  1
    0))

7.2.11 ascii-to-lower

;; File "ascii.lisp", line 43, characters 0-100
(defun ascii-to-lower (c)
  (declare (external "tolower"))
  (if (ascii-is-upper c) (logor c 32) c))

7.2.12 ascii-to-upper

;; File "ascii.lisp", line 47, characters 0-103
(defun ascii-to-upper (c)
  (declare (external "toupper"))
  (if (ascii-is-lower c) (logand c 0x5f) c))

7.2.13 atexit

;; File "stdlib.lisp", line 34, characters 0-70
(defun atexit (cb)
  (declare (external "atexit" "__cxa_atexit"))
  0)

7.2.14 atoi

;; File "atoi.lisp", line 31, characters 0-93
(defun atoi  (s)
  (declare (visibility :public) (external "atoi"))
  (make-converter int s))

7.2.15 atol

;; File "atoi.lisp", line 35, characters 0-94
(defun atol  (s)
  (declare (visibility :public) (external "atol"))
  (make-converter long s))

7.2.16 atoll

;; File "atoi.lisp", line 39, characters 0-100
(defun atoll (s)
  (declare (visibility :public) (external "atoll"))
  (make-converter long-long s))

7.2.17 bindtextdomain

;; File "libintl.lisp", line 3, characters 0-76
(defun bindtextdomain (_ dir)
  (declare (external "bindtextdomain"))
  dir)

7.2.18 brk

;; File "simple-memory-allocator.lisp", line 69, characters 0-66
(defun brk (val)
  (declare (external "brk"))
  (set brk val)
  0)

7.2.19 calloc

;; File "simple-memory-allocator.lisp", line 107, characters 0-202
(defun calloc (n s)
  "allocates memory and initializes it with zero"
  (declare (external "calloc"))
  (let ((*malloc-initialize-memory* true)
	(*malloc-initial-value* 0))
    (malloc (* n s))))

7.2.20 char

;; File "types.lisp", line 44, characters 0-67
(defun char ()  (declare (context (abi eabi))) (model-ilp32 'char))

7.2.21 coerce

;; File "init.lisp", line 178, characters 0-81
(defun coerce (type v)
  (if (/= (word-width v) type) (extract (-1 type) 0 v) v))

7.2.22 copy-byte

;; File "memory.lisp", line 9, characters 0-134
(defun copy-byte (dst src)
  "(copy-byte DST SRC) copies byte from
   the address SRC to DST."
  (memory-write dst (memory-read src)))

7.2.23 double

;; File "types.lisp", line 100, characters 0-20
(defun double () 64)

7.2.24 errno-location

;; File "errno.lisp", line 13, characters 0-116
(defun errno-location ()
  (declare (visibility :public)
	   (external "__errno_location"))
  errno-location)

7.2.25 exit

;; File "stdlib.lisp", line 29, characters 0-76
(defun exit (code)
  (declare (external "exit" "_exit"))
  (exit-with code))

7.2.26 fflush

;; File "stdio.lisp", line 28, characters 0-88
(defun fflush (s)
  (declare (external "fflush" "fflush_unlocked"))
  (channel-flush s))

7.2.27 fgetc

;; File "stdio.lisp", line 106, characters 0-118
(defun fgetc (stream)
  (declare (external "fgetc" "getc" "fgetc_unlocked" "getc_unlocked"))
  (channel-input stream))

7.2.28 fgets

;; File "stdio.lisp", line 115, characters 0-557
(defun fgets (ptr len str)
  (declare (external "fgets" "fgets_unlocked"))
  (if (= len 0) (terminate-string-and-return-null ptr)
    (let ((i 0)
	  (n (-1 len))
	  (continue true))
      (while (and continue (< i n))
	(let ((c (fgetc str)))
	  (if (= c -1)
	      (set continue false)
	    (memory-write (+ ptr i) (cast char c))
	    (set continue (/= c 0xA))
	    (incr i))))
      (if (= i 0)
	  (terminate-string-and-return-null ptr)
      (memory-write (+ ptr (min n i)) 0:8)
      ptr))))

7.2.29 fileno

;; File "stdio.lisp", line 42, characters 0-64
(defun fileno (stream)
  (declare (external "fileno"))
  stream)

7.2.30 float

;; File "types.lisp", line 101, characters 0-19
(defun float () 32)

7.2.31 fopen

;; File "stdio.lisp", line 37, characters 0-136
(defun fopen (path mode)
  (declare (external "fopen" "open" "fdopen"))
  (let ((file (channel-open path)))
    (if (< file 0) 0 file)))

7.2.32 fputc

;; File "stdio.lisp", line 7, characters 0-146
(defun fputc (char stream)
  (declare (external "fputc" "putc" "fputs_unlocked putc_unlocked"))
  (if (= 0 (channel-output stream char)) char -1))

7.2.33 fputs

;; File "stdio.lisp", line 15, characters 0-225
(defun fputs (p stream)
  (declare (external "fputs" "fputs_unlocked"))
  (while (not (points-to-null p))
    (fputc (cast int (memory-read p)) stream)
    (incr p))
  (let ((r (fputc 0xA stream)))
    (fflush stream)
    r))

7.2.34 fread

;; File "stdio.lisp", line 92, characters 0-208
(defun fread (ptr size n stream)
  (declare (external "fread" "fread_unlocked"))
  (let ((i 0))
    (while (and
	    (< i n)
	    (= size (input-item ptr size i stream)))
      (incr i))
    i))

7.2.35 free

;; File "simple-memory-allocator.lisp", line 103, characters 0-87
(defun free (p)
  "frees the memory region pointed by P"
  (declare (external "free")))

7.2.36 fwrite

;; File "stdio.lisp", line 63, characters 0-204
(defun fwrite (buf size n stream)
  (declare (external "fwrite" "fwrite_unlocked"))
  (let ((i 0))
    (while (and (< i n)
		(= size (output-item buf size i stream)))
      (incr i))
    i))

7.2.37 getchar

;; File "stdio.lisp", line 149, characters 0-97
(defun getchar ()
  (declare (external "getchar" "getchar_unlocked"))
  (fgetc *standard-input*))

7.2.38 getenv

;; File "stdlib.lisp", line 9, characters 0-452
(defun getenv (name)
  "finds a value of an environment variable with the given name"
  (declare (external "getenv" "secure_getenv"))
  (let ((p environ)
	(n (strlen name)))
    (while (and (not (points-to-null p))
		(/= (memcmp (read-word ptr_t p) name n) 0))
      (set p (ptr+1 ptr_t p)))
    (if (not (points-to-null p))
	(let ((p (read-word ptr_t p)))
	  (if (= (memory-read (+ p n)) ?=) (+ p n 1) 0))
      0)))

7.2.39 getopt

;; File "getopt.lisp", line 64, characters 0-688
(defun getopt (argc argv opts)
  (declare
   (visibility :public)
   (external "getopt"))
  (when (= 0 optind)
    (set optind 1)
    (set last-ofs 0))
  (if (getopt-finished argc argv) -1
    (if (getopt-nearly-finished argv)
	(prog (incr optind) -1)
      (let ((p 0) )
	(getopt-update-optopt argv last-ofs)
	(set p (strchr opts optopt))
	(if p
	    (prog
	     (when (points-to-null p)
	       (prog (incr optind) (getopt argc argv opts)))
	     (if (getopt-expects-argument p)
		 (getopt-with-argument argv opts p last-ofs)
	       (prog (incr last-ofs) optopt)))
	   (incr optind)
	   ??)))))

7.2.40 getopt_long

;; File "getopt.lisp", line 88, characters 0-149
(defun getopt_long (argc argv opts _ _)
  (declare
   (visibility :public)
   (external "getopt_long" "getopt_long_only"))
  (getopt argc argv opts))

7.2.41 getpagesize

;; File "unistd.lisp", line 13, characters 0-73
(defun getpagesize ()
  (declare (external "getpagesize"))
  (* 64 1024))

7.2.42 gets

;; File "stdio.lisp", line 133, characters 0-370
(defun gets (ptr)
  (declare (external "gets"))
  (let ((str *standard-input*)
	(i 0)
	(continue true))
    (while continue
      (let ((c (fgetc str)))
	(if (= c -1)
	    (set continue false)
	  (memory-write (+ ptr i) (cast char c))
	  (set continue (/= c 0xA))
	  (incr i))))
    (memory-write (+ ptr i) 0:8)
    ptr))

7.2.43 int

;; File "types.lisp", line 46, characters 0-66
(defun int ()   (declare (context (abi eabi))) (model-ilp32 'int))

7.2.44 int32_t

;; File "types.lisp", line 97, characters 0-21
(defun int32_t () 32)

7.2.45 int64_t

;; File "types.lisp", line 98, characters 0-21
(defun int64_t () 64)

7.2.46 ioctl

;; File "unistd.lisp", line 7, characters 0-50
(defun ioctl (_ _) (declare (external "ioctl")) 0)

7.2.47 isatty

;; File "unistd.lisp", line 3, characters 0-62
(defun isatty (fd)
  (declare (external "isatty"))
  (< fd 3))

7.2.48 lconv

;; File "locale.lisp", line 17, characters 0-55
(defun lconv ()
  (declare (external "lconv"))
  LCONV)

7.2.49 long

;; File "types.lisp", line 47, characters 0-67
(defun long ()  (declare (context (abi eabi))) (model-ilp32 'long))

7.2.50 long-long

;; File "types.lisp", line 95, characters 0-33
(defun long-long () (word-width))

7.2.51 longjmp

;; File "setjmp.lisp", line 11, characters 0-103
(defun longjmp (_ _)
  (declare (external "longjmp" "_longjmp" "siglongjmp" "_siglongjmp"))
  (exit 1))

7.2.52 malloc

;; File "simple-memory-allocator.lisp", line 52, characters 0-618
(defun malloc (n)
  "allocates a memory region of size N"
  (declare (external "malloc"))
  (if (= n 0) *malloc-zero-sentinel*
    (if (malloc-will-reach-limit n) 0
      (malloc/grow-arena-if-needed n)
      (+= *malloc/total-bytes-allocated* n)
      (let ((header-size (sizeof int))
	    (chunk-size (+ n (* 2 *malloc-guard-edges*) header-size))
	    (ptr *malloc/brk*))
	(malloc/initialize ptr chunk-size)
	(+= *malloc/brk* chunk-size)
	(malloc/fill-edges ptr chunk-size)
	(+= ptr *malloc-guard-edges*)
	(malloc/put-chunk-size ptr n)
	(+ ptr header-size)))))

7.2.53 memcasecmp

;; File "string.lisp", line 152, characters 0-265
(defun memcasecmp (p1 p2 n)
  (let ((res 0) (i 0))
    (while (and (< i n) (not res))
      (set res (compare
		(ascii-to-lower (cast int (memory-read p1)))
		(ascii-to-lower (cast int (memory-read p2)))))
      (incr p1 p2 i))
    res))

7.2.54 memccpy

;; File "string.lisp", line 53, characters 0-271
(defun memccpy (dst src c len)
  (declare (external "memccpy"))
  (let ((found 0))
    (while (and len (not found))
      (copy-byte dst src)
      (incr src)
      (decr len)
      (when (points-to char dst c)
	(set found (+ 1 dst)))
      (incr dst))
    found))

7.2.55 memchr

;; File "string.lisp", line 74, characters 0-84
(defun memchr (p c n)
  (declare (external "memchr"))
  (find-character incr p c n))

7.2.56 memcmp

;; File "string.lisp", line 127, characters 0-205
(defun memcmp (p1 p2 n)
  (declare (external "memcmp"))
  (let ((res 0) (i 0))
    (while (and (< i n) (not res))
      (set res (compare (memory-read p1) (memory-read p2)))
      (incr p1 p2 i))
    res))

7.2.57 memcpy

;; File "string.lisp", line 49, characters 0-87
(defun memcpy (dst src len)
  (declare (external "memcpy"))
  (copy-right dst src len))

7.2.58 memmove

;; File "string.lisp", line 39, characters 0-238
(defun memmove (dst src len)
  (declare (external "memmove"))
  (let ((a dst) (b src))
    (when (/= src dst)
      (if (> src dst) (copy-right a b len)
	(+= a (-1 len))
	(+= b (-1 len))
	(copy-left a b len))))
  dst)

7.2.59 memrchr

;; File "string.lisp", line 79, characters 0-86
(defun memrchr (p c n)
  (declare (external "memrchr"))
  (find-character decr p c n))

7.2.60 memset

;; File "string.lisp", line 118, characters 0-143
(defun memset (p c n)
  (declare (external "memset"))
  (let ((p p))
    (while n
      (memory-write p c)
      (incr p)
      (decr n)))
  p)

7.2.61 model-ilp32

;; File "types.lisp", line 3, characters 0-103
(defun model-ilp32 (type)
  (case type
    'char 8
    'short 16
    'int 32
    'long 32
    'ptr 32))

7.2.62 model-ilp64

;; File "types.lisp", line 19, characters 0-103
(defun model-ilp64 (type)
  (case type
    'char 8
    'short 16
    'int 64
    'long 64
    'ptr 64))

7.2.63 model-llp64

;; File "types.lisp", line 27, characters 0-103
(defun model-llp64 (type)
  (case type
    'char 8
    'short 16
    'int 32
    'long 32
    'ptr 64))

7.2.64 model-lp32

;; File "types.lisp", line 11, characters 0-102
(defun model-lp32 (type)
  (case type
    'char 8
    'short 16
    'int 16
    'long 32
    'ptr 32))

7.2.65 model-lp64

;; File "types.lisp", line 35, characters 0-102
(defun model-lp64 (type)
  (case type
    'char 8
    'short 16
    'int 32
    'long 64
    'ptr 64))

7.2.66 open3

;; File "stdio.lisp", line 46, characters 0-81
(defun open3 (path flags mode)
  (declare (external "open"))
  (fopen path mode))

7.2.67 output-item

;; File "stdio.lisp", line 55, characters 0-170
(defun output-item (buf size item fd)
  (let ((i 0))
    (while (and
	    (< i size)
	    (output-item-nth-char buf size item fd i))
      (incr i))
    i))

7.2.68 output-item-nth-char

;; File "stdio.lisp", line 50, characters 0-131
(defun output-item-nth-char (ptr size item fd i)
  (= 0 (channel-output
	fd
	(memory-read (+ ptr (* size item) i)))))

7.2.69 points-to-null

;; File "memory.lisp", line 5, characters 0-109
(defun points-to-null (p)
  "(points-to-null P) true if P points to a zero byte"
  (is-zero (memory-read p)))

7.2.70 ptr_t

;; File "types.lisp", line 48, characters 0-66
(defun ptr_t () (declare (context (abi eabi))) (model-ilp32 'ptr))

7.2.71 putchar

;; File "stdio.lisp", line 11, characters 0-107
(defun putchar (char)
  (declare (external "putchar" "putchar_unlocked"))
  (fputc char *standard-output*))

7.2.72 puts

;; File "stdio.lisp", line 24, characters 0-92
(defun puts (p)
  (declare (external "puts" "puts_unlocked"))
  (fputs p *standard-output*))

7.2.73 read

;; File "stdio.lisp", line 101, characters 0-74
(defun read (fd buf n)
  (declare (external "read"))
  (fread buf 1 n fd))

7.2.74 realloc

;; File "simple-memory-allocator.lisp", line 78, characters 0-166
(defun realloc (ptr len)
  (declare (external "realloc"))
  (if (not ptr) (malloc len)
    (if (not len) (realloc/as-free ptr)
      (realloc/update-chunk ptr len))))

7.2.75 sbrk

;; File "simple-memory-allocator.lisp", line 74, characters 0-75
(defun sbrk (increment)
  (declare (external "sbrk"))
  (+= brk increment))

7.2.76 setjmp

;; File "setjmp.lisp", line 3, characters 0-64
(defun setjmp (_)
  (declare (external "setjmp" "_setjmp"))
  0)

7.2.77 setlocale

;; File "locale.lisp", line 13, characters 0-72
(defun setlocale (_ locale)
  (declare (external "setlocale"))
  locale)

7.2.78 short

;; File "types.lisp", line 45, characters 0-68
(defun short () (declare (context (abi eabi))) (model-ilp32 'short))

7.2.79 sigsetjmp

;; File "setjmp.lisp", line 7, characters 0-75
(defun sigsetjmp (_ _)
  (declare (external "sigsetjmp" "_sigsetjmp"))
  0)

7.2.80 stpcpy

;; File "string.lisp", line 243, characters 0-136
(defun stpcpy (dst src)
  (declare (external "stpcpy"))
  (let ((len (strlen src)))
    (+ (memcpy dst src (+1 len)) (cast ptr_t len))))

7.2.81 strcasecmp

;; File "string.lisp", line 167, characters 0-160
(defun strcasecmp (p1 p2)
  (declare (external "strcasecmp"))
  (strncasecmp p1 p2 (min (strlen/with-null p1)
			  (strlen/with-null p2))))

7.2.82 strcasestr

;; File "string.lisp", line 185, characters 0-109
(defun strcasestr (hay needle)
  (declare (external "strcasestr"))
  (find-substring strncasecmp hay needle))

7.2.83 strcat

;; File "string.lisp", line 107, characters 0-111
(defun strcat (dst src)
  (declare (external "strcat"))
  (strcpy (+ dst (cast ptr_t (strlen dst))) src)
  dst)

7.2.84 strchr

;; File "string.lisp", line 84, characters 0-92
(defun strchr (p c)
  (declare (external "strchr" "index"))
  (memchr p c (+ (strlen p) 1)))

7.2.85 strchrnul

;; File "string.lisp", line 92, characters 0-111
(defun strchrnul (p c)
  (declare (external "strchrnul"))
  (let ((p (strchr p c)))
    (if p p (strchr p 0))))

7.2.86 strcmp

;; File "string.lisp", line 147, characters 0-152
(defun strcmp (s1 s2)
  (declare (external "strcmp" "strcoll"))
  (memcmp s1 s2 (min (strlen/with-null s1)
		     (strlen/with-null s2))))

7.2.87 strcpy

;; File "string.lisp", line 16, characters 0-180
(defun strcpy (dst src)
  (declare (external "strcpy"))
  (let ((dst dst))
    (while (not (points-to-null src))
      (copy-byte-shift dst src))
    (memory-write dst 0:8))
  dst)

7.2.88 strcspn

;; File "string.lisp", line 196, characters 0-176
(defun strcspn (str set)
  (declare (external "strcspn"))
  (let ((len 0))
    (until (or (points-to-null str)
	       (strpbrk str set))
      (incr str len))
    len))

7.2.89 strdup

;; File "string.lisp", line 33, characters 0-126
(defun strdup (src)
  (declare (external "strdup"))
  (let ((dst (malloc (+1 (strlen src)))))
    (and dst (strcpy dst src))))

7.2.90 strlen

;; File "string.lisp", line 8, characters 0-132
(defun strlen (p)
  (declare (external "strlen"))
  (let ((len 0))
    (while (not (points-to-null p))
      (incr len p))
    len))

7.2.91 strncasecmp

;; File "string.lisp", line 161, characters 0-189
(defun strncasecmp (p1 p2 n)
  (declare (external "strncasecmp"))
  (memcasecmp p1 p2 (min n
			 (strlen/with-null p1)
			 (strlen/with-null p2))))

7.2.92 strncat

;; File "string.lisp", line 112, characters 0-122
(defun strncat (dst src len)
  (declare (external "strncat"))
  (strncpy (+ dst (cast ptr_t (strlen dst))) src len)
  dst)

7.2.93 strncmp

;; File "string.lisp", line 141, characters 0-169
(defun strncmp (s1 s2 n)
  (declare (external "strncmp"))
  (memcmp s1 s2 (min n
		     (strlen/with-null s1)
		     (strlen/with-null s2))))

7.2.94 strncpy

;; File "string.lisp", line 24, characters 0-213
(defun strncpy (dst src len)
  (declare (external "strncpy"))
  (let ((dst dst))
    (while (and len (not (points-to-null src)))
      (decr len)
      (copy-byte-shift dst src))
    (memory-write dst 0:8))
  dst)

7.2.95 strpbrk

;; File "string.lisp", line 97, characters 0-248
(defun strpbrk (str set)
  (declare (external "strpbrk"))
  (let ((p set) (found 0))
    (while (and
	    (not (points-to-null p))
	    (not found))
      (set found (strchr str (cast int (memory-read p))))
      (incr p))
    found))

7.2.96 strrchr

;; File "string.lisp", line 88, characters 0-96
(defun strrchr (p c)
  (declare (external "strrchr" "rindex"))
  (memrchr p c (+ (strlen p) 1)))

7.2.97 strsep

;; File "string.lisp", line 205, characters 0-307
(defun strsep (strp sep)
  (declare (external "strsep"))
  (let ((str (read-word ptr_t strp))
	(pos (and str (+ str (strcspn str sep)))))
    (when str
      (if (points-to-null pos)
	  (write-word ptr_t strp 0)
	(memory-write pos 0)
	(write-word ptr_t strp (+1 pos))))
    str))

7.2.98 strstr

;; File "string.lisp", line 181, characters 0-97
(defun strstr (hay needle)
  (declare (external "strstr"))
  (find-substring strncmp hay needle))

7.2.99 strtok

;; File "string.lisp", line 231, characters 0-194
(defun strtok (str sep)
  (declare (external "strtok"))
  (unless *strtok-static-storage*
    (set *strtok-static-storage* (malloc (sizeof ptr_t))))
  (strtok_r str sep *strtok-static-storage*))

7.2.100 strtok_r

;; File "string.lisp", line 217, characters 0-346
(defun strtok_r (str sep ptr)
  (declare (external "strtok_r"))
  (when str (write-word ptr_t ptr str))
  (if (not ptr) ptr
    (let ((str (read-word ptr_t ptr))
	  (del (+ str (strspn str sep)))
	  (str (+ del (strcspn del sep))))
      (if (points-to-null del) 0
	(write-word ptr_t ptr str)
	(memory-write del 0)))))

7.2.101 strxfrm

;; File "string.lisp", line 238, characters 0-92
(defun strxfrm (dst src len)
  (declare (external "strxfrm"))
  (strncpy dst src len)
  len)

7.2.102 textdomain

;; File "libintl.lisp", line 7, characters 0-66
(defun textdomain (dom)
  (declare (external "textdomain"))
  dom)

7.2.103 write

;; File "stdio.lisp", line 71, characters 0-161
(defun write (fd buf cnt)
  (declare (external "write"))
  (let ((written (fwrite buf 1 cnt fd))
	(failure (channel-flush fd)))
    (or failure written)))

7.3 Macros

7.3.1 +1

;; File "init.lisp", line 89, characters 0-65
(defmacro +1 (x)
  "(+1 x) returns the successor of X"
  (+ x 1))

7.3.2 +=

;; File "init.lisp", line 81, characters 0-89
(defmacro += (x y)
  "(+= X Y) assigns a sum of X and Y to variable X"
  (set x (+ x y)))

7.3.3 -1

;; File "init.lisp", line 85, characters 0-67
(defmacro -1 (x)
  "(-1 X) returns the predecessor of X"
  (- x 1))

7.3.4 and

;; File "init.lisp", line 106, characters 0-194
(defmacro and (x)
  "(and X Y ...) evaluates expressions X,Y,... until the first
  expression that returns false. The value of the whole form is the
  value of the last evaluated expression." x)

7.3.5 array-get

;; File "pointers.lisp", line 67, characters 0-153
(defmacro array-get (t p n)
  "(array-get T P N) gets the N-th element of the
   array of elements of type T, pointed by P"
  (read-word t (ptr+ t p n)))

7.3.6 array-set

;; File "pointers.lisp", line 72, characters 0-205
(defmacro array-set (t p n w)
  "(array-set T P N W) sets to W the N-th element of the array of
   elements of type T, pointed by P. Returns a pointer to the next
   element"
  (write-word t (ptr+ p n) w))

7.3.7 assert

;; File "init.lisp", line 136, characters 0-164
(defmacro assert (c)
  "(assert COND) terminates program if COND is false"
  (when (not c)
    (msg "Assertion (assert $0) failed" c)
    (error "Assert_failure")))

7.3.8 assert-msg

;; File "init.lisp", line 142, characters 0-170
(defmacro assert-msg (c s)
  "(assert-msg c s) allows you to assert a condition and print a message on failure"
  (when (not c)
    (msg s)
    (error "Assert_failure")))

7.3.9 bitwidth

;; File "init.lisp", line 176, characters 0-33
(defmacro bitwidth (type) (type))

7.3.10 case

;; File "init.lisp", line 51, characters 0-783
(defmacro case (k x xs)
  "(case K K1 X1 K2 X2 ... Kn Xn [DEF]) evaluates K then consequently
   evaluates keys K1 ...Kn until a key Ki such that (= K Ki) is found.
   If such key is found then the whole form evaluates to Xi.
   If no matching key was found, then if the number of keys is equal to
   the number of expressions, nil is returned, otherwise, i.e., if an
   extra expression DEF is provided, then it becomes the value of the
   whole form.

   Examples:

       (defun dispatch-with-default-case (c)
	 (case c
	   1 'hello
	   2 'cruel
	   3 'world
	   'unknown))

       (defun dispatch-with-no-default (c)
	 (case c
	   1 'hello
	   2 'cruel
	   3 'world))"
  (let ((key k))
    (case/dispatch key x xs)))

7.3.11 case/dispatch

;; File "init.lisp", line 170, characters 0-32
(defmacro case/dispatch (k x) x)

7.3.12 cast

;; File "init.lisp", line 175, characters 0-51
(defmacro cast (type x) (extract  (-1 (type)) 0 x))

7.3.13 compare

;; File "init.lisp", line 131, characters 0-134
(defmacro compare (x y)
  "(compare X Y) returns 0 if X = Y, a negative value if X<Y
   and a positive value if X>Y"
  (sign (- x y)))

7.3.14 copy-byte-shift

;; File "memory.lisp", line 14, characters 0-177
(defmacro copy-byte-shift (dst src)
  "(copy-byte-shift DST SRC) copies byte from
   SRC to DST and increments SRC and DST."
  (prog (copy-byte dst src)
	(incr dst src)))

7.3.15 copy-byte-shift-left

;; File "memory.lisp", line 20, characters 0-187
(defmacro copy-byte-shift-left (dst src)
  "(copy-byte-shift-left DST SRC) copies
   byte from DST to SRC and decrements SRC and DST."
  (prog (copy-byte dst src)
	(decr dst src)))

7.3.16 copy-left

;; File "memory.lisp", line 39, characters 0-162
(defmacro copy-left (dst src len)
  "(copy-left DST SRC LEN) copies LEN bytes
    from SRC to DST (right to left)"
  (make-copy copy-byte-shift-left dst src len))

7.3.17 copy-right

;; File "memory.lisp", line 34, characters 0-159
(defmacro copy-right (dst src len)
  "(copy-right DST SRC LEN) copies LEN bytes
    from SRC to DST (left to right)"
  (make-copy copy-byte-shift dst src len))

7.3.18 decr

;; File "init.lisp", line 100, characters 0-111
(defmacro decr (x)
  "(decr X Y ...) decrements the value bound with the variables X, Y, ..."
  (set x (-1 x)))

7.3.19 endian

;; File "pointers.lisp", line 16, characters 0-154
(defmacro endian (f x y)
  "(endian F X Y) expands to (F Y X) if applied
   in the little endian context"
  (declare (context (endian little)))
  (f y x))

7.3.20 fold

;; File "init.lisp", line 154, characters 0-85
(defmacro fold (f a x)
  "(fold F A X Y ...) expands to (F (F A X) Y) ..."
  (f a x))

7.3.21 incr

;; File "init.lisp", line 93, characters 0-111
(defmacro incr (x)
  "(incr X Y ...) increments the value bound with the variables X, Y, ..."
  (set x (+1 x)))

7.3.22 is-in

;; File "init.lisp", line 148, characters 0-102
(defmacro is-in (x y)
  "(is-in X A B C ...) returns true if X is equal A or B or C or ..."
  (= x y))

7.3.23 make-copy

;; File "memory.lisp", line 26, characters 0-146
(defmacro make-copy (copy-byte dst src len)
  "<internal>"
  (let ((ret dst))
    (while len
      (decr len)
      (copy-byte dst src))
    ret))

7.3.24 max

;; File "init.lisp", line 164, characters 0-85
(defmacro max (x)
  "(max X Y ...) returns the upper bound of the (X,Y,...) set"
  x)

7.3.25 min

;; File "init.lisp", line 159, characters 0-89
(defmacro min (x)
  "(min X Y ...) returns the lower bound of the (X,Y,...) set"
      x)

7.3.26 non-zero

;; File "init.lisp", line 77, characters 0-85
(defmacro non-zero (x)
  "(non-zero X) is true if X is not zero"
  (not (is-zero x)))

7.3.27 nth-byte-of-word

;; File "pointers.lisp", line 28, characters 0-284
(defmacro nth-byte-of-word (t i x)
  "(nth-byte-of-word T N X) returns N-th byte
   of the word X that has type T"
  (let ((n (sizeof t))
	(j (endian - n i))
	(k (if (< j n) (- j 1) (+ j n)))
	(hi (- (* 8 (+ k 1)) 1))
	(lo (* k 8)))
    (extract hi lo x)))

7.3.28 or

;; File "init.lisp", line 113, characters 0-346
(defmacro or (x xs)
  "(or <expr> ...) evaluates a sequence of expressions EXPR from left
   to right until it meets the first expression that evaluates to the
   truth value, that will become the value of the whole form. If no
   expression returned the truth value, then the result of the whole
   form is 0:1"
  (let ((r x)) (if r r (or xs))))

7.3.29 points-to

;; File "pointers.lisp", line 62, characters 0-152
(defmacro points-to (t p v)
  "(points-to T P V) return true if t P points
  to a value of type T that is equal to V."
  (= (read-word t p) (cast t v)))

7.3.30 ptr+

;; File "pointers.lisp", line 6, characters 0-124
(defmacro ptr+ (t p n)
  "(ptr+ T P N) increments N times the
    pointer P to a value of type T."
  (+ p (* (sizeof t) n)))

7.3.31 ptr+1

;; File "pointers.lisp", line 11, characters 0-112
(defmacro ptr+1 (type p)
  "(ptr+1 T P) increments the pointer P to
   a value of to type T."
  (ptr+ type p 1))

7.3.32 read-word

;; File "pointers.lisp", line 38, characters 0-252
(defmacro read-word (t a)
  "(read-word T A) reads a word of type T at address A"
  (let ((p a)
	(x (memory-read p))
	(n (-1 (sizeof t))))
    (while n
      (incr p)
      (decr n)
      (set x (endian concat x (memory-read p))))
    x))

7.3.33 set$

;; File "init.lisp", line 122, characters 0-152
(defmacro set$ (s x)
  "(set$ s x) set the value of the symbol s to x, returns x.
   Like set but without implicit quotation."
  (set-symbol-value s x))

7.3.34 sign

;; File "init.lisp", line 127, characters 0-106
(defmacro sign (x)
  "returns 1 if X > 0, 0 if X = 0, and -1 if X < 0"
  (if (< x 0) -1 (if (> x 0) 1 0)))

7.3.35 sizeof

;; File "init.lisp", line 177, characters 0-46
(defmacro sizeof (type) (/ (bitwidth type) 8))

7.3.36 strspn

;; File "string.lisp", line 189, characters 0-136
(defmacro strspn (str set)
  (declare (external "strspn"))
  (let ((len 0))
    (while (strpbrk str set)
      (incr str len))
    len))

7.3.37 unless

;; File "init.lisp", line 38, characters 0-267
(defmacro unless (cnd body)
  "(unless CND BODY) if CND evaluates to false, then BODY is evaluated and
   the value of the last expression in BODY becomes the value of the
   whole expression. Otherwise, if CND evaluates to true, nil is returned."
  (if cnd () body))

7.3.38 until

;; File "init.lisp", line 44, characters 0-349
(defmacro until (c b)
  "(until COND BODY) if COND evaluates to true, then the whole expression
   evaluates to nil and BODY is not evaluated. Otherwise, if COND evaluates
   to false, then BODY is evaluated until COND evaluates to true and the value
   of the last evaluation of BODY becomes the value of the whole expression."
  (while (not c) b))

7.3.39 when

;; File "init.lisp", line 32, characters 0-270
(defmacro when (cnd body)
  "(when CND BODY) if CND evaluates to true, then BODY is evaluated and
   the value of the last expression in BODY becomes the value of the
   whole expression. Otherwise, if CND evaluates to false, nil is returned."
  (if cnd (prog body) ()))

7.3.40 write-word

;; File "pointers.lisp", line 49, characters 0-329
(defmacro write-word (t a x)
  "(write-word T A X) writes the word X of type T to address A
  returns an address that points to the first byte that follows
  the just written word.
  "
  (let ((p a)
	(n (sizeof t))
	(i 0))
    (while (< i n)
      (memory-write p (nth-byte-of-word t i x))
      (incr p i))
    p))

7.4 Methods

7.4.1 init

;; File "locale.lisp", line 9, characters 0-61
(defmethod init ()
  (set LCONV brk)
  (+= brk *lconv-size*))

7.4.2 machine-kill

;; File "stdio.lisp", line 153, characters 0-105
(defmethod primus:machine-kill ()
  (channel-flush *standard-output*)
  (channel-flush *standard-error*))

7.5 Parameters

7.5.1 *lconv-size*

;; File "locale.lisp", line 5, characters 0-49
(defparameter *lconv-size* (* 20 (sizeof ptr_t)))

7.5.2 *malloc-arena-end*

;; File "simple-memory-allocator.lisp", line 21, characters 0-82
(defparameter *malloc-arena-end* brk
  "the starting address of the malloc arena")

7.5.3 *malloc-arena-initial-size*

;; File "simple-memory-allocator.lisp", line 14, characters 0-149
(defparameter *malloc-arena-initial-size* 0x40000
  "the maximum number of bytes totally allocated by malloc,
   if not set, then there is no limit")

7.5.4 *malloc-arena-start*

;; File "simple-memory-allocator.lisp", line 18, characters 0-84
(defparameter *malloc-arena-start* brk
  "the starting address of the malloc arena")

7.5.5 *malloc-guard-edges*

;; File "simple-memory-allocator.lisp", line 24, characters 0-119
(defparameter *malloc-guard-edges* 0
  "if not nil, then add padding of the specified size
   around allocated chunks")

7.5.6 *malloc-guard-pattern*

;; File "simple-memory-allocator.lisp", line 28, characters 0-91
(defparameter *malloc-guard-pattern* 0xA5
  "a byte that will be used to fill guard edges")

7.5.7 *malloc-initial-value*

;; File "simple-memory-allocator.lisp", line 45, characters 0-91
(defparameter *malloc-initial-value* 0
  "initialize allocated memory with the said value")

7.5.8 *malloc-initialize-memory*

;; File "simple-memory-allocator.lisp", line 34, characters 0-120
(defparameter *malloc-initialize-memory* false
  "if true then initialize allocated memory with *malloc-initial-value*")

7.5.9 *malloc-max-arena-size*

;; File "simple-memory-allocator.lisp", line 10, characters 0-141
(defparameter *malloc-max-arena-size* nil
  "the maximum number of bytes totally allocated by malloc,
   if not set, then there is no limit")

7.5.10 *malloc-max-chunk-size*

;; File "simple-memory-allocator.lisp", line 6, characters 0-124
(defparameter *malloc-max-chunk-size* nil
  "the maximum size of a single memory chunk,
   if nil then there is no limit. ")

7.5.11 *malloc-uniform-max-value*

;; File "simple-memory-allocator.lisp", line 41, characters 0-180
(defparameter *malloc-uniform-max-value* nil
  "if set then defines the lower bound of the uniformely distributed
   random value that is used to represent an unitialized memory ")

7.5.12 *malloc-uniform-min-value*

;; File "simple-memory-allocator.lisp", line 37, characters 0-180
(defparameter *malloc-uniform-min-value* nil
  "if set then defines the lower bound of the uniformely distributed
   random value that is used to represent an unitialized memory ")

7.5.13 *malloc-zero-sentinel*

;; File "simple-memory-allocator.lisp", line 31, characters 0-84
(defparameter *malloc-zero-sentinel* 0
  "a pointer that is returned by (malloc 0)")

7.5.14 *malloc/brk*

;; File "simple-memory-allocator.lisp", line 48, characters 0-31
(defparameter *malloc/brk* brk)

7.5.15 *malloc/total-bytes-allocated*

;; File "simple-memory-allocator.lisp", line 50, characters 0-47
(defparameter *malloc/total-bytes-allocated* 0)

7.5.16 *strtok-static-storage*

;; File "string.lisp", line 229, characters 0-40
(defparameter *strtok-static-storage* 0)

7.6 Primitives

7.6.1 *

(* X Y Z …) returns the product of arguments or 0 if the list of arguments is empty

7.6.2 +

(+ X Y …) returns the sum of arguments, or 0 if there are no arguments,

7.6.3 -

(- X Y Z …) returns X - Y - Z - …, or 0 if there are no arguments.

7.6.4 /

(/ X Y Z …) returns X / Y / Z / … or 0 if the list of arguments is empty

7.6.5 /=

(/= X Y Z …) returns true if at least one argument is not equal to another argument. Returns false if the list of arguments is empty

7.6.6 <

(< X Y Z …) is true if the list of arguments is an strict ascending chain or if it is empty

7.6.7 <=

(< X Y Z …) is true if the list of arguments is an ascending chain or if it is empty

7.6.8 =

(= X Y Z …) returns true if all arguments are equal. True if the list of arguments is empty

7.6.9 >

(< X Y Z …) is true if the list of arguments is a strict descending chain or if it is empty

7.6.10 >=

(< X Y Z …) is true if the list of arguments is a descending chain or if it is empty

7.6.11 all-static-constant

(all-static-constant X Y ..) is true if X,Y,… are static constants. A value is a static constant if it was initialized from a constant value or computed from static constant values.

7.6.12 arshift

(arshift X N) arithmetically shifts X right by N bits

7.6.13 channel-close

(channel-close DESCR) closes a channel that has the specified descriptor DESCR. If no such channel exists, then returns -1. Otherwise returns 0. The descriptor of the closed channel will be reused by the consequent calls to `channel-open'. If the channel had any data associated with it and not yet flushed, then the data is discarded.

7.6.14 channel-flush

(channel-flush DESCR) forces data that were written to a channel that has the descriptor DESCR to be outputted to the associated destination. Returns -1 if no such channel exists or if in case of an IO error.

7.6.15 channel-input

(channel-input DESC) reads one byte from a channel that has the descriptor DESC. Returns -1 if no such channel exists, or if any IO error occurs, if the channel is not readable, or if the channel is in the end-of-file condition.

7.6.16 channel-open

(channel-open PTR) creates a new channel that is associated with a null-terminated path pointed by PTR. Returns a non-negative channel descriptor, if the channel subsystem have a mapping from the obtained path to a physical file and this file is accessible. Otherwise returns a negative value.

7.6.17 channel-output

(channel-output DESCR CHAR …) outputs one or more characters to a channel that has the descriptor DESCR. Returns -1 if no such channel exits, if a channel is not writable, or if any IO error occurs in an associated physical file. Otherwise, returns 0. Note: the channel system is buffered, and the actual IO operation (as well as errors) could be delayed until (channel-flush DESCR) is called.

7.6.18 concat

(concat X Y Z …) concatenates words X, Y, Z, … into one big word

7.6.19 dict-add

(dict-add DIC KEY DATA) associates DATA with KEY in the dictionary DIC. Returns KEY.

7.6.20 dict-del

(dict-del DIC KEY) deletes any association with KEY in the dictionary DIC

7.6.21 dict-first

(dict-first DIC) is the first key in DIC or nil if DIC is empty.

7.6.22 dict-get

(dict-get DIC KEY) returns data associated with KEY in the dictionary DIC, and returns NIL if either DIC doesn't exist on no data are associated

7.6.23 dict-has

(dict-has DIC KEY) returns T if the dictionary DIC has the key KEY

7.6.24 dict-length

(dict-first DIC) is the total number of keys in DIC.

7.6.25 dict-next

(dict-next DIC KEY) returns the next key after KEY Returns nil if KEY was the last key in the dictionary. Could be used together with DICT-FIRST for iterating.

7.6.26 exec-addr

(exec-addr D) passes the control flow to D and never returns

7.6.27 exec-symbol

(exec-symbol D) passes the control flow to D and never returns

7.6.28 exit-with

(exit-with N) terminates program with the exit codeN

7.6.29 extract

(extract HI LO X) extracts bits from HI to LO (including both) from the word X

7.6.30 get-current-program-counter

(get-current-program-counter) returns current program cunnter

7.6.31 ieee754-abs

applies abs to the operand

7.6.32 ieee754-acos

applies acos to the operand

7.6.33 ieee754-add

reduces the list of operands with add

7.6.34 ieee754-asin

applies asin to the operand

7.6.35 ieee754-atan

applies atan to the operand

7.6.36 ieee754-atan2

reduces the list of operands with atan2

7.6.37 ieee754-ceil

applies ceil to the operand

7.6.38 ieee754-cos

applies cos to the operand

7.6.39 ieee754-cosh

applies cosh to the operand

7.6.40 ieee754-cti

truncates to the nearest integer

7.6.41 ieee754-div

reduces the list of operands with div

7.6.42 ieee754-eq

returns true if all operands are ordered with the eq order

7.6.43 ieee754-exp

applies exp to the operand

7.6.44 ieee754-expm1

applies expm1 to the operand

7.6.45 ieee754-floor

applies floor to the operand

7.6.46 ieee754-ge

returns true if all operands are ordered with the ge order

7.6.47 ieee754-gt

returns true if all operands are ordered with the gt order

7.6.48 ieee754-hypot

reduces the list of operands with hypot

7.6.49 ieee754-is-nan

checks if is-nan holds

7.6.50 ieee754-le

returns true if all operands are ordered with the le order

7.6.51 ieee754-log

applies log to the operand

7.6.52 ieee754-log10

applies log10 to the operand

7.6.53 ieee754-log1p

applies log1p to the operand

7.6.54 ieee754-lt

returns true if all operands are ordered with the lt order

7.6.55 ieee754-mod

reduces the list of operands with mod

7.6.56 ieee754-mul

reduces the list of operands with mul

7.6.57 ieee754-ne

returns true if all operands are ordered with the ne order

7.6.58 ieee754-neg

applies neg to the operand

7.6.59 ieee754-pos

applies pos to the operand

7.6.60 ieee754-pow

reduces the list of operands with pow

7.6.61 ieee754-sin

applies sin to the operand

7.6.62 ieee754-sinh

applies sinh to the operand

7.6.63 ieee754-sqrt

applies sqrt to the operand

7.6.64 ieee754-sub

reduces the list of operands with sub

7.6.65 ieee754-tan

applies tan to the operand

7.6.66 ieee754-tanh

applies tanh to the operand

7.6.67 incident-location

7.6.68 incident-report

7.6.69 invoke-subroutine

(invoke-subroutine addr args …) calls the subroutine at the specified address.

7.6.70 is-negative

(is-negative X Y …) returns true if all arguments are negative

7.6.71 is-positive

(is-positive X Y …) returns true if all arguments are positive

7.6.72 is-zero

(is-zero X Y …) returns true if all arguments are zeros

7.6.73 lnot

(lnot X) returns the one complement of X

7.6.74 logand

(logand X Y Z …) returns X & Y & Z & … or 0 if the list of arguments is empty, where & is the bitwise AND operation. Returns ~0 if the list of arguments is empty

7.6.75 logor

(logor X Y Z …) returns X | Y | Z | … or 0 if the list of arguments is empty, where | is the bitwise OR operation

7.6.76 logxor

(logxor X Y Z …) returns X ^ Y ^ Z ^ … or 0 if the list of arguments is empty, where ^ is the bitwise XOR operation

7.6.77 lshift

(lshift X N) logically shifts X left by N bits

7.6.78 memory-allocate

(memory-allocate P N V?) maps memory region [P,P+N), if V is provided, then fills the newly mapped region with the value V

7.6.79 memory-read

(memory-read A) loads one byte from the address A

7.6.80 memory-write

(memory-write A X) stores by X to A

7.6.81 mod

(mod X Y Z …) returns X % Y % Z % … or 0 if the list of arguments is empty, where % is the modulo operation

7.6.82 neg

(neg X) returns the two complement of X

7.6.83 not

(not X) returns true if X is zero

7.6.84 points-to-static-data

(points-to-static-data PTR LEN) is true iff (all-static-constant *PTR .. *(PTR+LEN-1))

7.6.85 reg-name

(reg-name N) returns the name of the register with the index N

7.6.86 region-contains

(region-contains ID X) return the region in ID that has X. Returns the lower bound of the first region that contains value X in the set of regions with the given ID. Returns nil otherwise. Returns nil if a set with the given ID doesn't exist.

7.6.87 region-count

(region-count ID) the total number of regions in the set ID. Counts the number of regions (including intersecting) stored in the set of regions referenced by the symbol ID. Returns nil if there is no set with the given ID, otherwise returns the number of regions in that set.

7.6.88 region-create

(region-create ID LOWER UPPER) adds [LOWER,UPPER] to the set ID. Adds a region denoted with the interval [LOWER,UPPER] to the set of regions denoted by the symbol ID. Values LOWER and UPPER are included into the interval. If the set of regions ID doesn't exist, then it is created.

7.6.89 region-lower

(region-lower ID X) the lower bound of region that contains X. Returns nil if ID doesn't exist or if it doesn't have a region that includes X. This fucntion is an alias for REGION-CONTAINS. See also, REGION-UPPER.

7.6.90 region-move

(region-move DST SRC P) moves all regions that contain the point P from the set SRC to the set DST. Returns nil if SRC didn't contain any such region, otherwise returns t.

7.6.91 region-upper

(region-upper ID X) the upper bound of the region that contains X. Returns the upper bound of the region that contains point X or nil if there is no such region or such set of regions. See also, REGION-LOWER.

7.6.92 rshift

(rshift X N) logically shifts X right by N bits

7.6.93 s/

(s/ X Y Z …) returns X s/ Y s/ Z s/ … or 0 if the list of arguments is empty, where s/ is the signed division operation

7.6.94 set-symbol-value

(set-symbol-value S X) sets the value of the symbol S to X. Returns X

7.6.95 signed-mod

(signed-mod X Y Z …) returns X % Y % Z % … or 0 if the list of arguments is empty, where % is the signed modulo operation

7.6.96 symbol-concat

(symbol-concat X Y Z …) returns a new symbol that is a concatenation of symbols X,Y,Z,…

7.6.97 symbol-of-string

(symbol-of-string ptr) returns a symbol from a null-terminated string.

7.6.98 taint-get-direct

(taint-get-direct K X) returns the direct taint of the kind K associatedwith the value X, or nil if there is no such taint

7.6.99 taint-get-indirect

(taint-get-indirect K X) returns the indirect taint of the kind K associated with the value X, or nil if there is no such taint

7.6.100 taint-introduce-directly

(taint-introduce-directly K X) introduces a new taint of the kind K that is directly associated with the value X

7.6.101 taint-introduce-indirectly

(taint-introduce-indirectly K X N) introduces a new taint of the kind K that is indirectly associated with X pointing to an object of the size N

7.6.102 taint-kind

(taint-kind t) returns the kind of the taint T.

7.6.103 taint-policy-select

(taint-policy-select K P) selects the taint propagation policy P for the taints of the kind K

7.6.104 taint-policy-set-default

(taint-policy-set-default P) makes P the default taint propagation policy.

7.6.105 taint-sanitize-direct

(taint-sanitize-direct K X) removes any direct taint of the kind K that is directly associated with the value X

7.6.106 taint-sanitize-indirect

(taint-sanitize-indirect K X) removes any direct taint of the kind K that is indirectly associated with the value X

7.6.107 word-width

(word-width) returns machine word width in bits

7.7 Signals

7.7.1 call

(call NAME X Y …) is emitted when a call to a function with the symbolic NAME occurs with the specified list of arguments X,Y,…

7.7.2 call-return

(call-return NAME X Y … R) is emitted when a call to a function with the symbolic NAME returns with the specified list of arguments X,Y,… and return value R.

7.7.3 fini

(fini) occurs when the Primus Machine is finished

7.7.4 init

(init) occurs when the Primus Machine is initialized

7.7.5 interrupt

(interrupt N) is emitted when the hardware interrupt N occurs

7.7.6 jumping

(jumping C D) is emitted before jump to D occurs under the condition C

7.7.7 loaded

(loaded A X) is emitted when X is loaded from A

7.7.8 loading

(loading A) is emitted before load from A occurs

7.7.9 machine-kill

(machine-kill) occurs when Machine is killed and could be used for machine cleanup/teardown and analysis summaries. The machine is in the resticted mode in the body of the methods.

7.7.10 pc-changed

(pc-change PC) is emitted when PC is updated

7.7.11 read

(read V X) is emitted when X is read from V

7.7.12 stored

(stored A X) is emitted when X is stored to A

7.7.13 storing

(storing A) is emitted before store to A occurs

7.7.14 system-stop

(system-stop NAME) occurs when the system with the given name finished its execution. The machine is in the restricted mode in the body of the methods

7.7.15 taint-finalize

(taint-finalize T L) is emitted when the taint T is finilized while still live if L is true or dead if T is false.

7.7.16 written

(written V X) is emitted when X is written to V

Created: 2023-01-07 Sat 01:58

Emacs 25.2.2 (Org mode 8.2.10)

Validate