module Feldspar.Compiler.Compiler
( compile
, standaloneCompile
, icompile
, icompile'
, defaultOptions
, unrollOptions
, noSimplification
, noPrimitiveInstructionHandling
, includeGeneration
) where
import Data.Map
import Feldspar hiding ((++))
import Feldspar.Core.Graph
import Feldspar.Core.Expr (toGraph)
import qualified Feldspar.Core.Expr as Expr
import Feldspar.Compiler.Options
import Feldspar.Compiler.Transformation.GraphToImperative
import Feldspar.Compiler.Transformation.Lifting
import Feldspar.Compiler.Optimization.PrimitiveInstructions
import Feldspar.Compiler.Optimization.Simplification
import Feldspar.Compiler.Optimization.Unroll
import Feldspar.Compiler.Transformation.GraphUtils
import Feldspar.Compiler.Imperative.Representation hiding (Normal)
intro = "#include \"feldspar.h\"\n\n"
type Stage t = (t -> String -> Options -> [ImpFunction])
coreCompile :: (Expr.Program t) => (Stage t -> t -> FilePath -> String -> Options -> IO ())
-> t -> FilePath -> String -> Options -> IO ()
coreCompile write prg fileName funname opts = write stage prg fileName funname opts where
stage :: (Expr.Program t) => t -> String -> Options -> [ImpFunction]
stage = case debug opts of
NoDebug -> stage7
NoSimplification -> stage5
NoPrimitiveInstructionHandling -> stage3
includeGeneration :: FilePath -> IO ()
includeGeneration fileName
= appendFile fileName intro
standaloneWrite stage prg fileName functionName opts
= appendFile fileName $ toC 0 $ stage prg functionName opts
standaloneCompile:: (Expr.Program t) => t -> FilePath -> String -> Options -> IO ()
standaloneCompile prg fileName functionName opts
= coreCompile standaloneWrite prg fileName functionName opts
fileWrite stage prg fileName functionName opts
= writeFile fileName $ intro ++ (toC 0 $ stage prg functionName opts)
compile :: (Expr.Program t) => t -> FilePath -> String -> Options -> IO ()
compile prg fileName functionName opts
= coreCompile fileWrite prg fileName functionName opts
writeOut stage prg fileName functionName opts
= putStrLn $ intro ++ (toC 0 $ stage prg functionName opts)
icompile :: (Expr.Program t) => t -> IO ()
icompile prg
= coreCompile writeOut prg "" "test" defaultOptions
icompile' :: (Expr.Program t) => t -> String -> Options -> IO ()
icompile' prg functionName opts
= coreCompile writeOut prg "" functionName opts
defaultOptions
= Options
{ platform = AnsiC
, unroll = NoUnroll
, debug = NoDebug
}
unrollOptions
= defaultOptions { unroll = Unroll 8 }
noSimplification
= defaultOptions { debug = NoSimplification }
noPrimitiveInstructionHandling
= defaultOptions { debug = NoPrimitiveInstructionHandling }
stage1:: (Expr.Program t) => t -> HierarchicalGraph
stage1 = makeHierarchical . toGraph
stage2:: (Expr.Program t) => t -> HierarchicalGraph
stage2 = replaceNoInlines . stage1
stage3:: (Expr.Program t) => t -> String -> Options -> [ImpFunction]
stage3 prg name opt = graphToImperative name $ stage2 prg
stage4:: (Expr.Program t) => t -> String -> Options -> [ImpFunction]
stage4 prg name opt = handlePrimitives opt $ stage3 prg name opt
stage5:: (Expr.Program t) => t -> String -> Options -> [ImpFunction]
stage5 prg name opt = fst . computeSemInfVar $ stage4 prg name opt
stage6:: (Expr.Program t) => t -> String -> Options -> [ImpFunction]
stage6 prg name opt = doSimplification $ stage5 prg name opt
stage7:: (Expr.Program t) => t -> String -> Options -> [ImpFunction]
stage7 prg name opt = doUnroll opt $ stage6 prg name opt