Exemple #1
0
	bool operator()(SerializedCall const &serialized_call_, FusionVector &fusion_vector, ParamValueIter iter, ParamValueIter end) const
	{
		if (iter == end)
		{
			typename serialized_call_traits < SerializedCall > ::parameters_t parameters = get_parameters(serialized_call_);
			throw missing_call_parameters(get_function_name(serialized_call_), std::distance(parameters.begin(), parameters.end()));
		}

		typedef typename boost::remove_cv < typename boost::fusion::result_of::value_of < FusionVectorIter > ::type > ::type param_type_t;

		// Retrieve the value from the current element parameter sequence. The current element is (*iter).

		param_type_t param_value;
		if (get_parameter_value(serialized_call_, *iter, param_value))
		{
			// Store the value in the fusion vector
			// TODO: use fusion iterators instead of an index (if it makes sense; run-time fusion iterators need to be used here)
			boost::fusion::at_c < Index > (fusion_vector) = param_value;

			// Move to the next element
			ParamValueIter next_iter = iter;
			std::advance(next_iter, 1);

			// Next iteration (using recursion)
			return set_parameter_fusion_vector_value < SerializedCall, FusionVector, typename boost::fusion::result_of::next < FusionVectorIter > ::type, ParamValueIter, Index + 1 > ()(serialized_call_, fusion_vector, next_iter, end);
		}
		else
			return false;
	}
 bool get_display_data(pfc::string_base & p_out,unsigned & p_state) const
 {
     p_out = get_function_name(func_idx);
     if (checked != NULL && *checked)
         p_state = state_checked;
     else
         p_state = 0;
     return true;
 }
Exemple #3
0
 std::string get_function_name(boost::exception_ptr const& e)
 {
     try {
         boost::rethrow_exception(e);
     }
     catch (boost::exception const& be) {
         return get_function_name(be);
     }
 }
Exemple #4
0
void quan::report_errors()
{
   while ( ! error_fifo.is_empty()){
      error_info const & info = error_fifo.get();
      user_message("in fn ");
      user_message(get_function_name(info.function_id));
      user_message( " : ");
      user_message(get_error_string(info.error_id));
      user_message("\n");
   }
}
Exemple #5
0
/**
* Verifies ESR function call result and writes a ERROR log message
* @param e error code
* @param function name of function invoked
* @return true if no error happened, false otherwise
*/
bool esr::verify(esr::error e, esr::function_name function)
{
	if(e != esr::E_OK)
	{
		const __FlashStringHelper* function_name = get_function_name(function);
		esr::log(esr::LOG_ERROR, F("esr::%ps() failed with %e"), function_name, e);
		return false;
	}

	return true;
}
Exemple #6
0
/* Call hook (executed before any instruction in the function) */
static int getsockname_call(void *opaque)
{
  uint32_t esp;
  uint32_t eip;
  uint32_t buf[7]; // Assumes that all stack parameters are 4-byte long
  int read_err = 0;

  /* If not tracing yet, return */
  if (tracepid == 0) return 0;

  /* Read stack starting at ESP */
  read_reg(esp_reg, &esp);
  read_err = read_mem(esp, sizeof(buf), (unsigned char*)buf);
  if (read_err) return 0;

  /*
      BUF INDEX -> PARAMETER
      ws2_32.dll getsockname
      int getsockname(SOCKET s,struct sockaddr* name,int* namelen);
      0 -> return address
      1 -> IN socket descriptor
      2 -> OUT Address structure with socket information
      3 -> IN-OUT On call, size of the name buffer, in bytes.
        On return, size in bytes of the name parameter
  */

  /* Check which function we are jumping to */
  read_reg(eip_reg, &eip);
  char mod_name[512];
  char fun_name[512];
  get_function_name(eip,(char *)&mod_name,(char *)&fun_name);

  /* Print some information to monitor */
  WRITE("tracenetlog","Getting socket info using function %s::%s\n"
    "\tFD: %u BufStart: 0x%08x BufMaxLen: %d\n",
    mod_name, fun_name,buf[1],buf[2],(int)buf[3]);

  /* Store parameters so that they can be used by return hook */
  getsockname_t *s = malloc(sizeof(getsockname_t));
  if (s == NULL) return 0;
  s->eip = eip;
  s->sd = buf[1];
  s->bufStart = buf[2];
  s->bufMaxLen = buf[3];
  s->bufLenPtr = esp+12;

  /* Hook return of function */
  s->hook_handle = hookapi_hook_return(buf[0], getsockname_ret, s,
    sizeof(getsockname_t));

  return 0;
}
Exemple #7
0
void sccwriter::write_function_header( std::ostream& os, int index, int opts )
{
  //write the function header
  std::string fname;
  get_function_name( progNames[index], fname );
  os << "Expr* " << fname.c_str() << "( ";
  CExpr* progvars = (CExpr*)get_prog( index )->kids[1];
  int counter = 0;
  //write each argument
  while( progvars->kids[counter] )
  {
    if( counter!=0 )
    {
      os << ", ";
    }
    os << "Expr* ";
    write_variable( ((SymSExpr*)progvars->kids[counter])->s, os );
    //add to vars if options are set to do so
    if( opts&opt_write_add_args )
    {
      vars.push_back( ((SymSExpr*)progvars->kids[counter])->s );
    }
    counter++;
  }
  os << " )";
  if( opts&opt_write_call_debug )
  {
     os << "{" << std::endl;
     indent( os, 1 );
     os << "std::cout << \"Call function " << fname.c_str() << " with arguments \";" << std::endl;
     counter = 0; 
     while( progvars->kids[counter] )
     {
        if( counter!=0 )
        {
           indent( os, 1 );
           os << "std::cout << \", \";" << std::endl;
        }
        indent( os, 1 );
        write_variable( ((SymSExpr*)progvars->kids[counter])->s, os );
        os << "->print( std::cout );" << std::endl;
        counter++;
     }
     indent( os, 1 );
     os << "std::cout << std::endl;" << std::endl;
  }
}
Exemple #8
0
static toyvm_compiled_function *
toyvm_function_compile (toyvm_function *fn)
{
  compilation_state state;
  int pc;
  char *funcname;

  memset (&state, 0, sizeof (state));

  funcname = get_function_name (fn->fn_filename);

  state.ctxt = gcc_jit_context_acquire ();

  gcc_jit_context_set_bool_option (state.ctxt,
				   GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE,
				   0);
  gcc_jit_context_set_bool_option (state.ctxt,
				   GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE,
				   0);
  gcc_jit_context_set_int_option (state.ctxt,
				  GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL,
				  3);
  gcc_jit_context_set_bool_option (state.ctxt,
				   GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES,
				   0);
  gcc_jit_context_set_bool_option (state.ctxt,
				   GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING,
				   0);
  gcc_jit_context_set_bool_option (state.ctxt,
				   GCC_JIT_BOOL_OPTION_DEBUGINFO,
				   1);

  /* Create types.  */
  state.int_type =
    gcc_jit_context_get_type (state.ctxt, GCC_JIT_TYPE_INT);
  state.bool_type =
    gcc_jit_context_get_type (state.ctxt, GCC_JIT_TYPE_BOOL);
  state.stack_type =
    gcc_jit_context_new_array_type (state.ctxt, NULL,
				    state.int_type, MAX_STACK_DEPTH);

  /* The constant value 1.  */
  state.const_one = gcc_jit_context_one (state.ctxt, state.int_type);

  /* Create locations.  */
  for (pc = 0; pc < fn->fn_num_ops; pc++)
    {
      toyvm_op *op = &fn->fn_ops[pc];

      state.op_locs[pc] = gcc_jit_context_new_location (state.ctxt,
							fn->fn_filename,
							op->op_linenum,
							0); /* column */
    }

  /* Creating the function.  */
  state.param_arg =
    gcc_jit_context_new_param (state.ctxt, state.op_locs[0],
			       state.int_type, "arg");
  state.fn =
    gcc_jit_context_new_function (state.ctxt,
				  state.op_locs[0],
				  GCC_JIT_FUNCTION_EXPORTED,
				  state.int_type,
				  funcname,
				  1, &state.param_arg, 0);

  /* Create stack lvalues.  */
  state.stack =
    gcc_jit_function_new_local (state.fn, NULL,
				state.stack_type, "stack");
  state.stack_depth =
    gcc_jit_function_new_local (state.fn, NULL,
				state.int_type, "stack_depth");
  state.x =
    gcc_jit_function_new_local (state.fn, NULL,
				state.int_type, "x");
  state.y =
    gcc_jit_function_new_local (state.fn, NULL,
				state.int_type, "y");

  /* 1st pass: create blocks, one per opcode. */

  /* We need an entry block to do one-time initialization, so create that
     first.  */
  state.initial_block = gcc_jit_function_new_block (state.fn, "initial");

  /* Create a block per operation.  */
  for (pc = 0; pc < fn->fn_num_ops; pc++)
    {
      char buf[16];
      sprintf (buf, "instr%i", pc);
      state.op_blocks[pc] = gcc_jit_function_new_block (state.fn, buf);
    }

  /* Populate the initial block.  */

  /* "stack_depth = 0;".  */
  gcc_jit_block_add_assignment (
    state.initial_block,
    state.op_locs[0],
    state.stack_depth,
    gcc_jit_context_zero (state.ctxt, state.int_type));

  /* "PUSH (arg);".  */
  add_push (&state,
	    state.initial_block,
	    gcc_jit_param_as_rvalue (state.param_arg),
	    state.op_locs[0]);

  /* ...and jump to insn 0.  */
  gcc_jit_block_end_with_jump (state.initial_block,
			       state.op_locs[0],
			       state.op_blocks[0]);

  /* 2nd pass: fill in instructions.  */
  for (pc = 0; pc < fn->fn_num_ops; pc++)
    {
      gcc_jit_location *loc = state.op_locs[pc];

      gcc_jit_block *block = state.op_blocks[pc];
      gcc_jit_block *next_block = (pc < fn->fn_num_ops
				   ? state.op_blocks[pc + 1]
				   : NULL);

      toyvm_op *op;
      op = &fn->fn_ops[pc];

      /* Helper macros.  */

#define X_EQUALS_POP()\
      add_pop (&state, block, state.x, loc)
#define Y_EQUALS_POP()\
      add_pop (&state, block, state.y, loc)
#define PUSH_RVALUE(RVALUE)\
      add_push (&state, block, (RVALUE), loc)
#define PUSH_X()\
      PUSH_RVALUE (gcc_jit_lvalue_as_rvalue (state.x))
#define PUSH_Y() \
      PUSH_RVALUE (gcc_jit_lvalue_as_rvalue (state.y))

      gcc_jit_block_add_comment (block, loc, opcode_names[op->op_opcode]);

      /* Handle the individual opcodes.  */

      switch (op->op_opcode)
	{
	case DUP:
	  X_EQUALS_POP ();
	  PUSH_X ();
	  PUSH_X ();
	  break;

	case ROT:
	  Y_EQUALS_POP ();
	  X_EQUALS_POP ();
	  PUSH_Y ();
	  PUSH_X ();
	  break;

	case BINARY_ADD:
	  Y_EQUALS_POP ();
	  X_EQUALS_POP ();
	  PUSH_RVALUE (
	   gcc_jit_context_new_binary_op (
	     state.ctxt,
	     loc,
	     GCC_JIT_BINARY_OP_PLUS,
	     state.int_type,
	     gcc_jit_lvalue_as_rvalue (state.x),
	     gcc_jit_lvalue_as_rvalue (state.y)));
	  break;

	case BINARY_SUBTRACT:
	  Y_EQUALS_POP ();
	  X_EQUALS_POP ();
	  PUSH_RVALUE (
	   gcc_jit_context_new_binary_op (
	     state.ctxt,
	     loc,
	     GCC_JIT_BINARY_OP_MINUS,
	     state.int_type,
	     gcc_jit_lvalue_as_rvalue (state.x),
	     gcc_jit_lvalue_as_rvalue (state.y)));
	  break;

	case BINARY_MULT:
	  Y_EQUALS_POP ();
	  X_EQUALS_POP ();
	  PUSH_RVALUE (
	   gcc_jit_context_new_binary_op (
	     state.ctxt,
	     loc,
	     GCC_JIT_BINARY_OP_MULT,
	     state.int_type,
	     gcc_jit_lvalue_as_rvalue (state.x),
	     gcc_jit_lvalue_as_rvalue (state.y)));
	  break;

	case BINARY_COMPARE_LT:
	  Y_EQUALS_POP ();
	  X_EQUALS_POP ();
	  PUSH_RVALUE (
	     /* cast of bool to int */
	     gcc_jit_context_new_cast (
	       state.ctxt,
	       loc,
	       /* (x < y) as a bool */
	       gcc_jit_context_new_comparison (
		 state.ctxt,
		 loc,
		 GCC_JIT_COMPARISON_LT,
		 gcc_jit_lvalue_as_rvalue (state.x),
		 gcc_jit_lvalue_as_rvalue (state.y)),
	       state.int_type));
	  break;

	case RECURSE:
	  {
	    X_EQUALS_POP ();
	    gcc_jit_rvalue *arg = gcc_jit_lvalue_as_rvalue (state.x);
	    PUSH_RVALUE (
	      gcc_jit_context_new_call (
		state.ctxt,
		loc,
		state.fn,
		1, &arg));
	    break;
	  }

	case RETURN:
	  X_EQUALS_POP ();
	  gcc_jit_block_end_with_return (
	    block,
	    loc,
	    gcc_jit_lvalue_as_rvalue (state.x));
	  break;

	  /* Ops taking an operand.  */
	case PUSH_CONST:
	  PUSH_RVALUE (
	    gcc_jit_context_new_rvalue_from_int (
	      state.ctxt,
	      state.int_type,
	      op->op_operand));
	  break;

	case JUMP_ABS_IF_TRUE:
	  X_EQUALS_POP ();
	  gcc_jit_block_end_with_conditional (
	    block,
	    loc,
	    /* "(bool)x".  */
	    gcc_jit_context_new_cast (
	      state.ctxt,
	      loc,
	      gcc_jit_lvalue_as_rvalue (state.x),
	      state.bool_type),
	    state.op_blocks[op->op_operand], /* on_true */
	    next_block); /* on_false */
	  break;

	default:
	  assert(0);
	} /* end of switch on opcode */

      /* Go to the next block.  */
      if (op->op_opcode != JUMP_ABS_IF_TRUE
	  && op->op_opcode != RETURN)
	gcc_jit_block_end_with_jump (
	  block,
	  loc,
	  next_block);

    } /* end of loop on PC locations.  */

  /* We've now finished populating the context.  Compile it.  */
  gcc_jit_result *jit_result = gcc_jit_context_compile (state.ctxt);
  gcc_jit_context_release (state.ctxt);

  toyvm_compiled_function *toyvm_result =
    (toyvm_compiled_function *)calloc (1, sizeof (toyvm_compiled_function));
  if (!toyvm_result)
    {
      fprintf (stderr, "out of memory allocating toyvm_compiled_function\n");
      gcc_jit_result_release (jit_result);
      return NULL;
    }

  toyvm_result->cf_jit_result = jit_result;
  toyvm_result->cf_code =
    (toyvm_compiled_code)gcc_jit_result_get_code (jit_result,
						  funcname);

  free (funcname);

  return toyvm_result;
}
Exemple #9
0
static statement_t* build_ident_expr(expr_t* redexpr,datatype_t* pdt, expr_t* tempexpr) {
  char name[256];
  char *id;
  symboltable_t* identpst;
  expr_t* identexpr;
  expr_t* assignexpr;
  statement_t* assignstmt;

  switch (T_SUBTYPE(redexpr)) {
  case PLUS:
    sprintf(name,"_ADD");
    break;
  case TIMES:
    sprintf(name,"_MULT");
    break;
  case MIN:
    sprintf(name,"_MIN");
    break;
  case MAX:
    sprintf(name,"_MAX");
    break;
  case AND:
    sprintf(name,"_AND");
    break;
  case OR:
    sprintf(name,"_OR");
    break;
  case BAND:
    sprintf(name,"_BAND");
    break;
  case BOR:
    sprintf(name,"_BOR");
    break;
  case BXOR:
    sprintf(name,"_XOR");
    break;
  case USER:
    id = get_function_name(T_IDENT(redexpr), NULL, 1);
    if (id == NULL) {
      id = get_function_name(T_IDENT(redexpr), tempexpr, 1);
      identpst = lu(id);
      if (id == NULL || S_CLASS(S_FUN_TYPE(identpst)) != DT_VOID) {
	USR_FATALX(T_LINENO(T_STMT(redexpr)), T_FILENAME(T_STMT(redexpr)),
		   "Missing identity function for user defined reduction");
      }
      identexpr = build_Nary_op(FUNCTION, build_0ary_op(VARIABLE, check_var(id)), tempexpr);
      assignstmt = build_expr_statement(identexpr,T_LINENO(T_STMT(redexpr)),T_FILENAME(T_STMT(redexpr)));
      return assignstmt;
    }
    if (get_function_name(T_IDENT(redexpr), tempexpr, 1)) {
      USR_FATALX(T_LINENO(T_STMT(redexpr)), T_FILENAME(T_STMT(redexpr)),
		 "Multiple identity functions for user defined reduction");
    }
    identpst = lu(id);
    if (!(equiv_datatypes(pdt, S_FUN_TYPE(identpst)))) {
      USR_FATALX(T_LINENO(T_STMT(redexpr)), T_FILENAME(T_STMT(redexpr)),
		 "Identity function for user defined reduction returns wrong type");
    }
    identexpr = build_Nary_op(FUNCTION, build_0ary_op(VARIABLE, check_var(id)), NULL);
    assignexpr = build_typed_binary_op(BIASSIGNMENT, identexpr, tempexpr);
    assignstmt = build_expr_statement(assignexpr,T_LINENO(T_STMT(redexpr)),T_FILENAME(T_STMT(redexpr)));
    return assignstmt;
    break;
  default:
    sprintf(name,"_NOP");
    break;
  }
  append_sr_pdt(name,pdt);
  sprintf(name,"%s_IDENTITY",name);
  identpst = lookup(S_VARIABLE,name);
  if (identpst == NULL) {
    INT_FATAL(T_STMT(redexpr),"Couldn't find identity %s\n",name);
  }
  identexpr = build_typed_0ary_op(CONSTANT,identpst);
  assignexpr = build_typed_binary_op(BIASSIGNMENT, identexpr, tempexpr);
  assignstmt = build_expr_statement(assignexpr,T_LINENO(T_STMT(redexpr)),T_FILENAME(T_STMT(redexpr)));

  return assignstmt;
}
Exemple #10
0
static void process_reduction(expr_t* redexpr) {
  statement_t* stmt;
  int lineno;
  char* filename;
  statement_t* nextstmt;
  expr_t* assignexpr;
  expr_t* rhs;
  expr_t* lhs;
  int numdims = 0;
  datatype_t* pdt;
  char tempvarname[64];
  symboltable_t* tempvar;
  expr_t* tempexpr;
  statement_t* assignstmt;
  int complete;
  expr_t* rhsreg;
  expr_t* tempreg=NULL;
  datatype_t* temparrdt;
  expr_t* dstreg;
  expr_t* srcreg;
  expr_t* dstmask;
  int dstmaskbit;
  expr_t* srcmask;
  int srcmaskbit;
  int staticreg;
  int try_using_dest_as_temp=1;
  int using_dest_as_temp=0;
  char* id;
  expr_t* tmp;
  statement_t* newstmt;
  statement_t* laststmt;
  int free=0;
 
  stmt = T_STMT(redexpr);
  lineno = T_LINENO(stmt);
  filename = T_FILENAME(stmt);
  nextstmt = T_NEXT(stmt);
  assignexpr = T_EXPR(stmt);
  if (!T_IS_ASSIGNOP(T_TYPE(assignexpr))) {
    return;
  }
  /* if the assignment is +=, etc. we can't use the dest as the temp storage */
  if (T_TYPE(assignexpr) != BIASSIGNMENT) {
    try_using_dest_as_temp=0;
  }
  lhs = T_OPLS(assignexpr);
  rhs = T_NEXT(lhs);
  if (rhs != redexpr) {
    INT_FATAL(stmt,"Unexpected placement of reduction within statement");
  }
  rhs = T_OPLS(rhs);
  rhsreg = T_TYPEINFO_REG(rhs);

  if (rhsreg == NULL) {
    /* if rhsreg is NULL, either the expression is a scalar (which is
       illegal and will be handled by typechecking), or it's an
       indexed array of arrays which has not yet had its indices
       inserted by a2nloops, and therefore evaluates as a scalar
       array.  In this latter case, we search deeply to try and find
       something that tells the rank. */
    numdims = expr_rank(rhs);
    if (numdims == 0) {
      free = expr_is_free(rhs);
      if (!free) {
	USR_FATAL_CONT(stmt,"Reduce of scalar expression");
	return; 
      }
    }
  } else {
    numdims = D_REG_NUM(T_TYPEINFO(rhsreg));
  }

  dstreg = RMSCurrentRegion();
  dstmask = RMSCurrentMask(&dstmaskbit);
  if (T_IDENT(dstreg) == pst_qreg[0]) { /* unresolved quote region */
    if (free) {
      T_IDENT(dstreg) = pst_free;
    } else {
      T_IDENT(dstreg) = pst_qreg[numdims];
    }
  }
  /* save destination region away for later use at codegen time since we
     will be playing games with scopes */
  T_REGMASK2(redexpr) = T_REGION(build_reg_mask_scope(dstreg,NULL,0,NULL,
						      lineno,filename));
  complete = (T_REGMASK(redexpr) == NULL);
  if (complete) {
    srcreg = dstreg;
    srcmask = dstmask;
    srcmaskbit = dstmaskbit;
    dstreg = NULL;
    tempreg = NULL;
  } else {
    RMSPushScope(T_REGMASK(redexpr));
    if (!RMSLegalReduce(stmt,numdims)) {
      RMSPopScope(T_REGMASK(redexpr));
      return;
    }

    srcreg = RMSCurrentRegion();
    srcmask = RMSCurrentMask(&srcmaskbit);
    if (T_IDENT(srcreg) == pst_qreg[0]) { /* unresolved quote region */
      T_IDENT(srcreg) = pst_qreg[numdims];
    }

    /* build temp region */
    tempreg = create_red_reg(numdims,dstreg,srcreg,lineno,filename,&staticreg);

    RMSPopScope(T_REGMASK(redexpr));
  }

  pdt = T_TYPEINFO(lhs);
  if (T_SUBTYPE(redexpr) != USER) {
    pdt = ensure_good_scanred_type(pdt);
  }

  if ((try_using_dest_as_temp) && (tempreg == dstreg)) {
    symboltable_t* lhsroot;
    datatype_t* lhsensdt;

    lhsroot = expr_find_root_pst(lhs);
    lhsensdt = datatype_find_ensemble(S_DTYPE(lhsroot));
    if ((tempreg == NULL && lhsensdt == NULL) ||
	(tempreg != NULL && lhsensdt != NULL && 
	 expr_equal(D_ENS_REG(lhsensdt), tempreg) &&  /* must be size of tempreg */
	 expr_find_ensemble_root(lhs) == lhs)) {  /* must be whole array access */
      using_dest_as_temp = 1;
      tempexpr = copy_expr(lhs);
    }
  }
  if (!using_dest_as_temp) {
    if (complete) {
      /* build scalar (grid) temp */
      sprintf(tempvarname,"_red_data%d",full_red_buff_num++);
      tempvar = create_named_local_var(pdt,T_PARFCN(stmt),tempvarname);
    } else {
      /* build temp array */
      sprintf(tempvarname, "_Red_data%d", part_red_buff_num++);
      temparrdt = build_ensemble_type(pdt,tempreg,0,1,lineno,filename);
      if (staticreg) {  /* make global if we can */
	tempvar = LU_INS(tempvarname);
	S_DTYPE(tempvar) = temparrdt;
      } else {  /* otherwise, make it local */
	tempvar = create_named_local_var(temparrdt,T_PARFCN(stmt),tempvarname);
	S_SETUP(tempvar) = 0;
      }
    }

    /* build expression for the reduction temp */
    tempexpr = build_typed_0ary_op(VARIABLE,tempvar);
  }

  /* switch reduction argument, tag reduction's rank */
  T_OPLS(redexpr) = tempexpr;
  T_RED_RANK(redexpr) = numdims;
  tempexpr = copy_expr(tempexpr);

  if (!using_dest_as_temp) {
    /* add on blank array references */
    while (D_CLASS(pdt) == DT_ARRAY) {
      tempexpr = build_typed_Nary_op(ARRAY_REF, tempexpr, NULL);
      T_TYPEINFO_REG(tempexpr) = build_0ary_op(CONSTANT, pstGRID_SCALAR[numdims]);
      pdt = D_ARR_TYPE(pdt);
    }
  }

  /* build assignment to the reduction temp */
  assignstmt = build_ident_expr(redexpr,pdt,tempexpr);

  /* wrap a scope around it for partial reduction */
  if (!complete) {
    if (using_dest_as_temp) {
      /* use dest region */
      assignstmt = build_reg_mask_scope(dstreg,NULL,MASK_NONE,assignstmt,
					lineno,filename);
      /*      assignstmt = build_mloop_statement(dstreg, assignstmt, 
					 D_REG_NUM(T_TYPEINFO(dstreg)),
					 NULL, MASK_NONE, lineno, filename);*/
    } else {
      /* use temporary region that we created to describe internal temp */
      genlist_t* newgls;
      /* open region scope */
      
      assignstmt = build_reg_mask_scope(tempreg,NULL,MASK_NONE,assignstmt,
					lineno,filename);
      /*      assignstmt = build_mloop_statement(tempreg, assignstmt, 
					 D_REG_NUM(T_TYPEINFO(tempreg)),
					 NULL, MASK_NONE, lineno, filename);*/
      if (!staticreg) {
	newgls = alloc_gen();
	G_IDENT(newgls) = tempvar;
	T_PRE(assignstmt) = newgls;
      }
    }
  }
  newstmt = assignstmt;

  /*
  dbg_gen_stmtls(stdout, newstmt);
  printf("-------\n");
  */

  
  /* setup temp reduction region */
  if (!complete && !staticreg && !using_dest_as_temp) {
    expr_t* fncall;
    expr_t* argexpr;
    expr_t* newarg;

    argexpr = copy_expr(srcreg);
    newarg = copy_expr(dstreg);
    T_NEXT(newarg) = argexpr;
    argexpr = newarg;
    newarg = copy_expr(tempreg);
    T_NEXT(newarg) = argexpr;
    argexpr = newarg;
    fncall = build_typed_Nary_op(FUNCTION,build_0ary_op(VARIABLE,
							pstCalcRedReg),argexpr);
    
    assignstmt = build_expr_statement(fncall,lineno,filename);
    insertafter_stmt(newstmt,assignstmt);
    newstmt = assignstmt;
    /*
    dbg_gen_stmtls(stdout, newstmt);
    printf("-------\n");
    */
  }

  /* build local part of reduction */
  tempexpr = copy_expr(tempexpr);
  {
    expr_t* tmp;
    tmp = tempexpr;
    while (tmp) {
      T_FREE(tmp) = TRUE;
      tmp = T_OPLS(tmp);
    }
  }
  rhs = copy_exprls(rhs, T_PARENT(rhs));
  if (T_SUBTYPE(redexpr) != USER) {
    assignexpr = build_typed_binary_op(BIOP_GETS, rhs, tempexpr);
    T_SUBTYPE(assignexpr) = T_SUBTYPE(redexpr);
  }
  else {
    symboltable_t* pst;
    tmp = rhs;
    while (T_NEXT(tmp) != NULL) {
      tmp = T_NEXT(tmp);
    }
    T_NEXT(tmp) = copy_expr(tempexpr);
    id = get_function_name(T_IDENT(redexpr), rhs, 1);
    if (id == NULL) {
      USR_FATALX(lineno, filename, "Missing local function for user defined reduction");
    }
    assignexpr = build_typed_Nary_op(FUNCTION, build_0ary_op(VARIABLE, check_var(id)), rhs);
    pst = lu(id);
    if (pst != NULL) {
      subclass sc;
      symboltable_t* decls;

      decls = T_DECL(S_FUN_BODY(pst));
      while (decls != NULL && S_CLASS(decls) != S_PARAMETER) {
	decls = S_SIBLING(decls);
      }
      while (decls != NULL) {
	sc = S_PAR_CLASS(decls);
	decls = S_SIBLING(decls);
	while (decls != NULL && S_CLASS(decls) != S_PARAMETER) {
	  decls = S_SIBLING(decls);
	}
      }
      if (sc == SC_IN || sc == SC_CONST) {
	assignexpr = build_typed_binary_op(BIASSIGNMENT, assignexpr, tempexpr);
	if (!(equiv_datatypes(S_FUN_TYPE(pst), T_TYPEINFO(tempexpr)))) {
	  USR_FATALX(lineno, filename, "local function for user defined reduction returns wrong type");
	}
      }
      else {
	if (S_CLASS(S_FUN_TYPE(pst)) != DT_VOID) {
	  USR_FATALX(lineno, filename, "by reference local function for user defined reduction must not return a value");
	}
      }
    }
  }
  assignstmt = build_expr_statement(assignexpr,lineno,filename);
  if (numdims != 0) { /* don't put mloop around free reduce */
    /*    assignstmt = build_mloop_statement(srcreg, assignstmt,
				       D_REG_NUM(T_TYPEINFO(srcreg)),
				       srcmask, srcmaskbit, lineno, filename);*/
  }

  insertbefore_stmt(newstmt, assignstmt);
  /*
  dbg_gen_stmtls(stdout, newstmt);
  printf("-------\n");
  */

  /* create actual reduction statement */
  assignstmt = build_expr_statement(redexpr,lineno,filename);
  insertbefore_stmt(newstmt, assignstmt);
  /*
  dbg_gen_stmtls(stdout, newstmt);
  printf("-------\n");
  */

  /* wrap a scope around it for partial reduction */
  if (!complete) {
    newstmt = build_reg_mask_scope(srcreg,srcmask,srcmaskbit,newstmt,
				   lineno,filename);
  }
  insertbefore_stmt(newstmt,stmt);
  laststmt = stmt;

  /*
  dbg_gen_stmtls(stdout, newstmt);
  printf("-------\n");
  */


  if (!using_dest_as_temp) {
    /* build assignment of reduction result */
    tempexpr = copy_expr(tempexpr);
    lhs = copy_expr(lhs);
    assignexpr = build_typed_binary_op(BIASSIGNMENT, tempexpr, lhs);
    assignstmt = build_expr_statement(assignexpr,lineno,filename);
    if (!complete) {
      /*      assignstmt = build_mloop_statement(dstreg, assignstmt, 
					 D_REG_NUM(T_TYPEINFO(dstreg)),
					 dstmask, dstmaskbit, lineno, filename);*/
    }
    if (nextstmt == NULL) {
      T_NEXT(stmt) = assignstmt;
      T_PREV(assignstmt) = stmt;
      T_PARFCN(assignstmt) = T_PARFCN(stmt);
    } else {
      insertbefore_stmt(assignstmt,nextstmt);
      laststmt = nextstmt;
    }
  }

  /* deallocate local array if there was one */
  if (!complete && !staticreg && !using_dest_as_temp) {
    genlist_t* newgls;

    newgls = alloc_gen();
    G_IDENT(newgls) = tempvar;
    T_POST(assignstmt) = newgls;
  }

  /* convert original statement into a simple reduction expression */
  /* T_EXPR(stmt) = redexpr;*/
  T_PRE(newstmt) = cat_genlist_ls(T_PRE(newstmt), T_PRE(stmt));
  T_POST(laststmt) = cat_genlist_ls(T_POST(laststmt), T_POST(stmt));
  remove_stmt(stmt);
  /* for user defined reductions, find or create a global reduction function */
  if (T_SUBTYPE(redexpr) == USER) {
    expr_t* copyexpr;
    char gfn[256];
    statement_t* body;
    statement_t* comp_stmt;
    symboltable_t* pst;
    expr_t* oldarg1;
    expr_t* newarg1;
    expr_t* oldarg2;
    expr_t* newarg2;

    sprintf(gfn, "_ZPLGLOBALREDUCE_%s", S_IDENT(T_IDENT(redexpr)));
    pst = lu(gfn);
    if (pst != NULL) {
      T_IDENT(redexpr) = pst;
    }
    else {
      symboltable_t* locals;

      tempexpr = copy_expr(tempexpr);
      copyexpr = copy_expr(tempexpr);
      T_NEXT(tempexpr) = copyexpr;
      id = get_function_name(T_IDENT(redexpr), tempexpr, 1);
      if (id == NULL) {
	USR_FATALX(lineno, filename, "Missing global function for user defined reduction");
      }
      pst = lu(id);
      if (pst == NULL) {
	USR_FATALX(lineno, filename, "Missing global function for user defined reduction");
      }
      locals = T_DECL(S_FUN_BODY(pst));
      oldarg1 = build_typed_0ary_op(VARIABLE, T_DECL(S_FUN_BODY(pst)));
      oldarg2 = build_typed_0ary_op(VARIABLE, S_SIBLING(T_DECL(S_FUN_BODY(pst))));
      body = copy_stmtls(T_STLS(S_FUN_BODY(pst)));
      pst = insert_function(gfn,pdtVOID,2,T_TYPEINFO(tempexpr),SC_INOUT,
			    T_TYPEINFO(tempexpr),SC_INOUT);
      S_STD_CONTEXT(pst) = FALSE;
      if (T_TYPE(body) != S_COMPOUND) {
	comp_stmt = build_compound_statement( NULL, body, lineno, filename);
      } else {
	comp_stmt = body;
      }
      S_SIBLING(S_SIBLING(T_DECL(S_FUN_BODY(pst)))) = locals;
      T_DECL(T_CMPD(comp_stmt)) = T_DECL(S_FUN_BODY(pst));
      T_STLS(S_FUN_BODY(pst)) = comp_stmt;
      fix_stmtls(comp_stmt, NULL, S_FUN_BODY(pst));
      newarg1 = build_typed_0ary_op(VARIABLE, T_DECL(S_FUN_BODY(pst)));
      newarg2 = build_typed_0ary_op(VARIABLE, S_SIBLING(T_DECL(S_FUN_BODY(pst))));
      replaceall_stmtls(body, oldarg1, newarg1);
      replaceall_stmtls(body, oldarg2, newarg2);

      /* we can do this because we know it is not recursive! */
      body = returns2assigns(body, newarg2);






      T_IDENT(redexpr) = pst;
    }
  }
}
Exemple #11
0
void sccwriter::write_file()
{
  static std::string filename( "scccode" );

  //writer the h file
  std::fstream fsh;
  std::string fnameh( filename );
  fnameh.append(".h");
  fsh.open( fnameh.c_str(), std::ios::out );
  //write the header in h
  fsh << "#ifndef SCC_CODE_H" << std::endl;
  fsh << "#define SCC_CODE_H" << std::endl << std::endl;
  //include necessary files in h file
  fsh << "#include \"check.h\"" << std::endl << std::endl;
  //write the init function
  fsh << "void init_compiled_scc();" << std::endl << std::endl;
  //write the entry function
  fsh << "Expr* run_compiled_scc( Expr* p, std::vector< Expr* >& args );" << std::endl << std::endl;
  //write the side condition code functions
  for( int n=0; n<(int)progs.size(); n++ )
  {
    //write the header in the h file
    fsh << "inline ";
    write_function_header( fsh, n );
    fsh << ";" << std::endl << std::endl;
  }
  fsh << "#endif" << std::endl << std::endl;
  fsh.close();


  //writer the cpp code
  std::fstream fsc;
  std::string fnamec( filename );
  fnamec.append(".cpp");
  fsc.open( fnamec.c_str(), std::ios::out );
  //include the h file in the cpp
  fsc << "#include \"scccode.h\"" << std::endl << std::endl;
  std::ostringstream fsc_funcs;
  //write the side condition code functions
  for( currProgram=0; currProgram<(int)progs.size(); currProgram++ )
  {
    //reset naming counters
    vars.clear();
    exprCount = 0;
    strCount = 0;
    argsCount = 0;
    rnumCount = 0;

    //for debugging
    std::cout << "program #" << currProgram << " " << progNames[currProgram].c_str() << std::endl;

    //write the function header
    write_function_header( fsc_funcs, currProgram, opt_write_add_args|options );
    if( (options&opt_write_call_debug)==0 )
    {
       fsc_funcs << "{" << std::endl;
    }
    //write the code
    //std::vector< std::string > cleanVec;
    //write_code( get_prog( n )->kids[2], fsc, 1, "return ", cleanVec );
    //debug_write_code( progs[n].second->kids[2], fsc, 1 );
    std::string expr;
    write_expr( get_prog( currProgram )->kids[2], fsc_funcs, 1, expr );
    indent( fsc_funcs, 1 );
    fsc_funcs << "return " << expr.c_str() << ";" << std::endl;
    fsc_funcs << "}" << std::endl << std::endl;
  }
  //write the predefined symbols necessary - symbols and progs
  for( int a=0; a<(int)globalSyms.size(); a++ )
  {
    fsc << "Expr* e_" << globalSyms[a].c_str() << ";" << std::endl;
  }
  for( int a=0; a<(int)progs.size(); a++ )
  {
    fsc << "Expr* e_" << progNames[a].c_str() << ";" << std::endl;
  }
  fsc << std::endl;
  //write the init function - initialize symbols and progs
  fsc << "void init_compiled_scc(){" << std::endl;
  for( int a=0; a<(int)globalSyms.size(); a++ )
  {
    indent( fsc, 1 );
    fsc << "e_" << globalSyms[a].c_str() << " = symbols->get(\"" << globalSyms[a].c_str() << "\").first;" << std::endl;
  }
  for( int a=0; a<(int)progs.size(); a++ )
  {
    indent( fsc, 1 );
    fsc << "e_" << progNames[a].c_str() << " = progs[\"" << progNames[a].c_str() << "\"];" << std::endl;
  }
  fsc << "}" << std::endl << std::endl;
  fsc << "Expr* run_compiled_scc( Expr* p, std::vector< Expr* >& args ){" << std::endl;
  //for( int n=0; n<(int)progs.size(); n++ ){
  //  indent( fsc, 1 );
  //  fsc << "static std::string s_" << progNames[n].c_str() << " = std::string( \"" << progNames[n].c_str() << "\" );" << std::endl;
  //}
  for( int n=0; n<(int)progs.size(); n++ ){
    indent( fsc, 1 );
    if( n!=0 ){
      fsc << "}else ";
    }
    //for each function, test to see if the string matches the name of the function
    fsc << "if( p==e_" << progNames[n].c_str() << " ){" << std::endl;
    indent( fsc, 2 );
    std::string fname;
    get_function_name( progNames[n], fname );
    //map the function to the proper function
    fsc << "return " << fname.c_str() << "( ";
    //write the arguments to the function from args
    CExpr* progvars = (CExpr*)get_prog( n )->kids[1];
    int counter = 0;
    bool firstTime = true;
    while( progvars->kids[counter] )
    {
      if( !firstTime )
      {
        fsc << ", ";
      }
      fsc << "args[" << counter << "]";
      firstTime = false;
      counter++;
    }
    fsc << " );" << std::endl;
  }
  indent( fsc, 1 );
  fsc << "}else{" << std::endl;
  indent( fsc, 2 );
  //return null in the case the function could not be found
  fsc << "return NULL;" << std::endl;
  indent( fsc, 1 );
  fsc << "}" << std::endl;
  fsc << "}" << std::endl << std::endl;
  fsc << fsc_funcs.str().c_str();

  fsc.close();
}
Exemple #12
0
 std::string get_function_name(hpx::exception const& e)
 {
     return get_function_name(dynamic_cast<boost::exception const&>(e));
 }