Commit e901ccdd authored by bradnelson's avatar bradnelson Committed by Commit bot

[wasm] [asm.js] Fix asm.js issues around floating point globals.

Allow fround to take values without dots for globals (the spec allows this
subtly).

Drop over-restrictive assert preventing floating point globals from working.

BUG=v8:4203
R=jpp@chromium.org,aseemgarg@chromium.org

Review-Url: https://codereview.chromium.org/2397823003
Cr-Commit-Position: refs/heads/master@{#40013}
parent 8ba6686b
......@@ -762,7 +762,7 @@ AsmType* AsmTyper::ValidateGlobalDeclaration(Assignment* assign) {
bool global_variable = false;
if (value->IsLiteral() || value->IsCall()) {
AsmType* type = nullptr;
RECURSE(type = VariableTypeAnnotations(value));
RECURSE(type = VariableTypeAnnotations(value, true));
target_info = new (zone_) VariableInfo(type);
target_info->set_mutability(VariableInfo::kMutableGlobal);
global_variable = true;
......@@ -2681,7 +2681,8 @@ AsmType* AsmTyper::ReturnTypeAnnotations(ReturnStatement* statement) {
// 5.4 VariableTypeAnnotations
// Also used for 5.5 GlobalVariableTypeAnnotations
AsmType* AsmTyper::VariableTypeAnnotations(Expression* initializer) {
AsmType* AsmTyper::VariableTypeAnnotations(Expression* initializer,
bool global) {
if (auto* literal = initializer->AsLiteral()) {
if (literal->raw_value()->ContainsDot()) {
SetTypeOf(initializer, AsmType::Double());
......@@ -2722,10 +2723,13 @@ AsmType* AsmTyper::VariableTypeAnnotations(Expression* initializer) {
"to fround.");
}
if (!src_expr->raw_value()->ContainsDot()) {
FAIL(initializer,
"Invalid float type annotation - expected literal argument to be a "
"floating point literal.");
// Float constants must contain dots in local, but not in globals.
if (!global) {
if (!src_expr->raw_value()->ContainsDot()) {
FAIL(initializer,
"Invalid float type annotation - expected literal argument to be a "
"floating point literal.");
}
}
return AsmType::Float();
......
......@@ -306,8 +306,9 @@ class AsmTyper final {
// 5.2 ReturnTypeAnnotations
AsmType* ReturnTypeAnnotations(ReturnStatement* statement);
// 5.4 VariableTypeAnnotations
AsmType* VariableTypeAnnotations(Expression* initializer);
// 5.5 GlobalVariableTypeAnnotations
AsmType* VariableTypeAnnotations(Expression* initializer,
bool global = false);
AsmType* ImportExpression(Property* import);
AsmType* NewHeapView(CallNew* new_heap_view);
......
......@@ -1339,13 +1339,16 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> {
bool returns_value = true;
switch (call_type) {
case Call::OTHER_CALL: {
DCHECK_EQ(kFuncScope, scope_);
VariableProxy* proxy = expr->expression()->AsVariableProxy();
if (proxy != nullptr) {
DCHECK(kFuncScope == scope_ ||
typer_->VariableAsStandardMember(proxy->var()) ==
AsmTyper::kMathFround);
if (VisitStdlibFunction(expr, proxy)) {
return true;
}
}
DCHECK(kFuncScope == scope_);
VariableProxy* vp = expr->expression()->AsVariableProxy();
DCHECK_NOT_NULL(vp);
if (typer_->TypeOf(vp)->AsFFIType() != nullptr) {
......
......@@ -515,7 +515,6 @@ TEST(ErrorsInGlobalVariableDefinition) {
{"var v = __fround__(1.0);", "expected call fround(literal)"},
{"var v = fround(1.0, 1.0);", "expected call fround(literal)"},
{"var v = fround(not_fround);", "literal argument for call to fround"},
{"var v = fround(1);", "literal argument to be a floating point"},
{"var v = stdlib.nan", "Invalid import"},
{"var v = stdlib.Math.nan", "Invalid import"},
{"var v = stdlib.Mathh.E", "Invalid import"},
......
......@@ -1628,3 +1628,44 @@ function TestNotOne() {
}
assertWasm(55, TestNotOne);
function TestDotfulFloat(stdlib) {
"use asm";
var fround = stdlib.Math.fround;
var foo = fround(55.0);
function caller() {
return +foo;
}
return {caller: caller};
}
assertWasm(55, TestDotfulFloat);
function TestDotlessFloat(stdlib) {
"use asm";
var fround = stdlib.Math.fround;
var foo = fround(55);
function caller() {
return +foo;
}
return {caller: caller};
}
assertWasm(55, TestDotlessFloat);
function TestFloatGlobals(stdlib) {
"use asm";
var fround = stdlib.Math.fround;
var foo = fround(1.25);
function caller() {
foo = fround(foo + fround(1.0));
foo = fround(foo + fround(1.0));
return +foo;
}
return {caller: caller};
}
assertWasm(3.25, TestFloatGlobals);
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