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
133#define MP_BC_PRELUDE_SIZE_ENCODE(I, C, out_byte, out_env) \
134 do { \
135 /* Encode bit-wise as: xIIIIIIC */ \
136 uint8_t z = 0; \
137 do { \
138 z = (I & 0x3f) << 1 | (C & 1); \
139 C >>= 1; \
140 I >>= 6; \
141 if (C | I) { \
142 z |= 0x80; \
143 } \
144 out_byte(out_env, z); \
145 } while (C | I); \
146 } while (0)
147
148#define MP_BC_PRELUDE_SIZE_DECODE_INTO(ip, I, C) \
149 do { \
150 uint8_t z; \
151 C = 0; \
152 I = 0; \
153 for (unsigned n = 0;; ++n) { \
154 z = *(ip)++; \
155 /* xIIIIIIC */ \
156 C |= (z & 1) << n; \
157 I |= ((z & 0x7e) >> 1) << (6 * n); \
158 if (!(z & 0x80)) { \
159 break; \
160 } \
161 } \
162 } while (0)
163
164#define MP_BC_PRELUDE_SIZE_DECODE(ip) \
165 size_t n_info, n_cell; \
166 MP_BC_PRELUDE_SIZE_DECODE_INTO(ip, n_info, n_cell)
167
168// Sentinel value for mp_code_state_t.exc_sp_idx
169#define MP_CODE_STATE_EXC_SP_IDX_SENTINEL ((uint16_t)-1)
170
171// To convert mp_code_state_t.exc_sp_idx to/from a pointer to mp_exc_stack_t
172#define MP_CODE_STATE_EXC_SP_IDX_FROM_PTR(exc_stack, exc_sp) ((exc_sp) + 1 - (exc_stack))
173#define MP_CODE_STATE_EXC_SP_IDX_TO_PTR(exc_stack, exc_sp_idx) ((exc_stack) + (exc_sp_idx) - 1)
174
175typedef struct _mp_bytecode_prelude_t {
176 uint n_state;
177 uint n_exc_stack;
178 uint scope_flags;
179 uint n_pos_args;
180 uint n_kwonly_args;
181 uint n_def_pos_args;
182 qstr qstr_block_name;
183 qstr qstr_source_file;
184 const byte *line_info;
185 const byte *opcodes;
186} mp_bytecode_prelude_t;
187
188// Exception stack entry
189typedef struct _mp_exc_stack_t {
190 const byte *handler;
191 // bit 0 is currently unused
192 // bit 1 is whether the opcode was SETUP_WITH or SETUP_FINALLY
193 mp_obj_t *val_sp;
194 // Saved exception
195 mp_obj_base_t *prev_exc;
196} mp_exc_stack_t;
197
198typedef struct _mp_code_state_t {
199 // The fun_bc entry points to the underlying function object that is being executed.
200 // It is needed to access the start of bytecode and the const_table.
201 // It is also needed to prevent the GC from reclaiming the bytecode during execution,
202 // because the ip pointer below will always point to the interior of the bytecode.
203 mp_obj_fun_bc_t *fun_bc;
204 const byte *ip;
205 mp_obj_t *sp;
206 uint16_t n_state;
207 uint16_t exc_sp_idx;
208 mp_obj_dict_t *old_globals;
209 #if MICROPY_STACKLESS
210 struct _mp_code_state_t *prev;
211 #endif
212 #if MICROPY_PY_SYS_SETTRACE
213 struct _mp_code_state_t *prev_state;
214 struct _mp_obj_frame_t *frame;
215 #endif
216 // Variable-length
217 mp_obj_t state[0];
218 // Variable-length, never accessed by name, only as (void*)(state + n_state)
219 // mp_exc_stack_t exc_state[0];
220} mp_code_state_t;
221
222mp_uint_t mp_decode_uint(const byte **ptr);
223mp_uint_t mp_decode_uint_value(const byte *ptr);
224const byte *mp_decode_uint_skip(const byte *ptr);
225
226mp_vm_return_kind_t mp_execute_bytecode(mp_code_state_t *code_state, volatile mp_obj_t inject_exc);
227mp_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);
228void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args);
229void mp_bytecode_print(const mp_print_t *print, const void *descr, const byte *code, mp_uint_t len, const mp_uint_t *const_table);
230void mp_bytecode_print2(const mp_print_t *print, const byte *code, size_t len, const mp_uint_t *const_table);
231const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip);
232#define mp_bytecode_print_inst(print, code, const_table) mp_bytecode_print2(print, code, 1, const_table)
233
234// Helper macros to access pointer with least significant bits holding flags
235#define MP_TAGPTR_PTR(x) ((void *)((uintptr_t)(x) & ~((uintptr_t)3)))
236#define MP_TAGPTR_TAG0(x) ((uintptr_t)(x) & 1)
237#define MP_TAGPTR_TAG1(x) ((uintptr_t)(x) & 2)
238#define MP_TAGPTR_MAKE(ptr, tag) ((void *)((uintptr_t)(ptr) | (tag)))
239
240#if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE
241
242uint mp_opcode_format(const byte *ip, size_t *opcode_size, bool count_var_uint);
243
244#endif
245
246static inline size_t mp_bytecode_get_source_line(const byte *line_info, size_t bc_offset) {
247 size_t source_line = 1;
248 size_t c;
249 while ((c = *line_info)) {
250 size_t b, l;
251 if ((c & 0x80) == 0) {
252 // 0b0LLBBBBB encoding
253 b = c & 0x1f;
254 l = c >> 5;
255 line_info += 1;
256 } else {
257 // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
258 b = c & 0xf;
259 l = ((c << 4) & 0x700) | line_info[1];
260 line_info += 2;
261 }
262 if (bc_offset >= b) {
263 bc_offset -= b;
264 source_line += l;
265 } else {
266 // found source line corresponding to bytecode offset
267 break;
268 }
269 }
270 return source_line;
271}
272
273#endif // MICROPY_INCLUDED_PY_BC_H
274