• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "cfgBuilderPhase.h"
17 #include "compiler/core/CFG.h"
18 #include "util/options.h"
19 
20 namespace ark::es2panda::compiler {
21 
Perform(public_lib::Context * ctx,parser::Program * program)22 bool CFGBuilderPhase::Perform(public_lib::Context *ctx, parser::Program *program)
23 {
24     if (ctx->config->options->IsGenStdlib()) {
25         return true;
26     }
27 
28     for (auto &[_, ext_programs] : program->ExternalSources()) {
29         (void)_;
30         for (auto *extProg : ext_programs) {
31             Perform(ctx, extProg);
32         }
33     }
34 
35     compiler::CFG *cfg = program->GetCFG();
36 
37     program->Ast()->IterateRecursively([&](ir::AstNode *node) -> void {
38         if (node->IsScriptFunction()) {
39             cfg->Build(node->AsScriptFunction());
40         }
41     });
42 
43     cfg->MergeEmptyBlocks();
44 
45     if (ctx->config->options->IsDumpCfg()) {
46         std::string filename = program->AbsoluteName().Mutf8();
47         cfg->DumpDot((filename + ".dot").c_str());
48     }
49 
50     return true;
51 }
52 
53 }  // namespace ark::es2panda::compiler
54