1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef ECMASCRIPT_COMPILER_VERIFIER_H
17#define ECMASCRIPT_COMPILER_VERIFIER_H
18
19#include <algorithm>
20#include <deque>
21#include <functional>
22#include <numeric>
23#include <unordered_map>
24
25#include "ecmascript/compiler/circuit.h"
26
27namespace panda::ecmascript::kungfu {
28class Verifier {
29public:
30    static bool RunDataIntegrityCheck(const Circuit *circuit);
31
32    static bool RunStateGatesCheck(const Circuit *circuit, const std::vector<GateRef> &bbGatesList,
33                                   const std::string& methodName);
34
35    static bool RunCFGSoundnessCheck(const Circuit *circuit, const std::vector<GateRef> &bbGatesList,
36                                     const std::unordered_map<GateRef, size_t> &bbGatesAddrToIdx);
37
38    static bool RunCFGIsDAGCheck(const Circuit *circuit);
39
40    static bool RunCFGReducibilityCheck(const Circuit *circuit, const std::vector<GateRef> &bbGatesList,
41                                        const std::unordered_map<GateRef, size_t> &bbGatesAddrToIdx,
42                                        const std::function<bool(size_t, size_t)> &isAncestor);
43
44    static bool RunFixedGatesCheck(const Circuit *circuit, const std::vector<GateRef> &fixedGatesList);
45
46    static bool RunFixedGatesRelationsCheck(const Circuit *circuit, const std::vector<GateRef> &fixedGatesList,
47                                            const std::unordered_map<GateRef, size_t> &bbGatesAddrToIdx,
48                                            const std::function<bool(size_t, size_t)> &isAncestor);
49
50    static bool RunFlowCyclesFind(const Circuit *circuit, std::vector<GateRef> *schedulableGatesListPtr,
51                                  const std::vector<GateRef> &bbGatesList,
52                                  const std::vector<GateRef> &fixedGatesList);
53
54    static bool RunSchedulableGatesCheck(const Circuit *circuit, const std::vector<GateRef> &schedulableGatesList);
55
56    static bool RunPrologGatesCheck(const Circuit *circuit, const std::vector<GateRef> &schedulableGatesList);
57
58    static bool RunSchedulingBoundsCheck(const Circuit *circuit, const std::vector<GateRef> &schedulableGatesList,
59                                         const std::unordered_map<GateRef, size_t> &bbGatesAddrToIdx,
60                                         const std::function<bool(size_t, size_t)> &isAncestor,
61                                         const std::function<size_t(size_t, size_t)> &lowestCommonAncestor);
62
63    static void FindFixedGates(const Circuit *circuit, const std::vector<GateRef> &bbGatesList,
64                               std::vector<GateRef> &fixedGatesList);
65
66    static bool RunFlowCyclesFind(const Circuit* circuit);
67
68    static bool Run(const Circuit *circuit, const std::string& methodName = "", bool enableLog = false);
69};
70}  // namespace panda::ecmascript::kungfu
71
72#endif  // ECMASCRIPT_COMPILER_VERIFIER_H
73