Exemplo n.º 1
0
/*
 *   Finish the file.  Closes the current block if one is open, and writes
 *   the end-of-file marker to the file. 
 */
void CVmImageWriter::finish()
{
    /* if there's a block open, close it */
    end_block();

    /* 
     *   write the EOF block - the block contains no data, so simply begin
     *   and end it 
     */
    begin_block("EOF ", TRUE);
    end_block();
}
Exemplo n.º 2
0
static void loop_body(struct whiledata *wdata, fncode fn)
{
  set_label(wdata->mainlab, fn);
  start_block(NULL, TRUE, TRUE, fn);
  if (wdata->continue_label)
    start_block(wdata->continue_label, TRUE, TRUE, fn);
  generate_component(wdata->code, NULL, TRUE, fn);
  if (wdata->continue_label)
    end_block(fn);
  end_block(fn);
  if (wdata->next)
    generate_component(wdata->next, NULL, TRUE, fn);
}
Exemplo n.º 3
0
/*
 *   Write an entrypoint (ENTP) block
 */
void CVmImageWriter::write_entrypt(uint32 entry_ofs, size_t method_hdr_size,
                                   size_t exc_entry_size,
                                   size_t line_entry_size,
                                   size_t dbg_hdr_size,
                                   size_t dbg_lclsym_hdr_size,
                                   size_t dbg_frame_hdr_size,
                                   int dbg_vsn_id)
{
    char buf[32];
    
    /* prepare the block's contents */
    oswp4(buf, entry_ofs);
    oswp2(buf+4, method_hdr_size);
    oswp2(buf+6, exc_entry_size);
    oswp2(buf+8, line_entry_size);
    oswp2(buf+10, dbg_hdr_size);
    oswp2(buf+12, dbg_lclsym_hdr_size);
    oswp2(buf+14, dbg_vsn_id);
    oswp2(buf+16, dbg_frame_hdr_size);

    /* open the block, write the data, and close the block */
    begin_block("ENTP", TRUE);
    fp_->write_bytes(buf, 18);
    end_block();
}
Exemplo n.º 4
0
void generate_for(component init, component condition, component next,
		  component iteration, const char *continue_label,
		  bool discard, fncode fn)
{
  struct whiledata wdata;

  env_block_push(NULL); /* init may have local declarations */
  if (init)
    generate_component(init, NULL, TRUE, fn);

  start_block(NULL, FALSE, discard, fn);
  wdata.continue_label = continue_label;
  wdata.looplab = new_label(fn);
  wdata.mainlab = new_label(fn);
  wdata.endlab = new_label(fn);
  wdata.code = iteration;
  wdata.next = next;

  set_label(wdata.looplab, fn);
  if (condition)
    {
      generate_condition(condition, wdata.mainlab, wmain_code, &wdata,
			 wdata.endlab, NULL, NULL, fn);
      set_label(wdata.endlab, fn);
      if (!discard)
	generate_component(component_undefined, NULL, FALSE, fn);
    }
  else
    wmain_code(&wdata, fn);
  end_block(fn);
  env_block_pop();
}
Exemplo n.º 5
0
/*
 *   Begin a block
 */
void CVmImageWriter::begin_block(const char *block_id, int mandatory)
{
    char buf[10];
    uint flags;
    
    /* if there's a block currently open, close it */
    end_block();

    /* remember the seek location of the start of the block */
    block_start_ = fp_->get_pos();

    /* store the type string */
    memcpy(buf, block_id, 4);

    /* store four bytes of zeros as a placeholder for the size */
    memset(buf+4, 0, 4);

    /* compute the flags */
    flags = 0;
    if (mandatory)
        flags |= VMIMAGE_DBF_MANDATORY;

    /* store the flags */
    oswp2(buf+8, flags);

    /* write the header */
    fp_->write_bytes(buf, 10);
}
Exemplo n.º 6
0
static
do_block(stream, node, brk, cont, ret) {
    auto i = 0;
    start_block();
    while ( i < node[1] )
        stmt_code( stream, node[ 3 + i++ ], brk, cont, ret );
    end_block();
}
Exemplo n.º 7
0
void Profiler::end_frame()
{
    if (current_ != root_)
    {
        end_block();
        ++intervalFrames_;
        ++totalFrames_;
        if (!totalFrames_)
            ++totalFrames_;
        root_->end_frame();
        current_ = root_;
    }
}
Exemplo n.º 8
0
/*
 *   Write a constant pool definition block 
 */
void CVmImageWriter::write_pool_def(uint pool_id, uint32 page_count,
                                    uint32 page_size, int mandatory)
{
    char buf[16];
    
    /* prepare the block's data */
    oswp2(buf, pool_id);
    oswp4(buf+2, page_count);
    oswp4(buf+6, page_size);

    /* open the block, write the data, and end the block */
    begin_block("CPDF", mandatory);
    fp_->write_bytes(buf, 10);
    end_block();
}
Exemplo n.º 9
0
/*
 *   end a GSYM block 
 */
void CVmImageWriter::end_gsym_block(ulong cnt)
{
    long pos;

    /* remember the current file write position for a moment */
    pos = fp_->get_pos();

    /* go back and fix up the count in the header */
    fp_->set_pos(gsym_prefix_);
    fp_->write_uint4(cnt);

    /* seek back to the original position */
    fp_->set_pos(pos);
    
    /* end the block using the generic mechanism */
    end_block();
}
Exemplo n.º 10
0
/*
 *   end an object static data block
 */
void CVmImageWriter::end_objs_block(uint object_count)
{
    long pos;

    /* remember the current file write position for a moment */
    pos = fp_->get_pos();

    /* go back and fix up the object count in the header */
    fp_->set_pos(objs_prefix_);
    fp_->write_uint2(object_count);

    /* seek back to the original position */
    fp_->set_pos(pos);
    
    /* end the block */
    end_block();
}
Exemplo n.º 11
0
void generate_dowhile(component iteration, component condition,
		    const char *continue_label, bool discard, fncode fn)
{
  struct whiledata wdata;

  start_block(NULL, FALSE, discard, fn);
  wdata.continue_label = continue_label;
  wdata.looplab = new_label(fn);
  wdata.mainlab = new_label(fn);
  wdata.endlab = new_label(fn);
  wdata.code = iteration;
  wdata.next = NULL;

  loop_body(&wdata, fn);
  generate_condition(condition, wdata.mainlab, NULL, NULL,
		     wdata.endlab, NULL, NULL, fn);
  set_label(wdata.endlab, fn);
  if (!discard)
    generate_component(component_undefined, NULL, FALSE, fn);
  end_block(fn);
}
Exemplo n.º 12
0
/*
 *   end a symbolic names block 
 */
void CVmImageWriter::end_sym_block()
{
    long old_pos;
    char buf[4];
    
    /* end the block */
    end_block();

    /* 
     *   Go back and fix the header with the number of items we wrote.
     *   First, remember our current position, then seek back to the count
     *   prefix. 
     */
    old_pos = fp_->get_pos();
    fp_->set_pos(symd_prefix_);

    /* prepare the prefix, and write it out */
    oswp2(buf, symd_cnt_);
    fp_->write_bytes(buf, 2);

    /* restore the file position */
    fp_->set_pos(old_pos);
}
Exemplo n.º 13
0
/*
 *   Write a constant pool page 
 */
void CVmImageWriter::write_pool_page(uint pool_id, uint32 page_index,
                                     const char *page_data,
                                     uint32 page_data_size, int mandatory,
                                     uchar xor_mask)
{
    char buf[16];

    /* begin the block */
    begin_block("CPPG", mandatory);

    /* prepare the prefix */
    oswp2(buf, pool_id);
    oswp4(buf+2, page_index);
    buf[6] = xor_mask;

    /* write the prefix */
    fp_->write_bytes(buf, 7);

    /* write the page data, XOR'ing the data with the mask byte */
    xor_and_write_bytes(page_data, page_data_size, xor_mask);

    /* end the block */
    end_block();
}
Exemplo n.º 14
0
/*
 *   end a MACR block 
 */
void CVmImageWriter::end_macr_block()
{
    /* end the block using the generic mechanism */
    end_block();
}
Exemplo n.º 15
0
/*
 *   finish writing a pool page 
 */
void CVmImageWriter::end_pool_page()
{
    /* end the block */
    end_block();
}
Exemplo n.º 16
0
/*
 *   End a dependency block 
 */
void CVmImageWriter::end_dep_block()
{
    /* end the block */
    end_block();
}
Exemplo n.º 17
0
void generate_function(function f, fncode fn)
{
  struct code *c;
  struct string *help, *afilename, *varname;
  fncode newfn;
  vlist argument;
  u16 clen;
  i8 nargs;
  u8 nb_locals, *cvars;
  varlist closure, cvar;

  /* Code strings must be allocated before code (immutability restriction) */
  if (f->help)
    help = alloc_string(f->help);
  else
    help = NULL;
  GCPRO1(help);

  /* Make variable name (if present) */
  if (f->varname)
    varname = alloc_string(f->varname);
  else
    varname = NULL;
  GCPRO1(varname);

  /* Make filename string */
  afilename = make_filename(f->l.filename); 
  GCPRO1(afilename);

  if (f->varargs)
    /* varargs makes a vector from the first nargs entries of the stack and
       stores it in local value 0 */
    nargs = -1;
  else
    /* count the arguments */
    for (nargs = 0, argument = f->args; argument; argument = argument->next)
      nargs++;
  newfn = new_fncode(fnglobals(fn), f->l, FALSE, nargs);

  if (!f->varargs)
    {
      /* Generate code to check the argument types */
      for (nargs = 0, argument = f->args; argument; argument = argument->next) 
	{
	  if (argument->type != stype_any)
	    ins1(OPmvcheck4 + argument->type, nargs, newfn);

	  nargs++;
	}
    }

  /* Generate code of function */
  env_push(f->args, newfn);
  
  start_block("<return>", FALSE, FALSE, newfn);
  generate_component(f->value, NULL, FALSE, newfn);
  end_block(newfn);
  if (f->type != stype_any) ins0(OPmscheck4 + f->type, newfn);
  ins0(OPmreturn, newfn);
  closure = env_pop(&nb_locals);
  c = generate_fncode(newfn, nb_locals, help, varname, afilename, f->l.lineno);

  /* Generate code for creating closure */
  
  /* Count length of closure */
  clen = 0;
  for (cvar = closure; cvar; cvar = cvar->next) clen++;

  /* Generate closure */
  cvars = ins_closure(c, clen, fn);

  /* Add variables to it */
  for (cvar = closure; cvar; cvar = cvar->next)
    *cvars++ = (cvar->offset << 1) + cvar->vclass;

  delete_fncode(newfn);

  GCPOP(3);
}
Exemplo n.º 18
0
void generate_component(component comp, const char *mlabel, bool discard, fncode fn)
{
  clist args;

  switch (comp->vclass)
    {
    case c_assign:
      {
	u16 offset;
	mtype t;
	variable_class vclass = env_lookup(comp->l, comp->u.assign.symbol, &offset, &t, FALSE);
	component val = comp->u.assign.value;

	if (val->vclass == c_closure)
	  {
	    /* Defining a function, give it a name */
	    if (vclass == global_var)
	      val->u.closure->varname = comp->u.assign.symbol;
	    else
	      {
		char *varname = allocate(fnmemory(fn), strlen(comp->u.assign.symbol) + 7);

		sprintf(varname, "local-%s", comp->u.assign.symbol);
		val->u.closure->varname = varname;
	      }
	  }
	generate_component(comp->u.assign.value, NULL, FALSE, fn);
	if (t != stype_any)
	  ins0(OPmscheck4 + t, fn);
	if (vclass == global_var)
	  massign(comp->l, offset, comp->u.assign.symbol, fn);
	else if (vclass == closure_var)
	  ins1(OPmwritec, offset, fn);
	else
	  ins1(OPmwritel, offset, fn);
	/* Note: varname becomes a dangling pointer when fnmemory(fn) is
	   deallocated, but it is never used again so this does not cause
	   a problem. */
	break;
      }
    case c_recall:
      scompile_recall(comp->l, comp->u.recall, fn);
      break;
    case c_constant:
      ins_constant(make_constant(comp->u.cst, FALSE, fn), fn);
      break;
    case c_scheme:
      scheme_compile_mgc(comp->l, make_constant(comp->u.cst, TRUE, fn), discard, fn);
      discard = FALSE;
      break;
    case c_closure:
      generate_function(comp->u.closure, fn);
      break;
    case c_block:
      generate_block(comp->u.blk, discard, fn);
      discard = FALSE;
      break;
    case c_decl: 
      {
	vlist decl, next;

	/* declare variables one at a time (any x = y, y = 2; is an error) */
	for (decl = comp->u.decls; decl; decl = next)
	  {
	    next = decl->next;
	    decl->next = NULL;

	    env_declare(decl);
	    generate_decls(decl, fn);
	  }
	generate_component(component_undefined, NULL, FALSE, fn);
	break;
      }
    case c_labeled: {
      start_block(comp->u.labeled.name, FALSE, discard, fn);
      generate_component(comp->u.labeled.expression, comp->u.labeled.name, discard, fn);
      end_block(fn);
      discard = FALSE;
      break;
    }
    case c_exit:
      {
	bool discard_exit;
	label exitlab = exit_block(comp->u.labeled.name, FALSE, &discard_exit, fn);

	if (comp->u.labeled.expression != component_undefined && discard_exit)
	  warning(comp->l, "break result is ignored");
	generate_component(comp->u.labeled.expression, NULL, discard_exit, fn);
	if (exitlab)
	  branch(OPmba3, exitlab, fn);
	else 
	  {
	    if (!comp->u.labeled.name)
	      log_error(comp->l, "No loop to exit from");
	    else
	      log_error(comp->l, "No block labeled %s", comp->u.labeled.name);
	  }
	/* Callers expect generate_component to increase stack depth by 1  */
	if (discard_exit)
	  adjust_depth(1, fn);
	break;
      }
    case c_continue:
      {
	bool discard_exit; /* Meaningless for continue blocks */
	label exitlab = exit_block(comp->u.labeled.name, TRUE, &discard_exit, fn);

	if (exitlab)
	  branch(OPmba3, exitlab, fn);
	else 
	  {
	    if (comp->u.labeled.name[0] == '<')
	      log_error(comp->l, "No loop to continue");
	    else
	      log_error(comp->l, "No loop labeled %s", comp->u.labeled.name);
	  }
	/* Callers expect generate_component to increase stack depth by 1 (*/
	adjust_depth(1, fn);
	break;
      }
    case c_execute:
      {
	u16 count;

	generate_args(comp->u.execute->next, fn, &count);
	generate_execute(comp->u.execute->c, count, fn);
	break;
      }
    case c_builtin:
      args = comp->u.builtin.args;

      switch (comp->u.builtin.fn)
	{
	case b_if:
	  generate_if(args->c, args->next->c, NULL, TRUE, fn);
	  generate_component(component_undefined, NULL, FALSE, fn);
	  break;
	case b_ifelse:
	  generate_if(args->c, args->next->c, args->next->next->c, discard, fn);
	  discard = FALSE;
	  break;
	case b_sc_and: case b_sc_or:
	  generate_if(comp, component_true, component_false, discard, fn);
	  discard = FALSE;
	  break;

	case b_while:
	  enter_loop(fn);
	  generate_while(args->c, args->next->c, mlabel, discard, fn);
	  exit_loop(fn);
	  discard = FALSE;
	  break;

	case b_dowhile:
	  enter_loop(fn);
	  generate_dowhile(args->c, args->next->c, mlabel, discard, fn);
	  exit_loop(fn);
	  discard = FALSE;
	  break;

	case b_for:
	  enter_loop(fn);
	  generate_for(args->c, args->next->c, args->next->next->c,
		       args->next->next->next->c, mlabel, discard, fn);
	  exit_loop(fn);
	  discard = FALSE;
	  break;

	default:
	  {
	    u16 count;

	    assert(comp->u.builtin.fn < last_builtin);
	    generate_args(args, fn, &count);
	    ins0(builtin_ops[comp->u.builtin.fn], fn);
	    break;
	  }
	case b_cons:
	  {
	    u16 count;
	    u16 goffset;
	    mtype t;

	    assert(comp->u.builtin.fn < last_builtin);
	    generate_args(args, fn, &count);
	    goffset = global_lookup(fnglobals(fn),
				    builtin_functions[comp->u.builtin.fn], &t);
	    mexecute(comp->l, goffset, NULL, count, fn);
	    break;
	  }
	}
      break;
    default: assert(0);
    }
  if (discard)
    ins0(OPmpop, fn);
}
Exemplo n.º 19
0
static void generate_component(component comp, fncode fn)
{
  clist args;

  set_lineno(comp->lineno, fn);

  switch (comp->vclass)
    {
    case c_assign:
      {
	ulong offset;
        bool is_static;
	variable_class vclass = env_lookup(comp->u.assign.symbol, &offset,
                                           false, true, &is_static);
	component val = comp->u.assign.value;

	if (val->vclass == c_closure)
	  {
	    /* Defining a function, give it a name */
	    if (vclass == global_var)
	      val->u.closure->varname = comp->u.assign.symbol;
	    else
	      {
		char *varname = allocate(fnmemory(fn), strlen(comp->u.assign.symbol) + 7);

		sprintf(varname, "local-%s", comp->u.assign.symbol);
		val->u.closure->varname = varname;
	      }
	  }

        if (is_static)
          {
            ins1(op_recall + vclass, offset, fn);
            generate_component(comp->u.assign.value, fn);
            mexecute(g_symbol_set, NULL, 2, fn);
            break;
          }

	generate_component(comp->u.assign.value, fn);

	set_lineno(comp->lineno, fn);

        if (vclass == global_var)
	  massign(offset, comp->u.assign.symbol, fn);
	else
	  ins1(op_assign + vclass, offset, fn);
	/* Note: varname becomes a dangling pointer when fnmemory(fn) is
	   deallocated, but it is never used again so this does not cause
	   a problem. */
	break;
      }
    case c_vref:
    case c_recall:
      {
        bool is_vref = comp->vclass == c_vref;
	ulong offset;
        bool is_static;
	variable_class vclass = env_lookup(comp->u.recall, &offset,
                                           true, is_vref, &is_static);

        if (is_static)
          {
            assert(vclass != global_var);
            ins1(op_recall + vclass, offset, fn);
            ulong gidx = is_vref ? g_make_symbol_ref : g_symbol_get;
            mexecute(gidx, NULL, 1, fn);
            break;
          }
	if (vclass != global_var)
          ins1((is_vref ? op_vref : op_recall) + vclass, offset, fn);
        else if (is_vref)
          {
            if (!mwritable(offset, comp->u.recall))
              return;
            ins_constant(makeint(offset), fn);
          }
        else
          mrecall(offset, comp->u.recall, fn);
        if (is_vref)
          mexecute(g_make_variable_ref, "make_variable_ref", 1, fn);
	break;
      }
    case c_constant:
      ins_constant(make_constant(comp->u.cst), fn);
      break;
    case c_closure:
      {
	uword idx;

	idx = add_constant(generate_function(comp->u.closure, false, fn), fn);
	if (idx < ARG1_MAX) ins1(op_closure_code1, idx, fn);
	else ins2(op_closure_code2, idx, fn);
	break;
      }
    case c_block:
      generate_block(comp->u.blk, fn);
      break;
    case c_labeled:
      start_block(comp->u.labeled.name, fn);
      generate_component(comp->u.labeled.expression, fn);
      end_block(fn);
      break;
    case c_exit:
      generate_component(comp->u.labeled.expression, fn);
      if (!exit_block(comp->u.labeled.name, fn)) {
	if (!comp->u.labeled.name)
	  log_error("no loop to exit from");
	else
	  log_error("no block labeled %s", comp->u.labeled.name);
      }
      break;
    case c_execute:
      {
	uword count;

	generate_args(comp->u.execute->next, fn, &count);
	set_lineno(comp->lineno, fn);
	generate_execute(comp->u.execute->c, count, fn);
	break;
      }
    case c_builtin:
      args = comp->u.builtin.args;

      switch (comp->u.builtin.fn)
	{
	case b_if: {
          block cb = new_codeblock(fnmemory(fn), NULL,
                                   new_clist(fnmemory(fn), args->next->c,
                                             new_clist(fnmemory(fn),
                                                       component_undefined,
                                                       NULL)),
                                   NULL, NULL, -1);
	  generate_if(args->c, new_component(fnmemory(fn),
                                             args->next->c->lineno,
                                             c_block, cb),
		      component_undefined, fn);
	  break;
        }
	case b_ifelse:
	  generate_if(args->c, args->next->c, args->next->next->c, fn);
	  break;
	case b_sc_and: case b_sc_or:
	  generate_if(comp, component_true, component_false, fn);
	  break;

	case b_while:
	  generate_while(args->c, args->next->c, fn);
	  break;

	case b_loop:
	  {
	    label loop = new_label(fn);

            env_start_loop();
	    set_label(loop, fn);
	    start_block(NULL, fn);
	    generate_component(args->c, fn);
	    branch(op_loop1, loop, fn);
	    end_block(fn);
            env_end_loop();
	    adjust_depth(1, fn);
	    break;
	  }

	case b_add: case b_subtract:
	case b_ref: case b_set:
	case b_bitor: case b_bitand:
	case b_not:
	case b_eq: case b_ne:
	case b_lt: case b_le: case b_ge: case b_gt:
	  {
	    uword count;

	    assert(comp->u.builtin.fn < last_builtin);
	    generate_args(args, fn, &count);
	    set_lineno(comp->lineno, fn);
	    ins0(builtin_ops[comp->u.builtin.fn], fn);
	    break;
	  }
	default:
	  {
	    uword count;

	    assert(comp->u.builtin.fn < last_builtin);
	    generate_args(args, fn, &count);
	    set_lineno(comp->lineno, fn);
	    mexecute(builtin_functions[comp->u.builtin.fn], NULL, count, fn);
	    break;
	  }
	}
      break;
    default: abort();
    }
}
Exemplo n.º 20
0
/*
** Translate the input stream into the output stream
*/
static void trans(FILE *in, FILE *out){
  int i, j, k;          /* Loop counters */
  char c1, c2;          /* Characters used to start a comment */
  int lastWasEq = 0;    /* True if last non-whitespace character was "=" */
  int lastWasComma = 0; /* True if last non-whitespace character was "," */
  char zLine[2000];     /* A single line of input */
  char zOut[4000];      /* The input line translated into appropriate output */

  c1 = c2 = '-';
  while( fgets(zLine, sizeof(zLine), in) ){
    for(i=0; zLine[i] && isspace(zLine[i]); i++){}
    if( zLine[i]!='@' ){
      if( inPrint || inStr ) end_block(out);
      fprintf(out,"%s",zLine);
                       /* 0123456789 12345 */
      if( strncmp(zLine, "/* @-comment: ", 14)==0 ){
        c1 = zLine[14];
        c2 = zLine[15];
      }
      i += strlen(&zLine[i]);
      while( i>0 && isspace(zLine[i-1]) ){ i--; }
      lastWasEq    = i>0 && zLine[i-1]=='=';
      lastWasComma = i>0 && zLine[i-1]==',';
    }else if( lastWasEq || lastWasComma){
      /* If the last non-whitespace character before the first @ was
      ** an "="(var init/set) or a ","(const definition in list) then
      ** generate a string literal.  But skip comments
      ** consisting of all text between c1 and c2 (default "--")
      ** and end of line.
      */
      int indent, omitline;
      i++;
      if( isspace(zLine[i]) ){ i++; }
      indent = i - 2;
      if( indent<0 ) indent = 0;
      omitline = 0;
      for(j=0; zLine[i] && zLine[i]!='\r' && zLine[i]!='\n'; i++){
        if( zLine[i]==c1 && (c2==' ' || zLine[i+1]==c2) ){
           omitline = 1; break; 
        }
        if( zLine[i]=='"' || zLine[i]=='\\' ){ zOut[j++] = '\\'; }
        zOut[j++] = zLine[i];
      }
      while( j>0 && isspace(zOut[j-1]) ){ j--; }
      zOut[j] = 0;
      if( j<=0 && omitline ){
        fprintf(out,"\n");
      }else{
        fprintf(out,"%*s\"%s\\n\"\n",indent, "", zOut);
      }
    }else{
      /* Otherwise (if the last non-whitespace was not '=') then generate
      ** a cgi_printf() statement whose format is the text following the '@'.
      ** Substrings of the form "%C(...)" where C is any character will
      ** puts "%C" in the format and add the "..." as an argument to the
      ** cgi_printf call.
      */
      int indent;
      i++;
      if( isspace(zLine[i]) ){ i++; }
      indent = i;
      for(j=0; zLine[i] && zLine[i]!='\r' && zLine[i]!='\n'; i++){
        if( zLine[i]=='"' || zLine[i]=='\\' ){ zOut[j++] = '\\'; }
        zOut[j++] = zLine[i];
        if( zLine[i]!='%' || zLine[i+1]=='%' || zLine[i+1]==0 ) continue;
        if( zLine[i+2]!='(' ) continue;
        i++;
        zOut[j++] = zLine[i];
        zArg[nArg++] = ',';
        i += 2;
        k = 1;
        while( zLine[i] ){
          if( zLine[i]==')' ){
            k--;
            if( k==0 ) break;
          }else if( zLine[i]=='(' ){
            k++;
          }
          zArg[nArg++] = zLine[i++];
        }
      }
      zOut[j] = 0;
      if( !inPrint ){
        fprintf(out,"%*scgi_printf(\"%s\\n\"",indent-2,"", zOut);
        inPrint = 1;
      }else{
        fprintf(out,"\n%*s\"%s\\n\"",indent+5, "", zOut);
      }
    }      
  }
}
Exemplo n.º 21
0
void run_kmp ()
{
  nkmp = 0;
  int cur_c = 0, cur_i = 0;
  __int64 pos_c = 1, pos_i = 0;
  while (pos_c >= str[cur_c].b)
    cur_c++;
  char c_c = str[cur_c].c;
  char c_i = str[cur_i].c;
  int i_vel;
  if (c_c == c_i)
  {
    start_block (pos_c, pos_i + 1, 1);
    i_vel = 1;
  }
  else
  {
    start_block (pos_c, 0, 0);
    i_vel = 0;
  }
  __int64 time = 0;
  add_event (EVENT_CURSOR, str[cur_c].b - pos_c + time);
  add_event (EVENT_IMAGE, str[cur_i].b - pos_i + time);
  while (pos_c < len)
  {
    event e = next_event ();
    __int64 delta = e.time - time;
    time = e.time;
    pos_c += delta;
    pos_i += delta * i_vel;
    while (pos_i >= str[cur_i].b)
      cur_i++;
    while (pos_c >= str[cur_c].b)
      cur_c++;
    char c_c = str[cur_c].c;
    char c_i = str[cur_i].c;
    if (pos_c >= len)
      break;
    end_block (pos_c);
#ifdef _DBG_
    printf ("$%I64d (%d) %I64d (%d)\n", pos_i, cur_i, pos_c, cur_c);
#endif
    while (c_c != c_i && pos_i > 0)
    {
#ifdef _DBG_
      printf ("!jump from %I64d", pos_i - 1);
#endif
      pos_i = get_answer (pos_i - 1);
      cur_i = bin_search (pos_i);
      c_i = str[cur_i].c;
#ifdef _DBG_
      printf (" to %I64d (%d)\n", pos_i, cur_i);
#endif
    }
    if (c_c == c_i)
    {
      start_block (pos_c, pos_i + 1, 1);
      i_vel = 1;
    }
    else
    {
      start_block (pos_c, 0, 0);
      i_vel = 0;
    }
    add_event (EVENT_CURSOR, str[cur_c].b - pos_c + time);
    add_event (EVENT_IMAGE, str[cur_i].b - pos_i + time);
  }
  end_block (len);
}
Exemplo n.º 22
0
void reader::read_infercars(const ProblemInstance& cfg, std::vector<Genome>& genome) {
	
	std::ifstream input(cfg.get_blk_file().c_str());
	if(!input) {
		std::cerr << "Unable to open " << cfg.get_blk_file() << std::endl;
		exit(1);
	}
    
	std::string gene;
	std::vector<std::string> chromosome(cfg.get_count_genomes());
	std::vector<std::string> sign(cfg.get_count_genomes()); 
	std::vector<int> start_block(cfg.get_count_genomes());  
	std::vector<int> end_block(cfg.get_count_genomes()); 
	std::vector<int> count_block(cfg.get_count_genomes());

	while(!input.eof()) {
		std::string line;
		std::getline(input, line);
    		line = reader::trim(line);
 
		if (line.empty()) {
			if (gene.empty()) { 	
				continue;
    			} 

			bool ambig = false;
			for(int i = 0; i < count_block.size(); ++i) { 
				if (count_block[i] != 1) { 
					ambig = true;
					break;
				}  
			} 	
			if (ambig) {
				std::clog << "Ambiguous block: " << gene << std::endl;
			    	continue;
			}

			for(int i = 0; i < count_block.size(); ++i) { 
				if (count_block[i] == 1) {
					int sign_ = (sign[i] == "+")? +1: -1;
					genome[i].insert(gene, chromosome[i], (start_block[i] + end_block[i]) / 2, sign_, std::min(start_block[i], end_block[i]), std::max(start_block[i], end_block[i]));         				
				}
			} 
			gene.clear();
		} else if (line[0] == '#') { 
				continue;
    		} else if(line[0] == '>') {
			gene = line.substr(1);
			std::fill(count_block.begin(), count_block.end(), 0);
		} else { 
    			line[line.find(".")] = ' ';
			line[line.find(":")] = ' ';
			line[line.find("-")] = ' ';
	    
			std::istringstream istr(line);
 
			std::string genome_name;
 			istr >> genome_name;
    			if (!cfg.member_name(genome_name)) {
				std::cerr << "Unknown genome name: " << genome_name << std::endl;
				continue;
    			}

			size_t k = cfg.get_number(genome_name);
			istr >> chromosome[k] >> start_block[k] >> end_block[k] >> sign[k];
			++count_block[k];
		}
	} 
	input.close();
}
Exemplo n.º 23
0
static struct icode *generate_function(function f, bool toplevel, fncode fn)
{
  /* make help string; must be allocated before code (immutability
     restriction) */
  struct string *help = NULL;
  if (f->help.len)
    help = make_readonly(alloc_string_length(f->help.str, f->help.len));
  struct string *varname = NULL, *filename = NULL, *nicename = NULL;
  struct vector *arg_types = NULL;
  GCPRO5(help, varname, filename, nicename, arg_types);

  /* Make variable name (if present) */
  if (f->varname)
    varname = make_readonly(alloc_string(f->varname));
  else
    varname = NULL;

  /* Make filename string */
  filename = make_filename(f->filename);
  nicename = make_filename(f->nicename);

  arg_types = make_arg_types(f);

  fncode newfn = new_fncode(toplevel);

  set_lineno(f->lineno, newfn);

  if (f->varargs)
    /* varargs makes a vector from the first nargs entries of the stack and
       stores it in local value 0 */
    ins0(op_varargs, newfn);
  else
    {
      /* First, generate code to check the argument types & count */
      /* argcheck copies the arguments into the local variables, assuming that
	 the last argument (on top of the stack) is local value 0, the next to
	 last local value 1, and so on.
	 It then discards all the parameters */
      int nargs = 0;
      for (vlist argument = f->args; argument; argument = argument->next)
	nargs++;
      ins1(op_argcheck, nargs, newfn);

      nargs = 0;
      for (vlist argument = f->args; argument; argument = argument->next)
	{
          generate_typeset_check(argument->typeset, nargs, newfn);
	  nargs++;
	}
      ins1(op_pop_n, nargs, newfn);
    }

  /* Generate code of function */
  env_push(f->args, newfn);

  start_block("function", newfn);
  generate_component(f->value, newfn);
  end_block(newfn);

  generate_typeset_check(f->typeset, 0, newfn);

  ins0(op_return, newfn);
  peephole(newfn);

  struct icode *c = generate_fncode(
    newfn, help, varname, filename, nicename, f->lineno, arg_types,
    f->typeset, compile_level);
  varlist closure = env_pop(&c->nb_locals);

  UNGCPRO();

  /* Generate code for creating closure */

  /* Count length of closure */
  int clen = 0;
  for (varlist cvar = closure; cvar; cvar = cvar->next) clen++;

  /* Generate closure */
  ins1(op_closure, clen, fn);

  /* Add variables to it */
  for (varlist cvar = closure; cvar; cvar = cvar->next)
    ins1(op_closure_var + cvar->vclass, cvar->offset, fn);

  delete_fncode(newfn);

  return c;
}