aspose file tools*
The moose likes Java in General and the fly likes java jvm bytecode generation question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "java jvm bytecode generation question" Watch "java jvm bytecode generation question" New topic
Author

java jvm bytecode generation question

Rashad Alik
Greenhorn

Joined: Jun 22, 2012
Posts: 2
Hi people,

This may be a bit silly question, since I don't have much background in compiles, but I would like to sort this out.

Basically, when I write something like int a = 5+5; and after watching bytecode generated with javap I see that 10 is pushed on the operand stack, so addition operation is skipped.
1. Can someone tell me why is this?

However, when I write something like this:
boolean t=true;
if(t){
//if block
}else{
//else block
}

2. Instructions for both blocks are written, although else block will never execute. Why ?
3. Now, question in general, is it viable option when building compilers, to create full interpreter (ala old ver. of ruby,basic and alike), execute program and output results (where appropriate) to bytecode instructions ?
For example, in above example write if block directly, skipping if instructions (ifeq), since they would be interpreted already ?

Can someone enlighten me when it comes to this issue,
I would appreciate it, thanks
Alik
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Hi Rashad, welcome to CodeRanch!

Rashad Alik wrote:Basically, when I write something like int a = 5+5; and after watching bytecode generated with javap I see that 10 is pushed on the operand stack, so addition operation is skipped.
1. Can someone tell me why is this?

Because the compiler sees that you only use constants, so it can evaluate the expression ahead of time because the results will never change. This will save the JVM from having to execute the addition at every program run.

2. Instructions for both blocks are written, although else block will never execute. Why ?

I think the language designers figured that this situation is very rare in production code, and optimization doesn't lead to a big performance boost, so it's more trouble than it's worth it to implement the improvement.

3. Now, question in general, is it viable option when building compilers, to create full interpreter (ala old ver. of ruby,basic and alike), execute program and output results (where appropriate) to bytecode instructions ?
For example, in above example write if block directly, skipping if instructions (ifeq), since they would be interpreted already ?

Output results in bytecode? So you're really building a translator/compiler, not an interpreter. Anyway, it won't work in general, because determining whether an expression always results in true or false reduces to the halting problem, and may cause your translator to run indefinitely.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18999
    
  40

Stephan van Hulst wrote:
2. Instructions for both blocks are written, although else block will never execute. Why ?

I think the language designers figured that this situation is very rare in production code, and optimization doesn't lead to a big performance boost, so it's more trouble than it's worth it to implement the improvement.


The Java Language Specification explicitly mentions this case -- meaning that it defines that the unreachable code checker is *not* applied for conditional statements. The reason for this is to allow for conditional compiling -- which is a common programmer use-case for turning off sections of code for testing purposes.... and since the compiler doesn't know that the code is unreachable, I guess it generates the code.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rashad Alik
Greenhorn

Joined: Jun 22, 2012
Posts: 2
Thank you for your answers, they've been helpful
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java jvm bytecode generation question