arkanalyzer 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.en.md +88 -0
- package/README.md +98 -0
- package/docs/HowToCreatePR.md +55 -0
- package/docs/HowToHandleIssues.md +155 -0
- package/docs/quickstart.pdf +0 -0
- package/lib/Config.d.ts +26 -0
- package/lib/Config.d.ts.map +1 -0
- package/lib/Config.js +128 -0
- package/lib/Scene.d.ts +125 -0
- package/lib/Scene.d.ts.map +1 -0
- package/lib/Scene.js +762 -0
- package/lib/callgraph/algorithm/AbstractAnalysis.d.ts +28 -0
- package/lib/callgraph/algorithm/AbstractAnalysis.d.ts.map +1 -0
- package/lib/callgraph/algorithm/AbstractAnalysis.js +137 -0
- package/lib/callgraph/algorithm/ClassHierarchyAnalysis.d.ts +11 -0
- package/lib/callgraph/algorithm/ClassHierarchyAnalysis.d.ts.map +1 -0
- package/lib/callgraph/algorithm/ClassHierarchyAnalysis.js +71 -0
- package/lib/callgraph/algorithm/RapidTypeAnalysis.d.ts +16 -0
- package/lib/callgraph/algorithm/RapidTypeAnalysis.d.ts.map +1 -0
- package/lib/callgraph/algorithm/RapidTypeAnalysis.js +147 -0
- package/lib/callgraph/common/Statistics.d.ts +58 -0
- package/lib/callgraph/common/Statistics.d.ts.map +1 -0
- package/lib/callgraph/common/Statistics.js +203 -0
- package/lib/callgraph/model/BaseGraph.d.ts +62 -0
- package/lib/callgraph/model/BaseGraph.d.ts.map +1 -0
- package/lib/callgraph/model/BaseGraph.js +158 -0
- package/lib/callgraph/model/CallGraph.d.ts +94 -0
- package/lib/callgraph/model/CallGraph.d.ts.map +1 -0
- package/lib/callgraph/model/CallGraph.js +339 -0
- package/lib/callgraph/model/builder/CallGraphBuilder.d.ts +16 -0
- package/lib/callgraph/model/builder/CallGraphBuilder.d.ts.map +1 -0
- package/lib/callgraph/model/builder/CallGraphBuilder.js +106 -0
- package/lib/callgraph/pointerAnalysis/Context.d.ts +38 -0
- package/lib/callgraph/pointerAnalysis/Context.d.ts.map +1 -0
- package/lib/callgraph/pointerAnalysis/Context.js +156 -0
- package/lib/callgraph/pointerAnalysis/DummyCallCreator.d.ts +23 -0
- package/lib/callgraph/pointerAnalysis/DummyCallCreator.d.ts.map +1 -0
- package/lib/callgraph/pointerAnalysis/DummyCallCreator.js +104 -0
- package/lib/callgraph/pointerAnalysis/Pag.d.ts +209 -0
- package/lib/callgraph/pointerAnalysis/Pag.d.ts.map +1 -0
- package/lib/callgraph/pointerAnalysis/Pag.js +774 -0
- package/lib/callgraph/pointerAnalysis/PagBuilder.d.ts +88 -0
- package/lib/callgraph/pointerAnalysis/PagBuilder.d.ts.map +1 -0
- package/lib/callgraph/pointerAnalysis/PagBuilder.js +821 -0
- package/lib/callgraph/pointerAnalysis/PointerAnalysis.d.ts +64 -0
- package/lib/callgraph/pointerAnalysis/PointerAnalysis.d.ts.map +1 -0
- package/lib/callgraph/pointerAnalysis/PointerAnalysis.js +502 -0
- package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.d.ts +9 -0
- package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.d.ts.map +1 -0
- package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.js +57 -0
- package/lib/callgraph/pointerAnalysis/PtsDS.d.ts +58 -0
- package/lib/callgraph/pointerAnalysis/PtsDS.d.ts.map +1 -0
- package/lib/callgraph/pointerAnalysis/PtsDS.js +234 -0
- package/lib/core/base/Constant.d.ts +17 -0
- package/lib/core/base/Constant.d.ts.map +1 -0
- package/lib/core/base/Constant.js +53 -0
- package/lib/core/base/Decorator.d.ts +15 -0
- package/lib/core/base/Decorator.d.ts.map +1 -0
- package/lib/core/base/Decorator.js +43 -0
- package/lib/core/base/DefUseChain.d.ts +9 -0
- package/lib/core/base/DefUseChain.d.ts.map +1 -0
- package/lib/core/base/DefUseChain.js +25 -0
- package/lib/core/base/Expr.d.ts +219 -0
- package/lib/core/base/Expr.d.ts.map +1 -0
- package/lib/core/base/Expr.js +896 -0
- package/lib/core/base/Local.d.ts +40 -0
- package/lib/core/base/Local.d.ts.map +1 -0
- package/lib/core/base/Local.js +101 -0
- package/lib/core/base/Position.d.ts +39 -0
- package/lib/core/base/Position.d.ts.map +1 -0
- package/lib/core/base/Position.js +86 -0
- package/lib/core/base/Ref.d.ts +75 -0
- package/lib/core/base/Ref.d.ts.map +1 -0
- package/lib/core/base/Ref.js +286 -0
- package/lib/core/base/Stmt.d.ts +109 -0
- package/lib/core/base/Stmt.d.ts.map +1 -0
- package/lib/core/base/Stmt.js +374 -0
- package/lib/core/base/Type.d.ts +220 -0
- package/lib/core/base/Type.d.ts.map +1 -0
- package/lib/core/base/Type.js +477 -0
- package/lib/core/base/Value.d.ts +11 -0
- package/lib/core/base/Value.d.ts.map +1 -0
- package/lib/core/base/Value.js +16 -0
- package/lib/core/common/ArkIRTransformer.d.ts +101 -0
- package/lib/core/common/ArkIRTransformer.d.ts.map +1 -0
- package/lib/core/common/ArkIRTransformer.js +1674 -0
- package/lib/core/common/BodyBuilder.d.ts +10 -0
- package/lib/core/common/BodyBuilder.d.ts.map +1 -0
- package/lib/core/common/BodyBuilder.js +34 -0
- package/lib/core/common/Builtin.d.ts +35 -0
- package/lib/core/common/Builtin.d.ts.map +1 -0
- package/lib/core/common/Builtin.js +78 -0
- package/lib/core/common/CfgBuilder.d.ts +136 -0
- package/lib/core/common/CfgBuilder.d.ts.map +1 -0
- package/lib/core/common/CfgBuilder.js +1241 -0
- package/lib/core/common/Const.d.ts +15 -0
- package/lib/core/common/Const.d.ts.map +1 -0
- package/lib/core/common/Const.js +34 -0
- package/lib/core/common/DummyMainCreater.d.ts +48 -0
- package/lib/core/common/DummyMainCreater.d.ts.map +1 -0
- package/lib/core/common/DummyMainCreater.js +433 -0
- package/lib/core/common/EtsConst.d.ts +65 -0
- package/lib/core/common/EtsConst.d.ts.map +1 -0
- package/lib/core/common/EtsConst.js +918 -0
- package/lib/core/common/ExprUseReplacer.d.ts +18 -0
- package/lib/core/common/ExprUseReplacer.d.ts.map +1 -0
- package/lib/core/common/ExprUseReplacer.js +88 -0
- package/lib/core/common/IRUtils.d.ts +7 -0
- package/lib/core/common/IRUtils.d.ts.map +1 -0
- package/lib/core/common/IRUtils.js +39 -0
- package/lib/core/common/ModelUtils.d.ts +61 -0
- package/lib/core/common/ModelUtils.d.ts.map +1 -0
- package/lib/core/common/ModelUtils.js +628 -0
- package/lib/core/common/RefUseReplacer.d.ts +14 -0
- package/lib/core/common/RefUseReplacer.d.ts.map +1 -0
- package/lib/core/common/RefUseReplacer.js +50 -0
- package/lib/core/common/StmtUseReplacer.d.ts +16 -0
- package/lib/core/common/StmtUseReplacer.d.ts.map +1 -0
- package/lib/core/common/StmtUseReplacer.js +88 -0
- package/lib/core/common/TSConst.d.ts +11 -0
- package/lib/core/common/TSConst.d.ts.map +1 -0
- package/lib/core/common/TSConst.js +28 -0
- package/lib/core/common/TypeInference.d.ts +44 -0
- package/lib/core/common/TypeInference.d.ts.map +1 -0
- package/lib/core/common/TypeInference.js +519 -0
- package/lib/core/common/ValueUtil.d.ts +17 -0
- package/lib/core/common/ValueUtil.d.ts.map +1 -0
- package/lib/core/common/ValueUtil.js +71 -0
- package/lib/core/common/VisibleValue.d.ts +37 -0
- package/lib/core/common/VisibleValue.d.ts.map +1 -0
- package/lib/core/common/VisibleValue.js +212 -0
- package/lib/core/dataflow/DataflowProblem.d.ts +21 -0
- package/lib/core/dataflow/DataflowProblem.d.ts.map +1 -0
- package/lib/core/dataflow/DataflowProblem.js +33 -0
- package/lib/core/dataflow/DataflowResult.d.ts +8 -0
- package/lib/core/dataflow/DataflowResult.d.ts.map +1 -0
- package/lib/core/dataflow/DataflowResult.js +26 -0
- package/lib/core/dataflow/DataflowSolver.d.ts +40 -0
- package/lib/core/dataflow/DataflowSolver.d.ts.map +1 -0
- package/lib/core/dataflow/DataflowSolver.js +318 -0
- package/lib/core/dataflow/Edge.d.ts +16 -0
- package/lib/core/dataflow/Edge.d.ts.map +1 -0
- package/lib/core/dataflow/Edge.js +40 -0
- package/lib/core/dataflow/Fact.d.ts +7 -0
- package/lib/core/dataflow/Fact.d.ts.map +1 -0
- package/lib/core/dataflow/Fact.js +24 -0
- package/lib/core/dataflow/TiantAnalysis.d.ts +37 -0
- package/lib/core/dataflow/TiantAnalysis.d.ts.map +1 -0
- package/lib/core/dataflow/TiantAnalysis.js +326 -0
- package/lib/core/dataflow/UndefinedVariable.d.ts +32 -0
- package/lib/core/dataflow/UndefinedVariable.d.ts.map +1 -0
- package/lib/core/dataflow/UndefinedVariable.js +235 -0
- package/lib/core/dataflow/Util.d.ts +8 -0
- package/lib/core/dataflow/Util.d.ts.map +1 -0
- package/lib/core/dataflow/Util.js +137 -0
- package/lib/core/graph/BasicBlock.d.ts +26 -0
- package/lib/core/graph/BasicBlock.d.ts.map +1 -0
- package/lib/core/graph/BasicBlock.js +92 -0
- package/lib/core/graph/Cfg.d.ts +29 -0
- package/lib/core/graph/Cfg.d.ts.map +1 -0
- package/lib/core/graph/Cfg.js +154 -0
- package/lib/core/graph/DominanceFinder.d.ts +16 -0
- package/lib/core/graph/DominanceFinder.d.ts.map +1 -0
- package/lib/core/graph/DominanceFinder.js +121 -0
- package/lib/core/graph/DominanceTree.d.ts +13 -0
- package/lib/core/graph/DominanceTree.d.ts.map +1 -0
- package/lib/core/graph/DominanceTree.js +70 -0
- package/lib/core/graph/ViewTree.d.ts +115 -0
- package/lib/core/graph/ViewTree.d.ts.map +1 -0
- package/lib/core/graph/ViewTree.js +16 -0
- package/lib/core/graph/builder/ViewTreeBuilder.d.ts +204 -0
- package/lib/core/graph/builder/ViewTreeBuilder.d.ts.map +1 -0
- package/lib/core/graph/builder/ViewTreeBuilder.js +1046 -0
- package/lib/core/model/ArkBody.d.ts +22 -0
- package/lib/core/model/ArkBody.d.ts.map +1 -0
- package/lib/core/model/ArkBody.js +63 -0
- package/lib/core/model/ArkClass.d.ts +110 -0
- package/lib/core/model/ArkClass.d.ts.map +1 -0
- package/lib/core/model/ArkClass.js +315 -0
- package/lib/core/model/ArkExport.d.ts +70 -0
- package/lib/core/model/ArkExport.d.ts.map +1 -0
- package/lib/core/model/ArkExport.js +143 -0
- package/lib/core/model/ArkField.d.ts +60 -0
- package/lib/core/model/ArkField.d.ts.map +1 -0
- package/lib/core/model/ArkField.js +157 -0
- package/lib/core/model/ArkFile.d.ts +62 -0
- package/lib/core/model/ArkFile.d.ts.map +1 -0
- package/lib/core/model/ArkFile.js +160 -0
- package/lib/core/model/ArkImport.d.ts +43 -0
- package/lib/core/model/ArkImport.d.ts.map +1 -0
- package/lib/core/model/ArkImport.js +109 -0
- package/lib/core/model/ArkMethod.d.ts +76 -0
- package/lib/core/model/ArkMethod.d.ts.map +1 -0
- package/lib/core/model/ArkMethod.js +235 -0
- package/lib/core/model/ArkNamespace.d.ts +65 -0
- package/lib/core/model/ArkNamespace.d.ts.map +1 -0
- package/lib/core/model/ArkNamespace.js +182 -0
- package/lib/core/model/ArkSignature.d.ts +98 -0
- package/lib/core/model/ArkSignature.d.ts.map +1 -0
- package/lib/core/model/ArkSignature.js +281 -0
- package/lib/core/model/builder/ArkClassBuilder.d.ts +13 -0
- package/lib/core/model/builder/ArkClassBuilder.d.ts.map +1 -0
- package/lib/core/model/builder/ArkClassBuilder.js +422 -0
- package/lib/core/model/builder/ArkExportBuilder.d.ts +25 -0
- package/lib/core/model/builder/ArkExportBuilder.d.ts.map +1 -0
- package/lib/core/model/builder/ArkExportBuilder.js +207 -0
- package/lib/core/model/builder/ArkFieldBuilder.d.ts +9 -0
- package/lib/core/model/builder/ArkFieldBuilder.d.ts.map +1 -0
- package/lib/core/model/builder/ArkFieldBuilder.js +189 -0
- package/lib/core/model/builder/ArkFileBuilder.d.ts +10 -0
- package/lib/core/model/builder/ArkFileBuilder.d.ts.map +1 -0
- package/lib/core/model/builder/ArkFileBuilder.js +164 -0
- package/lib/core/model/builder/ArkImportBuilder.d.ts +4 -0
- package/lib/core/model/builder/ArkImportBuilder.d.ts.map +1 -0
- package/lib/core/model/builder/ArkImportBuilder.js +125 -0
- package/lib/core/model/builder/ArkMethodBuilder.d.ts +60 -0
- package/lib/core/model/builder/ArkMethodBuilder.d.ts.map +1 -0
- package/lib/core/model/builder/ArkMethodBuilder.js +419 -0
- package/lib/core/model/builder/ArkNamespaceBuilder.d.ts +5 -0
- package/lib/core/model/builder/ArkNamespaceBuilder.d.ts.map +1 -0
- package/lib/core/model/builder/ArkNamespaceBuilder.js +171 -0
- package/lib/core/model/builder/ArkSignatureBuilder.d.ts +9 -0
- package/lib/core/model/builder/ArkSignatureBuilder.d.ts.map +1 -0
- package/lib/core/model/builder/ArkSignatureBuilder.js +40 -0
- package/lib/core/model/builder/builderUtils.d.ts +18 -0
- package/lib/core/model/builder/builderUtils.d.ts.map +1 -0
- package/lib/core/model/builder/builderUtils.js +449 -0
- package/lib/index.d.ts +77 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +190 -0
- package/lib/save/ArkStream.d.ts +24 -0
- package/lib/save/ArkStream.d.ts.map +1 -0
- package/lib/save/ArkStream.js +83 -0
- package/lib/save/DotPrinter.d.ts +48 -0
- package/lib/save/DotPrinter.d.ts.map +1 -0
- package/lib/save/DotPrinter.js +229 -0
- package/lib/save/GraphPrinter.d.ts +17 -0
- package/lib/save/GraphPrinter.d.ts.map +1 -0
- package/lib/save/GraphPrinter.js +135 -0
- package/lib/save/JsonPrinter.d.ts +28 -0
- package/lib/save/JsonPrinter.d.ts.map +1 -0
- package/lib/save/JsonPrinter.js +511 -0
- package/lib/save/Printer.d.ts +17 -0
- package/lib/save/Printer.d.ts.map +1 -0
- package/lib/save/Printer.js +27 -0
- package/lib/save/PrinterBuilder.d.ts +38 -0
- package/lib/save/PrinterBuilder.d.ts.map +1 -0
- package/lib/save/PrinterBuilder.js +102 -0
- package/lib/save/serializeArkIR.d.ts +3 -0
- package/lib/save/serializeArkIR.d.ts.map +1 -0
- package/lib/save/serializeArkIR.js +166 -0
- package/lib/save/source/SourceBase.d.ts +32 -0
- package/lib/save/source/SourceBase.d.ts.map +1 -0
- package/lib/save/source/SourceBase.js +93 -0
- package/lib/save/source/SourceBody.d.ts +60 -0
- package/lib/save/source/SourceBody.d.ts.map +1 -0
- package/lib/save/source/SourceBody.js +292 -0
- package/lib/save/source/SourceClass.d.ts +27 -0
- package/lib/save/source/SourceClass.d.ts.map +1 -0
- package/lib/save/source/SourceClass.js +202 -0
- package/lib/save/source/SourceField.d.ts +15 -0
- package/lib/save/source/SourceField.d.ts.map +1 -0
- package/lib/save/source/SourceField.js +68 -0
- package/lib/save/source/SourceFilePrinter.d.ts +14 -0
- package/lib/save/source/SourceFilePrinter.d.ts.map +1 -0
- package/lib/save/source/SourceFilePrinter.js +73 -0
- package/lib/save/source/SourceMethod.d.ts +24 -0
- package/lib/save/source/SourceMethod.d.ts.map +1 -0
- package/lib/save/source/SourceMethod.js +170 -0
- package/lib/save/source/SourceModule.d.ts +18 -0
- package/lib/save/source/SourceModule.d.ts.map +1 -0
- package/lib/save/source/SourceModule.js +120 -0
- package/lib/save/source/SourceNamespace.d.ts +13 -0
- package/lib/save/source/SourceNamespace.d.ts.map +1 -0
- package/lib/save/source/SourceNamespace.js +80 -0
- package/lib/save/source/SourceStmt.d.ts +172 -0
- package/lib/save/source/SourceStmt.d.ts.map +1 -0
- package/lib/save/source/SourceStmt.js +757 -0
- package/lib/save/source/SourceTransformer.d.ts +37 -0
- package/lib/save/source/SourceTransformer.d.ts.map +1 -0
- package/lib/save/source/SourceTransformer.js +338 -0
- package/lib/save/source/SourceUtils.d.ts +25 -0
- package/lib/save/source/SourceUtils.d.ts.map +1 -0
- package/lib/save/source/SourceUtils.js +201 -0
- package/lib/transformer/FunctionTransformer.d.ts +3 -0
- package/lib/transformer/FunctionTransformer.d.ts.map +1 -0
- package/lib/transformer/FunctionTransformer.js +17 -0
- package/lib/transformer/SceneTransformer.d.ts +3 -0
- package/lib/transformer/SceneTransformer.d.ts.map +1 -0
- package/lib/transformer/SceneTransformer.js +17 -0
- package/lib/transformer/StaticSingleAssignmentFormer.d.ts +13 -0
- package/lib/transformer/StaticSingleAssignmentFormer.d.ts.map +1 -0
- package/lib/transformer/StaticSingleAssignmentFormer.js +259 -0
- package/lib/transformer/Transformer.d.ts +7 -0
- package/lib/transformer/Transformer.d.ts.map +1 -0
- package/lib/transformer/Transformer.js +22 -0
- package/lib/utils/CfgStructualAnalysis.d.ts +93 -0
- package/lib/utils/CfgStructualAnalysis.d.ts.map +1 -0
- package/lib/utils/CfgStructualAnalysis.js +955 -0
- package/lib/utils/FileUtils.d.ts +18 -0
- package/lib/utils/FileUtils.d.ts.map +1 -0
- package/lib/utils/FileUtils.js +131 -0
- package/lib/utils/callGraphUtils.d.ts +31 -0
- package/lib/utils/callGraphUtils.d.ts.map +1 -0
- package/lib/utils/callGraphUtils.js +208 -0
- package/lib/utils/entryMethodUtils.d.ts +16 -0
- package/lib/utils/entryMethodUtils.d.ts.map +1 -0
- package/lib/utils/entryMethodUtils.js +139 -0
- package/lib/utils/getAllFiles.d.ts +10 -0
- package/lib/utils/getAllFiles.d.ts.map +1 -0
- package/lib/utils/getAllFiles.js +91 -0
- package/lib/utils/json5parser.d.ts +7 -0
- package/lib/utils/json5parser.d.ts.map +1 -0
- package/lib/utils/json5parser.js +146 -0
- package/lib/utils/logger.d.ts +19 -0
- package/lib/utils/logger.d.ts.map +1 -0
- package/lib/utils/logger.js +90 -0
- package/lib/utils/pathTransfer.d.ts +2 -0
- package/lib/utils/pathTransfer.d.ts.map +1 -0
- package/lib/utils/pathTransfer.js +25 -0
- package/package.json +29 -0
|
@@ -0,0 +1,955 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.AbstractFlowGraph = exports.CodeBlockType = void 0;
|
|
18
|
+
const Stmt_1 = require("../core/base/Stmt");
|
|
19
|
+
var CodeBlockType;
|
|
20
|
+
(function (CodeBlockType) {
|
|
21
|
+
CodeBlockType[CodeBlockType["NORMAL"] = 0] = "NORMAL";
|
|
22
|
+
CodeBlockType[CodeBlockType["IF"] = 1] = "IF";
|
|
23
|
+
CodeBlockType[CodeBlockType["ELSE"] = 2] = "ELSE";
|
|
24
|
+
CodeBlockType[CodeBlockType["BREAK"] = 3] = "BREAK";
|
|
25
|
+
CodeBlockType[CodeBlockType["CONTINUE"] = 4] = "CONTINUE";
|
|
26
|
+
CodeBlockType[CodeBlockType["DO"] = 5] = "DO";
|
|
27
|
+
CodeBlockType[CodeBlockType["DO_WHILE"] = 6] = "DO_WHILE";
|
|
28
|
+
CodeBlockType[CodeBlockType["WHILE"] = 7] = "WHILE";
|
|
29
|
+
CodeBlockType[CodeBlockType["FOR"] = 8] = "FOR";
|
|
30
|
+
CodeBlockType[CodeBlockType["COMPOUND_END"] = 9] = "COMPOUND_END";
|
|
31
|
+
})(CodeBlockType = exports.CodeBlockType || (exports.CodeBlockType = {}));
|
|
32
|
+
class AbstractFlowGraph {
|
|
33
|
+
constructor(cfg) {
|
|
34
|
+
this.nodes = [];
|
|
35
|
+
this.structOf = new Map();
|
|
36
|
+
this.structTypes = new Map();
|
|
37
|
+
this.structBlocks = new Map();
|
|
38
|
+
this.loopMap = new Map();
|
|
39
|
+
this.block2NodeMap = new Map();
|
|
40
|
+
for (const bb of cfg.getBlocks()) {
|
|
41
|
+
let an = new AbstractNode();
|
|
42
|
+
an.setBlock(bb);
|
|
43
|
+
this.block2NodeMap.set(bb, an);
|
|
44
|
+
}
|
|
45
|
+
for (const bb of cfg.getBlocks()) {
|
|
46
|
+
let an = this.block2NodeMap.get(bb);
|
|
47
|
+
for (const succ of bb.getSuccessors()) {
|
|
48
|
+
an.addSucc(this.block2NodeMap.get(succ));
|
|
49
|
+
}
|
|
50
|
+
for (const pred of bb.getPredecessors()) {
|
|
51
|
+
an.addPred(this.block2NodeMap.get(pred));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
this.entry = this.block2NodeMap.get(cfg.getStartingBlock());
|
|
55
|
+
this.structuralAnalysis();
|
|
56
|
+
}
|
|
57
|
+
getEntry() {
|
|
58
|
+
return this.entry;
|
|
59
|
+
}
|
|
60
|
+
getForIncBlock(block) {
|
|
61
|
+
let node = this.block2NodeMap.get(block);
|
|
62
|
+
let loop = this.loopMap.get(node);
|
|
63
|
+
return loop.inc.getBlock();
|
|
64
|
+
}
|
|
65
|
+
preOrder(node, callback, visitor = new Set()) {
|
|
66
|
+
visitor.add(node);
|
|
67
|
+
node.traversal(callback, CodeBlockType.NORMAL);
|
|
68
|
+
for (const succ of node.getSucc()) {
|
|
69
|
+
if (!visitor.has(succ)) {
|
|
70
|
+
this.preOrder(succ, callback, visitor);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
structuralAnalysis() {
|
|
75
|
+
this.nodes = this.dfsPostOrder(this.entry);
|
|
76
|
+
this.buildCyclicStructural();
|
|
77
|
+
// acyclic structural
|
|
78
|
+
let postMax = this.nodes.length;
|
|
79
|
+
let change = true;
|
|
80
|
+
while (postMax > 1 && change) {
|
|
81
|
+
change = false;
|
|
82
|
+
for (let i = 0; i < postMax; i++) {
|
|
83
|
+
let node = this.nodes[i];
|
|
84
|
+
let nset = new Set();
|
|
85
|
+
let rtype = this.identifyRegionType(node, nset);
|
|
86
|
+
if (rtype != undefined) {
|
|
87
|
+
let p = this.reduce(rtype, nset);
|
|
88
|
+
if (p) {
|
|
89
|
+
if (nset.has(this.entry)) {
|
|
90
|
+
this.entry = p;
|
|
91
|
+
}
|
|
92
|
+
this.nodes = this.dfsPostOrder(this.entry);
|
|
93
|
+
postMax = this.nodes.length;
|
|
94
|
+
change = true;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
dfsPostOrder(node, visitor = new Set(), postOrder = []) {
|
|
101
|
+
visitor.add(node);
|
|
102
|
+
for (const succ of node.getSucc()) {
|
|
103
|
+
if (visitor.has(succ)) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
this.dfsPostOrder(succ, visitor, postOrder);
|
|
107
|
+
}
|
|
108
|
+
postOrder.push(node);
|
|
109
|
+
return postOrder;
|
|
110
|
+
}
|
|
111
|
+
buildCyclicStructural() {
|
|
112
|
+
for (const loop of this.prepareBuildLoops()) {
|
|
113
|
+
let nset = new Set();
|
|
114
|
+
for (const n of loop) {
|
|
115
|
+
if (this.structOf.has(n)) {
|
|
116
|
+
nset.add(this.structOf.get(n));
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
nset.add(n);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
let rtype = this.cyclicRegionType(nset);
|
|
123
|
+
let region = this.createRegion(rtype, nset);
|
|
124
|
+
region.revise();
|
|
125
|
+
this.structTypes.set(region, rtype);
|
|
126
|
+
let blocks = new Set();
|
|
127
|
+
for (const s of nset) {
|
|
128
|
+
if (!this.structOf.has(s)) {
|
|
129
|
+
this.structOf.set(s, region);
|
|
130
|
+
}
|
|
131
|
+
if (this.structBlocks.has(s)) {
|
|
132
|
+
for (const b of this.structBlocks.get(s)) {
|
|
133
|
+
blocks.add(b);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
blocks.add(s);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
this.structBlocks.set(region, blocks);
|
|
141
|
+
this.loopMap.set(region.header, region);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
prepareBuildLoops() {
|
|
145
|
+
let dom = this.buildDominator();
|
|
146
|
+
let loops = [];
|
|
147
|
+
for (const header of this.nodes) {
|
|
148
|
+
let innermost = undefined;
|
|
149
|
+
let longest = 0;
|
|
150
|
+
let backEdges = this.getBackEdges(dom, header);
|
|
151
|
+
if (backEdges.size == 0) {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
for (const start of backEdges) {
|
|
155
|
+
let loop = this.naturalLoop(start, header);
|
|
156
|
+
if (!innermost || loop.size > longest) {
|
|
157
|
+
innermost = loop;
|
|
158
|
+
longest = loop.size;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
loops.push(innermost);
|
|
162
|
+
}
|
|
163
|
+
loops.sort((a, b) => a.size - b.size);
|
|
164
|
+
return loops;
|
|
165
|
+
}
|
|
166
|
+
buildDominator() {
|
|
167
|
+
let domin = new Map();
|
|
168
|
+
domin.set(this.entry, new Set([this.entry]));
|
|
169
|
+
for (const node of this.nodes) {
|
|
170
|
+
if (node != this.entry) {
|
|
171
|
+
domin.set(node, new Set(this.nodes));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
let change = true;
|
|
175
|
+
while (change) {
|
|
176
|
+
change = false;
|
|
177
|
+
for (const node of this.nodes) {
|
|
178
|
+
if (node == this.entry) {
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
let t = new Set(domin.get(node));
|
|
182
|
+
for (const p of node.getPred()) {
|
|
183
|
+
t = this.setIntersect(t, domin.get(p));
|
|
184
|
+
}
|
|
185
|
+
t.add(node);
|
|
186
|
+
if (!this.isSetEqual(t, domin.get(node))) {
|
|
187
|
+
change = true;
|
|
188
|
+
domin.set(node, t);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return domin;
|
|
193
|
+
}
|
|
194
|
+
getBackEdges(dom, header) {
|
|
195
|
+
var _a;
|
|
196
|
+
let backEdges = new Set();
|
|
197
|
+
for (const n of header.getPred()) {
|
|
198
|
+
// h dom n && n -> h
|
|
199
|
+
if ((_a = dom.get(n)) === null || _a === void 0 ? void 0 : _a.has(header)) {
|
|
200
|
+
backEdges.add(n);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return backEdges;
|
|
204
|
+
}
|
|
205
|
+
naturalLoop(backEdgeStart, backEdgeEnd) {
|
|
206
|
+
let stack = [];
|
|
207
|
+
let loop = new Set([backEdgeEnd, backEdgeStart]);
|
|
208
|
+
stack.push(backEdgeStart);
|
|
209
|
+
while (stack.length > 0) {
|
|
210
|
+
let m = stack.shift();
|
|
211
|
+
for (const pred of m.getPred()) {
|
|
212
|
+
if (loop.has(pred)) {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
loop.add(pred);
|
|
216
|
+
stack.push(pred);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return loop;
|
|
220
|
+
}
|
|
221
|
+
isForLoopIncNode(node) {
|
|
222
|
+
for (const loop of this.loopMap.values()) {
|
|
223
|
+
if (loop.getType() == RegionType.FOR_LOOP_REGION) {
|
|
224
|
+
if (node == loop.inc) {
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
isValidInBlocks(node) {
|
|
232
|
+
if (this.isForLoopIncNode(node) || node.hasIfStmt()) {
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
isIfRegion(node, nodeSet) {
|
|
238
|
+
nodeSet.clear();
|
|
239
|
+
if (node.getSucc().length != 2) {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
let m = node.getSucc()[0];
|
|
243
|
+
let n = node.getSucc()[1];
|
|
244
|
+
if (m.getSucc().length == 1 && m.getSucc()[0] == n) {
|
|
245
|
+
nodeSet.add(node).add(m);
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
isIfExitRegion(node, nodeSet) {
|
|
251
|
+
nodeSet.clear();
|
|
252
|
+
if (node.getSucc().length != 2) {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
let m = node.getSucc()[0];
|
|
256
|
+
if (m.hasReturnStmt()) {
|
|
257
|
+
nodeSet.add(node).add(m);
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
isIfElseRegion(node, nodeSet) {
|
|
263
|
+
nodeSet.clear();
|
|
264
|
+
if (node.getSucc().length != 2) {
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
let m = node.getSucc()[0];
|
|
268
|
+
let n = node.getSucc()[1];
|
|
269
|
+
if ((m.getSucc().length == 1 &&
|
|
270
|
+
n.getSucc().length == 1 &&
|
|
271
|
+
m.getPred().length == 1 &&
|
|
272
|
+
n.getPred().length == 1 &&
|
|
273
|
+
m.getSucc()[0] == n.getSucc()[0]) ||
|
|
274
|
+
(m.getSucc().length == 0 && n.getSucc().length == 0)) {
|
|
275
|
+
nodeSet.add(node).add(m).add(n);
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
isBlockRegion(node, nodeSet) {
|
|
281
|
+
let n = node;
|
|
282
|
+
let p = true;
|
|
283
|
+
let s = n.getSucc().length == 1;
|
|
284
|
+
nodeSet.clear();
|
|
285
|
+
let blocks = [];
|
|
286
|
+
while (p && s && !nodeSet.has(n) && this.isValidInBlocks(n)) {
|
|
287
|
+
nodeSet.add(n);
|
|
288
|
+
blocks.push(n);
|
|
289
|
+
n = n.getSucc()[0];
|
|
290
|
+
p = n.getPred().length == 1;
|
|
291
|
+
s = n.getSucc().length == 1;
|
|
292
|
+
}
|
|
293
|
+
if (p && this.isValidInBlocks(n)) {
|
|
294
|
+
if (!nodeSet.has(n)) {
|
|
295
|
+
blocks.push(n);
|
|
296
|
+
}
|
|
297
|
+
nodeSet.add(n);
|
|
298
|
+
}
|
|
299
|
+
n = node;
|
|
300
|
+
p = n.getPred().length == 1;
|
|
301
|
+
s = true;
|
|
302
|
+
while (p && s && this.isValidInBlocks(n)) {
|
|
303
|
+
if (!nodeSet.has(n)) {
|
|
304
|
+
blocks.unshift(n);
|
|
305
|
+
}
|
|
306
|
+
nodeSet.add(n);
|
|
307
|
+
n = n.getPred()[0];
|
|
308
|
+
if (nodeSet.has(n)) {
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
p = n.getPred().length == 1;
|
|
312
|
+
s = n.getSucc().length == 1;
|
|
313
|
+
}
|
|
314
|
+
if (s && this.isValidInBlocks(n)) {
|
|
315
|
+
if (!nodeSet.has(n)) {
|
|
316
|
+
blocks.unshift(n);
|
|
317
|
+
}
|
|
318
|
+
nodeSet.add(n);
|
|
319
|
+
}
|
|
320
|
+
nodeSet.clear();
|
|
321
|
+
for (const n of blocks) {
|
|
322
|
+
nodeSet.add(n);
|
|
323
|
+
}
|
|
324
|
+
if (nodeSet.size >= 2) {
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
isIfBreakRegion(node, nodeSet, loop) {
|
|
330
|
+
let m = node.getSucc()[0];
|
|
331
|
+
nodeSet.clear();
|
|
332
|
+
if (this.isExitLoop(m, this.structBlocks.get(loop))) {
|
|
333
|
+
nodeSet.add(node);
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
336
|
+
if (m.getSucc().length == 1 && this.isExitLoop(m.getSucc()[0], this.structBlocks.get(loop))) {
|
|
337
|
+
nodeSet.add(node).add(m);
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
return false;
|
|
341
|
+
}
|
|
342
|
+
isIfContinueRegion(node, nodeSet, loop) {
|
|
343
|
+
nodeSet.clear();
|
|
344
|
+
let m = node.getSucc()[0];
|
|
345
|
+
let n = node.getSucc()[1];
|
|
346
|
+
if (loop.control.has(m)) {
|
|
347
|
+
nodeSet.add(node);
|
|
348
|
+
return true;
|
|
349
|
+
}
|
|
350
|
+
if (m.getSucc().length == 1 &&
|
|
351
|
+
loop.control.has(m.getSucc()[0]) &&
|
|
352
|
+
!loop.control.has(n) &&
|
|
353
|
+
!this.isIfElseRegion(node, nodeSet)) {
|
|
354
|
+
nodeSet.add(node).add(m);
|
|
355
|
+
return true;
|
|
356
|
+
}
|
|
357
|
+
return false;
|
|
358
|
+
}
|
|
359
|
+
isWhileRegion(node, nodeSet, loop) {
|
|
360
|
+
nodeSet.clear();
|
|
361
|
+
let m = node.getSucc()[0];
|
|
362
|
+
if (loop.header == node && m.getSucc().length == 1 && m.getPred().length == 1 && m.getSucc()[0] == node) {
|
|
363
|
+
nodeSet.add(node).add(m);
|
|
364
|
+
return true;
|
|
365
|
+
}
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
isForRegion(node, nodeSet, loop) {
|
|
369
|
+
nodeSet.clear();
|
|
370
|
+
if (loop.header == node && loop.getType() == RegionType.FOR_LOOP_REGION) {
|
|
371
|
+
let forLoop = loop;
|
|
372
|
+
let blocks = node.getSucc()[0];
|
|
373
|
+
if (forLoop.inc.getPred().length == 1 &&
|
|
374
|
+
forLoop.inc.getPred()[0] == blocks &&
|
|
375
|
+
blocks.getSucc().length == 1) {
|
|
376
|
+
nodeSet.add(node).add(forLoop.inc).add(blocks);
|
|
377
|
+
return true;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
isDoWhileRegion(node, nodeSet, loop) {
|
|
383
|
+
nodeSet.clear();
|
|
384
|
+
if (loop.back == node && loop.getType() == RegionType.DO_WHILE_LOOP_REGION) {
|
|
385
|
+
let blocks = node.getPred()[0];
|
|
386
|
+
if (blocks.getSucc().length == 1 && blocks.getSucc()[0] == node && node.getSucc()[0] == blocks) {
|
|
387
|
+
nodeSet.add(blocks).add(node);
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
identifyRegionType(node, nodeSet) {
|
|
394
|
+
if (this.isBlockRegion(node, nodeSet)) {
|
|
395
|
+
return RegionType.BLOCK_REGION;
|
|
396
|
+
}
|
|
397
|
+
let inLoop = false;
|
|
398
|
+
let region = this.structOf.get(node);
|
|
399
|
+
if (region && LOOP_TYPES.has(region === null || region === void 0 ? void 0 : region.getType())) {
|
|
400
|
+
inLoop = true;
|
|
401
|
+
}
|
|
402
|
+
if (new Set(node.getPred()).has(node) && new Set(node.getSucc()).has(node)) {
|
|
403
|
+
nodeSet.add(node);
|
|
404
|
+
return RegionType.SELF_LOOP_REGION;
|
|
405
|
+
}
|
|
406
|
+
if (node.getSucc().length != 2) {
|
|
407
|
+
return undefined;
|
|
408
|
+
}
|
|
409
|
+
if (inLoop) {
|
|
410
|
+
let loop = region;
|
|
411
|
+
if (!loop.control.has(node)) {
|
|
412
|
+
if (this.isIfBreakRegion(node, nodeSet, loop)) {
|
|
413
|
+
return RegionType.IF_THEN_BREAK_REGION;
|
|
414
|
+
}
|
|
415
|
+
if (this.isIfContinueRegion(node, nodeSet, loop)) {
|
|
416
|
+
return RegionType.IF_THEN_CONTINUE_REGION;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (this.isWhileRegion(node, nodeSet, loop)) {
|
|
420
|
+
return RegionType.WHILE_LOOP_REGION;
|
|
421
|
+
}
|
|
422
|
+
if (this.isForRegion(node, nodeSet, loop)) {
|
|
423
|
+
return RegionType.FOR_LOOP_REGION;
|
|
424
|
+
}
|
|
425
|
+
if (this.isDoWhileRegion(node, nodeSet, loop)) {
|
|
426
|
+
return RegionType.DO_WHILE_LOOP_REGION;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
// check for if
|
|
430
|
+
if (this.isIfExitRegion(node, nodeSet)) {
|
|
431
|
+
return RegionType.IF_THEN_EXIT_REGION;
|
|
432
|
+
}
|
|
433
|
+
if (this.isIfRegion(node, nodeSet)) {
|
|
434
|
+
return RegionType.IF_REGION;
|
|
435
|
+
}
|
|
436
|
+
// check for an if else
|
|
437
|
+
if (this.isIfElseRegion(node, nodeSet)) {
|
|
438
|
+
return RegionType.IF_ELSE_REGION;
|
|
439
|
+
}
|
|
440
|
+
return undefined;
|
|
441
|
+
}
|
|
442
|
+
cyclicRegionType(nodeSet) {
|
|
443
|
+
var _a, _b;
|
|
444
|
+
let nodes = Array.from(nodeSet);
|
|
445
|
+
let header = nodes[0];
|
|
446
|
+
if (nodeSet.size == 1) {
|
|
447
|
+
return RegionType.SELF_LOOP_REGION;
|
|
448
|
+
}
|
|
449
|
+
let back = nodes[1];
|
|
450
|
+
// exit loop from back
|
|
451
|
+
if (!this.hasExitLoopSucc(header, nodeSet) && this.hasExitLoopSucc(back, nodeSet)) {
|
|
452
|
+
return RegionType.DO_WHILE_LOOP_REGION;
|
|
453
|
+
}
|
|
454
|
+
if (this.hasExitLoopSucc(header, nodeSet) && this.hasExitLoopSucc(back, nodeSet)) {
|
|
455
|
+
// header true exit loop --> exit is break
|
|
456
|
+
if (!nodeSet.has(header.getSucc()[0])) {
|
|
457
|
+
return RegionType.DO_WHILE_LOOP_REGION;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
// for
|
|
461
|
+
if (back.getSucc().length == 1 && ((_b = (_a = back.getBlock()) === null || _a === void 0 ? void 0 : _a.getStmts()) === null || _b === void 0 ? void 0 : _b.length) == 1) {
|
|
462
|
+
let isForLoop = true;
|
|
463
|
+
for (const pred of header.getPred()) {
|
|
464
|
+
if (nodeSet.has(pred) && pred != back) {
|
|
465
|
+
isForLoop = false;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (isForLoop) {
|
|
469
|
+
return RegionType.FOR_LOOP_REGION;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return RegionType.WHILE_LOOP_REGION;
|
|
473
|
+
}
|
|
474
|
+
hasExitLoopSucc(node, nodeSet) {
|
|
475
|
+
for (const succ of node.getSucc()) {
|
|
476
|
+
if (!nodeSet.has(succ)) {
|
|
477
|
+
return true;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
isExitLoop(node, nodeSet) {
|
|
483
|
+
if (this.structBlocks.has(node)) {
|
|
484
|
+
for (const n of this.structBlocks.get(node)) {
|
|
485
|
+
if (!nodeSet.has(n)) {
|
|
486
|
+
return true;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
else {
|
|
491
|
+
if (!nodeSet.has(node)) {
|
|
492
|
+
return true;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
return false;
|
|
496
|
+
}
|
|
497
|
+
createRegion(rtype, nodeSet) {
|
|
498
|
+
let node;
|
|
499
|
+
if (rtype == RegionType.BLOCK_REGION) {
|
|
500
|
+
node = new BlockRegion(nodeSet);
|
|
501
|
+
}
|
|
502
|
+
else if (rtype == RegionType.IF_ELSE_REGION) {
|
|
503
|
+
node = new IfElseRegion(nodeSet);
|
|
504
|
+
}
|
|
505
|
+
else if (rtype == RegionType.IF_REGION) {
|
|
506
|
+
node = new IfRegion(nodeSet);
|
|
507
|
+
}
|
|
508
|
+
else if (rtype == RegionType.IF_THEN_EXIT_REGION) {
|
|
509
|
+
node = new IfExitRegion(nodeSet);
|
|
510
|
+
}
|
|
511
|
+
else if (rtype == RegionType.IF_THEN_BREAK_REGION) {
|
|
512
|
+
node = new IfBreakRegion(nodeSet);
|
|
513
|
+
}
|
|
514
|
+
else if (rtype == RegionType.IF_THEN_CONTINUE_REGION) {
|
|
515
|
+
node = new IfContinueRegion(nodeSet);
|
|
516
|
+
}
|
|
517
|
+
else if (rtype == RegionType.SELF_LOOP_REGION) {
|
|
518
|
+
node = new SelfLoopRegion(nodeSet);
|
|
519
|
+
}
|
|
520
|
+
else if (rtype == RegionType.WHILE_LOOP_REGION) {
|
|
521
|
+
let whileLoop = new WhileLoopRegion(nodeSet);
|
|
522
|
+
this.loopMap.set(whileLoop.header, whileLoop);
|
|
523
|
+
node = whileLoop;
|
|
524
|
+
}
|
|
525
|
+
else if (rtype == RegionType.FOR_LOOP_REGION) {
|
|
526
|
+
let forLoop = new ForLoopRegion(nodeSet);
|
|
527
|
+
this.loopMap.set(forLoop.header, forLoop);
|
|
528
|
+
node = forLoop;
|
|
529
|
+
}
|
|
530
|
+
else if (rtype == RegionType.DO_WHILE_LOOP_REGION) {
|
|
531
|
+
let doWhileLoop = new DoWhileLoopRegion(nodeSet);
|
|
532
|
+
this.loopMap.set(doWhileLoop.header, doWhileLoop);
|
|
533
|
+
node = doWhileLoop;
|
|
534
|
+
}
|
|
535
|
+
return node;
|
|
536
|
+
}
|
|
537
|
+
reduce(rtype, nodeSet) {
|
|
538
|
+
let region = this.createRegion(rtype, nodeSet);
|
|
539
|
+
region === null || region === void 0 ? void 0 : region.replace();
|
|
540
|
+
if (region) {
|
|
541
|
+
this.structTypes.set(region, rtype);
|
|
542
|
+
let blocks = new Set();
|
|
543
|
+
for (const s of nodeSet) {
|
|
544
|
+
this.structOf.set(s, region);
|
|
545
|
+
if (this.structBlocks.has(s)) {
|
|
546
|
+
for (const b of this.structBlocks.get(s)) {
|
|
547
|
+
blocks.add(b);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
else {
|
|
551
|
+
blocks.add(s);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
this.structBlocks.set(region, blocks);
|
|
555
|
+
}
|
|
556
|
+
return region;
|
|
557
|
+
}
|
|
558
|
+
setIntersect(a, b) {
|
|
559
|
+
let r = new Set();
|
|
560
|
+
if (b == undefined) {
|
|
561
|
+
debugger;
|
|
562
|
+
}
|
|
563
|
+
for (const n of b) {
|
|
564
|
+
if (a.has(n)) {
|
|
565
|
+
r.add(n);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
return r;
|
|
569
|
+
}
|
|
570
|
+
isSetEqual(a, b) {
|
|
571
|
+
if (a.size != b.size) {
|
|
572
|
+
return false;
|
|
573
|
+
}
|
|
574
|
+
return this.setIntersect(a, b).size == a.size;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
exports.AbstractFlowGraph = AbstractFlowGraph;
|
|
578
|
+
var RegionType;
|
|
579
|
+
(function (RegionType) {
|
|
580
|
+
RegionType[RegionType["ABSTRACT_NODE"] = 0] = "ABSTRACT_NODE";
|
|
581
|
+
/* Sequence of blocks. */
|
|
582
|
+
RegionType[RegionType["BLOCK_REGION"] = 1] = "BLOCK_REGION";
|
|
583
|
+
RegionType[RegionType["IF_REGION"] = 2] = "IF_REGION";
|
|
584
|
+
RegionType[RegionType["IF_ELSE_REGION"] = 3] = "IF_ELSE_REGION";
|
|
585
|
+
RegionType[RegionType["IF_THEN_EXIT_REGION"] = 4] = "IF_THEN_EXIT_REGION";
|
|
586
|
+
RegionType[RegionType["IF_THEN_BREAK_REGION"] = 5] = "IF_THEN_BREAK_REGION";
|
|
587
|
+
RegionType[RegionType["IF_THEN_CONTINUE_REGION"] = 6] = "IF_THEN_CONTINUE_REGION";
|
|
588
|
+
RegionType[RegionType["SELF_LOOP_REGION"] = 7] = "SELF_LOOP_REGION";
|
|
589
|
+
RegionType[RegionType["NATURAL_LOOP_REGION"] = 8] = "NATURAL_LOOP_REGION";
|
|
590
|
+
RegionType[RegionType["WHILE_LOOP_REGION"] = 9] = "WHILE_LOOP_REGION";
|
|
591
|
+
RegionType[RegionType["DO_WHILE_LOOP_REGION"] = 10] = "DO_WHILE_LOOP_REGION";
|
|
592
|
+
RegionType[RegionType["FOR_LOOP_REGION"] = 11] = "FOR_LOOP_REGION";
|
|
593
|
+
RegionType[RegionType["CASE_REGION"] = 12] = "CASE_REGION";
|
|
594
|
+
RegionType[RegionType["SWITCH_REGION"] = 13] = "SWITCH_REGION";
|
|
595
|
+
})(RegionType || (RegionType = {}));
|
|
596
|
+
const LOOP_TYPES = new Set([
|
|
597
|
+
RegionType.SELF_LOOP_REGION,
|
|
598
|
+
RegionType.NATURAL_LOOP_REGION,
|
|
599
|
+
RegionType.WHILE_LOOP_REGION,
|
|
600
|
+
RegionType.FOR_LOOP_REGION,
|
|
601
|
+
RegionType.DO_WHILE_LOOP_REGION,
|
|
602
|
+
]);
|
|
603
|
+
class AbstractNode {
|
|
604
|
+
constructor() {
|
|
605
|
+
this.predNodes = [];
|
|
606
|
+
this.succNodes = [];
|
|
607
|
+
this.type = RegionType.ABSTRACT_NODE;
|
|
608
|
+
}
|
|
609
|
+
traversal(callback, type) {
|
|
610
|
+
callback(this.bb, type);
|
|
611
|
+
}
|
|
612
|
+
getType() {
|
|
613
|
+
return this.type;
|
|
614
|
+
}
|
|
615
|
+
getSucc() {
|
|
616
|
+
return this.succNodes;
|
|
617
|
+
}
|
|
618
|
+
addSucc(node) {
|
|
619
|
+
this.succNodes.push(node);
|
|
620
|
+
}
|
|
621
|
+
replaceSucc(src, dst) {
|
|
622
|
+
for (let i = 0; i < this.succNodes.length; i++) {
|
|
623
|
+
if (this.succNodes[i] == src) {
|
|
624
|
+
this.succNodes[i] = dst;
|
|
625
|
+
break;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
removeSucc(src) {
|
|
630
|
+
for (let i = 0; i < this.predNodes.length; i++) {
|
|
631
|
+
if (this.succNodes[i] == src) {
|
|
632
|
+
this.succNodes.splice(i, 1);
|
|
633
|
+
break;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
getPred() {
|
|
638
|
+
return this.predNodes;
|
|
639
|
+
}
|
|
640
|
+
addPred(block) {
|
|
641
|
+
this.predNodes.push(block);
|
|
642
|
+
}
|
|
643
|
+
replacePred(src, dst) {
|
|
644
|
+
for (let i = 0; i < this.predNodes.length; i++) {
|
|
645
|
+
if (this.predNodes[i] == src) {
|
|
646
|
+
this.predNodes[i] = dst;
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
removePred(src) {
|
|
652
|
+
for (let i = 0; i < this.predNodes.length; i++) {
|
|
653
|
+
if (this.predNodes[i] == src) {
|
|
654
|
+
this.predNodes.splice(i, 1);
|
|
655
|
+
break;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
setBlock(bb) {
|
|
660
|
+
this.bb = bb;
|
|
661
|
+
}
|
|
662
|
+
getBlock() {
|
|
663
|
+
return this.bb;
|
|
664
|
+
}
|
|
665
|
+
hasIfStmt() {
|
|
666
|
+
if (!this.bb) {
|
|
667
|
+
return false;
|
|
668
|
+
}
|
|
669
|
+
for (let stmt of this.bb.getStmts()) {
|
|
670
|
+
if (stmt instanceof Stmt_1.ArkIfStmt) {
|
|
671
|
+
return true;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
return false;
|
|
675
|
+
}
|
|
676
|
+
hasReturnStmt() {
|
|
677
|
+
if (!this.bb) {
|
|
678
|
+
return false;
|
|
679
|
+
}
|
|
680
|
+
for (let stmt of this.bb.getStmts()) {
|
|
681
|
+
if (stmt instanceof Stmt_1.ArkReturnStmt) {
|
|
682
|
+
return true;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
return false;
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
class Region extends AbstractNode {
|
|
689
|
+
constructor(nset, type) {
|
|
690
|
+
super();
|
|
691
|
+
this.nset = nset;
|
|
692
|
+
this.type = type;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
class BlockRegion extends Region {
|
|
696
|
+
constructor(nset) {
|
|
697
|
+
super(nset, RegionType.BLOCK_REGION);
|
|
698
|
+
this.blocks = Array.from(nset);
|
|
699
|
+
}
|
|
700
|
+
replace() {
|
|
701
|
+
for (let pred of this.blocks[0].getPred()) {
|
|
702
|
+
pred.replaceSucc(this.blocks[0], this);
|
|
703
|
+
this.addPred(pred);
|
|
704
|
+
}
|
|
705
|
+
for (let succ of this.blocks[this.blocks.length - 1].getSucc()) {
|
|
706
|
+
succ.replacePred(this.blocks[this.blocks.length - 1], this);
|
|
707
|
+
this.addSucc(succ);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
traversal(callback) {
|
|
711
|
+
for (const node of this.blocks) {
|
|
712
|
+
node.traversal(callback, CodeBlockType.NORMAL);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
class NaturalLoopRegion extends Region {
|
|
717
|
+
constructor(nset, type = RegionType.NATURAL_LOOP_REGION) {
|
|
718
|
+
super(nset, type);
|
|
719
|
+
let nodes = Array.from(nset);
|
|
720
|
+
this.header = nodes[0];
|
|
721
|
+
this.back = nodes[1];
|
|
722
|
+
this.control = new Set([this.header]);
|
|
723
|
+
}
|
|
724
|
+
replace() {
|
|
725
|
+
for (let pred of this.header.getPred()) {
|
|
726
|
+
if (!this.nset.has(pred)) {
|
|
727
|
+
pred.replaceSucc(this.header, this);
|
|
728
|
+
this.addPred(pred);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
let succNodes = new Set();
|
|
732
|
+
for (let node of this.nset) {
|
|
733
|
+
for (let succ of node.getSucc()) {
|
|
734
|
+
if (!this.nset.has(succ)) {
|
|
735
|
+
succNodes.add(succ);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
if (succNodes.size == 0) {
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
let pred = Array.from(succNodes)[0];
|
|
743
|
+
let replaced = false;
|
|
744
|
+
for (let succ of pred.getPred()) {
|
|
745
|
+
if (this.nset.has(succ)) {
|
|
746
|
+
if (!replaced) {
|
|
747
|
+
pred.replacePred(succ, this);
|
|
748
|
+
this.addSucc(pred);
|
|
749
|
+
replaced = true;
|
|
750
|
+
}
|
|
751
|
+
else {
|
|
752
|
+
pred.removePred(succ);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
revise() {
|
|
758
|
+
// add node to loop sets
|
|
759
|
+
for (const node of this.nset) {
|
|
760
|
+
for (const succ of node.getSucc()) {
|
|
761
|
+
if (!this.nset.has(succ) &&
|
|
762
|
+
succ != this.getExitNode() &&
|
|
763
|
+
succ.getSucc().length == 1 &&
|
|
764
|
+
succ.getSucc()[0] == this.getExitNode()) {
|
|
765
|
+
this.nset.add(succ);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
class SelfLoopRegion extends NaturalLoopRegion {
|
|
772
|
+
constructor(nset) {
|
|
773
|
+
super(nset, RegionType.SELF_LOOP_REGION);
|
|
774
|
+
this.back = this.header;
|
|
775
|
+
}
|
|
776
|
+
replace() {
|
|
777
|
+
for (let pred of this.header.getPred()) {
|
|
778
|
+
if (pred != this.header) {
|
|
779
|
+
pred.replaceSucc(this.header, this);
|
|
780
|
+
this.addPred(pred);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
for (let succ of this.header.getSucc()) {
|
|
784
|
+
if (succ != this.header) {
|
|
785
|
+
succ.replacePred(this.header, this);
|
|
786
|
+
this.addSucc(succ);
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
getExitNode() {
|
|
791
|
+
return this.header.getSucc()[1];
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
class WhileLoopRegion extends NaturalLoopRegion {
|
|
795
|
+
constructor(nset) {
|
|
796
|
+
super(nset, RegionType.WHILE_LOOP_REGION);
|
|
797
|
+
}
|
|
798
|
+
traversal(callback) {
|
|
799
|
+
this.header.traversal(callback, CodeBlockType.WHILE);
|
|
800
|
+
this.back.traversal(callback, CodeBlockType.NORMAL);
|
|
801
|
+
callback(undefined, CodeBlockType.COMPOUND_END);
|
|
802
|
+
}
|
|
803
|
+
getExitNode() {
|
|
804
|
+
return this.header.getSucc()[1];
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
class DoWhileLoopRegion extends NaturalLoopRegion {
|
|
808
|
+
constructor(nset) {
|
|
809
|
+
super(nset, RegionType.DO_WHILE_LOOP_REGION);
|
|
810
|
+
this.control.clear();
|
|
811
|
+
this.control.add(this.back);
|
|
812
|
+
}
|
|
813
|
+
traversal(callback) {
|
|
814
|
+
callback(undefined, CodeBlockType.DO);
|
|
815
|
+
this.header.traversal(callback, CodeBlockType.NORMAL);
|
|
816
|
+
this.back.traversal(callback, CodeBlockType.DO_WHILE);
|
|
817
|
+
}
|
|
818
|
+
getExitNode() {
|
|
819
|
+
return this.back.getSucc()[1];
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
class ForLoopRegion extends NaturalLoopRegion {
|
|
823
|
+
constructor(nset) {
|
|
824
|
+
super(nset, RegionType.FOR_LOOP_REGION);
|
|
825
|
+
this.inc = this.back;
|
|
826
|
+
this.control.add(this.inc);
|
|
827
|
+
}
|
|
828
|
+
traversal(callback) {
|
|
829
|
+
this.header.traversal(callback, CodeBlockType.FOR);
|
|
830
|
+
for (const node of this.nset) {
|
|
831
|
+
if (node != this.header && node != this.inc) {
|
|
832
|
+
node.traversal(callback, CodeBlockType.NORMAL);
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
callback(undefined, CodeBlockType.COMPOUND_END);
|
|
836
|
+
}
|
|
837
|
+
getExitNode() {
|
|
838
|
+
return this.header.getSucc()[1];
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
class IfRegion extends Region {
|
|
842
|
+
constructor(nset) {
|
|
843
|
+
super(nset, RegionType.IF_REGION);
|
|
844
|
+
let nodes = Array.from(nset);
|
|
845
|
+
this.contition = nodes[0];
|
|
846
|
+
this.then = nodes[1];
|
|
847
|
+
}
|
|
848
|
+
replace() {
|
|
849
|
+
this.replaceContitionPred();
|
|
850
|
+
for (let succ of this.then.getSucc()) {
|
|
851
|
+
if (succ != this.then) {
|
|
852
|
+
succ.replacePred(this.then, this);
|
|
853
|
+
succ.removePred(this.contition);
|
|
854
|
+
this.addSucc(succ);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
traversal(callback) {
|
|
859
|
+
this.contition.traversal(callback, CodeBlockType.IF);
|
|
860
|
+
this.then.traversal(callback, CodeBlockType.NORMAL);
|
|
861
|
+
callback(undefined, CodeBlockType.COMPOUND_END);
|
|
862
|
+
}
|
|
863
|
+
replaceContitionPred() {
|
|
864
|
+
for (let pred of this.contition.getPred()) {
|
|
865
|
+
if (pred != this.contition) {
|
|
866
|
+
pred.replaceSucc(this.contition, this);
|
|
867
|
+
this.addPred(pred);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
class IfExitRegion extends IfRegion {
|
|
873
|
+
constructor(nset) {
|
|
874
|
+
super(nset);
|
|
875
|
+
this.type = RegionType.IF_THEN_EXIT_REGION;
|
|
876
|
+
}
|
|
877
|
+
replace() {
|
|
878
|
+
this.replaceContitionPred();
|
|
879
|
+
;
|
|
880
|
+
let succ = this.contition.getSucc()[1];
|
|
881
|
+
succ.replacePred(this.contition, this);
|
|
882
|
+
this.addSucc(succ);
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
class IfBreakRegion extends IfRegion {
|
|
886
|
+
constructor(nset) {
|
|
887
|
+
super(nset);
|
|
888
|
+
this.type = RegionType.IF_THEN_BREAK_REGION;
|
|
889
|
+
}
|
|
890
|
+
replace() {
|
|
891
|
+
this.replaceContitionPred();
|
|
892
|
+
let succ = this.contition.getSucc()[1];
|
|
893
|
+
succ.replacePred(this.contition, this);
|
|
894
|
+
this.addSucc(succ);
|
|
895
|
+
if (this.then) {
|
|
896
|
+
succ = this.then.getSucc()[0];
|
|
897
|
+
succ.removePred(this.then);
|
|
898
|
+
}
|
|
899
|
+
else {
|
|
900
|
+
succ = this.contition.getSucc()[0];
|
|
901
|
+
succ.removePred(this.contition);
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
traversal(callback) {
|
|
905
|
+
var _a;
|
|
906
|
+
this.contition.traversal(callback, CodeBlockType.IF);
|
|
907
|
+
(_a = this.then) === null || _a === void 0 ? void 0 : _a.traversal(callback, CodeBlockType.NORMAL);
|
|
908
|
+
callback(undefined, CodeBlockType.BREAK);
|
|
909
|
+
callback(undefined, CodeBlockType.COMPOUND_END);
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
class IfContinueRegion extends IfBreakRegion {
|
|
913
|
+
constructor(nset) {
|
|
914
|
+
super(nset);
|
|
915
|
+
this.type = RegionType.IF_THEN_CONTINUE_REGION;
|
|
916
|
+
}
|
|
917
|
+
traversal(callback) {
|
|
918
|
+
var _a;
|
|
919
|
+
this.contition.traversal(callback, CodeBlockType.IF);
|
|
920
|
+
(_a = this.then) === null || _a === void 0 ? void 0 : _a.traversal(callback, CodeBlockType.NORMAL);
|
|
921
|
+
callback(undefined, CodeBlockType.CONTINUE);
|
|
922
|
+
callback(undefined, CodeBlockType.COMPOUND_END);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
class IfElseRegion extends Region {
|
|
926
|
+
constructor(nset) {
|
|
927
|
+
super(nset, RegionType.IF_ELSE_REGION);
|
|
928
|
+
let nodes = Array.from(nset);
|
|
929
|
+
this.contition = nodes[0];
|
|
930
|
+
this.then = nodes[1];
|
|
931
|
+
this.else = nodes[2];
|
|
932
|
+
}
|
|
933
|
+
replace() {
|
|
934
|
+
for (let pred of this.contition.getPred()) {
|
|
935
|
+
if (pred != this.contition) {
|
|
936
|
+
pred.replaceSucc(this.contition, this);
|
|
937
|
+
this.addPred(pred);
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
for (let succ of this.then.getSucc()) {
|
|
941
|
+
if (succ != this.then) {
|
|
942
|
+
succ.replacePred(this.then, this);
|
|
943
|
+
succ.removePred(this.else);
|
|
944
|
+
this.addSucc(succ);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
traversal(callback) {
|
|
949
|
+
this.contition.traversal(callback, CodeBlockType.IF);
|
|
950
|
+
this.then.traversal(callback, CodeBlockType.NORMAL);
|
|
951
|
+
callback(undefined, CodeBlockType.ELSE);
|
|
952
|
+
this.else.traversal(callback, CodeBlockType.NORMAL);
|
|
953
|
+
callback(undefined, CodeBlockType.COMPOUND_END);
|
|
954
|
+
}
|
|
955
|
+
}
|