def freeze_mpy(base_qstrs, raw_codes): # add to qstrs new = {} for q in global_qstrs: # don't add duplicates if q.qstr_esc in base_qstrs or q.qstr_esc in new: continue new[q.qstr_esc] = (len(new), q.qstr_esc, q.str) new = sorted(new.values(), key=lambda x: x[0]) print('#include "py/mpconfig.h"') print('#include "py/objint.h"') print('#include "py/objstr.h"') print('#include "py/emitglue.h"') print() print('#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE != %u' % config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) print('#error "incompatible MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE"') print('#endif') print() print('#if MICROPY_LONGINT_IMPL != %u' % config.MICROPY_LONGINT_IMPL) print('#error "incompatible MICROPY_LONGINT_IMPL"') print('#endif') print() if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ: print('#if MPZ_DIG_SIZE != %u' % config.MPZ_DIG_SIZE) print('#error "incompatible MPZ_DIG_SIZE"') print('#endif') print() print('#if MICROPY_PY_BUILTINS_FLOAT') print('typedef struct _mp_obj_float_t {') print(' mp_obj_base_t base;') print(' mp_float_t value;') print('} mp_obj_float_t;') print('#endif') print() print('#if MICROPY_PY_BUILTINS_COMPLEX') print('typedef struct _mp_obj_complex_t {') print(' mp_obj_base_t base;') print(' mp_float_t real;') print(' mp_float_t imag;') print('} mp_obj_complex_t;') print('#endif') print() print('enum {') for i in range(len(new)): if i == 0: print(' MP_QSTR_%s = MP_QSTRnumber_of,' % new[i][1]) else: print(' MP_QSTR_%s,' % new[i][1]) print('};') print() print('extern const qstr_pool_t mp_qstr_const_pool;'); print('const qstr_pool_t mp_qstr_frozen_const_pool = {') print(' (qstr_pool_t*)&mp_qstr_const_pool, // previous pool') print(' MP_QSTRnumber_of, // previous pool size') print(' %u, // allocated entries' % len(new)) print(' %u, // used entries' % len(new)) print(' {') for _, _, qstr in new: print(' %s,' % qstrutil.make_bytes(config.MICROPY_QSTR_BYTES_IN_LEN, config.MICROPY_QSTR_BYTES_IN_HASH, qstr)) print(' },') print('};') for rc in raw_codes: rc.freeze(rc.source_file.str.replace('/', '_')[:-3] + '_') print() print('const char mp_frozen_mpy_names[] = {') for rc in raw_codes: module_name = rc.source_file.str print('"%s\\0"' % module_name) print('"\\0"};') print('const mp_raw_code_t *const mp_frozen_mpy_content[] = {') for rc in raw_codes: print(' &raw_code_%s,' % rc.escaped_name) print('};')
def freeze_mpy(base_qstrs, raw_codes): # add to qstrs new = {} for q in global_qstrs: # don't add duplicates if q is None or q.qstr_esc in base_qstrs or q.qstr_esc in new: continue new[q.qstr_esc] = (len(new), q.qstr_esc, q.str) new = sorted(new.values(), key=lambda x: x[2]) print('#include "py/mpconfig.h"') print('#include "py/objint.h"') print('#include "py/objstr.h"') print('#include "py/emitglue.h"') print('#include "py/nativeglue.h"') print() print("#if MICROPY_LONGINT_IMPL != %u" % config.MICROPY_LONGINT_IMPL) print('#error "incompatible MICROPY_LONGINT_IMPL"') print("#endif") print() if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ: print("#if MPZ_DIG_SIZE != %u" % config.MPZ_DIG_SIZE) print('#error "incompatible MPZ_DIG_SIZE"') print("#endif") print() print("#if MICROPY_PY_BUILTINS_FLOAT") print("typedef struct _mp_obj_float_t {") print(" mp_obj_base_t base;") print(" mp_float_t value;") print("} mp_obj_float_t;") print("#endif") print() print("#if MICROPY_PY_BUILTINS_COMPLEX") print("typedef struct _mp_obj_complex_t {") print(" mp_obj_base_t base;") print(" mp_float_t real;") print(" mp_float_t imag;") print("} mp_obj_complex_t;") print("#endif") print() if len(new) > 0: print("enum {") for i in range(len(new)): if i == 0: print(" MP_QSTR_%s = MP_QSTRnumber_of," % new[i][1]) else: print(" MP_QSTR_%s," % new[i][1]) print("};") # As in qstr.c, set so that the first dynamically allocated pool is twice this size; must be <= the len qstr_pool_alloc = min(len(new), 10) print() print("extern const qstr_pool_t mp_qstr_const_pool;") print("const qstr_pool_t mp_qstr_frozen_const_pool = {") print(" (qstr_pool_t*)&mp_qstr_const_pool, // previous pool") print(" MP_QSTRnumber_of, // previous pool size") print(" %u, // allocated entries" % qstr_pool_alloc) print(" %u, // used entries" % len(new)) print(" true, // entries are sorted") print(" {") for _, _, qstr in new: print( " %s," % qstrutil.make_bytes( config.MICROPY_QSTR_BYTES_IN_LEN, config.MICROPY_QSTR_BYTES_IN_HASH, qstr ) ) print(" },") print("};") for rc in raw_codes: rc.freeze(rc.source_file.str.replace("/", "_")[:-3] + "_") print() print("const char mp_frozen_names[] = {") print("#ifdef MP_FROZEN_STR_NAMES") # makemanifest.py might also include some frozen string content. print("MP_FROZEN_STR_NAMES") print("#endif") for rc in raw_codes: module_name = rc.source_file.str print('"%s\\0"' % module_name) print('"\\0"};') print("const mp_raw_code_t *const mp_frozen_mpy_content[] = {") for rc in raw_codes: print(" &raw_code_%s," % rc.escaped_name) print("};") # If a port defines MICROPY_FROZEN_LIST_ITEM then list all modules wrapped in that macro. print("#ifdef MICROPY_FROZEN_LIST_ITEM") for rc in raw_codes: module_name = rc.source_file.str if module_name.endswith("/__init__.py"): short_name = module_name[: -len("/__init__.py")] else: short_name = module_name[: -len(".py")] print('MICROPY_FROZEN_LIST_ITEM("%s", "%s")' % (short_name, module_name)) print("#endif")
def freeze_mpy(base_qstrs, raw_codes): # add to qstrs new = {} for q in global_qstrs: # don't add duplicates if q.qstr_esc in base_qstrs or q.qstr_esc in new: continue new[q.qstr_esc] = (len(new), q.qstr_esc, q.str) new = sorted(new.values(), key=lambda x: x[0]) print('#include "py/mpconfig.h"') print('#include "py/objint.h"') print('#include "py/objstr.h"') print('#include "py/emitglue.h"') print() print('#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE != %u' % config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) print('#error "incompatible MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE"') print('#endif') print() print('#if MICROPY_LONGINT_IMPL != %u' % config.MICROPY_LONGINT_IMPL) print('#error "incompatible MICROPY_LONGINT_IMPL"') print('#endif') print() if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ: print('#if MPZ_DIG_SIZE != %u' % config.MPZ_DIG_SIZE) print('#error "incompatible MPZ_DIG_SIZE"') print('#endif') print() print('#if MICROPY_PY_BUILTINS_FLOAT') print('typedef struct _mp_obj_float_t {') print(' mp_obj_base_t base;') print(' mp_float_t value;') print('} mp_obj_float_t;') print('#endif') print() print('#if MICROPY_PY_BUILTINS_COMPLEX') print('typedef struct _mp_obj_complex_t {') print(' mp_obj_base_t base;') print(' mp_float_t real;') print(' mp_float_t imag;') print('} mp_obj_complex_t;') print('#endif') print() print('enum {') for i in range(len(new)): if i == 0: print(' MP_QSTR_%s = MP_QSTRnumber_of,' % new[i][1]) else: print(' MP_QSTR_%s,' % new[i][1]) print('};') # As in qstr.c, set so that the first dynamically allocated pool is twice this size; must be <= the len qstr_pool_alloc = min(len(new), 10) print() print('extern const qstr_pool_t mp_qstr_const_pool;') print('const qstr_pool_t mp_qstr_frozen_const_pool = {') print(' (qstr_pool_t*)&mp_qstr_const_pool, // previous pool') print(' MP_QSTRnumber_of, // previous pool size') print(' %u, // allocated entries' % qstr_pool_alloc) print(' %u, // used entries' % len(new)) print(' {') for _, _, qstr in new: print(' %s,' % qstrutil.make_bytes(config.MICROPY_QSTR_BYTES_IN_LEN, config.MICROPY_QSTR_BYTES_IN_HASH, qstr)) print(' },') print('};') for rc in raw_codes: rc.freeze(rc.source_file.str.replace('/', '_')[:-3] + '_') print() print('const char mp_frozen_mpy_names[] = {') for rc in raw_codes: module_name = rc.source_file.str print('"%s\\0"' % module_name) print('"\\0"};') print('const mp_raw_code_t *const mp_frozen_mpy_content[] = {') for rc in raw_codes: print(' &raw_code_%s,' % rc.escaped_name) print('};')
def generatedQstrdefs(self, outputdir): sys.path.append( str(Path(user_data_dir+"/ardupycore/ArduPy/MicroPython/py"))) # import makemoduledefs import makeqstrdata import makeqstrdefs # import makeversionhdr genhdr = Path(outputdir+"/genhdr") os.makedirs(genhdr) extern_mp_src = [] # makeversionhdr.make_version_header(str(Path(genhdr,"mpversion.h"))) shutil.copyfile(str(Path( user_data_dir+"/ardupycore/Seeeduino/tools/genhdr/mpversion.h")), str(Path(genhdr, "mpversion.h"))) shutil.copyfile(str(Path(user_data_dir+"/ardupycore/Seeeduino/tools/genhdr/moduledefs.h")), str(Path(genhdr, "moduledefs.h"))) mp_generate_flag = micropython_CFLAGS.format(str(Path(user_data_dir+"/ardupycore/ArduPy")), str(Path(user_data_dir+"/ardupycore/ArduPy/boards/"+self.board))) # remove cpp files # todo; only scan file start wirh "mod_ardupy_" for f in self.srcfile: if f[-3:] == "cpp" or f[-2:] == "cc": continue if f.find("objmodule.c") != -1 or \ f.find("parse.c") != -1 or \ f.find("qstr.c") != -1: continue extern_mp_src.append(f) gen_i_last = self.gcc + "-E -DARDUPY_MODULE -DNO_QSTR " + mp_generate_flag + " ".join(extern_mp_src) + \ " " + str(Path(user_data_dir+"/ardupycore/ArduPy/boards/"+self.board+"/mpconfigport.h")) + \ " > " + str(Path(genhdr, "qstr.i.last")) print(gen_i_last) os.system(gen_i_last) import io class Args: pass args = Args() args.input_filename = str(Path(genhdr, "qstr.i.last")) args.output_dir = str(Path(genhdr, "qstr")) args.output_file = str(Path(genhdr, "qstrdefs.collected.h")) try: os.makedirs(args.output_dir) except OSError: pass makeqstrdefs.args = args with io.open(args.input_filename, encoding='utf-8') as infile: makeqstrdefs.process_file(infile) makeqstrdefs.cat_together() qcfgs, qstrs = makeqstrdata.parse_input_headers([str(Path(user_data_dir+"/ardupycore/Seeeduino/tools/genhdr/qstrdefs.preprocessed.h")), str(Path(genhdr, "qstrdefs.collected.h"))]) qstrdefs_generated_h = open(Path(genhdr, "qstrdefs.generated.h"), "w") # get config variables cfg_bytes_len = int(qcfgs['BYTES_IN_LEN']) cfg_bytes_hash = int(qcfgs['BYTES_IN_HASH']) # print out the starter of the generated C header file qstrdefs_generated_h.writelines( '// This file was automatically generated by makeqstrdata.py\n') qstrdefs_generated_h.writelines('\n') # add NULL qstr with no hash or data qstrdefs_generated_h.writelines('QDEF(MP_QSTR_NULL, (const byte*)"%s%s" "")\n' % ( '\\x00' * cfg_bytes_hash, '\\x00' * cfg_bytes_len)) # go through each qstr and print it out for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]): qbytes = makeqstrdata.make_bytes( cfg_bytes_len, cfg_bytes_hash, qstr) qstrdefs_generated_h.writelines( 'QDEF(MP_QSTR_%s, %s)\n' % (ident, qbytes)) qstrdefs_generated_h.close() # os.system("cp "+ str(Path(genhdr,"qstrdefs.generated.h"))+" /tmp") self.headerlist.append(str(outputdir)) return genhdr
def freeze_mpy(qcfgs, base_qstrs, raw_codes): cfg_bytes_len = int(qcfgs['BYTES_IN_LEN']) cfg_bytes_hash = int(qcfgs['BYTES_IN_HASH']) # add to qstrs new = {} for q in global_qstrs: # don't add duplicates if q.qstr_esc in base_qstrs or q.qstr_esc in new: continue new[q.qstr_esc] = (len(new), q.qstr_esc, q.str) new = sorted(new.values(), key=lambda x: x[0]) print('#include "py/mpconfig.h"') print('#include "py/objint.h"') print('#include "py/objstr.h"') print('#include "py/emitglue.h"') print() print('#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE') print('#error "MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE not supported with frozen mpy files"') print('#endif') print() print('#if MICROPY_LONGINT_IMPL != %u' % config.MICROPY_LONGINT_IMPL) print('#error "incompatible MICROPY_LONGINT_IMPL"') print('#endif') print() if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ: print('#if MPZ_DIG_SIZE != %u' % config.MPZ_DIG_SIZE) print('#error "incompatible MPZ_DIG_SIZE"') print('#endif') print() print('#if MICROPY_PY_BUILTINS_FLOAT') print('typedef struct _mp_obj_float_t {') print(' mp_obj_base_t base;') print(' mp_float_t value;') print('} mp_obj_float_t;') print('#endif') print() print('enum {') for i in range(len(new)): if i == 0: print(' MP_QSTR_%s = MP_QSTRnumber_of,' % new[i][1]) else: print(' MP_QSTR_%s,' % new[i][1]) print('};') print() print('extern const qstr_pool_t mp_qstr_const_pool;'); print('const qstr_pool_t mp_qstr_frozen_const_pool = {') print(' (qstr_pool_t*)&mp_qstr_const_pool, // previous pool') print(' MP_QSTRnumber_of, // previous pool size') print(' %u, // allocated entries' % len(new)) print(' %u, // used entries' % len(new)) print(' {') for _, _, qstr in new: print(' %s,' % qstrutil.make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr)) print(' },') print('};') for rc in raw_codes: rc.freeze(rc.source_file.str.replace('/', '_')[:-3] + '_') print() print('const char mp_frozen_mpy_names[] = {') for rc in raw_codes: module_name = rc.source_file.str print('"%s\\0"' % module_name) print('"\\0"};') print('const mp_raw_code_t *const mp_frozen_mpy_content[] = {') for rc in raw_codes: print(' &raw_code_%s,' % rc.escaped_name) print('};')
def freeze_mpy(qcfgs, base_qstrs, raw_codes): cfg_bytes_len = int(qcfgs['BYTES_IN_LEN']) cfg_bytes_hash = int(qcfgs['BYTES_IN_HASH']) # add to qstrs new = {} for q in global_qstrs: # don't add duplicates if q.qstr_esc in base_qstrs or q.qstr_esc in new: continue new[q.qstr_esc] = (len(new), q.qstr_esc, q.str) new = sorted(new.values(), key=lambda x: x[0]) print('#include "py/mpconfig.h"') print('#include "py/objint.h"') print('#include "py/objstr.h"') print('#include "py/emitglue.h"') print() print('#if MICROPY_PY_BUILTINS_FLOAT') print('typedef struct _mp_obj_float_t {') print(' mp_obj_base_t base;') print(' mp_float_t value;') print('} mp_obj_float_t;') print('#endif') print() print('enum {') for i in range(len(new)): if i == 0: print(' MP_QSTR_%s = MP_QSTRnumber_of,' % new[i][1]) else: print(' MP_QSTR_%s,' % new[i][1]) print('};') print() print('extern const qstr_pool_t mp_qstr_const_pool;') print('const qstr_pool_t mp_qstr_frozen_const_pool = {') print(' (qstr_pool_t*)&mp_qstr_const_pool, // previous pool') print(' MP_QSTRnumber_of, // previous pool size') print(' %u, // allocated entries' % len(new)) print(' %u, // used entries' % len(new)) print(' {') for _, _, qstr in new: print(' %s,' % qstrutil.make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr)) print(' },') print('};') for rc in raw_codes: rc.freeze(rc.source_file.str.replace('/', '_')[:-3] + '_') print() print('const char mp_frozen_mpy_names[] = {') for rc in raw_codes: module_name = rc.source_file.str[:-len(".py")] slash = module_name.rfind('/') if slash != -1: module_name = module_name[slash + 1:] print('"%s\\0"' % module_name) print('"\\0"};') print('const mp_raw_code_t *const mp_frozen_mpy_content[] = {') for rc in raw_codes: print(' &raw_code_%s,' % rc.escaped_name) print('};')
def freeze_mpy(base_qstrs, raw_codes): # add to qstrs new = {} for q in global_qstrs: # don't add duplicates if q.qstr_esc in base_qstrs or q.qstr_esc in new: continue new[q.qstr_esc] = (len(new), q.qstr_esc, q.str) new = sorted(new.values(), key=lambda x: x[0]) print('#include "py/bc0.h"') print('#include "py/mpconfig.h"') print('#include "py/objint.h"') print('#include "py/objstr.h"') print('#include "py/emitglue.h"') print() print('#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE != %u' % config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) print('#error "incompatible MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE"') print('#endif') print() print('#if MICROPY_LONGINT_IMPL != %u' % config.MICROPY_LONGINT_IMPL) print('#error "incompatible MICROPY_LONGINT_IMPL"') print('#endif') print() if config.MICROPY_LONGINT_IMPL == config.MICROPY_LONGINT_IMPL_MPZ: print('#if MPZ_DIG_SIZE != %u' % config.MPZ_DIG_SIZE) print('#error "incompatible MPZ_DIG_SIZE"') print('#endif') print() print('#if MICROPY_PY_BUILTINS_FLOAT') print('typedef struct _mp_obj_float_t {') print(' mp_obj_base_t base;') print(' mp_float_t value;') print('} mp_obj_float_t;') print('#endif') print() print('#if MICROPY_PY_BUILTINS_COMPLEX') print('typedef struct _mp_obj_complex_t {') print(' mp_obj_base_t base;') print(' mp_float_t real;') print(' mp_float_t imag;') print('} mp_obj_complex_t;') print('#endif') print() print('enum {') for i in range(len(new)): if i == 0: print(' MP_QSTR_%s = MP_QSTRnumber_of,' % new[i][1]) else: print(' MP_QSTR_%s,' % new[i][1]) print('};') print() print('extern const qstr_pool_t mp_qstr_const_pool;') print('const qstr_pool_t mp_qstr_frozen_const_pool = {') print(' (qstr_pool_t*)&mp_qstr_const_pool, // previous pool') print(' MP_QSTRnumber_of, // previous pool size') print(' %u, // allocated entries' % len(new)) print(' %u, // used entries' % len(new)) print(' {') qstr_size = {"metadata": 0, "data": 0} for _, _, qstr in new: qstr_size[ "metadata"] += config.MICROPY_QSTR_BYTES_IN_LEN + config.MICROPY_QSTR_BYTES_IN_HASH qstr_size["data"] += len(qstr) print(' %s,' % qstrutil.make_bytes(config.MICROPY_QSTR_BYTES_IN_LEN, config.MICROPY_QSTR_BYTES_IN_HASH, qstr)) print(' },') print('};') sizes = {} for rc in raw_codes: sizes[rc.source_file.str] = rc.freeze( rc.source_file.str.replace('/', '_')[:-3] + '_') print() print('const char mp_frozen_mpy_names[] = {') qstr_size["filenames"] = 1 for rc in raw_codes: module_name = rc.source_file.str print('"%s\\0"' % module_name) qstr_size["filenames"] += len(module_name) + 1 print('"\\0"};') print('const mp_raw_code_t *const mp_frozen_mpy_content[] = {') for rc in raw_codes: print(' &raw_code_%s,' % rc.escaped_name) size = sizes[rc.source_file.str] print(' // Total size:', sum(size.values())) for k in size: print(" // {} {}".format(k, size[k])) print('};') print() print( '// Total size:', sum([sum(x.values()) for x in sizes.values()]) + sum(qstr_size.values())) for k in size: total = sum([x[k] for x in sizes.values()]) print("// {} {}".format(k, total)) for k in qstr_size: print("// qstr {} {}".format(k, qstr_size[k]))
def freeze_mpy(qcfgs, base_qstrs, raw_codes): cfg_bytes_len = int(qcfgs['BYTES_IN_LEN']) cfg_bytes_hash = int(qcfgs['BYTES_IN_HASH']) # add to qstrs new = {} for q in global_qstrs: # don't add duplicates if q.qstr_esc in base_qstrs or q.qstr_esc in new: continue new[q.qstr_esc] = (len(new), q.qstr_esc, q.str) new = sorted(new.values(), key=lambda x: x[0]) print('#include "py/mpconfig.h"') print('#include "py/objint.h"') print('#include "py/objstr.h"') print('#include "py/emitglue.h"') print() print('#if MICROPY_PY_BUILTINS_FLOAT') print('typedef struct _mp_obj_float_t {') print(' mp_obj_base_t base;') print(' mp_float_t value;') print('} mp_obj_float_t;') print('#endif') print() print('enum {') for i in range(len(new)): if i == 0: print(' MP_QSTR_%s = MP_QSTRnumber_of,' % new[i][1]) else: print(' MP_QSTR_%s,' % new[i][1]) print('};') print() print('extern const qstr_pool_t mp_qstr_const_pool;'); print('const qstr_pool_t mp_qstr_frozen_const_pool = {') print(' (qstr_pool_t*)&mp_qstr_const_pool, // previous pool') print(' MP_QSTRnumber_of, // previous pool size') print(' %u, // allocated entries' % len(new)) print(' %u, // used entries' % len(new)) print(' {') for _, _, qstr in new: print(' %s,' % qstrutil.make_bytes(cfg_bytes_len, cfg_bytes_hash, qstr)) print(' },') print('};') for rc in raw_codes: rc.freeze(rc.source_file.str.replace('/', '_')[:-3] + '_') print() print('const char mp_frozen_mpy_names[] = {') for rc in raw_codes: module_name = rc.source_file.str[:-len(".py")] slash = module_name.rfind('/') if slash != -1: module_name = module_name[slash + 1:] print('"%s\\0"' % module_name) print('"\\0"};') print('const mp_raw_code_t *const mp_frozen_mpy_content[] = {') for rc in raw_codes: print(' &raw_code_%s,' % rc.escaped_name) print('};')