// $Id: ReturnInstruction.java,v 1.1.1.1 1999/12/05 22:19:52 mpp Exp $

package IR2;

import java.util.*;

public class ReturnInstruction 
  extends LowInstruction implements HighInstruction {
  protected RValue value;

  public ReturnInstruction() {
    value = null; next = null;
  }
  public ReturnInstruction(RValue val) {
    value = val; next = null;
  }

  public RValue get_value() {
    return value;
  }

  /* HighInstruction implementation. */
  public Enumeration subblocks() {
    return new ShortEnumeration();
  }

  public LowInstruction destructure(LowInstruction next) {
    this.next = next; 
    return this;
  }

  /* LowInstruction implementation */
  public String desc() {
    return "return";
  }

  public DelocalizedInstruction do_asm(Codegen c, MethodDescriptor d,
                                       CFG output,
                                       DelocalizedInstruction prev)
  {
    if (value != null) {
      DelocalizedRValue s = value.generate_rvalue_asm(d, c, output);
      return output.addElement(new DelocalizedReturnInstruction(s), 
                               output.last());
    }
    
    return prev; // nop-like
  } 
  
  /* Algebraic simplification optimization. */
  public void algebraic_simplify() {
    if (value != null)
      value = value.algebraic_simplify();
  }

  public String pretty_print_lowIR(int indent, boolean recursive) {
    boolean deja_vu = this.printed;
    String output = super.pretty_print_lowIR(indent, false);
    if (deja_vu) return output;
    
    for (int i = 0; i < indent; i++) output += " ";
    output += "   Value: " + value.desc() + "\n";
    
    for (int i = 0; i < indent; i++) output += " ";
    output += "---> " + next.get_label().get_name() + "\n";
    
    return output;
  }

  /* Walkable implementation. */
  public String node_name() {
    return "return";
  }
  public Enumeration neighbors() {
    if (value != null)
      return new ShortEnumeration(value);
    else
      return new ShortEnumeration();
  }
 
  public String pretty_print(int indent, boolean recursive) {
    String output = new String();
    for (int i = 0; i < indent; i++) output += " ";
    if (recursive && value != null) {
      output += "(return_instruction\n";
      output += value.pretty_print(indent + 2, true);
      for (int i = 0; i < indent; i++) output += " ";
      output += ") /* return_instruction */\n";
    } else {
      output += "(return_instruction)\n";
    }
    return output;
  }


  /* Traversable methods. */
  public void traverse(Traversal t) {
    t.traverse(next);
  }
}
