2014-05-03 22:27:38 +00:00
|
|
|
/*
|
2017-06-30 07:22:17 +00:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 22:27:38 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 21:14:58 +00:00
|
|
|
#ifndef MICROPY_INCLUDED_PY_RUNTIME0_H
|
|
|
|
#define MICROPY_INCLUDED_PY_RUNTIME0_H
|
2014-05-03 22:27:38 +00:00
|
|
|
|
2018-09-27 13:27:53 +00:00
|
|
|
// The first four must fit in 8 bits, see emitbc.c
|
|
|
|
// The remaining must fit in 16 bits, see scope.h
|
2019-10-06 12:29:40 +00:00
|
|
|
#define MP_SCOPE_FLAG_ALL_SIG (0x0f)
|
2019-09-16 12:12:59 +00:00
|
|
|
#define MP_SCOPE_FLAG_GENERATOR (0x01)
|
2015-08-17 21:39:03 +00:00
|
|
|
#define MP_SCOPE_FLAG_VARKEYWORDS (0x02)
|
2019-09-16 12:12:59 +00:00
|
|
|
#define MP_SCOPE_FLAG_VARARGS (0x04)
|
2015-10-22 22:45:37 +00:00
|
|
|
#define MP_SCOPE_FLAG_DEFKWARGS (0x08)
|
py: Fix native functions so they run with their correct globals context.
Prior to this commit a function compiled with the native decorator
@micropython.native would not work correctly when accessing global
variables, because the globals dict was not being set upon function entry.
This commit fixes this problem by, upon function entry, setting as the
current globals dict the globals dict context the function was defined
within, as per normal Python semantics, and as bytecode does. Upon
function exit the original globals dict is restored.
In order to restore the globals dict when an exception is raised the native
function must guard its internals with an nlr_push/nlr_pop pair. Because
this push/pop is relatively expensive, in both C stack usage for the
nlr_buf_t and CPU execution time, the implementation here optimises things
as much as possible. First, the compiler keeps track of whether a function
even needs to access global variables. Using this information the native
emitter then generates three different kinds of code:
1. no globals used, no exception handlers: no nlr handling code and no
setting of the globals dict.
2. globals used, no exception handlers: an nlr_buf_t is allocated on the
C stack but it is not used if the globals dict is unchanged, saving
execution time because nlr_push/nlr_pop don't need to run.
3. function has exception handlers, may use globals: an nlr_buf_t is
allocated and nlr_push/nlr_pop are always called.
In the end, native functions that don't access globals and don't have
exception handlers will run more efficiently than those that do.
Fixes issue #1573.
2018-09-13 12:03:48 +00:00
|
|
|
#define MP_SCOPE_FLAG_REFGLOBALS (0x10) // used only if native emitter enabled
|
2018-09-27 13:27:53 +00:00
|
|
|
#define MP_SCOPE_FLAG_HASCONSTS (0x20) // used only if native emitter enabled
|
2019-10-06 12:29:40 +00:00
|
|
|
#define MP_SCOPE_FLAG_VIPERRET_POS (6) // 3 bits used for viper return type, to pass from compiler to native emitter
|
|
|
|
#define MP_SCOPE_FLAG_VIPERRELOC (0x10) // used only when loading viper from .mpy
|
|
|
|
#define MP_SCOPE_FLAG_VIPERRODATA (0x20) // used only when loading viper from .mpy
|
|
|
|
#define MP_SCOPE_FLAG_VIPERBSS (0x40) // used only when loading viper from .mpy
|
2014-02-15 19:33:11 +00:00
|
|
|
|
2014-08-15 15:45:41 +00:00
|
|
|
// types for native (viper) function signature
|
|
|
|
#define MP_NATIVE_TYPE_OBJ (0x00)
|
|
|
|
#define MP_NATIVE_TYPE_BOOL (0x01)
|
|
|
|
#define MP_NATIVE_TYPE_INT (0x02)
|
|
|
|
#define MP_NATIVE_TYPE_UINT (0x03)
|
2016-02-02 23:16:05 +00:00
|
|
|
#define MP_NATIVE_TYPE_PTR (0x04)
|
|
|
|
#define MP_NATIVE_TYPE_PTR8 (0x05)
|
|
|
|
#define MP_NATIVE_TYPE_PTR16 (0x06)
|
|
|
|
#define MP_NATIVE_TYPE_PTR32 (0x07)
|
2014-08-15 15:45:41 +00:00
|
|
|
|
2019-10-29 01:08:07 +00:00
|
|
|
// Bytecode and runtime boundaries for unary ops
|
|
|
|
#define MP_UNARY_OP_NUM_BYTECODE (MP_UNARY_OP_NOT + 1)
|
|
|
|
#define MP_UNARY_OP_NUM_RUNTIME (MP_UNARY_OP_SIZEOF + 1)
|
|
|
|
|
|
|
|
// Bytecode and runtime boundaries for binary ops
|
|
|
|
#define MP_BINARY_OP_NUM_BYTECODE (MP_BINARY_OP_POWER + 1)
|
|
|
|
#if MICROPY_PY_REVERSE_SPECIAL_METHODS
|
|
|
|
#define MP_BINARY_OP_NUM_RUNTIME (MP_BINARY_OP_REVERSE_POWER + 1)
|
|
|
|
#else
|
|
|
|
#define MP_BINARY_OP_NUM_RUNTIME (MP_BINARY_OP_CONTAINS + 1)
|
|
|
|
#endif
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
typedef enum {
|
2017-09-25 23:35:19 +00:00
|
|
|
// These ops may appear in the bytecode. Changing this group
|
|
|
|
// in any way requires changing the bytecode version.
|
2014-03-30 12:35:08 +00:00
|
|
|
MP_UNARY_OP_POSITIVE,
|
|
|
|
MP_UNARY_OP_NEGATIVE,
|
|
|
|
MP_UNARY_OP_INVERT,
|
|
|
|
MP_UNARY_OP_NOT,
|
2017-09-25 23:35:19 +00:00
|
|
|
|
|
|
|
// Following ops cannot appear in the bytecode
|
2019-10-29 01:08:07 +00:00
|
|
|
MP_UNARY_OP_BOOL, // __bool__
|
2017-09-25 23:35:19 +00:00
|
|
|
MP_UNARY_OP_LEN, // __len__
|
|
|
|
MP_UNARY_OP_HASH, // __hash__; must return a small int
|
2017-09-17 21:06:43 +00:00
|
|
|
MP_UNARY_OP_ABS, // __abs__
|
2018-08-29 14:59:36 +00:00
|
|
|
MP_UNARY_OP_INT, // __int__
|
2017-08-11 06:42:39 +00:00
|
|
|
MP_UNARY_OP_SIZEOF, // for sys.getsizeof()
|
2014-03-30 12:35:08 +00:00
|
|
|
} mp_unary_op_t;
|
2013-12-21 18:17:45 +00:00
|
|
|
|
|
|
|
typedef enum {
|
2019-10-29 01:08:07 +00:00
|
|
|
// The following 9+13+13 ops are used in bytecode and changing
|
|
|
|
// them requires changing the bytecode version.
|
|
|
|
|
2019-07-25 02:10:45 +00:00
|
|
|
// 9 relational operations, should return a bool; order of first 6 matches corresponding mp_token_kind_t
|
2017-09-07 07:55:43 +00:00
|
|
|
MP_BINARY_OP_LESS,
|
|
|
|
MP_BINARY_OP_MORE,
|
|
|
|
MP_BINARY_OP_EQUAL,
|
|
|
|
MP_BINARY_OP_LESS_EQUAL,
|
|
|
|
MP_BINARY_OP_MORE_EQUAL,
|
|
|
|
MP_BINARY_OP_NOT_EQUAL,
|
|
|
|
MP_BINARY_OP_IN,
|
|
|
|
MP_BINARY_OP_IS,
|
|
|
|
MP_BINARY_OP_EXCEPTION_MATCH,
|
|
|
|
|
2019-07-25 03:15:54 +00:00
|
|
|
// 13 inplace arithmetic operations; order matches corresponding mp_token_kind_t
|
2014-03-30 12:35:08 +00:00
|
|
|
MP_BINARY_OP_INPLACE_OR,
|
|
|
|
MP_BINARY_OP_INPLACE_XOR,
|
2015-06-13 21:00:10 +00:00
|
|
|
MP_BINARY_OP_INPLACE_AND,
|
2014-03-30 12:35:08 +00:00
|
|
|
MP_BINARY_OP_INPLACE_LSHIFT,
|
|
|
|
MP_BINARY_OP_INPLACE_RSHIFT,
|
|
|
|
MP_BINARY_OP_INPLACE_ADD,
|
2017-09-07 10:37:33 +00:00
|
|
|
MP_BINARY_OP_INPLACE_SUBTRACT,
|
2015-06-13 21:00:10 +00:00
|
|
|
MP_BINARY_OP_INPLACE_MULTIPLY,
|
2019-07-25 03:15:54 +00:00
|
|
|
MP_BINARY_OP_INPLACE_MAT_MULTIPLY,
|
2014-03-30 12:35:08 +00:00
|
|
|
MP_BINARY_OP_INPLACE_FLOOR_DIVIDE,
|
|
|
|
MP_BINARY_OP_INPLACE_TRUE_DIVIDE,
|
|
|
|
MP_BINARY_OP_INPLACE_MODULO,
|
|
|
|
MP_BINARY_OP_INPLACE_POWER,
|
2017-09-07 08:26:42 +00:00
|
|
|
|
2019-07-25 03:15:54 +00:00
|
|
|
// 13 normal arithmetic operations; order matches corresponding mp_token_kind_t
|
2017-09-07 21:10:10 +00:00
|
|
|
MP_BINARY_OP_OR,
|
|
|
|
MP_BINARY_OP_XOR,
|
|
|
|
MP_BINARY_OP_AND,
|
|
|
|
MP_BINARY_OP_LSHIFT,
|
|
|
|
MP_BINARY_OP_RSHIFT,
|
|
|
|
MP_BINARY_OP_ADD,
|
|
|
|
MP_BINARY_OP_SUBTRACT,
|
|
|
|
MP_BINARY_OP_MULTIPLY,
|
2019-07-25 03:15:54 +00:00
|
|
|
MP_BINARY_OP_MAT_MULTIPLY,
|
2017-09-07 21:10:10 +00:00
|
|
|
MP_BINARY_OP_FLOOR_DIVIDE,
|
|
|
|
MP_BINARY_OP_TRUE_DIVIDE,
|
|
|
|
MP_BINARY_OP_MODULO,
|
|
|
|
MP_BINARY_OP_POWER,
|
|
|
|
|
2017-09-07 08:26:42 +00:00
|
|
|
// Operations below this line don't appear in bytecode, they
|
|
|
|
// just identify special methods.
|
|
|
|
|
2019-10-29 01:08:07 +00:00
|
|
|
// This is not emitted by the compiler but is supported by the runtime.
|
|
|
|
// It must follow immediately after MP_BINARY_OP_POWER.
|
|
|
|
MP_BINARY_OP_DIVMOD,
|
|
|
|
|
|
|
|
// The runtime will convert MP_BINARY_OP_IN to this operator with swapped args.
|
|
|
|
// A type should implement this containment operator instead of MP_BINARY_OP_IN.
|
|
|
|
MP_BINARY_OP_CONTAINS,
|
|
|
|
|
|
|
|
// 13 MP_BINARY_OP_REVERSE_* operations must be in the same order as MP_BINARY_OP_*,
|
|
|
|
// and be the last ones supported by the runtime.
|
|
|
|
MP_BINARY_OP_REVERSE_OR,
|
2017-09-10 14:05:20 +00:00
|
|
|
MP_BINARY_OP_REVERSE_XOR,
|
|
|
|
MP_BINARY_OP_REVERSE_AND,
|
|
|
|
MP_BINARY_OP_REVERSE_LSHIFT,
|
|
|
|
MP_BINARY_OP_REVERSE_RSHIFT,
|
|
|
|
MP_BINARY_OP_REVERSE_ADD,
|
|
|
|
MP_BINARY_OP_REVERSE_SUBTRACT,
|
|
|
|
MP_BINARY_OP_REVERSE_MULTIPLY,
|
2019-07-25 03:15:54 +00:00
|
|
|
MP_BINARY_OP_REVERSE_MAT_MULTIPLY,
|
2017-09-10 14:05:20 +00:00
|
|
|
MP_BINARY_OP_REVERSE_FLOOR_DIVIDE,
|
|
|
|
MP_BINARY_OP_REVERSE_TRUE_DIVIDE,
|
|
|
|
MP_BINARY_OP_REVERSE_MODULO,
|
|
|
|
MP_BINARY_OP_REVERSE_POWER,
|
2017-09-07 09:54:58 +00:00
|
|
|
|
2017-10-03 12:34:28 +00:00
|
|
|
// These 2 are not supported by the runtime and must be synthesised by the emitter
|
|
|
|
MP_BINARY_OP_NOT_IN,
|
|
|
|
MP_BINARY_OP_IS_NOT,
|
2014-03-30 12:35:08 +00:00
|
|
|
} mp_binary_op_t;
|
2013-12-21 18:17:45 +00:00
|
|
|
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 21:14:58 +00:00
|
|
|
#endif // MICROPY_INCLUDED_PY_RUNTIME0_H
|