summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--py/bc0.h169
-rw-r--r--py/emit.h10
-rw-r--r--py/nativeglue.c6
-rw-r--r--py/runtime0.h6
-rwxr-xr-xtools/mpy-tool.py16
5 files changed, 111 insertions, 96 deletions
diff --git a/py/bc0.h b/py/bc0.h
index fcb6eead5..857b75db9 100644
--- a/py/bc0.h
+++ b/py/bc0.h
@@ -26,93 +26,108 @@
#ifndef MICROPY_INCLUDED_PY_BC0_H
#define MICROPY_INCLUDED_PY_BC0_H
-// MicroPython byte-codes.
-// The comment at the end of the line (if it exists) tells the arguments to the byte-code.
+// MicroPython bytecode opcodes, grouped based on the format of the opcode
-#define MP_BC_LOAD_CONST_FALSE (0x10)
-#define MP_BC_LOAD_CONST_NONE (0x11)
-#define MP_BC_LOAD_CONST_TRUE (0x12)
-#define MP_BC_LOAD_CONST_SMALL_INT (0x14) // signed var-int
-#define MP_BC_LOAD_CONST_STRING (0x16) // qstr
-#define MP_BC_LOAD_CONST_OBJ (0x17) // ptr
-#define MP_BC_LOAD_NULL (0x18)
+#define MP_BC_MASK_FORMAT (0xf0)
+#define MP_BC_MASK_EXTRA_BYTE (0x9e)
-#define MP_BC_LOAD_FAST_N (0x19) // uint
-#define MP_BC_LOAD_DEREF (0x1a) // uint
-#define MP_BC_LOAD_NAME (0x1b) // qstr
-#define MP_BC_LOAD_GLOBAL (0x1c) // qstr
-#define MP_BC_LOAD_ATTR (0x1d) // qstr
-#define MP_BC_LOAD_METHOD (0x1e) // qstr
-#define MP_BC_LOAD_SUPER_METHOD (0x1f) // qstr
-#define MP_BC_LOAD_BUILD_CLASS (0x20)
-#define MP_BC_LOAD_SUBSCR (0x21)
+// Load, Store, Delete, Import, Make, Build, Unpack, Call, Jump, Exception, For, sTack, Return, Yield, Op
+#define MP_BC_BASE_RESERVED (0x00) // ----------------
+#define MP_BC_BASE_QSTR_O (0x10) // LLLLLLSSSDDII---
+#define MP_BC_BASE_VINT_E (0x20) // MMLLLLSSDDBBBBBB
+#define MP_BC_BASE_VINT_O (0x30) // UUMMCCCC--------
+#define MP_BC_BASE_JUMP_E (0x40) // J-JJJJJEEEEF----
+#define MP_BC_BASE_BYTE_O (0x50) // LLLLSSDTTTTTEEFF
+#define MP_BC_BASE_BYTE_E (0x60) // E-BRYYI---------
+#define MP_BC_LOAD_CONST_SMALL_INT_MULTI (0x70) // LLLLLLLLLLLLLLLL
+ // (0x80) // LLLLLLLLLLLLLLLL
+ // (0x90) // LLLLLLLLLLLLLLLL
+ // (0xa0) // LLLLLLLLLLLLLLLL
+#define MP_BC_LOAD_FAST_MULTI (0xb0) // LLLLLLLLLLLLLLLL
+#define MP_BC_STORE_FAST_MULTI (0xc0) // SSSSSSSSSSSSSSSS
+#define MP_BC_UNARY_OP_MULTI (0xd0) // OOOOOOO
+#define MP_BC_BINARY_OP_MULTI (0xd7) // OOOOOOOOO
+ // (0xe0) // OOOOOOOOOOOOOOOO
+ // (0xf0) // OOOOOOOOOO------
-#define MP_BC_STORE_FAST_N (0x22) // uint
-#define MP_BC_STORE_DEREF (0x23) // uint
-#define MP_BC_STORE_NAME (0x24) // qstr
-#define MP_BC_STORE_GLOBAL (0x25) // qstr
-#define MP_BC_STORE_ATTR (0x26) // qstr
-#define MP_BC_STORE_SUBSCR (0x27)
+#define MP_BC_LOAD_CONST_FALSE (MP_BC_BASE_BYTE_O + 0x00)
+#define MP_BC_LOAD_CONST_NONE (MP_BC_BASE_BYTE_O + 0x01)
+#define MP_BC_LOAD_CONST_TRUE (MP_BC_BASE_BYTE_O + 0x02)
+#define MP_BC_LOAD_CONST_SMALL_INT (MP_BC_BASE_VINT_E + 0x02) // signed var-int
+#define MP_BC_LOAD_CONST_STRING (MP_BC_BASE_QSTR_O + 0x00) // qstr
+#define MP_BC_LOAD_CONST_OBJ (MP_BC_BASE_VINT_E + 0x03) // ptr
+#define MP_BC_LOAD_NULL (MP_BC_BASE_BYTE_O + 0x03)
-#define MP_BC_DELETE_FAST (0x28) // uint
-#define MP_BC_DELETE_DEREF (0x29) // uint
-#define MP_BC_DELETE_NAME (0x2a) // qstr
-#define MP_BC_DELETE_GLOBAL (0x2b) // qstr
+#define MP_BC_LOAD_FAST_N (MP_BC_BASE_VINT_E + 0x04) // uint
+#define MP_BC_LOAD_DEREF (MP_BC_BASE_VINT_E + 0x05) // uint
+#define MP_BC_LOAD_NAME (MP_BC_BASE_QSTR_O + 0x01) // qstr
+#define MP_BC_LOAD_GLOBAL (MP_BC_BASE_QSTR_O + 0x02) // qstr
+#define MP_BC_LOAD_ATTR (MP_BC_BASE_QSTR_O + 0x03) // qstr
+#define MP_BC_LOAD_METHOD (MP_BC_BASE_QSTR_O + 0x04) // qstr
+#define MP_BC_LOAD_SUPER_METHOD (MP_BC_BASE_QSTR_O + 0x05) // qstr
+#define MP_BC_LOAD_BUILD_CLASS (MP_BC_BASE_BYTE_O + 0x04)
+#define MP_BC_LOAD_SUBSCR (MP_BC_BASE_BYTE_O + 0x05)
-#define MP_BC_DUP_TOP (0x30)
-#define MP_BC_DUP_TOP_TWO (0x31)
-#define MP_BC_POP_TOP (0x32)
-#define MP_BC_ROT_TWO (0x33)
-#define MP_BC_ROT_THREE (0x34)
+#define MP_BC_STORE_FAST_N (MP_BC_BASE_VINT_E + 0x06) // uint
+#define MP_BC_STORE_DEREF (MP_BC_BASE_VINT_E + 0x07) // uint
+#define MP_BC_STORE_NAME (MP_BC_BASE_QSTR_O + 0x06) // qstr
+#define MP_BC_STORE_GLOBAL (MP_BC_BASE_QSTR_O + 0x07) // qstr
+#define MP_BC_STORE_ATTR (MP_BC_BASE_QSTR_O + 0x08) // qstr
+#define MP_BC_STORE_SUBSCR (MP_BC_BASE_BYTE_O + 0x06)
-#define MP_BC_JUMP (0x35) // rel byte code offset, 16-bit signed, in excess
-#define MP_BC_POP_JUMP_IF_TRUE (0x36) // rel byte code offset, 16-bit signed, in excess
-#define MP_BC_POP_JUMP_IF_FALSE (0x37) // rel byte code offset, 16-bit signed, in excess
-#define MP_BC_JUMP_IF_TRUE_OR_POP (0x38) // rel byte code offset, 16-bit signed, in excess
-#define MP_BC_JUMP_IF_FALSE_OR_POP (0x39) // rel byte code offset, 16-bit signed, in excess
-#define MP_BC_SETUP_WITH (0x3d) // rel byte code offset, 16-bit unsigned
-#define MP_BC_WITH_CLEANUP (0x3e)
-#define MP_BC_SETUP_EXCEPT (0x3f) // rel byte code offset, 16-bit unsigned
-#define MP_BC_SETUP_FINALLY (0x40) // rel byte code offset, 16-bit unsigned
-#define MP_BC_END_FINALLY (0x41)
-#define MP_BC_GET_ITER (0x42)
-#define MP_BC_FOR_ITER (0x43) // rel byte code offset, 16-bit unsigned
-#define MP_BC_POP_EXCEPT_JUMP (0x44) // rel byte code offset, 16-bit unsigned
-#define MP_BC_UNWIND_JUMP (0x46) // rel byte code offset, 16-bit signed, in excess; then a byte
-#define MP_BC_GET_ITER_STACK (0x47)
+#define MP_BC_DELETE_FAST (MP_BC_BASE_VINT_E + 0x08) // uint
+#define MP_BC_DELETE_DEREF (MP_BC_BASE_VINT_E + 0x09) // uint
+#define MP_BC_DELETE_NAME (MP_BC_BASE_QSTR_O + 0x09) // qstr
+#define MP_BC_DELETE_GLOBAL (MP_BC_BASE_QSTR_O + 0x0a) // qstr
-#define MP_BC_BUILD_TUPLE (0x50) // uint
-#define MP_BC_BUILD_LIST (0x51) // uint
-#define MP_BC_BUILD_MAP (0x53) // uint
-#define MP_BC_STORE_MAP (0x54)
-#define MP_BC_BUILD_SET (0x56) // uint
-#define MP_BC_BUILD_SLICE (0x58) // uint
-#define MP_BC_STORE_COMP (0x57) // uint
-#define MP_BC_UNPACK_SEQUENCE (0x59) // uint
-#define MP_BC_UNPACK_EX (0x5a) // uint
+#define MP_BC_DUP_TOP (MP_BC_BASE_BYTE_O + 0x07)
+#define MP_BC_DUP_TOP_TWO (MP_BC_BASE_BYTE_O + 0x08)
+#define MP_BC_POP_TOP (MP_BC_BASE_BYTE_O + 0x09)
+#define MP_BC_ROT_TWO (MP_BC_BASE_BYTE_O + 0x0a)
+#define MP_BC_ROT_THREE (MP_BC_BASE_BYTE_O + 0x0b)
-#define MP_BC_RETURN_VALUE (0x5b)
-#define MP_BC_RAISE_VARARGS (0x5c) // byte
-#define MP_BC_YIELD_VALUE (0x5d)
-#define MP_BC_YIELD_FROM (0x5e)
+#define MP_BC_JUMP (MP_BC_BASE_JUMP_E + 0x02) // rel byte code offset, 16-bit signed, in excess
+#define MP_BC_POP_JUMP_IF_TRUE (MP_BC_BASE_JUMP_E + 0x03) // rel byte code offset, 16-bit signed, in excess
+#define MP_BC_POP_JUMP_IF_FALSE (MP_BC_BASE_JUMP_E + 0x04) // rel byte code offset, 16-bit signed, in excess
+#define MP_BC_JUMP_IF_TRUE_OR_POP (MP_BC_BASE_JUMP_E + 0x05) // rel byte code offset, 16-bit signed, in excess
+#define MP_BC_JUMP_IF_FALSE_OR_POP (MP_BC_BASE_JUMP_E + 0x06) // rel byte code offset, 16-bit signed, in excess
+#define MP_BC_UNWIND_JUMP (MP_BC_BASE_JUMP_E + 0x00) // rel byte code offset, 16-bit signed, in excess; then a byte
+#define MP_BC_SETUP_WITH (MP_BC_BASE_JUMP_E + 0x07) // rel byte code offset, 16-bit unsigned
+#define MP_BC_SETUP_EXCEPT (MP_BC_BASE_JUMP_E + 0x08) // rel byte code offset, 16-bit unsigned
+#define MP_BC_SETUP_FINALLY (MP_BC_BASE_JUMP_E + 0x09) // rel byte code offset, 16-bit unsigned
+#define MP_BC_POP_EXCEPT_JUMP (MP_BC_BASE_JUMP_E + 0x0a) // rel byte code offset, 16-bit unsigned
+#define MP_BC_FOR_ITER (MP_BC_BASE_JUMP_E + 0x0b) // rel byte code offset, 16-bit unsigned
+#define MP_BC_WITH_CLEANUP (MP_BC_BASE_BYTE_O + 0x0c)
+#define MP_BC_END_FINALLY (MP_BC_BASE_BYTE_O + 0x0d)
+#define MP_BC_GET_ITER (MP_BC_BASE_BYTE_O + 0x0e)
+#define MP_BC_GET_ITER_STACK (MP_BC_BASE_BYTE_O + 0x0f)
-#define MP_BC_MAKE_FUNCTION (0x60) // uint
-#define MP_BC_MAKE_FUNCTION_DEFARGS (0x61) // uint
-#define MP_BC_MAKE_CLOSURE (0x62) // uint; byte
-#define MP_BC_MAKE_CLOSURE_DEFARGS (0x63) // uint; byte
-#define MP_BC_CALL_FUNCTION (0x64) // uint
-#define MP_BC_CALL_FUNCTION_VAR_KW (0x65) // uint
-#define MP_BC_CALL_METHOD (0x66) // uint
-#define MP_BC_CALL_METHOD_VAR_KW (0x67) // uint
+#define MP_BC_BUILD_TUPLE (MP_BC_BASE_VINT_E + 0x0a) // uint
+#define MP_BC_BUILD_LIST (MP_BC_BASE_VINT_E + 0x0b) // uint
+#define MP_BC_BUILD_MAP (MP_BC_BASE_VINT_E + 0x0c) // uint
+#define MP_BC_STORE_MAP (MP_BC_BASE_BYTE_E + 0x02)
+#define MP_BC_BUILD_SET (MP_BC_BASE_VINT_E + 0x0d) // uint
+#define MP_BC_BUILD_SLICE (MP_BC_BASE_VINT_E + 0x0e) // uint
+#define MP_BC_STORE_COMP (MP_BC_BASE_VINT_E + 0x0f) // uint
+#define MP_BC_UNPACK_SEQUENCE (MP_BC_BASE_VINT_O + 0x00) // uint
+#define MP_BC_UNPACK_EX (MP_BC_BASE_VINT_O + 0x01) // uint
-#define MP_BC_IMPORT_NAME (0x68) // qstr
-#define MP_BC_IMPORT_FROM (0x69) // qstr
-#define MP_BC_IMPORT_STAR (0x6a)
+#define MP_BC_RETURN_VALUE (MP_BC_BASE_BYTE_E + 0x03)
+#define MP_BC_RAISE_VARARGS (MP_BC_BASE_BYTE_E + 0x00) // extra byte
+#define MP_BC_YIELD_VALUE (MP_BC_BASE_BYTE_E + 0x04)
+#define MP_BC_YIELD_FROM (MP_BC_BASE_BYTE_E + 0x05)
-#define MP_BC_LOAD_CONST_SMALL_INT_MULTI (0x70) // + N(64)
-#define MP_BC_LOAD_FAST_MULTI (0xb0) // + N(16)
-#define MP_BC_STORE_FAST_MULTI (0xc0) // + N(16)
-#define MP_BC_UNARY_OP_MULTI (0xd0) // + op(<MP_UNARY_OP_NUM_BYTECODE)
-#define MP_BC_BINARY_OP_MULTI (0xd7) // + op(<MP_BINARY_OP_NUM_BYTECODE)
+#define MP_BC_MAKE_FUNCTION (MP_BC_BASE_VINT_O + 0x02) // uint
+#define MP_BC_MAKE_FUNCTION_DEFARGS (MP_BC_BASE_VINT_O + 0x03) // uint
+#define MP_BC_MAKE_CLOSURE (MP_BC_BASE_VINT_E + 0x00) // uint; extra byte
+#define MP_BC_MAKE_CLOSURE_DEFARGS (MP_BC_BASE_VINT_E + 0x01) // uint; extra byte
+#define MP_BC_CALL_FUNCTION (MP_BC_BASE_VINT_O + 0x04) // uint
+#define MP_BC_CALL_FUNCTION_VAR_KW (MP_BC_BASE_VINT_O + 0x05) // uint
+#define MP_BC_CALL_METHOD (MP_BC_BASE_VINT_O + 0x06) // uint
+#define MP_BC_CALL_METHOD_VAR_KW (MP_BC_BASE_VINT_O + 0x07) // uint
+
+#define MP_BC_IMPORT_NAME (MP_BC_BASE_QSTR_O + 0x0b) // qstr
+#define MP_BC_IMPORT_FROM (MP_BC_BASE_QSTR_O + 0x0c) // qstr
+#define MP_BC_IMPORT_STAR (MP_BC_BASE_BYTE_E + 0x06)
#endif // MICROPY_INCLUDED_PY_BC0_H
diff --git a/py/emit.h b/py/emit.h
index 70f7bf122..b3b6d755b 100644
--- a/py/emit.h
+++ b/py/emit.h
@@ -76,15 +76,15 @@ typedef enum {
// Kind for emit->setup_block()
#define MP_EMIT_SETUP_BLOCK_WITH (0)
-#define MP_EMIT_SETUP_BLOCK_EXCEPT (2)
-#define MP_EMIT_SETUP_BLOCK_FINALLY (3)
+#define MP_EMIT_SETUP_BLOCK_EXCEPT (1)
+#define MP_EMIT_SETUP_BLOCK_FINALLY (2)
// Kind for emit->build()
#define MP_EMIT_BUILD_TUPLE (0)
#define MP_EMIT_BUILD_LIST (1)
-#define MP_EMIT_BUILD_MAP (3)
-#define MP_EMIT_BUILD_SET (6)
-#define MP_EMIT_BUILD_SLICE (8)
+#define MP_EMIT_BUILD_MAP (2)
+#define MP_EMIT_BUILD_SET (3)
+#define MP_EMIT_BUILD_SLICE (4)
// Kind for emit->yield()
#define MP_EMIT_YIELD_VALUE (0)
diff --git a/py/nativeglue.c b/py/nativeglue.c
index eb02907bd..596793a4f 100644
--- a/py/nativeglue.c
+++ b/py/nativeglue.c
@@ -210,13 +210,13 @@ const void *const mp_fun_table[MP_F_NUMBER_OF] = {
mp_binary_op,
mp_obj_new_tuple,
mp_obj_new_list,
- mp_obj_list_append,
mp_obj_new_dict,
- mp_obj_dict_store,
#if MICROPY_PY_BUILTINS_SET
- mp_obj_set_store,
mp_obj_new_set,
+ mp_obj_set_store,
#endif
+ mp_obj_list_append,
+ mp_obj_dict_store,
mp_make_function_from_raw_code,
mp_native_call_function_n_kw,
mp_call_method_n_kw,
diff --git a/py/runtime0.h b/py/runtime0.h
index 3ba089bb3..ef1160859 100644
--- a/py/runtime0.h
+++ b/py/runtime0.h
@@ -172,13 +172,13 @@ typedef enum {
MP_F_BINARY_OP,
MP_F_BUILD_TUPLE,
MP_F_BUILD_LIST,
- MP_F_LIST_APPEND,
MP_F_BUILD_MAP,
- MP_F_STORE_MAP,
#if MICROPY_PY_BUILTINS_SET
- MP_F_STORE_SET,
MP_F_BUILD_SET,
+ MP_F_STORE_SET,
#endif
+ MP_F_LIST_APPEND,
+ MP_F_STORE_MAP,
MP_F_MAKE_FUNCTION_FROM_RAW_CODE,
MP_F_NATIVE_CALL_FUNCTION_N_KW,
MP_F_CALL_METHOD_N_KW,
diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py
index e159165f1..d49789620 100755
--- a/tools/mpy-tool.py
+++ b/tools/mpy-tool.py
@@ -109,15 +109,15 @@ MP_OPCODE_VAR_UINT = 2
MP_OPCODE_OFFSET = 3
# extra bytes:
-MP_BC_UNWIND_JUMP = 0x46
-MP_BC_MAKE_CLOSURE = 0x62
-MP_BC_MAKE_CLOSURE_DEFARGS = 0x63
-MP_BC_RAISE_VARARGS = 0x5c
+MP_BC_UNWIND_JUMP = 0x40
+MP_BC_MAKE_CLOSURE = 0x20
+MP_BC_MAKE_CLOSURE_DEFARGS = 0x21
+MP_BC_RAISE_VARARGS = 0x60
# extra byte if caching enabled:
-MP_BC_LOAD_NAME = 0x1b
-MP_BC_LOAD_GLOBAL = 0x1c
-MP_BC_LOAD_ATTR = 0x1d
-MP_BC_STORE_ATTR = 0x26
+MP_BC_LOAD_NAME = 0x11
+MP_BC_LOAD_GLOBAL = 0x12
+MP_BC_LOAD_ATTR = 0x13
+MP_BC_STORE_ATTR = 0x18
def make_opcode_format():
def OC4(a, b, c, d):