Branch: Tag:

2001-07-12

2001-07-12 23:14:16 by Fredrik Hübinette (Hubbe) <hubbe@hubbe.net>

fixed to handle recursive stuff in programs

Rev: src/encode.c:1.109

25:   #include "version.h"   #include "bignum.h"    - RCSID("$Id: encode.c,v 1.108 2001/07/10 11:57:26 mast Exp $"); + RCSID("$Id: encode.c,v 1.109 2001/07/12 23:14:16 hubbe Exp $");      /* #define ENCODE_DEBUG */      #ifdef ENCODE_DEBUG   /* Pass a nonzero integer as the third arg to encode_value,    * encode_value_canonic and decode_value to activate this debug. */ - #define EDB(X) do if (data->debug) {X;} while (0) + #define EDB(N,X) do if (data->debug>=N) {X;} while (0)   #else - #define EDB(X) do {} while (0) + #define EDB(N,X) do {} while (0)   #endif      /* The sp macro conflicts with Solaris 2.5.1's <sys/conf.h>. */
201:   static void code_entry(int tag, INT64 num, struct encode_data *data)   {    int t; -  EDB( +  EDB(5,    fprintf(stderr,"%*sencode: code_entry(tag=%d (%s), num=%ld)\n",    data->depth, "", tag,    get_name_of_type(tag_to_type(tag)),
548:       if((tmp=low_mapping_lookup(data->encoded, val)))    { -  EDB(fprintf(stderr, "%*sEncoding TAG_AGAIN to <%d>\n", +  EDB(1,fprintf(stderr, "%*sEncoding TAG_AGAIN from <%d>\n",    data->depth, "", tmp->u.integer));    code_entry(TAG_AGAIN, tmp->u.integer, data);   #ifdef ENCODE_DEBUG
556:   #endif    return;    }else if (val->type != T_TYPE) { -  EDB(fprintf(stderr, "%*sEncoding to <%d>: ", +  EDB(1,fprintf(stderr, "%*sEncoding to <%d>: ",    data->depth, "", data->counter.u.integer); -  +  if(data->debug == 1) +  { +  fprintf(stderr,"TAG%d",val->type); +  }else{    print_svalue(stderr, val); -  +  +  }    fputc('\n', stderr););    mapping_insert(data->encoded, val, &data->counter);    data->counter.u.integer++;
588:    Pike_error("Canonical encoding of the type type not supported.\n");    code_entry(TAG_TYPE, 0, data); /* Type encoding #0 */    encode_type(val->u.type, data); -  EDB(fprintf(stderr, "%*sEncoded type to <%d>: ", +  EDB(2,fprintf(stderr, "%*sEncoded type to <%d>: ",    data->depth, "", data->counter.u.integer);    print_svalue(stderr, val);    fputc('\n', stderr););
611:    x = DO_NOT_WARN((INT64)((((INT64)1)<<(sizeof(INT64)*8 - 2))*tmp));    y -= sizeof(INT64)*8 - 2;    -  EDB(fprintf(stderr, +  EDB(2,fprintf(stderr,    "Encoding float... tmp: %10g, x: 0x%016llx, y: %d\n",    tmp, x, y));   
621:    x >>= 32;    y += 32;    -  EDB(fprintf(stderr, +  EDB(2,fprintf(stderr,    "Reducing float... x: 0x%08llx, y: %d\n",    x, y));   #if 0
742:    push_svalue(val);    f_object_program(1);    -  code_entry(type_to_tag(val->type), 1,data); +  /* FIXME: +  * If we want to be more backwards compatible, then +  * we could follow the following steps: +  * 1) do a code_entry(TAG_OBJECT, 1, data); +  * 2) remove ourselves from the cache +  * 3) code the program +  * 4) if val is present in the cache, goto 7 +  * 5) call encode_object +  * 6) break; +  * (This is the hard part...) +  * 7) change code_entry in (1) to code_entry(TAG_OBJECT,3 data) +  * 8) push_svalue(val) +  * 9) break; +  * +  * -Hubbe +  */ +  +  if(low_mapping_lookup(data->encoded, Pike_sp-1)) +  { +  code_entry(TAG_OBJECT, 1,data);    encode_value2(Pike_sp-1, data);    pop_stack();       push_svalue(val);    apply(data->codec,"encode_object",1); -  +  }else{ +  /* We have to remove ourself from the cache permanently */ +  map_delete(data->encoded, val); +  +  /* Code the program */ +  code_entry(TAG_OBJECT, 3,data); +  encode_value2(Pike_sp-1, data); +  pop_stack(); +  +  /* Code the object */ +  push_svalue(val); +  }    break;    }    /* FALL THROUGH */       default: -  code_entry(type_to_tag(val->type), 0,data); +  code_entry(TAG_OBJECT, 0,data);    break;    }    encode_value2(Pike_sp-1, data);
895:    code_number(p->identifier_references[d].inherit_offset,data);    code_number(p->identifier_references[d].identifier_offset,data);    code_number(p->identifier_references[d].id_flags,data); -  EDB(fprintf(stderr,"IDREF%x > %d: { %d, %d, %d }\n", +  EDB(3,fprintf(stderr,"IDREF%x > %d: { %d, %d, %d }\n",    p->id,d,    p->identifier_references[d].inherit_offset,    p->identifier_references[d].identifier_offset,
917:    ref_push_object(p->inherits[d].parent);    Pike_sp[-1].subtype=p->inherits[d].parent_identifier;    Pike_sp[-1].type=T_FUNCTION; -  EDB(fprintf(stderr,"INHERIT%x coded as func { %p, %d }\n", +  EDB(3,fprintf(stderr,"INHERIT%x coded as func { %p, %d }\n",    p->id, p->inherits[d].parent, p->inherits[d].parent_identifier););    }else if(p->inherits[d].prog){    ref_push_program(p->inherits[d].prog);
930:       adddata3(p->inherits[d].name);    -  EDB(fprintf(stderr,"INHERIT%x > %d: %d id=%d\n", +  EDB(3,fprintf(stderr,"INHERIT%x > %d: %d id=%d\n",    p->id,d,    p->inherits[d].prog->num_identifiers,    p->inherits[d].prog->id););
952:    }    }    +  for(d=0;d<NUM_LFUNS;d++) +  code_number(p->lfuns[d], data); +     for(d=0;d<p->num_constants;d++)    {    encode_value2(& p->constants[d].sval, data);    adddata3(p->constants[d].name);    } -  -  for(d=0;d<NUM_LFUNS;d++) -  code_number(p->lfuns[d], data); +     }else{    code_entry(type_to_tag(val->type), 0,data);    encode_value2(Pike_sp-1, data);
1115:    struct program *prog;   };    + struct unfinished_obj_link + { +  struct unfinished_prog_link *next; +  struct object *o; + }; +    struct decode_data   {    unsigned char *data;
1122:    ptrdiff_t ptr;    struct mapping *decoded;    struct unfinished_prog_link *unfinished_programs; +  struct unfinished_obj_link *unfinished_objects;    struct svalue counter;    struct object *codec;    int pickyness;
1161:   #define GETC() my_extract_char(data)      #define DECODE(Z) do { \ -  EDB( \ +  EDB(5, \    fprintf(stderr,"%*sdecode(%s) at %d: ", \    data->depth,"",(Z),__LINE__)); \    what=GETC(); \
1185:    num = ~num; \    numh = ~numh; \    } \ -  EDB( \ +  EDB(5, \    fprintf(stderr,"type=%d (%s), num=%ld\n", \    (what & TAG_MASK), \    get_name_of_type(tag_to_type(what & TAG_MASK)), \
1529:       DECODE("decode_value2");    + #ifdef ENCODE_DEBUG +  if(data->debug) +  { +  if((what & TAG_MASK ) == TAG_AGAIN) +  fprintf(stderr, "%*sDecoding TAG_AGAIN from <%d>\n", +  data->depth, "", num); +  +  else +  if(data->debug < 2) +  fprintf(stderr, "%*sDecoding to <%d>: TAG%d (%d)\n", +  data->depth, "", data->counter.u.integer , +  what & TAG_MASK, num); +  } + #endif +     check_stack(1);       switch(what & TAG_MASK)
1570:    {    double res;    -  EDB(fprintf(stderr, "Decoding float... numh:0x%08x, num:0x%08x\n", +  EDB(2,fprintf(stderr, "Decoding float... numh:0x%08x, num:0x%08x\n",    numh, num));       res = LDEXP((double)numh, 32) + (double)(unsigned INT32)num;    -  EDB(fprintf(stderr, "Mantissa: %10g\n", res)); +  EDB(2,fprintf(stderr, "Mantissa: %10g\n", res));       tmp = data->counter;    data->counter.u.integer++;       DECODE("float");    -  EDB(fprintf(stderr, "Exponent: %d\n", num)); +  EDB(2,fprintf(stderr, "Exponent: %d\n", num));       push_float(LDEXP(res, num));    break;
1614:       tmp.type=T_ARRAY;    tmp.u.array=a=allocate_array(num); -  EDB(fprintf(stderr, "%*sDecoding array of size %d to <%d>\n", +  EDB(2,fprintf(stderr, "%*sDecoding array of size %d to <%d>\n",    data->depth, "", num, data->counter.u.integer));    mapping_insert(data->decoded, & data->counter, &tmp);    data->counter.u.integer++;
1652:    m=allocate_mapping(num);    tmp.type=T_MAPPING;    tmp.u.mapping=m; -  EDB(fprintf(stderr, "%*sDecoding mapping of size %d to <%d>\n", +  EDB(2,fprintf(stderr, "%*sDecoding mapping of size %d to <%d>\n",    data->depth, "", num, data->counter.u.integer));    mapping_insert(data->decoded, & data->counter, &tmp);    data->counter.u.integer++;
1688:    m = allocate_multiset(a);    tmp.type = T_MULTISET;    tmp.u.multiset = m; -  EDB(fprintf(stderr, "%*sDecoding multiset of size %d to <%d>\n", +  EDB(2,fprintf(stderr, "%*sDecoding multiset of size %d to <%d>\n",    data->depth, "", num, data->counter.u.integer));    mapping_insert(data->decoded, & data->counter, &tmp);    data->counter.u.integer++;
1710:    return;    }    -  +     case TAG_OBJECT: -  +  {    tmp=data->counter;    data->counter.u.integer++; -  +     decode_value2(data);       switch(num)
1730:    case 1:    if(IS_ZERO(Pike_sp-1))    { -  EDB(fprintf(stderr, "%*sDecoded a failed object to <%d>: ", +  EDB(1,fprintf(stderr, "%*sDecoded a failed object to <%d>: ",    data->depth, "", tmp.u.integer);    print_svalue(stderr, Pike_sp-1);    fputc('\n', stderr);); -  mapping_insert(data->decoded, &tmp, Pike_sp-1); +     decode_value2(data);    pop_stack();    }else{ -  f_call_function(1); -  EDB(fprintf(stderr, "%*sDecoded an object to <%d>: ", +  struct object *o; +  /* decode_value_clone_object does not call __INIT, so +  * we want to do that ourselves... +  */ +  o=decode_value_clone_object(Pike_sp-1); +  debug_malloc_touch(o); +  pop_stack(); +  push_object(o); +  +  if(o->prog) +  { +  if(o->prog->flags & PROGRAM_FINISHED) +  { +  apply_lfun(o, LFUN___INIT, 0); +  pop_stack(); +  }else{ +  struct unfinished_obj_link *ol=ALLOC_STRUCT(unfinished_obj_link); +  ol->o=o; +  ol->next=data->unfinished_objects; +  data->unfinished_objects=ol; +  } +  } +  +  EDB(2,fprintf(stderr, "%*sDecoded an object to <%d>: ",    data->depth, "", tmp.u.integer);    print_svalue(stderr, Pike_sp-1);    fputc('\n', stderr);); -  mapping_insert(data->decoded, &tmp, Pike_sp-1); -  push_svalue(Pike_sp-1); +  +  ref_push_object(o);    decode_value2(data);    if(!data->codec)    Pike_error("Failed to decode (no codec)\n");    apply(data->codec,"decode_object",2);    pop_stack();    } -  if(data->pickyness && Pike_sp[-1].type != T_OBJECT) -  Pike_error("Failed to decode object.\n"); - #ifdef ENCODE_DEBUG -  data->depth -= 2; - #endif -  return; +     -  +  break; +    #ifdef AUTO_BIGNUM    /* It is possible that we should do this even without    * AUTO_BIGNUM /Hubbe
1776: Inside #if defined(AUTO_BIGNUM)
   }      #endif +  case 3: +  pop_stack(); +  decode_value2(data); +  break;       default:    Pike_error("Object coding not compatible.\n");    break;    } -  if(data->pickyness && Pike_sp[-1].type != T_OBJECT) -  Pike_error("Failed to decode (got type %d; expected object).\n", -  Pike_sp[-1].type); +  if(Pike_sp[-1].type != T_OBJECT) +  if(data->pickyness) +  Pike_error("Failed to decode object.\n"); +  + #ifdef ENCODE_DEBUG +  data->depth -= 2; + #endif    break; -  +  }       case TAG_FUNCTION:    tmp=data->counter;
1888:    debug_malloc_touch(p);    tmp.type=T_PROGRAM;    tmp.u.program=p; -  EDB(fprintf(stderr, "%*sDecoding a program to <%d>: ", +  EDB(2,fprintf(stderr, "%*sDecoding a program to <%d>: ",    data->depth, "", data->counter.u.integer);    print_svalue(stderr, &tmp);    fputc('\n', stderr););
1904:       debug_malloc_touch(p);    decode_number(p->flags,data); -  p->flags &= ~(PROGRAM_FINISHED | PROGRAM_OPTIMIZED); +  p->flags &= ~(PROGRAM_FINISHED | PROGRAM_OPTIMIZED | +  PROGRAM_FIXED | PROGRAM_PASS_1_DONE);    p->flags |= PROGRAM_AVOID_CHECK;    decode_number(p->storage_needed,data);    decode_number(p->xstorage,data);
2018:    decode_number(p->identifier_references[d].identifier_offset,data);    decode_number(p->identifier_references[d].id_flags,data);    -  EDB(fprintf(stderr,"IDREF%x < %d: { %d, %d, %d }\n", +  EDB(3,fprintf(stderr,"IDREF%x < %d: { %d, %d, %d }\n",    p->id,d,    p->identifier_references[d].inherit_offset,    p->identifier_references[d].identifier_offset,
2063:    if(Pike_sp[-1].subtype == FUNCTION_BUILTIN)    Pike_error("Failed to decode parent.\n");    -  EDB( fprintf(stderr,"INHERIT%x = func { %p, %d} \n",p->id,Pike_sp[-1].u.object, Pike_sp[-1].subtype); ); +  EDB(3, fprintf(stderr,"INHERIT%x = func { %p, %d} \n",p->id,Pike_sp[-1].u.object, Pike_sp[-1].subtype); );       p->inherits[d].parent_identifier=Pike_sp[-1].subtype;    p->inherits[d].prog=program_from_svalue(Pike_sp-1);
2076:    break;       case T_PROGRAM: -  EDB( fprintf(stderr,"INHERIT%x = prog\n",p->id); ); +  EDB(3, fprintf(stderr,"INHERIT%x = prog\n",p->id); );    p->inherits[d].prog=Pike_sp[-1].u.program;    Pike_sp--;    dmalloc_touch_svalue(Pike_sp);
2087:       getdata3(p->inherits[d].name);    -  EDB( fprintf(stderr,"INHERIT%x < %d: %d id=%d\n", +  EDB(3, fprintf(stderr,"INHERIT%x < %d: %d id=%d\n",    p->id,d,    p->inherits[d].prog->num_identifiers,    p->inherits[d].prog->id); );
2124:    debug_malloc_touch(dat);       debug_malloc_touch(p); -  for(d=0;d<p->num_constants;d++) -  { -  decode_value2(data); -  p->constants[d].sval=*--Pike_sp; -  dmalloc_touch_svalue(Pike_sp); -  getdata3(p->constants[d].name); -  } -  data->pickyness--; -  -  debug_malloc_touch(dat); -  -  debug_malloc_touch(p); +     for(d=0;d<NUM_LFUNS;d++)    decode_number(p->lfuns[d],data);   
2150:    sizeof(unsigned short),(fsortfun)program_function_index_compare);    Pike_compiler->new_program=new_program_save;    } +  +  debug_malloc_touch(dat); +  debug_malloc_touch(p); +  p->flags |= PROGRAM_PASS_1_DONE | PROGRAM_FIXED; +  for(d=0;d<p->num_constants;d++) +  { +  decode_value2(data); +  p->constants[d].sval=*--Pike_sp; +  dmalloc_touch_svalue(Pike_sp); +  getdata3(p->constants[d].name); +  } +  data->pickyness--; +  +     UNSET_ONERROR(err3);       ref_push_program(p);
2165:       if(d == p->num_inherits)    { -  int done=0; -  struct unfinished_prog_link *l, **ptr; +     p->flags &=~ PROGRAM_AVOID_CHECK;    p->flags |= PROGRAM_FINISHED;    -  +  /* Go through the linked list of unfinished programs +  * to see what programs are now finished. +  */ +  { +  struct unfinished_prog_link *l, **ptr; +    #ifdef PIKE_DEBUG    check_program(p);   #endif /* PIKE_DEBUG */
2199:    ptr=&l->next;    }    } +  } +  +  /* Go through the linked list of unfinished objects +  * to see what objects are now finished. +  */ +  { +  struct unfinished_obj_link *l, **ptr; +  for(ptr= &data->unfinished_objects ; (l=*ptr);) +  { +  struct object *o=l->o; +  if(o->prog) +  { +  if(o->prog->flags & PROGRAM_FINISHED) +  { +  apply_lfun(o, LFUN___INIT, 0); +  pop_stack();    }else{ -  +  ptr=&l->next; +  continue; +  } +  } +  *ptr = l->next; +  free((char *)l); +  } +  } +  +  +  }else{    struct unfinished_prog_link *l;    l=ALLOC_STRUCT(unfinished_prog_link);    l->prog=p;
2260:    Pike_error("Failed to restore string. (Illegal type)\n");    }    -  EDB(fprintf(stderr, "%*sDecoded to <%d>: ", data->depth, "", tmp.u.integer); +  EDB(2,fprintf(stderr, "%*sDecoded to <%d>: ", data->depth, "", tmp.u.integer);    print_svalue(stderr, Pike_sp-1);    fputc('\n', stderr););    mapping_insert(data->decoded, & tmp, Pike_sp-1);
2281:    data->unfinished_programs=tmp->next;    free((char *)tmp);    } +  +  while(data->unfinished_objects) +  { +  struct unfinished_obj_link *tmp=data->unfinished_objects; +  data->unfinished_objects=tmp->next; +  free((char *)tmp); +  }    if (current_decode == data) {    current_decode = data->next;    } else {
2338:    data->codec=codec;    data->pickyness=0;    data->unfinished_programs=0; +  data->unfinished_objects=0;    data->raw = tmp;    data->next = current_decode;   #ifdef ENCODE_DEBUG
2362: Inside #if defined(PIKE_DEBUG)
     #ifdef PIKE_DEBUG    if(data->unfinished_programs) -  fatal("We have unfinished programs left in decode()! We may need a double loop!\n"); +  fatal("We have unfinished programs left in decode()!\n"); +  if(data->unfinished_objects) +  fatal("We have unfinished objects left in decode()!\n");   #endif    CALL_AND_UNSET_ONERROR(err);    return 1;