blob: e5abfb2552dc9d067e341eb875488af82a124393 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include <errno.h>
Steve Blocka7e24c12009-10-30 11:49:00 +00006#include <signal.h>
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007#include <stdio.h>
Steve Blocka7e24c12009-10-30 11:49:00 +00008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000010
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#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 Blocka7e24c12009-10-30 11:49:00 +000020
Steve Blocka7e24c12009-10-30 11:49:00 +000021using namespace v8;
22
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023class SnapshotWriter {
Ben Murdoch257744e2011-11-30 15:57:28 +000024 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025 explicit SnapshotWriter(const char* snapshot_file)
Emily Bernierd0a1eb72015-03-24 16:35:39 -040026 : fp_(GetFileDescriptorOrDie(snapshot_file)),
27 startup_blob_file_(NULL) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028
29 ~SnapshotWriter() {
30 fclose(fp_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 if (startup_blob_file_) fclose(startup_blob_file_);
Ben Murdoch257744e2011-11-30 15:57:28 +000032 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034 void SetStartupBlobFile(const char* startup_blob_file) {
35 if (startup_blob_file != NULL)
36 startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file);
37 }
38
Emily Bernierd0a1eb72015-03-24 16:35:39 -040039 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 Murdochb8a8cc12014-11-26 15:28:44 +000044 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000045
Ben Murdoch257744e2011-11-30 15:57:28 +000046 private:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040047 void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const {
48 if (!startup_blob_file_) return;
Ben Murdoch257744e2011-11-30 15:57:28 +000049
Emily Bernierd0a1eb72015-03-24 16:35:39 -040050 size_t written = fwrite(blob.begin(), 1, blob.length(), startup_blob_file_);
51 if (written != static_cast<size_t>(blob.length())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052 i::PrintF("Writing snapshot file failed.. Aborting.\n");
Steve Blockd0582a62009-12-15 09:54:21 +000053 exit(1);
54 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 }
56
Emily Bernierd0a1eb72015-03-24 16:35:39 -040057 void WriteSnapshotFile(const i::Vector<const i::byte>& blob) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 WriteFilePrefix();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040059 WriteData(blob);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060 WriteFileSuffix();
61 }
62
63 void WriteFilePrefix() const {
Steve Blockd0582a62009-12-15 09:54:21 +000064 fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065 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 Blocka7e24c12009-10-30 11:49:00 +000070 }
Steve Blockd0582a62009-12-15 09:54:21 +000071
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072 void WriteFileSuffix() const {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040073 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 Murdochb8a8cc12014-11-26 15:28:44 +000079 fprintf(fp_, "} // namespace internal\n");
80 fprintf(fp_, "} // namespace v8\n");
Andrei Popescu31002712010-02-23 13:46:05 +000081 }
82
Emily Bernierd0a1eb72015-03-24 16:35:39 -040083 void WriteData(const i::Vector<const i::byte>& blob) const {
Rubin Xu3821a702016-02-03 15:20:28 +000084 fprintf(fp_, "static const byte blob_data[] __attribute__((aligned(8))) = {\n");
Emily Bernierd0a1eb72015-03-24 16:35:39 -040085 WriteSnapshotData(blob);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 fprintf(fp_, "};\n");
Emily Bernierd0a1eb72015-03-24 16:35:39 -040087 fprintf(fp_, "static const int blob_size = %d;\n", blob.length());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088 fprintf(fp_, "\n");
Steve Blockd0582a62009-12-15 09:54:21 +000089 }
90
Emily Bernierd0a1eb72015-03-24 16:35:39 -040091 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 Murdochb8a8cc12014-11-26 15:28:44 +000096 }
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 Blockd0582a62009-12-15 09:54:21 +0000109 FILE* fp_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000110 FILE* startup_blob_file_;
Steve Blockd0582a62009-12-15 09:54:21 +0000111};
Steve Blocka7e24c12009-10-30 11:49:00 +0000112
113
114int main(int argc, char** argv) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 // By default, log code create information in the snapshot.
116 i::FLAG_log_code = true;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000117
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400118 // 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 Blocka7e24c12009-10-30 11:49:00 +0000124 // 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 Murdochb8a8cc12014-11-26 15:28:44 +0000132
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 Bernierd0a1eb72015-03-24 16:35:39 -0400139 {
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 Blocka7e24c12009-10-30 11:49:00 +0000146 }
Andrei Popescu31002712010-02-23 13:46:05 +0000147
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148 V8::Dispose();
149 V8::ShutdownPlatform();
150 delete platform;
Steve Blocka7e24c12009-10-30 11:49:00 +0000151 return 0;
152}