{-# LANGUAGE GADTs #-}
module Copilot.Compile.C99.Translate where
import Control.Monad.State
import Copilot.Core
import Copilot.Compile.C99.Error (impossible)
import Copilot.Compile.C99.Util
import qualified Language.C99.Simple as C
transexpr :: Expr a -> State FunEnv C.Expr
transexpr :: Expr a -> State FunEnv Expr
transexpr (Const Type a
ty 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 Type a1
ty1 Type a
_ Name
name Expr a1
e1 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 Type a
_ 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 Type a
_ DropIdx
amount Id
sid) = do
let accessvar :: Name
accessvar = Id -> Name
streamaccessorname Id
sid
index :: Expr
index = Integer -> Expr
C.LitInt (DropIdx -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral DropIdx
amount)
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] -> Expr
funcall Name
accessvar [Expr
index]
transexpr (ExternVar Type a
_ Name
name Maybe [a]
_) = 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 Type a
_ Name
_ Expr a
e) = Expr a -> State FunEnv Expr
forall a. Expr a -> State FunEnv Expr
transexpr Expr a
e
transexpr (Op1 Op1 a1 a
op 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 Op2 a1 b a
op Expr a1
e1 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 Op3 a1 b c a
op Expr a1
e1 Expr b
e2 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'
transop1 :: Op1 a b -> C.Expr -> C.Expr
transop1 :: Op1 a b -> Expr -> Expr
transop1 Op1 a b
op Expr
e =
case Op1 a b
op of
Op1 a b
Not -> Expr -> Expr
(C..!) Expr
e
Abs Type a
ty -> Type a -> Expr -> Expr
forall a. Type a -> Expr -> Expr
transAbs Type a
ty Expr
e
Sign Type a
ty -> Type a -> Expr -> Expr
forall a. Type a -> Expr -> Expr
transSign Type a
ty Expr
e
Recip Type a
ty -> (Type a -> Integer -> Expr
forall a. Type a -> Integer -> Expr
constNumTy Type a
ty Integer
1) Expr -> Expr -> Expr
C../ Expr
e
Acos Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"acos") [Expr
e]
Asin Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"asin") [Expr
e]
Atan Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"atan") [Expr
e]
Cos Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"cos") [Expr
e]
Sin Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"sin") [Expr
e]
Tan Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"tan") [Expr
e]
Acosh Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"acosh") [Expr
e]
Asinh Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"asinh") [Expr
e]
Atanh Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"atanh") [Expr
e]
Cosh Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"cosh") [Expr
e]
Sinh Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"sinh") [Expr
e]
Tanh Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"tanh") [Expr
e]
Exp Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"exp") [Expr
e]
Log Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"log") [Expr
e]
Sqrt Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"sqrt") [Expr
e]
Ceiling Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"ceil") [Expr
e]
Floor Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"floor") [Expr
e]
BwNot Type a
_ -> Expr -> Expr
(C..~) Expr
e
Cast Type a
_ Type b
ty -> TypeName -> Expr -> Expr
C.Cast (Type b -> TypeName
forall a. Type a -> TypeName
transtypename Type b
ty) Expr
e
GetField (Struct a
_) Type b
_ 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)
transop2 :: Op2 a b c -> C.Expr -> C.Expr -> C.Expr
transop2 :: Op2 a b c -> Expr -> Expr -> Expr
transop2 Op2 a b c
op Expr
e1 Expr
e2 = case Op2 a b c
op of
Op2 a b c
And -> Expr
e1 Expr -> Expr -> Expr
C..&& Expr
e2
Op2 a b c
Or -> Expr
e1 Expr -> Expr -> Expr
C..|| Expr
e2
Add Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..+ Expr
e2
Sub Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..- Expr
e2
Mul Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..* Expr
e2
Mod Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..% Expr
e2
Div Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C../ Expr
e2
Fdiv Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C../ Expr
e2
Pow Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"pow") [Expr
e1, Expr
e2]
Logb Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"log") [Expr
e2] Expr -> Expr -> Expr
C../
Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"log") [Expr
e1]
Atan2 Type a
ty -> Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"atan2") [Expr
e1, Expr
e2]
Eq Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..== Expr
e2
Ne Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..!= Expr
e2
Le Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..<= Expr
e2
Ge Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..>= Expr
e2
Lt Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..< Expr
e2
Gt Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..> Expr
e2
BwAnd Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..& Expr
e2
BwOr Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..| Expr
e2
BwXor Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..^ Expr
e2
BwShiftL Type a
_ Type b
_ -> Expr
e1 Expr -> Expr -> Expr
C..<< Expr
e2
BwShiftR Type a
_ Type b
_ -> Expr
e1 Expr -> Expr -> Expr
C..>> Expr
e2
Index Type (Array n c)
_ -> Expr -> Expr -> Expr
C.Index Expr
e1 Expr
e2
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 Op3 a b c d
op Expr
e1 Expr
e2 Expr
e3 = case Op3 a b c d
op of
Mux Type b
_ -> Expr -> Expr -> Expr -> Expr
C.Cond Expr
e1 Expr
e2 Expr
e3
transAbs :: Type a -> C.Expr -> C.Expr
transAbs :: Type a -> Expr -> Expr
transAbs Type a
ty Expr
e
| Type a -> Bool
forall a. Type a -> Bool
typeIsFloating Type a
ty
= Name -> [Expr] -> Expr
funcall (Type a -> Name -> Name
forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"fabs") [Expr
e]
| Bool
otherwise
= (Expr
e Expr -> Expr -> Expr
C..+ (Expr
e Expr -> Expr -> Expr
C..>> Expr
tyBitSizeMinus1)) Expr -> Expr -> Expr
C..^ (Expr
e Expr -> Expr -> Expr
C..>> Expr
tyBitSizeMinus1)
where
tyBitSizeMinus1 :: C.Expr
tyBitSizeMinus1 :: Expr
tyBitSizeMinus1 = case Type a
ty of
Type a
Int8 -> Integer -> Expr
C.LitInt Integer
7
Type a
Int16 -> Integer -> Expr
C.LitInt Integer
15
Type a
Int32 -> Integer -> Expr
C.LitInt Integer
31
Type a
Int64 -> Integer -> Expr
C.LitInt Integer
63
Type a
Word8 -> Integer -> Expr
C.LitInt Integer
7
Type a
Word16 -> Integer -> Expr
C.LitInt Integer
15
Type a
Word32 -> Integer -> Expr
C.LitInt Integer
31
Type a
Word64 -> Integer -> Expr
C.LitInt Integer
63
Type a
_ -> Name -> Name -> Name -> Expr
forall a. Name -> Name -> a
impossible
Name
"transAbs"
Name
"copilot-c99"
Name
"Abs applied to unexpected types."
transSign :: Type a -> C.Expr -> C.Expr
transSign :: Type a -> Expr -> Expr
transSign Type a
ty Expr
e = Expr -> Expr
positiveCase (Expr -> Expr) -> Expr -> Expr
forall a b. (a -> b) -> a -> b
$ Expr -> Expr
negativeCase Expr
e
where
positiveCase :: C.Expr
-> C.Expr
positiveCase :: Expr -> Expr
positiveCase =
Expr -> Expr -> Expr -> Expr
C.Cond (BinaryOp -> Expr -> Expr -> Expr
C.BinaryOp BinaryOp
C.GT Expr
e (Type a -> Integer -> Expr
forall a. Type a -> Integer -> Expr
constNumTy Type a
ty Integer
0)) (Type a -> Integer -> Expr
forall a. Type a -> Integer -> Expr
constNumTy Type a
ty Integer
1)
negativeCase :: C.Expr
-> C.Expr
negativeCase :: Expr -> Expr
negativeCase =
Expr -> Expr -> Expr -> Expr
C.Cond (BinaryOp -> Expr -> Expr -> Expr
C.BinaryOp BinaryOp
C.LT Expr
e (Type a -> Integer -> Expr
forall a. Type a -> Integer -> Expr
constNumTy Type a
ty Integer
0)) (Type a -> Integer -> Expr
forall a. Type a -> Integer -> Expr
constNumTy Type a
ty (-Integer
1))
constty :: Type a -> a -> C.Expr
constty :: Type a -> a -> Expr
constty Type a
ty = case Type a
ty of
Type a
Bool -> a -> Expr
Bool -> Expr
C.LitBool
Type a
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
Type a
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
Type a
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
Type a
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
Type a
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
Type a
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
Type a
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
Type a
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
Type a
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
Type a
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 a
_ -> \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
constfieldinit (a -> [Value a]
forall a. Struct a => a -> [Value a]
toValues a
v))
Array Type t
ty' -> \a
v ->
TypeName -> [Init] -> Expr
C.InitVal (Type a -> TypeName
forall a. Type a -> TypeName
transtypename Type a
ty) (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 a
Array n t
v))
constinit :: Type a -> a -> C.Init
constinit :: Type a -> a -> Init
constinit Type a
ty a
val = case Type a
ty of
Array Type t
ty' -> [Init] -> Init
C.InitArray ([Init] -> Init) -> [Init] -> Init
forall a b. (a -> b) -> a -> b
$ Type t -> [t] -> [Init]
forall a. Type a -> [a] -> [Init]
constarray Type t
ty' ([t] -> [Init]) -> [t] -> [Init]
forall a b. (a -> b) -> a -> b
$ Array n t -> [t]
forall (n :: Nat) a. Array n a -> [a]
arrayelems a
Array n t
val
Struct a
_ -> [Init] -> Init
C.InitArray ([Init] -> Init) -> [Init] -> Init
forall a b. (a -> b) -> a -> b
$ (Value a -> Init) -> [Value a] -> [Init]
forall a b. (a -> b) -> [a] -> [b]
map Value a -> Init
forall a. Value a -> Init
constfieldinit ([Value a] -> [Init]) -> [Value a] -> [Init]
forall a b. (a -> b) -> a -> b
$ a -> [Value a]
forall a. Struct a => a -> [Value a]
toValues a
val
Type a
_ -> Expr -> Init
C.InitExpr (Expr -> Init) -> Expr -> Init
forall a b. (a -> b) -> a -> b
$ Type a -> a -> Expr
forall a. Type a -> a -> Expr
constty Type a
ty a
val
constfieldinit :: Value a -> C.Init
constfieldinit :: Value a -> Init
constfieldinit (Value Type t
ty (Field t
val)) = Type t -> t -> Init
forall a. Type a -> a -> Init
constinit Type t
ty t
val
constarray :: Type a -> [a] -> [C.Init]
constarray :: Type a -> [a] -> [Init]
constarray Type a
ty = (a -> Init) -> [a] -> [Init]
forall a b. (a -> b) -> [a] -> [b]
map (Type a -> a -> Init
forall a. Type a -> a -> Init
constinit Type a
ty)
explicitty :: Type a -> C.Expr -> C.Expr
explicitty :: Type a -> Expr -> Expr
explicitty Type a
ty = TypeName -> Expr -> Expr
C.Cast (Type a -> TypeName
forall a. Type a -> TypeName
transtypename Type a
ty)
transtype :: Type a -> C.Type
transtype :: Type a -> Type
transtype Type a
ty = case Type a
ty of
Type a
Bool -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"bool"
Type a
Int8 -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"int8_t"
Type a
Int16 -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"int16_t"
Type a
Int32 -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"int32_t"
Type a
Int64 -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"int64_t"
Type a
Word8 -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"uint8_t"
Type a
Word16 -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"uint16_t"
Type a
Word32 -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"uint32_t"
Type a
Word64 -> TypeSpec -> Type
C.TypeSpec (TypeSpec -> Type) -> TypeSpec -> Type
forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"uint64_t"
Type a
Float -> TypeSpec -> Type
C.TypeSpec TypeSpec
C.Float
Type a
Double -> TypeSpec -> Type
C.TypeSpec TypeSpec
C.Double
Array 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 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)
transtypename :: Type a -> C.TypeName
transtypename :: Type a -> TypeName
transtypename 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
constNumTy :: Type a -> Integer -> C.Expr
constNumTy :: Type a -> Integer -> Expr
constNumTy Type a
ty =
case Type a
ty of
Type a
Float -> Float -> Expr
C.LitFloat (Float -> Expr) -> (Integer -> Float) -> Integer -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Float
forall a. Num a => Integer -> a
fromInteger
Type a
Double -> Double -> Expr
C.LitDouble (Double -> Expr) -> (Integer -> Double) -> Integer -> Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Double
forall a. Num a => Integer -> a
fromInteger
Type a
_ -> Integer -> Expr
C.LitInt
specializeMathFunName :: Type a -> String -> String
specializeMathFunName :: Type a -> Name -> Name
specializeMathFunName Type a
ty Name
s
| Name -> Bool
isMathFPArgs Name
s
, Type a
Float <- Type a
ty
= Name
s Name -> Name -> Name
forall a. [a] -> [a] -> [a]
++ Name
"f"
| Bool
otherwise
= Name
s
where
isMathFPArgs :: String -> Bool
isMathFPArgs :: Name -> Bool
isMathFPArgs = (Name -> [Name] -> Bool) -> [Name] -> Name -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip Name -> [Name] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem
[ Name
"acos", Name
"asin", Name
"atan", Name
"atan2", Name
"cos", Name
"sin"
, Name
"tan", Name
"acosh", Name
"asinh", Name
"atanh", Name
"cosh", Name
"sinh"
, Name
"tanh", Name
"exp", Name
"exp2", Name
"expm1", Name
"frexp", Name
"ilogb"
, Name
"ldexp", Name
"log", Name
"log10", Name
"log1p", Name
"log2", Name
"logb"
, Name
"modf", Name
"scalbn", Name
"scalbln", Name
"cbrt", Name
"fabs", Name
"hypot"
, Name
"pow", Name
"sqrt", Name
"erf", Name
"erfc", Name
"lgamma", Name
"tgamma"
, Name
"ceil", Name
"floor", Name
"nearbyint", Name
"rint", Name
"lrint", Name
"llrint"
, Name
"round", Name
"lround", Name
"llround", Name
"trunc", Name
"fmod", Name
"remainder"
, Name
"remquo", Name
"copysign", Name
"nextafter", Name
"nexttoward", Name
"fdim"
, Name
"fmax", Name
"fmin", Name
"fma"
]
typeIsFloating :: Type a -> Bool
typeIsFloating :: Type a -> Bool
typeIsFloating Type a
Float = Bool
True
typeIsFloating Type a
Double = Bool
True
typeIsFloating Type a
_ = Bool
False