Skip to content

Instantly share code, notes, and snippets.

@satabin
Created April 28, 2020 16:07
Show Gist options
  • Select an option

  • Save satabin/0a2bd9d28d606b955eb2069eaf9e7db9 to your computer and use it in GitHub Desktop.

Select an option

Save satabin/0a2bd9d28d606b955eb2069eaf9e7db9 to your computer and use it in GitHub Desktop.
package swam
package runtime
import util.pretty._
import cfg._
import cfg.dot._
import decompilation._
import cats.effect._
import cats.implicits._
import io.odin._
import fs2._
import java.nio.file.Paths
import java.nio.file.Path
import swam.runtime.wasi.Core
import java.util.concurrent.TimeUnit
object Main extends IOApp {
def write(cfg: CFG, idx: Int, blocker: Blocker): IO[Unit] =
Stream.emits(cfg.show.getBytes()).through(io.file.writeAll[IO](Paths.get(s"cfg-$idx.dot"), blocker)).compile.drain
def decompile(path: Path, blocker: Blocker, decompiler: Decompiler[IO]): IO[Unit] =
decompiler.decompilePath(path, blocker).flatMap { doc =>
Stream
.emits(doc.pretty.render(10).getBytes())
.through(io.file.writeAll[IO](Paths.get(s"decompiled.wat"), blocker))
.compile
.drain
}
val logger = consoleLogger[IO]()
def run(args: List[String]): IO[ExitCode] =
Blocker[IO].use { blocker =>
Memory[IO](0).flatMap { mem =>
Core[IO](args.toVector, mem, logger, blocker).use { wasi =>
for {
tdecompiler <- TextDecompiler[IO](blocker)
engine <- Engine[IO](blocker)
_ <- decompile(Paths.get("readdir.wasm"), blocker, tdecompiler)
startCompile <- Clock[IO].realTime(TimeUnit.MILLISECONDS)
mod <- engine.compile(Paths.get("readdir.wasm"), blocker)
endCompile <- Clock[IO].realTime(TimeUnit.MILLISECONDS)
_ <- IO(println(s"Compiled in ${endCompile - startCompile}ms"))
startInst <- Clock[IO].realTime(TimeUnit.MILLISECONDS)
inst <- mod.importing("wasi_snapshot_preview1", wasi).instantiate
endInst <- Clock[IO].realTime(TimeUnit.MILLISECONDS)
_ <- IO(println(s"Instantiated in ${endInst - startInst}ms"))
} yield ExitCode.Success
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment