Skip to content

Commit 97164c1

Browse files
committed
Merge pull request #105 from scalan/codegen-fix
Codegen fix
2 parents 75e3ea4 + c09f7a0 commit 97164c1

File tree

7 files changed

+33
-13
lines changed

7 files changed

+33
-13
lines changed

src/common/ArrayOps.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ trait ScalaGenArrayOps extends BaseGenArrayOps with ScalaGenBase {
206206
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
207207
case a@ArrayNew(n) => emitValDef(sym, src"new Array[${remap(a.m)}]($n)")
208208
case e@ArrayFromSeq(xs) => {
209+
val m = e.m
209210
emitData(sym, xs)
210211
emitValDef(sym,
211212
if(xs.size > ARRAY_LITERAL_MAX_SIZE) {
@@ -217,10 +218,15 @@ trait ScalaGenArrayOps extends BaseGenArrayOps with ScalaGenBase {
217218
}
218219
val numBlocks = Math.ceil(xs.size / ARRAY_LITERAL_MAX_SIZE).intValue
219220
"{val buf=new Array[" + remap(e.mt) + "](" + xs.size + ")\n" + ((0 until numBlocks).map(append)).mkString("\n") + "buf}" */
220-
"{import scala.io.Source;(Source.fromFile(\"" + symDataPath(sym) + "\").getLines.map{Integer.parseInt(_)}).toArray}"
221-
}
222-
else {
223-
"Array(" + (xs map quote).mkString(",") + ")"
221+
val parseMethod = m.asInstanceOf[Manifest[_]] match {
222+
case Manifest.Int => "Integer.parseInt"
223+
case Manifest.Long | Manifest.Float | Manifest.Double => s"java.lang.$m.parse$m"
224+
case _ if m == manifest[String] => "x => x"
225+
case _ => throw new GenerationFailedException(s"Can't store an array of type $m in a file")
226+
}
227+
s"""scala.io.Source.fromFile("${symDataPath(sym)}").getLines.map($parseMethod).toArray"""
228+
} else {
229+
src"Array[$m]($xs)"
224230
}
225231
)
226232
}

src/common/OrderingOps.scala

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,19 @@ trait ScalaGenOrderingOps extends ScalaGenBase {
152152
case OrderingGT(a,b) => emitValDef(sym, src"$a > $b")
153153
case OrderingGTEQ(a,b) => emitValDef(sym, src"$a >= $b")
154154
case OrderingEquiv(a,b) => emitValDef(sym, src"$a equiv $b")
155-
case OrderingMax(a,b) => emitValDef(sym, src"$a max $b")
156-
case OrderingMin(a,b) => emitValDef(sym, src"$a min $b")
155+
// "$a max $b" is wrong for Strings because it tries to use `StringLike.max(Ordering)`
156+
case c@OrderingMax(a,b) =>
157+
val rhs = if (c.mev == manifest[String])
158+
src"scala.math.Ordering.String.max($a, $b)"
159+
else
160+
src"$a max $b"
161+
emitValDef(sym, rhs)
162+
case c@OrderingMin(a,b) =>
163+
val rhs = if (c.mev == manifest[String])
164+
src"scala.math.Ordering.String.min($a, $b)"
165+
else
166+
src"$a min $b"
167+
emitValDef(sym, rhs)
157168
case c@OrderingCompare(a,b) => c.mev match {
158169
case m if m == Manifest.Int => emitValDef(sym, "java.lang.Integer.compare("+quote(a)+","+quote(b)+")")
159170
case m if m == Manifest.Long => emitValDef(sym, "java.lang.Long.compare("+quote(a)+","+quote(b)+")")

src/internal/Config.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ trait Config {
55
val verbosity = System.getProperty("lms.verbosity","0").toInt
66
val sourceinfo = System.getProperty("lms.sourceinfo","0").toInt
77
val addControlDeps = System.getProperty("lms.controldeps","true").toBoolean
8+
9+
val scalaExplicitTypes = System.getProperty("lms.scala.explicitTypes","false").toBoolean
810

911
// memory management type for C++ target (refcnt or gc)
1012
val cppMemMgr = System.getProperty("lms.cpp.memmgr","malloc")

src/internal/ScalaCodegen.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ trait ScalaCodegen extends GenericCodegen with Config {
9595
val context = sym.pos(0)
9696
" // " + relativePath(context.fileName) + ":" + context.line
9797
}
98-
stream.println("val " + quote(sym) + " = " + rhs + extra)
98+
val typeSignature = if (scalaExplicitTypes) ": " + remap(sym.tp) else ""
99+
stream.println("val " + quote(sym) + typeSignature + " = " + rhs + extra)
99100
}
100101

101102
def emitVarDef(sym: Sym[Variable[Any]], rhs: String): Unit = {

test-out/epfl/test12-array-seq-creation.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*******************************************/
44
class IntArrayCreation extends ((Int)=>(Unit)) {
55
def apply(x0:Int): Unit = {
6-
val x1 = Array(1,2,3)
6+
val x1 = Array[Int](1,2,3)
77
val x2 = x1(0)
88
val x3 = println(x2)
99
x3
@@ -17,7 +17,7 @@ x3
1717
*******************************************/
1818
class CharArrayCreation extends ((Int)=>(Unit)) {
1919
def apply(x5:Int): Unit = {
20-
val x6 = Array('a','b','c')
20+
val x6 = Array[Char]('a','b','c')
2121
val x7 = x6(0)
2222
val x8 = println(x7)
2323
x8

test-out/epfl/test2-fft3.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ val x15 = x3-x7
2727
val x24 = x12-x15
2828
val x25 = x11-x16
2929
val x26 = x12+x15
30-
val x27 = Array(x17,x18,x23,x24,x19,x20,x25,x26)
30+
val x27 = Array[Double](x17,x18,x23,x24,x19,x20,x25,x26)
3131
x27
3232
}
3333
}

test-src/epfl/test1-arith/Arrays.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ trait Arrays extends Base {
2222
trait ArraysExp extends Arrays with BaseExp {
2323
case class ArrayApply[T:Manifest](x:Rep[Array[T]], i:Int) extends Def[T]
2424
//case class ArrayUpdate[T](x:Rep[Array[T]], i:Int) extends Def[T]
25-
case class MakeArray[T:Manifest](x:List[Rep[T]]) extends Def[Array[T]]
25+
case class MakeArray[T](x:List[Rep[T]])(implicit val m: Manifest[T]) extends Def[Array[T]]
2626

2727
def arrayApply[T:Manifest](x: Rep[Array[T]], i:Int) = ArrayApply(x, i)
2828
//def arrayUpdate(x: Rep[Double]) = ArrayUpdate(x)
@@ -34,8 +34,8 @@ trait ScalaGenArrays extends ScalaGenBase {
3434
import IR._
3535

3636
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
37-
case ArrayApply(x,i) => emitValDef(sym, "" + quote(x) + ".apply(" + i + ")")
38-
case MakeArray(x) => emitValDef(sym, "Array(" + x.map(quote).mkString(",") + ")")
37+
case ArrayApply(x,i) => emitValDef(sym, src"$x.apply(${i.toString})")
38+
case a @ MakeArray(x) => emitValDef(sym, src"Array[${a.m}]($x)")
3939
case _ => super.emitNode(sym, rhs)
4040
}
4141
}

0 commit comments

Comments
 (0)