Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why compiling into intermediate code?

Why do Actionscript, Java, C#, etc. compile into intermediate code? I'm aware of cross-platform benefits of using intermediate code.

The question is: What is the benefit of compiling to intermediate code, compared to scripts (JS, Python, PHP, Perl, etc.) that are interpreted?

Is it just for code obfuscation? Or what?

In addition what is the benefit compared to compiling to native code?

like image 628
DrStrangeLove Avatar asked Jun 22 '11 19:06

DrStrangeLove


3 Answers

It is much faster to parse and JIT-compile IL code than to parse a high-level language like Java or especially C# (which has more features).

It also allows developers to use new language features without updating anything on the end-users' machines. (eg, LINQBridge)

like image 109
SLaks Avatar answered Sep 27 '22 21:09

SLaks


First of all historically java was targeted to mobiles and embedded devices with very limited CPU/memory, and JVM on such devices can't do any intensive optimization, like modern JITs do for now. So java engeneers decided to move optimization to compile phase.

Next, having "byte code"/IL allows you to have many compilers from different languages into such byte code - and only one JVM/JIT. It is much more efficient then to create separate VM+JIT for each language. Remember, you have Java, JRuby, JPython, Groovy, etc... in JVM world, and C#, VB#, ASP.NET, F#, etc -- in .NET world, and only one runtime/VM for each.

like image 31
BegemoT Avatar answered Sep 27 '22 21:09

BegemoT


Intermediate code is very similar to assembly in that it contains a limited set of instructions. The runtime is then able to reliably and consistently operate on this (somewhat) small set of instructions without having to worry about parsing the language, etc. It can therefore gain performance improvements and make optimizations.

like image 38
daveaglick Avatar answered Sep 27 '22 22:09

daveaglick