29 (MFACTORIAL m_FACTORIAL
1)
36 ;(MQAPPLY m_QAPPLY 1 )
42 ;(MDEFINE m_DEFINE 2 )
47 ;(MNCEXPT m_NCEXPT 1 )
48 ;;(BIGFLOAT m_IGFLOAT 1 )
54 ;(MNCTIMES m_NCTIMES n )
85 (m_set_sp_frame_pointer)
88 (defmacro op-number
(x)
89 (let ((tem (assoc x
*q-ops
*)))
90 (or tem
(error "unrecognized op ~a" x
))
91 (position tem
*q-ops
*)))
93 (defun write-execute ( &aux x
)
94 (with-open-file (st "execute.c" :direction
:output
)
98 #include \"emulate.h\"
100 struct value_function
102 struct value *constants;
105 extern struct value stack[];
108 (sloop for v in
*q-ops
* do
(format st
"~(~a~),~%" (or (second v
) (car v
))))
110 (format st
"execute_fun(f,n)
111 struct value_function *f;
113 { unsigned char *body;
114 /* frame_pointer points just beyond last frames valid storage */
115 struct value *frame_pointer = &stack[s_p -(n -1)];
116 struct value *constants = f->constants;
121 (sloop for v in
*q-ops
* do
(setq x
(or (second v
) (car v
)))
122 (format st
"case ~(~a~): ~(f_~a~)(); break;~%" x
123 (subseq (symbol-name x
) 2)))
124 (format st
" default:
127 END: s_p = frame_pointer -stack -n;
129 s_p = frame_pointer -stack -1;
133 (defvar *max-locals
* 0)
134 (defvar *output
* nil
)
135 (defvar *constants
* (make-array 4 :fill-pointer
0))
136 (defun push-constant (x)
137 (let ((tem (position x
*constants
* :test
'eql
)))
138 (unless tem
(setq tem
(fill-pointer *constants
*))
139 (vector-push-extend x
*constants
*))
141 (q-push (+ tem
(op-number m_pushc_0
)) 'constant
))
143 (q-push (op-number m_pushc_i
) 'constant
)
147 (defun compile-expr (expr arglist recursive locals push-result
&aux tem n
)
148 (let ((*output
* (if recursive
*output
*
150 (make-array 10 :element-type
'fixnum
152 (make-array 10 :element-type
'long-float
154 (cond ( (not recursive
)
155 (setq *max-locals
* 0)
156 (setf (fill-pointer *constants
* ) 0))
158 (setq *max-locals
* (max *max-locals
* (length locals
)))))
162 ((setq tem
(position expr locals
))
163 (setq tem
(- (length locals
) tem
))
166 (+ tem
(op-number m_pushl_0
))))
167 (t (q-push (op-number m_pushl_i
*))
168 (q-push tem
'index
))))
169 ((setq tem
(position expr arglist
))
172 (+ tem
(op-number m_push_0
))))
173 (t (q-push (op-number m_push_i
))
174 (q-push tem
'index
))))
177 (push-constant expr
))
178 (t (error "unrecognized constant: ~s" expr
))))))
179 ((eq (caar expr
) 'mprog
)
180 (let* ((vars (cdr (second expr
)))
181 (offset (length locals
))
183 (cond ((eql offset
0)
184 (q-push (op-number m_set_sp_frame_pointer
)))
186 (q-push (op-number m_set_sp
))
187 (q-push offset
'index
)))
190 (cond ((symbolp v
) (push v new-locals
)
192 ((and (consp v
) (eq (caar v
) 'msetq
)
193 (symbolp (second v
)))
194 (push (second v
) new-locals
)
195 (compile-expr (third v
) arglist t locals t
))))
196 (setq new-locals
(append new-locals locals
))
197 (sloop for v on
(cddr expr
)
199 (compile-expr (car v
) arglist t new-locals
(if (cdr v
) nil t
)))))
200 ((eq (caar expr
) 'mprogn
)
201 (sloop for v on
(cdr expr
)
203 (compile-expr (car v
) arglist t locals
(if (cdr v
) nil t
))))
204 ((setq tem
(assoc (caar expr
) *q-ops
*))
205 (setq n
(position tem
*q-ops
*))
206 (dolist (v (cdr expr
))
207 (compile-expr v arglist t locals t
))
211 ((member (caar expr
) '(mtimes mplus mminus mquotient
))
212 (dolist (v (cdddr expr
))
214 (t (error "unknown nary op ~a ~a args"
215 (caar expr
) (length (cdr expr
)))))))))))
217 (defun q-push (i &optional
(type 'instr
))
218 (format t
"~%(~a) ~a" i
220 (call (second (nth i
*q-ops
*)))
221 (instr (or (second (nth i
*q-ops
*)) (car (nth i
*q-ops
*))))
224 (or (second (nth i
*q-ops
*)) (car (nth i
*q-ops
*)))
225 (if (< i
(op-number m_pushc_i
))
227 (- i
(op-number m_pushc_0
)))))))))