print("  message *msg;\n");
print("  message *msg, *last = NULL;\n");   if (restrictedExecution) { print("  while (__credit_"+selfID+" <= __counter_"+selfID+") {\n"); }  { Iterator i = receives_from.iterator(); while (i.hasNext()) { int src = NodeEnumerator.getSIROperatorId((SIRStream)i.next()); print("  if (__msg_sock_"+src+"_"+selfID+"in->data_available()) {\n    handle_message__"+selfID+"(__msg_sock_"+src+"_"+selfID+"in);\n  } } }  if (restrictedExecution) { print("  } }
    public void visitFilter(SIRFilter self,			    SIRFilterIter iter) {	HashSet sendsCreditsTo = LatencyConstraints.getOutgoingConstraints(self);	boolean restrictedExecution = LatencyConstraints.isRestricted(self); 	boolean sendsCredits = (sendsCreditsTo.size() > 0);	selfID = NodeEnumerator.getSIROperatorId(self);	SIRPortal outgoing[] = SIRPortal.getPortalsWithSender(self);	SIRPortal incoming[] = SIRPortal.getPortalsWithReceiver(self);	HashSet sends_to = new HashSet();	HashSet receives_from = new HashSet();	for (int t = 0; t < outgoing.length; t++) {	    SIRStream[] receivers = outgoing[t].getReceivers();	    for (int i = 0; i < receivers.length; i++) {		sends_to.add(receivers[i]);	    }	}	for (int t = 0; t < incoming.length; t++) {	    SIRPortalSender[] senders = incoming[t].getSenders();	    for (int i = 0; i < senders.length; i++) {		receives_from.add(senders[i].getStream());	    }	}	/*	{	    Iterator i = sends_to.iterator();	    while (i.hasNext()) {		print("// sends to "+NodeEnumerator.getSIROperatorId((SIRStream)i.next())+"\n");	    }	}	{	    Iterator i = receives_from.iterator();	    while (i.hasNext()) {		print("// receives from "+NodeEnumerator.getSIROperatorId((SIRStream)i.next())+"\n");	    }	}        	print("\n");	*/		//System.out.print("filter.equals(self): "+(filter.equals(self))+"\n");	//Entry point of the visitor	print("#include <stdlib.h>\n");	print("#include <unistd.h>\n");	print("#include <math.h>\n\n");		//if there are structures in the code, include	//the structure definition header files		//if (RawBackend.structures.length > 0) 	//    print("#include \"structs.h\"\n");	p.print("#include <init_instance.h>\n");	p.print("#include <mysocket.h>\n");	p.print("#include <peek_stream.h>\n");	p.print("#include <sdep.h>\n");	p.print("#include <message.h>\n");	p.print("#include <timer.h>\n");	p.print("\n");	p.print("extern int __number_of_iterations;\n");	p.print("message *__msg_stack_"+selfID+";\n");	p.print("int __counter_"+selfID+" = 0;\n");	if (restrictedExecution) {	    p.print("int __credit_"+selfID+" = 0;\n");	}	{	    Iterator i = sends_to.iterator();	    while (i.hasNext()) {		print("sdep *sdep_"+selfID+"_"+NodeEnumerator.getSIROperatorId((SIRStream)i.next())+";\n");	    }	}	//////////////////////////////////////////////	// Declare Filter Fields	//Visit fields declared in the filter class	JFieldDeclaration[] fields = self.getFields();	for (int i = 0; i < fields.length; i++)	   fields[i].accept(this);	print("\n");	//////////////////////////////////////////////	// Declare Socket Variables	//declare input/output socket variables	NetStream in = RegisterStreams.getFilterInStream(self);	NetStream out = RegisterStreams.getFilterOutStream(self);	if (in != null) {	    	    print("peek_stream<"+self.getInputType().toString()+"> *"+in.name()+"in;\n");	}	if (out != null) {	    	    print("mysocket *"+out.name()+"out;\n");	}	{	    Iterator i = sends_to.iterator();	    while (i.hasNext()) {		print("mysocket *__msg_sock_"+selfID+"_"+NodeEnumerator.getSIROperatorId((SIRStream)i.next())+"out;\n");	    }	}	{	    Iterator i = receives_from.iterator();	    while (i.hasNext()) {		print("mysocket *__msg_sock_"+NodeEnumerator.getSIROperatorId((SIRStream)i.next())+"_"+selfID+"in;\n");	    }	}	//////////////////////////////////////////////	// Declare_Sockets Method	print("\nvoid __declare_sockets_"+selfID+"() {\n");	if (in != null) {	    print("  init_instance::add_incoming("+in.getSource()+","+in.getDest()+");\n");	}	if (out != null) {	    print("  init_instance::add_outgoing("+out.getSource()+","+out.getDest()+",lookup_ip(init_instance::get_node_name("+out.getDest()+")));\n");	}	{	    Iterator i = sends_to.iterator();	    while (i.hasNext()) {		int dst = NodeEnumerator.getSIROperatorId((SIRStream)i.next());		print("  init_instance::add_outgoing("+(-selfID-1)+","+(-dst-1)+",lookup_ip(init_instance::get_node_name("+dst+")));\n");	    }	}	{	    Iterator i = receives_from.iterator();	    while (i.hasNext()) {		int src = NodeEnumerator.getSIROperatorId((SIRStream)i.next());		print("  init_instance::add_incoming("+(-src-1)+","+(-selfID-1)+");\n");	    }	}	print("}\n");	//////////////////////////////////////////////	// Method Declarations	JMethodDeclaration work = self.getWork();	//visit methods of filter, print the declaration first	declOnly = true;	JMethodDeclaration[] methods = self.getMethods();	for (int i =0; i < methods.length; i++) {	    if (!methods[i].equals(work)) methods[i].accept(this);	}	print("\nvoid check_messages__"+selfID+"();\n");	print("\nvoid handle_message__"+selfID+"(mysocket *sock);\n");	print("\nvoid send_credits__"+selfID+"();\n");	print("\n");	//////////////////////////////////////////////	// Method Bodies	//now print the functions with body	declOnly = false;	for (int i =0; i < methods.length; i++) {	    if (!methods[i].equals(work)) methods[i].accept(this);	}	//////////////////////////////////////////////	// Check Messages	print("\nvoid check_messages__"+selfID+"() {\n");	print("  message *msg;\n");	print("  for (msg = __msg_stack_"+selfID+"; msg != NULL; msg = msg->next) {\n");	print("    if (msg->execute_at == __counter_"+selfID+") {\n");	SIRPortal[] portals = SIRPortal.getPortalsWithReceiver(self);	/* there should be only one portal or none */	if (portals.length == 1) {	    	    CClass pclass = portals[0].getPortalType().getCClass();	    CMethod pmethods[] = pclass.getMethods();	    for (int i = 0 ; i < pmethods.length; i++) {		CMethod portal_method = pmethods[i];		CType portal_method_params[] = portal_method.getParameters();		String method_name = portal_method.getIdent();		int length = method_name.length();		if (!method_name.startsWith("<") && 		    !method_name.endsWith(">")) {		    print("      if (msg->method_id == "+i+") {\n");		    for (int t = 0; t < methods.length; t++) {		    			String thread_method_name = methods[t].getName();						if (thread_method_name.startsWith(method_name) &&			    thread_method_name.charAt(length) == '_' &&			    thread_method_name.charAt(length + 1) == '_') {			    			    int param_count = methods[t].getParameters().length;			    for (int a = 0; a < param_count; a++) {				if (portal_method_params[a].toString().equals("int")) {				    print("        int p"+a+" = msg->get_int_param();\n");				}				if (portal_method_params[a].toString().equals("float")) {				    print("        float p"+a+" = msg->get_float_param();\n");				}			    }			    print("        "+thread_method_name+"__"+selfID+"(");			    for (int a = 0; a < param_count; a++) {				if (a > 0) print(", ");				print("p"+a);			    }			    print(");\n");			}		    }		    print("      }\n");		}	    }	}	print("    }\n");	print("  } // for \n");		if (restrictedExecution) {	    print("  while (__credit_"+selfID+" <= __counter_"+selfID+") {\n");	}		{	    Iterator i = receives_from.iterator();	    while (i.hasNext()) {		int src = NodeEnumerator.getSIROperatorId((SIRStream)i.next());		print("  if (__msg_sock_"+src+"_"+selfID+"in->data_available()) {\n    handle_message__"+selfID+"(__msg_sock_"+src+"_"+selfID+"in);\n  } // if\n");	    }	}	if (restrictedExecution) {	    print("  } // while \n");	}	print("}\n");		//////////////////////////////////////////////	// Handle Message Method		print("\nvoid handle_message__"+selfID+"(mysocket *sock) {\n");	print("  int size = sock->read_int();\n");		if (restrictedExecution) {	    print("  if (size == -1) { // a credit message received\n");	    print("    __credit_"+selfID+" = sock->read_int();\n");	    print("    return;\n");	    print("  };\n");	}	print("  int index = sock->read_int();\n");	print("  int iteration = sock->read_int();\n");	print("  printf(\"Message receieved! thread: "+selfID+", method_index: %d excute at iteration: %d\\n\", index, iteration);\n");	print("  if (iteration > 0) {\n");	print("    message *msg = new message(size, index, iteration);\n");	print("    msg->read_params(sock);\n");	print("    __msg_stack_"+selfID+" = msg->push_on_stack(__msg_stack_"+selfID+");\n");	print("    return;\n");	print("  }\n");	//SIRPortal[] portals = SIRPortal.getPortalsWithReceiver(self);	/* there should be only one portal or none */	if (portals.length == 1) {	    	    CClass pclass = portals[0].getPortalType().getCClass();	    CMethod pmethods[] = pclass.getMethods();	    for (int i = 0 ; i < pmethods.length; i++) {		CMethod portal_method = pmethods[i];		CType portal_method_params[] = portal_method.getParameters();		String method_name = portal_method.getIdent();		int length = method_name.length();		if (!method_name.startsWith("<") && 		    !method_name.endsWith(">")) {		    print("  if (index == "+i+") {\n");		    for (int t = 0; t < methods.length; t++) {		    			String thread_method_name = methods[t].getName();						if (thread_method_name.startsWith(method_name) &&			    thread_method_name.charAt(length) == '_' &&			    thread_method_name.charAt(length + 1) == '_') {			    			    int param_count = methods[t].getParameters().length;			    for (int a = 0; a < param_count; a++) {				if (portal_method_params[a].toString().equals("int")) {				    print("    int p"+a+" = sock->read_int();\n");				}				if (portal_method_params[a].toString().equals("float")) {				    print("    float p"+a+" = sock->read_float();\n");				}			    }			    print("    "+thread_method_name+"__"+selfID+"(");			    for (int a = 0; a < param_count; a++) {				if (a > 0) print(", ");				print("p"+a);			    }			    print(");\n");			}		    }		    print("  }\n");		}	    }	}	print("}\n");		//////////////////////////////////////////////	// Send Credits Method	Iterator constrIter;	constrIter = sendsCreditsTo.iterator();	while (constrIter.hasNext()) {	    LatencyConstraint constraint = (LatencyConstraint)constrIter.next();	    int receiver = NodeEnumerator.getSIROperatorId(constraint.getReceiver());	    int sourcePhase = constraint.getSourceSteadyExec();	    print("int __init_"+selfID+"_"+receiver+" = "+constraint.getSourceInit()+";\n");	    print("int __source_phase_"+selfID+"_"+receiver+" = "+sourcePhase+";\n");	    print("int __dest_phase_"+selfID+"_"+receiver+" = "+constraint.getDestSteadyExec()+";\n");	    print("int __current_"+selfID+"_"+receiver+" = 0;\n");	    print("int __dest_offset_"+selfID+"_"+receiver+" = 0;\n");	    print("int __dependency_"+selfID+"_"+receiver+"[] = {");	    int y;	    	    for (y = 0; y < sourcePhase - 1; y++) {		print(constraint.getDependencyData(y)+",");	    }	    print(constraint.getDependencyData(y)+"};\n");	    print("\n");	}		print("\nvoid send_credits__"+selfID+"() {\n");	print("  int tmp;\n");	//HashSet sendsCreditsTo = LatencyConstraints.getOutgoingConstraints(self);	//boolean restrictedExecution = LatencyConstraints.isRestricted(self); 	//boolean sendsCredits;	constrIter = sendsCreditsTo.iterator();	while (constrIter.hasNext()) {	    LatencyConstraint constraint = (LatencyConstraint)constrIter.next();	    int receiver = NodeEnumerator.getSIROperatorId(constraint.getReceiver());	    print("  if (__counter_"+selfID+" > __init_"+selfID+"_"+receiver+") {\n");	    print("    tmp = __dependency_"+selfID+"_"+receiver+"[__current_"+selfID+"_"+receiver+"];\n");	    print("    if (tmp > 0) {\n");	    print("      __msg_sock_"+selfID+"_"+receiver+"out->write_int(-1);\n");	    print("      __msg_sock_"+selfID+"_"+receiver+"out->write_int(tmp + __dest_offset_"+selfID+"_"+receiver+");\n");	    print("    }\n");	    print("    __current_"+selfID+"_"+receiver+" = (__current_"+selfID+"_"+receiver+" + 1) % __source_phase_"+selfID+"_"+receiver+";\n");	    print("    if (__current_"+selfID+"_"+receiver+" == 0) __dest_offset_"+selfID+"_"+receiver+" += __dest_phase_"+selfID+"_"+receiver+";\n");	    print("  }\n");   	}	print("}\n");	//////////////////////////////////////////////	// The Run Method	//now the run function	print("\nvoid run_"+selfID+"() {\n");	print("  int i;\n");	if (in != null) {	    print("  "+in.name()+"in = new peek_stream<"+self.getInputType().toString()+">(new mysocket(init_instance::get_incoming_socket("+in.getSource()+","+in.getDest()+")));\n");	}	if (out != null) {	    print("  "+out.name()+"out = new mysocket(init_instance::get_outgoing_socket("+out.getSource()+","+out.getDest()+"));\n");	}	{	    Iterator i = sends_to.iterator();	    while (i.hasNext()) {		int dst = NodeEnumerator.getSIROperatorId((SIRStream)i.next());		print("  __msg_sock_"+selfID+"_"+dst+"out = new mysocket(init_instance::get_outgoing_socket("+(-selfID-1)+","+(-dst-1)+"));\n");	    }	}	{	    Iterator i = receives_from.iterator();	    while (i.hasNext()) {		int src = NodeEnumerator.getSIROperatorId((SIRStream)i.next());		print("  __msg_sock_"+src+"_"+selfID+"in = new mysocket(init_instance::get_incoming_socket("+(-src-1)+","+(-selfID-1)+"));\n");	    }	}	{	    Iterator i = sends_to.iterator();	    while (i.hasNext()) {		SIRStream stream = (SIRStream)i.next();		int fromID = selfID;		int toID = NodeEnumerator.getSIROperatorId(stream);		print("\n  //SDEP from: "+fromID+" to: "+toID+";\n");		streamit.scheduler2.constrained.Scheduler cscheduler =		    new streamit.scheduler2.constrained.Scheduler(ClusterBackend.topStreamIter);				streamit.scheduler2.iriter.Iterator firstIter = 		    IterFactory.createIter((SIRStream)NodeEnumerator.getNode(selfID));		streamit.scheduler2.iriter.Iterator lastIter = 		    IterFactory.createIter(stream);					streamit.scheduler2.SDEPData sdep;				try {		    sdep = cscheduler.computeSDEP(firstIter, lastIter);		    		    int srcInit = sdep.getNumSrcInitPhases();		    int srcSteady = sdep.getNumSrcSteadyPhases();		    int dstInit = sdep.getNumDstInitPhases();		    int dstSteady = sdep.getNumDstSteadyPhases();		    String sdepname = "sdep_"+fromID+"_"+toID;		    print("  "+sdepname+" = new sdep("+			  srcInit+","+dstInit+","+			  srcSteady+","+dstSteady+");\n");		    for (int y = 0; y < dstInit + dstSteady + 1; y++) {			print("  "+sdepname+"->setDst2SrcDependency("+y+","+sdep.getSrcPhase4DstPhase(y)+");\n");		    }		    		} catch (streamit.scheduler2.constrained.NoPathException ex) {		    		}	    }	}		print("\n  "+ClusterExecutionCode.rawMain+"__"+selfID+"();\n");		/*	print("  "+self.getInit().getName()+"__"+selfID+"();\n");	if (out == null) print("  timer t;\n");	if (out == null) print("  t.start();\n");		print("  for (i = 0; i < __number_of_iterations; i++) { \n");	{	    Iterator i = receives_from.iterator();	    while (i.hasNext()) {		int src = NodeEnumerator.getSIROperatorId((SIRStream)i.next());		print("    if (__msg_sock_"+src+"_"+selfID+"in->data_available()) {\n      handle_message_"+selfID+"(__msg_sock_"+src+"_"+selfID+"in);\n    }\n");	    }	}		print("    "+self.getWork().getName()+"__"+selfID+"();\n");	print("  } \n");	if (out == null) print("  t.stop();\n");	if (out == null) print("  t.output(stderr);\n");	*/	print("  sleep(3); // so that sockets dont get closed\n");	print("}\n");       	createFile(selfID);    }