diff --git a/es2panda/BUILD.gn b/es2panda/BUILD.gn index 31eda11e1fb5cb994b62b1ab1977499d511a2f89..b6f2a746ef298c1ccd484ea27378ec48d80c3516 100644 --- a/es2panda/BUILD.gn +++ b/es2panda/BUILD.gn @@ -234,6 +234,7 @@ es2panda_src = [ "typescript/types/unknownType.cpp", "typescript/types/voidType.cpp", "util/bitset.cpp", + "util/dumper.cpp", "util/helpers.cpp", "util/ustring.cpp", ] diff --git a/es2panda/aot/main.cpp b/es2panda/aot/main.cpp index 79c5131dec17642a0799feb1dc1c771c6999f135..467bff4775933bd2c8facb22eec3367a804d21cd 100644 --- a/es2panda/aot/main.cpp +++ b/es2panda/aot/main.cpp @@ -22,10 +22,11 @@ #include #include #endif +#include #include #include #include -#include +#include #include #include @@ -54,9 +55,12 @@ public: } }; -static int GenerateProgram(panda::pandasm::Program *prog, const std::string &output, int optLevel, bool dumpAsm, - bool dumpSize) +static int GenerateProgram(panda::pandasm::Program *prog, std::unique_ptr &options) { + const std::string output = options->CompilerOutput(); + int optLevel = options->OptLevel(); + const es2panda::CompilerOptions compilerOptions = options->CompilerOptions(); + bool dumpSize = options->SizeStat(); std::map stat; std::map *statp = optLevel != 0 ? &stat : nullptr; panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps maps {}; @@ -78,7 +82,7 @@ static int GenerateProgram(panda::pandasm::Program *prog, const std::string &out } #endif - if (dumpAsm) { + if (compilerOptions.dumpAsm) { es2panda::Compiler::DumpAsm(prog); } @@ -86,6 +90,10 @@ static int GenerateProgram(panda::pandasm::Program *prog, const std::string &out return 1; } + if (compilerOptions.dumpLiteralBuffer) { + panda::es2panda::util::Dumper::DumpLiterals(prog->literalarray_table); + } + if (dumpSize && optLevel != 0) { size_t totalSize = 0; std::cout << "Panda file size statistic:" << std::endl; @@ -136,8 +144,7 @@ int Run(int argc, const char **argv) return err.ErrorCode(); } - GenerateProgram(program, options->CompilerOutput(), options->OptLevel(), options->CompilerOptions().dumpAsm, - options->SizeStat()); + GenerateProgram(program, options); delete program; return 0; diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index 9f72eac598d446a9f0b38324db70539060e0ed72..ba5c41e50521a58d2f65b14af2a8213a1e0ee22e 100644 --- a/es2panda/aot/options.cpp +++ b/es2panda/aot/options.cpp @@ -62,6 +62,7 @@ bool Options::Parse(int argc, const char **argv) panda::PandArg opOptLevel("opt-level", 0, "Compiler optimization level (options: 0 | 1 | 2)"); panda::PandArg opThreadCount("thread", 0, "Number of worker theads"); panda::PandArg opSizeStat("dump-size-stat", false, "Dump size statistics"); + panda::PandArg opDumpLiteralBuffer("dump-literal-buffer", false, "Dump literal buffer"); panda::PandArg outputFile("output", "", "Compiler binary output (.abc)"); // tail arguments @@ -78,6 +79,7 @@ bool Options::Parse(int argc, const char **argv) argparser_->Add(&opOptLevel); argparser_->Add(&opThreadCount); argparser_->Add(&opSizeStat); + argparser_->Add(&opDumpLiteralBuffer); argparser_->Add(&inputExtension); argparser_->Add(&outputFile); @@ -157,6 +159,7 @@ bool Options::Parse(int argc, const char **argv) compilerOptions_.dumpDebugInfo = opDumpDebugInfo.GetValue(); compilerOptions_.isDebug = opDebugInfo.GetValue(); compilerOptions_.parseOnly = opParseOnly.GetValue(); + compilerOptions_.dumpLiteralBuffer = opDumpLiteralBuffer.GetValue(); return true; } diff --git a/es2panda/es2panda.h b/es2panda/es2panda.h index f68866c0caa52f0ecbf0a60cb7d8b18f122ddb6b..488d2fb422d245bd2c073c997b73a484c27369be 100644 --- a/es2panda/es2panda.h +++ b/es2panda/es2panda.h @@ -54,6 +54,7 @@ struct CompilerOptions { bool dumpAsm {false}; bool dumpDebugInfo {false}; bool parseOnly {false}; + bool dumpLiteralBuffer {false}; }; enum class ErrorType { diff --git a/es2panda/util/dumper.cpp b/es2panda/util/dumper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8fd3ff4e6b65a78ac71e77d54d01468ef714ec69 --- /dev/null +++ b/es2panda/util/dumper.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "dumper.h" + +namespace panda::es2panda::util { + +void Dumper::DumpLiterals(std::map &literalTable) +{ + std::cout << "======> literal array buffer <======" << std::endl; + for (auto it : literalTable) { + std::cout << "------------------------------------" << std::endl; + std::cout << "slot " << it.first << std::endl; + int count = 0; + for (auto literal : it.second.literals_) { + std::cout << "{" << std::endl; + std::cout << " index: " << count++ << std::endl; + std::cout << " tag: " << + static_cast::type>(literal.tag_) << std::endl; + std::visit([](auto&& element) { + std::cout << " val: " << element << std::endl; + }, literal.value_); + std::cout << "}," << std::endl; + } + } +} + +} // namespace panda::es2panda::util + diff --git a/es2panda/util/dumper.h b/es2panda/util/dumper.h new file mode 100644 index 0000000000000000000000000000000000000000..4024fb0c253a889a8143dcc6de4fcacb9972a35a --- /dev/null +++ b/es2panda/util/dumper.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ES2PANDA_UTIL_DUMPER_H +#define ES2PANDA_UTIL_DUMPER_H + +#include +#include +#include +#include + +namespace panda::es2panda::util { + +class Dumper { +public: + static void DumpLiterals(std::map &literalTable); +}; + +} // namespace panda::es2panda::util + +#endif \ No newline at end of file