This document discusses the internals of how Python code is evaluated in CPython. It explains how Python code is compiled to bytecode and represented as an AST that is then interpreted by ceval.c using a stack-based virtual machine. Key aspects covered include how frames, opcodes, and reference counting are used during evaluation.
31. stack machine
• stack machine (BINARY_ADD) 2
operands POP → POP → PUSH
•→
TARGET(BINARY_ADD) // only main
w = POP();
v = TOP();
x = PyNumber_Add(v, w);
Py_DECREF(v);
Py_DECREF(w);
SET_TOP(x);
if (x != NULL) DISPATCH();
break;
Sunday, March 27, 2011
32. • stack machine
• if
• if compare & branch
•
• python AST compile
Sunday, March 27, 2011
33. JUMP_IF_TRUE [FALSE]
(_OR_POP)
• stack machine jump
• JUMP_IF_TRUE [FALSE] (_OR_POP)
TARGET(POP_JUMP_IF_TRUE)
w = POP();
if (w == Py_False) {
Py_DECREF(w);
FAST_DISPATCH();
}
if (w == Py_True) {
Py_DECREF(w);
JUMPTO(oparg); // this will set the next instruction!
FAST_DISPATCH();
Sunday, March 27, 2011