}
ins = new FullIns(tile); if(end) ins.addRoute(SwitchIPort.CSTO,dest); else ins.addRoute(SwitchIPort.CSTO,dest2); code.appendIns(ins,false);  for(int i=0;i<pos*numPop;i++) { ins = new FullIns(tile); ins.addRoute(src,dest); code.appendIns(ins, false); } }
    private static void createLinearSwitchCode(FilterTraceNode node,FilterInfo filterInfo,int mult,RawTile tile,RawChip rawChip) {	System.err.println("Creating switchcode linear: "+node+" "+mult);	ComputeNode sourceNode = null;	//Get sourceNode and input port	if (node.getPrevious().isFilterTrace())	    sourceNode = rawChip.getTile(((FilterTraceNode)node.getPrevious()).getX(), 					 ((FilterTraceNode)node.getPrevious()).getY());	else {	    if (KjcOptions.magicdram && node.getPrevious() != null &&		node.getPrevious().isInputTrace() &&		tile.hasIODevice()) 		sourceNode = tile.getIODevice();	    else 		sourceNode = 		    IntraTraceBuffer.getBuffer((InputTraceNode)node.getPrevious(), 					       node).getNonRedundant().getDRAM();	}	SwitchIPort src = rawChip.getIPort(sourceNode, tile);	SwitchIPort src2 = rawChip.getIPort2(sourceNode, tile);	sourceNode = null;	//Get destNode and output port	ComputeNode destNode = null;	if (node.getNext().isFilterTrace())	    destNode = rawChip.getTile(((FilterTraceNode)node.getNext()).getX(), 				       ((FilterTraceNode)node.getNext()).getY());	else {	    if (KjcOptions.magicdram && node.getNext() != null &&		node.getNext().isOutputTrace() && tile.hasIODevice())		destNode = tile.getIODevice();	    else {		destNode = 		    IntraTraceBuffer.getBuffer(node, (OutputTraceNode)node.getNext()).		    getNonRedundant().getDRAM();	    }	}	SwitchOPort dest = rawChip.getOPort(tile, destNode);	SwitchOPort dest2 = rawChip.getOPort2(tile, destNode);	destNode = null;	//Get filter properties	FilterContent content = node.getFilter();	final int numCoeff = content.getArray().length;	final int peek=content.getPeek();	final int pop = content.getPopCount();	final int numPop = numCoeff/pop;	final boolean begin=content.getBegin();	final boolean end=content.getEnd();	final int pos=content.getPos();	int bufferSize; //Use peek buffer while bufferSize>0 else use net	int index=content.getTotal()-pos-1;	if(index==0) //If first tile	    bufferSize=filterInfo.remaining;	else { //Find first tile	    TraceNode curNode=node;	    for(int i=index;i>0;i--)		curNode=curNode.getPrevious();	    FilterInfo parentInfo=FilterInfo.getFilterInfo((FilterTraceNode)curNode);	    bufferSize=parentInfo.remaining;	}	if(filterInfo.initMult>0)	    bufferSize+=peek-pop;	int turns=pos*numCoeff; //Default number of turns	int extra=0; //Extra turns needed	int excess=bufferSize-pop*(int)Math.ceil(((double)peek)/pop);//pop*(numCoeff+turns);	if(excess>0) { //Handle excess items on peekbuffer	    extra=(int)Math.ceil(((double)excess)/pop);	    turns+=extra;	}	//System.out.println("SRC: "+src);	//System.out.println("DEST: "+dest);	//Begin codegen	SwitchCodeStore code = tile.getSwitchCode();	//System.err.println("Getting HERE!");	code.appendIns(new Comment("HERE!"),false);	//Get loop counter	FullIns loopCount=new FullIns(tile,new MoveIns(SwitchReg.R3,SwitchIPort.CSTO));	code.appendIns(loopCount,false);	//Preloop	if(begin) {	    int bufferRemaining=bufferSize;	    for(int i=0;i<numPop;i++)		for(int j=0;j<pop;j++)		    if(bufferRemaining>0)			bufferRemaining--;		    else {			//Pass first value			FullIns ins=new FullIns(tile, new MoveIns(SwitchReg.R1, src));			ins.addRoute(src, SwitchOPort.CSTI);			code.appendIns(ins, false);			//Repeat first value			for(int k=i-1;k>=0;k--) {			    FullIns newIns = new FullIns(tile);			    newIns.addRoute(SwitchReg.R1, SwitchOPort.CSTI);			    code.appendIns(newIns, false);			}		    } 	    if(turns>0) {		//Order between values (from peek buffer) and partial sums is reversed		//So use Reg2 as a buffer to reorder partial sum and values		//Save partial sum		FullIns ins=new FullIns(tile,new MoveIns(SwitchReg.R2,SwitchIPort.CSTO));		code.appendIns(ins, false);		for(int turn=0;turn<turns;turn++)		    for(int j = 0; j<pop; j++) {			if(bufferRemaining>0) {			    //Pass value from peek buffer			    ins=new FullIns(tile);			    ins.addRoute(SwitchIPort.CSTO,dest);			    code.appendIns(ins,false);			    bufferRemaining--;			} else {			    //Pass first value			    ins=new FullIns(tile, new MoveIns(SwitchReg.R1, src));			    ins.addRoute(src, SwitchOPort.CSTI);			    ins.addRoute(src,dest); //Send to next tile			    code.appendIns(ins, false);			    //Repeat first value			    for(int k=numPop-2;k>=0;k--) {				FullIns newIns = new FullIns(tile);				newIns.addRoute(SwitchReg.R1, SwitchOPort.CSTI);				code.appendIns(newIns, false);			    }			}			if(j==0) { //Partial sum			    //Save partial sum			    FullIns newIns;			    if(turn<turns-1)				newIns=new FullIns(tile,new MoveIns(SwitchReg.R2,SwitchIPort.CSTO));			    else				newIns=new FullIns(tile); //Don't pull off last partial sum			    if(end) //Send out partial sum				newIns.addRoute(SwitchReg.R2,dest); //Final output to static net1			    else				newIns.addRoute(SwitchReg.R2,dest2);			    code.appendIns(newIns, false);			}		    }	    }	} else {	    for(int i = 0; i<numPop; i++) {		for(int j = 0; j<pop; j++) {		    //Pass first value		    FullIns ins = new FullIns(tile, new MoveIns(SwitchReg.R1, src));		    ins.addRoute(src, SwitchOPort.CSTI);		    code.appendIns(ins, false);		    //Repeat first value		    for(int k = i-1; k>= 0; k--) {			FullIns newIns = new FullIns(tile);			newIns.addRoute(SwitchReg.R1, SwitchOPort.CSTI);			code.appendIns(newIns, false);		    }		    //Pass in partial sum		    if(j==0) {			FullIns newIns=new FullIns(tile);			newIns.addRoute(src2, SwitchOPort.CSTI2);			code.appendIns(newIns, false);		    }		}	    }	    for(int turn=0;turn<turns;turn++)		for(int j = 0; j<pop; j++) {		    //Pass first value		    FullIns ins = new FullIns(tile, new MoveIns(SwitchReg.R1, src));		    ins.addRoute(src, SwitchOPort.CSTI);		    if(!end)			ins.addRoute(src,dest);		    code.appendIns(ins, false);		    //Repeat first value		    for(int k = numPop-2; k>= 0; k--) {			FullIns newIns = new FullIns(tile);			newIns.addRoute(SwitchReg.R1, SwitchOPort.CSTI);			code.appendIns(newIns, false);		    }		    //Pass in partial sum		    if(j==0) {			FullIns newIns=new FullIns(tile);			newIns.addRoute(src2, SwitchOPort.CSTI2);			//Pass out partial sum to next filter			if(end)			    newIns.addRoute(SwitchIPort.CSTO,dest); //Final sum goes to static1			else			    newIns.addRoute(SwitchIPort.CSTO,dest2);			code.appendIns(newIns, false);		    }		}	}	//Innerloop	Label label = code.getFreshLabel();	final int numTimes=Linear.getMult(numCoeff);	final int target=filterInfo.steadyMult-2-extra; //2 iterations start before innerloop	final int newSteadyMult=target/numTimes;	final int remainingExec=target-newSteadyMult*numTimes;	//final int remainingExec=1;	int pendingSends=0;	//int pendingReceives=0;	int deferredSends=0; //Add a delay	FullIns ins=null;	for(int repeat=0;repeat<2+remainingExec;repeat++) {	    int times=0;	    if(repeat==1)		code.appendIns(label,false);	    for(int i = 0;i<numTimes;i++) {		for(int j=0;j<pop;j++) {		    for(int k=0;k<numPop;k++) {			if(j==0&&k==numPop-1)			    //pendingSends++;			    deferredSends++;			times++;			if(k==0) {			    ins=new FullIns(tile,new MoveIns(SwitchReg.R1, src));			    ins.addRoute(src, SwitchOPort.CSTI);			    if(!end) {				ins.addRoute(src,dest);			    }			} else {			    ins=new FullIns(tile);			    ins.addRoute(SwitchReg.R1, SwitchOPort.CSTI); //Temp reg			}			// Add Send			if(pendingSends>0) {			    if(end)				ins.addRoute(SwitchIPort.CSTO,dest); //Final sum goes to static1			    else				ins.addRoute(SwitchIPort.CSTO,dest2);			    pendingSends--;			}			code.appendIns(ins, false);			if(times==4) {			    times=0;			    /*int saveDeferredSends=deferredSends;			      while(pendingSends>0||deferredSends>0) {			      ins=new FullIns(tile);			      if(pendingSends>0) {			      if(end)			      ins.addRoute(SwitchIPort.CSTO,dest); //Final sum goes to static1			      else			      ins.addRoute(SwitchIPort.CSTO,dest2);			      pendingSends--;			      }			      if(deferredSends>0) {			      if(!begin) {			      ins.addRoute(src2,SwitchOPort.CSTI2);			      }			      deferredSends--;			      }			      code.appendIns(ins,false);			      }			      pendingSends=saveDeferredSends;*/			    			    /*if(pendingSends>0) {			      for(int l=0;l<pendingSends;l++) {			      ins=new FullIns(tile);			      if(end)			      ins.addRoute(SwitchIPort.CSTO,dest); //Final sum goes to static1			      else			      ins.addRoute(SwitchIPort.CSTO,dest2);			      code.appendIns(ins,false);			      }			      pendingSends=0;			      }*/			    if(deferredSends>0) {				pendingSends=deferredSends;				//pendingRecieves=defferredSends;				for(int l=0;l<deferredSends;l++) {				    ins=new FullIns(tile); //Put receive code here				    if(!begin) {					ins.addRoute(src2,SwitchOPort.CSTI2);				    }				    code.appendIns(ins,false);				}				deferredSends=0;			    }			}		    }		}	    }	    if(repeat==1)		ins.setProcessorIns(new BnezdIns(SwitchReg.R3,SwitchReg.R3,label.getLabel()));	}	if(pendingSends>0) {	    for(int l=0;l<pendingSends;l++) {		ins=new FullIns(tile);		if(end)		    ins.addRoute(SwitchIPort.CSTO,dest); //Final sum goes to static1		else		    ins.addRoute(SwitchIPort.CSTO,dest2);		code.appendIns(ins,false);	    }	    pendingSends=0;	}	//Postloop	turns=index*numPop+extra;	System.out.println("SWITCH TURNS: "+turns);	if(begin) {	    //int emptySpots=pop*(turns+numPop-1)-bufferSize; 	    if(turns>0) {		//Order between values (from peek buffer) and partial sums is reversed		//So use Reg2 as a buffer to reorder partial sum and values		//Save partial sum		//ins=new FullIns(tile,new MoveIns(SwitchReg.R2,SwitchIPort.CSTO));		//code.appendIns(ins, false);		for(int turn=0;turn<turns;turn++)		    for(int j = 0; j<pop; j++) {			//if(emptySpots>0) {			    //Pass first value			    ins=new FullIns(tile, new MoveIns(SwitchReg.R1, src));			    ins.addRoute(src, SwitchOPort.CSTI);			    ins.addRoute(src,dest); //Send to next tile			    code.appendIns(ins, false);			    //Repeat first value			    for(int k=numPop-2;k>=0;k--) {				FullIns newIns = new FullIns(tile);				newIns.addRoute(SwitchReg.R1, SwitchOPort.CSTI);				code.appendIns(newIns, false);			    }			    //emptySpots--;			    /*} else {			    //Pass value from peek buffer			    ins=new FullIns(tile);			    ins.addRoute(SwitchIPort.CSTO,dest);			    code.appendIns(ins,false);			    }*/			if(j==0) { //Partial sum			    //Save partial sum			    FullIns newIns=new FullIns(tile);			    /*if(turn<turns-1)			      newIns=new FullIns(tile,new MoveIns(SwitchReg.R2,SwitchIPort.CSTO));			      else			      newIns=new FullIns(tile); //Don't pull off last partial sum*/			    if(end) //Send out partial sum				newIns.addRoute(SwitchReg.R2,dest); //Final output to static net1			    else				newIns.addRoute(SwitchReg.R2,dest2);			    code.appendIns(newIns, false);			}		    }	    }	    for(int i=0;i<numPop;i++)		for(int j=0;j<pop;j++) {		    /*if(emptySpots>0)			emptySpots--;			else {*/		    //Pass first value		    ins=new FullIns(tile, new MoveIns(SwitchReg.R1, src));		    ins.addRoute(src, SwitchOPort.CSTI);		    code.appendIns(ins, false);		    //Repeat first value		    for(int k=i-1;k>=0;k--) {			FullIns newIns = new FullIns(tile);			newIns.addRoute(SwitchReg.R1, SwitchOPort.CSTI);			code.appendIns(newIns, false);		    }		    //}		}	} else {	    for(int turn=0;turn<turns;turn++)		for(int j = 0; j<pop; j++) {		    //Pass first value		    ins = new FullIns(tile, new MoveIns(SwitchReg.R1, src));		    ins.addRoute(src, SwitchOPort.CSTI);		    if(!end)			ins.addRoute(src,dest);		    code.appendIns(ins, false);		    //Repeat first value		    for(int k = numPop-2; k>= 0; k--) {			FullIns newIns = new FullIns(tile);			newIns.addRoute(SwitchReg.R1, SwitchOPort.CSTI);			code.appendIns(newIns, false);		    }		    //Pass in partial sum		    if(j==0) {			FullIns newIns=new FullIns(tile);			newIns.addRoute(src2, SwitchOPort.CSTI2);			//Pass out partial sum to next filter			if(end)			    newIns.addRoute(SwitchIPort.CSTO,dest); //Final sum goes to static1			else			    newIns.addRoute(SwitchIPort.CSTO,dest2);			code.appendIns(newIns, false);		    }		}	    for(int i = 0; i<numPop; i++) {		for(int j = 0; j<pop; j++) {		    //Pass first value		    ins = new FullIns(tile, new MoveIns(SwitchReg.R1, src));		    ins.addRoute(src, SwitchOPort.CSTI);		    code.appendIns(ins, false);		    //Repeat first value		    for(int k = i-1; k>= 0; k--) {			FullIns newIns = new FullIns(tile);			newIns.addRoute(SwitchReg.R1, SwitchOPort.CSTI);			code.appendIns(newIns, false);		    }		    //Pass in partial sum		    if(j==0) {			FullIns newIns=new FullIns(tile);			newIns.addRoute(src2, SwitchOPort.CSTI2);			code.appendIns(newIns, false);		    }		}	    }	}	    }