Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
Alexander Steffen | 299bc62 | 2017-06-29 23:14:58 +0200 | [diff] [blame] | 26 | #ifndef MICROPY_INCLUDED_PY_RUNTIME0_H |
| 27 | #define MICROPY_INCLUDED_PY_RUNTIME0_H |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 28 | |
Damien George | 5e3006f | 2024-02-09 17:38:25 +1100 | [diff] [blame] | 29 | // These constants are used by: |
| 30 | // - mp_raw_code_t::is_generator (only MP_SCOPE_FLAG_GENERATOR) |
| 31 | // - scope_t::scope_flags (16 bits) |
| 32 | // - MP_BC_PRELUDE_SIG_ENCODE macro, masked by MP_SCOPE_FLAG_ALL_SIG (4 bits) |
| 33 | // - tools/mpy_ld.py, when generating mpy files (maximum 7 bits) |
Damien George | b47e155 | 2019-10-06 23:29:40 +1100 | [diff] [blame] | 34 | #define MP_SCOPE_FLAG_ALL_SIG (0x0f) |
Damien George | b5ebfad | 2019-09-16 22:12:59 +1000 | [diff] [blame] | 35 | #define MP_SCOPE_FLAG_GENERATOR (0x01) |
Damien George | 7f70b60 | 2015-08-17 22:39:03 +0100 | [diff] [blame] | 36 | #define MP_SCOPE_FLAG_VARKEYWORDS (0x02) |
Damien George | b5ebfad | 2019-09-16 22:12:59 +1000 | [diff] [blame] | 37 | #define MP_SCOPE_FLAG_VARARGS (0x04) |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 38 | #define MP_SCOPE_FLAG_DEFKWARGS (0x08) |
Damien George | 4f3d942 | 2018-09-13 22:03:48 +1000 | [diff] [blame] | 39 | #define MP_SCOPE_FLAG_REFGLOBALS (0x10) // used only if native emitter enabled |
Damien George | 7d4b6cc | 2018-09-27 23:27:53 +1000 | [diff] [blame] | 40 | #define MP_SCOPE_FLAG_HASCONSTS (0x20) // used only if native emitter enabled |
Damien George | b47e155 | 2019-10-06 23:29:40 +1100 | [diff] [blame] | 41 | #define MP_SCOPE_FLAG_VIPERRET_POS (6) // 3 bits used for viper return type, to pass from compiler to native emitter |
| 42 | #define MP_SCOPE_FLAG_VIPERRELOC (0x10) // used only when loading viper from .mpy |
| 43 | #define MP_SCOPE_FLAG_VIPERRODATA (0x20) // used only when loading viper from .mpy |
| 44 | #define MP_SCOPE_FLAG_VIPERBSS (0x40) // used only when loading viper from .mpy |
Damien George | 8725f8f | 2014-02-15 19:33:11 +0000 | [diff] [blame] | 45 | |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 46 | // types for native (viper) function signature |
| 47 | #define MP_NATIVE_TYPE_OBJ (0x00) |
| 48 | #define MP_NATIVE_TYPE_BOOL (0x01) |
| 49 | #define MP_NATIVE_TYPE_INT (0x02) |
| 50 | #define MP_NATIVE_TYPE_UINT (0x03) |
Damien George | 5f3e005 | 2016-02-02 23:16:05 +0000 | [diff] [blame] | 51 | #define MP_NATIVE_TYPE_PTR (0x04) |
| 52 | #define MP_NATIVE_TYPE_PTR8 (0x05) |
| 53 | #define MP_NATIVE_TYPE_PTR16 (0x06) |
| 54 | #define MP_NATIVE_TYPE_PTR32 (0x07) |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 55 | |
Jim Mussared | a64f2fd | 2023-08-25 15:43:50 +1000 | [diff] [blame] | 56 | // Not use for viper, but for dynamic native modules |
| 57 | #define MP_NATIVE_TYPE_QSTR (0x08) |
| 58 | |
Damien George | 323d478 | 2019-10-29 12:08:07 +1100 | [diff] [blame] | 59 | // Bytecode and runtime boundaries for unary ops |
| 60 | #define MP_UNARY_OP_NUM_BYTECODE (MP_UNARY_OP_NOT + 1) |
| 61 | #define MP_UNARY_OP_NUM_RUNTIME (MP_UNARY_OP_SIZEOF + 1) |
| 62 | |
| 63 | // Bytecode and runtime boundaries for binary ops |
| 64 | #define MP_BINARY_OP_NUM_BYTECODE (MP_BINARY_OP_POWER + 1) |
| 65 | #if MICROPY_PY_REVERSE_SPECIAL_METHODS |
| 66 | #define MP_BINARY_OP_NUM_RUNTIME (MP_BINARY_OP_REVERSE_POWER + 1) |
| 67 | #else |
| 68 | #define MP_BINARY_OP_NUM_RUNTIME (MP_BINARY_OP_CONTAINS + 1) |
| 69 | #endif |
| 70 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 71 | typedef enum { |
Paul Sokolovsky | 9d836fe | 2017-09-25 16:35:19 -0700 | [diff] [blame] | 72 | // These ops may appear in the bytecode. Changing this group |
| 73 | // in any way requires changing the bytecode version. |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 74 | MP_UNARY_OP_POSITIVE, |
| 75 | MP_UNARY_OP_NEGATIVE, |
| 76 | MP_UNARY_OP_INVERT, |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 77 | MP_UNARY_OP_NOT, |
Paul Sokolovsky | 9d836fe | 2017-09-25 16:35:19 -0700 | [diff] [blame] | 78 | |
| 79 | // Following ops cannot appear in the bytecode |
Damien George | 323d478 | 2019-10-29 12:08:07 +1100 | [diff] [blame] | 80 | MP_UNARY_OP_BOOL, // __bool__ |
Paul Sokolovsky | 9d836fe | 2017-09-25 16:35:19 -0700 | [diff] [blame] | 81 | MP_UNARY_OP_LEN, // __len__ |
| 82 | MP_UNARY_OP_HASH, // __hash__; must return a small int |
Paul Sokolovsky | 9dce823 | 2017-09-18 00:06:43 +0300 | [diff] [blame] | 83 | MP_UNARY_OP_ABS, // __abs__ |
Damien George | 48ffd65 | 2023-05-25 10:57:08 +1000 | [diff] [blame] | 84 | MP_UNARY_OP_INT_MAYBE, // __int__; must return MP_OBJ_NULL, or an object satisfying mp_obj_is_int() |
Andrew Leech | 1e87b56 | 2022-07-04 17:35:46 +1000 | [diff] [blame] | 85 | MP_UNARY_OP_FLOAT_MAYBE, // __float__ |
| 86 | MP_UNARY_OP_COMPLEX_MAYBE, // __complex__ |
Paul Sokolovsky | bfc2092 | 2017-08-11 09:42:39 +0300 | [diff] [blame] | 87 | MP_UNARY_OP_SIZEOF, // for sys.getsizeof() |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 88 | } mp_unary_op_t; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 89 | |
| 90 | typedef enum { |
Damien George | 323d478 | 2019-10-29 12:08:07 +1100 | [diff] [blame] | 91 | // The following 9+13+13 ops are used in bytecode and changing |
| 92 | // them requires changing the bytecode version. |
| 93 | |
Damien George | 6ce7c05 | 2019-07-25 12:10:45 +1000 | [diff] [blame] | 94 | // 9 relational operations, should return a bool; order of first 6 matches corresponding mp_token_kind_t |
Paul Sokolovsky | d4d1c45 | 2017-09-07 10:55:43 +0300 | [diff] [blame] | 95 | MP_BINARY_OP_LESS, |
| 96 | MP_BINARY_OP_MORE, |
| 97 | MP_BINARY_OP_EQUAL, |
| 98 | MP_BINARY_OP_LESS_EQUAL, |
| 99 | MP_BINARY_OP_MORE_EQUAL, |
Paul Sokolovsky | d4d1c45 | 2017-09-07 10:55:43 +0300 | [diff] [blame] | 100 | MP_BINARY_OP_NOT_EQUAL, |
| 101 | MP_BINARY_OP_IN, |
| 102 | MP_BINARY_OP_IS, |
| 103 | MP_BINARY_OP_EXCEPTION_MATCH, |
Paul Sokolovsky | d4d1c45 | 2017-09-07 10:55:43 +0300 | [diff] [blame] | 104 | |
Damien George | 2069c56 | 2019-07-25 13:15:54 +1000 | [diff] [blame] | 105 | // 13 inplace arithmetic operations; order matches corresponding mp_token_kind_t |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 106 | MP_BINARY_OP_INPLACE_OR, |
| 107 | MP_BINARY_OP_INPLACE_XOR, |
Damien George | c5029bc | 2015-06-13 22:00:10 +0100 | [diff] [blame] | 108 | MP_BINARY_OP_INPLACE_AND, |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 109 | MP_BINARY_OP_INPLACE_LSHIFT, |
| 110 | MP_BINARY_OP_INPLACE_RSHIFT, |
| 111 | MP_BINARY_OP_INPLACE_ADD, |
Paul Sokolovsky | c460f6f | 2017-09-07 13:37:33 +0300 | [diff] [blame] | 112 | MP_BINARY_OP_INPLACE_SUBTRACT, |
Damien George | c5029bc | 2015-06-13 22:00:10 +0100 | [diff] [blame] | 113 | MP_BINARY_OP_INPLACE_MULTIPLY, |
Damien George | 2069c56 | 2019-07-25 13:15:54 +1000 | [diff] [blame] | 114 | MP_BINARY_OP_INPLACE_MAT_MULTIPLY, |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 115 | MP_BINARY_OP_INPLACE_FLOOR_DIVIDE, |
| 116 | MP_BINARY_OP_INPLACE_TRUE_DIVIDE, |
| 117 | MP_BINARY_OP_INPLACE_MODULO, |
| 118 | MP_BINARY_OP_INPLACE_POWER, |
Paul Sokolovsky | 50b9329 | 2017-09-07 11:26:42 +0300 | [diff] [blame] | 119 | |
Damien George | 2069c56 | 2019-07-25 13:15:54 +1000 | [diff] [blame] | 120 | // 13 normal arithmetic operations; order matches corresponding mp_token_kind_t |
Paul Sokolovsky | b8ee7ab | 2017-09-08 00:10:10 +0300 | [diff] [blame] | 121 | MP_BINARY_OP_OR, |
| 122 | MP_BINARY_OP_XOR, |
| 123 | MP_BINARY_OP_AND, |
| 124 | MP_BINARY_OP_LSHIFT, |
| 125 | MP_BINARY_OP_RSHIFT, |
| 126 | MP_BINARY_OP_ADD, |
Paul Sokolovsky | b8ee7ab | 2017-09-08 00:10:10 +0300 | [diff] [blame] | 127 | MP_BINARY_OP_SUBTRACT, |
| 128 | MP_BINARY_OP_MULTIPLY, |
Damien George | 2069c56 | 2019-07-25 13:15:54 +1000 | [diff] [blame] | 129 | MP_BINARY_OP_MAT_MULTIPLY, |
Paul Sokolovsky | b8ee7ab | 2017-09-08 00:10:10 +0300 | [diff] [blame] | 130 | MP_BINARY_OP_FLOOR_DIVIDE, |
| 131 | MP_BINARY_OP_TRUE_DIVIDE, |
| 132 | MP_BINARY_OP_MODULO, |
| 133 | MP_BINARY_OP_POWER, |
| 134 | |
Paul Sokolovsky | 50b9329 | 2017-09-07 11:26:42 +0300 | [diff] [blame] | 135 | // Operations below this line don't appear in bytecode, they |
| 136 | // just identify special methods. |
| 137 | |
Damien George | 323d478 | 2019-10-29 12:08:07 +1100 | [diff] [blame] | 138 | // This is not emitted by the compiler but is supported by the runtime. |
| 139 | // It must follow immediately after MP_BINARY_OP_POWER. |
| 140 | MP_BINARY_OP_DIVMOD, |
| 141 | |
| 142 | // The runtime will convert MP_BINARY_OP_IN to this operator with swapped args. |
| 143 | // A type should implement this containment operator instead of MP_BINARY_OP_IN. |
| 144 | MP_BINARY_OP_CONTAINS, |
| 145 | |
| 146 | // 13 MP_BINARY_OP_REVERSE_* operations must be in the same order as MP_BINARY_OP_*, |
| 147 | // and be the last ones supported by the runtime. |
| 148 | MP_BINARY_OP_REVERSE_OR, |
Paul Sokolovsky | eb84a83 | 2017-09-10 17:05:20 +0300 | [diff] [blame] | 149 | MP_BINARY_OP_REVERSE_XOR, |
| 150 | MP_BINARY_OP_REVERSE_AND, |
| 151 | MP_BINARY_OP_REVERSE_LSHIFT, |
| 152 | MP_BINARY_OP_REVERSE_RSHIFT, |
| 153 | MP_BINARY_OP_REVERSE_ADD, |
Paul Sokolovsky | eb84a83 | 2017-09-10 17:05:20 +0300 | [diff] [blame] | 154 | MP_BINARY_OP_REVERSE_SUBTRACT, |
| 155 | MP_BINARY_OP_REVERSE_MULTIPLY, |
Damien George | 2069c56 | 2019-07-25 13:15:54 +1000 | [diff] [blame] | 156 | MP_BINARY_OP_REVERSE_MAT_MULTIPLY, |
Paul Sokolovsky | eb84a83 | 2017-09-10 17:05:20 +0300 | [diff] [blame] | 157 | MP_BINARY_OP_REVERSE_FLOOR_DIVIDE, |
| 158 | MP_BINARY_OP_REVERSE_TRUE_DIVIDE, |
| 159 | MP_BINARY_OP_REVERSE_MODULO, |
| 160 | MP_BINARY_OP_REVERSE_POWER, |
Damien George | 0864a69 | 2017-10-03 23:34:28 +1100 | [diff] [blame] | 161 | |
| 162 | // These 2 are not supported by the runtime and must be synthesised by the emitter |
| 163 | MP_BINARY_OP_NOT_IN, |
| 164 | MP_BINARY_OP_IS_NOT, |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 165 | } mp_binary_op_t; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 166 | |
Alexander Steffen | 299bc62 | 2017-06-29 23:14:58 +0200 | [diff] [blame] | 167 | #endif // MICROPY_INCLUDED_PY_RUNTIME0_H |