How is scala generating byte code? Using some libraries like ASM, or write binary directly?

Deadly 提交于 2019-11-27 00:57:35

问题


I'm wondering how is scala generating byte code, does it use some libraries like ASM? Or just write binary to .class files for performance?


回答1:


Starting with 2.10 the Scala compiler uses ASM 4 to emit bytecode, supporting -target:jvm-1.5 , -target:jvm-1.6 , and -target:jvm-1.7

Implementation aspects of the backend are described in:

  • Emitting Scala classfiles via ASM http://lamp.epfl.ch/~magarcia/ScalaCompilerCornerReloaded/2012Q2/GenASM.pdf

The bytecode emitter (GenASM, source linked below) visits a Control Flow Graph (CFG) built by a previous phase, and uses the Streaming ASM API to directly emit classfiles:

https://github.com/scala/scala/blob/master/src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala

That's how things work now. In the realm of experimental possibilities, I've been working on an experimental bytecode emitted that is faster, in part because it skips building the CFG and visits Scala Abstract Syntax Trees directly (using then the Tree ASM API to build classfiles in memory). Details in:

https://github.com/magarciaEPFL/scala/blob/GenBCodeOpt/src/compiler/scala/tools/nsc/backend/jvm/GenBCode.scala




回答2:


looks like they're generating some intermediate code, and then converting it manually into java bytecode or .net msil

Nice overview of compiler here: https://wiki.scala-lang.org/display/SIW/Overview+of+Compiler+Phases



来源:https://stackoverflow.com/questions/13380807/how-is-scala-generating-byte-code-using-some-libraries-like-asm-or-write-binar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!