xref: /plan9/sys/src/cmd/gs/src/estack.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
17dd7cddfSDavid du Colombier /* Copyright (C) 1989, 1992, 1993, 1994, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
27dd7cddfSDavid du Colombier 
3*593dc095SDavid du Colombier   This software is provided AS-IS with no warranty, either express or
4*593dc095SDavid du Colombier   implied.
57dd7cddfSDavid du Colombier 
6*593dc095SDavid du Colombier   This software is distributed under license and may not be copied,
7*593dc095SDavid du Colombier   modified or distributed except as expressly authorized under the terms
8*593dc095SDavid du Colombier   of the license contained in the file LICENSE in this distribution.
97dd7cddfSDavid du Colombier 
10*593dc095SDavid du Colombier   For more information about licensing, please refer to
11*593dc095SDavid du Colombier   http://www.ghostscript.com/licensing/. For information on
12*593dc095SDavid du Colombier   commercial licensing, go to http://www.artifex.com/licensing/ or
13*593dc095SDavid du Colombier   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14*593dc095SDavid du Colombier   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
157dd7cddfSDavid du Colombier */
167dd7cddfSDavid du Colombier 
17*593dc095SDavid du Colombier /* $Id: estack.h,v 1.6 2002/06/16 04:47:10 lpd Exp $ */
187dd7cddfSDavid du Colombier /* Definitions for the execution stack */
197dd7cddfSDavid du Colombier 
207dd7cddfSDavid du Colombier #ifndef estack_INCLUDED
217dd7cddfSDavid du Colombier #  define estack_INCLUDED
227dd7cddfSDavid du Colombier 
237dd7cddfSDavid du Colombier #include "iestack.h"
247dd7cddfSDavid du Colombier #include "icstate.h"		/* for access to exec_stack */
257dd7cddfSDavid du Colombier 
26*593dc095SDavid du Colombier /* Define access to the cached current_file pointer. */
277dd7cddfSDavid du Colombier #define esfile (iexec_stack.current_file)
287dd7cddfSDavid du Colombier #define esfile_clear_cache() estack_clear_cache(&iexec_stack)
297dd7cddfSDavid du Colombier #define esfile_set_cache(pref) estack_set_cache(&iexec_stack, pref)
307dd7cddfSDavid du Colombier #define esfile_check_cache() estack_check_cache(&iexec_stack)
317dd7cddfSDavid du Colombier 
327dd7cddfSDavid du Colombier /* Define the execution stack pointers for operators. */
337dd7cddfSDavid du Colombier #define iexec_stack (i_ctx_p->exec_stack)
347dd7cddfSDavid du Colombier #define e_stack (iexec_stack.stack)
357dd7cddfSDavid du Colombier 
367dd7cddfSDavid du Colombier #define esbot (e_stack.bot)
377dd7cddfSDavid du Colombier #define esp (e_stack.p)
387dd7cddfSDavid du Colombier #define estop (e_stack.top)
397dd7cddfSDavid du Colombier 
407dd7cddfSDavid du Colombier /*
41*593dc095SDavid du Colombier  * The execution stack holds several different kinds of objects (refs)
42*593dc095SDavid du Colombier  * related to executing PostScript code:
437dd7cddfSDavid du Colombier  *
447dd7cddfSDavid du Colombier  *      - Procedures being executed are held here.  They always have
457dd7cddfSDavid du Colombier  * type = t_array, t_mixedarray, or t_shortarray, with a_executable set.
467dd7cddfSDavid du Colombier  * More specifically, the e-stack holds the as yet unexecuted tail of the
477dd7cddfSDavid du Colombier  * procedure.
487dd7cddfSDavid du Colombier  *
49*593dc095SDavid du Colombier  *      - if, ifelse, etc. push arguments to be executed here.  They may be
50*593dc095SDavid du Colombier  * any kind of object whatever.  Similarly, looping operators (forall, for,
51*593dc095SDavid du Colombier  * etc.) push the procedure that is to be executed for each iteration.
527dd7cddfSDavid du Colombier  *
537dd7cddfSDavid du Colombier  *      - Control operators (filenameforall, for, repeat, loop, forall,
54*593dc095SDavid du Colombier  * pathforall, run, stopped, ...) use continuations as described below.
557dd7cddfSDavid du Colombier  *
56*593dc095SDavid du Colombier  * Note that there are many internal operators that need to use
57*593dc095SDavid du Colombier  * continuations -- for example, all the 'show' operators, since they may
58*593dc095SDavid du Colombier  * call out to BuildChar procedures.
59*593dc095SDavid du Colombier  */
60*593dc095SDavid du Colombier 
61*593dc095SDavid du Colombier /*
62*593dc095SDavid du Colombier  * Because the Ghostscript architecture doesn't allow recursive calls to the
63*593dc095SDavid du Colombier  * interpreter, any operator that needs to call out to PostScript code (for
64*593dc095SDavid du Colombier  * example, the 'show' operators calling a BuildChar procedure, or setscreen
65*593dc095SDavid du Colombier  * sampling a spot function) must use a continuation -- an internal
66*593dc095SDavid du Colombier  * "operator" procedure that continues the logical thread of execution after
67*593dc095SDavid du Colombier  * the callout.  Operators needing to use continuations push the following
68*593dc095SDavid du Colombier  * onto the execution stack (from bottom to top):
697dd7cddfSDavid du Colombier  *
70*593dc095SDavid du Colombier  *	- An e-stack mark -- an executable null that indicates the bottom of
71*593dc095SDavid du Colombier  *	the block associated with a callout.  (This should not be confused
72*593dc095SDavid du Colombier  *	with a PostScript mark, a ref of type t_mark on the operand stack.)
73*593dc095SDavid du Colombier  *	See make_mark_estack and push_mark_estack below.  The value.opproc
74*593dc095SDavid du Colombier  *	member of the e-stack mark contains a procedure to execute in case
75*593dc095SDavid du Colombier  *	the e-stack is stripped back beyond this point by a 'stop' or
76*593dc095SDavid du Colombier  *	'exit': see pop_estack in zcontrol.c for details.
77*593dc095SDavid du Colombier  *
78*593dc095SDavid du Colombier  *	- Any number of refs holding information that the continuation
79*593dc095SDavid du Colombier  *	operator needs -- i.e., the saved logical state of the thread of
80*593dc095SDavid du Colombier  *	execution.  For example, 'for' stores the procedure, the current
81*593dc095SDavid du Colombier  *	value, the increment, and the limit here.
82*593dc095SDavid du Colombier  *
83*593dc095SDavid du Colombier  *	- The continuation procedure itself -- the pseudo-operator to be
84*593dc095SDavid du Colombier  *	called after returns from the interpreter callout.  See
85*593dc095SDavid du Colombier  *	make_op_estack and push_op_estack below.
86*593dc095SDavid du Colombier  *
87*593dc095SDavid du Colombier  *	- The PostScript procedure for the interpreter to execute.
88*593dc095SDavid du Colombier  *
89*593dc095SDavid du Colombier  * The operator then returns o_push_estack, indicating to the interpreter
90*593dc095SDavid du Colombier  * that the operator has pushed information on the e-stack for the
91*593dc095SDavid du Colombier  * interpreter to process.
92*593dc095SDavid du Colombier  *
93*593dc095SDavid du Colombier  * When the interpreter finishes executing the PostScript procedure, it pops
94*593dc095SDavid du Colombier  * the next item off the e-stack, which is the continuation procedure.  When
95*593dc095SDavid du Colombier  * the continuation procedure gets control, the top of the e-stack (esp)
96*593dc095SDavid du Colombier  * points just below the continuation procedure slot -- i.e., to the topmost
97*593dc095SDavid du Colombier  * saved state item.  The continuation procedure normally pops all of the
98*593dc095SDavid du Colombier  * saved state, and the e-stack mark, and continues execution normally,
99*593dc095SDavid du Colombier  * eventually returning o_pop_estack to tell the interpreter that the
100*593dc095SDavid du Colombier  * "operator" has popped information off the e-stack.  (Loop operators do
101*593dc095SDavid du Colombier  * something a bit more efficient than popping the information and then
102*593dc095SDavid du Colombier  * pushing it again: refer to the examples in zcontrol.c for details.)
103*593dc095SDavid du Colombier  *
104*593dc095SDavid du Colombier  * Continuation procedures are called just like any other operator, so they
105*593dc095SDavid du Colombier  * can call each other, or be called from ordinary operator procedures, as
106*593dc095SDavid du Colombier  * long as the e-stack is in the right state.  The most complex example of
107*593dc095SDavid du Colombier  * this is probably the Type 1 character rendering code in zchar1.c, where
108*593dc095SDavid du Colombier  * continuation procedures either call each other directly or call out to
109*593dc095SDavid du Colombier  * the interpreter to execute optional PostScript procedures like CDevProc.
1107dd7cddfSDavid du Colombier  */
1117dd7cddfSDavid du Colombier 
1127dd7cddfSDavid du Colombier /* Macro for marking the execution stack */
1137dd7cddfSDavid du Colombier #define make_mark_estack(ep, es_idx, proc)\
1147dd7cddfSDavid du Colombier   make_tasv(ep, t_null, a_executable, es_idx, opproc, proc)
1157dd7cddfSDavid du Colombier #define push_mark_estack(es_idx, proc)\
1167dd7cddfSDavid du Colombier   (++esp, make_mark_estack(esp, es_idx, proc))
1177dd7cddfSDavid du Colombier #define r_is_estack_mark(ep)\
1187dd7cddfSDavid du Colombier   r_has_type_attrs(ep, t_null, a_executable)
1197dd7cddfSDavid du Colombier #define estack_mark_index(ep) r_size(ep)
1207dd7cddfSDavid du Colombier #define set_estack_mark_index(ep, es_idx) r_set_size(ep, es_idx)
1217dd7cddfSDavid du Colombier 
1227dd7cddfSDavid du Colombier /* Macro for pushing an operator on the execution stack */
1237dd7cddfSDavid du Colombier /* to represent a continuation procedure */
1247dd7cddfSDavid du Colombier #define make_op_estack(ep, proc)\
1257dd7cddfSDavid du Colombier   make_oper(ep, 0, proc)
1267dd7cddfSDavid du Colombier #define push_op_estack(proc)\
1277dd7cddfSDavid du Colombier   (++esp, make_op_estack(esp, proc))
1287dd7cddfSDavid du Colombier 
1297dd7cddfSDavid du Colombier /* Macro to ensure enough room on the execution stack */
1307dd7cddfSDavid du Colombier #define check_estack(n)\
1317dd7cddfSDavid du Colombier   if ( esp > estop - (n) )\
1327dd7cddfSDavid du Colombier     { int es_code_ = ref_stack_extend(&e_stack, n);\
1337dd7cddfSDavid du Colombier       if ( es_code_ < 0 ) return es_code_;\
1347dd7cddfSDavid du Colombier     }
1357dd7cddfSDavid du Colombier 
1367dd7cddfSDavid du Colombier /* Macro to ensure enough entries on the execution stack */
1377dd7cddfSDavid du Colombier #define check_esp(n)\
1387dd7cddfSDavid du Colombier   if ( esp < esbot + ((n) - 1) )\
1397dd7cddfSDavid du Colombier     { e_stack.requested = (n); return_error(e_ExecStackUnderflow); }
1407dd7cddfSDavid du Colombier 
1417dd7cddfSDavid du Colombier /* Define the various kinds of execution stack marks. */
1427dd7cddfSDavid du Colombier #define es_other 0		/* internal use */
1437dd7cddfSDavid du Colombier #define es_show 1		/* show operators */
1447dd7cddfSDavid du Colombier #define es_for 2		/* iteration operators */
1457dd7cddfSDavid du Colombier #define es_stopped 3		/* stopped operator */
1467dd7cddfSDavid du Colombier 
1477dd7cddfSDavid du Colombier /*
1487dd7cddfSDavid du Colombier  * Pop a given number of elements off the execution stack,
1497dd7cddfSDavid du Colombier  * executing cleanup procedures as necessary.
1507dd7cddfSDavid du Colombier  */
151*593dc095SDavid du Colombier void pop_estack(i_ctx_t *, uint);
1527dd7cddfSDavid du Colombier 
1537dd7cddfSDavid du Colombier /*
1547dd7cddfSDavid du Colombier  * The execution stack is implemented as a linked list of blocks;
1557dd7cddfSDavid du Colombier  * operators that can push or pop an unbounded number of values, or that
1567dd7cddfSDavid du Colombier  * access the entire e-stack, must take this into account.  These are:
1577dd7cddfSDavid du Colombier  *      exit  .stop  .instopped  countexecstack  execstack  currentfile
1587dd7cddfSDavid du Colombier  *      .execn
1597dd7cddfSDavid du Colombier  *      pop_estack(exit, stop, error recovery)
1607dd7cddfSDavid du Colombier  *      gs_show_find(all the show operators)
1617dd7cddfSDavid du Colombier  * In addition, for e-stack entries created by control operators, we must
1627dd7cddfSDavid du Colombier  * ensure that the mark and its data are never separated.  We do this
1637dd7cddfSDavid du Colombier  * by ensuring that when splitting the top block, at least N items
1647dd7cddfSDavid du Colombier  * are kept in the new top block above the bottommost retained mark,
1657dd7cddfSDavid du Colombier  * where N is the largest number of data items associated with a mark.
1667dd7cddfSDavid du Colombier  * Finally, in order to avoid specific checks for underflowing a block,
1677dd7cddfSDavid du Colombier  * we put a guard entry at the bottom of each block except the top one
1687dd7cddfSDavid du Colombier  * that contains a procedure that returns an internal "exec stack block
1697dd7cddfSDavid du Colombier  * underflow" error.
1707dd7cddfSDavid du Colombier  */
1717dd7cddfSDavid du Colombier 
1727dd7cddfSDavid du Colombier #endif /* estack_INCLUDED */
173