blob: 63fe392d9e5de49d3cddc2cddc8425429cabd2ee [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 Sokolovsky3a83b802014-04-17 00:16:45 +030033#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
Damience89a212013-10-15 22:25:17 +010038typedef 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;
Damien George999cedb2015-11-27 17:01:44 +000042 void *ret_val; // always a concrete object (an exception instance)
Damien George7860c2a2014-11-05 21:16:41 +000043#if !defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP
Paul Sokolovsky82a165d2014-02-27 18:01:43 +020044#if defined(__i386__)
Damience89a212013-10-15 22:25:17 +010045 void *regs[6];
Paul Sokolovsky82a165d2014-02-27 18:01:43 +020046#elif defined(__x86_64__)
Damien George4b34c762014-04-03 23:51:16 +010047 #if defined(__CYGWIN__)
48 void *regs[12];
49 #else
Damience89a212013-10-15 22:25:17 +010050 void *regs[8];
Damien George4b34c762014-04-03 23:51:16 +010051 #endif
Paul Sokolovskya96cc822014-06-22 01:14:28 +030052#elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
Damien152568b2013-10-16 00:46:39 +010053 void *regs[10];
Damien George2399aa02014-11-27 20:29:33 +000054#elif defined(__xtensa__)
55 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
63 jmp_buf jmpbuf;
Damience89a212013-10-15 22:25:17 +010064#endif
65};
66
Paul Sokolovsky3a83b802014-04-17 00:16:45 +030067#if MICROPY_NLR_SETJMP
Damien Georgeb4b10fd2015-01-01 23:30:53 +000068#include "py/mpstate.h"
69
Paul Sokolovskye9085912014-04-30 05:35:18 +030070NORETURN void nlr_setjmp_jump(void *val);
Paul Sokolovsky3a83b802014-04-17 00:16:45 +030071// nlr_push() must be defined as a macro, because "The stack context will be
72// invalidated if the function which called setjmp() returns."
Damien George94238d42016-05-30 19:27:26 +010073#define nlr_push(buf) ((buf)->prev = MP_STATE_THREAD(nlr_top), MP_STATE_THREAD(nlr_top) = (buf), setjmp((buf)->jmpbuf))
74#define nlr_pop() { MP_STATE_THREAD(nlr_top) = MP_STATE_THREAD(nlr_top)->prev; }
Paul Sokolovsky3a83b802014-04-17 00:16:45 +030075#define nlr_jump(val) nlr_setjmp_jump(val)
76#else
Damience89a212013-10-15 22:25:17 +010077unsigned int nlr_push(nlr_buf_t *);
Damien8b3a7c22013-10-23 20:20:17 +010078void nlr_pop(void);
Paul Sokolovskye9085912014-04-30 05:35:18 +030079NORETURN void nlr_jump(void *val);
Paul Sokolovsky3a83b802014-04-17 00:16:45 +030080#endif
Damien Georgeea13f402014-04-05 18:32:08 +010081
Damien George26cf55a2014-04-08 14:08:14 +000082// 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.
Damien Georgebe3d7f92017-02-16 17:23:06 +110085NORETURN void nlr_jump_fail(void *val);
Damien George26cf55a2014-04-08 14:08:14 +000086
Damien Georgeea13f402014-04-05 18:32:08 +010087// use nlr_raise instead of nlr_jump so that debugging is easier
stijn2f0ce2a2017-04-26 13:17:55 +020088#ifndef MICROPY_DEBUG_NLR
Damien George999cedb2015-11-27 17:01:44 +000089#define nlr_raise(val) nlr_jump(MP_OBJ_TO_PTR(val))
Damien Georgeea13f402014-04-05 18:32:08 +010090#else
Paul Sokolovskye89cc132015-02-15 20:23:52 +030091#include "mpstate.h"
Damien Georgeea13f402014-04-05 18:32:08 +010092#define nlr_raise(val) \
93 do { \
Damien George94238d42016-05-30 19:27:26 +010094 /*printf("nlr_raise: nlr_top=%p\n", MP_STATE_THREAD(nlr_top)); \
Paul Sokolovsky3077fbf2015-02-15 20:28:18 +030095 fflush(stdout);*/ \
Damien George999cedb2015-11-27 17:01:44 +000096 void *_val = MP_OBJ_TO_PTR(val); \
Damien Georgeea13f402014-04-05 18:32:08 +010097 assert(_val != NULL); \
Damien George999cedb2015-11-27 17:01:44 +000098 assert(mp_obj_is_exception_instance(val)); \
Damien Georgeea13f402014-04-05 18:32:08 +010099 nlr_jump(_val); \
100 } while (0)
Paul Sokolovskye89cc132015-02-15 20:23:52 +0300101
stijn803264b2015-03-03 11:15:06 +0100102#if !MICROPY_NLR_SETJMP
Paul Sokolovskye89cc132015-02-15 20:23:52 +0300103#define nlr_push(val) \
Damien George94238d42016-05-30 19:27:26 +0100104 assert(MP_STATE_THREAD(nlr_top) != val),nlr_push(val)
Paul Sokolovskye89cc132015-02-15 20:23:52 +0300105
Paul Sokolovsky3077fbf2015-02-15 20:28:18 +0300106/*
107#define nlr_push(val) \
Damien George94238d42016-05-30 19:27:26 +0100108 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 Sokolovsky3077fbf2015-02-15 20:28:18 +0300109#endif
110*/
stijn803264b2015-03-03 11:15:06 +0100111#endif
Paul Sokolovsky3077fbf2015-02-15 20:28:18 +0300112
Damien Georgeea13f402014-04-05 18:32:08 +0100113#endif
Damien George51dfcb42015-01-01 20:27:54 +0000114
Alexander Steffen299bc622017-06-29 23:14:58 +0200115#endif // MICROPY_INCLUDED_PY_NLR_H