14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci#include "ecmascript/runtime.h"
164514f5e3Sopenharmony_ci
174514f5e3Sopenharmony_cinamespace panda::ecmascript {
184514f5e3Sopenharmony_civoid Barriers::UpdateWithoutEden(const JSThread *thread, uintptr_t slotAddr, Region *objectRegion, TaggedObject *value,
194514f5e3Sopenharmony_ci                                 Region *valueRegion, WriteBarrierType writeType)
204514f5e3Sopenharmony_ci{
214514f5e3Sopenharmony_ci    ASSERT(!valueRegion->InSharedHeap());
224514f5e3Sopenharmony_ci    auto heap = thread->GetEcmaVM()->GetHeap();
234514f5e3Sopenharmony_ci    if (heap->IsConcurrentFullMark()) {
244514f5e3Sopenharmony_ci        if (valueRegion->InCollectSet() && !objectRegion->InGeneralNewSpaceOrCSet()) {
254514f5e3Sopenharmony_ci            objectRegion->AtomicInsertCrossRegionRSet(slotAddr);
264514f5e3Sopenharmony_ci        }
274514f5e3Sopenharmony_ci    } else {
284514f5e3Sopenharmony_ci        if (!valueRegion->InYoungSpace()) {
294514f5e3Sopenharmony_ci            return;
304514f5e3Sopenharmony_ci        }
314514f5e3Sopenharmony_ci    }
324514f5e3Sopenharmony_ci
334514f5e3Sopenharmony_ci    // Weak ref record and concurrent mark record maybe conflict.
344514f5e3Sopenharmony_ci    // This conflict is solved by keeping alive weak reference. A small amount of floating garbage may be added.
354514f5e3Sopenharmony_ci    TaggedObject *heapValue = JSTaggedValue(value).GetHeapObject();
364514f5e3Sopenharmony_ci    if (valueRegion->IsFreshRegion()) {
374514f5e3Sopenharmony_ci        valueRegion->NonAtomicMark(heapValue);
384514f5e3Sopenharmony_ci    } else if (writeType != WriteBarrierType::DESERIALIZE && valueRegion->AtomicMark(heapValue)) {
394514f5e3Sopenharmony_ci        heap->GetWorkManager()->Push(MAIN_THREAD_INDEX, heapValue);
404514f5e3Sopenharmony_ci    }
414514f5e3Sopenharmony_ci}
424514f5e3Sopenharmony_ci
434514f5e3Sopenharmony_civoid Barriers::Update(const JSThread *thread, uintptr_t slotAddr, Region *objectRegion, TaggedObject *value,
444514f5e3Sopenharmony_ci                      Region *valueRegion, WriteBarrierType writeType)
454514f5e3Sopenharmony_ci{
464514f5e3Sopenharmony_ci    if (valueRegion->InSharedHeap()) {
474514f5e3Sopenharmony_ci        return;
484514f5e3Sopenharmony_ci    }
494514f5e3Sopenharmony_ci    auto heap = thread->GetEcmaVM()->GetHeap();
504514f5e3Sopenharmony_ci    if (heap->IsConcurrentFullMark()) {
514514f5e3Sopenharmony_ci        if (valueRegion->InCollectSet() && !objectRegion->InGeneralNewSpaceOrCSet()) {
524514f5e3Sopenharmony_ci            objectRegion->AtomicInsertCrossRegionRSet(slotAddr);
534514f5e3Sopenharmony_ci        }
544514f5e3Sopenharmony_ci    } else if (heap->IsYoungMark()) {
554514f5e3Sopenharmony_ci        if (!valueRegion->InGeneralNewSpace()) {
564514f5e3Sopenharmony_ci            return;
574514f5e3Sopenharmony_ci        }
584514f5e3Sopenharmony_ci    } else {
594514f5e3Sopenharmony_ci        if (!valueRegion->InEdenSpace()) {
604514f5e3Sopenharmony_ci            return;
614514f5e3Sopenharmony_ci        }
624514f5e3Sopenharmony_ci    }
634514f5e3Sopenharmony_ci
644514f5e3Sopenharmony_ci    // Weak ref record and concurrent mark record maybe conflict.
654514f5e3Sopenharmony_ci    // This conflict is solved by keeping alive weak reference. A small amount of floating garbage may be added.
664514f5e3Sopenharmony_ci    TaggedObject *heapValue = JSTaggedValue(value).GetHeapObject();
674514f5e3Sopenharmony_ci    if (valueRegion->IsFreshRegion()) {
684514f5e3Sopenharmony_ci        valueRegion->NonAtomicMark(heapValue);
694514f5e3Sopenharmony_ci    } else if (writeType != WriteBarrierType::DESERIALIZE && valueRegion->AtomicMark(heapValue)) {
704514f5e3Sopenharmony_ci        heap->GetWorkManager()->Push(MAIN_THREAD_INDEX, heapValue);
714514f5e3Sopenharmony_ci    }
724514f5e3Sopenharmony_ci}
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_civoid Barriers::UpdateShared(const JSThread *thread, TaggedObject *value, Region *valueRegion)
754514f5e3Sopenharmony_ci{
764514f5e3Sopenharmony_ci    ASSERT(DaemonThread::GetInstance()->IsConcurrentMarkingOrFinished());
774514f5e3Sopenharmony_ci    ASSERT(valueRegion->InSharedSweepableSpace());
784514f5e3Sopenharmony_ci
794514f5e3Sopenharmony_ci    // Weak ref record and concurrent mark record maybe conflict.
804514f5e3Sopenharmony_ci    // This conflict is solved by keeping alive weak reference. A small amount of floating garbage may be added.
814514f5e3Sopenharmony_ci    TaggedObject *heapValue = JSTaggedValue(value).GetHeapObject();
824514f5e3Sopenharmony_ci    if (valueRegion->AtomicMark(heapValue)) {
834514f5e3Sopenharmony_ci        Heap *heap = const_cast<Heap*>(thread->GetEcmaVM()->GetHeap());
844514f5e3Sopenharmony_ci        WorkNode *&localBuffer = heap->GetMarkingObjectLocalBuffer();
854514f5e3Sopenharmony_ci        SharedHeap::GetInstance()->GetWorkManager()->PushToLocalMarkingBuffer(localBuffer, heapValue);
864514f5e3Sopenharmony_ci    }
874514f5e3Sopenharmony_ci}
884514f5e3Sopenharmony_ci
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_citemplate <Region::RegionSpaceKind kind>
914514f5e3Sopenharmony_ciARK_NOINLINE bool BatchBitSet([[maybe_unused]] const JSThread* thread, Region* objectRegion, JSTaggedValue* dst,
924514f5e3Sopenharmony_ci                              size_t count)
934514f5e3Sopenharmony_ci{
944514f5e3Sopenharmony_ci    bool allValueNotHeap = true;
954514f5e3Sopenharmony_ci    Region::Updater updater = objectRegion->GetBatchRSetUpdater<kind>(ToUintPtr(dst));
964514f5e3Sopenharmony_ci    for (size_t i = 0; i < count; i++, updater.Next()) {
974514f5e3Sopenharmony_ci        JSTaggedValue taggedValue = dst[i];
984514f5e3Sopenharmony_ci        if (!taggedValue.IsHeapObject()) {
994514f5e3Sopenharmony_ci            continue;
1004514f5e3Sopenharmony_ci        }
1014514f5e3Sopenharmony_ci        allValueNotHeap = false;
1024514f5e3Sopenharmony_ci        const Region* valueRegion = Region::ObjectAddressToRange(taggedValue.GetTaggedObject());
1034514f5e3Sopenharmony_ci#if ECMASCRIPT_ENABLE_BARRIER_CHECK
1044514f5e3Sopenharmony_ci        ASSERT(taggedValue.GetRawData() != JSTaggedValue::VALUE_UNDEFINED);
1054514f5e3Sopenharmony_ci        if (!thread->GetEcmaVM()->GetHeap()->IsAlive(taggedValue.GetHeapObject())) {
1064514f5e3Sopenharmony_ci            LOG_FULL(FATAL) << "WriteBarrier checked value:" << taggedValue.GetRawData() << " is invalid!";
1074514f5e3Sopenharmony_ci        }
1084514f5e3Sopenharmony_ci#endif
1094514f5e3Sopenharmony_ci        if (valueRegion->InSharedSweepableSpace()) {
1104514f5e3Sopenharmony_ci#ifndef NDEBUG
1114514f5e3Sopenharmony_ci            if (UNLIKELY(taggedValue.IsWeakForHeapObject())) {
1124514f5e3Sopenharmony_ci                CHECK_NO_LOCAL_TO_SHARE_WEAK_REF_HANDLE;
1134514f5e3Sopenharmony_ci            }
1144514f5e3Sopenharmony_ci#endif
1154514f5e3Sopenharmony_ci            updater.UpdateLocalToShare();
1164514f5e3Sopenharmony_ci            continue;
1174514f5e3Sopenharmony_ci        }
1184514f5e3Sopenharmony_ci        if constexpr (kind == Region::InYoung) {
1194514f5e3Sopenharmony_ci            if (valueRegion->InEdenSpace()) {
1204514f5e3Sopenharmony_ci                updater.UpdateNewToEden();
1214514f5e3Sopenharmony_ci                continue;
1224514f5e3Sopenharmony_ci            }
1234514f5e3Sopenharmony_ci        } else if constexpr (kind == Region::InGeneralOld) {
1244514f5e3Sopenharmony_ci            if (valueRegion->InGeneralNewSpace()) {
1254514f5e3Sopenharmony_ci                updater.UpdateOldToNew();
1264514f5e3Sopenharmony_ci                continue;
1274514f5e3Sopenharmony_ci            }
1284514f5e3Sopenharmony_ci        }
1294514f5e3Sopenharmony_ci    }
1304514f5e3Sopenharmony_ci    return allValueNotHeap;
1314514f5e3Sopenharmony_ci}
1324514f5e3Sopenharmony_ci
1334514f5e3Sopenharmony_citemplate bool BatchBitSet<Region::InYoung>(const JSThread*, Region*, JSTaggedValue*, size_t);
1344514f5e3Sopenharmony_citemplate bool BatchBitSet<Region::InGeneralOld>(const JSThread*, Region*, JSTaggedValue*, size_t);
1354514f5e3Sopenharmony_citemplate bool BatchBitSet<Region::Other>(const JSThread*, Region*, JSTaggedValue*, size_t);
1364514f5e3Sopenharmony_ci
1374514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
138