home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 018.lha / sources / object.h < prev    next >
C/C++ Source or Header  |  1986-10-19  |  5KB  |  167 lines

  1. /*
  2.         Little Smalltalk object definitions
  3. */
  4. # include "env.h"
  5. /*
  6.     for objects the inst_var array is actually made as large as
  7.     necessary (as large as the size field).  since C does not do
  8.     subscript bounds checking array indexing can be used
  9. */
  10.  
  11. struct obj_struct {
  12.         int                   ref_count;
  13.     int                   size;
  14.         struct class_struct   *class;
  15.         struct obj_struct     *super_obj;
  16.         struct obj_struct     *inst_var[1];
  17.         };
  18.  
  19. /*
  20.     for classes
  21.         c_size = CLASSSIZE
  22.  
  23.         class_name and super_class should be SYMBOLs
  24.         containing the names of the class and superclass,
  25.         respectively.
  26.  
  27.         c_inst_vars should be an array of symbols, containing the
  28.         names of the instance variables
  29.  
  30.         context size is the size of the context that should be
  31.         created each time a message is sent to objects of this
  32.         class.
  33.  
  34.         message_names should be an array of symbols, corresponding
  35.         to the messages accepted by objects of this class.
  36.  
  37.         methods should be an array of arrays, each element being a
  38.         two element array of bytecodes and literals.
  39. */
  40.  
  41. struct class_struct {
  42.     int            c_ref_count;
  43.     int            c_size;
  44.     struct obj_struct    *class_name;
  45.     struct obj_struct    *super_class;
  46.     struct obj_struct    *file_name;
  47.     struct obj_struct    *c_inst_vars;
  48.     int            context_size;
  49.     struct obj_struct    *message_names;
  50.     struct obj_struct    *methods;
  51.     int            stack_max;
  52.     };
  53.  
  54. typedef struct class_struct class;
  55. typedef struct obj_struct object;
  56.  
  57. /*
  58.     objects with non-object value (classes, integers, etc) have a
  59.     negative size field, the particular value being used to indicate
  60.     the type of object (the class field cannot be used for this purpose
  61.     since all classes, even those for built in objects, can be redefined)
  62.  
  63.     check_bltin is a macro that tests the size field for a particular
  64.     value.  it is used to define other macros, such as is_class, that
  65.     test each particular type of object.
  66.  
  67.     The following classes are builtin
  68.  
  69.         Block
  70.         ByteArray
  71.         Char 
  72.         Class
  73.         Float
  74.         Integer
  75.         Interpreter
  76.         String
  77.         Symbol
  78. */
  79.  
  80. # define BLOCKSIZE     -83
  81. # define BYTEARRAYSIZE     -567
  82. # define CHARSIZE     -33
  83. # define CLASSSIZE     -3
  84. # define FILESIZE     -5
  85. # define FLOATSIZE     -31415
  86. # define INTEGERSIZE     -17
  87. # define INTERPSIZE     -15
  88. # define PROCSIZE      -100
  89. # define STRINGSIZE     -258
  90. # define SYMBOLSIZE     -14
  91.  
  92. # define is_bltin(x) (x && (((object *) x)->size < 0))
  93. # define check_bltin(obj, type) (obj && (((object *) obj)->size == type))
  94.  
  95. # define is_block(x)        check_bltin(x, BLOCKSIZE)
  96. # define is_bytearray(x)    check_bltin(x, BYTEARRAYSIZE)
  97. # define is_character(x)    check_bltin(x, CHARSIZE)
  98. # define is_class(x)        check_bltin(x, CLASSSIZE)
  99. # define is_file(x)        check_bltin(x, FILESIZE)
  100. # define is_float(x)        check_bltin(x, FLOATSIZE)
  101. # define is_integer(x)        check_bltin(x, INTEGERSIZE)
  102. # define is_interpreter(x)    check_bltin(x, INTERPSIZE)
  103. # define is_process(p)         check_bltin(p, PROCSIZE)
  104. # define is_string(x)        check_bltin(x, STRINGSIZE)
  105. # define is_symbol(x)        check_bltin(x, SYMBOLSIZE)
  106.  
  107. /*
  108.     mstruct is used (via casts) to store linked lists of structures of
  109.     various types for memory saving and recovering
  110. */
  111.  
  112. struct mem_struct {
  113.     struct mem_struct *mlink;
  114.     };
  115.  
  116. typedef struct mem_struct mstruct;
  117.  
  118. /*
  119.     sassign assigns val to obj, which should not have a valid
  120.     value in it already.
  121.     assign decrements an existing val field first, then assigns.
  122.     note this will not work for assign(x,x) if x ref count is 1.
  123.     safeassign, although producing less efficient code, will work even
  124.     in this case
  125. */
  126. # define sassign(obj, val) obj_inc((object *) (obj = val))
  127. # define assign(obj, val)  {obj_dec((object *) obj); sassign(obj, val);}
  128. # define safeassign(obj, val) {obj_inc((object *) val); obj_dec((object *) obj); obj = val; }
  129.  
  130. /* structalloc calls alloc to allocate a block of memory 
  131.    for a structure and casts the returned
  132.    pointer to the appropriate type */
  133. # define structalloc(type) (type *) o_alloc(sizeof(type))
  134.  
  135. /*
  136.     if INLINE is defined ( see env.h ) , inline code will be generated 
  137.     for object increments.  inline code is generally faster, but
  138.     larger than using subroutine calls for incs and decs
  139. */
  140.  
  141. extern int  n_incs, n_decs;
  142.  
  143. # ifdef INLINE
  144.  
  145. # define obj_inc(x) n_incs++, (x)->ref_count++
  146. extern object *_dx;
  147. # define obj_dec(x) {n_decs++; if (--((_dx=x)->ref_count) <= 0) ob_dec(_dx);}
  148.  
  149. # endif
  150.  
  151. extern char   *o_alloc();    /* allocate a block of memory */
  152. extern object *new_inst();    /* make a new instance of a class */
  153. extern object *new_sinst();    /* an internal (system) version of new_inst*/
  154. extern object *new_obj();    /* allocate a new object */
  155. extern object *new_array();    /* make a new array */
  156. extern object *primitive();    /* perform a primitive operation */
  157.  
  158. extern object *o_nil;        /* current value of pseudo variable nil */
  159. extern object *o_true;        /* current value of pseudo variable true */
  160. extern object *o_false;        /* current value of pseudo variable false */
  161. extern object *o_smalltalk;    /* current value of pseudo var smalltalk */
  162.  
  163. extern int debug;        /* debugging toggle */
  164.  
  165. /* reference count macro, used during debugging */
  166. # define rc(x) ((object *)x)->ref_count
  167.