From 41c0d2938da2cf112c2f977fe963306ff3c8d63e Mon Sep 17 00:00:00 2001 From: Ivan Perez Date: Sat, 21 May 2022 14:26:08 -0400 Subject: [PATCH 1/5] Update bounds of language-c99*. [ci skip] --- copilot-c99/copilot-c99.cabal | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/copilot-c99/copilot-c99.cabal b/copilot-c99/copilot-c99.cabal index 0d90e597c..c6b9ec921 100644 --- a/copilot-c99/copilot-c99.cabal +++ b/copilot-c99/copilot-c99.cabal @@ -49,9 +49,9 @@ library , mtl >= 2.2 && < 2.4 , pretty >= 1.1 && < 1.2 - , copilot-core >= 3.9 && < 3.10 - , language-c99 >= 0.1.1 && < 0.2 - , language-c99-simple >= 0.1.1 && < 0.2 + , copilot-core >= 3.9 && < 3.10 + , language-c99 >= 0.2 && < 0.3 + , language-c99-simple >= 0.2 && < 0.3 exposed-modules : Copilot.Compile.C99 From 617c77745cf7d13afc710d3f4eecf78f84de8075 Mon Sep 17 00:00:00 2001 From: Frank Dedden Date: Sat, 14 May 2022 21:47:06 +0200 Subject: [PATCH 2/5] Made changes to use NonEmpty initialisers in C99. The language-c99-simple library has been changed a bit, and requires one to provide a NonEmpty of initialiser values instead of a list (to better fit the C99 standard). The changes in this commit update copilot-c99 to reflect this requirement. [ci skip] --- copilot-c99/src/Copilot/Compile/C99/CodeGen.hs | 5 +++-- .../src/Copilot/Compile/C99/Translate.hs | 17 ++++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/copilot-c99/src/Copilot/Compile/C99/CodeGen.hs b/copilot-c99/src/Copilot/Compile/C99/CodeGen.hs index 5b6cd9d77..49dec64e5 100644 --- a/copilot-c99/src/Copilot/Compile/C99/CodeGen.hs +++ b/copilot-c99/src/Copilot/Compile/C99/CodeGen.hs @@ -6,6 +6,7 @@ module Copilot.Compile.C99.CodeGen where import Control.Monad.State (runState) import Data.List (union, unzip4) +import qualified Data.List.NonEmpty as NonEmpty import Data.Typeable (Typeable) import qualified Language.C99.Simple as C @@ -50,7 +51,7 @@ mkbuffdecln sid ty xs = C.VarDecln (Just C.Static) cty name initvals name = streamname sid cty = C.Array (transtype ty) (Just $ C.LitInt $ fromIntegral buffsize) buffsize = length xs - initvals = Just $ C.InitArray $ constarray ty xs + initvals = Just $ C.InitList $ constarray ty xs -- | Make a C index variable and initialise it to 0. mkindexdecln :: Id -> C.Decln @@ -224,7 +225,7 @@ mkstructdecln :: Struct a => Type a -> C.Decln mkstructdecln (Struct x) = C.TypeDecln struct where struct = C.TypeSpec $ C.StructDecln (Just $ typename x) fields - fields = map mkfield (toValues x) + fields = NonEmpty.fromList $ map mkfield (toValues x) mkfield :: Value a -> C.FieldDecln mkfield (Value ty field) = C.FieldDecln (transtype ty) (fieldname field) diff --git a/copilot-c99/src/Copilot/Compile/C99/Translate.hs b/copilot-c99/src/Copilot/Compile/C99/Translate.hs index 222fe2760..183053a97 100644 --- a/copilot-c99/src/Copilot/Compile/C99/Translate.hs +++ b/copilot-c99/src/Copilot/Compile/C99/Translate.hs @@ -9,6 +9,8 @@ import Copilot.Core import Copilot.Compile.C99.Error (impossible) import Copilot.Compile.C99.Util +import qualified Data.List.NonEmpty as NonEmpty + import qualified Language.C99.Simple as C -- | Translates a Copilot Core expression into a C99 expression. @@ -226,7 +228,7 @@ constty ty = case ty of Float -> explicitty ty . C.LitFloat Double -> explicitty ty . C.LitDouble Struct _ -> \v -> - C.InitVal (transtypename ty) (map constfieldinit (toValues v)) + C.InitVal (transtypename ty) (NonEmpty.fromList $ map constfieldinit (toValues v)) Array ty' -> \v -> C.InitVal (transtypename ty) (constarray ty' (arrayelems v)) @@ -254,7 +256,7 @@ constinit ty val = case ty of -- whole expression as an array of two int32_t's (as opposed to a nested -- array). This can either lead to compile-time errors (if you're lucky) or -- incorrect runtime semantics (if you're unlucky). - Array ty' -> C.InitArray $ constarray ty' $ arrayelems val + Array ty' -> C.InitList $ constarray ty' $ arrayelems val -- We use InitArray to initialize a struct because the syntax used for -- initializing arrays and structs is compatible. For instance, {1, 2} works @@ -263,17 +265,18 @@ constinit ty val = case ty of -- (structs can also be initialized as { .a = 1, .b = 2}, but language-c99 -- does not support such syntax and does not provide a specialized -- initialization construct for structs). - Struct _ -> C.InitArray $ map constfieldinit $ toValues val + Struct _ -> C.InitList $ NonEmpty.fromList $ map constfieldinit (toValues val) _ -> C.InitExpr $ constty ty val -- | Transform a Copilot Core struct field into a C99 initializer. -constfieldinit :: Value a -> C.Init -constfieldinit (Value ty (Field val)) = constinit ty val +constfieldinit :: Value a -> C.InitItem +constfieldinit (Value ty (Field val)) = C.InitItem Nothing $ constinit ty val -- | Transform a Copilot Array, based on the element values and their type, -- into a list of C99 initializer values. -constarray :: Type a -> [a] -> [C.Init] -constarray ty = map (constinit ty) +constarray :: Type a -> [a] -> NonEmpty.NonEmpty C.InitItem +constarray ty xs = + NonEmpty.fromList $ map (C.InitItem Nothing . constinit ty) xs -- | Explicitly cast a C99 value to a type. explicitty :: Type a -> C.Expr -> C.Expr From 92832634ee12f5a447bf46213af8dc740a96af72 Mon Sep 17 00:00:00 2001 From: Frank Dedden Date: Sat, 14 May 2022 21:51:50 +0200 Subject: [PATCH 3/5] Added conststruct function to cleanup code a bit. Some lines where getting a bit too long. The conststruct function is the analogue of the already existing constarray function. [ci skip] --- copilot-c99/src/Copilot/Compile/C99/Translate.hs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/copilot-c99/src/Copilot/Compile/C99/Translate.hs b/copilot-c99/src/Copilot/Compile/C99/Translate.hs index 183053a97..8d6b13979 100644 --- a/copilot-c99/src/Copilot/Compile/C99/Translate.hs +++ b/copilot-c99/src/Copilot/Compile/C99/Translate.hs @@ -228,7 +228,7 @@ constty ty = case ty of Float -> explicitty ty . C.LitFloat Double -> explicitty ty . C.LitDouble Struct _ -> \v -> - C.InitVal (transtypename ty) (NonEmpty.fromList $ map constfieldinit (toValues v)) + C.InitVal (transtypename ty) (conststruct (toValues v)) Array ty' -> \v -> C.InitVal (transtypename ty) (constarray ty' (arrayelems v)) @@ -265,13 +265,18 @@ constinit ty val = case ty of -- (structs can also be initialized as { .a = 1, .b = 2}, but language-c99 -- does not support such syntax and does not provide a specialized -- initialization construct for structs). - Struct _ -> C.InitList $ NonEmpty.fromList $ map constfieldinit (toValues val) + Struct _ -> C.InitList $ conststruct (toValues val) _ -> C.InitExpr $ constty ty val -- | Transform a Copilot Core struct field into a C99 initializer. constfieldinit :: Value a -> C.InitItem constfieldinit (Value ty (Field val)) = C.InitItem Nothing $ constinit ty val +-- | Transform a Copilot Struct, based on the struct fields, into a list of C99 +-- initializer values. +conststruct :: [Value a] -> NonEmpty.NonEmpty C.InitItem +conststruct val = NonEmpty.fromList $ map constfieldinit val + -- | Transform a Copilot Array, based on the element values and their type, -- into a list of C99 initializer values. constarray :: Type a -> [a] -> NonEmpty.NonEmpty C.InitItem From 213229801313f919ce9113501382a0a9223e7cff Mon Sep 17 00:00:00 2001 From: Frank Dedden Date: Sat, 14 May 2022 21:53:52 +0200 Subject: [PATCH 4/5] Removed a part of a comment in constinit that is incorrect now. The language-c99-simple library does support it since version 0.2. [ci skip] --- copilot-c99/src/Copilot/Compile/C99/Translate.hs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/copilot-c99/src/Copilot/Compile/C99/Translate.hs b/copilot-c99/src/Copilot/Compile/C99/Translate.hs index 8d6b13979..ad5b02ee1 100644 --- a/copilot-c99/src/Copilot/Compile/C99/Translate.hs +++ b/copilot-c99/src/Copilot/Compile/C99/Translate.hs @@ -262,9 +262,7 @@ constinit ty val = case ty of -- initializing arrays and structs is compatible. For instance, {1, 2} works -- both for initializing an int array of length 2 as well as a struct with -- two int fields, although the two expressions are conceptually different - -- (structs can also be initialized as { .a = 1, .b = 2}, but language-c99 - -- does not support such syntax and does not provide a specialized - -- initialization construct for structs). + -- (structs can also be initialized as { .a = 1, .b = 2}. Struct _ -> C.InitList $ conststruct (toValues val) _ -> C.InitExpr $ constty ty val From 6a0fb05ae1648007884d64540c469bc72c739bfe Mon Sep 17 00:00:00 2001 From: Frank Dedden Date: Sun, 15 May 2022 22:33:24 +0200 Subject: [PATCH 5/5] Updated changelog. [ci skip] --- copilot/CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/copilot/CHANGELOG b/copilot/CHANGELOG index b876e44dc..e49928304 100644 --- a/copilot/CHANGELOG +++ b/copilot/CHANGELOG @@ -3,6 +3,7 @@ * Remove duplicated compiler option. (#328) * Fix typos in README and Heater example. (#352) * Relax version bounds of dependencies. (#335) + * Update to support language-c99-0.2.0. (#334) 2022-05-06 * Version bump (3.9). (#320)