1 // Copyright 2017 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <fstream>
6 #include <iostream>
7
8 #include "src/torque/declarable.h"
9 #include "src/torque/declaration-visitor.h"
10 #include "src/torque/global-context.h"
11 #include "src/torque/implementation-visitor.h"
12 #include "src/torque/scope.h"
13 #include "src/torque/torque-parser.h"
14 #include "src/torque/type-oracle.h"
15 #include "src/torque/types.h"
16 #include "src/torque/utils.h"
17
18 namespace v8 {
19 namespace internal {
20 namespace torque {
21
WrappedMain(int argc,const char ** argv)22 int WrappedMain(int argc, const char** argv) {
23 std::string output_directory;
24 bool verbose = false;
25 SourceFileMap::Scope source_file_map_scope;
26 CurrentSourceFile::Scope unknown_sourcefile_scope(
27 SourceFileMap::AddSource("<unknown>"));
28 CurrentAst::Scope ast_scope;
29 for (int i = 1; i < argc; ++i) {
30 // Check for options
31 if (!strcmp("-o", argv[i])) {
32 output_directory = argv[++i];
33 continue;
34 }
35 if (!strcmp("-v", argv[i])) {
36 verbose = true;
37 continue;
38 }
39
40 // Otherwise it's a .tq
41 // file, parse it and
42 // remember the syntax tree
43 std::string path = argv[i];
44 SourceId source_id = SourceFileMap::AddSource(path);
45 CurrentSourceFile::Scope source_id_scope(source_id);
46 std::ifstream file_stream(path);
47 std::string file_content = {std::istreambuf_iterator<char>(file_stream),
48 std::istreambuf_iterator<char>()};
49 ParseTorque(file_content);
50 }
51
52 GlobalContext global_context(std::move(CurrentAst::Get()));
53 if (verbose) global_context.SetVerbose();
54 TypeOracle::Scope type_oracle(global_context.declarations());
55
56 if (output_directory.length() != 0) {
57 {
58 DeclarationVisitor visitor(global_context);
59
60 visitor.Visit(global_context.ast());
61
62 std::string output_header_path = output_directory;
63 output_header_path += "/builtin-definitions-from-dsl.h";
64 visitor.GenerateHeader(output_header_path);
65 }
66
67 ImplementationVisitor visitor(global_context);
68 for (auto& module : global_context.GetModules()) {
69 visitor.BeginModuleFile(module.second.get());
70 }
71
72 visitor.Visit(global_context.ast());
73
74 for (auto& module : global_context.GetModules()) {
75 visitor.EndModuleFile(module.second.get());
76 visitor.GenerateImplementation(output_directory, module.second.get());
77 }
78 }
79 return 0;
80 }
81
82 } // namespace torque
83 } // namespace internal
84 } // namespace v8
85
main(int argc,const char ** argv)86 int main(int argc, const char** argv) {
87 return v8::internal::torque::WrappedMain(argc, argv);
88 }
89