pike.git / src / encode.c

version» Context lines:

pike.git/src/encode.c:18:   #include "pike_error.h"   #include "operators.h"   #include "builtin_functions.h"   #include "module_support.h"   #include "fsort.h"   #include "threads.h"   #include "stuff.h"   #include "version.h"   #include "bignum.h"    - RCSID("$Id: encode.c,v 1.97 2002/05/02 14:48:01 mast Exp $"); + RCSID("$Id: encode.c,v 1.98 2002/05/06 14:37:18 mast Exp $");      /* #define ENCODE_DEBUG */      /* Use the old encoding method for programs. */   /* #define OLD_PIKE_ENCODE_PROGRAM */      #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(N,X) do if (data->debug>=N) {X;} while (0)
pike.git/src/encode.c:880:    struct svalue str_sval;    str_sval.type = T_STRING;    str_sval.subtype = 0;    /* strings */    for(d=0;d<p->num_strings;d++) {    str_sval.u.string = p->strings[d];    encode_value2(&str_sval, data);    }    }    -  EDB(5, -  { -  fprintf(stderr, -  "%*sencode: Program flags: 0x%04x\n\n", -  data->depth, "", p->flags); +  EDB(5, dump_program_tables (p, data->depth));    -  fprintf(stderr, -  "%*sencode: Reference table:\n" -  "%*s ####: Flags Inherit Identifier\n", -  data->depth, "", data->depth, ""); -  for (d=0; d < p->num_identifier_references; d++) { -  struct reference *ref = p->identifier_references + d; -  -  fprintf(stderr, "%*s %4d: %5x %7d %7d\n", -  data->depth, "", -  d, ref->id_flags, ref->inherit_offset, -  ref->identifier_offset); -  } -  fprintf(stderr, "\n" -  "%*sencode: Inherit table:\n" -  "%*s ####: Level id_level offset\n", -  data->depth, "", data->depth, ""); -  for (d=0; d < p->num_inherits; d++) { -  struct inherit *inh = p->inherits + d; -  -  fprintf(stderr, "%*s %4d: %5d %8d %6d\n", -  data->depth, "", -  d, inh->inherit_level, inh->identifier_level, -  inh->storage_offset); -  } -  fprintf(stderr, "\n" -  "%*sencode: Identifier table:\n" -  "%*s ####: Flags Offset Type Name\n", -  data->depth, "", data->depth, ""); -  for (d=0; d < p->num_identifiers; d++) { -  struct identifier *id = p->identifiers + d; -  -  fprintf(stderr, "%*s %4d: %5x %6d %4d \"%s\"\n", -  data->depth, "", -  d, id->identifier_flags, id->func.offset, -  id->run_time_type, id->name->str); -  } -  fprintf(stderr, "\n" -  "%*sencode: Variable table:\n" -  "%*s ####: Index\n", -  data->depth, "", data->depth, ""); -  for (d = 0; d < p->num_variable_index; d++) { -  fprintf(stderr, "%*s %4d: %5d\n", -  data->depth, "", -  d, p->variable_index[d]); -  } -  fprintf(stderr, "\n" -  "%*sencode: Constant table:\n" -  "%*s ####: Type Name\n", -  data->depth, "", data->depth, ""); -  for (d = 0; d < p->num_constants; d++) { -  struct program_constant *c = p->constants + d; -  fprintf(stderr, "%*s %4d: %4d %s%s%s\n", -  data->depth, "", -  d, c->sval.type, -  c->name?"\"":"",c->name?c->name->str:"NULL",c->name?"\"":""); -  } -  fprintf(stderr, "\n"); -  }); -  -  +     /* Dump the identifiers in a portable manner... */    {    int inherit_num = 1;    struct svalue str_sval; -  +  char *id_dumped = (char *) alloca(p->num_identifiers); +  MEMSET(id_dumped,0,p->num_identifiers);    str_sval.type = T_STRING;    str_sval.subtype = 0;       EDB(2,    fprintf(stderr, "%*sencode: encoding references\n",    data->depth, ""));      #ifdef ENCODE_DEBUG    data->depth += 2;   #endif
pike.git/src/encode.c:993:    data->depth, "", d,    id->identifier_flags,    id->name->str));      #ifdef ENCODE_DEBUG    data->depth += 2;   #endif       /* Variable, constant or function. */    -  if (ref->inherit_offset) { +  if (ref->inherit_offset || ref->id_flags & ID_HIDDEN) {    int ref_no = -1;    /* Explicit reference to inherited symbol. */       EDB(3,    fprintf(stderr, "%*sencode: encoding raw reference\n",    data->depth, ""));       code_number(ID_ENTRY_RAW, data);    code_number(ref->id_flags, data);   
pike.git/src/encode.c:1016:       /* identifier_offset */    /* Find the corresponding identifier reference    * in the inherit. */    {    struct program *p2 = p->inherits[ref->inherit_offset].prog;    int i;    for (i=0; i < p2->num_identifier_references; i++) {    struct reference *ref2 = p2->identifier_references + i;    if (!(ref2->inherit_offset) && +  !(ref2->id_flags & ID_HIDDEN) &&    (ref2->identifier_offset == ref->identifier_offset)) {    ref_no = i;    break;    }    }    }    if (ref_no == -1) {    Pike_error("Failed to reverse explicit reference\n");    }    code_number(ref_no, data);    } else if (IDENTIFIER_IS_CONSTANT(id->identifier_flags)) {    /* Constant */ -  +  +  if (id_dumped[ref->identifier_offset]) {    EDB(3, -  +  fprintf(stderr, "%*sencode: already encoded constant\n", +  data->depth, "")); +  goto next_identifier_ref; +  } +  id_dumped[ref->identifier_offset] = 1; +  +  EDB(3,    fprintf(stderr, "%*sencode: encoding constant\n",    data->depth, ""));       code_number(ID_ENTRY_CONSTANT, data);    code_number(ref->id_flags, data);       /* name */    str_sval.u.string = id->name;    encode_value2(&str_sval, data);   
pike.git/src/encode.c:1052:    /* type */    str_sval.u.string = id->type;    str_sval.type = T_TYPE;    encode_value2(&str_sval, data);    str_sval.type = T_STRING;       /* run-time type */    code_number(id->run_time_type, data);    } else if (IDENTIFIER_IS_PIKE_FUNCTION(id->identifier_flags)) {    /* Pike function */ +  +  if (id_dumped[ref->identifier_offset]) {    EDB(3, -  +  fprintf(stderr, "%*sencode: already encoded function\n", +  data->depth, "")); +  goto next_identifier_ref; +  } +  id_dumped[ref->identifier_offset] = 1; +  +  EDB(3,    fprintf(stderr, "%*sencode: encoding function\n",    data->depth, ""));       code_number(ID_ENTRY_FUNCTION, data);    code_number(ref->id_flags, data);       /* name */    str_sval.u.string = id->name;    encode_value2(&str_sval, data);   
pike.git/src/encode.c:1085:    /* opt_flags */    code_number(id->opt_flags, data);    } else if (id->identifier_flags & IDENTIFIER_C_FUNCTION) {    /* C Function */    /* Not supported. */    Pike_error("Cannot encode functions implemented in C "    "(identifier='%s').\n",    p->identifiers[d].name->str);    } else {    /* Variable */ +  +  if (id_dumped[ref->identifier_offset]) {    EDB(3, -  +  fprintf(stderr, "%*sencode: already encoded variable\n", +  data->depth, "")); +  goto next_identifier_ref; +  } +  id_dumped[ref->identifier_offset] = 1; +  +  EDB(3,    fprintf(stderr, "%*sencode: encoding variable\n",    data->depth, ""));       code_number(ID_ENTRY_VARIABLE, data);    code_number(ref->id_flags, data);       /* name */    str_sval.u.string = id->name;    encode_value2(&str_sval, data);       /* type */    ref_push_string(id->type);    Pike_sp[-1].type = T_TYPE;    encode_value2(Pike_sp-1, data);    pop_stack();    }       /* Identifier reference number */    code_number(d, data);    -  +  next_identifier_ref:   #ifdef ENCODE_DEBUG    data->depth -= 2;   #endif    }       /* Encode next inherit. */    if (inherit_num < p->num_inherits) {    /* Inherit */    INT16 inherit_flags_change = 0;    struct inherit *inh = p->inherits + inherit_num;
pike.git/src/encode.c:2018:          case TAG_PROGRAM:    EDB(3,    fprintf(stderr, "%*s TAG_PROGRAM(%d)\n",    data->depth, "", num));    switch(num)    {    case 0:    { +  struct program *p;    struct svalue *prog_code;       tmp=data->counter;    data->counter.u.integer++;    decode_value2(data);       /* Keep the value so that we can make a good error-message. */    prog_code = Pike_sp-1;    stack_dup();       if(data->codec)    {    apply(data->codec,"programof", 1);    }else{    fallback_codec();    } -  if(data->pickyness && !program_from_svalue(Pike_sp-1)) { +  +  p = program_from_svalue(Pike_sp-1); +  +  if(data->pickyness && !p) {    if ((prog_code->type == T_STRING) &&    (prog_code->u.string->len < 128) &&    (!prog_code->u.string->size_shift)) {    Pike_error("Failed to decode program \"%s\".\n",    prog_code->u.string->str);    }    Pike_error("Failed to decode program.\n");    }    /* Remove the extra entry from the stack. */    stack_swap();    pop_stack(); -  +  ref_push_program(p); +  stack_swap(); +  pop_stack();    break;    }       case 1:    {    int d;    size_t size=0;    char *dat;    struct program *p;    ONERROR err1, err2, err3;
pike.git/src/encode.c:2608:       /* identifier_offset */    /* Actually the id ref number from the inherited program */    decode_number(ref_no, data);    ref.identifier_offset = p->inherits[ref.inherit_offset].prog->    identifier_references[ref_no].identifier_offset;       /* Expected identifier reference number */    decode_number(no, data);    -  if (no != -  Pike_compiler->new_program->num_identifier_references) { +  if (no > p->num_identifier_references) { +  EDB (3, dump_program_tables (p, data->depth));    Pike_error("Bad identifier reference offset: %d != %d\n", no,    Pike_compiler->new_program->    num_identifier_references);    } -  +  else if (no == p->num_identifier_references) {    add_to_identifier_references(ref);    } -  +  else { +  p->identifier_references[no] = ref; +  } +  }    break;       case ID_ENTRY_VARIABLE:    {    int no;       /* name */    decode_value2(data);    if (Pike_sp[-1].type != T_STRING) {    Pike_error("Bad variable name (not a string)\n");
pike.git/src/encode.c:2663:    }       pop_n_elems(2);    }    break;    case ID_ENTRY_FUNCTION:    {    union idptr func;    unsigned INT8 func_flags;    unsigned INT16 opt_flags; -  int no; +  int no, n;       /* name */    decode_value2(data);    if (Pike_sp[-1].type != T_STRING) {    Pike_error("Bad function name (not a string)\n");    }       /* type */    decode_value2(data);    if (Pike_sp[-1].type != T_TYPE) {
pike.git/src/encode.c:2693:    /* opt_flags */    decode_number(opt_flags, data);       /* FIXME:    * Verify validity of func_flags, func.offset & opt_flags    */       /* Expected identifier offset */    decode_number(no, data);    -  EDB(5, +  EDB(5, { +  INT32 line; +  char *file = get_line (func.offset + p->program, p, &line);    fprintf(stderr,    "%*sdefine_function(\"%s\", X, 0x%04x, 0x%04x,\n" -  "%*s 0x%04x, 0x%04x)\n", +  "%*s 0x%04x, 0x%04x)\n" +  "%*s @ %s:%d\n",    data->depth, "",    Pike_sp[-2].u.string->str, id_flags, func_flags,    data->depth, "", -  func.offset, opt_flags)); +  func.offset, opt_flags, +  data->depth, "", +  file, line); +  });       /* Alters    *    * identifiers, identifier_references    */ -  if (no != define_function(Pike_sp[-2].u.string, +  n = define_function(Pike_sp[-2].u.string,    Pike_sp[-1].u.string,    id_flags, func_flags, -  &func, opt_flags)) { +  &func, opt_flags); +  if (no != n && +  (p->identifier_references[no].id_flags != id_flags || +  p->identifier_references[no].identifier_offset != +  p->identifier_references[n].identifier_offset || +  p->identifier_references[no].inherit_offset != 0)) {    Pike_error("Bad function identifier offset: %d\n", no);    }       pop_n_elems(2);    }    break;    case ID_ENTRY_CONSTANT:    {    struct identifier id;    struct reference ref;    int no; -  +  int n;       /* name */    decode_value2(data);    if (Pike_sp[-1].type != T_STRING) {    Pike_error("Bad function name (not a string)\n");    }    id.name = Pike_sp[-1].u.string;       /* identifier_flags */    id.identifier_flags = IDENTIFIER_CONSTANT;
pike.git/src/encode.c:2752:    Pike_error("Bad function type (not a type)\n");    }    id.type = Pike_sp[-1].u.string;       /* run_time_type */    decode_number(id.run_time_type, data);       /* Expected identifier number. */    decode_number(no, data);    -  if (no != -  Pike_compiler->new_program->num_identifier_references) { -  Pike_error("Bad constant identifier offset: %d\n", no); -  } +  n = isidentifier(id.name);      #ifdef PROFILING    id.self_time=0;    id.num_calls=0;    id.total_time=0;   #endif       /* id_flags */    ref.id_flags = id_flags;   
pike.git/src/encode.c:2779:       /* ref.inherit_offset */    ref.inherit_offset = 0;       EDB(5,    fprintf(stderr,    "%*sdefining constant(\"%s\", X, 0x%04x)\n",    data->depth, "",    Pike_sp[-2].u.string->str, id_flags));    +  if (n < 0 || (n = override_identifier (&ref, id.name, 0)) < 0) { +  n = p->num_identifier_references; +  add_to_identifier_references(ref); +  } +  +  if (no != n) { +  if (id.name->size_shift) +  Pike_error("Bad constant identifier offset: %d\n", no); +  else +  Pike_error("Bad constant identifier offset %d for %s\n", +  no, id.name->str); +  } +     /* Alters    *    * identifiers, identifier_references    */    add_to_identifiers(id);    Pike_sp -= 2; -  -  add_to_identifier_references(ref); +     }    break;    case ID_ENTRY_INHERIT:    {    struct program *prog;    struct object *parent = NULL;    int parent_identifier;    int parent_offset;    struct pike_string *name = NULL;    int no;
pike.git/src/encode.c:2876:    data->depth-=2;   #endif       UNSET_ONERROR(err);       /* De-kludge to get end_first_pass() to free the program. */    Pike_compiler->num_parse_error--;       p->flags |= PROGRAM_PASS_1_DONE;    -  EDB(5, -  { -  int d; +  EDB(5, dump_program_tables (p, data->depth));    -  fprintf(stderr, -  "%*sdecode: Program flags: 0x%04x\n\n", -  data->depth, "", p->flags); -  -  fprintf(stderr, -  "%*sdecode: Reference table:\n" -  "%*s ####: Flags Inherit Identifier\n", -  data->depth, "", data->depth, ""); -  for (d=0; d < p->num_identifier_references; d++) { -  struct reference *ref = p->identifier_references + d; -  -  fprintf(stderr, "%*s %4d: %5x %7d %7d\n", -  data->depth, "", -  d, ref->id_flags, ref->inherit_offset, -  ref->identifier_offset); -  } -  fprintf(stderr, "\n" -  "%*sdecode: Inherit table:\n" -  "%*s ####: Level id_level offset\n", -  data->depth, "", data->depth, ""); -  for (d=0; d < p->num_inherits; d++) { -  struct inherit *inh = p->inherits + d; -  -  fprintf(stderr, "%*s %4d: %5d %8d %6d\n", -  data->depth, "", -  d, inh->inherit_level, inh->identifier_level, -  inh->storage_offset); -  } -  fprintf(stderr, "\n" -  "%*sdecode: Identifier table:\n" -  "%*s ####: Flags Offset Type Name\n", -  data->depth, "", data->depth, ""); -  for (d=0; d < p->num_identifiers; d++) { -  struct identifier *id = p->identifiers + d; -  -  fprintf(stderr, "%*s %4d: %5x %6d %4d \"%s\"\n", -  data->depth, "", -  d, id->identifier_flags, id->func.offset, -  id->run_time_type, id->name->str); -  } -  fprintf(stderr, "\n" -  "%*sdecode: Variable table:\n" -  "%*s ####: Index\n", -  data->depth, "", data->depth, ""); -  for (d = 0; d < p->num_variable_index; d++) { -  fprintf(stderr, "%*s %4d: %5d\n", -  data->depth, "", -  d, p->variable_index[d]); -  } -  fprintf(stderr, "\n" -  "%*sdecode: Constant table:\n" -  "%*s ####: Type Name\n", -  data->depth, "", data->depth, ""); -  for (d = 0; d < p->num_constants; d++) { -  struct program_constant *c = p->constants + d; -  fprintf(stderr, "%*s %4d: %4d %s%s%s\n", -  data->depth, "", -  d, c->sval.type, -  c->name?"\"":"",c->name?c->name->str:"NULL",c->name?"\"":""); -  } -  fprintf(stderr, "\n"); -  }); -  +     /* Fixate & optimize    *    * lfuns and identifier_index    */    if (!(p = end_first_pass(2))) {    Pike_error("Failed to decode program.\n");    }    push_program(p);       /* Verify... */
pike.git/src/encode.c:2977:    /* value */    decode_value2(data);    /* name */    decode_value2(data);    if (Pike_sp[-1].type == T_STRING) {    constant->name = Pike_sp[-1].u.string;    } else if ((Pike_sp[-1].type == T_INT) &&    !Pike_sp[-1].u.integer) {    constant->name = NULL;    } else { -  Pike_error("Non strings in string table.\n"); +  Pike_error("Name of constant is not a string.\n");    }    constant->sval = Pike_sp[-2];    Pike_sp -= 2;    }       /* The program should be consistent now. */    p->flags &= ~PROGRAM_AVOID_CHECK;       EDB(5, fprintf(stderr, "%*sProgram flags: 0x%04x\n",    data->depth, "", p->flags));
pike.git/src/encode.c:3247:    Pike_error("Format error: Unknown type tag %ld:%ld\n",    PTRDIFF_T_TO_LONG(i), PTRDIFF_T_TO_LONG(t));    }   }      void f_decode_value(INT32 args)   {    struct pike_string *s;    struct object *codec;    - #ifdef ENCODE_DEBUG -  int debug; - #endif -  +     check_all_args("decode_value", args,    BIT_STRING, BIT_VOID | BIT_OBJECT | BIT_INT,   #ifdef ENCODE_DEBUG    /* This argument is only an internal debug helper.    * It's intentionally not part of the function    * prototype, to keep the argument position free for    * other uses in the future. */    BIT_VOID | BIT_INT,   #endif    0);    - #ifdef ENCODE_DEBUG -  debug = args > 2 ? Pike_sp[2-args].u.integer : 0; - #endif -  +     s = Pike_sp[-args].u.string;    if(args<2)    {    codec=get_master();    }    else if(Pike_sp[1-args].type == T_OBJECT)    {    codec=Pike_sp[1-args].u.object;    }    else    {    codec=0;    }       if(!my_decode(s, codec   #ifdef ENCODE_DEBUG -  , debug +  , args > 2 ? Pike_sp[2-args].u.integer : 0   #endif    ))    {    char *v=s->str;    ptrdiff_t l=s->len;    rec_restore_value(&v, &l);    }    assign_svalue(Pike_sp-args-1, Pike_sp-1);    pop_n_elems(args);   }