From 241d503702a9099b08e72d6be90115f7bcebf7d4 Mon Sep 17 00:00:00 2001 From: Pema Malling Date: Mon, 21 Feb 2022 19:14:12 +0100 Subject: [PATCH 01/20] ISPC uniform/varying --- Language/C/Parser/Parser.y | 23 ++++++++++++ Language/C/Parser/Tokens.hs | 20 +++++++++-- Language/C/Pretty.hs | 3 ++ Language/C/Quote/ISPC.hs | 65 ++++++++++++++++++++++++++++++++++ Language/C/Syntax-instances.hs | 4 +++ Language/C/Syntax.hs | 5 +++ language-c-quote.cabal | 5 +-- 7 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 Language/C/Quote/ISPC.hs diff --git a/Language/C/Parser/Parser.y b/Language/C/Parser/Parser.y index 6a646f1..c5c8f06 100644 --- a/Language/C/Parser/Parser.y +++ b/Language/C/Parser/Parser.y @@ -293,6 +293,12 @@ import qualified Language.C.Syntax as C 'kernel' { L _ T.TCLkernel } '__kernel' { L _ T.TCLkernel } + -- + -- ISPC + -- + 'uniform' { L _ T.TISPCuniform } + 'varying' { L _ T.TISPCvarying } + -- Three shift-reduce conflicts: -- (1) Documented conflict in 'objc_protocol_declaration' -- (2) Objective-C exception syntax (would need lookahead of 2 to disambiguate properly) @@ -1526,6 +1532,9 @@ type_qualifier : | 'kernel' { TSCLkernel (srclocOf $1) } | '__kernel' { TSCLkernel (srclocOf $1) } + | 'uniform' { TSISPCuniform (srclocOf $1) } + | 'varying' { TSISPCvarying (srclocOf $1) } + -- Consider the following C program: -- -- typedef struct foo { @@ -3508,6 +3517,10 @@ data TySpec = TSauto !SrcLoc | TSCLreadonly !SrcLoc | TSCLwriteonly !SrcLoc | TSCLkernel !SrcLoc + + -- ISPC + | TSISPCuniform !SrcLoc + | TSISPCvarying !SrcLoc deriving (Eq, Ord, Show) instance Located TySpec where @@ -3574,6 +3587,9 @@ instance Located TySpec where locOf (TSCLwriteonly loc) = locOf loc locOf (TSCLkernel loc) = locOf loc + locOf (TSISPCuniform loc) = locOf loc + locOf (TSISPCvarying loc) = locOf loc + instance Pretty TySpec where ppr (TSauto _) = text "auto" ppr (TSregister _) = text "register" @@ -3642,6 +3658,9 @@ instance Pretty TySpec where ppr (TSCLwriteonly _) = text "write_only" ppr (TSCLkernel _) = text "__kernel" + ppr (TSISPCuniform _) = text "uniform" + ppr (TSISPCvarying _) = text "varying" + isStorage :: TySpec -> Bool isStorage (TSauto _) = True isStorage (TSregister _) = True @@ -3692,6 +3711,8 @@ isTypeQual (TSCLconstant _) = True isTypeQual (TSCLreadonly _) = True isTypeQual (TSCLwriteonly _) = True isTypeQual (TSCLkernel _) = True +isTypeQual (TSISPCuniform _) = True +isTypeQual (TSISPCvarying _) = True isTypeQual _ = False mkTypeQuals :: [TySpec] -> [TypeQual] @@ -3720,6 +3741,8 @@ mkTypeQuals specs = map mk (filter isTypeQual specs) mk (TSCLreadonly loc) = TCLreadonly loc mk (TSCLwriteonly loc) = TCLwriteonly loc mk (TSCLkernel loc) = TCLkernel loc + mk (TSISPCuniform loc) = TISPCuniform loc + mk (TSISPCvarying loc) = TISPCvarying loc mk _ = error "internal error in mkTypeQual" isSign :: TySpec -> Bool diff --git a/Language/C/Parser/Tokens.hs b/Language/C/Parser/Tokens.hs index 2ee90b0..b38f82f 100644 --- a/Language/C/Parser/Tokens.hs +++ b/Language/C/Parser/Tokens.hs @@ -206,6 +206,10 @@ data Token = Teof | TCLwriteonly | TCLkernel + -- ISPC + | TISPCuniform + | TISPCvarying + -- Clang (currently active is Objective-C is active) | T__block @@ -522,7 +526,13 @@ tokenStrings = [(Tlparen, "("), (TCLconstant, "__constant"), (TCLreadonly, "read_only"), (TCLwriteonly, "write_only"), - (TCLkernel, "__kernel") + (TCLkernel, "__kernel"), + + -- + -- ISPC extensions + -- + (TISPCuniform, "uniform"), + (TISPCvarying, "varying") ] keywords :: [(String, Token, Maybe [Extensions])] @@ -653,7 +663,13 @@ keywords = [("auto", Tauto, Nothing), ("write_only", TCLwriteonly, Just [OpenCL]), ("__write_only", TCLwriteonly, Just [OpenCL]), ("kernel", TCLkernel, Just [OpenCL]), - ("__kernel", TCLkernel, Just [OpenCL]) + ("__kernel", TCLkernel, Just [OpenCL]), + + -- + -- ISPC + -- + ("uniform", TISPCuniform, Just [ISPC]), + ("varying", TISPCvarying, Just [ISPC]) ] type ExtensionsInt = Word32 diff --git a/Language/C/Pretty.hs b/Language/C/Pretty.hs index 150d403..c631585 100644 --- a/Language/C/Pretty.hs +++ b/Language/C/Pretty.hs @@ -251,6 +251,9 @@ instance Pretty TypeQual where ppr (TCLwriteonly _) = text "write_only" ppr (TCLkernel _) = text "__kernel" + ppr (TISPCuniform _) = text "uniform" + ppr (TISPCvarying _) = text "varying" + instance Pretty Sign where ppr (Tsigned _) = text "signed" ppr (Tunsigned _) = text "unsigned" diff --git a/Language/C/Quote/ISPC.hs b/Language/C/Quote/ISPC.hs new file mode 100644 index 0000000..3378e89 --- /dev/null +++ b/Language/C/Quote/ISPC.hs @@ -0,0 +1,65 @@ +-- | +-- Module : Language.C.Quote.ISPC +-- Copyright : (c) 2006-2011 Harvard University +-- (c) 2011-2013 Geoffrey Mainland +-- : (c) 2013-2015 Drexel University +-- License : BSD-style +-- Maintainer : mainland@drexel.edu +-- The quasiquoters exposed by this module support the ISPC extensions. +-- + +module Language.C.Quote.ISPC ( + ToIdent(..), + ToConst(..), + ToExp(..), + cexp, + cedecl, + cdecl, + csdecl, + cenum, + ctyquals, + cty, + cparam, + cparams, + cinit, + cstm, + cstms, + citem, + citems, + cunit, + cfun + ) where + +import qualified Language.C.Parser as P +import qualified Language.C.Syntax as C +import Language.C.Quote.Base (ToIdent(..), ToConst(..), ToExp(..), quasiquote) +import Language.Haskell.TH.Quote (QuasiQuoter) + +exts :: [C.Extensions] +exts = [C.ISPC] + +typenames :: [String] +typenames = + concatMap (typeN 4) ["int", "uint"] ++ ["float", "double"] + +typeN :: Int -> String -> [String] +typeN k typename = [typename ++ show (n*8) | n <- [1..k]] + +cdecl, cedecl, cenum, cexp, cfun, cinit, cparam, cparams, csdecl, cstm, cstms :: QuasiQuoter +citem, citems, ctyquals, cty, cunit :: QuasiQuoter +cdecl = quasiquote exts typenames P.parseDecl +cedecl = quasiquote exts typenames P.parseEdecl +cenum = quasiquote exts typenames P.parseEnum +cexp = quasiquote exts typenames P.parseExp +cfun = quasiquote exts typenames P.parseFunc +cinit = quasiquote exts typenames P.parseInit +cparam = quasiquote exts typenames P.parseParam +cparams = quasiquote exts typenames P.parseParams +csdecl = quasiquote exts typenames P.parseStructDecl +cstm = quasiquote exts typenames P.parseStm +cstms = quasiquote exts typenames P.parseStms +citem = quasiquote exts typenames P.parseBlockItem +citems = quasiquote exts typenames P.parseBlockItems +ctyquals = quasiquote exts typenames P.parseTypeQuals +cty = quasiquote exts typenames P.parseType +cunit = quasiquote exts typenames P.parseUnit diff --git a/Language/C/Syntax-instances.hs b/Language/C/Syntax-instances.hs index c320bed..2fc1e3d 100644 --- a/Language/C/Syntax-instances.hs +++ b/Language/C/Syntax-instances.hs @@ -37,6 +37,8 @@ instance Located TypeQual where locOf (TCLreadonly l) = locOf l locOf (TCLwriteonly l) = locOf l locOf (TCLkernel l) = locOf l + locOf (TISPCuniform l) = locOf l + locOf (TISPCvarying l) = locOf l instance Located Sign where locOf (Tsigned l) = locOf l locOf (Tunsigned l) = locOf l @@ -345,6 +347,8 @@ instance Relocatable TypeQual where reloc l (TCLreadonly _) = (TCLreadonly (fromLoc l)) reloc l (TCLwriteonly _) = (TCLwriteonly (fromLoc l)) reloc l (TCLkernel _) = (TCLkernel (fromLoc l)) + reloc l (TISPCuniform _) = (TISPCuniform (fromLoc l)) + reloc l (TISPCvarying _) = (TISPCvarying (fromLoc l)) instance Relocatable Sign where reloc l (Tsigned _) = (Tsigned (fromLoc l)) reloc l (Tunsigned _) = (Tunsigned (fromLoc l)) diff --git a/Language/C/Syntax.hs b/Language/C/Syntax.hs index 681119e..c531827 100644 --- a/Language/C/Syntax.hs +++ b/Language/C/Syntax.hs @@ -26,6 +26,7 @@ data Extensions = Antiquotation | ObjC | CUDA | OpenCL + | ISPC deriving (Eq, Ord, Enum, Show) data Id = Id String !SrcLoc @@ -85,6 +86,10 @@ data TypeQual = Tconst !SrcLoc | TCLreadonly !SrcLoc | TCLwriteonly !SrcLoc | TCLkernel !SrcLoc + + -- ISPC + | TISPCuniform !SrcLoc + | TISPCvarying !SrcLoc deriving (Eq, Ord, Show, Data, Typeable) data Sign = Tsigned !SrcLoc diff --git a/language-c-quote.cabal b/language-c-quote.cabal index f658c91..c090538 100644 --- a/language-c-quote.cabal +++ b/language-c-quote.cabal @@ -13,14 +13,14 @@ stability: alpha homepage: https://github.com/mainland/language-c-quote bug-reports: https://github.com/mainland/language-c-quote/issues category: Language -synopsis: C/CUDA/OpenCL/Objective-C quasiquoting library. +synopsis: C/CUDA/OpenCL/Objective-C/ISPC quasiquoting library. tested-with: GHC==7.4.2, GHC==7.6.3, GHC==7.8.3, GHC==7.10.3, GHC==8.0.2, GHC==8.2.2, GHC==8.4.3, GHC==8.6.5, GHC==8.8.4, GHC==8.10.4, GHC==9.0.1 description: This package provides a general parser for the C language, including most GCC - extensions and some CUDA and OpenCL extensions as well as the entire Objective-C + extensions and some CUDA, ISPC and OpenCL extensions as well as the entire Objective-C language. build-type: Simple @@ -92,6 +92,7 @@ library Language.C.Quote.GCC Language.C.Quote.ObjC Language.C.Quote.OpenCL + Language.C.Quote.ISPC Language.C.Smart Language.C.Syntax From 1f4b110755459adedb226584ab7b0cec830bb958 Mon Sep 17 00:00:00 2001 From: Pema Malling Date: Mon, 21 Feb 2022 20:13:06 +0100 Subject: [PATCH 02/20] Foreach support --- Language/C/Parser/Parser.y | 7 ++++++- Language/C/Parser/Tokens.hs | 7 +++++-- Language/C/Pretty.hs | 6 ++++++ Language/C/Syntax-instances.hs | 2 ++ Language/C/Syntax.hs | 3 +++ 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Language/C/Parser/Parser.y b/Language/C/Parser/Parser.y index c5c8f06..5ba1949 100644 --- a/Language/C/Parser/Parser.y +++ b/Language/C/Parser/Parser.y @@ -297,7 +297,8 @@ import qualified Language.C.Syntax as C -- ISPC -- 'uniform' { L _ T.TISPCuniform } - 'varying' { L _ T.TISPCvarying } + 'varying' { L _ T.TISPCvarying } + 'foreach' { L _ T.TISPCforeach } -- Three shift-reduce conflicts: -- (1) Documented conflict in 'objc_protocol_declaration' @@ -2186,6 +2187,10 @@ iteration_statement : | 'for' '(' maybe_expression_nlt semi maybe_expression semi expression error {% unclosed ($2 <--> $7) "(" } + -- ISPC + | 'foreach' '(' identifier '=' expression '...' expression ')' statement + { ForEach ($3) ($5) ($7) ($9) ($1 `srcspan` $9) } + jump_statement :: { Stm } jump_statement : 'goto' identifier ';' { Goto $2 ($1 `srcspan` $3) } diff --git a/Language/C/Parser/Tokens.hs b/Language/C/Parser/Tokens.hs index b38f82f..cbc2fc1 100644 --- a/Language/C/Parser/Tokens.hs +++ b/Language/C/Parser/Tokens.hs @@ -209,6 +209,7 @@ data Token = Teof -- ISPC | TISPCuniform | TISPCvarying + | TISPCforeach -- Clang (currently active is Objective-C is active) | T__block @@ -532,7 +533,8 @@ tokenStrings = [(Tlparen, "("), -- ISPC extensions -- (TISPCuniform, "uniform"), - (TISPCvarying, "varying") + (TISPCvarying, "varying"), + (TISPCforeach, "foreach") ] keywords :: [(String, Token, Maybe [Extensions])] @@ -669,7 +671,8 @@ keywords = [("auto", Tauto, Nothing), -- ISPC -- ("uniform", TISPCuniform, Just [ISPC]), - ("varying", TISPCvarying, Just [ISPC]) + ("varying", TISPCvarying, Just [ISPC]), + ("foreach", TISPCforeach, Just [ISPC]) ] type ExtensionsInt = Word32 diff --git a/Language/C/Pretty.hs b/Language/C/Pretty.hs index c631585..ba5f9a2 100644 --- a/Language/C/Pretty.hs +++ b/Language/C/Pretty.hs @@ -745,6 +745,12 @@ instance Pretty Stm where <> text "@autoreleasepool" ppr block + ppr (ForEach var ini to stm sloc) = + srcloc sloc <> + text "foreach" <+> + parens (ppr var <> text " = " <> ppr ini <> text " ... " <> ppr to) <> + pprBlock stm + pprBlock :: Stm -> Doc pprBlock stm@(Block {}) = space <> ppr stm pprBlock stm@(If {}) = space <> ppr [BlockStm stm] diff --git a/Language/C/Syntax-instances.hs b/Language/C/Syntax-instances.hs index 2fc1e3d..e8d1943 100644 --- a/Language/C/Syntax-instances.hs +++ b/Language/C/Syntax-instances.hs @@ -178,6 +178,7 @@ instance Located Stm where locOf (ObjCThrow _ l) = locOf l locOf (ObjCSynchronized _ _ l) = locOf l locOf (ObjCAutoreleasepool _ l) = locOf l + locOf (ForEach _ _ _ _ l) = locOf l instance Located BlockItem where locOf (BlockDecl _) = noLoc locOf (BlockStm _) = noLoc @@ -494,6 +495,7 @@ instance Relocatable Stm where reloc l (ObjCThrow x0 _) = (ObjCThrow x0 (fromLoc l)) reloc l (ObjCSynchronized x0 x1 _) = (ObjCSynchronized x0 x1 (fromLoc l)) reloc l (ObjCAutoreleasepool x0 _) = (ObjCAutoreleasepool x0 (fromLoc l)) + reloc l (ForEach x0 x1 x2 x3 _) = (ForEach x0 x1 x2 x3 (fromLoc l)) instance Relocatable BlockItem where reloc _ (BlockDecl x0) = (BlockDecl x0) reloc _ (BlockStm x0) = (BlockStm x0) diff --git a/Language/C/Syntax.hs b/Language/C/Syntax.hs index c531827..c583f8d 100644 --- a/Language/C/Syntax.hs +++ b/Language/C/Syntax.hs @@ -287,6 +287,9 @@ data Stm = Label Id [Attr] Stm !SrcLoc | ObjCThrow (Maybe Exp) !SrcLoc | ObjCSynchronized Exp [BlockItem] !SrcLoc | ObjCAutoreleasepool [BlockItem] !SrcLoc + + -- ISPC + | ForEach Id Exp Exp Stm !SrcLoc deriving (Eq, Ord, Show, Data, Typeable) data BlockItem = BlockDecl InitGroup From fd4e9279c6c31bc06cc7aefdd56641f0748da92f Mon Sep 17 00:00:00 2001 From: Pema Malling Date: Mon, 21 Feb 2022 20:33:24 +0100 Subject: [PATCH 03/20] Export keyword --- Language/C/Parser/Parser.y | 11 ++++++++++- Language/C/Parser/Tokens.hs | 7 +++++-- Language/C/Pretty.hs | 1 + Language/C/Syntax-instances.hs | 2 ++ Language/C/Syntax.hs | 3 +++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Language/C/Parser/Parser.y b/Language/C/Parser/Parser.y index 5ba1949..2d40bca 100644 --- a/Language/C/Parser/Parser.y +++ b/Language/C/Parser/Parser.y @@ -298,7 +298,8 @@ import qualified Language.C.Syntax as C -- 'uniform' { L _ T.TISPCuniform } 'varying' { L _ T.TISPCvarying } - 'foreach' { L _ T.TISPCforeach } + 'foreach' { L _ T.TISPCforeach } + 'export' { L _ T.TISPCexport } -- Three shift-reduce conflicts: -- (1) Documented conflict in 'objc_protocol_declaration' @@ -1313,6 +1314,9 @@ storage_class_specifier : | '__strong' { TSObjC__strong (srclocOf $1) } | '__unsafe_unretained' { TSObjC__unsafe_unretained (srclocOf $1) } + -- ISPC + | 'export' { TSISPCexport (srclocOf $1) } + type_specifier :: { TySpec } type_specifier : 'void' { TSvoid (srclocOf $1) } @@ -3526,6 +3530,7 @@ data TySpec = TSauto !SrcLoc -- ISPC | TSISPCuniform !SrcLoc | TSISPCvarying !SrcLoc + | TSISPCexport !SrcLoc deriving (Eq, Ord, Show) instance Located TySpec where @@ -3594,6 +3599,7 @@ instance Located TySpec where locOf (TSISPCuniform loc) = locOf loc locOf (TSISPCvarying loc) = locOf loc + locOf (TSISPCexport loc) = locOf loc instance Pretty TySpec where ppr (TSauto _) = text "auto" @@ -3665,6 +3671,7 @@ instance Pretty TySpec where ppr (TSISPCuniform _) = text "uniform" ppr (TSISPCvarying _) = text "varying" + ppr (TSISPCexport _) = text "export" isStorage :: TySpec -> Bool isStorage (TSauto _) = True @@ -3676,6 +3683,7 @@ isStorage (TS__block _) = True isStorage (TSObjC__weak _) = True isStorage (TSObjC__strong _) = True isStorage (TSObjC__unsafe_unretained _) = True +isStorage (TSISPCexport _) = True isStorage _ = False mkStorage :: [TySpec] -> [Storage] @@ -3691,6 +3699,7 @@ mkStorage specs = map mk (filter isStorage specs) mk (TSObjC__weak loc) = TObjC__weak loc mk (TSObjC__strong loc) = TObjC__strong loc mk (TSObjC__unsafe_unretained loc) = TObjC__unsafe_unretained loc + mk (TSISPCexport loc) = TISPCexport loc mk _ = error "internal error in mkStorage" isTypeQual :: TySpec -> Bool diff --git a/Language/C/Parser/Tokens.hs b/Language/C/Parser/Tokens.hs index cbc2fc1..4fc57b3 100644 --- a/Language/C/Parser/Tokens.hs +++ b/Language/C/Parser/Tokens.hs @@ -210,6 +210,7 @@ data Token = Teof | TISPCuniform | TISPCvarying | TISPCforeach + | TISPCexport -- Clang (currently active is Objective-C is active) | T__block @@ -534,7 +535,8 @@ tokenStrings = [(Tlparen, "("), -- (TISPCuniform, "uniform"), (TISPCvarying, "varying"), - (TISPCforeach, "foreach") + (TISPCforeach, "foreach"), + (TISPCexport, "export") ] keywords :: [(String, Token, Maybe [Extensions])] @@ -672,7 +674,8 @@ keywords = [("auto", Tauto, Nothing), -- ("uniform", TISPCuniform, Just [ISPC]), ("varying", TISPCvarying, Just [ISPC]), - ("foreach", TISPCforeach, Just [ISPC]) + ("foreach", TISPCforeach, Just [ISPC]), + ("export", TISPCexport, Just [ISPC]) ] type ExtensionsInt = Word32 diff --git a/Language/C/Pretty.hs b/Language/C/Pretty.hs index ba5f9a2..3bfe4a2 100644 --- a/Language/C/Pretty.hs +++ b/Language/C/Pretty.hs @@ -219,6 +219,7 @@ instance Pretty Storage where ppr (TObjC__weak _) = text "__weak" ppr (TObjC__strong _) = text "__strong" ppr (TObjC__unsafe_unretained _) = text "__unsafe_unretained" + ppr (TISPCexport _) = text "export" instance Pretty TypeQual where ppr (Tconst _) = text "const" diff --git a/Language/C/Syntax-instances.hs b/Language/C/Syntax-instances.hs index e8d1943..2ac5310 100644 --- a/Language/C/Syntax-instances.hs +++ b/Language/C/Syntax-instances.hs @@ -13,6 +13,7 @@ instance Located Storage where locOf (TObjC__weak l) = locOf l locOf (TObjC__strong l) = locOf l locOf (TObjC__unsafe_unretained l) = locOf l + locOf (TISPCexport l) = locOf l instance Located TypeQual where locOf (Tconst l) = locOf l locOf (Tvolatile l) = locOf l @@ -324,6 +325,7 @@ instance Relocatable Storage where reloc l (TObjC__weak _) = (TObjC__weak (fromLoc l)) reloc l (TObjC__strong _) = (TObjC__strong (fromLoc l)) reloc l (TObjC__unsafe_unretained _) = (TObjC__unsafe_unretained (fromLoc l)) + reloc l (TISPCexport _) = (TISPCexport (fromLoc l)) instance Relocatable TypeQual where reloc l (Tconst _) = (Tconst (fromLoc l)) reloc l (Tvolatile _) = (Tvolatile (fromLoc l)) diff --git a/Language/C/Syntax.hs b/Language/C/Syntax.hs index c583f8d..6d45952 100644 --- a/Language/C/Syntax.hs +++ b/Language/C/Syntax.hs @@ -51,6 +51,9 @@ data Storage = Tauto !SrcLoc | TObjC__weak !SrcLoc | TObjC__strong !SrcLoc | TObjC__unsafe_unretained !SrcLoc + + -- ISPC + | TISPCexport !SrcLoc deriving (Eq, Ord, Show, Data, Typeable) data TypeQual = Tconst !SrcLoc From 57711bf6ad8ceb88869dfad6f9721c913a9959af Mon Sep 17 00:00:00 2001 From: Louis Normann Date: Mon, 7 Mar 2022 17:07:13 +0100 Subject: [PATCH 04/20] Added foreach_active and tiled, and unmasked as a storage qualifier, WIP --- Language/C/Parser/Parser.y | 18 +++++++++++++++++- Language/C/Parser/Tokens.hs | 14 ++++++++++++-- Language/C/Pretty.hs | 15 +++++++++++++++ Language/C/Syntax-instances.hs | 8 ++++++++ Language/C/Syntax.hs | 7 ++++++- 5 files changed, 58 insertions(+), 4 deletions(-) diff --git a/Language/C/Parser/Parser.y b/Language/C/Parser/Parser.y index 2d40bca..0a27802 100644 --- a/Language/C/Parser/Parser.y +++ b/Language/C/Parser/Parser.y @@ -299,7 +299,10 @@ import qualified Language.C.Syntax as C 'uniform' { L _ T.TISPCuniform } 'varying' { L _ T.TISPCvarying } 'foreach' { L _ T.TISPCforeach } - 'export' { L _ T.TISPCexport } + 'export' { L _ T.TISPCexport } + 'foreach_active' { L _ T.TISPCactive } + 'foreach_tiled' { L _ T.TISPCtiled } + 'unmasked' { L _ T.TISPCunmasked } -- Three shift-reduce conflicts: -- (1) Documented conflict in 'objc_protocol_declaration' @@ -1316,6 +1319,7 @@ storage_class_specifier : -- ISPC | 'export' { TSISPCexport (srclocOf $1) } + | 'unmasked' { TSISPCunmasked (srclocOf $1) } type_specifier :: { TySpec } type_specifier : @@ -2194,6 +2198,13 @@ iteration_statement : -- ISPC | 'foreach' '(' identifier '=' expression '...' expression ')' statement { ForEach ($3) ($5) ($7) ($9) ($1 `srcspan` $9) } + | 'foreach_active' '(' identifier ')' statement + { ForEachActive ($3) ($5) ($1 `srcspan` $5) } + | 'foreach_tiled' '(' identifier '=' expression '...' expression ')' statement + { ForEachTiled ($3) ($5) ($7) ($9) ($1 `srcspan` $9) } + -- TODO | 'unmasked' statement + -- { Unmasked ($2) ($1 `srcspan` $2) } + jump_statement :: { Stm } jump_statement : @@ -3531,6 +3542,7 @@ data TySpec = TSauto !SrcLoc | TSISPCuniform !SrcLoc | TSISPCvarying !SrcLoc | TSISPCexport !SrcLoc + | TSISPCunmasked !SrcLoc deriving (Eq, Ord, Show) instance Located TySpec where @@ -3600,6 +3612,7 @@ instance Located TySpec where locOf (TSISPCuniform loc) = locOf loc locOf (TSISPCvarying loc) = locOf loc locOf (TSISPCexport loc) = locOf loc + locOf (TSISPCunmasked loc) = locOf loc instance Pretty TySpec where ppr (TSauto _) = text "auto" @@ -3672,6 +3685,7 @@ instance Pretty TySpec where ppr (TSISPCuniform _) = text "uniform" ppr (TSISPCvarying _) = text "varying" ppr (TSISPCexport _) = text "export" + ppr (TSISPCunmasked _) = text "unmasked" isStorage :: TySpec -> Bool isStorage (TSauto _) = True @@ -3684,6 +3698,7 @@ isStorage (TSObjC__weak _) = True isStorage (TSObjC__strong _) = True isStorage (TSObjC__unsafe_unretained _) = True isStorage (TSISPCexport _) = True +isStorage (TSISPCunmasked _) = True isStorage _ = False mkStorage :: [TySpec] -> [Storage] @@ -3700,6 +3715,7 @@ mkStorage specs = map mk (filter isStorage specs) mk (TSObjC__strong loc) = TObjC__strong loc mk (TSObjC__unsafe_unretained loc) = TObjC__unsafe_unretained loc mk (TSISPCexport loc) = TISPCexport loc + mk (TSISPCunmasked loc) = TISPCunmasked loc mk _ = error "internal error in mkStorage" isTypeQual :: TySpec -> Bool diff --git a/Language/C/Parser/Tokens.hs b/Language/C/Parser/Tokens.hs index 4fc57b3..97dd783 100644 --- a/Language/C/Parser/Tokens.hs +++ b/Language/C/Parser/Tokens.hs @@ -211,6 +211,10 @@ data Token = Teof | TISPCvarying | TISPCforeach | TISPCexport + -- | TISPCextern + | TISPCactive + | TISPCtiled + | TISPCunmasked -- Clang (currently active is Objective-C is active) | T__block @@ -536,7 +540,10 @@ tokenStrings = [(Tlparen, "("), (TISPCuniform, "uniform"), (TISPCvarying, "varying"), (TISPCforeach, "foreach"), - (TISPCexport, "export") + (TISPCexport, "export"), + (TISPCactive, "foreach_active"), + (TISPCtiled, "foreach_tiled"), + (TISPCunmasked, "unmasked") ] keywords :: [(String, Token, Maybe [Extensions])] @@ -675,7 +682,10 @@ keywords = [("auto", Tauto, Nothing), ("uniform", TISPCuniform, Just [ISPC]), ("varying", TISPCvarying, Just [ISPC]), ("foreach", TISPCforeach, Just [ISPC]), - ("export", TISPCexport, Just [ISPC]) + ("export", TISPCexport, Just [ISPC]), + ("foreach_tiled",TISPCtiled, Just [ISPC]), + ("foreach_active",TISPCactive, Just [ISPC]), + ("unmasked", TISPCunmasked, Just [ISPC]) ] type ExtensionsInt = Word32 diff --git a/Language/C/Pretty.hs b/Language/C/Pretty.hs index 3bfe4a2..95d8de7 100644 --- a/Language/C/Pretty.hs +++ b/Language/C/Pretty.hs @@ -220,6 +220,7 @@ instance Pretty Storage where ppr (TObjC__strong _) = text "__strong" ppr (TObjC__unsafe_unretained _) = text "__unsafe_unretained" ppr (TISPCexport _) = text "export" + ppr (TISPCunmasked _) = text "unmasked" instance Pretty TypeQual where ppr (Tconst _) = text "const" @@ -751,6 +752,20 @@ instance Pretty Stm where text "foreach" <+> parens (ppr var <> text " = " <> ppr ini <> text " ... " <> ppr to) <> pprBlock stm + ppr (ForEachTiled var ini to stm sloc) = + srcloc sloc <> + text "foreach_tiled" <+> + parens (ppr var <> text " = " <> ppr ini <> text " ... " <> ppr to) <> + pprBlock stm + ppr (ForEachActive var stm sloc) = + srcloc sloc <> + text "foreach_active" <+> + parens (ppr var) <> + pprBlock stm + -- TODO ppr (Unmasked stm sloc) = + -- srcloc sloc <> + -- text "unmasked" <+> + -- pprBlock stm pprBlock :: Stm -> Doc pprBlock stm@(Block {}) = space <> ppr stm diff --git a/Language/C/Syntax-instances.hs b/Language/C/Syntax-instances.hs index 2ac5310..fae5d06 100644 --- a/Language/C/Syntax-instances.hs +++ b/Language/C/Syntax-instances.hs @@ -14,6 +14,7 @@ instance Located Storage where locOf (TObjC__strong l) = locOf l locOf (TObjC__unsafe_unretained l) = locOf l locOf (TISPCexport l) = locOf l + locOf (TISPCunmasked l) = locOf l instance Located TypeQual where locOf (Tconst l) = locOf l locOf (Tvolatile l) = locOf l @@ -180,6 +181,9 @@ instance Located Stm where locOf (ObjCSynchronized _ _ l) = locOf l locOf (ObjCAutoreleasepool _ l) = locOf l locOf (ForEach _ _ _ _ l) = locOf l + locOf (ForEachTiled _ _ _ _ l) = locOf l + locOf (ForEachActive _ _ l) = locOf l + -- locOf (Unmasked _ l) = locOf l instance Located BlockItem where locOf (BlockDecl _) = noLoc locOf (BlockStm _) = noLoc @@ -326,6 +330,7 @@ instance Relocatable Storage where reloc l (TObjC__strong _) = (TObjC__strong (fromLoc l)) reloc l (TObjC__unsafe_unretained _) = (TObjC__unsafe_unretained (fromLoc l)) reloc l (TISPCexport _) = (TISPCexport (fromLoc l)) + reloc l (TISPCunmasked _) = (TISPCunmasked (fromLoc l)) instance Relocatable TypeQual where reloc l (Tconst _) = (Tconst (fromLoc l)) reloc l (Tvolatile _) = (Tvolatile (fromLoc l)) @@ -498,6 +503,9 @@ instance Relocatable Stm where reloc l (ObjCSynchronized x0 x1 _) = (ObjCSynchronized x0 x1 (fromLoc l)) reloc l (ObjCAutoreleasepool x0 _) = (ObjCAutoreleasepool x0 (fromLoc l)) reloc l (ForEach x0 x1 x2 x3 _) = (ForEach x0 x1 x2 x3 (fromLoc l)) + reloc l (ForEachTiled x0 x1 x2 x3 _) = (ForEachTiled x0 x1 x2 x3 (fromLoc l)) + reloc l (ForEachActive x0 x1 _) = (ForEachActive x0 x1 (fromLoc l)) + -- TODO reloc l (Unmasked x0 _) = (Unmasked x0 (fromLoc l)) instance Relocatable BlockItem where reloc _ (BlockDecl x0) = (BlockDecl x0) reloc _ (BlockStm x0) = (BlockStm x0) diff --git a/Language/C/Syntax.hs b/Language/C/Syntax.hs index 6d45952..063959c 100644 --- a/Language/C/Syntax.hs +++ b/Language/C/Syntax.hs @@ -53,7 +53,8 @@ data Storage = Tauto !SrcLoc | TObjC__unsafe_unretained !SrcLoc -- ISPC - | TISPCexport !SrcLoc + | TISPCexport !SrcLoc + | TISPCunmasked !SrcLoc deriving (Eq, Ord, Show, Data, Typeable) data TypeQual = Tconst !SrcLoc @@ -293,6 +294,10 @@ data Stm = Label Id [Attr] Stm !SrcLoc -- ISPC | ForEach Id Exp Exp Stm !SrcLoc + | ForEachActive Id Stm !SrcLoc + | ForEachTiled Id Exp Exp Stm !SrcLoc + -- TODO | Unmasked Stm !SrcLoc + deriving (Eq, Ord, Show, Data, Typeable) data BlockItem = BlockDecl InitGroup From 5031786fec2f80804bc688f63c06e5c1c23e2926 Mon Sep 17 00:00:00 2001 From: Louis Normann Date: Thu, 10 Mar 2022 11:41:43 +0100 Subject: [PATCH 05/20] Unmasked done, untested --- Language/C/Parser/Parser.y | 1 - Language/C/Parser/Tokens.hs | 1 - Language/C/Pretty.hs | 8 ++++---- Language/C/Syntax-instances.hs | 4 ++-- Language/C/Syntax.hs | 2 +- 5 files changed, 7 insertions(+), 9 deletions(-) diff --git a/Language/C/Parser/Parser.y b/Language/C/Parser/Parser.y index 0a27802..b24ceb3 100644 --- a/Language/C/Parser/Parser.y +++ b/Language/C/Parser/Parser.y @@ -353,7 +353,6 @@ import qualified Language.C.Syntax as C %name parseObjCKeywordArg objc_keywordarg %right NAMED OBJCNAMED - %% {------------------------------------------------------------------------------ diff --git a/Language/C/Parser/Tokens.hs b/Language/C/Parser/Tokens.hs index 97dd783..1036d0d 100644 --- a/Language/C/Parser/Tokens.hs +++ b/Language/C/Parser/Tokens.hs @@ -211,7 +211,6 @@ data Token = Teof | TISPCvarying | TISPCforeach | TISPCexport - -- | TISPCextern | TISPCactive | TISPCtiled | TISPCunmasked diff --git a/Language/C/Pretty.hs b/Language/C/Pretty.hs index 95d8de7..9120694 100644 --- a/Language/C/Pretty.hs +++ b/Language/C/Pretty.hs @@ -762,10 +762,10 @@ instance Pretty Stm where text "foreach_active" <+> parens (ppr var) <> pprBlock stm - -- TODO ppr (Unmasked stm sloc) = - -- srcloc sloc <> - -- text "unmasked" <+> - -- pprBlock stm + ppr (Unmasked stm sloc) = + srcloc sloc <> + text "unmasked" <+> + pprBlock stm pprBlock :: Stm -> Doc pprBlock stm@(Block {}) = space <> ppr stm diff --git a/Language/C/Syntax-instances.hs b/Language/C/Syntax-instances.hs index fae5d06..781dbac 100644 --- a/Language/C/Syntax-instances.hs +++ b/Language/C/Syntax-instances.hs @@ -183,7 +183,7 @@ instance Located Stm where locOf (ForEach _ _ _ _ l) = locOf l locOf (ForEachTiled _ _ _ _ l) = locOf l locOf (ForEachActive _ _ l) = locOf l - -- locOf (Unmasked _ l) = locOf l + locOf (Unmasked _ l) = locOf l instance Located BlockItem where locOf (BlockDecl _) = noLoc locOf (BlockStm _) = noLoc @@ -505,7 +505,7 @@ instance Relocatable Stm where reloc l (ForEach x0 x1 x2 x3 _) = (ForEach x0 x1 x2 x3 (fromLoc l)) reloc l (ForEachTiled x0 x1 x2 x3 _) = (ForEachTiled x0 x1 x2 x3 (fromLoc l)) reloc l (ForEachActive x0 x1 _) = (ForEachActive x0 x1 (fromLoc l)) - -- TODO reloc l (Unmasked x0 _) = (Unmasked x0 (fromLoc l)) + reloc l (Unmasked x0 _) = (Unmasked x0 (fromLoc l)) instance Relocatable BlockItem where reloc _ (BlockDecl x0) = (BlockDecl x0) reloc _ (BlockStm x0) = (BlockStm x0) diff --git a/Language/C/Syntax.hs b/Language/C/Syntax.hs index 063959c..9d8110d 100644 --- a/Language/C/Syntax.hs +++ b/Language/C/Syntax.hs @@ -296,7 +296,7 @@ data Stm = Label Id [Attr] Stm !SrcLoc | ForEach Id Exp Exp Stm !SrcLoc | ForEachActive Id Stm !SrcLoc | ForEachTiled Id Exp Exp Stm !SrcLoc - -- TODO | Unmasked Stm !SrcLoc + | Unmasked Stm !SrcLoc deriving (Eq, Ord, Show, Data, Typeable) From c0de1a535fed1d7fb0a81e1b843b882316d40396 Mon Sep 17 00:00:00 2001 From: Louis Normann Date: Thu, 10 Mar 2022 16:03:29 +0100 Subject: [PATCH 06/20] Unmasked, but for real this time --- Language/C/Parser/Parser.y | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Language/C/Parser/Parser.y b/Language/C/Parser/Parser.y index b24ceb3..00b5d14 100644 --- a/Language/C/Parser/Parser.y +++ b/Language/C/Parser/Parser.y @@ -2111,7 +2111,8 @@ compound_statement: { mkBlock $3 ($1 `srcspan` $5) } | '{' begin_scope error {% unclosed (locOf $3) "{" } - + | 'unmasked' '{' statement '}' + { Unmasked ($3) ($1 `srcspan` $4) } block_item_list :: { [BlockItem] } block_item_list : block_item_rlist { rev $1 } @@ -2201,8 +2202,6 @@ iteration_statement : { ForEachActive ($3) ($5) ($1 `srcspan` $5) } | 'foreach_tiled' '(' identifier '=' expression '...' expression ')' statement { ForEachTiled ($3) ($5) ($7) ($9) ($1 `srcspan` $9) } - -- TODO | 'unmasked' statement - -- { Unmasked ($2) ($1 `srcspan` $2) } jump_statement :: { Stm } From 40f2b3876676fd4b5fe3ba806d725d0b65324df2 Mon Sep 17 00:00:00 2001 From: Louis Normann Date: Thu, 10 Mar 2022 16:04:44 +0100 Subject: [PATCH 07/20] Cleaned up --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index da7fc24..9a94e10 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .cabal-sandbox cabal.sandbox.config /dist +.stack-work From f0b1f188a8dc84d4645851c773abe3893b8f159c Mon Sep 17 00:00:00 2001 From: Oliver Bak Date: Thu, 10 Mar 2022 17:19:59 +0100 Subject: [PATCH 08/20] added testing for ISPC keywords --- Language/C/Parser/Parser.y | 7 +- tests/unit/ISPC.hs | 128 +++++++++++++++++++++++++++++++++++++ tests/unit/Main.hs | 2 + 3 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 tests/unit/ISPC.hs diff --git a/Language/C/Parser/Parser.y b/Language/C/Parser/Parser.y index b24ceb3..81d60b3 100644 --- a/Language/C/Parser/Parser.y +++ b/Language/C/Parser/Parser.y @@ -19,6 +19,8 @@ import Control.Monad (forM_, unless, liftM) import Control.Monad.Exception +import Debug.Trace +import System.IO.Unsafe import Data.List (intersperse, sort) import Data.Loc import Data.Maybe (fromMaybe, catMaybes) @@ -2201,8 +2203,9 @@ iteration_statement : { ForEachActive ($3) ($5) ($1 `srcspan` $5) } | 'foreach_tiled' '(' identifier '=' expression '...' expression ')' statement { ForEachTiled ($3) ($5) ($7) ($9) ($1 `srcspan` $9) } - -- TODO | 'unmasked' statement - -- { Unmasked ($2) ($1 `srcspan` $2) } + -- | 'unmasked' statement + -- { Unmasked ($2) ($1 `srcspan` $2) } + jump_statement :: { Stm } diff --git a/tests/unit/ISPC.hs b/tests/unit/ISPC.hs new file mode 100644 index 0000000..0ba385c --- /dev/null +++ b/tests/unit/ISPC.hs @@ -0,0 +1,128 @@ +{-# LANGUAGE QuasiQuotes #-} + +module ISPC ( + ispcTests + ) where + +import Test.Framework +import Test.Framework.Providers.HUnit +import Test.HUnit (Assertion, assert, (@?=)) + +import qualified Data.ByteString.Char8 as B +import Data.Char (isSpace) +import Data.Loc (SrcLoc, noLoc, startPos) +import Control.Exception (SomeException) +import Language.C.Quote.ISPC as ISPC +import Language.C.Smart () +import qualified Language.C.Syntax as C +import qualified Language.C.Parser as P +import Text.PrettyPrint.Mainland +import Text.PrettyPrint.Mainland.Class + +ispcTests :: Test +ispcTests = testGroup "ISPC land" + [ testCase "ispc unmasked" test_unmasked_qualifier + ,testCase "ispc unmasked wvars" test_unmasked_qualifier_wvars + ,testCase "ispc foreach" test_foreach + ,testCase "ispc foreach_active" test_foreach_active + ,testCase "ispc foreach_tiled" test_foreach_tiled + ,testCase "ispc uniform" test_uniform + ,testCase "ispc varying" test_varying + ,testCase "ispc export" test_export + ,testCase "ispc extern" test_extern + --, testCase "attrs antiquote" test_attrs + --, testCase "attrs antiquote pretty" test_attr_p + --, testCase "case ranges quote" test_case_ranges + --, testCase "case ranges pretty" test_case_ranges_p + ] + +test_foreach :: Assertion +test_foreach = pretty 80 (ppr decl) @?= expected + where + decl = [ISPC.cstm|foreach(a = 1 ... n){int a = 10;}|] + expected = "foreach (a = 1 ... n) {\n int a = 10;\n}" + +test_foreach_active :: Assertion +test_foreach_active = pretty 80 (ppr decl) @?= expected + where + decl = [ISPC.cstm|foreach_active(index){int a = 10;}|] + expected = "foreach_active (index) {\n int a = 10;\n}" + +test_foreach_tiled :: Assertion +test_foreach_tiled = pretty 80 (ppr decl) @?= expected + where decl = [ISPC.cstm|foreach_tiled(a = 1 ...n){int a = 10;}|] + expected = "foreach_tiled (a = 1 ... n) {\n int a = 10;\n}" + +test_unmasked_qualifier :: Assertion +test_unmasked_qualifier = pretty 80 (ppr decl) @?= expected + where + decl = [ISPC.cedecl|extern "C" unmasked void func(int * uniform b, float c);|] + expected = "extern \"C\" unmasked void func(int *uniform b, float c);" + + +test_unmasked_qualifier_wvars :: Assertion +test_unmasked_qualifier_wvars = pretty 80 (ppr decl) @?= expected + where + decl = [ISPC.cedecl|extern "C" unmasked void $id:a(int * uniform $id:b, float $id:c);|] + where + a = "func" + b = "b" + c = "c" + expected = "extern \"C\" unmasked void func(int *uniform b, float c);" + +test_uniform :: Assertion +test_uniform = pretty 80 (ppr decl) @?= expected + where + decl = [ISPC.cedecl|uniform int a = 10;|] + expected = "uniform int a = 10;" + +test_varying :: Assertion +test_varying = pretty 80 (ppr decl) @?= expected + where + decl = [ISPC.cedecl|varying int a = 5;|] + expected = "varying int a = 5;" + +test_export :: Assertion +test_export = pretty 80 (ppr decl) @?= expected + where + decl = [ISPC.cedecl|export int func(int a, float b){int c = 10;}|] + expected = "export int func(int a, float b)\n{\n int c = 10;\n}" + +test_extern :: Assertion +test_extern = pretty 80 (ppr decl) @?= expected + where + decl = [ISPC.cedecl|extern "C" void func(int a, float b);|] + expected = "extern \"C\" void func(int a, float b);" + +-- where +-- test_attr :: Assertion +-- test_attr = +-- [cedecl| int test __attribute__(($attr:a,$attr:b));|] +-- @?= +-- [cedecl| int test __attribute__((section(".sram2"), noinit));|] +-- where +-- a = [cattr| section(".sram2") |] +-- b = [cattr| noinit |] +-- +-- test_attrs :: Assertion +-- test_attrs = +-- [cedecl| int test __attribute__(($attrs:as));|] +-- @?= +-- [cedecl| int test __attribute__((section(".sram2"), noinit));|] +-- where +-- a = [cattr| section(".sram2") |] +-- b = [cattr| noinit |] +-- as = [ a, b ] +-- +-- test_attr_p :: Assertion +-- test_attr_p = +-- pretty 80 (ppr [cattr|section(".sram2")|]) @?= "section(\".sram2\")" +-- +-- test_case_ranges :: Assertion +-- test_case_ranges = assert $ case [cstm| case 10 ... 20: ; |] of +-- C.CaseRange 10 20 (C.Exp Nothing _) _ -> True +-- _ -> False +-- +-- test_case_ranges_p :: Assertion +-- test_case_ranges_p = +-- pretty 80 (ppr [cstm| case 10 ... 20: ; |]) @?= "\ncase 10 ... 20:\n;" diff --git a/tests/unit/Main.hs b/tests/unit/Main.hs index c40e61d..0198d5d 100644 --- a/tests/unit/Main.hs +++ b/tests/unit/Main.hs @@ -19,6 +19,7 @@ import Numeric (showHex) import GCC (gccTests) import Objc (objcTests, objcRegressionTests) import CUDA (cudaTests) +import ISPC (ispcTests) import Text.PrettyPrint.Mainland import Text.PrettyPrint.Mainland.Class @@ -36,6 +37,7 @@ tests = [ constantTests , objcTests , objcRegressionTests , cudaTests + , ispcTests ] constantTests :: Test From 9e853921bb7d322f96b7d0c2281a2be5801c43a0 Mon Sep 17 00:00:00 2001 From: Louis Normann Date: Mon, 14 Mar 2022 15:12:01 +0100 Subject: [PATCH 09/20] Untested coherent control flow --- Language/C/Parser/Parser.y | 4 ++++ Language/C/Parser/Tokens.hs | 4 ++++ Language/C/Pretty.hs | 39 ++++++++++++++++++++++++++++++++++ Language/C/Syntax-instances.hs | 9 ++++++++ Language/C/Syntax.hs | 4 ++++ 5 files changed, 60 insertions(+) diff --git a/Language/C/Parser/Parser.y b/Language/C/Parser/Parser.y index 6aa25b9..03d983d 100644 --- a/Language/C/Parser/Parser.y +++ b/Language/C/Parser/Parser.y @@ -305,6 +305,10 @@ import qualified Language.C.Syntax as C 'foreach_active' { L _ T.TISPCactive } 'foreach_tiled' { L _ T.TISPCtiled } 'unmasked' { L _ T.TISPCunmasked } + 'cif' { L _ T.TISPCcif } + 'cwhile' { L _ T.TISPCcwhile } + 'cdo' { L _ T.TISPCcdo } + 'cfor' { L _ T.TISPCcfor } -- Three shift-reduce conflicts: -- (1) Documented conflict in 'objc_protocol_declaration' diff --git a/Language/C/Parser/Tokens.hs b/Language/C/Parser/Tokens.hs index 1036d0d..54f2a64 100644 --- a/Language/C/Parser/Tokens.hs +++ b/Language/C/Parser/Tokens.hs @@ -214,6 +214,10 @@ data Token = Teof | TISPCactive | TISPCtiled | TISPCunmasked + | TISPCcwhile + | TISPCcif + | TISPCcfor + | TISPCcdo -- Clang (currently active is Objective-C is active) | T__block diff --git a/Language/C/Pretty.hs b/Language/C/Pretty.hs index 9120694..8aeb594 100644 --- a/Language/C/Pretty.hs +++ b/Language/C/Pretty.hs @@ -766,6 +766,45 @@ instance Pretty Stm where srcloc sloc <> text "unmasked" <+> pprBlock stm + -- TODO do, while, for + ppr (CIf test then' maybe_else sloc) = + srcloc sloc <> + text "cif" <+> parens (ppr test) <> + pprThen then' (fmap pprElse maybe_else) + where + isIf :: Stm -> Bool + isIf If{} = True + isIf (Comment _ stm _) = isIf stm + isIf _ = False + + pprThen :: Stm -> Maybe Doc -> Doc + pprThen stm@(Block {}) rest = space <> ppr stm <+> maybe empty id rest + pprThen stm rest + | isIf stm = space <> ppr [BlockStm stm] <+> maybe empty id rest + pprThen stm Nothing = nest 4 (line <> ppr stm) + pprThen stm (Just rest) = nest 4 (line <> ppr stm) rest + + pprElse :: Stm -> Doc + pprElse stm = + text "else" <> go stm + where + go :: Stm -> Doc + go (Block {}) = space <> ppr stm + go (If {}) = space <> ppr stm + go _stm = nest 4 (line <> ppr stm) + ppr (CWhile e stm sloc) = + srcloc sloc <> + text "cwhile" <+> parens (ppr e) <> pprBlock stm + + ppr (CDo stm e sloc) = + srcloc sloc <> + text "cdo" <> pprBlock stm <+/> text "while" <> parens (ppr e) <> semi + + ppr (CFor ini test post stm sloc) = + srcloc sloc <> + text "cfor" <+> + (parens . semisep) [either ppr ppr ini, ppr test, ppr post] <> + pprBlock stm pprBlock :: Stm -> Doc pprBlock stm@(Block {}) = space <> ppr stm diff --git a/Language/C/Syntax-instances.hs b/Language/C/Syntax-instances.hs index 781dbac..4989914 100644 --- a/Language/C/Syntax-instances.hs +++ b/Language/C/Syntax-instances.hs @@ -184,6 +184,11 @@ instance Located Stm where locOf (ForEachTiled _ _ _ _ l) = locOf l locOf (ForEachActive _ _ l) = locOf l locOf (Unmasked _ l) = locOf l + locOf (CIf _ _ _ l) = locOf l + locOf (CWhile _ _ l) = locOf l + locOf (CFor _ _ _ _ l) = locOf l + locOf (CDo _ _ l) = locOf l + instance Located BlockItem where locOf (BlockDecl _) = noLoc locOf (BlockStm _) = noLoc @@ -506,6 +511,10 @@ instance Relocatable Stm where reloc l (ForEachTiled x0 x1 x2 x3 _) = (ForEachTiled x0 x1 x2 x3 (fromLoc l)) reloc l (ForEachActive x0 x1 _) = (ForEachActive x0 x1 (fromLoc l)) reloc l (Unmasked x0 _) = (Unmasked x0 (fromLoc l)) + reloc l (CIf x0 x1 x2 _) = (CIf x0 x1 x2 (fromLoc l)) + reloc l (CWhile x0 x1 _) = (CWhile x0 x1 (fromLoc l)) + reloc l (CFor x0 x1 x2 x3 _) = (CFor x0 x1 x2 x3 (fromLoc l)) + reloc l (CDo x0 x1 _) = (CDo x0 x1 (fromLoc l)) instance Relocatable BlockItem where reloc _ (BlockDecl x0) = (BlockDecl x0) reloc _ (BlockStm x0) = (BlockStm x0) diff --git a/Language/C/Syntax.hs b/Language/C/Syntax.hs index 9d8110d..c44695f 100644 --- a/Language/C/Syntax.hs +++ b/Language/C/Syntax.hs @@ -297,6 +297,10 @@ data Stm = Label Id [Attr] Stm !SrcLoc | ForEachActive Id Stm !SrcLoc | ForEachTiled Id Exp Exp Stm !SrcLoc | Unmasked Stm !SrcLoc + | CIf Exp Stm (Maybe Stm) !SrcLoc + | CWhile Exp Stm !SrcLoc + | CFor (Either InitGroup (Maybe Exp)) (Maybe Exp) (Maybe Exp) Stm !SrcLoc + | CDo Stm Exp !SrcLoc deriving (Eq, Ord, Show, Data, Typeable) From 8142f32f877609e5ea109aafbb5505aef0a81a4b Mon Sep 17 00:00:00 2001 From: Louis Normann Date: Mon, 14 Mar 2022 15:25:25 +0100 Subject: [PATCH 10/20] Missing keyword and token strings added --- Language/C/Parser/Tokens.hs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Language/C/Parser/Tokens.hs b/Language/C/Parser/Tokens.hs index 54f2a64..734b106 100644 --- a/Language/C/Parser/Tokens.hs +++ b/Language/C/Parser/Tokens.hs @@ -546,7 +546,11 @@ tokenStrings = [(Tlparen, "("), (TISPCexport, "export"), (TISPCactive, "foreach_active"), (TISPCtiled, "foreach_tiled"), - (TISPCunmasked, "unmasked") + (TISPCunmasked, "unmasked"), + (TISPCcif, "cif"), + (TISPCcwhile, "cwhile"), + (TISPCcdo, "cdo"), + (TISPCcfor, "cfor") ] keywords :: [(String, Token, Maybe [Extensions])] @@ -688,7 +692,11 @@ keywords = [("auto", Tauto, Nothing), ("export", TISPCexport, Just [ISPC]), ("foreach_tiled",TISPCtiled, Just [ISPC]), ("foreach_active",TISPCactive, Just [ISPC]), - ("unmasked", TISPCunmasked, Just [ISPC]) + ("unmasked", TISPCunmasked, Just [ISPC]), + ("cif", TISPCcif, Just [ISPC]), + ("cwhile", TISPCcwhile, Just [ISPC]), + ("cdo", TISPCcdo, Just [ISPC]), + ("cfor", TISPCcfor, Just [ISPC]) ] type ExtensionsInt = Word32 From 18ae1bde5fafbd564eb24d453ea8eab912afd787 Mon Sep 17 00:00:00 2001 From: Louis Normann Date: Mon, 14 Mar 2022 15:56:24 +0100 Subject: [PATCH 11/20] Parsing for coherent control flow, extra shift/reduce conflict --- Language/C/Parser/Parser.y | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Language/C/Parser/Parser.y b/Language/C/Parser/Parser.y index 03d983d..addd892 100644 --- a/Language/C/Parser/Parser.y +++ b/Language/C/Parser/Parser.y @@ -314,7 +314,7 @@ import qualified Language.C.Syntax as C -- (1) Documented conflict in 'objc_protocol_declaration' -- (2) Objective-C exception syntax (would need lookahead of 2 to disambiguate properly) -- (3) The standard dangling else conflict -%expect 3 +%expect 4 %monad { P } { >>= } { return } %lexer { lexer } { L _ T.Teof } @@ -2173,6 +2173,10 @@ selection_statement : { Switch $3 $5 ($1 `srcspan` $5) } | 'switch' '(' expression error {% unclosed ($2 <--> $3) "(" } + | 'cif' '(' expression ')' statement + { CIf $3 $5 Nothing ($1 `srcspan` $5) } + | 'cif' '(' expression ')' statement 'else' statement + { CIf $3 $5 (Just $7) ($1 `srcspan` $7) } iteration_statement :: { Stm } iteration_statement : @@ -2206,6 +2210,18 @@ iteration_statement : { ForEachActive ($3) ($5) ($1 `srcspan` $5) } | 'foreach_tiled' '(' identifier '=' expression '...' expression ')' statement { ForEachTiled ($3) ($5) ($7) ($9) ($1 `srcspan` $9) } + | 'cwhile' '(' expression ')' statement + { CWhile $3 $5 ($1 `srcspan` $5) } + | 'cdo' statement 'while' '(' expression ')' ';' + { CDo $2 $5 ($1 `srcspan` $7) } + | 'cfor' '(' declaration maybe_expression semi ')' statement + { CFor (Left $3) $4 Nothing $7 ($1 `srcspan` $7) } + | 'cfor' '(' maybe_expression_nlt semi maybe_expression semi ')' statement + { CFor (Right $3) $5 Nothing $8 ($1 `srcspan` $8) } + | 'cfor' '(' declaration maybe_expression semi expression ')' statement + { CFor (Left $3) $4 (Just $6) $8 ($1 `srcspan` $8) } + | 'cfor' '(' maybe_expression_nlt semi maybe_expression semi expression ')' statement + { CFor (Right $3) $5 (Just $7) $9 ($1 `srcspan` $9) } jump_statement :: { Stm } jump_statement : From 5a14b91317cd8fe08cb8c30ebf9d75cbeee2f70a Mon Sep 17 00:00:00 2001 From: Oliver Bak Date: Mon, 14 Mar 2022 16:24:38 +0100 Subject: [PATCH 12/20] added foreach_unique and in keywords + tests and uint/int/float types skkrt --- Language/C/Parser/Lexer.x | 4 ++++ Language/C/Parser/Parser.y | 4 ++++ Language/C/Parser/Tokens.hs | 10 ++++++++-- Language/C/Pretty.hs | 6 ++++++ Language/C/Quote/ISPC.hs | 5 +++-- Language/C/Syntax-instances.hs | 2 ++ Language/C/Syntax.hs | 1 + tests/unit/ISPC.hs | 6 ++++++ 8 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Language/C/Parser/Lexer.x b/Language/C/Parser/Lexer.x index 9b04ff5..afaff37 100644 --- a/Language/C/Parser/Lexer.x +++ b/Language/C/Parser/Lexer.x @@ -243,6 +243,10 @@ c :- ">>>" / { ifExtension cudaExts } { token TCUDA3gt } + -- + -- ISPC + -- + "in" { token TISPCin } } { diff --git a/Language/C/Parser/Parser.y b/Language/C/Parser/Parser.y index 6aa25b9..8725c87 100644 --- a/Language/C/Parser/Parser.y +++ b/Language/C/Parser/Parser.y @@ -305,6 +305,8 @@ import qualified Language.C.Syntax as C 'foreach_active' { L _ T.TISPCactive } 'foreach_tiled' { L _ T.TISPCtiled } 'unmasked' { L _ T.TISPCunmasked } + 'foreach_unique' { L _ T.TISPCunique } + 'in' { L _ T.TISPCin } -- Three shift-reduce conflicts: -- (1) Documented conflict in 'objc_protocol_declaration' @@ -2202,6 +2204,8 @@ iteration_statement : { ForEachActive ($3) ($5) ($1 `srcspan` $5) } | 'foreach_tiled' '(' identifier '=' expression '...' expression ')' statement { ForEachTiled ($3) ($5) ($7) ($9) ($1 `srcspan` $9) } + | 'foreach_unique' '(' identifier 'in' expression ')' statement + { ForEachUnique ($3) ($5) ($7) ($1 `srcspan` $7) } jump_statement :: { Stm } jump_statement : diff --git a/Language/C/Parser/Tokens.hs b/Language/C/Parser/Tokens.hs index 1036d0d..54a1547 100644 --- a/Language/C/Parser/Tokens.hs +++ b/Language/C/Parser/Tokens.hs @@ -214,6 +214,8 @@ data Token = Teof | TISPCactive | TISPCtiled | TISPCunmasked + | TISPCunique + | TISPCin -- Clang (currently active is Objective-C is active) | T__block @@ -542,7 +544,9 @@ tokenStrings = [(Tlparen, "("), (TISPCexport, "export"), (TISPCactive, "foreach_active"), (TISPCtiled, "foreach_tiled"), - (TISPCunmasked, "unmasked") + (TISPCunmasked, "unmasked"), + (TISPCunique, "unique"), + (TISPCin , "in") ] keywords :: [(String, Token, Maybe [Extensions])] @@ -684,7 +688,9 @@ keywords = [("auto", Tauto, Nothing), ("export", TISPCexport, Just [ISPC]), ("foreach_tiled",TISPCtiled, Just [ISPC]), ("foreach_active",TISPCactive, Just [ISPC]), - ("unmasked", TISPCunmasked, Just [ISPC]) + ("unmasked", TISPCunmasked, Just [ISPC]), + ("foreach_unique",TISPCunique, Just [ISPC]), + ("in" ,TISPCin , Just [ISPC]) ] type ExtensionsInt = Word32 diff --git a/Language/C/Pretty.hs b/Language/C/Pretty.hs index 9120694..f1fbe25 100644 --- a/Language/C/Pretty.hs +++ b/Language/C/Pretty.hs @@ -762,6 +762,12 @@ instance Pretty Stm where text "foreach_active" <+> parens (ppr var) <> pprBlock stm + + ppr (ForEachUnique ini var stm sloc) = + srcloc sloc <> + text "foreach_unique" <+> + parens (ppr ini <> (text " in ") <> ppr var) <> + pprBlock stm ppr (Unmasked stm sloc) = srcloc sloc <> text "unmasked" <+> diff --git a/Language/C/Quote/ISPC.hs b/Language/C/Quote/ISPC.hs index 3378e89..1e23144 100644 --- a/Language/C/Quote/ISPC.hs +++ b/Language/C/Quote/ISPC.hs @@ -40,10 +40,11 @@ exts = [C.ISPC] typenames :: [String] typenames = - concatMap (typeN 4) ["int", "uint"] ++ ["float", "double"] + concatMap (typeN 4) ["int", "uint"] + ++ ["float","float16", "double"] typeN :: Int -> String -> [String] -typeN k typename = [typename ++ show (n*8) | n <- [1..k]] +typeN k typename = [typename ++ show (2^n) | n <- [3..k+2]] cdecl, cedecl, cenum, cexp, cfun, cinit, cparam, cparams, csdecl, cstm, cstms :: QuasiQuoter citem, citems, ctyquals, cty, cunit :: QuasiQuoter diff --git a/Language/C/Syntax-instances.hs b/Language/C/Syntax-instances.hs index 781dbac..7ee64b3 100644 --- a/Language/C/Syntax-instances.hs +++ b/Language/C/Syntax-instances.hs @@ -183,6 +183,7 @@ instance Located Stm where locOf (ForEach _ _ _ _ l) = locOf l locOf (ForEachTiled _ _ _ _ l) = locOf l locOf (ForEachActive _ _ l) = locOf l + locOf (ForEachUnique _ _ _ l) = locOf l locOf (Unmasked _ l) = locOf l instance Located BlockItem where locOf (BlockDecl _) = noLoc @@ -505,6 +506,7 @@ instance Relocatable Stm where reloc l (ForEach x0 x1 x2 x3 _) = (ForEach x0 x1 x2 x3 (fromLoc l)) reloc l (ForEachTiled x0 x1 x2 x3 _) = (ForEachTiled x0 x1 x2 x3 (fromLoc l)) reloc l (ForEachActive x0 x1 _) = (ForEachActive x0 x1 (fromLoc l)) + reloc l (ForEachUnique x0 x1 x2 _) = (ForEachUnique x0 x1 x2 (fromLoc l)) reloc l (Unmasked x0 _) = (Unmasked x0 (fromLoc l)) instance Relocatable BlockItem where reloc _ (BlockDecl x0) = (BlockDecl x0) diff --git a/Language/C/Syntax.hs b/Language/C/Syntax.hs index 9d8110d..9c67ee9 100644 --- a/Language/C/Syntax.hs +++ b/Language/C/Syntax.hs @@ -296,6 +296,7 @@ data Stm = Label Id [Attr] Stm !SrcLoc | ForEach Id Exp Exp Stm !SrcLoc | ForEachActive Id Stm !SrcLoc | ForEachTiled Id Exp Exp Stm !SrcLoc + | ForEachUnique Id Exp Stm !SrcLoc | Unmasked Stm !SrcLoc deriving (Eq, Ord, Show, Data, Typeable) diff --git a/tests/unit/ISPC.hs b/tests/unit/ISPC.hs index 0ba385c..0332574 100644 --- a/tests/unit/ISPC.hs +++ b/tests/unit/ISPC.hs @@ -30,6 +30,7 @@ ispcTests = testGroup "ISPC land" ,testCase "ispc varying" test_varying ,testCase "ispc export" test_export ,testCase "ispc extern" test_extern + ,testCase "ispc foreach_unique" test_foreach_unique --, testCase "attrs antiquote" test_attrs --, testCase "attrs antiquote pretty" test_attr_p --, testCase "case ranges quote" test_case_ranges @@ -53,6 +54,11 @@ test_foreach_tiled = pretty 80 (ppr decl) @?= expected where decl = [ISPC.cstm|foreach_tiled(a = 1 ...n){int a = 10;}|] expected = "foreach_tiled (a = 1 ... n) {\n int a = 10;\n}" +test_foreach_unique :: Assertion +test_foreach_unique = pretty 80 (ppr decl) @?= expected + where decl = [ISPC.cstm|foreach_unique(y in x){int a = 10;}|] + expected = "foreach_unique (y in x) {\n int a = 10;\n}" + test_unmasked_qualifier :: Assertion test_unmasked_qualifier = pretty 80 (ppr decl) @?= expected where From 5668e74ef427f7c5ad6cd2fa66faba61f2a8a147 Mon Sep 17 00:00:00 2001 From: Pema Malling Date: Tue, 29 Mar 2022 00:30:41 +0200 Subject: [PATCH 13/20] Multidimensional foreach --- Language/C/Parser/Lexer.x | 5 +++ Language/C/Parser/Parser.y | 64 +++++++++++++++++++++++++--------- Language/C/Parser/Tokens.hs | 6 ++++ Language/C/Pretty.hs | 13 ++++--- Language/C/Quote/Base.hs | 8 +++++ Language/C/Quote/ISPC.hs | 6 +++- Language/C/Syntax-instances.hs | 14 +++++--- Language/C/Syntax.hs | 15 ++++++-- 8 files changed, 103 insertions(+), 28 deletions(-) diff --git a/Language/C/Parser/Lexer.x b/Language/C/Parser/Lexer.x index afaff37..a99607e 100644 --- a/Language/C/Parser/Lexer.x +++ b/Language/C/Parser/Lexer.x @@ -145,6 +145,11 @@ c :- "$recv:" / { allowAnti } { lexAnti Tanti_objc_recv } "$kwarg:" / { allowAnti } { lexAnti Tanti_objc_arg } "$kwargs:" / { allowAnti } { lexAnti Tanti_objc_args } + + -- + -- ISPC + -- + "$foreachiters:" / { allowAnti } { lexAnti Tanti_foreach_iters } } <0> { diff --git a/Language/C/Parser/Parser.y b/Language/C/Parser/Parser.y index b20e2a4..07e4099 100644 --- a/Language/C/Parser/Parser.y +++ b/Language/C/Parser/Parser.y @@ -298,19 +298,20 @@ import qualified Language.C.Syntax as C -- -- ISPC -- - 'uniform' { L _ T.TISPCuniform } - 'varying' { L _ T.TISPCvarying } - 'foreach' { L _ T.TISPCforeach } - 'export' { L _ T.TISPCexport } - 'foreach_active' { L _ T.TISPCactive } - 'foreach_tiled' { L _ T.TISPCtiled } - 'unmasked' { L _ T.TISPCunmasked } - 'foreach_unique' { L _ T.TISPCunique } - 'in' { L _ T.TISPCin } - 'cif' { L _ T.TISPCcif } - 'cwhile' { L _ T.TISPCcwhile } - 'cdo' { L _ T.TISPCcdo } - 'cfor' { L _ T.TISPCcfor } + 'uniform' { L _ T.TISPCuniform } + 'varying' { L _ T.TISPCvarying } + 'foreach' { L _ T.TISPCforeach } + 'export' { L _ T.TISPCexport } + 'foreach_active' { L _ T.TISPCactive } + 'foreach_tiled' { L _ T.TISPCtiled } + 'unmasked' { L _ T.TISPCunmasked } + 'foreach_unique' { L _ T.TISPCunique } + 'in' { L _ T.TISPCin } + 'cif' { L _ T.TISPCcif } + 'cwhile' { L _ T.TISPCcwhile } + 'cdo' { L _ T.TISPCcdo } + 'cfor' { L _ T.TISPCcfor } + ANTI_FOREACH_ITERS { L _ (T.Tanti_foreach_iters _) } -- Three shift-reduce conflicts: -- (1) Documented conflict in 'objc_protocol_declaration' @@ -360,6 +361,11 @@ import qualified Language.C.Syntax as C %name parseObjCMethodRecv objc_receiver %name parseObjCKeywordArg objc_keywordarg +-- +-- ISPC +-- +%name parseISPCForEachIters ispc_foreach_iter_list + %right NAMED OBJCNAMED %% @@ -2206,12 +2212,12 @@ iteration_statement : {% unclosed ($2 <--> $7) "(" } -- ISPC - | 'foreach' '(' identifier '=' expression '...' expression ')' statement - { ForEach ($3) ($5) ($7) ($9) ($1 `srcspan` $9) } + | 'foreach' '(' ispc_foreach_iter_list ')' statement + { ForEach ($3) ($5) ($1 `srcspan` $5) } | 'foreach_active' '(' identifier ')' statement { ForEachActive ($3) ($5) ($1 `srcspan` $5) } - | 'foreach_tiled' '(' identifier '=' expression '...' expression ')' statement - { ForEachTiled ($3) ($5) ($7) ($9) ($1 `srcspan` $9) } + | 'foreach_tiled' '(' ispc_foreach_iter_list ')' statement + { ForEachTiled ($3) ($5) ($1 `srcspan` $5) } | 'foreach_unique' '(' identifier 'in' expression ')' statement { ForEachUnique ($3) ($5) ($7) ($1 `srcspan` $7) } | 'cwhile' '(' expression ')' statement @@ -2241,6 +2247,25 @@ jump_statement : | 'return' expression ';' { Return (Just $2) ($1 `srcspan` $3) } | 'return' expression error {% expected ["';'"] Nothing } +ispc_foreach_iter :: { ForEachIter } +ispc_foreach_iter : + identifier '=' assignment_expression '...' assignment_expression { ForEachIter ($1) ($3) ($5) ($1 `srcspan` $5) } + +ispc_foreach_iter_list :: { [ForEachIter] } +ispc_foreach_iter_list : + ispc_foreach_iter_rlist { rev $1 } + +ispc_foreach_iter_rlist :: { RevList ForEachIter } +ispc_foreach_iter_rlist : + ispc_foreach_iter + { rsingleton $1 } + | ANTI_FOREACH_ITERS + { rsingleton (AntiForEachIters (getANTI_FOREACH_ITERS $1) (srclocOf $1)) } + | ispc_foreach_iter_rlist ',' ispc_foreach_iter + { rcons $3 $1} + | ispc_foreach_iter_rlist ',' ANTI_ARGS + { rcons (AntiForEachIters (getANTI_FOREACH_ITERS $3) (srclocOf $3)) $1 } + {------------------------------------------------------------------------------ - - External definitions @@ -3482,6 +3507,11 @@ getANTI_OBJC_RECV (L _ (T.Tanti_objc_recv v)) = v getANTI_OBJC_ARG (L _ (T.Tanti_objc_arg v)) = v getANTI_OBJC_ARGS (L _ (T.Tanti_objc_args v)) = v +-- +-- ISPC +-- +getANTI_FOREACH_ITERS (L _ (T.Tanti_foreach_iters v)) = v + lexer :: (L T.Token -> P a) -> P a lexer cont = do t <- lexToken diff --git a/Language/C/Parser/Tokens.hs b/Language/C/Parser/Tokens.hs index 049a15f..3259fc1 100644 --- a/Language/C/Parser/Tokens.hs +++ b/Language/C/Parser/Tokens.hs @@ -220,6 +220,7 @@ data Token = Teof | TISPCcif | TISPCcfor | TISPCcdo + | Tanti_foreach_iters String -- Clang (currently active is Objective-C is active) | T__block @@ -355,6 +356,11 @@ instance Show Token where show (Tanti_objc_arg s) = showAnti "kwarg" s show (Tanti_objc_args s) = showAnti "kwargs" s + -- + -- ISPC + -- + show (Tanti_foreach_iters s) = showAnti "foreachiters" s + show t = fromMaybe (error "language-c-quote: internal error: unknown token") (lookup t tokenStrings) diff --git a/Language/C/Pretty.hs b/Language/C/Pretty.hs index c93e5e2..3b4cf04 100644 --- a/Language/C/Pretty.hs +++ b/Language/C/Pretty.hs @@ -747,15 +747,15 @@ instance Pretty Stm where <> text "@autoreleasepool" ppr block - ppr (ForEach var ini to stm sloc) = + ppr (ForEach inis stm sloc) = srcloc sloc <> text "foreach" <+> - parens (ppr var <> text " = " <> ppr ini <> text " ... " <> ppr to) <> + parens (commasep $ map ppr inis) <> pprBlock stm - ppr (ForEachTiled var ini to stm sloc) = + ppr (ForEachTiled inis stm sloc) = srcloc sloc <> text "foreach_tiled" <+> - parens (ppr var <> text " = " <> ppr ini <> text " ... " <> ppr to) <> + parens (commasep $ map ppr inis) <> pprBlock stm ppr (ForEachActive var stm sloc) = srcloc sloc <> @@ -1264,3 +1264,8 @@ instance Pretty ObjCRecv where ppr (ObjCRecvSuper loc) = pprLoc loc $ text "super" ppr (ObjCRecvExp e loc) = pprLoc loc $ ppr e ppr (AntiObjCRecv v _) = pprAnti "recv" v + +instance Pretty ForEachIter where + ppr (ForEachIter ident from to loc) = + pprLoc loc $ ppr ident <+> text "=" <+> ppr from <+> text "..." <+> ppr to + ppr (AntiForEachIters v _) = pprAnti "foreachiters" v \ No newline at end of file diff --git a/Language/C/Quote/Base.hs b/Language/C/Quote/Base.hs index 398879a..22eeac3 100644 --- a/Language/C/Quote/Base.hs +++ b/Language/C/Quote/Base.hs @@ -501,6 +501,13 @@ qqObjCArgsE (C.AntiObjCArgs a _: args) = qqObjCArgsE (arg : args) = Just [|$(dataToExpQ qqExp arg) : $(dataToExpQ qqExp args)|] +qqForEachItersE :: [C.ForEachIter] -> Maybe (Q Exp) +qqForEachItersE [] = Just [|[]|] +qqForEachItersE (C.AntiForEachIters e _:elems) = + Just [|$(antiVarE e) ++ $(dataToExpQ qqExp elems)|] +qqForEachItersE (elem : elems) = + Just [|$(dataToExpQ qqExp elem) : $(dataToExpQ qqExp elems)|] + qqExp :: Typeable a => a -> Maybe (Q Exp) qqExp = const Nothing `extQ` qqStringE `extQ` qqIdE @@ -541,6 +548,7 @@ qqExp = const Nothing `extQ` qqStringE `extQ` qqObjCRecvE `extQ` qqObjCArgE `extQ` qqObjCArgsE + `extQ` qqForEachItersE antiVarP :: String -> PatQ antiVarP = either fail return . parsePat diff --git a/Language/C/Quote/ISPC.hs b/Language/C/Quote/ISPC.hs index 1e23144..24e0dcb 100644 --- a/Language/C/Quote/ISPC.hs +++ b/Language/C/Quote/ISPC.hs @@ -27,7 +27,8 @@ module Language.C.Quote.ISPC ( citem, citems, cunit, - cfun + cfun, + ispcforeachiters ) where import qualified Language.C.Parser as P @@ -64,3 +65,6 @@ citems = quasiquote exts typenames P.parseBlockItems ctyquals = quasiquote exts typenames P.parseTypeQuals cty = quasiquote exts typenames P.parseType cunit = quasiquote exts typenames P.parseUnit + +ispcforeachiters :: QuasiQuoter +ispcforeachiters = quasiquote exts typenames P.parseISPCForEachIters \ No newline at end of file diff --git a/Language/C/Syntax-instances.hs b/Language/C/Syntax-instances.hs index 6e75751..561dc9b 100644 --- a/Language/C/Syntax-instances.hs +++ b/Language/C/Syntax-instances.hs @@ -180,8 +180,8 @@ instance Located Stm where locOf (ObjCThrow _ l) = locOf l locOf (ObjCSynchronized _ _ l) = locOf l locOf (ObjCAutoreleasepool _ l) = locOf l - locOf (ForEach _ _ _ _ l) = locOf l - locOf (ForEachTiled _ _ _ _ l) = locOf l + locOf (ForEach _ _ l) = locOf l + locOf (ForEachTiled _ _ l) = locOf l locOf (ForEachActive _ _ l) = locOf l locOf (ForEachUnique _ _ _ l) = locOf l locOf (Unmasked _ l) = locOf l @@ -320,6 +320,9 @@ instance Located ObjCArg where instance Located ObjCDictElem where locOf (ObjCDictElem _ _ l) = locOf l locOf (AntiObjCDictElems _ l) = locOf l +instance Located ForEachIter where + locOf (ForEachIter _ _ _ l) = locOf l + locOf (AntiForEachIters _ l) = locOf l instance Relocatable Id where reloc l (Id x0 _) = (Id x0 (fromLoc l)) reloc l (AntiId x0 _) = (AntiId x0 (fromLoc l)) @@ -508,8 +511,8 @@ instance Relocatable Stm where reloc l (ObjCThrow x0 _) = (ObjCThrow x0 (fromLoc l)) reloc l (ObjCSynchronized x0 x1 _) = (ObjCSynchronized x0 x1 (fromLoc l)) reloc l (ObjCAutoreleasepool x0 _) = (ObjCAutoreleasepool x0 (fromLoc l)) - reloc l (ForEach x0 x1 x2 x3 _) = (ForEach x0 x1 x2 x3 (fromLoc l)) - reloc l (ForEachTiled x0 x1 x2 x3 _) = (ForEachTiled x0 x1 x2 x3 (fromLoc l)) + reloc l (ForEach x0 x1 _) = (ForEach x0 x1 (fromLoc l)) + reloc l (ForEachTiled x0 x1 _) = (ForEachTiled x0 x1 (fromLoc l)) reloc l (ForEachActive x0 x1 _) = (ForEachActive x0 x1 (fromLoc l)) reloc l (ForEachUnique x0 x1 x2 _) = (ForEachUnique x0 x1 x2 (fromLoc l)) reloc l (Unmasked x0 _) = (Unmasked x0 (fromLoc l)) @@ -650,3 +653,6 @@ instance Relocatable ObjCArg where instance Relocatable ObjCDictElem where reloc l (ObjCDictElem x0 x1 _) = (ObjCDictElem x0 x1 (fromLoc l)) reloc l (AntiObjCDictElems x0 _) = (AntiObjCDictElems x0 (fromLoc l)) +instance Relocatable ForEachIter where + reloc l (ForEachIter x0 x1 x2 _) = (ForEachIter x0 x1 x2 (fromLoc l)) + reloc l (AntiForEachIters x0 _) = (AntiForEachIters x0 (fromLoc l)) \ No newline at end of file diff --git a/Language/C/Syntax.hs b/Language/C/Syntax.hs index 1a44919..de04a22 100644 --- a/Language/C/Syntax.hs +++ b/Language/C/Syntax.hs @@ -293,9 +293,9 @@ data Stm = Label Id [Attr] Stm !SrcLoc | ObjCAutoreleasepool [BlockItem] !SrcLoc -- ISPC - | ForEach Id Exp Exp Stm !SrcLoc + | ForEach [ForEachIter] Stm !SrcLoc | ForEachActive Id Stm !SrcLoc - | ForEachTiled Id Exp Exp Stm !SrcLoc + | ForEachTiled [ForEachIter] Stm !SrcLoc | ForEachUnique Id Exp Stm !SrcLoc | Unmasked Stm !SrcLoc | CIf Exp Stm (Maybe Stm) !SrcLoc @@ -564,6 +564,17 @@ data ExeConfig = ExeConfig } deriving (Eq, Ord, Show, Data, Typeable) +{------------------------------------------------------------------------------ + - + - ISPC + - + ------------------------------------------------------------------------------} + +data ForEachIter = + ForEachIter Id Exp Exp !SrcLoc + | AntiForEachIters String !SrcLoc + deriving (Eq, Ord, Show, Data, Typeable) + {------------------------------------------------------------------------------ - - Instances From 080465a1f6546b0a7e0f48d6ffac67ef4975dd4b Mon Sep 17 00:00:00 2001 From: pema99 Date: Tue, 12 Apr 2022 01:02:22 +0200 Subject: [PATCH 14/20] Bit of cleanup --- Language/C/Quote/ISPC.hs | 7 ++++--- bin/gen-instances.hs | 2 ++ tests/unit/ISPC.hs | 22 ++++++++++------------ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Language/C/Quote/ISPC.hs b/Language/C/Quote/ISPC.hs index 24e0dcb..9a6d896 100644 --- a/Language/C/Quote/ISPC.hs +++ b/Language/C/Quote/ISPC.hs @@ -41,11 +41,12 @@ exts = [C.ISPC] typenames :: [String] typenames = - concatMap (typeN 4) ["int", "uint"] - ++ ["float","float16", "double"] + concatMap (typeN 6) ["int", "uint"] + ++ ["float", "float16", "double", "bool", "void"] + ++ ["size_t", "ptrdiff_t", "intptr_t", "uintptr_t"] typeN :: Int -> String -> [String] -typeN k typename = [typename ++ show (2^n) | n <- [3..k+2]] +typeN k typename = [typename ++ show (2^n :: Integer) | n <- [3..k]] cdecl, cedecl, cenum, cexp, cfun, cinit, cparam, cparams, csdecl, cstm, cstms :: QuasiQuoter citem, citems, ctyquals, cty, cunit :: QuasiQuoter diff --git a/bin/gen-instances.hs b/bin/gen-instances.hs index a5d1d99..b88e972 100644 --- a/bin/gen-instances.hs +++ b/bin/gen-instances.hs @@ -57,6 +57,7 @@ main = do DERIVE(ObjCRecv) DERIVE(ObjCArg) DERIVE(ObjCDictElem) + DERIVE(ForEachIter) #undef DERIVE #define DERIVE(a) deriveM deriveRelocatable (undefined::a) @@ -103,3 +104,4 @@ main = do DERIVE(ObjCRecv) DERIVE(ObjCArg) DERIVE(ObjCDictElem) + DERIVE(ForEachIter) diff --git a/tests/unit/ISPC.hs b/tests/unit/ISPC.hs index 0332574..a70c0e1 100644 --- a/tests/unit/ISPC.hs +++ b/tests/unit/ISPC.hs @@ -12,7 +12,6 @@ import qualified Data.ByteString.Char8 as B import Data.Char (isSpace) import Data.Loc (SrcLoc, noLoc, startPos) import Control.Exception (SomeException) -import Language.C.Quote.ISPC as ISPC import Language.C.Smart () import qualified Language.C.Syntax as C import qualified Language.C.Parser as P @@ -40,36 +39,35 @@ ispcTests = testGroup "ISPC land" test_foreach :: Assertion test_foreach = pretty 80 (ppr decl) @?= expected where - decl = [ISPC.cstm|foreach(a = 1 ... n){int a = 10;}|] + decl = [handleErrorcstm|foreach(a = 1 ... n){int a = 10;}|] expected = "foreach (a = 1 ... n) {\n int a = 10;\n}" test_foreach_active :: Assertion test_foreach_active = pretty 80 (ppr decl) @?= expected where - decl = [ISPC.cstm|foreach_active(index){int a = 10;}|] + decl = [handleErrorcstm|foreach_active(index){int a = 10;}|] expected = "foreach_active (index) {\n int a = 10;\n}" test_foreach_tiled :: Assertion test_foreach_tiled = pretty 80 (ppr decl) @?= expected - where decl = [ISPC.cstm|foreach_tiled(a = 1 ...n){int a = 10;}|] + where decl = [handleErrorcstm|foreach_tiled(a = 1 ...n){int a = 10;}|] expected = "foreach_tiled (a = 1 ... n) {\n int a = 10;\n}" test_foreach_unique :: Assertion test_foreach_unique = pretty 80 (ppr decl) @?= expected - where decl = [ISPC.cstm|foreach_unique(y in x){int a = 10;}|] + where decl = [handleErrorcstm|foreach_unique(y in x){int a = 10;}|] expected = "foreach_unique (y in x) {\n int a = 10;\n}" test_unmasked_qualifier :: Assertion test_unmasked_qualifier = pretty 80 (ppr decl) @?= expected where - decl = [ISPC.cedecl|extern "C" unmasked void func(int * uniform b, float c);|] + decl = [handleErrorcedecl|extern "C" unmasked void func(int * uniform b, float c);|] expected = "extern \"C\" unmasked void func(int *uniform b, float c);" - test_unmasked_qualifier_wvars :: Assertion test_unmasked_qualifier_wvars = pretty 80 (ppr decl) @?= expected where - decl = [ISPC.cedecl|extern "C" unmasked void $id:a(int * uniform $id:b, float $id:c);|] + decl = [handleErrorcedecl|extern "C" unmasked void $id:a(int * uniform $id:b, float $id:c);|] where a = "func" b = "b" @@ -79,25 +77,25 @@ test_unmasked_qualifier_wvars = pretty 80 (ppr decl) @?= expected test_uniform :: Assertion test_uniform = pretty 80 (ppr decl) @?= expected where - decl = [ISPC.cedecl|uniform int a = 10;|] + decl = [handleErrorcedecl|uniform int a = 10;|] expected = "uniform int a = 10;" test_varying :: Assertion test_varying = pretty 80 (ppr decl) @?= expected where - decl = [ISPC.cedecl|varying int a = 5;|] + decl = [handleErrorcedecl|varying int a = 5;|] expected = "varying int a = 5;" test_export :: Assertion test_export = pretty 80 (ppr decl) @?= expected where - decl = [ISPC.cedecl|export int func(int a, float b){int c = 10;}|] + decl = [handleErrorcedecl|export int func(int a, float b){int c = 10;}|] expected = "export int func(int a, float b)\n{\n int c = 10;\n}" test_extern :: Assertion test_extern = pretty 80 (ppr decl) @?= expected where - decl = [ISPC.cedecl|extern "C" void func(int a, float b);|] + decl = [handleErrorcedecl|extern "C" void func(int a, float b);|] expected = "extern \"C\" void func(int a, float b);" -- where From 8ebfbb256088dd342a09600b4b21c7d36ea3f231 Mon Sep 17 00:00:00 2001 From: pema99 Date: Tue, 12 Apr 2022 01:19:18 +0200 Subject: [PATCH 15/20] Various style changes --- Language/C/Parser/Lexer.x | 1 + Language/C/Parser/Parser.y | 8 +++++--- Language/C/Syntax.hs | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Language/C/Parser/Lexer.x b/Language/C/Parser/Lexer.x index a99607e..47dd95a 100644 --- a/Language/C/Parser/Lexer.x +++ b/Language/C/Parser/Lexer.x @@ -248,6 +248,7 @@ c :- ">>>" / { ifExtension cudaExts } { token TCUDA3gt } + -- -- ISPC -- diff --git a/Language/C/Parser/Parser.y b/Language/C/Parser/Parser.y index 6d1de68..1108989 100644 --- a/Language/C/Parser/Parser.y +++ b/Language/C/Parser/Parser.y @@ -19,8 +19,6 @@ import Control.Monad (forM_, unless, liftM) import Control.Monad.Exception -import Debug.Trace -import System.IO.Unsafe import Data.List (intersperse, sort) import Data.Loc import Data.Maybe (fromMaybe, catMaybes) @@ -362,12 +360,13 @@ import qualified Language.C.Syntax as C %name parseObjCMethodRecv objc_receiver %name parseObjCKeywordArg objc_keywordarg +%right NAMED OBJCNAMED + -- -- ISPC -- %name parseISPCForEachIters ispc_foreach_iter_list -%right NAMED OBJCNAMED %% {------------------------------------------------------------------------------ @@ -2126,6 +2125,7 @@ compound_statement: { mkBlock $3 ($1 `srcspan` $5) } | '{' begin_scope error {% unclosed (locOf $3) "{" } + block_item_list :: { [BlockItem] } block_item_list : block_item_rlist { rev $1 } @@ -2182,6 +2182,8 @@ selection_statement : { Switch $3 $5 ($1 `srcspan` $5) } | 'switch' '(' expression error {% unclosed ($2 <--> $3) "(" } + + -- ISPC | 'cif' '(' expression ')' statement { CIf $3 $5 Nothing ($1 `srcspan` $5) } | 'cif' '(' expression ')' statement 'else' statement diff --git a/Language/C/Syntax.hs b/Language/C/Syntax.hs index de04a22..98f8cb2 100644 --- a/Language/C/Syntax.hs +++ b/Language/C/Syntax.hs @@ -53,8 +53,8 @@ data Storage = Tauto !SrcLoc | TObjC__unsafe_unretained !SrcLoc -- ISPC - | TISPCexport !SrcLoc - | TISPCunmasked !SrcLoc + | TISPCexport !SrcLoc + | TISPCunmasked !SrcLoc deriving (Eq, Ord, Show, Data, Typeable) data TypeQual = Tconst !SrcLoc From cde10e3fee3a6d529a15ac7331483380c07a3d7d Mon Sep 17 00:00:00 2001 From: pema99 Date: Tue, 12 Apr 2022 01:26:52 +0200 Subject: [PATCH 16/20] Dont touch this --- Language/C/Pretty.hs | 6 ++---- bin/gen-instances.hs | 2 -- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/Language/C/Pretty.hs b/Language/C/Pretty.hs index 3b4cf04..5ac3339 100644 --- a/Language/C/Pretty.hs +++ b/Language/C/Pretty.hs @@ -762,17 +762,17 @@ instance Pretty Stm where text "foreach_active" <+> parens (ppr var) <> pprBlock stm - ppr (ForEachUnique ini var stm sloc) = srcloc sloc <> text "foreach_unique" <+> parens (ppr ini <> (text " in ") <> ppr var) <> pprBlock stm + ppr (Unmasked stm sloc) = srcloc sloc <> text "unmasked" <+> pprBlock stm - -- TODO do, while, for + ppr (CIf test then' maybe_else sloc) = srcloc sloc <> text "cif" <+> parens (ppr test) <> @@ -801,11 +801,9 @@ instance Pretty Stm where ppr (CWhile e stm sloc) = srcloc sloc <> text "cwhile" <+> parens (ppr e) <> pprBlock stm - ppr (CDo stm e sloc) = srcloc sloc <> text "cdo" <> pprBlock stm <+/> text "while" <> parens (ppr e) <> semi - ppr (CFor ini test post stm sloc) = srcloc sloc <> text "cfor" <+> diff --git a/bin/gen-instances.hs b/bin/gen-instances.hs index b88e972..a5d1d99 100644 --- a/bin/gen-instances.hs +++ b/bin/gen-instances.hs @@ -57,7 +57,6 @@ main = do DERIVE(ObjCRecv) DERIVE(ObjCArg) DERIVE(ObjCDictElem) - DERIVE(ForEachIter) #undef DERIVE #define DERIVE(a) deriveM deriveRelocatable (undefined::a) @@ -104,4 +103,3 @@ main = do DERIVE(ObjCRecv) DERIVE(ObjCArg) DERIVE(ObjCDictElem) - DERIVE(ForEachIter) From 62464c16bb8203e50a08d19483e3500643649702 Mon Sep 17 00:00:00 2001 From: pema99 Date: Tue, 12 Apr 2022 01:52:58 +0200 Subject: [PATCH 17/20] Some more tests --- Language/C/Parser/Parser.y | 2 +- language-c-quote.cabal | 1 + tests/unit/ISPC.hs | 75 +++++++++++++------------------------- 3 files changed, 27 insertions(+), 51 deletions(-) diff --git a/Language/C/Parser/Parser.y b/Language/C/Parser/Parser.y index 1108989..3a21c8f 100644 --- a/Language/C/Parser/Parser.y +++ b/Language/C/Parser/Parser.y @@ -315,7 +315,7 @@ import qualified Language.C.Syntax as C -- (1) Documented conflict in 'objc_protocol_declaration' -- (2) Objective-C exception syntax (would need lookahead of 2 to disambiguate properly) -- (3) The standard dangling else conflict --- (4) ISPC syntax for 'unmasked' keyword, it is both a control flow structure and a type qualifier. +-- (4) Ambiguity in ISPC syntax. %expect 4 %monad { P } { >>= } { return } diff --git a/language-c-quote.cabal b/language-c-quote.cabal index d1f794b..7fdb6ea 100644 --- a/language-c-quote.cabal +++ b/language-c-quote.cabal @@ -111,6 +111,7 @@ test-suite unit CUDA GCC Objc + ISPC MainCPP default-language: Haskell2010 diff --git a/tests/unit/ISPC.hs b/tests/unit/ISPC.hs index a70c0e1..da3bb53 100644 --- a/tests/unit/ISPC.hs +++ b/tests/unit/ISPC.hs @@ -1,8 +1,6 @@ {-# LANGUAGE QuasiQuotes #-} -module ISPC ( - ispcTests - ) where +module ISPC (ispcTests) where import Test.Framework import Test.Framework.Providers.HUnit @@ -12,6 +10,7 @@ import qualified Data.ByteString.Char8 as B import Data.Char (isSpace) import Data.Loc (SrcLoc, noLoc, startPos) import Control.Exception (SomeException) +import Language.C.Quote.ISPC import Language.C.Smart () import qualified Language.C.Syntax as C import qualified Language.C.Parser as P @@ -23,51 +22,60 @@ ispcTests = testGroup "ISPC land" [ testCase "ispc unmasked" test_unmasked_qualifier ,testCase "ispc unmasked wvars" test_unmasked_qualifier_wvars ,testCase "ispc foreach" test_foreach + ,testCase "ispc multidimensional foreach" test_foreach_multiple ,testCase "ispc foreach_active" test_foreach_active ,testCase "ispc foreach_tiled" test_foreach_tiled + ,testCase "ispc multidimensional foreach_tiled" test_foreach_tiled_multiple ,testCase "ispc uniform" test_uniform ,testCase "ispc varying" test_varying ,testCase "ispc export" test_export ,testCase "ispc extern" test_extern ,testCase "ispc foreach_unique" test_foreach_unique - --, testCase "attrs antiquote" test_attrs - --, testCase "attrs antiquote pretty" test_attr_p - --, testCase "case ranges quote" test_case_ranges - --, testCase "case ranges pretty" test_case_ranges_p ] test_foreach :: Assertion test_foreach = pretty 80 (ppr decl) @?= expected where - decl = [handleErrorcstm|foreach(a = 1 ... n){int a = 10;}|] + decl = [cstm|foreach(a = 1 ... n){int a = 10;}|] expected = "foreach (a = 1 ... n) {\n int a = 10;\n}" + +test_foreach_multiple :: Assertion +test_foreach_multiple = pretty 80 (ppr decl) @?= expected + where + decl = [cstm|foreach(a = 1 ... n, b = 2 ... m){int a = 10;}|] + expected = "foreach (a = 1 ... n, b = 2 ... m) {\n int a = 10;\n}" test_foreach_active :: Assertion test_foreach_active = pretty 80 (ppr decl) @?= expected where - decl = [handleErrorcstm|foreach_active(index){int a = 10;}|] + decl = [cstm|foreach_active(index){int a = 10;}|] expected = "foreach_active (index) {\n int a = 10;\n}" test_foreach_tiled :: Assertion test_foreach_tiled = pretty 80 (ppr decl) @?= expected - where decl = [handleErrorcstm|foreach_tiled(a = 1 ...n){int a = 10;}|] + where decl = [cstm|foreach_tiled(a = 1 ...n){int a = 10;}|] expected = "foreach_tiled (a = 1 ... n) {\n int a = 10;\n}" +test_foreach_tiled_multiple :: Assertion +test_foreach_tiled_multiple = pretty 80 (ppr decl) @?= expected + where decl = [cstm|foreach_tiled(a = 1 ...n, b= 2 ... m){int a = 10;}|] + expected = "foreach_tiled (a = 1 ... n, b = 2 ... m) {\n int a = 10;\n}" + test_foreach_unique :: Assertion test_foreach_unique = pretty 80 (ppr decl) @?= expected - where decl = [handleErrorcstm|foreach_unique(y in x){int a = 10;}|] + where decl = [cstm|foreach_unique(y in x){int a = 10;}|] expected = "foreach_unique (y in x) {\n int a = 10;\n}" test_unmasked_qualifier :: Assertion test_unmasked_qualifier = pretty 80 (ppr decl) @?= expected where - decl = [handleErrorcedecl|extern "C" unmasked void func(int * uniform b, float c);|] + decl = [cedecl|extern "C" unmasked void func(int * uniform b, float c);|] expected = "extern \"C\" unmasked void func(int *uniform b, float c);" test_unmasked_qualifier_wvars :: Assertion test_unmasked_qualifier_wvars = pretty 80 (ppr decl) @?= expected where - decl = [handleErrorcedecl|extern "C" unmasked void $id:a(int * uniform $id:b, float $id:c);|] + decl = [cedecl|extern "C" unmasked void $id:a(int * uniform $id:b, float $id:c);|] where a = "func" b = "b" @@ -77,56 +85,23 @@ test_unmasked_qualifier_wvars = pretty 80 (ppr decl) @?= expected test_uniform :: Assertion test_uniform = pretty 80 (ppr decl) @?= expected where - decl = [handleErrorcedecl|uniform int a = 10;|] + decl = [cedecl|uniform int a = 10;|] expected = "uniform int a = 10;" test_varying :: Assertion test_varying = pretty 80 (ppr decl) @?= expected where - decl = [handleErrorcedecl|varying int a = 5;|] + decl = [cedecl|varying int a = 5;|] expected = "varying int a = 5;" test_export :: Assertion test_export = pretty 80 (ppr decl) @?= expected where - decl = [handleErrorcedecl|export int func(int a, float b){int c = 10;}|] + decl = [cedecl|export int func(int a, float b){int c = 10;}|] expected = "export int func(int a, float b)\n{\n int c = 10;\n}" test_extern :: Assertion test_extern = pretty 80 (ppr decl) @?= expected where - decl = [handleErrorcedecl|extern "C" void func(int a, float b);|] + decl = [cedecl|extern "C" void func(int a, float b);|] expected = "extern \"C\" void func(int a, float b);" - --- where --- test_attr :: Assertion --- test_attr = --- [cedecl| int test __attribute__(($attr:a,$attr:b));|] --- @?= --- [cedecl| int test __attribute__((section(".sram2"), noinit));|] --- where --- a = [cattr| section(".sram2") |] --- b = [cattr| noinit |] --- --- test_attrs :: Assertion --- test_attrs = --- [cedecl| int test __attribute__(($attrs:as));|] --- @?= --- [cedecl| int test __attribute__((section(".sram2"), noinit));|] --- where --- a = [cattr| section(".sram2") |] --- b = [cattr| noinit |] --- as = [ a, b ] --- --- test_attr_p :: Assertion --- test_attr_p = --- pretty 80 (ppr [cattr|section(".sram2")|]) @?= "section(\".sram2\")" --- --- test_case_ranges :: Assertion --- test_case_ranges = assert $ case [cstm| case 10 ... 20: ; |] of --- C.CaseRange 10 20 (C.Exp Nothing _) _ -> True --- _ -> False --- --- test_case_ranges_p :: Assertion --- test_case_ranges_p = --- pretty 80 (ppr [cstm| case 10 ... 20: ; |]) @?= "\ncase 10 ... 20:\n;" From 836b552771f672f4c8498aaccb5d236d5e490ed3 Mon Sep 17 00:00:00 2001 From: pema99 Date: Tue, 12 Apr 2022 02:09:19 +0200 Subject: [PATCH 18/20] More tests --- tests/unit/ISPC.hs | 67 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/tests/unit/ISPC.hs b/tests/unit/ISPC.hs index da3bb53..b220a90 100644 --- a/tests/unit/ISPC.hs +++ b/tests/unit/ISPC.hs @@ -18,19 +18,25 @@ import Text.PrettyPrint.Mainland import Text.PrettyPrint.Mainland.Class ispcTests :: Test -ispcTests = testGroup "ISPC land" +ispcTests = testGroup "ISPC" [ testCase "ispc unmasked" test_unmasked_qualifier - ,testCase "ispc unmasked wvars" test_unmasked_qualifier_wvars - ,testCase "ispc foreach" test_foreach - ,testCase "ispc multidimensional foreach" test_foreach_multiple - ,testCase "ispc foreach_active" test_foreach_active - ,testCase "ispc foreach_tiled" test_foreach_tiled - ,testCase "ispc multidimensional foreach_tiled" test_foreach_tiled_multiple - ,testCase "ispc uniform" test_uniform - ,testCase "ispc varying" test_varying - ,testCase "ispc export" test_export - ,testCase "ispc extern" test_extern - ,testCase "ispc foreach_unique" test_foreach_unique + , testCase "ispc unmasked wvars" test_unmasked_qualifier_wvars + , testCase "ispc foreach" test_foreach + , testCase "ispc multidimensional foreach" test_foreach_multiple + , testCase "ispc foreach_active" test_foreach_active + , testCase "ispc foreach_tiled" test_foreach_tiled + , testCase "ispc multidimensional foreach_tiled" test_foreach_tiled_multiple + , testCase "ispc uniform" test_uniform + , testCase "ispc varying" test_varying + , testCase "ispc export" test_export + , testCase "ispc extern" test_extern + , testCase "ispc foreach_unique" test_foreach_unique + , testCase "ispc pointer qualifiers" test_pointer_quals + , testCase "ispc coherent if" test_cif + , testCase "ispc coherent while" test_cwhile + , testCase "ispc coherent for" test_cfor + , testCase "ispc coherent do" test_cdo + , testCase "ispc foreach iters" test_foreachiters ] test_foreach :: Assertion @@ -105,3 +111,40 @@ test_extern = pretty 80 (ppr decl) @?= expected where decl = [cedecl|extern "C" void func(int a, float b);|] expected = "extern \"C\" void func(int a, float b);" + +test_pointer_quals :: Assertion +test_pointer_quals = pretty 80 (ppr decl) @?= expected + where + decl = [cedecl|uniform int * varying a;|] + expected = "uniform int *varying a;" + +test_cif :: Assertion +test_cif = pretty 80 (ppr decl) @?= expected + where + decl = [cstm|cif (cond) { int a = 10; }|] + expected = "cif (cond) {\n int a = 10;\n}" + +test_cwhile :: Assertion +test_cwhile = pretty 80 (ppr decl) @?= expected + where + decl = [cstm|cwhile (cond) { int a = 10; }|] + expected = "cwhile (cond) {\n int a = 10;\n}" + +test_cfor :: Assertion +test_cfor = pretty 80 (ppr decl) @?= expected + where + decl = [cstm|cfor (uniform int i = 0; i < 5; i++) { int a = 10; }|] + expected = "cfor (uniform int i = 0; i < 5; i++) {\n int a = 10;\n}" + +test_cdo :: Assertion +test_cdo = pretty 80 (ppr decl) @?= expected + where + decl = [cstm|cdo { int a = 10; } while (cond);|] + expected = "cdo {\n int a = 10;\n} while(cond);" + +test_foreachiters :: Assertion +test_foreachiters = pretty 80 (ppr decl) @?= expected + where + iters = [ispcforeachiters|i = 5 ... n, j = 8 ... m|] + decl = [cstm|foreach ($foreachiters:iters, k = 0 ... l) { int a = 10; }|] + expected = "foreach (i = 5 ... n, j = 8 ... m, k = 0 ... l) {\n int a = 10;\n}" \ No newline at end of file From f1cdbdca33a79d8e9301cad981fd35ef7129f2d6 Mon Sep 17 00:00:00 2001 From: pema99 Date: Tue, 12 Apr 2022 02:15:34 +0200 Subject: [PATCH 19/20] Update note --- Language/C/Parser/Parser.y | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Language/C/Parser/Parser.y b/Language/C/Parser/Parser.y index 3a21c8f..de781c4 100644 --- a/Language/C/Parser/Parser.y +++ b/Language/C/Parser/Parser.y @@ -315,7 +315,7 @@ import qualified Language.C.Syntax as C -- (1) Documented conflict in 'objc_protocol_declaration' -- (2) Objective-C exception syntax (would need lookahead of 2 to disambiguate properly) -- (3) The standard dangling else conflict --- (4) Ambiguity in ISPC syntax. +-- (4) The dangling else conflict, but for the 'cif' construct in ISPC. %expect 4 %monad { P } { >>= } { return } From caed24e5d17a0f4a99dd6798f0f9097a2ee87950 Mon Sep 17 00:00:00 2001 From: pema99 Date: Mon, 18 Apr 2022 12:25:39 +0200 Subject: [PATCH 20/20] Fix pretty printing of deref statements, such as *((int*)&a) --- Language/C/Pretty.hs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Language/C/Pretty.hs b/Language/C/Pretty.hs index 5ac3339..8095745 100644 --- a/Language/C/Pretty.hs +++ b/Language/C/Pretty.hs @@ -923,6 +923,11 @@ instance Pretty Exp where parensIf (p > unopPrec) $ ppr op <> pprPrec unopPrec1 e + pprPrec p (UnOp op@Deref e loc) = + pprLoc loc $ + parensIf (p > unopPrec) $ + ppr op <> pprPrec unopPrec1 e + pprPrec p (UnOp op e loc) = pprLoc loc $ prefixop p op e