Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
* Function parameter weirdness
@ 1999-10-24  3:03 Dan Morrill
  1999-10-24 15:03 ` Uncle George
  0 siblings, 1 reply; 6+ messages in thread
From: Dan Morrill @ 1999-10-24  3:03 UTC (permalink / raw)
  To: axp-list

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Okay, here's a weird one for the gurii.  I am attempting to get the JavaComm
package working with UG's JDK 1.1.8.  The JavaCOMM API requires that you have
a native JNI driver for your platform;  the one for Linux is called
RXTX (at http://www.frii.com/~jarvi/rxtx/ ).

In one of the RXTX source files is the following rather simple function;
basically it maps integers to constants from a header file.  (The stuff in
/* DEBUG CODE */ ... /* END DEBUG CODE */ is my stuff, and is not in the
original source.)

**********************************
int translate_speed( JNIEnv *env, jint speed )
{
/* DEBUG CODE */
fprintf( stderr, "speed: %d\n", speed );
if (speed == 9600) {
    fprintf( stderr, "hit\n" );
}
fprintf( stderr, "b-and: %d\n", (9600 & speed) );
fprintf( stderr, "b-or: %d\n", (9600 | speed) );
/* END DEBUG CODE */
	switch( speed ) {
		case 0:		return B0;
		case 50:		return B50;
		case 75:		return B75;
		case 110:	return B110;
		case 134:	return B134;
		case 150:	return B150;
		case 200:	return B200;
		case 300:	return B300;
		case 600:	return B600;
		case 1200:	return B1200;
		case 1800:	return B1800;
		case 2400:	return B2400;
		case 4800:	return B4800;
		case 9600:	return B9600;
		case 19200:	return B19200;
		case 38400:	return B38400;
		case 57600:	return B57600;
		case 115200:	return B115200;
		case 230400:	return B230400;
#ifdef B460800
		case 460800:	return B460800;
#endif
/* DEBUG CODE */
		default:	fprintf( stderr, "wtf? %d\n", speed );
/* END DEBUG CODE */
	}

/* DEBUG CODE */
fprintf( stderr, "speed2: %d\n", speed );
/* END DEBUG CODE */
	throw_java_exception( env, UNSUPPORTED_COMM_OPERATION,
		"translate_speed", "speed" );
	return 0;
}
**********************************


"jint" is typedef'ed to "int" upstream in the JNI headers.  If you run one of
the sample test programs provided with the JavaComm package and tell it to
set the baud rate to 9600, you get the following output from my debug
statements:



speed: 9600
b-and: 9600
b-or: 9600
wtf? 9600
speed2: 9600 


So:  speed is a 32-bit int with the value of 9600, and is binary identical
to the constant 9600 (as shown by the & and | ).  However, speed != 9600
(since it fails both the switch and my if statement.)

Eh?


I don't know whether to blame some the compiler (egcs 1.1.2), glibc (2.1.1),
or JNI, or even myself (if this is normal.)

It gets better though.  If I change the switch (speed) to:

switch( speed | speed ) {
...
}

...it still fails to work.  If I do this, however:

fprintf( stderr, "%d", speed );
switch( speed | speed ) {
...
}

...it works!

Anyone have any ideas?  This has got me mightily flummoxed.


My machine is an SX164; kernel 2.2.12.  More info available upon request.


Thanks,

Dan Morrill
Computer Scientist, Physicist
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.0 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE4Enb6U6LPdJTKfTwRAid/AJ95OZM2CERcA7nGs+oH+EoRK/2cCgCfWYoe
liTmmZIDbcrrTnR/n7NM0oo=
=mi7/
-----END PGP SIGNATURE-----


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function parameter weirdness
  1999-10-24  3:03 Function parameter weirdness Dan Morrill
@ 1999-10-24 15:03 ` Uncle George
  1999-10-24 20:18   ` Dan Morrill
  0 siblings, 1 reply; 6+ messages in thread
From: Uncle George @ 1999-10-24 15:03 UTC (permalink / raw)
  To: axp-list, Dan Morrill

Dear flummoxed:
    What u have caught, as it has caught many of the unwary, is the methods of
maddness employed in passing parameters via register on the 64bit alpha.

    What u see in your int compare is only what u believe is happening. In
reality there is a long compare happening ( there is no int compare as part of
the hardware instruction set ).  At the time of compare, the subroutine presumes
that the Hi-Order Word ( 32bit ) is properly sign extended ( +=0fill, -=
0xffffffff filled ) BEFOR-HAND . So if u look at the register ( with gdb ) u
will see ur 9600 in the right half, but something else in the left half - junk.
One might presume that since the variable is 32 bits, that only the right half
will be used - BUT THIS AINT JUST SO.  this is especially so in compares.

This sorta makes the case for getting ur parameter casts properly done.

And I'm sure there is gonna be a question of the JNI for java/alpha. The JNI for
JDK1.2 got a whole lot better at figuring out what to do with parameters &
return values. But then that is what SUN designed into their assembler/JNI
interface, and I had little choice but to follow. The JNI interface for jdk118
was a lot simpler, more stupid, and not at all intelligent about what gets put
in the unused bits of the register.. :-\

Shazzbott, where's an orkin when u need one.



.Dan Morrill wrote:

>
>
> Okay, here's a weird one for the gurii.  I am attempting to get the JavaComm
> package working with UG's JDK 1.1.8.  The JavaCOMM API requires that you have
> a native JNI driver for your platform;  the one for Linux is called
> RXTX (at http://www.frii.com/~jarvi/rxtx/ ).
>
>
> Anyone have any ideas?  This has got me mightily flummoxed.
>
> My machine is an SX164; kernel 2.2.12.  More info available upon request.
>
>



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function parameter weirdness
  1999-10-24 15:03 ` Uncle George
@ 1999-10-24 20:18   ` Dan Morrill
  1999-10-25 10:19     ` Uncle George
  0 siblings, 1 reply; 6+ messages in thread
From: Dan Morrill @ 1999-10-24 20:18 UTC (permalink / raw)
  To: Uncle George; +Cc: axp-list

On Sun, 24 Oct 1999, Uncle George wrote:

>     What u have caught, as it has caught many of the unwary, is the methods of
> maddness employed in passing parameters via register on the 64bit alpha.

Hmm;  I see.  Thanks for the tip -- I'll poke around and see what I can come
up with.

I even thought that might be the problem, but I assumed explicit casts would
take care of it.  "If you assume, you make an Ass of out You and... uh...
some other guy." :)


Dan Morrill
Computer Scientist, Physicist


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function parameter weirdness
  1999-10-24 20:18   ` Dan Morrill
@ 1999-10-25 10:19     ` Uncle George
  1999-10-28 23:38       ` Richard Henderson
  0 siblings, 1 reply; 6+ messages in thread
From: Uncle George @ 1999-10-25 10:19 UTC (permalink / raw)
  To: axp-list, Dan Morrill

yup, but it sppears that the 'sender' of the parameter has to know what type it is
to get this picture correct. It was the same with postgresql & their JNI like
interface. The caller had all the parameters set to be the longest dataum ( 64bit
long ). The callee, had their parameter lists setup according to the sizeof the
variable used ( ie char, short, int, long ). proper action would only be taken if
both caller/callee passed/received longs in the parameter lists, AND then typecast
their usage.
gat

Dan Morrill wrote:

>
> I even thought that might be the problem, but I assumed explicit casts would
> take care of it.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function parameter weirdness
  1999-10-25 10:19     ` Uncle George
@ 1999-10-28 23:38       ` Richard Henderson
  1999-10-29 10:28         ` Uncle George
  0 siblings, 1 reply; 6+ messages in thread
From: Richard Henderson @ 1999-10-28 23:38 UTC (permalink / raw)
  To: axp-list; +Cc: Dan Morrill

On Mon, Oct 25, 1999 at 06:19:13AM -0400, Uncle George wrote:
> yup, but it sppears that the 'sender' of the parameter has to know what
> type it is to get this picture correct. 

Kindof.  The caller is supposed to sign-extend all 32-bit integers.
Note that this is without respect to whether the variable is declared
as `unsigned int' in the source.

Values smaller than `int' are promoted by K&R rules to `int', so eg
`unsigned short' winds up zero extended since bit 31 is never set.


r~


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Function parameter weirdness
  1999-10-28 23:38       ` Richard Henderson
@ 1999-10-29 10:28         ` Uncle George
  0 siblings, 0 replies; 6+ messages in thread
From: Uncle George @ 1999-10-29 10:28 UTC (permalink / raw)
  To: axp-list; +Cc: Dan Morrill, Richard Henderson

Ok,
    "sign extend all 32bit integers"  ( I dont think this is in k&r ) . what
of Sign extening "short"s? "char"s ? by the caller.

    But all this is related to the fact the the 'C" compiler really doesn't
know what the function parameters are at time of compile/link binding. they
are only known at runtime ( this is true for java/JNI, but not for
postgreSQL )  There is just one ( JNI ) interface to all of the
subroutines.  And apparently it, as well as the guy who writes the JNI
interface needs to know the 'tricks' of this machine's trade.


SOoooo By ur example:

        unsigned short a = 0xffff;
        call foo( a );
..............

        foo( short a )
            if ( a == -1 ) printf("Got a neg one");
            else                printf("Got bogus value");

In the land of the i386 i'd guess u'd "got a neg one", according to u u
would get a 65536 ( or was that 65535 ) value  Even though the # & pattern
of bits are =, the alpha will do a 64 bit compare, whereas ( i suspect ) the
i386 will do a (expected) 16bit ( signed ) compare.

Richard Henderson wrote:

> On Mon, Oct 25, 1999 at 06:19:13AM -0400, Uncle George wrote:
> > yup, but it sppears that the 'sender' of the parameter has to know what
> > type it is to get this picture correct.
>
> Kindof.  The caller is supposed to sign-extend all 32-bit integers.
> Note that this is without respect to whether the variable is declared
> as `unsigned int' in the source.
>
> Values smaller than `int' are promoted by K&R rules to `int', so eg
> `unsigned short' winds up zero extended since bit 31 is never set.
>



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1999-10-29 10:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-10-24  3:03 Function parameter weirdness Dan Morrill
1999-10-24 15:03 ` Uncle George
1999-10-24 20:18   ` Dan Morrill
1999-10-25 10:19     ` Uncle George
1999-10-28 23:38       ` Richard Henderson
1999-10-29 10:28         ` Uncle George

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox