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 *exec_path); | |
40 ~V8InitializationScope(); | |
41 | |
42 v8::Platform *platform() const { return platform_.get(); } | |
43 v8::Isolate *isolate() const { return isolate_; } | |
44 | |
45 private: | |
46 v8::base::SmartPointer<v8::Platform> platform_; | |
47 v8::Isolate *isolate_; | |
48 | |
49 DISALLOW_COPY_AND_ASSIGN(V8InitializationScope); | |
50 }; | |
51 | |
52 i::Isolate *GetInternalIsolate(v8::Isolate *isolate) { | |
53 return reinterpret_cast<i::Isolate *>(isolate); | |
54 } | |
55 | |
56 V8InitializationScope::V8InitializationScope(const char *exec_path) | |
57 : platform_(v8::platform::CreateDefaultPlatform()) { | |
58 i::FLAG_ignition = true; | |
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 GetInternalIsolate(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(v8::Isolate *isolate, | |
82 const char *data) { | |
83 return v8::String::NewFromUtf8(isolate, data, v8::NewStringType::kNormal) | |
84 .ToLocalChecked(); | |
85 } | |
86 | |
87 std::string WrapCodeInFunction(const char *function_name, | |
88 const std::string &source_file) { | |
89 std::ostringstream program_stream; | |
90 program_stream << "function " << function_name << "() {" << source_file | |
91 << "}\n" | |
92 << function_name << "();"; | |
93 | |
94 return program_stream.str(); | |
95 } | |
96 | |
97 v8::Local<v8::Value> CompileAndRun(v8::Isolate *isolate, | |
98 const v8::Local<v8::Context> &context, | |
99 const char *program) { | |
100 v8::Local<v8::String> source = MakeV8LocalStringFromUTF8(isolate, program); | |
101 v8::Local<v8::Script> script = | |
102 v8::Script::Compile(context, source).ToLocalChecked(); | |
103 | |
104 v8::Local<v8::Value> result; | |
105 CHECK(script->Run(context).ToLocal(&result)); | |
106 | |
107 return result; | |
108 } | |
109 | |
110 i::Handle<v8::internal::BytecodeArray> GetBytecodeArrayForGlobal( | |
111 v8::Isolate *isolate, const v8::Local<v8::Context> &context, | |
112 const char *global_name) { | |
113 v8::Local<v8::String> v8_global_name = | |
114 MakeV8LocalStringFromUTF8(isolate, global_name); | |
115 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( | |
116 context->Global()->Get(context, v8_global_name).ToLocalChecked()); | |
117 i::Handle<i::JSFunction> js_function = | |
118 i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*function)); | |
119 | |
120 i::Handle<i::BytecodeArray> bytecodes = i::handle( | |
121 js_function->shared()->bytecode_array(), GetInternalIsolate(isolate)); | |
122 | |
123 return bytecodes; | |
124 } | |
125 | |
126 void PrintBytecodeOperand(std::ostream &stream, | |
127 const BytecodeArrayIterator &bytecode_iter, | |
128 const Bytecode &bytecode, int op_index) { | |
129 OperandType op_type = Bytecodes::GetOperandType(bytecode, op_index); | |
130 OperandSize op_size = Bytecodes::GetOperandSize(bytecode, op_index); | |
131 | |
132 const char *size_tag; | |
133 switch (op_size) { | |
134 case OperandSize::kByte: | |
135 size_tag = "8"; | |
136 break; | |
137 case OperandSize::kShort: | |
138 size_tag = "16"; | |
139 break; | |
140 default: | |
141 UNREACHABLE(); | |
142 return; | |
143 } | |
144 | |
145 if (Bytecodes::IsRegisterOperandType(op_type)) { | |
146 Register register_value = bytecode_iter.GetRegisterOperand(op_index); | |
147 stream << 'R'; | |
148 if (op_size != OperandSize::kByte) stream << size_tag; | |
oth
2016/02/09 11:40:34
At some point we should make this consistent, R->R
Stefano Sanfilippo
2016/02/09 16:52:50
Yes, anytime. Whenever we do the change, this is a
| |
149 stream << '(' << register_value.index() << ')'; | |
150 } else { | |
151 uint32_t raw_value = bytecode_iter.GetRawOperand(op_index, op_type); | |
152 stream << 'U' << size_tag << '(' << raw_value << ')'; | |
153 } | |
154 } | |
155 | |
156 void PrintBytecode(std::ostream &stream, | |
157 const BytecodeArrayIterator &bytecode_iter) { | |
158 Bytecode bytecode = bytecode_iter.current_bytecode(); | |
159 | |
160 stream << "B(" << Bytecodes::ToString(bytecode) << ')'; | |
161 | |
162 int operands_count = Bytecodes::NumberOfOperands(bytecode); | |
163 for (int op_index = 0; op_index < operands_count; ++op_index) { | |
164 stream << ", "; | |
165 PrintBytecodeOperand(stream, bytecode_iter, bytecode, op_index); | |
166 } | |
167 } | |
168 | |
169 std::string RemoveTrailingSpaces(const std::string &source) { | |
170 std::string trimmed = source; | |
171 | |
172 auto tail = trimmed.end() - 1; | |
173 auto begin = trimmed.begin(); | |
174 while (tail >= begin && isspace(*tail)) { | |
175 --tail; | |
176 } | |
177 trimmed.erase(tail + 1, trimmed.end()); | |
178 return trimmed; | |
oth
2016/02/09 11:40:34
Nit. This would be slightly more succinct if it di
Stefano Sanfilippo
2016/02/09 16:52:50
I agree with you, anyway this function has been re
| |
179 } | |
180 | |
181 std::string QuoteCString(const std::string &source) { | |
182 std::stringstream quoted_buffer; | |
183 for (char c : source) { | |
184 switch (c) { | |
185 case '"': | |
186 quoted_buffer << "\\\""; | |
187 break; | |
188 case '\n': | |
189 quoted_buffer << "\\n"; | |
190 break; | |
191 case '\t': | |
192 quoted_buffer << "\\t"; | |
193 break; | |
194 case '\\': | |
195 quoted_buffer << "\\\\"; | |
196 break; | |
197 default: | |
198 quoted_buffer << c; | |
199 break; | |
200 } | |
201 } | |
202 return quoted_buffer.str(); | |
203 } | |
204 | |
205 void PrintBytecodeArray(std::ostream &stream, | |
206 i::Handle<i::BytecodeArray> bytecode_array, | |
207 const std::string &body, bool print_banner = true) { | |
208 const int kPointerSize = sizeof(void *); | |
209 | |
210 if (print_banner) { | |
211 stream << kIndent << "// ExpectedSnippet generated by bytecodechecker.\n"; | |
rmcilroy
2016/02/09 11:58:24
nit - add "===" header to make this obvious, i.e.,
Stefano Sanfilippo
2016/02/09 16:52:50
Done.
| |
212 } | |
213 | |
214 // Print the code snippet as a quoted C string. | |
215 stream << kIndent << "{" << '"' << QuoteCString(RemoveTrailingSpaces(body)) | |
216 << "\",\n" | |
217 << kIndent; | |
218 | |
219 // Print the frame size, in multiples of kPointerSize. | |
220 int frame_size = bytecode_array->frame_size(); | |
221 DCHECK(frame_size % kPointerSize == 0); | |
222 if (frame_size > kPointerSize) { | |
223 stream << ' ' << frame_size / kPointerSize << " * kPointerSize,\n" | |
224 << kIndent; | |
225 } else if (frame_size == kPointerSize) { | |
226 stream << " kPointerSize,\n" << kIndent; | |
227 } else if (frame_size == 0) { | |
228 stream << " 0,\n" << kIndent; | |
229 } | |
230 | |
231 // Print parameter count and size of the bytecode array. | |
232 stream << ' ' << bytecode_array->parameter_count() << ",\n" | |
233 << kIndent << ' ' << bytecode_array->length() << ",\n" | |
234 << kIndent << " {\n" | |
235 << kIndent << " "; | |
236 | |
237 // Print bytecodes. | |
238 BytecodeArrayIterator bytecode_iter{bytecode_array}; | |
239 for (; !bytecode_iter.done(); bytecode_iter.Advance()) { | |
240 // Print separator before each instruction, except the first one. | |
241 if (bytecode_iter.current_offset() > 0) { | |
242 stream << ",\n" << kIndent << " "; | |
243 } | |
244 PrintBytecode(stream, bytecode_iter); | |
245 } | |
246 | |
247 stream << "\n" << kIndent << " },\n"; | |
248 // TODO(ssanfilippo) add representation of constant pool and handlers. | |
249 stream << kIndent << " // constant pool and handlers here\n"; | |
250 stream << kIndent << "}\n"; | |
251 } | |
252 | |
253 bool ReadFromFileOrStdin(std::string *body, const char *body_filename) { | |
254 std::stringstream body_buffer; | |
255 if (strcmp(body_filename, "-") == 0) { | |
256 body_buffer << std::cin.rdbuf(); | |
257 } else { | |
258 std::ifstream body_file{body_filename}; | |
259 if (!body_file) return false; | |
260 body_buffer << body_file.rdbuf(); | |
261 } | |
262 *body = body_buffer.str(); | |
263 return true; | |
264 } | |
265 | |
266 void PrintUsage(const char *exec_path) { | |
267 std::cerr << "Usage: " << exec_path | |
268 << " [filename.js|-]\n\n" | |
269 "No arguments or - reads from standard input.\n" | |
270 "Anything else is interpreted as a filename.\n\n" | |
271 "This tool is intended as a help in writing tests.\n" | |
272 "Please, DO NOT blindly copy and paste the output " | |
273 "into the test suite.\n"; | |
274 } | |
275 | |
276 } // namespace | |
277 | |
278 int main(int argc, char **argv) { | |
279 if (argc > 1 && strcmp(argv[1], "--help") == 0) { | |
280 PrintUsage(argv[0]); | |
281 return 0; | |
282 } | |
283 | |
284 const char *body_filename = (argc > 1 ? argv[1] : "-"); | |
285 const char *wrapper_function_name = "__bytecodechecker_wrapper__"; | |
286 | |
287 std::string body; | |
288 if (!ReadFromFileOrStdin(&body, body_filename)) { | |
289 std::cerr << "Could not open '" << body_filename << "'.\n\n"; | |
290 PrintUsage(argv[0]); | |
291 return 1; | |
292 } | |
293 | |
294 V8InitializationScope platform(argv[0]); | |
295 { | |
296 v8::Isolate::Scope isolate_scope(platform.isolate()); | |
297 v8::HandleScope handle_scope(platform.isolate()); | |
298 v8::Local<v8::Context> context = v8::Context::New(platform.isolate()); | |
299 v8::Context::Scope context_scope(context); | |
300 | |
301 std::string source_code = WrapCodeInFunction(wrapper_function_name, body); | |
302 CompileAndRun(platform.isolate(), context, source_code.c_str()); | |
303 | |
304 i::Handle<i::BytecodeArray> bytecode_array = GetBytecodeArrayForGlobal( | |
305 platform.isolate(), context, wrapper_function_name); | |
306 | |
307 PrintBytecodeArray(std::cout, bytecode_array, body); | |
308 } | |
309 } | |
OLD | NEW |