summaryrefslogtreecommitdiff
blob: 2f2f0db9df889fab11f973254e03e3a2625c9d95 (plain)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* Copyright (C) 2001-2020 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/


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

/* <obj> pop - */
int
zpop(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    check_op(1);
    pop(1);
    return 0;
}

/* <obj1> <obj2> exch <obj2> <obj1> */
int
zexch(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    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(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    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(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    register os_ptr opn;

    check_type(*op, t_integer);
    if ((ulong)op->value.intval >= (ulong)(op - osbot)) {
        /* Might be in an older stack block. */
        ref *elt;

        if (op->value.intval < 0)
            return_error(gs_error_rangecheck);
        elt = ref_stack_index(&o_stack, op->value.intval + 1);
        if (elt == 0)
            return_error(gs_error_stackunderflow);
        ref_assign(op, elt);
        return 0;
    }
    opn = op + ~(int)op->value.intval;
    ref_assign_inline(op, opn);
    return 0;
}

/* <obj_n> ... <obj_0> <n> .argindex <obj_n> ... <obj_0> <obj_n> */
static int
zargindex(i_ctx_t *i_ctx_p)
{
    int code = zindex(i_ctx_p);

    /*
     * Pseudo-operators should use .argindex rather than index to access
     * their arguments on the stack, so that if there aren't enough, the
     * result will be a stackunderflow rather than a rangecheck.  (This is,
     * in fact, the only reason this operator exists.)
     */
    if (code == gs_error_rangecheck && osp->value.intval >= 0)
        code = gs_note_error(gs_error_stackunderflow);
    return code;
}

/* <obj_n-1> ... <obj_0> <n> <i> roll */
/*      <obj_(i-1)_mod_ n> ... <obj_0> <obj_n-1> ... <obj_i_mod_n> */
int
zroll(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    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 ((uint) op1->value.intval > (uint)(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)
            return_error(gs_error_rangecheck);
        if (op1->value.intval + 2 > (int)ref_stack_count(&o_stack))
            return_error(gs_error_stackunderflow);
        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(gs_error_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(gs_error_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. */
static int
zclear_stack(i_ctx_t *i_ctx_p)
{
    ref_stack_clear(&o_stack);
    return 0;
}

/* |- <obj_n-1> ... <obj_0> count <obj_n-1> ... <obj_0> <n> */
static int
zcount(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    push(1);
    make_int(op, ref_stack_count(&o_stack) - 1);
    return 0;
}

/* - mark <mark> */
static int
zmark(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    push(1);
    make_mark(op);
    return 0;
}

/* <mark> ... cleartomark */
int
zcleartomark(i_ctx_t *i_ctx_p)
{
    uint count = ref_stack_counttomark(&o_stack);

    if (count == 0)
        return_error(gs_error_unmatchedmark);
    ref_stack_pop(&o_stack, count);
    return 0;
}

/* <mark> <obj_n-1> ... <obj_0> counttomark */
/*      <mark> <obj_n-1> ... <obj_0> <n> */
static int
zcounttomark(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    uint count = ref_stack_counttomark(&o_stack);

    if (count == 0)
        return_error(gs_error_unmatchedmark);
    push(1);
    make_int(op, count - 1);
    return 0;
}

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

const op_def zstack_op_defs[] =
{
    {"2.argindex", zargindex},
    {"0clear", zclear_stack},
    {"0cleartomark", zcleartomark},
    {"0count", zcount},
    {"0counttomark", zcounttomark},
    {"1dup", zdup},
    {"2exch", zexch},
    {"2index", zindex},
    {"0mark", zmark},
    {"1pop", zpop},
    {"2roll", zroll},
    op_def_end(0)
};