blob: 93eaa78b768e6241720368963f884211dee8d74b [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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 */
26
Damience89a212013-10-15 22:25:17 +010027// non-local return
28// exception handling, basically a stack of setjmp/longjmp buffers
29
30#include <limits.h>
Paul Sokolovsky3a83b802014-04-17 00:16:45 +030031#include <setjmp.h>
Emmanuel Blotbf3366a2014-06-19 18:47:38 +020032#include <assert.h>
Damience89a212013-10-15 22:25:17 +010033
Damience89a212013-10-15 22:25:17 +010034typedef struct _nlr_buf_t nlr_buf_t;
35struct _nlr_buf_t {
36 // the entries here must all be machine word size
37 nlr_buf_t *prev;
38 void *ret_val;
Damien George7860c2a2014-11-05 21:16:41 +000039#if !defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP
Paul Sokolovsky82a165d2014-02-27 18:01:43 +020040#if defined(__i386__)
Damience89a212013-10-15 22:25:17 +010041 void *regs[6];
Paul Sokolovsky82a165d2014-02-27 18:01:43 +020042#elif defined(__x86_64__)
Damien George4b34c762014-04-03 23:51:16 +010043 #if defined(__CYGWIN__)
44 void *regs[12];
45 #else
Damience89a212013-10-15 22:25:17 +010046 void *regs[8];
Damien George4b34c762014-04-03 23:51:16 +010047 #endif
Paul Sokolovskya96cc822014-06-22 01:14:28 +030048#elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
Damien152568b2013-10-16 00:46:39 +010049 void *regs[10];
Damien George2399aa02014-11-27 20:29:33 +000050#elif defined(__xtensa__)
51 void *regs[10];
Paul Sokolovsky82a165d2014-02-27 18:01:43 +020052#else
Paul Sokolovsky3a83b802014-04-17 00:16:45 +030053 #define MICROPY_NLR_SETJMP (1)
Paul Sokolovsky851c8562014-04-30 04:14:31 +030054 //#warning "No native NLR support for this arch, using setjmp implementation"
Paul Sokolovsky3a83b802014-04-17 00:16:45 +030055#endif
56#endif
57
58#if MICROPY_NLR_SETJMP
59 jmp_buf jmpbuf;
Damience89a212013-10-15 22:25:17 +010060#endif
61};
62
Paul Sokolovsky3a83b802014-04-17 00:16:45 +030063#if MICROPY_NLR_SETJMP
64extern nlr_buf_t *nlr_setjmp_top;
Paul Sokolovskye9085912014-04-30 05:35:18 +030065NORETURN void nlr_setjmp_jump(void *val);
Paul Sokolovsky3a83b802014-04-17 00:16:45 +030066// nlr_push() must be defined as a macro, because "The stack context will be
67// invalidated if the function which called setjmp() returns."
68#define nlr_push(buf) ((buf)->prev = nlr_setjmp_top, nlr_setjmp_top = (buf), setjmp((buf)->jmpbuf))
69#define nlr_pop() { nlr_setjmp_top = nlr_setjmp_top->prev; }
70#define nlr_jump(val) nlr_setjmp_jump(val)
71#else
Damience89a212013-10-15 22:25:17 +010072unsigned int nlr_push(nlr_buf_t *);
Damien8b3a7c22013-10-23 20:20:17 +010073void nlr_pop(void);
Paul Sokolovskye9085912014-04-30 05:35:18 +030074NORETURN void nlr_jump(void *val);
Paul Sokolovsky3a83b802014-04-17 00:16:45 +030075#endif
Damien Georgeea13f402014-04-05 18:32:08 +010076
Damien George26cf55a2014-04-08 14:08:14 +000077// This must be implemented by a port. It's called by nlr_jump
78// if no nlr buf has been pushed. It must not return, but rather
79// should bail out with a fatal error.
80void nlr_jump_fail(void *val);
81
Damien Georgeea13f402014-04-05 18:32:08 +010082// use nlr_raise instead of nlr_jump so that debugging is easier
83#ifndef DEBUG
84#define nlr_raise(val) nlr_jump(val)
85#else
86#define nlr_raise(val) \
87 do { \
88 void *_val = val; \
89 assert(_val != NULL); \
90 assert(mp_obj_is_exception_instance(_val)); \
91 nlr_jump(_val); \
92 } while (0)
93#endif