Skip to content
Snippets Groups Projects
Commit c9f91976 authored by Damien's avatar Damien
Browse files

Crude try-except working.

parent ce89a21e
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
// args are in reverse order in array // args are in reverse order in array
py_obj_t py_execute_byte_code(const byte *code, uint len, const py_obj_t *args, uint n_args) { py_obj_t py_execute_byte_code(const byte *code, uint len, const py_obj_t *args, uint n_args) {
// careful: be sure to declare volatile any variables read in the exception handler (written is ok, I think)
const byte *ip = code; const byte *ip = code;
py_obj_t stack[10]; py_obj_t stack[10];
py_obj_t *sp = &stack[10]; // stack grows down, sp points to top of stack py_obj_t *sp = &stack[10]; // stack grows down, sp points to top of stack
...@@ -27,6 +29,10 @@ py_obj_t py_execute_byte_code(const byte *code, uint len, const py_obj_t *args, ...@@ -27,6 +29,10 @@ py_obj_t py_execute_byte_code(const byte *code, uint len, const py_obj_t *args,
py_obj_t fast0 = NULL, fast1 = NULL, fast2 = NULL, fastn[4] = {NULL, NULL, NULL, NULL}; py_obj_t fast0 = NULL, fast1 = NULL, fast2 = NULL, fastn[4] = {NULL, NULL, NULL, NULL};
nlr_buf_t nlr; nlr_buf_t nlr;
// on the exception stack we store (ip, sp) for each block
machine_uint_t exc_stack[8];
machine_uint_t *volatile exc_sp = &exc_stack[-1]; // stack grows up, exc_sp points to top of stack
// init args // init args
for (int i = 0; i < n_args; i++) { for (int i = 0; i < n_args; i++) {
if (i == 0) { if (i == 0) {
...@@ -203,8 +209,9 @@ py_obj_t py_execute_byte_code(const byte *code, uint len, const py_obj_t *args, ...@@ -203,8 +209,9 @@ py_obj_t py_execute_byte_code(const byte *code, uint len, const py_obj_t *args,
*/ */
case PYBC_SETUP_EXCEPT: case PYBC_SETUP_EXCEPT:
// push_block(PYBC_SETUP_EXCEPT, code + unum, sp) DECODE_UINT;
assert(0); *++exc_sp = (machine_uint_t)code + unum;
*++exc_sp = (machine_uint_t)sp;
break; break;
case PYBC_END_FINALLY: case PYBC_END_FINALLY:
...@@ -237,8 +244,14 @@ py_obj_t py_execute_byte_code(const byte *code, uint len, const py_obj_t *args, ...@@ -237,8 +244,14 @@ py_obj_t py_execute_byte_code(const byte *code, uint len, const py_obj_t *args,
break; break;
case PYBC_POP_EXCEPT: case PYBC_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(0); assert(exc_sp >= &exc_stack[0]);
//sp = (py_obj_t*)(*exc_sp--);
//exc_sp--; // discard ip
exc_sp -= 2;
//sp += 3; // pop 3 exception values
assert(sp <= &stack[10]);
break; break;
case PYBC_BINARY_OP: case PYBC_BINARY_OP:
...@@ -303,6 +316,8 @@ py_obj_t py_execute_byte_code(const byte *code, uint len, const py_obj_t *args, ...@@ -303,6 +316,8 @@ py_obj_t py_execute_byte_code(const byte *code, uint len, const py_obj_t *args,
case PYBC_RETURN_VALUE: case PYBC_RETURN_VALUE:
nlr_pop(); nlr_pop();
assert(sp == &stack[9]);
assert(exc_sp == &exc_stack[-1]);
return *sp; return *sp;
default: default:
...@@ -316,11 +331,14 @@ py_obj_t py_execute_byte_code(const byte *code, uint len, const py_obj_t *args, ...@@ -316,11 +331,14 @@ py_obj_t py_execute_byte_code(const byte *code, uint len, const py_obj_t *args,
} else { } else {
// exception occurred // exception occurred
if (0) { if (exc_sp >= &exc_stack[0]) {
// catch exception and pass to byte code // catch exception and pass to byte code
//ip = pop sp = (py_obj_t*)(exc_sp[0]);
//sp = pop ip = (const byte*)(exc_sp[-1]);
// push(traceback, exc-val, exc-type) // push(traceback, exc-val, exc-type)
PUSH(py_const_none);
PUSH(nlr.ret_val);
PUSH(py_const_none);
} else { } else {
// re-raise exception // re-raise exception
nlr_jump(nlr.ret_val); nlr_jump(nlr.ret_val);
......
// options to control how Micro Python is built // options to control how Micro Python is built
#define MICROPY_ENABLE_FLOAT (1) #define MICROPY_ENABLE_FLOAT (1)
#define MICROPY_EMIT_CPYTHON (1) #define MICROPY_EMIT_CPYTHON (0)
#define MICROPY_EMIT_X64 (0) #define MICROPY_EMIT_X64 (0)
#define MICROPY_EMIT_THUMB (0) #define MICROPY_EMIT_THUMB (0)
#define MICROPY_EMIT_INLINE_THUMB (0) #define MICROPY_EMIT_INLINE_THUMB (0)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment