zrelbit.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* Copyright (C) 1989, 1995, 1997, 1998, 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: zrelbit.c,v 1.6 2004/08/04 19:36:13 stefan Exp $ */
  14. /* Relational, boolean, and bit operators */
  15. #include "ghost.h"
  16. #include "oper.h"
  17. #include "gsutil.h"
  18. #include "idict.h"
  19. #include "store.h"
  20. /*
  21. * Many of the procedures in this file are public only so they can be
  22. * called from the FunctionType 4 interpreter (zfunc4.c).
  23. */
  24. /* ------ Standard operators ------ */
  25. /* Define the type test for eq and its relatives. */
  26. #define EQ_CHECK_READ(opp, dflt)\
  27. switch ( r_type(opp) ) {\
  28. case t_string:\
  29. check_read(*(opp));\
  30. break;\
  31. default:\
  32. dflt;\
  33. }
  34. /* Forward references */
  35. private int obj_le(os_ptr, os_ptr);
  36. /* <obj1> <obj2> eq <bool> */
  37. int
  38. zeq(i_ctx_t *i_ctx_p)
  39. {
  40. os_ptr op = osp;
  41. EQ_CHECK_READ(op - 1, check_op(2));
  42. EQ_CHECK_READ(op, DO_NOTHING);
  43. make_bool(op - 1, (obj_eq(imemory, op - 1, op) ? 1 : 0));
  44. pop(1);
  45. return 0;
  46. }
  47. /* <obj1> <obj2> ne <bool> */
  48. int
  49. zne(i_ctx_t *i_ctx_p)
  50. { /* We'll just be lazy and use eq. */
  51. int code = zeq(i_ctx_p);
  52. if (!code)
  53. osp->value.boolval ^= 1;
  54. return code;
  55. }
  56. /* <num1> <num2> ge <bool> */
  57. /* <str1> <str2> ge <bool> */
  58. int
  59. zge(i_ctx_t *i_ctx_p)
  60. {
  61. os_ptr op = osp;
  62. int code = obj_le(op, op - 1);
  63. if (code < 0)
  64. return code;
  65. make_bool(op - 1, code);
  66. pop(1);
  67. return 0;
  68. }
  69. /* <num1> <num2> gt <bool> */
  70. /* <str1> <str2> gt <bool> */
  71. int
  72. zgt(i_ctx_t *i_ctx_p)
  73. {
  74. os_ptr op = osp;
  75. int code = obj_le(op - 1, op);
  76. if (code < 0)
  77. return code;
  78. make_bool(op - 1, code ^ 1);
  79. pop(1);
  80. return 0;
  81. }
  82. /* <num1> <num2> le <bool> */
  83. /* <str1> <str2> le <bool> */
  84. int
  85. zle(i_ctx_t *i_ctx_p)
  86. {
  87. os_ptr op = osp;
  88. int code = obj_le(op - 1, op);
  89. if (code < 0)
  90. return code;
  91. make_bool(op - 1, code);
  92. pop(1);
  93. return 0;
  94. }
  95. /* <num1> <num2> lt <bool> */
  96. /* <str1> <str2> lt <bool> */
  97. int
  98. zlt(i_ctx_t *i_ctx_p)
  99. {
  100. os_ptr op = osp;
  101. int code = obj_le(op, op - 1);
  102. if (code < 0)
  103. return code;
  104. make_bool(op - 1, code ^ 1);
  105. pop(1);
  106. return 0;
  107. }
  108. /* <num1> <num2> .max <num> */
  109. /* <str1> <str2> .max <str> */
  110. private int
  111. zmax(i_ctx_t *i_ctx_p)
  112. {
  113. os_ptr op = osp;
  114. int code = obj_le(op - 1, op);
  115. if (code < 0)
  116. return code;
  117. if (code) {
  118. ref_assign(op - 1, op);
  119. }
  120. pop(1);
  121. return 0;
  122. }
  123. /* <num1> <num2> .min <num> */
  124. /* <str1> <str2> .min <str> */
  125. private int
  126. zmin(i_ctx_t *i_ctx_p)
  127. {
  128. os_ptr op = osp;
  129. int code = obj_le(op - 1, op);
  130. if (code < 0)
  131. return code;
  132. if (!code) {
  133. ref_assign(op - 1, op);
  134. }
  135. pop(1);
  136. return 0;
  137. }
  138. /* <bool1> <bool2> and <bool> */
  139. /* <int1> <int2> and <int> */
  140. int
  141. zand(i_ctx_t *i_ctx_p)
  142. {
  143. os_ptr op = osp;
  144. switch (r_type(op)) {
  145. case t_boolean:
  146. check_type(op[-1], t_boolean);
  147. op[-1].value.boolval &= op->value.boolval;
  148. break;
  149. case t_integer:
  150. check_type(op[-1], t_integer);
  151. op[-1].value.intval &= op->value.intval;
  152. break;
  153. default:
  154. return_op_typecheck(op);
  155. }
  156. pop(1);
  157. return 0;
  158. }
  159. /* <bool> not <bool> */
  160. /* <int> not <int> */
  161. int
  162. znot(i_ctx_t *i_ctx_p)
  163. {
  164. os_ptr op = osp;
  165. switch (r_type(op)) {
  166. case t_boolean:
  167. op->value.boolval = !op->value.boolval;
  168. break;
  169. case t_integer:
  170. op->value.intval = ~op->value.intval;
  171. break;
  172. default:
  173. return_op_typecheck(op);
  174. }
  175. return 0;
  176. }
  177. /* <bool1> <bool2> or <bool> */
  178. /* <int1> <int2> or <int> */
  179. int
  180. zor(i_ctx_t *i_ctx_p)
  181. {
  182. os_ptr op = osp;
  183. switch (r_type(op)) {
  184. case t_boolean:
  185. check_type(op[-1], t_boolean);
  186. op[-1].value.boolval |= op->value.boolval;
  187. break;
  188. case t_integer:
  189. check_type(op[-1], t_integer);
  190. op[-1].value.intval |= op->value.intval;
  191. break;
  192. default:
  193. return_op_typecheck(op);
  194. }
  195. pop(1);
  196. return 0;
  197. }
  198. /* <bool1> <bool2> xor <bool> */
  199. /* <int1> <int2> xor <int> */
  200. int
  201. zxor(i_ctx_t *i_ctx_p)
  202. {
  203. os_ptr op = osp;
  204. switch (r_type(op)) {
  205. case t_boolean:
  206. check_type(op[-1], t_boolean);
  207. op[-1].value.boolval ^= op->value.boolval;
  208. break;
  209. case t_integer:
  210. check_type(op[-1], t_integer);
  211. op[-1].value.intval ^= op->value.intval;
  212. break;
  213. default:
  214. return_op_typecheck(op);
  215. }
  216. pop(1);
  217. return 0;
  218. }
  219. /* <int> <shift> bitshift <int> */
  220. int
  221. zbitshift(i_ctx_t *i_ctx_p)
  222. {
  223. os_ptr op = osp;
  224. int shift;
  225. check_type(*op, t_integer);
  226. check_type(op[-1], t_integer);
  227. #define MAX_SHIFT (arch_sizeof_long * 8 - 1)
  228. if (op->value.intval < -MAX_SHIFT || op->value.intval > MAX_SHIFT)
  229. op[-1].value.intval = 0;
  230. #undef MAX_SHIFT
  231. else if ((shift = op->value.intval) < 0)
  232. op[-1].value.intval = ((ulong)(op[-1].value.intval)) >> -shift;
  233. else
  234. op[-1].value.intval <<= shift;
  235. pop(1);
  236. return 0;
  237. }
  238. /* ------ Extensions ------ */
  239. /* <obj1> <obj2> .identeq <bool> */
  240. private int
  241. zidenteq(i_ctx_t *i_ctx_p)
  242. {
  243. os_ptr op = osp;
  244. EQ_CHECK_READ(op - 1, check_op(2));
  245. EQ_CHECK_READ(op, DO_NOTHING);
  246. make_bool(op - 1, (obj_ident_eq(imemory, op - 1, op) ? 1 : 0));
  247. pop(1);
  248. return 0;
  249. }
  250. /* <obj1> <obj2> .identne <bool> */
  251. private int
  252. zidentne(i_ctx_t *i_ctx_p)
  253. {
  254. /* We'll just be lazy and use .identeq. */
  255. int code = zidenteq(i_ctx_p);
  256. if (!code)
  257. osp->value.boolval ^= 1;
  258. return code;
  259. }
  260. /* ------ Initialization procedure ------ */
  261. const op_def zrelbit_op_defs[] =
  262. {
  263. {"2and", zand},
  264. {"2bitshift", zbitshift},
  265. {"2eq", zeq},
  266. {"2ge", zge},
  267. {"2gt", zgt},
  268. {"2le", zle},
  269. {"2lt", zlt},
  270. {"2.max", zmax},
  271. {"2.min", zmin},
  272. {"2ne", zne},
  273. {"1not", znot},
  274. {"2or", zor},
  275. {"2xor", zxor},
  276. /* Extensions */
  277. {"2.identeq", zidenteq},
  278. {"2.identne", zidentne},
  279. op_def_end(0)
  280. };
  281. /* ------ Internal routines ------ */
  282. /* Compare two operands (both numeric, or both strings). */
  283. /* Return 1 if op[-1] <= op[0], 0 if op[-1] > op[0], */
  284. /* or a (negative) error code. */
  285. private int
  286. obj_le(register os_ptr op1, register os_ptr op)
  287. {
  288. switch (r_type(op1)) {
  289. case t_integer:
  290. switch (r_type(op)) {
  291. case t_integer:
  292. return (op1->value.intval <= op->value.intval);
  293. case t_real:
  294. return ((double)op1->value.intval <= op->value.realval);
  295. default:
  296. return_op_typecheck(op);
  297. }
  298. case t_real:
  299. switch (r_type(op)) {
  300. case t_real:
  301. return (op1->value.realval <= op->value.realval);
  302. case t_integer:
  303. return (op1->value.realval <= (double)op->value.intval);
  304. default:
  305. return_op_typecheck(op);
  306. }
  307. case t_string:
  308. check_read(*op1);
  309. check_read_type(*op, t_string);
  310. return (bytes_compare(op1->value.bytes, r_size(op1),
  311. op->value.bytes, r_size(op)) <= 0);
  312. default:
  313. return_op_typecheck(op1);
  314. }
  315. }