home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / DSTACK.H < prev    next >
C/C++ Source or Header  |  1994-07-27  |  8KB  |  205 lines

  1. /* Copyright (C) 1992, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* dstack.h */
  20. /* Definitions for the dictionary stack */
  21.  
  22. #include "istack.h"
  23.  
  24. /* Define the dictionary stack and systemdict. */
  25. extern ref_stack d_stack;
  26. extern ref ref_systemdict;
  27. #define systemdict (&ref_systemdict)
  28.  
  29. /* Define the dictionary stack pointers. */
  30. typedef s_ptr ds_ptr;
  31. typedef const_s_ptr const_ds_ptr;
  32. #define dsbot (d_stack.bot)
  33. #define dsp (d_stack.p)
  34. #define dstop (d_stack.top)
  35.  
  36. /* Macro to ensure enough room on the dictionary stack */
  37. #define check_dstack(n)\
  38.   if ( dstop - dsp < (n) )\
  39.     { d_stack.requested = (n); return_error(e_dictstackoverflow); }
  40.  
  41. /*
  42.  * Switching between Level 1 and Level 2 involves inserting and removing
  43.  * globaldict on the dictionary stack.  Instead of truly inserting and
  44.  * removing entries, we replace globaldict by a copy of systemdict in
  45.  * Level 1 mode.  min_dstack_size, the minimum number of entries, does not
  46.  * change depending on language level; the countdictstack and dictstack
  47.  * operators must take this into account.
  48.  */
  49. extern uint min_dstack_size;
  50.  
  51. /*
  52.  * The dictionary stack is implemented as a linked list of blocks;
  53.  * operators that access the entire d-stack must take this into account.
  54.  * These are:
  55.  *    countdictstack  dictstack
  56.  * In addition, name lookup requires searching the entire stack, not just
  57.  * the top block, and the underflow check for the dictionary stack
  58.  * (`end' operator) is not just a check for underflowing the top block.
  59.  */
  60.  
  61. /*
  62.  * Cache a value for fast checking of def operations.
  63.  * If the top entry on the dictionary stack is a writable local dictionary,
  64.  * dsmask = 0; if it is a writable global dictionary, dsmask = a_local;
  65.  * if it is a non-writable dictionary, dsmask = -1.  Then def is legal
  66.  * precisely if (r_type_attrs(pvalue) & dsmask) == 0.
  67.  */
  68. extern uint dsmask; 
  69. /*
  70.  * Cache values for fast name lookup.  If the top entry on the dictionary
  71.  * stack is a readable dictionary with packed keys, dtop_keys, dtop_npairs,
  72.  * and dtop_values are keys.value.packed, npairs, and values.value.refs
  73.  * for that dictionary; otherwise, these variables point to a dummy
  74.  * empty dictionary.
  75.  */
  76. extern const ref_packed *dtop_keys;
  77. extern uint dtop_npairs;
  78. extern ref *dtop_values;
  79. /*
  80.  * Reset the cached top values.  Every routine that alters the
  81.  * dictionary stack (including changing the protection or size of the
  82.  * top dictionary on the stack) must call this.
  83.  */
  84. void    dict_set_top(P0());
  85.  
  86. /*
  87.  * Define a special fast entry for name lookup in the interpreter.
  88.  * The key is known to be a name; search the entire dict stack.
  89.  * Return the pointer to the value slot.
  90.  * If the name isn't found, just return 0.
  91.  */
  92. ref *    dict_find_name_by_index(P1(uint nidx));
  93. #define dict_find_name(pnref) dict_find_name_by_index(name_index(pnref))
  94.  
  95. /* Define some auxiliary macros needed for inline code. */
  96.  
  97. #define dict_round_size (!arch_ints_are_short)
  98. #if dict_round_size
  99. #  define hash_mod(hash, size) ((hash) & ((size) - 1))
  100. #else
  101. #  define hash_mod(hash, size) ((hash) % (size))
  102. #endif
  103.  
  104. /* Define the hashing function for names. */
  105. /* We don't have to scramble the index, because */
  106. /* indices are assigned in a scattered order (see name_ref in iname.c). */
  107. #define dict_name_index_hash(nidx) (nidx)
  108.  
  109. /*
  110.  * Define an extra-fast macro for name lookup, optimized for
  111.  * a single-probe lookup in the top dictionary on the stack.
  112.  * Amazingly enough, this seems to hit over 90% of the time
  113.  * (aside from operators, of course, which are handled either with
  114.  * the special cache pointer or with 'bind').
  115.  */
  116. #define dict_find_name_by_index_inline(nidx,htemp)\
  117.   (dtop_keys[htemp = hash_mod(dict_name_index_hash(nidx),\
  118.      dtop_npairs) + 1] == pt_tag(pt_literal_name) + (nidx) ?\
  119.    dtop_values + htemp : dict_find_name_by_index(nidx))
  120. /*
  121.  * Define a similar macro that only checks the top dictionary on the stack.
  122.  */
  123. #define if_dict_find_name_by_index_top(nidx,htemp,pvslot)\
  124.   if ( ((dtop_keys[htemp = hash_mod(dict_name_index_hash(nidx),\
  125.      dtop_npairs) + 1] == pt_tag(pt_literal_name) + (nidx)) ?\
  126.     ((pvslot) = dtop_values + (htemp), 1) :\
  127.     0)\
  128.      ) 
  129.  
  130. /*
  131. Notes on "shallow binding".
  132.  
  133. We mark heavily used operations with a * below; moderately heavily used
  134. operations with a +.
  135.  
  136. The following operations change the dictionary stack:
  137.     +begin, +end
  138.     readonly (on a dictionary that is on the stack)
  139.     noaccess (on a dictionary that is on the stack)
  140. We implement cleardictstack as a series of ends.
  141.  
  142. The following operations change the contents of dictionaries:
  143.     *def, +put
  144.     undef
  145.     restore
  146.     .setmaxlength
  147. We implement store in PostScript, and copy as a series of puts.  Many
  148. other operators also do puts (e.g., ScaleMatrix in makefont,
  149. Implementation in makepattern, ...).  Note that put can do an implicit
  150. .setmaxlength (if it has to grow the dictionary).
  151.  
  152. The following operations look up keys on the dictionary stack:
  153.     *(interpreter name lookup)
  154.     load
  155.     where
  156.  
  157. We implement shallow binding with a pointer in each name that points to
  158. the value slot that holds the name's definition.  If the name is
  159. undefined, or if we don't know where the slot is, the binding pointer
  160. points to a ref with a special type t__invalid, which cannot occur
  161. anywhere else.  "Clearing" the pointer means setting it to point to this
  162. ref.
  163.  
  164. We also maintain a pair of pointers that bracket the value region of the
  165. top dictionary on the stack, for fast checking in def.  If the top
  166. dictionary is readonly or noaccess, the pointers designate an empty area.
  167. We call this the "def region" cache.
  168.  
  169. We implement the above operations as follows:
  170.     begin - push the dictionary on the stack; set the pointers of
  171.         all name keys to point to the corresponding value slots.
  172.     end - pop the stack; clear the pointers of all name keys.
  173.     readonly - if the dictionary is the top one on the stack,
  174.         reset the def region cache.
  175.     noaccess - clear the pointers of all name keys.  (This is overly
  176.         conservative, but this is a very rare operation.)
  177.         Also reset the def region cache if the dictionary is
  178.         the top one on the stack.
  179.     def - if the key is a name and its pointer points within the cached
  180.         def region, store the value through the pointer; otherwise,
  181.         look up the key in the top dictionary, store the value,
  182.         and if the key is a name, set its pointer to the value slot.
  183.     put - if the key is a name and wasn't in the dictionary before,
  184.         clear its pointer.  (Conservative, but rare.)
  185.     undef - if the key is a name, clear its pointer.  (Overly
  186.         conservative, but rare.)
  187.     restore - if either the old or the new value of a change is a name
  188.         (possibly in a packed array), clear its pointer.  This is
  189.         conservative, but easy to detect, and probably not *too*
  190.         conservative.
  191.     .setmaxlength - clear all the pointers, like noaccess.
  192.     (name lookup) - fetch the value through the pointer and dispatch
  193.         on its type; if the type is t__invalid, do a full search
  194.         and set the pointer.  This avoids a separate check for a
  195.         clear pointer in the usual case where the pointer is valid.
  196.     load - if the pointer is clear, do a search and set the pointer;
  197.         then fetch the value.
  198.     where - always do a full search and set the pointer.
  199.         (Conservative, but rare.)
  200.  
  201. One place where shallow binding will result in major new overhead is the
  202. extra push of systemdict for loading fonts.  This probably isn't a problem
  203. in real life.
  204. */
  205.