zarray.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Copyright (C) 1989, 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: zarray.c,v 1.5 2004/08/04 19:36:13 stefan Exp $ */
  14. /* Array operators */
  15. #include "memory_.h"
  16. #include "ghost.h"
  17. #include "ialloc.h"
  18. #include "ipacked.h"
  19. #include "oper.h"
  20. #include "store.h"
  21. /* The generic operators (copy, get, put, getinterval, putinterval, */
  22. /* length, and forall) are implemented in zgeneric.c. */
  23. /* <int> array <array> */
  24. int
  25. zarray(i_ctx_t *i_ctx_p)
  26. {
  27. os_ptr op = osp;
  28. uint size;
  29. int code;
  30. check_int_leu(*op, max_array_size);
  31. size = op->value.intval;
  32. code = ialloc_ref_array((ref *)op, a_all, size, "array");
  33. if (code < 0)
  34. return code;
  35. refset_null(op->value.refs, size);
  36. return 0;
  37. }
  38. /* <array> aload <obj_0> ... <obj_n-1> <array> */
  39. private int
  40. zaload(i_ctx_t *i_ctx_p)
  41. {
  42. os_ptr op = osp;
  43. ref aref;
  44. uint asize;
  45. ref_assign(&aref, op);
  46. if (!r_is_array(&aref))
  47. return_op_typecheck(op);
  48. check_read(aref);
  49. asize = r_size(&aref);
  50. if (asize > ostop - op) { /* Use the slow, general algorithm. */
  51. int code = ref_stack_push(&o_stack, asize);
  52. uint i;
  53. const ref_packed *packed = aref.value.packed;
  54. if (code < 0)
  55. return code;
  56. for (i = asize; i > 0; i--, packed = packed_next(packed))
  57. packed_get(imemory, packed, ref_stack_index(&o_stack, i));
  58. *osp = aref;
  59. return 0;
  60. }
  61. if (r_has_type(&aref, t_array))
  62. memcpy(op, aref.value.refs, asize * sizeof(ref));
  63. else {
  64. uint i;
  65. const ref_packed *packed = aref.value.packed;
  66. os_ptr pdest = op;
  67. for (i = 0; i < asize; i++, pdest++, packed = packed_next(packed))
  68. packed_get(imemory, packed, pdest);
  69. }
  70. push(asize);
  71. ref_assign(op, &aref);
  72. return 0;
  73. }
  74. /* <obj_0> ... <obj_n-1> <array> astore <array> */
  75. private int
  76. zastore(i_ctx_t *i_ctx_p)
  77. {
  78. os_ptr op = osp;
  79. uint size;
  80. int code;
  81. check_write_type(*op, t_array);
  82. size = r_size(op);
  83. if (size > op - osbot) {
  84. /* The store operation might involve other stack segments. */
  85. ref arr;
  86. if (size >= ref_stack_count(&o_stack))
  87. return_error(e_stackunderflow);
  88. arr = *op;
  89. code = ref_stack_store(&o_stack, &arr, size, 1, 0, true, idmemory,
  90. "astore");
  91. if (code < 0)
  92. return code;
  93. ref_stack_pop(&o_stack, size);
  94. *ref_stack_index(&o_stack, 0) = arr;
  95. } else {
  96. code = refcpy_to_old(op, 0, op - size, size, idmemory, "astore");
  97. if (code < 0)
  98. return code;
  99. op[-(int)size] = *op;
  100. pop(size);
  101. }
  102. return 0;
  103. }
  104. /* ------ Initialization procedure ------ */
  105. const op_def zarray_op_defs[] =
  106. {
  107. {"1aload", zaload},
  108. {"1array", zarray},
  109. {"1astore", zastore},
  110. op_def_end(0)
  111. };