// $Id: DelocalizedArrayFetchInstruction.java,v 1.4 1999/12/10 13:36:24 mpp Exp $

package IR2;

import java.util.*;

public class DelocalizedArrayFetchInstruction
  extends DelocalizedInstruction {

  private DelocalizedLValue dest;
  private ArrayVarDescriptor var;
  private DelocalizedRValue index;

  public DelocalizedArrayFetchInstruction(DelocalizedLValue d,
                                          ArrayVarDescriptor v,
                                          DelocalizedRValue i) {
	dest = d; var = v; index = i;
  }

  public DelocalizedLValue destination() {
    return dest;
  }

  public Enumeration lsources() {
    if (index instanceof DelocalizedLValue) {
      return new ShortEnumeration(index);
    } else {
      return new ShortEnumeration();
    }
  }

  public void generate_code(Codegen c){
    index.rproxy().load_into_reg(c, c.r(0));
    c.generate_array_fetch(c.r(1), var.get_offset(), c.r(0));
    dest.lproxy().store_from_reg(c, c.r(1));
  }

  // Substitution. For swapping in a symbolic register
  public void substitute_lvalue(DelocalizedLValue oldlval, 
                                DelocalizedLValue newlval) {
    if (dest.lproxy() == oldlval) dest = newlval;
  }

  public void substitute_rvalue(DelocalizedLValue oldlval, 
                                DelocalizedLValue newlval) {
    if (index.rproxy() == oldlval) index = newlval;
  }

  /* Copy propagation */
  public boolean kills_copy(CopyInstruction copy) {
    return dest.equals(copy.copy_source())
      || dest.equals(copy.copy_destination());
  }
  public void substitute_copies(Hashtable map) {
    if (map.contains(index))
      index = (DelocalizedRValue) map.get(index);
  }
}
