module Feldspar.Compiler.Imperative.FromCore where
import Data.List (nub)
import Data.Typeable
import Control.Monad.RWS
import Language.Syntactic
import Language.Syntactic.Constructs.Binding
import Language.Syntactic.Constructs.Binding.HigherOrder
import Feldspar.Core.Types
import Feldspar.Core.Interpretation
import Feldspar.Core.Constructs
import Feldspar.Core.Constructs.Literal
import Feldspar.Core.Constructs.Binding
import Feldspar.Core.Frontend
import Feldspar.Range (upperBound)
import qualified Feldspar.Compiler.Imperative.Representation as Rep (Variable(..), Type(..))
import Feldspar.Compiler.Imperative.Representation
( ActualParameter(..)
, Block(..)
, Declaration(..)
, Entity(..)
, Expression(..)
, Module(..)
, Program(..)
)
import Feldspar.Compiler.Imperative.Frontend
import Feldspar.Compiler.Imperative.FromCore.Interpretation
import Feldspar.Compiler.Imperative.FromCore.Array ()
import Feldspar.Compiler.Imperative.FromCore.Binding (compileBind)
import Feldspar.Compiler.Imperative.FromCore.Condition ()
import Feldspar.Compiler.Imperative.FromCore.ConditionM ()
import Feldspar.Compiler.Imperative.FromCore.Error ()
import Feldspar.Compiler.Imperative.FromCore.FFI ()
import Feldspar.Compiler.Imperative.FromCore.Future ()
import Feldspar.Compiler.Imperative.FromCore.Literal ()
import Feldspar.Compiler.Imperative.FromCore.Loop ()
import Feldspar.Compiler.Imperative.FromCore.Mutable ()
import Feldspar.Compiler.Imperative.FromCore.MutableToPure ()
import Feldspar.Compiler.Imperative.FromCore.NoInline ()
import Feldspar.Compiler.Imperative.FromCore.Par ()
import Feldspar.Compiler.Imperative.FromCore.Primitive ()
import Feldspar.Compiler.Imperative.FromCore.Save ()
import Feldspar.Compiler.Imperative.FromCore.SizeProp ()
import Feldspar.Compiler.Imperative.FromCore.Switch ()
import Feldspar.Compiler.Imperative.FromCore.SourceInfo ()
import Feldspar.Compiler.Imperative.FromCore.Tuple ()
import Feldspar.Compiler.Backend.C.Options (Options(..))
instance Compile FeldDom FeldDom
where
compileProgSym (C' a) = compileProgSym a
compileExprSym (C' a) = compileExprSym a
instance Compile Empty dom
where
compileProgSym _ = error "Can't compile Empty"
compileExprSym _ = error "Can't compile Empty"
compileProgTop :: ( Compile dom dom
, Project (CLambda Type) dom
, Project Let dom
, Project (Literal :|| Type) dom
, ConstrainedBy dom Typeable
) =>
Options -> String -> [(VarId, ASTB (Decor Info dom) Type)] ->
ASTF (Decor Info dom) a -> CodeWriter (Rep.Variable ())
compileProgTop opt funname bs (lam :$ body)
| Just (SubConstr2 (Lambda v)) <- prjLambda lam
= do
let ta = argType $ infoType $ getInfo lam
sa = fst $ infoSize $ getInfo lam
typ = compileTypeRep ta sa
arg | Rep.StructType{} <- typ = mkPointer typ v
| otherwise = mkVariable typ v
tell $ mempty {params=[arg]}
withAlias v (varToExpr arg) $
compileProgTop opt funname bs body
compileProgTop opt funname bs (lt :$ e :$ (lam :$ body))
| Just (SubConstr2 (Lambda v)) <- prjLambda lam
, Just Let <- prj lt
, Just (C' Literal{}) <- prjF e
, [ProcedureCall "copy" [ValueParameter (VarExpr vr), ValueParameter (ConstExpr c)]] <- bd
, freshName Prelude.== vName vr
= do tellDef [ValueDef var c]
withAlias v (varToExpr var) $
compileProgTop opt funname bs body
where
info = getInfo e
outType = case compileTypeRep (infoType info) (infoSize info) of
Rep.ArrayType rs t -> Rep.NativeArray (Just $ upperBound rs) t
t -> t
var@(Rep.Variable _ freshName) = case prjLambda lam of
Just (SubConstr2 (Lambda v)) -> mkVariable outType v
bd = sequenceProgs $ blockBody $ block $ snd $
evalRWS (compileProg (Just $ varToExpr var) e) (initReader opt) initState
compileProgTop opt funname bs e@(lt :$ _ :$ _)
| Just Let <- prj lt
, (bs', body) <- collectLetBinders e
= compileProgTop opt funname (reverse bs' ++ bs) body
compileProgTop _ _ bs a = do
let
info = getInfo a
outType = Rep.Pointer $ compileTypeRep (infoType info) (infoSize info)
outParam = Rep.Variable outType "out"
outLoc = varToExpr outParam
mapM_ compileBind (reverse bs)
compileProg (Just outLoc) a
return outParam
fromCore :: SyntacticFeld a => Options -> String -> a -> Module ()
fromCore opt funname prog = Module defs
where
(outParam,results) = evalRWS (compileProgTop opt funname [] ast) (initReader opt) initState
ast = reifyFeld (frontendOpts opt) N32 prog
decls = decl results
ins = params results
post = epilogue results
Block ds p = block results
paramTypes = getTypes opt $ Declaration outParam Nothing:map (`Declaration` Nothing) ins
defs = nub (def results ++ paramTypes)
++ [Proc funname ins [outParam] $ Just (Block (ds ++ decls) (Sequence (p:post)))]
getCore' :: SyntacticFeld a => Options -> a -> Module ()
getCore' opts = fromCore opts "test"