blob: 9c6f0e079fb5a5b9e0c6a8b6489b0b14ddd64c4e [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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 Steffen299bc622017-06-29 23:14:58 +020026#ifndef MICROPY_INCLUDED_PY_RUNTIME0_H
27#define MICROPY_INCLUDED_PY_RUNTIME0_H
Damien George04b91472014-05-03 23:27:38 +010028
Damien George5e3006f2024-02-09 17:38:25 +110029// 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 Georgeb47e1552019-10-06 23:29:40 +110034#define MP_SCOPE_FLAG_ALL_SIG (0x0f)
Damien Georgeb5ebfad2019-09-16 22:12:59 +100035#define MP_SCOPE_FLAG_GENERATOR (0x01)
Damien George7f70b602015-08-17 22:39:03 +010036#define MP_SCOPE_FLAG_VARKEYWORDS (0x02)
Damien Georgeb5ebfad2019-09-16 22:12:59 +100037#define MP_SCOPE_FLAG_VARARGS (0x04)
Damien George3a3db4d2015-10-22 23:45:37 +010038#define MP_SCOPE_FLAG_DEFKWARGS (0x08)
Damien George4f3d9422018-09-13 22:03:48 +100039#define MP_SCOPE_FLAG_REFGLOBALS (0x10) // used only if native emitter enabled
Damien George7d4b6cc2018-09-27 23:27:53 +100040#define MP_SCOPE_FLAG_HASCONSTS (0x20) // used only if native emitter enabled
Damien Georgeb47e1552019-10-06 23:29:40 +110041#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 George8725f8f2014-02-15 19:33:11 +000045
Damien George2ac4af62014-08-15 16:45:41 +010046// 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 George5f3e0052016-02-02 23:16:05 +000051#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 George2ac4af62014-08-15 16:45:41 +010055
Jim Mussareda64f2fd2023-08-25 15:43:50 +100056// Not use for viper, but for dynamic native modules
57#define MP_NATIVE_TYPE_QSTR (0x08)
58
Damien George323d4782019-10-29 12:08:07 +110059// 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
Damiend99b0522013-12-21 18:17:45 +000071typedef enum {
Paul Sokolovsky9d836fe2017-09-25 16:35:19 -070072 // These ops may appear in the bytecode. Changing this group
73 // in any way requires changing the bytecode version.
Damien Georged17926d2014-03-30 13:35:08 +010074 MP_UNARY_OP_POSITIVE,
75 MP_UNARY_OP_NEGATIVE,
76 MP_UNARY_OP_INVERT,
Damien Georged17926d2014-03-30 13:35:08 +010077 MP_UNARY_OP_NOT,
Paul Sokolovsky9d836fe2017-09-25 16:35:19 -070078
79 // Following ops cannot appear in the bytecode
Damien George323d4782019-10-29 12:08:07 +110080 MP_UNARY_OP_BOOL, // __bool__
Paul Sokolovsky9d836fe2017-09-25 16:35:19 -070081 MP_UNARY_OP_LEN, // __len__
82 MP_UNARY_OP_HASH, // __hash__; must return a small int
Paul Sokolovsky9dce8232017-09-18 00:06:43 +030083 MP_UNARY_OP_ABS, // __abs__
Damien George48ffd652023-05-25 10:57:08 +100084 MP_UNARY_OP_INT_MAYBE, // __int__; must return MP_OBJ_NULL, or an object satisfying mp_obj_is_int()
Andrew Leech1e87b562022-07-04 17:35:46 +100085 MP_UNARY_OP_FLOAT_MAYBE, // __float__
86 MP_UNARY_OP_COMPLEX_MAYBE, // __complex__
Paul Sokolovskybfc20922017-08-11 09:42:39 +030087 MP_UNARY_OP_SIZEOF, // for sys.getsizeof()
Damien Georged17926d2014-03-30 13:35:08 +010088} mp_unary_op_t;
Damiend99b0522013-12-21 18:17:45 +000089
90typedef enum {
Damien George323d4782019-10-29 12:08:07 +110091 // The following 9+13+13 ops are used in bytecode and changing
92 // them requires changing the bytecode version.
93
Damien George6ce7c052019-07-25 12:10:45 +100094 // 9 relational operations, should return a bool; order of first 6 matches corresponding mp_token_kind_t
Paul Sokolovskyd4d1c452017-09-07 10:55:43 +030095 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 Sokolovskyd4d1c452017-09-07 10:55:43 +0300100 MP_BINARY_OP_NOT_EQUAL,
101 MP_BINARY_OP_IN,
102 MP_BINARY_OP_IS,
103 MP_BINARY_OP_EXCEPTION_MATCH,
Paul Sokolovskyd4d1c452017-09-07 10:55:43 +0300104
Damien George2069c562019-07-25 13:15:54 +1000105 // 13 inplace arithmetic operations; order matches corresponding mp_token_kind_t
Damien Georged17926d2014-03-30 13:35:08 +0100106 MP_BINARY_OP_INPLACE_OR,
107 MP_BINARY_OP_INPLACE_XOR,
Damien Georgec5029bc2015-06-13 22:00:10 +0100108 MP_BINARY_OP_INPLACE_AND,
Damien Georged17926d2014-03-30 13:35:08 +0100109 MP_BINARY_OP_INPLACE_LSHIFT,
110 MP_BINARY_OP_INPLACE_RSHIFT,
111 MP_BINARY_OP_INPLACE_ADD,
Paul Sokolovskyc460f6f2017-09-07 13:37:33 +0300112 MP_BINARY_OP_INPLACE_SUBTRACT,
Damien Georgec5029bc2015-06-13 22:00:10 +0100113 MP_BINARY_OP_INPLACE_MULTIPLY,
Damien George2069c562019-07-25 13:15:54 +1000114 MP_BINARY_OP_INPLACE_MAT_MULTIPLY,
Damien Georged17926d2014-03-30 13:35:08 +0100115 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 Sokolovsky50b93292017-09-07 11:26:42 +0300119
Damien George2069c562019-07-25 13:15:54 +1000120 // 13 normal arithmetic operations; order matches corresponding mp_token_kind_t
Paul Sokolovskyb8ee7ab2017-09-08 00:10:10 +0300121 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 Sokolovskyb8ee7ab2017-09-08 00:10:10 +0300127 MP_BINARY_OP_SUBTRACT,
128 MP_BINARY_OP_MULTIPLY,
Damien George2069c562019-07-25 13:15:54 +1000129 MP_BINARY_OP_MAT_MULTIPLY,
Paul Sokolovskyb8ee7ab2017-09-08 00:10:10 +0300130 MP_BINARY_OP_FLOOR_DIVIDE,
131 MP_BINARY_OP_TRUE_DIVIDE,
132 MP_BINARY_OP_MODULO,
133 MP_BINARY_OP_POWER,
134
Paul Sokolovsky50b93292017-09-07 11:26:42 +0300135 // Operations below this line don't appear in bytecode, they
136 // just identify special methods.
137
Damien George323d4782019-10-29 12:08:07 +1100138 // 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 Sokolovskyeb84a832017-09-10 17:05:20 +0300149 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 Sokolovskyeb84a832017-09-10 17:05:20 +0300154 MP_BINARY_OP_REVERSE_SUBTRACT,
155 MP_BINARY_OP_REVERSE_MULTIPLY,
Damien George2069c562019-07-25 13:15:54 +1000156 MP_BINARY_OP_REVERSE_MAT_MULTIPLY,
Paul Sokolovskyeb84a832017-09-10 17:05:20 +0300157 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 George0864a692017-10-03 23:34:28 +1100161
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 Georged17926d2014-03-30 13:35:08 +0100165} mp_binary_op_t;
Damiend99b0522013-12-21 18:17:45 +0000166
Alexander Steffen299bc622017-06-29 23:14:58 +0200167#endif // MICROPY_INCLUDED_PY_RUNTIME0_H