- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
moving Scala ASTs one step closer to C
Tue, 2011-06-28, 14:50
Hi,
I've developed a compiler plugin that reduces post-CleanUp Scala ASTs from an expression-language into a statement-language, breaking up sub-expression evaluation into non-reducible expressions (informally, "Three-Address-Code"). The component in question can be used *as building block* for a variety of purposes (eg, translation into CUDA, OpenCL, LLVM, Objective-C, C#, etc.). The output ASTs are valid Scala, in fact GenICode and GenJVM and GenMSIL can deal with them.
Sample input:
object Test { def main(args: Array[String]) { val res = if(b(1) + b(2) * b(3) > 0) { throw new Exception; "thenBranch" } else { "elseBranch" } } def b(n: Int) = { scala.Console.println(n); n } }
Sample output (excerpt):
def main(args: Array[java.lang.String]): Unit = {
var tmp10: java.lang.String = _; val tmp1: Int = Test.this.b(1); val tmp2: Int = tmp1; val tmp3: Int = Test.this.b(2); val tmp4: Int = tmp3; val tmp5: Int = Test.this.b(3); val tmp6: Int = tmp4.*(tmp5); val tmp7: Int = tmp2.+(tmp6); val tmp8: Int = tmp7; val tmp9: Boolean = tmp8.>(0); if (tmp9) { val tmp11: java.lang.Exception = new java.lang.Exception(); throw tmp11; tmp10 = "thenBranch" } else tmp10 = "elseBranch"; val res: java.lang.String = tmp10; () }
The PDF at http://lamp.epfl.ch/~magarcia/ScalaCompilerCornerReloaded/2011Q2/Moving3... the technical documentation for the sources at http://lampsvn.epfl.ch/trac/scala/browser/scala-experimental/trunk/imp/s...
If you want to use this plugin *today*, please notice [1] (that came to light during the development of this plugin). Alternatively, the plugin sources show how to workaround that.
Comments and suggestions are welcome.
Miguel http://lamp.epfl.ch/~magarcia/ScalaCompilerCornerReloaded
[1] http://issues.scala-lang.org/browse/SI-4738
Wed, 2011-06-29, 23:17
#2
Aw: Re: moving Scala ASTs one step closer to C
Grzegorz,
About collaborating on AST transformations of mutual benefit, yes, if that's possible then it's great. However, experience shows that not everyone needs the same AST transformations (like, who needs "erasure for .NET" besides Scala.NET?). And, collaboration is easier when one does not drift too far away from the standard compilation pipeline (like, a three-address IR is a subset of the Scala ASTs I know, but Jribble is a different matter).
Talking about specifics, I don't have right now a grand plan to improve on the recent compiler plugins I've shared with the community (three-address code, source-level processors, GOTO elimination). For now, I'll just wait and see if others use them as building blocks (that's why I've documented them ;-).
Miguel http://lamp.epfl.ch/~magarcia/ScalaCompilerCornerReloaded/
Hi Miguel,
Thanks for sharing this. I have similar needs in scalagwt project but my transformations (called "normalize for jribble", see here). I'm not sure if we can share our work because my goal is to make Scala ASTs as close to proper Java as possible.
Are you planning to have some transformations optional like turning asts into having explicit evaluation order?
--
Grzegorz Kossakowski