1 /*
   2  * Copyright (c) 2009, 2012, 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 jdk.vm.ci.code;
  24 
  25 import static jdk.vm.ci.code.ValueUtil.*;
  26 
  27 import jdk.vm.ci.meta.*;
  28 
  29 /**
  30  * A calling convention describes the locations in which the arguments for a call are placed and the
  31  * location in which the return value is placed if the call is not void.
  32  */
  33 public class CallingConvention {
  34 
  35     /**
  36      * Constants denoting the type of a call for which a calling convention is requested.
  37      */
  38     public enum Type {
  39         /**
  40          * A request for the outgoing argument locations at a call site to Java code.
  41          */
  42         JavaCall(true),
  43 
  44         /**
  45          * A request for the incoming argument locations.
  46          */
  47         JavaCallee(false),
  48 
  49         /**
  50          * A request for the outgoing argument locations at a call site to external native code that
  51          * complies with the platform ABI.
  52          */
  53         NativeCall(true);
  54 
  55         /**
  56          * Determines if this is a request for the outgoing argument locations at a call site.
  57          */
  58         public final boolean out;
  59 
  60         public static final Type[] VALUES = values();
  61 
  62         private Type(boolean out) {
  63             this.out = out;
  64         }
  65     }
  66 
  67     /**
  68      * The amount of stack space (in bytes) required for the stack-based arguments of the call.
  69      */
  70     private final int stackSize;
  71 
  72     private final AllocatableValue returnLocation;
  73 
  74     /**
  75      * The ordered locations in which the arguments are placed.
  76      */
  77     private final AllocatableValue[] argumentLocations;
  78 
  79     /**
  80      * Creates a description of the registers and stack locations used by a call.
  81      *
  82      * @param stackSize amount of stack space (in bytes) required for the stack-based arguments of
  83      *            the call
  84      * @param returnLocation the location for the return value or {@link Value#ILLEGAL} if a void
  85      *            call
  86      * @param argumentLocations the ordered locations in which the arguments are placed
  87      */
  88     public CallingConvention(int stackSize, AllocatableValue returnLocation, AllocatableValue... argumentLocations) {
  89         assert argumentLocations != null;
  90         assert returnLocation != null;
  91         this.argumentLocations = argumentLocations;
  92         this.stackSize = stackSize;
  93         this.returnLocation = returnLocation;
  94         assert verify();
  95     }
  96 
  97     /**
  98      * Gets the location for the return value or {@link Value#ILLEGAL} if a void call.
  99      */
 100     public AllocatableValue getReturn() {
 101         return returnLocation;
 102     }
 103 
 104     /**
 105      * Gets the location for the {@code index}'th argument.
 106      */
 107     public AllocatableValue getArgument(int index) {
 108         return argumentLocations[index];
 109     }
 110 
 111     /**
 112      * Gets the amount of stack space (in bytes) required for the stack-based arguments of the call.
 113      */
 114     public int getStackSize() {
 115         return stackSize;
 116     }
 117 
 118     /**
 119      * Gets the number of locations required for the arguments.
 120      */
 121     public int getArgumentCount() {
 122         return argumentLocations.length;
 123     }
 124 
 125     /**
 126      * Gets the locations required for the arguments.
 127      */
 128     public AllocatableValue[] getArguments() {
 129         if (argumentLocations.length == 0) {
 130             return argumentLocations;
 131         }
 132         return argumentLocations.clone();
 133     }
 134 
 135     @Override
 136     public String toString() {
 137         StringBuilder sb = new StringBuilder();
 138         sb.append("CallingConvention[");
 139         String sep = "";
 140         for (Value op : argumentLocations) {
 141             sb.append(sep).append(op);
 142             sep = ", ";
 143         }
 144         if (!returnLocation.equals(Value.ILLEGAL)) {
 145             sb.append(" -> ").append(returnLocation);
 146         }
 147         sb.append("]");
 148         return sb.toString();
 149     }
 150 
 151     private boolean verify() {
 152         for (int i = 0; i < argumentLocations.length; i++) {
 153             Value location = argumentLocations[i];
 154             assert isStackSlot(location) || isAllocatableValue(location);
 155         }
 156         return true;
 157     }
 158 }