2015-05-07 19:19:56 -04:00
|
|
|
#include <stdio.h>
|
2017-01-15 23:04:53 -05:00
|
|
|
#include <string.h>
|
2015-05-07 19:19:56 -04:00
|
|
|
|
|
|
|
#include "py/obj.h"
|
py: Rework bytecode and .mpy file format to be mostly static data.
Background: .mpy files are precompiled .py files, built using mpy-cross,
that contain compiled bytecode functions (and can also contain machine
code). The benefit of using an .mpy file over a .py file is that they are
faster to import and take less memory when importing. They are also
smaller on disk.
But the real benefit of .mpy files comes when they are frozen into the
firmware. This is done by loading the .mpy file during compilation of the
firmware and turning it into a set of big C data structures (the job of
mpy-tool.py), which are then compiled and downloaded into the ROM of a
device. These C data structures can be executed in-place, ie directly from
ROM. This makes importing even faster because there is very little to do,
and also means such frozen modules take up much less RAM (because their
bytecode stays in ROM).
The downside of frozen code is that it requires recompiling and reflashing
the entire firmware. This can be a big barrier to entry, slows down
development time, and makes it harder to do OTA updates of frozen code
(because the whole firmware must be updated).
This commit attempts to solve this problem by providing a solution that
sits between loading .mpy files into RAM and freezing them into the
firmware. The .mpy file format has been reworked so that it consists of
data and bytecode which is mostly static and ready to run in-place. If
these new .mpy files are located in flash/ROM which is memory addressable,
the .mpy file can be executed (mostly) in-place.
With this approach there is still a small amount of unpacking and linking
of the .mpy file that needs to be done when it's imported, but it's still
much better than loading an .mpy from disk into RAM (although not as good
as freezing .mpy files into the firmware).
The main trick to make static .mpy files is to adjust the bytecode so any
qstrs that it references now go through a lookup table to convert from
local qstr number in the module to global qstr number in the firmware.
That means the bytecode does not need linking/rewriting of qstrs when it's
loaded. Instead only a small qstr table needs to be built (and put in RAM)
at import time. This means the bytecode itself is static/constant and can
be used directly if it's in addressable memory. Also the qstr string data
in the .mpy file, and some constant object data, can be used directly.
Note that the qstr table is global to the module (ie not per function).
In more detail, in the VM what used to be (schematically):
qst = DECODE_QSTR_VALUE;
is now (schematically):
idx = DECODE_QSTR_INDEX;
qst = qstr_table[idx];
That allows the bytecode to be fixed at compile time and not need
relinking/rewriting of the qstr values. Only qstr_table needs to be linked
when the .mpy is loaded.
Incidentally, this helps to reduce the size of bytecode because what used
to be 2-byte qstr values in the bytecode are now (mostly) 1-byte indices.
If the module uses the same qstr more than two times then the bytecode is
smaller than before.
The following changes are measured for this commit compared to the
previous (the baseline):
- average 7%-9% reduction in size of .mpy files
- frozen code size is reduced by about 5%-7%
- importing .py files uses about 5% less RAM in total
- importing .mpy files uses about 4% less RAM in total
- importing .py and .mpy files takes about the same time as before
The qstr indirection in the bytecode has only a small impact on VM
performance. For stm32 on PYBv1.0 the performance change of this commit
is:
diff of scores (higher is better)
N=100 M=100 baseline -> this-commit diff diff% (error%)
bm_chaos.py 371.07 -> 357.39 : -13.68 = -3.687% (+/-0.02%)
bm_fannkuch.py 78.72 -> 77.49 : -1.23 = -1.563% (+/-0.01%)
bm_fft.py 2591.73 -> 2539.28 : -52.45 = -2.024% (+/-0.00%)
bm_float.py 6034.93 -> 5908.30 : -126.63 = -2.098% (+/-0.01%)
bm_hexiom.py 48.96 -> 47.93 : -1.03 = -2.104% (+/-0.00%)
bm_nqueens.py 4510.63 -> 4459.94 : -50.69 = -1.124% (+/-0.00%)
bm_pidigits.py 650.28 -> 644.96 : -5.32 = -0.818% (+/-0.23%)
core_import_mpy_multi.py 564.77 -> 581.49 : +16.72 = +2.960% (+/-0.01%)
core_import_mpy_single.py 68.67 -> 67.16 : -1.51 = -2.199% (+/-0.01%)
core_qstr.py 64.16 -> 64.12 : -0.04 = -0.062% (+/-0.00%)
core_yield_from.py 362.58 -> 354.50 : -8.08 = -2.228% (+/-0.00%)
misc_aes.py 429.69 -> 405.59 : -24.10 = -5.609% (+/-0.01%)
misc_mandel.py 3485.13 -> 3416.51 : -68.62 = -1.969% (+/-0.00%)
misc_pystone.py 2496.53 -> 2405.56 : -90.97 = -3.644% (+/-0.01%)
misc_raytrace.py 381.47 -> 374.01 : -7.46 = -1.956% (+/-0.01%)
viper_call0.py 576.73 -> 572.49 : -4.24 = -0.735% (+/-0.04%)
viper_call1a.py 550.37 -> 546.21 : -4.16 = -0.756% (+/-0.09%)
viper_call1b.py 438.23 -> 435.68 : -2.55 = -0.582% (+/-0.06%)
viper_call1c.py 442.84 -> 440.04 : -2.80 = -0.632% (+/-0.08%)
viper_call2a.py 536.31 -> 532.35 : -3.96 = -0.738% (+/-0.06%)
viper_call2b.py 382.34 -> 377.07 : -5.27 = -1.378% (+/-0.03%)
And for unix on x64:
diff of scores (higher is better)
N=2000 M=2000 baseline -> this-commit diff diff% (error%)
bm_chaos.py 13594.20 -> 13073.84 : -520.36 = -3.828% (+/-5.44%)
bm_fannkuch.py 60.63 -> 59.58 : -1.05 = -1.732% (+/-3.01%)
bm_fft.py 112009.15 -> 111603.32 : -405.83 = -0.362% (+/-4.03%)
bm_float.py 246202.55 -> 247923.81 : +1721.26 = +0.699% (+/-2.79%)
bm_hexiom.py 615.65 -> 617.21 : +1.56 = +0.253% (+/-1.64%)
bm_nqueens.py 215807.95 -> 215600.96 : -206.99 = -0.096% (+/-3.52%)
bm_pidigits.py 8246.74 -> 8422.82 : +176.08 = +2.135% (+/-3.64%)
misc_aes.py 16133.00 -> 16452.74 : +319.74 = +1.982% (+/-1.50%)
misc_mandel.py 128146.69 -> 130796.43 : +2649.74 = +2.068% (+/-3.18%)
misc_pystone.py 83811.49 -> 83124.85 : -686.64 = -0.819% (+/-1.03%)
misc_raytrace.py 21688.02 -> 21385.10 : -302.92 = -1.397% (+/-3.20%)
The code size change is (firmware with a lot of frozen code benefits the
most):
bare-arm: +396 +0.697%
minimal x86: +1595 +0.979% [incl +32(data)]
unix x64: +2408 +0.470% [incl +800(data)]
unix nanbox: +1396 +0.309% [incl -96(data)]
stm32: -1256 -0.318% PYBV10
cc3200: +288 +0.157%
esp8266: -260 -0.037% GENERIC
esp32: -216 -0.014% GENERIC[incl -1072(data)]
nrf: +116 +0.067% pca10040
rp2: -664 -0.135% PICO
samd: +844 +0.607% ADAFRUIT_ITSYBITSY_M4_EXPRESS
As part of this change the .mpy file format version is bumped to version 6.
And mpy-tool.py has been improved to provide a good visualisation of the
contents of .mpy files.
In summary: this commit changes the bytecode to use qstr indirection, and
reworks the .mpy file format to be simpler and allow .mpy files to be
executed in-place. Performance is not impacted too much. Eventually it
will be possible to store such .mpy files in a linear, read-only, memory-
mappable filesystem so they can be executed from flash/ROM. This will
essentially be able to replace frozen code for most applications.
Signed-off-by: Damien George <damien@micropython.org>
2021-10-22 07:22:47 -04:00
|
|
|
#include "py/objfun.h"
|
2016-09-02 01:07:42 -04:00
|
|
|
#include "py/objstr.h"
|
2015-05-07 19:19:56 -04:00
|
|
|
#include "py/runtime.h"
|
2018-03-01 06:49:15 -05:00
|
|
|
#include "py/gc.h"
|
2015-05-07 19:19:56 -04:00
|
|
|
#include "py/repl.h"
|
2015-10-01 13:49:37 -04:00
|
|
|
#include "py/mpz.h"
|
2016-12-29 02:07:38 -05:00
|
|
|
#include "py/builtin.h"
|
2017-01-03 22:23:29 -05:00
|
|
|
#include "py/emit.h"
|
2017-01-04 19:12:05 -05:00
|
|
|
#include "py/formatfloat.h"
|
2021-04-23 15:26:42 -04:00
|
|
|
#include "py/ringbuf.h"
|
2020-01-13 08:02:01 -05:00
|
|
|
#include "py/pairheap.h"
|
2017-01-15 23:04:53 -05:00
|
|
|
#include "py/stream.h"
|
2017-03-14 02:58:43 -04:00
|
|
|
#include "py/binary.h"
|
2018-02-27 00:17:10 -05:00
|
|
|
#include "py/bc.h"
|
2017-01-15 23:04:53 -05:00
|
|
|
|
2020-01-22 14:44:52 -05:00
|
|
|
// expected output of this file is found in extra_coverage.py.exp
|
|
|
|
|
2017-01-16 00:20:18 -05:00
|
|
|
#if defined(MICROPY_UNIX_COVERAGE)
|
|
|
|
|
2017-01-15 23:04:53 -05:00
|
|
|
// stream testing object
|
|
|
|
typedef struct _mp_obj_streamtest_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
uint8_t *buf;
|
|
|
|
size_t len;
|
|
|
|
size_t pos;
|
|
|
|
int error_code;
|
|
|
|
} mp_obj_streamtest_t;
|
|
|
|
|
|
|
|
STATIC mp_obj_t stest_set_buf(mp_obj_t o_in, mp_obj_t buf_in) {
|
|
|
|
mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in);
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
|
|
|
|
o->buf = m_new(uint8_t, bufinfo.len);
|
|
|
|
memcpy(o->buf, bufinfo.buf, bufinfo.len);
|
|
|
|
o->len = bufinfo.len;
|
|
|
|
o->pos = 0;
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(stest_set_buf_obj, stest_set_buf);
|
|
|
|
|
|
|
|
STATIC mp_obj_t stest_set_error(mp_obj_t o_in, mp_obj_t err_in) {
|
|
|
|
mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in);
|
|
|
|
o->error_code = mp_obj_get_int(err_in);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(stest_set_error_obj, stest_set_error);
|
|
|
|
|
|
|
|
STATIC mp_uint_t stest_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
|
|
|
|
mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in);
|
|
|
|
if (o->pos < o->len) {
|
|
|
|
if (size > o->len - o->pos) {
|
|
|
|
size = o->len - o->pos;
|
|
|
|
}
|
|
|
|
memcpy(buf, o->buf + o->pos, size);
|
|
|
|
o->pos += size;
|
|
|
|
return size;
|
|
|
|
} else if (o->error_code == 0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
*errcode = o->error_code;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_uint_t stest_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
|
|
|
|
mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in);
|
|
|
|
(void)buf;
|
|
|
|
(void)size;
|
|
|
|
*errcode = o->error_code;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_uint_t stest_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
|
|
|
|
mp_obj_streamtest_t *o = MP_OBJ_TO_PTR(o_in);
|
|
|
|
(void)arg;
|
|
|
|
(void)request;
|
|
|
|
(void)errcode;
|
|
|
|
if (o->error_code != 0) {
|
|
|
|
*errcode = o->error_code;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t rawfile_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_set_buf), MP_ROM_PTR(&stest_set_buf_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_set_error), MP_ROM_PTR(&stest_set_error_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read1), MP_ROM_PTR(&mp_stream_read1_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_write1), MP_ROM_PTR(&mp_stream_write1_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&mp_stream_ioctl_obj) },
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(rawfile_locals_dict, rawfile_locals_dict_table);
|
|
|
|
|
|
|
|
STATIC const mp_stream_p_t fileio_stream_p = {
|
2023-08-15 14:25:25 -04:00
|
|
|
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_stream)
|
2017-01-15 23:04:53 -05:00
|
|
|
.read = stest_read,
|
|
|
|
.write = stest_write,
|
|
|
|
.ioctl = stest_ioctl,
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t mp_type_stest_fileio = {
|
|
|
|
{ &mp_type_type },
|
2023-08-14 09:51:52 -04:00
|
|
|
.flags = MP_TYPE_FLAG_EXTENDED,
|
2021-03-15 09:57:36 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&rawfile_locals_dict,
|
2023-08-14 09:51:52 -04:00
|
|
|
MP_TYPE_EXTENDED_FIELDS(
|
|
|
|
.protocol = &fileio_stream_p,
|
|
|
|
),
|
2017-01-15 23:04:53 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// stream read returns non-blocking error
|
|
|
|
STATIC mp_uint_t stest_read2(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
|
|
|
|
(void)o_in;
|
|
|
|
(void)buf;
|
|
|
|
(void)size;
|
|
|
|
*errcode = MP_EAGAIN;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t rawfile_locals_dict_table2[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(rawfile_locals_dict2, rawfile_locals_dict_table2);
|
|
|
|
|
|
|
|
STATIC const mp_stream_p_t textio_stream_p2 = {
|
2023-08-14 09:51:52 -04:00
|
|
|
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_stream)
|
2017-01-15 23:04:53 -05:00
|
|
|
.read = stest_read2,
|
|
|
|
.write = NULL,
|
|
|
|
.is_text = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t mp_type_stest_textio2 = {
|
|
|
|
{ &mp_type_type },
|
2023-08-14 09:51:52 -04:00
|
|
|
.flags = MP_TYPE_FLAG_EXTENDED,
|
2021-03-15 09:57:36 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&rawfile_locals_dict2,
|
2023-08-14 09:51:52 -04:00
|
|
|
MP_TYPE_EXTENDED_FIELDS(
|
|
|
|
.protocol = &textio_stream_p2,
|
|
|
|
),
|
2017-01-15 23:04:53 -05:00
|
|
|
};
|
|
|
|
|
2016-09-02 01:07:42 -04:00
|
|
|
// str/bytes objects without a valid hash
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC const mp_obj_str_t str_no_hash_obj = {{&mp_type_str}, 0, 10, (const byte *)"0123456789"};
|
|
|
|
STATIC const mp_obj_str_t bytes_no_hash_obj = {{&mp_type_bytes}, 0, 10, (const byte *)"0123456789"};
|
2016-09-02 01:07:42 -04:00
|
|
|
|
2020-01-13 08:02:01 -05:00
|
|
|
STATIC int pairheap_lt(mp_pairheap_t *a, mp_pairheap_t *b) {
|
|
|
|
return (uintptr_t)a < (uintptr_t)b;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ops array contain operations: x>=0 means push(x), x<0 means delete(-x)
|
|
|
|
STATIC void pairheap_test(size_t nops, int *ops) {
|
|
|
|
mp_pairheap_t node[8];
|
2020-03-13 00:06:10 -04:00
|
|
|
for (size_t i = 0; i < MP_ARRAY_SIZE(node); ++i) {
|
|
|
|
mp_pairheap_init_node(pairheap_lt, &node[i]);
|
|
|
|
}
|
2020-01-13 08:02:01 -05:00
|
|
|
mp_pairheap_t *heap = mp_pairheap_new(pairheap_lt);
|
2021-12-08 20:38:26 -05:00
|
|
|
mp_printf(&mp_plat_print, "create:");
|
2020-01-13 08:02:01 -05:00
|
|
|
for (size_t i = 0; i < nops; ++i) {
|
|
|
|
if (ops[i] >= 0) {
|
|
|
|
heap = mp_pairheap_push(pairheap_lt, heap, &node[ops[i]]);
|
|
|
|
} else {
|
|
|
|
heap = mp_pairheap_delete(pairheap_lt, heap, &node[-ops[i]]);
|
|
|
|
}
|
|
|
|
if (mp_pairheap_is_empty(pairheap_lt, heap)) {
|
|
|
|
mp_printf(&mp_plat_print, " -");
|
|
|
|
} else {
|
2020-02-26 23:36:53 -05:00
|
|
|
mp_printf(&mp_plat_print, " %d", mp_pairheap_peek(pairheap_lt, heap) - &node[0]);
|
|
|
|
;
|
2020-01-13 08:02:01 -05:00
|
|
|
}
|
|
|
|
}
|
2021-12-08 20:38:26 -05:00
|
|
|
mp_printf(&mp_plat_print, "\npop all:");
|
2020-01-13 08:02:01 -05:00
|
|
|
while (!mp_pairheap_is_empty(pairheap_lt, heap)) {
|
2020-02-26 23:36:53 -05:00
|
|
|
mp_printf(&mp_plat_print, " %d", mp_pairheap_peek(pairheap_lt, heap) - &node[0]);
|
|
|
|
;
|
2020-01-13 08:02:01 -05:00
|
|
|
heap = mp_pairheap_pop(pairheap_lt, heap);
|
|
|
|
}
|
2021-12-08 20:38:26 -05:00
|
|
|
mp_printf(&mp_plat_print, "\n");
|
2020-01-13 08:02:01 -05:00
|
|
|
}
|
|
|
|
|
2015-05-07 19:19:56 -04:00
|
|
|
// function to run extra tests for things that can't be checked by scripts
|
2015-05-08 04:18:38 -04:00
|
|
|
STATIC mp_obj_t extra_coverage(void) {
|
2015-05-28 10:25:07 -04:00
|
|
|
// mp_printf (used by ports that don't have a native printf)
|
|
|
|
{
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "# mp_printf\n");
|
2015-05-28 10:25:07 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d %+d % d\n", -123, 123, 123); // sign
|
|
|
|
mp_printf(&mp_plat_print, "%05d\n", -123); // negative number with zero padding
|
|
|
|
mp_printf(&mp_plat_print, "%ld\n", 123); // long
|
2017-12-18 23:01:17 -05:00
|
|
|
mp_printf(&mp_plat_print, "%lx\n", 0x123); // long hex
|
2015-05-28 10:25:07 -04:00
|
|
|
mp_printf(&mp_plat_print, "%X\n", 0x1abcdef); // capital hex
|
2020-10-22 04:38:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.2s %.3s '%4.4s' '%5.5q' '%.3q'\n", "abc", "abc", "abc", MP_QSTR_True, MP_QSTR_True); // fixed string precision
|
2015-05-28 10:25:07 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", -1, "abc"); // negative string precision
|
|
|
|
mp_printf(&mp_plat_print, "%b %b\n", 0, 1); // bools
|
2018-03-07 20:49:31 -05:00
|
|
|
#ifndef NDEBUG
|
2015-05-28 10:25:07 -04:00
|
|
|
mp_printf(&mp_plat_print, "%s\n", NULL); // null string
|
2018-03-07 20:49:31 -05:00
|
|
|
#else
|
|
|
|
mp_printf(&mp_plat_print, "(null)\n"); // without debugging mp_printf won't check for null
|
|
|
|
#endif
|
2016-02-01 10:08:42 -05:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", 0x80000000); // should print signed
|
|
|
|
mp_printf(&mp_plat_print, "%u\n", 0x80000000); // should print unsigned
|
|
|
|
mp_printf(&mp_plat_print, "%x\n", 0x80000000); // should print unsigned
|
|
|
|
mp_printf(&mp_plat_print, "%X\n", 0x80000000); // should print unsigned
|
2017-06-29 22:25:42 -04:00
|
|
|
mp_printf(&mp_plat_print, "abc\n%"); // string ends in middle of format specifier
|
2021-04-22 20:55:39 -04:00
|
|
|
mp_printf(&mp_plat_print, "%%\n"); // literal % character
|
2015-05-28 10:25:07 -04:00
|
|
|
}
|
|
|
|
|
2018-03-01 06:49:15 -05:00
|
|
|
// GC
|
|
|
|
{
|
|
|
|
mp_printf(&mp_plat_print, "# GC\n");
|
|
|
|
|
|
|
|
// calling gc_free while GC is locked
|
|
|
|
gc_lock();
|
|
|
|
gc_free(NULL);
|
|
|
|
gc_unlock();
|
|
|
|
|
|
|
|
// using gc_realloc to resize to 0, which means free the memory
|
2018-03-01 18:59:09 -05:00
|
|
|
void *p = gc_alloc(4, false);
|
2018-03-01 06:49:15 -05:00
|
|
|
mp_printf(&mp_plat_print, "%p\n", gc_realloc(p, 0, false));
|
|
|
|
|
|
|
|
// calling gc_nbytes with a non-heap pointer
|
|
|
|
mp_printf(&mp_plat_print, "%p\n", gc_nbytes(NULL));
|
|
|
|
}
|
|
|
|
|
2022-05-03 22:12:11 -04:00
|
|
|
// tracked allocation
|
|
|
|
{
|
|
|
|
#define NUM_PTRS (8)
|
|
|
|
#define NUM_BYTES (128)
|
|
|
|
#define FLIP_POINTER(p) ((uint8_t *)((uintptr_t)(p) ^ 0x0f))
|
|
|
|
|
|
|
|
mp_printf(&mp_plat_print, "# tracked allocation\n");
|
|
|
|
mp_printf(&mp_plat_print, "m_tracked_head = %p\n", MP_STATE_VM(m_tracked_head));
|
|
|
|
|
|
|
|
uint8_t *ptrs[NUM_PTRS];
|
|
|
|
|
|
|
|
// allocate memory blocks
|
|
|
|
for (size_t i = 0; i < NUM_PTRS; ++i) {
|
|
|
|
ptrs[i] = m_tracked_calloc(1, NUM_BYTES);
|
|
|
|
bool all_zero = true;
|
|
|
|
for (size_t j = 0; j < NUM_BYTES; ++j) {
|
|
|
|
if (ptrs[i][j] != 0) {
|
|
|
|
all_zero = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ptrs[i][j] = j;
|
|
|
|
}
|
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", i, all_zero);
|
|
|
|
|
|
|
|
// hide the pointer from the GC and collect
|
|
|
|
ptrs[i] = FLIP_POINTER(ptrs[i]);
|
|
|
|
gc_collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the memory blocks have the correct content
|
|
|
|
for (size_t i = 0; i < NUM_PTRS; ++i) {
|
|
|
|
bool correct_contents = true;
|
|
|
|
for (size_t j = 0; j < NUM_BYTES; ++j) {
|
|
|
|
if (FLIP_POINTER(ptrs[i])[j] != j) {
|
|
|
|
correct_contents = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", i, correct_contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
// free the memory blocks
|
|
|
|
for (size_t i = 0; i < NUM_PTRS; ++i) {
|
|
|
|
m_tracked_free(FLIP_POINTER(ptrs[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_printf(&mp_plat_print, "m_tracked_head = %p\n", MP_STATE_VM(m_tracked_head));
|
|
|
|
}
|
|
|
|
|
2015-05-12 18:34:10 -04:00
|
|
|
// vstr
|
|
|
|
{
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "# vstr\n");
|
2016-10-14 01:46:34 -04:00
|
|
|
vstr_t *vstr = vstr_new(16);
|
2015-05-12 18:34:10 -04:00
|
|
|
vstr_hint_size(vstr, 32);
|
|
|
|
vstr_add_str(vstr, "ts");
|
|
|
|
vstr_ins_byte(vstr, 1, 'e');
|
|
|
|
vstr_ins_char(vstr, 3, 't');
|
|
|
|
vstr_ins_char(vstr, 10, 's');
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
|
2015-05-12 18:34:10 -04:00
|
|
|
|
|
|
|
vstr_cut_head_bytes(vstr, 2);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
|
2015-05-12 18:34:10 -04:00
|
|
|
|
|
|
|
vstr_cut_tail_bytes(vstr, 10);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
|
2015-05-12 18:34:10 -04:00
|
|
|
|
|
|
|
vstr_printf(vstr, "t%cst", 'e');
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
|
2015-05-12 18:34:10 -04:00
|
|
|
|
|
|
|
vstr_cut_out_bytes(vstr, 3, 10);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
|
2015-05-12 18:34:10 -04:00
|
|
|
|
|
|
|
VSTR_FIXED(fix, 4);
|
2017-08-31 18:05:24 -04:00
|
|
|
nlr_buf_t nlr;
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
|
|
|
vstr_add_str(&fix, "large");
|
|
|
|
nlr_pop();
|
|
|
|
} else {
|
|
|
|
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
|
|
|
|
}
|
|
|
|
|
|
|
|
fix.len = fix.alloc;
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
|
|
|
vstr_null_terminated_str(&fix);
|
|
|
|
nlr_pop();
|
|
|
|
} else {
|
|
|
|
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
|
|
|
|
}
|
2015-05-12 18:34:10 -04:00
|
|
|
}
|
|
|
|
|
2015-05-07 19:19:56 -04:00
|
|
|
// repl autocomplete
|
|
|
|
{
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "# repl\n");
|
2015-05-12 18:34:10 -04:00
|
|
|
|
2015-05-07 19:19:56 -04:00
|
|
|
const char *str;
|
2017-07-04 09:44:54 -04:00
|
|
|
size_t len = mp_repl_autocomplete("__n", 3, &mp_plat_print, &str);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
|
2015-05-07 19:19:56 -04:00
|
|
|
|
2023-08-22 11:15:46 -04:00
|
|
|
len = mp_repl_autocomplete("im", 2, &mp_plat_print, &str);
|
2021-04-03 16:56:02 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
|
|
|
|
mp_repl_autocomplete("import ", 7, &mp_plat_print, &str);
|
2023-08-22 11:15:46 -04:00
|
|
|
len = mp_repl_autocomplete("import ti", 9, &mp_plat_print, &str);
|
2021-04-03 16:56:02 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
|
2023-08-22 11:15:46 -04:00
|
|
|
mp_repl_autocomplete("import ra", 9, &mp_plat_print, &str);
|
2021-04-03 16:56:02 -04:00
|
|
|
|
2015-05-07 19:19:56 -04:00
|
|
|
mp_store_global(MP_QSTR_sys, mp_import_name(MP_QSTR_sys, mp_const_none, MP_OBJ_NEW_SMALL_INT(0)));
|
|
|
|
mp_repl_autocomplete("sys.", 4, &mp_plat_print, &str);
|
|
|
|
len = mp_repl_autocomplete("sys.impl", 8, &mp_plat_print, &str);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
|
2015-05-07 19:19:56 -04:00
|
|
|
}
|
2015-05-08 04:18:38 -04:00
|
|
|
|
2015-05-12 18:34:10 -04:00
|
|
|
// attrtuple
|
|
|
|
{
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "# attrtuple\n");
|
2015-05-12 18:34:10 -04:00
|
|
|
|
|
|
|
static const qstr fields[] = {MP_QSTR_start, MP_QSTR_stop, MP_QSTR_step};
|
|
|
|
static const mp_obj_t items[] = {MP_OBJ_NEW_SMALL_INT(1), MP_OBJ_NEW_SMALL_INT(2), MP_OBJ_NEW_SMALL_INT(3)};
|
|
|
|
mp_obj_print_helper(&mp_plat_print, mp_obj_new_attrtuple(fields, 3, items), PRINT_REPR);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "\n");
|
2015-05-12 18:34:10 -04:00
|
|
|
}
|
|
|
|
|
2015-09-03 18:06:18 -04:00
|
|
|
// str
|
|
|
|
{
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "# str\n");
|
2015-09-03 18:06:18 -04:00
|
|
|
|
|
|
|
// intern string
|
2021-04-22 20:55:39 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", mp_obj_is_qstr(mp_obj_str_intern(mp_obj_new_str("intern me", 9))));
|
2015-09-03 18:06:18 -04:00
|
|
|
}
|
|
|
|
|
2018-02-07 19:14:30 -05:00
|
|
|
// bytearray
|
|
|
|
{
|
|
|
|
mp_printf(&mp_plat_print, "# bytearray\n");
|
|
|
|
|
|
|
|
// create a bytearray via mp_obj_new_bytearray
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(mp_obj_new_bytearray(4, "data"), &bufinfo, MP_BUFFER_RW);
|
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", bufinfo.len, bufinfo.buf);
|
|
|
|
}
|
|
|
|
|
2015-10-01 13:49:37 -04:00
|
|
|
// mpz
|
|
|
|
{
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "# mpz\n");
|
2015-10-01 13:49:37 -04:00
|
|
|
|
|
|
|
mp_uint_t value;
|
|
|
|
mpz_t mpz;
|
|
|
|
mpz_init_zero(&mpz);
|
|
|
|
|
|
|
|
// mpz_as_uint_checked, with success
|
|
|
|
mpz_set_from_int(&mpz, 12345678);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", mpz_as_uint_checked(&mpz, &value));
|
|
|
|
mp_printf(&mp_plat_print, "%d\n", (int)value);
|
2015-10-01 13:49:37 -04:00
|
|
|
|
|
|
|
// mpz_as_uint_checked, with negative arg
|
|
|
|
mpz_set_from_int(&mpz, -1);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", mpz_as_uint_checked(&mpz, &value));
|
2015-10-01 13:49:37 -04:00
|
|
|
|
|
|
|
// mpz_as_uint_checked, with overflowing arg
|
|
|
|
mpz_set_from_int(&mpz, 1);
|
|
|
|
mpz_shl_inpl(&mpz, &mpz, 70);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", mpz_as_uint_checked(&mpz, &value));
|
2018-02-25 07:43:16 -05:00
|
|
|
|
|
|
|
// mpz_set_from_float with inf as argument
|
|
|
|
mpz_set_from_float(&mpz, 1.0 / 0.0);
|
|
|
|
mpz_as_uint_checked(&mpz, &value);
|
|
|
|
mp_printf(&mp_plat_print, "%d\n", (int)value);
|
|
|
|
|
|
|
|
// mpz_set_from_float with 0 as argument
|
|
|
|
mpz_set_from_float(&mpz, 0);
|
|
|
|
mpz_as_uint_checked(&mpz, &value);
|
|
|
|
mp_printf(&mp_plat_print, "%d\n", (int)value);
|
|
|
|
|
|
|
|
// mpz_set_from_float with 0<x<1 as argument
|
|
|
|
mpz_set_from_float(&mpz, 1e-10);
|
|
|
|
mpz_as_uint_checked(&mpz, &value);
|
|
|
|
mp_printf(&mp_plat_print, "%d\n", (int)value);
|
|
|
|
|
|
|
|
// mpz_set_from_float with 1<=x<2 as argument
|
|
|
|
mpz_set_from_float(&mpz, 1.5);
|
|
|
|
mpz_as_uint_checked(&mpz, &value);
|
|
|
|
mp_printf(&mp_plat_print, "%d\n", (int)value);
|
|
|
|
|
|
|
|
// mpz_set_from_float with 2<x as argument
|
|
|
|
mpz_set_from_float(&mpz, 12345);
|
|
|
|
mpz_as_uint_checked(&mpz, &value);
|
|
|
|
mp_printf(&mp_plat_print, "%d\n", (int)value);
|
|
|
|
|
|
|
|
// mpz_mul_inpl with dest==rhs, lhs!=rhs
|
|
|
|
mpz_t mpz2;
|
|
|
|
mpz_set_from_int(&mpz, 2);
|
|
|
|
mpz_init_from_int(&mpz2, 3);
|
|
|
|
mpz_mul_inpl(&mpz, &mpz2, &mpz);
|
|
|
|
mpz_as_uint_checked(&mpz, &value);
|
|
|
|
mp_printf(&mp_plat_print, "%d\n", (int)value);
|
2015-10-01 13:49:37 -04:00
|
|
|
}
|
|
|
|
|
2016-12-29 02:07:38 -05:00
|
|
|
// runtime utils
|
|
|
|
{
|
|
|
|
mp_printf(&mp_plat_print, "# runtime utils\n");
|
|
|
|
|
|
|
|
// call mp_call_function_1_protected
|
|
|
|
mp_call_function_1_protected(MP_OBJ_FROM_PTR(&mp_builtin_abs_obj), MP_OBJ_NEW_SMALL_INT(1));
|
|
|
|
// call mp_call_function_1_protected with invalid args
|
2017-11-15 21:17:51 -05:00
|
|
|
mp_call_function_1_protected(MP_OBJ_FROM_PTR(&mp_builtin_abs_obj), mp_obj_new_str("abc", 3));
|
2016-12-29 02:07:38 -05:00
|
|
|
|
|
|
|
// call mp_call_function_2_protected
|
|
|
|
mp_call_function_2_protected(MP_OBJ_FROM_PTR(&mp_builtin_divmod_obj), MP_OBJ_NEW_SMALL_INT(1), MP_OBJ_NEW_SMALL_INT(1));
|
|
|
|
// call mp_call_function_2_protected with invalid args
|
2017-11-15 21:17:51 -05:00
|
|
|
mp_call_function_2_protected(MP_OBJ_FROM_PTR(&mp_builtin_divmod_obj), mp_obj_new_str("abc", 3), mp_obj_new_str("abc", 3));
|
2020-01-14 07:45:56 -05:00
|
|
|
|
|
|
|
// mp_obj_int_get_uint_checked with non-negative small-int
|
|
|
|
mp_printf(&mp_plat_print, "%d\n", (int)mp_obj_int_get_uint_checked(MP_OBJ_NEW_SMALL_INT(1)));
|
|
|
|
|
|
|
|
// mp_obj_int_get_uint_checked with non-negative big-int
|
|
|
|
mp_printf(&mp_plat_print, "%d\n", (int)mp_obj_int_get_uint_checked(mp_obj_new_int_from_ll(2)));
|
|
|
|
|
|
|
|
// mp_obj_int_get_uint_checked with negative small-int (should raise exception)
|
|
|
|
nlr_buf_t nlr;
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
|
|
|
mp_obj_int_get_uint_checked(MP_OBJ_NEW_SMALL_INT(-1));
|
|
|
|
nlr_pop();
|
|
|
|
} else {
|
|
|
|
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
|
|
|
|
}
|
|
|
|
|
|
|
|
// mp_obj_int_get_uint_checked with negative big-int (should raise exception)
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
|
|
|
mp_obj_int_get_uint_checked(mp_obj_new_int_from_ll(-2));
|
|
|
|
nlr_pop();
|
|
|
|
} else {
|
|
|
|
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
|
|
|
|
}
|
2020-01-22 21:18:34 -05:00
|
|
|
|
|
|
|
// call mp_obj_new_exception_args (it's a part of the public C API and not used in the core)
|
|
|
|
mp_obj_print_exception(&mp_plat_print, mp_obj_new_exception_args(&mp_type_ValueError, 0, NULL));
|
2016-12-29 02:07:38 -05:00
|
|
|
}
|
|
|
|
|
2017-01-03 22:23:29 -05:00
|
|
|
// warning
|
|
|
|
{
|
|
|
|
mp_emitter_warning(MP_PASS_CODE_SIZE, "test");
|
|
|
|
}
|
|
|
|
|
2017-01-04 19:12:05 -05:00
|
|
|
// format float
|
|
|
|
{
|
|
|
|
mp_printf(&mp_plat_print, "# format float\n");
|
|
|
|
|
|
|
|
// format with inadequate buffer size
|
|
|
|
char buf[5];
|
|
|
|
mp_format_float(1, buf, sizeof(buf), 'g', 0, '+');
|
|
|
|
mp_printf(&mp_plat_print, "%s\n", buf);
|
|
|
|
|
|
|
|
// format with just enough buffer so that precision must be
|
|
|
|
// set from 0 to 1 twice
|
|
|
|
char buf2[8];
|
|
|
|
mp_format_float(1, buf2, sizeof(buf2), 'g', 0, '+');
|
|
|
|
mp_printf(&mp_plat_print, "%s\n", buf2);
|
|
|
|
|
|
|
|
// format where precision is trimmed to avoid buffer overflow
|
|
|
|
mp_format_float(1, buf2, sizeof(buf2), 'e', 0, '+');
|
|
|
|
mp_printf(&mp_plat_print, "%s\n", buf2);
|
|
|
|
}
|
|
|
|
|
2017-03-14 02:58:43 -04:00
|
|
|
// binary
|
|
|
|
{
|
|
|
|
mp_printf(&mp_plat_print, "# binary\n");
|
|
|
|
|
|
|
|
// call function with float and double typecodes
|
|
|
|
float far[1];
|
|
|
|
double dar[1];
|
|
|
|
mp_binary_set_val_array_from_int('f', far, 0, 123);
|
|
|
|
mp_printf(&mp_plat_print, "%.0f\n", (double)far[0]);
|
|
|
|
mp_binary_set_val_array_from_int('d', dar, 0, 456);
|
|
|
|
mp_printf(&mp_plat_print, "%.0lf\n", dar[0]);
|
|
|
|
}
|
|
|
|
|
2018-02-27 00:17:10 -05:00
|
|
|
// VM
|
|
|
|
{
|
|
|
|
mp_printf(&mp_plat_print, "# VM\n");
|
|
|
|
|
2023-03-18 11:17:02 -04:00
|
|
|
// call mp_execute_bytecode with invalid bytecode (should raise NotImplementedError)
|
py: Rework bytecode and .mpy file format to be mostly static data.
Background: .mpy files are precompiled .py files, built using mpy-cross,
that contain compiled bytecode functions (and can also contain machine
code). The benefit of using an .mpy file over a .py file is that they are
faster to import and take less memory when importing. They are also
smaller on disk.
But the real benefit of .mpy files comes when they are frozen into the
firmware. This is done by loading the .mpy file during compilation of the
firmware and turning it into a set of big C data structures (the job of
mpy-tool.py), which are then compiled and downloaded into the ROM of a
device. These C data structures can be executed in-place, ie directly from
ROM. This makes importing even faster because there is very little to do,
and also means such frozen modules take up much less RAM (because their
bytecode stays in ROM).
The downside of frozen code is that it requires recompiling and reflashing
the entire firmware. This can be a big barrier to entry, slows down
development time, and makes it harder to do OTA updates of frozen code
(because the whole firmware must be updated).
This commit attempts to solve this problem by providing a solution that
sits between loading .mpy files into RAM and freezing them into the
firmware. The .mpy file format has been reworked so that it consists of
data and bytecode which is mostly static and ready to run in-place. If
these new .mpy files are located in flash/ROM which is memory addressable,
the .mpy file can be executed (mostly) in-place.
With this approach there is still a small amount of unpacking and linking
of the .mpy file that needs to be done when it's imported, but it's still
much better than loading an .mpy from disk into RAM (although not as good
as freezing .mpy files into the firmware).
The main trick to make static .mpy files is to adjust the bytecode so any
qstrs that it references now go through a lookup table to convert from
local qstr number in the module to global qstr number in the firmware.
That means the bytecode does not need linking/rewriting of qstrs when it's
loaded. Instead only a small qstr table needs to be built (and put in RAM)
at import time. This means the bytecode itself is static/constant and can
be used directly if it's in addressable memory. Also the qstr string data
in the .mpy file, and some constant object data, can be used directly.
Note that the qstr table is global to the module (ie not per function).
In more detail, in the VM what used to be (schematically):
qst = DECODE_QSTR_VALUE;
is now (schematically):
idx = DECODE_QSTR_INDEX;
qst = qstr_table[idx];
That allows the bytecode to be fixed at compile time and not need
relinking/rewriting of the qstr values. Only qstr_table needs to be linked
when the .mpy is loaded.
Incidentally, this helps to reduce the size of bytecode because what used
to be 2-byte qstr values in the bytecode are now (mostly) 1-byte indices.
If the module uses the same qstr more than two times then the bytecode is
smaller than before.
The following changes are measured for this commit compared to the
previous (the baseline):
- average 7%-9% reduction in size of .mpy files
- frozen code size is reduced by about 5%-7%
- importing .py files uses about 5% less RAM in total
- importing .mpy files uses about 4% less RAM in total
- importing .py and .mpy files takes about the same time as before
The qstr indirection in the bytecode has only a small impact on VM
performance. For stm32 on PYBv1.0 the performance change of this commit
is:
diff of scores (higher is better)
N=100 M=100 baseline -> this-commit diff diff% (error%)
bm_chaos.py 371.07 -> 357.39 : -13.68 = -3.687% (+/-0.02%)
bm_fannkuch.py 78.72 -> 77.49 : -1.23 = -1.563% (+/-0.01%)
bm_fft.py 2591.73 -> 2539.28 : -52.45 = -2.024% (+/-0.00%)
bm_float.py 6034.93 -> 5908.30 : -126.63 = -2.098% (+/-0.01%)
bm_hexiom.py 48.96 -> 47.93 : -1.03 = -2.104% (+/-0.00%)
bm_nqueens.py 4510.63 -> 4459.94 : -50.69 = -1.124% (+/-0.00%)
bm_pidigits.py 650.28 -> 644.96 : -5.32 = -0.818% (+/-0.23%)
core_import_mpy_multi.py 564.77 -> 581.49 : +16.72 = +2.960% (+/-0.01%)
core_import_mpy_single.py 68.67 -> 67.16 : -1.51 = -2.199% (+/-0.01%)
core_qstr.py 64.16 -> 64.12 : -0.04 = -0.062% (+/-0.00%)
core_yield_from.py 362.58 -> 354.50 : -8.08 = -2.228% (+/-0.00%)
misc_aes.py 429.69 -> 405.59 : -24.10 = -5.609% (+/-0.01%)
misc_mandel.py 3485.13 -> 3416.51 : -68.62 = -1.969% (+/-0.00%)
misc_pystone.py 2496.53 -> 2405.56 : -90.97 = -3.644% (+/-0.01%)
misc_raytrace.py 381.47 -> 374.01 : -7.46 = -1.956% (+/-0.01%)
viper_call0.py 576.73 -> 572.49 : -4.24 = -0.735% (+/-0.04%)
viper_call1a.py 550.37 -> 546.21 : -4.16 = -0.756% (+/-0.09%)
viper_call1b.py 438.23 -> 435.68 : -2.55 = -0.582% (+/-0.06%)
viper_call1c.py 442.84 -> 440.04 : -2.80 = -0.632% (+/-0.08%)
viper_call2a.py 536.31 -> 532.35 : -3.96 = -0.738% (+/-0.06%)
viper_call2b.py 382.34 -> 377.07 : -5.27 = -1.378% (+/-0.03%)
And for unix on x64:
diff of scores (higher is better)
N=2000 M=2000 baseline -> this-commit diff diff% (error%)
bm_chaos.py 13594.20 -> 13073.84 : -520.36 = -3.828% (+/-5.44%)
bm_fannkuch.py 60.63 -> 59.58 : -1.05 = -1.732% (+/-3.01%)
bm_fft.py 112009.15 -> 111603.32 : -405.83 = -0.362% (+/-4.03%)
bm_float.py 246202.55 -> 247923.81 : +1721.26 = +0.699% (+/-2.79%)
bm_hexiom.py 615.65 -> 617.21 : +1.56 = +0.253% (+/-1.64%)
bm_nqueens.py 215807.95 -> 215600.96 : -206.99 = -0.096% (+/-3.52%)
bm_pidigits.py 8246.74 -> 8422.82 : +176.08 = +2.135% (+/-3.64%)
misc_aes.py 16133.00 -> 16452.74 : +319.74 = +1.982% (+/-1.50%)
misc_mandel.py 128146.69 -> 130796.43 : +2649.74 = +2.068% (+/-3.18%)
misc_pystone.py 83811.49 -> 83124.85 : -686.64 = -0.819% (+/-1.03%)
misc_raytrace.py 21688.02 -> 21385.10 : -302.92 = -1.397% (+/-3.20%)
The code size change is (firmware with a lot of frozen code benefits the
most):
bare-arm: +396 +0.697%
minimal x86: +1595 +0.979% [incl +32(data)]
unix x64: +2408 +0.470% [incl +800(data)]
unix nanbox: +1396 +0.309% [incl -96(data)]
stm32: -1256 -0.318% PYBV10
cc3200: +288 +0.157%
esp8266: -260 -0.037% GENERIC
esp32: -216 -0.014% GENERIC[incl -1072(data)]
nrf: +116 +0.067% pca10040
rp2: -664 -0.135% PICO
samd: +844 +0.607% ADAFRUIT_ITSYBITSY_M4_EXPRESS
As part of this change the .mpy file format version is bumped to version 6.
And mpy-tool.py has been improved to provide a good visualisation of the
contents of .mpy files.
In summary: this commit changes the bytecode to use qstr indirection, and
reworks the .mpy file format to be simpler and allow .mpy files to be
executed in-place. Performance is not impacted too much. Eventually it
will be possible to store such .mpy files in a linear, read-only, memory-
mappable filesystem so they can be executed from flash/ROM. This will
essentially be able to replace frozen code for most applications.
Signed-off-by: Damien George <damien@micropython.org>
2021-10-22 07:22:47 -04:00
|
|
|
mp_module_context_t context;
|
2018-02-27 00:17:10 -05:00
|
|
|
mp_obj_fun_bc_t fun_bc;
|
py: Rework bytecode and .mpy file format to be mostly static data.
Background: .mpy files are precompiled .py files, built using mpy-cross,
that contain compiled bytecode functions (and can also contain machine
code). The benefit of using an .mpy file over a .py file is that they are
faster to import and take less memory when importing. They are also
smaller on disk.
But the real benefit of .mpy files comes when they are frozen into the
firmware. This is done by loading the .mpy file during compilation of the
firmware and turning it into a set of big C data structures (the job of
mpy-tool.py), which are then compiled and downloaded into the ROM of a
device. These C data structures can be executed in-place, ie directly from
ROM. This makes importing even faster because there is very little to do,
and also means such frozen modules take up much less RAM (because their
bytecode stays in ROM).
The downside of frozen code is that it requires recompiling and reflashing
the entire firmware. This can be a big barrier to entry, slows down
development time, and makes it harder to do OTA updates of frozen code
(because the whole firmware must be updated).
This commit attempts to solve this problem by providing a solution that
sits between loading .mpy files into RAM and freezing them into the
firmware. The .mpy file format has been reworked so that it consists of
data and bytecode which is mostly static and ready to run in-place. If
these new .mpy files are located in flash/ROM which is memory addressable,
the .mpy file can be executed (mostly) in-place.
With this approach there is still a small amount of unpacking and linking
of the .mpy file that needs to be done when it's imported, but it's still
much better than loading an .mpy from disk into RAM (although not as good
as freezing .mpy files into the firmware).
The main trick to make static .mpy files is to adjust the bytecode so any
qstrs that it references now go through a lookup table to convert from
local qstr number in the module to global qstr number in the firmware.
That means the bytecode does not need linking/rewriting of qstrs when it's
loaded. Instead only a small qstr table needs to be built (and put in RAM)
at import time. This means the bytecode itself is static/constant and can
be used directly if it's in addressable memory. Also the qstr string data
in the .mpy file, and some constant object data, can be used directly.
Note that the qstr table is global to the module (ie not per function).
In more detail, in the VM what used to be (schematically):
qst = DECODE_QSTR_VALUE;
is now (schematically):
idx = DECODE_QSTR_INDEX;
qst = qstr_table[idx];
That allows the bytecode to be fixed at compile time and not need
relinking/rewriting of the qstr values. Only qstr_table needs to be linked
when the .mpy is loaded.
Incidentally, this helps to reduce the size of bytecode because what used
to be 2-byte qstr values in the bytecode are now (mostly) 1-byte indices.
If the module uses the same qstr more than two times then the bytecode is
smaller than before.
The following changes are measured for this commit compared to the
previous (the baseline):
- average 7%-9% reduction in size of .mpy files
- frozen code size is reduced by about 5%-7%
- importing .py files uses about 5% less RAM in total
- importing .mpy files uses about 4% less RAM in total
- importing .py and .mpy files takes about the same time as before
The qstr indirection in the bytecode has only a small impact on VM
performance. For stm32 on PYBv1.0 the performance change of this commit
is:
diff of scores (higher is better)
N=100 M=100 baseline -> this-commit diff diff% (error%)
bm_chaos.py 371.07 -> 357.39 : -13.68 = -3.687% (+/-0.02%)
bm_fannkuch.py 78.72 -> 77.49 : -1.23 = -1.563% (+/-0.01%)
bm_fft.py 2591.73 -> 2539.28 : -52.45 = -2.024% (+/-0.00%)
bm_float.py 6034.93 -> 5908.30 : -126.63 = -2.098% (+/-0.01%)
bm_hexiom.py 48.96 -> 47.93 : -1.03 = -2.104% (+/-0.00%)
bm_nqueens.py 4510.63 -> 4459.94 : -50.69 = -1.124% (+/-0.00%)
bm_pidigits.py 650.28 -> 644.96 : -5.32 = -0.818% (+/-0.23%)
core_import_mpy_multi.py 564.77 -> 581.49 : +16.72 = +2.960% (+/-0.01%)
core_import_mpy_single.py 68.67 -> 67.16 : -1.51 = -2.199% (+/-0.01%)
core_qstr.py 64.16 -> 64.12 : -0.04 = -0.062% (+/-0.00%)
core_yield_from.py 362.58 -> 354.50 : -8.08 = -2.228% (+/-0.00%)
misc_aes.py 429.69 -> 405.59 : -24.10 = -5.609% (+/-0.01%)
misc_mandel.py 3485.13 -> 3416.51 : -68.62 = -1.969% (+/-0.00%)
misc_pystone.py 2496.53 -> 2405.56 : -90.97 = -3.644% (+/-0.01%)
misc_raytrace.py 381.47 -> 374.01 : -7.46 = -1.956% (+/-0.01%)
viper_call0.py 576.73 -> 572.49 : -4.24 = -0.735% (+/-0.04%)
viper_call1a.py 550.37 -> 546.21 : -4.16 = -0.756% (+/-0.09%)
viper_call1b.py 438.23 -> 435.68 : -2.55 = -0.582% (+/-0.06%)
viper_call1c.py 442.84 -> 440.04 : -2.80 = -0.632% (+/-0.08%)
viper_call2a.py 536.31 -> 532.35 : -3.96 = -0.738% (+/-0.06%)
viper_call2b.py 382.34 -> 377.07 : -5.27 = -1.378% (+/-0.03%)
And for unix on x64:
diff of scores (higher is better)
N=2000 M=2000 baseline -> this-commit diff diff% (error%)
bm_chaos.py 13594.20 -> 13073.84 : -520.36 = -3.828% (+/-5.44%)
bm_fannkuch.py 60.63 -> 59.58 : -1.05 = -1.732% (+/-3.01%)
bm_fft.py 112009.15 -> 111603.32 : -405.83 = -0.362% (+/-4.03%)
bm_float.py 246202.55 -> 247923.81 : +1721.26 = +0.699% (+/-2.79%)
bm_hexiom.py 615.65 -> 617.21 : +1.56 = +0.253% (+/-1.64%)
bm_nqueens.py 215807.95 -> 215600.96 : -206.99 = -0.096% (+/-3.52%)
bm_pidigits.py 8246.74 -> 8422.82 : +176.08 = +2.135% (+/-3.64%)
misc_aes.py 16133.00 -> 16452.74 : +319.74 = +1.982% (+/-1.50%)
misc_mandel.py 128146.69 -> 130796.43 : +2649.74 = +2.068% (+/-3.18%)
misc_pystone.py 83811.49 -> 83124.85 : -686.64 = -0.819% (+/-1.03%)
misc_raytrace.py 21688.02 -> 21385.10 : -302.92 = -1.397% (+/-3.20%)
The code size change is (firmware with a lot of frozen code benefits the
most):
bare-arm: +396 +0.697%
minimal x86: +1595 +0.979% [incl +32(data)]
unix x64: +2408 +0.470% [incl +800(data)]
unix nanbox: +1396 +0.309% [incl -96(data)]
stm32: -1256 -0.318% PYBV10
cc3200: +288 +0.157%
esp8266: -260 -0.037% GENERIC
esp32: -216 -0.014% GENERIC[incl -1072(data)]
nrf: +116 +0.067% pca10040
rp2: -664 -0.135% PICO
samd: +844 +0.607% ADAFRUIT_ITSYBITSY_M4_EXPRESS
As part of this change the .mpy file format version is bumped to version 6.
And mpy-tool.py has been improved to provide a good visualisation of the
contents of .mpy files.
In summary: this commit changes the bytecode to use qstr indirection, and
reworks the .mpy file format to be simpler and allow .mpy files to be
executed in-place. Performance is not impacted too much. Eventually it
will be possible to store such .mpy files in a linear, read-only, memory-
mappable filesystem so they can be executed from flash/ROM. This will
essentially be able to replace frozen code for most applications.
Signed-off-by: Damien George <damien@micropython.org>
2021-10-22 07:22:47 -04:00
|
|
|
fun_bc.context = &context;
|
|
|
|
fun_bc.child_table = NULL;
|
2021-03-15 09:57:36 -04:00
|
|
|
fun_bc.bytecode = (const byte *)"\x01"; // just needed for n_state
|
2018-02-27 00:17:10 -05:00
|
|
|
mp_code_state_t *code_state = m_new_obj_var(mp_code_state_t, mp_obj_t, 1);
|
|
|
|
code_state->fun_bc = &fun_bc;
|
2021-03-15 09:57:36 -04:00
|
|
|
code_state->ip = (const byte *)"\x00"; // just needed for an invalid opcode
|
2018-02-27 00:17:10 -05:00
|
|
|
code_state->sp = &code_state->state[0];
|
2021-04-23 15:26:42 -04:00
|
|
|
code_state->exc_sp_idx = 0;
|
2018-02-27 00:17:10 -05:00
|
|
|
code_state->old_globals = NULL;
|
|
|
|
mp_vm_return_kind_t ret = mp_execute_bytecode(code_state, MP_OBJ_NULL);
|
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", ret, mp_obj_get_type(code_state->state[0]) == &mp_type_NotImplementedError);
|
|
|
|
}
|
|
|
|
|
2017-03-16 03:05:33 -04:00
|
|
|
// scheduler
|
|
|
|
{
|
|
|
|
mp_printf(&mp_plat_print, "# scheduler\n");
|
|
|
|
|
|
|
|
// lock scheduler
|
|
|
|
mp_sched_lock();
|
|
|
|
|
|
|
|
// schedule multiple callbacks; last one should fail
|
|
|
|
for (int i = 0; i < 5; ++i) {
|
|
|
|
mp_printf(&mp_plat_print, "sched(%d)=%d\n", i, mp_sched_schedule(MP_OBJ_FROM_PTR(&mp_builtin_print_obj), MP_OBJ_NEW_SMALL_INT(i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// test nested locking/unlocking
|
|
|
|
mp_sched_lock();
|
|
|
|
mp_sched_unlock();
|
|
|
|
|
|
|
|
// shouldn't do anything while scheduler is locked
|
2020-02-05 09:05:47 -05:00
|
|
|
mp_handle_pending(true);
|
2017-03-16 03:05:33 -04:00
|
|
|
|
|
|
|
// unlock scheduler
|
|
|
|
mp_sched_unlock();
|
|
|
|
mp_printf(&mp_plat_print, "unlocked\n");
|
|
|
|
|
|
|
|
// drain pending callbacks
|
|
|
|
while (mp_sched_num_pending()) {
|
2020-02-05 09:05:47 -05:00
|
|
|
mp_handle_pending(true);
|
2017-03-16 03:05:33 -04:00
|
|
|
}
|
2020-02-06 04:26:29 -05:00
|
|
|
|
|
|
|
// setting the keyboard interrupt and raising it during mp_handle_pending
|
2021-04-27 20:57:34 -04:00
|
|
|
mp_sched_keyboard_interrupt();
|
2020-02-06 04:26:29 -05:00
|
|
|
nlr_buf_t nlr;
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
|
|
|
mp_handle_pending(true);
|
|
|
|
nlr_pop();
|
|
|
|
} else {
|
|
|
|
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
|
2017-03-16 03:05:33 -04:00
|
|
|
}
|
2020-02-06 04:26:29 -05:00
|
|
|
|
|
|
|
// setting the keyboard interrupt (twice) and cancelling it during mp_handle_pending
|
2021-04-27 20:57:34 -04:00
|
|
|
mp_sched_keyboard_interrupt();
|
|
|
|
mp_sched_keyboard_interrupt();
|
2020-02-06 04:26:29 -05:00
|
|
|
mp_handle_pending(false);
|
|
|
|
|
|
|
|
// setting keyboard interrupt and a pending event (intr should be handled first)
|
|
|
|
mp_sched_schedule(MP_OBJ_FROM_PTR(&mp_builtin_print_obj), MP_OBJ_NEW_SMALL_INT(10));
|
2021-04-27 20:57:34 -04:00
|
|
|
mp_sched_keyboard_interrupt();
|
2020-02-06 04:26:29 -05:00
|
|
|
if (nlr_push(&nlr) == 0) {
|
|
|
|
mp_handle_pending(true);
|
|
|
|
nlr_pop();
|
|
|
|
} else {
|
|
|
|
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
|
|
|
|
}
|
|
|
|
mp_handle_pending(true);
|
2017-03-16 03:05:33 -04:00
|
|
|
}
|
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
// ringbuf
|
|
|
|
{
|
2023-08-14 09:51:52 -04:00
|
|
|
#define RINGBUF_SIZE 99
|
|
|
|
|
|
|
|
byte buf[RINGBUF_SIZE];
|
|
|
|
ringbuf_t ringbuf;
|
|
|
|
ringbuf_init(&ringbuf, &buf[0], sizeof(buf));
|
2021-04-23 15:26:42 -04:00
|
|
|
|
|
|
|
mp_printf(&mp_plat_print, "# ringbuf\n");
|
|
|
|
|
|
|
|
// Single-byte put/get with empty ringbuf.
|
2023-08-14 09:51:52 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", ringbuf_num_empty(&ringbuf), ringbuf_num_filled(&ringbuf));
|
2021-04-23 15:26:42 -04:00
|
|
|
ringbuf_put(&ringbuf, 22);
|
2023-08-14 09:51:52 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", ringbuf_num_empty(&ringbuf), ringbuf_num_filled(&ringbuf));
|
2021-04-23 15:26:42 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", ringbuf_get(&ringbuf));
|
2023-08-14 09:51:52 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", ringbuf_num_empty(&ringbuf), ringbuf_num_filled(&ringbuf));
|
2021-04-23 15:26:42 -04:00
|
|
|
|
|
|
|
// Two-byte put/get with empty ringbuf.
|
|
|
|
ringbuf_put16(&ringbuf, 0xaa55);
|
2023-08-14 09:51:52 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", ringbuf_num_empty(&ringbuf), ringbuf_num_filled(&ringbuf));
|
2021-04-23 15:26:42 -04:00
|
|
|
mp_printf(&mp_plat_print, "%04x\n", ringbuf_get16(&ringbuf));
|
2023-08-14 09:51:52 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", ringbuf_num_empty(&ringbuf), ringbuf_num_filled(&ringbuf));
|
2021-04-23 15:26:42 -04:00
|
|
|
|
|
|
|
// Two-byte put with full ringbuf.
|
2023-08-14 09:51:52 -04:00
|
|
|
for (int i = 0; i < RINGBUF_SIZE; ++i) {
|
2021-04-23 15:26:42 -04:00
|
|
|
ringbuf_put(&ringbuf, i);
|
|
|
|
}
|
2023-08-14 09:51:52 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", ringbuf_num_empty(&ringbuf), ringbuf_num_filled(&ringbuf));
|
2021-04-23 15:26:42 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", ringbuf_put16(&ringbuf, 0x11bb));
|
|
|
|
// Two-byte put with one byte free.
|
|
|
|
ringbuf_get(&ringbuf);
|
2023-08-14 09:51:52 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", ringbuf_num_empty(&ringbuf), ringbuf_num_filled(&ringbuf));
|
2021-04-23 15:26:42 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", ringbuf_put16(&ringbuf, 0x3377));
|
|
|
|
ringbuf_get(&ringbuf);
|
2023-08-14 09:51:52 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", ringbuf_num_empty(&ringbuf), ringbuf_num_filled(&ringbuf));
|
2021-04-23 15:26:42 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", ringbuf_put16(&ringbuf, 0xcc99));
|
2023-08-14 09:51:52 -04:00
|
|
|
for (int i = 0; i < RINGBUF_SIZE - 2; ++i) {
|
2021-04-23 15:26:42 -04:00
|
|
|
ringbuf_get(&ringbuf);
|
|
|
|
}
|
|
|
|
mp_printf(&mp_plat_print, "%04x\n", ringbuf_get16(&ringbuf));
|
2023-08-14 09:51:52 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", ringbuf_num_empty(&ringbuf), ringbuf_num_filled(&ringbuf));
|
2021-04-23 15:26:42 -04:00
|
|
|
|
|
|
|
// Two-byte put with wrap around on first byte:
|
2023-08-14 09:51:52 -04:00
|
|
|
ringbuf_clear(&ringbuf);
|
|
|
|
for (int i = 0; i < RINGBUF_SIZE; ++i) {
|
2021-04-23 15:26:42 -04:00
|
|
|
ringbuf_put(&ringbuf, i);
|
|
|
|
ringbuf_get(&ringbuf);
|
|
|
|
}
|
|
|
|
mp_printf(&mp_plat_print, "%d\n", ringbuf_put16(&ringbuf, 0x11bb));
|
|
|
|
mp_printf(&mp_plat_print, "%04x\n", ringbuf_get16(&ringbuf));
|
|
|
|
|
|
|
|
// Two-byte put with wrap around on second byte:
|
2023-08-14 09:51:52 -04:00
|
|
|
ringbuf_clear(&ringbuf);
|
|
|
|
for (int i = 0; i < RINGBUF_SIZE - 1; ++i) {
|
2021-04-23 15:26:42 -04:00
|
|
|
ringbuf_put(&ringbuf, i);
|
|
|
|
ringbuf_get(&ringbuf);
|
|
|
|
}
|
|
|
|
mp_printf(&mp_plat_print, "%d\n", ringbuf_put16(&ringbuf, 0x22ff));
|
|
|
|
mp_printf(&mp_plat_print, "%04x\n", ringbuf_get16(&ringbuf));
|
|
|
|
|
|
|
|
// Two-byte get from empty ringbuf.
|
2023-08-14 09:51:52 -04:00
|
|
|
ringbuf_clear(&ringbuf);
|
2021-04-23 15:26:42 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", ringbuf_get16(&ringbuf));
|
|
|
|
|
|
|
|
// Two-byte get from ringbuf with one byte available.
|
2023-08-14 09:51:52 -04:00
|
|
|
ringbuf_clear(&ringbuf);
|
2021-04-23 15:26:42 -04:00
|
|
|
ringbuf_put(&ringbuf, 0xaa);
|
|
|
|
mp_printf(&mp_plat_print, "%d\n", ringbuf_get16(&ringbuf));
|
|
|
|
}
|
|
|
|
|
2020-01-13 08:02:01 -05:00
|
|
|
// pairheap
|
|
|
|
{
|
|
|
|
mp_printf(&mp_plat_print, "# pairheap\n");
|
|
|
|
|
|
|
|
// Basic case.
|
|
|
|
int t0[] = {0, 2, 1, 3};
|
|
|
|
pairheap_test(MP_ARRAY_SIZE(t0), t0);
|
|
|
|
|
|
|
|
// All pushed in reverse order.
|
|
|
|
int t1[] = {7, 6, 5, 4, 3, 2, 1, 0};
|
|
|
|
pairheap_test(MP_ARRAY_SIZE(t1), t1);
|
|
|
|
|
|
|
|
// Basic deletion.
|
|
|
|
int t2[] = {1, -1, -1, 1, 2, -2, 2, 3, -3};
|
|
|
|
pairheap_test(MP_ARRAY_SIZE(t2), t2);
|
|
|
|
|
|
|
|
// Deletion of first child that has next node (the -3).
|
|
|
|
int t3[] = {1, 2, 3, 4, -1, -3};
|
|
|
|
pairheap_test(MP_ARRAY_SIZE(t3), t3);
|
|
|
|
|
|
|
|
// Deletion of node that's not first child (the -2).
|
|
|
|
int t4[] = {1, 2, 3, 4, -2};
|
|
|
|
pairheap_test(MP_ARRAY_SIZE(t4), t4);
|
|
|
|
|
|
|
|
// Deletion of node that's not first child and has children (the -3).
|
|
|
|
int t5[] = {3, 4, 5, 1, 2, -3};
|
|
|
|
pairheap_test(MP_ARRAY_SIZE(t5), t5);
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:44:52 -05:00
|
|
|
// mp_obj_is_type and derivatives
|
|
|
|
{
|
|
|
|
mp_printf(&mp_plat_print, "# mp_obj_is_type\n");
|
|
|
|
|
|
|
|
// mp_obj_is_bool accepts only booleans
|
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", mp_obj_is_bool(mp_const_true), mp_obj_is_bool(mp_const_false));
|
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", mp_obj_is_bool(MP_OBJ_NEW_SMALL_INT(1)), mp_obj_is_bool(mp_const_none));
|
|
|
|
|
|
|
|
// mp_obj_is_integer accepts ints and booleans
|
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", mp_obj_is_integer(MP_OBJ_NEW_SMALL_INT(1)), mp_obj_is_integer(mp_obj_new_int_from_ll(1)));
|
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", mp_obj_is_integer(mp_const_true), mp_obj_is_integer(mp_const_false));
|
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", mp_obj_is_integer(mp_obj_new_str("1", 1)), mp_obj_is_integer(mp_const_none));
|
|
|
|
|
|
|
|
// mp_obj_is_int accepts small int and object ints
|
|
|
|
mp_printf(&mp_plat_print, "%d %d\n", mp_obj_is_int(MP_OBJ_NEW_SMALL_INT(1)), mp_obj_is_int(mp_obj_new_int_from_ll(1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_printf(&mp_plat_print, "# end coverage.c\n");
|
|
|
|
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_streamtest_t *s = mp_obj_malloc(mp_obj_streamtest_t, &mp_type_stest_fileio);
|
2017-01-15 23:04:53 -05:00
|
|
|
s->buf = NULL;
|
|
|
|
s->len = 0;
|
|
|
|
s->pos = 0;
|
|
|
|
s->error_code = 0;
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_streamtest_t *s2 = mp_obj_malloc(mp_obj_streamtest_t, &mp_type_stest_textio2);
|
2017-01-15 23:04:53 -05:00
|
|
|
|
2016-09-02 01:07:42 -04:00
|
|
|
// return a tuple of data for testing on the Python side
|
2017-01-15 23:04:53 -05:00
|
|
|
mp_obj_t items[] = {(mp_obj_t)&str_no_hash_obj, (mp_obj_t)&bytes_no_hash_obj, MP_OBJ_FROM_PTR(s), MP_OBJ_FROM_PTR(s2)};
|
2016-09-02 01:07:42 -04:00
|
|
|
return mp_obj_new_tuple(MP_ARRAY_SIZE(items), items);
|
2015-05-07 19:19:56 -04:00
|
|
|
}
|
2015-05-08 04:18:38 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(extra_coverage_obj, extra_coverage);
|
2015-05-07 19:19:56 -04:00
|
|
|
|
|
|
|
#endif
|