/ bytecode / InByIdVariant.cpp
InByIdVariant.cpp
  1  /*
  2   * Copyright (C) 2018 Yusuke Suzuki <utatane.tea@gmail.com>.
  3   * Copyright (C) 2018 Apple Inc. All rights reserved.
  4   *
  5   * Redistribution and use in source and binary forms, with or without
  6   * modification, are permitted provided that the following conditions
  7   * are met:
  8   * 1. Redistributions of source code must retain the above copyright
  9   *    notice, this list of conditions and the following disclaimer.
 10   * 2. Redistributions in binary form must reproduce the above copyright
 11   *    notice, this list of conditions and the following disclaimer in the
 12   *    documentation and/or other materials provided with the distribution.
 13   *
 14   * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 15   * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 16   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 17   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 18   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 19   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 20   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 21   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 22   * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 23   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 24   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 25   */
 26  
 27  #include "config.h"
 28  #include "InByIdVariant.h"
 29  
 30  namespace JSC {
 31  
 32  InByIdVariant::InByIdVariant(const StructureSet& structureSet, PropertyOffset offset, const ObjectPropertyConditionSet& conditionSet)
 33      : m_structureSet(structureSet)
 34      , m_conditionSet(conditionSet)
 35      , m_offset(offset)
 36  {
 37      if (!structureSet.size()) {
 38          ASSERT(offset == invalidOffset);
 39          ASSERT(conditionSet.isEmpty());
 40      }
 41  }
 42  
 43  bool InByIdVariant::attemptToMerge(const InByIdVariant& other)
 44  {
 45      if (m_offset != other.m_offset)
 46          return false;
 47  
 48      if (m_conditionSet.isEmpty() != other.m_conditionSet.isEmpty())
 49          return false;
 50  
 51      ObjectPropertyConditionSet mergedConditionSet;
 52      if (!m_conditionSet.isEmpty()) {
 53          mergedConditionSet = m_conditionSet.mergedWith(other.m_conditionSet);
 54          if (!mergedConditionSet.isValid())
 55              return false;
 56          // If this is a hit variant, one slot base should exist. If this is not a hit variant, the slot base is not necessary.
 57          if (isHit() && !mergedConditionSet.hasOneSlotBaseCondition())
 58              return false;
 59      }
 60      m_conditionSet = mergedConditionSet;
 61  
 62      m_structureSet.merge(other.m_structureSet);
 63  
 64      return true;
 65  }
 66  
 67  void InByIdVariant::markIfCheap(SlotVisitor& visitor)
 68  {
 69      m_structureSet.markIfCheap(visitor);
 70  }
 71  
 72  bool InByIdVariant::finalize(VM& vm)
 73  {
 74      if (!m_structureSet.isStillAlive(vm))
 75          return false;
 76      if (!m_conditionSet.areStillLive(vm))
 77          return false;
 78      return true;
 79  }
 80  
 81  void InByIdVariant::dump(PrintStream& out) const
 82  {
 83      dumpInContext(out, nullptr);
 84  }
 85  
 86  void InByIdVariant::dumpInContext(PrintStream& out, DumpContext* context) const
 87  {
 88      if (!isSet()) {
 89          out.print("<empty>");
 90          return;
 91      }
 92  
 93      out.print(
 94          "<", inContext(structureSet(), context), ", ", inContext(m_conditionSet, context));
 95      out.print(", offset = ", offset());
 96      out.print(">");
 97  }
 98  
 99  } // namespace JSC
100