py: Add constant table to bytecode.

Contains just argument names at the moment but makes it easy to add
arbitrary constants.
This commit is contained in:
Damien George
2015-10-23 01:23:11 +01:00
parent 3a3db4dcf0
commit 713ea1800d
12 changed files with 97 additions and 96 deletions

View File

@@ -567,6 +567,7 @@ struct _emit_t {
vtype_kind_t saved_stack_vtype;
int prelude_offset;
int const_table_offset;
int n_state;
int stack_start;
int stack_size;
@@ -828,7 +829,24 @@ STATIC void emit_native_end_pass(emit_t *emit) {
ASM_DATA(emit->as, 1, emit->scope->num_pos_args);
ASM_DATA(emit->as, 1, emit->scope->num_kwonly_args);
ASM_DATA(emit->as, 1, emit->scope->num_def_pos_args);
// write code info (just contains block name and source file)
ASM_DATA(emit->as, 1, 5);
ASM_DATA(emit->as, 2, emit->scope->simple_name);
ASM_DATA(emit->as, 2, emit->scope->source_file);
// bytecode prelude: initialise closed over variables
for (int i = 0; i < emit->scope->id_info_len; i++) {
id_info_t *id = &emit->scope->id_info[i];
if (id->kind == ID_INFO_KIND_CELL) {
assert(id->local_num < 255);
ASM_DATA(emit->as, 1, id->local_num); // write the local which should be converted to a cell
}
}
ASM_DATA(emit->as, 1, 255); // end of list sentinel
ASM_ALIGN(emit->as, ASM_WORD_SIZE);
emit->const_table_offset = ASM_GET_CODE_POS(emit->as);
// write argument names as qstr objects
// see comment in corresponding part of emitbc.c about the logic here
@@ -844,18 +862,6 @@ STATIC void emit_native_end_pass(emit_t *emit) {
ASM_DATA(emit->as, ASM_WORD_SIZE, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
}
// write dummy code info (for mp_setup_code_state to parse)
ASM_DATA(emit->as, 1, 1);
// bytecode prelude: initialise closed over variables
for (int i = 0; i < emit->scope->id_info_len; i++) {
id_info_t *id = &emit->scope->id_info[i];
if (id->kind == ID_INFO_KIND_CELL) {
assert(id->local_num < 255);
ASM_DATA(emit->as, 1, id->local_num); // write the local which should be converted to a cell
}
}
ASM_DATA(emit->as, 1, 255); // end of list sentinel
}
ASM_END_PASS(emit->as);
@@ -878,7 +884,8 @@ STATIC void emit_native_end_pass(emit_t *emit) {
mp_emit_glue_assign_native(emit->scope->raw_code,
emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY,
f, f_len, emit->scope->num_pos_args, emit->scope->scope_flags, type_sig);
f, f_len, (mp_uint_t*)((byte*)f + emit->const_table_offset),
emit->scope->num_pos_args, emit->scope->scope_flags, type_sig);
}
}