@@ -1338,6 +1338,49 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
1338
1338
block->list .push_back (continuer);
1339
1339
ret->body = block;
1340
1340
return ret;
1341
+ } else if (what == FOR) {
1342
+ Ref finit = ast[1 ],
1343
+ fcond = ast[2 ],
1344
+ finc = ast[3 ],
1345
+ fbody = ast[4 ];
1346
+ auto ret = allocator.alloc <Loop>();
1347
+ IString out, in;
1348
+ if (!parentLabel.isNull ()) {
1349
+ out = getBreakLabelName (parentLabel);
1350
+ in = getContinueLabelName (parentLabel);
1351
+ parentLabel = IString ();
1352
+ } else {
1353
+ out = getNextId (" for-out" );
1354
+ in = getNextId (" for-in" );
1355
+ }
1356
+ ret->out = out;
1357
+ ret->in = in;
1358
+ breakStack.push_back (out);
1359
+ continueStack.push_back (in);
1360
+ Break *breakOut = allocator.alloc <Break>();
1361
+ breakOut->name = out;
1362
+ If *condition = allocator.alloc <If>();
1363
+ condition->condition = process (fcond);
1364
+ condition->ifTrue = allocator.alloc <Nop>();
1365
+ condition->ifFalse = breakOut;
1366
+ auto body = allocator.alloc <Block>();
1367
+ body->list .push_back (condition);
1368
+ body->list .push_back (process (fbody));
1369
+ body->list .push_back (process (finc));
1370
+ ret->body = body;
1371
+ // loops do not automatically loop, add a branch back
1372
+ Block* block = blockify (ret->body );
1373
+ auto continuer = allocator.alloc <Break>();
1374
+ continuer->name = ret->in ;
1375
+ block->list .push_back (continuer);
1376
+ ret->body = block;
1377
+ continueStack.pop_back ();
1378
+ breakStack.pop_back ();
1379
+ Block *outer = allocator.alloc <Block>();
1380
+ // add an outer block for the init as well
1381
+ outer->list .push_back (process (finit));
1382
+ outer->list .push_back (ret);
1383
+ return outer;
1341
1384
} else if (what == LABEL) {
1342
1385
assert (parentLabel.isNull ());
1343
1386
parentLabel = ast[1 ]->getIString ();
0 commit comments