Skip to content

Commit 07995e9

Browse files
committed
Merge pull request micropython#649 from pfalcon/multi-opt
Support multiple bytecode optimisation levels
2 parents 509c7a7 + 411732e commit 07995e9

File tree

7 files changed

+28
-18
lines changed

7 files changed

+28
-18
lines changed

py/emitbc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,10 @@ STATIC void emit_bc_adjust_stack_size(emit_t *emit, int delta) {
352352
STATIC void emit_bc_set_source_line(emit_t *emit, int source_line) {
353353
//printf("source: line %d -> %d offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->bytecode_offset);
354354
#if MICROPY_ENABLE_SOURCE_LINE
355+
if (mp_optimise_value >= 3) {
356+
// If we compile with -O3, don't store line numbers.
357+
return;
358+
}
355359
if (source_line > emit->last_source_line) {
356360
uint bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
357361
uint lines_to_skip = source_line - emit->last_source_line;

py/lexer.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,7 @@ struct _mp_lexer_t {
6464
mp_token_t tok_cur;
6565
};
6666

67-
// debug flag for __debug__ constant
68-
STATIC mp_token_kind_t mp_debug_value;
69-
70-
void mp_set_debug(bool value) {
71-
mp_debug_value = value ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE;
72-
}
67+
uint mp_optimise_value;
7368

7469
// TODO replace with a call to a standard function
7570
bool str_strn_equal(const char *str, const char *strn, int len) {
@@ -703,7 +698,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
703698
if (str_strn_equal(tok_kw[i], tok->str, tok->len)) {
704699
if (i == ARRAY_SIZE(tok_kw) - 1) {
705700
// tok_kw[ARRAY_SIZE(tok_kw) - 1] == "__debug__"
706-
tok->kind = mp_debug_value;
701+
tok->kind = (mp_optimise_value == 0 ? MP_TOKEN_KW_TRUE : MP_TOKEN_KW_FALSE);
707702
} else {
708703
tok->kind = MP_TOKEN_KW_FALSE + i;
709704
}

py/lexer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,5 @@ typedef enum {
176176

177177
mp_import_stat_t mp_import_stat(const char *path);
178178
mp_lexer_t *mp_lexer_new_from_file(const char *filename);
179+
180+
extern uint mp_optimise_value;

py/runtime.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "bc.h"
4646
#include "smallint.h"
4747
#include "objgenerator.h"
48+
#include "lexer.h"
4849

4950
#if 0 // print debugging info
5051
#define DEBUG_PRINT (1)
@@ -74,8 +75,8 @@ void mp_init(void) {
7475
MICROPY_PORT_INIT_FUNC;
7576
#endif
7677

77-
// __debug__ enabled by default
78-
mp_set_debug(true);
78+
// optimization disabled by default
79+
mp_optimise_value = 0;
7980

8081
// init global module stuff
8182
mp_module_init();

py/runtime.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ typedef struct _mp_arg_t {
5454
void mp_init(void);
5555
void mp_deinit(void);
5656

57-
void mp_set_debug(bool value); // sets the value of __debug__; see lexer.c
58-
5957
void mp_arg_check_num(uint n_args, uint n_kw, uint n_args_min, uint n_args_max, bool takes_kw);
6058
void mp_arg_parse_all(uint n_pos, const mp_obj_t *pos, mp_map_t *kws, uint n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
6159
void mp_arg_parse_all_kw_array(uint n_pos, uint n_kw, const mp_obj_t *args, uint n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);

py/vm.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,12 +1042,16 @@ mp_vm_return_kind_t mp_execute_bytecode2(mp_code_state *code_state, volatile mp_
10421042
machine_uint_t code_info_size = code_info[0] | (code_info[1] << 8) | (code_info[2] << 16) | (code_info[3] << 24);
10431043
qstr source_file = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24);
10441044
qstr block_name = code_info[8] | (code_info[9] << 8) | (code_info[10] << 16) | (code_info[11] << 24);
1045-
machine_uint_t source_line = 1;
1045+
machine_uint_t source_line = 0;
10461046
machine_uint_t bc = code_state->ip - code_info - code_info_size;
10471047
//printf("find %lu %d %d\n", bc, code_info[12], code_info[13]);
1048-
for (const byte* ci = code_info + 12; *ci && bc >= ((*ci) & 31); ci++) {
1049-
bc -= *ci & 31;
1050-
source_line += *ci >> 5;
1048+
const byte* ci = code_info + 12;
1049+
if (*ci) {
1050+
source_line = 1;
1051+
for (; *ci && bc >= ((*ci) & 31); ci++) {
1052+
bc -= *ci & 31;
1053+
source_line += *ci >> 5;
1054+
}
10511055
}
10521056
mp_obj_exception_add_traceback(nlr.ret_val, source_file, source_line, block_name);
10531057
}

unix/main.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <string.h>
3131
#include <stdlib.h>
3232
#include <stdarg.h>
33+
#include <ctype.h>
3334
#include <sys/stat.h>
3435
#include <sys/types.h>
3536
#include <errno.h>
@@ -188,6 +189,7 @@ int usage(char **argv) {
188189
"usage: %s [<opts>] [-X <implopt>] [-c <command>] [<filename>]\n"
189190
"Options:\n"
190191
"-v : verbose (trace various operations); can be multiple\n"
192+
"-O[N] : apply bytecode optimizations of level N\n"
191193
"\n"
192194
"Implementation specific options:\n", argv[0]
193195
);
@@ -346,9 +348,13 @@ int main(int argc, char **argv) {
346348
a += 1;
347349
} else if (strcmp(argv[a], "-v") == 0) {
348350
mp_verbose_flag++;
349-
} else if (strcmp(argv[a], "-O") == 0) {
350-
// optimisation; sets __debug__ to False
351-
mp_set_debug(false);
351+
} else if (strncmp(argv[a], "-O", 2) == 0) {
352+
if (isdigit(argv[a][2])) {
353+
mp_optimise_value = argv[a][2] & 0xf;
354+
} else {
355+
mp_optimise_value = 0;
356+
for (char *p = argv[a] + 1; *p && *p == 'O'; p++, mp_optimise_value++);
357+
}
352358
} else {
353359
return usage(argv);
354360
}

0 commit comments

Comments
 (0)