// $Id: DelocalizedArrayFetchInstruction.java,v 1.8 1999/11/28 15:14:34 golem 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.load_into_reg(c, c.r(0));
    c.generate_array_fetch(c.r(1), var.get_offset(), c.r(0));
    dest.store_from_reg(c, c.r(1));
  }


  /* 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);
  }
}
