bc0.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_BC0_H
  27. #define MICROPY_INCLUDED_PY_BC0_H
  28. // MicroPython bytecode opcodes, grouped based on the format of the opcode
  29. // All opcodes are encoded as a byte with an optional argument. Arguments are
  30. // variable-length encoded so they can be as small as possible. The possible
  31. // encodings for arguments are (ip[0] is the opcode):
  32. //
  33. // - unsigned relative bytecode offset:
  34. // - if ip[1] high bit is clear then: arg = ip[1]
  35. // - if ip[1] high bit is set then: arg = ip[1] & 0x7f | ip[2] << 7
  36. //
  37. // - signed relative bytecode offset:
  38. // - if ip[1] high bit is clear then: arg = ip[1] - 0x40
  39. // - if ip[1] high bit is set then: arg = (ip[1] & 0x7f | ip[2] << 7) - 0x4000
  40. #define MP_BC_MASK_FORMAT (0xf0)
  41. #define MP_BC_MASK_EXTRA_BYTE (0x9e)
  42. #define MP_BC_FORMAT_BYTE (0)
  43. #define MP_BC_FORMAT_QSTR (1)
  44. #define MP_BC_FORMAT_VAR_UINT (2)
  45. #define MP_BC_FORMAT_OFFSET (3)
  46. // Nibbles in magic number are: BB BB BB BB BB BO VV QU
  47. #define MP_BC_FORMAT(op) ((0x000003a4 >> (2 * ((op) >> 4))) & 3)
  48. // Load, Store, Delete, Import, Make, Build, Unpack, Call, Jump, Exception, For, sTack, Return, Yield, Op
  49. #define MP_BC_BASE_RESERVED (0x00) // ----------------
  50. #define MP_BC_BASE_QSTR_O (0x10) // LLLLLLSSSDDII---
  51. #define MP_BC_BASE_VINT_E (0x20) // MMLLLLSSDDBBBBBB
  52. #define MP_BC_BASE_VINT_O (0x30) // UUMMCCCC--------
  53. #define MP_BC_BASE_JUMP_E (0x40) // J-JJJJJEEEEF----
  54. #define MP_BC_BASE_BYTE_O (0x50) // LLLLSSDTTTTTEEFF
  55. #define MP_BC_BASE_BYTE_E (0x60) // --BREEEYYI------
  56. #define MP_BC_LOAD_CONST_SMALL_INT_MULTI (0x70) // LLLLLLLLLLLLLLLL
  57. // (0x80) // LLLLLLLLLLLLLLLL
  58. // (0x90) // LLLLLLLLLLLLLLLL
  59. // (0xa0) // LLLLLLLLLLLLLLLL
  60. #define MP_BC_LOAD_FAST_MULTI (0xb0) // LLLLLLLLLLLLLLLL
  61. #define MP_BC_STORE_FAST_MULTI (0xc0) // SSSSSSSSSSSSSSSS
  62. #define MP_BC_UNARY_OP_MULTI (0xd0) // OOOOOOO
  63. #define MP_BC_BINARY_OP_MULTI (0xd7) // OOOOOOOOO
  64. // (0xe0) // OOOOOOOOOOOOOOOO
  65. // (0xf0) // OOOOOOOOOO------
  66. #define MP_BC_LOAD_CONST_SMALL_INT_MULTI_NUM (64)
  67. #define MP_BC_LOAD_CONST_SMALL_INT_MULTI_EXCESS (16)
  68. #define MP_BC_LOAD_FAST_MULTI_NUM (16)
  69. #define MP_BC_STORE_FAST_MULTI_NUM (16)
  70. #define MP_BC_UNARY_OP_MULTI_NUM (MP_UNARY_OP_NUM_BYTECODE)
  71. #define MP_BC_BINARY_OP_MULTI_NUM (MP_BINARY_OP_NUM_BYTECODE)
  72. #define MP_BC_LOAD_CONST_FALSE (MP_BC_BASE_BYTE_O + 0x00)
  73. #define MP_BC_LOAD_CONST_NONE (MP_BC_BASE_BYTE_O + 0x01)
  74. #define MP_BC_LOAD_CONST_TRUE (MP_BC_BASE_BYTE_O + 0x02)
  75. #define MP_BC_LOAD_CONST_SMALL_INT (MP_BC_BASE_VINT_E + 0x02) // signed var-int
  76. #define MP_BC_LOAD_CONST_STRING (MP_BC_BASE_QSTR_O + 0x00) // qstr
  77. #define MP_BC_LOAD_CONST_OBJ (MP_BC_BASE_VINT_E + 0x03) // ptr
  78. #define MP_BC_LOAD_NULL (MP_BC_BASE_BYTE_O + 0x03)
  79. #define MP_BC_LOAD_FAST_N (MP_BC_BASE_VINT_E + 0x04) // uint
  80. #define MP_BC_LOAD_DEREF (MP_BC_BASE_VINT_E + 0x05) // uint
  81. #define MP_BC_LOAD_NAME (MP_BC_BASE_QSTR_O + 0x01) // qstr
  82. #define MP_BC_LOAD_GLOBAL (MP_BC_BASE_QSTR_O + 0x02) // qstr
  83. #define MP_BC_LOAD_ATTR (MP_BC_BASE_QSTR_O + 0x03) // qstr
  84. #define MP_BC_LOAD_METHOD (MP_BC_BASE_QSTR_O + 0x04) // qstr
  85. #define MP_BC_LOAD_SUPER_METHOD (MP_BC_BASE_QSTR_O + 0x05) // qstr
  86. #define MP_BC_LOAD_BUILD_CLASS (MP_BC_BASE_BYTE_O + 0x04)
  87. #define MP_BC_LOAD_SUBSCR (MP_BC_BASE_BYTE_O + 0x05)
  88. #define MP_BC_STORE_FAST_N (MP_BC_BASE_VINT_E + 0x06) // uint
  89. #define MP_BC_STORE_DEREF (MP_BC_BASE_VINT_E + 0x07) // uint
  90. #define MP_BC_STORE_NAME (MP_BC_BASE_QSTR_O + 0x06) // qstr
  91. #define MP_BC_STORE_GLOBAL (MP_BC_BASE_QSTR_O + 0x07) // qstr
  92. #define MP_BC_STORE_ATTR (MP_BC_BASE_QSTR_O + 0x08) // qstr
  93. #define MP_BC_STORE_SUBSCR (MP_BC_BASE_BYTE_O + 0x06)
  94. #define MP_BC_DELETE_FAST (MP_BC_BASE_VINT_E + 0x08) // uint
  95. #define MP_BC_DELETE_DEREF (MP_BC_BASE_VINT_E + 0x09) // uint
  96. #define MP_BC_DELETE_NAME (MP_BC_BASE_QSTR_O + 0x09) // qstr
  97. #define MP_BC_DELETE_GLOBAL (MP_BC_BASE_QSTR_O + 0x0a) // qstr
  98. #define MP_BC_DUP_TOP (MP_BC_BASE_BYTE_O + 0x07)
  99. #define MP_BC_DUP_TOP_TWO (MP_BC_BASE_BYTE_O + 0x08)
  100. #define MP_BC_POP_TOP (MP_BC_BASE_BYTE_O + 0x09)
  101. #define MP_BC_ROT_TWO (MP_BC_BASE_BYTE_O + 0x0a)
  102. #define MP_BC_ROT_THREE (MP_BC_BASE_BYTE_O + 0x0b)
  103. #define MP_BC_UNWIND_JUMP (MP_BC_BASE_JUMP_E + 0x00) // signed relative bytecode offset; then a byte
  104. #define MP_BC_JUMP (MP_BC_BASE_JUMP_E + 0x02) // signed relative bytecode offset
  105. #define MP_BC_POP_JUMP_IF_TRUE (MP_BC_BASE_JUMP_E + 0x03) // signed relative bytecode offset
  106. #define MP_BC_POP_JUMP_IF_FALSE (MP_BC_BASE_JUMP_E + 0x04) // signed relative bytecode offset
  107. #define MP_BC_JUMP_IF_TRUE_OR_POP (MP_BC_BASE_JUMP_E + 0x05) // unsigned relative bytecode offset
  108. #define MP_BC_JUMP_IF_FALSE_OR_POP (MP_BC_BASE_JUMP_E + 0x06) // unsigned relative bytecode offset
  109. #define MP_BC_SETUP_WITH (MP_BC_BASE_JUMP_E + 0x07) // unsigned relative bytecode offset
  110. #define MP_BC_SETUP_EXCEPT (MP_BC_BASE_JUMP_E + 0x08) // unsigned relative bytecode offset
  111. #define MP_BC_SETUP_FINALLY (MP_BC_BASE_JUMP_E + 0x09) // unsigned relative bytecode offset
  112. #define MP_BC_POP_EXCEPT_JUMP (MP_BC_BASE_JUMP_E + 0x0a) // unsigned relative bytecode offset
  113. #define MP_BC_FOR_ITER (MP_BC_BASE_JUMP_E + 0x0b) // unsigned relative bytecode offset
  114. #define MP_BC_WITH_CLEANUP (MP_BC_BASE_BYTE_O + 0x0c)
  115. #define MP_BC_END_FINALLY (MP_BC_BASE_BYTE_O + 0x0d)
  116. #define MP_BC_GET_ITER (MP_BC_BASE_BYTE_O + 0x0e)
  117. #define MP_BC_GET_ITER_STACK (MP_BC_BASE_BYTE_O + 0x0f)
  118. #define MP_BC_BUILD_TUPLE (MP_BC_BASE_VINT_E + 0x0a) // uint
  119. #define MP_BC_BUILD_LIST (MP_BC_BASE_VINT_E + 0x0b) // uint
  120. #define MP_BC_BUILD_MAP (MP_BC_BASE_VINT_E + 0x0c) // uint
  121. #define MP_BC_STORE_MAP (MP_BC_BASE_BYTE_E + 0x02)
  122. #define MP_BC_BUILD_SET (MP_BC_BASE_VINT_E + 0x0d) // uint
  123. #define MP_BC_BUILD_SLICE (MP_BC_BASE_VINT_E + 0x0e) // uint
  124. #define MP_BC_STORE_COMP (MP_BC_BASE_VINT_E + 0x0f) // uint
  125. #define MP_BC_UNPACK_SEQUENCE (MP_BC_BASE_VINT_O + 0x00) // uint
  126. #define MP_BC_UNPACK_EX (MP_BC_BASE_VINT_O + 0x01) // uint
  127. #define MP_BC_RETURN_VALUE (MP_BC_BASE_BYTE_E + 0x03)
  128. #define MP_BC_RAISE_LAST (MP_BC_BASE_BYTE_E + 0x04)
  129. #define MP_BC_RAISE_OBJ (MP_BC_BASE_BYTE_E + 0x05)
  130. #define MP_BC_RAISE_FROM (MP_BC_BASE_BYTE_E + 0x06)
  131. #define MP_BC_YIELD_VALUE (MP_BC_BASE_BYTE_E + 0x07)
  132. #define MP_BC_YIELD_FROM (MP_BC_BASE_BYTE_E + 0x08)
  133. #define MP_BC_MAKE_FUNCTION (MP_BC_BASE_VINT_O + 0x02) // uint
  134. #define MP_BC_MAKE_FUNCTION_DEFARGS (MP_BC_BASE_VINT_O + 0x03) // uint
  135. #define MP_BC_MAKE_CLOSURE (MP_BC_BASE_VINT_E + 0x00) // uint; extra byte
  136. #define MP_BC_MAKE_CLOSURE_DEFARGS (MP_BC_BASE_VINT_E + 0x01) // uint; extra byte
  137. #define MP_BC_CALL_FUNCTION (MP_BC_BASE_VINT_O + 0x04) // uint
  138. #define MP_BC_CALL_FUNCTION_VAR_KW (MP_BC_BASE_VINT_O + 0x05) // uint
  139. #define MP_BC_CALL_METHOD (MP_BC_BASE_VINT_O + 0x06) // uint
  140. #define MP_BC_CALL_METHOD_VAR_KW (MP_BC_BASE_VINT_O + 0x07) // uint
  141. #define MP_BC_IMPORT_NAME (MP_BC_BASE_QSTR_O + 0x0b) // qstr
  142. #define MP_BC_IMPORT_FROM (MP_BC_BASE_QSTR_O + 0x0c) // qstr
  143. #define MP_BC_IMPORT_STAR (MP_BC_BASE_BYTE_E + 0x09)
  144. #endif // MICROPY_INCLUDED_PY_BC0_H