2828#include < pass.h>
2929#include < wasm-builder.h>
3030#include < ast_utils.h>
31+ #include < ast/literal-utils.h>
3132#include < parsing.h>
3233
3334namespace wasm {
@@ -121,11 +122,12 @@ struct Planner : public WalkerPass<PostWalker<Planner>> {
121122// Core inlining logic. Modifies the outside function (adding locals as
122123// needed), and returns the inlined code.
123124static Expression* doInlining (Module* module , Function* into, InliningAction& action) {
125+ Function* from = action.contents ;
124126 auto * call = (*action.callSite )->cast <Call>();
125127 Builder builder (*module );
126128 auto * block = Builder (*module ).makeBlock ();
127129 block->type = call->type ;
128- block->name = Name (std::string (" __inlined_func$" ) + action. contents ->name .str );
130+ block->name = Name (std::string (" __inlined_func$" ) + from ->name .str );
129131 *action.callSite = block;
130132 // set up a locals mapping
131133 struct Updater : public PostWalker <Updater> {
@@ -145,15 +147,19 @@ static Expression* doInlining(Module* module, Function* into, InliningAction& ac
145147 } updater;
146148 updater.returnName = block->name ;
147149 updater.builder = &builder;
148- for (Index i = 0 ; i < action. contents ->getNumLocals (); i++) {
149- updater.localMapping [i] = builder.addVar (into, action. contents ->getLocalType (i));
150+ for (Index i = 0 ; i < from ->getNumLocals (); i++) {
151+ updater.localMapping [i] = builder.addVar (into, from ->getLocalType (i));
150152 }
151153 // assign the operands into the params
152- for (Index i = 0 ; i < action. contents ->params .size (); i++) {
154+ for (Index i = 0 ; i < from ->params .size (); i++) {
153155 block->list .push_back (builder.makeSetLocal (updater.localMapping [i], call->operands [i]));
154156 }
157+ // zero out the vars (as we may be in a loop, and may depend on their zero-init value
158+ for (Index i = 0 ; i < from->vars .size (); i++) {
159+ block->list .push_back (builder.makeSetLocal (updater.localMapping [from->getVarIndexBase () + i], LiteralUtils::makeZero (from->vars [i], *module )));
160+ }
155161 // generate and update the inlined contents
156- auto * contents = ExpressionManipulator::copy (action. contents ->body , *module );
162+ auto * contents = ExpressionManipulator::copy (from ->body , *module );
157163 updater.walk (contents);
158164 block->list .push_back (contents);
159165 return block;
0 commit comments