Skip to content

Commit

Permalink
Rewriter: remove useless braces in a few more cases (#61)
Browse files Browse the repository at this point in the history
Curly braces can be removed in this case:
  if (x) y else { break; }

The previous optimization missed this case where there's a single (non-expression) statement in a block.
  • Loading branch information
laurentlb authored May 10, 2021
1 parent 1de5dee commit 2c18e5f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ let private simplifyStmt = function
let expr = List.reduce (fun acc x -> FunCall(Var ",", [acc;x])) (li@[e])
Jump(JumpKeyword.Return, Some expr)
else
Block (squeezeDeclarations b)
match squeezeDeclarations b with
| [stmt] -> stmt
| stmts -> Block stmts
| Decl (ty, li) -> Decl (rwType ty, declsNotToInline li)
| ForD((ty, d), cond, inc, body) -> ForD((rwType ty, declsNotToInline d), cond, inc, body)
// FIXME: properly handle booleans
Expand Down
4 changes: 4 additions & 0 deletions tests/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
--no-renaming --format js -o tests/real/disco.expected tests/real/disco.frag
--no-renaming --format nasm -o tests/real/leizex.expected tests/real/leizex.frag

# Unit tests

--no-renaming --format c-variables -o tests/unit/blocks.expected tests/unit/blocks.frag

# Partial renaming tests

--no-renaming --format c-variables -o tests/unit/function_comma.expected tests/unit/function_comma.frag
Expand Down
26 changes: 12 additions & 14 deletions tests/real/yx_long_way_from_home.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -157,31 +157,29 @@ const char *yx_long_way_from_home_frag =
"if(d(v,m,a,p,t))"
"{"
"float k=1.;"
"vec3 h=vec3(1);"
"vec3 b=vec3(1);"
"if(f==1)"
"h=vec3(.7);"
"b=vec3(.7);"
"k*=k;"
"{"
"v=a+p*.002;"
"vec3 S=reflect(m,p),u=e(p,1.);"
"m=normalize(mix(S,u,k));"
"r*=h;"
"vec3 h=reflect(m,p),u=e(p,1.);"
"m=normalize(mix(h,u,k));"
"r*=b;"
"}"
"vec3 u=d(y,n);"
"float S=dot(p,u);"
"vec3 B,b;"
"float R;"
"if(S>0.&&!d(a+p*.002,u,B,b,R))"
"o+=r*S*c;"
"vec3 h=d(y,n);"
"float u=dot(p,h);"
"vec3 S,R;"
"float B;"
"if(u>0.&&!d(a+p*.002,h,S,R,B))"
"o+=r*u*c;"
"i=s(i.y);"
"}"
"else"
" if(abs(t)>.1)"
"return o+l(m)*r;"
"else"
"{"
"break;"
"}"
" break;"
"}"
"return vec3(0);"
"}"
Expand Down
52 changes: 52 additions & 0 deletions tests/unit/blocks.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* File generated with Shader Minifier 1.1.6
* http://www.ctrl-alt-test.fr
*/
#ifndef BLOCKS_EXPECTED_
# define BLOCKS_EXPECTED_

const char *blocks_frag =
"float test_if()"
"{"
"int foo=2;"
"float bar;"
"if(foo==2)"
"foo++;"
"if(foo<5)"
"if(foo==3)"
"bar=.2;"
"else"
" bar=.3;"
"else"
" bar=.4;"
"return bar;"
"}"
"int k=5;"
"float test_for()"
"{"
"int foo=2,n=0;"
"for(int i=0;i<4;i++)"
"foo+=i;"
"for(foo++;n<4;n++)"
"{"
"int k=n-1;"
"foo+=k;"
"}"
"return 1./float(k);"
"}"
"int test_block()"
"{"
"if(k==0)"
";"
"for(int i=0;i<2;i++)"
"if(k==1)"
"return k++,2;"
"else"
" break;"
"}"
"void main()"
"{"
"float a=test_if(),b=test_for();"
"gl_FragColor=vec4(.2,a,b,0.);"
"}";

#endif // BLOCKS_EXPECTED_
9 changes: 9 additions & 0 deletions tests/unit/blocks.frag
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ float test_for()
return 1. / float(k);
}

int test_block()
{
{}
{if (k == 0) {} else {}}
for (int i = 0; i < 2; i++)
{
if (k == 1) {k++; return 2;} else {break;}
}
}

void main()
{
Expand Down

0 comments on commit 2c18e5f

Please sign in to comment.