Skip to content
Snippets Groups Projects
  1. May 27, 2019
  2. Mar 05, 2019
    • Damien George's avatar
      py: Replace POP_BLOCK and POP_EXCEPT opcodes with POP_EXCEPT_JUMP. · 5a2599d9
      Damien George authored
      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.
      5a2599d9
    • Damien George's avatar
      py/vm: Remove currently_in_except_block variable. · 6f9e3ff7
      Damien George authored
      After the previous commit it is no longer needed.
      6f9e3ff7
    • Damien George's avatar
      py: Fix VM crash with unwinding jump out of a finally block. · e1fb03f3
      Damien George authored
      This patch fixes a bug in the VM when breaking within a try-finally.  The
      bug has to do with executing a break within the finally block of a
      try-finally statement.  For example:
      
          def f():
              for x in (1,):
                  print('a', x)
                  try:
                      raise Exception
                  finally:
                      print(1)
                      break
                  print('b', x)
          f()
      
      Currently in uPy the above code will print:
      
          a 1
          1
          1
          segmentation fault (core dumped)  micropython
      
      Not only is there a seg fault, but the "1" in the finally block is printed
      twice.  This is because when the VM executes a finally block it doesn't
      really know if that block was executed due to a fall-through of the try (no
      exception raised), or because an exception is active.  In particular, for
      nested finallys the VM has no idea which of the nested ones have active
      exceptions and which are just fall-throughs.  So when a break (or continue)
      is executed it tries to unwind all of the finallys, when in fact only some
      may be active.
      
      It's questionable whether break (or return or continue) should be allowed
      within a finally block, because they implicitly swallow any active
      exception, but nevertheless it's allowed by CPython (although almost never
      used in the standard library).  And uPy should at least not crash in such a
      case.
      
      The solution here relies on the fact that exception and finally handlers
      always appear in the bytecode after the try body.
      
      Note: there was a similar bug with a return in a finally block, but that
      was previously fixed in b7352084
      e1fb03f3
  3. Feb 12, 2019
    • Damien George's avatar
      py: Downcase all MP_OBJ_IS_xxx macros to make a more consistent C API. · eee1e884
      Damien George authored
      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.
      eee1e884
  4. Feb 05, 2019
  5. Jan 31, 2019
    • Paul Sokolovsky's avatar
      py/warning: Support categories for warnings. · 2f5d113f
      Paul Sokolovsky authored
      Python defines warnings as belonging to categories, where category is a
      warning type (descending from exception type). This is useful, as e.g.
      allows to disable warnings selectively and provide user-defined warning
      types.  So, implement this in MicroPython, except that categories are
      represented just with strings.  However, enough hooks are left to implement
      categories differently per-port (e.g. as types), without need to patch each
      and every usage.
      2f5d113f
  6. Jan 04, 2019
  7. Sep 29, 2018
    • Damien George's avatar
      py/vm: When VM raises exception put exc obj at beginning of func state. · d95947b4
      Damien George authored
      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.
      d95947b4
  8. Sep 28, 2018
  9. Sep 27, 2018
  10. Sep 03, 2018
    • Damien George's avatar
      py/vm: Fix handling of finally-return with complex nested finallys. · b7352084
      Damien George authored
      Back in 8047340d basic support was added in
      the VM to handle return statements within a finally block.  But it didn't
      cover all cases, in particular when some finally's were active and others
      inactive when the "return" was executed.
      
      This patch adds further support for return-within-finally by correctly
      managing the currently_in_except_block flag, and should fix all cases.  The
      main point is that finally handlers remain on the exception stack even if
      they are active (currently being executed), and the unwind return code
      should only execute those finally's which are inactive.
      
      New tests are added for the cases which now pass.
      b7352084
  11. Jun 08, 2018
  12. May 18, 2018
  13. May 16, 2018
  14. Apr 03, 2018
    • Damien George's avatar
      py/vm: Optimise handling of stackless mode when pystack is enabled. · bc365213
      Damien George authored
      When pystack is enabled mp_obj_fun_bc_prepare_codestate() will always
      return a valid pointer, and if there is no more pystack available then it
      will raise an exception (a RuntimeError).  So having pystack enabled with
      stackless enabled automatically gives strict stackless mode.  There is
      therefore no need to have code for strict stackless mode when pystack is
      enabled, and this patch optimises the VM for such a case.
      bc365213
    • Damien George's avatar
      py/vm: Don't do unnecessary updates of ip and sp variables. · c7f880ed
      Damien George authored
      Neither the ip nor sp variables are used again after the execution of the
      RAISE_VARARGS opcode, so they don't need to be updated.
      c7f880ed
  15. Feb 27, 2018
    • Damien George's avatar
      py/vm: Simplify handling of special-case STOP_ITERATION in yield from. · a9f6d492
      Damien George authored
      There's no need to have MP_OBJ_NULL a special case, the code can re-use
      the MP_OBJ_STOP_ITERATION value to signal the special case and the VM can
      detect this with only one check (for MP_OBJ_STOP_ITERATION).
      a9f6d492
    • Damien George's avatar
      py/vm: Fix case of handling raised StopIteration within yield from. · 22ade2f5
      Damien George authored
      This patch concerns the handling of an NLR-raised StopIteration, raised
      during a call to mp_resume() which is handling the yield from opcode.
      
      Previously, commit 6738c1dd introduced code
      to handle this case, along with a test.  It seems that it was lucky that
      the test worked because the code did not correctly handle the stack pointer
      (sp).
      
      Furthermore, commit 79d996a5 improved the
      way mp_resume() propagated certain exceptions: it changed raising an NLR
      value to returning MP_VM_RETURN_EXCEPTION.  This change meant that the
      test introduced in gen_yield_from_ducktype.py was no longer hitting the
      code introduced in 6738c1dd.
      
      The patch here does two things:
      
      1. Fixes the handling of sp in the VM for the case that yield from is
         interrupted by a StopIteration raised via NLR.
      
      2. Introduces a new test to check this handling of sp and re-covers the
         code in the VM.
      22ade2f5
  16. Feb 15, 2018
    • Damien George's avatar
      py/objexcept: Remove long-obsolete mp_const_MemoryError_obj. · 73d1d20b
      Damien George authored
      This constant exception instance was once used by m_malloc_fail() to raise
      a MemoryError without allocating memory, but it was made obsolete long ago
      by 3556e457.  The functionality is now
      replaced by the use of mp_emergency_exception_obj which lives in the global
      uPy state, and which can handle any exception type, not just MemoryError.
      73d1d20b
  17. Feb 08, 2018
    • Damien George's avatar
      py/vm: Simplify stack sentinel values for unwind return and jump. · 0c650d42
      Damien George authored
      This patch simplifies how sentinel values are stored on the stack when
      doing an unwind return or jump.  Instead of storing two values on the stack
      for an unwind jump it now stores only one: a negative small integer means
      unwind-return and a non-negative small integer means unwind-jump with the
      value being the number of exceptions to unwind.  The savings in code size
      are:
      
         bare-arm:   -56
      minimal x86:   -68
         unix x64:   -80
      unix nanbox:    -4
            stm32:   -56
           cc3200:   -64
          esp8266:   -76
            esp32:  -156
      0c650d42
  18. Dec 11, 2017
  19. Dec 09, 2017
  20. Oct 04, 2017
    • Damien George's avatar
      all: Remove inclusion of internal py header files. · a3dc1b19
      Damien George authored
      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.
      a3dc1b19
  21. Sep 22, 2017
  22. Jul 31, 2017
  23. Jul 18, 2017
  24. Jul 03, 2017
  25. Jun 09, 2017
    • Damien George's avatar
      py: Provide mp_decode_uint_skip() to help reduce stack usage. · a8a5d1e8
      Damien George authored
      Taking the address of a local variable leads to increased stack usage, so
      the mp_decode_uint_skip() function is added to reduce the need for taking
      addresses.  The changes in this patch reduce stack usage of a Python call
      by 8 bytes on ARM Thumb, by 16 bytes on non-windowing Xtensa archs, and by
      16 bytes on x86-64.  Code size is also slightly reduced on most archs by
      around 32 bytes.
      a8a5d1e8
  26. May 29, 2017
  27. May 25, 2017
  28. Apr 22, 2017
    • Damien George's avatar
      py: Add LOAD_SUPER_METHOD bytecode to allow heap-free super meth calls. · dd11af20
      Damien George authored
      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
      dd11af20
  29. Mar 26, 2017
  30. Mar 24, 2017
  31. Mar 23, 2017
Loading