|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2025 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "py/objcode.h" |
| 28 | + |
| 29 | +#if MICROPY_PY_BUILTINS_CODE == MICROPY_PY_BUILTINS_CODE_NONE |
| 30 | + |
| 31 | +// Code object not implemented at this configuration level. |
| 32 | + |
| 33 | +#elif MICROPY_PY_BUILTINS_CODE <= MICROPY_PY_BUILTINS_CODE_BASIC |
| 34 | + |
| 35 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 36 | + mp_type_code, |
| 37 | + MP_QSTR_code, |
| 38 | + MP_TYPE_FLAG_NONE |
| 39 | + ); |
| 40 | + |
| 41 | +#elif MICROPY_PY_BUILTINS_CODE <= MICROPY_PY_BUILTINS_CODE_FULL |
| 42 | + |
| 43 | +#include "py/profile.h" |
| 44 | + |
| 45 | +static void code_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) { |
| 46 | + (void)kind; |
| 47 | + mp_obj_code_t *o = MP_OBJ_TO_PTR(o_in); |
| 48 | + const mp_raw_code_t *rc = o->rc; |
| 49 | + const mp_bytecode_prelude_t *prelude = &rc->prelude; |
| 50 | + mp_printf(print, |
| 51 | + "<code object %q at 0x%p, file \"%q\", line %d>", |
| 52 | + MP_CODE_QSTR_MAP(o->context, prelude->qstr_block_name_idx), |
| 53 | + o, |
| 54 | + MP_CODE_QSTR_MAP(o->context, 0), |
| 55 | + rc->line_of_definition |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +static mp_obj_tuple_t *code_consts(const mp_module_context_t *context, const mp_raw_code_t *rc) { |
| 60 | + mp_obj_tuple_t *consts = MP_OBJ_TO_PTR(mp_obj_new_tuple(rc->n_children + 1, NULL)); |
| 61 | + |
| 62 | + size_t const_no = 0; |
| 63 | + for (size_t i = 0; i < rc->n_children; ++i) { |
| 64 | + mp_obj_t code = mp_obj_new_code(context, rc->children[i], true); |
| 65 | + consts->items[const_no++] = code; |
| 66 | + } |
| 67 | + consts->items[const_no++] = mp_const_none; |
| 68 | + |
| 69 | + return consts; |
| 70 | +} |
| 71 | + |
| 72 | +static mp_obj_t raw_code_lnotab(const mp_raw_code_t *rc) { |
| 73 | + // const mp_bytecode_prelude_t *prelude = &rc->prelude; |
| 74 | + uint start = 0; |
| 75 | + uint stop = rc->fun_data_len - start; |
| 76 | + |
| 77 | + uint last_lineno = mp_prof_bytecode_lineno(rc, start); |
| 78 | + uint lasti = 0; |
| 79 | + |
| 80 | + const uint buffer_chunk_size = (stop - start) >> 2; // heuristic magic |
| 81 | + uint buffer_size = buffer_chunk_size; |
| 82 | + byte *buffer = m_new(byte, buffer_size); |
| 83 | + uint buffer_index = 0; |
| 84 | + |
| 85 | + for (uint i = start; i < stop; ++i) { |
| 86 | + uint lineno = mp_prof_bytecode_lineno(rc, i); |
| 87 | + size_t line_diff = lineno - last_lineno; |
| 88 | + if (line_diff > 0) { |
| 89 | + uint instr_diff = (i - start) - lasti; |
| 90 | + |
| 91 | + assert(instr_diff < 256); |
| 92 | + assert(line_diff < 256); |
| 93 | + |
| 94 | + if (buffer_index + 2 > buffer_size) { |
| 95 | + buffer = m_renew(byte, buffer, buffer_size, buffer_size + buffer_chunk_size); |
| 96 | + buffer_size = buffer_size + buffer_chunk_size; |
| 97 | + } |
| 98 | + last_lineno = lineno; |
| 99 | + lasti = i - start; |
| 100 | + buffer[buffer_index++] = instr_diff; |
| 101 | + buffer[buffer_index++] = line_diff; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + mp_obj_t o = mp_obj_new_bytes(buffer, buffer_index); |
| 106 | + m_del(byte, buffer, buffer_size); |
| 107 | + return o; |
| 108 | +} |
| 109 | + |
| 110 | +static void code_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { |
| 111 | + if (dest[0] != MP_OBJ_NULL) { |
| 112 | + // not load attribute |
| 113 | + return; |
| 114 | + } |
| 115 | + mp_obj_code_t *o = MP_OBJ_TO_PTR(self_in); |
| 116 | + const mp_raw_code_t *rc = o->rc; |
| 117 | + const mp_bytecode_prelude_t *prelude = &rc->prelude; |
| 118 | + switch (attr) { |
| 119 | + case MP_QSTR_co_code: |
| 120 | + dest[0] = mp_obj_new_bytes( |
| 121 | + (void *)prelude->opcodes, |
| 122 | + rc->fun_data_len - (prelude->opcodes - (const byte *)rc->fun_data) |
| 123 | + ); |
| 124 | + break; |
| 125 | + case MP_QSTR_co_consts: |
| 126 | + dest[0] = MP_OBJ_FROM_PTR(code_consts(o->context, rc)); |
| 127 | + break; |
| 128 | + case MP_QSTR_co_filename: |
| 129 | + dest[0] = MP_OBJ_NEW_QSTR(MP_CODE_QSTR_MAP(o->context, 0)); |
| 130 | + break; |
| 131 | + case MP_QSTR_co_firstlineno: |
| 132 | + dest[0] = MP_OBJ_NEW_SMALL_INT(mp_prof_bytecode_lineno(rc, 0)); |
| 133 | + break; |
| 134 | + case MP_QSTR_co_name: |
| 135 | + dest[0] = MP_OBJ_NEW_QSTR(MP_CODE_QSTR_MAP(o->context, prelude->qstr_block_name_idx)); |
| 136 | + break; |
| 137 | + case MP_QSTR_co_names: |
| 138 | + dest[0] = MP_OBJ_FROM_PTR(o->dict_locals); |
| 139 | + break; |
| 140 | + case MP_QSTR_co_lnotab: |
| 141 | + if (!o->lnotab) { |
| 142 | + o->lnotab = raw_code_lnotab(rc); |
| 143 | + } |
| 144 | + dest[0] = o->lnotab; |
| 145 | + break; |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 150 | + mp_type_code, |
| 151 | + MP_QSTR_code, |
| 152 | + MP_TYPE_FLAG_NONE, |
| 153 | + print, code_print, |
| 154 | + attr, code_attr |
| 155 | + ); |
| 156 | + |
| 157 | +mp_obj_t mp_obj_new_code(const mp_module_context_t *context, const mp_raw_code_t *rc, bool result_required) { |
| 158 | + mp_obj_code_t *o; |
| 159 | + if (result_required) { |
| 160 | + o = m_new_obj(mp_obj_code_t); |
| 161 | + } else { |
| 162 | + o = m_new_obj_maybe(mp_obj_code_t); |
| 163 | + if (o == NULL) { |
| 164 | + return MP_OBJ_NULL; |
| 165 | + } |
| 166 | + } |
| 167 | + o->base.type = &mp_type_code; |
| 168 | + o->context = context; |
| 169 | + o->rc = rc; |
| 170 | + o->dict_locals = mp_locals_get(); // this is a wrong! how to do this properly? |
| 171 | + o->lnotab = MP_OBJ_NULL; |
| 172 | + return MP_OBJ_FROM_PTR(o); |
| 173 | +} |
| 174 | + |
| 175 | +#endif |
0 commit comments