From 57f8b6ba5b6cd3186ebbb8481ef57711e348b859 Mon Sep 17 00:00:00 2001 From: Samuel Hammersberg Date: Sat, 4 Feb 2023 11:35:57 +0100 Subject: [PATCH] Optimized add even further. --- src/Compiler/Compiler.hs | 19 ++++++++++++++++--- test/simple.sf | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Compiler/Compiler.hs b/src/Compiler/Compiler.hs index d5051d4..465a3f1 100644 --- a/src/Compiler/Compiler.hs +++ b/src/Compiler/Compiler.hs @@ -133,14 +133,27 @@ compile (Program e) = do --- aux functions --- emitAdd :: Exp -> Exp -> CompilerState + -- instead of declaring two variables for this case, + -- we can directly pass them to add. emitAdd (EInt i1) (EInt i2) = do - -- instead of declaring two variables for this case, - -- we can directly pass them to add. - -- //TODO This should also be done with (EInt & Exp) and (Exp &EInt) increaseVarCount v1 <- gets variableCount emit $ Variable $ Ident $ show v1 emit $ Add I64 (VInteger i1) (VInteger i2) + emitAdd (EInt i) e2 = do + go e2 + v2 <- gets variableCount + increaseVarCount + v3 <- gets variableCount + emit $ Variable $ Ident $ show v3 + emit $ Add I64 (VInteger i) (VIdent (Ident $ show v2)) + emitAdd e1 (EInt i) = do + go e1 + v2 <- gets variableCount + increaseVarCount + v3 <- gets variableCount + emit $ Variable $ Ident $ show v3 + emit $ Add I64 (VIdent (Ident $ show v2)) (VInteger i) emitAdd e1 e2 = do go e1 v1 <- gets variableCount diff --git a/test/simple.sf b/test/simple.sf index 7e10b26..2b92e13 100644 --- a/test/simple.sf +++ b/test/simple.sf @@ -2,4 +2,4 @@ -main = (123 + 4214) + 1231 + 314 +main = 1 + (123 + 4214) + 1231 + 314