cb22561995-10-11Fredrik Hübinette (Hubbe) /*\
06983f1996-09-22Fredrik Hübinette (Hubbe) ||| This file a part of Pike, and is copyright by Fredrik Hubinette ||| Pike is distributed as GPL (General Public License)
cb22561995-10-11Fredrik Hübinette (Hubbe) ||| See the files COPYING and DISCLAIMER for more information. \*/
24ddc71998-03-28Henrik Grubbström (Grubba)  /*
7e97c31999-01-21Fredrik Hübinette (Hubbe)  * $Id: svalue.h,v 1.20 1999/01/21 09:15:17 hubbe Exp $
24ddc71998-03-28Henrik Grubbström (Grubba)  */
5267b71995-08-09Fredrik Hübinette (Hubbe) #ifndef SVALUE_H #define SVALUE_H
bed9601997-05-19Fredrik Hübinette (Hubbe) #include "global.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) 
dfa5b31996-12-05Fredrik Hübinette (Hubbe) #ifndef STRUCT_ARRAY_DECLARED #define STRUCT_ARRAY_DECLARED
5267b71995-08-09Fredrik Hübinette (Hubbe) struct array;
dfa5b31996-12-05Fredrik Hübinette (Hubbe) #endif #ifndef STRUCT_MAPPING_DECLARED #define STRUCT_MAPPING_DECLARED
5267b71995-08-09Fredrik Hübinette (Hubbe) struct mapping;
dfa5b31996-12-05Fredrik Hübinette (Hubbe) #endif #ifndef STRUCT_MULTISET_DECLARED #define STRUCT_MULTISET_DECLARED
06983f1996-09-22Fredrik Hübinette (Hubbe) struct multiset;
dfa5b31996-12-05Fredrik Hübinette (Hubbe) #endif #ifndef STRUCT_OBJECT_DECLARED #define STRUCT_OBJECT_DECLARED
5267b71995-08-09Fredrik Hübinette (Hubbe) struct object;
dfa5b31996-12-05Fredrik Hübinette (Hubbe) #endif #ifndef STRUCT_PROGRAM_DECLARED #define STRUCT_PROGRAM_DECLARED
5267b71995-08-09Fredrik Hübinette (Hubbe) struct program;
dfa5b31996-12-05Fredrik Hübinette (Hubbe) #endif #ifndef STRUCT_PIKE_STRING_DECLARED #define STRUCT_PIKE_STRING_DECLARED
06983f1996-09-22Fredrik Hübinette (Hubbe) struct pike_string;
dfa5b31996-12-05Fredrik Hübinette (Hubbe) #endif #ifndef STRUCT_CALLABLE_DECLARED #define STRUCT_CALLABLE_DECLARED
5267b71995-08-09Fredrik Hübinette (Hubbe) struct callable;
dfa5b31996-12-05Fredrik Hübinette (Hubbe) #endif
5267b71995-08-09Fredrik Hübinette (Hubbe)  struct processing { struct processing *next; void *pointer_a, *pointer_b; }; union anything { struct callable *efun; struct array *array; struct mapping *mapping;
06983f1996-09-22Fredrik Hübinette (Hubbe)  struct multiset *multiset;
5267b71995-08-09Fredrik Hübinette (Hubbe)  struct object *object; struct program *program;
06983f1996-09-22Fredrik Hübinette (Hubbe)  struct pike_string *string;
5267b71995-08-09Fredrik Hübinette (Hubbe)  INT32 *refs;
8a222b1998-02-27Fredrik Hübinette (Hubbe)  INT_TYPE integer;
5267b71995-08-09Fredrik Hübinette (Hubbe)  FLOAT_TYPE float_number; struct svalue *lval; /* only used on stack */ union anything *short_lval; /* only used on stack */ };
dfa5b31996-12-05Fredrik Hübinette (Hubbe) #ifndef STRUCT_SVALUE_DECLARED #define STRUCT_SVALUE_DECLARED #endif
5267b71995-08-09Fredrik Hübinette (Hubbe) struct svalue {
39e0bd1996-06-09Fredrik Hübinette (Hubbe)  unsigned INT16 type;
bdb5091996-09-25Fredrik Hübinette (Hubbe)  unsigned INT16 subtype;
5267b71995-08-09Fredrik Hübinette (Hubbe)  union anything u; }; #define T_ARRAY 0 #define T_MAPPING 1
06983f1996-09-22Fredrik Hübinette (Hubbe) #define T_MULTISET 2
5267b71995-08-09Fredrik Hübinette (Hubbe) #define T_OBJECT 3 #define T_FUNCTION 4 #define T_PROGRAM 5 #define T_STRING 6 #define T_FLOAT 7 #define T_INT 8
3c04e81997-03-13Fredrik Hübinette (Hubbe) #define T_VOID 16 #define T_MANY 17
9e52381998-03-01Fredrik Hübinette (Hubbe) #define T_ASSIGN 245
39e0bd1996-06-09Fredrik Hübinette (Hubbe) #define T_DELETED 246
bce86c1996-02-25Fredrik Hübinette (Hubbe) #define T_NOT 247 #define T_AND 248
0d202a1995-10-20Fredrik Hübinette (Hubbe) #define T_UNKNOWN 249
5267b71995-08-09Fredrik Hübinette (Hubbe) #define T_OR 251 #define T_SHORT_LVALUE 252 #define T_LVALUE 253
2a32691998-01-31Fredrik Hübinette (Hubbe) #define T_ARRAY_LVALUE 254
5267b71995-08-09Fredrik Hübinette (Hubbe) #define T_MIXED 255 #define BIT_ARRAY (1<<T_ARRAY) #define BIT_MAPPING (1<<T_MAPPING)
06983f1996-09-22Fredrik Hübinette (Hubbe) #define BIT_MULTISET (1<<T_MULTISET)
5267b71995-08-09Fredrik Hübinette (Hubbe) #define BIT_OBJECT (1<<T_OBJECT) #define BIT_FUNCTION (1<<T_FUNCTION) #define BIT_PROGRAM (1<<T_PROGRAM) #define BIT_STRING (1<<T_STRING) #define BIT_INT (1<<T_INT) #define BIT_FLOAT (1<<T_FLOAT)
3c04e81997-03-13Fredrik Hübinette (Hubbe) /* Used to signify that this array might not be finished yet */
4eb50a1996-05-16Fredrik Hübinette (Hubbe) /* garbage collect uses this */ #define BIT_UNFINISHED (1<<15)
3c04e81997-03-13Fredrik Hübinette (Hubbe) /* This is only used in typechecking to signify that this * argument may be omitted. */ #define BIT_VOID (1<< T_VOID) /* This is used in typechecking to signify that the rest of the * arguments has to be of this type. */ #define BIT_MANY (1 << T_MANY)
5267b71995-08-09Fredrik Hübinette (Hubbe) #define BIT_NOTHING 0
4eb50a1996-05-16Fredrik Hübinette (Hubbe) #define BIT_MIXED 0x7fff
624d091996-02-24Fredrik Hübinette (Hubbe) #define BIT_BASIC (BIT_INT|BIT_FLOAT|BIT_STRING)
624d091996-02-24Fredrik Hübinette (Hubbe) 
7e97c31999-01-21Fredrik Hübinette (Hubbe) /* Max type which contains svalues */ #define MAX_COMPLEX T_PROGRAM
5267b71995-08-09Fredrik Hübinette (Hubbe) /* Max type with ref count */ #define MAX_REF_TYPE T_STRING /* Max type handled by svalue primitives */ #define MAX_TYPE T_INT #define NUMBER_NUMBER 0 #define NUMBER_UNDEFINED 1 #define NUMBER_DESTRUCTED 2
bdb5091996-09-25Fredrik Hübinette (Hubbe) #define FUNCTION_BUILTIN USHRT_MAX
bce86c1996-02-25Fredrik Hübinette (Hubbe) #define is_gt(a,b) is_lt(b,a)
07c0731996-06-21Fredrik Hübinette (Hubbe) #define IS_ZERO(X) ((X)->type==T_INT?(X)->u.integer==0:(1<<(X)->type)&(BIT_OBJECT|BIT_FUNCTION)?!svalue_is_true(X):0)
5267b71995-08-09Fredrik Hübinette (Hubbe) 
3495fe1997-02-06Fredrik Hübinette (Hubbe) #define IS_UNDEFINED(X) ((X)->type==T_INT&&!(X)->u.integer&&(X)->subtype==1)
5267b71995-08-09Fredrik Hübinette (Hubbe) #define check_destructed(S) \ do{ \ struct svalue *_s=(S); \ if((_s->type == T_OBJECT || _s->type==T_FUNCTION) && !_s->u.object->prog) { \ free_object(_s->u.object); \ _s->type = T_INT; \ _s->subtype = NUMBER_DESTRUCTED ; \ _s->u.integer = 0; \ } \ }while(0)
453f4c1996-08-12Fredrik Hübinette (Hubbe) /* var MUST be a variable!!! */ #define safe_check_destructed(var) do{ \ if((var->type == T_OBJECT || var->type==T_FUNCTION) && !var->u.object->prog) \ var=&dest_ob_zero; \ }while(0)
5267b71995-08-09Fredrik Hübinette (Hubbe) #define check_short_destructed(U,T) \ do{ \ union anything *_u=(U); \ if(( (1<<(T)) & (BIT_OBJECT | BIT_FUNCTION) ) && \ _u->object && !_u->object->prog) { \ free_object(_u->object); \ _u->object = 0; \ } \ }while(0)
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
2a32691998-01-31Fredrik Hübinette (Hubbe) #define check_type(T) if(T > MAX_TYPE && T!=T_LVALUE && T!=T_SHORT_LVALUE && T!=T_VOID && T!=T_DELETED && T!=T_ARRAY_LVALUE) fatal("Type error\n")
5267b71995-08-09Fredrik Hübinette (Hubbe) #define check_refs(S) if((S)->type < MAX_REF_TYPE && (!(S)->u.refs || (S)->u.refs[0] < 0)) fatal("Svalue to object without references.\n") #define check_refs2(S,T) if((T) < MAX_REF_TYPE && (S)->refs && (S)->refs[0] <= 0) fatal("Svalue to object without references.\n")
8c83371998-04-16Fredrik Hübinette (Hubbe) #ifdef DEBUG_MALLOC #define add_ref(X) ((INT32 *)debug_malloc_pass( &((X)->refs)))[0]++ #else #define add_ref(X) (X)->refs++ #endif
5267b71995-08-09Fredrik Hübinette (Hubbe) #else #define check_type(T) #define check_refs(S) #define check_refs2(S,T)
8c83371998-04-16Fredrik Hübinette (Hubbe) #define add_ref(X) (X)->refs++
5267b71995-08-09Fredrik Hübinette (Hubbe)  #endif
3c0c281998-01-26Fredrik Hübinette (Hubbe) #define free_svalue(X) do { struct svalue *_s=(X); check_type(_s->type); check_refs(_s); if(_s->type<=MAX_REF_TYPE) { debug_malloc_touch(_s->u.refs); if(--*(_s->u.refs) <=0) really_free_svalue(_s); } }while(0)
98af0d1998-04-07Fredrik Hübinette (Hubbe) #define free_short_svalue(X,T) do { union anything *_s=(X); TYPE_T _t=(T); check_type(_t); check_refs2(_s,_t); if(_t<=MAX_REF_TYPE && _s->refs) if(--*(_s->refs) <= 0) really_free_short_svalue(_s,_t); }while(0)
3c0c281998-01-26Fredrik Hübinette (Hubbe) #define assign_svalue_no_free(X,Y) do { struct svalue _tmp, *_to=(X), *_from=(Y); check_type(_from->type); check_refs(_from); *_to=_tmp=*_from; if(_tmp.type <= MAX_REF_TYPE) { debug_malloc_touch(_tmp.u.refs); _tmp.u.refs[0]++; } }while(0)
d3d1721997-01-31Fredrik Hübinette (Hubbe) #define assign_svalue(X,Y) do { struct svalue *_to2=(X), *_from2=(Y); free_svalue(_to2); assign_svalue_no_free(_to2, _from2); }while(0)
453f4c1996-08-12Fredrik Hübinette (Hubbe) extern struct svalue dest_ob_zero;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
07ae471998-04-23Fredrik Hübinette (Hubbe) #ifdef DEBUG_MALLOC #define LINE_ARGS , int line, char * file #define free_svalues(X,Y,Z) debug_free_svalues((X),(Y),(Z),__LINE__,__FILE__) #else #define LINE_ARGS #define free_svalues(X,Y,Z) debug_free_svalues((X),(Y),(Z)) #endif
5267b71995-08-09Fredrik Hübinette (Hubbe) /* Prototypes begin here */
98af0d1998-04-07Fredrik Hübinette (Hubbe) void really_free_short_svalue(union anything *s, TYPE_T type);
d3d1721997-01-31Fredrik Hübinette (Hubbe) void really_free_svalue(struct svalue *s);
569d5e1998-09-18Fredrik Hübinette (Hubbe) void do_free_svalue(struct svalue *s);
07ae471998-04-23Fredrik Hübinette (Hubbe) void debug_free_svalues(struct svalue *s,INT32 num, INT32 type_hint LINE_ARGS);
5267b71995-08-09Fredrik Hübinette (Hubbe) void assign_svalues_no_free(struct svalue *to, struct svalue *from,
fc76951996-02-17Fredrik Hübinette (Hubbe)  INT32 num, INT32 type_hint);
99946c1996-02-17Fredrik Hübinette (Hubbe) void assign_svalues(struct svalue *to, struct svalue *from, INT32 num, TYPE_FIELD types);
5267b71995-08-09Fredrik Hübinette (Hubbe) void assign_to_short_svalue(union anything *u, TYPE_T type, struct svalue *s); void assign_to_short_svalue_no_free(union anything *u,
fc76951996-02-17Fredrik Hübinette (Hubbe)  TYPE_T type, struct svalue *s);
5267b71995-08-09Fredrik Hübinette (Hubbe) void assign_from_short_svalue_no_free(struct svalue *s,
569d5e1998-09-18Fredrik Hübinette (Hubbe)  union anything *u, TYPE_T type);
5267b71995-08-09Fredrik Hübinette (Hubbe) void assign_short_svalue_no_free(union anything *to, union anything *from, TYPE_T type); void assign_short_svalue(union anything *to, union anything *from, TYPE_T type);
07c0731996-06-21Fredrik Hübinette (Hubbe) unsigned INT32 hash_svalue(struct svalue *s); int svalue_is_true(struct svalue *s);
5267b71995-08-09Fredrik Hübinette (Hubbe) int is_eq(struct svalue *a, struct svalue *b); int low_is_equal(struct svalue *a, struct svalue *b, struct processing *p); int low_short_is_equal(const union anything *a, const union anything *b, TYPE_T type, struct processing *p); int is_equal(struct svalue *a,struct svalue *b);
07c0731996-06-21Fredrik Hübinette (Hubbe) int is_lt(struct svalue *a,struct svalue *b);
5267b71995-08-09Fredrik Hübinette (Hubbe) void describe_svalue(struct svalue *s,int indent,struct processing *p); void clear_svalues(struct svalue *s, INT32 num); void copy_svalues_recursively_no_free(struct svalue *to, struct svalue *from, INT32 num, struct processing *p);
569d5e1998-09-18Fredrik Hübinette (Hubbe) void check_short_svalue(union anything *u, TYPE_T type);
5267b71995-08-09Fredrik Hübinette (Hubbe) void check_svalue(struct svalue *s);
b95bef1996-03-29Fredrik Hübinette (Hubbe) TYPE_FIELD gc_check_svalues(struct svalue *s, int num);
05c7cd1997-07-19Fredrik Hübinette (Hubbe) void gc_xmark_svalues(struct svalue *s, int num);
bce86c1996-02-25Fredrik Hübinette (Hubbe) void gc_check_short_svalue(union anything *u, TYPE_T type);
b95bef1996-03-29Fredrik Hübinette (Hubbe) void gc_mark_svalues(struct svalue *s, int num); void gc_mark_short_svalue(union anything *u, TYPE_T type);
06983f1996-09-22Fredrik Hübinette (Hubbe) INT32 pike_sizeof(struct svalue *s);
5267b71995-08-09Fredrik Hübinette (Hubbe) /* Prototypes end here */ #endif