summaryrefslogtreecommitdiff
path: root/py
AgeCommit message (Collapse)Author
2017-04-25py/mpz: In mpn_sub, use existing function to remove trailing zeros.Damien George
2017-04-25py/mpz: Strip trailing zeros from mpz value when set from bytes.Damien George
2017-04-22py: Add LOAD_SUPER_METHOD bytecode to allow heap-free super meth calls.Damien George
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
2017-04-22py/compile: Refactor handling of special super() call.Damien George
This patch refactors the handling of the special super() call within the compiler. It removes the need for a global (to the compiler) state variable which keeps track of whether the subject of an expression is super. The handling of super() is now done entirely within one function, which makes the compiler a bit cleaner and allows to easily add more optimisations to super calls. Changes to the code size are: bare-arm: +12 minimal: +0 unix x64: +48 unix nanbox: -16 stmhal: +4 cc3200: +0 esp8266: -56
2017-04-22py/compile: Don't do unnecessary check if iter parse node is a struct.Damien George
If we get to this point in the code then pn_iter is guaranteed to be a struct.
2017-04-22py/compile: Add COMP_RETURN_IF_EXPR option to enable return-if-else opt.Damien George
With this optimisation enabled the compiler optimises the if-else expression within a return statement. The optimisation reduces bytecode size by 2 bytes for each use of such a return-if-else statement. Since such a statement is not often used, and costs bytes for the code, the feature is disabled by default. For example the following code: def f(x): return 1 if x else 2 compiles to this bytecode with the optimisation disabled (left column is bytecode offset in bytes): 00 LOAD_FAST 0 01 POP_JUMP_IF_FALSE 8 04 LOAD_CONST_SMALL_INT 1 05 JUMP 9 08 LOAD_CONST_SMALL_INT 2 09 RETURN_VALUE and to this bytecode with the optimisation enabled: 00 LOAD_FAST 0 01 POP_JUMP_IF_FALSE 6 04 LOAD_CONST_SMALL_INT 1 05 RETURN_VALUE 06 LOAD_CONST_SMALL_INT 2 07 RETURN_VALUE So the JUMP to RETURN_VALUE is optimised and replaced by RETURN_VALUE, saving 2 bytes and making the code a bit faster.
2017-04-22py/compile: Extract parse-node kind at start of func for efficiency.Damien George
Otherwise the type of parse-node and its kind has to be re-extracted multiple times. This optimisation reduces code size by a bit (16 bytes on bare-arm).
2017-04-22py/compile: Don't do unnecessary check if parse node is a struct.Damien George
PN_atom_expr_normal parse nodes always have structs for their second sub-node, so simplify the check for the sub-node kind to save code size.
2017-04-22py/objtype: mp_obj_new_super doesn't need to be public, so inline it.Damien George
Saves code size (20 bytes on bare-arm) and makes it a tiny bit more efficient.
2017-04-21extmod/moductypes: Fix bigint handling for 32-bit ports.Paul Sokolovsky
2017-04-21py: Reduce str/repr precision of float numbers when floats are 30-bit.Damien George
With 30-bit floats there aren't enough bits to faithfully print 7 decimal digits, so reduce the precision to 6 digits.
2017-04-18py/modmicropython: Add micropython.kbd_intr() function.Damien George
It controls the character that's used to (asynchronously) raise a KeyboardInterrupt exception. Passing "-1" allows to disable the interception of the interrupt character (as long as a port allows such a behaviour).
2017-04-12py/gc: Execute finaliser code in a protected environment.Damien George
If a finaliser raises an exception then it must not propagate through the GC sweep function. This patch protects against such a thing by running finaliser code via the mp_call_function_1_protected call. This patch also adds scheduler lock/unlock calls around the finaliser execution to further protect against any possible reentrancy issues: the memory manager is already locked when doing a collection, but we also don't want to allow any scheduled code to run, KeyboardInterrupts to interupt the code, nor threads to switch.
2017-04-12py/nlrsetjmp: Add check for failed NLR jump.Damien George
Also optimise the function so it only needs to call the MP_STATE_THREAD macro once (following how other nlr code is written).
2017-04-12py/objfloat: Add implementation of high-quality float hashing.Damien George
Disabled by default.
2017-04-12py: Optimise types for common case where type has a single parent type.Damien George
The common cases for inheritance are 0 or 1 parent types, for both built-in types (eg built-in exceptions) as well as user defined types. So it makes sense to optimise the case of 1 parent type by storing just the type and not a tuple of 1 value (that value being the single parent type). This patch makes such an optimisation. Even though there is a bit more code to handle the two cases (either a single type or a tuple with 2 or more values) it helps reduce overall code size because it eliminates the need to create a static tuple to hold single parents (eg for the built-in exceptions). It also helps reduce RAM usage for user defined types that only derive from a single parent. Changes in code size (in bytes) due to this patch: bare-arm: -16 minimal (x86): -176 unix (x86-64): -320 unix nanbox: -384 stmhal: -64 cc3200: -32 esp8266: -108
2017-04-12py/obj: Clean up and add comments describing mp_obj_type_t struct.Damien George
2017-04-11py/objint: Use unsigned arithmetic when formatting an integer.Damien George
Otherwise the edge case of the most negative integer value will not convert correctly.
2017-04-11py/objint: Extract small int value directly because type is known.Damien George
2017-04-10py/runtime: When init'ing kbd intr exc, use tuple ptr instead of object.Damien George
2017-04-10py: Make sure that static emg-exc-buffer is aligned to size of mp_obj_t.Damien George
This buffer is used to allocate objects temporarily, and such objects require that their underlying memory be correctly aligned for their data type. Aligning for mp_obj_t should be sufficient for emergency exceptions, but in general the memory buffer should aligned to the maximum alignment of the machine (eg on a 32-bit machine with mp_obj_t being 4 bytes, a double may not be correctly aligned). This patch fixes a bug for certain nan-boxing builds, where mp_obj_t is 8 bytes and must be aligned to 8 bytes (even though the machine is 32 bit).
2017-04-05py/objtuple: Add support for inplace add (same as normal add).Damien George
2017-04-05py: Raise a ValueError if range() step is zero.Damien George
Following CPython. Otherwise one gets either an infinite loop (if code is optimised by the uPy compiler) or possibly a divide-by-zero CPU exception.
2017-04-04py/objint: Consolidate mp_obj_new_int_from_float to one implementation.Damien George
This reduces code duplication and allows to make mp_classify_fp_as_int static, which reduces code size.
2017-04-04py: Add very simple but correct hashing for float and complex numbers.Damien George
Hashing of float and complex numbers that are exact (real) integers should return the same integer hash value as hashing the corresponding integer value. Eg hash(1), hash(1.0) and hash(1+0j) should all be the same (this is how Python is specified: if x==y then hash(x)==hash(y)). This patch implements the simplest way of doing float/complex hashing by just converting the value to int and returning that value.
2017-04-02py/objstr: Use MICROPY_FULL_CHECKS for range checking when constructing bytes.Paul Sokolovsky
Split this setting from MICROPY_CPYTHON_COMPAT. The idea is to be able to keep MICROPY_CPYTHON_COMPAT disabled, but still pass more of regression testsuite. In particular, this fixes last failing test in basics/ for Zephyr port.
2017-04-02py/obj.h: Make sequence grow more efficient and support overlapping.Damien George
The first memmove now copies less bytes in some cases (because len_adj <= slice_len), and the memcpy is replaced with memmove to support the possibility that dest and slice regions are overlapping.
2017-04-01all: Move BYTES_PER_WORD definition from ports to py/mpconfig.hDamien George
It can still be overwritten by a port in mpconfigport.h but for almost all cases one can use the provided default.
2017-03-31all: Use full path name when including mp-readline/timeutils/netutils.Damien George
This follows the pattern of how all other headers are now included, and makes it explicit where the header file comes from. This patch also removes -I options from Makefile's that specify the mp-readline/timeutils/ netutils directories, which are no longer needed.
2017-03-30py/objzip: Convert mp_uint_t to size_t.Damien George
2017-03-30zephyr/Makefile: Rework to use modern, official build integration.Paul Sokolovsky
Build happens in 3 stages: 1. Zephyr config header and make vars are generated from prj.conf. 2. libmicropython is built using them. 3. Zephyr is built and final link happens.
2017-03-29py: Change mp_uint_t to size_t for mp_obj_str_get_data len arg.Damien George
2017-03-29py: Convert mp_uint_t to size_t for tuple/list accessors.Damien George
This patch changes mp_uint_t to size_t for the len argument of the following public facing C functions: mp_obj_tuple_get mp_obj_list_get mp_obj_get_array These functions take a pointer to the len argument (to be filled in by the function) and callers of these functions should update their code so the type of len is changed to size_t. For ports that don't use nan-boxing there should be no change in generate code because the size of the type remains the same (word sized), and in a lot of cases there won't even be a compiler warning if the type remains as mp_uint_t. The reason for this change is to standardise on the use of size_t for variables that count memory (or memory related) sizes/lengths. It helps builds that use nan-boxing.
2017-03-29py/compile: Provide terse error message for invalid dict/set literals.Damien George
2017-03-29py: Shorten a couple of error messages.Damien George
2017-03-29py/compile: Simplify syntax-error messages for illegal assignments.Damien George
With this patch all illegal assignments are reported as "can't assign to expression". Before the patch there were special cases for a literal on the LHS, and for augmented assignments (eg +=), but it seems a waste of bytes (and there are lots of bytes used in error messages) to spend on distinguishing such errors which a user will rarely encounter.
2017-03-29py/lexer: Simplify and reduce code size for operator tokenising.Damien George
By removing the 'E' code from the operator token encoding mini-language the tokenising can be simplified. The 'E' code was only used for the != operator which is now handled as a special case; the optimisations for the general case more than make up for the addition of this single, special case. Furthermore, the . and ... operators can be handled in the same way as != which reduces the code size a little further. This simplification also removes a "goto". Changes in code size for this patch are (measured in bytes): bare-arm: -48 minimal x86: -64 unix x86-64: -112 unix nanbox: -64 stmhal: -48 cc3200: -48 esp8266: -76
2017-03-28py: Use mp_raise_TypeError/mp_raise_ValueError helpers where possible.Damien George
Saves 168 bytes on bare-arm.
2017-03-27py/objmap: Convert mp_uint_t to size_t.Damien George
2017-03-27py/compile: When compiling super(), handle closed-over self variable.Damien George
The self variable may be closed-over in the function, and in that case the call to super() should load the contents of the closure cell using LOAD_DEREF (before this patch it would just load the cell directly).
2017-03-27py/vm: Fix VM opcode tracing to print correct stack pointer.Damien George
Also const_table is now moved to the code_state->fun_bc structure.
2017-03-26py/obj: Change mp_uint_t to size_t for mp_obj_get_array_fixed_n len arg.Damien George
2017-03-26py/nlrx86: Better check for Zephyr (requires 1.7).Paul Sokolovsky
2017-03-25py/objarray: Use mp_obj_str_get_str instead of mp_obj_str_get_data.Damien George
2017-03-24py: Remove MP_STATE_CTX, use MP_STATE_THREAD instead (it's an alias).Damien George
MP_STATE_CTX was recently aliased to MP_STATE_THREAD and can now be removed.
2017-03-24py: Use mp_locals/mp_globals accessor funcs instead of MP_STATE_CTX.Damien George
To improve maintainability of the code.
2017-03-24py/objnamedtuple: Use size_t where appropriate, instead of mp_uint_t.Damien George
2017-03-24py/objtype: Use size_t where appropriate, instead of mp_uint_t or uint.Damien George
2017-03-24py/modbuiltins: Allow round() to return a big int if necessary.Damien George
Previous to this patch, if the result of the round function overflowed a small int, or was inf or nan, then a garbage value was returned. With this patch the correct big-int is returned if necessary and exceptions are raised for inf or nan.
2017-03-24py/modbuiltins: For round() builtin use nearbyint instead of round.Damien George
The C nearbyint function has exactly the semantics that Python's round() requires, whereas C's round() requires extra steps to handle rounding of numbers half way between integers. So using nearbyint reduces code size and potentially eliminates any source of errors in the handling of half-way numbers. Also, bare-metal implementations of nearbyint can be more efficient than round, so further code size is saved (and efficiency improved). nearbyint is provided in the C99 standard so it should be available on all supported platforms.