This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Compiler Back End as Plugin

3 replies
chris.dow8
Joined: 2011-11-24,
User offline. Last seen 49 weeks 4 days ago.

Is it possible to create a new backend for the Scala compiler as a
plugin? Based on the Scaladoc and the examples I have seen it looks
like you can add additional phases but you can't replace phases (i.e
replacing the GenJVM phase).

If you can't replace phases then how does the GenMSIL phase hook into
the compiler?

Also, does anybody know what the best place is to find documentation
for writing Scala compiler plugins?

Thanks

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Compiler Back End as Plugin

On Wed, Nov 23, 2011 at 7:55 PM, Chris Dow wrote:
> Is it possible to create a new backend for the Scala compiler as a
> plugin?

Sure. (But genjvm might do its thing too.)

> Based on the Scaladoc and the examples I have seen it looks
> like you can add additional phases but you can't replace phases (i.e
> replacing the GenJVM phase).

Are you determined to do it with a plugin? It's easy to do if you
subclass Global. I'm not aware of anything in the plugin architecture
that lets you evict phases.

> If you can't replace phases then how does the GenMSIL phase hook into
> the compiler?

There are "platform phases":

protected def computePlatformPhases() = platform.platformPhases
foreach { sub =>
addToPhasesSet(sub, otherPhaseDescriptions(sub.phaseName))
}

In MSILPlatform that's these:

def platformPhases = List(
genMSIL // generate .msil files
)

In JavaPlatform it's these:

def platformPhases = List(
flatten, // get rid of inner classes
genJVM // generate .class files
) ++ depAnalysisPhase

But GenMSIL isn't a plugin, it's part of the compiler.

sjrd
Joined: 2011-04-09,
User offline. Last seen 51 weeks 2 days ago.
Re: Compiler Back End as Plugin
Hi,

Subclassing Global is the easiest way to go if you want another back-end. That's what I did for Ozma, aka Scala-Mozart [1].

Sébastien

[1] https://github.com/sjrd/ozma/blob/master/src/compiler/scala/tools/nsc/ozma/OzmaGlobal.scala

On Thu, Nov 24, 2011 at 05:30, Paul Phillips <paulp@improving.org> wrote:
On Wed, Nov 23, 2011 at 7:55 PM, Chris Dow <chris.dow8@gmail.com> wrote:
> Is it possible to create a new backend for the Scala compiler as a
> plugin?

Sure.  (But genjvm might do its thing too.)

> Based on the Scaladoc and the examples I have seen it looks
> like you can add additional phases but you can't replace phases (i.e
> replacing the GenJVM phase).

Are you determined to do it with a plugin? It's easy to do if you
subclass Global.  I'm not aware of anything in the plugin architecture
that lets you evict phases.

> If you can't replace phases then how does the GenMSIL phase hook into
> the compiler?

There are "platform phases":

 protected def computePlatformPhases() = platform.platformPhases
foreach { sub =>
   addToPhasesSet(sub, otherPhaseDescriptions(sub.phaseName))
 }

In MSILPlatform that's these:

 def platformPhases = List(
   genMSIL   // generate .msil files
 )

In JavaPlatform it's these:

 def platformPhases = List(
   flatten,    // get rid of inner classes
   genJVM      // generate .class files
 ) ++ depAnalysisPhase

But GenMSIL isn't a plugin, it's part of the compiler.

Miguel Garcia 2
Joined: 2011-01-30,
User offline. Last seen 42 years 45 weeks ago.
Re: Compiler Back End as Plugin

For compiler phases, there's the -Yskip command-line option:

// Each subcomponent supplies a phase, which are chained together.
//   If -Ystop:phase is given, neither that phase nor any beyond it is added.
//   If -Yskip:phase is given, that phase will be skipped.

After a prototype compiler-plugin works, it's always possible to move that code to a built-in phase. Plugin development is faster (just the plugin needs recompiling, it's loaded dynamically on each compiler run).

My take on writing compiler plugins: http://lamp.epfl.ch/~magarcia/ScalaCompilerCornerReloaded/

Miguel


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland