2014-05-03 23:27:38 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-04-30 02:19:20 +01:00
|
|
|
#if defined(__x86_64__) && !MICROPY_NLR_SETJMP
|
2014-09-26 14:07:26 +01:00
|
|
|
|
|
|
|
// We only need the functions here if we are on x86-64, and we are not
|
|
|
|
// using setjmp/longjmp.
|
|
|
|
//
|
|
|
|
// For reference, x86-64 callee save regs are:
|
|
|
|
// rbx, rbp, rsp, r12, r13, r14, r15
|
2013-10-15 22:25:17 +01:00
|
|
|
|
2015-01-01 23:30:53 +00:00
|
|
|
// the offset of nlr_top within mp_state_ctx_t
|
|
|
|
#define NLR_TOP_OFFSET (2 * 8)
|
|
|
|
|
2015-01-08 16:24:44 +00:00
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
|
|
|
#define NLR_TOP (_mp_state_ctx + NLR_TOP_OFFSET)
|
|
|
|
#else
|
2015-01-01 23:30:53 +00:00
|
|
|
#define NLR_TOP (mp_state_ctx + NLR_TOP_OFFSET)
|
2015-11-20 14:18:42 +00:00
|
|
|
#endif
|
|
|
|
|
2016-04-22 23:52:33 +01:00
|
|
|
// offset of nlr_top within mp_state_thread_t structure
|
|
|
|
#define NLR_TOP_TH_OFF (0)
|
|
|
|
|
2015-11-20 14:18:42 +00:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
#define NLR_OS_WINDOWS
|
2015-01-08 16:24:44 +00:00
|
|
|
#endif
|
2015-01-01 23:30:53 +00:00
|
|
|
|
2013-10-15 22:25:17 +01:00
|
|
|
.file "nlr.s"
|
|
|
|
.text
|
|
|
|
|
2015-11-20 14:18:42 +00:00
|
|
|
#if !defined(NLR_OS_WINDOWS)
|
2014-04-03 23:51:16 +01:00
|
|
|
|
2014-09-26 14:07:26 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
//
|
|
|
|
// Functions for *nix and OSX.
|
|
|
|
// OSX needs _ prefix for binding to C, and doesn't support some directives.
|
|
|
|
//
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
/**************************************/
|
|
|
|
// mp_uint_t nlr_push(rdi=nlr_buf_t *nlr)
|
2014-05-11 20:28:26 +01:00
|
|
|
|
2014-03-21 09:05:39 +00:00
|
|
|
#if !(defined(__APPLE__) && defined(__MACH__))
|
2013-10-15 22:25:17 +01:00
|
|
|
.globl nlr_push
|
|
|
|
.type nlr_push, @function
|
|
|
|
nlr_push:
|
2014-01-04 19:15:04 +00:00
|
|
|
#else
|
|
|
|
.globl _nlr_push
|
|
|
|
_nlr_push:
|
|
|
|
#endif
|
2013-10-15 22:25:17 +01:00
|
|
|
movq (%rsp), %rax # load return %rip
|
|
|
|
movq %rax, 16(%rdi) # store %rip into nlr_buf
|
|
|
|
movq %rbp, 24(%rdi) # store %rbp into nlr_buf
|
|
|
|
movq %rsp, 32(%rdi) # store %rsp into nlr_buf
|
|
|
|
movq %rbx, 40(%rdi) # store %rbx into nlr_buf
|
|
|
|
movq %r12, 48(%rdi) # store %r12 into nlr_buf
|
|
|
|
movq %r13, 56(%rdi) # store %r13 into nlr_buf
|
|
|
|
movq %r14, 64(%rdi) # store %r14 into nlr_buf
|
|
|
|
movq %r15, 72(%rdi) # store %r15 into nlr_buf
|
2016-04-22 23:52:33 +01:00
|
|
|
|
|
|
|
#if !MICROPY_PY_THREAD
|
2015-01-01 23:30:53 +00:00
|
|
|
movq NLR_TOP(%rip), %rax # get last nlr_buf
|
2013-10-15 22:25:17 +01:00
|
|
|
movq %rax, (%rdi) # store it
|
2015-01-01 23:30:53 +00:00
|
|
|
movq %rdi, NLR_TOP(%rip) # stor new nlr_buf (to make linked list)
|
2016-04-22 23:52:33 +01:00
|
|
|
#else
|
|
|
|
movq %rdi, %rbp # since we make a call, must save rdi in rbp
|
|
|
|
callq mp_thread_get_state # get mp_state_thread ptr into rax
|
|
|
|
movq NLR_TOP_TH_OFF(%rax), %rsi # get thread.nlr_top (last nlr_buf)
|
|
|
|
movq %rsi, (%rbp) # store it
|
|
|
|
movq %rbp, NLR_TOP_TH_OFF(%rax) # store new nlr_buf (to make linked list)
|
|
|
|
movq 24(%rbp), %rbp # restore rbp
|
|
|
|
#endif
|
|
|
|
|
2013-10-15 22:25:17 +01:00
|
|
|
xorq %rax, %rax # return 0, normal return
|
|
|
|
ret # return
|
2014-03-21 09:05:39 +00:00
|
|
|
#if !(defined(__APPLE__) && defined(__MACH__))
|
2014-01-04 19:19:19 +00:00
|
|
|
.size nlr_push, .-nlr_push
|
|
|
|
#endif
|
2013-10-15 22:25:17 +01:00
|
|
|
|
2014-09-26 14:07:26 +01:00
|
|
|
/**************************************/
|
|
|
|
// void nlr_pop()
|
|
|
|
|
2014-03-21 09:05:39 +00:00
|
|
|
#if !(defined(__APPLE__) && defined(__MACH__))
|
2013-10-15 22:25:17 +01:00
|
|
|
.globl nlr_pop
|
|
|
|
.type nlr_pop, @function
|
|
|
|
nlr_pop:
|
2014-01-04 19:15:04 +00:00
|
|
|
#else
|
|
|
|
.globl _nlr_pop
|
|
|
|
_nlr_pop:
|
|
|
|
#endif
|
2016-04-22 23:52:33 +01:00
|
|
|
|
|
|
|
#if !MICROPY_PY_THREAD
|
2015-01-01 23:30:53 +00:00
|
|
|
movq NLR_TOP(%rip), %rax # get nlr_top into %rax
|
2013-10-15 22:25:17 +01:00
|
|
|
movq (%rax), %rax # load prev nlr_buf
|
2015-01-01 23:30:53 +00:00
|
|
|
movq %rax, NLR_TOP(%rip) # store prev nlr_buf (to unlink list)
|
2016-04-22 23:52:33 +01:00
|
|
|
#else
|
|
|
|
callq mp_thread_get_state # get mp_state_thread ptr into rax
|
|
|
|
movq NLR_TOP_TH_OFF(%rax), %rdi # get thread.nlr_top (last nlr_buf)
|
|
|
|
movq (%rdi), %rdi # load prev nlr_buf
|
|
|
|
movq %rdi, NLR_TOP_TH_OFF(%rax) # store prev nlr_buf (to unlink list)
|
|
|
|
#endif
|
|
|
|
|
2013-10-15 22:25:17 +01:00
|
|
|
ret # return
|
2014-03-21 09:05:39 +00:00
|
|
|
#if !(defined(__APPLE__) && defined(__MACH__))
|
2014-01-04 19:19:19 +00:00
|
|
|
.size nlr_pop, .-nlr_pop
|
|
|
|
#endif
|
2013-10-15 22:25:17 +01:00
|
|
|
|
2014-09-26 14:07:26 +01:00
|
|
|
/**************************************/
|
|
|
|
// void nlr_jump(rdi=mp_uint_t val)
|
|
|
|
|
2014-03-21 09:05:39 +00:00
|
|
|
#if !(defined(__APPLE__) && defined(__MACH__))
|
2013-10-15 22:25:17 +01:00
|
|
|
.globl nlr_jump
|
|
|
|
.type nlr_jump, @function
|
|
|
|
nlr_jump:
|
2014-01-04 19:15:04 +00:00
|
|
|
#else
|
|
|
|
.globl _nlr_jump
|
|
|
|
_nlr_jump:
|
|
|
|
#endif
|
2016-04-22 23:52:33 +01:00
|
|
|
|
|
|
|
#if !MICROPY_PY_THREAD
|
2013-10-15 22:25:17 +01:00
|
|
|
movq %rdi, %rax # put return value in %rax
|
2015-01-01 23:30:53 +00:00
|
|
|
movq NLR_TOP(%rip), %rdi # get nlr_top into %rdi
|
2014-04-08 15:08:14 +01:00
|
|
|
test %rdi, %rdi # check for nlr_top being NULL
|
|
|
|
je .fail # fail if nlr_top is NULL
|
2013-10-15 22:25:17 +01:00
|
|
|
movq %rax, 8(%rdi) # store return value
|
|
|
|
movq (%rdi), %rax # load prev nlr_buf
|
2015-01-01 23:30:53 +00:00
|
|
|
movq %rax, NLR_TOP(%rip) # store prev nlr_buf (to unlink list)
|
2016-04-22 23:52:33 +01:00
|
|
|
#else
|
|
|
|
movq %rdi, %rbp # put return value in rbp
|
|
|
|
callq mp_thread_get_state # get thread ptr in rax
|
|
|
|
movq %rax, %rsi # put thread ptr in rsi
|
|
|
|
movq %rbp, %rax # put return value to rax (for je .fail)
|
|
|
|
movq NLR_TOP_TH_OFF(%rsi), %rdi # get thread.nlr_top in rdi
|
|
|
|
test %rdi, %rdi # check for nlr_top being NULL
|
|
|
|
je .fail # fail if nlr_top is NULL
|
|
|
|
movq %rax, 8(%rdi) # store return value
|
|
|
|
movq (%rdi), %rax # load prev nlr_buf
|
|
|
|
movq %rax, NLR_TOP_TH_OFF(%rsi) # store prev nlr_buf (to unlink list)
|
|
|
|
#endif
|
|
|
|
|
2013-10-15 22:25:17 +01:00
|
|
|
movq 72(%rdi), %r15 # load saved %r15
|
|
|
|
movq 64(%rdi), %r14 # load saved %r14
|
|
|
|
movq 56(%rdi), %r13 # load saved %r13
|
|
|
|
movq 48(%rdi), %r12 # load saved %r12
|
|
|
|
movq 40(%rdi), %rbx # load saved %rbx
|
|
|
|
movq 32(%rdi), %rsp # load saved %rsp
|
|
|
|
movq 24(%rdi), %rbp # load saved %rbp
|
|
|
|
movq 16(%rdi), %rax # load saved %rip
|
|
|
|
movq %rax, (%rsp) # store saved %rip to stack
|
|
|
|
xorq %rax, %rax # clear return register
|
|
|
|
inc %al # increase to make 1, non-local return
|
|
|
|
ret # return
|
2014-04-08 15:08:14 +01:00
|
|
|
.fail:
|
|
|
|
movq %rax, %rdi # put argument back in first-arg register
|
2014-03-21 09:05:39 +00:00
|
|
|
#if !(defined(__APPLE__) && defined(__MACH__))
|
2014-09-26 14:07:26 +01:00
|
|
|
je nlr_jump_fail # transfer control to nlr_jump_fail
|
2013-10-15 22:25:17 +01:00
|
|
|
.size nlr_jump, .-nlr_jump
|
2014-09-26 14:07:26 +01:00
|
|
|
#else
|
|
|
|
je _nlr_jump_fail # transfer control to nlr_jump_fail
|
2014-01-04 19:15:04 +00:00
|
|
|
#endif
|
2013-10-15 22:25:17 +01:00
|
|
|
|
2015-11-20 14:18:42 +00:00
|
|
|
#else // !defined(NLR_OS_WINDOWS)
|
2014-04-03 23:51:16 +01:00
|
|
|
|
2014-09-26 14:07:26 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
//
|
2015-11-20 14:18:42 +00:00
|
|
|
// Functions for Windows
|
2014-09-26 14:07:26 +01:00
|
|
|
//
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
/**************************************/
|
|
|
|
// mp_uint_t nlr_push(rcx=nlr_buf_t *nlr)
|
|
|
|
|
2014-04-03 23:51:16 +01:00
|
|
|
.globl nlr_push
|
|
|
|
nlr_push:
|
|
|
|
movq (%rsp), %rax # load return %rip
|
|
|
|
movq %rax, 16(%rcx) # store %rip into nlr_buf
|
|
|
|
movq %rbp, 24(%rcx) # store %rbp into nlr_buf
|
|
|
|
movq %rsp, 32(%rcx) # store %rsp into nlr_buf
|
|
|
|
movq %rbx, 40(%rcx) # store %rbx into nlr_buf
|
|
|
|
movq %r12, 48(%rcx) # store %r12 into nlr_buf
|
|
|
|
movq %r13, 56(%rcx) # store %r13 into nlr_buf
|
|
|
|
movq %r14, 64(%rcx) # store %r14 into nlr_buf
|
|
|
|
movq %r15, 72(%rcx) # store %r15 into
|
|
|
|
movq %rdi, 80(%rcx) # store %rdr into
|
|
|
|
movq %rsi, 88(%rcx) # store %rsi into
|
2015-01-01 23:30:53 +00:00
|
|
|
movq NLR_TOP(%rip), %rax # get last nlr_buf
|
2014-04-03 23:51:16 +01:00
|
|
|
movq %rax, (%rcx) # store it
|
2015-01-01 23:30:53 +00:00
|
|
|
movq %rcx, NLR_TOP(%rip) # stor new nlr_buf (to make linked list)
|
2014-04-03 23:51:16 +01:00
|
|
|
xorq %rax, %rax # return 0, normal return
|
|
|
|
ret # return
|
|
|
|
|
2014-09-26 14:07:26 +01:00
|
|
|
/**************************************/
|
|
|
|
// void nlr_pop()
|
|
|
|
|
2014-04-08 15:08:14 +01:00
|
|
|
.globl nlr_pop
|
|
|
|
nlr_pop:
|
2015-01-01 23:30:53 +00:00
|
|
|
movq NLR_TOP(%rip), %rax # get nlr_top into %rax
|
2014-04-08 15:08:14 +01:00
|
|
|
movq (%rax), %rax # load prev nlr_buf
|
2015-01-01 23:30:53 +00:00
|
|
|
movq %rax, NLR_TOP(%rip) # store prev nlr_buf (to unlink list)
|
2014-04-08 15:08:14 +01:00
|
|
|
ret # return
|
2014-04-03 23:51:16 +01:00
|
|
|
|
2014-09-26 14:07:26 +01:00
|
|
|
/**************************************/
|
|
|
|
// void nlr_jump(rcx=mp_uint_t val)
|
|
|
|
|
2014-04-03 23:51:16 +01:00
|
|
|
.globl nlr_jump
|
|
|
|
nlr_jump:
|
|
|
|
movq %rcx, %rax # put return value in %rax
|
2015-01-01 23:30:53 +00:00
|
|
|
movq NLR_TOP(%rip), %rcx # get nlr_top into %rcx
|
2014-04-08 15:08:14 +01:00
|
|
|
test %rcx, %rcx # check for nlr_top being NULL
|
|
|
|
je .fail # fail if nlr_top is NULL
|
2014-04-03 23:51:16 +01:00
|
|
|
movq %rax, 8(%rcx) # store return value
|
|
|
|
movq (%rcx), %rax # load prev nlr_buf
|
2015-01-01 23:30:53 +00:00
|
|
|
movq %rax, NLR_TOP(%rip) # store prev nlr_buf (to unlink list)
|
2014-04-03 23:51:16 +01:00
|
|
|
movq 72(%rcx), %r15 # load saved %r15
|
|
|
|
movq 64(%rcx), %r14 # load saved %r14
|
|
|
|
movq 56(%rcx), %r13 # load saved %r13
|
|
|
|
movq 48(%rcx), %r12 # load saved %r12
|
|
|
|
movq 40(%rcx), %rbx # load saved %rbx
|
|
|
|
movq 32(%rcx), %rsp # load saved %rsp
|
|
|
|
movq 24(%rcx), %rbp # load saved %rbp
|
|
|
|
movq 16(%rcx), %rax # load saved %rip
|
|
|
|
movq 80(%rcx), %rdi # store %rdr into
|
|
|
|
movq 88(%rcx), %rsi # store %rsi into
|
|
|
|
movq %rax, (%rsp) # store saved %rip to stack
|
|
|
|
xorq %rax, %rax # clear return register
|
|
|
|
inc %al # increase to make 1, non-local return
|
|
|
|
ret # return
|
2014-04-08 15:08:14 +01:00
|
|
|
.fail:
|
|
|
|
movq %rax, %rcx # put argument back in first-arg register
|
|
|
|
je nlr_jump_fail # transfer control to nlr_jump_fail
|
2014-04-03 23:51:16 +01:00
|
|
|
|
2015-11-20 14:18:42 +00:00
|
|
|
#endif // !defined(NLR_OS_WINDOWS)
|
2014-04-03 23:51:16 +01:00
|
|
|
|
2014-09-26 14:07:26 +01:00
|
|
|
#endif // defined(__x86_64__) && !MICROPY_NLR_SETJMP
|