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,
|
442
|
1351 const Opbyte *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;
|
853
|
1380 TOP = internal_catch (TOP, Feval, arg, 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:
|
|
1605 abort();
|
|
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:
|
|
1976 abort();
|
|
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);
|
|
2052 f->specpdl_depth = XINT (Flength (f->arglist)) + varbind_count;
|
|
2053 f->instructions =
|
440
|
2054 make_opaque (program, program_length * sizeof (Opbyte));
|
428
|
2055 }
|
|
2056
|
|
2057 assert (OPAQUEP (f->instructions));
|
|
2058 }
|
|
2059
|
|
2060 /************************************************************************/
|
|
2061 /* The compiled-function object type */
|
|
2062 /************************************************************************/
|
|
2063 static void
|
|
2064 print_compiled_function (Lisp_Object obj, Lisp_Object printcharfun,
|
|
2065 int escapeflag)
|
|
2066 {
|
|
2067 /* This function can GC */
|
|
2068 Lisp_Compiled_Function *f =
|
|
2069 XCOMPILED_FUNCTION (obj); /* GC doesn't relocate */
|
|
2070 int docp = f->flags.documentationp;
|
|
2071 int intp = f->flags.interactivep;
|
|
2072 struct gcpro gcpro1, gcpro2;
|
|
2073 GCPRO2 (obj, printcharfun);
|
|
2074
|
826
|
2075 write_c_string (printcharfun, print_readably ? "#[" : "#<compiled-function ");
|
428
|
2076 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
|
|
2077 if (!print_readably)
|
|
2078 {
|
|
2079 Lisp_Object ann = compiled_function_annotation (f);
|
|
2080 if (!NILP (ann))
|
800
|
2081 write_fmt_string_lisp (printcharfun, "(from %S) ", 1, ann);
|
428
|
2082 }
|
|
2083 #endif /* COMPILED_FUNCTION_ANNOTATION_HACK */
|
|
2084 /* COMPILED_ARGLIST = 0 */
|
|
2085 print_internal (compiled_function_arglist (f), printcharfun, escapeflag);
|
|
2086
|
|
2087 /* COMPILED_INSTRUCTIONS = 1 */
|
826
|
2088 write_c_string (printcharfun, " ");
|
428
|
2089 {
|
|
2090 struct gcpro ngcpro1;
|
|
2091 Lisp_Object instructions = compiled_function_instructions (f);
|
|
2092 NGCPRO1 (instructions);
|
|
2093 if (STRINGP (instructions) && !print_readably)
|
|
2094 {
|
|
2095 /* We don't usually want to see that junk in the bytecode. */
|
800
|
2096 write_fmt_string (printcharfun, "\"...(%ld)\"",
|
826
|
2097 (long) string_char_length (instructions));
|
428
|
2098 }
|
|
2099 else
|
|
2100 print_internal (instructions, printcharfun, escapeflag);
|
|
2101 NUNGCPRO;
|
|
2102 }
|
|
2103
|
|
2104 /* COMPILED_CONSTANTS = 2 */
|
826
|
2105 write_c_string (printcharfun, " ");
|
428
|
2106 print_internal (compiled_function_constants (f), printcharfun, escapeflag);
|
|
2107
|
|
2108 /* COMPILED_STACK_DEPTH = 3 */
|
800
|
2109 write_fmt_string (printcharfun, " %d", compiled_function_stack_depth (f));
|
428
|
2110
|
|
2111 /* COMPILED_DOC_STRING = 4 */
|
|
2112 if (docp || intp)
|
|
2113 {
|
826
|
2114 write_c_string (printcharfun, " ");
|
428
|
2115 print_internal (compiled_function_documentation (f), printcharfun,
|
|
2116 escapeflag);
|
|
2117 }
|
|
2118
|
|
2119 /* COMPILED_INTERACTIVE = 5 */
|
|
2120 if (intp)
|
|
2121 {
|
826
|
2122 write_c_string (printcharfun, " ");
|
428
|
2123 print_internal (compiled_function_interactive (f), printcharfun,
|
|
2124 escapeflag);
|
|
2125 }
|
|
2126
|
|
2127 UNGCPRO;
|
826
|
2128 write_c_string (printcharfun, print_readably ? "]" : ">");
|
428
|
2129 }
|
|
2130
|
|
2131
|
|
2132 static Lisp_Object
|
|
2133 mark_compiled_function (Lisp_Object obj)
|
|
2134 {
|
|
2135 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (obj);
|
814
|
2136 int i;
|
428
|
2137
|
|
2138 mark_object (f->instructions);
|
|
2139 mark_object (f->arglist);
|
|
2140 mark_object (f->doc_and_interactive);
|
|
2141 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
|
|
2142 mark_object (f->annotated);
|
|
2143 #endif
|
814
|
2144 for (i = 0; i < f->args_in_array; i++)
|
|
2145 mark_object (f->args[i]);
|
|
2146
|
428
|
2147 /* tail-recurse on constants */
|
|
2148 return f->constants;
|
|
2149 }
|
|
2150
|
|
2151 static int
|
|
2152 compiled_function_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
2153 {
|
|
2154 Lisp_Compiled_Function *f1 = XCOMPILED_FUNCTION (obj1);
|
|
2155 Lisp_Compiled_Function *f2 = XCOMPILED_FUNCTION (obj2);
|
|
2156 return
|
|
2157 (f1->flags.documentationp == f2->flags.documentationp &&
|
|
2158 f1->flags.interactivep == f2->flags.interactivep &&
|
|
2159 f1->flags.domainp == f2->flags.domainp && /* I18N3 */
|
|
2160 internal_equal (compiled_function_instructions (f1),
|
|
2161 compiled_function_instructions (f2), depth + 1) &&
|
|
2162 internal_equal (f1->constants, f2->constants, depth + 1) &&
|
|
2163 internal_equal (f1->arglist, f2->arglist, depth + 1) &&
|
|
2164 internal_equal (f1->doc_and_interactive,
|
|
2165 f2->doc_and_interactive, depth + 1));
|
|
2166 }
|
|
2167
|
665
|
2168 static Hashcode
|
428
|
2169 compiled_function_hash (Lisp_Object obj, int depth)
|
|
2170 {
|
|
2171 Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (obj);
|
|
2172 return HASH3 ((f->flags.documentationp << 2) +
|
|
2173 (f->flags.interactivep << 1) +
|
|
2174 f->flags.domainp,
|
|
2175 internal_hash (f->instructions, depth + 1),
|
|
2176 internal_hash (f->constants, depth + 1));
|
|
2177 }
|
|
2178
|
1204
|
2179 static const struct memory_description compiled_function_description[] = {
|
814
|
2180 { XD_INT, offsetof (Lisp_Compiled_Function, args_in_array) },
|
|
2181 { XD_STRUCT_PTR, offsetof (Lisp_Compiled_Function, args),
|
1204
|
2182 XD_INDIRECT (0, 0), &lisp_object_description },
|
440
|
2183 { XD_LISP_OBJECT, offsetof (Lisp_Compiled_Function, instructions) },
|
|
2184 { XD_LISP_OBJECT, offsetof (Lisp_Compiled_Function, constants) },
|
|
2185 { XD_LISP_OBJECT, offsetof (Lisp_Compiled_Function, arglist) },
|
|
2186 { XD_LISP_OBJECT, offsetof (Lisp_Compiled_Function, doc_and_interactive) },
|
428
|
2187 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
|
440
|
2188 { XD_LISP_OBJECT, offsetof (Lisp_Compiled_Function, annotated) },
|
428
|
2189 #endif
|
|
2190 { XD_END }
|
|
2191 };
|
|
2192
|
934
|
2193 DEFINE_BASIC_LRECORD_IMPLEMENTATION ("compiled-function", compiled_function,
|
|
2194 1, /*dumpable_flag*/
|
|
2195 mark_compiled_function,
|
|
2196 print_compiled_function, 0,
|
|
2197 compiled_function_equal,
|
|
2198 compiled_function_hash,
|
|
2199 compiled_function_description,
|
|
2200 Lisp_Compiled_Function);
|
428
|
2201
|
|
2202 DEFUN ("compiled-function-p", Fcompiled_function_p, 1, 1, 0, /*
|
|
2203 Return t if OBJECT is a byte-compiled function object.
|
|
2204 */
|
|
2205 (object))
|
|
2206 {
|
|
2207 return COMPILED_FUNCTIONP (object) ? Qt : Qnil;
|
|
2208 }
|
|
2209
|
|
2210 /************************************************************************/
|
|
2211 /* compiled-function object accessor functions */
|
|
2212 /************************************************************************/
|
|
2213
|
|
2214 Lisp_Object
|
|
2215 compiled_function_arglist (Lisp_Compiled_Function *f)
|
|
2216 {
|
|
2217 return f->arglist;
|
|
2218 }
|
|
2219
|
|
2220 Lisp_Object
|
|
2221 compiled_function_instructions (Lisp_Compiled_Function *f)
|
|
2222 {
|
|
2223 if (! OPAQUEP (f->instructions))
|
|
2224 return f->instructions;
|
|
2225
|
|
2226 {
|
|
2227 /* Invert action performed by optimize_byte_code() */
|
|
2228 Lisp_Opaque *opaque = XOPAQUE (f->instructions);
|
|
2229
|
867
|
2230 Ibyte * const buffer =
|
|
2231 alloca_array (Ibyte, OPAQUE_SIZE (opaque) * MAX_ICHAR_LEN);
|
|
2232 Ibyte *bp = buffer;
|
428
|
2233
|
442
|
2234 const Opbyte * const program = (const Opbyte *) OPAQUE_DATA (opaque);
|
|
2235 const Opbyte *program_ptr = program;
|
|
2236 const Opbyte * const program_end = program_ptr + OPAQUE_SIZE (opaque);
|
428
|
2237
|
|
2238 while (program_ptr < program_end)
|
|
2239 {
|
|
2240 Opcode opcode = (Opcode) READ_UINT_1;
|
867
|
2241 bp += set_itext_ichar (bp, opcode);
|
428
|
2242 switch (opcode)
|
|
2243 {
|
|
2244 case Bvarref+7:
|
|
2245 case Bvarset+7:
|
|
2246 case Bvarbind+7:
|
|
2247 case Bcall+7:
|
|
2248 case Bunbind+7:
|
|
2249 case Bconstant2:
|
867
|
2250 bp += set_itext_ichar (bp, READ_UINT_1);
|
|
2251 bp += set_itext_ichar (bp, READ_UINT_1);
|
428
|
2252 break;
|
|
2253
|
|
2254 case Bvarref+6:
|
|
2255 case Bvarset+6:
|
|
2256 case Bvarbind+6:
|
|
2257 case Bcall+6:
|
|
2258 case Bunbind+6:
|
|
2259 case BlistN:
|
|
2260 case BconcatN:
|
|
2261 case BinsertN:
|
867
|
2262 bp += set_itext_ichar (bp, READ_UINT_1);
|
428
|
2263 break;
|
|
2264
|
|
2265 case Bgoto:
|
|
2266 case Bgotoifnil:
|
|
2267 case Bgotoifnonnil:
|
|
2268 case Bgotoifnilelsepop:
|
|
2269 case Bgotoifnonnilelsepop:
|
|
2270 {
|
|
2271 int jump = READ_INT_2;
|
|
2272 Opbyte buf2[2];
|
|
2273 Opbyte *buf2p = buf2;
|
|
2274 /* Convert back to program-relative address */
|
|
2275 WRITE_INT16 (jump + (program_ptr - 2 - program), buf2p);
|
867
|
2276 bp += set_itext_ichar (bp, buf2[0]);
|
|
2277 bp += set_itext_ichar (bp, buf2[1]);
|
428
|
2278 break;
|
|
2279 }
|
|
2280
|
|
2281 case BRgoto:
|
|
2282 case BRgotoifnil:
|
|
2283 case BRgotoifnonnil:
|
|
2284 case BRgotoifnilelsepop:
|
|
2285 case BRgotoifnonnilelsepop:
|
867
|
2286 bp += set_itext_ichar (bp, READ_INT_1 + 127);
|
428
|
2287 break;
|
|
2288
|
|
2289 default:
|
|
2290 break;
|
|
2291 }
|
|
2292 }
|
|
2293 return make_string (buffer, bp - buffer);
|
|
2294 }
|
|
2295 }
|
|
2296
|
|
2297 Lisp_Object
|
|
2298 compiled_function_constants (Lisp_Compiled_Function *f)
|
|
2299 {
|
|
2300 return f->constants;
|
|
2301 }
|
|
2302
|
|
2303 int
|
|
2304 compiled_function_stack_depth (Lisp_Compiled_Function *f)
|
|
2305 {
|
|
2306 return f->stack_depth;
|
|
2307 }
|
|
2308
|
|
2309 /* The compiled_function->doc_and_interactive slot uses the minimal
|
|
2310 number of conses, based on compiled_function->flags; it may take
|
|
2311 any of the following forms:
|
|
2312
|
|
2313 doc
|
|
2314 interactive
|
|
2315 domain
|
|
2316 (doc . interactive)
|
|
2317 (doc . domain)
|
|
2318 (interactive . domain)
|
|
2319 (doc . (interactive . domain))
|
|
2320 */
|
|
2321
|
|
2322 /* Caller must check flags.interactivep first */
|
|
2323 Lisp_Object
|
|
2324 compiled_function_interactive (Lisp_Compiled_Function *f)
|
|
2325 {
|
|
2326 assert (f->flags.interactivep);
|
|
2327 if (f->flags.documentationp && f->flags.domainp)
|
|
2328 return XCAR (XCDR (f->doc_and_interactive));
|
|
2329 else if (f->flags.documentationp)
|
|
2330 return XCDR (f->doc_and_interactive);
|
|
2331 else if (f->flags.domainp)
|
|
2332 return XCAR (f->doc_and_interactive);
|
|
2333 else
|
|
2334 return f->doc_and_interactive;
|
|
2335 }
|
|
2336
|
|
2337 /* Caller need not check flags.documentationp first */
|
|
2338 Lisp_Object
|
|
2339 compiled_function_documentation (Lisp_Compiled_Function *f)
|
|
2340 {
|
|
2341 if (! f->flags.documentationp)
|
|
2342 return Qnil;
|
|
2343 else if (f->flags.interactivep && f->flags.domainp)
|
|
2344 return XCAR (f->doc_and_interactive);
|
|
2345 else if (f->flags.interactivep)
|
|
2346 return XCAR (f->doc_and_interactive);
|
|
2347 else if (f->flags.domainp)
|
|
2348 return XCAR (f->doc_and_interactive);
|
|
2349 else
|
|
2350 return f->doc_and_interactive;
|
|
2351 }
|
|
2352
|
|
2353 /* Caller need not check flags.domainp first */
|
|
2354 Lisp_Object
|
|
2355 compiled_function_domain (Lisp_Compiled_Function *f)
|
|
2356 {
|
|
2357 if (! f->flags.domainp)
|
|
2358 return Qnil;
|
|
2359 else if (f->flags.documentationp && f->flags.interactivep)
|
|
2360 return XCDR (XCDR (f->doc_and_interactive));
|
|
2361 else if (f->flags.documentationp)
|
|
2362 return XCDR (f->doc_and_interactive);
|
|
2363 else if (f->flags.interactivep)
|
|
2364 return XCDR (f->doc_and_interactive);
|
|
2365 else
|
|
2366 return f->doc_and_interactive;
|
|
2367 }
|
|
2368
|
|
2369 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
|
|
2370
|
|
2371 Lisp_Object
|
|
2372 compiled_function_annotation (Lisp_Compiled_Function *f)
|
|
2373 {
|
|
2374 return f->annotated;
|
|
2375 }
|
|
2376
|
|
2377 #endif
|
|
2378
|
|
2379 /* used only by Snarf-documentation; there must be doc already. */
|
|
2380 void
|
|
2381 set_compiled_function_documentation (Lisp_Compiled_Function *f,
|
|
2382 Lisp_Object new_doc)
|
|
2383 {
|
|
2384 assert (f->flags.documentationp);
|
|
2385 assert (INTP (new_doc) || STRINGP (new_doc));
|
|
2386
|
|
2387 if (f->flags.interactivep && f->flags.domainp)
|
|
2388 XCAR (f->doc_and_interactive) = new_doc;
|
|
2389 else if (f->flags.interactivep)
|
|
2390 XCAR (f->doc_and_interactive) = new_doc;
|
|
2391 else if (f->flags.domainp)
|
|
2392 XCAR (f->doc_and_interactive) = new_doc;
|
|
2393 else
|
|
2394 f->doc_and_interactive = new_doc;
|
|
2395 }
|
|
2396
|
|
2397
|
|
2398 DEFUN ("compiled-function-arglist", Fcompiled_function_arglist, 1, 1, 0, /*
|
|
2399 Return the argument list of the compiled-function object FUNCTION.
|
|
2400 */
|
|
2401 (function))
|
|
2402 {
|
|
2403 CHECK_COMPILED_FUNCTION (function);
|
|
2404 return compiled_function_arglist (XCOMPILED_FUNCTION (function));
|
|
2405 }
|
|
2406
|
|
2407 DEFUN ("compiled-function-instructions", Fcompiled_function_instructions, 1, 1, 0, /*
|
|
2408 Return the byte-opcode string of the compiled-function object FUNCTION.
|
|
2409 */
|
|
2410 (function))
|
|
2411 {
|
|
2412 CHECK_COMPILED_FUNCTION (function);
|
|
2413 return compiled_function_instructions (XCOMPILED_FUNCTION (function));
|
|
2414 }
|
|
2415
|
|
2416 DEFUN ("compiled-function-constants", Fcompiled_function_constants, 1, 1, 0, /*
|
|
2417 Return the constants vector of the compiled-function object FUNCTION.
|
|
2418 */
|
|
2419 (function))
|
|
2420 {
|
|
2421 CHECK_COMPILED_FUNCTION (function);
|
|
2422 return compiled_function_constants (XCOMPILED_FUNCTION (function));
|
|
2423 }
|
|
2424
|
|
2425 DEFUN ("compiled-function-stack-depth", Fcompiled_function_stack_depth, 1, 1, 0, /*
|
444
|
2426 Return the maximum stack depth of the compiled-function object FUNCTION.
|
428
|
2427 */
|
|
2428 (function))
|
|
2429 {
|
|
2430 CHECK_COMPILED_FUNCTION (function);
|
|
2431 return make_int (compiled_function_stack_depth (XCOMPILED_FUNCTION (function)));
|
|
2432 }
|
|
2433
|
|
2434 DEFUN ("compiled-function-doc-string", Fcompiled_function_doc_string, 1, 1, 0, /*
|
|
2435 Return the doc string of the compiled-function object FUNCTION, if available.
|
|
2436 Functions that had their doc strings snarfed into the DOC file will have
|
|
2437 an integer returned instead of a string.
|
|
2438 */
|
|
2439 (function))
|
|
2440 {
|
|
2441 CHECK_COMPILED_FUNCTION (function);
|
|
2442 return compiled_function_documentation (XCOMPILED_FUNCTION (function));
|
|
2443 }
|
|
2444
|
|
2445 DEFUN ("compiled-function-interactive", Fcompiled_function_interactive, 1, 1, 0, /*
|
|
2446 Return the interactive spec of the compiled-function object FUNCTION, or nil.
|
|
2447 If non-nil, the return value will be a list whose first element is
|
|
2448 `interactive' and whose second element is the interactive spec.
|
|
2449 */
|
|
2450 (function))
|
|
2451 {
|
|
2452 CHECK_COMPILED_FUNCTION (function);
|
|
2453 return XCOMPILED_FUNCTION (function)->flags.interactivep
|
|
2454 ? list2 (Qinteractive,
|
|
2455 compiled_function_interactive (XCOMPILED_FUNCTION (function)))
|
|
2456 : Qnil;
|
|
2457 }
|
|
2458
|
|
2459 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
|
|
2460
|
826
|
2461 DEFUN ("compiled-function-annotation", Fcompiled_function_annotation, 1, 1, 0, /*
|
428
|
2462 Return the annotation of the compiled-function object FUNCTION, or nil.
|
|
2463 The annotation is a piece of information indicating where this
|
|
2464 compiled-function object came from. Generally this will be
|
|
2465 a symbol naming a function; or a string naming a file, if the
|
|
2466 compiled-function object was not defined in a function; or nil,
|
|
2467 if the compiled-function object was not created as a result of
|
|
2468 a `load'.
|
|
2469 */
|
|
2470 (function))
|
|
2471 {
|
|
2472 CHECK_COMPILED_FUNCTION (function);
|
|
2473 return compiled_function_annotation (XCOMPILED_FUNCTION (function));
|
|
2474 }
|
|
2475
|
|
2476 #endif /* COMPILED_FUNCTION_ANNOTATION_HACK */
|
|
2477
|
|
2478 DEFUN ("compiled-function-domain", Fcompiled_function_domain, 1, 1, 0, /*
|
|
2479 Return the domain of the compiled-function object FUNCTION, or nil.
|
|
2480 This is only meaningful if I18N3 was enabled when emacs was compiled.
|
|
2481 */
|
|
2482 (function))
|
|
2483 {
|
|
2484 CHECK_COMPILED_FUNCTION (function);
|
|
2485 return XCOMPILED_FUNCTION (function)->flags.domainp
|
|
2486 ? compiled_function_domain (XCOMPILED_FUNCTION (function))
|
|
2487 : Qnil;
|
|
2488 }
|
|
2489
|
|
2490
|
|
2491
|
|
2492 DEFUN ("fetch-bytecode", Ffetch_bytecode, 1, 1, 0, /*
|
|
2493 If the byte code for compiled function FUNCTION is lazy-loaded, fetch it now.
|
|
2494 */
|
|
2495 (function))
|
|
2496 {
|
|
2497 Lisp_Compiled_Function *f;
|
|
2498 CHECK_COMPILED_FUNCTION (function);
|
|
2499 f = XCOMPILED_FUNCTION (function);
|
|
2500
|
|
2501 if (OPAQUEP (f->instructions) || STRINGP (f->instructions))
|
|
2502 return function;
|
|
2503
|
|
2504 if (CONSP (f->instructions))
|
|
2505 {
|
|
2506 Lisp_Object tem = read_doc_string (f->instructions);
|
|
2507 if (!CONSP (tem))
|
563
|
2508 signal_error (Qinvalid_byte_code,
|
|
2509 "Invalid lazy-loaded byte code", tem);
|
428
|
2510 /* v18 or v19 bytecode file. Need to Ebolify. */
|
|
2511 if (f->flags.ebolified && VECTORP (XCDR (tem)))
|
|
2512 ebolify_bytecode_constants (XCDR (tem));
|
|
2513 f->instructions = XCAR (tem);
|
|
2514 f->constants = XCDR (tem);
|
|
2515 return function;
|
|
2516 }
|
|
2517 abort ();
|
801
|
2518 return Qnil; /* not (usually) reached */
|
428
|
2519 }
|
|
2520
|
|
2521 DEFUN ("optimize-compiled-function", Foptimize_compiled_function, 1, 1, 0, /*
|
|
2522 Convert compiled function FUNCTION into an optimized internal form.
|
|
2523 */
|
|
2524 (function))
|
|
2525 {
|
|
2526 Lisp_Compiled_Function *f;
|
|
2527 CHECK_COMPILED_FUNCTION (function);
|
|
2528 f = XCOMPILED_FUNCTION (function);
|
|
2529
|
|
2530 if (OPAQUEP (f->instructions)) /* Already optimized? */
|
|
2531 return Qnil;
|
|
2532
|
|
2533 optimize_compiled_function (function);
|
|
2534 return Qnil;
|
|
2535 }
|
|
2536
|
|
2537 DEFUN ("byte-code", Fbyte_code, 3, 3, 0, /*
|
|
2538 Function used internally in byte-compiled code.
|
|
2539 First argument INSTRUCTIONS is a string of byte code.
|
|
2540 Second argument CONSTANTS is a vector of constants.
|
|
2541 Third argument STACK-DEPTH is the maximum stack depth used in this function.
|
|
2542 If STACK-DEPTH is incorrect, Emacs may crash.
|
|
2543 */
|
|
2544 (instructions, constants, stack_depth))
|
|
2545 {
|
|
2546 /* This function can GC */
|
|
2547 int varbind_count;
|
|
2548 int program_length;
|
|
2549 Opbyte *program;
|
|
2550
|
|
2551 CHECK_STRING (instructions);
|
|
2552 CHECK_VECTOR (constants);
|
|
2553 CHECK_NATNUM (stack_depth);
|
|
2554
|
|
2555 /* Optimize the `instructions' string, just like when executing a
|
|
2556 regular compiled function, but don't save it for later since this is
|
|
2557 likely to only be executed once. */
|
|
2558 program = alloca_array (Opbyte, 1 + 2 * XSTRING_LENGTH (instructions));
|
|
2559 optimize_byte_code (instructions, constants, program,
|
|
2560 &program_length, &varbind_count);
|
|
2561 SPECPDL_RESERVE (varbind_count);
|
|
2562 return execute_optimized_program (program,
|
|
2563 XINT (stack_depth),
|
|
2564 XVECTOR_DATA (constants));
|
|
2565 }
|
|
2566
|
|
2567
|
|
2568 void
|
|
2569 syms_of_bytecode (void)
|
|
2570 {
|
442
|
2571 INIT_LRECORD_IMPLEMENTATION (compiled_function);
|
|
2572
|
|
2573 DEFERROR_STANDARD (Qinvalid_byte_code, Qinvalid_state);
|
563
|
2574 DEFSYMBOL (Qbyte_code);
|
|
2575 DEFSYMBOL_MULTIWORD_PREDICATE (Qcompiled_functionp);
|
428
|
2576
|
|
2577 DEFSUBR (Fbyte_code);
|
|
2578 DEFSUBR (Ffetch_bytecode);
|
|
2579 DEFSUBR (Foptimize_compiled_function);
|
|
2580
|
|
2581 DEFSUBR (Fcompiled_function_p);
|
|
2582 DEFSUBR (Fcompiled_function_instructions);
|
|
2583 DEFSUBR (Fcompiled_function_constants);
|
|
2584 DEFSUBR (Fcompiled_function_stack_depth);
|
|
2585 DEFSUBR (Fcompiled_function_arglist);
|
|
2586 DEFSUBR (Fcompiled_function_interactive);
|
|
2587 DEFSUBR (Fcompiled_function_doc_string);
|
|
2588 DEFSUBR (Fcompiled_function_domain);
|
|
2589 #ifdef COMPILED_FUNCTION_ANNOTATION_HACK
|
|
2590 DEFSUBR (Fcompiled_function_annotation);
|
|
2591 #endif
|
|
2592
|
|
2593 #ifdef BYTE_CODE_METER
|
563
|
2594 DEFSYMBOL (Qbyte_code_meter);
|
428
|
2595 #endif
|
|
2596 }
|
|
2597
|
|
2598 void
|
|
2599 vars_of_bytecode (void)
|
|
2600 {
|
|
2601 #ifdef BYTE_CODE_METER
|
|
2602
|
|
2603 DEFVAR_LISP ("byte-code-meter", &Vbyte_code_meter /*
|
|
2604 A vector of vectors which holds a histogram of byte code usage.
|
|
2605 \(aref (aref byte-code-meter 0) CODE) indicates how many times the byte
|
|
2606 opcode CODE has been executed.
|
|
2607 \(aref (aref byte-code-meter CODE1) CODE2), where CODE1 is not 0,
|
|
2608 indicates how many times the byte opcodes CODE1 and CODE2 have been
|
|
2609 executed in succession.
|
|
2610 */ );
|
|
2611 DEFVAR_BOOL ("byte-metering-on", &byte_metering_on /*
|
|
2612 If non-nil, keep profiling information on byte code usage.
|
|
2613 The variable `byte-code-meter' indicates how often each byte opcode is used.
|
|
2614 If a symbol has a property named `byte-code-meter' whose value is an
|
|
2615 integer, it is incremented each time that symbol's function is called.
|
|
2616 */ );
|
|
2617
|
|
2618 byte_metering_on = 0;
|
|
2619 Vbyte_code_meter = make_vector (256, Qzero);
|
|
2620 {
|
|
2621 int i = 256;
|
|
2622 while (i--)
|
|
2623 XVECTOR_DATA (Vbyte_code_meter)[i] = make_vector (256, Qzero);
|
|
2624 }
|
|
2625 #endif /* BYTE_CODE_METER */
|
|
2626 }
|