Skip to content

Commit 7783df7

Browse files
committed
builder: 19 emit methods in C (13% of 144)
Port 3 more emit methods: - emitStoreAttr (stack pop receiver+value, StoreAttr) - emitLoadType (LoadField ob_type) - emitCopyDictWithoutKeys (peek+replace stack top) Also: emitIsOp, emitContainsOp, emitDeleteAttr, emitUnaryOp, emitUnaryNot from prior uncommitted work. 19/144 builder.cpp emit methods now in C.
1 parent 891a814 commit 7783df7

2 files changed

Lines changed: 43 additions & 15 deletions

File tree

Python/jit/hir/builder.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3923,13 +3923,10 @@ void HIRBuilder::emitToBool(TranslationContext& tc) {
39233923
phx_ptr_arr_push(&tc.frame.stack, coerced_result);
39243924
}
39253925

3926+
extern "C" void hir_builder_emit_copy_dict_without_keys_c(void *tc, void *func);
3927+
39263928
void HIRBuilder::emitCopyDictWithoutKeys(TranslationContext& tc) {
3927-
PhxPtrArray& stack = tc.frame.stack;
3928-
Register* keys = static_cast<Register*>(stack.data[stack.count - 1]);
3929-
Register* subject = static_cast<Register*>(stack.data[stack.count - (1) - 1]);
3930-
Register* rest = temps_.AllocateStack();
3931-
tc.emitCopyDictWithoutKeys(rest, subject, keys, tc.frame);
3932-
stack.data[stack.count - (0) - 1] = rest;
3929+
hir_builder_emit_copy_dict_without_keys_c(static_cast<void*>(&tc), static_cast<void*>(current_func_));
39333930
}
39343931

39353932
extern "C" void hir_builder_emit_get_len_c(void *tc, void *func);
@@ -4578,14 +4575,12 @@ void HIRBuilder::emitStoreLocal(
45784575
tc.emitAssign(dst, src);
45794576
}
45804577

4578+
extern "C" void hir_builder_emit_load_type_c(void *tc, void *func);
4579+
45814580
void HIRBuilder::emitLoadType(
45824581
TranslationContext& tc,
45834582
const jit::BytecodeInstruction&) {
4584-
Register* instance = static_cast<Register*>(phx_ptr_arr_pop(&tc.frame.stack));
4585-
auto type = temps_.AllocateStack();
4586-
tc.emitLoadField(
4587-
type, instance, "ob_type", offsetof(PyObject, ob_type), TType);
4588-
phx_ptr_arr_push(&tc.frame.stack, type);
4583+
hir_builder_emit_load_type_c(static_cast<void*>(&tc), static_cast<void*>(current_func_));
45894584
}
45904585

45914586
void HIRBuilder::emitConvertPrimitive(
@@ -5337,13 +5332,12 @@ void HIRBuilder::emitPopJumpIfNone(
53375332
tc.emitCondBranch(is_true, true_block, false_block);
53385333
}
53395334

5335+
extern "C" void hir_builder_emit_store_attr_c(void *tc, int oparg);
5336+
53405337
void HIRBuilder::emitStoreAttr(
53415338
TranslationContext& tc,
53425339
const jit::BytecodeInstruction& bc_instr) {
5343-
Register* receiver = static_cast<Register*>(phx_ptr_arr_pop(&tc.frame.stack));
5344-
Register* value = static_cast<Register*>(phx_ptr_arr_pop(&tc.frame.stack));
5345-
5346-
tc.emitStoreAttr(receiver, value, bc_instr.oparg(), tc.frame);
5340+
hir_builder_emit_store_attr_c(static_cast<void*>(&tc), bc_instr.oparg());
53475341
}
53485342

53495343
void HIRBuilder::moveOverwrittenStackRegisters(

Python/jit/hir/builder_emit_c.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,37 @@ void hir_builder_emit_contains_op_c(PhxTranslationContext *tc, void *func, int o
274274
phx_tc_emit(tc, hir_c_create_compare_reg(result, op, left, right, &tc->frame));
275275
phx_ptr_arr_push(&tc->frame.stack, result);
276276
}
277+
278+
/* emitStoreAttr — pop receiver + value, emit StoreAttr */
279+
extern void *hir_c_create_store_attr_reg(void *recv, void *val, int name_idx, void *fs);
280+
281+
void hir_builder_emit_store_attr_c(PhxTranslationContext *tc, int oparg) {
282+
void *receiver = phx_ptr_arr_pop(&tc->frame.stack);
283+
void *value = phx_ptr_arr_pop(&tc->frame.stack);
284+
phx_tc_emit(tc, hir_c_create_store_attr_reg(receiver, value, oparg, &tc->frame));
285+
}
286+
287+
/* emitLoadType — pop instance, LoadField ob_type, push */
288+
extern void *hir_c_create_load_field_reg(void *dst, void *recv, const char *name,
289+
intptr_t offset, HirType type, int borrowed);
290+
291+
void hir_builder_emit_load_type_c(PhxTranslationContext *tc, void *func) {
292+
void *instance = phx_ptr_arr_pop(&tc->frame.stack);
293+
void *type_reg = hir_func_alloc_register(func);
294+
HirType t_type = HIR_TYPE_TYPE;
295+
phx_tc_emit(tc, hir_c_create_load_field_reg(type_reg, instance, "ob_type",
296+
offsetof(PyObject, ob_type), t_type, 0));
297+
phx_ptr_arr_push(&tc->frame.stack, type_reg);
298+
}
299+
300+
/* emitCopyDictWithoutKeys — peek keys+subject, emit, replace top */
301+
extern void *hir_c_create_copy_dict_without_keys_reg(void *dst, void *subj, void *keys, void *fs);
302+
303+
void hir_builder_emit_copy_dict_without_keys_c(PhxTranslationContext *tc, void *func) {
304+
PhxPtrArray *stack = &tc->frame.stack;
305+
void *keys = stack->data[stack->count - 1];
306+
void *subject = stack->data[stack->count - 2];
307+
void *rest = hir_func_alloc_register(func);
308+
phx_tc_emit(tc, hir_c_create_copy_dict_without_keys_reg(rest, subject, keys, &tc->frame));
309+
stack->data[stack->count - 1] = rest;
310+
}

0 commit comments

Comments
 (0)