-1

In Java, only few things are considered to be atomic (What operations in Java are considered atomic?). For example i++ consists of 3 different atomic operations: Load i into a register, add 1 to that register, write the new value of the register back to i. Something like that.

My question: Is it possible to "parse" Java Code into a sequence of it's atomic representation? So when input is "i++" i don't want to have i++ as output, i want to have "LOAD I TO REGISTER", "ADD 1 TO THAT REGISTER", "WRITE I BACK FROM REGISTER". Is that possible?

Googling didn't help much on that topic.

Community
  • 1
  • 1

2 Answers2

0

In Java you don't have register access or assembly code access. The only way you can realize the above is to use Atomic classes as provide in the java.util.conrrent.atomic package. However, that would be more costly than a simple i++ unless you are doing this on a multi-threading background.

Alex Suo
  • 2,977
  • 1
  • 14
  • 22
0

Yes and no. Short answer: it'll be very very hard.

The code will go through several steps before it arrives at the hardware where you could 'parse' the atomic representations.

It will first be compiled into bytecode. This is already closer to machine code, but not yet what you want. You can read it by opening the class file in an editor that can parse bytecode (Eclipse IDE can do this). When you run the program, the Java Virtual machine will execute your bytecode. It may, depending on the situation, execute the bytecode as a scripting engine, compile it to machine code or change the bytecode and then execute it to get better performance. What the Java Virtual Machine does will depend on the system, the current load on your system and the bytecode. So it's actually impossible to predict the actual machine code it would generate before hand.

For a specific run of code, you can use an assembly debugger (disassembler) such as IDA. It will allow you to view what is being executed on the system as assembly code. You will most likely see a lot of code that is part of the Java Virtual Machine and not of your program. This will make things even more difficult.

HSquirrel
  • 839
  • 4
  • 16