Numworks Epsilon  1.4.1
Graphing Calculator Operating System
nlr.h
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython 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 #ifndef MICROPY_INCLUDED_PY_NLR_H
27 #define MICROPY_INCLUDED_PY_NLR_H
28 
29 // non-local return
30 // exception handling, basically a stack of setjmp/longjmp buffers
31 
32 #include <limits.h>
33 #include <setjmp.h>
34 #include <assert.h>
35 
36 #include "py/mpconfig.h"
37 
38 typedef struct _nlr_buf_t nlr_buf_t;
39 struct _nlr_buf_t {
40  // the entries here must all be machine word size
42  void *ret_val; // always a concrete object (an exception instance)
43 #if !defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP
44 #if defined(__i386__)
45  void *regs[6];
46 #elif defined(__x86_64__)
47  #if defined(__CYGWIN__)
48  void *regs[12];
49  #else
50  void *regs[8];
51  #endif
52 #elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
53  void *regs[10];
54 #elif defined(__xtensa__)
55  void *regs[10];
56 #else
57  #define MICROPY_NLR_SETJMP (1)
58  //#warning "No native NLR support for this arch, using setjmp implementation"
59 #endif
60 #endif
61 
62 #if MICROPY_NLR_SETJMP
64 #endif
65 };
66 
67 #if MICROPY_NLR_SETJMP
68 #include "py/mpstate.h"
69 
70 NORETURN void nlr_setjmp_jump(void *val);
71 // nlr_push() must be defined as a macro, because "The stack context will be
72 // invalidated if the function which called setjmp() returns."
73 #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; }
75 #define nlr_jump(val) nlr_setjmp_jump(val)
76 #else
77 unsigned int nlr_push(nlr_buf_t *);
78 void nlr_pop(void);
79 NORETURN void nlr_jump(void *val);
80 #endif
81 
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 NORETURN void nlr_jump_fail(void *val);
86 
87 // use nlr_raise instead of nlr_jump so that debugging is easier
88 #ifndef MICROPY_DEBUG_NLR
89 #define nlr_raise(val) nlr_jump(MP_OBJ_TO_PTR(val))
90 #else
91 #include "mpstate.h"
92 #define nlr_raise(val) \
93  do { \
94  /*printf("nlr_raise: nlr_top=%p\n", MP_STATE_THREAD(nlr_top)); \
95  fflush(stdout);*/ \
96  void *_val = MP_OBJ_TO_PTR(val); \
97  assert(_val != NULL); \
98  assert(mp_obj_is_exception_instance(val)); \
99  nlr_jump(_val); \
100  } while (0)
101 
102 #if !MICROPY_NLR_SETJMP
103 #define nlr_push(val) \
104  assert(MP_STATE_THREAD(nlr_top) != val),nlr_push(val)
105 
106 /*
107 #define nlr_push(val) \
108  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)
109 #endif
110 */
111 #endif
112 
113 #endif
114 
115 #endif // MICROPY_INCLUDED_PY_NLR_H
NORETURN void nlr_setjmp_jump(void *val)
#define nlr_push(buf)
Definition: nlr.h:73
LIBA_BEGIN_DECLS typedef int jmp_buf[31]
Definition: setjmp.h:17
NORETURN void nlr_jump_fail(void *val)
Definition: port.cpp:142
nlr_buf_t * prev
Definition: nlr.h:41
#define NORETURN
Definition: mpconfig.h:1268
#define nlr_pop()
Definition: nlr.h:74
void * ret_val
Definition: nlr.h:42
Definition: nlr.h:39
jmp_buf jmpbuf
Definition: nlr.h:63
#define nlr_jump(val)
Definition: nlr.h:75