From 6b132181eb169febde3ab657746f09514ec322e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Z=C3=BCnd?= Date: Tue, 19 Feb 2019 15:22:14 +0100 Subject: [PATCH] [torque] Extract compiler into separate function for easier reusability MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R=tebbi@chromium.org Bug: v8:7793 Change-Id: I948b77a50cead2d031d007d06e9a4892a55b2408 Reviewed-on: https://chromium-review.googlesource.com/c/1477223 Reviewed-by: Tobias Tebbi Commit-Queue: Simon Zünd Cr-Commit-Position: refs/heads/master@{#59717} --- BUILD.gn | 2 + src/torque/torque-compiler.cc | 78 +++++++++++++++++++++++++++++++++++ src/torque/torque-compiler.h | 32 ++++++++++++++ src/torque/torque.cc | 62 +++------------------------- 4 files changed, 117 insertions(+), 57 deletions(-) create mode 100644 src/torque/torque-compiler.cc create mode 100644 src/torque/torque-compiler.h diff --git a/BUILD.gn b/BUILD.gn index 8e96a8cf47..0670f3a92d 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -3105,6 +3105,8 @@ v8_source_set("torque_base") { "src/torque/instructions.h", "src/torque/source-positions.cc", "src/torque/source-positions.h", + "src/torque/torque-compiler.cc", + "src/torque/torque-compiler.h", "src/torque/torque-parser.cc", "src/torque/torque-parser.h", "src/torque/type-oracle.cc", diff --git a/src/torque/torque-compiler.cc b/src/torque/torque-compiler.cc new file mode 100644 index 0000000000..dd55192a03 --- /dev/null +++ b/src/torque/torque-compiler.cc @@ -0,0 +1,78 @@ +// Copyright 2019 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "src/torque/torque-compiler.h" + +#include +#include "src/torque/declarable.h" +#include "src/torque/declaration-visitor.h" +#include "src/torque/global-context.h" +#include "src/torque/implementation-visitor.h" +#include "src/torque/torque-parser.h" +#include "src/torque/type-oracle.h" + +namespace v8 { +namespace internal { +namespace torque { + +namespace { + +void ReadAndParseTorqueFile(const std::string& path) { + SourceId source_id = SourceFileMap::AddSource(path); + CurrentSourceFile::Scope source_id_scope(source_id); + std::ifstream file_stream(path); + std::string file_content = {std::istreambuf_iterator(file_stream), + std::istreambuf_iterator()}; + ParseTorque(file_content); +} + +} // namespace + +void CompileTorque(std::vector files, + TorqueCompilerOptions options) { + SourceFileMap::Scope source_file_map_scope; + CurrentSourceFile::Scope unknown_source_file_scope( + SourceFileMap::AddSource("")); + CurrentAst::Scope ast_scope_; + LintErrorStatus::Scope lint_error_status_scope_; + + for (const auto& path : files) ReadAndParseTorqueFile(path); + + GlobalContext::Scope global_context(std::move(CurrentAst::Get())); + if (options.verbose) GlobalContext::SetVerbose(); + TypeOracle::Scope type_oracle; + + DeclarationVisitor declaration_visitor; + + declaration_visitor.Visit(GlobalContext::Get().ast()); + declaration_visitor.FinalizeStructsAndClasses(); + + ImplementationVisitor implementation_visitor; + for (Namespace* n : GlobalContext::Get().GetNamespaces()) { + implementation_visitor.BeginNamespaceFile(n); + } + + implementation_visitor.VisitAllDeclarables(); + + std::string output_directory = options.output_directory; + if (output_directory.length() != 0) { + std::string output_header_path = output_directory; + output_header_path += "/builtin-definitions-from-dsl.h"; + implementation_visitor.GenerateBuiltinDefinitions(output_header_path); + + output_header_path = output_directory + "/class-definitions-from-dsl.h"; + implementation_visitor.GenerateClassDefinitions(output_header_path); + + for (Namespace* n : GlobalContext::Get().GetNamespaces()) { + implementation_visitor.EndNamespaceFile(n); + implementation_visitor.GenerateImplementation(output_directory, n); + } + } + + if (LintErrorStatus::HasLintErrors()) std::abort(); +} + +} // namespace torque +} // namespace internal +} // namespace v8 diff --git a/src/torque/torque-compiler.h b/src/torque/torque-compiler.h new file mode 100644 index 0000000000..4dfada3d99 --- /dev/null +++ b/src/torque/torque-compiler.h @@ -0,0 +1,32 @@ +// Copyright 2019 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef V8_TORQUE_TORQUE_COMPILER_H_ +#define V8_TORQUE_TORQUE_COMPILER_H_ + +#include "src/torque/ast.h" +#include "src/torque/contextual.h" +#include "src/torque/source-positions.h" +#include "src/torque/utils.h" + +namespace v8 { +namespace internal { +namespace torque { + +struct TorqueCompilerOptions { + std::string output_directory; + bool verbose; + bool abort_on_lint_errors; + + static TorqueCompilerOptions Default() { return {"", false, false}; } +}; + +void CompileTorque(std::vector files, + TorqueCompilerOptions = TorqueCompilerOptions::Default()); + +} // namespace torque +} // namespace internal +} // namespace v8 + +#endif // V8_TORQUE_TORQUE_COMPILER_H_ diff --git a/src/torque/torque.cc b/src/torque/torque.cc index 876dd454e9..28f74eecb5 100644 --- a/src/torque/torque.cc +++ b/src/torque/torque.cc @@ -2,17 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include -#include - -#include "src/torque/declarable.h" -#include "src/torque/declaration-visitor.h" -#include "src/torque/global-context.h" -#include "src/torque/implementation-visitor.h" -#include "src/torque/torque-parser.h" -#include "src/torque/type-oracle.h" -#include "src/torque/types.h" -#include "src/torque/utils.h" +#include "src/torque/torque-compiler.h" namespace v8 { namespace internal { @@ -21,11 +11,7 @@ namespace torque { int WrappedMain(int argc, const char** argv) { std::string output_directory; bool verbose = false; - SourceFileMap::Scope source_file_map_scope; - CurrentSourceFile::Scope unknown_sourcefile_scope( - SourceFileMap::AddSource("")); - CurrentAst::Scope ast_scope; - LintErrorStatus::Scope lint_error_status_scope; + std::vector files; for (int i = 1; i < argc; ++i) { // Check for options @@ -38,49 +24,11 @@ int WrappedMain(int argc, const char** argv) { continue; } - // Otherwise it's a .tq - // file, parse it and - // remember the syntax tree - std::string path = argv[i]; - SourceId source_id = SourceFileMap::AddSource(path); - CurrentSourceFile::Scope source_id_scope(source_id); - std::ifstream file_stream(path); - std::string file_content = {std::istreambuf_iterator(file_stream), - std::istreambuf_iterator()}; - ParseTorque(file_content); + // Otherwise it's a .tq file. Remember it for compilation. + files.emplace_back(argv[i]); } - GlobalContext::Scope global_context(std::move(CurrentAst::Get())); - if (verbose) GlobalContext::SetVerbose(); - TypeOracle::Scope type_oracle; - - if (output_directory.length() != 0) { - DeclarationVisitor declaration_visitor; - - declaration_visitor.Visit(GlobalContext::Get().ast()); - declaration_visitor.FinalizeStructsAndClasses(); - - ImplementationVisitor implementation_visitor; - for (Namespace* n : GlobalContext::Get().GetNamespaces()) { - implementation_visitor.BeginNamespaceFile(n); - } - - implementation_visitor.VisitAllDeclarables(); - - std::string output_header_path = output_directory; - output_header_path += "/builtin-definitions-from-dsl.h"; - implementation_visitor.GenerateBuiltinDefinitions(output_header_path); - - output_header_path = output_directory + "/class-definitions-from-dsl.h"; - implementation_visitor.GenerateClassDefinitions(output_header_path); - - for (Namespace* n : GlobalContext::Get().GetNamespaces()) { - implementation_visitor.EndNamespaceFile(n); - implementation_visitor.GenerateImplementation(output_directory, n); - } - } - - if (LintErrorStatus::HasLintErrors()) std::abort(); + CompileTorque(files, {output_directory, verbose, true}); return 0; }