From 6f487aa39bd595c20312658321a2b8c1ce2bda61 Mon Sep 17 00:00:00 2001 From: Ryan Scott Date: Sun, 18 May 2025 08:31:59 -0400 Subject: [PATCH 1/5] Use primSelectFn instead of select `primSelectFn _ x i` will turn into `x[i]` in Bluespec SystemVerilog, which looks more natural than `select(x, i)`. --- src/Copilot/Compile/Bluespec/Expr.hs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Copilot/Compile/Bluespec/Expr.hs b/src/Copilot/Compile/Bluespec/Expr.hs index c4978ae..61d5d1f 100644 --- a/src/Copilot/Compile/Bluespec/Expr.hs +++ b/src/Copilot/Compile/Bluespec/Expr.hs @@ -630,7 +630,9 @@ cLit = BS.CLit . BS.CLiteral BS.NoPos -- | Create a Bluespec expression that indexes into a @Vector@. cIndexVector :: BS.CExpr -> BS.CExpr -> BS.CExpr cIndexVector vec idx = - BS.CApply (BS.CVar (BS.mkId BS.NoPos "select")) [vec, idx] + BS.CApply + (BS.CVar (BS.idPrimSelectFn BS.NoPos)) + [BS.CVar BS.idNoPosition, vec, idx] -- | Create a Bluespec expression that updates a @Vector@ element at a -- particular index. From 21cd147ff7eae8cc347c13c77ad065dd0d3ef311 Mon Sep 17 00:00:00 2001 From: Ryan Scott Date: Sun, 18 May 2025 08:36:07 -0400 Subject: [PATCH 2/5] Use CBinOp instead of CApply for binary operators This is required to produce syntactically valid Bluespec SystemVerilog, and it has the added bonus of making the Bluespec Haskell output look nicer. --- src/Copilot/Compile/Bluespec/CodeGen.hs | 28 +++++++++++++------------ src/Copilot/Compile/Bluespec/Expr.hs | 9 ++++---- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Copilot/Compile/Bluespec/CodeGen.hs b/src/Copilot/Compile/Bluespec/CodeGen.hs index 6d9fde0..b12ae6e 100644 --- a/src/Copilot/Compile/Bluespec/CodeGen.hs +++ b/src/Copilot/Compile/Bluespec/CodeGen.hs @@ -119,13 +119,13 @@ mkAccessDecln sId ty xs = nameId = BS.mkId BS.NoPos $ fromString name buffLength = cLit $ BS.LInt $ BS.ilDec $ toInteger $ length xs argId = BS.mkId BS.NoPos "x" - index = BS.CApply (BS.CVar (BS.idPercentAt BS.NoPos)) - [ BS.CApply (BS.CVar BS.idPlus) - [ BS.CVar (BS.mkId BS.NoPos (fromString (indexName sId))) - , BS.CVar argId - ] - , buffLength - ] + index = BS.CBinOp + (BS.CBinOp + (BS.CVar (BS.mkId BS.NoPos (fromString (indexName sId)))) + BS.idPlus + (BS.CVar argId)) + (BS.idPercentAt BS.NoPos) + buffLength indexExpr = cIndexVector (BS.CVar (BS.mkId BS.NoPos (fromString (streamName sId)))) index @@ -208,14 +208,16 @@ mkStepRule streams BS.Cwrite BS.NoPos (BS.CVar indexId) - (BS.CApply (BS.CVar (BS.idPercentAt BS.NoPos)) - [incIndex, buffLength]) + (BS.CBinOp + incIndex + (BS.idPercentAt BS.NoPos) + buffLength) where buffLength = cLit $ BS.LInt $ BS.ilDec $ toInteger $ length buff - incIndex = BS.CApply (BS.CVar BS.idPlus) - [ BS.CVar indexId - , cLit $ BS.LInt $ BS.ilDec 1 - ] + incIndex = BS.CBinOp + (BS.CVar indexId) + BS.idPlus + (cLit $ BS.LInt $ BS.ilDec 1) buffId = BS.mkId BS.NoPos $ fromString $ streamName sId genId = BS.mkId BS.NoPos $ fromString $ generatorName sId diff --git a/src/Copilot/Compile/Bluespec/Expr.hs b/src/Copilot/Compile/Bluespec/Expr.hs index 61d5d1f..3e25c3c 100644 --- a/src/Copilot/Compile/Bluespec/Expr.hs +++ b/src/Copilot/Compile/Bluespec/Expr.hs @@ -78,9 +78,10 @@ transOp1 op e = -- Bluespec's Arith class does not have a `recip` method corresponding to -- Haskell's `recip` in the `Fractional` class, so we implement it -- ourselves. - Recip ty -> BS.CApply - (BS.CVar (BS.idSlashAt BS.NoPos)) - [constNumTy ty 1, e] + Recip ty -> BS.CBinOp + (constNumTy ty 1) + (BS.idSlashAt BS.NoPos) + e BwNot _ty -> app $ BS.idInvertAt BS.NoPos Cast fromTy toTy -> transCast fromTy toTy e @@ -150,7 +151,7 @@ transOp2 op e1 e2 = Atan2 ty -> appFP ty "atan2" where app :: BS.Id -> BS.CExpr - app i = BS.CApply (BS.CVar i) [e1, e2] + app i = BS.CBinOp e1 i e2 appFP :: forall t. Type t -> String -> BS.CExpr appFP ty funPrefix = app $ fpFunId ty funPrefix From cf760efb554d2ee1083f4e4bf4917efd7a637efd Mon Sep 17 00:00:00 2001 From: Ryan Scott Date: Sun, 18 May 2025 08:38:09 -0400 Subject: [PATCH 3/5] Use idAssign instead of Cwrite for register updates --- src/Copilot/Compile/Bluespec/CodeGen.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Copilot/Compile/Bluespec/CodeGen.hs b/src/Copilot/Compile/Bluespec/CodeGen.hs index b12ae6e..ee9c296 100644 --- a/src/Copilot/Compile/Bluespec/CodeGen.hs +++ b/src/Copilot/Compile/Bluespec/CodeGen.hs @@ -198,16 +198,16 @@ mkStepRule streams where bufferUpdate = BS.CSExpr Nothing $ - BS.Cwrite - BS.NoPos + BS.CBinOp (cIndexVector (BS.CVar buffId) (BS.CVar indexId)) + BS.idAssign (BS.CVar genId) indexUpdate = BS.CSExpr Nothing $ - BS.Cwrite - BS.NoPos + BS.CBinOp (BS.CVar indexId) + BS.idAssign (BS.CBinOp incIndex (BS.idPercentAt BS.NoPos) From ecc4a705db769b634ff8041c161313c10d491011 Mon Sep 17 00:00:00 2001 From: Ryan Scott Date: Sun, 18 May 2025 13:26:34 -0400 Subject: [PATCH 4/5] Vendor in-flight copilot-bluespec changes --- cabal.project | 7 +++++++ src/Copilot/Compile/Bluespec/Compile.hs | 8 ++++---- 2 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 cabal.project diff --git a/cabal.project b/cabal.project new file mode 100644 index 0000000..4349842 --- /dev/null +++ b/cabal.project @@ -0,0 +1,7 @@ +packages: . + +-- TODO RGS: Remove this +source-repository-package + type: git + location: https://github.com/GaloisInc/language-bluespec + tag: 7f2daed111f6029953a63804500bd17ca6b5c4aa diff --git a/src/Copilot/Compile/Bluespec/Compile.hs b/src/Copilot/Compile/Bluespec/Compile.hs index c4701ea..e47aedb 100644 --- a/src/Copilot/Compile/Bluespec/Compile.hs +++ b/src/Copilot/Compile/Bluespec/Compile.hs @@ -16,7 +16,7 @@ import Data.Typeable (Typeable) import qualified Language.Bluespec.Classic.AST as BS import qualified Language.Bluespec.Classic.AST.Builtin.Ids as BS import qualified Language.Bluespec.Classic.AST.Builtin.Types as BS -import Text.PrettyPrint.HughesPJClass (Pretty (..), render) +import qualified Language.Bluespec.Classic.AST.Pretty as BS import System.Directory (createDirectoryIfMissing) import System.Exit (exitFailure) import System.FilePath (()) @@ -56,9 +56,9 @@ compileWith bsSettings prefix spec exitFailure | otherwise - = do let typesBsFile = render $ pPrint $ compileTypesBS bsSettings prefix spec - ifcBsFile = render $ pPrint $ compileIfcBS bsSettings prefix spec - bsFile = render $ pPrint $ compileBS bsSettings prefix spec + = do let typesBsFile = BS.ppReadable $ compileTypesBS bsSettings prefix spec + ifcBsFile = BS.ppReadable $ compileIfcBS bsSettings prefix spec + bsFile = BS.ppReadable $ compileBS bsSettings prefix spec let dir = bluespecSettingsOutputDirectory bsSettings createDirectoryIfMissing True dir From c803a418d2dd8fcc023c4e1cfe9204be221f8180 Mon Sep 17 00:00:00 2001 From: Ryan Scott Date: Sun, 18 May 2025 13:27:33 -0400 Subject: [PATCH 5/5] Draft: Try Bluespec SystemVerilog TODO RGS: Update test suite [ci skip] --- src/Copilot/Compile/Bluespec/Compile.hs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Copilot/Compile/Bluespec/Compile.hs b/src/Copilot/Compile/Bluespec/Compile.hs index e47aedb..cb5eb40 100644 --- a/src/Copilot/Compile/Bluespec/Compile.hs +++ b/src/Copilot/Compile/Bluespec/Compile.hs @@ -16,7 +16,7 @@ import Data.Typeable (Typeable) import qualified Language.Bluespec.Classic.AST as BS import qualified Language.Bluespec.Classic.AST.Builtin.Ids as BS import qualified Language.Bluespec.Classic.AST.Builtin.Types as BS -import qualified Language.Bluespec.Classic.AST.Pretty as BS +import qualified Language.Bluespec.SystemVerilog.AST.Pretty as BS import System.Directory (createDirectoryIfMissing) import System.Exit (exitFailure) import System.FilePath (()) @@ -56,17 +56,17 @@ compileWith bsSettings prefix spec exitFailure | otherwise - = do let typesBsFile = BS.ppReadable $ compileTypesBS bsSettings prefix spec - ifcBsFile = BS.ppReadable $ compileIfcBS bsSettings prefix spec - bsFile = BS.ppReadable $ compileBS bsSettings prefix spec + = do let typesBsFile = BS.pvpReadable $ compileTypesBS bsSettings prefix spec + ifcBsFile = BS.pvpReadable $ compileIfcBS bsSettings prefix spec + bsFile = BS.pvpReadable $ compileBS bsSettings prefix spec let dir = bluespecSettingsOutputDirectory bsSettings createDirectoryIfMissing True dir - writeFile (dir specTypesPkgName prefix ++ ".bs") typesBsFile - writeFile (dir specIfcPkgName prefix ++ ".bs") ifcBsFile + writeFile (dir specTypesPkgName prefix ++ ".bsv") typesBsFile + writeFile (dir specIfcPkgName prefix ++ ".bsv") ifcBsFile writeFile (dir "bs_fp.c") copilotBluespecFloatingPointC writeFile (dir "BluespecFP.bsv") copilotBluespecFloatingPointBSV - writeFile (dir prefix ++ ".bs") bsFile + writeFile (dir prefix ++ ".bsv") bsFile where triggers = specTriggers spec