2011-10-23 17:48:10 -07:00
|
|
|
#include "pyc_numeric.h"
|
2009-07-24 08:35:21 +00:00
|
|
|
#include "bytecode.h"
|
2021-08-16 15:25:09 +00:00
|
|
|
#include <stdexcept>
|
2023-05-07 13:28:10 -07:00
|
|
|
#include <cstdint>
|
2020-10-15 20:57:56 -04:00
|
|
|
#include <cmath>
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2010-11-07 15:39:36 -08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
2010-09-04 01:20:41 -07:00
|
|
|
#define DECLARE_PYTHON(maj, min) \
|
|
|
|
extern int python_##maj##min##_map(int); \
|
|
|
|
extern int python_##maj##min##_unmap(int);
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2010-09-04 01:20:41 -07:00
|
|
|
DECLARE_PYTHON(1, 0)
|
|
|
|
DECLARE_PYTHON(1, 1)
|
|
|
|
DECLARE_PYTHON(1, 3)
|
|
|
|
DECLARE_PYTHON(1, 4)
|
|
|
|
DECLARE_PYTHON(1, 5)
|
|
|
|
DECLARE_PYTHON(1, 6)
|
|
|
|
DECLARE_PYTHON(2, 0)
|
|
|
|
DECLARE_PYTHON(2, 1)
|
|
|
|
DECLARE_PYTHON(2, 2)
|
|
|
|
DECLARE_PYTHON(2, 3)
|
|
|
|
DECLARE_PYTHON(2, 4)
|
|
|
|
DECLARE_PYTHON(2, 5)
|
|
|
|
DECLARE_PYTHON(2, 6)
|
|
|
|
DECLARE_PYTHON(2, 7)
|
|
|
|
DECLARE_PYTHON(3, 0)
|
|
|
|
DECLARE_PYTHON(3, 1)
|
2011-01-06 17:15:48 -08:00
|
|
|
DECLARE_PYTHON(3, 2)
|
2012-05-26 13:50:23 -07:00
|
|
|
DECLARE_PYTHON(3, 3)
|
2013-11-27 14:10:05 -08:00
|
|
|
DECLARE_PYTHON(3, 4)
|
2015-10-01 16:06:09 -07:00
|
|
|
DECLARE_PYTHON(3, 5)
|
2016-06-22 22:06:09 -07:00
|
|
|
DECLARE_PYTHON(3, 6)
|
2017-12-20 13:15:03 -08:00
|
|
|
DECLARE_PYTHON(3, 7)
|
2019-02-08 16:06:55 -08:00
|
|
|
DECLARE_PYTHON(3, 8)
|
2020-09-29 09:28:15 -07:00
|
|
|
DECLARE_PYTHON(3, 9)
|
2021-10-10 00:52:41 +05:30
|
|
|
DECLARE_PYTHON(3, 10)
|
2022-12-02 16:34:58 -08:00
|
|
|
DECLARE_PYTHON(3, 11)
|
2023-11-09 11:11:37 -08:00
|
|
|
DECLARE_PYTHON(3, 12)
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2010-09-04 01:20:41 -07:00
|
|
|
const char* Pyc::OpcodeName(int opcode)
|
|
|
|
{
|
|
|
|
static const char* opcode_names[] = {
|
2016-11-08 16:00:44 -08:00
|
|
|
#define OPCODE(x) #x,
|
|
|
|
#define OPCODE_A_FIRST(x) #x,
|
|
|
|
#define OPCODE_A(x) #x,
|
|
|
|
#include "bytecode_ops.inl"
|
|
|
|
#undef OPCODE_A
|
|
|
|
#undef OPCODE_A_FIRST
|
|
|
|
#undef OPCODE
|
2010-09-04 01:20:41 -07:00
|
|
|
};
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2015-10-01 16:06:09 -07:00
|
|
|
#if __cplusplus >= 201103L
|
|
|
|
static_assert(sizeof(opcode_names) / sizeof(opcode_names[0]) == PYC_LAST_OPCODE,
|
|
|
|
"Pyc::OpcodeName opcode_names not in sync with opcode enum");
|
|
|
|
#endif
|
|
|
|
|
2010-09-04 01:20:41 -07:00
|
|
|
if (opcode < 0)
|
|
|
|
return "<INVALID>";
|
2009-07-24 19:52:47 +00:00
|
|
|
|
2010-09-04 01:20:41 -07:00
|
|
|
if (opcode < PYC_LAST_OPCODE)
|
|
|
|
return opcode_names[opcode];
|
2009-07-24 19:52:47 +00:00
|
|
|
|
2021-04-12 10:12:45 -07:00
|
|
|
static char badcode[16];
|
|
|
|
snprintf(badcode, sizeof(badcode), "<%d>", opcode);
|
2010-09-04 01:20:41 -07:00
|
|
|
return badcode;
|
|
|
|
};
|
2009-07-24 19:52:47 +00:00
|
|
|
|
2010-09-04 01:20:41 -07:00
|
|
|
int Pyc::ByteToOpcode(int maj, int min, int opcode)
|
2009-07-24 19:52:47 +00:00
|
|
|
{
|
2010-09-04 01:20:41 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
switch (min) {
|
|
|
|
case 0: return python_30_map(opcode);
|
|
|
|
case 1: return python_31_map(opcode);
|
2011-01-06 17:15:48 -08:00
|
|
|
case 2: return python_32_map(opcode);
|
2012-05-26 13:50:23 -07:00
|
|
|
case 3: return python_33_map(opcode);
|
2013-11-27 14:10:05 -08:00
|
|
|
case 4: return python_34_map(opcode);
|
2015-10-01 16:06:09 -07:00
|
|
|
case 5: return python_35_map(opcode);
|
2016-06-22 22:06:09 -07:00
|
|
|
case 6: return python_36_map(opcode);
|
2017-12-20 13:15:03 -08:00
|
|
|
case 7: return python_37_map(opcode);
|
2019-02-08 16:06:55 -08:00
|
|
|
case 8: return python_38_map(opcode);
|
2020-09-29 09:28:15 -07:00
|
|
|
case 9: return python_39_map(opcode);
|
2021-10-10 00:52:41 +05:30
|
|
|
case 10: return python_310_map(opcode);
|
2022-12-02 16:34:58 -08:00
|
|
|
case 11: return python_311_map(opcode);
|
2023-11-09 11:11:37 -08:00
|
|
|
case 12: return python_312_map(opcode);
|
2010-09-04 01:20:41 -07:00
|
|
|
}
|
|
|
|
break;
|
2010-09-02 01:01:05 -07:00
|
|
|
}
|
2010-09-04 01:20:41 -07:00
|
|
|
return PYC_INVALID_OPCODE;
|
2009-07-24 19:52:47 +00:00
|
|
|
}
|
|
|
|
|
2010-09-04 01:20:41 -07:00
|
|
|
bool Pyc::IsConstArg(int opcode)
|
2009-07-24 19:52:47 +00:00
|
|
|
{
|
2023-11-09 11:11:37 -08:00
|
|
|
return (opcode == Pyc::LOAD_CONST_A) || (opcode == Pyc::RESERVE_FAST_A) ||
|
|
|
|
(opcode == Pyc::RETURN_CONST_A);
|
2009-07-24 19:52:47 +00:00
|
|
|
}
|
|
|
|
|
2010-09-04 01:20:41 -07:00
|
|
|
bool Pyc::IsNameArg(int opcode)
|
2010-08-31 23:17:38 -07:00
|
|
|
{
|
2010-09-04 01:20:41 -07:00
|
|
|
return (opcode == Pyc::DELETE_ATTR_A) || (opcode == Pyc::DELETE_GLOBAL_A) ||
|
|
|
|
(opcode == Pyc::DELETE_NAME_A) || (opcode == Pyc::IMPORT_FROM_A) ||
|
|
|
|
(opcode == Pyc::IMPORT_NAME_A) || (opcode == Pyc::LOAD_ATTR_A) ||
|
|
|
|
(opcode == Pyc::LOAD_GLOBAL_A) || (opcode == Pyc::LOAD_LOCAL_A) ||
|
|
|
|
(opcode == Pyc::LOAD_NAME_A) || (opcode == Pyc::STORE_ATTR_A) ||
|
2017-12-20 13:15:03 -08:00
|
|
|
(opcode == Pyc::STORE_GLOBAL_A) || (opcode == Pyc::STORE_NAME_A) ||
|
|
|
|
(opcode == Pyc::LOAD_METHOD_A);
|
2010-08-31 23:17:38 -07:00
|
|
|
}
|
|
|
|
|
2010-09-04 01:20:41 -07:00
|
|
|
bool Pyc::IsVarNameArg(int opcode)
|
2009-07-24 19:52:47 +00:00
|
|
|
{
|
2010-09-04 01:20:41 -07:00
|
|
|
return (opcode == Pyc::DELETE_FAST_A) || (opcode == Pyc::LOAD_FAST_A) ||
|
|
|
|
(opcode == Pyc::STORE_FAST_A);
|
2009-07-24 19:52:47 +00:00
|
|
|
}
|
|
|
|
|
2010-09-04 01:20:41 -07:00
|
|
|
bool Pyc::IsCellArg(int opcode)
|
2009-07-24 19:52:47 +00:00
|
|
|
{
|
2010-09-04 01:20:41 -07:00
|
|
|
return (opcode == Pyc::LOAD_CLOSURE_A) || (opcode == Pyc::LOAD_DEREF_A) ||
|
|
|
|
(opcode == Pyc::STORE_DEREF_A);
|
2009-07-24 19:52:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 22:06:35 +01:00
|
|
|
bool Pyc::IsJumpArg(int opcode)
|
|
|
|
{
|
|
|
|
return (opcode == Pyc::POP_JUMP_IF_FALSE_A) || (opcode == Pyc::POP_JUMP_IF_TRUE_A) ||
|
2022-07-08 15:44:46 -05:00
|
|
|
(opcode == Pyc::JUMP_IF_FALSE_OR_POP_A) || (opcode == JUMP_IF_TRUE_OR_POP_A) ||
|
|
|
|
(opcode == Pyc::JUMP_ABSOLUTE_A) || (opcode == Pyc::JUMP_IF_NOT_EXC_MATCH_A);
|
2022-03-24 22:06:35 +01:00
|
|
|
}
|
|
|
|
|
2010-09-04 01:20:41 -07:00
|
|
|
bool Pyc::IsJumpOffsetArg(int opcode)
|
2009-07-24 19:52:47 +00:00
|
|
|
{
|
2010-09-04 01:20:41 -07:00
|
|
|
return (opcode == Pyc::JUMP_FORWARD_A) || (opcode == Pyc::JUMP_IF_FALSE_A) ||
|
2010-12-31 02:42:58 -08:00
|
|
|
(opcode == Pyc::JUMP_IF_TRUE_A) || (opcode == Pyc::SETUP_LOOP_A) ||
|
2011-01-01 02:31:31 -08:00
|
|
|
(opcode == Pyc::SETUP_FINALLY_A) || (opcode == Pyc::SETUP_EXCEPT_A) ||
|
2023-07-18 18:33:03 +03:00
|
|
|
(opcode == Pyc::FOR_LOOP_A) || (opcode == Pyc::FOR_ITER_A) ||
|
|
|
|
(opcode == Pyc::POP_JUMP_FORWARD_IF_FALSE_A) || (opcode == Pyc::POP_JUMP_FORWARD_IF_TRUE_A);
|
2010-08-31 23:17:38 -07:00
|
|
|
}
|
|
|
|
|
2016-07-10 17:40:14 +00:00
|
|
|
bool Pyc::IsCompareArg(int opcode)
|
|
|
|
{
|
|
|
|
return (opcode == Pyc::COMPARE_OP_A);
|
|
|
|
}
|
2009-07-24 19:52:47 +00:00
|
|
|
|
2023-06-05 13:56:25 -07:00
|
|
|
void print_const(std::ostream& pyc_output, PycRef<PycObject> obj, PycModule* mod,
|
|
|
|
const char* parent_f_string_quote)
|
2009-07-24 19:52:47 +00:00
|
|
|
{
|
2017-07-05 16:36:04 -07:00
|
|
|
if (obj == NULL) {
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "<NULL>";
|
2017-07-05 16:36:04 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-24 19:52:47 +00:00
|
|
|
switch (obj->type()) {
|
|
|
|
case PycObject::TYPE_STRING:
|
2014-01-21 00:47:11 -08:00
|
|
|
case PycObject::TYPE_UNICODE:
|
2009-07-24 19:52:47 +00:00
|
|
|
case PycObject::TYPE_INTERNED:
|
2014-01-21 00:07:34 -08:00
|
|
|
case PycObject::TYPE_ASCII:
|
|
|
|
case PycObject::TYPE_ASCII_INTERNED:
|
|
|
|
case PycObject::TYPE_SHORT_ASCII:
|
|
|
|
case PycObject::TYPE_SHORT_ASCII_INTERNED:
|
2023-06-09 09:09:03 -07:00
|
|
|
obj.cast<PycString>()->print(pyc_output, mod, false, parent_f_string_quote);
|
2009-07-25 00:02:31 +00:00
|
|
|
break;
|
2009-07-24 19:52:47 +00:00
|
|
|
case PycObject::TYPE_TUPLE:
|
2014-01-21 00:07:34 -08:00
|
|
|
case PycObject::TYPE_SMALL_TUPLE:
|
2009-07-24 19:52:47 +00:00
|
|
|
{
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "(";
|
2009-07-24 19:52:47 +00:00
|
|
|
PycTuple::value_t values = obj.cast<PycTuple>()->values();
|
2019-10-04 15:56:24 -07:00
|
|
|
auto it = values.cbegin();
|
|
|
|
if (it != values.cend()) {
|
2023-06-05 13:56:25 -07:00
|
|
|
print_const(pyc_output, *it, mod);
|
2019-10-04 15:56:24 -07:00
|
|
|
while (++it != values.cend()) {
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << ", ";
|
2023-06-05 13:56:25 -07:00
|
|
|
print_const(pyc_output, *it, mod);
|
2009-07-24 19:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-27 03:00:55 +00:00
|
|
|
if (values.size() == 1)
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << ",)";
|
2009-07-27 03:00:55 +00:00
|
|
|
else
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << ")";
|
2009-07-24 19:52:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PycObject::TYPE_LIST:
|
|
|
|
{
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "[";
|
2009-07-24 19:52:47 +00:00
|
|
|
PycList::value_t values = obj.cast<PycList>()->values();
|
2019-10-04 15:56:24 -07:00
|
|
|
auto it = values.cbegin();
|
|
|
|
if (it != values.cend()) {
|
2023-06-05 13:56:25 -07:00
|
|
|
print_const(pyc_output, *it, mod);
|
2019-10-04 15:56:24 -07:00
|
|
|
while (++it != values.cend()) {
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << ", ";
|
2023-06-05 13:56:25 -07:00
|
|
|
print_const(pyc_output, *it, mod);
|
2009-07-24 19:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "]";
|
2009-07-24 19:52:47 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-07-24 21:39:51 +00:00
|
|
|
case PycObject::TYPE_DICT:
|
|
|
|
{
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "{";
|
2009-07-24 21:39:51 +00:00
|
|
|
PycDict::value_t values = obj.cast<PycDict>()->values();
|
2023-11-09 15:05:55 -08:00
|
|
|
auto it = values.cbegin();
|
|
|
|
if (it != values.cend()) {
|
|
|
|
print_const(pyc_output, std::get<0>(*it), mod);
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << ": ";
|
2023-11-09 15:05:55 -08:00
|
|
|
print_const(pyc_output, std::get<1>(*it), mod);
|
|
|
|
while (++it != values.cend()) {
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << ", ";
|
2023-11-09 15:05:55 -08:00
|
|
|
print_const(pyc_output, std::get<0>(*it), mod);
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << ": ";
|
2023-11-09 15:05:55 -08:00
|
|
|
print_const(pyc_output, std::get<1>(*it), mod);
|
2009-07-24 21:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "}";
|
2009-07-24 21:39:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-07-25 02:41:15 +00:00
|
|
|
case PycObject::TYPE_SET:
|
|
|
|
{
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "{";
|
2009-07-25 02:41:15 +00:00
|
|
|
PycSet::value_t values = obj.cast<PycSet>()->values();
|
2019-10-04 15:56:24 -07:00
|
|
|
auto it = values.cbegin();
|
|
|
|
if (it != values.cend()) {
|
2023-06-05 13:56:25 -07:00
|
|
|
print_const(pyc_output, *it, mod);
|
2019-10-04 15:56:24 -07:00
|
|
|
while (++it != values.cend()) {
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << ", ";
|
2023-06-05 13:56:25 -07:00
|
|
|
print_const(pyc_output, *it, mod);
|
2009-07-25 02:41:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "}";
|
2009-07-25 02:41:15 +00:00
|
|
|
}
|
|
|
|
break;
|
2023-02-13 19:33:25 -08:00
|
|
|
case PycObject::TYPE_FROZENSET:
|
|
|
|
{
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "frozenset({";
|
2023-02-13 19:33:25 -08:00
|
|
|
PycSet::value_t values = obj.cast<PycSet>()->values();
|
|
|
|
auto it = values.cbegin();
|
|
|
|
if (it != values.cend()) {
|
2023-06-05 13:56:25 -07:00
|
|
|
print_const(pyc_output, *it, mod);
|
2023-02-13 19:33:25 -08:00
|
|
|
while (++it != values.cend()) {
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << ", ";
|
2023-06-05 13:56:25 -07:00
|
|
|
print_const(pyc_output, *it, mod);
|
2023-02-13 19:33:25 -08:00
|
|
|
}
|
|
|
|
}
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "})";
|
2023-02-13 19:33:25 -08:00
|
|
|
}
|
|
|
|
break;
|
2009-07-24 19:52:47 +00:00
|
|
|
case PycObject::TYPE_NONE:
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "None";
|
2009-07-24 19:52:47 +00:00
|
|
|
break;
|
|
|
|
case PycObject::TYPE_TRUE:
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "True";
|
2009-07-24 19:52:47 +00:00
|
|
|
break;
|
|
|
|
case PycObject::TYPE_FALSE:
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "False";
|
2009-07-24 19:52:47 +00:00
|
|
|
break;
|
2016-08-30 13:32:53 -07:00
|
|
|
case PycObject::TYPE_ELLIPSIS:
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "...";
|
2016-08-30 13:32:53 -07:00
|
|
|
break;
|
2009-07-24 19:52:47 +00:00
|
|
|
case PycObject::TYPE_INT:
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d", obj.cast<PycInt>()->value());
|
2009-07-24 19:52:47 +00:00
|
|
|
break;
|
2011-09-23 21:46:05 -07:00
|
|
|
case PycObject::TYPE_LONG:
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%s", obj.cast<PycLong>()->repr().c_str());
|
2011-09-23 21:46:05 -07:00
|
|
|
break;
|
2009-07-24 19:52:47 +00:00
|
|
|
case PycObject::TYPE_FLOAT:
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%s", obj.cast<PycFloat>()->value());
|
2009-07-24 19:52:47 +00:00
|
|
|
break;
|
2009-07-25 02:41:15 +00:00
|
|
|
case PycObject::TYPE_COMPLEX:
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "(%s+%sj)", obj.cast<PycComplex>()->value(),
|
2011-10-23 19:04:06 -07:00
|
|
|
obj.cast<PycComplex>()->imag());
|
2009-07-25 02:41:15 +00:00
|
|
|
break;
|
|
|
|
case PycObject::TYPE_BINARY_FLOAT:
|
2020-10-15 20:57:56 -04:00
|
|
|
{
|
|
|
|
// Wrap any nan/inf values in float('').
|
|
|
|
double value = obj.cast<PycCFloat>()->value();
|
2020-10-15 21:35:19 -04:00
|
|
|
bool is_negative = std::signbit(value);
|
|
|
|
if (std::isnan(value)) {
|
|
|
|
if (is_negative) {
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "float('-nan')";
|
2020-10-15 21:35:19 -04:00
|
|
|
} else {
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "float('nan')";
|
2020-10-15 21:35:19 -04:00
|
|
|
}
|
|
|
|
} else if (std::isinf(value)) {
|
|
|
|
if (is_negative) {
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "float('-inf')";
|
2020-10-15 21:35:19 -04:00
|
|
|
} else {
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "float('inf')";
|
2020-10-15 21:35:19 -04:00
|
|
|
}
|
2020-10-15 20:57:56 -04:00
|
|
|
} else {
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%g", value);
|
2020-10-15 20:57:56 -04:00
|
|
|
}
|
|
|
|
}
|
2009-07-25 02:41:15 +00:00
|
|
|
break;
|
|
|
|
case PycObject::TYPE_BINARY_COMPLEX:
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "(%g+%gj)", obj.cast<PycCComplex>()->value(),
|
2011-10-23 19:04:06 -07:00
|
|
|
obj.cast<PycCComplex>()->imag());
|
2009-07-25 02:41:15 +00:00
|
|
|
break;
|
2009-07-24 19:52:47 +00:00
|
|
|
case PycObject::TYPE_CODE:
|
2009-07-25 00:43:46 +00:00
|
|
|
case PycObject::TYPE_CODE2:
|
2023-06-05 13:56:25 -07:00
|
|
|
pyc_output << "<CODE> " << obj.cast<PycCode>()->name()->value();
|
2009-07-24 19:52:47 +00:00
|
|
|
break;
|
2023-02-13 19:33:25 -08:00
|
|
|
default:
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "<TYPE: %d>\n", obj->type());
|
2009-07-24 19:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-26 10:07:13 +00:00
|
|
|
void bc_next(PycBuffer& source, PycModule* mod, int& opcode, int& operand, int& pos)
|
|
|
|
{
|
2010-09-04 01:20:41 -07:00
|
|
|
opcode = Pyc::ByteToOpcode(mod->majorVer(), mod->minorVer(), source.getByte());
|
2023-01-19 11:20:06 -08:00
|
|
|
bool py36_opcode = (mod->verCompare(3, 6) >= 0);
|
2016-06-22 22:06:09 -07:00
|
|
|
if (py36_opcode) {
|
|
|
|
operand = source.getByte();
|
|
|
|
pos += 2;
|
|
|
|
} else {
|
|
|
|
operand = 0;
|
|
|
|
pos += 1;
|
|
|
|
}
|
2009-07-26 10:07:13 +00:00
|
|
|
|
2010-09-04 01:20:41 -07:00
|
|
|
if (opcode == Pyc::EXTENDED_ARG_A) {
|
2016-06-22 22:06:09 -07:00
|
|
|
if (py36_opcode) {
|
|
|
|
opcode = Pyc::ByteToOpcode(mod->majorVer(), mod->minorVer(), source.getByte());
|
|
|
|
operand <<= 8;
|
|
|
|
operand |= source.getByte();
|
|
|
|
pos += 2;
|
|
|
|
} else {
|
|
|
|
operand = source.get16() << 16;
|
|
|
|
opcode = Pyc::ByteToOpcode(mod->majorVer(), mod->minorVer(), source.getByte());
|
|
|
|
pos += 3;
|
|
|
|
}
|
2009-07-26 10:07:13 +00:00
|
|
|
}
|
2016-06-22 22:06:09 -07:00
|
|
|
if (!py36_opcode && (opcode >= Pyc::PYC_HAVE_ARG)) {
|
2013-08-06 22:16:28 -07:00
|
|
|
operand |= source.get16();
|
2009-07-26 10:07:13 +00:00
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 13:56:25 -07:00
|
|
|
void bc_disasm(std::ostream& pyc_output, PycRef<PycCode> code, PycModule* mod,
|
|
|
|
int indent, unsigned flags)
|
2009-07-24 08:35:21 +00:00
|
|
|
{
|
2016-07-10 17:40:14 +00:00
|
|
|
static const char *cmp_strings[] = {
|
|
|
|
"<", "<=", "==", "!=", ">", ">=", "in", "not in", "is", "is not",
|
|
|
|
"<EXCEPTION MATCH>", "<BAD>"
|
|
|
|
};
|
2016-07-27 15:15:19 +03:00
|
|
|
static const size_t cmp_strings_len = sizeof(cmp_strings) / sizeof(cmp_strings[0]);
|
2016-07-15 13:37:50 -07:00
|
|
|
|
2023-05-30 14:45:22 -07:00
|
|
|
static const char *binop_strings[] = {
|
|
|
|
"+", "&", "//", "<<", "@", "*", "%", "|", "**", ">>", "-", "/", "^",
|
|
|
|
"+=", "&=", "//=", "<<=", "@=", "*=", "%=", "|=", "**=", ">>=", "-=", "/=", "^=",
|
|
|
|
};
|
|
|
|
static const size_t binop_strings_len = sizeof(binop_strings) / sizeof(binop_strings[0]);
|
|
|
|
|
2023-11-09 11:11:37 -08:00
|
|
|
static const char *intrinsic1_names[] = {
|
|
|
|
"INTRINSIC_1_INVALID", "INTRINSIC_PRINT", "INTRINSIC_IMPORT_STAR",
|
|
|
|
"INTRINSIC_STOPITERATION_ERROR", "INTRINSIC_ASYNC_GEN_WRAP",
|
|
|
|
"INTRINSIC_UNARY_POSITIVE", "INTRINSIC_LIST_TO_TUPLE", "INTRINSIC_TYPEVAR",
|
|
|
|
"INTRINSIC_PARAMSPEC", "INTRINSIC_TYPEVARTUPLE",
|
|
|
|
"INTRINSIC_SUBSCRIPT_GENERIC", "INTRINSIC_TYPEALIAS",
|
|
|
|
};
|
|
|
|
static const size_t intrinsic1_names_len = sizeof(intrinsic1_names) / sizeof(intrinsic1_names[0]);
|
|
|
|
|
|
|
|
static const char *intrinsic2_names[] = {
|
|
|
|
"INTRINSIC_2_INVALID", "INTRINSIC_PREP_RERAISE_STAR",
|
|
|
|
"INTRINSIC_TYPEVAR_WITH_BOUND", "INTRINSIC_TYPEVAR_WITH_CONSTRAINTS",
|
|
|
|
"INTRINSIC_SET_FUNCTION_TYPE_PARAMS",
|
|
|
|
};
|
|
|
|
static const size_t intrinsic2_names_len = sizeof(intrinsic2_names) / sizeof(intrinsic2_names[0]);
|
|
|
|
|
2009-07-24 08:35:21 +00:00
|
|
|
PycBuffer source(code->code()->value(), code->code()->length());
|
|
|
|
|
2009-07-26 10:07:13 +00:00
|
|
|
int opcode, operand;
|
2009-07-25 02:41:15 +00:00
|
|
|
int pos = 0;
|
2009-07-24 08:35:21 +00:00
|
|
|
while (!source.atEof()) {
|
2023-01-19 14:50:55 -08:00
|
|
|
int start_pos = pos;
|
|
|
|
bc_next(source, mod, opcode, operand, pos);
|
|
|
|
if (opcode == Pyc::CACHE && (flags & Pyc::DISASM_SHOW_CACHES) == 0)
|
|
|
|
continue;
|
|
|
|
|
2009-07-25 02:41:15 +00:00
|
|
|
for (int i=0; i<indent; i++)
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << " ";
|
|
|
|
formatted_print(pyc_output, "%-7d %-30s", start_pos, Pyc::OpcodeName(opcode));
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2010-09-04 01:20:41 -07:00
|
|
|
if (opcode >= Pyc::PYC_HAVE_ARG) {
|
|
|
|
if (Pyc::IsConstArg(opcode)) {
|
2021-08-16 15:25:09 +00:00
|
|
|
try {
|
|
|
|
auto constParam = code->getConst(operand);
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d: ", operand);
|
2023-06-05 13:56:25 -07:00
|
|
|
print_const(pyc_output, constParam, mod);
|
2021-08-16 15:25:09 +00:00
|
|
|
} catch (const std::out_of_range &) {
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d <INVALID>", operand);
|
2021-08-16 15:25:09 +00:00
|
|
|
}
|
2023-01-19 13:05:06 -08:00
|
|
|
} else if (opcode == Pyc::LOAD_GLOBAL_A) {
|
|
|
|
try {
|
2023-08-19 21:24:03 -07:00
|
|
|
// Special case for Python 3.11+
|
|
|
|
if (mod->verCompare(3, 11) >= 0) {
|
2023-08-19 17:43:04 +03:00
|
|
|
if (operand & 1)
|
|
|
|
formatted_print(pyc_output, "%d: NULL + %s", operand, code->getName(operand >> 1)->value());
|
|
|
|
else
|
|
|
|
formatted_print(pyc_output, "%d: %s", operand, code->getName(operand >> 1)->value());
|
2023-08-19 21:24:03 -07:00
|
|
|
} else {
|
2023-08-19 17:43:04 +03:00
|
|
|
formatted_print(pyc_output, "%d: %s", operand, code->getName(operand)->value());
|
2023-08-19 21:24:03 -07:00
|
|
|
}
|
2023-01-19 13:05:06 -08:00
|
|
|
} catch (const std::out_of_range &) {
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d <INVALID>", operand);
|
2023-01-19 13:05:06 -08:00
|
|
|
}
|
2010-09-04 01:20:41 -07:00
|
|
|
} else if (Pyc::IsNameArg(opcode)) {
|
2021-08-16 15:25:09 +00:00
|
|
|
try {
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d: %s", operand, code->getName(operand)->value());
|
2021-08-16 15:25:09 +00:00
|
|
|
} catch (const std::out_of_range &) {
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d <INVALID>", operand);
|
2021-08-16 15:25:09 +00:00
|
|
|
}
|
2010-09-04 01:20:41 -07:00
|
|
|
} else if (Pyc::IsVarNameArg(opcode)) {
|
2021-08-16 15:25:09 +00:00
|
|
|
try {
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d: %s", operand, code->getLocal(operand)->value());
|
2021-08-16 15:25:09 +00:00
|
|
|
} catch (const std::out_of_range &) {
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d <INVALID>", operand);
|
2021-08-16 15:25:09 +00:00
|
|
|
}
|
2010-09-04 01:20:41 -07:00
|
|
|
} else if (Pyc::IsCellArg(opcode)) {
|
2021-08-16 15:25:09 +00:00
|
|
|
try {
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d: %s", operand, code->getCellVar(mod, operand)->value());
|
2021-08-16 15:25:09 +00:00
|
|
|
} catch (const std::out_of_range &) {
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d <INVALID>", operand);
|
2021-08-16 15:25:09 +00:00
|
|
|
}
|
2010-09-04 01:20:41 -07:00
|
|
|
} else if (Pyc::IsJumpOffsetArg(opcode)) {
|
2022-03-24 22:06:35 +01:00
|
|
|
int offs = operand;
|
|
|
|
if (mod->verCompare(3, 10) >= 0)
|
|
|
|
offs *= sizeof(uint16_t); // BPO-27129
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d (to %d)", operand, pos+offs);
|
2023-01-19 11:20:06 -08:00
|
|
|
} else if (Pyc::IsJumpArg(opcode)) {
|
2022-03-24 22:06:35 +01:00
|
|
|
if (mod->verCompare(3, 10) >= 0) // BPO-27129
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d (to %d)", operand, int(operand * sizeof(uint16_t)));
|
2022-03-24 22:06:35 +01:00
|
|
|
else
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d", operand);
|
2016-07-10 17:40:14 +00:00
|
|
|
} else if (Pyc::IsCompareArg(opcode)) {
|
2016-07-28 09:48:17 +02:00
|
|
|
if (static_cast<size_t>(operand) < cmp_strings_len)
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d (%s)", operand, cmp_strings[operand]);
|
2016-07-10 17:40:14 +00:00
|
|
|
else
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d (UNKNOWN)", operand);
|
2023-05-30 14:45:22 -07:00
|
|
|
} else if (opcode == Pyc::BINARY_OP_A) {
|
|
|
|
if (static_cast<size_t>(operand) < binop_strings_len)
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d (%s)", operand, binop_strings[operand]);
|
2023-05-30 14:45:22 -07:00
|
|
|
else
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d (UNKNOWN)", operand);
|
2020-09-29 09:28:15 -07:00
|
|
|
} else if (opcode == Pyc::IS_OP_A) {
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d (%s)", operand, (operand == 0) ? "is"
|
2020-09-29 09:28:15 -07:00
|
|
|
: (operand == 1) ? "is not"
|
|
|
|
: "UNKNOWN");
|
|
|
|
} else if (opcode == Pyc::CONTAINS_OP_A) {
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d (%s)", operand, (operand == 0) ? "in"
|
2020-09-29 09:28:15 -07:00
|
|
|
: (operand == 1) ? "not in"
|
|
|
|
: "UNKNOWN");
|
2023-11-09 11:11:37 -08:00
|
|
|
} else if (opcode == Pyc::CALL_INTRINSIC_1_A) {
|
|
|
|
if (static_cast<size_t>(operand) < intrinsic1_names_len)
|
|
|
|
formatted_print(pyc_output, "%d (%s)", operand, intrinsic1_names[operand]);
|
|
|
|
else
|
|
|
|
formatted_print(pyc_output, "%d (UNKNOWN)", operand);
|
|
|
|
} else if (opcode == Pyc::CALL_INTRINSIC_2_A) {
|
|
|
|
if (static_cast<size_t>(operand) < intrinsic2_names_len)
|
|
|
|
formatted_print(pyc_output, "%d (%s)", operand, intrinsic2_names[operand]);
|
|
|
|
else
|
|
|
|
formatted_print(pyc_output, "%d (UNKNOWN)", operand);
|
2009-07-24 19:52:47 +00:00
|
|
|
} else {
|
2023-06-02 00:36:58 +02:00
|
|
|
formatted_print(pyc_output, "%d", operand);
|
2009-07-24 19:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-02 00:36:58 +02:00
|
|
|
pyc_output << "\n";
|
2009-07-24 08:35:21 +00:00
|
|
|
}
|
|
|
|
}
|