From 2bba4125f8b81b28006b7bbcefd0c9a517e1c788 Mon Sep 17 00:00:00 2001 From: lep Date: Sun, 5 Mar 2023 13:48:40 +0100 Subject: [PATCH] Fixed type confusion bug when compiling unary minus Previously when `-a` was compiled in a `real` context but `a` is an integer `a` itself was being compiled in the real context, which is wrong. Instead a has to be compiled as an int and only then then converted to real. --- Hot/Instruction/Compiler.hs | 6 +++++- Main.hs | 6 +++--- flake.nix | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Hot/Instruction/Compiler.hs b/Hot/Instruction/Compiler.hs index ad3892e..9913b6a 100644 --- a/Hot/Instruction/Compiler.hs +++ b/Hot/Instruction/Compiler.hs @@ -252,8 +252,12 @@ compileExpr e = H.Call (Op "-") [a] -> do r <- newRegister - t <- compileExpr a + -- We need to compile `a` from `-a` as the type of `a` itself, + -- which does sound weird, but neccessary as otherwise a would be + -- put into the – let's say – real slot of the register when `a` + -- itself would be an integer ta <- typeOfExpr a + t <- typed ta $ compileExpr a emit $ Negate ta r t typedGet ta r diff --git a/Main.hs b/Main.hs index e4edf12..cfb7e4d 100644 --- a/Main.hs +++ b/Main.hs @@ -223,7 +223,7 @@ compileX o = do toCompile <- forM (inputPaths o) $ \j -> do src <- liftIO $ readFile j J.Programm ast <- exceptT $ parse J.programm j src - traceShowM ast + --traceShowM ast return ast return (commonj, J.Programm $ concat toCompile) case x of @@ -236,8 +236,8 @@ compileX o = do prog = jass_opt j prog' = H.jass2hot . fst $ Rename.compile Rename.Init id st prog asm = ins_opt $ Ins.compile typeHierachy prog' - traceShowM j - traceShowM prog + --traceShowM j + --traceShowM prog hPutBuilder stdout $ Ins.serializeAsm asm when (showSerialize o) $ do putStrLn "" diff --git a/flake.nix b/flake.nix index 2dada4b..76654af 100644 --- a/flake.nix +++ b/flake.nix @@ -59,6 +59,7 @@ buildInputs = [ pkgs.gnumake pkgs.cabal-install + ghcPackages ]; in rec { packages = {