mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-10 05:30:16 +00:00
Compare commits
2 Commits
8f7e9f5f6c
...
a55f62c860
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a55f62c860 | ||
|
|
5dba2cd8e6 |
@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
#define BC_NOARG 0
|
#define BC_NOARG 0
|
||||||
#define BC_KEEPLINE -1
|
#define BC_KEEPLINE -1
|
||||||
|
#define BC_RETURN_VIRTUAL 5
|
||||||
|
|
||||||
typedef enum FuncType {
|
typedef enum FuncType {
|
||||||
FuncType_UNSET,
|
FuncType_UNSET,
|
||||||
@ -62,7 +63,6 @@ typedef struct CodeBlock {
|
|||||||
|
|
||||||
typedef struct BytecodeEx {
|
typedef struct BytecodeEx {
|
||||||
int lineno; // line number for each bytecode
|
int lineno; // line number for each bytecode
|
||||||
bool is_virtual; // whether this bytecode is virtual (not in source code)
|
|
||||||
int iblock; // block index
|
int iblock; // block index
|
||||||
} BytecodeEx;
|
} BytecodeEx;
|
||||||
|
|
||||||
|
|||||||
@ -9,3 +9,13 @@ y = 2
|
|||||||
costly_func(2) # 3s
|
costly_func(2) # 3s
|
||||||
|
|
||||||
time.sleep(1) # 1s
|
time.sleep(1) # 1s
|
||||||
|
|
||||||
|
def new_func(a, b, c):
|
||||||
|
x = a + b
|
||||||
|
x = x + c
|
||||||
|
return a
|
||||||
|
|
||||||
|
def new_func2(a, b, c):
|
||||||
|
x = a + b
|
||||||
|
x = x + c
|
||||||
|
return a
|
||||||
|
|||||||
@ -76,7 +76,6 @@ static int Ctx__prepare_loop_divert(Ctx* self, int line, bool is_break);
|
|||||||
static int Ctx__enter_block(Ctx* self, CodeBlockType type);
|
static int Ctx__enter_block(Ctx* self, CodeBlockType type);
|
||||||
static void Ctx__exit_block(Ctx* self);
|
static void Ctx__exit_block(Ctx* self);
|
||||||
static int Ctx__emit_(Ctx* self, Opcode opcode, uint16_t arg, int line);
|
static int Ctx__emit_(Ctx* self, Opcode opcode, uint16_t arg, int line);
|
||||||
static int Ctx__emit_virtual(Ctx* self, Opcode opcode, uint16_t arg, int line, bool virtual);
|
|
||||||
// static void Ctx__revert_last_emit_(Ctx* self);
|
// static void Ctx__revert_last_emit_(Ctx* self);
|
||||||
static int Ctx__emit_int(Ctx* self, int64_t value, int line);
|
static int Ctx__emit_int(Ctx* self, int64_t value, int line);
|
||||||
static void Ctx__patch_jump(Ctx* self, int index);
|
static void Ctx__patch_jump(Ctx* self, int index);
|
||||||
@ -1177,9 +1176,9 @@ static void Ctx__s_emit_decorators(Ctx* self, int count) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int Ctx__emit_virtual(Ctx* self, Opcode opcode, uint16_t arg, int line, bool is_virtual) {
|
static int Ctx__emit_(Ctx* self, Opcode opcode, uint16_t arg, int line) {
|
||||||
Bytecode bc = {(uint8_t)opcode, arg};
|
Bytecode bc = {(uint8_t)opcode, arg};
|
||||||
BytecodeEx bcx = {line, is_virtual, self->curr_iblock};
|
BytecodeEx bcx = {line, self->curr_iblock};
|
||||||
c11_vector__push(Bytecode, &self->co->codes, bc);
|
c11_vector__push(Bytecode, &self->co->codes, bc);
|
||||||
c11_vector__push(BytecodeEx, &self->co->codes_ex, bcx);
|
c11_vector__push(BytecodeEx, &self->co->codes_ex, bcx);
|
||||||
int i = self->co->codes.length - 1;
|
int i = self->co->codes.length - 1;
|
||||||
@ -1188,10 +1187,6 @@ static int Ctx__emit_virtual(Ctx* self, Opcode opcode, uint16_t arg, int line, b
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int Ctx__emit_(Ctx* self, Opcode opcode, uint16_t arg, int line) {
|
|
||||||
return Ctx__emit_virtual(self, opcode, arg, line, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// static void Ctx__revert_last_emit_(Ctx* self) {
|
// static void Ctx__revert_last_emit_(Ctx* self) {
|
||||||
// c11_vector__pop(&self->co->codes);
|
// c11_vector__pop(&self->co->codes);
|
||||||
// c11_vector__pop(&self->co->codes_ex);
|
// c11_vector__pop(&self->co->codes_ex);
|
||||||
@ -1512,7 +1507,7 @@ static Error* pop_context(Compiler* self) {
|
|||||||
// previously, we only do this if the last opcode is not a return
|
// previously, we only do this if the last opcode is not a return
|
||||||
// however, this is buggy...since there may be a jump to the end (out of bound) even if the last
|
// however, this is buggy...since there may be a jump to the end (out of bound) even if the last
|
||||||
// opcode is a return
|
// opcode is a return
|
||||||
Ctx__emit_virtual(ctx(), OP_RETURN_VALUE, 1, BC_KEEPLINE, true);
|
Ctx__emit_(ctx(), OP_RETURN_VALUE, BC_RETURN_VIRTUAL, BC_KEEPLINE);
|
||||||
|
|
||||||
CodeObject* co = ctx()->co;
|
CodeObject* co = ctx()->co;
|
||||||
// find the last valid token
|
// find the last valid token
|
||||||
@ -2421,6 +2416,7 @@ static Error* consume_pep695_py312(Compiler* self) {
|
|||||||
|
|
||||||
static Error* compile_function(Compiler* self, int decorators) {
|
static Error* compile_function(Compiler* self, int decorators) {
|
||||||
Error* err;
|
Error* err;
|
||||||
|
int def_line = prev()->line;
|
||||||
consume(TK_ID);
|
consume(TK_ID);
|
||||||
c11_sv decl_name_sv = Token__sv(prev());
|
c11_sv decl_name_sv = Token__sv(prev());
|
||||||
int decl_index;
|
int decl_index;
|
||||||
@ -2450,7 +2446,7 @@ static Error* compile_function(Compiler* self, int decorators) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ctx__emit_(ctx(), OP_LOAD_FUNCTION, decl_index, prev()->line);
|
Ctx__emit_(ctx(), OP_LOAD_FUNCTION, decl_index, def_line);
|
||||||
Ctx__s_emit_decorators(ctx(), decorators);
|
Ctx__s_emit_decorators(ctx(), decorators);
|
||||||
|
|
||||||
py_Name decl_name = py_namev(decl_name_sv);
|
py_Name decl_name = py_namev(decl_name_sv);
|
||||||
@ -2463,9 +2459,9 @@ static Error* compile_function(Compiler* self, int decorators) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ctx__emit_(ctx(), OP_STORE_CLASS_ATTR, Ctx__add_name(ctx(), decl_name), prev()->line);
|
Ctx__emit_(ctx(), OP_STORE_CLASS_ATTR, Ctx__add_name(ctx(), decl_name), def_line);
|
||||||
} else {
|
} else {
|
||||||
NameExpr* e = NameExpr__new(prev()->line, decl_name, name_scope(self));
|
NameExpr* e = NameExpr__new(def_line, decl_name, name_scope(self));
|
||||||
vtemit_store((Expr*)e, ctx());
|
vtemit_store((Expr*)e, ctx());
|
||||||
vtdelete((Expr*)e);
|
vtdelete((Expr*)e);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -105,13 +105,16 @@ __NEXT_STEP:
|
|||||||
byte = co_codes[frame->ip];
|
byte = co_codes[frame->ip];
|
||||||
|
|
||||||
if(self->trace_info.func) {
|
if(self->trace_info.func) {
|
||||||
SourceLocation loc = Frame__source_location(frame);
|
bool is_virtual = byte.op == OP_RETURN_VALUE && byte.arg == BC_RETURN_VIRTUAL;
|
||||||
SourceLocation prev_loc = self->trace_info.prev_loc;
|
if(!is_virtual) {
|
||||||
if(loc.lineno != prev_loc.lineno || loc.src != prev_loc.src) {
|
SourceLocation loc = Frame__source_location(frame);
|
||||||
if(prev_loc.src) PK_DECREF(prev_loc.src);
|
SourceLocation prev_loc = self->trace_info.prev_loc;
|
||||||
PK_INCREF(loc.src);
|
if(loc.lineno != prev_loc.lineno || loc.src != prev_loc.src) {
|
||||||
self->trace_info.prev_loc = loc;
|
if(prev_loc.src) PK_DECREF(prev_loc.src);
|
||||||
self->trace_info.func(frame, TRACE_EVENT_LINE);
|
PK_INCREF(loc.src);
|
||||||
|
self->trace_info.prev_loc = loc;
|
||||||
|
self->trace_info.func(frame, TRACE_EVENT_LINE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1219,14 +1222,15 @@ __NEXT_STEP:
|
|||||||
c11__unreachable();
|
c11__unreachable();
|
||||||
|
|
||||||
__ERROR:
|
__ERROR:
|
||||||
py_BaseException__stpush(frame, &self->curr_exception,
|
py_BaseException__stpush(frame,
|
||||||
|
&self->curr_exception,
|
||||||
frame->co->src,
|
frame->co->src,
|
||||||
Frame__lineno(frame),
|
Frame__lineno(frame),
|
||||||
!frame->is_locals_special ? frame->co->name->data : NULL);
|
!frame->is_locals_special ? frame->co->name->data : NULL);
|
||||||
__ERROR_RE_RAISE:
|
__ERROR_RE_RAISE:
|
||||||
do {
|
do {
|
||||||
} while(0);
|
} while(0);
|
||||||
|
|
||||||
int target = Frame__prepare_jump_exception_handler(frame, &self->stack);
|
int target = Frame__prepare_jump_exception_handler(frame, &self->stack);
|
||||||
if(target >= 0) {
|
if(target >= 0) {
|
||||||
// 1. Exception can be handled inside the current frame
|
// 1. Exception can be handled inside the current frame
|
||||||
@ -1306,7 +1310,10 @@ bool pk_stack_binaryop(VM* self, py_Name op, py_Name rop) {
|
|||||||
|
|
||||||
py_Type lhs_t = rop ? TOP()->type : SECOND()->type;
|
py_Type lhs_t = rop ? TOP()->type : SECOND()->type;
|
||||||
py_Type rhs_t = rop ? SECOND()->type : TOP()->type;
|
py_Type rhs_t = rop ? SECOND()->type : TOP()->type;
|
||||||
return TypeError("unsupported operand type(s) for '%s': '%t' and '%t'", pk_op2str(op), lhs_t, rhs_t);
|
return TypeError("unsupported operand type(s) for '%s': '%t' and '%t'",
|
||||||
|
pk_op2str(op),
|
||||||
|
lhs_t,
|
||||||
|
rhs_t);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool py_binaryop(py_Ref lhs, py_Ref rhs, py_Name op, py_Name rop) {
|
bool py_binaryop(py_Ref lhs, py_Ref rhs, py_Name op, py_Name rop) {
|
||||||
|
|||||||
@ -44,7 +44,7 @@ static bool disassemble(CodeObject* co) {
|
|||||||
c11_sbuf__write_cstr(&ss, buf);
|
c11_sbuf__write_cstr(&ss, buf);
|
||||||
|
|
||||||
c11_sbuf__write_cstr(&ss, pk_opname(byte.op));
|
c11_sbuf__write_cstr(&ss, pk_opname(byte.op));
|
||||||
c11_sbuf__write_char(&ss, ex.is_virtual ? '*' : ' ');
|
c11_sbuf__write_char(&ss, ' ');
|
||||||
int padding = 24 - strlen(pk_opname(byte.op));
|
int padding = 24 - strlen(pk_opname(byte.op));
|
||||||
for(int j = 0; j < padding; j++)
|
for(int j = 0; j < padding; j++)
|
||||||
c11_sbuf__write_char(&ss, ' ');
|
c11_sbuf__write_char(&ss, ' ');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user