up

Update codeobject.h
This commit is contained in:
blueloveTH 2023-02-10 04:48:50 +08:00
parent e36eb9ff19
commit c35eff5a21
3 changed files with 16 additions and 34 deletions

View File

@ -86,34 +86,7 @@ struct CodeObject {
}
void optimize(){
for(int i=0; i<codes.size(); i++){
if(codes[i].op >= OP_BINARY_OP && codes[i].op <= OP_CONTAINS_OP){
for(int j=0; j<2; j++){
Bytecode& bc = codes[i-j-1];
if(bc.op >= OP_LOAD_CONST && bc.op <= OP_LOAD_NAME_REF){
if(bc.op == OP_LOAD_NAME_REF){
bc.op = OP_LOAD_NAME;
}
}else{
break;
}
}
}else if(codes[i].op == OP_CALL){
int ARGC = codes[i].arg & 0xFFFF;
int KWARGC = (codes[i].arg >> 16) & 0xFFFF;
if(KWARGC != 0) continue;
for(int j=0; j<ARGC+1; j++){
Bytecode& bc = codes[i-j-1];
if(bc.op >= OP_LOAD_CONST && bc.op <= OP_LOAD_NAME_REF){
if(bc.op == OP_LOAD_NAME_REF){
bc.op = OP_LOAD_NAME;
}
}else{
break;
}
}
}
}
for(int i=0; i<codes.size(); i++){}
}
/************************************************/

View File

@ -597,11 +597,11 @@ __LISTCOMP:
const _Str& key = parser->prev.str();
emit(OP_LOAD_CONST, co()->add_const(vm->PyStr(key)));
consume(TK("="));
EXPR();
co()->_rvalue=true; EXPR(); co()->_rvalue=false;
KWARGC++;
} else{
if(KWARGC > 0) SyntaxError("positional argument follows keyword argument");
EXPR();
co()->_rvalue=true; EXPR(); co()->_rvalue=false;
ARGC++;
}
match_newlines(mode()==REPL_MODE);
@ -758,8 +758,9 @@ __LISTCOMP:
void compile_if_stmt() {
match_newlines();
EXPR_TUPLE();
co()->_rvalue = true;
EXPR_TUPLE(); // condition
co()->_rvalue = false;
int ifpatch = emit(OP_POP_JUMP_IF_FALSE);
compile_block_body();
@ -780,7 +781,9 @@ __LISTCOMP:
void compile_while_loop() {
co()->_enter_block(WHILE_LOOP);
EXPR_TUPLE();
co()->_rvalue = true;
EXPR_TUPLE(); // condition
co()->_rvalue = false;
int patch = emit(OP_POP_JUMP_IF_FALSE);
compile_block_body();
emit(OP_LOOP_CONTINUE, -1, true);
@ -848,7 +851,9 @@ __LISTCOMP:
if(match_end_stmt()){
emit(OP_LOAD_NONE);
}else{
EXPR_TUPLE();
co()->_rvalue = true;
EXPR_TUPLE(); // return value
co()->_rvalue = false;
consume_end_stmt();
}
emit(OP_RETURN_VALUE, -1, true);

View File

@ -13,4 +13,8 @@ def test(n):
k += 1
return k
# from dis import dis
# dis(test)
# dis(is_prime)
print(test(10000))