diff --git a/es2panda/aot/main.cpp b/es2panda/aot/main.cpp index e1ca0abdefd830b046c1e6d33fce0a5eae8a8346..194d69922e8430fb318029055469e81ffa2cc8b0 100644 --- a/es2panda/aot/main.cpp +++ b/es2panda/aot/main.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -48,10 +49,9 @@ public: } }; -static void GenerateBase64Output(panda::pandasm::Program *prog, - panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps *mapsp) +static void GenerateBase64Output(panda::pandasm::Program *prog) { - auto pandaFile = panda::pandasm::AsmEmitter::Emit(*prog, mapsp); + auto pandaFile = panda::pandasm::AsmEmitter::Emit(*prog, nullptr); const uint8_t *buffer = pandaFile->GetBase(); size_t size = pandaFile->GetPtr().GetSize(); std::string content(reinterpret_cast(buffer), size); @@ -59,25 +59,52 @@ static void GenerateBase64Output(panda::pandasm::Program *prog, std::cout << base64Output << std::endl; } -static void DumpPandaFileSizeStatistic(std::map &stat) +static bool EmitProgramWithOpt(const std::string &output, panda::pandasm::Program *prog, + const std::unique_ptr &options) { - size_t totalSize = 0; - std::cout << "Panda file size statistic:" << std::endl; - constexpr std::array INFO_STATS = {"instructions_number", "codesize"}; - - for (const auto &[name, size] : stat) { - if (find(INFO_STATS.begin(), INFO_STATS.end(), name) != INFO_STATS.end()) { - continue; + const bool emitDebuginfo = options->CompilerOptions().isDebug; + bool isOpt = !emitDebuginfo && options->OptLevel() != 0 && options->ScriptKind() != parser::ScriptKind::COMMONJS; + std::map stat; + std::map *statp = options->SizeStat() ? &stat : nullptr; + panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps maps{}; + panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps *mapsp = isOpt ? &maps : nullptr; + + if (isOpt) { + if (!panda::pandasm::AsmEmitter::Emit(output, *prog, statp, mapsp, emitDebuginfo)) { + std::cerr << "Failed to emit unoptimized single abc file: " << output << std::endl; + return false; } - std::cout << name << " section: " << size << std::endl; - totalSize += size; + bytecodeopt::OptimizeBytecode(prog, mapsp, output, true); // true: has memory pool } - - for (const auto &name : INFO_STATS) { - std::cout << name << ": " << stat.at(std::string(name)) << std::endl; + if (!panda::pandasm::AsmEmitter::Emit(output, *prog, statp, mapsp, emitDebuginfo)) { + std::cerr << "Failed to emit single abc file: " << output << std::endl; + return false; } + return true; +} - std::cout << "total: " << totalSize << std::endl; +static bool EmitProgramsWithOpt(const std::string &output, const std::vector &progs, + const std::unique_ptr &options) +{ + const bool emitDebuginfo = options->CompilerOptions().isDebug; + bool isOpt = !emitDebuginfo && options->OptLevel() != 0 && options->ScriptKind() != parser::ScriptKind::COMMONJS; + std::map stat; + std::map *statp = options->SizeStat() ? &stat : nullptr; + panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps maps{}; + panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps *mapsp = isOpt ? &maps : nullptr; + + if (isOpt) { + if (!panda::pandasm::AsmEmitter::EmitPrograms(output, progs, statp, mapsp, emitDebuginfo)) { + std::cerr << "Failed to emit unoptimized abc file: " << output << std::endl; + return false; + } + bytecodeopt::OptimizeBytecodeWithPrograms(progs, mapsp, output, true); // true: has memory pool + } + if (!panda::pandasm::AsmEmitter::EmitPrograms(output, progs, statp, mapsp, emitDebuginfo)) { + std::cerr << "Failed to emit abc file: " << output << std::endl; + return false; + } + return true; } static bool GenerateMultiProgram(const std::unordered_map &programs, @@ -90,71 +117,48 @@ static bool GenerateMultiProgram(const std::unordered_mapsecond; - if (!panda::pandasm::AsmEmitter::EmitPrograms(output, progs, true)) { - std::cerr << "Failed to emit merged program, error: " << - panda::pandasm::AsmEmitter::GetLastError() << std::endl; - return false; - } - } else { - for (auto &prog: programs) { - if (!panda::pandasm::AsmEmitter::Emit(prog.second, *(prog.first), nullptr, nullptr, true)) { - std::cout << "Failed to emit single program, error: " << - panda::pandasm::AsmEmitter::GetLastError() << std::endl; - return false; - } + return EmitProgramsWithOpt(output, progs, options); + } + for (auto &prog: programs) { + if (!EmitProgramWithOpt(prog.second, prog.first, options)) { + return false; } } return true; } -static bool GenerateProgram(const std::unordered_map &programs, +static void TryDumpAsmAndLiteralBuffer(const std::unordered_map &programs, const std::unique_ptr &options) { - int optLevel = options->OptLevel(); - bool dumpSize = options->SizeStat(); - const es2panda::CompilerOptions compilerOptions = options->CompilerOptions(); - if (compilerOptions.dumpAsm || compilerOptions.dumpLiteralBuffer) { - for (auto &prog : programs) { - if (compilerOptions.dumpAsm) { - es2panda::Compiler::DumpAsm(prog.first); - } - - if (compilerOptions.dumpLiteralBuffer) { - panda::es2panda::util::Dumper::DumpLiterals(prog.first->literalarray_table); - } - } + const auto &compilerOptions = options->CompilerOptions(); + if (!compilerOptions.dumpAsm && !compilerOptions.dumpLiteralBuffer) { + return; } - - if (programs.size() > 1) { - return GenerateMultiProgram(programs, options); - } else { - auto *prog = programs.begin()->first; - std::map stat; - std::map *statp = optLevel != 0 ? &stat : nullptr; - panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps maps {}; - panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps *mapsp = optLevel != 0 ? &maps : nullptr; - - auto output = programs.begin()->second; - if (output.empty()) { - GenerateBase64Output(prog, mapsp); - return true; - } - - if (options->compilerProtoOutput().size() > 0) { - panda::proto::ProtobufSnapshotGenerator::GenerateSnapshot(*prog, options->compilerProtoOutput()); - return true; - } - - if (!panda::pandasm::AsmEmitter::Emit(output, *prog, statp, mapsp, true)) { - return false; + for (auto &prog : programs) { + if (compilerOptions.dumpAsm) { + es2panda::Compiler::DumpAsm(prog.first); } - if (dumpSize && optLevel != 0) { - DumpPandaFileSizeStatistic(stat); + if (compilerOptions.dumpLiteralBuffer) { + panda::es2panda::util::Dumper::DumpLiterals(prog.first->literalarray_table); } } +} - return true; +static bool GenerateProgram(const std::unordered_map &programs, + const std::unique_ptr &options) +{ + TryDumpAsmAndLiteralBuffer(programs, options); + if (programs.size() > 1) { + return GenerateMultiProgram(programs, options); + } + auto *prog = programs.begin()->first; + const auto &output = programs.begin()->second; + if (output.empty()) { + GenerateBase64Output(prog); + return true; + } + return EmitProgramWithOpt(output, prog, options); } static bool GenerateAbcFiles(const std::map &programsInfo, @@ -218,15 +222,15 @@ int Run(int argc, const char **argv) expectedProgsCount++; } + if (!GenerateAbcFiles(programsInfo, options, expectedProgsCount)) { + return 1; + } + if (!options->CacheFile().empty()) { proto::ProtobufSnapshotGenerator::UpdateCacheFile(programsInfo, options->CompilerOptions().isDebug, options->CacheFile()); } - if (!GenerateAbcFiles(programsInfo, options, expectedProgsCount)) { - return 1; - } - return 0; } } // namespace panda::es2panda::aot diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index e8d2e3ead687fb1d89491ce5bb3adfc151e7914b..db3494be7266373510a49810d5f4311b5368a1ce 100644 --- a/es2panda/aot/options.cpp +++ b/es2panda/aot/options.cpp @@ -145,7 +145,8 @@ bool Options::Parse(int argc, const char **argv) panda::PandArg opDumpAssembly("dump-assembly", false, "Dump pandasm"); panda::PandArg opDebugInfo("debug-info", false, "Compile with debug info"); panda::PandArg opDumpDebugInfo("dump-debug-info", false, "Dump debug info"); - panda::PandArg opOptLevel("opt-level", 0, "Compiler optimization level (options: 0 | 1 | 2)"); + constexpr static int OPT_2 = 2; + panda::PandArg opOptLevel("opt-level", OPT_2, "Compiler optimization level (options: 0 | 1 | 2), default: 2"); panda::PandArg opFunctionThreadCount("function-threads", 0, "Number of worker threads to compile function"); panda::PandArg opFileThreadCount("file-threads", 0, "Number of worker threads to compile file"); panda::PandArg opSizeStat("dump-size-stat", false, "Dump size statistics"); @@ -158,8 +159,6 @@ bool Options::Parse(int argc, const char **argv) panda::PandArg base64Output("base64Output", false, "output panda file content as base64 to std out"); panda::PandArg sourceFile("source-file", "", "specify the file path info recorded in generated abc"); - panda::PandArg outputProto("outputProto", "", - "specify the output name for serializd protobuf file (.protoBin)"); panda::PandArg opCacheFile("cache-file", "", "cache file for incremental compile"); panda::PandArg opNpmModuleEntryList("npm-module-entry-list", "", "entry list file for module compile"); panda::PandArg opMergeAbc("merge-abc", false, "Compile as merge abc"); @@ -201,7 +200,6 @@ bool Options::Parse(int argc, const char **argv) argparser_->Add(&outputFile); argparser_->Add(&sourceFile); argparser_->Add(&recordName); - argparser_->Add(&outputProto); argparser_->Add(&opCacheFile); argparser_->Add(&opNpmModuleEntryList); argparser_->Add(&opMergeAbc); @@ -349,10 +347,6 @@ bool Options::Parse(int argc, const char **argv) sourceFiles_.push_back(src); } - if (!outputProto.GetValue().empty()) { - compilerProtoOutput_ = outputProto.GetValue(); - } - optLevel_ = opOptLevel.GetValue(); functionThreadCount_ = opFunctionThreadCount.GetValue(); fileThreadCount_ = opFileThreadCount.GetValue(); diff --git a/es2panda/aot/options.h b/es2panda/aot/options.h index f2bae2a776c546fc16832cb5f24b8d4f501bb46a..cb435425df487c49edb10ccc035e12a151da47d7 100644 --- a/es2panda/aot/options.h +++ b/es2panda/aot/options.h @@ -117,11 +117,6 @@ public: std::string ExtractContentFromBase64Input(const std::string &inputBase64String); - const std::string &compilerProtoOutput() const - { - return compilerProtoOutput_; - } - const std::string &CacheFile() const { return cacheFile_; @@ -152,7 +147,6 @@ private: std::string sourceFile_; std::string recordName_; std::string errorMsg_; - std::string compilerProtoOutput_; int optLevel_ {0}; int functionThreadCount_ {0}; int fileThreadCount_ {0}; diff --git a/es2panda/compiler/core/compileQueue.cpp b/es2panda/compiler/core/compileQueue.cpp index ea11d2bd33e33b34c914a75c9296c6559dc50e12..bdd858326546e4c6ee703026b94308b2636b28cc 100644 --- a/es2panda/compiler/core/compileQueue.cpp +++ b/es2panda/compiler/core/compileQueue.cpp @@ -114,10 +114,6 @@ void CompileFileJob::Run() return; } - if (options_->optLevel != 0) { - util::Helpers::OptimizeProgram(prog, options_); - } - { std::unique_lock lock(global_m_); auto *cache = allocator_->New(src_->hash, prog); diff --git a/es2panda/util/helpers.cpp b/es2panda/util/helpers.cpp index 14f909b075711ebe5374ee7670ae324bb708e411..1fcf9ce9dd43c326a3811a64f02c40333b8c4372 100644 --- a/es2panda/util/helpers.cpp +++ b/es2panda/util/helpers.cpp @@ -427,27 +427,4 @@ std::tuple Helpers::ParamName(ArenaAllocator *allocator, return {Helpers::ToStringView(allocator, index), true}; } -bool Helpers::OptimizeProgram(panda::pandasm::Program * prog, es2panda::CompilerOptions *options) -{ - std::map stat; - std::map *statp = &stat; - panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps maps{}; - panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps *mapsp = &maps; - -#ifdef PANDA_WITH_BYTECODE_OPTIMIZER - const uint32_t COMPONENT_MASK = panda::Logger::Component::ASSEMBLER | - panda::Logger::Component::BYTECODE_OPTIMIZER | - panda::Logger::Component::COMPILER; - panda::Logger::InitializeStdLogging(panda::Logger::Level::ERROR, COMPONENT_MASK); - - if (!panda::pandasm::AsmEmitter::Emit(options->output, *prog, statp, mapsp, true)) { - return false; - } - - panda::bytecodeopt::options.SetOptLevel(options->optLevel); - panda::bytecodeopt::OptimizeBytecode(prog, mapsp, options->output, true, true); -#endif - return true; -} - } // namespace panda::es2panda::util diff --git a/es2panda/util/helpers.h b/es2panda/util/helpers.h index 9321178fda9329eab6de9b2a2c8dec489b21a699..deda59450a243c93d0ac0cb2e4235da17ef98220 100644 --- a/es2panda/util/helpers.h +++ b/es2panda/util/helpers.h @@ -76,8 +76,6 @@ public: static util::StringView FunctionName(const ir::ScriptFunction *func); static std::tuple ParamName(ArenaAllocator *allocator, const ir::AstNode *param, uint32_t index); - - static bool OptimizeProgram(panda::pandasm::Program *prog, es2panda::CompilerOptions *options); template static T BaseName(T const &path, T const &delims = std::string(panda::os::file::File::GetPathDelim())); diff --git a/merge_abc/src/main.cpp b/merge_abc/src/main.cpp index 00c860a4e79e57f2a6cc3dbe154e6a15296cc380..b7562aabb4f06ecc17cf12fcca2cfec5d82234e8 100644 --- a/merge_abc/src/main.cpp +++ b/merge_abc/src/main.cpp @@ -78,9 +78,11 @@ int Run(int argc, const char **argv) proto::ProtobufSnapshotGenerator::GenerateProgram(protoFile, *(programs[idx++]), &allocator); } + std::map stat; + std::map *statp = options->IsDumpSizeStat() ? &stat : nullptr; std::string outputFileName = outputFilePath.append(panda::os::file::File::GetPathDelim()). append(options->outputFileName()); - if (!panda::pandasm::AsmEmitter::EmitPrograms(outputFileName, programs, true)) { + if (!panda::pandasm::AsmEmitter::EmitPrograms(outputFileName, programs, statp, nullptr, true)) { return 1; } diff --git a/merge_abc/src/options.cpp b/merge_abc/src/options.cpp index 0745e103d66f06311922fb3a96e6148794193bde..c97f18467520212badc50a94467b92c574d9e027 100644 --- a/merge_abc/src/options.cpp +++ b/merge_abc/src/options.cpp @@ -36,12 +36,14 @@ bool Options::Parse(int argc, const char **argv) panda::PandArg protoBinSuffix("suffix", "", "suffix of proto bin file"); panda::PandArg outputFileName("output", "", "name of merged panda file"); panda::PandArg outputFilePath("outputFilePath", "", "output path for merged panda file"); + panda::PandArg dumpSizeStat("dump-size-stat", false, "display size statistics of the merged abc file"); argparser_->Add(&opHelp); argparser_->Add(&protoPathInput); argparser_->Add(&protoBinSuffix); argparser_->Add(&outputFileName); argparser_->Add(&outputFilePath); + argparser_->Add(&dumpSizeStat); if (!argparser_->Parse(argc, argv) || opHelp.GetValue() || protoPathInput.GetValue().empty()) { std::stringstream ss; @@ -68,6 +70,9 @@ bool Options::Parse(int argc, const char **argv) if (!outputFilePath.GetValue().empty()) { outputFilePath_ = outputFilePath.GetValue(); } + if (dumpSizeStat.GetValue()) { + dumpSizeStat_ = true; + } return true; } diff --git a/merge_abc/src/options.h b/merge_abc/src/options.h index 8475ab1d20515beba5af1d11809f3a69c6b172cd..0988999354012c342976c389f882b273adc44ab6 100644 --- a/merge_abc/src/options.h +++ b/merge_abc/src/options.h @@ -55,6 +55,11 @@ public: return errorMsg_; } + bool IsDumpSizeStat() const + { + return dumpSizeStat_; + } + private: panda::PandArgParser *argparser_; std::string errorMsg_; @@ -62,6 +67,7 @@ private: std::string protoPathInput_; std::string outputFileName_ {"modules.abc"}; std::string outputFilePath_; + bool dumpSizeStat_ {false}; }; } // panda::proto #endif diff --git a/ts2panda/src/cmdOptions.ts b/ts2panda/src/cmdOptions.ts index 8ea83e1e181970f3de477d0065f2a04b7282dd6e..0a998b496a71cb0df8d15e72c2238abf6f932faf 100644 --- a/ts2panda/src/cmdOptions.ts +++ b/ts2panda/src/cmdOptions.ts @@ -56,6 +56,7 @@ export const ts2pandaOptions = [ { name: 'output-proto', type: Boolean, defaultValue: false, description: "Output protoBin file. Default: false" }, { name: 'merge-abc', type: Boolean, defaultValue: false, description: "Compile as merge abc" }, { name: 'input-file', type: String, defaultValue: "", description: "A file containing a list of source files to be compiled. Each line of this file should be constructed in such format: fileName;recordName;moduleType;sourceFile" }, + { name: 'dump-size-stat', type: Boolean, defaultValue: false, description: "Dump size statistics" }, ] @@ -368,6 +369,13 @@ export class CmdOptions { return this.options["merge-abc"] } + static isDumpSizeStat(): boolean { + if (!this.options) { + return false; + } + return this.options["dump-size-stat"]; + } + // @ts-ignore static parseUserCmd(args: string[]): ts.ParsedCommandLine | undefined { this.options = commandLineArgs(ts2pandaOptions, { partial: true }); diff --git a/ts2panda/src/ts2panda.ts b/ts2panda/src/ts2panda.ts index f7504ddd47c1778b61cf29c35ab48ea4ff6404cf..abc5a13e1bc37cbd080a0c293ea5fd87e9e7eb02 100644 --- a/ts2panda/src/ts2panda.ts +++ b/ts2panda/src/ts2panda.ts @@ -231,7 +231,8 @@ export class Ts2Panda { "is_dts_file": isGlobalDeclare(), "output-proto": CmdOptions.isOutputproto(), "record_type": enableRecordType, - "input-file": CmdOptions.getCompileFilesList() + "input-file": CmdOptions.getCompileFilesList(), + "dump_size_stat": CmdOptions.isDumpSizeStat() }; let jsonOpt = JSON.stringify(options, null, 2); jsonOpt = "$" + jsonOpt.replace(dollarSign, '#$') + "$"; diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index 0c5593e91c2f8603c6d0a75d13973afd5c5e56f4..1d309c7f58ccb4d5100f02c7ee987f936ca20d5e 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -28,16 +28,13 @@ #include "ts2abc_options.h" #include "ts2abc.h" #include "protobufSnapshotGenerator.h" - -#ifdef ENABLE_BYTECODE_OPT #include "optimize_bytecode.h" -#endif - namespace panda::ts2abc { // pandasm definitions constexpr const auto LANG_EXT = panda::pandasm::extensions::Language::ECMASCRIPT; const std::string WHOLE_LINE; bool g_isMergeAbc = false; +bool g_isDumpSizeStat = false; bool g_debugModeEnabled = false; bool g_debugLogEnabled = false; int g_optLevel = 0; @@ -49,7 +46,6 @@ uint32_t g_literalArrayCount = 0; int32_t g_newLiteralArrayIndex = -1; static constexpr const char* TSTYPE_ANNO_RECORD_NAME = "_ESTypeAnnotation"; static constexpr const char* TSTYPE_ANNO_ELEMENT_NAME = "_TypeOfInstruction"; -std::string g_compilerOutputProto = ""; std::string g_recordName = ""; std::string g_outputFileName = ""; bool g_isStartDollar = true; @@ -944,6 +940,14 @@ static void ParseMergeAbcMode(const Json::Value &rootValue) } } +static void ParseDumpSizeStat(const Json::Value &rootValue) +{ + Logd("---------------parse is_dump_size_stat----------------"); + if (rootValue.isMember("dump_size_stat") && rootValue["dump_size_stat"].isBool()) { + g_isDumpSizeStat = rootValue["dump_size_stat"].asBool(); + } +} + static void ParseModuleMode(const Json::Value &rootValue, panda::pandasm::Program &prog) { Logd("----------------parse module_mode-----------------"); @@ -1050,6 +1054,7 @@ static void ParseOptions(const Json::Value &rootValue, panda::pandasm::Program & GenerateESCallTypeAnnotationRecord(prog); GenerateESTypeAnnotationRecord(prog); ParseMergeAbcMode(rootValue); + ParseDumpSizeStat(rootValue); ParseModuleMode(rootValue, prog); ParseCommonJsModuleMode(rootValue, prog); ParseLogEnable(rootValue); @@ -1412,66 +1417,61 @@ static bool IsStartOrEndPosition(int idx, char *buff, const std::string &data) return false; } -static bool EmitProgram(const std::string &output, int optLevel, std::string optLogLevel, panda::pandasm::Program &prog) +static void SetPandaLogger(const std::string &optLogLevel) { - if (g_isOutputProto) { - g_compilerOutputProto = output.substr(0, output.find_last_of(".") + 1).append(PROTO_BIN_SUFFIX); + if (g_optLogLevel == "error") { + return; } + std::string logLevel = (optLogLevel != "error") ? optLogLevel : g_optLogLevel; + panda::Logger::ComponentMask mask; + mask.set(panda::Logger::Component::ASSEMBLER); + mask.set(panda::Logger::Component::BYTECODE_OPTIMIZER); + mask.set(panda::Logger::Component::COMPILER); + panda::Logger::InitializeStdLogging(panda::Logger::LevelFromString(logLevel), mask); +} -#ifdef ENABLE_BYTECODE_OPT - if (g_optLevel != static_cast(OptLevel::O_LEVEL0) || optLevel != static_cast(OptLevel::O_LEVEL0)) { - optLogLevel = (optLogLevel != "error") ? optLogLevel : g_optLogLevel; +static bool EmitProgram(const std::string &output, const panda::ts2abc::Options &options, panda::pandasm::Program &prog) +{ + std::string outputPath = output; + if (g_isOutputProto) { + outputPath = output.substr(0, output.find_last_of(".") + 1).append(PROTO_BIN_SUFFIX); + } - if (g_optLogLevel != "error") { - panda::Logger::ComponentMask mask; - mask.set(panda::Logger::Component::ASSEMBLER); - mask.set(panda::Logger::Component::BYTECODE_OPTIMIZER); - mask.set(panda::Logger::Component::COMPILER); - panda::Logger::InitializeStdLogging(panda::Logger::LevelFromString(optLogLevel), mask); - } + int noOpt = static_cast(OptLevel::O_LEVEL0); + bool emitDebugInfo = GetDebugModeEnabled(); + bool isOpt = (g_optLevel != noOpt || options.GetOptLevelArg() != noOpt) && !emitDebugInfo; + bool dumpSize = g_isDumpSizeStat || options.GetSizeStatArg(); + std::map stat; + std::map *statp = dumpSize ? &stat : nullptr; + panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps maps {}; + panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps* mapsp = isOpt ? &maps : nullptr; - bool emitDebugInfo = true; - std::map stat; - std::map *statp = nullptr; - panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps maps {}; - panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps* mapsp = &maps; + SetPandaLogger(options.GetOptLogLevelArg()); - if (!panda::pandasm::AsmEmitter::Emit(output.c_str(), prog, statp, mapsp, emitDebugInfo)) { + if (isOpt) { + if (!panda::pandasm::AsmEmitter::Emit(outputPath, prog, statp, mapsp, emitDebugInfo)) { std::cerr << "Failed to emit binary data: " << panda::pandasm::AsmEmitter::GetLastError() << std::endl; return false; } + panda::bytecodeopt::OptimizeBytecode(&prog, mapsp, outputPath, false); // false: do not have memory pool + } - panda::bytecodeopt::OptimizeBytecode(&prog, mapsp, output.c_str(), true); - - if (g_compilerOutputProto.size() > 0) { - panda::proto::ProtobufSnapshotGenerator::GenerateSnapshot(prog, g_compilerOutputProto); - return true; - } - - if (!panda::pandasm::AsmEmitter::Emit(output.c_str(), prog, statp, mapsp, emitDebugInfo)) { + if (g_isOutputProto) { + panda::proto::ProtobufSnapshotGenerator::GenerateSnapshot(prog, outputPath); + } else { + if (!panda::pandasm::AsmEmitter::Emit(outputPath, prog, statp, mapsp, emitDebugInfo)) { std::cerr << "Failed to emit binary data: " << panda::pandasm::AsmEmitter::GetLastError() << std::endl; return false; } - return true; - } -#endif - if (g_compilerOutputProto.size() > 0) { - panda::proto::ProtobufSnapshotGenerator::GenerateSnapshot(prog, g_compilerOutputProto); - return true; - } - - if (!panda::pandasm::AsmEmitter::Emit(output.c_str(), prog, nullptr)) { - std::cerr << "Failed to emit binary data: " << panda::pandasm::AsmEmitter::GetLastError() << std::endl; - return false; } - Logd("Successfully generated: %s\n", output.c_str()); + Logd("Successfully generated: %s\n", outputPath.c_str()); return true; } static bool EmitAndRestoreProgram(panda::pandasm::Program &prog, panda::ts2abc::Options options) { - if (!EmitProgram(g_outputFileName, options.GetOptLevelArg(), options.GetOptLogLevelArg(), prog)) { + if (!EmitProgram(g_outputFileName, options, prog)) { std::cerr << "fail to emit porgram " << g_outputFileName << " in HandleBuffer" << std::endl; return false; } @@ -1572,8 +1572,6 @@ bool GenerateProgram([[maybe_unused]] const std::string &data, const std::string panda::ts2abc::Options options) { bool isParsingFromPipe = options.GetCompileByPipeArg(); - int optLevel = options.GetOptLevelArg(); - std::string optLogLevel = options.GetOptLogLevelArg(); panda::pandasm::Program prog = panda::pandasm::Program(); prog.lang = panda::pandasm::extensions::Language::ECMASCRIPT; @@ -1591,7 +1589,7 @@ bool GenerateProgram([[maybe_unused]] const std::string &data, const std::string Logd("parsing done, calling pandasm\n"); - return EmitProgram(output, optLevel, optLogLevel, prog); + return EmitProgram(output, options, prog); } bool CompileNpmEntries(const std::string &input, const std::string &output)