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  * Copyright (c) 2014 Paul Sokolovsky
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  * THE SOFTWARE.
26  */
27 #ifndef MICROPY_INCLUDED_PY_BC_H
28 #define MICROPY_INCLUDED_PY_BC_H
29 
30 #include "py/runtime.h"
31 #include "py/objfun.h"
32 
33 // bytecode layout:
34 //
35 //  func signature  : var uint
36 //      contains six values interleaved bit-wise as: xSSSSEAA [xFSSKAED repeated]
37 //          x = extension           another byte follows
38 //          S = n_state - 1         number of entries in Python value stack
39 //          E = n_exc_stack         number of entries in exception stack
40 //          F = scope_flags         four bits of flags, MP_SCOPE_FLAG_xxx
41 //          A = n_pos_args          number of arguments this function takes
42 //          K = n_kwonly_args       number of keyword-only arguments this function takes
43 //          D = n_def_pos_args      number of default positional arguments
44 //
45 //  prelude size    : var uint
46 //      contains two values interleaved bit-wise as: xIIIIIIC repeated
47 //          x = extension           another byte follows
48 //          I = n_info              number of bytes in source info section
49 //          C = n_cells             number of bytes/cells in closure section
50 //
51 //  source info section:
52 //      simple_name : var qstr
53 //      source_file : var qstr
54 //      <line number info>
55 //
56 //  closure section:
57 //      local_num0  : byte
58 //      ...         : byte
59 //      local_numN  : byte          N = n_cells-1
60 //
61 //  <word alignment padding>        only needed if bytecode contains pointers
62 //
63 //  <bytecode>
64 //
65 //
66 // constant table layout:
67 //
68 //  argname0        : obj (qstr)
69 //  ...             : obj (qstr)
70 //  argnameN        : obj (qstr)    N = num_pos_args + num_kwonly_args
71 //  const0          : obj
72 //  constN          : obj
73 
74 #define MP_BC_PRELUDE_SIG_ENCODE(S, E, scope, out_byte, out_env) \
75     do {                                                            \
76         /*// Get values to store in prelude */                      \
77         size_t F = scope->scope_flags & MP_SCOPE_FLAG_ALL_SIG;      \
78         size_t A = scope->num_pos_args;                             \
79         size_t K = scope->num_kwonly_args;                          \
80         size_t D = scope->num_def_pos_args;                         \
81                                                                 \
82         /* Adjust S to shrink range, to compress better */          \
83         S -= 1;                                                     \
84                                                                 \
85         /* Encode prelude */                                        \
86         /* xSSSSEAA */                                              \
87         uint8_t z = (S & 0xf) << 3 | (E & 1) << 2 | (A & 3);        \
88         S >>= 4;                                                    \
89         E >>= 1;                                                    \
90         A >>= 2;                                                    \
91         while (S | E | F | A | K | D) {                             \
92             out_byte(out_env, 0x80 | z);                            \
93             /* xFSSKAED */                                          \
94             z = (F & 1) << 6 | (S & 3) << 4 | (K & 1) << 3          \
95                 | (A & 1) << 2 | (E & 1) << 1 | (D & 1);            \
96             S >>= 2;                                                \
97             E >>= 1;                                                \
98             F >>= 1;                                                \
99             A >>= 1;                                                \
100             K >>= 1;                                                \
101             D >>= 1;                                                \
102         }                                                           \
103         out_byte(out_env, z);                                       \
104     } while (0)
105 
106 #define MP_BC_PRELUDE_SIG_DECODE_INTO(ip, S, E, F, A, K, D)     \
107     do {                                                            \
108         uint8_t z = *(ip)++;                                        \
109         /* xSSSSEAA */                                              \
110         S = (z >> 3) & 0xf;                                         \
111         E = (z >> 2) & 0x1;                                         \
112         F = 0;                                                      \
113         A = z & 0x3;                                                \
114         K = 0;                                                      \
115         D = 0;                                                      \
116         for (unsigned n = 0; z & 0x80; ++n) {                       \
117             z = *(ip)++;                                            \
118             /* xFSSKAED */                                          \
119             S |= (z & 0x30) << (2 * n);                             \
120             E |= (z & 0x02) << n;                                   \
121             F |= ((z & 0x40) >> 6) << n;                            \
122             A |= (z & 0x4) << n;                                    \
123             K |= ((z & 0x08) >> 3) << n;                            \
124             D |= (z & 0x1) << n;                                    \
125         }                                                           \
126         S += 1;                                                     \
127     } while (0)
128 
129 #define MP_BC_PRELUDE_SIG_DECODE(ip) \
130     size_t n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args; \
131     MP_BC_PRELUDE_SIG_DECODE_INTO(ip, n_state, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args); \
132     (void)n_state; (void)n_exc_stack; (void)scope_flags; \
133     (void)n_pos_args; (void)n_kwonly_args; (void)n_def_pos_args
134 
135 #define MP_BC_PRELUDE_SIZE_ENCODE(I, C, out_byte, out_env)      \
136     do {                                                            \
137         /* Encode bit-wise as: xIIIIIIC */                          \
138         uint8_t z = 0;                                              \
139         do {                                                        \
140             z = (I & 0x3f) << 1 | (C & 1);                          \
141             C >>= 1;                                                \
142             I >>= 6;                                                \
143             if (C | I) {                                            \
144                 z |= 0x80;                                          \
145             }                                                       \
146             out_byte(out_env, z);                                   \
147         } while (C | I);                                            \
148     } while (0)
149 
150 #define MP_BC_PRELUDE_SIZE_DECODE_INTO(ip, I, C)                \
151     do {                                                            \
152         uint8_t z;                                                  \
153         C = 0;                                                      \
154         I = 0;                                                      \
155         for (unsigned n = 0;; ++n) {                                \
156             z = *(ip)++;                                            \
157             /* xIIIIIIC */                                          \
158             C |= (z & 1) << n;                                      \
159             I |= ((z & 0x7e) >> 1) << (6 * n);                      \
160             if (!(z & 0x80)) {                                      \
161                 break;                                              \
162             }                                                       \
163         }                                                           \
164     } while (0)
165 
166 #define MP_BC_PRELUDE_SIZE_DECODE(ip) \
167     size_t n_info, n_cell; \
168     MP_BC_PRELUDE_SIZE_DECODE_INTO(ip, n_info, n_cell); \
169     (void)n_info; (void)n_cell
170 
171 // Sentinel value for mp_code_state_t.exc_sp_idx
172 #define MP_CODE_STATE_EXC_SP_IDX_SENTINEL ((uint16_t)-1)
173 
174 // To convert mp_code_state_t.exc_sp_idx to/from a pointer to mp_exc_stack_t
175 #define MP_CODE_STATE_EXC_SP_IDX_FROM_PTR(exc_stack, exc_sp) ((exc_sp) + 1 - (exc_stack))
176 #define MP_CODE_STATE_EXC_SP_IDX_TO_PTR(exc_stack, exc_sp_idx) ((exc_stack) + (exc_sp_idx) - 1)
177 
178 typedef struct _mp_bytecode_prelude_t {
179     uint n_state;
180     uint n_exc_stack;
181     uint scope_flags;
182     uint n_pos_args;
183     uint n_kwonly_args;
184     uint n_def_pos_args;
185     qstr qstr_block_name;
186     qstr qstr_source_file;
187     const byte *line_info;
188     const byte *opcodes;
189 } mp_bytecode_prelude_t;
190 
191 // Exception stack entry
192 typedef struct _mp_exc_stack_t {
193     const byte *handler;
194     // bit 0 is currently unused
195     // bit 1 is whether the opcode was SETUP_WITH or SETUP_FINALLY
196     mp_obj_t *val_sp;
197     // Saved exception
198     mp_obj_base_t *prev_exc;
199 } mp_exc_stack_t;
200 
201 typedef struct _mp_code_state_t {
202     // The fun_bc entry points to the underlying function object that is being executed.
203     // It is needed to access the start of bytecode and the const_table.
204     // It is also needed to prevent the GC from reclaiming the bytecode during execution,
205     // because the ip pointer below will always point to the interior of the bytecode.
206     mp_obj_fun_bc_t *fun_bc;
207     const byte *ip;
208     mp_obj_t *sp;
209     uint16_t n_state;
210     uint16_t exc_sp_idx;
211     mp_obj_dict_t *old_globals;
212     #if MICROPY_STACKLESS
213     struct _mp_code_state_t *prev;
214     #endif
215     #if MICROPY_PY_SYS_SETTRACE
216     struct _mp_code_state_t *prev_state;
217     struct _mp_obj_frame_t *frame;
218     #endif
219     // Variable-length
220     mp_obj_t state[0];
221     // Variable-length, never accessed by name, only as (void*)(state + n_state)
222     // mp_exc_stack_t exc_state[0];
223 } mp_code_state_t;
224 
225 mp_uint_t mp_decode_uint(const byte **ptr);
226 mp_uint_t mp_decode_uint_value(const byte *ptr);
227 const byte *mp_decode_uint_skip(const byte *ptr);
228 
229 mp_vm_return_kind_t mp_execute_bytecode(mp_code_state_t *code_state, volatile mp_obj_t inject_exc);
230 mp_code_state_t *mp_obj_fun_bc_prepare_codestate(mp_obj_t func, size_t n_args, size_t n_kw, const mp_obj_t *args);
231 void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args);
232 void mp_bytecode_print(const mp_print_t *print, const void *descr, const byte *code, mp_uint_t len, const mp_uint_t *const_table);
233 void mp_bytecode_print2(const mp_print_t *print, const byte *code, size_t len, const mp_uint_t *const_table);
234 const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip);
235 #define mp_bytecode_print_inst(print, code, const_table) mp_bytecode_print2(print, code, 1, const_table)
236 
237 // Helper macros to access pointer with least significant bits holding flags
238 #define MP_TAGPTR_PTR(x) ((void *)((uintptr_t)(x) & ~((uintptr_t)3)))
239 #define MP_TAGPTR_TAG0(x) ((uintptr_t)(x) & 1)
240 #define MP_TAGPTR_TAG1(x) ((uintptr_t)(x) & 2)
241 #define MP_TAGPTR_MAKE(ptr, tag) ((void *)((uintptr_t)(ptr) | (tag)))
242 
243 #if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE
244 
245 uint mp_opcode_format(const byte *ip, size_t *opcode_size, bool count_var_uint);
246 
247 #endif
248 
mp_bytecode_get_source_line(const byte * line_info,size_t bc_offset)249 static inline size_t mp_bytecode_get_source_line(const byte *line_info, size_t bc_offset) {
250     size_t source_line = 1;
251     size_t c;
252     while ((c = *line_info)) {
253         size_t b, l;
254         if ((c & 0x80) == 0) {
255             // 0b0LLBBBBB encoding
256             b = c & 0x1f;
257             l = c >> 5;
258             line_info += 1;
259         } else {
260             // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
261             b = c & 0xf;
262             l = ((c << 4) & 0x700) | line_info[1];
263             line_info += 2;
264         }
265         if (bc_offset >= b) {
266             bc_offset -= b;
267             source_line += l;
268         } else {
269             // found source line corresponding to bytecode offset
270             break;
271         }
272     }
273     return source_line;
274 }
275 
276 #endif // MICROPY_INCLUDED_PY_BC_H
277