{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}

-- | Compile Futhark to sequential imperative code.
module Futhark.CodeGen.ImpGen.Sequential
  ( compileProg,
    ImpGen.Warnings,
  )
where

import qualified Futhark.CodeGen.ImpCode.Sequential as Imp
import qualified Futhark.CodeGen.ImpGen as ImpGen
import Futhark.IR.SeqMem
import Futhark.MonadFreshNames

-- | Compile a 'SeqMem' program to sequential imperative code.
compileProg :: MonadFreshNames m => Prog SeqMem -> m (ImpGen.Warnings, Imp.Program)
compileProg :: forall (m :: * -> *).
MonadFreshNames m =>
Prog SeqMem -> m (Warnings, Program)
compileProg = ()
-> Operations SeqMem () Sequential
-> Space
-> Prog SeqMem
-> m (Warnings, Program)
forall rep op (m :: * -> *) r.
(Mem rep, FreeIn op, MonadFreshNames m) =>
r
-> Operations rep r op
-> Space
-> Prog rep
-> m (Warnings, Definitions op)
ImpGen.compileProg () Operations SeqMem () Sequential
forall {r}. Operations SeqMem r Sequential
ops Space
Imp.DefaultSpace
  where
    ops :: Operations SeqMem r Sequential
ops = OpCompiler SeqMem r Sequential -> Operations SeqMem r Sequential
forall rep op r.
(Mem rep, FreeIn op) =>
OpCompiler rep r op -> Operations rep r op
ImpGen.defaultOperations OpCompiler SeqMem r Sequential
forall {rep} {r} {op}.
(AllocOp (Op rep), ASTRep rep, OpReturns rep,
 LetDec rep ~ LParamMem, LParamInfo rep ~ LParamMem,
 RetType rep ~ RetTypeMem, FParamInfo rep ~ FParamMem,
 BranchType rep ~ BranchTypeMem) =>
PatternT (LetDec rep) -> MemOp () -> ImpM rep r op ()
opCompiler
    opCompiler :: PatternT (LetDec rep) -> MemOp () -> ImpM rep r op ()
opCompiler PatternT (LetDec rep)
dest (Alloc SubExp
e Space
space) =
      PatternT (LetDec rep) -> SubExp -> Space -> ImpM rep r op ()
forall rep r op.
Mem rep =>
Pattern rep -> SubExp -> Space -> ImpM rep r op ()
ImpGen.compileAlloc PatternT (LetDec rep)
dest SubExp
e Space
space
    opCompiler PatternT (LetDec rep)
_ (Inner ()) = () -> ImpM rep r op ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()