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