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 | */ |
| 26 | |
Paul Sokolovsky | b1ce37d | 2014-04-30 04:19:20 +0300 | [diff] [blame] | 27 | #if defined(__i386__) && !MICROPY_NLR_SETJMP |
Damien George | 133b083 | 2014-09-26 13:07:26 +0000 | [diff] [blame] | 28 | |
| 29 | // We only need the functions here if we are on x86, and we are not |
| 30 | // using setjmp/longjmp. |
| 31 | // |
| 32 | // For reference, x86 callee save regs are: |
| 33 | // ebx, esi, edi, ebp, esp, eip |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 34 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 35 | // the offset of nlr_top within mp_state_ctx_t |
| 36 | #define NLR_TOP_OFFSET (2 * 4) |
| 37 | |
Igor Gatis | f5c554d | 2015-11-19 13:53:37 -0300 | [diff] [blame] | 38 | #if defined(_WIN32) || defined(__CYGWIN__) |
stijn | bf1570c | 2015-11-20 15:18:42 +0100 | [diff] [blame] | 39 | #define NLR_OS_WINDOWS |
Jan Pochyla | ffb04a5 | 2016-06-02 19:10:39 +0200 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | #if defined(__APPLE__) && defined(__MACH__) |
| 43 | #define NLR_OS_MAC |
| 44 | #endif |
| 45 | |
| 46 | #if defined(NLR_OS_WINDOWS) || defined(NLR_OS_MAC) |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 47 | #define NLR_TOP (_mp_state_ctx + NLR_TOP_OFFSET) |
Jan Pochyla | ffb04a5 | 2016-06-02 19:10:39 +0200 | [diff] [blame] | 48 | #define MP_THREAD_GET_STATE _mp_thread_get_state |
Damien George | 872a829 | 2015-01-01 22:03:44 +0000 | [diff] [blame] | 49 | #else |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 50 | #define NLR_TOP (mp_state_ctx + NLR_TOP_OFFSET) |
Jan Pochyla | ffb04a5 | 2016-06-02 19:10:39 +0200 | [diff] [blame] | 51 | #define MP_THREAD_GET_STATE mp_thread_get_state |
Damien George | 872a829 | 2015-01-01 22:03:44 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 54 | // offset of nlr_top within mp_state_thread_t structure |
| 55 | #define NLR_TOP_TH_OFF (0) |
| 56 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 57 | .file "nlr.s" |
| 58 | .text |
| 59 | |
Damien George | 133b083 | 2014-09-26 13:07:26 +0000 | [diff] [blame] | 60 | /**************************************/ |
| 61 | // mp_uint_t nlr_push(4(%esp)=nlr_buf_t *nlr) |
| 62 | |
stijn | bf1570c | 2015-11-20 15:18:42 +0100 | [diff] [blame] | 63 | #if defined(NLR_OS_WINDOWS) |
Markus Siemens | 242856c | 2014-01-28 19:52:04 +0100 | [diff] [blame] | 64 | .globl _nlr_push |
| 65 | .def _nlr_push; .scl 2; .type 32; .endef |
| 66 | _nlr_push: |
Jan Pochyla | ffb04a5 | 2016-06-02 19:10:39 +0200 | [diff] [blame] | 67 | #elif defined(NLR_OS_MAC) |
| 68 | .globl _nlr_push |
| 69 | _nlr_push: |
Markus Siemens | 242856c | 2014-01-28 19:52:04 +0100 | [diff] [blame] | 70 | #else |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 71 | .globl nlr_push |
| 72 | .type nlr_push, @function |
| 73 | nlr_push: |
Markus Siemens | 19ccc6b | 2014-01-27 22:53:28 +0100 | [diff] [blame] | 74 | #endif |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 75 | mov 4(%esp), %edx # load nlr_buf |
| 76 | mov (%esp), %eax # load return %ip |
| 77 | mov %eax, 8(%edx) # store %ip into nlr_buf+8 |
| 78 | mov %ebp, 12(%edx) # store %bp into nlr_buf+12 |
| 79 | mov %esp, 16(%edx) # store %sp into nlr_buf+16 |
| 80 | mov %ebx, 20(%edx) # store %bx into nlr_buf+20 |
| 81 | mov %edi, 24(%edx) # store %di into nlr_buf |
| 82 | mov %esi, 28(%edx) # store %si into nlr_buf |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 83 | |
| 84 | #if !MICROPY_PY_THREAD |
Damien George | 872a829 | 2015-01-01 22:03:44 +0000 | [diff] [blame] | 85 | mov NLR_TOP, %eax # load nlr_top |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 86 | mov %eax, (%edx) # store it |
Damien George | 872a829 | 2015-01-01 22:03:44 +0000 | [diff] [blame] | 87 | mov %edx, NLR_TOP # stor new nlr_buf (to make linked list) |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 88 | #else |
| 89 | // to check: stack is aligned to 16-byte boundary before this call |
Jan Pochyla | ffb04a5 | 2016-06-02 19:10:39 +0200 | [diff] [blame] | 90 | call MP_THREAD_GET_STATE # get mp_state_thread ptr into eax |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 91 | mov 4(%esp), %edx # load nlr_buf argument into edx (edx clobbered by call) |
| 92 | mov NLR_TOP_TH_OFF(%eax), %ecx # get thread.nlr_top (last nlr_buf) |
| 93 | mov %ecx, (%edx) # store it |
| 94 | mov %edx, NLR_TOP_TH_OFF(%eax) # store new nlr_buf (to make linked list) |
| 95 | #endif |
| 96 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 97 | xor %eax, %eax # return 0, normal return |
| 98 | ret # return |
Jan Pochyla | ffb04a5 | 2016-06-02 19:10:39 +0200 | [diff] [blame] | 99 | #if !defined(NLR_OS_WINDOWS) && !defined(NLR_OS_MAC) |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 100 | .size nlr_push, .-nlr_push |
Markus Siemens | 19ccc6b | 2014-01-27 22:53:28 +0100 | [diff] [blame] | 101 | #endif |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 102 | |
Damien George | 133b083 | 2014-09-26 13:07:26 +0000 | [diff] [blame] | 103 | /**************************************/ |
| 104 | // void nlr_pop() |
| 105 | |
stijn | bf1570c | 2015-11-20 15:18:42 +0100 | [diff] [blame] | 106 | #if defined(NLR_OS_WINDOWS) |
Markus Siemens | 242856c | 2014-01-28 19:52:04 +0100 | [diff] [blame] | 107 | .globl _nlr_pop |
| 108 | .def _nlr_pop; .scl 2; .type 32; .endef |
| 109 | _nlr_pop: |
Jan Pochyla | ffb04a5 | 2016-06-02 19:10:39 +0200 | [diff] [blame] | 110 | #elif defined(NLR_OS_MAC) |
| 111 | .globl _nlr_pop |
| 112 | _nlr_pop: |
Markus Siemens | 242856c | 2014-01-28 19:52:04 +0100 | [diff] [blame] | 113 | #else |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 114 | .globl nlr_pop |
| 115 | .type nlr_pop, @function |
| 116 | nlr_pop: |
Markus Siemens | 19ccc6b | 2014-01-27 22:53:28 +0100 | [diff] [blame] | 117 | #endif |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 118 | |
| 119 | #if !MICROPY_PY_THREAD |
Damien George | 872a829 | 2015-01-01 22:03:44 +0000 | [diff] [blame] | 120 | mov NLR_TOP, %eax # load nlr_top |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 121 | mov (%eax), %eax # load prev nlr_buf |
Damien George | 872a829 | 2015-01-01 22:03:44 +0000 | [diff] [blame] | 122 | mov %eax, NLR_TOP # store nlr_top (to unlink list) |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 123 | #else |
Jan Pochyla | ffb04a5 | 2016-06-02 19:10:39 +0200 | [diff] [blame] | 124 | call MP_THREAD_GET_STATE # get mp_state_thread ptr into eax |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 125 | mov NLR_TOP_TH_OFF(%eax), %ecx # get thread.nlr_top (last nlr_buf) |
| 126 | mov (%ecx), %ecx # load prev nlr_buf |
| 127 | mov %ecx, NLR_TOP_TH_OFF(%eax) # store prev nlr_buf (to unlink list) |
| 128 | #endif |
| 129 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 130 | ret # return |
Jan Pochyla | ffb04a5 | 2016-06-02 19:10:39 +0200 | [diff] [blame] | 131 | #if !defined(NLR_OS_WINDOWS) && !defined(NLR_OS_MAC) |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 132 | .size nlr_pop, .-nlr_pop |
Markus Siemens | 19ccc6b | 2014-01-27 22:53:28 +0100 | [diff] [blame] | 133 | #endif |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 134 | |
Damien George | 133b083 | 2014-09-26 13:07:26 +0000 | [diff] [blame] | 135 | /**************************************/ |
| 136 | // void nlr_jump(4(%esp)=mp_uint_t val) |
| 137 | |
stijn | bf1570c | 2015-11-20 15:18:42 +0100 | [diff] [blame] | 138 | #if defined(NLR_OS_WINDOWS) |
Markus Siemens | 242856c | 2014-01-28 19:52:04 +0100 | [diff] [blame] | 139 | .globl _nlr_jump |
| 140 | .def _nlr_jump; .scl 2; .type 32; .endef |
| 141 | _nlr_jump: |
Jan Pochyla | ffb04a5 | 2016-06-02 19:10:39 +0200 | [diff] [blame] | 142 | #elif defined(NLR_OS_MAC) |
| 143 | .globl _nlr_jump |
| 144 | _nlr_jump: |
Markus Siemens | 242856c | 2014-01-28 19:52:04 +0100 | [diff] [blame] | 145 | #else |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 146 | .globl nlr_jump |
| 147 | .type nlr_jump, @function |
| 148 | nlr_jump: |
Markus Siemens | 19ccc6b | 2014-01-27 22:53:28 +0100 | [diff] [blame] | 149 | #endif |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 150 | |
| 151 | #if !MICROPY_PY_THREAD |
Damien George | 872a829 | 2015-01-01 22:03:44 +0000 | [diff] [blame] | 152 | mov NLR_TOP, %edx # load nlr_top |
Damien George | 26cf55a | 2014-04-08 14:08:14 +0000 | [diff] [blame] | 153 | test %edx, %edx # check for nlr_top being NULL |
Jan Pochyla | ffb04a5 | 2016-06-02 19:10:39 +0200 | [diff] [blame] | 154 | #if defined(NLR_OS_WINDOWS) || defined(NLR_OS_MAC) |
Paul Sokolovsky | 41809a1 | 2014-04-20 22:14:58 +0300 | [diff] [blame] | 155 | je _nlr_jump_fail # fail if nlr_top is NULL |
| 156 | #else |
Damien George | 26cf55a | 2014-04-08 14:08:14 +0000 | [diff] [blame] | 157 | je nlr_jump_fail # fail if nlr_top is NULL |
Paul Sokolovsky | 41809a1 | 2014-04-20 22:14:58 +0300 | [diff] [blame] | 158 | #endif |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 159 | mov 4(%esp), %eax # load return value |
| 160 | mov %eax, 4(%edx) # store return value |
| 161 | mov (%edx), %eax # load prev nlr_top |
Damien George | 872a829 | 2015-01-01 22:03:44 +0000 | [diff] [blame] | 162 | mov %eax, NLR_TOP # store nlr_top (to unlink list) |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 163 | #else |
Jan Pochyla | ffb04a5 | 2016-06-02 19:10:39 +0200 | [diff] [blame] | 164 | call MP_THREAD_GET_STATE # get mp_state_thread ptr into eax |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 165 | mov NLR_TOP_TH_OFF(%eax), %edx # get thread.nlr_top (last nlr_buf) |
| 166 | test %edx, %edx # check for nlr_top being NULL |
Jan Pochyla | ffb04a5 | 2016-06-02 19:10:39 +0200 | [diff] [blame] | 167 | #if defined(NLR_OS_WINDOWS) || defined(NLR_OS_MAC) |
Damien George | 27cc077 | 2016-04-22 22:52:33 +0000 | [diff] [blame] | 168 | je _nlr_jump_fail # fail if nlr_top is NULL |
| 169 | #else |
| 170 | je nlr_jump_fail # fail if nlr_top is NULL |
| 171 | #endif |
| 172 | mov 4(%esp), %ecx # load return value |
| 173 | mov %ecx, 4(%edx) # store return value |
| 174 | mov (%edx), %ecx # load prev nlr_top |
| 175 | mov %ecx, NLR_TOP_TH_OFF(%eax) # store nlr_top (to unlink list) |
| 176 | #endif |
| 177 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 178 | mov 28(%edx), %esi # load saved %si |
| 179 | mov 24(%edx), %edi # load saved %di |
| 180 | mov 20(%edx), %ebx # load saved %bx |
| 181 | mov 16(%edx), %esp # load saved %sp |
| 182 | mov 12(%edx), %ebp # load saved %bp |
| 183 | mov 8(%edx), %eax # load saved %ip |
| 184 | mov %eax, (%esp) # store saved %ip to stack |
| 185 | xor %eax, %eax # clear return register |
| 186 | inc %al # increase to make 1, non-local return |
| 187 | ret # return |
Jan Pochyla | ffb04a5 | 2016-06-02 19:10:39 +0200 | [diff] [blame] | 188 | #if !defined(NLR_OS_WINDOWS) && !defined(NLR_OS_MAC) |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 189 | .size nlr_jump, .-nlr_jump |
Markus Siemens | 19ccc6b | 2014-01-27 22:53:28 +0100 | [diff] [blame] | 190 | #endif |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 191 | |
Damien George | 133b083 | 2014-09-26 13:07:26 +0000 | [diff] [blame] | 192 | #endif // defined(__i386__) && !MICROPY_NLR_SETJMP |
Dave Hylands | aee74a1 | 2017-01-30 20:35:52 -0800 | [diff] [blame] | 193 | #if defined(linux) |
Dave Hylands | aa34c55 | 2017-02-07 14:22:55 -0800 | [diff] [blame] | 194 | .section .note.GNU-stack,"",%progbits |
Dave Hylands | aee74a1 | 2017-01-30 20:35:52 -0800 | [diff] [blame] | 195 | #endif |