OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "include/libplatform/libplatform.h" | |
9 #include "include/v8.h" | |
10 | |
11 #include "src/base/logging.h" | |
12 #include "src/base/smart-pointers.h" | |
13 #include "src/compiler.h" | |
14 | |
15 #include "src/interpreter/bytecode-array-iterator.h" | |
16 #include "src/interpreter/bytecode-generator.h" | |
17 #include "src/interpreter/bytecodes.h" | |
18 #include "src/interpreter/interpreter.h" | |
19 | |
20 using namespace i::interpreter; | |
21 | |
22 namespace { | |
23 | |
24 const char *kIndent = " "; | |
25 | |
26 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator { | |
27 public: | |
28 void *Allocate(size_t length) override { | |
29 void *data = AllocateUninitialized(length); | |
30 if (data != nullptr) memset(data, 0, length); | |
31 return data; | |
32 } | |
33 void *AllocateUninitialized(size_t length) override { return malloc(length); } | |
34 void Free(void *data, size_t) override { free(data); } | |
35 }; | |
36 | |
37 class V8InitializationScope final { | |
38 public: | |
39 explicit V8InitializationScope(const char *program_name); | |
rmcilroy
2016/02/08 13:55:32
/s/program_name/exec_path
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
40 ~V8InitializationScope(); | |
41 | |
42 v8::Platform *platform() const { return platform_.get(); } | |
43 v8::Isolate *isolate() const { return isolate_; } | |
44 i::Isolate *i_isolate() const { | |
45 return reinterpret_cast<i::Isolate *>(isolate_); | |
46 } | |
47 | |
48 private: | |
49 v8::base::SmartPointer<v8::Platform> platform_; | |
50 v8::Isolate *isolate_; | |
51 | |
52 DISALLOW_COPY_AND_ASSIGN(V8InitializationScope); | |
53 }; | |
54 | |
55 V8InitializationScope::V8InitializationScope(const char *exec_path) | |
56 : platform_(v8::platform::CreateDefaultPlatform()) { | |
57 i::FLAG_ignition = true; | |
58 i::FLAG_ignition_filter = i::StrDup("*"); | |
rmcilroy
2016/02/08 13:55:32
just drop this (the default is "*")
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
59 i::FLAG_always_opt = false; | |
60 i::FLAG_allow_natives_syntax = true; | |
61 | |
62 v8::V8::InitializeICU(); | |
63 v8::V8::InitializeExternalStartupData(exec_path); | |
64 v8::V8::InitializePlatform(platform_.get()); | |
65 v8::V8::Initialize(); | |
66 | |
67 ArrayBufferAllocator allocator; | |
68 v8::Isolate::CreateParams create_params; | |
69 create_params.array_buffer_allocator = &allocator; | |
70 | |
71 isolate_ = v8::Isolate::New(create_params); | |
72 i_isolate()->interpreter()->Initialize(); | |
73 } | |
74 | |
75 V8InitializationScope::~V8InitializationScope() { | |
76 isolate_->Dispose(); | |
77 v8::V8::Dispose(); | |
78 v8::V8::ShutdownPlatform(); | |
79 } | |
80 | |
81 v8::Local<v8::String> MakeV8LocalStringFromUTF8( | |
82 const V8InitializationScope &platform, const char *data) { | |
rmcilroy
2016/02/08 13:55:32
Just pass in the isolate for these functions (inst
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
83 return v8::String::NewFromUtf8(platform.isolate(), data, | |
84 v8::NewStringType::kNormal) | |
85 .ToLocalChecked(); | |
86 } | |
87 | |
88 static std::string WrapCodeInFunction(const char *function_name, | |
89 const std::string &source_file) { | |
90 std::ostringstream program_stream; | |
91 program_stream << "function " << function_name << "() {" << source_file | |
92 << "}\n" | |
93 << function_name << "();"; | |
94 | |
95 return program_stream.str(); | |
96 } | |
97 | |
98 v8::Local<v8::Value> CompileAndRun(const V8InitializationScope &platform, | |
rmcilroy
2016/02/08 13:55:32
ditto
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
99 const v8::Local<v8::Context> &context, | |
100 const char *program) { | |
101 v8::Local<v8::String> source = MakeV8LocalStringFromUTF8(platform, program); | |
102 v8::Local<v8::Script> script = | |
103 v8::Script::Compile(context, source).ToLocalChecked(); | |
104 | |
105 v8::Local<v8::Value> result; | |
106 bool success = script->Run(context).ToLocal(&result); | |
rmcilroy
2016/02/08 13:55:31
Just: CHECK(script->Run(context).ToLocal(&result))
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
107 CHECK(success); | |
108 | |
109 return result; | |
110 } | |
111 | |
112 i::Handle<v8::internal::BytecodeArray> GetBytecodeArrayForGlobal( | |
113 const V8InitializationScope &platform, | |
rmcilroy
2016/02/08 13:55:32
ditto
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
114 const v8::Local<v8::Context> &context, const char *global_name) { | |
115 v8::Local<v8::String> v8_global_name = | |
116 MakeV8LocalStringFromUTF8(platform, global_name); | |
117 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( | |
118 context->Global()->Get(context, v8_global_name).ToLocalChecked()); | |
119 i::Handle<i::JSFunction> js_function = | |
120 i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*function)); | |
121 | |
122 i::Handle<i::BytecodeArray> bytecodes = | |
123 i::handle(js_function->shared()->bytecode_array(), platform.i_isolate()); | |
124 | |
125 return bytecodes; | |
126 } | |
127 | |
128 void PrintBytecodeOperand(std::ostream &stream, | |
129 const BytecodeArrayIterator &bytecode_iter, | |
130 const Bytecode &bytecode, int op_index) { | |
131 OperandType op_type = Bytecodes::GetOperandType(bytecode, op_index); | |
132 OperandSize op_size = Bytecodes::GetOperandSize(bytecode, op_index); | |
133 | |
134 const char *size_tag; | |
135 bool size_tag_is_byte = false; | |
136 switch (op_size) { | |
137 case OperandSize::kByte: | |
138 size_tag = "8"; | |
139 size_tag_is_byte = true; | |
140 break; | |
141 case OperandSize::kShort: | |
142 size_tag = "16"; | |
143 break; | |
144 default: | |
145 UNREACHABLE(); | |
146 return; | |
147 } | |
148 | |
149 if (Bytecodes::IsRegisterOperandType(op_type)) { | |
150 Register register_value = bytecode_iter.GetRegisterOperand(op_index); | |
151 stream << 'R'; | |
152 if (!size_tag_is_byte) stream << size_tag; | |
rmcilroy
2016/02/08 13:55:32
no need for the size_tag_is_byte, just: if (op_siz
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
153 stream << '(' << register_value.index() << ')'; | |
154 } else { | |
155 uint32_t raw_value = bytecode_iter.GetRawOperand(op_index, op_type); | |
156 stream << 'U' << size_tag << '(' << raw_value << ')'; | |
157 } | |
158 } | |
159 | |
160 void PrintBytecode(std::ostream &stream, | |
161 const BytecodeArrayIterator &bytecode_iter) { | |
162 Bytecode bytecode = bytecode_iter.current_bytecode(); | |
163 | |
164 stream << "B(" << Bytecodes::ToString(bytecode) << ')'; | |
165 | |
166 int operands_count = Bytecodes::NumberOfOperands(bytecode); | |
167 for (int op_index = 0; op_index < operands_count; ++op_index) { | |
168 stream << ", "; | |
169 PrintBytecodeOperand(stream, bytecode_iter, bytecode, op_index); | |
170 } | |
171 } | |
172 | |
173 std::string RemoveTrailingSpaces(const std::string &source) { | |
rmcilroy
2016/02/08 13:55:32
is this necessary? The tests shouldn't have any tr
Stefano Sanfilippo
2016/02/09 10:09:58
This was done to avoid the ".... \n" thing when re
rmcilroy
2016/02/09 11:58:24
Most editors have an option to remove trailing spa
| |
174 std::string trimmed = source; | |
175 | |
176 auto tail = trimmed.end() - 1; | |
177 auto begin = trimmed.begin(); | |
178 while (tail >= begin && isspace(*tail)) { | |
179 --tail; | |
180 } | |
181 trimmed.erase(tail + 1, trimmed.end()); | |
182 return trimmed; | |
183 } | |
184 | |
185 std::string QuoteCString(const std::string &source) { | |
186 std::stringstream quoted_buffer; | |
187 for (char c : source) { | |
188 switch (c) { | |
189 case '"': | |
190 quoted_buffer << "\\\""; | |
191 break; | |
192 case '\n': | |
193 quoted_buffer << "\\n"; | |
194 break; | |
195 case '\t': | |
196 quoted_buffer << "\\t"; | |
197 break; | |
198 case '\\': | |
199 quoted_buffer << "\\\\"; | |
200 break; | |
201 default: | |
202 quoted_buffer << c; | |
203 break; | |
204 } | |
205 } | |
206 return quoted_buffer.str(); | |
207 } | |
208 | |
209 void PrintBytecodeArray(std::ostream &stream, | |
210 i::Handle<i::BytecodeArray> bytecode_array, | |
211 const std::string &body, bool print_banner = true) { | |
212 const int kPointerSize = sizeof(void *); | |
213 | |
214 if (print_banner) { | |
215 stream << "// ExpectedSnippet generated by bytecodechecker.\n" | |
rmcilroy
2016/02/08 13:55:32
Add indent before the comment.
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
216 "// It is intended for diffing and as a help in writing tests.\n" | |
217 "// DO NOT blindly copy and paste into the test suite.\n" | |
rmcilroy
2016/02/08 13:55:32
Not sure these two lines are necessary (you could
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
218 "// \n\n"; | |
219 } | |
220 | |
221 stream << kIndent << "{" << '"' << QuoteCString(RemoveTrailingSpaces(body)) | |
rmcilroy
2016/02/08 13:55:32
Some comments above each block that you print woul
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
222 << "\",\n" | |
223 << kIndent; | |
224 | |
225 int frame_size = bytecode_array->frame_size(); | |
226 if (frame_size > kPointerSize && frame_size % kPointerSize == 0) { | |
rmcilroy
2016/02/08 13:55:32
frame_size % kPointerSize must always be '0', so n
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
227 stream << ' ' << frame_size / kPointerSize << " * kPointerSize,\n" | |
228 << kIndent; | |
229 } else if (frame_size == kPointerSize) { | |
230 stream << " kPointerSize,\n" << kIndent; | |
rmcilroy
2016/02/08 13:55:32
No need for this special case, just printing '1 *
Stefano Sanfilippo
2016/02/09 10:09:58
This was done for consistency with the test cases
| |
231 } else if (frame_size == 0) { | |
232 stream << " 0,\n" << kIndent; | |
233 } else { | |
234 stream << frame_size << ",\n" << kIndent; | |
rmcilroy
2016/02/08 13:55:31
given the comment above, this shouldn't be necessa
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
235 } | |
236 | |
237 stream << ' ' << bytecode_array->parameter_count() << ",\n" | |
238 << kIndent << ' ' << bytecode_array->length() << ",\n" | |
239 << kIndent << " {\n" | |
240 << kIndent << " "; | |
241 | |
242 BytecodeArrayIterator bytecode_iter{bytecode_array}; | |
243 | |
244 for (; !bytecode_iter.done(); bytecode_iter.Advance()) { | |
245 // Print separator before each instruction, except the first one | |
rmcilroy
2016/02/08 13:55:32
full-stop after comments (throughout file).
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
246 if (bytecode_iter.current_offset() > 0) { | |
247 stream << ",\n" << kIndent << " "; | |
248 } | |
249 PrintBytecode(stream, bytecode_iter); | |
250 } | |
251 | |
252 stream << "\n" << kIndent << " },\n"; | |
253 stream << kIndent << " // constant pool and handlers here\n"; // placeholder | |
rmcilroy
2016/02/08 13:55:32
add a // TODO(ssanfillipo): ... above this line in
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
254 stream << kIndent << "}\n"; | |
255 } | |
256 | |
257 void PrintUsage(const char *exec_path) { | |
rmcilroy
2016/02/08 13:55:32
nit - PrintUsage just below ReadFromFileOrStdin (
Stefano Sanfilippo
2016/02/09 10:09:58
Done.
| |
258 std::cerr << "Usage: " << exec_path | |
259 << " [filename.js|-]\n\n" | |
260 "No arguments or - reads from standard input.\n" | |
261 "Anything else is interpreted as a filename.\n"; | |
262 } | |
263 | |
264 bool ReadFromFileOrStdin(std::string *body, const char *body_filename) { | |
265 std::stringstream body_buffer; | |
266 if (strcmp(body_filename, "-") == 0) { | |
267 body_buffer << std::cin.rdbuf(); | |
268 } else { | |
269 std::ifstream body_file{body_filename}; | |
270 if (!body_file) return false; | |
271 body_buffer << body_file.rdbuf(); | |
272 } | |
273 *body = body_buffer.str(); | |
274 return true; | |
275 } | |
276 | |
277 } // namespace | |
278 | |
279 int main(int argc, char **argv) { | |
280 if (argc > 1 && strcmp(argv[1], "--help") == 0) { | |
281 PrintUsage(argv[0]); | |
282 return 0; | |
283 } | |
284 | |
285 const char *body_filename = (argc > 1 ? argv[1] : "-"); | |
286 const char *wrapper_function_name = "__bytecodechecker_wrapper__"; | |
287 | |
288 std::string body; | |
289 if (!ReadFromFileOrStdin(&body, body_filename)) { | |
290 std::cerr << "Could not open '" << body_filename << "'.\n\n"; | |
291 PrintUsage(argv[0]); | |
292 return 1; | |
293 } | |
294 | |
295 V8InitializationScope platform(argv[0]); | |
296 { | |
297 v8::Isolate::Scope isolate_scope(platform.isolate()); | |
298 v8::HandleScope handle_scope(platform.isolate()); | |
299 v8::Local<v8::Context> context = v8::Context::New(platform.isolate()); | |
300 v8::Context::Scope context_scope(context); | |
301 | |
302 std::string source_code = WrapCodeInFunction(wrapper_function_name, body); | |
303 CompileAndRun(platform, context, source_code.c_str()); | |
304 | |
305 i::Handle<i::BytecodeArray> bytecode_array = | |
306 GetBytecodeArrayForGlobal(platform, context, wrapper_function_name); | |
307 | |
308 PrintBytecodeArray(std::cout, bytecode_array, body); | |
309 } | |
310 } | |
OLD | NEW |