


(defun normalize-instr-list (ilist)

  (mapcar #'(lambda (z) 
              (mapcar #'(lambda (z) 
                          (if (symbolp z)
                              (intern (symbol-name z))
                            z)) z)) ilist))

(defparameter *standard-subs*

  '( (%+ . pplus)
     (%- . pminus)
     (%* . ptimes)
     
     (%< . plt)
     (%> . pgt)
     (%= . peq)
     (%/= . pne)
     (%<= . ple)
     (%>= . pge)))

(defun subs-assoc (x assl)
  (let ((r (assoc x assl)))
    (if r 
        (cdr r)
      x)))

(defun normalize-instr-symbol (isymbol)
  (substitute #\_ #\- (substitute #\Q #\% (symbol-name (subs-assoc isymbol *standard-subs*)))))


(defun process-instrl-entry (entry)
  (let ((iname (normalize-instr-symbol (car entry)))
        (icode (cadddr entry))
        (classname "EmuProcessor"))
    (multiple-value-bind (ih il) (floor icode 256)
      (let ((resl
            (cond ((and (member ih '(0)) (not (member il '(32 33))))
                   (list (format nil "function_table[~a] = &~a::~a;" il classname iname)))
                  ((and (= il 32) (<= ih 64))
                   (list (format nil "opcode_2_table[~a] = &~a::~a;" ih classname iname)))
                  ((= il 33)
                   (list (format nil "opcode_1_table[~a] = &~a::~a;" ih classname iname)))
                  (t nil))))
        (if resl
            (progn
              (push
               (format nil "void ~a::~a () {~%}" classname iname) resl)

              (push
               (format nil "void ~a::~a ();" classname iname) resl)
              resl))))))

(defun process-instrl (ilist)
  (let ((resl
         (mapcar #'(lambda (z) (process-instrl-entry z)) ilist))
        (basedir  "/home/juergen/projekte/simemu/src/"))
    (with-open-file (fo 
                     (concatenate 'string basedir "funtable.h")
			 :direction :output :if-exists :supersede :if-does-not-exist :create)
      (loop
       for x in resl do
       (if (caddr x)
           (format fo "~a~%" (caddr x)))))

    (with-open-file (fo 
                     (concatenate 'string basedir "funtable.cpp")
			 :direction :output :if-exists :supersede :if-does-not-exist :create)
      (loop
       for x in resl do
       (if (cadr x)
           (format fo "~a~%~%~%" (cadr x)))))

    (with-open-file (fo 
                     (concatenate 'string basedir "fundeclares.h")
			 :direction :output :if-exists :supersede :if-does-not-exist :create)
      (loop
       for x in resl do
       (if (car x)
           (format fo "~a~%" (car x)))))))


(setq xx (normalize-instr-list emu::*instruction-list*))

