Skip to content

Commit

Permalink
Merge pull request #322 from Yoitsumi/topic/fix-varargs
Browse files Browse the repository at this point in the history
Fix c-style varargs accepting only single parameter
  • Loading branch information
densh committed Oct 13, 2016
2 parents 1fc82f2 + 4812341 commit c857f6a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
6 changes: 5 additions & 1 deletion sandbox/Test.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import scalanative.native._, stdlib._, stdio._

object Test {
def main(args: Array[String]): Unit = {
println("Hello, world!")
val W = 800
val H = 600
fprintf(stdout, c"P3\n%d %d\n%d\n", W, H, 255)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,20 @@ class GenTextualLLVM(assembly: Seq[Defn]) extends GenShow(assembly) {

def showCallArgs(args: Seq[Arg],
vals: Seq[Val]): (Seq[Show.Result], Seq[Show.Result]) = {
val res = (args zip vals) map {
case (Arg(Type.Ptr, Some(PassConv.Byval(pointee))), v) =>
val res = args.map(Some(_)).zipAll(vals, None, Val.None) map {
case (_, Val.None) => (Seq.empty, Seq.empty)
case (Some(Arg(Type.Ptr, Some(PassConv.Byval(pointee)))), v) =>
val bitcasted = fresh()
(Seq(sh"%$bitcasted = bitcast $v to $pointee*"),
sh"$pointee* %$bitcasted")
case (Arg(Type.Ptr, Some(PassConv.Sret(pointee))), v) =>
Seq(sh"$pointee* %$bitcasted"))
case (Some(Arg(Type.Ptr, Some(PassConv.Sret(pointee)))), v) =>
val bitcasted = fresh()
(Seq(sh"%$bitcasted = bitcast $v to $pointee*"),
sh"$pointee* %$bitcasted")
case (Arg(_, None), v) => (Seq(), sh"$v")
case _ => unsupported()
Seq(sh"$pointee* %$bitcasted"))
case (Some(Arg(_, None)) | None, v) => (Seq(), Seq(sh"$v"))
case _ => unsupported()
}
(res.flatMap(_._1), res.map(_._2))
(res.flatMap(_._1), res.flatMap(_._2))
}

insts.foreach { inst =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package scala.scalanative.native

import stdio._

object CInteropSuite extends tests.Suite {

test("varargs") {
val buff = stackalloc[CChar](64)
sprintf(buff, c"%d %d %d", 1, 2, 3)
for ((c, i) <- "1 2 3".zipWithIndex) {
assert(buff(i) == c)
}
}

}
1 change: 1 addition & 0 deletions unit-tests/src/main/scala/tests/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ object Main {
java.lang.DoubleSuite,
java.util.RandomSuite,
scala.scalanative.native.CStringSuite,
scala.scalanative.native.CInteropSuite,
scala.ArrayIntCopySuite,
scala.ArrayDoubleCopySuite,
scala.ArrayObjectCopySuite
Expand Down

0 comments on commit c857f6a

Please sign in to comment.