Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JVM (Bytecode to Machine Code)

 
aryan sahu
Greenhorn
Posts: 11
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone please explain how does the jvm generate machine code of a particular processor family.i am quite confused how does it know if it is a AMD or Intel.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aryan sahu wrote:can anyone please explain how does the jvm generate machine code of a particular processor family.i am quite confused how does it know if it is a AMD or Intel.

It is very straight-forward. From JVM perspective (and from most operations' perspective), there is no major difference between AMD and Intel, but I assume that you are aware of the fact that JVM is not platform independent (i.e. you cannot install JVM meant for 64 bit architecture on 32 bit processor).

So, JVM doesn't need to identify anything(because its not 'one JVM for all architectures' kind of thing).

I hope this helps.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's very simple: The JIT (Just In Time) compiler, which is part of the JVM, converts Java bytecode to native machine code at runtime. At that moment it can detect the exact brand and model of the CPU that the program it is running on, so it can generate native machine code that is optimal for that particular CPU.

Note that both AMD and Intel x86-compatible CPUs implement the same instruction set, with minor differences. Newer processors often have enhancements, such as SSE instructions, etc.

When you use a traditional ahead-of-time compiler to compile for example C or C++ source code to native machine code, then the compiler can't make use of CPU-model specific enhancements, at least not if you want the resulting executable to be useable for most people. Suppose you'd compile the code with Intel Core-family specific enhanced native machine code, then it will not run on older CPUs that don't have those extra instructions.

An advantage of having a JIT is that it can (in principle) generate machine code with those extra instructions, since it knows exactly what the target CPU is at runtime - so it can optimize the code very specifically for that CPU.
 
aryan sahu
Greenhorn
Posts: 11
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:
aryan sahu wrote:can anyone please explain how does the jvm generate machine code of a particular processor family.i am quite confused how does it know if it is a AMD or Intel.

It is very straight-forward. From JVM perspective (and from most operations' perspective), there is no major difference between AMD and Intel, but I assume that you are aware of the fact that JVM is not platform independent (i.e. you cannot install JVM meant for 64 bit architecture on 32 bit processor).

So, JVM doesn't need to identify anything(because its not 'one JVM for all architectures' kind of thing).

I hope this helps.


Thank you very much..
 
aryan sahu
Greenhorn
Posts: 11
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:It's very simple: The JIT (Just In Time) compiler, which is part of the JVM, converts Java bytecode to native machine code at runtime. At that moment it can detect the exact brand and model of the CPU that the program it is running on, so it can generate native machine code that is optimal for that particular CPU.

Note that both AMD and Intel x86-compatible CPUs implement the same instruction set, with minor differences. Newer processors often have enhancements, such as SSE instructions, etc.

When you use a traditional ahead-of-time compiler to compile for example C or C++ source code to native machine code, then the compiler can't make use of CPU-model specific enhancements, at least not if you want the resulting executable to be useable for most people. Suppose you'd compile the code with Intel Core-family specific enhanced native machine code, then it will not run on older CPUs that don't have those extra instructions.

An advantage of having a JIT is that it can (in principle) generate machine code with those extra instructions, since it knows exactly what the target CPU is at runtime - so it can optimize the code very specifically for that CPU.


Thank you very much. It is very informative.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic