Skip to content

Commit 7104260

Browse files
jimmodpgeorge
authored andcommitted
all: Factor gchelper code to one place and use it for unix & ARM ports.
No functionality change is intended with this commit, it just consolidates the separate implementations of GC helper code to the lib/utils/ directory as a general set of helper functions useful for any port. This reduces duplication of code, and makes it easier for future ports or embedders to get the GC implementation correct. Ports should now link against gchelper_native.c and either gchelper_m0.s or gchelper_m3.s (currently only Cortex-M is supported but other architectures can follow), or use the fallback gchelper_generic.c which will work on x86/x64/ARM. The gc_helper_get_sp function from gchelper_m3.s is not really GC related and was only used by cc3200, so it has been moved to that port and renamed to cortex_m3_get_sp.
1 parent 2e3c427 commit 7104260

29 files changed

+307
-319
lines changed

examples/embedding/mpconfigport_minimal.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,9 @@ extern const struct _mp_obj_module_t mp_module_os;
9797
// Do not change anything beyond this line
9898
//////////////////////////////////////////
9999

100-
// Define to 1 to use undertested inefficient GC helper implementation
101-
// (if more efficient arch-specific one is not available).
102-
#ifndef MICROPY_GCREGS_SETJMP
103-
#ifdef __mips__
104-
#define MICROPY_GCREGS_SETJMP (1)
105-
#else
106-
#define MICROPY_GCREGS_SETJMP (0)
107-
#endif
100+
#if !defined(__x86_64__) || !defined(__i386__) || !defined(__thumb2__) || !defined(__thumb__) || !defined(__arm__)
101+
// Fall back to setjmp() implementation for discovery of GC pointers in registers.
102+
#define MICROPY_GCREGS_SETJMP (1)
108103
#endif
109104

110105
// type definitions for the specific machine

lib/utils/gchelper.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,21 @@
2828

2929
#include <stdint.h>
3030

31-
uintptr_t gc_helper_get_sp(void);
32-
uintptr_t gc_helper_get_regs_and_sp(uintptr_t *regs);
31+
#if MICROPY_GCREGS_SETJMP
32+
#include <setjmp.h>
33+
typedef jmp_buf gc_helper_regs_t;
34+
#else
35+
36+
#if defined(__x86_64__)
37+
typedef uintptr_t gc_helper_regs_t[6];
38+
#elif defined(__i386__)
39+
typedef uintptr_t gc_helper_regs_t[4];
40+
#elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
41+
typedef uintptr_t gc_helper_regs_t[10];
42+
#endif
43+
44+
#endif
45+
46+
void gc_helper_collect_regs_and_stack(void);
3347

3448
#endif // MICROPY_INCLUDED_LIB_UTILS_GCHELPER_H

lib/utils/gchelper_generic.c

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* This file is part of the MicroPython 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+
27+
#include <stdio.h>
28+
29+
#include "py/mpstate.h"
30+
#include "py/gc.h"
31+
#include "lib/utils/gchelper.h"
32+
33+
#if MICROPY_ENABLE_GC
34+
35+
// Even if we have specific support for an architecture, it is
36+
// possible to force use of setjmp-based implementation.
37+
#if !MICROPY_GCREGS_SETJMP
38+
39+
// We capture here callee-save registers, i.e. ones which may contain
40+
// interesting values held there by our callers. It doesn't make sense
41+
// to capture caller-saved registers, because they, well, put on the
42+
// stack already by the caller.
43+
#if defined(__x86_64__)
44+
45+
STATIC void gc_helper_get_regs(gc_helper_regs_t arr) {
46+
register long rbx asm ("rbx");
47+
register long rbp asm ("rbp");
48+
register long r12 asm ("r12");
49+
register long r13 asm ("r13");
50+
register long r14 asm ("r14");
51+
register long r15 asm ("r15");
52+
#ifdef __clang__
53+
// TODO:
54+
// This is dirty workaround for Clang. It tries to get around
55+
// uncompliant (wrt to GCC) behavior of handling register variables.
56+
// Application of this patch here is random, and done only to unbreak
57+
// MacOS build. Better, cross-arch ways to deal with Clang issues should
58+
// be found.
59+
asm ("" : "=r" (rbx));
60+
asm ("" : "=r" (rbp));
61+
asm ("" : "=r" (r12));
62+
asm ("" : "=r" (r13));
63+
asm ("" : "=r" (r14));
64+
asm ("" : "=r" (r15));
65+
#endif
66+
arr[0] = rbx;
67+
arr[1] = rbp;
68+
arr[2] = r12;
69+
arr[3] = r13;
70+
arr[4] = r14;
71+
arr[5] = r15;
72+
}
73+
74+
#elif defined(__i386__)
75+
76+
STATIC void gc_helper_get_regs(gc_helper_regs_t arr) {
77+
register long ebx asm ("ebx");
78+
register long esi asm ("esi");
79+
register long edi asm ("edi");
80+
register long ebp asm ("ebp");
81+
#ifdef __clang__
82+
// TODO:
83+
// This is dirty workaround for Clang. It tries to get around
84+
// uncompliant (wrt to GCC) behavior of handling register variables.
85+
// Application of this patch here is random, and done only to unbreak
86+
// MacOS build. Better, cross-arch ways to deal with Clang issues should
87+
// be found.
88+
asm ("" : "=r" (ebx));
89+
asm ("" : "=r" (esi));
90+
asm ("" : "=r" (edi));
91+
asm ("" : "=r" (ebp));
92+
#endif
93+
arr[0] = ebx;
94+
arr[1] = esi;
95+
arr[2] = edi;
96+
arr[3] = ebp;
97+
}
98+
99+
#elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
100+
101+
// Fallback implementation, prefer gchelper_m0.s or gchelper_m3.s
102+
103+
STATIC void gc_helper_get_regs(gc_helper_regs_t arr) {
104+
register long r4 asm ("r4");
105+
register long r5 asm ("r5");
106+
register long r6 asm ("r6");
107+
register long r7 asm ("r7");
108+
register long r8 asm ("r8");
109+
register long r9 asm ("r9");
110+
register long r10 asm ("r10");
111+
register long r11 asm ("r11");
112+
register long r12 asm ("r12");
113+
register long r13 asm ("r13");
114+
arr[0] = r4;
115+
arr[1] = r5;
116+
arr[2] = r6;
117+
arr[3] = r7;
118+
arr[4] = r8;
119+
arr[5] = r9;
120+
arr[6] = r10;
121+
arr[7] = r11;
122+
arr[8] = r12;
123+
arr[9] = r13;
124+
}
125+
126+
#else
127+
128+
#error "Architecture not supported for gc_helper_get_regs. Set MICROPY_GCREGS_SETJMP to use the fallback implementation."
129+
130+
#endif
131+
132+
#else // !MICROPY_GCREGS_SETJMP
133+
134+
// Even if we have specific support for an architecture, it is
135+
// possible to force use of setjmp-based implementation.
136+
137+
STATIC void gc_helper_get_regs(gc_helper_regs_t arr) {
138+
setjmp(arr);
139+
}
140+
141+
#endif // MICROPY_GCREGS_SETJMP
142+
143+
// Explicitly mark this as noinline to make sure the regs variable
144+
// is effectively at the top of the stack: otherwise, in builds where
145+
// LTO is enabled and a lot of inlining takes place we risk a stack
146+
// layout where regs is lower on the stack than pointers which have
147+
// just been allocated but not yet marked, and get incorrectly sweeped.
148+
MP_NOINLINE void gc_helper_collect_regs_and_stack(void) {
149+
gc_helper_regs_t regs;
150+
gc_helper_get_regs(regs);
151+
// GC stack (and regs because we captured them)
152+
void **regs_ptr = (void **)(void *)&regs;
153+
gc_collect_root(regs_ptr, ((uintptr_t)MP_STATE_THREAD(stack_top) - (uintptr_t)&regs) / sizeof(uintptr_t));
154+
}
155+
156+
#endif // MICROPY_ENABLE_GC

lib/utils/gchelper_m3.s

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,6 @@
3131
.section .text
3232
.align 2
3333

34-
.global gc_helper_get_sp
35-
.type gc_helper_get_sp, %function
36-
37-
@ uint gc_helper_get_sp(void)
38-
gc_helper_get_sp:
39-
@ return the sp
40-
mov r0, sp
41-
bx lr
42-
43-
.size gc_helper_get_sp, .-gc_helper_get_sp
44-
45-
4634
.global gc_helper_get_regs_and_sp
4735
.type gc_helper_get_regs_and_sp, %function
4836

lib/utils/gchelper_native.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* This file is part of the MicroPython 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+
27+
#include <stdio.h>
28+
29+
#include "py/mpstate.h"
30+
#include "py/gc.h"
31+
#include "lib/utils/gchelper.h"
32+
33+
#if MICROPY_ENABLE_GC
34+
35+
// provided by gchelper_*.s
36+
uintptr_t gc_helper_get_regs_and_sp(uintptr_t *regs);
37+
38+
MP_NOINLINE void gc_helper_collect_regs_and_stack(void) {
39+
// get the registers and the sp
40+
gc_helper_regs_t regs;
41+
uintptr_t sp = gc_helper_get_regs_and_sp(regs);
42+
43+
// trace the stack, including the registers (since they live on the stack in this function)
44+
gc_collect_root((void **)sp, ((uint32_t)MP_STATE_THREAD(stack_top) - sp) / sizeof(uint32_t));
45+
}
46+
47+
#endif

mpy-cross/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ LDFLAGS = $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA)
4848
SRC_C = \
4949
main.c \
5050
gccollect.c \
51+
lib/utils/gchelper_generic.c \
5152

5253
# Add fmode when compiling with mingw gcc
5354
COMPILER_TARGET := $(shell $(CC) -dumpmachine)

mpy-cross/gccollect.c

Lines changed: 3 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -29,120 +29,13 @@
2929
#include "py/mpstate.h"
3030
#include "py/gc.h"
3131

32-
#if MICROPY_ENABLE_GC
33-
34-
// Even if we have specific support for an architecture, it is
35-
// possible to force use of setjmp-based implementation.
36-
#if !MICROPY_GCREGS_SETJMP
37-
38-
// We capture here callee-save registers, i.e. ones which may contain
39-
// interesting values held there by our callers. It doesn't make sense
40-
// to capture caller-saved registers, because they, well, put on the
41-
// stack already by the caller.
42-
#if defined(__x86_64__)
43-
typedef mp_uint_t regs_t[6];
44-
45-
STATIC void gc_helper_get_regs(regs_t arr) {
46-
register long rbx asm ("rbx");
47-
register long rbp asm ("rbp");
48-
register long r12 asm ("r12");
49-
register long r13 asm ("r13");
50-
register long r14 asm ("r14");
51-
register long r15 asm ("r15");
52-
#ifdef __clang__
53-
// TODO:
54-
// This is dirty workaround for Clang. It tries to get around
55-
// uncompliant (wrt to GCC) behavior of handling register variables.
56-
// Application of this patch here is random, and done only to unbreak
57-
// MacOS build. Better, cross-arch ways to deal with Clang issues should
58-
// be found.
59-
asm ("" : "=r" (rbx));
60-
asm ("" : "=r" (rbp));
61-
asm ("" : "=r" (r12));
62-
asm ("" : "=r" (r13));
63-
asm ("" : "=r" (r14));
64-
asm ("" : "=r" (r15));
65-
#endif
66-
arr[0] = rbx;
67-
arr[1] = rbp;
68-
arr[2] = r12;
69-
arr[3] = r13;
70-
arr[4] = r14;
71-
arr[5] = r15;
72-
}
73-
74-
#elif defined(__i386__)
75-
76-
typedef mp_uint_t regs_t[4];
77-
78-
STATIC void gc_helper_get_regs(regs_t arr) {
79-
register long ebx asm ("ebx");
80-
register long esi asm ("esi");
81-
register long edi asm ("edi");
82-
register long ebp asm ("ebp");
83-
arr[0] = ebx;
84-
arr[1] = esi;
85-
arr[2] = edi;
86-
arr[3] = ebp;
87-
}
88-
89-
#elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
90-
91-
typedef mp_uint_t regs_t[10];
32+
#include "lib/utils/gchelper.h"
9233

93-
STATIC void gc_helper_get_regs(regs_t arr) {
94-
register long r4 asm ("r4");
95-
register long r5 asm ("r5");
96-
register long r6 asm ("r6");
97-
register long r7 asm ("r7");
98-
register long r8 asm ("r8");
99-
register long r9 asm ("r9");
100-
register long r10 asm ("r10");
101-
register long r11 asm ("r11");
102-
register long r12 asm ("r12");
103-
register long r13 asm ("r13");
104-
arr[0] = r4;
105-
arr[1] = r5;
106-
arr[2] = r6;
107-
arr[3] = r7;
108-
arr[4] = r8;
109-
arr[5] = r9;
110-
arr[6] = r10;
111-
arr[7] = r11;
112-
arr[8] = r12;
113-
arr[9] = r13;
114-
}
115-
116-
#else
117-
118-
// If we don't have architecture-specific optimized support,
119-
// just fall back to setjmp-based implementation.
120-
#undef MICROPY_GCREGS_SETJMP
121-
#define MICROPY_GCREGS_SETJMP (1)
122-
123-
#endif // Arch-specific selection
124-
#endif // !MICROPY_GCREGS_SETJMP
125-
126-
// If MICROPY_GCREGS_SETJMP was requested explicitly, or if
127-
// we enabled it as a fallback above.
128-
#if MICROPY_GCREGS_SETJMP
129-
#include <setjmp.h>
130-
131-
typedef jmp_buf regs_t;
132-
133-
STATIC void gc_helper_get_regs(regs_t arr) {
134-
setjmp(arr);
135-
}
136-
137-
#endif // MICROPY_GCREGS_SETJMP
34+
#if MICROPY_ENABLE_GC
13835

13936
void gc_collect(void) {
14037
gc_collect_start();
141-
regs_t regs;
142-
gc_helper_get_regs(regs);
143-
// GC stack (and regs because we captured them)
144-
void **regs_ptr = (void **)(void *)&regs;
145-
gc_collect_root(regs_ptr, ((mp_uint_t)MP_STATE_THREAD(stack_top) - (mp_uint_t)&regs) / sizeof(mp_uint_t));
38+
gc_helper_collect_regs_and_stack();
14639
gc_collect_end();
14740
}
14841

0 commit comments

Comments
 (0)