This commit is contained in:
blueloveTH 2023-04-04 21:24:25 +08:00
parent 91f38518ff
commit 1e73f7e1bf
2 changed files with 354 additions and 344 deletions

View File

@ -5,8 +5,10 @@
namespace pkpy{ namespace pkpy{
#define DISPATCH() goto __NEXT_STEP
inline PyObject* VM::run_frame(Frame* frame){ inline PyObject* VM::run_frame(Frame* frame){
while(true){ __NEXT_STEP:;
/* NOTE: /* NOTE:
* Be aware of accidental gc! * Be aware of accidental gc!
* DO NOT leave any strong reference of PyObject* in the C stack * DO NOT leave any strong reference of PyObject* in the C stack
@ -18,29 +20,29 @@ inline PyObject* VM::run_frame(Frame* frame){
const Bytecode& byte = frame->next_bytecode(); const Bytecode& byte = frame->next_bytecode();
switch (byte.op) switch (byte.op)
{ {
case OP_NO_OP: continue; case OP_NO_OP: DISPATCH();
/*****************************************/ /*****************************************/
case OP_POP_TOP: frame->pop(); continue; case OP_POP_TOP: frame->pop(); DISPATCH();
case OP_DUP_TOP: frame->push(frame->top()); continue; case OP_DUP_TOP: frame->push(frame->top()); DISPATCH();
case OP_ROT_TWO: std::swap(frame->top(), frame->top_1()); continue; case OP_ROT_TWO: std::swap(frame->top(), frame->top_1()); DISPATCH();
case OP_PRINT_EXPR: { case OP_PRINT_EXPR: {
PyObject* obj = frame->top(); // use top() to avoid accidental gc PyObject* obj = frame->top(); // use top() to avoid accidental gc
if(obj != None) *_stdout << CAST(Str, asRepr(obj)) << '\n'; if(obj != None) *_stdout << CAST(Str, asRepr(obj)) << '\n';
frame->pop(); frame->pop();
} continue; } DISPATCH();
/*****************************************/ /*****************************************/
case OP_LOAD_CONST: frame->push(frame->co->consts[byte.arg]); continue; case OP_LOAD_CONST: frame->push(frame->co->consts[byte.arg]); DISPATCH();
case OP_LOAD_NONE: frame->push(None); continue; case OP_LOAD_NONE: frame->push(None); DISPATCH();
case OP_LOAD_TRUE: frame->push(True); continue; case OP_LOAD_TRUE: frame->push(True); DISPATCH();
case OP_LOAD_FALSE: frame->push(False); continue; case OP_LOAD_FALSE: frame->push(False); DISPATCH();
case OP_LOAD_ELLIPSIS: frame->push(Ellipsis); continue; case OP_LOAD_ELLIPSIS: frame->push(Ellipsis); DISPATCH();
case OP_LOAD_BUILTIN_EVAL: frame->push(builtins->attr(m_eval)); continue; case OP_LOAD_BUILTIN_EVAL: frame->push(builtins->attr(m_eval)); DISPATCH();
case OP_LOAD_FUNCTION: { case OP_LOAD_FUNCTION: {
PyObject* obj = frame->co->consts[byte.arg]; PyObject* obj = frame->co->consts[byte.arg];
Function f = CAST(Function, obj); // copy it! Function f = CAST(Function, obj); // copy it!
f._module = frame->_module; // setup module f._module = frame->_module; // setup module
frame->push(VAR(std::move(f))); frame->push(VAR(std::move(f)));
} continue; } DISPATCH();
/*****************************************/ /*****************************************/
case OP_LOAD_NAME: { case OP_LOAD_NAME: {
StrName name = frame->co->names[byte.arg]; StrName name = frame->co->names[byte.arg];
@ -48,43 +50,46 @@ inline PyObject* VM::run_frame(Frame* frame){
int i = 0; // names[0] is ensured to be non-null int i = 0; // names[0] is ensured to be non-null
do{ do{
val = frame->names[i++]->try_get(name); val = frame->names[i++]->try_get(name);
if(val != nullptr){ frame->push(val); break; } if(val != nullptr){
frame->push(val);
DISPATCH();
}
}while(frame->names[i] != nullptr); }while(frame->names[i] != nullptr);
vm->NameError(name); vm->NameError(name);
} continue; } DISPATCH();
case OP_LOAD_ATTR: { case OP_LOAD_ATTR: {
PyObject* a = frame->top(); PyObject* a = frame->top();
StrName name = frame->co->names[byte.arg]; StrName name = frame->co->names[byte.arg];
frame->top() = getattr(a, name); frame->top() = getattr(a, name);
} continue; } DISPATCH();
case OP_LOAD_SUBSCR: { case OP_LOAD_SUBSCR: {
Args args(2); Args args(2);
args[1] = frame->popx(); // b args[1] = frame->popx(); // b
args[0] = frame->top(); // a args[0] = frame->top(); // a
frame->top() = fast_call(__getitem__, std::move(args)); frame->top() = fast_call(__getitem__, std::move(args));
} continue; } DISPATCH();
case OP_STORE_LOCAL: { case OP_STORE_LOCAL: {
StrName name = frame->co->names[byte.arg]; StrName name = frame->co->names[byte.arg];
frame->f_locals().set(name, frame->popx()); frame->f_locals().set(name, frame->popx());
} continue; } DISPATCH();
case OP_STORE_GLOBAL: { case OP_STORE_GLOBAL: {
StrName name = frame->co->names[byte.arg]; StrName name = frame->co->names[byte.arg];
frame->f_globals().set(name, frame->popx()); frame->f_globals().set(name, frame->popx());
} continue; } DISPATCH();
case OP_STORE_ATTR: { case OP_STORE_ATTR: {
StrName name = frame->co->names[byte.arg]; StrName name = frame->co->names[byte.arg];
PyObject* a = frame->top(); PyObject* a = frame->top();
PyObject* val = frame->top_1(); PyObject* val = frame->top_1();
setattr(a, name, val); setattr(a, name, val);
frame->pop_n(2); frame->pop_n(2);
} continue; } DISPATCH();
case OP_STORE_SUBSCR: { case OP_STORE_SUBSCR: {
Args args(3); Args args(3);
args[1] = frame->popx(); // b args[1] = frame->popx(); // b
args[0] = frame->popx(); // a args[0] = frame->popx(); // a
args[2] = frame->popx(); // val args[2] = frame->popx(); // val
fast_call(__setitem__, std::move(args)); fast_call(__setitem__, std::move(args));
} continue; } DISPATCH();
case OP_DELETE_LOCAL: { case OP_DELETE_LOCAL: {
StrName name = frame->co->names[byte.arg]; StrName name = frame->co->names[byte.arg];
if(frame->f_locals().contains(name)){ if(frame->f_locals().contains(name)){
@ -92,7 +97,7 @@ inline PyObject* VM::run_frame(Frame* frame){
}else{ }else{
NameError(name); NameError(name);
} }
} continue; } DISPATCH();
case OP_DELETE_GLOBAL: { case OP_DELETE_GLOBAL: {
StrName name = frame->co->names[byte.arg]; StrName name = frame->co->names[byte.arg];
if(frame->f_globals().contains(name)){ if(frame->f_globals().contains(name)){
@ -100,33 +105,33 @@ inline PyObject* VM::run_frame(Frame* frame){
}else{ }else{
NameError(name); NameError(name);
} }
} continue; } DISPATCH();
case OP_DELETE_ATTR: { case OP_DELETE_ATTR: {
PyObject* a = frame->popx(); PyObject* a = frame->popx();
StrName name = frame->co->names[byte.arg]; StrName name = frame->co->names[byte.arg];
if(!a->is_attr_valid()) TypeError("cannot delete attribute"); if(!a->is_attr_valid()) TypeError("cannot delete attribute");
if(!a->attr().contains(name)) AttributeError(a, name); if(!a->attr().contains(name)) AttributeError(a, name);
a->attr().erase(name); a->attr().erase(name);
} continue; } DISPATCH();
case OP_DELETE_SUBSCR: { case OP_DELETE_SUBSCR: {
PyObject* b = frame->popx(); PyObject* b = frame->popx();
PyObject* a = frame->popx(); PyObject* a = frame->popx();
fast_call(__delitem__, Args{a, b}); fast_call(__delitem__, Args{a, b});
} continue; } DISPATCH();
/*****************************************/ /*****************************************/
case OP_BUILD_LIST: case OP_BUILD_LIST:
frame->push(VAR(frame->popx_n_reversed(byte.arg).to_list())); frame->push(VAR(frame->popx_n_reversed(byte.arg).to_list()));
continue; DISPATCH();
case OP_BUILD_DICT: { case OP_BUILD_DICT: {
PyObject* t = VAR(frame->popx_n_reversed(byte.arg)); PyObject* t = VAR(frame->popx_n_reversed(byte.arg));
PyObject* obj = call(builtins->attr(m_dict), Args{t}); PyObject* obj = call(builtins->attr(m_dict), Args{t});
frame->push(obj); frame->push(obj);
} continue; } DISPATCH();
case OP_BUILD_SET: { case OP_BUILD_SET: {
PyObject* t = VAR(frame->popx_n_reversed(byte.arg)); PyObject* t = VAR(frame->popx_n_reversed(byte.arg));
PyObject* obj = call(builtins->attr(m_set), Args{t}); PyObject* obj = call(builtins->attr(m_set), Args{t});
frame->push(obj); frame->push(obj);
} continue; } DISPATCH();
case OP_BUILD_SLICE: { case OP_BUILD_SLICE: {
PyObject* step = frame->popx(); PyObject* step = frame->popx();
PyObject* stop = frame->popx(); PyObject* stop = frame->popx();
@ -136,11 +141,11 @@ inline PyObject* VM::run_frame(Frame* frame){
if(stop != None) s.stop = CAST(int, stop); if(stop != None) s.stop = CAST(int, stop);
if(step != None) s.step = CAST(int, step); if(step != None) s.step = CAST(int, step);
frame->push(VAR(s)); frame->push(VAR(s));
} continue; } DISPATCH();
case OP_BUILD_TUPLE: { case OP_BUILD_TUPLE: {
Tuple items = frame->popx_n_reversed(byte.arg); Tuple items = frame->popx_n_reversed(byte.arg);
frame->push(VAR(std::move(items))); frame->push(VAR(std::move(items)));
} continue; } DISPATCH();
case OP_BUILD_STRING: { case OP_BUILD_STRING: {
// asStr() may run extra bytecode // asStr() may run extra bytecode
// so we use top_n_reversed() in order to avoid accidental gc // so we use top_n_reversed() in order to avoid accidental gc
@ -149,33 +154,33 @@ inline PyObject* VM::run_frame(Frame* frame){
for(int i=0; i<items.size(); i++) ss << CAST(Str, asStr(items[i])); for(int i=0; i<items.size(); i++) ss << CAST(Str, asStr(items[i]));
frame->pop_n(byte.arg); frame->pop_n(byte.arg);
frame->push(VAR(ss.str())); frame->push(VAR(ss.str()));
} continue; } DISPATCH();
/*****************************************/ /*****************************************/
case OP_BINARY_OP: { case OP_BINARY_OP: {
Args args(2); Args args(2);
args[1] = frame->popx(); // lhs args[1] = frame->popx(); // lhs
args[0] = frame->top(); // rhs args[0] = frame->top(); // rhs
frame->top() = fast_call(BINARY_SPECIAL_METHODS[byte.arg], std::move(args)); frame->top() = fast_call(BINARY_SPECIAL_METHODS[byte.arg], std::move(args));
} continue; } DISPATCH();
case OP_COMPARE_OP: { case OP_COMPARE_OP: {
Args args(2); Args args(2);
args[1] = frame->popx(); // lhs args[1] = frame->popx(); // lhs
args[0] = frame->top(); // rhs args[0] = frame->top(); // rhs
frame->top() = fast_call(COMPARE_SPECIAL_METHODS[byte.arg], std::move(args)); frame->top() = fast_call(COMPARE_SPECIAL_METHODS[byte.arg], std::move(args));
} continue; } DISPATCH();
case OP_BITWISE_OP: { case OP_BITWISE_OP: {
Args args(2); Args args(2);
args[1] = frame->popx(); // lhs args[1] = frame->popx(); // lhs
args[0] = frame->top(); // rhs args[0] = frame->top(); // rhs
frame->top() = fast_call(BITWISE_SPECIAL_METHODS[byte.arg], std::move(args)); frame->top() = fast_call(BITWISE_SPECIAL_METHODS[byte.arg], std::move(args));
} continue; } DISPATCH();
case OP_IS_OP: { case OP_IS_OP: {
PyObject* rhs = frame->popx(); PyObject* rhs = frame->popx();
PyObject* lhs = frame->top(); PyObject* lhs = frame->top();
bool ret_c = lhs == rhs; bool ret_c = lhs == rhs;
if(byte.arg == 1) ret_c = !ret_c; if(byte.arg == 1) ret_c = !ret_c;
frame->top() = VAR(ret_c); frame->top() = VAR(ret_c);
} continue; } DISPATCH();
case OP_CONTAINS_OP: { case OP_CONTAINS_OP: {
Args args(2); Args args(2);
args[0] = frame->popx(); args[0] = frame->popx();
@ -184,34 +189,34 @@ inline PyObject* VM::run_frame(Frame* frame){
bool ret_c = CAST(bool, ret); bool ret_c = CAST(bool, ret);
if(byte.arg == 1) ret_c = !ret_c; if(byte.arg == 1) ret_c = !ret_c;
frame->top() = VAR(ret_c); frame->top() = VAR(ret_c);
} continue; } DISPATCH();
/*****************************************/ /*****************************************/
case OP_JUMP_ABSOLUTE: frame->jump_abs(byte.arg); continue; case OP_JUMP_ABSOLUTE: frame->jump_abs(byte.arg); DISPATCH();
case OP_POP_JUMP_IF_FALSE: case OP_POP_JUMP_IF_FALSE:
if(!asBool(frame->popx())) frame->jump_abs(byte.arg); if(!asBool(frame->popx())) frame->jump_abs(byte.arg);
continue; DISPATCH();
case OP_JUMP_IF_TRUE_OR_POP: case OP_JUMP_IF_TRUE_OR_POP:
if(asBool(frame->top()) == true) frame->jump_abs(byte.arg); if(asBool(frame->top()) == true) frame->jump_abs(byte.arg);
else frame->pop(); else frame->pop();
continue; DISPATCH();
case OP_JUMP_IF_FALSE_OR_POP: case OP_JUMP_IF_FALSE_OR_POP:
if(asBool(frame->top()) == false) frame->jump_abs(byte.arg); if(asBool(frame->top()) == false) frame->jump_abs(byte.arg);
else frame->pop(); else frame->pop();
continue; DISPATCH();
case OP_LOOP_CONTINUE: { case OP_LOOP_CONTINUE: {
int target = frame->co->blocks[byte.block].start; int target = frame->co->blocks[byte.block].start;
frame->jump_abs(target); frame->jump_abs(target);
} continue; } DISPATCH();
case OP_LOOP_BREAK: { case OP_LOOP_BREAK: {
int target = frame->co->blocks[byte.block].end; int target = frame->co->blocks[byte.block].end;
frame->jump_abs_break(target); frame->jump_abs_break(target);
} continue; } DISPATCH();
case OP_GOTO: { case OP_GOTO: {
StrName label = frame->co->names[byte.arg]; StrName label = frame->co->names[byte.arg];
auto it = frame->co->labels.find(label); auto it = frame->co->labels.find(label);
if(it == frame->co->labels.end()) _error("KeyError", "label " + label.str().escape(true) + " not found"); if(it == frame->co->labels.end()) _error("KeyError", "label " + label.str().escape(true) + " not found");
frame->jump_abs_break(it->second); frame->jump_abs_break(it->second);
} continue; } DISPATCH();
/*****************************************/ /*****************************************/
// TODO: examine this later // TODO: examine this later
case OP_CALL: case OP_CALL_UNPACK: { case OP_CALL: case OP_CALL_UNPACK: {
@ -221,7 +226,7 @@ inline PyObject* VM::run_frame(Frame* frame){
PyObject* ret = call(callable, std::move(args), no_arg(), true); PyObject* ret = call(callable, std::move(args), no_arg(), true);
if(ret == _py_op_call) return ret; if(ret == _py_op_call) return ret;
frame->push(std::move(ret)); frame->push(std::move(ret));
} continue; } DISPATCH();
case OP_CALL_KWARGS: case OP_CALL_KWARGS_UNPACK: { case OP_CALL_KWARGS: case OP_CALL_KWARGS_UNPACK: {
int ARGC = byte.arg & 0xFFFF; int ARGC = byte.arg & 0xFFFF;
int KWARGC = (byte.arg >> 16) & 0xFFFF; int KWARGC = (byte.arg >> 16) & 0xFFFF;
@ -232,38 +237,38 @@ inline PyObject* VM::run_frame(Frame* frame){
PyObject* ret = call(callable, std::move(args), kwargs, true); PyObject* ret = call(callable, std::move(args), kwargs, true);
if(ret == _py_op_call) return ret; if(ret == _py_op_call) return ret;
frame->push(std::move(ret)); frame->push(std::move(ret));
} continue; } DISPATCH();
case OP_RETURN_VALUE: return frame->popx(); case OP_RETURN_VALUE: return frame->popx();
/*****************************************/ /*****************************************/
case OP_LIST_APPEND: { case OP_LIST_APPEND: {
PyObject* obj = frame->popx(); PyObject* obj = frame->popx();
List& list = CAST(List&, frame->top_1()); List& list = CAST(List&, frame->top_1());
list.push_back(obj); list.push_back(obj);
} continue; } DISPATCH();
case OP_DICT_ADD: { case OP_DICT_ADD: {
PyObject* kv = frame->popx(); PyObject* kv = frame->popx();
// we do copy here to avoid accidental gc in `kv` // we do copy here to avoid accidental gc in `kv`
// TODO: optimize to avoid copy // TODO: optimize to avoid copy
call(frame->top_1(), __setitem__, CAST(Tuple, kv)); call(frame->top_1(), __setitem__, CAST(Tuple, kv));
} continue; } DISPATCH();
case OP_SET_ADD: { case OP_SET_ADD: {
PyObject* obj = frame->popx(); PyObject* obj = frame->popx();
call(frame->top_1(), m_add, Args{obj}); call(frame->top_1(), m_add, Args{obj});
} continue; } DISPATCH();
/*****************************************/ /*****************************************/
case OP_UNARY_NEGATIVE: case OP_UNARY_NEGATIVE:
frame->top() = num_negated(frame->top()); frame->top() = num_negated(frame->top());
continue; DISPATCH();
case OP_UNARY_NOT: case OP_UNARY_NOT:
frame->top() = VAR(!asBool(frame->top())); frame->top() = VAR(!asBool(frame->top()));
continue; DISPATCH();
case OP_UNARY_STAR: case OP_UNARY_STAR:
frame->top() = VAR(StarWrapper(frame->top())); frame->top() = VAR(StarWrapper(frame->top()));
continue; DISPATCH();
/*****************************************/ /*****************************************/
case OP_GET_ITER: case OP_GET_ITER:
frame->top() = asIter(frame->top()); frame->top() = asIter(frame->top());
continue; DISPATCH();
case OP_FOR_ITER: { case OP_FOR_ITER: {
BaseIter* it = PyIter_AS_C(frame->top()); BaseIter* it = PyIter_AS_C(frame->top());
PyObject* obj = it->next(); PyObject* obj = it->next();
@ -273,7 +278,7 @@ inline PyObject* VM::run_frame(Frame* frame){
int target = frame->co->blocks[byte.block].end; int target = frame->co->blocks[byte.block].end;
frame->jump_abs_break(target); frame->jump_abs_break(target);
} }
} continue; } DISPATCH();
/*****************************************/ /*****************************************/
case OP_IMPORT_NAME: { case OP_IMPORT_NAME: {
StrName name = frame->co->names[byte.arg]; StrName name = frame->co->names[byte.arg];
@ -295,7 +300,7 @@ inline PyObject* VM::run_frame(Frame* frame){
new_mod->attr()._try_perfect_rehash(); new_mod->attr()._try_perfect_rehash();
} }
frame->push(ext_mod); frame->push(ext_mod);
} continue; } DISPATCH();
case OP_IMPORT_STAR: { case OP_IMPORT_STAR: {
PyObject* obj = frame->popx(); PyObject* obj = frame->popx();
for(auto& [name, value]: obj->attr().items()){ for(auto& [name, value]: obj->attr().items()){
@ -303,14 +308,14 @@ inline PyObject* VM::run_frame(Frame* frame){
if(s.empty() || s[0] == '_') continue; if(s.empty() || s[0] == '_') continue;
frame->f_globals().set(name, value); frame->f_globals().set(name, value);
} }
}; continue; }; DISPATCH();
/*****************************************/ /*****************************************/
/*****************************************/ /*****************************************/
// case OP_SETUP_DECORATOR: continue; // case OP_SETUP_DECORATOR: DISPATCH();
// case OP_SETUP_CLOSURE: { // case OP_SETUP_CLOSURE: {
// Function& f = CAST(Function&, frame->top()); // reference // Function& f = CAST(Function&, frame->top()); // reference
// f._closure = frame->_locals; // f._closure = frame->_locals;
// } continue; // } DISPATCH();
// case OP_BEGIN_CLASS: { // case OP_BEGIN_CLASS: {
// StrName name = frame->co->names[byte.arg]; // StrName name = frame->co->names[byte.arg];
// PyObject* clsBase = frame->popx(); // PyObject* clsBase = frame->popx();
@ -318,45 +323,46 @@ inline PyObject* VM::run_frame(Frame* frame){
// check_type(clsBase, tp_type); // check_type(clsBase, tp_type);
// PyObject* cls = new_type_object(frame->_module, name, OBJ_GET(Type, clsBase)); // PyObject* cls = new_type_object(frame->_module, name, OBJ_GET(Type, clsBase));
// frame->push(cls); // frame->push(cls);
// } continue; // } DISPATCH();
// case OP_END_CLASS: { // case OP_END_CLASS: {
// PyObject* cls = frame->popx(); // PyObject* cls = frame->popx();
// cls->attr()._try_perfect_rehash(); // cls->attr()._try_perfect_rehash();
// }; continue; // }; DISPATCH();
// case OP_STORE_CLASS_ATTR: { // case OP_STORE_CLASS_ATTR: {
// StrName name = frame->co->names[byte.arg]; // StrName name = frame->co->names[byte.arg];
// PyObject* obj = frame->popx(); // PyObject* obj = frame->popx();
// PyObject* cls = frame->top(); // PyObject* cls = frame->top();
// cls->attr().set(name, obj); // cls->attr().set(name, obj);
// } continue; // } DISPATCH();
// case OP_ASSERT: { // case OP_ASSERT: {
// PyObject* _msg = frame->pop_value(this); // PyObject* _msg = frame->pop_value(this);
// Str msg = CAST(Str, asStr(_msg)); // Str msg = CAST(Str, asStr(_msg));
// PyObject* expr = frame->pop_value(this); // PyObject* expr = frame->pop_value(this);
// if(asBool(expr) != True) _error("AssertionError", msg); // if(asBool(expr) != True) _error("AssertionError", msg);
// } continue; // } DISPATCH();
// case OP_EXCEPTION_MATCH: { // case OP_EXCEPTION_MATCH: {
// const auto& e = CAST(Exception&, frame->top()); // const auto& e = CAST(Exception&, frame->top());
// StrName name = frame->co->names[byte.arg].first; // StrName name = frame->co->names[byte.arg].first;
// frame->push(VAR(e.match_type(name))); // frame->push(VAR(e.match_type(name)));
// } continue; // } DISPATCH();
// case OP_RAISE: { // case OP_RAISE: {
// PyObject* obj = frame->pop_value(this); // PyObject* obj = frame->pop_value(this);
// Str msg = obj == None ? "" : CAST(Str, asStr(obj)); // Str msg = obj == None ? "" : CAST(Str, asStr(obj));
// StrName type = frame->co->names[byte.arg].first; // StrName type = frame->co->names[byte.arg].first;
// _error(type, msg); // _error(type, msg);
// } continue; // } DISPATCH();
// case OP_RE_RAISE: _raise(); continue; // case OP_RE_RAISE: _raise(); DISPATCH();
// case OP_YIELD_VALUE: return _py_op_yield; // case OP_YIELD_VALUE: return _py_op_yield;
// // TODO: using "goto" inside with block may cause __exit__ not called // // TODO: using "goto" inside with block may cause __exit__ not called
// case OP_WITH_ENTER: call(frame->pop_value(this), __enter__, no_arg()); continue; // case OP_WITH_ENTER: call(frame->pop_value(this), __enter__, no_arg()); DISPATCH();
// case OP_WITH_EXIT: call(frame->pop_value(this), __exit__, no_arg()); continue; // case OP_WITH_EXIT: call(frame->pop_value(this), __exit__, no_arg()); DISPATCH();
// case OP_TRY_BLOCK_ENTER: frame->on_try_block_enter(); continue; // case OP_TRY_BLOCK_ENTER: frame->on_try_block_enter(); DISPATCH();
// case OP_TRY_BLOCK_EXIT: frame->on_try_block_exit(); continue; // case OP_TRY_BLOCK_EXIT: frame->on_try_block_exit(); DISPATCH();
default: throw std::runtime_error(Str("opcode ") + OP_NAMES[byte.op] + " is not implemented"); default: throw std::runtime_error(Str("opcode ") + OP_NAMES[byte.op] + " is not implemented");
} }
}
UNREACHABLE(); UNREACHABLE();
} }
#undef DISPATCH
} // namespace pkpy } // namespace pkpy

View File

@ -587,9 +587,11 @@ class Compiler {
bool try_compile_assignment(){ bool try_compile_assignment(){
Expr* lhs_p = ctx()->s_expr.top().get(); Expr* lhs_p = ctx()->s_expr.top().get();
bool inplace;
switch (curr().type) { switch (curr().type) {
case TK("+="): case TK("-="): case TK("*="): case TK("/="): case TK("//="): case TK("%="): case TK("+="): case TK("-="): case TK("*="): case TK("/="): case TK("//="): case TK("%="):
case TK("<<="): case TK(">>="): case TK("&="): case TK("|="): case TK("^="): { case TK("<<="): case TK(">>="): case TK("&="): case TK("|="): case TK("^="): {
inplace = true;
advance(); advance();
auto e = make_expr<BinaryExpr>(); auto e = make_expr<BinaryExpr>();
e->op = prev().type - 1; // -1 to remove = e->op = prev().type - 1; // -1 to remove =
@ -599,6 +601,7 @@ class Compiler {
ctx()->s_expr.push(std::move(e)); ctx()->s_expr.push(std::move(e));
} break; } break;
case TK("="): case TK("="):
inplace = false;
advance(); advance();
EXPR_TUPLE(); EXPR_TUPLE();
break; break;
@ -608,6 +611,7 @@ class Compiler {
rhs->emit(ctx()); rhs->emit(ctx());
bool ok = lhs_p->emit_store(ctx()); bool ok = lhs_p->emit_store(ctx());
if(!ok) SyntaxError(); if(!ok) SyntaxError();
if(!inplace) ctx()->s_expr.pop();
return true; return true;
} }