Index: test/cctest/interpreter/bytecode-expectations.cc |
diff --git a/test/cctest/interpreter/generate-bytecode-expectations.cc b/test/cctest/interpreter/bytecode-expectations.cc |
similarity index 65% |
copy from test/cctest/interpreter/generate-bytecode-expectations.cc |
copy to test/cctest/interpreter/bytecode-expectations.cc |
index 74c3407d286d9915b69bdd33bd1789c02f0732f1..35ecc8075669c66fd3e3536f0208558a6e1e9a1e 100644 |
--- a/test/cctest/interpreter/generate-bytecode-expectations.cc |
+++ b/test/cctest/interpreter/bytecode-expectations.cc |
@@ -2,7 +2,8 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include <fstream> |
+#include "test/cctest/interpreter/bytecode-expectations.h" |
+ |
#include <iostream> |
#include "include/libplatform/libplatform.h" |
@@ -17,20 +18,10 @@ |
#include "src/interpreter/bytecodes.h" |
#include "src/interpreter/interpreter.h" |
-using namespace i::interpreter; |
+using namespace v8::internal::interpreter; |
namespace { |
-const char* kIndent = " "; |
- |
-enum ConstantPoolType { |
- kConstantPoolTypeUnknown, |
- kConstantPoolTypeString, |
- kConstantPoolTypeInteger, |
- kConstantPoolTypeDouble, |
- kConstantPoolTypeMixed, |
-}; |
- |
class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator { |
public: |
void* Allocate(size_t length) override { |
@@ -129,28 +120,20 @@ i::Handle<v8::internal::BytecodeArray> GetBytecodeArrayForGlobal( |
return bytecodes; |
} |
-std::string QuoteCString(const std::string& source) { |
- std::string quoted_buffer; |
- for (char c : source) { |
+void PrintEscapedString(std::ostream& stream, const std::string& string) { |
+ for (char c : string) { |
switch (c) { |
case '"': |
- quoted_buffer += "\\\""; |
- break; |
- case '\n': |
- quoted_buffer += "\\n"; |
- break; |
- case '\t': |
- quoted_buffer += "\\t"; |
+ stream << "\\\""; |
break; |
case '\\': |
- quoted_buffer += "\\\\"; |
+ stream << "\\\\"; |
break; |
default: |
- quoted_buffer += c; |
+ stream << c; |
break; |
} |
} |
- return quoted_buffer; |
} |
void PrintBytecodeOperand(std::ostream& stream, |
@@ -255,69 +238,59 @@ void PrintFrameSize(std::ostream& stream, |
const int kPointerSize = sizeof(void*); |
int frame_size = bytecode_array->frame_size(); |
- stream << kIndent; |
- |
- DCHECK(frame_size % kPointerSize == 0); |
- if (frame_size > kPointerSize) { |
- stream << ' ' << frame_size / kPointerSize << " * kPointerSize,\n" |
- << kIndent; |
- } else if (frame_size == kPointerSize) { |
- stream << " kPointerSize,\n" << kIndent; |
- } else if (frame_size == 0) { |
- stream << " 0,\n" << kIndent; |
- } |
- |
- stream << ' ' << bytecode_array->parameter_count() << ",\n"; |
+ DCHECK_EQ(frame_size % kPointerSize, 0); |
+ stream << "frame size: " << frame_size / kPointerSize; |
+ if (frame_size > 0) stream << " # in multiples of sizeof(void*)"; |
+ stream << "\nparameter count: " << bytecode_array->parameter_count() << '\n'; |
} |
void PrintBytecodeSequence(std::ostream& stream, |
i::Handle<i::BytecodeArray> bytecode_array) { |
- stream << kIndent << ' ' << bytecode_array->length() << ",\n" |
- << kIndent << " {\n" |
- << kIndent << " "; |
- |
+ stream << "bytecodes: [\n"; |
BytecodeArrayIterator bytecode_iter{bytecode_array}; |
for (; !bytecode_iter.done(); bytecode_iter.Advance()) { |
- // Print separator before each instruction, except the first one. |
- if (bytecode_iter.current_offset() > 0) { |
- stream << ",\n" << kIndent << " "; |
- } |
+ stream << " "; |
PrintBytecode(stream, bytecode_iter); |
+ stream << ",\n"; |
} |
+ stream << "]\n"; |
} |
void PrintConstantPool(std::ostream& stream, i::FixedArray* constant_pool, |
ConstantPoolType expected_constant_type, |
v8::Isolate* isolate) { |
+ stream << "constant pool: [\n"; |
int num_constants = constant_pool->length(); |
- stream << "\n" << kIndent << " },\n" << kIndent << ' ' << num_constants; |
if (num_constants > 0) { |
- stream << ",\n" << kIndent << " {"; |
for (int i = 0; i < num_constants; ++i) { |
- // Print separator before each constant, except the first one |
- if (i != 0) stream << ", "; |
+ stream << " "; |
PrintConstant( |
stream, expected_constant_type, |
i::FixedArray::get(constant_pool, i, GetInternalIsolate(isolate))); |
+ stream << ",\n"; |
} |
- stream << '}'; |
} |
- stream << '\n'; |
+ stream << "]\n"; |
+} |
+ |
+void PrintCodeSnippet(std::ostream& stream, const std::string& body) { |
+ stream << "snippet: \"\n"; |
+ std::stringstream body_stream{body}; |
+ std::string body_line; |
+ while (std::getline(body_stream, body_line)) { |
+ stream << " "; |
+ PrintEscapedString(stream, body_line); |
+ stream << '\n'; |
+ } |
+ stream << "\"\n"; |
} |
void PrintBytecodeArray(std::ostream& stream, |
i::Handle<i::BytecodeArray> bytecode_array, |
const std::string& body, v8::Isolate* isolate, |
- ConstantPoolType constant_pool_type, |
- bool print_banner = true) { |
- if (print_banner) { |
- stream << kIndent << "// === ExpectedSnippet generated by " |
- "generate-bytecode-expectations. ===\n"; |
- } |
- |
- // Print the code snippet as a quoted C string. |
- stream << kIndent << "{" << '"' << QuoteCString(body) << "\",\n"; |
- |
+ ConstantPoolType constant_pool_type) { |
+ stream << "\n---\n"; |
+ PrintCodeSnippet(stream, body); |
PrintFrameSize(stream, bytecode_array); |
PrintBytecodeSequence(stream, bytecode_array); |
PrintConstantPool(stream, bytecode_array->constant_pool(), constant_pool_type, |
@@ -327,92 +300,48 @@ void PrintBytecodeArray(std::ostream& stream, |
i::HandlerTable* handlers = |
i::HandlerTable::cast(bytecode_array->handler_table()); |
CHECK_EQ(handlers->NumberOfRangeEntries(), 0); |
- |
- stream << kIndent << "}\n"; |
} |
-void PrintExpectedSnippet(ConstantPoolType constant_pool_type, char* exec_path, |
- std::string body) { |
+void PrintExpectation(std::ostream& stream, const std::string& snippet, |
+ ConstantPoolType const_pool_type, |
+ v8::Local<v8::Context> context, v8::Isolate* isolate) { |
const char* wrapper_function_name = "__genbckexp_wrapper__"; |
- V8InitializationScope platform(exec_path); |
- { |
- v8::Isolate::Scope isolate_scope(platform.isolate()); |
- v8::HandleScope handle_scope(platform.isolate()); |
- v8::Local<v8::Context> context = v8::Context::New(platform.isolate()); |
- v8::Context::Scope context_scope(context); |
- |
- std::string source_code = WrapCodeInFunction(wrapper_function_name, body); |
- CompileAndRun(platform.isolate(), context, source_code.c_str()); |
- |
- i::Handle<i::BytecodeArray> bytecode_array = GetBytecodeArrayForGlobal( |
- platform.isolate(), context, wrapper_function_name); |
- |
- PrintBytecodeArray(std::cout, bytecode_array, body, platform.isolate(), |
- constant_pool_type); |
- } |
-} |
- |
-bool ReadFromFileOrStdin(std::string* body, const char* body_filename) { |
- std::stringstream body_buffer; |
- if (strcmp(body_filename, "-") == 0) { |
- body_buffer << std::cin.rdbuf(); |
- } else { |
- std::ifstream body_file{body_filename}; |
- if (!body_file) return false; |
- body_buffer << body_file.rdbuf(); |
- } |
- *body = body_buffer.str(); |
- return true; |
-} |
+ std::string source_code = WrapCodeInFunction(wrapper_function_name, snippet); |
+ CompileAndRun(isolate, context, source_code.c_str()); |
-ConstantPoolType ParseConstantPoolType(const char* type_string) { |
- if (strcmp(type_string, "int") == 0) { |
- return kConstantPoolTypeInteger; |
- } else if (strcmp(type_string, "double") == 0) { |
- return kConstantPoolTypeDouble; |
- } else if (strcmp(type_string, "string") == 0) { |
- return kConstantPoolTypeString; |
- } else if (strcmp(type_string, "mixed") == 0) { |
- return kConstantPoolTypeMixed; |
- } |
- return kConstantPoolTypeUnknown; |
-} |
+ i::Handle<i::BytecodeArray> bytecode_array = |
+ GetBytecodeArrayForGlobal(isolate, context, wrapper_function_name); |
-void PrintUsage(const char* exec_path) { |
- std::cerr << "Usage: " << exec_path |
- << " (int|double|string|mixed) [filename.js|-]\n\n" |
- "First argument is the type of objects in the constant pool.\n\n" |
- "Omitting the second argument or - reads from standard input.\n" |
- "Anything else is interpreted as a filename.\n\n" |
- "This tool is intended as a help in writing tests.\n" |
- "Please, DO NOT blindly copy and paste the output " |
- "into the test suite.\n"; |
+ PrintBytecodeArray(std::cout, bytecode_array, snippet, isolate, |
+ const_pool_type); |
} |
} // namespace |
-int main(int argc, char** argv) { |
- if (argc < 2) { |
- PrintUsage(argv[0]); |
- return 1; |
- } |
+namespace v8 { |
+namespace internal { |
+namespace interpreter { |
- if (argc > 1 && strcmp(argv[1], "--help") == 0) { |
- PrintUsage(argv[0]); |
- return 0; |
- } |
+void GenerateExpectationsFile(std::ostream& stream, |
+ const std::vector<TestInput>& test_list, |
+ const char* exec_path) { |
+ V8InitializationScope platform(exec_path); |
+ { |
+ v8::Isolate::Scope isolate_scope{platform.isolate()}; |
+ v8::HandleScope handle_scope{platform.isolate()}; |
+ v8::Local<v8::Context> context = v8::Context::New(platform.isolate()); |
+ v8::Context::Scope context_scope{context}; |
- const char* body_filename = (argc > 2 ? argv[2] : "-"); |
- const char* const_pool_type_string = argv[1]; |
+ stream << "#\n# Autogenerated by generate-bytecode-expectations\n#\n"; |
- std::string body; |
- if (!ReadFromFileOrStdin(&body, body_filename)) { |
- std::cerr << "Could not open '" << body_filename << "'.\n\n"; |
- PrintUsage(argv[0]); |
- return 1; |
+ for (const TestInput& test : test_list) { |
+ PrintExpectation(stream, test.snippet, test.const_pool_type, context, |
+ platform.isolate()); |
+ } |
} |
- |
- PrintExpectedSnippet(ParseConstantPoolType(const_pool_type_string), argv[0], |
- body); |
} |
+ |
+} // namespace interpreter |
+} // namespace internal |
+} // namespace v8 |