Commit 6478fbb9 authored by rtoy's avatar rtoy
Browse files

More updates from Paul.

code/extfmts.lisp:
o Fixed bug with shared code between formats
o Built a cache into the ef-macro functions so it doesn't need to call
  find-external-format so often at runtime

code/fd-stream-extfmt.lisp
o Use the changes in code/extfmts

code/fd-stream.lisp:
o Removed all the commented-out code in fd-stream which is duplicated
  in fd-stream-extfmt.
parent ed96e4b3
Loading
Loading
Loading
Loading
+69 −63
Changes for code/extfmts.lisp: 69 added lines, 63 removed lines.
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
;;; domain.
;;; 
(ext:file-comment
 "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/extfmts.lisp,v 1.2.4.3.2.7 2008/07/09 15:52:12 rtoy Exp $")
 "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/extfmts.lisp,v 1.2.4.3.2.8 2008/07/14 14:01:56 rtoy Exp $")
;;;
;;; **********************************************************************
;;;
@@ -122,7 +122,7 @@
				       &optional octets-to-code code-to-octets)
  (when (and (oddp (length args)) (not (= (length args) 1)))
    (warn "Nonsensical argument (~S) to DEFINE-EXTERNAL-FORMAT." args))
  (let* ((tmp1 (gensym)) (tmp2 (gensym))
  (let* ((tmp (gensym))
	 (min (if (evenp (length args))
		  (or (getf args :min) (getf args :size) 1)
		  1))
@@ -136,26 +136,25 @@
	 (slotd (%merge-slots bslotd slots))
	 (slotb (loop for slot in slotd
		  collect `(,(first slot)
			    (the ,(fourth slot)
			      (identity (svref ,tmp1 ,(second slot))))))))
			    `(the ,',(fourth slot)
			      ;; IDENTITY is here to protect against SETF
			      (identity (svref %slots% ,',(second slot))))))))
    `(macrolet ((octets-to-code ((state input unput &rest vars) body)
		  `(lambda (,',tmp1 ,state ,input ,unput)
		     (declare (type simple-vector ,',tmp1)
			      (ignorable ,state ,input ,unput)
		  `(lambda (,state ,input ,unput)
		     (declare (ignorable ,state ,input ,unput)
			      (optimize (ext:inhibit-warnings 3)))
		     (let (,@',slotb
			   (,input `(the (or (unsigned-byte 8) null) ,,input))
			   ,@(loop for var in vars collect `(,var (gensym))))
		       ,body)))
		(code-to-octets ((code state output &rest vars) body)
		  `(lambda (,',tmp1 ,',tmp2 ,state ,output)
		     (declare (type simple-vector ,',tmp1)
			      (ignorable ,state ,output)
		  `(lambda (,',tmp ,state ,output)
		     (declare (ignorable ,state ,output)
			      (optimize (ext:inhibit-warnings 3)))
		     (let (,@',slotb
			   (,code ',code)
			   ,@(loop for var in vars collect `(,var (gensym))))
		       `(let ((,',code (the (unsigned-byte 31) ,,',tmp2)))
		       `(let ((,',code (the (unsigned-byte 31) ,,',tmp)))
			  (declare (ignorable ,',code))
			  ,,body)))))
       (%intern-ef (make-external-format ,name
@@ -180,13 +179,12 @@
;;;
(defmacro define-composing-external-format (name (&key min max size)
						 input output)
  (let ((tmp1 (gensym)) (tmp2 (gensym))
  (let ((tmp (gensym))
	(min (or min size 1))
	(max (or max size 1)))
    `(macrolet ((input ((state input unput &rest vars) body)
		  `(lambda (,',tmp1 ,state ,input ,unput)
		     (declare (ignore ,',tmp1)
			      (ignorable ,state ,input ,unput)
		  `(lambda (,state ,input ,unput)
		     (declare (ignorable ,state ,input ,unput)
			      (optimize (ext:inhibit-warnings 3)))
		     (let ((,input `(the (values (or (unsigned-byte 31) null)
						 kernel:index)
@@ -194,13 +192,12 @@
			   ,@(loop for var in vars collect `(,var (gensym))))
		       ,body)))
		(output ((code state output &rest vars) body)
		  `(lambda (,',tmp1 ,',tmp2 ,state ,output)
		     (declare (ignore ,',tmp1)
			      (ignorable ,state ,output)
		  `(lambda (,',tmp ,state ,output)
		     (declare (ignorable ,state ,output)
			      (optimize (ext:inhibit-warnings 3)))
		     (let ((,code ',code)
			   ,@(loop for var in vars collect `(,var (gensym))))
		       `(let ((,',code (the (unsigned-byte 31) ,,',tmp2)))
		       `(let ((,',code (the (unsigned-byte 31) ,,',tmp)))
			  (declare (ignorable ,',code))
			  ,,body)))))
       (%intern-ef (make-external-format ,name
@@ -266,23 +263,15 @@
  (make-external-format
   (%composed-ef-name (ef-name a) (ef-name b))
   (make-efx
    :octets-to-code (lambda (tmp state input unput)
		      (declare (ignore tmp))
		      (funcall (ef-octets-to-code b) (ef-slots b)
			       state
			       (funcall (ef-octets-to-code a) (ef-slots a)
					state
					input
					unput)
    :octets-to-code (lambda (state input unput)
		      (funcall (ef-octets-to-code b) state
			       (funcall (ef-octets-to-code a)
					state input unput)
			       unput))
    :code-to-octets (lambda (tmp code state output)
		      (declare (ignore tmp))
		      (funcall (ef-code-to-octets b) (ef-slots b)
			       code
			       state
    :code-to-octets (lambda (code state output)
		      (funcall (ef-code-to-octets b) code state
			       `(lambda (x)
				 ,(funcall (ef-code-to-octets a)
					   (ef-slots a)
					   'x state output))))
    :cache (make-array +ef-max+ :initial-element nil)
    :min (* (ef-min-octets a) (ef-min-octets b))
@@ -382,17 +371,16 @@
;;; to read or write texts containing characters not supported by your Lisp,
;;; these macros can be used instead.
(defmacro octets-to-codepoint (external-format state count input unput)
  (let ((tmp1 (gensym)) (tmp2 (gensym)))
    `(let ((body (funcall (ef-octets-to-code ,external-format)
			  (ef-slots ,external-format)
			  ',state ',input ',unput)))
       `(multiple-value-bind (,',tmp1 ,',tmp2) ,body
	  (setf ,',count (the kernel:index ,',tmp2))
	  (the (or (unsigned-byte 31) null) ,',tmp1)))))
  (let ((tmp1 (gensym)) (tmp2 (gensym))
	(ef (find-external-format external-format)))
    `(multiple-value-bind (,tmp1 ,tmp2)
	 ,(funcall (ef-octets-to-code ef) state input unput)
       (setf ,count (the kernel:index ,tmp2))
       (the (or (unsigned-byte 31) null) ,tmp1))))

(defmacro codepoint-to-octets (external-format code state output)
  `(funcall (ef-code-to-octets ,external-format) (ef-slots ,external-format)
	    ',code ',state ',output))
  (let ((ef (find-external-format external-format)))
    (funcall (ef-code-to-octets ef) code state output)))



@@ -412,22 +400,38 @@
;;;
;;; 
(defmacro def-ef-macro (name (ef id reqd idx) body)
  (let ((tmp (gensym)))
    `(defun ,name (,ef)
       (let ((,tmp ,(if (eq id 'lisp::lisp)
  (let ((tmp1 (gensym))
	(tmp2 (gensym))
	(%name (intern (format nil "%~A" name) (symbol-package name))))
    `(progn
       (defun ,%name (,ef)
	 (let* ((,tmp1 (find-external-format ,ef))
		(,tmp2 ,(if (eq id 'lisp::lisp)
			    idx
			`(+ (ensure-cache ,ef ',id ,reqd) ,idx))))
	 (or (aref (ef-cache ,ef) ,tmp)
	     (setf (aref (ef-cache ,ef) ,tmp)
			    `(+ (ensure-cache ,tmp1 ',id ,reqd) ,idx))))
	   (funcall (or (aref (ef-cache ,tmp1) ,tmp2)
			(setf (aref (ef-cache ,tmp1) ,tmp2)
			    (let ((*compile-print* nil)
				  ;; Set default format when we compile so we
			 ;; can see compiler messages.  If we don't,
			 ;; we run into the problem that we might be
				  ;; can see compiler messages.  if we don't,
				  ;; we run into a problem that we might be
				  ;; changing the default format while we're
				  ;; compiling, and we don't know how to output
				  ;; the compiler messages.
				  (*default-external-format* :iso8859-1))
		     (compile nil ,body))))))))
			      (compile nil `(lambda (%slots%)
					      (declare (ignorable %slots%))
					      ,,body)))))
		    (ef-slots ,tmp1))))
       (declaim (inline ,name))
       (defun ,name (,tmp1)
	 (let ((,tmp2 (load-time-value (cons nil nil))))
	   (when (eq ,tmp1 :default)
	     (setq ,tmp1 *default-external-format*))
	   (if (eq ,tmp1 (car ,tmp2))
	       (cdr ,tmp2)
	       (setf (car ,tmp2) ,tmp1
		     (cdr ,tmp2) (,%name ,tmp1))))))))



@@ -436,13 +440,12 @@
;;; Read and write one character through an external-format
;;;
(defmacro octets-to-char (external-format state count input unput)
  `(let ((body (octets-to-codepoint ,external-format
  `(let ((code (octets-to-codepoint ,external-format
				    ,state ,count ,input ,unput)))
     `(let ((code ,body))
     (declare (type (unsigned-byte 31) code))
     (if (< code char-code-limit) (code-char code)
	 #-(and unicode (not unicode-bootstrap)) #\?
	    #+(and unicode (not unicode-bootstrap)) #\U+FFFD))))
	 #+(and unicode (not unicode-bootstrap)) #\U+FFFD)))

(defmacro char-to-octets (external-format char state output)
  `(codepoint-to-octets ,external-format (char-code ,char) ,state ,output))
@@ -457,7 +460,7 @@
	      (ignorable state))
     (dotimes (i (- end start) (values buffer ptr))
       (declare (type kernel:index i))
       ,(char-to-octets extfmt (schar string (+ start i)) state
       (char-to-octets ,extfmt (schar string (+ start i)) state
		       (lambda (b)
			 (when (= ptr (length buffer))
			   (setq buffer (adjust-array buffer (* 2 ptr))))
@@ -473,7 +476,7 @@
					:element-type '(unsigned-byte 8)))))
    (multiple-value-bind (buffer ptr)
	(lisp::with-array-data ((string string) (start start) (end end))
	  (funcall (ef-string-to-octets (find-external-format external-format))
	  (funcall (ef-string-to-octets external-format)
		   string start end buffer))
      (values (if bufferp buffer (lisp::shrink-vector buffer ptr)) ptr))))

@@ -489,7 +492,7 @@
	do (when (= pos (length string))
	     (setq string (adjust-array string (* 2 pos))))
	   (setf (schar string (incf pos))
	       ,(octets-to-char extfmt state count
	       (octets-to-char ,extfmt state count
			       (aref octets (incf ptr)) ;;@@ EOF??
			       (lambda (n) (decf ptr n))))
	finally (return (values string (1+ pos))))))
@@ -501,7 +504,7 @@
	   (type (or kernel:index null) end)
	   (type (or simple-string null) string))
  (multiple-value-bind (string pos)
      (funcall (ef-octets-to-string (find-external-format external-format))
      (funcall (ef-octets-to-string external-format)
	       octets (1- start) (1- (or end (length octets)))
	       (or string (make-string (length octets))))
    (values (if stringp string (lisp::shrink-vector string pos)) pos)))
@@ -517,7 +520,7 @@
	      (ignorable state))
     (dotimes (i (- end start) (values result ptr))
       (declare (type kernel:index i))
       ,(char-to-octets extfmt (schar string (+ start i)) state
       (char-to-octets ,extfmt (schar string (+ start i)) state
		       (lambda (b)
			 (when (= ptr (length result))
			   (setq result (adjust-array result (* 2 ptr))))
@@ -525,10 +528,11 @@
			     (code-char b)))))))

(defun string-encode (string external-format &optional (start 0) end)
  (when (zerop (length string))
    (return-from string-encode string))
  (multiple-value-bind (result ptr)
      (lisp::with-array-data ((string string) (start start) (end end))
	(funcall (ef-encode (find-external-format external-format))
		 string start end
	(funcall (ef-encode external-format) string start end
		 (make-string (length string) :element-type 'base-char)))
    (lisp::shrink-vector result ptr)))

@@ -544,7 +548,7 @@
	;; increasing size of result shouldn't ever be necessary, unless
	;; someone implements an encoding smaller than the source string...
	do (setf (schar result (incf pos))
	       ,(octets-to-char extfmt state count
	       (octets-to-char ,extfmt state count
			       ;; note the need to return NIL for EOF
			       (if (= (1+ ptr) (length string))
				   nil
@@ -553,8 +557,10 @@
	finally (return (values result (1+ pos))))))

(defun string-decode (string external-format &optional (start 0) end)
  (when (zerop (length string))
    (return-from string-decode string))
  (multiple-value-bind (result pos)
      (lisp::with-array-data ((string string) (start start) (end end))
	(funcall (ef-decode (find-external-format external-format))
	(funcall (ef-decode external-format)
		 string (1- start) (1- end) (make-string (length string))))
    (lisp::shrink-vector result pos)))
+6 −11
Changes for code/fd-stream-extfmt.lisp: 6 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
;;; domain.
;;; 
(ext:file-comment
 "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/fd-stream-extfmt.lisp,v 1.1.2.1 2008/07/05 12:37:42 rtoy Exp $")
 "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/fd-stream-extfmt.lisp,v 1.1.2.2 2008/07/14 14:01:56 rtoy Exp $")
;;;
;;; **********************************************************************
;;;
@@ -14,8 +14,7 @@
(in-package "LISP")

(defun output-char-none-buffered (stream char)
  (funcall (ef-cout (stream::find-external-format
		     (fd-stream-external-format stream)))
  (funcall (ef-cout (fd-stream-external-format stream))
	   stream char)
  (if (char= char #\Newline)
      (setf (fd-stream-char-pos stream) 0)
@@ -24,8 +23,7 @@
  (values))

(defun output-char-line-buffered (stream char)
  (funcall (ef-cout (stream::find-external-format
		     (fd-stream-external-format stream)))
  (funcall (ef-cout (fd-stream-external-format stream))
	   stream char)
  (if (char= char #\Newline)
      (progn (setf (fd-stream-char-pos stream) 0)
@@ -34,8 +32,7 @@
  (values))

(defun output-char-full-buffered (stream char)
  (funcall (ef-cout (stream::find-external-format
		     (fd-stream-external-format stream)))
  (funcall (ef-cout (fd-stream-external-format stream))
	   stream char)
  (if (char= char #\Newline)
      (setf (fd-stream-char-pos stream) 0)
@@ -54,8 +51,7 @@
					   :from-end t
					   :start start
					   :end end))))
	  (funcall (ef-sout (stream::find-external-format 
			     (fd-stream-external-format stream)))
	  (funcall (ef-sout (fd-stream-external-format stream))
		   stream thing start end)
	  (ecase (fd-stream-buffering stream)
	    (:full #| do nothing |#)
@@ -84,8 +80,7 @@
      (prog1 (fd-stream-unread stream)
	(setf (fd-stream-unread stream) nil)
	(setf (fd-stream-listen stream) nil))
      (let ((char (funcall (ef-cin (stream::find-external-format
				    (fd-stream-external-format stream)))
      (let ((char (funcall (ef-cin (fd-stream-external-format stream))
			   stream)))
	(if char
	    char
+7 −125
Changes for code/fd-stream.lisp: 7 added lines, 125 removed lines.
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
;;; Carnegie Mellon University, and has been placed in the public domain.
;;;
(ext:file-comment
  "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/fd-stream.lisp,v 1.85.4.1.2.5 2008/07/07 14:46:13 rtoy Exp $")
  "$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/fd-stream.lisp,v 1.85.4.1.2.6 2008/07/14 14:01:56 rtoy Exp $")
;;;
;;; **********************************************************************
;;;
@@ -403,7 +403,7 @@
  (setf (sap-ref-8 (fd-stream-obuf-sap stream) (fd-stream-obuf-tail stream))
	(char-code byte)))

#+(and unicode (not extfmts))
#+unicode
(def-output-routines ("OUTPUT-CHAR-~A-BUFFERED"
		      1
		      (:none character)
@@ -476,7 +476,7 @@
	    (len (fd-stream-obuf-length stream))
	    (tail (fd-stream-obuf-tail stream)))
       (declare (type sys:system-area-pointer sap) (type index len tail))
       ,(stream::char-to-octets extfmt
       (stream::char-to-octets ,extfmt
			       char
			       (fd-stream-co-state stream)
			       (lambda (byte)
@@ -487,56 +487,6 @@
				 (setf (bref sap (1- (incf tail))) byte)))
       (setf (fd-stream-obuf-tail stream) tail))))

#+(and unicode extfmts)
(defun output-char-none-buffered (stream char)
  (funcall (ef-cout (stream::find-external-format
		     (fd-stream-external-format stream)))
	   stream char)
  (if (char= char #\Newline)
      (setf (fd-stream-char-pos stream) 0)
      (incf (fd-stream-char-pos stream)))
  (flush-output-buffer stream)
  (values))
#+(and unicode extfmts)
(setf *output-routines*
    (nconc *output-routines*
	   ;; check that using 1 here doesn't cause problems if the size
	   ;; of a character is NOT actually 1 octet...
	   (list (list 'character :none 'output-char-none-buffered 1))))

#+(and unicode extfmts)
(defun output-char-line-buffered (stream char)
  (funcall (ef-cout (stream::find-external-format
		     (fd-stream-external-format stream)))
	   stream char)
  (if (char= char #\Newline)
      (progn (setf (fd-stream-char-pos stream) 0)
	     (flush-output-buffer stream))
      (incf (fd-stream-char-pos stream)))
  (values))
#+(and unicode extfmts)
(setf *output-routines*
    (nconc *output-routines*
	   ;; check that using 1 here doesn't cause problems if the size
	   ;; of a character is NOT actually 1 octet...
	   (list (list 'character :line 'output-char-line-buffered 1))))

#+(and unicode extfmts)
(defun output-char-full-buffered (stream char)
  (funcall (ef-cout (stream::find-external-format
		     (fd-stream-external-format stream)))
	   stream char)
  (if (char= char #\Newline)
      (setf (fd-stream-char-pos stream) 0)
      (incf (fd-stream-char-pos stream)))
  (values))
#+(and unicode extfmts)
(setf *output-routines*
    (nconc *output-routines*
	   ;; check that using 1 here doesn't cause problems if the size
	   ;; of a character is NOT actually 1 octet...
	   (list (list 'character :full 'output-char-full-buffered 1))))


;;; OUTPUT-RAW-BYTES -- public
;;;
@@ -632,7 +582,7 @@
	    (tail (fd-stream-obuf-tail stream)))
       (declare (type sys:system-area-pointer sap) (type index len tail))
       (dotimes (i (- end start))
	 ,(stream::char-to-octets extfmt
	 (stream::char-to-octets ,extfmt
				 (schar string (+ i start))
				 (fd-stream-co-state stream)
				 (lambda (byte)
@@ -643,39 +593,6 @@
				   (setf (bref sap (1- (incf tail))) byte))))
       (setf (fd-stream-obuf-tail stream) tail))))

#+(and unicode extfmts)
;; an fd-sout that works with external-formats; needs slots in fd-stream
(defun fd-sout (stream thing start end)
  (let ((start (or start 0))
	(end (or end (length (the vector thing)))))
    (declare (type index start end))
    (if (stringp thing)
	(let ((last-newline (and (find #\newline (the simple-string thing)
				       :start start :end end)
				 (position #\newline (the simple-string thing)
					   :from-end t
					   :start start
					   :end end))))
	  (funcall (ef-sout (stream::find-external-format 
			     (fd-stream-external-format stream)))
		   stream thing start end)
	  (ecase (fd-stream-buffering stream)
	    (:full #| do nothing |#)
	    (:line
	     (when last-newline
	       (flush-output-buffer stream)))
	    (:none
	     (flush-output-buffer stream)))
	  (if last-newline
	      (setf (fd-stream-char-pos stream)
		    (- end last-newline 1))
	      (incf (fd-stream-char-pos stream)
		    (- end start))))
	(ecase (fd-stream-buffering stream)
	  ((:line :full)
	   (output-raw-bytes stream thing start end))
	  (:none
	   (do-output stream thing start end nil))))))

#-unicode
(defun fd-sout (stream thing start end)
@@ -709,7 +626,7 @@
	  (:none
	   (do-output stream thing start end nil))))))

#+(and unicode (not extfmts))
#+unicode
(defun fd-sout (stream thing start end)
  (declare (type string thing))
  (let ((start (or start 0))
@@ -722,17 +639,6 @@
	     ((>= index end))
	   (funcall out stream (elt thing index))))))))

#+(or) ; a lame sout hack to make external-format work quickly
(defun fd-sout-each-character (stream thing start end)
  (declare (type string thing))
  (let ((start (or start 0))
	(end (or end (length (the vector thing)))))
    (declare (type index start end))
    (let ((out (fd-stream-out stream)))
      (do ((index start (+ index 1)))
          ((>= index end))
        (funcall out stream (aref thing index))))))

(defmacro output-wrapper ((stream size buffering) &body body)
  (let ((stream-var (gensym)))
    `(let ((,stream-var ,stream))
@@ -962,7 +868,6 @@
;;;
;;;   Routine to use in stream-in slot for reading string chars.
;;;
#-(and unicode extfmts)
(def-input-routine input-character
		   (character 1 sap head)
  (code-char (sap-ref-8 sap head)))
@@ -1021,7 +926,7 @@
	      (optimize (speed 3) (space 0) (debug 0) (safety 0)))
    (catch 'eof-input-catcher
      (let* ((head (fd-stream-ibuf-head stream))
	     (ch ,(stream::octets-to-char extfmt
	     (ch (stream::octets-to-char ,extfmt
					 (fd-stream-oc-state stream)
					 (fd-stream-last-char-read-size stream)
					 ;;@@ Note: need proper EOF handling...
@@ -1041,29 +946,6 @@
		(fd-stream-last-char-read-size stream))
	  ch)))))

#+(and unicode extfmts)
(defun input-character (stream eof-error eof-value)
  ;; This needs to go away.  Unreading a character needs to be done by backing
  ;; up the buffer head pointer, so that the external format will re-build the
  ;; character - else changing extfmts won't work.  But until we get around
  ;; to teaching UNREAD-CHAR to DTRT, keep this to maintain compatibility...
  (if (fd-stream-unread stream)
      (prog1 (fd-stream-unread stream)
	(setf (fd-stream-unread stream) nil)
	(setf (fd-stream-listen stream) nil))
      (let ((char (funcall (ef-cin (stream::find-external-format
				    (fd-stream-external-format stream)))
			   stream)))
	(if char
	    char
	    (eof-or-lose stream eof-error eof-value)))))
#+(and unicode extfmts)
(setf *input-routines*
    (nconc *input-routines*
	   ;; check that using 1 here doesn't cause problems if the size
	   ;; of a character is NOT actually 1 octet...
	   (list (list 'character 'input-character 1))))

#+(or)
(stream::def-ef-macro ef-sin (extfmt lisp stream::+ef-max+ stream::+ef-sin+)
  `(lambda (stream string char start end)
@@ -1082,7 +964,7 @@
	 ;;@@ Fix EOF handling
	 (let* ((sz 0)
		(ch (catch 'eof-input-catcher
		      ,(stream::octets-to-char extfmt
		      (stream::octets-to-char ,extfmt
					      (fd-stream-oc-state stream)
					      sz
					      (progn