summaryrefslogtreecommitdiffstatshomepage
path: root/py/persistentcode.c
Commit message (Collapse)AuthorAge
* all: Rename BYTES_PER_WORD to MP_BYTES_PER_OBJ_WORD.Damien George2021-02-04
| | | | | | | | | | | | | The "word" referred to by BYTES_PER_WORD is actually the size of mp_obj_t which is not always the same as the size of a pointer on the target architecture. So rename this config value to better reflect what it measures, and also prefix it with MP_. For uses of BYTES_PER_WORD in setting the stack limit this has been changed to sizeof(void *), because the stack usually grows with machine-word sized values (eg an nlr_buf_t has many machine words in it). Signed-off-by: Damien George <damien@micropython.org>
* py/persistentcode: Introduce MICROPY_PERSISTENT_CODE_SAVE_FILE option.David CARLIER2021-01-30
| | | | | | | This should be enabled when the mp_raw_code_save_file function is needed. It is enabled for mpy-cross, and a check for defined(__APPLE__) is added to cover Mac M1 systems.
* py/persistentcode: Maintain root ptr list of imported native .mpy code.Damien George2020-08-02
| | | | | | | | | | | | | | | | | | | | | | | On ports where normal heap memory can contain executable code (eg ARM-based ports such as stm32), native code loaded from an .mpy file may be reclaimed by the GC because there's no reference to the very start of the native machine code block that is reachable from root pointers (only pointers to internal parts of the machine code block are reachable, but that doesn't help the GC find the memory). This commit fixes this issue by maintaining an explicit list of root pointers pointing to native code that is loaded from an .mpy file. This is not needed for all ports so is selectable by the new configuration option MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE. It's enabled by default if a port does not specify any special functions to allocate or commit executable memory. A test is included to test that native code loaded from an .mpy file does not get reclaimed by the GC. Fixes #6045. Signed-off-by: Damien George <damien@micropython.org>
* all: Use MP_ERROR_TEXT for all error messages.Jim Mussared2020-04-05
|
* all: Reformat C and Python source code with tools/codeformat.py.Damien George2020-02-28
| | | | This is run with uncrustify 0.70.1, and black 19.10b0.
* py: Release GIL during syscalls in reader and writer code.David Lechner2020-01-26
| | | | This releases the GIL during POSIX system calls that could block.
* py/persistentcode: Move loading of rodata/bss to before obj/raw-code.Damien George2019-12-17
| | | | | | This makes the loading of viper-code-with-relocations a bit neater and easier to understand, by treating the rodata/bss like a special object to be loaded into the constant table (which is how it behaves).
* py/persistentcode: Make ARM Thumb archs support multiple sub-archs.Damien George2019-12-12
|
* py/nativeglue: Add new header file with native function table typedef.Damien George2019-12-12
|
* py/persistentcode: Add ability to relocate loaded native code.Damien George2019-12-12
| | | | | | Implements text, rodata and bss generalised relocations, as well as generic qstr-object linking. This allows importing dynamic native modules on all supported architectures in a unified way.
* py/persistentcode: Move declarations for .mpy header from .c to .h file.Damien George2019-11-04
|
* py/persistentcode: Make .mpy more compact with qstr directly in prelude.Damien George2019-10-15
| | | | | | | | | Instead of encoding 4 zero bytes as placeholders for the simple_name and source_file qstrs, and storing the qstrs after the bytecode, store the qstrs at the location of these 4 bytes. This saves 4 bytes per bytecode function stored in a .mpy file (for example lcd160cr.mpy drops by 232 bytes, 4x 58 functions). And resulting code size is slightly reduced on ports that use this feature.
* py: Add new Xtensa-Windowed arch for native emitter.Damien George2019-10-05
| | | | Enabled via the configuration MICROPY_EMIT_XTENSAWIN.
* py: Rework and compress second part of bytecode prelude.Damien George2019-10-01
| | | | | | | | | | | | | | | This patch compresses the second part of the bytecode prelude which contains the source file name, function name, source-line-number mapping and cell closure information. This part of the prelude now begins with a single varible length unsigned integer which encodes 2 numbers, being the byte-size of the following 2 sections in the header: the "source info section" and the "closure section". After decoding this variable unsigned integer it's possible to skip over one or both of these sections very easily. This scheme saves about 2 bytes for most functions compared to the original format: one in the case that there are no closure cells, and one because padding was eliminated.
* py: Compress first part of bytecode prelude.Damien George2019-10-01
| | | | | | | | | | | | | | | | | | | | | | | The start of the bytecode prelude contains 6 numbers telling the amount of stack needed for the Python values and exceptions, and the signature of the function. Prior to this patch these numbers were all encoded one after the other (2x variable unsigned integers, then 4x bytes), but using so many bytes is unnecessary. An entropy analysis of around 150,000 bytecode functions from the CPython standard library showed that the optimal Shannon coding would need about 7.1 bits on average to encode these 6 numbers, compared to the existing 48 bits. This patch attempts to get close to this optimal value by packing the 6 numbers into a single, varible-length unsigned integer via bit-wise interleaving. The interleaving scheme is chosen to minimise the average number of bytes needed, and at the same time keep the scheme simple enough so it can be implemented without too much overhead in code size or speed. The scheme requires about 10.5 bits on average to store the 6 numbers. As a result most functions which originally took 6 bytes to encode these 6 numbers now need only 1 byte (in 80% of cases).
* py/bc: Replace big opcode format table with simple macro.Damien George2019-09-26
|
* py/persistentcode: Enable persistent code saving for Windows ports.stijn2019-09-18
|
* py/persistentcode: Ensure prelude_offset is always initialised.Paul m. p. P2019-07-01
|
* py/persistentcode: Fix compilation with load and save both enabled.Jun Wu2019-06-28
| | | | | | | | | | | | With both MICROPY_PERSISTENT_CODE_SAVE and MICROPY_PERSISTENT_CODE_LOAD enabled the code fails to compile, due to undeclared 'n_obj'. If MICROPY_EMIT_NATIVE is disabled there are more errors due to the use of undefined fields in mp_raw_code_t. This patch fixes such compilation by avoiding undefined fields. MICROPY_EMIT_NATIVE was changed to MICROPY_EMIT_MACHINE_CODE in this file to match the mp_raw_code_t definition.
* py/persistentcode: Change "len" type to size_t for mp_obj_str_get_data.Henrik Vendelbo2019-05-13
|
* mpy-cross: Add --version command line option to print version info.Damien George2019-05-07
| | | | | | Prints something like: MicroPython v1.10-304-g8031b7a25 on 2019-05-02; mpy-cross emitting mpy v4
* py/compile: Add support to select the native emitter at runtime.Damien George2019-03-14
|
* py/persistentcode: Bump .mpy version to 4.Damien George2019-03-08
|
* py: Add support to save native, viper and asm code to .mpy files.Damien George2019-03-08
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* py/emitglue: Remove union in mp_raw_code_t to combine bytecode & native.Damien George2019-03-08
|
* py/persistentcode: Define static qstr set to reduce size of mpy files.Damien George2019-03-05
| | | | | | | | When encoded in the mpy file, if qstr <= QSTR_LAST_STATIC then store two bytes: 0, static_qstr_id. Otherwise encode the qstr as usual (either with string data or a reference into the qstr window). Reduces mpy file size by about 5%.
* py/persistentcode: Pack qstrs directly in bytecode to reduce mpy size.Damien George2019-03-05
| | | | | | | | Instead of emitting two bytes in the bytecode for where the linked qstr should be written to, it is now replaced by the actual qstr data, or a reference into the qstr window. Reduces mpy file size by about 10%.
* py/persistentcode: Add a qstr window to save mpy files more efficiently.Damien George2019-03-05
| | | | | | | | | | | | | This is an implementation of a sliding qstr window used to reduce the number of qstrs stored in a .mpy file. The window size is configured to 32 entries which takes a fixed 64 bytes (16-bits each) on the C stack when loading/saving a .mpy file. It allows to remember the most recent 32 qstrs so they don't need to be stored again in the .mpy file. The qstr window uses a simple least-recently-used mechanism to discard the least recently used qstr when the window overflows (similar to dictionary compression). This scheme only needs a single pass to save/load the .mpy file. Reduces mpy file size by about 25% with a window size of 32.
* py: Downcase all MP_OBJ_IS_xxx macros to make a more consistent C API.Damien George2019-02-12
| | | | | | | | | | | | | | | | | | | | | These macros could in principle be (inline) functions so it makes sense to have them lower case, to match the other C API functions. The remaining macros that are upper case are: - MP_OBJ_TO_PTR, MP_OBJ_FROM_PTR - MP_OBJ_NEW_SMALL_INT, MP_OBJ_SMALL_INT_VALUE - MP_OBJ_NEW_QSTR, MP_OBJ_QSTR_VALUE - MP_OBJ_FUN_MAKE_SIG - MP_DECLARE_CONST_xxx - MP_DEFINE_CONST_xxx These must remain macros because they are used when defining const data (at least, MP_OBJ_NEW_SMALL_INT is so it makes sense to have MP_OBJ_SMALL_INT_VALUE also a macro). For those macros that have been made lower case, compatibility macros are provided for the old names so that users do not need to change their code immediately.
* py: Remove calls to file reader functions when these are disabled.Sean Burton2019-01-27
| | | | | | | | | | | If MICROPY_PERSISTENT_CODE_LOAD or MICROPY_ENABLE_COMPILER are enabled then code gets enabled that calls file reading functions which may be disabled if no readers have been implemented. To fix this, introduce a MICROPY_HAS_FILE_READER variable, which is automatically set if MICROPY_READER_POSIX or MICROPY_READER_VFS is set but can also be manually set if a custom reader is being implemented. Then disable the file reading calls if this is not set.
* py/emitglue: When assigning bytecode only pass bytecode len if needed.Damien George2018-02-14
| | | | | Most embedded targets will have this bit of the code disabled, saving a small amount of code space.
* py/persistentcode: Bump .mpy version number to version 3.Damien George2017-10-05
| | | | The binary and unary ops have changed bytecode encoding.
* all: Remove inclusion of internal py header files.Damien George2017-10-04
| | | | | | | | | | | | | | | | Header files that are considered internal to the py core and should not normally be included directly are: py/nlr.h - internal nlr configuration and declarations py/bc0.h - contains bytecode macro definitions py/runtime0.h - contains basic runtime enums Instead, the top-level header files to include are one of: py/obj.h - includes runtime0.h and defines everything to use the mp_obj_t type py/runtime.h - includes mpstate.h and hence nlr.h, obj.h, runtime0.h, and defines everything to use the general runtime support functions Additional, specific headers (eg py/objlist.h) can be included if needed.
* py/persistentcode: Define mp_raw_code_save_file() for any unix target.Anton Patrushev2017-09-25
| | | | | A unix target should provide POSIX open/write/close functions regardless of its machine architecture. Fixes issue #3325.
* py/persistentcode: Allow to compile with complex numbers disabled.Damien George2017-06-08
|
* py: Add LOAD_SUPER_METHOD bytecode to allow heap-free super meth calls.Damien George2017-04-22
| | | | | | | | | | | | | | | | | | | | | | This patch allows the following code to run without allocating on the heap: super().foo(...) Before this patch such a call would allocate a super object on the heap and then load the foo method and call it right away. The super object is only needed to perform the lookup of the method and not needed after that. This patch makes an optimisation to allocate the super object on the C stack and discard it right after use. Changes in code size due to this patch are: bare-arm: +128 minimal: +232 unix x64: +416 unix nanbox: +364 stmhal: +184 esp8266: +340 cc3200: +128
* py: Allow lexer to raise exceptions during construction.Damien George2017-03-14
| | | | | | | | | | | | | | | | | | | | | | | | This patch refactors the error handling in the lexer, to simplify it (ie reduce code size). A long time ago, when the lexer/parser/compiler were first written, the lexer and parser were designed so they didn't use exceptions (ie nlr) to report errors but rather returned an error code. Over time that has gradually changed, the parser in particular has more and more ways of raising exceptions. Also, the lexer never really handled all errors without raising, eg there were some memory errors which could raise an exception (and in these rare cases one would get a fatal nlr-not-handled fault). This patch accepts the fact that the lexer can raise exceptions in some cases and allows it to raise exceptions to handle all its errors, which are for the most part just out-of-memory errors during construction of the lexer. This makes the lexer a bit simpler, and also the persistent code stuff is simplified. What this means for users of the lexer is that calls to it must be wrapped in a nlr handler. But all uses of the lexer already have such an nlr handler for the parser (and compiler) so that doesn't put any extra burden on the callers.
* py/persistentcode: Bump .mpy version due to change in bytecode.Damien George2017-02-17
|
* py/persistentcode: Replace mp_uint_t with size_t where appropriate.Damien George2017-02-16
|
* py: Factor out persistent-code reader into separate files.Damien George2016-11-16
| | | | | | | Implementations of persistent-code reader are provided for POSIX systems and systems using FatFS. Macros to use these are MICROPY_READER_POSIX and MICROPY_READER_FATFS respectively. If an alternative implementation is needed then a port can define the function mp_reader_new_file.
* py: Factor persistent code load/save funcs into persistentcode.[ch].Damien George2016-11-16