Reviewers: Jarin,

Description:
[strong] Simplify (and sortof optimize) string addition for strong mode.

In strong mode, whenever either operand to an addition is a string, both
must be strings, so we can just use a simple string map check instead of
the STRING_ADD_LEFT / STRING_ADD_RIGHT machinery, which tries to do sloppy
and strict mode conversions before giving up.

[email protected]

Please review this at https://codereview.chromium.org/1287203002/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+24, -40 lines):
  M src/builtins.h
  M src/hydrogen.cc
  M src/runtime.js


Index: src/builtins.h
diff --git a/src/builtins.h b/src/builtins.h
index 212ad89e4fea882975a5ddfad2cc828d86c2d6eb..8ced3e3b93b71b6c3f4acd186a1f96252ae88ef5 100644
--- a/src/builtins.h
+++ b/src/builtins.h
@@ -186,9 +186,7 @@ enum BuiltinExtraArguments {
   V(TO_STRING, 0)                          \
   V(TO_NAME, 0)                            \
   V(STRING_ADD_LEFT, 1)                    \
-  V(STRING_ADD_LEFT_STRONG, 1)             \
   V(STRING_ADD_RIGHT, 1)                   \
-  V(STRING_ADD_RIGHT_STRONG, 1)            \
   V(APPLY_PREPARE, 1)                      \
   V(REFLECT_APPLY_PREPARE, 1)              \
   V(REFLECT_CONSTRUCT_PREPARE, 2)          \
Index: src/hydrogen.cc
diff --git a/src/hydrogen.cc b/src/hydrogen.cc
index 559eca6e5847eb8d3503a1f2426a74e60d5c8026..9354266d768c656bfe5db0e1acb89d9cb5aedd04 100644
--- a/src/hydrogen.cc
+++ b/src/hydrogen.cc
@@ -10914,29 +10914,35 @@ HValue* HGraphBuilder::BuildBinaryOperation(
     }

     // Convert left argument as necessary.
-    if (left_type->Is(Type::Number()) && !is_strong(strength)) {
+    if (!left_type->Is(Type::String())) {
       DCHECK(right_type->Is(Type::String()));
-      left = BuildNumberToString(left, left_type);
-    } else if (!left_type->Is(Type::String())) {
-      DCHECK(right_type->Is(Type::String()));
-      HValue* function = AddLoadJSBuiltin(
-          is_strong(strength) ? Builtins::STRING_ADD_RIGHT_STRONG
-                              : Builtins::STRING_ADD_RIGHT);
-      Add<HPushArguments>(left, right);
-      return AddUncasted<HInvokeFunction>(function, 2);
+      if (is_strong(strength)) {
+ // In strong mode, if the left hand side of an additition is a string,
+        // the right hand side must be a string too.
+        left = BuildCheckString(left);
+      } else if (left_type->Is(Type::Number())) {
+        left = BuildNumberToString(left, left_type);
+      } else {
+        HValue* function = AddLoadJSBuiltin(Builtins::STRING_ADD_RIGHT);
+        Add<HPushArguments>(left, right);
+        return AddUncasted<HInvokeFunction>(function, 2);
+      }
     }

     // Convert right argument as necessary.
-    if (right_type->Is(Type::Number()) && !is_strong(strength)) {
+    if (!right_type->Is(Type::String())) {
       DCHECK(left_type->Is(Type::String()));
-      right = BuildNumberToString(right, right_type);
-    } else if (!right_type->Is(Type::String())) {
-      DCHECK(left_type->Is(Type::String()));
-      HValue* function = AddLoadJSBuiltin(is_strong(strength)
- ? Builtins::STRING_ADD_LEFT_STRONG
-                                              : Builtins::STRING_ADD_LEFT);
-      Add<HPushArguments>(left, right);
-      return AddUncasted<HInvokeFunction>(function, 2);
+      if (is_strong(strength)) {
+ // In strong mode, if the right hand side of an additition is a string,
+        // the left hand side must be a string too.
+        right = BuildCheckString(right);
+      } else if (right_type->Is(Type::Number())) {
+        right = BuildNumberToString(right, right_type);
+      } else {
+        HValue* function = AddLoadJSBuiltin(Builtins::STRING_ADD_LEFT);
+        Add<HPushArguments>(left, right);
+        return AddUncasted<HInvokeFunction>(function, 2);
+      }
     }

     // Fast paths for empty constant strings.
Index: src/runtime.js
diff --git a/src/runtime.js b/src/runtime.js
index a120d9648c494b1d586393a10d053912e09bd2d3..e82d25e3295c745af5ee701e1166f9fdc5935aea 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -23,9 +23,7 @@ var COMPARE_STRONG;
 var ADD;
 var ADD_STRONG;
 var STRING_ADD_LEFT;
-var STRING_ADD_LEFT_STRONG;
 var STRING_ADD_RIGHT;
-var STRING_ADD_RIGHT_STRONG;
 var SUB;
 var SUB_STRONG;
 var MUL;
@@ -262,15 +260,6 @@ STRING_ADD_LEFT = function STRING_ADD_LEFT(y) {
 }


-// Left operand (this) is already a string.
-STRING_ADD_LEFT_STRONG = function STRING_ADD_LEFT_STRONG(y) {
-  if (IS_STRING(y)) {
-    return %_StringAdd(this, y);
-  }
-  throw %MakeTypeError(kStrongImplicitConversion);
-}
-
-
 // Right operand (y) is already a string.
 STRING_ADD_RIGHT = function STRING_ADD_RIGHT(y) {
   var x = this;
@@ -287,15 +276,6 @@ STRING_ADD_RIGHT = function STRING_ADD_RIGHT(y) {
 }


-// Right operand (y) is already a string.
-STRING_ADD_RIGHT_STRONG = function STRING_ADD_RIGHT_STRONG(y) {
-  if (IS_STRING(this)) {
-    return %_StringAdd(this, y);
-  }
-  throw %MakeTypeError(kStrongImplicitConversion);
-}
-
-
 // ECMA-262, section 11.6.2, page 50.
 SUB = function SUB(y) {
   var x = IS_NUMBER(this) ? this : %$nonNumberToNumber(this);


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to