3

Some languages (mostly ones made in the last 2 decades or so) can now not only run (after some compilation step or directly) on several platforms, but also can be compiled to run on several virtual machines.

For example, take Scala. Pure Scala code can be either interpreted , compiled to run on the JVM (Java Virtual Machine) or can be compiled to run using the .NET Framework.

The benefits of platform-independence put aside, what exactly are the benefits of writing code that can compile on several virtual machines and runtimes?

ApprenticeHacker
  • 1,373
  • 1
  • 18
  • 27
  • 2
    Technical correction: Scala code cannot (or has not, at any rate) be interpreted. Both REPL and scripting rely on compilation. – Daniel C. Sobral Dec 25 '11 at 13:55
  • @DanielC.Sobral source? – Mahmoud Hossam Dec 25 '11 at 19:04
  • Diversity. Not all virtual machines and runtimes are available on all platforms. –  Dec 25 '11 at 19:36
  • @MahmoudHossam http://github.com/scala/scala. – Daniel C. Sobral Dec 25 '11 at 22:56
  • @DanielC.Sobral where does it say all code will be compiled? obviously I can't infer that from all this code. – Mahmoud Hossam Dec 26 '11 at 01:17
  • @MahmoudHossam If you can't read the code, how can you trust what I say about it? And this really isn't a topic for a *comment* on someone else's question. But briefly, `scala`, the script, calls [MainGenericRunner](https://github.com/scala/scala/blob/master/src/compiler/scala/tools/nsc/MainGenericRunner.scala#L78), which calls [ILoop](https://github.com/scala/scala/blob/master/src/compiler/scala/tools/nsc/interpreter/ILoop.scala#L743) which calls [IMain](https://github.com/scala/scala/blob/master/src/compiler/scala/tools/nsc/interpreter/IMain.scala#L568) which parses, loads and runs the code. – Daniel C. Sobral Dec 26 '11 at 12:53

2 Answers2

5

It is simpler to write the intermediate compilers - one to the bytecode/IL (whatever) from your source, and one from bytecode/IL (whatever) to native code.

It also allows you to create new languages and target the bytecode or write new bytecode to a new processor and automatically have a new platform available.

See this blog post by Eric Lippert (one of the C# compiler and specification writers) on the subject.

Glorfindel
  • 3,137
  • 6
  • 25
  • 33
Oded
  • 53,326
  • 19
  • 166
  • 181
3

In addition to what Oded said, it is a lot easier to write programs that dynamically produce code at runtime: all you have to do is emit bytecode, (or CIL, or "cross machine code", or whatever you call it,) which has a reduced, and at the same time relatively high-level, instruction set.

Mike Nakis
  • 32,003
  • 7
  • 76
  • 111