f110b91d834cda95a761b2989c88617439dace82
[chise/xemacs-chise.git-] / info / internals.info-6
1 This is ../info/internals.info, produced by makeinfo version 4.0 from
2 internals/internals.texi.
3
4 INFO-DIR-SECTION XEmacs Editor
5 START-INFO-DIR-ENTRY
6 * Internals: (internals).       XEmacs Internals Manual.
7 END-INFO-DIR-ENTRY
8
9    Copyright (C) 1992 - 1996 Ben Wing.  Copyright (C) 1996, 1997 Sun
10 Microsystems.  Copyright (C) 1994 - 1998 Free Software Foundation.
11 Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
12
13    Permission is granted to make and distribute verbatim copies of this
14 manual provided the copyright notice and this permission notice are
15 preserved on all copies.
16
17    Permission is granted to copy and distribute modified versions of
18 this manual under the conditions for verbatim copying, provided that the
19 entire resulting derived work is distributed under the terms of a
20 permission notice identical to this one.
21
22    Permission is granted to copy and distribute translations of this
23 manual into another language, under the above conditions for modified
24 versions, except that this permission notice may be stated in a
25 translation approved by the Foundation.
26
27    Permission is granted to copy and distribute modified versions of
28 this manual under the conditions for verbatim copying, provided also
29 that the section entitled "GNU General Public License" is included
30 exactly as in the original, and provided that the entire resulting
31 derived work is distributed under the terms of a permission notice
32 identical to this one.
33
34    Permission is granted to copy and distribute translations of this
35 manual into another language, under the above conditions for modified
36 versions, except that the section entitled "GNU General Public License"
37 may be included in a translation approved by the Free Software
38 Foundation instead of in the original English.
39
40 \1f
41 File: internals.info,  Node: Data descriptions,  Next: Dumping phase,  Prev: Overview,  Up: Dumping
42
43 Data descriptions
44 =================
45
46    The more complex task of the dumper is to be able to write lisp
47 objects (lrecords) and C structs to disk and reload them at a different
48 address, updating all the pointers they include in the process.  This
49 is done by using external data descriptions that give information about
50 the layout of the structures in memory.
51
52    The specification of these descriptions is in lrecord.h.  A
53 description of an lrecord is an array of struct lrecord_description.
54 Each of these structs include a type, an offset in the structure and
55 some optional parameters depending on the type.  For instance, here is
56 the string description:
57
58      static const struct lrecord_description string_description[] = {
59        { XD_BYTECOUNT,         offsetof (Lisp_String, size) },
60        { XD_OPAQUE_DATA_PTR,   offsetof (Lisp_String, data), XD_INDIRECT(0, 1) },
61        { XD_LISP_OBJECT,       offsetof (Lisp_String, plist) },
62        { XD_END }
63      };
64
65    The first line indicates a member of type Bytecount, which is used by
66 the next, indirect directive.  The second means "there is a pointer to
67 some opaque data in the field `data'".  The length of said data is
68 given by the expression `XD_INDIRECT(0, 1)', which means "the value in
69 the 0th line of the description (welcome to C) plus one".  The third
70 line means "there is a Lisp_Object member `plist' in the Lisp_String
71 structure".  `XD_END' then ends the description.
72
73    This gives us all the information we need to move around what is
74 pointed to by a structure (C or lrecord) and, by transitivity,
75 everything that it points to.  The only missing information for dumping
76 is the size of the structure.  For lrecords, this is part of the
77 lrecord_implementation, so we don't need to duplicate it.  For C
78 structures we use a struct struct_description, which includes a size
79 field and a pointer to an associated array of lrecord_description.
80
81 \1f
82 File: internals.info,  Node: Dumping phase,  Next: Reloading phase,  Prev: Data descriptions,  Up: Dumping
83
84 Dumping phase
85 =============
86
87    Dumping is done by calling the function pdump() (in dumper.c) which
88 is invoked from Fdump_emacs (in emacs.c).  This function performs a
89 number of tasks.
90
91 * Menu:
92
93 * Object inventory::
94 * Address allocation::
95 * The header::
96 * Data dumping::
97 * Pointers dumping::
98
99 \1f
100 File: internals.info,  Node: Object inventory,  Next: Address allocation,  Up: Dumping phase
101
102 Object inventory
103 ----------------
104
105    The first task is to build the list of the objects to dump.  This
106 includes:
107
108    * lisp objects
109
110    * C structures
111
112    We end up with one `pdump_entry_list_elmt' per object group (arrays
113 of C structs are kept together) which includes a pointer to the first
114 object of the group, the per-object size and the count of objects in the
115 group, along with some other information which is initialized later.
116
117    These entries are linked together in `pdump_entry_list' structures
118 and can be enumerated thru either:
119
120   1. the `pdump_object_table', an array of `pdump_entry_list', one per
121      lrecord type, indexed by type number.
122
123   2. the `pdump_opaque_data_list', used for the opaque data which does
124      not include pointers, and hence does not need descriptions.
125
126   3. the `pdump_struct_table', which is a vector of
127      `struct_description'/`pdump_entry_list' pairs, used for non-opaque
128      C structures.
129
130    This uses a marking strategy similar to the garbage collector.  Some
131 differences though:
132
133   1. We do not use the mark bit (which does not exist for C structures
134      anyway); we use a big hash table instead.
135
136   2. We do not use the mark function of lrecords but instead rely on the
137      external descriptions.  This happens essentially because we need to
138      follow pointers to C structures and opaque data in addition to
139      Lisp_Object members.
140
141    This is done by `pdump_register_object()', which handles Lisp_Object
142 variables, and `pdump_register_struct()' which handles C structures,
143 which both delegate the description management to
144 `pdump_register_sub()'.
145
146    The hash table doubles as a map object to pdump_entry_list_elmt (i.e.
147 allows us to look up a pdump_entry_list_elmt with the object it points
148 to).  Entries are added with `pdump_add_entry()' and looked up with
149 `pdump_get_entry()'.  There is no need for entry removal.  The hash
150 value is computed quite simply from the object pointer by
151 `pdump_make_hash()'.
152
153    The roots for the marking are:
154
155   1. the `staticpro''ed variables (there is a special
156      `staticpro_nodump()' call for protected variables we do not want
157      to dump).
158
159   2. the variables registered via `dump_add_root_object' (`staticpro()'
160      is equivalent to `staticpro_nodump()' + `dump_add_root_object()').
161
162   3. the variables registered via `dump_add_root_struct_ptr', each of
163      which points to a C structure.
164
165    This does not include the GCPRO'ed variables, the specbinds, the
166 catchtags, the backlist, the redisplay or the profiling info, since we
167 do not want to rebuild the actual chain of lisp calls which end up to
168 the dump-emacs call, only the global variables.
169
170    Weak lists and weak hash tables are dumped as if they were their
171 non-weak equivalent (without changing their type, of course).  This has
172 not yet been a problem.
173
174 \1f
175 File: internals.info,  Node: Address allocation,  Next: The header,  Prev: Object inventory,  Up: Dumping phase
176
177 Address allocation
178 ------------------
179
180    The next step is to allocate the offsets of each of the objects in
181 the final dump file.  This is done by `pdump_allocate_offset()' which
182 is called indirectly by `pdump_scan_by_alignment()'.
183
184    The strategy to deal with alignment problems uses these facts:
185
186   1. real world alignment requirements are powers of two.
187
188   2. the C compiler is required to adjust the size of a struct so that
189      you can have an array of them next to each other.  This means you
190      can have an upper bound of the alignment requirements of a given
191      structure by looking at which power of two its size is a multiple.
192
193   3. the non-variant part of variable size lrecords has an alignment
194      requirement of 4.
195
196    Hence, for each lrecord type, C struct type or opaque data block the
197 alignment requirement is computed as a power of two, with a minimum of
198 2^2 for lrecords.  `pdump_scan_by_alignment()' then scans all the
199 `pdump_entry_list_elmt''s, the ones with the highest requirements
200 first.  This ensures the best packing.
201
202    The maximum alignment requirement we take into account is 2^8.
203
204    `pdump_allocate_offset()' only has to do a linear allocation,
205 starting at offset 256 (this leaves room for the header and keeps the
206 alignments happy).
207
208 \1f
209 File: internals.info,  Node: The header,  Next: Data dumping,  Prev: Address allocation,  Up: Dumping phase
210
211 The header
212 ----------
213
214    The next step creates the file and writes a header with a signature
215 and some random information in it.  The `reloc_address' field, which
216 indicates at which address the file should be loaded if we want to avoid
217 post-reload relocation, is set to 0.  It then seeks to offset 256 (base
218 offset for the objects).
219
220 \1f
221 File: internals.info,  Node: Data dumping,  Next: Pointers dumping,  Prev: The header,  Up: Dumping phase
222
223 Data dumping
224 ------------
225
226    The data is dumped in the same order as the addresses were allocated
227 by `pdump_dump_data()', called from `pdump_scan_by_alignment()'.  This
228 function copies the data to a temporary buffer, relocates all pointers
229 in the object to the addresses allocated in step Address Allocation,
230 and writes it to the file.  Using the same order means that, if we are
231 careful with lrecords whose size is not a multiple of 4, we are ensured
232 that the object is always written at the offset in the file allocated
233 in step Address Allocation.
234
235 \1f
236 File: internals.info,  Node: Pointers dumping,  Prev: Data dumping,  Up: Dumping phase
237
238 Pointers dumping
239 ----------------
240
241    A bunch of tables needed to reassign properly the global pointers are
242 then written.  They are:
243
244   1. the pdump_root_struct_ptrs dynarr
245
246   2. the pdump_opaques dynarr
247
248   3. a vector of all the offsets to the objects in the file that
249      include a description (for faster relocation at reload time)
250
251   4. the pdump_root_objects and pdump_weak_object_chains dynarrs.
252
253    For each of the dynarrs we write both the pointer to the variables
254 and the relocated offset of the object they point to.  Since these
255 variables are global, the pointers are still valid when restarting the
256 program and are used to regenerate the global pointers.
257
258    The `pdump_weak_object_chains' dynarr is a special case.  The
259 variables it points to are the head of weak linked lists of lisp objects
260 of the same type.  Not all objects of this list are dumped so the
261 relocated pointer we associate with them points to the first dumped
262 object of the list, or Qnil if none is available.  This is also the
263 reason why they are not used as roots for the purpose of object
264 enumeration.
265
266    Some very important information like the `staticpros' and
267 `lrecord_implementations_table' are handled indirectly using
268 `dump_add_opaque' or `dump_add_root_struct_ptr'.
269
270    This is the end of the dumping part.
271
272 \1f
273 File: internals.info,  Node: Reloading phase,  Next: Remaining issues,  Prev: Dumping phase,  Up: Dumping
274
275 Reloading phase
276 ===============
277
278 File loading
279 ------------
280
281    The file is mmap'ed in memory (which ensures a PAGESIZE alignment, at
282 least 4096), or if mmap is unavailable or fails, a 256-bytes aligned
283 malloc is done and the file is loaded.
284
285    Some variables are reinitialized from the values found in the header.
286
287    The difference between the actual loading address and the
288 reloc_address is computed and will be used for all the relocations.
289
290 Putting back the pdump_opaques
291 ------------------------------
292
293    The memory contents are restored in the obvious and trivial way.
294
295 Putting back the pdump_root_struct_ptrs
296 ---------------------------------------
297
298    The variables pointed to by pdump_root_struct_ptrs in the dump phase
299 are reset to the right relocated object addresses.
300
301 Object relocation
302 -----------------
303
304    All the objects are relocated using their description and their
305 offset by `pdump_reloc_one'.  This step is unnecessary if the
306 reloc_address is equal to the file loading address.
307
308 Putting back the pdump_root_objects and pdump_weak_object_chains
309 ----------------------------------------------------------------
310
311    Same as Putting back the pdump_root_struct_ptrs.
312
313 Reorganize the hash tables
314 --------------------------
315
316    Since some of the hash values in the lisp hash tables are
317 address-dependent, their layout is now wrong.  So we go through each of
318 them and have them resorted by calling `pdump_reorganize_hash_table'.
319
320 \1f
321 File: internals.info,  Node: Remaining issues,  Prev: Reloading phase,  Up: Dumping
322
323 Remaining issues
324 ================
325
326    The build process will have to start a post-dump xemacs, ask it the
327 loading address (which will, hopefully, be always the same between
328 different xemacs invocations) and relocate the file to the new address.
329 This way the object relocation phase will not have to be done, which
330 means no writes in the objects and that, because of the use of mmap, the
331 dumped data will be shared between all the xemacs running on the
332 computer.
333
334    Some executable signature will be necessary to ensure that a given
335 dump file is really associated with a given executable, or random
336 crashes will occur.  Maybe a random number set at compile or configure
337 time thru a define.  This will also allow for having
338 differently-compiled xemacsen on the same system (mule and no-mule
339 comes to mind).
340
341    The DOC file contents should probably end up in the dump file.
342
343 \1f
344 File: internals.info,  Node: Events and the Event Loop,  Next: Evaluation; Stack Frames; Bindings,  Prev: Dumping,  Up: Top
345
346 Events and the Event Loop
347 *************************
348
349 * Menu:
350
351 * Introduction to Events::
352 * Main Loop::
353 * Specifics of the Event Gathering Mechanism::
354 * Specifics About the Emacs Event::
355 * The Event Stream Callback Routines::
356 * Other Event Loop Functions::
357 * Converting Events::
358 * Dispatching Events; The Command Builder::
359
360 \1f
361 File: internals.info,  Node: Introduction to Events,  Next: Main Loop,  Up: Events and the Event Loop
362
363 Introduction to Events
364 ======================
365
366    An event is an object that encapsulates information about an
367 interesting occurrence in the operating system.  Events are generated
368 either by user action, direct (e.g. typing on the keyboard or moving
369 the mouse) or indirect (moving another window, thereby generating an
370 expose event on an Emacs frame), or as a result of some other typically
371 asynchronous action happening, such as output from a subprocess being
372 ready or a timer expiring.  Events come into the system in an
373 asynchronous fashion (typically through a callback being called) and
374 are converted into a synchronous event queue (first-in, first-out) in a
375 process that we will call "collection".
376
377    Note that each application has its own event queue. (It is
378 immaterial whether the collection process directly puts the events in
379 the proper application's queue, or puts them into a single system
380 queue, which is later split up.)
381
382    The most basic level of event collection is done by the operating
383 system or window system.  Typically, XEmacs does its own event
384 collection as well.  Often there are multiple layers of collection in
385 XEmacs, with events from various sources being collected into a queue,
386 which is then combined with other sources to go into another queue
387 (i.e. a second level of collection), with perhaps another level on top
388 of this, etc.
389
390    XEmacs has its own types of events (called "Emacs events"), which
391 provides an abstract layer on top of the system-dependent nature of the
392 most basic events that are received.  Part of the complex nature of the
393 XEmacs event collection process involves converting from the
394 operating-system events into the proper Emacs events--there may not be
395 a one-to-one correspondence.
396
397    Emacs events are documented in `events.h'; I'll discuss them later.
398
399 \1f
400 File: internals.info,  Node: Main Loop,  Next: Specifics of the Event Gathering Mechanism,  Prev: Introduction to Events,  Up: Events and the Event Loop
401
402 Main Loop
403 =========
404
405    The "command loop" is the top-level loop that the editor is always
406 running.  It loops endlessly, calling `next-event' to retrieve an event
407 and `dispatch-event' to execute it. `dispatch-event' does the
408 appropriate thing with non-user events (process, timeout, magic, eval,
409 mouse motion); this involves calling a Lisp handler function, redrawing
410 a newly-exposed part of a frame, reading subprocess output, etc.  For
411 user events, `dispatch-event' looks up the event in relevant keymaps or
412 menubars; when a full key sequence or menubar selection is reached, the
413 appropriate function is executed. `dispatch-event' may have to keep
414 state across calls; this is done in the "command-builder" structure
415 associated with each console (remember, there's usually only one
416 console), and the engine that looks up keystrokes and constructs full
417 key sequences is called the "command builder".  This is documented
418 elsewhere.
419
420    The guts of the command loop are in `command_loop_1()'.  This
421 function doesn't catch errors, though--that's the job of
422 `command_loop_2()', which is a condition-case (i.e. error-trapping)
423 wrapper around `command_loop_1()'.  `command_loop_1()' never returns,
424 but may get thrown out of.
425
426    When an error occurs, `cmd_error()' is called, which usually invokes
427 the Lisp error handler in `command-error'; however, a default error
428 handler is provided if `command-error' is `nil' (e.g. during startup).
429 The purpose of the error handler is simply to display the error message
430 and do associated cleanup; it does not need to throw anywhere.  When
431 the error handler finishes, the condition-case in `command_loop_2()'
432 will finish and `command_loop_2()' will reinvoke `command_loop_1()'.
433
434    `command_loop_2()' is invoked from three places: from
435 `initial_command_loop()' (called from `main()' at the end of internal
436 initialization), from the Lisp function `recursive-edit', and from
437 `call_command_loop()'.
438
439    `call_command_loop()' is called when a macro is started and when the
440 minibuffer is entered; normal termination of the macro or minibuffer
441 causes a throw out of the recursive command loop. (To
442 `execute-kbd-macro' for macros and `exit' for minibuffers.  Note also
443 that the low-level minibuffer-entering function,
444 `read-minibuffer-internal', provides its own error handling and does
445 not need `command_loop_2()''s error encapsulation; so it tells
446 `call_command_loop()' to invoke `command_loop_1()' directly.)
447
448    Note that both read-minibuffer-internal and recursive-edit set up a
449 catch for `exit'; this is why `abort-recursive-edit', which throws to
450 this catch, exits out of either one.
451
452    `initial_command_loop()', called from `main()', sets up a catch for
453 `top-level' when invoking `command_loop_2()', allowing functions to
454 throw all the way to the top level if they really need to.  Before
455 invoking `command_loop_2()', `initial_command_loop()' calls
456 `top_level_1()', which handles all of the startup stuff (creating the
457 initial frame, handling the command-line options, loading the user's
458 `.emacs' file, etc.).  The function that actually does this is in Lisp
459 and is pointed to by the variable `top-level'; normally this function is
460 `normal-top-level'.  `top_level_1()' is just an error-handling wrapper
461 similar to `command_loop_2()'.  Note also that `initial_command_loop()'
462 sets up a catch for `top-level' when invoking `top_level_1()', just
463 like when it invokes `command_loop_2()'.
464
465 \1f
466 File: internals.info,  Node: Specifics of the Event Gathering Mechanism,  Next: Specifics About the Emacs Event,  Prev: Main Loop,  Up: Events and the Event Loop
467
468 Specifics of the Event Gathering Mechanism
469 ==========================================
470
471    Here is an approximate diagram of the collection processes at work
472 in XEmacs, under TTY's (TTY's are simpler than X so we'll look at this
473 first):
474
475       asynch.      asynch.    asynch.   asynch.             [Collectors in
476      kbd events  kbd events   process   process                the OS]
477            |         |         output    output
478            |         |           |         |
479            |         |           |         |      SIGINT,   [signal handlers
480            |         |           |         |      SIGQUIT,     in XEmacs]
481            V         V           V         V      SIGWINCH,
482           file      file        file      file    SIGALRM
483           desc.     desc.       desc.     desc.     |
484           (TTY)     (TTY)       (pipe)    (pipe)    |
485            |          |          |         |      fake    timeouts
486            |          |          |         |      file        |
487            |          |          |         |      desc.       |
488            |          |          |         |      (pipe)      |
489            |          |          |         |        |         |
490            |          |          |         |        |         |
491            |          |          |         |        |         |
492            V          V          V         V        V         V
493            ------>-----------<----------------<----------------
494                        |
495                        |
496                        | [collected using select() in emacs_tty_next_event()
497                        |  and converted to the appropriate Emacs event]
498                        |
499                        |
500                        V          (above this line is TTY-specific)
501                      Emacs -----------------------------------------------
502                      event (below this line is the generic event mechanism)
503                        |
504                        |
505      was there     if not, call
506      a SIGINT?  emacs_tty_next_event()
507          |             |
508          |             |
509          |             |
510          V             V
511          --->------<----
512                 |
513                 |     [collected in event_stream_next_event();
514                 |      SIGINT is converted using maybe_read_quit_event()]
515                 V
516               Emacs
517               event
518                 |
519                 \---->------>----- maybe_kbd_translate() ---->---\
520                                                                  |
521                                                                  |
522                                                                  |
523           command event queue                                    |
524                                                     if not from command
525        (contains events that were                   event queue, call
526        read earlier but not processed,              event_stream_next_event()
527        typically when waiting in a                               |
528        sit-for, sleep-for, etc. for                              |
529       a particular event to be received)                         |
530                     |                                            |
531                     |                                            |
532                     V                                            V
533                     ---->------------------------------------<----
534                                                     |
535                                                     | [collected in
536                                                     |  next_event_internal()]
537                                                     |
538       unread-     unread-       event from          |
539       command-    command-       keyboard       else, call
540       events      event           macro      next_event_internal()
541         |           |               |               |
542         |           |               |               |
543         |           |               |               |
544         V           V               V               V
545         --------->----------------------<------------
546                           |
547                           |      [collected in `next-event', which may loop
548                           |       more than once if the event it gets is on
549                           |       a dead frame, device, etc.]
550                           |
551                           |
552                           V
553                  feed into top-level event loop,
554                  which repeatedly calls `next-event'
555                  and then dispatches the event
556                  using `dispatch-event'
557
558    Notice the separation between TTY-specific and generic event
559 mechanism.  When using the Xt-based event loop, the TTY-specific stuff
560 is replaced but the rest stays the same.
561
562    It's also important to realize that only one different kind of
563 system-specific event loop can be operating at a time, and must be able
564 to receive all kinds of events simultaneously.  For the two existing
565 event loops (implemented in `event-tty.c' and `event-Xt.c',
566 respectively), the TTY event loop _only_ handles TTY consoles, while
567 the Xt event loop handles _both_ TTY and X consoles.  This situation is
568 different from all of the output handlers, where you simply have one
569 per console type.
570
571    Here's the Xt Event Loop Diagram (notice that below a certain point,
572 it's the same as the above diagram):
573
574      asynch. asynch. asynch. asynch.                 [Collectors in
575       kbd     kbd    process process                    the OS]
576      events  events  output  output
577        |       |       |       |
578        |       |       |       |     asynch. asynch. [Collectors in the
579        |       |       |       |       X        X     OS and X Window System]
580        |       |       |       |     events  events
581        |       |       |       |       |        |
582        |       |       |       |       |        |
583        |       |       |       |       |        |    SIGINT, [signal handlers
584        |       |       |       |       |        |    SIGQUIT,   in XEmacs]
585        |       |       |       |       |        |    SIGWINCH,
586        |       |       |       |       |        |    SIGALRM
587        |       |       |       |       |        |       |
588        |       |       |       |       |        |       |
589        |       |       |       |       |        |       |      timeouts
590        |       |       |       |       |        |       |          |
591        |       |       |       |       |        |       |          |
592        |       |       |       |       |        |       V          |
593        V       V       V       V       V        V      fake        |
594       file    file    file    file    file     file    file        |
595       desc.   desc.   desc.   desc.   desc.    desc.   desc.       |
596       (TTY)   (TTY)   (pipe)  (pipe) (socket) (socket) (pipe)      |
597        |       |       |       |       |        |       |          |
598        |       |       |       |       |        |       |          |
599        |       |       |       |       |        |       |          |
600        V       V       V       V       V        V       V          V
601        --->----------------------------------------<---------<------
602             |              |               |
603             |              |               |[collected using select() in
604             |              |               | _XtWaitForSomething(), called
605             |              |               | from XtAppProcessEvent(), called
606             |              |               | in emacs_Xt_next_event();
607             |              |               | dispatched to various callbacks]
608             |              |               |
609             |              |               |
610        emacs_Xt_        p_s_callback(),    | [popup_selection_callback]
611        event_handler()  x_u_v_s_callback(),| [x_update_vertical_scrollbar_
612             |           x_u_h_s_callback(),|  callback]
613             |           search_callback()  | [x_update_horizontal_scrollbar_
614             |              |               |  callback]
615             |              |               |
616             |              |               |
617        enqueue_Xt_       signal_special_   |
618        dispatch_event()  Xt_user_event()   |
619        [maybe multiple     |               |
620         times, maybe 0     |               |
621         times]             |               |
622             |            enqueue_Xt_       |
623             |            dispatch_event()  |
624             |              |               |
625             |              |               |
626             V              V               |
627             -->----------<--               |
628                    |                       |
629                    |                       |
630                 dispatch             Xt_what_callback()
631                 event                  sets flags
632                 queue                      |
633                    |                       |
634                    |                       |
635                    |                       |
636                    |                       |
637                    ---->-----------<--------
638                         |
639                         |
640                         |     [collected and converted as appropriate in
641                         |            emacs_Xt_next_event()]
642                         |
643                         |
644                         V          (above this line is Xt-specific)
645                       Emacs ------------------------------------------------
646                       event (below this line is the generic event mechanism)
647                         |
648                         |
649      was there      if not, call
650      a SIGINT?   emacs_Xt_next_event()
651          |              |
652          |              |
653          |              |
654          V              V
655          --->-------<----
656                 |
657                 |        [collected in event_stream_next_event();
658                 |         SIGINT is converted using maybe_read_quit_event()]
659                 V
660               Emacs
661               event
662                 |
663                 \---->------>----- maybe_kbd_translate() -->-----\
664                                                                  |
665                                                                  |
666                                                                  |
667           command event queue                                    |
668                                                    if not from command
669        (contains events that were                  event queue, call
670        read earlier but not processed,             event_stream_next_event()
671        typically when waiting in a                               |
672        sit-for, sleep-for, etc. for                              |
673       a particular event to be received)                         |
674                     |                                            |
675                     |                                            |
676                     V                                            V
677                     ---->----------------------------------<------
678                                                     |
679                                                     | [collected in
680                                                     |  next_event_internal()]
681                                                     |
682       unread-     unread-       event from          |
683       command-    command-       keyboard       else, call
684       events      event           macro      next_event_internal()
685         |           |               |               |
686         |           |               |               |
687         |           |               |               |
688         V           V               V               V
689         --------->----------------------<------------
690                           |
691                           |      [collected in `next-event', which may loop
692                           |       more than once if the event it gets is on
693                           |       a dead frame, device, etc.]
694                           |
695                           |
696                           V
697                  feed into top-level event loop,
698                  which repeatedly calls `next-event'
699                  and then dispatches the event
700                  using `dispatch-event'
701
702 \1f
703 File: internals.info,  Node: Specifics About the Emacs Event,  Next: The Event Stream Callback Routines,  Prev: Specifics of the Event Gathering Mechanism,  Up: Events and the Event Loop
704
705 Specifics About the Emacs Event
706 ===============================
707
708 \1f
709 File: internals.info,  Node: The Event Stream Callback Routines,  Next: Other Event Loop Functions,  Prev: Specifics About the Emacs Event,  Up: Events and the Event Loop
710
711 The Event Stream Callback Routines
712 ==================================
713
714 \1f
715 File: internals.info,  Node: Other Event Loop Functions,  Next: Converting Events,  Prev: The Event Stream Callback Routines,  Up: Events and the Event Loop
716
717 Other Event Loop Functions
718 ==========================
719
720    `detect_input_pending()' and `input-pending-p' look for input by
721 calling `event_stream->event_pending_p' and looking in
722 `[V]unread-command-event' and the `command_event_queue' (they do not
723 check for an executing keyboard macro, though).
724
725    `discard-input' cancels any command events pending (and any keyboard
726 macros currently executing), and puts the others onto the
727 `command_event_queue'.  There is a comment about a "race condition",
728 which is not a good sign.
729
730    `next-command-event' and `read-char' are higher-level interfaces to
731 `next-event'.  `next-command-event' gets the next "command" event (i.e.
732 keypress, mouse event, menu selection, or scrollbar action), calling
733 `dispatch-event' on any others.  `read-char' calls `next-command-event'
734 and uses `event_to_character()' to return the character equivalent.
735 With the right kind of input method support, it is possible for
736 (read-char) to return a Kanji character.
737
738 \1f
739 File: internals.info,  Node: Converting Events,  Next: Dispatching Events; The Command Builder,  Prev: Other Event Loop Functions,  Up: Events and the Event Loop
740
741 Converting Events
742 =================
743
744    `character_to_event()', `event_to_character()',
745 `event-to-character', and `character-to-event' convert between
746 characters and keypress events corresponding to the characters.  If the
747 event was not a keypress, `event_to_character()' returns -1 and
748 `event-to-character' returns `nil'.  These functions convert between
749 character representation and the split-up event representation (keysym
750 plus mod keys).
751
752 \1f
753 File: internals.info,  Node: Dispatching Events; The Command Builder,  Prev: Converting Events,  Up: Events and the Event Loop
754
755 Dispatching Events; The Command Builder
756 =======================================
757
758    Not yet documented.
759
760 \1f
761 File: internals.info,  Node: Evaluation; Stack Frames; Bindings,  Next: Symbols and Variables,  Prev: Events and the Event Loop,  Up: Top
762
763 Evaluation; Stack Frames; Bindings
764 **********************************
765
766 * Menu:
767
768 * Evaluation::
769 * Dynamic Binding; The specbinding Stack; Unwind-Protects::
770 * Simple Special Forms::
771 * Catch and Throw::
772
773 \1f
774 File: internals.info,  Node: Evaluation,  Next: Dynamic Binding; The specbinding Stack; Unwind-Protects,  Up: Evaluation; Stack Frames; Bindings
775
776 Evaluation
777 ==========
778
779    `Feval()' evaluates the form (a Lisp object) that is passed to it.
780 Note that evaluation is only non-trivial for two types of objects:
781 symbols and conses.  A symbol is evaluated simply by calling
782 `symbol-value' on it and returning the value.
783
784    Evaluating a cons means calling a function.  First, `eval' checks to
785 see if garbage-collection is necessary, and calls `garbage_collect_1()'
786 if so.  It then increases the evaluation depth by 1 (`lisp_eval_depth',
787 which is always less than `max_lisp_eval_depth') and adds an element to
788 the linked list of `struct backtrace''s (`backtrace_list').  Each such
789 structure contains a pointer to the function being called plus a list
790 of the function's arguments.  Originally these values are stored
791 unevalled, and as they are evaluated, the backtrace structure is
792 updated.  Garbage collection pays attention to the objects pointed to
793 in the backtrace structures (garbage collection might happen while a
794 function is being called or while an argument is being evaluated, and
795 there could easily be no other references to the arguments in the
796 argument list; once an argument is evaluated, however, the unevalled
797 version is not needed by eval, and so the backtrace structure is
798 changed).
799
800    At this point, the function to be called is determined by looking at
801 the car of the cons (if this is a symbol, its function definition is
802 retrieved and the process repeated).  The function should then consist
803 of either a `Lisp_Subr' (built-in function written in C), a
804 `Lisp_Compiled_Function' object, or a cons whose car is one of the
805 symbols `autoload', `macro' or `lambda'.
806
807    If the function is a `Lisp_Subr', the lisp object points to a
808 `struct Lisp_Subr' (created by `DEFUN()'), which contains a pointer to
809 the C function, a minimum and maximum number of arguments (or possibly
810 the special constants `MANY' or `UNEVALLED'), a pointer to the symbol
811 referring to that subr, and a couple of other things.  If the subr
812 wants its arguments `UNEVALLED', they are passed raw as a list.
813 Otherwise, an array of evaluated arguments is created and put into the
814 backtrace structure, and either passed whole (`MANY') or each argument
815 is passed as a C argument.
816
817    If the function is a `Lisp_Compiled_Function',
818 `funcall_compiled_function()' is called.  If the function is a lambda
819 list, `funcall_lambda()' is called.  If the function is a macro, [.....
820 fill in] is done.  If the function is an autoload, `do_autoload()' is
821 called to load the definition and then eval starts over [explain this
822 more].
823
824    When `Feval()' exits, the evaluation depth is reduced by one, the
825 debugger is called if appropriate, and the current backtrace structure
826 is removed from the list.
827
828    Both `funcall_compiled_function()' and `funcall_lambda()' need to go
829 through the list of formal parameters to the function and bind them to
830 the actual arguments, checking for `&rest' and `&optional' symbols in
831 the formal parameters and making sure the number of actual arguments is
832 correct.  `funcall_compiled_function()' can do this a little more
833 efficiently, since the formal parameter list can be checked for sanity
834 when the compiled function object is created.
835
836    `funcall_lambda()' simply calls `Fprogn' to execute the code in the
837 lambda list.
838
839    `funcall_compiled_function()' calls the real byte-code interpreter
840 `execute_optimized_program()' on the byte-code instructions, which are
841 converted into an internal form for faster execution.
842
843    When a compiled function is executed for the first time by
844 `funcall_compiled_function()', or during the dump phase of building
845 XEmacs, the byte-code instructions are converted from a `Lisp_String'
846 (which is inefficient to access, especially in the presence of MULE)
847 into a `Lisp_Opaque' object containing an array of unsigned char, which
848 can be directly executed by the byte-code interpreter.  At this time
849 the byte code is also analyzed for validity and transformed into a more
850 optimized form, so that `execute_optimized_program()' can really fly.
851
852    Here are some of the optimizations performed by the internal
853 byte-code transformer:
854   1. References to the `constants' array are checked for out-of-range
855      indices, so that the byte interpreter doesn't have to.
856
857   2. References to the `constants' array that will be used as a Lisp
858      variable are checked for being correct non-constant (i.e. not `t',
859      `nil', or `keywordp') symbols, so that the byte interpreter
860      doesn't have to.
861
862   3. The maximum number of variable bindings in the byte-code is
863      pre-computed, so that space on the `specpdl' stack can be
864      pre-reserved once for the whole function execution.
865
866   4. All byte-code jumps are relative to the current program counter
867      instead of the start of the program, thereby saving a register.
868
869   5. One-byte relative jumps are converted from the byte-code form of
870      unsigned chars offset by 127 to machine-friendly signed chars.
871
872    Of course, this transformation of the `instructions' should not be
873 visible to the user, so `Fcompiled_function_instructions()' needs to
874 know how to convert the optimized opaque object back into a Lisp string
875 that is identical to the original string from the `.elc' file.
876 (Actually, the resulting string may (rarely) contain slightly
877 different, yet equivalent, byte code.)
878
879    `Ffuncall()' implements Lisp `funcall'.  `(funcall fun x1 x2 x3
880 ...)' is equivalent to `(eval (list fun (quote x1) (quote x2) (quote
881 x3) ...))'.  `Ffuncall()' contains its own code to do the evaluation,
882 however, and is very similar to `Feval()'.
883
884    From the performance point of view, it is worth knowing that most of
885 the time in Lisp evaluation is spent executing `Lisp_Subr' and
886 `Lisp_Compiled_Function' objects via `Ffuncall()' (not `Feval()').
887
888    `Fapply()' implements Lisp `apply', which is very similar to
889 `funcall' except that if the last argument is a list, the result is the
890 same as if each of the arguments in the list had been passed separately.
891 `Fapply()' does some business to expand the last argument if it's a
892 list, then calls `Ffuncall()' to do the work.
893
894    `apply1()', `call0()', `call1()', `call2()', and `call3()' call a
895 function, passing it the argument(s) given (the arguments are given as
896 separate C arguments rather than being passed as an array).  `apply1()'
897 uses `Fapply()' while the others use `Ffuncall()' to do the real work.
898
899 \1f
900 File: internals.info,  Node: Dynamic Binding; The specbinding Stack; Unwind-Protects,  Next: Simple Special Forms,  Prev: Evaluation,  Up: Evaluation; Stack Frames; Bindings
901
902 Dynamic Binding; The specbinding Stack; Unwind-Protects
903 =======================================================
904
905      struct specbinding
906      {
907        Lisp_Object symbol;
908        Lisp_Object old_value;
909        Lisp_Object (*func) (Lisp_Object); /* for unwind-protect */
910      };
911
912    `struct specbinding' is used for local-variable bindings and
913 unwind-protects.  `specpdl' holds an array of `struct specbinding''s,
914 `specpdl_ptr' points to the beginning of the free bindings in the
915 array, `specpdl_size' specifies the total number of binding slots in
916 the array, and `max_specpdl_size' specifies the maximum number of
917 bindings the array can be expanded to hold.  `grow_specpdl()' increases
918 the size of the `specpdl' array, multiplying its size by 2 but never
919 exceeding `max_specpdl_size' (except that if this number is less than
920 400, it is first set to 400).
921
922    `specbind()' binds a symbol to a value and is used for local
923 variables and `let' forms.  The symbol and its old value (which might
924 be `Qunbound', indicating no prior value) are recorded in the specpdl
925 array, and `specpdl_size' is increased by 1.
926
927    `record_unwind_protect()' implements an "unwind-protect", which,
928 when placed around a section of code, ensures that some specified
929 cleanup routine will be executed even if the code exits abnormally
930 (e.g. through a `throw' or quit).  `record_unwind_protect()' simply
931 adds a new specbinding to the `specpdl' array and stores the
932 appropriate information in it.  The cleanup routine can either be a C
933 function, which is stored in the `func' field, or a `progn' form, which
934 is stored in the `old_value' field.
935
936    `unbind_to()' removes specbindings from the `specpdl' array until
937 the specified position is reached.  Each specbinding can be one of
938 three types:
939
940   1. an unwind-protect with a C cleanup function (`func' is not 0, and
941      `old_value' holds an argument to be passed to the function);
942
943   2. an unwind-protect with a Lisp form (`func' is 0, `symbol' is
944      `nil', and `old_value' holds the form to be executed with
945      `Fprogn()'); or
946
947   3. a local-variable binding (`func' is 0, `symbol' is not `nil', and
948      `old_value' holds the old value, which is stored as the symbol's
949      value).
950
951 \1f
952 File: internals.info,  Node: Simple Special Forms,  Next: Catch and Throw,  Prev: Dynamic Binding; The specbinding Stack; Unwind-Protects,  Up: Evaluation; Stack Frames; Bindings
953
954 Simple Special Forms
955 ====================
956
957    `or', `and', `if', `cond', `progn', `prog1', `prog2', `setq',
958 `quote', `function', `let*', `let', `while'
959
960    All of these are very simple and work as expected, calling `Feval()'
961 or `Fprogn()' as necessary and (in the case of `let' and `let*') using
962 `specbind()' to create bindings and `unbind_to()' to undo the bindings
963 when finished.
964
965    Note that, with the exception of `Fprogn', these functions are
966 typically called in real life only in interpreted code, since the byte
967 compiler knows how to convert calls to these functions directly into
968 byte code.
969
970 \1f
971 File: internals.info,  Node: Catch and Throw,  Prev: Simple Special Forms,  Up: Evaluation; Stack Frames; Bindings
972
973 Catch and Throw
974 ===============
975
976      struct catchtag
977      {
978        Lisp_Object tag;
979        Lisp_Object val;
980        struct catchtag *next;
981        struct gcpro *gcpro;
982        jmp_buf jmp;
983        struct backtrace *backlist;
984        int lisp_eval_depth;
985        int pdlcount;
986      };
987
988    `catch' is a Lisp function that places a catch around a body of
989 code.  A catch is a means of non-local exit from the code.  When a catch
990 is created, a tag is specified, and executing a `throw' to this tag
991 will exit from the body of code caught with this tag, and its value will
992 be the value given in the call to `throw'.  If there is no such call,
993 the code will be executed normally.
994
995    Information pertaining to a catch is held in a `struct catchtag',
996 which is placed at the head of a linked list pointed to by `catchlist'.
997 `internal_catch()' is passed a C function to call (`Fprogn()' when
998 Lisp `catch' is called) and arguments to give it, and places a catch
999 around the function.  Each `struct catchtag' is held in the stack frame
1000 of the `internal_catch()' instance that created the catch.
1001
1002    `internal_catch()' is fairly straightforward.  It stores into the
1003 `struct catchtag' the tag name and the current values of
1004 `backtrace_list', `lisp_eval_depth', `gcprolist', and the offset into
1005 the `specpdl' array, sets a jump point with `_setjmp()' (storing the
1006 jump point into the `struct catchtag'), and calls the function.
1007 Control will return to `internal_catch()' either when the function
1008 exits normally or through a `_longjmp()' to this jump point.  In the
1009 latter case, `throw' will store the value to be returned into the
1010 `struct catchtag' before jumping.  When it's done, `internal_catch()'
1011 removes the `struct catchtag' from the catchlist and returns the proper
1012 value.
1013
1014    `Fthrow()' goes up through the catchlist until it finds one with a
1015 matching tag.  It then calls `unbind_catch()' to restore everything to
1016 what it was when the appropriate catch was set, stores the return value
1017 in the `struct catchtag', and jumps (with `_longjmp()') to its jump
1018 point.
1019
1020    `unbind_catch()' removes all catches from the catchlist until it
1021 finds the correct one.  Some of the catches might have been placed for
1022 error-trapping, and if so, the appropriate entries on the handlerlist
1023 must be removed (see "errors").  `unbind_catch()' also restores the
1024 values of `gcprolist', `backtrace_list', and `lisp_eval', and calls
1025 `unbind_to()' to undo any specbindings created since the catch.
1026
1027 \1f
1028 File: internals.info,  Node: Symbols and Variables,  Next: Buffers and Textual Representation,  Prev: Evaluation; Stack Frames; Bindings,  Up: Top
1029
1030 Symbols and Variables
1031 *********************
1032
1033 * Menu:
1034
1035 * Introduction to Symbols::
1036 * Obarrays::
1037 * Symbol Values::
1038
1039 \1f
1040 File: internals.info,  Node: Introduction to Symbols,  Next: Obarrays,  Up: Symbols and Variables
1041
1042 Introduction to Symbols
1043 =======================
1044
1045    A symbol is basically just an object with four fields: a name (a
1046 string), a value (some Lisp object), a function (some Lisp object), and
1047 a property list (usually a list of alternating keyword/value pairs).
1048 What makes symbols special is that there is usually only one symbol with
1049 a given name, and the symbol is referred to by name.  This makes a
1050 symbol a convenient way of calling up data by name, i.e. of implementing
1051 variables. (The variable's value is stored in the "value slot".)
1052 Similarly, functions are referenced by name, and the definition of the
1053 function is stored in a symbol's "function slot".  This means that
1054 there can be a distinct function and variable with the same name.  The
1055 property list is used as a more general mechanism of associating
1056 additional values with particular names, and once again the namespace is
1057 independent of the function and variable namespaces.
1058