blob: 8a96af81ce5ca9a765ba4d41ff93b6e8aefebf12 [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
Paul Sokolovskyb1ce37d2014-04-30 04:19:20 +030027#if defined(__i386__) && !MICROPY_NLR_SETJMP
Damien George133b0832014-09-26 13:07:26 +000028
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
Damience89a212013-10-15 22:25:17 +010034
Damien Georgeb4b10fd2015-01-01 23:30:53 +000035// the offset of nlr_top within mp_state_ctx_t
36#define NLR_TOP_OFFSET (2 * 4)
37
Igor Gatisf5c554d2015-11-19 13:53:37 -030038#if defined(_WIN32) || defined(__CYGWIN__)
stijnbf1570c2015-11-20 15:18:42 +010039#define NLR_OS_WINDOWS
Damien Georgeb4b10fd2015-01-01 23:30:53 +000040#define NLR_TOP (_mp_state_ctx + NLR_TOP_OFFSET)
Damien George872a8292015-01-01 22:03:44 +000041#else
Damien Georgeb4b10fd2015-01-01 23:30:53 +000042#define NLR_TOP (mp_state_ctx + NLR_TOP_OFFSET)
Damien George872a8292015-01-01 22:03:44 +000043#endif
44
Damien George27cc0772016-04-22 22:52:33 +000045// offset of nlr_top within mp_state_thread_t structure
46#define NLR_TOP_TH_OFF (0)
47
Damience89a212013-10-15 22:25:17 +010048 .file "nlr.s"
49 .text
50
Damien George133b0832014-09-26 13:07:26 +000051/**************************************/
52// mp_uint_t nlr_push(4(%esp)=nlr_buf_t *nlr)
53
stijnbf1570c2015-11-20 15:18:42 +010054#if defined(NLR_OS_WINDOWS)
Markus Siemens242856c2014-01-28 19:52:04 +010055 .globl _nlr_push
56 .def _nlr_push; .scl 2; .type 32; .endef
57_nlr_push:
58#else
Damience89a212013-10-15 22:25:17 +010059 .globl nlr_push
60 .type nlr_push, @function
61nlr_push:
Markus Siemens19ccc6b2014-01-27 22:53:28 +010062#endif
Damience89a212013-10-15 22:25:17 +010063 mov 4(%esp), %edx # load nlr_buf
64 mov (%esp), %eax # load return %ip
65 mov %eax, 8(%edx) # store %ip into nlr_buf+8
66 mov %ebp, 12(%edx) # store %bp into nlr_buf+12
67 mov %esp, 16(%edx) # store %sp into nlr_buf+16
68 mov %ebx, 20(%edx) # store %bx into nlr_buf+20
69 mov %edi, 24(%edx) # store %di into nlr_buf
70 mov %esi, 28(%edx) # store %si into nlr_buf
Damien George27cc0772016-04-22 22:52:33 +000071
72#if !MICROPY_PY_THREAD
Damien George872a8292015-01-01 22:03:44 +000073 mov NLR_TOP, %eax # load nlr_top
Damience89a212013-10-15 22:25:17 +010074 mov %eax, (%edx) # store it
Damien George872a8292015-01-01 22:03:44 +000075 mov %edx, NLR_TOP # stor new nlr_buf (to make linked list)
Damien George27cc0772016-04-22 22:52:33 +000076#else
77 // to check: stack is aligned to 16-byte boundary before this call
78 call mp_thread_get_state # get mp_state_thread ptr into eax
79 mov 4(%esp), %edx # load nlr_buf argument into edx (edx clobbered by call)
80 mov NLR_TOP_TH_OFF(%eax), %ecx # get thread.nlr_top (last nlr_buf)
81 mov %ecx, (%edx) # store it
82 mov %edx, NLR_TOP_TH_OFF(%eax) # store new nlr_buf (to make linked list)
83#endif
84
Damience89a212013-10-15 22:25:17 +010085 xor %eax, %eax # return 0, normal return
86 ret # return
stijnbf1570c2015-11-20 15:18:42 +010087#if !defined(NLR_OS_WINDOWS)
Damience89a212013-10-15 22:25:17 +010088 .size nlr_push, .-nlr_push
Markus Siemens19ccc6b2014-01-27 22:53:28 +010089#endif
Damience89a212013-10-15 22:25:17 +010090
Damien George133b0832014-09-26 13:07:26 +000091/**************************************/
92// void nlr_pop()
93
stijnbf1570c2015-11-20 15:18:42 +010094#if defined(NLR_OS_WINDOWS)
Markus Siemens242856c2014-01-28 19:52:04 +010095 .globl _nlr_pop
96 .def _nlr_pop; .scl 2; .type 32; .endef
97_nlr_pop:
98#else
Damience89a212013-10-15 22:25:17 +010099 .globl nlr_pop
100 .type nlr_pop, @function
101nlr_pop:
Markus Siemens19ccc6b2014-01-27 22:53:28 +0100102#endif
Damien George27cc0772016-04-22 22:52:33 +0000103
104#if !MICROPY_PY_THREAD
Damien George872a8292015-01-01 22:03:44 +0000105 mov NLR_TOP, %eax # load nlr_top
Damience89a212013-10-15 22:25:17 +0100106 mov (%eax), %eax # load prev nlr_buf
Damien George872a8292015-01-01 22:03:44 +0000107 mov %eax, NLR_TOP # store nlr_top (to unlink list)
Damien George27cc0772016-04-22 22:52:33 +0000108#else
109 call mp_thread_get_state # get mp_state_thread ptr into eax
110 mov NLR_TOP_TH_OFF(%eax), %ecx # get thread.nlr_top (last nlr_buf)
111 mov (%ecx), %ecx # load prev nlr_buf
112 mov %ecx, NLR_TOP_TH_OFF(%eax) # store prev nlr_buf (to unlink list)
113#endif
114
Damience89a212013-10-15 22:25:17 +0100115 ret # return
stijnbf1570c2015-11-20 15:18:42 +0100116#if !defined(NLR_OS_WINDOWS)
Damience89a212013-10-15 22:25:17 +0100117 .size nlr_pop, .-nlr_pop
Markus Siemens19ccc6b2014-01-27 22:53:28 +0100118#endif
Damience89a212013-10-15 22:25:17 +0100119
Damien George133b0832014-09-26 13:07:26 +0000120/**************************************/
121// void nlr_jump(4(%esp)=mp_uint_t val)
122
stijnbf1570c2015-11-20 15:18:42 +0100123#if defined(NLR_OS_WINDOWS)
Markus Siemens242856c2014-01-28 19:52:04 +0100124 .globl _nlr_jump
125 .def _nlr_jump; .scl 2; .type 32; .endef
126_nlr_jump:
127#else
Damience89a212013-10-15 22:25:17 +0100128 .globl nlr_jump
129 .type nlr_jump, @function
130nlr_jump:
Markus Siemens19ccc6b2014-01-27 22:53:28 +0100131#endif
Damien George27cc0772016-04-22 22:52:33 +0000132
133#if !MICROPY_PY_THREAD
Damien George872a8292015-01-01 22:03:44 +0000134 mov NLR_TOP, %edx # load nlr_top
Damien George26cf55a2014-04-08 14:08:14 +0000135 test %edx, %edx # check for nlr_top being NULL
stijnbf1570c2015-11-20 15:18:42 +0100136#if defined(NLR_OS_WINDOWS)
Paul Sokolovsky41809a12014-04-20 22:14:58 +0300137 je _nlr_jump_fail # fail if nlr_top is NULL
138#else
Damien George26cf55a2014-04-08 14:08:14 +0000139 je nlr_jump_fail # fail if nlr_top is NULL
Paul Sokolovsky41809a12014-04-20 22:14:58 +0300140#endif
Damience89a212013-10-15 22:25:17 +0100141 mov 4(%esp), %eax # load return value
142 mov %eax, 4(%edx) # store return value
143 mov (%edx), %eax # load prev nlr_top
Damien George872a8292015-01-01 22:03:44 +0000144 mov %eax, NLR_TOP # store nlr_top (to unlink list)
Damien George27cc0772016-04-22 22:52:33 +0000145#else
146 call mp_thread_get_state # get mp_state_thread ptr into eax
147 mov NLR_TOP_TH_OFF(%eax), %edx # get thread.nlr_top (last nlr_buf)
148 test %edx, %edx # check for nlr_top being NULL
149#if defined(NLR_OS_WINDOWS)
150 je _nlr_jump_fail # fail if nlr_top is NULL
151#else
152 je nlr_jump_fail # fail if nlr_top is NULL
153#endif
154 mov 4(%esp), %ecx # load return value
155 mov %ecx, 4(%edx) # store return value
156 mov (%edx), %ecx # load prev nlr_top
157 mov %ecx, NLR_TOP_TH_OFF(%eax) # store nlr_top (to unlink list)
158#endif
159
Damience89a212013-10-15 22:25:17 +0100160 mov 28(%edx), %esi # load saved %si
161 mov 24(%edx), %edi # load saved %di
162 mov 20(%edx), %ebx # load saved %bx
163 mov 16(%edx), %esp # load saved %sp
164 mov 12(%edx), %ebp # load saved %bp
165 mov 8(%edx), %eax # load saved %ip
166 mov %eax, (%esp) # store saved %ip to stack
167 xor %eax, %eax # clear return register
168 inc %al # increase to make 1, non-local return
169 ret # return
stijnbf1570c2015-11-20 15:18:42 +0100170#if !defined(NLR_OS_WINDOWS)
Damience89a212013-10-15 22:25:17 +0100171 .size nlr_jump, .-nlr_jump
Markus Siemens19ccc6b2014-01-27 22:53:28 +0100172#endif
Damience89a212013-10-15 22:25:17 +0100173
Damien George133b0832014-09-26 13:07:26 +0000174#endif // defined(__i386__) && !MICROPY_NLR_SETJMP