0N/A/*
1472N/A * Copyright (c) 2002, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
0N/A#include <stdio.h>
0N/A#include <string.h>
0N/A
0N/Astatic const int R_O0_num = 1000;
0N/Astatic const int R_I0_num = 2000;
0N/Astatic const int R_F0_num = 3000;
0N/Astatic const int R_F1_num = R_F0_num + 1;
0N/Astatic const int R_F2_num = R_F0_num + 2;
0N/Astatic const int STACK_num= 4000;
0N/A
0N/Astatic bool LP64 = false;
0N/Astatic bool LONGS_IN_ONE_ENTRY = false;
0N/A
0N/Astatic const int Op_RegI = 'I';
0N/Astatic const int Op_RegP = 'P';
0N/Astatic const int Op_RegF = 'F';
0N/Astatic const int Op_RegD = 'D';
0N/Astatic const int Op_RegL = 'L';
0N/Astatic const int SPARC_ARGS_IN_REGS_NUM=6;
0N/A
0N/Astatic void print_reg( int reg ) {
0N/A if( reg == 0 )
0N/A printf("__"); // halve's
0N/A else if( reg >= STACK_num && reg < STACK_num+100 )
0N/A printf("S%d_",reg - STACK_num);
0N/A else if( reg >= R_F0_num && reg < R_F0_num+100 )
0N/A printf("F%d_",reg - R_F0_num);
0N/A else if( reg >= R_O0_num && reg < R_O0_num+100 ) {
0N/A if( LONGS_IN_ONE_ENTRY ) {
0N/A reg -= R_O0_num;
0N/A printf("O%d",reg>>1);
0N/A printf(reg&1 ? "H" : "L");
0N/A } else
0N/A printf("O%d_",reg - R_O0_num);
0N/A } else
0N/A printf("Wretched: %d\n", reg);
0N/A}
0N/A
0N/Astatic void print_convention( int *sig, const char *s, int length ) {
0N/A // Print it out
0N/A for( int i = 0; i < length; i++) {
0N/A if( sig[i] == 0 ) continue; // do not print 'halves'
0N/A print_reg( sig[i] & 0xFFFF );
0N/A int reg = sig[i] >> 16;
0N/A if( reg ) {
0N/A printf(":");
0N/A print_reg( reg );
0N/A } else {
0N/A printf(" ");
0N/A }
0N/A printf(" ");
0N/A }
0N/A printf("\n");
0N/A}
0N/A
0N/Astatic int INT_SCALE( int x ) {
0N/A return LONGS_IN_ONE_ENTRY ? (x<<1) : x;
0N/A}
0N/A
0N/Astatic void java_convention( int *sig, const char *s, int length ) {
0N/A if( LP64 && !LONGS_IN_ONE_ENTRY ) {
0N/A printf("LP64 and 2-reg longs not supported\n");
0N/A return;
0N/A }
0N/A for( int i = 0; i < length; i++ )
0N/A sig[i] = s[i]; // Reset sig array
0N/A bool is_outgoing = true;
0N/A
0N/A int int_base = (is_outgoing ? R_O0_num : R_I0_num);
0N/A
0N/A // Convention is to pack the first 6 int/oop args into the first 6
0N/A // registers (I0-I5), extras spill to the stack. Then pack the first
0N/A // 32 float args into F0-F31, extras spill to the stack. Then pad
0N/A // all register sets to align. Then put longs and doubles into the
0N/A // same registers as they fit, else spill to the stack.
0N/A int int_reg_max = SPARC_ARGS_IN_REGS_NUM;
0N/A int flt_reg_max = 32;
0N/A
0N/A // Count int/oop and float args. See how many stack slots we'll need
0N/A // and where the longs & doubles will go.
0N/A int int_reg_cnt = 0;
0N/A int flt_reg_cnt = 0;
0N/A int stk_reg_pairs = 0;
0N/A for( int i = 0; i < length; i++) {
0N/A switch( sig[i] ) {
0N/A case Op_RegL: // Longs-in-1-reg compete with int args
0N/A if( LONGS_IN_ONE_ENTRY ) {
0N/A if( int_reg_cnt < int_reg_max ) int_reg_cnt++;
0N/A }
0N/A break;
0N/A case Op_RegP:
0N/A if( int_reg_cnt < int_reg_max ) int_reg_cnt++;
0N/A else if( !LP64 ) stk_reg_pairs++;
0N/A break;
0N/A case Op_RegI:
0N/A if( int_reg_cnt < int_reg_max ) int_reg_cnt++;
0N/A else stk_reg_pairs++;
0N/A break;
0N/A case Op_RegF:
0N/A if( flt_reg_cnt < flt_reg_max ) flt_reg_cnt++;
0N/A else stk_reg_pairs++;
0N/A break;
0N/A }
0N/A }
0N/A
0N/A // This is where the longs/doubles start on the stack.
0N/A stk_reg_pairs = (stk_reg_pairs+1) & ~1; // Round
0N/A
0N/A int int_reg_pairs = (int_reg_cnt+1) & ~1; // 32-bit 2-reg longs only
0N/A int flt_reg_pairs = (flt_reg_cnt+1) & ~1;
0N/A
0N/A int stk_reg = 0;
0N/A int int_reg = 0;
0N/A int flt_reg = 0;
0N/A
0N/A // Now do the signature layout
0N/A for( int i = 0; i < length; i++) {
0N/A int tmp = sig[i];
0N/A if( tmp == Op_RegP )
0N/A tmp = LP64 ? Op_RegL : Op_RegI; // Treat ptrs and ints or long accordingly
0N/A switch( tmp ) {
0N/A case Op_RegI:
0N/A// case Op_RegP:
0N/A if( int_reg < int_reg_max) tmp = INT_SCALE(int_reg++) + int_base;
0N/A else tmp = STACK_num + stk_reg++;
0N/A sig[i] = tmp;
0N/A break;
0N/A
0N/A case Op_RegL:
0N/A if( sig[i] != Op_RegP && sig[i+1] != 'h' ) { printf("expecting (h)alf, found %c\n", sig[i+1]); return; }
0N/A// case Op_RegP:
0N/A if( LONGS_IN_ONE_ENTRY ) {
0N/A if( int_reg < int_reg_max ) {
0N/A tmp = INT_SCALE(int_reg++) + int_base;
0N/A } else {
0N/A tmp = STACK_num + stk_reg_pairs;
0N/A stk_reg_pairs += 2;
0N/A }
0N/A } else {
0N/A if( int_reg_pairs < int_reg_max ) {
0N/A tmp = int_reg_pairs + int_base;
0N/A int_reg_pairs += 2;
0N/A } else {
0N/A tmp = STACK_num + stk_reg_pairs;
0N/A stk_reg_pairs += 2;
0N/A }
0N/A }
0N/A sig[i] = tmp | (tmp+1)<<16; // Smear to pair
0N/A break;
0N/A
0N/A case Op_RegF:
0N/A sig[i] = (flt_reg < flt_reg_max) ? (R_F0_num + flt_reg++) : STACK_num + stk_reg++;
0N/A break;
0N/A case Op_RegD:
0N/A if( sig[i+1] != 'h' ) { printf("expecting (h)alf, found %c\n", sig[i+1]); return; }
0N/A if( flt_reg_pairs < flt_reg_max ) {
0N/A tmp = R_F0_num + flt_reg_pairs;
0N/A flt_reg_pairs += 2;
0N/A } else {
0N/A tmp = STACK_num + stk_reg_pairs;
0N/A stk_reg_pairs += 2;
0N/A }
0N/A sig[i] = tmp | (tmp+1)<<16; // Smear to pair
0N/A break;
0N/A case 'h': sig[i] = 0; break;
0N/A default:
0N/A printf("Bad character: %c\n", sig[i] );
0N/A return;
0N/A }
0N/A }
0N/A
0N/A printf("java ");
0N/A printf(LP64 ? "LP64 " : "LP32 ");
0N/A printf(LONGS_IN_ONE_ENTRY ? "long1: " : "long2: ");
0N/A print_convention(sig,s,length);
0N/A}
0N/A
0N/Astatic int int_stk_helper( int i ) {
0N/A if( i < 6 ) return R_O0_num + (LONGS_IN_ONE_ENTRY ? i<<1 : i);
0N/A else return STACK_num + (LP64 ? i<<1 : i);
0N/A}
0N/A
0N/Astatic void native_convention( int *sig, const char *s, int length ) {
0N/A if( LP64 && !LONGS_IN_ONE_ENTRY ) {
0N/A printf("LP64 and 2-reg longs not supported\n");
0N/A return;
0N/A }
0N/A for( int i = 0; i < length; i++ )
0N/A sig[i] = s[i]; // Reset sig array
0N/A
0N/A // The native convention is V8 if !LP64, which means the V8 convention is
0N/A // used both with and without LONGS_IN_ONE_ENTRY, an unfortunate split. The
0N/A // same actual machine registers are used, but they are named differently in
0N/A // the LONGS_IN_ONE_ENTRY mode. The LP64 convention is the V9 convention
0N/A // which is slightly more sane.
0N/A
0N/A if( LP64 ) {
0N/A // V9 convention: All things "as-if" on double-wide stack slots.
0N/A // Hoist any int/ptr/long's in the first 6 to int regs.
0N/A // Hoist any flt/dbl's in the first 16 dbl regs.
0N/A int j = 0; // Count of actual args, not HALVES
0N/A for( int i=0; i<length; i++, j++ ) {
0N/A int tmp;
0N/A switch( sig[i] ) {
0N/A case Op_RegI:
0N/A sig[i] = int_stk_helper( j );
0N/A break;
0N/A case Op_RegL:
0N/A if( sig[i+1] != 'h' ) { printf("expecting (h)alf, found %c\n", sig[i+1]); return; }
0N/A case Op_RegP:
0N/A tmp = int_stk_helper( j );
0N/A sig[i] = tmp | ((tmp+1) << 16); // Smear to pair
0N/A break;
0N/A case Op_RegF: // V9ism: floats go in ODD registers
0N/A sig[i] = ((j < 16) ? R_F1_num : (STACK_num + 1)) + (j<<1);
0N/A break;
0N/A case Op_RegD: // V9ism: doubles go in EVEN/ODD regs
0N/A tmp = ((j < 16) ? R_F0_num : STACK_num) + (j<<1);
0N/A sig[i] = tmp | ((tmp+1) << 16); // Smear to pair
0N/A break;
0N/A case 'h': sig[i] = 0; j--; break; // Do not count HALVES
0N/A default:
0N/A printf("Bad character: %c\n", sig[i] );
0N/A return;
0N/A }
0N/A }
0N/A
0N/A } else {
0N/A // V8 convention: first 6 things in O-regs, rest on stack.
0N/A // Alignment is willy-nilly.
0N/A for( int i=0; i<length; i++ ) {
0N/A int tmp;
0N/A switch( sig[i] ) {
0N/A case Op_RegI:
0N/A case Op_RegP:
0N/A case Op_RegF:
0N/A sig[i] = int_stk_helper( i );
0N/A break;
0N/A case Op_RegL:
0N/A case Op_RegD:
0N/A if( sig[i+1] != 'h' ) { printf("expecting (h)alf, found %c\n", sig[i+1]); return; }
0N/A tmp = int_stk_helper( i );
0N/A sig[i] = tmp | (int_stk_helper( i+1 ) << 16);
0N/A break;
0N/A case 'h': sig[i] = 0; break;
0N/A default:
0N/A printf("Bad character: %c\n", sig[i] );
0N/A return;
0N/A }
0N/A }
0N/A }
0N/A
0N/A printf("natv ");
0N/A printf(LP64 ? "LP64 " : "LP32 ");
0N/A printf(LONGS_IN_ONE_ENTRY ? "long1: " : "long2: ");
0N/A print_convention(sig,s,length);
0N/A}
0N/A
0N/Aint main( int argc, char **argv ) {
0N/A
0N/A if( argc != 2 ) {
0N/A printf("Usage: args IPFLhDh... (Java argument string)\n");
0N/A printf("Returns argument layout\n");
0N/A return -1;
0N/A }
0N/A
0N/A const char *s = argv[1];
0N/A int length = strlen(s);
0N/A int sig[1000];
0N/A
0N/A LP64 = false; LONGS_IN_ONE_ENTRY = false;
0N/A java_convention( sig, s, length );
0N/A LP64 = false; LONGS_IN_ONE_ENTRY = true;
0N/A java_convention( sig, s, length );
0N/A LP64 = true ; LONGS_IN_ONE_ENTRY = true;
0N/A java_convention( sig, s, length );
0N/A
0N/A LP64 = false; LONGS_IN_ONE_ENTRY = false;
0N/A native_convention( sig, s, length );
0N/A LP64 = false; LONGS_IN_ONE_ENTRY = true;
0N/A native_convention( sig, s, length );
0N/A LP64 = true ; LONGS_IN_ONE_ENTRY = true;
0N/A native_convention( sig, s, length );
0N/A}
0N/A