Skip to content

Commit

Permalink
Merge branch 'zrax:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
merdw authored Feb 3, 2024
2 parents 40008e2 + 2da061f commit 8381017
Show file tree
Hide file tree
Showing 77 changed files with 3,610 additions and 4,075 deletions.
45 changes: 37 additions & 8 deletions ASTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
break;
case Pyc::CALL_A:
case Pyc::CALL_FUNCTION_A:
case Pyc::INSTRUMENTED_CALL_A:
{
int kwparams = (operand & 0xFF00) >> 8;
int pparams = (operand & 0xFF);
Expand All @@ -431,8 +432,8 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
bases.resize(basecnt);
PycRef<ASTNode> TOS = stack.top();
int TOS_type = TOS.type();
// bases are NODE_NAME at TOS
while (TOS_type == ASTNode::NODE_NAME) {
// bases are NODE_NAME and NODE_BINARY at TOS
while (TOS_type == ASTNode::NODE_NAME || TOS_type == ASTNode::NODE_BINARY) {
bases.resize(basecnt + 1);
bases[basecnt] = TOS;
basecnt++;
Expand Down Expand Up @@ -508,8 +509,10 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}
PycRef<ASTNode> func = stack.top();
stack.pop();
if (opcode == Pyc::CALL_A && stack.top() == nullptr)
if ((opcode == Pyc::CALL_A || opcode == Pyc::INSTRUMENTED_CALL_A) &&
stack.top() == nullptr) {
stack.pop();
}

stack.push(new ASTCall(func, pparamList, kwparamList));
}
Expand Down Expand Up @@ -635,7 +638,10 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.pop();
PycRef<ASTNode> left = stack.top();
stack.pop();
stack.push(new ASTCompare(left, right, operand));
auto arg = operand;
if (mod->verCompare(3, 12) >= 0)
arg >>= 4; // changed under GH-100923
stack.push(new ASTCompare(left, right, arg));
}
break;
case Pyc::CONTAINS_OP_A:
Expand Down Expand Up @@ -880,6 +886,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}
break;
case Pyc::FOR_ITER_A:
case Pyc::INSTRUMENTED_FOR_ITER_A:
{
PycRef<ASTNode> iter = stack.top(); // Iterable
stack.pop();
Expand Down Expand Up @@ -1041,6 +1048,8 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
case Pyc::POP_JUMP_FORWARD_IF_FALSE_A:
case Pyc::JUMP_IF_NOT_EXC_MATCH_A:
case Pyc::POP_JUMP_FORWARD_IF_TRUE_A:
case Pyc::INSTRUMENTED_POP_JUMP_IF_FALSE_A:
case Pyc::INSTRUMENTED_POP_JUMP_IF_TRUE_A:
{
PycRef<ASTNode> cond = stack.top();
PycRef<ASTCondBlock> ifblk;
Expand All @@ -1049,7 +1058,9 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (opcode == Pyc::POP_JUMP_IF_FALSE_A
|| opcode == Pyc::POP_JUMP_IF_TRUE_A
|| opcode == Pyc::POP_JUMP_FORWARD_IF_FALSE_A
|| opcode == Pyc::POP_JUMP_FORWARD_IF_TRUE_A) {
|| opcode == Pyc::POP_JUMP_FORWARD_IF_TRUE_A
|| opcode == Pyc::INSTRUMENTED_POP_JUMP_IF_FALSE_A
|| opcode == Pyc::INSTRUMENTED_POP_JUMP_IF_TRUE_A) {
/* Pop condition before the jump */
stack.pop();
popped = ASTCondBlock::PRE_POPPED;
Expand All @@ -1069,12 +1080,14 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
bool neg = opcode == Pyc::JUMP_IF_TRUE_A
|| opcode == Pyc::JUMP_IF_TRUE_OR_POP_A
|| opcode == Pyc::POP_JUMP_IF_TRUE_A
|| opcode == Pyc::POP_JUMP_FORWARD_IF_TRUE_A;
|| opcode == Pyc::POP_JUMP_FORWARD_IF_TRUE_A
|| opcode == Pyc::INSTRUMENTED_POP_JUMP_IF_TRUE_A;

int offs = operand;
if (mod->verCompare(3, 10) >= 0)
offs *= sizeof(uint16_t); // // BPO-27129
if (opcode == Pyc::JUMP_IF_FALSE_A
if (mod->verCompare(3, 12) >= 0
|| opcode == Pyc::JUMP_IF_FALSE_A
|| opcode == Pyc::JUMP_IF_TRUE_A
|| opcode == Pyc::POP_JUMP_FORWARD_IF_TRUE_A
|| opcode == Pyc::POP_JUMP_FORWARD_IF_FALSE_A) {
Expand Down Expand Up @@ -1271,6 +1284,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}
break;
case Pyc::JUMP_FORWARD_A:
case Pyc::INSTRUMENTED_JUMP_FORWARD_A:
{
int offs = operand;
if (mod->verCompare(3, 10) >= 0)
Expand Down Expand Up @@ -1783,6 +1797,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}
break;
case Pyc::RETURN_VALUE:
case Pyc::INSTRUMENTED_RETURN_VALUE_A:
{
PycRef<ASTNode> value = stack.top();
stack.pop();
Expand All @@ -1804,6 +1819,13 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}
}
break;
case Pyc::RETURN_CONST_A:
case Pyc::INSTRUMENTED_RETURN_CONST_A:
{
PycRef<ASTObject> value = new ASTObject(code->getConst(operand));
curblock->append(new ASTReturn(value.cast<ASTNode>()));
}
break;
case Pyc::ROT_TWO:
{
PycRef<ASTNode> one = stack.top();
Expand Down Expand Up @@ -2405,6 +2427,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}
break;
case Pyc::YIELD_VALUE:
case Pyc::INSTRUMENTED_YIELD_VALUE_A:
{
PycRef<ASTNode> value = stack.top();
stack.pop();
Expand All @@ -2416,6 +2439,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
break;
case Pyc::PRECALL_A:
case Pyc::RESUME_A:
case Pyc::INSTRUMENTED_RESUME_A:
/* We just entirely ignore this / no-op */
break;
case Pyc::CACHE:
Expand All @@ -2427,6 +2451,9 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
case Pyc::PUSH_NULL:
stack.push(nullptr);
break;
case Pyc::GEN_START_A:
stack.pop();
break;
default:
fprintf(stderr, "Unsupporteddwhere opcode: %s\n", Pyc::OpcodeName(opcode & 0xFF));
cleanBuild = false;
Expand Down Expand Up @@ -3352,7 +3379,9 @@ void decompyle(PycRef<PycCode> code, PycModule* mod, std::ostream& pyc_output)
if (clean->nodes().back().type() == ASTNode::NODE_RETURN) {
PycRef<ASTReturn> ret = clean->nodes().back().cast<ASTReturn>();

if (ret->value() == NULL || ret->value().type() == ASTNode::NODE_LOCALS) {
PycRef<ASTObject> retObj = ret->value().try_cast<ASTObject>();
if (ret->value() == NULL || ret->value().type() == ASTNode::NODE_LOCALS ||
(retObj && retObj->object().type() == PycObject::TYPE_NONE)) {
clean->removeLast(); // Always an extraneous return statement
}
}
Expand Down
53 changes: 28 additions & 25 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.12)
project(pycdc)

set(CMAKE_CXX_STANDARD 11)
Expand All @@ -16,36 +16,13 @@ if (ENABLE_STACK_DEBUG)
add_definitions(-DSTACK_DEBUG)
endif()

# For generating the bytes tables
find_package(PythonInterp REQUIRED)

if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wshadow -Werror ${CMAKE_CXX_FLAGS}")
elseif(MSVC)
set(CMAKE_CXX_FLAGS "/WX ${CMAKE_CXX_FLAGS}")
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()

set(PYTHON_VERSIONS
10 11 13 14 15 16 # Python 1.1 and 1.2 are marshal-identical
20 21 22 23 24 25 26 27
30 31 32 33 34 35 36 37 38 39 310 311
)

foreach(ver ${PYTHON_VERSIONS})
list(APPEND MAP_FILES ${CMAKE_CURRENT_SOURCE_DIR}/bytes/python_${ver}.map)
list(APPEND MAP_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/bytes/python_${ver}.cpp)
endforeach()

add_custom_command(OUTPUT ${MAP_SOURCES}
COMMAND ${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/bytes/comp_map.py
${CMAKE_CURRENT_SOURCE_DIR}/bytes
${CMAKE_CURRENT_BINARY_DIR}/bytes
DEPENDS ${MAP_FILES}
${CMAKE_CURRENT_SOURCE_DIR}/bytes/comp_map.py
)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

add_library(pycxx STATIC
Expand All @@ -57,7 +34,33 @@ add_library(pycxx STATIC
pyc_object.cpp
pyc_sequence.cpp
pyc_string.cpp
${MAP_SOURCES}
bytes/python_1_0.cpp
bytes/python_1_1.cpp
bytes/python_1_3.cpp
bytes/python_1_4.cpp
bytes/python_1_5.cpp
bytes/python_1_6.cpp
bytes/python_2_0.cpp
bytes/python_2_1.cpp
bytes/python_2_2.cpp
bytes/python_2_3.cpp
bytes/python_2_4.cpp
bytes/python_2_5.cpp
bytes/python_2_6.cpp
bytes/python_2_7.cpp
bytes/python_3_0.cpp
bytes/python_3_1.cpp
bytes/python_3_2.cpp
bytes/python_3_3.cpp
bytes/python_3_4.cpp
bytes/python_3_5.cpp
bytes/python_3_6.cpp
bytes/python_3_7.cpp
bytes/python_3_8.cpp
bytes/python_3_9.cpp
bytes/python_3_10.cpp
bytes/python_3_11.cpp
bytes/python_3_12.cpp
)

add_executable(pycdas pycdas.cpp)
Expand Down
Loading

0 comments on commit 8381017

Please sign in to comment.