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.92 2002/04/15 12:23:28 grubba Exp $"); + RCSID("$Id: encode.c,v 1.93 2002/04/15 15:53:02 grubba 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:945:    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 local_num_identifiers = 0; -  int local_num_variable_index = 0; -  int local_num_inherits = 0; +  int inherit_num = 1;    struct svalue str_sval;    str_sval.type = T_STRING;    str_sval.subtype = 0;       EDB(2,    fprintf(stderr, "%*sencode: encoding references\n",    data->depth, ""));       /* NOTE: d is incremented by hand inside the loop. */    for (d=0; d < p->num_identifier_references;)    { -  +  int d_max = p->num_identifier_references; +  +  /* Find insertion point of next inherit. */ +  if (inherit_num < p->num_inherits) { +  d_max = p->inherits[inherit_num].identifier_ref_offset; +  } +  +  /* Fix locally defined identifiers. */ +  for (; d < d_max; d++) {    struct reference *ref = p->identifier_references + d;    struct inherit *inh = INHERIT_FROM_PTR(p, ref);    struct identifier *id = ID_FROM_PTR(p, ref);       EDB(3,    fprintf(stderr, "%*s identifier_flags: %4x\n",    data->depth, "", id->identifier_flags));    -  if (!inh->inherit_level) { -  /* Defined at scope 0. */ +  /* Skip identifiers that haven't been overloaded. */ +  if (inh->inherit_level) continue; +     /* Variable, constant or function. */    if (IDENTIFIER_IS_CONSTANT(id->identifier_flags)) {    /* Constant */    EDB(3,    fprintf(stderr, "%*s encode: encoding constant\n",    data->depth, ""));       code_number(ID_ENTRY_CONSTANT, data);    code_number(ref->id_flags, data);   
pike.git/src/encode.c:994:    code_number(id->func.offset, data);       /* 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); -  -  /* Verify that we can restore the id */ -  if (ref->identifier_offset != local_num_identifiers) { -  Pike_error("Constant identifier offset mismatch: %d != %d\n", -  ref->identifier_offset, local_num_identifiers); -  } -  local_num_identifiers++; +     } else if (IDENTIFIER_IS_PIKE_FUNCTION(id->identifier_flags)) {    /* Pike function */    EDB(3,    fprintf(stderr, "%*s encode: encoding function\n",    data->depth, ""));       code_number(ID_ENTRY_FUNCTION, data);    code_number(ref->id_flags, data);       /* name */
pike.git/src/encode.c:1028:    pop_stack();       /* func_flags (aka identifier_flags) */    code_number(id->identifier_flags, data);       /* func */    code_number(p->identifiers[d].func.offset, data);       /* opt_flags */    code_number(id->opt_flags, data); -  -  /* Verify that we can restore the id */ -  if (ref->identifier_offset != local_num_identifiers) { -  Pike_error("Function identifier offset mismatch: %d != %d\n", -  ref->identifier_offset, local_num_identifiers); -  } -  local_num_identifiers++; +     } 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 */    EDB(3,    fprintf(stderr, "%*s encode: encoding variable\n",
pike.git/src/encode.c:1059:       /* 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(); -  -  /* Verify that we can restore the id */ -  if (ref->identifier_offset != local_num_identifiers) { -  Pike_error("Variable identifier offset mismatch: %d != %d\n", -  ref->identifier_offset, local_num_identifiers); +     }    -  if (p->variable_index[local_num_variable_index] != -  local_num_identifiers) { -  Pike_error("Variable %d index offset mismatch: %d != %d\n", -  local_num_variable_index, -  p->variable_index[local_num_variable_index], -  local_num_identifiers); +  /* Identifier reference number */ +  code_number(d, data);    } -  local_num_identifiers++; -  local_num_variable_index++; -  } -  } else { +  +  /* Encode next inherit. */ +  if (inherit_num < p->num_inherits) {    /* Inherit */    INT16 inherit_flags_change = 0; -  +  struct inherit *inh = p->inherits + inherit_num; +  struct reference *ref = p->identifier_references + d;    int i;       EDB(3,    fprintf(stderr, "%*s encode: encoding inherit\n",    data->depth, ""));    -  code_number(ID_ENTRY_VARIABLE, data); +  code_number(ID_ENTRY_INHERIT, data);       /* Calculate id_flags */    for (i = 0; i < inh->prog->num_identifier_references; i++) { -  +  /* Ignore overloaded identifiers. */ +  if (ref[i].inherit_offset) {    inherit_flags_change |= ref[i].id_flags ^    inh->prog->identifier_references[i].id_flags;    } -  +  }    inherit_flags_change &= ~(ID_HIDDEN|ID_INHERITED);    code_number(inherit_flags_change, data);       /* name */    str_sval.u.string = inh->name;    encode_value2(&str_sval, data);       /* prog */    ref_push_program(inh->prog);    encode_value2(Pike_sp-1, data);
pike.git/src/encode.c:1120:    }    encode_value2(Pike_sp-1, data);    pop_stack();       /* parent_identifier */    code_number(inh->parent_identifier, data);       /* parent_offset */    code_number(inh->parent_offset, data);    -  d += inh->prog->num_identifier_references; -  continue; -  } -  d++; -  } - #if 0 -  while (next_inherit < p->num_inherits) { -  /* Some identifier-less inherits... */ -  /* Inherit */ -  INT16 inherit_flags_change = 0; -  int i; +  /* Number of identifier references. */ +  code_number(inh->prog->num_identifier_references, data);    -  code_number(ID_ENTRY_VARIABLE, data); -  -  /* Calculate id_flags */ -  for (i = 0; i < inh->prog->num_identifier_references; i++) { -  inherit_flags_change |= ref[i].id_flags ^ -  inh->prog->identifier_references[i].id_flags; +  inherit_num += inh->prog->num_inherits;    } -  inherit_flags_change &= ~(ID_HIDDEN|ID_INHERITED); -  code_number(inherit_flags_change); -  -  /* name */ -  str_sval.u.string = inh->name; -  encode_value2(&str_sval, data); -  -  /* prog */ -  ref_push_program(inh->prog); -  encode_value2(Pike_sp-1, data); -  pop_stack(); -  -  /* parent */ -  if (inh->prog) { -  ref_push_object(inh->parent); -  } else { -  push_int(0); -  Pike_sp[-1].subtype = NUMBER_UNDEFINED; +     } -  encode_value2(Pike_sp-1, data); -  pop_stack(); -  -  /* parent_identifier */ -  code_number(inh->parent_identifier); -  -  /* parent_offset */ -  code_number(inh->parent_offset); -  -  d += inh->prog->num_identifier_references; -  } - #endif /* 0 */ +     /* End-marker */    code_number(ID_ENTRY_EOT, data);    }    -  +  /* Encode the constant values table. */    {    struct svalue str_sval;    str_sval.type = T_STRING;    str_sval.subtype = 0;       /* constants */    for(d=0;d<p->num_constants;d++)    {    /* value */    encode_value2(&p->constants[d].sval, data);
pike.git/src/encode.c:2407:    PIKE_BYTEORDER, byteorder);    }       /* Decode flags. */    decode_number(p_flags,data);    p_flags &= ~(PROGRAM_FINISHED | PROGRAM_OPTIMIZED |    PROGRAM_FIXED | PROGRAM_PASS_1_DONE);    p_flags |= PROGRAM_AVOID_CHECK;       /* Start the new program. */ -  low_start_new_program(NULL, NULL, p_flags, NULL); +  low_start_new_program(NULL, NULL, 0, NULL);    p = Pike_compiler->new_program;    push_program(p);    -  +  p->flags = p_flags; +     SET_ONERROR(err, end_first_pass, 0);       debug_malloc_touch(p);    tmp.type=T_PROGRAM;    tmp.u.program=p;    EDB(2,fprintf(stderr, "%*sDecoding a program to <%d>: ",    data->depth, "", data->counter.u.integer);    print_svalue(stderr, &tmp);    fputc('\n', stderr););    mapping_insert(data->decoded, & data->counter, &tmp);
pike.git/src/encode.c:2522:    }    }       /* Decode identifier_references, inherits and identifiers. */    decode_number(entry_type, data);    while (entry_type != ID_ENTRY_EOT) {    decode_number(id_flags, data);    switch(entry_type) {    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");    }       /* type */    decode_value2(data);    if (Pike_sp[-1].type != T_TYPE) {    Pike_error("Bad variable type (not a type)\n");    }    -  +  /* Expected identifier offset */ +  decode_number(no, data); +     /* Alters    *    * storage, variable_index, identifiers and    * identifier_references    */ -  define_variable(Pike_sp[-2].u.string, Pike_sp[-1].u.string, -  id_flags); +  if (no != define_variable(Pike_sp[-2].u.string, +  Pike_sp[-1].u.string, +  id_flags)) { +  Pike_error("Bad variable identifier offset: %d\n", no); +  }       pop_n_elems(2);    }    break;    case ID_ENTRY_FUNCTION:    {    union idptr func;    unsigned INT8 func_flags;    unsigned INT16 opt_flags; -  +  int no;       /* 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:2576:    /* func */    decode_number(func.offset, data);       /* opt_flags */    decode_number(opt_flags, data);       /* FIXME:    * Verify validity of func_flags, func.offset & opt_flags    */    +  /* Expected identifier offset */ +  decode_number(no, data); +     /* Alters    *    * identifiers, identifier_references    */ -  define_function(Pike_sp[-2].u.string, +  if (no != define_function(Pike_sp[-2].u.string,    Pike_sp[-1].u.string, -  id_flags, func_flags, &func, opt_flags); +  id_flags, func_flags, +  &func, opt_flags)) { +  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;       /* 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:2619:    /* type */    decode_value2(data);    if (Pike_sp[-1].type != T_TYPE) {    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); +  } +    #ifdef PROFILING    id.self_time=0;    id.num_calls=0;    id.total_time=0;   #endif       /* id_flags */    ref.id_flags = id_flags;       /* identifier_offset */
pike.git/src/encode.c:2652:    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;       /* name */    decode_value2(data);    if (Pike_sp[-1].type == T_STRING) {    name = Pike_sp[-1].u.string;    } else if ((Pike_sp[-1].type != T_INT) ||    Pike_sp[-1].u.integer) {    Pike_error("Bad function name (not a string)\n");    }   
pike.git/src/encode.c:2685:    Pike_sp[-1].u.integer) {    Pike_error("Bad inherit: Parent isn't an object.\n");    }       /* parent_identifier */    decode_number(parent_identifier, data);       /* parent_offset */    decode_number(parent_offset, data);    +  /* Expected number of identifier references. */ +  decode_number(no, data); +  +  if (prog->num_identifier_references != no) { +  Pike_error("Bad number of identifiers in inherit: %d\n", no); +  } +     /* Alters    *    * storage, inherits and identifier_references    */    low_inherit(prog, parent, parent_identifier, -  parent_offset, id_flags, name); +  parent_offset + 42, id_flags, name);       pop_n_elems(3);    }    break;    default:    Pike_error("Unsupported id entry type: %d\n", entry_type);    }    decode_number(entry_type, data);    }