summaryrefslogtreecommitdiff
path: root/psi/zpacked.c
blob: a0353e005ca5b7a7fa1c4768bd07c08bbcda0e1a (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
/* Copyright (C) 2001-2023 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.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* Packed array operators */
#include "ghost.h"
#include "ialloc.h"
#include "idict.h"
#include "iname.h"
#include "istack.h"		/* for iparray.h */
#include "ipacked.h"
#include "iparray.h"
#include "ivmspace.h"
#include "oper.h"
#include "store.h"
#include "gxalloc.h"

/* - currentpacking <bool> */
static int
zcurrentpacking(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    push(1);
    ref_assign(op, &ref_array_packing);
    return 0;
}

/* <obj_0> ... <obj_n-1> <n> packedarray <packedarray> */
int
zpackedarray(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    int code;
    ref parr;

    check_type(*op, t_integer);
    if (op->value.intval < 0)
        return_error(gs_error_rangecheck);
    if (op->value.intval > op - osbot &&
        op->value.intval >= ref_stack_count(&o_stack)
        )
        return_error(gs_error_stackunderflow);
    osp--;
    code = make_packed_array(&parr, &o_stack, (uint) op->value.intval,
                             idmemory, "packedarray");
    osp++;
    if (code >= 0)
        *osp = parr;
    return code;
}

/* <bool> setpacking - */
static int
zsetpacking(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    ref cont;
    check_type(*op, t_boolean);
    make_struct(&cont, avm_local, ref_array_packing_container);
    ref_assign_old(&cont, &ref_array_packing, op, "setpacking");
    pop(1);
    return 0;
}

/* ------ Non-operator routines ------ */

/* Make a packed array.  See the comment in packed.h about */
/* ensuring that refs in mixed arrays are properly aligned. */
#undef idmemory			/****** NOTA BENE ******/
int
make_packed_array(ref * parr, ref_stack_t * pstack, uint size,
                  gs_dual_memory_t *idmemory, client_name_t cname)
{
    uint i;
    const ref *pref;
    uint idest = 0, ishort = 0;
    ref_packed *pbody;
    ref_packed *pdest;
    ref_packed *pshort;		/* points to start of */
                                /* last run of short elements */
    gs_ref_memory_t *imem = idmemory->current;
    uint space = imemory_space(imem);
    int skip = 0, pad;
    ref rtemp;
    int code;

    /* Do a first pass to calculate the size of the array, */
    /* and to detect local-into-global stores. */

    for (i = size; i != 0; i--) {
        pref = ref_stack_index(pstack, i - 1);
        switch (r_btype(pref)) {	/* not r_type, opers are special */
            case t_name:
              if (name_index(imem, pref) >= packed_name_max_index)
                    break;	/* can't pack */
                idest++;
                continue;
            case t_integer:
                if (pref->value.intval < packed_min_intval ||
                    pref->value.intval > packed_max_intval
                    )
                    break;
                idest++;
                continue;
            case t_oparray:
                /* Check for local-into-global store. */
                store_check_space(space, pref);
                /* falls through */
            case t_operator:
                {
                    uint oidx;

                    if (!r_has_attr(pref, a_executable))
                        break;
                    oidx = op_index(pref);
                    if (oidx == 0 || oidx > packed_int_mask)
                        break;
                }
                idest++;
                continue;
            default:
                /* Check for local-into-global store. */
                store_check_space(space, pref);
        }
        /* Can't pack this element, use a full ref. */
        /* We may have to unpack up to align_packed_per_ref - 1 */
        /* preceding short elements. */
        /* If we are at the beginning of the array, however, */
        /* we can just move the elements up. */
        {
            int i = (idest - ishort) & (align_packed_per_ref - 1);

            if (ishort == 0)	/* first time */
                idest += skip = -i & (align_packed_per_ref - 1);
            else
                idest += (packed_per_ref - 1) * i;
        }
        ishort = idest += packed_per_ref;
    }
    pad = (packed_per_ref - idest % packed_per_ref) % packed_per_ref;	/* padding at end */

    /* Now we can allocate the array. */

    code = gs_alloc_ref_array(imem, &rtemp, 0, (idest + pad) / packed_per_ref,
                              cname);
    if (code < 0)
        return code;
    pbody = (ref_packed *) rtemp.value.refs;

    /* Make sure any initial skipped elements contain legal packed */
    /* refs, so that the garbage collector can scan storage. */

    pshort = pbody;
    for (; skip; skip--)
        *pbody++ = pt_tag(pt_integer);
    pdest = pbody;

    for (i = size; i != 0; i--) {
        pref = ref_stack_index(pstack, i - 1);
        switch (r_btype(pref)) {	/* not r_type, opers are special */
            case t_name:
                {
                    uint nidx = name_index(imem, pref);

                    if (nidx >= packed_name_max_index)
                        break;	/* can't pack */
                    *pdest++ = nidx +
                        (r_has_attr(pref, a_executable) ?
                         pt_tag(pt_executable_name) :
                         pt_tag(pt_literal_name));
                }
                continue;
            case t_integer:
                if (pref->value.intval < packed_min_intval ||
                    pref->value.intval > packed_max_intval
                    )
                    break;
                *pdest++ = pt_tag(pt_integer) +
                    ((short)pref->value.intval - packed_min_intval);
                continue;
            case t_oparray:
            case t_operator:
                {
                    uint oidx;

                    if (!r_has_attr(pref, a_executable))
                        break;
                    oidx = op_index(pref);
                    if (oidx == 0 || oidx > packed_int_mask)
                        break;
                    *pdest++ = pt_tag(pt_executable_operator) + oidx;
                }
                continue;
        }
        /* Can't pack this element, use a full ref. */
        /* We may have to unpack up to align_packed_per_ref - 1 */
        /* preceding short elements. */
        /* Note that if we are at the beginning of the array, */
        /* 'skip' already ensures that we don't need to do this. */
        {
            int i = (pdest - pshort) & (align_packed_per_ref - 1);
            const ref_packed *psrc = pdest;
            ref *pmove =
            (ref *) (pdest += (packed_per_ref - 1) * i);

            ref_assign_new(pmove, pref);
            while (--i >= 0) {
                --psrc;
                --pmove;
                packed_get(imem->non_gc_memory, psrc, pmove);
            }
        }
        pshort = pdest += packed_per_ref;
    }

    {
        int atype =
        (pdest == pbody + size ? t_shortarray : t_mixedarray);

        /* Pad with legal packed refs so that the garbage collector */
        /* can scan storage. */

        for (; pad; pad--)
            *pdest++ = pt_tag(pt_integer);

        /* Finally, make the array. */

        ref_stack_pop(pstack, size);
        make_tasv_new(parr, atype, a_readonly | space, size,
                      packed, pbody + skip);
    }
    return 0;
}

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

const op_def zpacked_op_defs[] =
{
    {"0currentpacking", zcurrentpacking},
    {"1packedarray", zpackedarray},
    {"1setpacking", zsetpacking},
    op_def_end(0)
};