From 2273d84fd1081d9e12f65398b9f1936b357b56ad Mon Sep 17 00:00:00 2001 From: Laurent Le Brun Date: Sat, 12 Nov 2022 20:41:31 +0100 Subject: [PATCH] Fix stackoverflow error in simplifyExpr (#165) This was caused by trying to reorder multiple arguments back and forth --- src/rewriter.fs | 8 ++++---- tests/unit/operators.expected | 4 ++++ tests/unit/operators.frag | 4 ++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/rewriter.fs b/src/rewriter.fs index e7076fa3..e91b0135 100644 --- a/src/rewriter.fs +++ b/src/rewriter.fs @@ -78,7 +78,7 @@ let private inlineFn (declArgs:Decl list) passedArgs bodyExpr = /// Expression that doesn't need parentheses around it. let (|NoParen|_|) = function - | Int _ | Float _ | Dot _ | Var _ | FunCall _ | Subscript _ as x -> Some x + | Int _ | Float _ | Dot _ | Var _ | FunCall (Var _, _) | Subscript _ as x -> Some x | _ -> None let rec private simplifyExpr (didInline: bool ref) env = function @@ -108,7 +108,7 @@ let rec private simplifyExpr (didInline: bool ref) env = function | FunCall(Op "-", [x; Int (i, s)]) when i < 0 -> FunCall(Op "+", [x; Int (-i, s)]) |> env.fExpr env - // Swap operands to get rid of parenthese + // Swap operands to get rid of parentheses // x*(y*z) -> y*z*x | FunCall(Op "*", [NoParen x; FunCall(Op "*", [y; z])]) -> FunCall(Op "*", [FunCall(Op "*", [y; z]); x]) |> env.fExpr env @@ -117,10 +117,10 @@ let rec private simplifyExpr (didInline: bool ref) env = function | FunCall(Op "+", [NoParen x; FunCall(Op ("+"|"-") as op, [y; z])]) -> FunCall(Op "+", [FunCall(op, [y; z]); x]) |> env.fExpr env // x-(y+z) -> x-y-z - | FunCall(Op "-", [NoParen x; FunCall(Op "+", [y; z])]) -> + | FunCall(Op "-", [x; FunCall(Op "+", [y; z])]) -> FunCall(Op "-", [FunCall(Op "-", [x; y]); z]) |> env.fExpr env // x-(y-z) -> x-y+z - | FunCall(Op "-", [NoParen x; FunCall(Op "-", [y; z])]) -> + | FunCall(Op "-", [x; FunCall(Op "-", [y; z])]) -> FunCall(Op "+", [FunCall(Op "-", [x; y]); z]) |> env.fExpr env // Boolean simplifications (let's ignore the suffix) diff --git a/tests/unit/operators.expected b/tests/unit/operators.expected index ad01da30..ce261155 100644 --- a/tests/unit/operators.expected +++ b/tests/unit/operators.expected @@ -20,4 +20,8 @@ "{" "int d=b*c*a;" "return a-b+1-d+c;" + "}" + "int other(int a,int b,int c,int d)" + "{" + "return a*b*(c*d)*(a*b);" "}", diff --git a/tests/unit/operators.frag b/tests/unit/operators.frag index bfb82ece..777a68e8 100644 --- a/tests/unit/operators.frag +++ b/tests/unit/operators.frag @@ -20,3 +20,7 @@ int no_parens2(int a, int b, int c) { int d = a*(b*c); return a - (b - 1) - (d - c); } + +int other(int a, int b, int c, int d) { + return (a*b)*(c*d)*(a*b); +}