Skip to content

Commit 56f76b8

Browse files
committed
mpy-cross: Add new component, a cross compiler for MicroPython bytecode.
This component allows to generate .mpy files (pre compiled bytecode) which can be executed within any MicroPython runtime/VM.
1 parent ea23520 commit 56f76b8

File tree

8 files changed

+632
-0
lines changed

8 files changed

+632
-0
lines changed

mpy-cross/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mpy-cross

mpy-cross/Makefile

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
MICROPY_FORCE_32BIT = 1
2+
3+
include ../py/mkenv.mk
4+
5+
# define main target
6+
PROG = mpy-cross
7+
8+
# qstr definitions (must come before including py.mk)
9+
QSTR_DEFS = qstrdefsport.h
10+
11+
# OS name, for simple autoconfig
12+
UNAME_S := $(shell uname -s)
13+
14+
# include py core make definitions
15+
include ../py/py.mk
16+
17+
INC += -I.
18+
INC += -I..
19+
INC += -I$(BUILD)
20+
21+
# compiler settings
22+
CWARN = -Wall -Werror
23+
CWARN += -Wpointer-arith -Wuninitialized
24+
CFLAGS = $(INC) $(CWARN) -ansi -std=gnu99 $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA)
25+
CFLAGS += -fdata-sections -ffunction-sections -fno-asynchronous-unwind-tables
26+
27+
# Debugging/Optimization
28+
ifdef DEBUG
29+
CFLAGS += -g
30+
COPT = -O0
31+
else
32+
COPT = -Os #-DNDEBUG
33+
endif
34+
35+
# On OSX, 'gcc' is a symlink to clang unless a real gcc is installed.
36+
# The unix port of micropython on OSX must be compiled with clang,
37+
# while cross-compile ports require gcc, so we test here for OSX and
38+
# if necessary override the value of 'CC' set in py/mkenv.mk
39+
ifeq ($(UNAME_S),Darwin)
40+
CC = clang
41+
# Use clang syntax for map file
42+
LDFLAGS_ARCH = -Wl,-map,$@.map
43+
else
44+
# Use gcc syntax for map file
45+
LDFLAGS_ARCH = -Wl,-Map=$@.map,--cref
46+
endif
47+
LDFLAGS = $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA)
48+
LDFLAGS += -Wl,--gc-sections
49+
50+
# source files
51+
SRC_C = \
52+
main.c \
53+
gccollect.c \
54+
55+
OBJ = $(PY_O)
56+
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
57+
58+
include ../py/mkrules.mk

mpy-cross/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
MicroPython cross compiler
2+
==========================
3+
4+
This directory contains the MicroPython cross compiler, which runs under any
5+
Unix-like system and compiles .py scripts into .mpy files.
6+
7+
Build it as usual:
8+
9+
$ make
10+
11+
The compiler is called `mpy-cross`. Invoke it as:
12+
13+
$ ./mpy-cross foo.py
14+
15+
This will create a file foo.mpy which can then be copied to a place accessible
16+
by the target MicroPython runtime (eg onto a pyboard's filesystem), and then
17+
imported like any other Python module using `import foo`.
18+
19+
Different target runtimes may require a different format of the compiled
20+
bytecode, and such options can be passed to the cross compiler. For example,
21+
the unix port of MicroPython requires the following:
22+
23+
$ ./mpy-cross -mcache-lookup-bc foo.py
24+
25+
Run `./mpy-cross -h` to get a full list of options.

mpy-cross/gccollect.c

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
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];
92+
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
138+
139+
void gc_collect(void) {
140+
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_VM(stack_top) - (mp_uint_t)&regs) / sizeof(mp_uint_t));
146+
#if MICROPY_EMIT_NATIVE
147+
mp_unix_mark_exec();
148+
#endif
149+
gc_collect_end();
150+
}
151+
152+
#endif //MICROPY_ENABLE_GC

0 commit comments

Comments
 (0)