;;; -*- Mode:LISP; Base:10; Readtable:CL -*-

(defun convert-opcode-byte-specs-to-opcode-masks ()
  (with-open-file (stream "jb:k.opcodes;opcode-fields.lisp" :direction :input :characters t)
    (do* ((low-mask (1- (expt 2 32.)))
	  (high-mask (ash low-mask 32.))
	  (form (read-for-top-level stream nil)
		(read-for-top-level stream nil))
	  (new-format nil))
	 ((null form)
	  new-format)
      (let* ((name (second form))
	     (bytespec (third form))
	     (mask (bytespec-to-mask (eval bytespec)))
	     )
	(push (list name bytespec mask
		    :high (ash (logand mask high-mask) -32.)
		    :low (logand mask low-mask))
	      new-format)
	))))

(defun bytespec-to-mask (bytespec)
  (let ((size (byte-size bytespec))
	(position (byte-position bytespec))
	(val 0))
    (do ((pp position (1+ pp))
	 (i 0 (1+ i)))
	((>= i size) val)
      (setq val (dpb 1 (byte 1 pp) val)))))

(defun bin (x)
  (format t "~b" x))

;;; called with output of CONVERT-OPCODE-BYTE-SPECS-TO-OPCODE-MASKS
(defun fancy-print-masks (masks)
  (with-open-file (s "ed-buffer:opcode-masks" :direction :output)
    (mapc #'(lambda (mask-desc)
	      (apply #'(lambda (name bytespec mask &optional &key high low)
			 (let ((base 10.))
			   (format s "~&~16,'0x  ~8,'0x  ~8,'0x  ~13a  ~a"
				   mask high low bytespec name)))
		     mask-desc))
	  masks))
  nil)
