Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include <errno.h> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6 | #include <signal.h> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 7 | #include <stdio.h> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9 | #include "src/v8.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11 | #include "include/libplatform/libplatform.h" |
| 12 | #include "src/assembler.h" |
| 13 | #include "src/base/platform/platform.h" |
| 14 | #include "src/bootstrapper.h" |
| 15 | #include "src/flags.h" |
| 16 | #include "src/list.h" |
| 17 | #include "src/natives.h" |
| 18 | #include "src/serialize.h" |
| 19 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 20 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 21 | using namespace v8; |
| 22 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 23 | class SnapshotWriter { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 24 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 25 | explicit SnapshotWriter(const char* snapshot_file) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 26 | : fp_(GetFileDescriptorOrDie(snapshot_file)), |
| 27 | startup_blob_file_(NULL) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | |
| 29 | ~SnapshotWriter() { |
| 30 | fclose(fp_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | if (startup_blob_file_) fclose(startup_blob_file_); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 32 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 33 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | void SetStartupBlobFile(const char* startup_blob_file) { |
| 35 | if (startup_blob_file != NULL) |
| 36 | startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file); |
| 37 | } |
| 38 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 39 | void WriteSnapshot(v8::StartupData blob) const { |
| 40 | i::Vector<const i::byte> blob_vector( |
| 41 | reinterpret_cast<const i::byte*>(blob.data), blob.raw_size); |
| 42 | WriteSnapshotFile(blob_vector); |
| 43 | MaybeWriteStartupBlob(blob_vector); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 44 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 45 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 46 | private: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 47 | void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const { |
| 48 | if (!startup_blob_file_) return; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 49 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 50 | size_t written = fwrite(blob.begin(), 1, blob.length(), startup_blob_file_); |
| 51 | if (written != static_cast<size_t>(blob.length())) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 52 | i::PrintF("Writing snapshot file failed.. Aborting.\n"); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 53 | exit(1); |
| 54 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 57 | void WriteSnapshotFile(const i::Vector<const i::byte>& blob) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 58 | WriteFilePrefix(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 59 | WriteData(blob); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 60 | WriteFileSuffix(); |
| 61 | } |
| 62 | |
| 63 | void WriteFilePrefix() const { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 64 | fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 65 | fprintf(fp_, "#include \"src/v8.h\"\n"); |
| 66 | fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n"); |
| 67 | fprintf(fp_, "#include \"src/snapshot.h\"\n\n"); |
| 68 | fprintf(fp_, "namespace v8 {\n"); |
| 69 | fprintf(fp_, "namespace internal {\n\n"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 71 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 72 | void WriteFileSuffix() const { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 73 | fprintf(fp_, "const v8::StartupData Snapshot::SnapshotBlob() {\n"); |
| 74 | fprintf(fp_, " v8::StartupData blob;\n"); |
| 75 | fprintf(fp_, " blob.data = reinterpret_cast<const char*>(blob_data);\n"); |
| 76 | fprintf(fp_, " blob.raw_size = blob_size;\n"); |
| 77 | fprintf(fp_, " return blob;\n"); |
| 78 | fprintf(fp_, "}\n\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 79 | fprintf(fp_, "} // namespace internal\n"); |
| 80 | fprintf(fp_, "} // namespace v8\n"); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 83 | void WriteData(const i::Vector<const i::byte>& blob) const { |
Rubin Xu | 3821a70 | 2016-02-03 15:20:28 +0000 | [diff] [blame^] | 84 | fprintf(fp_, "static const byte blob_data[] __attribute__((aligned(8))) = {\n"); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 85 | WriteSnapshotData(blob); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 86 | fprintf(fp_, "};\n"); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 87 | fprintf(fp_, "static const int blob_size = %d;\n", blob.length()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 88 | fprintf(fp_, "\n"); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 91 | void WriteSnapshotData(const i::Vector<const i::byte>& blob) const { |
| 92 | for (int i = 0; i < blob.length(); i++) { |
| 93 | if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n"); |
| 94 | if (i > 0) fprintf(fp_, ","); |
| 95 | fprintf(fp_, "%u", static_cast<unsigned char>(blob.at(i))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 96 | } |
| 97 | fprintf(fp_, "\n"); |
| 98 | } |
| 99 | |
| 100 | FILE* GetFileDescriptorOrDie(const char* filename) { |
| 101 | FILE* fp = base::OS::FOpen(filename, "wb"); |
| 102 | if (fp == NULL) { |
| 103 | i::PrintF("Unable to open file \"%s\" for writing.\n", filename); |
| 104 | exit(1); |
| 105 | } |
| 106 | return fp; |
| 107 | } |
| 108 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 109 | FILE* fp_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 110 | FILE* startup_blob_file_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 111 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | |
| 113 | |
| 114 | int main(int argc, char** argv) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 115 | // By default, log code create information in the snapshot. |
| 116 | i::FLAG_log_code = true; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 117 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 118 | // Omit from the snapshot natives for features that can be turned off |
| 119 | // at runtime. |
| 120 | i::FLAG_harmony_shipping = false; |
| 121 | |
| 122 | i::FLAG_logfile_per_isolate = false; |
| 123 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 124 | // Print the usage if an error occurs when parsing the command line |
| 125 | // flags or if the help flag is set. |
| 126 | int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true); |
| 127 | if (result > 0 || argc != 2 || i::FLAG_help) { |
| 128 | ::printf("Usage: %s [flag] ... outfile\n", argv[0]); |
| 129 | i::FlagList::PrintHelp(); |
| 130 | return !i::FLAG_help; |
| 131 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 132 | |
| 133 | i::CpuFeatures::Probe(true); |
| 134 | V8::InitializeICU(); |
| 135 | v8::Platform* platform = v8::platform::CreateDefaultPlatform(); |
| 136 | v8::V8::InitializePlatform(platform); |
| 137 | v8::V8::Initialize(); |
| 138 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 139 | { |
| 140 | SnapshotWriter writer(argv[1]); |
| 141 | if (i::FLAG_startup_blob) writer.SetStartupBlobFile(i::FLAG_startup_blob); |
| 142 | StartupData blob = v8::V8::CreateSnapshotDataBlob(); |
| 143 | CHECK(blob.data); |
| 144 | writer.WriteSnapshot(blob); |
| 145 | delete[] blob.data; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 146 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 147 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 148 | V8::Dispose(); |
| 149 | V8::ShutdownPlatform(); |
| 150 | delete platform; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 151 | return 0; |
| 152 | } |