Skip to content

Commit 78035b9

Browse files
committed
py, compiler: Clean up and compress scope/compile structures.
Convert int types to uint where sensible, and then to uint8_t or uint16_t where possible to reduce RAM usage.
1 parent fc18c8e commit 78035b9

7 files changed

Lines changed: 25 additions & 40 deletions

File tree

py/compile.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,17 @@ typedef enum {
3838

3939
typedef struct _compiler_t {
4040
qstr source_file;
41-
bool is_repl;
42-
pass_kind_t pass;
43-
bool had_error; // try to keep compiler clean from nlr
41+
uint8_t is_repl;
42+
uint8_t pass; // holds enum type pass_kind_t
43+
uint8_t had_error; // try to keep compiler clean from nlr
44+
uint8_t func_arg_is_super; // used to compile special case of super() function call
4445

4546
int next_label;
4647

4748
int break_label;
4849
int continue_label;
4950
int break_continue_except_level;
50-
int cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
51+
uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
5152

5253
int n_arg_keyword;
5354
bool have_star_arg;
@@ -57,8 +58,6 @@ typedef struct _compiler_t {
5758
int param_pass_num_dict_params;
5859
int param_pass_num_default_params;
5960

60-
bool func_arg_is_super; // used to compile special case of super() function call
61-
6261
scope_t *scope_head;
6362
scope_t *scope_cur;
6463

py/compile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// These must fit in 8 bits; see scope.h
12
enum {
23
MP_EMIT_OPT_NONE,
34
MP_EMIT_OPT_BYTE_CODE,

py/emitbc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ STATIC void emit_bc_delete_id(emit_t *emit, qstr qstr) {
321321
}
322322

323323
STATIC void emit_bc_pre(emit_t *emit, int stack_size_delta) {
324+
assert((int)emit->stack_size + stack_size_delta >= 0);
324325
emit->stack_size += stack_size_delta;
325326
if (emit->stack_size > emit->scope->stack_size) {
326327
emit->scope->stack_size = emit->stack_size;

py/emitnative.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ STATIC void emit_native_set_source_line(emit_t *emit, int source_line) {
308308

309309
STATIC void adjust_stack(emit_t *emit, int stack_size_delta) {
310310
DEBUG_printf("adjust stack: stack:%d + delta:%d\n", emit->stack_size, stack_size_delta);
311+
assert((int)emit->stack_size + stack_size_delta >= 0);
311312
emit->stack_size += stack_size_delta;
312-
assert(emit->stack_size >= 0);
313313
if (emit->pass > PASS_1 && emit->stack_size > emit->scope->stack_size) {
314314
emit->scope->stack_size = emit->stack_size;
315315
}

py/runtime0.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// taken from python source, Include/code.h
2+
// These must fit in 8 bits; see scope.h
23
#define MP_SCOPE_FLAG_OPTIMISED 0x01
34
#define MP_SCOPE_FLAG_NEWLOCALS 0x02
45
#define MP_SCOPE_FLAG_VARARGS 0x04

py/scope.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
#include "scope.h"
1111

1212
scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint unique_code_id, uint emit_options) {
13-
scope_t *scope = m_new(scope_t, 1);
13+
scope_t *scope = m_new0(scope_t, 1);
1414
scope->kind = kind;
15-
scope->parent = NULL;
16-
scope->next = NULL;
1715
scope->pn = pn;
1816
scope->source_file = source_file;
1917
switch (kind) {
@@ -43,19 +41,10 @@ scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint
4341
default:
4442
assert(0);
4543
}
46-
scope->id_info_alloc = 8;
47-
scope->id_info_len = 0;
48-
scope->id_info = m_new(id_info_t, scope->id_info_alloc);
49-
50-
scope->scope_flags = 0;
51-
scope->num_params = 0;
52-
/* not needed
53-
scope->num_default_params = 0;
54-
scope->num_dict_params = 0;
55-
*/
56-
scope->num_locals = 0;
5744
scope->unique_code_id = unique_code_id;
5845
scope->emit_options = emit_options;
46+
scope->id_info_alloc = 8;
47+
scope->id_info = m_new(id_info_t, scope->id_info_alloc);
5948

6049
return scope;
6150
}

py/scope.h

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ enum {
77
};
88

99
typedef struct _id_info_t {
10-
// TODO compress this info to make structure smaller in memory
11-
bool param;
12-
int kind;
13-
qstr qstr;
14-
10+
uint8_t param;
11+
uint8_t kind;
1512
// when it's an ID_INFO_KIND_LOCAL this is the unique number of the local
1613
// whet it's an ID_INFO_KIND_CELL/FREE this is the unique number of the closed over variable
17-
int local_num;
14+
uint16_t local_num;
15+
qstr qstr;
1816
} id_info_t;
1917

2018
// scope is a "block" in Python parlance
@@ -26,20 +24,16 @@ typedef struct _scope_t {
2624
mp_parse_node_t pn;
2725
qstr source_file;
2826
qstr simple_name;
29-
int id_info_alloc;
30-
int id_info_len;
31-
id_info_t *id_info;
32-
uint scope_flags; // see runtime0.h
33-
int num_params;
34-
/* not needed
35-
int num_default_params;
36-
int num_dict_params;
37-
*/
38-
int num_locals;
39-
int stack_size; // maximum size of the locals stack
40-
int exc_stack_size; // maximum size of the exception stack
4127
uint unique_code_id;
42-
uint emit_options;
28+
uint8_t scope_flags; // see runtime0.h
29+
uint8_t emit_options; // see compile.h
30+
uint16_t num_params;
31+
uint16_t num_locals;
32+
uint16_t stack_size; // maximum size of the locals stack
33+
uint16_t exc_stack_size; // maximum size of the exception stack
34+
uint16_t id_info_alloc;
35+
uint16_t id_info_len;
36+
id_info_t *id_info;
4337
} scope_t;
4438

4539
scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint unique_code_id, uint emit_options);

0 commit comments

Comments
 (0)