Skip to content

Commit

Permalink
separate out Config from Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
rachitnigam committed May 15, 2019
1 parent 6a2f0e2 commit 0b72f76
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fuselang

import scala.util.{Try, Success, Failure}
import java.nio.file.Path
import Utils._
import Configuration._

object Compiler {

Expand Down
34 changes: 34 additions & 0 deletions src/main/scala/Configuration.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fuselang

import java.io.File

import backend.{VivadoBackend, CppRunnable, Backend}

object Configuration {

sealed trait Mode
final case object Compile extends Mode
final case object Run extends Mode

val emptyConf = Config(null)

val validBackends = Set("vivado", "c++")

def toBackend(str: String): Backend = str match {
case "vivado" => VivadoBackend
case "c++" => CppRunnable
case b => throw CompilerError.Impossible("toBackend", s"Unknown backend $b")
}

case class Config(
srcFile: File, // Required: Name of the source file
kernelName: String = "kernel", // Name of the kernel to emit
output: Option[String] = None, // Name of output file.
backend: Backend = VivadoBackend, // Backend used for code generation
mode: Mode = Compile, // Compilation mode
compilerOpts: List[String] = List(), // Extra options to the generateExec Compiler
header: Boolean = false, // Generate a header
logLevel: scribe.Level = scribe.Level.Info
)

}
2 changes: 1 addition & 1 deletion src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package fuselang
import java.nio.file.{Files, Path}
import java.io.File

import Utils._
import Configuration._
import Compiler._

object Main {
Expand Down
30 changes: 0 additions & 30 deletions src/main/scala/Utils.scala
Original file line number Diff line number Diff line change
@@ -1,37 +1,7 @@
package fuselang

import java.io.File
import scribe.Level

import backend.{VivadoBackend, CppRunnable, Backend}

object Utils {

sealed trait Mode
final case object Compile extends Mode
final case object Run extends Mode

val emptyConf = Config(null)

val validBackends = Set("vivado", "c++")

def toBackend(str: String): Backend = str match {
case "vivado" => VivadoBackend
case "c++" => CppRunnable
case b => throw CompilerError.Impossible("toBackend", s"Unknown backend $b")
}

case class Config(
srcFile: File, // Required: Name of the source file
kernelName: String = "kernel", // Name of the kernel to emit
output: Option[String] = None, // Name of output file.
backend: Backend = VivadoBackend, // Backend used for code generation
mode: Mode = Compile, // Compilation mode
compilerOpts: List[String] = List(),
logLevel: Level = Level.Info,
header: Boolean = false
)

implicit class RichOption[A](opt: Option[A]) {
def getOrThrow[T <: Throwable](except: T) = opt match {
case Some(v) => v
Expand Down
7 changes: 4 additions & 3 deletions src/main/scala/backends/Backend.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package fuselang.backend

import fuselang.CompilerError.BackendError
import fuselang._
import CompilerError.BackendError

/**
* Abstract definition of a Fuse backend.
*/
trait Backend {

def emit(p: fuselang.Syntax.Prog, c: fuselang.Utils.Config): String = {
def emit(p: Syntax.Prog, c: Configuration.Config): String = {
if (c.header && (canGenerateHeader == false)) {
throw BackendError(s"Backend $this does not support header generation.")
}
Expand All @@ -19,7 +20,7 @@ trait Backend {
* program. Assumes that typechecking pass has been done on the AST and
* might use the type decorations added to the nodes.
*/
def emitProg(p: fuselang.Syntax.Prog, c: fuselang.Utils.Config): String
def emitProg(p: Syntax.Prog, c: Configuration.Config): String

/**
* True if the backend can generate header files.
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/backends/CppRunnable.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fuselang.backend

import fuselang.Syntax._
import fuselang.Utils._
import fuselang.Configuration._
import fuselang.CompilerError._

import Cpp._
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/backends/VivadoBackend.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fuselang.backend

import fuselang.Syntax._
import fuselang.Utils._
import fuselang.Configuration._
import fuselang.CompilerError._

import Cpp._
Expand Down Expand Up @@ -57,7 +57,7 @@ private class VivadoBackend extends CppLike {
case TAlias(n) => n
}

def emitProg(p: Prog, c: fuselang.Utils.Config): String = {
def emitProg(p: Prog, c: Config): String = {
val layout =
vsep(p.includes.map(emitInclude)) <@>
vsep(p.defs.map(emitDef)) <@>
Expand Down

0 comments on commit 0b72f76

Please sign in to comment.