-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
windows: Support EMIT_X64/X86 with msvc toolchain #4699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2014 Fabian Vogt | ||
* | ||
* 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. | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include <windows.h> | ||
|
||
#include "py/mpstate.h" | ||
#include "py/gc.h" | ||
|
||
#if MICROPY_EMIT_NATIVE || (MICROPY_PY_FFI && MICROPY_FORCE_PLAT_ALLOC_EXEC) | ||
|
||
// The memory allocated here is not on the GC heap (and it may contain pointers | ||
// that need to be GC'd) so we must somehow trace this memory. We do it by | ||
// keeping a linked list of all mmap'd regions, and tracing them explicitly. | ||
|
||
typedef struct _mmap_region_t { | ||
void *ptr; | ||
size_t len; | ||
struct _mmap_region_t *next; | ||
} mmap_region_t; | ||
|
||
void mp_win_alloc_exec(size_t min_size, void **ptr, size_t *size) { | ||
// size needs to be a multiple of the page size | ||
*size = (min_size + 0xfff) & (~0xfff); | ||
*ptr = VirtualAlloc(NULL, *size, MEM_RESERVE|MEM_COMMIT, PAGE_EXECUTE_READWRITE); | ||
|
||
// add new link to the list of mmap'd regions | ||
mmap_region_t *rg = m_new_obj(mmap_region_t); | ||
rg->ptr = *ptr; | ||
rg->len = min_size; | ||
rg->next = MP_STATE_VM(mmap_region_head); | ||
MP_STATE_VM(mmap_region_head) = rg; | ||
} | ||
|
||
void mp_win_free_exec(void *ptr, size_t size) { | ||
VirtualFree(ptr, 0, MEM_RELEASE); | ||
|
||
// unlink the mmap'd region from the list | ||
for (mmap_region_t **rg = (mmap_region_t**)&MP_STATE_VM(mmap_region_head); *rg != NULL; *rg = (*rg)->next) { | ||
if ((*rg)->ptr == ptr) { | ||
mmap_region_t *next = (*rg)->next; | ||
m_del_obj(mmap_region_t, *rg); | ||
*rg = next; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
void mp_win_mark_exec(void) { | ||
for (mmap_region_t *rg = MP_STATE_VM(mmap_region_head); rg != NULL; rg = rg->next) { | ||
gc_collect_root(rg->ptr, rg->len / sizeof(mp_uint_t)); | ||
} | ||
} | ||
|
||
#if MICROPY_FORCE_PLAT_ALLOC_EXEC | ||
// Provide implementation of libffi ffi_closure_* functions in terms | ||
// of the functions above. On a normal Linux system, this save a lot | ||
// of code size. | ||
void *ffi_closure_alloc(size_t size, void **code); | ||
void ffi_closure_free(void *ptr); | ||
|
||
void *ffi_closure_alloc(size_t size, void **code) { | ||
size_t dummy; | ||
mp_win_alloc_exec(size, code, &dummy); | ||
return *code; | ||
} | ||
|
||
void ffi_closure_free(void *ptr) { | ||
(void)ptr; | ||
// TODO | ||
} | ||
#endif | ||
|
||
#endif // MICROPY_EMIT_NATIVE || (MICROPY_PY_FFI && MICROPY_FORCE_PLAT_ALLOC_EXEC) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same remark as the other file: I'm not sure there even is a functional difference with unix/gccollect.c? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's windows api used. I'm not sure it's ok to put them in unix folder. So I made a copy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's only a slight change, or a function which has the same signature in POSIX but just a different name, it's ok to do this I think. Else the way this has be fixed until now is to define our own implementation of the posix function (for example like is done for realpath/gettimeofday/opendir/...). |
||
* This file is part of the MicroPython 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. | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
#include "py/mpstate.h" | ||
#include "py/gc.h" | ||
|
||
#if MICROPY_ENABLE_GC | ||
|
||
// If MICROPY_GCREGS_SETJMP was requested explicitly, or if | ||
// we enabled it as a fallback above. | ||
#if MICROPY_GCREGS_SETJMP | ||
#include <setjmp.h> | ||
|
||
typedef jmp_buf regs_t; | ||
|
||
STATIC void gc_helper_get_regs(regs_t arr) { | ||
setjmp(arr); | ||
} | ||
|
||
#endif // MICROPY_GCREGS_SETJMP | ||
|
||
// this function is used by mpthreadport.c | ||
void gc_collect_regs_and_stack(void); | ||
|
||
void gc_collect_regs_and_stack(void) { | ||
regs_t regs; | ||
gc_helper_get_regs(regs); | ||
// GC stack (and regs because we captured them) | ||
void **regs_ptr = (void**)(void*)®s; | ||
gc_collect_root(regs_ptr, ((uintptr_t)MP_STATE_THREAD(stack_top) - (uintptr_t)®s) / sizeof(uintptr_t)); | ||
} | ||
|
||
void gc_collect(void) { | ||
//gc_dump_info(); | ||
|
||
gc_collect_start(); | ||
gc_collect_regs_and_stack(); | ||
#if MICROPY_PY_THREAD | ||
mp_thread_gc_others(); | ||
#endif | ||
#if MICROPY_EMIT_NATIVE | ||
mp_win_mark_exec(); | ||
#endif | ||
gc_collect_end(); | ||
|
||
//printf("-----\n"); | ||
//gc_dump_info(); | ||
} | ||
|
||
#endif //MICROPY_ENABLE_GC |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
; | ||
; This file is part of the MicroPython project, http://micropython.org/ | ||
; | ||
; The MIT License (MIT) | ||
; | ||
; Copyright (c) 2019 Jian Wang | ||
; | ||
; 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. | ||
; | ||
|
||
EXTERN nlr_push_tail:PROC | ||
|
||
.code | ||
|
||
nlr_push PROC | ||
pop rdx ; load rip as well as fix rsp | ||
mov [rcx + 10h], rbx | ||
mov [rcx + 18h], rsp | ||
mov [rcx + 20h], rbp | ||
mov [rcx + 28h], rdi | ||
mov [rcx + 30h], rsi | ||
mov [rcx + 38h], r12 | ||
mov [rcx + 40h], r13 | ||
mov [rcx + 48h], r14 | ||
mov [rcx + 50h], r15 | ||
mov [rcx + 58h], rdx ; rip | ||
|
||
push rdx ;nlr_push_tail needs the return address | ||
jwang36 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
jmp nlr_push_tail ;do the rest in C | ||
nlr_push ENDP | ||
|
||
asm_nlr_jump PROC | ||
mov rbx, [rcx + 10h] | ||
mov rsp, [rcx + 18h] | ||
mov rbp, [rcx + 20h] | ||
mov rdi, [rcx + 28h] | ||
mov rsi, [rcx + 30h] | ||
mov r12, [rcx + 38h] | ||
mov r13, [rcx + 40h] | ||
mov r14, [rcx + 48h] | ||
mov r15, [rcx + 50h] | ||
|
||
mov rax, rdx ; set return value | ||
jmp qword ptr [rcx + 58h] ; rip | ||
asm_nlr_jump ENDP | ||
|
||
END | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
; | ||
; This file is part of the MicroPython project, http://micropython.org/ | ||
; | ||
; The MIT License (MIT) | ||
; | ||
; Copyright (c) 2019 Jian Wang | ||
; | ||
; 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. | ||
; | ||
|
||
.model flat | ||
.code | ||
|
||
EXTERN _nlr_push_tail:PROC | ||
|
||
_nlr_push PROC | ||
pop ecx ; ecx <- return address | ||
mov edx, [esp] ; edx <- nlr | ||
mov [edx + 08h], ecx ; eip value to restore in nlr_jump | ||
mov [edx + 0Ch], ebp | ||
mov [edx + 10h], esp | ||
mov [edx + 14h], ebx | ||
mov [edx + 18h], edi | ||
mov [edx + 1Ch], esi | ||
|
||
push ecx ; make sure nlr_push_tail return back to nlr_push's caller() | ||
jmp _nlr_push_tail ; do the rest in C | ||
_nlr_push ENDP | ||
|
||
_asm_nlr_jump PROC | ||
pop eax ; skip return address | ||
pop edx ; edx <- nlr (top) | ||
pop eax ; eax <- val | ||
mov esi, [edx + 1Ch] | ||
mov edi, [edx + 18h] | ||
mov ebx, [edx + 14h] | ||
mov esp, [edx + 10h] | ||
mov ebp, [edx + 0Ch] | ||
jmp dword ptr [edx + 08h] ; restore "eip" | ||
_asm_nlr_jump ENDP | ||
|
||
END |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a copy of unix/alloc.c with like 3 lines changed. In such case I think it's better to modify unix/alloc.c instead to make it work with windows builds.