Commit Graph

83 Commits

Author SHA1 Message Date
Paul Sokolovsky d5e629ad0e objgenerator: Can optimize StopIteration to STOP_ITERATION only if arg is None.
Unfortunately, MP_OBJ_STOP_ITERATION doesn't have means to pass an associated
value, so we can't optimize StopIteration exception with (non-None) argument
to MP_OBJ_STOP_ITERATION.
2015-05-11 23:57:42 +01:00
Paul Sokolovsky aa9dbb1b03 objgenerator: If generator yielded STOP_ITERATION value, it's stopped.
MP_OBJ_STOP_ITERATION is equivalent of raising StopIteration, except
mp_vm_return_kind_t for it is "yield".
2015-05-11 23:57:42 +01:00
Damien George 044c473de2 py: Add %q format support to mp_[v]printf, and use it. 2015-04-16 14:30:16 +00:00
Damien George 7f9d1d6ab9 py: Overhaul and simplify printf/pfenv mechanism.
Previous to this patch the printing mechanism was a bit of a tangled
mess.  This patch attempts to consolidate printing into one interface.

All (non-debug) printing now uses the mp_print* family of functions,
mainly mp_printf.  All these functions take an mp_print_t structure as
their first argument, and this structure defines the printing backend
through the "print_strn" function of said structure.

Printing from the uPy core can reach the platform-defined print code via
two paths: either through mp_sys_stdout_obj (defined pert port) in
conjunction with mp_stream_write; or through the mp_plat_print structure
which uses the MP_PLAT_PRINT_STRN macro to define how string are printed
on the platform.  The former is only used when MICROPY_PY_IO is defined.

With this new scheme printing is generally more efficient (less layers
to go through, less arguments to pass), and, given an mp_print_t*
structure, one can call mp_print_str for efficiency instead of
mp_printf("%s", ...).  Code size is also reduced by around 200 bytes on
Thumb2 archs.
2015-04-16 14:30:16 +00:00
Damien George 9988618e0e py: Implement full func arg passing for native emitter.
This patch gets full function argument passing working with native
emitter.  Includes named args, keyword args, default args, var args
and var keyword args.  Fully Python compliant.

It reuses the bytecode mp_setup_code_state function to do all the hard
work.  This function is slightly adjusted to accommodate native calls,
and the native emitter is forced a bit to emit similar prelude and
code-info as bytecode.
2015-04-07 22:43:28 +01:00
stijn 3cc17c69ff py: Allow retrieving a function's __name__.
Disabled by default.  Enabled on unix and stmhal ports.
2015-03-20 23:13:32 +00:00
Damien George ff8dd3f486 py, unix: Allow to compile with -Wunused-parameter.
See issue #699.
2015-01-20 12:47:20 +00:00
Damien George d2d64f00fb py: Add "default" to switches to allow better code flow analysis.
This helps compiler produce smaller code.  Saves 124 bytes on stmhal and
bare-arm.
2015-01-14 21:32:42 +00:00
Damien George 51dfcb4bb7 py: Move to guarded includes, everywhere in py/ core.
Addresses issue #1022.
2015-01-01 20:32:09 +00:00
Damien George 969a6b37bf py: Make functions static where appropriate. 2014-12-10 22:08:14 +00:00
Damien George 1084b0f9c2 py: Store bytecode arg names in bytecode (were in own array).
This saves a lot of RAM for 2 reasons:

1. For functions that don't have default values, var args or var kw
args (which is a large number of functions in the general case), the
mp_obj_fun_bc_t type now fits in 1 GC block (previously needed 2 because
of the extra pointer to point to the arg_names array).  So this saves 16
bytes per function (32 bytes on 64-bit machines).

2. Combining separate memory regions generally saves RAM because the
unused bytes at the end of the GC block are saved for 1 of the blocks
(since that block doesn't exist on its own anymore).  So generally this
saves 8 bytes per function.

Tested by importing lots of modules:

- 64-bit Linux gave about an 8% RAM saving for 86k of used RAM.
- pyboard gave about a 6% RAM saving for 31k of used RAM.
2014-10-25 20:23:13 +01:00
Damien George b534e1b9f1 py: Use variable length encoded uints in more places in bytecode.
Code-info size, block name, source name, n_state and n_exc_stack now use
variable length encoded uints.  This saves 7-9 bytes per bytecode
function for most functions.
2014-09-04 14:44:01 +01:00
Damien George ecc88e949c Change some parts of the core API to use mp_uint_t instead of uint/int.
Addressing issue #50, still some way to go yet.
2014-08-30 00:35:11 +01:00
Damien George 40f3c02682 Rename machine_(u)int_t to mp_(u)int_t.
See discussion in issue #50.
2014-07-03 13:25:24 +01:00
Paul Sokolovsky 5f4a667ea4 objgenerator: Finish refactor to use mp_setup_code_state(). 2014-06-11 20:43:47 +03:00
Paul Sokolovsky f77d0c5bb3 objgenerator: First iteration of refactor to use mp_setup_code_state(). 2014-06-11 20:43:47 +03:00
Damien George aabd83ea20 py: Merge mp_execute_bytecode into fun_bc_call.
This reduces stack usage by 16 words (64 bytes) for stmhal/ port.

See issue #640.
2014-06-07 14:16:08 +01:00
Paul Sokolovsky b4ebad3310 vm: Factor out structure with code execution state and pass it around.
This improves stack usage in callers to mp_execute_bytecode2, and is step
forward towards unifying execution interface for function and generators
(which is important because generators don't even support full forms
of arguments passing (keywords, etc.)).
2014-05-31 18:22:01 +03:00
Paul Sokolovsky 1f07b7e3c3 py: Reformat few long functions argument lists for clarity. 2014-05-31 03:36:37 +03:00
Paul Sokolovsky bf27140193 py: More mp_identity usage. 2014-05-17 11:20:10 +03:00
Paul Sokolovsky da9f0924ef py, unix: Add copyright for modules I worked closely on. 2014-05-13 18:41:25 +03:00
Damien George 3417bc2f25 py: Rename byte_code to bytecode everywhere.
bytecode is the more widely used.  See issue #590.
2014-05-10 10:36:38 +01:00
stijn 912ca7701d py: Comment exc_state member from mp_obj_gen_instance_t as it gives trouble
...to some compilers who can't process 2 zero-sized arrays in structs. It's
never referenced directly anyway.

See disussion on #568 as well.
2014-05-05 22:56:27 +03:00
Damien George 04b9147e15 Add license header to (almost) all files.
Blanket wide to all .c and .h files.  Some files originating from ST are
difficult to deal with (license wise) so it was left out of those.

Also merged modpyb.h, modos.h, modstm.h and modtime.h in stmhal/.
2014-05-03 23:27:38 +01:00
Paul Sokolovsky f54bcbf099 py, unix: Make "mpconfig.h" be first included, as other headers depend on it.
Specifically, nlr.h does.
2014-05-02 17:48:40 +03:00
Paul Sokolovsky c3103b55c1 objgenerator: .print(): Output real underlying function name. 2014-05-01 23:22:11 +03:00
Damien George ea8d06c39d py: Add MP_OBJ_STOP_ITERATION and make good use of it.
Also make consistent use of MP_OBJ_NOT_SUPPORTED and MP_OBJ_NULL.
This helps a lot in debugging and understanding of function API.
2014-04-17 23:19:36 +01:00
Damien George de7c425139 py: Simplify objfun/objgenerator connection, no need to call bc_get. 2014-04-17 19:16:11 +01:00
Paul Sokolovsky b7e90ea078 objgenerator: Generator must execute in its defining lexical context.
I.e. with its own globals. So, just as for functions, we need to switch
globals when resuming a generator.
2014-04-17 20:27:00 +03:00
Damien George d99944acdd py: Clear state to MP_OBJ_NULL before executing byte code. 2014-04-09 19:53:31 +01:00
Damien George 13d6739cc7 py: Generators can have their locals closed over. 2014-04-09 19:01:45 +01:00
Damien George ea13f407a3 py: Change nlr_jump to nlr_raise, to aid in debugging.
This does not affect code size or performance when debugging turned off.

To address issue #420.
2014-04-05 18:32:08 +01:00
Paul Sokolovsky 817e76a1a5 objgenerator.throw(GeneratorExit) is not equivalent to .close().
.throw() propagates any exceptions, and .close() swallows them. Yielding
in reponse to .throw(GeneratorExit) is still fatal, and we need to
handle it for .throw() case separately (previously it was handled only
for .close() case).

Obscure corner cases due to test_pep380.py.
2014-03-31 17:22:37 +03:00
Paul Sokolovsky 1eac05d541 objgenerator: Another obscure case of propagating MP_OBJ_NULL optimization. 2014-03-31 17:22:37 +03:00
Damien George 0997af932f py: Don't wrap necessary function calls in assert. 2014-03-30 21:55:28 +01:00
Damien George 0aa4379543 Merge pull request #399 from pfalcon/gen-defargs
objgenerator: Handle default args to generator functions.
2014-03-30 21:51:49 +01:00
Paul Sokolovsky f39d3b93da py: Implement support for generalized generator protocol.
Iterators and ducktype objects can now be arguments of yield from.
2014-03-30 23:30:16 +03:00
Paul Sokolovsky 7fafb28f6d objgenerator: Handle default args to generator functions.
Addresses #397.
2014-03-30 20:25:19 +03:00
Damien George df6567e634 Merge map.h into obj.h.
Pretty much everyone needs to include map.h, since it's such an integral
part of the Micro Python object implementation.  Thus, the definitions
are now in obj.h instead.  map.h is removed.
2014-03-30 13:54:02 +01:00
Damien George d17926db71 Rename rt_* to mp_*.
Mostly just a global search and replace.  Except rt_is_true which
becomes mp_obj_is_true.

Still would like to tidy up some of the names, but this will do for now.
2014-03-30 13:35:08 +01:00
Paul Sokolovsky ee5ecc9de2 objgenerator.throw: Instantiate if exception type passed, just as "raise".
Caught by CPython test_pep380.py.
2014-03-30 13:21:11 +03:00
Paul Sokolovsky 9a54a22318 objgenerator.throw(): Throwing GeneratorExit is equivalent to .close().
According to PEP380 and caught by CPython test_pep380.py .
2014-03-30 13:21:11 +03:00
Paul Sokolovsky 6ae237d2bd objgenerator: Store proper code_info pointer. 2014-03-30 04:16:58 +03:00
Paul Sokolovsky aaff716189 objgenerator: mp_obj_gen_resume() suitable only for generators. 2014-03-30 04:16:43 +03:00
Damien George 89f94b51ab py: Rename mp_exc_stack to mp_exc_stack_t. 2014-03-30 00:57:09 +00:00
Damien George 3e1a5c10c5 py: Rename old const type objects to mp_type_* for consistency. 2014-03-29 13:43:38 +00:00
Damien George 07ddab529c py: Change mp_const_* objects to macros.
Addresses issue #388.
2014-03-29 13:15:08 +00:00
Paul Sokolovsky c4d589e2bb objgenerator: close(): Throw instance of GeneratorExit (not type).
To comply with Python semantics and allow use of mp_obj_is_subclass_fast()
for exception matching.
2014-03-28 02:40:26 +02:00
Damien George bee17b00e3 py: Put n_state for bytecode in the bytecode prelude.
Rationale: setting up the stack (state for locals and exceptions) is
really part of the "code", it's the prelude of the function.  For
example, native code adjusts the stack pointer on entry to the function.
Native code doesn't need to know n_state for any other reason.  So
putting the state size in the bytecode prelude is sensible.

It reduced ROM usage on STM by about 30 bytes :)  And makes it easier to
pass information about the bytecode between functions.
2014-03-27 11:07:04 +00:00
Damien George 8dcc0c7924 py: Calculate maximum exception stack size in compiler. 2014-03-27 10:55:21 +00:00