module Feldspar.Compiler.Imperative.FromCore.Literal
(literal)
where
import Data.Complex
import Language.Syntactic
import Feldspar.Core.Types as Core
import Feldspar.Core.Interpretation (Info(..))
import Feldspar.Core.Constructs.Literal
import Feldspar.Compiler.Imperative.Frontend
import Feldspar.Compiler.Imperative.Representation (Expression(..),Constant(..))
import qualified Feldspar.Compiler.Imperative.Representation as Rep (Type(..),
Signedness(..),
Size(..))
import Feldspar.Compiler.Imperative.FromCore.Interpretation
instance Compile (Literal :|| Core.Type) dom
where
compileExprSym (C' (Literal a)) info Nil = literal (infoType info) (infoSize info) a
compileProgSym (C' (Literal a)) info loc Nil = case loc of
Just l -> literalLoc l (infoType info) (infoSize info) a
Nothing -> return ()
literal :: TypeRep a -> Size a -> a -> CodeWriter (Expression ())
literal t@UnitType sz a = return (ConstExpr $ literalConst t sz a)
literal t@BoolType sz a = return (ConstExpr $ literalConst t sz a)
literal t@IntType{} sz a = return (ConstExpr $ literalConst t sz a)
literal t@FloatType sz a = return (ConstExpr $ literalConst t sz a)
literal t@DoubleType sz a = return (ConstExpr $ literalConst t sz a)
literal t@ComplexType{} sz a = return (ConstExpr $ literalConst t sz a)
literal t@ArrayType{} sz a = return (ConstExpr $ literalConst t sz a)
literal t s a = do loc <- freshVar "x" t s
literalLoc loc t s a
return loc
literalConst :: TypeRep a -> Size a -> a -> Constant ()
literalConst UnitType _ () = IntConst 0 (Rep.NumType Rep.Unsigned Rep.S32)
literalConst BoolType _ a = BoolConst a
literalConst trep@IntType{} sz a = IntConst (toInteger a) (compileTypeRep trep sz)
literalConst FloatType _ a = FloatConst a
literalConst DoubleType _ a = DoubleConst a
literalConst (ArrayType t) _ a = ArrayConst $ map (literalConst t (defaultSize t)) a
literalConst (ComplexType t) _ (r:+i) = ComplexConst re ie
where re = literalConst t (defaultSize t) r
ie = literalConst t (defaultSize t) i
literalLoc :: Expression () -> TypeRep a -> Size a -> a -> CodeWriter ()
literalLoc loc arr@ArrayType{} sz e
= tellProg [copyProg (Just loc) [ConstExpr $ literalConst arr sz e]]
literalLoc loc (Tup2Type ta tb) (sa,sb) (a,b) =
do literalLoc (StructField loc "member1") ta sa a
literalLoc (StructField loc "member2") tb sb b
literalLoc loc (Tup3Type ta tb tc) (sa,sb,sc) (a,b,c) =
do literalLoc (StructField loc "member1") ta sa a
literalLoc (StructField loc "member2") tb sb b
literalLoc (StructField loc "member3") tc sc c
literalLoc loc (Tup4Type ta tb tc td) (sa,sb,sc,sd) (a,b,c,d) =
do literalLoc (StructField loc "member1") ta sa a
literalLoc (StructField loc "member2") tb sb b
literalLoc (StructField loc "member3") tc sc c
literalLoc (StructField loc "member4") td sd d
literalLoc loc (Tup5Type ta tb tc td te) (sa,sb,sc,sd,se) (a,b,c,d,e) =
do literalLoc (StructField loc "member1") ta sa a
literalLoc (StructField loc "member2") tb sb b
literalLoc (StructField loc "member3") tc sc c
literalLoc (StructField loc "member4") td sd d
literalLoc (StructField loc "member5") te se e
literalLoc loc (Tup6Type ta tb tc td te tf) (sa,sb,sc,sd,se,sf) (a,b,c,d,e,f) =
do literalLoc (StructField loc "member1") ta sa a
literalLoc (StructField loc "member2") tb sb b
literalLoc (StructField loc "member3") tc sc c
literalLoc (StructField loc "member4") td sd d
literalLoc (StructField loc "member5") te se e
literalLoc (StructField loc "member6") tf sf f
literalLoc loc (Tup7Type ta tb tc td te tf tg) (sa,sb,sc,sd,se,sf,sg) (a,b,c,d,e,f,g) =
do literalLoc (StructField loc "member1") ta sa a
literalLoc (StructField loc "member2") tb sb b
literalLoc (StructField loc "member3") tc sc c
literalLoc (StructField loc "member4") td sd d
literalLoc (StructField loc "member5") te se e
literalLoc (StructField loc "member6") tf sf f
literalLoc (StructField loc "member7") tg sg g
literalLoc loc t sz a =
do rhs <- literal t sz a
assign (Just loc) rhs