Skip to content
Snippets Groups Projects
  1. Feb 24, 2017
    • Damien George's avatar
      py/parse: Simplify handling of errors by raising them directly. · f615d82d
      Damien George authored
      The parser was originally written to work without raising any exceptions
      and instead return an error value to the caller.  But it's now required
      that a call to the parser be wrapped in an nlr handler, so we may as well
      make use of that fact and simplify the parser so that it doesn't need to
      keep track of any memory errors that it had.  The parser anyway explicitly
      raises an exception at the end if there was an error.
      
      This patch simplifies the parser by letting the underlying memory
      allocation functions raise an exception if they fail to allocate any
      memory.  And if there is an error parsing the "<id> = const(<val>)" pattern
      then that also raises an exception right away instead of trying to recover
      gracefully and then raise.
      f615d82d
    • Damien George's avatar
      py: Create str/bytes objects in the parser, not the compiler. · 5255255f
      Damien George authored
      Previous to this patch any non-interned str/bytes objects would create a
      special parse node that held a copy of the str/bytes data.  Then in the
      compiler this data would be turned into a str/bytes object.  This actually
      lead to 2 copies of the data, one in the parse node and one in the object.
      The parse node's copy of the data would be freed at the end of the compile
      stage but nevertheless it meant that the peak memory usage of the
      parse/compile stage was higher than it needed to be (by an amount equal to
      the number of bytes in all the non-interned str/bytes objects).
      
      This patch changes the behaviour so that str/bytes objects are created
      directly in the parser and the object stored in a const-object parse node
      (which already exists for bignum, float and complex const objects).  This
      reduces peak RAM usage of the parse/compile stage, simplifies the parser
      and compiler, and reduces code size by about 170 bytes on Thumb2 archs,
      and by about 300 bytes on Xtensa archs.
      5255255f
    • Damien George's avatar
      py/parse: Allow parser/compiler consts to be bignums. · 74f4d2c6
      Damien George authored
      This patch allows uPy consts to be bignums, eg:
      
          X = const(1 << 100)
      
      The infrastructure for consts to be a bignum (rather than restricted to
      small integers) has been in place for a while, ever since constant folding
      was upgraded to allow bignums.  It just required a small change (in this
      patch) to enable it.
      74f4d2c6
  2. Feb 16, 2017
    • Damien George's avatar
      py/grammar: Group no-compile grammar rules together to shrink tables. · 71019ae4
      Damien George authored
      Grammar rules have 2 variants: ones that are attached to a specific
      compile function which is called to compile that grammar node, and ones
      that don't have a compile function and are instead just inspected to see
      what form they take.
      
      In the compiler there is a table of all grammar rules, with each entry
      having a pointer to the associated compile function.  Those rules with no
      compile function have a null pointer.  There are 120 such rules, so that's
      120 words of essentially wasted code space.
      
      By grouping together the compile vs no-compile rules we can put all the
      no-compile rules at the end of the list of rules, and then we don't need
      to store the null pointers.  We just have a truncated table and it's
      guaranteed that when indexing this table we only index the first half,
      the half with populated pointers.
      
      This patch implements such a grouping by having a specific macro for the
      compile vs no-compile grammar rules (DEF_RULE vs DEF_RULE_NC).  It saves
      around 460 bytes of code on 32-bit archs.
      71019ae4
  3. Jan 17, 2017
  4. Nov 15, 2016
  5. Nov 02, 2016
    • Colin Hogben's avatar
      py: Fix wrong assumption that m_renew will not move if shrinking · f9b6b37c
      Colin Hogben authored
      In both parse.c and qstr.c, an internal chunking allocator tidies up
      by calling m_renew to shrink an allocated chunk to the size used, and
      assumes that the chunk will not move.  However, when MICROPY_ENABLE_GC
      is false, m_renew calls the system realloc, which does not guarantee
      this behaviour.  Environments where realloc may return a different
      pointer include:
      
      (1) mbed-os with MBED_HEAP_STATS_ENABLED (which adds a wrapper around
      malloc & friends; this is where I was hit by the bug);
      
      (2) valgrind on linux (how I diagnosed it).
      
      The fix is to call m_renew_maybe with allow_move=false.
      f9b6b37c
  6. Sep 23, 2016
  7. Jun 06, 2016
  8. May 20, 2016
  9. May 10, 2016
  10. Apr 14, 2016
    • Damien George's avatar
      py: Simplify "and" action within parser by making ident-rules explicit. · 0c1de1cd
      Damien George authored
      Most grammar rules can optimise to the identity if they only have a single
      argument, saving a lot of RAM building the parse tree.  Previous to this
      patch, whether a given grammar rule could be optimised was defined (mostly
      implicitly) by a complicated set of logic rules.  With this patch the
      definition is always specified explicitly by using "and_ident" in the rule
      definition in the grammar.  This simplifies the logic of the parser,
      making it a bit smaller and faster.  RAM usage in unaffected.
      0c1de1cd
  11. Apr 13, 2016
  12. Mar 19, 2016
  13. Feb 23, 2016
  14. Jan 12, 2016
  15. Jan 08, 2016
  16. Jan 07, 2016
    • Damien George's avatar
      py/parse: Improve constant folding to operate on small and big ints. · 22b22650
      Damien George authored
      Constant folding in the parser can now operate on big ints, whatever
      their representation.  This is now possible because the parser can create
      parse nodes holding arbitrary objects.  For the case of small ints the
      folding is still efficient in RAM because the folded small int is stored
      inplace in the parse node.
      
      Adds 48 bytes to code size on Thumb2 architecture.  Helps reduce heap
      usage because more constants can be computed at compile time, leading to
      a smaller parse tree, and most importantly means that the constants don't
      have to be computed at runtime (perhaps more than once).  Parser will now
      be a little slower when folding due to calls to runtime to do the
      arithmetic.
      22b22650
    • Damien George's avatar
      py/parse: Optimise away parse node that's just parenthesis around expr. · 93b37262
      Damien George authored
      Before this patch, (x+y)*z would be parsed to a tree that contained a
      redundant identity parse node corresponding to the parenthesis.  With
      this patch such nodes are optimised away, which reduces memory
      requirements for expressions with parenthesis, and simplifies the
      compiler because it doesn't need to handle this identity case.
      
      A parenthesis parse node is still needed for tuples.
      93b37262
  17. Dec 18, 2015
    • Damien George's avatar
      py: Add MICROPY_ENABLE_COMPILER and MICROPY_PY_BUILTINS_EVAL_EXEC opts. · dd5353a4
      Damien George authored
      MICROPY_ENABLE_COMPILER can be used to enable/disable the entire compiler,
      which is useful when only loading of pre-compiled bytecode is supported.
      It is enabled by default.
      
      MICROPY_PY_BUILTINS_EVAL_EXEC controls support of eval and exec builtin
      functions.  By default they are only included if MICROPY_ENABLE_COMPILER
      is enabled.
      
      Disabling both options saves about 40k of code size on 32-bit x86.
      dd5353a4
  18. Dec 17, 2015
  19. Nov 29, 2015
  20. Nov 17, 2015
  21. Oct 12, 2015
  22. Oct 08, 2015
  23. Oct 01, 2015
  24. Aug 17, 2015
    • Damien George's avatar
      unix-cpy: Remove unix-cpy. It's no longer needed. · 65dc960e
      Damien George authored
      unix-cpy was originally written to get semantic equivalent with CPython
      without writing functional tests.  When writing the initial
      implementation of uPy it was a long way between lexer and functional
      tests, so the half-way test was to make sure that the bytecode was
      correct.  The idea was that if the uPy bytecode matched CPython 1-1 then
      uPy would be proper Python if the bytecodes acted correctly.  And having
      matching bytecode meant that it was less likely to miss some deep
      subtlety in the Python semantics that would require an architectural
      change later on.
      
      But that is all history and it no longer makes sense to retain the
      ability to output CPython bytecode, because:
      
      1. It outputs CPython 3.3 compatible bytecode.  CPython's bytecode
      changes from version to version, and seems to have changed quite a bit
      in 3.5.  There's no point in changing the bytecode output to match
      CPython anymore.
      
      2. uPy and CPy do different optimisations to the bytecode which makes it
      harder to match.
      
      3. The bytecode tests are not run.  They were never part of Travis and
      are not run locally anymore.
      
      4. The EMIT_CPYTHON option needs a lot of extra source code which adds
      heaps of noise, especially in compile.c.
      
      5. Now that there is an extensive test suite (which tests functionality)
      there is no need to match the bytecode.  Some very subtle behaviour is
      tested with the test suite and passing these tests is a much better
      way to stay Python-language compliant, rather than trying to match
      CPy bytecode.
      65dc960e
  25. Jul 24, 2015
  26. Jul 14, 2015
    • Damien George's avatar
      py: Improve allocation policy of qstr data. · ade9a052
      Damien George authored
      Previous to this patch all interned strings lived in their own malloc'd
      chunk.  On average this wastes N/2 bytes per interned string, where N is
      the number-of-bytes for a quanta of the memory allocator (16 bytes on 32
      bit archs).
      
      With this patch interned strings are concatenated into the same malloc'd
      chunk when possible.  Such chunks are enlarged inplace when possible,
      and shrunk to fit when a new chunk is needed.
      
      RAM savings with this patch are highly varied, but should always show an
      improvement (unless only 3 or 4 strings are interned).  New version
      typically uses about 70% of previous memory for the qstr data, and can
      lead to savings of around 10% of total memory footprint of a running
      script.
      
      Costs about 120 bytes code size on Thumb2 archs (depends on how many
      calls to gc_realloc are made).
      ade9a052
  27. Apr 21, 2015
  28. Feb 23, 2015
  29. Feb 13, 2015
  30. Feb 08, 2015
Loading