Commit 1c8130bd authored by aseemgarg's avatar aseemgarg Committed by Commit bot

Add for loop to asm-to-wasm

TEST=asm-wasm.js
R=titzer@chromium.org,bradnelson@google.com
BUG=

Review URL: https://codereview.chromium.org/1523843003

Cr-Commit-Position: refs/heads/master@{#32895}
parent 7ae140fe
......@@ -226,16 +226,39 @@ class AsmWasmBuilderImpl : public AstVisitor {
}
void VisitForStatement(ForStatement* stmt) {
DCHECK(in_function_);
if (stmt->init() != NULL) {
block_size_++;
RECURSE(Visit(stmt->init()));
}
current_function_builder_->Emit(kExprLoop);
uint32_t index = current_function_builder_->EmitEditableImmediate(0);
int prev_block_size = block_size_;
block_size_ = 0;
breakable_blocks_.push_back(
std::make_pair(stmt->AsBreakableStatement(), true));
if (stmt->cond() != NULL) {
block_size_++;
current_function_builder_->Emit(kExprIf);
current_function_builder_->Emit(kExprBoolNot);
RECURSE(Visit(stmt->cond()));
current_function_builder_->EmitWithU8(kExprBr, 1);
current_function_builder_->Emit(kExprNop);
}
if (stmt->body() != NULL) {
block_size_++;
RECURSE(Visit(stmt->body()));
}
if (stmt->next() != NULL) {
block_size_++;
RECURSE(Visit(stmt->next()));
}
RECURSE(Visit(stmt->body()));
block_size_++;
current_function_builder_->EmitWithU8(kExprBr, 0);
current_function_builder_->Emit(kExprNop);
current_function_builder_->EditImmediate(index, block_size_);
block_size_ = prev_block_size;
breakable_blocks_.pop_back();
}
void VisitForInStatement(ForInStatement* stmt) {
......
......@@ -551,3 +551,88 @@ function TestGlobalsWithInit() {
var module = WASM.instantiateModuleFromAsm(TestGlobalsWithInit.toString());
module.__init__();
assertEquals(77.5, module.add());
function TestForLoop() {
"use asm"
function caller() {
var ret = 0;
var i = 0;
for (i = 2; i <= 10; i = (i+1)|0) {
ret = (ret + i) | 0;
}
return ret|0;
}
return {caller:caller};
}
assertEquals(54, WASM.asmCompileRun(TestForLoop.toString()));
function TestForLoopWithoutInit() {
"use asm"
function caller() {
var ret = 0;
var i = 0;
for (; i < 10; i = (i+1)|0) {
ret = (ret + 10) | 0;
}
return ret|0;
}
return {caller:caller};
}
assertEquals(100, WASM.asmCompileRun(TestForLoopWithoutInit.toString()));
function TestForLoopWithoutCondition() {
"use asm"
function caller() {
var ret = 0;
var i = 0;
for (i=1;; i = (i+1)|0) {
ret = (ret + i) | 0;
if (i == 11) {
break;
}
}
return ret|0;
}
return {caller:caller};
}
assertEquals(66, WASM.asmCompileRun(TestForLoopWithoutCondition.toString()));
function TestForLoopWithoutNext() {
"use asm"
function caller() {
var i = 0;
for (i=1; i < 41;) {
i = (i + 1) | 0;
}
return i|0;
}
return {caller:caller};
}
assertEquals(41, WASM.asmCompileRun(TestForLoopWithoutNext.toString()));
function TestForLoopWithoutBody() {
"use asm"
function caller() {
var i = 0;
for (i=1; i < 45 ; i = (i+1)|0) {
}
return i|0;
}
return {caller:caller};
}
assertEquals(45, WASM.asmCompileRun(TestForLoopWithoutBody.toString()));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment