Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java jvm bytecode generation question

 
Rashad Alik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6323
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rashad Alik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answers, they've been helpful
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic