Re: i++ in Java

Steven Carroll <scarroll@csrd.uiuc.edu>
5 Feb 2000 23:10:06 -0500

          From comp.compilers

Related articles
i++ in Java eddy@mip.sdu.dk (Eddy Truyen) (2000-02-05)
Re: i++ in Java scarroll@csrd.uiuc.edu (Steven Carroll) (2000-02-05)
Re: i++ in Java eddy@mip.sdu.dk (Eddy Truyen) (2000-02-10)
Re: i++ in Java eddy@mip.sdu.dk (Eddy Truyen) (2000-02-10)
| List of all articles for this month |

From: Steven Carroll <scarroll@csrd.uiuc.edu>
Newsgroups: comp.compilers
Date: 5 Feb 2000 23:10:06 -0500
Organization: University of Illinois at Urbana-Champaign
References: 00-02-021
Keywords: Java, code



Eddy Truyen wrote:


> Hi there,
>
> javac compiles statements like i++, i-- and similar to optimized byte
> code. This means that the resulting bytecodes are not the same when
> compiling i=i+1;
> Is their anyway to turn this off?
> I looked and at first glance their seems to be an option -O to optimize
> the compiler, but no option that leads to compiling only 'clean' code
> without dupx statements that insert elements within the Java stack




Dupx statements are used for more than just optimizations. say you
have the sequence:


new java/lang/String
dup
invokevirtual (some string function) // note invokevirtual takes the object
off the stack
invokevirtual (some other string function)


The only way I can think of to perform the same optimization is:


new java/lang/String
astore_0
aload_0
invokevirtual (Some string function)
aload_0
invokevirtual (some other string function)


I don't really think that's any cleaner, so I doubt there is an option
to do that. Java Bytecode is a stack based architecture, so naturally
the generated code is stack like and doesn't do all that loading and
storing that is common in load/store architectures.


As far as i++ goes, iinc (the optimized bytecode you refer to)is
pretty straightforward. You are correct though. I was surprised that
i = i + 1 is not translated to the same thing as i++. javac is not
known for its powerful optimization though.


STEVE


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.