From 82bd2838ce2136635797fd8582b5294c7c918709 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 6 Jan 2026 14:32:00 +0800 Subject: [PATCH] fix abs name --- include/pocketpy/xmacros/opcodes.h | 1 + src/compiler/compiler.c | 9 ++++++++- src/interpreter/ceval.c | 5 +++++ src/modules/dis.c | 2 +- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/include/pocketpy/xmacros/opcodes.h b/include/pocketpy/xmacros/opcodes.h index 3253cfd7..3a91ed38 100644 --- a/include/pocketpy/xmacros/opcodes.h +++ b/include/pocketpy/xmacros/opcodes.h @@ -16,6 +16,7 @@ OPCODE(LOAD_TRUE) OPCODE(LOAD_FALSE) /**************************/ OPCODE(LOAD_SMALL_INT) +OPCODE(LOAD_NAME_AS_INT) /**************************/ OPCODE(LOAD_ELLIPSIS) OPCODE(LOAD_FUNCTION) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index b374244c..641bcf89 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -77,6 +77,7 @@ static int Ctx__enter_block(Ctx* self, CodeBlockType type); static void Ctx__exit_block(Ctx* self); static int Ctx__emit_(Ctx* self, Opcode opcode, uint16_t arg, int line); static int Ctx__emit_int(Ctx* self, int64_t value, int line); +static int Ctx__emit_name(Ctx* self, py_Name name, int line); static void Ctx__patch_jump(Ctx* self, int index); static void Ctx__emit_jump(Ctx* self, int target, int line); static int Ctx__add_varname(Ctx* self, py_Name name); @@ -1070,7 +1071,7 @@ void CallExpr__emit_(Expr* self_, Ctx* ctx) { c11__foreach(Expr*, &self->args, e) { vtemit_(*e, ctx); } c11__foreach(CallExprKwArg, &self->kwargs, e) { - Ctx__emit_int(ctx, (uintptr_t)e->key, self->line); + Ctx__emit_name(ctx, e->key, self->line); vtemit_(e->val, ctx); } int KWARGC = self->kwargs.length; @@ -1196,6 +1197,12 @@ static int Ctx__emit_int(Ctx* self, int64_t value, int line) { } } +static int Ctx__emit_name(Ctx* self, py_Name name, int line) { + int index = Ctx__add_name(self, name); + assert(index <= UINT16_MAX); + return Ctx__emit_(self, OP_LOAD_NAME_AS_INT, (uint16_t)index, line); +} + static void Ctx__patch_jump(Ctx* self, int index) { Bytecode* co_codes = (Bytecode*)self->co->codes.data; int target = self->co->codes.length; diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index 0ef2c30c..fb16e3c8 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -189,6 +189,11 @@ __NEXT_STEP: py_newint(SP()++, (int16_t)byte.arg); DISPATCH(); } + case OP_LOAD_NAME_AS_INT: { + py_Name name = co_names[byte.arg]; + py_newint(SP()++, (uintptr_t)name); + DISPATCH(); + } /*****************************************/ case OP_LOAD_ELLIPSIS: { py_newellipsis(SP()++); diff --git a/src/modules/dis.c b/src/modules/dis.c index 175ae2db..fa1e10ab 100644 --- a/src/modules/dis.c +++ b/src/modules/dis.c @@ -72,7 +72,7 @@ static bool disassemble(CodeObject* co) { pk_sprintf(&ss, " (%q)", py_tosv(path)); break; } - case OP_LOAD_NAME: + case OP_LOAD_NAME: case OP_LOAD_NAME_AS_INT: case OP_LOAD_GLOBAL: case OP_LOAD_NONLOCAL: case OP_STORE_GLOBAL: