{-# LANGUAGE GADTs #-}

-- | Translate Copilot Core expressions and operators to C99.
module Copilot.Compile.C99.Translate where

import Control.Monad.State

import Copilot.Core
import Copilot.Compile.C99.Util

import qualified Language.C99.Simple as C

-- | Translates a Copilot Core expression into a C99 expression.
transexpr :: Expr a -> State FunEnv C.Expr
transexpr :: Expr a -> State FunEnv Expr
transexpr (Const ty :: Type a
ty x :: a
x) = Expr -> State FunEnv Expr
forall (m :: * -> *) a. Monad m => a -> m a
return (Expr -> State FunEnv Expr) -> Expr -> State FunEnv Expr
forall a b. (a -> b) -> a -> b
$ Type a -> a -> Expr
forall a. Type a -> a -> Expr
constty Type a
ty a
x

transexpr (Local ty1 :: Type a1
ty1 _ name :: Name
name e1 :: Expr a1
e1 e2 :: Expr a
e2) = do
  Expr
e1' <- Expr a1 -> State FunEnv Expr
forall a. Expr a -> State FunEnv Expr
transexpr Expr a1
e1
  let cty1 :: Type
cty1 = Type a1 -> Type
forall a. Type a -> Type
transtype Type a1
ty1
      init :: Maybe Init
init = Init -> Maybe Init
forall a. a -> Maybe a
Just (Init -> Maybe Init) -> Init -> Maybe Init
forall a b. (a -> b) -> a -> b
$ Expr -> Init
C.InitExpr Expr
e1'
  FunEnv -> State FunEnv ()
forall m. Monoid m => m -> State m ()
statetell ([Maybe StorageSpec -> Type -> Name -> Maybe Init -> Decln
C.VarDecln Maybe StorageSpec
forall a. Maybe a
Nothing Type
cty1 Name
name Maybe Init
init], [])

  Expr a -> State FunEnv Expr
forall a. Expr a -> State FunEnv Expr
transexpr Expr a
e2

transexpr (Var _ n :: Name
n) = Expr -> State FunEnv Expr
forall (m :: * -> *) a. Monad m => a -> m a
return (Expr -> State FunEnv Expr) -> Expr -> State FunEnv Expr
forall a b. (a -> b) -> a -> b
$ Name -> Expr
C.Ident Name
n

transexpr (Drop _ amount :: DropIdx
amount sid :: Id
sid) = do
  let var :: Name
var    = Id -> Name
streamname Id
sid
      indexvar :: Name
indexvar = Id -> Name
indexname Id
sid
      index :: Expr
index  = case DropIdx
amount of
        0 -> Name -> Expr
C.Ident Name
indexvar
        n :: DropIdx
n -> Name -> Expr
C.Ident Name
indexvar Expr -> Expr -> Expr
C..+ Integer -> Expr
C.LitInt (DropIdx -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral DropIdx
n)
  Expr -> State FunEnv Expr
forall (m :: * -> *) a. Monad m => a -> m a
return (Expr -> State FunEnv Expr) -> Expr -> State FunEnv Expr
forall a b. (a -> b) -> a -> b
$ Expr -> Expr -> Expr
C.Index (Name -> Expr
C.Ident Name
var) Expr
index

transexpr (ExternVar _ name :: Name
name _) = Expr -> State FunEnv Expr
forall (m :: * -> *) a. Monad m => a -> m a
return (Expr -> State FunEnv Expr) -> Expr -> State FunEnv Expr
forall a b. (a -> b) -> a -> b
$ Name -> Expr
C.Ident (Name -> Name
excpyname Name
name)

transexpr (Label _ _ e :: Expr a
e) = Expr a -> State FunEnv Expr
forall a. Expr a -> State FunEnv Expr
transexpr Expr a
e -- ignore label

transexpr (Op1 op :: Op1 a1 a
op e :: Expr a1
e) = do
  Expr
e' <- Expr a1 -> State FunEnv Expr
forall a. Expr a -> State FunEnv Expr
transexpr Expr a1
e
  Expr -> State FunEnv Expr
forall (m :: * -> *) a. Monad m => a -> m a
return (Expr -> State FunEnv Expr) -> Expr -> State FunEnv Expr
forall a b. (a -> b) -> a -> b
$ Op1 a1 a -> Expr -> Expr
forall a b. Op1 a b -> Expr -> Expr
transop1 Op1 a1 a
op Expr
e'

transexpr (Op2 op :: Op2 a1 b a
op e1 :: Expr a1
e1 e2 :: Expr b
e2) = do
  Expr
e1' <- Expr a1 -> State FunEnv Expr
forall a. Expr a -> State FunEnv Expr
transexpr Expr a1
e1
  Expr
e2' <- Expr b -> State FunEnv Expr
forall a. Expr a -> State FunEnv Expr
transexpr Expr b
e2
  Expr -> State FunEnv Expr
forall (m :: * -> *) a. Monad m => a -> m a
return (Expr -> State FunEnv Expr) -> Expr -> State FunEnv Expr
forall a b. (a -> b) -> a -> b
$ Op2 a1 b a -> Expr -> Expr -> Expr
forall a b c. Op2 a b c -> Expr -> Expr -> Expr
transop2 Op2 a1 b a
op Expr
e1' Expr
e2'

transexpr (Op3 op :: Op3 a1 b c a
op e1 :: Expr a1
e1 e2 :: Expr b
e2 e3 :: Expr c
e3) = do
  Expr
e1' <- Expr a1 -> State FunEnv Expr
forall a. Expr a -> State FunEnv Expr
transexpr Expr a1
e1
  Expr
e2' <- Expr b -> State FunEnv Expr
forall a. Expr a -> State FunEnv Expr
transexpr Expr b
e2
  Expr
e3' <- Expr c -> State FunEnv Expr
forall a. Expr a -> State FunEnv Expr
transexpr Expr c
e3
  Expr -> State FunEnv Expr
forall (m :: * -> *) a. Monad m => a -> m a
return (Expr -> State FunEnv Expr) -> Expr -> State FunEnv Expr
forall a b. (a -> b) -> a -> b
$ Op3 a1 b c a -> Expr -> Expr -> Expr -> Expr
forall a b c d. Op3 a b c d -> Expr -> Expr -> Expr -> Expr
transop3 Op3 a1 b c a
op Expr
e1' Expr
e2' Expr
e3'


-- | Translates a Copilot unary operator and its argument into a C99
-- expression.
transop1 :: Op1 a b -> C.Expr -> C.Expr
transop1 :: Op1 a b -> Expr -> Expr
transop1 op :: Op1 a b
op e :: Expr
e = case Op1 a b
op of
  Not             -> Expr -> Expr
(C..!) Expr
e
  Abs      _      -> Name -> [Expr] -> Expr
funcall "abs"      [Expr
e]
  Sign     _      -> Name -> [Expr] -> Expr
funcall "copysign" [Double -> Expr
C.LitDouble 1.0, Expr
e]
  Recip    _      -> Double -> Expr
C.LitDouble 1.0 Expr -> Expr -> Expr
C../ Expr
e
  Exp      _      -> Name -> [Expr] -> Expr
funcall "exp"   [Expr
e]
  Sqrt     _      -> Name -> [Expr] -> Expr
funcall "sqrt"  [Expr
e]
  Log      _      -> Name -> [Expr] -> Expr
funcall "log"   [Expr
e]
  Sin      _      -> Name -> [Expr] -> Expr
funcall "sin"   [Expr
e]
  Tan      _      -> Name -> [Expr] -> Expr
funcall "tan"   [Expr
e]
  Cos      _      -> Name -> [Expr] -> Expr
funcall "cos"   [Expr
e]
  Asin     _      -> Name -> [Expr] -> Expr
funcall "asin"  [Expr
e]
  Atan     _      -> Name -> [Expr] -> Expr
funcall "atan"  [Expr
e]
  Acos     _      -> Name -> [Expr] -> Expr
funcall "acos"  [Expr
e]
  Sinh     _      -> Name -> [Expr] -> Expr
funcall "sinh"  [Expr
e]
  Tanh     _      -> Name -> [Expr] -> Expr
funcall "tanh"  [Expr
e]
  Cosh     _      -> Name -> [Expr] -> Expr
funcall "cosh"  [Expr
e]
  Asinh    _      -> Name -> [Expr] -> Expr
funcall "asinh" [Expr
e]
  Atanh    _      -> Name -> [Expr] -> Expr
funcall "atanh" [Expr
e]
  Acosh    _      -> Name -> [Expr] -> Expr
funcall "acosh" [Expr
e]
  BwNot    _      -> Expr -> Expr
(C..~) Expr
e
  Cast     _ ty :: Type b
ty  -> TypeName -> Expr -> Expr
C.Cast (Type b -> TypeName
forall a. Type a -> TypeName
transtypename Type b
ty) Expr
e
  GetField (Struct _)  _ f :: a -> Field s b
f -> Expr -> Name -> Expr
C.Dot Expr
e ((a -> Field s b) -> Name
forall a (s :: Symbol) t.
(Struct a, KnownSymbol s) =>
(a -> Field s t) -> Name
accessorname a -> Field s b
f)

-- | Translates a Copilot binary operator and its arguments into a C99
-- expression.
transop2 :: Op2 a b c -> C.Expr -> C.Expr -> C.Expr
transop2 :: Op2 a b c -> Expr -> Expr -> Expr
transop2 op :: Op2 a b c
op e1 :: Expr
e1 e2 :: Expr
e2 = case Op2 a b c
op of
  And          -> Expr
e1 Expr -> Expr -> Expr
C..&& Expr
e2
  Or           -> Expr
e1 Expr -> Expr -> Expr
C..|| Expr
e2
  Add      _   -> Expr
e1 Expr -> Expr -> Expr
C..+  Expr
e2
  Sub      _   -> Expr
e1 Expr -> Expr -> Expr
C..-  Expr
e2
  Mul      _   -> Expr
e1 Expr -> Expr -> Expr
C..*  Expr
e2
  Mod      _   -> Expr
e1 Expr -> Expr -> Expr
C..%  Expr
e2
  Div      _   -> Expr
e1 Expr -> Expr -> Expr
C../  Expr
e2
  Fdiv     _   -> Expr
e1 Expr -> Expr -> Expr
C../  Expr
e2
  Pow      _   -> Name -> [Expr] -> Expr
funcall "pow" [Expr
e1, Expr
e2]
  Logb     _   -> Name -> [Expr] -> Expr
funcall "log" [Expr
e2] Expr -> Expr -> Expr
C../ Name -> [Expr] -> Expr
funcall "log" [Expr
e1]
  Eq       _   -> Expr
e1 Expr -> Expr -> Expr
C..== Expr
e2
  Ne       _   -> Expr
e1 Expr -> Expr -> Expr
C..!= Expr
e2
  Le       _   -> Expr
e1 Expr -> Expr -> Expr
C..<= Expr
e2
  Ge       _   -> Expr
e1 Expr -> Expr -> Expr
C..>= Expr
e2
  Lt       _   -> Expr
e1 Expr -> Expr -> Expr
C..<  Expr
e2
  Gt       _   -> Expr
e1 Expr -> Expr -> Expr
C..>  Expr
e2
  BwAnd    _   -> Expr
e1 Expr -> Expr -> Expr
C..&  Expr
e2
  BwOr     _   -> Expr
e1 Expr -> Expr -> Expr
C..|  Expr
e2
  BwXor    _   -> Expr
e1 Expr -> Expr -> Expr
C..^  Expr
e2
  BwShiftL _ _ -> Expr
e1 Expr -> Expr -> Expr
C..<< Expr
e2
  BwShiftR _ _ -> Expr
e1 Expr -> Expr -> Expr
C..>> Expr
e2
  Index    _   -> Expr -> Expr -> Expr
C.Index Expr
e1 Expr
e2

-- | Translates a Copilot ternary operator and its arguments into a C99
-- expression.
transop3 :: Op3 a b c d -> C.Expr -> C.Expr -> C.Expr -> C.Expr
transop3 :: Op3 a b c d -> Expr -> Expr -> Expr -> Expr
transop3 op :: Op3 a b c d
op e1 :: Expr
e1 e2 :: Expr
e2 e3 :: Expr
e3 = case Op3 a b c d
op of
  Mux _ -> Expr -> Expr -> Expr -> Expr
C.Cond Expr
e1 Expr
e2 Expr
e3

-- | Transform a Copilot Core literal, based on its value and type, into a C99
-- literal.
constty :: Type a -> a -> C.Expr
constty :: Type a -> a -> Expr
constty ty :: Type a
ty = case Type a
ty of
  Bool   -> a -> Expr
Bool -> Expr
C.LitBool
  Int8   -> Type a -> Expr -> Expr
forall a. Type a -> Expr -> Expr
explicitty Type a
ty (Expr -> Expr) -> (a -> Expr) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt (Integer -> Expr) -> (a -> Integer) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  Int16  -> Type a -> Expr -> Expr
forall a. Type a -> Expr -> Expr
explicitty Type a
ty (Expr -> Expr) -> (a -> Expr) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt (Integer -> Expr) -> (a -> Integer) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  Int32  -> Type a -> Expr -> Expr
forall a. Type a -> Expr -> Expr
explicitty Type a
ty (Expr -> Expr) -> (a -> Expr) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt (Integer -> Expr) -> (a -> Integer) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  Int64  -> Type a -> Expr -> Expr
forall a. Type a -> Expr -> Expr
explicitty Type a
ty (Expr -> Expr) -> (a -> Expr) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt (Integer -> Expr) -> (a -> Integer) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  Word8  -> Type a -> Expr -> Expr
forall a. Type a -> Expr -> Expr
explicitty Type a
ty (Expr -> Expr) -> (a -> Expr) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt (Integer -> Expr) -> (a -> Integer) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  Word16 -> Type a -> Expr -> Expr
forall a. Type a -> Expr -> Expr
explicitty Type a
ty (Expr -> Expr) -> (a -> Expr) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt (Integer -> Expr) -> (a -> Integer) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  Word32 -> Type a -> Expr -> Expr
forall a. Type a -> Expr -> Expr
explicitty Type a
ty (Expr -> Expr) -> (a -> Expr) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt (Integer -> Expr) -> (a -> Integer) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  Word64 -> Type a -> Expr -> Expr
forall a. Type a -> Expr -> Expr
explicitty Type a
ty (Expr -> Expr) -> (a -> Expr) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt (Integer -> Expr) -> (a -> Integer) -> a -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral
  Float  -> Type a -> Expr -> Expr
forall a. Type a -> Expr -> Expr
explicitty Type a
ty (Expr -> Expr) -> (Float -> Expr) -> Float -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Expr
C.LitFloat
  Double -> Type a -> Expr -> Expr
forall a. Type a -> Expr -> Expr
explicitty Type a
ty (Expr -> Expr) -> (Double -> Expr) -> Double -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Expr
C.LitDouble
  Struct _ -> \v :: a
v -> TypeName -> [Init] -> Expr
C.InitVal (Type a -> TypeName
forall a. Type a -> TypeName
transtypename Type a
ty) ((Value a -> Init) -> [Value a] -> [Init]
forall a b. (a -> b) -> [a] -> [b]
map Value a -> Init
forall a. Value a -> Init
fieldinit (a -> [Value a]
forall a. Struct a => a -> [Value a]
toValues a
v))
    where
      fieldinit :: Value a -> Init
fieldinit (Value ty :: Type t
ty (Field val :: t
val)) = Expr -> Init
C.InitExpr (Expr -> Init) -> Expr -> Init
forall a b. (a -> b) -> a -> b
$ Type t -> t -> Expr
forall a. Type a -> a -> Expr
constty Type t
ty t
val
  Array ty' :: Type t
ty' -> \v :: a
v -> TypeName -> [Init] -> Expr
C.InitVal (Type a -> TypeName
forall a. Type a -> TypeName
transtypename Type a
ty) (Array n t -> [Init]
vals a
Array n t
v)
    where
      vals :: Array n t -> [Init]
vals v :: Array n t
v = Type t -> [t] -> [Init]
forall a. Type a -> [a] -> [Init]
constarray Type t
ty' (Array n t -> [t]
forall (n :: Nat) a. Array n a -> [a]
arrayelems Array n t
v)

      constarray :: Type a -> [a] -> [C.Init]
      constarray :: Type a -> [a] -> [Init]
constarray ty :: Type a
ty xs :: [a]
xs = case Type a
ty of
        Array ty' :: Type t
ty' -> Type t -> [t] -> [Init]
forall a. Type a -> [a] -> [Init]
constarray Type t
ty' ((Array n t -> [t]) -> [Array n t] -> [t]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Array n t -> [t]
forall (n :: Nat) a. Array n a -> [a]
arrayelems [a]
[Array n t]
xs)
        _         -> (a -> Init) -> [a] -> [Init]
forall a b. (a -> b) -> [a] -> [b]
map (Expr -> Init
C.InitExpr (Expr -> Init) -> (a -> Expr) -> a -> Init
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type a -> a -> Expr
forall a. Type a -> a -> Expr
constty Type a
ty) [a]
xs


-- | Explicitly cast a C99 value to a type.
explicitty :: Type a -> C.Expr -> C.Expr
explicitty :: Type a -> Expr -> Expr
explicitty ty :: Type a
ty = TypeName -> Expr -> Expr
C.Cast (Type a -> TypeName
forall a. Type a -> TypeName
transtypename Type a
ty)

-- | Translate a Copilot type to a C99 type.
transtype :: Type a -> C.Type
transtype :: Type a -> Type
transtype ty :: Type a
ty = case Type a
ty of
  Bool      -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName "bool"
  Int8      -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName "int8_t"
  Int16     -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName "int16_t"
  Int32     -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName "int32_t"
  Int64     -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName "int64_t"
  Word8     -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName "uint8_t"
  Word16    -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName "uint16_t"
  Word32    -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName "uint32_t"
  Word64    -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName "uint64_t"
  Float     -> TypeSpec -> Type
C.TypeSpec TypeSpec
C.Float
  Double    -> TypeSpec -> Type
C.TypeSpec TypeSpec
C.Double
  Array ty' :: Type t
ty' -> Type -> Maybe Expr -> Type
C.Array (Type t -> Type
forall a. Type a -> Type
transtype Type t
ty') Maybe Expr
length where
    length :: Maybe Expr
length = Expr -> Maybe Expr
forall a. a -> Maybe a
Just (Expr -> Maybe Expr) -> Expr -> Maybe Expr
forall a b. (a -> b) -> a -> b
$ Integer -> Expr
C.LitInt (Integer -> Expr) -> Integer -> Expr
forall a b. (a -> b) -> a -> b
$ Id -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Id -> Integer) -> Id -> Integer
forall a b. (a -> b) -> a -> b
$ Type (Array n t) -> Id
forall (n :: Nat) t. KnownNat n => Type (Array n t) -> Id
tylength Type a
Type (Array n t)
ty
  Struct s :: a
s  -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.Struct (a -> Name
forall a. Struct a => a -> Name
typename a
s)

-- | Translate a Copilot type intro a C typename
transtypename :: Type a -> C.TypeName
transtypename :: Type a -> TypeName
transtypename ty :: Type a
ty = Type -> TypeName
C.TypeName (Type -> TypeName) -> Type -> TypeName
forall a b. (a -> b) -> a -> b
$ Type a -> Type
forall a. Type a -> Type
transtype Type a
ty