Example #1
0
int sperateCMD(char* in){
	int i=0;
	int len = strlen(in);
	/* flag to determine if we should skip the space in 
	   a string */
	int findQuote = 0;
	/* Stack to valid the quotes pair */
	Stack	stk;
	Stack_Init(&stk);

	for(; i<len; i++){
		if(in[i] == ' ' && !findQuote)
			in[i]='\0';
		if( in[i] == '\'' || in[i] == '\"' ){

			if( stk.size > 0 && Stack_Top(&stk) == in[i])
				Stack_Pop(&stk);
			else
				Stack_Push(&stk,in[i]);
			findQuote++;
			if(stk.size == 0)
				findQuote = 0;
			in[i] = '\0';
		}
	}
	return len;
}
Example #2
0
/// <summary>
/// Evaluate_next_operator for the next operator.
/// </summary>
/// <param name="operands">The operands.</param>
/// <param name="operators">The operators.</param>
bool evaluate_next_operator(struct Stack* operands, struct Stack* operators) {
    char op = Stack_Top(operators);
    Stack_Pop(operators);

    if (op == '_') { // deal with unary '-' differently
        int val = -Stack_Top(operands);
        Stack_Pop(operands);
        Stack_Push(operands, val);
        return;
    }

    int opr2 = Stack_Top(operands);
    Stack_Pop(operands);
    int opr1 = Stack_Top(operands);
    Stack_Pop(operands);

    switch (op) {
        case '*':
            Stack_Push(operands, opr1 * opr2);
            break;
        case '/':
            if (opr2 == 0) {
                syslog(LOG_ERR, "Cannot divide by 0");
                return DIVIDED_BY_0;
            }
            Stack_Push(operands, opr1 / opr2);
            break;
        case '+':
            Stack_Push(operands, opr1 + opr2);
            break;
        case '-':
            Stack_Push(operands, opr1 - opr2);
            break;
    }
    return false;
}
Example #3
0
/*
 * see "Beyond Induction Variables" paper for details on this
 * implementation of tarjan's algo
 */
static void
Find_Components(PSS_TarLoop tloop)
{
    PSS_TarNode tnode;

    tloop->number = 0;

#if 0 /* this does not work cause stacks are lame */
    /* sanity check */
    if (Stack_Top(tloop->node_stack) != NULL)
        P_punt("Find_Components: stack not empty before tarjan calculations");
#endif

    /* all the ops are allocated to NOTYET to start */

    /* process all the ops */
    for (tnode = tloop->first; tnode; tnode = tnode->next)
    {
        if (tnode->status == NOTYET)
        {
            Visit_Node(tloop, tnode);
        } /* only process if not yet processed */
    } /* for all the ops in the loop */
}
Example #4
0
static void decode_location(Dwarf_Locdesc *locationList, Dwarf_Signed listLength, long *offset, long *init_val, int *frameRel){

  /*Location Decoding Code from http://ns.dyninst.org/coverage/dyninstAPI/src/parseDwarf.C.gcov.html*/
  Stack *opStack = (Stack*)malloc(sizeof(Stack));
  Stack_Init(opStack);
  assert( listLength > 0 );
 
  if( listLength > 1 ) { fprintf( stderr, "Warning: more than one location, ignoring all but first.\n" ); }
 
  /* Initialize the stack. */
  if( init_val != NULL ) { Stack_Push(opStack, * init_val); }
 
  /* Variable can only become frame-relative by using the frame pointer operand. */
  if( frameRel != NULL ) { * frameRel = false; }
 
  /* Ignore all but the first location, for now. */
  Dwarf_Locdesc location = locationList[0];
  Dwarf_Loc * locations = location.ld_s;
  unsigned int i;
  for( i = 0; i < location.ld_cents; i++ ) {
    /* Handle the literals w/o 32 case statements. */
    if( DW_OP_lit0 <= locations[i].lr_atom && locations[i].lr_atom <= DW_OP_lit31 ) {
      //fprintf( stderr, "Pushing named constant: %d\n", locations[i].lr_atom - DW_OP_lit0 );
      Stack_Push(opStack, locations[i].lr_atom - DW_OP_lit0 );
      continue;
    }
 
    if( (DW_OP_breg0 <= locations[i].lr_atom && locations[i].lr_atom <= DW_OP_breg31) || locations[i].lr_atom == DW_OP_bregx ) {
      /* Offsets from the specified register.  Since we're not
         doing this at runtime, we can't do anything useful here. */
      fprintf( stderr, "Warning: location decode requires run-time information, giving up.\n" );
      *offset = -1;
      return;
    }
 
    if( (DW_OP_reg0 <= locations[i].lr_atom && locations[i].lr_atom <= DW_OP_reg31) || locations[i].lr_atom == DW_OP_regx ) {
      /* The variable resides in the particular register.  We can't do anything
         useful with this information yet. */
      fprintf( stderr, "Warning: location decode indicates register, giving up.\n" );
      *offset = -1;
      return;
    }
 
    switch( locations[i].lr_atom ) {
      case DW_OP_addr:
      case DW_OP_const1u:
      case DW_OP_const2u:
      case DW_OP_const4u:
      case DW_OP_const8u:
      case DW_OP_constu:
        // fprintf( stderr, "Pushing constant %lu\n", (unsigned long)locations[i].lr_number );
        Stack_Push(opStack, (Dwarf_Unsigned)locations[i].lr_number );
        break;
 
      case DW_OP_const1s:
      case DW_OP_const2s:
      case DW_OP_const4s:
      case DW_OP_const8s:
      case DW_OP_consts:
        // fprintf( stderr, "Pushing constant %ld\n", (signed long)(locations[i].lr_number) );
        Stack_Push(opStack, (Dwarf_Signed)(locations[i].lr_number) );
        break;
 
      case DW_OP_fbreg:
        /* We're only interested in offsets, so don't add the FP. */
        // fprintf( stderr, "Pushing FP offset %ld\n", (signed long)(locations[i].lr_number) );
        Stack_Push(opStack, (Dwarf_Signed)(locations[i].lr_number) );
        if( frameRel != NULL ) { * frameRel = true; }
        break;
 
      case DW_OP_dup:
        Stack_Push(opStack, Stack_Top(opStack) );
        break;
 
      case DW_OP_drop:
        Stack_Pop(opStack);
        break;
 
      case DW_OP_over: {
          long int first = Stack_Top(opStack); Stack_Pop(opStack);
          long int second = Stack_Top(opStack); Stack_Pop(opStack);
          Stack_Push(opStack, second ); Stack_Push(opStack, first ); Stack_Push(opStack, second );
        } break;
 
      case DW_OP_pick: {
        /* Duplicate the entry at index locations[i].lr_number. */
        Stack temp;
        Stack_Init(&temp);
        unsigned int i;
        for( i = 0; i < locations[i].lr_number; i++ ) {
          Stack_Push(&temp, Stack_Top(opStack) ); Stack_Pop(opStack);
        }
        long int dup = Stack_Top(opStack);
        for( i = 0; i < locations[i].lr_number; i++ ) {
          Stack_Push(opStack, Stack_Top(&temp) ); Stack_Pop(&temp);
        }
        Stack_Push(opStack, dup );
        } break;
 
      case DW_OP_swap: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first ); Stack_Push(opStack, second );
      } break;
 
      case DW_OP_rot: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        long int third = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first ); Stack_Push(opStack, third ); Stack_Push(opStack, second );
      } break;
 
      case DW_OP_deref:
      case DW_OP_deref_size:
      case DW_OP_xderef:
      case DW_OP_xderef_size:
        // fprintf( stderr, "Warning: location decode requires run-time information, giving up.\n" );
        * offset = -1;
        return;
 
      case DW_OP_abs: {
        long int top = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, abs( top ) );
        } break;
 
      case DW_OP_and: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second & first );
      } break;
                  
      case DW_OP_div: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second / first );
      } break;
 
      case DW_OP_minus: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second - first );
      } break;
 
      case DW_OP_mod: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second % first );
      } break;
 
      case DW_OP_mul: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second * first );
      } break;
 
      case DW_OP_neg: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first * (-1) );
      } break;
 
      case DW_OP_not: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, ~ first );
      } break;
 
      case DW_OP_or: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second | first );
      } break;
 
      case DW_OP_plus: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second + first );
      } break;
 
      case DW_OP_plus_uconst: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first + locations[i].lr_number );
      } break;
 
      case DW_OP_shl: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second << first );
      } break;
 
      case DW_OP_shr: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, (long int)((unsigned long)second >> (unsigned long)first) );
      } break;
 
      case DW_OP_shra: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second >> first );
      } break;
 
      case DW_OP_xor: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second ^ first );
      } break;
 
      case DW_OP_le: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first <= second ? 1 : 0 );
      } break;
 
      case DW_OP_ge: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first >= second ? 1 : 0 );
      } break;
 
      case DW_OP_eq: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first == second ? 1 : 0 );
      } break;
 
      case DW_OP_lt: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first < second ? 1 : 0 );
      } break;
 
      case DW_OP_gt: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first > second ? 1 : 0 );
      } break;
 
      case DW_OP_ne: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first != second ? 1 : 0 );
      } break;
 
      case DW_OP_bra:
        if( Stack_Top(opStack) == 0 ) { break; }
        Stack_Pop(opStack);
        break;/*Right?*/
      case DW_OP_skip: {
        int bytes = (int)(Dwarf_Signed)locations[i].lr_number;
        unsigned int target = locations[i].lr_offset + bytes;
 
        int j = i;
        if( bytes < 0 ) {
          for( j = i - 1; j >= 0; j-- ) {
            if( locations[j].lr_offset == target ) { break; }
          } /* end search backward */
        } else {
          for( j = i + 1; j < location.ld_cents; j ++ ) {
            if( locations[j].lr_offset == target ) { break; }
          } /* end search forward */
        } /* end if positive offset */
 
        /* Because i will be incremented the next time around the loop. */
        i = j - 1;
      } break;
 
      case DW_OP_piece:
        /* For multi-part variables, which we don't handle. */
        break;
 
      case DW_OP_nop:
        break;
 
      default:
        fprintf( stderr, "Unrecognized location opcode 0x%x, returning offset -1.\n", locations[i].lr_atom );
        *offset = -1;
        return;
    } /* end operand switch */
  } /* end iteration over Dwarf_Loc entries. */
 
  /* The top of the stack is the computed location. */
  //fprintf( stderr, "Location decoded: %ld\n", Stack_Top(opStack) );
  *offset = Stack_Top(opStack);
}  
Example #5
0
/// <summary>
/// Evaluate_exps evaluates the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The evaluated result</returns>
struct Result evaluate_expr(char* exp) {
    static const char* ods = "0123456789(";
    static const char* ops = "+-*/_"; // '_' is unary '-'
    struct Stack operands;
    struct Stack operators;
    Stack_Init(&operators);
    Stack_Init(&operands);

    removeSpaces(exp);
    findUnaryOperators(exp);

    //syslog(LOG_INFO, "parsing: %s", exp);

    int i;
    i = 0;
    while (exp[i] != '\0') {
        char c = exp[i++];
        if (strchr(ods, c) != NULL) { // if char == operand
            if (c == '(') {
                Stack_Push(&operators, c);
            } else {
                int iStart = i - 1;
                int iEnd = iStart;
                do {
                    c = exp[i++];
                } while (c != '\n' && isdigit(c));
                iEnd = --i;
                char num[MAX_DIGITS] = "";
                strncat(num, exp + iStart, iEnd - iStart);
                Stack_Push(&operands, atoi(num));
            }
        } else if (strchr(ops, c) != NULL) { // if char == operator
            int currentPres = getPrecendence(c);
            while (Stack_Size(&operators) > 0 &&
                   Stack_Size(&operands)  > 0 &&
                   currentPres < getPrecendence((char)Stack_Top(&operators))) {
                if (evaluate_next_operator(&operands, &operators) == DIVIDED_BY_0) {
                    return inf;
                }
            }
            Stack_Push(&operators, c);
        } else if (c == ')') {
            while (Stack_Top(&operators) != '(') {
                if (evaluate_next_operator(&operands, &operators) == DIVIDED_BY_0) {
                    return inf;
                }
            }
            Stack_Pop(&operators);
        }
    }

    while (Stack_Size(&operators) > 0) {
        if (evaluate_next_operator(&operands, &operators) == DIVIDED_BY_0) {
            return inf;
        }
    }

    int resultInt = Stack_Top(&operands);
    syslog(LOG_INFO, "Result: %d", resultInt);
    struct Result result = {resultInt, false};
    return result;
}
Example #6
0
/*
 * see "Beyond Induction Variables" paper for details on this
 * implementation of tarjan's algo
 */
static void
Visit_Node (PSS_TarLoop tloop, PSS_TarNode tnode)
{
    int low, this;
    Expr def_var_expr, operand_expr;
    List operand_list;

    tnode->status = ONSTACK;
    tnode->lowlink = low = this = tloop->number++;

    Push_Top(tloop->node_stack, tnode);

    /* o visit all operand descendents
     * o visit all ssa flows
     *   - in our implementation, these all happen together
     *     because the ssa links off of phi functions are just
     *     treated like operands to a phi function */
    operand_list = PSS_GetSubExprByOpcode_List (tnode->expr->operands, OP_var);
    List_start(operand_list);
    while ((operand_expr = List_next(operand_list)))
    {
        Expr op_def_expr;
        PSS_TarNode op_def_node;

        /* we can't have parameters or undef's on the SCC */
        if (operand_expr->value.var.ssa &&
                !UNINITIALIZED_TYPE (operand_expr->value.var.ssa->type))
        {
            op_def_expr = operand_expr->value.var.ssa->var->parentexpr;

            if ((op_def_node = Find_TarNode_Expr(tloop, op_def_expr)))
            {
                low = MIN(low, Visit_Descendent(tloop, op_def_node));
            }
        }
    }

    tnode->lowlink = low;

    /* check if classification not possible at this time */
    if (this != low) return;

    def_var_expr = tnode->expr->operands;

    /* sanity check */
    if (tnode->expr->opcode != OP_assign || def_var_expr->opcode != OP_var)
        P_punt("Visit_Node: Illegal TarNode Expression");

    /* SCC's always finish off with the same element
     * on the top of the stack, unless the node we are
     * taking a look at is a mu node */
    if (Stack_Top(tloop->node_stack) == tnode &&
            !MU_TYPE (def_var_expr->value.var.ssa->type))
    {
        Classify_Trivial(tloop, tnode);
        Pop(tloop->node_stack);
        tnode->status = DONE;
    } /* trivial */
    else /* SCC */
    {
        PSS_TarNode stacktop;
        PSS_TarSCC scc;

        scc = New_TarSCC(tloop);
        do
        {
            stacktop = Pop(tloop->node_stack);
            stacktop->status = DONE;
            Add_Node_To_Scc(stacktop, scc);
            /* === ADD STACKTOP TO COMPONENT === */
        } while(stacktop != tnode);
        /* in the algo presented in the paper, one would classify
         * the sequences here.  however, because different types
         * of induction variables depend on knowledge abou previous
         * types (for example, a polynomial induction var is made
         * up of linear induction vars), it is necessary to identify
         * all the SCC's (and vars) that are of certain types before
         * identifying other types.  as such, we will classify all
         * the induction vars at one time at the end.
         */
#if 0
        Classify_Sequence(scc);
#endif
    } /* SCC */
}