Commit Graph

1898 Commits

Author SHA1 Message Date
Damien George 30d8a82220 py: Allow to compile with -Wredundant-decls. 2015-01-12 22:41:55 +00:00
Damien George abc1959e2c py, unix, lib: Allow to compile with -Wold-style-definition. 2015-01-12 22:34:38 +00:00
Damien George cd34207409 py: Can compile with -Wmissing-declarations and -Wmissing-prototypes. 2015-01-12 22:30:49 +00:00
Damien George 3dd1c0a88a py: Make a function static and comment out those not used. 2015-01-12 22:22:46 +00:00
Damien George 0178aa9a11 py, unix: Allow to compile with -Wdouble-promotion.
Ref issue #699.
2015-01-12 21:56:35 +00:00
Damien George 99ab64ffd4 py/makeqstrdata.py: Make it work again with both Python2 and Python3. 2015-01-11 22:40:38 +00:00
Damien George 95836f8439 py: Add MICROPY_QSTR_BYTES_IN_LEN config option, defaulting to 1.
This new config option sets how many fixed-number-of-bytes to use to
store the length of each qstr.  Previously this was hard coded to 2,
but, as per issue #1056, this is considered overkill since no-one
needs identifiers longer than 255 bytes.

With this patch the number of bytes for the length is configurable, and
defaults to 1 byte.  The configuration option filters through to the
makeqstrdata.py script.

Code size savings going from 2 to 1 byte:
- unix x64 down by 592 bytes
- stmhal down by 1148 bytes
- bare-arm down by 284 bytes

Also has RAM savings, and will be slightly more efficient in execution.
2015-01-11 22:27:30 +00:00
Damien George 6942f80a8f py: Add qstr cfg capability; generate QSTR_NULL and QSTR_ from script. 2015-01-11 22:06:53 +00:00
Damien George e233a55a29 py: Remove unnecessary BINARY_OP_EQUAL code that just checks pointers.
Previous patch c38dc3ccc7 allowed any
object to be compared with any other, using pointer comparison for a
fallback.  As such, existing code which checked for this case is no
longer needed.
2015-01-11 21:07:15 +00:00
Damien George c38dc3ccc7 py: Implement fallback for equality check for all types.
Return "not equal" for objects that don't implement equality check.
This is as per Python specs.
2015-01-11 15:13:18 +00:00
Damien George ec21405821 py: Add (commented out) code to gc_dump_alloc_table for qstr info. 2015-01-11 14:37:06 +00:00
Damien George 56e1f99ca1 py/makeqstrdata.py: Add more allowed qstr characters; escape quot. 2015-01-11 14:16:24 +00:00
Damien George 01418e9690 py: Fix hard-coded hash for empty qstr (was 0x0000 now 0x1505). 2015-01-11 14:15:45 +00:00
Damien George ddd1e18801 py: Add config option MICROPY_COMP_MODULE_CONST for module consts.
Compiler optimises lookup of module.CONST when enabled (an existing
feature).  Disabled by default; enabled for unix, windows, stmhal.
Costs about 100 bytes ROM on stmhal.
2015-01-10 14:07:24 +00:00
Damien George 58056b0f43 py: Fix handling of "0" mpz in some functions. 2015-01-09 20:58:58 +00:00
Damien George 5d48f234d2 py: Make mem_info print correct remaining stack bytes. 2015-01-09 20:37:49 +00:00
Damien George 89deec0bab py: Add MICROPY_PY_MICROPYTHON_MEM_INFO to enable mem-info funcs.
This allows to enable mem-info functions in micropython module, even if
MICROPY_MEM_STATS is not enabled.  In this case, you get mem_info and
qstr_info but not mem_{total,current,peak}.
2015-01-09 20:12:54 +00:00
Damien George 4a5895c4eb py: Disable stack checking by default; enable on most ports. 2015-01-09 00:10:55 +00:00
Damien George c33ecb83ba tests: Add test for when instance member overrides class member. 2015-01-08 17:48:44 +00:00
Damien George 5b7aa294e0 py: Fix nlr mp_state_ctx symbol error for Mac. 2015-01-08 16:24:44 +00:00
stijn afd6c8e1d2 Remove obsolete bss-related code/build features
GC for unix/windows builds doesn't make use of the bss section anymore,
so we do not need the (sometimes complicated) build features and code related to it
2015-01-08 15:29:44 +01:00
Damien George 7ee91cf861 py: Add option to cache map lookup results in bytecode.
This is a simple optimisation inspired by JITing technology: we cache in
the bytecode (using 1 byte) the offset of the last successful lookup in
a map. This allows us next time round to check in that location in the
hash table (mp_map_t) for the desired entry, and if it's there use that
entry straight away.  Otherwise fallback to a normal map lookup.

Works for LOAD_NAME, LOAD_GLOBAL, LOAD_ATTR and STORE_ATTR opcodes.

On a few tests it gives >90% cache hit and greatly improves speed of
code.

Disabled by default.  Enabled for unix and stmhal ports.
2015-01-07 21:07:23 +00:00
Damien George b4b10fd350 py: Put all global state together in state structures.
This patch consolidates all global variables in py/ core into one place,
in a global structure.  Root pointers are all located together to make
GC tracing easier and more efficient.
2015-01-07 20:33:00 +00:00
Damien George ad2307c92c py: Temporary fix for conversion of float to int when fits in small int.
Addresses issue #1044 (see also #1040).  Could do with a better fix.
2015-01-07 12:10:47 +00:00
Paul Sokolovsky d8bfd77ad5 showbc: Show conditional jump destination as unsigned value.
This is consistent with how BC_JUMP was handled before. We never show jumps
destinations relative to jump instrucion itself, only relative to beginning
of function. Another useful way to show them as absolute (real memory
address), and this change makes result expected and consistent with how
BC_JUMP is shown.
2015-01-07 00:29:15 +02:00
Paul Sokolovsky 343ca1e63a objarray: Make sure that longint works as bytearray size. 2015-01-04 17:19:16 +02:00
Paul Sokolovsky ed3b20aae8 modbuiltins.c: Fix NULL vs MP_OBJ_NULL usage. 2015-01-04 13:26:43 +02:00
Paul Sokolovsky ff8e35b42e objstr: Common subexpression elimination for vstr_str(field_name). 2015-01-04 13:23:44 +02:00
Paul Sokolovsky c114496641 objstr: Implement kwargs support for str.format(). 2015-01-04 00:26:31 +02:00
Damien George 6fd4b36bc5 py: Raise exception if trying to convert inf/nan to int. 2015-01-02 23:04:09 +00:00
David Steinberg 6e0b6d02db py: Fix float to int conversion for large exponents. 2015-01-02 22:31:41 +00:00
stijn fbfd3554fa msvc: Fix unresolved mp_arg_error_terse_mismatch since 7f23384
The compiler treats `if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE)` as
a normal statement and generates assembly for it in degug mode as if MICROPY_ERROR_REPORTING
is an actual symbol instead of a preprocessor definition.
As such linking fails because mp_arg_error_terse_mismatch is not defined when
MICROPY_ERROR_REPORTING_TERSE is detailed or normal.
2015-01-02 16:53:54 +01:00
Damien George fd40a9c38e py: Make GC's STACK_SIZE definition a proper MICROPY_ config variable. 2015-01-01 22:04:46 +00:00
Damien George 872a82970d py: Fix windows external name error for nlr_top. 2015-01-01 22:03:44 +00:00
Damien George 8a2347723e py: Move global variable nlr_top to one place, in a .c file.
This reduces dependency on assembler, and allows to consolidate global
variables in the future.
2015-01-01 21:47:58 +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
Paul Sokolovsky db1ac360c3 emitnative: Disable warning in delete_fast for now (breaks test). 2015-01-01 22:09:18 +02:00
Paul Sokolovsky 8a8c1fc82f py: Add basic framework for issuing compile/runtime warnings. 2015-01-01 22:09:18 +02:00
Damien George ebde3c694f py: Add guarded includes for asm-based headers. 2015-01-01 18:07:43 +00:00
Damien George 84e0cf0d21 py: Change namedtuple error messages to reduce code size.
We are not word-for-word compatible with CPython exceptions, so we are
free to make them short but informative in order to reduce code size.
Also, try to make messages the same as existing ones where possible.
2015-01-01 15:43:25 +00:00
Damien George 7f23384d49 py: Make terse_arg_mismatch a global function and use it elsewhere.
Reduces code size when MICROPY_ERROR_REPORTING_TERSE is selected.
2015-01-01 15:33:50 +00:00
Paul Sokolovsky 276159e5dd objnamedtuple: Make sure to initialize type structure completely. 2015-01-01 15:31:51 +02:00
stijn 021dc44009 py: Allow keyword arguments for namedtuple 2015-01-01 14:53:23 +02:00
stijn 12340147b0 py: Use sequence of strings for named tuple initialization
- remove single string initialization style
- take list of strings instead
- store list in the type for fast lookup
2015-01-01 14:53:23 +02:00
Damien George e0ac194f4f py: Fix rshift and not of zero/one edge cases in mpz.
Addresses issue #1027.
2014-12-31 19:35:01 +00:00
Paul Sokolovsky 2c75665445 objstr: Fix %d-formatting of floats. 2014-12-31 02:21:19 +02:00
Paul Sokolovsky 5f68094e10 py: mp_obj_new_int_from_float() supported only for MICROPY_PY_BUILTINS_FLOAT. 2014-12-30 00:34:54 +02:00
Paul Sokolovsky f79cd6a233 py: Implement mp_obj_new_int_from_float() for MICROPY_LONGINT_IMPL_NONE. 2014-12-30 00:33:32 +02:00
Paul Sokolovsky 12033df511 py: Partially fix float to int conversion.
This fixes conversion when float type has more mantissa bits than small int,
and float value has small exponent. This is for example the case of 32-bit
platform using doubles, and converting value of time.time(). Conversion of
floats with larg exponnet is still not handled correctly.
2014-12-30 00:22:50 +02:00
Damien George 9ddbe291c4 py: Add include guards to mpconfig,misc,qstr,obj,runtime,parsehelper. 2014-12-29 01:02:19 +00:00
Damien George f89d659e3b py: In VM, for selective ip saving, store 1 byte past last opcode.
This is for efficiency, so we don't need to subtract 1 from the ip
before storing it to code_state->ip.  It saves a lot of ROM bytes on
unix and stmhal.
2014-12-29 00:29:59 +00:00
Paul Sokolovsky 23f1b5ff66 py: Add note about -fno-crossjumping when compiling vm.c. 2014-12-29 00:07:47 +00:00
Paul Sokolovsky 361909e3ca py: Add MP_LIKELY(), MP_UNLIKELY() macros to help branch prediction. 2014-12-29 00:51:24 +02:00
Paul Sokolovsky 1ee1785bed showbc: Print operation mnemonic in BINARY_OP. 2014-12-28 21:43:44 +02:00
Paul Sokolovsky df103462dc showbc: Make code object start pointer semi-public.
This allows to pring either absolute addresses or relative offsets in jumps
and code references.
2014-12-28 21:37:17 +02:00
Paul Sokolovsky 749575097f vm: Record exception ip only for instructions where exceptions may happen.
Mirroring ip to a volatile memory variable for each opcode is an expensive
operation. For quite a lot of often executed opcodes like stack manipulation
or jumps, exceptions cannot actually happen. So, record ip only for opcode
where that's possible.
2014-12-28 07:37:04 +02:00
Damien George 83204f3406 py: Allow to properly disable builtin slice operation.
This patch makes the MICROPY_PY_BUILTINS_SLICE compile-time option
fully disable the builtin slice operation (when set to 0).  This
includes removing the slice sytanx from the grammar.  Now, enabling
slice costs 4228 bytes on unix x64, and 1816 bytes on stmhal.
2014-12-27 17:33:30 +00:00
Damien George e37dcaafb4 py: Allow to properly disable builtin "set" object.
This patch makes MICROPY_PY_BUILTINS_SET compile-time option fully
disable the builtin set object (when set to 0).  This includes removing
set constructor/comprehension from the grammar, the compiler and the
emitters.  Now, enabling set costs 8168 bytes on unix x64, and 3576
bytes on stmhal.
2014-12-27 17:33:30 +00:00
Paul Sokolovsky 8ab6f90674 py: Move to guarded includes for compile.h and related headers. 2014-12-27 16:12:17 +02:00
Paul Sokolovsky 343266ea51 showbc: Refactor to allow inline instruction printing. 2014-12-27 05:01:21 +02:00
Damien George c55a4d82cf py: Make bytes objs work with more str methods; add tests. 2014-12-24 20:28:30 +00:00
Damien George 74eb44c392 py: Reduce size of VM exception stack element by 1 machine word.
This optimisation reduces the VM exception stack element (mp_exc_stack_t)
by 1 word, by using bit 1 of a pointer to store whether the opcode was a
FINALLY or WITH opcode.  This optimisation was pending, waiting for
maturity of the exception handling code, which has now proven itself.

Saves 1 machine word RAM for each exception (4->3 words per exception).
Increases stmhal code by 4 bytes, and decreases unix x64 code by 32
bytes.
2014-12-22 12:49:57 +00:00
Damien George 81836c28b3 py: Use str_to_int function in more places to reduce code size. 2014-12-21 21:07:03 +00:00
Damien George 01039b5bd8 py: Remove last uses of printf from compile; use proper SyntaxError. 2014-12-21 17:44:27 +00:00
Damien George 584ba6762f py: Move global/nonlocal decl code to compiler for proper SyntaxError.
This patch gives proper SyntaxError exceptions for bad global/nonlocal
declarations.  It also reduces code size: 304 bytes on unix x64, 132
bytes on stmhal.
2014-12-21 17:26:45 +00:00
Damien George b063b9b36d py: Fix iteration over map in 2 places. 2014-12-21 16:24:09 +00:00
Damien George 6efa66f125 py: Remove unnecessary RULE_none and PN_none from parser. 2014-12-20 18:41:59 +00:00
Damien George b47ea4eadd py: Add blank and ident flags to grammar rules to simplify parser.
This saves around 100 bytes code space on stmhal, more on unix.
2014-12-20 18:37:50 +00:00
Damien George 2870d85a11 py: Save a few code bytes in parser; make vars local where possible. 2014-12-20 18:06:08 +00:00
Damien George 2a3e2b9033 py: Add execfile function (from Python 2); enable in stmhal port.
Adds just 60 bytes to stmhal binary.  Addresses issue #362.
2014-12-19 13:36:17 +00:00
Paul Sokolovsky c0bc3bd736 asmarm: Fix bug with encoding small negative ints using MVN instruction. 2014-12-14 03:24:17 +02:00
Damien George e181c0dc07 py: Fix optimised for-loop compiler so it follows proper semantics.
You can now assign to the range end variable and the for-loop still
works correctly.  This fully addresses issue #565.

Also fixed a bug with the stack not being fully popped when breaking out
of an optimised for-loop (and it's actually impossible to write a test
for this case!).
2014-12-12 17:19:56 +00:00
Damien George 7764f163fa py: Fix label printing in showbc; print sp in vm trace. 2014-12-12 17:18:56 +00:00
Damien George c33ce606cf py: Fix a semantic issue with range optimisation.
Now you can assign to the range variable within the for loop and it will
still work.

Partially addresses issue #565.
2014-12-11 17:35:23 +00:00
Damien George 5318cc028a py: Tidy up a few function declarations. 2014-12-10 22:37:07 +00:00
Damien George 7eb2317fa2 py: Remove static from definition of pfenv_printf.
It's used by stmhal, but not unix.
2014-12-10 22:11:01 +00:00
Damien George 969a6b37bf py: Make functions static where appropriate. 2014-12-10 22:08:14 +00:00
Damien George b4fe6e28eb py: Fix function type: () -> (void). 2014-12-10 18:05:42 +00:00
Damien George 78d702c300 py: Allow builtins to be overridden.
This patch adds a configuration option (MICROPY_CAN_OVERRIDE_BUILTINS)
which, when enabled, allows to override all names within the builtins
module.  A builtins override dict is created the first time the user
assigns to a name in the builtins model, and then that dict is searched
first on subsequent lookups.  Note that this implementation doesn't
allow deleting of names.

This patch also does some refactoring of builtins code, creating the
modbuiltins.c file.

Addresses issue #959.
2014-12-09 16:19:48 +00:00
Paul Sokolovsky 46c3ab2004 modsys: Add sys.print_exception(exc, file=sys.stdout) function.
The function is modeled after traceback.print_exception(), but unbloated,
and put into existing module to save overhead on adding another module.
Compliant traceback.print_exception() is intended to be implemented in
micropython-lib in terms of sys.print_exception().

This change required refactoring mp_obj_print_exception() to take pfenv_t
interface arguments.

Addresses #751.
2014-12-08 20:25:49 +00:00
Damien George be6d8be91e py: Rename mp_obj_int_get to mp_obj_int_get_truncated; fix struct.pack.
mp_obj_int_get_truncated is used as a "fast path" int accessor that
doesn't check for overflow and returns the int truncated to the machine
word size, ie mp_int_t.

Use mp_obj_int_get_truncated to fix struct.pack when packing maximum word
sized values.

Addresses issues #779 and #998.
2014-12-05 23:13:52 +00:00
Damien George 451a087075 py: Fix printing of size_t entity; fix qemu-arm for changes to lexer. 2014-12-05 22:50:16 +00:00
Damien George a4c52c5a3d py: Optimise lexer by exposing lexer type.
mp_lexer_t type is exposed, mp_token_t type is removed, and simple lexer
functions (like checking current token kind) are now inlined.

This saves 784 bytes ROM on 32-bit unix, 348 bytes on stmhal, and 460
bytes on bare-arm.  It also saves a tiny bit of RAM since mp_lexer_t
is a bit smaller.  Also will run a bit more efficiently.
2014-12-05 19:35:18 +00:00
Damien George 32ef3a3517 py: Allow bytes/bytearray/array to be init'd by buffer protocol objects.
Behaviour of array initialisation is subtly different for bytes,
bytearray and array.array when argument has buffer protocol.  This patch
gets us CPython conformant (except we allow initialisation of
array.array by buffer with length not a multiple of typecode).
2014-12-04 15:46:14 +00:00
Damien George cd97a43f8d py, vm: Make unum a local variable for each opcode that uses it.
This makes no change to the generated code, but it's now easier to
understand since unum is not a "global" variable anymore.
2014-12-02 19:25:10 +00:00
Paul Sokolovsky 6e8ff9cd68 modmicropython: Move mem_info() and qstr_info() functions from unix port.
TODO: Merge useful functionality from modpyb too.
2014-12-01 20:42:24 +02:00
Damien George c7ca01ad96 py: Generalise and reduce code size of array +, += and .extend().
By using the buffer protocol for these array operations, we now allow
addition of memoryview objects, and objects with "incompatible"
typecodes (in this case it just adds bytes naively).  This is an
extension to CPython which seems sensible.  It also reduces the code
size.
2014-11-30 14:01:33 +00:00
Damien George b2e731177e py: Implement +, += and .extend for bytearray and array objs.
Addresses issue #994.
2014-11-30 00:00:55 +00:00
Damien George 3b603f29ec Use MP_DEFINE_CONST_DICT macro to define module dicts.
This is just a clean-up of the code.  Generated code is exactly the
same.
2014-11-29 14:39:27 +00:00
Paul Sokolovsky bfdc205934 modubinascii: Add, with hexlify() implementation. 2014-11-29 13:52:47 +00:00
Damien George 2399aa03b8 py: Add NLR support for xtensa CPU. 2014-11-27 20:29:33 +00:00
Paul Sokolovsky df732bb01b pfenv_printf: Properly implement %p format specifier.
Previously, it truncated pointer value to 32 bits on 64-bit systems.
2014-11-27 17:38:03 +00:00
Paul Sokolovsky 1eca32836d builtin: Reimplement __repl_print__() in terms of print().
Before, __repl_print__() used libc printf(), while print() used uPy streams
and own printf() implementation. This led to subtle, but confusing
differences in output when just doing "foo" vs "print(foo)" on interactive
prompt.
2014-11-27 17:38:03 +00:00
Paul Sokolovsky e5dbe1e283 map: Add empty fixed map.
Useful when need to call kw-receiving functions  without any keywords
from C, etc.
2014-11-27 17:37:07 +00:00
Damien George 6f5eb84c19 py: #if guard str_make_new when not needed. 2014-11-27 16:55:47 +00:00
Paul Sokolovsky f4b19c873f moduhashlib: Initial module skeleton. 2014-11-22 14:47:46 +02:00
Damien George c9f8f653ad py: Add support for float/double arrays in array module.
Addresses issue #981.
2014-11-21 18:16:25 +00:00
Damien George 5694cc5490 py: Make stream seek correctly check for ioctl fn; add seek for textio. 2014-11-16 23:56:37 +00:00
Paul Sokolovsky 838eb1fa2d stream: Implement seek operation support via ioctl, wrapped in generic method.
Also, implement for unix port.
2014-11-17 00:16:14 +02:00
Paul Sokolovsky f4a6a577ab stream: Convert .ioctl() to take fixed number of args.
This is more efficient, as allows to use register calling convention.
If needed, a structure pointer can be passed as argument to pass more
data.
2014-11-17 00:16:14 +02:00
stijn e00eeaf4cd py: Use __hash__ method if a type defines it 2014-11-15 18:24:22 +00:00
Sven Wegener b98c162c52 py: Fix order-only dependencies in mkrules.mk and py.mk.
Currently compilation sporadically fails, because the automatic
dependency gets created *during* the compilation of objects.

OBJ is a auperset of PY_O and the dependencies apply to all objects.

Signed-off-by: Sven Wegener <sven.wegener@stealer.net>
2014-11-06 18:48:38 +00:00
Damien George 1e9a92f84f py: Use shorter, static error msgs when ERROR_REPORTING_TERSE enabled.
Going from MICROPY_ERROR_REPORTING_NORMAL to
MICROPY_ERROR_REPORTING_TERSE now saves 2020 bytes ROM for ARM Thumb2,
and 2200 bytes ROM for 32-bit x86.

This is about a 2.5% code size reduction for bare-arm.
2014-11-06 17:36:16 +00:00
Damien George b6b34cd3f6 py: Remove obsolute function declaration. 2014-11-06 17:31:18 +00:00
Sven Wegener bfb18a714b py: Add order-only dependency for py-version.h
Else the directory might not exist.

Signed-off-by: Sven Wegener <sven.wegener@stealer.net>
2014-11-05 22:52:51 +00:00
Sven Wegener 238ab5013b py: Deactivate more code without MICROPY_PY_SYS
When compiler optimization has been turned on, gcc knows that this code
block is not going to be executed. But with -O0 it complains about
path_items being used uninitialized.

Signed-off-by: Sven Wegener <sven.wegener@stealer.net>
2014-11-05 22:52:51 +00:00
Damien George 7860c2a68a py: Fix some macros defines; cleanup some includes. 2014-11-05 21:16:41 +00:00
Damien George be8e99c7d4 py: Allow bytes object as argument to some str methods.
This turns failing assertions to type exceptions for things like
b"123".find(...).  We still don't support operations like this on bytes
objects (unlike CPython), but at least it no longer crashes.
2014-11-05 16:45:54 +00:00
Damien George a65c03c6c0 py: Allow +, in, and compare ops between bytes and bytearray/array.
Eg b"123" + bytearray(2) now works.  This patch actually decreases code
size while adding functionality: 32-bit unix down by 128 bytes, stmhal
down by 84 bytes.
2014-11-05 16:30:34 +00:00
Paul Sokolovsky 346aacf27f unix: fast: Set initial module dict size big to have high pystone score.
For this, introduce MICROPY_MODULE_DICT_SIZE config setting.
2014-11-05 00:30:21 +02:00
Damien George ff319dffad py: Explicitly set uninitialised struct member to false.
Uninitialised struct members get a default value of 0/false, so this is
not strictly needed.  But it actually decreases code size because when
all members are initialised the compiler doesn't need to insert a call
to memset to clear everything.  In other words, setting 1 extra member
to 0 uses less code than calling memset.

ROM savings in bytes: 32-bit unix: 100; bare-arm: 44; stmhal: 52.
2014-11-03 16:18:51 +00:00
Damien George 0344fa1ddf py: Fix builtin callable so it checks user-defined instances correctly.
Addresses issue #953.
2014-11-03 16:09:39 +00:00
Paul Sokolovsky 039887a0ac py: Fix bug with right-shifting small ints by large amounts.
Undefined behavior in C, needs explicit check.
2014-11-02 02:41:30 +02:00
Damien George 109c1de015 py: Make gc.enable/disable just control auto-GC; alloc is still allowed.
gc.enable/disable are now the same as CPython: they just control whether
automatic garbage collection is enabled or not.  If disabled, you can
still allocate heap memory, and initiate a manual collection.
2014-10-31 21:30:46 +00:00
Damien George 1559a97810 py: Add builtin round function.
Addresses issue #934.
2014-10-31 11:28:50 +00:00
stijn 0e557facb9 mpz: Fix 64bit msvc build
msvc does not treat 1L a 64bit integer hence all occurences of shifting it left or right
result in undefined behaviour since the maximum allowed shift count for 32bit ints is 31.
Forcing the correct type explicitely, stored in MPZ_LONG_1, solves this.
2014-10-30 23:00:24 +00:00
Paul Sokolovsky e62a0fe367 objstr: Allow to convert any buffer proto object to str.
Original motivation is to support converting bytearrays, but easier to just
support buffer protocol at all.
2014-10-31 00:03:53 +02:00
Paul Sokolovsky 31619cc589 py: mp_obj_str_get_str(): Work with bytes too.
It should be fair to say that almost in all cases where some API call
expects string, it should be also possible to pass byte string. For example,
it should be open/delete/rename file with name as bytestring. Note that
similar change was done quite a long ago to mp_obj_str_get_data().
2014-10-31 00:00:39 +02:00
Paul Sokolovsky b1422de12f py: Allow to override port config file and thus have >1 configs per port.
Use it like:

make CFLAGS_EXTRA='-DMP_CONFIGFILE="<mpconfigport_my.h>"'
2014-10-29 23:37:45 +00:00
stijn 49c47da804 Fix errors after enabling -Wpointer-arith 2014-10-29 15:42:38 +00:00
Paul Sokolovsky 429e3f077e unix: Make -v dump memory info at exit.
Also, move bytecode dumps to -v -v, because they're too verbose for just -v.
2014-10-26 22:36:56 +00:00
Paul Sokolovsky e503512f83 unix: Implement -m option (execute module from stdlib).
Support for packages as argument not implemented, but otherwise error and
exit handling should be correct. This for example will allow to do:

pip-micropython install micropython-test.pystone
micropython -m test.pystone
2014-10-26 15:54:19 +00:00
Damien George de3c806965 py: Fix memoryview referencing so it retains ptr to original buffer.
This way, if original parent object is GC'd, the memoryview still points
to the underlying buffer data so that buffer is not GC'd.
2014-10-26 13:20:50 +00:00
Damien George b0b0012fd8 py: Fix VM dispatch following a pending exception check. 2014-10-26 00:33:23 +01:00
Damien George 124df6f8d0 py: Add mp_pending_exception global variable, for VM soft interrupt.
This allows to implement KeyboardInterrupt on unix, and a much safer
ctrl-C in stmhal port.  First ctrl-C is a soft one, with hope that VM
will notice it; second ctrl-C is a hard one that kills anything (for
both unix and stmhal).

One needs to check for a pending exception in the VM only for jump
opcodes.  Others can't produce an infinite loop (infinite recursion is
caught by stack check).
2014-10-25 23:37:57 +01:00
Damien George c9fc620723 py: Implement compile builtin, enabled only on unix port.
This should be pretty compliant with CPython, except perhaps for some
corner cases to do with globals/locals context.

Addresses issue #879.
2014-10-25 22:07:25 +01:00
Paul Sokolovsky e5a3759ff5 py: Factor out mp_obj_is_package() function. 2014-10-25 22:31:33 +03:00
Paul Sokolovsky 8becca7c82 py: mp_builtin___import__(): Add const to arg type. 2014-10-25 22:31:26 +03:00
Damien George 8456cc017b py: Compress load-int, load-fast, store-fast, unop, binop bytecodes.
There is a lot potential in compress bytecodes and make more use of the
coding space.  This patch introduces "multi" bytecodes which have their
argument included in the bytecode (by addition).

UNARY_OP and BINARY_OP now no longer take a 1 byte argument for the
opcode.  Rather, the opcode is included in the first byte itself.

LOAD_FAST_[0,1,2] and STORE_FAST_[0,1,2] are removed in favour of their
multi versions, which can take an argument between 0 and 15 inclusive.
The majority of LOAD_FAST/STORE_FAST codes fit in this range and so this
saves a byte for each of these.

LOAD_CONST_SMALL_INT_MULTI is used to load small ints between -16 and 47
inclusive.  Such ints are quite common and now only need 1 byte to
store, and now have much faster decoding.

In all this patch saves about 2% RAM for typically bytecode (1.8% on
64-bit test, 2.5% on pyboard test).  It also reduces the binary size
(because bytecodes are simplified) and doesn't harm performance.
2014-10-25 20:23:13 +01: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 0b13f3e026 py: Improve memory usage debugging; better GC AT dumping.
In unix port, mem_info(1) now prints pretty GC alloc table.
2014-10-24 23:12:25 +01:00
Damien George 564963a170 py: Fix debug-printing of bytecode line numbers.
Also move the raw bytecode printing code from emitglue to mp_bytecode_print.
2014-10-24 14:42:50 +00:00
Damien George d00d8ac95c py: Use mp_uint_t where appropriate in stream functions. 2014-10-24 11:26:12 +00:00
Paul Sokolovsky e2f8d98525 stream: Add optional 2nd "length" arg to .readinto() - extension to CPython.
While extension to file.readinto() definition of CPython, the additional arg
is similar to what in CPython available in socket.recv_into().
2014-10-23 21:43:59 +03:00
Damien George e7bb0443cd py: Properly free string parse-node; add assertion to gc_free. 2014-10-23 14:13:05 +01:00
Damien George dd4f4530ab py: Add builtin memoryview object (mostly using array code). 2014-10-23 13:34:35 +01:00
Damien George 3aa09f5784 py: Use MP_OBJ_NULL instead of NULL in a few places. 2014-10-23 12:06:53 +01:00
Damien George 37378f8a9d py: Clean up edge cases of malloc/realloc/free. 2014-10-23 12:02:00 +01:00
Damien George f5d69794a8 extmod: Add uheapq module. 2014-10-22 23:20:15 +01:00
Damien George e72be1b999 py: Fix smallint modulo with negative arguments.
Addresses issue #927.
2014-10-22 23:05:50 +01:00
Damien George e7a478204a py: Remove unused and unneeded SystemError exception.
It's purpose is for internal errors that are not catastrophic (ie not as
bad as RuntimeError).  Since we don't use it, we don't need it.
2014-10-22 19:42:55 +01:00
Damien George 20f59e182e py: Make mp_const_empty_bytes globally available. 2014-10-21 21:02:56 +01:00
stijn 2fe4cf7761 Implement kwargs for builtin open() and _io.FileIO
This makes open() and _io.FileIO() more CPython compliant.
The mode kwarg is fully iplemented.
The encoding kwarg is allowed but not implemented; mainly to allow
the tests to specify encoding for CPython, see #874
2014-10-21 22:10:01 +03:00
Damien George 21ca2d76a2 py: Partially fix viper multi-comparison; add test for it. 2014-10-19 19:00:51 +01:00
Paul Sokolovsky 1a55b6a787 unix, stmhal: Implement file.readinto() method.
Also, usocket.readinto(). Known issue is that .readinto() should be available
only for binary files, but micropython uses single method table for both
binary and text files.
2014-10-18 22:44:07 +03:00
Damien George b7a4b0f86f py: Improve stream_read so it doesn't need to alloc 2 bits of heap. 2014-10-17 23:34:06 +01:00
Damien George 391db8669b py: Add more compiler optimisations for constant if/while conditions. 2014-10-17 17:57:33 +00:00
Damien George 235f9b33c8 py: Simplify compilation of elif blocks. 2014-10-17 17:30:16 +00:00
Damien George c30595eb1b py: Add more debug printing code in gc_dump_alloc_table. 2014-10-17 14:12:57 +00:00
Damien George 090c9236e8 py: Fix compiling of nested while/for and exception handler.
Addresses issue #912.
2014-10-17 14:08:49 +00:00
Damien George 37ada236b3 py: Take gc_pool_start out of bss section, to reclaim 1st block of heap. 2014-10-16 21:50:39 +01:00
Paul Sokolovsky 923a8a8320 stream: Handle non-blocking errors in readline() properly.
Just like they handled in other read*(). Note that behavior of readline()
in case there's no data when it's called is underspecified in Python lib
spec, implemented to behave as read() - return None.
2014-10-16 12:22:52 +03:00
Paul Sokolovsky 0c7b26c0f8 stream: Return errno value as first arg of OSError exception.
This is CPython-compatible convention established yet in acb13886fc.
2014-10-16 02:58:52 +03:00
Paul Sokolovsky 067ae1269d objclosure: Fix printing of generator closures.
The code previously assumed that only functions can be closed over.
2014-10-16 00:14:01 +03:00
Damien George 9b0b373e5e py: Fix GC realloc issue, where memory chunks were never shrunk.
Previously, a realloc to a smaller memory chunk size would not free the
unused blocks in the tail of the chunk.
2014-10-15 18:24:47 +00:00
Damien George 4859edb95b py: Fix dummy definition of BEGIN/END_ATOMIC_SECTION. 2014-10-15 17:33:24 +00:00
Paul Sokolovsky 911c00bbc5 modzlibd: Remove, superceded by moduzlib. 2014-10-13 14:13:22 +03:00
Damien George 29f5682621 Merge pull request #904 from pfalcon/moduzlib
Module "uzlib" - based on similarly named library
2014-10-12 23:25:24 +01:00
Paul Sokolovsky 34162872b1 moduzlib: Integrate into the system. 2014-10-13 00:07:44 +03:00
Damien George c14a81662c py: Add module weak link support.
With this patch a port can enable module weak link support and provide
a dict of qstr->module mapping.  This mapping is looked up only if an
import fails to find the requested module in the filesystem.

This allows to have the builtin module named, eg, usocket, and provide
a weak link of "socket" to the same module, but this weak link can be
overridden if a file by the name "socket.py" is found in the import
path.
2014-10-12 20:18:40 +01:00
Damien George 3c34d4140d py: Fix x86 viper code generation, mem8 <-> mem16 for load. 2014-10-12 16:10:25 +00:00
Damien George 91cfd414c0 py: Implement native load for viper.
Viper can now do: ptr8(buf)[0], which loads a byte from a buffer using
machine instructions.
2014-10-12 16:59:29 +01:00
Damien George 1ef2348df0 py: Implement and,or,xor native ops for viper. 2014-10-12 14:21:06 +01:00
Paul Sokolovsky 1606607bd4 modure: Make sure that re1.5 compiled in only of modure itself is enabled.
This is achieved by including re1.5 *.c files straight from modure.c .
2014-10-12 03:40:20 +03:00
Paul Sokolovsky c71e045165 modure: Initial module, using re1.5 (which is based on re1 codebase).
https://github.com/pfalcon/re1.5
2014-10-11 14:36:32 +03:00
Damien George 9bf5f2857d py: Add further checks for failed malloc in lexer init functions. 2014-10-09 16:53:37 +01:00
Damien George a8202762f0 Merge branch 'lexer-crash' of https://github.com/dhylands/micropython into dhylands-lexer-crash 2014-10-09 16:48:55 +01:00
Damien George 4091445612 py: Add #if guard around gc-specific code. 2014-10-09 16:44:43 +01:00
Dave Hylands e20cbbec73 Make lexer fail gracefully when memory can't be allocated. 2014-10-08 23:17:35 -07:00
Dave Hylands 3556e45711 Allow real memory errors (from locked gc) to be reported with traceback. 2014-10-07 08:07:49 -07:00
Damien George f32498fe04 py: Extra autodetect for little endianness using __LITTLE_ENDIAN__. 2014-10-06 16:09:31 +00:00
Damien George 9336ee320a py: Make mp_binary_set_val work on big endian machine. 2014-10-06 15:05:35 +00:00
Damien George fcdb239815 py: Make int.to_bytes work on big endian machine.
Partly addresses issue #856.
2014-10-06 13:45:34 +00:00
Damien George a9bcd51dc7 py: Try to autodetect machine endianness when not defined by port. 2014-10-06 13:44:59 +00:00
Damien George c4d0868df1 py: Implement proper context save/restore for eval/exec; factor code.
This has benefits all round: code factoring for parse/compile/execute,
proper context save/restore for exec, allow to sepcify globals/locals
for eval, and reduced ROM usage by >100 bytes on stmhal and unix.

Also, the call to mp_parse_compile_execute is tail call optimised for
the import code, so it doesn't increase stack memory usage.
2014-10-05 20:13:34 +01:00
Damien George a91ac2011f py: Make compiler return a proper exception on SyntaxError. 2014-10-05 19:01:34 +01:00
Fabian Vogt e5268963c6 Implement missing ARM emitter functions for viper 2014-10-04 00:57:21 +02:00
Damien George 00be7a849a py: Fix unix-cpy to compile with uint->mp_uint_t changes. 2014-10-03 20:05:44 +01:00
Damien George 39dc145478 py: Change [u]int to mp_[u]int_t in qstr.[ch], and some other places.
This should pretty much resolve issue #50.
2014-10-03 19:52:22 +01:00
Damien George 3eaa0c3833 py: Use UINT_FMT instead of %d. 2014-10-03 17:54:25 +00:00
Damien George 42f3de924b py: Convert [u]int to mp_[u]int_t where appropriate.
Addressing issue #50.
2014-10-03 17:44:14 +00:00
Damien George 8b03d944e2 py: Remove IOError since it's deprecated; use OSError instead.
In CPython IOError (and EnvironmentError) is deprecated and aliased to
OSError.  All modules that used to raise IOError now raise OSError (or a
derived exception).

In Micro Python we never used IOError (except 1 place, incorrectly) and
so don't need to keep it.

See http://legacy.python.org/dev/peps/pep-3151/ for background.
2014-09-30 13:59:30 +00:00
Damien George 1c6a1dc740 py: Allow x86-64 to mov r16 to rm16 with extended src reg.
Fixes bug with x86-64 viper ptr16.
2014-09-29 22:44:18 +01:00
Damien George dfef4249eb py: Fix viper store on x86; add tests for viper ptr16. 2014-09-29 21:41:41 +00:00
Damien George e9dac3b4d0 py: Add casting to viper; add native mem stores to viper.
Viper can now do the following:

def store(p:ptr8, c:int):
    p[0] = c

This does a store of c to the memory pointed to by p using a machine
instructions inline in the code.
2014-09-29 22:10:41 +01:00
Damien George 3112cde900 py: Implement more binary ops for viper emitter.
This included a bit of restructuring of the assembler backends.  Note
that the ARM backend is missing a few functions and won't compile.
2014-09-29 19:42:06 +01:00
Damien George 6f81348fa2 py: Allow viper to use ints as direct conditionals in jumps.
Allows things like: if 1: ...
2014-09-29 19:42:06 +01:00
Damien George a7329615eb py: Fix types, uint -> mp_uint_t. 2014-09-29 19:42:06 +01:00
Damien George 0b610de017 py: Make macro names in assemblers consistent, and tidy up a bit. 2014-09-29 19:42:06 +01:00
Damien George d66e48662b py: Add store r8 and store r16 ops to asm_x86 and asm_x64. 2014-09-29 19:42:06 +01:00
Damien George 851f15f34c py: In asmthumb, clean up unit/int types and ite ops. 2014-09-29 19:42:06 +01:00
bvernoux f6f248b464 Fix error: unknown type name 'size_t' 2014-09-28 09:54:35 +02:00
Damien George 133b083b89 py: Clean up nlr*.S to make it easier to read; fix clang .bss error.
It seems that newer versions of clang don't like the .bss directive, so
we don't use it for OSX.

Addressing issues #865 and #875.
2014-09-26 13:07:26 +00:00
Damien George cde0ca21bf py: Simplify JSON str printing (while still conforming to JSON spec).
The JSON specs are relatively flexible and allow us to use one function
to print strings, be they ascii, bytes or utf-8 encoded.
2014-09-25 17:35:56 +01:00
Damien George 4bcd04bcad py: Tidy up exception matching; allow matching of tuple of exceptions.
Addresses issue #864.
2014-09-25 15:49:26 +01:00
Damien George b0261341d3 py: For malloc and vstr functions, use size_t exclusively for int type.
It seems most sensible to use size_t for measuring "number of bytes" in
malloc and vstr functions (since that's what size_t is for).  We don't
use mp_uint_t because malloc and vstr are not Micro Python specific.
2014-09-25 15:49:26 +01:00
Damien George 52b5d76a6b py: Free non-interned strings in the parser when not needed.
mp_parse_node_free now frees the memory associated with non-interned
strings.  And the parser calls mp_parse_node_free when discarding a
non-used node (such as a doc string).

Also, the compiler now frees the parse tree explicitly just before it
exits (as opposed to relying on the caller to do this).

Addresses issue #708 as best we can.
2014-09-23 15:31:56 +00:00
Damien George d6230f62c7 py: Make native emitter handle multi-compare and not/is not/not in ops. 2014-09-23 14:15:45 +00:00
Damien George eaaebf3291 stmhal: Initialise stack pointer correctly.
Stack is full descending and must be 8-byte aligned.  It must start off
pointing to just above the last byte of RAM.

Previously, stack started pointed to last byte of RAM (eg 0x2001ffff)
and so was not 8-byte aligned.  This caused a bug in combination with
alloca.

This patch also updates some debug printing code.

Addresses issue #872 (among many other undiscovered issues).
2014-09-23 10:59:05 +01:00
Damien George 89e4657c69 extmod: Add loads to ujson module. 2014-09-21 23:43:03 +01:00