packages feed

copilot-c99 3.7 → 3.8

raw patch · 11 files changed

+183/−205 lines, 11 filesdep ~copilot-corePVP ok

version bump matches the API change (PVP)

Dependency ranges changed: copilot-core

API changes (from Hackage documentation)

- Copilot.Compile.C99.CodeGen: exprtypes :: Typeable a => Expr a -> [UType]
- Copilot.Compile.C99.CodeGen: gatherexprs :: [Stream] -> [Trigger] -> [UExpr]
- Copilot.Compile.C99.CodeGen: gendecln :: String -> Type a -> Decln
- Copilot.Compile.C99.CodeGen: genfun :: String -> Expr a -> Type a -> FunDef
- Copilot.Compile.C99.CodeGen: mkaccessdecln :: Id -> Type a -> [a] -> FunDef
- Copilot.Compile.C99.CodeGen: mkbuffdecln :: Id -> Type a -> [a] -> Decln
- Copilot.Compile.C99.CodeGen: mkextcpydecln :: External -> Decln
- Copilot.Compile.C99.CodeGen: mkextdecln :: External -> Decln
- Copilot.Compile.C99.CodeGen: mkindexdecln :: Id -> Decln
- Copilot.Compile.C99.CodeGen: mkstep :: CSettings -> [Stream] -> [Trigger] -> [External] -> FunDef
- Copilot.Compile.C99.CodeGen: mkstructdecln :: Struct a => Type a -> Decln
- Copilot.Compile.C99.CodeGen: mkstructforwdecln :: Struct a => Type a -> Decln
- Copilot.Compile.C99.CodeGen: typetypes :: Typeable a => Type a -> [UType]
- Copilot.Compile.C99.Compile: CSettings :: String -> FilePath -> CSettings
- Copilot.Compile.C99.Compile: [cSettingsOutputDirectory] :: CSettings -> FilePath
- Copilot.Compile.C99.Compile: [cSettingsStepFunctionName] :: CSettings -> String
- Copilot.Compile.C99.Compile: compile :: String -> Spec -> IO ()
- Copilot.Compile.C99.Compile: compileWith :: CSettings -> String -> Spec -> IO ()
- Copilot.Compile.C99.Compile: data CSettings
- Copilot.Compile.C99.Compile: mkDefaultCSettings :: CSettings
- Copilot.Compile.C99.External: External :: String -> String -> Type a -> External
- Copilot.Compile.C99.External: [extcpyname] :: External -> String
- Copilot.Compile.C99.External: [extname] :: External -> String
- Copilot.Compile.C99.External: [exttype] :: External -> Type a
- Copilot.Compile.C99.External: data External
- Copilot.Compile.C99.External: extunion :: [External] -> [External] -> [External]
- Copilot.Compile.C99.External: gatherexts :: [Stream] -> [Trigger] -> [External]
- Copilot.Compile.C99.Settings: CSettings :: String -> FilePath -> CSettings
- Copilot.Compile.C99.Settings: [cSettingsOutputDirectory] :: CSettings -> FilePath
- Copilot.Compile.C99.Settings: [cSettingsStepFunctionName] :: CSettings -> String
- Copilot.Compile.C99.Settings: data CSettings
- Copilot.Compile.C99.Settings: mkDefaultCSettings :: CSettings
- Copilot.Compile.C99.Translate: constty :: Type a -> a -> Expr
- Copilot.Compile.C99.Translate: explicitty :: Type a -> Expr -> Expr
- Copilot.Compile.C99.Translate: transexpr :: Expr a -> State FunEnv Expr
- Copilot.Compile.C99.Translate: transop1 :: Op1 a b -> Expr -> Expr
- Copilot.Compile.C99.Translate: transop2 :: Op2 a b c -> Expr -> Expr -> Expr
- Copilot.Compile.C99.Translate: transop3 :: Op3 a b c d -> Expr -> Expr -> Expr -> Expr
- Copilot.Compile.C99.Translate: transtype :: Type a -> Type
- Copilot.Compile.C99.Translate: transtypename :: Type a -> TypeName
- Copilot.Compile.C99.Util: argname :: String -> Int -> String
- Copilot.Compile.C99.Util: argnames :: String -> [String]
- Copilot.Compile.C99.Util: excpyname :: String -> String
- Copilot.Compile.C99.Util: fresh :: String -> [String] -> String
- Copilot.Compile.C99.Util: funcall :: Ident -> [Expr] -> Expr
- Copilot.Compile.C99.Util: generatorname :: Id -> String
- Copilot.Compile.C99.Util: guardname :: String -> String
- Copilot.Compile.C99.Util: indexname :: Id -> String
- Copilot.Compile.C99.Util: names :: [Decln] -> [String]
- Copilot.Compile.C99.Util: statetell :: Monoid m => m -> State m ()
- Copilot.Compile.C99.Util: streamaccessorname :: Id -> String
- Copilot.Compile.C99.Util: streamname :: Id -> String
- Copilot.Compile.C99.Util: type FunEnv = [Decln]

Files

CHANGELOG view
@@ -1,3 +1,8 @@+2022-03-07+        * Version bump (3.8). (#298)+        * Hide internal modules deprecated in Copilot 3.5. (#289)+        * Mark package as uncurated to avoid modification. (#288)+ 2022-01-07         * Version bump (3.7). (#287)         * Guard against empty specs. (#274)
copilot-c99.cabal view
@@ -1,6 +1,6 @@ cabal-version             : >= 1.10 name                      : copilot-c99-version                   : 3.7+version                   : 3.8 synopsis                  : A compiler for Copilot targeting C99. description               :   This package is a back-end from Copilot to C.@@ -27,6 +27,8 @@                           , Alwyn Goodloe                           , Ivan Perez +x-curation: uncurated+ source-repository head     type:       git     location:   git://github.com/Copilot-Language/copilot.git@@ -48,18 +50,16 @@                           , mtl                 >= 2.2 && < 2.3                           , pretty              >= 1.1 && < 1.2 -                          , copilot-core        >= 3.7   && < 3.8+                          , copilot-core        >= 3.8   && < 3.9                           , language-c99        >= 0.1.1 && < 0.2                           , language-c99-util   >= 0.1.1 && < 0.2                           , language-c99-simple >= 0.1.1 && < 0.2    exposed-modules         : Copilot.Compile.C99-                          , Copilot.Compile.C99.Translate-                          , Copilot.Compile.C99.Util-                          , Copilot.Compile.C99.CodeGen-                          , Copilot.Compile.C99.External-                          , Copilot.Compile.C99.Compile-                          , Copilot.Compile.C99.Settings -  other-modules          : Copilot.Compile.C99.Compile.Internal-                         , Copilot.Compile.C99.Settings.Internal+  other-modules          : Copilot.Compile.C99.Translate+                         , Copilot.Compile.C99.Util+                         , Copilot.Compile.C99.CodeGen+                         , Copilot.Compile.C99.External+                         , Copilot.Compile.C99.Compile+                         , Copilot.Compile.C99.Settings
src/Copilot/Compile/C99.hs view
@@ -6,5 +6,5 @@   , mkDefaultCSettings   ) where -import Copilot.Compile.C99.Compile.Internal-import Copilot.Compile.C99.Settings.Internal+import Copilot.Compile.C99.Compile+import Copilot.Compile.C99.Settings
src/Copilot/Compile/C99/CodeGen.hs view
@@ -2,9 +2,7 @@ {-# LANGUAGE ScopedTypeVariables #-}  -- | High-level translation of Copilot Core into C99.-module Copilot.Compile.C99.CodeGen-  {-# DEPRECATED "This module will be hidden in future versions." #-}-  where+module Copilot.Compile.C99.CodeGen where  import Control.Monad.State  (runState) import Data.List            (union, unzip4)
src/Copilot/Compile/C99/Compile.hs view
@@ -1,11 +1,159 @@ -- | Compile Copilot specifications to C99 code. module Copilot.Compile.C99.Compile-  {-# DEPRECATED "This module will be hidden in future versions." #-}   ( compile   , compileWith-  , CSettings(..)-  , mkDefaultCSettings   ) where -import Copilot.Compile.C99.Compile.Internal-import Copilot.Compile.C99.Settings.Internal+import Text.PrettyPrint     (render)+import Data.List            (nub)+import Data.Maybe           (catMaybes)+import System.Directory     (createDirectoryIfMissing)+import System.Exit          (exitFailure)+import System.FilePath      ((</>))+import System.IO            (hPutStrLn, stderr)++import Language.C99.Pretty  (pretty)+import qualified Language.C99.Simple as C++import Copilot.Core+import Copilot.Compile.C99.Util+import Copilot.Compile.C99.External+import Copilot.Compile.C99.Settings+import Copilot.Compile.C99.Translate+import Copilot.Compile.C99.CodeGen++-- | Compile a specification to a .h and a .c file.+--+-- The first argument is the settings for the C code generated.+--+-- The second argument is used as prefix for the .h and .c files generated.+compileWith :: CSettings -> String -> Spec -> IO ()+compileWith cSettings prefix spec+  | null (specTriggers spec)+  = do hPutStrLn stderr $+         "Copilot error: attempt at compiling empty specification.\n"+         ++ "You must define at least one trigger to generate C monitors."+       exitFailure++  | otherwise+  = do let cfile = render $ pretty $ C.translate $ compilec cSettings spec+           hfile = render $ pretty $ C.translate $ compileh cSettings spec++           -- TODO: find a nicer solution using annotated AST's+           -- Should figure out exactly which headers are needed, based on what+           -- is used.+           cmacros = unlines [ "#include <stdint.h>"+                             , "#include <stdbool.h>"+                             , "#include <string.h>"+                             , "#include <stdlib.h>"+                             , "#include <math.h>"+                             , ""+                             , "#include \"" ++ prefix ++ ".h\""+                             , ""+                             ]++       let dir = cSettingsOutputDirectory cSettings+       createDirectoryIfMissing True dir+       writeFile (dir </> prefix ++ ".c") $ cmacros ++ cfile+       writeFile (dir </> prefix ++ ".h") hfile++-- | Compile a specification to a .h and a .c file.+--+-- The first argument is used as prefix for the .h and .c files generated.+compile :: String -> Spec -> IO ()+compile = compileWith mkDefaultCSettings++-- | Generate the .c file from a 'Spec'.+--+-- The generated C file has the following structure:+--+-- * Include .h file.+-- * Declarations of global buffers and indices.+-- * Generator functions for streams, guards and trigger arguments.+-- * Declaration of the @step()@ function.+compilec :: CSettings -> Spec -> C.TransUnit+compilec cSettings spec = C.TransUnit declns funs where+  streams  = specStreams spec+  triggers = specTriggers spec+  exts     = gatherexts streams triggers+  exprs    = gatherexprs streams triggers++  declns = mkstructdeclns exprs ++ mkexts exts ++ mkglobals streams+  funs   = genfuns streams triggers ++ [mkstep cSettings streams triggers exts]++  -- Write struct datatypes+  mkstructdeclns :: [UExpr] -> [C.Decln]+  mkstructdeclns es = catMaybes $ map mkdecln utypes where+    mkdecln (UType ty) = case ty of+      Struct x -> Just $ mkstructdecln ty+      _        -> Nothing++    utypes = nub $ concatMap (\(UExpr _ e) -> exprtypes e) es++  -- Make declarations for copies of external variables.+  mkexts :: [External] -> [C.Decln]+  mkexts exts = map mkextcpydecln exts++  -- Make buffer and index declarations for streams.+  mkglobals :: [Stream] -> [C.Decln]+  mkglobals streams = map buffdecln streams ++ map indexdecln streams where+    buffdecln  (Stream sid buff _ ty) = mkbuffdecln  sid ty buff+    indexdecln (Stream sid _    _ _ ) = mkindexdecln sid++  -- Make generator functions, including trigger arguments.+  genfuns :: [Stream] -> [Trigger] -> [C.FunDef]+  genfuns streams triggers =  map accessdecln streams+                           ++ map streamgen streams+                           ++ concatMap triggergen triggers where++    accessdecln :: Stream -> C.FunDef+    accessdecln (Stream sid buff _ ty) = mkaccessdecln sid ty buff++    streamgen :: Stream -> C.FunDef+    streamgen (Stream sid _ expr ty) = genfun (generatorname sid) expr ty++    triggergen :: Trigger -> [C.FunDef]+    triggergen (Trigger name guard args) = guarddef : argdefs where+      guarddef = genfun (guardname name) guard Bool+      argdefs  = map arggen (zip (argnames name) args)++      arggen :: (String, UExpr) -> C.FunDef+      arggen (argname, UExpr ty expr) = genfun argname expr ty++-- | Generate the .h file from a 'Spec'.+compileh :: CSettings -> Spec -> C.TransUnit+compileh cSettings spec = C.TransUnit declns [] where+  streams  = specStreams spec+  triggers = specTriggers spec+  exts     = gatherexts streams triggers+  exprs    = gatherexprs streams triggers++  declns =  mkstructforwdeclns exprs+         ++ mkexts exts+         ++ extfundeclns triggers+         ++ [stepdecln]++  mkstructforwdeclns :: [UExpr] -> [C.Decln]+  mkstructforwdeclns es = catMaybes $ map mkdecln utypes where+    mkdecln (UType ty) = case ty of+      Struct x -> Just $ mkstructforwdecln ty+      _        -> Nothing++    utypes = nub $ concatMap (\(UExpr _ e) -> exprtypes e) es++  -- Make declarations for external variables.+  mkexts :: [External] -> [C.Decln]+  mkexts = map mkextdecln++  extfundeclns :: [Trigger] -> [C.Decln]+  extfundeclns triggers = map extfundecln triggers where+    extfundecln :: Trigger -> C.Decln+    extfundecln (Trigger name _ args) = C.FunDecln Nothing cty name params where+        cty    = C.TypeSpec C.Void+        params = map mkparam $ zip (argnames name) args+        mkparam (name, UExpr ty _) = C.Param (transtype ty) name++  -- Declaration for the step function.+  stepdecln :: C.Decln+  stepdecln = C.FunDecln Nothing (C.TypeSpec C.Void)+                  (cSettingsStepFunctionName cSettings) []
− src/Copilot/Compile/C99/Compile/Internal.hs
@@ -1,159 +0,0 @@--- | Compile Copilot specifications to C99 code.-module Copilot.Compile.C99.Compile.Internal-  ( compile-  , compileWith-  ) where--import Text.PrettyPrint     (render)-import Data.List            (nub)-import Data.Maybe           (catMaybes)-import System.Directory     (createDirectoryIfMissing)-import System.Exit          (exitFailure)-import System.FilePath      ((</>))-import System.IO            (hPutStrLn, stderr)--import Language.C99.Pretty  (pretty)-import qualified Language.C99.Simple as C--import Copilot.Core-import Copilot.Compile.C99.Util-import Copilot.Compile.C99.External-import Copilot.Compile.C99.Settings.Internal-import Copilot.Compile.C99.Translate-import Copilot.Compile.C99.CodeGen---- | Compile a specification to a .h and a .c file.------ The first argument is the settings for the C code generated.------ The second argument is used as prefix for the .h and .c files generated.-compileWith :: CSettings -> String -> Spec -> IO ()-compileWith cSettings prefix spec-  | null (specTriggers spec)-  = do hPutStrLn stderr $-         "Copilot error: attempt at compiling empty specification.\n"-         ++ "You must define at least one trigger to generate C monitors."-       exitFailure--  | otherwise-  = do let cfile = render $ pretty $ C.translate $ compilec cSettings spec-           hfile = render $ pretty $ C.translate $ compileh cSettings spec--           -- TODO: find a nicer solution using annotated AST's-           -- Should figure out exactly which headers are needed, based on what-           -- is used.-           cmacros = unlines [ "#include <stdint.h>"-                             , "#include <stdbool.h>"-                             , "#include <string.h>"-                             , "#include <stdlib.h>"-                             , "#include <math.h>"-                             , ""-                             , "#include \"" ++ prefix ++ ".h\""-                             , ""-                             ]--       let dir = cSettingsOutputDirectory cSettings-       createDirectoryIfMissing True dir-       writeFile (dir </> prefix ++ ".c") $ cmacros ++ cfile-       writeFile (dir </> prefix ++ ".h") hfile---- | Compile a specification to a .h and a .c file.------ The first argument is used as prefix for the .h and .c files generated.-compile :: String -> Spec -> IO ()-compile = compileWith mkDefaultCSettings---- | Generate the .c file from a 'Spec'.------ The generated C file has the following structure:------ * Include .h file.--- * Declarations of global buffers and indices.--- * Generator functions for streams, guards and trigger arguments.--- * Declaration of the @step()@ function.-compilec :: CSettings -> Spec -> C.TransUnit-compilec cSettings spec = C.TransUnit declns funs where-  streams  = specStreams spec-  triggers = specTriggers spec-  exts     = gatherexts streams triggers-  exprs    = gatherexprs streams triggers--  declns = mkstructdeclns exprs ++ mkexts exts ++ mkglobals streams-  funs   = genfuns streams triggers ++ [mkstep cSettings streams triggers exts]--  -- Write struct datatypes-  mkstructdeclns :: [UExpr] -> [C.Decln]-  mkstructdeclns es = catMaybes $ map mkdecln utypes where-    mkdecln (UType ty) = case ty of-      Struct x -> Just $ mkstructdecln ty-      _        -> Nothing--    utypes = nub $ concatMap (\(UExpr _ e) -> exprtypes e) es--  -- Make declarations for copies of external variables.-  mkexts :: [External] -> [C.Decln]-  mkexts exts = map mkextcpydecln exts--  -- Make buffer and index declarations for streams.-  mkglobals :: [Stream] -> [C.Decln]-  mkglobals streams = map buffdecln streams ++ map indexdecln streams where-    buffdecln  (Stream sid buff _ ty) = mkbuffdecln  sid ty buff-    indexdecln (Stream sid _    _ _ ) = mkindexdecln sid--  -- Make generator functions, including trigger arguments.-  genfuns :: [Stream] -> [Trigger] -> [C.FunDef]-  genfuns streams triggers =  map accessdecln streams-                           ++ map streamgen streams-                           ++ concatMap triggergen triggers where--    accessdecln :: Stream -> C.FunDef-    accessdecln (Stream sid buff _ ty) = mkaccessdecln sid ty buff--    streamgen :: Stream -> C.FunDef-    streamgen (Stream sid _ expr ty) = genfun (generatorname sid) expr ty--    triggergen :: Trigger -> [C.FunDef]-    triggergen (Trigger name guard args) = guarddef : argdefs where-      guarddef = genfun (guardname name) guard Bool-      argdefs  = map arggen (zip (argnames name) args)--      arggen :: (String, UExpr) -> C.FunDef-      arggen (argname, UExpr ty expr) = genfun argname expr ty---- | Generate the .h file from a 'Spec'.-compileh :: CSettings -> Spec -> C.TransUnit-compileh cSettings spec = C.TransUnit declns [] where-  streams  = specStreams spec-  triggers = specTriggers spec-  exts     = gatherexts streams triggers-  exprs    = gatherexprs streams triggers--  declns =  mkstructforwdeclns exprs-         ++ mkexts exts-         ++ extfundeclns triggers-         ++ [stepdecln]--  mkstructforwdeclns :: [UExpr] -> [C.Decln]-  mkstructforwdeclns es = catMaybes $ map mkdecln utypes where-    mkdecln (UType ty) = case ty of-      Struct x -> Just $ mkstructforwdecln ty-      _        -> Nothing--    utypes = nub $ concatMap (\(UExpr _ e) -> exprtypes e) es--  -- Make declarations for external variables.-  mkexts :: [External] -> [C.Decln]-  mkexts = map mkextdecln--  extfundeclns :: [Trigger] -> [C.Decln]-  extfundeclns triggers = map extfundecln triggers where-    extfundecln :: Trigger -> C.Decln-    extfundecln (Trigger name _ args) = C.FunDecln Nothing cty name params where-        cty    = C.TypeSpec C.Void-        params = map mkparam $ zip (argnames name) args-        mkparam (name, UExpr ty _) = C.Param (transtype ty) name--  -- Declaration for the step function.-  stepdecln :: C.Decln-  stepdecln = C.FunDecln Nothing (C.TypeSpec C.Void)-                  (cSettingsStepFunctionName cSettings) []
src/Copilot/Compile/C99/External.hs view
@@ -2,9 +2,7 @@  -- | Represent information about externs needed in the generation of C99 code -- for stream declarations and triggers.-module Copilot.Compile.C99.External-  {-# DEPRECATED "This module will be hidden in future versions." #-}-  where+module Copilot.Compile.C99.External where  import Data.List  (unionBy) 
src/Copilot/Compile/C99/Settings.hs view
@@ -1,8 +1,12 @@ -- | Settings used by the code generator to customize the code.-module Copilot.Compile.C99.Settings-  {-# DEPRECATED "This module will be hidden in future versions." #-}-  (module Copilot.Compile.C99.Settings.Internal)- where+module Copilot.Compile.C99.Settings where -import Copilot.Compile.C99.Settings.Internal+-- | Settings used to customize the code generated.+data CSettings = CSettings+  { cSettingsStepFunctionName :: String+  , cSettingsOutputDirectory  :: FilePath+  } +-- | Default settings with a step function called @step@.+mkDefaultCSettings :: CSettings+mkDefaultCSettings = CSettings "step" "."
− src/Copilot/Compile/C99/Settings/Internal.hs
@@ -1,12 +0,0 @@--- | Settings used by the code generator to customize the code.-module Copilot.Compile.C99.Settings.Internal where---- | Settings used to customize the code generated.-data CSettings = CSettings-  { cSettingsStepFunctionName :: String-  , cSettingsOutputDirectory  :: FilePath-  }---- | Default settings with a step function called @step@.-mkDefaultCSettings :: CSettings-mkDefaultCSettings = CSettings "step" "."
src/Copilot/Compile/C99/Translate.hs view
@@ -1,9 +1,7 @@ {-# LANGUAGE GADTs #-}  -- | Translate Copilot Core expressions and operators to C99.-module Copilot.Compile.C99.Translate-  {-# DEPRECATED "This module will be hidden in future versions." #-}-  where+module Copilot.Compile.C99.Translate where  import Control.Monad.State 
src/Copilot/Compile/C99/Util.hs view
@@ -1,7 +1,5 @@ -- | Auxiliary helper functions to generate C99 code.-module Copilot.Compile.C99.Util-  {-# DEPRECATED "This module will be hidden in future versions." #-}-  where+module Copilot.Compile.C99.Util where  import Control.Monad.State