Commit 78035b99 authored by Damien George's avatar Damien George
Browse files

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.
parent fc18c8e8
...@@ -38,16 +38,17 @@ typedef enum { ...@@ -38,16 +38,17 @@ typedef enum {
typedef struct _compiler_t { typedef struct _compiler_t {
qstr source_file; qstr source_file;
bool is_repl; uint8_t is_repl;
pass_kind_t pass; uint8_t pass; // holds enum type pass_kind_t
bool had_error; // try to keep compiler clean from nlr uint8_t had_error; // try to keep compiler clean from nlr
uint8_t func_arg_is_super; // used to compile special case of super() function call
int next_label; int next_label;
int break_label; int break_label;
int continue_label; int continue_label;
int break_continue_except_level; int break_continue_except_level;
int cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
int n_arg_keyword; int n_arg_keyword;
bool have_star_arg; bool have_star_arg;
...@@ -57,8 +58,6 @@ typedef struct _compiler_t { ...@@ -57,8 +58,6 @@ typedef struct _compiler_t {
int param_pass_num_dict_params; int param_pass_num_dict_params;
int param_pass_num_default_params; int param_pass_num_default_params;
bool func_arg_is_super; // used to compile special case of super() function call
scope_t *scope_head; scope_t *scope_head;
scope_t *scope_cur; scope_t *scope_cur;
......
// These must fit in 8 bits; see scope.h
enum { enum {
MP_EMIT_OPT_NONE, MP_EMIT_OPT_NONE,
MP_EMIT_OPT_BYTE_CODE, MP_EMIT_OPT_BYTE_CODE,
......
...@@ -321,6 +321,7 @@ STATIC void emit_bc_delete_id(emit_t *emit, qstr qstr) { ...@@ -321,6 +321,7 @@ STATIC void emit_bc_delete_id(emit_t *emit, qstr qstr) {
} }
STATIC void emit_bc_pre(emit_t *emit, int stack_size_delta) { STATIC void emit_bc_pre(emit_t *emit, int stack_size_delta) {
assert((int)emit->stack_size + stack_size_delta >= 0);
emit->stack_size += stack_size_delta; emit->stack_size += stack_size_delta;
if (emit->stack_size > emit->scope->stack_size) { if (emit->stack_size > emit->scope->stack_size) {
emit->scope->stack_size = emit->stack_size; emit->scope->stack_size = emit->stack_size;
......
...@@ -308,8 +308,8 @@ STATIC void emit_native_set_source_line(emit_t *emit, int source_line) { ...@@ -308,8 +308,8 @@ STATIC void emit_native_set_source_line(emit_t *emit, int source_line) {
STATIC void adjust_stack(emit_t *emit, int stack_size_delta) { STATIC void adjust_stack(emit_t *emit, int stack_size_delta) {
DEBUG_printf("adjust stack: stack:%d + delta:%d\n", emit->stack_size, stack_size_delta); DEBUG_printf("adjust stack: stack:%d + delta:%d\n", emit->stack_size, stack_size_delta);
assert((int)emit->stack_size + stack_size_delta >= 0);
emit->stack_size += stack_size_delta; emit->stack_size += stack_size_delta;
assert(emit->stack_size >= 0);
if (emit->pass > PASS_1 && emit->stack_size > emit->scope->stack_size) { if (emit->pass > PASS_1 && emit->stack_size > emit->scope->stack_size) {
emit->scope->stack_size = emit->stack_size; emit->scope->stack_size = emit->stack_size;
} }
......
// taken from python source, Include/code.h // taken from python source, Include/code.h
// These must fit in 8 bits; see scope.h
#define MP_SCOPE_FLAG_OPTIMISED 0x01 #define MP_SCOPE_FLAG_OPTIMISED 0x01
#define MP_SCOPE_FLAG_NEWLOCALS 0x02 #define MP_SCOPE_FLAG_NEWLOCALS 0x02
#define MP_SCOPE_FLAG_VARARGS 0x04 #define MP_SCOPE_FLAG_VARARGS 0x04
......
...@@ -10,10 +10,8 @@ ...@@ -10,10 +10,8 @@
#include "scope.h" #include "scope.h"
scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint unique_code_id, uint emit_options) { scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint unique_code_id, uint emit_options) {
scope_t *scope = m_new(scope_t, 1); scope_t *scope = m_new0(scope_t, 1);
scope->kind = kind; scope->kind = kind;
scope->parent = NULL;
scope->next = NULL;
scope->pn = pn; scope->pn = pn;
scope->source_file = source_file; scope->source_file = source_file;
switch (kind) { switch (kind) {
...@@ -43,19 +41,10 @@ scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint ...@@ -43,19 +41,10 @@ scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint
default: default:
assert(0); assert(0);
} }
scope->id_info_alloc = 8;
scope->id_info_len = 0;
scope->id_info = m_new(id_info_t, scope->id_info_alloc);
scope->scope_flags = 0;
scope->num_params = 0;
/* not needed
scope->num_default_params = 0;
scope->num_dict_params = 0;
*/
scope->num_locals = 0;
scope->unique_code_id = unique_code_id; scope->unique_code_id = unique_code_id;
scope->emit_options = emit_options; scope->emit_options = emit_options;
scope->id_info_alloc = 8;
scope->id_info = m_new(id_info_t, scope->id_info_alloc);
return scope; return scope;
} }
......
...@@ -7,14 +7,12 @@ enum { ...@@ -7,14 +7,12 @@ enum {
}; };
typedef struct _id_info_t { typedef struct _id_info_t {
// TODO compress this info to make structure smaller in memory uint8_t param;
bool param; uint8_t kind;
int kind;
qstr qstr;
// when it's an ID_INFO_KIND_LOCAL this is the unique number of the local // when it's an ID_INFO_KIND_LOCAL this is the unique number of the local
// whet it's an ID_INFO_KIND_CELL/FREE this is the unique number of the closed over variable // whet it's an ID_INFO_KIND_CELL/FREE this is the unique number of the closed over variable
int local_num; uint16_t local_num;
qstr qstr;
} id_info_t; } id_info_t;
// scope is a "block" in Python parlance // scope is a "block" in Python parlance
...@@ -26,20 +24,16 @@ typedef struct _scope_t { ...@@ -26,20 +24,16 @@ typedef struct _scope_t {
mp_parse_node_t pn; mp_parse_node_t pn;
qstr source_file; qstr source_file;
qstr simple_name; qstr simple_name;
int id_info_alloc;
int id_info_len;
id_info_t *id_info;
uint scope_flags; // see runtime0.h
int num_params;
/* not needed
int num_default_params;
int num_dict_params;
*/
int num_locals;
int stack_size; // maximum size of the locals stack
int exc_stack_size; // maximum size of the exception stack
uint unique_code_id; uint unique_code_id;
uint emit_options; uint8_t scope_flags; // see runtime0.h
uint8_t emit_options; // see compile.h
uint16_t num_params;
uint16_t num_locals;
uint16_t stack_size; // maximum size of the locals stack
uint16_t exc_stack_size; // maximum size of the exception stack
uint16_t id_info_alloc;
uint16_t id_info_len;
id_info_t *id_info;
} scope_t; } scope_t;
scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint unique_code_id, uint emit_options); scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint unique_code_id, uint emit_options);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment