• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interpreting IF and Jumps in bytecode  RSS feed

 
Robert Konigsberg
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am now looking at the part of my decompiler where I need to deal with the ifs and jumps, and, surprisingly, it's turning out to be more complicated than I thought. I see now that I need to handle decompiling with a couple of extra steps:

1. Without ifs and gotos, the whole thing is pretty easy. Whenever there's a push, I create an "Expression" object and push the Expression object on an Expression stack. If there's, say, an ADD instruction, I pop two Expressions off the Expression stack and replace it with an AddExpression (extends Expression) and that AddExpression has elements leftExpression and rightExpression.

2. Finally, operations such as field and local variable assignments pop the Expression off the Expression stack, and create an Instruction. There is an Instruction LIST which manages instructions, in order.

This works great until loops enter the picture.

For instance, have you seen what "a && b" looks like in bytecode? It looks like this:



So now I'm thinking that before even looking at expressions, I need to create a directed graph showing bytecode flow, and from there I can extract sections of code that represent branches. I can then pick out patterns that look like if-then, or && or || operations, but of course, different java compilers might handle && differently, but I do doubt it.

Summary:

1. Create directed graph of java bytecodes.
2. Optimize out "&&", "||", "!" by looking for patterns in the graph. (Sounds easy, doesn't it?)
3. Optimize out while() loops by looking for patterns in the graph (easy -- just find jumps that go backwards.

What are your thoughts on this matter?

Now I see that when I master break, continue and switch, I'll be home free. Anybody thinking of adding "goto" as a java keyword that actually does something


Rob
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a bit off topic -- but have you thought of simply contributing to an existing open-source decompiler rather than re-inventing the wheel? I found a few projects on sourceforge....
 
Robert Konigsberg
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jessica,

Yeah, it's not really about that. I've been out of work for a while (though this will change on Monday) and it's been the best project for me to get my head in the game.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!