runtime0.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. // These must fit in 8 bits; see scope.h
  29. #define MP_SCOPE_FLAG_VARARGS (0x01)
  30. #define MP_SCOPE_FLAG_VARKEYWORDS (0x02)
  31. #define MP_SCOPE_FLAG_GENERATOR (0x04)
  32. #define MP_SCOPE_FLAG_DEFKWARGS (0x08)
  33. // types for native (viper) function signature
  34. #define MP_NATIVE_TYPE_OBJ (0x00)
  35. #define MP_NATIVE_TYPE_BOOL (0x01)
  36. #define MP_NATIVE_TYPE_INT (0x02)
  37. #define MP_NATIVE_TYPE_UINT (0x03)
  38. #define MP_NATIVE_TYPE_PTR (0x04)
  39. #define MP_NATIVE_TYPE_PTR8 (0x05)
  40. #define MP_NATIVE_TYPE_PTR16 (0x06)
  41. #define MP_NATIVE_TYPE_PTR32 (0x07)
  42. typedef enum {
  43. // These ops may appear in the bytecode. Changing this group
  44. // in any way requires changing the bytecode version.
  45. MP_UNARY_OP_POSITIVE,
  46. MP_UNARY_OP_NEGATIVE,
  47. MP_UNARY_OP_INVERT,
  48. MP_UNARY_OP_NOT,
  49. // Following ops cannot appear in the bytecode
  50. MP_UNARY_OP_NUM_BYTECODE,
  51. MP_UNARY_OP_BOOL = MP_UNARY_OP_NUM_BYTECODE, // __bool__
  52. MP_UNARY_OP_LEN, // __len__
  53. MP_UNARY_OP_HASH, // __hash__; must return a small int
  54. MP_UNARY_OP_ABS, // __abs__
  55. MP_UNARY_OP_SIZEOF, // for sys.getsizeof()
  56. MP_UNARY_OP_NUM_RUNTIME,
  57. } mp_unary_op_t;
  58. // Note: the first 9+12+12 of these are used in bytecode and changing
  59. // them requires changing the bytecode version.
  60. typedef enum {
  61. // 9 relational operations, should return a bool
  62. MP_BINARY_OP_LESS,
  63. MP_BINARY_OP_MORE,
  64. MP_BINARY_OP_EQUAL,
  65. MP_BINARY_OP_LESS_EQUAL,
  66. MP_BINARY_OP_MORE_EQUAL,
  67. MP_BINARY_OP_NOT_EQUAL,
  68. MP_BINARY_OP_IN,
  69. MP_BINARY_OP_IS,
  70. MP_BINARY_OP_EXCEPTION_MATCH,
  71. // 12 inplace arithmetic operations
  72. MP_BINARY_OP_INPLACE_OR,
  73. MP_BINARY_OP_INPLACE_XOR,
  74. MP_BINARY_OP_INPLACE_AND,
  75. MP_BINARY_OP_INPLACE_LSHIFT,
  76. MP_BINARY_OP_INPLACE_RSHIFT,
  77. MP_BINARY_OP_INPLACE_ADD,
  78. MP_BINARY_OP_INPLACE_SUBTRACT,
  79. MP_BINARY_OP_INPLACE_MULTIPLY,
  80. MP_BINARY_OP_INPLACE_FLOOR_DIVIDE,
  81. MP_BINARY_OP_INPLACE_TRUE_DIVIDE,
  82. MP_BINARY_OP_INPLACE_MODULO,
  83. MP_BINARY_OP_INPLACE_POWER,
  84. // 12 normal arithmetic operations
  85. MP_BINARY_OP_OR,
  86. MP_BINARY_OP_XOR,
  87. MP_BINARY_OP_AND,
  88. MP_BINARY_OP_LSHIFT,
  89. MP_BINARY_OP_RSHIFT,
  90. MP_BINARY_OP_ADD,
  91. MP_BINARY_OP_SUBTRACT,
  92. MP_BINARY_OP_MULTIPLY,
  93. MP_BINARY_OP_FLOOR_DIVIDE,
  94. MP_BINARY_OP_TRUE_DIVIDE,
  95. MP_BINARY_OP_MODULO,
  96. MP_BINARY_OP_POWER,
  97. // Operations below this line don't appear in bytecode, they
  98. // just identify special methods.
  99. MP_BINARY_OP_NUM_BYTECODE,
  100. // MP_BINARY_OP_REVERSE_* must follow immediately after MP_BINARY_OP_*
  101. #if MICROPY_PY_REVERSE_SPECIAL_METHODS
  102. MP_BINARY_OP_REVERSE_OR = MP_BINARY_OP_NUM_BYTECODE,
  103. MP_BINARY_OP_REVERSE_XOR,
  104. MP_BINARY_OP_REVERSE_AND,
  105. MP_BINARY_OP_REVERSE_LSHIFT,
  106. MP_BINARY_OP_REVERSE_RSHIFT,
  107. MP_BINARY_OP_REVERSE_ADD,
  108. MP_BINARY_OP_REVERSE_SUBTRACT,
  109. MP_BINARY_OP_REVERSE_MULTIPLY,
  110. MP_BINARY_OP_REVERSE_FLOOR_DIVIDE,
  111. MP_BINARY_OP_REVERSE_TRUE_DIVIDE,
  112. MP_BINARY_OP_REVERSE_MODULO,
  113. MP_BINARY_OP_REVERSE_POWER,
  114. #endif
  115. // This is not emitted by the compiler but is supported by the runtime
  116. MP_BINARY_OP_DIVMOD
  117. #if !MICROPY_PY_REVERSE_SPECIAL_METHODS
  118. = MP_BINARY_OP_NUM_BYTECODE
  119. #endif
  120. ,
  121. // The runtime will convert MP_BINARY_OP_IN to this operator with swapped args.
  122. // A type should implement this containment operator instead of MP_BINARY_OP_IN.
  123. MP_BINARY_OP_CONTAINS,
  124. MP_BINARY_OP_NUM_RUNTIME,
  125. // These 2 are not supported by the runtime and must be synthesised by the emitter
  126. MP_BINARY_OP_NOT_IN,
  127. MP_BINARY_OP_IS_NOT,
  128. } mp_binary_op_t;
  129. typedef enum {
  130. MP_F_CONVERT_OBJ_TO_NATIVE = 0,
  131. MP_F_CONVERT_NATIVE_TO_OBJ,
  132. MP_F_LOAD_NAME,
  133. MP_F_LOAD_GLOBAL,
  134. MP_F_LOAD_BUILD_CLASS,
  135. MP_F_LOAD_ATTR,
  136. MP_F_LOAD_METHOD,
  137. MP_F_LOAD_SUPER_METHOD,
  138. MP_F_STORE_NAME,
  139. MP_F_STORE_GLOBAL,
  140. MP_F_STORE_ATTR,
  141. MP_F_OBJ_SUBSCR,
  142. MP_F_OBJ_IS_TRUE,
  143. MP_F_UNARY_OP,
  144. MP_F_BINARY_OP,
  145. MP_F_BUILD_TUPLE,
  146. MP_F_BUILD_LIST,
  147. MP_F_LIST_APPEND,
  148. MP_F_BUILD_MAP,
  149. MP_F_STORE_MAP,
  150. #if MICROPY_PY_BUILTINS_SET
  151. MP_F_STORE_SET,
  152. MP_F_BUILD_SET,
  153. #endif
  154. MP_F_MAKE_FUNCTION_FROM_RAW_CODE,
  155. MP_F_NATIVE_CALL_FUNCTION_N_KW,
  156. MP_F_CALL_METHOD_N_KW,
  157. MP_F_CALL_METHOD_N_KW_VAR,
  158. MP_F_NATIVE_GETITER,
  159. MP_F_NATIVE_ITERNEXT,
  160. MP_F_NLR_PUSH,
  161. MP_F_NLR_POP,
  162. MP_F_NATIVE_RAISE,
  163. MP_F_IMPORT_NAME,
  164. MP_F_IMPORT_FROM,
  165. MP_F_IMPORT_ALL,
  166. #if MICROPY_PY_BUILTINS_SLICE
  167. MP_F_NEW_SLICE,
  168. #endif
  169. MP_F_UNPACK_SEQUENCE,
  170. MP_F_UNPACK_EX,
  171. MP_F_DELETE_NAME,
  172. MP_F_DELETE_GLOBAL,
  173. MP_F_NEW_CELL,
  174. MP_F_MAKE_CLOSURE_FROM_RAW_CODE,
  175. MP_F_SETUP_CODE_STATE,
  176. MP_F_SMALL_INT_FLOOR_DIVIDE,
  177. MP_F_SMALL_INT_MODULO,
  178. MP_F_NUMBER_OF,
  179. } mp_fun_kind_t;
  180. extern void *const mp_fun_table[MP_F_NUMBER_OF];
  181. #endif // MICROPY_INCLUDED_PY_RUNTIME0_H