Skip to content

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions ports/windows/alloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
Copy link
Contributor

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.

* 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)
75 changes: 75 additions & 0 deletions ports/windows/gccollect.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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*)&regs;
gc_collect_root(regs_ptr, ((uintptr_t)MP_STATE_THREAD(stack_top) - (uintptr_t)&regs) / 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
5 changes: 5 additions & 0 deletions ports/windows/micropython.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
Expand Down Expand Up @@ -82,6 +83,9 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile />
<Link />
<Link>
<ImageHasSafeExceptionHandlers>false</ImageHasSafeExceptionHandlers>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile />
Expand All @@ -100,5 +104,6 @@
</Target>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" />
</ImportGroup>
</Project>
25 changes: 21 additions & 4 deletions ports/windows/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

#define MICROPY_ALLOC_PATH_MAX (260) //see minwindef.h for msvc or limits.h for mingw
#define MICROPY_PERSISTENT_CODE_LOAD (1)
#define MICROPY_EMIT_X64 (0)
#define MICROPY_EMIT_THUMB (0)
#define MICROPY_EMIT_INLINE_THUMB (0)
#define MICROPY_COMP_MODULE_CONST (1)
Expand Down Expand Up @@ -119,9 +118,21 @@

extern const struct _mp_print_t mp_stderr_print;

#if defined(__MINGW64__) || defined(__MINGW32__)
#define MICROPY_NLR_SETJMP (1)
#endif

#ifdef _MSC_VER
#define MICROPY_GCREGS_SETJMP (1)
#define MICROPY_USE_INTERNAL_PRINTF (0)
#define MICROPY_NLR_SETJMP (0)

#if defined(_WIN64)
#define MICROPY_EMIT_X64 (1)
#elif defined(_WIN32)
#define MICROPY_EMIT_X86 (1)
#endif

#endif

#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
Expand Down Expand Up @@ -181,10 +192,10 @@ extern const struct _mp_obj_module_t mp_module_time;
{ MP_ROM_QSTR(MP_QSTR_umachine), MP_ROM_PTR(&mp_module_machine) }, \
{ MP_ROM_QSTR(MP_QSTR_uos), MP_ROM_PTR(&mp_module_os) }, \

#if MICROPY_USE_READLINE == 1
#define MICROPY_PORT_ROOT_POINTERS \
char *readline_hist[50];
#endif
char *readline_hist[50]; \
void *mmap_region_head;


#define MP_STATE_PORT MP_STATE_VM

Expand All @@ -197,6 +208,12 @@ extern const struct _mp_obj_module_t mp_module_time;
#include "init.h"
#include "sleep.h"

void mp_win_alloc_exec(size_t min_size, void** ptr, size_t *size);
void mp_win_free_exec(void *ptr, size_t size);
void mp_win_mark_exec(void);
#define MP_PLAT_ALLOC_EXEC(min_size, ptr, size) mp_win_alloc_exec(min_size, ptr, size)
#define MP_PLAT_FREE_EXEC(ptr, size) mp_win_free_exec(ptr, size)

#ifdef __GNUC__
#define MP_NOINLINE __attribute__((noinline))
#endif
Expand Down
64 changes: 64 additions & 0 deletions ports/windows/msvc/asmnlrx64.asm
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
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

58 changes: 58 additions & 0 deletions ports/windows/msvc/asmnlrx86.asm
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
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy