1 /* Copyright (C) 2001-2006 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied, modified
8    or distributed except as expressly authorized under the terms of that
9    license.  Refer to licensing information at http://www.artifex.com/
10    or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
11    San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
12 */
13 
14 /* $Id: ostack.h 9043 2008-08-28 22:48:19Z giles $ */
15 /* Definitions for Ghostscript operand stack */
16 
17 #ifndef ostack_INCLUDED
18 #  define ostack_INCLUDED
19 
20 #include "iostack.h"
21 #include "icstate.h"		/* for access to op_stack */
22 
23 /* Define the operand stack pointers for operators. */
24 #define iop_stack (i_ctx_p->op_stack)
25 #define o_stack (iop_stack.stack)
26 
27 #define osbot (o_stack.bot)
28 #define osp (o_stack.p)
29 #define ostop (o_stack.top)
30 
31 /* Macro to ensure enough room on the operand stack */
32 #define check_ostack(n)\
33   if ( ostop - osp < (n) )\
34     { o_stack.requested = (n); return_error(e_stackoverflow); }
35 
36 /* Operand stack manipulation. */
37 
38 /* Note that push sets osp to (the new value of) op. */
39 #define push(n)\
40   BEGIN\
41     if ( (op += (n)) > ostop )\
42       { o_stack.requested = (n); return_error(e_stackoverflow); }\
43     else osp = op;\
44   END
45 
46 /*
47  * Note that the pop macro only decrements osp, not op.  For this reason,
48  *
49  *      >>>     pop should only be used just before returning,  <<<
50  *      >>>     or else op must be decremented explicitly.      <<<
51  */
52 #define pop(n) (osp -= (n))
53 
54 /*
55  * Note that the interpreter does not check for operand stack underflow
56  * before calling the operator procedure.  There are "guard" entries
57  * with invalid types and attributes just below the bottom of the
58  * operand stack: if the operator returns with a typecheck error,
59  * the interpreter checks for underflow at that time.
60  * Operators that don't typecheck their arguments must check for
61  * operand stack underflow explicitly; operators that take a variable
62  * number of arguments must also check for stack underflow in those cases
63  * where they expect more than their minimum number of arguments.
64  * (This is because the interpreter can only recognize that a typecheck
65  * is really a stackunderflow when the stack has fewer than the
66  * operator's declared minimum number of entries.)
67  */
68 #define check_op(nargs)\
69   if ( op < osbot + ((nargs) - 1) ) return_error(e_stackunderflow)
70 /*
71  * Similarly, in order to simplify some overflow checks, we allocate
72  * a few guard entries just above the top of the o-stack.
73  */
74 
75 /*
76  * The operand stack is implemented as a linked list of blocks:
77  * operators that can push or pop an unbounded number of values, or that
78  * access the entire o-stack, must take this into account.  These are:
79  *      (int)copy  index  roll  clear  count  cleartomark
80  *      counttomark  aload  astore  packedarray
81  *      .get/.putdeviceparams .gethardwareparams
82  */
83 
84 #endif /* ostack_INCLUDED */
85