runtime0.h 6.4 KB

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