zstack.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* Copyright (C) 1989, 1991, 1992, 1994, 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: zstack.c,v 1.4 2002/02/21 22:24:54 giles Exp $ */
  14. /* Operand stack operators */
  15. #include "memory_.h"
  16. #include "ghost.h"
  17. #include "ialloc.h"
  18. #include "istack.h"
  19. #include "oper.h"
  20. #include "store.h"
  21. /* <obj> pop - */
  22. int
  23. zpop(i_ctx_t *i_ctx_p)
  24. {
  25. os_ptr op = osp;
  26. check_op(1);
  27. pop(1);
  28. return 0;
  29. }
  30. /* <obj1> <obj2> exch <obj2> <obj1> */
  31. int
  32. zexch(i_ctx_t *i_ctx_p)
  33. {
  34. os_ptr op = osp;
  35. ref next;
  36. check_op(2);
  37. ref_assign_inline(&next, op - 1);
  38. ref_assign_inline(op - 1, op);
  39. ref_assign_inline(op, &next);
  40. return 0;
  41. }
  42. /* <obj> dup <obj> <obj> */
  43. int
  44. zdup(i_ctx_t *i_ctx_p)
  45. {
  46. os_ptr op = osp;
  47. check_op(1);
  48. push(1);
  49. ref_assign_inline(op, op - 1);
  50. return 0;
  51. }
  52. /* <obj_n> ... <obj_0> <n> index <obj_n> ... <obj_0> <obj_n> */
  53. int
  54. zindex(i_ctx_t *i_ctx_p)
  55. {
  56. os_ptr op = osp;
  57. register os_ptr opn;
  58. check_type(*op, t_integer);
  59. if ((ulong)op->value.intval >= op - osbot) {
  60. /* Might be in an older stack block. */
  61. ref *elt;
  62. if (op->value.intval < 0)
  63. return_error(e_rangecheck);
  64. elt = ref_stack_index(&o_stack, op->value.intval + 1);
  65. if (elt == 0)
  66. return_error(e_rangecheck);
  67. ref_assign(op, elt);
  68. return 0;
  69. }
  70. opn = op + ~(int)op->value.intval;
  71. ref_assign_inline(op, opn);
  72. return 0;
  73. }
  74. /* <obj_n-1> ... <obj_0> <n> <i> roll */
  75. /* <obj_(i-1)_mod_ n> ... <obj_0> <obj_n-1> ... <obj_i_mod_n> */
  76. int
  77. zroll(i_ctx_t *i_ctx_p)
  78. {
  79. os_ptr op = osp;
  80. os_ptr op1 = op - 1;
  81. int count, mod;
  82. register os_ptr from, to;
  83. register int n;
  84. check_type(*op1, t_integer);
  85. check_type(*op, t_integer);
  86. if ((ulong) op1->value.intval > op1 - osbot) {
  87. /*
  88. * The data might span multiple stack blocks.
  89. * There are efficient ways to handle this situation,
  90. * but they're more complicated than seems worth implementing;
  91. * for now, do something very simple and inefficient.
  92. */
  93. int left, i;
  94. if (op1->value.intval < 0 ||
  95. op1->value.intval + 2 > ref_stack_count(&o_stack)
  96. )
  97. return_error(e_rangecheck);
  98. count = op1->value.intval;
  99. if (count <= 1) {
  100. pop(2);
  101. return 0;
  102. }
  103. mod = op->value.intval;
  104. if (mod >= count)
  105. mod %= count;
  106. else if (mod < 0) {
  107. mod %= count;
  108. if (mod < 0)
  109. mod += count; /* can't assume % means mod! */
  110. }
  111. /* Use the chain rotation algorithm mentioned below. */
  112. for (i = 0, left = count; left; i++) {
  113. ref *elt = ref_stack_index(&o_stack, i + 2);
  114. ref save;
  115. int j, k;
  116. ref *next;
  117. save = *elt;
  118. for (j = i, left--;; j = k, elt = next, left--) {
  119. k = (j + mod) % count;
  120. if (k == i)
  121. break;
  122. next = ref_stack_index(&o_stack, k + 2);
  123. ref_assign(elt, next);
  124. }
  125. *elt = save;
  126. }
  127. pop(2);
  128. return 0;
  129. }
  130. count = op1->value.intval;
  131. if (count <= 1) {
  132. pop(2);
  133. return 0;
  134. }
  135. mod = op->value.intval;
  136. /*
  137. * The elegant approach, requiring no extra space, would be to
  138. * rotate the elements in chains separated by mod elements.
  139. * Instead, we simply check to make sure there is enough space
  140. * above op to do the roll in two block moves.
  141. * Unfortunately, we can't count on memcpy doing the right thing
  142. * in *either* direction.
  143. */
  144. switch (mod) {
  145. case 1: /* common special case */
  146. pop(2);
  147. op -= 2;
  148. {
  149. ref top;
  150. ref_assign_inline(&top, op);
  151. for (from = op, n = count; --n; from--)
  152. ref_assign_inline(from, from - 1);
  153. ref_assign_inline(from, &top);
  154. }
  155. return 0;
  156. case -1: /* common special case */
  157. pop(2);
  158. op -= 2;
  159. {
  160. ref bot;
  161. to = op - count + 1;
  162. ref_assign_inline(&bot, to);
  163. for (n = count; --n; to++)
  164. ref_assign(to, to + 1);
  165. ref_assign_inline(to, &bot);
  166. }
  167. return 0;
  168. }
  169. if (mod < 0) {
  170. mod += count;
  171. if (mod < 0) {
  172. mod %= count;
  173. if (mod < 0)
  174. mod += count; /* can't assume % means mod! */
  175. }
  176. } else if (mod >= count)
  177. mod %= count;
  178. if (mod <= count >> 1) {
  179. /* Move everything up, then top elements down. */
  180. if (mod >= ostop - op) {
  181. o_stack.requested = mod;
  182. return_error(e_stackoverflow);
  183. }
  184. pop(2);
  185. op -= 2;
  186. for (to = op + mod, from = op, n = count; n--; to--, from--)
  187. ref_assign(to, from);
  188. memcpy((char *)(from + 1), (char *)(op + 1), mod * sizeof(ref));
  189. } else {
  190. /* Move bottom elements up, then everything down. */
  191. mod = count - mod;
  192. if (mod >= ostop - op) {
  193. o_stack.requested = mod;
  194. return_error(e_stackoverflow);
  195. }
  196. pop(2);
  197. op -= 2;
  198. to = op - count + 1;
  199. memcpy((char *)(op + 1), (char *)to, mod * sizeof(ref));
  200. for (from = to + mod, n = count; n--; to++, from++)
  201. ref_assign(to, from);
  202. }
  203. return 0;
  204. }
  205. /* |- ... clear |- */
  206. /* The function name is changed, because the IRIS library has */
  207. /* a function called zclear. */
  208. private int
  209. zclear_stack(i_ctx_t *i_ctx_p)
  210. {
  211. ref_stack_clear(&o_stack);
  212. return 0;
  213. }
  214. /* |- <obj_n-1> ... <obj_0> count <obj_n-1> ... <obj_0> <n> */
  215. private int
  216. zcount(i_ctx_t *i_ctx_p)
  217. {
  218. os_ptr op = osp;
  219. push(1);
  220. make_int(op, ref_stack_count(&o_stack) - 1);
  221. return 0;
  222. }
  223. /* - mark <mark> */
  224. private int
  225. zmark(i_ctx_t *i_ctx_p)
  226. {
  227. os_ptr op = osp;
  228. push(1);
  229. make_mark(op);
  230. return 0;
  231. }
  232. /* <mark> ... cleartomark */
  233. int
  234. zcleartomark(i_ctx_t *i_ctx_p)
  235. {
  236. uint count = ref_stack_counttomark(&o_stack);
  237. if (count == 0)
  238. return_error(e_unmatchedmark);
  239. ref_stack_pop(&o_stack, count);
  240. return 0;
  241. }
  242. /* <mark> <obj_n-1> ... <obj_0> counttomark */
  243. /* <mark> <obj_n-1> ... <obj_0> <n> */
  244. private int
  245. zcounttomark(i_ctx_t *i_ctx_p)
  246. {
  247. os_ptr op = osp;
  248. uint count = ref_stack_counttomark(&o_stack);
  249. if (count == 0)
  250. return_error(e_unmatchedmark);
  251. push(1);
  252. make_int(op, count - 1);
  253. return 0;
  254. }
  255. /* ------ Initialization procedure ------ */
  256. const op_def zstack_op_defs[] =
  257. {
  258. {"0clear", zclear_stack},
  259. {"0cleartomark", zcleartomark},
  260. {"0count", zcount},
  261. {"0counttomark", zcounttomark},
  262. {"1dup", zdup},
  263. {"2exch", zexch},
  264. {"2index", zindex},
  265. {"0mark", zmark},
  266. {"1pop", zpop},
  267. {"2roll", zroll},
  268. op_def_end(0)
  269. };