/*
 * Copyright (c) 2002,2006 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 */
package com.ibm.wala.shrike.shrikeBT;

import java.util.Arrays;

/** This class represents unary operators where the result is the same type as the operand. */
public final class UnaryOpInstruction extends Instruction implements IUnaryOpInstruction {
  private UnaryOpInstruction(short opcode) {
    super(opcode);
  }

  private static final UnaryOpInstruction[] preallocated = preallocate();

  private static UnaryOpInstruction[] preallocate() {
    UnaryOpInstruction[] r = new UnaryOpInstruction[OP_dneg - OP_ineg + 1];
    Arrays.setAll(r, i -> new UnaryOpInstruction((short) (OP_ineg + i)));
    return r;
  }

  public static UnaryOpInstruction make(String type) throws IllegalArgumentException {
    int t = Util.getTypeIndex(type);
    if (t < 0 || t > TYPE_double_index) {
      throw new IllegalArgumentException("Type " + type + " cannot have a unary operator applied");
    }
    return preallocated[t];
  }

  @Override
  public boolean equals(Object o) {
    if (o instanceof UnaryOpInstruction) {
      UnaryOpInstruction i = (UnaryOpInstruction) o;
      return i.opcode == opcode;
    } else {
      return false;
    }
  }

  @Override
  public IUnaryOpInstruction.Operator getOperator() {
    return IUnaryOpInstruction.Operator.NEG;
  }

  @Override
  public int hashCode() {
    return opcode;
  }

  @Override
  public int getPoppedCount() {
    return 1;
  }

  @Override
  public String getPushedType(String[] types) {
    return getType();
  }

  @Override
  public byte getPushedWordSize() {
    return Util.getWordSize(getType());
  }

  @Override
  public String getType() {
    return indexedTypes[opcode - OP_ineg];
  }

  @Override
  public void visit(IInstruction.Visitor v) throws NullPointerException {
    v.visitUnaryOp(this);
  }

  @Override
  public String toString() {
    return "UnaryOp(" + getType() + ',' + getOperator() + ')';
  }

  @Override
  public boolean isPEI() {
    return false;
  }
}
