From f7ae5b428046912ab9bc43ef4fc2ec5cfa741774 Mon Sep 17 00:00:00 2001 From: Lukasz Czajka Date: Mon, 8 Apr 2024 17:52:56 +0200 Subject: [PATCH 1/3] display negative field elements --- src/Juvix/Data/Field.hs | 16 ++++++++++------ tests/Casm/Compilation/positive/out/test074.out | 2 +- tests/Casm/positive/out/test014.out | 2 +- tests/Compilation/positive/out/test074.out | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Juvix/Data/Field.hs b/src/Juvix/Data/Field.hs index 6e701511f9..b2dfae5747 100644 --- a/src/Juvix/Data/Field.hs +++ b/src/Juvix/Data/Field.hs @@ -16,7 +16,7 @@ newtype FField = FField makeLenses ''FField defaultFieldSize :: Natural -defaultFieldSize = smallFieldSize +defaultFieldSize = cairoFieldSize cairoFieldSize :: Natural cairoFieldSize = 3618502788666131213697322783095070105623107215331596699973092056135872020481 @@ -35,10 +35,10 @@ instance Serialize FField where return $ fieldFromInteger n f instance Pretty FField where - pretty (FField (_ :&: f)) = pretty (toInteger f) + pretty f = pretty (fieldToInteger f) instance Show FField where - show (FField (_ :&: f)) = show (toInteger f) + show f = show (fieldToInteger f) fieldAdd :: FField -> FField -> FField fieldAdd @@ -87,11 +87,15 @@ fieldSize fieldToInteger :: FField -> Integer fieldToInteger - (FField ((_ :: Sing (p :: Natural)) :&: (f1 :: PrimeField p))) = - toInteger f1 + (FField ((n1 :: Sing (p :: Natural)) :&: (f1 :: PrimeField p))) + | i < n `div` 2 = i + | otherwise = -(n - i) + where + i = toInteger f1 + n = fromIntegral (fromSing n1) fieldToNatural :: FField -> Natural -fieldToNatural f = (fromIntegral (fieldToInteger f) + s) `mod` s +fieldToNatural f = (fromIntegral ((fieldToInteger f) + fromIntegral s)) `mod` s where s = fieldSize f diff --git a/tests/Casm/Compilation/positive/out/test074.out b/tests/Casm/Compilation/positive/out/test074.out index 2c8b00b0ff..eb3e83f932 100644 --- a/tests/Casm/Compilation/positive/out/test074.out +++ b/tests/Casm/Compilation/positive/out/test074.out @@ -1 +1 @@ -2532951952066291849588125948166549073936175050732117689981164439295110414326 +-1085550836599839364109196834928521031686932164599479009991927616840761606155 diff --git a/tests/Casm/positive/out/test014.out b/tests/Casm/positive/out/test014.out index 2c8b00b0ff..eb3e83f932 100644 --- a/tests/Casm/positive/out/test014.out +++ b/tests/Casm/positive/out/test014.out @@ -1 +1 @@ -2532951952066291849588125948166549073936175050732117689981164439295110414326 +-1085550836599839364109196834928521031686932164599479009991927616840761606155 diff --git a/tests/Compilation/positive/out/test074.out b/tests/Compilation/positive/out/test074.out index 4c68a1a641..eb3e83f932 100644 --- a/tests/Compilation/positive/out/test074.out +++ b/tests/Compilation/positive/out/test074.out @@ -1 +1 @@ -214748354 +-1085550836599839364109196834928521031686932164599479009991927616840761606155 From da830a64f8168961d8e84062d5bc0036bd36363a Mon Sep 17 00:00:00 2001 From: Lukasz Czajka Date: Mon, 8 Apr 2024 18:23:53 +0200 Subject: [PATCH 2/3] tests --- .../Compiler/Casm/Translation/FromReg.hs | 19 ++++++++++++++++--- test/Casm/Compilation/Positive.hs | 3 +-- test/Casm/Run/Positive.hs | 2 +- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Juvix/Compiler/Casm/Translation/FromReg.hs b/src/Juvix/Compiler/Casm/Translation/FromReg.hs index 2b676ef12e..ea7f13a8e7 100644 --- a/src/Juvix/Compiler/Casm/Translation/FromReg.hs +++ b/src/Juvix/Compiler/Casm/Translation/FromReg.hs @@ -2,6 +2,7 @@ module Juvix.Compiler.Casm.Translation.FromReg where import Data.HashMap.Strict qualified as HashMap import Data.HashSet qualified as HashSet +import Data.Text qualified as Text import Juvix.Compiler.Casm.Data.LabelInfoBuilder import Juvix.Compiler.Casm.Data.Limits import Juvix.Compiler.Casm.Data.Result @@ -38,7 +39,7 @@ fromReg tab = uncurry Result $ run $ runLabelInfoBuilderWithNextId (Reg.getNextS mkFunCall :: Symbol -> [Instruction] mkFunCall sym = - [ mkCallRel $ Lab $ LabelRef sym (Just $ Reg.lookupFunInfo tab sym ^. Reg.functionName), + [ mkCallRel $ Lab $ LabelRef sym (Just $ quoteName $ Reg.lookupFunInfo tab sym ^. Reg.functionName), Return, Nop ] @@ -50,10 +51,22 @@ fromReg tab = uncurry Result $ run $ runLabelInfoBuilderWithNextId (Reg.getNextS unsupported :: Text -> a unsupported what = error ("Cairo backend: unsupported: " <> what) + quoteName :: Text -> Text + quoteName txt = + foldr + (uncurry Text.replace) + txt + [ ("$", "__dollar__"), + (":", "__colon__"), + ("@", "__at__"), + ("ap", "__ap__"), + ("fp", "__fp__") + ] + goFun :: forall r. (Member LabelInfoBuilder r) => StdlibBuiltins -> LabelRef -> (Address, [[Instruction]]) -> Reg.FunctionInfo -> Sem r (Address, [[Instruction]]) goFun blts failLab (addr0, acc) funInfo = do let sym = funInfo ^. Reg.functionSymbol - funName = funInfo ^. Reg.functionName + funName = quoteName (funInfo ^. Reg.functionName) registerLabelName sym funName registerLabelAddress sym addr0 let lab = Label $ LabelRef sym (Just funName) @@ -400,7 +413,7 @@ fromReg tab = uncurry Result $ run $ runLabelInfoBuilderWithNextId (Reg.getNextS mapM_ goAssignApValue (reverse args) output'' $ mkCallRel $ Lab $ LabelRef sym (Just funName) where - funName = Reg.lookupFunInfo tab sym ^. Reg.functionName + funName = quoteName (Reg.lookupFunInfo tab sym ^. Reg.functionName) Reg.CallClosure cl -> do mapM_ goAssignApValue (reverse args) r <- mkMemRef cl diff --git a/test/Casm/Compilation/Positive.hs b/test/Casm/Compilation/Positive.hs index 48d3356c5d..34e1315428 100644 --- a/test/Casm/Compilation/Positive.hs +++ b/test/Casm/Compilation/Positive.hs @@ -405,8 +405,7 @@ tests = $(mkRelFile "out/test073.out"), posTest "Test074: Fields" - -- different textual format for "negative" field elements - False + True $(mkRelDir ".") $(mkRelFile "test074.juvix") $(mkRelFile "out/test074.out") diff --git a/test/Casm/Run/Positive.hs b/test/Casm/Run/Positive.hs index 9435e2a4d9..e2cd16f2c6 100644 --- a/test/Casm/Run/Positive.hs +++ b/test/Casm/Run/Positive.hs @@ -116,7 +116,7 @@ tests = $(mkRelFile "out/test013.out"), PosTest "Test014: Field arithmetic" - False -- different textual representation of "negative" field elements + True $(mkRelDir ".") $(mkRelFile "test014.casm") $(mkRelFile "out/test014.out") From 810bac2600be65f4d7718bc4d36a2d3a398783d9 Mon Sep 17 00:00:00 2001 From: Lukasz Czajka Date: Mon, 8 Apr 2024 18:51:57 +0200 Subject: [PATCH 3/3] fix tests --- tests/Core/positive/out/test061.out | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Core/positive/out/test061.out b/tests/Core/positive/out/test061.out index 4c68a1a641..eb3e83f932 100644 --- a/tests/Core/positive/out/test061.out +++ b/tests/Core/positive/out/test061.out @@ -1 +1 @@ -214748354 +-1085550836599839364109196834928521031686932164599479009991927616840761606155