- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Compiler Back End as Plugin
Thu, 2011-11-24, 04:55
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
Thu, 2011-11-24, 11:07
#2
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:
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.
Thu, 2011-11-24, 11:47
#3
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
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.