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, | |
rmcilroy
2016/02/10 09:18:00
nit - V8StringFromUTF8
Stefano Sanfilippo
2016/02/10 11:00:15
Done.
| |
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) { | |
rmcilroy
2016/02/10 09:18:00
/s/source_file/function_body
Stefano Sanfilippo
2016/02/10 11:00:15
Done.
| |
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; | |
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 QuoteCString(const std::string& source) { | |
170 std::string quoted_buffer; | |
171 for (char c : source) { | |
172 switch (c) { | |
173 case '"': | |
174 quoted_buffer += "\\\""; | |
175 break; | |
176 case '\n': | |
177 quoted_buffer += "\\n"; | |
178 break; | |
179 case '\t': | |
180 quoted_buffer += "\\t"; | |
181 break; | |
182 case '\\': | |
183 quoted_buffer += "\\\\"; | |
184 break; | |
185 default: | |
186 quoted_buffer += c; | |
187 break; | |
188 } | |
189 } | |
190 return quoted_buffer; | |
191 } | |
192 | |
193 void PrintBytecodeArray(std::ostream& stream, | |
194 i::Handle<i::BytecodeArray> bytecode_array, | |
195 const std::string& body, bool print_banner = true) { | |
196 const int kPointerSize = sizeof(void*); | |
197 | |
198 if (print_banner) { | |
199 stream << kIndent << "// === ExpectedSnippet generated by " | |
200 "generate-bytecode-expectations. ===\n"; | |
201 } | |
202 | |
203 // Print the code snippet as a quoted C string. | |
204 stream << kIndent << "{" << '"' << QuoteCString(body) << "\",\n" << kIndent; | |
205 | |
206 // Print the frame size, in multiples of kPointerSize. | |
207 int frame_size = bytecode_array->frame_size(); | |
208 DCHECK(frame_size % kPointerSize == 0); | |
209 if (frame_size > kPointerSize) { | |
210 stream << ' ' << frame_size / kPointerSize << " * kPointerSize,\n" | |
211 << kIndent; | |
212 } else if (frame_size == kPointerSize) { | |
213 stream << " kPointerSize,\n" << kIndent; | |
214 } else if (frame_size == 0) { | |
215 stream << " 0,\n" << kIndent; | |
216 } | |
217 | |
218 // Print parameter count and size of the bytecode array. | |
219 stream << ' ' << bytecode_array->parameter_count() << ",\n" | |
220 << kIndent << ' ' << bytecode_array->length() << ",\n" | |
221 << kIndent << " {\n" | |
222 << kIndent << " "; | |
223 | |
224 // Print bytecodes. | |
225 BytecodeArrayIterator bytecode_iter{bytecode_array}; | |
226 for (; !bytecode_iter.done(); bytecode_iter.Advance()) { | |
227 // Print separator before each instruction, except the first one. | |
228 if (bytecode_iter.current_offset() > 0) { | |
229 stream << ",\n" << kIndent << " "; | |
230 } | |
231 PrintBytecode(stream, bytecode_iter); | |
232 } | |
233 | |
234 stream << "\n" << kIndent << " },\n"; | |
235 // TODO(ssanfilippo) add representation of constant pool and handlers. | |
236 stream << kIndent << " // constant pool and handlers here\n"; | |
237 stream << kIndent << "}\n"; | |
238 } | |
239 | |
240 bool ReadFromFileOrStdin(std::string* body, const char* body_filename) { | |
241 std::stringstream body_buffer; | |
242 if (strcmp(body_filename, "-") == 0) { | |
243 body_buffer << std::cin.rdbuf(); | |
244 } else { | |
245 std::ifstream body_file{body_filename}; | |
246 if (!body_file) return false; | |
247 body_buffer << body_file.rdbuf(); | |
248 } | |
249 *body = body_buffer.str(); | |
250 return true; | |
251 } | |
252 | |
253 void PrintUsage(const char* exec_path) { | |
254 std::cerr << "Usage: " << exec_path | |
255 << " [filename.js|-]\n\n" | |
256 "No arguments or - reads from standard input.\n" | |
257 "Anything else is interpreted as a filename.\n\n" | |
258 "This tool is intended as a help in writing tests.\n" | |
259 "Please, DO NOT blindly copy and paste the output " | |
260 "into the test suite.\n"; | |
261 } | |
262 | |
263 } // namespace | |
264 | |
265 int main(int argc, char** argv) { | |
266 if (argc > 1 && strcmp(argv[1], "--help") == 0) { | |
267 PrintUsage(argv[0]); | |
268 return 0; | |
269 } | |
270 | |
271 const char* body_filename = (argc > 1 ? argv[1] : "-"); | |
272 const char* wrapper_function_name = "__genbckexp_wrapper__"; | |
273 | |
274 std::string body; | |
275 if (!ReadFromFileOrStdin(&body, body_filename)) { | |
276 std::cerr << "Could not open '" << body_filename << "'.\n\n"; | |
277 PrintUsage(argv[0]); | |
278 return 1; | |
279 } | |
280 | |
281 V8InitializationScope platform(argv[0]); | |
282 { | |
283 v8::Isolate::Scope isolate_scope(platform.isolate()); | |
284 v8::HandleScope handle_scope(platform.isolate()); | |
285 v8::Local<v8::Context> context = v8::Context::New(platform.isolate()); | |
286 v8::Context::Scope context_scope(context); | |
287 | |
288 std::string source_code = WrapCodeInFunction(wrapper_function_name, body); | |
289 CompileAndRun(platform.isolate(), context, source_code.c_str()); | |
290 | |
291 i::Handle<i::BytecodeArray> bytecode_array = GetBytecodeArrayForGlobal( | |
292 platform.isolate(), context, wrapper_function_name); | |
293 | |
294 PrintBytecodeArray(std::cout, bytecode_array, body); | |
295 } | |
296 } | |
OLD | NEW |