Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(460)

Side by Side Diff: test/cctest/interpreter/generate-bytecode-expectations.cc

Issue 1671863002: [Interpreter] Initial generate-bytecode-expectations implementation. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Moved to interpreter/ Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/cctest/cctest.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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> V8StringFromUTF8(v8::Isolate* isolate, const char* data) {
82 return v8::String::NewFromUtf8(isolate, data, v8::NewStringType::kNormal)
83 .ToLocalChecked();
84 }
85
86 std::string WrapCodeInFunction(const char* function_name,
87 const std::string& function_body) {
88 std::ostringstream program_stream;
89 program_stream << "function " << function_name << "() {" << function_body
90 << "}\n"
91 << function_name << "();";
92
93 return program_stream.str();
94 }
95
96 v8::Local<v8::Value> CompileAndRun(v8::Isolate* isolate,
97 const v8::Local<v8::Context>& context,
98 const char* program) {
99 v8::Local<v8::String> source = V8StringFromUTF8(isolate, program);
100 v8::Local<v8::Script> script =
101 v8::Script::Compile(context, source).ToLocalChecked();
102
103 v8::Local<v8::Value> result;
104 CHECK(script->Run(context).ToLocal(&result));
105
106 return result;
107 }
108
109 i::Handle<v8::internal::BytecodeArray> GetBytecodeArrayForGlobal(
110 v8::Isolate* isolate, const v8::Local<v8::Context>& context,
111 const char* global_name) {
112 v8::Local<v8::String> v8_global_name = V8StringFromUTF8(isolate, global_name);
113 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(
114 context->Global()->Get(context, v8_global_name).ToLocalChecked());
115 i::Handle<i::JSFunction> js_function =
116 i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*function));
117
118 i::Handle<i::BytecodeArray> bytecodes = i::handle(
119 js_function->shared()->bytecode_array(), GetInternalIsolate(isolate));
120
121 return bytecodes;
122 }
123
124 void PrintBytecodeOperand(std::ostream& stream,
125 const BytecodeArrayIterator& bytecode_iter,
126 const Bytecode& bytecode, int op_index) {
127 OperandType op_type = Bytecodes::GetOperandType(bytecode, op_index);
128 OperandSize op_size = Bytecodes::GetOperandSize(bytecode, op_index);
129
130 const char* size_tag;
131 switch (op_size) {
132 case OperandSize::kByte:
133 size_tag = "8";
134 break;
135 case OperandSize::kShort:
136 size_tag = "16";
137 break;
138 default:
139 UNREACHABLE();
140 return;
141 }
142
143 if (Bytecodes::IsRegisterOperandType(op_type)) {
144 Register register_value = bytecode_iter.GetRegisterOperand(op_index);
145 stream << 'R';
146 if (op_size != OperandSize::kByte) stream << size_tag;
147 stream << '(' << register_value.index() << ')';
148 } else {
149 uint32_t raw_value = bytecode_iter.GetRawOperand(op_index, op_type);
150 stream << 'U' << size_tag << '(' << raw_value << ')';
151 }
152 }
153
154 void PrintBytecode(std::ostream& stream,
155 const BytecodeArrayIterator& bytecode_iter) {
156 Bytecode bytecode = bytecode_iter.current_bytecode();
157
158 stream << "B(" << Bytecodes::ToString(bytecode) << ')';
159
160 int operands_count = Bytecodes::NumberOfOperands(bytecode);
161 for (int op_index = 0; op_index < operands_count; ++op_index) {
162 stream << ", ";
163 PrintBytecodeOperand(stream, bytecode_iter, bytecode, op_index);
164 }
165 }
166
167 std::string QuoteCString(const std::string& source) {
168 std::string quoted_buffer;
169 for (char c : source) {
170 switch (c) {
171 case '"':
172 quoted_buffer += "\\\"";
173 break;
174 case '\n':
175 quoted_buffer += "\\n";
176 break;
177 case '\t':
178 quoted_buffer += "\\t";
179 break;
180 case '\\':
181 quoted_buffer += "\\\\";
182 break;
183 default:
184 quoted_buffer += c;
185 break;
186 }
187 }
188 return quoted_buffer;
189 }
190
191 void PrintBytecodeArray(std::ostream& stream,
192 i::Handle<i::BytecodeArray> bytecode_array,
193 const std::string& body, bool print_banner = true) {
194 const int kPointerSize = sizeof(void*);
195
196 if (print_banner) {
197 stream << kIndent << "// === ExpectedSnippet generated by "
198 "generate-bytecode-expectations. ===\n";
199 }
200
201 // Print the code snippet as a quoted C string.
202 stream << kIndent << "{" << '"' << QuoteCString(body) << "\",\n" << kIndent;
203
204 // Print the frame size, in multiples of kPointerSize.
205 int frame_size = bytecode_array->frame_size();
206 DCHECK(frame_size % kPointerSize == 0);
207 if (frame_size > kPointerSize) {
208 stream << ' ' << frame_size / kPointerSize << " * kPointerSize,\n"
209 << kIndent;
210 } else if (frame_size == kPointerSize) {
211 stream << " kPointerSize,\n" << kIndent;
212 } else if (frame_size == 0) {
213 stream << " 0,\n" << kIndent;
214 }
215
216 // Print parameter count and size of the bytecode array.
217 stream << ' ' << bytecode_array->parameter_count() << ",\n"
218 << kIndent << ' ' << bytecode_array->length() << ",\n"
219 << kIndent << " {\n"
220 << kIndent << " ";
221
222 // Print bytecodes.
223 BytecodeArrayIterator bytecode_iter{bytecode_array};
224 for (; !bytecode_iter.done(); bytecode_iter.Advance()) {
225 // Print separator before each instruction, except the first one.
226 if (bytecode_iter.current_offset() > 0) {
227 stream << ",\n" << kIndent << " ";
228 }
229 PrintBytecode(stream, bytecode_iter);
230 }
231
232 stream << "\n" << kIndent << " },\n";
233 // TODO(ssanfilippo) add representation of constant pool and handlers.
234 stream << kIndent << " // constant pool and handlers here\n";
235 stream << kIndent << "}\n";
236 }
237
238 bool ReadFromFileOrStdin(std::string* body, const char* body_filename) {
239 std::stringstream body_buffer;
240 if (strcmp(body_filename, "-") == 0) {
241 body_buffer << std::cin.rdbuf();
242 } else {
243 std::ifstream body_file{body_filename};
244 if (!body_file) return false;
245 body_buffer << body_file.rdbuf();
246 }
247 *body = body_buffer.str();
248 return true;
249 }
250
251 void PrintUsage(const char* exec_path) {
252 std::cerr << "Usage: " << exec_path
253 << " [filename.js|-]\n\n"
254 "No arguments or - reads from standard input.\n"
255 "Anything else is interpreted as a filename.\n\n"
256 "This tool is intended as a help in writing tests.\n"
257 "Please, DO NOT blindly copy and paste the output "
258 "into the test suite.\n";
259 }
260
261 } // namespace
262
263 int main(int argc, char** argv) {
264 if (argc > 1 && strcmp(argv[1], "--help") == 0) {
265 PrintUsage(argv[0]);
266 return 0;
267 }
268
269 const char* body_filename = (argc > 1 ? argv[1] : "-");
270 const char* wrapper_function_name = "__genbckexp_wrapper__";
271
272 std::string body;
273 if (!ReadFromFileOrStdin(&body, body_filename)) {
274 std::cerr << "Could not open '" << body_filename << "'.\n\n";
275 PrintUsage(argv[0]);
276 return 1;
277 }
278
279 V8InitializationScope platform(argv[0]);
280 {
281 v8::Isolate::Scope isolate_scope(platform.isolate());
282 v8::HandleScope handle_scope(platform.isolate());
283 v8::Local<v8::Context> context = v8::Context::New(platform.isolate());
284 v8::Context::Scope context_scope(context);
285
286 std::string source_code = WrapCodeInFunction(wrapper_function_name, body);
287 CompileAndRun(platform.isolate(), context, source_code.c_str());
288
289 i::Handle<i::BytecodeArray> bytecode_array = GetBytecodeArrayForGlobal(
290 platform.isolate(), context, wrapper_function_name);
291
292 PrintBytecodeArray(std::cout, bytecode_array, body);
293 }
294 }
OLDNEW
« no previous file with comments | « test/cctest/cctest.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698