home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / microcode / prim.c < prev    next >
C/C++ Source or Header  |  1999-01-02  |  8KB  |  256 lines

  1. /* -*-C-*-
  2.  
  3. $Id: prim.c,v 9.39 1999/01/02 06:11:34 cph Exp $
  4.  
  5. Copyright (c) 1988-1999 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. /* The leftovers ... primitives that don't seem to belong elsewhere. */
  23.  
  24. #include "scheme.h"
  25. #include "prims.h"
  26.  
  27. /* Low level object manipulation */
  28.  
  29. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-TYPE", Prim_prim_obj_type, 1, 1,
  30.   "Return the type code of OBJECT as an unsigned integer.")
  31. {
  32.   PRIMITIVE_HEADER (1);
  33.   PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (OBJECT_TYPE (ARG_REF (1))));
  34. }
  35.  
  36. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-GC-TYPE", Prim_prim_obj_gc_type, 1, 1,
  37.   "Return an unsigned integer indicating the GC type of the object.")
  38. {
  39.   PRIMITIVE_HEADER (1);
  40.   PRIMITIVE_RETURN (LONG_TO_FIXNUM (GC_Type_Map [OBJECT_TYPE (ARG_REF (1))]));
  41. }
  42.  
  43. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-TYPE?", Prim_prim_obj_type_p, 2, 2,
  44.   "Return #T if TYPE-CODE is OBJECT's type code, else #F.")
  45. {
  46.   PRIMITIVE_HEADER (2);
  47.   PRIMITIVE_RETURN
  48.     (BOOLEAN_TO_OBJECT
  49.      (((long) (OBJECT_TYPE (ARG_REF (2))))
  50.       == (arg_index_integer (1, (MAX_TYPE_CODE + 1)))));
  51. }
  52.  
  53. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-DATUM", Prim_prim_obj_datum, 1, 1,
  54.   "Return the datum part of OBJECT as an unsigned integer.")
  55. {
  56.   PRIMITIVE_HEADER (1);
  57.   PRIMITIVE_RETURN (long_to_integer (OBJECT_DATUM (ARG_REF (1))));
  58. }
  59.  
  60. DEFINE_PRIMITIVE ("MAKE-NON-POINTER-OBJECT", Prim_make_non_pointer_object, 1, 1,
  61.   "Convert the unsigned integer NUMBER into a fixnum.\n\
  62. Assert: (= (OBJECT-DATUM (MAKE-NON-POINTER-OBJECT X)) X).")
  63. {
  64.   PRIMITIVE_HEADER (1);
  65.   PRIMITIVE_RETURN
  66.     (LONG_TO_UNSIGNED_FIXNUM
  67.      (arg_index_integer (1, (((unsigned long) 1) << DATUM_LENGTH))));
  68. }
  69.  
  70. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-SET-TYPE", Prim_prim_obj_set_type, 2, 2,
  71.   "Return a new object made from TYPE-CODE and the datum part of OBJECT.")
  72. {
  73.   PRIMITIVE_HEADER (2);
  74.   PRIMITIVE_RETURN
  75.     (OBJECT_NEW_TYPE
  76.      ((arg_index_integer (1, (MAX_TYPE_CODE + 1))), (ARG_REF (2))));
  77. }
  78.  
  79. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-EQ?", Prim_prim_obj_eq_p, 2, 2, 0)
  80. {
  81.   PRIMITIVE_HEADER (2);
  82.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT ((ARG_REF (1)) == (ARG_REF (2))));
  83. }
  84.  
  85. /* Low level memory references.
  86.  
  87.    Many primitives can be built out of these, and eventually should be.
  88.    These are extremely unsafe, since there is no consistency checking.
  89.    In particular, they are not gc-safe: You can screw yourself royally
  90.    by using them.  */
  91.  
  92. /* (PRIMITIVE-OBJECT-REF OBJECT INDEX)
  93.    Fetches the index'ed slot in object.
  94.    Performs no type checking on object.  */
  95.  
  96. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-REF", Prim_prim_obj_ref, 2, 2, 0)
  97. {
  98.   PRIMITIVE_HEADER (2);
  99.   PRIMITIVE_RETURN (MEMORY_REF ((ARG_REF (1)), (arg_nonnegative_integer (2))));
  100. }
  101.  
  102. /* (PRIMITIVE-OBJECT-SET! OBJECT INDEX VALUE)
  103.    Stores value in the index'ed slot in object.
  104.    Performs no type checking on object.  */
  105.  
  106. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-SET!", Prim_prim_obj_set, 3, 3, 0)
  107. {
  108.   PRIMITIVE_HEADER (3);
  109.   MEMORY_SET ((ARG_REF (1)), (arg_nonnegative_integer (2)), (ARG_REF (3)));
  110.   PRIMITIVE_RETURN (UNSPECIFIC);
  111. }
  112.  
  113. /* Safe versions of the object manipulators.
  114.    These touch their arguments, and provide GC safety tests.  */
  115.  
  116. DEFINE_PRIMITIVE ("OBJECT-TYPE", Prim_object_type, 1, 1, 0)
  117. {
  118.   fast SCHEME_OBJECT object;
  119.   PRIMITIVE_HEADER (1);
  120.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), object);
  121.   PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (OBJECT_TYPE (object)));
  122. }
  123.  
  124. DEFINE_PRIMITIVE ("OBJECT-GC-TYPE", Prim_object_gc_type, 1, 1, 0)
  125. {
  126.   fast SCHEME_OBJECT object;
  127.   PRIMITIVE_HEADER (1);
  128.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), object);
  129.   PRIMITIVE_RETURN (LONG_TO_FIXNUM (GC_Type (object)));
  130. }
  131.  
  132. DEFINE_PRIMITIVE ("OBJECT-TYPE?", Prim_object_type_p, 2, 2, 0)
  133. {
  134.   fast SCHEME_OBJECT object;
  135.   PRIMITIVE_HEADER (2);
  136.   TOUCH_IN_PRIMITIVE ((ARG_REF (2)), object);
  137.   PRIMITIVE_RETURN
  138.     (BOOLEAN_TO_OBJECT
  139.      (((long) (OBJECT_TYPE (object)))
  140.       == (arg_index_integer (1, (MAX_TYPE_CODE + 1)))));
  141. }
  142.  
  143. DEFINE_PRIMITIVE ("OBJECT-DATUM", Prim_object_datum, 1, 1, 0)
  144. {
  145.   fast SCHEME_OBJECT object;
  146.   PRIMITIVE_HEADER (1);
  147.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), object);
  148.   PRIMITIVE_RETURN (long_to_integer (OBJECT_DATUM (object)));
  149. }
  150.  
  151. DEFINE_PRIMITIVE ("OBJECT-SET-TYPE", Prim_object_set_type, 2, 2, 0)
  152. {
  153.   fast long type_code;
  154.   fast SCHEME_OBJECT object;
  155.   PRIMITIVE_HEADER (2);
  156.   type_code = (arg_index_integer (1, (MAX_TYPE_CODE + 1)));
  157.   TOUCH_IN_PRIMITIVE ((ARG_REF (2)), object);
  158.   {
  159.     fast long gc_type_code;
  160.  
  161.     gc_type_code = (GC_Type_Map [type_code]);
  162.     if ((gc_type_code == GC_Undefined) ||
  163.     (! ((gc_type_code == GC_Non_Pointer) ||
  164.         (gc_type_code == (GC_Type (object))))))
  165.       error_bad_range_arg (1);
  166.   }
  167.   PRIMITIVE_RETURN (OBJECT_NEW_TYPE (type_code, object));
  168. }
  169.  
  170. /* (EQ? OBJECT-1 OBJECT-2)
  171.    Returns #T if the two objects have the same type code and datum.
  172.    Returns #F otherwise.
  173.    Touches both arguments.  */
  174.  
  175. DEFINE_PRIMITIVE ("EQ?", Prim_eq, 2, 2, 0)
  176. {
  177.   fast SCHEME_OBJECT object_1;
  178.   fast SCHEME_OBJECT object_2;
  179.   PRIMITIVE_HEADER (2);
  180.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), object_1);
  181.   TOUCH_IN_PRIMITIVE ((ARG_REF (2)), object_2);
  182.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (object_1 == object_2));
  183. }
  184.  
  185. /* (NOT OBJECT)
  186.    Returns #T if OBJECT is #F.  Otherwise returns #F.  This is
  187.    the primitive known as NOT and FALSE? in Scheme.
  188.    Touches the argument.  */
  189.  
  190. DEFINE_PRIMITIVE ("NOT", Prim_not, 1, 1, 0)
  191. {
  192.   fast SCHEME_OBJECT object;
  193.   PRIMITIVE_HEADER (1);
  194.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), object);
  195.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (object == SHARP_F));
  196. }
  197.  
  198. /* (NULL? OBJECT)
  199.    Returns #T if OBJECT is '().  Otherwise returns #F.
  200.    Touches the argument.  */
  201.  
  202. DEFINE_PRIMITIVE ("NULL?", Prim_null_p, 1, 1, 0)
  203. {
  204.   fast SCHEME_OBJECT object;
  205.   PRIMITIVE_HEADER (1);
  206.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), object);
  207.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (object == EMPTY_LIST));
  208. }
  209.  
  210. /* Cells */
  211.  
  212. /* (MAKE-CELL CONTENTS)
  213.    Creates a cell with contents CONTENTS. */
  214.  
  215. DEFINE_PRIMITIVE ("MAKE-CELL", Prim_make_cell, 1, 1, 0)
  216. {
  217.   PRIMITIVE_HEADER (1);
  218.   Primitive_GC_If_Needed (1);
  219.   (*Free++) = (ARG_REF (1));
  220.   PRIMITIVE_RETURN (MAKE_POINTER_OBJECT (TC_CELL, (Free - 1)));
  221. }
  222.  
  223. /* (CELL? OBJECT)
  224.    Returns #T if OBJECT is a cell, else #F.  */
  225.  
  226. DEFINE_PRIMITIVE ("CELL?", Prim_cell_p, 1, 1, 0)
  227. {
  228.   PRIMITIVE_HEADER (1);
  229.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (CELL_P (ARG_REF (1))));
  230. }
  231.  
  232. /* (CELL-CONTENTS CELL)
  233.    Returns the contents of the cell CELL.  */
  234.  
  235. DEFINE_PRIMITIVE ("CELL-CONTENTS", Prim_cell_contents, 1, 1, 0)
  236. {
  237.   PRIMITIVE_HEADER (1);
  238.   PRIMITIVE_RETURN (MEMORY_REF ((CELL_ARG (1)), CELL_CONTENTS));
  239. }
  240.  
  241. /* (SET-CELL-CONTENTS! CELL OBJECT)
  242.    Stores OBJECT as contents of CELL.
  243.    Returns the previous contents of CELL. */
  244.  
  245. DEFINE_PRIMITIVE ("SET-CELL-CONTENTS!", Prim_set_cell_contents, 2, 2, 0)
  246. {
  247.   fast SCHEME_OBJECT cell;
  248.   fast SCHEME_OBJECT object;
  249.   PRIMITIVE_HEADER (2);
  250.   cell = (CELL_ARG (1));
  251.   object = (ARG_REF (2));
  252.   SIDE_EFFECT_IMPURIFY (cell, object);
  253.   MEMORY_SET (cell, CELL_CONTENTS, object);
  254.   PRIMITIVE_RETURN (UNSPECIFIC);
  255. }
  256.