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 / extern.c < prev    next >
C/C++ Source or Header  |  2001-03-08  |  6KB  |  200 lines

  1. /* -*-C-*-
  2.  
  3. $Id: extern.c,v 9.38 2001/03/08 18:00:21 cph Exp $
  4.  
  5. Copyright (c) 1987-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. #include "scheme.h"
  23. #include "prims.h"
  24.  
  25. /* Mapping between the internal and external representations of
  26.    primitives and return addresses.  */
  27.  
  28. DEFINE_PRIMITIVE ("MAP-CODE-TO-MACHINE-ADDRESS", Prim_map_code_to_address, 2, 2,
  29.   "For return codes and primitives, this returns the internal\n\
  30. representation of the return address or primitive address given the\n\
  31. external representation.\n\
  32. \n\
  33. This accepts two arguments, TYPE-CODE and VALUE-CODE.  TYPE-CODE is\n\
  34. the microcode type of the object to be returned; it must be either a\n\
  35. return address or primitive procedure type.  VALUE-CODE is the index\n\
  36. number (i.e. external representation) of the desired result.")
  37. {
  38.   long tc, number;
  39.   PRIMITIVE_HEADER (2);
  40.   tc = (arg_nonnegative_integer (1));
  41.   number = (arg_nonnegative_integer (2));
  42.   switch (tc)
  43.   {
  44.     case TC_RETURN_CODE:
  45.       if (number > MAX_RETURN_CODE)
  46.     error_bad_range_arg (2);
  47.       PRIMITIVE_RETURN (MAKE_OBJECT (tc, number));
  48.  
  49.     case TC_PRIMITIVE:
  50.       if (number > (NUMBER_OF_PRIMITIVES ()))
  51.     error_bad_range_arg (2);
  52.       PRIMITIVE_RETURN (MAKE_PRIMITIVE_OBJECT (number));
  53.  
  54.     default:
  55.       error_bad_range_arg (1);
  56.   }
  57.   /*NOTREACHED*/
  58.   return (0);
  59. }
  60.  
  61. DEFINE_PRIMITIVE ("MAP-MACHINE-ADDRESS-TO-CODE", Prim_map_address_to_code, 2, 2,
  62.   "This is the inverse operation of `map-code-to-machine-address'.  Given\n\
  63. a machine ADDRESS and a TYPE-CODE (either return code or primitive\n\
  64. procedure), it finds the number for the external representation for\n\
  65. the internal address.")
  66. {
  67.   fast SCHEME_OBJECT tc;
  68.   fast SCHEME_OBJECT address;
  69.   PRIMITIVE_HEADER (2);
  70.   tc = (arg_nonnegative_integer (1));
  71.   address = (ARG_REF (2));
  72.   if ((OBJECT_TYPE (address)) != tc)
  73.     error_wrong_type_arg (2);
  74.   switch (tc)
  75.     {
  76.     case TC_RETURN_CODE:
  77.       {
  78.     fast long number = (OBJECT_DATUM (address));
  79.     if (number > MAX_RETURN_CODE)
  80.       error_bad_range_arg (2);
  81.     PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (number));
  82.       }
  83.  
  84.     case TC_PRIMITIVE:
  85.       PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (PRIMITIVE_NUMBER (address)));
  86.  
  87.     default:
  88.       error_bad_range_arg (1);
  89.     }
  90.   /*NOTREACHED*/
  91.   return (0);
  92. }
  93.  
  94. DEFINE_PRIMITIVE ("PRIMITIVE-PROCEDURE-ARITY", Prim_primitive_procedure_arity, 1, 1,
  95.   "Given a primitive procedure, returns the number of arguments it requires.")
  96. {
  97.   PRIMITIVE_HEADER (1);
  98.   CHECK_ARG (1, PRIMITIVE_P);
  99.   {
  100.     fast SCHEME_OBJECT primitive = (ARG_REF (1));
  101.     if ((PRIMITIVE_NUMBER (primitive))
  102.     > ((unsigned long) (NUMBER_OF_PRIMITIVES ())))
  103.       error_bad_range_arg (1);
  104.     PRIMITIVE_RETURN (LONG_TO_FIXNUM (PRIMITIVE_ARITY (primitive)));
  105.   }
  106. }
  107.  
  108. DEFINE_PRIMITIVE ("PRIMITIVE-PROCEDURE-DOCUMENTATION",
  109.           Prim_primitive_procedure_doc, 1, 1,
  110.   "Given a primitive procedure, return its documentation string.")
  111. {
  112.   PRIMITIVE_HEADER (1);
  113.   CHECK_ARG (1, PRIMITIVE_P);
  114.   {
  115.     SCHEME_OBJECT primitive = (ARG_REF (1));
  116.     if ((PRIMITIVE_NUMBER (primitive))
  117.     > ((unsigned long) (NUMBER_OF_PRIMITIVES ())))
  118.       error_bad_range_arg (1);
  119.     {
  120.       CONST char * answer = (PRIMITIVE_DOCUMENTATION (primitive));
  121.       PRIMITIVE_RETURN
  122.     ((answer == 0)
  123.      ? SHARP_F
  124.      : (char_pointer_to_string ((unsigned char *) answer)));
  125.     }
  126.   }
  127. }
  128.  
  129. DEFINE_PRIMITIVE ("GET-PRIMITIVE-COUNTS", Prim_get_primitive_counts, 0, 0,
  130.   "Return a pair of integers which are the number of primitive procedures.\n\
  131. The car is the count of defined primitives;\n\
  132. the cdr is the count of undefined primitives that are referenced.")
  133. {
  134.   PRIMITIVE_HEADER (0);
  135.   PRIMITIVE_RETURN
  136.     (cons ((LONG_TO_UNSIGNED_FIXNUM ((NUMBER_OF_PRIMITIVES ()))),
  137.        (LONG_TO_UNSIGNED_FIXNUM (0))));
  138. }
  139.  
  140. DEFINE_PRIMITIVE ("GET-PRIMITIVE-NAME", Prim_get_primitive_name, 1, 1,
  141.   "Return the (string) name of PRIMITIVE-PROCEDURE.")
  142. {
  143.   PRIMITIVE_HEADER (1);
  144.   {
  145.     fast SCHEME_OBJECT primitive = (ARG_REF (1));
  146.     if (! ((PRIMITIVE_P (primitive)) || (FIXNUM_P (primitive))))
  147.       error_wrong_type_arg (1);
  148.     {
  149.       fast long number = (PRIMITIVE_NUMBER (primitive));
  150.       if ((number < 0) || (number > (NUMBER_OF_PRIMITIVES ())))
  151.     error_bad_range_arg (1);
  152.       PRIMITIVE_RETURN
  153.     (char_pointer_to_string ((unsigned char *)
  154.                  (PRIMITIVE_NAME (primitive))));
  155.     }
  156.   }
  157. }
  158.  
  159. DEFINE_PRIMITIVE ("GET-PRIMITIVE-ADDRESS", Prim_get_primitive_address, 2, 2,
  160.   "Given a symbol NAME, return the primitive object of that name.\n\
  161. ARITY is the number of arguments which the primitive should expect.\n\
  162. If ARITY is #F, #F is returned if the primitive is not implemented,\n\
  163. even if the name already exists.\n\
  164. If ARITY is an integer, a primitive object will always be returned,\n\
  165. whether the corresponding primitive is implemented or not.")
  166. {
  167.   fast SCHEME_OBJECT name;
  168.   fast SCHEME_OBJECT arity_arg;
  169.   extern SCHEME_OBJECT EXFUN
  170.     (find_primitive, (SCHEME_OBJECT, Boolean, Boolean, int));
  171.   Boolean intern_p, allow_p;
  172.   long arity;
  173.   PRIMITIVE_HEADER (2);
  174.   CHECK_ARG (1, SYMBOL_P);
  175.   name = (ARG_REF (1));
  176.   TOUCH_IN_PRIMITIVE ((ARG_REF (2)), arity_arg);
  177.   if (arity_arg == SHARP_F)
  178.     {
  179.       allow_p = false;
  180.       intern_p = false;
  181.       arity = UNKNOWN_PRIMITIVE_ARITY;
  182.     }
  183.   else if (arity_arg == SHARP_T)
  184.     {
  185.       allow_p = true;
  186.       intern_p = false;
  187.       arity = UNKNOWN_PRIMITIVE_ARITY;
  188.     }
  189.   else
  190.     {
  191.       CHECK_ARG(2, FIXNUM_P);
  192.       allow_p = true;
  193.       intern_p = true;
  194.       arity = (FIXNUM_TO_LONG (arity_arg));
  195.     }
  196.   PRIMITIVE_RETURN
  197.     (find_primitive
  198.      ((FAST_MEMORY_REF (name, SYMBOL_NAME)), intern_p, allow_p, arity));
  199. }
  200.