copilot-c99 3.2 → 3.2.1
raw patch · 8 files changed
+51/−24 lines, 8 filesdep ~copilot-c99dep ~copilot-coredep ~copilot-languagePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: copilot-c99, copilot-core, copilot-language
API changes (from Hackage documentation)
Files
- CHANGELOG +4/−0
- copilot-c99.cabal +4/−4
- src/Copilot/Compile/C99.hs +1/−0
- src/Copilot/Compile/C99/CodeGen.hs +5/−4
- src/Copilot/Compile/C99/Compile.hs +13/−8
- src/Copilot/Compile/C99/External.hs +7/−1
- src/Copilot/Compile/C99/Translate.hs +11/−5
- src/Copilot/Compile/C99/Util.hs +6/−2
CHANGELOG view
@@ -1,3 +1,7 @@+2021-03-07+ * Version bump (3.2.1). (#46)+ * Completed the documentation. (#42)+ 2020-12-06 * Version bump (3.2). * Implemented arrays in test driver (#37).
copilot-c99.cabal view
@@ -1,6 +1,6 @@ cabal-version : >= 1.10 name : copilot-c99-version : 3.2+version : 3.2.1 synopsis : A compiler for Copilot targeting C99. description : This package is a back-end from Copilot to C.@@ -41,7 +41,7 @@ , mtl >= 2.2 && < 2.3 , pretty >= 1.1 && < 1.2 - , copilot-core >= 3.2 && < 3.3+ , copilot-core >= 3.2.1 && < 3.3 , language-c99 >= 0.1.1 && < 0.2 , language-c99-util >= 0.1.1 && < 0.2 , language-c99-simple >= 0.1.1 && < 0.2@@ -64,8 +64,8 @@ , Copilot.Compile.C99.Property.SequencePoint build-depends: base , copilot-core- , copilot-c99 >= 3.2 && < 3.3- , copilot-language >= 3.2 && < 3.3+ , copilot-c99 >= 3.2.1 && < 3.3+ , copilot-language >= 3.2.1 && < 3.3 , language-c99 >= 0.1.2 && < 0.2 , language-c99-simple , pretty >= 1.1.3 && < 1.2
src/Copilot/Compile/C99.hs view
@@ -1,3 +1,4 @@+-- | Compile Copilot specifications to C99 code. module Copilot.Compile.C99 ( compile ) where
src/Copilot/Compile/C99/CodeGen.hs view
@@ -1,6 +1,7 @@ {-# LANGUAGE GADTs #-} {-# LANGUAGE ScopedTypeVariables #-} +-- | High-level translation of Copilot Core into C99. module Copilot.Compile.C99.CodeGen where import Control.Monad.State (runState)@@ -25,7 +26,7 @@ cty = C.decay $ transtype ty (cexpr, (cvars, _)) = runState (transexpr expr) mempty --- | Make a extern declaration of an variable.+-- | Make a extern declaration of a variable. mkextdecln :: External -> C.Decln mkextdecln (External name _ ty) = decln where decln = C.VarDecln (Just C.Extern) cty name Nothing@@ -52,7 +53,7 @@ cty = C.TypeSpec $ C.TypedefName "size_t" initval = Just $ C.InitExpr $ C.LitInt 0 --- | The step function updates all streams,a+-- | Writes the step function, that updates all streams. mkstep :: [Stream] -> [Trigger] -> [External] -> C.FunDef mkstep streams triggers exts = C.FunDef void "step" [] declns stmts where void = C.TypeSpec C.Void@@ -126,7 +127,7 @@ mkfield :: Value a -> C.FieldDecln mkfield (Value ty field) = C.FieldDecln (transtype ty) (fieldname field) --- | Write a forward struct decralration.+-- | Write a forward struct declaration. mkstructforwdecln :: Struct a => Type a -> C.Decln mkstructforwdecln (Struct x) = C.TypeDecln struct where struct = C.TypeSpec $ C.Struct (typename x)@@ -145,7 +146,7 @@ Op3 _ e1 e2 e3 -> exprtypes e1 `union` exprtypes e2 `union` exprtypes e3 Label ty _ _ -> typetypes ty --- | List all types of an type, returns items uniquely.+-- | List all types of a type, returns items uniquely. typetypes :: Typeable a => Type a -> [UType] typetypes ty = case ty of Array ty' -> [UType ty] `union` typetypes ty'
src/Copilot/Compile/C99/Compile.hs view
@@ -1,3 +1,4 @@+-- | Compile Copilot specifications to C99 code. module Copilot.Compile.C99.Compile ( compile ) where@@ -15,7 +16,9 @@ import Copilot.Compile.C99.Translate import Copilot.Compile.C99.CodeGen --- | Compile the specification to a .h and a .c file.+-- | 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 prefix spec = do let cfile = render $ pretty $ C.translate $ compilec spec@@ -37,12 +40,14 @@ writeFile (prefix ++ ".c") $ cmacros ++ cfile writeFile (prefix ++ ".h") hfile --- | Generate the .c file from a spec. It has the following structure:--- |--- | * Include .h file--- | * Declarations of global buffers and indices.--- | * Generator functions for streams, guards and trigger args.--- | * Declaration of step() function.+-- | 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 :: Spec -> C.TransUnit compilec spec = C.TransUnit declns funs where streams = specStreams spec@@ -87,7 +92,7 @@ arggen :: (String, UExpr) -> C.FunDef arggen (argname, UExpr ty expr) = genfun argname expr ty --- | Generate the .h file from a spec.+-- | Generate the .h file from a 'Spec'. compileh :: Spec -> C.TransUnit compileh spec = C.TransUnit declns [] where streams = specStreams spec
src/Copilot/Compile/C99/External.hs view
@@ -1,5 +1,7 @@ {-# LANGUAGE ExistentialQuantification #-} +-- | Represent information about externs needed in the generation of C99 code+-- for stream declarations and triggers. module Copilot.Compile.C99.External where import Data.List (unionBy)@@ -14,11 +16,15 @@ , exttype :: Type a } --- | Union over lists of External, we solely base the equality on the extname's.+-- | Union over lists of External, we solely base the equality on the+-- extname's. extunion :: [External] -> [External] -> [External] extunion = unionBy (\a b -> extname a == extname b) -- | Collect all external variables from the streams and triggers.+--+-- Although Copilot specifications can contain also properties and theorems,+-- the C99 backend currently only generates code for streams and triggers. gatherexts :: [Stream] -> [Trigger] -> [External] gatherexts streams triggers = streamsexts `extunion` triggersexts where streamsexts = foldr extunion mempty $ map streamexts streams
src/Copilot/Compile/C99/Translate.hs view
@@ -1,5 +1,6 @@ {-# LANGUAGE GADTs #-} +-- | Translate Copilot Core expressions and operators to C99. module Copilot.Compile.C99.Translate where import Control.Monad.State@@ -9,7 +10,7 @@ import qualified Language.C99.Simple as C --- | Translates a Copilot expression into a C99 expression.+-- | Translates a Copilot Core expression into a C99 expression. transexpr :: Expr a -> State FunEnv C.Expr transexpr (Const ty x) = return $ constty ty x @@ -51,7 +52,8 @@ return $ transop3 op e1' e2' e3' --- | Translates a Copilot unary operator and arguments into a C99 expression.+-- | Translates a Copilot unary operator and its argument into a C99+-- expression. transop1 :: Op1 a b -> C.Expr -> C.Expr transop1 op e = case op of Not -> (C..!) e@@ -77,7 +79,8 @@ Cast _ ty -> C.Cast (transtypename ty) e GetField (Struct _) _ f -> C.Dot e (accessorname f) --- | Translates a Copilot binary operator and arguments into a C99 expression.+-- | Translates a Copilot binary operator and its arguments into a C99+-- expression. transop2 :: Op2 a b c -> C.Expr -> C.Expr -> C.Expr transop2 op e1 e2 = case op of And -> e1 C..&& e2@@ -103,12 +106,14 @@ BwShiftR _ _ -> e1 C..>> e2 Index _ -> C.Index e1 e2 --- | Translates a Copilot ternaty operator and arguments into a C99 expression.+-- | 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 op e1 e2 e3 = case op of Mux _ -> C.Cond e1 e2 e3 --- | Give a C99 literal expression based on a value and a type.+-- | Transform a Copilot Core literal, based on its value and type, into a C99+-- literal. constty :: Type a -> a -> C.Expr constty ty = case ty of Bool -> C.LitBool@@ -135,6 +140,7 @@ _ -> map (C.InitExpr . constty ty) xs +-- | Explicitly cast a C99 value to a type. explicitty :: Type a -> C.Expr -> C.Expr explicitty ty = C.Cast (transtypename ty)
src/Copilot/Compile/C99/Util.hs view
@@ -1,3 +1,4 @@+-- | Auxiliary helper functions to generate C99 code. module Copilot.Compile.C99.Util where import Control.Monad.State@@ -5,7 +6,9 @@ import Copilot.Core (Id) import qualified Language.C99.Simple.AST as C -+-- | Auxiliary type used to collect all the declarations of all the variables+-- used in a function to be generated, since variable declarations are always+-- listed first at the top of the function body. type FunEnv = ([C.Decln], [C.Ident]) -- | `tell` equivalent for `State`.@@ -38,7 +41,7 @@ generatorname :: Id -> String generatorname sid = streamname sid ++ "_gen" --- | Turn a the name of a trigger into a guard generator.+-- | Turn the name of a trigger into a guard generator. guardname :: String -> String guardname name = name ++ "_guard" @@ -50,5 +53,6 @@ argnames :: String -> [String] argnames base = [aname | n <- [0..], let aname = argname base n] +-- | Define a C expression that calls a function with arguments. funcall :: C.Ident -> [C.Expr] -> C.Expr funcall name args = C.Funcall (C.Ident name) args