summaryrefslogtreecommitdiff
path: root/py
AgeCommit message (Collapse)Author
2022-05-17py/bc: Provide separate code-state setup funcs for bytecode and native.Damien George
mpy-cross will now generate native code based on the size of mp_code_state_native_t, and the runtime will use this struct to calculate the offset of the .state field. This makes native code generation and execution (which rely on this struct) independent to the settings MICROPY_STACKLESS and MICROPY_PY_SYS_SETTRACE, both of which change the size of the mp_code_state_t struct. Fixes issue #5059. Signed-off-by: Damien George <damien@micropython.org>
2022-05-17py/asmx64: Support full range of regs in asm_x64_lea_disp_to_r64.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-05-17py/persistentcode: Remove unicode feature flag from .mpy file.Damien George
Prior to this commit, even with unicode disabled .py and .mpy files could contain unicode characters, eg by entering them directly in a string as utf-8 encoded. The only thing the compiler disallowed (with unicode disabled) was using \uxxxx and \Uxxxxxxxx notation to specify a character within a string with value >= 0x100; that would give a SyntaxError. With this change mpy-cross will now accept \u and \U notation to insert a character with value >= 0x100 into a string (because the -mno-unicode option is now gone, there's no way to forbid this). The runtime will happily work with strings with such characters, just like it already works with strings with characters that were utf-8 encoded directly. This change simplifies things because there are no longer any feature flags in .mpy files, and any bytecode .mpy will now run on any target. Signed-off-by: Damien George <damien@micropython.org>
2022-05-17py/persistentcode: Remove obsolete comment about qstr window size.Damien George
This was made obsolete in f2040bfc7ee033e48acef9f289790f3b4e6b74e5 Signed-off-by: Damien George <damien@micropython.org>
2022-05-05all: Reformat remaining C code that doesn't have a space after a comma.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-05-05py/objmodule: Move stray #include to top of file.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-05-05py/malloc: Introduce m_tracked_calloc, m_tracked_free functions.Damien George
Enabled by MICROPY_TRACKED_ALLOC. Signed-off-by: Damien George <damien@micropython.org>
2022-05-03all: Use mp_obj_malloc everywhere it's applicable.Jim Mussared
This replaces occurences of foo_t *foo = m_new_obj(foo_t); foo->base.type = &foo_type; with foo_t *foo = mp_obj_malloc(foo_t, &foo_type); Excludes any places where base is a sub-field or when new0/memset is used. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-05-03py/objfloat: Explain why mp_obj_malloc isn't used.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-05-03py/obj: Introduce mp_obj_malloc macro to allocate, and set object type.Jim Mussared
This is to replace the following: mp_foo_obj_t *self = m_new_obj(mp_foo_obj_t); self->base.type = &mp_type_foo; with: mp_foo_obj_t *self = mp_obj_malloc(mp_foo_obj_t, &mp_type_foo); Calling the function is less code than inlining setting the type everywhere, adds up to ~100 bytes on PYBV11. It also helps to avoid an easy mistake of forgetting to set the type. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-05-03py/emitcommon: Don't implicitly close class vars that are assigned to.Damien George
When in a class body or at the module level don't implicitly close over variables that have been assigned to. Fixes issue #8603. Signed-off-by: Damien George <damien@micropython.org>
2022-04-28py/modsys: Introduce sys.implementation._machine constant.Damien George
This contains a string useful for identifying the underlying machine. This string is kept consistent with the second part of the REPL banner via the new config option MICROPY_BANNER_MACHINE. This makes os.uname() more or less redundant, as all the information in os.uname() is now available in the sys module. Signed-off-by: Damien George <damien@micropython.org>
2022-04-28py/modsys: Rename sys.implementation.mpy to sys.implementation._mpy.Damien George
Per CPython docs, non-standard attributes must begin with an underscore. Signed-off-by: Damien George <damien@micropython.org>
2022-04-28py/modsys: Append MicroPython git version and build date to sys.version.Damien George
This commit adds the git hash and build date to sys.version. This is allowed according to CPython docs, and is what PyPy does. The docs state: A string containing the version number of the Python interpreter plus additional information on the build number and compiler used. Eg on CPython: Python 3.10.4 (main, Mar 23 2022, 23:05:40) [GCC 11.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.version '3.10.4 (main, Mar 23 2022, 23:05:40) [GCC 11.2.0]' and PyPy: Python 2.7.12 (5.6.0+dfsg-4, Nov 20 2016, 10:43:30) [PyPy 5.6.0 with GCC 6.2.0 20161109] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>> import sys >>>> sys.version '2.7.12 (5.6.0+dfsg-4, Nov 20 2016, 10:43:30)\n[PyPy 5.6.0 with GCC ... With this commit on MicroPython we now have: MicroPython v1.18-371-g9d08eb024 on 2022-04-28; linux [GCC 11.2.0] v... Use Ctrl-D to exit, Ctrl-E for paste mode >>> import sys >>> sys.version '3.4.0; MicroPython v1.18-371-g9d08eb024 on 2022-04-28' Note that the start of the banner is the same as the end of sys.version. This helps to keep code size under control because the string can be reused by the compiler. Signed-off-by: Damien George <damien@micropython.org>
2022-04-22shared/readline: Make tab insert an indent when it follows whitespace.Damien George
Entering tab at the REPL will now make it insert an indent (4 spaces) in the following cases: - after any whitespace on a line - at the start of a line that is not the first line This changes the existing behaviour where a tab would insert an indent only if there were no matches in the auto-complete search, and it was the start of the line. This means, if there were any symbols in the global namespace, tab could never be used to indent. Note that entering tab at the start of the first line will still do auto-completion, but will now do nothing if there are no symbols in the global namespace, which is more consistent than before. Signed-off-by: Damien George <damien@micropython.org>
2022-04-20py/objtype: Convert result of user __contains__ method to bool.Jon Bjarni Bjarnason
Per https://docs.python.org/3/reference/expressions.html#membership-test-operations For user-defined classes which define the contains() method, x in y returns True if y.contains(x) returns a true value, and False otherwise. Fixes issue #7884.
2022-04-14py/persistentcode: Support loading and saving tuples in .mpy files.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-04-14py/parse: Add MICROPY_COMP_CONST_TUPLE option to build const tuples.Damien George
This commit adds support to the parser so that tuples which contain only constant elements (bool, int, str, bytes, etc) are immediately converted to a tuple object. This makes it more efficient to use tuples containing constant data because they no longer need to be created at runtime by the bytecode (or native code). Furthermore, with this improvement constant tuples that are part of frozen code are now able to be stored fully in ROM (this will be implemented in later commits). Code size is increased by about 400 bytes on Cortex-M4 platforms. See related issue #722. Signed-off-by: Damien George <damien@micropython.org>
2022-04-14py/parse: Print const object value in mp_parse_node_print.Damien George
To give more information when printing the parse tree. Signed-off-by: Damien George <damien@micropython.org>
2022-04-14py/parse: Factor obj extract code to mp_parse_node_extract_const_object.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-04-14py/persistentcode: Define enum values for obj types instead of letters.Damien George
To keep the separate parts of the code that use these values in sync. And make it easier to add new object types. Signed-off-by: Damien George <damien@micropython.org>
2022-04-14Revert "py/emitnative: Don't store prelude at end of machine code if..."Damien George
This reverts commit 7e8222ae063d78ae8f901bb0f9fef663edd2edb6. The prelude data must exist somewhere in the native code so load_raw_code and mpy-tool.py can access and parse it. Signed-off-by: Damien George <damien@micropython.org>
2022-04-14py/scheduler: Add support for scheduling static C-based callbacks.Damien George
If MICROPY_SCHEDULER_STATIC_NODES is enabled then C code can declare a static mp_sched_node_t and schedule a callback using mp_sched_schedule_node(). In contrast to using mp_sched_schedule(), the node version will have at most one pending callback outstanding, and will always be able to schedule if there is nothing already scheduled on this node. This guarantees that the the callback will be called exactly once after it is scheduled. Signed-off-by: Damien George <damien@micropython.org>
2022-04-11py/emitinlinethumb: Use 16 bit encodings for PUSH LR and POP PC.Christian Zietz
The Thumb instruction set has special 16 bit encodings for PUSH involving LR and POP involving PC, which are commonly used in nested functions. Using this encoding is particularly important for ARMv6-M, where the more general 32 bit encoding of PUSH and POP is unavailable.
2022-04-11extmod/modusocket: Provide config macro for socket.listen backlog deflt.Damien George
To make it possible to change this value for any given port or board. Signed-off-by: Damien George <damien@micropython.org>
2022-04-01py/makeqstrdefs: Cleanup and extend source file classification.Daniel Jour
- The classification of source files in makeqstrdefs.py has been moved into functions to consolidate the logic for that classification into a single place. - Classification of source files (into C or C++ or "other" files) is based on the filename extension. - For C++ there are many more common filename extensions than just ".cpp"; see "Options Controlling the Kind of Output" in man gcc for example. All common extensions for C++ source files which need preprocessing have been added.
2022-04-01py/runtime: Remove unnecessary check for kw_value == MP_OBJ_NULL.Damien George
The values are always real objects, only the key can be MP_OBJ_NULL to indicate a **kwargs entry. Signed-off-by: Damien George <damien@micropython.org>
2022-04-01py: Fix compiling and decoding of *args at large arg positions.Damien George
There were two issues with the existing code: 1. "1 << i" is computed as a 32-bit number so would overflow when executed on 64-bit machines (when mp_uint_t is 64-bit). This meant that *args beyond 32 positions would not be handled correctly. 2. star_args must fit as a positive small int so that it is encoded correctly in the emitted code. MP_SMALL_INT_BITS is too big because it overflows a small int by 1 bit. MP_SMALL_INT_BITS - 1 does not work because it produces a signed small int which is then sign extended when extracted (even by mp_obj_get_int_truncated), and this sign extension means that any position arg after *args is also treated as a star-arg. So the maximum bit position is MP_SMALL_INT_BITS - 2. This means that MP_OBJ_SMALL_INT_VALUE() can be used instead of mp_obj_get_int_truncated() to get the value of star_args. These issues are fixed by this commit, and a test added. Signed-off-by: Damien George <damien@micropython.org>
2022-03-31py/emitbc: Assert that a small int fits its encoding when emitting one.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-03-31py/runtime: Use size_t/ssize_t instead of uint/int.David Lechner
This replaces instances of uint with size_t and int with ssize_t in the mp_call_prepare_args_n_kw_var() function since all of the variables are used as array offsets. Also sort headers while we are touching this. Signed-off-by: David Lechner <david@pybricks.com>
2022-03-31py/runtime: Drop new_alloc < 4 check.David Lechner
To reach this check, n_kw has to be >= 1 and therefore args2_alloc has to be >= 2. Therefore new_alloc will always be >= 4. So this check will never be true and can be removed. Signed-off-by: David Lechner <david@pybricks.com>
2022-03-31py/runtime: Do not overallocate when len is known.David Lechner
This fixes overallocating an extra mp_obj_t when the length of *args and **args is known. Previously we were allocating 1 mp_obj_t for each n_args and n_kw plus the length of each *arg and **arg (if they are known). Since n_args includes *args and n_kw includes **args, this was allocating an extra mp_obj_t in addition to the length of these args when unpacked. To fix this, we just subtract 1 from the length to account for the 1 already implicitly allocated by n_args and n_kw. Signed-off-by: David Lechner <david@pybricks.com>
2022-03-31py/runtime: Allow multiple *args in a function call.David Lechner
This is a partial implementation of PEP 448 to allow unpacking multiple star args in a function or method call. This is implemented by changing the emitted bytecodes so that both positional args and star args are stored as positional args. A bitmap is added to indicate if an argument at a given position is a positional argument or a star arg. In the generated code, this new bitmap takes the place of the old star arg. It is stored as a small int, so this means only the first N arguments can be star args where N is the number of bits in a small int. The runtime is modified to interpret this new bytecode format while still trying to perform as few memory reallocations as possible. Signed-off-by: David Lechner <david@pybricks.com>
2022-03-31py/runtime: Allow multiple **args in a function call.David Lechner
This is a partial implementation of PEP 448 to allow multiple ** unpackings when calling a function or method. The compiler is modified to encode the argument as a None: obj key-value pair (similar to how regular keyword arguments are encoded as str: obj pairs). The extra object that was pushed on the stack to hold a single ** unpacking object is no longer used and is removed. The runtime is modified to decode this new format. Signed-off-by: David Lechner <david@pybricks.com>
2022-03-31py/vm: Prevent array bound warning when using -MP_OBJ_ITER_BUF_NSLOTS.Damien George
This warning can happen on clang 13.0.1 building mpy-cross: ../py/vm.c:748:25: error: array index -3 refers past the last possible element for an array in 64-bit address space containing 64-bit (8-byte) elements (max possible 2305843009213693952 elements) [-Werror,-Warray-bounds] sp[-MP_OBJ_ITER_BUF_NSLOTS + 1] = MP_OBJ_NULL; ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Using pointer access instead of array access works around this warning. Fixes issue #8467. Signed-off-by: Damien George <damien@micropython.org>
2022-03-30py/emitnative: Don't store prelude at end of machine code if not needed.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-03-30py/asmxtensa: Fix use of l32i/s32i when offset won't fit in encoding.Damien George
This commit adds optimised l32i/s32i functions that select the best load/ store encoding based on the size of the offset, and uses the function when necessary in code generation. Without this, ASM_LOAD_REG_REG_OFFSET() could overflow the word offset (using a narrow encoding), for example when loading the prelude from the constant table when there are many (>16) constants. Fixes issue #8458. Signed-off-by: Damien George <damien@micropython.org>
2022-03-30py/compile: Only show raw code that is bytecode.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-03-30py/bc.h: Fix C++20 compilation with "volatile".stijn
C++20 is deprecating several usages of the volatile keyword so remove this one affected case in the codebase which causes such warning.
2022-03-28py/builtinimport: Alias sys to usys if import weak links aren't enabled.Damien George
The sys module should always be available (if it's compiled in), eg to change sys.path for importing. So provide an explicit alias from "sys" to "usys" so that "import sys" can always work. Signed-off-by: Damien George <damien@micropython.org>
2022-03-28py: Change jump-if-x-or-pop opcodes to have unsigned offset argument.Damien George
These jumps are always forwards, and it's more efficient in the VM to decode an unsigned argument. These opcodes are already optimised versions of the sequence "dup-top pop-jump-if-x pop" so it doesn't hurt generality to optimise them further. Signed-off-by: Damien George <damien@micropython.org>
2022-03-28py/emitbc: Add check for bytecode jump offset overflow.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-03-28py: Change jump opcodes to emit 1-byte jump offset when possible.Damien George
This commit introduces changes: - All jump opcodes are changed to have variable length arguments, of either 1 or 2 bytes (previously they were fixed at 2 bytes). In most cases only 1 byte is needed to encode the short jump offset, saving bytecode size. - The bytecode emitter now selects 1 byte jump arguments when the jump offset is guaranteed to fit in 1 byte. This is achieved by checking if the code size changed during the last pass and, if it did (if it shrank), then requesting that the compiler make another pass to get the correct offsets of the now-smaller code. This can continue multiple times until the code stabilises. The code can only ever shrink so this iteration is guaranteed to complete. In most cases no extra passes are needed, the original 4 passes are enough to get it right by the 4th pass (because the 2nd pass computes roughly the correct labels and the 3rd pass computes the correct size for the jump argument). This change to the jump opcode encoding reduces .mpy files and RAM usage (when bytecode is in RAM) by about 2% on average. The performance of the VM is not impacted, at least within measurment of the performance benchmark suite. Code size is reduced for builds that include a decent amount of frozen bytecode. ARM Cortex-M builds without any frozen code increase by about 350 bytes. Signed-off-by: Damien George <damien@micropython.org>
2022-03-25py/objgenerator: Fix unused variables when native gen extracts prelude.Damien George
Some compilers will warn about unused variables like scope_flags. So use MP_BC_PRELUDE_SIG_DECODE() which will silence these warnings. Signed-off-by: Damien George <damien@micropython.org>
2022-03-25py/smallint: Introduce MP_SMALL_INT_BITS macro.David Lechner
This adds a new MP_SMALL_INT_BITS macro that is a compile-time constant that contains the number of bits available in an MP_SMALL_INT. We can use this in place of the runtime function mp_small_int_bits(). Signed-off-by: David Lechner <david@pybricks.com>
2022-03-16py/showbc: Remove global variables and make DECODE_PTR work correctly.Damien George
The bytecode state variables mp_showbc_code_start and mp_showbc_constants have been removed and made local variables passed into the various functions. As part of this, the DECODE_PTR macro is fixed so it extracts the relevant pointer from the child_table (a regression introduced in f2040bfc7ee033e48acef9f289790f3b4e6b74e5). Signed-off-by: Damien George <damien@micropython.org>
2022-03-16py/parse: Handle check for target small-int size in parser.Damien George
This means that all constants for EMIT_ARG(load_const_obj, obj) are created in the parser (rather than some in the compiler). Signed-off-by: Damien George <damien@micropython.org>
2022-03-16py/parse: Put const bytes objects in parse tree as const object.Damien George
Instead of as an intermediate qstr, which may unnecessarily intern the data of the bytes object. Signed-off-by: Damien George <damien@micropython.org>
2022-03-16py/parse: Simplify handling of const int parse nodes.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-03-10py/modsys: Add optional mutable attributes sys.ps1/ps2 and use them.Damien George
This allows customising the REPL prompt strings. Signed-off-by: Damien George <damien@micropython.org>