Skip to content

Commit 00fe312

Browse files
committed
py/bc: Factor out helper for line-number decoding.
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
1 parent 05342b0 commit 00fe312

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

py/bc.h

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -308,25 +308,35 @@ static inline void mp_module_context_alloc_tables(mp_module_context_t *context,
308308
#endif
309309
}
310310

311+
typedef struct _mp_code_lineinfo_t {
312+
size_t bc_increment;
313+
size_t line_increment;
314+
} mp_code_lineinfo_t;
315+
316+
static inline mp_code_lineinfo_t mp_bytecode_decode_lineinfo(const byte **line_info) {
317+
mp_code_lineinfo_t result;
318+
size_t c = (*line_info)[0];
319+
if ((c & 0x80) == 0) {
320+
// 0b0LLBBBBB encoding
321+
result.bc_increment = c & 0x1f;
322+
result.line_increment = c >> 5;
323+
*line_info += 1;
324+
} else {
325+
// 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
326+
result.bc_increment = c & 0xf;
327+
result.line_increment = ((c << 4) & 0x700) | (*line_info)[1];
328+
*line_info += 2;
329+
}
330+
return result;
331+
}
332+
311333
static inline size_t mp_bytecode_get_source_line(const byte *line_info, const byte *line_info_top, size_t bc_offset) {
312334
size_t source_line = 1;
313335
while (line_info < line_info_top) {
314-
size_t c = *line_info;
315-
size_t b, l;
316-
if ((c & 0x80) == 0) {
317-
// 0b0LLBBBBB encoding
318-
b = c & 0x1f;
319-
l = c >> 5;
320-
line_info += 1;
321-
} else {
322-
// 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
323-
b = c & 0xf;
324-
l = ((c << 4) & 0x700) | line_info[1];
325-
line_info += 2;
326-
}
327-
if (bc_offset >= b) {
328-
bc_offset -= b;
329-
source_line += l;
336+
mp_code_lineinfo_t decoded = mp_bytecode_decode_lineinfo(&line_info);
337+
if (bc_offset >= decoded.bc_increment) {
338+
bc_offset -= decoded.bc_increment;
339+
source_line += decoded.line_increment;
330340
} else {
331341
// found source line corresponding to bytecode offset
332342
break;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy