py: Add qstr cfg capability; generate QSTR_NULL and QSTR_ from script.

This commit is contained in:
Damien George 2015-01-11 17:52:45 +00:00
parent e233a55a29
commit 6942f80a8f
4 changed files with 26 additions and 10 deletions

View File

@ -40,12 +40,25 @@ def compute_hash(qstr):
def do_work(infiles): def do_work(infiles):
# read the qstrs in from the input files # read the qstrs in from the input files
qcfgs = {}
qstrs = {} qstrs = {}
for infile in infiles: for infile in infiles:
with open(infile, 'rt') as f: with open(infile, 'rt') as f:
for line in f: for line in f:
line = line.strip()
# is this a config line?
match = re.match(r'^QCFG\((.+), (.+)\)', line)
if match:
value = match.group(2)
if value[0] == '(' and value[-1] == ')':
# strip parenthesis from config value
value = value[1:-1]
qcfgs[match.group(1)] = value
continue
# is this a QSTR line? # is this a QSTR line?
match = re.match(r'^Q\((.+)\)$', line.strip()) match = re.match(r'^Q\((.*)\)$', line)
if not match: if not match:
continue continue
@ -63,11 +76,13 @@ def do_work(infiles):
# process the qstrs, printing out the generated C header file # process the qstrs, printing out the generated C header file
print('// This file was automatically generated by makeqstrdata.py') print('// This file was automatically generated by makeqstrdata.py')
print('') print('')
# add NULL qstr with no hash or data
print('QDEF(MP_QSTR_NULL, (const byte*)"\\x00\\x00\\x00\\x00" "")')
for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]): for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
qhash = compute_hash(qstr) qhash = compute_hash(qstr)
qlen = len(qstr) qlen = len(qstr)
qdata = qstr.replace('"', '\\"') qdata = qstr.replace('"', '\\"')
print('Q(%s, (const byte*)"\\x%02x\\x%02x\\x%02x\\x%02x" "%s")' % (ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qdata)) 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))
return True return True

View File

@ -75,11 +75,9 @@ STATIC const qstr_pool_t const_pool = {
10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below) 10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below)
MP_QSTR_number_of, // corresponds to number of strings in array just below MP_QSTR_number_of, // corresponds to number of strings in array just below
{ {
(const byte*) "\0\0\0\0", // invalid/no qstr has empty data #define QDEF(id, str) str,
(const byte*) "\x05\x15\0\0", // empty qstr with hash=5381=0x1505
#define Q(id, str) str,
#include "genhdr/qstrdefs.generated.h" #include "genhdr/qstrdefs.generated.h"
#undef Q #undef QDEF
}, },
}; };

View File

@ -35,12 +35,11 @@
// Note: it would be possible to define MP_QSTR_xxx as qstr_from_str_static("xxx") // Note: it would be possible to define MP_QSTR_xxx as qstr_from_str_static("xxx")
// for qstrs that are referenced this way, but you don't want to have them in ROM. // for qstrs that are referenced this way, but you don't want to have them in ROM.
// first entry in enum will be MP_QSTR_NULL=0, which indicates invalid/no qstr
enum { enum {
MP_QSTR_NULL = 0, // indicates invalid/no qstr #define QDEF(id, str) id,
MP_QSTR_ = 1, // the empty qstr
#define Q(id, str) MP_QSTR_##id,
#include "genhdr/qstrdefs.generated.h" #include "genhdr/qstrdefs.generated.h"
#undef Q #undef QDEF
MP_QSTR_number_of, MP_QSTR_number_of,
}; };

View File

@ -29,6 +29,10 @@
// All the qstr definitions in this file are available as constants. // All the qstr definitions in this file are available as constants.
// That is, they are in ROM and you can reference them simply as MP_QSTR_xxxx. // That is, they are in ROM and you can reference them simply as MP_QSTR_xxxx.
// qstr configuration passed to makeqstrdata.py of the form QCFG(key, value)
//QCFG(somekey, somevalue)
Q()
Q(*) Q(*)
Q(__build_class__) Q(__build_class__)
Q(__class__) Q(__class__)