Skip to content

Commit

Permalink
Convert opcode maps to C++ source with macros.
Browse files Browse the repository at this point in the history
This removes the requirement to run a python script to preprocess the
input files before compiling pycdas/pycdc.
  • Loading branch information
zrax committed Dec 5, 2023
1 parent 20ac52d commit ab6aaf6
Show file tree
Hide file tree
Showing 58 changed files with 3,013 additions and 2,941 deletions.
51 changes: 27 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,13 @@ if (ENABLE_STACK_DEBUG)
add_definitions(-DSTACK_DEBUG)
endif()

# For generating the bytes tables
find_package(Python COMPONENTS Interpreter 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 312
)

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
56 changes: 28 additions & 28 deletions bytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#endif

#define DECLARE_PYTHON(maj, min) \
extern int python_##maj##min##_map(int);
extern int python_##maj##_##min##_map(int);

DECLARE_PYTHON(1, 0)
DECLARE_PYTHON(1, 1)
Expand Down Expand Up @@ -72,41 +72,41 @@ int Pyc::ByteToOpcode(int maj, int min, int opcode)
switch (maj) {
case 1:
switch (min) {
case 0: return python_10_map(opcode);
case 1: return python_11_map(opcode);
case 3: return python_13_map(opcode);
case 4: return python_14_map(opcode);
case 5: return python_15_map(opcode);
case 6: return python_16_map(opcode);
case 0: return python_1_0_map(opcode);
case 1: return python_1_1_map(opcode);
case 3: return python_1_3_map(opcode);
case 4: return python_1_4_map(opcode);
case 5: return python_1_5_map(opcode);
case 6: return python_1_6_map(opcode);
}
break;
case 2:
switch (min) {
case 0: return python_20_map(opcode);
case 1: return python_21_map(opcode);
case 2: return python_22_map(opcode);
case 3: return python_23_map(opcode);
case 4: return python_24_map(opcode);
case 5: return python_25_map(opcode);
case 6: return python_26_map(opcode);
case 7: return python_27_map(opcode);
case 0: return python_2_0_map(opcode);
case 1: return python_2_1_map(opcode);
case 2: return python_2_2_map(opcode);
case 3: return python_2_3_map(opcode);
case 4: return python_2_4_map(opcode);
case 5: return python_2_5_map(opcode);
case 6: return python_2_6_map(opcode);
case 7: return python_2_7_map(opcode);
}
break;
case 3:
switch (min) {
case 0: return python_30_map(opcode);
case 1: return python_31_map(opcode);
case 2: return python_32_map(opcode);
case 3: return python_33_map(opcode);
case 4: return python_34_map(opcode);
case 5: return python_35_map(opcode);
case 6: return python_36_map(opcode);
case 7: return python_37_map(opcode);
case 8: return python_38_map(opcode);
case 9: return python_39_map(opcode);
case 10: return python_310_map(opcode);
case 11: return python_311_map(opcode);
case 12: return python_312_map(opcode);
case 0: return python_3_0_map(opcode);
case 1: return python_3_1_map(opcode);
case 2: return python_3_2_map(opcode);
case 3: return python_3_3_map(opcode);
case 4: return python_3_4_map(opcode);
case 5: return python_3_5_map(opcode);
case 6: return python_3_6_map(opcode);
case 7: return python_3_7_map(opcode);
case 8: return python_3_8_map(opcode);
case 9: return python_3_9_map(opcode);
case 10: return python_3_10_map(opcode);
case 11: return python_3_11_map(opcode);
case 12: return python_3_12_map(opcode);
}
break;
}
Expand Down
14 changes: 14 additions & 0 deletions bytes/bytecode_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "bytecode.h"

#define BEGIN_MAP(maj, min) \
int python_##maj##_##min##_map(int id) \
{ \
switch (id) {

#define MAP_OP(op, name) \
case op: return Pyc::name;

#define END_MAP() \
default: return Pyc::PYC_INVALID_OPCODE; \
} \
}
53 changes: 0 additions & 53 deletions bytes/comp_map.py

This file was deleted.

85 changes: 0 additions & 85 deletions bytes/python_10.map

This file was deleted.

86 changes: 0 additions & 86 deletions bytes/python_11.map

This file was deleted.

Loading

0 comments on commit ab6aaf6

Please sign in to comment.