/*****************************************************
*				Print Statement Block				 *
*****************************************************/
void CompField::Printblocks(StmtBlock* stmt, string fname)
{
	nbASSERT(stmt!=NULL, "This pointer can't be null! (statement) - (printBlock)");	

	ofstream m_StreamData(fname.c_str(), ios::app);				


	StmtBase *Head=stmt->Code->Front();
	//StmtBase *Tail=stmt->Code->Back();

	m_StreamData<<"*x*x*x*x*  PRINT-BLOCK(START): "<<endl;


	m_StreamData<<"	PRINT-BLOCK(stmt-BASE): "<<endl;
	PrintStmt(stmt,fname);
	m_StreamData<<"	PRINT-BLOCK(stmt-BASE) END"<<endl;

	m_StreamData<<"*Starting NODES inside Block:"<<endl;
	for(StmtBase *it=Head; it!=NULL; it=it->Next)
		PrintStmt(it,fname);		

	m_StreamData<<"*Ended    NODES inside Block"<<endl;
	m_StreamData<<"*x*x*x*x*  PRINT-BLOCK(END) "<<endl;
	m_StreamData.close();
}
Example #2
0
//---------------------------------------------------------
void DocumentXML::writeDeclToXML(Decl *D) {
  DeclPrinter(*this).Visit(D);
  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
    if (Stmt *Body = FD->getBody()) {
      addSubNode("Body");
      PrintStmt(Body);
      toParent();
    }
  }
  toParent();
}
/*****************************************************
*				Print Statement While				 *
*****************************************************/
void CompField::PrintWhile(StmtWhile* stmt, string fname){
	nbASSERT(stmt!=NULL, "This pointer can't be null! (statement) - (StmtWhile)");	

	ofstream m_StreamData(fname.c_str(), ios::app);				
	m_StreamData<<"	PRINT-WHILE(START): "<<endl;

	PrintStmt(stmt,fname);			//print base statement

	Printblocks(stmt->Code,fname);	//print specific-block statement

	m_StreamData<<"	PRINT-WHILE(END) "<<endl;
	m_StreamData<<"*******************************************************************************"<<endl;
	m_StreamData.close();
}
Example #4
0
Node* Parser::Statement() {
	Node *link = NULL;
	
	if(simple)
		link = FunctionDefinitionStmt();

	if(link == NULL) link = CompoundStmt();
	if(link == NULL) link = ExpressionStmt();
	if(link == NULL) link = DeclarationList();
	if(link == NULL) link = SelectionStmt();
	if(link == NULL) link = IterationStmt();
	if(link == NULL) link = JumpStmt();
	if(link == NULL) link = PrintStmt();
	if(link == NULL) link = PauseStmt();
	if(link == NULL) link = LabelStmt();

	return link;
}
/*****************************************************
*				Compact fields (from start to stop)  *
*****************************************************/
void CompField::CompactFields(){
	SymbolFieldFixed *newSymb;   //new symbolfield

	HIRNode *newNodeExt;	//new node
	HIRNode *newNodeInt;	//new node
	StmtBase *newStmt; 	//new statement

	StmtBase *before; 	
	StmtBase *after; 	
	bool deletelonelybits=false;
	nbASSERT(m_actualstmt!=NULL, "This pointer can't be null! (actual_statement)");	
	nbASSERT(m_startstmt!=NULL, "This pointer can't be null! (start_statement)");
	nbASSERT(m_stopstmt!=NULL, "This pointer can't be null! (stop_statement)");		

	/********************************************/
	//	BUILD & LINK NEW SYMBOLFIELD	    //
	/********************************************/
	nbASSERT(m_longsize, "Size of the new Field Error!!");	


	if(m_startstmt==m_stopstmt)		//List of COMPATTABLE fields contains only one field!! (LONELY-COMPATTABLE-FIELD)
	{
		nbASSERT(m_startstmt->Forest->Kids[0]!=NULL,"THIS Node CAN'T BE NULL! ");
		SymbolField *symb=((SymbolField *)m_startstmt->Forest->Kids[0]->Sym);
		nbASSERT(symb!=NULL,"THIS Symbol CAN'T BE NULL! ");

		if(symb->Used==true || symb->Compattable==false)
		{
		   //this can't be! Who Called CompactFields??!!
		  return;
		}

		FieldsList_t container=((SymbolFieldContainer *)symb)->InnerFields;
		if(container.begin()!=container.end())
		{
			deletelonelybits = true;	//This fixed is a container of fields (e.g. bit_union)
		}
		else
		{
#ifdef DEBUG_COMPACT_FIELDS
			ofstream my_ir("Debug_Lonely_fields.txt",ios::app);
			my_ir <<m_longname<<": "<<m_longsize<<" Don't need to be compatted! (LONELY FIELD) "<<endl;
#endif
			return;				//this is a lonely fixed-field
		}
	}

	m_CurrProtoSym = GetCurrentProto();			//Get Current Protocol by checkin field->Protocol->Name && Check if startfield->proto != stopfield->proto (CAN'T BE!)

	newSymb = new SymbolFieldFixed(m_longname,m_longsize);
	nbASSERT(newSymb!=NULL, "Problems on Allocation of the new SymbolField");	

	newSymb->Used=false;			//GENERATED COMPATTED-FIELD CAN'T BE USED ANYWHERE!
	newSymb->Compattable=true;		//ONCOMINGS TransformLoop() can look this attribute


	// Add the protocol reference in the field Symbol
	newSymb->Protocol=m_CurrProtoSym;
	//Store the field symbol into the field symbol tables                      
	newSymb = (SymbolFieldFixed *)m_GlobalSymbols.StoreProtoField(m_CurrProtoSym,newSymb);


#ifdef DEBUG_COMPACT_FIELDS
	PrintSymbol(newSymb,"Debug_Fields_Fixed_COMPATTED.txt");
#endif

	/********************************************/
	//	BUILD & CONNECT NEW NODES	    //
	/********************************************/

	newNodeInt = new HIRNode(IR_FIELD,newSymb);  //leaf-node!!
	nbASSERT(newNodeInt!=NULL, "Problems on Allocation of the new Internal-node (leaf)");	
	
	newNodeExt = new HIRNode(IR_DEFFLD,newNodeInt);  
	nbASSERT(newNodeExt!=NULL, "Problems on Allocation of the new External-node");	

	/********************************************/
	//	BUILD & CONNECT NEW STATEMENT	    //
	/********************************************/

	newStmt = new StmtGen(newNodeExt);  
	nbASSERT(newStmt!=NULL, "Problems on Allocation of the new Statement");	

	if(deletelonelybits)		//THIS IS A SINGLE FIELD-FIXED BUT IT'S A CONTAINER!
	{
		nbASSERT(m_stopstmt->Next!=NULL,"IF THIS IS A CONTAINER - HERE MUST FOUND INNERFIELD! NOT NULL!");
		StmtBase *it;

#ifdef DEBUG_COMPACT_FIELDS
		ofstream Lonely_container("Debug_Fields_Container_lonely.txt",ios::app);
		Lonely_container <<"FROM: "<<((SymbolField *)m_stopstmt->Forest->Kids[0]->Sym)->Name<<" *******************"<<endl;
#endif
		//we need to find the last bitfield (cause now StopStmt is pointing to the container!)
		for( it = m_stopstmt->Next; it!=NULL && ((SymbolField *)it->Forest->Kids[0]->Sym)->FieldType==PDL_FIELD_BITFIELD ;it=it->Next)	//works well??
		{
#ifdef DEBUG_COMPACT_FIELDS
			Lonely_container <<"INNER: "<<((SymbolField *)it->Forest->Kids[0]->Sym)->Name<<endl;			
#endif
		}
		//nbASSERT(it!=NULL,"IF THIS IS A CONTAINER - HERE MUST FOUND INNERFIELD! NOT NULL!");
		before=m_startstmt->Prev; 
		after=it;

#ifdef DEBUG_COMPACT_FIELDS
		Lonely_container <<"END INNER &( "<<after<<" )"<<endl;			
#endif

	}
	else	
	{
		after=m_stopstmt->Next;
		before=m_startstmt->Prev;
	}


	/********************************************/
	/**	       CHECK HEAD & TAIL	   **/
	/********************************************/
	if(before==NULL)	//if first -> update HEAD
	{
		m_headcode->SetHead(newStmt);		//m_headcode = pointers to the first Statement
	}

	if(after==NULL)		//if last -> update TAIL
	{

		m_headcode->SetTail(newStmt);
	}

	
	//FIXME: Ivano - this method causes segfoult on windows
	//DESTROY ALL THE STATEMENTS Between Start & Stop
	//for(StmtBase *it = m_startstmt;it!=NULL && it!=after;it=it->Next)	
	//	destroy_stmt(it);

	/*********************************************/
	/**CONNECT WITH PREVIOUS AND NEXT STATEMENTS**/
	/*********************************************/

	newStmt->Next=after;
	newStmt->Prev=before;

	if(before!=NULL)	//it's the first stmt?	
	{
		before->Next=newStmt;
	}

	
	if(after!=NULL)		//it's the last stmt?	
	{
		after->Prev=newStmt;
	}


#ifdef DEBUG_COMPACT_FIELDS
	PrintStmt((StmtBase *)newStmt,"COMPATTED_FIXED_fields.txt");
#endif

}
/*******************************************************************
*			check if all the elements of the list are compattable  *
*******************************************************************/
bool CompField::AllCompattable(StmtWhile *stmt){

	//****************************************************************************//
	//		First Check: No Used Fields Here!			      //
	//****************************************************************************//
	//for each element of localRef check if references are all inside this loop
	if(m_loopcomp==1)		//this loop it's a leaf (considering loop-TREE) and it contain only - Not Used Fields 
	{

		#ifdef DEBUG_COMPACT_FIELDS	
			PrintStmt(stmt,"Debug_List_of_Loop_without_used_fields.asm");
		#endif

		return true;
	}

	//****************************************************************************//
	//		Second Check: 						      //
	//****************************************************************************//
	//	if Map<SymbF*,int> -> int == -1 means: FIELD USED OUTSIDE LOOPS
	//			   -> int > 0   means: FIELD USED INSIDE LOOPS
	//	IF USED OUTSIDE -> CAN'T COMPACT THIS LOOP
	//	IF USED INSIDE LOOPS CHECK IF IT'S USED ONLY HERE -> TRANSFORMFLAG = TRUE (#GLOBAL == #LOCAL)
	//	OTHERWISE WAIT PROTOCOL END, THEN CHECK IF CAN BE COMPATTED AND DO IT
	
	bool TransformFlag=true;
	bool never=false;
	IntMap::iterator it;
	IntMap::iterator global_it;
	IntMap::iterator localuse_it;

	for ( it=m_DefUseLocal.begin() ; it != m_DefUseLocal.end(); it++ )	// for each Defined-Used-Field (Inside this Loop)
  	{

	  global_it=m_symbolref.find((*it).first);				// Check if #ofUse is limitated to this loop
	  nbASSERT(global_it != m_symbolref.end(), "global MAP cannot be NULL here"); 

	  if((*global_it).second == -1)  // this field is used outside loops -> Can't Compact this one
	  {
		TransformFlag=false;  // for compatibility

		//Add this LOOP to NeverCompattableLoopList
		never=true;
          }

	  localuse_it=m_symboltemp.find((*it).first);
	  if(localuse_it != m_symboltemp.end()) 		//if this field is Used in expressions inside this loop 
	  {
	  	if((*global_it).second > (*localuse_it).second)	// if #ofTotalUse > #ofUse (Locally) -> Also used later!!
		{
		//Add this LOOP to UnCompattableLoopList
		TransformFlag=false;
		}
	  }else
		TransformFlag=false;		//this field is defined here but it used later... (outside this loop)
	}


	if(never==true)		
	{
		AddNeverCompattableLoop(stmt,&m_DefUseLocal);
	}
	else
	{
		if(TransformFlag==false)
		{
			//Add this LOOP to UnCompattableLoopList + this_IntMap	
			AddUncompattedLoop((StmtWhile*)stmt,&m_DefUseLocal);								
		}
	}

	
	return TransformFlag;	//IF TRUE COMPACT-LOOP NOW! 

}
void CompField::VisitStatement(StmtBase *stmt)
{
	nbASSERT(stmt != NULL , "stmt cannot be NULL");

//Start STATEMENT CHECK:
//if this statement it's not GEN -> means that can't be a DEFFLD! -> we must "close" compattable-fixed-field-list!!!!
	if(stmt->Kind!=STMT_GEN)
		StartCheck(NULL);	
//End of STATEMENT CHECK

	switch(stmt->Kind)
	{
	case STMT_GEN:			//here we recognize FIELD DEFINITIONS -> DEFFLD
	{

		#ifdef DEBUG_COMPACT_FIELDS
		   PrintStmt(stmt,"Debug_Statement_Gen.txt");
		#endif
		VisitTree(stmt->Forest);	
	}break;
	case STMT_JUMP:
	case STMT_JUMP_FIELD:
	{
		VisitJump((StmtJump*)stmt);
	}break;
	case STMT_CASE:	  			//Gettin inside a Case! New "Context"
	{
		VisitCase((StmtCase*)stmt, false);
	}break;
	case STMT_SWITCH: 			//Gettin inside a SWITCH! New "Context"
	{
		VisitSwitch((StmtSwitch*)stmt);
	}break;
	case STMT_IF:  				//Gettin inside an IF! New "Context"
	{
		VisitIf((StmtIf*)stmt);
	}break;
	case STMT_LOOP:  			//only <loop type="times2repeat">
	{
		VisitLoop((StmtLoop*)stmt);
	}break;
	case STMT_WHILE:			//LOOP SIZE -> (CONVERTED) -> STATEMENT WHILE -> i'll convert loop in one variable FIELD (if can)
	{
	
		//if this is Loop-Size (converted in pdlparser to stmtWhile!) -> try compatting, otherwise not! only visit
		
		if(((StmtWhile*)stmt)->isLoopSize==true)	
		{
			
			checkingloop++; 	//Integer USED TO IDENTIFY if This LOOP is the first one or it's an Inner-Loop
			m_loopcomp=1;	

			if(checkingloop>1)	//fields used inside LOOP expression-> If InnerLoop ->need to check it - Otherwise not!
				UpdateMapField((StmtWhile*)stmt);	//ADD field-expr to Local-MAP of Used fields


			VisitWhileDo((StmtWhile*)stmt);		//Visit inside this WHILE 
			if(AllCompattable((StmtWhile*)stmt))//check if the validity of these fields end here or if at least one is also referentiated later
			{
			#ifdef DEBUG_COMPACT_FIELDS
				PrintFieldsMap(&m_DefUseLocal,"Debug_List_of_Fields_Used_insideLoop_Compattable.txt");
			#endif
				/****TRANSFORM LOOP IN A VARIABLE-FIELD****/
				TransformLoop((StmtWhile*)stmt);
			}

			CleanTempMap(&m_symboltemp);	//Clean Used Fields of this loop 
			CleanTempMap(&m_DefUseLocal);	//Clean Used Fields Defined inside this loop

			checkingloop--;
		}
		else
		{
				VisitWhileDo((StmtWhile*)stmt);		//Visit inside this WHILE 			
		}

		}break;
	case STMT_DO_WHILE:   	//Gettin inside a WHILE! New "Context"
	{
		VisitDoWhile((StmtWhile*)stmt);
	}break;
	case STMT_BLOCK:  	//Gettin inside a Block! New "Context"
	{
		VisitBlock((StmtBlock*)stmt);
	}break;
	case STMT_BREAK:			
	case STMT_CONTINUE:		//If Break or Continue (Inside Loops) -> Can't Compact this!! Don't know Total Size!! (Offset)
	{
		//TEMP!! THIS loop must be added to NeverLOOP LIST!!?
		//AddNeverCompattableLoop(actualoop,&m_symboltemp);
		m_loopcomp=0;
	}break;
	case STMT_LABEL:
	case STMT_FINFOST:			
	case STMT_COMMENT:			
	{	
		//Do nothing
	}break;
	default:
		nbASSERT(false, "COMPACT_STMT: CANNOT BE HERE!");
		break;
	}

}