From bf17a1f9ecec633a3f0a64da0fc9d54fd9e78000 Mon Sep 17 00:00:00 2001 From: Lucas Kramer Date: Mon, 22 Jul 2024 11:52:30 -0700 Subject: [PATCH 1/4] Add a BuildList utility --- src/Libraries/Base3-Misc/BuildList.bs | 25 +++++++++++++++++++++++++ src/Libraries/Base3-Misc/Misc.bsv | 1 + 2 files changed, 26 insertions(+) create mode 100644 src/Libraries/Base3-Misc/BuildList.bs diff --git a/src/Libraries/Base3-Misc/BuildList.bs b/src/Libraries/Base3-Misc/BuildList.bs new file mode 100644 index 000000000..282c33782 --- /dev/null +++ b/src/Libraries/Base3-Misc/BuildList.bs @@ -0,0 +1,25 @@ +package BuildList where + +import List + +-- A typeclass used to implement a vector construction function which can take +-- any number of arguments (>0). +-- The type parameter `a` is the type of the elements in the list. +-- The type parameter `r` is the return type of the function, which can be a +-- list (base case) or a function (recursive case) that takes another element +-- and returns a new function. +-- The list here is built in reverse for efficiency, and then reversed. +class BuildList a r | r -> a where + lst' :: List a -> a -> r + +instance BuildList a (List a) where + lst' l x = reverse $ x :> l + +instance (BuildList a r) => BuildList a (a -> r) where + lst' l x y = lst' (x :> l) y + +-- Example usage: +-- lst 1 2 3 4 5 => 1 :> 2 :> 3 :> 4 :> 5 :> nil +-- lst False => False :> nil +lst :: (BuildList a r) => a -> r +lst x = lst' nil x diff --git a/src/Libraries/Base3-Misc/Misc.bsv b/src/Libraries/Base3-Misc/Misc.bsv index 13f624428..de6675418 100644 --- a/src/Libraries/Base3-Misc/Misc.bsv +++ b/src/Libraries/Base3-Misc/Misc.bsv @@ -3,6 +3,7 @@ package Misc; import Arbiter::*; import BRAM::*; import BRAMFIFO::*; +import BuildList::*; import BuildVector::*; import BUtils::*; import BypassReg::*; From b0b2128643cf80dfb4c95db612f423fac1bda9bf Mon Sep 17 00:00:00 2001 From: Julie Schwartz Date: Tue, 13 Aug 2024 11:31:13 +1200 Subject: [PATCH 2/4] Testsuite: Add tests for the BuildList library adapted from the tests for BuildVector --- testsuite/bsc.lib/BuildList/BuildList.exp | 8 ++++ testsuite/bsc.lib/BuildList/Makefile | 5 +++ testsuite/bsc.lib/BuildList/TestBuildList.bs | 42 +++++++++++++++++++ .../bsc.lib/BuildList/TestBuildListFail.bs | 27 ++++++++++++ .../TestBuildListFail.bs.bsc-out.expected | 24 +++++++++++ .../BuildList/sysTestBuildList.out.expected | 10 +++++ 6 files changed, 116 insertions(+) create mode 100644 testsuite/bsc.lib/BuildList/BuildList.exp create mode 100644 testsuite/bsc.lib/BuildList/Makefile create mode 100644 testsuite/bsc.lib/BuildList/TestBuildList.bs create mode 100644 testsuite/bsc.lib/BuildList/TestBuildListFail.bs create mode 100644 testsuite/bsc.lib/BuildList/TestBuildListFail.bs.bsc-out.expected create mode 100644 testsuite/bsc.lib/BuildList/sysTestBuildList.out.expected diff --git a/testsuite/bsc.lib/BuildList/BuildList.exp b/testsuite/bsc.lib/BuildList/BuildList.exp new file mode 100644 index 000000000..d540eff0f --- /dev/null +++ b/testsuite/bsc.lib/BuildList/BuildList.exp @@ -0,0 +1,8 @@ + +# Test functionality +test_veri_only TestBuildList + +# Test type check error messages +compile_fail TestBuildListFail.bs +compare_file TestBuildListFail.bs.bsc-out + diff --git a/testsuite/bsc.lib/BuildList/Makefile b/testsuite/bsc.lib/BuildList/Makefile new file mode 100644 index 000000000..b953e8132 --- /dev/null +++ b/testsuite/bsc.lib/BuildList/Makefile @@ -0,0 +1,5 @@ +# for "make clean" to work everywhere + +CONFDIR = $(realpath ../..) + +include $(CONFDIR)/clean.mk diff --git a/testsuite/bsc.lib/BuildList/TestBuildList.bs b/testsuite/bsc.lib/BuildList/TestBuildList.bs new file mode 100644 index 000000000..637a49b1c --- /dev/null +++ b/testsuite/bsc.lib/BuildList/TestBuildList.bs @@ -0,0 +1,42 @@ +package TestBuildList where + +import List +import BuildList + +{-# properties sysTestBuildList = { synthesize } #-} + +sysTestBuildList :: Module Empty +sysTestBuildList = + module + let + v1 :: List Bool + v1 = lst True False True True + + v2 :: List (UInt 8); + v2 = lst 7 32 + + v3 :: List Bool + v3 = lst False True True + + v4 :: List (UInt 4) + v4 = lst 3 + + done :: Reg Bool <- mkReg False + + rules + "r": when not done + ==> action + $display "v1[0] -> %b" (v1!!0) + $display "v1[1] -> %b" (v1!!1) + $display "v1[2] -> %b" (v1!!2) + $display "v1[3] -> %b" (v1!!3) + $display "v2[0] -> %d" (v2!!0) + $display "v2[1] -> %d" (v2!!1) + $display "v3[0] -> %b" (v3!!0) + $display "v3[1] -> %b" (v3!!1) + $display "v3[2] -> %b" (v3!!2) + $display "v4[0] -> %d" (v4!!0) + done := True + + "r2": when done + ==> $finish 0 diff --git a/testsuite/bsc.lib/BuildList/TestBuildListFail.bs b/testsuite/bsc.lib/BuildList/TestBuildListFail.bs new file mode 100644 index 000000000..d6b5d5ce0 --- /dev/null +++ b/testsuite/bsc.lib/BuildList/TestBuildListFail.bs @@ -0,0 +1,27 @@ +package TestBuildListFail where + +import List +import BuildList + +-- Test error messages + +-- Wrong element type, Literal +fn1 :: Bool +fn1 = + let v :: List Bool + v = lst 0 1 2 + in v == v + +-- Wrong element type, concrete +fn2 :: Bool +fn2 = + let v :: List Integer + v = lst True False True + in v == v + +-- Wrong return type +fn3 :: Bool +fn3 = + let v :: Bit 3 + v = lst True False True + in v == v diff --git a/testsuite/bsc.lib/BuildList/TestBuildListFail.bs.bsc-out.expected b/testsuite/bsc.lib/BuildList/TestBuildListFail.bs.bsc-out.expected new file mode 100644 index 000000000..ab6cb322e --- /dev/null +++ b/testsuite/bsc.lib/BuildList/TestBuildListFail.bs.bsc-out.expected @@ -0,0 +1,24 @@ +checking package dependencies +compiling TestBuildListFail.bs +Error: "TestBuildListFail.bs", line 12, column 13: (T0031) + The contexts for this expression could not be resolved because there are no + instances of the form: + Prelude.Literal Prelude.Bool + The context was implied by expressions at the following positions: + "TestBuildListFail.bs", line 12, column 17 +Error: "TestBuildListFail.bs", line 18, column 8: (T0032) + This expression requires the following context which could not be resolved: + BuildList.BuildList Prelude.Bool (Prelude.Bool -> Prelude.Bool -> Prelude.List Prelude.Integer) + The context was implied by expressions at the following positions: + "TestBuildListFail.bs", line 19, column 13 + An instance for this context exists, but it depends on the following context + for which there is no instance: + BuildList.BuildList Prelude.Bool (Prelude.List Prelude.Integer) +Error: "TestBuildListFail.bs", line 25, column 8: (T0032) + This expression requires the following context which could not be resolved: + BuildList.BuildList Prelude.Bool (Prelude.Bool -> Prelude.Bool -> Prelude.Bit 3) + The context was implied by expressions at the following positions: + "TestBuildListFail.bs", line 26, column 13 + An instance for this context exists, but it depends on the following context + for which there is no instance: + BuildList.BuildList Prelude.Bool (Prelude.Bit 3) diff --git a/testsuite/bsc.lib/BuildList/sysTestBuildList.out.expected b/testsuite/bsc.lib/BuildList/sysTestBuildList.out.expected new file mode 100644 index 000000000..3d6f2ff78 --- /dev/null +++ b/testsuite/bsc.lib/BuildList/sysTestBuildList.out.expected @@ -0,0 +1,10 @@ +v1[0] -> 1 +v1[1] -> 0 +v1[2] -> 1 +v1[3] -> 1 +v2[0] -> 7 +v2[1] -> 32 +v3[0] -> 0 +v3[1] -> 1 +v3[2] -> 1 +v4[0] -> 3 From 9c0178bc818a19b04ca5a173e291d59d939babb6 Mon Sep 17 00:00:00 2001 From: Julie Schwartz Date: Tue, 13 Aug 2024 13:13:33 +1200 Subject: [PATCH 3/4] Remove tab in BH file --- testsuite/bsc.lib/BuildList/TestBuildListFail.bs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testsuite/bsc.lib/BuildList/TestBuildListFail.bs b/testsuite/bsc.lib/BuildList/TestBuildListFail.bs index d6b5d5ce0..119835791 100644 --- a/testsuite/bsc.lib/BuildList/TestBuildListFail.bs +++ b/testsuite/bsc.lib/BuildList/TestBuildListFail.bs @@ -16,12 +16,12 @@ fn1 = fn2 :: Bool fn2 = let v :: List Integer - v = lst True False True + v = lst True False True in v == v -- Wrong return type fn3 :: Bool fn3 = let v :: Bit 3 - v = lst True False True + v = lst True False True in v == v From 09045e2545c5b9278f2a387fcb6e6d5719229fa2 Mon Sep 17 00:00:00 2001 From: Julie Schwartz Date: Tue, 13 Aug 2024 13:15:32 +1200 Subject: [PATCH 4/4] Remove trailing whitespace from BH files --- testsuite/bsc.lib/BuildList/TestBuildList.bs | 2 +- testsuite/bsc.lib/BuildList/TestBuildListFail.bs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/testsuite/bsc.lib/BuildList/TestBuildList.bs b/testsuite/bsc.lib/BuildList/TestBuildList.bs index 637a49b1c..b06a43d52 100644 --- a/testsuite/bsc.lib/BuildList/TestBuildList.bs +++ b/testsuite/bsc.lib/BuildList/TestBuildList.bs @@ -6,7 +6,7 @@ import BuildList {-# properties sysTestBuildList = { synthesize } #-} sysTestBuildList :: Module Empty -sysTestBuildList = +sysTestBuildList = module let v1 :: List Bool diff --git a/testsuite/bsc.lib/BuildList/TestBuildListFail.bs b/testsuite/bsc.lib/BuildList/TestBuildListFail.bs index 119835791..8538a4355 100644 --- a/testsuite/bsc.lib/BuildList/TestBuildListFail.bs +++ b/testsuite/bsc.lib/BuildList/TestBuildListFail.bs @@ -7,21 +7,21 @@ import BuildList -- Wrong element type, Literal fn1 :: Bool -fn1 = +fn1 = let v :: List Bool v = lst 0 1 2 in v == v -- Wrong element type, concrete fn2 :: Bool -fn2 = +fn2 = let v :: List Integer v = lst True False True in v == v -- Wrong return type fn3 :: Bool -fn3 = +fn3 = let v :: Bit 3 v = lst True False True in v == v