From ad5f39db56b5b93ffb7cb39c7dc1f62237d7e164 Mon Sep 17 00:00:00 2001 From: Sahil Jain Date: Mon, 30 Jun 2025 23:57:31 +0530 Subject: [PATCH 1/4] Support SLICE opcodes --- ASTree.cpp | 67 +++++++++++++++++++++++++++ tests/compiled/binary_slice.3.12.pyc | Bin 0 -> 412 bytes tests/compiled/store_slice.3.12.pyc | Bin 0 -> 358 bytes tests/input/binary_slice.py | 7 +++ tests/input/store_slice.py | 8 ++++ tests/tokenized/binary_slice.txt | 7 +++ tests/tokenized/store_slice.txt | 7 +++ 7 files changed, 96 insertions(+) create mode 100644 tests/compiled/binary_slice.3.12.pyc create mode 100644 tests/compiled/store_slice.3.12.pyc create mode 100644 tests/input/binary_slice.py create mode 100644 tests/input/store_slice.py create mode 100644 tests/tokenized/binary_slice.txt create mode 100644 tests/tokenized/store_slice.txt diff --git a/ASTree.cpp b/ASTree.cpp index 1f419d0..281d224 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -2473,6 +2473,73 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) stack.push(next_tup); } break; + case Pyc::BINARY_SLICE: + { + PycRef end = stack.top(); + stack.pop(); + PycRef start = stack.top(); + stack.pop(); + PycRef dest = stack.top(); + stack.pop(); + + if (start.type() == ASTNode::NODE_OBJECT + && start.cast()->object() == Pyc_None) { + start = NULL; + } + + if (end.type() == ASTNode::NODE_OBJECT + && end.cast()->object() == Pyc_None) { + end = NULL; + } + + PycRef slice; + if (start == NULL && end == NULL) { + slice = new ASTSlice(ASTSlice::SLICE0); + } else if (start == NULL) { + slice = new ASTSlice(ASTSlice::SLICE2, start, end); + } else if (end == NULL) { + slice = new ASTSlice(ASTSlice::SLICE1, start, end); + } else { + slice = new ASTSlice(ASTSlice::SLICE3, start, end); + } + stack.push(new ASTSubscr(dest, slice)); + } + break; + case Pyc::STORE_SLICE: + { + PycRef end = stack.top(); + stack.pop(); + PycRef start = stack.top(); + stack.pop(); + PycRef dest = stack.top(); + stack.pop(); + PycRef values = stack.top(); + stack.pop(); + + if (start.type() == ASTNode::NODE_OBJECT + && start.cast()->object() == Pyc_None) { + start = NULL; + } + + if (end.type() == ASTNode::NODE_OBJECT + && end.cast()->object() == Pyc_None) { + end = NULL; + } + + PycRef slice; + if (start == NULL && end == NULL) { + slice = new ASTSlice(ASTSlice::SLICE0); + } else if (start == NULL) { + slice = new ASTSlice(ASTSlice::SLICE2, start, end); + } else if (end == NULL) { + slice = new ASTSlice(ASTSlice::SLICE1, start, end); + } else { + slice = new ASTSlice(ASTSlice::SLICE3, start, end); + } + + curblock->append(new ASTStore(values, new ASTSubscr(dest, slice))); + } + break; default: fprintf(stderr, "Unsupported opcode: %s (%d)\n", Pyc::OpcodeName(opcode), opcode); cleanBuild = false; diff --git a/tests/compiled/binary_slice.3.12.pyc b/tests/compiled/binary_slice.3.12.pyc new file mode 100644 index 0000000000000000000000000000000000000000..24cc0f2f81a267dfc0a819000ef4f8d51af39dcd GIT binary patch literal 412 zcmX@j%ge<81R|G{G7^FGV-N=hSfPy16M&5A4CxFh42u|}7?>DR8B-Zj7*m*}7*;bv z)G)$CnNwJ>h%%)xV-aP=D$1O~hD9wis@h6sO?FMTmmu3;GJyzY5WxZ>K(2nt1|*6= z5=9^}zap^opa1{=zx)RzG?{KO=GR?7FW9|@wb2=FtQd+r-c!5R(0QW~*_W%F@ literal 0 HcmV?d00001 diff --git a/tests/compiled/store_slice.3.12.pyc b/tests/compiled/store_slice.3.12.pyc new file mode 100644 index 0000000000000000000000000000000000000000..252f5af5f78ffd0479cbdcf5adf17aca3ee23abc GIT binary patch literal 358 zcmXwzF;BxV5QXm&yG;raFjovMQx5VI)up)Ldrj2h7Ivk zn2|y@#FP!G8x!Y3Z+QOh`RVT7JZ}J0Pw!VZ=k$J=W>f4RR+j{iARt14cN|~_5eVxU zXDCsEumu}};6%MCB}HF2kwqO7&JZk+mcUp1o3(Z+O|C6r<06#IcRLg5c1e7@Gx^Rk zVjJ;DPCIjINBcpU)082fwK#7vX}OxGk!*RU;ODV~s7_(%85Ije4vPbg${Vgrka) fS5Iob_9vQu^tbD?`%+^y((Dtso +print ( l [ 1 : 3 ] ) +print ( l [ : 2 ] ) +print ( l [ 3 : ] ) +print ( l [ - 4 : ] ) +print ( l [ : - 2 ] ) +print ( l [ : ] ) diff --git a/tests/tokenized/store_slice.txt b/tests/tokenized/store_slice.txt new file mode 100644 index 0000000..2842070 --- /dev/null +++ b/tests/tokenized/store_slice.txt @@ -0,0 +1,7 @@ +a = [ 0 ] * 16 +l = [ 1 , 2 , 3 ] +a [ 13 : ] = l +a [ 5 ] = 10 +a [ : 2 ] = [ 1 , 2 ] +a [ : ] = range ( 16 ) +print ( a ) From 5fe61462a244e5a919a79740d28483dc9e047660 Mon Sep 17 00:00:00 2001 From: Sahil Jain Date: Mon, 30 Jun 2025 23:58:54 +0530 Subject: [PATCH 2/4] Support COPY opcde --- ASTree.cpp | 6 ++++++ FastStack.h | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ASTree.cpp b/ASTree.cpp index 281d224..851f735 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -2540,6 +2540,12 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) curblock->append(new ASTStore(values, new ASTSubscr(dest, slice))); } break; + case Pyc::COPY_A: + { + PycRef value = stack.top(operand); + stack.push(value); + } + break; default: fprintf(stderr, "Unsupported opcode: %s (%d)\n", Pyc::OpcodeName(opcode), opcode); cleanBuild = false; diff --git a/FastStack.h b/FastStack.h index 45f8ed5..d8fb08b 100644 --- a/FastStack.h +++ b/FastStack.h @@ -32,11 +32,21 @@ public: m_stack[m_ptr--] = nullptr; } - PycRef top() const + PycRef top(int i = 1) const { - if (m_ptr > -1) - return m_stack[m_ptr]; - else + if (i > 0) { + int idx = m_ptr + 1 - i; + if ((m_ptr > -1) && (idx >= 0)) + return m_stack[idx]; + else { + #ifdef BLOCK_DEBUG + fprintf(stderr, "insufficient values on stack\n"); + #endif + return nullptr; + } + } + else { + fprintf(stderr, "incorrect operand %i\n", i); return nullptr; } From aa292c76820fac67a064f20cafb9946cd746d1e1 Mon Sep 17 00:00:00 2001 From: Sahil Jain Date: Tue, 1 Jul 2025 09:27:07 +0530 Subject: [PATCH 3/4] Add newlines --- tests/input/binary_slice.py | 2 +- tests/input/store_slice.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/input/binary_slice.py b/tests/input/binary_slice.py index fb47b61..361d922 100644 --- a/tests/input/binary_slice.py +++ b/tests/input/binary_slice.py @@ -4,4 +4,4 @@ print(l[:2]) print(l[3:]) print(l[-4:]) print(l[:-2]) -print(l[:]) \ No newline at end of file +print(l[:]) diff --git a/tests/input/store_slice.py b/tests/input/store_slice.py index 1e04a3a..8d8369a 100644 --- a/tests/input/store_slice.py +++ b/tests/input/store_slice.py @@ -5,4 +5,4 @@ a[5] = 10 a[:2] = [1,2] a[:] = range(16) -print(a) \ No newline at end of file +print(a) From 3afcfbc6a76307b0df68499254a3f04ed462ea7a Mon Sep 17 00:00:00 2001 From: Sahil Jain Date: Tue, 1 Jul 2025 14:01:05 +0530 Subject: [PATCH 4/4] Fix compilation error --- FastStack.h | 1 + 1 file changed, 1 insertion(+) diff --git a/FastStack.h b/FastStack.h index d8fb08b..3f8950c 100644 --- a/FastStack.h +++ b/FastStack.h @@ -48,6 +48,7 @@ public: else { fprintf(stderr, "incorrect operand %i\n", i); return nullptr; + } } bool empty() const