Esempio n. 1
0
void updateFlowGraph(t_cflow_Graph *graph)
{
   t_list *current_element;
   t_basic_block *current_block;
   t_basic_block *successor;
   
   /* preconditions: graph should not be a NULL pointer */
   if (graph == NULL){
      cflow_errorcode = CFLOW_GRAPH_UNDEFINED;
      return;
   }

   successor = NULL;
   current_element = graph->blocks;
   while(current_element != NULL)
   {
      t_list *last_element;
      t_cflow_Node *last_node;
      t_axe_instruction *last_instruction;
      t_basic_block *jumpBlock;
      
      /* retrieve the current block */
      current_block = (t_basic_block *) LDATA(current_element);
      assert(current_block != NULL);
      assert(current_block->nodes != NULL);

      /* get the last node of the basic block */
      last_element = getLastElement(current_block->nodes);
      assert(last_element != NULL);
      
      last_node = (t_cflow_Node *) LDATA(last_element);
      assert(last_node != NULL);

      last_instruction = last_node->instr;
      assert(last_instruction != NULL);

      if (isHaltInstruction(last_instruction))
      {
         setSucc(current_block, graph->endingBlock);
         setPred(graph->endingBlock, current_block);
      }
      else
      {
         if (isJumpInstruction(last_instruction))
         {
            if (  (last_instruction->address == NULL)
                  || ((last_instruction->address)->labelID == NULL) )
            {
               cflow_errorcode = CFLOW_INVALID_LABEL_FOUND;
               return;
            }
         
            jumpBlock = searchLabel(graph
                  , (last_instruction->address)->labelID);
            if (jumpBlock == NULL) {
               cflow_errorcode = CFLOW_INVALID_LABEL_FOUND;
               return;
            }

            /* add the jumpBlock to the list of successors of current_block */
            /* add also current_block to the list of predecessors of jumpBlock */
            setPred(jumpBlock, current_block);
            if (cflow_errorcode != CFLOW_OK)
               return;
            setSucc(current_block, jumpBlock);
            if (cflow_errorcode != CFLOW_OK)
               return;
         }

         if (!isUnconditionalJump(last_instruction))
         {
            t_basic_block *nextBlock;
            t_list *next_element;
            
            next_element = LNEXT(current_element);
            if (next_element != NULL)
            {
               nextBlock = LDATA(next_element);
               assert(nextBlock != NULL);
               
               setSucc(current_block, nextBlock);
               setPred(nextBlock, current_block);
            }
            else
            {
               setSucc(current_block, graph->endingBlock);
               setPred(graph->endingBlock, current_block);
            }
         
            if (cflow_errorcode != CFLOW_OK)
               return;
         }
      }

      /* update the value of `current_element' */
      current_element = LNEXT(current_element);
   }
}
Esempio n. 2
0
int runProgram ( interpreteer * toRun )
{
	instruction * buffer = toRun->instrList;
	while ( 1 )
	{
		int a, b;
        switch ( toRun->instrList->instrType )
		{
			//	add, br, cmp, divd, hlt, jmp, labe, ldc, ld, mul, mod, sub, st
		case ADD:
			a = pop ( toRun->spine );
			b = pop ( toRun->spine );
			push ( toRun->spine, a + b );
			toRun->instrList = toRun->instrList->next;
			break;
		case BR:
			if ( !(pop ( toRun->spine ) == 0) )
				toRun->instrList = searchLabel ( toRun->instrList->stringValue,  buffer );
			else toRun->instrList = toRun->instrList->next;
			break;
		case CMP:
			a = pop ( toRun->spine );
			b = pop ( toRun->spine );
			if ( b < a )
				push ( toRun->spine, -1 );
			else if ( b == a )
				push ( toRun->spine, 0 );
			else push ( toRun->spine, 1 );
			toRun->instrList = toRun->instrList->next;
			break;
		case DIVD:
			a = pop ( toRun->spine );
			b = pop ( toRun->spine );
			push ( toRun->spine, b / a );
			toRun->instrList = toRun->instrList->next;
			break;
		case HLT:
			return pop ( toRun->spine );
			break;
		case JMP:
			toRun->instrList = searchLabel ( toRun->instrList->stringValue, buffer );
			break;
		case LABEL:
			toRun->instrList = toRun->instrList->next;
			break;
		case LDC:
			push ( toRun->spine, toRun->instrList->value );
			toRun->instrList = toRun->instrList->next;
			break;
		case LD:
			push ( toRun->spine , *at ( toRun->virtualMem, toRun->instrList->value ) );
			toRun->instrList = toRun->instrList->next;
			break;
		case MUL:
			a = pop ( toRun->spine );
			b = pop ( toRun->spine );
			push ( toRun->spine, b * a );
			toRun->instrList = toRun->instrList->next;
			break;
		case MOD:
			a = pop ( toRun->spine );
			b = pop ( toRun->spine );
			push ( toRun->spine, b % a );
			toRun->instrList = toRun->instrList->next;
			break;
		case SUB:
			a = pop ( toRun->spine );
			b = pop ( toRun->spine );
			push ( toRun->spine, b - a );
			toRun->instrList = toRun->instrList->next;
			break;
		case ST:
			*at ( toRun->virtualMem, toRun->instrList->value ) = pop ( toRun->spine );
			toRun->instrList = toRun->instrList->next;
			break;
		case STI:
			a = pop ( toRun->spine );
			b = pop ( toRun->spine );
			*at ( toRun->virtualMem, a ) = b;
			toRun->instrList = toRun->instrList->next;
			break;
		case LDI:
			a = pop ( toRun->spine );
			push ( toRun->spine, *at ( toRun->virtualMem, a ) );
			toRun->instrList = toRun->instrList->next;
			break;
		case NA:
			exit ( pop ( toRun->spine ) );
			break;
		default:
			exit ( pop ( toRun->spine ) );
			break;
		}
	}
}