scala编译器阶段的顺序是什么?

时间:2010-12-24 18:58:29

标签: scala compiler-construction scalac

我想要了解scala编译阶段的图片。我知道某些事情必须在编译器中发生,但是不知道它们发生的顺序以及顺序应该如何影响我的编程。

我是否正确地说以下内容是编译器的完整列表?

  • 解析程序
  • 已检查类型
  • do erasure
  • 隐式转化
  • 生成字节码
  • 优化

如果是这样,这些阶段的顺序是什么? 这个顺序如何影响程序员,尤其是类型级程序员?

4 个答案:

答案 0 :(得分:46)

您可以使用scalac -Xshow-phases来查看阶段,顺序和说明。

在2.11中,使用-Xshow-phases -Ydebug显示启用和禁用阶段。

以下是2.10.0:

的用法
 » scalac -Xshow-phases

             phase name  id  description
             ----------  --  -----------
                 parser   1  parse source into ASTs, perform simple desugaring
                  namer   2  resolve names, attach symbols to named trees
         packageobjects   3  load package objects
                  typer   4  the meat and potatoes: type the trees
                 patmat   5  translate match expressions
         superaccessors   6  add super accessors in traits and nested classes
             extmethods   7  add extension methods for inline classes
                pickler   8  serialize symbol tables
              refchecks   9  reference/override checking, translate nested objects
           selectiveanf  10  
           selectivecps  11  
                uncurry  12  uncurry, translate function values to anonymous classes
              tailcalls  13  replace tail calls by jumps
             specialize  14  @specialized-driven class and method specialization
          explicitouter  15  this refs to outer pointers, translate patterns
                erasure  16  erase types, add interfaces for traits
            posterasure  17  clean up erased inline classes
               lazyvals  18  allocate bitmaps, translate lazy vals into lazified defs
             lambdalift  19  move nested functions to top level
           constructors  20  move field definitions into constructors
                flatten  21  eliminate inner classes
                  mixin  22  mixin composition
                cleanup  23  platform-specific cleanups, generate reflective calls
                  icode  24  generate portable intermediate code
                inliner  25  optimization: do inlining
inlineExceptionHandlers  26  optimization: inline exception handlers
               closelim  27  optimization: eliminate uncalled closures
                    dce  28  optimization: eliminate dead code
                    jvm  29  generate JVM bytecode
               terminal  30  The last phase in the compiler chain

答案 1 :(得分:10)

你看过here了吗?该页面指向http://www.scala-lang.org/sites/default/files/sids/nielsen/Thu,%202009-05-28,%2008:13/compiler-phases-sid.pdf(PDF),它描述了Scala 2.8的编译器阶段。

答案 2 :(得分:2)

除非您正在编写编译器插件,否则您真的不应该担心编译器阶段的顺序。即使对于类型级编程,您也应该专注于语言语义。

就擦除而言,例如,了解静态与动态类型以及单独编译的模型非常重要。同样,了解编译器应用隐式转换的条件也很重要。在编译周期中出现这些只是一个实现细节。

答案 3 :(得分:1)

Scala.js 0.6.23

     phase name  id  description
     ----------  --  -----------
         parser   1  parse source into ASTs, perform simple desugaring
     jspretyper   2  capture pre-typer only tree info (for Scala.js)
          namer   3  resolve names, attach symbols to named trees
 packageobjects   4  load package objects
          typer   5  the meat and potatoes: type the trees
      jsinterop   6  prepare ASTs for JavaScript interop
         patmat   7  translate match expressions
 superaccessors   8  add super accessors in traits and nested classes
     extmethods   9  add extension methods for inline classes
        pickler  10  serialize symbol tables
       xsbt-api  11  
xsbt-dependency  12  
      refchecks  13  reference/override checking, translate nested objects
        uncurry  14  uncurry, translate function values to anonymous classes
      tailcalls  15  replace tail calls by jumps
     specialize  16  @specialized-driven class and method specialization
  explicitouter  17  this refs to outer pointers
        erasure  18  erase types, add interfaces for traits
    posterasure  19  clean up erased inline classes
       lazyvals  20  allocate bitmaps, translate lazy vals into lazified defs
     lambdalift  21  move nested functions to top level
   constructors  22  move field definitions into constructors
        flatten  23  eliminate inner classes
          mixin  24  mixin composition
         jscode  25  generate JavaScript code from ASTs
        cleanup  26  platform-specific cleanups, generate reflective calls
     delambdafy  27  remove lambdas
          icode  28  generate portable intermediate code
        inliner  xx  optimization: do inlining
 inlinehandlers  xx  optimization: inline exception handlers
       closelim  xx  optimization: eliminate uncalled closures
       constopt  xx  optimization: optimize null and other constants
            dce  xx  optimization: eliminate dead code
            jvm  29  generate JVM bytecode
  xsbt-analyzer  30  
       terminal  31  the last phase during a compilation run

Scala本机0.3.7

     phase name  id  description
     ----------  --  -----------
         parser   1  parse source into ASTs, perform simple desugaring
          namer   2  resolve names, attach symbols to named trees
 packageobjects   3  load package objects
          typer   4  the meat and potatoes: type the trees
  nativeinterop   5  prepare ASTs for Native interop
         patmat   6  translate match expressions
 superaccessors   7  add super accessors in traits and nested classes
     extmethods   8  add extension methods for inline classes
        pickler   9  serialize symbol tables
       xsbt-api  10  
xsbt-dependency  11  
      refchecks  12  reference/override checking, translate nested objects
        uncurry  13  uncurry, translate function values to anonymous classes
      tailcalls  14  replace tail calls by jumps
     specialize  15  @specialized-driven class and method specialization
  explicitouter  16  this refs to outer pointers
        erasure  17  erase types, add interfaces for traits
    posterasure  18  clean up erased inline classes
       lazyvals  19  allocate bitmaps, translate lazy vals into lazified defs
     lambdalift  20  move nested functions to top level
   constructors  21  move field definitions into constructors
        flatten  22  eliminate inner classes
          mixin  23  mixin composition
            nir  24  
        cleanup  25  platform-specific cleanups, generate reflective calls
     delambdafy  26  remove lambdas
          icode  27  generate portable intermediate code
        inliner  xx  optimization: do inlining
 inlinehandlers  xx  optimization: inline exception handlers
       closelim  xx  optimization: eliminate uncalled closures
       constopt  xx  optimization: optimize null and other constants
            dce  xx  optimization: eliminate dead code
            jvm  28  generate JVM bytecode
  xsbt-analyzer  29  
       terminal  30  the last phase during a compilation run