zpacked.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* Copyright (C) 1990, 1992, 1993, 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: zpacked.c,v 1.7 2004/08/19 19:33:09 stefan Exp $ */
  14. /* Packed array operators */
  15. #include "ghost.h"
  16. #include "ialloc.h"
  17. #include "idict.h"
  18. #include "iname.h"
  19. #include "istack.h" /* for iparray.h */
  20. #include "ipacked.h"
  21. #include "iparray.h"
  22. #include "ivmspace.h"
  23. #include "oper.h"
  24. #include "store.h"
  25. #include "gxalloc.h"
  26. /* - currentpacking <bool> */
  27. private int
  28. zcurrentpacking(i_ctx_t *i_ctx_p)
  29. {
  30. os_ptr op = osp;
  31. push(1);
  32. ref_assign(op, &ref_array_packing);
  33. return 0;
  34. }
  35. /* <obj_0> ... <obj_n-1> <n> packedarray <packedarray> */
  36. int
  37. zpackedarray(i_ctx_t *i_ctx_p)
  38. {
  39. os_ptr op = osp;
  40. int code;
  41. ref parr;
  42. check_type(*op, t_integer);
  43. if (op->value.intval < 0 ||
  44. (op->value.intval > op - osbot &&
  45. op->value.intval >= ref_stack_count(&o_stack))
  46. )
  47. return_error(e_rangecheck);
  48. osp--;
  49. code = make_packed_array(&parr, &o_stack, (uint) op->value.intval,
  50. idmemory, "packedarray");
  51. osp++;
  52. if (code >= 0)
  53. *osp = parr;
  54. return code;
  55. }
  56. /* <bool> setpacking - */
  57. private int
  58. zsetpacking(i_ctx_t *i_ctx_p)
  59. {
  60. os_ptr op = osp;
  61. ref cont;
  62. check_type(*op, t_boolean);
  63. make_struct(&cont, avm_local, ref_array_packing_container);
  64. ref_assign_old(&cont, &ref_array_packing, op, "setpacking");
  65. pop(1);
  66. return 0;
  67. }
  68. /* ------ Non-operator routines ------ */
  69. /* Make a packed array. See the comment in packed.h about */
  70. /* ensuring that refs in mixed arrays are properly aligned. */
  71. #undef idmemory /****** NOTA BENE ******/
  72. int
  73. make_packed_array(ref * parr, ref_stack_t * pstack, uint size,
  74. gs_dual_memory_t *idmemory, client_name_t cname)
  75. {
  76. uint i;
  77. const ref *pref;
  78. uint idest = 0, ishort = 0;
  79. ref_packed *pbody;
  80. ref_packed *pdest;
  81. ref_packed *pshort; /* points to start of */
  82. /* last run of short elements */
  83. gs_ref_memory_t *imem = idmemory->current;
  84. uint space = imemory_space(imem);
  85. int skip = 0, pad;
  86. ref rtemp;
  87. int code;
  88. /* Do a first pass to calculate the size of the array, */
  89. /* and to detect local-into-global stores. */
  90. for (i = size; i != 0; i--) {
  91. pref = ref_stack_index(pstack, i - 1);
  92. switch (r_btype(pref)) { /* not r_type, opers are special */
  93. case t_name:
  94. if (name_index(imem, pref) >= packed_name_max_index)
  95. break; /* can't pack */
  96. idest++;
  97. continue;
  98. case t_integer:
  99. if (pref->value.intval < packed_min_intval ||
  100. pref->value.intval > packed_max_intval
  101. )
  102. break;
  103. idest++;
  104. continue;
  105. case t_oparray:
  106. /* Check for local-into-global store. */
  107. store_check_space(space, pref);
  108. /* falls through */
  109. case t_operator:
  110. {
  111. uint oidx;
  112. if (!r_has_attr(pref, a_executable))
  113. break;
  114. oidx = op_index(pref);
  115. if (oidx == 0 || oidx > packed_int_mask)
  116. break;
  117. }
  118. idest++;
  119. continue;
  120. default:
  121. /* Check for local-into-global store. */
  122. store_check_space(space, pref);
  123. }
  124. /* Can't pack this element, use a full ref. */
  125. /* We may have to unpack up to align_packed_per_ref - 1 */
  126. /* preceding short elements. */
  127. /* If we are at the beginning of the array, however, */
  128. /* we can just move the elements up. */
  129. {
  130. int i = (idest - ishort) & (align_packed_per_ref - 1);
  131. if (ishort == 0) /* first time */
  132. idest += skip = -i & (align_packed_per_ref - 1);
  133. else
  134. idest += (packed_per_ref - 1) * i;
  135. }
  136. ishort = idest += packed_per_ref;
  137. }
  138. pad = -(int)idest & (packed_per_ref - 1); /* padding at end */
  139. /* Now we can allocate the array. */
  140. code = gs_alloc_ref_array(imem, &rtemp, 0, (idest + pad) / packed_per_ref,
  141. cname);
  142. if (code < 0)
  143. return code;
  144. pbody = (ref_packed *) rtemp.value.refs;
  145. /* Make sure any initial skipped elements contain legal packed */
  146. /* refs, so that the garbage collector can scan storage. */
  147. pshort = pbody;
  148. for (; skip; skip--)
  149. *pbody++ = pt_tag(pt_integer);
  150. pdest = pbody;
  151. for (i = size; i != 0; i--) {
  152. pref = ref_stack_index(pstack, i - 1);
  153. switch (r_btype(pref)) { /* not r_type, opers are special */
  154. case t_name:
  155. {
  156. uint nidx = name_index(imem, pref);
  157. if (nidx >= packed_name_max_index)
  158. break; /* can't pack */
  159. *pdest++ = nidx +
  160. (r_has_attr(pref, a_executable) ?
  161. pt_tag(pt_executable_name) :
  162. pt_tag(pt_literal_name));
  163. }
  164. continue;
  165. case t_integer:
  166. if (pref->value.intval < packed_min_intval ||
  167. pref->value.intval > packed_max_intval
  168. )
  169. break;
  170. *pdest++ = pt_tag(pt_integer) +
  171. ((short)pref->value.intval - packed_min_intval);
  172. continue;
  173. case t_oparray:
  174. case t_operator:
  175. {
  176. uint oidx;
  177. if (!r_has_attr(pref, a_executable))
  178. break;
  179. oidx = op_index(pref);
  180. if (oidx == 0 || oidx > packed_int_mask)
  181. break;
  182. *pdest++ = pt_tag(pt_executable_operator) + oidx;
  183. }
  184. continue;
  185. }
  186. /* Can't pack this element, use a full ref. */
  187. /* We may have to unpack up to align_packed_per_ref - 1 */
  188. /* preceding short elements. */
  189. /* Note that if we are at the beginning of the array, */
  190. /* 'skip' already ensures that we don't need to do this. */
  191. {
  192. int i = (pdest - pshort) & (align_packed_per_ref - 1);
  193. const ref_packed *psrc = pdest;
  194. ref *pmove =
  195. (ref *) (pdest += (packed_per_ref - 1) * i);
  196. ref_assign_new(pmove, pref);
  197. while (--i >= 0) {
  198. --psrc;
  199. --pmove;
  200. packed_get(imem->non_gc_memory, psrc, pmove);
  201. }
  202. }
  203. pshort = pdest += packed_per_ref;
  204. }
  205. {
  206. int atype =
  207. (pdest == pbody + size ? t_shortarray : t_mixedarray);
  208. /* Pad with legal packed refs so that the garbage collector */
  209. /* can scan storage. */
  210. for (; pad; pad--)
  211. *pdest++ = pt_tag(pt_integer);
  212. /* Finally, make the array. */
  213. ref_stack_pop(pstack, size);
  214. make_tasv_new(parr, atype, a_readonly | space, size,
  215. packed, pbody + skip);
  216. }
  217. return 0;
  218. }
  219. /* ------ Initialization procedure ------ */
  220. const op_def zpacked_op_defs[] =
  221. {
  222. {"0currentpacking", zcurrentpacking},
  223. {"1packedarray", zpackedarray},
  224. {"1setpacking", zsetpacking},
  225. op_def_end(0)
  226. };