Branch data Line data Source code
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 : : #ifndef MICROPY_INCLUDED_PY_OBJCODE_H 27 : : #define MICROPY_INCLUDED_PY_OBJCODE_H 28 : : 29 : : #include "py/bc.h" 30 : : 31 : : #if MICROPY_PY_BUILTINS_CODE == MICROPY_PY_BUILTINS_CODE_NONE 32 : : 33 : : // Code object not implemented at this configuration level. 34 : : 35 : : #elif MICROPY_PY_BUILTINS_CODE <= MICROPY_PY_BUILTINS_CODE_MINIMUM 36 : : 37 : : typedef struct _mp_obj_code_t { 38 : : mp_obj_base_t base; 39 : : mp_obj_t module_fun; 40 : : } mp_obj_code_t; 41 : : 42 : : static inline mp_obj_t mp_obj_new_code(mp_obj_t module_fun) { 43 : : mp_obj_code_t *code = mp_obj_malloc(mp_obj_code_t, &mp_type_code); 44 : : code->module_fun = module_fun; 45 : : return MP_OBJ_FROM_PTR(code); 46 : : } 47 : : 48 : : #elif MICROPY_PY_BUILTINS_CODE <= MICROPY_PY_BUILTINS_CODE_BASIC 49 : : 50 : : typedef struct _mp_obj_code_t { 51 : : mp_obj_base_t base; 52 : : mp_module_constants_t constants; 53 : : const void *proto_fun; 54 : : } mp_obj_code_t; 55 : : 56 : 66 : static inline mp_obj_t mp_obj_new_code(const mp_module_constants_t constants, const void *proto_fun) { 57 : 66 : mp_obj_code_t *code = mp_obj_malloc(mp_obj_code_t, &mp_type_code); 58 : 66 : code->constants = constants; 59 : 66 : code->proto_fun = proto_fun; 60 : 66 : return MP_OBJ_FROM_PTR(code); 61 : : } 62 : : 63 : 7 : static inline const mp_module_constants_t *mp_code_get_constants(mp_obj_code_t *self) { 64 : 7 : return &self->constants; 65 : : } 66 : : 67 : 59 : static inline const void *mp_code_get_proto_fun(mp_obj_code_t *self) { 68 [ + + ]: 59 : return self->proto_fun; 69 : : } 70 : : 71 : : #elif MICROPY_PY_BUILTINS_CODE <= MICROPY_PY_BUILTINS_CODE_FULL 72 : : 73 : : #include "py/emitglue.h" 74 : : 75 : : #define MP_CODE_QSTR_MAP(context, idx) (context->constants.qstr_table[idx]) 76 : : 77 : : typedef struct _mp_obj_code_t { 78 : : // TODO this was 4 words 79 : : mp_obj_base_t base; 80 : : const mp_module_context_t *context; 81 : : const mp_raw_code_t *rc; 82 : : mp_obj_dict_t *dict_locals; 83 : : mp_obj_t lnotab; 84 : : } mp_obj_code_t; 85 : : 86 : : mp_obj_t mp_obj_new_code(const mp_module_context_t *context, const mp_raw_code_t *rc, bool result_required); 87 : : 88 : : static inline const mp_module_constants_t *mp_code_get_constants(mp_obj_code_t *self) { 89 : : return &self->context->constants; 90 : : } 91 : : 92 : : static inline const void *mp_code_get_proto_fun(mp_obj_code_t *self) { 93 : : // A mp_raw_code_t is always a proto_fun (but not the other way around). 94 : : return self->rc; 95 : : } 96 : : 97 : : #endif 98 : : 99 : : #endif // MICROPY_INCLUDED_PY_OBJCODE_H