Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug output of asm instructions in pycdc #272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions ASTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,23 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
bool variable_annotations = false;

while (!source.atEof()) {
#if defined(BLOCK_DEBUG) || defined(STACK_DEBUG)
curpos = pos;
bc_next(source, mod, opcode, operand, pos);

#if defined(BLOCK_DEBUG) || defined(STACK_DEBUG) || defined(ASM_DEBUG)
fprintf(stderr, "%-7d", pos);
#ifdef ASM_DEBUG
{
const int asm_column_width = 40;
std::string s = bc_instruction_to_string(code, mod, pos, opcode, operand).c_str();
if (s.size() > asm_column_width) {
// fixed-size column
s = s.substr(0, asm_column_width - 3) + "...";
}
s.insert(s.end(), asm_column_width - s.size(), ' ');
fputs(s.c_str(), stderr);
}
#endif
#ifdef STACK_DEBUG
fprintf(stderr, "%-5d", (unsigned int)stack_hist.size() + 1);
#endif
Expand All @@ -101,9 +116,6 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
fprintf(stderr, "\n");
#endif

curpos = pos;
bc_next(source, mod, opcode, operand, pos);

if (need_try && opcode != Pyc::SETUP_EXCEPT_A) {
need_try = false;

Expand Down Expand Up @@ -2763,9 +2775,12 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
print_formatted_value(val.cast<ASTFormattedValue>(), mod);
break;
case ASTNode::NODE_OBJECT:
// When printing a piece of the f-string, keep the quote style consistent.
// This avoids problems when ''' or """ is part of the string.
print_const(val.cast<ASTObject>()->object(), mod, F_STRING_QUOTE);
{
// When printing a piece of the f-string, keep the quote style consistent.
// This avoids problems when ''' or """ is part of the string.
std::string s = const_to_string(val.cast<ASTObject>()->object(), mod, F_STRING_QUOTE);
fputs(s.c_str(), pyc_output);
}
break;
default:
fprintf(stderr, "Unsupported node type %d in NODE_JOINEDSTR\n", val.type());
Expand Down Expand Up @@ -2925,7 +2940,8 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
PycRef<PycCode> code = obj.cast<PycCode>();
decompyle(code, mod);
} else {
print_const(obj, mod);
std::string s = const_to_string(obj, mod);
fputs(s.c_str(), pyc_output);
}
}
break;
Expand Down Expand Up @@ -3343,8 +3359,8 @@ bool print_docstring(PycRef<PycObject> obj, int indent, PycModule* mod)
}
if (prefix != -1) {
start_line(indent);
OutputString(obj.cast<PycString>(), prefix, true);
fputs("\n", pyc_output);
std::string s = OutputString(obj.cast<PycString>(), prefix, true) + "\n";
fputs(s.c_str(), pyc_output);
return true;
} else
return false;
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Debug options.
option(ENABLE_BLOCK_DEBUG "Enable block debugging" OFF)
option(ENABLE_STACK_DEBUG "Enable stack debugging" OFF)
option(ENABLE_ASM_DEBUG "Enable assembly debugging" OFF)

# Turn debug defs on if they're enabled.
if (ENABLE_BLOCK_DEBUG)
Expand All @@ -15,6 +16,9 @@ endif()
if (ENABLE_STACK_DEBUG)
add_definitions(-DSTACK_DEBUG)
endif()
if (ENABLE_ASM_DEBUG)
add_definitions(-DASM_DEBUG)
endif()

# For generating the bytes tables
find_package(PythonInterp REQUIRED)
Expand Down
Loading