summaryrefslogtreecommitdiff
path: root/py
AgeCommit message (Collapse)Author
2018-09-29py/vm: When VM raises exception put exc obj at beginning of func state.Damien George
Instead of at end of state, n_state - 1. It was originally (way back in v1.0) put at the end of the state because the VM didn't have a pointer to the start. But now that the VM takes a mp_code_state_t pointer it does have a pointer to the start of the state so can put the exception object there. This commit saves about 30 bytes of code on all architectures, and, more importantly, reduces C-stack usage by a couple of words (8 bytes on Thumb2 and 16 bytes on x86-64) for every (non-generator) call of a bytecode function because fun_bc_call no longer needs to remember the n_state variable.
2018-09-28py/objtype: Support full object model for get/set/delitem special meths.Damien George
This makes these special methods have the same calling behaviour as other methods in a class instance (mp_convert_member_lookup() is already called by mp_obj_class_lookup()).
2018-09-28py/objtype: Remove TODO about storing attributes to classes.Damien George
This behaviour is tested in basics/class_store.py and follows CPython.
2018-09-28py/runtime: Remove nlr protection when calling __next__ in mp_resume.Damien George
And remove related comment about needing such protection when calling send. Reasoning for removal is as follows: - mp_resume is only called by the VM in YIELD_FROM opcode - if send_value != MP_OBJ_NULL then throw_value == MP_OBJ_NULL - so if __next__ or send are called then throw_value == MP_OBJ_NULL - if __next__ or send raise an exception without nlr protection then the exception will be handled by the global exception handler of the VM - this handler already has code to handle exceptions raised in YIELD_FROM, including correct handling of StopIteration - this handler doesn't handle the case of injection of GeneratorExit, but this won't be needed because throw_value == MP_OBJ_NULL Note that it's already possible for mp_resume() to raise an exception (including StopIteration) from the unprotected call to type->iternext(), so that's why the VM already has code to handle the case of exceptions coming out of mp_resume(). This commit reduces code size by a bit, and significantly reduces C stack usage when using yield-from, from 88 bytes down to 40 for Thumb2, and 152 down to 72 bytes for x86-64 (better than half). (Note that gcc doesn't seem to tail-call optimise the call from mp_resume() to mp_obj_gen_resume() so this saving in C stack usage helps all uses of yield-from.)
2018-09-28py/vm: Fix case of throwing GeneratorExit type into yield-from.Damien George
mp_make_raise_obj must be used to convert a possible exception type to an instance object, otherwise the VM may raise a non-exception object. An existing test is adjusted to test this case, with the original test already moved to generator_throw.py.
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/asm*: Remove ASM_MOV_REG_ALIGNED_IMM emit macro, it's no longer used.Damien George
After the previous commit this macro is no longer needed by the native emitter because live heap pointers are no longer stored in generated native machine code.
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-27py/objfloat: Fix abs(-0.0) so it returns 0.0.Damien George
Nan and inf (signed and unsigned) are also handled correctly by using signbit (they were also handled correctly with "val<0", but that didn't handle -0.0 correctly). A test case is added for this behaviour.
2018-09-27py/objgenerator: Remove TODO about returning gen being called again.Damien George
The code implements correct behaviour, as tested by the new test case added in this commit.
2018-09-27py/vm: Reword TODO about invalid ip/sp after an exception to a note.Damien George
2018-09-27py/objmodule: Remove TODO about checking store attr to a module.Damien George
The code implements correct behaviour, as tested by basics/module1.py.
2018-09-27py/objint: Remove TODO about checking of int() arg types with 2 args.Damien George
The arguments are checked by mp_obj_str_get_data and mp_obj_get_int.
2018-09-27py/objdict: Reword TODO about inlining mp_obj_dict_get to a note.Damien George
2018-09-27py/objslice: Remove long-obsolete comment about enhancing slice object.Damien George
Commit afaaf535e6cfaf599432b13a2fbe9373e6a2c4b8 made this comment obsolete.
2018-09-27py/vm: Make small optimisation of BUILD_SLICE opcode.Damien George
No need to call DECODE_UINT since the value will always be either 2 or 3.
2018-09-26py: Fix msvc C++ compiler warnings with MP_OBJ_FUN_MAKE_SIG macro.stijn
When obj.h is compiled as C++ code, the cl compiler emits a warning about possibly unsafe mixing of size_t and bool types in the or operation in MP_OBJ_FUN_MAKE_SIG. Similarly there's an implicit narrowing integer conversion in runtime.h. This commit fixes this by being explicit.
2018-09-26py/objstr: format: Return bytes result for bytes format string.Paul Sokolovsky
This is an improvement over previous behavior when str was returned for both str and bytes input format. This new behaviour is also consistent with how the % operator works, as well as many other str/bytes methods. It should be noted that it's not how current versions of CPython work, where there's a gap in the functionality and bytes.format() is not supported.
2018-09-26py/modmath: Add math.factorial, optimised and non-opt implementations.Christopher Swenson
This commit adds the math.factorial function in two variants: - squared difference, which is faster than the naive version, relatively compact, and non-recursive; - a mildly optimised recursive version, faster than the above one. There are some more optimisations that could be done, but they tend to take more code, and more storage space. The recursive version seems like a sensible compromise. The new function is disabled by default, and uses the non-optimised version by default if it is enabled. The options are MICROPY_PY_MATH_FACTORIAL and MICROPY_OPT_MATH_FACTORIAL.
2018-09-20py/parsenum: Avoid rounding errors with negative powers-of-10.Romain Goyet
This patches avoids multiplying with negative powers-of-10 when parsing floating-point values, when those powers-of-10 can be exactly represented as a positive power. When represented as a positive power and used to divide, the resulting float will not have any rounding errors. The issue is that mp_parse_num_decimal will sometimes not give the closest floating representation of the input string. Eg for "0.3", which can't be represented exactly in floating point, mp_parse_num_decimal gives a slightly high (by 1LSB) result. This is because it computes the answer as 3 * 0.1, and since 0.1 also can't be represented exactly, multiplying by 3 multiplies up the rounding error in the 0.1. Computing it as 3 / 10, as now done by the change in this commit, gives an answer which is as close to the true value of "0.3" as possible.
2018-09-20py/objgenerator: Implement PEP479, StopIteration convs to RuntimeError.Damien George
This commit implements PEP479 which disallows raising StopIteration inside a generator to signal that it should be finished. Instead, the generator should simply return when it is complete. See https://www.python.org/dev/peps/pep-0479/ for details.
2018-09-20py/modbuiltins: Make oct/hex work when !MICROPY_PY_BUILTINS_STR_OP_MODULOPaul Sokolovsky
Instead of redirecting to str.__mod__(), use str.format() in this case.
2018-09-20py/objstr: Make % (__mod__) formatting operator configurable.Paul Sokolovsky
Default is enabled, disabled for minimal builds. Saves 1296 bytes on x86, 976 bytes on ARM.
2018-09-20py: Shorten error messages by using contractions and some rewording.Damien George
2018-09-20py/objtype: Clarify comment about configuring inplace op methods.Damien George
In 0e80f345f88c5db7c2353a5a9d29ed08b0af42f4 the inplace operations __iadd__ and __isub__ were made unconditionally available, so the comment about this section is changed to reflect that.
2018-09-16py/asmthumb: Detect presence of I-cache using CMSIS macro.Damien George
Fixes issue #4113.
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.
2018-09-15py/emitnative: Support arbitrary number of arguments to viper functions.Damien George
2018-09-15py: Make viper functions have the same entry signature as native.Damien George
This commit makes viper functions have the same signature as native functions, at the level of the emitter/assembler. This means that viper functions can now be wrapped in the same uPy object as native functions. Viper functions are now responsible for parsing their arguments (before it was done by the runtime), and this makes calling them more efficient (in most cases) because the viper entry code can be custom generated to suit the signature of the function. This change also opens the way forward for viper functions to take arbitrary numbers of arguments, and for them to handle globals correctly, among other things.
2018-09-15py/emitnative: Reuse mp_native_type_from_qstr when searching for a cast.Damien George
2018-09-15py/compile: Factor code that compiles viper type annotations.Damien George
2018-09-15py/compile: Merge viper annotation and normal param compilation stages.Damien George
Now that the compiler can store the results of the viper types in the scope, the viper parameter annotation compilation stage can be merged with the normal parameter compilation stage.
2018-09-15py/emit: Completely remove set_native_type, arg type is set in compiler.Damien George
In viper mode, the type of the argument is now stored in id_info->flags.
2018-09-15py/emit: Remove need to call set_native_type to set viper return type.Damien George
Instead this return type is now stored in the scope_flags.
2018-09-15py/emit: Remove need to call set_native_type to set native/viper mode.Damien George
The native emitter can easily determine the mode via scope->emit_options.
2018-09-15py/emit: Move MP_EMIT_OPT_xxx enums from compile.h to emitglue.h.Damien George
2018-09-14py/{asmx86,asmx64}: Extend test_r8_with_r8 to accept all 8 lower regs.Damien George
2018-09-14py/asmx64: Fix bug in assembler when creating disp with r13 and 0 offsetDamien George
2018-09-14py: Optimise call to mp_arg_check_num by compressing fun signature.Damien George
With 5 arguments to mp_arg_check_num(), some architectures need to pass values on the stack. So compressing n_args_min, n_args_max, takes_kw into a single word and passing only 3 arguments makes the call more efficient, because almost all calls to this function pass in constant values. Code size is also reduced by a decent amount: bare-arm: -116 minimal x86: -64 unix x64: -256 unix nanbox: -112 stm32: -324 cc3200: -192 esp8266: -192 esp32: -144
2018-09-13py: Fix native functions so they run with their correct globals context.Damien George
Prior to this commit a function compiled with the native decorator @micropython.native would not work correctly when accessing global variables, because the globals dict was not being set upon function entry. This commit fixes this problem by, upon function entry, setting as the current globals dict the globals dict context the function was defined within, as per normal Python semantics, and as bytecode does. Upon function exit the original globals dict is restored. In order to restore the globals dict when an exception is raised the native function must guard its internals with an nlr_push/nlr_pop pair. Because this push/pop is relatively expensive, in both C stack usage for the nlr_buf_t and CPU execution time, the implementation here optimises things as much as possible. First, the compiler keeps track of whether a function even needs to access global variables. Using this information the native emitter then generates three different kinds of code: 1. no globals used, no exception handlers: no nlr handling code and no setting of the globals dict. 2. globals used, no exception handlers: an nlr_buf_t is allocated on the C stack but it is not used if the globals dict is unchanged, saving execution time because nlr_push/nlr_pop don't need to run. 3. function has exception handlers, may use globals: an nlr_buf_t is allocated and nlr_push/nlr_pop are always called. In the end, native functions that don't access globals and don't have exception handlers will run more efficiently than those that do. Fixes issue #1573.
2018-09-11py/emitnative: Fix try-finally in outer scope, so finally is cancelled.Damien George
2018-09-11py/objarray: bytearray: Allow 2nd/3rd arg to constructor.Paul Sokolovsky
If bytearray is constructed from str, a second argument of encoding is required (in CPython), and third arg of Unicode error handling is allowed, e.g.: bytearray("str", "utf-8", "strict") This is similar to bytes: bytes("str", "utf-8", "strict") This patch just allows to pass 2nd/3rd arguments to bytearray, but doesn't try to validate them to not impact code size. (This is also similar to how bytes constructor is handled, though it does a bit more validation, e.g. check that in case of str arg, encoding argument is passed.)
2018-09-11extmod/moduhashlib: Add md5 implementation, using axTLS.Paul Sokolovsky
MD5 is still widely used, and may be important in some cases for networking interoperability, e.g. HTTP Digest authentication.
2018-09-11py/runtime: Fix incorrect test for MICROPY_PORT_DEINIT_FUNC.stijn
2018-09-08py/py.mk: Build axtls library directly from its source files.Damien George
This removes the need for a separate axtls build stage, and builds all axtls object files along with other code. This simplifies and cleans up the build process, automatically builds axtls when needed, and puts the axtls object files in the correct $(BUILD) location. The MicroPython axtls configuration file is provided in extmod/axtls-include/config.h
2018-09-04py/compile: Factor code that compiles start/end of exception handler.Damien George
2018-09-04py/emitnative: Add support for return/break/continue in try and with.Damien George
This patch adds full support for unwinding jumps to the native emitter. This means that return/break/continue can be used in try-except, try-finally and with statements. For code that doesn't use unwinding jumps there is almost no overhead added to the generated code.
2018-09-03py/emitnative: Cancel caught exception once handled to prevent reraise.Damien George
The native emitter keeps the current exception in a slot in its C stack (instead of on its Python value stack), so when it catches an exception it must explicitly clear that slot so the same exception is not reraised later on.