Reviewers: Kevin Millikin, Message: We may use something like this to emit the IR as comments. Tests pass with --debug_code.
But I'm not 100% sure if I'm doing the right thing for allocating the comment
strings. Description: Add the possibility to emit formatted comments in debug mode. This change makes the comment class more powerful. It is now possible to emit printf-formatted comments in generated code in debug mode. E.g. Comment cmnt(masm_, "number=%d", n); Comments longer than the maximum length (80 currently) are truncated. Please review this at http://codereview.chromium.org/549221 SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/ Affected files: M src/codegen-inl.h M src/ia32/codegen-ia32.cc M src/token.h M src/x64/codegen-x64.cc Index: src/codegen-inl.h =================================================================== --- src/codegen-inl.h (revision 3750) +++ src/codegen-inl.h (working copy) @@ -58,17 +58,32 @@ class Comment BASE_EMBEDDED { public: - Comment(MacroAssembler* masm, const char* msg) : masm_(masm), msg_(msg) { - __ RecordComment(msg); + Comment(MacroAssembler* masm, const char* format, ...) + : masm_(masm), format_(format) { + EmbeddedVector<char, kMaxCommentLength> tmp_str; + va_list args; + va_start(args, format); + int num_printed = OS::VSNPrintF(tmp_str, format, args); + va_end(args); + // Allocate just enough space for the output + the terminating \0. + // Output longer than the maximum length is truncated. + int num_alloc = num_printed < 0 ? kMaxCommentLength : num_printed + 1; + Vector<char> buffer = Vector<char>::New(num_alloc); + OS::StrNCpy(buffer, tmp_str.start(), num_alloc); + __ RecordComment(buffer.start()); } ~Comment() { - if (msg_[0] == '[') __ RecordComment("]"); + if (format_[0] == '[') __ RecordComment("]"); } private: + + // Comments longer than this (incl. the terminating \0) are truncated. + static const int kMaxCommentLength = 80; + MacroAssembler* masm_; - const char* msg_; + const char* format_; }; #else Index: src/ia32/codegen-ia32.cc =================================================================== --- src/ia32/codegen-ia32.cc (revision 3750) +++ src/ia32/codegen-ia32.cc (working copy) @@ -930,7 +930,7 @@ StaticType* type, OverwriteMode overwrite_mode) { Comment cmnt(masm_, "[ BinaryOperation"); - Comment cmnt_token(masm_, Token::String(op)); + Comment cmnt_token(masm_, Token::FormatString(op)); if (op == Token::COMMA) { // Simply discard left value. Index: src/token.h =================================================================== --- src/token.h (revision 3750) +++ src/token.h (working copy) @@ -252,6 +252,15 @@ return string_[tok]; } + // Returns a string corresponding to the JS token string + // which is suitable for passing as a format string to sprintf. + // (i.e. with escaped % characters) + static const char* FormatString(Value tok) { + if (tok == Token::MOD) return "%%"; + if (tok == Token::ASSIGN_MOD) return "%%="; + return String(tok); + } + // Returns the precedence > 0 for binary and compare // operators; returns 0 otherwise. static int Precedence(Value tok) { Index: src/x64/codegen-x64.cc =================================================================== --- src/x64/codegen-x64.cc (revision 3750) +++ src/x64/codegen-x64.cc (working copy) @@ -5074,7 +5074,7 @@ StaticType* type, OverwriteMode overwrite_mode) { Comment cmnt(masm_, "[ BinaryOperation"); - Comment cmnt_token(masm_, Token::String(op)); + Comment cmnt_token(masm_, Token::FormatString(op)); if (op == Token::COMMA) { // Simply discard left value. -- v8-dev mailing list [email protected] http://groups.google.com/group/v8-dev
