Add code in VM to handle nested exceptions correctly.

This commit is contained in:
Damien 2013-12-29 16:54:59 +00:00
parent dd12d1378f
commit 8f9e2ee157
2 changed files with 35 additions and 24 deletions

View File

@ -135,12 +135,9 @@ void mp_show_byte_code(const byte *ip, int len) {
printf("STORE_SUBSCR"); printf("STORE_SUBSCR");
break; break;
/*
case MP_BC_DUP_TOP: case MP_BC_DUP_TOP:
obj1 = *sp; printf("DUP_TOP");
PUSH(obj1);
break; break;
*/
case MP_BC_DUP_TOP_TWO: case MP_BC_DUP_TOP_TWO:
printf("DUP_TOP_TWO"); printf("DUP_TOP_TWO");
@ -195,22 +192,20 @@ void mp_show_byte_code(const byte *ip, int len) {
ip += unum; ip += unum;
} }
break; break;
*/
case MP_BC_SETUP_EXCEPT: case MP_BC_SETUP_EXCEPT:
DECODE_ULABEL; // except labels are always forward DECODE_ULABEL; // except labels are always forward
*++exc_sp = (machine_uint_t)ip + unum; printf("SETUP_EXCEPT %lu", ip + unum - ip_start);
*++exc_sp = (machine_uint_t)sp;
break; break;
case MP_BC_END_FINALLY: case MP_BC_END_FINALLY:
// not implemented
// if TOS is an exception, reraises the exception (3 values on TOS) // if TOS is an exception, reraises the exception (3 values on TOS)
// if TOS is an integer, does something else // if TOS is an integer, does something else
// if TOS is None, just pops it and continues // if TOS is None, just pops it and continues
// else error // else error
assert(0); printf("END_FINALLY");
break; break;
*/
case MP_BC_GET_ITER: case MP_BC_GET_ITER:
printf("GET_ITER"); printf("GET_ITER");
@ -221,22 +216,17 @@ void mp_show_byte_code(const byte *ip, int len) {
printf("FOR_ITER %lu", ip + unum - ip_start); printf("FOR_ITER %lu", ip + unum - ip_start);
break; break;
/*
case MP_BC_POP_BLOCK: case MP_BC_POP_BLOCK:
// pops block and restores the stack // pops block and restores the stack
assert(0); printf("POP_BLOCK");
break; break;
case MP_BC_POP_EXCEPT: case MP_BC_POP_EXCEPT:
// TODO need to work out how blocks work etc
// pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
assert(exc_sp >= &exc_stack[0]); printf("POP_EXCEPT");
//sp = (mp_obj_t*)(*exc_sp--);
//exc_sp--; // discard ip
exc_sp -= 2;
//sp += 3; // pop 3 exception values
break; break;
/*
case MP_BC_UNARY_OP: case MP_BC_UNARY_OP:
unum = *ip++; unum = *ip++;
*sp = rt_unary_op(unum, *sp); *sp = rt_unary_op(unum, *sp);
@ -270,12 +260,14 @@ void mp_show_byte_code(const byte *ip, int len) {
rt_list_append(sp[unum], sp[0]); rt_list_append(sp[unum], sp[0]);
sp++; sp++;
break; break;
*/
case MP_BC_BUILD_MAP: case MP_BC_BUILD_MAP:
DECODE_UINT; DECODE_UINT;
PUSH(rt_build_map(unum)); printf("BUILD_MAP %lu", unum);
break; break;
/*
case MP_BC_STORE_MAP: case MP_BC_STORE_MAP:
sp += 2; sp += 2;
rt_store_map(sp[0], sp[-2], sp[-1]); rt_store_map(sp[0], sp[-2], sp[-1]);

31
py/vm.c
View File

@ -61,8 +61,8 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **
mp_obj_t fast0 = fastn[0], fast1 = fastn[1], fast2 = fastn[2]; mp_obj_t fast0 = fastn[0], fast1 = fastn[1], fast2 = fastn[2];
nlr_buf_t nlr; nlr_buf_t nlr;
// on the exception stack we store (ip, sp) for each block volatile machine_uint_t currently_in_except_block = 0; // 0 or 1, to detect nested exceptions
machine_uint_t exc_stack[8]; machine_uint_t exc_stack[8]; // on the exception stack we store (ip, sp | X) for each block, X = previous value of currently_in_except_block
machine_uint_t *volatile exc_sp = &exc_stack[-1]; // stack grows up, exc_sp points to top of stack machine_uint_t *volatile exc_sp = &exc_stack[-1]; // stack grows up, exc_sp points to top of stack
// outer exception handling loop // outer exception handling loop
@ -275,7 +275,8 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **
case MP_BC_SETUP_EXCEPT: case MP_BC_SETUP_EXCEPT:
DECODE_ULABEL; // except labels are always forward DECODE_ULABEL; // except labels are always forward
*++exc_sp = (machine_uint_t)ip + unum; *++exc_sp = (machine_uint_t)ip + unum;
*++exc_sp = (machine_uint_t)sp; *++exc_sp = (((machine_uint_t)sp) | currently_in_except_block);
currently_in_except_block = 0; // in a try block now
break; break;
case MP_BC_END_FINALLY: case MP_BC_END_FINALLY:
@ -313,7 +314,8 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **
assert(exc_sp >= &exc_stack[0]); assert(exc_sp >= &exc_stack[0]);
//sp = (mp_obj_t*)(*exc_sp--); //sp = (mp_obj_t*)(*exc_sp--);
//exc_sp--; // discard ip //exc_sp--; // discard ip
exc_sp -= 2; currently_in_except_block = (exc_sp[0] & 1); // restore previous state
exc_sp -= 2; // pop back to previous exception handler
//sp += 3; // pop 3 exception values //sp += 3; // pop 3 exception values
break; break;
@ -466,16 +468,33 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **
} else { } else {
// exception occurred // exception occurred
while (currently_in_except_block) {
// nested exception
assert(exc_sp >= &exc_stack[0]);
// TODO make a proper message for nested exception
// at the moment we are just raising the very last exception (the one that caused the nested exception)
// move up to previous exception handler
currently_in_except_block = (exc_sp[0] & 1); // restore previous state
exc_sp -= 2; // pop back to previous exception handler
}
if (exc_sp >= &exc_stack[0]) { if (exc_sp >= &exc_stack[0]) {
// set flag to indicate that we are now handling an exception
currently_in_except_block = 1;
// catch exception and pass to byte code // catch exception and pass to byte code
sp = (mp_obj_t*)(exc_sp[0]); sp = (mp_obj_t*)(exc_sp[0] & (~((machine_uint_t)1)));
ip = (const byte*)(exc_sp[-1]); ip = (const byte*)(exc_sp[-1]);
// push(traceback, exc-val, exc-type) // push(traceback, exc-val, exc-type)
PUSH(mp_const_none); PUSH(mp_const_none);
PUSH(nlr.ret_val); PUSH(nlr.ret_val);
PUSH(mp_const_none); PUSH(mp_const_none);
} else { } else {
// re-raise exception // re-raise exception to higher level
// TODO what to do if this is a generator?? // TODO what to do if this is a generator??
nlr_jump(nlr.ret_val); nlr_jump(nlr.ret_val);
} }