src/cpu/sparc/vm/args.cc

changeset 435
a61af66fc99e
child 1907
c18cbe5936b8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/cpu/sparc/vm/args.cc	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,309 @@
     1.4 +/*
     1.5 + * Copyright 2002-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *  
    1.26 + */
    1.27 +
    1.28 +#include <stdio.h>
    1.29 +#include <string.h>
    1.30 +
    1.31 +static const int R_O0_num = 1000;
    1.32 +static const int R_I0_num = 2000;
    1.33 +static const int R_F0_num = 3000;
    1.34 +static const int R_F1_num = R_F0_num + 1;
    1.35 +static const int R_F2_num = R_F0_num + 2;
    1.36 +static const int STACK_num= 4000;
    1.37 +
    1.38 +static bool LP64 = false;
    1.39 +static bool LONGS_IN_ONE_ENTRY = false;
    1.40 +
    1.41 +static const int Op_RegI = 'I';
    1.42 +static const int Op_RegP = 'P';
    1.43 +static const int Op_RegF = 'F';
    1.44 +static const int Op_RegD = 'D';
    1.45 +static const int Op_RegL = 'L';
    1.46 +static const int SPARC_ARGS_IN_REGS_NUM=6;
    1.47 +
    1.48 +static void print_reg( int reg ) {
    1.49 +  if( reg == 0 )
    1.50 +    printf("__");               // halve's
    1.51 +  else if( reg >= STACK_num && reg < STACK_num+100 )
    1.52 +    printf("S%d_",reg - STACK_num);
    1.53 +  else if( reg >= R_F0_num && reg < R_F0_num+100 )
    1.54 +    printf("F%d_",reg - R_F0_num);
    1.55 +  else if( reg >= R_O0_num && reg < R_O0_num+100 ) {
    1.56 +    if( LONGS_IN_ONE_ENTRY ) {
    1.57 +      reg -= R_O0_num;
    1.58 +      printf("O%d",reg>>1);
    1.59 +      printf(reg&1 ? "H" : "L");
    1.60 +    } else
    1.61 +      printf("O%d_",reg - R_O0_num);
    1.62 +  } else
    1.63 +    printf("Wretched: %d\n", reg);
    1.64 +}
    1.65 +
    1.66 +static void print_convention( int *sig, const char *s, int length ) {
    1.67 +  // Print it out
    1.68 +  for( int i = 0; i < length; i++) {
    1.69 +    if( sig[i] == 0 ) continue; // do not print 'halves'
    1.70 +    print_reg( sig[i] & 0xFFFF );
    1.71 +    int reg = sig[i] >> 16;
    1.72 +    if( reg ) {
    1.73 +      printf(":");
    1.74 +      print_reg( reg );
    1.75 +    } else {
    1.76 +      printf("    ");
    1.77 +    }
    1.78 +    printf("  ");
    1.79 +  }
    1.80 +  printf("\n");
    1.81 +}
    1.82 +
    1.83 +static int INT_SCALE( int x ) {
    1.84 +  return LONGS_IN_ONE_ENTRY ? (x<<1) : x;
    1.85 +}
    1.86 +
    1.87 +static void java_convention( int *sig, const char *s, int length ) {
    1.88 +  if( LP64 && !LONGS_IN_ONE_ENTRY ) {
    1.89 +    printf("LP64 and 2-reg longs not supported\n");
    1.90 +    return;
    1.91 +  }
    1.92 +  for( int i = 0; i < length; i++ )
    1.93 +    sig[i] = s[i];              // Reset sig array
    1.94 +  bool is_outgoing = true;
    1.95 +
    1.96 +  int int_base = (is_outgoing ? R_O0_num : R_I0_num);
    1.97 +
    1.98 +  // Convention is to pack the first 6 int/oop args into the first 6
    1.99 +  // registers (I0-I5), extras spill to the stack.  Then pack the first
   1.100 +  // 32 float args into F0-F31, extras spill to the stack.  Then pad
   1.101 +  // all register sets to align.  Then put longs and doubles into the
   1.102 +  // same registers as they fit, else spill to the stack.
   1.103 +  int int_reg_max = SPARC_ARGS_IN_REGS_NUM;
   1.104 +  int flt_reg_max = 32;
   1.105 +  
   1.106 +  // Count int/oop and float args.  See how many stack slots we'll need
   1.107 +  // and where the longs & doubles will go.
   1.108 +  int int_reg_cnt   = 0;
   1.109 +  int flt_reg_cnt   = 0;
   1.110 +  int stk_reg_pairs = 0;
   1.111 +  for( int i = 0; i < length; i++) {
   1.112 +    switch( sig[i] ) {
   1.113 +    case Op_RegL:               // Longs-in-1-reg compete with int args
   1.114 +      if( LONGS_IN_ONE_ENTRY ) { 
   1.115 +        if( int_reg_cnt < int_reg_max ) int_reg_cnt++;
   1.116 +      }
   1.117 +      break;
   1.118 +    case Op_RegP:
   1.119 +      if( int_reg_cnt < int_reg_max ) int_reg_cnt++;
   1.120 +      else if( !LP64 )                stk_reg_pairs++;
   1.121 +      break;
   1.122 +    case Op_RegI:
   1.123 +      if( int_reg_cnt < int_reg_max ) int_reg_cnt++;
   1.124 +      else                            stk_reg_pairs++;
   1.125 +      break;
   1.126 +    case Op_RegF:
   1.127 +      if( flt_reg_cnt < flt_reg_max ) flt_reg_cnt++;
   1.128 +      else                            stk_reg_pairs++;
   1.129 +      break;
   1.130 +    }
   1.131 +  }
   1.132 +
   1.133 +  // This is where the longs/doubles start on the stack.
   1.134 +  stk_reg_pairs = (stk_reg_pairs+1) & ~1; // Round
   1.135 +  
   1.136 +  int int_reg_pairs = (int_reg_cnt+1) & ~1; // 32-bit 2-reg longs only
   1.137 +  int flt_reg_pairs = (flt_reg_cnt+1) & ~1;
   1.138 +
   1.139 +  int stk_reg = 0;
   1.140 +  int int_reg = 0;
   1.141 +  int flt_reg = 0;
   1.142 +  
   1.143 +  // Now do the signature layout
   1.144 +  for( int i = 0; i < length; i++) {
   1.145 +    int tmp = sig[i];
   1.146 +    if( tmp == Op_RegP )
   1.147 +      tmp = LP64 ? Op_RegL : Op_RegI;   // Treat ptrs and ints or long accordingly
   1.148 +    switch( tmp ) {
   1.149 +    case Op_RegI:
   1.150 +//  case Op_RegP: 
   1.151 +      if( int_reg < int_reg_max) tmp = INT_SCALE(int_reg++) + int_base;
   1.152 +      else                       tmp = STACK_num + stk_reg++;
   1.153 +      sig[i] = tmp;
   1.154 +      break;
   1.155 +
   1.156 +    case Op_RegL: 
   1.157 +      if( sig[i] != Op_RegP && sig[i+1] != 'h' ) { printf("expecting (h)alf, found %c\n", sig[i+1]); return; }
   1.158 +//  case Op_RegP: 
   1.159 +      if( LONGS_IN_ONE_ENTRY ) {
   1.160 +        if( int_reg < int_reg_max ) {
   1.161 +          tmp = INT_SCALE(int_reg++) + int_base;
   1.162 +        } else {
   1.163 +          tmp = STACK_num + stk_reg_pairs;
   1.164 +          stk_reg_pairs += 2;
   1.165 +        }
   1.166 +      } else {
   1.167 +        if( int_reg_pairs < int_reg_max ) {
   1.168 +          tmp = int_reg_pairs + int_base;
   1.169 +          int_reg_pairs += 2;
   1.170 +        } else {
   1.171 +          tmp = STACK_num + stk_reg_pairs;
   1.172 +          stk_reg_pairs += 2;
   1.173 +        }
   1.174 +      }
   1.175 +      sig[i] = tmp | (tmp+1)<<16; // Smear to pair
   1.176 +      break;
   1.177 +
   1.178 +    case Op_RegF: 
   1.179 +      sig[i] = (flt_reg < flt_reg_max) ? (R_F0_num + flt_reg++) : STACK_num + stk_reg++;
   1.180 +      break;
   1.181 +    case Op_RegD: 
   1.182 +      if( sig[i+1] != 'h' ) { printf("expecting (h)alf, found %c\n", sig[i+1]); return; }
   1.183 +      if( flt_reg_pairs < flt_reg_max ) {
   1.184 +        tmp = R_F0_num + flt_reg_pairs;
   1.185 +        flt_reg_pairs += 2;
   1.186 +      } else {
   1.187 +        tmp = STACK_num + stk_reg_pairs;
   1.188 +        stk_reg_pairs += 2;
   1.189 +      }
   1.190 +      sig[i] = tmp | (tmp+1)<<16; // Smear to pair
   1.191 +      break;
   1.192 +    case 'h': sig[i] = 0; break;
   1.193 +    default:
   1.194 +      printf("Bad character: %c\n", sig[i] );
   1.195 +      return;
   1.196 +    }
   1.197 +  }
   1.198 +
   1.199 +  printf("java ");
   1.200 +  printf(LP64 ? "LP64 " : "LP32 ");
   1.201 +  printf(LONGS_IN_ONE_ENTRY ? "long1: " : "long2: ");
   1.202 +  print_convention(sig,s,length);
   1.203 +}
   1.204 +
   1.205 +static int int_stk_helper( int i ) {
   1.206 +  if( i < 6 ) return R_O0_num + (LONGS_IN_ONE_ENTRY ? i<<1 : i);
   1.207 +  else        return STACK_num + (LP64              ? i<<1 : i);
   1.208 +}
   1.209 +
   1.210 +static void native_convention( int *sig, const char *s, int length ) {
   1.211 +  if( LP64 && !LONGS_IN_ONE_ENTRY ) {
   1.212 +    printf("LP64 and 2-reg longs not supported\n");
   1.213 +    return;
   1.214 +  }
   1.215 +  for( int i = 0; i < length; i++ )
   1.216 +    sig[i] = s[i];              // Reset sig array
   1.217 +
   1.218 +  // The native convention is V8 if !LP64, which means the V8 convention is
   1.219 +  // used both with and without LONGS_IN_ONE_ENTRY, an unfortunate split.  The
   1.220 +  // same actual machine registers are used, but they are named differently in
   1.221 +  // the LONGS_IN_ONE_ENTRY mode.  The LP64 convention is the V9 convention
   1.222 +  // which is slightly more sane.
   1.223 +  
   1.224 +  if( LP64 ) {
   1.225 +    // V9 convention: All things "as-if" on double-wide stack slots.
   1.226 +    // Hoist any int/ptr/long's in the first 6 to int regs.
   1.227 +    // Hoist any flt/dbl's in the first 16 dbl regs.
   1.228 +    int j = 0;                  // Count of actual args, not HALVES
   1.229 +    for( int i=0; i<length; i++, j++ ) {
   1.230 +      int tmp;
   1.231 +      switch( sig[i] ) {
   1.232 +      case Op_RegI:
   1.233 +        sig[i] = int_stk_helper( j );
   1.234 +        break;
   1.235 +      case Op_RegL:
   1.236 +        if( sig[i+1] != 'h' ) { printf("expecting (h)alf, found %c\n", sig[i+1]); return; }
   1.237 +      case Op_RegP:
   1.238 +        tmp = int_stk_helper( j );
   1.239 +        sig[i] = tmp | ((tmp+1) << 16); // Smear to pair
   1.240 +        break;
   1.241 +      case Op_RegF:                 // V9ism: floats go in ODD registers
   1.242 +        sig[i] = ((j < 16) ? R_F1_num : (STACK_num + 1)) + (j<<1);
   1.243 +        break;
   1.244 +      case Op_RegD:                 // V9ism: doubles go in EVEN/ODD regs
   1.245 +        tmp    = ((j < 16) ? R_F0_num : STACK_num) + (j<<1);
   1.246 +        sig[i] = tmp | ((tmp+1) << 16); // Smear to pair
   1.247 +        break;
   1.248 +      case 'h': sig[i] = 0; j--; break; // Do not count HALVES
   1.249 +      default:
   1.250 +        printf("Bad character: %c\n", sig[i] );
   1.251 +        return;
   1.252 +      }
   1.253 +    }
   1.254 +
   1.255 +  } else {
   1.256 +    // V8 convention: first 6 things in O-regs, rest on stack.
   1.257 +    // Alignment is willy-nilly.
   1.258 +    for( int i=0; i<length; i++ ) {
   1.259 +      int tmp;
   1.260 +      switch( sig[i] ) {
   1.261 +      case Op_RegI:
   1.262 +      case Op_RegP:
   1.263 +      case Op_RegF:
   1.264 +        sig[i] = int_stk_helper( i );
   1.265 +        break;
   1.266 +      case Op_RegL:
   1.267 +      case Op_RegD:
   1.268 +        if( sig[i+1] != 'h' ) { printf("expecting (h)alf, found %c\n", sig[i+1]); return; }
   1.269 +        tmp = int_stk_helper( i );
   1.270 +        sig[i] = tmp | (int_stk_helper( i+1 ) << 16);
   1.271 +        break;
   1.272 +      case 'h': sig[i] = 0; break;
   1.273 +      default:
   1.274 +        printf("Bad character: %c\n", sig[i] );
   1.275 +        return;
   1.276 +      }
   1.277 +    }
   1.278 +  }
   1.279 +
   1.280 +  printf("natv ");
   1.281 +  printf(LP64 ? "LP64 " : "LP32 ");
   1.282 +  printf(LONGS_IN_ONE_ENTRY ? "long1: " : "long2: ");
   1.283 +  print_convention(sig,s,length);
   1.284 +}
   1.285 +
   1.286 +int main( int argc, char **argv ) {
   1.287 +
   1.288 +  if( argc != 2 ) {
   1.289 +    printf("Usage: args IPFLhDh... (Java argument string)\n");
   1.290 +    printf("Returns argument layout\n");
   1.291 +    return -1;
   1.292 +  }
   1.293 +
   1.294 +  const char *s = argv[1];
   1.295 +  int length = strlen(s);
   1.296 +  int sig[1000];
   1.297 +
   1.298 +  LP64 = false; LONGS_IN_ONE_ENTRY = false;
   1.299 +  java_convention( sig, s, length );
   1.300 +  LP64 = false; LONGS_IN_ONE_ENTRY = true;
   1.301 +  java_convention( sig, s, length );
   1.302 +  LP64 = true ; LONGS_IN_ONE_ENTRY = true;
   1.303 +  java_convention( sig, s, length );
   1.304 +
   1.305 +  LP64 = false; LONGS_IN_ONE_ENTRY = false;
   1.306 +  native_convention( sig, s, length );
   1.307 +  LP64 = false; LONGS_IN_ONE_ENTRY = true;
   1.308 +  native_convention( sig, s, length );
   1.309 +  LP64 = true ; LONGS_IN_ONE_ENTRY = true;
   1.310 +  native_convention( sig, s, length );
   1.311 +}
   1.312 +

mercurial