summaryrefslogtreecommitdiffstatshomepage
path: root/py/bc.h
diff options
context:
space:
mode:
Diffstat (limited to 'py/bc.h')
-rw-r--r--py/bc.h74
1 files changed, 68 insertions, 6 deletions
diff --git a/py/bc.h b/py/bc.h
index 69bce89028..1ae8c99258 100644
--- a/py/bc.h
+++ b/py/bc.h
@@ -32,12 +32,15 @@
// bytecode layout:
//
-// n_state : var uint
-// n_exc_stack : var uint
-// scope_flags : byte
-// n_pos_args : byte number of arguments this function takes
-// n_kwonly_args : byte number of keyword-only arguments this function takes
-// n_def_pos_args : byte number of default positional arguments
+// func signature : var uint
+// contains six values interleaved bit-wise as: xSSSSEAA [xFSSKAED repeated]
+// x = extension another byte follows
+// S = n_state - 1 number of entries in Python value stack
+// E = n_exc_stack number of entries in exception stack
+// F = scope_flags four bits of flags, MP_SCOPE_FLAG_xxx
+// A = n_pos_args number of arguments this function takes
+// K = n_kwonly_args number of keyword-only arguments this function takes
+// D = n_def_pos_args number of default positional arguments
//
// code_info_size : var uint | code_info_size counts bytes in this chunk
// simple_name : var qstr |
@@ -60,6 +63,65 @@
// const0 : obj
// constN : obj
+#define MP_BC_PRELUDE_SIG_ENCODE(S, E, scope, out_byte, out_env) \
+do { \
+ /*// Get values to store in prelude */ \
+ size_t F = scope->scope_flags & 0x0f; /* only need to store lower 4 flag bits */ \
+ size_t A = scope->num_pos_args; \
+ size_t K = scope->num_kwonly_args; \
+ size_t D = scope->num_def_pos_args; \
+ \
+ /* Adjust S to shrink range, to compress better */ \
+ S -= 1; \
+ \
+ /* Encode prelude */ \
+ /* xSSSSEAA */ \
+ uint8_t z = (S & 0xf) << 3 | (E & 1) << 2 | (A & 3); \
+ S >>= 4; \
+ E >>= 1; \
+ A >>= 2; \
+ while (S | E | F | A | K | D) { \
+ out_byte(out_env, 0x80 | z); \
+ /* xFSSKAED */ \
+ z = (F & 1) << 6 | (S & 3) << 4 | (K & 1) << 3 \
+ | (A & 1) << 2 | (E & 1) << 1 | (D & 1); \
+ S >>= 2; \
+ E >>= 1; \
+ F >>= 1; \
+ A >>= 1; \
+ K >>= 1; \
+ D >>= 1; \
+ } \
+ out_byte(out_env, z); \
+} while (0)
+
+#define MP_BC_PRELUDE_SIG_DECODE_INTO(ip, S, E, F, A, K, D) \
+do { \
+ uint8_t z = *(ip)++; \
+ /* xSSSSEAA */ \
+ S = (z >> 3) & 0xf; \
+ E = (z >> 2) & 0x1; \
+ F = 0; \
+ A = z & 0x3; \
+ K = 0; \
+ D = 0; \
+ for (unsigned n = 0; z & 0x80; ++n) { \
+ z = *(ip)++; \
+ /* xFSSKAED */ \
+ S |= (z & 0x30) << (2 * n); \
+ E |= (z & 0x02) << n; \
+ F |= ((z & 0x40) >> 6) << n; \
+ A |= (z & 0x4) << n; \
+ K |= ((z & 0x08) >> 3) << n; \
+ D |= (z & 0x1) << n; \
+ } \
+ S += 1; \
+} while (0)
+
+#define MP_BC_PRELUDE_SIG_DECODE(ip) \
+ size_t n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args; \
+ MP_BC_PRELUDE_SIG_DECODE_INTO(ip, n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args)
+
// Sentinel value for mp_code_state_t.exc_sp_idx
#define MP_CODE_STATE_EXC_SP_IDX_SENTINEL ((uint16_t)-1)