1 /*
   2  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.lir;
  24 
  25 import java.lang.annotation.ElementType;
  26 import java.lang.annotation.Retention;
  27 import java.lang.annotation.RetentionPolicy;
  28 import java.lang.annotation.Target;
  29 import java.util.EnumSet;
  30 
  31 import org.graalvm.compiler.lir.LIRInstruction.OperandFlag;
  32 import org.graalvm.compiler.lir.LIRInstruction.OperandMode;
  33 
  34 import jdk.vm.ci.meta.Value;
  35 import jdk.vm.ci.meta.ValueKind;
  36 
  37 /**
  38  * Base class to represent values that need to be stored in more than one register. This is mainly
  39  * intended to support addresses and not general arbitrary nesting of composite values. Because of
  40  * the possibility of sharing of CompositeValues they should be immutable.
  41  */
  42 public abstract class CompositeValue extends Value {
  43 
  44     @Retention(RetentionPolicy.RUNTIME)
  45     @Target(ElementType.FIELD)
  46     public static @interface Component {
  47 
  48         OperandFlag[] value() default OperandFlag.REG;
  49     }
  50 
  51     public CompositeValue(ValueKind<?> kind) {
  52         super(kind);
  53         assert CompositeValueClass.get(getClass()) != null;
  54     }
  55 
  56     /**
  57      * Invoke {@code proc} on each {@link Value} element of this {@link CompositeValue}. If
  58      * {@code proc} replaces any value then a new CompositeValue should be returned.
  59      *
  60      * @param inst
  61      * @param mode
  62      * @param proc
  63      * @return the original CompositeValue or a copy with any modified values
  64      */
  65     public abstract CompositeValue forEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueProcedure proc);
  66 
  67     /**
  68      * A helper method to visit {@link Value}[] ensuring that a copy of the array is made if it's
  69      * needed.
  70      *
  71      * @param inst
  72      * @param values
  73      * @param mode
  74      * @param proc
  75      * @param flags
  76      * @return the original {@code values} array or a copy if values changed
  77      */
  78     protected Value[] visitValueArray(LIRInstruction inst, Value[] values, OperandMode mode, InstructionValueProcedure proc, EnumSet<OperandFlag> flags) {
  79         Value[] newValues = null;
  80         for (int i = 0; i < values.length; i++) {
  81             Value value = values[i];
  82             Value newValue = proc.doValue(inst, value, mode, flags);
  83             if (!value.identityEquals(newValue)) {
  84                 if (newValues == null) {
  85                     newValues = values.clone();
  86                 }
  87                 newValues[i] = value;
  88             }
  89         }
  90         return newValues != null ? newValues : values;
  91     }
  92 
  93     protected abstract void visitEachComponent(LIRInstruction inst, OperandMode mode, InstructionValueConsumer proc);
  94 
  95     @Override
  96     public String toString() {
  97         return CompositeValueClass.format(this);
  98     }
  99 
 100     @Override
 101     public int hashCode() {
 102         return 53 * super.hashCode();
 103     }
 104 
 105     @Override
 106     public boolean equals(Object obj) {
 107         if (obj instanceof CompositeValue) {
 108             CompositeValue other = (CompositeValue) obj;
 109             return super.equals(other);
 110         }
 111         return false;
 112     }
 113 }