diff options
author | Damien George <damien.p.george@gmail.com> | 2015-01-11 22:27:30 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-01-11 22:27:30 +0000 |
commit | 95836f8439b9f1ca0b6ff20f56a03253aa9ba836 (patch) | |
tree | c9fa6b9d1c2aab8d9db9cf1884176a7b4b67c3d5 /py/makeqstrdata.py | |
parent | 6942f80a8feb521ff4dc2b457fa5cb3d039bee54 (diff) |
py: Add MICROPY_QSTR_BYTES_IN_LEN config option, defaulting to 1.
This new config option sets how many fixed-number-of-bytes to use to
store the length of each qstr. Previously this was hard coded to 2,
but, as per issue #1056, this is considered overkill since no-one
needs identifiers longer than 255 bytes.
With this patch the number of bytes for the length is configurable, and
defaults to 1 byte. The configuration option filters through to the
makeqstrdata.py script.
Code size savings going from 2 to 1 byte:
- unix x64 down by 592 bytes
- stmhal down by 1148 bytes
- bare-arm down by 284 bytes
Also has RAM savings, and will be slightly more efficient in execution.
Diffstat (limited to 'py/makeqstrdata.py')
-rw-r--r-- | py/makeqstrdata.py | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py index c8e998ed1..48fd9e09d 100644 --- a/py/makeqstrdata.py +++ b/py/makeqstrdata.py @@ -73,16 +73,27 @@ def do_work(infiles): # add the qstr to the list, with order number to retain original order in file qstrs[ident] = (len(qstrs), ident, qstr) - # process the qstrs, printing out the generated C header file + # get config variables + cfg_bytes_len = int(qcfgs['BYTES_IN_LEN']) + cfg_max_len = 1 << (8 * cfg_bytes_len) + + # print out the starte of the generated C header file print('// This file was automatically generated by makeqstrdata.py') print('') + # add NULL qstr with no hash or data - print('QDEF(MP_QSTR_NULL, (const byte*)"\\x00\\x00\\x00\\x00" "")') + print('QDEF(MP_QSTR_NULL, (const byte*)"\\x00\\x00%s" "")' % ('\\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]): qhash = compute_hash(qstr) qlen = len(qstr) qdata = qstr.replace('"', '\\"') - print('QDEF(MP_QSTR_%s, (const byte*)"\\x%02x\\x%02x\\x%02x\\x%02x" "%s")' % (ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qdata)) + if qlen >= cfg_max_len: + print('qstr is too long:', qstr) + assert False + qlen_str = ('\\x%02x' * cfg_bytes_len) % tuple(qlen.to_bytes(cfg_bytes_len, 'little')) + print('QDEF(MP_QSTR_%s, (const byte*)"\\x%02x\\x%02x%s" "%s")' % (ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen_str, qdata)) return True |