Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 26 | #ifndef __MICROPY_INCLUDED_PY_NLR_H__ |
| 27 | #define __MICROPY_INCLUDED_PY_NLR_H__ |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 28 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 29 | // non-local return |
| 30 | // exception handling, basically a stack of setjmp/longjmp buffers |
| 31 | |
| 32 | #include <limits.h> |
Paul Sokolovsky | 3a83b80 | 2014-04-17 00:16:45 +0300 | [diff] [blame] | 33 | #include <setjmp.h> |
Emmanuel Blot | bf3366a | 2014-06-19 18:47:38 +0200 | [diff] [blame] | 34 | #include <assert.h> |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 35 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 36 | #include "py/mpconfig.h" |
| 37 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 38 | typedef struct _nlr_buf_t nlr_buf_t; |
| 39 | struct _nlr_buf_t { |
| 40 | // the entries here must all be machine word size |
| 41 | nlr_buf_t *prev; |
| 42 | void *ret_val; |
Damien George | 7860c2a | 2014-11-05 21:16:41 +0000 | [diff] [blame] | 43 | #if !defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP |
Paul Sokolovsky | 82a165d | 2014-02-27 18:01:43 +0200 | [diff] [blame] | 44 | #if defined(__i386__) |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 45 | void *regs[6]; |
Paul Sokolovsky | 82a165d | 2014-02-27 18:01:43 +0200 | [diff] [blame] | 46 | #elif defined(__x86_64__) |
Damien George | 4b34c76 | 2014-04-03 23:51:16 +0100 | [diff] [blame] | 47 | #if defined(__CYGWIN__) |
| 48 | void *regs[12]; |
| 49 | #else |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 50 | void *regs[8]; |
Damien George | 4b34c76 | 2014-04-03 23:51:16 +0100 | [diff] [blame] | 51 | #endif |
Paul Sokolovsky | a96cc82 | 2014-06-22 01:14:28 +0300 | [diff] [blame] | 52 | #elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__) |
Damien | 152568b | 2013-10-16 00:46:39 +0100 | [diff] [blame] | 53 | void *regs[10]; |
Damien George | 2399aa0 | 2014-11-27 20:29:33 +0000 | [diff] [blame] | 54 | #elif defined(__xtensa__) |
| 55 | void *regs[10]; |
Paul Sokolovsky | 82a165d | 2014-02-27 18:01:43 +0200 | [diff] [blame] | 56 | #else |
Paul Sokolovsky | 3a83b80 | 2014-04-17 00:16:45 +0300 | [diff] [blame] | 57 | #define MICROPY_NLR_SETJMP (1) |
Paul Sokolovsky | 851c856 | 2014-04-30 04:14:31 +0300 | [diff] [blame] | 58 | //#warning "No native NLR support for this arch, using setjmp implementation" |
Paul Sokolovsky | 3a83b80 | 2014-04-17 00:16:45 +0300 | [diff] [blame] | 59 | #endif |
| 60 | #endif |
| 61 | |
| 62 | #if MICROPY_NLR_SETJMP |
| 63 | jmp_buf jmpbuf; |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 64 | #endif |
| 65 | }; |
| 66 | |
Damien George | 8a23477 | 2015-01-01 21:47:58 +0000 | [diff] [blame] | 67 | extern nlr_buf_t *nlr_top; |
| 68 | |
Paul Sokolovsky | 3a83b80 | 2014-04-17 00:16:45 +0300 | [diff] [blame] | 69 | #if MICROPY_NLR_SETJMP |
Paul Sokolovsky | e908591 | 2014-04-30 05:35:18 +0300 | [diff] [blame] | 70 | NORETURN void nlr_setjmp_jump(void *val); |
Paul Sokolovsky | 3a83b80 | 2014-04-17 00:16:45 +0300 | [diff] [blame] | 71 | // nlr_push() must be defined as a macro, because "The stack context will be |
| 72 | // invalidated if the function which called setjmp() returns." |
Damien George | 8a23477 | 2015-01-01 21:47:58 +0000 | [diff] [blame] | 73 | #define nlr_push(buf) ((buf)->prev = nlr_top, nlr_top = (buf), setjmp((buf)->jmpbuf)) |
| 74 | #define nlr_pop() { nlr_top = nlr_top->prev; } |
Paul Sokolovsky | 3a83b80 | 2014-04-17 00:16:45 +0300 | [diff] [blame] | 75 | #define nlr_jump(val) nlr_setjmp_jump(val) |
| 76 | #else |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 77 | unsigned int nlr_push(nlr_buf_t *); |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 78 | void nlr_pop(void); |
Paul Sokolovsky | e908591 | 2014-04-30 05:35:18 +0300 | [diff] [blame] | 79 | NORETURN void nlr_jump(void *val); |
Paul Sokolovsky | 3a83b80 | 2014-04-17 00:16:45 +0300 | [diff] [blame] | 80 | #endif |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 81 | |
Damien George | 26cf55a | 2014-04-08 14:08:14 +0000 | [diff] [blame] | 82 | // This must be implemented by a port. It's called by nlr_jump |
| 83 | // if no nlr buf has been pushed. It must not return, but rather |
| 84 | // should bail out with a fatal error. |
| 85 | void nlr_jump_fail(void *val); |
| 86 | |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 87 | // use nlr_raise instead of nlr_jump so that debugging is easier |
| 88 | #ifndef DEBUG |
| 89 | #define nlr_raise(val) nlr_jump(val) |
| 90 | #else |
| 91 | #define nlr_raise(val) \ |
| 92 | do { \ |
| 93 | void *_val = val; \ |
| 94 | assert(_val != NULL); \ |
| 95 | assert(mp_obj_is_exception_instance(_val)); \ |
| 96 | nlr_jump(_val); \ |
| 97 | } while (0) |
| 98 | #endif |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 99 | |
| 100 | #endif // __MICROPY_INCLUDED_PY_NLR_H__ |