package IR2;

import java.util.*;

public class Web {

  private Vector strands;
  private DelocalizedLValue symbolic_register;

  public Web() {
    strands = new Vector();
  }

  public boolean add_DUChain(DUChain c){
    if (strands.isEmpty()) {
      //System.out.println("Pong");
      strands.addElement(c);
      return true;
    } else {
      Enumeration stuff = strands.elements();
      while (stuff.hasMoreElements()) {
        DUChain foo = (DUChain)stuff.nextElement();
        //System.out.println("Pung");
        if (c.related(foo)) {
          strands.addElement(c);
          return true;
        }
      }
      return false;
    }
  }

  public boolean interfere(Web w){
    Enumeration ostrands = w.get_strands();
    while (ostrands.hasMoreElements()){
      DUChain od = (DUChain)ostrands.nextElement();
      Enumeration lstrands = strands.elements();
      while (lstrands.hasMoreElements()){
        DUChain ld = (DUChain)lstrands.nextElement();
        if (ld.overlap(od)) {
          return true;
        }
      }
    }
    return false;
  }
    
  public Enumeration get_strands(){
    return strands.elements();
  }

  public DelocalizedLValue get_lvalue() {
    return symbolic_register;
  }
  
  // Right. So switch the actual lvalue here to a symbolic register. This
  // way it is now completely separated from any other uses of the lvalue
  // in other places in the procedure. They are separate nodes now.
  public void substitute_symbolic_regs(MethodDescriptor d) {
    Enumeration chains;

    // Create a symbolic register.
    symbolic_register = new SymbolicRegister(d);

    // For each chain, substitute the sym_reg in for the original register.
    for (chains = strands.elements(); chains.hasMoreElements(); )
      ((DUChain)chains.nextElement()).substitute_lvalue(symbolic_register);
  }
}
