py: Reorganise bytecode layout so it's more structured, easier to edit.

This commit is contained in:
Damien George
2015-03-18 17:47:47 +00:00
parent f882d53fcd
commit 9b7f583b0c
7 changed files with 130 additions and 123 deletions

View File

@@ -57,7 +57,16 @@ const byte *mp_showbc_code_start;
void mp_bytecode_print(const void *descr, mp_uint_t n_total_args, const byte *ip, mp_uint_t len) {
mp_showbc_code_start = ip;
// get code info size
// get state size and exception stack size
mp_uint_t n_state = mp_decode_uint(&ip);
mp_uint_t n_exc_stack = mp_decode_uint(&ip);
ip = MP_ALIGN(ip, sizeof(mp_uint_t));
// get and skip arg names
const mp_obj_t *arg_names = (const mp_obj_t*)ip;
ip += n_total_args * sizeof(mp_uint_t);
const byte *code_info = ip;
mp_uint_t code_info_size = mp_decode_uint(&code_info);
ip += code_info_size;
@@ -65,7 +74,7 @@ void mp_bytecode_print(const void *descr, mp_uint_t n_total_args, const byte *ip
qstr block_name = mp_decode_uint(&code_info);
qstr source_file = mp_decode_uint(&code_info);
printf("File %s, code block '%s' (descriptor: %p, bytecode @%p " UINT_FMT " bytes)\n",
qstr_str(source_file), qstr_str(block_name), descr, code_info, len);
qstr_str(source_file), qstr_str(block_name), descr, mp_showbc_code_start, len);
// raw bytecode dump
printf("Raw bytecode (code_info_size=" UINT_FMT ", bytecode_size=" UINT_FMT "):\n", code_info_size, len - code_info_size);
@@ -80,18 +89,15 @@ void mp_bytecode_print(const void *descr, mp_uint_t n_total_args, const byte *ip
// bytecode prelude: arg names (as qstr objects)
printf("arg names:");
for (mp_uint_t i = 0; i < n_total_args; i++) {
printf(" %s", qstr_str(MP_OBJ_QSTR_VALUE(*(mp_obj_t*)ip)));
ip += sizeof(mp_obj_t);
printf(" %s", qstr_str(MP_OBJ_QSTR_VALUE(arg_names[i])));
}
printf("\n");
// bytecode prelude: state size and exception stack size; 16 bit uints
{
uint n_state = mp_decode_uint(&ip);
uint n_exc_stack = mp_decode_uint(&ip);
printf("(N_STATE %u)\n", n_state);
printf("(N_EXC_STACK %u)\n", n_exc_stack);
}
printf("(N_STATE " UINT_FMT ")\n", n_state);
printf("(N_EXC_STACK " UINT_FMT ")\n", n_exc_stack);
// for printing line number info
const byte *bytecode_start = ip;
// bytecode prelude: initialise closed over variables
{
@@ -104,7 +110,7 @@ void mp_bytecode_print(const void *descr, mp_uint_t n_total_args, const byte *ip
// print out line number info
{
mp_int_t bc = (mp_showbc_code_start + code_info_size) - ip; // start counting from the prelude
mp_int_t bc = bytecode_start - ip;
mp_uint_t source_line = 1;
printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
for (const byte* ci = code_info; *ci;) {