Re: Possible contradiction between JLS and Sun's Java compiler



Oliver Wong wrote:

<quote>
a=b=c means a=(b=c), which assigns the value of c to b and then assigns
the value of b to a.
</quote>

[...]
However, a friend of mine said when they disassembled the code
produced by javac essentially says "read c; duplicate; write b; write a;"
which seems to contradict what the JLS claims.

The following code will not compile under JDK1.4.2 or 1.5.0.
==========
class Abc
{
static void
method()
{
double a;
float b;
double c = 27.3;

a = b = c;
}
}

==========

The compiler won't accept it without a cast of c to float. With the cast it
(1.4 and 1.5) produce the bytecode you'd expect (with converstions from double
to float and back again). Note, btw, that b takes up 1 stack slot while a and
c take up 2 each -- which makes it hard to see how the compiler could "get
away" with generating incorect code.

FWIW, the actual disassembly is:
ldc2_w 27.3
dstore_3
dload_3
d2f
dup
fstore_2
f2d
dstore_0
return

Perhaps your friend has a more complicated example ?

-- chris


.



Relevant Pages

  • Re: 12.34f vs (float) 12.34
    ... where cast followed by a literal parses the literal ... believe the compiler makes passes over the statement/expression trees it has ... generated, looking for certain patterns, and replacing them with ... float it's not guaranteed to be no more than a float. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C to Java Byte Code
    ... The actual topic is about creating a Java equivalent for a C program. ... as well have argued that a byte, int, long, float, double can all be ... The values of the compiler depend on the underlying hardware. ...
    (comp.programming)
  • Re: OT: C++ overloading operators
    ... way before complex numbers were part of the STL. ... I used the INLINE directive for these trivial functions. ... A compiler can not do inlining without access to the source code. ...
    (comp.dsp)
  • Re: Richard heathfields casting operation
    ... in this book there is a section named "How and why to cast" ... if you say 3.0 it is a type disambiguation,and the compiler ... If you need a float with the value 3.0f, just say 3.0f - there is no need ... I manually did the calculation for one value by using calculator ...
    (comp.lang.c)
  • Re: Newbie Questions: variable, operators
    ... Its type is float. ... you initialize a float variable with a double value (which ... There is an upper level of what compiler writers build into their ... I would not expect any compiler to actually recognize this optimization. ...
    (alt.comp.lang.learn.c-cpp)