Skip to content

Commit 110ba35

Browse files
committed
py: Move native glue code from runtime.c to new file nativeglue.c.
This way, the native glue code is only compiled if native code is enabled (which makes complete sense; thanks to Paul Sokolovsky for the idea). Should fix issue adafruit#834.
1 parent 1ac6faa commit 110ba35

File tree

3 files changed

+137
-88
lines changed

3 files changed

+137
-88
lines changed

py/nativeglue.c

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 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+
#include <string.h>
29+
#include <assert.h>
30+
31+
#include "mpconfig.h"
32+
#include "nlr.h"
33+
#include "misc.h"
34+
#include "qstr.h"
35+
#include "obj.h"
36+
#include "runtime0.h"
37+
#include "runtime.h"
38+
#include "emitglue.h"
39+
40+
#if MICROPY_EMIT_NATIVE
41+
42+
#if 0 // print debugging info
43+
#define DEBUG_printf DEBUG_printf
44+
#else // don't print debugging info
45+
#define DEBUG_printf(...) (void)0
46+
#endif
47+
48+
// convert a Micro Python object to a valid native value based on type
49+
mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type) {
50+
DEBUG_printf("mp_convert_obj_to_native(%p, " UINT_FMT ")\n", obj, type);
51+
switch (type & 3) {
52+
case MP_NATIVE_TYPE_OBJ: return (mp_uint_t)obj;
53+
case MP_NATIVE_TYPE_BOOL:
54+
case MP_NATIVE_TYPE_INT:
55+
case MP_NATIVE_TYPE_UINT: return mp_obj_get_int(obj);
56+
default: assert(0); return 0;
57+
}
58+
}
59+
60+
// convert a native value to a Micro Python object based on type
61+
mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type) {
62+
DEBUG_printf("mp_convert_native_to_obj(" UINT_FMT ", " UINT_FMT ")\n", val, type);
63+
switch (type & 3) {
64+
case MP_NATIVE_TYPE_OBJ: return (mp_obj_t)val;
65+
case MP_NATIVE_TYPE_BOOL: return MP_BOOL(val);
66+
case MP_NATIVE_TYPE_INT: return mp_obj_new_int(val);
67+
case MP_NATIVE_TYPE_UINT: return mp_obj_new_int_from_uint(val);
68+
default: assert(0); return mp_const_none;
69+
}
70+
}
71+
72+
// wrapper that accepts n_args and n_kw in one argument
73+
// (native emitter can only pass at most 3 arguments to a function)
74+
mp_obj_t mp_native_call_function_n_kw(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
75+
return mp_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
76+
}
77+
78+
// wrapper that makes raise obj and raises it
79+
NORETURN void mp_native_raise(mp_obj_t o) {
80+
nlr_raise(mp_make_raise_obj(o));
81+
}
82+
83+
// these must correspond to the respective enum in runtime0.h
84+
void *const mp_fun_table[MP_F_NUMBER_OF] = {
85+
mp_convert_obj_to_native,
86+
mp_convert_native_to_obj,
87+
mp_load_const_int,
88+
mp_load_const_dec,
89+
mp_load_const_str,
90+
mp_load_const_bytes,
91+
mp_load_name,
92+
mp_load_global,
93+
mp_load_build_class,
94+
mp_load_attr,
95+
mp_load_method,
96+
mp_store_name,
97+
mp_store_global,
98+
mp_store_attr,
99+
mp_obj_subscr,
100+
mp_obj_is_true,
101+
mp_unary_op,
102+
mp_binary_op,
103+
mp_obj_new_tuple,
104+
mp_obj_new_list,
105+
mp_obj_list_append,
106+
mp_obj_new_dict,
107+
mp_obj_dict_store,
108+
#if MICROPY_PY_BUILTINS_SET
109+
mp_obj_new_set,
110+
mp_obj_set_store,
111+
#endif
112+
mp_make_function_from_raw_code,
113+
mp_native_call_function_n_kw,
114+
mp_call_method_n_kw,
115+
mp_getiter,
116+
mp_iternext,
117+
nlr_push,
118+
nlr_pop,
119+
mp_native_raise,
120+
mp_import_name,
121+
mp_import_from,
122+
mp_import_all,
123+
#if MICROPY_PY_BUILTINS_SLICE
124+
mp_obj_new_slice,
125+
#endif
126+
mp_unpack_sequence,
127+
mp_unpack_ex,
128+
};
129+
130+
/*
131+
void mp_f_vector(mp_fun_kind_t fun_kind) {
132+
(mp_f_table[fun_kind])();
133+
}
134+
*/
135+
136+
#endif // MICROPY_EMIT_NATIVE

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ PY_O_BASENAME = \
4545
parsenum.o \
4646
emitglue.o \
4747
runtime.o \
48+
nativeglue.o \
4849
stackctrl.o \
4950
argcheck.o \
5051
map.o \

py/runtime.c

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,91 +1156,3 @@ void *m_malloc_fail(int num_bytes) {
11561156
NORETURN void mp_not_implemented(const char *msg) {
11571157
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError, msg));
11581158
}
1159-
1160-
// convert a Micro Python object to a valid native value based on type
1161-
mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type) {
1162-
DEBUG_printf("mp_convert_obj_to_native(%p, " UINT_FMT ")\n", obj, type);
1163-
switch (type & 3) {
1164-
case MP_NATIVE_TYPE_OBJ: return (mp_uint_t)obj;
1165-
case MP_NATIVE_TYPE_BOOL:
1166-
case MP_NATIVE_TYPE_INT:
1167-
case MP_NATIVE_TYPE_UINT: return mp_obj_get_int(obj);
1168-
default: assert(0); return 0;
1169-
}
1170-
}
1171-
1172-
// convert a native value to a Micro Python object based on type
1173-
mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type) {
1174-
DEBUG_printf("mp_convert_native_to_obj(" UINT_FMT ", " UINT_FMT ")\n", val, type);
1175-
switch (type & 3) {
1176-
case MP_NATIVE_TYPE_OBJ: return (mp_obj_t)val;
1177-
case MP_NATIVE_TYPE_BOOL: return MP_BOOL(val);
1178-
case MP_NATIVE_TYPE_INT: return mp_obj_new_int(val);
1179-
case MP_NATIVE_TYPE_UINT: return mp_obj_new_int_from_uint(val);
1180-
default: assert(0); return mp_const_none;
1181-
}
1182-
}
1183-
1184-
// wrapper that accepts n_args and n_kw in one argument
1185-
// (native emitter can only pass at most 3 arguments to a function)
1186-
mp_obj_t mp_native_call_function_n_kw(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
1187-
return mp_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
1188-
}
1189-
1190-
// wrapper that makes raise obj and raises it
1191-
NORETURN void mp_native_raise(mp_obj_t o) {
1192-
nlr_raise(mp_make_raise_obj(o));
1193-
}
1194-
1195-
// these must correspond to the respective enum in runtime0.h
1196-
void *const mp_fun_table[MP_F_NUMBER_OF] = {
1197-
mp_convert_obj_to_native,
1198-
mp_convert_native_to_obj,
1199-
mp_load_const_int,
1200-
mp_load_const_dec,
1201-
mp_load_const_str,
1202-
mp_load_const_bytes,
1203-
mp_load_name,
1204-
mp_load_global,
1205-
mp_load_build_class,
1206-
mp_load_attr,
1207-
mp_load_method,
1208-
mp_store_name,
1209-
mp_store_global,
1210-
mp_store_attr,
1211-
mp_obj_subscr,
1212-
mp_obj_is_true,
1213-
mp_unary_op,
1214-
mp_binary_op,
1215-
mp_obj_new_tuple,
1216-
mp_obj_new_list,
1217-
mp_obj_list_append,
1218-
mp_obj_new_dict,
1219-
mp_obj_dict_store,
1220-
#if MICROPY_PY_BUILTINS_SET
1221-
mp_obj_new_set,
1222-
mp_obj_set_store,
1223-
#endif
1224-
mp_make_function_from_raw_code,
1225-
mp_native_call_function_n_kw,
1226-
mp_call_method_n_kw,
1227-
mp_getiter,
1228-
mp_iternext,
1229-
nlr_push,
1230-
nlr_pop,
1231-
mp_native_raise,
1232-
mp_import_name,
1233-
mp_import_from,
1234-
mp_import_all,
1235-
#if MICROPY_PY_BUILTINS_SLICE
1236-
mp_obj_new_slice,
1237-
#endif
1238-
mp_unpack_sequence,
1239-
mp_unpack_ex,
1240-
};
1241-
1242-
/*
1243-
void mp_f_vector(mp_fun_kind_t fun_kind) {
1244-
(mp_f_table[fun_kind])();
1245-
}
1246-
*/

0 commit comments

Comments
 (0)
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