blob: 1235f1460948de3d8e61e4a8fc288e8c92604cf5 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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 */
Alexander Steffen299bc622017-06-29 23:14:58 +020026#ifndef MICROPY_INCLUDED_PY_NLR_H
27#define MICROPY_INCLUDED_PY_NLR_H
Damien George04b91472014-05-03 23:27:38 +010028
Damience89a212013-10-15 22:25:17 +010029// non-local return
30// exception handling, basically a stack of setjmp/longjmp buffers
31
32#include <limits.h>
Paul Sokolovsky096e9672017-12-26 18:39:51 +020033#include <setjmp.h>
Emmanuel Blotbf3366a2014-06-19 18:47:38 +020034#include <assert.h>
Damience89a212013-10-15 22:25:17 +010035
Damien George51dfcb42015-01-01 20:27:54 +000036#include "py/mpconfig.h"
37
Paul Sokolovsky096e9672017-12-26 18:39:51 +020038typedef struct _nlr_buf_t nlr_buf_t;
39struct _nlr_buf_t {
40 // the entries here must all be machine word size
41 nlr_buf_t *prev;
42 void *ret_val; // always a concrete object (an exception instance)
43#if !defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP
Paul Sokolovsky82a165d2014-02-27 18:01:43 +020044#if defined(__i386__)
Paul Sokolovsky096e9672017-12-26 18:39:51 +020045 void *regs[6];
Paul Sokolovsky82a165d2014-02-27 18:01:43 +020046#elif defined(__x86_64__)
Paul Sokolovsky096e9672017-12-26 18:39:51 +020047 #if defined(__CYGWIN__)
48 void *regs[12];
49 #else
50 void *regs[8];
51 #endif
Paul Sokolovskya96cc822014-06-22 01:14:28 +030052#elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
Paul Sokolovsky096e9672017-12-26 18:39:51 +020053 void *regs[10];
Damien George2399aa02014-11-27 20:29:33 +000054#elif defined(__xtensa__)
Paul Sokolovsky096e9672017-12-26 18:39:51 +020055 void *regs[10];
Paul Sokolovsky82a165d2014-02-27 18:01:43 +020056#else
Paul Sokolovsky3a83b802014-04-17 00:16:45 +030057 #define MICROPY_NLR_SETJMP (1)
Paul Sokolovsky851c8562014-04-30 04:14:31 +030058 //#warning "No native NLR support for this arch, using setjmp implementation"
Paul Sokolovsky3a83b802014-04-17 00:16:45 +030059#endif
60#endif
61
62#if MICROPY_NLR_SETJMP
Damien George6a3a7422017-12-18 18:57:15 +110063 jmp_buf jmpbuf;
Paul Sokolovsky096e9672017-12-26 18:39:51 +020064#endif
Damien George6a3a7422017-12-18 18:57:15 +110065
Damien George02d830c2017-11-26 23:28:40 +110066 #if MICROPY_ENABLE_PYSTACK
67 void *pystack;
68 #endif
Damience89a212013-10-15 22:25:17 +010069};
70
Paul Sokolovsky096e9672017-12-26 18:39:51 +020071// Helper macros to save/restore the pystack state
72#if MICROPY_ENABLE_PYSTACK
73#define MP_NLR_SAVE_PYSTACK(nlr_buf) (nlr_buf)->pystack = MP_STATE_THREAD(pystack_cur)
74#define MP_NLR_RESTORE_PYSTACK(nlr_buf) MP_STATE_THREAD(pystack_cur) = (nlr_buf)->pystack
Paul Sokolovsky3a83b802014-04-17 00:16:45 +030075#else
Paul Sokolovsky096e9672017-12-26 18:39:51 +020076#define MP_NLR_SAVE_PYSTACK(nlr_buf) (void)nlr_buf
77#define MP_NLR_RESTORE_PYSTACK(nlr_buf) (void)nlr_buf
Damien George6a3a7422017-12-18 18:57:15 +110078#endif
79
Paul Sokolovsky096e9672017-12-26 18:39:51 +020080#if MICROPY_NLR_SETJMP
81#include "py/mpstate.h"
82
83NORETURN void nlr_setjmp_jump(void *val);
84// nlr_push() must be defined as a macro, because "The stack context will be
85// invalidated if the function which called setjmp() returns."
86#define nlr_push(buf) ( \
87 (buf)->prev = MP_STATE_THREAD(nlr_top), \
88 MP_NLR_SAVE_PYSTACK(buf), \
89 MP_STATE_THREAD(nlr_top) = (buf), \
90 setjmp((buf)->jmpbuf))
91#define nlr_pop() { MP_STATE_THREAD(nlr_top) = MP_STATE_THREAD(nlr_top)->prev; }
92#define nlr_jump(val) nlr_setjmp_jump(val)
93#else
94unsigned int nlr_push(nlr_buf_t *);
Damien8b3a7c22013-10-23 20:20:17 +010095void nlr_pop(void);
Paul Sokolovskye9085912014-04-30 05:35:18 +030096NORETURN void nlr_jump(void *val);
Paul Sokolovsky096e9672017-12-26 18:39:51 +020097#endif
Damien Georgeea13f402014-04-05 18:32:08 +010098
Damien George26cf55a2014-04-08 14:08:14 +000099// This must be implemented by a port. It's called by nlr_jump
100// if no nlr buf has been pushed. It must not return, but rather
101// should bail out with a fatal error.
Damien Georgebe3d7f92017-02-16 17:23:06 +1100102NORETURN void nlr_jump_fail(void *val);
Damien George26cf55a2014-04-08 14:08:14 +0000103
Damien Georgeea13f402014-04-05 18:32:08 +0100104// use nlr_raise instead of nlr_jump so that debugging is easier
stijn2f0ce2a2017-04-26 13:17:55 +0200105#ifndef MICROPY_DEBUG_NLR
Damien George999cedb2015-11-27 17:01:44 +0000106#define nlr_raise(val) nlr_jump(MP_OBJ_TO_PTR(val))
Damien Georgeea13f402014-04-05 18:32:08 +0100107#else
Paul Sokolovskye89cc132015-02-15 20:23:52 +0300108#include "mpstate.h"
Damien Georgeea13f402014-04-05 18:32:08 +0100109#define nlr_raise(val) \
110 do { \
Damien George94238d42016-05-30 19:27:26 +0100111 /*printf("nlr_raise: nlr_top=%p\n", MP_STATE_THREAD(nlr_top)); \
Paul Sokolovsky3077fbf2015-02-15 20:28:18 +0300112 fflush(stdout);*/ \
Damien George999cedb2015-11-27 17:01:44 +0000113 void *_val = MP_OBJ_TO_PTR(val); \
Damien Georgeea13f402014-04-05 18:32:08 +0100114 assert(_val != NULL); \
Damien George999cedb2015-11-27 17:01:44 +0000115 assert(mp_obj_is_exception_instance(val)); \
Damien Georgeea13f402014-04-05 18:32:08 +0100116 nlr_jump(_val); \
117 } while (0)
Paul Sokolovskye89cc132015-02-15 20:23:52 +0300118
stijn803264b2015-03-03 11:15:06 +0100119#if !MICROPY_NLR_SETJMP
Paul Sokolovskye89cc132015-02-15 20:23:52 +0300120#define nlr_push(val) \
Damien George94238d42016-05-30 19:27:26 +0100121 assert(MP_STATE_THREAD(nlr_top) != val),nlr_push(val)
Paul Sokolovskye89cc132015-02-15 20:23:52 +0300122
Paul Sokolovsky3077fbf2015-02-15 20:28:18 +0300123/*
124#define nlr_push(val) \
Damien George94238d42016-05-30 19:27:26 +0100125 printf("nlr_push: before: nlr_top=%p, val=%p\n", MP_STATE_THREAD(nlr_top), val),assert(MP_STATE_THREAD(nlr_top) != val),nlr_push(val)
Paul Sokolovsky096e9672017-12-26 18:39:51 +0200126#endif
Paul Sokolovsky3077fbf2015-02-15 20:28:18 +0300127*/
stijn803264b2015-03-03 11:15:06 +0100128#endif
Paul Sokolovsky3077fbf2015-02-15 20:28:18 +0300129
Damien Georgeea13f402014-04-05 18:32:08 +0100130#endif
Damien George51dfcb42015-01-01 20:27:54 +0000131
Alexander Steffen299bc622017-06-29 23:14:58 +0200132#endif // MICROPY_INCLUDED_PY_NLR_H