blob: 3ba089bb37cc47cd26de45908691e87736f67be3 [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 George7d4b6cc2018-09-27 23:27:53 +100029// The first four must fit in 8 bits, see emitbc.c
30// The remaining must fit in 16 bits, see scope.h
Damien George7f70b602015-08-17 22:39:03 +010031#define MP_SCOPE_FLAG_VARARGS (0x01)
32#define MP_SCOPE_FLAG_VARKEYWORDS (0x02)
33#define MP_SCOPE_FLAG_GENERATOR (0x04)
Damien George3a3db4d2015-10-22 23:45:37 +010034#define MP_SCOPE_FLAG_DEFKWARGS (0x08)
Damien George4f3d9422018-09-13 22:03:48 +100035#define MP_SCOPE_FLAG_REFGLOBALS (0x10) // used only if native emitter enabled
Damien George7d4b6cc2018-09-27 23:27:53 +100036#define MP_SCOPE_FLAG_HASCONSTS (0x20) // used only if native emitter enabled
37#define MP_SCOPE_FLAG_VIPERRET_POS (6) // 3 bits used for viper return type
Damien George8725f8f2014-02-15 19:33:11 +000038
Damien George2ac4af62014-08-15 16:45:41 +010039// types for native (viper) function signature
40#define MP_NATIVE_TYPE_OBJ (0x00)
41#define MP_NATIVE_TYPE_BOOL (0x01)
42#define MP_NATIVE_TYPE_INT (0x02)
43#define MP_NATIVE_TYPE_UINT (0x03)
Damien George5f3e0052016-02-02 23:16:05 +000044#define MP_NATIVE_TYPE_PTR (0x04)
45#define MP_NATIVE_TYPE_PTR8 (0x05)
46#define MP_NATIVE_TYPE_PTR16 (0x06)
47#define MP_NATIVE_TYPE_PTR32 (0x07)
Damien George2ac4af62014-08-15 16:45:41 +010048
Damiend99b0522013-12-21 18:17:45 +000049typedef enum {
Paul Sokolovsky9d836fe2017-09-25 16:35:19 -070050 // These ops may appear in the bytecode. Changing this group
51 // in any way requires changing the bytecode version.
Damien Georged17926d2014-03-30 13:35:08 +010052 MP_UNARY_OP_POSITIVE,
53 MP_UNARY_OP_NEGATIVE,
54 MP_UNARY_OP_INVERT,
Damien Georged17926d2014-03-30 13:35:08 +010055 MP_UNARY_OP_NOT,
Paul Sokolovsky9d836fe2017-09-25 16:35:19 -070056
57 // Following ops cannot appear in the bytecode
Damien George0864a692017-10-03 23:34:28 +110058 MP_UNARY_OP_NUM_BYTECODE,
Paul Sokolovsky9d836fe2017-09-25 16:35:19 -070059
Damien George0864a692017-10-03 23:34:28 +110060 MP_UNARY_OP_BOOL = MP_UNARY_OP_NUM_BYTECODE, // __bool__
Paul Sokolovsky9d836fe2017-09-25 16:35:19 -070061 MP_UNARY_OP_LEN, // __len__
62 MP_UNARY_OP_HASH, // __hash__; must return a small int
Paul Sokolovsky9dce8232017-09-18 00:06:43 +030063 MP_UNARY_OP_ABS, // __abs__
Paul Sokolovskyb1d08722018-08-29 17:59:36 +030064 MP_UNARY_OP_INT, // __int__
Paul Sokolovskybfc20922017-08-11 09:42:39 +030065 MP_UNARY_OP_SIZEOF, // for sys.getsizeof()
Damien George0864a692017-10-03 23:34:28 +110066
67 MP_UNARY_OP_NUM_RUNTIME,
Damien Georged17926d2014-03-30 13:35:08 +010068} mp_unary_op_t;
Damiend99b0522013-12-21 18:17:45 +000069
Damien George0864a692017-10-03 23:34:28 +110070// Note: the first 9+12+12 of these are used in bytecode and changing
Damien George8edc2e42017-09-22 11:54:08 +100071// them requires changing the bytecode version.
Damiend99b0522013-12-21 18:17:45 +000072typedef enum {
Damien George6ce7c052019-07-25 12:10:45 +100073 // 9 relational operations, should return a bool; order of first 6 matches corresponding mp_token_kind_t
Paul Sokolovskyd4d1c452017-09-07 10:55:43 +030074 MP_BINARY_OP_LESS,
75 MP_BINARY_OP_MORE,
76 MP_BINARY_OP_EQUAL,
77 MP_BINARY_OP_LESS_EQUAL,
78 MP_BINARY_OP_MORE_EQUAL,
Paul Sokolovskyd4d1c452017-09-07 10:55:43 +030079 MP_BINARY_OP_NOT_EQUAL,
80 MP_BINARY_OP_IN,
81 MP_BINARY_OP_IS,
82 MP_BINARY_OP_EXCEPTION_MATCH,
Paul Sokolovskyd4d1c452017-09-07 10:55:43 +030083
Damien George2069c562019-07-25 13:15:54 +100084 // 13 inplace arithmetic operations; order matches corresponding mp_token_kind_t
Damien Georged17926d2014-03-30 13:35:08 +010085 MP_BINARY_OP_INPLACE_OR,
86 MP_BINARY_OP_INPLACE_XOR,
Damien Georgec5029bc2015-06-13 22:00:10 +010087 MP_BINARY_OP_INPLACE_AND,
Damien Georged17926d2014-03-30 13:35:08 +010088 MP_BINARY_OP_INPLACE_LSHIFT,
89 MP_BINARY_OP_INPLACE_RSHIFT,
90 MP_BINARY_OP_INPLACE_ADD,
Paul Sokolovskyc460f6f2017-09-07 13:37:33 +030091 MP_BINARY_OP_INPLACE_SUBTRACT,
Damien Georgec5029bc2015-06-13 22:00:10 +010092 MP_BINARY_OP_INPLACE_MULTIPLY,
Damien George2069c562019-07-25 13:15:54 +100093 MP_BINARY_OP_INPLACE_MAT_MULTIPLY,
Damien Georged17926d2014-03-30 13:35:08 +010094 MP_BINARY_OP_INPLACE_FLOOR_DIVIDE,
95 MP_BINARY_OP_INPLACE_TRUE_DIVIDE,
96 MP_BINARY_OP_INPLACE_MODULO,
97 MP_BINARY_OP_INPLACE_POWER,
Paul Sokolovsky50b93292017-09-07 11:26:42 +030098
Damien George2069c562019-07-25 13:15:54 +100099 // 13 normal arithmetic operations; order matches corresponding mp_token_kind_t
Paul Sokolovskyb8ee7ab2017-09-08 00:10:10 +0300100 MP_BINARY_OP_OR,
101 MP_BINARY_OP_XOR,
102 MP_BINARY_OP_AND,
103 MP_BINARY_OP_LSHIFT,
104 MP_BINARY_OP_RSHIFT,
105 MP_BINARY_OP_ADD,
Paul Sokolovskyb8ee7ab2017-09-08 00:10:10 +0300106 MP_BINARY_OP_SUBTRACT,
107 MP_BINARY_OP_MULTIPLY,
Damien George2069c562019-07-25 13:15:54 +1000108 MP_BINARY_OP_MAT_MULTIPLY,
Paul Sokolovskyb8ee7ab2017-09-08 00:10:10 +0300109 MP_BINARY_OP_FLOOR_DIVIDE,
110 MP_BINARY_OP_TRUE_DIVIDE,
111 MP_BINARY_OP_MODULO,
112 MP_BINARY_OP_POWER,
113
Paul Sokolovsky50b93292017-09-07 11:26:42 +0300114 // Operations below this line don't appear in bytecode, they
115 // just identify special methods.
Damien George0864a692017-10-03 23:34:28 +1100116 MP_BINARY_OP_NUM_BYTECODE,
Paul Sokolovsky50b93292017-09-07 11:26:42 +0300117
Paul Sokolovskyeb84a832017-09-10 17:05:20 +0300118 // MP_BINARY_OP_REVERSE_* must follow immediately after MP_BINARY_OP_*
119#if MICROPY_PY_REVERSE_SPECIAL_METHODS
Damien George0864a692017-10-03 23:34:28 +1100120 MP_BINARY_OP_REVERSE_OR = MP_BINARY_OP_NUM_BYTECODE,
Paul Sokolovskyeb84a832017-09-10 17:05:20 +0300121 MP_BINARY_OP_REVERSE_XOR,
122 MP_BINARY_OP_REVERSE_AND,
123 MP_BINARY_OP_REVERSE_LSHIFT,
124 MP_BINARY_OP_REVERSE_RSHIFT,
125 MP_BINARY_OP_REVERSE_ADD,
Paul Sokolovskyeb84a832017-09-10 17:05:20 +0300126 MP_BINARY_OP_REVERSE_SUBTRACT,
127 MP_BINARY_OP_REVERSE_MULTIPLY,
Damien George2069c562019-07-25 13:15:54 +1000128 MP_BINARY_OP_REVERSE_MAT_MULTIPLY,
Paul Sokolovskyeb84a832017-09-10 17:05:20 +0300129 MP_BINARY_OP_REVERSE_FLOOR_DIVIDE,
130 MP_BINARY_OP_REVERSE_TRUE_DIVIDE,
131 MP_BINARY_OP_REVERSE_MODULO,
132 MP_BINARY_OP_REVERSE_POWER,
133#endif
134
Damien George0864a692017-10-03 23:34:28 +1100135 // This is not emitted by the compiler but is supported by the runtime
136 MP_BINARY_OP_DIVMOD
137 #if !MICROPY_PY_REVERSE_SPECIAL_METHODS
138 = MP_BINARY_OP_NUM_BYTECODE
139 #endif
140 ,
Paul Sokolovsky6d4cac02017-09-07 12:54:58 +0300141
Damien George5e34a112017-11-24 13:04:24 +1100142 // 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
Damien George0864a692017-10-03 23:34:28 +1100146 MP_BINARY_OP_NUM_RUNTIME,
147
148 // These 2 are not supported by the runtime and must be synthesised by the emitter
149 MP_BINARY_OP_NOT_IN,
150 MP_BINARY_OP_IS_NOT,
Damien Georged17926d2014-03-30 13:35:08 +0100151} mp_binary_op_t;
Damiend99b0522013-12-21 18:17:45 +0000152
153typedef enum {
Damien Georgede710352018-10-14 23:56:46 +1100154 MP_F_CONST_NONE_OBJ = 0,
155 MP_F_CONST_FALSE_OBJ,
156 MP_F_CONST_TRUE_OBJ,
157 MP_F_CONVERT_OBJ_TO_NATIVE,
Damien Georgee6c0dff2014-08-15 23:47:59 +0100158 MP_F_CONVERT_NATIVE_TO_OBJ,
Damien George4f3d9422018-09-13 22:03:48 +1000159 MP_F_NATIVE_SWAP_GLOBALS,
Damien Georged17926d2014-03-30 13:35:08 +0100160 MP_F_LOAD_NAME,
161 MP_F_LOAD_GLOBAL,
162 MP_F_LOAD_BUILD_CLASS,
163 MP_F_LOAD_ATTR,
164 MP_F_LOAD_METHOD,
Damien Georgedd11af22017-04-19 09:45:59 +1000165 MP_F_LOAD_SUPER_METHOD,
Damien Georged17926d2014-03-30 13:35:08 +0100166 MP_F_STORE_NAME,
Damien Georgee6c0dff2014-08-15 23:47:59 +0100167 MP_F_STORE_GLOBAL,
Damien Georged17926d2014-03-30 13:35:08 +0100168 MP_F_STORE_ATTR,
Damien George729f7b42014-04-17 22:10:53 +0100169 MP_F_OBJ_SUBSCR,
Damien Georged17926d2014-03-30 13:35:08 +0100170 MP_F_OBJ_IS_TRUE,
171 MP_F_UNARY_OP,
172 MP_F_BINARY_OP,
173 MP_F_BUILD_TUPLE,
174 MP_F_BUILD_LIST,
175 MP_F_LIST_APPEND,
176 MP_F_BUILD_MAP,
177 MP_F_STORE_MAP,
Damien George3ebd4d02014-06-01 13:46:47 +0100178#if MICROPY_PY_BUILTINS_SET
Damien Georged17926d2014-03-30 13:35:08 +0100179 MP_F_STORE_SET,
Damien George436e0d42018-05-22 22:18:42 +1000180 MP_F_BUILD_SET,
Damien George3ebd4d02014-06-01 13:46:47 +0100181#endif
Damien Georgedf8127a2014-04-13 11:04:33 +0100182 MP_F_MAKE_FUNCTION_FROM_RAW_CODE,
Damien George86de21b2014-08-16 22:06:11 +0100183 MP_F_NATIVE_CALL_FUNCTION_N_KW,
Damien Georged17926d2014-03-30 13:35:08 +0100184 MP_F_CALL_METHOD_N_KW,
Damien George78772ad2015-04-06 22:48:21 +0100185 MP_F_CALL_METHOD_N_KW_VAR,
Damien George088740e2017-01-17 15:27:37 +1100186 MP_F_NATIVE_GETITER,
187 MP_F_NATIVE_ITERNEXT,
Damien George7fe21912014-08-16 22:31:57 +0100188 MP_F_NLR_PUSH,
189 MP_F_NLR_POP,
Damien George86de21b2014-08-16 22:06:11 +0100190 MP_F_NATIVE_RAISE,
Damien Georgecdd96df2014-04-06 12:58:40 +0100191 MP_F_IMPORT_NAME,
Damien George7fe21912014-08-16 22:31:57 +0100192 MP_F_IMPORT_FROM,
Damien Georgecdd96df2014-04-06 12:58:40 +0100193 MP_F_IMPORT_ALL,
Damien Georgec49ddb92014-06-01 13:49:35 +0100194#if MICROPY_PY_BUILTINS_SLICE
Damien Georgecdd96df2014-04-06 12:58:40 +0100195 MP_F_NEW_SLICE,
Damien Georgec49ddb92014-06-01 13:49:35 +0100196#endif
Damien Georgecdd96df2014-04-06 12:58:40 +0100197 MP_F_UNPACK_SEQUENCE,
Damien Georgea32c1e42014-05-07 18:30:52 +0100198 MP_F_UNPACK_EX,
Damien Georgee6ce10a2014-09-06 18:38:20 +0100199 MP_F_DELETE_NAME,
200 MP_F_DELETE_GLOBAL,
Damien George4cd9ced2015-01-15 14:41:41 +0000201 MP_F_NEW_CELL,
202 MP_F_MAKE_CLOSURE_FROM_RAW_CODE,
Damien George43f18482018-09-14 17:40:59 +1000203 MP_F_ARG_CHECK_NUM_SIG,
Damien George99886182015-04-06 22:38:53 +0100204 MP_F_SETUP_CODE_STATE,
Damien Georgea3afa8c2017-10-11 18:54:34 +1100205 MP_F_SMALL_INT_FLOOR_DIVIDE,
206 MP_F_SMALL_INT_MODULO,
Damien Georgecc2bd632018-10-01 13:07:04 +1000207 MP_F_NATIVE_YIELD_FROM,
Damien Georged17926d2014-03-30 13:35:08 +0100208 MP_F_NUMBER_OF,
209} mp_fun_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000210
Damien Georgede710352018-10-14 23:56:46 +1100211extern const void *const mp_fun_table[MP_F_NUMBER_OF];
Paul Sokolovsky8ab6f902014-12-25 23:29:19 +0200212
Alexander Steffen299bc622017-06-29 23:14:58 +0200213#endif // MICROPY_INCLUDED_PY_RUNTIME0_H