File: zstack.c

package info (click to toggle)
gs 5.10-3.99.slink.1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 14,992 kB
  • ctags: 25,337
  • sloc: ansic: 163,794; makefile: 3,156; cpp: 2,237; sh: 1,030; asm: 684; tcl: 434; perl: 56
file content (267 lines) | stat: -rw-r--r-- 6,791 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* Copyright (C) 1989, 1991, 1992, 1994 Aladdin Enterprises.  All rights reserved.
  
  This file is part of GNU Ghostscript.
  
  GNU Ghostscript is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  anyone for the consequences of using it or for whether it serves any
  particular purpose or works at all, unless he says so in writing.  Refer to
  the GNU General Public License for full details.
  
  Everyone is granted permission to copy, modify and redistribute GNU
  Ghostscript, but only under the conditions described in the GNU General
  Public License.  A copy of this license is supposed to have been given to
  you along with GNU Ghostscript so you can know your rights and
  responsibilities.  It should be in a file named COPYING.  Among other
  things, the copyright notice and this notice must be preserved on all
  copies.
  
  Aladdin Enterprises is not affiliated with the Free Software Foundation or
  the GNU Project.  GNU Ghostscript, as distributed by Aladdin Enterprises,
  does not depend on any other GNU software.
*/

/* zstack.c */
/* Operand stack operators */
#include "memory_.h"
#include "ghost.h"
#include "errors.h"
#include "ialloc.h"
#include "istack.h"
#include "oper.h"
#include "store.h"

/* <obj> pop - */
int
zpop(register os_ptr op)
{	check_op(1);
	pop(1);
	return 0;
}

/* <obj1> <obj2> exch <obj2> <obj1> */
int
zexch(register os_ptr op)
{	ref next;
	check_op(2);
	ref_assign_inline(&next, op - 1);
	ref_assign_inline(op - 1, op);
	ref_assign_inline(op, &next);
	return 0;
}

/* <obj> dup <obj> <obj> */
int
zdup(register os_ptr op)
{	check_op(1);
	push(1);
	ref_assign_inline(op, op - 1);
	return 0;
}

/* <obj_n> ... <obj_0> <n> index <obj_n> ... <obj_0> <obj_n> */
int
zindex(register os_ptr op)
{	register os_ptr opn;
	check_type(*op, t_integer);
	if ( (ulong)op->value.intval >= op - osbot )
	{	/* Might be in an older stack block. */
		ref *elt;
		if ( op->value.intval < 0 )
		  return_error(e_rangecheck);
		elt = ref_stack_index(&o_stack, op->value.intval + 1);
		if ( elt == 0 )
		  return_error(e_rangecheck);
		ref_assign(op, elt);
		return 0;
	}
	opn = op + ~(int)op->value.intval;
	ref_assign_inline(op, opn);
	return 0;
}

/* <obj_n-1> ... <obj_0> <n> <i> roll */
/*	<obj_(i-1)_mod_ n> ... <obj_0> <obj_n-1> ... <obj_i_mod_n> */
int
zroll(register os_ptr op)
{	os_ptr op1 = op - 1;
	int count, mod;
	register os_ptr from, to;
	register int n;
	check_type(*op1, t_integer);
	check_type(*op, t_integer);
	if ( (ulong)op1->value.intval > op1 - osbot )
	{	/*
		 * The data might span multiple stack blocks.
		 * There are efficient ways to handle this situation,
		 * but they're more complicated than seems worth implementing;
		 * for now, do something very simple and inefficient.
		 */
		int left, i;
		if ( op1->value.intval < 0 ||
		     op1->value.intval + 2 > ref_stack_count(&o_stack)
		   )
		  return_error(e_rangecheck);
		count = op1->value.intval;
		if ( count <= 1 )
		{	pop(2);
			return 0;
		}
		mod = op->value.intval;
		if ( mod >= count )
			mod %= count;
		else if ( mod < 0 )
		{	mod %= count;
			if ( mod < 0 ) mod += count;  /* can't assume % means mod! */
		}
		/* Use the chain rotation algorithm mentioned below. */
		for ( i = 0, left = count; left; i++ )
		  {	ref *elt = ref_stack_index(&o_stack, i + 2);
			ref save;
			int j, k;
			ref *next;
			save = *elt;
			for ( j = i, left--; ; j = k, elt = next, left-- )
			  {	k = (j + mod) % count;
				if ( k == i )
				  break;
				next = ref_stack_index(&o_stack, k + 2);
				ref_assign(elt, next);
			  }
			*elt = save;
		  }
		pop(2);
		return 0;
	}
	count = op1->value.intval;
	if ( count <= 1 )
	{	pop(2);
		return 0;
	}
	mod = op->value.intval;
	/*
	 * The elegant approach, requiring no extra space, would be to
	 * rotate the elements in chains separated by mod elements.
	 * Instead, we simply check to make sure there is enough space
	 * above op to do the roll in two block moves.
	 * Unfortunately, we can't count on memcpy doing the right thing
	 * in *either* direction.
	 */
	switch ( mod )
	{
	case 1:			/* common special case */
		pop(2);  op -= 2;
	{	ref top;
		ref_assign_inline(&top, op);
		for ( from = op, n = count; --n; from-- )
			ref_assign_inline(from, from - 1);
		ref_assign_inline(from, &top);
	}	return 0;
	case -1:		/* common special case */
		pop(2);  op -= 2;
	{	ref bot;
		to = op - count + 1;
		ref_assign_inline(&bot, to);
		for ( n = count; --n; to++ )
			ref_assign(to, to + 1);
		ref_assign_inline(to, &bot);
	}	return 0;
	}
	if ( mod < 0 )
	  {	mod += count;
		if ( mod < 0 )
		  {	mod %= count;
			if ( mod < 0 )
			  mod += count;  /* can't assume % means mod! */
		  }
	  }
	else if ( mod >= count )
	  mod %= count;
	if ( mod <= count >> 1)
	{	 /* Move everything up, then top elements down. */
		if ( mod >= ostop - op )
		  {	o_stack.requested = mod;
			return_error(e_stackoverflow);
		  }
		pop(2);  op -= 2;
		for ( to = op + mod, from = op, n = count; n--; to--, from-- )
			ref_assign(to, from);
		memcpy((char *)(from + 1), (char *)(op + 1), mod * sizeof(ref));
	}
	else
	{	/* Move bottom elements up, then everything down. */
		mod = count - mod;
		if ( mod >= ostop - op )
		  {	o_stack.requested = mod;
			return_error(e_stackoverflow);
		  }
		pop(2);  op -= 2;
		to = op - count + 1;
		memcpy((char *)(op + 1), (char *)to, mod * sizeof(ref));
		for ( from = to + mod, n = count; n--; to++, from++ )
			ref_assign(to, from);
	}
	return 0;
}

/* |- ... clear |- */
/* The function name is changed, because the IRIS library has */
/* a function called zclear. */
private int
zclear_stack(os_ptr op)
{	ref_stack_clear(&o_stack);
	return 0;
}

/* |- <obj_n-1> ... <obj_0> count <obj_n-1> ... <obj_0> <n> */
private int
zcount(register os_ptr op)
{	push(1);
	make_int(op, ref_stack_count(&o_stack) - 1);
	return 0;
}

/* - mark <mark> */
private int
zmark(register os_ptr op)
{	push(1);
	make_mark(op);
	return 0;
}

/* <mark> ... cleartomark */
int
zcleartomark(register os_ptr op)
{	uint count = ref_stack_counttomark(&o_stack);
	if ( count == 0 )
	  return_error(e_unmatchedmark);
	ref_stack_pop(&o_stack, count);
	return 0;
}

/* <mark> <obj_n-1> ... <obj_0> counttomark */
/*	<mark> <obj_n-1> ... <obj_0> <n> */
private int
zcounttomark(os_ptr op)
{	uint count = ref_stack_counttomark(&o_stack);
	if ( count == 0 )
	  return_error(e_unmatchedmark);
	push(1);
	make_int(op, count - 1);
	return 0;
}

/* ------ Initialization procedure ------ */

BEGIN_OP_DEFS(zstack_op_defs) {
	{"0clear", zclear_stack},
	{"0cleartomark", zcleartomark},
	{"0count", zcount},
	{"0counttomark", zcounttomark},
	{"1dup", zdup},
	{"2exch", zexch},
	{"2index", zindex},
	{"0mark", zmark},
	{"1pop", zpop},
	{"2roll", zroll},
END_OP_DEFS(0) }