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

package IR2;

import java.util.*;

public class DelocalizedArrayStoreInstruction
  extends DelocalizedInstruction {

  private ArrayVarDescriptor var;
  private DelocalizedRValue index;
  private DelocalizedRValue src;

  public DelocalizedArrayStoreInstruction(ArrayVarDescriptor v,
                                          DelocalizedRValue i,
                                          DelocalizedRValue s) {
	var = v; index = i; src = s;
  }

  public DelocalizedLValue destination() {
    return null;
  }

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

  public void generate_code(Codegen c){
    index.rproxy().load_into_reg(c, c.r(0));
    src.rproxy().load_into_reg(c, c.r(1));
    c.generate_array_store(var.get_offset(), c.r(0), c.r(1));
  }

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

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

  /* Copy propagation */
  public boolean kills_copy(CopyInstruction copy) {
    return false;
  }
  public void substitute_copies(Hashtable map) {
    if (map.contains(index))
      index = (DelocalizedRValue) map.get(index);
    if (map.contains(src))
      src   = (DelocalizedRValue) map.get(src);
  }
}
