Visitar URL original
W27a #2: emitPrimitiveUnbox → C body + delegation stub · SonicField/cpython@0f6117d · GitHub
Skip to content

Commit 0f6117d

Browse files
committed
W27a python#2: emitPrimitiveUnbox → C body + delegation stub
PRIMITIVE_UNBOX (Static Python) opcode handler. Per W27a Step A scope. C body: hir_builder_emit_primitive_unbox_c (~25 lines). Inlines unboxPrimitive: emit primitive_unbox_reg, then conditionally emit IsNegativeAndErrOccurred (skipped for CBool|CDouble types — they cannot fail). hir_type_union + hir_type_is_subtype handle the type- class check. C primitives used: hir_c_create_primitive_unbox_reg + hir_c_create_is_neg_and_err_reg + hir_builder_temps_alloc_stack (all existing). C++ HIRBuilder::emitPrimitiveUnbox → 3-line delegation stub. Verification: cmake --build target jit/phoenix_jit clean (BUILD_EXIT=0). Sole-path validation: PRIMITIVE_UNBOX only in __static__ modules (emitInvokeNative precedent caveat acknowledged).
1 parent e61eced commit 0f6117d

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

‎Python/jit/hir/builder.cpp‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3937,14 +3937,13 @@ void HIRBuilder::emitPrimitiveBox(
39373937
hir_builder_emit_primitive_box_c(&tc, this, bc_instr.oparg());
39383938
}
39393939

3940+
extern "C" void hir_builder_emit_primitive_unbox_c(
3941+
void *tc, void *builder, int oparg);
3942+
39403943
void HIRBuilder::emitPrimitiveUnbox(
39413944
TranslationContext& tc,
39423945
const jit::BytecodeInstruction& bc_instr) {
3943-
Register* tmp = temps_.AllocateStack();
3944-
Register* src = static_cast<Register*>(phx_ptr_arr_pop(&tc.frame.stack));
3945-
Type typ = prim_type_to_type(bc_instr.oparg());
3946-
unboxPrimitive(tc, tmp, src, typ);
3947-
phx_ptr_arr_push(&tc.frame.stack, tmp);
3946+
hir_builder_emit_primitive_unbox_c(&tc, this, bc_instr.oparg());
39483947
}
39493948

39503949
void HIRBuilder::boxPrimitive(

‎Python/jit/hir/builder_emit_c.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3676,4 +3676,30 @@ void hir_builder_emit_primitive_box_c(
36763676
phx_ptr_arr_push(&tc->frame.stack, tmp);
36773677
}
36783678

3679+
/* W27a #2: emitPrimitiveUnbox (PRIMITIVE_UNBOX opcode, Static Python).
3680+
* Mirrors C++ HIRBuilder::emitPrimitiveUnbox @ builder.cpp:3941 + inline
3681+
* unboxPrimitive @ builder.cpp:3963 (TCBool|TCDouble error-check skip). */
3682+
void hir_builder_emit_primitive_unbox_c(
3683+
PhxTranslationContext *tc,
3684+
void *builder,
3685+
int oparg) {
3686+
void *tmp = hir_builder_temps_alloc_stack(builder);
3687+
void *src = phx_ptr_arr_pop(&tc->frame.stack);
3688+
HirType type = hir_prim_type_to_type(oparg);
3689+
3690+
/* Inline unboxPrimitive: emit primitive_unbox_reg, then conditionally
3691+
* emit IsNegativeAndErrOccurred to detect unbox failure. The check is
3692+
* skipped for CBool|CDouble types (they cannot fail). */
3693+
phx_tc_emit(tc, hir_c_create_primitive_unbox_reg(tmp, src, type));
3694+
HirType cbool = HIR_TYPE_CBOOL;
3695+
HirType cdouble = HIR_TYPE_CDOUBLE;
3696+
HirType cbool_or_cdouble = hir_type_union(cbool, cdouble);
3697+
if (!hir_type_is_subtype(type, cbool_or_cdouble)) {
3698+
void *did_unbox_work = hir_builder_temps_alloc_stack(builder);
3699+
phx_tc_emit(tc, hir_c_create_is_neg_and_err_reg(
3700+
did_unbox_work, tmp, &tc->frame));
3701+
}
3702+
phx_ptr_arr_push(&tc->frame.stack, tmp);
3703+
}
3704+
36793705

0 commit comments

Comments
 (0)