Skip to content

Commit

Permalink
Merge pull request TiarkRompf#1 from Lewix/booster-develop-0.3
Browse files Browse the repository at this point in the history
String interpolators for codegens
  • Loading branch information
yannisklonatos committed Oct 28, 2013
2 parents 1402d75 + 7fe93ba commit f194eb4
Show file tree
Hide file tree
Showing 59 changed files with 1,003 additions and 408 deletions.
10 changes: 5 additions & 5 deletions src/common/ArrayBufferOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ trait ScalaGenArrayBufferOps extends BaseGenArrayBufferOps with ScalaGenEffect {
import IR._

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case a@ArrayBufferNew(xs) => emitValDef(sym, "scala.collection.mutable.ArrayBuffer[" + remap(a.mA) + "](" + (xs map {quote}).mkString(",") + ")")
case ArrayBufferMkString(l, sep) => emitValDef(sym, quote(l) + ".mkString(" + quote(sep) + ")")
case ArrayBufferAppend(l, e) => emitValDef(sym, quote(l) + " += " + quote(e))
case ArrayBufferToArray(x) => emitValDef(sym, quote(x) + ".toArray")
case ArrayBufferToSeq(x) => emitValDef(sym, quote(x) + ".toSeq")
case a@ArrayBufferNew(xs) => emitValDef(sym, src"scala.collection.mutable.ArrayBuffer[${a.mA}](${(xs map {quote}).mkString(",")})")
case ArrayBufferMkString(l, sep) => emitValDef(sym, src"$l.mkString($sep)")
case ArrayBufferAppend(l, e) => emitValDef(sym, src"$l += $e")
case ArrayBufferToArray(x) => emitValDef(sym, src"$x.toArray")
case ArrayBufferToSeq(x) => emitValDef(sym, src"$x.toSeq")
case _ => super.emitNode(sym, rhs)
}
}
Expand Down
111 changes: 55 additions & 56 deletions src/common/ArrayOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ trait ArrayOps extends Variables {
object NewArray {
def apply[T:Manifest](n: Rep[Int], specializedType: Rep[String] = unit("")) = array_obj_new(n, specializedType)
}

object Array {
def apply[T:Manifest](xs: T*) = array_obj_fromseq(xs)
}

class ArrayOpsCls[T:Manifest](a: Rep[Array[T]]){
def apply(n: Rep[Int])(implicit pos: SourceContext) = array_apply(a, n)
def update(n: Rep[Int], y: Rep[T])(implicit pos: SourceContext) = array_update(a,n,y)
Expand Down Expand Up @@ -62,7 +62,7 @@ trait ArrayOpsExp extends ArrayOps with EffectExp with VariablesExp {
val m = manifest[T]
}
case class ArrayApply[T:Manifest](a: Exp[Array[T]], n: Exp[Int]) extends Def[T]
case class ArrayUpdate[T:Manifest](a: Exp[Array[T]], n: Exp[Int], y: Exp[T]) extends Def[Unit]
case class ArrayUpdate[T:Manifest](a: Exp[Array[T]], n: Exp[Int], y: Exp[T]) extends Def[Unit]
case class ArrayLength[T:Manifest](a: Exp[Array[T]]) extends Def[Int] {
val m = manifest[T]
}
Expand Down Expand Up @@ -98,7 +98,7 @@ trait ArrayOpsExp extends ArrayOps with EffectExp with VariablesExp {
def array_map[A:Manifest,B:Manifest](a: Exp[Array[A]], f: Exp[A] => Exp[B]) = {
val x = fresh[A]
val b = reifyEffects(f(x))
reflectEffect(ArrayMap(a, x, b), summarizeEffects(b))
reflectEffect(ArrayMap(a, x, b), summarizeEffects(b))
}
def array_toseq[A:Manifest](a: Exp[Array[A]]) = ArrayToSeq(a)
def array_zip[A:Manifest, B: Manifest](a: Exp[Array[A]], a2: Exp[Array[B]]) = reflectEffect(ArrayZip(a,a2))
Expand All @@ -117,11 +117,11 @@ trait ArrayOpsExp extends ArrayOps with EffectExp with VariablesExp {
case Reflect(e@ArrayLength(x), u, es) => reflectMirrored(Reflect(ArrayLength(f(x))(e.m), mapOver(f,u), f(es)))(mtype(manifest[A]))
case Reflect(ArrayApply(l,r), u, es) => reflectMirrored(Reflect(ArrayApply(f(l),f(r))(mtype(manifest[A])), mapOver(f,u), f(es)))(mtype(manifest[A]))
case Reflect(e@ArraySort(x), u, es) => reflectMirrored(Reflect(ArraySort(f(x))(e.m), mapOver(f,u), f(es)))(mtype(manifest[A]))
case Reflect(ArrayUpdate(l,i,r), u, es) => reflectMirrored(Reflect(ArrayUpdate(f(l),f(i),f(r)), mapOver(f,u), f(es)))(mtype(manifest[A]))
case Reflect(e@ArrayCopy(a,ap,d,dp,l), u, es) => reflectMirrored(Reflect(ArrayCopy(f(a),f(ap),f(d),f(dp),f(l))(e.m), mapOver(f,u), f(es)))(mtype(manifest[A]))
case Reflect(ArrayUpdate(l,i,r), u, es) => reflectMirrored(Reflect(ArrayUpdate(f(l),f(i),f(r)), mapOver(f,u), f(es)))(mtype(manifest[A]))
case Reflect(e@ArrayCopy(a,ap,d,dp,l), u, es) => reflectMirrored(Reflect(ArrayCopy(f(a),f(ap),f(d),f(dp),f(l))(e.m), mapOver(f,u), f(es)))(mtype(manifest[A]))
case _ => super.mirror(e,f)
}).asInstanceOf[Exp[A]] // why??

override def syms(e: Any): List[Sym[Any]] = e match {
case ArrayForeach(a, x, body) => syms(a):::syms(body)
case ArrayMap(a, x, body) => syms(a):::syms(body)
Expand All @@ -139,7 +139,7 @@ trait ArrayOpsExp extends ArrayOps with EffectExp with VariablesExp {
case ArrayMap(a, x, body) => freqNormal(a):::freqHot(body)
case _ => super.symsFreq(e)
}

}

trait ArrayOpsExpOpt extends ArrayOpsExp {
Expand All @@ -148,11 +148,11 @@ trait ArrayOpsExpOpt extends ArrayOpsExp {
override def array_apply[T:Manifest](x: Exp[Array[T]], n: Exp[Int])(implicit pos: SourceContext): Exp[T] = {
if (context ne null) {
// find the last modification of array x
// if it is an assigment at index n, just return the last value assigned
// if it is an assigment at index n, just return the last value assigned
val vs = x.asInstanceOf[Sym[Array[T]]]
//TODO: could use calculateDependencies?
val rhs = context.reverse.collectFirst {

val rhs = context.reverse.collectFirst {
//case w @ Def(Reflect(ArrayNew(sz: Exp[T]), _, _)) if w == x => Some(Const(0)) // FIXME: bounds check!
case Def(Reflect(ArrayUpdate(`x`, `n`, rhs: Exp[T]), _, _)) => Some(rhs)
case Def(Reflect(_, u, _)) if mayWrite(u, List(vs)) => None // not a simple assignment
Expand All @@ -162,15 +162,15 @@ trait ArrayOpsExpOpt extends ArrayOpsExp {
super.array_apply(x,n)
}
}

override def array_update[T:Manifest](x: Exp[Array[T]], n: Exp[Int], y: Exp[T])(implicit pos: SourceContext) = {
if (context ne null) {
// find the last modification of array x
// if it is an assigment at index n with the same value, just do nothing
val vs = x.asInstanceOf[Sym[Array[T]]]
//TODO: could use calculateDependencies?
val rhs = context.reverse.collectFirst {

val rhs = context.reverse.collectFirst {
//case w @ Def(Reflect(ArrayNew(sz: Exp[T]), _, _)) if w == x => Some(Const(())) // FIXME: bounds check!
case Def(Reflect(ArrayUpdate(`x`, `n`, `y`), _, _)) => Some(Const(()))
case Def(Reflect(_, u, _)) if mayWrite(u, List(vs)) => None // not a simple assignment
Expand All @@ -197,17 +197,17 @@ trait BaseGenArrayOps extends GenericNestedCodegen {
trait ScalaGenArrayOps extends BaseGenArrayOps with ScalaGenBase {
val IR: ArrayOpsExp
import IR._

val ARRAY_LITERAL_MAX_SIZE = 1000

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case a@ArrayNew(n, sType) => {
val arrType = if (quote(sType) != "\"\"") quote(sType).replaceAll("\"","") else remap(a.m)
emitValDef(sym, "new Array[" + arrType + "](" + quote(n) + ")")
emitValDef(sym, src"new Array[$arrType]($n)")
}
case e@ArrayFromSeq(xs) => {
emitData(sym, xs)
emitValDef(sym,
emitValDef(sym,
if(xs.size > ARRAY_LITERAL_MAX_SIZE) {
/* def append(i: Int) = {
val start = i*ARRAY_LITERAL_MAX_SIZE
Expand All @@ -220,50 +220,49 @@ trait ScalaGenArrayOps extends BaseGenArrayOps with ScalaGenBase {
"{import scala.io.Source;(Source.fromFile(\"" + symDataPath(sym) + "\").getLines.map{Integer.parseInt(_)}).toArray}"
}
else {
"Array(" + xs.mkString(",") + ")"
src"Array($xs)"
}
)
}
case ArrayApply(x,n) => emitValDef(sym, "" + quote(x) + "(" + quote(n) + ")")
case ArrayUpdate(x,n,y) => emitAssignment(sym, "" + quote(x) + "(" + quote(n) + ")", quote(y))
case ArrayLength(x) => emitValDef(sym, "" + quote(x) + ".length")
case ArrayApply(x,n) => emitValDef(sym, src"$x($n)")
case ArrayUpdate(x,n,y) => emitAssignment(sym, src"$x($n)", quote(y))
case ArrayLength(x) => emitValDef(sym, src"$x.length")
case ArrayForeach(a,x,block) =>
emitValDef(sym, quote(a) + ".foreach{")
stream.println(quote(x) + " => ")
emitBlock(block)
emitBlockResult(block)
stream.println("}")
case ArrayCopy(src,srcPos,dest,destPos,len) => emitValDef(sym, "System.arraycopy(" + quote(src) + "," + quote(srcPos) + "," + quote(dest) + "," + quote(destPos) + "," + quote(len) + ")")
emitValDef(sym, src"$a.foreach{")
gen"""$x =>
|${nestedBlock(block)}
|$block
|}"""
case ArrayCopy(src,srcPos,dest,destPos,len) => emitValDef(sym, src"System.arraycopy($src,$srcPos,$dest,$destPos,$len)")
case a@ArraySort(x) =>
val strWriter = new java.io.StringWriter
val localStream = new PrintWriter(strWriter);
withStream(localStream) {
stream.println("{")
stream.println("val d = new Array[" + remap(a.m) + "](" + quote(x) + ".length" + ")")
stream.println("System.arraycopy(" + quote(x) + ", 0, d, 0, " + quote(x) + ".length)")
stream.println("scala.util.Sorting.quickSort(d)")
stream.println("d")
stream.print("}")
gen"""{
|val d = new Array[${a.m}]($x.length)
|System.arraycopy($x, 0, d, 0, $x.length)
|scala.util.Sorting.quickSort(d)
|d
|}"""
}
emitValDef(sym, strWriter.toString)
case n@ArrayMap(a,x,blk) =>
val strWriter = new java.io.StringWriter
val localStream = new PrintWriter(strWriter);
withStream(localStream) {
//stream.println("/* workaround for refinedManifest problem */")
stream.println("{")
stream.println("val out = " + quote(n.array))
stream.println("val in = " + quote(a))
stream.println("var i = 0")
stream.println("while (i < in.length) {")
stream.println("val " + quote(x) + " = in(i)")
emitBlock(blk)
stream.print("out(i) = ")
emitBlockResult(blk)
stream.println("i += 1")
stream.println("}")
stream.println("out")
stream.print("}")
gen"""{
|val out = ${n.array}
|val in = $a
|var i = 0
|while (i < in.length) {
|val $x = in(i)
|${nestedBlock(blk)}
|out(i) = $blk
|i += 1
|}
|out
|}"""
}
emitValDef(sym, strWriter.toString)

Expand All @@ -272,14 +271,14 @@ trait ScalaGenArrayOps extends BaseGenArrayOps with ScalaGenBase {
// emitBlock(blk)
// stream.println(quote(getBlockResult(blk)))
// stream.println("}")
case ArrayToSeq(a) => emitValDef(sym, quote(a) + ".toSeq")
case ArrayZip(a,a2) => emitValDef(sym, quote(a) + " zip " + quote(a2))
case ArrayToSeq(a) => emitValDef(sym, src"$a.toSeq")
case ArrayZip(a,a2) => emitValDef(sym, src"$a zip $a2")
case ArrayMkString(a, del) =>
if (del != unit(""))
emitValDef(sym, quote(a) + ".mkString(" + quote(del) + ")")
else
emitValDef(sym, quote(a) + ".mkString")
case ArrayCorresponds(a,a2) => emitValDef(sym, quote(a) + ".corresponds(" + quote(a2) + "){_==_}")
if (del != unit(""))
emitValDef(sym, src"$a.mkString($del)")
else
emitValDef(sym, src"$a.mkString")
case ArrayCorresponds(a,a2) => emitValDef(sym, src"$a.corresponds($a2){_==_}")
case _ => super.emitNode(sym, rhs)
}
}
Expand All @@ -290,9 +289,9 @@ trait CLikeGenArrayOps extends BaseGenArrayOps with CLikeGenBase {

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = {
rhs match {
case ArrayLength(x) => emitValDef(sym, quote(x) + ".length")
case ArrayApply(x,n) => emitValDef(sym, quote(x) + ".apply(" + quote(n) + ")")
case ArrayUpdate(x,n,y) => stream.println(quote(x) + ".update(" + quote(n) + "," + quote(y) + ");")
case ArrayLength(x) => emitValDef(sym, src"$x.length")
case ArrayApply(x,n) => emitValDef(sym, src"$x.apply($n)")
case ArrayUpdate(x,n,y) => stream.println(src"$x.update($n,$y);")
case _ => super.emitNode(sym, rhs)
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/common/BooleanOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ trait ScalaGenBooleanOps extends ScalaGenBase with GenericNestedCodegen {
import IR._

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case BooleanNegate(b) => emitValDef(sym, "!" + quote(b))
case BooleanNegate(b) => emitValDef(sym, src"!$b")
case BooleanAnd(lhs,rhs) =>
val strWriter = new java.io.StringWriter
val localStream = new PrintWriter(strWriter);
withStream(localStream) {
stream.println("if (" + quote(lhs) + " == true) {")
emitBlock(rhs)
emitBlockResult(rhs)
stream.print("} else false")
gen"""if ($lhs == true) {
|${nestedBlock(rhs)}
|$rhs
|} else false"""
}
emitValDef(sym, strWriter.toString)
case BooleanOr(lhs,rhs) => emitValDef(sym, quote(lhs) + " || " + quote(rhs))
case BooleanOr(lhs,rhs) => emitValDef(sym, src"$lhs || $rhs")
case _ => super.emitNode(sym,rhs)
}
}
Expand All @@ -100,7 +100,7 @@ trait CLikeGenBooleanOps extends CLikeGenBase {
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = {
rhs match {
case BooleanNegate(b) =>
emitValDef(sym, "!" + quote(b))
emitValDef(sym, src"!$b")
case _ => super.emitNode(sym,rhs)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/common/CastingOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ trait ScalaGenCastingOps extends ScalaGenBase {
import IR._

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case RepIsInstanceOf(x,mA,mB) => emitValDef(sym, quote(x) + ".isInstanceOf[" + remap(mB) + "]")
case RepAsInstanceOf(x,mA,mB) => emitValDef(sym, quote(x) + ".asInstanceOf[" + remap(mB) + "]")
case RepIsInstanceOf(x,mA,mB) => emitValDef(sym, src"$x.isInstanceOf[$mB]")
case RepAsInstanceOf(x,mA,mB) => emitValDef(sym, src"$x.asInstanceOf[$mB]")
case _ => super.emitNode(sym, rhs)
}
}
Expand All @@ -54,7 +54,7 @@ trait CLikeGenCastingOps extends CLikeGenBase {
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = {
rhs match {
//case RepIsInstanceOf(x,mA,mB) => //TODO: How?
case RepAsInstanceOf(x,mA,mB) => emitValDef(sym, "(%s) %s".format(remap(mB),quote(x)))
case RepAsInstanceOf(x,mA,mB) => emitValDef(sym, src"($mB) $x")
case _ => super.emitNode(sym, rhs)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/common/DSLOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ trait ScalaGenDSLOps extends ScalaGenEffect with BaseGenDSLOps {
val strWriter = new java.io.StringWriter
val localStream = new PrintWriter(strWriter);
withStream(localStream) {
stream.println("{ ")
emitBlock(b)
stream.println(quote(getBlockResult(b)))
stream.print("}")
gen"""{
|${nestedBlock(b)}
|$b
|}"""
}
emitValDef(sym, strWriter.toString)

Expand Down
8 changes: 4 additions & 4 deletions src/common/Equal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ trait ScalaGenEqual extends ScalaGenBase {
import IR._

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case Equal(a,b) => emitValDef(sym, quote(a) + " == " + quote(b))
case NotEqual(a,b) => emitValDef(sym, quote(a) + " != " + quote(b))
case Equal(a,b) => emitValDef(sym, src"$a == $b")
case NotEqual(a,b) => emitValDef(sym, src"$a != $b")
case _ => super.emitNode(sym, rhs)
}
}
Expand All @@ -96,9 +96,9 @@ trait CLikeGenEqual extends CLikeGenBase {
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = {
rhs match {
case Equal(a,b) =>
emitValDef(sym, quote(a) + " == " + quote(b))
emitValDef(sym, src"$a == $b")
case NotEqual(a,b) =>
emitValDef(sym, quote(a) + " != " + quote(b))
emitValDef(sym, src"$a != $b")
case _ => super.emitNode(sym, rhs)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/ExceptionOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ trait ScalaGenExceptionOps extends ScalaGenBase {
import IR._

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case ThrowException(m) => emitValDef(sym, "throw new Exception(" + quote(m) + ")")
case ThrowException(m) => emitValDef(sym, src"throw new Exception($m)")
case _ => super.emitNode(sym, rhs)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/FractionalOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait ScalaGenFractionalOps extends ScalaGenBase {
import IR._

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case FractionalDivide(a,b) => emitValDef(sym, quote(a) + " / " + quote(b))
case FractionalDivide(a,b) => emitValDef(sym, src"$a / $b")
case _ => super.emitNode(sym, rhs)
}
}
Expand All @@ -34,7 +34,7 @@ trait CLikeGenFractionalOps extends CLikeGenBase {
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = {
rhs match {
case FractionalDivide(a,b) =>
emitValDef(sym, quote(a) + " / " + quote(b))
emitValDef(sym, src"$a / $b")
case _ => super.emitNode(sym, rhs)
}
}
Expand Down
Loading

0 comments on commit f194eb4

Please sign in to comment.