summaryrefslogtreecommitdiff
path: root/py/emitnative.c
AgeCommit message (Collapse)Author
2021-05-20py/emitnative: Fix x86-64 emitter to generate correct 8/16-bit stores.Damien George
Fixes issue #6643. Signed-off-by: Damien George <damien@micropython.org>
2021-01-29py/emitnative: Support binary ops on ARMv6M without use of ite instr.graham sanderson
2021-01-29py/emitnative: Ensure encoding to load prelude_offset doesn't change sz.Damien George
Based on change made by Graham Sanderson. Signed-off-by: Damien George <damien@micropython.org>
2020-06-27py/emitnative: Implement binary operations for viper uint operands.Damien George
uint types in viper mode can now be used for all binary operators except floor-divide and modulo. Fixes issue #1847 and issue #6177. Signed-off-by: Damien George <damien@micropython.org>
2020-04-05all: Use MP_ERROR_TEXT for all error messages.Jim Mussared
2020-02-28all: Reformat C and Python source code with tools/codeformat.py.Damien George
This is run with uncrustify 0.70.1, and black 19.10b0.
2020-02-28py: Removing dangling "else" to improve code format consistency.Damien George
2020-01-27py/emitnative: Stop after finding an unwind target.Jim Mussared
The loop searches backwards for a target, but doesn't stop after finding the first result, meaning that it'll always end up at the outermost exception handler.
2020-01-27py/emitnative: Use NULL for pending exception (not None).Jim Mussared
This previously made the native emitter incompatible with the bytecode emitter, and mp_resume (and subsequently mp_obj_generator_resume) expects the bytecode emitter behavior (i.e. throw==NULL).
2019-12-12py/nativeglue: Add new header file with native function table typedef.Damien George
2019-11-07py/emitnative: Fix typo, REG_PARENT_ARG_RET should be REG_PARENT_RET.Thea Flowers
2019-10-05py: Add new Xtensa-Windowed arch for native emitter.Damien George
Enabled via the configuration MICROPY_EMIT_XTENSAWIN.
2019-10-05py/emitnative: Add support for archs that cannot read executable data.Damien George
In which case place the native function prelude in a bytes object, linked from the const_table of that function. An architecture should define N_PRELUDE_AS_BYTES_OBJ to 1 before including py/emitnative.c to emit correct machine code, then enable MICROPY_EMIT_NATIVE_PRELUDE_AS_BYTES_OBJ so the runtime can correctly handle the prelude being in a bytes object.
2019-10-05py/emitnative: Add support for using setjmp with native emitter.Damien George
To enable this feature the N_NLR_SETJMP macro should be set to 1 before including py/emitnative.c.
2019-10-05py/emitnative: Add support for archs with windowed registers.Damien George
Such that args/return regs for the parent are different to args/return regs for child calls. For an architecture to use this feature it should define the REG_PARENT_xxx macros before including py/emitnative.c.
2019-10-01py: Rework and compress second part of bytecode prelude.Damien George
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.
2019-10-01py: Compress first part of bytecode prelude.Damien George
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).
2019-10-01py: Add n_state to mp_code_state_t struct.Damien George
This value is used often enough that it is better to cache it instead of decode it each time.
2019-09-26mpy-cross: Set number of registers in nlr_buf_t based on native arch.Damien George
Fixes #5059. Done in collaboration with Jim Mussared.
2019-09-26py/emitnative: Factor sizeof/offsetof calculations to macros.Damien George
2019-05-29py/nativeglue: Remove dependency on mp_fun_table in dyn-compiler mode.Damien George
mpy-cross uses MICROPY_DYNAMIC_COMPILER and MICROPY_EMIT_NATIVE but does not actually need to execute native functions, and does not need mp_fun_table. This commit makes it so mp_fun_table and all its entries are not built when MICROPY_DYNAMIC_COMPILER is enabled, significantly reducing the size of the mpy-cross executable and allowing it to be built on more machines/OS's.
2019-05-06py: remove "if (0)" and "if (false)" branches.Jun Wu
Prior to this commit, building the unix port with `DEBUG=1` and `-finstrument-functions` the compilation would fail with an error like "control reaches end of non-void function". This change fixes this by removing the problematic "if (0)" branches. Not all branches affect compilation, but they are all removed for consistency.
2019-05-03py/native: Improve support for bool type in viper functions.Damien George
Variables with type bool now act more like an int, and there is proper casting to/from Python objects.
2019-03-14py/compile: Add support to select the native emitter at runtime.Damien George
2019-03-14py: Move mp_native_type_from_qstr() from emitnative.c to nativeglue.c.Damien George
2019-03-08py: Add support to save native, viper and asm code to .mpy files.Damien George
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-03-08py/emitnative: Adjust accounting of size of const_table.Damien George
n_obj no longer includes a count for mp_fun_table to make it a bit simpler.
2019-03-08py/emitnative: Provide concentrated points of qstr emit.Damien George
2019-03-08py/emitnative: Consolidate where HASCONSTS is set to load-const-obj fun.Damien George
Simplifies the code and fixes handling of the Ellipsis const in native code generation (which also needs the constant table so must set this flag).
2019-03-08py: Add independent config for debugging sentinel object values.Damien George
The new compile-time option is MICROPY_DEBUG_MP_OBJ_SENTINELS, disabled by default. This is to allow finer control of whether this debugging feature is enabled or not (because, for example, this setting must be the same for mpy-cross and the MicroPython main code when using native code generation).
2019-03-05py: Replace POP_BLOCK and POP_EXCEPT opcodes with POP_EXCEPT_JUMP.Damien George
POP_BLOCK and POP_EXCEPT are now the same, and are always followed by a JUMP. So this optimisation reduces code size, and RAM usage of bytecode by two bytes for each try-except handler.
2019-02-25py: Eliminate warnings about unused arguments when debugging disabled.Damien George
2018-10-15py/emitnative: Put None/False/True in global native const table.Damien George
So these constant objects can be loaded by dereferencing the REG_FUN_TABLE pointer instead of loading immediate values. This reduces the size of generated native code (when such constants are used), and means that pointers to these constants are no longer stored in the assembly code.
2018-10-15py/emitnative: Push internal None rather than const obj where possible.Damien George
This shifts the work of loading the constant None object on to load_reg_stack_imm(), making the handling of None more centralised.
2018-10-15py/emitnative: Simplify viper mode handling in emit_native_import_name.Damien George
2018-10-15py/emitnative: Consolidate use of stacked immediate values to one func.Damien George
This commit adds the helper function load_reg_stack_imm() which deals with constant immediate values and converting them to Python objects if needed.
2018-10-13py/emitnative: Remove unused ptr argument from ASM_CALL_IND macro.Damien George
2018-10-13py/asmthumb: Remove unused fun_ptr arg from asm_thumb_bl_ind function.Damien George
2018-10-13py/asmarm: Simplify asm_arm_bl_ind to only load via index, not literal.Damien George
The maximum index into mp_fun_table is currently less than 1024 and should stay that way to keep things efficient for all architectures, so there is no need to handle loading the pointer directly via a literal in this function.
2018-10-13py/emitnative: Load native fun table ptr from const table for all archs.Damien George
All architectures now have a dedicated register to hold the pointer to the native function table mp_fun_table, and so they all need to load this register at the start of the native function. This commit makes the loading of this register uniform across architectures by passing the pointer in the constant table for the native function, and then loading the register from the constant table. Doing it this way means that the pointer is not stored in the assembly code, helping to make the code more portable.
2018-10-13py/asmx86: Change indirect calls to load fun ptr from the native table.Damien George
Instead of storing the function pointer directly in the assembly code. This makes the generated code more independent of the runtime (so easier to relocate the code), and reduces the generated code size.
2018-10-13py/asmx64: Change indirect calls to load fun ptr from the native table.Damien George
Instead of storing the function pointer directly in the assembly code. This makes the generated code more independent of the runtime (so easier to relocate the code), and reduces the generated code size.
2018-10-02py/emitnative: Clean up unused macro and forward function declarations.Damien George
2018-10-01py/emitnative: Implement yield and yield-from in native emitter.Damien George
This commit adds first class support for yield and yield-from in the native emitter, including send and throw support, and yields enclosed in exception handlers (which requires pulling down the NLR stack before yielding, then rebuilding it when resuming). This has been fully tested and is working on unix x86 and x86-64, and stm32. Also basic tests have been done with the esp8266 port. Performance of existing native code is unchanged.
2018-10-01py/emitnative: Reorder native state on C stack so nlr_buf_t is first.Damien George
The nlr_buf_t doesn't need to be part of the Python value stack (as it was before this commit), it's simpler to have it separated as auxiliary state that lives on the C stack. This will help adding yield support because in that case the nlr_buf_t and Python value stack live in separate memory areas (C stack and heap respectively).
2018-09-28py/emitnative: Change type of const_table from uintptr_t to mp_uint_t.Damien George
This matches how bytecode does it, and matches the signature of mp_emit_glue_assign_native. Since the native emitter doesn't support nan-boxing uintptr_t and mp_uint_t are anyway the same bit-width.
2018-09-27py/emitnative: Place const objs for native code in separate const table.Damien George
This commit changes native code to handle constant objects like bytecode: instead of storing the pointers inside the native code they are now stored in a separate constant table (such pointers include objects like bignum, bytes, and raw code for nested functions). This removes the need for the GC to scan native code for root pointers, and takes a step towards making native code independent of the runtime (eg so it can be compiled offline by mpy-cross). Note that the changes to the struct scope_t did not increase its size: on a 32-bit architecture it is still 48 bytes, and on a 64-bit architecture it decreased from 80 to 72 bytes.
2018-09-16py/asmxtensa: Make indirect calls using func table, not raw pointers.Damien George
Loading a pointer by indexing into the native function table mp_fun_table, rather than loading an immediate value (via a PC-relative load), uses less code space.
2018-09-15py/emitnative: Make viper funcs run with their correct globals context.Damien George
Viper functions will now capture the globals at the point they were defined and use these globals when executing.
2018-09-15py/emitnative: Use macros instead of raw offsetof for slot locations.Damien George
Old globals are now stored in the second slot (ip in mp_code_state_t) to make things simpler for viper.