Mercurial > hg > xemacs-beta
annotate src/bytecode.c @ 5117:3742ea8250b5 ben-lisp-object ben-lisp-object-final-ws-year-2005
Checking in final CVS version of workspace 'ben-lisp-object'
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Sat, 26 Dec 2009 00:20:27 -0600 |
parents | 6fa9919a9a0b |
children | e0db3c197671 |
rev | line source |
---|---|
428 | 1 /* Execution of byte code produced by bytecomp.el. |
2 Implementation of compiled-function objects. | |
3 Copyright (C) 1992, 1993 Free Software Foundation, Inc. | |
814 | 4 Copyright (C) 1995, 2002 Ben Wing. |
428 | 5 |
6 This file is part of XEmacs. | |
7 | |
8 XEmacs is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
10 Free Software Foundation; either version 2, or (at your option) any | |
11 later version. | |
12 | |
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with XEmacs; see the file COPYING. If not, write to | |
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 Boston, MA 02111-1307, USA. */ | |
22 | |
23 /* Synched up with: Mule 2.0, FSF 19.30. */ | |
24 | |
25 /* This file has been Mule-ized. */ | |
26 | |
27 | |
28 /* Authorship: | |
29 | |
30 FSF: long ago. | |
31 | |
32 hacked on by jwz@jwz.org 1991-06 | |
33 o added a compile-time switch to turn on simple sanity checking; | |
34 o put back the obsolete byte-codes for error-detection; | |
35 o added a new instruction, unbind_all, which I will use for | |
36 tail-recursion elimination; | |
37 o made temp_output_buffer_show be called with the right number | |
38 of args; | |
39 o made the new bytecodes be called with args in the right order; | |
40 o added metering support. | |
41 | |
42 by Hallvard: | |
43 o added relative jump instructions; | |
44 o all conditionals now only do QUIT if they jump. | |
45 | |
46 Ben Wing: some changes for Mule, 1995-06. | |
47 | |
48 Martin Buchholz: performance hacking, 1998-09. | |
49 See Internals Manual, Evaluation. | |
50 */ | |
51 | |
52 #include <config.h> | |
53 #include "lisp.h" | |
54 #include "backtrace.h" | |
55 #include "buffer.h" | |
56 #include "bytecode.h" | |
57 #include "opaque.h" | |
58 #include "syntax.h" | |
872 | 59 #include "window.h" |
428 | 60 |
61 EXFUN (Ffetch_bytecode, 1); | |
62 | |
63 Lisp_Object Qbyte_code, Qcompiled_functionp, Qinvalid_byte_code; | |
64 | |
65 enum Opcode /* Byte codes */ | |
66 { | |
67 Bvarref = 010, | |
68 Bvarset = 020, | |
69 Bvarbind = 030, | |
70 Bcall = 040, | |
71 Bunbind = 050, | |
72 | |
73 Bnth = 070, | |
74 Bsymbolp = 071, | |
75 Bconsp = 072, | |
76 Bstringp = 073, | |
77 Blistp = 074, | |
78 Bold_eq = 075, | |
79 Bold_memq = 076, | |
80 Bnot = 077, | |
81 Bcar = 0100, | |
82 Bcdr = 0101, | |
83 Bcons = 0102, | |
84 Blist1 = 0103, | |
85 Blist2 = 0104, | |
86 Blist3 = 0105, | |
87 Blist4 = 0106, | |
88 Blength = 0107, | |
89 Baref = 0110, | |
90 Baset = 0111, | |
91 Bsymbol_value = 0112, | |
92 Bsymbol_function = 0113, | |
93 Bset = 0114, | |
94 Bfset = 0115, | |
95 Bget = 0116, | |
96 Bsubstring = 0117, | |
97 Bconcat2 = 0120, | |
98 Bconcat3 = 0121, | |
99 Bconcat4 = 0122, | |
100 Bsub1 = 0123, | |
101 Badd1 = 0124, | |
102 Beqlsign = 0125, | |
103 Bgtr = 0126, | |
104 Blss = 0127, | |
105 Bleq = 0130, | |
106 Bgeq = 0131, | |
107 Bdiff = 0132, | |
108 Bnegate = 0133, | |
109 Bplus = 0134, | |
110 Bmax = 0135, | |
111 Bmin = 0136, | |
112 Bmult = 0137, | |
113 | |
114 Bpoint = 0140, | |
115 Beq = 0141, /* was Bmark, | |
116 but no longer generated as of v18 */ | |
117 Bgoto_char = 0142, | |
118 Binsert = 0143, | |
119 Bpoint_max = 0144, | |
120 Bpoint_min = 0145, | |
121 Bchar_after = 0146, | |
122 Bfollowing_char = 0147, | |
123 Bpreceding_char = 0150, | |
124 Bcurrent_column = 0151, | |
125 Bindent_to = 0152, | |
126 Bequal = 0153, /* was Bscan_buffer, | |
127 but no longer generated as of v18 */ | |
128 Beolp = 0154, | |
129 Beobp = 0155, | |
130 Bbolp = 0156, | |
131 Bbobp = 0157, | |
132 Bcurrent_buffer = 0160, | |
133 Bset_buffer = 0161, | |
134 Bsave_current_buffer = 0162, /* was Bread_char, | |
135 but no longer generated as of v19 */ | |
136 Bmemq = 0163, /* was Bset_mark, | |
137 but no longer generated as of v18 */ | |
138 Binteractive_p = 0164, /* Needed since interactive-p takes | |
139 unevalled args */ | |
140 Bforward_char = 0165, | |
141 Bforward_word = 0166, | |
142 Bskip_chars_forward = 0167, | |
143 Bskip_chars_backward = 0170, | |
144 Bforward_line = 0171, | |
145 Bchar_syntax = 0172, | |
146 Bbuffer_substring = 0173, | |
147 Bdelete_region = 0174, | |
148 Bnarrow_to_region = 0175, | |
149 Bwiden = 0176, | |
150 Bend_of_line = 0177, | |
151 | |
152 Bconstant2 = 0201, | |
153 Bgoto = 0202, | |
154 Bgotoifnil = 0203, | |
155 Bgotoifnonnil = 0204, | |
156 Bgotoifnilelsepop = 0205, | |
157 Bgotoifnonnilelsepop = 0206, | |
158 Breturn = 0207, | |
159 Bdiscard = 0210, | |
160 Bdup = 0211, | |
161 | |
162 Bsave_excursion = 0212, | |
163 Bsave_window_excursion= 0213, | |
164 Bsave_restriction = 0214, | |
165 Bcatch = 0215, | |
166 | |
167 Bunwind_protect = 0216, | |
168 Bcondition_case = 0217, | |
169 Btemp_output_buffer_setup = 0220, | |
170 Btemp_output_buffer_show = 0221, | |
171 | |
172 Bunbind_all = 0222, | |
173 | |
174 Bset_marker = 0223, | |
175 Bmatch_beginning = 0224, | |
176 Bmatch_end = 0225, | |
177 Bupcase = 0226, | |
178 Bdowncase = 0227, | |
179 | |
180 Bstring_equal = 0230, | |
181 Bstring_lessp = 0231, | |
182 Bold_equal = 0232, | |
183 Bnthcdr = 0233, | |
184 Belt = 0234, | |
185 Bold_member = 0235, | |
186 Bold_assq = 0236, | |
187 Bnreverse = 0237, | |
188 Bsetcar = 0240, | |
189 Bsetcdr = 0241, | |
190 Bcar_safe = 0242, | |
191 Bcdr_safe = 0243, | |
192 Bnconc = 0244, | |
193 Bquo = 0245, | |
194 Brem = 0246, | |
195 Bnumberp = 0247, | |
196 Bintegerp = 0250, | |
197 | |
198 BRgoto = 0252, | |
199 BRgotoifnil = 0253, | |
200 BRgotoifnonnil = 0254, | |
201 BRgotoifnilelsepop = 0255, | |
202 BRgotoifnonnilelsepop = 0256, | |
203 | |
204 BlistN = 0257, | |
205 BconcatN = 0260, | |
206 BinsertN = 0261, | |
207 Bmember = 0266, /* new in v20 */ | |
208 Bassq = 0267, /* new in v20 */ | |
209 | |
210 Bconstant = 0300 | |
211 }; | |
212 typedef enum Opcode Opcode; | |
213 | |
214 | |
215 Lisp_Object * execute_rare_opcode (Lisp_Object *stack_ptr, | |
442 | 216 const Opbyte *program_ptr, |
428 | 217 Opcode opcode); |
218 | |
219 /* Define BYTE_CODE_METER to enable generation of a byte-op usage histogram. | |
220 This isn't defined in FSF Emacs and isn't defined in XEmacs v19. */ | |
221 /* #define BYTE_CODE_METER */ | |
222 | |
223 | |
224 #ifdef BYTE_CODE_METER | |
225 | |
226 Lisp_Object Vbyte_code_meter, Qbyte_code_meter; | |
227 int byte_metering_on; | |
228 | |
229 static void | |
230 meter_code (Opcode prev_opcode, Opcode this_opcode) | |
231 { | |
232 if (byte_metering_on) | |
233 { | |
234 Lisp_Object *p = XVECTOR_DATA (XVECTOR_DATA (Vbyte_code_meter)[this_opcode]); | |
235 p[0] = INT_PLUS1 (p[0]); | |
236 if (prev_opcode) | |
237 p[prev_opcode] = INT_PLUS1 (p[prev_opcode]); | |
238 } | |
239 } | |
240 | |
241 #endif /* BYTE_CODE_METER */ | |
242 | |
243 | |
244 static Lisp_Object | |
245 bytecode_negate (Lisp_Object obj) | |
246 { | |
247 retry: | |
248 | |
1983 | 249 if (INTP (obj)) return make_integer (- XINT (obj)); |
428 | 250 if (FLOATP (obj)) return make_float (- XFLOAT_DATA (obj)); |
1983 | 251 if (CHARP (obj)) return make_integer (- ((int) XCHAR (obj))); |
252 if (MARKERP (obj)) return make_integer (- ((int) marker_position (obj))); | |
253 #ifdef HAVE_BIGNUM | |
254 if (BIGNUMP (obj)) BIGNUM_ARITH_RETURN (obj, neg); | |
255 #endif | |
256 #ifdef HAVE_RATIO | |
257 if (RATIOP (obj)) RATIO_ARITH_RETURN (obj, neg); | |
258 #endif | |
259 #ifdef HAVE_BIG_FLOAT | |
260 if (BIGFLOAT_P (obj)) BIGFLOAT_ARITH_RETURN (obj, neg); | |
261 #endif | |
428 | 262 |
263 obj = wrong_type_argument (Qnumber_char_or_marker_p, obj); | |
264 goto retry; | |
265 } | |
266 | |
267 static Lisp_Object | |
268 bytecode_nreverse (Lisp_Object list) | |
269 { | |
270 REGISTER Lisp_Object prev = Qnil; | |
271 REGISTER Lisp_Object tail = list; | |
272 | |
273 while (!NILP (tail)) | |
274 { | |
275 REGISTER Lisp_Object next; | |
276 CHECK_CONS (tail); | |
277 next = XCDR (tail); | |
278 XCDR (tail) = prev; | |
279 prev = tail; | |
280 tail = next; | |
281 } | |
282 return prev; | |
283 } | |
284 | |
285 | |
286 /* We have our own two-argument versions of various arithmetic ops. | |
287 Only two-argument arithmetic operations have their own byte codes. */ | |
288 static int | |
289 bytecode_arithcompare (Lisp_Object obj1, Lisp_Object obj2) | |
290 { | |
1983 | 291 #ifdef WITH_NUMBER_TYPES |
292 switch (promote_args (&obj1, &obj2)) | |
293 { | |
294 case FIXNUM_T: | |
295 { | |
296 EMACS_INT ival1 = XREALINT (obj1), ival2 = XREALINT (obj2); | |
297 return ival1 < ival2 ? -1 : ival1 > ival2 ? 1 : 0; | |
298 } | |
299 #ifdef HAVE_BIGNUM | |
300 case BIGNUM_T: | |
301 return bignum_cmp (XBIGNUM_DATA (obj1), XBIGNUM_DATA (obj2)); | |
302 #endif | |
303 #ifdef HAVE_RATIO | |
304 case RATIO_T: | |
305 return ratio_cmp (XRATIO_DATA (obj1), XRATIO_DATA (obj2)); | |
306 #endif | |
1995 | 307 #ifdef HAVE_BIGFLOAT |
308 case BIGFLOAT_T: | |
309 return bigfloat_cmp (XBIGFLOAT_DATA (obj1), XBIGFLOAT_DATA (obj2)); | |
310 #endif | |
311 default: /* FLOAT_T */ | |
1983 | 312 { |
313 double dval1 = XFLOAT_DATA (obj1), dval2 = XFLOAT_DATA (obj2); | |
314 return dval1 < dval2 ? -1 : dval1 > dval2 ? 1 : 0; | |
315 } | |
316 } | |
317 #else /* !WITH_NUMBER_TYPES */ | |
428 | 318 retry: |
319 | |
320 { | |
321 EMACS_INT ival1, ival2; | |
322 | |
323 if (INTP (obj1)) ival1 = XINT (obj1); | |
324 else if (CHARP (obj1)) ival1 = XCHAR (obj1); | |
325 else if (MARKERP (obj1)) ival1 = marker_position (obj1); | |
326 else goto arithcompare_float; | |
327 | |
328 if (INTP (obj2)) ival2 = XINT (obj2); | |
329 else if (CHARP (obj2)) ival2 = XCHAR (obj2); | |
330 else if (MARKERP (obj2)) ival2 = marker_position (obj2); | |
331 else goto arithcompare_float; | |
332 | |
333 return ival1 < ival2 ? -1 : ival1 > ival2 ? 1 : 0; | |
334 } | |
335 | |
336 arithcompare_float: | |
337 | |
338 { | |
339 double dval1, dval2; | |
340 | |
341 if (FLOATP (obj1)) dval1 = XFLOAT_DATA (obj1); | |
342 else if (INTP (obj1)) dval1 = (double) XINT (obj1); | |
343 else if (CHARP (obj1)) dval1 = (double) XCHAR (obj1); | |
344 else if (MARKERP (obj1)) dval1 = (double) marker_position (obj1); | |
345 else | |
346 { | |
347 obj1 = wrong_type_argument (Qnumber_char_or_marker_p, obj1); | |
348 goto retry; | |
349 } | |
350 | |
351 if (FLOATP (obj2)) dval2 = XFLOAT_DATA (obj2); | |
352 else if (INTP (obj2)) dval2 = (double) XINT (obj2); | |
353 else if (CHARP (obj2)) dval2 = (double) XCHAR (obj2); | |
354 else if (MARKERP (obj2)) dval2 = (double) marker_position (obj2); | |
355 else | |
356 { | |
357 obj2 = wrong_type_argument (Qnumber_char_or_marker_p, obj2); | |
358 goto retry; | |
359 } | |
360 | |
361 return dval1 < dval2 ? -1 : dval1 > dval2 ? 1 : 0; | |
362 } | |
1983 | 363 #endif /* WITH_NUMBER_TYPES */ |
428 | 364 } |
365 | |
366 static Lisp_Object | |
367 bytecode_arithop (Lisp_Object obj1, Lisp_Object obj2, Opcode opcode) | |
368 { | |
1983 | 369 #ifdef WITH_NUMBER_TYPES |
370 switch (promote_args (&obj1, &obj2)) | |
371 { | |
372 case FIXNUM_T: | |
373 { | |
374 EMACS_INT ival1 = XREALINT (obj1), ival2 = XREALINT (obj2); | |
375 switch (opcode) | |
376 { | |
377 case Bplus: ival1 += ival2; break; | |
378 case Bdiff: ival1 -= ival2; break; | |
379 case Bmult: | |
380 #ifdef HAVE_BIGNUM | |
381 /* Due to potential overflow, we compute using bignums */ | |
382 bignum_set_long (scratch_bignum, ival1); | |
383 bignum_set_long (scratch_bignum2, ival2); | |
384 bignum_mul (scratch_bignum, scratch_bignum, scratch_bignum2); | |
385 return Fcanonicalize_number (make_bignum_bg (scratch_bignum)); | |
386 #else | |
387 ival1 *= ival2; break; | |
388 #endif | |
389 case Bquo: | |
390 if (ival2 == 0) Fsignal (Qarith_error, Qnil); | |
391 ival1 /= ival2; | |
392 break; | |
393 case Bmax: if (ival1 < ival2) ival1 = ival2; break; | |
394 case Bmin: if (ival1 > ival2) ival1 = ival2; break; | |
395 } | |
396 return make_integer (ival1); | |
397 } | |
398 #ifdef HAVE_BIGNUM | |
399 case BIGNUM_T: | |
400 switch (opcode) | |
401 { | |
402 case Bplus: | |
403 bignum_add (scratch_bignum, XBIGNUM_DATA (obj1), | |
404 XBIGNUM_DATA (obj2)); | |
405 break; | |
406 case Bdiff: | |
407 bignum_sub (scratch_bignum, XBIGNUM_DATA (obj1), | |
408 XBIGNUM_DATA (obj2)); | |
409 break; | |
410 case Bmult: | |
411 bignum_mul (scratch_bignum, XBIGNUM_DATA (obj1), | |
412 XBIGNUM_DATA (obj2)); | |
413 break; | |
414 case Bquo: | |
415 if (bignum_sign (XBIGNUM_DATA (obj2)) == 0) | |
416 Fsignal (Qarith_error, Qnil); | |
417 bignum_div (scratch_bignum, XBIGNUM_DATA (obj1), | |
418 XBIGNUM_DATA (obj2)); | |
419 break; | |
420 case Bmax: | |
421 return bignum_gt (XBIGNUM_DATA (obj1), XBIGNUM_DATA (obj2)) | |
422 ? obj1 : obj2; | |
423 case Bmin: | |
424 return bignum_lt (XBIGNUM_DATA (obj1), XBIGNUM_DATA (obj2)) | |
425 ? obj1 : obj2; | |
426 } | |
427 return Fcanonicalize_number (make_bignum_bg (scratch_bignum)); | |
428 #endif | |
429 #ifdef HAVE_RATIO | |
430 case RATIO_T: | |
431 switch (opcode) | |
432 { | |
433 case Bplus: | |
434 ratio_add (scratch_ratio, XRATIO_DATA (obj1), XRATIO_DATA (obj2)); | |
435 break; | |
436 case Bdiff: | |
437 ratio_sub (scratch_ratio, XRATIO_DATA (obj1), XRATIO_DATA (obj2)); | |
438 break; | |
439 case Bmult: | |
440 ratio_mul (scratch_ratio, XRATIO_DATA (obj1), XRATIO_DATA (obj2)); | |
441 break; | |
442 case Bquo: | |
443 if (ratio_sign (XRATIO_DATA (obj2)) == 0) | |
444 Fsignal (Qarith_error, Qnil); | |
445 ratio_div (scratch_ratio, XRATIO_DATA (obj1), XRATIO_DATA (obj2)); | |
446 break; | |
447 case Bmax: | |
448 return ratio_gt (XRATIO_DATA (obj1), XRATIO_DATA (obj2)) | |
449 ? obj1 : obj2; | |
450 case Bmin: | |
451 return ratio_lt (XRATIO_DATA (obj1), XRATIO_DATA (obj2)) | |
452 ? obj1 : obj2; | |
453 } | |
454 return make_ratio_rt (scratch_ratio); | |
455 #endif | |
456 #ifdef HAVE_BIGFLOAT | |
457 case BIGFLOAT_T: | |
458 bigfloat_set_prec (scratch_bigfloat, max (XBIGFLOAT_GET_PREC (obj1), | |
459 XBIGFLOAT_GET_PREC (obj2))); | |
460 switch (opcode) | |
461 { | |
462 case Bplus: | |
463 bigfloat_add (scratch_bigfloat, XBIGFLOAT_DATA (obj1), | |
464 XBIGFLOAT_DATA (obj2)); | |
465 break; | |
466 case Bdiff: | |
467 bigfloat_sub (scratch_bigfloat, XBIGFLOAT_DATA (obj1), | |
468 XBIGFLOAT_DATA (obj2)); | |
469 break; | |
470 case Bmult: | |
471 bigfloat_mul (scratch_bigfloat, XBIGFLOAT_DATA (obj1), | |
472 XBIGFLOAT_DATA (obj2)); | |
473 break; | |
474 case Bquo: | |
475 if (bigfloat_sign (XBIGFLOAT_DATA (obj2)) == 0) | |
476 Fsignal (Qarith_error, Qnil); | |
477 bigfloat_div (scratch_bigfloat, XBIGFLOAT_DATA (obj1), | |
478 XBIGFLOAT_DATA (obj2)); | |
479 break; | |
480 case Bmax: | |
481 return bigfloat_gt (XBIGFLOAT_DATA (obj1), XBIGFLOAT_DATA (obj2)) | |
482 ? obj1 : obj2; | |
483 case Bmin: | |
484 return bigfloat_lt (XBIGFLOAT_DATA (obj1), XBIGFLOAT_DATA (obj2)) | |
485 ? obj1 : obj2; | |
486 } | |
487 return make_bigfloat_bf (scratch_bigfloat); | |
488 #endif | |
1995 | 489 default: /* FLOAT_T */ |
490 { | |
491 double dval1 = XFLOAT_DATA (obj1), dval2 = XFLOAT_DATA (obj2); | |
492 switch (opcode) | |
493 { | |
494 case Bplus: dval1 += dval2; break; | |
495 case Bdiff: dval1 -= dval2; break; | |
496 case Bmult: dval1 *= dval2; break; | |
497 case Bquo: | |
498 if (dval2 == 0.0) Fsignal (Qarith_error, Qnil); | |
499 dval1 /= dval2; | |
500 break; | |
501 case Bmax: if (dval1 < dval2) dval1 = dval2; break; | |
502 case Bmin: if (dval1 > dval2) dval1 = dval2; break; | |
503 } | |
504 return make_float (dval1); | |
505 } | |
1983 | 506 } |
507 #else /* !WITH_NUMBER_TYPES */ | |
428 | 508 EMACS_INT ival1, ival2; |
509 int float_p; | |
510 | |
511 retry: | |
512 | |
513 float_p = 0; | |
514 | |
515 if (INTP (obj1)) ival1 = XINT (obj1); | |
516 else if (CHARP (obj1)) ival1 = XCHAR (obj1); | |
517 else if (MARKERP (obj1)) ival1 = marker_position (obj1); | |
518 else if (FLOATP (obj1)) ival1 = 0, float_p = 1; | |
519 else | |
520 { | |
521 obj1 = wrong_type_argument (Qnumber_char_or_marker_p, obj1); | |
522 goto retry; | |
523 } | |
524 | |
525 if (INTP (obj2)) ival2 = XINT (obj2); | |
526 else if (CHARP (obj2)) ival2 = XCHAR (obj2); | |
527 else if (MARKERP (obj2)) ival2 = marker_position (obj2); | |
528 else if (FLOATP (obj2)) ival2 = 0, float_p = 1; | |
529 else | |
530 { | |
531 obj2 = wrong_type_argument (Qnumber_char_or_marker_p, obj2); | |
532 goto retry; | |
533 } | |
534 | |
535 if (!float_p) | |
536 { | |
537 switch (opcode) | |
538 { | |
539 case Bplus: ival1 += ival2; break; | |
540 case Bdiff: ival1 -= ival2; break; | |
541 case Bmult: ival1 *= ival2; break; | |
542 case Bquo: | |
543 if (ival2 == 0) Fsignal (Qarith_error, Qnil); | |
544 ival1 /= ival2; | |
545 break; | |
546 case Bmax: if (ival1 < ival2) ival1 = ival2; break; | |
547 case Bmin: if (ival1 > ival2) ival1 = ival2; break; | |
548 } | |
549 return make_int (ival1); | |
550 } | |
551 else | |
552 { | |
553 double dval1 = FLOATP (obj1) ? XFLOAT_DATA (obj1) : (double) ival1; | |
554 double dval2 = FLOATP (obj2) ? XFLOAT_DATA (obj2) : (double) ival2; | |
555 switch (opcode) | |
556 { | |
557 case Bplus: dval1 += dval2; break; | |
558 case Bdiff: dval1 -= dval2; break; | |
559 case Bmult: dval1 *= dval2; break; | |
560 case Bquo: | |
561 if (dval2 == 0) Fsignal (Qarith_error, Qnil); | |
562 dval1 /= dval2; | |
563 break; | |
564 case Bmax: if (dval1 < dval2) dval1 = dval2; break; | |
565 case Bmin: if (dval1 > dval2) dval1 = dval2; break; | |
566 } | |
567 return make_float (dval1); | |
568 } | |
1983 | 569 #endif /* WITH_NUMBER_TYPES */ |
428 | 570 } |
571 | |
572 | |
573 /* Read next uint8 from the instruction stream. */ | |
574 #define READ_UINT_1 ((unsigned int) (unsigned char) *program_ptr++) | |
575 | |
576 /* Read next uint16 from the instruction stream. */ | |
577 #define READ_UINT_2 \ | |
578 (program_ptr += 2, \ | |
579 (((unsigned int) (unsigned char) program_ptr[-1]) * 256 + \ | |
580 ((unsigned int) (unsigned char) program_ptr[-2]))) | |
581 | |
582 /* Read next int8 from the instruction stream. */ | |
583 #define READ_INT_1 ((int) (signed char) *program_ptr++) | |
584 | |
585 /* Read next int16 from the instruction stream. */ | |
586 #define READ_INT_2 \ | |
587 (program_ptr += 2, \ | |
588 (((int) ( signed char) program_ptr[-1]) * 256 + \ | |
589 ((int) (unsigned char) program_ptr[-2]))) | |
590 | |
591 /* Read next int8 from instruction stream; don't advance program_pointer */ | |
592 #define PEEK_INT_1 ((int) (signed char) program_ptr[0]) | |
593 | |
594 /* Read next int16 from instruction stream; don't advance program_pointer */ | |
595 #define PEEK_INT_2 \ | |
596 ((((int) ( signed char) program_ptr[1]) * 256) | \ | |
597 ((int) (unsigned char) program_ptr[0])) | |
598 | |
599 /* Do relative jumps from the current location. | |
600 We only do a QUIT if we jump backwards, for efficiency. | |
601 No infloops without backward jumps! */ | |
602 #define JUMP_RELATIVE(jump) do { \ | |
603 int JR_jump = (jump); \ | |
604 if (JR_jump < 0) QUIT; \ | |
605 program_ptr += JR_jump; \ | |
606 } while (0) | |
607 | |
608 #define JUMP JUMP_RELATIVE (PEEK_INT_2) | |
609 #define JUMPR JUMP_RELATIVE (PEEK_INT_1) | |
610 | |
611 #define JUMP_NEXT ((void) (program_ptr += 2)) | |
612 #define JUMPR_NEXT ((void) (program_ptr += 1)) | |
613 | |
614 /* Push x onto the execution stack. */ | |
615 #define PUSH(x) (*++stack_ptr = (x)) | |
616 | |
617 /* Pop a value off the execution stack. */ | |
618 #define POP (*stack_ptr--) | |
619 | |
620 /* Discard n values from the execution stack. */ | |
621 #define DISCARD(n) (stack_ptr -= (n)) | |
622 | |
623 /* Get the value which is at the top of the execution stack, | |
624 but don't pop it. */ | |
625 #define TOP (*stack_ptr) | |
626 | |
1920 | 627 /* See comment before the big switch in execute_optimized_program(). */ |
1884 | 628 #define GCPRO_STACK (gcpro1.nvars = stack_ptr - stack_beg) |
629 | |
428 | 630 /* The actual interpreter for byte code. |
631 This function has been seriously optimized for performance. | |
632 Don't change the constructs unless you are willing to do | |
633 real benchmarking and profiling work -- martin */ | |
634 | |
635 | |
814 | 636 Lisp_Object |
442 | 637 execute_optimized_program (const Opbyte *program, |
428 | 638 int stack_depth, |
639 Lisp_Object *constants_data) | |
640 { | |
641 /* This function can GC */ | |
442 | 642 REGISTER const Opbyte *program_ptr = (Opbyte *) program; |
1884 | 643 Lisp_Object *stack_beg = alloca_array (Lisp_Object, stack_depth + 1); |
644 REGISTER Lisp_Object *stack_ptr = stack_beg; | |
428 | 645 int speccount = specpdl_depth (); |
646 struct gcpro gcpro1; | |
647 | |
648 #ifdef BYTE_CODE_METER | |
649 Opcode this_opcode = 0; | |
650 Opcode prev_opcode; | |
651 #endif | |
652 | |
653 #ifdef ERROR_CHECK_BYTE_CODE | |
654 Lisp_Object *stack_end = stack_beg + stack_depth; | |
655 #endif | |
656 | |
1920 | 657 /* We used to GCPRO the whole interpreter stack before entering this while |
658 loop (21.5.14 and before), but that interferes with collection of weakly | |
659 referenced objects. Although strictly speaking there's no promise that | |
660 weak references will disappear by any given point in time, they should | |
661 be collected at the first opportunity. Waiting until exit from the | |
662 function caused test failures because "stale" objects "above" the top of | |
663 the stack were still GCPROed, and they were not getting collected until | |
664 after exit from the (byte-compiled) test! | |
665 | |
666 Now the idea is to dynamically adjust the array of GCPROed objects to | |
667 include only the "active" region of the stack. | |
668 | |
669 We use the "GCPRO1 the array base and set the nvars member" method. It | |
670 would be slightly inefficient but correct to use GCPRO1_ARRAY here. It | |
671 would just redundantly set nvars. | |
672 #### Maybe it would be clearer to use GCPRO1_ARRAY and do GCPRO_STACK | |
673 after the switch? | |
674 | |
675 GCPRO_STACK is something of a misnomer, because it suggests that a | |
676 struct gcpro is initialized each time. This is false; only the nvars | |
677 member of a single struct gcpro is being adjusted. This works because | |
678 each time a new object is assigned to a stack location, the old object | |
679 loses its reference and is effectively UNGCPROed, and the new object is | |
680 automatically GCPROed as long as nvars is correct. Only when we | |
681 return from the interpreter do we need to finalize the struct gcpro | |
682 itself, and that's done at case Breturn. | |
683 */ | |
428 | 684 GCPRO1 (stack_ptr[1]); |
1758 | 685 |
428 | 686 while (1) |
687 { | |
688 REGISTER Opcode opcode = (Opcode) READ_UINT_1; | |
1920 | 689 |
690 GCPRO_STACK; /* Get nvars right before maybe signaling. */ | |
428 | 691 #ifdef ERROR_CHECK_BYTE_CODE |
692 if (stack_ptr > stack_end) | |
563 | 693 stack_overflow ("byte code stack overflow", Qunbound); |
428 | 694 if (stack_ptr < stack_beg) |
563 | 695 stack_overflow ("byte code stack underflow", Qunbound); |
428 | 696 #endif |
697 | |
698 #ifdef BYTE_CODE_METER | |
699 prev_opcode = this_opcode; | |
700 this_opcode = opcode; | |
701 meter_code (prev_opcode, this_opcode); | |
702 #endif | |
703 | |
704 switch (opcode) | |
705 { | |
706 REGISTER int n; | |
707 | |
708 default: | |
709 if (opcode >= Bconstant) | |
710 PUSH (constants_data[opcode - Bconstant]); | |
711 else | |
1884 | 712 { |
713 /* We're not sure what these do, so better safe than sorry. */ | |
714 /* GCPRO_STACK; */ | |
715 stack_ptr = execute_rare_opcode (stack_ptr, program_ptr, opcode); | |
716 } | |
428 | 717 break; |
718 | |
719 case Bvarref: | |
720 case Bvarref+1: | |
721 case Bvarref+2: | |
722 case Bvarref+3: | |
723 case Bvarref+4: | |
724 case Bvarref+5: n = opcode - Bvarref; goto do_varref; | |
725 case Bvarref+7: n = READ_UINT_2; goto do_varref; | |
726 case Bvarref+6: n = READ_UINT_1; /* most common */ | |
727 do_varref: | |
728 { | |
729 Lisp_Object symbol = constants_data[n]; | |
730 Lisp_Object value = XSYMBOL (symbol)->value; | |
731 if (SYMBOL_VALUE_MAGIC_P (value)) | |
1920 | 732 /* I GCPRO_STACKed Fsymbol_value elsewhere, but I dunno why. */ |
733 /* GCPRO_STACK; */ | |
428 | 734 value = Fsymbol_value (symbol); |
735 PUSH (value); | |
736 break; | |
737 } | |
738 | |
739 case Bvarset: | |
740 case Bvarset+1: | |
741 case Bvarset+2: | |
742 case Bvarset+3: | |
743 case Bvarset+4: | |
744 case Bvarset+5: n = opcode - Bvarset; goto do_varset; | |
745 case Bvarset+7: n = READ_UINT_2; goto do_varset; | |
746 case Bvarset+6: n = READ_UINT_1; /* most common */ | |
747 do_varset: | |
748 { | |
749 Lisp_Object symbol = constants_data[n]; | |
440 | 750 Lisp_Symbol *symbol_ptr = XSYMBOL (symbol); |
428 | 751 Lisp_Object old_value = symbol_ptr->value; |
752 Lisp_Object new_value = POP; | |
1661 | 753 if (!SYMBOL_VALUE_MAGIC_P (old_value) || UNBOUNDP (old_value)) |
428 | 754 symbol_ptr->value = new_value; |
1884 | 755 else { |
756 /* Fset may call magic handlers */ | |
757 /* GCPRO_STACK; */ | |
428 | 758 Fset (symbol, new_value); |
1884 | 759 } |
760 | |
428 | 761 break; |
762 } | |
763 | |
764 case Bvarbind: | |
765 case Bvarbind+1: | |
766 case Bvarbind+2: | |
767 case Bvarbind+3: | |
768 case Bvarbind+4: | |
769 case Bvarbind+5: n = opcode - Bvarbind; goto do_varbind; | |
770 case Bvarbind+7: n = READ_UINT_2; goto do_varbind; | |
771 case Bvarbind+6: n = READ_UINT_1; /* most common */ | |
772 do_varbind: | |
773 { | |
774 Lisp_Object symbol = constants_data[n]; | |
440 | 775 Lisp_Symbol *symbol_ptr = XSYMBOL (symbol); |
428 | 776 Lisp_Object old_value = symbol_ptr->value; |
777 Lisp_Object new_value = POP; | |
778 if (!SYMBOL_VALUE_MAGIC_P (old_value) || UNBOUNDP (old_value)) | |
779 { | |
780 specpdl_ptr->symbol = symbol; | |
781 specpdl_ptr->old_value = old_value; | |
782 specpdl_ptr->func = 0; | |
783 specpdl_ptr++; | |
784 specpdl_depth_counter++; | |
785 | |
786 symbol_ptr->value = new_value; | |
853 | 787 |
788 #ifdef ERROR_CHECK_CATCH | |
789 check_specbind_stack_sanity (); | |
790 #endif | |
428 | 791 } |
792 else | |
1884 | 793 { |
794 /* does an Fset, may call magic handlers */ | |
795 /* GCPRO_STACK; */ | |
796 specbind_magic (symbol, new_value); | |
797 } | |
428 | 798 break; |
799 } | |
800 | |
801 case Bcall: | |
802 case Bcall+1: | |
803 case Bcall+2: | |
804 case Bcall+3: | |
805 case Bcall+4: | |
806 case Bcall+5: | |
807 case Bcall+6: | |
808 case Bcall+7: | |
809 n = (opcode < Bcall+6 ? opcode - Bcall : | |
810 opcode == Bcall+6 ? READ_UINT_1 : READ_UINT_2); | |
1920 | 811 /* #### Shouldn't this be just before the Ffuncall? |
812 Neither Fget nor Fput can GC. */ | |
1884 | 813 /* GCPRO_STACK; */ |
428 | 814 DISCARD (n); |
815 #ifdef BYTE_CODE_METER | |
816 if (byte_metering_on && SYMBOLP (TOP)) | |
817 { | |
818 Lisp_Object val = Fget (TOP, Qbyte_code_meter, Qnil); | |
819 if (INTP (val)) | |
820 Fput (TOP, Qbyte_code_meter, make_int (XINT (val) + 1)); | |
821 } | |
822 #endif | |
823 TOP = Ffuncall (n + 1, &TOP); | |
824 break; | |
825 | |
826 case Bunbind: | |
827 case Bunbind+1: | |
828 case Bunbind+2: | |
829 case Bunbind+3: | |
830 case Bunbind+4: | |
831 case Bunbind+5: | |
832 case Bunbind+6: | |
833 case Bunbind+7: | |
834 UNBIND_TO (specpdl_depth() - | |
835 (opcode < Bunbind+6 ? opcode-Bunbind : | |
836 opcode == Bunbind+6 ? READ_UINT_1 : READ_UINT_2)); | |
837 break; | |
838 | |
839 | |
840 case Bgoto: | |
841 JUMP; | |
842 break; | |
843 | |
844 case Bgotoifnil: | |
845 if (NILP (POP)) | |
846 JUMP; | |
847 else | |
848 JUMP_NEXT; | |
849 break; | |
850 | |
851 case Bgotoifnonnil: | |
852 if (!NILP (POP)) | |
853 JUMP; | |
854 else | |
855 JUMP_NEXT; | |
856 break; | |
857 | |
858 case Bgotoifnilelsepop: | |
859 if (NILP (TOP)) | |
860 JUMP; | |
861 else | |
862 { | |
863 DISCARD (1); | |
864 JUMP_NEXT; | |
865 } | |
866 break; | |
867 | |
868 case Bgotoifnonnilelsepop: | |
869 if (!NILP (TOP)) | |
870 JUMP; | |
871 else | |
872 { | |
873 DISCARD (1); | |
874 JUMP_NEXT; | |
875 } | |
876 break; | |
877 | |
878 | |
879 case BRgoto: | |
880 JUMPR; | |
881 break; | |
882 | |
883 case BRgotoifnil: | |
884 if (NILP (POP)) | |
885 JUMPR; | |
886 else | |
887 JUMPR_NEXT; | |
888 break; | |
889 | |
890 case BRgotoifnonnil: | |
891 if (!NILP (POP)) | |
892 JUMPR; | |
893 else | |
894 JUMPR_NEXT; | |
895 break; | |
896 | |
897 case BRgotoifnilelsepop: | |
898 if (NILP (TOP)) | |
899 JUMPR; | |
900 else | |
901 { | |
902 DISCARD (1); | |
903 JUMPR_NEXT; | |
904 } | |
905 break; | |
906 | |
907 case BRgotoifnonnilelsepop: | |
908 if (!NILP (TOP)) | |
909 JUMPR; | |
910 else | |
911 { | |
912 DISCARD (1); | |
913 JUMPR_NEXT; | |
914 } | |
915 break; | |
916 | |
917 case Breturn: | |
918 UNGCPRO; | |
919 #ifdef ERROR_CHECK_BYTE_CODE | |
920 /* Binds and unbinds are supposed to be compiled balanced. */ | |
921 if (specpdl_depth() != speccount) | |
563 | 922 invalid_byte_code ("unbalanced specbinding stack", Qunbound); |
428 | 923 #endif |
924 return TOP; | |
925 | |
926 case Bdiscard: | |
927 DISCARD (1); | |
928 break; | |
929 | |
930 case Bdup: | |
931 { | |
932 Lisp_Object arg = TOP; | |
933 PUSH (arg); | |
934 break; | |
935 } | |
936 | |
937 case Bconstant2: | |
938 PUSH (constants_data[READ_UINT_2]); | |
939 break; | |
940 | |
941 case Bcar: | |
1920 | 942 /* Fcar can GC via wrong_type_argument. */ |
943 /* GCPRO_STACK; */ | |
428 | 944 TOP = CONSP (TOP) ? XCAR (TOP) : Fcar (TOP); |
945 break; | |
946 | |
947 case Bcdr: | |
1920 | 948 /* Fcdr can GC via wrong_type_argument. */ |
949 /* GCPRO_STACK; */ | |
428 | 950 TOP = CONSP (TOP) ? XCDR (TOP) : Fcdr (TOP); |
951 break; | |
952 | |
953 | |
954 case Bunbind_all: | |
955 /* To unbind back to the beginning of this frame. Not used yet, | |
956 but will be needed for tail-recursion elimination. */ | |
771 | 957 unbind_to (speccount); |
428 | 958 break; |
959 | |
960 case Bnth: | |
961 { | |
962 Lisp_Object arg = POP; | |
1920 | 963 /* Fcar and Fnthcdr can GC via wrong_type_argument. */ |
964 /* GCPRO_STACK; */ | |
428 | 965 TOP = Fcar (Fnthcdr (TOP, arg)); |
966 break; | |
967 } | |
968 | |
969 case Bsymbolp: | |
970 TOP = SYMBOLP (TOP) ? Qt : Qnil; | |
971 break; | |
972 | |
973 case Bconsp: | |
974 TOP = CONSP (TOP) ? Qt : Qnil; | |
975 break; | |
976 | |
977 case Bstringp: | |
978 TOP = STRINGP (TOP) ? Qt : Qnil; | |
979 break; | |
980 | |
981 case Blistp: | |
982 TOP = LISTP (TOP) ? Qt : Qnil; | |
983 break; | |
984 | |
985 case Bnumberp: | |
1983 | 986 #ifdef WITH_NUMBER_TYPES |
987 TOP = NUMBERP (TOP) ? Qt : Qnil; | |
988 #else | |
428 | 989 TOP = INT_OR_FLOATP (TOP) ? Qt : Qnil; |
1983 | 990 #endif |
428 | 991 break; |
992 | |
993 case Bintegerp: | |
1983 | 994 #ifdef HAVE_BIGNUM |
995 TOP = INTEGERP (TOP) ? Qt : Qnil; | |
996 #else | |
428 | 997 TOP = INTP (TOP) ? Qt : Qnil; |
1983 | 998 #endif |
428 | 999 break; |
1000 | |
1001 case Beq: | |
1002 { | |
1003 Lisp_Object arg = POP; | |
1004 TOP = EQ_WITH_EBOLA_NOTICE (TOP, arg) ? Qt : Qnil; | |
1005 break; | |
1006 } | |
1007 | |
1008 case Bnot: | |
1009 TOP = NILP (TOP) ? Qt : Qnil; | |
1010 break; | |
1011 | |
1012 case Bcons: | |
1013 { | |
1014 Lisp_Object arg = POP; | |
1015 TOP = Fcons (TOP, arg); | |
1016 break; | |
1017 } | |
1018 | |
1019 case Blist1: | |
1020 TOP = Fcons (TOP, Qnil); | |
1021 break; | |
1022 | |
1023 | |
1024 case BlistN: | |
1025 n = READ_UINT_1; | |
1026 goto do_list; | |
1027 | |
1028 case Blist2: | |
1029 case Blist3: | |
1030 case Blist4: | |
1031 /* common case */ | |
1032 n = opcode - (Blist1 - 1); | |
1033 do_list: | |
1034 { | |
1035 Lisp_Object list = Qnil; | |
1036 list_loop: | |
1037 list = Fcons (TOP, list); | |
1038 if (--n) | |
1039 { | |
1040 DISCARD (1); | |
1041 goto list_loop; | |
1042 } | |
1043 TOP = list; | |
1044 break; | |
1045 } | |
1046 | |
1047 | |
1048 case Bconcat2: | |
1049 case Bconcat3: | |
1050 case Bconcat4: | |
1051 n = opcode - (Bconcat2 - 2); | |
1052 goto do_concat; | |
1053 | |
1054 case BconcatN: | |
1055 /* common case */ | |
1056 n = READ_UINT_1; | |
1057 do_concat: | |
1058 DISCARD (n - 1); | |
1920 | 1059 /* Apparently `concat' can GC; Fconcat GCPROs its arguments. */ |
1060 /* GCPRO_STACK; */ | |
428 | 1061 TOP = Fconcat (n, &TOP); |
1062 break; | |
1063 | |
1064 | |
1065 case Blength: | |
1066 TOP = Flength (TOP); | |
1067 break; | |
1068 | |
1069 case Baset: | |
1070 { | |
1071 Lisp_Object arg2 = POP; | |
1072 Lisp_Object arg1 = POP; | |
1073 TOP = Faset (TOP, arg1, arg2); | |
1074 break; | |
1075 } | |
1076 | |
1077 case Bsymbol_value: | |
1920 | 1078 /* Why does this need GCPRO_STACK? If not, remove others, too. */ |
1884 | 1079 /* GCPRO_STACK; */ |
428 | 1080 TOP = Fsymbol_value (TOP); |
1081 break; | |
1082 | |
1083 case Bsymbol_function: | |
1084 TOP = Fsymbol_function (TOP); | |
1085 break; | |
1086 | |
1087 case Bget: | |
1088 { | |
1089 Lisp_Object arg = POP; | |
1090 TOP = Fget (TOP, arg, Qnil); | |
1091 break; | |
1092 } | |
1093 | |
1094 case Bsub1: | |
1983 | 1095 #ifdef HAVE_BIGNUM |
1096 TOP = Fsub1 (TOP); | |
1097 #else | |
428 | 1098 TOP = INTP (TOP) ? INT_MINUS1 (TOP) : Fsub1 (TOP); |
1983 | 1099 #endif |
428 | 1100 break; |
1101 | |
1102 case Badd1: | |
1983 | 1103 #ifdef HAVE_BIGNUM |
1104 TOP = Fadd1 (TOP); | |
1105 #else | |
428 | 1106 TOP = INTP (TOP) ? INT_PLUS1 (TOP) : Fadd1 (TOP); |
1983 | 1107 #endif |
428 | 1108 break; |
1109 | |
1110 | |
1111 case Beqlsign: | |
1112 { | |
1113 Lisp_Object arg = POP; | |
1114 TOP = bytecode_arithcompare (TOP, arg) == 0 ? Qt : Qnil; | |
1115 break; | |
1116 } | |
1117 | |
1118 case Bgtr: | |
1119 { | |
1120 Lisp_Object arg = POP; | |
1121 TOP = bytecode_arithcompare (TOP, arg) > 0 ? Qt : Qnil; | |
1122 break; | |
1123 } | |
1124 | |
1125 case Blss: | |
1126 { | |
1127 Lisp_Object arg = POP; | |
1128 TOP = bytecode_arithcompare (TOP, arg) < 0 ? Qt : Qnil; | |
1129 break; | |
1130 } | |
1131 | |
1132 case Bleq: | |
1133 { | |
1134 Lisp_Object arg = POP; | |
1135 TOP = bytecode_arithcompare (TOP, arg) <= 0 ? Qt : Qnil; | |
1136 break; | |
1137 } | |
1138 | |
1139 case Bgeq: | |
1140 { | |
1141 Lisp_Object arg = POP; | |
1142 TOP = bytecode_arithcompare (TOP, arg) >= 0 ? Qt : Qnil; | |
1143 break; | |
1144 } | |
1145 | |
1146 | |
1147 case Bnegate: | |
1148 TOP = bytecode_negate (TOP); | |
1149 break; | |
1150 | |
1151 case Bnconc: | |
1152 DISCARD (1); | |
1920 | 1153 /* nconc2 GCPROs before calling this. */ |
1154 /* GCPRO_STACK; */ | |
428 | 1155 TOP = bytecode_nconc2 (&TOP); |
1156 break; | |
1157 | |
1158 case Bplus: | |
1159 { | |
1160 Lisp_Object arg2 = POP; | |
1161 Lisp_Object arg1 = TOP; | |
1983 | 1162 #ifdef HAVE_BIGNUM |
1163 TOP = bytecode_arithop (arg1, arg2, opcode); | |
1164 #else | |
428 | 1165 TOP = INTP (arg1) && INTP (arg2) ? |
1166 INT_PLUS (arg1, arg2) : | |
1167 bytecode_arithop (arg1, arg2, opcode); | |
1983 | 1168 #endif |
428 | 1169 break; |
1170 } | |
1171 | |
1172 case Bdiff: | |
1173 { | |
1174 Lisp_Object arg2 = POP; | |
1175 Lisp_Object arg1 = TOP; | |
1983 | 1176 #ifdef HAVE_BIGNUM |
1177 TOP = bytecode_arithop (arg1, arg2, opcode); | |
1178 #else | |
428 | 1179 TOP = INTP (arg1) && INTP (arg2) ? |
1180 INT_MINUS (arg1, arg2) : | |
1181 bytecode_arithop (arg1, arg2, opcode); | |
1983 | 1182 #endif |
428 | 1183 break; |
1184 } | |
1185 | |
1186 case Bmult: | |
1187 case Bquo: | |
1188 case Bmax: | |
1189 case Bmin: | |
1190 { | |
1191 Lisp_Object arg = POP; | |
1192 TOP = bytecode_arithop (TOP, arg, opcode); | |
1193 break; | |
1194 } | |
1195 | |
1196 case Bpoint: | |
1197 PUSH (make_int (BUF_PT (current_buffer))); | |
1198 break; | |
1199 | |
1200 case Binsert: | |
1920 | 1201 /* Says it can GC. */ |
1202 /* GCPRO_STACK; */ | |
428 | 1203 TOP = Finsert (1, &TOP); |
1204 break; | |
1205 | |
1206 case BinsertN: | |
1207 n = READ_UINT_1; | |
1208 DISCARD (n - 1); | |
1920 | 1209 /* See Binsert. */ |
1210 /* GCPRO_STACK; */ | |
428 | 1211 TOP = Finsert (n, &TOP); |
1212 break; | |
1213 | |
1214 case Baref: | |
1215 { | |
1216 Lisp_Object arg = POP; | |
1217 TOP = Faref (TOP, arg); | |
1218 break; | |
1219 } | |
1220 | |
1221 case Bmemq: | |
1222 { | |
1223 Lisp_Object arg = POP; | |
1224 TOP = Fmemq (TOP, arg); | |
1225 break; | |
1226 } | |
1227 | |
1228 case Bset: | |
1229 { | |
1230 Lisp_Object arg = POP; | |
1884 | 1231 /* Fset may call magic handlers */ |
1232 /* GCPRO_STACK; */ | |
428 | 1233 TOP = Fset (TOP, arg); |
1234 break; | |
1235 } | |
1236 | |
1237 case Bequal: | |
1238 { | |
1239 Lisp_Object arg = POP; | |
1920 | 1240 /* Can QUIT, so can GC, right? */ |
1241 /* GCPRO_STACK; */ | |
428 | 1242 TOP = Fequal (TOP, arg); |
1243 break; | |
1244 } | |
1245 | |
1246 case Bnthcdr: | |
1247 { | |
1248 Lisp_Object arg = POP; | |
1249 TOP = Fnthcdr (TOP, arg); | |
1250 break; | |
1251 } | |
1252 | |
1253 case Belt: | |
1254 { | |
1255 Lisp_Object arg = POP; | |
1256 TOP = Felt (TOP, arg); | |
1257 break; | |
1258 } | |
1259 | |
1260 case Bmember: | |
1261 { | |
1262 Lisp_Object arg = POP; | |
1920 | 1263 /* Can QUIT, so can GC, right? */ |
1264 /* GCPRO_STACK; */ | |
428 | 1265 TOP = Fmember (TOP, arg); |
1266 break; | |
1267 } | |
1268 | |
1269 case Bgoto_char: | |
1270 TOP = Fgoto_char (TOP, Qnil); | |
1271 break; | |
1272 | |
1273 case Bcurrent_buffer: | |
1274 { | |
793 | 1275 Lisp_Object buffer = wrap_buffer (current_buffer); |
1276 | |
428 | 1277 PUSH (buffer); |
1278 break; | |
1279 } | |
1280 | |
1281 case Bset_buffer: | |
1884 | 1282 /* #### WAG: set-buffer may cause Fset's of buffer locals |
1283 Didn't prevent crash. :-( */ | |
1284 /* GCPRO_STACK; */ | |
428 | 1285 TOP = Fset_buffer (TOP); |
1286 break; | |
1287 | |
1288 case Bpoint_max: | |
1289 PUSH (make_int (BUF_ZV (current_buffer))); | |
1290 break; | |
1291 | |
1292 case Bpoint_min: | |
1293 PUSH (make_int (BUF_BEGV (current_buffer))); | |
1294 break; | |
1295 | |
1296 case Bskip_chars_forward: | |
1297 { | |
1298 Lisp_Object arg = POP; | |
1920 | 1299 /* Can QUIT, so can GC, right? */ |
1300 /* GCPRO_STACK; */ | |
428 | 1301 TOP = Fskip_chars_forward (TOP, arg, Qnil); |
1302 break; | |
1303 } | |
1304 | |
1305 case Bassq: | |
1306 { | |
1307 Lisp_Object arg = POP; | |
1308 TOP = Fassq (TOP, arg); | |
1309 break; | |
1310 } | |
1311 | |
1312 case Bsetcar: | |
1313 { | |
1314 Lisp_Object arg = POP; | |
1315 TOP = Fsetcar (TOP, arg); | |
1316 break; | |
1317 } | |
1318 | |
1319 case Bsetcdr: | |
1320 { | |
1321 Lisp_Object arg = POP; | |
1322 TOP = Fsetcdr (TOP, arg); | |
1323 break; | |
1324 } | |
1325 | |
1326 case Bnreverse: | |
1327 TOP = bytecode_nreverse (TOP); | |
1328 break; | |
1329 | |
1330 case Bcar_safe: | |
1331 TOP = CONSP (TOP) ? XCAR (TOP) : Qnil; | |
1332 break; | |
1333 | |
1334 case Bcdr_safe: | |
1335 TOP = CONSP (TOP) ? XCDR (TOP) : Qnil; | |
1336 break; | |
1337 | |
1338 } | |
1339 } | |
1340 } | |
1341 | |
1342 /* It makes a worthwhile performance difference (5%) to shunt | |
1343 lesser-used opcodes off to a subroutine, to keep the switch in | |
1344 execute_optimized_program small. If you REALLY care about | |
1345 performance, you want to keep your heavily executed code away from | |
1346 rarely executed code, to minimize cache misses. | |
1347 | |
1348 Don't make this function static, since then the compiler might inline it. */ | |
1349 Lisp_Object * | |
1350 execute_rare_opcode (Lisp_Object *stack_ptr, | |
2286 | 1351 const Opbyte *UNUSED (program_ptr), |
428 | 1352 Opcode opcode) |
1353 { | |
1354 switch (opcode) | |
1355 { | |
1356 | |
1357 case Bsave_excursion: | |
1358 record_unwind_protect (save_excursion_restore, | |
1359 save_excursion_save ()); | |
1360 break; | |
1361 | |
1362 case Bsave_window_excursion: | |
1363 { | |
1364 int count = specpdl_depth (); | |
1365 record_unwind_protect (save_window_excursion_unwind, | |
1149 | 1366 call1 (Qcurrent_window_configuration, Qnil)); |
428 | 1367 TOP = Fprogn (TOP); |
771 | 1368 unbind_to (count); |
428 | 1369 break; |
1370 } | |
1371 | |
1372 case Bsave_restriction: | |
1373 record_unwind_protect (save_restriction_restore, | |
844 | 1374 save_restriction_save (current_buffer)); |
428 | 1375 break; |
1376 | |
1377 case Bcatch: | |
1378 { | |
1379 Lisp_Object arg = POP; | |
2532 | 1380 TOP = internal_catch (TOP, Feval, arg, 0, 0, 0); |
428 | 1381 break; |
1382 } | |
1383 | |
1384 case Bskip_chars_backward: | |
1385 { | |
1386 Lisp_Object arg = POP; | |
1387 TOP = Fskip_chars_backward (TOP, arg, Qnil); | |
1388 break; | |
1389 } | |
1390 | |
1391 case Bunwind_protect: | |
1392 record_unwind_protect (Fprogn, POP); | |
1393 break; | |
1394 | |
1395 case Bcondition_case: | |
1396 { | |
1397 Lisp_Object arg2 = POP; /* handlers */ | |
1398 Lisp_Object arg1 = POP; /* bodyform */ | |
1399 TOP = condition_case_3 (arg1, TOP, arg2); | |
1400 break; | |
1401 } | |
1402 | |
1403 case Bset_marker: | |
1404 { | |
1405 Lisp_Object arg2 = POP; | |
1406 Lisp_Object arg1 = POP; | |
1407 TOP = Fset_marker (TOP, arg1, arg2); | |
1408 break; | |
1409 } | |
1410 | |
1411 case Brem: | |
1412 { | |
1413 Lisp_Object arg = POP; | |
1414 TOP = Frem (TOP, arg); | |
1415 break; | |
1416 } | |
1417 | |
1418 case Bmatch_beginning: | |
1419 TOP = Fmatch_beginning (TOP); | |
1420 break; | |
1421 | |
1422 case Bmatch_end: | |
1423 TOP = Fmatch_end (TOP); | |
1424 break; | |
1425 | |
1426 case Bupcase: | |
1427 TOP = Fupcase (TOP, Qnil); | |
1428 break; | |
1429 | |
1430 case Bdowncase: | |
1431 TOP = Fdowncase (TOP, Qnil); | |
1432 break; | |
1433 | |
1434 case Bfset: | |
1435 { | |
1436 Lisp_Object arg = POP; | |
1437 TOP = Ffset (TOP, arg); | |
1438 break; | |
1439 } | |
1440 | |
1441 case Bstring_equal: | |
1442 { | |
1443 Lisp_Object arg = POP; | |
1444 TOP = Fstring_equal (TOP, arg); | |
1445 break; | |
1446 } | |
1447 | |
1448 case Bstring_lessp: | |
1449 { | |
1450 Lisp_Object arg = POP; | |
1451 TOP = Fstring_lessp (TOP, arg); | |
1452 break; | |
1453 } | |
1454 | |
1455 case Bsubstring: | |
1456 { | |
1457 Lisp_Object arg2 = POP; | |
1458 Lisp_Object arg1 = POP; | |
1459 TOP = Fsubstring (TOP, arg1, arg2); | |
1460 break; | |
1461 } | |
1462 | |
1463 case Bcurrent_column: | |
1464 PUSH (make_int (current_column (current_buffer))); | |
1465 break; | |
1466 | |
1467 case Bchar_after: | |
1468 TOP = Fchar_after (TOP, Qnil); | |
1469 break; | |
1470 | |
1471 case Bindent_to: | |
1472 TOP = Findent_to (TOP, Qnil, Qnil); | |
1473 break; | |
1474 | |
1475 case Bwiden: | |
1476 PUSH (Fwiden (Qnil)); | |
1477 break; | |
1478 | |
1479 case Bfollowing_char: | |
1480 PUSH (Ffollowing_char (Qnil)); | |
1481 break; | |
1482 | |
1483 case Bpreceding_char: | |
1484 PUSH (Fpreceding_char (Qnil)); | |
1485 break; | |
1486 | |
1487 case Beolp: | |
1488 PUSH (Feolp (Qnil)); | |
1489 break; | |
1490 | |
1491 case Beobp: | |
1492 PUSH (Feobp (Qnil)); | |
1493 break; | |
1494 | |
1495 case Bbolp: | |
1496 PUSH (Fbolp (Qnil)); | |
1497 break; | |
1498 | |
1499 case Bbobp: | |
1500 PUSH (Fbobp (Qnil)); | |
1501 break; | |
1502 | |
1503 case Bsave_current_buffer: | |
1504 record_unwind_protect (save_current_buffer_restore, | |
1505 Fcurrent_buffer ()); | |
1506 break; | |
1507 | |
1508 case Binteractive_p: | |
1509 PUSH (Finteractive_p ()); | |
1510 break; | |
1511 | |
1512 case Bforward_char: | |
1513 TOP = Fforward_char (TOP, Qnil); | |
1514 break; | |
1515 | |
1516 case Bforward_word: | |
1517 TOP = Fforward_word (TOP, Qnil); | |
1518 break; | |
1519 | |
1520 case Bforward_line: | |
1521 TOP = Fforward_line (TOP, Qnil); | |
1522 break; | |
1523 | |
1524 case Bchar_syntax: | |
1525 TOP = Fchar_syntax (TOP, Qnil); | |
1526 break; | |
1527 | |
1528 case Bbuffer_substring: | |
1529 { | |
1530 Lisp_Object arg = POP; | |
1531 TOP = Fbuffer_substring (TOP, arg, Qnil); | |
1532 break; | |
1533 } | |
1534 | |
1535 case Bdelete_region: | |
1536 { | |
1537 Lisp_Object arg = POP; | |
1538 TOP = Fdelete_region (TOP, arg, Qnil); | |
1539 break; | |
1540 } | |
1541 | |
1542 case Bnarrow_to_region: | |
1543 { | |
1544 Lisp_Object arg = POP; | |
1545 TOP = Fnarrow_to_region (TOP, arg, Qnil); | |
1546 break; | |
1547 } | |
1548 | |
1549 case Bend_of_line: | |
1550 TOP = Fend_of_line (TOP, Qnil); | |
1551 break; | |
1552 | |
1553 case Btemp_output_buffer_setup: | |
1554 temp_output_buffer_setup (TOP); | |
1555 TOP = Vstandard_output; | |
1556 break; | |
1557 | |
1558 case Btemp_output_buffer_show: | |
1559 { | |
1560 Lisp_Object arg = POP; | |
1561 temp_output_buffer_show (TOP, Qnil); | |
1562 TOP = arg; | |
1563 /* GAG ME!! */ | |
1564 /* pop binding of standard-output */ | |
771 | 1565 unbind_to (specpdl_depth() - 1); |
428 | 1566 break; |
1567 } | |
1568 | |
1569 case Bold_eq: | |
1570 { | |
1571 Lisp_Object arg = POP; | |
1572 TOP = HACKEQ_UNSAFE (TOP, arg) ? Qt : Qnil; | |
1573 break; | |
1574 } | |
1575 | |
1576 case Bold_memq: | |
1577 { | |
1578 Lisp_Object arg = POP; | |
1579 TOP = Fold_memq (TOP, arg); | |
1580 break; | |
1581 } | |
1582 | |
1583 case Bold_equal: | |
1584 { | |
1585 Lisp_Object arg = POP; | |
1586 TOP = Fold_equal (TOP, arg); | |
1587 break; | |
1588 } | |
1589 | |
1590 case Bold_member: | |
1591 { | |
1592 Lisp_Object arg = POP; | |
1593 TOP = Fold_member (TOP, arg); | |
1594 break; | |
1595 } | |
1596 | |
1597 case Bold_assq: | |
1598 { | |
1599 Lisp_Object arg = POP; | |
1600 TOP = Fold_assq (TOP, arg); | |
1601 break; | |
1602 } | |
1603 | |
1604 default: | |
2500 | 1605 ABORT(); |
428 | 1606 break; |
1607 } | |
1608 return stack_ptr; | |
1609 } | |
1610 | |
1611 | |
563 | 1612 DOESNT_RETURN |
867 | 1613 invalid_byte_code (const CIbyte *reason, Lisp_Object frob) |
428 | 1614 { |
563 | 1615 signal_error (Qinvalid_byte_code, reason, frob); |
428 | 1616 } |
1617 | |
1618 /* Check for valid opcodes. Change this when adding new opcodes. */ | |
1619 static void | |
1620 check_opcode (Opcode opcode) | |
1621 { | |
1622 if ((opcode < Bvarref) || | |
1623 (opcode == 0251) || | |
1624 (opcode > Bassq && opcode < Bconstant)) | |
563 | 1625 invalid_byte_code ("invalid opcode in instruction stream", |
1626 make_int (opcode)); | |
428 | 1627 } |
1628 | |
1629 /* Check that IDX is a valid offset into the `constants' vector */ | |
1630 static void | |
1631 check_constants_index (int idx, Lisp_Object constants) | |
1632 { | |
1633 if (idx < 0 || idx >= XVECTOR_LENGTH (constants)) | |
563 | 1634 signal_ferror |
1635 (Qinvalid_byte_code, | |
1636 "reference %d to constants array out of range 0, %ld", | |
428 | 1637 idx, XVECTOR_LENGTH (constants) - 1); |
1638 } | |
1639 | |
1640 /* Get next character from Lisp instructions string. */ | |
563 | 1641 #define READ_INSTRUCTION_CHAR(lvalue) do { \ |
867 | 1642 (lvalue) = itext_ichar (ptr); \ |
1643 INC_IBYTEPTR (ptr); \ | |
563 | 1644 *icounts_ptr++ = program_ptr - program; \ |
1645 if (lvalue > UCHAR_MAX) \ | |
1646 invalid_byte_code \ | |
1647 ("Invalid character in byte code string", make_char (lvalue)); \ | |
428 | 1648 } while (0) |
1649 | |
1650 /* Get opcode from Lisp instructions string. */ | |
1651 #define READ_OPCODE do { \ | |
1652 unsigned int c; \ | |
1653 READ_INSTRUCTION_CHAR (c); \ | |
1654 opcode = (Opcode) c; \ | |
1655 } while (0) | |
1656 | |
1657 /* Get next operand, a uint8, from Lisp instructions string. */ | |
1658 #define READ_OPERAND_1 do { \ | |
1659 READ_INSTRUCTION_CHAR (arg); \ | |
1660 argsize = 1; \ | |
1661 } while (0) | |
1662 | |
1663 /* Get next operand, a uint16, from Lisp instructions string. */ | |
1664 #define READ_OPERAND_2 do { \ | |
1665 unsigned int arg1, arg2; \ | |
1666 READ_INSTRUCTION_CHAR (arg1); \ | |
1667 READ_INSTRUCTION_CHAR (arg2); \ | |
1668 arg = arg1 + (arg2 << 8); \ | |
1669 argsize = 2; \ | |
1670 } while (0) | |
1671 | |
1672 /* Write 1 byte to PTR, incrementing PTR */ | |
1673 #define WRITE_INT8(value, ptr) do { \ | |
1674 *((ptr)++) = (value); \ | |
1675 } while (0) | |
1676 | |
1677 /* Write 2 bytes to PTR, incrementing PTR */ | |
1678 #define WRITE_INT16(value, ptr) do { \ | |
1679 WRITE_INT8 (((unsigned) (value)) & 0x00ff, (ptr)); \ | |
1680 WRITE_INT8 (((unsigned) (value)) >> 8 , (ptr)); \ | |
1681 } while (0) | |
1682 | |
1683 /* We've changed our minds about the opcode we've already written. */ | |
1684 #define REWRITE_OPCODE(new_opcode) ((void) (program_ptr[-1] = new_opcode)) | |
1685 | |
1686 /* Encode an op arg within the opcode, or as a 1 or 2-byte operand. */ | |
1687 #define WRITE_NARGS(base_opcode) do { \ | |
1688 if (arg <= 5) \ | |
1689 { \ | |
1690 REWRITE_OPCODE (base_opcode + arg); \ | |
1691 } \ | |
1692 else if (arg <= UCHAR_MAX) \ | |
1693 { \ | |
1694 REWRITE_OPCODE (base_opcode + 6); \ | |
1695 WRITE_INT8 (arg, program_ptr); \ | |
1696 } \ | |
1697 else \ | |
1698 { \ | |
1699 REWRITE_OPCODE (base_opcode + 7); \ | |
1700 WRITE_INT16 (arg, program_ptr); \ | |
1701 } \ | |
1702 } while (0) | |
1703 | |
1704 /* Encode a constants reference within the opcode, or as a 2-byte operand. */ | |
1705 #define WRITE_CONSTANT do { \ | |
1706 check_constants_index(arg, constants); \ | |
1707 if (arg <= UCHAR_MAX - Bconstant) \ | |
1708 { \ | |
1709 REWRITE_OPCODE (Bconstant + arg); \ | |
1710 } \ | |
1711 else \ | |
1712 { \ | |
1713 REWRITE_OPCODE (Bconstant2); \ | |
1714 WRITE_INT16 (arg, program_ptr); \ | |
1715 } \ | |
1716 } while (0) | |
1717 | |
1718 #define WRITE_OPCODE WRITE_INT8 (opcode, program_ptr) | |
1719 | |
1720 /* Compile byte code instructions into free space provided by caller, with | |
1721 size >= (2 * string_char_length (instructions) + 1) * sizeof (Opbyte). | |
1722 Returns length of compiled code. */ | |
1723 static void | |
1724 optimize_byte_code (/* in */ | |
1725 Lisp_Object instructions, | |
1726 Lisp_Object constants, | |
1727 /* out */ | |
442 | 1728 Opbyte * const program, |
1729 int * const program_length, | |
1730 int * const varbind_count) | |
428 | 1731 { |
647 | 1732 Bytecount instructions_length = XSTRING_LENGTH (instructions); |
665 | 1733 Elemcount comfy_size = (Elemcount) (2 * instructions_length); |
428 | 1734 |
442 | 1735 int * const icounts = alloca_array (int, comfy_size); |
428 | 1736 int * icounts_ptr = icounts; |
1737 | |
1738 /* We maintain a table of jumps in the source code. */ | |
1739 struct jump | |
1740 { | |
1741 int from; | |
1742 int to; | |
1743 }; | |
442 | 1744 struct jump * const jumps = alloca_array (struct jump, comfy_size); |
428 | 1745 struct jump *jumps_ptr = jumps; |
1746 | |
1747 Opbyte *program_ptr = program; | |
1748 | |
867 | 1749 const Ibyte *ptr = XSTRING_DATA (instructions); |
1750 const Ibyte * const end = ptr + instructions_length; | |
428 | 1751 |
1752 *varbind_count = 0; | |
1753 | |
1754 while (ptr < end) | |
1755 { | |
1756 Opcode opcode; | |
1757 int arg; | |
1758 int argsize = 0; | |
1759 READ_OPCODE; | |
1760 WRITE_OPCODE; | |
1761 | |
1762 switch (opcode) | |
1763 { | |
1764 Lisp_Object val; | |
1765 | |
1766 case Bvarref+7: READ_OPERAND_2; goto do_varref; | |
1767 case Bvarref+6: READ_OPERAND_1; goto do_varref; | |
1768 case Bvarref: case Bvarref+1: case Bvarref+2: | |
1769 case Bvarref+3: case Bvarref+4: case Bvarref+5: | |
1770 arg = opcode - Bvarref; | |
1771 do_varref: | |
1772 check_constants_index (arg, constants); | |
1773 val = XVECTOR_DATA (constants) [arg]; | |
1774 if (!SYMBOLP (val)) | |
563 | 1775 invalid_byte_code ("variable reference to non-symbol", val); |
428 | 1776 if (EQ (val, Qnil) || EQ (val, Qt) || (SYMBOL_IS_KEYWORD (val))) |
563 | 1777 invalid_byte_code ("variable reference to constant symbol", val); |
428 | 1778 WRITE_NARGS (Bvarref); |
1779 break; | |
1780 | |
1781 case Bvarset+7: READ_OPERAND_2; goto do_varset; | |
1782 case Bvarset+6: READ_OPERAND_1; goto do_varset; | |
1783 case Bvarset: case Bvarset+1: case Bvarset+2: | |
1784 case Bvarset+3: case Bvarset+4: case Bvarset+5: | |
1785 arg = opcode - Bvarset; | |
1786 do_varset: | |
1787 check_constants_index (arg, constants); | |
1788 val = XVECTOR_DATA (constants) [arg]; | |
1789 if (!SYMBOLP (val)) | |
563 | 1790 wtaerror ("attempt to set non-symbol", val); |
428 | 1791 if (EQ (val, Qnil) || EQ (val, Qt)) |
563 | 1792 signal_error (Qsetting_constant, 0, val); |
428 | 1793 /* Ignore assignments to keywords by converting to Bdiscard. |
1794 For backward compatibility only - we'd like to make this an error. */ | |
1795 if (SYMBOL_IS_KEYWORD (val)) | |
1796 REWRITE_OPCODE (Bdiscard); | |
1797 else | |
1798 WRITE_NARGS (Bvarset); | |
1799 break; | |
1800 | |
1801 case Bvarbind+7: READ_OPERAND_2; goto do_varbind; | |
1802 case Bvarbind+6: READ_OPERAND_1; goto do_varbind; | |
1803 case Bvarbind: case Bvarbind+1: case Bvarbind+2: | |
1804 case Bvarbind+3: case Bvarbind+4: case Bvarbind+5: | |
1805 arg = opcode - Bvarbind; | |
1806 do_varbind: | |
1807 (*varbind_count)++; | |
1808 check_constants_index (arg, constants); | |
1809 val = XVECTOR_DATA (constants) [arg]; | |
1810 if (!SYMBOLP (val)) | |
563 | 1811 wtaerror ("attempt to let-bind non-symbol", val); |
428 | 1812 if (EQ (val, Qnil) || EQ (val, Qt) || (SYMBOL_IS_KEYWORD (val))) |
563 | 1813 signal_error (Qsetting_constant, |
1814 "attempt to let-bind constant symbol", val); | |
428 | 1815 WRITE_NARGS (Bvarbind); |
1816 break; | |
1817 | |
1818 case Bcall+7: READ_OPERAND_2; goto do_call; | |
1819 case Bcall+6: READ_OPERAND_1; goto do_call; | |
1820 case Bcall: case Bcall+1: case Bcall+2: | |
1821 case Bcall+3: case Bcall+4: case Bcall+5: | |
1822 arg = opcode - Bcall; | |
1823 do_call: | |
1824 WRITE_NARGS (Bcall); | |
1825 break; | |
1826 | |
1827 case Bunbind+7: READ_OPERAND_2; goto do_unbind; | |
1828 case Bunbind+6: READ_OPERAND_1; goto do_unbind; | |
1829 case Bunbind: case Bunbind+1: case Bunbind+2: | |
1830 case Bunbind+3: case Bunbind+4: case Bunbind+5: | |
1831 arg = opcode - Bunbind; | |
1832 do_unbind: | |
1833 WRITE_NARGS (Bunbind); | |
1834 break; | |
1835 | |
1836 case Bgoto: | |
1837 case Bgotoifnil: | |
1838 case Bgotoifnonnil: | |
1839 case Bgotoifnilelsepop: | |
1840 case Bgotoifnonnilelsepop: | |
1841 READ_OPERAND_2; | |
1842 /* Make program_ptr-relative */ | |
1843 arg += icounts - (icounts_ptr - argsize); | |
1844 goto do_jump; | |
1845 | |
1846 case BRgoto: | |
1847 case BRgotoifnil: | |
1848 case BRgotoifnonnil: | |
1849 case BRgotoifnilelsepop: | |
1850 case BRgotoifnonnilelsepop: | |
1851 READ_OPERAND_1; | |
1852 /* Make program_ptr-relative */ | |
1853 arg -= 127; | |
1854 do_jump: | |
1855 /* Record program-relative goto addresses in `jumps' table */ | |
1856 jumps_ptr->from = icounts_ptr - icounts - argsize; | |
1857 jumps_ptr->to = jumps_ptr->from + arg; | |
1858 jumps_ptr++; | |
1859 if (arg >= -1 && arg <= argsize) | |
563 | 1860 invalid_byte_code ("goto instruction is its own target", Qunbound); |
428 | 1861 if (arg <= SCHAR_MIN || |
1862 arg > SCHAR_MAX) | |
1863 { | |
1864 if (argsize == 1) | |
1865 REWRITE_OPCODE (opcode + Bgoto - BRgoto); | |
1866 WRITE_INT16 (arg, program_ptr); | |
1867 } | |
1868 else | |
1869 { | |
1870 if (argsize == 2) | |
1871 REWRITE_OPCODE (opcode + BRgoto - Bgoto); | |
1872 WRITE_INT8 (arg, program_ptr); | |
1873 } | |
1874 break; | |
1875 | |
1876 case Bconstant2: | |
1877 READ_OPERAND_2; | |
1878 WRITE_CONSTANT; | |
1879 break; | |
1880 | |
1881 case BlistN: | |
1882 case BconcatN: | |
1883 case BinsertN: | |
1884 READ_OPERAND_1; | |
1885 WRITE_INT8 (arg, program_ptr); | |
1886 break; | |
1887 | |
1888 default: | |
1889 if (opcode < Bconstant) | |
1890 check_opcode (opcode); | |
1891 else | |
1892 { | |
1893 arg = opcode - Bconstant; | |
1894 WRITE_CONSTANT; | |
1895 } | |
1896 break; | |
1897 } | |
1898 } | |
1899 | |
1900 /* Fix up jumps table to refer to NEW offsets. */ | |
1901 { | |
1902 struct jump *j; | |
1903 for (j = jumps; j < jumps_ptr; j++) | |
1904 { | |
1905 #ifdef ERROR_CHECK_BYTE_CODE | |
1906 assert (j->from < icounts_ptr - icounts); | |
1907 assert (j->to < icounts_ptr - icounts); | |
1908 #endif | |
1909 j->from = icounts[j->from]; | |
1910 j->to = icounts[j->to]; | |
1911 #ifdef ERROR_CHECK_BYTE_CODE | |
1912 assert (j->from < program_ptr - program); | |
1913 assert (j->to < program_ptr - program); | |
1914 check_opcode ((Opcode) (program[j->from-1])); | |
1915 #endif | |
1916 check_opcode ((Opcode) (program[j->to])); | |
1917 } | |
1918 } | |
1919 | |
1920 /* Fixup jumps in byte-code until no more fixups needed */ | |
1921 { | |
1922 int more_fixups_needed = 1; | |
1923 | |
1924 while (more_fixups_needed) | |
1925 { | |
1926 struct jump *j; | |
1927 more_fixups_needed = 0; | |
1928 for (j = jumps; j < jumps_ptr; j++) | |
1929 { | |
1930 int from = j->from; | |
1931 int to = j->to; | |
1932 int jump = to - from; | |
1933 Opbyte *p = program + from; | |
1934 Opcode opcode = (Opcode) p[-1]; | |
1935 if (!more_fixups_needed) | |
1936 check_opcode ((Opcode) p[jump]); | |
1937 assert (to >= 0 && program + to < program_ptr); | |
1938 switch (opcode) | |
1939 { | |
1940 case Bgoto: | |
1941 case Bgotoifnil: | |
1942 case Bgotoifnonnil: | |
1943 case Bgotoifnilelsepop: | |
1944 case Bgotoifnonnilelsepop: | |
1945 WRITE_INT16 (jump, p); | |
1946 break; | |
1947 | |
1948 case BRgoto: | |
1949 case BRgotoifnil: | |
1950 case BRgotoifnonnil: | |
1951 case BRgotoifnilelsepop: | |
1952 case BRgotoifnonnilelsepop: | |
1953 if (jump > SCHAR_MIN && | |
1954 jump <= SCHAR_MAX) | |
1955 { | |
1956 WRITE_INT8 (jump, p); | |
1957 } | |
1958 else /* barf */ | |
1959 { | |
1960 struct jump *jj; | |
1961 for (jj = jumps; jj < jumps_ptr; jj++) | |
1962 { | |
1963 assert (jj->from < program_ptr - program); | |
1964 assert (jj->to < program_ptr - program); | |
1965 if (jj->from > from) jj->from++; | |
1966 if (jj->to > from) jj->to++; | |
1967 } | |
1968 p[-1] += Bgoto - BRgoto; | |
1969 more_fixups_needed = 1; | |
1970 memmove (p+1, p, program_ptr++ - p); | |
1971 WRITE_INT16 (jump, p); | |
1972 } | |
1973 break; | |
1974 | |
1975 default: | |
2500 | 1976 ABORT(); |
428 | 1977 break; |
1978 } | |
1979 } | |
1980 } | |
1981 } | |
1982 | |
1983 /* *program_ptr++ = 0; */ | |
1984 *program_length = program_ptr - program; | |
1985 } | |
1986 | |
1987 /* Optimize the byte code and store the optimized program, only | |
1988 understood by bytecode.c, in an opaque object in the | |
1989 instructions slot of the Compiled_Function object. */ | |
1990 void | |
1991 optimize_compiled_function (Lisp_Object compiled_function) | |
1992 { | |
1993 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (compiled_function); | |
1994 int program_length; | |
1995 int varbind_count; | |
1996 Opbyte *program; | |
1997 | |
1737 | 1998 { |
1999 int minargs = 0, maxargs = 0, totalargs = 0; | |
2000 int optional_p = 0, rest_p = 0, i = 0; | |
2001 { | |
2002 LIST_LOOP_2 (arg, f->arglist) | |
2003 { | |
2004 if (EQ (arg, Qand_optional)) | |
2005 optional_p = 1; | |
2006 else if (EQ (arg, Qand_rest)) | |
2007 rest_p = 1; | |
2008 else | |
2009 { | |
2010 if (rest_p) | |
2011 { | |
2012 maxargs = MANY; | |
2013 totalargs++; | |
2014 break; | |
2015 } | |
2016 if (!optional_p) | |
2017 minargs++; | |
2018 maxargs++; | |
2019 totalargs++; | |
2020 } | |
2021 } | |
2022 } | |
2023 | |
2024 if (totalargs) | |
2025 f->args = xnew_array (Lisp_Object, totalargs); | |
2026 | |
2027 { | |
2028 LIST_LOOP_2 (arg, f->arglist) | |
2029 { | |
2030 if (!EQ (arg, Qand_optional) && !EQ (arg, Qand_rest)) | |
2031 f->args[i++] = arg; | |
2032 } | |
2033 } | |
2034 | |
2035 f->max_args = maxargs; | |
2036 f->min_args = minargs; | |
2037 f->args_in_array = totalargs; | |
2038 } | |
2039 | |
428 | 2040 /* If we have not actually read the bytecode string |
2041 and constants vector yet, fetch them from the file. */ | |
2042 if (CONSP (f->instructions)) | |
2043 Ffetch_bytecode (compiled_function); | |
2044 | |
2045 if (STRINGP (f->instructions)) | |
2046 { | |
826 | 2047 /* XSTRING_LENGTH() is more efficient than string_char_length(), |
428 | 2048 which would be slightly more `proper' */ |
2049 program = alloca_array (Opbyte, 1 + 2 * XSTRING_LENGTH (f->instructions)); | |
2050 optimize_byte_code (f->instructions, f->constants, | |
2051 program, &program_length, &varbind_count); | |
2500 | 2052 f->specpdl_depth = (unsigned short) (XINT (Flength (f->arglist)) + |
2053 varbind_count); | |
428 | 2054 f->instructions = |
440 | 2055 make_opaque (program, program_length * sizeof (Opbyte)); |
428 | 2056 } |
2057 | |
2058 assert (OPAQUEP (f->instructions)); | |
2059 } | |
2060 | |
2061 /************************************************************************/ | |
2062 /* The compiled-function object type */ | |
2063 /************************************************************************/ | |
2064 static void | |
2065 print_compiled_function (Lisp_Object obj, Lisp_Object printcharfun, | |
2066 int escapeflag) | |
2067 { | |
2068 /* This function can GC */ | |
2069 Lisp_Compiled_Function *f = | |
2070 XCOMPILED_FUNCTION (obj); /* GC doesn't relocate */ | |
2071 int docp = f->flags.documentationp; | |
2072 int intp = f->flags.interactivep; | |
2073 struct gcpro gcpro1, gcpro2; | |
2074 GCPRO2 (obj, printcharfun); | |
2075 | |
826 | 2076 write_c_string (printcharfun, print_readably ? "#[" : "#<compiled-function "); |
428 | 2077 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK |
2078 if (!print_readably) | |
2079 { | |
2080 Lisp_Object ann = compiled_function_annotation (f); | |
2081 if (!NILP (ann)) | |
800 | 2082 write_fmt_string_lisp (printcharfun, "(from %S) ", 1, ann); |
428 | 2083 } |
2084 #endif /* COMPILED_FUNCTION_ANNOTATION_HACK */ | |
2085 /* COMPILED_ARGLIST = 0 */ | |
2086 print_internal (compiled_function_arglist (f), printcharfun, escapeflag); | |
2087 | |
2088 /* COMPILED_INSTRUCTIONS = 1 */ | |
826 | 2089 write_c_string (printcharfun, " "); |
428 | 2090 { |
2091 struct gcpro ngcpro1; | |
2092 Lisp_Object instructions = compiled_function_instructions (f); | |
2093 NGCPRO1 (instructions); | |
2094 if (STRINGP (instructions) && !print_readably) | |
2095 { | |
2096 /* We don't usually want to see that junk in the bytecode. */ | |
800 | 2097 write_fmt_string (printcharfun, "\"...(%ld)\"", |
826 | 2098 (long) string_char_length (instructions)); |
428 | 2099 } |
2100 else | |
2101 print_internal (instructions, printcharfun, escapeflag); | |
2102 NUNGCPRO; | |
2103 } | |
2104 | |
2105 /* COMPILED_CONSTANTS = 2 */ | |
826 | 2106 write_c_string (printcharfun, " "); |
428 | 2107 print_internal (compiled_function_constants (f), printcharfun, escapeflag); |
2108 | |
2109 /* COMPILED_STACK_DEPTH = 3 */ | |
800 | 2110 write_fmt_string (printcharfun, " %d", compiled_function_stack_depth (f)); |
428 | 2111 |
2112 /* COMPILED_DOC_STRING = 4 */ | |
2113 if (docp || intp) | |
2114 { | |
826 | 2115 write_c_string (printcharfun, " "); |
428 | 2116 print_internal (compiled_function_documentation (f), printcharfun, |
2117 escapeflag); | |
2118 } | |
2119 | |
2120 /* COMPILED_INTERACTIVE = 5 */ | |
2121 if (intp) | |
2122 { | |
826 | 2123 write_c_string (printcharfun, " "); |
428 | 2124 print_internal (compiled_function_interactive (f), printcharfun, |
2125 escapeflag); | |
2126 } | |
2127 | |
2128 UNGCPRO; | |
826 | 2129 write_c_string (printcharfun, print_readably ? "]" : ">"); |
428 | 2130 } |
2131 | |
2132 | |
2133 static Lisp_Object | |
2134 mark_compiled_function (Lisp_Object obj) | |
2135 { | |
2136 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (obj); | |
814 | 2137 int i; |
428 | 2138 |
2139 mark_object (f->instructions); | |
2140 mark_object (f->arglist); | |
2141 mark_object (f->doc_and_interactive); | |
2142 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK | |
2143 mark_object (f->annotated); | |
2144 #endif | |
814 | 2145 for (i = 0; i < f->args_in_array; i++) |
2146 mark_object (f->args[i]); | |
2147 | |
428 | 2148 /* tail-recurse on constants */ |
2149 return f->constants; | |
2150 } | |
2151 | |
2152 static int | |
2153 compiled_function_equal (Lisp_Object obj1, Lisp_Object obj2, int depth) | |
2154 { | |
2155 Lisp_Compiled_Function *f1 = XCOMPILED_FUNCTION (obj1); | |
2156 Lisp_Compiled_Function *f2 = XCOMPILED_FUNCTION (obj2); | |
2157 return | |
2158 (f1->flags.documentationp == f2->flags.documentationp && | |
2159 f1->flags.interactivep == f2->flags.interactivep && | |
2160 f1->flags.domainp == f2->flags.domainp && /* I18N3 */ | |
2161 internal_equal (compiled_function_instructions (f1), | |
2162 compiled_function_instructions (f2), depth + 1) && | |
2163 internal_equal (f1->constants, f2->constants, depth + 1) && | |
2164 internal_equal (f1->arglist, f2->arglist, depth + 1) && | |
2165 internal_equal (f1->doc_and_interactive, | |
2166 f2->doc_and_interactive, depth + 1)); | |
2167 } | |
2168 | |
665 | 2169 static Hashcode |
428 | 2170 compiled_function_hash (Lisp_Object obj, int depth) |
2171 { | |
2172 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (obj); | |
2173 return HASH3 ((f->flags.documentationp << 2) + | |
2174 (f->flags.interactivep << 1) + | |
2175 f->flags.domainp, | |
2176 internal_hash (f->instructions, depth + 1), | |
2177 internal_hash (f->constants, depth + 1)); | |
2178 } | |
2179 | |
1204 | 2180 static const struct memory_description compiled_function_description[] = { |
814 | 2181 { XD_INT, offsetof (Lisp_Compiled_Function, args_in_array) }, |
2367 | 2182 { XD_BLOCK_PTR, offsetof (Lisp_Compiled_Function, args), |
2551 | 2183 XD_INDIRECT (0, 0), { &lisp_object_description } }, |
440 | 2184 { XD_LISP_OBJECT, offsetof (Lisp_Compiled_Function, instructions) }, |
2185 { XD_LISP_OBJECT, offsetof (Lisp_Compiled_Function, constants) }, | |
2186 { XD_LISP_OBJECT, offsetof (Lisp_Compiled_Function, arglist) }, | |
2187 { XD_LISP_OBJECT, offsetof (Lisp_Compiled_Function, doc_and_interactive) }, | |
428 | 2188 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK |
440 | 2189 { XD_LISP_OBJECT, offsetof (Lisp_Compiled_Function, annotated) }, |
428 | 2190 #endif |
2191 { XD_END } | |
2192 }; | |
2193 | |
2720 | 2194 #ifdef MC_ALLOC |
2195 static void | |
2196 finalize_compiled_function (void *header, int for_disksave) | |
2197 { | |
2198 if (!for_disksave) | |
2199 { | |
2200 struct Lisp_Compiled_Function *cf = | |
2201 (struct Lisp_Compiled_Function *) header; | |
2202 if (cf->args_in_array) | |
2203 xfree (cf->args, Lisp_Object *); | |
2204 } | |
2205 } | |
2206 | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
2720
diff
changeset
|
2207 DEFINE_BASIC_LISP_OBJECT ("compiled-function", compiled_function, |
2720 | 2208 mark_compiled_function, |
2209 print_compiled_function, | |
2210 finalize_compiled_function, | |
2211 compiled_function_equal, | |
2212 compiled_function_hash, | |
2213 compiled_function_description, | |
2214 Lisp_Compiled_Function); | |
2215 #else /* not MC_ALLOC */ | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
2720
diff
changeset
|
2216 DEFINE_BASIC_LISP_OBJECT ("compiled-function", compiled_function, |
934 | 2217 mark_compiled_function, |
2218 print_compiled_function, 0, | |
2219 compiled_function_equal, | |
2220 compiled_function_hash, | |
2221 compiled_function_description, | |
2222 Lisp_Compiled_Function); | |
2720 | 2223 #endif /* not MC_ALLOC */ |
428 | 2224 |
2225 DEFUN ("compiled-function-p", Fcompiled_function_p, 1, 1, 0, /* | |
2226 Return t if OBJECT is a byte-compiled function object. | |
2227 */ | |
2228 (object)) | |
2229 { | |
2230 return COMPILED_FUNCTIONP (object) ? Qt : Qnil; | |
2231 } | |
2232 | |
2233 /************************************************************************/ | |
2234 /* compiled-function object accessor functions */ | |
2235 /************************************************************************/ | |
2236 | |
2237 Lisp_Object | |
2238 compiled_function_arglist (Lisp_Compiled_Function *f) | |
2239 { | |
2240 return f->arglist; | |
2241 } | |
2242 | |
2243 Lisp_Object | |
2244 compiled_function_instructions (Lisp_Compiled_Function *f) | |
2245 { | |
2246 if (! OPAQUEP (f->instructions)) | |
2247 return f->instructions; | |
2248 | |
2249 { | |
2250 /* Invert action performed by optimize_byte_code() */ | |
2251 Lisp_Opaque *opaque = XOPAQUE (f->instructions); | |
2252 | |
867 | 2253 Ibyte * const buffer = |
2367 | 2254 alloca_ibytes (OPAQUE_SIZE (opaque) * MAX_ICHAR_LEN); |
867 | 2255 Ibyte *bp = buffer; |
428 | 2256 |
442 | 2257 const Opbyte * const program = (const Opbyte *) OPAQUE_DATA (opaque); |
2258 const Opbyte *program_ptr = program; | |
2259 const Opbyte * const program_end = program_ptr + OPAQUE_SIZE (opaque); | |
428 | 2260 |
2261 while (program_ptr < program_end) | |
2262 { | |
2263 Opcode opcode = (Opcode) READ_UINT_1; | |
867 | 2264 bp += set_itext_ichar (bp, opcode); |
428 | 2265 switch (opcode) |
2266 { | |
2267 case Bvarref+7: | |
2268 case Bvarset+7: | |
2269 case Bvarbind+7: | |
2270 case Bcall+7: | |
2271 case Bunbind+7: | |
2272 case Bconstant2: | |
867 | 2273 bp += set_itext_ichar (bp, READ_UINT_1); |
2274 bp += set_itext_ichar (bp, READ_UINT_1); | |
428 | 2275 break; |
2276 | |
2277 case Bvarref+6: | |
2278 case Bvarset+6: | |
2279 case Bvarbind+6: | |
2280 case Bcall+6: | |
2281 case Bunbind+6: | |
2282 case BlistN: | |
2283 case BconcatN: | |
2284 case BinsertN: | |
867 | 2285 bp += set_itext_ichar (bp, READ_UINT_1); |
428 | 2286 break; |
2287 | |
2288 case Bgoto: | |
2289 case Bgotoifnil: | |
2290 case Bgotoifnonnil: | |
2291 case Bgotoifnilelsepop: | |
2292 case Bgotoifnonnilelsepop: | |
2293 { | |
2294 int jump = READ_INT_2; | |
2295 Opbyte buf2[2]; | |
2296 Opbyte *buf2p = buf2; | |
2297 /* Convert back to program-relative address */ | |
2298 WRITE_INT16 (jump + (program_ptr - 2 - program), buf2p); | |
867 | 2299 bp += set_itext_ichar (bp, buf2[0]); |
2300 bp += set_itext_ichar (bp, buf2[1]); | |
428 | 2301 break; |
2302 } | |
2303 | |
2304 case BRgoto: | |
2305 case BRgotoifnil: | |
2306 case BRgotoifnonnil: | |
2307 case BRgotoifnilelsepop: | |
2308 case BRgotoifnonnilelsepop: | |
867 | 2309 bp += set_itext_ichar (bp, READ_INT_1 + 127); |
428 | 2310 break; |
2311 | |
2312 default: | |
2313 break; | |
2314 } | |
2315 } | |
2316 return make_string (buffer, bp - buffer); | |
2317 } | |
2318 } | |
2319 | |
2320 Lisp_Object | |
2321 compiled_function_constants (Lisp_Compiled_Function *f) | |
2322 { | |
2323 return f->constants; | |
2324 } | |
2325 | |
2326 int | |
2327 compiled_function_stack_depth (Lisp_Compiled_Function *f) | |
2328 { | |
2329 return f->stack_depth; | |
2330 } | |
2331 | |
2332 /* The compiled_function->doc_and_interactive slot uses the minimal | |
2333 number of conses, based on compiled_function->flags; it may take | |
2334 any of the following forms: | |
2335 | |
2336 doc | |
2337 interactive | |
2338 domain | |
2339 (doc . interactive) | |
2340 (doc . domain) | |
2341 (interactive . domain) | |
2342 (doc . (interactive . domain)) | |
2343 */ | |
2344 | |
2345 /* Caller must check flags.interactivep first */ | |
2346 Lisp_Object | |
2347 compiled_function_interactive (Lisp_Compiled_Function *f) | |
2348 { | |
2349 assert (f->flags.interactivep); | |
2350 if (f->flags.documentationp && f->flags.domainp) | |
2351 return XCAR (XCDR (f->doc_and_interactive)); | |
2352 else if (f->flags.documentationp) | |
2353 return XCDR (f->doc_and_interactive); | |
2354 else if (f->flags.domainp) | |
2355 return XCAR (f->doc_and_interactive); | |
2356 else | |
2357 return f->doc_and_interactive; | |
2358 } | |
2359 | |
2360 /* Caller need not check flags.documentationp first */ | |
2361 Lisp_Object | |
2362 compiled_function_documentation (Lisp_Compiled_Function *f) | |
2363 { | |
2364 if (! f->flags.documentationp) | |
2365 return Qnil; | |
2366 else if (f->flags.interactivep && f->flags.domainp) | |
2367 return XCAR (f->doc_and_interactive); | |
2368 else if (f->flags.interactivep) | |
2369 return XCAR (f->doc_and_interactive); | |
2370 else if (f->flags.domainp) | |
2371 return XCAR (f->doc_and_interactive); | |
2372 else | |
2373 return f->doc_and_interactive; | |
2374 } | |
2375 | |
2376 /* Caller need not check flags.domainp first */ | |
2377 Lisp_Object | |
2378 compiled_function_domain (Lisp_Compiled_Function *f) | |
2379 { | |
2380 if (! f->flags.domainp) | |
2381 return Qnil; | |
2382 else if (f->flags.documentationp && f->flags.interactivep) | |
2383 return XCDR (XCDR (f->doc_and_interactive)); | |
2384 else if (f->flags.documentationp) | |
2385 return XCDR (f->doc_and_interactive); | |
2386 else if (f->flags.interactivep) | |
2387 return XCDR (f->doc_and_interactive); | |
2388 else | |
2389 return f->doc_and_interactive; | |
2390 } | |
2391 | |
2392 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK | |
2393 | |
2394 Lisp_Object | |
2395 compiled_function_annotation (Lisp_Compiled_Function *f) | |
2396 { | |
2397 return f->annotated; | |
2398 } | |
2399 | |
2400 #endif | |
2401 | |
2402 /* used only by Snarf-documentation; there must be doc already. */ | |
2403 void | |
2404 set_compiled_function_documentation (Lisp_Compiled_Function *f, | |
2405 Lisp_Object new_doc) | |
2406 { | |
2407 assert (f->flags.documentationp); | |
2408 assert (INTP (new_doc) || STRINGP (new_doc)); | |
2409 | |
2410 if (f->flags.interactivep && f->flags.domainp) | |
2411 XCAR (f->doc_and_interactive) = new_doc; | |
2412 else if (f->flags.interactivep) | |
2413 XCAR (f->doc_and_interactive) = new_doc; | |
2414 else if (f->flags.domainp) | |
2415 XCAR (f->doc_and_interactive) = new_doc; | |
2416 else | |
2417 f->doc_and_interactive = new_doc; | |
2418 } | |
2419 | |
2420 | |
2421 DEFUN ("compiled-function-arglist", Fcompiled_function_arglist, 1, 1, 0, /* | |
2422 Return the argument list of the compiled-function object FUNCTION. | |
2423 */ | |
2424 (function)) | |
2425 { | |
2426 CHECK_COMPILED_FUNCTION (function); | |
2427 return compiled_function_arglist (XCOMPILED_FUNCTION (function)); | |
2428 } | |
2429 | |
2430 DEFUN ("compiled-function-instructions", Fcompiled_function_instructions, 1, 1, 0, /* | |
2431 Return the byte-opcode string of the compiled-function object FUNCTION. | |
2432 */ | |
2433 (function)) | |
2434 { | |
2435 CHECK_COMPILED_FUNCTION (function); | |
2436 return compiled_function_instructions (XCOMPILED_FUNCTION (function)); | |
2437 } | |
2438 | |
2439 DEFUN ("compiled-function-constants", Fcompiled_function_constants, 1, 1, 0, /* | |
2440 Return the constants vector of the compiled-function object FUNCTION. | |
2441 */ | |
2442 (function)) | |
2443 { | |
2444 CHECK_COMPILED_FUNCTION (function); | |
2445 return compiled_function_constants (XCOMPILED_FUNCTION (function)); | |
2446 } | |
2447 | |
2448 DEFUN ("compiled-function-stack-depth", Fcompiled_function_stack_depth, 1, 1, 0, /* | |
444 | 2449 Return the maximum stack depth of the compiled-function object FUNCTION. |
428 | 2450 */ |
2451 (function)) | |
2452 { | |
2453 CHECK_COMPILED_FUNCTION (function); | |
2454 return make_int (compiled_function_stack_depth (XCOMPILED_FUNCTION (function))); | |
2455 } | |
2456 | |
2457 DEFUN ("compiled-function-doc-string", Fcompiled_function_doc_string, 1, 1, 0, /* | |
2458 Return the doc string of the compiled-function object FUNCTION, if available. | |
2459 Functions that had their doc strings snarfed into the DOC file will have | |
2460 an integer returned instead of a string. | |
2461 */ | |
2462 (function)) | |
2463 { | |
2464 CHECK_COMPILED_FUNCTION (function); | |
2465 return compiled_function_documentation (XCOMPILED_FUNCTION (function)); | |
2466 } | |
2467 | |
2468 DEFUN ("compiled-function-interactive", Fcompiled_function_interactive, 1, 1, 0, /* | |
2469 Return the interactive spec of the compiled-function object FUNCTION, or nil. | |
2470 If non-nil, the return value will be a list whose first element is | |
2471 `interactive' and whose second element is the interactive spec. | |
2472 */ | |
2473 (function)) | |
2474 { | |
2475 CHECK_COMPILED_FUNCTION (function); | |
2476 return XCOMPILED_FUNCTION (function)->flags.interactivep | |
2477 ? list2 (Qinteractive, | |
2478 compiled_function_interactive (XCOMPILED_FUNCTION (function))) | |
2479 : Qnil; | |
2480 } | |
2481 | |
2482 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK | |
2483 | |
826 | 2484 DEFUN ("compiled-function-annotation", Fcompiled_function_annotation, 1, 1, 0, /* |
428 | 2485 Return the annotation of the compiled-function object FUNCTION, or nil. |
2486 The annotation is a piece of information indicating where this | |
2487 compiled-function object came from. Generally this will be | |
2488 a symbol naming a function; or a string naming a file, if the | |
2489 compiled-function object was not defined in a function; or nil, | |
2490 if the compiled-function object was not created as a result of | |
2491 a `load'. | |
2492 */ | |
2493 (function)) | |
2494 { | |
2495 CHECK_COMPILED_FUNCTION (function); | |
2496 return compiled_function_annotation (XCOMPILED_FUNCTION (function)); | |
2497 } | |
2498 | |
2499 #endif /* COMPILED_FUNCTION_ANNOTATION_HACK */ | |
2500 | |
2501 DEFUN ("compiled-function-domain", Fcompiled_function_domain, 1, 1, 0, /* | |
2502 Return the domain of the compiled-function object FUNCTION, or nil. | |
2503 This is only meaningful if I18N3 was enabled when emacs was compiled. | |
2504 */ | |
2505 (function)) | |
2506 { | |
2507 CHECK_COMPILED_FUNCTION (function); | |
2508 return XCOMPILED_FUNCTION (function)->flags.domainp | |
2509 ? compiled_function_domain (XCOMPILED_FUNCTION (function)) | |
2510 : Qnil; | |
2511 } | |
2512 | |
2513 | |
2514 | |
2515 DEFUN ("fetch-bytecode", Ffetch_bytecode, 1, 1, 0, /* | |
2516 If the byte code for compiled function FUNCTION is lazy-loaded, fetch it now. | |
2517 */ | |
2518 (function)) | |
2519 { | |
2520 Lisp_Compiled_Function *f; | |
2521 CHECK_COMPILED_FUNCTION (function); | |
2522 f = XCOMPILED_FUNCTION (function); | |
2523 | |
2524 if (OPAQUEP (f->instructions) || STRINGP (f->instructions)) | |
2525 return function; | |
2526 | |
2527 if (CONSP (f->instructions)) | |
2528 { | |
2529 Lisp_Object tem = read_doc_string (f->instructions); | |
2530 if (!CONSP (tem)) | |
563 | 2531 signal_error (Qinvalid_byte_code, |
2532 "Invalid lazy-loaded byte code", tem); | |
428 | 2533 /* v18 or v19 bytecode file. Need to Ebolify. */ |
2534 if (f->flags.ebolified && VECTORP (XCDR (tem))) | |
2535 ebolify_bytecode_constants (XCDR (tem)); | |
2536 f->instructions = XCAR (tem); | |
2537 f->constants = XCDR (tem); | |
2538 return function; | |
2539 } | |
2500 | 2540 ABORT (); |
801 | 2541 return Qnil; /* not (usually) reached */ |
428 | 2542 } |
2543 | |
2544 DEFUN ("optimize-compiled-function", Foptimize_compiled_function, 1, 1, 0, /* | |
2545 Convert compiled function FUNCTION into an optimized internal form. | |
2546 */ | |
2547 (function)) | |
2548 { | |
2549 Lisp_Compiled_Function *f; | |
2550 CHECK_COMPILED_FUNCTION (function); | |
2551 f = XCOMPILED_FUNCTION (function); | |
2552 | |
2553 if (OPAQUEP (f->instructions)) /* Already optimized? */ | |
2554 return Qnil; | |
2555 | |
2556 optimize_compiled_function (function); | |
2557 return Qnil; | |
2558 } | |
2559 | |
2560 DEFUN ("byte-code", Fbyte_code, 3, 3, 0, /* | |
2561 Function used internally in byte-compiled code. | |
2562 First argument INSTRUCTIONS is a string of byte code. | |
2563 Second argument CONSTANTS is a vector of constants. | |
2564 Third argument STACK-DEPTH is the maximum stack depth used in this function. | |
2565 If STACK-DEPTH is incorrect, Emacs may crash. | |
2566 */ | |
2567 (instructions, constants, stack_depth)) | |
2568 { | |
2569 /* This function can GC */ | |
2570 int varbind_count; | |
2571 int program_length; | |
2572 Opbyte *program; | |
2573 | |
2574 CHECK_STRING (instructions); | |
2575 CHECK_VECTOR (constants); | |
2576 CHECK_NATNUM (stack_depth); | |
2577 | |
2578 /* Optimize the `instructions' string, just like when executing a | |
2579 regular compiled function, but don't save it for later since this is | |
2580 likely to only be executed once. */ | |
2581 program = alloca_array (Opbyte, 1 + 2 * XSTRING_LENGTH (instructions)); | |
2582 optimize_byte_code (instructions, constants, program, | |
2583 &program_length, &varbind_count); | |
2584 SPECPDL_RESERVE (varbind_count); | |
2585 return execute_optimized_program (program, | |
2586 XINT (stack_depth), | |
2587 XVECTOR_DATA (constants)); | |
2588 } | |
2589 | |
2590 | |
2591 void | |
2592 syms_of_bytecode (void) | |
2593 { | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
2720
diff
changeset
|
2594 INIT_LISP_OBJECT (compiled_function); |
442 | 2595 |
2596 DEFERROR_STANDARD (Qinvalid_byte_code, Qinvalid_state); | |
563 | 2597 DEFSYMBOL (Qbyte_code); |
2598 DEFSYMBOL_MULTIWORD_PREDICATE (Qcompiled_functionp); | |
428 | 2599 |
2600 DEFSUBR (Fbyte_code); | |
2601 DEFSUBR (Ffetch_bytecode); | |
2602 DEFSUBR (Foptimize_compiled_function); | |
2603 | |
2604 DEFSUBR (Fcompiled_function_p); | |
2605 DEFSUBR (Fcompiled_function_instructions); | |
2606 DEFSUBR (Fcompiled_function_constants); | |
2607 DEFSUBR (Fcompiled_function_stack_depth); | |
2608 DEFSUBR (Fcompiled_function_arglist); | |
2609 DEFSUBR (Fcompiled_function_interactive); | |
2610 DEFSUBR (Fcompiled_function_doc_string); | |
2611 DEFSUBR (Fcompiled_function_domain); | |
2612 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK | |
2613 DEFSUBR (Fcompiled_function_annotation); | |
2614 #endif | |
2615 | |
2616 #ifdef BYTE_CODE_METER | |
563 | 2617 DEFSYMBOL (Qbyte_code_meter); |
428 | 2618 #endif |
2619 } | |
2620 | |
2621 void | |
2622 vars_of_bytecode (void) | |
2623 { | |
2624 #ifdef BYTE_CODE_METER | |
2625 | |
2626 DEFVAR_LISP ("byte-code-meter", &Vbyte_code_meter /* | |
2627 A vector of vectors which holds a histogram of byte code usage. | |
2628 \(aref (aref byte-code-meter 0) CODE) indicates how many times the byte | |
2629 opcode CODE has been executed. | |
2630 \(aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0, | |
2631 indicates how many times the byte opcodes CODE1 and CODE2 have been | |
2632 executed in succession. | |
2633 */ ); | |
2634 DEFVAR_BOOL ("byte-metering-on", &byte_metering_on /* | |
2635 If non-nil, keep profiling information on byte code usage. | |
2636 The variable `byte-code-meter' indicates how often each byte opcode is used. | |
2637 If a symbol has a property named `byte-code-meter' whose value is an | |
2638 integer, it is incremented each time that symbol's function is called. | |
2639 */ ); | |
2640 | |
2641 byte_metering_on = 0; | |
2642 Vbyte_code_meter = make_vector (256, Qzero); | |
2643 { | |
2644 int i = 256; | |
2645 while (i--) | |
2646 XVECTOR_DATA (Vbyte_code_meter)[i] = make_vector (256, Qzero); | |
2647 } | |
2648 #endif /* BYTE_CODE_METER */ | |
2649 } |