utils.lisp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ;;; -*- Mode: Lisp; show-trailing-whitespace: t; Base: 10; indent-tabs: nil; Syntax: ANSI-Common-Lisp; Package: UTILS; -*-
  2. ;;; Copyright (c) 2013, Mark VandenBrink. All rights reserved.
  3. (in-package #:utils)
  4. (eval-when (:compile-toplevel :load-toplevel :execute)
  5. (pushnew :INSTRUMENT-MEMOIZED *features*)
  6. (defvar *standard-optimize-settings* '(optimize (speed 3) (safety 0) (space 0) (debug 0))))
  7. (defparameter *break-on-warn-user* nil "set to T if you'd like to stop in warn-user")
  8. (defun warn-user (format-string &rest args)
  9. "Print a warning error to *ERROR-OUTPUT* and continue"
  10. (when *break-on-warn-user* (break "Breaking in WARN-USER"))
  11. (format *error-output* "~&********************************************************************************~%")
  12. #+CCL (format *error-output* "~&WARNING in ~a:: " (ccl::%last-fn-on-stack 1))
  13. (apply #'format *error-output* format-string args)
  14. (format *error-output* "~&**********************************************************************************~%"))
  15. (defparameter *max-raw-bytes-print-len* 10 "Max number of octets to print from an array")
  16. (defun printable-array (array &optional (max-len *max-raw-bytes-print-len*))
  17. "Given an array, return a string of the first *MAX-RAW-BYTES-PRINT-LEN* bytes"
  18. (declare #.utils:*standard-optimize-settings*)
  19. (let* ((len (length array))
  20. (print-len (min len max-len))
  21. (printable-array (make-array print-len :displaced-to array)))
  22. (declare (fixnum max-len len)
  23. (type (array (unsigned-byte 8) 1) array))
  24. (format nil "[~:d of ~:d bytes] <~x>" print-len len printable-array)))
  25. (declaim (inline upto-null))
  26. (defun upto-null (string)
  27. "Trim STRING to end at first NULL found"
  28. (subseq string 0 (position #\Null string)))
  29. (defun dump-data (file-name data)
  30. (with-open-file (f file-name :direction :output :if-exists :supersede :element-type '(unsigned-byte 8))
  31. (write-sequence data f)))
  32. (defmacro redirect (filename &rest body)
  33. "Temporarily set *STANDARD-OUTPUT* to FILENAME and execute BODY."
  34. `(let ((*standard-output* (open ,filename :direction :output :if-does-not-exist :create :if-exists :supersede)))
  35. ,@body
  36. (finish-output *standard-output*)))
  37. (defun get-bitmask(start width)
  38. "Create a bit mask that begins at bit START (31 is MSB) and is WIDTH bits wide.
  39. Example: (get-bitmask 31 11) -->> #xffe00000"
  40. (declare #.utils:*standard-optimize-settings*)
  41. (ash (- (ash 1 width) 1) (- (1+ start) width)))
  42. (defmacro get-bitfield (int start width)
  43. "Extract WIDTH bits from INT starting at START
  44. Example: (get-bitfield #xFFFBB240 31 11) -->> #x7ff.
  45. The above will expand to (ash (logand #xFFFBB240 #xFFE00000) -21) at COMPILE time."
  46. `(ash (logand ,int ,(utils::get-bitmask start width)) ,(- ( - start width -1))))
  47. ;;;;;;;;;;;;;;;;;;;; convenience macros ;;;;;;;;;;;;;;;;;;;;
  48. (defmacro with-gensyms (syms &body body)
  49. `(let ,(mapcar #'(lambda (s)
  50. `(,s (gensym)))
  51. syms)
  52. ,@body))
  53. (defun make-keyword (name)
  54. (intern (string name) :keyword))
  55. (defmacro while (test &body body)
  56. `(do ()
  57. ((not ,test))
  58. ,@body))
  59. (defmacro aif (test-form then-form &optional else-form)
  60. `(let ((it ,test-form))
  61. (if it ,then-form ,else-form)))
  62. (defmacro awhen (test-form &body body)
  63. `(aif ,test-form
  64. (progn ,@body)))
  65. #+INSTRUMENT-MEMOIZED (progn
  66. (defstruct memoized-funcs
  67. name
  68. table
  69. calls
  70. finds
  71. news)
  72. (defvar *memoized-funcs* nil))
  73. (defun mk-memoize (func-name)
  74. "Takes a normal function object and returns a memoized one"
  75. (let* ((func (symbol-function func-name))
  76. (hash-table (make-hash-table :test 'equal))
  77. #+INSTRUMENT-MEMOIZED (s (make-memoized-funcs :table hash-table :calls 0 :finds 0 :news 0 :name func-name))
  78. )
  79. #+INSTRUMENT-MEMOIZED (push s *memoized-funcs*)
  80. #'(lambda (arg)
  81. (multiple-value-bind (value foundp) (gethash arg hash-table)
  82. #+INSTRUMENT-MEMOIZED (incf (memoized-funcs-calls s))
  83. (if foundp
  84. (progn
  85. #+INSTRUMENT-MEMOIZED (incf (memoized-funcs-finds s))
  86. value)
  87. (progn
  88. #+INSTRUMENT-MEMOIZED (incf (memoized-funcs-news s))
  89. (setf (gethash arg hash-table) (funcall func arg))))))))
  90. (defmacro memoize (func-name)
  91. "Memoize function associated with Function-Name. Simplified version"
  92. `(setf (symbol-function ,func-name) (utils::mk-memoize ,func-name)))
  93. (defun timings (function)
  94. (let ((real-base (get-internal-real-time)))
  95. (funcall function)
  96. (float (/ (- (get-internal-real-time) real-base) internal-time-units-per-second))))
  97. ;;; Taken from ASDF
  98. (defmacro DBG (tag &rest exprs)
  99. "debug macro for print-debugging:
  100. TAG is typically a constant string or keyword to identify who is printing,
  101. but can be an arbitrary expression returning a tag to be princ'ed first;
  102. if the expression returns NIL, nothing is printed.
  103. EXPRS are expressions, which when the TAG was not NIL are evaluated in order,
  104. with their source code then their return values being printed each time.
  105. The last expresion is *always* evaluated and its multiple values are returned,
  106. but its source and return values are only printed if TAG was not NIL;
  107. previous expressions are not evaluated at all if TAG returned NIL.
  108. The macro expansion has relatively low overhead in space or time."
  109. (let* ((last-expr (car (last exprs)))
  110. (other-exprs (butlast exprs))
  111. (tag-var (gensym "TAG"))
  112. (thunk-var (gensym "THUNK")))
  113. `(let ((,tag-var ,tag))
  114. (flet ,(when exprs `((,thunk-var () ,last-expr)))
  115. (if ,tag-var
  116. (DBG-helper ,tag-var
  117. (list ,@(loop :for x :in other-exprs :collect
  118. `(cons ',x #'(lambda () ,x))))
  119. ',last-expr ,(if exprs `#',thunk-var nil))
  120. ,(if exprs `(,thunk-var) '(values)))))))
  121. (defun DBG-helper (tag expressions-thunks last-expression last-thunk)
  122. ;; Helper for the above debugging macro
  123. (labels
  124. ((f (stream fmt &rest args)
  125. (with-standard-io-syntax
  126. (let ((*print-readably* nil)
  127. (*package* (find-package :cl)))
  128. (apply 'format stream fmt args)
  129. (finish-output stream))))
  130. (z (stream)
  131. (f stream "~&"))
  132. (e (fmt arg)
  133. (f *error-output* fmt arg))
  134. (x (expression thunk)
  135. (e "~& ~S => " expression)
  136. (let ((results (multiple-value-list (funcall thunk))))
  137. (e "~{~S~^ ~}~%" results)
  138. (apply 'values results))))
  139. (map () #'z (list *standard-output* *error-output* *trace-output*))
  140. (e "~A~%" tag)
  141. (loop :for (expression . thunk) :in expressions-thunks
  142. :do (x expression thunk))
  143. (if last-thunk
  144. (x last-expression last-thunk)
  145. (values))))