62bf8869a5c8392c44f8b8e3fec5e4e68c0db8d6
[fleet.git] / src / edu / berkeley / fleet / dataflow / UnPunctuatorNode.java
1 package edu.berkeley.fleet.dataflow;
2 import java.util.*;
3 import edu.berkeley.fleet.loops.*;
4 import edu.berkeley.fleet.api.*;
5 import edu.berkeley.fleet.api.Instruction.Set.FlagFunction;
6
7 /**
8  *  Takes an item from the "count" port, then passes/drops through count-1
9  *  items from "val" to "out" and drops/passes the count-th item.
10  */
11 public class UnPunctuatorNode extends Node {
12     private final Ship    ship  = dfg.pool.allocateShip("Counter");
13     private final InPort  op    = new DockInPort("op",    ship.getDock("inOp"), "PASS_C2_V1");
14     public  final InPort  val   = new DockInPort("val",   ship.getDock("in1"));
15     public  final InPort  count = new DockInPort("count", ship.getDock("in2"));
16     public  final OutPort out   = new DockOutPort("out", ship.getDock("out")) {
17             protected void build(CodeBag ctx, LoopFactory lf) {
18                 lf = lf.makeNext(0);
19                 lf.abortLoopIfTorpedoPresent();
20                 lf.collectWord();
21                 lf.setFlags(FlagFunction.ZERO.add(Predicate.FlagC), FlagFunction.ZERO);
22                 lf.setPredicate(passPunctuationInsteadOfValues ? Predicate.FlagA : Predicate.NotFlagA);
23                 lf.abortLoopIfTorpedoPresent();
24                 peer.recvToken(lf);
25                 peer.sendWord(lf);
26                 lf.setPredicate(null);
27             }
28         };
29
30     private boolean passPunctuationInsteadOfValues;
31     public UnPunctuatorNode(DataFlowGraph dfg) { this(dfg, false); }
32     public UnPunctuatorNode(DataFlowGraph dfg, boolean passPunctuationInsteadOfValues) {
33         super(dfg);
34         this.passPunctuationInsteadOfValues = passPunctuationInsteadOfValues;
35     }
36 }