From 8bd1c4cd9d40e23ccc91726d9c3aa00849dcf509 Mon Sep 17 00:00:00 2001
From: Eduardo Aguilar <eduardo.aguilar@mind.be>
Date: Thu, 13 Aug 2020 14:24:34 +0200
Subject: [PATCH] ppc32: check float conversion exception

Signed-off-by: Eduardo Aguilar <eduardo.aguilar@mind.be>
---
 deps/v8/src/builtins/ppc/builtins-ppc.cc            | 13 ++++++++++---
 deps/v8/src/codegen/ppc/macro-assembler-ppc.cc      | 10 ++++++++--
 deps/v8/src/codegen/ppc/macro-assembler-ppc.h       |  7 -------
 .../src/compiler/backend/ppc/code-generator-ppc.cc  | 11 ++++++++++-
 4 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/deps/v8/src/builtins/ppc/builtins-ppc.cc b/deps/v8/src/builtins/ppc/builtins-ppc.cc
index 435e11d6..f29f7ccc 100644
--- a/deps/v8/src/builtins/ppc/builtins-ppc.cc
+++ b/deps/v8/src/builtins/ppc/builtins-ppc.cc
@@ -2818,7 +2818,11 @@ void Builtins::Generate_DoubleToI(MacroAssembler* masm) {
   Register scratch_high =
       GetRegisterThatIsNotOneOf(result_reg, scratch, scratch_low);
   DoubleRegister double_scratch = kScratchDoubleReg;
-
+#if !V8_TARGET_ARCH_PPC64
+  CRegister cr = cr7;
+  int crbit = v8::internal::Assembler::encode_crbit(
+      cr, static_cast<CRBit>(VXCVI % CRWIDTH));
+#endif
   __ Push(result_reg, scratch);
   // Account for saved regs.
   int argument_offset = 2 * kPointerSize;
@@ -2828,13 +2832,16 @@ void Builtins::Generate_DoubleToI(MacroAssembler* masm) {
 
   // Do fast-path convert from double to int.
 #if !V8_TARGET_ARCH_PPC64
+  __ mtfsb0(VXCVI);
   __ ConvertDoubleToInt32NoPPC64(double_scratch, result_reg, scratch);
-  __ TestIfInt32(scratch, result_reg, r0);
+  __ mcrfs(cr, VXCVI);
+  __ bc(v8::internal::kInstrSize * 2, BT, crbit);
+  __ b(&fastpath_done);
 #else
   __ ConvertDoubleToInt64(double_scratch, result_reg, d0);
   __ TestIfInt32(result_reg, r0);
-#endif
   __ beq(&fastpath_done);
+#endif
 
   __ Push(scratch_high, scratch_low);
   // Account for saved regs.
diff --git a/deps/v8/src/codegen/ppc/macro-assembler-ppc.cc b/deps/v8/src/codegen/ppc/macro-assembler-ppc.cc
index 30fe1f45..2a213c80 100644
--- a/deps/v8/src/codegen/ppc/macro-assembler-ppc.cc
+++ b/deps/v8/src/codegen/ppc/macro-assembler-ppc.cc
@@ -1715,14 +1715,20 @@ void TurboAssembler::TryInlineTruncateDoubleToI(Register result,
                                                 Label* done) {
 #if !V8_TARGET_ARCH_PPC64
   Register scratch = r11;
+  CRegister cr = cr7;
+  int crbit = v8::internal::Assembler::encode_crbit(
+    cr, static_cast<CRBit>(VXCVI % CRWIDTH));
+  mtfsb0(VXCVI);
   ConvertDoubleToInt32NoPPC64(double_input, result, scratch);
-  TestIfInt32(scratch, result, r0);
+  mcrfs(cr, VXCVI);
+  bc(v8::internal::kInstrSize * 2, BT, crbit);
+  b(done);
 #else
   DoubleRegister double_scratch = kScratchDoubleReg;
   ConvertDoubleToInt64(double_input, result, double_scratch);
   TestIfInt32(result, r0);
-#endif
   beq(done);
+#endif
 }
 
 void TurboAssembler::CallRuntimeWithCEntry(Runtime::FunctionId fid,
diff --git a/deps/v8/src/codegen/ppc/macro-assembler-ppc.h b/deps/v8/src/codegen/ppc/macro-assembler-ppc.h
index e4edd19e..964a1f79 100644
--- a/deps/v8/src/codegen/ppc/macro-assembler-ppc.h
+++ b/deps/v8/src/codegen/ppc/macro-assembler-ppc.h
@@ -592,13 +592,6 @@ class V8_EXPORT_PRIVATE TurboAssembler : public TurboAssemblerBase {
     extsw(scratch, value);
     cmp(scratch, value, cr);
   }
-#else
-  inline void TestIfInt32(Register hi_word, Register lo_word, Register scratch,
-                          CRegister cr = cr7) {
-    // High bits must be identical to fit into an 32-bit integer
-    srawi(scratch, lo_word, 31);
-    cmp(scratch, hi_word, cr);
-  }
 #endif
 
   // Overflow handling functions.
diff --git a/deps/v8/src/compiler/backend/ppc/code-generator-ppc.cc b/deps/v8/src/compiler/backend/ppc/code-generator-ppc.cc
index b5d4fbca..203feca8 100644
--- a/deps/v8/src/compiler/backend/ppc/code-generator-ppc.cc
+++ b/deps/v8/src/compiler/backend/ppc/code-generator-ppc.cc
@@ -1866,9 +1866,18 @@ CodeGenerator::CodeGenResult CodeGenerator::AssembleArchInstruction(
     case kPPC_DoubleToUint32:
 #if !V8_TARGET_ARCH_PPC64
     {
+      Label exit_inv;
       Register scratch = kScratchReg;
+      CRegister cr = cr7;
+      int crbit = v8::internal::Assembler::encode_crbit(
+            cr, static_cast<CRBit>(VXCVI % CRWIDTH));
+      __ mtfsb0(VXCVI);
       __ ConvertDoubleToInt32NoPPC64(i.InputDoubleRegister(0), i.OutputRegister(), scratch);
-      __ TestIfInt32(scratch,  i.OutputRegister(), r0);
+      __ mcrfs(cr, VXCVI);
+      __ bc(__ branch_offset(&exit_inv), BT, crbit);
+      break;
+      __ bind(&exit_inv);
+      __ li(i.OutputRegister(), Operand::Zero());
       break;
     }
 #endif
-- 
2.20.1

