Prechádzať zdrojové kódy

Encode int64 constants

Michael Ferris 9 rokov pred
rodič
commit
d414d31cb1
4 zmenil súbory, kde vykonal 103 pridanie a 114 odobranie
  1. 36 57
      lib/Backend/Opnd.cpp
  2. 28 24
      lib/Backend/Opnd.h
  3. 37 32
      lib/Backend/Security.cpp
  4. 2 1
      lib/Backend/Security.h

+ 36 - 57
lib/Backend/Opnd.cpp

@@ -1401,23 +1401,8 @@ IntConstOpnd::New(IntConstType value, IRType type, Func *func, bool dontEncode)
     intConstOpnd->m_dontEncode = dontEncode;
     intConstOpnd->SetValue(value);
 
-#if DBG_DUMP || defined(ENABLE_IR_VIEWER)
-    intConstOpnd->decodedValue = 0;
-    intConstOpnd->name = nullptr;
-#endif
-
-    return intConstOpnd;
-}
-
-#if DBG_DUMP || defined(ENABLE_IR_VIEWER)
-IntConstOpnd *
-IntConstOpnd::New(IntConstType value, IRType type, const char16 * name, Func *func, bool dontEncode)
-{
-    IntConstOpnd * intConstOpnd = IntConstOpnd::New(value, type, func, dontEncode);
-    intConstOpnd->name = name;
     return intConstOpnd;
 }
-#endif
 
 ///----------------------------------------------------------------------------
 ///
@@ -1612,12 +1597,6 @@ void Int64ConstOpnd::FreeInternal(Func * func)
     JitAdelete(func->m_alloc, this);
 }
 
-int64 Int64ConstOpnd::GetValue()
-{
-    Assert(m_type == TyInt64);
-    return m_value;
-}
-
 ///----------------------------------------------------------------------------
 ///
 /// RegBVOpnd::New
@@ -2881,6 +2860,40 @@ Opnd::DumpFunctionInfo(_Outptr_result_buffer_(*count) char16 ** buffer, size_t *
     }
 }
 
+
+void EncodableOpnd<int32>::DumpEncodable() const
+{
+    if (name != nullptr)
+    {
+        Output::Print(_u("<%s> (value: 0x%X)"), name, m_value);
+    }
+    else if (decodedValue != 0)
+    {
+        Output::Print(_u("%d (0x%X) [encoded: 0x%X]"), decodedValue, decodedValue, m_value);
+    }
+    else
+    {
+        Output::Print(_u("%d (0x%X)"), m_value, m_value);
+    }
+}
+
+void EncodableOpnd<int64>::DumpEncodable() const
+{
+    if (name != nullptr)
+    {
+        Output::Print(_u("<%s> (value: 0x%llX)"), name, m_value);
+    }
+    else if (decodedValue != 0)
+    {
+        Output::Print(_u("%lld (0x%llX) [encoded: 0x%llX]"), decodedValue, decodedValue, m_value);
+    }
+    else
+    {
+        Output::Print(_u("%lld (0x%llX)"), m_value, m_value);
+    }
+}
+
+
 ///----------------------------------------------------------------------------
 ///
 /// Opnd::Dump
@@ -3138,47 +3151,13 @@ Opnd::Dump(IRDumpFlags flags, Func *func)
     case OpndKindInt64Const:
     {
         Int64ConstOpnd * intConstOpnd = this->AsInt64ConstOpnd();
-        int64 intValue = intConstOpnd->GetValue();
-        Output::Print(_u("%lld (0x%llX)"), intValue, intValue);
+        intConstOpnd->DumpEncodable();
         break;
     }
     case OpndKindIntConst:
     {
         IntConstOpnd * intConstOpnd = this->AsIntConstOpnd();
-        if (intConstOpnd->name != nullptr)
-        {
-            if (!Js::Configuration::Global.flags.DumpIRAddresses)
-            {
-                Output::Print(_u("<%s>"), intConstOpnd->name);
-            }
-            else
-            {
-                Output::Print(_u("<%s> (value: 0x%X)"), intConstOpnd->name, intConstOpnd->GetValue());
-            }
-        }
-        else
-        {
-            IntConstType intValue;
-            if (intConstOpnd->decodedValue != 0)
-            {
-                intValue = intConstOpnd->decodedValue;
-                Output::Print(_u("%d (0x%X)"), intValue, intValue);
-                if (!Js::Configuration::Global.flags.DumpIRAddresses)
-                {
-                    Output::Print(_u(" [encoded]"));
-                }
-                else
-                {
-                    Output::Print(_u(" [encoded: 0x%X]"), intConstOpnd->GetValue());
-                }
-            }
-            else
-            {
-                intValue = intConstOpnd->GetValue();
-                Output::Print(_u("%d (0x%X)"), intValue, intValue);
-            }
-        }
-
+        intConstOpnd->DumpEncodable();
         break;
     }
 

+ 28 - 24
lib/Backend/Opnd.h

@@ -312,19 +312,42 @@ public:
 #endif
 };
 
+template<typename ConstType>
+class EncodableOpnd
+{
+protected:
+    ConstType m_value;
+
+public:
+    ConstType GetValue() const { return m_value; }
+    void SetEncodedValue(ConstType encodedValue)
+    {
+#if DBG_DUMP
+        decodedValue = m_value;
+#endif
+        m_value = encodedValue;
+    }
+
+#if DBG_DUMP
+    void SetName(const char16* name) { this->name = name; }
+    void DumpEncodable() const;
+private:
+    ConstType decodedValue = 0;
+    const char16* name = nullptr;
+    static const char16* fmt;
+#endif
+};
+
 ///---------------------------------------------------------------------------
 ///
 /// class IntConstOpnd
 ///
 ///---------------------------------------------------------------------------
 
-class IntConstOpnd sealed : public Opnd
+class IntConstOpnd sealed : public Opnd, public EncodableOpnd<IntConstType>
 {
 public:
     static IntConstOpnd *   New(IntConstType value, IRType type, Func *func, bool dontEncode = false);
-#if DBG_DUMP || defined(ENABLE_IR_VIEWER)
-    static IntConstOpnd *   New(IntConstType value, IRType type, const char16 * name, Func *func, bool dontEncode = false);
-#endif
     static IR::Opnd*        NewFromType(int64 value, IRType type, Func* func);
 
 public:
@@ -336,11 +359,6 @@ public:
     bool                    m_dontEncode;       // Setting this to true turns off XOR encoding for this constant.  Only set this on
                                                 // constants not controllable by the user.
 
-    IntConstType GetValue()
-    {
-        return m_value;
-    }
-
     void IncrValue(IntConstType by)
     {
         SetValue(m_value + by);
@@ -354,14 +372,6 @@ public:
     void SetValue(IntConstType value);
     int32 AsInt32();
     uint32 AsUint32();
-
-#if DBG_DUMP || defined(ENABLE_IR_VIEWER)
-    IntConstType            decodedValue;  // FIXME (t-doilij) set ENABLE_IR_VIEWER blocks where this is set
-    char16 const *         name;  // FIXME (t-doilij) set ENABLE_IR_VIEWER blocks where this is set
-#endif
-
-private:
-    IntConstType            m_value;
 };
 
 ///---------------------------------------------------------------------------
@@ -369,21 +379,15 @@ private:
 /// class Int64ConstOpnd
 ///
 ///---------------------------------------------------------------------------
-class Int64ConstOpnd sealed : public Opnd
+class Int64ConstOpnd sealed : public Opnd, public EncodableOpnd<int64>
 {
 public:
     static Int64ConstOpnd* New(int64 value, IRType type, Func *func);
 
 public:
-    //Note: type OpndKindIntConst
     Int64ConstOpnd* CopyInternal(Func *func);
     bool IsEqualInternal(Opnd *opnd);
     void FreeInternal(Func * func) ;
-public:
-    int64 GetValue();
-
-private:
-    int64            m_value;
 };
 
 ///---------------------------------------------------------------------------

+ 37 - 32
lib/Backend/Security.cpp

@@ -234,8 +234,39 @@ Security::EncodeOpnd(IR::Instr *instr, IR::Opnd *opnd)
         return;
     }
 
+    const auto unlinkSrc = [&]() {
+        if (opnd != instr->GetSrc1())
+        {
+            Assert(opnd == instr->GetSrc2());
+            isSrc2 = true;
+            instr->UnlinkSrc2();
+        }
+        else
+        {
+            instr->UnlinkSrc1();
+        }
+    };
+
     switch(opnd->GetKind())
     {
+    case IR::OpndKindInt64Const:
+    {
+#if TARGET_64
+        IR::Int64ConstOpnd *intConstOpnd = opnd->AsInt64ConstOpnd();
+        if (!this->IsLargeConstant(intConstOpnd->GetValue()))
+        {
+            return;
+        }
+        unlinkSrc();
+        int64 encodedValue = EncodeValue(instr, intConstOpnd, intConstOpnd->GetValue(), &newOpnd);
+        intConstOpnd->SetEncodedValue(encodedValue);
+#else
+        Assert(UNREACHED);
+        return;
+#endif
+    }
+    break;
+
     case IR::OpndKindIntConst:
     {
         IR::IntConstOpnd *intConstOpnd = opnd->AsIntConstOpnd();
@@ -248,23 +279,9 @@ Security::EncodeOpnd(IR::Instr *instr, IR::Opnd *opnd)
         {
             return;
         }
+        unlinkSrc();
 
-        if (opnd != instr->GetSrc1())
-        {
-            Assert(opnd == instr->GetSrc2());
-            isSrc2 = true;
-            instr->UnlinkSrc2();
-        }
-        else
-        {
-            instr->UnlinkSrc1();
-        }
-
-#if DBG_DUMP || defined(ENABLE_IR_VIEWER)
-        intConstOpnd->decodedValue = intConstOpnd->GetValue();
-#endif
-
-        intConstOpnd->SetValue(EncodeValue(instr, intConstOpnd, intConstOpnd->GetValue(), &newOpnd));
+        intConstOpnd->SetEncodedValue(EncodeValue(instr, intConstOpnd, intConstOpnd->GetValue(), &newOpnd));
     }
     break;
 
@@ -278,16 +295,7 @@ Security::EncodeOpnd(IR::Instr *instr, IR::Opnd *opnd)
             return;
         }
 
-        if (opnd != instr->GetSrc1())
-        {
-            Assert(opnd == instr->GetSrc2());
-            isSrc2 = true;
-            instr->UnlinkSrc2();
-        }
-        else
-        {
-            instr->UnlinkSrc1();
-        }
+        unlinkSrc();
 
         addrOpnd->SetEncodedValue((Js::Var)this->EncodeValue(instr, addrOpnd, (IntConstType)addrOpnd->m_address, &newOpnd), addrOpnd->GetAddrOpndKind());
     }
@@ -304,11 +312,8 @@ Security::EncodeOpnd(IR::Instr *instr, IR::Opnd *opnd)
         AssertMsg(indirOpnd->GetIndexOpnd() == nullptr, "Code currently doesn't support indir with offset and indexOpnd");
 
         IR::IntConstOpnd *indexOpnd = IR::IntConstOpnd::New(indirOpnd->GetOffset(), TyInt32, instr->m_func);
-#if DBG_DUMP || defined(ENABLE_IR_VIEWER)
-        indexOpnd->decodedValue = indexOpnd->GetValue();
-#endif
 
-        indexOpnd->SetValue(EncodeValue(instr, indexOpnd, indexOpnd->GetValue(), &newOpnd));
+        indexOpnd->SetEncodedValue(EncodeValue(instr, indexOpnd, indexOpnd->GetValue(), &newOpnd));
         indirOpnd->SetOffset(0);
         indirOpnd->SetIndexOpnd(newOpnd);
     }
@@ -361,7 +366,7 @@ Security::EncodeValue(IR::Instr *instr, IR::Opnd *opnd, IntConstType constValue,
         IR::IntConstOpnd * cookieOpnd = IR::IntConstOpnd::New(cookie, TyInt32, instr->m_func);
 
 #if DBG_DUMP
-        cookieOpnd->name = _u("cookie");
+        cookieOpnd->SetName(_u("cookie"));
 #endif
 
         instrNew = IR::Instr::New(Js::OpCode::Xor_I4, regOpnd, regOpnd, cookieOpnd, instr->m_func);
@@ -390,7 +395,7 @@ Security::EncodeValue(IR::Instr *instr, IR::Opnd *opnd, IntConstType constValue,
         IR::IntConstOpnd * cookieOpnd = IR::IntConstOpnd::New(cookie, TyUint32, instr->m_func);
 
 #if DBG_DUMP
-        cookieOpnd->name = _u("cookie");
+        cookieOpnd->SetName(_u("cookie"));
 #endif
 
         instrNew = IR::Instr::New(Js::OpCode::Xor_I4, regOpnd, regOpnd, cookieOpnd, instr->m_func);

+ 2 - 1
lib/Backend/Security.h

@@ -26,7 +26,8 @@ private:
 
     // Large constants have more than 16 significant bits.
     // Constants except these are considered large: 0x0000????, 0xffff????, 0x????0000, 0x????ffff
-    static bool     IsLargeConstant(IntConstType value) { return static_cast<int16>(value) != 0 && static_cast<int16>(value) != -1 && (value >> 16) != 0 && (value >> 16) != -1; }
+    static bool     IsLargeConstant(int32 value) { return static_cast<int16>(value) != 0 && static_cast<int16>(value) != -1 && (value >> 16) != 0 && (value >> 16) != -1; }
+    static bool     IsLargeConstant(int64 value) { return IsLargeConstant((int32)value) || IsLargeConstant((int32)(value >> 16)) || IsLargeConstant((int32)(value >> 32)); }
 
     void            InsertNOPBefore(IR::Instr *instr);
     int             GetNextNOPInsertPoint();