runtime0.h 6.2 KB

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