Skip to content

Commit d3439d0

Browse files
committed
py: Instead of having "debug on" var, have "optimization level" var.
This allows to have multiple "optimization" levels (CPython has two (-OO removes docstrings), we can have more).
1 parent 509c7a7 commit d3439d0

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

py/lexer.c

+2-7
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

+2
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

+3-2
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

-2
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);

unix/main.c

+9-3
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)