2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-03-27 23:26:35 +00:00
|
|
|
// This code glues the code emitters to the runtime.
|
|
|
|
|
2015-11-02 21:55:42 +00:00
|
|
|
#include <stdint.h>
|
2014-03-28 18:38:20 +00:00
|
|
|
#include <stdio.h>
|
2014-03-29 11:39:36 +00:00
|
|
|
#include <string.h>
|
2014-03-27 23:26:35 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/emitglue.h"
|
|
|
|
#include "py/runtime0.h"
|
|
|
|
#include "py/bc.h"
|
2019-08-14 15:09:36 +01:00
|
|
|
#include "py/profile.h"
|
2014-03-27 23:26:35 +00:00
|
|
|
|
2017-07-24 17:55:14 +01:00
|
|
|
#if MICROPY_DEBUG_VERBOSE // print debugging info
|
2014-03-27 23:26:35 +00:00
|
|
|
#define DEBUG_PRINT (1)
|
|
|
|
#define WRITE_CODE (1)
|
|
|
|
#define DEBUG_printf DEBUG_printf
|
|
|
|
#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
|
|
|
|
#else // don't print debugging info
|
|
|
|
#define DEBUG_printf(...) (void)0
|
|
|
|
#define DEBUG_OP_printf(...) (void)0
|
|
|
|
#endif
|
|
|
|
|
2015-11-21 14:34:57 +00:00
|
|
|
#if MICROPY_DEBUG_PRINTERS
|
|
|
|
mp_uint_t mp_verbose_flag = 0;
|
|
|
|
#endif
|
|
|
|
|
2014-04-13 11:04:33 +01:00
|
|
|
mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
|
|
|
|
mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1);
|
|
|
|
rc->kind = MP_CODE_RESERVED;
|
2019-08-14 15:09:36 +01:00
|
|
|
#if MICROPY_PY_SYS_SETTRACE
|
|
|
|
rc->line_of_definition = 0;
|
|
|
|
#endif
|
2014-04-13 11:04:33 +01:00
|
|
|
return rc;
|
2014-03-27 23:26:35 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 07:41:17 +00:00
|
|
|
void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code,
|
|
|
|
#if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
|
|
|
|
size_t len,
|
|
|
|
#endif
|
2015-11-02 21:55:42 +00:00
|
|
|
const mp_uint_t *const_table,
|
|
|
|
#if MICROPY_PERSISTENT_CODE_SAVE
|
|
|
|
uint16_t n_obj, uint16_t n_raw_code,
|
|
|
|
#endif
|
|
|
|
mp_uint_t scope_flags) {
|
|
|
|
|
2014-05-10 13:40:46 +01:00
|
|
|
rc->kind = MP_CODE_BYTECODE;
|
2014-04-13 11:04:33 +01:00
|
|
|
rc->scope_flags = scope_flags;
|
2019-02-19 03:15:39 +00:00
|
|
|
rc->fun_data = code;
|
|
|
|
rc->const_table = const_table;
|
2015-11-02 21:55:42 +00:00
|
|
|
#if MICROPY_PERSISTENT_CODE_SAVE
|
2019-02-19 03:15:39 +00:00
|
|
|
rc->fun_data_len = len;
|
|
|
|
rc->n_obj = n_obj;
|
|
|
|
rc->n_raw_code = n_raw_code;
|
2015-11-02 21:55:42 +00:00
|
|
|
#endif
|
2014-03-27 23:26:35 +00:00
|
|
|
|
2019-08-14 15:09:36 +01:00
|
|
|
#if MICROPY_PY_SYS_SETTRACE
|
|
|
|
mp_bytecode_prelude_t *prelude = &rc->prelude;
|
|
|
|
mp_prof_extract_prelude(code, prelude);
|
|
|
|
#endif
|
|
|
|
|
2014-03-27 23:26:35 +00:00
|
|
|
#ifdef DEBUG_PRINT
|
2018-08-02 05:17:24 +01:00
|
|
|
#if !MICROPY_DEBUG_PRINTERS
|
|
|
|
const size_t len = 0;
|
|
|
|
#endif
|
2015-10-22 23:45:37 +01:00
|
|
|
DEBUG_printf("assign byte code: code=%p len=" UINT_FMT " flags=%x\n", code, len, (uint)scope_flags);
|
2014-03-27 23:26:35 +00:00
|
|
|
#endif
|
2014-05-04 22:50:05 +01:00
|
|
|
#if MICROPY_DEBUG_PRINTERS
|
2014-10-26 21:20:22 +00:00
|
|
|
if (mp_verbose_flag >= 2) {
|
2020-09-11 08:22:28 +01:00
|
|
|
mp_bytecode_print(&mp_plat_print, rc, code, len, const_table);
|
2014-05-04 22:50:05 +01:00
|
|
|
}
|
2014-03-27 23:26:35 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-05-06 08:31:11 +01:00
|
|
|
#if MICROPY_EMIT_MACHINE_CODE
|
py: Add support to save native, viper and asm code to .mpy files.
This commit adds support for saving and loading .mpy files that contain
native code (native, viper and inline-asm). A lot of the ground work was
already done for this in the form of removing pointers from generated
native code. The changes here are mainly to link in qstr values to the
native code, and change the format of .mpy files to contain native code
blocks (possibly mixed with bytecode).
A top-level summary:
- @micropython.native, @micropython.viper and @micropython.asm_thumb/
asm_xtensa are now allowed in .py files when compiling to .mpy, and they
work transparently to the user.
- Entire .py files can be compiled to native via mpy-cross -X emit=native
and for the most part the generated .mpy files should work the same as
their bytecode version.
- The .mpy file format is changed to 1) specify in the header if the file
contains native code and if so the architecture (eg x86, ARMV7M, Xtensa);
2) for each function block the kind of code is specified (bytecode,
native, viper, asm).
- When native code is loaded from a .mpy file the native code must be
modified (in place) to link qstr values in, just like bytecode (see
py/persistentcode.c:arch_link_qstr() function).
In addition, this now defines a public, native ABI for dynamically loadable
native code generated by other languages, like C.
2019-02-21 04:18:33 +00:00
|
|
|
void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void *fun_data, mp_uint_t fun_len, const mp_uint_t *const_table,
|
|
|
|
#if MICROPY_PERSISTENT_CODE_SAVE
|
|
|
|
uint16_t prelude_offset,
|
|
|
|
uint16_t n_obj, uint16_t n_raw_code,
|
|
|
|
uint16_t n_qstr, mp_qstr_link_entry_t *qstr_link,
|
|
|
|
#endif
|
|
|
|
mp_uint_t n_pos_args, mp_uint_t scope_flags, mp_uint_t type_sig) {
|
|
|
|
|
2014-05-10 13:40:46 +01:00
|
|
|
assert(kind == MP_CODE_NATIVE_PY || kind == MP_CODE_NATIVE_VIPER || kind == MP_CODE_NATIVE_ASM);
|
py: Add support to save native, viper and asm code to .mpy files.
This commit adds support for saving and loading .mpy files that contain
native code (native, viper and inline-asm). A lot of the ground work was
already done for this in the form of removing pointers from generated
native code. The changes here are mainly to link in qstr values to the
native code, and change the format of .mpy files to contain native code
blocks (possibly mixed with bytecode).
A top-level summary:
- @micropython.native, @micropython.viper and @micropython.asm_thumb/
asm_xtensa are now allowed in .py files when compiling to .mpy, and they
work transparently to the user.
- Entire .py files can be compiled to native via mpy-cross -X emit=native
and for the most part the generated .mpy files should work the same as
their bytecode version.
- The .mpy file format is changed to 1) specify in the header if the file
contains native code and if so the architecture (eg x86, ARMV7M, Xtensa);
2) for each function block the kind of code is specified (bytecode,
native, viper, asm).
- When native code is loaded from a .mpy file the native code must be
modified (in place) to link qstr values in, just like bytecode (see
py/persistentcode.c:arch_link_qstr() function).
In addition, this now defines a public, native ABI for dynamically loadable
native code generated by other languages, like C.
2019-02-21 04:18:33 +00:00
|
|
|
|
2021-06-03 16:17:09 +01:00
|
|
|
// Some architectures require flushing/invalidation of the I/D caches,
|
|
|
|
// so that the generated native code which was created in data RAM will
|
|
|
|
// be available for execution from instruction RAM.
|
|
|
|
#if MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB
|
|
|
|
#if __ICACHE_PRESENT == 1
|
|
|
|
// Flush D-cache, so the code emitted is stored in RAM.
|
|
|
|
MP_HAL_CLEAN_DCACHE(fun_data, fun_len);
|
|
|
|
// Invalidate I-cache, so the newly-created code is reloaded from RAM.
|
|
|
|
SCB_InvalidateICache();
|
|
|
|
#endif
|
|
|
|
#elif MICROPY_EMIT_ARM
|
|
|
|
#if (defined(__linux__) && defined(__GNUC__)) || __ARM_ARCH == 7
|
|
|
|
__builtin___clear_cache(fun_data, (uint8_t *)fun_data + fun_len);
|
|
|
|
#elif defined(__arm__)
|
|
|
|
// Flush I-cache and D-cache.
|
|
|
|
asm volatile (
|
|
|
|
"0:"
|
|
|
|
"mrc p15, 0, r15, c7, c10, 3\n" // test and clean D-cache
|
|
|
|
"bne 0b\n"
|
|
|
|
"mov r0, #0\n"
|
|
|
|
"mcr p15, 0, r0, c7, c7, 0\n" // invalidate I-cache and D-cache
|
|
|
|
: : : "r0", "cc");
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2014-05-10 13:40:46 +01:00
|
|
|
rc->kind = kind;
|
2015-04-06 22:38:53 +01:00
|
|
|
rc->scope_flags = scope_flags;
|
|
|
|
rc->n_pos_args = n_pos_args;
|
2019-02-19 03:15:39 +00:00
|
|
|
rc->fun_data = fun_data;
|
|
|
|
rc->const_table = const_table;
|
|
|
|
rc->type_sig = type_sig;
|
2014-03-27 23:26:35 +00:00
|
|
|
|
py: Add support to save native, viper and asm code to .mpy files.
This commit adds support for saving and loading .mpy files that contain
native code (native, viper and inline-asm). A lot of the ground work was
already done for this in the form of removing pointers from generated
native code. The changes here are mainly to link in qstr values to the
native code, and change the format of .mpy files to contain native code
blocks (possibly mixed with bytecode).
A top-level summary:
- @micropython.native, @micropython.viper and @micropython.asm_thumb/
asm_xtensa are now allowed in .py files when compiling to .mpy, and they
work transparently to the user.
- Entire .py files can be compiled to native via mpy-cross -X emit=native
and for the most part the generated .mpy files should work the same as
their bytecode version.
- The .mpy file format is changed to 1) specify in the header if the file
contains native code and if so the architecture (eg x86, ARMV7M, Xtensa);
2) for each function block the kind of code is specified (bytecode,
native, viper, asm).
- When native code is loaded from a .mpy file the native code must be
modified (in place) to link qstr values in, just like bytecode (see
py/persistentcode.c:arch_link_qstr() function).
In addition, this now defines a public, native ABI for dynamically loadable
native code generated by other languages, like C.
2019-02-21 04:18:33 +00:00
|
|
|
#if MICROPY_PERSISTENT_CODE_SAVE
|
|
|
|
rc->fun_data_len = fun_len;
|
|
|
|
rc->prelude_offset = prelude_offset;
|
|
|
|
rc->n_obj = n_obj;
|
|
|
|
rc->n_raw_code = n_raw_code;
|
|
|
|
rc->n_qstr = n_qstr;
|
|
|
|
rc->qstr_link = qstr_link;
|
|
|
|
#endif
|
|
|
|
|
2014-03-27 23:26:35 +00:00
|
|
|
#ifdef DEBUG_PRINT
|
2015-10-22 23:45:37 +01:00
|
|
|
DEBUG_printf("assign native: kind=%d fun=%p len=" UINT_FMT " n_pos_args=" UINT_FMT " flags=%x\n", kind, fun_data, fun_len, n_pos_args, (uint)scope_flags);
|
2014-08-24 16:28:17 +01:00
|
|
|
for (mp_uint_t i = 0; i < fun_len; i++) {
|
2014-03-27 23:26:35 +00:00
|
|
|
if (i > 0 && i % 16 == 0) {
|
|
|
|
DEBUG_printf("\n");
|
|
|
|
}
|
2014-08-24 16:28:17 +01:00
|
|
|
DEBUG_printf(" %02x", ((byte *)fun_data)[i]);
|
2014-03-27 23:26:35 +00:00
|
|
|
}
|
|
|
|
DEBUG_printf("\n");
|
|
|
|
|
|
|
|
#ifdef WRITE_CODE
|
2014-05-12 23:11:14 +01:00
|
|
|
FILE *fp_write_code = fopen("out-code", "wb");
|
2014-08-26 23:35:57 +01:00
|
|
|
fwrite(fun_data, fun_len, 1, fp_write_code);
|
2014-05-12 23:11:14 +01:00
|
|
|
fclose(fp_write_code);
|
2014-03-27 23:26:35 +00:00
|
|
|
#endif
|
2015-01-20 12:47:20 +00:00
|
|
|
#else
|
|
|
|
(void)fun_len;
|
2014-03-27 23:26:35 +00:00
|
|
|
#endif
|
|
|
|
}
|
2014-08-15 16:45:41 +01:00
|
|
|
#endif
|
2014-03-27 23:26:35 +00:00
|
|
|
|
2016-01-31 22:19:42 +00:00
|
|
|
mp_obj_t mp_make_function_from_raw_code(const mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args) {
|
2014-04-13 11:04:33 +01:00
|
|
|
DEBUG_OP_printf("make_function_from_raw_code %p\n", rc);
|
|
|
|
assert(rc != NULL);
|
2014-03-27 23:26:35 +00:00
|
|
|
|
2014-04-11 14:38:30 +01:00
|
|
|
// def_args must be MP_OBJ_NULL or a tuple
|
2019-01-30 07:49:52 +00:00
|
|
|
assert(def_args == MP_OBJ_NULL || mp_obj_is_type(def_args, &mp_type_tuple));
|
2014-04-11 14:38:30 +01:00
|
|
|
|
2014-06-07 22:01:00 +01:00
|
|
|
// def_kw_args must be MP_OBJ_NULL or a dict
|
2019-01-30 07:49:52 +00:00
|
|
|
assert(def_kw_args == MP_OBJ_NULL || mp_obj_is_type(def_kw_args, &mp_type_dict));
|
2014-03-31 15:18:37 +01:00
|
|
|
|
2014-04-13 11:04:33 +01:00
|
|
|
// make the function, depending on the raw code kind
|
2014-03-27 23:26:35 +00:00
|
|
|
mp_obj_t fun;
|
2014-04-13 11:04:33 +01:00
|
|
|
switch (rc->kind) {
|
2014-08-15 16:45:41 +01:00
|
|
|
#if MICROPY_EMIT_NATIVE
|
2014-05-10 13:40:46 +01:00
|
|
|
case MP_CODE_NATIVE_PY:
|
|
|
|
case MP_CODE_NATIVE_VIPER:
|
2019-02-19 03:15:39 +00:00
|
|
|
fun = mp_obj_new_fun_native(def_args, def_kw_args, rc->fun_data, rc->const_table);
|
2018-10-01 04:07:04 +01:00
|
|
|
// Check for a generator function, and if so change the type of the object
|
|
|
|
if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
|
|
|
|
((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_native_gen_wrap;
|
|
|
|
}
|
2014-08-15 16:45:41 +01:00
|
|
|
break;
|
|
|
|
#endif
|
2016-12-09 02:17:49 +00:00
|
|
|
#if MICROPY_EMIT_INLINE_ASM
|
2014-05-10 13:40:46 +01:00
|
|
|
case MP_CODE_NATIVE_ASM:
|
2019-02-19 03:15:39 +00:00
|
|
|
fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->fun_data, rc->type_sig);
|
2014-03-27 23:26:35 +00:00
|
|
|
break;
|
2014-08-15 16:45:41 +01:00
|
|
|
#endif
|
2014-03-27 23:26:35 +00:00
|
|
|
default:
|
2016-12-21 00:52:05 +00:00
|
|
|
// rc->kind should always be set and BYTECODE is the only remaining case
|
|
|
|
assert(rc->kind == MP_CODE_BYTECODE);
|
2019-02-19 03:15:39 +00:00
|
|
|
fun = mp_obj_new_fun_bc(def_args, def_kw_args, rc->fun_data, rc->const_table);
|
2018-06-29 07:32:58 +01:00
|
|
|
// check for generator functions and if so change the type of the object
|
|
|
|
if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
|
|
|
|
((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_gen_wrap;
|
|
|
|
}
|
2019-08-14 15:09:36 +01:00
|
|
|
|
|
|
|
#if MICROPY_PY_SYS_SETTRACE
|
|
|
|
mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t *)MP_OBJ_TO_PTR(fun);
|
|
|
|
self_fun->rc = rc;
|
|
|
|
#endif
|
|
|
|
|
2016-12-21 00:52:05 +00:00
|
|
|
break;
|
2014-03-27 23:26:35 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 12:58:40 +01:00
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
2016-01-31 22:19:42 +00:00
|
|
|
mp_obj_t mp_make_closure_from_raw_code(const mp_raw_code_t *rc, mp_uint_t n_closed_over, const mp_obj_t *args) {
|
2014-08-24 16:28:17 +01:00
|
|
|
DEBUG_OP_printf("make_closure_from_raw_code %p " UINT_FMT " %p\n", rc, n_closed_over, args);
|
2014-03-27 23:26:35 +00:00
|
|
|
// make function object
|
2014-04-20 17:50:40 +01:00
|
|
|
mp_obj_t ffun;
|
|
|
|
if (n_closed_over & 0x100) {
|
|
|
|
// default positional and keyword args given
|
|
|
|
ffun = mp_make_function_from_raw_code(rc, args[0], args[1]);
|
|
|
|
} else {
|
|
|
|
// default positional and keyword args not given
|
|
|
|
ffun = mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
|
|
|
|
}
|
2014-03-27 23:26:35 +00:00
|
|
|
// wrap function in closure object
|
2014-04-20 17:50:40 +01:00
|
|
|
return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2));
|
2014-03-27 23:26:35 +00:00
|
|
|
}
|