Compare commits

...

532 Commits
v1.0 ... v1.2

Author SHA1 Message Date
Paul Sokolovsky
dce8876dbe unix: file: No fsync() on Windows. 2014-07-13 23:34:35 +03:00
Paul Sokolovsky
ac736f15c9 stream: Factor out mp_stream_write() method to write a memstring to stream. 2014-07-13 23:14:32 +03:00
Paul Sokolovsky
122c9db3db unix: file: Implement .flush() method.
This method apparently should be part of stream interface.
2014-07-13 23:14:24 +03:00
Paul Sokolovsky
a1760a56ff test: Add run-tests-exp.sh, script to run testsuite with only sh dependency.
This script uses expected test results as generated by run-tests --write-exp,
and requires only standard unix shell funtionality (no bash). It is useful
to run testsuite on embedded systems, where there's no CPython and Bash.
2014-07-13 18:49:56 +03:00
Paul Sokolovsky
b82f34edde unix: Allow to disable MICROPY_EMIT_X64 from commandline.
emitnative in particular requires nlr_* to be real functions, so doesn't
compile with MICROPY_NLR_SETJMP=1.
2014-07-13 13:49:51 +03:00
Paul Sokolovsky
2cf381081a run-tests: Add option to write CPython's test results to .exp files.
Mostly to run testsuite on targets which doesn't have CPython.
2014-07-12 16:34:51 +03:00
Paul Sokolovsky
564e46452d py: Add generic helper to align a pointer. 2014-07-12 15:57:28 +03:00
Paul Sokolovsky
58c9586c34 emitbc: Fix structure field alignment issue.
dummy_data field is accessed as uint value (e.g.
in emit_write_bytecode_byte_ptr), but is not aligned as such, which causes
bus errors or incorrect behavior on any arch requiring strictly aligned
data (ARM pre-v7, MIPS, etc, etc).
2014-07-12 15:57:28 +03:00
Paul Sokolovsky
2097c8b1e1 moductypes: Add symbolic constants to specify bitfield position/length. 2014-07-11 00:06:36 +03:00
Paul Sokolovsky
8215847b4d moductypes: Foreign data interface module, roughly based on ctype ideas.
But much smaller and memory-efficient. Uses Python builtin data structures
(dict, tuple, int) to describe structure layout.
2014-07-09 19:28:24 +03:00
Damien George
42b6419056 Merge branch 'dhylands-fix-sdcard-read' 2014-07-07 07:29:52 +01:00
Damien George
594699bc88 stmhal: Protect SD_WriteBlocks by IRQ disable/enable pair. 2014-07-07 07:29:06 +01:00
Dave Hylands
90ba80dc36 Disable IRQs around sdcard reads.
Once the code switches to using DMA, this can be removed.
2014-07-06 09:51:22 -07:00
Paul Sokolovsky
5fa5ca40e6 binary: Factor out mp_binary_set_int(). 2014-07-05 23:54:03 +03:00
Damien George
539681fffd tests: Rename test scripts, changing - to _ for consistency.
From now on, all new tests must use underscore.

Addresses issue #727.
2014-07-05 06:14:29 +01:00
Damien George
0182385ab0 py: Automatically ake __new__ a staticmethod.
Addresses issue #622.
2014-07-05 05:55:00 +01:00
Paul Sokolovsky
4e0eeebdc2 py: Implement sys.maxsize, standard way to check platform "bitness".
Implementing it as a static constant is a bit peculiar and require cooperation
from long int implementation.
2014-07-03 18:09:36 +03:00
Damien George
381618269a parser: Convert (u)int to mp_(u)int_t. 2014-07-03 14:13:33 +01:00
Damien George
54eb4e723e lexer: Convert type (u)int to mp_(u)int_t. 2014-07-03 13:47:47 +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
Damien George
065aba5875 Merge pull request #739 from errordeveloper/patch-1
qemu: fix typo in readme
2014-07-03 10:16:07 +01:00
Ilya Dmitrichenko
e4e55047b3 qemu: fix typo in readme 2014-07-02 18:30:46 +01:00
Damien George
5e6419cb11 Merge branch 'dhylands-add-timer-deinit' 2014-07-02 14:10:18 +01:00
Damien George
e70b5dbe58 stmhal: Some reordering of code/functions. 2014-07-02 14:09:44 +01:00
Damien George
92a47b4dae Merge branch 'add-timer-deinit' of github.com:dhylands/micropython into dhylands-add-timer-deinit 2014-07-02 14:06:28 +01:00
Damien George
9cd96cf25d Merge pull request #709 from windelbouwman/master
Added hexfile target
2014-07-02 13:53:28 +01:00
Damien George
f83debc716 Merge branch 'dhylands-teensy-new' 2014-07-02 13:45:00 +01:00
Damien George
7a37f647a5 Merge branch 'teensy-new' of github.com:dhylands/micropython into dhylands-teensy-new
Conflicts:
	stmhal/pin_named_pins.c
	stmhal/readline.c

Renamed HAL_H to MICROPY_HAL_H.  Made stmhal/mphal.h which intends to
define the generic Micro Python HAL, which in stmhal sits above the ST
HAL.
2014-07-02 13:42:37 +01:00
Damien George
5fc580475f Merge branch 'dhylands-preserve-except' 2014-07-01 14:28:40 +01:00
Damien George
f0b29729aa py, objexcept: Only check for locked gc if gc is enabled. 2014-07-01 14:28:09 +01:00
Damien George
f065344d3b Merge branch 'preserve-except' of github.com:dhylands/micropython into dhylands-preserve-except 2014-07-01 14:26:37 +01:00
Damien George
aa47f3968b Merge pull request #734 from iabdalkader/copysign
Add copysignf
2014-07-01 14:03:55 +01:00
Dave Hylands
2fe841d2fa Try not to cause a MemoryError when raising an exception during nterrupt handling.
Step 1 fixes #732
2014-06-30 22:49:21 -07:00
Paul Sokolovsky
caa7334141 stackctrl: Add "mp_" prefix. 2014-07-01 02:14:08 +03:00
Paul Sokolovsky
e95b6b5e07 modffi: Add special 'C' code for passing a callback function pointer. 2014-07-01 02:05:34 +03:00
Dave Hylands
0d81c133b3 Add timer_deinit and call it just before doing a soft-restart
This fixes #733.
2014-06-30 08:07:38 -07:00
mux
5d44e6a92c Add copysignf
* Fix #692
2014-06-30 16:31:06 +02:00
Damien George
4039a26679 Merge pull request #710 from iabdalkader/assert
Fix assert_func warning/error
2014-06-30 09:09:24 +01:00
Damien George
b601d9574a py: Improvements to native emitter.
Native emitter can now compile try/except blocks using nlr_push/nlr_pop.
It probably only works for 1 level of exception handling.  It doesn't
work on Thumb (only x64).

Native emitter can also handle some additional op codes.

With this patch, 198 tests now pass using "-X emit=native" option to
micropython.
2014-06-30 05:17:25 +01:00
Paul Sokolovsky
5813efd634 stmhal: pyb.adc: Clarify that buffer with elements of any size can be used.
Based on forum post: http://forum.micropython.org/viewtopic.php?f=6&t=193
2014-06-29 21:34:58 +03:00
Paul Sokolovsky
bb35f425f9 Merge pull request #730 from stinos/windows-mpconfig
windows: Sync mpconfigport.h with the unix' version
2014-06-29 20:50:16 +03:00
Paul Sokolovsky
c10a4405cd gendoc.py: Support modules w/o functions and/or classes.
I.e. don't assume that both are always present.
2014-06-29 15:48:30 +03:00
Paul Sokolovsky
a23475979b modffi: Support short types. 2014-06-29 15:48:21 +03:00
stijn
ec6fa8732b windows: Sync mpconfigport.h with the unix' version
- rearrange/add definitions that were not there so it's easier to compare both
- use MICROPY_PY_SYS_PLATFORM in main.c since it's available anyway
- define EWOULDBLOCK, it is missing from ingw32
2014-06-29 09:40:20 +02:00
Paul Sokolovsky
8139494e54 stmhal: Include mpconfig.h before all other includes.
It defines types used by all other headers.

Fixes #691.
2014-06-28 23:32:03 +03:00
Paul Sokolovsky
9e215fa4c2 py: Make unichar_charlen() accept/return machine_uint_t. 2014-06-28 23:15:29 +03:00
Paul Sokolovsky
a62da515af Merge pull request #729 from stinos/fix-include-order
unix: Fix mpconfig.h not being included before misc.h
2014-06-28 23:12:39 +03:00
stijn
5478ed18ea unix: Fix mpconfig.h not being included before misc.h
This fixes count_lead_ones in misc.h not compiling due to unknown types
2014-06-28 20:49:39 +02:00
Damien George
b1b840554d Merge branch 'unicode' 2014-06-28 10:30:53 +01:00
Damien George
635b60e299 unix, stmhal: Add option for STR_UNICODE to mpconfigport.h.
Unicode is disabled by default for now, since FileIO.read(n) is
currently not implemented for text-mode files, and this is an
often function.
2014-06-28 10:29:52 +01:00
Damien George
8546ce1e28 py: Add missing #endif. 2014-06-28 10:29:22 +01:00
Damien George
41736f8201 tests: Write output in byte mode, not text mode.
This enables testing unicode and non-unicode implementations.
2014-06-28 10:29:12 +01:00
Damien George
e04a44e2f6 py: Small comments, name changes, use of machine_int_t. 2014-06-28 10:27:23 +01:00
Damien George
b3a50f0f3e Merge branch 'master' into unicode
Conflicts:
	py/mpconfig.h
2014-06-28 10:27:15 +01:00
Paul Sokolovsky
8993fb6cf0 py: Add protection against printing too nested or recursive data structures.
With a test which cannot be automatically validated so far.
2014-06-28 02:25:04 +03:00
Paul Sokolovsky
7e4ec3bf4f bare-arm: Hint of setting MICROPY_ERROR_REPORTING to REPORTING_TERSE.
Commented out so far, as enabled leads to dozen more bytes used actually
(due to string pooling effects).
2014-06-27 21:02:04 +03:00
Paul Sokolovsky
81df1e6c98 bare-arm: Disable array module and even bytearray type.
To squeeze few more hundreds of bytes.
2014-06-27 21:02:04 +03:00
Paul Sokolovsky
cb78f862cb py: Allow to disable array module and bytearray type.
array.array and bytearray share big deal of code, so to get real savings,
both need to be disabled.
2014-06-27 21:02:04 +03:00
Paul Sokolovsky
0a1ea40273 bare-arm: Enable link map file.
This port supposed to be demo of uPy minimality, so let people behold it in
details.
2014-06-27 21:02:04 +03:00
Paul Sokolovsky
8a96ebea75 py: Move stack_ctrl_init() to mp_init().
As stack checking is enabled by default, ports which don't call
stack_ctrl_init() are broken now (report RuntimeError on startup). Save
them trouble and just init stack control framework in interpreter init.
2014-06-27 21:02:04 +03:00
Paul Sokolovsky
64c58403ef Merge pull request #720 from iabdalkader/mcu_name
Change MCU name config micro
2014-06-27 18:24:32 +03:00
mux
a75e382a9b Change MCU name config micro 2014-06-27 00:35:53 +02:00
Paul Sokolovsky
3c8ce38d20 Merge pull request #717 from stinos/dead-code
unix: Remove unused CTRL-D definition
2014-06-27 01:23:18 +03:00
Paul Sokolovsky
3659af97c5 Merge pull request #703 from iabdalkader/micro_names
Add MICROPY_HW_MICRO_NAME to boards config
2014-06-27 01:19:17 +03:00
Paul Sokolovsky
ed07d035d5 tests: Add basic test for unicode file i/o. 2014-06-27 00:04:20 +03:00
Paul Sokolovsky
f5f6c3b792 streams: Reading by char count from unicode text streams is not implemented. 2014-06-27 00:04:20 +03:00
Paul Sokolovsky
ce81312d8a misc: Add count_lead_ones() function, useful for UTF-8 handling. 2014-06-27 00:04:20 +03:00
Paul Sokolovsky
63143c94ce tests: Test for explicit start/end args to str methods for unicode. 2014-06-27 00:04:20 +03:00
Paul Sokolovsky
ea2c936c7e objstrunicode: Refactor str_index_to_ptr() following objstr. 2014-06-27 00:04:20 +03:00
Paul Sokolovsky
26fda6dc8e objstr: 64-bit issues. 2014-06-27 00:04:19 +03:00
Paul Sokolovsky
00c904b47a objstrunicode: Signedness issues. 2014-06-27 00:04:19 +03:00
Paul Sokolovsky
1044c3dfe6 unicode: Make get_char()/next_char()/charlen() be 8-bit compatible.
Based on config define.
2014-06-27 00:04:19 +03:00
Paul Sokolovsky
b1949e4c09 tests: Add tests for unicode find()/rfind()/index(). 2014-06-27 00:04:19 +03:00
Paul Sokolovsky
5048df0b7c objstr: find(), rfind(), index(): Make return value be unicode-aware. 2014-06-27 00:04:19 +03:00
Paul Sokolovsky
46d31e9ca9 unicode: Add utf8_ptr_to_index().
Useful when we have pointer to char inside string, but need to return char
index. (E.g. str.find()).
2014-06-27 00:04:19 +03:00
Paul Sokolovsky
ded0fc77f7 py: Add dedicated unicode header. 2014-06-27 00:04:19 +03:00
Paul Sokolovsky
17994d1bd3 tests: Add test for unicode string iteration. 2014-06-27 00:04:19 +03:00
Paul Sokolovsky
79b7fe2ee5 objstrunicode: Implement iterator. 2014-06-27 00:04:19 +03:00
Paul Sokolovsky
cdc020da4b objstrunicode: Re-add buffer protocol back for now, required for io.StringIO. 2014-06-27 00:04:18 +03:00
Paul Sokolovsky
e7f2b4c875 objstrunicode: Revamp len() handling for unicode, and optimize bool(). 2014-06-27 00:04:18 +03:00
Paul Sokolovsky
86d3898e70 objstrunicode: Get rid of bytes checking, it's separate type. 2014-06-27 00:04:18 +03:00
Paul Sokolovsky
d215ee1dc1 py: Make MICROPY_PY_BUILTINS_STR_UNICODE=1 buildable. 2014-06-27 00:04:18 +03:00
Paul Sokolovsky
9731912ccb py: Prune unneeded code from objstrunicode, reuse code in objstr. 2014-06-27 00:04:18 +03:00
Paul Sokolovsky
165eb69b86 vstr: Restore bytestr compatibility. 2014-06-27 00:04:18 +03:00
Paul Sokolovsky
42a52516fe builtin: Restore bytestr compatibility. 2014-06-27 00:04:18 +03:00
Chris Angelico
2ba2299d28 lexer, vstr: Add unicode support. 2014-06-27 00:04:18 +03:00
Chris Angelico
1e3781bc35 tests: Add unicode test. 2014-06-27 00:04:17 +03:00
Chris Angelico
9a1a4beb56 builtin: ord, chr: Unicode support. 2014-06-27 00:04:17 +03:00
Chris Angelico
64b468d873 objstrunicode: Basic implementation of unicode handling.
Squashed commit of the following:

commit 99dc21b67a
Author: Chris Angelico <rosuav@gmail.com>
Date:   Thu Jun 12 02:18:54 2014 +1000

    Optimize as per TODO (thanks Damien!)

commit 5bf0153eca
Author: Chris Angelico <rosuav@gmail.com>
Date:   Tue Jun 10 08:42:06 2014 +1000

    Test a default (= UTF-8) encode and decode

commit c962057ac3
Merge: e2c9782 195de32
Author: Chris Angelico <rosuav@gmail.com>
Date:   Tue Jun 10 05:23:03 2014 +1000

    Merge branch 'master' into unicode, resolving conflict on py/obj.h

commit e2c9782a65
Author: Chris Angelico <rosuav@gmail.com>
Date:   Tue Jun 10 05:05:57 2014 +1000

    More whitespace fixups

commit 086a2a0f57
Author: Chris Angelico <rosuav@gmail.com>
Date:   Tue Jun 10 05:04:20 2014 +1000

    Properly implement string slicing

commit 0d339a143e
Author: Chris Angelico <rosuav@gmail.com>
Date:   Tue Jun 10 02:24:11 2014 +1000

    Support slicing in str_index_to_ptr, and fix a bounds error

commit 24371c7267
Author: Chris Angelico <rosuav@gmail.com>
Date:   Tue Jun 10 02:10:22 2014 +1000

    Break out index-to-pointer calculation into a function

commit 616c24ac01
Author: Chris Angelico <rosuav@gmail.com>
Date:   Tue Jun 10 02:03:11 2014 +1000

    Add tests of string slicing, which currently fail

commit a24d19f676
Author: Chris Angelico <rosuav@gmail.com>
Date:   Tue Jun 10 01:56:53 2014 +1000

    Change string indexing to not precalculate the charlen, and add test for neg indexing

commit 0bcc7ab89e
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sun Jun 8 22:09:17 2014 +1000

    Clean up constant qstr declarations now that charlen isn't needed

commit 5473e1a1db
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sun Jun 8 07:18:42 2014 +1000

    Remove the charlen field from strings, calculating it when required

commit 5c1658ec71
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sun Jun 8 07:11:27 2014 +1000

    Get rid of mp_obj_str_get_data_len() which was used in only one place

commit a019ba968b
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sun Jun 8 06:58:26 2014 +1000

    Add a unichar_charlen() function to calculate length-in-characters from length-in-bytes

commit 44b0d5cff8
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sun Jun 8 06:32:44 2014 +1000

    Use utf8_get/next_char in building up a string's repr

commit 30d1bad33f
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sun Jun 8 06:10:45 2014 +1000

    Make utf8_get_char() and utf8_next_char() actually do what their names say

commit bc990dad9a
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sun Jun 8 02:10:59 2014 +1000

    Revert "Add PEP 393-flags to strings and stub usage."

    This reverts commit c239f50952.

commit f9bebb28ad
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sat Jun 7 15:41:48 2014 +1000

    Whitespace fixes

commit 279de0c8eb
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sat Jun 7 15:28:35 2014 +1000

    Formatting/layout improvements - introduce macros for UTF-8 byte detection, add braces. No functional changes.

commit f1911f53d5
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sat Jun 7 11:56:02 2014 +1000

    Make chr() Unicode-aware

commit f51ad737b4
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sat Jun 7 11:44:07 2014 +1000

    Make a string's repr Unicode-aware

commit 01bd686846
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sat Jun 7 11:33:43 2014 +1000

    Expand the Unicode tests

commit 7bc91904f8
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sat Jun 7 11:27:30 2014 +1000

    Record byte lengths for byte strings

commit bb13212071
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sat Jun 7 11:25:06 2014 +1000

    Make ord() Unicode-aware

commit 03f0cbe905
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sat Jun 7 10:24:35 2014 +1000

    Retain characters as UTF-8 encoded Unicode

commit e924659b85
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sat Jun 7 08:37:27 2014 +1000

    Add support for \u and \U escapes, but not \N (with explanatory comment)

commit 231031ac5f
Author: Chris Angelico <rosuav@gmail.com>
Date:   Sat Jun 7 05:09:35 2014 +1000

    Add character length to qstr

commit 6df1b946fb
Author: Chris Angelico <rosuav@gmail.com>
Date:   Fri Jun 6 13:48:36 2014 +1000

    Add test of UTF-8 encoded source file resulting in properly formed string

commit 16429b81a8
Author: Chris Angelico <rosuav@gmail.com>
Date:   Fri Jun 6 13:44:15 2014 +1000

    Make len(s) return character length (even though creation's still buggy)

commit cd2cf6663c
Author: Chris Angelico <rosuav@gmail.com>
Date:   Fri Jun 6 13:15:36 2014 +1000

    HACK - When indexing a qstr, count its charlen. Stupidly inefficient but POC.

    All tests pass now, though string creation is still buggy.

commit 47c234584d
Author: Chris Angelico <rosuav@gmail.com>
Date:   Fri Jun 6 13:15:32 2014 +1000

    objstr: Record character length separately from byte length

    CAUTION: Buggy, may crash stuff - qstr needs equivalent functionality too

commit b0f41c72af
Author: Chris Angelico <rosuav@gmail.com>
Date:   Fri Jun 6 05:37:36 2014 +1000

    Beginnings of UTF-8 support - construct strings from that many UTF-8-encoded chars, and subscript bytes the same way

commit 89452be641
Author: Chris Angelico <rosuav@gmail.com>
Date:   Fri Jun 6 05:28:47 2014 +1000

    Update comments - now aiming for UTF-8 rather than PEP 393 strings

commit c239f50952
Author: Chris Angelico <rosuav@gmail.com>
Date:   Wed Jun 4 05:28:12 2014 +1000

    Add PEP 393-flags to strings and stub usage.

    The test suite all passes, but nothing has actually been changed.
2014-06-27 00:04:17 +03:00
Paul Sokolovsky
83865347db objstrunicode: Complete copy of objstr, to be patched for unicode support. 2014-06-27 00:04:17 +03:00
Chris Angelico
c88987c1af py: Implement basic unicode functions. 2014-06-27 00:04:17 +03:00
Paul Sokolovsky
12bc13eeb8 mpconfig.h: Add MICROPY_PY_BUILTINS_STR_UNICODE. 2014-06-27 00:04:17 +03:00
Paul Sokolovsky
16ac4962ae tests: Add test for catching infinite function recursion.
Put into misc/ to not complicate life for builds with check disabled.
2014-06-27 00:03:56 +03:00
Paul Sokolovsky
7a8ab5a730 stmhal: Use stackctrl framework. 2014-06-27 00:03:55 +03:00
Paul Sokolovsky
23668698cb py: Add portable framework to query/check C stack usage.
Such mechanism is important to get stable Python functioning, because Python
function calling is handled with C stack. The idea is to sprinkle
STACK_CHECK() calls in places where there can be C recursion.

TODO: Add more STACK_CHECK()'s.
2014-06-27 00:03:55 +03:00
Paul Sokolovsky
91b576d147 Merge pull request #719 from dhylands/pin_fix
Use mp_const_none to initialize mapper and map_dict (fix #701)
2014-06-26 22:49:44 +03:00
Dave Hylands
f170735b73 Use mp_const_none to initialize mapper and map_dict 2014-06-25 16:01:19 -07:00
Paul Sokolovsky
f3de62e6c2 binary: machine_uint_t vs uint dichotomy starts doing real damage. 2014-06-26 00:41:08 +03:00
Paul Sokolovsky
8e01291c18 travis: Use unified diffs for failed tests. 2014-06-26 00:05:53 +03:00
Paul Sokolovsky
7a2f166949 modstruct: Fix alignment handling issues.
Also, factor out mp_binary_get_int() function.
2014-06-25 23:34:44 +03:00
stijn
39b6e27944 unix: Remove unused CTRL-D definition 2014-06-25 13:33:10 +02:00
Paul Sokolovsky
5aa740c3e2 modgc: Add mem_free()/mem_alloc() methods.
Return free/allocated memory on GC heap.
2014-06-25 14:28:11 +03:00
Damien George
e973acde81 Merge branch 'master' of github.com:micropython/micropython 2014-06-25 04:10:34 +01:00
Paul Sokolovsky
939c2e7f44 Merge pull request #690 from stinos/msvc-gc
msvc: Enable GC
2014-06-24 21:34:51 +03:00
Paul Sokolovsky
3c9b24bebe modsocket: Fix uClibc detection. 2014-06-24 21:20:38 +03:00
Paul Sokolovsky
141df2d350 unix: Dump default heap size in usage message. 2014-06-24 16:58:00 +03:00
Damien George
780e54cdc3 py: Implement delete_attr in native emitter. 2014-06-22 18:35:04 +01:00
Paul Sokolovsky
cd590cbfaa unix: Don't error out on #warning directive. 2014-06-22 19:20:55 +03:00
Paul Sokolovsky
ff5932a8d8 modsocket: Workaround uClibc issue with numeric port for getaddrinfo().
It sucks to workaround this on uPy side, but upgrading not upgradable
embedded systems sucks even more.
2014-06-22 19:20:55 +03:00
Paul Sokolovsky
949a49c9da modsocket: Add call to freeaddrinfo(). 2014-06-22 19:11:34 +03:00
Paul Sokolovsky
69d0a1c540 unix: uClibc doesn't like NULL as a buffer arg to realpath().
So, allocate one explicitly.
2014-06-22 19:08:32 +03:00
stijn
de5ce6d461 gc: Use simple cast instead of union to silence compiler 2014-06-22 11:32:32 +02:00
stijn
8abcf666cb windows: Enable GC and implement bss start and end symbols
The pointers to the bss section are acquired in init.c()
by inspecting the PE header. Works for msvc and mingw.
2014-06-22 11:31:16 +02:00
Paul Sokolovsky
a96cc824bd py: Support arm and thumb ARM ISAs, in addition to thumb2.
These changes were tested with QEMU, and by few people of real hardware.
2014-06-22 01:40:45 +03:00
Paul Sokolovsky
59c675a64c py: Include mpconfig.h before all other includes.
It defines types used by all other headers.

Fixes #691.
2014-06-21 22:43:22 +03:00
mux
89b38d96c9 Add NORETURN to __fatal_error 2014-06-21 18:43:44 +02:00
mux
5c8db48541 Fix asser_func warning/error
* Add while(1) to assert_func to avoid func returns warning
* Define a weak attr in mpconfig.h
2014-06-21 17:24:55 +02:00
Paul Sokolovsky
4c4b9d15ab mkrules.mk: Pass $(COPT) to link stage.
In generalize case, optimization options should be passed to all stages of
the build process.
2014-06-20 23:49:30 +03:00
Paul Sokolovsky
0fc7efb663 makefile: Pass STRIPFLAGS_EXTRA to strip.
Expected to be set on command line, with the idea being that for different
targets, there're different smartass ABIs which strive to put unneeded
sections into executables, etc., so let people have flexible way to
strip that.

The option name is similar to previously introduced CLFAGS_EXTRA &
LDFLAGS_EXTRA.
2014-06-20 20:25:35 +03:00
Paul Sokolovsky
17a49431d4 unix: Allow to override MICROPY_GCREGS_SETJMP from cmdline. 2014-06-20 20:22:31 +03:00
Paul Sokolovsky
7cd46a12ae unix: Add CFLAGS_EXTRA & LDFLAGS_EXTRA for command line usage.
The idea is that it should be possible to pass any additional params for
experimentation without need to patch sources (and without need to deviate
from or repeat baseline options).
2014-06-20 20:21:21 +03:00
Paul Sokolovsky
7e56e55252 unix: Refactor order file munging fo MacOSX. 2014-06-20 20:21:11 +03:00
Paul Sokolovsky
eecf3e90c6 unix: Group CFLAGS related stuff together. 2014-06-20 20:21:11 +03:00
Paul Sokolovsky
2099b6897f unix: Allow to override compiler warning options without touching the rest.
Some people want to enable even more warnings. Let them do it without putting
burden on everyone. Some people vice versa think that current settings should
be relaxed. In this regard, -Werror is the most problematic, it disallows to
use #warning directive, and disallows to pass configuration settings on make
command lines. Again, until decided how to deal with these globally, allow to
work around these problems locally.
2014-06-20 20:18:08 +03:00
Paul Sokolovsky
f605172d2b tests/float/: Skip tests if "math" module is not available. 2014-06-20 18:00:23 +03:00
Paul Sokolovsky
3b6f7b95eb py: Separate MICROPY_PY_BUILTINS_COMPLEX from MICROPY_PY_BUILTINS_FLOAT.
One thing is wanting to do 1 / 2 and get something else but 0, and quite
another - doing rocket science ;-).
2014-06-20 18:00:23 +03:00
Paul Sokolovsky
7efbd325bb Merge pull request #697 from stinos/gc-debug
gc: More verbose debugging
2014-06-20 17:33:02 +03:00
Paul Sokolovsky
09e3f8f0d1 Merge pull request #707 from eblot/master-v1.1.1-build-fixes
Fix missing declaration of assert()
Replace ARRAY_SIZE with MP_ARRAY_SIZE
2014-06-20 17:29:58 +03:00
Windel Bouwman
b6af4c8104 Added hexfile target 2014-06-20 16:14:55 +02:00
Paul Sokolovsky
74c710187c bench: Three ways to process a byte buffer. 2014-06-19 22:27:13 +03:00
Paul Sokolovsky
59ced651b5 bench: Add test for map() vs inplace operations in array-likes.
map() is 5 times slower. That's mostly because of inefficiency of creating
containers from iterables of unknown length (like map()).
2014-06-19 22:19:24 +03:00
Paul Sokolovsky
17db096505 bench: Add tests for constructing various containers from iterator.
Both "bound" (like, length known) and "unbound" (length unknown) are tested.
All of list, tuple, bytes, bytesarray offer approximately the same
performance, with "unbound" case being 30 times slower.
2014-06-19 21:44:33 +03:00
Paul Sokolovsky
e53d2197e4 bench: Add test for function call overhead.
For a trivial operation, calling a function is 5 times slower than doing
operation inline.
2014-06-19 20:49:03 +03:00
Emmanuel Blot
f6932d6506 Prefix ARRAY_SIZE with micropython prefix MP_ 2014-06-19 18:54:34 +02:00
Emmanuel Blot
bf3366a48b Add missing “assert.h” file header inclusion from “nlr.h” 2014-06-19 18:47:38 +02:00
mux
fe81eea967 Add MICROPY_HW_MICRO_NAME to boards config 2014-06-19 14:40:57 +02:00
Damien George
b0851e5949 Merge pull request #700 from swegener/for-upstream
bare-arm, stmhal: Disable stack protector
2014-06-18 14:59:39 +01:00
Sven Wegener
c3cabf4e33 bare-arm, stmhal, teensy: Duplicate -nostdlib to CFLAGS
As we are building with -nostdlib gcc features like the stack protector
will fail linking, because the failure handlers are in gcc's internal
libs. Such features are implicitly disabled during compilation when
-nostdlib is used in CFLAGS too.

Signed-off-by: Sven Wegener <sven.wegener@stealer.net>
2014-06-18 13:34:07 +02:00
Sven Wegener
afc67c6dc5 bare-arm, stmhal: Fix --nostdlib to -nostdlib
-nostdlib is the correct option, gcc recognizes the double dash version
when in link-only mode, but not when compiling.

Signed-off-by: Sven Wegener <sven.wegener@stealer.net>
2014-06-18 13:33:47 +02:00
stijn
9acb5e4cf0 gc: Turn off debugging info again 2014-06-18 12:29:03 +02:00
stijn
def10cecd1 gc: Keep debug statements at beginning of scope where possible 2014-06-18 10:20:41 +02:00
Damien George
720f55cc4b Merge pull request #698 from dhylands/adc-fix
Fix problem with ADC reads and multiple channels
2014-06-17 20:52:47 +01:00
Damien George
bcb3ab451b stmhal: Toggle LED using ODR ^= pin_mask. 2014-06-17 19:57:17 +01:00
Dave Hylands
535b88133c Fix problem with ADC reads and multiple channels 2014-06-16 09:41:58 -07:00
stijn
bbcea3f62b gc: More verbose debugging
Add more DEBUG_printf statements to trace gc behaviour
2014-06-16 12:43:35 +02:00
Dave Hylands
4f1b7fec9f Updated teensy to build.
Refactored some stmhal files which are shared with teensy.
2014-06-15 22:48:05 -07:00
Damien George
2547928148 stmhal: Add Python function to set UART for REPL.
This adds a hook to get/set pyb_uart_global_debug from Python, using
pyb.repl_uart().  You can set it to an arbitrary UART object, and then
the REPL (in and out) is repeated on this UART object (as well as on USB
CDC).

Ultimately, this will be replaced with a proper Pythonic interface to
set sys.stdin and sys.stdout.
2014-06-15 09:47:27 +01:00
Damien George
c0711cbefa stmhal: Fix type signatures on functions that take variable args. 2014-06-15 09:32:42 +01:00
Damien George
e79c6696c5 stmhal: Fix file print methods to use print instead of printf.
Also make stdout_print_strn static (ultimately this function needs to be
merged with stdout_tx_strn).
2014-06-15 09:10:07 +01:00
Damien George
34ab8dd6dd stmhal: Update and improve LCD driver.
Still some method names to iron out, and funtionality to add, but this
will do for the first, basic version.
2014-06-15 00:41:47 +01:00
Paul Sokolovsky
0294661da5 parsenum: Signedness issues.
char can be signedness, and using signedness types is dangerous - it can
lead to negative offsets when doing table lookups. We apparently should just
ban char usage.
2014-06-14 18:02:21 +03:00
Damien George
812025bd83 Merge pull request #693 from iabdalkader/assert
Add __assert_func
2014-06-14 15:51:40 +01:00
mux
5f6f47a688 Make __assert_func weak 2014-06-14 17:02:50 +02:00
mux
00db5c81e1 Add __assert_func only if DEBUG=1 2014-06-14 15:53:11 +02:00
mux
34e7b67d3c Add __assert_func
* issue #692
2014-06-14 14:41:11 +02:00
Paul Sokolovsky
e3cfc0d33d objstr: Refactor to work with char pointers instead of indexes.
In preparation for unicode support.
2014-06-14 06:30:30 +03:00
Paul Sokolovsky
7ddbd1bee7 unicode: Add trivial implementation of unichar_charlen(). 2014-06-14 06:30:30 +03:00
Paul Sokolovsky
b0bb458810 unicode: String API is const byte*.
We still have that char vs byte dichotomy, but majority of string operations
now use byte.
2014-06-14 06:22:11 +03:00
Paul Sokolovsky
2ec38a17d4 objstr: Be 8-bit clean even for repr().
This will allow roughly the same behavior as Python3 for non-ASCII strings,
for example, print("<phrase in non-Latin script>".split()) will print list
of words, not weird hex dump (like Python2 behaves). (Of course, that it
will print list of words, if there're "words" in that phrase at all, separated
by ASCII-compatible whitespace; that surely won't apply to every human
language in existence).
2014-06-14 01:21:13 +03:00
Damien George
e9036c295c Merge branch 'stinos-gc-pointers' 2014-06-13 22:34:11 +01:00
Damien George
c037694957 py, gc: Revert ret_ptr to void*, casting to byte* for memset. 2014-06-13 22:33:31 +01:00
Damien George
63b2237323 Merge branch 'gc-pointers' of github.com:stinos/micropython into stinos-gc-pointers 2014-06-13 22:30:46 +01:00
Paul Sokolovsky
e22cddbe2a stream: Use mp_obj_is_true() for EOF testing.
Getting a length of string may be expensive, depending on the underlying
implementation.
2014-06-13 23:53:10 +03:00
stijn
f33385f56d gc: Use byte* pointers instead of void* for pointer arithmetic
void* is of unknown size
2014-06-13 20:42:06 +02:00
Damien George
8340c48389 py: Revert change of include, "" back to <> for mpconfigport.h. 2014-06-12 19:50:17 +01:00
Paul Sokolovsky
fbdf2f1d63 py: Rename builtin "io" to "_io".
Functionality we provide in builtin io module is fairly minimal. Some
code, including CPython stdlib, depends on more functionality. So, there's
a choice to either implement it in C, or move it _io, and let implement other
functionality in Python. 2nd choice is pursued. This setup matches CPython
too (_io is builtin, io is Python-level).
2014-06-12 01:22:25 +03:00
Damien George
8a0801ad24 py: Make 3 functions static. 2014-06-11 19:55:46 +01:00
Damien George
73c98d8709 py: Fix static defn in qstr; include mpconfigport.h with "" (not <>). 2014-06-11 19:18:03 +01:00
Paul Sokolovsky
0c0f446840 objfun: Remove no longer used mp_obj_fun_prepare_simple_args(). 2014-06-11 20:43:47 +03:00
Paul Sokolovsky
f4bf065dac tests: Add testcases for "complicated" args to generator functions. 2014-06-11 20:43:47 +03: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
Paul Sokolovsky
49df795d1d objfun: Factor out mp_setup_code_state() function to set up code_state object.
It needs to be reused for generator functions, too.
2014-06-11 20:43:47 +03:00
Damien George
820896746c stmhal, file: Seek to end of file if opened in 'a' mode. 2014-06-11 16:01:52 +01:00
Damien George
b7572ad11b stmhal, file: Implement a,x,+ open modes, seek and tell.
Also now returns correct POSIX errno when an IO operation fails.

Addresses issues #516 and #676.
2014-06-11 15:41:14 +01:00
Damien George
58cbb4d661 py: Implement __contains__ special method. 2014-06-10 23:07:56 +01:00
Paul Sokolovsky
62f7ba7a81 Merge pull request #675 from Rosuav/seq_simplify
Remove unnecessary bounds check from mp_seq_get_fast_slice_indexes.
2014-06-09 23:40:04 +03:00
Chris Angelico
1f44e118f0 Remove unnecessary bounds check from mp_seq_get_fast_slice_indexes.
At this point, start will be >= 0, so checking if stop < 0 is redundant with
checking if start > stop a few lines later.
2014-06-10 03:59:55 +10:00
Paul Sokolovsky
195de3247b objtype: Fix passing of class param to inherited classmethods.
This is getting more and more tangled, but that's old news.
2014-06-08 22:28:44 +03:00
Paul Sokolovsky
639863d36e objtype: Optimize stack usage mp_obj_class_lookup().
As before, instead of pushing constant values on stack again and again, just
pass around pointer to a structure.
2014-06-08 20:50:12 +03:00
Damien George
57b4dfa9c9 stmhal: Fix pyb.bootloader so it works for gcc-4.9.0.
See PR #618.
2014-06-08 14:01:43 +01:00
Damien George
26a95ae1e7 windows: Move include of malloc.h outside #ifdef msvc. 2014-06-08 13:50:57 +01:00
Damien George
4297fed1c3 tests: Run 'micropython' tests on pyboard. 2014-06-08 13:46:03 +01:00
Damien George
70c289a7a6 Merge branch 'marcusva-alloca' 2014-06-08 13:26:02 +01:00
Damien George
4480cb3711 Provide definition of alloca() in mpconfigport.h. 2014-06-08 13:25:33 +01:00
Damien George
df896eceef Merge branch 'alloca' of github.com:marcusva/micropython into marcusva-alloca 2014-06-08 13:18:14 +01:00
Damien George
9e951498b2 tests: Add more tests for default keyword-only args. 2014-06-08 12:58:32 +01:00
Damien George
049a7a8153 py: Simplify function call of a bytecode object. 2014-06-08 00:37:40 +01:00
Damien George
c06427c019 tests: Fix default arg test. 2014-06-08 00:12:32 +01:00
Paul Sokolovsky
b4efac14cd py: Make sure getattr() works with non-interned strings (by interning them). 2014-06-08 01:15:06 +03:00
Damien George
d31a093f9c Merge branch 'master' of github.com:micropython/micropython
Conflicts:
	py/emitglue.c
2014-06-07 22:02:35 +01:00
Paul Sokolovsky
5473f743f3 objtype: Enable __lt__ method support for instances. 2014-06-08 00:01:46 +03:00
Damien George
f0778a7ccb py: Implement default keyword only args.
Should finish addressing issue #524.
2014-06-07 22:01:00 +01:00
Paul Sokolovsky
b9b9354e6c modsys: Add optional support for sys.platform.
Ports which wants to have it, should define MICROPY_PY_SYS_PLATFORM to a
string value they need.
2014-06-07 23:40:04 +03:00
Paul Sokolovsky
7e4a2b0edc py: Add generic mp_not_implemented() func to use instead of assert().
Benefits: won't crash baremetal targets, will provide Python source location
when not implemented feature used (it will no longer provide C source
location, but just grep for error message).
2014-06-07 23:26:27 +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
Damien George
82ed3d62f6 py, mk: Revert change where build variables set with ?=.
?= operator does not do delayed expansion (unlike =).
2014-06-07 13:14:45 +01:00
Damien George
a9b5248e18 Merge pull request #672 from marcusva/makefile
toolchain fixes to enable cross compatibility
2014-06-07 13:03:29 +01:00
Damien George
dc931934b3 Merge pull request #674 from marcusva/fbsd-patches
Build patches for FreeBSD (as discussed in the former pull request #666)
2014-06-07 13:00:35 +01:00
Marcus von Appen
585a3394df - Cast the struct stat sb.st_ino field to machine_int_t explicitly to avoid a
cast error in MP_OBJ_NEW_SMALL_INT(). This is necessary for FreeBSD, where
  st_ino is of different size
- If MP_CLOCKS_PER_SEC is defined on the target host, simply define CLOCK_DIV
  as a fraction, regardless of the value of MP_CLOCKS_PER_SEC.
  FreeBSD uses a non-POSIX compliant value of 128 for CLOCKS_PER_SEC
2014-06-07 09:50:18 +02:00
Marcus von Appen
0c90eb1658 - FreeBSD provides alloca() via stdlib.h, in contrast to Linux and Windows
- Move the includes for alloca() intp mpconfigport.h
2014-06-07 09:36:04 +02:00
Marcus von Appen
8ffc02495f - Let the build environment decide about the toolchain to be used, in case
there are special tweaks and paths to be considered. Just provide some
  defaults, in case the values are undefined.
- py-version.sh does not need any bash specific features.
- Use libdl only on Linux for now. FreeBSD provides dl*() calls from libc.
2014-06-07 09:16:42 +02:00
Damien George
c61be8e1e1 Merge pull request #662 from stinos/windows-pathsep
unix: Fix path seperator used depending on OS
2014-06-07 00:05:59 +01:00
Paul Sokolovsky
180751fbf3 Merge pull request #670 from Rosuav/stringhash
Bring the C and Python compute_hash functions into consistency
2014-06-07 00:10:10 +03:00
Chris Angelico
de09caaa37 Bring the C and Python compute_hash functions into consistency 2014-06-07 07:06:18 +10:00
Paul Sokolovsky
d72bc2713a objint: Fix corner case in buffer access. 2014-06-06 23:08:37 +03:00
Paul Sokolovsky
b56a53dfd6 Merge pull request #667 from Rosuav/testfixes
A couple of small fixes to run-tests
2014-06-06 22:23:23 +03:00
Paul Sokolovsky
8c75bd26e2 Merge pull request #668 from dhylands/print-prec
Fix str.modulo when precision is specified.
2014-06-06 22:12:42 +03:00
Dave Hylands
b69f9fa31f Fix str.modulo when precision is specified. 2014-06-05 23:09:02 -07:00
Paul Sokolovsky
380f147d2e modgc: Real 64-bit cleanness. 2014-06-06 03:01:39 +03:00
Paul Sokolovsky
a3ef8087e8 modgc: 64-bit cleanness. 2014-06-06 02:34:17 +03:00
Chris Angelico
047db2299c Turn the Travis CI test skipping mechanism into something more generic 2014-06-06 07:51:01 +10:00
Chris Angelico
88b11b50e5 Figure out the test_name before using it (significant only to Travis skips) 2014-06-06 07:51:01 +10:00
Paul Sokolovsky
755a55f507 modgc: Implement return value for gc.collect(), enable on Unix. 2014-06-05 22:48:02 +03:00
Damien George
d4c2bddd0c py: Raise TypeError when trying to format non-int with %x,%o,%X.
This behaviour follows Python 3.5 standard (in 3.4 it's a
DeprecationWarning which we'd rather make a TypeError).
2014-06-05 19:44:54 +01:00
Paul Sokolovsky
f675ff3957 Merge pull request #665 from Rosuav/naming3.3
Change comments (mainly URLs) to no longer specifically say Python 3.3
2014-06-05 21:17:46 +03:00
Damien George
11de8399fe py: Small changes to objstr.c, including a bug fix.
Some small fixed:

- Combine 'x' and 'X' cases in str format code.

- Remove trailing spaces from some lines.

- Make exception messages consistently begin with lower case (then
needed to change those in objarray and objtuple so the same
constant string data could be used).

- Fix bug with exception message having %c instead of %%c.
2014-06-05 18:57:38 +01:00
Chris Angelico
daf973ae00 Change comments (mainly URLs) to no longer specifically say Python 3.3 2014-06-06 03:51:03 +10:00
Damien George
c074cd38c3 Merge pull request #663 from Rosuav/floatpercentx
Remove tests that fail under CPython 3.5
2014-06-05 18:39:08 +01:00
Paul Sokolovsky
75ce9256b2 objstr: Implement "%(key)s" % {} formatting for strings and dicts.
Also, make sure that args to "*" format specifiers are bounds-checked
properly and don't lead for segfaults in case of mismatch.
2014-06-05 20:06:15 +03:00
Chris Angelico
7a6e09635a Remove tests that fail under CPython 3.5
See http://bugs.python.org/issue19995 for rationale. As micropython currently
aims for Python 3.3 compatibility I have not changed behaviour, but this
change allows the test suite to be run against a newer Python without having
spurious failures.
2014-06-05 22:34:11 +10:00
stijn
df3ab07994 unix: Fix path seperator used depending on OS
';' is the standard seperator used for paths in environment variables on Windows
2014-06-05 12:34:41 +02:00
Paul Sokolovsky
1e82ef3ae8 Merge pull request #660 from Rosuav/assert0
Replace assert(0) with a self-documenting TODO string
2014-06-05 12:11:48 +03:00
Paul Sokolovsky
76c8a4c91b unix: Add setjmp-based GC register helper implementation.
As I suspected for a long time, for x86, register helper doesn't really make
any difference - there's simply not enough register to keep anything in
them for any prolonged time. Anything gets pushed on stack anyway. So, on
x86, uPy passed all tests even with empty reg helper. So, this setjmp
implementation goes as "untested".
2014-06-05 04:32:17 +03:00
Chris Angelico
9ab8ab2117 Replace assert(0) with a self-documenting TODO string 2014-06-05 06:05:57 +10:00
Damien George
30583f58d5 Merge pull request #653 from Metallicow/switch-example-correction
remove `__doc__ =` and fix tweak doc diffs
2014-06-04 13:46:35 +01:00
Damien George
95fd3528c1 Merge pull request #650 from bvernoux/master
micropython port for HydraBus
2014-06-03 19:29:03 +01:00
Damien George
9b967dd3cd Merge pull request #655 from Rosuav/master
Two small changes
2014-06-03 19:25:37 +01:00
Chris Angelico
4867413e69 Simplify detection of quote characters in mp_str_print_quoted.
Once a double quote has been found, the subsequent discovery of a single quote
won't change behaviour at all, so don't bother looking for one.
2014-06-04 03:26:40 +10:00
bvernoux
82560fce75 Merge branch 'master' of https://github.com/micropython/micropython 2014-06-03 19:26:34 +02:00
Chris Angelico
29bf7393c1 Correct file reference (there's no qstrraw.h) 2014-06-04 03:15:46 +10:00
bvernoux
0a1dbfe02f Merge branch 'master' of https://github.com/bvernoux/micropython
Conflicts:
	README.md
2014-06-03 19:00:31 +02:00
bvernoux
c3c353d7f1 Cleanup/removed specific stuff specific to HydraBus (except board). 2014-06-03 18:59:24 +02:00
Metallicow
3d5ffa8318 remove __doc__ = and fix tweak doc diffs 2014-06-03 07:46:12 -06:00
Damien George
b294a7e3c9 py: Properly fix configuration of float and math module. 2014-06-03 13:43:20 +01:00
Damien George
3f52262465 py: Allow tail call optimisation in mp_call_function_n_kw.
This saves 4 words of stack space per Python call.
2014-06-03 13:40:16 +01:00
Damien George
65ec33200a py: Fix configuration of math module. 2014-06-03 12:33:38 +00:00
Damien George
bcb6ca4d5e py: Implement full behaviour of dict.update(), and dict().
Add keyword args to dict.update(), and ability to take a dictionary as
argument.

dict() class constructor can now use dict.update() directly.

This patch loses fast path for dict(other_dict), but is that really
needed?  Any anyway, this idiom will now re-hash the dictionary, so is
arguably more memory efficient.

Addresses issue #647.
2014-06-03 12:53:44 +01:00
Damien George
07995e9479 Merge pull request #649 from pfalcon/multi-opt
Support multiple bytecode optimisation levels
2014-06-03 10:46:36 +01:00
Paul Sokolovsky
411732e93b vm: If there's no lineno info, set lineno in traceback to 0, not 1.
To clearly signify that lineno is not known.
2014-06-03 12:32:59 +03:00
Paul Sokolovsky
b8f117dd32 py: For optimization level -O3 and higher, remove lineno info from bytecode. 2014-06-03 12:32:59 +03:00
Paul Sokolovsky
d3439d0c60 py: Instead of having "debug on" var, have "optimization level" var.
This allows to have multiple "optimization" levels (CPython has two
(-OO removes docstrings), we can have more).
2014-06-03 12:32:59 +03:00
Paul Sokolovsky
509c7a7854 Merge pull request #651 from dhylands/fix-af-csv2
Add missing commas to stm32f4xx-af.csv
2014-06-03 11:16:31 +03:00
Dave Hylands
4e0573e5cf Add missing commas to stm32f4xx-af.csv 2014-06-02 23:11:14 -07:00
Paul Sokolovsky
f753971e5d showbc: Make micropython -v also dump bytecode in hex form. 2014-06-03 01:39:13 +03:00
Paul Sokolovsky
a4ac5b9f05 showbc: Make sure it's possible to trace MAKE_FUNCTION arg to actual bytecode. 2014-06-03 01:26:51 +03:00
Paul Sokolovsky
dd0dee3afc unix: Properly print script filename in case of error. 2014-06-03 01:26:43 +03:00
Benjamin Vernoux
2abfeebf4a Update README.md 2014-06-02 22:09:47 +02:00
bvernoux
65a97e8d9c Merge branch 'master' of https://github.com/bvernoux/micropython 2014-06-02 22:09:20 +02:00
bvernoux
586f02a015 HydraBus board 2014-06-02 22:08:59 +02:00
Benjamin Vernoux
a5892a13b4 Update README.md 2014-06-02 22:04:37 +02:00
Benjamin Vernoux
a7d963d171 Update README.md 2014-06-02 22:03:22 +02:00
Benjamin Vernoux
d7da92a8f0 Update README.md 2014-06-02 21:54:48 +02:00
Benjamin Vernoux
8db7804496 Update README.md 2014-06-02 21:53:52 +02:00
bvernoux
569aa90137 micropython port for HydraBus 2014-06-02 21:43:02 +02:00
Paul Sokolovsky
8bf8404c15 showbc: Print code block header at the beginning, not in the middle of dump.
Also, dump code block in bytes.
2014-06-02 16:35:57 +03:00
Paul Sokolovsky
b325d25e46 lexer: Add another comment for somewhat obscure way __debug__ is handled. 2014-06-02 16:35:57 +03:00
Paul Sokolovsky
62798831be modstruct: Add one more extension to typecodes - 'S', a pointer to C string.
Also, add comment with description of extension to CPython's typecodes.
2014-06-02 16:35:56 +03:00
Damien George
b55a59de4c Merge branch 'Metallicow-LED-Fix' 2014-06-01 18:35:57 +01:00
Damien George
517f292c8d examples, switch: Make run_loop take sequence of LED objects. 2014-06-01 18:34:58 +01:00
Damien George
15a5738e1d Merge branch 'LED-Fix' of github.com:Metallicow/micropython into Metallicow-LED-Fix 2014-06-01 18:30:03 +01:00
Damien George
fcc9cf63f1 py, str: Replace enum with actual function pointer.
This way, it's slightly more efficient, uses less ROM (60 bytes less
for stmhal), and doesn't require to raise exception if bad operation
given.
2014-06-01 18:22:09 +01:00
Damien George
f1dbd78b30 stmhal: Document pyb.Accel() constructor, that it takes time to start. 2014-06-01 17:57:06 +01:00
Metallicow
9500e98433 __doc__ switch, make importable, and easy to test 2014-06-01 08:18:13 -06:00
Damien George
f917f06384 bare-arm: Disable slice and set. 2014-06-01 13:49:54 +01:00
Damien George
c49ddb9315 py: Fix configurability of builtin slice. 2014-06-01 13:49:35 +01:00
Damien George
3ebd4d0cae py: Add option to disable set() object (enabled by default). 2014-06-01 13:46:47 +01:00
Damien George
fb510b3bf9 Rename bultins config variables to MICROPY_PY_BUILTINS_*.
This renames:
MICROPY_PY_FROZENSET -> MICROPY_PY_BUILTINS_FROZENSET
MICROPY_PY_PROPERTY -> MICROPY_PY_BUILTINS_PROPERTY
MICROPY_PY_SLICE -> MICROPY_PY_BUILTINS_SLICE
MICROPY_ENABLE_FLOAT -> MICROPY_PY_BUILTINS_FLOAT

See issue #35 for discussion.
2014-06-01 13:32:54 +01:00
Damien George
c60a261ef0 py, vm: Replace save_ip, save_sp with code_state->{ip, sp}.
This may seem a bit of a risky change, in that it may introduce crazy
bugs with respect to volatile variables in the VM loop.  But, I think it
should be fine: code_state points to some external memory, so the
compiler should always read/write to that memory when accessing the
ip/sp variables (ie not put them in registers).

Anyway, it passes all tests and improves on all efficiency fronts: about
2-4% faster (64-bit unix), 16 bytes less stack space per call (64-bit
unix) and slightly less executable size (unix and stmhal).

The reason it's more efficient is save_ip and save_sp were volatile
variables, so were anyway stored on the stack (in memory, not regs).
Thus converting them to code_state->{ip, sp} doesn't cost an extra
memory dereference (except maybe to get code_state, but that can be put
in a register and then made more efficient for other uses of it).
2014-06-01 12:32:28 +01:00
Damien George
c7969857f4 Merge branch 'pfalcon-vm-alloca' 2014-06-01 12:08:55 +01:00
Damien George
1b87d1098a Merge branch 'vm-alloca' of github.com:pfalcon/micropython into pfalcon-vm-alloca
Conflicts:
	py/vm.c

Fixed stack underflow check.  Use UINT_FMT/INT_FMT where necessary.
Specify maximum VM-stack byte size by multiple of machine word size, so
that on 64 bit machines it has same functionality as 32 bit.
2014-06-01 12:06:17 +01:00
Metallicow
f94cc975a2 Add switch test example 2014-05-31 23:15:04 -06:00
Metallicow
fa82aa81c0 LED Fix 2014-05-31 23:10:10 -06:00
Damien George
6c13d7965e Merge branch 'master' of github.com:micropython/micropython 2014-05-31 18:35:00 +01:00
Damien George
4d659f566f tests: Add feature test for when heap allocation is disabled. 2014-05-31 18:33:16 +01:00
Damien George
6e18835b94 Merge pull request #643 from dhylands/fix-af-csv
Add a comma to make the .csv look proper in github
2014-05-31 18:13:28 +01:00
Damien George
a053e37b2c tests: Change --test_dirs to --test-dirs. 2014-05-31 18:11:01 +01:00
Damien George
e7412ab37b Merge pull request #632 from stinos/tests-dir-argument
tests: Add argument to allow specifying which directories to test
2014-05-31 18:11:03 +01:00
Damien George
5b5562c1d1 py: Fix stack underflow with optimised for loop. 2014-05-31 17:59:11 +01:00
Damien George
049a01d148 tests: Add another test for break-from-for-loop. 2014-05-31 16:56:15 +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
b16523aa95 vm: Don't unconditionally allocate state on stack, do that only if needed.
This makes sure that only as much stack allocated as actually used, reducing
stack usage for each Python function call.
2014-05-31 18:19:33 +03:00
Paul Sokolovsky
ff8da0b835 vm: Detect stack underflow in addition to overflow. 2014-05-31 18:14:54 +03:00
Paul Sokolovsky
ae9c82d5f3 objstr: str_uni_istype(): Spurious whitespace on empty lines. 2014-05-31 11:00:25 +03:00
Paul Sokolovsky
f69b9d379c objstr: str_uni_istype(): Codestyle. 2014-05-31 10:59:34 +03:00
Paul Sokolovsky
69a8b23651 Merge pull request #644 from kimbauters/master
add methods isspace(), isalpha(), isdigit(), isupper() and islower() to str
2014-05-31 10:52:20 +03:00
Kim Bauters
a3f4b83018 add methods isspace(), isalpha(), isdigit(), isupper() and islower() to str 2014-05-31 07:30:57 +01:00
Dave Hylands
b5fb9b22e2 Add a comma to make the .csv look proper in github 2014-05-30 22:18:52 -07:00
Paul Sokolovsky
1f07b7e3c3 py: Reformat few long functions argument lists for clarity. 2014-05-31 03:36:37 +03:00
Paul Sokolovsky
3dfa76cb85 unix: 64-bit cleanness. 2014-05-31 03:19:15 +03:00
Paul Sokolovsky
914bcf16d8 unix: Add poorman's stack usage info to mem_info() dump. 2014-05-31 02:34:39 +03:00
Paul Sokolovsky
b30a777ace objfun: Typo fixes in comments. 2014-05-31 02:24:47 +03:00
Paul Sokolovsky
347b3a3d1f modsocket: Add some comments on intended usage/API design of module. 2014-05-31 01:48:26 +03:00
Paul Sokolovsky
50b08c920a modsocket: Remove stale ifdef. 2014-05-31 01:41:41 +03:00
Paul Sokolovsky
ccd0e0afcd tests: Add test for break in for.
For #635 / 25c84643b6.
2014-05-31 00:43:41 +03:00
Damien George
25c84643b6 py: Fix break from within a for loop.
Needed to pop the iterator object when breaking out of a for loop.  Need
also to be careful to unwind exception handler before popping iterator.

Addresses issue #635.
2014-05-30 15:20:41 +01:00
Paul Sokolovsky
8827682b35 objstr: *strip(): If nothing is stripped, don't create dup string. 2014-05-30 03:15:17 +03:00
Paul Sokolovsky
bcdffe53c6 objstr: *strip(): Fix handling of one-char subject strings. 2014-05-30 03:15:17 +03:00
Paul Sokolovsky
059f95b2cb Merge pull request #633 from stinos/msvc-fix-genhdr-dep
msvc: Only update generated headers when there are changes
2014-05-30 02:48:04 +03:00
Paul Sokolovsky
97953f6ce7 qemu-arm: Add port README.
Based on https://github.com/micropython/micropython/pull/630 by @errordeveloper.
2014-05-30 02:40:09 +03:00
Damien George
f55cf10101 py: Implement bignum '&' with negatives on lhs and rhs.
Needs proper coverage testing.  Doesn't implement -ve & -ve.
Addresses issue #611.
2014-05-29 15:01:49 +01:00
stijn
48d641e41a msvc: Only update generated headers when there are changes
This fixes generating the headers casuing complete rebuilds,
even when the headere's content didn't really change.
2014-05-28 16:03:38 +02:00
Damien George
d1e355ea8e py: Fix check of small-int overflow when parsing ints.
Also unifies use of SMALL_INT_FITS macro across parser and runtime.
2014-05-28 14:51:12 +01:00
Damien George
813ed3bda6 py: Make int(<longint>) work by just returning the longint. 2014-05-28 14:09:46 +01:00
Damien George
503d611033 py: Implement long int parsing in int(...).
Addresses issue #627.
2014-05-28 14:07:21 +01:00
Paul Sokolovsky
1d567592b1 unix/gccollect.c: Make Clang workaround apply only to it. Unbreaks gcc builds. 2014-05-28 15:37:22 +03:00
stijn
8ac3b578e5 tests: Add argument to allow specifying which directories to test 2014-05-28 14:33:30 +02:00
Paul Sokolovsky
168a9ce863 Revert "Fix DEBUG=1 builds"
This reverts commit 6e76f7bc90.

This patch tries to workaround a previous clang workaround. Instead of going
into workaround of workaround spiral, the original workaround should be tamed.
2014-05-28 15:28:30 +03:00
Damien George
ae13758dd7 Merge pull request #631 from stinos/fix-win-def
windows: Complete rename of MICROPY_PATH_MAX to MICROPY_ALLOC_PATH_MAX (...
2014-05-28 13:10:03 +01:00
Damien George
4de2fe10b4 Merge pull request #629 from dhylands/fix-unix-debug
Fix unix DEBUG=1 builds
2014-05-28 13:00:02 +01:00
stijn
34c24a0fc2 windows: Complete rename of MICROPY_PATH_MAX to MICROPY_ALLOC_PATH_MAX (58ebde4) 2014-05-27 14:35:56 +02:00
Dave Hylands
6e76f7bc90 Fix DEBUG=1 builds
Without this fix, I get the following error:

CC gccollect.c
gccollect.c: In function ‘gc_helper_get_regs’:
gccollect.c:63:1: error: bp cannot be used in asm here
2014-05-26 16:29:24 -07:00
Paul Sokolovsky
0405b2210d modos: stat(): Accept bytes argument. 2014-05-26 02:02:47 +03:00
Paul Sokolovsky
d07bf029b7 tests: Add small testcase for 3-arg slices. 2014-05-26 02:02:47 +03:00
Damien George
d8675541a9 py, vm: Where possible, make variables local to each opcode.
This helps the compiler do its optimisation, makes it clear which
variables are local per opcode and which global, and makes it consistent
when extra variables are needed in an opcode (in addition to old obj1,
obj2 pair, for example).

Could also make unum local, but that's for another time.
2014-05-25 22:58:04 +01:00
Damien George
f600a6a085 py: Slightly improve efficiency of mp_obj_new_str; rename str_new.
Reorder interning logic in mp_obj_new_str, to be more efficient.

str_new is globally accessible, so should be prefixed with mp_obj_.
2014-05-25 22:34:34 +01:00
Damien George
2617eebf2f Change const byte* to const char* where sensible.
This removes need for some casts (at least, more than it adds need
for new casts!).
2014-05-25 22:27:57 +01:00
Damien George
f88fc7bd23 Merge branch 'pfalcon-keep-strings-uninterned' 2014-05-25 22:13:47 +01:00
Damien George
5042bce8fb py: Don't automatically intern strings in parser.
This completes non-automatic interning of strings in the parser, so that
doc strings don't take up RAM.  It complicates the parser and compiler,
and bloats stmhal by about 300 bytes.  It's complicated because now
there are 2 kinds of parse-nodes that can be strings: interned leaves
and non-interned structs.
2014-05-25 22:06:06 +01:00
Paul Sokolovsky
5fd5af98d0 objlist: Implement support for arbitrary (3-arg) slices. 2014-05-25 22:12:56 +03:00
Paul Sokolovsky
de4b9329f9 py: Refactor slice helpers, preparing to support arbitrary slicing. 2014-05-25 21:21:57 +03:00
Damien George
3aaabd11a0 Merge branch 'keep-strings-uninterned' of github.com:pfalcon/micropython into pfalcon-keep-strings-uninterned
Conflicts:
	py/parse.c
2014-05-25 13:19:31 +01:00
Paul Sokolovsky
ff4b6daa4f sequence: Throw exception for not implemented slice steps. 2014-05-25 03:02:57 +03:00
Paul Sokolovsky
2705f4c782 objlist: Implement growing slice assignment.
This means that complete slice operations are supported for lists (but not
for bytearray's and array.array's).
2014-05-25 02:36:12 +03:00
Paul Sokolovsky
69d081a7cf py: Handle case of slice start > stop in common sequence function. 2014-05-25 02:29:40 +03:00
Paul Sokolovsky
afaaf535e6 objslice: Support arbitrary objects start, stop, and step.
Older int-only encoding is not expressive enough to support arbitrary slice
assignment operations.
2014-05-25 01:42:24 +03:00
Damien George
7a4ddd2428 Add SystemExit exception and use it in unix/ and stmhal/ ports.
Addresses issue #598.
2014-05-24 23:32:19 +01:00
Damien George
ee3fd46f13 Rename configuration variables controling Python features.
Now of the form MICROPY_PY_*.  See issue #35.
2014-05-24 23:03:12 +01:00
Paul Sokolovsky
d0ceb04b90 modsocket: 64-bit cleanness. 2014-05-24 23:00:09 +03:00
Paul Sokolovsky
d098c6bf85 objstr: Implement .endswith(). 2014-05-24 22:46:51 +03:00
Paul Sokolovsky
561789d718 unix modsocket: Make .makefile() method more compliant.
.makefile() should allow to specify which stream time to create - byte
or text.
2014-05-24 21:24:37 +03:00
Paul Sokolovsky
806ea1f6ca py: Initial attempts to actually allow implementing __new__ in Python.
Caveat is that __new__ should recurse to base class __new__, and ultimately,
object.__new__ is what handles instance allocation.
2014-05-22 00:32:00 +03:00
Paul Sokolovsky
0c937fa25a objobject: Fix arguments to __init__(). 2014-05-21 23:10:48 +03:00
Damien George
efaef6eea3 unix: Fix casting issue, int to small int object. 2014-05-21 20:53:56 +01:00
Damien George
90886807a1 Merge branch 'master' of github.com:micropython/micropython 2014-05-21 20:35:02 +01:00
Damien George
58ebde4664 Tidy up some configuration options.
MP_ALLOC_* -> MICROPY_ALLOC_*
MICROPY_PATH_MAX -> MICROPY_ALLOC_PATH_MAX
MICROPY_ENABLE_REPL_HELPERS -> MICROPY_HELPER_REPL
MICROPY_ENABLE_LEXER_UNIX -> MICROPY_HELPER_LEXER_UNIX
MICROPY_EXTRA_* -> MICROPY_PORT_*

See issue #35.
2014-05-21 20:32:59 +01:00
Paul Sokolovsky
a8408a8abe objtype: super: Fall back to "object" lookup as last resort.
Also, define object.__init__() (semantically empty, purely CPython compat
measure). Addresses #520.
2014-05-21 22:27:03 +03:00
Paul Sokolovsky
6a410789b8 objtype: super: Add stop condition for looking up in base types. 2014-05-21 22:27:03 +03:00
Damien George
aa7cf6f72f stm: Remove long-obsolete stm/ port. 2014-05-21 20:14:27 +01:00
Damien George
63436ce22e unix, Mac support: Generate order.def via Makefile. 2014-05-21 19:56:54 +01:00
Damien George
0fd01683c6 Merge pull request #607 from Anton-2/osx-clang
Allow compilation of unix port under clang on OS X
2014-05-21 19:51:05 +01:00
Damien George
6ac5dced24 py: Rename MP_OBJ_NOT_SUPPORTED to MP_OBJ_NULL.
See issue #608 for justification.
2014-05-21 19:42:43 +01:00
Damien George
6d197740cf stmhal: Stop USB before entering DFU by software. 2014-05-21 19:25:34 +01:00
Paul Sokolovsky
008343f640 Merge pull request #621 from stinos/migw-w64-fix
windows: Fix compilation with mingw-w64 so it uses correct printf implem...
2014-05-20 16:38:16 +03:00
Paul Sokolovsky
053765414c modstruct: struct_calcsize: Fix case of uninitialized var. 2014-05-20 16:34:05 +03:00
stijn
32acd4b9f1 windows: Fix compilation with mingw-w64 so it uses correct printf implementations
Without this flag, mingw-w64 uses the MS implementations of snpintf and the likes.
This is not really a problem since they work with the the fixes provided for msvc,
but due to the way mingw-w64's stdio.h is structured we cannot get it to use the fixes.
2014-05-20 12:14:28 +02:00
Paul Sokolovsky
44a949d58c qemu-arm: Disable "io" module. 2014-05-19 22:33:35 +03:00
Paul Sokolovsky
9e29666bf9 py: Implement proper separation between io.FileIO and io.TextIOWrapper.
io.FileIO is binary I/O, ans actually optional. Default file type is
io.TextIOWrapper, which provides str results. CPython3 explicitly describes
io.TextIOWrapper as buffered I/O, but we don't have buffering support yet
anyway.
2014-05-19 21:56:07 +03:00
Paul Sokolovsky
52386cafa0 objexcept: Implement explicit __init__ method, useful for subclasses. 2014-05-19 21:56:07 +03:00
Paul Sokolovsky
66ab571cca tests: Update subclass-native2.py for __new__/__init__ refactor.
Now case of subclassing tuple works, and list is broken, see comments.
2014-05-19 21:56:07 +03:00
Paul Sokolovsky
13684fd60b objtype: Separate __new__ and __init__ methods.
Now schedule is: for native types, we call ->make_new() C-level method, which
should perform actions of __new__ and __init__ (note that this is not
compliant, but is efficient), but for user types, __new__ and __init__ are
called as expected.

Also, make sure we convert scalar attribute value to a bound-pair tight in
mp_obj_class_lookup() method, which avoids converting it again and again in
its callers.
2014-05-19 21:56:06 +03:00
Damien George
eee31288dd stmhal: Fix DAC documentation: need to convert float to int for buf. 2014-05-19 19:08:12 +01:00
Damien George
2de4d59171 stmhal: Fix write_timed function for DAC(2).
Addresses issue #617.
2014-05-19 18:58:53 +01:00
Damien George
f905ebb173 stmhal: Make pyb.bootloader take no arguments. 2014-05-19 18:26:51 +01:00
Damien George
404f7cf902 Merge pull request #618 from swegener/jump-to-bootloader
Jump to bootloader
2014-05-19 18:13:34 +01:00
Sven Wegener
9bf4f7e3d3 stmhal: Remap system flash and adjust addresses
Signed-off-by: Sven Wegener <sven.wegener@stealer.net>
2014-05-18 13:15:02 +02:00
Sven Wegener
7ae8e4b679 stmhal: Activate bootloader with pyb.bootloader()
Signed-off-by: Sven Wegener <sven.wegener@stealer.net>
2014-05-18 12:51:21 +02:00
Paul Sokolovsky
5cdff5fa61 Merge pull request #615 from swegener/for-upstream
py: Fix mp_obj_t -> mp_const_obj_t for mp_obj_int_get_checked()
2014-05-17 23:30:00 +03:00
Sven Wegener
7ba0fedf13 py: Fix mp_obj_t -> mp_const_obj_t for mp_obj_int_get_checked()
Signed-off-by: Sven Wegener <sven.wegener@stealer.net>
2014-05-17 11:21:12 +02:00
Paul Sokolovsky
bf27140193 py: More mp_identity usage. 2014-05-17 11:20:10 +03:00
Paul Sokolovsky
ab7bf28489 py: More const usage. 2014-05-17 11:20:10 +03:00
Paul Sokolovsky
c18ef2a9dd objstr: startswith(): Accept optional "start" arg. 2014-05-15 21:33:18 +03:00
Paul Sokolovsky
70328e419a py: Implement more complete bytes comparison handling. 2014-05-15 20:58:40 +03:00
Paul Sokolovsky
ad3baec12f sequence: Fix yet another case of improper sequence comparison.
This time, in mp_seq_cmp_bytes(). How many more cases are still lurking?
2014-05-15 19:09:06 +03:00
Paul Sokolovsky
767e45c290 modos: Clean 64-bit issues. 2014-05-15 18:38:54 +03:00
Paul Sokolovsky
a47b64ae2d objstringio: Implement io.BytesIO.
Done in generalized manner, allowing any stream class to be specified as
working with bytes.
2014-05-15 07:28:19 +03:00
Paul Sokolovsky
0c124c3123 unix: Add "_os" module with stat().
stat() is bad function to use using FFI, because its ABI is largely private.
To start with, Glibc .so doesn't even have "stat" symbol. Then, layout of
struct stat is too implementation-dependent. So, introduce _os to deal
with stat() and other similar cases.
2014-05-14 22:08:45 +03:00
Paul Sokolovsky
2a27365854 objstr.c: Partial implementation of .rsplit().
sep=None is TODO.
2014-05-14 02:42:20 +03:00
Damien George
51fab28e94 py: Improve mpz_and function.
This should now have correct (and optimal) behaviour.
2014-05-13 22:58:00 +01:00
Damien George
f6e430f77f Merge pull request #600 from stinos/unix-exitcode
unix: Use standard return codes for main
2014-05-13 22:39:35 +01:00
Damien George
aeeb448eb6 Merge pull request #613 from pfalcon/pauls-copyr
py, unix: Add copyright for modules I worked closely on.
2014-05-13 22:36:36 +01:00
Paul Sokolovsky
da9f0924ef py, unix: Add copyright for modules I worked closely on. 2014-05-13 18:41:25 +03:00
Paul Sokolovsky
7074f25768 tests/int-long.py: Try to expose issue with recent "&" optimization. 2014-05-13 08:24:54 +03:00
Damien George
561e425903 py: Fix bug in mpz_and function.
Addresses issue #610.
2014-05-12 23:27:29 +01:00
Damien George
cc97446ca5 unix: Implement -O option to turn off __debug__ flag. 2014-05-12 23:14:52 +01:00
Damien George
915197a8f9 py: Remove emit_glue init and deinit. Needed only for debugging.
Debugging output for emit_glue now simplified so that the init and
deinit functions are no longer needed.
2014-05-12 23:11:14 +01:00
Damien George
97f9a2813e py: Add support for __debug__ constant.
__debug__ now resolves to True or False.  Its value needs to be set by
mp_set_debug().

TODO: call mp_set_debug in unix/ port.

TODO: optimise away "if False:" statements in compiler.
2014-05-12 23:07:34 +01:00
Damien George
96f137b24a py: Rename BYTE_CODE to BYTECODE (this was missed in previous rename). 2014-05-12 22:35:37 +01:00
stijn
f42dbb98d1 unix: Fix linker errors when time/ffi modules are disabled
When disabling these via mpconfigport.mk or on the commandline,
the correspoding build options are not set and the sources are not
built so the modules should not be added to the
MICROPY_EXTRA_BUILTIN_MODULES list since they are undefined.
2014-05-13 00:03:24 +03:00
Paul Sokolovsky
df94b717b4 modstruct: Implement count specifier for strings (e.g. "100s").
Infra for counts of other types is there, need last mile to be implemented.
2014-05-12 23:45:50 +03:00
Antonin ENFRUN
da1fffaa09 Fix some unused variables, and silence a clang warning about initialization override in vmentrytable.h 2014-05-12 09:06:18 +02:00
Antonin ENFRUN
ceac71f1f5 unix: Add asm statements needed to read registers with clang. Code generated by gcc 4.9.0 is unchanged (same statements, different order).
Both are inefficient, saving unmodified registers on the stack.
2014-05-12 09:06:18 +02:00
Antonin ENFRUN
1b901c320b tests: create result file for test/basics/memoryerror.py .
On Mac OS "python3 test/basics/memoryerror.py" never runs out of memory, the process is frozen by the os before.
2014-05-12 00:13:10 +02:00
Antonin ENFRUN
6caae0bcb1 unix: Create __bss_start and _end symbols for Mach-O targets.
It's a hack, but can't find a cleaner way to do it.
2014-05-12 00:13:10 +02:00
Paul Sokolovsky
147c80bf7c modstruct: Use MP_OBJ_FUN_ARGS_MAX instead of -1. 2014-05-11 22:50:27 +03:00
Paul Sokolovsky
5ebd5f0f19 objstr: Slice indexing: support bytes properly. 2014-05-11 21:22:59 +03:00
Paul Sokolovsky
bfb8819c0c objstr: Make .split() support bytes. 2014-05-11 21:17:28 +03:00
Paul Sokolovsky
5e5d69b35e objstr: Make .join() support bytes. 2014-05-11 21:13:01 +03:00
Paul Sokolovsky
7e7940c39d py: Fix __len__ special method result handling.
Having both MP_OBJ_NOT_SUPPORTED and MP_OBJ_NULL is arguably confusing.
2014-05-11 20:51:31 +03:00
Paul Sokolovsky
c48d6f7add py: Don't expect that type->getiter() always returns iterator, check for NULL.
This is better than forcing each getiter() implementation to raise exception.
2014-05-11 20:51:31 +03:00
Paul Sokolovsky
0f570cfccf showbc: Decode MAP_ADD. 2014-05-11 20:51:31 +03:00
Paul Sokolovsky
ff30666c69 py: Add basic implementation of hasattr() function. 2014-05-11 20:51:30 +03:00
Damien George
a0863158f5 Merge pull request #605 from stinos/travis-mingw
travis: Add cross-compilation of mingw port
2014-05-11 18:48:15 +01:00
Damien George
96b62855b5 Merge pull request #604 from stinos/windows-enablefeat
windows: Enable frozen set and sys.exit
2014-05-11 18:41:48 +01:00
Damien George
ee7a880d6e py: Use mp_arg_check_num in more places.
Updated functions now do proper checking that n_kw==0, and are simpler
because they don't have to explicitly raise an exception.  Down side is
that the error messages no longer include the function name, but that's
acceptable.

Saves order 300 text bytes on x64 and ARM.
2014-05-11 18:37:21 +01:00
stijn
5f9ebd36cd travis: Add cross-compilation of mingw port 2014-05-11 19:33:13 +02:00
Damien George
1d34e32431 py: frozenset() creates an empty frozenset. 2014-05-11 18:28:48 +01:00
stijn
b9d8091d0e windows: Enable frozen set and sys.exit 2014-05-11 19:26:36 +02:00
stijn
9e040b7cd8 unix: Use standard return codes for main
As in the CPython manual: "Unix programs generally use 2 for
command line syntax errors and 1 for all other kind of errors"
2014-05-11 19:23:30 +02:00
Damien George
2323ef9182 py: Rename globally-accessible tuple functions, prefix with mp_obj_.
Likely there are other functions that should be renamed, but this is a
start.
2014-05-11 18:00:45 +01:00
Damien George
c59af52e84 py: Rename some unichar functions for consistency. 2014-05-11 17:53:11 +01:00
Damien George
89755ae67f py: Rename MICROPY_SYS_EXIT to MICROPY_MOD_SYS_EXIT.
For consistency with MICROPY_MOD_SYS_STDFILES, etc.
2014-05-11 17:35:43 +01:00
Damien George
f92a0d4d16 stmhal: Enable frozenset. Takes 292 text bytes in ROM. 2014-05-11 17:27:31 +01:00
Damien George
bd651d1a67 Merge pull request #597 from stinos/mingw-compilation
mingw: Fix compilation issues
2014-05-11 15:06:26 +01:00
Damien George
c3dcb590b8 Merge pull request #603 from stinos/windows-tests-newline
tests: Fix handling of newlines from expected output files on windows
2014-05-11 15:03:57 +01:00
Damien George
18ceb7055b Merge branch 'master' of github.com:micropython/micropython 2014-05-11 12:10:35 +01:00
Damien George
a7a1a38df4 stmhal: Update CC3000 driver to newer version.
Still not working properly.
2014-05-11 12:09:13 +01:00
Paul Sokolovsky
eea0118654 py: Give up and make mp_obj_str_get_data() deal with bytes too.
This is not fully correct re: error handling, because we should check that
that types are used consistently (only str's or only bytes), but magically
makes lot of functions support bytes.
2014-05-11 13:51:24 +03:00
stijn
a4dbc73e8a tests: Fix handling of newlines from expected output files 2014-05-11 12:45:02 +02:00
Paul Sokolovsky
b2d4fc06fc objstr: Make *strip() accept bytes. 2014-05-11 13:17:29 +03:00
Paul Sokolovsky
ce6c10172b tests: Really fix import. 2014-05-11 03:45:42 +03:00
Paul Sokolovsky
b4acd028b6 tests: Fix import. 2014-05-11 03:40:32 +03:00
Paul Sokolovsky
ea9708092e objtuple: Go out of the way to support comparison of subclasses.
Two things are handled here: allow to compare native subtypes of tuple,
e.g. namedtuple (TODO: should compare type too, currently compared
duck-typedly by content). Secondly, allow user sunclasses of tuples
(and its subtypes) be compared either. "Magic" I did previously in
objtype.c covers only one argument (lhs is many), so we're in trouble
when lhs is native type - there's no other option besides handling
rhs in special manner. Fortunately, this patch outlines approach with
fast path for native types.
2014-05-11 03:33:19 +03:00
Paul Sokolovsky
9511f60f01 py: Don't try to "bind" types store as attributes of objects.
This was hit when trying to make urlparse.py from stdlib run. Took
quite some time to debug.

TODO: Reconsile bound method creation process better, maybe callable is
to generic type to bind at all?
2014-05-11 03:33:19 +03:00
Paul Sokolovsky
69f3eb2c96 objstr: Make .[r]partition() work with bytes. 2014-05-11 03:33:19 +03:00
Paul Sokolovsky
285683d203 objboundmeth: If detailed reporting enabled, print object content.
Similar to closure and cell.
2014-05-11 02:27:42 +03:00
Paul Sokolovsky
7aca1cae34 py: Start making good use of mp_const_obj_t. 2014-05-11 02:26:42 +03:00
Damien George
50073ed5d6 stmhal/cc3k: Remove spaces and tabs at end of lines. 2014-05-10 21:28:40 +01:00
Paul Sokolovsky
3f8d34ca83 objlist: Support list slice deletion. 2014-05-10 23:03:30 +03:00
Paul Sokolovsky
94d8246272 objlist: Implement non-growing slice assignment.
Slice value to assign can be only a list so far too.
2014-05-10 22:24:31 +03:00
Paul Sokolovsky
d915a52eb6 py: Fix prefix on few sequence helpers, was incorrectly "mp_". 2014-05-10 21:36:33 +03:00
Paul Sokolovsky
aa4d19a05c objtype: Comments for duplicating code in runtime.c. 2014-05-10 21:26:08 +03:00
Paul Sokolovsky
1a7403bb74 objtype: Implement ->getiter() method for instances.
Includes support for native bases.
2014-05-10 21:26:08 +03:00
Paul Sokolovsky
0bc15941c2 py: Make mp_obj_print() handle null object w/o segfault if debug build.
Happens regularly when used for debugging.
2014-05-10 21:26:07 +03:00
Paul Sokolovsky
7067d69bcc objnamedtuple: Support iteration. 2014-05-10 21:26:07 +03:00
Damien George
3793830ed9 tools: Move gendoc.py to tools, and make it a little more generic. 2014-05-10 19:12:47 +01:00
Damien George
09bbe7215a stmhal: Fix USB CDC not flushing packets when an exact multiple of 64.
Need to send a zero-sized packet after sending an exact multiple of 64
bytes (not just after sending 64 bytes exactly).

Addresses issue #494, part 2.
2014-05-10 18:56:16 +01:00
Damien George
0fb80c303a py: Compress a little the bytecode emitter structure. 2014-05-10 18:16:21 +01:00
Damien George
9597771fe4 py, emitters: Fix dummy_data size for bytecode and thumb.
Thumb uses a bit less RAM, bytecode uses a tiny bit more, to avoid
overflow of the dummy buffer in certain cases.

Addresses issue #599.
2014-05-10 18:07:08 +01:00
Damien George
7db57bf6b2 Merge branch 'master' of github.com:micropython/micropython 2014-05-10 17:50:05 +01:00
Paul Sokolovsky
6913521911 objstr: Implement .lower() and .upper(). 2014-05-10 19:49:07 +03:00
Damien George
b0edec61ac stmhal: Improve handling of out-of-memory in REPL.
Addresses issue #558, but it's likely that other out-of-memory errors
could crash the pyboard.  Reason is that qstrs use m_new and can raise
an exception within the parser.
2014-05-10 17:48:46 +01:00
Damien George
e1199ecf10 py, lexer: Add allocation policy config; return NULL if can't allocate. 2014-05-10 17:48:01 +01:00
Damien George
1b82e9af5c py: Improve handling of memory error in parser.
Parser shouldn't raise exceptions, so needs to check when memory
allocation fails.  This patch does that for the initial set up of the
parser state.

Also, we now put the parser object on the stack.  It's small enough to
go there instead of on the heap.

This partially addresses issue #558.
2014-05-10 17:36:41 +01:00
Paul Sokolovsky
ad6178bb08 builtinimport: Fix broken namespace imports due to dup vstr_cut_tail_bytes(). 2014-05-10 19:00:03 +03:00
Paul Sokolovsky
f9589d2f23 builtinimport: Fix comment orphaned by one of previous commits. 2014-05-10 18:46:02 +03:00
Paul Sokolovsky
deaeaac469 modsys: Enable sys.exit() per port after all. 2014-05-10 17:26:47 +03:00
Paul Sokolovsky
37b0f33545 objset: Add frozenset tests, skippable if frozenset not available. 2014-05-10 16:56:21 +03:00
Paul Sokolovsky
43d4a6fa31 run-tests: Add support for skipping tests.
MicrpPython test should print single "SKIP" line for test to be skipped.
2014-05-10 16:56:21 +03:00
Paul Sokolovsky
0f14fdea0c stmhal: Implement draft version of sys.exit(). 2014-05-10 16:56:21 +03:00
Paul Sokolovsky
d99e9083cb modsys, unix: Add sys.exit(), should be implemented by a port. 2014-05-10 16:56:21 +03:00
Paul Sokolovsky
d80e2476c7 py: Disable frozenset by default, enable on unix.
Takes 416 text bytes on x86.
2014-05-10 16:56:20 +03:00
Paul Sokolovsky
b181b581aa objset: Give up and implement frozenset.
Tired of patching CPython stdlib for it.
2014-05-10 16:56:20 +03:00
Paul Sokolovsky
d86020ac4f objtype: Don't treat inheritance from "object" as from native type.
"object" type in MicroPython currently doesn't implement any methods, and
hopefully, we'll try to stay like that for as long as possible. Even if we
have to add something eventually, look up from there might be handled in
adhoc manner, as last resort (that's not compliant with Python3 MRO, but
we're already non-compliant). Hence: 1) no need to spend type trying to
lookup anything in object; 2) no need to allocate subobject when explicitly
inheriting from object; 3) and having multiple bases inheriting from object
is not a case of incompatible multiple inheritance.
2014-05-10 16:56:20 +03:00
Damien George
d0a5bf34f7 py: Tidy up returning NULL which should be MP_OBJ_NOT_SUPPORTED. 2014-05-10 13:55:11 +01:00
Damien George
2bb179e124 bare-arm: Change output file from flash.elf to firmware.elf. 2014-05-10 13:45:47 +01:00
Damien George
ccc85ea0da py: Combine native emitters to 1 glue function; distinguish viper.
This patch simplifies the glue between native emitter and runtime,
and handles viper code like inline assember: return values are
converted to Python objects.

Fixes issue #531.
2014-05-10 13:40:46 +01:00
Damien George
04b7cc4df0 stmhal: Fix setting of RTC: was BCD now BIN encoded.
Addresses issue #592.
2014-05-10 11:56:58 +01:00
Damien George
c17fd70de9 stm: Reorder mpconfig.h header inclusion to get stm building. 2014-05-10 10:38:38 +01: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
f45a83d7fc mingw: Fix compilation issues
- use lowercase windows.h
- fix for mingw32 using preprocessor-unfriendly definition of CLOCKS_PER_SEC
2014-05-10 10:42:40 +02:00
Paul Sokolovsky
6e8085b425 py: Fix base "detection" for int('0<hexdigit>', 16). 2014-05-10 04:45:15 +03:00
Paul Sokolovsky
7b0f9a7d9b bytes: Implement comparison and other binary operations.
Should support everything supported by strings.
2014-05-10 04:45:02 +03:00
Paul Sokolovsky
070c78af5d runtime0.h: Group binary ops by fives.
So one has some chance to convert numeric op code into symbol.
2014-05-10 04:44:55 +03:00
Paul Sokolovsky
affa870cc2 Merge pull request #575 from stinos/windows-modtime
Add modtime implementation for mingw
2014-05-09 22:09:10 +03:00
stijn
5ed284a15e windows: Add modtime implementation 2014-05-09 13:58:15 +02:00
Damien George
d25cba4f64 Merge branch 'pfalcon-README-features-overview' 2014-05-09 12:01:15 +01:00
Damien George
65114ca015 README: Add articles, and update doc for deploying firmware. 2014-05-09 12:00:23 +01:00
Paul Sokolovsky
ad79ecdf96 README: Add short overview of Python features supported.
Also, "upgrade" project to "early beta", and elaborate pyboard description.
2014-05-09 04:15:24 +03:00
Damien George
8c1c7488b2 Add gc.enable, gc.disable; remove pyb.gc. 2014-05-08 23:04:49 +01:00
Damien George
d6cbbc51ab stmhal: Add time.time() and time.localtime().
time.time: returns seconds since 1/1/2000, as an integer.

time.localtime: Returns 8-tuple: (year, month, date, hour, minute,
    second, weekday, yearday).
2014-05-08 22:25:49 +01:00
Paul Sokolovsky
62b5f42d81 Merge pull request #568 from stinos/windows-msvc-port
Windows MS Visual C port
2014-05-09 00:03:42 +03:00
Paul Sokolovsky
9e76b1181b Draft approach towards resolving https://github.com/micropython/micropython/issues/560#issuecomment-42213955 2014-05-08 22:43:46 +03:00
Damien George
ffae48d750 py, compiler: Add basic support for A=const(123).
You can now do:

    X = const(123)
    Y = const(456 + X)

and the compiler will replace X and Y with their values.

See discussion in issue #266 and issue #573.
2014-05-08 15:58:39 +00:00
stijn
01d6be4d51 Windows MSVC port
Extend the windows port so it compiles with the toolchain from Visual Studio 2013
2014-05-08 10:06:43 +02:00
Ilya Dmitrichenko
c1c32d65af qemu-arm: fully integrated test suite.
This is primarily intended to provide testing of Thumb-specific code within
Travis CI as well as if anyone else want to run it locally.  As discussed in
purposes.  This is currently agains an emulated Cortex-M3 core, however in
the near future it can extended to support M0, M0+ as well M4 (work in
progress exists in sushihangover/qemu).

It's probably true that most of the code base can be covered running uPy
natively on a POSIX system, however we do have the tiny bit of assembly
code.  There may exist bugs related to endianness and type aliases, let
alone potential standard library or compiler bugs or even
architecture-specific optimisations.

This could also incorporate lwIP (or other TCP/IP stack) integration as well
as SDIO+FATFS drivers.

The solution to inline the test cases was chose due to simplicity. It could
alternatively be implemented in a number of different way (see #515), but
this looked the simplest.

Inclusion of tinytest was just to avoid writing boilerplate code for
counting failed tests and other utility functions.  Currently only a few
functions are used, however this could be extended.  Checking in the code
instead of using submodule was a personal preference, but if people do want
the pain of submodules, this can provided.  This particular framework is
also pretty good if one desires to run unit test on target.  The approach
with scripts being inlined is probably not quite suited for the size of
memory an MCU has, but the tinytest itself should be good, if lower-level C
code is to be unit tested.
2014-05-08 01:41:32 +03:00
Ilya Dmitrichenko
be86596bb9 tools: inline test suite generator. 2014-05-08 01:41:22 +03:00
Ilya Dmitrichenko
b1442e04d1 tools: check-in errordeveloper/tinytest@eb2dbc858f 2014-05-08 01:41:21 +03:00
Damien George
d509ac25f9 py: Fix stack access in thumb native emitter. 2014-05-07 23:27:45 +01:00
Paul Sokolovsky
be6aa53cdb Merge pull request #584 from stinos/windows-input
windows: Fix input.c missing in Makefile after changes for #582
2014-05-07 22:44:19 +03:00
Paul Sokolovsky
69cbec4afb tests/bench: Add testcase for positional/kwargs to enumerate().
Inspired by discussion in #577. So, in this case of builtin function,
passing args by keyword has less than 1% overhead.
2014-05-07 22:34:06 +03:00
Paul Sokolovsky
2a05f05f44 tests/bench: Add tests for various ways to pass function args.
Passing 3 args with keywords is for example 50% slower than via positional
args.
2014-05-07 22:34:04 +03:00
Paul Sokolovsky
1695151267 tests/bench: Add variation on loop_count/while_down_ne test. 2014-05-07 22:34:04 +03:00
Paul Sokolovsky
6638ea9ca3 tests/bench: Add testcases for lookup in 5-el instance and namedtuple.
... and we have not that bad mapping type after all - lookup time is ~ the
same as in one-attr instance. My namedtuple implementation on the other
hand degrades awfully.

So, need to rework it. First observation is that named tuple fields are
accessed as attributes, so all names are interned at the program start.
Then, really should store field array as qstr[], and do quick 32/64 bit
scan thru it.
2014-05-07 22:34:00 +03:00
Paul Sokolovsky
52b25293e2 tests/bench: Time namedtuple field access.
That's higher than instance field access - behold the power of hashing.
2014-05-07 22:33:37 +03:00
stijn
951335e102 windows: Fix input.c missing in Makefile after changes for #582 2014-05-07 21:15:00 +02:00
Damien George
c3602e159c py: Fix emitcpy, to work with latest changes to PASS variables. 2014-05-07 18:57:32 +01:00
Damien George
9102af6afb tests: Add a test for native code on pyboard. 2014-05-07 18:55:31 +01:00
Damien George
c4ccb078a5 tests: Add inline assembler test for pyboard. 2014-05-07 18:31:14 +01:00
Damien George
a32c1e41cc py: Improve native emitter; now supports more opcodes. 2014-05-07 18:30:52 +01:00
Damien George
36db6bcf54 py, compiler: Improve passes; add an extra pass for native emitter. 2014-05-07 17:24:22 +01:00
Damien George
ca25c15d56 py, compiler: Start adding support for compile-time constants.
Just a start, no working code yet.  As per issue #573.
2014-05-07 15:42:03 +01:00
Damien George
7c6c843965 unix: Add missing stdio.h header for readline. 2014-05-07 15:33:15 +01:00
Damien George
c35e53436b Merge pull request #582 from dhylands/unix-input
Add input command for unix
2014-05-07 15:30:15 +01:00
Dave Hylands
117c46d9eb Add input command for unix 2014-05-07 07:19:51 -07:00
Damien George
1dd46fafbd Merge pull request #581 from stinos/windows-math
windows: Enable math module
2014-05-07 12:11:32 +01:00
stijn
3ce10935f0 windows: Enable math module 2014-05-07 12:39:02 +02:00
Paul Sokolovsky
0ef015b253 stream: Make non-blcoking stream support configurable.
Enable only on unix. To avoid unpleasant surprises with error codes.
2014-05-07 02:25:45 +03:00
Paul Sokolovsky
6c62e7257f unix modsocket: Add comments re: recv() vs read(), etc. semantics. 2014-05-07 02:17:14 +03:00
Paul Sokolovsky
b9be45e421 stream: Use standard name of DEFAULT_BUFFER_SIZE. 2014-05-07 02:17:14 +03:00
Paul Sokolovsky
6e73143de8 stream: Add compliant handling of non-blocking readall(). 2014-05-07 02:17:14 +03:00
Paul Sokolovsky
a592104acd stream: Add compliant handling of non-blocking read()/write().
In case of empty non-blocking read()/write(), both return None. read()
cannot return 0, as that means EOF, so returns another value, and then
write() just follows. This is still pretty unexpected, and typical
"if not len:" check would treat this as EOF. Well, non-blocking files
require special handling!

This also kind of makes it depending on POSIX, but well, anything else
should emulate POSIX anyway ;-).
2014-05-07 02:17:14 +03:00
Damien George
93afa230a4 py, parser: Add commented-out code to discard doc strings.
Doesn't help with RAM reduction because doc strings are interned as soon
as they are encountered, which is too soon to do any optimisations on
them.
2014-05-06 21:44:11 +01:00
Damien George
c53b408f28 Merge branch 'master' of https://github.com/micropython/micropython
Conflicts:
	py/argcheck.c
	py/objenumerate.c
	py/runtime.h
2014-05-06 16:52:35 +00:00
Damien George
491cbd6a7c py: Add keyword arg support to enumerate constructor.
Need to have a policy as to how far we go adding keyword support to
built ins.  It's nice to have, and gets better CPython compatibility,
but hurts the micro nature of uPy.

Addresses issue #577.
2014-05-06 16:38:54 +00:00
Paul Sokolovsky
b473d0ae86 py: bytes(), str(): Add NotImplementedError for kwargs.
Addresses #567.
2014-05-06 19:31:58 +03:00
Paul Sokolovsky
47d3bd3b31 py: enumerate(): Add NotImplementedError for kwargs.
Addresses #577.
2014-05-06 19:31:49 +03:00
Paul Sokolovsky
33b3a6905d stmhal: pyb: Use gc() function as defined by standard module "gc".
TODO: Get rid of this compatibility define and rely on standard module.
2014-05-06 02:28:49 +03:00
Paul Sokolovsky
f9e54e0ea5 modgc: Add new module for GC-related functionality. 2014-05-06 02:28:49 +03: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
Paul Sokolovsky
179977a0da py-version.sh: Use --always option of git describe. 2014-05-05 21:28:12 +03:00
Damien George
fcb347b90a Merge pull request #571 from dhylands/fix-extint-doc
Change references (in comments) of pyb.GPIO to be pyb.Pin
2014-05-05 19:03:25 +01:00
Dave Hylands
1145a0706c Change references (in comments) of pyb.GPIO to be pyb.Pin
The documentation at http://micropython.org/doc/module/pyb/ExtInt should also be
updated.
2014-05-05 10:58:38 -07:00
Damien George
fa2e701e23 examples, SDdatalogger: Add more comments; reduce power consumption. 2014-05-05 14:09:23 +01:00
Damien George
2a5b3cdf82 Merge pull request #566 from turbinenreiter/master
added SDdatalogger example
2014-05-05 14:02:38 +01:00
Damien George
66e18f04d8 py: Turn down amount of RAM parser and compiler use.
There are 2 locations in parser, and 1 in compiler, where memory
allocation is not precise.  In the parser it's the rule stack and result
stack, in the compiler it's the array for the identifiers in the current
scope.  All other mallocs are exact (ie they don't allocate more than is
needed).

This patch adds tuning options (MP_ALLOC_*) to mpconfig.h for these 3
inexact allocations.

The inexact allocations in the parser should actually be close to
logarithmic: you need an exponentially larger script (absent pathological
cases) to use up more room on the rule and result stacks.  As such, the
default allocation policy for these is now to start with a modest sized
stack, but grow only in small increments.

For the identifier arrays in the compiler, these now start out quite
small (4 entries, since most functions don't have that many ids), and
grow incrementally by 6 (since if you have more ids than 4, you probably
have quite a few more, but it wouldn't be exponentially more).

Partially addresses issue #560.
2014-05-05 13:19:03 +01:00
Paul Sokolovsky
f01fa458d8 tests/bench/var: Add tests for class/instance var access.
Also compared with method abstraction for accessing instance vars -
it's more than 3 times slower than accessing var directly.
2014-05-05 02:17:13 +03:00
Paul Sokolovsky
aaff82afe5 tests: Add framework for comparative benchmarking.
Motivation is optimizing handling of various constructs as well as
understanding which constructs are more efficient in MicroPython.
More info: http://forum.micropython.org/viewtopic.php?f=3&t=77

Results are wildly unexpected. For example, "optimization" of range
iteration into while loop makes it twice as slow. Generally, the more
bytecodes, the slower the code.
2014-05-05 01:24:16 +03:00
Paul Sokolovsky
22a0d67c0f py-version.sh: Make it work in case no git tag is present. 2014-05-05 01:01:01 +03:00
Paul Sokolovsky
6b344d7816 py, unix: Add -v option, print bytecode dump if used.
This will work if MICROPY_DEBUG_PRINTERS is defined, which is only for
unix/windows ports. This makes it convenient to user uPy normally, but
easily get bytecode dump on the spot if needed, without constant recompiles
back and forth.

TODO: Add more useful debug output, adjust verbosity level on which
specifically bytecode dump happens.
2014-05-05 00:57:00 +03:00
Paul Sokolovsky
4187068cad showbc: Quote block name, so it was easily visible. 2014-05-04 22:42:11 +03:00
Sebastian Plamauer
98243ccca4 deleted garbage 2014-05-04 19:08:14 +02:00
Sebastian Plamauer
96e97ed2ce created SDdatalogger example 2014-05-04 19:07:17 +02:00
Damien George
5fc400ccdb stmhal: Document physical pins for SPI, I2C, UART busses. 2014-05-04 14:28:11 +01:00
Damien George
cda363a036 tests, pyb: Add 'import pyb' when needed. 2014-05-04 12:40:51 +01:00
Damien George
c327c0de5d unix: Remove test class and code. 2014-05-04 12:24:26 +01:00
Damien George
2c9c200494 Merge pull request #563 from turbinenreiter/patch-2
updated to use new pyb.Accel() object
2014-05-04 12:17:18 +01:00
Sebastian Plamauer
c114565bfa updated to use new pyb.Accel() object 2014-05-04 13:11:16 +02:00
Damien George
113872af6f Merge pull request #561 from turbinenreiter/patch-1
updated to fit new acceleration and time/millis
2014-05-04 12:00:19 +01:00
Sebastian Plamauer
3526716a5b updated to fit new acceleration and time/millis
Changed pyb.accel() and pyb.time() to the new pyb.Accel() object and pyb.millis() function.
Also shortened the loop so the writing is finished before the USB connection messes things up.
2014-05-04 12:53:01 +02:00
Damien George
37936bebbf tools: In build-stm-latest, replace git hash with git tag. 2014-05-04 00:11:09 +00: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
Damien George
ff380c2558 Merge pull request #535 from pfalcon/blurb
Proposed license/copyright file header.
2014-05-03 22:56:50 +01:00
Damien George
75aebda809 Merge branch 'stinos-mingw-float-printf' 2014-05-03 22:44:24 +01:00
Damien George
3a01840390 windows: Argument to () function should be (void). 2014-05-03 22:43:58 +01:00
Damien George
cff638a43a Merge branch 'mingw-float-printf' of github.com:stinos/micropython into stinos-mingw-float-printf 2014-05-03 22:41:40 +01:00
Damien George
d5f5b2f766 py, stream: Implement readlines for a stream. 2014-05-03 22:01:32 +01:00
Damien George
5320bff32c Merge pull request #557 from cjbarnes18/device_id_in_deploy
Add device ID to deploy make target for stmhal.
2014-05-03 21:21:20 +01:00
Damien George
349e4c4a2f py: Add --dirty flag to git describe. 2014-05-03 21:15:32 +01:00
Craig Barnes
c9f9e547d6 Add device ID to deploy make target for stmhal. 2014-05-03 19:51:47 +01:00
stijn
72521a1c17 mingw: Fix number of exponent digits in floating point formatting
By default mingw outputs 3 digits instead of the standard 2 so all float
tests using printf fail. Using setenv at the start of the program fixes this.
To accomodate calling platform specific initialization a
MICROPY_MAIN_INIT_FUNC macro is used which is called in mp_init()
2014-05-03 20:15:15 +02:00
Paul Sokolovsky
d32bab27bb py: Add copyright/license header to each file. 2014-05-01 02:54:16 +03:00
800 changed files with 22773 additions and 100209 deletions

View File

@@ -7,7 +7,7 @@ before_script:
- sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
- sudo add-apt-repository -y ppa:terry.guo/gcc-arm-embedded
- sudo apt-get update -qq
- sudo apt-get install -y python3.3 python3 gcc-4.7 gcc-arm-none-eabi qemu-system
- sudo apt-get install -y python3.3 python3 gcc-4.7 gcc-arm-none-eabi qemu-system mingw32
script:
- make -C unix CC=gcc-4.7
@@ -15,7 +15,9 @@ script:
- make -C bare-arm
- make -C qemu-arm
- make -C stmhal
- make -C windows CROSS_COMPILE=i586-mingw32msvc-
- (cd tests && MICROPY_CPYTHON3=python3.3 ./run-tests)
after_failure:
- (cd tests && for exp in *.exp; do testbase=$(basename $exp .exp); echo -e "\nFAILURE $testbase"; diff $testbase.exp $testbase.out; done)
- (cd tests && for exp in *.exp; do testbase=$(basename $exp .exp); echo -e "\nFAILURE $testbase"; diff -u $testbase.exp $testbase.out; done)

View File

@@ -1,6 +1,6 @@
The MIT License (MIT)
Copyright (c) 2013 Damien P. George
Copyright (c) 2013, 2014 Damien P. George
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -11,26 +11,31 @@ The Micro Python project
This is the Micro Python project, which aims to put an implementation
of Python 3.x on a microcontroller.
WARNING: this project is in its early stages and is subject to large
WARNING: this project is in early beta stage and is subject to large
changes of the code-base, including project-wide name changes and API
changes.
Micro Python implements the entire Python 3.4 syntax (including exceptions,
"with", "yield from", etc.). The following core datatypes are provided:
str (no Unicode support yet), bytes, bytearray, tuple, list, dict, set,
array.array, collections.namedtuple, classes and instances. Builtin
modules include sys, time, and struct. Note that only subset of
Python 3.4 functionality implemented for the data types and modules.
See the repository www.github.com/micropython/pyboard for the Micro
Python board.
Python board, the officially supported reference electronic circuit board.
Major components in this repository:
- py/ -- the core Python implementation, including compiler and runtime.
- unix/ -- a version of Micro Python that runs on Unix.
- stmhal/ -- a version of Micro Python that runs on the Micro Python board
with an STM32F405RG (using ST's new Cube HAL drivers).
with an STM32F405RG (using ST's Cube HAL drivers).
- teensy/ -- a version of Micro Python that runs on the Teensy 3.1
(preliminary but functional).
Additional components:
- bare-arm/ -- a bare minimum version of Micro Python for ARM MCUs. Start
with this if you want to port Micro Python to another microcontroller.
- stm/ -- obsolete version of Micro Python for the Micro Python board
that uses ST's old peripheral drivers.
- unix-cpy/ -- a version of Micro Python that outputs bytecode (for testing).
- tests/ -- test framework and test scripts.
- tools/ -- various tools, including the pyboard.py module.
@@ -81,6 +86,10 @@ on the bottom left of the board, second row from the bottom).
Then to flash the code via USB DFU to your device:
$ dfu-util -a 0 -D build/flash.dfu
$ make deploy
You will need the dfu-util program, on Arch Linux it's dfu-util-git in the AUR.
You will need the dfu-util program, on Arch Linux it's dfu-util-git in the
AUR. If the above does not work it may be because you don't have the
correct permissions. Try then:
$ sudo dfu-util -a 0 -D build-PYBV10/firmware.dfu

View File

@@ -13,7 +13,7 @@ INC += -I$(PY_SRC)
INC += -I$(BUILD)
CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion
CFLAGS = $(INC) -Wall -Werror -ansi -std=gnu99 $(CFLAGS_CORTEX_M4) $(COPT)
CFLAGS = $(INC) -Wall -Werror -ansi -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) $(COPT)
#Debugging/Optimization
ifeq ($(DEBUG), 1)
@@ -22,7 +22,7 @@ else
CFLAGS += -Os -DNDEBUG
endif
LDFLAGS = --nostdlib -T stm32f405.ld
LDFLAGS = -nostdlib -T stm32f405.ld -Map=$@.map --cref
LIBS =
SRC_C = \
@@ -38,9 +38,9 @@ SRC_S = \
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o))
all: $(BUILD)/flash.elf
all: $(BUILD)/firmware.elf
$(BUILD)/flash.elf: $(OBJ)
$(BUILD)/firmware.elf: $(OBJ)
$(ECHO) "LINK $@"
$(Q)$(LD) $(LDFLAGS) -o $@ $(OBJ) $(LIBS)
$(Q)$(SIZE) $@

View File

@@ -2,26 +2,33 @@
// options to control how Micro Python is built
#define MICROPY_ALLOC_PATH_MAX (512)
#define MICROPY_EMIT_X64 (0)
#define MICROPY_EMIT_THUMB (0)
#define MICROPY_EMIT_INLINE_THUMB (0)
#define MICROPY_MEM_STATS (0)
#define MICROPY_DEBUG_PRINTERS (0)
#define MICROPY_ENABLE_GC (0)
#define MICROPY_ENABLE_REPL_HELPERS (0)
#define MICROPY_ENABLE_LEXER_UNIX (0)
#define MICROPY_HELPER_REPL (0)
#define MICROPY_HELPER_LEXER_UNIX (0)
#define MICROPY_ENABLE_SOURCE_LINE (0)
#define MICROPY_ENABLE_MOD_COLLECTIONS (0)
#define MICROPY_ENABLE_MOD_MATH (0)
#define MICROPY_ENABLE_MOD_CMATH (0)
#define MICROPY_ENABLE_MOD_IO (0)
#define MICROPY_ENABLE_MOD_STRUCT (0)
#define MICROPY_ENABLE_MOD_SYS (0)
#define MICROPY_ENABLE_PROPERTY (0)
#define MICROPY_PY_BUILTINS_BYTEARRAY (0)
#define MICROPY_PY_BUILTINS_FROZENSET (0)
#define MICROPY_PY_BUILTINS_SET (0)
#define MICROPY_PY_BUILTINS_SLICE (0)
#define MICROPY_PY_BUILTINS_PROPERTY (0)
#define MICROPY_PY_ARRAY (0)
#define MICROPY_PY_COLLECTIONS (0)
#define MICROPY_PY_MATH (0)
#define MICROPY_PY_CMATH (0)
#define MICROPY_PY_IO (0)
#define MICROPY_PY_STRUCT (0)
#define MICROPY_PY_SYS (0)
#define MICROPY_CPYTHON_COMPAT (0)
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE)
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE)
#define MICROPY_PATH_MAX (512)
//#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE)
// type definitions for the specific machine
@@ -30,13 +37,15 @@
#define UINT_FMT "%lu"
#define INT_FMT "%ld"
typedef int32_t machine_int_t; // must be pointer size
typedef uint32_t machine_uint_t; // must be pointer size
typedef int32_t mp_int_t; // must be pointer size
typedef uint32_t mp_uint_t; // must be pointer size
typedef void *machine_ptr_t; // must be of pointer size
typedef const void *machine_const_ptr_t; // must be of pointer size
// extra built in names to add to the global namespace
extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
#define MICROPY_EXTRA_BUILTINS \
#define MICROPY_PORT_BUILTINS \
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },
// We need to provide a declaration/definition of alloca()
#include <alloca.h>

View File

@@ -0,0 +1,4 @@
This is a SDdatalogger, to log data from the accelerometer to the SD-card. It also functions as card reader, so you can easily get the data on your PC.
To run, put the boot.py, cardreader.py and datalogger.py files on either the flash or the SD-card of your pyboard.
Upon reset, the datalogger script is run and logs the data. If you press the user button after reset and hold it until the orange LED goes out, you enter the cardreader mode and the filesystem is mounted to your PC.

View File

@@ -0,0 +1,25 @@
# boot.py -- runs on boot-up
# Let's you choose which script to run.
# > To run 'datalogger.py':
# * press reset and do nothing else
# > To run 'cardreader.py':
# * press reset
# * press user switch and hold until orange LED goes out
import pyb
pyb.LED(3).on() # indicate we are waiting for switch press
pyb.delay(2000) # wait for user to maybe press the switch
switch_value = pyb.Switch()() # sample the switch at end of delay
pyb.LED(3).off() # indicate that we finished waiting for the switch
pyb.LED(4).on() # indicate that we are selecting the mode
if switch_value:
pyb.usb_mode('CDC+MSC')
pyb.main('cardreader.py') # if switch was pressed, run this
else:
pyb.usb_mode('CDC+HID')
pyb.main('datalogger.py') # if switch wasn't pressed, run this
pyb.LED(4).off() # indicate that we finished selecting the mode

View File

@@ -0,0 +1,2 @@
# cardread.py
# This is called when the user enters cardreader mode. It does nothing.

View File

@@ -0,0 +1,33 @@
# datalogger.py
# Logs the data from the acceleromter to a file on the SD-card
import pyb
# creating objects
accel = pyb.Accel()
blue = pyb.LED(4)
switch = pyb.Switch()
# loop
while True:
# wait for interrupt
# this reduces power consumption while waiting for switch press
pyb.wfi()
# start if switch is pressed
if switch():
pyb.delay(200) # delay avoids detection of multiple presses
blue.on() # blue LED indicates file open
log = open('1:/log.csv', 'w') # open file on SD (SD: '1:/', flash: '0/)
# until switch is pressed again
while not switch():
t = pyb.millis() # get time
x, y, z = accel.filtered_xyz() # get acceleration data
log.write('{},{},{},{}\n'.format(t,x,y,z)) # write data to file
# end after switch is pressed again
log.close() # close file
blue.off() # blue LED indicates file closed
pyb.delay(200) # delay avoids detection of multiple presses

View File

@@ -1,14 +1,17 @@
# log the accelerometer values to a file, 1 per second
# log the accelerometer values to a .csv-file on the SD-card
f = open('motion.dat', 'w') # open the file for writing
import pyb
for i in range(60): # loop 60 times
time = pyb.time() # get the current time
accel = pyb.accel() # get the accelerometer data
accel = pyb.Accel() # create object of accelerometer
blue = pyb.LED(4) # create object of blue LED
# write time and x,y,z values to the file
f.write('{} {} {} {}\n'.format(time, accel[0], accel[1], accel[2]))
log = open('1:/log.csv', 'w') # open file to write data - 1:/ ist the SD-card, 0:/ the internal memory
blue.on() # turn on blue LED
pyb.delay(1000) # wait 1000 ms = 1 second
for i in range(100): # do 100 times (if the board is connected via USB, you can't write longer because the PC tries to open the filesystem which messes up your file.)
t = pyb.millis() # get time since reset
x, y, z = accel.filtered_xyz() # get acceleration data
log.write('{},{},{},{}\n'.format(t,x,y,z)) # write data to file
f.close() # close the file
log.close() # close file
blue.off() # turn off LED

View File

@@ -1,17 +1,20 @@
import pyb
def led_angle(seconds_to_run_for):
# make LED objects
l1 = pyb.Led(1)
l2 = pyb.Led(2)
l1 = pyb.LED(1)
l2 = pyb.LED(2)
accel = pyb.Accel()
for i in range(20 * seconds_to_run_for):
# get x-axis
accel = pyb.accel()[0]
x = accel.x()
# turn on LEDs depending on angle
if accel < -10:
if x < -10:
l1.on()
l2.off()
elif accel > 10:
elif x > 10:
l1.off()
l2.on()
else:

View File

@@ -5,4 +5,4 @@
def nodecor(x):
return x
byte_code = native = viper = nodecor
bytecode = native = viper = nodecor

45
examples/switch.py Normal file
View File

@@ -0,0 +1,45 @@
"""
switch.py
=========
Light up some leds when the USR switch on the pyboard is pressed.
Example Usage::
Micro Python v1.0.1 on 2014-05-12; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> import switch
>>> switch.run_loop()
Loop started.
Press Ctrl+C to break out of the loop.
"""
import pyb
switch = pyb.Switch()
red_led = pyb.LED(1)
green_led = pyb.LED(2)
orange_led = pyb.LED(3)
blue_led = pyb.LED(4)
all_leds = (red_led, green_led, orange_led, blue_led)
def run_loop(leds=all_leds):
"""
Start the loop.
:param `leds`: Which LEDs to light up upon switch press.
:type `leds`: sequence of LED objects
"""
print('Loop started.\nPress Ctrl+C to break out of the loop.')
while 1:
try:
if switch():
[led.on() for led in leds]
else:
[led.off() for led in leds]
except OSError: # VCPInterrupt # Ctrl+C in interpreter mode.
break
if __name__ == '__main__':
run_loop()

633
extmod/moductypes.c Normal file
View File

@@ -0,0 +1,633 @@
#include <stdio.h>
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2014 Paul Sokolovsky
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <assert.h>
#include <string.h>
#include <stdint.h>
#include "mpconfig.h"
#include "misc.h"
#include "nlr.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
#include "objtuple.h"
#include "binary.h"
#if MICROPY_PY_UCTYPES
/// \module uctypes - Access data structures in memory
///
/// The module allows to define layout of raw data structure (using terms
/// of C language), and then access memory buffers using this definition.
/// The module also provides convenience functions to access memory buffers
/// contained in Python objects or wrap memory buffers in Python objects.
/// \constant UINT8_1 - uint8_t value type
/// \class struct - C-like structure
///
/// Encapsulalation of in-memory data structure. This class doesn't define
/// any methods, only attribute access (for structure fields) and
/// indexing (for pointer and array fields).
///
/// Usage:
///
/// # Define layout of a structure with 2 fields
/// # 0 and 4 are byte offsets of fields from the beginning of struct
/// # they are logically ORed with field type
/// FOO_STRUCT = {"a": 0 | uctypes.UINT32, "b": 4 | uctypes.UINT8}
///
/// # Example memory buffer to access (contained in bytes object)
/// buf = b"\x64\0\0\0\0x14"
///
/// # Create structure object referring to address of
/// # the data in the buffer above
/// s = uctypes.struct(FOO_STRUCT, uctypes.addressof(buf))
///
/// # Access fields
/// print(s.a, s.b)
/// # Result:
/// # 100, 20
#define LAYOUT_LITTLE_ENDIAN (0)
#define LAYOUT_BIG_ENDIAN (1)
#define LAYOUT_NATIVE (2)
#define VAL_TYPE_BITS 4
#define BITF_LEN_BITS 5
#define BITF_OFF_BITS 5
#define OFFSET_BITS 17
#if VAL_TYPE_BITS + BITF_LEN_BITS + BITF_OFF_BITS + OFFSET_BITS != 31
#error Invalid encoding field length
#endif
enum {
UINT8, INT8, UINT16, INT16,
UINT32, INT32, UINT64, INT64,
BFUINT8, BFINT8, BFUINT16, BFINT16,
BFUINT32, BFINT32,
FLOAT32, FLOAT64,
};
#define AGG_TYPE_BITS 2
enum {
STRUCT, PTR, ARRAY, BITFIELD,
};
// Here we need to set sign bit right
#define TYPE2SMALLINT(x, nbits) ((((int)x) << (32 - nbits)) >> 1)
#define GET_TYPE(x, nbits) (((x) >> (31 - nbits)) & ((1 << nbits) - 1));
// Bit 0 is "is_signed"
#define GET_SCALAR_SIZE(val_type) (1 << ((val_type) >> 1))
#define VALUE_MASK(type_nbits) ~((int)0x80000000 >> type_nbits)
STATIC const mp_obj_type_t uctypes_struct_type;
typedef struct _mp_obj_uctypes_struct_t {
mp_obj_base_t base;
mp_obj_t desc;
byte *addr;
uint32_t flags;
} mp_obj_uctypes_struct_t;
STATIC NORETURN void syntax_error() {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "syntax error in uctypes descriptor"));
}
STATIC mp_obj_t uctypes_struct_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
if (n_args < 2 || n_args > 3) {
syntax_error();
}
mp_obj_uctypes_struct_t *o = m_new_obj(mp_obj_uctypes_struct_t);
o->base.type = type_in;
o->desc = args[0];
o->addr = (void*)mp_obj_get_int(args[1]);
o->flags = LAYOUT_NATIVE;
if (n_args == 3) {
o->flags = mp_obj_get_int(args[2]);
}
return o;
}
STATIC void uctypes_struct_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
mp_obj_uctypes_struct_t *self = self_in;
const char *typen = "unk";
if (MP_OBJ_IS_TYPE(self->desc, &mp_type_dict)) {
typen = "STRUCT";
} else if (MP_OBJ_IS_TYPE(self->desc, &mp_type_tuple)) {
mp_obj_tuple_t *t = (mp_obj_tuple_t*)self->desc;
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(t->items[0]);
uint agg_type = GET_TYPE(offset, AGG_TYPE_BITS);
switch (agg_type) {
case PTR: typen = "PTR"; break;
case ARRAY: typen = "ARRAY"; break;
}
} else {
typen = "ERROR";
}
print(env, "<struct %s %p>", typen, self->addr);
}
static inline mp_uint_t uctypes_struct_scalar_size(int val_type) {
if (val_type == FLOAT32) {
return 4;
} else {
return GET_SCALAR_SIZE(val_type & 7);
}
}
STATIC mp_uint_t uctypes_struct_size(mp_obj_t desc_in, mp_uint_t *max_field_size) {
mp_obj_dict_t *d = desc_in;
mp_uint_t total_size = 0;
if (!MP_OBJ_IS_TYPE(desc_in, &mp_type_dict)) {
syntax_error();
}
for (mp_uint_t i = 0; i < d->map.alloc; i++) {
if (MP_MAP_SLOT_IS_FILLED(&d->map, i)) {
mp_obj_t v = d->map.table[i].value;
if (MP_OBJ_IS_SMALL_INT(v)) {
mp_uint_t offset = MP_OBJ_SMALL_INT_VALUE(v);
mp_uint_t val_type = GET_TYPE(offset, VAL_TYPE_BITS);
offset &= VALUE_MASK(VAL_TYPE_BITS);
mp_uint_t s = uctypes_struct_scalar_size(val_type);
if (s > *max_field_size) {
*max_field_size = s;
}
if (offset + s > total_size) {
total_size = offset + s;
}
} else {
if (!MP_OBJ_IS_TYPE(v, &mp_type_tuple)) {
syntax_error();
}
mp_obj_tuple_t *t = (mp_obj_tuple_t*)v;
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(t->items[0]);
mp_uint_t agg_type = GET_TYPE(offset, AGG_TYPE_BITS);
offset &= VALUE_MASK(AGG_TYPE_BITS);
switch (agg_type) {
case STRUCT: {
mp_uint_t s = uctypes_struct_size(t->items[1], max_field_size);
if (offset + s > total_size) {
total_size = offset + s;
}
break;
}
case PTR: {
if (offset + sizeof(void*) > total_size) {
total_size = offset + sizeof(void*);
}
if (sizeof(void*) > *max_field_size) {
*max_field_size = sizeof(void*);
}
break;
}
case ARRAY: {
mp_int_t arr_sz = MP_OBJ_SMALL_INT_VALUE(t->items[1]);
uint val_type = GET_TYPE(arr_sz, VAL_TYPE_BITS);
arr_sz &= VALUE_MASK(VAL_TYPE_BITS);
mp_uint_t item_s;
if (t->len == 2) {
item_s = GET_SCALAR_SIZE(val_type);
if (item_s > *max_field_size) {
*max_field_size = item_s;
}
} else {
item_s = uctypes_struct_size(t->items[2], max_field_size);
}
mp_uint_t byte_sz = item_s * arr_sz;
if (offset + byte_sz > total_size) {
total_size = offset + byte_sz;
}
break;
}
default:
assert(0);
}
}
}
}
// Round size up to alignment of biggest field
total_size = (total_size + *max_field_size - 1) & ~(*max_field_size - 1);
return total_size;
}
STATIC mp_obj_t uctypes_struct_sizeof(mp_obj_t obj_in) {
mp_uint_t max_field_size = 0;
if (MP_OBJ_IS_TYPE(obj_in, &uctypes_struct_type)) {
mp_obj_uctypes_struct_t *obj = obj_in;
obj_in = obj->desc;
}
mp_uint_t size = uctypes_struct_size(obj_in, &max_field_size);
return MP_OBJ_NEW_SMALL_INT(size);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(uctypes_struct_sizeof_obj, uctypes_struct_sizeof);
STATIC inline mp_obj_t get_unaligned(uint val_type, void *p, int big_endian) {
mp_int_t val = mp_binary_get_int(GET_SCALAR_SIZE(val_type), val_type & 1, big_endian, p);
if (val_type == UINT32) {
return mp_obj_new_int_from_uint(val);
} else {
return mp_obj_new_int(val);
}
}
STATIC inline void set_unaligned(uint val_type, void *p, int big_endian, mp_obj_t val) {
char struct_type = big_endian ? '>' : '<';
static const char type2char[8] = "BbHhIiQq";
mp_binary_set_val(struct_type, type2char[val_type], val, (byte**)&p);
}
static inline mp_uint_t get_aligned_basic(uint val_type, void *p) {
switch (val_type) {
case UINT8:
return *(uint8_t*)p;
case UINT16:
return *(uint16_t*)p;
case UINT32:
return *(uint32_t*)p;
}
assert(0);
return 0;
}
static inline void set_aligned_basic(uint val_type, void *p, mp_uint_t v) {
switch (val_type) {
case UINT8:
*(uint8_t*)p = (uint8_t)v; return;
case UINT16:
*(uint16_t*)p = (uint16_t)v; return;
case UINT32:
*(uint32_t*)p = (uint32_t)v; return;
}
assert(0);
}
STATIC mp_obj_t get_aligned(uint val_type, void *p, mp_int_t index) {
switch (val_type) {
case UINT8:
return MP_OBJ_NEW_SMALL_INT((mp_int_t)((uint8_t*)p)[index]);
case INT8:
return MP_OBJ_NEW_SMALL_INT((mp_int_t)((int8_t*)p)[index]);
case UINT16:
return MP_OBJ_NEW_SMALL_INT((mp_int_t)((uint16_t*)p)[index]);
case INT16:
return MP_OBJ_NEW_SMALL_INT((mp_int_t)((int16_t*)p)[index]);
case UINT32:
return mp_obj_new_int_from_uint(((uint32_t*)p)[index]);
case INT32:
return mp_obj_new_int(((int32_t*)p)[index]);
case UINT64:
case INT64:
return mp_obj_new_int_from_ll(((int64_t*)p)[index]);
case FLOAT32:
return mp_obj_new_float(((float*)p)[index]);
case FLOAT64:
return mp_obj_new_float(((double*)p)[index]);
default:
assert(0);
}
}
STATIC void set_aligned(uint val_type, void *p, mp_int_t index, mp_obj_t val) {
mp_int_t v = mp_obj_get_int(val);
switch (val_type) {
case UINT8:
((uint8_t*)p)[index] = (uint8_t)v; return;
case INT8:
((int8_t*)p)[index] = (int8_t)v; return;
case UINT16:
((uint16_t*)p)[index] = (uint16_t)v; return;
case INT16:
((int16_t*)p)[index] = (int16_t)v; return;
case UINT32:
((uint32_t*)p)[index] = (uint32_t)v; return;
case INT32:
((int32_t*)p)[index] = (int32_t)v; return;
default:
assert(0);
}
}
STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set_val) {
mp_obj_uctypes_struct_t *self = self_in;
// TODO: Support at least OrderedDict in addition
if (!MP_OBJ_IS_TYPE(self->desc, &mp_type_dict)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "struct: no fields"));
}
mp_obj_t deref = mp_obj_dict_get(self->desc, MP_OBJ_NEW_QSTR(attr));
if (MP_OBJ_IS_SMALL_INT(deref)) {
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(deref);
mp_uint_t val_type = GET_TYPE(offset, VAL_TYPE_BITS);
offset &= VALUE_MASK(VAL_TYPE_BITS);
//printf("scalar type=%d offset=%x\n", val_type, offset);
if (val_type <= INT64) {
// printf("size=%d\n", GET_SCALAR_SIZE(val_type));
if (self->flags == LAYOUT_NATIVE) {
if (set_val == MP_OBJ_NULL) {
return get_aligned(val_type, self->addr + offset, 0);
} else {
set_aligned(val_type, self->addr + offset, 0, set_val);
return set_val; // just !MP_OBJ_NULL
}
} else {
if (set_val == MP_OBJ_NULL) {
return get_unaligned(val_type, self->addr + offset, self->flags);
} else {
set_unaligned(val_type, self->addr + offset, self->flags, set_val);
return set_val; // just !MP_OBJ_NULL
}
}
} else if (val_type >= BFUINT8 && val_type <= BFINT32) {
uint bit_offset = (offset >> 17) & 31;
uint bit_len = (offset >> 22) & 31;
offset &= (1 << 17) - 1;
mp_uint_t val;
if (self->flags == LAYOUT_NATIVE) {
val = get_aligned_basic(val_type & 6, self->addr + offset);
} else {
val = mp_binary_get_int(GET_SCALAR_SIZE(val_type & 7), val_type & 1, self->flags, self->addr + offset);
}
if (set_val == MP_OBJ_NULL) {
val >>= bit_offset;
val &= (1 << bit_len) - 1;
// TODO: signed
assert((val_type & 1) == 0);
return mp_obj_new_int(val);
} else {
mp_uint_t set_val_int = (mp_uint_t)mp_obj_get_int(set_val);
mp_uint_t mask = (1 << bit_len) - 1;
set_val_int &= mask;
set_val_int <<= bit_offset;
mask <<= bit_offset;
val = (val & ~mask) | set_val_int;
if (self->flags == LAYOUT_NATIVE) {
set_aligned_basic(val_type & 6, self->addr + offset, val);
} else {
mp_binary_set_int(GET_SCALAR_SIZE(val_type & 7), self->flags == LAYOUT_BIG_ENDIAN,
self->addr + offset, (byte*)&val);
}
return set_val; // just !MP_OBJ_NULL
}
}
assert(0);
return MP_OBJ_NULL;
}
if (!MP_OBJ_IS_TYPE(deref, &mp_type_tuple)) {
syntax_error();
}
if (set_val != MP_OBJ_NULL) {
// Cannot assign to aggregate
syntax_error();
}
mp_obj_tuple_t *sub = (mp_obj_tuple_t*)deref;
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(sub->items[0]);
mp_uint_t agg_type = GET_TYPE(offset, AGG_TYPE_BITS);
offset &= VALUE_MASK(AGG_TYPE_BITS);
//printf("agg type=%d offset=%x\n", agg_type, offset);
switch (agg_type) {
case STRUCT: {
mp_obj_uctypes_struct_t *o = m_new_obj(mp_obj_uctypes_struct_t);
o->base.type = &uctypes_struct_type;
o->desc = sub->items[1];
o->addr = self->addr + offset;
o->flags = self->flags;
return o;
}
case PTR: case ARRAY: {
mp_obj_uctypes_struct_t *o = m_new_obj(mp_obj_uctypes_struct_t);
o->base.type = &uctypes_struct_type;
o->desc = sub;
o->addr = self->addr + offset;
o->flags = self->flags;
//printf("PTR/ARR base addr=%p\n", o->addr);
return o;
}
}
// Should be unreachable once all cases are handled
return MP_OBJ_NULL;
}
STATIC void uctypes_struct_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
mp_obj_t val = uctypes_struct_attr_op(self_in, attr, MP_OBJ_NULL);
*dest = val;
}
STATIC bool uctypes_struct_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t val) {
return uctypes_struct_attr_op(self_in, attr, val) != MP_OBJ_NULL;
}
STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
mp_obj_uctypes_struct_t *self = self_in;
if (value == MP_OBJ_NULL) {
// delete
return MP_OBJ_NULL; // op not supported
} else if (value == MP_OBJ_SENTINEL) {
// load
if (!MP_OBJ_IS_TYPE(self->desc, &mp_type_tuple)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "struct: cannot index"));
}
mp_obj_tuple_t *t = (mp_obj_tuple_t*)self->desc;
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(t->items[0]);
uint agg_type = GET_TYPE(offset, AGG_TYPE_BITS);
mp_int_t index = MP_OBJ_SMALL_INT_VALUE(index_in);
if (agg_type == ARRAY) {
mp_int_t arr_sz = MP_OBJ_SMALL_INT_VALUE(t->items[1]);
uint val_type = GET_TYPE(arr_sz, VAL_TYPE_BITS);
arr_sz &= VALUE_MASK(VAL_TYPE_BITS);
if (index >= arr_sz) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "struct: index out of range"));
}
if (t->len == 2) {
byte *p = self->addr + GET_SCALAR_SIZE(val_type) * index;
return get_unaligned(val_type, p, self->flags);
} else {
mp_uint_t dummy = 0;
mp_uint_t size = uctypes_struct_size(t->items[2], &dummy);
mp_obj_uctypes_struct_t *o = m_new_obj(mp_obj_uctypes_struct_t);
o->base.type = &uctypes_struct_type;
o->desc = t->items[2];
o->addr = self->addr + size * index;
o->flags = self->flags;
return o;
}
} else if (agg_type == PTR) {
byte *p = *(void**)self->addr;
if (MP_OBJ_IS_SMALL_INT(t->items[1])) {
uint val_type = GET_TYPE(MP_OBJ_SMALL_INT_VALUE(t->items[1]), VAL_TYPE_BITS);
return get_aligned(val_type, p, index);
} else {
mp_uint_t dummy = 0;
mp_uint_t size = uctypes_struct_size(t->items[1], &dummy);
mp_obj_uctypes_struct_t *o = m_new_obj(mp_obj_uctypes_struct_t);
o->base.type = &uctypes_struct_type;
o->desc = t->items[1];
o->addr = p + size * index;
o->flags = self->flags;
return o;
}
}
assert(0);
} else {
// store
return MP_OBJ_NULL; // op not supported
}
}
/// \function addressof()
/// Return address of object's data (applies to object providing buffer
/// interface).
mp_obj_t uctypes_struct_addressof(mp_obj_t buf) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
return mp_obj_new_int((mp_int_t)bufinfo.buf);
}
MP_DEFINE_CONST_FUN_OBJ_1(uctypes_struct_addressof_obj, uctypes_struct_addressof);
/// \function bytearray_at()
/// Capture memory at given address of given size as bytearray. Memory is
/// captured by reference (and thus memory pointed by bytearray may change
/// or become invalid at later time). Use bytes_at() to capture by value.
mp_obj_t uctypes_struct_bytearray_at(mp_obj_t ptr, mp_obj_t size) {
return mp_obj_new_bytearray_by_ref(mp_obj_int_get(size), (void*)mp_obj_int_get(ptr));
}
MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytearray_at_obj, uctypes_struct_bytearray_at);
/// \function bytes_at()
/// Capture memory at given address of given size as bytes. Memory is
/// captured by value, i.e. copied. Use bytearray_at() to capture by reference
/// ("zero copy").
mp_obj_t uctypes_struct_bytes_at(mp_obj_t ptr, mp_obj_t size) {
return mp_obj_new_bytes((void*)mp_obj_int_get(ptr), mp_obj_int_get(size));
}
MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytes_at_obj, uctypes_struct_bytes_at);
STATIC const mp_obj_type_t uctypes_struct_type = {
{ &mp_type_type },
.name = MP_QSTR_struct,
.print = uctypes_struct_print,
.make_new = uctypes_struct_make_new,
.load_attr = uctypes_struct_load_attr,
.store_attr = uctypes_struct_store_attr,
.subscr = uctypes_struct_subscr,
};
STATIC const mp_map_elem_t mp_module_uctypes_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uctypes) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_struct), (mp_obj_t)&uctypes_struct_type },
{ MP_OBJ_NEW_QSTR(MP_QSTR_sizeof), (mp_obj_t)&uctypes_struct_sizeof_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_addressof), (mp_obj_t)&uctypes_struct_addressof_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_bytes_at), (mp_obj_t)&uctypes_struct_bytes_at_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_bytearray_at), (mp_obj_t)&uctypes_struct_bytearray_at_obj },
/// \moduleref uctypes
/// \constant NATIVE - Native structure layout - native endianness,
/// platform-specific field alignment
{ MP_OBJ_NEW_QSTR(MP_QSTR_NATIVE), MP_OBJ_NEW_SMALL_INT(LAYOUT_NATIVE) },
/// \constant LITTLE_ENDIAN - Little-endian structure layout, tightly packed
/// (no alignment constraints)
{ MP_OBJ_NEW_QSTR(MP_QSTR_LITTLE_ENDIAN), MP_OBJ_NEW_SMALL_INT(LAYOUT_LITTLE_ENDIAN) },
/// \constant BIG_ENDIAN - Big-endian structure layout, tightly packed
/// (no alignment constraints)
{ MP_OBJ_NEW_QSTR(MP_QSTR_BIG_ENDIAN), MP_OBJ_NEW_SMALL_INT(LAYOUT_BIG_ENDIAN) },
/// \constant VOID - void value type, may be used only as pointer target type.
{ MP_OBJ_NEW_QSTR(MP_QSTR_VOID), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(UINT8, VAL_TYPE_BITS)) },
/// \constant UINT8 - uint8_t value type
{ MP_OBJ_NEW_QSTR(MP_QSTR_UINT8), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(UINT8, 4)) },
/// \constant INT8 - int8_t value type
{ MP_OBJ_NEW_QSTR(MP_QSTR_INT8), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(INT8, 4)) },
/// \constant UINT16 - uint16_t value type
{ MP_OBJ_NEW_QSTR(MP_QSTR_UINT16), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(UINT16, 4)) },
/// \constant INT16 - int16_t value type
{ MP_OBJ_NEW_QSTR(MP_QSTR_INT16), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(INT16, 4)) },
/// \constant UINT32 - uint32_t value type
{ MP_OBJ_NEW_QSTR(MP_QSTR_UINT32), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(UINT32, 4)) },
/// \constant INT32 - int32_t value type
{ MP_OBJ_NEW_QSTR(MP_QSTR_INT32), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(INT32, 4)) },
/// \constant UINT64 - uint64_t value type
{ MP_OBJ_NEW_QSTR(MP_QSTR_UINT64), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(UINT64, 4)) },
/// \constant INT64 - int64_t value type
{ MP_OBJ_NEW_QSTR(MP_QSTR_INT64), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(INT64, 4)) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_BFUINT8), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(BFUINT8, 4)) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_BFINT8), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(BFINT8, 4)) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_BFUINT16), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(BFUINT16, 4)) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_BFINT16), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(BFINT16, 4)) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_BFUINT32), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(BFUINT32, 4)) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_BFINT32), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(BFINT32, 4)) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_BF_POS), MP_OBJ_NEW_SMALL_INT(17) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_BF_LEN), MP_OBJ_NEW_SMALL_INT(22) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PTR), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(PTR, AGG_TYPE_BITS)) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ARRAY), MP_OBJ_NEW_SMALL_INT(TYPE2SMALLINT(ARRAY, AGG_TYPE_BITS)) },
};
STATIC const mp_obj_dict_t mp_module_uctypes_globals = {
.base = {&mp_type_dict},
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = MP_ARRAY_SIZE(mp_module_uctypes_globals_table),
.alloc = MP_ARRAY_SIZE(mp_module_uctypes_globals_table),
.table = (mp_map_elem_t*)mp_module_uctypes_globals_table,
},
};
const mp_obj_module_t mp_module_uctypes = {
.base = { &mp_type_module },
.name = MP_QSTR_uctypes,
.globals = (mp_obj_dict_t*)&mp_module_uctypes_globals,
};
#endif

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <assert.h>
@@ -77,3 +103,16 @@ void mp_arg_parse_all(uint n_pos, const mp_obj_t *pos, mp_map_t *kws, uint n_all
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "extra keyword arguments given"));
}
}
void mp_arg_parse_all_kw_array(uint n_pos, uint n_kw, const mp_obj_t *args, uint n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals) {
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, args + n_pos);
mp_arg_parse_all(n_pos, args, &kw_args, n_allowed, allowed, out_vals);
}
#if MICROPY_CPYTHON_COMPAT
NORETURN void mp_arg_error_unimpl_kw(void) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
"keyword argument(s) not yet implemented - use normal args instead"));
}
#endif

View File

@@ -1,9 +1,35 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "asmthumb.h"
// wrapper around everything in this file
@@ -16,11 +42,11 @@
#define SIGNED_FIT12(x) (((x) & 0xfffff800) == 0) || (((x) & 0xfffff800) == 0xfffff800)
struct _asm_thumb_t {
int pass;
uint pass;
uint code_offset;
uint code_size;
byte *code_base;
byte dummy_data[8];
byte dummy_data[4];
uint max_num_labels;
int *label_offsets;
@@ -32,14 +58,9 @@ struct _asm_thumb_t {
asm_thumb_t *asm_thumb_new(uint max_num_labels) {
asm_thumb_t *as;
as = m_new(asm_thumb_t, 1);
as->pass = 0;
as->code_offset = 0;
as->code_size = 0;
as->code_base = NULL;
as = m_new0(asm_thumb_t, 1);
as->max_num_labels = max_num_labels;
as->label_offsets = m_new(int, max_num_labels);
as->num_locals = 0;
return as;
}
@@ -63,16 +84,16 @@ void asm_thumb_free(asm_thumb_t *as, bool free_code) {
m_del_obj(asm_thumb_t, as);
}
void asm_thumb_start_pass(asm_thumb_t *as, int pass) {
void asm_thumb_start_pass(asm_thumb_t *as, uint pass) {
as->pass = pass;
as->code_offset = 0;
if (pass == ASM_THUMB_PASS_2) {
if (pass == ASM_THUMB_PASS_COMPUTE) {
memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
}
}
void asm_thumb_end_pass(asm_thumb_t *as) {
if (as->pass == ASM_THUMB_PASS_2) {
if (as->pass == ASM_THUMB_PASS_COMPUTE) {
// calculate size of code in bytes
as->code_size = as->code_offset;
as->code_base = m_new(byte, as->code_size);
@@ -92,9 +113,10 @@ void asm_thumb_end_pass(asm_thumb_t *as) {
}
// all functions must go through this one to emit bytes
// if as->pass < ASM_THUMB_PASS_EMIT, then this function only returns a buffer of 4 bytes length
STATIC byte *asm_thumb_get_cur_to_write_bytes(asm_thumb_t *as, int num_bytes_to_write) {
//printf("emit %d\n", num_bytes_to_write);
if (as->pass < ASM_THUMB_PASS_3) {
if (as->pass < ASM_THUMB_PASS_EMIT) {
as->code_offset += num_bytes_to_write;
return as->dummy_data;
} else {
@@ -111,7 +133,7 @@ uint asm_thumb_get_code_size(asm_thumb_t *as) {
void *asm_thumb_get_code(asm_thumb_t *as) {
// need to set low bit to indicate that it's thumb code
return (void *)(((machine_uint_t)as->code_base) | 1);
return (void *)(((mp_uint_t)as->code_base) | 1);
}
/*
@@ -145,15 +167,29 @@ STATIC void asm_thumb_write_word32(asm_thumb_t *as, int w32) {
#define OP_ADD_SP(num_words) (0xb000 | (num_words))
#define OP_SUB_SP(num_words) (0xb080 | (num_words))
// locals:
// - stored on the stack in ascending order
// - numbered 0 through as->num_locals-1
// - SP points to first local
//
// | SP
// v
// l0 l1 l2 ... l(n-1)
// ^ ^
// | low address | high address in RAM
void asm_thumb_entry(asm_thumb_t *as, int num_locals) {
// work out what to push and how many extra space to reserve on stack
// work out what to push and how many extra spaces to reserve on stack
// so that we have enough for all locals and it's aligned an 8-byte boundary
// we push extra regs (r1, r2, r3) to help do the stack adjustment
// we probably should just always subtract from sp, since this would be more efficient
// for push rlist, lowest numbered register at the lowest address
uint reglist;
uint stack_adjust;
if (num_locals < 0) {
num_locals = 0;
}
// don't ppop r0 because it's used for return value
// don't pop r0 because it's used for return value
switch (num_locals) {
case 0:
reglist = 0xf2;
@@ -198,12 +234,12 @@ void asm_thumb_exit(asm_thumb_t *as) {
void asm_thumb_label_assign(asm_thumb_t *as, uint label) {
assert(label < as->max_num_labels);
if (as->pass == ASM_THUMB_PASS_2) {
if (as->pass < ASM_THUMB_PASS_EMIT) {
// assign label offset
assert(as->label_offsets[label] == -1);
as->label_offsets[label] = as->code_offset;
} else if (as->pass == ASM_THUMB_PASS_3) {
// ensure label offset has not changed from PASS_2 to PASS_3
} else {
// ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
//printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
assert(as->label_offsets[label] == as->code_offset);
}
@@ -216,10 +252,13 @@ void asm_thumb_align(asm_thumb_t* as, uint align) {
void asm_thumb_data(asm_thumb_t* as, uint bytesize, uint val) {
byte *c = asm_thumb_get_cur_to_write_bytes(as, bytesize);
// little endian
for (uint i = 0; i < bytesize; i++) {
*c++ = val;
val >>= 8;
// only write to the buffer in the emit pass (otherwise we overflow dummy_data)
if (as->pass == ASM_THUMB_PASS_EMIT) {
// little endian
for (uint i = 0; i < bytesize; i++) {
*c++ = val;
val >>= 8;
}
}
}
@@ -339,7 +378,7 @@ void asm_thumb_bcc_n(asm_thumb_t *as, int cond, uint label) {
}
}
void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, machine_uint_t i32) {
void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32) {
// movw, movt does it in 8 bytes
// ldr [pc, #], dw does it in 6 bytes, but we might not reach to end of code for dw
@@ -357,20 +396,35 @@ void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) {
}
}
// i32 is stored as a full word in the code, and aligned to machine-word boundary
// TODO this is very inefficient, improve it!
void asm_thumb_mov_reg_i32_aligned(asm_thumb_t *as, uint reg_dest, int i32) {
// align on machine-word + 2
if ((as->code_offset & 3) == 0) {
asm_thumb_op16(as, ASM_THUMB_OP_NOP);
}
// jump over the i32 value (instruction prefect adds 4 to PC)
asm_thumb_op16(as, OP_B_N(0));
// store i32 on machine-word aligned boundary
asm_thumb_data(as, 4, i32);
// do the actual load of the i32 value
asm_thumb_mov_reg_i32_optimised(as, reg_dest, i32);
}
#define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
#define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) {
assert(rlo_src < REG_R8);
int word_offset = as->num_locals - local_num - 1;
assert(as->pass < ASM_THUMB_PASS_3 || word_offset >= 0);
int word_offset = local_num;
assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
asm_thumb_op16(as, OP_STR_TO_SP_OFFSET(rlo_src, word_offset));
}
void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
assert(rlo_dest < REG_R8);
int word_offset = as->num_locals - local_num - 1;
assert(as->pass < ASM_THUMB_PASS_3 || word_offset >= 0);
int word_offset = local_num;
assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
asm_thumb_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset));
}
@@ -378,8 +432,8 @@ void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num) {
assert(rlo_dest < REG_R8);
int word_offset = as->num_locals - local_num - 1;
assert(as->pass < ASM_THUMB_PASS_3 || word_offset >= 0);
int word_offset = local_num;
assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
asm_thumb_op16(as, OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset));
}
@@ -445,7 +499,7 @@ void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp
if (0) {
// load ptr to function into register using immediate, then branch
// not relocatable
asm_thumb_mov_reg_i32(as, reg_temp, (machine_uint_t)fun_ptr);
asm_thumb_mov_reg_i32(as, reg_temp, (mp_uint_t)fun_ptr);
asm_thumb_op16(as, OP_BLX(reg_temp));
} else if (1) {
asm_thumb_op16(as, OP_FORMAT_9_10(ASM_THUMB_FORMAT_9_LDR | ASM_THUMB_FORMAT_9_WORD_TRANSFER, reg_temp, REG_R7, fun_id));

View File

@@ -1,6 +1,31 @@
#define ASM_THUMB_PASS_1 (1)
#define ASM_THUMB_PASS_2 (2)
#define ASM_THUMB_PASS_3 (3)
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#define ASM_THUMB_PASS_COMPUTE (1)
#define ASM_THUMB_PASS_EMIT (2)
#define REG_R0 (0)
#define REG_R1 (1)
@@ -45,7 +70,7 @@ typedef struct _asm_thumb_t asm_thumb_t;
asm_thumb_t *asm_thumb_new(uint max_num_labels);
void asm_thumb_free(asm_thumb_t *as, bool free_code);
void asm_thumb_start_pass(asm_thumb_t *as, int pass);
void asm_thumb_start_pass(asm_thumb_t *as, uint pass);
void asm_thumb_end_pass(asm_thumb_t *as);
uint asm_thumb_get_code_size(asm_thumb_t *as);
void *asm_thumb_get_code(asm_thumb_t *as);
@@ -160,8 +185,9 @@ void asm_thumb_ite_ge(asm_thumb_t *as);
void asm_thumb_b_n(asm_thumb_t *as, uint label);
void asm_thumb_bcc_n(asm_thumb_t *as, int cond, uint label);
void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, machine_uint_t i32_src); // convenience
void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32_src); // convenience
void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32_src); // convenience
void asm_thumb_mov_reg_i32_aligned(asm_thumb_t *as, uint reg_dest, int i32); // convenience
void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num_dest, uint rlo_src); // convenience
void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num); // convenience
void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num); // convenience

View File

@@ -1,10 +1,36 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
// wrapper around everything in this file
#if MICROPY_EMIT_X64
@@ -86,7 +112,7 @@
#define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
struct _asm_x64_t {
int pass;
uint pass;
uint code_offset;
uint code_size;
byte *code_base;
@@ -112,14 +138,9 @@ void *alloc_mem(uint req_size, uint *alloc_size, bool is_exec) {
asm_x64_t *asm_x64_new(uint max_num_labels) {
asm_x64_t *as;
as = m_new(asm_x64_t, 1);
as->pass = 0;
as->code_offset = 0;
as->code_size = 0;
as->code_base = NULL;
as = m_new0(asm_x64_t, 1);
as->max_num_labels = max_num_labels;
as->label_offsets = m_new(int, max_num_labels);
as->num_locals = 0;
return as;
}
@@ -144,17 +165,17 @@ void asm_x64_free(asm_x64_t *as, bool free_code) {
m_del_obj(asm_x64_t, as);
}
void asm_x64_start_pass(asm_x64_t *as, int pass) {
void asm_x64_start_pass(asm_x64_t *as, uint pass) {
as->pass = pass;
as->code_offset = 0;
if (pass == ASM_X64_PASS_2) {
if (pass == ASM_X64_PASS_COMPUTE) {
// reset all labels
memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
}
}
void asm_x64_end_pass(asm_x64_t *as) {
if (as->pass == ASM_X64_PASS_2) {
if (as->pass == ASM_X64_PASS_COMPUTE) {
// calculate size of code in bytes
as->code_size = as->code_offset;
//as->code_base = m_new(byte, as->code_size); need to allocale executable memory
@@ -178,7 +199,7 @@ void asm_x64_end_pass(asm_x64_t *as) {
// all functions must go through this one to emit bytes
STATIC byte *asm_x64_get_cur_to_write_bytes(asm_x64_t *as, int num_bytes_to_write) {
//printf("emit %d\n", num_bytes_to_write);
if (as->pass < ASM_X64_PASS_3) {
if (as->pass < ASM_X64_PASS_EMIT) {
as->code_offset += num_bytes_to_write;
return as->dummy_data;
} else {
@@ -341,6 +362,15 @@ void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r
}
}
// src_i64 is stored as a full word in the code, and aligned to machine-word boundary
void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64) {
// mov instruction uses 2 bytes for the instruction, before the i64
while (((as->code_offset + 2) & (WORD_SIZE - 1)) != 0) {
asm_x64_nop(as);
}
asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
}
void asm_x64_mov_i32_to_disp(asm_x64_t *as, int src_i32, int dest_r32, int dest_disp)
{
assert(0);
@@ -461,12 +491,12 @@ void asm_x64_setcc_r8(asm_x64_t *as, int jcc_type, int dest_r8) {
void asm_x64_label_assign(asm_x64_t *as, int label) {
assert(label < as->max_num_labels);
if (as->pass == ASM_X64_PASS_2) {
if (as->pass < ASM_X64_PASS_EMIT) {
// assign label offset
assert(as->label_offsets[label] == -1);
as->label_offsets[label] = as->code_offset;
} else if (as->pass == ASM_X64_PASS_3) {
// ensure label offset has not changed from PASS_2 to PASS_3
} else {
// ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
//printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
assert(as->label_offsets[label] == as->code_offset);
}

View File

@@ -1,6 +1,31 @@
#define ASM_X64_PASS_1 (1)
#define ASM_X64_PASS_2 (2)
#define ASM_X64_PASS_3 (3)
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#define ASM_X64_PASS_COMPUTE (1)
#define ASM_X64_PASS_EMIT (2)
#define REG_RAX (0)
#define REG_RCX (1)
@@ -28,7 +53,7 @@ typedef struct _asm_x64_t asm_x64_t;
asm_x64_t* asm_x64_new(uint max_num_labels);
void asm_x64_free(asm_x64_t* as, bool free_code);
void asm_x64_start_pass(asm_x64_t *as, int pass);
void asm_x64_start_pass(asm_x64_t *as, uint pass);
void asm_x64_end_pass(asm_x64_t *as);
uint asm_x64_get_code_size(asm_x64_t* as);
void* asm_x64_get_code(asm_x64_t* as);
@@ -45,6 +70,7 @@ void asm_x64_mov_i32_to_r64(asm_x64_t* as, int src_i32, int dest_r64);
void asm_x64_mov_i64_to_r64(asm_x64_t* as, int64_t src_i64, int dest_r64);
void asm_x64_mov_i32_to_disp(asm_x64_t* as, int src_i32, int dest_r32, int dest_disp);
void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64);
void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64);
void asm_x64_xor_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64);
void asm_x64_add_r64_to_r64(asm_x64_t* as, int src_r64, int dest_r64);
void asm_x64_add_i32_to_r32(asm_x64_t* as, int src_i32, int dest_r32);

53
py/bc.h
View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// Exception stack entry
typedef struct _mp_exc_stack {
const byte *handler;
@@ -10,12 +36,25 @@ typedef struct _mp_exc_stack {
byte opcode;
} mp_exc_stack_t;
mp_vm_return_kind_t mp_execute_byte_code(const byte *code, const mp_obj_t *args, uint n_args, const mp_obj_t *args2, uint n_args2, mp_obj_t *ret);
mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **sp_in_out, mp_exc_stack_t *exc_stack, mp_exc_stack_t **exc_sp_in_out, volatile mp_obj_t inject_exc);
void mp_byte_code_print(const byte *code, int len);
void mp_byte_code_print2(const byte *code, int len);
typedef struct _mp_code_state {
const byte *code_info;
const byte *ip;
mp_obj_t *sp;
// bit 0 is saved currently_in_except_block value
mp_exc_stack_t *exc_sp;
uint n_state;
// Variable-length
mp_obj_t state[0];
// Variable-length, never accessed by name, only as (void*)(state + n_state)
//mp_exc_stack_t exc_state[0];
} mp_code_state;
mp_vm_return_kind_t mp_execute_bytecode(mp_code_state *code_state, volatile mp_obj_t inject_exc);
void mp_setup_code_state(mp_code_state *code_state, mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args);
void mp_bytecode_print(const void *descr, const byte *code, int len);
void mp_bytecode_print2(const byte *code, int len);
// Helper macros to access pointer with least significant bit holding a flag
#define MP_TAGPTR_PTR(x) ((void*)((machine_uint_t)(x) & ~((machine_uint_t)1)))
#define MP_TAGPTR_TAG(x) ((machine_uint_t)(x) & 1)
#define MP_TAGPTR_MAKE(ptr, tag) ((void*)((machine_uint_t)(ptr) | tag))
#define MP_TAGPTR_PTR(x) ((void*)((mp_uint_t)(x) & ~((mp_uint_t)1)))
#define MP_TAGPTR_TAG(x) ((mp_uint_t)(x) & 1)
#define MP_TAGPTR_MAKE(ptr, tag) ((void*)((mp_uint_t)(ptr) | tag))

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// Micro Python byte-codes.
// The comment at the end of the line (if it exists) tells the arguments to the byte-code.

View File

@@ -1,9 +1,36 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "binary.h"
@@ -49,7 +76,7 @@ int mp_binary_get_size(char struct_type, char val_type, uint *palign) {
case 'q': case 'Q':
// TODO: This is for x86
align = sizeof(int); size = sizeof(long long); break;
case 'P': case 'O':
case 'P': case 'O': case 'S':
align = size = sizeof(void*); break;
}
}
@@ -61,7 +88,7 @@ int mp_binary_get_size(char struct_type, char val_type, uint *palign) {
}
mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
machine_int_t val = 0;
mp_int_t val = 0;
switch (typecode) {
case 'b':
val = ((int8_t*)p)[index];
@@ -88,7 +115,7 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
// TODO: Explode API more to cover signedness
return mp_obj_new_int_from_ll(((long long*)p)[index]);
#endif
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
case 'f':
return mp_obj_new_float(((float*)p)[index]);
case 'd':
@@ -98,6 +125,28 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
return MP_OBJ_NEW_SMALL_INT(val);
}
mp_int_t mp_binary_get_int(uint size, bool is_signed, bool big_endian, byte *p) {
int delta;
if (!big_endian) {
delta = -1;
p += size - 1;
} else {
delta = 1;
}
mp_int_t val = 0;
if (is_signed && *p & 0x80) {
val = -1;
}
for (uint i = 0; i < size; i++) {
val <<= 8;
val |= *p;
p += delta;
}
return val;
}
#define is_signed(typecode) (typecode > 'Z')
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
byte *p = *ptr;
@@ -106,35 +155,21 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
int size = mp_binary_get_size(struct_type, val_type, &align);
if (struct_type == '@') {
// Make pointer aligned
p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1));
#if MP_ENDIANNESS_LITTLE
struct_type = '<';
#else
struct_type = '>';
#endif
}
*ptr = p + size;
int delta;
if (struct_type == '<') {
delta = -1;
p += size - 1;
} else {
delta = 1;
}
mp_int_t val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
machine_int_t val = 0;
if (is_signed(val_type) && *p & 0x80) {
val = -1;
}
for (uint i = 0; i < size; i++) {
val <<= 8;
val |= *p;
p += delta;
}
*ptr += size;
if (val_type == 'O') {
return (mp_obj_t)val;
} else if (val_type == 'S') {
return mp_obj_new_str((char*)val, strlen((char*)val), false);
} else if (is_signed(val_type)) {
return mp_obj_new_int(val);
} else {
@@ -142,6 +177,23 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
}
}
void mp_binary_set_int(uint val_sz, bool big_endian, byte *p, byte *val_ptr) {
int in_delta, out_delta;
if (big_endian) {
in_delta = -1;
out_delta = 1;
val_ptr += val_sz - 1;
} else {
in_delta = out_delta = 1;
}
for (uint i = val_sz; i > 0; i--) {
*p = *val_ptr;
p += out_delta;
val_ptr += in_delta;
}
}
void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr) {
byte *p = *ptr;
uint align;
@@ -149,18 +201,19 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
int size = mp_binary_get_size(struct_type, val_type, &align);
if (struct_type == '@') {
// Make pointer aligned
p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1));
#if MP_ENDIANNESS_LITTLE
struct_type = '<';
#else
struct_type = '>';
#endif
}
*ptr = p + size;
#if MP_ENDIANNESS_BIG
#error Not implemented
#endif
machine_int_t val;
mp_int_t val;
byte *in = (byte*)&val;
switch (val_type) {
case 'O':
@@ -170,28 +223,12 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
val = mp_obj_get_int(val_in);
}
int in_delta, out_delta;
uint val_sz = MIN(size, sizeof(val));
if (struct_type == '>') {
in_delta = -1;
out_delta = 1;
in += val_sz - 1;
} else {
in_delta = out_delta = 1;
}
for (uint i = val_sz; i > 0; i--) {
*p = *in;
p += out_delta;
in += in_delta;
}
*ptr += size;
mp_binary_set_int(MIN(size, sizeof(val)), struct_type == '>', p, in);
}
void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in) {
switch (typecode) {
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
case 'f':
((float*)p)[index] = mp_obj_float_get(val_in);
break;
@@ -204,7 +241,7 @@ void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in)
}
}
void mp_binary_set_val_array_from_int(char typecode, void *p, int index, machine_int_t val) {
void mp_binary_set_val_array_from_int(char typecode, void *p, int index, mp_int_t val) {
switch (typecode) {
case 'b':
((int8_t*)p)[index] = val;
@@ -234,7 +271,7 @@ void mp_binary_set_val_array_from_int(char typecode, void *p, int index, machine
((long long*)p)[index] = val;
break;
#endif
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
case 'f':
((float*)p)[index] = val;
break;

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// Use special typecode to differentiate repr() of bytearray vs array.array('B')
// (underlyingly they're same).
#define BYTEARRAY_TYPECODE 0
@@ -5,6 +31,8 @@
int mp_binary_get_size(char struct_type, char val_type, uint *palign);
mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index);
void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in);
void mp_binary_set_val_array_from_int(char typecode, void *p, int index, machine_int_t val);
void mp_binary_set_val_array_from_int(char typecode, void *p, int index, mp_int_t val);
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr);
void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr);
mp_int_t mp_binary_get_int(uint size, bool is_signed, bool big_endian, byte *p);
void mp_binary_set_int(uint val_sz, bool big_endian, byte *p, byte *val_ptr);

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <assert.h>
@@ -11,7 +37,7 @@
#include "runtime.h"
#include "builtin.h"
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
#include <math.h>
#endif
@@ -73,12 +99,12 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin___repl_print___obj, mp_builtin___repl_print
mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
if (MP_OBJ_IS_SMALL_INT(o_in)) {
mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in);
mp_int_t val = MP_OBJ_SMALL_INT_VALUE(o_in);
if (val < 0) {
val = -val;
}
return MP_OBJ_NEW_SMALL_INT(val);
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_float)) {
mp_float_t value = mp_obj_float_get(o_in);
// TODO check for NaN etc
@@ -87,10 +113,12 @@ mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
} else {
return o_in;
}
#if MICROPY_PY_BUILTINS_COMPLEX
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_complex)) {
mp_float_t real, imag;
mp_obj_complex_get(o_in, &real, &imag);
return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag));
#endif
#endif
} else {
assert(0);
@@ -128,7 +156,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_any_obj, mp_builtin_any);
STATIC mp_obj_t mp_builtin_bin(mp_obj_t o_in) {
mp_obj_t args[] = { MP_OBJ_NEW_QSTR(MP_QSTR__brace_open__colon__hash_b_brace_close_), o_in };
return mp_obj_str_format(ARRAY_SIZE(args), args);
return mp_obj_str_format(MP_ARRAY_SIZE(args), args);
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bin_obj, mp_builtin_bin);
@@ -144,13 +172,40 @@ STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
int ord = mp_obj_get_int(o_in);
#if MICROPY_PY_BUILTINS_STR_UNICODE
mp_int_t c = mp_obj_get_int(o_in);
char str[4];
int len = 0;
if (c < 0x80) {
*str = c; len = 1;
} else if (c < 0x800) {
str[0] = (c >> 6) | 0xC0;
str[1] = (c & 0x3F) | 0x80;
len = 2;
} else if (c < 0x10000) {
str[0] = (c >> 12) | 0xE0;
str[1] = ((c >> 6) & 0x3F) | 0x80;
str[2] = (c & 0x3F) | 0x80;
len = 3;
} else if (c < 0x110000) {
str[0] = (c >> 18) | 0xF0;
str[1] = ((c >> 12) & 0x3F) | 0x80;
str[2] = ((c >> 6) & 0x3F) | 0x80;
str[3] = (c & 0x3F) | 0x80;
len = 4;
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
}
return mp_obj_new_str(str, len, true);
#else
mp_int_t ord = mp_obj_get_int(o_in);
if (0 <= ord && ord <= 0x10ffff) {
byte str[1] = {ord};
char str[1] = {ord};
return mp_obj_new_str(str, 1, true);
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "chr() arg not in range(0x110000)"));
}
#endif
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_chr_obj, mp_builtin_chr);
@@ -195,8 +250,8 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_dir_obj, 0, 1, mp_builtin_dir);
STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
if (MP_OBJ_IS_SMALL_INT(o1_in) && MP_OBJ_IS_SMALL_INT(o2_in)) {
mp_small_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
mp_small_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
mp_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
mp_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
mp_obj_t args[2];
args[0] = MP_OBJ_NEW_SMALL_INT(i1 / i2);
args[1] = MP_OBJ_NEW_SMALL_INT(i1 % i2);
@@ -316,13 +371,32 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct);
STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
uint len;
const char *str = mp_obj_str_get_data(o_in, &len);
#if MICROPY_PY_BUILTINS_STR_UNICODE
mp_uint_t charlen = unichar_charlen(str, len);
if (charlen == 1) {
if (MP_OBJ_IS_STR(o_in) && UTF8_IS_NONASCII(*str)) {
mp_int_t ord = *str++ & 0x7F;
for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) {
ord &= ~mask;
}
while (UTF8_IS_CONT(*str)) {
ord = (ord << 6) | (*str++ & 0x3F);
}
return mp_obj_new_int(ord);
} else {
return mp_obj_new_int(((const byte*)str)[0]);
}
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "ord() expected a character, but string of length %d found", charlen));
}
#else
if (len == 1) {
// don't sign extend when converting to ord
// TODO unicode
return mp_obj_new_int(((const byte*)str)[0]);
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "ord() expected a character, but string of length %d found", len));
}
#endif
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
@@ -365,7 +439,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_print_obj, 0, mp_builtin_print);
STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
vstr_t *vstr = vstr_new();
mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, vstr, o_in, PRINT_REPR);
mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
mp_obj_t s = mp_obj_new_str(vstr->buf, vstr->len, false);
vstr_free(vstr);
return s;
}
@@ -404,7 +478,7 @@ STATIC mp_obj_t mp_builtin_sorted(uint n_args, const mp_obj_t *args, mp_map_t *k
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
STATIC mp_obj_t mp_builtin_id(mp_obj_t o_in) {
return mp_obj_new_int((machine_int_t)o_in);
return mp_obj_new_int((mp_int_t)o_in);
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_id_obj, mp_builtin_id);
@@ -426,16 +500,36 @@ STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t d
}
STATIC mp_obj_t mp_builtin_getattr(uint n_args, const mp_obj_t *args) {
assert(MP_OBJ_IS_QSTR(args[1]));
mp_obj_t attr = args[1];
if (MP_OBJ_IS_TYPE(attr, &mp_type_str)) {
attr = mp_obj_str_intern(attr);
} else if (!MP_OBJ_IS_QSTR(attr)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "string required"));
}
mp_obj_t defval = MP_OBJ_NULL;
if (n_args > 2) {
defval = args[2];
}
return mp_load_attr_default(args[0], MP_OBJ_QSTR_VALUE(args[1]), defval);
return mp_load_attr_default(args[0], MP_OBJ_QSTR_VALUE(attr), defval);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_getattr_obj, 2, 3, mp_builtin_getattr);
STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) {
assert(MP_OBJ_IS_QSTR(attr_in));
mp_obj_t dest[2];
// TODO: https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr
// explicitly says "This is implemented by calling getattr(object, name) and seeing
// whether it raises an AttributeError or not.", so we should explicitly wrap this
// in nlr_push and handle exception.
mp_load_method_maybe(object_in, MP_OBJ_QSTR_VALUE(attr_in), dest);
return MP_BOOL(dest[0] != MP_OBJ_NULL);
}
MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_hasattr_obj, mp_builtin_hasattr);
// These two are defined in terms of MicroPython API functions right away
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_globals_obj, mp_globals_get);
MP_DEFINE_CONST_FUN_OBJ_0(mp_builtin_locals_obj, mp_locals_get);

View File

@@ -1,4 +1,31 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args);
mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin___build_class___obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin___import___obj);
@@ -15,6 +42,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_eval_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_exec_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_getattr_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_globals_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hasattr_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hex_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_id_obj);
@@ -51,3 +79,7 @@ extern const mp_obj_module_t mp_module_cmath;
extern const mp_obj_module_t mp_module_micropython;
extern const mp_obj_module_t mp_module_struct;
extern const mp_obj_module_t mp_module_sys;
extern const mp_obj_module_t mp_module_gc;
// extmod modules
extern const mp_obj_module_t mp_module_uctypes;

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include "mpconfig.h"

View File

@@ -1,8 +1,34 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2014 Paul Sokolovsky
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <alloca.h>
#include "mpconfig.h"
#include "nlr.h"
@@ -28,7 +54,7 @@
#define PATH_SEP_CHAR '/'
mp_import_stat_t stat_dir_or_file(vstr_t *path) {
STATIC mp_import_stat_t stat_dir_or_file(vstr_t *path) {
//printf("stat %s\n", vstr_str(path));
mp_import_stat_t stat = mp_import_stat(vstr_str(path));
if (stat == MP_IMPORT_STAT_DIR) {
@@ -42,11 +68,11 @@ mp_import_stat_t stat_dir_or_file(vstr_t *path) {
return MP_IMPORT_STAT_NO_EXIST;
}
mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) {
STATIC mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) {
// extract the list of paths
uint path_num = 0;
mp_obj_t *path_items;
#if MICROPY_ENABLE_MOD_SYS
#if MICROPY_PY_SYS
mp_obj_list_get(mp_sys_path, &path_num, &path_items);
#endif
@@ -76,7 +102,7 @@ mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *dest) {
}
}
void do_load(mp_obj_t module_obj, vstr_t *file) {
STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
// create the lexer
mp_lexer_t *lex = mp_lexer_new_from_file(vstr_str(file));
@@ -136,16 +162,6 @@ void do_load(mp_obj_t module_obj, vstr_t *file) {
mp_globals_set(old_globals);
}
// TODO: Move to objdict?
STATIC inline mp_obj_t mp_obj_dict_get(mp_obj_t dict_in, mp_obj_t key) {
mp_obj_dict_t *dict = dict_in;
mp_map_elem_t *elem = mp_map_lookup(&dict->map, key, MP_MAP_LOOKUP);
if (elem == NULL) {
return elem;
}
return elem->value;
}
mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
#if DEBUG_PRINT
printf("__import__:\n");
@@ -251,7 +267,7 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
DEBUG_printf("Module not yet loaded\n");
uint last = 0;
VSTR_FIXED(path, MICROPY_PATH_MAX)
VSTR_FIXED(path, MICROPY_ALLOC_PATH_MAX)
module_obj = MP_OBJ_NULL;
mp_obj_t top_module_obj = MP_OBJ_NULL;
mp_obj_t outer_module_obj = MP_OBJ_NULL;
@@ -261,6 +277,7 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
// create a qstr for the module name up to this depth
qstr mod_name = qstr_from_strn(mod_str, i);
DEBUG_printf("Processing module: %s\n", qstr_str(mod_name));
DEBUG_printf("Previous path: %s\n", vstr_str(&path));
// find the file corresponding to the module name
mp_import_stat_t stat;
@@ -273,6 +290,7 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
vstr_add_strn(&path, mod_str + last, i - last);
stat = stat_dir_or_file(&path);
}
DEBUG_printf("Current path: %s\n", vstr_str(&path));
// fail if we couldn't find the file
if (stat == MP_IMPORT_STAT_NO_EXIST) {
@@ -287,7 +305,9 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
if (stat == MP_IMPORT_STAT_DIR) {
DEBUG_printf("%s is dir\n", vstr_str(&path));
mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str((byte*)vstr_str(&path), vstr_len(&path), false));
// https://docs.python.org/3/reference/import.html
// "Specifically, any module that contains a __path__ attribute is considered a package."
mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str(vstr_str(&path), vstr_len(&path), false));
vstr_add_char(&path, PATH_SEP_CHAR);
vstr_add_str(&path, "__init__.py");
if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) {
@@ -295,10 +315,8 @@ mp_obj_t mp_builtin___import__(uint n_args, mp_obj_t *args) {
printf("Notice: %s is imported as namespace package\n", vstr_str(&path));
} else {
do_load(module_obj, &path);
vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
}
vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
// https://docs.python.org/3.3/reference/import.html
// "Specifically, any module that contains a __path__ attribute is considered a package."
} else { // MP_IMPORT_STAT_FILE
do_load(module_obj, &path);
// TODO: We cannot just break here, at the very least, we must execute

View File

@@ -1,7 +1,33 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "builtin.h"
@@ -17,25 +43,32 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
// built-in types
{ MP_OBJ_NEW_QSTR(MP_QSTR_bool), (mp_obj_t)&mp_type_bool },
{ MP_OBJ_NEW_QSTR(MP_QSTR_bytes), (mp_obj_t)&mp_type_bytes },
#if MICROPY_PY_BUILTINS_BYTEARRAY
{ MP_OBJ_NEW_QSTR(MP_QSTR_bytearray), (mp_obj_t)&mp_type_bytearray },
#if MICROPY_ENABLE_FLOAT
#endif
#if MICROPY_PY_BUILTINS_COMPLEX
{ MP_OBJ_NEW_QSTR(MP_QSTR_complex), (mp_obj_t)&mp_type_complex },
#endif
{ MP_OBJ_NEW_QSTR(MP_QSTR_dict), (mp_obj_t)&mp_type_dict },
{ MP_OBJ_NEW_QSTR(MP_QSTR_enumerate), (mp_obj_t)&mp_type_enumerate },
{ MP_OBJ_NEW_QSTR(MP_QSTR_filter), (mp_obj_t)&mp_type_filter },
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
{ MP_OBJ_NEW_QSTR(MP_QSTR_float), (mp_obj_t)&mp_type_float },
#endif
#if MICROPY_PY_BUILTINS_FROZENSET
{ MP_OBJ_NEW_QSTR(MP_QSTR_frozenset), (mp_obj_t)&mp_type_frozenset },
#endif
{ MP_OBJ_NEW_QSTR(MP_QSTR_int), (mp_obj_t)&mp_type_int },
{ MP_OBJ_NEW_QSTR(MP_QSTR_list), (mp_obj_t)&mp_type_list },
{ MP_OBJ_NEW_QSTR(MP_QSTR_map), (mp_obj_t)&mp_type_map },
{ MP_OBJ_NEW_QSTR(MP_QSTR_object), (mp_obj_t)&mp_type_object },
#if MICROPY_ENABLE_PROPERTY
#if MICROPY_PY_BUILTINS_PROPERTY
{ MP_OBJ_NEW_QSTR(MP_QSTR_property), (mp_obj_t)&mp_type_property },
#endif
{ MP_OBJ_NEW_QSTR(MP_QSTR_range), (mp_obj_t)&mp_type_range },
#if MICROPY_PY_BUILTINS_SET
{ MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&mp_type_set },
#endif
{ MP_OBJ_NEW_QSTR(MP_QSTR_str), (mp_obj_t)&mp_type_str },
{ MP_OBJ_NEW_QSTR(MP_QSTR_super), (mp_obj_t)&mp_type_super },
{ MP_OBJ_NEW_QSTR(MP_QSTR_tuple), (mp_obj_t)&mp_type_tuple },
@@ -61,6 +94,7 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_exec), (mp_obj_t)&mp_builtin_exec_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_getattr), (mp_obj_t)&mp_builtin_getattr_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_globals), (mp_obj_t)&mp_builtin_globals_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_hasattr), (mp_obj_t)&mp_builtin_hasattr_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_hash), (mp_obj_t)&mp_builtin_hash_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_hex), (mp_obj_t)&mp_builtin_hex_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_id), (mp_obj_t)&mp_builtin_id_obj },
@@ -110,7 +144,7 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
// TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
// Extra builtins as defined by a port
MICROPY_EXTRA_BUILTINS
MICROPY_PORT_BUILTINS
};
const mp_obj_dict_t mp_builtin_object_dict_obj = {
@@ -118,8 +152,8 @@ const mp_obj_dict_t mp_builtin_object_dict_obj = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = ARRAY_SIZE(mp_builtin_object_table),
.alloc = ARRAY_SIZE(mp_builtin_object_table),
.used = MP_ARRAY_SIZE(mp_builtin_object_table),
.alloc = MP_ARRAY_SIZE(mp_builtin_object_table),
.table = (mp_map_elem_t*)mp_builtin_object_table,
},
};
@@ -128,29 +162,42 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___main__), (mp_obj_t)&mp_module___main__ },
{ MP_OBJ_NEW_QSTR(MP_QSTR_micropython), (mp_obj_t)&mp_module_micropython },
#if MICROPY_PY_ARRAY
{ MP_OBJ_NEW_QSTR(MP_QSTR_array), (mp_obj_t)&mp_module_array },
#if MICROPY_ENABLE_MOD_IO
{ MP_OBJ_NEW_QSTR(MP_QSTR_io), (mp_obj_t)&mp_module_io },
#endif
#if MICROPY_ENABLE_MOD_COLLECTIONS
#if MICROPY_PY_IO
{ MP_OBJ_NEW_QSTR(MP_QSTR__io), (mp_obj_t)&mp_module_io },
#endif
#if MICROPY_PY_COLLECTIONS
{ MP_OBJ_NEW_QSTR(MP_QSTR__collections), (mp_obj_t)&mp_module_collections },
#endif
#if MICROPY_ENABLE_MOD_STRUCT
#if MICROPY_PY_STRUCT
{ MP_OBJ_NEW_QSTR(MP_QSTR_struct), (mp_obj_t)&mp_module_struct },
#endif
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
#if MICROPY_PY_MATH
{ MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&mp_module_math },
#if MICROPY_ENABLE_MOD_CMATH
#endif
#if MICROPY_PY_CMATH
{ MP_OBJ_NEW_QSTR(MP_QSTR_cmath), (mp_obj_t)&mp_module_cmath },
#endif
#endif
#if MICROPY_ENABLE_MOD_SYS
#if MICROPY_PY_SYS
{ MP_OBJ_NEW_QSTR(MP_QSTR_sys), (mp_obj_t)&mp_module_sys },
#endif
#if MICROPY_PY_GC && MICROPY_ENABLE_GC
{ MP_OBJ_NEW_QSTR(MP_QSTR_gc), (mp_obj_t)&mp_module_gc },
#endif
// extmod modules
#if MICROPY_PY_UCTYPES
{ MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
#endif
// extra builtin modules as defined by a port
MICROPY_EXTRA_BUILTIN_MODULES
MICROPY_PORT_BUILTIN_MODULES
};
const mp_obj_dict_t mp_builtin_module_dict_obj = {
@@ -158,8 +205,8 @@ const mp_obj_dict_t mp_builtin_module_dict_obj = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = ARRAY_SIZE(mp_builtin_module_table),
.alloc = ARRAY_SIZE(mp_builtin_module_table),
.used = MP_ARRAY_SIZE(mp_builtin_module_table),
.alloc = MP_ARRAY_SIZE(mp_builtin_module_table),
.table = (mp_map_elem_t*)mp_builtin_module_table,
},
};

View File

@@ -1,2 +1,28 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
extern const mp_obj_dict_t mp_builtin_object_dict_obj;
extern const mp_obj_dict_t mp_builtin_module_dict_obj;

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@@ -5,8 +31,8 @@
#include <assert.h>
#include <math.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "lexer.h"
#include "parse.h"
@@ -30,6 +56,7 @@ typedef enum {
#include "grammar.h"
#undef DEF_RULE
PN_maximum_number_of,
PN_string, // special node for non-interned string
} pn_kind_t;
#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
@@ -46,8 +73,8 @@ typedef struct _compiler_t {
uint next_label;
uint break_label;
uint continue_label;
uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
uint16_t continue_label;
int break_continue_except_level;
uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
@@ -68,7 +95,7 @@ typedef struct _compiler_t {
STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
// TODO store the error message to a variable in compiler_t instead of printing it
if (MP_PARSE_NODE_IS_STRUCT(pn)) {
printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (machine_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
} else {
printf(" File \"%s\"\n", qstr_str(comp->source_file));
}
@@ -77,29 +104,94 @@ STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const cha
}
STATIC const mp_map_elem_t mp_constants_table[] = {
#if MICROPY_PY_UCTYPES
{ MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
#endif
// Extra constants as defined by a port
MICROPY_EXTRA_CONSTANTS
MICROPY_PORT_CONSTANTS
};
STATIC const mp_map_t mp_constants_map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = ARRAY_SIZE(mp_constants_table),
.alloc = ARRAY_SIZE(mp_constants_table),
.used = MP_ARRAY_SIZE(mp_constants_table),
.alloc = MP_ARRAY_SIZE(mp_constants_table),
.table = (mp_map_elem_t*)mp_constants_table,
};
STATIC mp_parse_node_t fold_constants(mp_parse_node_t pn) {
if (MP_PARSE_NODE_IS_STRUCT(pn)) {
// this function is essentially a simple preprocessor
STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
if (0) {
// dummy
#if MICROPY_COMP_CONST
} else if (MP_PARSE_NODE_IS_ID(pn)) {
// lookup identifier in table of dynamic constants
qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
if (elem != NULL) {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
}
#endif
} else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
// fold arguments first
for (int i = 0; i < n; i++) {
pns->nodes[i] = fold_constants(pns->nodes[i]);
// fold some parse nodes before folding their arguments
switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
#if MICROPY_COMP_CONST
case PN_expr_stmt:
if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
&& MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
&& MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
&& MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
&& MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
&& MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
) {
// code to assign dynamic constants: id = const(value)
// get the id
qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
// get the value
mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
pn_value = fold_constants(comp, pn_value, consts);
if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
break;
}
mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
// store the value in the table of dynamic constants
mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
if (elem->value != MP_OBJ_NULL) {
compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
break;
}
elem->value = MP_OBJ_NEW_SMALL_INT(value);
// replace const(value) with value
pns1->nodes[0] = pn_value;
// finished folding this assignment
return pn;
}
}
}
break;
#endif
case PN_string:
return pn;
}
// now try to fold this parse node
// fold arguments
int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
for (int i = 0; i < n; i++) {
pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
}
// try to fold this parse node
switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
case PN_atom_paren:
if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
@@ -111,8 +203,8 @@ STATIC mp_parse_node_t fold_constants(mp_parse_node_t pn) {
case PN_expr:
if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
// int | int
machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
}
break;
@@ -120,16 +212,16 @@ STATIC mp_parse_node_t fold_constants(mp_parse_node_t pn) {
case PN_and_expr:
if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
// int & int
machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
}
break;
case PN_shift_expr:
if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
// int << int
if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
@@ -146,10 +238,10 @@ STATIC mp_parse_node_t fold_constants(mp_parse_node_t pn) {
break;
case PN_arith_expr:
// overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
// overflow checking here relies on SMALL_INT being strictly smaller than mp_int_t
if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
// int + int
arg0 += arg1;
@@ -160,7 +252,7 @@ STATIC mp_parse_node_t fold_constants(mp_parse_node_t pn) {
// shouldn't happen
assert(0);
}
if (MP_PARSE_FITS_SMALL_INT(arg0)) {
if (MP_SMALL_INT_FITS(arg0)) {
//printf("%ld + %ld\n", arg0, arg1);
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
}
@@ -169,13 +261,13 @@ STATIC mp_parse_node_t fold_constants(mp_parse_node_t pn) {
case PN_term:
if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
mp_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
mp_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
// int * int
if (!mp_small_int_mul_overflow(arg0, arg1)) {
arg0 *= arg1;
if (MP_PARSE_FITS_SMALL_INT(arg0)) {
if (MP_SMALL_INT_FITS(arg0)) {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
}
}
@@ -199,7 +291,7 @@ STATIC mp_parse_node_t fold_constants(mp_parse_node_t pn) {
case PN_factor_2:
if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
// +int
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
@@ -247,8 +339,8 @@ STATIC mp_parse_node_t fold_constants(mp_parse_node_t pn) {
mp_obj_t dest[2];
mp_load_method_maybe(elem->value, q_attr, dest);
if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
machine_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
if (MP_PARSE_FITS_SMALL_INT(val)) {
mp_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
if (MP_SMALL_INT_FITS(val)) {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
}
}
@@ -340,6 +432,9 @@ void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
#if MICROPY_EMIT_CPYTHON
STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
return true;
}
if (!MP_PARSE_NODE_IS_LEAF(pn)) {
return false;
}
@@ -349,9 +444,7 @@ STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
return true;
}
STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
uint len;
const byte *str = qstr_data(qstr, &len);
STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
bool has_single_quote = false;
bool has_double_quote = false;
for (int i = 0; i < len; i++) {
@@ -390,6 +483,12 @@ STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
}
STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
cpython_c_print_quoted_str(vstr, (const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1], false);
return;
}
assert(MP_PARSE_NODE_IS_LEAF(pn));
if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
@@ -401,8 +500,13 @@ STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vst
case MP_PARSE_NODE_ID: assert(0);
case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
case MP_PARSE_NODE_STRING:
case MP_PARSE_NODE_BYTES: {
uint len;
const byte *str = qstr_data(arg, &len);
cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
break;
}
case MP_PARSE_NODE_TOKEN:
switch (arg) {
case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
@@ -934,7 +1038,10 @@ void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
if (comp->have_star) {
comp->num_dict_params += 1;
#if !MICROPY_EMIT_CPYTHON
#if MICROPY_EMIT_CPYTHON
EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
compile_node(comp, pn_equal);
#else
// in Micro Python we put the default dict parameters into a dictionary using the bytecode
if (comp->num_dict_params == 1) {
// in Micro Python we put the default positional parameters into a tuple using the bytecode
@@ -947,11 +1054,10 @@ void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
// first default dict param, so make the map
EMIT_ARG(build_map, 0);
}
#endif
EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
// compile value then key, then store it to the dict
compile_node(comp, pn_equal);
#if !MICROPY_EMIT_CPYTHON
// in Micro Python we put the default dict parameters into a dictionary using the bytecode
EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
EMIT(store_map);
#endif
} else {
@@ -968,7 +1074,7 @@ void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
// leaves function object on stack
// returns function name
qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
if (comp->pass == PASS_1) {
if (comp->pass == MP_PASS_SCOPE) {
// create a new scope for this function
scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
// store the function scope so the compiling function can use it at each pass
@@ -1017,7 +1123,7 @@ qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint
// leaves class object on stack
// returns class name
qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
if (comp->pass == PASS_1) {
if (comp->pass == MP_PASS_SCOPE) {
// create a new scope for this class
scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
// store the class scope so the compiling function can use it at each pass
@@ -1060,8 +1166,8 @@ STATIC bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_
}
qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
if (attr == MP_QSTR_byte_code) {
*emit_options = MP_EMIT_OPT_BYTE_CODE;
if (attr == MP_QSTR_bytecode) {
*emit_options = MP_EMIT_OPT_BYTECODE;
#if MICROPY_EMIT_NATIVE
} else if (attr == MP_QSTR_native) {
*emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
@@ -1484,7 +1590,7 @@ void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
}
void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
if (comp->pass == PASS_1) {
if (comp->pass == MP_PASS_SCOPE) {
if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
} else {
@@ -1498,7 +1604,7 @@ void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
}
void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
if (comp->pass == PASS_1) {
if (comp->pass == MP_PASS_SCOPE) {
if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
} else {
@@ -1644,6 +1750,7 @@ void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
// And, if the loop never runs, the loop variable should never be assigned
void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t pn_var, mp_parse_node_t pn_start, mp_parse_node_t pn_end, mp_parse_node_t pn_step, mp_parse_node_t pn_body, mp_parse_node_t pn_else) {
START_BREAK_CONTINUE_BLOCK
// note that we don't need to pop anything when breaking from an optimise for loop
uint top_label = comp_next_label(comp);
uint entry_label = comp_next_label(comp);
@@ -1742,6 +1849,7 @@ void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
#endif
START_BREAK_CONTINUE_BLOCK
comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
uint pop_label = comp_next_label(comp);
uint end_label = comp_next_label(comp);
@@ -1789,7 +1897,7 @@ void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except,
EMIT_ARG(jump, success_label); // jump over exception handler
EMIT_ARG(label_assign, l1); // start of exception handler
EMIT_ARG(adjust_stack_size, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
EMIT(start_except_handler);
uint l2 = comp_next_label(comp);
@@ -1861,7 +1969,7 @@ void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except,
compile_decrease_except_level(comp);
EMIT(end_finally);
EMIT_ARG(adjust_stack_size, -5); // stack adjust
EMIT(end_except_handler);
EMIT_ARG(label_assign, success_label);
compile_node(comp, pn_else); // else block, can be null
@@ -1972,7 +2080,8 @@ void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
} else {
// for non-REPL, evaluate then discard the expression
if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
|| MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
// do nothing with a lonely constant
} else {
compile_node(comp, pns->nodes[0]); // just an expression
@@ -2098,7 +2207,7 @@ void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
//mp_parse_node_t pn_params = pns->nodes[0];
//mp_parse_node_t pn_body = pns->nodes[1];
if (comp->pass == PASS_1) {
if (comp->pass == MP_PASS_SCOPE) {
// create a new scope for this lambda
scope_t *s = scope_new_and_link(comp, SCOPE_LAMBDA, (mp_parse_node_t)pns, comp->scope_cur->emit_options);
// store the lambda scope so the compiling function (this one) can use it at each pass
@@ -2412,26 +2521,40 @@ void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
int n_bytes = 0;
int string_kind = MP_PARSE_NODE_NULL;
for (int i = 0; i < n; i++) {
assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
int pn_kind;
if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
} else {
assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
pn_kind = MP_PARSE_NODE_STRING;
n_bytes += (mp_uint_t)pns_string->nodes[1];
}
if (i == 0) {
string_kind = pn_kind;
} else if (pn_kind != string_kind) {
compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
return;
}
n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
}
// concatenate string/bytes
byte *q_ptr;
byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
for (int i = 0; i < n; i++) {
uint s_len;
const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
memcpy(s_dest, s, s_len);
s_dest += s_len;
if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
uint s_len;
const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
memcpy(s_dest, s, s_len);
s_dest += s_len;
} else {
mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
memcpy(s_dest, (const char*)pns_string->nodes[0], (mp_uint_t)pns_string->nodes[1]);
s_dest += (mp_uint_t)pns_string->nodes[1];
}
}
qstr q = qstr_build_end(q_ptr);
@@ -2444,7 +2567,7 @@ void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
if (comp->pass == PASS_1) {
if (comp->pass == MP_PASS_SCOPE) {
// create a new scope for this comprehension
scope_t *s = scope_new_and_link(comp, kind, (mp_parse_node_t)pns, comp->scope_cur->emit_options);
// store the comprehension scope so the compiling function (this one) can use it at each pass
@@ -2738,10 +2861,10 @@ void compile_node(compiler_t *comp, mp_parse_node_t pn) {
if (MP_PARSE_NODE_IS_NULL(pn)) {
// pass
} else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
EMIT_ARG(load_const_small_int, arg);
} else if (MP_PARSE_NODE_IS_LEAF(pn)) {
machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
@@ -2762,15 +2885,19 @@ void compile_node(compiler_t *comp, mp_parse_node_t pn) {
} else {
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
EMIT_ARG(set_line_number, pns->source_line);
compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
if (f == NULL) {
printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
#if MICROPY_DEBUG_PRINTERS
mp_parse_node_print(pn, 0);
#endif
compile_syntax_error(comp, pn, "internal compiler error");
if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
EMIT_ARG(load_const_str, qstr_from_strn((const char*)pns->nodes[0], (mp_uint_t)pns->nodes[1]), false);
} else {
f(comp, pns);
compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
if (f == NULL) {
printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
#if MICROPY_DEBUG_PRINTERS
mp_parse_node_print(pn, 0);
#endif
compile_syntax_error(comp, pn, "internal compiler error");
} else {
f(comp, pns);
}
}
}
}
@@ -2947,13 +3074,13 @@ STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
// check the first statement for a doc string
if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
if (kind == MP_PARSE_NODE_STRING) {
compile_node(comp, pns->nodes[0]); // a doc string
// store doc string
if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
&& MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
|| MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
// compile the doc string
compile_node(comp, pns->nodes[0]);
// store the doc string
EMIT_ARG(store_id, MP_QSTR___doc__);
}
}
}
#endif
@@ -2965,7 +3092,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
comp->next_label = 1;
EMIT_ARG(start_pass, pass, scope);
if (comp->pass == PASS_1) {
if (comp->pass == MP_PASS_SCOPE) {
// reset maximum stack sizes in scope
// they will be computed in this first pass
scope->stack_size = 0;
@@ -2973,7 +3100,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
}
#if MICROPY_EMIT_CPYTHON
if (comp->pass == PASS_3) {
if (comp->pass == MP_PASS_EMIT) {
scope_print_info(scope);
}
#endif
@@ -2998,7 +3125,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
// work out number of parameters, keywords and default parameters, and add them to the id_info array
// must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
if (comp->pass == PASS_1) {
if (comp->pass == MP_PASS_SCOPE) {
comp->have_star = false;
apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
}
@@ -3018,7 +3145,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
// work out number of parameters, keywords and default parameters, and add them to the id_info array
// must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
if (comp->pass == PASS_1) {
if (comp->pass == MP_PASS_SCOPE) {
comp->have_star = false;
apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
}
@@ -3049,7 +3176,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
#else
qstr qstr_arg = MP_QSTR_;
#endif
if (comp->pass == PASS_1) {
if (comp->pass == MP_PASS_SCOPE) {
bool added;
id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
assert(added);
@@ -3086,7 +3213,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
if (comp->pass == PASS_1) {
if (comp->pass == MP_PASS_SCOPE) {
bool added;
id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
assert(added);
@@ -3122,6 +3249,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
}
#if MICROPY_EMIT_INLINE_THUMB
// requires 3 passes: SCOPE, CODE_SIZE, EMIT
STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
comp->pass = pass;
comp->scope_cur = scope;
@@ -3132,7 +3260,7 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
return;
}
if (comp->pass > PASS_1) {
if (comp->pass > MP_PASS_SCOPE) {
EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
}
@@ -3144,7 +3272,7 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
//qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
// parameters are in pns->nodes[1]
if (comp->pass == PASS_2) {
if (comp->pass == MP_PASS_CODE_SIZE) {
mp_parse_node_t *pn_params;
int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
@@ -3157,7 +3285,7 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
/*
if (comp->pass == PASS_3) {
if (comp->pass == MP_PASS_EMIT) {
//printf("----\n");
scope_print_info(scope);
}
@@ -3195,7 +3323,7 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
return;
}
uint lab = comp_next_label(comp);
if (pass > PASS_1) {
if (pass > MP_PASS_SCOPE) {
EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
}
} else if (op == MP_QSTR_align) {
@@ -3203,7 +3331,7 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
return;
}
if (pass > PASS_1) {
if (pass > MP_PASS_SCOPE) {
EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
}
} else if (op == MP_QSTR_data) {
@@ -3211,8 +3339,8 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
return;
}
if (pass > PASS_1) {
machine_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
if (pass > MP_PASS_SCOPE) {
mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
for (uint i = 1; i < n_args; i++) {
if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
@@ -3222,13 +3350,13 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
}
}
} else {
if (pass > PASS_1) {
if (pass > MP_PASS_SCOPE) {
EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
}
}
}
if (comp->pass > PASS_1) {
if (comp->pass > MP_PASS_SCOPE) {
bool success = EMIT_INLINE_ASM(end_pass);
if (!success) {
comp->had_error = true;
@@ -3368,7 +3496,10 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
comp->is_repl = is_repl;
// optimise constants
pn = fold_constants(pn);
mp_map_t consts;
mp_map_init(&consts, 0);
pn = fold_constants(comp, pn, &consts);
mp_map_deinit(&consts);
// set the outer scope
scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
@@ -3383,10 +3514,10 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
if (false) {
#if MICROPY_EMIT_INLINE_THUMB
} else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
compile_scope_inline_asm(comp, s, PASS_1);
compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
#endif
} else {
compile_scope(comp, s, PASS_1);
compile_scope(comp, s, MP_PASS_SCOPE);
}
// update maximim number of labels needed
@@ -3427,9 +3558,9 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
comp->emit_method_table = NULL;
comp->emit_inline_asm = emit_inline_thumb;
comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
compile_scope_inline_asm(comp, s, PASS_2);
compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
if (!comp->had_error) {
compile_scope_inline_asm(comp, s, PASS_3);
compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
}
#endif
@@ -3459,6 +3590,10 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
#endif
comp->emit = emit_native;
comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
// native emitters need an extra pass to compute stack size
compile_scope(comp, s, MP_PASS_STACK_SIZE);
break;
#endif // MICROPY_EMIT_NATIVE
@@ -3472,10 +3607,14 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
}
#endif // !MICROPY_EMIT_CPYTHON
// compile pass 2 and pass 3
compile_scope(comp, s, PASS_2);
// second last pass: compute code size
if (!comp->had_error) {
compile_scope(comp, s, PASS_3);
compile_scope(comp, s, MP_PASS_CODE_SIZE);
}
// final pass: emit code
if (!comp->had_error) {
compile_scope(comp, s, MP_PASS_EMIT);
}
}
}

View File

@@ -1,7 +1,33 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// These must fit in 8 bits; see scope.h
enum {
MP_EMIT_OPT_NONE,
MP_EMIT_OPT_BYTE_CODE,
MP_EMIT_OPT_BYTECODE,
MP_EMIT_OPT_NATIVE_PYTHON,
MP_EMIT_OPT_VIPER,
MP_EMIT_OPT_ASM_THUMB,

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/* Notes on passes:
* We don't know exactly the opcodes in pass 1 because they depend on the
* closing over of variables (LOAD_CLOSURE, BUILD_TUPLE, MAKE_CLOSURE), which
@@ -9,14 +35,17 @@
*/
typedef enum {
PASS_1 = 1, // work out id's and their kind, and number of labels
PASS_2 = 2, // work out stack size and code size and label offsets
PASS_3 = 3, // emit code
MP_PASS_SCOPE = 1, // work out id's and their kind, and number of labels
MP_PASS_STACK_SIZE = 2, // work out maximum stack size
MP_PASS_CODE_SIZE = 3, // work out code size and label offsets
MP_PASS_EMIT = 4, // emit code
} pass_kind_t;
#define MP_EMIT_STAR_FLAG_SINGLE (0x01)
#define MP_EMIT_STAR_FLAG_DOUBLE (0x02)
#define MP_EMIT_BREAK_FROM_FOR (0x8000)
typedef struct _emit_t emit_t;
typedef struct _emit_method_table_t {
@@ -36,7 +65,7 @@ typedef struct _emit_method_table_t {
void (*import_from)(emit_t *emit, qstr qstr);
void (*import_star)(emit_t *emit);
void (*load_const_tok)(emit_t *emit, mp_token_kind_t tok);
void (*load_const_small_int)(emit_t *emit, machine_int_t arg);
void (*load_const_small_int)(emit_t *emit, mp_int_t arg);
void (*load_const_int)(emit_t *emit, qstr qstr);
void (*load_const_dec)(emit_t *emit, qstr qstr);
void (*load_const_str)(emit_t *emit, qstr qstr, bool bytes);
@@ -105,6 +134,11 @@ typedef struct _emit_method_table_t {
void (*yield_value)(emit_t *emit);
void (*yield_from)(emit_t *emit);
// these methods are used to control entry to/exit from an exception handler
// they may or may not emit code
void (*start_except_handler)(emit_t *emit);
void (*end_except_handler)(emit_t *emit);
#if MICROPY_EMIT_CPYTHON
// these methods are only needed for emitcpy
void (*load_const_verbatim_str)(emit_t *emit, const char *str);

View File

@@ -1,11 +1,37 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "lexer.h"
#include "parse.h"
@@ -18,10 +44,14 @@
#if !MICROPY_EMIT_CPYTHON
#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
#define DUMMY_DATA_SIZE (BYTES_FOR_INT)
struct _emit_t {
pass_kind_t pass;
pass_kind_t pass : 8;
uint last_emit_was_return_value : 8;
int stack_size;
bool last_emit_was_return_value;
scope_t *scope;
@@ -33,10 +63,11 @@ struct _emit_t {
uint code_info_offset;
uint code_info_size;
uint byte_code_offset;
uint byte_code_size;
uint bytecode_offset;
uint bytecode_size;
byte *code_base; // stores both byte code and code info
byte dummy_data[8];
// Accessed as uint, so must be aligned as such
byte dummy_data[DUMMY_DATA_SIZE];
};
STATIC void emit_bc_rot_two(emit_t *emit);
@@ -57,7 +88,7 @@ void emit_bc_free(emit_t *emit) {
// all functions must go through this one to emit code info
STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) {
//printf("emit %d\n", num_bytes_to_write);
if (emit->pass < PASS_3) {
if (emit->pass < MP_PASS_EMIT) {
emit->code_info_offset += num_bytes_to_write;
return emit->dummy_data;
} else {
@@ -69,7 +100,7 @@ STATIC byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_writ
}
STATIC void emit_align_code_info_to_machine_word(emit_t* emit) {
emit->code_info_offset = (emit->code_info_offset + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1));
emit->code_info_offset = (emit->code_info_offset + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1));
}
STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qstr) {
@@ -95,57 +126,57 @@ STATIC void emit_write_code_info_bytes_lines(emit_t* emit, uint bytes_to_skip, u
#endif
// all functions must go through this one to emit byte code
STATIC byte* emit_get_cur_to_write_byte_code(emit_t* emit, int num_bytes_to_write) {
STATIC byte* emit_get_cur_to_write_bytecode(emit_t* emit, int num_bytes_to_write) {
//printf("emit %d\n", num_bytes_to_write);
if (emit->pass < PASS_3) {
emit->byte_code_offset += num_bytes_to_write;
if (emit->pass < MP_PASS_EMIT) {
emit->bytecode_offset += num_bytes_to_write;
return emit->dummy_data;
} else {
assert(emit->byte_code_offset + num_bytes_to_write <= emit->byte_code_size);
byte *c = emit->code_base + emit->code_info_size + emit->byte_code_offset;
emit->byte_code_offset += num_bytes_to_write;
assert(emit->bytecode_offset + num_bytes_to_write <= emit->bytecode_size);
byte *c = emit->code_base + emit->code_info_size + emit->bytecode_offset;
emit->bytecode_offset += num_bytes_to_write;
return c;
}
}
STATIC void emit_align_byte_code_to_machine_word(emit_t* emit) {
emit->byte_code_offset = (emit->byte_code_offset + sizeof(machine_uint_t) - 1) & (~(sizeof(machine_uint_t) - 1));
STATIC void emit_align_bytecode_to_machine_word(emit_t* emit) {
emit->bytecode_offset = (emit->bytecode_offset + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1));
}
STATIC void emit_write_byte_code_byte(emit_t* emit, byte b1) {
byte* c = emit_get_cur_to_write_byte_code(emit, 1);
STATIC void emit_write_bytecode_byte(emit_t* emit, byte b1) {
byte* c = emit_get_cur_to_write_bytecode(emit, 1);
c[0] = b1;
}
STATIC void emit_write_byte_code_byte_byte(emit_t* emit, byte b1, uint b2) {
STATIC void emit_write_bytecode_byte_byte(emit_t* emit, byte b1, uint b2) {
assert((b2 & (~0xff)) == 0);
byte* c = emit_get_cur_to_write_byte_code(emit, 2);
byte* c = emit_get_cur_to_write_bytecode(emit, 2);
c[0] = b1;
c[1] = b2;
}
STATIC void emit_write_byte_code_uint(emit_t* emit, uint num) {
STATIC void emit_write_bytecode_uint(emit_t* emit, uint num) {
// We store each 7 bits in a separate byte, and that's how many bytes needed
byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
byte buf[BYTES_FOR_INT];
byte *p = buf + sizeof(buf);
// We encode in little-ending order, but store in big-endian, to help decoding
do {
*--p = num & 0x7f;
num >>= 7;
} while (num != 0);
byte* c = emit_get_cur_to_write_byte_code(emit, buf + sizeof(buf) - p);
byte* c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
while (p != buf + sizeof(buf) - 1) {
*c++ = *p++ | 0x80;
}
*c = *p;
}
// Similar to emit_write_byte_code_uint(), just some extra handling to encode sign
STATIC void emit_write_byte_code_byte_int(emit_t* emit, byte b1, machine_int_t num) {
emit_write_byte_code_byte(emit, b1);
// Similar to emit_write_bytecode_uint(), just some extra handling to encode sign
STATIC void emit_write_bytecode_byte_int(emit_t* emit, byte b1, mp_int_t num) {
emit_write_bytecode_byte(emit, b1);
// We store each 7 bits in a separate byte, and that's how many bytes needed
byte buf[(BYTES_PER_WORD * 8 + 6) / 7];
byte buf[BYTES_FOR_INT];
byte *p = buf + sizeof(buf);
// We encode in little-ending order, but store in big-endian, to help decoding
do {
@@ -160,64 +191,66 @@ STATIC void emit_write_byte_code_byte_int(emit_t* emit, byte b1, machine_int_t n
*--p = 0;
}
byte* c = emit_get_cur_to_write_byte_code(emit, buf + sizeof(buf) - p);
byte* c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
while (p != buf + sizeof(buf) - 1) {
*c++ = *p++ | 0x80;
}
*c = *p;
}
STATIC void emit_write_byte_code_byte_uint(emit_t* emit, byte b, uint num) {
emit_write_byte_code_byte(emit, b);
emit_write_byte_code_uint(emit, num);
STATIC void emit_write_bytecode_byte_uint(emit_t* emit, byte b, uint num) {
emit_write_bytecode_byte(emit, b);
emit_write_bytecode_uint(emit, num);
}
// aligns the pointer so it is friendly to GC
STATIC void emit_write_byte_code_byte_ptr(emit_t* emit, byte b, void *ptr) {
emit_write_byte_code_byte(emit, b);
emit_align_byte_code_to_machine_word(emit);
machine_uint_t *c = (machine_uint_t*)emit_get_cur_to_write_byte_code(emit, sizeof(machine_uint_t));
*c = (machine_uint_t)ptr;
STATIC void emit_write_bytecode_byte_ptr(emit_t* emit, byte b, void *ptr) {
emit_write_bytecode_byte(emit, b);
emit_align_bytecode_to_machine_word(emit);
mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_uint_t));
// Verify thar c is already uint-aligned
assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
*c = (mp_uint_t)ptr;
}
/* currently unused
STATIC void emit_write_byte_code_byte_uint_uint(emit_t* emit, byte b, uint num1, uint num2) {
emit_write_byte_code_byte(emit, b);
emit_write_byte_code_byte_uint(emit, num1);
emit_write_byte_code_byte_uint(emit, num2);
STATIC void emit_write_bytecode_byte_uint_uint(emit_t* emit, byte b, uint num1, uint num2) {
emit_write_bytecode_byte(emit, b);
emit_write_bytecode_byte_uint(emit, num1);
emit_write_bytecode_byte_uint(emit, num2);
}
*/
STATIC void emit_write_byte_code_byte_qstr(emit_t* emit, byte b, qstr qstr) {
emit_write_byte_code_byte_uint(emit, b, qstr);
STATIC void emit_write_bytecode_byte_qstr(emit_t* emit, byte b, qstr qstr) {
emit_write_bytecode_byte_uint(emit, b, qstr);
}
// unsigned labels are relative to ip following this instruction, stored as 16 bits
STATIC void emit_write_byte_code_byte_unsigned_label(emit_t* emit, byte b1, uint label) {
uint byte_code_offset;
if (emit->pass < PASS_3) {
byte_code_offset = 0;
STATIC void emit_write_bytecode_byte_unsigned_label(emit_t* emit, byte b1, uint label) {
uint bytecode_offset;
if (emit->pass < MP_PASS_EMIT) {
bytecode_offset = 0;
} else {
byte_code_offset = emit->label_offsets[label] - emit->byte_code_offset - 3;
bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3;
}
byte *c = emit_get_cur_to_write_byte_code(emit, 3);
byte *c = emit_get_cur_to_write_bytecode(emit, 3);
c[0] = b1;
c[1] = byte_code_offset;
c[2] = byte_code_offset >> 8;
c[1] = bytecode_offset;
c[2] = bytecode_offset >> 8;
}
// signed labels are relative to ip following this instruction, stored as 16 bits, in excess
STATIC void emit_write_byte_code_byte_signed_label(emit_t* emit, byte b1, uint label) {
int byte_code_offset;
if (emit->pass < PASS_3) {
byte_code_offset = 0;
STATIC void emit_write_bytecode_byte_signed_label(emit_t* emit, byte b1, uint label) {
int bytecode_offset;
if (emit->pass < MP_PASS_EMIT) {
bytecode_offset = 0;
} else {
byte_code_offset = emit->label_offsets[label] - emit->byte_code_offset - 3 + 0x8000;
bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 3 + 0x8000;
}
byte* c = emit_get_cur_to_write_byte_code(emit, 3);
byte* c = emit_get_cur_to_write_bytecode(emit, 3);
c[0] = b1;
c[1] = byte_code_offset;
c[2] = byte_code_offset >> 8;
c[1] = bytecode_offset;
c[2] = bytecode_offset >> 8;
}
STATIC void emit_bc_set_native_types(emit_t *emit, bool do_native_types) {
@@ -230,16 +263,16 @@ STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
emit->scope = scope;
emit->last_source_line_offset = 0;
emit->last_source_line = 1;
if (pass == PASS_2) {
if (pass < MP_PASS_EMIT) {
memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(uint));
}
emit->byte_code_offset = 0;
emit->bytecode_offset = 0;
emit->code_info_offset = 0;
// write code info size (don't know size at this stage in PASS_2 so need to use maximum space (4 bytes) to write it)
// write code info size; use maximum space (4 bytes) to write it; TODO possible optimise this
{
byte* c = emit_get_cur_to_write_code_info(emit, 4);
machine_uint_t s = emit->code_info_size;
mp_uint_t s = emit->code_info_size;
c[0] = s & 0xff;
c[1] = (s >> 8) & 0xff;
c[2] = (s >> 16) & 0xff;
@@ -252,7 +285,7 @@ STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
// bytecode prelude: local state size and exception stack size; 16 bit uints for now
{
byte* c = emit_get_cur_to_write_byte_code(emit, 4);
byte* c = emit_get_cur_to_write_bytecode(emit, 4);
uint n_state = scope->num_locals + scope->stack_size;
if (n_state == 0) {
// Need at least 1 entry in the state, in the case an exception is
@@ -275,11 +308,11 @@ STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
}
}
assert(num_cell <= 255);
emit_write_byte_code_byte(emit, num_cell); // write number of locals that are cells
emit_write_bytecode_byte(emit, num_cell); // write number of locals that are cells
for (int i = 0; i < scope->id_info_len; i++) {
id_info_t *id = &scope->id_info[i];
if (id->kind == ID_INFO_KIND_CELL) {
emit_write_byte_code_byte(emit, id->local_num); // write the local which should be converted to a cell
emit_write_bytecode_byte(emit, id->local_num); // write the local which should be converted to a cell
}
}
}
@@ -291,21 +324,21 @@ STATIC void emit_bc_end_pass(emit_t *emit) {
}
*emit_get_cur_to_write_code_info(emit, 1) = 0; // end of line number info
emit_align_code_info_to_machine_word(emit); // align so that following byte_code is aligned
emit_align_code_info_to_machine_word(emit); // align so that following bytecode is aligned
if (emit->pass == PASS_2) {
if (emit->pass == MP_PASS_CODE_SIZE) {
// calculate size of code in bytes
emit->code_info_size = emit->code_info_offset;
emit->byte_code_size = emit->byte_code_offset;
emit->code_base = m_new0(byte, emit->code_info_size + emit->byte_code_size);
emit->bytecode_size = emit->bytecode_offset;
emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
} else if (emit->pass == PASS_3) {
} else if (emit->pass == MP_PASS_EMIT) {
qstr *arg_names = m_new(qstr, emit->scope->num_pos_args + emit->scope->num_kwonly_args);
for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
arg_names[i] = emit->scope->id_info[i].qstr;
}
mp_emit_glue_assign_byte_code(emit->scope->raw_code, emit->code_base,
emit->code_info_size + emit->byte_code_size,
mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
emit->code_info_size + emit->bytecode_size,
emit->scope->num_pos_args, emit->scope->num_kwonly_args, arg_names,
emit->scope->scope_flags);
}
@@ -320,14 +353,18 @@ STATIC void emit_bc_adjust_stack_size(emit_t *emit, int delta) {
}
STATIC void emit_bc_set_source_line(emit_t *emit, int source_line) {
//printf("source: line %d -> %d offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->byte_code_offset);
//printf("source: line %d -> %d offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->bytecode_offset);
#if MICROPY_ENABLE_SOURCE_LINE
if (mp_optimise_value >= 3) {
// If we compile with -O3, don't store line numbers.
return;
}
if (source_line > emit->last_source_line) {
uint bytes_to_skip = emit->byte_code_offset - emit->last_source_line_offset;
uint bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
uint lines_to_skip = source_line - emit->last_source_line;
emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
//printf(" %d %d\n", bytes_to_skip, lines_to_skip);
emit->last_source_line_offset = emit->byte_code_offset;
emit->last_source_line_offset = emit->bytecode_offset;
emit->last_source_line = source_line;
}
#endif
@@ -357,170 +394,170 @@ STATIC void emit_bc_pre(emit_t *emit, int stack_size_delta) {
STATIC void emit_bc_label_assign(emit_t *emit, uint l) {
emit_bc_pre(emit, 0);
assert(l < emit->max_num_labels);
if (emit->pass == PASS_2) {
if (emit->pass < MP_PASS_EMIT) {
// assign label offset
assert(emit->label_offsets[l] == -1);
emit->label_offsets[l] = emit->byte_code_offset;
} else if (emit->pass == PASS_3) {
// ensure label offset has not changed from PASS_2 to PASS_3
//printf("l%d: (at %d vs %d)\n", l, emit->byte_code_offset, emit->label_offsets[l]);
assert(emit->label_offsets[l] == emit->byte_code_offset);
emit->label_offsets[l] = emit->bytecode_offset;
} else {
// ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
//printf("l%d: (at %d vs %d)\n", l, emit->bytecode_offset, emit->label_offsets[l]);
assert(emit->label_offsets[l] == emit->bytecode_offset);
}
}
STATIC void emit_bc_import_name(emit_t *emit, qstr qstr) {
emit_bc_pre(emit, -1);
emit_write_byte_code_byte_qstr(emit, MP_BC_IMPORT_NAME, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qstr);
}
STATIC void emit_bc_import_from(emit_t *emit, qstr qstr) {
emit_bc_pre(emit, 1);
emit_write_byte_code_byte_qstr(emit, MP_BC_IMPORT_FROM, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qstr);
}
STATIC void emit_bc_import_star(emit_t *emit) {
emit_bc_pre(emit, -1);
emit_write_byte_code_byte(emit, MP_BC_IMPORT_STAR);
emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
}
STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
emit_bc_pre(emit, 1);
switch (tok) {
case MP_TOKEN_KW_FALSE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
case MP_TOKEN_KW_NONE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_NONE); break;
case MP_TOKEN_KW_TRUE: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
case MP_TOKEN_ELLIPSIS: emit_write_byte_code_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;
case MP_TOKEN_ELLIPSIS: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_ELLIPSIS); break;
default: assert(0);
}
}
STATIC void emit_bc_load_const_small_int(emit_t *emit, machine_int_t arg) {
STATIC void emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
emit_bc_pre(emit, 1);
emit_write_byte_code_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
emit_write_bytecode_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg);
}
STATIC void emit_bc_load_const_int(emit_t *emit, qstr qstr) {
emit_bc_pre(emit, 1);
emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qstr);
}
STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qstr) {
emit_bc_pre(emit, 1);
emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qstr);
}
STATIC void emit_bc_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
emit_bc_pre(emit, 1);
if (bytes) {
emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qstr);
} else {
emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_STRING, qstr);
}
}
STATIC void emit_bc_load_null(emit_t *emit) {
emit_bc_pre(emit, 1);
emit_write_byte_code_byte(emit, MP_BC_LOAD_NULL);
emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
};
STATIC void emit_bc_load_fast(emit_t *emit, qstr qstr, uint id_flags, int local_num) {
assert(local_num >= 0);
emit_bc_pre(emit, 1);
switch (local_num) {
case 0: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_0); break;
case 1: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_1); break;
case 2: emit_write_byte_code_byte(emit, MP_BC_LOAD_FAST_2); break;
default: emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num); break;
case 0: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_0); break;
case 1: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_1); break;
case 2: emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_2); break;
default: emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num); break;
}
}
STATIC void emit_bc_load_deref(emit_t *emit, qstr qstr, int local_num) {
emit_bc_pre(emit, 1);
emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
}
STATIC void emit_bc_load_name(emit_t *emit, qstr qstr) {
emit_bc_pre(emit, 1);
emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_NAME, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qstr);
}
STATIC void emit_bc_load_global(emit_t *emit, qstr qstr) {
emit_bc_pre(emit, 1);
emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qstr);
}
STATIC void emit_bc_load_attr(emit_t *emit, qstr qstr) {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_ATTR, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qstr);
}
STATIC void emit_bc_load_method(emit_t *emit, qstr qstr) {
emit_bc_pre(emit, 1);
emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_METHOD, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qstr);
}
STATIC void emit_bc_load_build_class(emit_t *emit) {
emit_bc_pre(emit, 1);
emit_write_byte_code_byte(emit, MP_BC_LOAD_BUILD_CLASS);
emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
}
STATIC void emit_bc_load_subscr(emit_t *emit) {
emit_bc_pre(emit, -1);
emit_write_byte_code_byte(emit, MP_BC_LOAD_SUBSCR);
emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
}
STATIC void emit_bc_store_fast(emit_t *emit, qstr qstr, int local_num) {
assert(local_num >= 0);
emit_bc_pre(emit, -1);
switch (local_num) {
case 0: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_0); break;
case 1: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_1); break;
case 2: emit_write_byte_code_byte(emit, MP_BC_STORE_FAST_2); break;
default: emit_write_byte_code_byte_uint(emit, MP_BC_STORE_FAST_N, local_num); break;
case 0: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_0); break;
case 1: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_1); break;
case 2: emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_2); break;
default: emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num); break;
}
}
STATIC void emit_bc_store_deref(emit_t *emit, qstr qstr, int local_num) {
emit_bc_pre(emit, -1);
emit_write_byte_code_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
}
STATIC void emit_bc_store_name(emit_t *emit, qstr qstr) {
emit_bc_pre(emit, -1);
emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_NAME, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qstr);
}
STATIC void emit_bc_store_global(emit_t *emit, qstr qstr) {
emit_bc_pre(emit, -1);
emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_GLOBAL, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qstr);
}
STATIC void emit_bc_store_attr(emit_t *emit, qstr qstr) {
emit_bc_pre(emit, -2);
emit_write_byte_code_byte_qstr(emit, MP_BC_STORE_ATTR, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qstr);
}
STATIC void emit_bc_store_subscr(emit_t *emit) {
emit_bc_pre(emit, -3);
emit_write_byte_code_byte(emit, MP_BC_STORE_SUBSCR);
emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
}
STATIC void emit_bc_delete_fast(emit_t *emit, qstr qstr, int local_num) {
emit_write_byte_code_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
}
STATIC void emit_bc_delete_deref(emit_t *emit, qstr qstr, int local_num) {
emit_write_byte_code_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
}
STATIC void emit_bc_delete_name(emit_t *emit, qstr qstr) {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_NAME, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qstr);
}
STATIC void emit_bc_delete_global(emit_t *emit, qstr qstr) {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qstr);
emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qstr);
}
STATIC void emit_bc_delete_attr(emit_t *emit, qstr qstr) {
@@ -537,97 +574,101 @@ STATIC void emit_bc_delete_subscr(emit_t *emit) {
STATIC void emit_bc_dup_top(emit_t *emit) {
emit_bc_pre(emit, 1);
emit_write_byte_code_byte(emit, MP_BC_DUP_TOP);
emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
}
STATIC void emit_bc_dup_top_two(emit_t *emit) {
emit_bc_pre(emit, 2);
emit_write_byte_code_byte(emit, MP_BC_DUP_TOP_TWO);
emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
}
STATIC void emit_bc_pop_top(emit_t *emit) {
emit_bc_pre(emit, -1);
emit_write_byte_code_byte(emit, MP_BC_POP_TOP);
emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
}
STATIC void emit_bc_rot_two(emit_t *emit) {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte(emit, MP_BC_ROT_TWO);
emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
}
STATIC void emit_bc_rot_three(emit_t *emit) {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte(emit, MP_BC_ROT_THREE);
emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
}
STATIC void emit_bc_jump(emit_t *emit, uint label) {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP, label);
emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
}
STATIC void emit_bc_pop_jump_if_true(emit_t *emit, uint label) {
emit_bc_pre(emit, -1);
emit_write_byte_code_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
}
STATIC void emit_bc_pop_jump_if_false(emit_t *emit, uint label) {
emit_bc_pre(emit, -1);
emit_write_byte_code_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
}
STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, uint label) {
emit_bc_pre(emit, -1);
emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
}
STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, uint label) {
emit_bc_pre(emit, -1);
emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
}
STATIC void emit_bc_unwind_jump(emit_t *emit, uint label, int except_depth) {
if (except_depth == 0) {
emit_bc_jump(emit, label);
} else {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label);
emit_write_byte_code_byte(emit, except_depth);
if (label & MP_EMIT_BREAK_FROM_FOR) {
// need to pop the iterator if we are breaking out of a for loop
emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
}
emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
} else {
emit_write_bytecode_byte_signed_label(emit, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
emit_write_bytecode_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
}
}
STATIC void emit_bc_setup_with(emit_t *emit, uint label) {
emit_bc_pre(emit, 7);
emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
}
STATIC void emit_bc_with_cleanup(emit_t *emit) {
emit_bc_pre(emit, -7);
emit_write_byte_code_byte(emit, MP_BC_WITH_CLEANUP);
emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
}
STATIC void emit_bc_setup_except(emit_t *emit, uint label) {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
}
STATIC void emit_bc_setup_finally(emit_t *emit, uint label) {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
}
STATIC void emit_bc_end_finally(emit_t *emit) {
emit_bc_pre(emit, -1);
emit_write_byte_code_byte(emit, MP_BC_END_FINALLY);
emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
}
STATIC void emit_bc_get_iter(emit_t *emit) {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte(emit, MP_BC_GET_ITER);
emit_write_bytecode_byte(emit, MP_BC_GET_ITER);
}
STATIC void emit_bc_for_iter(emit_t *emit, uint label) {
emit_bc_pre(emit, 1);
emit_write_byte_code_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
}
STATIC void emit_bc_for_iter_end(emit_t *emit) {
@@ -636,23 +677,23 @@ STATIC void emit_bc_for_iter_end(emit_t *emit) {
STATIC void emit_bc_pop_block(emit_t *emit) {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte(emit, MP_BC_POP_BLOCK);
emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
}
STATIC void emit_bc_pop_except(emit_t *emit) {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte(emit, MP_BC_POP_EXCEPT);
emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
}
STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
if (op == MP_UNARY_OP_NOT) {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte_byte(emit, MP_BC_UNARY_OP, MP_UNARY_OP_BOOL);
emit_write_bytecode_byte_byte(emit, MP_BC_UNARY_OP, MP_UNARY_OP_BOOL);
emit_bc_pre(emit, 0);
emit_write_byte_code_byte(emit, MP_BC_NOT);
emit_write_bytecode_byte(emit, MP_BC_NOT);
} else {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte_byte(emit, MP_BC_UNARY_OP, op);
emit_write_bytecode_byte_byte(emit, MP_BC_UNARY_OP, op);
}
}
@@ -666,98 +707,98 @@ STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
op = MP_BINARY_OP_IS;
}
emit_bc_pre(emit, -1);
emit_write_byte_code_byte_byte(emit, MP_BC_BINARY_OP, op);
emit_write_bytecode_byte_byte(emit, MP_BC_BINARY_OP, op);
if (invert) {
emit_bc_pre(emit, 0);
emit_write_byte_code_byte(emit, MP_BC_NOT);
emit_write_bytecode_byte(emit, MP_BC_NOT);
}
}
STATIC void emit_bc_build_tuple(emit_t *emit, int n_args) {
assert(n_args >= 0);
emit_bc_pre(emit, 1 - n_args);
emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
}
STATIC void emit_bc_build_list(emit_t *emit, int n_args) {
assert(n_args >= 0);
emit_bc_pre(emit, 1 - n_args);
emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
}
STATIC void emit_bc_list_append(emit_t *emit, int list_stack_index) {
assert(list_stack_index >= 0);
emit_bc_pre(emit, -1);
emit_write_byte_code_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
}
STATIC void emit_bc_build_map(emit_t *emit, int n_args) {
assert(n_args >= 0);
emit_bc_pre(emit, 1);
emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
}
STATIC void emit_bc_store_map(emit_t *emit) {
emit_bc_pre(emit, -2);
emit_write_byte_code_byte(emit, MP_BC_STORE_MAP);
emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
}
STATIC void emit_bc_map_add(emit_t *emit, int map_stack_index) {
assert(map_stack_index >= 0);
emit_bc_pre(emit, -2);
emit_write_byte_code_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
}
STATIC void emit_bc_build_set(emit_t *emit, int n_args) {
assert(n_args >= 0);
emit_bc_pre(emit, 1 - n_args);
emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_SET, n_args);
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
}
STATIC void emit_bc_set_add(emit_t *emit, int set_stack_index) {
assert(set_stack_index >= 0);
emit_bc_pre(emit, -1);
emit_write_byte_code_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
}
STATIC void emit_bc_build_slice(emit_t *emit, int n_args) {
assert(n_args >= 0);
emit_bc_pre(emit, 1 - n_args);
emit_write_byte_code_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
}
STATIC void emit_bc_unpack_sequence(emit_t *emit, int n_args) {
assert(n_args >= 0);
emit_bc_pre(emit, -1 + n_args);
emit_write_byte_code_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
}
STATIC void emit_bc_unpack_ex(emit_t *emit, int n_left, int n_right) {
assert(n_left >=0 && n_right >= 0);
emit_bc_pre(emit, -1 + n_left + n_right + 1);
emit_write_byte_code_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
}
STATIC void emit_bc_make_function(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults) {
if (n_pos_defaults == 0 && n_kw_defaults == 0) {
emit_bc_pre(emit, 1);
emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
} else {
emit_bc_pre(emit, -1);
emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
}
}
STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, uint n_closed_over, uint n_pos_defaults, uint n_kw_defaults) {
if (n_pos_defaults == 0 && n_kw_defaults == 0) {
emit_bc_pre(emit, -n_closed_over + 1);
emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
emit_write_byte_code_byte(emit, n_closed_over);
emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
emit_write_bytecode_byte(emit, n_closed_over);
} else {
assert(n_closed_over <= 255);
emit_bc_pre(emit, -2 - n_closed_over + 1);
emit_write_byte_code_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
emit_write_byte_code_byte(emit, n_closed_over);
emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
emit_write_bytecode_byte(emit, n_closed_over);
}
}
@@ -772,10 +813,10 @@ STATIC void emit_bc_call_function_method_helper(emit_t *emit, int stack_adj, uin
emit_bc_load_null(emit);
}
emit_bc_pre(emit, stack_adj - n_positional - 2 * n_keyword - 2);
emit_write_byte_code_byte_uint(emit, bytecode_base + 1, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
emit_write_bytecode_byte_uint(emit, bytecode_base + 1, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
} else {
emit_bc_pre(emit, stack_adj - n_positional - 2 * n_keyword);
emit_write_byte_code_byte_uint(emit, bytecode_base, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
emit_write_bytecode_byte_uint(emit, bytecode_base, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
}
}
@@ -790,29 +831,33 @@ STATIC void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, u
STATIC void emit_bc_return_value(emit_t *emit) {
emit_bc_pre(emit, -1);
emit->last_emit_was_return_value = true;
emit_write_byte_code_byte(emit, MP_BC_RETURN_VALUE);
emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
}
STATIC void emit_bc_raise_varargs(emit_t *emit, int n_args) {
assert(0 <= n_args && n_args <= 2);
emit_bc_pre(emit, -n_args);
emit_write_byte_code_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
}
STATIC void emit_bc_yield_value(emit_t *emit) {
emit_bc_pre(emit, 0);
if (emit->pass == PASS_2) {
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
}
emit_write_byte_code_byte(emit, MP_BC_YIELD_VALUE);
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
}
STATIC void emit_bc_yield_from(emit_t *emit) {
emit_bc_pre(emit, -1);
if (emit->pass == PASS_2) {
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
}
emit_write_byte_code_byte(emit, MP_BC_YIELD_FROM);
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
}
STATIC void emit_bc_start_except_handler(emit_t *emit) {
emit_bc_adjust_stack_size(emit, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
}
STATIC void emit_bc_end_except_handler(emit_t *emit) {
emit_bc_adjust_stack_size(emit, -5); // stack adjust
}
const emit_method_table_t emit_bc_method_table = {
@@ -900,6 +945,9 @@ const emit_method_table_t emit_bc_method_table = {
emit_bc_raise_varargs,
emit_bc_yield_value,
emit_bc_yield_from,
emit_bc_start_except_handler,
emit_bc_end_except_handler,
};
#endif // !MICROPY_EMIT_CPYTHON

View File

@@ -1,9 +1,35 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "lexer.h"
#include "parse.h"

View File

@@ -1,11 +1,37 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "lexer.h"
#include "parse.h"
@@ -20,7 +46,7 @@
struct _emit_t {
int pass;
int byte_code_offset;
int bytecode_offset;
int stack_size;
bool last_emit_was_return_value;
@@ -42,11 +68,11 @@ STATIC void emit_cpy_set_native_types(emit_t *emit, bool do_native_types) {
STATIC void emit_cpy_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
emit->pass = pass;
emit->byte_code_offset = 0;
emit->bytecode_offset = 0;
emit->stack_size = 0;
emit->last_emit_was_return_value = false;
emit->scope = scope;
if (pass == PASS_2) {
if (pass < MP_PASS_EMIT) {
memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(int));
}
}
@@ -82,60 +108,60 @@ STATIC void emit_cpy_delete_id(emit_t *emit, qstr qstr) {
}
// TODO: module-polymorphic function (read: name clash if made global)
static void emit_pre(emit_t *emit, int stack_size_delta, int byte_code_size) {
static void emit_pre(emit_t *emit, int stack_size_delta, int bytecode_size) {
emit->stack_size += stack_size_delta;
if (emit->stack_size > emit->scope->stack_size) {
emit->scope->stack_size = emit->stack_size;
}
emit->last_emit_was_return_value = false;
if (emit->pass == PASS_3 && byte_code_size > 0) {
if (emit->byte_code_offset >= 1000) {
printf("%d ", emit->byte_code_offset);
if (emit->pass == MP_PASS_EMIT && bytecode_size > 0) {
if (emit->bytecode_offset >= 1000) {
printf("%d ", emit->bytecode_offset);
} else {
printf("% 4d ", emit->byte_code_offset);
printf("% 4d ", emit->bytecode_offset);
}
}
emit->byte_code_offset += byte_code_size;
emit->bytecode_offset += bytecode_size;
}
STATIC void emit_cpy_label_assign(emit_t *emit, uint l) {
emit_pre(emit, 0, 0);
assert(l < emit->max_num_labels);
if (emit->pass == PASS_2) {
if (emit->pass < MP_PASS_EMIT) {
// assign label offset
assert(emit->label_offsets[l] == -1);
emit->label_offsets[l] = emit->byte_code_offset;
} else if (emit->pass == PASS_3) {
// ensure label offset has not changed from PASS_2 to PASS_3
assert(emit->label_offsets[l] == emit->byte_code_offset);
//printf("l%d: (at %d)\n", l, emit->byte_code_offset);
emit->label_offsets[l] = emit->bytecode_offset;
} else {
// ensure label offset has not changed from MP_PASS_CODE_SIZE to MP_PASS_EMIT
assert(emit->label_offsets[l] == emit->bytecode_offset);
//printf("l%d: (at %d)\n", l, emit->bytecode_offset);
}
}
STATIC void emit_cpy_import_name(emit_t *emit, qstr qstr) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("IMPORT_NAME %s\n", qstr_str(qstr));
}
}
STATIC void emit_cpy_import_from(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("IMPORT_FROM %s\n", qstr_str(qstr));
}
}
STATIC void emit_cpy_import_star(emit_t *emit) {
emit_pre(emit, -1, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("IMPORT_STAR\n");
}
}
STATIC void emit_cpy_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_CONST ");
switch (tok) {
case MP_TOKEN_KW_FALSE: printf("False"); break;
@@ -147,23 +173,23 @@ STATIC void emit_cpy_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
}
}
STATIC void emit_cpy_load_const_small_int(emit_t *emit, machine_int_t arg) {
STATIC void emit_cpy_load_const_small_int(emit_t *emit, mp_int_t arg) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_CONST " INT_FMT "\n", arg);
}
}
STATIC void emit_cpy_load_const_int(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_CONST %s\n", qstr_str(qstr));
}
}
STATIC void emit_cpy_load_const_dec(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_CONST %s\n", qstr_str(qstr));
}
}
@@ -207,7 +233,7 @@ STATIC void print_quoted_str(qstr qstr, bool bytes) {
STATIC void emit_cpy_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_CONST ");
print_quoted_str(qstr, bytes);
printf("\n");
@@ -221,35 +247,35 @@ STATIC void emit_cpy_load_null(emit_t *emit) {
STATIC void emit_cpy_load_fast(emit_t *emit, qstr qstr, uint id_flags, int local_num) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_FAST %d %s\n", local_num, qstr_str(qstr));
}
}
STATIC void emit_cpy_load_deref(emit_t *emit, qstr qstr, int local_num) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_DEREF %d %s\n", local_num, qstr_str(qstr));
}
}
STATIC void emit_cpy_load_name(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_NAME %s\n", qstr_str(qstr));
}
}
STATIC void emit_cpy_load_global(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_GLOBAL %s\n", qstr_str(qstr));
}
}
STATIC void emit_cpy_load_attr(emit_t *emit, qstr qstr) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_ATTR %s\n", qstr_str(qstr));
}
}
@@ -260,142 +286,142 @@ STATIC void emit_cpy_load_method(emit_t *emit, qstr qstr) {
STATIC void emit_cpy_load_build_class(emit_t *emit) {
emit_pre(emit, 1, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_BUILD_CLASS\n");
}
}
STATIC void emit_cpy_load_subscr(emit_t *emit) {
emit_pre(emit, -1, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("BINARY_SUBSCR\n");
}
}
STATIC void emit_cpy_store_fast(emit_t *emit, qstr qstr, int local_num) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("STORE_FAST %d %s\n", local_num, qstr_str(qstr));
}
}
STATIC void emit_cpy_store_deref(emit_t *emit, qstr qstr, int local_num) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("STORE_DEREF %d %s\n", local_num, qstr_str(qstr));
}
}
STATIC void emit_cpy_store_name(emit_t *emit, qstr qstr) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("STORE_NAME %s\n", qstr_str(qstr));
}
}
STATIC void emit_cpy_store_global(emit_t *emit, qstr qstr) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("STORE_GLOBAL %s\n", qstr_str(qstr));
}
}
STATIC void emit_cpy_store_attr(emit_t *emit, qstr qstr) {
emit_pre(emit, -2, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("STORE_ATTR %s\n", qstr_str(qstr));
}
}
STATIC void emit_cpy_store_subscr(emit_t *emit) {
emit_pre(emit, -3, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("STORE_SUBSCR\n");
}
}
STATIC void emit_cpy_delete_fast(emit_t *emit, qstr qstr, int local_num) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("DELETE_FAST %d %s\n", local_num, qstr_str(qstr));
}
}
STATIC void emit_cpy_delete_deref(emit_t *emit, qstr qstr, int local_num) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("DELETE_DEREF %d %s\n", local_num, qstr_str(qstr));
}
}
STATIC void emit_cpy_delete_name(emit_t *emit, qstr qstr) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("DELETE_NAME %s\n", qstr_str(qstr));
}
}
STATIC void emit_cpy_delete_global(emit_t *emit, qstr qstr) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("DELETE_GLOBAL %s\n", qstr_str(qstr));
}
}
STATIC void emit_cpy_delete_attr(emit_t *emit, qstr qstr) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("DELETE_ATTR %s\n", qstr_str(qstr));
}
}
STATIC void emit_cpy_delete_subscr(emit_t *emit) {
emit_pre(emit, -2, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("DELETE_SUBSCR\n");
}
}
STATIC void emit_cpy_dup_top(emit_t *emit) {
emit_pre(emit, 1, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("DUP_TOP\n");
}
}
STATIC void emit_cpy_dup_top_two(emit_t *emit) {
emit_pre(emit, 2, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("DUP_TOP_TWO\n");
}
}
STATIC void emit_cpy_pop_top(emit_t *emit) {
emit_pre(emit, -1, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("POP_TOP\n");
}
}
STATIC void emit_cpy_rot_two(emit_t *emit) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("ROT_TWO\n");
}
}
STATIC void emit_cpy_rot_three(emit_t *emit) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("ROT_THREE\n");
}
}
STATIC void emit_cpy_jump(emit_t *emit, uint label) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
int dest = emit->label_offsets[label];
if (dest < emit->byte_code_offset) {
if (dest < emit->bytecode_offset) {
printf("JUMP_ABSOLUTE %d\n", emit->label_offsets[label]);
} else {
printf("JUMP_FORWARD %d\n", emit->label_offsets[label]);
@@ -405,35 +431,35 @@ STATIC void emit_cpy_jump(emit_t *emit, uint label) {
STATIC void emit_cpy_pop_jump_if_true(emit_t *emit, uint label) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("POP_JUMP_IF_TRUE %d\n", emit->label_offsets[label]);
}
}
STATIC void emit_cpy_pop_jump_if_false(emit_t *emit, uint label) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("POP_JUMP_IF_FALSE %d\n", emit->label_offsets[label]);
}
}
STATIC void emit_cpy_jump_if_true_or_pop(emit_t *emit, uint label) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("JUMP_IF_TRUE_OR_POP %d\n", emit->label_offsets[label]);
}
}
STATIC void emit_cpy_jump_if_false_or_pop(emit_t *emit, uint label) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("JUMP_IF_FALSE_OR_POP %d\n", emit->label_offsets[label]);
}
}
STATIC void emit_cpy_break_loop(emit_t *emit, uint label, int except_depth) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("BREAK_LOOP\n");
}
}
@@ -443,7 +469,7 @@ STATIC void emit_cpy_continue_loop(emit_t *emit, uint label, int except_depth) {
emit_cpy_jump(emit, label);
} else {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("CONTINUE_LOOP %d\n", emit->label_offsets[label]);
}
}
@@ -451,49 +477,49 @@ STATIC void emit_cpy_continue_loop(emit_t *emit, uint label, int except_depth) {
STATIC void emit_cpy_setup_with(emit_t *emit, uint label) {
emit_pre(emit, 7, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("SETUP_WITH %d\n", emit->label_offsets[label]);
}
}
STATIC void emit_cpy_with_cleanup(emit_t *emit) {
emit_pre(emit, -7, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("WITH_CLEANUP\n");
}
}
STATIC void emit_cpy_setup_except(emit_t *emit, uint label) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("SETUP_EXCEPT %d\n", emit->label_offsets[label]);
}
}
STATIC void emit_cpy_setup_finally(emit_t *emit, uint label) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("SETUP_FINALLY %d\n", emit->label_offsets[label]);
}
}
STATIC void emit_cpy_end_finally(emit_t *emit) {
emit_pre(emit, -1, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("END_FINALLY\n");
}
}
STATIC void emit_cpy_get_iter(emit_t *emit) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("GET_ITER\n");
}
}
STATIC void emit_cpy_for_iter(emit_t *emit, uint label) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("FOR_ITER %d\n", emit->label_offsets[label]);
}
}
@@ -504,21 +530,21 @@ STATIC void emit_cpy_for_iter_end(emit_t *emit) {
STATIC void emit_cpy_pop_block(emit_t *emit) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("POP_BLOCK\n");
}
}
STATIC void emit_cpy_pop_except(emit_t *emit) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("POP_EXCEPT\n");
}
}
STATIC void emit_cpy_unary_op(emit_t *emit, mp_unary_op_t op) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
switch (op) {
case MP_UNARY_OP_POSITIVE: printf("UNARY_POSITIVE\n"); break;
case MP_UNARY_OP_NEGATIVE: printf("UNARY_NEGATIVE\n"); break;
@@ -537,7 +563,7 @@ STATIC void emit_cpy_binary_op(emit_t *emit, mp_binary_op_t op) {
// CPython uses a byte code plus an argument for compare ops
emit_pre(emit, -1, 3);
}
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
switch (op) {
case MP_BINARY_OP_OR: printf("BINARY_OR\n"); break;
case MP_BINARY_OP_XOR: printf("BINARY_XOR\n"); break;
@@ -581,77 +607,77 @@ STATIC void emit_cpy_binary_op(emit_t *emit, mp_binary_op_t op) {
STATIC void emit_cpy_build_tuple(emit_t *emit, int n_args) {
emit_pre(emit, 1 - n_args, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("BUILD_TUPLE %d\n", n_args);
}
}
STATIC void emit_cpy_build_list(emit_t *emit, int n_args) {
emit_pre(emit, 1 - n_args, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("BUILD_LIST %d\n", n_args);
}
}
STATIC void emit_cpy_list_append(emit_t *emit, int list_index) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LIST_APPEND %d\n", list_index);
}
}
STATIC void emit_cpy_build_map(emit_t *emit, int n_args) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("BUILD_MAP %d\n", n_args);
}
}
STATIC void emit_cpy_store_map(emit_t *emit) {
emit_pre(emit, -2, 1);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("STORE_MAP\n");
}
}
STATIC void emit_cpy_map_add(emit_t *emit, int map_index) {
emit_pre(emit, -2, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("MAP_ADD %d\n", map_index);
}
}
STATIC void emit_cpy_build_set(emit_t *emit, int n_args) {
emit_pre(emit, 1 - n_args, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("BUILD_SET %d\n", n_args);
}
}
STATIC void emit_cpy_set_add(emit_t *emit, int set_index) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("SET_ADD %d\n", set_index);
}
}
STATIC void emit_cpy_build_slice(emit_t *emit, int n_args) {
emit_pre(emit, 1 - n_args, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("BUILD_SLICE %d\n", n_args);
}
}
STATIC void emit_cpy_unpack_sequence(emit_t *emit, int n_args) {
emit_pre(emit, -1 + n_args, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("UNPACK_SEQUENCE %d\n", n_args);
}
}
STATIC void emit_cpy_unpack_ex(emit_t *emit, int n_left, int n_right) {
emit_pre(emit, -1 + n_left + n_right + 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("UNPACK_EX %d\n", n_left | (n_right << 8));
}
}
@@ -665,7 +691,7 @@ STATIC void emit_cpy_call_function(emit_t *emit, int n_positional, int n_keyword
s += 1;
}
emit_pre(emit, -n_positional - 2 * n_keyword - s, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
printf("CALL_FUNCTION_VAR_KW");
@@ -690,26 +716,26 @@ STATIC void emit_cpy_call_method(emit_t *emit, int n_positional, int n_keyword,
STATIC void emit_cpy_return_value(emit_t *emit) {
emit_pre(emit, -1, 1);
emit->last_emit_was_return_value = true;
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("RETURN_VALUE\n");
}
}
STATIC void emit_cpy_raise_varargs(emit_t *emit, int n_args) {
emit_pre(emit, -n_args, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("RAISE_VARARGS %d\n", n_args);
}
}
STATIC void load_cpy_const_code_and_name(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_CONST code %s\n", qstr_str(qstr));
}
// load qualified name
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_CONST '");
// code just to work out the qualname (or whatever it is)
{
@@ -736,7 +762,7 @@ STATIC void load_cpy_const_code_and_name(emit_t *emit, qstr qstr) {
STATIC void emit_cpy_make_function(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults) {
load_cpy_const_code_and_name(emit, scope->simple_name);
emit_pre(emit, -1 - n_pos_defaults - 2 * n_kw_defaults, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("MAKE_FUNCTION %d\n", (n_kw_defaults << 8) | n_pos_defaults);
}
}
@@ -745,48 +771,52 @@ STATIC void emit_cpy_make_closure(emit_t *emit, scope_t *scope, uint n_closed_ov
emit_cpy_build_tuple(emit, n_closed_over);
load_cpy_const_code_and_name(emit, scope->simple_name);
emit_pre(emit, -2 - n_pos_defaults - 2 * n_kw_defaults, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("MAKE_CLOSURE %d\n", (n_kw_defaults << 8) | n_pos_defaults);
}
}
STATIC void emit_cpy_yield_value(emit_t *emit) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_2) {
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
}
if (emit->pass == PASS_3) {
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
if (emit->pass == MP_PASS_EMIT) {
printf("YIELD_VALUE\n");
}
}
STATIC void emit_cpy_yield_from(emit_t *emit) {
emit_pre(emit, -1, 1);
if (emit->pass == PASS_2) {
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
}
if (emit->pass == PASS_3) {
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
if (emit->pass == MP_PASS_EMIT) {
printf("YIELD_FROM\n");
}
}
STATIC void emit_cpy_start_except_handler(emit_t *emit) {
emit_cpy_adjust_stack_size(emit, 3); // stack adjust for the 3 exception items
}
STATIC void emit_cpy_end_except_handler(emit_t *emit) {
emit_cpy_adjust_stack_size(emit, -5); // stack adjust
}
STATIC void emit_cpy_load_const_verbatim_str(emit_t *emit, const char *str) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_CONST %s\n", str);
}
}
STATIC void emit_cpy_load_closure(emit_t *emit, qstr qstr, int local_num) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_CLOSURE %d %s\n", local_num, qstr_str(qstr));
}
}
STATIC void emit_cpy_setup_loop(emit_t *emit, uint label) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
printf("SETUP_LOOP %d\n", emit->label_offsets[label]);
}
}
@@ -877,6 +907,9 @@ const emit_method_table_t emit_cpython_method_table = {
emit_cpy_yield_value,
emit_cpy_yield_from,
emit_cpy_start_except_handler,
emit_cpy_end_except_handler,
// emitcpy specific functions
emit_cpy_load_const_verbatim_str,
emit_cpy_load_closure,

View File

@@ -1,11 +1,37 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// This code glues the code emitters to the runtime.
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
@@ -23,32 +49,14 @@
#define DEBUG_OP_printf(...) (void)0
#endif
#ifdef WRITE_CODE
FILE *fp_write_code = NULL;
#endif
void mp_emit_glue_init(void) {
#ifdef WRITE_CODE
fp_write_code = fopen("out-code", "wb");
#endif
}
void mp_emit_glue_deinit(void) {
#ifdef WRITE_CODE
if (fp_write_code != NULL) {
fclose(fp_write_code);
}
#endif
}
mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1);
rc->kind = MP_CODE_RESERVED;
return rc;
}
void mp_emit_glue_assign_byte_code(mp_raw_code_t *rc, byte *code, uint len, uint n_pos_args, uint n_kwonly_args, qstr *arg_names, uint scope_flags) {
rc->kind = MP_CODE_BYTE;
void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, byte *code, uint len, uint n_pos_args, uint n_kwonly_args, qstr *arg_names, uint scope_flags) {
rc->kind = MP_CODE_BYTECODE;
rc->scope_flags = scope_flags;
rc->n_pos_args = n_pos_args;
rc->n_kwonly_args = n_kwonly_args;
@@ -63,28 +71,31 @@ void mp_emit_glue_assign_byte_code(mp_raw_code_t *rc, byte *code, uint len, uint
DEBUG_printf(" %s", qstr_str(arg_names[i]));
}
DEBUG_printf("\n");
for (int i = 0; i < 128 && i < len; i++) {
if (i > 0 && i % 16 == 0) {
DEBUG_printf("\n");
}
DEBUG_printf(" %02x", code[i]);
}
DEBUG_printf("\n");
#if MICROPY_DEBUG_PRINTERS
mp_byte_code_print(code, len);
#endif
#if MICROPY_DEBUG_PRINTERS
if (mp_verbose_flag > 0) {
for (int i = 0; i < 128 && i < len; i++) {
if (i > 0 && i % 16 == 0) {
printf("\n");
}
printf(" %02x", code[i]);
}
printf("\n");
mp_bytecode_print(rc, code, len);
}
#endif
}
void mp_emit_glue_assign_native_code(mp_raw_code_t *rc, void *fun, uint len, int n_args) {
rc->kind = MP_CODE_NATIVE;
void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void *fun, uint len, int n_args) {
assert(kind == MP_CODE_NATIVE_PY || kind == MP_CODE_NATIVE_VIPER || kind == MP_CODE_NATIVE_ASM);
rc->kind = kind;
rc->scope_flags = 0;
rc->n_pos_args = n_args;
rc->u_native.fun = fun;
#ifdef DEBUG_PRINT
DEBUG_printf("assign native code: fun=%p len=%u n_args=%d\n", fun, len, n_args);
byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
DEBUG_printf("assign native: kind=%d fun=%p len=%u n_args=%d\n", kind, fun, len, n_args);
byte *fun_data = (byte*)(((mp_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
for (int i = 0; i < 128 && i < len; i++) {
if (i > 0 && i % 16 == 0) {
DEBUG_printf("\n");
@@ -94,36 +105,9 @@ void mp_emit_glue_assign_native_code(mp_raw_code_t *rc, void *fun, uint len, int
DEBUG_printf("\n");
#ifdef WRITE_CODE
if (fp_write_code != NULL) {
fwrite(fun_data, len, 1, fp_write_code);
fflush(fp_write_code);
}
#endif
#endif
}
void mp_emit_glue_assign_inline_asm_code(mp_raw_code_t *rc, void *fun, uint len, int n_args) {
rc->kind = MP_CODE_INLINE_ASM;
rc->scope_flags = 0;
rc->n_pos_args = n_args;
rc->u_inline_asm.fun = fun;
#ifdef DEBUG_PRINT
DEBUG_printf("assign inline asm code: fun=%p len=%u n_args=%d\n", fun, len, n_args);
byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
for (int i = 0; i < 128 && i < len; i++) {
if (i > 0 && i % 16 == 0) {
DEBUG_printf("\n");
}
DEBUG_printf(" %02x", fun_data[i]);
}
DEBUG_printf("\n");
#ifdef WRITE_CODE
if (fp_write_code != NULL) {
fwrite(fun_data, len, 1, fp_write_code);
fflush(fp_write_code);
}
FILE *fp_write_code = fopen("out-code", "wb");
fwrite(fun_data, len, 1, fp_write_code);
fclose(fp_write_code);
#endif
#endif
}
@@ -135,20 +119,21 @@ mp_obj_t mp_make_function_from_raw_code(mp_raw_code_t *rc, mp_obj_t def_args, mp
// def_args must be MP_OBJ_NULL or a tuple
assert(def_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));
// TODO implement default kw args
assert(def_kw_args == MP_OBJ_NULL);
// def_kw_args must be MP_OBJ_NULL or a dict
assert(def_kw_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_kw_args, &mp_type_dict));
// make the function, depending on the raw code kind
mp_obj_t fun;
switch (rc->kind) {
case MP_CODE_BYTE:
fun = mp_obj_new_fun_bc(rc->scope_flags, rc->arg_names, rc->n_pos_args, rc->n_kwonly_args, def_args, rc->u_byte.code);
case MP_CODE_BYTECODE:
fun = mp_obj_new_fun_bc(rc->scope_flags, rc->arg_names, rc->n_pos_args, rc->n_kwonly_args, def_args, def_kw_args, rc->u_byte.code);
break;
case MP_CODE_NATIVE:
case MP_CODE_NATIVE_PY:
fun = mp_make_function_n(rc->n_pos_args, rc->u_native.fun);
break;
case MP_CODE_INLINE_ASM:
fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->u_inline_asm.fun);
case MP_CODE_NATIVE_VIPER:
case MP_CODE_NATIVE_ASM:
fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->u_native.fun);
break;
default:
// raw code was never set (this should not happen)

View File

@@ -1,11 +1,38 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// These variables and functions glue the code emitters to the runtime.
typedef enum {
MP_CODE_UNUSED,
MP_CODE_RESERVED,
MP_CODE_BYTE,
MP_CODE_NATIVE,
MP_CODE_INLINE_ASM,
MP_CODE_BYTECODE,
MP_CODE_NATIVE_PY,
MP_CODE_NATIVE_VIPER,
MP_CODE_NATIVE_ASM,
} mp_raw_code_kind_t;
typedef struct _mp_code_t {
@@ -19,23 +46,16 @@ typedef struct _mp_code_t {
byte *code;
uint len;
} u_byte;
struct {
mp_fun_t fun;
} u_native;
struct {
void *fun;
} u_inline_asm;
} u_native;
};
} mp_raw_code_t;
void mp_emit_glue_init(void);
void mp_emit_glue_deinit(void);
mp_raw_code_t *mp_emit_glue_new_raw_code(void);
void mp_emit_glue_assign_byte_code(mp_raw_code_t *rc, byte *code, uint len, uint n_pos_args, uint n_kwonly_args, qstr *arg_names, uint scope_flags);
void mp_emit_glue_assign_native_code(mp_raw_code_t *rc, void *f, uint len, int n_args);
void mp_emit_glue_assign_inline_asm_code(mp_raw_code_t *rc, void *f, uint len, int n_args);
void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, byte *code, uint len, uint n_pos_args, uint n_kwonly_args, qstr *arg_names, uint scope_flags);
void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void *f, uint len, int n_args);
mp_obj_t mp_make_function_from_raw_code(mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args);
mp_obj_t mp_make_closure_from_raw_code(mp_raw_code_t *rc, uint n_closed_over, const mp_obj_t *args);

View File

@@ -1,11 +1,37 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "lexer.h"
#include "parse.h"
@@ -63,7 +89,7 @@ STATIC void emit_inline_thumb_start_pass(emit_inline_asm_t *emit, pass_kind_t pa
emit->pass = pass;
emit->success = true;
emit->scope = scope;
asm_thumb_start_pass(emit->as, pass);
asm_thumb_start_pass(emit->as, pass == MP_PASS_EMIT ? ASM_THUMB_PASS_EMIT : ASM_THUMB_PASS_COMPUTE);
asm_thumb_entry(emit->as, 0);
}
@@ -71,9 +97,9 @@ STATIC bool emit_inline_thumb_end_pass(emit_inline_asm_t *emit) {
asm_thumb_exit(emit->as);
asm_thumb_end_pass(emit->as);
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
void *f = asm_thumb_get_code(emit->as);
mp_emit_glue_assign_inline_asm_code(emit->scope->raw_code, f, asm_thumb_get_code_size(emit->as), emit->scope->num_pos_args);
mp_emit_glue_assign_native(emit->scope->raw_code, MP_CODE_NATIVE_ASM, f, asm_thumb_get_code_size(emit->as), emit->scope->num_pos_args);
}
return emit->success;
@@ -141,7 +167,7 @@ STATIC uint get_arg_reg(emit_inline_asm_t *emit, const char *op, mp_parse_node_t
if (MP_PARSE_NODE_IS_ID(pn)) {
qstr reg_qstr = MP_PARSE_NODE_LEAF_ARG(pn);
const char *reg_str = qstr_str(reg_qstr);
for (uint i = 0; i < ARRAY_SIZE(reg_name_table); i++) {
for (uint i = 0; i < MP_ARRAY_SIZE(reg_name_table); i++) {
const reg_name_t *r = &reg_name_table[i];
if (reg_str[0] == r->name[0] && reg_str[1] == r->name[1] && reg_str[2] == r->name[2] && (reg_str[2] == '\0' || reg_str[3] == '\0')) {
if (r->reg > max_reg) {
@@ -204,7 +230,7 @@ STATIC int get_arg_label(emit_inline_asm_t *emit, const char *op, mp_parse_node_
}
}
// only need to have the labels on the last pass
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
emit_inline_thumb_error(emit, "label '%s' not defined\n", qstr_str(label_qstr));
}
return 0;
@@ -260,7 +286,7 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, int n_args, m
asm_thumb_b_n(emit->as, label_num);
} else if (op_str[0] == 'b' && op_len == 3) {
uint cc = -1;
for (uint i = 0; i < ARRAY_SIZE(cc_name_table); i++) {
for (uint i = 0; i < MP_ARRAY_SIZE(cc_name_table); i++) {
if (op_str[1] == cc_name_table[i].name[0] && op_str[2] == cc_name_table[i].name[1]) {
cc = cc_name_table[i].cc;
}

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// Essentially normal Python has 1 type: Python objects
// Viper has more than 1 type, and is just a more complicated (a superset of) Python.
// If you declare everything in Viper as a Python object (ie omit type decls) then
@@ -22,8 +48,9 @@
#include <string.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
#include "qstr.h"
#include "lexer.h"
#include "parse.h"
@@ -60,6 +87,7 @@
#define REG_TEMP2 (REG_RSI)
#define ASM_MOV_REG_TO_LOCAL(reg, local_num) asm_x64_mov_r64_to_local(emit->as, (reg), (local_num))
#define ASM_MOV_IMM_TO_REG(imm, reg) asm_x64_mov_i64_to_r64_optimised(emit->as, (imm), (reg))
#define ASM_MOV_ALIGNED_IMM_TO_REG(imm, reg) asm_x64_mov_i64_to_r64_aligned(emit->as, (imm), (reg))
#define ASM_MOV_IMM_TO_LOCAL_USING(imm, local_num, reg_temp) do { asm_x64_mov_i64_to_r64_optimised(emit->as, (imm), (reg_temp)); asm_x64_mov_r64_to_local(emit->as, (reg_temp), (local_num)); } while (false)
#define ASM_MOV_LOCAL_TO_REG(local_num, reg) asm_x64_mov_local_to_r64(emit->as, (local_num), (reg))
#define ASM_MOV_REG_TO_REG(reg_src, reg_dest) asm_x64_mov_r64_to_r64(emit->as, (reg_src), (reg_dest))
@@ -83,6 +111,7 @@
#define REG_TEMP2 (REG_R2)
#define ASM_MOV_REG_TO_LOCAL(reg, local_num) asm_thumb_mov_local_reg(emit->as, (local_num), (reg))
#define ASM_MOV_IMM_TO_REG(imm, reg) asm_thumb_mov_reg_i32_optimised(emit->as, (reg), (imm))
#define ASM_MOV_ALIGNED_IMM_TO_REG(imm, reg) asm_thumb_mov_reg_i32_aligned(emit->as, (reg), (imm))
#define ASM_MOV_IMM_TO_LOCAL_USING(imm, local_num, reg_temp) do { asm_thumb_mov_reg_i32_optimised(emit->as, (reg_temp), (imm)); asm_thumb_mov_local_reg(emit->as, (local_num), (reg_temp)); } while (false)
#define ASM_MOV_LOCAL_TO_REG(local_num, reg) asm_thumb_mov_reg_local(emit->as, (reg), (local_num))
#define ASM_MOV_REG_TO_REG(reg_src, reg_dest) asm_thumb_mov_reg_reg(emit->as, (reg_dest), (reg_src))
@@ -111,7 +140,7 @@ typedef struct _stack_info_t {
stack_info_kind_t kind;
union {
int u_reg;
machine_int_t u_imm;
mp_int_t u_imm;
};
} stack_info_t;
@@ -120,10 +149,10 @@ struct _emit_t {
bool do_viper_types;
int local_vtype_alloc;
uint local_vtype_alloc;
vtype_kind_t *local_vtype;
int stack_info_alloc;
uint stack_info_alloc;
stack_info_t *stack_info;
int stack_start;
@@ -141,10 +170,7 @@ struct _emit_t {
};
emit_t *EXPORT_FUN(new)(uint max_num_labels) {
emit_t *emit = m_new(emit_t, 1);
emit->do_viper_types = false;
emit->local_vtype = NULL;
emit->stack_info = NULL;
emit_t *emit = m_new0(emit_t, 1);
#if N_X64
emit->as = asm_x64_new(max_num_labels);
#elif N_THUMB
@@ -159,6 +185,8 @@ void EXPORT_FUN(free)(emit_t *emit) {
#elif N_THUMB
asm_thumb_free(emit->as, false);
#endif
m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
m_del_obj(emit_t, emit);
}
@@ -173,12 +201,16 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
emit->last_emit_was_return_value = false;
emit->scope = scope;
if (emit->local_vtype == NULL) {
emit->local_vtype_alloc = scope->num_locals + 20; // XXX should be maximum over all scopes
emit->local_vtype = m_new(vtype_kind_t, emit->local_vtype_alloc);
// allocate memory for keeping track of the types of locals
if (emit->local_vtype_alloc < scope->num_locals) {
emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
emit->local_vtype_alloc = scope->num_locals;
}
// allocate memory for keeping track of the objects on the stack
// XXX don't know stack size on entry, and it should be maximum over all scopes
if (emit->stack_info == NULL) {
emit->stack_info_alloc = scope->stack_size + 50; // XXX don't know stack size on entry, should be maximum over all scopes
emit->stack_info_alloc = scope->stack_size + 50;
emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
}
@@ -202,14 +234,14 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
}
#if N_X64
asm_x64_start_pass(emit->as, pass);
asm_x64_start_pass(emit->as, pass == MP_PASS_EMIT ? ASM_X64_PASS_EMIT : ASM_X64_PASS_COMPUTE);
#elif N_THUMB
asm_thumb_start_pass(emit->as, pass);
asm_thumb_start_pass(emit->as, pass == MP_PASS_EMIT ? ASM_THUMB_PASS_EMIT : ASM_THUMB_PASS_COMPUTE);
#endif
// entry to function
int num_locals = 0;
if (pass > PASS_1) {
if (pass > MP_PASS_SCOPE) {
num_locals = scope->num_locals - REG_LOCAL_NUM;
if (num_locals < 0) {
num_locals = 0;
@@ -217,11 +249,6 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
emit->stack_start = num_locals;
num_locals += scope->stack_size;
}
if (pass == PASS_2) {
// XXX big hack to make sure we have some locals in PASS_2
// this is so that on PASS_2 the code emitted in x64 has the right size
num_locals += 2;
}
#if N_X64
asm_x64_entry(emit->as, num_locals);
#elif N_THUMB
@@ -234,9 +261,9 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
if (i == 0) {
asm_x64_mov_r64_to_r64(emit->as, REG_ARG_1, REG_LOCAL_1);
} else if (i == 1) {
asm_x64_mov_r64_to_local(emit->as, REG_ARG_2, i - 1);
asm_x64_mov_r64_to_local(emit->as, REG_ARG_2, i - REG_LOCAL_NUM);
} else if (i == 2) {
asm_x64_mov_r64_to_local(emit->as, REG_ARG_3, i - 1);
asm_x64_mov_r64_to_local(emit->as, REG_ARG_3, i - REG_LOCAL_NUM);
} else {
// TODO not implemented
assert(0);
@@ -258,7 +285,7 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
}
}
asm_thumb_mov_reg_i32(emit->as, REG_R7, (machine_uint_t)mp_fun_table);
asm_thumb_mov_reg_i32(emit->as, REG_R7, (mp_uint_t)mp_fun_table);
#endif
}
@@ -280,13 +307,13 @@ STATIC void emit_native_end_pass(emit_t *emit) {
printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
}
if (emit->pass == PASS_3) {
if (emit->pass == MP_PASS_EMIT) {
#if N_X64
void *f = asm_x64_get_code(emit->as);
mp_emit_glue_assign_native_code(emit->scope->raw_code, f, asm_x64_get_code_size(emit->as), emit->scope->num_pos_args);
mp_emit_glue_assign_native(emit->scope->raw_code, emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY, f, asm_x64_get_code_size(emit->as), emit->scope->num_pos_args);
#elif N_THUMB
void *f = asm_thumb_get_code(emit->as);
mp_emit_glue_assign_native_code(emit->scope->raw_code, f, asm_thumb_get_code_size(emit->as), emit->scope->num_pos_args);
mp_emit_glue_assign_native(emit->scope->raw_code, emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY, f, asm_thumb_get_code_size(emit->as), emit->scope->num_pos_args);
#endif
}
}
@@ -306,7 +333,7 @@ STATIC void adjust_stack(emit_t *emit, int stack_size_delta) {
DEBUG_printf("adjust stack: stack:%d + delta:%d\n", emit->stack_size, stack_size_delta);
assert((int)emit->stack_size + stack_size_delta >= 0);
emit->stack_size += stack_size_delta;
if (emit->pass > PASS_1 && emit->stack_size > emit->scope->stack_size) {
if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
emit->scope->stack_size = emit->stack_size;
}
}
@@ -412,6 +439,11 @@ STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int re
}
}
STATIC void emit_pre_pop_discard(emit_t *emit, vtype_kind_t *vtype) {
emit->last_emit_was_return_value = false;
adjust_stack(emit, -1);
}
STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
emit->last_emit_was_return_value = false;
emit_access_stack(emit, 1, vtype, reg_dest);
@@ -440,7 +472,7 @@ STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
adjust_stack(emit, 1);
}
STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, machine_int_t imm) {
STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
stack_info_t *si = &emit->stack_info[emit->stack_size];
si->vtype = vtype;
si->kind = STACK_IMM;
@@ -484,9 +516,9 @@ STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, int reg_dest, in
case VTYPE_BOOL:
si->vtype = VTYPE_PYOBJ;
if (si->u_imm == 0) {
ASM_MOV_IMM_TO_LOCAL_USING((machine_uint_t)mp_const_false, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
ASM_MOV_IMM_TO_LOCAL_USING((mp_uint_t)mp_const_false, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
} else {
ASM_MOV_IMM_TO_LOCAL_USING((machine_uint_t)mp_const_true, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
ASM_MOV_IMM_TO_LOCAL_USING((mp_uint_t)mp_const_true, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
}
break;
case VTYPE_INT:
@@ -525,7 +557,7 @@ STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind, void *fun) {
#endif
}
STATIC void emit_call_with_imm_arg(emit_t *emit, mp_fun_kind_t fun_kind, void *fun, machine_int_t arg_val, int arg_reg) {
STATIC void emit_call_with_imm_arg(emit_t *emit, mp_fun_kind_t fun_kind, void *fun, mp_int_t arg_val, int arg_reg) {
need_reg_all(emit);
ASM_MOV_IMM_TO_REG(arg_val, arg_reg);
#if N_X64
@@ -535,7 +567,18 @@ STATIC void emit_call_with_imm_arg(emit_t *emit, mp_fun_kind_t fun_kind, void *f
#endif
}
STATIC void emit_call_with_2_imm_args(emit_t *emit, mp_fun_kind_t fun_kind, void *fun, machine_int_t arg_val1, int arg_reg1, machine_int_t arg_val2, int arg_reg2) {
// the first arg is stored in the code aligned on a mp_uint_t boundary
STATIC void emit_call_with_imm_arg_aligned(emit_t *emit, mp_fun_kind_t fun_kind, void *fun, mp_int_t arg_val, int arg_reg) {
need_reg_all(emit);
ASM_MOV_ALIGNED_IMM_TO_REG(arg_val, arg_reg);
#if N_X64
asm_x64_call_ind(emit->as, fun, REG_RAX);
#elif N_THUMB
asm_thumb_bl_ind(emit->as, mp_fun_table[fun_kind], fun_kind, REG_R3);
#endif
}
STATIC void emit_call_with_2_imm_args(emit_t *emit, mp_fun_kind_t fun_kind, void *fun, mp_int_t arg_val1, int arg_reg1, mp_int_t arg_val2, int arg_reg2) {
need_reg_all(emit);
ASM_MOV_IMM_TO_REG(arg_val1, arg_reg1);
ASM_MOV_IMM_TO_REG(arg_val2, arg_reg2);
@@ -546,9 +589,10 @@ STATIC void emit_call_with_2_imm_args(emit_t *emit, mp_fun_kind_t fun_kind, void
#endif
}
STATIC void emit_call_with_3_imm_args(emit_t *emit, mp_fun_kind_t fun_kind, void *fun, machine_int_t arg_val1, int arg_reg1, machine_int_t arg_val2, int arg_reg2, machine_int_t arg_val3, int arg_reg3) {
// the first arg is stored in the code aligned on a mp_uint_t boundary
STATIC void emit_call_with_3_imm_args_and_first_aligned(emit_t *emit, mp_fun_kind_t fun_kind, void *fun, mp_int_t arg_val1, int arg_reg1, mp_int_t arg_val2, int arg_reg2, mp_int_t arg_val3, int arg_reg3) {
need_reg_all(emit);
ASM_MOV_IMM_TO_REG(arg_val1, arg_reg1);
ASM_MOV_ALIGNED_IMM_TO_REG(arg_val1, arg_reg1);
ASM_MOV_IMM_TO_REG(arg_val2, arg_reg2);
ASM_MOV_IMM_TO_REG(arg_val3, arg_reg3);
#if N_X64
@@ -627,7 +671,7 @@ STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
DEBUG_printf("load_const_tok %d\n", tok);
emit_native_pre(emit);
int vtype;
machine_uint_t val;
mp_uint_t val;
if (emit->do_viper_types) {
switch (tok) {
case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
@@ -638,16 +682,16 @@ STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
} else {
vtype = VTYPE_PYOBJ;
switch (tok) {
case MP_TOKEN_KW_NONE: val = (machine_uint_t)mp_const_none; break;
case MP_TOKEN_KW_FALSE: val = (machine_uint_t)mp_const_false; break;
case MP_TOKEN_KW_TRUE: val = (machine_uint_t)mp_const_true; break;
case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
default: assert(0); vtype = 0; val = 0; // shouldn't happen
}
}
emit_post_push_imm(emit, vtype, val);
}
STATIC void emit_native_load_const_small_int(emit_t *emit, machine_int_t arg) {
STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
DEBUG_printf("load_const_small_int %d\n", arg);
emit_native_pre(emit);
if (emit->do_viper_types) {
@@ -661,7 +705,7 @@ STATIC void emit_native_load_const_int(emit_t *emit, qstr qst) {
DEBUG_printf("load_const_int %s\n", qstr_str(st));
// for viper: load integer, check fits in 32 bits
emit_native_pre(emit);
emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_INT, mp_obj_new_int_from_long_str, qst, REG_ARG_1);
emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_INT, mp_load_const_int, qst, REG_ARG_1);
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
}
@@ -678,9 +722,13 @@ STATIC void emit_native_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
// not implemented properly
// load a pointer to the asciiz string?
assert(0);
emit_post_push_imm(emit, VTYPE_PTR, (machine_uint_t)qstr_str(qstr));
emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qstr));
} else {
emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, mp_load_const_str, qstr, REG_ARG_1);
if (bytes) {
emit_call_with_imm_arg(emit, 0, mp_load_const_bytes, qstr, REG_ARG_1); // TODO need to add function to runtime table
} else {
emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, mp_load_const_str, qstr, REG_ARG_1);
}
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
}
}
@@ -701,7 +749,7 @@ STATIC void emit_native_load_fast(emit_t *emit, qstr qstr, uint id_flags, int lo
emit_post_push_reg(emit, vtype, REG_LOCAL_1);
} else {
need_reg_single(emit, REG_RAX, 0);
asm_x64_mov_local_to_r64(emit->as, local_num - 1, REG_RAX);
asm_x64_mov_local_to_r64(emit->as, local_num - REG_LOCAL_NUM, REG_RAX);
emit_post_push_reg(emit, vtype, REG_RAX);
}
#elif N_THUMB
@@ -713,7 +761,7 @@ STATIC void emit_native_load_fast(emit_t *emit, qstr qstr, uint id_flags, int lo
emit_post_push_reg(emit, vtype, REG_LOCAL_3);
} else {
need_reg_single(emit, REG_R0, 0);
asm_thumb_mov_reg_local(emit->as, REG_R0, local_num - 1);
asm_thumb_mov_reg_local(emit->as, REG_R0, local_num - REG_LOCAL_NUM);
emit_post_push_reg(emit, vtype, REG_R0);
}
#endif
@@ -767,7 +815,7 @@ STATIC void emit_native_load_subscr(emit_t *emit) {
vtype_kind_t vtype_lhs, vtype_rhs;
emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_2, &vtype_lhs, REG_ARG_1);
if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, mp_obj_subscr, (machine_uint_t)MP_OBJ_SENTINEL, REG_ARG_3);
emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, mp_obj_subscr, (mp_uint_t)MP_OBJ_SENTINEL, REG_ARG_3);
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
} else {
printf("ViperTypeError: can't do subscr of types %d and %d\n", vtype_lhs, vtype_rhs);
@@ -782,7 +830,7 @@ STATIC void emit_native_store_fast(emit_t *emit, qstr qstr, int local_num) {
emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
} else {
emit_pre_pop_reg(emit, &vtype, REG_RAX);
asm_x64_mov_r64_to_local(emit->as, REG_RAX, local_num - 1);
asm_x64_mov_r64_to_local(emit->as, REG_RAX, local_num - REG_LOCAL_NUM);
}
#elif N_THUMB
if (local_num == 0) {
@@ -793,7 +841,7 @@ STATIC void emit_native_store_fast(emit_t *emit, qstr qstr, int local_num) {
emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
} else {
emit_pre_pop_reg(emit, &vtype, REG_R0);
asm_thumb_mov_local_reg(emit->as, local_num - 1, REG_R0);
asm_thumb_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_R0);
}
#endif
@@ -874,8 +922,11 @@ STATIC void emit_native_delete_global(emit_t *emit, qstr qstr) {
}
STATIC void emit_native_delete_attr(emit_t *emit, qstr qstr) {
// not supported
assert(0);
vtype_kind_t vtype_base;
emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
assert(vtype_base == VTYPE_PYOBJ);
emit_call_with_2_imm_args(emit, MP_F_STORE_ATTR, mp_store_attr, qstr, REG_ARG_2, (mp_uint_t)MP_OBJ_NULL, REG_ARG_3); // arg2 = attribute name, arg3 = value (null for delete)
emit_post(emit);
}
STATIC void emit_native_delete_subscr(emit_t *emit) {
@@ -883,7 +934,7 @@ STATIC void emit_native_delete_subscr(emit_t *emit) {
emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
assert(vtype_index == VTYPE_PYOBJ);
assert(vtype_base == VTYPE_PYOBJ);
emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, mp_obj_subscr, (machine_uint_t)MP_OBJ_NULL, REG_ARG_3);
emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, mp_obj_subscr, (mp_uint_t)MP_OBJ_NULL, REG_ARG_3);
}
STATIC void emit_native_dup_top(emit_t *emit) {
@@ -900,7 +951,7 @@ STATIC void emit_native_dup_top_two(emit_t *emit) {
STATIC void emit_native_pop_top(emit_t *emit) {
vtype_kind_t vtype;
emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
emit_pre_pop_discard(emit, &vtype);
emit_post(emit);
}
@@ -918,6 +969,8 @@ STATIC void emit_native_rot_three(emit_t *emit) {
STATIC void emit_native_jump(emit_t *emit, uint label) {
emit_native_pre(emit);
// need to commit stack because we are jumping elsewhere
need_stack_settled(emit);
#if N_X64
asm_x64_jmp_label(emit->as, label);
#elif N_THUMB
@@ -926,33 +979,29 @@ STATIC void emit_native_jump(emit_t *emit, uint label) {
emit_post(emit);
}
STATIC void emit_native_pop_jump_pre_helper(emit_t *emit, uint label) {
STATIC void emit_native_jump_helper(emit_t *emit, uint label, bool pop) {
vtype_kind_t vtype = peek_vtype(emit);
if (vtype == VTYPE_BOOL) {
emit_pre_pop_reg(emit, &vtype, REG_RET);
if (!pop) {
adjust_stack(emit, 1);
}
} else if (vtype == VTYPE_PYOBJ) {
emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
emit_call(emit, MP_F_OBJ_IS_TRUE, mp_obj_is_true);
if (!pop) {
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
}
} else {
printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
assert(0);
}
}
STATIC void emit_native_pop_jump_if_false(emit_t *emit, uint label) {
emit_native_pop_jump_pre_helper(emit, label);
#if N_X64
asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
asm_x64_jcc_label(emit->as, JCC_JZ, label);
#elif N_THUMB
asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0);
asm_thumb_bcc_label(emit->as, THUMB_CC_EQ, label);
#endif
emit_post(emit);
// need to commit stack because we may jump elsewhere
need_stack_settled(emit);
}
STATIC void emit_native_pop_jump_if_true(emit_t *emit, uint label) {
emit_native_pop_jump_pre_helper(emit, label);
emit_native_jump_helper(emit, label, true);
#if N_X64
asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
asm_x64_jcc_label(emit->as, JCC_JNZ, label);
@@ -963,34 +1012,83 @@ STATIC void emit_native_pop_jump_if_true(emit_t *emit, uint label) {
emit_post(emit);
}
STATIC void emit_native_jump_if_true_or_pop(emit_t *emit, uint label) {
assert(0);
STATIC void emit_native_pop_jump_if_false(emit_t *emit, uint label) {
emit_native_jump_helper(emit, label, true);
#if N_X64
asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
asm_x64_jcc_label(emit->as, JCC_JZ, label);
#elif N_THUMB
asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0);
asm_thumb_bcc_label(emit->as, THUMB_CC_EQ, label);
#endif
emit_post(emit);
}
STATIC void emit_native_jump_if_true_or_pop(emit_t *emit, uint label) {
emit_native_jump_helper(emit, label, false);
#if N_X64
asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
asm_x64_jcc_label(emit->as, JCC_JNZ, label);
#elif N_THUMB
asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0);
asm_thumb_bcc_label(emit->as, THUMB_CC_NE, label);
#endif
adjust_stack(emit, -1);
emit_post(emit);
}
STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, uint label) {
assert(0);
emit_native_jump_helper(emit, label, false);
#if N_X64
asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
asm_x64_jcc_label(emit->as, JCC_JZ, label);
#elif N_THUMB
asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0);
asm_thumb_bcc_label(emit->as, THUMB_CC_EQ, label);
#endif
adjust_stack(emit, -1);
emit_post(emit);
}
STATIC void emit_native_break_loop(emit_t *emit, uint label, int except_depth) {
emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
}
STATIC void emit_native_continue_loop(emit_t *emit, uint label, int except_depth) {
emit_native_jump(emit, label); // TODO properly
}
STATIC void emit_native_continue_loop(emit_t *emit, uint label, int except_depth) {
assert(0);
}
STATIC void emit_native_setup_with(emit_t *emit, uint label) {
// not supported, or could be with runtime call
assert(0);
}
STATIC void emit_native_with_cleanup(emit_t *emit) {
assert(0);
}
STATIC void emit_native_setup_except(emit_t *emit, uint label) {
assert(0);
emit_native_pre(emit);
// need to commit stack because we may jump elsewhere
need_stack_settled(emit);
emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_1, sizeof(nlr_buf_t) / sizeof(mp_uint_t)); // arg1 = pointer to nlr buf
emit_call(emit, 0, nlr_push); // TODO need to add function to runtime table
#if N_X64
asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
asm_x64_jcc_label(emit->as, JCC_JNZ, label);
#elif N_THUMB
asm_thumb_cmp_rlo_i8(emit->as, REG_RET, 0);
asm_thumb_bcc_label(emit->as, THUMB_CC_NE, label);
#endif
emit_post(emit);
}
STATIC void emit_native_setup_finally(emit_t *emit, uint label) {
assert(0);
}
STATIC void emit_native_end_finally(emit_t *emit) {
assert(0);
//assert(0);
}
STATIC void emit_native_get_iter(emit_t *emit) {
@@ -1010,7 +1108,7 @@ STATIC void emit_native_for_iter(emit_t *emit, uint label) {
emit_access_stack(emit, 1, &vtype, REG_ARG_1);
assert(vtype == VTYPE_PYOBJ);
emit_call(emit, MP_F_ITERNEXT, mp_iternext);
ASM_MOV_IMM_TO_REG((machine_uint_t)MP_OBJ_NULL, REG_TEMP1);
ASM_MOV_IMM_TO_REG((mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
#if N_X64
asm_x64_cmp_r64_with_r64(emit->as, REG_RET, REG_TEMP1);
asm_x64_jcc_label(emit->as, JCC_JE, label);
@@ -1030,19 +1128,31 @@ STATIC void emit_native_for_iter_end(emit_t *emit) {
STATIC void emit_native_pop_block(emit_t *emit) {
emit_native_pre(emit);
emit_call(emit, 0, nlr_pop); // TODO need to add function to runtime table
adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
emit_post(emit);
}
STATIC void emit_native_pop_except(emit_t *emit) {
assert(0);
/*
emit_native_pre(emit);
emit_call(emit, 0, nlr_pop); // TODO need to add function to runtime table
adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
emit_post(emit);
*/
}
STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
vtype_kind_t vtype;
emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
assert(vtype == VTYPE_PYOBJ);
emit_call_with_imm_arg(emit, MP_F_UNARY_OP, mp_unary_op, op, REG_ARG_1);
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
if (op == MP_UNARY_OP_NOT) {
// we need to synthesise this operation
assert(0);
} else {
vtype_kind_t vtype;
emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
assert(vtype == VTYPE_PYOBJ);
emit_call_with_imm_arg(emit, MP_F_UNARY_OP, mp_unary_op, op, REG_ARG_1);
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
}
}
STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
@@ -1156,17 +1266,26 @@ STATIC void emit_native_set_add(emit_t *emit, int set_index) {
STATIC void emit_native_build_slice(emit_t *emit, int n_args) {
DEBUG_printf("build_slice %d\n", n_args);
assert(n_args == 2);
vtype_kind_t vtype_start, vtype_stop;
emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
assert(vtype_start == VTYPE_PYOBJ);
assert(vtype_stop == VTYPE_PYOBJ);
emit_call_with_imm_arg(emit, MP_F_NEW_SLICE, mp_obj_new_slice, (machine_uint_t)MP_OBJ_NULL, REG_ARG_3); // arg3 = step
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
if (n_args == 2) {
vtype_kind_t vtype_start, vtype_stop;
emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
assert(vtype_start == VTYPE_PYOBJ);
assert(vtype_stop == VTYPE_PYOBJ);
emit_call_with_imm_arg(emit, MP_F_NEW_SLICE, mp_obj_new_slice, (mp_uint_t)mp_const_none, REG_ARG_3); // arg3 = step
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
} else {
assert(n_args == 3);
vtype_kind_t vtype_start, vtype_stop, vtype_step;
emit_pre_pop_reg_reg_reg(emit, &vtype_step, REG_ARG_3, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop, arg3 = step
assert(vtype_start == VTYPE_PYOBJ);
assert(vtype_stop == VTYPE_PYOBJ);
assert(vtype_step == VTYPE_PYOBJ);
emit_call(emit, MP_F_NEW_SLICE, mp_obj_new_slice);
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
}
}
STATIC void emit_native_unpack_sequence(emit_t *emit, int n_args) {
// TODO this is untested
DEBUG_printf("unpack_sequence %d\n", n_args);
vtype_kind_t vtype_base;
emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
@@ -1176,16 +1295,26 @@ STATIC void emit_native_unpack_sequence(emit_t *emit, int n_args) {
}
STATIC void emit_native_unpack_ex(emit_t *emit, int n_left, int n_right) {
assert(0);
DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
vtype_kind_t vtype_base;
emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
assert(vtype_base == VTYPE_PYOBJ);
emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_left + n_right + 1); // arg3 = dest ptr
emit_call_with_imm_arg(emit, MP_F_UNPACK_EX, mp_unpack_ex, n_left | (n_right << 8), REG_ARG_2); // arg2 = n_left + n_right
}
STATIC void emit_native_make_function(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults) {
// call runtime, with type info for args, or don't support dict/default params, or only support Python objects for them
assert(n_pos_defaults == 0 && n_kw_defaults == 0);
emit_native_pre(emit);
assert(0);
// TODO we need to store the raw_code ptr aligned within the code for the GC
emit_call_with_3_imm_args(emit, MP_F_MAKE_FUNCTION_FROM_RAW_CODE, mp_make_function_from_raw_code, (machine_uint_t)scope->raw_code, REG_ARG_1, (machine_uint_t)MP_OBJ_NULL, REG_ARG_2, (machine_uint_t)MP_OBJ_NULL, REG_ARG_3);
if (n_pos_defaults == 0 && n_kw_defaults == 0) {
emit_call_with_3_imm_args_and_first_aligned(emit, MP_F_MAKE_FUNCTION_FROM_RAW_CODE, mp_make_function_from_raw_code, (mp_uint_t)scope->raw_code, REG_ARG_1, (mp_uint_t)MP_OBJ_NULL, REG_ARG_2, (mp_uint_t)MP_OBJ_NULL, REG_ARG_3);
} else {
vtype_kind_t vtype_def_tuple, vtype_def_dict;
emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
assert(vtype_def_tuple == VTYPE_PYOBJ);
assert(vtype_def_dict == VTYPE_PYOBJ);
emit_call_with_imm_arg_aligned(emit, MP_F_MAKE_FUNCTION_FROM_RAW_CODE, mp_make_function_from_raw_code, (mp_uint_t)scope->raw_code, REG_ARG_1);
}
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
}
@@ -1280,9 +1409,16 @@ STATIC void emit_native_return_value(emit_t *emit) {
}
STATIC void emit_native_raise_varargs(emit_t *emit, int n_args) {
// call runtime
assert(0);
assert(n_args == 1);
vtype_kind_t vtype_err;
emit_pre_pop_reg(emit, &vtype_err, REG_ARG_1); // arg1 = object to raise
assert(vtype_err == VTYPE_PYOBJ);
emit_call(emit, 0, mp_make_raise_obj); // TODO need to add function to runtime table
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
emit_pre_pop_reg(emit, &vtype_err, REG_ARG_1);
emit_call(emit, 0, nlr_jump); // TODO need to add function to runtime table
}
STATIC void emit_native_yield_value(emit_t *emit) {
// not supported (for now)
assert(0);
@@ -1292,6 +1428,21 @@ STATIC void emit_native_yield_from(emit_t *emit) {
assert(0);
}
STATIC void emit_native_start_except_handler(emit_t *emit) {
// This instruction follows an nlr_pop, so the stack counter is back to zero, when really
// it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
// the first 2 elements, so we can get the thrown value.
adjust_stack(emit, 2);
vtype_kind_t vtype_nlr;
emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
emit_pre_pop_discard(emit, &vtype_nlr); // discard the linked-list pointer in the nlr_buf
emit_post_push_reg_reg_reg(emit, VTYPE_PYOBJ, REG_ARG_1, VTYPE_PYOBJ, REG_ARG_1, VTYPE_PYOBJ, REG_ARG_1); // push the 3 exception items
}
STATIC void emit_native_end_except_handler(emit_t *emit) {
adjust_stack(emit, -3); // stack adjust (not sure why it's this much...)
}
const emit_method_table_t EXPORT_FUN(method_table) = {
emit_native_set_viper_types,
emit_native_start_pass,
@@ -1377,6 +1528,9 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
emit_native_raise_varargs,
emit_native_yield_value,
emit_native_yield_from,
emit_native_start_except_handler,
emit_native_end_except_handler,
};
#endif // (MICROPY_EMIT_X64 && N_X64) || (MICROPY_EMIT_THUMB && N_THUMB)

View File

@@ -1,9 +1,35 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "lexer.h"
#include "parse.h"
@@ -30,7 +56,7 @@ STATIC void emit_pass1_dummy(emit_t *emit) {
}
STATIC void emit_pass1_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
assert(pass == PASS_1);
assert(pass == MP_PASS_SCOPE);
emit->scope = scope;
}
@@ -188,6 +214,10 @@ const emit_method_table_t emit_pass1_method_table = {
(void*)emit_pass1_dummy,
(void*)emit_pass1_dummy,
(void*)emit_pass1_dummy,
(void*)emit_pass1_dummy,
(void*)emit_pass1_dummy,
#if MICROPY_EMIT_CPYTHON
(void*)emit_pass1_dummy,
(void*)emit_pass1_dummy,

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/***********************************************************************
formatfloat.c - Ruutine for converting a single-precision floating

View File

@@ -1 +1,27 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
int format_float(float f, char *buf, size_t bufSize, char fmt, int prec, char sign);

192
py/gc.c
View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
@@ -7,7 +33,6 @@
#include "misc.h"
#include "gc.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
@@ -26,17 +51,17 @@
#define STACK_SIZE (64) // tunable; minimum is 1
STATIC byte *gc_alloc_table_start;
STATIC machine_uint_t gc_alloc_table_byte_len;
STATIC mp_uint_t gc_alloc_table_byte_len;
#if MICROPY_ENABLE_FINALISER
STATIC byte *gc_finaliser_table_start;
#endif
STATIC machine_uint_t *gc_pool_start;
STATIC machine_uint_t *gc_pool_end;
STATIC mp_uint_t *gc_pool_start;
STATIC mp_uint_t *gc_pool_end;
STATIC int gc_stack_overflow;
STATIC machine_uint_t gc_stack[STACK_SIZE];
STATIC machine_uint_t *gc_sp;
STATIC machine_uint_t gc_lock_depth;
STATIC mp_uint_t gc_stack[STACK_SIZE];
STATIC mp_uint_t *gc_sp;
STATIC mp_uint_t gc_lock_depth;
// ATB = allocation table byte
// 0b00 = FREE -- free block
@@ -68,8 +93,8 @@ STATIC machine_uint_t gc_lock_depth;
#define ATB_HEAD_TO_MARK(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_MARK << BLOCK_SHIFT(block)); } while (0)
#define ATB_MARK_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_TAIL << BLOCK_SHIFT(block))); } while (0)
#define BLOCK_FROM_PTR(ptr) (((ptr) - (machine_uint_t)gc_pool_start) / BYTES_PER_BLOCK)
#define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (machine_uint_t)gc_pool_start))
#define BLOCK_FROM_PTR(ptr) (((ptr) - (mp_uint_t)gc_pool_start) / BYTES_PER_BLOCK)
#define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (mp_uint_t)gc_pool_start))
#define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB)
#if MICROPY_ENABLE_FINALISER
@@ -86,15 +111,15 @@ STATIC machine_uint_t gc_lock_depth;
// TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool
void gc_init(void *start, void *end) {
// align end pointer on block boundary
end = (void*)((machine_uint_t)end & (~(BYTES_PER_BLOCK - 1)));
DEBUG_printf("Initializing GC heap: %p..%p = %ld bytes\n", start, end, end - start);
end = (void*)((mp_uint_t)end & (~(BYTES_PER_BLOCK - 1)));
DEBUG_printf("Initializing GC heap: %p..%p = " UINT_FMT " bytes\n", start, end, (byte*)end - (byte*)start);
// calculate parameters for GC (T=total, A=alloc table, F=finaliser table, P=pool; all in bytes):
// T = A + F + P
// F = A * BLOCKS_PER_ATB / BLOCKS_PER_FTB
// P = A * BLOCKS_PER_ATB * BYTES_PER_BLOCK
// => T = A * (1 + BLOCKS_PER_ATB / BLOCKS_PER_FTB + BLOCKS_PER_ATB * BYTES_PER_BLOCK)
machine_uint_t total_byte_len = end - start;
mp_uint_t total_byte_len = (byte*)end - (byte*)start;
#if MICROPY_ENABLE_FINALISER
gc_alloc_table_byte_len = total_byte_len * BITS_PER_BYTE / (BITS_PER_BYTE + BITS_PER_BYTE * BLOCKS_PER_ATB / BLOCKS_PER_FTB + BITS_PER_BYTE * BLOCKS_PER_ATB * BYTES_PER_BLOCK);
#else
@@ -105,13 +130,13 @@ void gc_init(void *start, void *end) {
gc_alloc_table_start = (byte*)start;
#if MICROPY_ENABLE_FINALISER
machine_uint_t gc_finaliser_table_byte_len = (gc_alloc_table_byte_len * BLOCKS_PER_ATB) / BLOCKS_PER_FTB;
mp_uint_t gc_finaliser_table_byte_len = (gc_alloc_table_byte_len * BLOCKS_PER_ATB) / BLOCKS_PER_FTB;
gc_finaliser_table_start = gc_alloc_table_start + gc_alloc_table_byte_len;
#endif
machine_uint_t gc_pool_block_len = gc_alloc_table_byte_len * BLOCKS_PER_ATB;
gc_pool_start = end - gc_pool_block_len * BYTES_PER_BLOCK;
gc_pool_end = end;
mp_uint_t gc_pool_block_len = gc_alloc_table_byte_len * BLOCKS_PER_ATB;
gc_pool_start = (mp_uint_t*)((byte*)end - gc_pool_block_len * BYTES_PER_BLOCK);
gc_pool_end = (mp_uint_t*)end;
// clear ATBs
memset(gc_alloc_table_start, 0, gc_alloc_table_byte_len);
@@ -147,16 +172,20 @@ void gc_unlock(void) {
gc_lock_depth--;
}
bool gc_is_locked(void) {
return gc_lock_depth != 0;
}
#define VERIFY_PTR(ptr) ( \
(ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
&& ptr >= (machine_uint_t)gc_pool_start /* must be above start of pool */ \
&& ptr < (machine_uint_t)gc_pool_end /* must be below end of pool */ \
&& ptr >= (mp_uint_t)gc_pool_start /* must be above start of pool */ \
&& ptr < (mp_uint_t)gc_pool_end /* must be below end of pool */ \
)
#define VERIFY_MARK_AND_PUSH(ptr) \
do { \
if (VERIFY_PTR(ptr)) { \
machine_uint_t _block = BLOCK_FROM_PTR(ptr); \
mp_uint_t _block = BLOCK_FROM_PTR(ptr); \
if (ATB_GET_KIND(_block) == AT_HEAD) { \
/* an unmarked head, mark it, and push it on gc stack */ \
ATB_HEAD_TO_MARK(_block); \
@@ -172,18 +201,18 @@ void gc_unlock(void) {
STATIC void gc_drain_stack(void) {
while (gc_sp > gc_stack) {
// pop the next block off the stack
machine_uint_t block = *--gc_sp;
mp_uint_t block = *--gc_sp;
// work out number of consecutive blocks in the chain starting with this one
machine_uint_t n_blocks = 0;
mp_uint_t n_blocks = 0;
do {
n_blocks += 1;
} while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
// check this block's children
machine_uint_t *scan = (machine_uint_t*)PTR_FROM_BLOCK(block);
for (machine_uint_t i = n_blocks * WORDS_PER_BLOCK; i > 0; i--, scan++) {
machine_uint_t ptr2 = *scan;
mp_uint_t *scan = (mp_uint_t*)PTR_FROM_BLOCK(block);
for (mp_uint_t i = n_blocks * WORDS_PER_BLOCK; i > 0; i--, scan++) {
mp_uint_t ptr2 = *scan;
VERIFY_MARK_AND_PUSH(ptr2);
}
}
@@ -195,7 +224,7 @@ STATIC void gc_deal_with_stack_overflow(void) {
gc_sp = gc_stack;
// scan entire memory looking for blocks which have been marked but not their children
for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
for (mp_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
// trace (again) if mark bit set
if (ATB_GET_KIND(block) == AT_MARK) {
*gc_sp++ = block;
@@ -205,10 +234,17 @@ STATIC void gc_deal_with_stack_overflow(void) {
}
}
#if MICROPY_PY_GC_COLLECT_RETVAL
uint gc_collected;
#endif
STATIC void gc_sweep(void) {
#if MICROPY_PY_GC_COLLECT_RETVAL
gc_collected = 0;
#endif
// free unmarked heads and their tails
int free_tail = 0;
for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
for (mp_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
switch (ATB_GET_KIND(block)) {
case AT_HEAD:
#if MICROPY_ENABLE_FINALISER
@@ -228,10 +264,14 @@ STATIC void gc_sweep(void) {
}
#endif
free_tail = 1;
#if MICROPY_PY_GC_COLLECT_RETVAL
gc_collected++;
#endif
// fall through to free the head
case AT_TAIL:
if (free_tail) {
DEBUG_printf("gc_sweep(%p)\n",PTR_FROM_BLOCK(block));
ATB_ANY_TO_FREE(block);
}
break;
@@ -250,9 +290,9 @@ void gc_collect_start(void) {
gc_sp = gc_stack;
}
void gc_collect_root(void **ptrs, machine_uint_t len) {
for (machine_uint_t i = 0; i < len; i++) {
machine_uint_t ptr = (machine_uint_t)ptrs[i];
void gc_collect_root(void **ptrs, mp_uint_t len) {
for (mp_uint_t i = 0; i < len; i++) {
mp_uint_t ptr = (mp_uint_t)ptrs[i];
VERIFY_MARK_AND_PUSH(ptr);
gc_drain_stack();
}
@@ -265,14 +305,14 @@ void gc_collect_end(void) {
}
void gc_info(gc_info_t *info) {
info->total = (gc_pool_end - gc_pool_start) * sizeof(machine_uint_t);
info->total = (gc_pool_end - gc_pool_start) * sizeof(mp_uint_t);
info->used = 0;
info->free = 0;
info->num_1block = 0;
info->num_2block = 0;
info->max_block = 0;
for (machine_uint_t block = 0, len = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
machine_uint_t kind = ATB_GET_KIND(block);
for (mp_uint_t block = 0, len = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
mp_uint_t kind = ATB_GET_KIND(block);
if (kind == AT_FREE || kind == AT_HEAD) {
if (len == 1) {
info->num_1block += 1;
@@ -309,8 +349,8 @@ void gc_info(gc_info_t *info) {
info->free *= BYTES_PER_BLOCK;
}
void *gc_alloc(machine_uint_t n_bytes, bool has_finaliser) {
machine_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK;
void *gc_alloc(mp_uint_t n_bytes, bool has_finaliser) {
mp_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK;
DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks);
// check if GC is locked
@@ -323,10 +363,10 @@ void *gc_alloc(machine_uint_t n_bytes, bool has_finaliser) {
return NULL;
}
machine_uint_t i;
machine_uint_t end_block;
machine_uint_t start_block;
machine_uint_t n_free = 0;
mp_uint_t i;
mp_uint_t end_block;
mp_uint_t start_block;
mp_uint_t n_free = 0;
int collected = 0;
for (;;) {
@@ -359,19 +399,20 @@ found:
// mark rest of blocks as used tail
// TODO for a run of many blocks can make this more efficient
for (machine_uint_t bl = start_block + 1; bl <= end_block; bl++) {
for (mp_uint_t bl = start_block + 1; bl <= end_block; bl++) {
ATB_FREE_TO_TAIL(bl);
}
// get pointer to first block
void *ret_ptr = (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK);
DEBUG_printf("gc_alloc(%p)\n", ret_ptr);
// zero out the additional bytes of the newly allocated blocks
// This is needed because the blocks may have previously held pointers
// to the heap and will not be set to something else if the caller
// doesn't actually use the entire block. As such they will continue
// to point to the heap and may prevent other blocks from being reclaimed.
memset(ret_ptr + n_bytes, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK - n_bytes);
memset((byte*)ret_ptr + n_bytes, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK - n_bytes);
#if MICROPY_ENABLE_FINALISER
if (has_finaliser) {
@@ -386,11 +427,11 @@ found:
}
/*
void *gc_alloc(machine_uint_t n_bytes) {
void *gc_alloc(mp_uint_t n_bytes) {
return _gc_alloc(n_bytes, false);
}
void *gc_alloc_with_finaliser(machine_uint_t n_bytes) {
void *gc_alloc_with_finaliser(mp_uint_t n_bytes) {
return _gc_alloc(n_bytes, true);
}
*/
@@ -402,10 +443,11 @@ void gc_free(void *ptr_in) {
return;
}
machine_uint_t ptr = (machine_uint_t)ptr_in;
mp_uint_t ptr = (mp_uint_t)ptr_in;
DEBUG_printf("gc_free(%p)\n", ptr);
if (VERIFY_PTR(ptr)) {
machine_uint_t block = BLOCK_FROM_PTR(ptr);
mp_uint_t block = BLOCK_FROM_PTR(ptr);
if (ATB_GET_KIND(block) == AT_HEAD) {
// free head and all of its tail blocks
do {
@@ -416,14 +458,14 @@ void gc_free(void *ptr_in) {
}
}
machine_uint_t gc_nbytes(void *ptr_in) {
machine_uint_t ptr = (machine_uint_t)ptr_in;
mp_uint_t gc_nbytes(void *ptr_in) {
mp_uint_t ptr = (mp_uint_t)ptr_in;
if (VERIFY_PTR(ptr)) {
machine_uint_t block = BLOCK_FROM_PTR(ptr);
mp_uint_t block = BLOCK_FROM_PTR(ptr);
if (ATB_GET_KIND(block) == AT_HEAD) {
// work out number of consecutive blocks in the chain starting with this on
machine_uint_t n_blocks = 0;
mp_uint_t n_blocks = 0;
do {
n_blocks += 1;
} while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
@@ -437,8 +479,8 @@ machine_uint_t gc_nbytes(void *ptr_in) {
#if 0
// old, simple realloc that didn't expand memory in place
void *gc_realloc(void *ptr, machine_uint_t n_bytes) {
machine_uint_t n_existing = gc_nbytes(ptr);
void *gc_realloc(void *ptr, mp_uint_t n_bytes) {
mp_uint_t n_existing = gc_nbytes(ptr);
if (n_bytes <= n_existing) {
return ptr;
} else {
@@ -447,7 +489,7 @@ void *gc_realloc(void *ptr, machine_uint_t n_bytes) {
has_finaliser = false;
} else {
#if MICROPY_ENABLE_FINALISER
has_finaliser = FTB_GET(BLOCK_FROM_PTR((machine_uint_t)ptr));
has_finaliser = FTB_GET(BLOCK_FROM_PTR((mp_uint_t)ptr));
#else
has_finaliser = false;
#endif
@@ -464,7 +506,7 @@ void *gc_realloc(void *ptr, machine_uint_t n_bytes) {
#else // Alternative gc_realloc impl
void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) {
void *gc_realloc(void *ptr_in, mp_uint_t n_bytes) {
if (gc_lock_depth > 0) {
return NULL;
}
@@ -474,7 +516,7 @@ void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) {
return gc_alloc(n_bytes, false);
}
machine_uint_t ptr = (machine_uint_t)ptr_in;
mp_uint_t ptr = (mp_uint_t)ptr_in;
// sanity check the ptr
if (!VERIFY_PTR(ptr)) {
@@ -482,7 +524,7 @@ void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) {
}
// get first block
machine_uint_t block = BLOCK_FROM_PTR(ptr);
mp_uint_t block = BLOCK_FROM_PTR(ptr);
// sanity check the ptr is pointing to the head of a block
if (ATB_GET_KIND(block) != AT_HEAD) {
@@ -490,14 +532,14 @@ void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) {
}
// compute number of new blocks that are requested
machine_uint_t new_blocks = (n_bytes + BYTES_PER_BLOCK - 1) / BYTES_PER_BLOCK;
mp_uint_t new_blocks = (n_bytes + BYTES_PER_BLOCK - 1) / BYTES_PER_BLOCK;
// get the number of consecutive tail blocks and
// the number of free blocks after last tail block
// stop if we reach (or are at) end of heap
machine_uint_t n_free = 0;
machine_uint_t n_blocks = 1; // counting HEAD block
machine_uint_t max_block = gc_alloc_table_byte_len * BLOCKS_PER_ATB;
mp_uint_t n_free = 0;
mp_uint_t n_blocks = 1; // counting HEAD block
mp_uint_t max_block = gc_alloc_table_byte_len * BLOCKS_PER_ATB;
while (block + n_blocks + n_free < max_block) {
if (n_blocks + n_free >= new_blocks) {
// stop as soon as we find enough blocks for n_bytes
@@ -520,7 +562,7 @@ void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) {
// check if we can shrink the allocated area
if (new_blocks < n_blocks) {
// free unneeded tail blocks
for (machine_uint_t bl = block + new_blocks; ATB_GET_KIND(bl) == AT_TAIL; bl++) {
for (mp_uint_t bl = block + new_blocks; ATB_GET_KIND(bl) == AT_TAIL; bl++) {
ATB_ANY_TO_FREE(bl);
}
return ptr_in;
@@ -529,13 +571,13 @@ void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) {
// check if we can expand in place
if (new_blocks <= n_blocks + n_free) {
// mark few more blocks as used tail
for (machine_uint_t bl = block + n_blocks; bl < block + new_blocks; bl++) {
for (mp_uint_t bl = block + n_blocks; bl < block + new_blocks; bl++) {
assert(ATB_GET_KIND(bl) == AT_FREE);
ATB_FREE_TO_TAIL(bl);
}
// zero out the additional bytes of the newly allocated blocks (see comment above in gc_alloc)
memset(ptr_in + n_bytes, 0, new_blocks * BYTES_PER_BLOCK - n_bytes);
memset((byte*)ptr_in + n_bytes, 0, new_blocks * BYTES_PER_BLOCK - n_bytes);
return ptr_in;
}
@@ -554,7 +596,7 @@ void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) {
return NULL;
}
DEBUG_printf("gc_realloc: allocating new block\n");
DEBUG_printf("gc_realloc(%p -> %p)\n", ptr_in, ptr_out);
memcpy(ptr_out, ptr_in, n_blocks * BYTES_PER_BLOCK);
gc_free(ptr_in);
return ptr_out;
@@ -571,7 +613,7 @@ void gc_dump_info() {
void gc_dump_alloc_table(void) {
printf("GC memory layout; from %p:", gc_pool_start);
for (machine_uint_t bl = 0; bl < gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) {
for (mp_uint_t bl = 0; bl < gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) {
if (bl % 64 == 0) {
printf("\n%04x: ", (uint)bl);
}
@@ -581,12 +623,12 @@ void gc_dump_alloc_table(void) {
case AT_HEAD: c = 'h'; break;
/* this prints the uPy object type of the head block
case AT_HEAD: {
machine_uint_t *ptr = gc_pool_start + bl * WORDS_PER_BLOCK;
if (*ptr == (machine_uint_t)&mp_type_tuple) { c = 'T'; }
else if (*ptr == (machine_uint_t)&mp_type_list) { c = 'L'; }
else if (*ptr == (machine_uint_t)&mp_type_dict) { c = 'D'; }
else if (*ptr == (machine_uint_t)&mp_type_float) { c = 'F'; }
else if (*ptr == (machine_uint_t)&mp_type_fun_bc) { c = 'B'; }
mp_uint_t *ptr = gc_pool_start + bl * WORDS_PER_BLOCK;
if (*ptr == (mp_uint_t)&mp_type_tuple) { c = 'T'; }
else if (*ptr == (mp_uint_t)&mp_type_list) { c = 'L'; }
else if (*ptr == (mp_uint_t)&mp_type_dict) { c = 'D'; }
else if (*ptr == (mp_uint_t)&mp_type_float) { c = 'F'; }
else if (*ptr == (mp_uint_t)&mp_type_fun_bc) { c = 'B'; }
else { c = 'h'; }
break;
}
@@ -601,23 +643,23 @@ void gc_dump_alloc_table(void) {
#if DEBUG_PRINT
void gc_test(void) {
machine_uint_t len = 500;
machine_uint_t *heap = malloc(len);
gc_init(heap, heap + len / sizeof(machine_uint_t));
mp_uint_t len = 500;
mp_uint_t *heap = malloc(len);
gc_init(heap, heap + len / sizeof(mp_uint_t));
void *ptrs[100];
{
machine_uint_t **p = gc_alloc(16, false);
mp_uint_t **p = gc_alloc(16, false);
p[0] = gc_alloc(64, false);
p[1] = gc_alloc(1, false);
p[2] = gc_alloc(1, false);
p[3] = gc_alloc(1, false);
machine_uint_t ***p2 = gc_alloc(16, false);
mp_uint_t ***p2 = gc_alloc(16, false);
p2[0] = p;
p2[1] = p;
ptrs[0] = p2;
}
for (int i = 0; i < 25; i+=2) {
machine_uint_t *p = gc_alloc(i, false);
mp_uint_t *p = gc_alloc(i, false);
printf("p=%p\n", p);
if (i & 3) {
//ptrs[i] = p;

47
py/gc.h
View File

@@ -1,28 +1,55 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
void gc_init(void *start, void *end);
// These lock/unlock functions can be nested.
// They can be used to prevent the GC from allocating/freeing.
void gc_lock(void);
void gc_unlock(void);
bool gc_is_locked(void);
// A given port must implement gc_collect by using the other collect functions.
void gc_collect(void);
void gc_collect_start(void);
void gc_collect_root(void **ptrs, machine_uint_t len);
void gc_collect_root(void **ptrs, mp_uint_t len);
void gc_collect_end(void);
void *gc_alloc(machine_uint_t n_bytes, bool has_finaliser);
void *gc_alloc(mp_uint_t n_bytes, bool has_finaliser);
void gc_free(void *ptr);
machine_uint_t gc_nbytes(void *ptr);
void *gc_realloc(void *ptr, machine_uint_t n_bytes);
mp_uint_t gc_nbytes(void *ptr);
void *gc_realloc(void *ptr, mp_uint_t n_bytes);
typedef struct _gc_info_t {
machine_uint_t total;
machine_uint_t used;
machine_uint_t free;
machine_uint_t num_1block;
machine_uint_t num_2block;
machine_uint_t max_block;
mp_uint_t total;
mp_uint_t used;
mp_uint_t free;
mp_uint_t num_1block;
mp_uint_t num_2block;
mp_uint_t max_block;
} gc_info_t;
void gc_info(gc_info_t *info);

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// rules for writing rules:
// - zero_or_more is implemented using opt_rule around a one_or_more rule
// - don't put opt_rule in arguments of or rule; instead, wrap the call to this or rule in opt_rule

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/* lexer.c -- simple tokeniser for Python implementation
*/
@@ -6,8 +32,8 @@
#include <stdio.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "lexer.h"
@@ -24,23 +50,25 @@ struct _mp_lexer_t {
unichar chr0, chr1, chr2; // current cached characters from source
uint line; // source line
uint column; // source column
mp_uint_t line; // source line
mp_uint_t column; // source column
int emit_dent; // non-zero when there are INDENT/DEDENT tokens to emit
int nested_bracket_level; // >0 when there are nested brackets over multiple lines
mp_int_t emit_dent; // non-zero when there are INDENT/DEDENT tokens to emit
mp_int_t nested_bracket_level; // >0 when there are nested brackets over multiple lines
uint alloc_indent_level;
uint num_indent_level;
mp_uint_t alloc_indent_level;
mp_uint_t num_indent_level;
uint16_t *indent_level;
vstr_t vstr;
mp_token_t tok_cur;
};
mp_uint_t mp_optimise_value;
// TODO replace with a call to a standard function
bool str_strn_equal(const char *str, const char *strn, int len) {
uint i = 0;
bool str_strn_equal(const char *str, const char *strn, mp_uint_t len) {
mp_uint_t i = 0;
while (i < len && *str == *strn) {
++i;
@@ -53,10 +81,10 @@ bool str_strn_equal(const char *str, const char *strn, int len) {
#ifdef MICROPY_DEBUG_PRINTERS
void mp_token_show(const mp_token_t *tok) {
printf("(%d:%d) kind:%d str:%p len:%d", tok->src_line, tok->src_column, tok->kind, tok->str, tok->len);
printf("(" UINT_FMT ":" UINT_FMT ") kind:%u str:%p len:" UINT_FMT, tok->src_line, tok->src_column, tok->kind, tok->str, tok->len);
if (tok->str != NULL && tok->len > 0) {
const char *i = tok->str;
const char *j = i + tok->len;
const byte *i = (const byte *)tok->str;
const byte *j = (const byte *)i + tok->len;
printf(" ");
while (i < j) {
unichar c = utf8_get_char(i);
@@ -147,7 +175,7 @@ STATIC void next_char(mp_lexer_t *lex) {
return;
}
int advance = 1;
mp_uint_t advance = 1;
if (lex->chr0 == '\n') {
// LF is a new line
@@ -182,15 +210,16 @@ STATIC void next_char(mp_lexer_t *lex) {
}
}
void indent_push(mp_lexer_t *lex, uint indent) {
void indent_push(mp_lexer_t *lex, mp_uint_t indent) {
if (lex->num_indent_level >= lex->alloc_indent_level) {
lex->indent_level = m_renew(uint16_t, lex->indent_level, lex->alloc_indent_level, lex->alloc_indent_level * 2);
lex->alloc_indent_level *= 2;
// TODO use m_renew_maybe and somehow indicate an error if it fails... probably by using MP_TOKEN_MEMORY_ERROR
lex->indent_level = m_renew(uint16_t, lex->indent_level, lex->alloc_indent_level, lex->alloc_indent_level + MICROPY_ALLOC_LEXEL_INDENT_INC);
lex->alloc_indent_level += MICROPY_ALLOC_LEXEL_INDENT_INC;
}
lex->indent_level[lex->num_indent_level++] = indent;
}
uint indent_top(mp_lexer_t *lex) {
mp_uint_t indent_top(mp_lexer_t *lex) {
return lex->indent_level[lex->num_indent_level - 1];
}
@@ -276,12 +305,12 @@ STATIC const char *tok_kw[] = {
"while",
"with",
"yield",
NULL,
"__debug__",
};
STATIC int hex_digit(unichar c) {
STATIC mp_uint_t hex_digit(unichar c) {
// c is assumed to be hex digit
int n = c - '0';
mp_uint_t n = c - '0';
if (n > 9) {
n &= ~('a' - 'A');
n -= ('A' - ('9' + 1));
@@ -291,8 +320,9 @@ STATIC int hex_digit(unichar c) {
// This is called with CUR_CHAR() before first hex digit, and should return with
// it pointing to last hex digit
STATIC bool get_hex(mp_lexer_t *lex, int num_digits, uint *result) {
uint num = 0;
// num_digits must be greater than zero
STATIC bool get_hex(mp_lexer_t *lex, mp_uint_t num_digits, mp_uint_t *result) {
mp_uint_t num = 0;
while (num_digits-- != 0) {
next_char(lex);
unichar c = CUR_CHAR(lex);
@@ -365,7 +395,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
} else if (had_physical_newline && lex->nested_bracket_level == 0) {
tok->kind = MP_TOKEN_NEWLINE;
uint num_spaces = lex->column - 1;
mp_uint_t num_spaces = lex->column - 1;
lex->emit_dent = 0;
if (num_spaces == indent_top(lex)) {
} else if (num_spaces > indent_top(lex)) {
@@ -434,7 +464,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
next_char(lex);
// work out if it's a single or triple quoted literal
int num_quotes;
mp_uint_t num_quotes;
if (is_char_and(lex, quote_char, quote_char)) {
// triple quotes
next_char(lex);
@@ -446,7 +476,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
}
// parse the literal
int n_closing = 0;
mp_uint_t n_closing = 0;
while (!is_end(lex) && (num_quotes > 1 || !is_char(lex, '\n')) && n_closing < num_quotes) {
if (is_char(lex, quote_char)) {
n_closing += 1;
@@ -473,24 +503,37 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
case 'v': c = 0x0b; break;
case 'f': c = 0x0c; break;
case 'r': c = 0x0d; break;
case 'u':
case 'U':
if (is_bytes) {
// b'\u1234' == b'\\u1234'
vstr_add_char(&lex->vstr, '\\');
break;
}
// Otherwise fall through.
case 'x':
{
uint num = 0;
if (!get_hex(lex, 2, &num)) {
mp_uint_t num = 0;
if (!get_hex(lex, (c == 'x' ? 2 : c == 'u' ? 4 : 8), &num)) {
// TODO error message
assert(0);
}
c = num;
break;
}
case 'N': break; // TODO \N{name} only in strings
case 'u': break; // TODO \uxxxx only in strings
case 'U': break; // TODO \Uxxxxxxxx only in strings
case 'N':
// Supporting '\N{LATIN SMALL LETTER A}' == 'a' would require keeping the
// entire Unicode name table in the core. As of Unicode 6.3.0, that's nearly
// 3MB of text; even gzip-compressed and with minimal structure, it'll take
// roughly half a meg of storage. This form of Unicode escape may be added
// later on, but it's definitely not a priority right now. -- CJA 20140607
assert(!"Unicode name escapes not supported");
break;
default:
if (c >= '0' && c <= '7') {
// Octal sequence, 1-3 chars
int digits = 3;
int num = c - '0';
mp_uint_t digits = 3;
mp_uint_t num = c - '0';
while (is_following_odigit(lex) && --digits != 0) {
next_char(lex);
num = num * 8 + (CUR_CHAR(lex) - '0');
@@ -504,7 +547,13 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
}
}
if (c != MP_LEXER_CHAR_EOF) {
vstr_add_char(&lex->vstr, c);
if (c < 0x110000 && !is_bytes) {
vstr_add_char(&lex->vstr, c);
} else if (c < 0x100 && is_bytes) {
vstr_add_byte(&lex->vstr, c);
} else {
assert(!"TODO: Throw an error, invalid escape code probably");
}
}
} else {
vstr_add_char(&lex->vstr, CUR_CHAR(lex));
@@ -579,7 +628,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
// search for encoded delimiter or operator
const char *t = tok_enc;
uint tok_enc_index = 0;
mp_uint_t tok_enc_index = 0;
for (; *t != 0 && !is_char(lex, *t); t += 1) {
if (*t == 'e' || *t == 'c') {
t += 1;
@@ -601,7 +650,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
// get the maximum characters for a valid token
t += 1;
uint t_index = tok_enc_index;
mp_uint_t t_index = tok_enc_index;
for (;;) {
for (; *t == 'e'; t += 1) {
t += 1;
@@ -660,9 +709,19 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
// check for keywords
if (tok->kind == MP_TOKEN_NAME) {
for (int i = 0; tok_kw[i] != NULL; i++) {
// We check for __debug__ here and convert it to its value. This is so
// the parser gives a syntax error on, eg, x.__debug__. Otherwise, we
// need to check for this special token in many places in the compiler.
// TODO improve speed of these string comparisons
//for (mp_int_t i = 0; tok_kw[i] != NULL; i++) {
for (mp_int_t i = 0; i < MP_ARRAY_SIZE(tok_kw); i++) {
if (str_strn_equal(tok_kw[i], tok->str, tok->len)) {
tok->kind = MP_TOKEN_KW_FALSE + i;
if (i == MP_ARRAY_SIZE(tok_kw) - 1) {
// tok_kw[MP_ARRAY_SIZE(tok_kw) - 1] == "__debug__"
tok->kind = (mp_optimise_value == 0 ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE);
} else {
tok->kind = MP_TOKEN_KW_FALSE + i;
}
break;
}
}
@@ -670,7 +729,15 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
}
mp_lexer_t *mp_lexer_new(qstr src_name, void *stream_data, mp_lexer_stream_next_char_t stream_next_char, mp_lexer_stream_close_t stream_close) {
mp_lexer_t *lex = m_new(mp_lexer_t, 1);
mp_lexer_t *lex = m_new_maybe(mp_lexer_t, 1);
// check for memory allocation error
if (lex == NULL) {
if (stream_close) {
stream_close(stream_data);
}
return NULL;
}
lex->source_name = src_name;
lex->stream_data = stream_data;
@@ -680,12 +747,20 @@ mp_lexer_t *mp_lexer_new(qstr src_name, void *stream_data, mp_lexer_stream_next_
lex->column = 1;
lex->emit_dent = 0;
lex->nested_bracket_level = 0;
lex->alloc_indent_level = 16;
lex->alloc_indent_level = MICROPY_ALLOC_LEXER_INDENT_INIT;
lex->num_indent_level = 1;
lex->indent_level = m_new(uint16_t, lex->alloc_indent_level);
lex->indent_level[0] = 0;
lex->indent_level = m_new_maybe(uint16_t, lex->alloc_indent_level);
vstr_init(&lex->vstr, 32);
// check for memory allocation error
if (lex->indent_level == NULL || vstr_had_error(&lex->vstr)) {
mp_lexer_free(lex);
return NULL;
}
// store sentinel for first indentation level
lex->indent_level[0] = 0;
// preload characters
lex->chr0 = stream_next_char(stream_data);
lex->chr1 = stream_next_char(stream_data);

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/* lexer.h -- simple tokeniser for Micro Python
*
* Uses (byte) length instead of null termination.
@@ -105,12 +131,12 @@ typedef enum _mp_token_kind_t {
} mp_token_kind_t;
typedef struct _mp_token_t {
uint src_line; // source line
uint src_column; // source column
mp_uint_t src_line; // source line
mp_uint_t src_column; // source column
mp_token_kind_t kind; // kind of token
const char *str; // string of token (valid only while this token is current token)
uint len; // (byte) length of string of token
mp_uint_t len; // (byte) length of string of token
} mp_token_t;
// the next-char function must return the next character in the stream
@@ -125,7 +151,7 @@ typedef struct _mp_lexer_t mp_lexer_t;
void mp_token_show(const mp_token_t *tok);
mp_lexer_t *mp_lexer_new(qstr src_name, void *stream_data, mp_lexer_stream_next_char_t stream_next_char, mp_lexer_stream_close_t stream_close);
mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, uint len, uint free_len);
mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len);
void mp_lexer_free(mp_lexer_t *lex);
qstr mp_lexer_source_name(mp_lexer_t *lex);
@@ -150,3 +176,5 @@ typedef enum {
mp_import_stat_t mp_import_stat(const char *path);
mp_lexer_t *mp_lexer_new_from_file(const char *filename);
extern mp_uint_t mp_optimise_value;

View File

@@ -1,11 +1,36 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "lexer.h"
typedef struct _mp_lexer_str_buf_t {
uint free_len; // if > 0, src_beg will be freed when done by: m_free(src_beg, free_len)
mp_uint_t free_len; // if > 0, src_beg will be freed when done by: m_free(src_beg, free_len)
const char *src_beg; // beginning of source
const char *src_cur; // current location in source
const char *src_end; // end (exclusive) of source
@@ -26,7 +51,7 @@ STATIC void str_buf_free(mp_lexer_str_buf_t *sb) {
m_del_obj(mp_lexer_str_buf_t, sb);
}
mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, uint len, uint free_len) {
mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t len, mp_uint_t free_len) {
mp_lexer_str_buf_t *sb = m_new_obj(mp_lexer_str_buf_t);
sb->free_len = free_len;
sb->src_beg = str;

View File

@@ -1,7 +1,33 @@
#include "misc.h"
#include "mpconfig.h"
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#if MICROPY_ENABLE_LEXER_UNIX
#include "mpconfig.h"
#include "misc.h"
#if MICROPY_HELPER_LEXER_UNIX
#include <stdio.h>
#include <unistd.h>
@@ -55,4 +81,4 @@ mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
return mp_lexer_new(qstr_from_str(filename), fb, (mp_lexer_stream_next_char_t)file_buf_next_char, (mp_lexer_stream_close_t)file_buf_close);
}
#endif // MICROPY_ENABLE_LEXER_UNIX
#endif // MICROPY_HELPER_LEXER_UNIX

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
mp_lexer_t *mp_lexer_new_from_file(const char *filename);
void mp_import_set_directory(const char *dir);

View File

@@ -27,7 +27,8 @@ def compute_hash(qstr):
hash = 5381
for char in qstr:
hash = (hash * 33) ^ ord(char)
return hash & 0xffff
# Make sure that valid hash is never zero, zero means "hash not computed"
return (hash & 0xffff) or 1
def do_work(infiles):
# read the qstrs in from the input files

View File

@@ -1,9 +1,35 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#if 0 // print debugging info
#define DEBUG_printf DEBUG_printf

View File

@@ -1,8 +1,34 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
@@ -141,7 +167,7 @@ mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
}
}
machine_uint_t hash = mp_obj_hash(index);
mp_uint_t hash = mp_obj_hash(index);
uint pos = hash % map->alloc;
uint start_pos = pos;
mp_map_elem_t *avail_slot = NULL;
@@ -244,7 +270,7 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t looku
return NULL;
}
}
machine_uint_t hash = mp_obj_hash(index);
mp_uint_t hash = mp_obj_hash(index);
uint pos = hash % set->alloc;
uint start_pos = pos;
mp_obj_t *avail_slot = NULL;

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// a mini library of useful types and functions
#ifndef _INCLUDED_MINILIB_H
@@ -24,6 +50,7 @@ typedef unsigned int uint;
// TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)
#define m_new(type, num) ((type*)(m_malloc(sizeof(type) * (num))))
#define m_new_maybe(type, num) ((type*)(m_malloc_maybe(sizeof(type) * (num))))
#define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
#define m_new_obj(type) (m_new(type, 1))
#define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num)))
@@ -55,20 +82,30 @@ int m_get_peak_bytes_allocated(void);
/** array helpers ***********************************************/
// get the number of elements in a fixed-size array
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#define MP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
// align ptr to the nearest multiple of "alignment"
#define MP_ALIGN(ptr, alignment) (void*)(((mp_uint_t)(ptr) + ((alignment) - 1)) & ~((alignment) - 1))
/** unichar / UTF-8 *********************************************/
typedef int unichar; // TODO
unichar utf8_get_char(const char *s);
char *utf8_next_char(const char *s);
unichar utf8_get_char(const byte *s);
const byte *utf8_next_char(const byte *s);
bool unichar_isspace(unichar c);
bool unichar_isalpha(unichar c);
bool unichar_isprint(unichar c);
bool unichar_isdigit(unichar c);
bool unichar_isxdigit(unichar c);
bool unichar_isupper(unichar c);
bool unichar_islower(unichar c);
unichar unichar_tolower(unichar c);
unichar unichar_toupper(unichar c);
mp_uint_t unichar_charlen(const char *str, mp_uint_t len);
#define UTF8_IS_NONASCII(ch) ((ch) & 0x80)
#define UTF8_IS_CONT(ch) (((ch) & 0xC0) == 0x80)
/** variable string *********************************************/
@@ -130,4 +167,20 @@ void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
// Debugging helpers
int DEBUG_printf(const char *fmt, ...);
extern uint mp_verbose_flag;
// This is useful for unicode handling. Some CPU archs has
// special instructions for efficient implentation of this
// function (e.g. CLZ on ARM).
// NOTE: this function is unused at the moment
#ifndef count_lead_ones
static inline uint count_lead_ones(byte val) {
uint c = 0;
for (byte mask = 0x80; val & mask; mask >>= 1) {
c++;
}
return c;
}
#endif
#endif // _INCLUDED_MINILIB_H

View File

@@ -73,9 +73,9 @@ all: $(PROG)
$(PROG): $(OBJ)
$(ECHO) "LINK $@"
$(Q)$(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS)
$(Q)$(CC) $(COPT) -o $@ $(OBJ) $(LIB) $(LDFLAGS)
ifndef DEBUG
$(Q)$(STRIP) $(PROG)
$(Q)$(STRIP) $(STRIPFLAGS_EXTRA) $(PROG)
endif
$(Q)$(SIZE) $(PROG)
@@ -97,4 +97,10 @@ print-cfg:
$(ECHO) "OBJ = $(OBJ)"
.PHONY: print-cfg
print-def:
@$(ECHO) "The following defines are built into the $(CC) compiler"
touch __empty__.c
@$(CC) -E -Wp,-dM __empty__.c
@$(RM) -f __empty__.c
-include $(OBJ:.o=.P)

View File

@@ -1,9 +1,37 @@
#include "misc.h"
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "builtin.h"
#if MICROPY_PY_ARRAY
STATIC const mp_map_elem_t mp_module_array_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_array) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_array), (mp_obj_t)&mp_type_array },
@@ -14,8 +42,8 @@ STATIC const mp_obj_dict_t mp_module_array_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = ARRAY_SIZE(mp_module_array_globals_table),
.alloc = ARRAY_SIZE(mp_module_array_globals_table),
.used = MP_ARRAY_SIZE(mp_module_array_globals_table),
.alloc = MP_ARRAY_SIZE(mp_module_array_globals_table),
.table = (mp_map_elem_t*)mp_module_array_globals_table,
},
};
@@ -25,3 +53,5 @@ const mp_obj_module_t mp_module_array = {
.name = MP_QSTR_array,
.globals = (mp_obj_dict_t*)&mp_module_array_globals,
};
#endif

View File

@@ -1,12 +1,38 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <math.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "builtin.h"
#if MICROPY_ENABLE_FLOAT && MICROPY_ENABLE_MOD_CMATH
#if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_CMATH
// These are defined in modmath.c
extern const mp_obj_float_t mp_math_e_obj;
@@ -116,8 +142,8 @@ STATIC const mp_obj_dict_t mp_module_cmath_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = ARRAY_SIZE(mp_module_cmath_globals_table),
.alloc = ARRAY_SIZE(mp_module_cmath_globals_table),
.used = MP_ARRAY_SIZE(mp_module_cmath_globals_table),
.alloc = MP_ARRAY_SIZE(mp_module_cmath_globals_table),
.table = (mp_map_elem_t*)mp_module_cmath_globals_table,
},
};
@@ -128,4 +154,4 @@ const mp_obj_module_t mp_module_cmath = {
.globals = (mp_obj_dict_t*)&mp_module_cmath_globals,
};
#endif // MICROPY_ENABLE_FLOAT && MICROPY_ENABLE_MOD_CMATH
#endif // MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_CMATH

View File

@@ -1,10 +1,36 @@
#include "misc.h"
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "builtin.h"
#if MICROPY_ENABLE_MOD_COLLECTIONS
#if MICROPY_PY_COLLECTIONS
STATIC const mp_map_elem_t mp_module_collections_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR__collections) },
@@ -16,8 +42,8 @@ STATIC const mp_obj_dict_t mp_module_collections_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = ARRAY_SIZE(mp_module_collections_globals_table),
.alloc = ARRAY_SIZE(mp_module_collections_globals_table),
.used = MP_ARRAY_SIZE(mp_module_collections_globals_table),
.alloc = MP_ARRAY_SIZE(mp_module_collections_globals_table),
.table = (mp_map_elem_t*)mp_module_collections_globals_table,
},
};
@@ -28,4 +54,4 @@ const mp_obj_module_t mp_module_collections = {
.globals = (mp_obj_dict_t*)&mp_module_collections_globals,
};
#endif // MICROPY_ENABLE_MOD_COLLECTIONS
#endif // MICROPY_PY_COLLECTIONS

104
py/modgc.c Normal file
View File

@@ -0,0 +1,104 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "builtin.h"
#include "runtime.h"
#include "objlist.h"
#include "objtuple.h"
#include "objstr.h"
#include "gc.h"
#if MICROPY_PY_GC && MICROPY_ENABLE_GC
extern uint gc_collected;
STATIC mp_obj_t py_gc_collect(void) {
gc_collect();
#if MICROPY_PY_GC_COLLECT_RETVAL
return MP_OBJ_NEW_SMALL_INT((mp_uint_t)gc_collected);
#else
return mp_const_none;
#endif
}
MP_DEFINE_CONST_FUN_OBJ_0(gc_collect_obj, py_gc_collect);
STATIC mp_obj_t gc_disable(void) {
gc_lock();
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(gc_disable_obj, gc_disable);
STATIC mp_obj_t gc_enable(void) {
gc_unlock();
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(gc_enable_obj, gc_enable);
STATIC mp_obj_t gc_mem_free(void) {
gc_info_t info;
gc_info(&info);
return MP_OBJ_NEW_SMALL_INT((mp_uint_t)info.free);
}
MP_DEFINE_CONST_FUN_OBJ_0(gc_mem_free_obj, gc_mem_free);
STATIC mp_obj_t gc_mem_alloc(void) {
gc_info_t info;
gc_info(&info);
return MP_OBJ_NEW_SMALL_INT((mp_uint_t)info.used);
}
MP_DEFINE_CONST_FUN_OBJ_0(gc_mem_alloc_obj, gc_mem_alloc);
STATIC const mp_map_elem_t mp_module_gc_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_gc) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_collect), (mp_obj_t)&gc_collect_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_disable), (mp_obj_t)&gc_disable_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_enable), (mp_obj_t)&gc_enable_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_free), (mp_obj_t)&gc_mem_free_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_alloc), (mp_obj_t)&gc_mem_alloc_obj },
};
STATIC const mp_obj_dict_t mp_module_gc_globals = {
.base = {&mp_type_dict},
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = MP_ARRAY_SIZE(mp_module_gc_globals_table),
.alloc = MP_ARRAY_SIZE(mp_module_gc_globals_table),
.table = (mp_map_elem_t*)mp_module_gc_globals_table,
},
};
const mp_obj_module_t mp_module_gc = {
.base = { &mp_type_module },
.name = MP_QSTR_gc,
.globals = (mp_obj_dict_t*)&mp_module_gc_globals,
};
#endif

View File

@@ -1,18 +1,55 @@
#include "misc.h"
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "builtin.h"
#if MICROPY_ENABLE_MOD_IO
#if MICROPY_PY_IO
extern const mp_obj_type_t mp_type_fileio;
extern const mp_obj_type_t mp_type_textio;
STATIC const mp_map_elem_t mp_module_io_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_io) },
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR__io) },
// Note: mp_builtin_open_obj should be defined by port, it's not
// part of the core.
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_BytesIO), (mp_obj_t)&mp_type_stringio },
#if MICROPY_PY_IO_FILEIO
{ MP_OBJ_NEW_QSTR(MP_QSTR_FileIO), (mp_obj_t)&mp_type_fileio },
#endif
#if MICROPY_CPYTHON_COMPAT
{ MP_OBJ_NEW_QSTR(MP_QSTR_TextIOWrapper), (mp_obj_t)&mp_type_textio },
#endif
{ MP_OBJ_NEW_QSTR(MP_QSTR_StringIO), (mp_obj_t)&mp_type_stringio },
#if MICROPY_PY_IO_BYTESIO
{ MP_OBJ_NEW_QSTR(MP_QSTR_BytesIO), (mp_obj_t)&mp_type_bytesio },
#endif
};
STATIC const mp_obj_dict_t mp_module_io_globals = {
@@ -20,15 +57,15 @@ STATIC const mp_obj_dict_t mp_module_io_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = ARRAY_SIZE(mp_module_io_globals_table),
.alloc = ARRAY_SIZE(mp_module_io_globals_table),
.used = MP_ARRAY_SIZE(mp_module_io_globals_table),
.alloc = MP_ARRAY_SIZE(mp_module_io_globals_table),
.table = (mp_map_elem_t*)mp_module_io_globals_table,
},
};
const mp_obj_module_t mp_module_io = {
.base = { &mp_type_module },
.name = MP_QSTR_io,
.name = MP_QSTR__io,
.globals = (mp_obj_dict_t*)&mp_module_io_globals,
};

View File

@@ -1,12 +1,38 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <math.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "builtin.h"
#if MICROPY_ENABLE_FLOAT && MICROPY_ENABLE_MOD_MATH
#if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH
//TODO: Change macros to check for overflow and raise OverflowError or RangeError
#define MATH_FUN_1(py_name, c_name) \
@@ -22,7 +48,7 @@
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
#define MATH_FUN_1_TO_INT(py_name, c_name) \
mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { return mp_obj_new_int((machine_int_t)MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj))); } \
mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { return mp_obj_new_int((mp_int_t)MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj))); } \
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
// These are also used by cmath.c
@@ -125,6 +151,7 @@ STATIC const mp_map_elem_t mp_module_math_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_copysign), (mp_obj_t)&mp_math_copysign_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_fabs), (mp_obj_t)&mp_math_fabs_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_floor), (mp_obj_t)&mp_math_floor_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_fmod), (mp_obj_t)&mp_math_fmod_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_frexp), (mp_obj_t)&mp_math_frexp_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_ldexp), (mp_obj_t)&mp_math_ldexp_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_modf), (mp_obj_t)&mp_math_modf_obj },
@@ -145,8 +172,8 @@ STATIC const mp_obj_dict_t mp_module_math_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = ARRAY_SIZE(mp_module_math_globals_table),
.alloc = ARRAY_SIZE(mp_module_math_globals_table),
.used = MP_ARRAY_SIZE(mp_module_math_globals_table),
.alloc = MP_ARRAY_SIZE(mp_module_math_globals_table),
.table = (mp_map_elem_t*)mp_module_math_globals_table,
},
};
@@ -157,4 +184,4 @@ const mp_obj_module_t mp_module_math = {
.globals = (mp_obj_dict_t*)&mp_module_math_globals,
};
#endif // MICROPY_ENABLE_FLOAT && MICROPY_ENABLE_MOD_MATH
#endif // MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH

View File

@@ -1,5 +1,31 @@
#include "misc.h"
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "builtin.h"
@@ -9,15 +35,15 @@
#if MICROPY_MEM_STATS
STATIC mp_obj_t mp_micropython_mem_total() {
return MP_OBJ_NEW_SMALL_INT((machine_int_t)m_get_total_bytes_allocated());
return MP_OBJ_NEW_SMALL_INT((mp_int_t)m_get_total_bytes_allocated());
}
STATIC mp_obj_t mp_micropython_mem_current() {
return MP_OBJ_NEW_SMALL_INT((machine_int_t)m_get_current_bytes_allocated());
return MP_OBJ_NEW_SMALL_INT((mp_int_t)m_get_current_bytes_allocated());
}
STATIC mp_obj_t mp_micropython_mem_peak() {
return MP_OBJ_NEW_SMALL_INT((machine_int_t)m_get_peak_bytes_allocated());
return MP_OBJ_NEW_SMALL_INT((mp_int_t)m_get_peak_bytes_allocated());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_micropython_mem_total_obj, mp_micropython_mem_total);
@@ -39,8 +65,8 @@ STATIC const mp_obj_dict_t mp_module_micropython_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = ARRAY_SIZE(mp_module_micropython_globals_table),
.alloc = ARRAY_SIZE(mp_module_micropython_globals_table),
.used = MP_ARRAY_SIZE(mp_module_micropython_globals_table),
.alloc = MP_ARRAY_SIZE(mp_module_micropython_globals_table),
.table = (mp_map_elem_t*)mp_module_micropython_globals_table,
},
};

View File

@@ -1,15 +1,59 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2014 Paul Sokolovsky
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <assert.h>
#include <string.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "builtin.h"
#include "objtuple.h"
#include "objstr.h"
#include "binary.h"
#include "parsenum.h"
#if MICROPY_ENABLE_MOD_STRUCT
#if MICROPY_PY_STRUCT
/*
This module implements most of character typecodes from CPython, with
some extensions:
O - (Pointer to) an arbitrary Python object. This is useful for callback
data, etc. Note that you must keep reference to passed object in
your Python application, otherwise it may be garbage-collected,
and then when you get back this value from callback it may be
invalid (and lead to crash).
S - Pointer to a string (returned as a Python string). Note the
difference from "Ns", - the latter says "in this place of structure
is character data of up to N bytes length", while "S" means
"in this place of a structure is a pointer to zero-terminated
character data".
*/
STATIC char get_fmt_type(const char **fmt) {
char t = **fmt;
@@ -30,20 +74,51 @@ STATIC char get_fmt_type(const char **fmt) {
return t;
}
STATIC mp_uint_t get_fmt_num(const char **p) {
const char *num = *p;
uint len = 1;
while (unichar_isdigit(*++num)) {
len++;
}
mp_uint_t val = (mp_uint_t)MP_OBJ_SMALL_INT_VALUE(mp_parse_num_integer(*p, len, 10));
*p = num;
return val;
}
STATIC uint calcsize_items(const char *fmt) {
// TODO
return strlen(fmt);
uint cnt = 0;
while (*fmt) {
// TODO supports size spec only for "s"
if (!unichar_isdigit(*fmt++)) {
cnt++;
}
}
return cnt;
}
STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) {
const char *fmt = mp_obj_str_get_str(fmt_in);
char fmt_type = get_fmt_type(&fmt);
machine_uint_t size;
mp_uint_t size;
for (size = 0; *fmt; fmt++) {
uint align;
int sz = mp_binary_get_size(fmt_type, *fmt, &align);
uint align = 1;
mp_uint_t cnt = 1;
if (unichar_isdigit(*fmt)) {
cnt = get_fmt_num(&fmt);
}
if (cnt > 1) {
// TODO: count spec support only for string len
assert(*fmt == 's');
}
mp_uint_t sz;
if (*fmt == 's') {
sz = cnt;
} else {
sz = (mp_uint_t)mp_binary_get_size(fmt_type, *fmt, &align);
}
// TODO
assert(sz != -1);
assert(sz != (mp_uint_t)-1);
// Apply alignment
size = (size + align - 1) & ~(align - 1);
size += sz;
@@ -63,7 +138,22 @@ STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
byte *p = bufinfo.buf;
for (uint i = 0; i < size; i++) {
mp_obj_t item = mp_binary_get_val(fmt_type, *fmt++, &p);
mp_uint_t sz = 1;
if (unichar_isdigit(*fmt)) {
sz = get_fmt_num(&fmt);
}
if (sz > 1) {
// TODO: size spec support only for string len
assert(*fmt == 's');
}
mp_obj_t item;
if (*fmt == 's') {
item = mp_obj_new_bytes(p, sz);
p += sz;
fmt++;
} else {
item = mp_binary_get_val(fmt_type, *fmt++, &p);
}
res->items[i] = item;
}
return res;
@@ -80,11 +170,33 @@ STATIC mp_obj_t struct_pack(uint n_args, mp_obj_t *args) {
memset(p, 0, size);
for (uint i = 1; i < n_args; i++) {
mp_binary_set_val(fmt_type, *fmt++, args[i], &p);
mp_uint_t sz = 1;
if (unichar_isdigit(*fmt)) {
sz = get_fmt_num(&fmt);
}
if (sz > 1) {
// TODO: size spec support only for string len
assert(*fmt == 's');
}
if (*fmt == 's') {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[i], &bufinfo, MP_BUFFER_READ);
mp_uint_t to_copy = sz;
if (bufinfo.len < to_copy) {
to_copy = bufinfo.len;
}
memcpy(p, bufinfo.buf, to_copy);
memset(p + to_copy, 0, sz - to_copy);
p += sz;
fmt++;
} else {
mp_binary_set_val(fmt_type, *fmt++, args[i], &p);
}
}
return res;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, -1, struct_pack);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, MP_OBJ_FUN_ARGS_MAX, struct_pack);
STATIC const mp_map_elem_t mp_module_struct_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_struct) },
@@ -98,8 +210,8 @@ STATIC const mp_obj_dict_t mp_module_struct_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = ARRAY_SIZE(mp_module_struct_globals_table),
.alloc = ARRAY_SIZE(mp_module_struct_globals_table),
.used = MP_ARRAY_SIZE(mp_module_struct_globals_table),
.alloc = MP_ARRAY_SIZE(mp_module_struct_globals_table),
.table = (mp_map_elem_t*)mp_module_struct_globals_table,
},
};

View File

@@ -1,5 +1,33 @@
#include "misc.h"
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <limits.h>
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "builtin.h"
@@ -7,16 +35,21 @@
#include "objlist.h"
#include "objtuple.h"
#include "objstr.h"
#include "mpz.h"
#include "objint.h"
#if MICROPY_ENABLE_MOD_SYS
#if MICROPY_PY_SYS
// These should be implemented by ports, specific types don't matter,
// only addresses.
struct _dummy_t;
extern struct _dummy_t mp_sys_exit_obj;
extern struct _dummy_t mp_sys_stdin_obj;
extern struct _dummy_t mp_sys_stdout_obj;
extern struct _dummy_t mp_sys_stderr_obj;
extern mp_obj_int_t mp_maxsize_obj;
mp_obj_list_t mp_sys_path_obj;
mp_obj_list_t mp_sys_argv_obj;
#define I(n) MP_OBJ_NEW_SMALL_INT(n)
@@ -24,20 +57,44 @@ mp_obj_list_t mp_sys_argv_obj;
STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3), I(4), I(0)}};
#undef I
STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0");
#ifdef MICROPY_PY_SYS_PLATFORM
STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM);
#endif
STATIC const mp_map_elem_t mp_module_sys_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_sys) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_path), (mp_obj_t)&mp_sys_path_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_argv), (mp_obj_t)&mp_sys_argv_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_version), (mp_obj_t)&version_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_version_info), (mp_obj_t)&mp_sys_version_info_obj },
#ifdef MICROPY_PY_SYS_PLATFORM
{ MP_OBJ_NEW_QSTR(MP_QSTR_platform), (mp_obj_t)&platform_obj },
#endif
#if MP_ENDIANNESS_LITTLE
{ MP_OBJ_NEW_QSTR(MP_QSTR_byteorder), MP_OBJ_NEW_QSTR(MP_QSTR_little) },
#else
{ MP_OBJ_NEW_QSTR(MP_QSTR_byteorder), MP_OBJ_NEW_QSTR(MP_QSTR_big) },
#endif
#if MICROPY_PY_SYS_MAXSIZE
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
// INT_MAX is not representable as small int, as we know that small int
// takes one bit for tag. So, we have little choice but to provide this
// value. Apps also should be careful to not try to compare sys.maxsize
// with some number (which may not fit in available int size), but instead
// count number of significant bits in sys.maxsize.
{ MP_OBJ_NEW_QSTR(MP_QSTR_maxsize), MP_OBJ_NEW_SMALL_INT(INT_MAX >> 1) },
#else
{ MP_OBJ_NEW_QSTR(MP_QSTR_maxsize), (mp_obj_t)&mp_maxsize_obj },
#endif
#endif
#if MICROPY_MOD_SYS_STDFILES
#if MICROPY_PY_SYS_EXIT
{ MP_OBJ_NEW_QSTR(MP_QSTR_exit), (mp_obj_t)&mp_sys_exit_obj },
#endif
#if MICROPY_PY_SYS_STDFILES
{ MP_OBJ_NEW_QSTR(MP_QSTR_stdin), (mp_obj_t)&mp_sys_stdin_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_stdout), (mp_obj_t)&mp_sys_stdout_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_stderr), (mp_obj_t)&mp_sys_stderr_obj },
@@ -49,8 +106,8 @@ STATIC const mp_obj_dict_t mp_module_sys_globals = {
.map = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = ARRAY_SIZE(mp_module_sys_globals_table),
.alloc = ARRAY_SIZE(mp_module_sys_globals_table),
.used = MP_ARRAY_SIZE(mp_module_sys_globals_table),
.alloc = MP_ARRAY_SIZE(mp_module_sys_globals_table),
.table = (mp_map_elem_t*)mp_module_sys_globals_table,
},
};

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// This file contains default configuration settings for MicroPython.
// You can override any of these options using mpconfigport.h file located
// in a directory of your port.
@@ -7,6 +33,60 @@
// Any options not explicitly set in mpconfigport.h will get default
// values below.
/*****************************************************************************/
/* Memory allocation policy */
// Initial amount for lexer indentation level
#ifndef MICROPY_ALLOC_LEXER_INDENT_INIT
#define MICROPY_ALLOC_LEXER_INDENT_INIT (10)
#endif
// Increment for lexer indentation level
#ifndef MICROPY_ALLOC_LEXEL_INDENT_INC
#define MICROPY_ALLOC_LEXEL_INDENT_INC (8)
#endif
// Initial amount for parse rule stack
#ifndef MICROPY_ALLOC_PARSE_RULE_INIT
#define MICROPY_ALLOC_PARSE_RULE_INIT (64)
#endif
// Increment for parse rule stack
#ifndef MICROPY_ALLOC_PARSE_RULE_INC
#define MICROPY_ALLOC_PARSE_RULE_INC (16)
#endif
// Initial amount for parse result stack
#ifndef MICROPY_ALLOC_PARSE_RESULT_INIT
#define MICROPY_ALLOC_PARSE_RESULT_INIT (32)
#endif
// Increment for parse result stack
#ifndef MICROPY_ALLOC_PARSE_RESULT_INC
#define MICROPY_ALLOC_PARSE_RESULT_INC (16)
#endif
// Strings this length or less will be interned by the parser
#ifndef MICROPY_ALLOC_PARSE_INTERN_STRING_LEN
#define MICROPY_ALLOC_PARSE_INTERN_STRING_LEN (10)
#endif
// Initial amount for ids in a scope
#ifndef MICROPY_ALLOC_SCOPE_ID_INIT
#define MICROPY_ALLOC_SCOPE_ID_INIT (4)
#endif
// Increment for ids in a scope
#ifndef MICROPY_ALLOC_SCOPE_ID_INC
#define MICROPY_ALLOC_SCOPE_ID_INC (6)
#endif
// Maximum length of a path in the filesystem
// So we can allocate a buffer on the stack for path manipulation in import
#ifndef MICROPY_ALLOC_PATH_MAX
#define MICROPY_ALLOC_PATH_MAX (512)
#endif
/*****************************************************************************/
/* Micro Python emitters */
@@ -31,6 +111,14 @@
#define MICROPY_EMIT_INLINE_THUMB (0)
#endif
/*****************************************************************************/
/* Compiler configuration */
// Whether to enable constant optimisation; id = const(value)
#ifndef MICROPY_COMP_CONST
#define MICROPY_COMP_CONST (1)
#endif
/*****************************************************************************/
/* Internal debugging stuff */
@@ -41,14 +129,23 @@
// Whether to build functions that print debugging info:
// mp_token_show
// mp_byte_code_print
// mp_bytecode_print
// mp_parse_node_print
#ifndef MICROPY_DEBUG_PRINTERS
#define MICROPY_DEBUG_PRINTERS (0)
#endif
/*****************************************************************************/
/* Fine control over Python features */
/* Optimisations */
// Whether to use computed gotos in the VM, or a switch
// Computed gotos are roughly 10% faster, and increase VM code size by a little
#ifndef MICROPY_OPT_COMPUTED_GOTO
#define MICROPY_OPT_COMPUTED_GOTO (0)
#endif
/*****************************************************************************/
/* Python internal features */
// Whether to include the garbage collector
#ifndef MICROPY_ENABLE_GC
@@ -60,14 +157,20 @@
#define MICROPY_ENABLE_GC_FINALISER (0)
#endif
// Whether to check C stack usage. C stack used for calling Python functions,
// etc. Not checking means segfault on overflow.
#ifndef MICROPY_STACK_CHECK
#define MICROPY_STACK_CHECK (1)
#endif
// Whether to include REPL helper function
#ifndef MICROPY_ENABLE_REPL_HELPERS
#define MICROPY_ENABLE_REPL_HELPERS (0)
#ifndef MICROPY_HELPER_REPL
#define MICROPY_HELPER_REPL (0)
#endif
// Whether to include lexer helper function for unix
#ifndef MICROPY_ENABLE_LEXER_UNIX
#define MICROPY_ENABLE_LEXER_UNIX (0)
#ifndef MICROPY_HELPER_LEXER_UNIX
#define MICROPY_HELPER_LEXER_UNIX (0)
#endif
// Long int implementation
@@ -115,60 +218,19 @@ typedef long long mp_longint_impl_t;
#endif
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
#define MICROPY_ENABLE_FLOAT (1)
#define MICROPY_PY_BUILTINS_FLOAT (1)
#define MICROPY_FLOAT_C_FUN(fun) fun##f
typedef float mp_float_t;
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
#define MICROPY_ENABLE_FLOAT (1)
#define MICROPY_PY_BUILTINS_FLOAT (1)
#define MICROPY_FLOAT_C_FUN(fun) fun
typedef double mp_float_t;
#else
#define MICROPY_ENABLE_FLOAT (0)
#define MICROPY_PY_BUILTINS_FLOAT (0)
#endif
// Whether to provide "collections" module
#ifndef MICROPY_ENABLE_MOD_COLLECTIONS
#define MICROPY_ENABLE_MOD_COLLECTIONS (1)
#endif
// Whether to provide "math" module
#ifndef MICROPY_ENABLE_MOD_MATH
#define MICROPY_ENABLE_MOD_MATH (1)
#endif
// Whether to provide "cmath" module
#ifndef MICROPY_ENABLE_MOD_CMATH
#define MICROPY_ENABLE_MOD_CMATH (0)
#endif
// Whether to provide "io" module
#ifndef MICROPY_ENABLE_MOD_IO
#define MICROPY_ENABLE_MOD_IO (1)
#endif
// Whether to provide "struct" module
#ifndef MICROPY_ENABLE_MOD_STRUCT
#define MICROPY_ENABLE_MOD_STRUCT (1)
#endif
// Whether to provide "sys" module
#ifndef MICROPY_ENABLE_MOD_SYS
#define MICROPY_ENABLE_MOD_SYS (1)
#endif
#ifndef MICROPY_MOD_SYS_STDFILES
#define MICROPY_MOD_SYS_STDFILES (0)
#endif
// Whether to support slice object and correspondingly
// slice subscript operators
#ifndef MICROPY_ENABLE_SLICE
#define MICROPY_ENABLE_SLICE (1)
#endif
// Whether to support the property object
#ifndef MICROPY_ENABLE_PROPERTY
#define MICROPY_ENABLE_PROPERTY (1)
#ifndef MICROPY_PY_BUILTINS_COMPLEX
#define MICROPY_PY_BUILTINS_COMPLEX (MICROPY_PY_BUILTINS_FLOAT)
#endif
// Enable features which improve CPython compatibility
@@ -179,31 +241,138 @@ typedef double mp_float_t;
#define MICROPY_CPYTHON_COMPAT (1)
#endif
// Maximum length of a path in the filesystem
// So we can allocate a buffer on the stack for path manipulation in import
#ifndef MICROPY_PATH_MAX
#define MICROPY_PATH_MAX (512)
// Whether POSIX-semantics non-blocking streams are supported
#ifndef MICROPY_STREAMS_NON_BLOCK
#define MICROPY_STREAMS_NON_BLOCK (0)
#endif
// Whether to use computed gotos in the VM, or a switch
// Computed gotos are roughly 10% faster, and increase VM code size by a little
#ifndef MICROPY_USE_COMPUTED_GOTO
#define MICROPY_USE_COMPUTED_GOTO (0)
/*****************************************************************************/
/* Fine control over Python builtins, classes, modules, etc */
// Whether str object is proper unicode
#ifndef MICROPY_PY_BUILTINS_STR_UNICODE
#define MICROPY_PY_BUILTINS_STR_UNICODE (0)
#endif
// Whether to support bytearray object
#ifndef MICROPY_PY_BUILTINS_BYTEARRAY
#define MICROPY_PY_BUILTINS_BYTEARRAY (1)
#endif
// Whether to support set object
#ifndef MICROPY_PY_BUILTINS_SET
#define MICROPY_PY_BUILTINS_SET (1)
#endif
// Whether to support slice subscript operators and slice object
#ifndef MICROPY_PY_BUILTINS_SLICE
#define MICROPY_PY_BUILTINS_SLICE (1)
#endif
// Whether to support frozenset object
#ifndef MICROPY_PY_BUILTINS_FROZENSET
#define MICROPY_PY_BUILTINS_FROZENSET (0)
#endif
// Whether to support property object
#ifndef MICROPY_PY_BUILTINS_PROPERTY
#define MICROPY_PY_BUILTINS_PROPERTY (1)
#endif
// Whether to provide "array" module. Note that large chunk of the
// underlying code is shared with "bytearray" builtin type, so to
// get real savings, it should be disabled too.
#ifndef MICROPY_PY_ARRAY
#define MICROPY_PY_ARRAY (1)
#endif
// Whether to provide "collections" module
#ifndef MICROPY_PY_COLLECTIONS
#define MICROPY_PY_COLLECTIONS (1)
#endif
// Whether to provide "math" module
#ifndef MICROPY_PY_MATH
#define MICROPY_PY_MATH (1)
#endif
// Whether to provide "cmath" module
#ifndef MICROPY_PY_CMATH
#define MICROPY_PY_CMATH (0)
#endif
// Whether to provide "gc" module
#ifndef MICROPY_PY_GC
#define MICROPY_PY_GC (1)
#endif
// Whether to return number of collected objects from gc.collect()
#ifndef MICROPY_PY_GC_COLLECT_RETVAL
#define MICROPY_PY_GC_COLLECT_RETVAL (0)
#endif
// Whether to provide "io" module
#ifndef MICROPY_PY_IO
#define MICROPY_PY_IO (1)
#endif
// Whether to provide "io.FileIO" class
#ifndef MICROPY_PY_IO_FILEIO
#define MICROPY_PY_IO_FILEIO (0)
#endif
// Whether to provide "io.BytesIO" class
#ifndef MICROPY_PY_IO_BYTESIO
#define MICROPY_PY_IO_BYTESIO (1)
#endif
// Whether to provide "struct" module
#ifndef MICROPY_PY_STRUCT
#define MICROPY_PY_STRUCT (1)
#endif
// Whether to provide "sys" module
#ifndef MICROPY_PY_SYS
#define MICROPY_PY_SYS (1)
#endif
// Whether to provide "sys.maxsize" constant
#ifndef MICROPY_PY_SYS_MAXSIZE
#define MICROPY_PY_SYS_MAXSIZE (0)
#endif
// Whether to provide "sys.exit" function
#ifndef MICROPY_PY_SYS_EXIT
#define MICROPY_PY_SYS_EXIT (0)
#endif
// Whether to provide sys.{stdin,stdout,stderr} objects
#ifndef MICROPY_PY_SYS_STDFILES
#define MICROPY_PY_SYS_STDFILES (0)
#endif
// Extended modules
#ifndef MICROPY_PY_UCTYPES
#define MICROPY_PY_UCTYPES (0)
#endif
/*****************************************************************************/
/* Hooks for a port to add builtins */
// Additional builtin function definitions - see builtintables.c:builtin_object_table for format.
#ifndef MICROPY_EXTRA_BUILTINS
#define MICROPY_EXTRA_BUILTINS
#ifndef MICROPY_PORT_BUILTINS
#define MICROPY_PORT_BUILTINS
#endif
// Additional builtin module definitions - see builtintables.c:builtin_module_table for format.
#ifndef MICROPY_EXTRA_BUILTIN_MODULES
#define MICROPY_EXTRA_BUILTIN_MODULES
#ifndef MICROPY_PORT_BUILTIN_MODULES
#define MICROPY_PORT_BUILTIN_MODULES
#endif
// Additional constant definitions for the compiler - see compile.c:mp_constants_table.
#ifndef MICROPY_EXTRA_CONSTANTS
#define MICROPY_EXTRA_CONSTANTS
#ifndef MICROPY_PORT_CONSTANTS
#define MICROPY_PORT_CONSTANTS
#endif
/*****************************************************************************/
@@ -217,8 +386,8 @@ typedef double mp_float_t;
#define BITS_PER_BYTE (8)
#define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD)
// machine_int_t value with most significant bit set
#define WORD_MSBIT_HIGH (((machine_uint_t)1) << (BYTES_PER_WORD * 8 - 1))
// mp_int_t value with most significant bit set
#define WORD_MSBIT_HIGH (((mp_uint_t)1) << (BYTES_PER_WORD * 8 - 1))
#if !defined(MP_ENDIANNESS_LITTLE) && !defined(MP_ENDIANNESS_BIG)
// Just because most archs are such?
@@ -229,18 +398,25 @@ typedef double mp_float_t;
#define MP_ENDIANNESS_LITTLE (0)
#endif
// printf format spec to use for machine_int_t and friends
// printf format spec to use for mp_int_t and friends
#ifndef INT_FMT
#ifdef __LP64__
// Archs where machine_int_t == long, long != int
// Archs where mp_int_t == long, long != int
#define UINT_FMT "%lu"
#define INT_FMT "%ld"
#else
// Archs where machine_int_t == int
// Archs where mp_int_t == int
#define UINT_FMT "%u"
#define INT_FMT "%d"
#endif
#endif //INT_FMT
// Modifier for function which doesn't return
#ifndef NORETURN
#define NORETURN __attribute__((noreturn))
#endif
// Modifier for weak functions
#ifndef MP_WEAK
#define MP_WEAK __attribute__((weak))
#endif

147
py/mpz.c
View File

@@ -1,11 +1,37 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "mpz.h"
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
@@ -181,17 +207,47 @@ STATIC uint mpn_sub(mpz_dig_t *idig, const mpz_dig_t *jdig, uint jlen, const mpz
STATIC uint mpn_and(mpz_dig_t *idig, const mpz_dig_t *jdig, uint jlen, const mpz_dig_t *kdig, uint klen) {
mpz_dig_t *oidig = idig;
jlen -= klen;
for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
*idig = *jdig & *kdig;
}
for (; jlen > 0; --jlen, ++idig) {
*idig = 0;
// remove trailing zeros
for (--idig; idig >= oidig && *idig == 0; --idig) {
}
return idig - oidig;
return idig + 1 - oidig;
}
/* computes i = j & -k = j & (~k + 1)
returns number of digits in i
assumes enough memory in i; assumes normalised j, k
can have i, j, k pointing to same memory
*/
STATIC uint mpn_and_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, uint jlen, const mpz_dig_t *kdig, uint klen) {
mpz_dig_t *oidig = idig;
mpz_dbl_dig_t carry = 1;
for (; jlen > 0 && klen > 0; --jlen, --klen, ++idig, ++jdig, ++kdig) {
carry += *kdig ^ DIG_MASK;
*idig = (*jdig & carry) & DIG_MASK;
carry >>= DIG_SIZE;
}
for (; jlen > 0; --jlen, ++idig, ++jdig) {
carry += DIG_MASK;
*idig = (*jdig & carry) & DIG_MASK;
carry >>= DIG_SIZE;
}
if (carry != 0) {
*idig = carry;
} else {
// remove trailing zeros
for (--idig; idig >= oidig && *idig == 0; --idig) {
}
}
return idig + 1 - oidig;
}
/* computes i = j | k
@@ -295,7 +351,7 @@ STATIC uint mpn_mul(mpz_dig_t *idig, mpz_dig_t *jdig, uint jlen, mpz_dig_t *kdig
modifies den_dig memory, but restors it to original state at end
*/
STATIC void mpn_div(mpz_dig_t *num_dig, machine_uint_t *num_len, mpz_dig_t *den_dig, machine_uint_t den_len, mpz_dig_t *quo_dig, machine_uint_t *quo_len) {
STATIC void mpn_div(mpz_dig_t *num_dig, mp_uint_t *num_len, mpz_dig_t *den_dig, mp_uint_t den_len, mpz_dig_t *quo_dig, mp_uint_t *quo_len) {
mpz_dig_t *orig_num_dig = num_dig;
mpz_dig_t *orig_quo_dig = quo_dig;
mpz_dig_t norm_shift = 0;
@@ -446,12 +502,12 @@ void mpz_init_zero(mpz_t *z) {
z->dig = NULL;
}
void mpz_init_from_int(mpz_t *z, machine_int_t val) {
void mpz_init_from_int(mpz_t *z, mp_int_t val) {
mpz_init_zero(z);
mpz_set_from_int(z, val);
}
void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, uint alloc, machine_int_t val) {
void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, uint alloc, mp_int_t val) {
z->neg = 0;
z->fixed_dig = 1;
z->alloc = alloc;
@@ -472,7 +528,7 @@ mpz_t *mpz_zero(void) {
return z;
}
mpz_t *mpz_from_int(machine_int_t val) {
mpz_t *mpz_from_int(mp_int_t val) {
mpz_t *z = mpz_zero();
mpz_set_from_int(z, val);
return z;
@@ -538,10 +594,10 @@ void mpz_set(mpz_t *dest, const mpz_t *src) {
memcpy(dest->dig, src->dig, src->len * sizeof(mpz_dig_t));
}
void mpz_set_from_int(mpz_t *z, machine_int_t val) {
void mpz_set_from_int(mpz_t *z, mp_int_t val) {
mpz_need_dig(z, MPZ_NUM_DIG_FOR_INT);
machine_uint_t uval;
mp_uint_t uval;
if (val < 0) {
z->neg = 1;
uval = -val;
@@ -648,7 +704,7 @@ int mpz_cmp(const mpz_t *z1, const mpz_t *z2) {
#if 0
// obsolete
// compares mpz with an integer that fits within DIG_SIZE bits
int mpz_cmp_sml_int(const mpz_t *z, machine_int_t sml_int) {
int mpz_cmp_sml_int(const mpz_t *z, mp_int_t sml_int) {
int cmp;
if (z->neg == 0) {
if (sml_int < 0) return 1;
@@ -774,7 +830,7 @@ void mpz_not_inpl(mpz_t *dest, const mpz_t *z) {
/* computes dest = lhs << rhs
can have dest, lhs the same
*/
void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, machine_int_t rhs) {
void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, mp_int_t rhs) {
if (lhs->len == 0 || rhs == 0) {
mpz_set(dest, lhs);
} else if (rhs < 0) {
@@ -789,7 +845,7 @@ void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, machine_int_t rhs) {
/* computes dest = lhs >> rhs
can have dest, lhs the same
*/
void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, machine_int_t rhs) {
void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, mp_int_t rhs) {
if (lhs->len == 0 || rhs == 0) {
mpz_set(dest, lhs);
} else if (rhs < 0) {
@@ -872,23 +928,35 @@ void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
can have dest, lhs, rhs the same
*/
void mpz_and_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
if (mpn_cmp(lhs->dig, lhs->len, rhs->dig, rhs->len) < 0) {
const mpz_t *temp = lhs;
lhs = rhs;
rhs = temp;
}
if (lhs->neg == rhs->neg) {
mpz_need_dig(dest, lhs->len);
dest->len = mpn_and(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
if (lhs->neg == 0) {
// make sure lhs has the most digits
if (lhs->len < rhs->len) {
const mpz_t *temp = lhs;
lhs = rhs;
rhs = temp;
}
// do the and'ing
mpz_need_dig(dest, rhs->len);
dest->len = mpn_and(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
dest->neg = 0;
} else {
// TODO both args are negative
assert(0);
}
} else {
mpz_need_dig(dest, lhs->len);
// TODO
assert(0);
// dest->len = mpn_and_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
// args have different sign
// make sure lhs is the positive arg
if (rhs->neg == 0) {
const mpz_t *temp = lhs;
lhs = rhs;
rhs = temp;
}
mpz_need_dig(dest, lhs->len + 1);
dest->len = mpn_and_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
assert(dest->len <= dest->alloc);
dest->neg = 0;
}
dest->neg = lhs->neg;
}
/* computes dest = lhs | rhs
@@ -1066,8 +1134,11 @@ mpz_t *mpz_gcd(const mpz_t *z1, const mpz_t *z2) {
*/
mpz_t *mpz_lcm(const mpz_t *z1, const mpz_t *z2)
{
if (z1->len == 0 || z2->len == 0)
return mpz_zero();
// braces below are required for compilation to succeed with CL, see bug report
// https://connect.microsoft.com/VisualStudio/feedback/details/864169/compilation-error-when-braces-are-left-out-of-single-line-if-statement
if (z1->len == 0 || z2->len == 0) {
return mpz_zero();
}
mpz_t *gcd = mpz_gcd(z1, z2);
mpz_t *quo = mpz_zero();
@@ -1143,12 +1214,12 @@ mpz_t *mpz_mod(const mpz_t *lhs, const mpz_t *rhs) {
#endif
// TODO check that this correctly handles overflow in all cases
machine_int_t mpz_as_int(const mpz_t *i) {
machine_int_t val = 0;
mp_int_t mpz_as_int(const mpz_t *i) {
mp_int_t val = 0;
mpz_dig_t *d = i->dig + i->len;
while (--d >= i->dig) {
machine_int_t oldval = val;
mp_int_t oldval = val;
val = (val << DIG_SIZE) | *d;
if (val < oldval) {
// overflow, return +/- "infinity"
@@ -1170,12 +1241,12 @@ machine_int_t mpz_as_int(const mpz_t *i) {
}
// TODO check that this correctly handles overflow in all cases
bool mpz_as_int_checked(const mpz_t *i, machine_int_t *value) {
machine_int_t val = 0;
bool mpz_as_int_checked(const mpz_t *i, mp_int_t *value) {
mp_int_t val = 0;
mpz_dig_t *d = i->dig + i->len;
while (--d >= i->dig) {
machine_int_t oldval = val;
mp_int_t oldval = val;
val = (val << DIG_SIZE) | *d;
if (val < oldval) {
// overflow
@@ -1191,7 +1262,7 @@ bool mpz_as_int_checked(const mpz_t *i, machine_int_t *value) {
return true;
}
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
mp_float_t mpz_as_float(const mpz_t *i) {
mp_float_t val = 0;
mpz_dig_t *d = i->dig + i->len;

View File

@@ -1,29 +1,55 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
typedef uint16_t mpz_dig_t;
typedef uint32_t mpz_dbl_dig_t;
typedef int32_t mpz_dbl_dig_signed_t;
typedef struct _mpz_t {
machine_uint_t neg : 1;
machine_uint_t fixed_dig : 1;
machine_uint_t alloc : 30;
machine_uint_t len;
mp_uint_t neg : 1;
mp_uint_t fixed_dig : 1;
mp_uint_t alloc : 30;
mp_uint_t len;
mpz_dig_t *dig;
} mpz_t;
#define MPZ_DIG_SIZE (15) // see mpn_div for why this needs to be at most 15
#define MPZ_NUM_DIG_FOR_INT (sizeof(machine_int_t) * 8 / MPZ_DIG_SIZE + 1)
#define MPZ_NUM_DIG_FOR_INT (sizeof(mp_int_t) * 8 / MPZ_DIG_SIZE + 1)
#define MPZ_NUM_DIG_FOR_LL (sizeof(long long) * 8 / MPZ_DIG_SIZE + 1)
// convenience macro to declare an mpz with a digit array from the stack, initialised by an integer
#define MPZ_CONST_INT(z, val) mpz_t z; mpz_dig_t z ## _digits[MPZ_NUM_DIG_FOR_INT]; mpz_init_fixed_from_int(&z, z_digits, MPZ_NUM_DIG_FOR_INT, val);
void mpz_init_zero(mpz_t *z);
void mpz_init_from_int(mpz_t *z, machine_int_t val);
void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, uint dig_alloc, machine_int_t val);
void mpz_init_from_int(mpz_t *z, mp_int_t val);
void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, uint dig_alloc, mp_int_t val);
void mpz_deinit(mpz_t *z);
mpz_t *mpz_zero();
mpz_t *mpz_from_int(machine_int_t i);
mpz_t *mpz_from_int(mp_int_t i);
mpz_t *mpz_from_ll(long long i);
mpz_t *mpz_from_str(const char *str, uint len, bool neg, uint base);
void mpz_free(mpz_t *z);
@@ -31,7 +57,7 @@ void mpz_free(mpz_t *z);
mpz_t *mpz_clone(const mpz_t *src);
void mpz_set(mpz_t *dest, const mpz_t *src);
void mpz_set_from_int(mpz_t *z, machine_int_t src);
void mpz_set_from_int(mpz_t *z, mp_int_t src);
void mpz_set_from_ll(mpz_t *z, long long i);
uint mpz_set_from_str(mpz_t *z, const char *str, uint len, bool neg, uint base);
@@ -53,8 +79,8 @@ mpz_t *mpz_pow(const mpz_t *lhs, const mpz_t *rhs);
void mpz_abs_inpl(mpz_t *dest, const mpz_t *z);
void mpz_neg_inpl(mpz_t *dest, const mpz_t *z);
void mpz_not_inpl(mpz_t *dest, const mpz_t *z);
void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, machine_int_t rhs);
void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, machine_int_t rhs);
void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, mp_int_t rhs);
void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, mp_int_t rhs);
void mpz_add_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
@@ -70,9 +96,9 @@ void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const m
mpz_t *mpz_div(const mpz_t *lhs, const mpz_t *rhs);
mpz_t *mpz_mod(const mpz_t *lhs, const mpz_t *rhs);
machine_int_t mpz_as_int(const mpz_t *z);
bool mpz_as_int_checked(const mpz_t *z, machine_int_t *value);
#if MICROPY_ENABLE_FLOAT
mp_int_t mpz_as_int(const mpz_t *z);
bool mpz_as_int_checked(const mpz_t *z, mp_int_t *value);
#if MICROPY_PY_BUILTINS_FLOAT
mp_float_t mpz_as_float(const mpz_t *z);
#endif
uint mpz_as_str_size(const mpz_t *z, uint base);

View File

@@ -1,8 +1,35 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// non-local return
// exception handling, basically a stack of setjmp/longjmp buffers
#include <limits.h>
#include <setjmp.h>
#include <assert.h>
typedef struct _nlr_buf_t nlr_buf_t;
struct _nlr_buf_t {
@@ -18,7 +45,7 @@ struct _nlr_buf_t {
#else
void *regs[8];
#endif
#elif defined(__thumb2__)
#elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
void *regs[10];
#else
#define MICROPY_NLR_SETJMP (1)

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <setjmp.h>
#include <stdio.h>
#include "mpconfig.h"

View File

@@ -1,16 +1,44 @@
#if defined(__thumb2__) && !MICROPY_NLR_SETJMP
/* thumb callee save: bx, bp, sp, r12, r14, r14, r15 */
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#if !MICROPY_NLR_SETJMP && (defined(__thumb2__) || defined(__thumb__) || defined(__arm__))
/* arm callee save: bx, bp, sp, r12, r14, r14, r15 */
.syntax unified
/*.cpu cortex-m4*/
.thumb
/*.thumb*/
.text
.align 2
/* uint nlr_push(r0=nlr_buf_t *nlr) */
.global nlr_push
#if defined(__thumb2__)
.thumb
.thumb_func
#endif
.type nlr_push, %function
nlr_push:
str lr, [r0, #8] @ store lr into nlr_buf
@@ -38,8 +66,10 @@ nlr_push:
@ void nlr_pop()
.global nlr_pop
#if defined(__thumb2__)
.thumb
.thumb_func
#endif
.type nlr_pop, %function
nlr_pop:
ldr r3, .L5 @ load addr of nlr_top
@@ -54,8 +84,10 @@ nlr_pop:
/* void nlr_jump(r0=uint val) */
.global nlr_jump
#if defined(__thumb2__)
.thumb
.thumb_func
#endif
.type nlr_jump, %function
nlr_jump:
ldr r3, .L2 @ load addr of nlr_top

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#if defined(__x86_64__) && !MICROPY_NLR_SETJMP
/* x64 callee save: bx, bp, sp, r12, r13, r14, r15 */
@@ -6,6 +32,10 @@
#if !defined(__CYGWIN__)
#if (defined(__APPLE__) && defined(__MACH__))
#define nlr_jump_fail _nlr_jump_fail
#endif // (defined(__APPLE__) && defined(__MACH__))
/* uint nlr_push(rdi=nlr_buf_t *nlr) */
#if !(defined(__APPLE__) && defined(__MACH__))
.globl nlr_push

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#if defined(__i386__) && !MICROPY_NLR_SETJMP
/* x86 callee save: bx, di, si, bp, sp */

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
@@ -9,19 +35,20 @@
#include "obj.h"
#include "runtime0.h"
#include "runtime.h"
#include "stackctrl.h"
mp_obj_type_t *mp_obj_get_type(mp_obj_t o_in) {
mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
if (MP_OBJ_IS_SMALL_INT(o_in)) {
return (mp_obj_t)&mp_type_int;
} else if (MP_OBJ_IS_QSTR(o_in)) {
return (mp_obj_t)&mp_type_str;
} else {
mp_obj_base_t *o = o_in;
const mp_obj_base_t *o = o_in;
return (mp_obj_t)o->type;
}
}
const char *mp_obj_get_type_str(mp_obj_t o_in) {
const char *mp_obj_get_type_str(mp_const_obj_t o_in) {
return qstr_str(mp_obj_get_type(o_in)->name);
}
@@ -33,6 +60,14 @@ void printf_wrapper(void *env, const char *fmt, ...) {
}
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
// There can be data structures nested too deep, or just recursive
MP_STACK_CHECK();
#if !NDEBUG
if (o_in == NULL) {
print(env, "(nil)");
return;
}
#endif
mp_obj_type_t *type = mp_obj_get_type(o_in);
if (type->print != NULL) {
type->print(print, env, o_in, kind);
@@ -48,7 +83,7 @@ void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
// helper function to print an exception with traceback
void mp_obj_print_exception(mp_obj_t exc) {
if (mp_obj_is_exception_instance(exc)) {
machine_uint_t n, *values;
mp_uint_t n, *values;
mp_obj_exception_get_traceback(exc, &n, &values);
if (n > 0) {
assert(n % 3 == 0);
@@ -90,7 +125,7 @@ int mp_obj_is_true(mp_obj_t arg) {
mp_obj_type_t *type = mp_obj_get_type(arg);
if (type->unary_op != NULL) {
mp_obj_t result = type->unary_op(MP_UNARY_OP_BOOL, arg);
if (result != MP_OBJ_NOT_SUPPORTED) {
if (result != MP_OBJ_NULL) {
return result == mp_const_true;
}
}
@@ -110,7 +145,7 @@ bool mp_obj_is_callable(mp_obj_t o_in) {
return mp_obj_get_type(o_in)->call != NULL;
}
machine_int_t mp_obj_hash(mp_obj_t o_in) {
mp_int_t mp_obj_hash(mp_obj_t o_in) {
if (o_in == mp_const_false) {
return 0; // needs to hash to same as the integer 0, since False==0
} else if (o_in == mp_const_true) {
@@ -120,13 +155,13 @@ machine_int_t mp_obj_hash(mp_obj_t o_in) {
} else if (MP_OBJ_IS_STR(o_in) || MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
return mp_obj_str_get_hash(o_in);
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_NoneType)) {
return (machine_int_t)o_in;
return (mp_int_t)o_in;
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_fun_native) || MP_OBJ_IS_TYPE(o_in, &mp_type_fun_bc)) {
return (machine_int_t)o_in;
return (mp_int_t)o_in;
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_tuple)) {
return mp_obj_tuple_hash(o_in);
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_type)) {
return (machine_int_t)o_in;
return (mp_int_t)o_in;
// TODO hash class and instances
// TODO delegate to __hash__ method if it exists
@@ -180,7 +215,7 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
mp_obj_type_t *type = mp_obj_get_type(o1);
if (type->binary_op != NULL) {
mp_obj_t r = type->binary_op(MP_BINARY_OP_EQUAL, o1, o2);
if (r != MP_OBJ_NOT_SUPPORTED) {
if (r != MP_OBJ_NULL) {
return r == mp_const_true ? true : false;
}
}
@@ -190,7 +225,7 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
return false;
}
machine_int_t mp_obj_get_int(mp_obj_t arg) {
mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
// This function essentially performs implicit type conversion to int
// Note that Python does NOT provide implicit type conversion from
// float to int in the core expression language, try some_list[1.0].
@@ -209,8 +244,8 @@ machine_int_t mp_obj_get_int(mp_obj_t arg) {
// returns false if arg is not of integral type
// returns true and sets *value if it is of integral type
// can throw OverflowError if arg is of integral type, but doesn't fit in a machine_int_t
bool mp_obj_get_int_maybe(mp_obj_t arg, machine_int_t *value) {
// can throw OverflowError if arg is of integral type, but doesn't fit in a mp_int_t
bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value) {
if (arg == mp_const_false) {
*value = 0;
} else if (arg == mp_const_true) {
@@ -225,7 +260,7 @@ bool mp_obj_get_int_maybe(mp_obj_t arg, machine_int_t *value) {
return true;
}
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
mp_float_t mp_obj_get_float(mp_obj_t arg) {
if (arg == mp_const_false) {
return 0;
@@ -242,6 +277,7 @@ mp_float_t mp_obj_get_float(mp_obj_t arg) {
}
}
#if MICROPY_PY_BUILTINS_COMPLEX
void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
if (arg == mp_const_false) {
*real = 0;
@@ -265,6 +301,7 @@ void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
}
}
#endif
#endif
void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items) {
if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {
@@ -293,8 +330,8 @@ void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items) {
}
// is_slice determines whether the index is a slice index
uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index, bool is_slice) {
machine_int_t i;
uint mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice) {
mp_int_t i;
if (MP_OBJ_IS_SMALL_INT(index)) {
i = MP_OBJ_SMALL_INT_VALUE(index);
} else if (!mp_obj_get_int_maybe(index, &i)) {
@@ -320,8 +357,13 @@ uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index,
// may return MP_OBJ_NULL
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in) {
if (MP_OBJ_IS_STR(o_in) || MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
return MP_OBJ_NEW_SMALL_INT((machine_int_t)mp_obj_str_get_len(o_in));
if (
#if !MICROPY_PY_BUILTINS_STR_UNICODE
// It's simple - unicode is slow, non-unicode is fast
MP_OBJ_IS_STR(o_in) ||
#endif
MP_OBJ_IS_TYPE(o_in, &mp_type_bytes)) {
return MP_OBJ_NEW_SMALL_INT((mp_int_t)mp_obj_str_get_len(o_in));
} else {
mp_obj_type_t *type = mp_obj_get_type(o_in);
if (type->unary_op != NULL) {
@@ -336,7 +378,7 @@ mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
mp_obj_type_t *type = mp_obj_get_type(base);
if (type->subscr != NULL) {
mp_obj_t ret = type->subscr(base, index, value);
if (ret != MP_OBJ_NOT_SUPPORTED) {
if (ret != MP_OBJ_NULL) {
return ret;
}
// TODO: call base classes here?

187
py/obj.h
View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// A Micro Python object is a machine word having the following form:
// - xxxx...xxx1 : a small int, bits 1 and above are the value
// - xxxx...xx10 : a qstr, bits 2 and above are the value
@@ -9,11 +35,6 @@
typedef machine_ptr_t mp_obj_t;
typedef machine_const_ptr_t mp_const_obj_t;
// Integers that fit in a pointer have this type
// (do we need to expose this in the public API?)
typedef machine_int_t mp_small_int_t;
// Anything that wants to be a Micro Python object must have
// mp_obj_base_t as its first member (except small ints and qstrs)
@@ -26,8 +47,7 @@ typedef struct _mp_obj_base_t mp_obj_base_t;
// These fake objects are used to indicate certain things in arguments or return
// values, and should only be used when explicitly allowed.
//
// - MP_OBJ_NULL : used to indicate the absence of an object.
// - MP_OBJ_NOT_SUPPORTED : a return value that indicates an unsupported operation.
// - MP_OBJ_NULL : used to indicate the absence of an object, or unsupported operation.
// - MP_OBJ_STOP_ITERATION : used instead of throwing a StopIteration, for efficiency.
// - MP_OBJ_SENTINEL : used for various internal purposes where one needs
// an object which is unique from all other objects, including MP_OBJ_NULL.
@@ -37,35 +57,29 @@ typedef struct _mp_obj_base_t mp_obj_base_t;
#if NDEBUG
#define MP_OBJ_NULL ((mp_obj_t)0)
#define MP_OBJ_NOT_SUPPORTED ((mp_obj_t)0)
#define MP_OBJ_STOP_ITERATION ((mp_obj_t)0)
#define MP_OBJ_SENTINEL ((mp_obj_t)4)
#else
#define MP_OBJ_NULL ((mp_obj_t)0)
#define MP_OBJ_NOT_SUPPORTED ((mp_obj_t)4)
#define MP_OBJ_STOP_ITERATION ((mp_obj_t)8)
#define MP_OBJ_SENTINEL ((mp_obj_t)12)
#define MP_OBJ_STOP_ITERATION ((mp_obj_t)4)
#define MP_OBJ_SENTINEL ((mp_obj_t)8)
#endif
// These macros check for small int, qstr or object, and access small int and qstr values
// In SMALL_INT, next-to-highest bits is used as sign, so both must match for value in range
#define MP_SMALL_INT_MIN ((mp_small_int_t)(((machine_int_t)WORD_MSBIT_HIGH) >> 1))
#define MP_SMALL_INT_MAX ((mp_small_int_t)(~(MP_SMALL_INT_MIN)))
#define MP_OBJ_FITS_SMALL_INT(n) ((((n) ^ ((n) << 1)) & WORD_MSBIT_HIGH) == 0)
// these macros have now become inline functions; see below
//#define MP_OBJ_IS_SMALL_INT(o) ((((mp_small_int_t)(o)) & 1) != 0)
//#define MP_OBJ_IS_QSTR(o) ((((mp_small_int_t)(o)) & 3) == 2)
//#define MP_OBJ_IS_OBJ(o) ((((mp_small_int_t)(o)) & 3) == 0)
//#define MP_OBJ_IS_SMALL_INT(o) ((((mp_int_t)(o)) & 1) != 0)
//#define MP_OBJ_IS_QSTR(o) ((((mp_int_t)(o)) & 3) == 2)
//#define MP_OBJ_IS_OBJ(o) ((((mp_int_t)(o)) & 3) == 0)
#define MP_OBJ_IS_TYPE(o, t) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)(o))->type == (t))) // this does not work for checking a string, use below macro for that
#define MP_OBJ_IS_INT(o) (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int))
#define MP_OBJ_IS_STR(o) (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str))
#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_small_int_t)(o)) >> 1)
#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)(((small_int) << 1) | 1))
#define MP_OBJ_QSTR_VALUE(o) (((mp_small_int_t)(o)) >> 2)
#define MP_OBJ_NEW_QSTR(qstr) ((mp_obj_t)((((machine_uint_t)qstr) << 2) | 2))
#define MP_OBJ_QSTR_VALUE(o) (((mp_int_t)(o)) >> 2)
#define MP_OBJ_NEW_QSTR(qstr) ((mp_obj_t)((((mp_uint_t)qstr) << 2) | 2))
// These macros are used to declare and define constant function objects
// You can put "static" in front of the definitions to make them local
@@ -118,10 +132,10 @@ typedef struct _mp_map_elem_t {
// would also need a trucated dict structure
typedef struct _mp_map_t {
machine_uint_t all_keys_are_qstrs : 1;
machine_uint_t table_is_fixed_array : 1;
machine_uint_t used : (8 * sizeof(machine_uint_t) - 2);
machine_uint_t alloc;
mp_uint_t all_keys_are_qstrs : 1;
mp_uint_t table_is_fixed_array : 1;
mp_uint_t used : (8 * sizeof(mp_uint_t) - 2);
mp_uint_t alloc;
mp_map_elem_t *table;
} mp_map_t;
@@ -132,7 +146,7 @@ typedef enum _mp_map_lookup_kind_t {
MP_MAP_LOOKUP_REMOVE_IF_FOUND, // 2
} mp_map_lookup_kind_t;
static inline bool MP_MAP_SLOT_IS_FILLED(mp_map_t *map, machine_uint_t pos) { return ((map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL); }
static inline bool MP_MAP_SLOT_IS_FILLED(const mp_map_t *map, mp_uint_t pos) { return ((map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL); }
void mp_map_init(mp_map_t *map, int n);
void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table);
@@ -146,12 +160,12 @@ void mp_map_dump(mp_map_t *map);
// Underlying set implementation (not set object)
typedef struct _mp_set_t {
machine_uint_t alloc;
machine_uint_t used;
mp_uint_t alloc;
mp_uint_t used;
mp_obj_t *table;
} mp_set_t;
static inline bool MP_SET_SLOT_IS_FILLED(mp_set_t *set, machine_uint_t pos) { return ((set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL); }
static inline bool MP_SET_SLOT_IS_FILLED(const mp_set_t *set, mp_uint_t pos) { return ((set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL); }
void mp_set_init(mp_set_t *set, int n);
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
@@ -197,7 +211,7 @@ typedef struct _mp_buffer_info_t {
//int ver; // ?
void *buf;
machine_int_t len; // in bytes
mp_int_t len; // in bytes
int typecode; // as per binary.h
// Rationale: to load arbitrary-sized sprites directly to LCD
@@ -208,7 +222,7 @@ typedef struct _mp_buffer_info_t {
#define MP_BUFFER_WRITE (2)
#define MP_BUFFER_RW (MP_BUFFER_READ | MP_BUFFER_WRITE)
typedef struct _mp_buffer_p_t {
machine_int_t (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags);
mp_int_t (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags);
} mp_buffer_p_t;
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags);
void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags);
@@ -217,9 +231,10 @@ void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags);
typedef struct _mp_stream_p_t {
// On error, functions should return -1 and fill in *errcode (values are
// implementation-dependent, but will be exposed to user, e.g. via exception).
machine_int_t (*read)(mp_obj_t obj, void *buf, machine_uint_t size, int *errcode);
machine_int_t (*write)(mp_obj_t obj, const void *buf, machine_uint_t size, int *errcode);
mp_int_t (*read)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode);
mp_int_t (*write)(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode);
// add seek() ?
int is_bytes : 1;
} mp_stream_p_t;
struct _mp_obj_type_t {
@@ -229,18 +244,18 @@ struct _mp_obj_type_t {
mp_make_new_fun_t make_new; // to make an instance of the type
mp_call_fun_t call;
mp_unary_op_fun_t unary_op; // can return NULL if op not supported
mp_binary_op_fun_t binary_op; // can return NULL if op not supported
mp_unary_op_fun_t unary_op; // can return MP_OBJ_NULL if op not supported
mp_binary_op_fun_t binary_op; // can return MP_OBJ_NULL if op not supported
mp_load_attr_fun_t load_attr;
mp_store_attr_fun_t store_attr; // if value is MP_OBJ_NULL, then delete that attribute
mp_subscr_fun_t subscr; // implements load, store, delete subscripting
// value=MP_OBJ_NULL means delete, value=MP_OBJ_SENTINEL means load, else store
// can return MP_OBJ_NOT_SUPPORTED
// can return MP_OBJ_NULL if op not supported
mp_fun_1_t getiter;
mp_fun_1_t iternext; // may return MP_OBJ_NULL as an optimisation instead of raising StopIteration() (with no args)
mp_fun_1_t iternext; // may return MP_OBJ_STOP_ITERATION as an optimisation instead of raising StopIteration() (with no args)
mp_buffer_p_t buffer_p;
const mp_stream_p_t *stream_p;
@@ -282,6 +297,7 @@ extern const mp_obj_type_t mp_type_filter;
extern const mp_obj_type_t mp_type_dict;
extern const mp_obj_type_t mp_type_range;
extern const mp_obj_type_t mp_type_set;
extern const mp_obj_type_t mp_type_frozenset;
extern const mp_obj_type_t mp_type_slice;
extern const mp_obj_type_t mp_type_zip;
extern const mp_obj_type_t mp_type_array;
@@ -294,6 +310,7 @@ extern const mp_obj_type_t mp_type_staticmethod;
extern const mp_obj_type_t mp_type_classmethod;
extern const mp_obj_type_t mp_type_property;
extern const mp_obj_type_t mp_type_stringio;
extern const mp_obj_type_t mp_type_bytesio;
// Exceptions
extern const mp_obj_type_t mp_type_BaseException;
@@ -318,6 +335,7 @@ extern const mp_obj_type_t mp_type_RuntimeError;
extern const mp_obj_type_t mp_type_StopIteration;
extern const mp_obj_type_t mp_type_SyntaxError;
extern const mp_obj_type_t mp_type_SystemError;
extern const mp_obj_type_t mp_type_SystemExit;
extern const mp_obj_type_t mp_type_TypeError;
extern const mp_obj_type_t mp_type_ValueError;
extern const mp_obj_type_t mp_type_ZeroDivisionError;
@@ -342,13 +360,13 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
mp_obj_t mp_obj_new_none(void);
mp_obj_t mp_obj_new_bool(bool value);
mp_obj_t mp_obj_new_cell(mp_obj_t obj);
mp_obj_t mp_obj_new_int(machine_int_t value);
mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value);
mp_obj_t mp_obj_new_int_from_long_str(const char *s);
mp_obj_t mp_obj_new_int(mp_int_t value);
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value);
mp_obj_t mp_obj_new_int_from_str_len(const char **str, uint len, bool neg, uint base);
mp_obj_t mp_obj_new_int_from_ll(long long val); // this must return a multi-precision integer object (or raise an overflow exception)
mp_obj_t mp_obj_new_str(const byte* data, uint len, bool make_qstr_if_not_already);
mp_obj_t mp_obj_new_str(const char* data, uint len, bool make_qstr_if_not_already);
mp_obj_t mp_obj_new_bytes(const byte* data, uint len);
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
mp_obj_t mp_obj_new_float(mp_float_t val);
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
#endif
@@ -357,7 +375,7 @@ mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg);
mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args);
mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg);
mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!)
mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_pos_args, uint n_kwonly_args, mp_obj_t def_args, const byte *code);
mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_pos_args, uint n_kwonly_args, mp_obj_t def_args, mp_obj_t def_kw_args, const byte *code);
mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun);
mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
mp_obj_t mp_obj_new_closure(mp_obj_t fun, uint n_closed, const mp_obj_t *closed);
@@ -371,9 +389,10 @@ mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self);
mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args);
mp_obj_t mp_obj_new_module(qstr module_name);
mp_obj_type_t *mp_obj_get_type(mp_obj_t o_in);
const char *mp_obj_get_type_str(mp_obj_t o_in);
mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
const char *mp_obj_get_type_str(mp_const_obj_t o_in);
bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects
mp_obj_t mp_instance_cast_to_native_base(mp_const_obj_t self_in, mp_const_obj_t native_type);
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind);
void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
@@ -382,47 +401,47 @@ void mp_obj_print_exception(mp_obj_t exc);
int mp_obj_is_true(mp_obj_t arg);
// TODO make these all lower case when they have proven themselves
static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o) { return ((((mp_small_int_t)(o)) & 3) == 0); }
static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o) { return ((((mp_small_int_t)(o)) & 1) != 0); }
static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o) { return ((((mp_int_t)(o)) & 3) == 0); }
static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o) { return ((((mp_int_t)(o)) & 1) != 0); }
//static inline bool MP_OBJ_IS_TYPE(mp_const_obj_t o, const mp_obj_type_t *t) { return (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)(o))->type == (t))); } // this does not work for checking a string, use below macro for that
//static inline bool MP_OBJ_IS_INT(mp_const_obj_t o) { return (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int)); } // returns true if o is a small int or long int
static inline bool mp_obj_is_integer(mp_const_obj_t o) { return MP_OBJ_IS_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_bool); } // returns true if o is bool, small int or long int
static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o) { return ((((mp_small_int_t)(o)) & 3) == 2); }
static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o) { return ((((mp_int_t)(o)) & 3) == 2); }
//static inline bool MP_OBJ_IS_STR(mp_const_obj_t o) { return (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str)); }
bool mp_obj_is_callable(mp_obj_t o_in);
machine_int_t mp_obj_hash(mp_obj_t o_in);
mp_int_t mp_obj_hash(mp_obj_t o_in);
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
machine_int_t mp_obj_get_int(mp_obj_t arg);
bool mp_obj_get_int_maybe(mp_obj_t arg, machine_int_t *value);
#if MICROPY_ENABLE_FLOAT
mp_int_t mp_obj_get_int(mp_const_obj_t arg);
bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);
#if MICROPY_PY_BUILTINS_FLOAT
mp_float_t mp_obj_get_float(mp_obj_t self_in);
void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
#endif
//qstr mp_obj_get_qstr(mp_obj_t arg);
void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items);
void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items);
uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index, bool is_slice);
uint mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index, bool is_slice);
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); /* may return MP_OBJ_NULL */
mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
// bool
// TODO make lower case when it has proven itself
static inline mp_obj_t MP_BOOL(machine_int_t x) { return x ? mp_const_true : mp_const_false; }
static inline mp_obj_t MP_BOOL(mp_int_t x) { return x ? mp_const_true : mp_const_false; }
// cell
mp_obj_t mp_obj_cell_get(mp_obj_t self_in);
void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
// int
// For long int, returns value truncated to machine_int_t
machine_int_t mp_obj_int_get(mp_obj_t self_in);
#if MICROPY_ENABLE_FLOAT
// For long int, returns value truncated to mp_int_t
mp_int_t mp_obj_int_get(mp_const_obj_t self_in);
#if MICROPY_PY_BUILTINS_FLOAT
mp_float_t mp_obj_int_as_float(mp_obj_t self_in);
#endif
// Will raise exception if value doesn't fit into machine_int_t
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in);
// Will raise exception if value doesn't fit into mp_int_t
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
// exception
#define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)
@@ -430,8 +449,8 @@ bool mp_obj_is_exception_type(mp_obj_t self_in);
bool mp_obj_is_exception_instance(mp_obj_t self_in);
bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type);
void mp_obj_exception_clear_traceback(mp_obj_t self_in);
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block);
void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values);
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, mp_uint_t line, qstr block);
void mp_obj_exception_get_traceback(mp_obj_t self_in, mp_uint_t *n, mp_uint_t **values);
mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args);
@@ -444,27 +463,27 @@ uint mp_obj_str_get_len(mp_obj_t self_in);
qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr
const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
const char *mp_obj_str_get_data(mp_obj_t self_in, uint *len);
void mp_str_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len);
mp_obj_t mp_obj_str_intern(mp_obj_t str);
void mp_str_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len, bool is_bytes);
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
// float
typedef struct _mp_obj_float_t {
mp_obj_base_t base;
mp_float_t value;
} mp_obj_float_t;
mp_float_t mp_obj_float_get(mp_obj_t self_in);
mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs); // can return MP_OBJ_NULL
mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs); // can return MP_OBJ_NULL if op not supported
// complex
void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in); // can return MP_OBJ_NULL
mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in); // can return MP_OBJ_NULL if op not supported
#endif
// tuple
void mp_obj_tuple_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
void mp_obj_tuple_del(mp_obj_t self_in);
machine_int_t mp_obj_tuple_hash(mp_obj_t self_in);
mp_obj_t mp_obj_tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args);
mp_int_t mp_obj_tuple_hash(mp_obj_t self_in);
// list
struct _mp_obj_list_t;
@@ -482,6 +501,7 @@ typedef struct _mp_obj_dict_t {
} mp_obj_dict_t;
void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args);
uint mp_obj_dict_len(mp_obj_t self_in);
mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in);
@@ -490,7 +510,7 @@ mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in);
void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
// slice
void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *stop, machine_int_t *step);
void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_t *step);
// array
uint mp_obj_array_len(mp_obj_t self_in);
@@ -509,9 +529,7 @@ typedef struct _mp_obj_fun_native_t { // need this so we can define const object
// such functions won't be able to access the global scope, but that's probably okay
} mp_obj_fun_native_t;
bool mp_obj_fun_prepare_simple_args(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args,
uint *out_args1_len, const mp_obj_t **out_args1, uint *out_args2_len, const mp_obj_t **out_args2);
const char *mp_obj_fun_get_name(mp_obj_t fun);
const char *mp_obj_fun_get_name(mp_const_obj_t fun);
const char *mp_obj_code_get_name(const byte *code_info);
mp_obj_t mp_identity(mp_obj_t self);
@@ -536,13 +554,34 @@ typedef struct _mp_obj_static_class_method_t {
const mp_obj_t *mp_obj_property_get(mp_obj_t self_in);
// sequence helpers
// slice indexes resolved to particular sequence
typedef struct {
mp_uint_t start;
mp_uint_t stop;
mp_int_t step;
} mp_bound_slice_t;
void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void *dest);
bool m_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_uint_t *begin, machine_uint_t *end);
#define m_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t))
#define m_seq_cat(dest, src1, len1, src2, len2, item_t) { memcpy(dest, src1, (len1) * sizeof(item_t)); memcpy(dest + (len1), src2, (len2) * sizeof(item_t)); }
#if MICROPY_PY_BUILTINS_SLICE
bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes);
#endif
#define mp_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t))
#define mp_seq_cat(dest, src1, len1, src2, len2, item_t) { memcpy(dest, src1, (len1) * sizeof(item_t)); memcpy(dest + (len1), src2, (len2) * sizeof(item_t)); }
bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, uint len2);
bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *items2, uint len2);
mp_obj_t mp_seq_index_obj(const mp_obj_t *items, uint len, uint n_args, const mp_obj_t *args);
mp_obj_t mp_seq_count_obj(const mp_obj_t *items, uint len, mp_obj_t value);
mp_obj_t mp_seq_extract_slice(uint len, const mp_obj_t *seq, mp_bound_slice_t *indexes);
// Helper to clear stale pointers from allocated, but unused memory, to preclude GC problems
#define mp_seq_clear(start, len, alloc_len, item_sz) memset((byte*)(start) + (len) * (item_sz), 0, ((alloc_len) - (len)) * (item_sz))
#define mp_seq_replace_slice_no_grow(dest, dest_len, beg, end, slice, slice_len, item_t) \
/*printf("memcpy(%p, %p, %d)\n", dest + beg, slice, slice_len * sizeof(item_t));*/ \
memcpy(dest + beg, slice, slice_len * sizeof(item_t)); \
/*printf("memcpy(%p, %p, %d)\n", dest + (beg + slice_len), dest + end, (dest_len - end) * sizeof(item_t));*/ \
memcpy(dest + (beg + slice_len), dest + end, (dest_len - end) * sizeof(item_t));
#define mp_seq_replace_slice_grow_inplace(dest, dest_len, beg, end, slice, slice_len, len_adj, item_t) \
/*printf("memmove(%p, %p, %d)\n", dest + beg + len_adj, dest + beg, (dest_len - beg) * sizeof(item_t));*/ \
memmove(dest + beg + len_adj, dest + beg, (dest_len - beg) * sizeof(item_t)); \
memcpy(dest + beg, slice, slice_len * sizeof(item_t));

View File

@@ -1,3 +1,30 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2014 Paul Sokolovsky
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include <assert.h>
@@ -10,13 +37,15 @@
#include "runtime.h"
#include "binary.h"
#if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY
typedef struct _mp_obj_array_t {
mp_obj_base_t base;
machine_uint_t typecode : 8;
mp_uint_t typecode : 8;
// free is number of unused elements after len used elements
// alloc size = len + free
machine_uint_t free : (8 * sizeof(machine_uint_t) - 8);
machine_uint_t len; // in elements
mp_uint_t free : (8 * sizeof(mp_uint_t) - 8);
mp_uint_t len; // in elements
void *items;
} mp_obj_array_t;
@@ -31,7 +60,7 @@ STATIC void array_print(void (*print)(void *env, const char *fmt, ...), void *en
mp_obj_array_t *o = o_in;
if (o->typecode == BYTEARRAY_TYPECODE) {
print(env, "bytearray(b", o->typecode);
mp_str_print_quoted(print, env, o->items, o->len);
mp_str_print_quoted(print, env, o->items, o->len, true);
} else {
print(env, "array('%c'", o->typecode);
if (o->len > 0) {
@@ -113,7 +142,7 @@ STATIC mp_obj_t array_unary_op(int op, mp_obj_t o_in) {
switch (op) {
case MP_UNARY_OP_BOOL: return MP_BOOL(o->len != 0);
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(o->len);
default: return MP_OBJ_NOT_SUPPORTED;
default: return MP_OBJ_NULL; // op not supported
}
}
@@ -139,26 +168,30 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
// TODO implement
// TODO: confirmed that both bytearray and array.array support
// slice deletion
return MP_OBJ_NOT_SUPPORTED;
return MP_OBJ_NULL; // op not supported
} else {
mp_obj_array_t *o = self_in;
if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
if (0) {
#if MICROPY_PY_BUILTINS_SLICE
} else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
if (value != MP_OBJ_SENTINEL) {
// Only getting a slice is suported so far, not assignment
// TODO: confirmed that both bytearray and array.array support
// slice assignment (incl. of different size)
return MP_OBJ_NOT_SUPPORTED;
return MP_OBJ_NULL; // op not supported
}
machine_uint_t start, stop;
if (!m_seq_get_fast_slice_indexes(o->len, index_in, &start, &stop)) {
assert(0);
mp_bound_slice_t slice;
if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
"only slices with step=1 (aka None) are supported"));
}
mp_obj_array_t *res = array_new(o->typecode, stop - start);
mp_obj_array_t *res = array_new(o->typecode, slice.stop - slice.start);
int sz = mp_binary_get_size('@', o->typecode, NULL);
assert(sz > 0);
byte *p = o->items;
memcpy(res->items, p + start * sz, (stop - start) * sz);
memcpy(res->items, p + slice.start * sz, (slice.stop - slice.start) * sz);
return res;
#endif
} else {
uint index = mp_get_index(o->base.type, o->len, index_in, false);
if (value == MP_OBJ_SENTINEL) {
@@ -173,7 +206,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
}
}
STATIC machine_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, int flags) {
STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, int flags) {
mp_obj_array_t *o = o_in;
bufinfo->buf = o->items;
bufinfo->len = o->len * mp_binary_get_size('@', o->typecode, NULL);
@@ -252,7 +285,7 @@ mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items) {
typedef struct _mp_obj_array_it_t {
mp_obj_base_t base;
mp_obj_array_t *array;
machine_uint_t cur;
mp_uint_t cur;
} mp_obj_array_it_t;
STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
@@ -279,3 +312,5 @@ STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
o->cur = 0;
return o;
}
#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY

View File

@@ -1 +1,27 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
mp_obj_t mp_obj_new_bytearray(uint n, void *items);

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include "mpconfig.h"
@@ -23,17 +49,19 @@ STATIC void bool_print(void (*print)(void *env, const char *fmt, ...), void *env
}
STATIC mp_obj_t bool_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// TODO check n_kw == 0
mp_arg_check_num(n_args, n_kw, 0, 1, false);
switch (n_args) {
case 0: return mp_const_false;
case 1: if (mp_obj_is_true(args[0])) { return mp_const_true; } else { return mp_const_false; }
default: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "bool takes at most 1 argument, %d given", n_args));
case 0:
return mp_const_false;
case 1:
default: // must be 0 or 1
if (mp_obj_is_true(args[0])) { return mp_const_true; } else { return mp_const_false; }
}
}
STATIC mp_obj_t bool_unary_op(int op, mp_obj_t o_in) {
machine_int_t value = ((mp_obj_bool_t*)o_in)->value;
mp_int_t value = ((mp_obj_bool_t*)o_in)->value;
switch (op) {
case MP_UNARY_OP_BOOL: return o_in;
case MP_UNARY_OP_POSITIVE: return MP_OBJ_NEW_SMALL_INT(value);
@@ -46,9 +74,9 @@ STATIC mp_obj_t bool_unary_op(int op, mp_obj_t o_in) {
STATIC mp_obj_t bool_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
if (MP_BINARY_OP_OR <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
return mp_binary_op(op, MP_OBJ_NEW_SMALL_INT((machine_int_t)mp_obj_is_true(lhs_in)), rhs_in);
return mp_binary_op(op, MP_OBJ_NEW_SMALL_INT((mp_int_t)mp_obj_is_true(lhs_in)), rhs_in);
}
return MP_OBJ_NOT_SUPPORTED;
return MP_OBJ_NULL; // op not supported
}
const mp_obj_type_t mp_type_bool = {

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include "mpconfig.h"
@@ -13,6 +39,17 @@ typedef struct _mp_obj_bound_meth_t {
mp_obj_t self;
} mp_obj_bound_meth_t;
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
STATIC void bound_meth_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
mp_obj_bound_meth_t *o = o_in;
print(env, "<bound_method %p ", o);
mp_obj_print_helper(print, env, o->self, PRINT_REPR);
print(env, ".");
mp_obj_print_helper(print, env, o->meth, PRINT_REPR);
print(env, ">");
}
#endif
mp_obj_t bound_meth_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
mp_obj_bound_meth_t *self = self_in;
@@ -39,6 +76,9 @@ mp_obj_t bound_meth_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_
const mp_obj_type_t bound_meth_type = {
{ &mp_type_type },
.name = MP_QSTR_bound_method,
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
.print = bound_meth_print,
#endif
.call = bound_meth_call,
};

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include "mpconfig.h"
@@ -11,7 +37,7 @@
typedef struct _mp_obj_closure_t {
mp_obj_base_t base;
mp_obj_t fun;
machine_uint_t n_closed;
mp_uint_t n_closed;
mp_obj_t closed[];
} mp_obj_closure_t;

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <assert.h>
@@ -8,8 +34,9 @@
#include "obj.h"
#include "parsenum.h"
#include "runtime0.h"
#include "runtime.h"
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_COMPLEX
#include <math.h>
@@ -48,7 +75,7 @@ STATIC void complex_print(void (*print)(void *env, const char *fmt, ...), void *
}
STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// TODO check n_kw == 0
mp_arg_check_num(n_args, n_kw, 0, 2, false);
switch (n_args) {
case 0:
@@ -68,7 +95,8 @@ STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
}
case 2: {
case 2:
default: {
mp_float_t real, imag;
if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
mp_obj_complex_get(args[0], &real, &imag);
@@ -86,9 +114,6 @@ STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
}
return mp_obj_new_complex(real, imag);
}
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "complex takes at most 2 arguments, %d given", n_args));
}
}
@@ -98,7 +123,7 @@ STATIC mp_obj_t complex_unary_op(int op, mp_obj_t o_in) {
case MP_UNARY_OP_BOOL: return MP_BOOL(o->real != 0 || o->imag != 0);
case MP_UNARY_OP_POSITIVE: return o_in;
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
default: return MP_OBJ_NOT_SUPPORTED;
default: return MP_OBJ_NULL; // op not supported
}
}
@@ -208,7 +233,7 @@ mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_im
case MP_BINARY_OP_EQUAL: return MP_BOOL(lhs_real == rhs_real && lhs_imag == rhs_imag);
default:
return MP_OBJ_NOT_SUPPORTED;
return MP_OBJ_NULL; // op not supported
}
return mp_obj_new_complex(lhs_real, lhs_imag);
}

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdbool.h>
#include <string.h>
#include <assert.h>
@@ -14,7 +40,7 @@
STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur);
STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in);
STATIC mp_obj_t dict_copy(mp_obj_t self_in);
STATIC mp_obj_t dict_update(uint n_args, const mp_obj_t *args, mp_map_t *kwargs);
STATIC void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
mp_obj_dict_t *self = self_in;
@@ -35,40 +61,13 @@ STATIC void dict_print(void (*print)(void *env, const char *fmt, ...), void *env
}
STATIC mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
mp_obj_t dict;
switch (n_args) {
case 0:
dict = mp_obj_new_dict(0);
break;
case 1: {
if (MP_OBJ_IS_TYPE(args[0], &mp_type_dict)) {
return dict_copy(args[0]);
}
// TODO create dict from an arbitrary mapping!
// Make dict from iterable of pairs
mp_obj_t iterable = mp_getiter(args[0]);
mp_obj_t dict = mp_obj_new_dict(0);
// TODO: support arbitrary seq as a pair
mp_obj_t item;
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
mp_obj_t *sub_items;
mp_obj_get_array_fixed_n(item, 2, &sub_items);
mp_obj_dict_store(dict, sub_items[0], sub_items[1]);
}
return dict;
}
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "dict takes at most 1 argument"));
mp_obj_t dict = mp_obj_new_dict(0);
if (n_args > 0 || n_kw > 0) {
mp_obj_t args2[2] = {dict, args[0]}; // args[0] is always valid, even if it's not a positional arg
mp_map_t kwargs;
mp_map_init_fixed_table(&kwargs, n_kw, args + n_args);
dict_update(n_args + 1, args2, &kwargs); // dict_update will check that n_args + 1 == 1 or 2
}
// add to the new dict any keyword args
for (const mp_obj_t *a = args + n_args; n_kw > 0; n_kw--, a += 2) {
mp_obj_dict_store(dict, a[0], a[1]);
}
return dict;
}
@@ -76,8 +75,8 @@ STATIC mp_obj_t dict_unary_op(int op, mp_obj_t self_in) {
mp_obj_dict_t *self = self_in;
switch (op) {
case MP_UNARY_OP_BOOL: return MP_BOOL(self->map.used != 0);
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT((machine_int_t)self->map.used);
default: return MP_OBJ_NOT_SUPPORTED;
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT((mp_int_t)self->map.used);
default: return MP_OBJ_NULL; // op not supported
}
}
@@ -95,10 +94,10 @@ STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
return mp_const_false;
}
machine_uint_t size = o->map.alloc;
mp_uint_t size = o->map.alloc;
mp_map_t *map = &o->map;
for (machine_uint_t i = 0; i < size; i++) {
for (mp_uint_t i = 0; i < size; i++) {
if (MP_MAP_SLOT_IS_FILLED(map, i)) {
mp_map_elem_t *elem = mp_map_lookup(&rhs->map, map->table[i].key, MP_MAP_LOOKUP);
if (elem == NULL || !mp_obj_equal(map->table[i].value, elem->value)) {
@@ -114,7 +113,18 @@ STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
}
default:
// op not supported
return MP_OBJ_NOT_SUPPORTED;
return MP_OBJ_NULL;
}
}
// TODO: Make sure this is inlined in dict_subscr() below.
mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index) {
mp_obj_dict_t *self = self_in;
mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
if (elem == NULL) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
} else {
return elem->value;
}
}
@@ -145,12 +155,12 @@ STATIC mp_obj_t dict_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
typedef struct _mp_obj_dict_it_t {
mp_obj_base_t base;
mp_obj_dict_t *dict;
machine_uint_t cur;
mp_uint_t cur;
} mp_obj_dict_it_t;
STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in) {
mp_obj_dict_it_t *self = self_in;
machine_uint_t max = self->dict->map.alloc;
mp_uint_t max = self->dict->map.alloc;
mp_map_t *map = &self->dict->map;
for (int i = self->cur; i < max; i++) {
@@ -322,31 +332,57 @@ STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
STATIC mp_obj_t dict_update(mp_obj_t self_in, mp_obj_t iterable) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
mp_obj_dict_t *self = self_in;
/* TODO: check for the "keys" method */
mp_obj_t iter = mp_getiter(iterable);
mp_obj_t next = NULL;
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
mp_obj_t inneriter = mp_getiter(next);
mp_obj_t key = mp_iternext(inneriter);
mp_obj_t value = mp_iternext(inneriter);
mp_obj_t stop = mp_iternext(inneriter);
if (key == MP_OBJ_STOP_ITERATION
|| value == MP_OBJ_STOP_ITERATION
|| stop != MP_OBJ_STOP_ITERATION) {
nlr_raise(mp_obj_new_exception_msg(
&mp_type_ValueError,
"dictionary update sequence has the wrong length"));
STATIC mp_obj_t dict_update(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
mp_obj_dict_t *self = args[0];
mp_arg_check_num(n_args, kwargs->used, 1, 2, true);
if (n_args == 2) {
// given a positional argument
if (MP_OBJ_IS_TYPE(args[1], &mp_type_dict)) {
// update from other dictionary (make sure other is not self)
if (args[1] != self) {
// TODO don't allocate heap object for this iterator
mp_obj_t *dict_iter = mp_obj_new_dict_iterator(args[1], 0);
mp_map_elem_t *elem = NULL;
while ((elem = dict_it_iternext_elem(dict_iter)) != MP_OBJ_STOP_ITERATION) {
mp_map_lookup(&self->map, elem->key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = elem->value;
}
}
} else {
mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
// update from a generic iterable of pairs
mp_obj_t iter = mp_getiter(args[1]);
mp_obj_t next = NULL;
while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
mp_obj_t inneriter = mp_getiter(next);
mp_obj_t key = mp_iternext(inneriter);
mp_obj_t value = mp_iternext(inneriter);
mp_obj_t stop = mp_iternext(inneriter);
if (key == MP_OBJ_STOP_ITERATION
|| value == MP_OBJ_STOP_ITERATION
|| stop != MP_OBJ_STOP_ITERATION) {
nlr_raise(mp_obj_new_exception_msg(
&mp_type_ValueError,
"dictionary update sequence has the wrong length"));
} else {
mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
}
}
}
}
// update the dict with any keyword args
for (mp_uint_t i = 0; i < kwargs->alloc; i++) {
if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
mp_map_lookup(&self->map, kwargs->table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = kwargs->table[i].value;
}
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(dict_update_obj, dict_update);
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(dict_update_obj, 1, dict_update);
/******************************************************************************/
@@ -367,7 +403,7 @@ typedef struct _mp_obj_dict_view_it_t {
mp_obj_base_t base;
mp_dict_view_kind_t kind;
mp_obj_dict_it_t *iter;
machine_uint_t cur;
mp_uint_t cur;
} mp_obj_dict_view_it_t;
typedef struct _mp_obj_dict_view_t {
@@ -437,14 +473,17 @@ STATIC void dict_view_print(void (*print)(void *env, const char *fmt, ...), void
}
STATIC mp_obj_t dict_view_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
/* only supported for the 'keys' kind until sets and dicts are refactored */
// only supported for the 'keys' kind until sets and dicts are refactored
mp_obj_dict_view_t *o = lhs_in;
if (o->kind != MP_DICT_VIEW_KEYS) return NULL;
if (op != MP_BINARY_OP_IN) return NULL;
if (o->kind != MP_DICT_VIEW_KEYS) {
return MP_OBJ_NULL; // op not supported
}
if (op != MP_BINARY_OP_IN) {
return MP_OBJ_NULL; // op not supported
}
return dict_binary_op(op, o->dict, rhs_in);
}
STATIC const mp_obj_type_t dict_view_type = {
{ &mp_type_type },
.name = MP_QSTR_dict_view,

View File

@@ -1,8 +1,34 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <assert.h>
#include "misc.h"
#include "mpconfig.h"
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
@@ -10,19 +36,35 @@
typedef struct _mp_obj_enumerate_t {
mp_obj_base_t base;
mp_obj_t iter;
machine_int_t cur;
mp_int_t cur;
} mp_obj_enumerate_t;
STATIC mp_obj_t enumerate_iternext(mp_obj_t self_in);
/* TODO: enumerate is one of the ones that can take args or kwargs.
Sticking to args for now */
STATIC const mp_arg_t enumerate_make_new_args[] = {
{ MP_QSTR_iterable, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_start, MP_ARG_INT, {.u_int = 0} },
};
#define ENUMERATE_MAKE_NEW_NUM_ARGS MP_ARRAY_SIZE(enumerate_make_new_args)
STATIC mp_obj_t enumerate_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
assert(n_args > 0);
#if MICROPY_CPYTHON_COMPAT
// parse args
mp_arg_val_t vals[ENUMERATE_MAKE_NEW_NUM_ARGS];
mp_arg_parse_all_kw_array(n_args, n_kw, args, ENUMERATE_MAKE_NEW_NUM_ARGS, enumerate_make_new_args, vals);
// create enumerate object
mp_obj_enumerate_t *o = m_new_obj(mp_obj_enumerate_t);
o->base.type = &mp_type_enumerate;
o->iter = mp_getiter(vals[0].u_obj);
o->cur = vals[1].u_int;
#else
mp_obj_enumerate_t *o = m_new_obj(mp_obj_enumerate_t);
o->base.type = &mp_type_enumerate;
o->iter = mp_getiter(args[0]);
o->cur = n_args > 1 ? mp_obj_get_int(args[1]) : 0;
#endif
return o;
}

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include <stdarg.h>
#include <assert.h>
@@ -11,6 +37,7 @@
#include "objtype.h"
#include "runtime.h"
#include "runtime0.h"
#include "gc.h"
typedef struct _mp_obj_exception_t {
mp_obj_base_t base;
@@ -50,7 +77,7 @@ STATIC void mp_obj_exception_print(void (*print)(void *env, const char *fmt, ...
return;
}
}
tuple_print(print, env, o->args, kind);
mp_obj_tuple_print(print, env, o->args, kind);
}
mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
@@ -94,12 +121,27 @@ STATIC void exception_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
}
}
STATIC mp_obj_t exc___init__(uint n_args, const mp_obj_t *args) {
mp_obj_exception_t *self = args[0];
mp_obj_t argst = mp_obj_new_tuple(n_args - 1, args + 1);
self->args = argst;
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(exc___init___obj, 1, MP_OBJ_FUN_ARGS_MAX, exc___init__);
STATIC const mp_map_elem_t exc_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&exc___init___obj },
};
STATIC MP_DEFINE_CONST_DICT(exc_locals_dict, exc_locals_dict_table);
const mp_obj_type_t mp_type_BaseException = {
{ &mp_type_type },
.name = MP_QSTR_BaseException,
.print = mp_obj_exception_print,
.make_new = mp_obj_exception_make_new,
.load_attr = exception_load_attr,
.locals_dict = (mp_obj_t)&exc_locals_dict,
};
#define MP_DEFINE_EXCEPTION_BASE(base_name) \
@@ -116,9 +158,9 @@ const mp_obj_type_t mp_type_ ## exc_name = { \
};
// List of all exceptions, arranged as in the table at:
// http://docs.python.org/3.3/library/exceptions.html
// http://docs.python.org/3/library/exceptions.html
MP_DEFINE_EXCEPTION_BASE(BaseException)
//MP_DEFINE_EXCEPTION(SystemExit, BaseException)
MP_DEFINE_EXCEPTION(SystemExit, BaseException)
//MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
MP_DEFINE_EXCEPTION(Exception, BaseException)
@@ -142,11 +184,13 @@ MP_DEFINE_EXCEPTION(Exception, BaseException)
MP_DEFINE_EXCEPTION(KeyError, LookupError)
MP_DEFINE_EXCEPTION(MemoryError, Exception)
MP_DEFINE_EXCEPTION(NameError, Exception)
MP_DEFINE_EXCEPTION_BASE(NameError)
//MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
MP_DEFINE_EXCEPTION(OSError, Exception)
MP_DEFINE_EXCEPTION_BASE(OSError)
/*
MP_DEFINE_EXCEPTION_BASE(NameError)
MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
*/
MP_DEFINE_EXCEPTION(OSError, Exception)
/*
MP_DEFINE_EXCEPTION_BASE(OSError)
MP_DEFINE_EXCEPTION(BlockingIOError, OSError)
MP_DEFINE_EXCEPTION(ChildProcessError, OSError)
MP_DEFINE_EXCEPTION(ConnectionError, OSError)
@@ -240,7 +284,7 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
va_start(ap, fmt);
vstr_vprintf(vstr, fmt, ap);
va_end(ap);
o->args->items[0] = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
o->args->items[0] = mp_obj_new_str(vstr->buf, vstr->len, false);
vstr_free(vstr);
}
}
@@ -291,19 +335,26 @@ void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
self->traceback = MP_OBJ_NULL;
}
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) {
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, mp_uint_t line, qstr block) {
#if MICROPY_ENABLE_GC
if (gc_is_locked()) {
// We can't allocate memory, so don't bother to try
return;
}
#endif
GET_NATIVE_EXCEPTION(self, self_in);
// for traceback, we are just using the list object for convenience, it's not really a list of Python objects
if (self->traceback == MP_OBJ_NULL) {
self->traceback = mp_obj_new_list(0, NULL);
}
mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
mp_obj_list_append(self->traceback, (mp_obj_t)(mp_uint_t)file);
mp_obj_list_append(self->traceback, (mp_obj_t)(mp_uint_t)line);
mp_obj_list_append(self->traceback, (mp_obj_t)(mp_uint_t)block);
}
void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values) {
void mp_obj_exception_get_traceback(mp_obj_t self_in, mp_uint_t *n, mp_uint_t **values) {
GET_NATIVE_EXCEPTION(self, self_in);
if (self->traceback == MP_OBJ_NULL) {

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <assert.h>

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -11,8 +37,9 @@
#include "obj.h"
#include "parsenum.h"
#include "runtime0.h"
#include "runtime.h"
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
#include "formatfloat.h"
@@ -40,13 +67,14 @@ STATIC void float_print(void (*print)(void *env, const char *fmt, ...), void *en
}
STATIC mp_obj_t float_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// TODO check n_kw == 0
mp_arg_check_num(n_args, n_kw, 0, 1, false);
switch (n_args) {
case 0:
return mp_obj_new_float(0);
case 1:
default:
if (MP_OBJ_IS_STR(args[0])) {
// a string, parse it
uint l;
@@ -59,9 +87,6 @@ STATIC mp_obj_t float_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
// something else, try to cast it to a float
return mp_obj_new_float(mp_obj_get_float(args[0]));
}
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "float takes at most 1 argument, %d given", n_args));
}
}
@@ -71,15 +96,18 @@ STATIC mp_obj_t float_unary_op(int op, mp_obj_t o_in) {
case MP_UNARY_OP_BOOL: return MP_BOOL(o->value != 0);
case MP_UNARY_OP_POSITIVE: return o_in;
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-o->value);
default: return NULL; // op not supported
default: return MP_OBJ_NULL; // op not supported
}
}
STATIC mp_obj_t float_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
mp_obj_float_t *lhs = lhs_in;
#if MICROPY_PY_BUILTINS_COMPLEX
if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
return mp_obj_complex_binary_op(op, lhs->value, 0, rhs_in);
} else {
} else
#endif
{
return mp_obj_float_binary_op(op, lhs->value, rhs_in);
}
}
@@ -140,9 +168,9 @@ mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs_in) {
case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val);
default:
return MP_OBJ_NOT_SUPPORTED;
return MP_OBJ_NULL; // op not supported
}
return mp_obj_new_float(lhs_val);
}
#endif // MICROPY_ENABLE_FLOAT
#endif // MICROPY_PY_BUILTINS_FLOAT

View File

@@ -1,7 +1,33 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2014 Paul Sokolovsky
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <alloca.h>
#include "mpconfig.h"
#include "nlr.h"
@@ -13,6 +39,7 @@
#include "runtime0.h"
#include "runtime.h"
#include "bc.h"
#include "stackctrl.h"
#if 0 // print debugging info
#define DEBUG_PRINT (1)
@@ -32,7 +59,7 @@ STATIC mp_obj_t fun_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
// we don't even need to check for 2nd arg type.
return MP_BOOL(lhs_in == rhs_in);
}
return NULL;
return MP_OBJ_NULL; // op not supported
}
STATIC mp_obj_t fun_native_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
@@ -127,8 +154,8 @@ const char *mp_obj_code_get_name(const byte *code_info) {
return qstr_str(block_name);
}
const char *mp_obj_fun_get_name(mp_obj_t fun_in) {
mp_obj_fun_bc_t *fun = fun_in;
const char *mp_obj_fun_get_name(mp_const_obj_t fun_in) {
const mp_obj_fun_bc_t *fun = fun_in;
const byte *code_info = fun->bytecode;
return mp_obj_code_get_name(code_info);
}
@@ -167,66 +194,34 @@ STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, uint expected, ui
#endif
}
// If it's possible to call a function without allocating new argument array,
// this function returns true, together with pointers to 2 subarrays to be used
// as arguments. Otherwise, it returns false. It is expected that this fucntion
// will be accompanied by another, mp_obj_fun_prepare_full_args(), which will
// instead take pointer to full-length out-array, and will fill it in. Rationale
// being that a caller can try this function and if it succeeds, the function call
// can be made without allocating extra memory. Otherwise, caller can allocate memory
// and try "full" function. These functions are expected to be refactoring of
// code in fun_bc_call() and evenrually replace it.
bool mp_obj_fun_prepare_simple_args(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args,
uint *out_args1_len, const mp_obj_t **out_args1, uint *out_args2_len, const mp_obj_t **out_args2) {
mp_obj_fun_bc_t *self = self_in;
DEBUG_printf("mp_obj_fun_prepare_simple_args: given: %d pos, %d kw, expected: %d pos (%d default)\n",
n_args, n_kw, self->n_pos_args, self->n_def_args);
// With this macro you can tune the maximum number of function state bytes
// that will be allocated on the stack. Any function that needs more
// than this will use the heap.
#define VM_MAX_STATE_ON_STACK (10 * sizeof(mp_uint_t))
assert(n_kw == 0);
assert(self->n_kwonly_args == 0);
assert(self->takes_var_args == 0);
assert(self->takes_kw_args == 0);
// Set this to enable a simple stack overflow check.
#define VM_DETECT_STACK_OVERFLOW (0)
mp_obj_t *extra_args = self->extra_args + self->n_def_args;
uint n_extra_args = 0;
if (n_args > self->n_pos_args) {
goto arg_error;
} else {
if (n_args >= self->n_pos_args - self->n_def_args) {
extra_args -= self->n_pos_args - n_args;
n_extra_args += self->n_pos_args - n_args;
} else {
fun_pos_args_mismatch(self, self->n_pos_args - self->n_def_args, n_args);
}
}
*out_args1 = args;
*out_args1_len = n_args;
*out_args2 = extra_args;
*out_args2_len = n_extra_args;
return true;
arg_error:
fun_pos_args_mismatch(self, self->n_pos_args, n_args);
}
STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// code_state should have ->ip filled in (pointing past code info block),
// as well as ->n_state.
void mp_setup_code_state(mp_code_state *code_state, mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// This function is pretty complicated. It's main aim is to be efficient in speed and RAM
// usage for the common case of positional only args.
//
// extra_args layout: def_args, var_arg tuple, kwonly args, var_kw dict
DEBUG_printf("Input n_args: %d, n_kw: %d\n", n_args, n_kw);
DEBUG_printf("Input pos args: ");
dump_args(args, n_args);
DEBUG_printf("Input kw args: ");
dump_args(args + n_args, n_kw * 2);
mp_obj_fun_bc_t *self = self_in;
DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
mp_uint_t n_state = code_state->n_state;
const byte *ip = code_state->ip;
code_state->code_info = self->bytecode;
code_state->sp = &code_state->state[0] - 1;
code_state->exc_sp = (mp_exc_stack_t*)(code_state->state + n_state) - 1;
// zero out the local stack to begin with
memset(code_state->state, 0, n_state * sizeof(*code_state->state));
const mp_obj_t *kwargs = args + n_args;
mp_obj_t *extra_args = self->extra_args + self->n_def_args;
uint n_extra_args = 0;
// var_pos_kw_args points to the stack where the var-args tuple, and var-kw dict, should go (if they are needed)
mp_obj_t *var_pos_kw_args = &code_state->state[n_state - 1 - self->n_pos_args - self->n_kwonly_args];
// check positional arguments
@@ -236,57 +231,53 @@ STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_o
fun_pos_args_mismatch(self, self->n_pos_args, n_args);
}
// put extra arguments in varargs tuple
*extra_args = mp_obj_new_tuple(n_args - self->n_pos_args, args + self->n_pos_args);
n_extra_args = 1;
*var_pos_kw_args-- = mp_obj_new_tuple(n_args - self->n_pos_args, args + self->n_pos_args);
n_args = self->n_pos_args;
} else {
if (self->takes_var_args) {
DEBUG_printf("passing empty tuple as *args\n");
*extra_args = mp_const_empty_tuple;
n_extra_args = 1;
*var_pos_kw_args-- = mp_const_empty_tuple;
}
// Apply processing and check below only if we don't have kwargs,
// otherwise, kw handling code below has own extensive checks.
if (n_kw == 0) {
if (n_kw == 0 && !self->has_def_kw_args) {
if (n_args >= self->n_pos_args - self->n_def_args) {
// given enough arguments, but may need to use some default arguments
extra_args -= self->n_pos_args - n_args;
n_extra_args += self->n_pos_args - n_args;
for (uint i = n_args; i < self->n_pos_args; i++) {
code_state->state[n_state - 1 - i] = self->extra_args[i - (self->n_pos_args - self->n_def_args)];
}
} else {
fun_pos_args_mismatch(self, self->n_pos_args - self->n_def_args, n_args);
}
}
}
// copy positional args into state
for (uint i = 0; i < n_args; i++) {
code_state->state[n_state - 1 - i] = args[i];
}
// check keyword arguments
if (n_kw != 0) {
// We cannot use dynamically-sized array here, because GCC indeed
// deallocates it on leaving defining scope (unlike most static stack allocs).
// So, we have 2 choices: allocate it unconditionally at the top of function
// (wastes stack), or use alloca which is guaranteed to dealloc on func exit.
//mp_obj_t flat_args[self->n_args];
mp_obj_t *flat_args = alloca((self->n_pos_args + self->n_kwonly_args) * sizeof(mp_obj_t));
for (int i = self->n_pos_args + self->n_kwonly_args - 1; i >= 0; i--) {
flat_args[i] = MP_OBJ_NULL;
}
memcpy(flat_args, args, sizeof(*args) * n_args);
if (n_kw != 0 || self->has_def_kw_args) {
DEBUG_printf("Initial args: ");
dump_args(flat_args, self->n_pos_args + self->n_kwonly_args);
dump_args(code_state->state + n_state - self->n_pos_args - self->n_kwonly_args, self->n_pos_args + self->n_kwonly_args);
mp_obj_t dict = MP_OBJ_NULL;
if (self->takes_kw_args) {
dict = mp_obj_new_dict(n_kw); // TODO: better go conservative with 0?
*var_pos_kw_args = dict;
}
for (uint i = 0; i < n_kw; i++) {
qstr arg_name = MP_OBJ_QSTR_VALUE(kwargs[2 * i]);
for (uint j = 0; j < self->n_pos_args + self->n_kwonly_args; j++) {
if (arg_name == self->args[j]) {
if (flat_args[j] != MP_OBJ_NULL) {
if (code_state->state[n_state - 1 - j] != MP_OBJ_NULL) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function got multiple values for argument '%s'", qstr_str(arg_name)));
}
flat_args[j] = kwargs[2 * i + 1];
code_state->state[n_state - 1 - j] = kwargs[2 * i + 1];
goto continue2;
}
}
@@ -297,43 +288,47 @@ STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_o
mp_obj_dict_store(dict, kwargs[2 * i], kwargs[2 * i + 1]);
continue2:;
}
DEBUG_printf("Args with kws flattened: ");
dump_args(flat_args, self->n_pos_args + self->n_kwonly_args);
// Now fill in defaults for positional args
mp_obj_t *d = &flat_args[self->n_pos_args - 1];
DEBUG_printf("Args with kws flattened: ");
dump_args(code_state->state + n_state - self->n_pos_args - self->n_kwonly_args, self->n_pos_args + self->n_kwonly_args);
// fill in defaults for positional args
mp_obj_t *d = &code_state->state[n_state - self->n_pos_args];
mp_obj_t *s = &self->extra_args[self->n_def_args - 1];
for (int i = self->n_def_args; i > 0; i--, d--, s--) {
for (int i = self->n_def_args; i > 0; i--, d++, s--) {
if (*d == MP_OBJ_NULL) {
*d = *s;
}
}
DEBUG_printf("Args after filling defaults: ");
dump_args(flat_args, self->n_pos_args + self->n_kwonly_args);
DEBUG_printf("Args after filling default positional: ");
dump_args(code_state->state + n_state - self->n_pos_args - self->n_kwonly_args, self->n_pos_args + self->n_kwonly_args);
// Check that all mandatory positional args are specified
while (d >= flat_args) {
if (*d-- == MP_OBJ_NULL) {
while (d < &code_state->state[n_state]) {
if (*d++ == MP_OBJ_NULL) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function missing required positional argument #%d", d - flat_args));
"function missing required positional argument #%d", &code_state->state[n_state] - d));
}
}
// Check that all mandatory keyword args are specified
for (int i = 0; i < self->n_kwonly_args; i++) {
if (flat_args[self->n_pos_args + i] == MP_OBJ_NULL) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function missing required keyword argument '%s'", qstr_str(self->args[self->n_pos_args + i])));
// Fill in default kw args if we have them
for (uint i = 0; i < self->n_kwonly_args; i++) {
if (code_state->state[n_state - 1 - self->n_pos_args - i] == MP_OBJ_NULL) {
mp_map_elem_t *elem = NULL;
if (self->has_def_kw_args) {
elem = mp_map_lookup(&((mp_obj_dict_t*)self->extra_args[self->n_def_args])->map, MP_OBJ_NEW_QSTR(self->args[self->n_pos_args + i]), MP_MAP_LOOKUP);
}
if (elem != NULL) {
code_state->state[n_state - 1 - self->n_pos_args - i] = elem->value;
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function missing required keyword argument '%s'", qstr_str(self->args[self->n_pos_args + i])));
}
}
}
args = flat_args;
n_args = self->n_pos_args + self->n_kwonly_args;
if (self->takes_kw_args) {
extra_args[n_extra_args] = dict;
n_extra_args += 1;
}
} else {
// no keyword arguments given
if (self->n_kwonly_args != 0) {
@@ -341,20 +336,121 @@ continue2:;
"function missing keyword-only argument"));
}
if (self->takes_kw_args) {
extra_args[n_extra_args] = mp_obj_new_dict(0);
n_extra_args += 1;
*var_pos_kw_args = mp_obj_new_dict(0);
}
}
// bytecode prelude: initialise closed over variables
for (uint n_local = *ip++; n_local > 0; n_local--) {
uint local_num = *ip++;
code_state->state[n_state - 1 - local_num] = mp_obj_new_cell(code_state->state[n_state - 1 - local_num]);
}
// now that we skipped over the prelude, set the ip for the VM
code_state->ip = ip;
DEBUG_printf("Calling: n_pos_args=%d, n_kwonly_args=%d\n", self->n_pos_args, self->n_kwonly_args);
dump_args(code_state->state + n_state - self->n_pos_args - self->n_kwonly_args, self->n_pos_args + self->n_kwonly_args);
dump_args(code_state->state, n_state);
}
STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
MP_STACK_CHECK();
DEBUG_printf("Input n_args: %d, n_kw: %d\n", n_args, n_kw);
DEBUG_printf("Input pos args: ");
dump_args(args, n_args);
DEBUG_printf("Input kw args: ");
dump_args(args + n_args, n_kw * 2);
mp_obj_fun_bc_t *self = self_in;
DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
const byte *ip = self->bytecode;
// get code info size, and skip line number table
mp_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
ip += code_info_size;
// bytecode prelude: state size and exception stack size; 16 bit uints
mp_uint_t n_state = ip[0] | (ip[1] << 8);
mp_uint_t n_exc_stack = ip[2] | (ip[3] << 8);
ip += 4;
#if VM_DETECT_STACK_OVERFLOW
n_state += 1;
#endif
// allocate state for locals and stack
uint state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
mp_code_state *code_state;
if (state_size > VM_MAX_STATE_ON_STACK) {
code_state = m_new_obj_var(mp_code_state, byte, state_size);
} else {
code_state = alloca(sizeof(mp_code_state) + state_size);
}
code_state->n_state = n_state;
code_state->ip = ip;
mp_setup_code_state(code_state, self_in, n_args, n_kw, args);
// execute the byte code with the correct globals context
mp_obj_dict_t *old_globals = mp_globals_get();
mp_globals_set(self->globals);
mp_obj_t result;
DEBUG_printf("Calling: args=%p, n_args=%d, extra_args=%p, n_extra_args=%d\n", args, n_args, extra_args, n_extra_args);
dump_args(args, n_args);
dump_args(extra_args, n_extra_args);
mp_vm_return_kind_t vm_return_kind = mp_execute_byte_code(self->bytecode, args, n_args, extra_args, n_extra_args, &result);
mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
mp_globals_set(old_globals);
#if VM_DETECT_STACK_OVERFLOW
if (vm_return_kind == MP_VM_RETURN_NORMAL) {
if (code_state->sp < code_state->state) {
printf("VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
assert(0);
}
}
// We can't check the case when an exception is returned in state[n_state - 1]
// and there are no arguments, because in this case our detection slot may have
// been overwritten by the returned exception (which is allowed).
if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && self->n_pos_args + self->n_kwonly_args == 0)) {
// Just check to see that we have at least 1 null object left in the state.
bool overflow = true;
for (uint i = 0; i < n_state - self->n_pos_args - self->n_kwonly_args; i++) {
if (code_state->state[i] == MP_OBJ_NULL) {
overflow = false;
break;
}
}
if (overflow) {
printf("VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
assert(0);
}
}
#endif
mp_obj_t result;
switch (vm_return_kind) {
case MP_VM_RETURN_NORMAL:
// return value is in *sp
result = *code_state->sp;
break;
case MP_VM_RETURN_EXCEPTION:
// return value is in state[n_state - 1]
result = code_state->state[n_state - 1];
break;
case MP_VM_RETURN_YIELD: // byte-code shouldn't yield
default:
assert(0);
result = mp_const_none;
vm_return_kind = MP_VM_RETURN_NORMAL;
break;
}
// free the state if it was allocated on the heap
if (state_size > VM_MAX_STATE_ON_STACK) {
m_del_var(mp_code_state, byte, state_size, code_state);
}
if (vm_return_kind == MP_VM_RETURN_NORMAL) {
return result;
} else { // MP_VM_RETURN_EXCEPTION
@@ -372,7 +468,7 @@ const mp_obj_type_t mp_type_fun_bc = {
.binary_op = fun_binary_op,
};
mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_pos_args, uint n_kwonly_args, mp_obj_t def_args_in, const byte *code) {
mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_pos_args, uint n_kwonly_args, mp_obj_t def_args_in, mp_obj_t def_kw_args, const byte *code) {
uint n_def_args = 0;
uint n_extra_args = 0;
mp_obj_tuple_t *def_args = def_args_in;
@@ -381,10 +477,7 @@ mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_pos_args, uint n
n_def_args = def_args->len;
n_extra_args = def_args->len;
}
if ((scope_flags & MP_SCOPE_FLAG_VARARGS) != 0) {
n_extra_args += 1;
}
if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
if (def_kw_args != MP_OBJ_NULL) {
n_extra_args += 1;
}
mp_obj_fun_bc_t *o = m_new_obj_var(mp_obj_fun_bc_t, mp_obj_t, n_extra_args);
@@ -394,18 +487,15 @@ mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_pos_args, uint n
o->n_pos_args = n_pos_args;
o->n_kwonly_args = n_kwonly_args;
o->n_def_args = n_def_args;
o->has_def_kw_args = def_kw_args != MP_OBJ_NULL;
o->takes_var_args = (scope_flags & MP_SCOPE_FLAG_VARARGS) != 0;
o->takes_kw_args = (scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0;
o->bytecode = code;
memset(o->extra_args, 0, n_extra_args * sizeof(mp_obj_t));
if (def_args != MP_OBJ_NULL) {
memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
}
if ((scope_flags & MP_SCOPE_FLAG_VARARGS) != 0) {
o->extra_args[n_def_args] = MP_OBJ_NULL;
}
if ((scope_flags & MP_SCOPE_FLAG_VARARGS) != 0) {
o->extra_args[n_extra_args - 1] = MP_OBJ_NULL;
if (def_kw_args != MP_OBJ_NULL) {
o->extra_args[n_def_args] = def_kw_args;
}
return o;
}
@@ -419,13 +509,13 @@ typedef struct _mp_obj_fun_asm_t {
void *fun;
} mp_obj_fun_asm_t;
typedef machine_uint_t (*inline_asm_fun_0_t)();
typedef machine_uint_t (*inline_asm_fun_1_t)(machine_uint_t);
typedef machine_uint_t (*inline_asm_fun_2_t)(machine_uint_t, machine_uint_t);
typedef machine_uint_t (*inline_asm_fun_3_t)(machine_uint_t, machine_uint_t, machine_uint_t);
typedef mp_uint_t (*inline_asm_fun_0_t)();
typedef mp_uint_t (*inline_asm_fun_1_t)(mp_uint_t);
typedef mp_uint_t (*inline_asm_fun_2_t)(mp_uint_t, mp_uint_t);
typedef mp_uint_t (*inline_asm_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
// convert a Micro Python object to a sensible value for inline asm
STATIC machine_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
// TODO for byte_array, pass pointer to the array
if (MP_OBJ_IS_SMALL_INT(obj)) {
return MP_OBJ_SMALL_INT_VALUE(obj);
@@ -438,56 +528,51 @@ STATIC machine_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
} else if (MP_OBJ_IS_STR(obj)) {
// pointer to the string (it's probably constant though!)
uint l;
return (machine_uint_t)mp_obj_str_get_data(obj, &l);
return (mp_uint_t)mp_obj_str_get_data(obj, &l);
} else {
mp_obj_type_t *type = mp_obj_get_type(obj);
if (0) {
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
} else if (type == &mp_type_float) {
// convert float to int (could also pass in float registers)
return (machine_int_t)mp_obj_float_get(obj);
return (mp_int_t)mp_obj_float_get(obj);
#endif
} else if (type == &mp_type_tuple) {
// pointer to start of tuple (could pass length, but then could use len(x) for that)
uint len;
mp_obj_t *items;
mp_obj_tuple_get(obj, &len, &items);
return (machine_uint_t)items;
return (mp_uint_t)items;
} else if (type == &mp_type_list) {
// pointer to start of list (could pass length, but then could use len(x) for that)
uint len;
mp_obj_t *items;
mp_obj_list_get(obj, &len, &items);
return (machine_uint_t)items;
return (mp_uint_t)items;
} else {
mp_buffer_info_t bufinfo;
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
// supports the buffer protocol, return a pointer to the data
return (machine_uint_t)bufinfo.buf;
return (mp_uint_t)bufinfo.buf;
} else {
// just pass along a pointer to the object
return (machine_uint_t)obj;
return (mp_uint_t)obj;
}
}
}
}
// convert a return value from inline asm to a sensible Micro Python object
STATIC mp_obj_t convert_val_from_inline_asm(machine_uint_t val) {
STATIC mp_obj_t convert_val_from_inline_asm(mp_uint_t val) {
return MP_OBJ_NEW_SMALL_INT(val);
}
STATIC mp_obj_t fun_asm_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
mp_obj_fun_asm_t *self = self_in;
if (n_args != self->n_args) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "function takes %d positional arguments but %d were given", self->n_args, n_args));
}
if (n_kw != 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
}
mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
machine_uint_t ret;
mp_uint_t ret;
if (n_args == 0) {
ret = ((inline_asm_fun_0_t)self->fun)();
} else if (n_args == 1) {

View File

@@ -1,13 +1,44 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
typedef struct _mp_obj_fun_bc_t {
mp_obj_base_t base;
mp_obj_dict_t *globals; // the context within which this function was defined
machine_uint_t n_pos_args : 16; // number of arguments this function takes
machine_uint_t n_kwonly_args : 16; // number of arguments this function takes
machine_uint_t n_def_args : 16; // number of default arguments
machine_uint_t takes_var_args : 1; // set if this function takes variable args
machine_uint_t takes_kw_args : 1; // set if this function takes keyword args
const byte *bytecode; // bytecode for the function
qstr *args; // argument names (needed to resolve positional args passed as keywords)
// values of default args (if any), plus a slot at the end for var args and/or kw args (if it takes them)
mp_obj_dict_t *globals; // the context within which this function was defined
mp_uint_t n_pos_args : 16; // number of arguments this function takes
mp_uint_t n_kwonly_args : 16; // number of arguments this function takes
mp_uint_t n_def_args : 16; // number of default arguments
mp_uint_t has_def_kw_args : 1; // set if this function has default keyword args
mp_uint_t takes_var_args : 1; // set if this function takes variable args
mp_uint_t takes_kw_args : 1; // set if this function takes keyword args
const byte *bytecode; // bytecode for the function
qstr *args; // argument names (needed to resolve positional args passed as keywords)
// the following extra_args array is allocated space to take (in order):
// - values of positional default args (if any)
// - a single slot for default kw args dict (if it has them)
// - a single slot for var args tuple (if it takes them)
// - a single slot for kw args dict (if it takes them)
mp_obj_t extra_args[];
} mp_obj_fun_bc_t;

View File

@@ -1,3 +1,30 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2014 Paul Sokolovsky
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <assert.h>
@@ -19,21 +46,37 @@ typedef struct _mp_obj_gen_wrap_t {
mp_obj_t *fun;
} mp_obj_gen_wrap_t;
mp_obj_t mp_obj_new_gen_instance(mp_obj_dict_t *globals, const byte *bytecode, uint n_args, const mp_obj_t *args,
uint n_args2, const mp_obj_t *args2);
typedef struct _mp_obj_gen_instance_t {
mp_obj_base_t base;
mp_obj_dict_t *globals;
mp_code_state code_state;
} mp_obj_gen_instance_t;
STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
mp_obj_gen_wrap_t *self = self_in;
mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t*)self->fun;
assert(MP_OBJ_IS_TYPE(self_fun, &mp_type_fun_bc));
const mp_obj_t *args1, *args2;
uint len1, len2;
if (!mp_obj_fun_prepare_simple_args(self_fun, n_args, n_kw, args, &len1, &args1, &len2, &args2)) {
assert(0);
}
const byte *bytecode = self_fun->bytecode;
// get code info size, and skip the line number table
mp_uint_t code_info_size = bytecode[0] | (bytecode[1] << 8) | (bytecode[2] << 16) | (bytecode[3] << 24);
bytecode += code_info_size;
return mp_obj_new_gen_instance(self_fun->globals, self_fun->bytecode, len1, args1, len2, args2);
// bytecode prelude: get state size and exception stack size
mp_uint_t n_state = bytecode[0] | (bytecode[1] << 8);
mp_uint_t n_exc_stack = bytecode[2] | (bytecode[3] << 8);
bytecode += 4;
// allocate the generator object, with room for local stack and exception stack
mp_obj_gen_instance_t *o = m_new_obj_var(mp_obj_gen_instance_t, byte,
n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t));
o->base.type = &mp_type_gen_instance;
o->globals = self_fun->globals;
o->code_state.n_state = n_state;
o->code_state.ip = bytecode;
mp_setup_code_state(&o->code_state, self_fun, n_args, n_kw, args);
return o;
}
const mp_obj_type_t mp_type_gen_wrap = {
@@ -52,49 +95,28 @@ mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
/******************************************************************************/
/* generator instance */
typedef struct _mp_obj_gen_instance_t {
mp_obj_base_t base;
mp_obj_dict_t *globals;
const byte *code_info;
const byte *ip;
mp_obj_t *sp;
// bit 0 is saved currently_in_except_block value
mp_exc_stack_t *exc_sp;
uint n_state;
// Variable-length
mp_obj_t state[0];
// Variable-length, never accessed by name, only as (void*)(state + n_state)
mp_exc_stack_t exc_state[0];
} mp_obj_gen_instance_t;
void gen_instance_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
mp_obj_gen_instance_t *self = self_in;
print(env, "<generator object '%s' at %p>", mp_obj_code_get_name(self->code_info), self_in);
}
mp_obj_t gen_instance_getiter(mp_obj_t self_in) {
return self_in;
print(env, "<generator object '%s' at %p>", mp_obj_code_get_name(self->code_state.code_info), self_in);
}
mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_gen_instance));
mp_obj_gen_instance_t *self = self_in;
if (self->ip == 0) {
if (self->code_state.ip == 0) {
*ret_val = MP_OBJ_STOP_ITERATION;
return MP_VM_RETURN_NORMAL;
}
if (self->sp == self->state - 1) {
if (self->code_state.sp == self->code_state.state - 1) {
if (send_value != mp_const_none) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "can't send non-None value to a just-started generator"));
}
} else {
*self->sp = send_value;
*self->code_state.sp = send_value;
}
mp_obj_dict_t *old_globals = mp_globals_get();
mp_globals_set(self->globals);
mp_vm_return_kind_t ret_kind = mp_execute_byte_code_2(self->code_info, &self->ip,
&self->state[self->n_state - 1], &self->sp, (mp_exc_stack_t*)(self->state + self->n_state),
&self->exc_sp, throw_value);
mp_vm_return_kind_t ret_kind = mp_execute_bytecode(&self->code_state, throw_value);
mp_globals_set(old_globals);
switch (ret_kind) {
@@ -104,17 +126,17 @@ mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_
// again and again, leading to side effects.
// TODO: check how return with value behaves under such conditions
// in CPython.
self->ip = 0;
*ret_val = *self->sp;
self->code_state.ip = 0;
*ret_val = *self->code_state.sp;
break;
case MP_VM_RETURN_YIELD:
*ret_val = *self->sp;
*ret_val = *self->code_state.sp;
break;
case MP_VM_RETURN_EXCEPTION:
self->ip = 0;
*ret_val = self->state[self->n_state - 1];
self->code_state.ip = 0;
*ret_val = self->code_state.state[self->code_state.n_state - 1];
break;
default:
@@ -224,53 +246,7 @@ const mp_obj_type_t mp_type_gen_instance = {
{ &mp_type_type },
.name = MP_QSTR_generator,
.print = gen_instance_print,
.getiter = gen_instance_getiter,
.getiter = mp_identity,
.iternext = gen_instance_iternext,
.locals_dict = (mp_obj_t)&gen_instance_locals_dict,
};
mp_obj_t mp_obj_new_gen_instance(mp_obj_dict_t *globals, const byte *bytecode, uint n_args, const mp_obj_t *args,
uint n_args2, const mp_obj_t *args2) {
const byte *code_info = bytecode;
// get code info size, and skip the line number table
machine_uint_t code_info_size = bytecode[0] | (bytecode[1] << 8) | (bytecode[2] << 16) | (bytecode[3] << 24);
bytecode += code_info_size;
// bytecode prelude: get state size and exception stack size
machine_uint_t n_state = bytecode[0] | (bytecode[1] << 8);
machine_uint_t n_exc_stack = bytecode[2] | (bytecode[3] << 8);
bytecode += 4;
// allocate the generator object, with room for local stack and exception stack
mp_obj_gen_instance_t *o = m_new_obj_var(mp_obj_gen_instance_t, byte, n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t));
o->base.type = &mp_type_gen_instance;
o->globals = globals;
o->code_info = code_info;
o->sp = &o->state[0] - 1; // sp points to top of stack, which starts off 1 below the state
o->exc_sp = (mp_exc_stack_t*)(o->state + n_state) - 1;
o->n_state = n_state;
// copy args to end of state array, in reverse (that's how mp_execute_byte_code_2 needs it)
for (uint i = 0; i < n_args; i++) {
o->state[n_state - 1 - i] = args[i];
}
for (uint i = 0; i < n_args2; i++) {
o->state[n_state - 1 - n_args - i] = args2[i];
}
// set rest of state to MP_OBJ_NULL
for (uint i = 0; i < n_state - n_args - n_args2; i++) {
o->state[i] = MP_OBJ_NULL;
}
// bytecode prelude: initialise closed over variables
for (uint n_local = *bytecode++; n_local > 0; n_local--) {
uint local_num = *bytecode++;
o->state[n_state - 1 - local_num] = mp_obj_new_cell(o->state[n_state - 1 - local_num]);
}
// set ip to start of actual byte code
o->ip = bytecode;
return o;
}

View File

@@ -1 +1,27 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_val, mp_obj_t throw_val, mp_obj_t *ret_val);

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include "mpconfig.h"

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
@@ -9,55 +35,57 @@
#include "qstr.h"
#include "obj.h"
#include "parsenum.h"
#include "smallint.h"
#include "mpz.h"
#include "objint.h"
#include "runtime0.h"
#include "runtime.h"
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
#include <math.h>
#endif
// This dispatcher function is expected to be independent of the implementation of long int
STATIC mp_obj_t mp_obj_int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// TODO check n_kw == 0
mp_arg_check_num(n_args, n_kw, 0, 2, false);
switch (n_args) {
case 0:
return MP_OBJ_NEW_SMALL_INT(0);
case 1:
if (MP_OBJ_IS_STR(args[0])) {
if (MP_OBJ_IS_INT(args[0])) {
// already an int (small or long), just return it
return args[0];
} else if (MP_OBJ_IS_STR(args[0])) {
// a string, parse it
uint l;
const char *s = mp_obj_str_get_data(args[0], &l);
return mp_parse_num_integer(s, l, 0);
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
} else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
return MP_OBJ_NEW_SMALL_INT((machine_int_t)(MICROPY_FLOAT_C_FUN(trunc)(mp_obj_float_get(args[0]))));
return MP_OBJ_NEW_SMALL_INT((mp_int_t)(MICROPY_FLOAT_C_FUN(trunc)(mp_obj_float_get(args[0]))));
#endif
} else {
// try to convert to small int (eg from bool)
return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
}
case 2:
{
default: {
// should be a string, parse it
// TODO proper error checking of argument types
uint l;
const char *s = mp_obj_str_get_data(args[0], &l);
return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]));
}
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "int takes at most 2 arguments, %d given", n_args));
}
}
void mp_obj_int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
// The size of this buffer is rather arbitrary. If it's not large
// enough, a dynamic one will be allocated.
char stack_buf[sizeof(machine_int_t) * 4];
char stack_buf[sizeof(mp_int_t) * 4];
char *buf = stack_buf;
int buf_size = sizeof(stack_buf);
int fmt_size;
@@ -73,7 +101,7 @@ void mp_obj_int_print(void (*print)(void *env, const char *fmt, ...), void *env,
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
typedef mp_longint_impl_t fmt_int_t;
#else
typedef mp_small_int_t fmt_int_t;
typedef mp_int_t fmt_int_t;
#endif
STATIC const uint log_base2_floor[] = {
@@ -99,14 +127,14 @@ STATIC uint int_as_str_size_formatted(uint base, const char *prefix, char comma)
return num_digits + num_commas + prefix_len + 2; // +1 for sign, +1 for null byte
}
// This routine expects you to pass in a buffer and size (in *buf and buf_size).
// This routine expects you to pass in a buffer and size (in *buf and *buf_size).
// If, for some reason, this buffer is too small, then it will allocate a
// buffer and return the allocated buffer and size in *buf and *buf_size. It
// is the callers responsibility to free this allocated buffer.
//
// The resulting formatted string will be returned from this function and the
// formatted size will be in *fmt_size.
char *mp_obj_int_formatted(char **buf, int *buf_size, int *fmt_size, mp_obj_t self_in,
char *mp_obj_int_formatted(char **buf, int *buf_size, int *fmt_size, mp_const_obj_t self_in,
int base, const char *prefix, char base_char, char comma) {
fmt_int_t num;
if (MP_OBJ_IS_SMALL_INT(self_in)) {
@@ -116,8 +144,8 @@ char *mp_obj_int_formatted(char **buf, int *buf_size, int *fmt_size, mp_obj_t se
} else if (MP_OBJ_IS_TYPE(self_in, &mp_type_int)) {
// Not a small int.
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
mp_obj_int_t *self = self_in;
// Get the value to format; mp_obj_get_int truncates to machine_int_t.
const mp_obj_int_t *self = self_in;
// Get the value to format; mp_obj_get_int truncates to mp_int_t.
num = self->val;
#else
// Delegate to the implementation for the long int.
@@ -126,7 +154,7 @@ char *mp_obj_int_formatted(char **buf, int *buf_size, int *fmt_size, mp_obj_t se
#endif
} else {
// Not an int.
buf[0] = '\0';
**buf = '\0';
*fmt_size = 0;
return *buf;
}
@@ -193,7 +221,7 @@ bool mp_obj_int_is_positive(mp_obj_t self_in) {
// This is called for operations on SMALL_INT that are not handled by mp_unary_op
mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
return MP_OBJ_NOT_SUPPORTED;
return MP_OBJ_NULL; // op not supported
}
// This is called for operations on SMALL_INT that are not handled by mp_binary_op
@@ -202,7 +230,7 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
}
// This is called only with strings whose value doesn't fit in SMALL_INT
mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
mp_obj_t mp_obj_new_int_from_str_len(const char **str, uint len, bool neg, uint base) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "long int not supported in this build"));
return mp_const_none;
}
@@ -213,7 +241,7 @@ mp_obj_t mp_obj_new_int_from_ll(long long val) {
return mp_const_none;
}
mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
// SMALL_INT accepts only signed numbers, of one bit less size
// then word size, which totals 2 bits less for unsigned numbers.
if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
@@ -223,23 +251,23 @@ mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
return mp_const_none;
}
mp_obj_t mp_obj_new_int(machine_int_t value) {
if (MP_OBJ_FITS_SMALL_INT(value)) {
mp_obj_t mp_obj_new_int(mp_int_t value) {
if (MP_SMALL_INT_FITS(value)) {
return MP_OBJ_NEW_SMALL_INT(value);
}
nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
return mp_const_none;
}
machine_int_t mp_obj_int_get(mp_obj_t self_in) {
mp_int_t mp_obj_int_get(mp_const_obj_t self_in) {
return MP_OBJ_SMALL_INT_VALUE(self_in);
}
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
return MP_OBJ_SMALL_INT_VALUE(self_in);
}
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
return MP_OBJ_SMALL_INT_VALUE(self_in);
}
@@ -262,7 +290,7 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(int op, mp_obj_t lhs_in, mp_obj_t rhs_
return mp_binary_op(op, rhs_in, lhs_in);
}
}
return MP_OBJ_NOT_SUPPORTED;
return MP_OBJ_NULL; // op not supported
}
// this is a classmethod
@@ -276,8 +304,8 @@ STATIC mp_obj_t int_from_bytes(uint n_args, const mp_obj_t *args) {
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
// convert the bytes to an integer
machine_uint_t value = 0;
for (const byte* buf = bufinfo.buf + bufinfo.len - 1; buf >= (byte*)bufinfo.buf; buf--) {
mp_uint_t value = 0;
for (const byte* buf = (const byte*)bufinfo.buf + bufinfo.len - 1; buf >= (byte*)bufinfo.buf; buf--) {
value = (value << 8) | *buf;
}
@@ -288,7 +316,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 2, 3, int_fro
STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, (const mp_obj_t)&int_from_bytes_fun_obj);
STATIC mp_obj_t int_to_bytes(uint n_args, const mp_obj_t *args) {
machine_int_t val = mp_obj_int_get_checked(args[0]);
mp_int_t val = mp_obj_int_get_checked(args[0]);
uint len = MP_OBJ_SMALL_INT_VALUE(args[1]);
byte *data;
@@ -298,7 +326,7 @@ STATIC mp_obj_t int_to_bytes(uint n_args, const mp_obj_t *args) {
// TODO: Support signed param
mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, len, &data);
memset(data, 0, len);
memcpy(data, &val, len < sizeof(machine_int_t) ? len : sizeof(machine_int_t));
memcpy(data, &val, len < sizeof(mp_int_t) ? len : sizeof(mp_int_t));
return mp_obj_str_builder_end(o);
}

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
typedef struct _mp_obj_int_t {
mp_obj_base_t base;
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
@@ -8,9 +34,9 @@ typedef struct _mp_obj_int_t {
} mp_obj_int_t;
void mp_obj_int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind);
char *mp_obj_int_formatted(char **buf, int *buf_size, int *fmt_size, mp_obj_t self_in,
char *mp_obj_int_formatted(char **buf, int *buf_size, int *fmt_size, mp_const_obj_t self_in,
int base, const char *prefix, char base_char, char comma);
char *mp_obj_int_formatted_impl(char **buf, int *buf_size, int *fmt_size, mp_obj_t self_in,
char *mp_obj_int_formatted_impl(char **buf, int *buf_size, int *fmt_size, mp_const_obj_t self_in,
int base, const char *prefix, char base_char, char comma);
bool mp_obj_int_is_positive(mp_obj_t self_in);
mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in);

View File

@@ -1,3 +1,30 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2014 Paul Sokolovsky
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
@@ -7,6 +34,7 @@
#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "smallint.h"
#include "mpz.h"
#include "objint.h"
#include "runtime0.h"
@@ -22,6 +50,11 @@
#define SUFFIX ""
#endif
#if MICROPY_PY_SYS_MAXSIZE
// Export value for sys.maxsize
const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, INT_MAX};
#endif
bool mp_obj_int_is_positive(mp_obj_t self_in) {
if (MP_OBJ_IS_SMALL_INT(self_in)) {
return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
@@ -37,7 +70,7 @@ mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
case MP_UNARY_OP_POSITIVE: return o_in;
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
default: return MP_OBJ_NOT_SUPPORTED;
default: return MP_OBJ_NULL; // op not supported
}
}
@@ -50,7 +83,7 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
} else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
lhs_val = ((mp_obj_int_t*)lhs_in)->val;
} else {
return MP_OBJ_NOT_SUPPORTED;
return MP_OBJ_NULL; // op not supported
}
if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
@@ -108,18 +141,18 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
return MP_BOOL(lhs_val == rhs_val);
default:
return MP_OBJ_NOT_SUPPORTED;
return MP_OBJ_NULL; // op not supported
}
}
mp_obj_t mp_obj_new_int(machine_int_t value) {
if (MP_OBJ_FITS_SMALL_INT(value)) {
mp_obj_t mp_obj_new_int(mp_int_t value) {
if (MP_SMALL_INT_FITS(value)) {
return MP_OBJ_NEW_SMALL_INT(value);
}
return mp_obj_new_int_from_ll(value);
}
mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
// SMALL_INT accepts only signed numbers, of one bit less size
// than word size, which totals 2 bits less for unsigned numbers.
if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
@@ -135,35 +168,32 @@ mp_obj_t mp_obj_new_int_from_ll(long long val) {
return o;
}
mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
long long v;
char *end;
// TODO: this doesn't handle Python hacked 0o octal syntax
v = strtoll(s, &end, 0);
if (*end != 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
}
mp_obj_t mp_obj_new_int_from_str_len(const char **str, uint len, bool neg, uint base) {
// TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated
// TODO check overflow
mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
o->base.type = &mp_type_int;
o->val = v;
char *endptr;
o->val = strtoll(*str, &endptr, base);
*str = endptr;
return o;
}
machine_int_t mp_obj_int_get(mp_obj_t self_in) {
mp_int_t mp_obj_int_get(mp_const_obj_t self_in) {
if (MP_OBJ_IS_SMALL_INT(self_in)) {
return MP_OBJ_SMALL_INT_VALUE(self_in);
} else {
mp_obj_int_t *self = self_in;
const mp_obj_int_t *self = self_in;
return self->val;
}
}
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
// TODO: Check overflow
return mp_obj_int_get(self_in);
}
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
if (MP_OBJ_IS_SMALL_INT(self_in)) {
return MP_OBJ_SMALL_INT_VALUE(self_in);

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <string.h>
#include <stdio.h>
@@ -9,6 +35,7 @@
#include "qstr.h"
#include "parsenumbase.h"
#include "obj.h"
#include "smallint.h"
#include "mpz.h"
#include "objint.h"
#include "runtime0.h"
@@ -16,6 +43,26 @@
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
#if MICROPY_PY_SYS_MAXSIZE
// Export value for sys.maxsize
#define DIG_MASK ((1 << MPZ_DIG_SIZE) - 1)
STATIC const mpz_dig_t maxsize_dig[MPZ_NUM_DIG_FOR_INT] = {
(INT_MAX >> MPZ_DIG_SIZE * 0) & DIG_MASK,
(INT_MAX >> MPZ_DIG_SIZE * 1) & DIG_MASK,
(INT_MAX >> MPZ_DIG_SIZE * 2) & DIG_MASK,
#if (INT_MAX >> MPZ_DIG_SIZE * 2) > DIG_MASK
(INT_MAX >> MPZ_DIG_SIZE * 3) & DIG_MASK,
(INT_MAX >> MPZ_DIG_SIZE * 4) & DIG_MASK,
// (INT_MAX >> MPZ_DIG_SIZE * 5) & DIG_MASK,
#endif
};
const mp_obj_int_t mp_maxsize_obj = {
{&mp_type_int},
{.fixed_dig = 1, .len = MPZ_NUM_DIG_FOR_INT, .alloc = MPZ_NUM_DIG_FOR_INT, .dig = (mpz_dig_t*)maxsize_dig}
};
#undef DIG_MASK
#endif
STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {
mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
o->base.type = &mp_type_int;
@@ -32,10 +79,10 @@ STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {
// formatted size will be in *fmt_size.
//
// This particular routine should only be called for the mpz representation of the int.
char *mp_obj_int_formatted_impl(char **buf, int *buf_size, int *fmt_size, mp_obj_t self_in,
char *mp_obj_int_formatted_impl(char **buf, int *buf_size, int *fmt_size, mp_const_obj_t self_in,
int base, const char *prefix, char base_char, char comma) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
mp_obj_int_t *self = self_in;
const mp_obj_int_t *self = self_in;
uint needed_size = mpz_as_str_size_formatted(&self->mpz, base, prefix, comma);
if (needed_size > *buf_size) {
@@ -64,7 +111,7 @@ mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
case MP_UNARY_OP_POSITIVE: return o_in;
case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return o2; }
case MP_UNARY_OP_INVERT: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_not_inpl(&o2->mpz, &o->mpz); return o2; }
default: return NULL; // op not supported
default: return MP_OBJ_NULL; // op not supported
}
}
@@ -82,7 +129,7 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
} else {
// unsupported type
return MP_OBJ_NOT_SUPPORTED;
return MP_OBJ_NULL;
}
// if rhs is small int, then lhs was not (otherwise mp_binary_op handles it)
@@ -91,11 +138,13 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
zrhs = &z_int;
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
zrhs = &((mp_obj_int_t*)rhs_in)->mpz;
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_float)) {
return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in);
#if MICROPY_PY_BUILTINS_COMPLEX
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in);
#endif
#endif
} else {
// delegate to generic function to check for extra cases
@@ -103,7 +152,7 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
}
if (0) {
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
} else if (op == MP_BINARY_OP_TRUE_DIVIDE || op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE) {
mp_float_t flhs = mpz_as_float(zlhs);
mp_float_t frhs = mpz_as_float(zrhs);
@@ -169,7 +218,7 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
case MP_BINARY_OP_RSHIFT:
case MP_BINARY_OP_INPLACE_RSHIFT: {
// TODO check conversion overflow
machine_int_t irhs = mpz_as_int(zrhs);
mp_int_t irhs = mpz_as_int(zrhs);
if (irhs < 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
}
@@ -187,7 +236,7 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
break;
default:
return MP_OBJ_NOT_SUPPORTED;
return MP_OBJ_NULL; // op not supported
}
return res;
@@ -207,13 +256,13 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
return MP_BOOL(cmp == 0);
default:
return MP_OBJ_NOT_SUPPORTED;
return MP_OBJ_NULL; // op not supported
}
}
}
mp_obj_t mp_obj_new_int(machine_int_t value) {
if (MP_OBJ_FITS_SMALL_INT(value)) {
mp_obj_t mp_obj_new_int(mp_int_t value) {
if (MP_SMALL_INT_FITS(value)) {
return MP_OBJ_NEW_SMALL_INT(value);
}
return mp_obj_new_int_from_ll(value);
@@ -225,7 +274,7 @@ mp_obj_t mp_obj_new_int_from_ll(long long val) {
return o;
}
mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
// SMALL_INT accepts only signed numbers, of one bit less size
// than word size, which totals 2 bits less for unsigned numbers.
if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
@@ -234,35 +283,28 @@ mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
return mp_obj_new_int_from_ll(value);
}
mp_obj_t mp_obj_new_int_from_long_str(const char *str) {
mp_obj_t mp_obj_new_int_from_str_len(const char **str, uint len, bool neg, uint base) {
mp_obj_int_t *o = mp_obj_int_new_mpz();
uint len = strlen(str);
int base = 0;
int skip = mp_parse_num_base(str, len, &base);
str += skip;
len -= skip;
uint n = mpz_set_from_str(&o->mpz, str, len, false, base);
if (n != len) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
}
uint n = mpz_set_from_str(&o->mpz, *str, len, neg, base);
*str += n;
return o;
}
machine_int_t mp_obj_int_get(mp_obj_t self_in) {
mp_int_t mp_obj_int_get(mp_const_obj_t self_in) {
if (MP_OBJ_IS_SMALL_INT(self_in)) {
return MP_OBJ_SMALL_INT_VALUE(self_in);
} else {
mp_obj_int_t *self = self_in;
const mp_obj_int_t *self = self_in;
return mpz_as_int(&self->mpz);
}
}
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
if (MP_OBJ_IS_SMALL_INT(self_in)) {
return MP_OBJ_SMALL_INT_VALUE(self_in);
} else {
mp_obj_int_t *self = self_in;
machine_int_t value;
const mp_obj_int_t *self = self_in;
mp_int_t value;
if (mpz_as_int_checked(&self->mpz, &value)) {
return value;
} else {
@@ -272,7 +314,7 @@ machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
}
}
#if MICROPY_ENABLE_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
if (MP_OBJ_IS_SMALL_INT(self_in)) {
return MP_OBJ_SMALL_INT_VALUE(self_in);

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include <assert.h>
@@ -43,7 +69,7 @@ STATIC mp_obj_t list_extend_from_iter(mp_obj_t list, mp_obj_t iterable) {
}
STATIC mp_obj_t list_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// TODO check n_kw == 0
mp_arg_check_num(n_args, n_kw, 0, 1, false);
switch (n_args) {
case 0:
@@ -51,17 +77,13 @@ STATIC mp_obj_t list_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
return mp_obj_new_list(0, NULL);
case 1:
{
default: {
// make list from iterable
// TODO: optimize list/tuple
mp_obj_t list = mp_obj_new_list(0, NULL);
return list_extend_from_iter(list, args[0]);
}
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "list takes at most 1 argument, %d given", n_args));
}
return NULL;
}
// Don't pass MP_BINARY_OP_NOT_EQUAL here
@@ -81,7 +103,7 @@ STATIC mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
switch (op) {
case MP_UNARY_OP_BOOL: return MP_BOOL(self->len != 0);
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
default: return MP_OBJ_NOT_SUPPORTED;
default: return MP_OBJ_NULL; // op not supported
}
}
@@ -90,24 +112,24 @@ STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
switch (op) {
case MP_BINARY_OP_ADD: {
if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
return NULL;
return MP_OBJ_NULL; // op not supported
}
mp_obj_list_t *p = rhs;
mp_obj_list_t *s = list_new(o->len + p->len);
m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
mp_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
return s;
}
case MP_BINARY_OP_INPLACE_ADD: {
if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
return NULL;
return MP_OBJ_NULL; // op not supported
}
list_extend(lhs, rhs);
return o;
}
case MP_BINARY_OP_MULTIPLY: {
machine_int_t n;
mp_int_t n;
if (!mp_obj_get_int_maybe(rhs, &n)) {
return NULL;
return MP_OBJ_NULL; // op not supported
}
mp_obj_list_t *s = list_new(o->len * n);
mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
@@ -121,33 +143,82 @@ STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
return MP_BOOL(list_cmp_helper(op, lhs, rhs));
default:
return MP_OBJ_NOT_SUPPORTED;
return MP_OBJ_NULL; // op not supported
}
}
STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
if (value == MP_OBJ_NULL) {
// delete
#if MICROPY_PY_BUILTINS_SLICE
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
mp_obj_list_t *self = self_in;
mp_bound_slice_t slice;
if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
assert(0);
}
int len_adj = slice.start - slice.stop;
//printf("Len adj: %d\n", len_adj);
assert(len_adj <= 0);
mp_seq_replace_slice_no_grow(self->items, self->len, slice.start, slice.stop, self->items/*NULL*/, 0, mp_obj_t);
// Clear "freed" elements at the end of list
mp_seq_clear(self->items, self->len + len_adj, self->len, sizeof(*self->items));
self->len += len_adj;
return mp_const_none;
}
#endif
mp_obj_t args[2] = {self_in, index};
list_pop(2, args);
return mp_const_none;
} else if (value == MP_OBJ_SENTINEL) {
// load
mp_obj_list_t *self = self_in;
#if MICROPY_ENABLE_SLICE
#if MICROPY_PY_BUILTINS_SLICE
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
machine_uint_t start, stop;
if (!m_seq_get_fast_slice_indexes(self->len, index, &start, &stop)) {
assert(0);
mp_bound_slice_t slice;
if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
return mp_seq_extract_slice(self->len, self->items, &slice);
}
mp_obj_list_t *res = list_new(stop - start);
m_seq_copy(res->items, self->items + start, res->len, mp_obj_t);
mp_obj_list_t *res = list_new(slice.stop - slice.start);
mp_seq_copy(res->items, self->items + slice.start, res->len, mp_obj_t);
return res;
}
#endif
uint index_val = mp_get_index(self->base.type, self->len, index, false);
return self->items[index_val];
} else {
#if MICROPY_PY_BUILTINS_SLICE
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
mp_obj_list_t *self = self_in;
assert(MP_OBJ_IS_TYPE(value, &mp_type_list));
mp_obj_list_t *slice = value;
mp_bound_slice_t slice_out;
if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice_out)) {
assert(0);
}
int len_adj = slice->len - (slice_out.stop - slice_out.start);
//printf("Len adj: %d\n", len_adj);
if (len_adj > 0) {
if (self->len + len_adj > self->alloc) {
// TODO: Might optimize memory copies here by checking if block can
// be grown inplace or not
self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + len_adj);
self->alloc = self->len + len_adj;
}
mp_seq_replace_slice_grow_inplace(self->items, self->len,
slice_out.start, slice_out.stop, slice->items, slice->len, len_adj, mp_obj_t);
} else {
mp_seq_replace_slice_no_grow(self->items, self->len,
slice_out.start, slice_out.stop, slice->items, slice->len, mp_obj_t);
// Clear "freed" elements at the end of list
mp_seq_clear(self->items, self->len + len_adj, self->len, sizeof(*self->items));
// TODO: apply allocation policy re: alloc_size
}
self->len += len_adj;
return mp_const_none;
}
#endif
mp_obj_list_store(self_in, index, value);
return mp_const_none;
}
@@ -417,7 +488,7 @@ void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
typedef struct _mp_obj_list_it_t {
mp_obj_base_t base;
mp_obj_list_t *list;
machine_uint_t cur;
mp_uint_t cur;
} mp_obj_list_it_t;
mp_obj_t list_it_iternext(mp_obj_t self_in) {

View File

@@ -1,6 +1,32 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
typedef struct _mp_obj_list_t {
mp_obj_base_t base;
machine_uint_t alloc;
machine_uint_t len;
mp_uint_t alloc;
mp_uint_t len;
mp_obj_t *items;
} mp_obj_list_t;

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <assert.h>
@@ -10,7 +36,7 @@
typedef struct _mp_obj_map_t {
mp_obj_base_t base;
machine_uint_t n_iters;
mp_uint_t n_iters;
mp_obj_t fun;
mp_obj_t iters[];
} mp_obj_map_t;
@@ -30,10 +56,6 @@ STATIC mp_obj_t map_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_
return o;
}
STATIC mp_obj_t map_getiter(mp_obj_t self_in) {
return self_in;
}
STATIC mp_obj_t map_iternext(mp_obj_t self_in) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_map));
mp_obj_map_t *self = self_in;
@@ -54,6 +76,6 @@ const mp_obj_type_t mp_type_map = {
{ &mp_type_type },
.name = MP_QSTR_map,
.make_new = map_make_new,
.getiter = map_getiter,
.getiter = mp_identity,
.iternext = map_iternext,
};

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <assert.h>

View File

@@ -1,3 +1,29 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
void mp_module_init(void);
void mp_module_deinit(void);
mp_obj_t mp_module_get(qstr module_name);

Some files were not shown because too many files have changed in this diff Show More