@@ -136,10 +136,7 @@ class Wasm2JSBuilder {
136136
137137 // The second pass on an expression: process it fully, generating
138138 // JS
139- // @param result Whether the context we are in receives a value,
140- // and its type, or if not, then we can drop our return,
141- // if we have one.
142- Ref processFunctionBody (Module* m, Function* func, IString result);
139+ Ref processFunctionBody (Module* m, Function* func);
143140
144141 // Get a temp var.
145142 IString getTemp (Type type, Function* func) {
@@ -274,6 +271,7 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) {
274271 runner.add (" flatten" );
275272 runner.add (" simplify-locals-notee-nostructure" );
276273 runner.add (" reorder-locals" );
274+ runner.add (" remove-unused-names" );
277275 runner.add (" vacuum" );
278276 runner.add (" remove-unused-module-elements" );
279277 runner.setDebug (flags.debug );
@@ -632,6 +630,7 @@ Ref Wasm2JSBuilder::processFunction(Module* m, Function* func, bool standaloneFu
632630 runner.add (" flatten" );
633631 runner.add (" simplify-locals-notee-nostructure" );
634632 runner.add (" reorder-locals" );
633+ runner.add (" remove-unused-names" );
635634 runner.add (" vacuum" );
636635 runner.runOnFunction (func);
637636 }
@@ -665,7 +664,7 @@ Ref Wasm2JSBuilder::processFunction(Module* m, Function* func, bool standaloneFu
665664 size_t theVarIndex = ret[3 ]->size ();
666665 ret[3 ]->push_back (theVar);
667666 // body
668- flattenAppend (ret, processFunctionBody (m, func, NO_RESULT ));
667+ flattenAppend (ret, processFunctionBody (m, func));
669668 // vars, including new temp vars
670669 for (Index i = func->getVarIndexBase (); i < func->getNumLocals (); i++) {
671670 ValueBuilder::appendToVar (
@@ -684,7 +683,7 @@ Ref Wasm2JSBuilder::processFunction(Module* m, Function* func, bool standaloneFu
684683 return ret;
685684}
686685
687- Ref Wasm2JSBuilder::processFunctionBody (Module* m, Function* func, IString result ) {
686+ Ref Wasm2JSBuilder::processFunctionBody (Module* m, Function* func) {
688687 struct ExpressionProcessor : public Visitor <ExpressionProcessor, Ref> {
689688 Wasm2JSBuilder* parent;
690689 IString result; // TODO: remove
@@ -741,14 +740,9 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m, Function* func, IString resul
741740 }
742741
743742 Ref visitAndAssign (Expression* curr, IString result) {
743+ assert (result != NO_RESULT);
744744 Ref ret = visit (curr, result);
745- // if it's not already a statement, then it's an expression, and we need to assign it
746- // (if it is a statement, it already assigns to the result var)
747- if (result != NO_RESULT) {
748- ret = ValueBuilder::makeStatement (
749- ValueBuilder::makeBinary (ValueBuilder::makeName (result), SET, ret));
750- }
751- return ret;
745+ return ValueBuilder::makeStatement (ValueBuilder::makeBinary (ValueBuilder::makeName (result), SET, ret));
752746 }
753747
754748 Ref visitAndAssign (Expression* curr, ScopedTemp& temp) {
@@ -768,10 +762,6 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m, Function* func, IString resul
768762 return ret;
769763 }
770764
771- // For spooky return-at-a-distance/break-with-result, this tells us
772- // what the result var is for a specific label.
773- std::map<Name, IString> breakResults;
774-
775765 // Breaks to the top of a loop should be emitted as continues, to that loop's main label
776766 std::unordered_set<Name> continueLabels;
777767
@@ -782,7 +772,6 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m, Function* func, IString resul
782772 // Visitors
783773
784774 Ref visitBlock (Block* curr) {
785- breakResults[curr->name ] = result;
786775 Ref ret = ValueBuilder::makeBlock ();
787776 size_t size = curr->list .size ();
788777 auto noResults = result == NO_RESULT ? size : size-1 ;
@@ -799,20 +788,13 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m, Function* func, IString resul
799788 }
800789
801790 Ref visitIf (If* curr) {
802- IString temp;
803791 Ref condition = visit (curr->condition , EXPRESSION_RESULT);
804- Ref ifTrue = ValueBuilder::makeStatement ( visitAndAssign ( curr->ifTrue , result) );
792+ Ref ifTrue = visit ( curr->ifTrue , NO_RESULT );
805793 Ref ifFalse;
806794 if (curr->ifFalse ) {
807- ifFalse = ValueBuilder::makeStatement (visitAndAssign (curr->ifFalse , result));
808- }
809- if (temp.isNull ()) {
810- return ValueBuilder::makeIf (condition, ifTrue, ifFalse); // simple if
795+ ifFalse = visit (curr->ifFalse , NO_RESULT);
811796 }
812- condition = blockify (condition);
813- // just add an if to the block
814- condition[1 ]->push_back (ValueBuilder::makeIf (ValueBuilder::makeName (temp), ifTrue, ifFalse));
815- return condition;
797+ return ValueBuilder::makeIf (condition, ifTrue, ifFalse); // simple if
816798 }
817799
818800 Ref visitLoop (Loop* curr) {
@@ -842,13 +824,7 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m, Function* func, IString resul
842824 fakeIf.ifTrue = &fakeBreak;
843825 return visit (&fakeIf, result);
844826 }
845- Ref theBreak = makeBreakOrContinue (curr->name );
846- if (!curr->value ) return theBreak;
847- // generate the value, including assigning to the result, and then do the break
848- Ref ret = visitAndAssign (curr->value , breakResults[curr->name ]);
849- ret = blockify (ret);
850- ret[1 ]->push_back (theBreak);
851- return ret;
827+ return makeBreakOrContinue (curr->name );
852828 }
853829
854830 Expression* defaultBody = nullptr ; // default must be last in asm.js
@@ -1141,7 +1117,7 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m, Function* func, IString resul
11411117 }
11421118
11431119 Ref visitDrop (Drop* curr) {
1144- return visitAndAssign (curr->value , result );
1120+ return visit (curr->value , NO_RESULT );
11451121 }
11461122
11471123 Ref visitConst (Const* curr) {
@@ -1587,7 +1563,7 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m, Function* func, IString resul
15871563 }
15881564 };
15891565
1590- return ExpressionProcessor (this , m, func).visit (func->body , result );
1566+ return ExpressionProcessor (this , m, func).visit (func->body , NO_RESULT );
15911567}
15921568
15931569void Wasm2JSBuilder::addMemoryGrowthFuncs (Ref ast, Module* wasm) {
0 commit comments