diff --git a/BUILD.gn b/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..98f9ae49d21746e1d122bb81dacd94d1eb2556aa --- /dev/null +++ b/BUILD.gn @@ -0,0 +1,22 @@ +# Copyright (c) 2021-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. + +import("//arkcompiler/runtime_core/ark_config.gni") +import("//build/ohos.gni") + +group("ets_frontend") { + deps = [ + "./es2panda:es2panda_build", + "./ts2panda:ark_ts2abc_build", + ] +} diff --git a/bundle.json b/bundle.json index e881bff9c346d0407cb6676fb0dc2ca78bf7a0ac..0b9bb4fba46fdbe0fdce667d9427a1f7109eec99 100644 --- a/bundle.json +++ b/bundle.json @@ -18,7 +18,9 @@ "third_party": [] }, "build": { - "sub_component": [], + "sub_component": [ + "//arkcompiler/ets_frontend:ets_frontend" + ], "inner_kits": [], "test": [] } diff --git a/es2panda/BUILD.gn b/es2panda/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..31eda11e1fb5cb994b62b1ab1977499d511a2f89 --- /dev/null +++ b/es2panda/BUILD.gn @@ -0,0 +1,484 @@ +# Copyright (c) 2021-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. + +import("//arkcompiler/ets_frontend/ets_frontend_config.gni") +import("//arkcompiler/runtime_core/ark_config.gni") +import("//build/ohos.gni") + +es2panda_src = [ + "es2panda.cpp", + "binder/binder.cpp", + "binder/declaration.cpp", + "binder/scope.cpp", + "binder/variable.cpp", + "compiler/base/catchTable.cpp", + "compiler/base/condition.cpp", + "compiler/base/destructuring.cpp", + "compiler/base/hoisting.cpp", + "compiler/base/iterators.cpp", + "compiler/base/lexenv.cpp", + "compiler/base/literals.cpp", + "compiler/base/lreference.cpp", + "compiler/core/compileQueue.cpp", + "compiler/core/compilerContext.cpp", + "compiler/core/compilerImpl.cpp", + "compiler/core/dynamicContext.cpp", + "compiler/core/emitter.cpp", + "compiler/core/envScope.cpp", + "compiler/core/function.cpp", + "compiler/core/inlineCache.cpp", + "compiler/core/labelTarget.cpp", + "compiler/core/moduleContext.cpp", + "compiler/core/pandagen.cpp", + "compiler/core/regAllocator.cpp", + "compiler/core/regScope.cpp", + "compiler/core/switchBuilder.cpp", + "compiler/debugger/debuginfoDumper.cpp", + "compiler/function/asyncFunctionBuilder.cpp", + "compiler/function/asyncGeneratorFunctionBuilder.cpp", + "compiler/function/functionBuilder.cpp", + "compiler/function/generatorFunctionBuilder.cpp", + "ir/astDump.cpp", + "ir/base/catchClause.cpp", + "ir/base/classDefinition.cpp", + "ir/base/classProperty.cpp", + "ir/base/decorator.cpp", + "ir/base/metaProperty.cpp", + "ir/base/methodDefinition.cpp", + "ir/base/property.cpp", + "ir/base/scriptFunction.cpp", + "ir/base/spreadElement.cpp", + "ir/base/templateElement.cpp", + "ir/expression.cpp", + "ir/expressions/arrayExpression.cpp", + "ir/expressions/arrowFunctionExpression.cpp", + "ir/expressions/assignmentExpression.cpp", + "ir/expressions/awaitExpression.cpp", + "ir/expressions/binaryExpression.cpp", + "ir/expressions/callExpression.cpp", + "ir/expressions/chainExpression.cpp", + "ir/expressions/classExpression.cpp", + "ir/expressions/conditionalExpression.cpp", + "ir/expressions/functionExpression.cpp", + "ir/expressions/identifier.cpp", + "ir/expressions/importExpression.cpp", + "ir/expressions/literal.cpp", + "ir/expressions/literals/bigIntLiteral.cpp", + "ir/expressions/literals/booleanLiteral.cpp", + "ir/expressions/literals/nullLiteral.cpp", + "ir/expressions/literals/numberLiteral.cpp", + "ir/expressions/literals/regExpLiteral.cpp", + "ir/expressions/literals/stringLiteral.cpp", + "ir/expressions/literals/taggedLiteral.cpp", + "ir/expressions/memberExpression.cpp", + "ir/expressions/newExpression.cpp", + "ir/expressions/objectExpression.cpp", + "ir/expressions/omittedExpression.cpp", + "ir/expressions/sequenceExpression.cpp", + "ir/expressions/superExpression.cpp", + "ir/expressions/taggedTemplateExpression.cpp", + "ir/expressions/templateLiteral.cpp", + "ir/expressions/thisExpression.cpp", + "ir/expressions/unaryExpression.cpp", + "ir/expressions/updateExpression.cpp", + "ir/expressions/yieldExpression.cpp", + "ir/module/exportAllDeclaration.cpp", + "ir/module/exportDefaultDeclaration.cpp", + "ir/module/exportNamedDeclaration.cpp", + "ir/module/exportSpecifier.cpp", + "ir/module/importDeclaration.cpp", + "ir/module/importDefaultSpecifier.cpp", + "ir/module/importNamespaceSpecifier.cpp", + "ir/module/importSpecifier.cpp", + "ir/statement.cpp", + "ir/statements/blockStatement.cpp", + "ir/statements/breakStatement.cpp", + "ir/statements/classDeclaration.cpp", + "ir/statements/continueStatement.cpp", + "ir/statements/debuggerStatement.cpp", + "ir/statements/doWhileStatement.cpp", + "ir/statements/emptyStatement.cpp", + "ir/statements/expressionStatement.cpp", + "ir/statements/forInStatement.cpp", + "ir/statements/forOfStatement.cpp", + "ir/statements/forUpdateStatement.cpp", + "ir/statements/functionDeclaration.cpp", + "ir/statements/ifStatement.cpp", + "ir/statements/labelledStatement.cpp", + "ir/statements/loopStatement.cpp", + "ir/statements/returnStatement.cpp", + "ir/statements/switchCaseStatement.cpp", + "ir/statements/switchStatement.cpp", + "ir/statements/throwStatement.cpp", + "ir/statements/tryStatement.cpp", + "ir/statements/variableDeclaration.cpp", + "ir/statements/variableDeclarator.cpp", + "ir/statements/whileStatement.cpp", + "ir/ts/tsAnyKeyword.cpp", + "ir/ts/tsArrayType.cpp", + "ir/ts/tsAsExpression.cpp", + "ir/ts/tsBigintKeyword.cpp", + "ir/ts/tsBooleanKeyword.cpp", + "ir/ts/tsClassImplements.cpp", + "ir/ts/tsConditionalType.cpp", + "ir/ts/tsConstructorType.cpp", + "ir/ts/tsEnumDeclaration.cpp", + "ir/ts/tsEnumMember.cpp", + "ir/ts/tsExternalModuleReference.cpp", + "ir/ts/tsFunctionType.cpp", + "ir/ts/tsImportEqualsDeclaration.cpp", + "ir/ts/tsImportType.cpp", + "ir/ts/tsIndexSignature.cpp", + "ir/ts/tsIndexedAccessType.cpp", + "ir/ts/tsInferType.cpp", + "ir/ts/tsInterfaceBody.cpp", + "ir/ts/tsInterfaceDeclaration.cpp", + "ir/ts/tsInterfaceHeritage.cpp", + "ir/ts/tsIntersectionType.cpp", + "ir/ts/tsLiteralType.cpp", + "ir/ts/tsMappedType.cpp", + "ir/ts/tsMethodSignature.cpp", + "ir/ts/tsModuleBlock.cpp", + "ir/ts/tsModuleDeclaration.cpp", + "ir/ts/tsNamedTupleMember.cpp", + "ir/ts/tsNeverKeyword.cpp", + "ir/ts/tsNonNullExpression.cpp", + "ir/ts/tsNullKeyword.cpp", + "ir/ts/tsNumberKeyword.cpp", + "ir/ts/tsObjectKeyword.cpp", + "ir/ts/tsParameterProperty.cpp", + "ir/ts/tsParenthesizedType.cpp", + "ir/ts/tsPrivateIdentifier.cpp", + "ir/ts/tsPropertySignature.cpp", + "ir/ts/tsQualifiedName.cpp", + "ir/ts/tsSignatureDeclaration.cpp", + "ir/ts/tsStringKeyword.cpp", + "ir/ts/tsThisType.cpp", + "ir/ts/tsTupleType.cpp", + "ir/ts/tsTypeAliasDeclaration.cpp", + "ir/ts/tsTypeAssertion.cpp", + "ir/ts/tsTypeLiteral.cpp", + "ir/ts/tsTypeOperator.cpp", + "ir/ts/tsTypeParameter.cpp", + "ir/ts/tsTypeParameterDeclaration.cpp", + "ir/ts/tsTypeParameterInstantiation.cpp", + "ir/ts/tsTypePredicate.cpp", + "ir/ts/tsTypeQuery.cpp", + "ir/ts/tsTypeReference.cpp", + "ir/ts/tsUndefinedKeyword.cpp", + "ir/ts/tsUnionType.cpp", + "ir/ts/tsUnknownKeyword.cpp", + "ir/ts/tsVoidKeyword.cpp", + "lexer/keywordsUtil.cpp", + "lexer/lexer.cpp", + "lexer/regexp/regexp.cpp", + "lexer/token/sourceLocation.cpp", + "lexer/token/token.cpp", + "parser/context/parserContext.cpp", + "parser/expressionParser.cpp", + "parser/parserImpl.cpp", + "parser/program/program.cpp", + "parser/statementParser.cpp", + "typescript/checker.cpp", + "typescript/core/binaryLikeExpression.cpp", + "typescript/core/destructuring.cpp", + "typescript/core/function.cpp", + "typescript/core/generics.cpp", + "typescript/core/helpers.cpp", + "typescript/core/object.cpp", + "typescript/core/typeCreation.cpp", + "typescript/core/typeElaboration.cpp", + "typescript/core/typeRelation.cpp", + "typescript/core/util.cpp", + "typescript/types/anyType.cpp", + "typescript/types/arrayType.cpp", + "typescript/types/bigintLiteralType.cpp", + "typescript/types/bigintType.cpp", + "typescript/types/booleanLiteralType.cpp", + "typescript/types/booleanType.cpp", + "typescript/types/constructorType.cpp", + "typescript/types/enumLiteralType.cpp", + "typescript/types/enumType.cpp", + "typescript/types/functionType.cpp", + "typescript/types/globalTypesHolder.cpp", + "typescript/types/indexInfo.cpp", + "typescript/types/interfaceType.cpp", + "typescript/types/neverType.cpp", + "typescript/types/nonPrimitiveType.cpp", + "typescript/types/nullType.cpp", + "typescript/types/numberLiteralType.cpp", + "typescript/types/numberType.cpp", + "typescript/types/objectDescriptor.cpp", + "typescript/types/objectLiteralType.cpp", + "typescript/types/objectType.cpp", + "typescript/types/signature.cpp", + "typescript/types/stringLiteralType.cpp", + "typescript/types/stringType.cpp", + "typescript/types/tupleType.cpp", + "typescript/types/type.cpp", + "typescript/types/typeParameter.cpp", + "typescript/types/typeReference.cpp", + "typescript/types/typeRelation.cpp", + "typescript/types/undefinedType.cpp", + "typescript/types/unionType.cpp", + "typescript/types/unknownType.cpp", + "typescript/types/voidType.cpp", + "util/bitset.cpp", + "util/helpers.cpp", + "util/ustring.cpp", +] + +config("es2abc_config_src") { + include_dirs = [ + "./", + "./binder", + "./util", + "./compiler/base", + "./compiler/core", + "./compiler/debugger", + "./compiler/function", + "./ir", + "./ir/base", + "./ir/expressions", + "./ir/module", + "./ir/statements", + "./ir/ts", + "./parser", + "./parser/context", + "./lexer", + "./lexer/token", + "./lexer/regexp", + "./typescript", + "./typescript/types", + ] + + cflags = [ "-fexceptions" ] +} + +config("es2abc_config_common") { + configs = [ + "$ark_root:ark_config", + "$sdk_libc_secshared_config", + "$ark_root/libpandafile:arkfile_public_config", + "$ark_root/libpandabase:arkbase_public_config", + "$ark_root/runtime:arkruntime_public_config", + "$ark_root/assembler:arkassembler_public_config", + ":es2abc_config_src", + ] + + if (enable_bytecode_optimizer) { + defines = [ "ENABLE_BYTECODE_OPT" ] + configs += [ + "$ark_root/compiler:arkcompiler_public_config", + "$ark_root/bytecode_optimizer:bytecodeopt_public_config", + ] + } +} + +# generate headers for es2panda parser +generated_header_dir = "${target_out_dir}/gen" +action_foreach("gen_keywords_headers") { + template_files = [ + "keywords.h.erb", + "keywordsMap.h.erb", + ] + keywords_templates_dir = "./lexer/templates" + keywords_generator = "./lexer/scripts/keywords.rb" + + sources = [] + foreach(file, template_files) { + sources += [ rebase_path("${keywords_templates_dir}/${file}") ] + } + outputs = [ "${generated_header_dir}/{{source_name_part}}" ] + + script = "scripts/gen_keywords.sh" + inputs = [ "${keywords_generator}" ] + args = [ + "-g", + rebase_path("${keywords_generator}"), + "-t", + "{{source}}", + "-o", + "{{source_name_part}}", + "-d", + rebase_path("${generated_header_dir}"), + ] +} + +# generate headers for es2panda compiler +isa_gen = "${ark_root}/isa/gen.rb" +isa_gen_data = "$root_gen_dir/isa/isa.yaml" +isa_gen_require = "${ark_root}/isa/isapi.rb" +action_foreach("gen_isa_headers") { + deps = [ "//arkcompiler/runtime_core/isa:isa_combine" ] + + template_files = [ + "isa.h.erb", + "formats.h.erb", + ] + keywords_templates_dir = "./compiler/templates" + + sources = [] + foreach(file, template_files) { + sources += [ rebase_path("${keywords_templates_dir}/${file}") ] + } + outputs = [ "${generated_header_dir}/{{source_name_part}}" ] + + script = "scripts/gen_isa.sh" + inputs = [ + "${isa_gen}", + "${isa_gen_data}", + "${isa_gen_require}", + ] + args = [ + "-g", + rebase_path("${isa_gen}"), + "-t", + "{{source}}", + "-a", + rebase_path("${isa_gen_data}"), + "-o", + "{{source_name_part}}", + "-d", + rebase_path("${generated_header_dir}"), + "-r", + rebase_path("${isa_gen_require}"), + ] +} + +ohos_static_library("es2panda_lib") { + sources = es2panda_src + use_exceptions = true + + configs = [ ":es2abc_config_common" ] + + include_dirs = [ + "${target_out_dir}", + "//third_party/icu/icu4c/source/common", + "//third_party/icu/icu4c/source/i18n", + "//third_party/icu/icu4c/source ", + ] + + deps = [ + ":gen_isa_headers", + ":gen_keywords_headers", + "$ark_root/assembler:libarkassembler_frontend_static", + "$ark_root/libpandabase:libarkbase_frontend_static", + "$ark_root/libpandafile:libarkfile_frontend_static", + "$ark_root/libziparchive:libarkziparchive_frontend_static", + "//third_party/icu/icu4c:static_icuuc", + ] + + cflags = [ "-Wno-implicit-fallthrough" ] +} + +ohos_executable("es2panda") { + use_exceptions = true + sources = [ + "aot/main.cpp", + "aot/options.cpp", + ] + + include_dirs = [ "./aot" ] + + configs = [ ":es2abc_config_common" ] + + deps = [ ":es2panda_lib" ] + + if (is_linux) { + if (build_public_version) { + ldflags = [ "-static-libstdc++" ] + } else { + libs = [ libcpp_static_lib ] + } + } + + if (enable_bytecode_optimizer) { + deps += [ + "$ark_root/bytecode_optimizer:libarkbytecodeopt_frontend_static", + "$ark_root/compiler:libarkcompiler_frontend_static", + ] + } + + output_name = "es2abc" + install_enable = true + subsystem_name = "ark" +} + +if (is_linux) { + ohos_copy("es2abc_build") { + deps = [ ":es2panda" ] + sources = [ "${root_out_dir}/ark/ark/es2abc" ] + outputs = [ "${target_out_dir}/es2abc-tmp" ] + module_source_dir = "${root_out_dir}/ark/ark" + module_install_name = "es2abc" + } + + ohos_copy("es2abc_build_ets") { + deps = [ ":es2panda" ] + sources = [ "${root_out_dir}/ark/ark/es2abc" ] + outputs = [ "${root_out_dir}/ark/ark/es2abc-ets" ] + module_source_dir = "${root_out_dir}/ark/ark" + module_install_name = "es2abc" + } +} + +if (is_mingw) { + ohos_copy("es2abc_build_win") { + deps = [ ":es2panda" ] + sources = [ "${root_out_dir}/ark/ark/es2abc.exe" ] + outputs = [ "${target_out_dir}/es2abc-tmp" ] + module_source_dir = "${root_out_dir}/ark/ark" + module_install_name = "es2abc.exe" + } + + ohos_copy("es2abc_build_win_ets") { + deps = [ ":es2panda" ] + sources = [ "${root_out_dir}/ark/ark/es2abc.exe" ] + outputs = [ "${root_out_dir}/ark/ark/es2abc-win-ets" ] + module_source_dir = "${root_out_dir}/ark/ark" + module_install_name = "es2abc.exe" + } +} + +if (is_mac) { + ohos_copy("es2abc_build_mac") { + deps = [ ":es2panda" ] + sources = [ "${root_out_dir}/ark/ark/es2abc" ] + outputs = [ "${target_out_dir}/es2abc-tmp" ] + module_source_dir = "${root_out_dir}/ark/ark" + module_install_name = "es2abc" + } + + ohos_copy("es2abc_build_mac_ets") { + deps = [ ":es2panda" ] + sources = [ "${root_out_dir}/ark/ark/es2abc" ] + outputs = [ "${root_out_dir}/ark/ark/es2abc-mac-ets" ] + module_source_dir = "${root_out_dir}/ark/ark" + module_install_name = "es2abc" + } +} + +group("es2panda_build") { + if (host_os == "linux") { + deps = [ ":es2panda(${toolchain_linux})" ] + } + + if (host_os == "mac") { + deps = [ ":es2panda(${toolchain_mac})" ] + } +} + +group("es2panda_build_win") { + deps = [ ":es2panda(${toolchain_win})" ] +} diff --git a/es2panda/aot/main.cpp b/es2panda/aot/main.cpp index 8dbd680977fa6e901ca1e41d7f6dbc8235458125..ad0dde48b47004dd97d13f6bbc7eb2125990f32c 100644 --- a/es2panda/aot/main.cpp +++ b/es2panda/aot/main.cpp @@ -14,12 +14,18 @@ */ #include +#ifdef ENABLE_BYTECODE_OPT #include #include +#else +#include +#include +#include +#endif #include #include #include -#include +#include #include #include diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index 9be3f001c5b1f2f8a6c5e7e5cd796649fe38f70d..9f72eac598d446a9f0b38324db70539060e0ed72 100644 --- a/es2panda/aot/options.cpp +++ b/es2panda/aot/options.cpp @@ -18,6 +18,7 @@ #include #include +#include namespace panda::es2panda::aot { diff --git a/es2panda/aot/options.h b/es2panda/aot/options.h index af8c8e3093a40394bd8679a28407a28d4dd16708..9b681e7f11c393c02d38bcb12ad22715e34947db 100644 --- a/es2panda/aot/options.h +++ b/es2panda/aot/options.h @@ -17,7 +17,7 @@ #define ES2PANDA_AOT_OPTIONS_H #include -#include +#include #include #include diff --git a/es2panda/compiler/base/condition.cpp b/es2panda/compiler/base/condition.cpp index 644e9195fecef0aaa030ac05f26f19ebe9c77636..577d8c63e49a2a67b455939e124e1c9b00346dde 100644 --- a/es2panda/compiler/base/condition.cpp +++ b/es2panda/compiler/base/condition.cpp @@ -51,11 +51,9 @@ void Condition::Compile(PandaGen *pg, const ir::Expression *expr, Label *falseLa } case lexer::TokenType::PUNCTUATOR_LOGICAL_AND: { binExpr->Left()->Compile(pg); - pg->ToBoolean(binExpr); pg->BranchIfFalse(binExpr, falseLabel); binExpr->Right()->Compile(pg); - pg->ToBoolean(binExpr); pg->BranchIfFalse(binExpr, falseLabel); return; } @@ -63,11 +61,9 @@ void Condition::Compile(PandaGen *pg, const ir::Expression *expr, Label *falseLa auto *endLabel = pg->AllocLabel(); binExpr->Left()->Compile(pg); - pg->ToBoolean(binExpr); pg->BranchIfTrue(binExpr, endLabel); binExpr->Right()->Compile(pg); - pg->ToBoolean(binExpr); pg->BranchIfFalse(binExpr, falseLabel); pg->SetLabel(binExpr, endLabel); return; @@ -87,7 +83,6 @@ void Condition::Compile(PandaGen *pg, const ir::Expression *expr, Label *falseLa // General case including some binExpr i.E.(a+b) expr->Compile(pg); - pg->ToBoolean(expr); pg->BranchIfFalse(expr, falseLabel); } diff --git a/es2panda/compiler/base/destructuring.cpp b/es2panda/compiler/base/destructuring.cpp index 7ff756529f3b4f1b528f3245a35b19636af46963..70d9ef0754d749ed9ca0aee4dbe9c1fd9857bf7c 100644 --- a/es2panda/compiler/base/destructuring.cpp +++ b/es2panda/compiler/base/destructuring.cpp @@ -71,7 +71,6 @@ static void GenRestElement(PandaGen *pg, const ir::SpreadElement *restElement, static void GenArray(PandaGen *pg, const ir::ArrayExpression *array) { - // RegScope rs(pg); DestructuringIterator iterator(pg, array); if (array->Elements().empty()) { @@ -223,9 +222,15 @@ static void GenObject(PandaGen *pg, const ir::ObjectExpression *object, VReg rhs if (properties.empty() || properties.back()->IsRestElement()) { auto *notNullish = pg->AllocLabel(); + auto *nullish = pg->AllocLabel(); - pg->LoadAccumulator(object, rhs); - pg->BranchIfCoercible(object, notNullish); + pg->LoadConst(object, Constant::JS_NULL); + pg->Condition(object, lexer::TokenType::PUNCTUATOR_NOT_STRICT_EQUAL, rhs, nullish); + pg->LoadConst(object, Constant::JS_UNDEFINED); + pg->Condition(object, lexer::TokenType::PUNCTUATOR_NOT_STRICT_EQUAL, rhs, nullish); + pg->Branch(object, notNullish); + + pg->SetLabel(object, nullish); pg->ThrowObjectNonCoercible(object); pg->SetLabel(object, notNullish); diff --git a/es2panda/compiler/base/iterators.cpp b/es2panda/compiler/base/iterators.cpp index 1bf7ad888797b1fb5f2c829aab527b4979a09c8a..8ba4d12a112babd5be5ced988353548bac097703 100644 --- a/es2panda/compiler/base/iterators.cpp +++ b/es2panda/compiler/base/iterators.cpp @@ -70,7 +70,6 @@ void Iterator::Next() const void Iterator::Complete() const { pg_->LoadObjByName(node_, nextResult_, "done"); - pg_->ToBoolean(node_); } void Iterator::Value() const diff --git a/es2panda/compiler/core/emitter.cpp b/es2panda/compiler/core/emitter.cpp index 6e617cb41f196d3b1ae3fdc361eed462f28af155..ac04127360e7dd7c2b6bba3e29f049b18963d528 100644 --- a/es2panda/compiler/core/emitter.cpp +++ b/es2panda/compiler/core/emitter.cpp @@ -75,7 +75,8 @@ void FunctionEmitter::GenBufferLiterals(const LiteralBuffer *buff) { auto &[idx, array] = literalBuffers_.emplace_back(); idx = buff->Index(); - array.reserve(buff->Literals().size() * 2); + constexpr size_t ARRAY_EXPANSION = 2; + array.reserve(buff->Literals().size() * ARRAY_EXPANSION); for (const auto *literal : buff->Literals()) { panda::pandasm::LiteralArray::Literal valueLit; @@ -119,11 +120,7 @@ void FunctionEmitter::GenBufferLiterals(const LiteralBuffer *buff) valueLit.value_ = literal->GetMethod().Mutf8(); break; } - case ir::LiteralTag::ASYNC_GENERATOR_METHOD: { - valueLit.tag_ = panda::panda_file::LiteralTag::ASYNCGENERATORMETHOD; - valueLit.value_ = literal->GetMethod().Mutf8(); - break; - } + // TODO: support ir::LiteralTag::ASYNC_GENERATOR_METHOD case ir::LiteralTag::NULL_VALUE: { valueLit.tag_ = panda::panda_file::LiteralTag::NULLVALUE; valueLit.value_ = static_cast(0); @@ -181,10 +178,11 @@ static size_t GetIRNodeWholeLength(const IRNode *node) } size_t len = 1; + constexpr size_t BIT_WIDTH = 8; const auto format = MatchFormat(node, formats); for (auto fi : format.GetFormatItem()) { - len += fi.Bitwidth() / 8; + len += fi.Bitwidth() / BIT_WIDTH; } return len; diff --git a/es2panda/compiler/core/pandagen.cpp b/es2panda/compiler/core/pandagen.cpp index 7d58fa71c45b4622695a76884a4c7035854e2eac..7cf1bb96f5a871e58b477d4779e57ce2b7ebc416 100644 --- a/es2panda/compiler/core/pandagen.cpp +++ b/es2panda/compiler/core/pandagen.cpp @@ -415,7 +415,7 @@ void PandaGen::StoreGlobalVar(const ir::AstNode *node, const util::StringView &n void PandaGen::StoreGlobalLet(const ir::AstNode *node, const util::StringView &name) { - sa_.Emit(node, name); + sa_.Emit(node, name); strings_.insert(name); } @@ -646,7 +646,7 @@ void PandaGen::Unary(const ir::AstNode *node, lexer::TokenType op, VReg operand) break; } case lexer::TokenType::PUNCTUATOR_EXCLAMATION_MARK: { - sa_.Emit(node); + Negate(node); break; } case lexer::TokenType::PUNCTUATOR_PLUS_PLUS: { @@ -784,19 +784,27 @@ void PandaGen::Binary(const ir::AstNode *node, lexer::TokenType op, VReg lhs) void PandaGen::BranchIfUndefined(const ir::AstNode *node, Label *target) { - sa_.Emit(node); - BranchIfTrue(node, target); + RegScope rs(this); + VReg tmp = AllocReg(); + StoreAccumulator(node, tmp); + sa_.Emit(node); + ra_.Emit(node, tmp); + sa_.Emit(node, target); } void PandaGen::BranchIfNotUndefined(const ir::AstNode *node, Label *target) { - sa_.Emit(node); - BranchIfFalse(node, target); + RegScope rs(this); + VReg tmp = AllocReg(); + StoreAccumulator(node, tmp); + sa_.Emit(node); + ra_.Emit(node, tmp); + sa_.Emit(node, target); } void PandaGen::BranchIfTrue(const ir::AstNode *node, Label *target) { - sa_.Emit(node, target); + sa_.Emit(node, target); } void PandaGen::BranchIfNotTrue(const ir::AstNode *node, Label *target) @@ -807,13 +815,7 @@ void PandaGen::BranchIfNotTrue(const ir::AstNode *node, Label *target) void PandaGen::BranchIfFalse(const ir::AstNode *node, Label *target) { - sa_.Emit(node, target); -} - -void PandaGen::BranchIfCoercible(const ir::AstNode *node, Label *target) -{ - sa_.Emit(node); - BranchIfTrue(node, target); + sa_.Emit(node, target); } void PandaGen::EmitThrow(const ir::AstNode *node) @@ -823,12 +825,12 @@ void PandaGen::EmitThrow(const ir::AstNode *node) void PandaGen::EmitRethrow(const ir::AstNode *node) { - sa_.Emit(node); + // TODO: rethrow in try-catch } void PandaGen::EmitReturn(const ir::AstNode *node) { - sa_.Emit(node); + sa_.Emit(node); } void PandaGen::EmitReturnUndefined(const ir::AstNode *node) @@ -888,25 +890,25 @@ void PandaGen::Call(const ir::AstNode *node, VReg startReg, size_t argCount) switch (argCount) { case 0: { // 0 args - ra_.Emit(node, callee); + ra_.Emit(node, callee); break; } case 1: { // 1 arg VReg arg0 = callee + 1; - ra_.Emit(node, callee, arg0); + ra_.Emit(node, callee, arg0); break; } case 2: { // 2 args VReg arg0 = callee + 1; VReg arg1 = arg0 + 1; - ra_.Emit(node, callee, arg0, arg1); + ra_.Emit(node, callee, arg0, arg1); break; } case 3: { // 3 args VReg arg0 = callee + 1; VReg arg1 = arg0 + 1; VReg arg2 = arg1 + 1; - ra_.Emit(node, callee, arg0, arg1, arg2); + ra_.Emit(node, callee, arg0, arg1, arg2); break; } default: { @@ -938,21 +940,22 @@ void PandaGen::LoadHomeObject(const ir::AstNode *node) void PandaGen::DefineFunction(const ir::AstNode *node, const ir::ScriptFunction *realNode, const util::StringView &name) { + auto formalParamCnt = FormalParametersCount(); if (realNode->IsAsync()) { if (realNode->IsGenerator()) { - ra_.Emit(node, name, LexEnv()); + // TODO(): async generator } else { - ra_.Emit(node, name, LexEnv()); + ra_.Emit(node, name, static_cast(formalParamCnt), LexEnv()); } } else if (realNode->IsGenerator()) { - ra_.Emit(node, name, LexEnv()); + ra_.Emit(node, name, static_cast(formalParamCnt), LexEnv()); } else if (realNode->IsArrow()) { LoadHomeObject(node); - ra_.Emit(node, name, LexEnv()); + ra_.Emit(node, name, static_cast(formalParamCnt), LexEnv()); } else if (realNode->IsMethod()) { - ra_.Emit(node, name, LexEnv()); + ra_.Emit(node, name, static_cast(formalParamCnt), LexEnv()); } else { - ra_.Emit(node, name, LexEnv()); + ra_.Emit(node, name, static_cast(formalParamCnt), LexEnv()); } strings_.insert(name); @@ -980,12 +983,14 @@ void PandaGen::GetUnmappedArgs(const ir::AstNode *node) void PandaGen::Negate(const ir::AstNode *node) { - sa_.Emit(node); -} - -void PandaGen::ToBoolean(const ir::AstNode *node) -{ - sa_.Emit(node); + auto *falseLabel = AllocLabel(); + auto *endLabel = AllocLabel(); + BranchIfTrue(node, falseLabel); + LoadConst(node, Constant::JS_TRUE); + Branch(node, endLabel); + SetLabel(node, falseLabel); + LoadConst(node, Constant::JS_FALSE); + SetLabel(node, endLabel); } void PandaGen::ToNumber(const ir::AstNode *node, VReg arg) @@ -995,8 +1000,11 @@ void PandaGen::ToNumber(const ir::AstNode *node, VReg arg) void PandaGen::GetMethod(const ir::AstNode *node, VReg obj, const util::StringView &name) { - ra_.Emit(node, name, obj); - strings_.insert(name); + /** + * TODO + * ra_.Emit(node, name, obj); + * strings_.insert(name); + */ } void PandaGen::CreateGeneratorObj(const ir::AstNode *node, VReg funcObj) @@ -1006,32 +1014,50 @@ void PandaGen::CreateGeneratorObj(const ir::AstNode *node, VReg funcObj) void PandaGen::CreateAsyncGeneratorObj(const ir::AstNode *node, VReg funcObj) { - ra_.Emit(node, funcObj); + /* + * TODO: async generator + * ra_.Emit(node, funcObj); + */ } void PandaGen::CreateIterResultObject(const ir::AstNode *node, bool done) { - ra_.Emit(node, static_cast(done)); + /* + * TODO: create iter result + * ra_.Emit(node, static_cast(done)); + */ } void PandaGen::SuspendGenerator(const ir::AstNode *node, VReg genObj) { - ra_.Emit(node, genObj); + /* + * TODO: suspend generator + * ra_.Emit(node, genObj); + */ } void PandaGen::SuspendAsyncGenerator(const ir::AstNode *node, VReg asyncGenObj) { - ra_.Emit(node, asyncGenObj); + /* + * TODO: suspend async generator + * ra_.Emit(node, asyncGenObj); + */ } void PandaGen::GeneratorYield(const ir::AstNode *node, VReg genObj) { - ra_.Emit(node, genObj, static_cast(GeneratorState::SUSPENDED_YIELD)); + /* + * TODO: set generator yield + * ra_.Emit(node, genObj, static_cast(GeneratorState::SUSPENDED_YIELD)); + */ } void PandaGen::GeneratorComplete(const ir::AstNode *node, VReg genObj) { - ra_.Emit(node, genObj, static_cast(GeneratorState::COMPLETED)); + /* + * TODO: set generator complete + * ra_.Emit(node, genObj, static_cast(GeneratorState::COMPLETED)); + */ } void PandaGen::ResumeGenerator(const ir::AstNode *node, VReg genObj) @@ -1051,27 +1077,42 @@ void PandaGen::AsyncFunctionEnter(const ir::AstNode *node) void PandaGen::AsyncFunctionAwait(const ir::AstNode *node, VReg asyncFuncObj) { - ra_.Emit(node, asyncFuncObj); + /* + * TODO: async function await + * ra_.Emit(node, asyncFuncObj); + */ } void PandaGen::AsyncFunctionResolve(const ir::AstNode *node, VReg asyncFuncObj) { - ra_.Emit(node, asyncFuncObj); + /* + * TODO: async function resolve + * ra_.Emit(node, asyncFuncObj); + */ } void PandaGen::AsyncFunctionReject(const ir::AstNode *node, VReg asyncFuncObj) { - ra_.Emit(node, asyncFuncObj); + /* + * TODO: async function reject + * ra_.Emit(node, asyncFuncObj); + */ } void PandaGen::AsyncGeneratorResolve(const ir::AstNode *node, VReg asyncGenObj) { - ra_.Emit(node, asyncGenObj); + /* + * TODO: async generator resolve + * ra_.Emit(node, asyncGenObj); + */ } void PandaGen::AsyncGeneratorReject(const ir::AstNode *node, VReg asyncGenObj) { - ra_.Emit(node, asyncGenObj); + /* + * TODO: async generator reject + * ra_.Emit(node, asyncGenObj); + */ } void PandaGen::GetTemplateObject(const ir::AstNode *node, VReg value) @@ -1243,7 +1284,10 @@ void PandaGen::StoreArraySpread(const ir::AstNode *node, VReg array, VReg index) void PandaGen::ThrowIfNotObject(const ir::AstNode *node) { - ra_.Emit(node); + // TODO: implement this method correctly + RegScope rs(this); + VReg value = AllocReg(); + ra_.Emit(node, value); } void PandaGen::ThrowThrowNotExist(const ir::AstNode *node) @@ -1258,7 +1302,10 @@ void PandaGen::GetIterator(const ir::AstNode *node) void PandaGen::GetAsyncIterator(const ir::AstNode *node) { - sa_.Emit(node); + /* + * TODO: async iterator + * sa_.Emit(node); + */ } void PandaGen::CreateObjectWithExcludedKeys(const ir::AstNode *node, VReg obj, VReg argStart, size_t argCount) @@ -1284,21 +1331,28 @@ void PandaGen::CloseIterator(const ir::AstNode *node, VReg iter) void PandaGen::ImportModule(const ir::AstNode *node, const util::StringView &name) { - sa_.Emit(node, name); - strings_.insert(name); + /* + * TODO: module + * sa_.Emit(node, name); + * strings_.insert(name); + */ } void PandaGen::DefineClassWithBuffer(const ir::AstNode *node, const util::StringView &ctorId, int32_t litIdx, VReg lexenv, VReg base) { - ra_.Emit(node, ctorId, litIdx, lexenv, base); + auto formalParamCnt = FormalParametersCount(); + ra_.Emit(node, ctorId, litIdx, static_cast(formalParamCnt), lexenv, base); strings_.insert(ctorId); } void PandaGen::LoadModuleVariable(const ir::AstNode *node, VReg module, const util::StringView &name) { - ra_.Emit(node, name, module); - strings_.insert(name); + /* + * TODO: module + * ra_.Emit(node, name, module); + * strings_.insert(name); + */ } void PandaGen::StoreModuleVar(const ir::AstNode *node, const util::StringView &name) @@ -1386,7 +1440,11 @@ void PandaGen::LoadLexicalVar(const ir::AstNode *node, uint32_t level, uint32_t void PandaGen::StoreLexicalVar(const ir::AstNode *node, uint32_t level, uint32_t slot) { - ra_.Emit(node, level, slot); + // TODO: need to reconsider this part + RegScope rs(this); + VReg value = AllocReg(); + StoreAccumulator(node, value); + ra_.Emit(node, level, slot, value); } void PandaGen::ThrowIfSuperNotCorrectCall(const ir::AstNode *node, int64_t num) @@ -1396,13 +1454,23 @@ void PandaGen::ThrowIfSuperNotCorrectCall(const ir::AstNode *node, int64_t num) void PandaGen::ThrowUndefinedIfHole(const ir::AstNode *node, const util::StringView &name) { - ra_.Emit(node, name); + RegScope rs(this); + VReg holeReg = AllocReg(); + StoreAccumulator(node, holeReg); + LoadAccumulatorString(node, name); + VReg nameReg = AllocReg(); + StoreAccumulator(node, nameReg); + ra_.Emit(node, holeReg, nameReg); strings_.insert(name); } void PandaGen::ThrowConstAssignment(const ir::AstNode *node, const util::StringView &name) { - ra_.Emit(node, name); + RegScope rs(this); + LoadAccumulatorString(node, name); + VReg nameReg = AllocReg(); + StoreAccumulator(node, nameReg); + ra_.Emit(node, nameReg); strings_.insert(name); } @@ -1413,7 +1481,10 @@ void PandaGen::PopLexEnv(const ir::AstNode *node) void PandaGen::CopyLexEnv(const ir::AstNode *node) { - sa_.Emit(node); + /* + * TODO: copy lexenv + * sa_.Emit(node); + */ } void PandaGen::NewLexEnv(const ir::AstNode *node, uint32_t num) diff --git a/es2panda/compiler/core/pandagen.h b/es2panda/compiler/core/pandagen.h index eefd2e4b24180c09cf99b988a6a7f24cd2927186..77d127c423840d623295924523c4544345361cbb 100644 --- a/es2panda/compiler/core/pandagen.h +++ b/es2panda/compiler/core/pandagen.h @@ -255,7 +255,6 @@ public: void BranchIfTrue(const ir::AstNode *node, class Label *target); void BranchIfNotTrue(const ir::AstNode *node, class Label *target); void BranchIfFalse(const ir::AstNode *node, class Label *target); - void BranchIfCoercible(const ir::AstNode *node, class Label *target); void EmitThrow(const ir::AstNode *node); void EmitRethrow(const ir::AstNode *node); @@ -281,7 +280,6 @@ public: void GetUnmappedArgs(const ir::AstNode *node); void Negate(const ir::AstNode *node); - void ToBoolean(const ir::AstNode *node); void ToNumber(const ir::AstNode *node, VReg arg); void CreateGeneratorObj(const ir::AstNode *node, VReg funcObj); diff --git a/es2panda/es2abc_config.gni b/es2panda/es2abc_config.gni new file mode 100644 index 0000000000000000000000000000000000000000..008466806f5076e43604f814f5fece83d3b0c401 --- /dev/null +++ b/es2panda/es2abc_config.gni @@ -0,0 +1,91 @@ +# 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. + +import("//arkcompiler/ets_frontend/ets_frontend_config.gni") +if (defined(ark_independent_build)) { + import("$build_root/ark.gni") + es2abc_root = "//ets_frontend/es2panda" +} else { + import("//build/ohos.gni") + build_root = "//build" + es2abc_root = "//arkcompiler/ets_frontend/es2panda" +} + +es2abc_build_path = "" +es2abc_build_deps = "" +es2abc_out_root = "" + +if (host_toolchain == toolchain_mac) { + es2abc_out_root = + get_label_info("$es2abc_root:es2panda($toolchain_mac)", "root_out_dir") + es2abc_build_deps = [ "$es2abc_root:es2panda($toolchain_mac)" ] +} else if (host_toolchain == toolchain_win) { + es2abc_out_root = + get_label_info("$es2abc_root:es2panda($toolchain_win)", "root_out_dir") + es2abc_build_deps = [ "$es2abc_root:es2panda($toolchain_win)" ] +} else { + es2abc_out_root = + get_label_info("$es2abc_root:es2panda($toolchain_linux)", "root_out_dir") + es2abc_build_deps = [ "$es2abc_root:es2panda($toolchain_linux)" ] +} +es2abc_build_path = es2abc_out_root + "/ark/ark" + +# Generate abc. +# +# Mandatory arguments: +# plugin_path -- plugin js file path +# plugin_name -- name of js file, ex: BatteryPlugin.js +# generat_file -- name of generated file +# package_name -- name of generated file's package +# extra_dependencies -- a list of files that should be considered as dependencies, must be label +# out_puts +template("es2abc_gen_abc") { + assert(defined(invoker.src_js), "src_js is required!") + assert(defined(invoker.dst_file), "dst_file is required!") + assert(defined(invoker.out_puts), "out_puts is required!") + + extra_dependencies = [] + if (defined(invoker.extra_dependencies)) { + extra_dependencies += invoker.extra_dependencies + } + + action("$target_name") { + if (defined(invoker.extra_visibility)) { + visibility = invoker.extra_visibility + } + + script = "${es2abc_root}/scripts/generate_js_bytecode.py" + + deps = extra_dependencies + deps += es2abc_build_deps + + args = [ + "--src-js", + invoker.src_js, + "--dst-file", + invoker.dst_file, + "--frontend-tool-path", + rebase_path("${es2abc_build_path}"), + ] + + if (defined(invoker.extra_args)) { + args += invoker.extra_args + } + + if (defined(invoker.in_puts)) { + inputs = invoker.in_puts + } + + outputs = invoker.out_puts + } +} diff --git a/es2panda/ir/expressions/binaryExpression.cpp b/es2panda/ir/expressions/binaryExpression.cpp index 7423a2dfb2b77762dd586c92af91a76d97420630..5621dfe8e527df72cfb90a74986ce8464631a8d4 100644 --- a/es2panda/ir/expressions/binaryExpression.cpp +++ b/es2panda/ir/expressions/binaryExpression.cpp @@ -56,7 +56,6 @@ void BinaryExpression::CompileLogical(compiler::PandaGen *pg) const // left -> acc -> lhs -> toboolean -> acc -> bool_lhs left_->Compile(pg); pg->StoreAccumulator(this, lhs); - pg->ToBoolean(this); if (operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_AND) { pg->BranchIfFalse(this, skipRight); diff --git a/es2panda/ir/expressions/chainExpression.cpp b/es2panda/ir/expressions/chainExpression.cpp index 0869146030a2790ec4ab1515f6a239faffe2a89e..16e2b3a29991f70cf1ba27f16a050a5a195fcf9e 100644 --- a/es2panda/ir/expressions/chainExpression.cpp +++ b/es2panda/ir/expressions/chainExpression.cpp @@ -1,4 +1,4 @@ -/** +/* * Copyright (c) 2021-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. diff --git a/es2panda/scripts/gen_isa.sh b/es2panda/scripts/gen_isa.sh new file mode 100755 index 0000000000000000000000000000000000000000..28ab703f1b0856e7efe47de9712beceff7e0d30c --- /dev/null +++ b/es2panda/scripts/gen_isa.sh @@ -0,0 +1,52 @@ +#!/bin/bash +# 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. + +set -e + +while getopts "g:t:a:o:d:r:h" arg +do + case "${arg}" in + "g") + GENERATOR=${OPTARG} + ;; + "t") + TEMPLATE=${OPTARG} + ;; + "a") + DATA=${OPTARG} + ;; + "o") + OUTPUT=${OPTARG} + ;; + "d") + OUTDIR=${OPTARG} + ;; + "r") + REQUIRE=${OPTARG} + ;; + "h") + echo "help" + ;; + ?) + echo "unkonw argument" + exit 1 + ;; + esac +done + +if [ ! -d ${OUTDIR} ]; then + mkdir -p ${OUTDIR} +fi +echo "${GENERATOR} --template ${TEMPLATE} --data ${DATA} --output ${OUTDIR}/${OUTPUT} --require ${REQUIRE}" +${GENERATOR} --template ${TEMPLATE} --data ${DATA} --output ${OUTDIR}/${OUTPUT} --require ${REQUIRE} diff --git a/es2panda/scripts/gen_keywords.sh b/es2panda/scripts/gen_keywords.sh new file mode 100755 index 0000000000000000000000000000000000000000..43f4069f464845de95f1f72ed85e44f8e22c908f --- /dev/null +++ b/es2panda/scripts/gen_keywords.sh @@ -0,0 +1,46 @@ +#!/bin/bash +# 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. + +set -e + +while getopts "g:t:o:d:h" arg +do + case "${arg}" in + "g") + GENERATOR=${OPTARG} + ;; + "t") + TEMPLATE=${OPTARG} + ;; + "o") + OUTPUT=${OPTARG} + ;; + "d") + OUTDIR=${OPTARG} + ;; + "h") + echo "help" + ;; + ?) + echo "unkonw argument" + exit 1 + ;; + esac +done + +if [ ! -d ${OUTDIR} ]; then + mkdir -p ${OUTDIR} +fi + +ruby ${GENERATOR} ${TEMPLATE} ${OUTDIR}/${OUTPUT} diff --git a/es2panda/scripts/generate_js_bytecode.py b/es2panda/scripts/generate_js_bytecode.py new file mode 100755 index 0000000000000000000000000000000000000000..f3afc4dfc80bba611baf18c3a1627e661080be8d --- /dev/null +++ b/es2panda/scripts/generate_js_bytecode.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +# coding: utf-8 + +""" +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. + +Description: Generate javascript byte code using es2abc +""" + +import os +import subprocess +import platform +import argparse + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument('--src-js', + help = 'js source file') + parser.add_argument('--dst-file', + help = 'the converted target file') + parser.add_argument('--frontend-tool-path', + help = 'path of the frontend conversion tool') + parser.add_argument("--debug", action='store_true', + help='whether add debuginfo') + parser.add_argument("--module", action='store_true', + help='whether is module') + parser.add_argument("--commonjs", action='store_true', + help='whether is commonjs') + arguments = parser.parse_args() + return arguments + +def run_command(cmd, execution_path): + print(" ".join(cmd) + " | execution_path: " + execution_path) + proc = subprocess.Popen(cmd, cwd=execution_path) + proc.wait() + + +def gen_abc_info(input_arguments): + frontend_tool_path = input_arguments.frontend_tool_path + + (path, name) = os.path.split(frontend_tool_path) + + cmd = [os.path.join("./", name, "es2abc"), + '--output', input_arguments.dst_file, + input_arguments.src_js] + + if input_arguments.debug: + src_index = cmd.index(input_arguments.src_js) + cmd.insert(src_index, '--debug-info') + if input_arguments.module: + src_index = cmd.index(input_arguments.src_js) + cmd.insert(src_index, '--module') + if input_arguments.commonjs: + src_index = cmd.index(input_arguments.src_js) + # insert commonjs option to cmd later + run_command(cmd, path) + + +if __name__ == '__main__': + gen_abc_info(parse_args()) \ No newline at end of file diff --git a/ets_frontend_config.gni b/ets_frontend_config.gni new file mode 100644 index 0000000000000000000000000000000000000000..b4a3c1928a180a9b5d115404362e4f3fb50ec4b0 --- /dev/null +++ b/ets_frontend_config.gni @@ -0,0 +1,27 @@ +# 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. + +if (defined(ark_independent_build)) { + ets_frontend_root = "//ets_frontend" +} else { + build_root = "//build" + ets_frontend_root = "//arkcompiler/ets_frontend" +} + +toolchain_linux = "$build_root/toolchain/linux:clang_x64" +if (host_cpu == "arm64") { + toolchain_mac = "$build_root/toolchain/mac:clang_arm64" +} else { + toolchain_mac = "$build_root/toolchain/mac:clang_x64" +} +toolchain_win = "$build_root/toolchain/mingw:mingw_x86_64" diff --git a/ts2panda/BUILD.gn b/ts2panda/BUILD.gn index b51922857af40773e7d896ac1535d6d2f4851ba5..815bcb1def6f07baca8e3dd1e441d8fbd364e5ce 100755 --- a/ts2panda/BUILD.gn +++ b/ts2panda/BUILD.gn @@ -223,21 +223,21 @@ group("ark_ts2abc_build") { deps = [] if (host_os != "mac") { deps += [ - "${ts2abc_root}:ts2abc_build(${buildtool_linux})", - "${ts2abc_root}:ts2abc_build_ets(${buildtool_linux})", + "${ts2abc_root}:ts2abc_build(${toolchain_linux})", + "${ts2abc_root}:ts2abc_build_ets(${toolchain_linux})", ] } else { deps += [ - "${ts2abc_root}:ts2abc_build_mac(${buildtool_mac})", - "${ts2abc_root}:ts2abc_build_mac_ets(${buildtool_mac})", + "${ts2abc_root}:ts2abc_build_mac(${toolchain_mac})", + "${ts2abc_root}:ts2abc_build_mac_ets(${toolchain_mac})", ] } } group("ark_ts2abc_build_win") { deps = [ - "${ts2abc_root}:ts2abc_build_win(${buildtool_win})", - "${ts2abc_root}:ts2abc_build_win_ets(${buildtool_win})", + "${ts2abc_root}:ts2abc_build_win(${toolchain_win})", + "${ts2abc_root}:ts2abc_build_win_ets(${toolchain_win})", ] } @@ -252,13 +252,13 @@ ohos_copy("copy_ts2abc_tests") { group("ts2abc_unittests") { if (host_os == "linux") { testonly = true - deps = [ "tests:ts2abc_tests(${buildtool_linux})" ] + deps = [ "tests:ts2abc_tests(${toolchain_linux})" ] } } group("ts2abc_type_adapter_unit_tests") { if (host_os == "linux") { testonly = true - deps = [ "${ts2abc_root}/ts2abc/tests/type_adapter_test:ts2abc_type_adapter_unit_tests(${buildtool_linux})" ] + deps = [ "${ts2abc_root}/ts2abc/tests/type_adapter_test:ts2abc_type_adapter_unit_tests(${toolchain_linux})" ] } } diff --git a/ts2panda/tests/BUILD.gn b/ts2panda/tests/BUILD.gn index 8f29fa280374d822b5da6a3c4ffe56d802c113a5..1da6e41ffb2c63c016baa55517c34c55c9c664fe 100644 --- a/ts2panda/tests/BUILD.gn +++ b/ts2panda/tests/BUILD.gn @@ -28,12 +28,12 @@ action("ts2abc_tests") { rebase_path("${node_modules}"), ] - if (host_toolchain == buildtool_linux) { + if (host_toolchain == toolchain_linux) { args += [ "--platform", "linux", ] - } else if (host_toolchain == buildtool_mac) { + } else if (host_toolchain == toolchain_mac) { args += [ "--platform", "mac", diff --git a/ts2panda/ts2abc_config.gni b/ts2panda/ts2abc_config.gni index 99cc458e35d6740a942334782d50c942e33bee9f..01d9002487e336478430b860b36fbc909642af8a 100755 --- a/ts2panda/ts2abc_config.gni +++ b/ts2panda/ts2abc_config.gni @@ -10,7 +10,7 @@ # 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. - +import("//arkcompiler/ets_frontend/ets_frontend_config.gni") if (!defined(ark_independent_build)) { import("//build/ohos.gni") import("//build/test.gni") @@ -22,14 +22,6 @@ if (!defined(ark_independent_build)) { } declare_args() { - buildtool_linux = "$build_root/toolchain/linux:clang_x64" - if (host_cpu == "arm64") { - buildtool_mac = "$build_root/toolchain/mac:clang_arm64" - } else { - buildtool_mac = "$build_root/toolchain/mac:clang_x64" - } - buildtool_win = "$build_root/toolchain/mingw:mingw_x86_64" - nodejs_dir = "" node_path = "" node_modules = "" @@ -45,23 +37,23 @@ if (build_public_version) { nodejs_dir = "//prebuilts/ace-toolkit/nodejs" } -if (host_toolchain == buildtool_mac) { - ts2abc_build_deps = [ "$ts2abc_root:ts2abc_build_mac($buildtool_mac)" ] +if (host_toolchain == toolchain_mac) { + ts2abc_build_deps = [ "$ts2abc_root:ts2abc_build_mac($toolchain_mac)" ] ts2abc_build_path = - get_label_info("$ts2abc_root:ts2abc_build_mac($buildtool_mac)", + get_label_info("$ts2abc_root:ts2abc_build_mac($toolchain_mac)", "root_out_dir") + "/obj/arkcompiler/ets_frontend/ts2panda/build-mac" node_path = "${nodejs_dir}/node-v12.18.4-darwin-x64/bin/" -} else if (host_toolchain == buildtool_win) { - ts2abc_build_deps = [ "$ts2abc_root:ts2abc_build_win($buildtool_win)" ] +} else if (host_toolchain == toolchain_win) { + ts2abc_build_deps = [ "$ts2abc_root:ts2abc_build_win($toolchain_win)" ] ts2abc_build_path = - get_label_info("$ts2abc_root:ts2abc_build_win($buildtool_win)", + get_label_info("$ts2abc_root:ts2abc_build_win($toolchain_win)", "root_out_dir") + "/obj/arkcompiler/ets_frontend/ts2panda/build_win" } else { - ts2abc_build_deps = [ "$ts2abc_root:ts2abc_build($buildtool_linux)" ] + ts2abc_build_deps = [ "$ts2abc_root:ts2abc_build($toolchain_linux)" ] ts2abc_build_path = - get_label_info("$ts2abc_root:ts2abc_build($buildtool_linux)", + get_label_info("$ts2abc_root:ts2abc_build($toolchain_linux)", "root_out_dir") + "/obj/arkcompiler/ets_frontend/ts2panda/build" node_path = "${nodejs_dir}/node-v12.18.4-linux-x64/bin/" @@ -221,12 +213,12 @@ template("ts2abc_unittest") { "--gn-build", ] - if (host_toolchain == buildtool_linux) { + if (host_toolchain == toolchain_linux) { args += [ "--platform", "linux", ] - } else if (host_toolchain == buildtool_mac) { + } else if (host_toolchain == toolchain_mac) { args += [ "--platform", "mac",