Commit Graph

98 Commits

Author SHA1 Message Date
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
Damien George
89f94b51ab py: Rename mp_exc_stack to mp_exc_stack_t. 2014-03-30 00:57:09 +00:00
Damien George
d7592a1c3f py: Fix reraise logic. 2014-03-30 00:54:48 +00:00
Paul Sokolovsky
0c904df8e6 vm: Save current active exception on opening new try block.
Required to reraise correct exceptions in except block, regardless if more
try blocks with active exceptions happen in the same except block.

P.S. This "automagic reraise" appears to be quite wasteful feature of Python
- we need to save pending exception just in case it *might* be reraised.
Instead, programmer could explcitly capture exception to a variable using
"except ... as var", and reraise that. So, consider disabling argless raise
support as an optimization.
2014-03-30 01:01:35 +02:00
Paul Sokolovsky
69975df3ff vm: WITH_CLEANUP: use POP_EXC_BLOCK(). 2014-03-30 01:00:51 +02:00
Paul Sokolovsky
a0ad77ba08 vm: Establish macros PUSH_EXC_BLOCK & POP_EXC_BLOCK to deal with exc stack.
E.g. to handle currently_in_except_block restoring properly.
2014-03-29 23:18:59 +02:00
Paul Sokolovsky
d109676ec0 py: Reraising exception possible only in except block. 2014-03-29 23:18:59 +02:00
Paul Sokolovsky
40d6d29af6 vm: Elaborate comments for WITH_CLEANUP, other cosmetic fixes. 2014-03-29 18:46:04 +02:00
Damien George
c689c19471 py: Make MP_BC_SETUP_WITH use the bytecode stack for load_method.
The compiler allocates 7 entries on the stack for a with statement
(following CPython, but probably can be reduced).  This is enough for
the method load and call in SETUP_WITH.
2014-03-29 14:06:14 +00:00
Damien George
21a07dc50f Merge pull request #389 from pfalcon/with-statement
With statement implementation
2014-03-29 14:00:03 +00:00
Damien George
b04be056fe py: Fix regress with GeneratorExit object becoming truly const. 2014-03-29 13:52:51 +00:00
Damien George
07ddab529c py: Change mp_const_* objects to macros.
Addresses issue #388.
2014-03-29 13:15:08 +00:00
Damien George
da51a399cf Merge pull request #383 from pfalcon/yield-from
Implement "yield from"
2014-03-29 12:18:14 +00:00
Damien George
d1e443d0bc py: Free unique_code slot for outer module.
Partly (very partly!) addresses issue #386.  Most importantly, at the
REPL command line, each invocation does not now lead to increased memory
usage (unless you define a function/lambda).
2014-03-29 11:39:36 +00:00
Paul Sokolovsky
44307d5ef8 vm: Implement "with" statement (SETUP_WITH and WITH_CLEANUP bytecodes). 2014-03-29 04:39:24 +02:00
Paul Sokolovsky
682f9e639d vm: Make sure that exception triple is <type, instance, traceback>.
This reduntant triple is one of the ugliest parts of Python, which they
chickened out to fix in Python3. We really should consider passing just
as single exception instance (without breaking Python-level APIs of course),
but until we do, let's follow CPython layout.
2014-03-29 04:35:36 +02:00
Paul Sokolovsky
4fff26a35c vm: Factor out exception block setup to a macro.
Will be reused in WITH bytecodes.
2014-03-29 04:35:23 +02:00
Paul Sokolovsky
55234f4617 py: yield from: Elaborate GeneratorExit (gen.close()) handling.
Handling of GeneratorExit is really peculiar - it subverts normal exception
propagation rules.
2014-03-28 02:50:56 +02:00
Paul Sokolovsky
cf21a4e7f4 py: Core "yield from" implementation. 2014-03-28 02:50:56 +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
Paul Sokolovsky
2447a5b582 py: Support closures with default args. 2014-03-26 23:17:44 +02:00
Damien George
66eaf84b8c py: Replace mp_const_stop_iteration object with MP_OBJ_NULL. 2014-03-26 19:27:58 +00:00
Damien George
688e220d26 Merge pull request #379 from pfalcon/reraise
vm: Implement raise statement w/o args (reraising last exception).
2014-03-26 18:59:15 +00:00
Damien George
9e6e935df0 py: Add support for user-defined iterators via __iter__, __next__. 2014-03-26 18:37:06 +00:00
Paul Sokolovsky
c403076ef8 vm: Implement raise statement w/o args (reraising last exception). 2014-03-26 17:32:02 +02:00
Paul Sokolovsky
f909034400 py: Implement support for "except Exception as var" clause.
For this, needed to implement DELETE_NAME bytecode (because var bound
in except clause is automatically deleted at its end).
http://docs.python.org/3/reference/compound_stmts.html#except :
"When an exception has been assigned using as target, it is cleared at
the end of the except clause."
2014-03-23 22:00:04 +02:00
Paul Sokolovsky
1673420053 vm: Abstract working with tagged pointers in VM using macro accessors.
Based on issues raised during recent review and inconsistency of different
implementations.
2014-03-23 21:41:29 +02:00
Damien George
ffa37db5c5 py: Fix int -> machine_uint_t. 2014-03-22 20:43:45 +00:00
Paul Sokolovsky
48caa09a9d objgenerator: Implement .throw() method to throw exceptions into generator. 2014-03-22 17:55:42 +02:00
Paul Sokolovsky
c0abc28aa1 objgenerator: Keep exception stack within generator object, like value stack.
This is required to properly handle exceptions across yields.
2014-03-22 17:55:42 +02:00
xbe
efe3422394 py: Clean up includes.
Remove unnecessary includes. Add includes that improve portability.
2014-03-17 02:43:40 -07:00
Damien George
4d79d5dd7c py: Fix type of integer in decoding int. 2014-02-20 00:00:04 +00:00
Paul Sokolovsky
047cd40313 Bytecode int varlen encoding: support arbitrary values for signed ints too. 2014-02-19 15:53:41 +02:00
Paul Sokolovsky
0f96ec8268 Bytecode uint varlen encoding: support arbitrary values. 2014-02-19 15:52:57 +02:00
Damien George
c8f78bc280 py: VM never throws an exception, instead returns a status and value.
Addresses issue #290, and hopefully sets up things to allow generators
throwing exceptions, etc.
2014-02-15 22:55:00 +00:00
Damien George
c5966128c7 Implement proper exception type hierarchy.
Each built-in exception is now a type, with base type BaseException.
C exceptions are created by passing a pointer to the exception type to
make an instance of.  When raising an exception from the VM, an
instance is created automatically if an exception type is raised (as
opposed to an exception instance).

Exception matching (RT_BINARY_OP_EXCEPTION_MATCH) is now proper.

Handling of parse error changed to match new exceptions.

mp_const_type renamed to mp_type_type for consistency.
2014-02-15 16:10:44 +00:00
Damien George
aa9b74fead py: Fix IMPORT_STAR, needs to pop the stack. 2014-02-14 23:06:33 +00:00
Paul Sokolovsky
da1ce93da8 Implement "from module import *" construct. 2014-02-14 00:30:36 +02:00
Damien George
9aa2a527b5 py: Tidy up BINARY_OPs; negation done by special NOT bytecode.
IS_NOT and NOT_IN are now compiled to IS + NOT and IN + NOT, with a new
special NOT bytecode.
2014-02-01 23:04:09 +00:00
Damien George
cbddb279bb py: Implement break/continue from an exception with finally.
Still todo: break/continue from within the finally block itself.
2014-02-01 20:08:18 +00:00
Damien George
fb083ea986 py: mp_execute_byte_code has 2 arg arrays, for more efficient default params. 2014-02-01 18:29:40 +00:00
Damien George
87413a4d0c Merge branch 'fun-defargs' of github.com:pfalcon/micropython into pfalcon-fun-defargs 2014-02-01 17:51:47 +00:00
Paul Sokolovsky
90750029df Implement default function arguments (for Python functions).
TODO: Decide if we really need separate bytecode for creating functions
with default arguments - we would need same for closures, then there're
keywords arguments too. Having all combinations is a small exponential
explosion, likely we need just 2 cases - simplest (no defaults, no kw),
and full - defaults & kw.
2014-02-01 15:38:22 +02:00
Paul Sokolovsky
6472dea146 Add exception stack unwind support for RETURN_VALUE.
This properly implements return from try/finally block(s).

TODO: Consider if we need to do any value stack unwinding for RETURN_VALUE
case. Intuitively, this is "success" return, so value stack should be in
good shape, and unwinding shouldn't be required.
2014-02-01 15:23:03 +02:00
Paul Sokolovsky
8519342c1a Update VM stacks comments. 2014-01-31 19:48:53 +02:00
Paul Sokolovsky
c7a0b14df9 vm: Introduce structure for exception stack entry, record entry type.
Also, handle SETUP_FINALLY opcode.
2014-01-31 19:48:53 +02:00
Paul Sokolovsky
382e8eeea2 vm: Add basic implementation of END_FINALLY opcode.
Allows to have nested try blocks with except filters.

TODO: Don't add END_FINALLY's exception re-raise points to traceback.
2014-01-30 13:49:18 +02:00
Damien George
d0691ccaec py: Simplify fastn in VM; reduce size of unique code struct.
We still have FAST_[0,1,2] byte codes, but they now just access the
fastn array (before they had special local variables).  It's now
simpler, a bit faster, and uses a bit less stack space (on STM at least,
which is most important).

The only reason now to keep FAST_[0,1,2] byte codes is for compressed
byte code size.
2014-01-29 20:30:52 +00:00
Damien George
08d075592f py: Fix bug with LOAD_METHOD; fix int->machine_int_t for small int.
LOAD_METHOD bug was: emitbc did not correctly calculate the amount of
stack usage for a LOAD_METHOD operation.

small int bug was: int was being used to pass small ints, when it should
have been machine_int_t.
2014-01-29 18:58:52 +00:00