1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
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  */
26 #ifndef MICROPY_INCLUDED_PY_RUNTIME0_H
27 #define MICROPY_INCLUDED_PY_RUNTIME0_H
28 
29 // The first four must fit in 8 bits, see emitbc.c
30 // The remaining must fit in 16 bits, see scope.h
31 #define MP_SCOPE_FLAG_ALL_SIG      (0x0f)
32 #define MP_SCOPE_FLAG_GENERATOR    (0x01)
33 #define MP_SCOPE_FLAG_VARKEYWORDS  (0x02)
34 #define MP_SCOPE_FLAG_VARARGS      (0x04)
35 #define MP_SCOPE_FLAG_DEFKWARGS    (0x08)
36 #define MP_SCOPE_FLAG_REFGLOBALS   (0x10) // used only if native emitter enabled
37 #define MP_SCOPE_FLAG_HASCONSTS    (0x20) // used only if native emitter enabled
38 #define MP_SCOPE_FLAG_VIPERRET_POS    (6) // 3 bits used for viper return type, to pass from compiler to native emitter
39 #define MP_SCOPE_FLAG_VIPERRELOC   (0x10) // used only when loading viper from .mpy
40 #define MP_SCOPE_FLAG_VIPERRODATA  (0x20) // used only when loading viper from .mpy
41 #define MP_SCOPE_FLAG_VIPERBSS     (0x40) // used only when loading viper from .mpy
42 
43 // types for native (viper) function signature
44 #define MP_NATIVE_TYPE_OBJ  (0x00)
45 #define MP_NATIVE_TYPE_BOOL (0x01)
46 #define MP_NATIVE_TYPE_INT  (0x02)
47 #define MP_NATIVE_TYPE_UINT (0x03)
48 #define MP_NATIVE_TYPE_PTR  (0x04)
49 #define MP_NATIVE_TYPE_PTR8 (0x05)
50 #define MP_NATIVE_TYPE_PTR16 (0x06)
51 #define MP_NATIVE_TYPE_PTR32 (0x07)
52 
53 // Bytecode and runtime boundaries for unary ops
54 #define MP_UNARY_OP_NUM_BYTECODE    (MP_UNARY_OP_NOT + 1)
55 #define MP_UNARY_OP_NUM_RUNTIME     (MP_UNARY_OP_SIZEOF + 1)
56 
57 // Bytecode and runtime boundaries for binary ops
58 #define MP_BINARY_OP_NUM_BYTECODE   (MP_BINARY_OP_POWER + 1)
59 #if MICROPY_PY_REVERSE_SPECIAL_METHODS
60 #define MP_BINARY_OP_NUM_RUNTIME    (MP_BINARY_OP_REVERSE_POWER + 1)
61 #else
62 #define MP_BINARY_OP_NUM_RUNTIME    (MP_BINARY_OP_CONTAINS + 1)
63 #endif
64 
65 typedef enum {
66     // These ops may appear in the bytecode. Changing this group
67     // in any way requires changing the bytecode version.
68     MP_UNARY_OP_POSITIVE,
69     MP_UNARY_OP_NEGATIVE,
70     MP_UNARY_OP_INVERT,
71     MP_UNARY_OP_NOT,
72 
73     // Following ops cannot appear in the bytecode
74     MP_UNARY_OP_BOOL, // __bool__
75     MP_UNARY_OP_LEN, // __len__
76     MP_UNARY_OP_HASH, // __hash__; must return a small int
77     MP_UNARY_OP_ABS, // __abs__
78     MP_UNARY_OP_INT, // __int__
79     MP_UNARY_OP_SIZEOF, // for sys.getsizeof()
80 } mp_unary_op_t;
81 
82 typedef enum {
83     // The following 9+13+13 ops are used in bytecode and changing
84     // them requires changing the bytecode version.
85 
86     // 9 relational operations, should return a bool; order of first 6 matches corresponding mp_token_kind_t
87     MP_BINARY_OP_LESS,
88     MP_BINARY_OP_MORE,
89     MP_BINARY_OP_EQUAL,
90     MP_BINARY_OP_LESS_EQUAL,
91     MP_BINARY_OP_MORE_EQUAL,
92     MP_BINARY_OP_NOT_EQUAL,
93     MP_BINARY_OP_IN,
94     MP_BINARY_OP_IS,
95     MP_BINARY_OP_EXCEPTION_MATCH,
96 
97     // 13 inplace arithmetic operations; order matches corresponding mp_token_kind_t
98     MP_BINARY_OP_INPLACE_OR,
99     MP_BINARY_OP_INPLACE_XOR,
100     MP_BINARY_OP_INPLACE_AND,
101     MP_BINARY_OP_INPLACE_LSHIFT,
102     MP_BINARY_OP_INPLACE_RSHIFT,
103     MP_BINARY_OP_INPLACE_ADD,
104     MP_BINARY_OP_INPLACE_SUBTRACT,
105     MP_BINARY_OP_INPLACE_MULTIPLY,
106     MP_BINARY_OP_INPLACE_MAT_MULTIPLY,
107     MP_BINARY_OP_INPLACE_FLOOR_DIVIDE,
108     MP_BINARY_OP_INPLACE_TRUE_DIVIDE,
109     MP_BINARY_OP_INPLACE_MODULO,
110     MP_BINARY_OP_INPLACE_POWER,
111 
112     // 13 normal arithmetic operations; order matches corresponding mp_token_kind_t
113     MP_BINARY_OP_OR,
114     MP_BINARY_OP_XOR,
115     MP_BINARY_OP_AND,
116     MP_BINARY_OP_LSHIFT,
117     MP_BINARY_OP_RSHIFT,
118     MP_BINARY_OP_ADD,
119     MP_BINARY_OP_SUBTRACT,
120     MP_BINARY_OP_MULTIPLY,
121     MP_BINARY_OP_MAT_MULTIPLY,
122     MP_BINARY_OP_FLOOR_DIVIDE,
123     MP_BINARY_OP_TRUE_DIVIDE,
124     MP_BINARY_OP_MODULO,
125     MP_BINARY_OP_POWER,
126 
127     // Operations below this line don't appear in bytecode, they
128     // just identify special methods.
129 
130     // This is not emitted by the compiler but is supported by the runtime.
131     // It must follow immediately after MP_BINARY_OP_POWER.
132     MP_BINARY_OP_DIVMOD,
133 
134     // The runtime will convert MP_BINARY_OP_IN to this operator with swapped args.
135     // A type should implement this containment operator instead of MP_BINARY_OP_IN.
136     MP_BINARY_OP_CONTAINS,
137 
138     // 13 MP_BINARY_OP_REVERSE_* operations must be in the same order as MP_BINARY_OP_*,
139     // and be the last ones supported by the runtime.
140     MP_BINARY_OP_REVERSE_OR,
141     MP_BINARY_OP_REVERSE_XOR,
142     MP_BINARY_OP_REVERSE_AND,
143     MP_BINARY_OP_REVERSE_LSHIFT,
144     MP_BINARY_OP_REVERSE_RSHIFT,
145     MP_BINARY_OP_REVERSE_ADD,
146     MP_BINARY_OP_REVERSE_SUBTRACT,
147     MP_BINARY_OP_REVERSE_MULTIPLY,
148     MP_BINARY_OP_REVERSE_MAT_MULTIPLY,
149     MP_BINARY_OP_REVERSE_FLOOR_DIVIDE,
150     MP_BINARY_OP_REVERSE_TRUE_DIVIDE,
151     MP_BINARY_OP_REVERSE_MODULO,
152     MP_BINARY_OP_REVERSE_POWER,
153 
154     // These 2 are not supported by the runtime and must be synthesised by the emitter
155     MP_BINARY_OP_NOT_IN,
156     MP_BINARY_OP_IS_NOT,
157 } mp_binary_op_t;
158 
159 #endif // MICROPY_INCLUDED_PY_RUNTIME0_H
160