pycdc/FastStack.h
polfg 717b3d7d9c Fix SWAP_A as a real stack swap (chained comparisons, starred unpack)
SWAP_A was modelled only as tuple-unpack construction, corrupting the stack
for the 3.11 chained-comparison idiom (SWAP n; COPY n). Implement it as a
genuine stack swap via FastStack::swap.

Harness: +17 files (decompilation target: 212→224, stdlib corpus 13->18), 0 regressions.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-05 11:43:44 +02:00

80 lines
1.7 KiB
C++

#ifndef _PYC_FASTSTACK_H
#define _PYC_FASTSTACK_H
#include "ASTNode.h"
#include <stack>
class FastStack {
public:
FastStack(int size) : m_ptr(-1) { m_stack.resize(size); }
FastStack(const FastStack& copy)
: m_stack(copy.m_stack), m_ptr(copy.m_ptr) { }
FastStack& operator=(const FastStack& copy)
{
m_stack = copy.m_stack;
m_ptr = copy.m_ptr;
return *this;
}
void push(PycRef<ASTNode> node)
{
if (static_cast<int>(m_stack.size()) == m_ptr + 1)
m_stack.emplace_back(nullptr);
m_stack[++m_ptr] = std::move(node);
}
void pop()
{
if (m_ptr > -1)
m_stack[m_ptr--] = nullptr;
else {
#ifdef BLOCK_DEBUG
fprintf(stderr, "pop from empty stack\n");
#endif
}
}
PycRef<ASTNode> top(int i = 1) const
{
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;
}
}
/* Swap the top of stack with the i-th element from the top (i >= 2). */
void swap(int i)
{
int a = m_ptr;
int b = m_ptr + 1 - i;
if (a >= 0 && b >= 0 && a != b)
std::swap(m_stack[a], m_stack[b]);
}
bool empty() const
{
return m_ptr == -1;
}
private:
std::vector<PycRef<ASTNode>> m_stack;
int m_ptr;
};
typedef std::stack<FastStack> stackhist_t;
#endif